OSDN Git Service

2007-08-16 Robert Dewar <dewar@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_ch3.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              S E M _ C H 3                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2007, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Checks;   use Checks;
29 with Debug;    use Debug;
30 with Elists;   use Elists;
31 with Einfo;    use Einfo;
32 with Errout;   use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3;  use Exp_Ch3;
35 with Exp_Dist; use Exp_Dist;
36 with Exp_Tss;  use Exp_Tss;
37 with Exp_Util; use Exp_Util;
38 with Freeze;   use Freeze;
39 with Itypes;   use Itypes;
40 with Layout;   use Layout;
41 with Lib;      use Lib;
42 with Lib.Xref; use Lib.Xref;
43 with Namet;    use Namet;
44 with Nmake;    use Nmake;
45 with Opt;      use Opt;
46 with Restrict; use Restrict;
47 with Rident;   use Rident;
48 with Rtsfind;  use Rtsfind;
49 with Sem;      use Sem;
50 with Sem_Case; use Sem_Case;
51 with Sem_Cat;  use Sem_Cat;
52 with Sem_Ch6;  use Sem_Ch6;
53 with Sem_Ch7;  use Sem_Ch7;
54 with Sem_Ch8;  use Sem_Ch8;
55 with Sem_Ch13; use Sem_Ch13;
56 with Sem_Disp; use Sem_Disp;
57 with Sem_Dist; use Sem_Dist;
58 with Sem_Elim; use Sem_Elim;
59 with Sem_Eval; use Sem_Eval;
60 with Sem_Mech; use Sem_Mech;
61 with Sem_Res;  use Sem_Res;
62 with Sem_Smem; use Sem_Smem;
63 with Sem_Type; use Sem_Type;
64 with Sem_Util; use Sem_Util;
65 with Sem_Warn; use Sem_Warn;
66 with Stand;    use Stand;
67 with Sinfo;    use Sinfo;
68 with Snames;   use Snames;
69 with Targparm; use Targparm;
70 with Tbuild;   use Tbuild;
71 with Ttypes;   use Ttypes;
72 with Uintp;    use Uintp;
73 with Urealp;   use Urealp;
74
75 package body Sem_Ch3 is
76
77    -----------------------
78    -- Local Subprograms --
79    -----------------------
80
81    procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
82    --  Ada 2005 (AI-251): Add the tag components corresponding to all the
83    --  abstract interface types implemented by a record type or a derived
84    --  record type.
85
86    procedure Build_Derived_Type
87      (N             : Node_Id;
88       Parent_Type   : Entity_Id;
89       Derived_Type  : Entity_Id;
90       Is_Completion : Boolean;
91       Derive_Subps  : Boolean := True);
92    --  Create and decorate a Derived_Type given the Parent_Type entity. N is
93    --  the N_Full_Type_Declaration node containing the derived type definition.
94    --  Parent_Type is the entity for the parent type in the derived type
95    --  definition and Derived_Type the actual derived type. Is_Completion must
96    --  be set to False if Derived_Type is the N_Defining_Identifier node in N
97    --  (ie Derived_Type = Defining_Identifier (N)). In this case N is not the
98    --  completion of a private type declaration. If Is_Completion is set to
99    --  True, N is the completion of a private type declaration and Derived_Type
100    --  is different from the defining identifier inside N (i.e. Derived_Type /=
101    --  Defining_Identifier (N)). Derive_Subps indicates whether the parent
102    --  subprograms should be derived. The only case where this parameter is
103    --  False is when Build_Derived_Type is recursively called to process an
104    --  implicit derived full type for a type derived from a private type (in
105    --  that case the subprograms must only be derived for the private view of
106    --  the type).
107    --
108    --  ??? These flags need a bit of re-examination and re-documentation:
109    --  ???  are they both necessary (both seem related to the recursion)?
110
111    procedure Build_Derived_Access_Type
112      (N            : Node_Id;
113       Parent_Type  : Entity_Id;
114       Derived_Type : Entity_Id);
115    --  Subsidiary procedure to Build_Derived_Type. For a derived access type,
116    --  create an implicit base if the parent type is constrained or if the
117    --  subtype indication has a constraint.
118
119    procedure Build_Derived_Array_Type
120      (N            : Node_Id;
121       Parent_Type  : Entity_Id;
122       Derived_Type : Entity_Id);
123    --  Subsidiary procedure to Build_Derived_Type. For a derived array type,
124    --  create an implicit base if the parent type is constrained or if the
125    --  subtype indication has a constraint.
126
127    procedure Build_Derived_Concurrent_Type
128      (N            : Node_Id;
129       Parent_Type  : Entity_Id;
130       Derived_Type : Entity_Id);
131    --  Subsidiary procedure to Build_Derived_Type. For a derived task or
132    --  protected type, inherit entries and protected subprograms, check
133    --  legality of discriminant constraints if any.
134
135    procedure Build_Derived_Enumeration_Type
136      (N            : Node_Id;
137       Parent_Type  : Entity_Id;
138       Derived_Type : Entity_Id);
139    --  Subsidiary procedure to Build_Derived_Type. For a derived enumeration
140    --  type, we must create a new list of literals. Types derived from
141    --  Character and Wide_Character are special-cased.
142
143    procedure Build_Derived_Numeric_Type
144      (N            : Node_Id;
145       Parent_Type  : Entity_Id;
146       Derived_Type : Entity_Id);
147    --  Subsidiary procedure to Build_Derived_Type. For numeric types, create
148    --  an anonymous base type, and propagate constraint to subtype if needed.
149
150    procedure Build_Derived_Private_Type
151      (N             : Node_Id;
152       Parent_Type   : Entity_Id;
153       Derived_Type  : Entity_Id;
154       Is_Completion : Boolean;
155       Derive_Subps  : Boolean := True);
156    --  Subsidiary procedure to Build_Derived_Type. This procedure is complex
157    --  because the parent may or may not have a completion, and the derivation
158    --  may itself be a completion.
159
160    procedure Build_Derived_Record_Type
161      (N            : Node_Id;
162       Parent_Type  : Entity_Id;
163       Derived_Type : Entity_Id;
164       Derive_Subps : Boolean := True);
165    --  Subsidiary procedure for Build_Derived_Type and
166    --  Analyze_Private_Extension_Declaration used for tagged and untagged
167    --  record types. All parameters are as in Build_Derived_Type except that
168    --  N, in addition to being an N_Full_Type_Declaration node, can also be an
169    --  N_Private_Extension_Declaration node. See the definition of this routine
170    --  for much more info. Derive_Subps indicates whether subprograms should
171    --  be derived from the parent type. The only case where Derive_Subps is
172    --  False is for an implicit derived full type for a type derived from a
173    --  private type (see Build_Derived_Type).
174
175    procedure Build_Discriminal (Discrim : Entity_Id);
176    --  Create the discriminal corresponding to discriminant Discrim, that is
177    --  the parameter corresponding to Discrim to be used in initialization
178    --  procedures for the type where Discrim is a discriminant. Discriminals
179    --  are not used during semantic analysis, and are not fully defined
180    --  entities until expansion. Thus they are not given a scope until
181    --  initialization procedures are built.
182
183    function Build_Discriminant_Constraints
184      (T           : Entity_Id;
185       Def         : Node_Id;
186       Derived_Def : Boolean := False) return Elist_Id;
187    --  Validate discriminant constraints and return the list of the constraints
188    --  in order of discriminant declarations, where T is the discriminated
189    --  unconstrained type. Def is the N_Subtype_Indication node where the
190    --  discriminants constraints for T are specified. Derived_Def is True
191    --  when building the discriminant constraints in a derived type definition
192    --  of the form "type D (...) is new T (xxx)". In this case T is the parent
193    --  type and Def is the constraint "(xxx)" on T and this routine sets the
194    --  Corresponding_Discriminant field of the discriminants in the derived
195    --  type D to point to the corresponding discriminants in the parent type T.
196
197    procedure Build_Discriminated_Subtype
198      (T           : Entity_Id;
199       Def_Id      : Entity_Id;
200       Elist       : Elist_Id;
201       Related_Nod : Node_Id;
202       For_Access  : Boolean := False);
203    --  Subsidiary procedure to Constrain_Discriminated_Type and to
204    --  Process_Incomplete_Dependents. Given
205    --
206    --     T (a possibly discriminated base type)
207    --     Def_Id (a very partially built subtype for T),
208    --
209    --  the call completes Def_Id to be the appropriate E_*_Subtype.
210    --
211    --  The Elist is the list of discriminant constraints if any (it is set
212    --  to No_Elist if T is not a discriminated type, and to an empty list if
213    --  T has discriminants but there are no discriminant constraints). The
214    --  Related_Nod is the same as Decl_Node in Create_Constrained_Components.
215    --  The For_Access says whether or not this subtype is really constraining
216    --  an access type. That is its sole purpose is the designated type of an
217    --  access type -- in which case a Private_Subtype Is_For_Access_Subtype
218    --  is built to avoid freezing T when the access subtype is frozen.
219
220    function Build_Scalar_Bound
221      (Bound : Node_Id;
222       Par_T : Entity_Id;
223       Der_T : Entity_Id) return Node_Id;
224    --  The bounds of a derived scalar type are conversions of the bounds of
225    --  the parent type. Optimize the representation if the bounds are literals.
226    --  Needs a more complete spec--what are the parameters exactly, and what
227    --  exactly is the returned value, and how is Bound affected???
228
229    procedure Build_Itype_Reference
230      (Ityp : Entity_Id;
231       Nod  : Node_Id);
232    --  Create a reference to an internal type, for use by Gigi. The back-end
233    --  elaborates itypes on demand, i.e. when their first use is seen. This
234    --  can lead to scope anomalies if the first use is within a scope that is
235    --  nested within the scope that contains  the point of definition of the
236    --  itype. The Itype_Reference node forces the elaboration of the itype
237    --  in the proper scope. The node is inserted after Nod, which is the
238    --  enclosing declaration that generated Ityp.
239    --  A related mechanism is used during expansion, for itypes created in
240    --  branches of conditionals. See Ensure_Defined in exp_util.
241    --  Could both mechanisms be merged ???
242
243    procedure Build_Underlying_Full_View
244      (N   : Node_Id;
245       Typ : Entity_Id;
246       Par : Entity_Id);
247    --  If the completion of a private type is itself derived from a private
248    --  type, or if the full view of a private subtype is itself private, the
249    --  back-end has no way to compute the actual size of this type. We build
250    --  an internal subtype declaration of the proper parent type to convey
251    --  this information. This extra mechanism is needed because a full
252    --  view cannot itself have a full view (it would get clobbered during
253    --  view exchanges).
254
255    procedure Check_Abstract_Interfaces (N : Node_Id; Def : Node_Id);
256    --  Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
257
258    procedure Check_Access_Discriminant_Requires_Limited
259      (D   : Node_Id;
260       Loc : Node_Id);
261    --  Check the restriction that the type to which an access discriminant
262    --  belongs must be a concurrent type or a descendant of a type with
263    --  the reserved word 'limited' in its declaration.
264
265    procedure Check_Anonymous_Access_Components
266       (Typ_Decl  : Node_Id;
267        Typ       : Entity_Id;
268        Prev      : Entity_Id;
269        Comp_List : Node_Id);
270    --  Ada 2005 AI-382: an access component in a record definition can refer to
271    --  the enclosing record, in which case it denotes the type itself, and not
272    --  the current instance of the type. We create an anonymous access type for
273    --  the component, and flag it as an access to a component, so accessibility
274    --  checks are properly performed on it. The declaration of the access type
275    --  is placed ahead of that of the record to prevent order-of-elaboration
276    --  circularity issues in Gigi. We create an incomplete type for the record
277    --  declaration, which is the designated type of the anonymous access.
278
279    procedure Check_Delta_Expression (E : Node_Id);
280    --  Check that the expression represented by E is suitable for use as a
281    --  delta expression, i.e. it is of real type and is static.
282
283    procedure Check_Digits_Expression (E : Node_Id);
284    --  Check that the expression represented by E is suitable for use as a
285    --  digits expression, i.e. it is of integer type, positive and static.
286
287    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
288    --  Validate the initialization of an object declaration. T is the required
289    --  type, and Exp is the initialization expression.
290
291    procedure Check_Or_Process_Discriminants
292      (N    : Node_Id;
293       T    : Entity_Id;
294       Prev : Entity_Id := Empty);
295    --  If T is the full declaration of an incomplete or private type, check the
296    --  conformance of the discriminants, otherwise process them. Prev is the
297    --  entity of the partial declaration, if any.
298
299    procedure Check_Real_Bound (Bound : Node_Id);
300    --  Check given bound for being of real type and static. If not, post an
301    --  appropriate message, and rewrite the bound with the real literal zero.
302
303    procedure Constant_Redeclaration
304      (Id : Entity_Id;
305       N  : Node_Id;
306       T  : out Entity_Id);
307    --  Various checks on legality of full declaration of deferred constant.
308    --  Id is the entity for the redeclaration, N is the N_Object_Declaration,
309    --  node. The caller has not yet set any attributes of this entity.
310
311    function Contain_Interface
312      (Iface  : Entity_Id;
313       Ifaces : Elist_Id) return Boolean;
314    --  Ada 2005: Determine whether Iface is present in the list Ifaces
315
316    procedure Convert_Scalar_Bounds
317      (N            : Node_Id;
318       Parent_Type  : Entity_Id;
319       Derived_Type : Entity_Id;
320       Loc          : Source_Ptr);
321    --  For derived scalar types, convert the bounds in the type definition to
322    --  the derived type, and complete their analysis. Given a constraint of the
323    --  form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
324    --  T'Base, the parent_type. The bounds of the derived type (the anonymous
325    --  base) are copies of Lo and Hi. Finally, the bounds of the derived
326    --  subtype are conversions of those bounds to the derived_type, so that
327    --  their typing is consistent.
328
329    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
330    --  Copies attributes from array base type T2 to array base type T1. Copies
331    --  only attributes that apply to base types, but not subtypes.
332
333    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
334    --  Copies attributes from array subtype T2 to array subtype T1. Copies
335    --  attributes that apply to both subtypes and base types.
336
337    procedure Create_Constrained_Components
338      (Subt        : Entity_Id;
339       Decl_Node   : Node_Id;
340       Typ         : Entity_Id;
341       Constraints : Elist_Id);
342    --  Build the list of entities for a constrained discriminated record
343    --  subtype. If a component depends on a discriminant, replace its subtype
344    --  using the discriminant values in the discriminant constraint. Subt is
345    --  the defining identifier for the subtype whose list of constrained
346    --  entities we will create. Decl_Node is the type declaration node where we
347    --  will attach all the itypes created. Typ is the base discriminated type
348    --  for the subtype Subt. Constraints is the list of discriminant
349    --  constraints for Typ.
350
351    function Constrain_Component_Type
352      (Comp            : Entity_Id;
353       Constrained_Typ : Entity_Id;
354       Related_Node    : Node_Id;
355       Typ             : Entity_Id;
356       Constraints     : Elist_Id) return Entity_Id;
357    --  Given a discriminated base type Typ, a list of discriminant constraint
358    --  Constraints for Typ and a component of Typ, with type Compon_Type,
359    --  create and return the type corresponding to Compon_type where all
360    --  discriminant references are replaced with the corresponding constraint.
361    --  If no discriminant references occur in Compon_Typ then return it as is.
362    --  Constrained_Typ is the final constrained subtype to which the
363    --  constrained Compon_Type belongs. Related_Node is the node where we will
364    --  attach all the itypes created.
365    --  Above description is confused, what is Compon_Type???
366
367    procedure Constrain_Access
368      (Def_Id      : in out Entity_Id;
369       S           : Node_Id;
370       Related_Nod : Node_Id);
371    --  Apply a list of constraints to an access type. If Def_Id is empty, it is
372    --  an anonymous type created for a subtype indication. In that case it is
373    --  created in the procedure and attached to Related_Nod.
374
375    procedure Constrain_Array
376      (Def_Id      : in out Entity_Id;
377       SI          : Node_Id;
378       Related_Nod : Node_Id;
379       Related_Id  : Entity_Id;
380       Suffix      : Character);
381    --  Apply a list of index constraints to an unconstrained array type. The
382    --  first parameter is the entity for the resulting subtype. A value of
383    --  Empty for Def_Id indicates that an implicit type must be created, but
384    --  creation is delayed (and must be done by this procedure) because other
385    --  subsidiary implicit types must be created first (which is why Def_Id
386    --  is an in/out parameter). The second parameter is a subtype indication
387    --  node for the constrained array to be created (e.g. something of the
388    --  form string (1 .. 10)). Related_Nod gives the place where this type
389    --  has to be inserted in the tree. The Related_Id and Suffix parameters
390    --  are used to build the associated Implicit type name.
391
392    procedure Constrain_Concurrent
393      (Def_Id      : in out Entity_Id;
394       SI          : Node_Id;
395       Related_Nod : Node_Id;
396       Related_Id  : Entity_Id;
397       Suffix      : Character);
398    --  Apply list of discriminant constraints to an unconstrained concurrent
399    --  type.
400    --
401    --    SI is the N_Subtype_Indication node containing the constraint and
402    --    the unconstrained type to constrain.
403    --
404    --    Def_Id is the entity for the resulting constrained subtype. A value
405    --    of Empty for Def_Id indicates that an implicit type must be created,
406    --    but creation is delayed (and must be done by this procedure) because
407    --    other subsidiary implicit types must be created first (which is why
408    --    Def_Id is an in/out parameter).
409    --
410    --    Related_Nod gives the place where this type has to be inserted
411    --    in the tree
412    --
413    --  The last two arguments are used to create its external name if needed.
414
415    function Constrain_Corresponding_Record
416      (Prot_Subt   : Entity_Id;
417       Corr_Rec    : Entity_Id;
418       Related_Nod : Node_Id;
419       Related_Id  : Entity_Id) return Entity_Id;
420    --  When constraining a protected type or task type with discriminants,
421    --  constrain the corresponding record with the same discriminant values.
422
423    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
424    --  Constrain a decimal fixed point type with a digits constraint and/or a
425    --  range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
426
427    procedure Constrain_Discriminated_Type
428      (Def_Id      : Entity_Id;
429       S           : Node_Id;
430       Related_Nod : Node_Id;
431       For_Access  : Boolean := False);
432    --  Process discriminant constraints of composite type. Verify that values
433    --  have been provided for all discriminants, that the original type is
434    --  unconstrained, and that the types of the supplied expressions match
435    --  the discriminant types. The first three parameters are like in routine
436    --  Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
437    --  of For_Access.
438
439    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
440    --  Constrain an enumeration type with a range constraint. This is identical
441    --  to Constrain_Integer, but for the Ekind of the resulting subtype.
442
443    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
444    --  Constrain a floating point type with either a digits constraint
445    --  and/or a range constraint, building a E_Floating_Point_Subtype.
446
447    procedure Constrain_Index
448      (Index        : Node_Id;
449       S            : Node_Id;
450       Related_Nod  : Node_Id;
451       Related_Id   : Entity_Id;
452       Suffix       : Character;
453       Suffix_Index : Nat);
454    --  Process an index constraint in a constrained array declaration. The
455    --  constraint can be a subtype name, or a range with or without an explicit
456    --  subtype mark. The index is the corresponding index of the unconstrained
457    --  array. The Related_Id and Suffix parameters are used to build the
458    --  associated Implicit type name.
459
460    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
461    --  Build subtype of a signed or modular integer type
462
463    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
464    --  Constrain an ordinary fixed point type with a range constraint, and
465    --  build an E_Ordinary_Fixed_Point_Subtype entity.
466
467    procedure Copy_And_Swap (Priv, Full : Entity_Id);
468    --  Copy the Priv entity into the entity of its full declaration then swap
469    --  the two entities in such a manner that the former private type is now
470    --  seen as a full type.
471
472    procedure Decimal_Fixed_Point_Type_Declaration
473      (T   : Entity_Id;
474       Def : Node_Id);
475    --  Create a new decimal fixed point type, and apply the constraint to
476    --  obtain a subtype of this new type.
477
478    procedure Complete_Private_Subtype
479      (Priv        : Entity_Id;
480       Full        : Entity_Id;
481       Full_Base   : Entity_Id;
482       Related_Nod : Node_Id);
483    --  Complete the implicit full view of a private subtype by setting the
484    --  appropriate semantic fields. If the full view of the parent is a record
485    --  type, build constrained components of subtype.
486
487    procedure Derive_Interface_Subprograms
488      (Parent_Type : Entity_Id;
489       Tagged_Type : Entity_Id;
490       Ifaces_List : Elist_Id);
491    --  Ada 2005 (AI-251): Derive primitives of abstract interface types that
492    --  are not immediate ancestors of Tagged type and associate them their
493    --  aliased primitive. Ifaces_List contains the abstract interface
494    --  primitives that have been derived from Parent_Type.
495
496    procedure Derived_Standard_Character
497      (N             : Node_Id;
498       Parent_Type   : Entity_Id;
499       Derived_Type  : Entity_Id);
500    --  Subsidiary procedure to Build_Derived_Enumeration_Type which handles
501    --  derivations from types Standard.Character and Standard.Wide_Character.
502
503    procedure Derived_Type_Declaration
504      (T             : Entity_Id;
505       N             : Node_Id;
506       Is_Completion : Boolean);
507    --  Process a derived type declaration. This routine will invoke
508    --  Build_Derived_Type to process the actual derived type definition.
509    --  Parameters N and Is_Completion have the same meaning as in
510    --  Build_Derived_Type. T is the N_Defining_Identifier for the entity
511    --  defined in the N_Full_Type_Declaration node N, that is T is the derived
512    --  type.
513
514    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
515    --  Insert each literal in symbol table, as an overloadable identifier. Each
516    --  enumeration type is mapped into a sequence of integers, and each literal
517    --  is defined as a constant with integer value. If any of the literals are
518    --  character literals, the type is a character type, which means that
519    --  strings are legal aggregates for arrays of components of the type.
520
521    function Expand_To_Stored_Constraint
522      (Typ        : Entity_Id;
523       Constraint : Elist_Id) return Elist_Id;
524    --  Given a Constraint (i.e. a list of expressions) on the discriminants of
525    --  Typ, expand it into a constraint on the stored discriminants and return
526    --  the new list of expressions constraining the stored discriminants.
527
528    function Find_Type_Of_Object
529      (Obj_Def     : Node_Id;
530       Related_Nod : Node_Id) return Entity_Id;
531    --  Get type entity for object referenced by Obj_Def, attaching the
532    --  implicit types generated to Related_Nod
533
534    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
535    --  Create a new float, and apply the constraint to obtain subtype of it
536
537    function Has_Range_Constraint (N : Node_Id) return Boolean;
538    --  Given an N_Subtype_Indication node N, return True if a range constraint
539    --  is present, either directly, or as part of a digits or delta constraint.
540    --  In addition, a digits constraint in the decimal case returns True, since
541    --  it establishes a default range if no explicit range is present.
542
543    function Inherit_Components
544      (N             : Node_Id;
545       Parent_Base   : Entity_Id;
546       Derived_Base  : Entity_Id;
547       Is_Tagged     : Boolean;
548       Inherit_Discr : Boolean;
549       Discs         : Elist_Id) return Elist_Id;
550    --  Called from Build_Derived_Record_Type to inherit the components of
551    --  Parent_Base (a base type) into the Derived_Base (the derived base type).
552    --  For more information on derived types and component inheritance please
553    --  consult the comment above the body of Build_Derived_Record_Type.
554    --
555    --    N is the original derived type declaration
556    --
557    --    Is_Tagged is set if we are dealing with tagged types
558    --
559    --    If Inherit_Discr is set, Derived_Base inherits its discriminants from
560    --    Parent_Base, otherwise no discriminants are inherited.
561    --
562    --    Discs gives the list of constraints that apply to Parent_Base in the
563    --    derived type declaration. If Discs is set to No_Elist, then we have
564    --    the following situation:
565    --
566    --      type Parent (D1..Dn : ..) is [tagged] record ...;
567    --      type Derived is new Parent [with ...];
568    --
569    --    which gets treated as
570    --
571    --      type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
572    --
573    --  For untagged types the returned value is an association list. The list
574    --  starts from the association (Parent_Base => Derived_Base), and then it
575    --  contains a sequence of the associations of the form
576    --
577    --    (Old_Component => New_Component),
578    --
579    --  where Old_Component is the Entity_Id of a component in Parent_Base and
580    --  New_Component is the Entity_Id of the corresponding component in
581    --  Derived_Base. For untagged records, this association list is needed when
582    --  copying the record declaration for the derived base. In the tagged case
583    --  the value returned is irrelevant.
584
585    function Is_Valid_Constraint_Kind
586      (T_Kind          : Type_Kind;
587       Constraint_Kind : Node_Kind) return Boolean;
588    --  Returns True if it is legal to apply the given kind of constraint to the
589    --  given kind of type (index constraint to an array type, for example).
590
591    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
592    --  Create new modular type. Verify that modulus is in  bounds and is
593    --  a power of two (implementation restriction).
594
595    procedure New_Concatenation_Op (Typ : Entity_Id);
596    --  Create an abbreviated declaration for an operator in order to
597    --  materialize concatenation on array types.
598
599    procedure Ordinary_Fixed_Point_Type_Declaration
600      (T   : Entity_Id;
601       Def : Node_Id);
602    --  Create a new ordinary fixed point type, and apply the constraint to
603    --  obtain subtype of it.
604
605    procedure Prepare_Private_Subtype_Completion
606      (Id          : Entity_Id;
607       Related_Nod : Node_Id);
608    --  Id is a subtype of some private type. Creates the full declaration
609    --  associated with Id whenever possible, i.e. when the full declaration
610    --  of the base type is already known. Records each subtype into
611    --  Private_Dependents of the base type.
612
613    procedure Process_Incomplete_Dependents
614      (N      : Node_Id;
615       Full_T : Entity_Id;
616       Inc_T  : Entity_Id);
617    --  Process all entities that depend on an incomplete type. There include
618    --  subtypes, subprogram types that mention the incomplete type in their
619    --  profiles, and subprogram with access parameters that designate the
620    --  incomplete type.
621
622    --  Inc_T is the defining identifier of an incomplete type declaration, its
623    --  Ekind is E_Incomplete_Type.
624    --
625    --    N is the corresponding N_Full_Type_Declaration for Inc_T.
626    --
627    --    Full_T is N's defining identifier.
628    --
629    --  Subtypes of incomplete types with discriminants are completed when the
630    --  parent type is. This is simpler than private subtypes, because they can
631    --  only appear in the same scope, and there is no need to exchange views.
632    --  Similarly, access_to_subprogram types may have a parameter or a return
633    --  type that is an incomplete type, and that must be replaced with the
634    --  full type.
635
636    --  If the full type is tagged, subprogram with access parameters that
637    --  designated the incomplete may be primitive operations of the full type,
638    --  and have to be processed accordingly.
639
640    procedure Process_Real_Range_Specification (Def : Node_Id);
641    --  Given the type definition for a real type, this procedure processes
642    --  and checks the real range specification of this type definition if
643    --  one is present. If errors are found, error messages are posted, and
644    --  the Real_Range_Specification of Def is reset to Empty.
645
646    procedure Record_Type_Declaration
647      (T    : Entity_Id;
648       N    : Node_Id;
649       Prev : Entity_Id);
650    --  Process a record type declaration (for both untagged and tagged
651    --  records). Parameters T and N are exactly like in procedure
652    --  Derived_Type_Declaration, except that no flag Is_Completion is needed
653    --  for this routine. If this is the completion of an incomplete type
654    --  declaration, Prev is the entity of the incomplete declaration, used for
655    --  cross-referencing. Otherwise Prev = T.
656
657    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
658    --  This routine is used to process the actual record type definition
659    --  (both for untagged and tagged records). Def is a record type
660    --  definition node. This procedure analyzes the components in this
661    --  record type definition. Prev_T is the entity for the enclosing record
662    --  type. It is provided so that its Has_Task flag can be set if any of
663    --  the component have Has_Task set. If the declaration is the completion
664    --  of an incomplete type declaration, Prev_T is the original incomplete
665    --  type, whose full view is the record type.
666
667    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
668    --  Subsidiary to Build_Derived_Record_Type. For untagged records, we
669    --  build a copy of the declaration tree of the parent, and we create
670    --  independently the list of components for the derived type. Semantic
671    --  information uses the component entities, but record representation
672    --  clauses are validated on the declaration tree. This procedure replaces
673    --  discriminants and components in the declaration with those that have
674    --  been created by Inherit_Components.
675
676    procedure Set_Fixed_Range
677      (E   : Entity_Id;
678       Loc : Source_Ptr;
679       Lo  : Ureal;
680       Hi  : Ureal);
681    --  Build a range node with the given bounds and set it as the Scalar_Range
682    --  of the given fixed-point type entity. Loc is the source location used
683    --  for the constructed range. See body for further details.
684
685    procedure Set_Scalar_Range_For_Subtype
686      (Def_Id : Entity_Id;
687       R      : Node_Id;
688       Subt   : Entity_Id);
689    --  This routine is used to set the scalar range field for a subtype given
690    --  Def_Id, the entity for the subtype, and R, the range expression for the
691    --  scalar range. Subt provides the parent subtype to be used to analyze,
692    --  resolve, and check the given range.
693
694    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
695    --  Create a new signed integer entity, and apply the constraint to obtain
696    --  the required first named subtype of this type.
697
698    procedure Set_Stored_Constraint_From_Discriminant_Constraint
699      (E : Entity_Id);
700    --  E is some record type. This routine computes E's Stored_Constraint
701    --  from its Discriminant_Constraint.
702
703    -----------------------
704    -- Access_Definition --
705    -----------------------
706
707    function Access_Definition
708      (Related_Nod : Node_Id;
709       N           : Node_Id) return Entity_Id
710    is
711       Loc        : constant Source_Ptr := Sloc (Related_Nod);
712       Anon_Type  : Entity_Id;
713       Anon_Scope : Entity_Id;
714       Desig_Type : Entity_Id;
715       Decl       : Entity_Id;
716
717    begin
718       if Is_Entry (Current_Scope)
719         and then Is_Task_Type (Etype (Scope (Current_Scope)))
720       then
721          Error_Msg_N ("task entries cannot have access parameters", N);
722          return Empty;
723       end if;
724
725       --  Ada 2005: for an object declaration the corresponding anonymous
726       --  type is declared in the current scope.
727
728       --  If the access definition is the return type of another access to
729       --  function, scope is the current one, because it is the one of the
730       --  current type declaration.
731
732       if Nkind (Related_Nod) = N_Object_Declaration
733         or else Nkind (Related_Nod) = N_Access_Function_Definition
734       then
735          Anon_Scope := Current_Scope;
736
737       --  For the anonymous function result case, retrieve the scope of the
738       --  function specification's associated entity rather than using the
739       --  current scope. The current scope will be the function itself if the
740       --  formal part is currently being analyzed, but will be the parent scope
741       --  in the case of a parameterless function, and we always want to use
742       --  the function's parent scope. Finally, if the function is a child
743       --  unit, we must traverse the the tree to retrieve the proper entity.
744
745       elsif Nkind (Related_Nod) = N_Function_Specification
746          and then Nkind (Parent (N)) /= N_Parameter_Specification
747       then
748          --  If the current scope is a protected type, the anonymous access
749          --  is associated with one of the protected operations, and must
750          --  be available in the scope that encloses the protected declaration.
751          --  Otherwise the type is is in the scope enclosing the subprogram.
752
753          if Ekind (Current_Scope) = E_Protected_Type then
754             Anon_Scope := Scope (Scope (Defining_Entity (Related_Nod)));
755          else
756             Anon_Scope := Scope (Defining_Entity (Related_Nod));
757          end if;
758
759       else
760          --  For access formals, access components, and access discriminants,
761          --  the scope is that of the enclosing declaration,
762
763          Anon_Scope := Scope (Current_Scope);
764       end if;
765
766       Anon_Type :=
767         Create_Itype
768          (E_Anonymous_Access_Type, Related_Nod, Scope_Id =>  Anon_Scope);
769
770       if All_Present (N)
771         and then Ada_Version >= Ada_05
772       then
773          Error_Msg_N ("ALL is not permitted for anonymous access types", N);
774       end if;
775
776       --  Ada 2005 (AI-254): In case of anonymous access to subprograms call
777       --  the corresponding semantic routine
778
779       if Present (Access_To_Subprogram_Definition (N)) then
780          Access_Subprogram_Declaration
781            (T_Name => Anon_Type,
782             T_Def  => Access_To_Subprogram_Definition (N));
783
784          if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
785             Set_Ekind
786               (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
787          else
788             Set_Ekind
789               (Anon_Type, E_Anonymous_Access_Subprogram_Type);
790          end if;
791
792          --  If the anonymous access is associated with a protected operation
793          --  create a reference to it after the enclosing protected definition
794          --  because the itype will be used in the subsequent bodies.
795
796          if Ekind (Current_Scope) = E_Protected_Type then
797             Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
798          end if;
799
800          return Anon_Type;
801       end if;
802
803       Find_Type (Subtype_Mark (N));
804       Desig_Type := Entity (Subtype_Mark (N));
805
806       Set_Directly_Designated_Type
807                              (Anon_Type, Desig_Type);
808       Set_Etype              (Anon_Type, Anon_Type);
809       Init_Size_Align        (Anon_Type);
810       Set_Depends_On_Private (Anon_Type, Has_Private_Component (Anon_Type));
811
812       --  Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
813       --  from Ada 95 semantics. In Ada 2005, anonymous access must specify if
814       --  the null value is allowed. In Ada 95 the null value is never allowed.
815
816       if Ada_Version >= Ada_05 then
817          Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
818       else
819          Set_Can_Never_Be_Null (Anon_Type, True);
820       end if;
821
822       --  The anonymous access type is as public as the discriminated type or
823       --  subprogram that defines it. It is imported (for back-end purposes)
824       --  if the designated type is.
825
826       Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
827
828       --  Ada 2005 (AI-50217): Propagate the attribute that indicates that the
829       --  designated type comes from the limited view.
830
831       Set_From_With_Type (Anon_Type, From_With_Type (Desig_Type));
832
833       --  Ada 2005 (AI-231): Propagate the access-constant attribute
834
835       Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
836
837       --  The context is either a subprogram declaration, object declaration,
838       --  or an access discriminant, in a private or a full type declaration.
839       --  In the case of a subprogram, if the designated type is incomplete,
840       --  the operation will be a primitive operation of the full type, to be
841       --  updated subsequently. If the type is imported through a limited_with
842       --  clause, the subprogram is not a primitive operation of the type
843       --  (which is declared elsewhere in some other scope).
844
845       if Ekind (Desig_Type) = E_Incomplete_Type
846         and then not From_With_Type (Desig_Type)
847         and then Is_Overloadable (Current_Scope)
848       then
849          Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
850          Set_Has_Delayed_Freeze (Current_Scope);
851       end if;
852
853       --  Ada 2005: if the designated type is an interface that may contain
854       --  tasks, create a Master entity for the declaration. This must be done
855       --  before expansion of the full declaration, because the declaration may
856       --  include an expression that is an allocator, whose expansion needs the
857       --  proper Master for the created tasks.
858
859       if Nkind (Related_Nod) = N_Object_Declaration
860          and then Expander_Active
861       then
862          if Is_Interface (Desig_Type)
863            and then Is_Limited_Record (Desig_Type)
864          then
865             Build_Class_Wide_Master (Anon_Type);
866
867          --  Similarly, if the type is an anonymous access that designates
868          --  tasks, create a master entity for it in the current context.
869
870          elsif Has_Task (Desig_Type)
871            and then Comes_From_Source (Related_Nod)
872          then
873             if not Has_Master_Entity (Current_Scope) then
874                Decl :=
875                  Make_Object_Declaration (Loc,
876                    Defining_Identifier =>
877                      Make_Defining_Identifier (Loc, Name_uMaster),
878                    Constant_Present => True,
879                    Object_Definition =>
880                      New_Reference_To (RTE (RE_Master_Id), Loc),
881                    Expression =>
882                      Make_Explicit_Dereference (Loc,
883                        New_Reference_To (RTE (RE_Current_Master), Loc)));
884
885                Insert_Before (Related_Nod, Decl);
886                Analyze (Decl);
887
888                Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
889                Set_Has_Master_Entity (Current_Scope);
890             else
891                Build_Master_Renaming (Related_Nod, Anon_Type);
892             end if;
893          end if;
894       end if;
895
896       --  For a private component of a protected type, it is imperative that
897       --  the back-end elaborate the type immediately after the protected
898       --  declaration, because this type will be used in the declarations
899       --  created for the component within each protected body, so we must
900       --  create an itype reference for it now.
901
902       if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
903          Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
904       end if;
905
906       return Anon_Type;
907    end Access_Definition;
908
909    -----------------------------------
910    -- Access_Subprogram_Declaration --
911    -----------------------------------
912
913    procedure Access_Subprogram_Declaration
914      (T_Name : Entity_Id;
915       T_Def  : Node_Id)
916    is
917       Formals : constant List_Id := Parameter_Specifications (T_Def);
918       Formal  : Entity_Id;
919       D_Ityp  : Node_Id;
920
921       Desig_Type : constant Entity_Id :=
922                      Create_Itype (E_Subprogram_Type, Parent (T_Def));
923
924    begin
925       --  Associate the Itype node with the inner full-type declaration or
926       --  subprogram spec. This is required to handle nested anonymous
927       --  declarations. For example:
928
929       --      procedure P
930       --       (X : access procedure
931       --                     (Y : access procedure
932       --                                   (Z : access T)))
933
934       D_Ityp := Associated_Node_For_Itype (Desig_Type);
935       while Nkind (D_Ityp) /= N_Full_Type_Declaration
936          and then Nkind (D_Ityp) /= N_Private_Type_Declaration
937          and then Nkind (D_Ityp) /= N_Private_Extension_Declaration
938          and then Nkind (D_Ityp) /= N_Procedure_Specification
939          and then Nkind (D_Ityp) /= N_Function_Specification
940          and then Nkind (D_Ityp) /= N_Object_Declaration
941          and then Nkind (D_Ityp) /= N_Object_Renaming_Declaration
942          and then Nkind (D_Ityp) /= N_Formal_Type_Declaration
943          and then Nkind (D_Ityp) /= N_Task_Type_Declaration
944          and then Nkind (D_Ityp) /= N_Protected_Type_Declaration
945       loop
946          D_Ityp := Parent (D_Ityp);
947          pragma Assert (D_Ityp /= Empty);
948       end loop;
949
950       Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
951
952       if Nkind (D_Ityp) = N_Procedure_Specification
953         or else Nkind (D_Ityp) = N_Function_Specification
954       then
955          Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
956
957       elsif Nkind (D_Ityp) = N_Full_Type_Declaration
958         or else Nkind (D_Ityp) = N_Object_Declaration
959         or else Nkind (D_Ityp) = N_Object_Renaming_Declaration
960         or else Nkind (D_Ityp) = N_Formal_Type_Declaration
961       then
962          Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
963       end if;
964
965       if Nkind (T_Def) = N_Access_Function_Definition then
966          if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
967
968             declare
969                Acc : constant Node_Id := Result_Definition (T_Def);
970
971             begin
972                if Present (Access_To_Subprogram_Definition (Acc))
973                  and then
974                    Protected_Present (Access_To_Subprogram_Definition (Acc))
975                then
976                   Set_Etype
977                     (Desig_Type,
978                        Replace_Anonymous_Access_To_Protected_Subprogram
979                          (T_Def));
980
981                else
982                   Set_Etype
983                     (Desig_Type,
984                        Access_Definition (T_Def, Result_Definition (T_Def)));
985                end if;
986             end;
987
988          else
989             Analyze (Result_Definition (T_Def));
990             Set_Etype (Desig_Type, Entity (Result_Definition (T_Def)));
991          end if;
992
993          if not (Is_Type (Etype (Desig_Type))) then
994             Error_Msg_N
995               ("expect type in function specification",
996                Result_Definition (T_Def));
997          end if;
998
999       else
1000          Set_Etype (Desig_Type, Standard_Void_Type);
1001       end if;
1002
1003       if Present (Formals) then
1004          Push_Scope (Desig_Type);
1005          Process_Formals (Formals, Parent (T_Def));
1006
1007          --  A bit of a kludge here, End_Scope requires that the parent
1008          --  pointer be set to something reasonable, but Itypes don't have
1009          --  parent pointers. So we set it and then unset it ??? If and when
1010          --  Itypes have proper parent pointers to their declarations, this
1011          --  kludge can be removed.
1012
1013          Set_Parent (Desig_Type, T_Name);
1014          End_Scope;
1015          Set_Parent (Desig_Type, Empty);
1016       end if;
1017
1018       --  The return type and/or any parameter type may be incomplete. Mark
1019       --  the subprogram_type as depending on the incomplete type, so that
1020       --  it can be updated when the full type declaration is seen. This
1021       --  only applies to incomplete types declared in some enclosing scope,
1022       --  not to limited views from other packages.
1023
1024       if Present (Formals) then
1025          Formal := First_Formal (Desig_Type);
1026          while Present (Formal) loop
1027             if Ekind (Formal) /= E_In_Parameter
1028               and then Nkind (T_Def) = N_Access_Function_Definition
1029             then
1030                Error_Msg_N ("functions can only have IN parameters", Formal);
1031             end if;
1032
1033             if Ekind (Etype (Formal)) = E_Incomplete_Type
1034               and then In_Open_Scopes (Scope (Etype (Formal)))
1035             then
1036                Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1037                Set_Has_Delayed_Freeze (Desig_Type);
1038             end if;
1039
1040             Next_Formal (Formal);
1041          end loop;
1042       end if;
1043
1044       if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1045         and then not Has_Delayed_Freeze (Desig_Type)
1046       then
1047          Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1048          Set_Has_Delayed_Freeze (Desig_Type);
1049       end if;
1050
1051       Check_Delayed_Subprogram (Desig_Type);
1052
1053       if Protected_Present (T_Def) then
1054          Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1055          Set_Convention (Desig_Type, Convention_Protected);
1056       else
1057          Set_Ekind (T_Name, E_Access_Subprogram_Type);
1058       end if;
1059
1060       Set_Etype                    (T_Name, T_Name);
1061       Init_Size_Align              (T_Name);
1062       Set_Directly_Designated_Type (T_Name, Desig_Type);
1063
1064       --  Ada 2005 (AI-231): Propagate the null-excluding attribute
1065
1066       Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1067
1068       Check_Restriction (No_Access_Subprograms, T_Def);
1069    end Access_Subprogram_Declaration;
1070
1071    ----------------------------
1072    -- Access_Type_Declaration --
1073    ----------------------------
1074
1075    procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1076       S : constant Node_Id := Subtype_Indication (Def);
1077       P : constant Node_Id := Parent (Def);
1078
1079       Desig : Entity_Id;
1080       --  Designated type
1081
1082    begin
1083       --  Check for permissible use of incomplete type
1084
1085       if Nkind (S) /= N_Subtype_Indication then
1086          Analyze (S);
1087
1088          if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1089             Set_Directly_Designated_Type (T, Entity (S));
1090          else
1091             Set_Directly_Designated_Type (T,
1092               Process_Subtype (S, P, T, 'P'));
1093          end if;
1094
1095       else
1096          Set_Directly_Designated_Type (T,
1097            Process_Subtype (S, P, T, 'P'));
1098       end if;
1099
1100       if All_Present (Def) or Constant_Present (Def) then
1101          Set_Ekind (T, E_General_Access_Type);
1102       else
1103          Set_Ekind (T, E_Access_Type);
1104       end if;
1105
1106       if Base_Type (Designated_Type (T)) = T then
1107          Error_Msg_N ("access type cannot designate itself", S);
1108
1109       --  In Ada 2005, the type may have a limited view through some unit
1110       --  in its own context, allowing the following circularity that cannot
1111       --  be detected earlier
1112
1113       elsif Is_Class_Wide_Type (Designated_Type (T))
1114         and then Etype (Designated_Type (T)) = T
1115       then
1116          Error_Msg_N
1117            ("access type cannot designate its own classwide type", S);
1118
1119          --  Clean up indication of tagged status to prevent cascaded errors
1120
1121          Set_Is_Tagged_Type (T, False);
1122       end if;
1123
1124       Set_Etype (T, T);
1125
1126       --  If the type has appeared already in a with_type clause, it is
1127       --  frozen and the pointer size is already set. Else, initialize.
1128
1129       if not From_With_Type (T) then
1130          Init_Size_Align (T);
1131       end if;
1132
1133       Desig := Designated_Type (T);
1134
1135       --  If designated type is an imported tagged type, indicate that the
1136       --  access type is also imported, and therefore restricted in its use.
1137       --  The access type may already be imported, so keep setting otherwise.
1138
1139       --  Ada 2005 (AI-50217): If the non-limited view of the designated type
1140       --  is available, use it as the designated type of the access type, so
1141       --  that the back-end gets a usable entity.
1142
1143       if From_With_Type (Desig)
1144         and then Ekind (Desig) /= E_Access_Type
1145       then
1146          Set_From_With_Type (T);
1147       end if;
1148
1149       --  Note that Has_Task is always false, since the access type itself
1150       --  is not a task type. See Einfo for more description on this point.
1151       --  Exactly the same consideration applies to Has_Controlled_Component.
1152
1153       Set_Has_Task (T, False);
1154       Set_Has_Controlled_Component (T, False);
1155
1156       --  Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1157       --  attributes
1158
1159       Set_Can_Never_Be_Null  (T, Null_Exclusion_Present (Def));
1160       Set_Is_Access_Constant (T, Constant_Present (Def));
1161    end Access_Type_Declaration;
1162
1163    ----------------------------------
1164    -- Add_Interface_Tag_Components --
1165    ----------------------------------
1166
1167    procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1168       Loc      : constant Source_Ptr := Sloc (N);
1169       Elmt     : Elmt_Id;
1170       Ext      : Node_Id;
1171       L        : List_Id;
1172       Last_Tag : Node_Id;
1173       Comp     : Node_Id;
1174
1175       procedure Add_Sync_Iface_Tags (T : Entity_Id);
1176       --  Local subprogram used to recursively climb through the parents
1177       --  of T to add the tags of all the progenitor interfaces.
1178
1179       procedure Add_Tag (Iface : Entity_Id);
1180       --  Add tag for one of the progenitor interfaces
1181
1182       -------------------------
1183       -- Add_Sync_Iface_Tags --
1184       -------------------------
1185
1186       procedure Add_Sync_Iface_Tags (T : Entity_Id) is
1187       begin
1188          if Etype (T) /= T then
1189             Add_Sync_Iface_Tags (Etype (T));
1190          end if;
1191
1192          Elmt := First_Elmt (Abstract_Interfaces (T));
1193          while Present (Elmt) loop
1194             Add_Tag (Node (Elmt));
1195             Next_Elmt (Elmt);
1196          end loop;
1197       end Add_Sync_Iface_Tags;
1198
1199       -------------
1200       -- Add_Tag --
1201       -------------
1202
1203       procedure Add_Tag (Iface : Entity_Id) is
1204          Decl   : Node_Id;
1205          Def    : Node_Id;
1206          Tag    : Entity_Id;
1207          Offset : Entity_Id;
1208
1209       begin
1210          pragma Assert (Is_Tagged_Type (Iface)
1211            and then Is_Interface (Iface));
1212
1213          Def :=
1214            Make_Component_Definition (Loc,
1215              Aliased_Present    => True,
1216              Subtype_Indication =>
1217                New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1218
1219          Tag := Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1220
1221          Decl :=
1222            Make_Component_Declaration (Loc,
1223              Defining_Identifier  => Tag,
1224              Component_Definition => Def);
1225
1226          Analyze_Component_Declaration (Decl);
1227
1228          Set_Analyzed (Decl);
1229          Set_Ekind               (Tag, E_Component);
1230          Set_Is_Tag              (Tag);
1231          Set_Is_Aliased          (Tag);
1232          Set_Related_Interface   (Tag, Iface);
1233          Init_Component_Location (Tag);
1234
1235          pragma Assert (Is_Frozen (Iface));
1236
1237          Set_DT_Entry_Count    (Tag,
1238            DT_Entry_Count (First_Entity (Iface)));
1239
1240          if No (Last_Tag) then
1241             Prepend (Decl, L);
1242          else
1243             Insert_After (Last_Tag, Decl);
1244          end if;
1245
1246          Last_Tag := Decl;
1247
1248          --  If the ancestor has discriminants we need to give special support
1249          --  to store the offset_to_top value of the secondary dispatch tables.
1250          --  For this purpose we add a supplementary component just after the
1251          --  field that contains the tag associated with each secondary DT.
1252
1253          if Typ /= Etype (Typ)
1254            and then Has_Discriminants (Etype (Typ))
1255          then
1256             Def :=
1257               Make_Component_Definition (Loc,
1258                 Subtype_Indication =>
1259                   New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1260
1261             Offset :=
1262               Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1263
1264             Decl :=
1265               Make_Component_Declaration (Loc,
1266                 Defining_Identifier  => Offset,
1267                 Component_Definition => Def);
1268
1269             Analyze_Component_Declaration (Decl);
1270
1271             Set_Analyzed (Decl);
1272             Set_Ekind               (Offset, E_Component);
1273             Set_Is_Aliased          (Offset);
1274             Set_Related_Interface   (Offset, Iface);
1275             Init_Component_Location (Offset);
1276             Insert_After (Last_Tag, Decl);
1277             Last_Tag := Decl;
1278          end if;
1279       end Add_Tag;
1280
1281       --  Local variables
1282
1283       Iface_List : List_Id;
1284
1285    --  Start of processing for Add_Interface_Tag_Components
1286
1287    begin
1288       if not RTE_Available (RE_Interface_Tag) then
1289          Error_Msg
1290            ("(Ada 2005) interface types not supported by this run-time!",
1291             Sloc (N));
1292          return;
1293       end if;
1294
1295       if Ekind (Typ) /= E_Record_Type
1296         or else (Is_Concurrent_Record_Type (Typ)
1297                   and then Is_Empty_List (Abstract_Interface_List (Typ)))
1298         or else (not Is_Concurrent_Record_Type (Typ)
1299                   and then No (Abstract_Interfaces (Typ))
1300                   and then Is_Empty_Elmt_List (Abstract_Interfaces (Typ)))
1301       then
1302          return;
1303       end if;
1304
1305       --  Find the current last tag
1306
1307       if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1308          Ext := Record_Extension_Part (Type_Definition (N));
1309       else
1310          pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1311          Ext := Type_Definition (N);
1312       end if;
1313
1314       Last_Tag := Empty;
1315
1316       if not (Present (Component_List (Ext))) then
1317          Set_Null_Present (Ext, False);
1318          L := New_List;
1319          Set_Component_List (Ext,
1320            Make_Component_List (Loc,
1321              Component_Items => L,
1322              Null_Present => False));
1323       else
1324          if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1325             L := Component_Items
1326                    (Component_List
1327                      (Record_Extension_Part
1328                        (Type_Definition (N))));
1329          else
1330             L := Component_Items
1331                    (Component_List
1332                      (Type_Definition (N)));
1333          end if;
1334
1335          --  Find the last tag component
1336
1337          Comp := First (L);
1338          while Present (Comp) loop
1339             if Nkind (Comp) = N_Component_Declaration
1340               and then Is_Tag (Defining_Identifier (Comp))
1341             then
1342                Last_Tag := Comp;
1343             end if;
1344
1345             Next (Comp);
1346          end loop;
1347       end if;
1348
1349       --  At this point L references the list of components and Last_Tag
1350       --  references the current last tag (if any). Now we add the tag
1351       --  corresponding with all the interfaces that are not implemented
1352       --  by the parent.
1353
1354       if Is_Concurrent_Record_Type (Typ) then
1355          Iface_List := Abstract_Interface_List (Typ);
1356
1357          if Is_Non_Empty_List (Iface_List) then
1358             Add_Sync_Iface_Tags (Etype (First (Iface_List)));
1359          end if;
1360       end if;
1361
1362       if Present (Abstract_Interfaces (Typ)) then
1363          Elmt := First_Elmt (Abstract_Interfaces (Typ));
1364          while Present (Elmt) loop
1365             Add_Tag (Node (Elmt));
1366             Next_Elmt (Elmt);
1367          end loop;
1368       end if;
1369    end Add_Interface_Tag_Components;
1370
1371    -----------------------------------
1372    -- Analyze_Component_Declaration --
1373    -----------------------------------
1374
1375    procedure Analyze_Component_Declaration (N : Node_Id) is
1376       Id : constant Entity_Id := Defining_Identifier (N);
1377       E  : constant Node_Id   := Expression (N);
1378       T  : Entity_Id;
1379       P  : Entity_Id;
1380
1381       function Contains_POC (Constr : Node_Id) return Boolean;
1382       --  Determines whether a constraint uses the discriminant of a record
1383       --  type thus becoming a per-object constraint (POC).
1384
1385       function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1386       --  Typ is the type of the current component, check whether this type is
1387       --  a limited type. Used to validate declaration against that of
1388       --  enclosing record.
1389
1390       ------------------
1391       -- Contains_POC --
1392       ------------------
1393
1394       function Contains_POC (Constr : Node_Id) return Boolean is
1395       begin
1396          --  Prevent cascaded errors
1397
1398          if Error_Posted (Constr) then
1399             return False;
1400          end if;
1401
1402          case Nkind (Constr) is
1403             when N_Attribute_Reference =>
1404                return
1405                  Attribute_Name (Constr) = Name_Access
1406                    and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1407
1408             when N_Discriminant_Association =>
1409                return Denotes_Discriminant (Expression (Constr));
1410
1411             when N_Identifier =>
1412                return Denotes_Discriminant (Constr);
1413
1414             when N_Index_Or_Discriminant_Constraint =>
1415                declare
1416                   IDC : Node_Id;
1417
1418                begin
1419                   IDC := First (Constraints (Constr));
1420                   while Present (IDC) loop
1421
1422                      --  One per-object constraint is sufficient
1423
1424                      if Contains_POC (IDC) then
1425                         return True;
1426                      end if;
1427
1428                      Next (IDC);
1429                   end loop;
1430
1431                   return False;
1432                end;
1433
1434             when N_Range =>
1435                return Denotes_Discriminant (Low_Bound (Constr))
1436                         or else
1437                       Denotes_Discriminant (High_Bound (Constr));
1438
1439             when N_Range_Constraint =>
1440                return Denotes_Discriminant (Range_Expression (Constr));
1441
1442             when others =>
1443                return False;
1444
1445          end case;
1446       end Contains_POC;
1447
1448       ----------------------
1449       -- Is_Known_Limited --
1450       ----------------------
1451
1452       function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1453          P : constant Entity_Id := Etype (Typ);
1454          R : constant Entity_Id := Root_Type (Typ);
1455
1456       begin
1457          if Is_Limited_Record (Typ) then
1458             return True;
1459
1460          --  If the root type is limited (and not a limited interface)
1461          --  so is the current type
1462
1463          elsif Is_Limited_Record (R)
1464            and then
1465              (not Is_Interface (R)
1466                or else not Is_Limited_Interface (R))
1467          then
1468             return True;
1469
1470          --  Else the type may have a limited interface progenitor, but a
1471          --  limited record parent.
1472
1473          elsif R /= P
1474            and then Is_Limited_Record (P)
1475          then
1476             return True;
1477
1478          else
1479             return False;
1480          end if;
1481       end Is_Known_Limited;
1482
1483    --  Start of processing for Analyze_Component_Declaration
1484
1485    begin
1486       Generate_Definition (Id);
1487       Enter_Name (Id);
1488
1489       if Present (Subtype_Indication (Component_Definition (N))) then
1490          T := Find_Type_Of_Object
1491                 (Subtype_Indication (Component_Definition (N)), N);
1492
1493       --  Ada 2005 (AI-230): Access Definition case
1494
1495       else
1496          pragma Assert (Present
1497                           (Access_Definition (Component_Definition (N))));
1498
1499          T := Access_Definition
1500                 (Related_Nod => N,
1501                  N => Access_Definition (Component_Definition (N)));
1502          Set_Is_Local_Anonymous_Access (T);
1503
1504          --  Ada 2005 (AI-254)
1505
1506          if Present (Access_To_Subprogram_Definition
1507                       (Access_Definition (Component_Definition (N))))
1508            and then Protected_Present (Access_To_Subprogram_Definition
1509                                         (Access_Definition
1510                                           (Component_Definition (N))))
1511          then
1512             T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1513          end if;
1514       end if;
1515
1516       --  If the subtype is a constrained subtype of the enclosing record,
1517       --  (which must have a partial view) the back-end does not properly
1518       --  handle the recursion. Rewrite the component declaration with an
1519       --  explicit subtype indication, which is acceptable to Gigi. We can copy
1520       --  the tree directly because side effects have already been removed from
1521       --  discriminant constraints.
1522
1523       if Ekind (T) = E_Access_Subtype
1524         and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1525         and then Comes_From_Source (T)
1526         and then Nkind (Parent (T)) = N_Subtype_Declaration
1527         and then Etype (Directly_Designated_Type (T)) = Current_Scope
1528       then
1529          Rewrite
1530            (Subtype_Indication (Component_Definition (N)),
1531              New_Copy_Tree (Subtype_Indication (Parent (T))));
1532          T := Find_Type_Of_Object
1533                  (Subtype_Indication (Component_Definition (N)), N);
1534       end if;
1535
1536       --  If the component declaration includes a default expression, then we
1537       --  check that the component is not of a limited type (RM 3.7(5)),
1538       --  and do the special preanalysis of the expression (see section on
1539       --  "Handling of Default and Per-Object Expressions" in the spec of
1540       --  package Sem).
1541
1542       if Present (E) then
1543          Analyze_Per_Use_Expression (E, T);
1544          Check_Initialization (T, E);
1545
1546          if Ada_Version >= Ada_05
1547            and then Ekind (T) = E_Anonymous_Access_Type
1548          then
1549             --  Check RM 3.9.2(9): "if the expected type for an expression is
1550             --  an anonymous access-to-specific tagged type, then the object
1551             --  designated by the expression shall not be dynamically tagged
1552             --  unless it is a controlling operand in a call on a dispatching
1553             --  operation"
1554
1555             if Is_Tagged_Type (Directly_Designated_Type (T))
1556               and then
1557                 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1558               and then
1559                 Ekind (Directly_Designated_Type (Etype (E))) =
1560                   E_Class_Wide_Type
1561             then
1562                Error_Msg_N
1563                  ("access to specific tagged type required (RM 3.9.2(9))", E);
1564             end if;
1565
1566             --  (Ada 2005: AI-230): Accessibility check for anonymous
1567             --  components
1568
1569             if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1570                Error_Msg_N
1571                  ("expression has deeper access level than component " &
1572                   "(RM 3.10.2 (12.2))", E);
1573             end if;
1574
1575             --  The initialization expression is a reference to an access
1576             --  discriminant. The type of the discriminant is always deeper
1577             --  than any access type.
1578
1579             if Ekind (Etype (E)) = E_Anonymous_Access_Type
1580               and then Is_Entity_Name (E)
1581               and then Ekind (Entity (E)) = E_In_Parameter
1582               and then Present (Discriminal_Link (Entity (E)))
1583             then
1584                Error_Msg_N
1585                  ("discriminant has deeper accessibility level than target",
1586                   E);
1587             end if;
1588          end if;
1589       end if;
1590
1591       --  The parent type may be a private view with unknown discriminants,
1592       --  and thus unconstrained. Regular components must be constrained.
1593
1594       if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1595          if Is_Class_Wide_Type (T) then
1596             Error_Msg_N
1597                ("class-wide subtype with unknown discriminants" &
1598                  " in component declaration",
1599                  Subtype_Indication (Component_Definition (N)));
1600          else
1601             Error_Msg_N
1602               ("unconstrained subtype in component declaration",
1603                Subtype_Indication (Component_Definition (N)));
1604          end if;
1605
1606       --  Components cannot be abstract, except for the special case of
1607       --  the _Parent field (case of extending an abstract tagged type)
1608
1609       elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1610          Error_Msg_N ("type of a component cannot be abstract", N);
1611       end if;
1612
1613       Set_Etype (Id, T);
1614       Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1615
1616       --  The component declaration may have a per-object constraint, set
1617       --  the appropriate flag in the defining identifier of the subtype.
1618
1619       if Present (Subtype_Indication (Component_Definition (N))) then
1620          declare
1621             Sindic : constant Node_Id :=
1622                        Subtype_Indication (Component_Definition (N));
1623
1624          begin
1625             if Nkind (Sindic) = N_Subtype_Indication
1626               and then Present (Constraint (Sindic))
1627               and then Contains_POC (Constraint (Sindic))
1628             then
1629                Set_Has_Per_Object_Constraint (Id);
1630             end if;
1631          end;
1632       end if;
1633
1634       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1635       --  out some static checks.
1636
1637       if Ada_Version >= Ada_05
1638         and then Can_Never_Be_Null (T)
1639       then
1640          Null_Exclusion_Static_Checks (N);
1641       end if;
1642
1643       --  If this component is private (or depends on a private type), flag the
1644       --  record type to indicate that some operations are not available.
1645
1646       P := Private_Component (T);
1647
1648       if Present (P) then
1649
1650          --  Check for circular definitions
1651
1652          if P = Any_Type then
1653             Set_Etype (Id, Any_Type);
1654
1655          --  There is a gap in the visibility of operations only if the
1656          --  component type is not defined in the scope of the record type.
1657
1658          elsif Scope (P) = Scope (Current_Scope) then
1659             null;
1660
1661          elsif Is_Limited_Type (P) then
1662             Set_Is_Limited_Composite (Current_Scope);
1663
1664          else
1665             Set_Is_Private_Composite (Current_Scope);
1666          end if;
1667       end if;
1668
1669       if P /= Any_Type
1670         and then Is_Limited_Type (T)
1671         and then Chars (Id) /= Name_uParent
1672         and then Is_Tagged_Type (Current_Scope)
1673       then
1674          if Is_Derived_Type (Current_Scope)
1675            and then not Is_Known_Limited (Current_Scope)
1676          then
1677             Error_Msg_N
1678               ("extension of nonlimited type cannot have limited components",
1679                N);
1680
1681             if Is_Interface (Root_Type (Current_Scope)) then
1682                Error_Msg_N
1683                  ("\limitedness is not inherited from limited interface", N);
1684                Error_Msg_N
1685                  ("\add LIMITED to type indication", N);
1686             end if;
1687
1688             Explain_Limited_Type (T, N);
1689             Set_Etype (Id, Any_Type);
1690             Set_Is_Limited_Composite (Current_Scope, False);
1691
1692          elsif not Is_Derived_Type (Current_Scope)
1693            and then not Is_Limited_Record (Current_Scope)
1694            and then not Is_Concurrent_Type (Current_Scope)
1695          then
1696             Error_Msg_N
1697               ("nonlimited tagged type cannot have limited components", N);
1698             Explain_Limited_Type (T, N);
1699             Set_Etype (Id, Any_Type);
1700             Set_Is_Limited_Composite (Current_Scope, False);
1701          end if;
1702       end if;
1703
1704       Set_Original_Record_Component (Id, Id);
1705    end Analyze_Component_Declaration;
1706
1707    --------------------------
1708    -- Analyze_Declarations --
1709    --------------------------
1710
1711    procedure Analyze_Declarations (L : List_Id) is
1712       D           : Node_Id;
1713       Freeze_From : Entity_Id := Empty;
1714       Next_Node   : Node_Id;
1715
1716       procedure Adjust_D;
1717       --  Adjust D not to include implicit label declarations, since these
1718       --  have strange Sloc values that result in elaboration check problems.
1719       --  (They have the sloc of the label as found in the source, and that
1720       --  is ahead of the current declarative part).
1721
1722       --------------
1723       -- Adjust_D --
1724       --------------
1725
1726       procedure Adjust_D is
1727       begin
1728          while Present (Prev (D))
1729            and then Nkind (D) = N_Implicit_Label_Declaration
1730          loop
1731             Prev (D);
1732          end loop;
1733       end Adjust_D;
1734
1735    --  Start of processing for Analyze_Declarations
1736
1737    begin
1738       D := First (L);
1739       while Present (D) loop
1740
1741          --  Complete analysis of declaration
1742
1743          Analyze (D);
1744          Next_Node := Next (D);
1745
1746          if No (Freeze_From) then
1747             Freeze_From := First_Entity (Current_Scope);
1748          end if;
1749
1750          --  At the end of a declarative part, freeze remaining entities
1751          --  declared in it. The end of the visible declarations of package
1752          --  specification is not the end of a declarative part if private
1753          --  declarations are present. The end of a package declaration is a
1754          --  freezing point only if it a library package. A task definition or
1755          --  protected type definition is not a freeze point either. Finally,
1756          --  we do not freeze entities in generic scopes, because there is no
1757          --  code generated for them and freeze nodes will be generated for
1758          --  the instance.
1759
1760          --  The end of a package instantiation is not a freeze point, but
1761          --  for now we make it one, because the generic body is inserted
1762          --  (currently) immediately after. Generic instantiations will not
1763          --  be a freeze point once delayed freezing of bodies is implemented.
1764          --  (This is needed in any case for early instantiations ???).
1765
1766          if No (Next_Node) then
1767             if Nkind (Parent (L)) = N_Component_List
1768               or else Nkind (Parent (L)) = N_Task_Definition
1769               or else Nkind (Parent (L)) = N_Protected_Definition
1770             then
1771                null;
1772
1773             elsif Nkind (Parent (L)) /= N_Package_Specification then
1774                if Nkind (Parent (L)) = N_Package_Body then
1775                   Freeze_From := First_Entity (Current_Scope);
1776                end if;
1777
1778                Adjust_D;
1779                Freeze_All (Freeze_From, D);
1780                Freeze_From := Last_Entity (Current_Scope);
1781
1782             elsif Scope (Current_Scope) /= Standard_Standard
1783               and then not Is_Child_Unit (Current_Scope)
1784               and then No (Generic_Parent (Parent (L)))
1785             then
1786                null;
1787
1788             elsif L /= Visible_Declarations (Parent (L))
1789                or else No (Private_Declarations (Parent (L)))
1790                or else Is_Empty_List (Private_Declarations (Parent (L)))
1791             then
1792                Adjust_D;
1793                Freeze_All (Freeze_From, D);
1794                Freeze_From := Last_Entity (Current_Scope);
1795             end if;
1796
1797          --  If next node is a body then freeze all types before the body.
1798          --  An exception occurs for some expander-generated bodies. If these
1799          --  are generated at places where in general language rules would not
1800          --  allow a freeze point, then we assume that the expander has
1801          --  explicitly checked that all required types are properly frozen,
1802          --  and we do not cause general freezing here. This special circuit
1803          --  is used when the encountered body is marked as having already
1804          --  been analyzed.
1805
1806          --  In all other cases (bodies that come from source, and expander
1807          --  generated bodies that have not been analyzed yet), freeze all
1808          --  types now. Note that in the latter case, the expander must take
1809          --  care to attach the bodies at a proper place in the tree so as to
1810          --  not cause unwanted freezing at that point.
1811
1812          elsif not Analyzed (Next_Node)
1813            and then (Nkind (Next_Node) = N_Subprogram_Body
1814              or else Nkind (Next_Node) = N_Entry_Body
1815              or else Nkind (Next_Node) = N_Package_Body
1816              or else Nkind (Next_Node) = N_Protected_Body
1817              or else Nkind (Next_Node) = N_Task_Body
1818              or else Nkind (Next_Node) in N_Body_Stub)
1819          then
1820             Adjust_D;
1821             Freeze_All (Freeze_From, D);
1822             Freeze_From := Last_Entity (Current_Scope);
1823          end if;
1824
1825          D := Next_Node;
1826       end loop;
1827    end Analyze_Declarations;
1828
1829    ----------------------------------
1830    -- Analyze_Incomplete_Type_Decl --
1831    ----------------------------------
1832
1833    procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
1834       F : constant Boolean := Is_Pure (Current_Scope);
1835       T : Entity_Id;
1836
1837    begin
1838       Generate_Definition (Defining_Identifier (N));
1839
1840       --  Process an incomplete declaration. The identifier must not have been
1841       --  declared already in the scope. However, an incomplete declaration may
1842       --  appear in the private part of a package, for a private type that has
1843       --  already been declared.
1844
1845       --  In this case, the discriminants (if any) must match
1846
1847       T := Find_Type_Name (N);
1848
1849       Set_Ekind (T, E_Incomplete_Type);
1850       Init_Size_Align (T);
1851       Set_Is_First_Subtype (T, True);
1852       Set_Etype (T, T);
1853
1854       --  Ada 2005 (AI-326): Minimum decoration to give support to tagged
1855       --  incomplete types.
1856
1857       if Tagged_Present (N) then
1858          Set_Is_Tagged_Type (T);
1859          Make_Class_Wide_Type (T);
1860          Set_Primitive_Operations (T, New_Elmt_List);
1861       end if;
1862
1863       Push_Scope (T);
1864
1865       Set_Stored_Constraint (T, No_Elist);
1866
1867       if Present (Discriminant_Specifications (N)) then
1868          Process_Discriminants (N);
1869       end if;
1870
1871       End_Scope;
1872
1873       --  If the type has discriminants, non-trivial subtypes may be be
1874       --  declared before the full view of the type. The full views of those
1875       --  subtypes will be built after the full view of the type.
1876
1877       Set_Private_Dependents (T, New_Elmt_List);
1878       Set_Is_Pure (T, F);
1879    end Analyze_Incomplete_Type_Decl;
1880
1881    -----------------------------------
1882    -- Analyze_Interface_Declaration --
1883    -----------------------------------
1884
1885    procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
1886       CW : constant Entity_Id := Class_Wide_Type (T);
1887
1888    begin
1889       Set_Is_Tagged_Type      (T);
1890
1891       Set_Is_Limited_Record   (T, Limited_Present (Def)
1892                                    or else Task_Present (Def)
1893                                    or else Protected_Present (Def)
1894                                    or else Synchronized_Present (Def));
1895
1896       --  Type is abstract if full declaration carries keyword, or if previous
1897       --  partial view did.
1898
1899       Set_Is_Abstract_Type (T);
1900       Set_Is_Interface     (T);
1901
1902       --  Type is a limited interface if it includes the keyword limited, task,
1903       --  protected, or synchronized.
1904
1905       Set_Is_Limited_Interface
1906         (T, Limited_Present (Def)
1907               or else Protected_Present (Def)
1908               or else Synchronized_Present (Def)
1909               or else Task_Present (Def));
1910
1911       Set_Is_Protected_Interface    (T, Protected_Present (Def));
1912       Set_Is_Task_Interface         (T, Task_Present (Def));
1913
1914       --  Type is a synchronized interface if it includes the keyword task,
1915       --  protected, or synchronized.
1916
1917       Set_Is_Synchronized_Interface
1918         (T, Synchronized_Present (Def)
1919               or else Protected_Present (Def)
1920               or else Task_Present (Def));
1921
1922       Set_Abstract_Interfaces       (T, New_Elmt_List);
1923       Set_Primitive_Operations      (T, New_Elmt_List);
1924
1925       --  Complete the decoration of the class-wide entity if it was already
1926       --  built (ie. during the creation of the limited view)
1927
1928       if Present (CW) then
1929          Set_Is_Interface (CW);
1930          Set_Is_Limited_Interface      (CW, Is_Limited_Interface (T));
1931          Set_Is_Protected_Interface    (CW, Is_Protected_Interface (T));
1932          Set_Is_Synchronized_Interface (CW, Is_Synchronized_Interface (T));
1933          Set_Is_Task_Interface         (CW, Is_Task_Interface (T));
1934       end if;
1935    end Analyze_Interface_Declaration;
1936
1937    -----------------------------
1938    -- Analyze_Itype_Reference --
1939    -----------------------------
1940
1941    --  Nothing to do. This node is placed in the tree only for the benefit of
1942    --  back end processing, and has no effect on the semantic processing.
1943
1944    procedure Analyze_Itype_Reference (N : Node_Id) is
1945    begin
1946       pragma Assert (Is_Itype (Itype (N)));
1947       null;
1948    end Analyze_Itype_Reference;
1949
1950    --------------------------------
1951    -- Analyze_Number_Declaration --
1952    --------------------------------
1953
1954    procedure Analyze_Number_Declaration (N : Node_Id) is
1955       Id    : constant Entity_Id := Defining_Identifier (N);
1956       E     : constant Node_Id   := Expression (N);
1957       T     : Entity_Id;
1958       Index : Interp_Index;
1959       It    : Interp;
1960
1961    begin
1962       Generate_Definition (Id);
1963       Enter_Name (Id);
1964
1965       --  This is an optimization of a common case of an integer literal
1966
1967       if Nkind (E) = N_Integer_Literal then
1968          Set_Is_Static_Expression (E, True);
1969          Set_Etype                (E, Universal_Integer);
1970
1971          Set_Etype     (Id, Universal_Integer);
1972          Set_Ekind     (Id, E_Named_Integer);
1973          Set_Is_Frozen (Id, True);
1974          return;
1975       end if;
1976
1977       Set_Is_Pure (Id, Is_Pure (Current_Scope));
1978
1979       --  Process expression, replacing error by integer zero, to avoid
1980       --  cascaded errors or aborts further along in the processing
1981
1982       --  Replace Error by integer zero, which seems least likely to
1983       --  cause cascaded errors.
1984
1985       if E = Error then
1986          Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
1987          Set_Error_Posted (E);
1988       end if;
1989
1990       Analyze (E);
1991
1992       --  Verify that the expression is static and numeric. If
1993       --  the expression is overloaded, we apply the preference
1994       --  rule that favors root numeric types.
1995
1996       if not Is_Overloaded (E) then
1997          T := Etype (E);
1998
1999       else
2000          T := Any_Type;
2001
2002          Get_First_Interp (E, Index, It);
2003          while Present (It.Typ) loop
2004             if (Is_Integer_Type (It.Typ)
2005                  or else Is_Real_Type (It.Typ))
2006               and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2007             then
2008                if T = Any_Type then
2009                   T := It.Typ;
2010
2011                elsif It.Typ = Universal_Real
2012                  or else It.Typ = Universal_Integer
2013                then
2014                   --  Choose universal interpretation over any other
2015
2016                   T := It.Typ;
2017                   exit;
2018                end if;
2019             end if;
2020
2021             Get_Next_Interp (Index, It);
2022          end loop;
2023       end if;
2024
2025       if Is_Integer_Type (T)  then
2026          Resolve (E, T);
2027          Set_Etype (Id, Universal_Integer);
2028          Set_Ekind (Id, E_Named_Integer);
2029
2030       elsif Is_Real_Type (T) then
2031
2032          --  Because the real value is converted to universal_real, this is a
2033          --  legal context for a universal fixed expression.
2034
2035          if T = Universal_Fixed then
2036             declare
2037                Loc  : constant Source_Ptr := Sloc (N);
2038                Conv : constant Node_Id := Make_Type_Conversion (Loc,
2039                         Subtype_Mark =>
2040                           New_Occurrence_Of (Universal_Real, Loc),
2041                         Expression => Relocate_Node (E));
2042
2043             begin
2044                Rewrite (E, Conv);
2045                Analyze (E);
2046             end;
2047
2048          elsif T = Any_Fixed then
2049             Error_Msg_N ("illegal context for mixed mode operation", E);
2050
2051             --  Expression is of the form : universal_fixed * integer. Try to
2052             --  resolve as universal_real.
2053
2054             T := Universal_Real;
2055             Set_Etype (E, T);
2056          end if;
2057
2058          Resolve (E, T);
2059          Set_Etype (Id, Universal_Real);
2060          Set_Ekind (Id, E_Named_Real);
2061
2062       else
2063          Wrong_Type (E, Any_Numeric);
2064          Resolve (E, T);
2065
2066          Set_Etype               (Id, T);
2067          Set_Ekind               (Id, E_Constant);
2068          Set_Never_Set_In_Source (Id, True);
2069          Set_Is_True_Constant    (Id, True);
2070          return;
2071       end if;
2072
2073       if Nkind (E) = N_Integer_Literal
2074         or else Nkind (E) = N_Real_Literal
2075       then
2076          Set_Etype (E, Etype (Id));
2077       end if;
2078
2079       if not Is_OK_Static_Expression (E) then
2080          Flag_Non_Static_Expr
2081            ("non-static expression used in number declaration!", E);
2082          Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2083          Set_Etype (E, Any_Type);
2084       end if;
2085    end Analyze_Number_Declaration;
2086
2087    --------------------------------
2088    -- Analyze_Object_Declaration --
2089    --------------------------------
2090
2091    procedure Analyze_Object_Declaration (N : Node_Id) is
2092       Loc   : constant Source_Ptr := Sloc (N);
2093       Id    : constant Entity_Id  := Defining_Identifier (N);
2094       T     : Entity_Id;
2095       Act_T : Entity_Id;
2096
2097       E : Node_Id := Expression (N);
2098       --  E is set to Expression (N) throughout this routine. When
2099       --  Expression (N) is modified, E is changed accordingly.
2100
2101       Prev_Entity : Entity_Id := Empty;
2102
2103       function Count_Tasks (T : Entity_Id) return Uint;
2104       --  This function is called when a library level object of type is
2105       --  declared. It's function is to count the static number of tasks
2106       --  declared within the type (it is only called if Has_Tasks is set for
2107       --  T). As a side effect, if an array of tasks with non-static bounds or
2108       --  a variant record type is encountered, Check_Restrictions is called
2109       --  indicating the count is unknown.
2110
2111       -----------------
2112       -- Count_Tasks --
2113       -----------------
2114
2115       function Count_Tasks (T : Entity_Id) return Uint is
2116          C : Entity_Id;
2117          X : Node_Id;
2118          V : Uint;
2119
2120       begin
2121          if Is_Task_Type (T) then
2122             return Uint_1;
2123
2124          elsif Is_Record_Type (T) then
2125             if Has_Discriminants (T) then
2126                Check_Restriction (Max_Tasks, N);
2127                return Uint_0;
2128
2129             else
2130                V := Uint_0;
2131                C := First_Component (T);
2132                while Present (C) loop
2133                   V := V + Count_Tasks (Etype (C));
2134                   Next_Component (C);
2135                end loop;
2136
2137                return V;
2138             end if;
2139
2140          elsif Is_Array_Type (T) then
2141             X := First_Index (T);
2142             V := Count_Tasks (Component_Type (T));
2143             while Present (X) loop
2144                C := Etype (X);
2145
2146                if not Is_Static_Subtype (C) then
2147                   Check_Restriction (Max_Tasks, N);
2148                   return Uint_0;
2149                else
2150                   V := V * (UI_Max (Uint_0,
2151                                     Expr_Value (Type_High_Bound (C)) -
2152                                     Expr_Value (Type_Low_Bound (C)) + Uint_1));
2153                end if;
2154
2155                Next_Index (X);
2156             end loop;
2157
2158             return V;
2159
2160          else
2161             return Uint_0;
2162          end if;
2163       end Count_Tasks;
2164
2165    --  Start of processing for Analyze_Object_Declaration
2166
2167    begin
2168       --  There are three kinds of implicit types generated by an
2169       --  object declaration:
2170
2171       --   1. Those for generated by the original Object Definition
2172
2173       --   2. Those generated by the Expression
2174
2175       --   3. Those used to constrained the Object Definition with the
2176       --       expression constraints when it is unconstrained
2177
2178       --  They must be generated in this order to avoid order of elaboration
2179       --  issues. Thus the first step (after entering the name) is to analyze
2180       --  the object definition.
2181
2182       if Constant_Present (N) then
2183          Prev_Entity := Current_Entity_In_Scope (Id);
2184
2185          --  If homograph is an implicit subprogram, it is overridden by the
2186          --  current declaration.
2187
2188          if Present (Prev_Entity)
2189            and then Is_Overloadable (Prev_Entity)
2190            and then Is_Inherited_Operation (Prev_Entity)
2191          then
2192             Prev_Entity := Empty;
2193          end if;
2194       end if;
2195
2196       if Present (Prev_Entity) then
2197          Constant_Redeclaration (Id, N, T);
2198
2199          Generate_Reference (Prev_Entity, Id, 'c');
2200          Set_Completion_Referenced (Id);
2201
2202          if Error_Posted (N) then
2203
2204             --  Type mismatch or illegal redeclaration, Do not analyze
2205             --  expression to avoid cascaded errors.
2206
2207             T := Find_Type_Of_Object (Object_Definition (N), N);
2208             Set_Etype (Id, T);
2209             Set_Ekind (Id, E_Variable);
2210             return;
2211          end if;
2212
2213       --  In the normal case, enter identifier at the start to catch premature
2214       --  usage in the initialization expression.
2215
2216       else
2217          Generate_Definition (Id);
2218          Enter_Name (Id);
2219
2220          Mark_Coextensions (N, Object_Definition (N));
2221
2222          T := Find_Type_Of_Object (Object_Definition (N), N);
2223
2224          if Nkind (Object_Definition (N)) = N_Access_Definition
2225            and then Present
2226              (Access_To_Subprogram_Definition (Object_Definition (N)))
2227            and then Protected_Present
2228              (Access_To_Subprogram_Definition (Object_Definition (N)))
2229          then
2230             T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2231          end if;
2232
2233          if Error_Posted (Id) then
2234             Set_Etype (Id, T);
2235             Set_Ekind (Id, E_Variable);
2236             return;
2237          end if;
2238       end if;
2239
2240       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2241       --  out some static checks
2242
2243       if Ada_Version >= Ada_05
2244         and then Can_Never_Be_Null (T)
2245       then
2246          --  In case of aggregates we must also take care of the correct
2247          --  initialization of nested aggregates bug this is done at the
2248          --  point of the analysis of the aggregate (see sem_aggr.adb)
2249
2250          if Present (Expression (N))
2251            and then Nkind (Expression (N)) = N_Aggregate
2252          then
2253             null;
2254
2255          else
2256             declare
2257                Save_Typ : constant Entity_Id := Etype (Id);
2258             begin
2259                Set_Etype (Id, T); --  Temp. decoration for static checks
2260                Null_Exclusion_Static_Checks (N);
2261                Set_Etype (Id, Save_Typ);
2262             end;
2263          end if;
2264       end if;
2265
2266       Set_Is_Pure (Id, Is_Pure (Current_Scope));
2267
2268       --  If deferred constant, make sure context is appropriate. We detect
2269       --  a deferred constant as a constant declaration with no expression.
2270       --  A deferred constant can appear in a package body if its completion
2271       --  is by means of an interface pragma.
2272
2273       if Constant_Present (N)
2274         and then No (E)
2275       then
2276          --  We exclude forward references to tags
2277
2278          if Is_Imported (Defining_Identifier (N))
2279            and then
2280             (T = RTE (RE_Tag)
2281               or else (Present (Full_View (T))
2282                         and then Full_View (T) = RTE (RE_Tag)))
2283          then
2284             null;
2285
2286          elsif not Is_Package_Or_Generic_Package (Current_Scope) then
2287             Error_Msg_N
2288               ("invalid context for deferred constant declaration (RM 7.4)",
2289                 N);
2290             Error_Msg_N
2291               ("\declaration requires an initialization expression",
2292                 N);
2293             Set_Constant_Present (N, False);
2294
2295          --  In Ada 83, deferred constant must be of private type
2296
2297          elsif not Is_Private_Type (T) then
2298             if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2299                Error_Msg_N
2300                  ("(Ada 83) deferred constant must be private type", N);
2301             end if;
2302          end if;
2303
2304       --  If not a deferred constant, then object declaration freezes its type
2305
2306       else
2307          Check_Fully_Declared (T, N);
2308          Freeze_Before (N, T);
2309       end if;
2310
2311       --  If the object was created by a constrained array definition, then
2312       --  set the link in both the anonymous base type and anonymous subtype
2313       --  that are built to represent the array type to point to the object.
2314
2315       if Nkind (Object_Definition (Declaration_Node (Id))) =
2316                         N_Constrained_Array_Definition
2317       then
2318          Set_Related_Array_Object (T, Id);
2319          Set_Related_Array_Object (Base_Type (T), Id);
2320       end if;
2321
2322       --  Special checks for protected objects not at library level
2323
2324       if Is_Protected_Type (T)
2325         and then not Is_Library_Level_Entity (Id)
2326       then
2327          Check_Restriction (No_Local_Protected_Objects, Id);
2328
2329          --  Protected objects with interrupt handlers must be at library level
2330
2331          --  Ada 2005: this test is not needed (and the corresponding clause
2332          --  in the RM is removed) because accessibility checks are sufficient
2333          --  to make handlers not at the library level illegal.
2334
2335          if Has_Interrupt_Handler (T)
2336            and then Ada_Version < Ada_05
2337          then
2338             Error_Msg_N
2339               ("interrupt object can only be declared at library level", Id);
2340          end if;
2341       end if;
2342
2343       --  The actual subtype of the object is the nominal subtype, unless
2344       --  the nominal one is unconstrained and obtained from the expression.
2345
2346       Act_T := T;
2347
2348       --  Process initialization expression if present and not in error
2349
2350       if Present (E) and then E /= Error then
2351          Mark_Coextensions (N, E);
2352          Analyze (E);
2353
2354          --  In case of errors detected in the analysis of the expression,
2355          --  decorate it with the expected type to avoid cascade errors
2356
2357          if No (Etype (E)) then
2358             Set_Etype (E, T);
2359          end if;
2360
2361          --  If an initialization expression is present, then we set the
2362          --  Is_True_Constant flag. It will be reset if this is a variable
2363          --  and it is indeed modified.
2364
2365          Set_Is_True_Constant (Id, True);
2366
2367          --  If we are analyzing a constant declaration, set its completion
2368          --  flag after analyzing the expression.
2369
2370          if Constant_Present (N) then
2371             Set_Has_Completion (Id);
2372          end if;
2373
2374          Set_Etype (Id, T);             --  may be overridden later on
2375          Resolve (E, T);
2376
2377          if not Assignment_OK (N) then
2378             Check_Initialization (T, E);
2379          end if;
2380
2381          Check_Unset_Reference (E);
2382
2383          --  If this is a variable, then set current value
2384
2385          if not Constant_Present (N) then
2386             if Compile_Time_Known_Value (E) then
2387                Set_Current_Value (Id, E);
2388             end if;
2389          end if;
2390
2391          --  Deal with setting of null flags
2392
2393          if Is_Access_Type (T) then
2394             if Known_Non_Null (E) then
2395                Set_Is_Known_Non_Null (Id, True);
2396             elsif Known_Null (E)
2397               and then not Can_Never_Be_Null (Id)
2398             then
2399                Set_Is_Known_Null (Id, True);
2400             end if;
2401          end if;
2402
2403          --  Check incorrect use of dynamically tagged expressions. Note
2404          --  the use of Is_Tagged_Type (T) which seems redundant but is in
2405          --  fact important to avoid spurious errors due to expanded code
2406          --  for dispatching functions over an anonymous access type
2407
2408          if (Is_Class_Wide_Type (Etype (E)) or else Is_Dynamically_Tagged (E))
2409            and then Is_Tagged_Type (T)
2410            and then not Is_Class_Wide_Type (T)
2411          then
2412             Error_Msg_N ("dynamically tagged expression not allowed!", E);
2413          end if;
2414
2415          Apply_Scalar_Range_Check (E, T);
2416          Apply_Static_Length_Check (E, T);
2417       end if;
2418
2419       --  If the No_Streams restriction is set, check that the type of the
2420       --  object is not, and does not contain, any subtype derived from
2421       --  Ada.Streams.Root_Stream_Type. Note that we guard the call to
2422       --  Has_Stream just for efficiency reasons. There is no point in
2423       --  spending time on a Has_Stream check if the restriction is not set.
2424
2425       if Restrictions.Set (No_Streams) then
2426          if Has_Stream (T) then
2427             Check_Restriction (No_Streams, N);
2428          end if;
2429       end if;
2430
2431       --  Abstract type is never permitted for a variable or constant.
2432       --  Note: we inhibit this check for objects that do not come from
2433       --  source because there is at least one case (the expansion of
2434       --  x'class'input where x is abstract) where we legitimately
2435       --  generate an abstract object.
2436
2437       if Is_Abstract_Type (T) and then Comes_From_Source (N) then
2438          Error_Msg_N ("type of object cannot be abstract",
2439                       Object_Definition (N));
2440
2441          if Is_CPP_Class (T) then
2442             Error_Msg_NE ("\} may need a cpp_constructor",
2443               Object_Definition (N), T);
2444          end if;
2445
2446       --  Case of unconstrained type
2447
2448       elsif Is_Indefinite_Subtype (T) then
2449
2450          --  Nothing to do in deferred constant case
2451
2452          if Constant_Present (N) and then No (E) then
2453             null;
2454
2455          --  Case of no initialization present
2456
2457          elsif No (E) then
2458             if No_Initialization (N) then
2459                null;
2460
2461             elsif Is_Class_Wide_Type (T) then
2462                Error_Msg_N
2463                  ("initialization required in class-wide declaration ", N);
2464
2465             else
2466                Error_Msg_N
2467                  ("unconstrained subtype not allowed (need initialization)",
2468                   Object_Definition (N));
2469             end if;
2470
2471          --  Case of initialization present but in error. Set initial
2472          --  expression as absent (but do not make above complaints)
2473
2474          elsif E = Error then
2475             Set_Expression (N, Empty);
2476             E := Empty;
2477
2478          --  Case of initialization present
2479
2480          else
2481             --  Not allowed in Ada 83
2482
2483             if not Constant_Present (N) then
2484                if Ada_Version = Ada_83
2485                  and then Comes_From_Source (Object_Definition (N))
2486                then
2487                   Error_Msg_N
2488                     ("(Ada 83) unconstrained variable not allowed",
2489                      Object_Definition (N));
2490                end if;
2491             end if;
2492
2493             --  Now we constrain the variable from the initializing expression
2494
2495             --  If the expression is an aggregate, it has been expanded into
2496             --  individual assignments. Retrieve the actual type from the
2497             --  expanded construct.
2498
2499             if Is_Array_Type (T)
2500               and then No_Initialization (N)
2501               and then Nkind (Original_Node (E)) = N_Aggregate
2502             then
2503                Act_T := Etype (E);
2504
2505             else
2506                Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
2507                Act_T := Find_Type_Of_Object (Object_Definition (N), N);
2508             end if;
2509
2510             Set_Is_Constr_Subt_For_U_Nominal (Act_T);
2511
2512             if Aliased_Present (N) then
2513                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2514             end if;
2515
2516             Freeze_Before (N, Act_T);
2517             Freeze_Before (N, T);
2518          end if;
2519
2520       elsif Is_Array_Type (T)
2521         and then No_Initialization (N)
2522         and then Nkind (Original_Node (E)) = N_Aggregate
2523       then
2524          if not Is_Entity_Name (Object_Definition (N)) then
2525             Act_T := Etype (E);
2526             Check_Compile_Time_Size (Act_T);
2527
2528             if Aliased_Present (N) then
2529                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2530             end if;
2531          end if;
2532
2533          --  When the given object definition and the aggregate are specified
2534          --  independently, and their lengths might differ do a length check.
2535          --  This cannot happen if the aggregate is of the form (others =>...)
2536
2537          if not Is_Constrained (T) then
2538             null;
2539
2540          elsif Nkind (E) = N_Raise_Constraint_Error then
2541
2542             --  Aggregate is statically illegal. Place back in declaration
2543
2544             Set_Expression (N, E);
2545             Set_No_Initialization (N, False);
2546
2547          elsif T = Etype (E) then
2548             null;
2549
2550          elsif Nkind (E) = N_Aggregate
2551            and then Present (Component_Associations (E))
2552            and then Present (Choices (First (Component_Associations (E))))
2553            and then Nkind (First
2554             (Choices (First (Component_Associations (E))))) = N_Others_Choice
2555          then
2556             null;
2557
2558          else
2559             Apply_Length_Check (E, T);
2560          end if;
2561
2562       --  If the type is limited unconstrained with defaulted discriminants
2563       --  and there is no expression, then the object is constrained by the
2564       --  defaults, so it is worthwhile building the corresponding subtype.
2565
2566       elsif (Is_Limited_Record (T)
2567                or else Is_Concurrent_Type (T))
2568         and then not Is_Constrained (T)
2569         and then Has_Discriminants (T)
2570       then
2571          if No (E) then
2572             Act_T := Build_Default_Subtype (T, N);
2573          else
2574             --  Ada 2005:  a limited object may be initialized by means of an
2575             --  aggregate. If the type has default discriminants it has an
2576             --  unconstrained nominal type, Its actual subtype will be obtained
2577             --  from the aggregate, and not from the default discriminants.
2578
2579             Act_T := Etype (E);
2580          end if;
2581
2582          Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
2583
2584       elsif Present (Underlying_Type (T))
2585         and then not Is_Constrained (Underlying_Type (T))
2586         and then Has_Discriminants (Underlying_Type (T))
2587         and then Nkind (E) = N_Function_Call
2588         and then Constant_Present (N)
2589       then
2590          --  The back-end has problems with constants of a discriminated type
2591          --  with defaults, if the initial value is a function call. We
2592          --  generate an intermediate temporary for the result of the call.
2593          --  It is unclear why this should make it acceptable to gcc. ???
2594
2595          Remove_Side_Effects (E);
2596       end if;
2597
2598       if T = Standard_Wide_Character or else T = Standard_Wide_Wide_Character
2599         or else Root_Type (T) = Standard_Wide_String
2600         or else Root_Type (T) = Standard_Wide_Wide_String
2601       then
2602          Check_Restriction (No_Wide_Characters, Object_Definition (N));
2603       end if;
2604
2605       --  Indicate this is not set in source. Certainly true for constants,
2606       --  and true for variables so far (will be reset for a variable if and
2607       --  when we encounter a modification in the source).
2608
2609       Set_Never_Set_In_Source (Id, True);
2610
2611       --  Now establish the proper kind and type of the object
2612
2613       if Constant_Present (N) then
2614          Set_Ekind            (Id, E_Constant);
2615          Set_Is_True_Constant (Id, True);
2616
2617       else
2618          Set_Ekind (Id, E_Variable);
2619
2620          --  A variable is set as shared passive if it appears in a shared
2621          --  passive package, and is at the outer level. This is not done
2622          --  for entities generated during expansion, because those are
2623          --  always manipulated locally.
2624
2625          if Is_Shared_Passive (Current_Scope)
2626            and then Is_Library_Level_Entity (Id)
2627            and then Comes_From_Source (Id)
2628          then
2629             Set_Is_Shared_Passive (Id);
2630             Check_Shared_Var (Id, T, N);
2631          end if;
2632
2633          --  Set Has_Initial_Value if initializing expression present. Note
2634          --  that if there is no initializating expression, we leave the state
2635          --  of this flag unchanged (usually it will be False, but notably in
2636          --  the case of exception choice variables, it will already be true).
2637
2638          if Present (E) then
2639             Set_Has_Initial_Value (Id, True);
2640          end if;
2641       end if;
2642
2643       --  Initialize alignment and size
2644
2645       Init_Alignment (Id);
2646       Init_Esize     (Id);
2647
2648       --  Deal with aliased case
2649
2650       if Aliased_Present (N) then
2651          Set_Is_Aliased (Id);
2652
2653          --  If the object is aliased and the type is unconstrained with
2654          --  defaulted discriminants and there is no expression, then the
2655          --  object is constrained by the defaults, so it is worthwhile
2656          --  building the corresponding subtype.
2657
2658          --  Ada 2005 (AI-363): If the aliased object is discriminated and
2659          --  unconstrained, then only establish an actual subtype if the
2660          --  nominal subtype is indefinite. In definite cases the object is
2661          --  unconstrained in Ada 2005.
2662
2663          if No (E)
2664            and then Is_Record_Type (T)
2665            and then not Is_Constrained (T)
2666            and then Has_Discriminants (T)
2667            and then (Ada_Version < Ada_05 or else Is_Indefinite_Subtype (T))
2668          then
2669             Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
2670          end if;
2671       end if;
2672
2673       --  Now we can set the type of the object
2674
2675       Set_Etype (Id, Act_T);
2676
2677       --  Deal with controlled types
2678
2679       if Has_Controlled_Component (Etype (Id))
2680         or else Is_Controlled (Etype (Id))
2681       then
2682          if not Is_Library_Level_Entity (Id) then
2683             Check_Restriction (No_Nested_Finalization, N);
2684          else
2685             Validate_Controlled_Object (Id);
2686          end if;
2687
2688          --  Generate a warning when an initialization causes an obvious ABE
2689          --  violation. If the init expression is a simple aggregate there
2690          --  shouldn't be any initialize/adjust call generated. This will be
2691          --  true as soon as aggregates are built in place when possible.
2692
2693          --  ??? at the moment we do not generate warnings for temporaries
2694          --  created for those aggregates although Program_Error might be
2695          --  generated if compiled with -gnato.
2696
2697          if Is_Controlled (Etype (Id))
2698             and then Comes_From_Source (Id)
2699          then
2700             declare
2701                BT : constant Entity_Id := Base_Type (Etype (Id));
2702
2703                Implicit_Call : Entity_Id;
2704                pragma Warnings (Off, Implicit_Call);
2705                --  ??? what is this for (never referenced!)
2706
2707                function Is_Aggr (N : Node_Id) return Boolean;
2708                --  Check that N is an aggregate
2709
2710                -------------
2711                -- Is_Aggr --
2712                -------------
2713
2714                function Is_Aggr (N : Node_Id) return Boolean is
2715                begin
2716                   case Nkind (Original_Node (N)) is
2717                      when N_Aggregate | N_Extension_Aggregate =>
2718                         return True;
2719
2720                      when N_Qualified_Expression |
2721                           N_Type_Conversion      |
2722                           N_Unchecked_Type_Conversion =>
2723                         return Is_Aggr (Expression (Original_Node (N)));
2724
2725                      when others =>
2726                         return False;
2727                   end case;
2728                end Is_Aggr;
2729
2730             begin
2731                --  If no underlying type, we already are in an error situation.
2732                --  Do not try to add a warning since we do not have access to
2733                --  prim-op list.
2734
2735                if No (Underlying_Type (BT)) then
2736                   Implicit_Call := Empty;
2737
2738                --  A generic type does not have usable primitive operators.
2739                --  Initialization calls are built for instances.
2740
2741                elsif Is_Generic_Type (BT) then
2742                   Implicit_Call := Empty;
2743
2744                --  If the init expression is not an aggregate, an adjust call
2745                --  will be generated
2746
2747                elsif Present (E) and then not Is_Aggr (E) then
2748                   Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
2749
2750                --  If no init expression and we are not in the deferred
2751                --  constant case, an Initialize call will be generated
2752
2753                elsif No (E) and then not Constant_Present (N) then
2754                   Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
2755
2756                else
2757                   Implicit_Call := Empty;
2758                end if;
2759             end;
2760          end if;
2761       end if;
2762
2763       if Has_Task (Etype (Id)) then
2764          Check_Restriction (No_Tasking, N);
2765
2766          if Is_Library_Level_Entity (Id) then
2767             Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
2768          else
2769             Check_Restriction (Max_Tasks, N);
2770             Check_Restriction (No_Task_Hierarchy, N);
2771             Check_Potentially_Blocking_Operation (N);
2772          end if;
2773
2774          --  A rather specialized test. If we see two tasks being declared
2775          --  of the same type in the same object declaration, and the task
2776          --  has an entry with an address clause, we know that program error
2777          --  will be raised at run-time since we can't have two tasks with
2778          --  entries at the same address.
2779
2780          if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
2781             declare
2782                E : Entity_Id;
2783
2784             begin
2785                E := First_Entity (Etype (Id));
2786                while Present (E) loop
2787                   if Ekind (E) = E_Entry
2788                     and then Present (Get_Attribute_Definition_Clause
2789                                         (E, Attribute_Address))
2790                   then
2791                      Error_Msg_N
2792                        ("?more than one task with same entry address", N);
2793                      Error_Msg_N
2794                        ("\?Program_Error will be raised at run time", N);
2795                      Insert_Action (N,
2796                        Make_Raise_Program_Error (Loc,
2797                          Reason => PE_Duplicated_Entry_Address));
2798                      exit;
2799                   end if;
2800
2801                   Next_Entity (E);
2802                end loop;
2803             end;
2804          end if;
2805       end if;
2806
2807       --  Some simple constant-propagation: if the expression is a constant
2808       --  string initialized with a literal, share the literal. This avoids
2809       --  a run-time copy.
2810
2811       if Present (E)
2812         and then Is_Entity_Name (E)
2813         and then Ekind (Entity (E)) = E_Constant
2814         and then Base_Type (Etype (E)) = Standard_String
2815       then
2816          declare
2817             Val : constant Node_Id := Constant_Value (Entity (E));
2818          begin
2819             if Present (Val)
2820               and then Nkind (Val) = N_String_Literal
2821             then
2822                Rewrite (E, New_Copy (Val));
2823             end if;
2824          end;
2825       end if;
2826
2827       --  Another optimization: if the nominal subtype is unconstrained and
2828       --  the expression is a function call that returns an unconstrained
2829       --  type, rewrite the declaration as a renaming of the result of the
2830       --  call. The exceptions below are cases where the copy is expected,
2831       --  either by the back end (Aliased case) or by the semantics, as for
2832       --  initializing controlled types or copying tags for classwide types.
2833
2834       if Present (E)
2835         and then Nkind (E) = N_Explicit_Dereference
2836         and then Nkind (Original_Node (E)) = N_Function_Call
2837         and then not Is_Library_Level_Entity (Id)
2838         and then not Is_Constrained (Underlying_Type (T))
2839         and then not Is_Aliased (Id)
2840         and then not Is_Class_Wide_Type (T)
2841         and then not Is_Controlled (T)
2842         and then not Has_Controlled_Component (Base_Type (T))
2843         and then Expander_Active
2844       then
2845          Rewrite (N,
2846            Make_Object_Renaming_Declaration (Loc,
2847              Defining_Identifier => Id,
2848              Access_Definition   => Empty,
2849              Subtype_Mark        => New_Occurrence_Of
2850                                       (Base_Type (Etype (Id)), Loc),
2851              Name                => E));
2852
2853          Set_Renamed_Object (Id, E);
2854
2855          --  Force generation of debugging information for the constant and for
2856          --  the renamed function call.
2857
2858          Set_Needs_Debug_Info (Id);
2859          Set_Needs_Debug_Info (Entity (Prefix (E)));
2860       end if;
2861
2862       if Present (Prev_Entity)
2863         and then Is_Frozen (Prev_Entity)
2864         and then not Error_Posted (Id)
2865       then
2866          Error_Msg_N ("full constant declaration appears too late", N);
2867       end if;
2868
2869       Check_Eliminated (Id);
2870
2871       --  Deal with setting In_Private_Part flag if in private part
2872
2873       if Ekind (Scope (Id)) = E_Package
2874         and then In_Private_Part (Scope (Id))
2875       then
2876          Set_In_Private_Part (Id);
2877       end if;
2878    end Analyze_Object_Declaration;
2879
2880    ---------------------------
2881    -- Analyze_Others_Choice --
2882    ---------------------------
2883
2884    --  Nothing to do for the others choice node itself, the semantic analysis
2885    --  of the others choice will occur as part of the processing of the parent
2886
2887    procedure Analyze_Others_Choice (N : Node_Id) is
2888       pragma Warnings (Off, N);
2889    begin
2890       null;
2891    end Analyze_Others_Choice;
2892
2893    --------------------------------
2894    -- Analyze_Per_Use_Expression --
2895    --------------------------------
2896
2897    procedure Analyze_Per_Use_Expression (N : Node_Id; T : Entity_Id) is
2898       Save_In_Default_Expression : constant Boolean := In_Default_Expression;
2899    begin
2900       In_Default_Expression := True;
2901       Pre_Analyze_And_Resolve (N, T);
2902       In_Default_Expression := Save_In_Default_Expression;
2903    end Analyze_Per_Use_Expression;
2904
2905    -------------------------------------------
2906    -- Analyze_Private_Extension_Declaration --
2907    -------------------------------------------
2908
2909    procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
2910       T           : constant Entity_Id := Defining_Identifier (N);
2911       Indic       : constant Node_Id   := Subtype_Indication (N);
2912       Parent_Type : Entity_Id;
2913       Parent_Base : Entity_Id;
2914
2915    begin
2916       --  Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
2917
2918       if Is_Non_Empty_List (Interface_List (N)) then
2919          declare
2920             Intf : Node_Id;
2921             T    : Entity_Id;
2922
2923          begin
2924             Intf := First (Interface_List (N));
2925             while Present (Intf) loop
2926                T := Find_Type_Of_Subtype_Indic (Intf);
2927
2928                if not Is_Interface (T) then
2929                   Error_Msg_NE ("(Ada 2005) & must be an interface", Intf, T);
2930                end if;
2931
2932                Next (Intf);
2933             end loop;
2934          end;
2935       end if;
2936
2937       Generate_Definition (T);
2938       Enter_Name (T);
2939
2940       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
2941       Parent_Base := Base_Type (Parent_Type);
2942
2943       if Parent_Type = Any_Type
2944         or else Etype (Parent_Type) = Any_Type
2945       then
2946          Set_Ekind (T, Ekind (Parent_Type));
2947          Set_Etype (T, Any_Type);
2948          return;
2949
2950       elsif not Is_Tagged_Type (Parent_Type) then
2951          Error_Msg_N
2952            ("parent of type extension must be a tagged type ", Indic);
2953          return;
2954
2955       elsif Ekind (Parent_Type) = E_Void
2956         or else Ekind (Parent_Type) = E_Incomplete_Type
2957       then
2958          Error_Msg_N ("premature derivation of incomplete type", Indic);
2959          return;
2960
2961       elsif Is_Concurrent_Type (Parent_Type) then
2962          Error_Msg_N
2963            ("parent type of a private extension cannot be "
2964             & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
2965
2966          Set_Etype              (T, Any_Type);
2967          Set_Ekind              (T, E_Limited_Private_Type);
2968          Set_Private_Dependents (T, New_Elmt_List);
2969          Set_Error_Posted       (T);
2970          return;
2971       end if;
2972
2973       --  Perhaps the parent type should be changed to the class-wide type's
2974       --  specific type in this case to prevent cascading errors ???
2975
2976       if Is_Class_Wide_Type (Parent_Type) then
2977          Error_Msg_N
2978            ("parent of type extension must not be a class-wide type", Indic);
2979          return;
2980       end if;
2981
2982       if (not Is_Package_Or_Generic_Package (Current_Scope)
2983            and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
2984         or else In_Private_Part (Current_Scope)
2985
2986       then
2987          Error_Msg_N ("invalid context for private extension", N);
2988       end if;
2989
2990       --  Set common attributes
2991
2992       Set_Is_Pure          (T, Is_Pure (Current_Scope));
2993       Set_Scope            (T, Current_Scope);
2994       Set_Ekind            (T, E_Record_Type_With_Private);
2995       Init_Size_Align      (T);
2996
2997       Set_Etype            (T,            Parent_Base);
2998       Set_Has_Task         (T, Has_Task  (Parent_Base));
2999
3000       Set_Convention       (T, Convention     (Parent_Type));
3001       Set_First_Rep_Item   (T, First_Rep_Item (Parent_Type));
3002       Set_Is_First_Subtype (T);
3003       Make_Class_Wide_Type (T);
3004
3005       if Unknown_Discriminants_Present (N) then
3006          Set_Discriminant_Constraint (T, No_Elist);
3007       end if;
3008
3009       Build_Derived_Record_Type (N, Parent_Type, T);
3010
3011       --  Ada 2005 (AI-443): Synchronized private extension or a rewritten
3012       --  synchronized formal derived type.
3013
3014       if Ada_Version >= Ada_05
3015         and then Synchronized_Present (N)
3016       then
3017          Set_Is_Limited_Record (T);
3018
3019          --  Formal derived type case
3020
3021          if Is_Generic_Type (T) then
3022
3023             --  The parent must be a tagged limited type or a synchronized
3024             --  interface.
3025
3026             if (not Is_Tagged_Type (Parent_Type)
3027                   or else not Is_Limited_Type (Parent_Type))
3028               and then
3029                (not Is_Interface (Parent_Type)
3030                   or else not Is_Synchronized_Interface (Parent_Type))
3031             then
3032                Error_Msg_NE ("parent type of & must be tagged limited " &
3033                              "or synchronized", N, T);
3034             end if;
3035
3036             --  The progenitors (if any) must be limited or synchronized
3037             --  interfaces.
3038
3039             if Present (Abstract_Interfaces (T)) then
3040                declare
3041                   Iface      : Entity_Id;
3042                   Iface_Elmt : Elmt_Id;
3043
3044                begin
3045                   Iface_Elmt := First_Elmt (Abstract_Interfaces (T));
3046                   while Present (Iface_Elmt) loop
3047                      Iface := Node (Iface_Elmt);
3048
3049                      if not Is_Limited_Interface (Iface)
3050                        and then not Is_Synchronized_Interface (Iface)
3051                      then
3052                         Error_Msg_NE ("progenitor & must be limited " &
3053                                       "or synchronized", N, Iface);
3054                      end if;
3055
3056                      Next_Elmt (Iface_Elmt);
3057                   end loop;
3058                end;
3059             end if;
3060
3061          --  Regular derived extension, the parent must be a limited or
3062          --  synchronized interface.
3063
3064          else
3065             if not Is_Interface (Parent_Type)
3066               or else (not Is_Limited_Interface (Parent_Type)
3067                          and then
3068                        not Is_Synchronized_Interface (Parent_Type))
3069             then
3070                Error_Msg_NE
3071                  ("parent type of & must be limited interface", N, T);
3072             end if;
3073          end if;
3074
3075       elsif Limited_Present (N) then
3076          Set_Is_Limited_Record (T);
3077
3078          if not Is_Limited_Type (Parent_Type)
3079            and then
3080              (not Is_Interface (Parent_Type)
3081                or else not Is_Limited_Interface (Parent_Type))
3082          then
3083             Error_Msg_NE ("parent type& of limited extension must be limited",
3084               N, Parent_Type);
3085          end if;
3086       end if;
3087    end Analyze_Private_Extension_Declaration;
3088
3089    ---------------------------------
3090    -- Analyze_Subtype_Declaration --
3091    ---------------------------------
3092
3093    procedure Analyze_Subtype_Declaration
3094      (N    : Node_Id;
3095       Skip : Boolean := False)
3096    is
3097       Id       : constant Entity_Id := Defining_Identifier (N);
3098       T        : Entity_Id;
3099       R_Checks : Check_Result;
3100
3101    begin
3102       Generate_Definition (Id);
3103       Set_Is_Pure (Id, Is_Pure (Current_Scope));
3104       Init_Size_Align (Id);
3105
3106       --  The following guard condition on Enter_Name is to handle cases where
3107       --  the defining identifier has already been entered into the scope but
3108       --  the declaration as a whole needs to be analyzed.
3109
3110       --  This case in particular happens for derived enumeration types. The
3111       --  derived enumeration type is processed as an inserted enumeration type
3112       --  declaration followed by a rewritten subtype declaration. The defining
3113       --  identifier, however, is entered into the name scope very early in the
3114       --  processing of the original type declaration and therefore needs to be
3115       --  avoided here, when the created subtype declaration is analyzed. (See
3116       --  Build_Derived_Types)
3117
3118       --  This also happens when the full view of a private type is derived
3119       --  type with constraints. In this case the entity has been introduced
3120       --  in the private declaration.
3121
3122       if Skip
3123         or else (Present (Etype (Id))
3124                    and then (Is_Private_Type (Etype (Id))
3125                                or else Is_Task_Type (Etype (Id))
3126                                or else Is_Rewrite_Substitution (N)))
3127       then
3128          null;
3129
3130       else
3131          Enter_Name (Id);
3132       end if;
3133
3134       T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
3135
3136       --  Inherit common attributes
3137
3138       Set_Is_Generic_Type   (Id, Is_Generic_Type   (Base_Type (T)));
3139       Set_Is_Volatile       (Id, Is_Volatile       (T));
3140       Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
3141       Set_Is_Atomic         (Id, Is_Atomic         (T));
3142       Set_Is_Ada_2005_Only  (Id, Is_Ada_2005_Only  (T));
3143
3144       --  In the case where there is no constraint given in the subtype
3145       --  indication, Process_Subtype just returns the Subtype_Mark, so its
3146       --  semantic attributes must be established here.
3147
3148       if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
3149          Set_Etype (Id, Base_Type (T));
3150
3151          case Ekind (T) is
3152             when Array_Kind =>
3153                Set_Ekind                       (Id, E_Array_Subtype);
3154                Copy_Array_Subtype_Attributes   (Id, T);
3155
3156             when Decimal_Fixed_Point_Kind =>
3157                Set_Ekind                (Id, E_Decimal_Fixed_Point_Subtype);
3158                Set_Digits_Value         (Id, Digits_Value       (T));
3159                Set_Delta_Value          (Id, Delta_Value        (T));
3160                Set_Scale_Value          (Id, Scale_Value        (T));
3161                Set_Small_Value          (Id, Small_Value        (T));
3162                Set_Scalar_Range         (Id, Scalar_Range       (T));
3163                Set_Machine_Radix_10     (Id, Machine_Radix_10   (T));
3164                Set_Is_Constrained       (Id, Is_Constrained     (T));
3165                Set_RM_Size              (Id, RM_Size            (T));
3166
3167             when Enumeration_Kind =>
3168                Set_Ekind                (Id, E_Enumeration_Subtype);
3169                Set_First_Literal        (Id, First_Literal (Base_Type (T)));
3170                Set_Scalar_Range         (Id, Scalar_Range       (T));
3171                Set_Is_Character_Type    (Id, Is_Character_Type  (T));
3172                Set_Is_Constrained       (Id, Is_Constrained     (T));
3173                Set_RM_Size              (Id, RM_Size            (T));
3174
3175             when Ordinary_Fixed_Point_Kind =>
3176                Set_Ekind                (Id, E_Ordinary_Fixed_Point_Subtype);
3177                Set_Scalar_Range         (Id, Scalar_Range       (T));
3178                Set_Small_Value          (Id, Small_Value        (T));
3179                Set_Delta_Value          (Id, Delta_Value        (T));
3180                Set_Is_Constrained       (Id, Is_Constrained     (T));
3181                Set_RM_Size              (Id, RM_Size            (T));
3182
3183             when Float_Kind =>
3184                Set_Ekind                (Id, E_Floating_Point_Subtype);
3185                Set_Scalar_Range         (Id, Scalar_Range       (T));
3186                Set_Digits_Value         (Id, Digits_Value       (T));
3187                Set_Is_Constrained       (Id, Is_Constrained     (T));
3188
3189             when Signed_Integer_Kind =>
3190                Set_Ekind                (Id, E_Signed_Integer_Subtype);
3191                Set_Scalar_Range         (Id, Scalar_Range       (T));
3192                Set_Is_Constrained       (Id, Is_Constrained     (T));
3193                Set_RM_Size              (Id, RM_Size            (T));
3194
3195             when Modular_Integer_Kind =>
3196                Set_Ekind                (Id, E_Modular_Integer_Subtype);
3197                Set_Scalar_Range         (Id, Scalar_Range       (T));
3198                Set_Is_Constrained       (Id, Is_Constrained     (T));
3199                Set_RM_Size              (Id, RM_Size            (T));
3200
3201             when Class_Wide_Kind =>
3202                Set_Ekind                (Id, E_Class_Wide_Subtype);
3203                Set_First_Entity         (Id, First_Entity       (T));
3204                Set_Last_Entity          (Id, Last_Entity        (T));
3205                Set_Class_Wide_Type      (Id, Class_Wide_Type    (T));
3206                Set_Cloned_Subtype       (Id, T);
3207                Set_Is_Tagged_Type       (Id, True);
3208                Set_Has_Unknown_Discriminants
3209                                         (Id, True);
3210
3211                if Ekind (T) = E_Class_Wide_Subtype then
3212                   Set_Equivalent_Type   (Id, Equivalent_Type    (T));
3213                end if;
3214
3215             when E_Record_Type | E_Record_Subtype =>
3216                Set_Ekind                (Id, E_Record_Subtype);
3217
3218                if Ekind (T) = E_Record_Subtype
3219                  and then Present (Cloned_Subtype (T))
3220                then
3221                   Set_Cloned_Subtype    (Id, Cloned_Subtype (T));
3222                else
3223                   Set_Cloned_Subtype    (Id, T);
3224                end if;
3225
3226                Set_First_Entity         (Id, First_Entity       (T));
3227                Set_Last_Entity          (Id, Last_Entity        (T));
3228                Set_Has_Discriminants    (Id, Has_Discriminants  (T));
3229                Set_Is_Constrained       (Id, Is_Constrained     (T));
3230                Set_Is_Limited_Record    (Id, Is_Limited_Record  (T));
3231                Set_Has_Unknown_Discriminants
3232                                         (Id, Has_Unknown_Discriminants (T));
3233
3234                if Has_Discriminants (T) then
3235                   Set_Discriminant_Constraint
3236                                         (Id, Discriminant_Constraint (T));
3237                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3238
3239                elsif Has_Unknown_Discriminants (Id) then
3240                   Set_Discriminant_Constraint (Id, No_Elist);
3241                end if;
3242
3243                if Is_Tagged_Type (T) then
3244                   Set_Is_Tagged_Type    (Id);
3245                   Set_Is_Abstract_Type  (Id, Is_Abstract_Type (T));
3246                   Set_Primitive_Operations
3247                                         (Id, Primitive_Operations (T));
3248                   Set_Class_Wide_Type   (Id, Class_Wide_Type (T));
3249
3250                   if Is_Interface (T) then
3251                      Set_Is_Interface (Id);
3252                      Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
3253                   end if;
3254                end if;
3255
3256             when Private_Kind =>
3257                Set_Ekind              (Id, Subtype_Kind (Ekind   (T)));
3258                Set_Has_Discriminants  (Id, Has_Discriminants     (T));
3259                Set_Is_Constrained     (Id, Is_Constrained        (T));
3260                Set_First_Entity       (Id, First_Entity          (T));
3261                Set_Last_Entity        (Id, Last_Entity           (T));
3262                Set_Private_Dependents (Id, New_Elmt_List);
3263                Set_Is_Limited_Record  (Id, Is_Limited_Record     (T));
3264                Set_Has_Unknown_Discriminants
3265                                       (Id, Has_Unknown_Discriminants (T));
3266
3267                if Is_Tagged_Type (T) then
3268                   Set_Is_Tagged_Type       (Id);
3269                   Set_Is_Abstract_Type     (Id, Is_Abstract_Type (T));
3270                   Set_Primitive_Operations (Id, Primitive_Operations (T));
3271                   Set_Class_Wide_Type      (Id, Class_Wide_Type (T));
3272                end if;
3273
3274                --  In general the attributes of the subtype of a private type
3275                --  are the attributes of the partial view of parent. However,
3276                --  the full view may be a discriminated type, and the subtype
3277                --  must share the discriminant constraint to generate correct
3278                --  calls to initialization procedures.
3279
3280                if Has_Discriminants (T) then
3281                   Set_Discriminant_Constraint
3282                                      (Id, Discriminant_Constraint (T));
3283                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3284
3285                elsif Present (Full_View (T))
3286                  and then Has_Discriminants (Full_View (T))
3287                then
3288                   Set_Discriminant_Constraint
3289                                (Id, Discriminant_Constraint (Full_View (T)));
3290                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3291
3292                   --  This would seem semantically correct, but apparently
3293                   --  confuses the back-end (4412-009). To be explained ???
3294
3295                   --  Set_Has_Discriminants (Id);
3296                end if;
3297
3298                Prepare_Private_Subtype_Completion (Id, N);
3299
3300             when Access_Kind =>
3301                Set_Ekind             (Id, E_Access_Subtype);
3302                Set_Is_Constrained    (Id, Is_Constrained        (T));
3303                Set_Is_Access_Constant
3304                                      (Id, Is_Access_Constant    (T));
3305                Set_Directly_Designated_Type
3306                                      (Id, Designated_Type       (T));
3307                Set_Can_Never_Be_Null (Id, Can_Never_Be_Null     (T));
3308
3309                --  A Pure library_item must not contain the declaration of a
3310                --  named access type, except within a subprogram, generic
3311                --  subprogram, task unit, or protected unit (RM 10.2.1(16)).
3312
3313                if Comes_From_Source (Id)
3314                  and then In_Pure_Unit
3315                  and then not In_Subprogram_Task_Protected_Unit
3316                then
3317                   Error_Msg_N
3318                     ("named access types not allowed in pure unit", N);
3319                end if;
3320
3321             when Concurrent_Kind =>
3322                Set_Ekind                (Id, Subtype_Kind (Ekind   (T)));
3323                Set_Corresponding_Record_Type (Id,
3324                                          Corresponding_Record_Type (T));
3325                Set_First_Entity         (Id, First_Entity          (T));
3326                Set_First_Private_Entity (Id, First_Private_Entity  (T));
3327                Set_Has_Discriminants    (Id, Has_Discriminants     (T));
3328                Set_Is_Constrained       (Id, Is_Constrained        (T));
3329                Set_Is_Tagged_Type       (Id, Is_Tagged_Type        (T));
3330                Set_Last_Entity          (Id, Last_Entity           (T));
3331
3332                if Has_Discriminants (T) then
3333                   Set_Discriminant_Constraint (Id,
3334                                            Discriminant_Constraint (T));
3335                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3336                end if;
3337
3338             when E_Incomplete_Type =>
3339                if Ada_Version >= Ada_05 then
3340                   Set_Ekind (Id, E_Incomplete_Subtype);
3341
3342                   --  Ada 2005 (AI-412): Decorate an incomplete subtype
3343                   --  of an incomplete type visible through a limited
3344                   --  with clause.
3345
3346                   if From_With_Type (T)
3347                     and then Present (Non_Limited_View (T))
3348                   then
3349                      Set_From_With_Type   (Id);
3350                      Set_Non_Limited_View (Id, Non_Limited_View (T));
3351
3352                   --  Ada 2005 (AI-412): Add the regular incomplete subtype
3353                   --  to the private dependents of the original incomplete
3354                   --  type for future transformation.
3355
3356                   else
3357                      Append_Elmt (Id, Private_Dependents (T));
3358                   end if;
3359
3360                --  If the subtype name denotes an incomplete type an error
3361                --  was already reported by Process_Subtype.
3362
3363                else
3364                   Set_Etype (Id, Any_Type);
3365                end if;
3366
3367             when others =>
3368                raise Program_Error;
3369          end case;
3370       end if;
3371
3372       if Etype (Id) = Any_Type then
3373          return;
3374       end if;
3375
3376       --  Some common processing on all types
3377
3378       Set_Size_Info      (Id,                 T);
3379       Set_First_Rep_Item (Id, First_Rep_Item (T));
3380
3381       T := Etype (Id);
3382
3383       Set_Is_Immediately_Visible (Id, True);
3384       Set_Depends_On_Private     (Id, Has_Private_Component (T));
3385
3386       if Is_Interface (T) then
3387          Set_Is_Interface (Id);
3388       end if;
3389
3390       if Present (Generic_Parent_Type (N))
3391         and then
3392           (Nkind
3393              (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
3394             or else Nkind
3395               (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
3396                 /=  N_Formal_Private_Type_Definition)
3397       then
3398          if Is_Tagged_Type (Id) then
3399
3400             --  If this is a generic actual subtype for a synchronized type,
3401             --  the primitive operations are those of the corresponding record
3402             --  for which there is a separate subtype declaration.
3403
3404             if Is_Concurrent_Type (Id) then
3405                null;
3406             elsif Is_Class_Wide_Type (Id) then
3407                Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
3408             else
3409                Derive_Subprograms (Generic_Parent_Type (N), Id, T);
3410             end if;
3411
3412          elsif Scope (Etype (Id)) /= Standard_Standard then
3413             Derive_Subprograms (Generic_Parent_Type (N), Id);
3414          end if;
3415       end if;
3416
3417       if Is_Private_Type (T)
3418         and then Present (Full_View (T))
3419       then
3420          Conditional_Delay (Id, Full_View (T));
3421
3422       --  The subtypes of components or subcomponents of protected types
3423       --  do not need freeze nodes, which would otherwise appear in the
3424       --  wrong scope (before the freeze node for the protected type). The
3425       --  proper subtypes are those of the subcomponents of the corresponding
3426       --  record.
3427
3428       elsif Ekind (Scope (Id)) /= E_Protected_Type
3429         and then Present (Scope (Scope (Id))) -- error defense!
3430         and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
3431       then
3432          Conditional_Delay (Id, T);
3433       end if;
3434
3435       --  Check that constraint_error is raised for a scalar subtype
3436       --  indication when the lower or upper bound of a non-null range
3437       --  lies outside the range of the type mark.
3438
3439       if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
3440          if Is_Scalar_Type (Etype (Id))
3441             and then Scalar_Range (Id) /=
3442                      Scalar_Range (Etype (Subtype_Mark
3443                                            (Subtype_Indication (N))))
3444          then
3445             Apply_Range_Check
3446               (Scalar_Range (Id),
3447                Etype (Subtype_Mark (Subtype_Indication (N))));
3448
3449          elsif Is_Array_Type (Etype (Id))
3450            and then Present (First_Index (Id))
3451          then
3452             --  This really should be a subprogram that finds the indications
3453             --  to check???
3454
3455             if ((Nkind (First_Index (Id)) = N_Identifier
3456                    and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
3457                  or else Nkind (First_Index (Id)) = N_Subtype_Indication)
3458               and then
3459                 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
3460             then
3461                declare
3462                   Target_Typ : constant Entity_Id :=
3463                                  Etype
3464                                    (First_Index (Etype
3465                                      (Subtype_Mark (Subtype_Indication (N)))));
3466                begin
3467                   R_Checks :=
3468                     Get_Range_Checks
3469                       (Scalar_Range (Etype (First_Index (Id))),
3470                        Target_Typ,
3471                        Etype (First_Index (Id)),
3472                        Defining_Identifier (N));
3473
3474                   Insert_Range_Checks
3475                     (R_Checks,
3476                      N,
3477                      Target_Typ,
3478                      Sloc (Defining_Identifier (N)));
3479                end;
3480             end if;
3481          end if;
3482       end if;
3483
3484       Check_Eliminated (Id);
3485    end Analyze_Subtype_Declaration;
3486
3487    --------------------------------
3488    -- Analyze_Subtype_Indication --
3489    --------------------------------
3490
3491    procedure Analyze_Subtype_Indication (N : Node_Id) is
3492       T : constant Entity_Id := Subtype_Mark (N);
3493       R : constant Node_Id   := Range_Expression (Constraint (N));
3494
3495    begin
3496       Analyze (T);
3497
3498       if R /= Error then
3499          Analyze (R);
3500          Set_Etype (N, Etype (R));
3501          Resolve (R, Entity (T));
3502       else
3503          Set_Error_Posted (R);
3504          Set_Error_Posted (T);
3505       end if;
3506    end Analyze_Subtype_Indication;
3507
3508    ------------------------------
3509    -- Analyze_Type_Declaration --
3510    ------------------------------
3511
3512    procedure Analyze_Type_Declaration (N : Node_Id) is
3513       Def    : constant Node_Id   := Type_Definition (N);
3514       Def_Id : constant Entity_Id := Defining_Identifier (N);
3515       T      : Entity_Id;
3516       Prev   : Entity_Id;
3517
3518       Is_Remote : constant Boolean :=
3519                     (Is_Remote_Types (Current_Scope)
3520                        or else Is_Remote_Call_Interface (Current_Scope))
3521                     and then not (In_Private_Part (Current_Scope)
3522                                     or else In_Package_Body (Current_Scope));
3523
3524       procedure Check_Ops_From_Incomplete_Type;
3525       --  If there is a tagged incomplete partial view of the type, transfer
3526       --  its operations to the full view, and indicate that the type of the
3527       --  controlling parameter (s) is this full view.
3528
3529       ------------------------------------
3530       -- Check_Ops_From_Incomplete_Type --
3531       ------------------------------------
3532
3533       procedure Check_Ops_From_Incomplete_Type is
3534          Elmt   : Elmt_Id;
3535          Formal : Entity_Id;
3536          Op     : Entity_Id;
3537
3538       begin
3539          if Prev /= T
3540            and then Ekind (Prev) = E_Incomplete_Type
3541            and then Is_Tagged_Type (Prev)
3542            and then Is_Tagged_Type (T)
3543          then
3544             Elmt := First_Elmt (Primitive_Operations (Prev));
3545             while Present (Elmt) loop
3546                Op := Node (Elmt);
3547                Prepend_Elmt (Op, Primitive_Operations (T));
3548
3549                Formal := First_Formal (Op);
3550                while Present (Formal) loop
3551                   if Etype (Formal) = Prev then
3552                      Set_Etype (Formal, T);
3553                   end if;
3554
3555                   Next_Formal (Formal);
3556                end loop;
3557
3558                if Etype (Op) = Prev then
3559                   Set_Etype (Op, T);
3560                end if;
3561
3562                Next_Elmt (Elmt);
3563             end loop;
3564          end if;
3565       end Check_Ops_From_Incomplete_Type;
3566
3567    --  Start of processing for Analyze_Type_Declaration
3568
3569    begin
3570       Prev := Find_Type_Name (N);
3571
3572       --  The full view, if present, now points to the current type
3573
3574       --  Ada 2005 (AI-50217): If the type was previously decorated when
3575       --  imported through a LIMITED WITH clause, it appears as incomplete
3576       --  but has no full view.
3577       --  If the incomplete view is tagged, a class_wide type has been
3578       --  created already. Use it for the full view as well, to prevent
3579       --  multiple incompatible class-wide types that may be  created for
3580       --  self-referential anonymous access components.
3581
3582       if Ekind (Prev) = E_Incomplete_Type
3583         and then Present (Full_View (Prev))
3584       then
3585          T := Full_View (Prev);
3586
3587          if Is_Tagged_Type (Prev)
3588            and then Present (Class_Wide_Type (Prev))
3589          then
3590             Set_Ekind (T, Ekind (Prev));         --  will be reset later
3591             Set_Class_Wide_Type (T, Class_Wide_Type (Prev));
3592             Set_Etype (Class_Wide_Type (T), T);
3593          end if;
3594
3595       else
3596          T := Prev;
3597       end if;
3598
3599       Set_Is_Pure (T, Is_Pure (Current_Scope));
3600
3601       --  We set the flag Is_First_Subtype here. It is needed to set the
3602       --  corresponding flag for the Implicit class-wide-type created
3603       --  during tagged types processing.
3604
3605       Set_Is_First_Subtype (T, True);
3606
3607       --  Only composite types other than array types are allowed to have
3608       --  discriminants.
3609
3610       case Nkind (Def) is
3611
3612          --  For derived types, the rule will be checked once we've figured
3613          --  out the parent type.
3614
3615          when N_Derived_Type_Definition =>
3616             null;
3617
3618          --  For record types, discriminants are allowed
3619
3620          when N_Record_Definition =>
3621             null;
3622
3623          when others =>
3624             if Present (Discriminant_Specifications (N)) then
3625                Error_Msg_N
3626                  ("elementary or array type cannot have discriminants",
3627                   Defining_Identifier
3628                   (First (Discriminant_Specifications (N))));
3629             end if;
3630       end case;
3631
3632       --  Elaborate the type definition according to kind, and generate
3633       --  subsidiary (implicit) subtypes where needed. We skip this if it was
3634       --  already done (this happens during the reanalysis that follows a call
3635       --  to the high level optimizer).
3636
3637       if not Analyzed (T) then
3638          Set_Analyzed (T);
3639
3640          case Nkind (Def) is
3641
3642             when N_Access_To_Subprogram_Definition =>
3643                Access_Subprogram_Declaration (T, Def);
3644
3645                --  If this is a remote access to subprogram, we must create the
3646                --  equivalent fat pointer type, and related subprograms.
3647
3648                if Is_Remote then
3649                   Process_Remote_AST_Declaration (N);
3650                end if;
3651
3652                --  Validate categorization rule against access type declaration
3653                --  usually a violation in Pure unit, Shared_Passive unit.
3654
3655                Validate_Access_Type_Declaration (T, N);
3656
3657             when N_Access_To_Object_Definition =>
3658                Access_Type_Declaration (T, Def);
3659
3660                --  Validate categorization rule against access type declaration
3661                --  usually a violation in Pure unit, Shared_Passive unit.
3662
3663                Validate_Access_Type_Declaration (T, N);
3664
3665                --  If we are in a Remote_Call_Interface package and define
3666                --  a RACW, Read and Write attribute must be added.
3667
3668                if Is_Remote
3669                  and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3670                then
3671                   Add_RACW_Features (Def_Id);
3672                end if;
3673
3674                --  Set no strict aliasing flag if config pragma seen
3675
3676                if Opt.No_Strict_Aliasing then
3677                   Set_No_Strict_Aliasing (Base_Type (Def_Id));
3678                end if;
3679
3680             when N_Array_Type_Definition =>
3681                Array_Type_Declaration (T, Def);
3682
3683             when N_Derived_Type_Definition =>
3684                Derived_Type_Declaration (T, N, T /= Def_Id);
3685
3686             when N_Enumeration_Type_Definition =>
3687                Enumeration_Type_Declaration (T, Def);
3688
3689             when N_Floating_Point_Definition =>
3690                Floating_Point_Type_Declaration (T, Def);
3691
3692             when N_Decimal_Fixed_Point_Definition =>
3693                Decimal_Fixed_Point_Type_Declaration (T, Def);
3694
3695             when N_Ordinary_Fixed_Point_Definition =>
3696                Ordinary_Fixed_Point_Type_Declaration (T, Def);
3697
3698             when N_Signed_Integer_Type_Definition =>
3699                Signed_Integer_Type_Declaration (T, Def);
3700
3701             when N_Modular_Type_Definition =>
3702                Modular_Type_Declaration (T, Def);
3703
3704             when N_Record_Definition =>
3705                Record_Type_Declaration (T, N, Prev);
3706
3707             when others =>
3708                raise Program_Error;
3709
3710          end case;
3711       end if;
3712
3713       if Etype (T) = Any_Type then
3714          return;
3715       end if;
3716
3717       --  Some common processing for all types
3718
3719       Set_Depends_On_Private (T, Has_Private_Component (T));
3720       Check_Ops_From_Incomplete_Type;
3721
3722       --  Both the declared entity, and its anonymous base type if one
3723       --  was created, need freeze nodes allocated.
3724
3725       declare
3726          B : constant Entity_Id := Base_Type (T);
3727
3728       begin
3729          --  In the case where the base type is different from the first
3730          --  subtype, we pre-allocate a freeze node, and set the proper link
3731          --  to the first subtype. Freeze_Entity will use this preallocated
3732          --  freeze node when it freezes the entity.
3733
3734          if B /= T then
3735             Ensure_Freeze_Node (B);
3736             Set_First_Subtype_Link (Freeze_Node (B), T);
3737          end if;
3738
3739          if not From_With_Type (T) then
3740             Set_Has_Delayed_Freeze (T);
3741          end if;
3742       end;
3743
3744       --  Case of T is the full declaration of some private type which has
3745       --  been swapped in Defining_Identifier (N).
3746
3747       if T /= Def_Id and then Is_Private_Type (Def_Id) then
3748          Process_Full_View (N, T, Def_Id);
3749
3750          --  Record the reference. The form of this is a little strange,
3751          --  since the full declaration has been swapped in. So the first
3752          --  parameter here represents the entity to which a reference is
3753          --  made which is the "real" entity, i.e. the one swapped in,
3754          --  and the second parameter provides the reference location.
3755
3756          --  Also, we want to kill Has_Pragma_Unreferenced temporarily here
3757          --  since we don't want a complaint about the full type being an
3758          --  unwanted reference to the private type
3759
3760          declare
3761             B : constant Boolean := Has_Pragma_Unreferenced (T);
3762          begin
3763             Set_Has_Pragma_Unreferenced (T, False);
3764             Generate_Reference (T, T, 'c');
3765             Set_Has_Pragma_Unreferenced (T, B);
3766          end;
3767
3768          Set_Completion_Referenced (Def_Id);
3769
3770       --  For completion of incomplete type, process incomplete dependents
3771       --  and always mark the full type as referenced (it is the incomplete
3772       --  type that we get for any real reference).
3773
3774       elsif Ekind (Prev) = E_Incomplete_Type then
3775          Process_Incomplete_Dependents (N, T, Prev);
3776          Generate_Reference (Prev, Def_Id, 'c');
3777          Set_Completion_Referenced (Def_Id);
3778
3779       --  If not private type or incomplete type completion, this is a real
3780       --  definition of a new entity, so record it.
3781
3782       else
3783          Generate_Definition (Def_Id);
3784       end if;
3785
3786       Check_Eliminated (Def_Id);
3787    end Analyze_Type_Declaration;
3788
3789    --------------------------
3790    -- Analyze_Variant_Part --
3791    --------------------------
3792
3793    procedure Analyze_Variant_Part (N : Node_Id) is
3794
3795       procedure Non_Static_Choice_Error (Choice : Node_Id);
3796       --  Error routine invoked by the generic instantiation below when
3797       --  the variant part has a non static choice.
3798
3799       procedure Process_Declarations (Variant : Node_Id);
3800       --  Analyzes all the declarations associated with a Variant.
3801       --  Needed by the generic instantiation below.
3802
3803       package Variant_Choices_Processing is new
3804         Generic_Choices_Processing
3805           (Get_Alternatives          => Variants,
3806            Get_Choices               => Discrete_Choices,
3807            Process_Empty_Choice      => No_OP,
3808            Process_Non_Static_Choice => Non_Static_Choice_Error,
3809            Process_Associated_Node   => Process_Declarations);
3810       use Variant_Choices_Processing;
3811       --  Instantiation of the generic choice processing package
3812
3813       -----------------------------
3814       -- Non_Static_Choice_Error --
3815       -----------------------------
3816
3817       procedure Non_Static_Choice_Error (Choice : Node_Id) is
3818       begin
3819          Flag_Non_Static_Expr
3820            ("choice given in variant part is not static!", Choice);
3821       end Non_Static_Choice_Error;
3822
3823       --------------------------
3824       -- Process_Declarations --
3825       --------------------------
3826
3827       procedure Process_Declarations (Variant : Node_Id) is
3828       begin
3829          if not Null_Present (Component_List (Variant)) then
3830             Analyze_Declarations (Component_Items (Component_List (Variant)));
3831
3832             if Present (Variant_Part (Component_List (Variant))) then
3833                Analyze (Variant_Part (Component_List (Variant)));
3834             end if;
3835          end if;
3836       end Process_Declarations;
3837
3838       --  Variables local to Analyze_Case_Statement
3839
3840       Discr_Name : Node_Id;
3841       Discr_Type : Entity_Id;
3842
3843       Case_Table     : Choice_Table_Type (1 .. Number_Of_Choices (N));
3844       Last_Choice    : Nat;
3845       Dont_Care      : Boolean;
3846       Others_Present : Boolean := False;
3847
3848    --  Start of processing for Analyze_Variant_Part
3849
3850    begin
3851       Discr_Name := Name (N);
3852       Analyze (Discr_Name);
3853
3854       if Etype (Discr_Name) = Any_Type then
3855
3856          --  Prevent cascaded errors
3857
3858          return;
3859
3860       elsif Ekind (Entity (Discr_Name)) /= E_Discriminant then
3861          Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
3862       end if;
3863
3864       Discr_Type := Etype (Entity (Discr_Name));
3865
3866       if not Is_Discrete_Type (Discr_Type) then
3867          Error_Msg_N
3868            ("discriminant in a variant part must be of a discrete type",
3869              Name (N));
3870          return;
3871       end if;
3872
3873       --  Call the instantiated Analyze_Choices which does the rest of the work
3874
3875       Analyze_Choices
3876         (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
3877    end Analyze_Variant_Part;
3878
3879    ----------------------------
3880    -- Array_Type_Declaration --
3881    ----------------------------
3882
3883    procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
3884       Component_Def : constant Node_Id := Component_Definition (Def);
3885       Element_Type  : Entity_Id;
3886       Implicit_Base : Entity_Id;
3887       Index         : Node_Id;
3888       Related_Id    : Entity_Id := Empty;
3889       Nb_Index      : Nat;
3890       P             : constant Node_Id := Parent (Def);
3891       Priv          : Entity_Id;
3892
3893    begin
3894       if Nkind (Def) = N_Constrained_Array_Definition then
3895          Index := First (Discrete_Subtype_Definitions (Def));
3896       else
3897          Index := First (Subtype_Marks (Def));
3898       end if;
3899
3900       --  Find proper names for the implicit types which may be public.
3901       --  in case of anonymous arrays we use the name of the first object
3902       --  of that type as prefix.
3903
3904       if No (T) then
3905          Related_Id :=  Defining_Identifier (P);
3906       else
3907          Related_Id := T;
3908       end if;
3909
3910       Nb_Index := 1;
3911       while Present (Index) loop
3912          Analyze (Index);
3913
3914          --  Add a subtype declaration for each index of private array type
3915          --  declaration whose etype is also private. For example:
3916
3917          --     package Pkg is
3918          --        type Index is private;
3919          --     private
3920          --        type Table is array (Index) of ...
3921          --     end;
3922
3923          --  This is currently required by the expander to generate the
3924          --  internally generated equality subprogram of records with variant
3925          --  parts in which the etype of some component is such private type.
3926
3927          if Ekind (Current_Scope) = E_Package
3928            and then In_Private_Part (Current_Scope)
3929            and then Has_Private_Declaration (Etype (Index))
3930          then
3931             declare
3932                Loc   : constant Source_Ptr := Sloc (Def);
3933                New_E : Entity_Id;
3934                Decl  : Entity_Id;
3935
3936             begin
3937                New_E :=
3938                  Make_Defining_Identifier (Loc,
3939                    Chars => New_Internal_Name ('T'));
3940                Set_Is_Internal (New_E);
3941
3942                Decl :=
3943                  Make_Subtype_Declaration (Loc,
3944                    Defining_Identifier => New_E,
3945                    Subtype_Indication  =>
3946                      New_Occurrence_Of (Etype (Index), Loc));
3947
3948                Insert_Before (Parent (Def), Decl);
3949                Analyze (Decl);
3950                Set_Etype (Index, New_E);
3951
3952                --  If the index is a range the Entity attribute is not
3953                --  available. Example:
3954
3955                --     package Pkg is
3956                --        type T is private;
3957                --     private
3958                --        type T is new Natural;
3959                --        Table : array (T(1) .. T(10)) of Boolean;
3960                --     end Pkg;
3961
3962                if Nkind (Index) /= N_Range then
3963                   Set_Entity (Index, New_E);
3964                end if;
3965             end;
3966          end if;
3967
3968          Make_Index (Index, P, Related_Id, Nb_Index);
3969          Next_Index (Index);
3970          Nb_Index := Nb_Index + 1;
3971       end loop;
3972
3973       --  Process subtype indication if one is present
3974
3975       if Present (Subtype_Indication (Component_Def)) then
3976          Element_Type :=
3977            Process_Subtype
3978              (Subtype_Indication (Component_Def), P, Related_Id, 'C');
3979
3980       --  Ada 2005 (AI-230): Access Definition case
3981
3982       else pragma Assert (Present (Access_Definition (Component_Def)));
3983
3984          --  Indicate that the anonymous access type is created by the
3985          --  array type declaration.
3986
3987          Element_Type := Access_Definition
3988                            (Related_Nod => P,
3989                             N           => Access_Definition (Component_Def));
3990          Set_Is_Local_Anonymous_Access (Element_Type);
3991
3992          --  Propagate the parent. This field is needed if we have to generate
3993          --  the master_id associated with an anonymous access to task type
3994          --  component (see Expand_N_Full_Type_Declaration.Build_Master)
3995
3996          Set_Parent (Element_Type, Parent (T));
3997
3998          --  Ada 2005 (AI-230): In case of components that are anonymous
3999          --  access types the level of accessibility depends on the enclosing
4000          --  type declaration
4001
4002          Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4003
4004          --  Ada 2005 (AI-254)
4005
4006          declare
4007             CD : constant Node_Id :=
4008                    Access_To_Subprogram_Definition
4009                      (Access_Definition (Component_Def));
4010          begin
4011             if Present (CD) and then Protected_Present (CD) then
4012                Element_Type :=
4013                  Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4014             end if;
4015          end;
4016       end if;
4017
4018       --  Constrained array case
4019
4020       if No (T) then
4021          T := Create_Itype (E_Void, P, Related_Id, 'T');
4022       end if;
4023
4024       if Nkind (Def) = N_Constrained_Array_Definition then
4025
4026          --  Establish Implicit_Base as unconstrained base type
4027
4028          Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4029
4030          Init_Size_Align        (Implicit_Base);
4031          Set_Etype              (Implicit_Base, Implicit_Base);
4032          Set_Scope              (Implicit_Base, Current_Scope);
4033          Set_Has_Delayed_Freeze (Implicit_Base);
4034
4035          --  The constrained array type is a subtype of the unconstrained one
4036
4037          Set_Ekind          (T, E_Array_Subtype);
4038          Init_Size_Align    (T);
4039          Set_Etype          (T, Implicit_Base);
4040          Set_Scope          (T, Current_Scope);
4041          Set_Is_Constrained (T, True);
4042          Set_First_Index    (T, First (Discrete_Subtype_Definitions (Def)));
4043          Set_Has_Delayed_Freeze (T);
4044
4045          --  Complete setup of implicit base type
4046
4047          Set_First_Index       (Implicit_Base, First_Index (T));
4048          Set_Component_Type    (Implicit_Base, Element_Type);
4049          Set_Has_Task          (Implicit_Base, Has_Task (Element_Type));
4050          Set_Component_Size    (Implicit_Base, Uint_0);
4051          Set_Packed_Array_Type (Implicit_Base, Empty);
4052          Set_Has_Controlled_Component
4053                                (Implicit_Base, Has_Controlled_Component
4054                                                         (Element_Type)
4055                                                  or else Is_Controlled
4056                                                         (Element_Type));
4057          Set_Finalize_Storage_Only
4058                                (Implicit_Base, Finalize_Storage_Only
4059                                                         (Element_Type));
4060
4061       --  Unconstrained array case
4062
4063       else
4064          Set_Ekind                    (T, E_Array_Type);
4065          Init_Size_Align              (T);
4066          Set_Etype                    (T, T);
4067          Set_Scope                    (T, Current_Scope);
4068          Set_Component_Size           (T, Uint_0);
4069          Set_Is_Constrained           (T, False);
4070          Set_First_Index              (T, First (Subtype_Marks (Def)));
4071          Set_Has_Delayed_Freeze       (T, True);
4072          Set_Has_Task                 (T, Has_Task      (Element_Type));
4073          Set_Has_Controlled_Component (T, Has_Controlled_Component
4074                                                         (Element_Type)
4075                                             or else
4076                                           Is_Controlled (Element_Type));
4077          Set_Finalize_Storage_Only    (T, Finalize_Storage_Only
4078                                                         (Element_Type));
4079       end if;
4080
4081       --  Common attributes for both cases
4082
4083       Set_Component_Type (Base_Type (T), Element_Type);
4084       Set_Packed_Array_Type (T, Empty);
4085
4086       if Aliased_Present (Component_Definition (Def)) then
4087          Set_Has_Aliased_Components (Etype (T));
4088       end if;
4089
4090       --  Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4091       --  array type to ensure that objects of this type are initialized.
4092
4093       if Ada_Version >= Ada_05
4094         and then Can_Never_Be_Null (Element_Type)
4095       then
4096          Set_Can_Never_Be_Null (T);
4097
4098          if Null_Exclusion_Present (Component_Definition (Def))
4099
4100             --  No need to check itypes because in their case this check
4101             --  was done at their point of creation
4102
4103            and then not Is_Itype (Element_Type)
4104          then
4105             Error_Msg_N
4106               ("`NOT NULL` not allowed (null already excluded)",
4107                Subtype_Indication (Component_Definition (Def)));
4108          end if;
4109       end if;
4110
4111       Priv := Private_Component (Element_Type);
4112
4113       if Present (Priv) then
4114
4115          --  Check for circular definitions
4116
4117          if Priv = Any_Type then
4118             Set_Component_Type (Etype (T), Any_Type);
4119
4120          --  There is a gap in the visibility of operations on the composite
4121          --  type only if the component type is defined in a different scope.
4122
4123          elsif Scope (Priv) = Current_Scope then
4124             null;
4125
4126          elsif Is_Limited_Type (Priv) then
4127             Set_Is_Limited_Composite (Etype (T));
4128             Set_Is_Limited_Composite (T);
4129          else
4130             Set_Is_Private_Composite (Etype (T));
4131             Set_Is_Private_Composite (T);
4132          end if;
4133       end if;
4134
4135       --  A syntax error in the declaration itself may lead to an empty
4136       --  index list, in which case do a minimal patch.
4137
4138       if No (First_Index (T)) then
4139          Error_Msg_N ("missing index definition in array type declaration", T);
4140
4141          declare
4142             Indices : constant List_Id :=
4143                         New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4144          begin
4145             Set_Discrete_Subtype_Definitions (Def, Indices);
4146             Set_First_Index (T, First (Indices));
4147             return;
4148          end;
4149       end if;
4150
4151       --  Create a concatenation operator for the new type. Internal array
4152       --  types created for packed entities do not need such, they are
4153       --  compatible with the user-defined type.
4154
4155       if Number_Dimensions (T) = 1
4156          and then not Is_Packed_Array_Type (T)
4157       then
4158          New_Concatenation_Op (T);
4159       end if;
4160
4161       --  In the case of an unconstrained array the parser has already verified
4162       --  that all the indices are unconstrained but we still need to make sure
4163       --  that the element type is constrained.
4164
4165       if Is_Indefinite_Subtype (Element_Type) then
4166          Error_Msg_N
4167            ("unconstrained element type in array declaration",
4168             Subtype_Indication (Component_Def));
4169
4170       elsif Is_Abstract_Type (Element_Type) then
4171          Error_Msg_N
4172            ("the type of a component cannot be abstract",
4173             Subtype_Indication (Component_Def));
4174       end if;
4175
4176    end Array_Type_Declaration;
4177
4178    ------------------------------------------------------
4179    -- Replace_Anonymous_Access_To_Protected_Subprogram --
4180    ------------------------------------------------------
4181
4182    function Replace_Anonymous_Access_To_Protected_Subprogram
4183      (N : Node_Id) return Entity_Id
4184    is
4185       Loc : constant Source_Ptr := Sloc (N);
4186
4187       Curr_Scope : constant Scope_Stack_Entry :=
4188                      Scope_Stack.Table (Scope_Stack.Last);
4189
4190       Anon : constant Entity_Id :=
4191                Make_Defining_Identifier (Loc,
4192                  Chars => New_Internal_Name ('S'));
4193
4194       Acc  : Node_Id;
4195       Comp : Node_Id;
4196       Decl : Node_Id;
4197       P    : Node_Id;
4198
4199    begin
4200       Set_Is_Internal (Anon);
4201
4202       case Nkind (N) is
4203          when N_Component_Declaration       |
4204            N_Unconstrained_Array_Definition |
4205            N_Constrained_Array_Definition   =>
4206             Comp := Component_Definition (N);
4207             Acc  := Access_Definition (Comp);
4208
4209          when N_Discriminant_Specification =>
4210             Comp := Discriminant_Type (N);
4211             Acc  := Comp;
4212
4213          when N_Parameter_Specification =>
4214             Comp := Parameter_Type (N);
4215             Acc  := Comp;
4216
4217          when N_Access_Function_Definition  =>
4218             Comp := Result_Definition (N);
4219             Acc  := Comp;
4220
4221          when N_Object_Declaration  =>
4222             Comp := Object_Definition (N);
4223             Acc  := Comp;
4224
4225          when others =>
4226             raise Program_Error;
4227       end case;
4228
4229       Decl := Make_Full_Type_Declaration (Loc,
4230                 Defining_Identifier => Anon,
4231                 Type_Definition   =>
4232                   Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
4233
4234       Mark_Rewrite_Insertion (Decl);
4235
4236       --  Insert the new declaration in the nearest enclosing scope
4237
4238       P := Parent (N);
4239       while Present (P) and then not Has_Declarations (P) loop
4240          P := Parent (P);
4241       end loop;
4242
4243       pragma Assert (Present (P));
4244
4245       if Nkind (P) = N_Package_Specification then
4246          Prepend (Decl, Visible_Declarations (P));
4247       else
4248          Prepend (Decl, Declarations (P));
4249       end if;
4250
4251       --  Replace the anonymous type with an occurrence of the new declaration.
4252       --  In all cases the rewritten node does not have the null-exclusion
4253       --  attribute because (if present) it was already inherited by the
4254       --  anonymous entity (Anon). Thus, in case of components we do not
4255       --  inherit this attribute.
4256
4257       if Nkind (N) = N_Parameter_Specification then
4258          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4259          Set_Etype (Defining_Identifier (N), Anon);
4260          Set_Null_Exclusion_Present (N, False);
4261
4262       elsif Nkind (N) = N_Object_Declaration then
4263          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4264          Set_Etype (Defining_Identifier (N), Anon);
4265
4266       elsif Nkind (N) = N_Access_Function_Definition then
4267          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4268
4269       else
4270          Rewrite (Comp,
4271            Make_Component_Definition (Loc,
4272              Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
4273       end if;
4274
4275       Mark_Rewrite_Insertion (Comp);
4276
4277       --  Temporarily remove the current scope from the stack to add the new
4278       --  declarations to the enclosing scope
4279
4280       if Nkind (N) = N_Object_Declaration
4281         or else Nkind (N) = N_Access_Function_Definition
4282       then
4283          Analyze (Decl);
4284
4285       else
4286          Scope_Stack.Decrement_Last;
4287          Analyze (Decl);
4288          Set_Is_Itype (Anon);
4289          Scope_Stack.Append (Curr_Scope);
4290       end if;
4291
4292       Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
4293       return Anon;
4294    end Replace_Anonymous_Access_To_Protected_Subprogram;
4295
4296    -------------------------------
4297    -- Build_Derived_Access_Type --
4298    -------------------------------
4299
4300    procedure Build_Derived_Access_Type
4301      (N            : Node_Id;
4302       Parent_Type  : Entity_Id;
4303       Derived_Type : Entity_Id)
4304    is
4305       S : constant Node_Id := Subtype_Indication (Type_Definition (N));
4306
4307       Desig_Type      : Entity_Id;
4308       Discr           : Entity_Id;
4309       Discr_Con_Elist : Elist_Id;
4310       Discr_Con_El    : Elmt_Id;
4311       Subt            : Entity_Id;
4312
4313    begin
4314       --  Set the designated type so it is available in case this is an access
4315       --  to a self-referential type, e.g. a standard list type with a next
4316       --  pointer. Will be reset after subtype is built.
4317
4318       Set_Directly_Designated_Type
4319         (Derived_Type, Designated_Type (Parent_Type));
4320
4321       Subt := Process_Subtype (S, N);
4322
4323       if Nkind (S) /= N_Subtype_Indication
4324         and then Subt /= Base_Type (Subt)
4325       then
4326          Set_Ekind (Derived_Type, E_Access_Subtype);
4327       end if;
4328
4329       if Ekind (Derived_Type) = E_Access_Subtype then
4330          declare
4331             Pbase      : constant Entity_Id := Base_Type (Parent_Type);
4332             Ibase      : constant Entity_Id :=
4333                            Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
4334             Svg_Chars  : constant Name_Id   := Chars (Ibase);
4335             Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
4336
4337          begin
4338             Copy_Node (Pbase, Ibase);
4339
4340             Set_Chars             (Ibase, Svg_Chars);
4341             Set_Next_Entity       (Ibase, Svg_Next_E);
4342             Set_Sloc              (Ibase, Sloc (Derived_Type));
4343             Set_Scope             (Ibase, Scope (Derived_Type));
4344             Set_Freeze_Node       (Ibase, Empty);
4345             Set_Is_Frozen         (Ibase, False);
4346             Set_Comes_From_Source (Ibase, False);
4347             Set_Is_First_Subtype  (Ibase, False);
4348
4349             Set_Etype (Ibase, Pbase);
4350             Set_Etype (Derived_Type, Ibase);
4351          end;
4352       end if;
4353
4354       Set_Directly_Designated_Type
4355         (Derived_Type, Designated_Type (Subt));
4356
4357       Set_Is_Constrained     (Derived_Type, Is_Constrained (Subt));
4358       Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
4359       Set_Size_Info          (Derived_Type,                     Parent_Type);
4360       Set_RM_Size            (Derived_Type, RM_Size            (Parent_Type));
4361       Set_Depends_On_Private (Derived_Type,
4362                               Has_Private_Component (Derived_Type));
4363       Conditional_Delay      (Derived_Type, Subt);
4364
4365       --  Ada 2005 (AI-231). Set the null-exclusion attribute
4366
4367       if Null_Exclusion_Present (Type_Definition (N))
4368         or else Can_Never_Be_Null (Parent_Type)
4369       then
4370          Set_Can_Never_Be_Null (Derived_Type);
4371       end if;
4372
4373       --  Note: we do not copy the Storage_Size_Variable, since we always go to
4374       --  the root type for this information.
4375
4376       --  Apply range checks to discriminants for derived record case
4377       --  ??? THIS CODE SHOULD NOT BE HERE REALLY.
4378
4379       Desig_Type := Designated_Type (Derived_Type);
4380       if Is_Composite_Type (Desig_Type)
4381         and then (not Is_Array_Type (Desig_Type))
4382         and then Has_Discriminants (Desig_Type)
4383         and then Base_Type (Desig_Type) /= Desig_Type
4384       then
4385          Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
4386          Discr_Con_El := First_Elmt (Discr_Con_Elist);
4387
4388          Discr := First_Discriminant (Base_Type (Desig_Type));
4389          while Present (Discr_Con_El) loop
4390             Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
4391             Next_Elmt (Discr_Con_El);
4392             Next_Discriminant (Discr);
4393          end loop;
4394       end if;
4395    end Build_Derived_Access_Type;
4396
4397    ------------------------------
4398    -- Build_Derived_Array_Type --
4399    ------------------------------
4400
4401    procedure Build_Derived_Array_Type
4402      (N            : Node_Id;
4403       Parent_Type  : Entity_Id;
4404       Derived_Type : Entity_Id)
4405    is
4406       Loc           : constant Source_Ptr := Sloc (N);
4407       Tdef          : constant Node_Id    := Type_Definition (N);
4408       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
4409       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
4410       Implicit_Base : Entity_Id;
4411       New_Indic     : Node_Id;
4412
4413       procedure Make_Implicit_Base;
4414       --  If the parent subtype is constrained, the derived type is a subtype
4415       --  of an implicit base type derived from the parent base.
4416
4417       ------------------------
4418       -- Make_Implicit_Base --
4419       ------------------------
4420
4421       procedure Make_Implicit_Base is
4422       begin
4423          Implicit_Base :=
4424            Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4425
4426          Set_Ekind (Implicit_Base, Ekind (Parent_Base));
4427          Set_Etype (Implicit_Base, Parent_Base);
4428
4429          Copy_Array_Subtype_Attributes   (Implicit_Base, Parent_Base);
4430          Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
4431
4432          Set_Has_Delayed_Freeze (Implicit_Base, True);
4433       end Make_Implicit_Base;
4434
4435    --  Start of processing for Build_Derived_Array_Type
4436
4437    begin
4438       if not Is_Constrained (Parent_Type) then
4439          if Nkind (Indic) /= N_Subtype_Indication then
4440             Set_Ekind (Derived_Type, E_Array_Type);
4441
4442             Copy_Array_Subtype_Attributes   (Derived_Type, Parent_Type);
4443             Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
4444
4445             Set_Has_Delayed_Freeze (Derived_Type, True);
4446
4447          else
4448             Make_Implicit_Base;
4449             Set_Etype (Derived_Type, Implicit_Base);
4450
4451             New_Indic :=
4452               Make_Subtype_Declaration (Loc,
4453                 Defining_Identifier => Derived_Type,
4454                 Subtype_Indication  =>
4455                   Make_Subtype_Indication (Loc,
4456                     Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
4457                     Constraint => Constraint (Indic)));
4458
4459             Rewrite (N, New_Indic);
4460             Analyze (N);
4461          end if;
4462
4463       else
4464          if Nkind (Indic) /= N_Subtype_Indication then
4465             Make_Implicit_Base;
4466
4467             Set_Ekind             (Derived_Type, Ekind (Parent_Type));
4468             Set_Etype             (Derived_Type, Implicit_Base);
4469             Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4470
4471          else
4472             Error_Msg_N ("illegal constraint on constrained type", Indic);
4473          end if;
4474       end if;
4475
4476       --  If parent type is not a derived type itself, and is declared in
4477       --  closed scope (e.g. a subprogram), then we must explicitly introduce
4478       --  the new type's concatenation operator since Derive_Subprograms
4479       --  will not inherit the parent's operator. If the parent type is
4480       --  unconstrained, the operator is of the unconstrained base type.
4481
4482       if Number_Dimensions (Parent_Type) = 1
4483         and then not Is_Limited_Type (Parent_Type)
4484         and then not Is_Derived_Type (Parent_Type)
4485         and then not Is_Package_Or_Generic_Package
4486                        (Scope (Base_Type (Parent_Type)))
4487       then
4488          if not Is_Constrained (Parent_Type)
4489            and then Is_Constrained (Derived_Type)
4490          then
4491             New_Concatenation_Op (Implicit_Base);
4492          else
4493             New_Concatenation_Op (Derived_Type);
4494          end if;
4495       end if;
4496    end Build_Derived_Array_Type;
4497
4498    -----------------------------------
4499    -- Build_Derived_Concurrent_Type --
4500    -----------------------------------
4501
4502    procedure Build_Derived_Concurrent_Type
4503      (N            : Node_Id;
4504       Parent_Type  : Entity_Id;
4505       Derived_Type : Entity_Id)
4506    is
4507       D_Constraint : Node_Id;
4508       Disc_Spec    : Node_Id;
4509       Old_Disc     : Entity_Id;
4510       New_Disc     : Entity_Id;
4511
4512       Constraint_Present : constant Boolean :=
4513                              Nkind (Subtype_Indication (Type_Definition (N)))
4514                                                      = N_Subtype_Indication;
4515
4516    begin
4517       Set_Stored_Constraint (Derived_Type, No_Elist);
4518
4519       if Is_Task_Type (Parent_Type) then
4520          Set_Storage_Size_Variable (Derived_Type,
4521            Storage_Size_Variable (Parent_Type));
4522       end if;
4523
4524       if Present (Discriminant_Specifications (N)) then
4525          Push_Scope (Derived_Type);
4526          Check_Or_Process_Discriminants (N, Derived_Type);
4527          End_Scope;
4528
4529       elsif Constraint_Present then
4530
4531          --  Build constrained subtype and derive from it
4532
4533          declare
4534             Loc  : constant Source_Ptr := Sloc (N);
4535             Anon : constant Entity_Id :=
4536                      Make_Defining_Identifier (Loc,
4537                        New_External_Name (Chars (Derived_Type), 'T'));
4538             Decl : Node_Id;
4539
4540          begin
4541             Decl :=
4542               Make_Subtype_Declaration (Loc,
4543                 Defining_Identifier => Anon,
4544                 Subtype_Indication =>
4545                   Subtype_Indication (Type_Definition (N)));
4546             Insert_Before (N, Decl);
4547             Analyze (Decl);
4548
4549             Rewrite (Subtype_Indication (Type_Definition (N)),
4550               New_Occurrence_Of (Anon, Loc));
4551             Set_Analyzed (Derived_Type, False);
4552             Analyze (N);
4553             return;
4554          end;
4555       end if;
4556
4557       --  All attributes are inherited from parent. In particular,
4558       --  entries and the corresponding record type are the same.
4559       --  Discriminants may be renamed, and must be treated separately.
4560
4561       Set_Has_Discriminants
4562         (Derived_Type, Has_Discriminants         (Parent_Type));
4563       Set_Corresponding_Record_Type
4564         (Derived_Type, Corresponding_Record_Type (Parent_Type));
4565
4566       if Constraint_Present then
4567          if not Has_Discriminants (Parent_Type) then
4568             Error_Msg_N ("untagged parent must have discriminants", N);
4569
4570          elsif Present (Discriminant_Specifications (N)) then
4571
4572             --  Verify that new discriminants are used to constrain old ones
4573
4574             D_Constraint :=
4575               First
4576                 (Constraints
4577                   (Constraint (Subtype_Indication (Type_Definition (N)))));
4578
4579             Old_Disc  := First_Discriminant (Parent_Type);
4580             New_Disc  := First_Discriminant (Derived_Type);
4581             Disc_Spec := First (Discriminant_Specifications (N));
4582             while Present (Old_Disc) and then Present (Disc_Spec) loop
4583                if Nkind (Discriminant_Type (Disc_Spec)) /=
4584                                               N_Access_Definition
4585                then
4586                   Analyze (Discriminant_Type (Disc_Spec));
4587
4588                   if not Subtypes_Statically_Compatible (
4589                              Etype (Discriminant_Type (Disc_Spec)),
4590                                Etype (Old_Disc))
4591                   then
4592                      Error_Msg_N
4593                        ("not statically compatible with parent discriminant",
4594                         Discriminant_Type (Disc_Spec));
4595                   end if;
4596                end if;
4597
4598                if Nkind (D_Constraint) = N_Identifier
4599                  and then Chars (D_Constraint) /=
4600                    Chars (Defining_Identifier (Disc_Spec))
4601                then
4602                   Error_Msg_N ("new discriminants must constrain old ones",
4603                     D_Constraint);
4604                else
4605                   Set_Corresponding_Discriminant (New_Disc, Old_Disc);
4606                end if;
4607
4608                Next_Discriminant (Old_Disc);
4609                Next_Discriminant (New_Disc);
4610                Next (Disc_Spec);
4611             end loop;
4612
4613             if Present (Old_Disc) or else Present (Disc_Spec) then
4614                Error_Msg_N ("discriminant mismatch in derivation", N);
4615             end if;
4616
4617          end if;
4618
4619       elsif Present (Discriminant_Specifications (N)) then
4620          Error_Msg_N
4621            ("missing discriminant constraint in untagged derivation",
4622             N);
4623       end if;
4624
4625       if Present (Discriminant_Specifications (N)) then
4626          Old_Disc := First_Discriminant (Parent_Type);
4627          while Present (Old_Disc) loop
4628
4629             if No (Next_Entity (Old_Disc))
4630               or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
4631             then
4632                Set_Next_Entity (Last_Entity (Derived_Type),
4633                                          Next_Entity (Old_Disc));
4634                exit;
4635             end if;
4636
4637             Next_Discriminant (Old_Disc);
4638          end loop;
4639
4640       else
4641          Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
4642          if Has_Discriminants (Parent_Type) then
4643             Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
4644             Set_Discriminant_Constraint (
4645               Derived_Type, Discriminant_Constraint (Parent_Type));
4646          end if;
4647       end if;
4648
4649       Set_Last_Entity  (Derived_Type, Last_Entity  (Parent_Type));
4650
4651       Set_Has_Completion (Derived_Type);
4652    end Build_Derived_Concurrent_Type;
4653
4654    ------------------------------------
4655    -- Build_Derived_Enumeration_Type --
4656    ------------------------------------
4657
4658    procedure Build_Derived_Enumeration_Type
4659      (N            : Node_Id;
4660       Parent_Type  : Entity_Id;
4661       Derived_Type : Entity_Id)
4662    is
4663       Loc           : constant Source_Ptr := Sloc (N);
4664       Def           : constant Node_Id    := Type_Definition (N);
4665       Indic         : constant Node_Id    := Subtype_Indication (Def);
4666       Implicit_Base : Entity_Id;
4667       Literal       : Entity_Id;
4668       New_Lit       : Entity_Id;
4669       Literals_List : List_Id;
4670       Type_Decl     : Node_Id;
4671       Hi, Lo        : Node_Id;
4672       Rang_Expr     : Node_Id;
4673
4674    begin
4675       --  Since types Standard.Character and Standard.Wide_Character do
4676       --  not have explicit literals lists we need to process types derived
4677       --  from them specially. This is handled by Derived_Standard_Character.
4678       --  If the parent type is a generic type, there are no literals either,
4679       --  and we construct the same skeletal representation as for the generic
4680       --  parent type.
4681
4682       if Root_Type (Parent_Type) = Standard_Character
4683         or else Root_Type (Parent_Type) = Standard_Wide_Character
4684         or else Root_Type (Parent_Type) = Standard_Wide_Wide_Character
4685       then
4686          Derived_Standard_Character (N, Parent_Type, Derived_Type);
4687
4688       elsif Is_Generic_Type (Root_Type (Parent_Type)) then
4689          declare
4690             Lo : Node_Id;
4691             Hi : Node_Id;
4692
4693          begin
4694             Lo :=
4695                Make_Attribute_Reference (Loc,
4696                  Attribute_Name => Name_First,
4697                  Prefix => New_Reference_To (Derived_Type, Loc));
4698             Set_Etype (Lo, Derived_Type);
4699
4700             Hi :=
4701                Make_Attribute_Reference (Loc,
4702                  Attribute_Name => Name_Last,
4703                  Prefix => New_Reference_To (Derived_Type, Loc));
4704             Set_Etype (Hi, Derived_Type);
4705
4706             Set_Scalar_Range (Derived_Type,
4707                Make_Range (Loc,
4708                  Low_Bound => Lo,
4709                  High_Bound => Hi));
4710          end;
4711
4712       else
4713          --  If a constraint is present, analyze the bounds to catch
4714          --  premature usage of the derived literals.
4715
4716          if Nkind (Indic) = N_Subtype_Indication
4717            and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
4718          then
4719             Analyze (Low_Bound  (Range_Expression (Constraint (Indic))));
4720             Analyze (High_Bound (Range_Expression (Constraint (Indic))));
4721          end if;
4722
4723          --  Introduce an implicit base type for the derived type even if there
4724          --  is no constraint attached to it, since this seems closer to the
4725          --  Ada semantics. Build a full type declaration tree for the derived
4726          --  type using the implicit base type as the defining identifier. The
4727          --  build a subtype declaration tree which applies the constraint (if
4728          --  any) have it replace the derived type declaration.
4729
4730          Literal := First_Literal (Parent_Type);
4731          Literals_List := New_List;
4732          while Present (Literal)
4733            and then Ekind (Literal) = E_Enumeration_Literal
4734          loop
4735             --  Literals of the derived type have the same representation as
4736             --  those of the parent type, but this representation can be
4737             --  overridden by an explicit representation clause. Indicate
4738             --  that there is no explicit representation given yet. These
4739             --  derived literals are implicit operations of the new type,
4740             --  and can be overridden by explicit ones.
4741
4742             if Nkind (Literal) = N_Defining_Character_Literal then
4743                New_Lit :=
4744                  Make_Defining_Character_Literal (Loc, Chars (Literal));
4745             else
4746                New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
4747             end if;
4748
4749             Set_Ekind                (New_Lit, E_Enumeration_Literal);
4750             Set_Enumeration_Pos      (New_Lit, Enumeration_Pos (Literal));
4751             Set_Enumeration_Rep      (New_Lit, Enumeration_Rep (Literal));
4752             Set_Enumeration_Rep_Expr (New_Lit, Empty);
4753             Set_Alias                (New_Lit, Literal);
4754             Set_Is_Known_Valid       (New_Lit, True);
4755
4756             Append (New_Lit, Literals_List);
4757             Next_Literal (Literal);
4758          end loop;
4759
4760          Implicit_Base :=
4761            Make_Defining_Identifier (Sloc (Derived_Type),
4762              New_External_Name (Chars (Derived_Type), 'B'));
4763
4764          --  Indicate the proper nature of the derived type. This must be done
4765          --  before analysis of the literals, to recognize cases when a literal
4766          --  may be hidden by a previous explicit function definition (cf.
4767          --  c83031a).
4768
4769          Set_Ekind (Derived_Type, E_Enumeration_Subtype);
4770          Set_Etype (Derived_Type, Implicit_Base);
4771
4772          Type_Decl :=
4773            Make_Full_Type_Declaration (Loc,
4774              Defining_Identifier => Implicit_Base,
4775              Discriminant_Specifications => No_List,
4776              Type_Definition =>
4777                Make_Enumeration_Type_Definition (Loc, Literals_List));
4778
4779          Mark_Rewrite_Insertion (Type_Decl);
4780          Insert_Before (N, Type_Decl);
4781          Analyze (Type_Decl);
4782
4783          --  After the implicit base is analyzed its Etype needs to be changed
4784          --  to reflect the fact that it is derived from the parent type which
4785          --  was ignored during analysis. We also set the size at this point.
4786
4787          Set_Etype (Implicit_Base, Parent_Type);
4788
4789          Set_Size_Info      (Implicit_Base,                 Parent_Type);
4790          Set_RM_Size        (Implicit_Base, RM_Size        (Parent_Type));
4791          Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
4792
4793          Set_Has_Non_Standard_Rep
4794                             (Implicit_Base, Has_Non_Standard_Rep
4795                                                            (Parent_Type));
4796          Set_Has_Delayed_Freeze (Implicit_Base);
4797
4798          --  Process the subtype indication including a validation check on the
4799          --  constraint, if any. If a constraint is given, its bounds must be
4800          --  implicitly converted to the new type.
4801
4802          if Nkind (Indic) = N_Subtype_Indication then
4803             declare
4804                R : constant Node_Id :=
4805                      Range_Expression (Constraint (Indic));
4806
4807             begin
4808                if Nkind (R) = N_Range then
4809                   Hi := Build_Scalar_Bound
4810                           (High_Bound (R), Parent_Type, Implicit_Base);
4811                   Lo := Build_Scalar_Bound
4812                           (Low_Bound  (R), Parent_Type, Implicit_Base);
4813
4814                else
4815                   --  Constraint is a Range attribute. Replace with explicit
4816                   --  mention of the bounds of the prefix, which must be a
4817                   --  subtype.
4818
4819                   Analyze (Prefix (R));
4820                   Hi :=
4821                     Convert_To (Implicit_Base,
4822                       Make_Attribute_Reference (Loc,
4823                         Attribute_Name => Name_Last,
4824                         Prefix =>
4825                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
4826
4827                   Lo :=
4828                     Convert_To (Implicit_Base,
4829                       Make_Attribute_Reference (Loc,
4830                         Attribute_Name => Name_First,
4831                         Prefix =>
4832                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
4833                end if;
4834             end;
4835
4836          else
4837             Hi :=
4838               Build_Scalar_Bound
4839                 (Type_High_Bound (Parent_Type),
4840                  Parent_Type, Implicit_Base);
4841             Lo :=
4842                Build_Scalar_Bound
4843                  (Type_Low_Bound (Parent_Type),
4844                   Parent_Type, Implicit_Base);
4845          end if;
4846
4847          Rang_Expr :=
4848            Make_Range (Loc,
4849              Low_Bound  => Lo,
4850              High_Bound => Hi);
4851
4852          --  If we constructed a default range for the case where no range
4853          --  was given, then the expressions in the range must not freeze
4854          --  since they do not correspond to expressions in the source.
4855
4856          if Nkind (Indic) /= N_Subtype_Indication then
4857             Set_Must_Not_Freeze (Lo);
4858             Set_Must_Not_Freeze (Hi);
4859             Set_Must_Not_Freeze (Rang_Expr);
4860          end if;
4861
4862          Rewrite (N,
4863            Make_Subtype_Declaration (Loc,
4864              Defining_Identifier => Derived_Type,
4865              Subtype_Indication =>
4866                Make_Subtype_Indication (Loc,
4867                  Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
4868                  Constraint =>
4869                    Make_Range_Constraint (Loc,
4870                      Range_Expression => Rang_Expr))));
4871
4872          Analyze (N);
4873
4874          --  If pragma Discard_Names applies on the first subtype of the parent
4875          --  type, then it must be applied on this subtype as well.
4876
4877          if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
4878             Set_Discard_Names (Derived_Type);
4879          end if;
4880
4881          --  Apply a range check. Since this range expression doesn't have an
4882          --  Etype, we have to specifically pass the Source_Typ parameter. Is
4883          --  this right???
4884
4885          if Nkind (Indic) = N_Subtype_Indication then
4886             Apply_Range_Check (Range_Expression (Constraint (Indic)),
4887                                Parent_Type,
4888                                Source_Typ => Entity (Subtype_Mark (Indic)));
4889          end if;
4890       end if;
4891    end Build_Derived_Enumeration_Type;
4892
4893    --------------------------------
4894    -- Build_Derived_Numeric_Type --
4895    --------------------------------
4896
4897    procedure Build_Derived_Numeric_Type
4898      (N            : Node_Id;
4899       Parent_Type  : Entity_Id;
4900       Derived_Type : Entity_Id)
4901    is
4902       Loc           : constant Source_Ptr := Sloc (N);
4903       Tdef          : constant Node_Id    := Type_Definition (N);
4904       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
4905       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
4906       No_Constraint : constant Boolean    := Nkind (Indic) /=
4907                                                   N_Subtype_Indication;
4908       Implicit_Base : Entity_Id;
4909
4910       Lo : Node_Id;
4911       Hi : Node_Id;
4912
4913    begin
4914       --  Process the subtype indication including a validation check on
4915       --  the constraint if any.
4916
4917       Discard_Node (Process_Subtype (Indic, N));
4918
4919       --  Introduce an implicit base type for the derived type even if there
4920       --  is no constraint attached to it, since this seems closer to the Ada
4921       --  semantics.
4922
4923       Implicit_Base :=
4924         Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4925
4926       Set_Etype          (Implicit_Base, Parent_Base);
4927       Set_Ekind          (Implicit_Base, Ekind          (Parent_Base));
4928       Set_Size_Info      (Implicit_Base,                 Parent_Base);
4929       Set_RM_Size        (Implicit_Base, RM_Size        (Parent_Base));
4930       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
4931       Set_Parent         (Implicit_Base, Parent (Derived_Type));
4932
4933       if Is_Discrete_Or_Fixed_Point_Type (Parent_Base) then
4934          Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
4935       end if;
4936
4937       Set_Has_Delayed_Freeze (Implicit_Base);
4938
4939       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
4940       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
4941
4942       Set_Scalar_Range (Implicit_Base,
4943         Make_Range (Loc,
4944           Low_Bound  => Lo,
4945           High_Bound => Hi));
4946
4947       if Has_Infinities (Parent_Base) then
4948          Set_Includes_Infinities (Scalar_Range (Implicit_Base));
4949       end if;
4950
4951       --  The Derived_Type, which is the entity of the declaration, is a
4952       --  subtype of the implicit base. Its Ekind is a subtype, even in the
4953       --  absence of an explicit constraint.
4954
4955       Set_Etype (Derived_Type, Implicit_Base);
4956
4957       --  If we did not have a constraint, then the Ekind is set from the
4958       --  parent type (otherwise Process_Subtype has set the bounds)
4959
4960       if No_Constraint then
4961          Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
4962       end if;
4963
4964       --  If we did not have a range constraint, then set the range from the
4965       --  parent type. Otherwise, the call to Process_Subtype has set the
4966       --  bounds.
4967
4968       if No_Constraint
4969         or else not Has_Range_Constraint (Indic)
4970       then
4971          Set_Scalar_Range (Derived_Type,
4972            Make_Range (Loc,
4973              Low_Bound  => New_Copy_Tree (Type_Low_Bound  (Parent_Type)),
4974              High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
4975          Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
4976
4977          if Has_Infinities (Parent_Type) then
4978             Set_Includes_Infinities (Scalar_Range (Derived_Type));
4979          end if;
4980       end if;
4981
4982       --  Set remaining type-specific fields, depending on numeric type
4983
4984       if Is_Modular_Integer_Type (Parent_Type) then
4985          Set_Modulus (Implicit_Base, Modulus (Parent_Base));
4986
4987          Set_Non_Binary_Modulus
4988            (Implicit_Base, Non_Binary_Modulus (Parent_Base));
4989
4990       elsif Is_Floating_Point_Type (Parent_Type) then
4991
4992          --  Digits of base type is always copied from the digits value of
4993          --  the parent base type, but the digits of the derived type will
4994          --  already have been set if there was a constraint present.
4995
4996          Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
4997          Set_Vax_Float    (Implicit_Base, Vax_Float    (Parent_Base));
4998
4999          if No_Constraint then
5000             Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5001          end if;
5002
5003       elsif Is_Fixed_Point_Type (Parent_Type) then
5004
5005          --  Small of base type and derived type are always copied from the
5006          --  parent base type, since smalls never change. The delta of the
5007          --  base type is also copied from the parent base type. However the
5008          --  delta of the derived type will have been set already if a
5009          --  constraint was present.
5010
5011          Set_Small_Value (Derived_Type,  Small_Value (Parent_Base));
5012          Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5013          Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
5014
5015          if No_Constraint then
5016             Set_Delta_Value (Derived_Type,  Delta_Value (Parent_Type));
5017          end if;
5018
5019          --  The scale and machine radix in the decimal case are always
5020          --  copied from the parent base type.
5021
5022          if Is_Decimal_Fixed_Point_Type (Parent_Type) then
5023             Set_Scale_Value (Derived_Type,  Scale_Value (Parent_Base));
5024             Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
5025
5026             Set_Machine_Radix_10
5027               (Derived_Type,  Machine_Radix_10 (Parent_Base));
5028             Set_Machine_Radix_10
5029               (Implicit_Base, Machine_Radix_10 (Parent_Base));
5030
5031             Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5032
5033             if No_Constraint then
5034                Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
5035
5036             else
5037                --  the analysis of the subtype_indication sets the
5038                --  digits value of the derived type.
5039
5040                null;
5041             end if;
5042          end if;
5043       end if;
5044
5045       --  The type of the bounds is that of the parent type, and they
5046       --  must be converted to the derived type.
5047
5048       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
5049
5050       --  The implicit_base should be frozen when the derived type is frozen,
5051       --  but note that it is used in the conversions of the bounds. For fixed
5052       --  types we delay the determination of the bounds until the proper
5053       --  freezing point. For other numeric types this is rejected by GCC, for
5054       --  reasons that are currently unclear (???), so we choose to freeze the
5055       --  implicit base now. In the case of integers and floating point types
5056       --  this is harmless because subsequent representation clauses cannot
5057       --  affect anything, but it is still baffling that we cannot use the
5058       --  same mechanism for all derived numeric types.
5059
5060       --  There is a further complication: actually *some* representation
5061       --  clauses can affect the implicit base type. Namely, attribute
5062       --  definition clauses for stream-oriented attributes need to set the
5063       --  corresponding TSS entries on the base type, and this normally cannot
5064       --  be done after the base type is frozen, so the circuitry in
5065       --  Sem_Ch13.New_Stream_Subprogram must account for this possibility and
5066       --  not use Set_TSS in this case.
5067
5068       if Is_Fixed_Point_Type (Parent_Type) then
5069          Conditional_Delay (Implicit_Base, Parent_Type);
5070       else
5071          Freeze_Before (N, Implicit_Base);
5072       end if;
5073    end Build_Derived_Numeric_Type;
5074
5075    --------------------------------
5076    -- Build_Derived_Private_Type --
5077    --------------------------------
5078
5079    procedure Build_Derived_Private_Type
5080      (N             : Node_Id;
5081       Parent_Type   : Entity_Id;
5082       Derived_Type  : Entity_Id;
5083       Is_Completion : Boolean;
5084       Derive_Subps  : Boolean := True)
5085    is
5086       Der_Base    : Entity_Id;
5087       Discr       : Entity_Id;
5088       Full_Decl   : Node_Id := Empty;
5089       Full_Der    : Entity_Id;
5090       Full_P      : Entity_Id;
5091       Last_Discr  : Entity_Id;
5092       Par_Scope   : constant Entity_Id := Scope (Base_Type (Parent_Type));
5093       Swapped     : Boolean := False;
5094
5095       procedure Copy_And_Build;
5096       --  Copy derived type declaration, replace parent with its full view,
5097       --  and analyze new declaration.
5098
5099       --------------------
5100       -- Copy_And_Build --
5101       --------------------
5102
5103       procedure Copy_And_Build is
5104          Full_N : Node_Id;
5105
5106       begin
5107          if Ekind (Parent_Type) in Record_Kind
5108            or else
5109              (Ekind (Parent_Type) in Enumeration_Kind
5110                and then Root_Type (Parent_Type) /= Standard_Character
5111                and then Root_Type (Parent_Type) /= Standard_Wide_Character
5112                and then Root_Type (Parent_Type) /= Standard_Wide_Wide_Character
5113                and then not Is_Generic_Type (Root_Type (Parent_Type)))
5114          then
5115             Full_N := New_Copy_Tree (N);
5116             Insert_After (N, Full_N);
5117             Build_Derived_Type (
5118               Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
5119
5120          else
5121             Build_Derived_Type (
5122               N, Parent_Type, Full_Der, True, Derive_Subps => False);
5123          end if;
5124       end Copy_And_Build;
5125
5126    --  Start of processing for Build_Derived_Private_Type
5127
5128    begin
5129       if Is_Tagged_Type (Parent_Type) then
5130          Build_Derived_Record_Type
5131            (N, Parent_Type, Derived_Type, Derive_Subps);
5132          return;
5133
5134       elsif Has_Discriminants (Parent_Type) then
5135          if Present (Full_View (Parent_Type)) then
5136             if not Is_Completion then
5137
5138                --  Copy declaration for subsequent analysis, to provide a
5139                --  completion for what is a private declaration. Indicate that
5140                --  the full type is internally generated.
5141
5142                Full_Decl := New_Copy_Tree (N);
5143                Full_Der  := New_Copy (Derived_Type);
5144                Set_Comes_From_Source (Full_Decl, False);
5145                Set_Comes_From_Source (Full_Der, False);
5146
5147                Insert_After (N, Full_Decl);
5148
5149             else
5150                --  If this is a completion, the full view being built is
5151                --  itself private. We build a subtype of the parent with
5152                --  the same constraints as this full view, to convey to the
5153                --  back end the constrained components and the size of this
5154                --  subtype. If the parent is constrained, its full view can
5155                --  serve as the underlying full view of the derived type.
5156
5157                if No (Discriminant_Specifications (N)) then
5158                   if Nkind (Subtype_Indication (Type_Definition (N))) =
5159                                                         N_Subtype_Indication
5160                   then
5161                      Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
5162
5163                   elsif Is_Constrained (Full_View (Parent_Type)) then
5164                      Set_Underlying_Full_View (Derived_Type,
5165                        Full_View (Parent_Type));
5166                   end if;
5167
5168                else
5169                   --  If there are new discriminants, the parent subtype is
5170                   --  constrained by them, but it is not clear how to build
5171                   --  the underlying_full_view in this case ???
5172
5173                   null;
5174                end if;
5175             end if;
5176          end if;
5177
5178          --  Build partial view of derived type from partial view of parent
5179
5180          Build_Derived_Record_Type
5181            (N, Parent_Type, Derived_Type, Derive_Subps);
5182
5183          if Present (Full_View (Parent_Type))
5184            and then not Is_Completion
5185          then
5186             if not In_Open_Scopes (Par_Scope)
5187               or else not In_Same_Source_Unit (N, Parent_Type)
5188             then
5189                --  Swap partial and full views temporarily
5190
5191                Install_Private_Declarations (Par_Scope);
5192                Install_Visible_Declarations (Par_Scope);
5193                Swapped := True;
5194             end if;
5195
5196             --  Build full view of derived type from full view of parent which
5197             --  is now installed. Subprograms have been derived on the partial
5198             --  view, the completion does not derive them anew.
5199
5200             if not Is_Tagged_Type (Parent_Type) then
5201
5202                --  If the parent is itself derived from another private type,
5203                --  installing the private declarations has not affected its
5204                --  privacy status, so use its own full view explicitly.
5205
5206                if Is_Private_Type (Parent_Type) then
5207                   Build_Derived_Record_Type
5208                     (Full_Decl, Full_View (Parent_Type), Full_Der, False);
5209                else
5210                   Build_Derived_Record_Type
5211                     (Full_Decl, Parent_Type, Full_Der, False);
5212                end if;
5213
5214             else
5215                --  If full view of parent is tagged, the completion
5216                --  inherits the proper primitive operations.
5217
5218                Set_Defining_Identifier (Full_Decl, Full_Der);
5219                Build_Derived_Record_Type
5220                  (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
5221                Set_Analyzed (Full_Decl);
5222             end if;
5223
5224             if Swapped then
5225                Uninstall_Declarations (Par_Scope);
5226
5227                if In_Open_Scopes (Par_Scope) then
5228                   Install_Visible_Declarations (Par_Scope);
5229                end if;
5230             end if;
5231
5232             Der_Base := Base_Type (Derived_Type);
5233             Set_Full_View (Derived_Type, Full_Der);
5234             Set_Full_View (Der_Base, Base_Type (Full_Der));
5235
5236             --  Copy the discriminant list from full view to the partial views
5237             --  (base type and its subtype). Gigi requires that the partial
5238             --  and full views have the same discriminants.
5239
5240             --  Note that since the partial view is pointing to discriminants
5241             --  in the full view, their scope will be that of the full view.
5242             --  This might cause some front end problems and need
5243             --  adjustment???
5244
5245             Discr := First_Discriminant (Base_Type (Full_Der));
5246             Set_First_Entity (Der_Base, Discr);
5247
5248             loop
5249                Last_Discr := Discr;
5250                Next_Discriminant (Discr);
5251                exit when No (Discr);
5252             end loop;
5253
5254             Set_Last_Entity (Der_Base, Last_Discr);
5255
5256             Set_First_Entity (Derived_Type, First_Entity (Der_Base));
5257             Set_Last_Entity  (Derived_Type, Last_Entity  (Der_Base));
5258             Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
5259
5260          else
5261             --  If this is a completion, the derived type stays private
5262             --  and there is no need to create a further full view, except
5263             --  in the unusual case when the derivation is nested within a
5264             --  child unit, see below.
5265
5266             null;
5267          end if;
5268
5269       elsif Present (Full_View (Parent_Type))
5270         and then  Has_Discriminants (Full_View (Parent_Type))
5271       then
5272          if Has_Unknown_Discriminants (Parent_Type)
5273            and then Nkind (Subtype_Indication (Type_Definition (N)))
5274              = N_Subtype_Indication
5275          then
5276             Error_Msg_N
5277               ("cannot constrain type with unknown discriminants",
5278                Subtype_Indication (Type_Definition (N)));
5279             return;
5280          end if;
5281
5282          --  If full view of parent is a record type, Build full view as
5283          --  a derivation from the parent's full view. Partial view remains
5284          --  private. For code generation and linking, the full view must
5285          --  have the same public status as the partial one. This full view
5286          --  is only needed if the parent type is in an enclosing scope, so
5287          --  that the full view may actually become visible, e.g. in a child
5288          --  unit. This is both more efficient, and avoids order of freezing
5289          --  problems with the added entities.
5290
5291          if not Is_Private_Type (Full_View (Parent_Type))
5292            and then (In_Open_Scopes (Scope (Parent_Type)))
5293          then
5294             Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
5295                                               Chars (Derived_Type));
5296             Set_Is_Itype (Full_Der);
5297             Set_Has_Private_Declaration (Full_Der);
5298             Set_Has_Private_Declaration (Derived_Type);
5299             Set_Associated_Node_For_Itype (Full_Der, N);
5300             Set_Parent (Full_Der, Parent (Derived_Type));
5301             Set_Full_View (Derived_Type, Full_Der);
5302             Set_Is_Public (Full_Der, Is_Public (Derived_Type));
5303             Full_P := Full_View (Parent_Type);
5304             Exchange_Declarations (Parent_Type);
5305             Copy_And_Build;
5306             Exchange_Declarations (Full_P);
5307
5308          else
5309             Build_Derived_Record_Type
5310               (N, Full_View (Parent_Type), Derived_Type,
5311                 Derive_Subps => False);
5312          end if;
5313
5314          --  In any case, the primitive operations are inherited from
5315          --  the parent type, not from the internal full view.
5316
5317          Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
5318
5319          if Derive_Subps then
5320             Derive_Subprograms (Parent_Type, Derived_Type);
5321          end if;
5322
5323       else
5324          --  Untagged type, No discriminants on either view
5325
5326          if Nkind (Subtype_Indication (Type_Definition (N))) =
5327                                                    N_Subtype_Indication
5328          then
5329             Error_Msg_N
5330               ("illegal constraint on type without discriminants", N);
5331          end if;
5332
5333          if Present (Discriminant_Specifications (N))
5334            and then Present (Full_View (Parent_Type))
5335            and then not Is_Tagged_Type (Full_View (Parent_Type))
5336          then
5337             Error_Msg_N
5338               ("cannot add discriminants to untagged type", N);
5339          end if;
5340
5341          Set_Stored_Constraint (Derived_Type, No_Elist);
5342          Set_Is_Constrained    (Derived_Type, Is_Constrained (Parent_Type));
5343          Set_Is_Controlled     (Derived_Type, Is_Controlled  (Parent_Type));
5344          Set_Has_Controlled_Component
5345                                (Derived_Type, Has_Controlled_Component
5346                                                              (Parent_Type));
5347
5348          --  Direct controlled types do not inherit Finalize_Storage_Only flag
5349
5350          if not Is_Controlled  (Parent_Type) then
5351             Set_Finalize_Storage_Only
5352               (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
5353          end if;
5354
5355          --  Construct the implicit full view by deriving from full view of
5356          --  the parent type. In order to get proper visibility, we install
5357          --  the parent scope and its declarations.
5358
5359          --  ??? if the parent is untagged private and its completion is
5360          --  tagged, this mechanism will not work because we cannot derive
5361          --  from the tagged full view unless we have an extension
5362
5363          if Present (Full_View (Parent_Type))
5364            and then not Is_Tagged_Type (Full_View (Parent_Type))
5365            and then not Is_Completion
5366          then
5367             Full_Der :=
5368               Make_Defining_Identifier (Sloc (Derived_Type),
5369                 Chars => Chars (Derived_Type));
5370             Set_Is_Itype (Full_Der);
5371             Set_Has_Private_Declaration (Full_Der);
5372             Set_Has_Private_Declaration (Derived_Type);
5373             Set_Associated_Node_For_Itype (Full_Der, N);
5374             Set_Parent (Full_Der, Parent (Derived_Type));
5375             Set_Full_View (Derived_Type, Full_Der);
5376
5377             if not In_Open_Scopes (Par_Scope) then
5378                Install_Private_Declarations (Par_Scope);
5379                Install_Visible_Declarations (Par_Scope);
5380                Copy_And_Build;
5381                Uninstall_Declarations (Par_Scope);
5382
5383             --  If parent scope is open and in another unit, and parent has a
5384             --  completion, then the derivation is taking place in the visible
5385             --  part of a child unit. In that case retrieve the full view of
5386             --  the parent momentarily.
5387
5388             elsif not In_Same_Source_Unit (N, Parent_Type) then
5389                Full_P := Full_View (Parent_Type);
5390                Exchange_Declarations (Parent_Type);
5391                Copy_And_Build;
5392                Exchange_Declarations (Full_P);
5393
5394             --  Otherwise it is a local derivation
5395
5396             else
5397                Copy_And_Build;
5398             end if;
5399
5400             Set_Scope                (Full_Der, Current_Scope);
5401             Set_Is_First_Subtype     (Full_Der,
5402                                        Is_First_Subtype (Derived_Type));
5403             Set_Has_Size_Clause      (Full_Der, False);
5404             Set_Has_Alignment_Clause (Full_Der, False);
5405             Set_Next_Entity          (Full_Der, Empty);
5406             Set_Has_Delayed_Freeze   (Full_Der);
5407             Set_Is_Frozen            (Full_Der, False);
5408             Set_Freeze_Node          (Full_Der, Empty);
5409             Set_Depends_On_Private   (Full_Der,
5410                                         Has_Private_Component    (Full_Der));
5411             Set_Public_Status        (Full_Der);
5412          end if;
5413       end if;
5414
5415       Set_Has_Unknown_Discriminants (Derived_Type,
5416         Has_Unknown_Discriminants (Parent_Type));
5417
5418       if Is_Private_Type (Derived_Type) then
5419          Set_Private_Dependents (Derived_Type, New_Elmt_List);
5420       end if;
5421
5422       if Is_Private_Type (Parent_Type)
5423         and then Base_Type (Parent_Type) = Parent_Type
5424         and then In_Open_Scopes (Scope (Parent_Type))
5425       then
5426          Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
5427
5428          if Is_Child_Unit (Scope (Current_Scope))
5429            and then Is_Completion
5430            and then In_Private_Part (Current_Scope)
5431            and then Scope (Parent_Type) /= Current_Scope
5432          then
5433             --  This is the unusual case where a type completed by a private
5434             --  derivation occurs within a package nested in a child unit,
5435             --  and the parent is declared in an ancestor. In this case, the
5436             --  full view of the parent type will become visible in the body
5437             --  of the enclosing child, and only then will the current type
5438             --  be possibly non-private. We build a underlying full view that
5439             --  will be installed when the enclosing child body is compiled.
5440
5441             Full_Der :=
5442               Make_Defining_Identifier (Sloc (Derived_Type),
5443                 Chars => Chars (Derived_Type));
5444             Set_Is_Itype (Full_Der);
5445             Build_Itype_Reference (Full_Der, N);
5446
5447             --  The full view will be used to swap entities on entry/exit to
5448             --  the body, and must appear in the entity list for the package.
5449
5450             Append_Entity (Full_Der, Scope (Derived_Type));
5451             Set_Has_Private_Declaration (Full_Der);
5452             Set_Has_Private_Declaration (Derived_Type);
5453             Set_Associated_Node_For_Itype (Full_Der, N);
5454             Set_Parent (Full_Der, Parent (Derived_Type));
5455             Full_P := Full_View (Parent_Type);
5456             Exchange_Declarations (Parent_Type);
5457             Copy_And_Build;
5458             Exchange_Declarations (Full_P);
5459             Set_Underlying_Full_View (Derived_Type, Full_Der);
5460          end if;
5461       end if;
5462    end Build_Derived_Private_Type;
5463
5464    -------------------------------
5465    -- Build_Derived_Record_Type --
5466    -------------------------------
5467
5468    --  1. INTRODUCTION
5469
5470    --  Ideally we would like to use the same model of type derivation for
5471    --  tagged and untagged record types. Unfortunately this is not quite
5472    --  possible because the semantics of representation clauses is different
5473    --  for tagged and untagged records under inheritance. Consider the
5474    --  following:
5475
5476    --     type R (...) is [tagged] record ... end record;
5477    --     type T (...) is new R (...) [with ...];
5478
5479    --  The representation clauses for T can specify a completely different
5480    --  record layout from R's. Hence the same component can be placed in two
5481    --  very different positions in objects of type T and R. If R and are tagged
5482    --  types, representation clauses for T can only specify the layout of non
5483    --  inherited components, thus components that are common in R and T have
5484    --  the same position in objects of type R and T.
5485
5486    --  This has two implications. The first is that the entire tree for R's
5487    --  declaration needs to be copied for T in the untagged case, so that T
5488    --  can be viewed as a record type of its own with its own representation
5489    --  clauses. The second implication is the way we handle discriminants.
5490    --  Specifically, in the untagged case we need a way to communicate to Gigi
5491    --  what are the real discriminants in the record, while for the semantics
5492    --  we need to consider those introduced by the user to rename the
5493    --  discriminants in the parent type. This is handled by introducing the
5494    --  notion of stored discriminants. See below for more.
5495
5496    --  Fortunately the way regular components are inherited can be handled in
5497    --  the same way in tagged and untagged types.
5498
5499    --  To complicate things a bit more the private view of a private extension
5500    --  cannot be handled in the same way as the full view (for one thing the
5501    --  semantic rules are somewhat different). We will explain what differs
5502    --  below.
5503
5504    --  2. DISCRIMINANTS UNDER INHERITANCE
5505
5506    --  The semantic rules governing the discriminants of derived types are
5507    --  quite subtle.
5508
5509    --   type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
5510    --      [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
5511
5512    --  If parent type has discriminants, then the discriminants that are
5513    --  declared in the derived type are [3.4 (11)]:
5514
5515    --  o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
5516    --    there is one;
5517
5518    --  o Otherwise, each discriminant of the parent type (implicitly declared
5519    --    in the same order with the same specifications). In this case, the
5520    --    discriminants are said to be "inherited", or if unknown in the parent
5521    --    are also unknown in the derived type.
5522
5523    --  Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
5524
5525    --  o The parent subtype shall be constrained;
5526
5527    --  o If the parent type is not a tagged type, then each discriminant of
5528    --    the derived type shall be used in the constraint defining a parent
5529    --    subtype. [Implementation note: This ensures that the new discriminant
5530    --    can share storage with an existing discriminant.]
5531
5532    --  For the derived type each discriminant of the parent type is either
5533    --  inherited, constrained to equal some new discriminant of the derived
5534    --  type, or constrained to the value of an expression.
5535
5536    --  When inherited or constrained to equal some new discriminant, the
5537    --  parent discriminant and the discriminant of the derived type are said
5538    --  to "correspond".
5539
5540    --  If a discriminant of the parent type is constrained to a specific value
5541    --  in the derived type definition, then the discriminant is said to be
5542    --  "specified" by that derived type definition.
5543
5544    --  3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
5545
5546    --  We have spoken about stored discriminants in point 1 (introduction)
5547    --  above. There are two sort of stored discriminants: implicit and
5548    --  explicit. As long as the derived type inherits the same discriminants as
5549    --  the root record type, stored discriminants are the same as regular
5550    --  discriminants, and are said to be implicit. However, if any discriminant
5551    --  in the root type was renamed in the derived type, then the derived
5552    --  type will contain explicit stored discriminants. Explicit stored
5553    --  discriminants are discriminants in addition to the semantically visible
5554    --  discriminants defined for the derived type. Stored discriminants are
5555    --  used by Gigi to figure out what are the physical discriminants in
5556    --  objects of the derived type (see precise definition in einfo.ads).
5557    --  As an example, consider the following:
5558
5559    --           type R  (D1, D2, D3 : Int) is record ... end record;
5560    --           type T1 is new R;
5561    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
5562    --           type T3 is new T2;
5563    --           type T4 (Y : Int) is new T3 (Y, 99);
5564
5565    --  The following table summarizes the discriminants and stored
5566    --  discriminants in R and T1 through T4.
5567
5568    --   Type      Discrim     Stored Discrim  Comment
5569    --    R      (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in R
5570    --    T1     (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in T1
5571    --    T2     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T2
5572    --    T3     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T3
5573    --    T4     (Y)            (D1, D2, D3)   Girder discrims EXPLICIT in T4
5574
5575    --  Field Corresponding_Discriminant (abbreviated CD below) allows us to
5576    --  find the corresponding discriminant in the parent type, while
5577    --  Original_Record_Component (abbreviated ORC below), the actual physical
5578    --  component that is renamed. Finally the field Is_Completely_Hidden
5579    --  (abbreviated ICH below) is set for all explicit stored discriminants
5580    --  (see einfo.ads for more info). For the above example this gives:
5581
5582    --                 Discrim     CD        ORC     ICH
5583    --                 ^^^^^^^     ^^        ^^^     ^^^
5584    --                 D1 in R    empty     itself    no
5585    --                 D2 in R    empty     itself    no
5586    --                 D3 in R    empty     itself    no
5587
5588    --                 D1 in T1  D1 in R    itself    no
5589    --                 D2 in T1  D2 in R    itself    no
5590    --                 D3 in T1  D3 in R    itself    no
5591
5592    --                 X1 in T2  D3 in T1  D3 in T2   no
5593    --                 X2 in T2  D1 in T1  D1 in T2   no
5594    --                 D1 in T2   empty    itself    yes
5595    --                 D2 in T2   empty    itself    yes
5596    --                 D3 in T2   empty    itself    yes
5597
5598    --                 X1 in T3  X1 in T2  D3 in T3   no
5599    --                 X2 in T3  X2 in T2  D1 in T3   no
5600    --                 D1 in T3   empty    itself    yes
5601    --                 D2 in T3   empty    itself    yes
5602    --                 D3 in T3   empty    itself    yes
5603
5604    --                 Y  in T4  X1 in T3  D3 in T3   no
5605    --                 D1 in T3   empty    itself    yes
5606    --                 D2 in T3   empty    itself    yes
5607    --                 D3 in T3   empty    itself    yes
5608
5609    --  4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
5610
5611    --  Type derivation for tagged types is fairly straightforward. If no
5612    --  discriminants are specified by the derived type, these are inherited
5613    --  from the parent. No explicit stored discriminants are ever necessary.
5614    --  The only manipulation that is done to the tree is that of adding a
5615    --  _parent field with parent type and constrained to the same constraint
5616    --  specified for the parent in the derived type definition. For instance:
5617
5618    --           type R  (D1, D2, D3 : Int) is tagged record ... end record;
5619    --           type T1 is new R with null record;
5620    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
5621
5622    --  are changed into:
5623
5624    --           type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
5625    --              _parent : R (D1, D2, D3);
5626    --           end record;
5627
5628    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
5629    --              _parent : T1 (X2, 88, X1);
5630    --           end record;
5631
5632    --  The discriminants actually present in R, T1 and T2 as well as their CD,
5633    --  ORC and ICH fields are:
5634
5635    --                 Discrim     CD        ORC     ICH
5636    --                 ^^^^^^^     ^^        ^^^     ^^^
5637    --                 D1 in R    empty     itself    no
5638    --                 D2 in R    empty     itself    no
5639    --                 D3 in R    empty     itself    no
5640
5641    --                 D1 in T1  D1 in R    D1 in R   no
5642    --                 D2 in T1  D2 in R    D2 in R   no
5643    --                 D3 in T1  D3 in R    D3 in R   no
5644
5645    --                 X1 in T2  D3 in T1   D3 in R   no
5646    --                 X2 in T2  D1 in T1   D1 in R   no
5647
5648    --  5. FIRST TRANSFORMATION FOR DERIVED RECORDS
5649    --
5650    --  Regardless of whether we dealing with a tagged or untagged type
5651    --  we will transform all derived type declarations of the form
5652    --
5653    --               type T is new R (...) [with ...];
5654    --  or
5655    --               subtype S is R (...);
5656    --               type T is new S [with ...];
5657    --  into
5658    --               type BT is new R [with ...];
5659    --               subtype T is BT (...);
5660    --
5661    --  That is, the base derived type is constrained only if it has no
5662    --  discriminants. The reason for doing this is that GNAT's semantic model
5663    --  assumes that a base type with discriminants is unconstrained.
5664    --
5665    --  Note that, strictly speaking, the above transformation is not always
5666    --  correct. Consider for instance the following excerpt from ACVC b34011a:
5667    --
5668    --       procedure B34011A is
5669    --          type REC (D : integer := 0) is record
5670    --             I : Integer;
5671    --          end record;
5672
5673    --          package P is
5674    --             type T6 is new Rec;
5675    --             function F return T6;
5676    --          end P;
5677
5678    --          use P;
5679    --          package Q6 is
5680    --             type U is new T6 (Q6.F.I);                   -- ERROR: Q6.F.
5681    --          end Q6;
5682    --
5683    --  The definition of Q6.U is illegal. However transforming Q6.U into
5684
5685    --             type BaseU is new T6;
5686    --             subtype U is BaseU (Q6.F.I)
5687
5688    --  turns U into a legal subtype, which is incorrect. To avoid this problem
5689    --  we always analyze the constraint (in this case (Q6.F.I)) before applying
5690    --  the transformation described above.
5691
5692    --  There is another instance where the above transformation is incorrect.
5693    --  Consider:
5694
5695    --          package Pack is
5696    --             type Base (D : Integer) is tagged null record;
5697    --             procedure P (X : Base);
5698
5699    --             type Der is new Base (2) with null record;
5700    --             procedure P (X : Der);
5701    --          end Pack;
5702
5703    --  Then the above transformation turns this into
5704
5705    --             type Der_Base is new Base with null record;
5706    --             --  procedure P (X : Base) is implicitly inherited here
5707    --             --  as procedure P (X : Der_Base).
5708
5709    --             subtype Der is Der_Base (2);
5710    --             procedure P (X : Der);
5711    --             --  The overriding of P (X : Der_Base) is illegal since we
5712    --             --  have a parameter conformance problem.
5713
5714    --  To get around this problem, after having semantically processed Der_Base
5715    --  and the rewritten subtype declaration for Der, we copy Der_Base field
5716    --  Discriminant_Constraint from Der so that when parameter conformance is
5717    --  checked when P is overridden, no semantic errors are flagged.
5718
5719    --  6. SECOND TRANSFORMATION FOR DERIVED RECORDS
5720
5721    --  Regardless of whether we are dealing with a tagged or untagged type
5722    --  we will transform all derived type declarations of the form
5723
5724    --               type R (D1, .., Dn : ...) is [tagged] record ...;
5725    --               type T is new R [with ...];
5726    --  into
5727    --               type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
5728
5729    --  The reason for such transformation is that it allows us to implement a
5730    --  very clean form of component inheritance as explained below.
5731
5732    --  Note that this transformation is not achieved by direct tree rewriting
5733    --  and manipulation, but rather by redoing the semantic actions that the
5734    --  above transformation will entail. This is done directly in routine
5735    --  Inherit_Components.
5736
5737    --  7. TYPE DERIVATION AND COMPONENT INHERITANCE
5738
5739    --  In both tagged and untagged derived types, regular non discriminant
5740    --  components are inherited in the derived type from the parent type. In
5741    --  the absence of discriminants component, inheritance is straightforward
5742    --  as components can simply be copied from the parent.
5743
5744    --  If the parent has discriminants, inheriting components constrained with
5745    --  these discriminants requires caution. Consider the following example:
5746
5747    --      type R  (D1, D2 : Positive) is [tagged] record
5748    --         S : String (D1 .. D2);
5749    --      end record;
5750
5751    --      type T1                is new R        [with null record];
5752    --      type T2 (X : positive) is new R (1, X) [with null record];
5753
5754    --  As explained in 6. above, T1 is rewritten as
5755    --      type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
5756    --  which makes the treatment for T1 and T2 identical.
5757
5758    --  What we want when inheriting S, is that references to D1 and D2 in R are
5759    --  replaced with references to their correct constraints, ie D1 and D2 in
5760    --  T1 and 1 and X in T2. So all R's discriminant references are replaced
5761    --  with either discriminant references in the derived type or expressions.
5762    --  This replacement is achieved as follows: before inheriting R's
5763    --  components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
5764    --  created in the scope of T1 (resp. scope of T2) so that discriminants D1
5765    --  and D2 of T1 are visible (resp. discriminant X of T2 is visible).
5766    --  For T2, for instance, this has the effect of replacing String (D1 .. D2)
5767    --  by String (1 .. X).
5768
5769    --  8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
5770
5771    --  We explain here the rules governing private type extensions relevant to
5772    --  type derivation. These rules are explained on the following example:
5773
5774    --      type D [(...)] is new A [(...)] with private;      <-- partial view
5775    --      type D [(...)] is new P [(...)] with null record;  <-- full view
5776
5777    --  Type A is called the ancestor subtype of the private extension.
5778    --  Type P is the parent type of the full view of the private extension. It
5779    --  must be A or a type derived from A.
5780
5781    --  The rules concerning the discriminants of private type extensions are
5782    --  [7.3(10-13)]:
5783
5784    --  o If a private extension inherits known discriminants from the ancestor
5785    --    subtype, then the full view shall also inherit its discriminants from
5786    --    the ancestor subtype and the parent subtype of the full view shall be
5787    --    constrained if and only if the ancestor subtype is constrained.
5788
5789    --  o If a partial view has unknown discriminants, then the full view may
5790    --    define a definite or an indefinite subtype, with or without
5791    --    discriminants.
5792
5793    --  o If a partial view has neither known nor unknown discriminants, then
5794    --    the full view shall define a definite subtype.
5795
5796    --  o If the ancestor subtype of a private extension has constrained
5797    --    discriminants, then the parent subtype of the full view shall impose a
5798    --    statically matching constraint on those discriminants.
5799
5800    --  This means that only the following forms of private extensions are
5801    --  allowed:
5802
5803    --      type D is new A with private;      <-- partial view
5804    --      type D is new P with null record;  <-- full view
5805
5806    --  If A has no discriminants than P has no discriminants, otherwise P must
5807    --  inherit A's discriminants.
5808
5809    --      type D is new A (...) with private;      <-- partial view
5810    --      type D is new P (:::) with null record;  <-- full view
5811
5812    --  P must inherit A's discriminants and (...) and (:::) must statically
5813    --  match.
5814
5815    --      subtype A is R (...);
5816    --      type D is new A with private;      <-- partial view
5817    --      type D is new P with null record;  <-- full view
5818
5819    --  P must have inherited R's discriminants and must be derived from A or
5820    --  any of its subtypes.
5821
5822    --      type D (..) is new A with private;              <-- partial view
5823    --      type D (..) is new P [(:::)] with null record;  <-- full view
5824
5825    --  No specific constraints on P's discriminants or constraint (:::).
5826    --  Note that A can be unconstrained, but the parent subtype P must either
5827    --  be constrained or (:::) must be present.
5828
5829    --      type D (..) is new A [(...)] with private;      <-- partial view
5830    --      type D (..) is new P [(:::)] with null record;  <-- full view
5831
5832    --  P's constraints on A's discriminants must statically match those
5833    --  imposed by (...).
5834
5835    --  9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
5836
5837    --  The full view of a private extension is handled exactly as described
5838    --  above. The model chose for the private view of a private extension is
5839    --  the same for what concerns discriminants (ie they receive the same
5840    --  treatment as in the tagged case). However, the private view of the
5841    --  private extension always inherits the components of the parent base,
5842    --  without replacing any discriminant reference. Strictly speaking this is
5843    --  incorrect. However, Gigi never uses this view to generate code so this
5844    --  is a purely semantic issue. In theory, a set of transformations similar
5845    --  to those given in 5. and 6. above could be applied to private views of
5846    --  private extensions to have the same model of component inheritance as
5847    --  for non private extensions. However, this is not done because it would
5848    --  further complicate private type processing. Semantically speaking, this
5849    --  leaves us in an uncomfortable situation. As an example consider:
5850
5851    --          package Pack is
5852    --             type R (D : integer) is tagged record
5853    --                S : String (1 .. D);
5854    --             end record;
5855    --             procedure P (X : R);
5856    --             type T is new R (1) with private;
5857    --          private
5858    --             type T is new R (1) with null record;
5859    --          end;
5860
5861    --  This is transformed into:
5862
5863    --          package Pack is
5864    --             type R (D : integer) is tagged record
5865    --                S : String (1 .. D);
5866    --             end record;
5867    --             procedure P (X : R);
5868    --             type T is new R (1) with private;
5869    --          private
5870    --             type BaseT is new R with null record;
5871    --             subtype  T is BaseT (1);
5872    --          end;
5873
5874    --  (strictly speaking the above is incorrect Ada)
5875
5876    --  From the semantic standpoint the private view of private extension T
5877    --  should be flagged as constrained since one can clearly have
5878    --
5879    --             Obj : T;
5880    --
5881    --  in a unit withing Pack. However, when deriving subprograms for the
5882    --  private view of private extension T, T must be seen as unconstrained
5883    --  since T has discriminants (this is a constraint of the current
5884    --  subprogram derivation model). Thus, when processing the private view of
5885    --  a private extension such as T, we first mark T as unconstrained, we
5886    --  process it, we perform program derivation and just before returning from
5887    --  Build_Derived_Record_Type we mark T as constrained.
5888
5889    --  ??? Are there are other uncomfortable cases that we will have to
5890    --      deal with.
5891
5892    --  10. RECORD_TYPE_WITH_PRIVATE complications
5893
5894    --  Types that are derived from a visible record type and have a private
5895    --  extension present other peculiarities. They behave mostly like private
5896    --  types, but if they have primitive operations defined, these will not
5897    --  have the proper signatures for further inheritance, because other
5898    --  primitive operations will use the implicit base that we define for
5899    --  private derivations below. This affect subprogram inheritance (see
5900    --  Derive_Subprograms for details). We also derive the implicit base from
5901    --  the base type of the full view, so that the implicit base is a record
5902    --  type and not another private type, This avoids infinite loops.
5903
5904    procedure Build_Derived_Record_Type
5905      (N            : Node_Id;
5906       Parent_Type  : Entity_Id;
5907       Derived_Type : Entity_Id;
5908       Derive_Subps : Boolean := True)
5909    is
5910       Loc          : constant Source_Ptr := Sloc (N);
5911       Parent_Base  : Entity_Id;
5912       Type_Def     : Node_Id;
5913       Indic        : Node_Id;
5914       Discrim      : Entity_Id;
5915       Last_Discrim : Entity_Id;
5916       Constrs      : Elist_Id;
5917
5918       Discs : Elist_Id := New_Elmt_List;
5919       --  An empty Discs list means that there were no constraints in the
5920       --  subtype indication or that there was an error processing it.
5921
5922       Assoc_List : Elist_Id;
5923       New_Discrs : Elist_Id;
5924       New_Base   : Entity_Id;
5925       New_Decl   : Node_Id;
5926       New_Indic  : Node_Id;
5927
5928       Is_Tagged          : constant Boolean := Is_Tagged_Type (Parent_Type);
5929       Discriminant_Specs : constant Boolean :=
5930                              Present (Discriminant_Specifications (N));
5931       Private_Extension  : constant Boolean :=
5932                              (Nkind (N) = N_Private_Extension_Declaration);
5933
5934       Constraint_Present : Boolean;
5935       Inherit_Discrims   : Boolean := False;
5936       Save_Etype         : Entity_Id;
5937       Save_Discr_Constr  : Elist_Id;
5938       Save_Next_Entity   : Entity_Id;
5939
5940    begin
5941       if Ekind (Parent_Type) = E_Record_Type_With_Private
5942         and then Present (Full_View (Parent_Type))
5943         and then Has_Discriminants (Parent_Type)
5944       then
5945          Parent_Base := Base_Type (Full_View (Parent_Type));
5946       else
5947          Parent_Base := Base_Type (Parent_Type);
5948       end if;
5949
5950       --  Before we start the previously documented transformations, here is
5951       --  little fix for size and alignment of tagged types. Normally when we
5952       --  derive type D from type P, we copy the size and alignment of P as the
5953       --  default for D, and in the absence of explicit representation clauses
5954       --  for D, the size and alignment are indeed the same as the parent.
5955
5956       --  But this is wrong for tagged types, since fields may be added, and
5957       --  the default size may need to be larger, and the default alignment may
5958       --  need to be larger.
5959
5960       --  We therefore reset the size and alignment fields in the tagged case.
5961       --  Note that the size and alignment will in any case be at least as
5962       --  large as the parent type (since the derived type has a copy of the
5963       --  parent type in the _parent field)
5964
5965       --  The type is also marked as being tagged here, which is needed when
5966       --  processing components with a self-referential anonymous access type
5967       --  in the call to Check_Anonymous_Access_Components below. Note that
5968       --  this flag is also set later on for completeness.
5969
5970       if Is_Tagged then
5971          Set_Is_Tagged_Type (Derived_Type);
5972          Init_Size_Align    (Derived_Type);
5973       end if;
5974
5975       --  STEP 0a: figure out what kind of derived type declaration we have
5976
5977       if Private_Extension then
5978          Type_Def := N;
5979          Set_Ekind (Derived_Type, E_Record_Type_With_Private);
5980
5981       else
5982          Type_Def := Type_Definition (N);
5983
5984          --  Ekind (Parent_Base) is not necessarily E_Record_Type since
5985          --  Parent_Base can be a private type or private extension. However,
5986          --  for tagged types with an extension the newly added fields are
5987          --  visible and hence the Derived_Type is always an E_Record_Type.
5988          --  (except that the parent may have its own private fields).
5989          --  For untagged types we preserve the Ekind of the Parent_Base.
5990
5991          if Present (Record_Extension_Part (Type_Def)) then
5992             Set_Ekind (Derived_Type, E_Record_Type);
5993
5994             --  Create internal access types for components with anonymous
5995             --  access types.
5996
5997             if Ada_Version >= Ada_05 then
5998                Check_Anonymous_Access_Components
5999                  (N, Derived_Type, Derived_Type,
6000                    Component_List (Record_Extension_Part (Type_Def)));
6001             end if;
6002
6003          else
6004             Set_Ekind (Derived_Type, Ekind (Parent_Base));
6005          end if;
6006       end if;
6007
6008       --  Indic can either be an N_Identifier if the subtype indication
6009       --  contains no constraint or an N_Subtype_Indication if the subtype
6010       --  indication has a constraint.
6011
6012       Indic := Subtype_Indication (Type_Def);
6013       Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
6014
6015       --  Check that the type has visible discriminants. The type may be
6016       --  a private type with unknown discriminants whose full view has
6017       --  discriminants which are invisible.
6018
6019       if Constraint_Present then
6020          if not Has_Discriminants (Parent_Base)
6021            or else
6022              (Has_Unknown_Discriminants (Parent_Base)
6023                 and then Is_Private_Type (Parent_Base))
6024          then
6025             Error_Msg_N
6026               ("invalid constraint: type has no discriminant",
6027                  Constraint (Indic));
6028
6029             Constraint_Present := False;
6030             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6031
6032          elsif Is_Constrained (Parent_Type) then
6033             Error_Msg_N
6034                ("invalid constraint: parent type is already constrained",
6035                   Constraint (Indic));
6036
6037             Constraint_Present := False;
6038             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6039          end if;
6040       end if;
6041
6042       --  STEP 0b: If needed, apply transformation given in point 5. above
6043
6044       if not Private_Extension
6045         and then Has_Discriminants (Parent_Type)
6046         and then not Discriminant_Specs
6047         and then (Is_Constrained (Parent_Type) or else Constraint_Present)
6048       then
6049          --  First, we must analyze the constraint (see comment in point 5.)
6050
6051          if Constraint_Present then
6052             New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
6053
6054             if Has_Discriminants (Derived_Type)
6055               and then Has_Private_Declaration (Derived_Type)
6056               and then Present (Discriminant_Constraint (Derived_Type))
6057             then
6058                --  Verify that constraints of the full view conform to those
6059                --  given in partial view.
6060
6061                declare
6062                   C1, C2 : Elmt_Id;
6063
6064                begin
6065                   C1 := First_Elmt (New_Discrs);
6066                   C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
6067                   while Present (C1) and then Present (C2) loop
6068                      if not
6069                        Fully_Conformant_Expressions (Node (C1), Node (C2))
6070                      then
6071                         Error_Msg_N (
6072                           "constraint not conformant to previous declaration",
6073                              Node (C1));
6074                      end if;
6075
6076                      Next_Elmt (C1);
6077                      Next_Elmt (C2);
6078                   end loop;
6079                end;
6080             end if;
6081          end if;
6082
6083          --  Insert and analyze the declaration for the unconstrained base type
6084
6085          New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6086
6087          New_Decl :=
6088            Make_Full_Type_Declaration (Loc,
6089               Defining_Identifier => New_Base,
6090               Type_Definition     =>
6091                 Make_Derived_Type_Definition (Loc,
6092                   Abstract_Present      => Abstract_Present (Type_Def),
6093                   Subtype_Indication    =>
6094                     New_Occurrence_Of (Parent_Base, Loc),
6095                   Record_Extension_Part =>
6096                     Relocate_Node (Record_Extension_Part (Type_Def))));
6097
6098          Set_Parent (New_Decl, Parent (N));
6099          Mark_Rewrite_Insertion (New_Decl);
6100          Insert_Before (N, New_Decl);
6101
6102          --  Note that this call passes False for the Derive_Subps parameter
6103          --  because subprogram derivation is deferred until after creating
6104          --  the subtype (see below).
6105
6106          Build_Derived_Type
6107            (New_Decl, Parent_Base, New_Base,
6108             Is_Completion => True, Derive_Subps => False);
6109
6110          --  ??? This needs re-examination to determine whether the
6111          --  above call can simply be replaced by a call to Analyze.
6112
6113          Set_Analyzed (New_Decl);
6114
6115          --  Insert and analyze the declaration for the constrained subtype
6116
6117          if Constraint_Present then
6118             New_Indic :=
6119               Make_Subtype_Indication (Loc,
6120                 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6121                 Constraint   => Relocate_Node (Constraint (Indic)));
6122
6123          else
6124             declare
6125                Constr_List : constant List_Id := New_List;
6126                C           : Elmt_Id;
6127                Expr        : Node_Id;
6128
6129             begin
6130                C := First_Elmt (Discriminant_Constraint (Parent_Type));
6131                while Present (C) loop
6132                   Expr := Node (C);
6133
6134                   --  It is safe here to call New_Copy_Tree since
6135                   --  Force_Evaluation was called on each constraint in
6136                   --  Build_Discriminant_Constraints.
6137
6138                   Append (New_Copy_Tree (Expr), To => Constr_List);
6139
6140                   Next_Elmt (C);
6141                end loop;
6142
6143                New_Indic :=
6144                  Make_Subtype_Indication (Loc,
6145                    Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6146                    Constraint   =>
6147                      Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
6148             end;
6149          end if;
6150
6151          Rewrite (N,
6152            Make_Subtype_Declaration (Loc,
6153              Defining_Identifier => Derived_Type,
6154              Subtype_Indication  => New_Indic));
6155
6156          Analyze (N);
6157
6158          --  Derivation of subprograms must be delayed until the full subtype
6159          --  has been established to ensure proper overriding of subprograms
6160          --  inherited by full types. If the derivations occurred as part of
6161          --  the call to Build_Derived_Type above, then the check for type
6162          --  conformance would fail because earlier primitive subprograms
6163          --  could still refer to the full type prior the change to the new
6164          --  subtype and hence would not match the new base type created here.
6165
6166          Derive_Subprograms (Parent_Type, Derived_Type);
6167
6168          --  For tagged types the Discriminant_Constraint of the new base itype
6169          --  is inherited from the first subtype so that no subtype conformance
6170          --  problem arise when the first subtype overrides primitive
6171          --  operations inherited by the implicit base type.
6172
6173          if Is_Tagged then
6174             Set_Discriminant_Constraint
6175               (New_Base, Discriminant_Constraint (Derived_Type));
6176          end if;
6177
6178          return;
6179       end if;
6180
6181       --  If we get here Derived_Type will have no discriminants or it will be
6182       --  a discriminated unconstrained base type.
6183
6184       --  STEP 1a: perform preliminary actions/checks for derived tagged types
6185
6186       if Is_Tagged then
6187
6188          --  The parent type is frozen for non-private extensions (RM 13.14(7))
6189          --  The declaration of a specific descendant of an interface type
6190          --  freezes the interface type (RM 13.14).
6191
6192          if not Private_Extension
6193            or else Is_Interface (Parent_Base)
6194          then
6195             Freeze_Before (N, Parent_Type);
6196          end if;
6197
6198          --  In Ada 2005 (AI-344), the restriction that a derived tagged type
6199          --  cannot be declared at a deeper level than its parent type is
6200          --  removed. The check on derivation within a generic body is also
6201          --  relaxed, but there's a restriction that a derived tagged type
6202          --  cannot be declared in a generic body if it's derived directly
6203          --  or indirectly from a formal type of that generic.
6204
6205          if Ada_Version >= Ada_05 then
6206             if Present (Enclosing_Generic_Body (Derived_Type)) then
6207                declare
6208                   Ancestor_Type : Entity_Id;
6209
6210                begin
6211                   --  Check to see if any ancestor of the derived type is a
6212                   --  formal type.
6213
6214                   Ancestor_Type := Parent_Type;
6215                   while not Is_Generic_Type (Ancestor_Type)
6216                     and then Etype (Ancestor_Type) /= Ancestor_Type
6217                   loop
6218                      Ancestor_Type := Etype (Ancestor_Type);
6219                   end loop;
6220
6221                   --  If the derived type does have a formal type as an
6222                   --  ancestor, then it's an error if the derived type is
6223                   --  declared within the body of the generic unit that
6224                   --  declares the formal type in its generic formal part. It's
6225                   --  sufficient to check whether the ancestor type is declared
6226                   --  inside the same generic body as the derived type (such as
6227                   --  within a nested generic spec), in which case the
6228                   --  derivation is legal. If the formal type is declared
6229                   --  outside of that generic body, then it's guaranteed that
6230                   --  the derived type is declared within the generic body of
6231                   --  the generic unit declaring the formal type.
6232
6233                   if Is_Generic_Type (Ancestor_Type)
6234                     and then Enclosing_Generic_Body (Ancestor_Type) /=
6235                                Enclosing_Generic_Body (Derived_Type)
6236                   then
6237                      Error_Msg_NE
6238                        ("parent type of& must not be descendant of formal type"
6239                           & " of an enclosing generic body",
6240                             Indic, Derived_Type);
6241                   end if;
6242                end;
6243             end if;
6244
6245          elsif Type_Access_Level (Derived_Type) /=
6246                  Type_Access_Level (Parent_Type)
6247            and then not Is_Generic_Type (Derived_Type)
6248          then
6249             if Is_Controlled (Parent_Type) then
6250                Error_Msg_N
6251                  ("controlled type must be declared at the library level",
6252                   Indic);
6253             else
6254                Error_Msg_N
6255                  ("type extension at deeper accessibility level than parent",
6256                   Indic);
6257             end if;
6258
6259          else
6260             declare
6261                GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
6262
6263             begin
6264                if Present (GB)
6265                  and then GB /= Enclosing_Generic_Body (Parent_Base)
6266                then
6267                   Error_Msg_NE
6268                     ("parent type of& must not be outside generic body"
6269                        & " (RM 3.9.1(4))",
6270                          Indic, Derived_Type);
6271                end if;
6272             end;
6273          end if;
6274       end if;
6275
6276       --  Ada 2005 (AI-251)
6277
6278       if Ada_Version = Ada_05
6279         and then Is_Tagged
6280       then
6281          --  "The declaration of a specific descendant of an interface type
6282          --  freezes the interface type" (RM 13.14).
6283
6284          declare
6285             Iface : Node_Id;
6286          begin
6287             if Is_Non_Empty_List (Interface_List (Type_Def)) then
6288                Iface := First (Interface_List (Type_Def));
6289                while Present (Iface) loop
6290                   Freeze_Before (N, Etype (Iface));
6291                   Next (Iface);
6292                end loop;
6293             end if;
6294          end;
6295       end if;
6296
6297       --  STEP 1b : preliminary cleanup of the full view of private types
6298
6299       --  If the type is already marked as having discriminants, then it's the
6300       --  completion of a private type or private extension and we need to
6301       --  retain the discriminants from the partial view if the current
6302       --  declaration has Discriminant_Specifications so that we can verify
6303       --  conformance. However, we must remove any existing components that
6304       --  were inherited from the parent (and attached in Copy_And_Swap)
6305       --  because the full type inherits all appropriate components anyway, and
6306       --  we do not want the partial view's components interfering.
6307
6308       if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
6309          Discrim := First_Discriminant (Derived_Type);
6310          loop
6311             Last_Discrim := Discrim;
6312             Next_Discriminant (Discrim);
6313             exit when No (Discrim);
6314          end loop;
6315
6316          Set_Last_Entity (Derived_Type, Last_Discrim);
6317
6318       --  In all other cases wipe out the list of inherited components (even
6319       --  inherited discriminants), it will be properly rebuilt here.
6320
6321       else
6322          Set_First_Entity (Derived_Type, Empty);
6323          Set_Last_Entity  (Derived_Type, Empty);
6324       end if;
6325
6326       --  STEP 1c: Initialize some flags for the Derived_Type
6327
6328       --  The following flags must be initialized here so that
6329       --  Process_Discriminants can check that discriminants of tagged types do
6330       --  not have a default initial value and that access discriminants are
6331       --  only specified for limited records. For completeness, these flags are
6332       --  also initialized along with all the other flags below.
6333
6334       --  AI-419: Limitedness is not inherited from an interface parent, so to
6335       --  be limited in that case the type must be explicitly declared as
6336       --  limited. However, task and protected interfaces are always limited.
6337
6338       if Limited_Present (Type_Def) then
6339          Set_Is_Limited_Record (Derived_Type);
6340
6341       elsif Is_Limited_Record (Parent_Type) then
6342          if not Is_Interface (Parent_Type)
6343            or else Is_Synchronized_Interface (Parent_Type)
6344            or else Is_Protected_Interface (Parent_Type)
6345            or else Is_Task_Interface (Parent_Type)
6346          then
6347             Set_Is_Limited_Record (Derived_Type);
6348          end if;
6349       end if;
6350
6351       --  STEP 2a: process discriminants of derived type if any
6352
6353       Push_Scope (Derived_Type);
6354
6355       if Discriminant_Specs then
6356          Set_Has_Unknown_Discriminants (Derived_Type, False);
6357
6358          --  The following call initializes fields Has_Discriminants and
6359          --  Discriminant_Constraint, unless we are processing the completion
6360          --  of a private type declaration.
6361
6362          Check_Or_Process_Discriminants (N, Derived_Type);
6363
6364          --  For non-tagged types the constraint on the Parent_Type must be
6365          --  present and is used to rename the discriminants.
6366
6367          if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
6368             Error_Msg_N ("untagged parent must have discriminants", Indic);
6369
6370          elsif not Is_Tagged and then not Constraint_Present then
6371             Error_Msg_N
6372               ("discriminant constraint needed for derived untagged records",
6373                Indic);
6374
6375          --  Otherwise the parent subtype must be constrained unless we have a
6376          --  private extension.
6377
6378          elsif not Constraint_Present
6379            and then not Private_Extension
6380            and then not Is_Constrained (Parent_Type)
6381          then
6382             Error_Msg_N
6383               ("unconstrained type not allowed in this context", Indic);
6384
6385          elsif Constraint_Present then
6386             --  The following call sets the field Corresponding_Discriminant
6387             --  for the discriminants in the Derived_Type.
6388
6389             Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
6390
6391             --  For untagged types all new discriminants must rename
6392             --  discriminants in the parent. For private extensions new
6393             --  discriminants cannot rename old ones (implied by [7.3(13)]).
6394
6395             Discrim := First_Discriminant (Derived_Type);
6396             while Present (Discrim) loop
6397                if not Is_Tagged
6398                  and then No (Corresponding_Discriminant (Discrim))
6399                then
6400                   Error_Msg_N
6401                     ("new discriminants must constrain old ones", Discrim);
6402
6403                elsif Private_Extension
6404                  and then Present (Corresponding_Discriminant (Discrim))
6405                then
6406                   Error_Msg_N
6407                     ("only static constraints allowed for parent"
6408                      & " discriminants in the partial view", Indic);
6409                   exit;
6410                end if;
6411
6412                --  If a new discriminant is used in the constraint, then its
6413                --  subtype must be statically compatible with the parent
6414                --  discriminant's subtype (3.7(15)).
6415
6416                if Present (Corresponding_Discriminant (Discrim))
6417                  and then
6418                    not Subtypes_Statically_Compatible
6419                          (Etype (Discrim),
6420                           Etype (Corresponding_Discriminant (Discrim)))
6421                then
6422                   Error_Msg_N
6423                     ("subtype must be compatible with parent discriminant",
6424                      Discrim);
6425                end if;
6426
6427                Next_Discriminant (Discrim);
6428             end loop;
6429
6430             --  Check whether the constraints of the full view statically
6431             --  match those imposed by the parent subtype [7.3(13)].
6432
6433             if Present (Stored_Constraint (Derived_Type)) then
6434                declare
6435                   C1, C2 : Elmt_Id;
6436
6437                begin
6438                   C1 := First_Elmt (Discs);
6439                   C2 := First_Elmt (Stored_Constraint (Derived_Type));
6440                   while Present (C1) and then Present (C2) loop
6441                      if not
6442                        Fully_Conformant_Expressions (Node (C1), Node (C2))
6443                      then
6444                         Error_Msg_N
6445                           ("not conformant with previous declaration",
6446                            Node (C1));
6447                      end if;
6448
6449                      Next_Elmt (C1);
6450                      Next_Elmt (C2);
6451                   end loop;
6452                end;
6453             end if;
6454          end if;
6455
6456       --  STEP 2b: No new discriminants, inherit discriminants if any
6457
6458       else
6459          if Private_Extension then
6460             Set_Has_Unknown_Discriminants
6461               (Derived_Type,
6462                Has_Unknown_Discriminants (Parent_Type)
6463                  or else Unknown_Discriminants_Present (N));
6464
6465          --  The partial view of the parent may have unknown discriminants,
6466          --  but if the full view has discriminants and the parent type is
6467          --  in scope they must be inherited.
6468
6469          elsif Has_Unknown_Discriminants (Parent_Type)
6470            and then
6471             (not Has_Discriminants (Parent_Type)
6472               or else not In_Open_Scopes (Scope (Parent_Type)))
6473          then
6474             Set_Has_Unknown_Discriminants (Derived_Type);
6475          end if;
6476
6477          if not Has_Unknown_Discriminants (Derived_Type)
6478            and then not Has_Unknown_Discriminants (Parent_Base)
6479            and then Has_Discriminants (Parent_Type)
6480          then
6481             Inherit_Discrims := True;
6482             Set_Has_Discriminants
6483               (Derived_Type, True);
6484             Set_Discriminant_Constraint
6485               (Derived_Type, Discriminant_Constraint (Parent_Base));
6486          end if;
6487
6488          --  The following test is true for private types (remember
6489          --  transformation 5. is not applied to those) and in an error
6490          --  situation.
6491
6492          if Constraint_Present then
6493             Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
6494          end if;
6495
6496          --  For now mark a new derived type as constrained only if it has no
6497          --  discriminants. At the end of Build_Derived_Record_Type we properly
6498          --  set this flag in the case of private extensions. See comments in
6499          --  point 9. just before body of Build_Derived_Record_Type.
6500
6501          Set_Is_Constrained
6502            (Derived_Type,
6503             not (Inherit_Discrims
6504                    or else Has_Unknown_Discriminants (Derived_Type)));
6505       end if;
6506
6507       --  STEP 3: initialize fields of derived type
6508
6509       Set_Is_Tagged_Type    (Derived_Type, Is_Tagged);
6510       Set_Stored_Constraint (Derived_Type, No_Elist);
6511
6512       --  Ada 2005 (AI-251): Private type-declarations can implement interfaces
6513       --  but cannot be interfaces
6514
6515       if not Private_Extension
6516          and then Ekind (Derived_Type) /= E_Private_Type
6517          and then Ekind (Derived_Type) /= E_Limited_Private_Type
6518       then
6519          if Interface_Present (Type_Def) then
6520             Analyze_Interface_Declaration (Derived_Type, Type_Def);
6521          end if;
6522
6523          Set_Abstract_Interfaces (Derived_Type, No_Elist);
6524       end if;
6525
6526       --  Fields inherited from the Parent_Type
6527
6528       Set_Discard_Names
6529         (Derived_Type, Einfo.Discard_Names  (Parent_Type));
6530       Set_Has_Specified_Layout
6531         (Derived_Type, Has_Specified_Layout (Parent_Type));
6532       Set_Is_Limited_Composite
6533         (Derived_Type, Is_Limited_Composite (Parent_Type));
6534       Set_Is_Private_Composite
6535         (Derived_Type, Is_Private_Composite (Parent_Type));
6536
6537       --  Fields inherited from the Parent_Base
6538
6539       Set_Has_Controlled_Component
6540         (Derived_Type, Has_Controlled_Component (Parent_Base));
6541       Set_Has_Non_Standard_Rep
6542         (Derived_Type, Has_Non_Standard_Rep     (Parent_Base));
6543       Set_Has_Primitive_Operations
6544         (Derived_Type, Has_Primitive_Operations (Parent_Base));
6545
6546       --  For non-private case, we also inherit Has_Complex_Representation
6547
6548       if Ekind (Derived_Type) = E_Record_Type then
6549          Set_Has_Complex_Representation
6550            (Derived_Type, Has_Complex_Representation (Parent_Base));
6551       end if;
6552
6553       --  Direct controlled types do not inherit Finalize_Storage_Only flag
6554
6555       if not Is_Controlled (Parent_Type) then
6556          Set_Finalize_Storage_Only
6557            (Derived_Type, Finalize_Storage_Only (Parent_Type));
6558       end if;
6559
6560       --  Set fields for private derived types
6561
6562       if Is_Private_Type (Derived_Type) then
6563          Set_Depends_On_Private (Derived_Type, True);
6564          Set_Private_Dependents (Derived_Type, New_Elmt_List);
6565
6566       --  Inherit fields from non private record types. If this is the
6567       --  completion of a derivation from a private type, the parent itself
6568       --  is private, and the attributes come from its full view, which must
6569       --  be present.
6570
6571       else
6572          if Is_Private_Type (Parent_Base)
6573            and then not Is_Record_Type (Parent_Base)
6574          then
6575             Set_Component_Alignment
6576               (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
6577             Set_C_Pass_By_Copy
6578               (Derived_Type, C_Pass_By_Copy      (Full_View (Parent_Base)));
6579          else
6580             Set_Component_Alignment
6581               (Derived_Type, Component_Alignment (Parent_Base));
6582
6583             Set_C_Pass_By_Copy
6584               (Derived_Type, C_Pass_By_Copy      (Parent_Base));
6585          end if;
6586       end if;
6587
6588       --  Set fields for tagged types
6589
6590       if Is_Tagged then
6591          Set_Primitive_Operations (Derived_Type, New_Elmt_List);
6592
6593          --  All tagged types defined in Ada.Finalization are controlled
6594
6595          if Chars (Scope (Derived_Type)) = Name_Finalization
6596            and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
6597            and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
6598          then
6599             Set_Is_Controlled (Derived_Type);
6600          else
6601             Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
6602          end if;
6603
6604          Make_Class_Wide_Type (Derived_Type);
6605          Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
6606
6607          if Has_Discriminants (Derived_Type)
6608            and then Constraint_Present
6609          then
6610             Set_Stored_Constraint
6611               (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
6612          end if;
6613
6614          if Ada_Version >= Ada_05 then
6615             declare
6616                Ifaces_List : Elist_Id;
6617
6618             begin
6619                --  Checks rules 3.9.4 (13/2 and 14/2)
6620
6621                if Comes_From_Source (Derived_Type)
6622                  and then not Is_Private_Type (Derived_Type)
6623                  and then Is_Interface (Parent_Type)
6624                  and then not Is_Interface (Derived_Type)
6625                then
6626                   if Is_Task_Interface (Parent_Type) then
6627                      Error_Msg_N
6628                        ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
6629                         Derived_Type);
6630
6631                   elsif Is_Protected_Interface (Parent_Type) then
6632                      Error_Msg_N
6633                        ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
6634                         Derived_Type);
6635                   end if;
6636                end if;
6637
6638                --  Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
6639
6640                Check_Abstract_Interfaces (N, Type_Def);
6641
6642                --  Ada 2005 (AI-251): Collect the list of progenitors that are
6643                --  not already in the parents.
6644
6645                Collect_Abstract_Interfaces
6646                  (T                         => Derived_Type,
6647                   Ifaces_List               => Ifaces_List,
6648                   Exclude_Parent_Interfaces => True);
6649                Set_Abstract_Interfaces (Derived_Type, Ifaces_List);
6650             end;
6651          end if;
6652
6653       else
6654          Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
6655          Set_Has_Non_Standard_Rep
6656                        (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
6657       end if;
6658
6659       --  STEP 4: Inherit components from the parent base and constrain them.
6660       --          Apply the second transformation described in point 6. above.
6661
6662       if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
6663         or else not Has_Discriminants (Parent_Type)
6664         or else not Is_Constrained (Parent_Type)
6665       then
6666          Constrs := Discs;
6667       else
6668          Constrs := Discriminant_Constraint (Parent_Type);
6669       end if;
6670
6671       Assoc_List :=
6672         Inherit_Components
6673           (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
6674
6675       --  STEP 5a: Copy the parent record declaration for untagged types
6676
6677       if not Is_Tagged then
6678
6679          --  Discriminant_Constraint (Derived_Type) has been properly
6680          --  constructed. Save it and temporarily set it to Empty because we
6681          --  do not want the call to New_Copy_Tree below to mess this list.
6682
6683          if Has_Discriminants (Derived_Type) then
6684             Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
6685             Set_Discriminant_Constraint (Derived_Type, No_Elist);
6686          else
6687             Save_Discr_Constr := No_Elist;
6688          end if;
6689
6690          --  Save the Etype field of Derived_Type. It is correctly set now,
6691          --  but the call to New_Copy tree may remap it to point to itself,
6692          --  which is not what we want. Ditto for the Next_Entity field.
6693
6694          Save_Etype       := Etype (Derived_Type);
6695          Save_Next_Entity := Next_Entity (Derived_Type);
6696
6697          --  Assoc_List maps all stored discriminants in the Parent_Base to
6698          --  stored discriminants in the Derived_Type. It is fundamental that
6699          --  no types or itypes with discriminants other than the stored
6700          --  discriminants appear in the entities declared inside
6701          --  Derived_Type, since the back end cannot deal with it.
6702
6703          New_Decl :=
6704            New_Copy_Tree
6705              (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
6706
6707          --  Restore the fields saved prior to the New_Copy_Tree call
6708          --  and compute the stored constraint.
6709
6710          Set_Etype       (Derived_Type, Save_Etype);
6711          Set_Next_Entity (Derived_Type, Save_Next_Entity);
6712
6713          if Has_Discriminants (Derived_Type) then
6714             Set_Discriminant_Constraint
6715               (Derived_Type, Save_Discr_Constr);
6716             Set_Stored_Constraint
6717               (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
6718             Replace_Components (Derived_Type, New_Decl);
6719          end if;
6720
6721          --  Insert the new derived type declaration
6722
6723          Rewrite (N, New_Decl);
6724
6725       --  STEP 5b: Complete the processing for record extensions in generics
6726
6727       --  There is no completion for record extensions declared in the
6728       --  parameter part of a generic, so we need to complete processing for
6729       --  these generic record extensions here. The Record_Type_Definition call
6730       --  will change the Ekind of the components from E_Void to E_Component.
6731
6732       elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
6733          Record_Type_Definition (Empty, Derived_Type);
6734
6735       --  STEP 5c: Process the record extension for non private tagged types
6736
6737       elsif not Private_Extension then
6738
6739          --  Add the _parent field in the derived type
6740
6741          Expand_Record_Extension (Derived_Type, Type_Def);
6742
6743          --  Ada 2005 (AI-251): Addition of the Tag corresponding to all the
6744          --  implemented interfaces if we are in expansion mode
6745
6746          if Expander_Active
6747            and then Has_Abstract_Interfaces (Derived_Type)
6748          then
6749             Add_Interface_Tag_Components (N, Derived_Type);
6750          end if;
6751
6752          --  Analyze the record extension
6753
6754          Record_Type_Definition
6755            (Record_Extension_Part (Type_Def), Derived_Type);
6756       end if;
6757
6758       End_Scope;
6759
6760       --  Nothing else to do if there is an error in the derivation.
6761       --  An unusual case: the full view may be derived from a type in an
6762       --  instance, when the partial view was used illegally as an actual
6763       --  in that instance, leading to a circular definition.
6764
6765       if Etype (Derived_Type) = Any_Type
6766         or else Etype (Parent_Type) = Derived_Type
6767       then
6768          return;
6769       end if;
6770
6771       --  Set delayed freeze and then derive subprograms, we need to do
6772       --  this in this order so that derived subprograms inherit the
6773       --  derived freeze if necessary.
6774
6775       Set_Has_Delayed_Freeze (Derived_Type);
6776
6777       if Derive_Subps then
6778          Derive_Subprograms (Parent_Type, Derived_Type);
6779       end if;
6780
6781       --  If we have a private extension which defines a constrained derived
6782       --  type mark as constrained here after we have derived subprograms. See
6783       --  comment on point 9. just above the body of Build_Derived_Record_Type.
6784
6785       if Private_Extension and then Inherit_Discrims then
6786          if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
6787             Set_Is_Constrained          (Derived_Type, True);
6788             Set_Discriminant_Constraint (Derived_Type, Discs);
6789
6790          elsif Is_Constrained (Parent_Type) then
6791             Set_Is_Constrained
6792               (Derived_Type, True);
6793             Set_Discriminant_Constraint
6794               (Derived_Type, Discriminant_Constraint (Parent_Type));
6795          end if;
6796       end if;
6797
6798       --  Update the class_wide type, which shares the now-completed
6799       --  entity list with its specific type.
6800
6801       if Is_Tagged then
6802          Set_First_Entity
6803            (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
6804          Set_Last_Entity
6805            (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
6806       end if;
6807
6808       --  Update the scope of anonymous access types of discriminants and other
6809       --  components, to prevent scope anomalies in gigi, when the derivation
6810       --  appears in a scope nested within that of the parent.
6811
6812       declare
6813          D : Entity_Id;
6814
6815       begin
6816          D := First_Entity (Derived_Type);
6817          while Present (D) loop
6818             if Ekind (D) = E_Discriminant
6819               or else Ekind (D) = E_Component
6820             then
6821                if Is_Itype (Etype (D))
6822                   and then Ekind (Etype (D)) = E_Anonymous_Access_Type
6823                then
6824                   Set_Scope (Etype (D), Current_Scope);
6825                end if;
6826             end if;
6827
6828             Next_Entity (D);
6829          end loop;
6830       end;
6831    end Build_Derived_Record_Type;
6832
6833    ------------------------
6834    -- Build_Derived_Type --
6835    ------------------------
6836
6837    procedure Build_Derived_Type
6838      (N             : Node_Id;
6839       Parent_Type   : Entity_Id;
6840       Derived_Type  : Entity_Id;
6841       Is_Completion : Boolean;
6842       Derive_Subps  : Boolean := True)
6843    is
6844       Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6845
6846    begin
6847       --  Set common attributes
6848
6849       Set_Scope         (Derived_Type, Current_Scope);
6850
6851       Set_Ekind         (Derived_Type, Ekind    (Parent_Base));
6852       Set_Etype         (Derived_Type,           Parent_Base);
6853       Set_Has_Task      (Derived_Type, Has_Task (Parent_Base));
6854
6855       Set_Size_Info     (Derived_Type,                Parent_Type);
6856       Set_RM_Size       (Derived_Type, RM_Size       (Parent_Type));
6857       Set_Convention    (Derived_Type, Convention    (Parent_Type));
6858       Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
6859
6860       --  The derived type inherits the representation clauses of the parent.
6861       --  However, for a private type that is completed by a derivation, there
6862       --  may be operation attributes that have been specified already (stream
6863       --  attributes and External_Tag) and those must be provided. Finally,
6864       --  if the partial view is a private extension, the representation items
6865       --  of the parent have been inherited already, and should not be chained
6866       --  twice to the derived type.
6867
6868       if Is_Tagged_Type (Parent_Type)
6869         and then Present (First_Rep_Item (Derived_Type))
6870       then
6871          --  The existing items are either operational items or items inherited
6872          --  from a private extension declaration.
6873
6874          declare
6875             Rep : Node_Id;
6876             --  Used to iterate over representation items of the derived type
6877
6878             Last_Rep : Node_Id;
6879             --  Last representation item of the (non-empty) representation
6880             --  item list of the derived type.
6881
6882             Found : Boolean := False;
6883
6884          begin
6885             Rep      := First_Rep_Item (Derived_Type);
6886             Last_Rep := Rep;
6887             while Present (Rep) loop
6888                if Rep = First_Rep_Item (Parent_Type) then
6889                   Found := True;
6890                   exit;
6891
6892                else
6893                   Rep := Next_Rep_Item (Rep);
6894
6895                   if Present (Rep) then
6896                      Last_Rep := Rep;
6897                   end if;
6898                end if;
6899             end loop;
6900
6901             --  Here if we either encountered the parent type's first rep
6902             --  item on the derived type's rep item list (in which case
6903             --  Found is True, and we have nothing else to do), or if we
6904             --  reached the last rep item of the derived type, which is
6905             --  Last_Rep, in which case we further chain the parent type's
6906             --  rep items to those of the derived type.
6907
6908             if not Found then
6909                Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
6910             end if;
6911          end;
6912
6913       else
6914          Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
6915       end if;
6916
6917       case Ekind (Parent_Type) is
6918          when Numeric_Kind =>
6919             Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
6920
6921          when Array_Kind =>
6922             Build_Derived_Array_Type (N, Parent_Type,  Derived_Type);
6923
6924          when E_Record_Type
6925             | E_Record_Subtype
6926             | Class_Wide_Kind  =>
6927             Build_Derived_Record_Type
6928               (N, Parent_Type, Derived_Type, Derive_Subps);
6929             return;
6930
6931          when Enumeration_Kind =>
6932             Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
6933
6934          when Access_Kind =>
6935             Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
6936
6937          when Incomplete_Or_Private_Kind =>
6938             Build_Derived_Private_Type
6939               (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
6940
6941             --  For discriminated types, the derivation includes deriving
6942             --  primitive operations. For others it is done below.
6943
6944             if Is_Tagged_Type (Parent_Type)
6945               or else Has_Discriminants (Parent_Type)
6946               or else (Present (Full_View (Parent_Type))
6947                         and then Has_Discriminants (Full_View (Parent_Type)))
6948             then
6949                return;
6950             end if;
6951
6952          when Concurrent_Kind =>
6953             Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
6954
6955          when others =>
6956             raise Program_Error;
6957       end case;
6958
6959       if Etype (Derived_Type) = Any_Type then
6960          return;
6961       end if;
6962
6963       --  Set delayed freeze and then derive subprograms, we need to do this
6964       --  in this order so that derived subprograms inherit the derived freeze
6965       --  if necessary.
6966
6967       Set_Has_Delayed_Freeze (Derived_Type);
6968       if Derive_Subps then
6969          Derive_Subprograms (Parent_Type, Derived_Type);
6970       end if;
6971
6972       Set_Has_Primitive_Operations
6973         (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
6974    end Build_Derived_Type;
6975
6976    -----------------------
6977    -- Build_Discriminal --
6978    -----------------------
6979
6980    procedure Build_Discriminal (Discrim : Entity_Id) is
6981       D_Minal : Entity_Id;
6982       CR_Disc : Entity_Id;
6983
6984    begin
6985       --  A discriminal has the same name as the discriminant
6986
6987       D_Minal :=
6988         Make_Defining_Identifier (Sloc (Discrim),
6989           Chars => Chars (Discrim));
6990
6991       Set_Ekind     (D_Minal, E_In_Parameter);
6992       Set_Mechanism (D_Minal, Default_Mechanism);
6993       Set_Etype     (D_Minal, Etype (Discrim));
6994
6995       Set_Discriminal (Discrim, D_Minal);
6996       Set_Discriminal_Link (D_Minal, Discrim);
6997
6998       --  For task types, build at once the discriminants of the corresponding
6999       --  record, which are needed if discriminants are used in entry defaults
7000       --  and in family bounds.
7001
7002       if Is_Concurrent_Type (Current_Scope)
7003         or else Is_Limited_Type (Current_Scope)
7004       then
7005          CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
7006
7007          Set_Ekind            (CR_Disc, E_In_Parameter);
7008          Set_Mechanism        (CR_Disc, Default_Mechanism);
7009          Set_Etype            (CR_Disc, Etype (Discrim));
7010          Set_Discriminal_Link (CR_Disc, Discrim);
7011          Set_CR_Discriminant  (Discrim, CR_Disc);
7012       end if;
7013    end Build_Discriminal;
7014
7015    ------------------------------------
7016    -- Build_Discriminant_Constraints --
7017    ------------------------------------
7018
7019    function Build_Discriminant_Constraints
7020      (T           : Entity_Id;
7021       Def         : Node_Id;
7022       Derived_Def : Boolean := False) return Elist_Id
7023    is
7024       C        : constant Node_Id := Constraint (Def);
7025       Nb_Discr : constant Nat     := Number_Discriminants (T);
7026
7027       Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
7028       --  Saves the expression corresponding to a given discriminant in T
7029
7030       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
7031       --  Return the Position number within array Discr_Expr of a discriminant
7032       --  D within the discriminant list of the discriminated type T.
7033
7034       ------------------
7035       -- Pos_Of_Discr --
7036       ------------------
7037
7038       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
7039          Disc : Entity_Id;
7040
7041       begin
7042          Disc := First_Discriminant (T);
7043          for J in Discr_Expr'Range loop
7044             if Disc = D then
7045                return J;
7046             end if;
7047
7048             Next_Discriminant (Disc);
7049          end loop;
7050
7051          --  Note: Since this function is called on discriminants that are
7052          --  known to belong to the discriminated type, falling through the
7053          --  loop with no match signals an internal compiler error.
7054
7055          raise Program_Error;
7056       end Pos_Of_Discr;
7057
7058       --  Declarations local to Build_Discriminant_Constraints
7059
7060       Discr : Entity_Id;
7061       E     : Entity_Id;
7062       Elist : constant Elist_Id := New_Elmt_List;
7063
7064       Constr   : Node_Id;
7065       Expr     : Node_Id;
7066       Id       : Node_Id;
7067       Position : Nat;
7068       Found    : Boolean;
7069
7070       Discrim_Present : Boolean := False;
7071
7072    --  Start of processing for Build_Discriminant_Constraints
7073
7074    begin
7075       --  The following loop will process positional associations only.
7076       --  For a positional association, the (single) discriminant is
7077       --  implicitly specified by position, in textual order (RM 3.7.2).
7078
7079       Discr  := First_Discriminant (T);
7080       Constr := First (Constraints (C));
7081       for D in Discr_Expr'Range loop
7082          exit when Nkind (Constr) = N_Discriminant_Association;
7083
7084          if No (Constr) then
7085             Error_Msg_N ("too few discriminants given in constraint", C);
7086             return New_Elmt_List;
7087
7088          elsif Nkind (Constr) = N_Range
7089            or else (Nkind (Constr) = N_Attribute_Reference
7090                      and then
7091                     Attribute_Name (Constr) = Name_Range)
7092          then
7093             Error_Msg_N
7094               ("a range is not a valid discriminant constraint", Constr);
7095             Discr_Expr (D) := Error;
7096
7097          else
7098             Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
7099             Discr_Expr (D) := Constr;
7100          end if;
7101
7102          Next_Discriminant (Discr);
7103          Next (Constr);
7104       end loop;
7105
7106       if No (Discr) and then Present (Constr) then
7107          Error_Msg_N ("too many discriminants given in constraint", Constr);
7108          return New_Elmt_List;
7109       end if;
7110
7111       --  Named associations can be given in any order, but if both positional
7112       --  and named associations are used in the same discriminant constraint,
7113       --  then positional associations must occur first, at their normal
7114       --  position. Hence once a named association is used, the rest of the
7115       --  discriminant constraint must use only named associations.
7116
7117       while Present (Constr) loop
7118
7119          --  Positional association forbidden after a named association
7120
7121          if Nkind (Constr) /= N_Discriminant_Association then
7122             Error_Msg_N ("positional association follows named one", Constr);
7123             return New_Elmt_List;
7124
7125          --  Otherwise it is a named association
7126
7127          else
7128             --  E records the type of the discriminants in the named
7129             --  association. All the discriminants specified in the same name
7130             --  association must have the same type.
7131
7132             E := Empty;
7133
7134             --  Search the list of discriminants in T to see if the simple name
7135             --  given in the constraint matches any of them.
7136
7137             Id := First (Selector_Names (Constr));
7138             while Present (Id) loop
7139                Found := False;
7140
7141                --  If Original_Discriminant is present, we are processing a
7142                --  generic instantiation and this is an instance node. We need
7143                --  to find the name of the corresponding discriminant in the
7144                --  actual record type T and not the name of the discriminant in
7145                --  the generic formal. Example:
7146
7147                --    generic
7148                --       type G (D : int) is private;
7149                --    package P is
7150                --       subtype W is G (D => 1);
7151                --    end package;
7152                --    type Rec (X : int) is record ... end record;
7153                --    package Q is new P (G => Rec);
7154
7155                --  At the point of the instantiation, formal type G is Rec
7156                --  and therefore when reanalyzing "subtype W is G (D => 1);"
7157                --  which really looks like "subtype W is Rec (D => 1);" at
7158                --  the point of instantiation, we want to find the discriminant
7159                --  that corresponds to D in Rec, ie X.
7160
7161                if Present (Original_Discriminant (Id)) then
7162                   Discr := Find_Corresponding_Discriminant (Id, T);
7163                   Found := True;
7164
7165                else
7166                   Discr := First_Discriminant (T);
7167                   while Present (Discr) loop
7168                      if Chars (Discr) = Chars (Id) then
7169                         Found := True;
7170                         exit;
7171                      end if;
7172
7173                      Next_Discriminant (Discr);
7174                   end loop;
7175
7176                   if not Found then
7177                      Error_Msg_N ("& does not match any discriminant", Id);
7178                      return New_Elmt_List;
7179
7180                   --  The following is only useful for the benefit of generic
7181                   --  instances but it does not interfere with other
7182                   --  processing for the non-generic case so we do it in all
7183                   --  cases (for generics this statement is executed when
7184                   --  processing the generic definition, see comment at the
7185                   --  beginning of this if statement).
7186
7187                   else
7188                      Set_Original_Discriminant (Id, Discr);
7189                   end if;
7190                end if;
7191
7192                Position := Pos_Of_Discr (T, Discr);
7193
7194                if Present (Discr_Expr (Position)) then
7195                   Error_Msg_N ("duplicate constraint for discriminant&", Id);
7196
7197                else
7198                   --  Each discriminant specified in the same named association
7199                   --  must be associated with a separate copy of the
7200                   --  corresponding expression.
7201
7202                   if Present (Next (Id)) then
7203                      Expr := New_Copy_Tree (Expression (Constr));
7204                      Set_Parent (Expr, Parent (Expression (Constr)));
7205                   else
7206                      Expr := Expression (Constr);
7207                   end if;
7208
7209                   Discr_Expr (Position) := Expr;
7210                   Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
7211                end if;
7212
7213                --  A discriminant association with more than one discriminant
7214                --  name is only allowed if the named discriminants are all of
7215                --  the same type (RM 3.7.1(8)).
7216
7217                if E = Empty then
7218                   E := Base_Type (Etype (Discr));
7219
7220                elsif Base_Type (Etype (Discr)) /= E then
7221                   Error_Msg_N
7222                     ("all discriminants in an association " &
7223                      "must have the same type", Id);
7224                end if;
7225
7226                Next (Id);
7227             end loop;
7228          end if;
7229
7230          Next (Constr);
7231       end loop;
7232
7233       --  A discriminant constraint must provide exactly one value for each
7234       --  discriminant of the type (RM 3.7.1(8)).
7235
7236       for J in Discr_Expr'Range loop
7237          if No (Discr_Expr (J)) then
7238             Error_Msg_N ("too few discriminants given in constraint", C);
7239             return New_Elmt_List;
7240          end if;
7241       end loop;
7242
7243       --  Determine if there are discriminant expressions in the constraint
7244
7245       for J in Discr_Expr'Range loop
7246          if Denotes_Discriminant
7247               (Discr_Expr (J), Check_Concurrent => True)
7248          then
7249             Discrim_Present := True;
7250          end if;
7251       end loop;
7252
7253       --  Build an element list consisting of the expressions given in the
7254       --  discriminant constraint and apply the appropriate checks. The list
7255       --  is constructed after resolving any named discriminant associations
7256       --  and therefore the expressions appear in the textual order of the
7257       --  discriminants.
7258
7259       Discr := First_Discriminant (T);
7260       for J in Discr_Expr'Range loop
7261          if Discr_Expr (J) /= Error then
7262             Append_Elmt (Discr_Expr (J), Elist);
7263
7264             --  If any of the discriminant constraints is given by a
7265             --  discriminant and we are in a derived type declaration we
7266             --  have a discriminant renaming. Establish link between new
7267             --  and old discriminant.
7268
7269             if Denotes_Discriminant (Discr_Expr (J)) then
7270                if Derived_Def then
7271                   Set_Corresponding_Discriminant
7272                     (Entity (Discr_Expr (J)), Discr);
7273                end if;
7274
7275             --  Force the evaluation of non-discriminant expressions.
7276             --  If we have found a discriminant in the constraint 3.4(26)
7277             --  and 3.8(18) demand that no range checks are performed are
7278             --  after evaluation. If the constraint is for a component
7279             --  definition that has a per-object constraint, expressions are
7280             --  evaluated but not checked either. In all other cases perform
7281             --  a range check.
7282
7283             else
7284                if Discrim_Present then
7285                   null;
7286
7287                elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
7288                  and then
7289                    Has_Per_Object_Constraint
7290                      (Defining_Identifier (Parent (Parent (Def))))
7291                then
7292                   null;
7293
7294                elsif Is_Access_Type (Etype (Discr)) then
7295                   Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
7296
7297                else
7298                   Apply_Range_Check (Discr_Expr (J), Etype (Discr));
7299                end if;
7300
7301                Force_Evaluation (Discr_Expr (J));
7302             end if;
7303
7304             --  Check that the designated type of an access discriminant's
7305             --  expression is not a class-wide type unless the discriminant's
7306             --  designated type is also class-wide.
7307
7308             if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
7309               and then not Is_Class_Wide_Type
7310                          (Designated_Type (Etype (Discr)))
7311               and then Etype (Discr_Expr (J)) /= Any_Type
7312               and then Is_Class_Wide_Type
7313                          (Designated_Type (Etype (Discr_Expr (J))))
7314             then
7315                Wrong_Type (Discr_Expr (J), Etype (Discr));
7316             end if;
7317          end if;
7318
7319          Next_Discriminant (Discr);
7320       end loop;
7321
7322       return Elist;
7323    end Build_Discriminant_Constraints;
7324
7325    ---------------------------------
7326    -- Build_Discriminated_Subtype --
7327    ---------------------------------
7328
7329    procedure Build_Discriminated_Subtype
7330      (T           : Entity_Id;
7331       Def_Id      : Entity_Id;
7332       Elist       : Elist_Id;
7333       Related_Nod : Node_Id;
7334       For_Access  : Boolean := False)
7335    is
7336       Has_Discrs  : constant Boolean := Has_Discriminants (T);
7337       Constrained : constant Boolean :=
7338                       (Has_Discrs
7339                          and then not Is_Empty_Elmt_List (Elist)
7340                          and then not Is_Class_Wide_Type (T))
7341                         or else Is_Constrained (T);
7342
7343    begin
7344       if Ekind (T) = E_Record_Type then
7345          if For_Access then
7346             Set_Ekind (Def_Id, E_Private_Subtype);
7347             Set_Is_For_Access_Subtype (Def_Id, True);
7348          else
7349             Set_Ekind (Def_Id, E_Record_Subtype);
7350          end if;
7351
7352       elsif Ekind (T) = E_Task_Type then
7353          Set_Ekind (Def_Id, E_Task_Subtype);
7354
7355       elsif Ekind (T) = E_Protected_Type then
7356          Set_Ekind (Def_Id, E_Protected_Subtype);
7357
7358       elsif Is_Private_Type (T) then
7359          Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
7360
7361       elsif Is_Class_Wide_Type (T) then
7362          Set_Ekind (Def_Id, E_Class_Wide_Subtype);
7363
7364       else
7365          --  Incomplete type. Attach subtype to list of dependents, to be
7366          --  completed with full view of parent type,  unless is it the
7367          --  designated subtype of a record component within an init_proc.
7368          --  This last case arises for a component of an access type whose
7369          --  designated type is incomplete (e.g. a Taft Amendment type).
7370          --  The designated subtype is within an inner scope, and needs no
7371          --  elaboration, because only the access type is needed in the
7372          --  initialization procedure.
7373
7374          Set_Ekind (Def_Id, Ekind (T));
7375
7376          if For_Access and then Within_Init_Proc then
7377             null;
7378          else
7379             Append_Elmt (Def_Id, Private_Dependents (T));
7380          end if;
7381       end if;
7382
7383       Set_Etype             (Def_Id, T);
7384       Init_Size_Align       (Def_Id);
7385       Set_Has_Discriminants (Def_Id, Has_Discrs);
7386       Set_Is_Constrained    (Def_Id, Constrained);
7387
7388       Set_First_Entity      (Def_Id, First_Entity   (T));
7389       Set_Last_Entity       (Def_Id, Last_Entity    (T));
7390       Set_First_Rep_Item    (Def_Id, First_Rep_Item (T));
7391
7392       if Is_Tagged_Type (T) then
7393          Set_Is_Tagged_Type  (Def_Id);
7394          Make_Class_Wide_Type (Def_Id);
7395       end if;
7396
7397       Set_Stored_Constraint (Def_Id, No_Elist);
7398
7399       if Has_Discrs then
7400          Set_Discriminant_Constraint (Def_Id, Elist);
7401          Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
7402       end if;
7403
7404       if Is_Tagged_Type (T) then
7405
7406          --  Ada 2005 (AI-251): In case of concurrent types we inherit the
7407          --  concurrent record type (which has the list of primitive
7408          --  operations).
7409
7410          if Ada_Version >= Ada_05
7411            and then Is_Concurrent_Type (T)
7412          then
7413             Set_Corresponding_Record_Type (Def_Id,
7414                Corresponding_Record_Type (T));
7415          else
7416             Set_Primitive_Operations (Def_Id, Primitive_Operations (T));
7417          end if;
7418
7419          Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
7420       end if;
7421
7422       --  Subtypes introduced by component declarations do not need to be
7423       --  marked as delayed, and do not get freeze nodes, because the semantics
7424       --  verifies that the parents of the subtypes are frozen before the
7425       --  enclosing record is frozen.
7426
7427       if not Is_Type (Scope (Def_Id)) then
7428          Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
7429
7430          if Is_Private_Type (T)
7431            and then Present (Full_View (T))
7432          then
7433             Conditional_Delay (Def_Id, Full_View (T));
7434          else
7435             Conditional_Delay (Def_Id, T);
7436          end if;
7437       end if;
7438
7439       if Is_Record_Type (T) then
7440          Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
7441
7442          if Has_Discrs
7443             and then not Is_Empty_Elmt_List (Elist)
7444             and then not For_Access
7445          then
7446             Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
7447          elsif not For_Access then
7448             Set_Cloned_Subtype (Def_Id, T);
7449          end if;
7450       end if;
7451    end Build_Discriminated_Subtype;
7452
7453    ---------------------------
7454    -- Build_Itype_Reference --
7455    ---------------------------
7456
7457    procedure Build_Itype_Reference
7458      (Ityp : Entity_Id;
7459       Nod  : Node_Id)
7460    is
7461       IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
7462    begin
7463       Set_Itype (IR, Ityp);
7464       Insert_After (Nod, IR);
7465    end Build_Itype_Reference;
7466
7467    ------------------------
7468    -- Build_Scalar_Bound --
7469    ------------------------
7470
7471    function Build_Scalar_Bound
7472      (Bound : Node_Id;
7473       Par_T : Entity_Id;
7474       Der_T : Entity_Id) return Node_Id
7475    is
7476       New_Bound : Entity_Id;
7477
7478    begin
7479       --  Note: not clear why this is needed, how can the original bound
7480       --  be unanalyzed at this point? and if it is, what business do we
7481       --  have messing around with it? and why is the base type of the
7482       --  parent type the right type for the resolution. It probably is
7483       --  not! It is OK for the new bound we are creating, but not for
7484       --  the old one??? Still if it never happens, no problem!
7485
7486       Analyze_And_Resolve (Bound, Base_Type (Par_T));
7487
7488       if Nkind (Bound) = N_Integer_Literal
7489         or else Nkind (Bound) = N_Real_Literal
7490       then
7491          New_Bound := New_Copy (Bound);
7492          Set_Etype (New_Bound, Der_T);
7493          Set_Analyzed (New_Bound);
7494
7495       elsif Is_Entity_Name (Bound) then
7496          New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
7497
7498       --  The following is almost certainly wrong. What business do we have
7499       --  relocating a node (Bound) that is presumably still attached to
7500       --  the tree elsewhere???
7501
7502       else
7503          New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
7504       end if;
7505
7506       Set_Etype (New_Bound, Der_T);
7507       return New_Bound;
7508    end Build_Scalar_Bound;
7509
7510    --------------------------------
7511    -- Build_Underlying_Full_View --
7512    --------------------------------
7513
7514    procedure Build_Underlying_Full_View
7515      (N   : Node_Id;
7516       Typ : Entity_Id;
7517       Par : Entity_Id)
7518    is
7519       Loc  : constant Source_Ptr := Sloc (N);
7520       Subt : constant Entity_Id :=
7521                Make_Defining_Identifier
7522                  (Loc, New_External_Name (Chars (Typ), 'S'));
7523
7524       Constr : Node_Id;
7525       Indic  : Node_Id;
7526       C      : Node_Id;
7527       Id     : Node_Id;
7528
7529       procedure Set_Discriminant_Name (Id : Node_Id);
7530       --  If the derived type has discriminants, they may rename discriminants
7531       --  of the parent. When building the full view of the parent, we need to
7532       --  recover the names of the original discriminants if the constraint is
7533       --  given by named associations.
7534
7535       ---------------------------
7536       -- Set_Discriminant_Name --
7537       ---------------------------
7538
7539       procedure Set_Discriminant_Name (Id : Node_Id) is
7540          Disc : Entity_Id;
7541
7542       begin
7543          Set_Original_Discriminant (Id, Empty);
7544
7545          if Has_Discriminants (Typ) then
7546             Disc := First_Discriminant (Typ);
7547             while Present (Disc) loop
7548                if Chars (Disc) = Chars (Id)
7549                  and then Present (Corresponding_Discriminant (Disc))
7550                then
7551                   Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
7552                end if;
7553                Next_Discriminant (Disc);
7554             end loop;
7555          end if;
7556       end Set_Discriminant_Name;
7557
7558    --  Start of processing for Build_Underlying_Full_View
7559
7560    begin
7561       if Nkind (N) = N_Full_Type_Declaration then
7562          Constr := Constraint (Subtype_Indication (Type_Definition (N)));
7563
7564       elsif Nkind (N) = N_Subtype_Declaration then
7565          Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
7566
7567       elsif Nkind (N) = N_Component_Declaration then
7568          Constr :=
7569            New_Copy_Tree
7570              (Constraint (Subtype_Indication (Component_Definition (N))));
7571
7572       else
7573          raise Program_Error;
7574       end if;
7575
7576       C := First (Constraints (Constr));
7577       while Present (C) loop
7578          if Nkind (C) = N_Discriminant_Association then
7579             Id := First (Selector_Names (C));
7580             while Present (Id) loop
7581                Set_Discriminant_Name (Id);
7582                Next (Id);
7583             end loop;
7584          end if;
7585
7586          Next (C);
7587       end loop;
7588
7589       Indic :=
7590         Make_Subtype_Declaration (Loc,
7591           Defining_Identifier => Subt,
7592           Subtype_Indication  =>
7593             Make_Subtype_Indication (Loc,
7594               Subtype_Mark => New_Reference_To (Par, Loc),
7595               Constraint   => New_Copy_Tree (Constr)));
7596
7597       --  If this is a component subtype for an outer itype, it is not
7598       --  a list member, so simply set the parent link for analysis: if
7599       --  the enclosing type does not need to be in a declarative list,
7600       --  neither do the components.
7601
7602       if Is_List_Member (N)
7603         and then Nkind (N) /= N_Component_Declaration
7604       then
7605          Insert_Before (N, Indic);
7606       else
7607          Set_Parent (Indic, Parent (N));
7608       end if;
7609
7610       Analyze (Indic);
7611       Set_Underlying_Full_View (Typ, Full_View (Subt));
7612    end Build_Underlying_Full_View;
7613
7614    -------------------------------
7615    -- Check_Abstract_Interfaces --
7616    -------------------------------
7617
7618    procedure Check_Abstract_Interfaces (N : Node_Id; Def : Node_Id) is
7619
7620       procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
7621       --  Local subprogram used to avoid code duplication. In case of error
7622       --  the message will be associated to Error_Node.
7623
7624       ------------------
7625       -- Check_Ifaces --
7626       ------------------
7627
7628       procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
7629       begin
7630          --  Ada 2005 (AI-345): Protected interfaces can only inherit from
7631          --  limited, synchronized or protected interfaces.
7632
7633          if Protected_Present (Def) then
7634             if Limited_Present (Iface_Def)
7635               or else Synchronized_Present (Iface_Def)
7636               or else Protected_Present (Iface_Def)
7637             then
7638                null;
7639
7640             elsif Task_Present (Iface_Def) then
7641                Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
7642                             & " from task interface", Error_Node);
7643
7644             else
7645                Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
7646                             & " from non-limited interface", Error_Node);
7647             end if;
7648
7649          --  Ada 2005 (AI-345): Synchronized interfaces can only inherit from
7650          --  limited and synchronized.
7651
7652          elsif Synchronized_Present (Def) then
7653             if Limited_Present (Iface_Def)
7654               or else Synchronized_Present (Iface_Def)
7655             then
7656                null;
7657
7658             elsif Protected_Present (Iface_Def) then
7659                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
7660                             & " from protected interface", Error_Node);
7661
7662             elsif Task_Present (Iface_Def) then
7663                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
7664                             & " from task interface", Error_Node);
7665
7666             else
7667                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
7668                             & " from non-limited interface", Error_Node);
7669             end if;
7670
7671          --  Ada 2005 (AI-345): Task interfaces can only inherit from limited,
7672          --  synchronized or task interfaces.
7673
7674          elsif Task_Present (Def) then
7675             if Limited_Present (Iface_Def)
7676               or else Synchronized_Present (Iface_Def)
7677               or else Task_Present (Iface_Def)
7678             then
7679                null;
7680
7681             elsif Protected_Present (Iface_Def) then
7682                Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
7683                             & " protected interface", Error_Node);
7684
7685             else
7686                Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
7687                             & " non-limited interface", Error_Node);
7688             end if;
7689          end if;
7690       end Check_Ifaces;
7691
7692       --  Local variables
7693
7694       Iface       : Node_Id;
7695       Iface_Def   : Node_Id;
7696       Iface_Typ   : Entity_Id;
7697       Parent_Node : Node_Id;
7698
7699    --  Start of processing for Check_Abstract_Interfaces
7700
7701    begin
7702       --  Why is this still unsupported???
7703
7704       if Nkind (N) = N_Private_Extension_Declaration then
7705          return;
7706       end if;
7707
7708       --  Check the parent in case of derivation of interface type
7709
7710       if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
7711         and then Is_Interface (Etype (Defining_Identifier (N)))
7712       then
7713          Parent_Node := Parent (Etype (Defining_Identifier (N)));
7714
7715          Check_Ifaces
7716            (Iface_Def  => Type_Definition (Parent_Node),
7717             Error_Node => Subtype_Indication (Type_Definition (N)));
7718       end if;
7719
7720       Iface := First (Interface_List (Def));
7721       while Present (Iface) loop
7722          Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
7723
7724          Parent_Node := Parent (Base_Type (Iface_Typ));
7725          Iface_Def   := Type_Definition (Parent_Node);
7726
7727          if not Is_Interface (Iface_Typ) then
7728             Error_Msg_NE ("(Ada 2005) & must be an interface",
7729                           Iface, Iface_Typ);
7730
7731          else
7732             --  "The declaration of a specific descendant of an interface
7733             --   type freezes the interface type" RM 13.14
7734
7735             Freeze_Before (N, Iface_Typ);
7736             Check_Ifaces (Iface_Def, Error_Node => Iface);
7737          end if;
7738
7739          Next (Iface);
7740       end loop;
7741    end Check_Abstract_Interfaces;
7742
7743    -------------------------------
7744    -- Check_Abstract_Overriding --
7745    -------------------------------
7746
7747    procedure Check_Abstract_Overriding (T : Entity_Id) is
7748       Alias_Subp : Entity_Id;
7749       Elmt       : Elmt_Id;
7750       Op_List    : Elist_Id;
7751       Subp       : Entity_Id;
7752       Type_Def   : Node_Id;
7753
7754    begin
7755       Op_List := Primitive_Operations (T);
7756
7757       --  Loop to check primitive operations
7758
7759       Elmt := First_Elmt (Op_List);
7760       while Present (Elmt) loop
7761          Subp := Node (Elmt);
7762          Alias_Subp := Alias (Subp);
7763
7764          --  Inherited subprograms are identified by the fact that they do not
7765          --  come from source, and the associated source location is the
7766          --  location of the first subtype of the derived type.
7767
7768          --  Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
7769          --  subprograms that "require overriding".
7770
7771          --  Special exception, do not complain about failure to override the
7772          --  stream routines _Input and _Output, as well as the primitive
7773          --  operations used in dispatching selects since we always provide
7774          --  automatic overridings for these subprograms.
7775
7776          --  Also ignore this rule for convention CIL since .NET libraries
7777          --  do bizarre things with interfaces???
7778
7779          --  The partial view of T may have been a private extension, for
7780          --  which inherited functions dispatching on result are abstract.
7781          --  If the full view is a null extension, there is no need for
7782          --  overriding in Ada2005, but wrappers need to be built for them
7783          --  (see exp_ch3, Build_Controlling_Function_Wrappers).
7784
7785          --  Use elseif here and avoid above goto???
7786
7787          if Is_Null_Extension (T)
7788            and then Has_Controlling_Result (Subp)
7789            and then Ada_Version >= Ada_05
7790            and then Present (Alias (Subp))
7791            and then not Comes_From_Source (Subp)
7792            and then not Is_Abstract_Subprogram (Alias (Subp))
7793          then
7794             goto Next_Subp;
7795          end if;
7796
7797          if (Is_Abstract_Subprogram (Subp)
7798               or else Requires_Overriding (Subp)
7799               or else (Has_Controlling_Result (Subp)
7800                         and then Present (Alias_Subp)
7801                         and then not Comes_From_Source (Subp)
7802                         and then Sloc (Subp) = Sloc (First_Subtype (T))))
7803            and then not Is_TSS (Subp, TSS_Stream_Input)
7804            and then not Is_TSS (Subp, TSS_Stream_Output)
7805            and then not Is_Abstract_Type (T)
7806            and then Convention (T) /= Convention_CIL
7807            and then Chars (Subp) /= Name_uDisp_Asynchronous_Select
7808            and then Chars (Subp) /= Name_uDisp_Conditional_Select
7809            and then Chars (Subp) /= Name_uDisp_Get_Prim_Op_Kind
7810            and then Chars (Subp) /= Name_uDisp_Timed_Select
7811
7812             --  Ada 2005 (AI-251): Do not consider hidden entities associated
7813             --  with abstract interface types because the check will be done
7814             --  with the aliased entity (otherwise we generate a duplicated
7815             --  error message).
7816
7817            and then not Present (Abstract_Interface_Alias (Subp))
7818          then
7819             if Present (Alias_Subp) then
7820
7821                --  Only perform the check for a derived subprogram when the
7822                --  type has an explicit record extension. This avoids
7823                --  incorrectly flagging abstract subprograms for the case of a
7824                --  type without an extension derived from a formal type with a
7825                --  tagged actual (can occur within a private part).
7826
7827                --  Ada 2005 (AI-391): In the case of an inherited function with
7828                --  a controlling result of the type, the rule does not apply if
7829                --  the type is a null extension (unless the parent function
7830                --  itself is abstract, in which case the function must still be
7831                --  be overridden). The expander will generate an overriding
7832                --  wrapper function calling the parent subprogram (see
7833                --  Exp_Ch3.Make_Controlling_Wrapper_Functions).
7834
7835                Type_Def := Type_Definition (Parent (T));
7836                if Nkind (Type_Def) = N_Derived_Type_Definition
7837                  and then Present (Record_Extension_Part (Type_Def))
7838                  and then
7839                    (Ada_Version < Ada_05
7840                       or else not Is_Null_Extension (T)
7841                       or else Ekind (Subp) = E_Procedure
7842                       or else not Has_Controlling_Result (Subp)
7843                       or else Is_Abstract_Subprogram (Alias_Subp)
7844                       or else Requires_Overriding (Subp)
7845                       or else Is_Access_Type (Etype (Subp)))
7846                then
7847                   Error_Msg_NE
7848                     ("type must be declared abstract or & overridden",
7849                      T, Subp);
7850
7851                   --  Traverse the whole chain of aliased subprograms to
7852                   --  complete the error notification. This is especially
7853                   --  useful for traceability of the chain of entities when the
7854                   --  subprogram corresponds with an interface subprogram
7855                   --  (which might be defined in another package)
7856
7857                   if Present (Alias_Subp) then
7858                      declare
7859                         E : Entity_Id;
7860
7861                      begin
7862                         E := Subp;
7863                         while Present (Alias (E)) loop
7864                            Error_Msg_Sloc := Sloc (E);
7865                            Error_Msg_NE ("\& has been inherited #", T, Subp);
7866                            E := Alias (E);
7867                         end loop;
7868
7869                         Error_Msg_Sloc := Sloc (E);
7870                         Error_Msg_NE
7871                           ("\& has been inherited from subprogram #", T, Subp);
7872                      end;
7873                   end if;
7874
7875                --  Ada 2005 (AI-345): Protected or task type implementing
7876                --  abstract interfaces.
7877
7878                elsif Is_Concurrent_Record_Type (T)
7879                  and then Present (Abstract_Interfaces (T))
7880                then
7881                   --  The controlling formal of Subp must be of mode "out",
7882                   --  "in out" or an access-to-variable to be overridden.
7883
7884                   --  Error message below needs rewording (remember comma
7885                   --  in -gnatj mode) ???
7886
7887                   if Ekind (First_Formal (Subp)) = E_In_Parameter then
7888                      Error_Msg_NE
7889                        ("first formal of & must be of mode `OUT`, `IN OUT` " &
7890                         "or access-to-variable", T, Subp);
7891                      Error_Msg_N
7892                        ("\to be overridden by protected procedure or " &
7893                         "entry (RM 9.4(11.9/2))", T);
7894
7895                   --  Some other kind of overriding failure
7896
7897                   else
7898                      Error_Msg_NE
7899                        ("interface subprogram & must be overridden",
7900                         T, Subp);
7901                   end if;
7902                end if;
7903
7904             else
7905                Error_Msg_Node_2 := T;
7906                Error_Msg_N
7907                  ("abstract subprogram& not allowed for type&", Subp);
7908
7909                --  Also post unconditional warning on the type (unconditional
7910                --  so that if there are more than one of these cases, we get
7911                --  them all, and not just the first one).
7912
7913                Error_Msg_Node_2 := Subp;
7914                Error_Msg_N
7915                  ("nonabstract type& has abstract subprogram&!", T);
7916             end if;
7917          end if;
7918
7919          <<Next_Subp>>
7920             Next_Elmt (Elmt);
7921       end loop;
7922    end Check_Abstract_Overriding;
7923
7924    ------------------------------------------------
7925    -- Check_Access_Discriminant_Requires_Limited --
7926    ------------------------------------------------
7927
7928    procedure Check_Access_Discriminant_Requires_Limited
7929      (D   : Node_Id;
7930       Loc : Node_Id)
7931    is
7932    begin
7933       --  A discriminant_specification for an access discriminant shall appear
7934       --  only in the declaration for a task or protected type, or for a type
7935       --  with the reserved word 'limited' in its definition or in one of its
7936       --  ancestors. (RM 3.7(10))
7937
7938       if Nkind (Discriminant_Type (D)) = N_Access_Definition
7939         and then not Is_Concurrent_Type (Current_Scope)
7940         and then not Is_Concurrent_Record_Type (Current_Scope)
7941         and then not Is_Limited_Record (Current_Scope)
7942         and then Ekind (Current_Scope) /= E_Limited_Private_Type
7943       then
7944          Error_Msg_N
7945            ("access discriminants allowed only for limited types", Loc);
7946       end if;
7947    end Check_Access_Discriminant_Requires_Limited;
7948
7949    -----------------------------------
7950    -- Check_Aliased_Component_Types --
7951    -----------------------------------
7952
7953    procedure Check_Aliased_Component_Types (T : Entity_Id) is
7954       C : Entity_Id;
7955
7956    begin
7957       --  ??? Also need to check components of record extensions, but not
7958       --  components of protected types (which are always limited).
7959
7960       --  Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
7961       --  types to be unconstrained. This is safe because it is illegal to
7962       --  create access subtypes to such types with explicit discriminant
7963       --  constraints.
7964
7965       if not Is_Limited_Type (T) then
7966          if Ekind (T) = E_Record_Type then
7967             C := First_Component (T);
7968             while Present (C) loop
7969                if Is_Aliased (C)
7970                  and then Has_Discriminants (Etype (C))
7971                  and then not Is_Constrained (Etype (C))
7972                  and then not In_Instance_Body
7973                  and then Ada_Version < Ada_05
7974                then
7975                   Error_Msg_N
7976                     ("aliased component must be constrained (RM 3.6(11))",
7977                       C);
7978                end if;
7979
7980                Next_Component (C);
7981             end loop;
7982
7983          elsif Ekind (T) = E_Array_Type then
7984             if Has_Aliased_Components (T)
7985               and then Has_Discriminants (Component_Type (T))
7986               and then not Is_Constrained (Component_Type (T))
7987               and then not In_Instance_Body
7988               and then Ada_Version < Ada_05
7989             then
7990                Error_Msg_N
7991                  ("aliased component type must be constrained (RM 3.6(11))",
7992                     T);
7993             end if;
7994          end if;
7995       end if;
7996    end Check_Aliased_Component_Types;
7997
7998    ----------------------
7999    -- Check_Completion --
8000    ----------------------
8001
8002    procedure Check_Completion (Body_Id : Node_Id := Empty) is
8003       E : Entity_Id;
8004
8005       procedure Post_Error;
8006       --  Post error message for lack of completion for entity E
8007
8008       ----------------
8009       -- Post_Error --
8010       ----------------
8011
8012       procedure Post_Error is
8013       begin
8014          if not Comes_From_Source (E) then
8015
8016             if Ekind (E) = E_Task_Type
8017               or else Ekind (E) = E_Protected_Type
8018             then
8019                --  It may be an anonymous protected type created for a
8020                --  single variable. Post error on variable, if present.
8021
8022                declare
8023                   Var : Entity_Id;
8024
8025                begin
8026                   Var := First_Entity (Current_Scope);
8027                   while Present (Var) loop
8028                      exit when Etype (Var) = E
8029                        and then Comes_From_Source (Var);
8030
8031                      Next_Entity (Var);
8032                   end loop;
8033
8034                   if Present (Var) then
8035                      E := Var;
8036                   end if;
8037                end;
8038             end if;
8039          end if;
8040
8041          --  If a generated entity has no completion, then either previous
8042          --  semantic errors have disabled the expansion phase, or else we had
8043          --  missing subunits, or else we are compiling without expansion,
8044          --  or else something is very wrong.
8045
8046          if not Comes_From_Source (E) then
8047             pragma Assert
8048               (Serious_Errors_Detected > 0
8049                 or else Configurable_Run_Time_Violations > 0
8050                 or else Subunits_Missing
8051                 or else not Expander_Active);
8052             return;
8053
8054          --  Here for source entity
8055
8056          else
8057             --  Here if no body to post the error message, so we post the error
8058             --  on the declaration that has no completion. This is not really
8059             --  the right place to post it, think about this later ???
8060
8061             if No (Body_Id) then
8062                if Is_Type (E) then
8063                   Error_Msg_NE
8064                     ("missing full declaration for }", Parent (E), E);
8065                else
8066                   Error_Msg_NE
8067                     ("missing body for &", Parent (E), E);
8068                end if;
8069
8070             --  Package body has no completion for a declaration that appears
8071             --  in the corresponding spec. Post error on the body, with a
8072             --  reference to the non-completed declaration.
8073
8074             else
8075                Error_Msg_Sloc := Sloc (E);
8076
8077                if Is_Type (E) then
8078                   Error_Msg_NE
8079                     ("missing full declaration for }!", Body_Id, E);
8080
8081                elsif Is_Overloadable (E)
8082                  and then Current_Entity_In_Scope (E) /= E
8083                then
8084                   --  It may be that the completion is mistyped and appears
8085                   --  as a  distinct overloading of the entity.
8086
8087                   declare
8088                      Candidate : constant Entity_Id :=
8089                                    Current_Entity_In_Scope (E);
8090                      Decl      : constant Node_Id :=
8091                                    Unit_Declaration_Node (Candidate);
8092
8093                   begin
8094                      if Is_Overloadable (Candidate)
8095                        and then Ekind (Candidate) = Ekind (E)
8096                        and then Nkind (Decl) = N_Subprogram_Body
8097                        and then Acts_As_Spec (Decl)
8098                      then
8099                         Check_Type_Conformant (Candidate, E);
8100
8101                      else
8102                         Error_Msg_NE ("missing body for & declared#!",
8103                            Body_Id, E);
8104                      end if;
8105                   end;
8106                else
8107                   Error_Msg_NE ("missing body for & declared#!",
8108                      Body_Id, E);
8109                end if;
8110             end if;
8111          end if;
8112       end Post_Error;
8113
8114    --  Start processing for Check_Completion
8115
8116    begin
8117       E := First_Entity (Current_Scope);
8118       while Present (E) loop
8119          if Is_Intrinsic_Subprogram (E) then
8120             null;
8121
8122          --  The following situation requires special handling: a child
8123          --  unit that appears in the context clause of the body of its
8124          --  parent:
8125
8126          --    procedure Parent.Child (...);
8127
8128          --    with Parent.Child;
8129          --    package body Parent is
8130
8131          --  Here Parent.Child appears as a local entity, but should not
8132          --  be flagged as requiring completion, because it is a
8133          --  compilation unit.
8134
8135          --  Ignore missing completion for a subprogram that does not come from
8136          --  source (including the _Call primitive operation of RAS types,
8137          --  which has to have the flag Comes_From_Source for other purposes):
8138          --  we assume that the expander will provide the missing completion.
8139
8140          elsif     Ekind (E) = E_Function
8141            or else Ekind (E) = E_Procedure
8142            or else Ekind (E) = E_Generic_Function
8143            or else Ekind (E) = E_Generic_Procedure
8144          then
8145             if not Has_Completion (E)
8146               and then not (Is_Subprogram (E)
8147                             and then Is_Abstract_Subprogram (E))
8148               and then not (Is_Subprogram (E)
8149                               and then
8150                             (not Comes_From_Source (E)
8151                               or else Chars (E) = Name_uCall))
8152               and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8153                                                        N_Compilation_Unit
8154               and then Chars (E) /= Name_uSize
8155             then
8156                Post_Error;
8157             end if;
8158
8159          elsif Is_Entry (E) then
8160             if not Has_Completion (E) and then
8161               (Ekind (Scope (E)) = E_Protected_Object
8162                 or else Ekind (Scope (E)) = E_Protected_Type)
8163             then
8164                Post_Error;
8165             end if;
8166
8167          elsif Is_Package_Or_Generic_Package (E) then
8168             if Unit_Requires_Body (E) then
8169                if not Has_Completion (E)
8170                  and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8171                                                        N_Compilation_Unit
8172                then
8173                   Post_Error;
8174                end if;
8175
8176             elsif not Is_Child_Unit (E) then
8177                May_Need_Implicit_Body (E);
8178             end if;
8179
8180          elsif Ekind (E) = E_Incomplete_Type
8181            and then No (Underlying_Type (E))
8182          then
8183             Post_Error;
8184
8185          elsif (Ekind (E) = E_Task_Type or else
8186                 Ekind (E) = E_Protected_Type)
8187            and then not Has_Completion (E)
8188          then
8189             Post_Error;
8190
8191          --  A single task declared in the current scope is a constant, verify
8192          --  that the body of its anonymous type is in the same scope. If the
8193          --  task is defined elsewhere, this may be a renaming declaration for
8194          --  which no completion is needed.
8195
8196          elsif Ekind (E) = E_Constant
8197            and then Ekind (Etype (E)) = E_Task_Type
8198            and then not Has_Completion (Etype (E))
8199            and then Scope (Etype (E)) = Current_Scope
8200          then
8201             Post_Error;
8202
8203          elsif Ekind (E) = E_Protected_Object
8204            and then not Has_Completion (Etype (E))
8205          then
8206             Post_Error;
8207
8208          elsif Ekind (E) = E_Record_Type then
8209             if Is_Tagged_Type (E) then
8210                Check_Abstract_Overriding (E);
8211                Check_Conventions (E);
8212             end if;
8213
8214             Check_Aliased_Component_Types (E);
8215
8216          elsif Ekind (E) = E_Array_Type then
8217             Check_Aliased_Component_Types (E);
8218
8219          end if;
8220
8221          Next_Entity (E);
8222       end loop;
8223    end Check_Completion;
8224
8225    ----------------------------
8226    -- Check_Delta_Expression --
8227    ----------------------------
8228
8229    procedure Check_Delta_Expression (E : Node_Id) is
8230    begin
8231       if not (Is_Real_Type (Etype (E))) then
8232          Wrong_Type (E, Any_Real);
8233
8234       elsif not Is_OK_Static_Expression (E) then
8235          Flag_Non_Static_Expr
8236            ("non-static expression used for delta value!", E);
8237
8238       elsif not UR_Is_Positive (Expr_Value_R (E)) then
8239          Error_Msg_N ("delta expression must be positive", E);
8240
8241       else
8242          return;
8243       end if;
8244
8245       --  If any of above errors occurred, then replace the incorrect
8246       --  expression by the real 0.1, which should prevent further errors.
8247
8248       Rewrite (E,
8249         Make_Real_Literal (Sloc (E), Ureal_Tenth));
8250       Analyze_And_Resolve (E, Standard_Float);
8251    end Check_Delta_Expression;
8252
8253    -----------------------------
8254    -- Check_Digits_Expression --
8255    -----------------------------
8256
8257    procedure Check_Digits_Expression (E : Node_Id) is
8258    begin
8259       if not (Is_Integer_Type (Etype (E))) then
8260          Wrong_Type (E, Any_Integer);
8261
8262       elsif not Is_OK_Static_Expression (E) then
8263          Flag_Non_Static_Expr
8264            ("non-static expression used for digits value!", E);
8265
8266       elsif Expr_Value (E) <= 0 then
8267          Error_Msg_N ("digits value must be greater than zero", E);
8268
8269       else
8270          return;
8271       end if;
8272
8273       --  If any of above errors occurred, then replace the incorrect
8274       --  expression by the integer 1, which should prevent further errors.
8275
8276       Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
8277       Analyze_And_Resolve (E, Standard_Integer);
8278
8279    end Check_Digits_Expression;
8280
8281    --------------------------
8282    -- Check_Initialization --
8283    --------------------------
8284
8285    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
8286    begin
8287       if Is_Limited_Type (T)
8288         and then not In_Instance
8289         and then not In_Inlined_Body
8290       then
8291          if not OK_For_Limited_Init (Exp) then
8292             --  In GNAT mode, this is just a warning, to allow it to be
8293             --  evilly turned off. Otherwise it is a real error.
8294
8295             if GNAT_Mode then
8296                Error_Msg_N
8297                  ("cannot initialize entities of limited type?", Exp);
8298             else
8299                Error_Msg_N
8300                  ("cannot initialize entities of limited type", Exp);
8301                Explain_Limited_Type (T, Exp);
8302             end if;
8303          end if;
8304       end if;
8305    end Check_Initialization;
8306
8307    ------------------------------------
8308    -- Check_Or_Process_Discriminants --
8309    ------------------------------------
8310
8311    --  If an incomplete or private type declaration was already given for the
8312    --  type, the discriminants may have already been processed if they were
8313    --  present on the incomplete declaration. In this case a full conformance
8314    --  check is performed otherwise just process them.
8315
8316    procedure Check_Or_Process_Discriminants
8317      (N    : Node_Id;
8318       T    : Entity_Id;
8319       Prev : Entity_Id := Empty)
8320    is
8321    begin
8322       if Has_Discriminants (T) then
8323
8324          --  Make the discriminants visible to component declarations
8325
8326          declare
8327             D    : Entity_Id;
8328             Prev : Entity_Id;
8329
8330          begin
8331             D := First_Discriminant (T);
8332             while Present (D) loop
8333                Prev := Current_Entity (D);
8334                Set_Current_Entity (D);
8335                Set_Is_Immediately_Visible (D);
8336                Set_Homonym (D, Prev);
8337
8338                --  Ada 2005 (AI-230): Access discriminant allowed in
8339                --  non-limited record types.
8340
8341                if Ada_Version < Ada_05 then
8342
8343                   --  This restriction gets applied to the full type here. It
8344                   --  has already been applied earlier to the partial view.
8345
8346                   Check_Access_Discriminant_Requires_Limited (Parent (D), N);
8347                end if;
8348
8349                Next_Discriminant (D);
8350             end loop;
8351          end;
8352
8353       elsif Present (Discriminant_Specifications (N)) then
8354          Process_Discriminants (N, Prev);
8355       end if;
8356    end Check_Or_Process_Discriminants;
8357
8358    ----------------------
8359    -- Check_Real_Bound --
8360    ----------------------
8361
8362    procedure Check_Real_Bound (Bound : Node_Id) is
8363    begin
8364       if not Is_Real_Type (Etype (Bound)) then
8365          Error_Msg_N
8366            ("bound in real type definition must be of real type", Bound);
8367
8368       elsif not Is_OK_Static_Expression (Bound) then
8369          Flag_Non_Static_Expr
8370            ("non-static expression used for real type bound!", Bound);
8371
8372       else
8373          return;
8374       end if;
8375
8376       Rewrite
8377         (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
8378       Analyze (Bound);
8379       Resolve (Bound, Standard_Float);
8380    end Check_Real_Bound;
8381
8382    ------------------------------
8383    -- Complete_Private_Subtype --
8384    ------------------------------
8385
8386    procedure Complete_Private_Subtype
8387      (Priv        : Entity_Id;
8388       Full        : Entity_Id;
8389       Full_Base   : Entity_Id;
8390       Related_Nod : Node_Id)
8391    is
8392       Save_Next_Entity : Entity_Id;
8393       Save_Homonym     : Entity_Id;
8394
8395    begin
8396       --  Set semantic attributes for (implicit) private subtype completion.
8397       --  If the full type has no discriminants, then it is a copy of the full
8398       --  view of the base. Otherwise, it is a subtype of the base with a
8399       --  possible discriminant constraint. Save and restore the original
8400       --  Next_Entity field of full to ensure that the calls to Copy_Node
8401       --  do not corrupt the entity chain.
8402
8403       --  Note that the type of the full view is the same entity as the type of
8404       --  the partial view. In this fashion, the subtype has access to the
8405       --  correct view of the parent.
8406
8407       Save_Next_Entity := Next_Entity (Full);
8408       Save_Homonym     := Homonym (Priv);
8409
8410       case Ekind (Full_Base) is
8411          when E_Record_Type    |
8412               E_Record_Subtype |
8413               Class_Wide_Kind  |
8414               Private_Kind     |
8415               Task_Kind        |
8416               Protected_Kind   =>
8417             Copy_Node (Priv, Full);
8418
8419             Set_Has_Discriminants  (Full, Has_Discriminants (Full_Base));
8420             Set_First_Entity       (Full, First_Entity (Full_Base));
8421             Set_Last_Entity        (Full, Last_Entity (Full_Base));
8422
8423          when others =>
8424             Copy_Node (Full_Base, Full);
8425             Set_Chars          (Full, Chars (Priv));
8426             Conditional_Delay  (Full, Priv);
8427             Set_Sloc           (Full, Sloc (Priv));
8428       end case;
8429
8430       Set_Next_Entity (Full, Save_Next_Entity);
8431       Set_Homonym     (Full, Save_Homonym);
8432       Set_Associated_Node_For_Itype (Full, Related_Nod);
8433
8434       --  Set common attributes for all subtypes
8435
8436       Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
8437
8438       --  The Etype of the full view is inconsistent. Gigi needs to see the
8439       --  structural full view,  which is what the current scheme gives:
8440       --  the Etype of the full view is the etype of the full base. However,
8441       --  if the full base is a derived type, the full view then looks like
8442       --  a subtype of the parent, not a subtype of the full base. If instead
8443       --  we write:
8444
8445       --       Set_Etype (Full, Full_Base);
8446
8447       --  then we get inconsistencies in the front-end (confusion between
8448       --  views). Several outstanding bugs are related to this ???
8449
8450       Set_Is_First_Subtype (Full, False);
8451       Set_Scope            (Full, Scope (Priv));
8452       Set_Size_Info        (Full, Full_Base);
8453       Set_RM_Size          (Full, RM_Size (Full_Base));
8454       Set_Is_Itype         (Full);
8455
8456       --  A subtype of a private-type-without-discriminants, whose full-view
8457       --  has discriminants with default expressions, is not constrained!
8458
8459       if not Has_Discriminants (Priv) then
8460          Set_Is_Constrained (Full, Is_Constrained (Full_Base));
8461
8462          if Has_Discriminants (Full_Base) then
8463             Set_Discriminant_Constraint
8464               (Full, Discriminant_Constraint (Full_Base));
8465
8466             --  The partial view may have been indefinite, the full view
8467             --  might not be.
8468
8469             Set_Has_Unknown_Discriminants
8470               (Full, Has_Unknown_Discriminants (Full_Base));
8471          end if;
8472       end if;
8473
8474       Set_First_Rep_Item     (Full, First_Rep_Item (Full_Base));
8475       Set_Depends_On_Private (Full, Has_Private_Component (Full));
8476
8477       --  Freeze the private subtype entity if its parent is delayed, and not
8478       --  already frozen. We skip this processing if the type is an anonymous
8479       --  subtype of a record component, or is the corresponding record of a
8480       --  protected type, since ???
8481
8482       if not Is_Type (Scope (Full)) then
8483          Set_Has_Delayed_Freeze (Full,
8484            Has_Delayed_Freeze (Full_Base)
8485              and then (not Is_Frozen (Full_Base)));
8486       end if;
8487
8488       Set_Freeze_Node (Full, Empty);
8489       Set_Is_Frozen (Full, False);
8490       Set_Full_View (Priv, Full);
8491
8492       if Has_Discriminants (Full) then
8493          Set_Stored_Constraint_From_Discriminant_Constraint (Full);
8494          Set_Stored_Constraint (Priv, Stored_Constraint (Full));
8495
8496          if Has_Unknown_Discriminants (Full) then
8497             Set_Discriminant_Constraint (Full, No_Elist);
8498          end if;
8499       end if;
8500
8501       if Ekind (Full_Base) = E_Record_Type
8502         and then Has_Discriminants (Full_Base)
8503         and then Has_Discriminants (Priv) -- might not, if errors
8504         and then not Has_Unknown_Discriminants (Priv)
8505         and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
8506       then
8507          Create_Constrained_Components
8508            (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
8509
8510       --  If the full base is itself derived from private, build a congruent
8511       --  subtype of its underlying type, for use by the back end. For a
8512       --  constrained record component, the declaration cannot be placed on
8513       --  the component list, but it must nevertheless be built an analyzed, to
8514       --  supply enough information for Gigi to compute the size of component.
8515
8516       elsif Ekind (Full_Base) in Private_Kind
8517         and then Is_Derived_Type (Full_Base)
8518         and then Has_Discriminants (Full_Base)
8519         and then (Ekind (Current_Scope) /= E_Record_Subtype)
8520       then
8521          if not Is_Itype (Priv)
8522            and then
8523              Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
8524          then
8525             Build_Underlying_Full_View
8526               (Parent (Priv), Full, Etype (Full_Base));
8527
8528          elsif Nkind (Related_Nod) = N_Component_Declaration then
8529             Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
8530          end if;
8531
8532       elsif Is_Record_Type (Full_Base) then
8533
8534          --  Show Full is simply a renaming of Full_Base
8535
8536          Set_Cloned_Subtype (Full, Full_Base);
8537       end if;
8538
8539       --  It is unsafe to share to bounds of a scalar type, because the Itype
8540       --  is elaborated on demand, and if a bound is non-static then different
8541       --  orders of elaboration in different units will lead to different
8542       --  external symbols.
8543
8544       if Is_Scalar_Type (Full_Base) then
8545          Set_Scalar_Range (Full,
8546            Make_Range (Sloc (Related_Nod),
8547              Low_Bound  =>
8548                Duplicate_Subexpr_No_Checks (Type_Low_Bound  (Full_Base)),
8549              High_Bound =>
8550                Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
8551
8552          --  This completion inherits the bounds of the full parent, but if
8553          --  the parent is an unconstrained floating point type, so is the
8554          --  completion.
8555
8556          if Is_Floating_Point_Type (Full_Base) then
8557             Set_Includes_Infinities
8558              (Scalar_Range (Full), Has_Infinities (Full_Base));
8559          end if;
8560       end if;
8561
8562       --  ??? It seems that a lot of fields are missing that should be copied
8563       --  from Full_Base to Full. Here are some that are introduced in a
8564       --  non-disruptive way but a cleanup is necessary.
8565
8566       if Is_Tagged_Type (Full_Base) then
8567          Set_Is_Tagged_Type (Full);
8568          Set_Primitive_Operations (Full, Primitive_Operations (Full_Base));
8569          Set_Class_Wide_Type      (Full, Class_Wide_Type (Full_Base));
8570
8571       --  If this is a subtype of a protected or task type, constrain its
8572       --  corresponding record, unless this is a subtype without constraints,
8573       --  i.e. a simple renaming as with an actual subtype in an instance.
8574
8575       elsif Is_Concurrent_Type (Full_Base) then
8576          if Has_Discriminants (Full)
8577            and then Present (Corresponding_Record_Type (Full_Base))
8578            and then
8579              not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
8580          then
8581             Set_Corresponding_Record_Type (Full,
8582               Constrain_Corresponding_Record
8583                 (Full, Corresponding_Record_Type (Full_Base),
8584                   Related_Nod, Full_Base));
8585
8586          else
8587             Set_Corresponding_Record_Type (Full,
8588               Corresponding_Record_Type (Full_Base));
8589          end if;
8590       end if;
8591    end Complete_Private_Subtype;
8592
8593    ----------------------------
8594    -- Constant_Redeclaration --
8595    ----------------------------
8596
8597    procedure Constant_Redeclaration
8598      (Id : Entity_Id;
8599       N  : Node_Id;
8600       T  : out Entity_Id)
8601    is
8602       Prev    : constant Entity_Id := Current_Entity_In_Scope (Id);
8603       Obj_Def : constant Node_Id := Object_Definition (N);
8604       New_T   : Entity_Id;
8605
8606       procedure Check_Possible_Deferred_Completion
8607         (Prev_Id      : Entity_Id;
8608          Prev_Obj_Def : Node_Id;
8609          Curr_Obj_Def : Node_Id);
8610       --  Determine whether the two object definitions describe the partial
8611       --  and the full view of a constrained deferred constant. Generate
8612       --  a subtype for the full view and verify that it statically matches
8613       --  the subtype of the partial view.
8614
8615       procedure Check_Recursive_Declaration (Typ : Entity_Id);
8616       --  If deferred constant is an access type initialized with an allocator,
8617       --  check whether there is an illegal recursion in the definition,
8618       --  through a default value of some record subcomponent. This is normally
8619       --  detected when generating init procs, but requires this additional
8620       --  mechanism when expansion is disabled.
8621
8622       ----------------------------------------
8623       -- Check_Possible_Deferred_Completion --
8624       ----------------------------------------
8625
8626       procedure Check_Possible_Deferred_Completion
8627         (Prev_Id      : Entity_Id;
8628          Prev_Obj_Def : Node_Id;
8629          Curr_Obj_Def : Node_Id)
8630       is
8631       begin
8632          if Nkind (Prev_Obj_Def) = N_Subtype_Indication
8633            and then Present (Constraint (Prev_Obj_Def))
8634            and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
8635            and then Present (Constraint (Curr_Obj_Def))
8636          then
8637             declare
8638                Loc    : constant Source_Ptr := Sloc (N);
8639                Def_Id : constant Entity_Id :=
8640                           Make_Defining_Identifier (Loc,
8641                             New_Internal_Name ('S'));
8642                Decl   : constant Node_Id :=
8643                           Make_Subtype_Declaration (Loc,
8644                             Defining_Identifier =>
8645                               Def_Id,
8646                             Subtype_Indication =>
8647                               Relocate_Node (Curr_Obj_Def));
8648
8649             begin
8650                Insert_Before_And_Analyze (N, Decl);
8651                Set_Etype (Id, Def_Id);
8652
8653                if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
8654                   Error_Msg_Sloc := Sloc (Prev_Id);
8655                   Error_Msg_N ("subtype does not statically match deferred " &
8656                                "declaration#", N);
8657                end if;
8658             end;
8659          end if;
8660       end Check_Possible_Deferred_Completion;
8661
8662       ---------------------------------
8663       -- Check_Recursive_Declaration --
8664       ---------------------------------
8665
8666       procedure Check_Recursive_Declaration (Typ : Entity_Id) is
8667          Comp : Entity_Id;
8668
8669       begin
8670          if Is_Record_Type (Typ) then
8671             Comp := First_Component (Typ);
8672             while Present (Comp) loop
8673                if Comes_From_Source (Comp) then
8674                   if Present (Expression (Parent (Comp)))
8675                     and then Is_Entity_Name (Expression (Parent (Comp)))
8676                     and then Entity (Expression (Parent (Comp))) = Prev
8677                   then
8678                      Error_Msg_Sloc := Sloc (Parent (Comp));
8679                      Error_Msg_NE
8680                        ("illegal circularity with declaration for&#",
8681                          N, Comp);
8682                      return;
8683
8684                   elsif Is_Record_Type (Etype (Comp)) then
8685                      Check_Recursive_Declaration (Etype (Comp));
8686                   end if;
8687                end if;
8688
8689                Next_Component (Comp);
8690             end loop;
8691          end if;
8692       end Check_Recursive_Declaration;
8693
8694    --  Start of processing for Constant_Redeclaration
8695
8696    begin
8697       if Nkind (Parent (Prev)) = N_Object_Declaration then
8698          if Nkind (Object_Definition
8699                      (Parent (Prev))) = N_Subtype_Indication
8700          then
8701             --  Find type of new declaration. The constraints of the two
8702             --  views must match statically, but there is no point in
8703             --  creating an itype for the full view.
8704
8705             if Nkind (Obj_Def) = N_Subtype_Indication then
8706                Find_Type (Subtype_Mark (Obj_Def));
8707                New_T := Entity (Subtype_Mark (Obj_Def));
8708
8709             else
8710                Find_Type (Obj_Def);
8711                New_T := Entity (Obj_Def);
8712             end if;
8713
8714             T := Etype (Prev);
8715
8716          else
8717             --  The full view may impose a constraint, even if the partial
8718             --  view does not, so construct the subtype.
8719
8720             New_T := Find_Type_Of_Object (Obj_Def, N);
8721             T     := New_T;
8722          end if;
8723
8724       else
8725          --  Current declaration is illegal, diagnosed below in Enter_Name
8726
8727          T := Empty;
8728          New_T := Any_Type;
8729       end if;
8730
8731       --  If previous full declaration exists, or if a homograph is present,
8732       --  let Enter_Name handle it, either with an error, or with the removal
8733       --  of an overridden implicit subprogram.
8734
8735       if Ekind (Prev) /= E_Constant
8736         or else Present (Expression (Parent (Prev)))
8737         or else Present (Full_View (Prev))
8738       then
8739          Enter_Name (Id);
8740
8741       --  Verify that types of both declarations match, or else that both types
8742       --  are anonymous access types whose designated subtypes statically match
8743       --  (as allowed in Ada 2005 by AI-385).
8744
8745       elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
8746         and then
8747           (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
8748              or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
8749              or else not Subtypes_Statically_Match
8750                            (Designated_Type (Etype (Prev)),
8751                             Designated_Type (Etype (New_T))))
8752       then
8753          Error_Msg_Sloc := Sloc (Prev);
8754          Error_Msg_N ("type does not match declaration#", N);
8755          Set_Full_View (Prev, Id);
8756          Set_Etype (Id, Any_Type);
8757
8758       --  If so, process the full constant declaration
8759
8760       else
8761          --  RM 7.4 (6): If the subtype defined by the subtype_indication in
8762          --  the deferred declaration is constrained, then the subtype defined
8763          --  by the subtype_indication in the full declaration shall match it
8764          --  statically.
8765
8766          Check_Possible_Deferred_Completion
8767            (Prev_Id      => Prev,
8768             Prev_Obj_Def => Object_Definition (Parent (Prev)),
8769             Curr_Obj_Def => Obj_Def);
8770
8771          Set_Full_View (Prev, Id);
8772          Set_Is_Public (Id, Is_Public (Prev));
8773          Set_Is_Internal (Id);
8774          Append_Entity (Id, Current_Scope);
8775
8776          --  Check ALIASED present if present before (RM 7.4(7))
8777
8778          if Is_Aliased (Prev)
8779            and then not Aliased_Present (N)
8780          then
8781             Error_Msg_Sloc := Sloc (Prev);
8782             Error_Msg_N ("ALIASED required (see declaration#)", N);
8783          end if;
8784
8785          --  Allow incomplete declaration of tags (used to handle forward
8786          --  references to tags). The check on Ada_Tags avoids cicularities
8787          --  when rebuilding the compiler.
8788
8789          if RTU_Loaded (Ada_Tags)
8790            and then T = RTE (RE_Tag)
8791          then
8792             null;
8793
8794          --  Check that placement is in private part and that the incomplete
8795          --  declaration appeared in the visible part.
8796
8797          elsif Ekind (Current_Scope) = E_Package
8798            and then not In_Private_Part (Current_Scope)
8799          then
8800             Error_Msg_Sloc := Sloc (Prev);
8801             Error_Msg_N ("full constant for declaration#"
8802                          & " must be in private part", N);
8803
8804          elsif Ekind (Current_Scope) = E_Package
8805            and then List_Containing (Parent (Prev))
8806            /= Visible_Declarations
8807              (Specification (Unit_Declaration_Node (Current_Scope)))
8808          then
8809             Error_Msg_N
8810               ("deferred constant must be declared in visible part",
8811                  Parent (Prev));
8812          end if;
8813
8814          if Is_Access_Type (T)
8815            and then Nkind (Expression (N)) = N_Allocator
8816          then
8817             Check_Recursive_Declaration (Designated_Type (T));
8818          end if;
8819       end if;
8820    end Constant_Redeclaration;
8821
8822    ----------------------
8823    -- Constrain_Access --
8824    ----------------------
8825
8826    procedure Constrain_Access
8827      (Def_Id      : in out Entity_Id;
8828       S           : Node_Id;
8829       Related_Nod : Node_Id)
8830    is
8831       T             : constant Entity_Id := Entity (Subtype_Mark (S));
8832       Desig_Type    : constant Entity_Id := Designated_Type (T);
8833       Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
8834       Constraint_OK : Boolean := True;
8835
8836       function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
8837       --  Simple predicate to test for defaulted discriminants
8838       --  Shouldn't this be in sem_util???
8839
8840       ---------------------------------
8841       -- Has_Defaulted_Discriminants --
8842       ---------------------------------
8843
8844       function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
8845       begin
8846          return Has_Discriminants (Typ)
8847           and then Present (First_Discriminant (Typ))
8848           and then Present
8849             (Discriminant_Default_Value (First_Discriminant (Typ)));
8850       end Has_Defaulted_Discriminants;
8851
8852    --  Start of processing for Constrain_Access
8853
8854    begin
8855       if Is_Array_Type (Desig_Type) then
8856          Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
8857
8858       elsif (Is_Record_Type (Desig_Type)
8859               or else Is_Incomplete_Or_Private_Type (Desig_Type))
8860         and then not Is_Constrained (Desig_Type)
8861       then
8862          --  ??? The following code is a temporary kludge to ignore a
8863          --  discriminant constraint on access type if it is constraining
8864          --  the current record. Avoid creating the implicit subtype of the
8865          --  record we are currently compiling since right now, we cannot
8866          --  handle these. For now, just return the access type itself.
8867
8868          if Desig_Type = Current_Scope
8869            and then No (Def_Id)
8870          then
8871             Set_Ekind (Desig_Subtype, E_Record_Subtype);
8872             Def_Id := Entity (Subtype_Mark (S));
8873
8874             --  This call added to ensure that the constraint is analyzed
8875             --  (needed for a B test). Note that we still return early from
8876             --  this procedure to avoid recursive processing. ???
8877
8878             Constrain_Discriminated_Type
8879               (Desig_Subtype, S, Related_Nod, For_Access => True);
8880             return;
8881          end if;
8882
8883          if Ekind (T) = E_General_Access_Type
8884            and then Has_Private_Declaration (Desig_Type)
8885            and then In_Open_Scopes (Scope (Desig_Type))
8886          then
8887             --  Enforce rule that the constraint is illegal if there is
8888             --  an unconstrained view of the designated type. This means
8889             --  that the partial view (either a private type declaration or
8890             --  a derivation from a private type) has no discriminants.
8891             --  (Defect Report 8652/0008, Technical Corrigendum 1, checked
8892             --  by ACATS B371001).
8893
8894             --  Rule updated for Ada 2005: the private type is said to have
8895             --  a constrained partial view, given that objects of the type
8896             --  can be declared.
8897
8898             declare
8899                Pack  : constant Node_Id :=
8900                          Unit_Declaration_Node (Scope (Desig_Type));
8901                Decls : List_Id;
8902                Decl  : Node_Id;
8903
8904             begin
8905                if Nkind (Pack) = N_Package_Declaration then
8906                   Decls := Visible_Declarations (Specification (Pack));
8907                   Decl := First (Decls);
8908                   while Present (Decl) loop
8909                      if (Nkind (Decl) = N_Private_Type_Declaration
8910                           and then
8911                             Chars (Defining_Identifier (Decl)) =
8912                                                      Chars (Desig_Type))
8913
8914                        or else
8915                         (Nkind (Decl) = N_Full_Type_Declaration
8916                           and then
8917                             Chars (Defining_Identifier (Decl)) =
8918                                                      Chars (Desig_Type)
8919                           and then Is_Derived_Type (Desig_Type)
8920                           and then
8921                             Has_Private_Declaration (Etype (Desig_Type)))
8922                      then
8923                         if No (Discriminant_Specifications (Decl)) then
8924                            Error_Msg_N
8925                             ("cannot constrain general access type if " &
8926                                "designated type has constrained partial view",
8927                                 S);
8928                         end if;
8929
8930                         exit;
8931                      end if;
8932
8933                      Next (Decl);
8934                   end loop;
8935                end if;
8936             end;
8937          end if;
8938
8939          Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
8940            For_Access => True);
8941
8942       elsif (Is_Task_Type (Desig_Type)
8943               or else Is_Protected_Type (Desig_Type))
8944         and then not Is_Constrained (Desig_Type)
8945       then
8946          Constrain_Concurrent
8947            (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
8948
8949       else
8950          Error_Msg_N ("invalid constraint on access type", S);
8951          Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
8952          Constraint_OK := False;
8953       end if;
8954
8955       if No (Def_Id) then
8956          Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
8957       else
8958          Set_Ekind (Def_Id, E_Access_Subtype);
8959       end if;
8960
8961       if Constraint_OK then
8962          Set_Etype (Def_Id, Base_Type (T));
8963
8964          if Is_Private_Type (Desig_Type) then
8965             Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
8966          end if;
8967       else
8968          Set_Etype (Def_Id, Any_Type);
8969       end if;
8970
8971       Set_Size_Info                (Def_Id, T);
8972       Set_Is_Constrained           (Def_Id, Constraint_OK);
8973       Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
8974       Set_Depends_On_Private       (Def_Id, Has_Private_Component (Def_Id));
8975       Set_Is_Access_Constant       (Def_Id, Is_Access_Constant (T));
8976
8977       Conditional_Delay (Def_Id, T);
8978
8979       --  AI-363 : Subtypes of general access types whose designated types have
8980       --  default discriminants are disallowed. In instances, the rule has to
8981       --  be checked against the actual, of which T is the subtype. In a
8982       --  generic body, the rule is checked assuming that the actual type has
8983       --  defaulted discriminants.
8984
8985       if Ada_Version >= Ada_05 or else Warn_On_Ada_2005_Compatibility then
8986          if Ekind (Base_Type (T)) = E_General_Access_Type
8987            and then Has_Defaulted_Discriminants (Desig_Type)
8988          then
8989             if Ada_Version < Ada_05 then
8990                Error_Msg_N
8991                  ("access subtype of general access type would not " &
8992                   "be allowed in Ada 2005?", S);
8993             else
8994                Error_Msg_N
8995                  ("access subype of general access type not allowed", S);
8996             end if;
8997
8998             Error_Msg_N ("\discriminants have defaults", S);
8999
9000          elsif Is_Access_Type (T)
9001            and then Is_Generic_Type (Desig_Type)
9002            and then Has_Discriminants (Desig_Type)
9003            and then In_Package_Body (Current_Scope)
9004          then
9005             if Ada_Version < Ada_05 then
9006                Error_Msg_N
9007                  ("access subtype would not be allowed in generic body " &
9008                   "in Ada 2005?", S);
9009             else
9010                Error_Msg_N
9011                  ("access subtype not allowed in generic body", S);
9012             end if;
9013
9014             Error_Msg_N
9015               ("\designated type is a discriminated formal", S);
9016          end if;
9017       end if;
9018    end Constrain_Access;
9019
9020    ---------------------
9021    -- Constrain_Array --
9022    ---------------------
9023
9024    procedure Constrain_Array
9025      (Def_Id      : in out Entity_Id;
9026       SI          : Node_Id;
9027       Related_Nod : Node_Id;
9028       Related_Id  : Entity_Id;
9029       Suffix      : Character)
9030    is
9031       C                     : constant Node_Id := Constraint (SI);
9032       Number_Of_Constraints : Nat := 0;
9033       Index                 : Node_Id;
9034       S, T                  : Entity_Id;
9035       Constraint_OK         : Boolean := True;
9036
9037    begin
9038       T := Entity (Subtype_Mark (SI));
9039
9040       if Ekind (T) in Access_Kind then
9041          T := Designated_Type (T);
9042       end if;
9043
9044       --  If an index constraint follows a subtype mark in a subtype indication
9045       --  then the type or subtype denoted by the subtype mark must not already
9046       --  impose an index constraint. The subtype mark must denote either an
9047       --  unconstrained array type or an access type whose designated type
9048       --  is such an array type... (RM 3.6.1)
9049
9050       if Is_Constrained (T) then
9051          Error_Msg_N
9052            ("array type is already constrained", Subtype_Mark (SI));
9053          Constraint_OK := False;
9054
9055       else
9056          S := First (Constraints (C));
9057          while Present (S) loop
9058             Number_Of_Constraints := Number_Of_Constraints + 1;
9059             Next (S);
9060          end loop;
9061
9062          --  In either case, the index constraint must provide a discrete
9063          --  range for each index of the array type and the type of each
9064          --  discrete range must be the same as that of the corresponding
9065          --  index. (RM 3.6.1)
9066
9067          if Number_Of_Constraints /= Number_Dimensions (T) then
9068             Error_Msg_NE ("incorrect number of index constraints for }", C, T);
9069             Constraint_OK := False;
9070
9071          else
9072             S := First (Constraints (C));
9073             Index := First_Index (T);
9074             Analyze (Index);
9075
9076             --  Apply constraints to each index type
9077
9078             for J in 1 .. Number_Of_Constraints loop
9079                Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
9080                Next (Index);
9081                Next (S);
9082             end loop;
9083
9084          end if;
9085       end if;
9086
9087       if No (Def_Id) then
9088          Def_Id :=
9089            Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
9090          Set_Parent (Def_Id, Related_Nod);
9091
9092       else
9093          Set_Ekind (Def_Id, E_Array_Subtype);
9094       end if;
9095
9096       Set_Size_Info      (Def_Id,                (T));
9097       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9098       Set_Etype          (Def_Id, Base_Type      (T));
9099
9100       if Constraint_OK then
9101          Set_First_Index (Def_Id, First (Constraints (C)));
9102       else
9103          Set_First_Index (Def_Id, First_Index (T));
9104       end if;
9105
9106       Set_Is_Constrained     (Def_Id, True);
9107       Set_Is_Aliased         (Def_Id, Is_Aliased (T));
9108       Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
9109
9110       Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
9111       Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
9112
9113       --  A subtype does not inherit the packed_array_type of is parent. We
9114       --  need to initialize the attribute because if Def_Id is previously
9115       --  analyzed through a limited_with clause, it will have the attributes
9116       --  of an incomplete type, one of which is an Elist that overlaps the
9117       --  Packed_Array_Type field.
9118
9119       Set_Packed_Array_Type (Def_Id, Empty);
9120
9121       --  Build a freeze node if parent still needs one. Also make sure that
9122       --  the Depends_On_Private status is set because the subtype will need
9123       --  reprocessing at the time the base type does, and also we must set a
9124       --  conditional delay.
9125
9126       Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9127       Conditional_Delay (Def_Id, T);
9128    end Constrain_Array;
9129
9130    ------------------------------
9131    -- Constrain_Component_Type --
9132    ------------------------------
9133
9134    function Constrain_Component_Type
9135      (Comp            : Entity_Id;
9136       Constrained_Typ : Entity_Id;
9137       Related_Node    : Node_Id;
9138       Typ             : Entity_Id;
9139       Constraints     : Elist_Id) return Entity_Id
9140    is
9141       Loc         : constant Source_Ptr := Sloc (Constrained_Typ);
9142       Compon_Type : constant Entity_Id := Etype (Comp);
9143
9144       function Build_Constrained_Array_Type
9145         (Old_Type : Entity_Id) return Entity_Id;
9146       --  If Old_Type is an array type, one of whose indices is constrained
9147       --  by a discriminant, build an Itype whose constraint replaces the
9148       --  discriminant with its value in the constraint.
9149
9150       function Build_Constrained_Discriminated_Type
9151         (Old_Type : Entity_Id) return Entity_Id;
9152       --  Ditto for record components
9153
9154       function Build_Constrained_Access_Type
9155         (Old_Type : Entity_Id) return Entity_Id;
9156       --  Ditto for access types. Makes use of previous two functions, to
9157       --  constrain designated type.
9158
9159       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
9160       --  T is an array or discriminated type, C is a list of constraints
9161       --  that apply to T. This routine builds the constrained subtype.
9162
9163       function Is_Discriminant (Expr : Node_Id) return Boolean;
9164       --  Returns True if Expr is a discriminant
9165
9166       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
9167       --  Find the value of discriminant Discrim in Constraint
9168
9169       -----------------------------------
9170       -- Build_Constrained_Access_Type --
9171       -----------------------------------
9172
9173       function Build_Constrained_Access_Type
9174         (Old_Type : Entity_Id) return Entity_Id
9175       is
9176          Desig_Type    : constant Entity_Id := Designated_Type (Old_Type);
9177          Itype         : Entity_Id;
9178          Desig_Subtype : Entity_Id;
9179          Scop          : Entity_Id;
9180
9181       begin
9182          --  if the original access type was not embedded in the enclosing
9183          --  type definition, there is no need to produce a new access
9184          --  subtype. In fact every access type with an explicit constraint
9185          --  generates an itype whose scope is the enclosing record.
9186
9187          if not Is_Type (Scope (Old_Type)) then
9188             return Old_Type;
9189
9190          elsif Is_Array_Type (Desig_Type) then
9191             Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
9192
9193          elsif Has_Discriminants (Desig_Type) then
9194
9195             --  This may be an access type to an enclosing record type for
9196             --  which we are constructing the constrained components. Return
9197             --  the enclosing record subtype. This is not always correct,
9198             --  but avoids infinite recursion. ???
9199
9200             Desig_Subtype := Any_Type;
9201
9202             for J in reverse 0 .. Scope_Stack.Last loop
9203                Scop := Scope_Stack.Table (J).Entity;
9204
9205                if Is_Type (Scop)
9206                  and then Base_Type (Scop) = Base_Type (Desig_Type)
9207                then
9208                   Desig_Subtype := Scop;
9209                end if;
9210
9211                exit when not Is_Type (Scop);
9212             end loop;
9213
9214             if Desig_Subtype = Any_Type then
9215                Desig_Subtype :=
9216                  Build_Constrained_Discriminated_Type (Desig_Type);
9217             end if;
9218
9219          else
9220             return Old_Type;
9221          end if;
9222
9223          if Desig_Subtype /= Desig_Type then
9224
9225             --  The Related_Node better be here or else we won't be able
9226             --  to attach new itypes to a node in the tree.
9227
9228             pragma Assert (Present (Related_Node));
9229
9230             Itype := Create_Itype (E_Access_Subtype, Related_Node);
9231
9232             Set_Etype                    (Itype, Base_Type      (Old_Type));
9233             Set_Size_Info                (Itype,                (Old_Type));
9234             Set_Directly_Designated_Type (Itype, Desig_Subtype);
9235             Set_Depends_On_Private       (Itype, Has_Private_Component
9236                                                                 (Old_Type));
9237             Set_Is_Access_Constant       (Itype, Is_Access_Constant
9238                                                                 (Old_Type));
9239
9240             --  The new itype needs freezing when it depends on a not frozen
9241             --  type and the enclosing subtype needs freezing.
9242
9243             if Has_Delayed_Freeze (Constrained_Typ)
9244               and then not Is_Frozen (Constrained_Typ)
9245             then
9246                Conditional_Delay (Itype, Base_Type (Old_Type));
9247             end if;
9248
9249             return Itype;
9250
9251          else
9252             return Old_Type;
9253          end if;
9254       end Build_Constrained_Access_Type;
9255
9256       ----------------------------------
9257       -- Build_Constrained_Array_Type --
9258       ----------------------------------
9259
9260       function Build_Constrained_Array_Type
9261         (Old_Type : Entity_Id) return Entity_Id
9262       is
9263          Lo_Expr     : Node_Id;
9264          Hi_Expr     : Node_Id;
9265          Old_Index   : Node_Id;
9266          Range_Node  : Node_Id;
9267          Constr_List : List_Id;
9268
9269          Need_To_Create_Itype : Boolean := False;
9270
9271       begin
9272          Old_Index := First_Index (Old_Type);
9273          while Present (Old_Index) loop
9274             Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
9275
9276             if Is_Discriminant (Lo_Expr)
9277               or else Is_Discriminant (Hi_Expr)
9278             then
9279                Need_To_Create_Itype := True;
9280             end if;
9281
9282             Next_Index (Old_Index);
9283          end loop;
9284
9285          if Need_To_Create_Itype then
9286             Constr_List := New_List;
9287
9288             Old_Index := First_Index (Old_Type);
9289             while Present (Old_Index) loop
9290                Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
9291
9292                if Is_Discriminant (Lo_Expr) then
9293                   Lo_Expr := Get_Discr_Value (Lo_Expr);
9294                end if;
9295
9296                if Is_Discriminant (Hi_Expr) then
9297                   Hi_Expr := Get_Discr_Value (Hi_Expr);
9298                end if;
9299
9300                Range_Node :=
9301                  Make_Range
9302                    (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
9303
9304                Append (Range_Node, To => Constr_List);
9305
9306                Next_Index (Old_Index);
9307             end loop;
9308
9309             return Build_Subtype (Old_Type, Constr_List);
9310
9311          else
9312             return Old_Type;
9313          end if;
9314       end Build_Constrained_Array_Type;
9315
9316       ------------------------------------------
9317       -- Build_Constrained_Discriminated_Type --
9318       ------------------------------------------
9319
9320       function Build_Constrained_Discriminated_Type
9321         (Old_Type : Entity_Id) return Entity_Id
9322       is
9323          Expr           : Node_Id;
9324          Constr_List    : List_Id;
9325          Old_Constraint : Elmt_Id;
9326
9327          Need_To_Create_Itype : Boolean := False;
9328
9329       begin
9330          Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
9331          while Present (Old_Constraint) loop
9332             Expr := Node (Old_Constraint);
9333
9334             if Is_Discriminant (Expr) then
9335                Need_To_Create_Itype := True;
9336             end if;
9337
9338             Next_Elmt (Old_Constraint);
9339          end loop;
9340
9341          if Need_To_Create_Itype then
9342             Constr_List := New_List;
9343
9344             Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
9345             while Present (Old_Constraint) loop
9346                Expr := Node (Old_Constraint);
9347
9348                if Is_Discriminant (Expr) then
9349                   Expr := Get_Discr_Value (Expr);
9350                end if;
9351
9352                Append (New_Copy_Tree (Expr), To => Constr_List);
9353
9354                Next_Elmt (Old_Constraint);
9355             end loop;
9356
9357             return Build_Subtype (Old_Type, Constr_List);
9358
9359          else
9360             return Old_Type;
9361          end if;
9362       end Build_Constrained_Discriminated_Type;
9363
9364       -------------------
9365       -- Build_Subtype --
9366       -------------------
9367
9368       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
9369          Indic       : Node_Id;
9370          Subtyp_Decl : Node_Id;
9371          Def_Id      : Entity_Id;
9372          Btyp        : Entity_Id := Base_Type (T);
9373
9374       begin
9375          --  The Related_Node better be here or else we won't be able to
9376          --  attach new itypes to a node in the tree.
9377
9378          pragma Assert (Present (Related_Node));
9379
9380          --  If the view of the component's type is incomplete or private
9381          --  with unknown discriminants, then the constraint must be applied
9382          --  to the full type.
9383
9384          if Has_Unknown_Discriminants (Btyp)
9385            and then Present (Underlying_Type (Btyp))
9386          then
9387             Btyp := Underlying_Type (Btyp);
9388          end if;
9389
9390          Indic :=
9391            Make_Subtype_Indication (Loc,
9392              Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9393              Constraint   => Make_Index_Or_Discriminant_Constraint (Loc, C));
9394
9395          Def_Id := Create_Itype (Ekind (T), Related_Node);
9396
9397          Subtyp_Decl :=
9398            Make_Subtype_Declaration (Loc,
9399              Defining_Identifier => Def_Id,
9400              Subtype_Indication  => Indic);
9401
9402          Set_Parent (Subtyp_Decl, Parent (Related_Node));
9403
9404          --  Itypes must be analyzed with checks off (see package Itypes)
9405
9406          Analyze (Subtyp_Decl, Suppress => All_Checks);
9407
9408          return Def_Id;
9409       end Build_Subtype;
9410
9411       ---------------------
9412       -- Get_Discr_Value --
9413       ---------------------
9414
9415       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
9416          D : Entity_Id;
9417          E : Elmt_Id;
9418          G : Elmt_Id;
9419
9420       begin
9421          --  The discriminant may be declared for the type, in which case we
9422          --  find it by iterating over the list of discriminants. If the
9423          --  discriminant is inherited from a parent type, it appears as the
9424          --  corresponding discriminant of the current type. This will be the
9425          --  case when constraining an inherited component whose constraint is
9426          --  given by a discriminant of the parent.
9427
9428          D := First_Discriminant (Typ);
9429          E := First_Elmt (Constraints);
9430
9431          while Present (D) loop
9432             if D = Entity (Discrim)
9433               or else D = CR_Discriminant (Entity (Discrim))
9434               or else Corresponding_Discriminant (D) = Entity (Discrim)
9435             then
9436                return Node (E);
9437             end if;
9438
9439             Next_Discriminant (D);
9440             Next_Elmt (E);
9441          end loop;
9442
9443          --  The corresponding_Discriminant mechanism is incomplete, because
9444          --  the correspondence between new and old discriminants is not one
9445          --  to one: one new discriminant can constrain several old ones. In
9446          --  that case, scan sequentially the stored_constraint, the list of
9447          --  discriminants of the parents, and the constraints.
9448
9449          if Is_Derived_Type (Typ)
9450            and then Present (Stored_Constraint (Typ))
9451            and then Scope (Entity (Discrim)) = Etype (Typ)
9452          then
9453             D := First_Discriminant (Etype (Typ));
9454             E := First_Elmt (Constraints);
9455             G := First_Elmt (Stored_Constraint (Typ));
9456             while Present (D) loop
9457                if D = Entity (Discrim) then
9458                   return Node (E);
9459                end if;
9460
9461                Next_Discriminant (D);
9462                Next_Elmt (E);
9463                Next_Elmt (G);
9464             end loop;
9465          end if;
9466
9467          --  Something is wrong if we did not find the value
9468
9469          raise Program_Error;
9470       end Get_Discr_Value;
9471
9472       ---------------------
9473       -- Is_Discriminant --
9474       ---------------------
9475
9476       function Is_Discriminant (Expr : Node_Id) return Boolean is
9477          Discrim_Scope : Entity_Id;
9478
9479       begin
9480          if Denotes_Discriminant (Expr) then
9481             Discrim_Scope := Scope (Entity (Expr));
9482
9483             --  Either we have a reference to one of Typ's discriminants,
9484
9485             pragma Assert (Discrim_Scope = Typ
9486
9487                --  or to the discriminants of the parent type, in the case
9488                --  of a derivation of a tagged type with variants.
9489
9490                or else Discrim_Scope = Etype (Typ)
9491                or else Full_View (Discrim_Scope) = Etype (Typ)
9492
9493                --  or same as above for the case where the discriminants
9494                --  were declared in Typ's private view.
9495
9496                or else (Is_Private_Type (Discrim_Scope)
9497                         and then Chars (Discrim_Scope) = Chars (Typ))
9498
9499                --  or else we are deriving from the full view and the
9500                --  discriminant is declared in the private entity.
9501
9502                or else (Is_Private_Type (Typ)
9503                         and then Chars (Discrim_Scope) = Chars (Typ))
9504
9505                --  Or we are constrained the corresponding record of a
9506                --  synchronized type that completes a private declaration.
9507
9508                or else (Is_Concurrent_Record_Type (Typ)
9509                          and then
9510                            Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
9511
9512                --  or we have a class-wide type, in which case make sure the
9513                --  discriminant found belongs to the root type.
9514
9515                or else (Is_Class_Wide_Type (Typ)
9516                         and then Etype (Typ) = Discrim_Scope));
9517
9518             return True;
9519          end if;
9520
9521          --  In all other cases we have something wrong
9522
9523          return False;
9524       end Is_Discriminant;
9525
9526    --  Start of processing for Constrain_Component_Type
9527
9528    begin
9529       if Nkind (Parent (Comp)) = N_Component_Declaration
9530         and then Comes_From_Source (Parent (Comp))
9531         and then Comes_From_Source
9532           (Subtype_Indication (Component_Definition (Parent (Comp))))
9533         and then
9534           Is_Entity_Name
9535             (Subtype_Indication (Component_Definition (Parent (Comp))))
9536       then
9537          return Compon_Type;
9538
9539       elsif Is_Array_Type (Compon_Type) then
9540          return Build_Constrained_Array_Type (Compon_Type);
9541
9542       elsif Has_Discriminants (Compon_Type) then
9543          return Build_Constrained_Discriminated_Type (Compon_Type);
9544
9545       elsif Is_Access_Type (Compon_Type) then
9546          return Build_Constrained_Access_Type (Compon_Type);
9547
9548       else
9549          return Compon_Type;
9550       end if;
9551    end Constrain_Component_Type;
9552
9553    --------------------------
9554    -- Constrain_Concurrent --
9555    --------------------------
9556
9557    --  For concurrent types, the associated record value type carries the same
9558    --  discriminants, so when we constrain a concurrent type, we must constrain
9559    --  the corresponding record type as well.
9560
9561    procedure Constrain_Concurrent
9562      (Def_Id      : in out Entity_Id;
9563       SI          : Node_Id;
9564       Related_Nod : Node_Id;
9565       Related_Id  : Entity_Id;
9566       Suffix      : Character)
9567    is
9568       T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
9569       T_Val : Entity_Id;
9570
9571    begin
9572       if Ekind (T_Ent) in Access_Kind then
9573          T_Ent := Designated_Type (T_Ent);
9574       end if;
9575
9576       T_Val := Corresponding_Record_Type (T_Ent);
9577
9578       if Present (T_Val) then
9579
9580          if No (Def_Id) then
9581             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
9582          end if;
9583
9584          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
9585
9586          Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
9587          Set_Corresponding_Record_Type (Def_Id,
9588            Constrain_Corresponding_Record
9589              (Def_Id, T_Val, Related_Nod, Related_Id));
9590
9591       else
9592          --  If there is no associated record, expansion is disabled and this
9593          --  is a generic context. Create a subtype in any case, so that
9594          --  semantic analysis can proceed.
9595
9596          if No (Def_Id) then
9597             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
9598          end if;
9599
9600          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
9601       end if;
9602    end Constrain_Concurrent;
9603
9604    ------------------------------------
9605    -- Constrain_Corresponding_Record --
9606    ------------------------------------
9607
9608    function Constrain_Corresponding_Record
9609      (Prot_Subt   : Entity_Id;
9610       Corr_Rec    : Entity_Id;
9611       Related_Nod : Node_Id;
9612       Related_Id  : Entity_Id) return Entity_Id
9613    is
9614       T_Sub : constant Entity_Id :=
9615                 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
9616
9617    begin
9618       Set_Etype             (T_Sub, Corr_Rec);
9619       Init_Size_Align       (T_Sub);
9620       Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
9621       Set_Is_Constrained    (T_Sub, True);
9622       Set_First_Entity      (T_Sub, First_Entity (Corr_Rec));
9623       Set_Last_Entity       (T_Sub, Last_Entity  (Corr_Rec));
9624
9625       --  As elsewhere, we do not want to create a freeze node for this itype
9626       --  if it is created for a constrained component of an enclosing record
9627       --  because references to outer discriminants will appear out of scope.
9628
9629       if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
9630          Conditional_Delay (T_Sub, Corr_Rec);
9631       else
9632          Set_Is_Frozen (T_Sub);
9633       end if;
9634
9635       if Has_Discriminants (Prot_Subt) then -- False only if errors.
9636          Set_Discriminant_Constraint
9637            (T_Sub, Discriminant_Constraint (Prot_Subt));
9638          Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
9639          Create_Constrained_Components
9640            (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
9641       end if;
9642
9643       Set_Depends_On_Private      (T_Sub, Has_Private_Component (T_Sub));
9644
9645       return T_Sub;
9646    end Constrain_Corresponding_Record;
9647
9648    -----------------------
9649    -- Constrain_Decimal --
9650    -----------------------
9651
9652    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
9653       T           : constant Entity_Id  := Entity (Subtype_Mark (S));
9654       C           : constant Node_Id    := Constraint (S);
9655       Loc         : constant Source_Ptr := Sloc (C);
9656       Range_Expr  : Node_Id;
9657       Digits_Expr : Node_Id;
9658       Digits_Val  : Uint;
9659       Bound_Val   : Ureal;
9660
9661    begin
9662       Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
9663
9664       if Nkind (C) = N_Range_Constraint then
9665          Range_Expr := Range_Expression (C);
9666          Digits_Val := Digits_Value (T);
9667
9668       else
9669          pragma Assert (Nkind (C) = N_Digits_Constraint);
9670          Digits_Expr := Digits_Expression (C);
9671          Analyze_And_Resolve (Digits_Expr, Any_Integer);
9672
9673          Check_Digits_Expression (Digits_Expr);
9674          Digits_Val := Expr_Value (Digits_Expr);
9675
9676          if Digits_Val > Digits_Value (T) then
9677             Error_Msg_N
9678                ("digits expression is incompatible with subtype", C);
9679             Digits_Val := Digits_Value (T);
9680          end if;
9681
9682          if Present (Range_Constraint (C)) then
9683             Range_Expr := Range_Expression (Range_Constraint (C));
9684          else
9685             Range_Expr := Empty;
9686          end if;
9687       end if;
9688
9689       Set_Etype            (Def_Id, Base_Type        (T));
9690       Set_Size_Info        (Def_Id,                  (T));
9691       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
9692       Set_Delta_Value      (Def_Id, Delta_Value      (T));
9693       Set_Scale_Value      (Def_Id, Scale_Value      (T));
9694       Set_Small_Value      (Def_Id, Small_Value      (T));
9695       Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
9696       Set_Digits_Value     (Def_Id, Digits_Val);
9697
9698       --  Manufacture range from given digits value if no range present
9699
9700       if No (Range_Expr) then
9701          Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
9702          Range_Expr :=
9703            Make_Range (Loc,
9704              Low_Bound =>
9705                Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
9706              High_Bound =>
9707                Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
9708       end if;
9709
9710       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
9711       Set_Discrete_RM_Size (Def_Id);
9712
9713       --  Unconditionally delay the freeze, since we cannot set size
9714       --  information in all cases correctly until the freeze point.
9715
9716       Set_Has_Delayed_Freeze (Def_Id);
9717    end Constrain_Decimal;
9718
9719    ----------------------------------
9720    -- Constrain_Discriminated_Type --
9721    ----------------------------------
9722
9723    procedure Constrain_Discriminated_Type
9724      (Def_Id      : Entity_Id;
9725       S           : Node_Id;
9726       Related_Nod : Node_Id;
9727       For_Access  : Boolean := False)
9728    is
9729       E     : constant Entity_Id := Entity (Subtype_Mark (S));
9730       T     : Entity_Id;
9731       C     : Node_Id;
9732       Elist : Elist_Id := New_Elmt_List;
9733
9734       procedure Fixup_Bad_Constraint;
9735       --  This is called after finding a bad constraint, and after having
9736       --  posted an appropriate error message. The mission is to leave the
9737       --  entity T in as reasonable state as possible!
9738
9739       --------------------------
9740       -- Fixup_Bad_Constraint --
9741       --------------------------
9742
9743       procedure Fixup_Bad_Constraint is
9744       begin
9745          --  Set a reasonable Ekind for the entity. For an incomplete type,
9746          --  we can't do much, but for other types, we can set the proper
9747          --  corresponding subtype kind.
9748
9749          if Ekind (T) = E_Incomplete_Type then
9750             Set_Ekind (Def_Id, Ekind (T));
9751          else
9752             Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9753          end if;
9754
9755          Set_Etype (Def_Id, Any_Type);
9756          Set_Error_Posted (Def_Id);
9757       end Fixup_Bad_Constraint;
9758
9759    --  Start of processing for Constrain_Discriminated_Type
9760
9761    begin
9762       C := Constraint (S);
9763
9764       --  A discriminant constraint is only allowed in a subtype indication,
9765       --  after a subtype mark. This subtype mark must denote either a type
9766       --  with discriminants, or an access type whose designated type is a
9767       --  type with discriminants. A discriminant constraint specifies the
9768       --  values of these discriminants (RM 3.7.2(5)).
9769
9770       T := Base_Type (Entity (Subtype_Mark (S)));
9771
9772       if Ekind (T) in Access_Kind then
9773          T := Designated_Type (T);
9774       end if;
9775
9776       --  Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
9777       --  Avoid generating an error for access-to-incomplete subtypes.
9778
9779       if Ada_Version >= Ada_05
9780         and then Ekind (T) = E_Incomplete_Type
9781         and then Nkind (Parent (S)) = N_Subtype_Declaration
9782         and then not Is_Itype (Def_Id)
9783       then
9784          --  A little sanity check, emit an error message if the type
9785          --  has discriminants to begin with. Type T may be a regular
9786          --  incomplete type or imported via a limited with clause.
9787
9788          if Has_Discriminants (T)
9789            or else
9790              (From_With_Type (T)
9791                 and then Present (Non_Limited_View (T))
9792                 and then Nkind (Parent (Non_Limited_View (T))) =
9793                            N_Full_Type_Declaration
9794                 and then Present (Discriminant_Specifications
9795                           (Parent (Non_Limited_View (T)))))
9796          then
9797             Error_Msg_N
9798               ("(Ada 2005) incomplete subtype may not be constrained", C);
9799          else
9800             Error_Msg_N
9801               ("invalid constraint: type has no discriminant", C);
9802          end if;
9803
9804          Fixup_Bad_Constraint;
9805          return;
9806
9807       --  Check that the type has visible discriminants. The type may be
9808       --  a private type with unknown discriminants whose full view has
9809       --  discriminants which are invisible.
9810
9811       elsif not Has_Discriminants (T)
9812         or else
9813           (Has_Unknown_Discriminants (T)
9814              and then Is_Private_Type (T))
9815       then
9816          Error_Msg_N ("invalid constraint: type has no discriminant", C);
9817          Fixup_Bad_Constraint;
9818          return;
9819
9820       elsif Is_Constrained (E)
9821         or else (Ekind (E) = E_Class_Wide_Subtype
9822                   and then Present (Discriminant_Constraint (E)))
9823       then
9824          Error_Msg_N ("type is already constrained", Subtype_Mark (S));
9825          Fixup_Bad_Constraint;
9826          return;
9827       end if;
9828
9829       --  T may be an unconstrained subtype (e.g. a generic actual).
9830       --  Constraint applies to the base type.
9831
9832       T := Base_Type (T);
9833
9834       Elist := Build_Discriminant_Constraints (T, S);
9835
9836       --  If the list returned was empty we had an error in building the
9837       --  discriminant constraint. We have also already signalled an error
9838       --  in the incomplete type case
9839
9840       if Is_Empty_Elmt_List (Elist) then
9841          Fixup_Bad_Constraint;
9842          return;
9843       end if;
9844
9845       Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
9846    end Constrain_Discriminated_Type;
9847
9848    ---------------------------
9849    -- Constrain_Enumeration --
9850    ---------------------------
9851
9852    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
9853       T : constant Entity_Id := Entity (Subtype_Mark (S));
9854       C : constant Node_Id   := Constraint (S);
9855
9856    begin
9857       Set_Ekind (Def_Id, E_Enumeration_Subtype);
9858
9859       Set_First_Literal     (Def_Id, First_Literal (Base_Type (T)));
9860
9861       Set_Etype             (Def_Id, Base_Type         (T));
9862       Set_Size_Info         (Def_Id,                   (T));
9863       Set_First_Rep_Item    (Def_Id, First_Rep_Item    (T));
9864       Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
9865
9866       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
9867
9868       Set_Discrete_RM_Size (Def_Id);
9869    end Constrain_Enumeration;
9870
9871    ----------------------
9872    -- Constrain_Float --
9873    ----------------------
9874
9875    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
9876       T    : constant Entity_Id := Entity (Subtype_Mark (S));
9877       C    : Node_Id;
9878       D    : Node_Id;
9879       Rais : Node_Id;
9880
9881    begin
9882       Set_Ekind (Def_Id, E_Floating_Point_Subtype);
9883
9884       Set_Etype          (Def_Id, Base_Type      (T));
9885       Set_Size_Info      (Def_Id,                (T));
9886       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9887
9888       --  Process the constraint
9889
9890       C := Constraint (S);
9891
9892       --  Digits constraint present
9893
9894       if Nkind (C) = N_Digits_Constraint then
9895          Check_Restriction (No_Obsolescent_Features, C);
9896
9897          if Warn_On_Obsolescent_Feature then
9898             Error_Msg_N
9899               ("subtype digits constraint is an " &
9900                "obsolescent feature (RM J.3(8))?", C);
9901          end if;
9902
9903          D := Digits_Expression (C);
9904          Analyze_And_Resolve (D, Any_Integer);
9905          Check_Digits_Expression (D);
9906          Set_Digits_Value (Def_Id, Expr_Value (D));
9907
9908          --  Check that digits value is in range. Obviously we can do this
9909          --  at compile time, but it is strictly a runtime check, and of
9910          --  course there is an ACVC test that checks this!
9911
9912          if Digits_Value (Def_Id) > Digits_Value (T) then
9913             Error_Msg_Uint_1 := Digits_Value (T);
9914             Error_Msg_N ("?digits value is too large, maximum is ^", D);
9915             Rais :=
9916               Make_Raise_Constraint_Error (Sloc (D),
9917                 Reason => CE_Range_Check_Failed);
9918             Insert_Action (Declaration_Node (Def_Id), Rais);
9919          end if;
9920
9921          C := Range_Constraint (C);
9922
9923       --  No digits constraint present
9924
9925       else
9926          Set_Digits_Value (Def_Id, Digits_Value (T));
9927       end if;
9928
9929       --  Range constraint present
9930
9931       if Nkind (C) = N_Range_Constraint then
9932          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
9933
9934       --  No range constraint present
9935
9936       else
9937          pragma Assert (No (C));
9938          Set_Scalar_Range (Def_Id, Scalar_Range (T));
9939       end if;
9940
9941       Set_Is_Constrained (Def_Id);
9942    end Constrain_Float;
9943
9944    ---------------------
9945    -- Constrain_Index --
9946    ---------------------
9947
9948    procedure Constrain_Index
9949      (Index        : Node_Id;
9950       S            : Node_Id;
9951       Related_Nod  : Node_Id;
9952       Related_Id   : Entity_Id;
9953       Suffix       : Character;
9954       Suffix_Index : Nat)
9955    is
9956       Def_Id : Entity_Id;
9957       R      : Node_Id := Empty;
9958       T      : constant Entity_Id := Etype (Index);
9959
9960    begin
9961       if Nkind (S) = N_Range
9962         or else
9963           (Nkind (S) = N_Attribute_Reference
9964             and then Attribute_Name (S) = Name_Range)
9965       then
9966          --  A Range attribute will transformed into N_Range by Resolve
9967
9968          Analyze (S);
9969          Set_Etype (S, T);
9970          R := S;
9971
9972          Process_Range_Expr_In_Decl (R, T, Empty_List);
9973
9974          if not Error_Posted (S)
9975            and then
9976              (Nkind (S) /= N_Range
9977                or else not Covers (T, (Etype (Low_Bound (S))))
9978                or else not Covers (T, (Etype (High_Bound (S)))))
9979          then
9980             if Base_Type (T) /= Any_Type
9981               and then Etype (Low_Bound (S)) /= Any_Type
9982               and then Etype (High_Bound (S)) /= Any_Type
9983             then
9984                Error_Msg_N ("range expected", S);
9985             end if;
9986          end if;
9987
9988       elsif Nkind (S) = N_Subtype_Indication then
9989
9990          --  The parser has verified that this is a discrete indication
9991
9992          Resolve_Discrete_Subtype_Indication (S, T);
9993          R := Range_Expression (Constraint (S));
9994
9995       elsif Nkind (S) = N_Discriminant_Association then
9996
9997          --  Syntactically valid in subtype indication
9998
9999          Error_Msg_N ("invalid index constraint", S);
10000          Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10001          return;
10002
10003       --  Subtype_Mark case, no anonymous subtypes to construct
10004
10005       else
10006          Analyze (S);
10007
10008          if Is_Entity_Name (S) then
10009             if not Is_Type (Entity (S)) then
10010                Error_Msg_N ("expect subtype mark for index constraint", S);
10011
10012             elsif Base_Type (Entity (S)) /= Base_Type (T) then
10013                Wrong_Type (S, Base_Type (T));
10014             end if;
10015
10016             return;
10017
10018          else
10019             Error_Msg_N ("invalid index constraint", S);
10020             Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10021             return;
10022          end if;
10023       end if;
10024
10025       Def_Id :=
10026         Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
10027
10028       Set_Etype (Def_Id, Base_Type (T));
10029
10030       if Is_Modular_Integer_Type (T) then
10031          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10032
10033       elsif Is_Integer_Type (T) then
10034          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10035
10036       else
10037          Set_Ekind (Def_Id, E_Enumeration_Subtype);
10038          Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10039       end if;
10040
10041       Set_Size_Info      (Def_Id,                (T));
10042       Set_RM_Size        (Def_Id, RM_Size        (T));
10043       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10044
10045       Set_Scalar_Range   (Def_Id, R);
10046
10047       Set_Etype (S, Def_Id);
10048       Set_Discrete_RM_Size (Def_Id);
10049    end Constrain_Index;
10050
10051    -----------------------
10052    -- Constrain_Integer --
10053    -----------------------
10054
10055    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
10056       T : constant Entity_Id := Entity (Subtype_Mark (S));
10057       C : constant Node_Id   := Constraint (S);
10058
10059    begin
10060       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10061
10062       if Is_Modular_Integer_Type (T) then
10063          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10064       else
10065          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10066       end if;
10067
10068       Set_Etype            (Def_Id, Base_Type        (T));
10069       Set_Size_Info        (Def_Id,                  (T));
10070       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
10071       Set_Discrete_RM_Size (Def_Id);
10072    end Constrain_Integer;
10073
10074    ------------------------------
10075    -- Constrain_Ordinary_Fixed --
10076    ------------------------------
10077
10078    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
10079       T    : constant Entity_Id := Entity (Subtype_Mark (S));
10080       C    : Node_Id;
10081       D    : Node_Id;
10082       Rais : Node_Id;
10083
10084    begin
10085       Set_Ekind          (Def_Id, E_Ordinary_Fixed_Point_Subtype);
10086       Set_Etype          (Def_Id, Base_Type        (T));
10087       Set_Size_Info      (Def_Id,                  (T));
10088       Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
10089       Set_Small_Value    (Def_Id, Small_Value      (T));
10090
10091       --  Process the constraint
10092
10093       C := Constraint (S);
10094
10095       --  Delta constraint present
10096
10097       if Nkind (C) = N_Delta_Constraint then
10098          Check_Restriction (No_Obsolescent_Features, C);
10099
10100          if Warn_On_Obsolescent_Feature then
10101             Error_Msg_S
10102               ("subtype delta constraint is an " &
10103                "obsolescent feature (RM J.3(7))?");
10104          end if;
10105
10106          D := Delta_Expression (C);
10107          Analyze_And_Resolve (D, Any_Real);
10108          Check_Delta_Expression (D);
10109          Set_Delta_Value (Def_Id, Expr_Value_R (D));
10110
10111          --  Check that delta value is in range. Obviously we can do this
10112          --  at compile time, but it is strictly a runtime check, and of
10113          --  course there is an ACVC test that checks this!
10114
10115          if Delta_Value (Def_Id) < Delta_Value (T) then
10116             Error_Msg_N ("?delta value is too small", D);
10117             Rais :=
10118               Make_Raise_Constraint_Error (Sloc (D),
10119                 Reason => CE_Range_Check_Failed);
10120             Insert_Action (Declaration_Node (Def_Id), Rais);
10121          end if;
10122
10123          C := Range_Constraint (C);
10124
10125       --  No delta constraint present
10126
10127       else
10128          Set_Delta_Value (Def_Id, Delta_Value (T));
10129       end if;
10130
10131       --  Range constraint present
10132
10133       if Nkind (C) = N_Range_Constraint then
10134          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10135
10136       --  No range constraint present
10137
10138       else
10139          pragma Assert (No (C));
10140          Set_Scalar_Range (Def_Id, Scalar_Range (T));
10141
10142       end if;
10143
10144       Set_Discrete_RM_Size (Def_Id);
10145
10146       --  Unconditionally delay the freeze, since we cannot set size
10147       --  information in all cases correctly until the freeze point.
10148
10149       Set_Has_Delayed_Freeze (Def_Id);
10150    end Constrain_Ordinary_Fixed;
10151
10152    -----------------------
10153    -- Contain_Interface --
10154    -----------------------
10155
10156    function Contain_Interface
10157      (Iface  : Entity_Id;
10158       Ifaces : Elist_Id) return Boolean
10159    is
10160       Iface_Elmt : Elmt_Id;
10161
10162    begin
10163       if Present (Ifaces) then
10164          Iface_Elmt := First_Elmt (Ifaces);
10165          while Present (Iface_Elmt) loop
10166             if Node (Iface_Elmt) = Iface then
10167                return True;
10168             end if;
10169
10170             Next_Elmt (Iface_Elmt);
10171          end loop;
10172       end if;
10173
10174       return False;
10175    end Contain_Interface;
10176
10177    ---------------------------
10178    -- Convert_Scalar_Bounds --
10179    ---------------------------
10180
10181    procedure Convert_Scalar_Bounds
10182      (N            : Node_Id;
10183       Parent_Type  : Entity_Id;
10184       Derived_Type : Entity_Id;
10185       Loc          : Source_Ptr)
10186    is
10187       Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
10188
10189       Lo  : Node_Id;
10190       Hi  : Node_Id;
10191       Rng : Node_Id;
10192
10193    begin
10194       Lo := Build_Scalar_Bound
10195               (Type_Low_Bound (Derived_Type),
10196                Parent_Type, Implicit_Base);
10197
10198       Hi := Build_Scalar_Bound
10199               (Type_High_Bound (Derived_Type),
10200                Parent_Type, Implicit_Base);
10201
10202       Rng :=
10203         Make_Range (Loc,
10204           Low_Bound  => Lo,
10205           High_Bound => Hi);
10206
10207       Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
10208
10209       Set_Parent (Rng, N);
10210       Set_Scalar_Range (Derived_Type, Rng);
10211
10212       --  Analyze the bounds
10213
10214       Analyze_And_Resolve (Lo, Implicit_Base);
10215       Analyze_And_Resolve (Hi, Implicit_Base);
10216
10217       --  Analyze the range itself, except that we do not analyze it if
10218       --  the bounds are real literals, and we have a fixed-point type.
10219       --  The reason for this is that we delay setting the bounds in this
10220       --  case till we know the final Small and Size values (see circuit
10221       --  in Freeze.Freeze_Fixed_Point_Type for further details).
10222
10223       if Is_Fixed_Point_Type (Parent_Type)
10224         and then Nkind (Lo) = N_Real_Literal
10225         and then Nkind (Hi) = N_Real_Literal
10226       then
10227          return;
10228
10229       --  Here we do the analysis of the range
10230
10231       --  Note: we do this manually, since if we do a normal Analyze and
10232       --  Resolve call, there are problems with the conversions used for
10233       --  the derived type range.
10234
10235       else
10236          Set_Etype    (Rng, Implicit_Base);
10237          Set_Analyzed (Rng, True);
10238       end if;
10239    end Convert_Scalar_Bounds;
10240
10241    -------------------
10242    -- Copy_And_Swap --
10243    -------------------
10244
10245    procedure Copy_And_Swap (Priv, Full : Entity_Id) is
10246    begin
10247       --  Initialize new full declaration entity by copying the pertinent
10248       --  fields of the corresponding private declaration entity.
10249
10250       --  We temporarily set Ekind to a value appropriate for a type to
10251       --  avoid assert failures in Einfo from checking for setting type
10252       --  attributes on something that is not a type. Ekind (Priv) is an
10253       --  appropriate choice, since it allowed the attributes to be set
10254       --  in the first place. This Ekind value will be modified later.
10255
10256       Set_Ekind (Full, Ekind (Priv));
10257
10258       --  Also set Etype temporarily to Any_Type, again, in the absence
10259       --  of errors, it will be properly reset, and if there are errors,
10260       --  then we want a value of Any_Type to remain.
10261
10262       Set_Etype (Full, Any_Type);
10263
10264       --  Now start copying attributes
10265
10266       Set_Has_Discriminants          (Full, Has_Discriminants       (Priv));
10267
10268       if Has_Discriminants (Full) then
10269          Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
10270          Set_Stored_Constraint       (Full, Stored_Constraint       (Priv));
10271       end if;
10272
10273       Set_First_Rep_Item             (Full, First_Rep_Item          (Priv));
10274       Set_Homonym                    (Full, Homonym                 (Priv));
10275       Set_Is_Immediately_Visible     (Full, Is_Immediately_Visible  (Priv));
10276       Set_Is_Public                  (Full, Is_Public               (Priv));
10277       Set_Is_Pure                    (Full, Is_Pure                 (Priv));
10278       Set_Is_Tagged_Type             (Full, Is_Tagged_Type          (Priv));
10279       Set_Has_Pragma_Unreferenced    (Full, Has_Pragma_Unreferenced (Priv));
10280       Set_Has_Pragma_Unreferenced_Objects
10281                                      (Full, Has_Pragma_Unreferenced_Objects
10282                                                                     (Priv));
10283
10284       Conditional_Delay              (Full,                          Priv);
10285
10286       if Is_Tagged_Type (Full) then
10287          Set_Primitive_Operations    (Full, Primitive_Operations    (Priv));
10288
10289          if Priv = Base_Type (Priv) then
10290             Set_Class_Wide_Type      (Full, Class_Wide_Type         (Priv));
10291          end if;
10292       end if;
10293
10294       Set_Is_Volatile                (Full, Is_Volatile             (Priv));
10295       Set_Treat_As_Volatile          (Full, Treat_As_Volatile       (Priv));
10296       Set_Scope                      (Full, Scope                   (Priv));
10297       Set_Next_Entity                (Full, Next_Entity             (Priv));
10298       Set_First_Entity               (Full, First_Entity            (Priv));
10299       Set_Last_Entity                (Full, Last_Entity             (Priv));
10300
10301       --  If access types have been recorded for later handling, keep them in
10302       --  the full view so that they get handled when the full view freeze
10303       --  node is expanded.
10304
10305       if Present (Freeze_Node (Priv))
10306         and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
10307       then
10308          Ensure_Freeze_Node (Full);
10309          Set_Access_Types_To_Process
10310            (Freeze_Node (Full),
10311             Access_Types_To_Process (Freeze_Node (Priv)));
10312       end if;
10313
10314       --  Swap the two entities. Now Privat is the full type entity and
10315       --  Full is the private one. They will be swapped back at the end
10316       --  of the private part. This swapping ensures that the entity that
10317       --  is visible in the private part is the full declaration.
10318
10319       Exchange_Entities (Priv, Full);
10320       Append_Entity (Full, Scope (Full));
10321    end Copy_And_Swap;
10322
10323    -------------------------------------
10324    -- Copy_Array_Base_Type_Attributes --
10325    -------------------------------------
10326
10327    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
10328    begin
10329       Set_Component_Alignment      (T1, Component_Alignment      (T2));
10330       Set_Component_Type           (T1, Component_Type           (T2));
10331       Set_Component_Size           (T1, Component_Size           (T2));
10332       Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
10333       Set_Finalize_Storage_Only    (T1, Finalize_Storage_Only    (T2));
10334       Set_Has_Non_Standard_Rep     (T1, Has_Non_Standard_Rep     (T2));
10335       Set_Has_Task                 (T1, Has_Task                 (T2));
10336       Set_Is_Packed                (T1, Is_Packed                (T2));
10337       Set_Has_Aliased_Components   (T1, Has_Aliased_Components   (T2));
10338       Set_Has_Atomic_Components    (T1, Has_Atomic_Components    (T2));
10339       Set_Has_Volatile_Components  (T1, Has_Volatile_Components  (T2));
10340    end Copy_Array_Base_Type_Attributes;
10341
10342    -----------------------------------
10343    -- Copy_Array_Subtype_Attributes --
10344    -----------------------------------
10345
10346    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
10347    begin
10348       Set_Size_Info (T1, T2);
10349
10350       Set_First_Index          (T1, First_Index           (T2));
10351       Set_Is_Aliased           (T1, Is_Aliased            (T2));
10352       Set_Is_Atomic            (T1, Is_Atomic             (T2));
10353       Set_Is_Volatile          (T1, Is_Volatile           (T2));
10354       Set_Treat_As_Volatile    (T1, Treat_As_Volatile     (T2));
10355       Set_Is_Constrained       (T1, Is_Constrained        (T2));
10356       Set_Depends_On_Private   (T1, Has_Private_Component (T2));
10357       Set_First_Rep_Item       (T1, First_Rep_Item        (T2));
10358       Set_Convention           (T1, Convention            (T2));
10359       Set_Is_Limited_Composite (T1, Is_Limited_Composite  (T2));
10360       Set_Is_Private_Composite (T1, Is_Private_Composite  (T2));
10361    end Copy_Array_Subtype_Attributes;
10362
10363    -----------------------------------
10364    -- Create_Constrained_Components --
10365    -----------------------------------
10366
10367    procedure Create_Constrained_Components
10368      (Subt        : Entity_Id;
10369       Decl_Node   : Node_Id;
10370       Typ         : Entity_Id;
10371       Constraints : Elist_Id)
10372    is
10373       Loc         : constant Source_Ptr := Sloc (Subt);
10374       Comp_List   : constant Elist_Id   := New_Elmt_List;
10375       Parent_Type : constant Entity_Id  := Etype (Typ);
10376       Assoc_List  : constant List_Id    := New_List;
10377       Discr_Val   : Elmt_Id;
10378       Errors      : Boolean;
10379       New_C       : Entity_Id;
10380       Old_C       : Entity_Id;
10381       Is_Static   : Boolean := True;
10382
10383       procedure Collect_Fixed_Components (Typ : Entity_Id);
10384       --  Collect parent type components that do not appear in a variant part
10385
10386       procedure Create_All_Components;
10387       --  Iterate over Comp_List to create the components of the subtype
10388
10389       function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
10390       --  Creates a new component from Old_Compon, copying all the fields from
10391       --  it, including its Etype, inserts the new component in the Subt entity
10392       --  chain and returns the new component.
10393
10394       function Is_Variant_Record (T : Entity_Id) return Boolean;
10395       --  If true, and discriminants are static, collect only components from
10396       --  variants selected by discriminant values.
10397
10398       ------------------------------
10399       -- Collect_Fixed_Components --
10400       ------------------------------
10401
10402       procedure Collect_Fixed_Components (Typ : Entity_Id) is
10403       begin
10404       --  Build association list for discriminants, and find components of the
10405       --  variant part selected by the values of the discriminants.
10406
10407          Old_C := First_Discriminant (Typ);
10408          Discr_Val := First_Elmt (Constraints);
10409          while Present (Old_C) loop
10410             Append_To (Assoc_List,
10411               Make_Component_Association (Loc,
10412                  Choices    => New_List (New_Occurrence_Of (Old_C, Loc)),
10413                  Expression => New_Copy (Node (Discr_Val))));
10414
10415             Next_Elmt (Discr_Val);
10416             Next_Discriminant (Old_C);
10417          end loop;
10418
10419          --  The tag, and the possible parent and controller components
10420          --  are unconditionally in the subtype.
10421
10422          if Is_Tagged_Type (Typ)
10423            or else Has_Controlled_Component (Typ)
10424          then
10425             Old_C := First_Component (Typ);
10426             while Present (Old_C) loop
10427                if Chars ((Old_C)) = Name_uTag
10428                  or else Chars ((Old_C)) = Name_uParent
10429                  or else Chars ((Old_C)) = Name_uController
10430                then
10431                   Append_Elmt (Old_C, Comp_List);
10432                end if;
10433
10434                Next_Component (Old_C);
10435             end loop;
10436          end if;
10437       end Collect_Fixed_Components;
10438
10439       ---------------------------
10440       -- Create_All_Components --
10441       ---------------------------
10442
10443       procedure Create_All_Components is
10444          Comp : Elmt_Id;
10445
10446       begin
10447          Comp := First_Elmt (Comp_List);
10448          while Present (Comp) loop
10449             Old_C := Node (Comp);
10450             New_C := Create_Component (Old_C);
10451
10452             Set_Etype
10453               (New_C,
10454                Constrain_Component_Type
10455                  (Old_C, Subt, Decl_Node, Typ, Constraints));
10456             Set_Is_Public (New_C, Is_Public (Subt));
10457
10458             Next_Elmt (Comp);
10459          end loop;
10460       end Create_All_Components;
10461
10462       ----------------------
10463       -- Create_Component --
10464       ----------------------
10465
10466       function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
10467          New_Compon : constant Entity_Id := New_Copy (Old_Compon);
10468
10469       begin
10470          if Ekind (Old_Compon) = E_Discriminant
10471            and then Is_Completely_Hidden (Old_Compon)
10472          then
10473             --  This is a shadow discriminant created for a discriminant of
10474             --  the parent type that is one of several renamed by the same
10475             --  new discriminant. Give the shadow discriminant an internal
10476             --  name that cannot conflict with that of visible components.
10477
10478             Set_Chars (New_Compon, New_Internal_Name ('C'));
10479          end if;
10480
10481          --  Set the parent so we have a proper link for freezing etc. This is
10482          --  not a real parent pointer, since of course our parent does not own
10483          --  up to us and reference us, we are an illegitimate child of the
10484          --  original parent!
10485
10486          Set_Parent (New_Compon, Parent (Old_Compon));
10487
10488          --  If the old component's Esize was already determined and is a
10489          --  static value, then the new component simply inherits it. Otherwise
10490          --  the old component's size may require run-time determination, but
10491          --  the new component's size still might be statically determinable
10492          --  (if, for example it has a static constraint). In that case we want
10493          --  Layout_Type to recompute the component's size, so we reset its
10494          --  size and positional fields.
10495
10496          if Frontend_Layout_On_Target
10497            and then not Known_Static_Esize (Old_Compon)
10498          then
10499             Set_Esize (New_Compon, Uint_0);
10500             Init_Normalized_First_Bit    (New_Compon);
10501             Init_Normalized_Position     (New_Compon);
10502             Init_Normalized_Position_Max (New_Compon);
10503          end if;
10504
10505          --  We do not want this node marked as Comes_From_Source, since
10506          --  otherwise it would get first class status and a separate cross-
10507          --  reference line would be generated. Illegitimate children do not
10508          --  rate such recognition.
10509
10510          Set_Comes_From_Source (New_Compon, False);
10511
10512          --  But it is a real entity, and a birth certificate must be properly
10513          --  registered by entering it into the entity list.
10514
10515          Enter_Name (New_Compon);
10516
10517          return New_Compon;
10518       end Create_Component;
10519
10520       -----------------------
10521       -- Is_Variant_Record --
10522       -----------------------
10523
10524       function Is_Variant_Record (T : Entity_Id) return Boolean is
10525       begin
10526          return Nkind (Parent (T)) = N_Full_Type_Declaration
10527            and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
10528            and then Present (Component_List (Type_Definition (Parent (T))))
10529            and then
10530              Present
10531                (Variant_Part (Component_List (Type_Definition (Parent (T)))));
10532       end Is_Variant_Record;
10533
10534    --  Start of processing for Create_Constrained_Components
10535
10536    begin
10537       pragma Assert (Subt /= Base_Type (Subt));
10538       pragma Assert (Typ = Base_Type (Typ));
10539
10540       Set_First_Entity (Subt, Empty);
10541       Set_Last_Entity  (Subt, Empty);
10542
10543       --  Check whether constraint is fully static, in which case we can
10544       --  optimize the list of components.
10545
10546       Discr_Val := First_Elmt (Constraints);
10547       while Present (Discr_Val) loop
10548          if not Is_OK_Static_Expression (Node (Discr_Val)) then
10549             Is_Static := False;
10550             exit;
10551          end if;
10552
10553          Next_Elmt (Discr_Val);
10554       end loop;
10555
10556       Set_Has_Static_Discriminants (Subt, Is_Static);
10557
10558       Push_Scope (Subt);
10559
10560       --  Inherit the discriminants of the parent type
10561
10562       Add_Discriminants : declare
10563          Num_Disc : Int;
10564          Num_Gird : Int;
10565
10566       begin
10567          Num_Disc := 0;
10568          Old_C := First_Discriminant (Typ);
10569
10570          while Present (Old_C) loop
10571             Num_Disc := Num_Disc + 1;
10572             New_C := Create_Component (Old_C);
10573             Set_Is_Public (New_C, Is_Public (Subt));
10574             Next_Discriminant (Old_C);
10575          end loop;
10576
10577          --  For an untagged derived subtype, the number of discriminants may
10578          --  be smaller than the number of inherited discriminants, because
10579          --  several of them may be renamed by a single new discriminant.
10580          --  In this case, add the hidden discriminants back into the subtype,
10581          --  because otherwise the size of the subtype is computed incorrectly
10582          --  in GCC 4.1.
10583
10584          Num_Gird := 0;
10585
10586          if Is_Derived_Type (Typ)
10587            and then not Is_Tagged_Type (Typ)
10588          then
10589             Old_C := First_Stored_Discriminant (Typ);
10590
10591             while Present (Old_C) loop
10592                Num_Gird := Num_Gird + 1;
10593                Next_Stored_Discriminant (Old_C);
10594             end loop;
10595          end if;
10596
10597          if Num_Gird > Num_Disc then
10598
10599             --  Find out multiple uses of new discriminants, and add hidden
10600             --  components for the extra renamed discriminants. We recognize
10601             --  multiple uses through the Corresponding_Discriminant of a
10602             --  new discriminant: if it constrains several old discriminants,
10603             --  this field points to the last one in the parent type. The
10604             --  stored discriminants of the derived type have the same name
10605             --  as those of the parent.
10606
10607             declare
10608                Constr    : Elmt_Id;
10609                New_Discr : Entity_Id;
10610                Old_Discr : Entity_Id;
10611
10612             begin
10613                Constr    := First_Elmt (Stored_Constraint (Typ));
10614                Old_Discr := First_Stored_Discriminant (Typ);
10615                while Present (Constr) loop
10616                   if Is_Entity_Name (Node (Constr))
10617                     and then Ekind (Entity (Node (Constr))) = E_Discriminant
10618                   then
10619                      New_Discr := Entity (Node (Constr));
10620
10621                      if Chars (Corresponding_Discriminant (New_Discr)) /=
10622                         Chars (Old_Discr)
10623                      then
10624                         --  The new discriminant has been used to rename a
10625                         --  subsequent old discriminant. Introduce a shadow
10626                         --  component for the current old discriminant.
10627
10628                         New_C := Create_Component (Old_Discr);
10629                         Set_Original_Record_Component  (New_C, Old_Discr);
10630                      end if;
10631                   end if;
10632
10633                   Next_Elmt (Constr);
10634                   Next_Stored_Discriminant (Old_Discr);
10635                end loop;
10636             end;
10637          end if;
10638       end Add_Discriminants;
10639
10640       if Is_Static
10641         and then Is_Variant_Record (Typ)
10642       then
10643          Collect_Fixed_Components (Typ);
10644
10645          Gather_Components (
10646            Typ,
10647            Component_List (Type_Definition (Parent (Typ))),
10648            Governed_By   => Assoc_List,
10649            Into          => Comp_List,
10650            Report_Errors => Errors);
10651          pragma Assert (not Errors);
10652
10653          Create_All_Components;
10654
10655       --  If the subtype declaration is created for a tagged type derivation
10656       --  with constraints, we retrieve the record definition of the parent
10657       --  type to select the components of the proper variant.
10658
10659       elsif Is_Static
10660         and then Is_Tagged_Type (Typ)
10661         and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
10662         and then
10663           Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
10664         and then Is_Variant_Record (Parent_Type)
10665       then
10666          Collect_Fixed_Components (Typ);
10667
10668          Gather_Components (
10669            Typ,
10670            Component_List (Type_Definition (Parent (Parent_Type))),
10671            Governed_By   => Assoc_List,
10672            Into          => Comp_List,
10673            Report_Errors => Errors);
10674          pragma Assert (not Errors);
10675
10676          --  If the tagged derivation has a type extension, collect all the
10677          --  new components therein.
10678
10679          if Present
10680               (Record_Extension_Part (Type_Definition (Parent (Typ))))
10681          then
10682             Old_C := First_Component (Typ);
10683             while Present (Old_C) loop
10684                if Original_Record_Component (Old_C) = Old_C
10685                 and then Chars (Old_C) /= Name_uTag
10686                 and then Chars (Old_C) /= Name_uParent
10687                 and then Chars (Old_C) /= Name_uController
10688                then
10689                   Append_Elmt (Old_C, Comp_List);
10690                end if;
10691
10692                Next_Component (Old_C);
10693             end loop;
10694          end if;
10695
10696          Create_All_Components;
10697
10698       else
10699          --  If discriminants are not static, or if this is a multi-level type
10700          --  extension, we have to include all components of the parent type.
10701
10702          Old_C := First_Component (Typ);
10703          while Present (Old_C) loop
10704             New_C := Create_Component (Old_C);
10705
10706             Set_Etype
10707               (New_C,
10708                Constrain_Component_Type
10709                  (Old_C, Subt, Decl_Node, Typ, Constraints));
10710             Set_Is_Public (New_C, Is_Public (Subt));
10711
10712             Next_Component (Old_C);
10713          end loop;
10714       end if;
10715
10716       End_Scope;
10717    end Create_Constrained_Components;
10718
10719    ------------------------------------------
10720    -- Decimal_Fixed_Point_Type_Declaration --
10721    ------------------------------------------
10722
10723    procedure Decimal_Fixed_Point_Type_Declaration
10724      (T   : Entity_Id;
10725       Def : Node_Id)
10726    is
10727       Loc           : constant Source_Ptr := Sloc (Def);
10728       Digs_Expr     : constant Node_Id    := Digits_Expression (Def);
10729       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
10730       Implicit_Base : Entity_Id;
10731       Digs_Val      : Uint;
10732       Delta_Val     : Ureal;
10733       Scale_Val     : Uint;
10734       Bound_Val     : Ureal;
10735
10736    --  Start of processing for Decimal_Fixed_Point_Type_Declaration
10737
10738    begin
10739       Check_Restriction (No_Fixed_Point, Def);
10740
10741       --  Create implicit base type
10742
10743       Implicit_Base :=
10744         Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
10745       Set_Etype (Implicit_Base, Implicit_Base);
10746
10747       --  Analyze and process delta expression
10748
10749       Analyze_And_Resolve (Delta_Expr, Universal_Real);
10750
10751       Check_Delta_Expression (Delta_Expr);
10752       Delta_Val := Expr_Value_R (Delta_Expr);
10753
10754       --  Check delta is power of 10, and determine scale value from it
10755
10756       declare
10757          Val : Ureal;
10758
10759       begin
10760          Scale_Val := Uint_0;
10761          Val := Delta_Val;
10762
10763          if Val < Ureal_1 then
10764             while Val < Ureal_1 loop
10765                Val := Val * Ureal_10;
10766                Scale_Val := Scale_Val + 1;
10767             end loop;
10768
10769             if Scale_Val > 18 then
10770                Error_Msg_N ("scale exceeds maximum value of 18", Def);
10771                Scale_Val := UI_From_Int (+18);
10772             end if;
10773
10774          else
10775             while Val > Ureal_1 loop
10776                Val := Val / Ureal_10;
10777                Scale_Val := Scale_Val - 1;
10778             end loop;
10779
10780             if Scale_Val < -18 then
10781                Error_Msg_N ("scale is less than minimum value of -18", Def);
10782                Scale_Val := UI_From_Int (-18);
10783             end if;
10784          end if;
10785
10786          if Val /= Ureal_1 then
10787             Error_Msg_N ("delta expression must be a power of 10", Def);
10788             Delta_Val := Ureal_10 ** (-Scale_Val);
10789          end if;
10790       end;
10791
10792       --  Set delta, scale and small (small = delta for decimal type)
10793
10794       Set_Delta_Value (Implicit_Base, Delta_Val);
10795       Set_Scale_Value (Implicit_Base, Scale_Val);
10796       Set_Small_Value (Implicit_Base, Delta_Val);
10797
10798       --  Analyze and process digits expression
10799
10800       Analyze_And_Resolve (Digs_Expr, Any_Integer);
10801       Check_Digits_Expression (Digs_Expr);
10802       Digs_Val := Expr_Value (Digs_Expr);
10803
10804       if Digs_Val > 18 then
10805          Digs_Val := UI_From_Int (+18);
10806          Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
10807       end if;
10808
10809       Set_Digits_Value (Implicit_Base, Digs_Val);
10810       Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
10811
10812       --  Set range of base type from digits value for now. This will be
10813       --  expanded to represent the true underlying base range by Freeze.
10814
10815       Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
10816
10817       --  Set size to zero for now, size will be set at freeze time. We have
10818       --  to do this for ordinary fixed-point, because the size depends on
10819       --  the specified small, and we might as well do the same for decimal
10820       --  fixed-point.
10821
10822       Init_Size_Align (Implicit_Base);
10823
10824       --  If there are bounds given in the declaration use them as the
10825       --  bounds of the first named subtype.
10826
10827       if Present (Real_Range_Specification (Def)) then
10828          declare
10829             RRS      : constant Node_Id := Real_Range_Specification (Def);
10830             Low      : constant Node_Id := Low_Bound (RRS);
10831             High     : constant Node_Id := High_Bound (RRS);
10832             Low_Val  : Ureal;
10833             High_Val : Ureal;
10834
10835          begin
10836             Analyze_And_Resolve (Low, Any_Real);
10837             Analyze_And_Resolve (High, Any_Real);
10838             Check_Real_Bound (Low);
10839             Check_Real_Bound (High);
10840             Low_Val := Expr_Value_R (Low);
10841             High_Val := Expr_Value_R (High);
10842
10843             if Low_Val < (-Bound_Val) then
10844                Error_Msg_N
10845                  ("range low bound too small for digits value", Low);
10846                Low_Val := -Bound_Val;
10847             end if;
10848
10849             if High_Val > Bound_Val then
10850                Error_Msg_N
10851                  ("range high bound too large for digits value", High);
10852                High_Val := Bound_Val;
10853             end if;
10854
10855             Set_Fixed_Range (T, Loc, Low_Val, High_Val);
10856          end;
10857
10858       --  If no explicit range, use range that corresponds to given
10859       --  digits value. This will end up as the final range for the
10860       --  first subtype.
10861
10862       else
10863          Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
10864       end if;
10865
10866       --  Complete entity for first subtype
10867
10868       Set_Ekind          (T, E_Decimal_Fixed_Point_Subtype);
10869       Set_Etype          (T, Implicit_Base);
10870       Set_Size_Info      (T, Implicit_Base);
10871       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
10872       Set_Digits_Value   (T, Digs_Val);
10873       Set_Delta_Value    (T, Delta_Val);
10874       Set_Small_Value    (T, Delta_Val);
10875       Set_Scale_Value    (T, Scale_Val);
10876       Set_Is_Constrained (T);
10877    end Decimal_Fixed_Point_Type_Declaration;
10878
10879    ----------------------------------
10880    -- Derive_Interface_Subprograms --
10881    ----------------------------------
10882
10883    procedure Derive_Interface_Subprograms
10884      (Parent_Type : Entity_Id;
10885       Tagged_Type : Entity_Id;
10886       Ifaces_List : Elist_Id)
10887    is
10888       function Collect_Interface_Primitives
10889         (Tagged_Type : Entity_Id) return Elist_Id;
10890       --  Ada 2005 (AI-251): Collect the primitives of all the implemented
10891       --  interfaces.
10892
10893       function In_List (L : Elist_Id; Subp : Entity_Id) return Boolean;
10894       --  Determine if Subp already in the list L
10895
10896       procedure Remove_Homonym (E : Entity_Id);
10897       --  Removes E from the homonym chain
10898
10899       ----------------------------------
10900       -- Collect_Interface_Primitives --
10901       ----------------------------------
10902
10903       function Collect_Interface_Primitives
10904          (Tagged_Type : Entity_Id) return Elist_Id
10905       is
10906          Op_List     : constant Elist_Id := New_Elmt_List;
10907          Elmt        : Elmt_Id;
10908          Ifaces_List : Elist_Id;
10909          Iface_Elmt  : Elmt_Id;
10910          Prim        : Entity_Id;
10911
10912       begin
10913          pragma Assert (Is_Tagged_Type (Tagged_Type)
10914            and then Has_Abstract_Interfaces (Tagged_Type));
10915
10916          Collect_Abstract_Interfaces (Tagged_Type, Ifaces_List);
10917
10918          Iface_Elmt := First_Elmt (Ifaces_List);
10919          while Present (Iface_Elmt) loop
10920             Elmt := First_Elmt (Primitive_Operations (Node (Iface_Elmt)));
10921
10922             while Present (Elmt) loop
10923                Prim := Node (Elmt);
10924
10925                if not Is_Predefined_Dispatching_Operation (Prim) then
10926                   Append_Elmt (Prim, Op_List);
10927                end if;
10928
10929                Next_Elmt (Elmt);
10930             end loop;
10931
10932             Next_Elmt (Iface_Elmt);
10933          end loop;
10934
10935          return Op_List;
10936       end Collect_Interface_Primitives;
10937
10938       -------------
10939       -- In_List --
10940       -------------
10941
10942       function In_List (L : Elist_Id; Subp : Entity_Id) return Boolean is
10943          Elmt : Elmt_Id;
10944       begin
10945          Elmt := First_Elmt (L);
10946          while Present (Elmt) loop
10947             if Node (Elmt) = Subp then
10948                return True;
10949             end if;
10950
10951             Next_Elmt (Elmt);
10952          end loop;
10953
10954          return False;
10955       end In_List;
10956
10957       --------------------
10958       -- Remove_Homonym --
10959       --------------------
10960
10961       procedure Remove_Homonym (E : Entity_Id) is
10962          Prev  : Entity_Id := Empty;
10963          H     : Entity_Id;
10964
10965       begin
10966          if E = Current_Entity (E) then
10967             Set_Current_Entity (Homonym (E));
10968          else
10969             H := Current_Entity (E);
10970             while Present (H) and then H /= E loop
10971                Prev := H;
10972                H    := Homonym (H);
10973             end loop;
10974
10975             Set_Homonym (Prev, Homonym (E));
10976          end if;
10977       end Remove_Homonym;
10978
10979       --  Local Variables
10980
10981       E           : Entity_Id;
10982       Elmt        : Elmt_Id;
10983       Iface       : Entity_Id;
10984       Iface_Subp  : Entity_Id;
10985       New_Subp    : Entity_Id := Empty;
10986       Op_List     : Elist_Id;
10987       Parent_Base : Entity_Id;
10988       Subp        : Entity_Id;
10989
10990    --  Start of processing for Derive_Interface_Subprograms
10991
10992    begin
10993       if Ada_Version < Ada_05
10994         or else not Is_Record_Type (Tagged_Type)
10995         or else not Is_Tagged_Type (Tagged_Type)
10996         or else not Has_Abstract_Interfaces (Tagged_Type)
10997       then
10998          return;
10999       end if;
11000
11001       --  Add to the list of interface subprograms all the primitives inherited
11002       --  from abstract interfaces that are not immediate ancestors and also
11003       --  add their derivation to the list of interface primitives.
11004
11005       Op_List := Collect_Interface_Primitives (Tagged_Type);
11006
11007       Elmt := First_Elmt (Op_List);
11008       while Present (Elmt) loop
11009          Subp  := Node (Elmt);
11010          Iface := Find_Dispatching_Type (Subp);
11011
11012          if Is_Concurrent_Record_Type (Tagged_Type) then
11013             if not Present (Abstract_Interface_Alias (Subp)) then
11014                Derive_Subprogram (New_Subp, Subp, Tagged_Type, Iface);
11015                Append_Elmt (New_Subp, Ifaces_List);
11016             end if;
11017
11018          elsif not Is_Parent (Iface, Tagged_Type) then
11019             Derive_Subprogram (New_Subp, Subp, Tagged_Type, Iface);
11020             Append_Elmt (New_Subp, Ifaces_List);
11021          end if;
11022
11023          Next_Elmt (Elmt);
11024       end loop;
11025
11026       --  Complete the derivation of the interface subprograms. Assignate to
11027       --  each entity associated with abstract interfaces their aliased entity
11028       --  and complete their decoration as hidden interface entities that will
11029       --  be used later to build the secondary dispatch tables.
11030
11031       if not Is_Empty_Elmt_List (Ifaces_List) then
11032          if Ekind (Parent_Type) = E_Record_Type_With_Private
11033            and then Has_Discriminants (Parent_Type)
11034            and then Present (Full_View (Parent_Type))
11035          then
11036             Parent_Base := Full_View (Parent_Type);
11037          else
11038             Parent_Base := Parent_Type;
11039          end if;
11040
11041          Elmt := First_Elmt (Ifaces_List);
11042          while Present (Elmt) loop
11043             Iface_Subp := Node (Elmt);
11044
11045             --  Look for the first overriding entity in the homonym chain.
11046             --  In this way if we are in the private part of a package spec
11047             --  we get the last overriding subprogram.
11048
11049             E  := Current_Entity_In_Scope (Iface_Subp);
11050             while Present (E) loop
11051                if Is_Dispatching_Operation (E)
11052                  and then Scope (E) = Scope (Iface_Subp)
11053                  and then Type_Conformant (E, Iface_Subp)
11054                  and then not In_List (Ifaces_List, E)
11055                then
11056                   exit;
11057                end if;
11058
11059                E := Homonym (E);
11060             end loop;
11061
11062             --  Create an overriding entity if not found in the homonym chain
11063
11064             if not Present (E) then
11065                Derive_Subprogram
11066                  (E, Alias (Iface_Subp), Tagged_Type, Parent_Base);
11067
11068             elsif not In_List (Primitive_Operations (Tagged_Type), E) then
11069
11070                --  Inherit the operation from the private view
11071
11072                Append_Elmt (E, Primitive_Operations (Tagged_Type));
11073             end if;
11074
11075             --  Complete the decoration of the hidden interface entity
11076
11077             Set_Is_Hidden                (Iface_Subp);
11078             Set_Abstract_Interface_Alias (Iface_Subp, Alias (Iface_Subp));
11079             Set_Alias                    (Iface_Subp, E);
11080             Set_Is_Abstract_Subprogram   (Iface_Subp,
11081                                           Is_Abstract_Subprogram (E));
11082             Remove_Homonym               (Iface_Subp);
11083
11084             --  Hidden entities associated with interfaces must have set the
11085             --  Has_Delay_Freeze attribute to ensure that the corresponding
11086             --  entry of the secondary dispatch table is filled when such
11087             --  entity is frozen.
11088
11089             Set_Has_Delayed_Freeze (Iface_Subp);
11090
11091             Next_Elmt (Elmt);
11092          end loop;
11093       end if;
11094    end Derive_Interface_Subprograms;
11095
11096    -----------------------
11097    -- Derive_Subprogram --
11098    -----------------------
11099
11100    procedure Derive_Subprogram
11101      (New_Subp     : in out Entity_Id;
11102       Parent_Subp  : Entity_Id;
11103       Derived_Type : Entity_Id;
11104       Parent_Type  : Entity_Id;
11105       Actual_Subp  : Entity_Id := Empty)
11106    is
11107       Formal       : Entity_Id;
11108       New_Formal   : Entity_Id;
11109       Visible_Subp : Entity_Id := Parent_Subp;
11110
11111       function Is_Private_Overriding return Boolean;
11112       --  If Subp is a private overriding of a visible operation, the in-
11113       --  herited operation derives from the overridden op (even though
11114       --  its body is the overriding one) and the inherited operation is
11115       --  visible now. See sem_disp to see the details of the handling of
11116       --  the overridden subprogram, which is removed from the list of
11117       --  primitive operations of the type. The overridden subprogram is
11118       --  saved locally in Visible_Subp, and used to diagnose abstract
11119       --  operations that need overriding in the derived type.
11120
11121       procedure Replace_Type (Id, New_Id : Entity_Id);
11122       --  When the type is an anonymous access type, create a new access type
11123       --  designating the derived type.
11124
11125       procedure Set_Derived_Name;
11126       --  This procedure sets the appropriate Chars name for New_Subp. This
11127       --  is normally just a copy of the parent name. An exception arises for
11128       --  type support subprograms, where the name is changed to reflect the
11129       --  name of the derived type, e.g. if type foo is derived from type bar,
11130       --  then a procedure barDA is derived with a name fooDA.
11131
11132       ---------------------------
11133       -- Is_Private_Overriding --
11134       ---------------------------
11135
11136       function Is_Private_Overriding return Boolean is
11137          Prev : Entity_Id;
11138
11139       begin
11140          --  If the parent is not a dispatching operation there is no
11141          --  need to investigate overridings
11142
11143          if not Is_Dispatching_Operation (Parent_Subp) then
11144             return False;
11145          end if;
11146
11147          --  The visible operation that is overridden is a homonym of the
11148          --  parent subprogram. We scan the homonym chain to find the one
11149          --  whose alias is the subprogram we are deriving.
11150
11151          Prev := Current_Entity (Parent_Subp);
11152          while Present (Prev) loop
11153             if Ekind (Prev) = Ekind (Parent_Subp)
11154               and then Alias (Prev) = Parent_Subp
11155               and then Scope (Parent_Subp) = Scope (Prev)
11156               and then not Is_Hidden (Prev)
11157             then
11158                Visible_Subp := Prev;
11159                return True;
11160             end if;
11161
11162             Prev := Homonym (Prev);
11163          end loop;
11164
11165          return False;
11166       end Is_Private_Overriding;
11167
11168       ------------------
11169       -- Replace_Type --
11170       ------------------
11171
11172       procedure Replace_Type (Id, New_Id : Entity_Id) is
11173          Acc_Type : Entity_Id;
11174          Par      : constant Node_Id := Parent (Derived_Type);
11175
11176       begin
11177          --  When the type is an anonymous access type, create a new access
11178          --  type designating the derived type. This itype must be elaborated
11179          --  at the point of the derivation, not on subsequent calls that may
11180          --  be out of the proper scope for Gigi, so we insert a reference to
11181          --  it after the derivation.
11182
11183          if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
11184             declare
11185                Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
11186
11187             begin
11188                if Ekind (Desig_Typ) = E_Record_Type_With_Private
11189                  and then Present (Full_View (Desig_Typ))
11190                  and then not Is_Private_Type (Parent_Type)
11191                then
11192                   Desig_Typ := Full_View (Desig_Typ);
11193                end if;
11194
11195                if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
11196
11197                   --  Ada 2005 (AI-251): Handle also derivations of abstract
11198                   --  interface primitives.
11199
11200                  or else (Is_Interface (Desig_Typ)
11201                           and then not Is_Class_Wide_Type (Desig_Typ))
11202                then
11203                   Acc_Type := New_Copy (Etype (Id));
11204                   Set_Etype (Acc_Type, Acc_Type);
11205                   Set_Scope (Acc_Type, New_Subp);
11206
11207                   --  Compute size of anonymous access type
11208
11209                   if Is_Array_Type (Desig_Typ)
11210                     and then not Is_Constrained (Desig_Typ)
11211                   then
11212                      Init_Size (Acc_Type, 2 * System_Address_Size);
11213                   else
11214                      Init_Size (Acc_Type, System_Address_Size);
11215                   end if;
11216
11217                   Init_Alignment (Acc_Type);
11218                   Set_Directly_Designated_Type (Acc_Type, Derived_Type);
11219
11220                   Set_Etype (New_Id, Acc_Type);
11221                   Set_Scope (New_Id, New_Subp);
11222
11223                   --  Create a reference to it
11224                   Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
11225
11226                else
11227                   Set_Etype (New_Id, Etype (Id));
11228                end if;
11229             end;
11230
11231          elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
11232            or else
11233              (Ekind (Etype (Id)) = E_Record_Type_With_Private
11234                and then Present (Full_View (Etype (Id)))
11235                and then
11236                  Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
11237          then
11238             --  Constraint checks on formals are generated during expansion,
11239             --  based on the signature of the original subprogram. The bounds
11240             --  of the derived type are not relevant, and thus we can use
11241             --  the base type for the formals. However, the return type may be
11242             --  used in a context that requires that the proper static bounds
11243             --  be used (a case statement, for example)  and for those cases
11244             --  we must use the derived type (first subtype), not its base.
11245
11246             --  If the derived_type_definition has no constraints, we know that
11247             --  the derived type has the same constraints as the first subtype
11248             --  of the parent, and we can also use it rather than its base,
11249             --  which can lead to more efficient code.
11250
11251             if Etype (Id) = Parent_Type then
11252                if Is_Scalar_Type (Parent_Type)
11253                  and then
11254                    Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
11255                then
11256                   Set_Etype (New_Id, Derived_Type);
11257
11258                elsif Nkind (Par) = N_Full_Type_Declaration
11259                  and then
11260                    Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
11261                  and then
11262                    Is_Entity_Name
11263                      (Subtype_Indication (Type_Definition (Par)))
11264                then
11265                   Set_Etype (New_Id, Derived_Type);
11266
11267                else
11268                   Set_Etype (New_Id, Base_Type (Derived_Type));
11269                end if;
11270
11271             else
11272                Set_Etype (New_Id, Base_Type (Derived_Type));
11273             end if;
11274
11275          --  Ada 2005 (AI-251): Handle derivations of abstract interface
11276          --  primitives.
11277
11278          elsif Is_Interface (Etype (Id))
11279            and then not Is_Class_Wide_Type (Etype (Id))
11280          then
11281             Set_Etype (New_Id, Derived_Type);
11282
11283          else
11284             Set_Etype (New_Id, Etype (Id));
11285          end if;
11286       end Replace_Type;
11287
11288       ----------------------
11289       -- Set_Derived_Name --
11290       ----------------------
11291
11292       procedure Set_Derived_Name is
11293          Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
11294       begin
11295          if Nm = TSS_Null then
11296             Set_Chars (New_Subp, Chars (Parent_Subp));
11297          else
11298             Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
11299          end if;
11300       end Set_Derived_Name;
11301
11302    --  Start of processing for Derive_Subprogram
11303
11304    begin
11305       New_Subp :=
11306          New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
11307       Set_Ekind (New_Subp, Ekind (Parent_Subp));
11308
11309       --  Check whether the inherited subprogram is a private operation that
11310       --  should be inherited but not yet made visible. Such subprograms can
11311       --  become visible at a later point (e.g., the private part of a public
11312       --  child unit) via Declare_Inherited_Private_Subprograms. If the
11313       --  following predicate is true, then this is not such a private
11314       --  operation and the subprogram simply inherits the name of the parent
11315       --  subprogram. Note the special check for the names of controlled
11316       --  operations, which are currently exempted from being inherited with
11317       --  a hidden name because they must be findable for generation of
11318       --  implicit run-time calls.
11319
11320       if not Is_Hidden (Parent_Subp)
11321         or else Is_Internal (Parent_Subp)
11322         or else Is_Private_Overriding
11323         or else Is_Internal_Name (Chars (Parent_Subp))
11324         or else Chars (Parent_Subp) = Name_Initialize
11325         or else Chars (Parent_Subp) = Name_Adjust
11326         or else Chars (Parent_Subp) = Name_Finalize
11327       then
11328          Set_Derived_Name;
11329
11330       --  If parent is hidden, this can be a regular derivation if the
11331       --  parent is immediately visible in a non-instantiating context,
11332       --  or if we are in the private part of an instance. This test
11333       --  should still be refined ???
11334
11335       --  The test for In_Instance_Not_Visible avoids inheriting the derived
11336       --  operation as a non-visible operation in cases where the parent
11337       --  subprogram might not be visible now, but was visible within the
11338       --  original generic, so it would be wrong to make the inherited
11339       --  subprogram non-visible now. (Not clear if this test is fully
11340       --  correct; are there any cases where we should declare the inherited
11341       --  operation as not visible to avoid it being overridden, e.g., when
11342       --  the parent type is a generic actual with private primitives ???)
11343
11344       --  (they should be treated the same as other private inherited
11345       --  subprograms, but it's not clear how to do this cleanly). ???
11346
11347       elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
11348               and then Is_Immediately_Visible (Parent_Subp)
11349               and then not In_Instance)
11350         or else In_Instance_Not_Visible
11351       then
11352          Set_Derived_Name;
11353
11354       --  Ada 2005 (AI-251): Hidden entity associated with abstract interface
11355       --  primitive
11356
11357       elsif Present (Abstract_Interface_Alias (Parent_Subp)) then
11358          Set_Derived_Name;
11359
11360       --  The type is inheriting a private operation, so enter
11361       --  it with a special name so it can't be overridden.
11362
11363       else
11364          Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
11365       end if;
11366
11367       Set_Parent (New_Subp, Parent (Derived_Type));
11368       Replace_Type (Parent_Subp, New_Subp);
11369       Conditional_Delay (New_Subp, Parent_Subp);
11370
11371       Formal := First_Formal (Parent_Subp);
11372       while Present (Formal) loop
11373          New_Formal := New_Copy (Formal);
11374
11375          --  Normally we do not go copying parents, but in the case of
11376          --  formals, we need to link up to the declaration (which is the
11377          --  parameter specification), and it is fine to link up to the
11378          --  original formal's parameter specification in this case.
11379
11380          Set_Parent (New_Formal, Parent (Formal));
11381
11382          Append_Entity (New_Formal, New_Subp);
11383
11384          Replace_Type (Formal, New_Formal);
11385          Next_Formal (Formal);
11386       end loop;
11387
11388       --  If this derivation corresponds to a tagged generic actual, then
11389       --  primitive operations rename those of the actual. Otherwise the
11390       --  primitive operations rename those of the parent type, If the
11391       --  parent renames an intrinsic operator, so does the new subprogram.
11392       --  We except concatenation, which is always properly typed, and does
11393       --  not get expanded as other intrinsic operations.
11394
11395       if No (Actual_Subp) then
11396          if Is_Intrinsic_Subprogram (Parent_Subp) then
11397             Set_Is_Intrinsic_Subprogram (New_Subp);
11398
11399             if Present (Alias (Parent_Subp))
11400               and then Chars (Parent_Subp) /= Name_Op_Concat
11401             then
11402                Set_Alias (New_Subp, Alias (Parent_Subp));
11403             else
11404                Set_Alias (New_Subp, Parent_Subp);
11405             end if;
11406
11407          else
11408             Set_Alias (New_Subp, Parent_Subp);
11409          end if;
11410
11411       else
11412          Set_Alias (New_Subp, Actual_Subp);
11413       end if;
11414
11415       --  Derived subprograms of a tagged type must inherit the convention
11416       --  of the parent subprogram (a requirement of AI-117). Derived
11417       --  subprograms of untagged types simply get convention Ada by default.
11418
11419       if Is_Tagged_Type (Derived_Type) then
11420          Set_Convention (New_Subp, Convention (Parent_Subp));
11421       end if;
11422
11423       Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
11424       Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
11425
11426       if Ekind (Parent_Subp) = E_Procedure then
11427          Set_Is_Valued_Procedure
11428            (New_Subp, Is_Valued_Procedure (Parent_Subp));
11429       end if;
11430
11431       --  No_Return must be inherited properly. If this is overridden in the
11432       --  case of a dispatching operation, then a check is made in Sem_Disp
11433       --  that the overriding operation is also No_Return (no such check is
11434       --  required for the case of non-dispatching operation.
11435
11436       Set_No_Return (New_Subp, No_Return (Parent_Subp));
11437
11438       --  A derived function with a controlling result is abstract. If the
11439       --  Derived_Type is a nonabstract formal generic derived type, then
11440       --  inherited operations are not abstract: the required check is done at
11441       --  instantiation time. If the derivation is for a generic actual, the
11442       --  function is not abstract unless the actual is.
11443
11444       if Is_Generic_Type (Derived_Type)
11445         and then not Is_Abstract_Type (Derived_Type)
11446       then
11447          null;
11448
11449       --  Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
11450       --  properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
11451
11452       elsif Ada_Version >= Ada_05
11453         and then (Is_Abstract_Subprogram (Alias (New_Subp))
11454                    or else (Is_Tagged_Type (Derived_Type)
11455                             and then Etype (New_Subp) = Derived_Type
11456                             and then not Is_Null_Extension (Derived_Type))
11457                    or else (Is_Tagged_Type (Derived_Type)
11458                             and then Ekind (Etype (New_Subp)) =
11459                                                        E_Anonymous_Access_Type
11460                             and then Designated_Type (Etype (New_Subp)) =
11461                                                        Derived_Type
11462                             and then not Is_Null_Extension (Derived_Type)))
11463         and then No (Actual_Subp)
11464       then
11465          if not Is_Tagged_Type (Derived_Type)
11466            or else Is_Abstract_Type (Derived_Type)
11467            or else Is_Abstract_Subprogram (Alias (New_Subp))
11468          then
11469             Set_Is_Abstract_Subprogram (New_Subp);
11470          else
11471             Set_Requires_Overriding (New_Subp);
11472          end if;
11473
11474       elsif Ada_Version < Ada_05
11475         and then (Is_Abstract_Subprogram (Alias (New_Subp))
11476                    or else (Is_Tagged_Type (Derived_Type)
11477                              and then Etype (New_Subp) = Derived_Type
11478                              and then No (Actual_Subp)))
11479       then
11480          Set_Is_Abstract_Subprogram (New_Subp);
11481
11482       --  Finally, if the parent type is abstract we must verify that all
11483       --  inherited operations are either non-abstract or overridden, or
11484       --  that the derived type itself is abstract (this check is performed
11485       --  at the end of a package declaration, in Check_Abstract_Overriding).
11486       --  A private overriding in the parent type will not be visible in the
11487       --  derivation if we are not in an inner package or in a child unit of
11488       --  the parent type, in which case the abstractness of the inherited
11489       --  operation is carried to the new subprogram.
11490
11491       elsif Is_Abstract_Type (Parent_Type)
11492         and then not In_Open_Scopes (Scope (Parent_Type))
11493         and then Is_Private_Overriding
11494         and then Is_Abstract_Subprogram (Visible_Subp)
11495       then
11496          if No (Actual_Subp) then
11497             Set_Alias (New_Subp, Visible_Subp);
11498             Set_Is_Abstract_Subprogram
11499               (New_Subp, True);
11500          else
11501             --  If this is a derivation for an instance of a formal derived
11502             --  type, abstractness comes from the primitive operation of the
11503             --  actual, not from the operation inherited from the ancestor.
11504
11505             Set_Is_Abstract_Subprogram
11506               (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
11507          end if;
11508       end if;
11509
11510       New_Overloaded_Entity (New_Subp, Derived_Type);
11511
11512       --  Check for case of a derived subprogram for the instantiation of a
11513       --  formal derived tagged type, if so mark the subprogram as dispatching
11514       --  and inherit the dispatching attributes of the parent subprogram. The
11515       --  derived subprogram is effectively renaming of the actual subprogram,
11516       --  so it needs to have the same attributes as the actual.
11517
11518       if Present (Actual_Subp)
11519         and then Is_Dispatching_Operation (Parent_Subp)
11520       then
11521          Set_Is_Dispatching_Operation (New_Subp);
11522
11523          if Present (DTC_Entity (Parent_Subp)) then
11524             Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
11525             Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
11526          end if;
11527       end if;
11528
11529       --  Indicate that a derived subprogram does not require a body and that
11530       --  it does not require processing of default expressions.
11531
11532       Set_Has_Completion (New_Subp);
11533       Set_Default_Expressions_Processed (New_Subp);
11534
11535       if Ekind (New_Subp) = E_Function then
11536          Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
11537       end if;
11538    end Derive_Subprogram;
11539
11540    ------------------------
11541    -- Derive_Subprograms --
11542    ------------------------
11543
11544    procedure Derive_Subprograms
11545      (Parent_Type           : Entity_Id;
11546       Derived_Type          : Entity_Id;
11547       Generic_Actual        : Entity_Id := Empty)
11548    is
11549       Op_List      : constant Elist_Id :=
11550                        Collect_Primitive_Operations (Parent_Type);
11551       Ifaces_List  : constant Elist_Id := New_Elmt_List;
11552       Act_List     : Elist_Id;
11553       Act_Elmt     : Elmt_Id;
11554       Elmt         : Elmt_Id;
11555       New_Subp     : Entity_Id := Empty;
11556       Parent_Base  : Entity_Id;
11557       Subp         : Entity_Id;
11558
11559    begin
11560       if Ekind (Parent_Type) = E_Record_Type_With_Private
11561         and then Has_Discriminants (Parent_Type)
11562         and then Present (Full_View (Parent_Type))
11563       then
11564          Parent_Base := Full_View (Parent_Type);
11565       else
11566          Parent_Base := Parent_Type;
11567       end if;
11568
11569       --  Derive primitives inherited from the parent
11570
11571       if Present (Generic_Actual) then
11572          Act_List := Collect_Primitive_Operations (Generic_Actual);
11573          Act_Elmt := First_Elmt (Act_List);
11574       else
11575          Act_Elmt := No_Elmt;
11576       end if;
11577
11578       --  Literals are derived earlier in the process of building the derived
11579       --  type, and are skipped here.
11580
11581       Elmt := First_Elmt (Op_List);
11582       while Present (Elmt) loop
11583          Subp := Node (Elmt);
11584
11585          if Ekind (Subp) /= E_Enumeration_Literal then
11586
11587             if Ada_Version >= Ada_05
11588               and then Present (Abstract_Interface_Alias (Subp))
11589             then
11590                null;
11591
11592             elsif No (Generic_Actual) then
11593                Derive_Subprogram (New_Subp, Subp, Derived_Type, Parent_Base);
11594
11595                --  Ada 2005 (AI-251): Add the derivation of an abstract
11596                --  interface primitive to the list of entities to which
11597                --  we have to associate an aliased entity.
11598
11599                if Ada_Version >= Ada_05
11600                  and then Is_Dispatching_Operation (Subp)
11601                  and then Present (Find_Dispatching_Type (Subp))
11602                  and then Is_Interface (Find_Dispatching_Type (Subp))
11603                  and then not Is_Predefined_Dispatching_Operation (Subp)
11604                then
11605                   Append_Elmt (New_Subp, Ifaces_List);
11606                end if;
11607
11608             else
11609
11610                --  If the generic parent type is present, the derived type
11611                --  is an instance of a formal derived type, and within the
11612                --  instance its operations are those of the actual. We derive
11613                --  from the formal type but make the inherited operations
11614                --  aliases of the corresponding operations of the actual.
11615
11616                if Is_Interface (Parent_Type) then
11617
11618                   --  Find the corresponding operation in the generic actual.
11619                   --  Given that the actual is not a direct descendant of the
11620                   --  parent, as in Ada 95, the primitives are not necessarily
11621                   --  in the same order, so we have to traverse the list of
11622                   --  primitive operations of the actual to find the one that
11623                   --  implements the interface operation.
11624
11625                   Act_Elmt := First_Elmt (Act_List);
11626
11627                   while Present (Act_Elmt) loop
11628                      exit when
11629                        Abstract_Interface_Alias (Node (Act_Elmt)) = Subp;
11630                      Next_Elmt (Act_Elmt);
11631                   end loop;
11632                end if;
11633
11634                --  If the formal is not an interface, the actual is a direct
11635                --  descendant and the common  primitive operations appear in
11636                --  the same order.
11637
11638                Derive_Subprogram
11639                  (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
11640
11641                if Present (Act_Elmt) then
11642                   Next_Elmt (Act_Elmt);
11643                end if;
11644             end if;
11645          end if;
11646
11647          Next_Elmt (Elmt);
11648       end loop;
11649
11650       --  Inherit additional operations from progenitor interfaces.
11651       --  However, if the derived type is a generic actual, there
11652       --  are not new primitive operations for the type, because
11653       --  it has those of the actual, so nothing needs to be done.
11654       --  The renamings generated above are not primitive operations,
11655       --  and their purpose is simply to make the proper operations
11656       --  visible within an instantiation.
11657
11658       if Ada_Version >= Ada_05
11659         and then Is_Tagged_Type (Derived_Type)
11660         and then No (Generic_Actual)
11661       then
11662          Derive_Interface_Subprograms (Parent_Type, Derived_Type, Ifaces_List);
11663       end if;
11664    end Derive_Subprograms;
11665
11666    --------------------------------
11667    -- Derived_Standard_Character --
11668    --------------------------------
11669
11670    procedure Derived_Standard_Character
11671      (N             : Node_Id;
11672       Parent_Type   : Entity_Id;
11673       Derived_Type  : Entity_Id)
11674    is
11675       Loc           : constant Source_Ptr := Sloc (N);
11676       Def           : constant Node_Id    := Type_Definition (N);
11677       Indic         : constant Node_Id    := Subtype_Indication (Def);
11678       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
11679       Implicit_Base : constant Entity_Id  :=
11680                         Create_Itype
11681                           (E_Enumeration_Type, N, Derived_Type, 'B');
11682
11683       Lo : Node_Id;
11684       Hi : Node_Id;
11685
11686    begin
11687       Discard_Node (Process_Subtype (Indic, N));
11688
11689       Set_Etype     (Implicit_Base, Parent_Base);
11690       Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
11691       Set_RM_Size   (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
11692
11693       Set_Is_Character_Type  (Implicit_Base, True);
11694       Set_Has_Delayed_Freeze (Implicit_Base);
11695
11696       --  The bounds of the implicit base are the bounds of the parent base.
11697       --  Note that their type is the parent base.
11698
11699       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
11700       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
11701
11702       Set_Scalar_Range (Implicit_Base,
11703         Make_Range (Loc,
11704           Low_Bound  => Lo,
11705           High_Bound => Hi));
11706
11707       Conditional_Delay (Derived_Type, Parent_Type);
11708
11709       Set_Ekind (Derived_Type, E_Enumeration_Subtype);
11710       Set_Etype (Derived_Type, Implicit_Base);
11711       Set_Size_Info         (Derived_Type, Parent_Type);
11712
11713       if Unknown_RM_Size (Derived_Type) then
11714          Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
11715       end if;
11716
11717       Set_Is_Character_Type (Derived_Type, True);
11718
11719       if Nkind (Indic) /= N_Subtype_Indication then
11720
11721          --  If no explicit constraint, the bounds are those
11722          --  of the parent type.
11723
11724          Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Type));
11725          Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
11726          Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
11727       end if;
11728
11729       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
11730
11731       --  Because the implicit base is used in the conversion of the bounds,
11732       --  we have to freeze it now. This is similar to what is done for
11733       --  numeric types, and it equally suspicious, but otherwise a non-
11734       --  static bound will have a reference to an unfrozen type, which is
11735       --  rejected by Gigi (???). This requires specific care for definition
11736       --  of stream attributes. For details, see comments at the end of
11737       --  Build_Derived_Numeric_Type.
11738
11739       Freeze_Before (N, Implicit_Base);
11740    end Derived_Standard_Character;
11741
11742    ------------------------------
11743    -- Derived_Type_Declaration --
11744    ------------------------------
11745
11746    procedure Derived_Type_Declaration
11747      (T             : Entity_Id;
11748       N             : Node_Id;
11749       Is_Completion : Boolean)
11750    is
11751       Parent_Type  : Entity_Id;
11752
11753       function Comes_From_Generic (Typ : Entity_Id) return Boolean;
11754       --  Check whether the parent type is a generic formal, or derives
11755       --  directly or indirectly from one.
11756
11757       ------------------------
11758       -- Comes_From_Generic --
11759       ------------------------
11760
11761       function Comes_From_Generic (Typ : Entity_Id) return Boolean is
11762       begin
11763          if Is_Generic_Type (Typ) then
11764             return True;
11765
11766          elsif Is_Generic_Type (Root_Type (Parent_Type)) then
11767             return True;
11768
11769          elsif Is_Private_Type (Typ)
11770            and then Present (Full_View (Typ))
11771            and then Is_Generic_Type (Root_Type (Full_View (Typ)))
11772          then
11773             return True;
11774
11775          elsif Is_Generic_Actual_Type (Typ) then
11776             return True;
11777
11778          else
11779             return False;
11780          end if;
11781       end Comes_From_Generic;
11782
11783       --  Local variables
11784
11785       Def          : constant Node_Id := Type_Definition (N);
11786       Iface_Def    : Node_Id;
11787       Indic        : constant Node_Id := Subtype_Indication (Def);
11788       Extension    : constant Node_Id := Record_Extension_Part (Def);
11789       Parent_Node  : Node_Id;
11790       Parent_Scope : Entity_Id;
11791       Taggd        : Boolean;
11792
11793    --  Start of processing for Derived_Type_Declaration
11794
11795    begin
11796       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
11797
11798       --  Ada 2005 (AI-251): In case of interface derivation check that the
11799       --  parent is also an interface.
11800
11801       if Interface_Present (Def) then
11802          if not Is_Interface (Parent_Type) then
11803             Error_Msg_NE
11804               ("(Ada 2005) & must be an interface", Indic, Parent_Type);
11805
11806          else
11807             Parent_Node := Parent (Base_Type (Parent_Type));
11808             Iface_Def   := Type_Definition (Parent_Node);
11809
11810             --  Ada 2005 (AI-251): Limited interfaces can only inherit from
11811             --  other limited interfaces.
11812
11813             if Limited_Present (Def) then
11814                if Limited_Present (Iface_Def) then
11815                   null;
11816
11817                elsif Protected_Present (Iface_Def) then
11818                   Error_Msg_N
11819                     ("(Ada 2005) limited interface cannot "
11820                      & "inherit from protected interface", Indic);
11821
11822                elsif Synchronized_Present (Iface_Def) then
11823                   Error_Msg_N
11824                     ("(Ada 2005) limited interface cannot "
11825                      & "inherit from synchronized interface", Indic);
11826
11827                elsif Task_Present (Iface_Def) then
11828                   Error_Msg_N
11829                     ("(Ada 2005) limited interface cannot "
11830                      & "inherit from task interface", Indic);
11831
11832                else
11833                   Error_Msg_N
11834                     ("(Ada 2005) limited interface cannot "
11835                      & "inherit from non-limited interface", Indic);
11836                end if;
11837
11838             --  Ada 2005 (AI-345): Non-limited interfaces can only inherit
11839             --  from non-limited or limited interfaces.
11840
11841             elsif not Protected_Present (Def)
11842               and then not Synchronized_Present (Def)
11843               and then not Task_Present (Def)
11844             then
11845                if Limited_Present (Iface_Def) then
11846                   null;
11847
11848                elsif Protected_Present (Iface_Def) then
11849                   Error_Msg_N
11850                     ("(Ada 2005) non-limited interface cannot "
11851                      & "inherit from protected interface", Indic);
11852
11853                elsif Synchronized_Present (Iface_Def) then
11854                   Error_Msg_N
11855                     ("(Ada 2005) non-limited interface cannot "
11856                      & "inherit from synchronized interface", Indic);
11857
11858                elsif Task_Present (Iface_Def) then
11859                   Error_Msg_N
11860                     ("(Ada 2005) non-limited interface cannot "
11861                      & "inherit from task interface", Indic);
11862
11863                else
11864                   null;
11865                end if;
11866             end if;
11867          end if;
11868       end if;
11869
11870       if Is_Tagged_Type (Parent_Type)
11871         and then Is_Concurrent_Type (Parent_Type)
11872         and then not Is_Interface (Parent_Type)
11873       then
11874          Error_Msg_N
11875            ("parent type of a record extension cannot be "
11876             & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
11877          Set_Etype (T, Any_Type);
11878          return;
11879       end if;
11880
11881       --  Ada 2005 (AI-251): Decorate all the names in the list of ancestor
11882       --  interfaces
11883
11884       if Is_Tagged_Type (Parent_Type)
11885         and then Is_Non_Empty_List (Interface_List (Def))
11886       then
11887          declare
11888             Intf : Node_Id;
11889             T    : Entity_Id;
11890
11891          begin
11892             Intf := First (Interface_List (Def));
11893             while Present (Intf) loop
11894                T := Find_Type_Of_Subtype_Indic (Intf);
11895
11896                if not Is_Interface (T) then
11897                   Error_Msg_NE ("(Ada 2005) & must be an interface", Intf, T);
11898
11899                --  Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
11900                --  a limited type from having a nonlimited progenitor.
11901
11902                elsif (Limited_Present (Def)
11903                        or else (not Is_Interface (Parent_Type)
11904                                  and then Is_Limited_Type (Parent_Type)))
11905                  and then not Is_Limited_Interface (T)
11906                then
11907                   Error_Msg_NE
11908                    ("progenitor interface& of limited type must be limited",
11909                      N, T);
11910                end if;
11911
11912                Next (Intf);
11913             end loop;
11914          end;
11915       end if;
11916
11917       if Parent_Type = Any_Type
11918         or else Etype (Parent_Type) = Any_Type
11919         or else (Is_Class_Wide_Type (Parent_Type)
11920                    and then Etype (Parent_Type) = T)
11921       then
11922          --  If Parent_Type is undefined or illegal, make new type into a
11923          --  subtype of Any_Type, and set a few attributes to prevent cascaded
11924          --  errors. If this is a self-definition, emit error now.
11925
11926          if T = Parent_Type
11927            or else T = Etype (Parent_Type)
11928          then
11929             Error_Msg_N ("type cannot be used in its own definition", Indic);
11930          end if;
11931
11932          Set_Ekind        (T, Ekind (Parent_Type));
11933          Set_Etype        (T, Any_Type);
11934          Set_Scalar_Range (T, Scalar_Range (Any_Type));
11935
11936          if Is_Tagged_Type (T) then
11937             Set_Primitive_Operations (T, New_Elmt_List);
11938          end if;
11939
11940          return;
11941       end if;
11942
11943       --  Ada 2005 (AI-251): The case in which the parent of the full-view is
11944       --  an interface is special because the list of interfaces in the full
11945       --  view can be given in any order. For example:
11946
11947       --     type A is interface;
11948       --     type B is interface and A;
11949       --     type D is new B with private;
11950       --   private
11951       --     type D is new A and B with null record; -- 1 --
11952
11953       --  In this case we perform the following transformation of -1-:
11954
11955       --     type D is new B and A with null record;
11956
11957       --  If the parent of the full-view covers the parent of the partial-view
11958       --  we have two possible cases:
11959
11960       --     1) They have the same parent
11961       --     2) The parent of the full-view implements some further interfaces
11962
11963       --  In both cases we do not need to perform the transformation. In the
11964       --  first case the source program is correct and the transformation is
11965       --  not needed; in the second case the source program does not fulfill
11966       --  the no-hidden interfaces rule (AI-396) and the error will be reported
11967       --  later.
11968
11969       --  This transformation not only simplifies the rest of the analysis of
11970       --  this type declaration but also simplifies the correct generation of
11971       --  the object layout to the expander.
11972
11973       if In_Private_Part (Current_Scope)
11974         and then Is_Interface (Parent_Type)
11975       then
11976          declare
11977             Iface               : Node_Id;
11978             Partial_View        : Entity_Id;
11979             Partial_View_Parent : Entity_Id;
11980             New_Iface           : Node_Id;
11981
11982          begin
11983             --  Look for the associated private type declaration
11984
11985             Partial_View := First_Entity (Current_Scope);
11986             loop
11987                exit when No (Partial_View)
11988                  or else (Has_Private_Declaration (Partial_View)
11989                            and then Full_View (Partial_View) = T);
11990
11991                Next_Entity (Partial_View);
11992             end loop;
11993
11994             --  If the partial view was not found then the source code has
11995             --  errors and the transformation is not needed.
11996
11997             if Present (Partial_View) then
11998                Partial_View_Parent := Etype (Partial_View);
11999
12000                --  If the parent of the full-view covers the parent of the
12001                --  partial-view we have nothing else to do.
12002
12003                if Interface_Present_In_Ancestor
12004                     (Parent_Type, Partial_View_Parent)
12005                then
12006                   null;
12007
12008                --  Traverse the list of interfaces of the full-view to look
12009                --  for the parent of the partial-view and perform the tree
12010                --  transformation.
12011
12012                else
12013                   Iface := First (Interface_List (Def));
12014                   while Present (Iface) loop
12015                      if Etype (Iface) = Etype (Partial_View) then
12016                         Rewrite (Subtype_Indication (Def),
12017                           New_Copy (Subtype_Indication
12018                                      (Parent (Partial_View))));
12019
12020                         New_Iface := Make_Identifier (Sloc (N),
12021                                        Chars (Parent_Type));
12022                         Append (New_Iface, Interface_List (Def));
12023
12024                         --  Analyze the transformed code
12025
12026                         Derived_Type_Declaration (T, N, Is_Completion);
12027                         return;
12028                      end if;
12029
12030                      Next (Iface);
12031                   end loop;
12032                end if;
12033             end if;
12034          end;
12035       end if;
12036
12037       --  Only composite types other than array types are allowed to have
12038       --  discriminants.
12039
12040       if Present (Discriminant_Specifications (N))
12041         and then (Is_Elementary_Type (Parent_Type)
12042                   or else Is_Array_Type (Parent_Type))
12043         and then not Error_Posted (N)
12044       then
12045          Error_Msg_N
12046            ("elementary or array type cannot have discriminants",
12047             Defining_Identifier (First (Discriminant_Specifications (N))));
12048          Set_Has_Discriminants (T, False);
12049       end if;
12050
12051       --  In Ada 83, a derived type defined in a package specification cannot
12052       --  be used for further derivation until the end of its visible part.
12053       --  Note that derivation in the private part of the package is allowed.
12054
12055       if Ada_Version = Ada_83
12056         and then Is_Derived_Type (Parent_Type)
12057         and then In_Visible_Part (Scope (Parent_Type))
12058       then
12059          if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
12060             Error_Msg_N
12061               ("(Ada 83): premature use of type for derivation", Indic);
12062          end if;
12063       end if;
12064
12065       --  Check for early use of incomplete or private type
12066
12067       if Ekind (Parent_Type) = E_Void
12068         or else Ekind (Parent_Type) = E_Incomplete_Type
12069       then
12070          Error_Msg_N ("premature derivation of incomplete type", Indic);
12071          return;
12072
12073       elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
12074               and then not Comes_From_Generic (Parent_Type))
12075         or else Has_Private_Component (Parent_Type)
12076       then
12077          --  The ancestor type of a formal type can be incomplete, in which
12078          --  case only the operations of the partial view are available in
12079          --  the generic. Subsequent checks may be required when the full
12080          --  view is analyzed, to verify that derivation from a tagged type
12081          --  has an extension.
12082
12083          if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
12084             null;
12085
12086          elsif No (Underlying_Type (Parent_Type))
12087            or else Has_Private_Component (Parent_Type)
12088          then
12089             Error_Msg_N
12090               ("premature derivation of derived or private type", Indic);
12091
12092             --  Flag the type itself as being in error, this prevents some
12093             --  nasty problems with subsequent uses of the malformed type.
12094
12095             Set_Error_Posted (T);
12096
12097          --  Check that within the immediate scope of an untagged partial
12098          --  view it's illegal to derive from the partial view if the
12099          --  full view is tagged. (7.3(7))
12100
12101          --  We verify that the Parent_Type is a partial view by checking
12102          --  that it is not a Full_Type_Declaration (i.e. a private type or
12103          --  private extension declaration), to distinguish a partial view
12104          --  from  a derivation from a private type which also appears as
12105          --  E_Private_Type.
12106
12107          elsif Present (Full_View (Parent_Type))
12108            and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
12109            and then not Is_Tagged_Type (Parent_Type)
12110            and then Is_Tagged_Type (Full_View (Parent_Type))
12111          then
12112             Parent_Scope := Scope (T);
12113             while Present (Parent_Scope)
12114               and then Parent_Scope /= Standard_Standard
12115             loop
12116                if Parent_Scope = Scope (Parent_Type) then
12117                   Error_Msg_N
12118                     ("premature derivation from type with tagged full view",
12119                      Indic);
12120                end if;
12121
12122                Parent_Scope := Scope (Parent_Scope);
12123             end loop;
12124          end if;
12125       end if;
12126
12127       --  Check that form of derivation is appropriate
12128
12129       Taggd := Is_Tagged_Type (Parent_Type);
12130
12131       --  Perhaps the parent type should be changed to the class-wide type's
12132       --  specific type in this case to prevent cascading errors ???
12133
12134       if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
12135          Error_Msg_N ("parent type must not be a class-wide type", Indic);
12136          return;
12137       end if;
12138
12139       if Present (Extension) and then not Taggd then
12140          Error_Msg_N
12141            ("type derived from untagged type cannot have extension", Indic);
12142
12143       elsif No (Extension) and then Taggd then
12144
12145          --  If this declaration is within a private part (or body) of a
12146          --  generic instantiation then the derivation is allowed (the parent
12147          --  type can only appear tagged in this case if it's a generic actual
12148          --  type, since it would otherwise have been rejected in the analysis
12149          --  of the generic template).
12150
12151          if not Is_Generic_Actual_Type (Parent_Type)
12152            or else In_Visible_Part (Scope (Parent_Type))
12153          then
12154             Error_Msg_N
12155               ("type derived from tagged type must have extension", Indic);
12156          end if;
12157       end if;
12158
12159       --  AI-443: Synchronized formal derived types require a private
12160       --  extension. There is no point in checking the ancestor type or
12161       --  the progenitors since the construct is wrong to begin with.
12162
12163       if Ada_Version >= Ada_05
12164         and then Is_Generic_Type (T)
12165         and then Present (Original_Node (N))
12166       then
12167          declare
12168             Decl : constant Node_Id := Original_Node (N);
12169
12170          begin
12171             if Nkind (Decl) = N_Formal_Type_Declaration
12172               and then Nkind (Formal_Type_Definition (Decl)) =
12173                          N_Formal_Derived_Type_Definition
12174               and then Synchronized_Present (Formal_Type_Definition (Decl))
12175               and then No (Extension)
12176
12177                --  Avoid emitting a duplicate error message
12178
12179               and then not Error_Posted (Indic)
12180             then
12181                Error_Msg_N
12182                  ("synchronized derived type must have extension", N);
12183             end if;
12184          end;
12185       end if;
12186
12187       Build_Derived_Type (N, Parent_Type, T, Is_Completion);
12188
12189       --  AI-419: The parent type of an explicitly limited derived type must
12190       --  be a limited type or a limited interface.
12191
12192       if Limited_Present (Def) then
12193          Set_Is_Limited_Record (T);
12194
12195          if Is_Interface (T) then
12196             Set_Is_Limited_Interface (T);
12197          end if;
12198
12199          if not Is_Limited_Type (Parent_Type)
12200            and then
12201              (not Is_Interface (Parent_Type)
12202                or else not Is_Limited_Interface (Parent_Type))
12203          then
12204             Error_Msg_NE ("parent type& of limited type must be limited",
12205               N, Parent_Type);
12206          end if;
12207       end if;
12208    end Derived_Type_Declaration;
12209
12210    ----------------------------------
12211    -- Enumeration_Type_Declaration --
12212    ----------------------------------
12213
12214    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
12215       Ev     : Uint;
12216       L      : Node_Id;
12217       R_Node : Node_Id;
12218       B_Node : Node_Id;
12219
12220    begin
12221       --  Create identifier node representing lower bound
12222
12223       B_Node := New_Node (N_Identifier, Sloc (Def));
12224       L := First (Literals (Def));
12225       Set_Chars (B_Node, Chars (L));
12226       Set_Entity (B_Node,  L);
12227       Set_Etype (B_Node, T);
12228       Set_Is_Static_Expression (B_Node, True);
12229
12230       R_Node := New_Node (N_Range, Sloc (Def));
12231       Set_Low_Bound  (R_Node, B_Node);
12232
12233       Set_Ekind (T, E_Enumeration_Type);
12234       Set_First_Literal (T, L);
12235       Set_Etype (T, T);
12236       Set_Is_Constrained (T);
12237
12238       Ev := Uint_0;
12239
12240       --  Loop through literals of enumeration type setting pos and rep values
12241       --  except that if the Ekind is already set, then it means that the
12242       --  literal was already constructed (case of a derived type declaration
12243       --  and we should not disturb the Pos and Rep values.
12244
12245       while Present (L) loop
12246          if Ekind (L) /= E_Enumeration_Literal then
12247             Set_Ekind (L, E_Enumeration_Literal);
12248             Set_Enumeration_Pos (L, Ev);
12249             Set_Enumeration_Rep (L, Ev);
12250             Set_Is_Known_Valid  (L, True);
12251          end if;
12252
12253          Set_Etype (L, T);
12254          New_Overloaded_Entity (L);
12255          Generate_Definition (L);
12256          Set_Convention (L, Convention_Intrinsic);
12257
12258          if Nkind (L) = N_Defining_Character_Literal then
12259             Set_Is_Character_Type (T, True);
12260          end if;
12261
12262          Ev := Ev + 1;
12263          Next (L);
12264       end loop;
12265
12266       --  Now create a node representing upper bound
12267
12268       B_Node := New_Node (N_Identifier, Sloc (Def));
12269       Set_Chars (B_Node, Chars (Last (Literals (Def))));
12270       Set_Entity (B_Node,  Last (Literals (Def)));
12271       Set_Etype (B_Node, T);
12272       Set_Is_Static_Expression (B_Node, True);
12273
12274       Set_High_Bound (R_Node, B_Node);
12275
12276       --  Initialize various fields of the type. Some of this information
12277       --  may be overwritten later through rep.clauses.
12278
12279       Set_Scalar_Range    (T, R_Node);
12280       Set_RM_Size         (T, UI_From_Int (Minimum_Size (T)));
12281       Set_Enum_Esize      (T);
12282       Set_Enum_Pos_To_Rep (T, Empty);
12283
12284       --  Set Discard_Names if configuration pragma set, or if there is
12285       --  a parameterless pragma in the current declarative region
12286
12287       if Global_Discard_Names
12288         or else Discard_Names (Scope (T))
12289       then
12290          Set_Discard_Names (T);
12291       end if;
12292
12293       --  Process end label if there is one
12294
12295       if Present (Def) then
12296          Process_End_Label (Def, 'e', T);
12297       end if;
12298    end Enumeration_Type_Declaration;
12299
12300    ---------------------------------
12301    -- Expand_To_Stored_Constraint --
12302    ---------------------------------
12303
12304    function Expand_To_Stored_Constraint
12305      (Typ        : Entity_Id;
12306       Constraint : Elist_Id) return Elist_Id
12307    is
12308       Explicitly_Discriminated_Type : Entity_Id;
12309       Expansion    : Elist_Id;
12310       Discriminant : Entity_Id;
12311
12312       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
12313       --  Find the nearest type that actually specifies discriminants
12314
12315       ---------------------------------
12316       -- Type_With_Explicit_Discrims --
12317       ---------------------------------
12318
12319       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
12320          Typ : constant E := Base_Type (Id);
12321
12322       begin
12323          if Ekind (Typ) in Incomplete_Or_Private_Kind then
12324             if Present (Full_View (Typ)) then
12325                return Type_With_Explicit_Discrims (Full_View (Typ));
12326             end if;
12327
12328          else
12329             if Has_Discriminants (Typ) then
12330                return Typ;
12331             end if;
12332          end if;
12333
12334          if Etype (Typ) = Typ then
12335             return Empty;
12336          elsif Has_Discriminants (Typ) then
12337             return Typ;
12338          else
12339             return Type_With_Explicit_Discrims (Etype (Typ));
12340          end if;
12341
12342       end Type_With_Explicit_Discrims;
12343
12344    --  Start of processing for Expand_To_Stored_Constraint
12345
12346    begin
12347       if No (Constraint)
12348         or else Is_Empty_Elmt_List (Constraint)
12349       then
12350          return No_Elist;
12351       end if;
12352
12353       Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
12354
12355       if No (Explicitly_Discriminated_Type) then
12356          return No_Elist;
12357       end if;
12358
12359       Expansion := New_Elmt_List;
12360
12361       Discriminant :=
12362          First_Stored_Discriminant (Explicitly_Discriminated_Type);
12363       while Present (Discriminant) loop
12364          Append_Elmt (
12365            Get_Discriminant_Value (
12366              Discriminant, Explicitly_Discriminated_Type, Constraint),
12367            Expansion);
12368          Next_Stored_Discriminant (Discriminant);
12369       end loop;
12370
12371       return Expansion;
12372    end Expand_To_Stored_Constraint;
12373
12374    ---------------------------
12375    -- Find_Hidden_Interface --
12376    ---------------------------
12377
12378    function Find_Hidden_Interface
12379      (Src  : Elist_Id;
12380       Dest : Elist_Id) return Entity_Id
12381    is
12382       Iface      : Entity_Id;
12383       Iface_Elmt : Elmt_Id;
12384
12385    begin
12386       if Present (Src) and then Present (Dest) then
12387          Iface_Elmt := First_Elmt (Src);
12388          while Present (Iface_Elmt) loop
12389             Iface := Node (Iface_Elmt);
12390
12391             if Is_Interface (Iface)
12392               and then not Contain_Interface (Iface, Dest)
12393             then
12394                return Iface;
12395             end if;
12396
12397             Next_Elmt (Iface_Elmt);
12398          end loop;
12399       end if;
12400
12401       return Empty;
12402    end Find_Hidden_Interface;
12403
12404    --------------------
12405    -- Find_Type_Name --
12406    --------------------
12407
12408    function Find_Type_Name (N : Node_Id) return Entity_Id is
12409       Id       : constant Entity_Id := Defining_Identifier (N);
12410       Prev     : Entity_Id;
12411       New_Id   : Entity_Id;
12412       Prev_Par : Node_Id;
12413
12414    begin
12415       --  Find incomplete declaration, if one was given
12416
12417       Prev := Current_Entity_In_Scope (Id);
12418
12419       if Present (Prev) then
12420
12421          --  Previous declaration exists. Error if not incomplete/private case
12422          --  except if previous declaration is implicit, etc. Enter_Name will
12423          --  emit error if appropriate.
12424
12425          Prev_Par := Parent (Prev);
12426
12427          if not Is_Incomplete_Or_Private_Type (Prev) then
12428             Enter_Name (Id);
12429             New_Id := Id;
12430
12431          elsif Nkind (N) /= N_Full_Type_Declaration
12432            and then Nkind (N) /= N_Task_Type_Declaration
12433            and then Nkind (N) /= N_Protected_Type_Declaration
12434          then
12435             --  Completion must be a full type declarations (RM 7.3(4))
12436
12437             Error_Msg_Sloc := Sloc (Prev);
12438             Error_Msg_NE ("invalid completion of }", Id, Prev);
12439
12440             --  Set scope of Id to avoid cascaded errors. Entity is never
12441             --  examined again, except when saving globals in generics.
12442
12443             Set_Scope (Id, Current_Scope);
12444             New_Id := Id;
12445
12446          --  Case of full declaration of incomplete type
12447
12448          elsif Ekind (Prev) = E_Incomplete_Type then
12449
12450             --  Indicate that the incomplete declaration has a matching full
12451             --  declaration. The defining occurrence of the incomplete
12452             --  declaration remains the visible one, and the procedure
12453             --  Get_Full_View dereferences it whenever the type is used.
12454
12455             if Present (Full_View (Prev)) then
12456                Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
12457             end if;
12458
12459             Set_Full_View (Prev,  Id);
12460             Append_Entity (Id, Current_Scope);
12461             Set_Is_Public (Id, Is_Public (Prev));
12462             Set_Is_Internal (Id);
12463             New_Id := Prev;
12464
12465          --  Case of full declaration of private type
12466
12467          else
12468             if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
12469                if Etype (Prev) /= Prev then
12470
12471                   --  Prev is a private subtype or a derived type, and needs
12472                   --  no completion.
12473
12474                   Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
12475                   New_Id := Id;
12476
12477                elsif Ekind (Prev) = E_Private_Type
12478                  and then
12479                    (Nkind (N) = N_Task_Type_Declaration
12480                      or else Nkind (N) = N_Protected_Type_Declaration)
12481                then
12482                   Error_Msg_N
12483                    ("completion of nonlimited type cannot be limited", N);
12484
12485                elsif Ekind (Prev) = E_Record_Type_With_Private
12486                  and then
12487                    (Nkind (N) = N_Task_Type_Declaration
12488                      or else Nkind (N) = N_Protected_Type_Declaration)
12489                then
12490                   if not Is_Limited_Record (Prev) then
12491                      Error_Msg_N
12492                         ("completion of nonlimited type cannot be limited", N);
12493
12494                   elsif No (Interface_List (N)) then
12495                      Error_Msg_N
12496                         ("completion of tagged private type must be tagged",
12497                            N);
12498                   end if;
12499                end if;
12500
12501             --  Ada 2005 (AI-251): Private extension declaration of a task
12502             --  type or a protected type. This case arises when covering
12503             --  interface types.
12504
12505             elsif Nkind (N) = N_Task_Type_Declaration
12506               or else Nkind (N) = N_Protected_Type_Declaration
12507             then
12508                null;
12509
12510             elsif Nkind (N) /= N_Full_Type_Declaration
12511               or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
12512             then
12513                Error_Msg_N
12514                  ("full view of private extension must be an extension", N);
12515
12516             elsif not (Abstract_Present (Parent (Prev)))
12517               and then Abstract_Present (Type_Definition (N))
12518             then
12519                Error_Msg_N
12520                  ("full view of non-abstract extension cannot be abstract", N);
12521             end if;
12522
12523             if not In_Private_Part (Current_Scope) then
12524                Error_Msg_N
12525                  ("declaration of full view must appear in private part", N);
12526             end if;
12527
12528             Copy_And_Swap (Prev, Id);
12529             Set_Has_Private_Declaration (Prev);
12530             Set_Has_Private_Declaration (Id);
12531
12532             --  If no error, propagate freeze_node from private to full view.
12533             --  It may have been generated for an early operational item.
12534
12535             if Present (Freeze_Node (Id))
12536               and then Serious_Errors_Detected = 0
12537               and then No (Full_View (Id))
12538             then
12539                Set_Freeze_Node (Prev, Freeze_Node (Id));
12540                Set_Freeze_Node (Id, Empty);
12541                Set_First_Rep_Item (Prev, First_Rep_Item (Id));
12542             end if;
12543
12544             Set_Full_View (Id, Prev);
12545             New_Id := Prev;
12546          end if;
12547
12548          --  Verify that full declaration conforms to incomplete one
12549
12550          if Is_Incomplete_Or_Private_Type (Prev)
12551            and then Present (Discriminant_Specifications (Prev_Par))
12552          then
12553             if Present (Discriminant_Specifications (N)) then
12554                if Ekind (Prev) = E_Incomplete_Type then
12555                   Check_Discriminant_Conformance (N, Prev, Prev);
12556                else
12557                   Check_Discriminant_Conformance (N, Prev, Id);
12558                end if;
12559
12560             else
12561                Error_Msg_N
12562                  ("missing discriminants in full type declaration", N);
12563
12564                --  To avoid cascaded errors on subsequent use, share the
12565                --  discriminants of the partial view.
12566
12567                Set_Discriminant_Specifications (N,
12568                  Discriminant_Specifications (Prev_Par));
12569             end if;
12570          end if;
12571
12572          --  A prior untagged private type can have an associated class-wide
12573          --  type due to use of the class attribute, and in this case also the
12574          --  full type is required to be tagged.
12575
12576          if Is_Type (Prev)
12577            and then (Is_Tagged_Type (Prev)
12578                       or else Present (Class_Wide_Type (Prev)))
12579            and then (Nkind (N) /= N_Task_Type_Declaration
12580                       and then Nkind (N) /= N_Protected_Type_Declaration)
12581          then
12582             --  The full declaration is either a tagged record or an
12583             --  extension otherwise this is an error
12584
12585             if Nkind (Type_Definition (N)) = N_Record_Definition then
12586                if not Tagged_Present (Type_Definition (N)) then
12587                   Error_Msg_NE
12588                     ("full declaration of } must be tagged", Prev, Id);
12589                   Set_Is_Tagged_Type (Id);
12590                   Set_Primitive_Operations (Id, New_Elmt_List);
12591                end if;
12592
12593             elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
12594                if No (Record_Extension_Part (Type_Definition (N))) then
12595                   Error_Msg_NE (
12596                     "full declaration of } must be a record extension",
12597                     Prev, Id);
12598                   Set_Is_Tagged_Type (Id);
12599                   Set_Primitive_Operations (Id, New_Elmt_List);
12600                end if;
12601
12602             else
12603                Error_Msg_NE
12604                  ("full declaration of } must be a tagged type", Prev, Id);
12605
12606             end if;
12607          end if;
12608
12609          return New_Id;
12610
12611       else
12612          --  New type declaration
12613
12614          Enter_Name (Id);
12615          return Id;
12616       end if;
12617    end Find_Type_Name;
12618
12619    -------------------------
12620    -- Find_Type_Of_Object --
12621    -------------------------
12622
12623    function Find_Type_Of_Object
12624      (Obj_Def     : Node_Id;
12625       Related_Nod : Node_Id) return Entity_Id
12626    is
12627       Def_Kind : constant Node_Kind := Nkind (Obj_Def);
12628       P        : Node_Id := Parent (Obj_Def);
12629       T        : Entity_Id;
12630       Nam      : Name_Id;
12631
12632    begin
12633       --  If the parent is a component_definition node we climb to the
12634       --  component_declaration node
12635
12636       if Nkind (P) = N_Component_Definition then
12637          P := Parent (P);
12638       end if;
12639
12640       --  Case of an anonymous array subtype
12641
12642       if Def_Kind = N_Constrained_Array_Definition
12643         or else Def_Kind = N_Unconstrained_Array_Definition
12644       then
12645          T := Empty;
12646          Array_Type_Declaration (T, Obj_Def);
12647
12648       --  Create an explicit subtype whenever possible
12649
12650       elsif Nkind (P) /= N_Component_Declaration
12651         and then Def_Kind = N_Subtype_Indication
12652       then
12653          --  Base name of subtype on object name, which will be unique in
12654          --  the current scope.
12655
12656          --  If this is a duplicate declaration, return base type, to avoid
12657          --  generating duplicate anonymous types.
12658
12659          if Error_Posted (P) then
12660             Analyze (Subtype_Mark (Obj_Def));
12661             return Entity (Subtype_Mark (Obj_Def));
12662          end if;
12663
12664          Nam :=
12665             New_External_Name
12666              (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
12667
12668          T := Make_Defining_Identifier (Sloc (P), Nam);
12669
12670          Insert_Action (Obj_Def,
12671            Make_Subtype_Declaration (Sloc (P),
12672              Defining_Identifier => T,
12673              Subtype_Indication  => Relocate_Node (Obj_Def)));
12674
12675          --  This subtype may need freezing, and this will not be done
12676          --  automatically if the object declaration is not in declarative
12677          --  part. Since this is an object declaration, the type cannot always
12678          --  be frozen here. Deferred constants do not freeze their type
12679          --  (which often enough will be private).
12680
12681          if Nkind (P) = N_Object_Declaration
12682            and then Constant_Present (P)
12683            and then No (Expression (P))
12684          then
12685             null;
12686          else
12687             Insert_Actions (Obj_Def, Freeze_Entity (T, Sloc (P)));
12688          end if;
12689
12690       --  Ada 2005 AI-406: the object definition in an object declaration
12691       --  can be an access definition.
12692
12693       elsif Def_Kind = N_Access_Definition then
12694          T := Access_Definition (Related_Nod, Obj_Def);
12695          Set_Is_Local_Anonymous_Access (T);
12696
12697       --  Otherwise, the object definition is just a subtype_mark
12698
12699       else
12700          T := Process_Subtype (Obj_Def, Related_Nod);
12701       end if;
12702
12703       return T;
12704    end Find_Type_Of_Object;
12705
12706    --------------------------------
12707    -- Find_Type_Of_Subtype_Indic --
12708    --------------------------------
12709
12710    function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
12711       Typ : Entity_Id;
12712
12713    begin
12714       --  Case of subtype mark with a constraint
12715
12716       if Nkind (S) = N_Subtype_Indication then
12717          Find_Type (Subtype_Mark (S));
12718          Typ := Entity (Subtype_Mark (S));
12719
12720          if not
12721            Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
12722          then
12723             Error_Msg_N
12724               ("incorrect constraint for this kind of type", Constraint (S));
12725             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
12726          end if;
12727
12728       --  Otherwise we have a subtype mark without a constraint
12729
12730       elsif Error_Posted (S) then
12731          Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
12732          return Any_Type;
12733
12734       else
12735          Find_Type (S);
12736          Typ := Entity (S);
12737       end if;
12738
12739       if Typ = Standard_Wide_Character
12740         or else Typ = Standard_Wide_Wide_Character
12741         or else Typ = Standard_Wide_String
12742         or else Typ = Standard_Wide_Wide_String
12743       then
12744          Check_Restriction (No_Wide_Characters, S);
12745       end if;
12746
12747       return Typ;
12748    end Find_Type_Of_Subtype_Indic;
12749
12750    -------------------------------------
12751    -- Floating_Point_Type_Declaration --
12752    -------------------------------------
12753
12754    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
12755       Digs          : constant Node_Id := Digits_Expression (Def);
12756       Digs_Val      : Uint;
12757       Base_Typ      : Entity_Id;
12758       Implicit_Base : Entity_Id;
12759       Bound         : Node_Id;
12760
12761       function Can_Derive_From (E : Entity_Id) return Boolean;
12762       --  Find if given digits value allows derivation from specified type
12763
12764       ---------------------
12765       -- Can_Derive_From --
12766       ---------------------
12767
12768       function Can_Derive_From (E : Entity_Id) return Boolean is
12769          Spec : constant Entity_Id := Real_Range_Specification (Def);
12770
12771       begin
12772          if Digs_Val > Digits_Value (E) then
12773             return False;
12774          end if;
12775
12776          if Present (Spec) then
12777             if Expr_Value_R (Type_Low_Bound (E)) >
12778                Expr_Value_R (Low_Bound (Spec))
12779             then
12780                return False;
12781             end if;
12782
12783             if Expr_Value_R (Type_High_Bound (E)) <
12784                Expr_Value_R (High_Bound (Spec))
12785             then
12786                return False;
12787             end if;
12788          end if;
12789
12790          return True;
12791       end Can_Derive_From;
12792
12793    --  Start of processing for Floating_Point_Type_Declaration
12794
12795    begin
12796       Check_Restriction (No_Floating_Point, Def);
12797
12798       --  Create an implicit base type
12799
12800       Implicit_Base :=
12801         Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
12802
12803       --  Analyze and verify digits value
12804
12805       Analyze_And_Resolve (Digs, Any_Integer);
12806       Check_Digits_Expression (Digs);
12807       Digs_Val := Expr_Value (Digs);
12808
12809       --  Process possible range spec and find correct type to derive from
12810
12811       Process_Real_Range_Specification (Def);
12812
12813       if Can_Derive_From (Standard_Short_Float) then
12814          Base_Typ := Standard_Short_Float;
12815       elsif Can_Derive_From (Standard_Float) then
12816          Base_Typ := Standard_Float;
12817       elsif Can_Derive_From (Standard_Long_Float) then
12818          Base_Typ := Standard_Long_Float;
12819       elsif Can_Derive_From (Standard_Long_Long_Float) then
12820          Base_Typ := Standard_Long_Long_Float;
12821
12822       --  If we can't derive from any existing type, use long_long_float
12823       --  and give appropriate message explaining the problem.
12824
12825       else
12826          Base_Typ := Standard_Long_Long_Float;
12827
12828          if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
12829             Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
12830             Error_Msg_N ("digits value out of range, maximum is ^", Digs);
12831
12832          else
12833             Error_Msg_N
12834               ("range too large for any predefined type",
12835                Real_Range_Specification (Def));
12836          end if;
12837       end if;
12838
12839       --  If there are bounds given in the declaration use them as the bounds
12840       --  of the type, otherwise use the bounds of the predefined base type
12841       --  that was chosen based on the Digits value.
12842
12843       if Present (Real_Range_Specification (Def)) then
12844          Set_Scalar_Range (T, Real_Range_Specification (Def));
12845          Set_Is_Constrained (T);
12846
12847          --  The bounds of this range must be converted to machine numbers
12848          --  in accordance with RM 4.9(38).
12849
12850          Bound := Type_Low_Bound (T);
12851
12852          if Nkind (Bound) = N_Real_Literal then
12853             Set_Realval
12854               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
12855             Set_Is_Machine_Number (Bound);
12856          end if;
12857
12858          Bound := Type_High_Bound (T);
12859
12860          if Nkind (Bound) = N_Real_Literal then
12861             Set_Realval
12862               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
12863             Set_Is_Machine_Number (Bound);
12864          end if;
12865
12866       else
12867          Set_Scalar_Range (T, Scalar_Range (Base_Typ));
12868       end if;
12869
12870       --  Complete definition of implicit base and declared first subtype
12871
12872       Set_Etype          (Implicit_Base, Base_Typ);
12873
12874       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
12875       Set_Size_Info      (Implicit_Base,                (Base_Typ));
12876       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
12877       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
12878       Set_Digits_Value   (Implicit_Base, Digits_Value   (Base_Typ));
12879       Set_Vax_Float      (Implicit_Base, Vax_Float      (Base_Typ));
12880
12881       Set_Ekind          (T, E_Floating_Point_Subtype);
12882       Set_Etype          (T, Implicit_Base);
12883
12884       Set_Size_Info      (T,                (Implicit_Base));
12885       Set_RM_Size        (T, RM_Size        (Implicit_Base));
12886       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
12887       Set_Digits_Value   (T, Digs_Val);
12888    end Floating_Point_Type_Declaration;
12889
12890    ----------------------------
12891    -- Get_Discriminant_Value --
12892    ----------------------------
12893
12894    --  This is the situation:
12895
12896    --  There is a non-derived type
12897
12898    --       type T0 (Dx, Dy, Dz...)
12899
12900    --  There are zero or more levels of derivation, with each derivation
12901    --  either purely inheriting the discriminants, or defining its own.
12902
12903    --       type Ti      is new Ti-1
12904    --  or
12905    --       type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
12906    --  or
12907    --       subtype Ti is ...
12908
12909    --  The subtype issue is avoided by the use of Original_Record_Component,
12910    --  and the fact that derived subtypes also derive the constraints.
12911
12912    --  This chain leads back from
12913
12914    --       Typ_For_Constraint
12915
12916    --  Typ_For_Constraint has discriminants, and the value for each
12917    --  discriminant is given by its corresponding Elmt of Constraints.
12918
12919    --  Discriminant is some discriminant in this hierarchy
12920
12921    --  We need to return its value
12922
12923    --  We do this by recursively searching each level, and looking for
12924    --  Discriminant. Once we get to the bottom, we start backing up
12925    --  returning the value for it which may in turn be a discriminant
12926    --  further up, so on the backup we continue the substitution.
12927
12928    function Get_Discriminant_Value
12929      (Discriminant       : Entity_Id;
12930       Typ_For_Constraint : Entity_Id;
12931       Constraint         : Elist_Id) return Node_Id
12932    is
12933       function Search_Derivation_Levels
12934         (Ti                    : Entity_Id;
12935          Discrim_Values        : Elist_Id;
12936          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
12937       --  This is the routine that performs the recursive search of levels
12938       --  as described above.
12939
12940       ------------------------------
12941       -- Search_Derivation_Levels --
12942       ------------------------------
12943
12944       function Search_Derivation_Levels
12945         (Ti                    : Entity_Id;
12946          Discrim_Values        : Elist_Id;
12947          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
12948       is
12949          Assoc          : Elmt_Id;
12950          Disc           : Entity_Id;
12951          Result         : Node_Or_Entity_Id;
12952          Result_Entity  : Node_Id;
12953
12954       begin
12955          --  If inappropriate type, return Error, this happens only in
12956          --  cascaded error situations, and we want to avoid a blow up.
12957
12958          if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
12959             return Error;
12960          end if;
12961
12962          --  Look deeper if possible. Use Stored_Constraints only for
12963          --  untagged types. For tagged types use the given constraint.
12964          --  This asymmetry needs explanation???
12965
12966          if not Stored_Discrim_Values
12967            and then Present (Stored_Constraint (Ti))
12968            and then not Is_Tagged_Type (Ti)
12969          then
12970             Result :=
12971               Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
12972          else
12973             declare
12974                Td : constant Entity_Id := Etype (Ti);
12975
12976             begin
12977                if Td = Ti then
12978                   Result := Discriminant;
12979
12980                else
12981                   if Present (Stored_Constraint (Ti)) then
12982                      Result :=
12983                         Search_Derivation_Levels
12984                           (Td, Stored_Constraint (Ti), True);
12985                   else
12986                      Result :=
12987                         Search_Derivation_Levels
12988                           (Td, Discrim_Values, Stored_Discrim_Values);
12989                   end if;
12990                end if;
12991             end;
12992          end if;
12993
12994          --  Extra underlying places to search, if not found above. For
12995          --  concurrent types, the relevant discriminant appears in the
12996          --  corresponding record. For a type derived from a private type
12997          --  without discriminant, the full view inherits the discriminants
12998          --  of the full view of the parent.
12999
13000          if Result = Discriminant then
13001             if Is_Concurrent_Type (Ti)
13002               and then Present (Corresponding_Record_Type (Ti))
13003             then
13004                Result :=
13005                  Search_Derivation_Levels (
13006                    Corresponding_Record_Type (Ti),
13007                    Discrim_Values,
13008                    Stored_Discrim_Values);
13009
13010             elsif Is_Private_Type (Ti)
13011               and then not Has_Discriminants (Ti)
13012               and then Present (Full_View (Ti))
13013               and then Etype (Full_View (Ti)) /= Ti
13014             then
13015                Result :=
13016                  Search_Derivation_Levels (
13017                    Full_View (Ti),
13018                    Discrim_Values,
13019                    Stored_Discrim_Values);
13020             end if;
13021          end if;
13022
13023          --  If Result is not a (reference to a) discriminant, return it,
13024          --  otherwise set Result_Entity to the discriminant.
13025
13026          if Nkind (Result) = N_Defining_Identifier then
13027             pragma Assert (Result = Discriminant);
13028             Result_Entity := Result;
13029
13030          else
13031             if not Denotes_Discriminant (Result) then
13032                return Result;
13033             end if;
13034
13035             Result_Entity := Entity (Result);
13036          end if;
13037
13038          --  See if this level of derivation actually has discriminants
13039          --  because tagged derivations can add them, hence the lower
13040          --  levels need not have any.
13041
13042          if not Has_Discriminants (Ti) then
13043             return Result;
13044          end if;
13045
13046          --  Scan Ti's discriminants for Result_Entity,
13047          --  and return its corresponding value, if any.
13048
13049          Result_Entity := Original_Record_Component (Result_Entity);
13050
13051          Assoc := First_Elmt (Discrim_Values);
13052
13053          if Stored_Discrim_Values then
13054             Disc := First_Stored_Discriminant (Ti);
13055          else
13056             Disc := First_Discriminant (Ti);
13057          end if;
13058
13059          while Present (Disc) loop
13060             pragma Assert (Present (Assoc));
13061
13062             if Original_Record_Component (Disc) = Result_Entity then
13063                return Node (Assoc);
13064             end if;
13065
13066             Next_Elmt (Assoc);
13067
13068             if Stored_Discrim_Values then
13069                Next_Stored_Discriminant (Disc);
13070             else
13071                Next_Discriminant (Disc);
13072             end if;
13073          end loop;
13074
13075          --  Could not find it
13076          --
13077          return Result;
13078       end Search_Derivation_Levels;
13079
13080       Result : Node_Or_Entity_Id;
13081
13082    --  Start of processing for Get_Discriminant_Value
13083
13084    begin
13085       --  ??? This routine is a gigantic mess and will be deleted. For the
13086       --  time being just test for the trivial case before calling recurse.
13087
13088       if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
13089          declare
13090             D : Entity_Id;
13091             E : Elmt_Id;
13092
13093          begin
13094             D := First_Discriminant (Typ_For_Constraint);
13095             E := First_Elmt (Constraint);
13096             while Present (D) loop
13097                if Chars (D) = Chars (Discriminant) then
13098                   return Node (E);
13099                end if;
13100
13101                Next_Discriminant (D);
13102                Next_Elmt (E);
13103             end loop;
13104          end;
13105       end if;
13106
13107       Result := Search_Derivation_Levels
13108         (Typ_For_Constraint, Constraint, False);
13109
13110       --  ??? hack to disappear when this routine is gone
13111
13112       if  Nkind (Result) = N_Defining_Identifier then
13113          declare
13114             D : Entity_Id;
13115             E : Elmt_Id;
13116
13117          begin
13118             D := First_Discriminant (Typ_For_Constraint);
13119             E := First_Elmt (Constraint);
13120             while Present (D) loop
13121                if Corresponding_Discriminant (D) = Discriminant then
13122                   return Node (E);
13123                end if;
13124
13125                Next_Discriminant (D);
13126                Next_Elmt (E);
13127             end loop;
13128          end;
13129       end if;
13130
13131       pragma Assert (Nkind (Result) /= N_Defining_Identifier);
13132       return Result;
13133    end Get_Discriminant_Value;
13134
13135    --------------------------
13136    -- Has_Range_Constraint --
13137    --------------------------
13138
13139    function Has_Range_Constraint (N : Node_Id) return Boolean is
13140       C : constant Node_Id := Constraint (N);
13141
13142    begin
13143       if Nkind (C) = N_Range_Constraint then
13144          return True;
13145
13146       elsif Nkind (C) = N_Digits_Constraint then
13147          return
13148             Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
13149               or else
13150             Present (Range_Constraint (C));
13151
13152       elsif Nkind (C) = N_Delta_Constraint then
13153          return Present (Range_Constraint (C));
13154
13155       else
13156          return False;
13157       end if;
13158    end Has_Range_Constraint;
13159
13160    ------------------------
13161    -- Inherit_Components --
13162    ------------------------
13163
13164    function Inherit_Components
13165      (N             : Node_Id;
13166       Parent_Base   : Entity_Id;
13167       Derived_Base  : Entity_Id;
13168       Is_Tagged     : Boolean;
13169       Inherit_Discr : Boolean;
13170       Discs         : Elist_Id) return Elist_Id
13171    is
13172       Assoc_List : constant Elist_Id := New_Elmt_List;
13173
13174       procedure Inherit_Component
13175         (Old_C          : Entity_Id;
13176          Plain_Discrim  : Boolean := False;
13177          Stored_Discrim : Boolean := False);
13178       --  Inherits component Old_C from Parent_Base to the Derived_Base. If
13179       --  Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
13180       --  True, Old_C is a stored discriminant. If they are both false then
13181       --  Old_C is a regular component.
13182
13183       -----------------------
13184       -- Inherit_Component --
13185       -----------------------
13186
13187       procedure Inherit_Component
13188         (Old_C          : Entity_Id;
13189          Plain_Discrim  : Boolean := False;
13190          Stored_Discrim : Boolean := False)
13191       is
13192          New_C : constant Entity_Id := New_Copy (Old_C);
13193
13194          Discrim      : Entity_Id;
13195          Corr_Discrim : Entity_Id;
13196
13197       begin
13198          pragma Assert (not Is_Tagged or else not Stored_Discrim);
13199
13200          Set_Parent (New_C, Parent (Old_C));
13201
13202          --  Regular discriminants and components must be inserted in the scope
13203          --  of the Derived_Base. Do it here.
13204
13205          if not Stored_Discrim then
13206             Enter_Name (New_C);
13207          end if;
13208
13209          --  For tagged types the Original_Record_Component must point to
13210          --  whatever this field was pointing to in the parent type. This has
13211          --  already been achieved by the call to New_Copy above.
13212
13213          if not Is_Tagged then
13214             Set_Original_Record_Component (New_C, New_C);
13215          end if;
13216
13217          --  If we have inherited a component then see if its Etype contains
13218          --  references to Parent_Base discriminants. In this case, replace
13219          --  these references with the constraints given in Discs. We do not
13220          --  do this for the partial view of private types because this is
13221          --  not needed (only the components of the full view will be used
13222          --  for code generation) and cause problem. We also avoid this
13223          --  transformation in some error situations.
13224
13225          if Ekind (New_C) = E_Component then
13226             if (Is_Private_Type (Derived_Base)
13227                  and then not Is_Generic_Type (Derived_Base))
13228               or else (Is_Empty_Elmt_List (Discs)
13229                         and then  not Expander_Active)
13230             then
13231                Set_Etype (New_C, Etype (Old_C));
13232
13233             else
13234                --  The current component introduces a circularity of the
13235                --  following kind:
13236
13237                --     limited with Pack_2;
13238                --     package Pack_1 is
13239                --        type T_1 is tagged record
13240                --           Comp : access Pack_2.T_2;
13241                --           ...
13242                --        end record;
13243                --     end Pack_1;
13244
13245                --     with Pack_1;
13246                --     package Pack_2 is
13247                --        type T_2 is new Pack_1.T_1 with ...;
13248                --     end Pack_2;
13249
13250                Set_Etype
13251                  (New_C,
13252                   Constrain_Component_Type
13253                   (Old_C, Derived_Base, N, Parent_Base, Discs));
13254             end if;
13255          end if;
13256
13257          --  In derived tagged types it is illegal to reference a non
13258          --  discriminant component in the parent type. To catch this, mark
13259          --  these components with an Ekind of E_Void. This will be reset in
13260          --  Record_Type_Definition after processing the record extension of
13261          --  the derived type.
13262
13263          --  If the declaration is a private extension, there is no further
13264          --  record extension to process, and the components retain their
13265          --  current kind, because they are visible at this point.
13266
13267          if Is_Tagged and then Ekind (New_C) = E_Component
13268            and then Nkind (N) /= N_Private_Extension_Declaration
13269          then
13270             Set_Ekind (New_C, E_Void);
13271          end if;
13272
13273          if Plain_Discrim then
13274             Set_Corresponding_Discriminant (New_C, Old_C);
13275             Build_Discriminal (New_C);
13276
13277          --  If we are explicitly inheriting a stored discriminant it will be
13278          --  completely hidden.
13279
13280          elsif Stored_Discrim then
13281             Set_Corresponding_Discriminant (New_C, Empty);
13282             Set_Discriminal (New_C, Empty);
13283             Set_Is_Completely_Hidden (New_C);
13284
13285             --  Set the Original_Record_Component of each discriminant in the
13286             --  derived base to point to the corresponding stored that we just
13287             --  created.
13288
13289             Discrim := First_Discriminant (Derived_Base);
13290             while Present (Discrim) loop
13291                Corr_Discrim := Corresponding_Discriminant (Discrim);
13292
13293                --  Corr_Discrim could be missing in an error situation
13294
13295                if Present (Corr_Discrim)
13296                  and then Original_Record_Component (Corr_Discrim) = Old_C
13297                then
13298                   Set_Original_Record_Component (Discrim, New_C);
13299                end if;
13300
13301                Next_Discriminant (Discrim);
13302             end loop;
13303
13304             Append_Entity (New_C, Derived_Base);
13305          end if;
13306
13307          if not Is_Tagged then
13308             Append_Elmt (Old_C, Assoc_List);
13309             Append_Elmt (New_C, Assoc_List);
13310          end if;
13311       end Inherit_Component;
13312
13313       --  Variables local to Inherit_Component
13314
13315       Loc : constant Source_Ptr := Sloc (N);
13316
13317       Parent_Discrim : Entity_Id;
13318       Stored_Discrim : Entity_Id;
13319       D              : Entity_Id;
13320       Component      : Entity_Id;
13321
13322    --  Start of processing for Inherit_Components
13323
13324    begin
13325       if not Is_Tagged then
13326          Append_Elmt (Parent_Base,  Assoc_List);
13327          Append_Elmt (Derived_Base, Assoc_List);
13328       end if;
13329
13330       --  Inherit parent discriminants if needed
13331
13332       if Inherit_Discr then
13333          Parent_Discrim := First_Discriminant (Parent_Base);
13334          while Present (Parent_Discrim) loop
13335             Inherit_Component (Parent_Discrim, Plain_Discrim => True);
13336             Next_Discriminant (Parent_Discrim);
13337          end loop;
13338       end if;
13339
13340       --  Create explicit stored discrims for untagged types when necessary
13341
13342       if not Has_Unknown_Discriminants (Derived_Base)
13343         and then Has_Discriminants (Parent_Base)
13344         and then not Is_Tagged
13345         and then
13346           (not Inherit_Discr
13347              or else First_Discriminant (Parent_Base) /=
13348                      First_Stored_Discriminant (Parent_Base))
13349       then
13350          Stored_Discrim := First_Stored_Discriminant (Parent_Base);
13351          while Present (Stored_Discrim) loop
13352             Inherit_Component (Stored_Discrim, Stored_Discrim => True);
13353             Next_Stored_Discriminant (Stored_Discrim);
13354          end loop;
13355       end if;
13356
13357       --  See if we can apply the second transformation for derived types, as
13358       --  explained in point 6. in the comments above Build_Derived_Record_Type
13359       --  This is achieved by appending Derived_Base discriminants into Discs,
13360       --  which has the side effect of returning a non empty Discs list to the
13361       --  caller of Inherit_Components, which is what we want. This must be
13362       --  done for private derived types if there are explicit stored
13363       --  discriminants, to ensure that we can retrieve the values of the
13364       --  constraints provided in the ancestors.
13365
13366       if Inherit_Discr
13367         and then Is_Empty_Elmt_List (Discs)
13368         and then Present (First_Discriminant (Derived_Base))
13369         and then
13370           (not Is_Private_Type (Derived_Base)
13371              or else Is_Completely_Hidden
13372                (First_Stored_Discriminant (Derived_Base))
13373              or else Is_Generic_Type (Derived_Base))
13374       then
13375          D := First_Discriminant (Derived_Base);
13376          while Present (D) loop
13377             Append_Elmt (New_Reference_To (D, Loc), Discs);
13378             Next_Discriminant (D);
13379          end loop;
13380       end if;
13381
13382       --  Finally, inherit non-discriminant components unless they are not
13383       --  visible because defined or inherited from the full view of the
13384       --  parent. Don't inherit the _parent field of the parent type.
13385
13386       Component := First_Entity (Parent_Base);
13387       while Present (Component) loop
13388
13389          --  Ada 2005 (AI-251): Do not inherit components associated with
13390          --  secondary tags of the parent.
13391
13392          if Ekind (Component) = E_Component
13393            and then Present (Related_Interface (Component))
13394          then
13395             null;
13396
13397          elsif Ekind (Component) /= E_Component
13398            or else Chars (Component) = Name_uParent
13399          then
13400             null;
13401
13402          --  If the derived type is within the parent type's declarative
13403          --  region, then the components can still be inherited even though
13404          --  they aren't visible at this point. This can occur for cases
13405          --  such as within public child units where the components must
13406          --  become visible upon entering the child unit's private part.
13407
13408          elsif not Is_Visible_Component (Component)
13409            and then not In_Open_Scopes (Scope (Parent_Base))
13410          then
13411             null;
13412
13413          elsif Ekind (Derived_Base) = E_Private_Type
13414            or else Ekind (Derived_Base) = E_Limited_Private_Type
13415          then
13416             null;
13417
13418          else
13419             Inherit_Component (Component);
13420          end if;
13421
13422          Next_Entity (Component);
13423       end loop;
13424
13425       --  For tagged derived types, inherited discriminants cannot be used in
13426       --  component declarations of the record extension part. To achieve this
13427       --  we mark the inherited discriminants as not visible.
13428
13429       if Is_Tagged and then Inherit_Discr then
13430          D := First_Discriminant (Derived_Base);
13431          while Present (D) loop
13432             Set_Is_Immediately_Visible (D, False);
13433             Next_Discriminant (D);
13434          end loop;
13435       end if;
13436
13437       return Assoc_List;
13438    end Inherit_Components;
13439
13440    -----------------------
13441    -- Is_Null_Extension --
13442    -----------------------
13443
13444    function Is_Null_Extension (T : Entity_Id) return Boolean is
13445       Type_Decl : constant Node_Id := Parent (T);
13446       Comp_List : Node_Id;
13447       Comp      : Node_Id;
13448
13449    begin
13450       if Nkind (Type_Decl) /= N_Full_Type_Declaration
13451         or else not Is_Tagged_Type (T)
13452         or else Nkind (Type_Definition (Type_Decl)) /=
13453                                               N_Derived_Type_Definition
13454         or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
13455       then
13456          return False;
13457       end if;
13458
13459       Comp_List :=
13460         Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
13461
13462       if Present (Discriminant_Specifications (Type_Decl)) then
13463          return False;
13464
13465       elsif Present (Comp_List)
13466         and then Is_Non_Empty_List (Component_Items (Comp_List))
13467       then
13468          Comp := First (Component_Items (Comp_List));
13469
13470          --  Only user-defined components are relevant. The component list
13471          --  may also contain a parent component and internal components
13472          --  corresponding to secondary tags, but these do not determine
13473          --  whether this is a null extension.
13474
13475          while Present (Comp) loop
13476             if Comes_From_Source (Comp) then
13477                return False;
13478             end if;
13479
13480             Next (Comp);
13481          end loop;
13482
13483          return True;
13484       else
13485          return True;
13486       end if;
13487    end Is_Null_Extension;
13488
13489    ------------------------------
13490    -- Is_Valid_Constraint_Kind --
13491    ------------------------------
13492
13493    function Is_Valid_Constraint_Kind
13494      (T_Kind          : Type_Kind;
13495       Constraint_Kind : Node_Kind) return Boolean
13496    is
13497    begin
13498       case T_Kind is
13499          when Enumeration_Kind |
13500               Integer_Kind =>
13501             return Constraint_Kind = N_Range_Constraint;
13502
13503          when Decimal_Fixed_Point_Kind =>
13504             return
13505               Constraint_Kind = N_Digits_Constraint
13506                 or else
13507               Constraint_Kind = N_Range_Constraint;
13508
13509          when Ordinary_Fixed_Point_Kind =>
13510             return
13511               Constraint_Kind = N_Delta_Constraint
13512                 or else
13513               Constraint_Kind = N_Range_Constraint;
13514
13515          when Float_Kind =>
13516             return
13517               Constraint_Kind = N_Digits_Constraint
13518                 or else
13519               Constraint_Kind = N_Range_Constraint;
13520
13521          when Access_Kind       |
13522               Array_Kind        |
13523               E_Record_Type     |
13524               E_Record_Subtype  |
13525               Class_Wide_Kind   |
13526               E_Incomplete_Type |
13527               Private_Kind      |
13528               Concurrent_Kind  =>
13529             return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
13530
13531          when others =>
13532             return True; -- Error will be detected later
13533       end case;
13534    end Is_Valid_Constraint_Kind;
13535
13536    --------------------------
13537    -- Is_Visible_Component --
13538    --------------------------
13539
13540    function Is_Visible_Component (C : Entity_Id) return Boolean is
13541       Original_Comp  : Entity_Id := Empty;
13542       Original_Scope : Entity_Id;
13543       Type_Scope     : Entity_Id;
13544
13545       function Is_Local_Type (Typ : Entity_Id) return Boolean;
13546       --  Check whether parent type of inherited component is declared locally,
13547       --  possibly within a nested package or instance. The current scope is
13548       --  the derived record itself.
13549
13550       -------------------
13551       -- Is_Local_Type --
13552       -------------------
13553
13554       function Is_Local_Type (Typ : Entity_Id) return Boolean is
13555          Scop : Entity_Id;
13556
13557       begin
13558          Scop := Scope (Typ);
13559          while Present (Scop)
13560            and then Scop /= Standard_Standard
13561          loop
13562             if Scop = Scope (Current_Scope) then
13563                return True;
13564             end if;
13565
13566             Scop := Scope (Scop);
13567          end loop;
13568
13569          return False;
13570       end Is_Local_Type;
13571
13572    --  Start of processing for Is_Visible_Component
13573
13574    begin
13575       if Ekind (C) = E_Component
13576         or else Ekind (C) = E_Discriminant
13577       then
13578          Original_Comp := Original_Record_Component (C);
13579       end if;
13580
13581       if No (Original_Comp) then
13582
13583          --  Premature usage, or previous error
13584
13585          return False;
13586
13587       else
13588          Original_Scope := Scope (Original_Comp);
13589          Type_Scope     := Scope (Base_Type (Scope (C)));
13590       end if;
13591
13592       --  This test only concerns tagged types
13593
13594       if not Is_Tagged_Type (Original_Scope) then
13595          return True;
13596
13597       --  If it is _Parent or _Tag, there is no visibility issue
13598
13599       elsif not Comes_From_Source (Original_Comp) then
13600          return True;
13601
13602       --  If we are in the body of an instantiation, the component is visible
13603       --  even when the parent type (possibly defined in an enclosing unit or
13604       --  in a parent unit) might not.
13605
13606       elsif In_Instance_Body then
13607          return True;
13608
13609       --  Discriminants are always visible
13610
13611       elsif Ekind (Original_Comp) = E_Discriminant
13612         and then not Has_Unknown_Discriminants (Original_Scope)
13613       then
13614          return True;
13615
13616       --  If the component has been declared in an ancestor which is currently
13617       --  a private type, then it is not visible. The same applies if the
13618       --  component's containing type is not in an open scope and the original
13619       --  component's enclosing type is a visible full view of a private type
13620       --  (which can occur in cases where an attempt is being made to reference
13621       --  a component in a sibling package that is inherited from a visible
13622       --  component of a type in an ancestor package; the component in the
13623       --  sibling package should not be visible even though the component it
13624       --  inherited from is visible). This does not apply however in the case
13625       --  where the scope of the type is a private child unit, or when the
13626       --  parent comes from a local package in which the ancestor is currently
13627       --  visible. The latter suppression of visibility is needed for cases
13628       --  that are tested in B730006.
13629
13630       elsif Is_Private_Type (Original_Scope)
13631         or else
13632           (not Is_Private_Descendant (Type_Scope)
13633             and then not In_Open_Scopes (Type_Scope)
13634             and then Has_Private_Declaration (Original_Scope))
13635       then
13636          --  If the type derives from an entity in a formal package, there
13637          --  are no additional visible components.
13638
13639          if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
13640             N_Formal_Package_Declaration
13641          then
13642             return False;
13643
13644          --  if we are not in the private part of the current package, there
13645          --  are no additional visible components.
13646
13647          elsif Ekind (Scope (Current_Scope)) = E_Package
13648            and then not In_Private_Part (Scope (Current_Scope))
13649          then
13650             return False;
13651          else
13652             return
13653               Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
13654                 and then In_Open_Scopes (Scope (Original_Scope))
13655                 and then Is_Local_Type (Type_Scope);
13656          end if;
13657
13658       --  There is another weird way in which a component may be invisible
13659       --  when the private and the full view are not derived from the same
13660       --  ancestor. Here is an example :
13661
13662       --       type A1 is tagged      record F1 : integer; end record;
13663       --       type A2 is new A1 with record F2 : integer; end record;
13664       --       type T is new A1 with private;
13665       --     private
13666       --       type T is new A2 with null record;
13667
13668       --  In this case, the full view of T inherits F1 and F2 but the private
13669       --  view inherits only F1
13670
13671       else
13672          declare
13673             Ancestor : Entity_Id := Scope (C);
13674
13675          begin
13676             loop
13677                if Ancestor = Original_Scope then
13678                   return True;
13679                elsif Ancestor = Etype (Ancestor) then
13680                   return False;
13681                end if;
13682
13683                Ancestor := Etype (Ancestor);
13684             end loop;
13685
13686             return True;
13687          end;
13688       end if;
13689    end Is_Visible_Component;
13690
13691    --------------------------
13692    -- Make_Class_Wide_Type --
13693    --------------------------
13694
13695    procedure Make_Class_Wide_Type (T : Entity_Id) is
13696       CW_Type : Entity_Id;
13697       CW_Name : Name_Id;
13698       Next_E  : Entity_Id;
13699
13700    begin
13701       --  The class wide type can have been defined by the partial view, in
13702       --  which case everything is already done.
13703
13704       if Present (Class_Wide_Type (T)) then
13705          return;
13706       end if;
13707
13708       CW_Type :=
13709         New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
13710
13711       --  Inherit root type characteristics
13712
13713       CW_Name := Chars (CW_Type);
13714       Next_E  := Next_Entity (CW_Type);
13715       Copy_Node (T, CW_Type);
13716       Set_Comes_From_Source (CW_Type, False);
13717       Set_Chars (CW_Type, CW_Name);
13718       Set_Parent (CW_Type, Parent (T));
13719       Set_Next_Entity (CW_Type, Next_E);
13720
13721       --  Ensure we have a new freeze node for the class-wide type. The partial
13722       --  view may have freeze action of its own, requiring a proper freeze
13723       --  node, and the same freeze node cannot be shared between the two
13724       --  types.
13725
13726       Set_Has_Delayed_Freeze (CW_Type);
13727       Set_Freeze_Node (CW_Type, Empty);
13728
13729       --  Customize the class-wide type: It has no prim. op., it cannot be
13730       --  abstract and its Etype points back to the specific root type.
13731
13732       Set_Ekind                (CW_Type, E_Class_Wide_Type);
13733       Set_Is_Tagged_Type       (CW_Type, True);
13734       Set_Primitive_Operations (CW_Type, New_Elmt_List);
13735       Set_Is_Abstract_Type     (CW_Type, False);
13736       Set_Is_Constrained       (CW_Type, False);
13737       Set_Is_First_Subtype     (CW_Type, Is_First_Subtype (T));
13738       Init_Size_Align          (CW_Type);
13739
13740       if Ekind (T) = E_Class_Wide_Subtype then
13741          Set_Etype             (CW_Type, Etype (Base_Type (T)));
13742       else
13743          Set_Etype             (CW_Type, T);
13744       end if;
13745
13746       --  If this is the class_wide type of a constrained subtype, it does
13747       --  not have discriminants.
13748
13749       Set_Has_Discriminants (CW_Type,
13750         Has_Discriminants (T) and then not Is_Constrained (T));
13751
13752       Set_Has_Unknown_Discriminants (CW_Type, True);
13753       Set_Class_Wide_Type (T, CW_Type);
13754       Set_Equivalent_Type (CW_Type, Empty);
13755
13756       --  The class-wide type of a class-wide type is itself (RM 3.9(14))
13757
13758       Set_Class_Wide_Type (CW_Type, CW_Type);
13759    end Make_Class_Wide_Type;
13760
13761    ----------------
13762    -- Make_Index --
13763    ----------------
13764
13765    procedure Make_Index
13766      (I            : Node_Id;
13767       Related_Nod  : Node_Id;
13768       Related_Id   : Entity_Id := Empty;
13769       Suffix_Index : Nat := 1)
13770    is
13771       R      : Node_Id;
13772       T      : Entity_Id;
13773       Def_Id : Entity_Id := Empty;
13774       Found  : Boolean := False;
13775
13776    begin
13777       --  For a discrete range used in a constrained array definition and
13778       --  defined by a range, an implicit conversion to the predefined type
13779       --  INTEGER is assumed if each bound is either a numeric literal, a named
13780       --  number, or an attribute, and the type of both bounds (prior to the
13781       --  implicit conversion) is the type universal_integer. Otherwise, both
13782       --  bounds must be of the same discrete type, other than universal
13783       --  integer; this type must be determinable independently of the
13784       --  context, but using the fact that the type must be discrete and that
13785       --  both bounds must have the same type.
13786
13787       --  Character literals also have a universal type in the absence of
13788       --  of additional context,  and are resolved to Standard_Character.
13789
13790       if Nkind (I) = N_Range then
13791
13792          --  The index is given by a range constraint. The bounds are known
13793          --  to be of a consistent type.
13794
13795          if not Is_Overloaded (I) then
13796             T := Etype (I);
13797
13798             --  For universal bounds, choose the specific predefined type
13799
13800             if T = Universal_Integer then
13801                T := Standard_Integer;
13802
13803             elsif T = Any_Character then
13804                Ambiguous_Character (Low_Bound (I));
13805
13806                T := Standard_Character;
13807             end if;
13808
13809          else
13810             T := Any_Type;
13811
13812             declare
13813                Ind : Interp_Index;
13814                It  : Interp;
13815
13816             begin
13817                Get_First_Interp (I, Ind, It);
13818                while Present (It.Typ) loop
13819                   if Is_Discrete_Type (It.Typ) then
13820
13821                      if Found
13822                        and then not Covers (It.Typ, T)
13823                        and then not Covers (T, It.Typ)
13824                      then
13825                         Error_Msg_N ("ambiguous bounds in discrete range", I);
13826                         exit;
13827                      else
13828                         T := It.Typ;
13829                         Found := True;
13830                      end if;
13831                   end if;
13832
13833                   Get_Next_Interp (Ind, It);
13834                end loop;
13835
13836                if T = Any_Type then
13837                   Error_Msg_N ("discrete type required for range", I);
13838                   Set_Etype (I, Any_Type);
13839                   return;
13840
13841                elsif T = Universal_Integer then
13842                   T := Standard_Integer;
13843                end if;
13844             end;
13845          end if;
13846
13847          if not Is_Discrete_Type (T) then
13848             Error_Msg_N ("discrete type required for range", I);
13849             Set_Etype (I, Any_Type);
13850             return;
13851          end if;
13852
13853          if Nkind (Low_Bound (I)) = N_Attribute_Reference
13854            and then Attribute_Name (Low_Bound (I)) = Name_First
13855            and then Is_Entity_Name (Prefix (Low_Bound (I)))
13856            and then Is_Type (Entity (Prefix (Low_Bound (I))))
13857            and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
13858          then
13859             --  The type of the index will be the type of the prefix, as long
13860             --  as the upper bound is 'Last of the same type.
13861
13862             Def_Id := Entity (Prefix (Low_Bound (I)));
13863
13864             if Nkind (High_Bound (I)) /= N_Attribute_Reference
13865               or else Attribute_Name (High_Bound (I)) /= Name_Last
13866               or else not Is_Entity_Name (Prefix (High_Bound (I)))
13867               or else Entity (Prefix (High_Bound (I))) /= Def_Id
13868             then
13869                Def_Id := Empty;
13870             end if;
13871          end if;
13872
13873          R := I;
13874          Process_Range_Expr_In_Decl (R, T);
13875
13876       elsif Nkind (I) = N_Subtype_Indication then
13877
13878          --  The index is given by a subtype with a range constraint
13879
13880          T :=  Base_Type (Entity (Subtype_Mark (I)));
13881
13882          if not Is_Discrete_Type (T) then
13883             Error_Msg_N ("discrete type required for range", I);
13884             Set_Etype (I, Any_Type);
13885             return;
13886          end if;
13887
13888          R := Range_Expression (Constraint (I));
13889
13890          Resolve (R, T);
13891          Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
13892
13893       elsif Nkind (I) = N_Attribute_Reference then
13894
13895          --  The parser guarantees that the attribute is a RANGE attribute
13896
13897          --  If the node denotes the range of a type mark, that is also the
13898          --  resulting type, and we do no need to create an Itype for it.
13899
13900          if Is_Entity_Name (Prefix (I))
13901            and then Comes_From_Source (I)
13902            and then Is_Type (Entity (Prefix (I)))
13903            and then Is_Discrete_Type (Entity (Prefix (I)))
13904          then
13905             Def_Id := Entity (Prefix (I));
13906          end if;
13907
13908          Analyze_And_Resolve (I);
13909          T := Etype (I);
13910          R := I;
13911
13912       --  If none of the above, must be a subtype. We convert this to a
13913       --  range attribute reference because in the case of declared first
13914       --  named subtypes, the types in the range reference can be different
13915       --  from the type of the entity. A range attribute normalizes the
13916       --  reference and obtains the correct types for the bounds.
13917
13918       --  This transformation is in the nature of an expansion, is only
13919       --  done if expansion is active. In particular, it is not done on
13920       --  formal generic types,  because we need to retain the name of the
13921       --  original index for instantiation purposes.
13922
13923       else
13924          if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
13925             Error_Msg_N ("invalid subtype mark in discrete range ", I);
13926             Set_Etype (I, Any_Integer);
13927             return;
13928
13929          else
13930             --  The type mark may be that of an incomplete type. It is only
13931             --  now that we can get the full view, previous analysis does
13932             --  not look specifically for a type mark.
13933
13934             Set_Entity (I, Get_Full_View (Entity (I)));
13935             Set_Etype  (I, Entity (I));
13936             Def_Id := Entity (I);
13937
13938             if not Is_Discrete_Type (Def_Id) then
13939                Error_Msg_N ("discrete type required for index", I);
13940                Set_Etype (I, Any_Type);
13941                return;
13942             end if;
13943          end if;
13944
13945          if Expander_Active then
13946             Rewrite (I,
13947               Make_Attribute_Reference (Sloc (I),
13948                 Attribute_Name => Name_Range,
13949                 Prefix         => Relocate_Node (I)));
13950
13951             --  The original was a subtype mark that does not freeze. This
13952             --  means that the rewritten version must not freeze either.
13953
13954             Set_Must_Not_Freeze (I);
13955             Set_Must_Not_Freeze (Prefix (I));
13956
13957             --  Is order critical??? if so, document why, if not
13958             --  use Analyze_And_Resolve
13959
13960             Analyze_And_Resolve (I);
13961             T := Etype (I);
13962             R := I;
13963
13964          --  If expander is inactive, type is legal, nothing else to construct
13965
13966          else
13967             return;
13968          end if;
13969       end if;
13970
13971       if not Is_Discrete_Type (T) then
13972          Error_Msg_N ("discrete type required for range", I);
13973          Set_Etype (I, Any_Type);
13974          return;
13975
13976       elsif T = Any_Type then
13977          Set_Etype (I, Any_Type);
13978          return;
13979       end if;
13980
13981       --  We will now create the appropriate Itype to describe the range, but
13982       --  first a check. If we originally had a subtype, then we just label
13983       --  the range with this subtype. Not only is there no need to construct
13984       --  a new subtype, but it is wrong to do so for two reasons:
13985
13986       --    1. A legality concern, if we have a subtype, it must not freeze,
13987       --       and the Itype would cause freezing incorrectly
13988
13989       --    2. An efficiency concern, if we created an Itype, it would not be
13990       --       recognized as the same type for the purposes of eliminating
13991       --       checks in some circumstances.
13992
13993       --  We signal this case by setting the subtype entity in Def_Id
13994
13995       if No (Def_Id) then
13996          Def_Id :=
13997            Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
13998          Set_Etype (Def_Id, Base_Type (T));
13999
14000          if Is_Signed_Integer_Type (T) then
14001             Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14002
14003          elsif Is_Modular_Integer_Type (T) then
14004             Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14005
14006          else
14007             Set_Ekind             (Def_Id, E_Enumeration_Subtype);
14008             Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14009             Set_First_Literal     (Def_Id, First_Literal (T));
14010          end if;
14011
14012          Set_Size_Info      (Def_Id,                  (T));
14013          Set_RM_Size        (Def_Id, RM_Size          (T));
14014          Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
14015
14016          Set_Scalar_Range   (Def_Id, R);
14017          Conditional_Delay  (Def_Id, T);
14018
14019          --  In the subtype indication case, if the immediate parent of the
14020          --  new subtype is non-static, then the subtype we create is non-
14021          --  static, even if its bounds are static.
14022
14023          if Nkind (I) = N_Subtype_Indication
14024            and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
14025          then
14026             Set_Is_Non_Static_Subtype (Def_Id);
14027          end if;
14028       end if;
14029
14030       --  Final step is to label the index with this constructed type
14031
14032       Set_Etype (I, Def_Id);
14033    end Make_Index;
14034
14035    ------------------------------
14036    -- Modular_Type_Declaration --
14037    ------------------------------
14038
14039    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14040       Mod_Expr : constant Node_Id := Expression (Def);
14041       M_Val    : Uint;
14042
14043       procedure Set_Modular_Size (Bits : Int);
14044       --  Sets RM_Size to Bits, and Esize to normal word size above this
14045
14046       ----------------------
14047       -- Set_Modular_Size --
14048       ----------------------
14049
14050       procedure Set_Modular_Size (Bits : Int) is
14051       begin
14052          Set_RM_Size (T, UI_From_Int (Bits));
14053
14054          if Bits <= 8 then
14055             Init_Esize (T, 8);
14056
14057          elsif Bits <= 16 then
14058             Init_Esize (T, 16);
14059
14060          elsif Bits <= 32 then
14061             Init_Esize (T, 32);
14062
14063          else
14064             Init_Esize (T, System_Max_Binary_Modulus_Power);
14065          end if;
14066       end Set_Modular_Size;
14067
14068    --  Start of processing for Modular_Type_Declaration
14069
14070    begin
14071       Analyze_And_Resolve (Mod_Expr, Any_Integer);
14072       Set_Etype (T, T);
14073       Set_Ekind (T, E_Modular_Integer_Type);
14074       Init_Alignment (T);
14075       Set_Is_Constrained (T);
14076
14077       if not Is_OK_Static_Expression (Mod_Expr) then
14078          Flag_Non_Static_Expr
14079            ("non-static expression used for modular type bound!", Mod_Expr);
14080          M_Val := 2 ** System_Max_Binary_Modulus_Power;
14081       else
14082          M_Val := Expr_Value (Mod_Expr);
14083       end if;
14084
14085       if M_Val < 1 then
14086          Error_Msg_N ("modulus value must be positive", Mod_Expr);
14087          M_Val := 2 ** System_Max_Binary_Modulus_Power;
14088       end if;
14089
14090       Set_Modulus (T, M_Val);
14091
14092       --   Create bounds for the modular type based on the modulus given in
14093       --   the type declaration and then analyze and resolve those bounds.
14094
14095       Set_Scalar_Range (T,
14096         Make_Range (Sloc (Mod_Expr),
14097           Low_Bound  =>
14098             Make_Integer_Literal (Sloc (Mod_Expr), 0),
14099           High_Bound =>
14100             Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
14101
14102       --  Properly analyze the literals for the range. We do this manually
14103       --  because we can't go calling Resolve, since we are resolving these
14104       --  bounds with the type, and this type is certainly not complete yet!
14105
14106       Set_Etype (Low_Bound  (Scalar_Range (T)), T);
14107       Set_Etype (High_Bound (Scalar_Range (T)), T);
14108       Set_Is_Static_Expression (Low_Bound  (Scalar_Range (T)));
14109       Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
14110
14111       --  Loop through powers of two to find number of bits required
14112
14113       for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
14114
14115          --  Binary case
14116
14117          if M_Val = 2 ** Bits then
14118             Set_Modular_Size (Bits);
14119             return;
14120
14121          --  Non-binary case
14122
14123          elsif M_Val < 2 ** Bits then
14124             Set_Non_Binary_Modulus (T);
14125
14126             if Bits > System_Max_Nonbinary_Modulus_Power then
14127                Error_Msg_Uint_1 :=
14128                  UI_From_Int (System_Max_Nonbinary_Modulus_Power);
14129                Error_Msg_F
14130                  ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
14131                Set_Modular_Size (System_Max_Binary_Modulus_Power);
14132                return;
14133
14134             else
14135                --  In the non-binary case, set size as per RM 13.3(55)
14136
14137                Set_Modular_Size (Bits);
14138                return;
14139             end if;
14140          end if;
14141
14142       end loop;
14143
14144       --  If we fall through, then the size exceed System.Max_Binary_Modulus
14145       --  so we just signal an error and set the maximum size.
14146
14147       Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
14148       Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
14149
14150       Set_Modular_Size (System_Max_Binary_Modulus_Power);
14151       Init_Alignment (T);
14152
14153    end Modular_Type_Declaration;
14154
14155    --------------------------
14156    -- New_Concatenation_Op --
14157    --------------------------
14158
14159    procedure New_Concatenation_Op (Typ : Entity_Id) is
14160       Loc : constant Source_Ptr := Sloc (Typ);
14161       Op  : Entity_Id;
14162
14163       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
14164       --  Create abbreviated declaration for the formal of a predefined
14165       --  Operator 'Op' of type 'Typ'
14166
14167       --------------------
14168       -- Make_Op_Formal --
14169       --------------------
14170
14171       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
14172          Formal : Entity_Id;
14173       begin
14174          Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
14175          Set_Etype (Formal, Typ);
14176          Set_Mechanism (Formal, Default_Mechanism);
14177          return Formal;
14178       end Make_Op_Formal;
14179
14180    --  Start of processing for New_Concatenation_Op
14181
14182    begin
14183       Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
14184
14185       Set_Ekind                   (Op, E_Operator);
14186       Set_Scope                   (Op, Current_Scope);
14187       Set_Etype                   (Op, Typ);
14188       Set_Homonym                 (Op, Get_Name_Entity_Id (Name_Op_Concat));
14189       Set_Is_Immediately_Visible  (Op);
14190       Set_Is_Intrinsic_Subprogram (Op);
14191       Set_Has_Completion          (Op);
14192       Append_Entity               (Op, Current_Scope);
14193
14194       Set_Name_Entity_Id (Name_Op_Concat, Op);
14195
14196       Append_Entity (Make_Op_Formal (Typ, Op), Op);
14197       Append_Entity (Make_Op_Formal (Typ, Op), Op);
14198    end New_Concatenation_Op;
14199
14200    -------------------------
14201    -- OK_For_Limited_Init --
14202    -------------------------
14203
14204    --  ???Check all calls of this, and compare the conditions under which it's
14205    --  called.
14206
14207    function OK_For_Limited_Init (Exp : Node_Id) return Boolean is
14208    begin
14209       return Ada_Version >= Ada_05
14210         and then not Debug_Flag_Dot_L
14211         and then OK_For_Limited_Init_In_05 (Exp);
14212    end OK_For_Limited_Init;
14213
14214    -------------------------------
14215    -- OK_For_Limited_Init_In_05 --
14216    -------------------------------
14217
14218    function OK_For_Limited_Init_In_05 (Exp : Node_Id) return Boolean is
14219    begin
14220       --  ???Expand_N_Extended_Return_Statement generates code that would
14221       --  violate the rules in some cases. Once we have build-in-place
14222       --  function returns working, we can probably remove the following
14223       --  check.
14224
14225       if not Comes_From_Source (Exp) then
14226          return True;
14227       end if;
14228
14229       --  Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
14230       --  case of limited aggregates (including extension aggregates), and
14231       --  function calls. The function call may have been give in prefixed
14232       --  notation, in which case the original node is an indexed component.
14233
14234       case Nkind (Original_Node (Exp)) is
14235          when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
14236             return True;
14237
14238          --  Ada 2005 (AI-251): If a class-wide interface object is initialized
14239          --  with a function call, the expander has rewritten the call into an
14240          --  N_Type_Conversion node to force displacement of the pointer to
14241          --  reference the component containing the secondary dispatch table.
14242
14243          when N_Qualified_Expression | N_Type_Conversion =>
14244             return OK_For_Limited_Init_In_05
14245                      (Expression (Original_Node (Exp)));
14246
14247          when N_Indexed_Component | N_Selected_Component  =>
14248             return Nkind (Exp) = N_Function_Call;
14249
14250          --  A use of 'Input is a function call, hence allowed. Normally the
14251          --  attribute will be changed to a call, but the attribute by itself
14252          --  can occur with -gnatc.
14253
14254          when N_Attribute_Reference =>
14255             return Attribute_Name (Original_Node (Exp)) = Name_Input;
14256
14257          when others =>
14258             return False;
14259       end case;
14260    end OK_For_Limited_Init_In_05;
14261
14262    -------------------------------------------
14263    -- Ordinary_Fixed_Point_Type_Declaration --
14264    -------------------------------------------
14265
14266    procedure Ordinary_Fixed_Point_Type_Declaration
14267      (T   : Entity_Id;
14268       Def : Node_Id)
14269    is
14270       Loc           : constant Source_Ptr := Sloc (Def);
14271       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
14272       RRS           : constant Node_Id    := Real_Range_Specification (Def);
14273       Implicit_Base : Entity_Id;
14274       Delta_Val     : Ureal;
14275       Small_Val     : Ureal;
14276       Low_Val       : Ureal;
14277       High_Val      : Ureal;
14278
14279    begin
14280       Check_Restriction (No_Fixed_Point, Def);
14281
14282       --  Create implicit base type
14283
14284       Implicit_Base :=
14285         Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
14286       Set_Etype (Implicit_Base, Implicit_Base);
14287
14288       --  Analyze and process delta expression
14289
14290       Analyze_And_Resolve (Delta_Expr, Any_Real);
14291
14292       Check_Delta_Expression (Delta_Expr);
14293       Delta_Val := Expr_Value_R (Delta_Expr);
14294
14295       Set_Delta_Value (Implicit_Base, Delta_Val);
14296
14297       --  Compute default small from given delta, which is the largest power
14298       --  of two that does not exceed the given delta value.
14299
14300       declare
14301          Tmp   : Ureal;
14302          Scale : Int;
14303
14304       begin
14305          Tmp := Ureal_1;
14306          Scale := 0;
14307
14308          if Delta_Val < Ureal_1 then
14309             while Delta_Val < Tmp loop
14310                Tmp := Tmp / Ureal_2;
14311                Scale := Scale + 1;
14312             end loop;
14313
14314          else
14315             loop
14316                Tmp := Tmp * Ureal_2;
14317                exit when Tmp > Delta_Val;
14318                Scale := Scale - 1;
14319             end loop;
14320          end if;
14321
14322          Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
14323       end;
14324
14325       Set_Small_Value (Implicit_Base, Small_Val);
14326
14327       --  If no range was given, set a dummy range
14328
14329       if RRS <= Empty_Or_Error then
14330          Low_Val  := -Small_Val;
14331          High_Val := Small_Val;
14332
14333       --  Otherwise analyze and process given range
14334
14335       else
14336          declare
14337             Low  : constant Node_Id := Low_Bound  (RRS);
14338             High : constant Node_Id := High_Bound (RRS);
14339
14340          begin
14341             Analyze_And_Resolve (Low, Any_Real);
14342             Analyze_And_Resolve (High, Any_Real);
14343             Check_Real_Bound (Low);
14344             Check_Real_Bound (High);
14345
14346             --  Obtain and set the range
14347
14348             Low_Val  := Expr_Value_R (Low);
14349             High_Val := Expr_Value_R (High);
14350
14351             if Low_Val > High_Val then
14352                Error_Msg_NE ("?fixed point type& has null range", Def, T);
14353             end if;
14354          end;
14355       end if;
14356
14357       --  The range for both the implicit base and the declared first subtype
14358       --  cannot be set yet, so we use the special routine Set_Fixed_Range to
14359       --  set a temporary range in place. Note that the bounds of the base
14360       --  type will be widened to be symmetrical and to fill the available
14361       --  bits when the type is frozen.
14362
14363       --  We could do this with all discrete types, and probably should, but
14364       --  we absolutely have to do it for fixed-point, since the end-points
14365       --  of the range and the size are determined by the small value, which
14366       --  could be reset before the freeze point.
14367
14368       Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
14369       Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14370
14371       Init_Size_Align (Implicit_Base);
14372
14373       --  Complete definition of first subtype
14374
14375       Set_Ekind          (T, E_Ordinary_Fixed_Point_Subtype);
14376       Set_Etype          (T, Implicit_Base);
14377       Init_Size_Align    (T);
14378       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
14379       Set_Small_Value    (T, Small_Val);
14380       Set_Delta_Value    (T, Delta_Val);
14381       Set_Is_Constrained (T);
14382
14383    end Ordinary_Fixed_Point_Type_Declaration;
14384
14385    ----------------------------------------
14386    -- Prepare_Private_Subtype_Completion --
14387    ----------------------------------------
14388
14389    procedure Prepare_Private_Subtype_Completion
14390      (Id          : Entity_Id;
14391       Related_Nod : Node_Id)
14392    is
14393       Id_B   : constant Entity_Id := Base_Type (Id);
14394       Full_B : constant Entity_Id := Full_View (Id_B);
14395       Full   : Entity_Id;
14396
14397    begin
14398       if Present (Full_B) then
14399
14400          --  The Base_Type is already completed, we can complete the subtype
14401          --  now. We have to create a new entity with the same name, Thus we
14402          --  can't use Create_Itype.
14403
14404          --  This is messy, should be fixed ???
14405
14406          Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
14407          Set_Is_Itype (Full);
14408          Set_Associated_Node_For_Itype (Full, Related_Nod);
14409          Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
14410       end if;
14411
14412       --  The parent subtype may be private, but the base might not, in some
14413       --  nested instances. In that case, the subtype does not need to be
14414       --  exchanged. It would still be nice to make private subtypes and their
14415       --  bases consistent at all times ???
14416
14417       if Is_Private_Type (Id_B) then
14418          Append_Elmt (Id, Private_Dependents (Id_B));
14419       end if;
14420
14421    end Prepare_Private_Subtype_Completion;
14422
14423    ---------------------------
14424    -- Process_Discriminants --
14425    ---------------------------
14426
14427    procedure Process_Discriminants
14428      (N    : Node_Id;
14429       Prev : Entity_Id := Empty)
14430    is
14431       Elist               : constant Elist_Id := New_Elmt_List;
14432       Id                  : Node_Id;
14433       Discr               : Node_Id;
14434       Discr_Number        : Uint;
14435       Discr_Type          : Entity_Id;
14436       Default_Present     : Boolean := False;
14437       Default_Not_Present : Boolean := False;
14438
14439    begin
14440       --  A composite type other than an array type can have discriminants.
14441       --  On entry, the current scope is the composite type.
14442
14443       --  The discriminants are initially entered into the scope of the type
14444       --  via Enter_Name with the default Ekind of E_Void to prevent premature
14445       --  use, as explained at the end of this procedure.
14446
14447       Discr := First (Discriminant_Specifications (N));
14448       while Present (Discr) loop
14449          Enter_Name (Defining_Identifier (Discr));
14450
14451          --  For navigation purposes we add a reference to the discriminant
14452          --  in the entity for the type. If the current declaration is a
14453          --  completion, place references on the partial view. Otherwise the
14454          --  type is the current scope.
14455
14456          if Present (Prev) then
14457
14458             --  The references go on the partial view, if present. If the
14459             --  partial view has discriminants, the references have been
14460             --  generated already.
14461
14462             if not Has_Discriminants (Prev) then
14463                Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
14464             end if;
14465          else
14466             Generate_Reference
14467               (Current_Scope, Defining_Identifier (Discr), 'd');
14468          end if;
14469
14470          if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
14471             Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
14472
14473             --  Ada 2005 (AI-254)
14474
14475             if Present (Access_To_Subprogram_Definition
14476                          (Discriminant_Type (Discr)))
14477               and then Protected_Present (Access_To_Subprogram_Definition
14478                                            (Discriminant_Type (Discr)))
14479             then
14480                Discr_Type :=
14481                  Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
14482             end if;
14483
14484          else
14485             Find_Type (Discriminant_Type (Discr));
14486             Discr_Type := Etype (Discriminant_Type (Discr));
14487
14488             if Error_Posted (Discriminant_Type (Discr)) then
14489                Discr_Type := Any_Type;
14490             end if;
14491          end if;
14492
14493          if Is_Access_Type (Discr_Type) then
14494
14495             --  Ada 2005 (AI-230): Access discriminant allowed in non-limited
14496             --  record types
14497
14498             if Ada_Version < Ada_05 then
14499                Check_Access_Discriminant_Requires_Limited
14500                  (Discr, Discriminant_Type (Discr));
14501             end if;
14502
14503             if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
14504                Error_Msg_N
14505                  ("(Ada 83) access discriminant not allowed", Discr);
14506             end if;
14507
14508          elsif not Is_Discrete_Type (Discr_Type) then
14509             Error_Msg_N ("discriminants must have a discrete or access type",
14510               Discriminant_Type (Discr));
14511          end if;
14512
14513          Set_Etype (Defining_Identifier (Discr), Discr_Type);
14514
14515          --  If a discriminant specification includes the assignment compound
14516          --  delimiter followed by an expression, the expression is the default
14517          --  expression of the discriminant; the default expression must be of
14518          --  the type of the discriminant. (RM 3.7.1) Since this expression is
14519          --  a default expression, we do the special preanalysis, since this
14520          --  expression does not freeze (see "Handling of Default and Per-
14521          --  Object Expressions" in spec of package Sem).
14522
14523          if Present (Expression (Discr)) then
14524             Analyze_Per_Use_Expression (Expression (Discr), Discr_Type);
14525
14526             if Nkind (N) = N_Formal_Type_Declaration then
14527                Error_Msg_N
14528                  ("discriminant defaults not allowed for formal type",
14529                   Expression (Discr));
14530
14531             --  Tagged types cannot have defaulted discriminants, but a
14532             --  non-tagged private type with defaulted discriminants
14533             --   can have a tagged completion.
14534
14535             elsif Is_Tagged_Type (Current_Scope)
14536               and then Comes_From_Source (N)
14537             then
14538                Error_Msg_N
14539                  ("discriminants of tagged type cannot have defaults",
14540                   Expression (Discr));
14541
14542             else
14543                Default_Present := True;
14544                Append_Elmt (Expression (Discr), Elist);
14545
14546                --  Tag the defining identifiers for the discriminants with
14547                --  their corresponding default expressions from the tree.
14548
14549                Set_Discriminant_Default_Value
14550                  (Defining_Identifier (Discr), Expression (Discr));
14551             end if;
14552
14553          else
14554             Default_Not_Present := True;
14555          end if;
14556
14557          --  Ada 2005 (AI-231): Create an Itype that is a duplicate of
14558          --  Discr_Type but with the null-exclusion attribute
14559
14560          if Ada_Version >= Ada_05 then
14561
14562             --  Ada 2005 (AI-231): Static checks
14563
14564             if Can_Never_Be_Null (Discr_Type) then
14565                Null_Exclusion_Static_Checks (Discr);
14566
14567             elsif Is_Access_Type (Discr_Type)
14568               and then Null_Exclusion_Present (Discr)
14569
14570                --  No need to check itypes because in their case this check
14571                --  was done at their point of creation
14572
14573               and then not Is_Itype (Discr_Type)
14574             then
14575                if Can_Never_Be_Null (Discr_Type) then
14576                   Error_Msg_NE
14577                     ("`NOT NULL` not allowed (& already excludes null)",
14578                      Discr,
14579                      Discr_Type);
14580                end if;
14581
14582                Set_Etype (Defining_Identifier (Discr),
14583                  Create_Null_Excluding_Itype
14584                    (T           => Discr_Type,
14585                     Related_Nod => Discr));
14586             end if;
14587
14588             --  Ada 2005 (AI-402): access discriminants of nonlimited types
14589             --  can't have defaults
14590
14591             if Is_Access_Type (Discr_Type) then
14592                if Ekind (Discr_Type) /= E_Anonymous_Access_Type
14593                  or else not Default_Present
14594                  or else Is_Limited_Record (Current_Scope)
14595                  or else Is_Concurrent_Type (Current_Scope)
14596                  or else Is_Concurrent_Record_Type (Current_Scope)
14597                  or else Ekind (Current_Scope) = E_Limited_Private_Type
14598                then
14599                   null;
14600
14601                elsif Present (Expression (Discr)) then
14602                   Error_Msg_N
14603                     ("(Ada 2005) access discriminants of nonlimited types",
14604                      Expression (Discr));
14605                   Error_Msg_N ("\cannot have defaults", Expression (Discr));
14606                end if;
14607             end if;
14608          end if;
14609
14610          Next (Discr);
14611       end loop;
14612
14613       --  An element list consisting of the default expressions of the
14614       --  discriminants is constructed in the above loop and used to set
14615       --  the Discriminant_Constraint attribute for the type. If an object
14616       --  is declared of this (record or task) type without any explicit
14617       --  discriminant constraint given, this element list will form the
14618       --  actual parameters for the corresponding initialization procedure
14619       --  for the type.
14620
14621       Set_Discriminant_Constraint (Current_Scope, Elist);
14622       Set_Stored_Constraint (Current_Scope, No_Elist);
14623
14624       --  Default expressions must be provided either for all or for none
14625       --  of the discriminants of a discriminant part. (RM 3.7.1)
14626
14627       if Default_Present and then Default_Not_Present then
14628          Error_Msg_N
14629            ("incomplete specification of defaults for discriminants", N);
14630       end if;
14631
14632       --  The use of the name of a discriminant is not allowed in default
14633       --  expressions of a discriminant part if the specification of the
14634       --  discriminant is itself given in the discriminant part. (RM 3.7.1)
14635
14636       --  To detect this, the discriminant names are entered initially with an
14637       --  Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
14638       --  attempt to use a void entity (for example in an expression that is
14639       --  type-checked) produces the error message: premature usage. Now after
14640       --  completing the semantic analysis of the discriminant part, we can set
14641       --  the Ekind of all the discriminants appropriately.
14642
14643       Discr := First (Discriminant_Specifications (N));
14644       Discr_Number := Uint_1;
14645       while Present (Discr) loop
14646          Id := Defining_Identifier (Discr);
14647          Set_Ekind (Id, E_Discriminant);
14648          Init_Component_Location (Id);
14649          Init_Esize (Id);
14650          Set_Discriminant_Number (Id, Discr_Number);
14651
14652          --  Make sure this is always set, even in illegal programs
14653
14654          Set_Corresponding_Discriminant (Id, Empty);
14655
14656          --  Initialize the Original_Record_Component to the entity itself.
14657          --  Inherit_Components will propagate the right value to
14658          --  discriminants in derived record types.
14659
14660          Set_Original_Record_Component (Id, Id);
14661
14662          --  Create the discriminal for the discriminant
14663
14664          Build_Discriminal (Id);
14665
14666          Next (Discr);
14667          Discr_Number := Discr_Number + 1;
14668       end loop;
14669
14670       Set_Has_Discriminants (Current_Scope);
14671    end Process_Discriminants;
14672
14673    -----------------------
14674    -- Process_Full_View --
14675    -----------------------
14676
14677    procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
14678       Priv_Parent : Entity_Id;
14679       Full_Parent : Entity_Id;
14680       Full_Indic  : Node_Id;
14681
14682       procedure Collect_Implemented_Interfaces
14683         (Typ    : Entity_Id;
14684          Ifaces : Elist_Id);
14685       --  Ada 2005: Gather all the interfaces that Typ directly or
14686       --  inherently implements. Duplicate entries are not added to
14687       --  the list Ifaces.
14688
14689       ------------------------------------
14690       -- Collect_Implemented_Interfaces --
14691       ------------------------------------
14692
14693       procedure Collect_Implemented_Interfaces
14694         (Typ    : Entity_Id;
14695          Ifaces : Elist_Id)
14696       is
14697          Iface      : Entity_Id;
14698          Iface_Elmt : Elmt_Id;
14699
14700       begin
14701          --  Abstract interfaces are only associated with tagged record types
14702
14703          if not Is_Tagged_Type (Typ)
14704            or else not Is_Record_Type (Typ)
14705          then
14706             return;
14707          end if;
14708
14709          --  Recursively climb to the ancestors
14710
14711          if Etype (Typ) /= Typ
14712
14713             --  Protect the frontend against wrong cyclic declarations like:
14714
14715             --     type B is new A with private;
14716             --     type C is new A with private;
14717             --  private
14718             --     type B is new C with null record;
14719             --     type C is new B with null record;
14720
14721            and then Etype (Typ) /= Priv_T
14722            and then Etype (Typ) /= Full_T
14723          then
14724             --  Keep separate the management of private type declarations
14725
14726             if Ekind (Typ) = E_Record_Type_With_Private then
14727
14728                --  Handle the following erronous case:
14729                --      type Private_Type is tagged private;
14730                --   private
14731                --      type Private_Type is new Type_Implementing_Iface;
14732
14733                if Present (Full_View (Typ))
14734                  and then Etype (Typ) /= Full_View (Typ)
14735                then
14736                   if Is_Interface (Etype (Typ)) then
14737                      Append_Unique_Elmt (Etype (Typ), Ifaces);
14738                   end if;
14739
14740                   Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
14741                end if;
14742
14743             --  Non-private types
14744
14745             else
14746                if Is_Interface (Etype (Typ)) then
14747                   Append_Unique_Elmt (Etype (Typ), Ifaces);
14748                end if;
14749
14750                Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
14751             end if;
14752          end if;
14753
14754          --  Handle entities in the list of abstract interfaces
14755
14756          if Present (Abstract_Interfaces (Typ)) then
14757             Iface_Elmt := First_Elmt (Abstract_Interfaces (Typ));
14758             while Present (Iface_Elmt) loop
14759                Iface := Node (Iface_Elmt);
14760
14761                pragma Assert (Is_Interface (Iface));
14762
14763                if not Contain_Interface (Iface, Ifaces) then
14764                   Append_Elmt (Iface, Ifaces);
14765                   Collect_Implemented_Interfaces (Iface, Ifaces);
14766                end if;
14767
14768                Next_Elmt (Iface_Elmt);
14769             end loop;
14770          end if;
14771       end Collect_Implemented_Interfaces;
14772
14773    --  Start of processing for Process_Full_View
14774
14775    begin
14776       --  First some sanity checks that must be done after semantic
14777       --  decoration of the full view and thus cannot be placed with other
14778       --  similar checks in Find_Type_Name
14779
14780       if not Is_Limited_Type (Priv_T)
14781         and then (Is_Limited_Type (Full_T)
14782                    or else Is_Limited_Composite (Full_T))
14783       then
14784          Error_Msg_N
14785            ("completion of nonlimited type cannot be limited", Full_T);
14786          Explain_Limited_Type (Full_T, Full_T);
14787
14788       elsif Is_Abstract_Type (Full_T)
14789         and then not Is_Abstract_Type (Priv_T)
14790       then
14791          Error_Msg_N
14792            ("completion of nonabstract type cannot be abstract", Full_T);
14793
14794       elsif Is_Tagged_Type (Priv_T)
14795         and then Is_Limited_Type (Priv_T)
14796         and then not Is_Limited_Type (Full_T)
14797       then
14798          --  If pragma CPP_Class was applied to the private declaration
14799          --  propagate the limitedness to the full-view
14800
14801          if Is_CPP_Class (Priv_T) then
14802             Set_Is_Limited_Record (Full_T);
14803
14804          --  GNAT allow its own definition of Limited_Controlled to disobey
14805          --  this rule in order in ease the implementation. The next test is
14806          --  safe because Root_Controlled is defined in a private system child
14807
14808          elsif Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
14809             Set_Is_Limited_Composite (Full_T);
14810          else
14811             Error_Msg_N
14812               ("completion of limited tagged type must be limited", Full_T);
14813          end if;
14814
14815       elsif Is_Generic_Type (Priv_T) then
14816          Error_Msg_N ("generic type cannot have a completion", Full_T);
14817       end if;
14818
14819       --  Check that ancestor interfaces of private and full views are
14820       --  consistent. We omit this check for synchronized types because
14821       --  they are performed on the corresponding record type when frozen.
14822
14823       if Ada_Version >= Ada_05
14824         and then Is_Tagged_Type (Priv_T)
14825         and then Is_Tagged_Type (Full_T)
14826         and then not Is_Concurrent_Type (Full_T)
14827       then
14828          declare
14829             Iface         : Entity_Id;
14830             Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
14831             Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
14832
14833          begin
14834             Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
14835             Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
14836
14837             --  Ada 2005 (AI-251): The partial view shall be a descendant of
14838             --  an interface type if and only if the full type is descendant
14839             --  of the interface type (AARM 7.3 (7.3/2).
14840
14841             Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
14842
14843             if Present (Iface) then
14844                Error_Msg_NE ("interface & not implemented by full type " &
14845                              "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
14846             end if;
14847
14848             Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
14849
14850             if Present (Iface) then
14851                Error_Msg_NE ("interface & not implemented by partial view " &
14852                              "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
14853             end if;
14854          end;
14855       end if;
14856
14857       if Is_Tagged_Type (Priv_T)
14858         and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
14859         and then Is_Derived_Type (Full_T)
14860       then
14861          Priv_Parent := Etype (Priv_T);
14862
14863          --  The full view of a private extension may have been transformed
14864          --  into an unconstrained derived type declaration and a subtype
14865          --  declaration (see build_derived_record_type for details).
14866
14867          if Nkind (N) = N_Subtype_Declaration then
14868             Full_Indic  := Subtype_Indication (N);
14869             Full_Parent := Etype (Base_Type (Full_T));
14870          else
14871             Full_Indic  := Subtype_Indication (Type_Definition (N));
14872             Full_Parent := Etype (Full_T);
14873          end if;
14874
14875          --  Check that the parent type of the full type is a descendant of
14876          --  the ancestor subtype given in the private extension. If either
14877          --  entity has an Etype equal to Any_Type then we had some previous
14878          --  error situation [7.3(8)].
14879
14880          if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
14881             return;
14882
14883          --  Ada 2005 (AI-251): Interfaces in the full-typ can be given in
14884          --  any order. Therefore we don't have to check that its parent must
14885          --  be a descendant of the parent of the private type declaration.
14886
14887          elsif Is_Interface (Priv_Parent)
14888            and then Is_Interface (Full_Parent)
14889          then
14890             null;
14891
14892          --  Ada 2005 (AI-251): If the parent of the private type declaration
14893          --  is an interface there is no need to check that it is an ancestor
14894          --  of the associated full type declaration. The required tests for
14895          --  this case case are performed by Build_Derived_Record_Type.
14896
14897          elsif not Is_Interface (Base_Type (Priv_Parent))
14898            and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
14899          then
14900             Error_Msg_N
14901               ("parent of full type must descend from parent"
14902                   & " of private extension", Full_Indic);
14903
14904          --  Check the rules of 7.3(10): if the private extension inherits
14905          --  known discriminants, then the full type must also inherit those
14906          --  discriminants from the same (ancestor) type, and the parent
14907          --  subtype of the full type must be constrained if and only if
14908          --  the ancestor subtype of the private extension is constrained.
14909
14910          elsif No (Discriminant_Specifications (Parent (Priv_T)))
14911            and then not Has_Unknown_Discriminants (Priv_T)
14912            and then Has_Discriminants (Base_Type (Priv_Parent))
14913          then
14914             declare
14915                Priv_Indic  : constant Node_Id :=
14916                                Subtype_Indication (Parent (Priv_T));
14917
14918                Priv_Constr : constant Boolean :=
14919                                Is_Constrained (Priv_Parent)
14920                                  or else
14921                                    Nkind (Priv_Indic) = N_Subtype_Indication
14922                                  or else Is_Constrained (Entity (Priv_Indic));
14923
14924                Full_Constr : constant Boolean :=
14925                                Is_Constrained (Full_Parent)
14926                                  or else
14927                                    Nkind (Full_Indic) = N_Subtype_Indication
14928                                  or else Is_Constrained (Entity (Full_Indic));
14929
14930                Priv_Discr : Entity_Id;
14931                Full_Discr : Entity_Id;
14932
14933             begin
14934                Priv_Discr := First_Discriminant (Priv_Parent);
14935                Full_Discr := First_Discriminant (Full_Parent);
14936                while Present (Priv_Discr) and then Present (Full_Discr) loop
14937                   if Original_Record_Component (Priv_Discr) =
14938                      Original_Record_Component (Full_Discr)
14939                     or else
14940                      Corresponding_Discriminant (Priv_Discr) =
14941                      Corresponding_Discriminant (Full_Discr)
14942                   then
14943                      null;
14944                   else
14945                      exit;
14946                   end if;
14947
14948                   Next_Discriminant (Priv_Discr);
14949                   Next_Discriminant (Full_Discr);
14950                end loop;
14951
14952                if Present (Priv_Discr) or else Present (Full_Discr) then
14953                   Error_Msg_N
14954                     ("full view must inherit discriminants of the parent type"
14955                      & " used in the private extension", Full_Indic);
14956
14957                elsif Priv_Constr and then not Full_Constr then
14958                   Error_Msg_N
14959                     ("parent subtype of full type must be constrained",
14960                      Full_Indic);
14961
14962                elsif Full_Constr and then not Priv_Constr then
14963                   Error_Msg_N
14964                     ("parent subtype of full type must be unconstrained",
14965                      Full_Indic);
14966                end if;
14967             end;
14968
14969          --  Check the rules of 7.3(12): if a partial view has neither known
14970          --  or unknown discriminants, then the full type declaration shall
14971          --  define a definite subtype.
14972
14973          elsif      not Has_Unknown_Discriminants (Priv_T)
14974            and then not Has_Discriminants (Priv_T)
14975            and then not Is_Constrained (Full_T)
14976          then
14977             Error_Msg_N
14978               ("full view must define a constrained type if partial view"
14979                 & " has no discriminants", Full_T);
14980          end if;
14981
14982          --  ??????? Do we implement the following properly ?????
14983          --  If the ancestor subtype of a private extension has constrained
14984          --  discriminants, then the parent subtype of the full view shall
14985          --  impose a statically matching constraint on those discriminants
14986          --  [7.3(13)].
14987
14988       else
14989          --  For untagged types, verify that a type without discriminants
14990          --  is not completed with an unconstrained type.
14991
14992          if not Is_Indefinite_Subtype (Priv_T)
14993            and then Is_Indefinite_Subtype (Full_T)
14994          then
14995             Error_Msg_N ("full view of type must be definite subtype", Full_T);
14996          end if;
14997       end if;
14998
14999       --  AI-419: verify that the use of "limited" is consistent
15000
15001       declare
15002          Orig_Decl : constant Node_Id := Original_Node (N);
15003
15004       begin
15005          if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15006            and then not Limited_Present (Parent (Priv_T))
15007            and then not Synchronized_Present (Parent (Priv_T))
15008            and then Nkind (Orig_Decl) = N_Full_Type_Declaration
15009            and then Nkind
15010              (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
15011            and then Limited_Present (Type_Definition (Orig_Decl))
15012          then
15013             Error_Msg_N
15014               ("full view of non-limited extension cannot be limited", N);
15015          end if;
15016       end;
15017
15018       --  Ada 2005 (AI-443): A synchronized private extension must be
15019       --  completed by a task or protected type.
15020
15021       if Ada_Version >= Ada_05
15022         and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15023         and then Synchronized_Present (Parent (Priv_T))
15024         and then not Is_Concurrent_Type (Full_T)
15025       then
15026          Error_Msg_N ("full view of synchronized extension must " &
15027                       "be synchronized type", N);
15028       end if;
15029
15030       --  Ada 2005 AI-363: if the full view has discriminants with
15031       --  defaults, it is illegal to declare constrained access subtypes
15032       --  whose designated type is the current type. This allows objects
15033       --  of the type that are declared in the heap to be unconstrained.
15034
15035       if not Has_Unknown_Discriminants (Priv_T)
15036         and then not Has_Discriminants (Priv_T)
15037         and then Has_Discriminants (Full_T)
15038         and then
15039           Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
15040       then
15041          Set_Has_Constrained_Partial_View (Full_T);
15042          Set_Has_Constrained_Partial_View (Priv_T);
15043       end if;
15044
15045       --  Create a full declaration for all its subtypes recorded in
15046       --  Private_Dependents and swap them similarly to the base type. These
15047       --  are subtypes that have been define before the full declaration of
15048       --  the private type. We also swap the entry in Private_Dependents list
15049       --  so we can properly restore the private view on exit from the scope.
15050
15051       declare
15052          Priv_Elmt : Elmt_Id;
15053          Priv      : Entity_Id;
15054          Full      : Entity_Id;
15055
15056       begin
15057          Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
15058          while Present (Priv_Elmt) loop
15059             Priv := Node (Priv_Elmt);
15060
15061             if Ekind (Priv) = E_Private_Subtype
15062               or else Ekind (Priv) = E_Limited_Private_Subtype
15063               or else Ekind (Priv) = E_Record_Subtype_With_Private
15064             then
15065                Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
15066                Set_Is_Itype (Full);
15067                Set_Parent (Full, Parent (Priv));
15068                Set_Associated_Node_For_Itype (Full, N);
15069
15070                --  Now we need to complete the private subtype, but since the
15071                --  base type has already been swapped, we must also swap the
15072                --  subtypes (and thus, reverse the arguments in the call to
15073                --  Complete_Private_Subtype).
15074
15075                Copy_And_Swap (Priv, Full);
15076                Complete_Private_Subtype (Full, Priv, Full_T, N);
15077                Replace_Elmt (Priv_Elmt, Full);
15078             end if;
15079
15080             Next_Elmt (Priv_Elmt);
15081          end loop;
15082       end;
15083
15084       --  If the private view was tagged, copy the new primitive operations
15085       --  from the private view to the full view.
15086
15087       if Is_Tagged_Type (Full_T)
15088         and then not Is_Concurrent_Type (Full_T)
15089       then
15090          declare
15091             Priv_List : Elist_Id;
15092             Full_List : constant Elist_Id := Primitive_Operations (Full_T);
15093             P1, P2    : Elmt_Id;
15094             Prim      : Entity_Id;
15095             D_Type    : Entity_Id;
15096
15097          begin
15098             if Is_Tagged_Type (Priv_T) then
15099                Priv_List := Primitive_Operations (Priv_T);
15100
15101                P1 := First_Elmt (Priv_List);
15102                while Present (P1) loop
15103                   Prim := Node (P1);
15104
15105                   --  Transfer explicit primitives, not those inherited from
15106                   --  parent of partial view, which will be re-inherited on
15107                   --  the full view.
15108
15109                   if Comes_From_Source (Prim) then
15110                      P2 := First_Elmt (Full_List);
15111                      while Present (P2) and then Node (P2) /= Prim loop
15112                         Next_Elmt (P2);
15113                      end loop;
15114
15115                      --  If not found, that is a new one
15116
15117                      if No (P2) then
15118                         Append_Elmt (Prim, Full_List);
15119                      end if;
15120                   end if;
15121
15122                   Next_Elmt (P1);
15123                end loop;
15124
15125             else
15126                --  In this case the partial view is untagged, so here we locate
15127                --  all of the earlier primitives that need to be treated as
15128                --  dispatching (those that appear between the two views). Note
15129                --  that these additional operations must all be new operations
15130                --  (any earlier operations that override inherited operations
15131                --  of the full view will already have been inserted in the
15132                --  primitives list, marked by Check_Operation_From_Private_View
15133                --  as dispatching. Note that implicit "/=" operators are
15134                --  excluded from being added to the primitives list since they
15135                --  shouldn't be treated as dispatching (tagged "/=" is handled
15136                --  specially).
15137
15138                Prim := Next_Entity (Full_T);
15139                while Present (Prim) and then Prim /= Priv_T loop
15140                   if Ekind (Prim) = E_Procedure
15141                        or else
15142                      Ekind (Prim) = E_Function
15143                   then
15144
15145                      D_Type := Find_Dispatching_Type (Prim);
15146
15147                      if D_Type = Full_T
15148                        and then (Chars (Prim) /= Name_Op_Ne
15149                                   or else Comes_From_Source (Prim))
15150                      then
15151                         Check_Controlling_Formals (Full_T, Prim);
15152
15153                         if not Is_Dispatching_Operation (Prim) then
15154                            Append_Elmt (Prim, Full_List);
15155                            Set_Is_Dispatching_Operation (Prim, True);
15156                            Set_DT_Position (Prim, No_Uint);
15157                         end if;
15158
15159                      elsif Is_Dispatching_Operation (Prim)
15160                        and then D_Type  /= Full_T
15161                      then
15162
15163                         --  Verify that it is not otherwise controlled by a
15164                         --  formal or a return value of type T.
15165
15166                         Check_Controlling_Formals (D_Type, Prim);
15167                      end if;
15168                   end if;
15169
15170                   Next_Entity (Prim);
15171                end loop;
15172             end if;
15173
15174             --  For the tagged case, the two views can share the same
15175             --  Primitive Operation list and the same class wide type.
15176             --  Update attributes of the class-wide type which depend on
15177             --  the full declaration.
15178
15179             if Is_Tagged_Type (Priv_T) then
15180                Set_Primitive_Operations (Priv_T, Full_List);
15181                Set_Class_Wide_Type
15182                  (Base_Type (Full_T), Class_Wide_Type (Priv_T));
15183
15184                Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
15185             end if;
15186          end;
15187       end if;
15188
15189       --  Ada 2005 AI 161: Check preelaboratable initialization consistency
15190
15191       if Known_To_Have_Preelab_Init (Priv_T) then
15192
15193          --  Case where there is a pragma Preelaborable_Initialization. We
15194          --  always allow this in predefined units, which is a bit of a kludge,
15195          --  but it means we don't have to struggle to meet the requirements in
15196          --  the RM for having Preelaborable Initialization. Otherwise we
15197          --  require that the type meets the RM rules. But we can't check that
15198          --  yet, because of the rule about overriding Ininitialize, so we
15199          --  simply set a flag that will be checked at freeze time.
15200
15201          if not In_Predefined_Unit (Full_T) then
15202             Set_Must_Have_Preelab_Init (Full_T);
15203          end if;
15204       end if;
15205
15206       --  If pragma CPP_Class was applied to the private type declaration,
15207       --  propagate it now to the full type declaration.
15208
15209       if Is_CPP_Class (Priv_T) then
15210          Set_Is_CPP_Class (Full_T);
15211          Set_Convention   (Full_T, Convention_CPP);
15212       end if;
15213    end Process_Full_View;
15214
15215    -----------------------------------
15216    -- Process_Incomplete_Dependents --
15217    -----------------------------------
15218
15219    procedure Process_Incomplete_Dependents
15220      (N      : Node_Id;
15221       Full_T : Entity_Id;
15222       Inc_T  : Entity_Id)
15223    is
15224       Inc_Elmt : Elmt_Id;
15225       Priv_Dep : Entity_Id;
15226       New_Subt : Entity_Id;
15227
15228       Disc_Constraint : Elist_Id;
15229
15230    begin
15231       if No (Private_Dependents (Inc_T)) then
15232          return;
15233       end if;
15234
15235       --  Itypes that may be generated by the completion of an incomplete
15236       --  subtype are not used by the back-end and not attached to the tree.
15237       --  They are created only for constraint-checking purposes.
15238
15239       Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
15240       while Present (Inc_Elmt) loop
15241          Priv_Dep := Node (Inc_Elmt);
15242
15243          if Ekind (Priv_Dep) = E_Subprogram_Type then
15244
15245             --  An Access_To_Subprogram type may have a return type or a
15246             --  parameter type that is incomplete. Replace with the full view.
15247
15248             if Etype (Priv_Dep) = Inc_T then
15249                Set_Etype (Priv_Dep, Full_T);
15250             end if;
15251
15252             declare
15253                Formal : Entity_Id;
15254
15255             begin
15256                Formal := First_Formal (Priv_Dep);
15257                while Present (Formal) loop
15258                   if Etype (Formal) = Inc_T then
15259                      Set_Etype (Formal, Full_T);
15260                   end if;
15261
15262                   Next_Formal (Formal);
15263                end loop;
15264             end;
15265
15266          elsif Is_Overloadable (Priv_Dep) then
15267
15268             --  A protected operation is never dispatching: only its
15269             --  wrapper operation (which has convention Ada) is.
15270
15271             if Is_Tagged_Type (Full_T)
15272               and then Convention (Priv_Dep) /= Convention_Protected
15273             then
15274
15275                --  Subprogram has an access parameter whose designated type
15276                --  was incomplete. Reexamine declaration now, because it may
15277                --  be a primitive operation of the full type.
15278
15279                Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
15280                Set_Is_Dispatching_Operation (Priv_Dep);
15281                Check_Controlling_Formals (Full_T, Priv_Dep);
15282             end if;
15283
15284          elsif Ekind (Priv_Dep) = E_Subprogram_Body then
15285
15286             --  Can happen during processing of a body before the completion
15287             --  of a TA type. Ignore, because spec is also on dependent list.
15288
15289             return;
15290
15291          --  Ada 2005 (AI-412): Transform a regular incomplete subtype into a
15292          --  corresponding subtype of the full view.
15293
15294          elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
15295             Set_Subtype_Indication
15296               (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
15297             Set_Etype (Priv_Dep, Full_T);
15298             Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
15299             Set_Analyzed (Parent (Priv_Dep), False);
15300
15301             --  Reanalyze the declaration, suppressing the call to
15302             --  Enter_Name to avoid duplicate names.
15303
15304             Analyze_Subtype_Declaration
15305               (N    => Parent (Priv_Dep),
15306                Skip => True);
15307
15308          --  Dependent is a subtype
15309
15310          else
15311             --  We build a new subtype indication using the full view of the
15312             --  incomplete parent. The discriminant constraints have been
15313             --  elaborated already at the point of the subtype declaration.
15314
15315             New_Subt := Create_Itype (E_Void, N);
15316
15317             if Has_Discriminants (Full_T) then
15318                Disc_Constraint := Discriminant_Constraint (Priv_Dep);
15319             else
15320                Disc_Constraint := No_Elist;
15321             end if;
15322
15323             Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
15324             Set_Full_View (Priv_Dep, New_Subt);
15325          end if;
15326
15327          Next_Elmt (Inc_Elmt);
15328       end loop;
15329    end Process_Incomplete_Dependents;
15330
15331    --------------------------------
15332    -- Process_Range_Expr_In_Decl --
15333    --------------------------------
15334
15335    procedure Process_Range_Expr_In_Decl
15336      (R           : Node_Id;
15337       T           : Entity_Id;
15338       Check_List  : List_Id := Empty_List;
15339       R_Check_Off : Boolean := False)
15340    is
15341       Lo, Hi    : Node_Id;
15342       R_Checks  : Check_Result;
15343       Type_Decl : Node_Id;
15344       Def_Id    : Entity_Id;
15345
15346    begin
15347       Analyze_And_Resolve (R, Base_Type (T));
15348
15349       if Nkind (R) = N_Range then
15350          Lo := Low_Bound (R);
15351          Hi := High_Bound (R);
15352
15353          --  We need to ensure validity of the bounds here, because if we
15354          --  go ahead and do the expansion, then the expanded code will get
15355          --  analyzed with range checks suppressed and we miss the check.
15356
15357          Validity_Check_Range (R);
15358
15359          --  If there were errors in the declaration, try and patch up some
15360          --  common mistakes in the bounds. The cases handled are literals
15361          --  which are Integer where the expected type is Real and vice versa.
15362          --  These corrections allow the compilation process to proceed further
15363          --  along since some basic assumptions of the format of the bounds
15364          --  are guaranteed.
15365
15366          if Etype (R) = Any_Type then
15367
15368             if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
15369                Rewrite (Lo,
15370                  Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
15371
15372             elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
15373                Rewrite (Hi,
15374                  Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
15375
15376             elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
15377                Rewrite (Lo,
15378                  Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
15379
15380             elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
15381                Rewrite (Hi,
15382                  Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
15383             end if;
15384
15385             Set_Etype (Lo, T);
15386             Set_Etype (Hi, T);
15387          end if;
15388
15389          --  If the bounds of the range have been mistakenly given as string
15390          --  literals (perhaps in place of character literals), then an error
15391          --  has already been reported, but we rewrite the string literal as a
15392          --  bound of the range's type to avoid blowups in later processing
15393          --  that looks at static values.
15394
15395          if Nkind (Lo) = N_String_Literal then
15396             Rewrite (Lo,
15397               Make_Attribute_Reference (Sloc (Lo),
15398                 Attribute_Name => Name_First,
15399                 Prefix => New_Reference_To (T, Sloc (Lo))));
15400             Analyze_And_Resolve (Lo);
15401          end if;
15402
15403          if Nkind (Hi) = N_String_Literal then
15404             Rewrite (Hi,
15405               Make_Attribute_Reference (Sloc (Hi),
15406                 Attribute_Name => Name_First,
15407                 Prefix => New_Reference_To (T, Sloc (Hi))));
15408             Analyze_And_Resolve (Hi);
15409          end if;
15410
15411          --  If bounds aren't scalar at this point then exit, avoiding
15412          --  problems with further processing of the range in this procedure.
15413
15414          if not Is_Scalar_Type (Etype (Lo)) then
15415             return;
15416          end if;
15417
15418          --  Resolve (actually Sem_Eval) has checked that the bounds are in
15419          --  then range of the base type. Here we check whether the bounds
15420          --  are in the range of the subtype itself. Note that if the bounds
15421          --  represent the null range the Constraint_Error exception should
15422          --  not be raised.
15423
15424          --  ??? The following code should be cleaned up as follows
15425
15426          --  1. The Is_Null_Range (Lo, Hi) test should disappear since it
15427          --     is done in the call to Range_Check (R, T); below
15428
15429          --  2. The use of R_Check_Off should be investigated and possibly
15430          --     removed, this would clean up things a bit.
15431
15432          if Is_Null_Range (Lo, Hi) then
15433             null;
15434
15435          else
15436             --  Capture values of bounds and generate temporaries for them
15437             --  if needed, before applying checks, since checks may cause
15438             --  duplication of the expression without forcing evaluation.
15439
15440             if Expander_Active then
15441                Force_Evaluation (Lo);
15442                Force_Evaluation (Hi);
15443             end if;
15444
15445             --  We use a flag here instead of suppressing checks on the
15446             --  type because the type we check against isn't necessarily
15447             --  the place where we put the check.
15448
15449             if not R_Check_Off then
15450                R_Checks := Get_Range_Checks (R, T);
15451
15452                --  Look up tree to find an appropriate insertion point.
15453                --  This seems really junk code, and very brittle, couldn't
15454                --  we just use an insert actions call of some kind ???
15455
15456                Type_Decl := Parent (R);
15457                while Present (Type_Decl) and then not
15458                  (Nkind (Type_Decl) = N_Full_Type_Declaration
15459                     or else
15460                   Nkind (Type_Decl) = N_Subtype_Declaration
15461                     or else
15462                   Nkind (Type_Decl) = N_Loop_Statement
15463                     or else
15464                   Nkind (Type_Decl) = N_Task_Type_Declaration
15465                     or else
15466                   Nkind (Type_Decl) = N_Single_Task_Declaration
15467                     or else
15468                   Nkind (Type_Decl) = N_Protected_Type_Declaration
15469                     or else
15470                   Nkind (Type_Decl) = N_Single_Protected_Declaration)
15471                loop
15472                   Type_Decl := Parent (Type_Decl);
15473                end loop;
15474
15475                --  Why would Type_Decl not be present???  Without this test,
15476                --  short regression tests fail.
15477
15478                if Present (Type_Decl) then
15479
15480                   --  Case of loop statement (more comments ???)
15481
15482                   if Nkind (Type_Decl) = N_Loop_Statement then
15483                      declare
15484                         Indic : Node_Id;
15485
15486                      begin
15487                         Indic := Parent (R);
15488                         while Present (Indic) and then not
15489                           (Nkind (Indic) = N_Subtype_Indication)
15490                         loop
15491                            Indic := Parent (Indic);
15492                         end loop;
15493
15494                         if Present (Indic) then
15495                            Def_Id := Etype (Subtype_Mark (Indic));
15496
15497                            Insert_Range_Checks
15498                              (R_Checks,
15499                               Type_Decl,
15500                               Def_Id,
15501                               Sloc (Type_Decl),
15502                               R,
15503                               Do_Before => True);
15504                         end if;
15505                      end;
15506
15507                   --  All other cases (more comments ???)
15508
15509                   else
15510                      Def_Id := Defining_Identifier (Type_Decl);
15511
15512                      if (Ekind (Def_Id) = E_Record_Type
15513                           and then Depends_On_Discriminant (R))
15514                        or else
15515                         (Ekind (Def_Id) = E_Protected_Type
15516                           and then Has_Discriminants (Def_Id))
15517                      then
15518                         Append_Range_Checks
15519                           (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
15520
15521                      else
15522                         Insert_Range_Checks
15523                           (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
15524
15525                      end if;
15526                   end if;
15527                end if;
15528             end if;
15529          end if;
15530
15531       elsif Expander_Active then
15532          Get_Index_Bounds (R, Lo, Hi);
15533          Force_Evaluation (Lo);
15534          Force_Evaluation (Hi);
15535       end if;
15536    end Process_Range_Expr_In_Decl;
15537
15538    --------------------------------------
15539    -- Process_Real_Range_Specification --
15540    --------------------------------------
15541
15542    procedure Process_Real_Range_Specification (Def : Node_Id) is
15543       Spec : constant Node_Id := Real_Range_Specification (Def);
15544       Lo   : Node_Id;
15545       Hi   : Node_Id;
15546       Err  : Boolean := False;
15547
15548       procedure Analyze_Bound (N : Node_Id);
15549       --  Analyze and check one bound
15550
15551       -------------------
15552       -- Analyze_Bound --
15553       -------------------
15554
15555       procedure Analyze_Bound (N : Node_Id) is
15556       begin
15557          Analyze_And_Resolve (N, Any_Real);
15558
15559          if not Is_OK_Static_Expression (N) then
15560             Flag_Non_Static_Expr
15561               ("bound in real type definition is not static!", N);
15562             Err := True;
15563          end if;
15564       end Analyze_Bound;
15565
15566    --  Start of processing for Process_Real_Range_Specification
15567
15568    begin
15569       if Present (Spec) then
15570          Lo := Low_Bound (Spec);
15571          Hi := High_Bound (Spec);
15572          Analyze_Bound (Lo);
15573          Analyze_Bound (Hi);
15574
15575          --  If error, clear away junk range specification
15576
15577          if Err then
15578             Set_Real_Range_Specification (Def, Empty);
15579          end if;
15580       end if;
15581    end Process_Real_Range_Specification;
15582
15583    ---------------------
15584    -- Process_Subtype --
15585    ---------------------
15586
15587    function Process_Subtype
15588      (S           : Node_Id;
15589       Related_Nod : Node_Id;
15590       Related_Id  : Entity_Id := Empty;
15591       Suffix      : Character := ' ') return Entity_Id
15592    is
15593       P               : Node_Id;
15594       Def_Id          : Entity_Id;
15595       Error_Node      : Node_Id;
15596       Full_View_Id    : Entity_Id;
15597       Subtype_Mark_Id : Entity_Id;
15598
15599       May_Have_Null_Exclusion : Boolean;
15600
15601       procedure Check_Incomplete (T : Entity_Id);
15602       --  Called to verify that an incomplete type is not used prematurely
15603
15604       ----------------------
15605       -- Check_Incomplete --
15606       ----------------------
15607
15608       procedure Check_Incomplete (T : Entity_Id) is
15609       begin
15610          --  Ada 2005 (AI-412): Incomplete subtypes are legal
15611
15612          if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
15613            and then
15614              not (Ada_Version >= Ada_05
15615                     and then
15616                        (Nkind (Parent (T)) = N_Subtype_Declaration
15617                           or else
15618                             (Nkind (Parent (T)) = N_Subtype_Indication
15619                                and then Nkind (Parent (Parent (T))) =
15620                                           N_Subtype_Declaration)))
15621          then
15622             Error_Msg_N ("invalid use of type before its full declaration", T);
15623          end if;
15624       end Check_Incomplete;
15625
15626    --  Start of processing for Process_Subtype
15627
15628    begin
15629       --  Case of no constraints present
15630
15631       if Nkind (S) /= N_Subtype_Indication then
15632
15633          Find_Type (S);
15634          Check_Incomplete (S);
15635          P := Parent (S);
15636
15637          --  Ada 2005 (AI-231): Static check
15638
15639          if Ada_Version >= Ada_05
15640            and then Present (P)
15641            and then Null_Exclusion_Present (P)
15642            and then Nkind (P) /= N_Access_To_Object_Definition
15643            and then not Is_Access_Type (Entity (S))
15644          then
15645             Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
15646          end if;
15647
15648          May_Have_Null_Exclusion :=
15649            Nkind (P) = N_Access_Definition
15650            or else Nkind (P) = N_Access_Function_Definition
15651            or else Nkind (P) = N_Access_Procedure_Definition
15652            or else Nkind (P) = N_Access_To_Object_Definition
15653            or else Nkind (P) = N_Allocator
15654            or else Nkind (P) = N_Component_Definition
15655            or else Nkind (P) = N_Derived_Type_Definition
15656            or else Nkind (P) = N_Discriminant_Specification
15657            or else Nkind (P) = N_Object_Declaration
15658            or else Nkind (P) = N_Parameter_Specification
15659            or else Nkind (P) = N_Subtype_Declaration;
15660
15661          --  Create an Itype that is a duplicate of Entity (S) but with the
15662          --  null-exclusion attribute
15663
15664          if May_Have_Null_Exclusion
15665            and then Is_Access_Type (Entity (S))
15666            and then Null_Exclusion_Present (P)
15667
15668             --  No need to check the case of an access to object definition.
15669             --  It is correct to define double not-null pointers.
15670
15671             --  Example:
15672             --     type Not_Null_Int_Ptr is not null access Integer;
15673             --     type Acc is not null access Not_Null_Int_Ptr;
15674
15675            and then Nkind (P) /= N_Access_To_Object_Definition
15676          then
15677             if Can_Never_Be_Null (Entity (S)) then
15678                case Nkind (Related_Nod) is
15679                   when N_Full_Type_Declaration =>
15680                      if Nkind (Type_Definition (Related_Nod))
15681                        in N_Array_Type_Definition
15682                      then
15683                         Error_Node :=
15684                           Subtype_Indication
15685                             (Component_Definition
15686                              (Type_Definition (Related_Nod)));
15687                      else
15688                         Error_Node :=
15689                           Subtype_Indication (Type_Definition (Related_Nod));
15690                      end if;
15691
15692                   when N_Subtype_Declaration =>
15693                      Error_Node := Subtype_Indication (Related_Nod);
15694
15695                   when N_Object_Declaration =>
15696                      Error_Node := Object_Definition (Related_Nod);
15697
15698                   when N_Component_Declaration =>
15699                      Error_Node :=
15700                        Subtype_Indication (Component_Definition (Related_Nod));
15701
15702                   when others =>
15703                      pragma Assert (False);
15704                      Error_Node := Related_Nod;
15705                end case;
15706
15707                Error_Msg_NE
15708                  ("`NOT NULL` not allowed (& already excludes null)",
15709                   Error_Node,
15710                   Entity (S));
15711             end if;
15712
15713             Set_Etype  (S,
15714               Create_Null_Excluding_Itype
15715                 (T           => Entity (S),
15716                  Related_Nod => P));
15717             Set_Entity (S, Etype (S));
15718          end if;
15719
15720          return Entity (S);
15721
15722       --  Case of constraint present, so that we have an N_Subtype_Indication
15723       --  node (this node is created only if constraints are present).
15724
15725       else
15726          Find_Type (Subtype_Mark (S));
15727
15728          if Nkind (Parent (S)) /= N_Access_To_Object_Definition
15729            and then not
15730             (Nkind (Parent (S)) = N_Subtype_Declaration
15731               and then Is_Itype (Defining_Identifier (Parent (S))))
15732          then
15733             Check_Incomplete (Subtype_Mark (S));
15734          end if;
15735
15736          P := Parent (S);
15737          Subtype_Mark_Id := Entity (Subtype_Mark (S));
15738
15739          --  Explicit subtype declaration case
15740
15741          if Nkind (P) = N_Subtype_Declaration then
15742             Def_Id := Defining_Identifier (P);
15743
15744          --  Explicit derived type definition case
15745
15746          elsif Nkind (P) = N_Derived_Type_Definition then
15747             Def_Id := Defining_Identifier (Parent (P));
15748
15749          --  Implicit case, the Def_Id must be created as an implicit type.
15750          --  The one exception arises in the case of concurrent types, array
15751          --  and access types, where other subsidiary implicit types may be
15752          --  created and must appear before the main implicit type. In these
15753          --  cases we leave Def_Id set to Empty as a signal that Create_Itype
15754          --  has not yet been called to create Def_Id.
15755
15756          else
15757             if Is_Array_Type (Subtype_Mark_Id)
15758               or else Is_Concurrent_Type (Subtype_Mark_Id)
15759               or else Is_Access_Type (Subtype_Mark_Id)
15760             then
15761                Def_Id := Empty;
15762
15763             --  For the other cases, we create a new unattached Itype,
15764             --  and set the indication to ensure it gets attached later.
15765
15766             else
15767                Def_Id :=
15768                  Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
15769             end if;
15770          end if;
15771
15772          --  If the kind of constraint is invalid for this kind of type,
15773          --  then give an error, and then pretend no constraint was given.
15774
15775          if not Is_Valid_Constraint_Kind
15776                    (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
15777          then
15778             Error_Msg_N
15779               ("incorrect constraint for this kind of type", Constraint (S));
15780
15781             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
15782
15783             --  Set Ekind of orphan itype, to prevent cascaded errors
15784
15785             if Present (Def_Id) then
15786                Set_Ekind (Def_Id, Ekind (Any_Type));
15787             end if;
15788
15789             --  Make recursive call, having got rid of the bogus constraint
15790
15791             return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
15792          end if;
15793
15794          --  Remaining processing depends on type
15795
15796          case Ekind (Subtype_Mark_Id) is
15797             when Access_Kind =>
15798                Constrain_Access (Def_Id, S, Related_Nod);
15799
15800                if Expander_Active
15801                  and then  Is_Itype (Designated_Type (Def_Id))
15802                  and then Nkind (Related_Nod) = N_Subtype_Declaration
15803                  and then not Is_Incomplete_Type (Designated_Type (Def_Id))
15804                then
15805                   Build_Itype_Reference
15806                     (Designated_Type (Def_Id), Related_Nod);
15807                end if;
15808
15809             when Array_Kind =>
15810                Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
15811
15812             when Decimal_Fixed_Point_Kind =>
15813                Constrain_Decimal (Def_Id, S);
15814
15815             when Enumeration_Kind =>
15816                Constrain_Enumeration (Def_Id, S);
15817
15818             when Ordinary_Fixed_Point_Kind =>
15819                Constrain_Ordinary_Fixed (Def_Id, S);
15820
15821             when Float_Kind =>
15822                Constrain_Float (Def_Id, S);
15823
15824             when Integer_Kind =>
15825                Constrain_Integer (Def_Id, S);
15826
15827             when E_Record_Type     |
15828                  E_Record_Subtype  |
15829                  Class_Wide_Kind   |
15830                  E_Incomplete_Type =>
15831                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
15832
15833             when Private_Kind =>
15834                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
15835                Set_Private_Dependents (Def_Id, New_Elmt_List);
15836
15837                --  In case of an invalid constraint prevent further processing
15838                --  since the type constructed is missing expected fields.
15839
15840                if Etype (Def_Id) = Any_Type then
15841                   return Def_Id;
15842                end if;
15843
15844                --  If the full view is that of a task with discriminants,
15845                --  we must constrain both the concurrent type and its
15846                --  corresponding record type. Otherwise we will just propagate
15847                --  the constraint to the full view, if available.
15848
15849                if Present (Full_View (Subtype_Mark_Id))
15850                  and then Has_Discriminants (Subtype_Mark_Id)
15851                  and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
15852                then
15853                   Full_View_Id :=
15854                     Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
15855
15856                   Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
15857                   Constrain_Concurrent (Full_View_Id, S,
15858                     Related_Nod, Related_Id, Suffix);
15859                   Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
15860                   Set_Full_View (Def_Id, Full_View_Id);
15861
15862                   --  Introduce an explicit reference to the private subtype,
15863                   --  to prevent scope anomalies in gigi if first use appears
15864                   --  in a nested context, e.g. a later function body.
15865                   --  Should this be generated in other contexts than a full
15866                   --  type declaration?
15867
15868                   if Is_Itype (Def_Id)
15869                     and then
15870                       Nkind (Parent (P)) = N_Full_Type_Declaration
15871                   then
15872                      Build_Itype_Reference (Def_Id, Parent (P));
15873                   end if;
15874
15875                else
15876                   Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
15877                end if;
15878
15879             when Concurrent_Kind  =>
15880                Constrain_Concurrent (Def_Id, S,
15881                  Related_Nod, Related_Id, Suffix);
15882
15883             when others =>
15884                Error_Msg_N ("invalid subtype mark in subtype indication", S);
15885          end case;
15886
15887          --  Size and Convention are always inherited from the base type
15888
15889          Set_Size_Info  (Def_Id,            (Subtype_Mark_Id));
15890          Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
15891
15892          return Def_Id;
15893       end if;
15894    end Process_Subtype;
15895
15896    ---------------------------------------
15897    -- Check_Anonymous_Access_Components --
15898    ---------------------------------------
15899
15900    procedure Check_Anonymous_Access_Components
15901       (Typ_Decl  : Node_Id;
15902        Typ       : Entity_Id;
15903        Prev      : Entity_Id;
15904        Comp_List : Node_Id)
15905    is
15906       Loc         : constant Source_Ptr := Sloc (Typ_Decl);
15907       Anon_Access : Entity_Id;
15908       Acc_Def     : Node_Id;
15909       Comp        : Node_Id;
15910       Comp_Def    : Node_Id;
15911       Decl        : Node_Id;
15912       Type_Def    : Node_Id;
15913
15914       procedure Build_Incomplete_Type_Declaration;
15915       --  If the record type contains components that include an access to the
15916       --  current record, then create an incomplete type declaration for the
15917       --  record, to be used as the designated type of the anonymous access.
15918       --  This is done only once, and only if there is no previous partial
15919       --  view of the type.
15920
15921       function Mentions_T (Acc_Def : Node_Id) return Boolean;
15922       --  Check whether an access definition includes a reference to
15923       --  the enclosing record type. The reference can be a subtype
15924       --  mark in the access definition itself, or a 'Class attribute
15925       --  reference, or recursively a reference appearing in a parameter
15926       --  type in an access_to_subprogram definition.
15927
15928       --------------------------------------
15929       -- Build_Incomplete_Type_Declaration --
15930       --------------------------------------
15931
15932       procedure Build_Incomplete_Type_Declaration is
15933          Decl  : Node_Id;
15934          Inc_T : Entity_Id;
15935          H     : Entity_Id;
15936
15937       begin
15938          --  If there is a previous partial view, no need to create a new one
15939          --  If the partial view, given by Prev, is incomplete,  If Prev is
15940          --  a private declaration, full declaration is flagged accordingly.
15941
15942          if Prev /= Typ then
15943             if Tagged_Present (Type_Definition (Typ_Decl)) then
15944                Make_Class_Wide_Type (Prev);
15945                Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
15946                Set_Etype (Class_Wide_Type (Typ), Typ);
15947             end if;
15948
15949             return;
15950
15951          elsif Has_Private_Declaration (Typ) then
15952             return;
15953
15954          --  If there was a previous anonymous access type, the incomplete
15955          --  type declaration will have been created already.
15956
15957          elsif Present (Current_Entity (Typ))
15958            and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
15959            and then Full_View (Current_Entity (Typ)) = Typ
15960          then
15961             return;
15962
15963          else
15964             Inc_T  := Make_Defining_Identifier (Loc, Chars (Typ));
15965             Decl   := Make_Incomplete_Type_Declaration (Loc, Inc_T);
15966
15967             --  Type has already been inserted into the current scope.
15968             --  Remove it, and add incomplete declaration for type, so
15969             --  that subsequent anonymous access types can use it.
15970             --  The entity is unchained from the homonym list and from
15971             --  immediate visibility. After analysis, the entity in the
15972             --  incomplete declaration becomes immediately visible in the
15973             --  record declaration that follows.
15974
15975             H := Current_Entity (Typ);
15976
15977             if H = Typ then
15978                Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
15979             else
15980                while Present (H)
15981                  and then Homonym (H) /= Typ
15982                loop
15983                   H := Homonym (Typ);
15984                end loop;
15985
15986                Set_Homonym (H, Homonym (Typ));
15987             end if;
15988
15989             Insert_Before (Typ_Decl, Decl);
15990             Analyze (Decl);
15991             Set_Full_View (Inc_T, Typ);
15992
15993             if (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
15994                  and then
15995                    Present
15996                      (Record_Extension_Part (Type_Definition (Typ_Decl))))
15997               or else Tagged_Present (Type_Definition (Typ_Decl))
15998             then
15999                --  Create a common class-wide type for both views, and set
16000                --  the etype of the class-wide type to the full view.
16001
16002                Make_Class_Wide_Type (Inc_T);
16003                Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
16004                Set_Etype (Class_Wide_Type (Typ), Typ);
16005             end if;
16006          end if;
16007       end Build_Incomplete_Type_Declaration;
16008
16009       ----------------
16010       -- Mentions_T --
16011       ----------------
16012
16013       function Mentions_T (Acc_Def : Node_Id) return Boolean is
16014          Subt : Node_Id;
16015          Type_Id : constant Name_Id := Chars (Typ);
16016
16017          function Names_T (Nam : Node_Id) return Boolean;
16018          --  The record type has not been introduced in the current scope
16019          --  yet, so we must examine the name of the type itself, either
16020          --  an identifier T, or an expanded name of the form P.T, where
16021          --  P denotes the current scope.
16022
16023          -------------
16024          -- Names_T --
16025          -------------
16026
16027          function Names_T (Nam : Node_Id) return Boolean is
16028          begin
16029             if Nkind (Nam) = N_Identifier then
16030                return Chars (Nam) = Type_Id;
16031
16032             elsif Nkind (Nam) = N_Selected_Component then
16033                if Chars (Selector_Name (Nam)) = Type_Id then
16034                   if Nkind (Prefix (Nam)) = N_Identifier then
16035                      return Chars (Prefix (Nam)) = Chars (Current_Scope);
16036
16037                   elsif Nkind (Prefix (Nam)) = N_Selected_Component then
16038                      return Chars (Selector_Name (Prefix (Nam))) =
16039                             Chars (Current_Scope);
16040                   else
16041                      return False;
16042                   end if;
16043                else
16044                   return False;
16045                end if;
16046             else
16047                return False;
16048             end if;
16049          end Names_T;
16050
16051       --  Start of processing for Mentions_T
16052
16053       begin
16054          if No (Access_To_Subprogram_Definition (Acc_Def)) then
16055             Subt := Subtype_Mark (Acc_Def);
16056
16057             if Nkind (Subt) = N_Identifier then
16058                return Chars (Subt) = Type_Id;
16059
16060             --  Reference can be through an expanded name which has not been
16061             --  analyzed yet, and which designates enclosing scopes.
16062
16063             elsif Nkind (Subt) = N_Selected_Component then
16064                if Names_T (Subt) then
16065                   return True;
16066
16067                --  Otherwise it must denote an entity that is already visible.
16068                --  The access definition may name a subtype of the enclosing
16069                --  type, if there is a previous incomplete declaration for it.
16070
16071                else
16072                   Find_Selected_Component (Subt);
16073                   return
16074                     Is_Entity_Name (Subt)
16075                       and then Scope (Entity (Subt)) = Current_Scope
16076                       and then (Chars (Base_Type (Entity (Subt))) = Type_Id
16077                         or else
16078                           (Is_Class_Wide_Type (Entity (Subt))
16079                             and then
16080                               Chars (Etype (Base_Type (Entity (Subt))))
16081                                 = Type_Id));
16082                end if;
16083
16084             --  A reference to the current type may appear as the prefix of
16085             --  a 'Class attribute.
16086
16087             elsif Nkind (Subt) = N_Attribute_Reference
16088               and then Attribute_Name (Subt) = Name_Class
16089             then
16090                return Names_T (Prefix (Subt));
16091             else
16092                return False;
16093             end if;
16094
16095          else
16096             --  Component is an access_to_subprogram: examine its formals
16097
16098             declare
16099                Param_Spec : Node_Id;
16100
16101             begin
16102                Param_Spec :=
16103                  First
16104                    (Parameter_Specifications
16105                      (Access_To_Subprogram_Definition (Acc_Def)));
16106                while Present (Param_Spec) loop
16107                   if Nkind (Parameter_Type (Param_Spec))
16108                        = N_Access_Definition
16109                     and then Mentions_T (Parameter_Type (Param_Spec))
16110                   then
16111                      return True;
16112                   end if;
16113
16114                   Next (Param_Spec);
16115                end loop;
16116
16117                return False;
16118             end;
16119          end if;
16120       end Mentions_T;
16121
16122    --  Start of processing for Check_Anonymous_Access_Components
16123
16124    begin
16125       if No (Comp_List) then
16126          return;
16127       end if;
16128
16129       Comp := First (Component_Items (Comp_List));
16130       while Present (Comp) loop
16131          if Nkind (Comp) = N_Component_Declaration
16132            and then Present
16133              (Access_Definition (Component_Definition (Comp)))
16134            and then
16135              Mentions_T (Access_Definition (Component_Definition (Comp)))
16136          then
16137             Comp_Def := Component_Definition (Comp);
16138             Acc_Def :=
16139               Access_To_Subprogram_Definition
16140                 (Access_Definition (Comp_Def));
16141
16142             Build_Incomplete_Type_Declaration;
16143             Anon_Access :=
16144               Make_Defining_Identifier (Loc,
16145                 Chars => New_Internal_Name ('S'));
16146
16147             --  Create a declaration for the anonymous access type: either
16148             --  an access_to_object or an access_to_subprogram.
16149
16150             if Present (Acc_Def) then
16151                if Nkind  (Acc_Def) = N_Access_Function_Definition then
16152                   Type_Def :=
16153                     Make_Access_Function_Definition (Loc,
16154                       Parameter_Specifications =>
16155                         Parameter_Specifications (Acc_Def),
16156                       Result_Definition => Result_Definition (Acc_Def));
16157                else
16158                   Type_Def :=
16159                     Make_Access_Procedure_Definition (Loc,
16160                       Parameter_Specifications =>
16161                         Parameter_Specifications (Acc_Def));
16162                end if;
16163
16164             else
16165                Type_Def :=
16166                  Make_Access_To_Object_Definition (Loc,
16167                    Subtype_Indication =>
16168                       Relocate_Node
16169                         (Subtype_Mark
16170                           (Access_Definition (Comp_Def))));
16171
16172                Set_Constant_Present
16173                  (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
16174                Set_All_Present
16175                  (Type_Def, All_Present (Access_Definition (Comp_Def)));
16176             end if;
16177
16178             Set_Null_Exclusion_Present
16179               (Type_Def,
16180                Null_Exclusion_Present (Access_Definition (Comp_Def)));
16181
16182             Decl :=
16183               Make_Full_Type_Declaration (Loc,
16184                 Defining_Identifier => Anon_Access,
16185                 Type_Definition     => Type_Def);
16186
16187             Insert_Before (Typ_Decl, Decl);
16188             Analyze (Decl);
16189
16190             --  If an access to object, Preserve entity of designated type,
16191             --  for ASIS use, before rewriting the component definition.
16192
16193             if No (Acc_Def) then
16194                declare
16195                   Desig : Entity_Id;
16196
16197                begin
16198                   Desig := Entity (Subtype_Indication (Type_Def));
16199
16200                   --  If the access definition is to the current  record,
16201                   --  the visible entity at this point is an  incomplete
16202                   --  type. Retrieve the full view to simplify  ASIS queries
16203
16204                   if Ekind (Desig) = E_Incomplete_Type then
16205                      Desig := Full_View (Desig);
16206                   end if;
16207
16208                   Set_Entity
16209                     (Subtype_Mark (Access_Definition  (Comp_Def)), Desig);
16210                end;
16211             end if;
16212
16213             Rewrite (Comp_Def,
16214               Make_Component_Definition (Loc,
16215                 Subtype_Indication =>
16216                New_Occurrence_Of (Anon_Access, Loc)));
16217             Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
16218             Set_Is_Local_Anonymous_Access (Anon_Access);
16219          end if;
16220
16221          Next (Comp);
16222       end loop;
16223
16224       if Present (Variant_Part (Comp_List)) then
16225          declare
16226             V : Node_Id;
16227          begin
16228             V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
16229             while Present (V) loop
16230                Check_Anonymous_Access_Components
16231                  (Typ_Decl, Typ, Prev, Component_List (V));
16232                Next_Non_Pragma (V);
16233             end loop;
16234          end;
16235       end if;
16236    end Check_Anonymous_Access_Components;
16237
16238    -----------------------------
16239    -- Record_Type_Declaration --
16240    -----------------------------
16241
16242    procedure Record_Type_Declaration
16243      (T    : Entity_Id;
16244       N    : Node_Id;
16245       Prev : Entity_Id)
16246    is
16247       Def       : constant Node_Id := Type_Definition (N);
16248       Is_Tagged : Boolean;
16249       Tag_Comp  : Entity_Id;
16250
16251    begin
16252       --  These flags must be initialized before calling Process_Discriminants
16253       --  because this routine makes use of them.
16254
16255       Set_Ekind               (T, E_Record_Type);
16256       Set_Etype               (T, T);
16257       Init_Size_Align         (T);
16258       Set_Abstract_Interfaces (T, No_Elist);
16259       Set_Stored_Constraint   (T, No_Elist);
16260
16261       --  Normal case
16262
16263       if Ada_Version < Ada_05
16264         or else not Interface_Present (Def)
16265       then
16266          --  The flag Is_Tagged_Type might have already been set by
16267          --  Find_Type_Name if it detected an error for declaration T. This
16268          --  arises in the case of private tagged types where the full view
16269          --  omits the word tagged.
16270
16271          Is_Tagged :=
16272            Tagged_Present (Def)
16273              or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
16274
16275          Set_Is_Tagged_Type      (T, Is_Tagged);
16276          Set_Is_Limited_Record   (T, Limited_Present (Def));
16277
16278          --  Type is abstract if full declaration carries keyword, or if
16279          --  previous partial view did.
16280
16281          Set_Is_Abstract_Type    (T, Is_Abstract_Type (T)
16282                                       or else Abstract_Present (Def));
16283
16284       else
16285          Is_Tagged := True;
16286          Analyze_Interface_Declaration (T, Def);
16287
16288          if Present (Discriminant_Specifications (N)) then
16289             Error_Msg_N
16290               ("interface types cannot have discriminants",
16291                 Defining_Identifier
16292                   (First (Discriminant_Specifications (N))));
16293          end if;
16294       end if;
16295
16296       --  First pass: if there are self-referential access components,
16297       --  create the required anonymous access type declarations, and if
16298       --  need be an incomplete type declaration for T itself.
16299
16300       Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
16301
16302       if Ada_Version >= Ada_05
16303         and then Present (Interface_List (Def))
16304       then
16305          Check_Abstract_Interfaces (N, Def);
16306
16307          declare
16308             Ifaces_List : Elist_Id;
16309
16310          begin
16311             --  Ada 2005 (AI-251): Collect the list of progenitors that are not
16312             --  already in the parents.
16313
16314             Collect_Abstract_Interfaces
16315               (T                         => T,
16316                Ifaces_List               => Ifaces_List,
16317                Exclude_Parent_Interfaces => True);
16318
16319             Set_Abstract_Interfaces (T, Ifaces_List);
16320          end;
16321       end if;
16322
16323       --  Records constitute a scope for the component declarations within.
16324       --  The scope is created prior to the processing of these declarations.
16325       --  Discriminants are processed first, so that they are visible when
16326       --  processing the other components. The Ekind of the record type itself
16327       --  is set to E_Record_Type (subtypes appear as E_Record_Subtype).
16328
16329       --  Enter record scope
16330
16331       Push_Scope (T);
16332
16333       --  If an incomplete or private type declaration was already given for
16334       --  the type, then this scope already exists, and the discriminants have
16335       --  been declared within. We must verify that the full declaration
16336       --  matches the incomplete one.
16337
16338       Check_Or_Process_Discriminants (N, T, Prev);
16339
16340       Set_Is_Constrained     (T, not Has_Discriminants (T));
16341       Set_Has_Delayed_Freeze (T, True);
16342
16343       --  For tagged types add a manually analyzed component corresponding
16344       --  to the component _tag, the corresponding piece of tree will be
16345       --  expanded as part of the freezing actions if it is not a CPP_Class.
16346
16347       if Is_Tagged then
16348
16349          --  Do not add the tag unless we are in expansion mode
16350
16351          if Expander_Active then
16352             Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
16353             Enter_Name (Tag_Comp);
16354
16355             Set_Is_Tag                    (Tag_Comp);
16356             Set_Is_Aliased                (Tag_Comp);
16357             Set_Ekind                     (Tag_Comp, E_Component);
16358             Set_Etype                     (Tag_Comp, RTE (RE_Tag));
16359             Set_DT_Entry_Count            (Tag_Comp, No_Uint);
16360             Set_Original_Record_Component (Tag_Comp, Tag_Comp);
16361             Init_Component_Location       (Tag_Comp);
16362
16363             --  Ada 2005 (AI-251): Addition of the Tag corresponding to all the
16364             --  implemented interfaces.
16365
16366             if Has_Abstract_Interfaces (T) then
16367                Add_Interface_Tag_Components (N, T);
16368             end if;
16369          end if;
16370
16371          Make_Class_Wide_Type (T);
16372          Set_Primitive_Operations (T, New_Elmt_List);
16373       end if;
16374
16375       --  We must suppress range checks when processing the components
16376       --  of a record in the presence of discriminants, since we don't
16377       --  want spurious checks to be generated during their analysis, but
16378       --  must reset the Suppress_Range_Checks flags after having processed
16379       --  the record definition.
16380
16381       --  Note: this is the only use of Kill_Range_Checks, and is a bit odd,
16382       --  couldn't we just use the normal range check suppression method here.
16383       --  That would seem cleaner ???
16384
16385       if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
16386          Set_Kill_Range_Checks (T, True);
16387          Record_Type_Definition (Def, Prev);
16388          Set_Kill_Range_Checks (T, False);
16389       else
16390          Record_Type_Definition (Def, Prev);
16391       end if;
16392
16393       --  Exit from record scope
16394
16395       End_Scope;
16396
16397       --  Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
16398       --  the implemented interfaces and associate them an aliased entity.
16399
16400       if Is_Tagged
16401         and then not Is_Empty_List (Interface_List (Def))
16402       then
16403          declare
16404             Ifaces_List : constant Elist_Id := New_Elmt_List;
16405          begin
16406             Derive_Interface_Subprograms (T, T, Ifaces_List);
16407          end;
16408       end if;
16409    end Record_Type_Declaration;
16410
16411    ----------------------------
16412    -- Record_Type_Definition --
16413    ----------------------------
16414
16415    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
16416       Component          : Entity_Id;
16417       Ctrl_Components    : Boolean := False;
16418       Final_Storage_Only : Boolean;
16419       T                  : Entity_Id;
16420
16421    begin
16422       if Ekind (Prev_T) = E_Incomplete_Type then
16423          T := Full_View (Prev_T);
16424       else
16425          T := Prev_T;
16426       end if;
16427
16428       Final_Storage_Only := not Is_Controlled (T);
16429
16430       --  Ada 2005: check whether an explicit Limited is present in a derived
16431       --  type declaration.
16432
16433       if Nkind (Parent (Def)) = N_Derived_Type_Definition
16434         and then Limited_Present (Parent (Def))
16435       then
16436          Set_Is_Limited_Record (T);
16437       end if;
16438
16439       --  If the component list of a record type is defined by the reserved
16440       --  word null and there is no discriminant part, then the record type has
16441       --  no components and all records of the type are null records (RM 3.7)
16442       --  This procedure is also called to process the extension part of a
16443       --  record extension, in which case the current scope may have inherited
16444       --  components.
16445
16446       if No (Def)
16447         or else No (Component_List (Def))
16448         or else Null_Present (Component_List (Def))
16449       then
16450          null;
16451
16452       else
16453          Analyze_Declarations (Component_Items (Component_List (Def)));
16454
16455          if Present (Variant_Part (Component_List (Def))) then
16456             Analyze (Variant_Part (Component_List (Def)));
16457          end if;
16458       end if;
16459
16460       --  After completing the semantic analysis of the record definition,
16461       --  record components, both new and inherited, are accessible. Set their
16462       --  kind accordingly. Exclude malformed itypes from illegal declarations,
16463       --  whose Ekind may be void.
16464
16465       Component := First_Entity (Current_Scope);
16466       while Present (Component) loop
16467          if Ekind (Component) = E_Void
16468            and then not Is_Itype (Component)
16469          then
16470             Set_Ekind (Component, E_Component);
16471             Init_Component_Location (Component);
16472          end if;
16473
16474          if Has_Task (Etype (Component)) then
16475             Set_Has_Task (T);
16476          end if;
16477
16478          if Ekind (Component) /= E_Component then
16479             null;
16480
16481          elsif Has_Controlled_Component (Etype (Component))
16482            or else (Chars (Component) /= Name_uParent
16483                     and then Is_Controlled (Etype (Component)))
16484          then
16485             Set_Has_Controlled_Component (T, True);
16486             Final_Storage_Only := Final_Storage_Only
16487               and then Finalize_Storage_Only (Etype (Component));
16488             Ctrl_Components := True;
16489          end if;
16490
16491          Next_Entity (Component);
16492       end loop;
16493
16494       --  A Type is Finalize_Storage_Only only if all its controlled components
16495       --  are also.
16496
16497       if Ctrl_Components then
16498          Set_Finalize_Storage_Only (T, Final_Storage_Only);
16499       end if;
16500
16501       --  Place reference to end record on the proper entity, which may
16502       --  be a partial view.
16503
16504       if Present (Def) then
16505          Process_End_Label (Def, 'e', Prev_T);
16506       end if;
16507    end Record_Type_Definition;
16508
16509    ------------------------
16510    -- Replace_Components --
16511    ------------------------
16512
16513    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
16514       function Process (N : Node_Id) return Traverse_Result;
16515
16516       -------------
16517       -- Process --
16518       -------------
16519
16520       function Process (N : Node_Id) return Traverse_Result is
16521          Comp : Entity_Id;
16522
16523       begin
16524          if Nkind (N) = N_Discriminant_Specification then
16525             Comp := First_Discriminant (Typ);
16526             while Present (Comp) loop
16527                if Chars (Comp) = Chars (Defining_Identifier (N)) then
16528                   Set_Defining_Identifier (N, Comp);
16529                   exit;
16530                end if;
16531
16532                Next_Discriminant (Comp);
16533             end loop;
16534
16535          elsif Nkind (N) = N_Component_Declaration then
16536             Comp := First_Component (Typ);
16537             while Present (Comp) loop
16538                if Chars (Comp) = Chars (Defining_Identifier (N)) then
16539                   Set_Defining_Identifier (N, Comp);
16540                   exit;
16541                end if;
16542
16543                Next_Component (Comp);
16544             end loop;
16545          end if;
16546
16547          return OK;
16548       end Process;
16549
16550       procedure Replace is new Traverse_Proc (Process);
16551
16552    --  Start of processing for Replace_Components
16553
16554    begin
16555       Replace (Decl);
16556    end Replace_Components;
16557
16558    -------------------------------
16559    -- Set_Completion_Referenced --
16560    -------------------------------
16561
16562    procedure Set_Completion_Referenced (E : Entity_Id) is
16563    begin
16564       --  If in main unit, mark entity that is a completion as referenced,
16565       --  warnings go on the partial view when needed.
16566
16567       if In_Extended_Main_Source_Unit (E) then
16568          Set_Referenced (E);
16569       end if;
16570    end Set_Completion_Referenced;
16571
16572    ---------------------
16573    -- Set_Fixed_Range --
16574    ---------------------
16575
16576    --  The range for fixed-point types is complicated by the fact that we
16577    --  do not know the exact end points at the time of the declaration. This
16578    --  is true for three reasons:
16579
16580    --     A size clause may affect the fudging of the end-points
16581    --     A small clause may affect the values of the end-points
16582    --     We try to include the end-points if it does not affect the size
16583
16584    --  This means that the actual end-points must be established at the point
16585    --  when the type is frozen. Meanwhile, we first narrow the range as
16586    --  permitted (so that it will fit if necessary in a small specified size),
16587    --  and then build a range subtree with these narrowed bounds.
16588
16589    --  Set_Fixed_Range constructs the range from real literal values, and sets
16590    --  the range as the Scalar_Range of the given fixed-point type entity.
16591
16592    --  The parent of this range is set to point to the entity so that it is
16593    --  properly hooked into the tree (unlike normal Scalar_Range entries for
16594    --  other scalar types, which are just pointers to the range in the
16595    --  original tree, this would otherwise be an orphan).
16596
16597    --  The tree is left unanalyzed. When the type is frozen, the processing
16598    --  in Freeze.Freeze_Fixed_Point_Type notices that the range is not
16599    --  analyzed, and uses this as an indication that it should complete
16600    --  work on the range (it will know the final small and size values).
16601
16602    procedure Set_Fixed_Range
16603      (E   : Entity_Id;
16604       Loc : Source_Ptr;
16605       Lo  : Ureal;
16606       Hi  : Ureal)
16607    is
16608       S : constant Node_Id :=
16609             Make_Range (Loc,
16610               Low_Bound  => Make_Real_Literal (Loc, Lo),
16611               High_Bound => Make_Real_Literal (Loc, Hi));
16612    begin
16613       Set_Scalar_Range (E, S);
16614       Set_Parent (S, E);
16615    end Set_Fixed_Range;
16616
16617    ----------------------------------
16618    -- Set_Scalar_Range_For_Subtype --
16619    ----------------------------------
16620
16621    procedure Set_Scalar_Range_For_Subtype
16622      (Def_Id : Entity_Id;
16623       R      : Node_Id;
16624       Subt   : Entity_Id)
16625    is
16626       Kind : constant Entity_Kind :=  Ekind (Def_Id);
16627
16628    begin
16629       Set_Scalar_Range (Def_Id, R);
16630
16631       --  We need to link the range into the tree before resolving it so
16632       --  that types that are referenced, including importantly the subtype
16633       --  itself, are properly frozen (Freeze_Expression requires that the
16634       --  expression be properly linked into the tree). Of course if it is
16635       --  already linked in, then we do not disturb the current link.
16636
16637       if No (Parent (R)) then
16638          Set_Parent (R, Def_Id);
16639       end if;
16640
16641       --  Reset the kind of the subtype during analysis of the range, to
16642       --  catch possible premature use in the bounds themselves.
16643
16644       Set_Ekind (Def_Id, E_Void);
16645       Process_Range_Expr_In_Decl (R, Subt);
16646       Set_Ekind (Def_Id, Kind);
16647    end Set_Scalar_Range_For_Subtype;
16648
16649    --------------------------------------------------------
16650    -- Set_Stored_Constraint_From_Discriminant_Constraint --
16651    --------------------------------------------------------
16652
16653    procedure Set_Stored_Constraint_From_Discriminant_Constraint
16654      (E : Entity_Id)
16655    is
16656    begin
16657       --  Make sure set if encountered during Expand_To_Stored_Constraint
16658
16659       Set_Stored_Constraint (E, No_Elist);
16660
16661       --  Give it the right value
16662
16663       if Is_Constrained (E) and then Has_Discriminants (E) then
16664          Set_Stored_Constraint (E,
16665            Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
16666       end if;
16667    end Set_Stored_Constraint_From_Discriminant_Constraint;
16668
16669    -------------------------------------
16670    -- Signed_Integer_Type_Declaration --
16671    -------------------------------------
16672
16673    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16674       Implicit_Base : Entity_Id;
16675       Base_Typ      : Entity_Id;
16676       Lo_Val        : Uint;
16677       Hi_Val        : Uint;
16678       Errs          : Boolean := False;
16679       Lo            : Node_Id;
16680       Hi            : Node_Id;
16681
16682       function Can_Derive_From (E : Entity_Id) return Boolean;
16683       --  Determine whether given bounds allow derivation from specified type
16684
16685       procedure Check_Bound (Expr : Node_Id);
16686       --  Check bound to make sure it is integral and static. If not, post
16687       --  appropriate error message and set Errs flag
16688
16689       ---------------------
16690       -- Can_Derive_From --
16691       ---------------------
16692
16693       --  Note we check both bounds against both end values, to deal with
16694       --  strange types like ones with a range of 0 .. -12341234.
16695
16696       function Can_Derive_From (E : Entity_Id) return Boolean is
16697          Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
16698          Hi : constant Uint := Expr_Value (Type_High_Bound (E));
16699       begin
16700          return Lo <= Lo_Val and then Lo_Val <= Hi
16701                   and then
16702                 Lo <= Hi_Val and then Hi_Val <= Hi;
16703       end Can_Derive_From;
16704
16705       -----------------
16706       -- Check_Bound --
16707       -----------------
16708
16709       procedure Check_Bound (Expr : Node_Id) is
16710       begin
16711          --  If a range constraint is used as an integer type definition, each
16712          --  bound of the range must be defined by a static expression of some
16713          --  integer type, but the two bounds need not have the same integer
16714          --  type (Negative bounds are allowed.) (RM 3.5.4)
16715
16716          if not Is_Integer_Type (Etype (Expr)) then
16717             Error_Msg_N
16718               ("integer type definition bounds must be of integer type", Expr);
16719             Errs := True;
16720
16721          elsif not Is_OK_Static_Expression (Expr) then
16722             Flag_Non_Static_Expr
16723               ("non-static expression used for integer type bound!", Expr);
16724             Errs := True;
16725
16726          --  The bounds are folded into literals, and we set their type to be
16727          --  universal, to avoid typing difficulties: we cannot set the type
16728          --  of the literal to the new type, because this would be a forward
16729          --  reference for the back end,  and if the original type is user-
16730          --  defined this can lead to spurious semantic errors (e.g. 2928-003).
16731
16732          else
16733             if Is_Entity_Name (Expr) then
16734                Fold_Uint (Expr, Expr_Value (Expr), True);
16735             end if;
16736
16737             Set_Etype (Expr, Universal_Integer);
16738          end if;
16739       end Check_Bound;
16740
16741    --  Start of processing for Signed_Integer_Type_Declaration
16742
16743    begin
16744       --  Create an anonymous base type
16745
16746       Implicit_Base :=
16747         Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
16748
16749       --  Analyze and check the bounds, they can be of any integer type
16750
16751       Lo := Low_Bound (Def);
16752       Hi := High_Bound (Def);
16753
16754       --  Arbitrarily use Integer as the type if either bound had an error
16755
16756       if Hi = Error or else Lo = Error then
16757          Base_Typ := Any_Integer;
16758          Set_Error_Posted (T, True);
16759
16760       --  Here both bounds are OK expressions
16761
16762       else
16763          Analyze_And_Resolve (Lo, Any_Integer);
16764          Analyze_And_Resolve (Hi, Any_Integer);
16765
16766          Check_Bound (Lo);
16767          Check_Bound (Hi);
16768
16769          if Errs then
16770             Hi := Type_High_Bound (Standard_Long_Long_Integer);
16771             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
16772          end if;
16773
16774          --  Find type to derive from
16775
16776          Lo_Val := Expr_Value (Lo);
16777          Hi_Val := Expr_Value (Hi);
16778
16779          if Can_Derive_From (Standard_Short_Short_Integer) then
16780             Base_Typ := Base_Type (Standard_Short_Short_Integer);
16781
16782          elsif Can_Derive_From (Standard_Short_Integer) then
16783             Base_Typ := Base_Type (Standard_Short_Integer);
16784
16785          elsif Can_Derive_From (Standard_Integer) then
16786             Base_Typ := Base_Type (Standard_Integer);
16787
16788          elsif Can_Derive_From (Standard_Long_Integer) then
16789             Base_Typ := Base_Type (Standard_Long_Integer);
16790
16791          elsif Can_Derive_From (Standard_Long_Long_Integer) then
16792             Base_Typ := Base_Type (Standard_Long_Long_Integer);
16793
16794          else
16795             Base_Typ := Base_Type (Standard_Long_Long_Integer);
16796             Error_Msg_N ("integer type definition bounds out of range", Def);
16797             Hi := Type_High_Bound (Standard_Long_Long_Integer);
16798             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
16799          end if;
16800       end if;
16801
16802       --  Complete both implicit base and declared first subtype entities
16803
16804       Set_Etype          (Implicit_Base, Base_Typ);
16805       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
16806       Set_Size_Info      (Implicit_Base,                (Base_Typ));
16807       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
16808       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
16809
16810       Set_Ekind          (T, E_Signed_Integer_Subtype);
16811       Set_Etype          (T, Implicit_Base);
16812
16813       Set_Size_Info      (T,                (Implicit_Base));
16814       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
16815       Set_Scalar_Range   (T, Def);
16816       Set_RM_Size        (T, UI_From_Int (Minimum_Size (T)));
16817       Set_Is_Constrained (T);
16818    end Signed_Integer_Type_Declaration;
16819
16820 end Sem_Ch3;