OSDN Git Service

gcc/ada/
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_ch3.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              S E M _ C H 3                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2008, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 3,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT; see file COPYING3.  If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license.          --
20 --                                                                          --
21 -- GNAT was originally developed  by the GNAT team at  New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
23 --                                                                          --
24 ------------------------------------------------------------------------------
25
26 with Atree;    use Atree;
27 with Checks;   use Checks;
28 with Debug;    use Debug;
29 with Elists;   use Elists;
30 with Einfo;    use Einfo;
31 with Errout;   use Errout;
32 with Eval_Fat; use Eval_Fat;
33 with Exp_Ch3;  use Exp_Ch3;
34 with Exp_Ch9;  use Exp_Ch9;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Dist; use Exp_Dist;
37 with Exp_Tss;  use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname;    use Fname;
40 with Freeze;   use Freeze;
41 with Itypes;   use Itypes;
42 with Layout;   use Layout;
43 with Lib;      use Lib;
44 with Lib.Xref; use Lib.Xref;
45 with Namet;    use Namet;
46 with Nmake;    use Nmake;
47 with Opt;      use Opt;
48 with Restrict; use Restrict;
49 with Rident;   use Rident;
50 with Rtsfind;  use Rtsfind;
51 with Sem;      use Sem;
52 with Sem_Case; use Sem_Case;
53 with Sem_Cat;  use Sem_Cat;
54 with Sem_Ch6;  use Sem_Ch6;
55 with Sem_Ch7;  use Sem_Ch7;
56 with Sem_Ch8;  use Sem_Ch8;
57 with Sem_Ch13; use Sem_Ch13;
58 with Sem_Disp; use Sem_Disp;
59 with Sem_Dist; use Sem_Dist;
60 with Sem_Elim; use Sem_Elim;
61 with Sem_Eval; use Sem_Eval;
62 with Sem_Mech; use Sem_Mech;
63 with Sem_Res;  use Sem_Res;
64 with Sem_Smem; use Sem_Smem;
65 with Sem_Type; use Sem_Type;
66 with Sem_Util; use Sem_Util;
67 with Sem_Warn; use Sem_Warn;
68 with Stand;    use Stand;
69 with Sinfo;    use Sinfo;
70 with Snames;   use Snames;
71 with Targparm; use Targparm;
72 with Tbuild;   use Tbuild;
73 with Ttypes;   use Ttypes;
74 with Uintp;    use Uintp;
75 with Urealp;   use Urealp;
76
77 package body Sem_Ch3 is
78
79    -----------------------
80    -- Local Subprograms --
81    -----------------------
82
83    procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
84    --  Ada 2005 (AI-251): Add the tag components corresponding to all the
85    --  abstract interface types implemented by a record type or a derived
86    --  record type.
87
88    procedure Build_Derived_Type
89      (N             : Node_Id;
90       Parent_Type   : Entity_Id;
91       Derived_Type  : Entity_Id;
92       Is_Completion : Boolean;
93       Derive_Subps  : Boolean := True);
94    --  Create and decorate a Derived_Type given the Parent_Type entity. N is
95    --  the N_Full_Type_Declaration node containing the derived type definition.
96    --  Parent_Type is the entity for the parent type in the derived type
97    --  definition and Derived_Type the actual derived type. Is_Completion must
98    --  be set to False if Derived_Type is the N_Defining_Identifier node in N
99    --  (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
100    --  completion of a private type declaration. If Is_Completion is set to
101    --  True, N is the completion of a private type declaration and Derived_Type
102    --  is different from the defining identifier inside N (i.e. Derived_Type /=
103    --  Defining_Identifier (N)). Derive_Subps indicates whether the parent
104    --  subprograms should be derived. The only case where this parameter is
105    --  False is when Build_Derived_Type is recursively called to process an
106    --  implicit derived full type for a type derived from a private type (in
107    --  that case the subprograms must only be derived for the private view of
108    --  the type).
109    --
110    --  ??? These flags need a bit of re-examination and re-documentation:
111    --  ???  are they both necessary (both seem related to the recursion)?
112
113    procedure Build_Derived_Access_Type
114      (N            : Node_Id;
115       Parent_Type  : Entity_Id;
116       Derived_Type : Entity_Id);
117    --  Subsidiary procedure to Build_Derived_Type. For a derived access type,
118    --  create an implicit base if the parent type is constrained or if the
119    --  subtype indication has a constraint.
120
121    procedure Build_Derived_Array_Type
122      (N            : Node_Id;
123       Parent_Type  : Entity_Id;
124       Derived_Type : Entity_Id);
125    --  Subsidiary procedure to Build_Derived_Type. For a derived array type,
126    --  create an implicit base if the parent type is constrained or if the
127    --  subtype indication has a constraint.
128
129    procedure Build_Derived_Concurrent_Type
130      (N            : Node_Id;
131       Parent_Type  : Entity_Id;
132       Derived_Type : Entity_Id);
133    --  Subsidiary procedure to Build_Derived_Type. For a derived task or
134    --  protected type, inherit entries and protected subprograms, check
135    --  legality of discriminant constraints if any.
136
137    procedure Build_Derived_Enumeration_Type
138      (N            : Node_Id;
139       Parent_Type  : Entity_Id;
140       Derived_Type : Entity_Id);
141    --  Subsidiary procedure to Build_Derived_Type. For a derived enumeration
142    --  type, we must create a new list of literals. Types derived from
143    --  Character and Wide_Character are special-cased.
144
145    procedure Build_Derived_Numeric_Type
146      (N            : Node_Id;
147       Parent_Type  : Entity_Id;
148       Derived_Type : Entity_Id);
149    --  Subsidiary procedure to Build_Derived_Type. For numeric types, create
150    --  an anonymous base type, and propagate constraint to subtype if needed.
151
152    procedure Build_Derived_Private_Type
153      (N             : Node_Id;
154       Parent_Type   : Entity_Id;
155       Derived_Type  : Entity_Id;
156       Is_Completion : Boolean;
157       Derive_Subps  : Boolean := True);
158    --  Subsidiary procedure to Build_Derived_Type. This procedure is complex
159    --  because the parent may or may not have a completion, and the derivation
160    --  may itself be a completion.
161
162    procedure Build_Derived_Record_Type
163      (N            : Node_Id;
164       Parent_Type  : Entity_Id;
165       Derived_Type : Entity_Id;
166       Derive_Subps : Boolean := True);
167    --  Subsidiary procedure for Build_Derived_Type and
168    --  Analyze_Private_Extension_Declaration used for tagged and untagged
169    --  record types. All parameters are as in Build_Derived_Type except that
170    --  N, in addition to being an N_Full_Type_Declaration node, can also be an
171    --  N_Private_Extension_Declaration node. See the definition of this routine
172    --  for much more info. Derive_Subps indicates whether subprograms should
173    --  be derived from the parent type. The only case where Derive_Subps is
174    --  False is for an implicit derived full type for a type derived from a
175    --  private type (see Build_Derived_Type).
176
177    procedure Build_Discriminal (Discrim : Entity_Id);
178    --  Create the discriminal corresponding to discriminant Discrim, that is
179    --  the parameter corresponding to Discrim to be used in initialization
180    --  procedures for the type where Discrim is a discriminant. Discriminals
181    --  are not used during semantic analysis, and are not fully defined
182    --  entities until expansion. Thus they are not given a scope until
183    --  initialization procedures are built.
184
185    function Build_Discriminant_Constraints
186      (T           : Entity_Id;
187       Def         : Node_Id;
188       Derived_Def : Boolean := False) return Elist_Id;
189    --  Validate discriminant constraints and return the list of the constraints
190    --  in order of discriminant declarations, where T is the discriminated
191    --  unconstrained type. Def is the N_Subtype_Indication node where the
192    --  discriminants constraints for T are specified. Derived_Def is True
193    --  when building the discriminant constraints in a derived type definition
194    --  of the form "type D (...) is new T (xxx)". In this case T is the parent
195    --  type and Def is the constraint "(xxx)" on T and this routine sets the
196    --  Corresponding_Discriminant field of the discriminants in the derived
197    --  type D to point to the corresponding discriminants in the parent type T.
198
199    procedure Build_Discriminated_Subtype
200      (T           : Entity_Id;
201       Def_Id      : Entity_Id;
202       Elist       : Elist_Id;
203       Related_Nod : Node_Id;
204       For_Access  : Boolean := False);
205    --  Subsidiary procedure to Constrain_Discriminated_Type and to
206    --  Process_Incomplete_Dependents. Given
207    --
208    --     T (a possibly discriminated base type)
209    --     Def_Id (a very partially built subtype for T),
210    --
211    --  the call completes Def_Id to be the appropriate E_*_Subtype.
212    --
213    --  The Elist is the list of discriminant constraints if any (it is set
214    --  to No_Elist if T is not a discriminated type, and to an empty list if
215    --  T has discriminants but there are no discriminant constraints). The
216    --  Related_Nod is the same as Decl_Node in Create_Constrained_Components.
217    --  The For_Access says whether or not this subtype is really constraining
218    --  an access type. That is its sole purpose is the designated type of an
219    --  access type -- in which case a Private_Subtype Is_For_Access_Subtype
220    --  is built to avoid freezing T when the access subtype is frozen.
221
222    function Build_Scalar_Bound
223      (Bound : Node_Id;
224       Par_T : Entity_Id;
225       Der_T : Entity_Id) return Node_Id;
226    --  The bounds of a derived scalar type are conversions of the bounds of
227    --  the parent type. Optimize the representation if the bounds are literals.
228    --  Needs a more complete spec--what are the parameters exactly, and what
229    --  exactly is the returned value, and how is Bound affected???
230
231    procedure Build_Itype_Reference
232      (Ityp : Entity_Id;
233       Nod  : Node_Id);
234    --  Create a reference to an internal type, for use by Gigi. The back-end
235    --  elaborates itypes on demand, i.e. when their first use is seen. This
236    --  can lead to scope anomalies if the first use is within a scope that is
237    --  nested within the scope that contains  the point of definition of the
238    --  itype. The Itype_Reference node forces the elaboration of the itype
239    --  in the proper scope. The node is inserted after Nod, which is the
240    --  enclosing declaration that generated Ityp.
241    --
242    --  A related mechanism is used during expansion, for itypes created in
243    --  branches of conditionals. See Ensure_Defined in exp_util.
244    --  Could both mechanisms be merged ???
245
246    procedure Build_Underlying_Full_View
247      (N   : Node_Id;
248       Typ : Entity_Id;
249       Par : Entity_Id);
250    --  If the completion of a private type is itself derived from a private
251    --  type, or if the full view of a private subtype is itself private, the
252    --  back-end has no way to compute the actual size of this type. We build
253    --  an internal subtype declaration of the proper parent type to convey
254    --  this information. This extra mechanism is needed because a full
255    --  view cannot itself have a full view (it would get clobbered during
256    --  view exchanges).
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_Interfaces (N : Node_Id; Def : Node_Id);
292    --  Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
293
294    procedure Check_Or_Process_Discriminants
295      (N    : Node_Id;
296       T    : Entity_Id;
297       Prev : Entity_Id := Empty);
298    --  If T is the full declaration of an incomplete or private type, check the
299    --  conformance of the discriminants, otherwise process them. Prev is the
300    --  entity of the partial declaration, if any.
301
302    procedure Check_Real_Bound (Bound : Node_Id);
303    --  Check given bound for being of real type and static. If not, post an
304    --  appropriate message, and rewrite the bound with the real literal zero.
305
306    procedure Constant_Redeclaration
307      (Id : Entity_Id;
308       N  : Node_Id;
309       T  : out Entity_Id);
310    --  Various checks on legality of full declaration of deferred constant.
311    --  Id is the entity for the redeclaration, N is the N_Object_Declaration,
312    --  node. The caller has not yet set any attributes of this entity.
313
314    function Contain_Interface
315      (Iface  : Entity_Id;
316       Ifaces : Elist_Id) return Boolean;
317    --  Ada 2005: Determine whether Iface is present in the list Ifaces
318
319    procedure Convert_Scalar_Bounds
320      (N            : Node_Id;
321       Parent_Type  : Entity_Id;
322       Derived_Type : Entity_Id;
323       Loc          : Source_Ptr);
324    --  For derived scalar types, convert the bounds in the type definition to
325    --  the derived type, and complete their analysis. Given a constraint of the
326    --  form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
327    --  T'Base, the parent_type. The bounds of the derived type (the anonymous
328    --  base) are copies of Lo and Hi. Finally, the bounds of the derived
329    --  subtype are conversions of those bounds to the derived_type, so that
330    --  their typing is consistent.
331
332    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
333    --  Copies attributes from array base type T2 to array base type T1. Copies
334    --  only attributes that apply to base types, but not subtypes.
335
336    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
337    --  Copies attributes from array subtype T2 to array subtype T1. Copies
338    --  attributes that apply to both subtypes and base types.
339
340    procedure Create_Constrained_Components
341      (Subt        : Entity_Id;
342       Decl_Node   : Node_Id;
343       Typ         : Entity_Id;
344       Constraints : Elist_Id);
345    --  Build the list of entities for a constrained discriminated record
346    --  subtype. If a component depends on a discriminant, replace its subtype
347    --  using the discriminant values in the discriminant constraint. Subt
348    --  is the defining identifier for the subtype whose list of constrained
349    --  entities we will create. Decl_Node is the type declaration node where
350    --  we will attach all the itypes created. Typ is the base discriminated
351    --  type for the subtype Subt. Constraints is the list of discriminant
352    --  constraints for Typ.
353
354    function Constrain_Component_Type
355      (Comp            : Entity_Id;
356       Constrained_Typ : Entity_Id;
357       Related_Node    : Node_Id;
358       Typ             : Entity_Id;
359       Constraints     : Elist_Id) return Entity_Id;
360    --  Given a discriminated base type Typ, a list of discriminant constraint
361    --  Constraints for Typ and a component of Typ, with type Compon_Type,
362    --  create and return the type corresponding to Compon_type where all
363    --  discriminant references are replaced with the corresponding constraint.
364    --  If no discriminant references occur in Compon_Typ then return it as is.
365    --  Constrained_Typ is the final constrained subtype to which the
366    --  constrained Compon_Type belongs. Related_Node is the node where we will
367    --  attach all the itypes created.
368    --
369    --  Above description is confused, what is Compon_Type???
370
371    procedure Constrain_Access
372      (Def_Id      : in out Entity_Id;
373       S           : Node_Id;
374       Related_Nod : Node_Id);
375    --  Apply a list of constraints to an access type. If Def_Id is empty, it is
376    --  an anonymous type created for a subtype indication. In that case it is
377    --  created in the procedure and attached to Related_Nod.
378
379    procedure Constrain_Array
380      (Def_Id      : in out Entity_Id;
381       SI          : Node_Id;
382       Related_Nod : Node_Id;
383       Related_Id  : Entity_Id;
384       Suffix      : Character);
385    --  Apply a list of index constraints to an unconstrained array type. The
386    --  first parameter is the entity for the resulting subtype. A value of
387    --  Empty for Def_Id indicates that an implicit type must be created, but
388    --  creation is delayed (and must be done by this procedure) because other
389    --  subsidiary implicit types must be created first (which is why Def_Id
390    --  is an in/out parameter). The second parameter is a subtype indication
391    --  node for the constrained array to be created (e.g. something of the
392    --  form string (1 .. 10)). Related_Nod gives the place where this type
393    --  has to be inserted in the tree. The Related_Id and Suffix parameters
394    --  are used to build the associated Implicit type name.
395
396    procedure Constrain_Concurrent
397      (Def_Id      : in out Entity_Id;
398       SI          : Node_Id;
399       Related_Nod : Node_Id;
400       Related_Id  : Entity_Id;
401       Suffix      : Character);
402    --  Apply list of discriminant constraints to an unconstrained concurrent
403    --  type.
404    --
405    --    SI is the N_Subtype_Indication node containing the constraint and
406    --    the unconstrained type to constrain.
407    --
408    --    Def_Id is the entity for the resulting constrained subtype. A value
409    --    of Empty for Def_Id indicates that an implicit type must be created,
410    --    but creation is delayed (and must be done by this procedure) because
411    --    other subsidiary implicit types must be created first (which is why
412    --    Def_Id is an in/out parameter).
413    --
414    --    Related_Nod gives the place where this type has to be inserted
415    --    in the tree
416    --
417    --  The last two arguments are used to create its external name if needed.
418
419    function Constrain_Corresponding_Record
420      (Prot_Subt   : Entity_Id;
421       Corr_Rec    : Entity_Id;
422       Related_Nod : Node_Id;
423       Related_Id  : Entity_Id) return Entity_Id;
424    --  When constraining a protected type or task type with discriminants,
425    --  constrain the corresponding record with the same discriminant values.
426
427    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
428    --  Constrain a decimal fixed point type with a digits constraint and/or a
429    --  range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
430
431    procedure Constrain_Discriminated_Type
432      (Def_Id      : Entity_Id;
433       S           : Node_Id;
434       Related_Nod : Node_Id;
435       For_Access  : Boolean := False);
436    --  Process discriminant constraints of composite type. Verify that values
437    --  have been provided for all discriminants, that the original type is
438    --  unconstrained, and that the types of the supplied expressions match
439    --  the discriminant types. The first three parameters are like in routine
440    --  Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
441    --  of For_Access.
442
443    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
444    --  Constrain an enumeration type with a range constraint. This is identical
445    --  to Constrain_Integer, but for the Ekind of the resulting subtype.
446
447    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
448    --  Constrain a floating point type with either a digits constraint
449    --  and/or a range constraint, building a E_Floating_Point_Subtype.
450
451    procedure Constrain_Index
452      (Index        : Node_Id;
453       S            : Node_Id;
454       Related_Nod  : Node_Id;
455       Related_Id   : Entity_Id;
456       Suffix       : Character;
457       Suffix_Index : Nat);
458    --  Process an index constraint in a constrained array declaration. The
459    --  constraint can be a subtype name, or a range with or without an explicit
460    --  subtype mark. The index is the corresponding index of the unconstrained
461    --  array. The Related_Id and Suffix parameters are used to build the
462    --  associated Implicit type name.
463
464    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
465    --  Build subtype of a signed or modular integer type
466
467    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
468    --  Constrain an ordinary fixed point type with a range constraint, and
469    --  build an E_Ordinary_Fixed_Point_Subtype entity.
470
471    procedure Copy_And_Swap (Priv, Full : Entity_Id);
472    --  Copy the Priv entity into the entity of its full declaration then swap
473    --  the two entities in such a manner that the former private type is now
474    --  seen as a full type.
475
476    procedure Decimal_Fixed_Point_Type_Declaration
477      (T   : Entity_Id;
478       Def : Node_Id);
479    --  Create a new decimal fixed point type, and apply the constraint to
480    --  obtain a subtype of this new type.
481
482    procedure Complete_Private_Subtype
483      (Priv        : Entity_Id;
484       Full        : Entity_Id;
485       Full_Base   : Entity_Id;
486       Related_Nod : Node_Id);
487    --  Complete the implicit full view of a private subtype by setting the
488    --  appropriate semantic fields. If the full view of the parent is a record
489    --  type, build constrained components of subtype.
490
491    procedure Derive_Progenitor_Subprograms
492      (Parent_Type : Entity_Id;
493       Tagged_Type : Entity_Id);
494    --  Ada 2005 (AI-251): To complete type derivation, collect the primitive
495    --  operations of progenitors of Tagged_Type, and replace the subsidiary
496    --  subtypes with Tagged_Type, to build the specs of the inherited interface
497    --  primitives. The derived primitives are aliased to those of the
498    --  interface. This routine takes care also of transferring to the full-view
499    --  subprograms associated with the partial-view of Tagged_Type that cover
500    --  interface primitives.
501
502    procedure Derived_Standard_Character
503      (N             : Node_Id;
504       Parent_Type   : Entity_Id;
505       Derived_Type  : Entity_Id);
506    --  Subsidiary procedure to Build_Derived_Enumeration_Type which handles
507    --  derivations from types Standard.Character and Standard.Wide_Character.
508
509    procedure Derived_Type_Declaration
510      (T             : Entity_Id;
511       N             : Node_Id;
512       Is_Completion : Boolean);
513    --  Process a derived type declaration. Build_Derived_Type is invoked
514    --  to process the actual derived type definition. Parameters N and
515    --  Is_Completion have the same meaning as in Build_Derived_Type.
516    --  T is the N_Defining_Identifier for the entity defined in the
517    --  N_Full_Type_Declaration node N, that is T is the derived type.
518
519    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
520    --  Insert each literal in symbol table, as an overloadable identifier. Each
521    --  enumeration type is mapped into a sequence of integers, and each literal
522    --  is defined as a constant with integer value. If any of the literals are
523    --  character literals, the type is a character type, which means that
524    --  strings are legal aggregates for arrays of components of the type.
525
526    function Expand_To_Stored_Constraint
527      (Typ        : Entity_Id;
528       Constraint : Elist_Id) return Elist_Id;
529    --  Given a constraint (i.e. a list of expressions) on the discriminants of
530    --  Typ, expand it into a constraint on the stored discriminants and return
531    --  the new list of expressions constraining the stored discriminants.
532
533    function Find_Type_Of_Object
534      (Obj_Def     : Node_Id;
535       Related_Nod : Node_Id) return Entity_Id;
536    --  Get type entity for object referenced by Obj_Def, attaching the
537    --  implicit types generated to Related_Nod
538
539    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
540    --  Create a new float and apply the constraint to obtain subtype of it
541
542    function Has_Range_Constraint (N : Node_Id) return Boolean;
543    --  Given an N_Subtype_Indication node N, return True if a range constraint
544    --  is present, either directly, or as part of a digits or delta constraint.
545    --  In addition, a digits constraint in the decimal case returns True, since
546    --  it establishes a default range if no explicit range is present.
547
548    function Inherit_Components
549      (N             : Node_Id;
550       Parent_Base   : Entity_Id;
551       Derived_Base  : Entity_Id;
552       Is_Tagged     : Boolean;
553       Inherit_Discr : Boolean;
554       Discs         : Elist_Id) return Elist_Id;
555    --  Called from Build_Derived_Record_Type to inherit the components of
556    --  Parent_Base (a base type) into the Derived_Base (the derived base type).
557    --  For more information on derived types and component inheritance please
558    --  consult the comment above the body of Build_Derived_Record_Type.
559    --
560    --    N is the original derived type declaration
561    --
562    --    Is_Tagged is set if we are dealing with tagged types
563    --
564    --    If Inherit_Discr is set, Derived_Base inherits its discriminants from
565    --    Parent_Base, otherwise no discriminants are inherited.
566    --
567    --    Discs gives the list of constraints that apply to Parent_Base in the
568    --    derived type declaration. If Discs is set to No_Elist, then we have
569    --    the following situation:
570    --
571    --      type Parent (D1..Dn : ..) is [tagged] record ...;
572    --      type Derived is new Parent [with ...];
573    --
574    --    which gets treated as
575    --
576    --      type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
577    --
578    --  For untagged types the returned value is an association list. The list
579    --  starts from the association (Parent_Base => Derived_Base), and then it
580    --  contains a sequence of the associations of the form
581    --
582    --    (Old_Component => New_Component),
583    --
584    --  where Old_Component is the Entity_Id of a component in Parent_Base and
585    --  New_Component is the Entity_Id of the corresponding component in
586    --  Derived_Base. For untagged records, this association list is needed when
587    --  copying the record declaration for the derived base. In the tagged case
588    --  the value returned is irrelevant.
589
590    function Is_Progenitor
591      (Iface : Entity_Id;
592       Typ   :  Entity_Id) return Boolean;
593    --  Determine whether type Typ implements interface Iface. This requires
594    --  traversing the list of abstract interfaces of the type, as well as that
595    --  of the ancestor types. The predicate is used to determine when a formal
596    --  in the signature of an inherited operation must carry the derived type.
597
598    function Is_Valid_Constraint_Kind
599      (T_Kind          : Type_Kind;
600       Constraint_Kind : Node_Kind) return Boolean;
601    --  Returns True if it is legal to apply the given kind of constraint to the
602    --  given kind of type (index constraint to an array type, for example).
603
604    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
605    --  Create new modular type. Verify that modulus is in  bounds and is
606    --  a power of two (implementation restriction).
607
608    procedure New_Concatenation_Op (Typ : Entity_Id);
609    --  Create an abbreviated declaration for an operator in order to
610    --  materialize concatenation on array types.
611
612    procedure Ordinary_Fixed_Point_Type_Declaration
613      (T   : Entity_Id;
614       Def : Node_Id);
615    --  Create a new ordinary fixed point type, and apply the constraint to
616    --  obtain subtype of it.
617
618    procedure Prepare_Private_Subtype_Completion
619      (Id          : Entity_Id;
620       Related_Nod : Node_Id);
621    --  Id is a subtype of some private type. Creates the full declaration
622    --  associated with Id whenever possible, i.e. when the full declaration
623    --  of the base type is already known. Records each subtype into
624    --  Private_Dependents of the base type.
625
626    procedure Process_Incomplete_Dependents
627      (N      : Node_Id;
628       Full_T : Entity_Id;
629       Inc_T  : Entity_Id);
630    --  Process all entities that depend on an incomplete type. There include
631    --  subtypes, subprogram types that mention the incomplete type in their
632    --  profiles, and subprogram with access parameters that designate the
633    --  incomplete type.
634
635    --  Inc_T is the defining identifier of an incomplete type declaration, its
636    --  Ekind is E_Incomplete_Type.
637    --
638    --    N is the corresponding N_Full_Type_Declaration for Inc_T.
639    --
640    --    Full_T is N's defining identifier.
641    --
642    --  Subtypes of incomplete types with discriminants are completed when the
643    --  parent type is. This is simpler than private subtypes, because they can
644    --  only appear in the same scope, and there is no need to exchange views.
645    --  Similarly, access_to_subprogram types may have a parameter or a return
646    --  type that is an incomplete type, and that must be replaced with the
647    --  full type.
648    --
649    --  If the full type is tagged, subprogram with access parameters that
650    --  designated the incomplete may be primitive operations of the full type,
651    --  and have to be processed accordingly.
652
653    procedure Process_Real_Range_Specification (Def : Node_Id);
654    --  Given the type definition for a real type, this procedure processes and
655    --  checks the real range specification of this type definition if one is
656    --  present. If errors are found, error messages are posted, and the
657    --  Real_Range_Specification of Def is reset to Empty.
658
659    procedure Record_Type_Declaration
660      (T    : Entity_Id;
661       N    : Node_Id;
662       Prev : Entity_Id);
663    --  Process a record type declaration (for both untagged and tagged
664    --  records). Parameters T and N are exactly like in procedure
665    --  Derived_Type_Declaration, except that no flag Is_Completion is needed
666    --  for this routine. If this is the completion of an incomplete type
667    --  declaration, Prev is the entity of the incomplete declaration, used for
668    --  cross-referencing. Otherwise Prev = T.
669
670    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
671    --  This routine is used to process the actual record type definition (both
672    --  for untagged and tagged records). Def is a record type definition node.
673    --  This procedure analyzes the components in this record type definition.
674    --  Prev_T is the entity for the enclosing record type. It is provided so
675    --  that its Has_Task flag can be set if any of the component have Has_Task
676    --  set. If the declaration is the completion of an incomplete type
677    --  declaration, Prev_T is the original incomplete type, whose full view is
678    --  the record type.
679
680    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
681    --  Subsidiary to Build_Derived_Record_Type. For untagged records, we
682    --  build a copy of the declaration tree of the parent, and we create
683    --  independently the list of components for the derived type. Semantic
684    --  information uses the component entities, but record representation
685    --  clauses are validated on the declaration tree. This procedure replaces
686    --  discriminants and components in the declaration with those that have
687    --  been created by Inherit_Components.
688
689    procedure Set_Fixed_Range
690      (E   : Entity_Id;
691       Loc : Source_Ptr;
692       Lo  : Ureal;
693       Hi  : Ureal);
694    --  Build a range node with the given bounds and set it as the Scalar_Range
695    --  of the given fixed-point type entity. Loc is the source location used
696    --  for the constructed range. See body for further details.
697
698    procedure Set_Scalar_Range_For_Subtype
699      (Def_Id : Entity_Id;
700       R      : Node_Id;
701       Subt   : Entity_Id);
702    --  This routine is used to set the scalar range field for a subtype given
703    --  Def_Id, the entity for the subtype, and R, the range expression for the
704    --  scalar range. Subt provides the parent subtype to be used to analyze,
705    --  resolve, and check the given range.
706
707    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
708    --  Create a new signed integer entity, and apply the constraint to obtain
709    --  the required first named subtype of this type.
710
711    procedure Set_Stored_Constraint_From_Discriminant_Constraint
712      (E : Entity_Id);
713    --  E is some record type. This routine computes E's Stored_Constraint
714    --  from its Discriminant_Constraint.
715
716    procedure Diagnose_Interface (N : Node_Id;  E : Entity_Id);
717    --  Check that an entity in a list of progenitors is an interface,
718    --  emit error otherwise.
719
720    -----------------------
721    -- Access_Definition --
722    -----------------------
723
724    function Access_Definition
725      (Related_Nod : Node_Id;
726       N           : Node_Id) return Entity_Id
727    is
728       Loc        : constant Source_Ptr := Sloc (Related_Nod);
729       Anon_Type  : Entity_Id;
730       Anon_Scope : Entity_Id;
731       Desig_Type : Entity_Id;
732       Decl       : Entity_Id;
733
734    begin
735       if Is_Entry (Current_Scope)
736         and then Is_Task_Type (Etype (Scope (Current_Scope)))
737       then
738          Error_Msg_N ("task entries cannot have access parameters", N);
739          return Empty;
740       end if;
741
742       --  Ada 2005: for an object declaration the corresponding anonymous
743       --  type is declared in the current scope.
744
745       --  If the access definition is the return type of another access to
746       --  function, scope is the current one, because it is the one of the
747       --  current type declaration.
748
749       if Nkind_In (Related_Nod, N_Object_Declaration,
750                                 N_Access_Function_Definition)
751       then
752          Anon_Scope := Current_Scope;
753
754       --  For the anonymous function result case, retrieve the scope of the
755       --  function specification's associated entity rather than using the
756       --  current scope. The current scope will be the function itself if the
757       --  formal part is currently being analyzed, but will be the parent scope
758       --  in the case of a parameterless function, and we always want to use
759       --  the function's parent scope. Finally, if the function is a child
760       --  unit, we must traverse the tree to retrieve the proper entity.
761
762       elsif Nkind (Related_Nod) = N_Function_Specification
763         and then Nkind (Parent (N)) /= N_Parameter_Specification
764       then
765          --  If the current scope is a protected type, the anonymous access
766          --  is associated with one of the protected operations, and must
767          --  be available in the scope that encloses the protected declaration.
768          --  Otherwise the type is in the scope enclosing the subprogram.
769
770          if Ekind (Current_Scope) = E_Protected_Type then
771             Anon_Scope := Scope (Scope (Defining_Entity (Related_Nod)));
772          else
773             Anon_Scope := Scope (Defining_Entity (Related_Nod));
774          end if;
775
776       else
777          --  For access formals, access components, and access discriminants,
778          --  the scope is that of the enclosing declaration,
779
780          Anon_Scope := Scope (Current_Scope);
781       end if;
782
783       Anon_Type :=
784         Create_Itype
785          (E_Anonymous_Access_Type, Related_Nod, Scope_Id =>  Anon_Scope);
786
787       if All_Present (N)
788         and then Ada_Version >= Ada_05
789       then
790          Error_Msg_N ("ALL is not permitted for anonymous access types", N);
791       end if;
792
793       --  Ada 2005 (AI-254): In case of anonymous access to subprograms call
794       --  the corresponding semantic routine
795
796       if Present (Access_To_Subprogram_Definition (N)) then
797          Access_Subprogram_Declaration
798            (T_Name => Anon_Type,
799             T_Def  => Access_To_Subprogram_Definition (N));
800
801          if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
802             Set_Ekind
803               (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
804          else
805             Set_Ekind
806               (Anon_Type, E_Anonymous_Access_Subprogram_Type);
807          end if;
808
809          Set_Can_Use_Internal_Rep
810            (Anon_Type, not Always_Compatible_Rep_On_Target);
811
812          --  If the anonymous access is associated with a protected operation
813          --  create a reference to it after the enclosing protected definition
814          --  because the itype will be used in the subsequent bodies.
815
816          if Ekind (Current_Scope) = E_Protected_Type then
817             Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
818          end if;
819
820          return Anon_Type;
821       end if;
822
823       Find_Type (Subtype_Mark (N));
824       Desig_Type := Entity (Subtype_Mark (N));
825
826       Set_Directly_Designated_Type
827                              (Anon_Type, Desig_Type);
828       Set_Etype              (Anon_Type, Anon_Type);
829
830       --  Make sure the anonymous access type has size and alignment fields
831       --  set, as required by gigi. This is necessary in the case of the
832       --  Task_Body_Procedure.
833
834       if not Has_Private_Component (Desig_Type) then
835          Layout_Type (Anon_Type);
836       end if;
837
838       --  ???The following makes no sense, because Anon_Type is an access type
839       --  and therefore cannot have components, private or otherwise. Hence
840       --  the assertion. Not sure what was meant, here.
841       Set_Depends_On_Private (Anon_Type, Has_Private_Component (Anon_Type));
842       pragma Assert (not Depends_On_Private (Anon_Type));
843
844       --  Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
845       --  from Ada 95 semantics. In Ada 2005, anonymous access must specify if
846       --  the null value is allowed. In Ada 95 the null value is never allowed.
847
848       if Ada_Version >= Ada_05 then
849          Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
850       else
851          Set_Can_Never_Be_Null (Anon_Type, True);
852       end if;
853
854       --  The anonymous access type is as public as the discriminated type or
855       --  subprogram that defines it. It is imported (for back-end purposes)
856       --  if the designated type is.
857
858       Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
859
860       --  Ada 2005 (AI-50217): Propagate the attribute that indicates that the
861       --  designated type comes from the limited view.
862
863       Set_From_With_Type (Anon_Type, From_With_Type (Desig_Type));
864
865       --  Ada 2005 (AI-231): Propagate the access-constant attribute
866
867       Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
868
869       --  The context is either a subprogram declaration, object declaration,
870       --  or an access discriminant, in a private or a full type declaration.
871       --  In the case of a subprogram, if the designated type is incomplete,
872       --  the operation will be a primitive operation of the full type, to be
873       --  updated subsequently. If the type is imported through a limited_with
874       --  clause, the subprogram is not a primitive operation of the type
875       --  (which is declared elsewhere in some other scope).
876
877       if Ekind (Desig_Type) = E_Incomplete_Type
878         and then not From_With_Type (Desig_Type)
879         and then Is_Overloadable (Current_Scope)
880       then
881          Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
882          Set_Has_Delayed_Freeze (Current_Scope);
883       end if;
884
885       --  Ada 2005: if the designated type is an interface that may contain
886       --  tasks, create a Master entity for the declaration. This must be done
887       --  before expansion of the full declaration, because the declaration may
888       --  include an expression that is an allocator, whose expansion needs the
889       --  proper Master for the created tasks.
890
891       if Nkind (Related_Nod) = N_Object_Declaration
892          and then Expander_Active
893       then
894          if Is_Interface (Desig_Type)
895            and then Is_Limited_Record (Desig_Type)
896          then
897             Build_Class_Wide_Master (Anon_Type);
898
899          --  Similarly, if the type is an anonymous access that designates
900          --  tasks, create a master entity for it in the current context.
901
902          elsif Has_Task (Desig_Type)
903            and then Comes_From_Source (Related_Nod)
904          then
905             if not Has_Master_Entity (Current_Scope) then
906                Decl :=
907                  Make_Object_Declaration (Loc,
908                    Defining_Identifier =>
909                      Make_Defining_Identifier (Loc, Name_uMaster),
910                    Constant_Present => True,
911                    Object_Definition =>
912                      New_Reference_To (RTE (RE_Master_Id), Loc),
913                    Expression =>
914                      Make_Explicit_Dereference (Loc,
915                        New_Reference_To (RTE (RE_Current_Master), Loc)));
916
917                Insert_Before (Related_Nod, Decl);
918                Analyze (Decl);
919
920                Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
921                Set_Has_Master_Entity (Current_Scope);
922             else
923                Build_Master_Renaming (Related_Nod, Anon_Type);
924             end if;
925          end if;
926       end if;
927
928       --  For a private component of a protected type, it is imperative that
929       --  the back-end elaborate the type immediately after the protected
930       --  declaration, because this type will be used in the declarations
931       --  created for the component within each protected body, so we must
932       --  create an itype reference for it now.
933
934       if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
935          Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
936
937       --  Similarly, if the access definition is the return result of a
938       --  function, create an itype reference for it because it
939       --  will be used within the function body. For a regular function that
940       --  is not a compilation unit, insert reference after the declaration.
941       --  For a protected operation, insert it after the enclosing protected
942       --  type declaration. In either case, do not create a reference for a
943       --  type obtained through a limited_with clause, because this would
944       --  introduce semantic dependencies.
945
946       elsif Nkind (Related_Nod) = N_Function_Specification
947         and then not From_With_Type (Anon_Type)
948       then
949          if Ekind (Current_Scope) = E_Protected_Type then
950             Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
951
952          elsif Is_List_Member (Parent (Related_Nod))
953            and then Nkind (Parent (N)) /= N_Parameter_Specification
954          then
955             Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
956          end if;
957
958       --  Finally, create an itype reference for an object declaration of
959       --  an anonymous access type. This is strictly necessary only for
960       --  deferred constants, but in any case will avoid out-of-scope
961       --  problems in the back-end.
962
963       elsif Nkind (Related_Nod) = N_Object_Declaration then
964          Build_Itype_Reference (Anon_Type, Related_Nod);
965       end if;
966
967       return Anon_Type;
968    end Access_Definition;
969
970    -----------------------------------
971    -- Access_Subprogram_Declaration --
972    -----------------------------------
973
974    procedure Access_Subprogram_Declaration
975      (T_Name : Entity_Id;
976       T_Def  : Node_Id)
977    is
978
979       procedure Check_For_Premature_Usage (Def : Node_Id);
980       --  Check that type T_Name is not used, directly or recursively,
981       --  as a parameter or a return type in Def. Def is either a subtype,
982       --  an access_definition, or an access_to_subprogram_definition.
983
984       -------------------------------
985       -- Check_For_Premature_Usage --
986       -------------------------------
987
988       procedure Check_For_Premature_Usage (Def : Node_Id) is
989          Param : Node_Id;
990
991       begin
992          --  Check for a subtype mark
993
994          if Nkind (Def) in N_Has_Etype then
995             if Etype (Def) = T_Name then
996                Error_Msg_N
997                  ("type& cannot be used before end of its declaration", Def);
998             end if;
999
1000          --  If this is not a subtype, then this is an access_definition
1001
1002          elsif Nkind (Def) = N_Access_Definition then
1003             if Present (Access_To_Subprogram_Definition (Def)) then
1004                Check_For_Premature_Usage
1005                  (Access_To_Subprogram_Definition (Def));
1006             else
1007                Check_For_Premature_Usage (Subtype_Mark (Def));
1008             end if;
1009
1010          --  The only cases left are N_Access_Function_Definition and
1011          --  N_Access_Procedure_Definition.
1012
1013          else
1014             if Present (Parameter_Specifications (Def)) then
1015                Param := First (Parameter_Specifications (Def));
1016                while Present (Param) loop
1017                   Check_For_Premature_Usage (Parameter_Type (Param));
1018                   Param := Next (Param);
1019                end loop;
1020             end if;
1021
1022             if Nkind (Def) = N_Access_Function_Definition then
1023                Check_For_Premature_Usage (Result_Definition (Def));
1024             end if;
1025          end if;
1026       end Check_For_Premature_Usage;
1027
1028       --  Local variables
1029
1030       Formals    : constant List_Id := Parameter_Specifications (T_Def);
1031       Formal     : Entity_Id;
1032       D_Ityp     : Node_Id;
1033       Desig_Type : constant Entity_Id :=
1034                      Create_Itype (E_Subprogram_Type, Parent (T_Def));
1035
1036    --  Start of processing for Access_Subprogram_Declaration
1037
1038    begin
1039       --  Associate the Itype node with the inner full-type declaration or
1040       --  subprogram spec. This is required to handle nested anonymous
1041       --  declarations. For example:
1042
1043       --      procedure P
1044       --       (X : access procedure
1045       --                     (Y : access procedure
1046       --                                   (Z : access T)))
1047
1048       D_Ityp := Associated_Node_For_Itype (Desig_Type);
1049       while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1050                                    N_Private_Type_Declaration,
1051                                    N_Private_Extension_Declaration,
1052                                    N_Procedure_Specification,
1053                                    N_Function_Specification)
1054                    or else
1055                  Nkind_In (D_Ityp, N_Object_Declaration,
1056                                    N_Object_Renaming_Declaration,
1057                                    N_Formal_Object_Declaration,
1058                                    N_Formal_Type_Declaration,
1059                                    N_Task_Type_Declaration,
1060                                    N_Protected_Type_Declaration))
1061       loop
1062          D_Ityp := Parent (D_Ityp);
1063          pragma Assert (D_Ityp /= Empty);
1064       end loop;
1065
1066       Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1067
1068       if Nkind_In (D_Ityp, N_Procedure_Specification,
1069                            N_Function_Specification)
1070       then
1071          Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1072
1073       elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1074                               N_Object_Declaration,
1075                               N_Object_Renaming_Declaration,
1076                               N_Formal_Type_Declaration)
1077       then
1078          Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1079       end if;
1080
1081       if Nkind (T_Def) = N_Access_Function_Definition then
1082          if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1083             declare
1084                Acc : constant Node_Id := Result_Definition (T_Def);
1085
1086             begin
1087                if Present (Access_To_Subprogram_Definition (Acc))
1088                  and then
1089                    Protected_Present (Access_To_Subprogram_Definition (Acc))
1090                then
1091                   Set_Etype
1092                     (Desig_Type,
1093                        Replace_Anonymous_Access_To_Protected_Subprogram
1094                          (T_Def));
1095
1096                else
1097                   Set_Etype
1098                     (Desig_Type,
1099                        Access_Definition (T_Def, Result_Definition (T_Def)));
1100                end if;
1101             end;
1102
1103          else
1104             Analyze (Result_Definition (T_Def));
1105             Set_Etype (Desig_Type, Entity (Result_Definition (T_Def)));
1106          end if;
1107
1108          if not (Is_Type (Etype (Desig_Type))) then
1109             Error_Msg_N
1110               ("expect type in function specification",
1111                Result_Definition (T_Def));
1112          end if;
1113
1114       else
1115          Set_Etype (Desig_Type, Standard_Void_Type);
1116       end if;
1117
1118       if Present (Formals) then
1119          Push_Scope (Desig_Type);
1120
1121          --  A bit of a kludge here. These kludges will be removed when Itypes
1122          --  have proper parent pointers to their declarations???
1123
1124          --  Kludge 1) Link defining_identifier of formals. Required by
1125          --  First_Formal to provide its functionality.
1126
1127          declare
1128             F : Node_Id;
1129
1130          begin
1131             F := First (Formals);
1132             while Present (F) loop
1133                if No (Parent (Defining_Identifier (F))) then
1134                   Set_Parent (Defining_Identifier (F), F);
1135                end if;
1136
1137                Next (F);
1138             end loop;
1139          end;
1140
1141          Process_Formals (Formals, Parent (T_Def));
1142
1143          --  Kludge 2) End_Scope requires that the parent pointer be set to
1144          --  something reasonable, but Itypes don't have parent pointers. So
1145          --  we set it and then unset it ???
1146
1147          Set_Parent (Desig_Type, T_Name);
1148          End_Scope;
1149          Set_Parent (Desig_Type, Empty);
1150       end if;
1151
1152       --  Check for premature usage of the type being defined
1153
1154       Check_For_Premature_Usage (T_Def);
1155
1156       --  The return type and/or any parameter type may be incomplete. Mark
1157       --  the subprogram_type as depending on the incomplete type, so that
1158       --  it can be updated when the full type declaration is seen. This
1159       --  only applies to incomplete types declared in some enclosing scope,
1160       --  not to limited views from other packages.
1161
1162       if Present (Formals) then
1163          Formal := First_Formal (Desig_Type);
1164          while Present (Formal) loop
1165             if Ekind (Formal) /= E_In_Parameter
1166               and then Nkind (T_Def) = N_Access_Function_Definition
1167             then
1168                Error_Msg_N ("functions can only have IN parameters", Formal);
1169             end if;
1170
1171             if Ekind (Etype (Formal)) = E_Incomplete_Type
1172               and then In_Open_Scopes (Scope (Etype (Formal)))
1173             then
1174                Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1175                Set_Has_Delayed_Freeze (Desig_Type);
1176             end if;
1177
1178             Next_Formal (Formal);
1179          end loop;
1180       end if;
1181
1182       --  If the return type is incomplete, this is legal as long as the
1183       --  type is declared in the current scope and will be completed in
1184       --  it (rather than being part of limited view).
1185
1186       if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1187         and then not Has_Delayed_Freeze (Desig_Type)
1188         and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1189       then
1190          Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1191          Set_Has_Delayed_Freeze (Desig_Type);
1192       end if;
1193
1194       Check_Delayed_Subprogram (Desig_Type);
1195
1196       if Protected_Present (T_Def) then
1197          Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1198          Set_Convention (Desig_Type, Convention_Protected);
1199       else
1200          Set_Ekind (T_Name, E_Access_Subprogram_Type);
1201       end if;
1202
1203       Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1204
1205       Set_Etype                    (T_Name, T_Name);
1206       Init_Size_Align              (T_Name);
1207       Set_Directly_Designated_Type (T_Name, Desig_Type);
1208
1209       --  Ada 2005 (AI-231): Propagate the null-excluding attribute
1210
1211       Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1212
1213       Check_Restriction (No_Access_Subprograms, T_Def);
1214    end Access_Subprogram_Declaration;
1215
1216    ----------------------------
1217    -- Access_Type_Declaration --
1218    ----------------------------
1219
1220    procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1221       S : constant Node_Id := Subtype_Indication (Def);
1222       P : constant Node_Id := Parent (Def);
1223
1224       Desig : Entity_Id;
1225       --  Designated type
1226
1227    begin
1228       --  Check for permissible use of incomplete type
1229
1230       if Nkind (S) /= N_Subtype_Indication then
1231          Analyze (S);
1232
1233          if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1234             Set_Directly_Designated_Type (T, Entity (S));
1235          else
1236             Set_Directly_Designated_Type (T,
1237               Process_Subtype (S, P, T, 'P'));
1238          end if;
1239
1240       else
1241          Set_Directly_Designated_Type (T,
1242            Process_Subtype (S, P, T, 'P'));
1243       end if;
1244
1245       if All_Present (Def) or Constant_Present (Def) then
1246          Set_Ekind (T, E_General_Access_Type);
1247       else
1248          Set_Ekind (T, E_Access_Type);
1249       end if;
1250
1251       if Base_Type (Designated_Type (T)) = T then
1252          Error_Msg_N ("access type cannot designate itself", S);
1253
1254       --  In Ada 2005, the type may have a limited view through some unit
1255       --  in its own context, allowing the following circularity that cannot
1256       --  be detected earlier
1257
1258       elsif Is_Class_Wide_Type (Designated_Type (T))
1259         and then Etype (Designated_Type (T)) = T
1260       then
1261          Error_Msg_N
1262            ("access type cannot designate its own classwide type", S);
1263
1264          --  Clean up indication of tagged status to prevent cascaded errors
1265
1266          Set_Is_Tagged_Type (T, False);
1267       end if;
1268
1269       Set_Etype (T, T);
1270
1271       --  If the type has appeared already in a with_type clause, it is
1272       --  frozen and the pointer size is already set. Else, initialize.
1273
1274       if not From_With_Type (T) then
1275          Init_Size_Align (T);
1276       end if;
1277
1278       Desig := Designated_Type (T);
1279
1280       --  If designated type is an imported tagged type, indicate that the
1281       --  access type is also imported, and therefore restricted in its use.
1282       --  The access type may already be imported, so keep setting otherwise.
1283
1284       --  Ada 2005 (AI-50217): If the non-limited view of the designated type
1285       --  is available, use it as the designated type of the access type, so
1286       --  that the back-end gets a usable entity.
1287
1288       if From_With_Type (Desig)
1289         and then Ekind (Desig) /= E_Access_Type
1290       then
1291          Set_From_With_Type (T);
1292       end if;
1293
1294       --  Note that Has_Task is always false, since the access type itself
1295       --  is not a task type. See Einfo for more description on this point.
1296       --  Exactly the same consideration applies to Has_Controlled_Component.
1297
1298       Set_Has_Task (T, False);
1299       Set_Has_Controlled_Component (T, False);
1300
1301       --  Initialize Associated_Final_Chain explicitly to Empty, to avoid
1302       --  problems where an incomplete view of this entity has been previously
1303       --  established by a limited with and an overlaid version of this field
1304       --  (Stored_Constraint) was initialized for the incomplete view.
1305
1306       Set_Associated_Final_Chain (T, Empty);
1307
1308       --  Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1309       --  attributes
1310
1311       Set_Can_Never_Be_Null  (T, Null_Exclusion_Present (Def));
1312       Set_Is_Access_Constant (T, Constant_Present (Def));
1313    end Access_Type_Declaration;
1314
1315    ----------------------------------
1316    -- Add_Interface_Tag_Components --
1317    ----------------------------------
1318
1319    procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1320       Loc      : constant Source_Ptr := Sloc (N);
1321       L        : List_Id;
1322       Last_Tag : Node_Id;
1323
1324       procedure Add_Tag (Iface : Entity_Id);
1325       --  Add tag for one of the progenitor interfaces
1326
1327       -------------
1328       -- Add_Tag --
1329       -------------
1330
1331       procedure Add_Tag (Iface : Entity_Id) is
1332          Decl   : Node_Id;
1333          Def    : Node_Id;
1334          Tag    : Entity_Id;
1335          Offset : Entity_Id;
1336
1337       begin
1338          pragma Assert (Is_Tagged_Type (Iface)
1339            and then Is_Interface (Iface));
1340
1341          Def :=
1342            Make_Component_Definition (Loc,
1343              Aliased_Present    => True,
1344              Subtype_Indication =>
1345                New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1346
1347          Tag := Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1348
1349          Decl :=
1350            Make_Component_Declaration (Loc,
1351              Defining_Identifier  => Tag,
1352              Component_Definition => Def);
1353
1354          Analyze_Component_Declaration (Decl);
1355
1356          Set_Analyzed (Decl);
1357          Set_Ekind               (Tag, E_Component);
1358          Set_Is_Tag              (Tag);
1359          Set_Is_Aliased          (Tag);
1360          Set_Related_Type        (Tag, Iface);
1361          Init_Component_Location (Tag);
1362
1363          pragma Assert (Is_Frozen (Iface));
1364
1365          Set_DT_Entry_Count    (Tag,
1366            DT_Entry_Count (First_Entity (Iface)));
1367
1368          if No (Last_Tag) then
1369             Prepend (Decl, L);
1370          else
1371             Insert_After (Last_Tag, Decl);
1372          end if;
1373
1374          Last_Tag := Decl;
1375
1376          --  If the ancestor has discriminants we need to give special support
1377          --  to store the offset_to_top value of the secondary dispatch tables.
1378          --  For this purpose we add a supplementary component just after the
1379          --  field that contains the tag associated with each secondary DT.
1380
1381          if Typ /= Etype (Typ)
1382            and then Has_Discriminants (Etype (Typ))
1383          then
1384             Def :=
1385               Make_Component_Definition (Loc,
1386                 Subtype_Indication =>
1387                   New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1388
1389             Offset :=
1390               Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1391
1392             Decl :=
1393               Make_Component_Declaration (Loc,
1394                 Defining_Identifier  => Offset,
1395                 Component_Definition => Def);
1396
1397             Analyze_Component_Declaration (Decl);
1398
1399             Set_Analyzed (Decl);
1400             Set_Ekind               (Offset, E_Component);
1401             Set_Is_Aliased          (Offset);
1402             Set_Related_Type        (Offset, Iface);
1403             Init_Component_Location (Offset);
1404             Insert_After (Last_Tag, Decl);
1405             Last_Tag := Decl;
1406          end if;
1407       end Add_Tag;
1408
1409       --  Local variables
1410
1411       Elmt : Elmt_Id;
1412       Ext  : Node_Id;
1413       Comp : Node_Id;
1414
1415    --  Start of processing for Add_Interface_Tag_Components
1416
1417    begin
1418       if not RTE_Available (RE_Interface_Tag) then
1419          Error_Msg
1420            ("(Ada 2005) interface types not supported by this run-time!",
1421             Sloc (N));
1422          return;
1423       end if;
1424
1425       if Ekind (Typ) /= E_Record_Type
1426         or else (Is_Concurrent_Record_Type (Typ)
1427                   and then Is_Empty_List (Abstract_Interface_List (Typ)))
1428         or else (not Is_Concurrent_Record_Type (Typ)
1429                   and then No (Interfaces (Typ))
1430                   and then Is_Empty_Elmt_List (Interfaces (Typ)))
1431       then
1432          return;
1433       end if;
1434
1435       --  Find the current last tag
1436
1437       if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1438          Ext := Record_Extension_Part (Type_Definition (N));
1439       else
1440          pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1441          Ext := Type_Definition (N);
1442       end if;
1443
1444       Last_Tag := Empty;
1445
1446       if not (Present (Component_List (Ext))) then
1447          Set_Null_Present (Ext, False);
1448          L := New_List;
1449          Set_Component_List (Ext,
1450            Make_Component_List (Loc,
1451              Component_Items => L,
1452              Null_Present => False));
1453       else
1454          if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1455             L := Component_Items
1456                    (Component_List
1457                      (Record_Extension_Part
1458                        (Type_Definition (N))));
1459          else
1460             L := Component_Items
1461                    (Component_List
1462                      (Type_Definition (N)));
1463          end if;
1464
1465          --  Find the last tag component
1466
1467          Comp := First (L);
1468          while Present (Comp) loop
1469             if Nkind (Comp) = N_Component_Declaration
1470               and then Is_Tag (Defining_Identifier (Comp))
1471             then
1472                Last_Tag := Comp;
1473             end if;
1474
1475             Next (Comp);
1476          end loop;
1477       end if;
1478
1479       --  At this point L references the list of components and Last_Tag
1480       --  references the current last tag (if any). Now we add the tag
1481       --  corresponding with all the interfaces that are not implemented
1482       --  by the parent.
1483
1484       if Present (Interfaces (Typ)) then
1485          Elmt := First_Elmt (Interfaces (Typ));
1486          while Present (Elmt) loop
1487             Add_Tag (Node (Elmt));
1488             Next_Elmt (Elmt);
1489          end loop;
1490       end if;
1491    end Add_Interface_Tag_Components;
1492
1493    -----------------------------------
1494    -- Analyze_Component_Declaration --
1495    -----------------------------------
1496
1497    procedure Analyze_Component_Declaration (N : Node_Id) is
1498       Id : constant Entity_Id := Defining_Identifier (N);
1499       E  : constant Node_Id   := Expression (N);
1500       T  : Entity_Id;
1501       P  : Entity_Id;
1502
1503       function Contains_POC (Constr : Node_Id) return Boolean;
1504       --  Determines whether a constraint uses the discriminant of a record
1505       --  type thus becoming a per-object constraint (POC).
1506
1507       function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1508       --  Typ is the type of the current component, check whether this type is
1509       --  a limited type. Used to validate declaration against that of
1510       --  enclosing record.
1511
1512       ------------------
1513       -- Contains_POC --
1514       ------------------
1515
1516       function Contains_POC (Constr : Node_Id) return Boolean is
1517       begin
1518          --  Prevent cascaded errors
1519
1520          if Error_Posted (Constr) then
1521             return False;
1522          end if;
1523
1524          case Nkind (Constr) is
1525             when N_Attribute_Reference =>
1526                return
1527                  Attribute_Name (Constr) = Name_Access
1528                    and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1529
1530             when N_Discriminant_Association =>
1531                return Denotes_Discriminant (Expression (Constr));
1532
1533             when N_Identifier =>
1534                return Denotes_Discriminant (Constr);
1535
1536             when N_Index_Or_Discriminant_Constraint =>
1537                declare
1538                   IDC : Node_Id;
1539
1540                begin
1541                   IDC := First (Constraints (Constr));
1542                   while Present (IDC) loop
1543
1544                      --  One per-object constraint is sufficient
1545
1546                      if Contains_POC (IDC) then
1547                         return True;
1548                      end if;
1549
1550                      Next (IDC);
1551                   end loop;
1552
1553                   return False;
1554                end;
1555
1556             when N_Range =>
1557                return Denotes_Discriminant (Low_Bound (Constr))
1558                         or else
1559                       Denotes_Discriminant (High_Bound (Constr));
1560
1561             when N_Range_Constraint =>
1562                return Denotes_Discriminant (Range_Expression (Constr));
1563
1564             when others =>
1565                return False;
1566
1567          end case;
1568       end Contains_POC;
1569
1570       ----------------------
1571       -- Is_Known_Limited --
1572       ----------------------
1573
1574       function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1575          P : constant Entity_Id := Etype (Typ);
1576          R : constant Entity_Id := Root_Type (Typ);
1577
1578       begin
1579          if Is_Limited_Record (Typ) then
1580             return True;
1581
1582          --  If the root type is limited (and not a limited interface)
1583          --  so is the current type
1584
1585          elsif Is_Limited_Record (R)
1586            and then
1587              (not Is_Interface (R)
1588                or else not Is_Limited_Interface (R))
1589          then
1590             return True;
1591
1592          --  Else the type may have a limited interface progenitor, but a
1593          --  limited record parent.
1594
1595          elsif R /= P
1596            and then Is_Limited_Record (P)
1597          then
1598             return True;
1599
1600          else
1601             return False;
1602          end if;
1603       end Is_Known_Limited;
1604
1605    --  Start of processing for Analyze_Component_Declaration
1606
1607    begin
1608       Generate_Definition (Id);
1609       Enter_Name (Id);
1610
1611       if Present (Subtype_Indication (Component_Definition (N))) then
1612          T := Find_Type_Of_Object
1613                 (Subtype_Indication (Component_Definition (N)), N);
1614
1615       --  Ada 2005 (AI-230): Access Definition case
1616
1617       else
1618          pragma Assert (Present
1619                           (Access_Definition (Component_Definition (N))));
1620
1621          T := Access_Definition
1622                 (Related_Nod => N,
1623                  N => Access_Definition (Component_Definition (N)));
1624          Set_Is_Local_Anonymous_Access (T);
1625
1626          --  Ada 2005 (AI-254)
1627
1628          if Present (Access_To_Subprogram_Definition
1629                       (Access_Definition (Component_Definition (N))))
1630            and then Protected_Present (Access_To_Subprogram_Definition
1631                                         (Access_Definition
1632                                           (Component_Definition (N))))
1633          then
1634             T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1635          end if;
1636       end if;
1637
1638       --  If the subtype is a constrained subtype of the enclosing record,
1639       --  (which must have a partial view) the back-end does not properly
1640       --  handle the recursion. Rewrite the component declaration with an
1641       --  explicit subtype indication, which is acceptable to Gigi. We can copy
1642       --  the tree directly because side effects have already been removed from
1643       --  discriminant constraints.
1644
1645       if Ekind (T) = E_Access_Subtype
1646         and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1647         and then Comes_From_Source (T)
1648         and then Nkind (Parent (T)) = N_Subtype_Declaration
1649         and then Etype (Directly_Designated_Type (T)) = Current_Scope
1650       then
1651          Rewrite
1652            (Subtype_Indication (Component_Definition (N)),
1653              New_Copy_Tree (Subtype_Indication (Parent (T))));
1654          T := Find_Type_Of_Object
1655                  (Subtype_Indication (Component_Definition (N)), N);
1656       end if;
1657
1658       --  If the component declaration includes a default expression, then we
1659       --  check that the component is not of a limited type (RM 3.7(5)),
1660       --  and do the special preanalysis of the expression (see section on
1661       --  "Handling of Default and Per-Object Expressions" in the spec of
1662       --  package Sem).
1663
1664       if Present (E) then
1665          Preanalyze_Spec_Expression (E, T);
1666          Check_Initialization (T, E);
1667
1668          if Ada_Version >= Ada_05
1669            and then Ekind (T) = E_Anonymous_Access_Type
1670            and then Etype (E) /= Any_Type
1671          then
1672             --  Check RM 3.9.2(9): "if the expected type for an expression is
1673             --  an anonymous access-to-specific tagged type, then the object
1674             --  designated by the expression shall not be dynamically tagged
1675             --  unless it is a controlling operand in a call on a dispatching
1676             --  operation"
1677
1678             if Is_Tagged_Type (Directly_Designated_Type (T))
1679               and then
1680                 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1681               and then
1682                 Ekind (Directly_Designated_Type (Etype (E))) =
1683                   E_Class_Wide_Type
1684             then
1685                Error_Msg_N
1686                  ("access to specific tagged type required (RM 3.9.2(9))", E);
1687             end if;
1688
1689             --  (Ada 2005: AI-230): Accessibility check for anonymous
1690             --  components
1691
1692             if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1693                Error_Msg_N
1694                  ("expression has deeper access level than component " &
1695                   "(RM 3.10.2 (12.2))", E);
1696             end if;
1697
1698             --  The initialization expression is a reference to an access
1699             --  discriminant. The type of the discriminant is always deeper
1700             --  than any access type.
1701
1702             if Ekind (Etype (E)) = E_Anonymous_Access_Type
1703               and then Is_Entity_Name (E)
1704               and then Ekind (Entity (E)) = E_In_Parameter
1705               and then Present (Discriminal_Link (Entity (E)))
1706             then
1707                Error_Msg_N
1708                  ("discriminant has deeper accessibility level than target",
1709                   E);
1710             end if;
1711          end if;
1712       end if;
1713
1714       --  The parent type may be a private view with unknown discriminants,
1715       --  and thus unconstrained. Regular components must be constrained.
1716
1717       if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1718          if Is_Class_Wide_Type (T) then
1719             Error_Msg_N
1720                ("class-wide subtype with unknown discriminants" &
1721                  " in component declaration",
1722                  Subtype_Indication (Component_Definition (N)));
1723          else
1724             Error_Msg_N
1725               ("unconstrained subtype in component declaration",
1726                Subtype_Indication (Component_Definition (N)));
1727          end if;
1728
1729       --  Components cannot be abstract, except for the special case of
1730       --  the _Parent field (case of extending an abstract tagged type)
1731
1732       elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1733          Error_Msg_N ("type of a component cannot be abstract", N);
1734       end if;
1735
1736       Set_Etype (Id, T);
1737       Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1738
1739       --  The component declaration may have a per-object constraint, set
1740       --  the appropriate flag in the defining identifier of the subtype.
1741
1742       if Present (Subtype_Indication (Component_Definition (N))) then
1743          declare
1744             Sindic : constant Node_Id :=
1745                        Subtype_Indication (Component_Definition (N));
1746          begin
1747             if Nkind (Sindic) = N_Subtype_Indication
1748               and then Present (Constraint (Sindic))
1749               and then Contains_POC (Constraint (Sindic))
1750             then
1751                Set_Has_Per_Object_Constraint (Id);
1752             end if;
1753          end;
1754       end if;
1755
1756       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1757       --  out some static checks.
1758
1759       if Ada_Version >= Ada_05
1760         and then Can_Never_Be_Null (T)
1761       then
1762          Null_Exclusion_Static_Checks (N);
1763       end if;
1764
1765       --  If this component is private (or depends on a private type), flag the
1766       --  record type to indicate that some operations are not available.
1767
1768       P := Private_Component (T);
1769
1770       if Present (P) then
1771
1772          --  Check for circular definitions
1773
1774          if P = Any_Type then
1775             Set_Etype (Id, Any_Type);
1776
1777          --  There is a gap in the visibility of operations only if the
1778          --  component type is not defined in the scope of the record type.
1779
1780          elsif Scope (P) = Scope (Current_Scope) then
1781             null;
1782
1783          elsif Is_Limited_Type (P) then
1784             Set_Is_Limited_Composite (Current_Scope);
1785
1786          else
1787             Set_Is_Private_Composite (Current_Scope);
1788          end if;
1789       end if;
1790
1791       if P /= Any_Type
1792         and then Is_Limited_Type (T)
1793         and then Chars (Id) /= Name_uParent
1794         and then Is_Tagged_Type (Current_Scope)
1795       then
1796          if Is_Derived_Type (Current_Scope)
1797            and then not Is_Known_Limited (Current_Scope)
1798          then
1799             Error_Msg_N
1800               ("extension of nonlimited type cannot have limited components",
1801                N);
1802
1803             if Is_Interface (Root_Type (Current_Scope)) then
1804                Error_Msg_N
1805                  ("\limitedness is not inherited from limited interface", N);
1806                Error_Msg_N
1807                  ("\add LIMITED to type indication", N);
1808             end if;
1809
1810             Explain_Limited_Type (T, N);
1811             Set_Etype (Id, Any_Type);
1812             Set_Is_Limited_Composite (Current_Scope, False);
1813
1814          elsif not Is_Derived_Type (Current_Scope)
1815            and then not Is_Limited_Record (Current_Scope)
1816            and then not Is_Concurrent_Type (Current_Scope)
1817          then
1818             Error_Msg_N
1819               ("nonlimited tagged type cannot have limited components", N);
1820             Explain_Limited_Type (T, N);
1821             Set_Etype (Id, Any_Type);
1822             Set_Is_Limited_Composite (Current_Scope, False);
1823          end if;
1824       end if;
1825
1826       Set_Original_Record_Component (Id, Id);
1827    end Analyze_Component_Declaration;
1828
1829    --------------------------
1830    -- Analyze_Declarations --
1831    --------------------------
1832
1833    procedure Analyze_Declarations (L : List_Id) is
1834       D           : Node_Id;
1835       Freeze_From : Entity_Id := Empty;
1836       Next_Node   : Node_Id;
1837
1838       procedure Adjust_D;
1839       --  Adjust D not to include implicit label declarations, since these
1840       --  have strange Sloc values that result in elaboration check problems.
1841       --  (They have the sloc of the label as found in the source, and that
1842       --  is ahead of the current declarative part).
1843
1844       --------------
1845       -- Adjust_D --
1846       --------------
1847
1848       procedure Adjust_D is
1849       begin
1850          while Present (Prev (D))
1851            and then Nkind (D) = N_Implicit_Label_Declaration
1852          loop
1853             Prev (D);
1854          end loop;
1855       end Adjust_D;
1856
1857    --  Start of processing for Analyze_Declarations
1858
1859    begin
1860       D := First (L);
1861       while Present (D) loop
1862
1863          --  Complete analysis of declaration
1864
1865          Analyze (D);
1866          Next_Node := Next (D);
1867
1868          if No (Freeze_From) then
1869             Freeze_From := First_Entity (Current_Scope);
1870          end if;
1871
1872          --  At the end of a declarative part, freeze remaining entities
1873          --  declared in it. The end of the visible declarations of package
1874          --  specification is not the end of a declarative part if private
1875          --  declarations are present. The end of a package declaration is a
1876          --  freezing point only if it a library package. A task definition or
1877          --  protected type definition is not a freeze point either. Finally,
1878          --  we do not freeze entities in generic scopes, because there is no
1879          --  code generated for them and freeze nodes will be generated for
1880          --  the instance.
1881
1882          --  The end of a package instantiation is not a freeze point, but
1883          --  for now we make it one, because the generic body is inserted
1884          --  (currently) immediately after. Generic instantiations will not
1885          --  be a freeze point once delayed freezing of bodies is implemented.
1886          --  (This is needed in any case for early instantiations ???).
1887
1888          if No (Next_Node) then
1889             if Nkind_In (Parent (L), N_Component_List,
1890                                      N_Task_Definition,
1891                                      N_Protected_Definition)
1892             then
1893                null;
1894
1895             elsif Nkind (Parent (L)) /= N_Package_Specification then
1896                if Nkind (Parent (L)) = N_Package_Body then
1897                   Freeze_From := First_Entity (Current_Scope);
1898                end if;
1899
1900                Adjust_D;
1901                Freeze_All (Freeze_From, D);
1902                Freeze_From := Last_Entity (Current_Scope);
1903
1904             elsif Scope (Current_Scope) /= Standard_Standard
1905               and then not Is_Child_Unit (Current_Scope)
1906               and then No (Generic_Parent (Parent (L)))
1907             then
1908                null;
1909
1910             elsif L /= Visible_Declarations (Parent (L))
1911                or else No (Private_Declarations (Parent (L)))
1912                or else Is_Empty_List (Private_Declarations (Parent (L)))
1913             then
1914                Adjust_D;
1915                Freeze_All (Freeze_From, D);
1916                Freeze_From := Last_Entity (Current_Scope);
1917             end if;
1918
1919          --  If next node is a body then freeze all types before the body.
1920          --  An exception occurs for some expander-generated bodies. If these
1921          --  are generated at places where in general language rules would not
1922          --  allow a freeze point, then we assume that the expander has
1923          --  explicitly checked that all required types are properly frozen,
1924          --  and we do not cause general freezing here. This special circuit
1925          --  is used when the encountered body is marked as having already
1926          --  been analyzed.
1927
1928          --  In all other cases (bodies that come from source, and expander
1929          --  generated bodies that have not been analyzed yet), freeze all
1930          --  types now. Note that in the latter case, the expander must take
1931          --  care to attach the bodies at a proper place in the tree so as to
1932          --  not cause unwanted freezing at that point.
1933
1934          elsif not Analyzed (Next_Node)
1935            and then (Nkind_In (Next_Node, N_Subprogram_Body,
1936                                           N_Entry_Body,
1937                                           N_Package_Body,
1938                                           N_Protected_Body,
1939                                           N_Task_Body)
1940                        or else
1941                      Nkind (Next_Node) in N_Body_Stub)
1942          then
1943             Adjust_D;
1944             Freeze_All (Freeze_From, D);
1945             Freeze_From := Last_Entity (Current_Scope);
1946          end if;
1947
1948          D := Next_Node;
1949       end loop;
1950    end Analyze_Declarations;
1951
1952    ----------------------------------
1953    -- Analyze_Incomplete_Type_Decl --
1954    ----------------------------------
1955
1956    procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
1957       F : constant Boolean := Is_Pure (Current_Scope);
1958       T : Entity_Id;
1959
1960    begin
1961       Generate_Definition (Defining_Identifier (N));
1962
1963       --  Process an incomplete declaration. The identifier must not have been
1964       --  declared already in the scope. However, an incomplete declaration may
1965       --  appear in the private part of a package, for a private type that has
1966       --  already been declared.
1967
1968       --  In this case, the discriminants (if any) must match
1969
1970       T := Find_Type_Name (N);
1971
1972       Set_Ekind (T, E_Incomplete_Type);
1973       Init_Size_Align (T);
1974       Set_Is_First_Subtype (T, True);
1975       Set_Etype (T, T);
1976
1977       --  Ada 2005 (AI-326): Minimum decoration to give support to tagged
1978       --  incomplete types.
1979
1980       if Tagged_Present (N) then
1981          Set_Is_Tagged_Type (T);
1982          Make_Class_Wide_Type (T);
1983          Set_Primitive_Operations (T, New_Elmt_List);
1984       end if;
1985
1986       Push_Scope (T);
1987
1988       Set_Stored_Constraint (T, No_Elist);
1989
1990       if Present (Discriminant_Specifications (N)) then
1991          Process_Discriminants (N);
1992       end if;
1993
1994       End_Scope;
1995
1996       --  If the type has discriminants, non-trivial subtypes may be
1997       --  declared before the full view of the type. The full views of those
1998       --  subtypes will be built after the full view of the type.
1999
2000       Set_Private_Dependents (T, New_Elmt_List);
2001       Set_Is_Pure (T, F);
2002    end Analyze_Incomplete_Type_Decl;
2003
2004    -----------------------------------
2005    -- Analyze_Interface_Declaration --
2006    -----------------------------------
2007
2008    procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2009       CW : constant Entity_Id := Class_Wide_Type (T);
2010
2011    begin
2012       Set_Is_Tagged_Type (T);
2013
2014       Set_Is_Limited_Record (T, Limited_Present (Def)
2015                                   or else Task_Present (Def)
2016                                   or else Protected_Present (Def)
2017                                   or else Synchronized_Present (Def));
2018
2019       --  Type is abstract if full declaration carries keyword, or if previous
2020       --  partial view did.
2021
2022       Set_Is_Abstract_Type (T);
2023       Set_Is_Interface (T);
2024
2025       --  Type is a limited interface if it includes the keyword limited, task,
2026       --  protected, or synchronized.
2027
2028       Set_Is_Limited_Interface
2029         (T, Limited_Present (Def)
2030               or else Protected_Present (Def)
2031               or else Synchronized_Present (Def)
2032               or else Task_Present (Def));
2033
2034       Set_Is_Protected_Interface (T, Protected_Present (Def));
2035       Set_Is_Task_Interface (T, Task_Present (Def));
2036
2037       --  Type is a synchronized interface if it includes the keyword task,
2038       --  protected, or synchronized.
2039
2040       Set_Is_Synchronized_Interface
2041         (T, Synchronized_Present (Def)
2042               or else Protected_Present (Def)
2043               or else Task_Present (Def));
2044
2045       Set_Interfaces (T, New_Elmt_List);
2046       Set_Primitive_Operations (T, New_Elmt_List);
2047
2048       --  Complete the decoration of the class-wide entity if it was already
2049       --  built (i.e. during the creation of the limited view)
2050
2051       if Present (CW) then
2052          Set_Is_Interface (CW);
2053          Set_Is_Limited_Interface      (CW, Is_Limited_Interface (T));
2054          Set_Is_Protected_Interface    (CW, Is_Protected_Interface (T));
2055          Set_Is_Synchronized_Interface (CW, Is_Synchronized_Interface (T));
2056          Set_Is_Task_Interface         (CW, Is_Task_Interface (T));
2057       end if;
2058
2059       --  Check runtime support for synchronized interfaces
2060
2061       if VM_Target = No_VM
2062         and then (Is_Task_Interface (T)
2063                     or else Is_Protected_Interface (T)
2064                     or else Is_Synchronized_Interface (T))
2065         and then not RTE_Available (RE_Select_Specific_Data)
2066       then
2067          Error_Msg_CRT ("synchronized interfaces", T);
2068       end if;
2069    end Analyze_Interface_Declaration;
2070
2071    -----------------------------
2072    -- Analyze_Itype_Reference --
2073    -----------------------------
2074
2075    --  Nothing to do. This node is placed in the tree only for the benefit of
2076    --  back end processing, and has no effect on the semantic processing.
2077
2078    procedure Analyze_Itype_Reference (N : Node_Id) is
2079    begin
2080       pragma Assert (Is_Itype (Itype (N)));
2081       null;
2082    end Analyze_Itype_Reference;
2083
2084    --------------------------------
2085    -- Analyze_Number_Declaration --
2086    --------------------------------
2087
2088    procedure Analyze_Number_Declaration (N : Node_Id) is
2089       Id    : constant Entity_Id := Defining_Identifier (N);
2090       E     : constant Node_Id   := Expression (N);
2091       T     : Entity_Id;
2092       Index : Interp_Index;
2093       It    : Interp;
2094
2095    begin
2096       Generate_Definition (Id);
2097       Enter_Name (Id);
2098
2099       --  This is an optimization of a common case of an integer literal
2100
2101       if Nkind (E) = N_Integer_Literal then
2102          Set_Is_Static_Expression (E, True);
2103          Set_Etype                (E, Universal_Integer);
2104
2105          Set_Etype     (Id, Universal_Integer);
2106          Set_Ekind     (Id, E_Named_Integer);
2107          Set_Is_Frozen (Id, True);
2108          return;
2109       end if;
2110
2111       Set_Is_Pure (Id, Is_Pure (Current_Scope));
2112
2113       --  Process expression, replacing error by integer zero, to avoid
2114       --  cascaded errors or aborts further along in the processing
2115
2116       --  Replace Error by integer zero, which seems least likely to
2117       --  cause cascaded errors.
2118
2119       if E = Error then
2120          Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2121          Set_Error_Posted (E);
2122       end if;
2123
2124       Analyze (E);
2125
2126       --  Verify that the expression is static and numeric. If
2127       --  the expression is overloaded, we apply the preference
2128       --  rule that favors root numeric types.
2129
2130       if not Is_Overloaded (E) then
2131          T := Etype (E);
2132
2133       else
2134          T := Any_Type;
2135
2136          Get_First_Interp (E, Index, It);
2137          while Present (It.Typ) loop
2138             if (Is_Integer_Type (It.Typ)
2139                  or else Is_Real_Type (It.Typ))
2140               and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2141             then
2142                if T = Any_Type then
2143                   T := It.Typ;
2144
2145                elsif It.Typ = Universal_Real
2146                  or else It.Typ = Universal_Integer
2147                then
2148                   --  Choose universal interpretation over any other
2149
2150                   T := It.Typ;
2151                   exit;
2152                end if;
2153             end if;
2154
2155             Get_Next_Interp (Index, It);
2156          end loop;
2157       end if;
2158
2159       if Is_Integer_Type (T)  then
2160          Resolve (E, T);
2161          Set_Etype (Id, Universal_Integer);
2162          Set_Ekind (Id, E_Named_Integer);
2163
2164       elsif Is_Real_Type (T) then
2165
2166          --  Because the real value is converted to universal_real, this is a
2167          --  legal context for a universal fixed expression.
2168
2169          if T = Universal_Fixed then
2170             declare
2171                Loc  : constant Source_Ptr := Sloc (N);
2172                Conv : constant Node_Id := Make_Type_Conversion (Loc,
2173                         Subtype_Mark =>
2174                           New_Occurrence_Of (Universal_Real, Loc),
2175                         Expression => Relocate_Node (E));
2176
2177             begin
2178                Rewrite (E, Conv);
2179                Analyze (E);
2180             end;
2181
2182          elsif T = Any_Fixed then
2183             Error_Msg_N ("illegal context for mixed mode operation", E);
2184
2185             --  Expression is of the form : universal_fixed * integer. Try to
2186             --  resolve as universal_real.
2187
2188             T := Universal_Real;
2189             Set_Etype (E, T);
2190          end if;
2191
2192          Resolve (E, T);
2193          Set_Etype (Id, Universal_Real);
2194          Set_Ekind (Id, E_Named_Real);
2195
2196       else
2197          Wrong_Type (E, Any_Numeric);
2198          Resolve (E, T);
2199
2200          Set_Etype               (Id, T);
2201          Set_Ekind               (Id, E_Constant);
2202          Set_Never_Set_In_Source (Id, True);
2203          Set_Is_True_Constant    (Id, True);
2204          return;
2205       end if;
2206
2207       if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2208          Set_Etype (E, Etype (Id));
2209       end if;
2210
2211       if not Is_OK_Static_Expression (E) then
2212          Flag_Non_Static_Expr
2213            ("non-static expression used in number declaration!", E);
2214          Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2215          Set_Etype (E, Any_Type);
2216       end if;
2217    end Analyze_Number_Declaration;
2218
2219    --------------------------------
2220    -- Analyze_Object_Declaration --
2221    --------------------------------
2222
2223    procedure Analyze_Object_Declaration (N : Node_Id) is
2224       Loc   : constant Source_Ptr := Sloc (N);
2225       Id    : constant Entity_Id  := Defining_Identifier (N);
2226       T     : Entity_Id;
2227       Act_T : Entity_Id;
2228
2229       E : Node_Id := Expression (N);
2230       --  E is set to Expression (N) throughout this routine. When
2231       --  Expression (N) is modified, E is changed accordingly.
2232
2233       Prev_Entity : Entity_Id := Empty;
2234
2235       function Count_Tasks (T : Entity_Id) return Uint;
2236       --  This function is called when a non-generic library level object of a
2237       --  task type is declared. Its function is to count the static number of
2238       --  tasks declared within the type (it is only called if Has_Tasks is set
2239       --  for T). As a side effect, if an array of tasks with non-static bounds
2240       --  or a variant record type is encountered, Check_Restrictions is called
2241       --  indicating the count is unknown.
2242
2243       -----------------
2244       -- Count_Tasks --
2245       -----------------
2246
2247       function Count_Tasks (T : Entity_Id) return Uint is
2248          C : Entity_Id;
2249          X : Node_Id;
2250          V : Uint;
2251
2252       begin
2253          if Is_Task_Type (T) then
2254             return Uint_1;
2255
2256          elsif Is_Record_Type (T) then
2257             if Has_Discriminants (T) then
2258                Check_Restriction (Max_Tasks, N);
2259                return Uint_0;
2260
2261             else
2262                V := Uint_0;
2263                C := First_Component (T);
2264                while Present (C) loop
2265                   V := V + Count_Tasks (Etype (C));
2266                   Next_Component (C);
2267                end loop;
2268
2269                return V;
2270             end if;
2271
2272          elsif Is_Array_Type (T) then
2273             X := First_Index (T);
2274             V := Count_Tasks (Component_Type (T));
2275             while Present (X) loop
2276                C := Etype (X);
2277
2278                if not Is_Static_Subtype (C) then
2279                   Check_Restriction (Max_Tasks, N);
2280                   return Uint_0;
2281                else
2282                   V := V * (UI_Max (Uint_0,
2283                                     Expr_Value (Type_High_Bound (C)) -
2284                                     Expr_Value (Type_Low_Bound (C)) + Uint_1));
2285                end if;
2286
2287                Next_Index (X);
2288             end loop;
2289
2290             return V;
2291
2292          else
2293             return Uint_0;
2294          end if;
2295       end Count_Tasks;
2296
2297    --  Start of processing for Analyze_Object_Declaration
2298
2299    begin
2300       --  There are three kinds of implicit types generated by an
2301       --  object declaration:
2302
2303       --   1. Those for generated by the original Object Definition
2304
2305       --   2. Those generated by the Expression
2306
2307       --   3. Those used to constrained the Object Definition with the
2308       --       expression constraints when it is unconstrained
2309
2310       --  They must be generated in this order to avoid order of elaboration
2311       --  issues. Thus the first step (after entering the name) is to analyze
2312       --  the object definition.
2313
2314       if Constant_Present (N) then
2315          Prev_Entity := Current_Entity_In_Scope (Id);
2316
2317          --  If the homograph is an implicit subprogram, it is overridden by
2318          --  the current declaration.
2319
2320          if Present (Prev_Entity)
2321            and then
2322              ((Is_Overloadable (Prev_Entity)
2323                  and then Is_Inherited_Operation (Prev_Entity))
2324
2325                --  The current object is a discriminal generated for an entry
2326                --  family index. Even though the index is a constant, in this
2327                --  particular context there is no true constant redeclaration.
2328                --  Enter_Name will handle the visibility.
2329
2330                or else
2331                 (Is_Discriminal (Id)
2332                    and then Ekind (Discriminal_Link (Id)) =
2333                               E_Entry_Index_Parameter))
2334          then
2335             Prev_Entity := Empty;
2336          end if;
2337       end if;
2338
2339       if Present (Prev_Entity) then
2340          Constant_Redeclaration (Id, N, T);
2341
2342          Generate_Reference (Prev_Entity, Id, 'c');
2343          Set_Completion_Referenced (Id);
2344
2345          if Error_Posted (N) then
2346
2347             --  Type mismatch or illegal redeclaration, Do not analyze
2348             --  expression to avoid cascaded errors.
2349
2350             T := Find_Type_Of_Object (Object_Definition (N), N);
2351             Set_Etype (Id, T);
2352             Set_Ekind (Id, E_Variable);
2353             return;
2354          end if;
2355
2356       --  In the normal case, enter identifier at the start to catch premature
2357       --  usage in the initialization expression.
2358
2359       else
2360          Generate_Definition (Id);
2361          Enter_Name (Id);
2362
2363          Mark_Coextensions (N, Object_Definition (N));
2364
2365          T := Find_Type_Of_Object (Object_Definition (N), N);
2366
2367          if Nkind (Object_Definition (N)) = N_Access_Definition
2368            and then Present
2369              (Access_To_Subprogram_Definition (Object_Definition (N)))
2370            and then Protected_Present
2371              (Access_To_Subprogram_Definition (Object_Definition (N)))
2372          then
2373             T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2374          end if;
2375
2376          if Error_Posted (Id) then
2377             Set_Etype (Id, T);
2378             Set_Ekind (Id, E_Variable);
2379             return;
2380          end if;
2381       end if;
2382
2383       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2384       --  out some static checks
2385
2386       if Ada_Version >= Ada_05
2387         and then Can_Never_Be_Null (T)
2388       then
2389          --  In case of aggregates we must also take care of the correct
2390          --  initialization of nested aggregates bug this is done at the
2391          --  point of the analysis of the aggregate (see sem_aggr.adb)
2392
2393          if Present (Expression (N))
2394            and then Nkind (Expression (N)) = N_Aggregate
2395          then
2396             null;
2397
2398          else
2399             declare
2400                Save_Typ : constant Entity_Id := Etype (Id);
2401             begin
2402                Set_Etype (Id, T); --  Temp. decoration for static checks
2403                Null_Exclusion_Static_Checks (N);
2404                Set_Etype (Id, Save_Typ);
2405             end;
2406          end if;
2407       end if;
2408
2409       Set_Is_Pure (Id, Is_Pure (Current_Scope));
2410
2411       --  If deferred constant, make sure context is appropriate. We detect
2412       --  a deferred constant as a constant declaration with no expression.
2413       --  A deferred constant can appear in a package body if its completion
2414       --  is by means of an interface pragma.
2415
2416       if Constant_Present (N)
2417         and then No (E)
2418       then
2419          --  We exclude forward references to tags
2420
2421          if Is_Imported (Defining_Identifier (N))
2422            and then
2423              (T = RTE (RE_Tag)
2424                or else
2425                  (Present (Full_View (T))
2426                    and then Full_View (T) = RTE (RE_Tag)))
2427          then
2428             null;
2429
2430          --  A deferred constant may appear in the declarative part of the
2431          --  following constructs:
2432
2433          --     blocks
2434          --     entry bodies
2435          --     extended return statements
2436          --     package specs
2437          --     package bodies
2438          --     subprogram bodies
2439          --     task bodies
2440
2441          --  When declared inside a package spec, a deferred constant must be
2442          --  completed by a full constant declaration or pragma Import. In all
2443          --  other cases, the only proper completion is pragma Import. Extended
2444          --  return statements are flagged as invalid contexts because they do
2445          --  not have a declarative part and so cannot accommodate the pragma.
2446
2447          elsif Ekind (Current_Scope) = E_Return_Statement then
2448             Error_Msg_N
2449               ("invalid context for deferred constant declaration (RM 7.4)",
2450                N);
2451             Error_Msg_N
2452               ("\declaration requires an initialization expression",
2453                 N);
2454             Set_Constant_Present (N, False);
2455
2456          --  In Ada 83, deferred constant must be of private type
2457
2458          elsif not Is_Private_Type (T) then
2459             if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2460                Error_Msg_N
2461                  ("(Ada 83) deferred constant must be private type", N);
2462             end if;
2463          end if;
2464
2465       --  If not a deferred constant, then object declaration freezes its type
2466
2467       else
2468          Check_Fully_Declared (T, N);
2469          Freeze_Before (N, T);
2470       end if;
2471
2472       --  If the object was created by a constrained array definition, then
2473       --  set the link in both the anonymous base type and anonymous subtype
2474       --  that are built to represent the array type to point to the object.
2475
2476       if Nkind (Object_Definition (Declaration_Node (Id))) =
2477                         N_Constrained_Array_Definition
2478       then
2479          Set_Related_Array_Object (T, Id);
2480          Set_Related_Array_Object (Base_Type (T), Id);
2481       end if;
2482
2483       --  Special checks for protected objects not at library level
2484
2485       if Is_Protected_Type (T)
2486         and then not Is_Library_Level_Entity (Id)
2487       then
2488          Check_Restriction (No_Local_Protected_Objects, Id);
2489
2490          --  Protected objects with interrupt handlers must be at library level
2491
2492          --  Ada 2005: this test is not needed (and the corresponding clause
2493          --  in the RM is removed) because accessibility checks are sufficient
2494          --  to make handlers not at the library level illegal.
2495
2496          if Has_Interrupt_Handler (T)
2497            and then Ada_Version < Ada_05
2498          then
2499             Error_Msg_N
2500               ("interrupt object can only be declared at library level", Id);
2501          end if;
2502       end if;
2503
2504       --  The actual subtype of the object is the nominal subtype, unless
2505       --  the nominal one is unconstrained and obtained from the expression.
2506
2507       Act_T := T;
2508
2509       --  Process initialization expression if present and not in error
2510
2511       if Present (E) and then E /= Error then
2512
2513          --  Generate an error in case of CPP class-wide object initialization.
2514          --  Required because otherwise the expansion of the class-wide
2515          --  assignment would try to use 'size to initialize the object
2516          --  (primitive that is not available in CPP tagged types).
2517
2518          if Is_Class_Wide_Type (Act_T)
2519            and then
2520              (Is_CPP_Class (Root_Type (Etype (Act_T)))
2521                or else
2522                  (Present (Full_View (Root_Type (Etype (Act_T))))
2523                     and then
2524                       Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
2525          then
2526             Error_Msg_N
2527               ("predefined assignment not available for 'C'P'P tagged types",
2528                E);
2529          end if;
2530
2531          Mark_Coextensions (N, E);
2532          Analyze (E);
2533
2534          --  In case of errors detected in the analysis of the expression,
2535          --  decorate it with the expected type to avoid cascaded errors
2536
2537          if No (Etype (E)) then
2538             Set_Etype (E, T);
2539          end if;
2540
2541          --  If an initialization expression is present, then we set the
2542          --  Is_True_Constant flag. It will be reset if this is a variable
2543          --  and it is indeed modified.
2544
2545          Set_Is_True_Constant (Id, True);
2546
2547          --  If we are analyzing a constant declaration, set its completion
2548          --  flag after analyzing and resolving the expression.
2549
2550          if Constant_Present (N) then
2551             Set_Has_Completion (Id);
2552          end if;
2553
2554          --  Set type and resolve (type may be overridden later on)
2555
2556          Set_Etype (Id, T);
2557          Resolve (E, T);
2558
2559          --  If E is null and has been replaced by an N_Raise_Constraint_Error
2560          --  node (which was marked already-analyzed), we need to set the type
2561          --  to something other than Any_Access in order to keep gigi happy.
2562
2563          if Etype (E) = Any_Access then
2564             Set_Etype (E, T);
2565          end if;
2566
2567          --  If the object is an access to variable, the initialization
2568          --  expression cannot be an access to constant.
2569
2570          if Is_Access_Type (T)
2571            and then not Is_Access_Constant (T)
2572            and then Is_Access_Type (Etype (E))
2573            and then Is_Access_Constant (Etype (E))
2574          then
2575             Error_Msg_N
2576               ("access to variable cannot be initialized " &
2577                 "with an access-to-constant expression", E);
2578          end if;
2579
2580          if not Assignment_OK (N) then
2581             Check_Initialization (T, E);
2582          end if;
2583
2584          Check_Unset_Reference (E);
2585
2586          --  If this is a variable, then set current value
2587
2588          if not Constant_Present (N) then
2589             if Compile_Time_Known_Value (E) then
2590                Set_Current_Value (Id, E);
2591             end if;
2592          end if;
2593
2594          --  Deal with setting of null flags
2595
2596          if Is_Access_Type (T) then
2597             if Known_Non_Null (E) then
2598                Set_Is_Known_Non_Null (Id, True);
2599             elsif Known_Null (E)
2600               and then not Can_Never_Be_Null (Id)
2601             then
2602                Set_Is_Known_Null (Id, True);
2603             end if;
2604          end if;
2605
2606          --  Check incorrect use of dynamically tagged expressions. Note
2607          --  the use of Is_Tagged_Type (T) which seems redundant but is in
2608          --  fact important to avoid spurious errors due to expanded code
2609          --  for dispatching functions over an anonymous access type
2610
2611          if (Is_Class_Wide_Type (Etype (E)) or else Is_Dynamically_Tagged (E))
2612            and then Is_Tagged_Type (T)
2613            and then not Is_Class_Wide_Type (T)
2614          then
2615             Error_Msg_N ("dynamically tagged expression not allowed!", E);
2616          end if;
2617
2618          Apply_Scalar_Range_Check (E, T);
2619          Apply_Static_Length_Check (E, T);
2620       end if;
2621
2622       --  If the No_Streams restriction is set, check that the type of the
2623       --  object is not, and does not contain, any subtype derived from
2624       --  Ada.Streams.Root_Stream_Type. Note that we guard the call to
2625       --  Has_Stream just for efficiency reasons. There is no point in
2626       --  spending time on a Has_Stream check if the restriction is not set.
2627
2628       if Restrictions.Set (No_Streams) then
2629          if Has_Stream (T) then
2630             Check_Restriction (No_Streams, N);
2631          end if;
2632       end if;
2633
2634       --  Abstract type is never permitted for a variable or constant.
2635       --  Note: we inhibit this check for objects that do not come from
2636       --  source because there is at least one case (the expansion of
2637       --  x'class'input where x is abstract) where we legitimately
2638       --  generate an abstract object.
2639
2640       if Is_Abstract_Type (T) and then Comes_From_Source (N) then
2641          Error_Msg_N ("type of object cannot be abstract",
2642                       Object_Definition (N));
2643
2644          if Is_CPP_Class (T) then
2645             Error_Msg_NE ("\} may need a cpp_constructor",
2646               Object_Definition (N), T);
2647          end if;
2648
2649       --  Case of unconstrained type
2650
2651       elsif Is_Indefinite_Subtype (T) then
2652
2653          --  Nothing to do in deferred constant case
2654
2655          if Constant_Present (N) and then No (E) then
2656             null;
2657
2658          --  Case of no initialization present
2659
2660          elsif No (E) then
2661             if No_Initialization (N) then
2662                null;
2663
2664             elsif Is_Class_Wide_Type (T) then
2665                Error_Msg_N
2666                  ("initialization required in class-wide declaration ", N);
2667
2668             else
2669                Error_Msg_N
2670                  ("unconstrained subtype not allowed (need initialization)",
2671                   Object_Definition (N));
2672
2673                if Is_Record_Type (T) and then Has_Discriminants (T) then
2674                   Error_Msg_N
2675                     ("\provide initial value or explicit discriminant values",
2676                      Object_Definition (N));
2677
2678                   Error_Msg_NE
2679                     ("\or give default discriminant values for type&",
2680                      Object_Definition (N), T);
2681
2682                elsif Is_Array_Type (T) then
2683                   Error_Msg_N
2684                     ("\provide initial value or explicit array bounds",
2685                      Object_Definition (N));
2686                end if;
2687             end if;
2688
2689          --  Case of initialization present but in error. Set initial
2690          --  expression as absent (but do not make above complaints)
2691
2692          elsif E = Error then
2693             Set_Expression (N, Empty);
2694             E := Empty;
2695
2696          --  Case of initialization present
2697
2698          else
2699             --  Not allowed in Ada 83
2700
2701             if not Constant_Present (N) then
2702                if Ada_Version = Ada_83
2703                  and then Comes_From_Source (Object_Definition (N))
2704                then
2705                   Error_Msg_N
2706                     ("(Ada 83) unconstrained variable not allowed",
2707                      Object_Definition (N));
2708                end if;
2709             end if;
2710
2711             --  Now we constrain the variable from the initializing expression
2712
2713             --  If the expression is an aggregate, it has been expanded into
2714             --  individual assignments. Retrieve the actual type from the
2715             --  expanded construct.
2716
2717             if Is_Array_Type (T)
2718               and then No_Initialization (N)
2719               and then Nkind (Original_Node (E)) = N_Aggregate
2720             then
2721                Act_T := Etype (E);
2722
2723             else
2724                Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
2725                Act_T := Find_Type_Of_Object (Object_Definition (N), N);
2726             end if;
2727
2728             Set_Is_Constr_Subt_For_U_Nominal (Act_T);
2729
2730             if Aliased_Present (N) then
2731                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2732             end if;
2733
2734             Freeze_Before (N, Act_T);
2735             Freeze_Before (N, T);
2736          end if;
2737
2738       elsif Is_Array_Type (T)
2739         and then No_Initialization (N)
2740         and then Nkind (Original_Node (E)) = N_Aggregate
2741       then
2742          if not Is_Entity_Name (Object_Definition (N)) then
2743             Act_T := Etype (E);
2744             Check_Compile_Time_Size (Act_T);
2745
2746             if Aliased_Present (N) then
2747                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2748             end if;
2749          end if;
2750
2751          --  When the given object definition and the aggregate are specified
2752          --  independently, and their lengths might differ do a length check.
2753          --  This cannot happen if the aggregate is of the form (others =>...)
2754
2755          if not Is_Constrained (T) then
2756             null;
2757
2758          elsif Nkind (E) = N_Raise_Constraint_Error then
2759
2760             --  Aggregate is statically illegal. Place back in declaration
2761
2762             Set_Expression (N, E);
2763             Set_No_Initialization (N, False);
2764
2765          elsif T = Etype (E) then
2766             null;
2767
2768          elsif Nkind (E) = N_Aggregate
2769            and then Present (Component_Associations (E))
2770            and then Present (Choices (First (Component_Associations (E))))
2771            and then Nkind (First
2772             (Choices (First (Component_Associations (E))))) = N_Others_Choice
2773          then
2774             null;
2775
2776          else
2777             Apply_Length_Check (E, T);
2778          end if;
2779
2780       --  If the type is limited unconstrained with defaulted discriminants
2781       --  and there is no expression, then the object is constrained by the
2782       --  defaults, so it is worthwhile building the corresponding subtype.
2783
2784       elsif (Is_Limited_Record (T)
2785                or else Is_Concurrent_Type (T))
2786         and then not Is_Constrained (T)
2787         and then Has_Discriminants (T)
2788       then
2789          if No (E) then
2790             Act_T := Build_Default_Subtype (T, N);
2791          else
2792             --  Ada 2005:  a limited object may be initialized by means of an
2793             --  aggregate. If the type has default discriminants it has an
2794             --  unconstrained nominal type, Its actual subtype will be obtained
2795             --  from the aggregate, and not from the default discriminants.
2796
2797             Act_T := Etype (E);
2798          end if;
2799
2800          Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
2801
2802       elsif Present (Underlying_Type (T))
2803         and then not Is_Constrained (Underlying_Type (T))
2804         and then Has_Discriminants (Underlying_Type (T))
2805         and then Nkind (E) = N_Function_Call
2806         and then Constant_Present (N)
2807       then
2808          --  The back-end has problems with constants of a discriminated type
2809          --  with defaults, if the initial value is a function call. We
2810          --  generate an intermediate temporary for the result of the call.
2811          --  It is unclear why this should make it acceptable to gcc. ???
2812
2813          Remove_Side_Effects (E);
2814       end if;
2815
2816       --  Check No_Wide_Characters restriction
2817
2818       if T = Standard_Wide_Character
2819         or else T = Standard_Wide_Wide_Character
2820         or else Root_Type (T) = Standard_Wide_String
2821         or else Root_Type (T) = Standard_Wide_Wide_String
2822       then
2823          Check_Restriction (No_Wide_Characters, Object_Definition (N));
2824       end if;
2825
2826       --  Indicate this is not set in source. Certainly true for constants,
2827       --  and true for variables so far (will be reset for a variable if and
2828       --  when we encounter a modification in the source).
2829
2830       Set_Never_Set_In_Source (Id, True);
2831
2832       --  Now establish the proper kind and type of the object
2833
2834       if Constant_Present (N) then
2835          Set_Ekind            (Id, E_Constant);
2836          Set_Is_True_Constant (Id, True);
2837
2838       else
2839          Set_Ekind (Id, E_Variable);
2840
2841          --  A variable is set as shared passive if it appears in a shared
2842          --  passive package, and is at the outer level. This is not done
2843          --  for entities generated during expansion, because those are
2844          --  always manipulated locally.
2845
2846          if Is_Shared_Passive (Current_Scope)
2847            and then Is_Library_Level_Entity (Id)
2848            and then Comes_From_Source (Id)
2849          then
2850             Set_Is_Shared_Passive (Id);
2851             Check_Shared_Var (Id, T, N);
2852          end if;
2853
2854          --  Set Has_Initial_Value if initializing expression present. Note
2855          --  that if there is no initializing expression, we leave the state
2856          --  of this flag unchanged (usually it will be False, but notably in
2857          --  the case of exception choice variables, it will already be true).
2858
2859          if Present (E) then
2860             Set_Has_Initial_Value (Id, True);
2861          end if;
2862       end if;
2863
2864       --  Initialize alignment and size and capture alignment setting
2865
2866       Init_Alignment               (Id);
2867       Init_Esize                   (Id);
2868       Set_Optimize_Alignment_Flags (Id);
2869
2870       --  Deal with aliased case
2871
2872       if Aliased_Present (N) then
2873          Set_Is_Aliased (Id);
2874
2875          --  If the object is aliased and the type is unconstrained with
2876          --  defaulted discriminants and there is no expression, then the
2877          --  object is constrained by the defaults, so it is worthwhile
2878          --  building the corresponding subtype.
2879
2880          --  Ada 2005 (AI-363): If the aliased object is discriminated and
2881          --  unconstrained, then only establish an actual subtype if the
2882          --  nominal subtype is indefinite. In definite cases the object is
2883          --  unconstrained in Ada 2005.
2884
2885          if No (E)
2886            and then Is_Record_Type (T)
2887            and then not Is_Constrained (T)
2888            and then Has_Discriminants (T)
2889            and then (Ada_Version < Ada_05 or else Is_Indefinite_Subtype (T))
2890          then
2891             Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
2892          end if;
2893       end if;
2894
2895       --  Now we can set the type of the object
2896
2897       Set_Etype (Id, Act_T);
2898
2899       --  Deal with controlled types
2900
2901       if Has_Controlled_Component (Etype (Id))
2902         or else Is_Controlled (Etype (Id))
2903       then
2904          if not Is_Library_Level_Entity (Id) then
2905             Check_Restriction (No_Nested_Finalization, N);
2906          else
2907             Validate_Controlled_Object (Id);
2908          end if;
2909
2910          --  Generate a warning when an initialization causes an obvious ABE
2911          --  violation. If the init expression is a simple aggregate there
2912          --  shouldn't be any initialize/adjust call generated. This will be
2913          --  true as soon as aggregates are built in place when possible.
2914
2915          --  ??? at the moment we do not generate warnings for temporaries
2916          --  created for those aggregates although Program_Error might be
2917          --  generated if compiled with -gnato.
2918
2919          if Is_Controlled (Etype (Id))
2920             and then Comes_From_Source (Id)
2921          then
2922             declare
2923                BT : constant Entity_Id := Base_Type (Etype (Id));
2924
2925                Implicit_Call : Entity_Id;
2926                pragma Warnings (Off, Implicit_Call);
2927                --  ??? what is this for (never referenced!)
2928
2929                function Is_Aggr (N : Node_Id) return Boolean;
2930                --  Check that N is an aggregate
2931
2932                -------------
2933                -- Is_Aggr --
2934                -------------
2935
2936                function Is_Aggr (N : Node_Id) return Boolean is
2937                begin
2938                   case Nkind (Original_Node (N)) is
2939                      when N_Aggregate | N_Extension_Aggregate =>
2940                         return True;
2941
2942                      when N_Qualified_Expression |
2943                           N_Type_Conversion      |
2944                           N_Unchecked_Type_Conversion =>
2945                         return Is_Aggr (Expression (Original_Node (N)));
2946
2947                      when others =>
2948                         return False;
2949                   end case;
2950                end Is_Aggr;
2951
2952             begin
2953                --  If no underlying type, we already are in an error situation.
2954                --  Do not try to add a warning since we do not have access to
2955                --  prim-op list.
2956
2957                if No (Underlying_Type (BT)) then
2958                   Implicit_Call := Empty;
2959
2960                --  A generic type does not have usable primitive operators.
2961                --  Initialization calls are built for instances.
2962
2963                elsif Is_Generic_Type (BT) then
2964                   Implicit_Call := Empty;
2965
2966                --  If the init expression is not an aggregate, an adjust call
2967                --  will be generated
2968
2969                elsif Present (E) and then not Is_Aggr (E) then
2970                   Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
2971
2972                --  If no init expression and we are not in the deferred
2973                --  constant case, an Initialize call will be generated
2974
2975                elsif No (E) and then not Constant_Present (N) then
2976                   Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
2977
2978                else
2979                   Implicit_Call := Empty;
2980                end if;
2981             end;
2982          end if;
2983       end if;
2984
2985       if Has_Task (Etype (Id)) then
2986          Check_Restriction (No_Tasking, N);
2987
2988          --  Deal with counting max tasks
2989
2990          --  Nothing to do if inside a generic
2991
2992          if Inside_A_Generic then
2993             null;
2994
2995          --  If library level entity, then count tasks
2996
2997          elsif Is_Library_Level_Entity (Id) then
2998             Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
2999
3000          --  If not library level entity, then indicate we don't know max
3001          --  tasks and also check task hierarchy restriction and blocking
3002          --  operation (since starting a task is definitely blocking!)
3003
3004          else
3005             Check_Restriction (Max_Tasks, N);
3006             Check_Restriction (No_Task_Hierarchy, N);
3007             Check_Potentially_Blocking_Operation (N);
3008          end if;
3009
3010          --  A rather specialized test. If we see two tasks being declared
3011          --  of the same type in the same object declaration, and the task
3012          --  has an entry with an address clause, we know that program error
3013          --  will be raised at run-time since we can't have two tasks with
3014          --  entries at the same address.
3015
3016          if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
3017             declare
3018                E : Entity_Id;
3019
3020             begin
3021                E := First_Entity (Etype (Id));
3022                while Present (E) loop
3023                   if Ekind (E) = E_Entry
3024                     and then Present (Get_Attribute_Definition_Clause
3025                                         (E, Attribute_Address))
3026                   then
3027                      Error_Msg_N
3028                        ("?more than one task with same entry address", N);
3029                      Error_Msg_N
3030                        ("\?Program_Error will be raised at run time", N);
3031                      Insert_Action (N,
3032                        Make_Raise_Program_Error (Loc,
3033                          Reason => PE_Duplicated_Entry_Address));
3034                      exit;
3035                   end if;
3036
3037                   Next_Entity (E);
3038                end loop;
3039             end;
3040          end if;
3041       end if;
3042
3043       --  Some simple constant-propagation: if the expression is a constant
3044       --  string initialized with a literal, share the literal. This avoids
3045       --  a run-time copy.
3046
3047       if Present (E)
3048         and then Is_Entity_Name (E)
3049         and then Ekind (Entity (E)) = E_Constant
3050         and then Base_Type (Etype (E)) = Standard_String
3051       then
3052          declare
3053             Val : constant Node_Id := Constant_Value (Entity (E));
3054          begin
3055             if Present (Val)
3056               and then Nkind (Val) = N_String_Literal
3057             then
3058                Rewrite (E, New_Copy (Val));
3059             end if;
3060          end;
3061       end if;
3062
3063       --  Another optimization: if the nominal subtype is unconstrained and
3064       --  the expression is a function call that returns an unconstrained
3065       --  type, rewrite the declaration as a renaming of the result of the
3066       --  call. The exceptions below are cases where the copy is expected,
3067       --  either by the back end (Aliased case) or by the semantics, as for
3068       --  initializing controlled types or copying tags for classwide types.
3069
3070       if Present (E)
3071         and then Nkind (E) = N_Explicit_Dereference
3072         and then Nkind (Original_Node (E)) = N_Function_Call
3073         and then not Is_Library_Level_Entity (Id)
3074         and then not Is_Constrained (Underlying_Type (T))
3075         and then not Is_Aliased (Id)
3076         and then not Is_Class_Wide_Type (T)
3077         and then not Is_Controlled (T)
3078         and then not Has_Controlled_Component (Base_Type (T))
3079         and then Expander_Active
3080       then
3081          Rewrite (N,
3082            Make_Object_Renaming_Declaration (Loc,
3083              Defining_Identifier => Id,
3084              Access_Definition   => Empty,
3085              Subtype_Mark        => New_Occurrence_Of
3086                                       (Base_Type (Etype (Id)), Loc),
3087              Name                => E));
3088
3089          Set_Renamed_Object (Id, E);
3090
3091          --  Force generation of debugging information for the constant and for
3092          --  the renamed function call.
3093
3094          Set_Debug_Info_Needed (Id);
3095          Set_Debug_Info_Needed (Entity (Prefix (E)));
3096       end if;
3097
3098       if Present (Prev_Entity)
3099         and then Is_Frozen (Prev_Entity)
3100         and then not Error_Posted (Id)
3101       then
3102          Error_Msg_N ("full constant declaration appears too late", N);
3103       end if;
3104
3105       Check_Eliminated (Id);
3106
3107       --  Deal with setting In_Private_Part flag if in private part
3108
3109       if Ekind (Scope (Id)) = E_Package
3110         and then In_Private_Part (Scope (Id))
3111       then
3112          Set_In_Private_Part (Id);
3113       end if;
3114
3115       --  Check for violation of No_Local_Timing_Events
3116
3117       if Is_RTE (Etype (Id), RE_Timing_Event)
3118         and then not Is_Library_Level_Entity (Id)
3119       then
3120          Check_Restriction (No_Local_Timing_Events, N);
3121       end if;
3122    end Analyze_Object_Declaration;
3123
3124    ---------------------------
3125    -- Analyze_Others_Choice --
3126    ---------------------------
3127
3128    --  Nothing to do for the others choice node itself, the semantic analysis
3129    --  of the others choice will occur as part of the processing of the parent
3130
3131    procedure Analyze_Others_Choice (N : Node_Id) is
3132       pragma Warnings (Off, N);
3133    begin
3134       null;
3135    end Analyze_Others_Choice;
3136
3137    -------------------------------------------
3138    -- Analyze_Private_Extension_Declaration --
3139    -------------------------------------------
3140
3141    procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3142       T           : constant Entity_Id := Defining_Identifier (N);
3143       Indic       : constant Node_Id   := Subtype_Indication (N);
3144       Parent_Type : Entity_Id;
3145       Parent_Base : Entity_Id;
3146
3147    begin
3148       --  Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3149
3150       if Is_Non_Empty_List (Interface_List (N)) then
3151          declare
3152             Intf : Node_Id;
3153             T    : Entity_Id;
3154
3155          begin
3156             Intf := First (Interface_List (N));
3157             while Present (Intf) loop
3158                T := Find_Type_Of_Subtype_Indic (Intf);
3159
3160                Diagnose_Interface (Intf, T);
3161                Next (Intf);
3162             end loop;
3163          end;
3164       end if;
3165
3166       Generate_Definition (T);
3167       Enter_Name (T);
3168
3169       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3170       Parent_Base := Base_Type (Parent_Type);
3171
3172       if Parent_Type = Any_Type
3173         or else Etype (Parent_Type) = Any_Type
3174       then
3175          Set_Ekind (T, Ekind (Parent_Type));
3176          Set_Etype (T, Any_Type);
3177          return;
3178
3179       elsif not Is_Tagged_Type (Parent_Type) then
3180          Error_Msg_N
3181            ("parent of type extension must be a tagged type ", Indic);
3182          return;
3183
3184       elsif Ekind (Parent_Type) = E_Void
3185         or else Ekind (Parent_Type) = E_Incomplete_Type
3186       then
3187          Error_Msg_N ("premature derivation of incomplete type", Indic);
3188          return;
3189
3190       elsif Is_Concurrent_Type (Parent_Type) then
3191          Error_Msg_N
3192            ("parent type of a private extension cannot be "
3193             & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3194
3195          Set_Etype              (T, Any_Type);
3196          Set_Ekind              (T, E_Limited_Private_Type);
3197          Set_Private_Dependents (T, New_Elmt_List);
3198          Set_Error_Posted       (T);
3199          return;
3200       end if;
3201
3202       --  Perhaps the parent type should be changed to the class-wide type's
3203       --  specific type in this case to prevent cascading errors ???
3204
3205       if Is_Class_Wide_Type (Parent_Type) then
3206          Error_Msg_N
3207            ("parent of type extension must not be a class-wide type", Indic);
3208          return;
3209       end if;
3210
3211       if (not Is_Package_Or_Generic_Package (Current_Scope)
3212            and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3213         or else In_Private_Part (Current_Scope)
3214
3215       then
3216          Error_Msg_N ("invalid context for private extension", N);
3217       end if;
3218
3219       --  Set common attributes
3220
3221       Set_Is_Pure          (T, Is_Pure (Current_Scope));
3222       Set_Scope            (T, Current_Scope);
3223       Set_Ekind            (T, E_Record_Type_With_Private);
3224       Init_Size_Align      (T);
3225
3226       Set_Etype            (T,            Parent_Base);
3227       Set_Has_Task         (T, Has_Task  (Parent_Base));
3228
3229       Set_Convention       (T, Convention     (Parent_Type));
3230       Set_First_Rep_Item   (T, First_Rep_Item (Parent_Type));
3231       Set_Is_First_Subtype (T);
3232       Make_Class_Wide_Type (T);
3233
3234       if Unknown_Discriminants_Present (N) then
3235          Set_Discriminant_Constraint (T, No_Elist);
3236       end if;
3237
3238       Build_Derived_Record_Type (N, Parent_Type, T);
3239
3240       --  Ada 2005 (AI-443): Synchronized private extension or a rewritten
3241       --  synchronized formal derived type.
3242
3243       if Ada_Version >= Ada_05
3244         and then Synchronized_Present (N)
3245       then
3246          Set_Is_Limited_Record (T);
3247
3248          --  Formal derived type case
3249
3250          if Is_Generic_Type (T) then
3251
3252             --  The parent must be a tagged limited type or a synchronized
3253             --  interface.
3254
3255             if (not Is_Tagged_Type (Parent_Type)
3256                   or else not Is_Limited_Type (Parent_Type))
3257               and then
3258                (not Is_Interface (Parent_Type)
3259                   or else not Is_Synchronized_Interface (Parent_Type))
3260             then
3261                Error_Msg_NE ("parent type of & must be tagged limited " &
3262                              "or synchronized", N, T);
3263             end if;
3264
3265             --  The progenitors (if any) must be limited or synchronized
3266             --  interfaces.
3267
3268             if Present (Interfaces (T)) then
3269                declare
3270                   Iface      : Entity_Id;
3271                   Iface_Elmt : Elmt_Id;
3272
3273                begin
3274                   Iface_Elmt := First_Elmt (Interfaces (T));
3275                   while Present (Iface_Elmt) loop
3276                      Iface := Node (Iface_Elmt);
3277
3278                      if not Is_Limited_Interface (Iface)
3279                        and then not Is_Synchronized_Interface (Iface)
3280                      then
3281                         Error_Msg_NE ("progenitor & must be limited " &
3282                                       "or synchronized", N, Iface);
3283                      end if;
3284
3285                      Next_Elmt (Iface_Elmt);
3286                   end loop;
3287                end;
3288             end if;
3289
3290          --  Regular derived extension, the parent must be a limited or
3291          --  synchronized interface.
3292
3293          else
3294             if not Is_Interface (Parent_Type)
3295               or else (not Is_Limited_Interface (Parent_Type)
3296                          and then
3297                        not Is_Synchronized_Interface (Parent_Type))
3298             then
3299                Error_Msg_NE
3300                  ("parent type of & must be limited interface", N, T);
3301             end if;
3302          end if;
3303
3304       elsif Limited_Present (N) then
3305          Set_Is_Limited_Record (T);
3306
3307          if not Is_Limited_Type (Parent_Type)
3308            and then
3309              (not Is_Interface (Parent_Type)
3310                or else not Is_Limited_Interface (Parent_Type))
3311          then
3312             Error_Msg_NE ("parent type& of limited extension must be limited",
3313               N, Parent_Type);
3314          end if;
3315       end if;
3316    end Analyze_Private_Extension_Declaration;
3317
3318    ---------------------------------
3319    -- Analyze_Subtype_Declaration --
3320    ---------------------------------
3321
3322    procedure Analyze_Subtype_Declaration
3323      (N    : Node_Id;
3324       Skip : Boolean := False)
3325    is
3326       Id       : constant Entity_Id := Defining_Identifier (N);
3327       T        : Entity_Id;
3328       R_Checks : Check_Result;
3329
3330    begin
3331       Generate_Definition (Id);
3332       Set_Is_Pure (Id, Is_Pure (Current_Scope));
3333       Init_Size_Align (Id);
3334
3335       --  The following guard condition on Enter_Name is to handle cases where
3336       --  the defining identifier has already been entered into the scope but
3337       --  the declaration as a whole needs to be analyzed.
3338
3339       --  This case in particular happens for derived enumeration types. The
3340       --  derived enumeration type is processed as an inserted enumeration type
3341       --  declaration followed by a rewritten subtype declaration. The defining
3342       --  identifier, however, is entered into the name scope very early in the
3343       --  processing of the original type declaration and therefore needs to be
3344       --  avoided here, when the created subtype declaration is analyzed. (See
3345       --  Build_Derived_Types)
3346
3347       --  This also happens when the full view of a private type is derived
3348       --  type with constraints. In this case the entity has been introduced
3349       --  in the private declaration.
3350
3351       if Skip
3352         or else (Present (Etype (Id))
3353                    and then (Is_Private_Type (Etype (Id))
3354                                or else Is_Task_Type (Etype (Id))
3355                                or else Is_Rewrite_Substitution (N)))
3356       then
3357          null;
3358
3359       else
3360          Enter_Name (Id);
3361       end if;
3362
3363       T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
3364
3365       --  Inherit common attributes
3366
3367       Set_Is_Generic_Type   (Id, Is_Generic_Type   (Base_Type (T)));
3368       Set_Is_Volatile       (Id, Is_Volatile       (T));
3369       Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
3370       Set_Is_Atomic         (Id, Is_Atomic         (T));
3371       Set_Is_Ada_2005_Only  (Id, Is_Ada_2005_Only  (T));
3372       Set_Convention        (Id, Convention        (T));
3373
3374       --  In the case where there is no constraint given in the subtype
3375       --  indication, Process_Subtype just returns the Subtype_Mark, so its
3376       --  semantic attributes must be established here.
3377
3378       if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
3379          Set_Etype (Id, Base_Type (T));
3380
3381          case Ekind (T) is
3382             when Array_Kind =>
3383                Set_Ekind                       (Id, E_Array_Subtype);
3384                Copy_Array_Subtype_Attributes   (Id, T);
3385
3386             when Decimal_Fixed_Point_Kind =>
3387                Set_Ekind                (Id, E_Decimal_Fixed_Point_Subtype);
3388                Set_Digits_Value         (Id, Digits_Value       (T));
3389                Set_Delta_Value          (Id, Delta_Value        (T));
3390                Set_Scale_Value          (Id, Scale_Value        (T));
3391                Set_Small_Value          (Id, Small_Value        (T));
3392                Set_Scalar_Range         (Id, Scalar_Range       (T));
3393                Set_Machine_Radix_10     (Id, Machine_Radix_10   (T));
3394                Set_Is_Constrained       (Id, Is_Constrained     (T));
3395                Set_RM_Size              (Id, RM_Size            (T));
3396
3397             when Enumeration_Kind =>
3398                Set_Ekind                (Id, E_Enumeration_Subtype);
3399                Set_First_Literal        (Id, First_Literal (Base_Type (T)));
3400                Set_Scalar_Range         (Id, Scalar_Range       (T));
3401                Set_Is_Character_Type    (Id, Is_Character_Type  (T));
3402                Set_Is_Constrained       (Id, Is_Constrained     (T));
3403                Set_RM_Size              (Id, RM_Size            (T));
3404
3405             when Ordinary_Fixed_Point_Kind =>
3406                Set_Ekind                (Id, E_Ordinary_Fixed_Point_Subtype);
3407                Set_Scalar_Range         (Id, Scalar_Range       (T));
3408                Set_Small_Value          (Id, Small_Value        (T));
3409                Set_Delta_Value          (Id, Delta_Value        (T));
3410                Set_Is_Constrained       (Id, Is_Constrained     (T));
3411                Set_RM_Size              (Id, RM_Size            (T));
3412
3413             when Float_Kind =>
3414                Set_Ekind                (Id, E_Floating_Point_Subtype);
3415                Set_Scalar_Range         (Id, Scalar_Range       (T));
3416                Set_Digits_Value         (Id, Digits_Value       (T));
3417                Set_Is_Constrained       (Id, Is_Constrained     (T));
3418
3419             when Signed_Integer_Kind =>
3420                Set_Ekind                (Id, E_Signed_Integer_Subtype);
3421                Set_Scalar_Range         (Id, Scalar_Range       (T));
3422                Set_Is_Constrained       (Id, Is_Constrained     (T));
3423                Set_RM_Size              (Id, RM_Size            (T));
3424
3425             when Modular_Integer_Kind =>
3426                Set_Ekind                (Id, E_Modular_Integer_Subtype);
3427                Set_Scalar_Range         (Id, Scalar_Range       (T));
3428                Set_Is_Constrained       (Id, Is_Constrained     (T));
3429                Set_RM_Size              (Id, RM_Size            (T));
3430
3431             when Class_Wide_Kind =>
3432                Set_Ekind                (Id, E_Class_Wide_Subtype);
3433                Set_First_Entity         (Id, First_Entity       (T));
3434                Set_Last_Entity          (Id, Last_Entity        (T));
3435                Set_Class_Wide_Type      (Id, Class_Wide_Type    (T));
3436                Set_Cloned_Subtype       (Id, T);
3437                Set_Is_Tagged_Type       (Id, True);
3438                Set_Has_Unknown_Discriminants
3439                                         (Id, True);
3440
3441                if Ekind (T) = E_Class_Wide_Subtype then
3442                   Set_Equivalent_Type   (Id, Equivalent_Type    (T));
3443                end if;
3444
3445             when E_Record_Type | E_Record_Subtype =>
3446                Set_Ekind                (Id, E_Record_Subtype);
3447
3448                if Ekind (T) = E_Record_Subtype
3449                  and then Present (Cloned_Subtype (T))
3450                then
3451                   Set_Cloned_Subtype    (Id, Cloned_Subtype (T));
3452                else
3453                   Set_Cloned_Subtype    (Id, T);
3454                end if;
3455
3456                Set_First_Entity         (Id, First_Entity       (T));
3457                Set_Last_Entity          (Id, Last_Entity        (T));
3458                Set_Has_Discriminants    (Id, Has_Discriminants  (T));
3459                Set_Is_Constrained       (Id, Is_Constrained     (T));
3460                Set_Is_Limited_Record    (Id, Is_Limited_Record  (T));
3461                Set_Has_Unknown_Discriminants
3462                                         (Id, Has_Unknown_Discriminants (T));
3463
3464                if Has_Discriminants (T) then
3465                   Set_Discriminant_Constraint
3466                                         (Id, Discriminant_Constraint (T));
3467                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3468
3469                elsif Has_Unknown_Discriminants (Id) then
3470                   Set_Discriminant_Constraint (Id, No_Elist);
3471                end if;
3472
3473                if Is_Tagged_Type (T) then
3474                   Set_Is_Tagged_Type    (Id);
3475                   Set_Is_Abstract_Type  (Id, Is_Abstract_Type (T));
3476                   Set_Primitive_Operations
3477                                         (Id, Primitive_Operations (T));
3478                   Set_Class_Wide_Type   (Id, Class_Wide_Type (T));
3479
3480                   if Is_Interface (T) then
3481                      Set_Is_Interface (Id);
3482                      Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
3483                   end if;
3484                end if;
3485
3486             when Private_Kind =>
3487                Set_Ekind              (Id, Subtype_Kind (Ekind   (T)));
3488                Set_Has_Discriminants  (Id, Has_Discriminants     (T));
3489                Set_Is_Constrained     (Id, Is_Constrained        (T));
3490                Set_First_Entity       (Id, First_Entity          (T));
3491                Set_Last_Entity        (Id, Last_Entity           (T));
3492                Set_Private_Dependents (Id, New_Elmt_List);
3493                Set_Is_Limited_Record  (Id, Is_Limited_Record     (T));
3494                Set_Has_Unknown_Discriminants
3495                                       (Id, Has_Unknown_Discriminants (T));
3496                Set_Known_To_Have_Preelab_Init
3497                                       (Id, Known_To_Have_Preelab_Init (T));
3498
3499                if Is_Tagged_Type (T) then
3500                   Set_Is_Tagged_Type       (Id);
3501                   Set_Is_Abstract_Type     (Id, Is_Abstract_Type (T));
3502                   Set_Primitive_Operations (Id, Primitive_Operations (T));
3503                   Set_Class_Wide_Type      (Id, Class_Wide_Type (T));
3504                end if;
3505
3506                --  In general the attributes of the subtype of a private type
3507                --  are the attributes of the partial view of parent. However,
3508                --  the full view may be a discriminated type, and the subtype
3509                --  must share the discriminant constraint to generate correct
3510                --  calls to initialization procedures.
3511
3512                if Has_Discriminants (T) then
3513                   Set_Discriminant_Constraint
3514                                      (Id, Discriminant_Constraint (T));
3515                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3516
3517                elsif Present (Full_View (T))
3518                  and then Has_Discriminants (Full_View (T))
3519                then
3520                   Set_Discriminant_Constraint
3521                                (Id, Discriminant_Constraint (Full_View (T)));
3522                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3523
3524                   --  This would seem semantically correct, but apparently
3525                   --  confuses the back-end. To be explained and checked with
3526                   --  current version ???
3527
3528                   --  Set_Has_Discriminants (Id);
3529                end if;
3530
3531                Prepare_Private_Subtype_Completion (Id, N);
3532
3533             when Access_Kind =>
3534                Set_Ekind             (Id, E_Access_Subtype);
3535                Set_Is_Constrained    (Id, Is_Constrained        (T));
3536                Set_Is_Access_Constant
3537                                      (Id, Is_Access_Constant    (T));
3538                Set_Directly_Designated_Type
3539                                      (Id, Designated_Type       (T));
3540                Set_Can_Never_Be_Null (Id, Can_Never_Be_Null     (T));
3541
3542                --  A Pure library_item must not contain the declaration of a
3543                --  named access type, except within a subprogram, generic
3544                --  subprogram, task unit, or protected unit (RM 10.2.1(16)).
3545
3546                if Comes_From_Source (Id)
3547                  and then In_Pure_Unit
3548                  and then not In_Subprogram_Task_Protected_Unit
3549                then
3550                   Error_Msg_N
3551                     ("named access types not allowed in pure unit", N);
3552                end if;
3553
3554             when Concurrent_Kind =>
3555                Set_Ekind                (Id, Subtype_Kind (Ekind   (T)));
3556                Set_Corresponding_Record_Type (Id,
3557                                          Corresponding_Record_Type (T));
3558                Set_First_Entity         (Id, First_Entity          (T));
3559                Set_First_Private_Entity (Id, First_Private_Entity  (T));
3560                Set_Has_Discriminants    (Id, Has_Discriminants     (T));
3561                Set_Is_Constrained       (Id, Is_Constrained        (T));
3562                Set_Is_Tagged_Type       (Id, Is_Tagged_Type        (T));
3563                Set_Last_Entity          (Id, Last_Entity           (T));
3564
3565                if Has_Discriminants (T) then
3566                   Set_Discriminant_Constraint (Id,
3567                                            Discriminant_Constraint (T));
3568                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3569                end if;
3570
3571             when E_Incomplete_Type =>
3572                if Ada_Version >= Ada_05 then
3573                   Set_Ekind (Id, E_Incomplete_Subtype);
3574
3575                   --  Ada 2005 (AI-412): Decorate an incomplete subtype
3576                   --  of an incomplete type visible through a limited
3577                   --  with clause.
3578
3579                   if From_With_Type (T)
3580                     and then Present (Non_Limited_View (T))
3581                   then
3582                      Set_From_With_Type   (Id);
3583                      Set_Non_Limited_View (Id, Non_Limited_View (T));
3584
3585                   --  Ada 2005 (AI-412): Add the regular incomplete subtype
3586                   --  to the private dependents of the original incomplete
3587                   --  type for future transformation.
3588
3589                   else
3590                      Append_Elmt (Id, Private_Dependents (T));
3591                   end if;
3592
3593                --  If the subtype name denotes an incomplete type an error
3594                --  was already reported by Process_Subtype.
3595
3596                else
3597                   Set_Etype (Id, Any_Type);
3598                end if;
3599
3600             when others =>
3601                raise Program_Error;
3602          end case;
3603       end if;
3604
3605       if Etype (Id) = Any_Type then
3606          return;
3607       end if;
3608
3609       --  Some common processing on all types
3610
3611       Set_Size_Info      (Id,                 T);
3612       Set_First_Rep_Item (Id, First_Rep_Item (T));
3613
3614       T := Etype (Id);
3615
3616       Set_Is_Immediately_Visible   (Id, True);
3617       Set_Depends_On_Private       (Id, Has_Private_Component (T));
3618       Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
3619
3620       if Is_Interface (T) then
3621          Set_Is_Interface (Id);
3622       end if;
3623
3624       if Present (Generic_Parent_Type (N))
3625         and then
3626           (Nkind
3627              (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
3628             or else Nkind
3629               (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
3630                 /=  N_Formal_Private_Type_Definition)
3631       then
3632          if Is_Tagged_Type (Id) then
3633
3634             --  If this is a generic actual subtype for a synchronized type,
3635             --  the primitive operations are those of the corresponding record
3636             --  for which there is a separate subtype declaration.
3637
3638             if Is_Concurrent_Type (Id) then
3639                null;
3640             elsif Is_Class_Wide_Type (Id) then
3641                Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
3642             else
3643                Derive_Subprograms (Generic_Parent_Type (N), Id, T);
3644             end if;
3645
3646          elsif Scope (Etype (Id)) /= Standard_Standard then
3647             Derive_Subprograms (Generic_Parent_Type (N), Id);
3648          end if;
3649       end if;
3650
3651       if Is_Private_Type (T)
3652         and then Present (Full_View (T))
3653       then
3654          Conditional_Delay (Id, Full_View (T));
3655
3656       --  The subtypes of components or subcomponents of protected types
3657       --  do not need freeze nodes, which would otherwise appear in the
3658       --  wrong scope (before the freeze node for the protected type). The
3659       --  proper subtypes are those of the subcomponents of the corresponding
3660       --  record.
3661
3662       elsif Ekind (Scope (Id)) /= E_Protected_Type
3663         and then Present (Scope (Scope (Id))) -- error defense!
3664         and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
3665       then
3666          Conditional_Delay (Id, T);
3667       end if;
3668
3669       --  Check that constraint_error is raised for a scalar subtype
3670       --  indication when the lower or upper bound of a non-null range
3671       --  lies outside the range of the type mark.
3672
3673       if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
3674          if Is_Scalar_Type (Etype (Id))
3675             and then Scalar_Range (Id) /=
3676                      Scalar_Range (Etype (Subtype_Mark
3677                                            (Subtype_Indication (N))))
3678          then
3679             Apply_Range_Check
3680               (Scalar_Range (Id),
3681                Etype (Subtype_Mark (Subtype_Indication (N))));
3682
3683          elsif Is_Array_Type (Etype (Id))
3684            and then Present (First_Index (Id))
3685          then
3686             --  This really should be a subprogram that finds the indications
3687             --  to check???
3688
3689             if ((Nkind (First_Index (Id)) = N_Identifier
3690                    and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
3691                  or else Nkind (First_Index (Id)) = N_Subtype_Indication)
3692               and then
3693                 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
3694             then
3695                declare
3696                   Target_Typ : constant Entity_Id :=
3697                                  Etype
3698                                    (First_Index (Etype
3699                                      (Subtype_Mark (Subtype_Indication (N)))));
3700                begin
3701                   R_Checks :=
3702                     Get_Range_Checks
3703                       (Scalar_Range (Etype (First_Index (Id))),
3704                        Target_Typ,
3705                        Etype (First_Index (Id)),
3706                        Defining_Identifier (N));
3707
3708                   Insert_Range_Checks
3709                     (R_Checks,
3710                      N,
3711                      Target_Typ,
3712                      Sloc (Defining_Identifier (N)));
3713                end;
3714             end if;
3715          end if;
3716       end if;
3717
3718       Set_Optimize_Alignment_Flags (Id);
3719       Check_Eliminated (Id);
3720    end Analyze_Subtype_Declaration;
3721
3722    --------------------------------
3723    -- Analyze_Subtype_Indication --
3724    --------------------------------
3725
3726    procedure Analyze_Subtype_Indication (N : Node_Id) is
3727       T : constant Entity_Id := Subtype_Mark (N);
3728       R : constant Node_Id   := Range_Expression (Constraint (N));
3729
3730    begin
3731       Analyze (T);
3732
3733       if R /= Error then
3734          Analyze (R);
3735          Set_Etype (N, Etype (R));
3736          Resolve (R, Entity (T));
3737       else
3738          Set_Error_Posted (R);
3739          Set_Error_Posted (T);
3740       end if;
3741    end Analyze_Subtype_Indication;
3742
3743    ------------------------------
3744    -- Analyze_Type_Declaration --
3745    ------------------------------
3746
3747    procedure Analyze_Type_Declaration (N : Node_Id) is
3748       Def    : constant Node_Id   := Type_Definition (N);
3749       Def_Id : constant Entity_Id := Defining_Identifier (N);
3750       T      : Entity_Id;
3751       Prev   : Entity_Id;
3752
3753       Is_Remote : constant Boolean :=
3754                     (Is_Remote_Types (Current_Scope)
3755                        or else Is_Remote_Call_Interface (Current_Scope))
3756                     and then not (In_Private_Part (Current_Scope)
3757                                     or else In_Package_Body (Current_Scope));
3758
3759       procedure Check_Ops_From_Incomplete_Type;
3760       --  If there is a tagged incomplete partial view of the type, transfer
3761       --  its operations to the full view, and indicate that the type of the
3762       --  controlling parameter (s) is this full view.
3763
3764       ------------------------------------
3765       -- Check_Ops_From_Incomplete_Type --
3766       ------------------------------------
3767
3768       procedure Check_Ops_From_Incomplete_Type is
3769          Elmt   : Elmt_Id;
3770          Formal : Entity_Id;
3771          Op     : Entity_Id;
3772
3773       begin
3774          if Prev /= T
3775            and then Ekind (Prev) = E_Incomplete_Type
3776            and then Is_Tagged_Type (Prev)
3777            and then Is_Tagged_Type (T)
3778          then
3779             Elmt := First_Elmt (Primitive_Operations (Prev));
3780             while Present (Elmt) loop
3781                Op := Node (Elmt);
3782                Prepend_Elmt (Op, Primitive_Operations (T));
3783
3784                Formal := First_Formal (Op);
3785                while Present (Formal) loop
3786                   if Etype (Formal) = Prev then
3787                      Set_Etype (Formal, T);
3788                   end if;
3789
3790                   Next_Formal (Formal);
3791                end loop;
3792
3793                if Etype (Op) = Prev then
3794                   Set_Etype (Op, T);
3795                end if;
3796
3797                Next_Elmt (Elmt);
3798             end loop;
3799          end if;
3800       end Check_Ops_From_Incomplete_Type;
3801
3802    --  Start of processing for Analyze_Type_Declaration
3803
3804    begin
3805       Prev := Find_Type_Name (N);
3806
3807       --  The full view, if present, now points to the current type
3808
3809       --  Ada 2005 (AI-50217): If the type was previously decorated when
3810       --  imported through a LIMITED WITH clause, it appears as incomplete
3811       --  but has no full view.
3812       --  If the incomplete view is tagged, a class_wide type has been
3813       --  created already. Use it for the full view as well, to prevent
3814       --  multiple incompatible class-wide types that may be  created for
3815       --  self-referential anonymous access components.
3816
3817       if Ekind (Prev) = E_Incomplete_Type
3818         and then Present (Full_View (Prev))
3819       then
3820          T := Full_View (Prev);
3821
3822          if Is_Tagged_Type (Prev)
3823            and then Present (Class_Wide_Type (Prev))
3824          then
3825             Set_Ekind (T, Ekind (Prev));         --  will be reset later
3826             Set_Class_Wide_Type (T, Class_Wide_Type (Prev));
3827             Set_Etype (Class_Wide_Type (T), T);
3828          end if;
3829
3830       else
3831          T := Prev;
3832       end if;
3833
3834       Set_Is_Pure (T, Is_Pure (Current_Scope));
3835
3836       --  We set the flag Is_First_Subtype here. It is needed to set the
3837       --  corresponding flag for the Implicit class-wide-type created
3838       --  during tagged types processing.
3839
3840       Set_Is_First_Subtype (T, True);
3841
3842       --  Only composite types other than array types are allowed to have
3843       --  discriminants.
3844
3845       case Nkind (Def) is
3846
3847          --  For derived types, the rule will be checked once we've figured
3848          --  out the parent type.
3849
3850          when N_Derived_Type_Definition =>
3851             null;
3852
3853          --  For record types, discriminants are allowed
3854
3855          when N_Record_Definition =>
3856             null;
3857
3858          when others =>
3859             if Present (Discriminant_Specifications (N)) then
3860                Error_Msg_N
3861                  ("elementary or array type cannot have discriminants",
3862                   Defining_Identifier
3863                   (First (Discriminant_Specifications (N))));
3864             end if;
3865       end case;
3866
3867       --  Elaborate the type definition according to kind, and generate
3868       --  subsidiary (implicit) subtypes where needed. We skip this if it was
3869       --  already done (this happens during the reanalysis that follows a call
3870       --  to the high level optimizer).
3871
3872       if not Analyzed (T) then
3873          Set_Analyzed (T);
3874
3875          case Nkind (Def) is
3876
3877             when N_Access_To_Subprogram_Definition =>
3878                Access_Subprogram_Declaration (T, Def);
3879
3880                --  If this is a remote access to subprogram, we must create the
3881                --  equivalent fat pointer type, and related subprograms.
3882
3883                if Is_Remote then
3884                   Process_Remote_AST_Declaration (N);
3885                end if;
3886
3887                --  Validate categorization rule against access type declaration
3888                --  usually a violation in Pure unit, Shared_Passive unit.
3889
3890                Validate_Access_Type_Declaration (T, N);
3891
3892             when N_Access_To_Object_Definition =>
3893                Access_Type_Declaration (T, Def);
3894
3895                --  Validate categorization rule against access type declaration
3896                --  usually a violation in Pure unit, Shared_Passive unit.
3897
3898                Validate_Access_Type_Declaration (T, N);
3899
3900                --  If we are in a Remote_Call_Interface package and define a
3901                --  RACW, then calling stubs and specific stream attributes
3902                --  must be added.
3903
3904                if Is_Remote
3905                  and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3906                then
3907                   Add_RACW_Features (Def_Id);
3908                end if;
3909
3910                --  Set no strict aliasing flag if config pragma seen
3911
3912                if Opt.No_Strict_Aliasing then
3913                   Set_No_Strict_Aliasing (Base_Type (Def_Id));
3914                end if;
3915
3916             when N_Array_Type_Definition =>
3917                Array_Type_Declaration (T, Def);
3918
3919             when N_Derived_Type_Definition =>
3920                Derived_Type_Declaration (T, N, T /= Def_Id);
3921
3922             when N_Enumeration_Type_Definition =>
3923                Enumeration_Type_Declaration (T, Def);
3924
3925             when N_Floating_Point_Definition =>
3926                Floating_Point_Type_Declaration (T, Def);
3927
3928             when N_Decimal_Fixed_Point_Definition =>
3929                Decimal_Fixed_Point_Type_Declaration (T, Def);
3930
3931             when N_Ordinary_Fixed_Point_Definition =>
3932                Ordinary_Fixed_Point_Type_Declaration (T, Def);
3933
3934             when N_Signed_Integer_Type_Definition =>
3935                Signed_Integer_Type_Declaration (T, Def);
3936
3937             when N_Modular_Type_Definition =>
3938                Modular_Type_Declaration (T, Def);
3939
3940             when N_Record_Definition =>
3941                Record_Type_Declaration (T, N, Prev);
3942
3943             when others =>
3944                raise Program_Error;
3945
3946          end case;
3947       end if;
3948
3949       if Etype (T) = Any_Type then
3950          return;
3951       end if;
3952
3953       --  Some common processing for all types
3954
3955       Set_Depends_On_Private (T, Has_Private_Component (T));
3956       Check_Ops_From_Incomplete_Type;
3957
3958       --  Both the declared entity, and its anonymous base type if one
3959       --  was created, need freeze nodes allocated.
3960
3961       declare
3962          B : constant Entity_Id := Base_Type (T);
3963
3964       begin
3965          --  In the case where the base type differs from the first subtype, we
3966          --  pre-allocate a freeze node, and set the proper link to the first
3967          --  subtype. Freeze_Entity will use this preallocated freeze node when
3968          --  it freezes the entity.
3969
3970          if B /= T then
3971             Ensure_Freeze_Node (B);
3972             Set_First_Subtype_Link (Freeze_Node (B), T);
3973          end if;
3974
3975          if not From_With_Type (T) then
3976             Set_Has_Delayed_Freeze (T);
3977          end if;
3978       end;
3979
3980       --  Case of T is the full declaration of some private type which has
3981       --  been swapped in Defining_Identifier (N).
3982
3983       if T /= Def_Id and then Is_Private_Type (Def_Id) then
3984          Process_Full_View (N, T, Def_Id);
3985
3986          --  Record the reference. The form of this is a little strange, since
3987          --  the full declaration has been swapped in. So the first parameter
3988          --  here represents the entity to which a reference is made which is
3989          --  the "real" entity, i.e. the one swapped in, and the second
3990          --  parameter provides the reference location.
3991
3992          --  Also, we want to kill Has_Pragma_Unreferenced temporarily here
3993          --  since we don't want a complaint about the full type being an
3994          --  unwanted reference to the private type
3995
3996          declare
3997             B : constant Boolean := Has_Pragma_Unreferenced (T);
3998          begin
3999             Set_Has_Pragma_Unreferenced (T, False);
4000             Generate_Reference (T, T, 'c');
4001             Set_Has_Pragma_Unreferenced (T, B);
4002          end;
4003
4004          Set_Completion_Referenced (Def_Id);
4005
4006       --  For completion of incomplete type, process incomplete dependents
4007       --  and always mark the full type as referenced (it is the incomplete
4008       --  type that we get for any real reference).
4009
4010       elsif Ekind (Prev) = E_Incomplete_Type then
4011          Process_Incomplete_Dependents (N, T, Prev);
4012          Generate_Reference (Prev, Def_Id, 'c');
4013          Set_Completion_Referenced (Def_Id);
4014
4015       --  If not private type or incomplete type completion, this is a real
4016       --  definition of a new entity, so record it.
4017
4018       else
4019          Generate_Definition (Def_Id);
4020       end if;
4021
4022       if Chars (Scope (Def_Id)) =  Name_System
4023         and then Chars (Def_Id) = Name_Address
4024         and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
4025       then
4026          Set_Is_Descendent_Of_Address (Def_Id);
4027          Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
4028          Set_Is_Descendent_Of_Address (Prev);
4029       end if;
4030
4031       Set_Optimize_Alignment_Flags (Def_Id);
4032       Check_Eliminated (Def_Id);
4033    end Analyze_Type_Declaration;
4034
4035    --------------------------
4036    -- Analyze_Variant_Part --
4037    --------------------------
4038
4039    procedure Analyze_Variant_Part (N : Node_Id) is
4040
4041       procedure Non_Static_Choice_Error (Choice : Node_Id);
4042       --  Error routine invoked by the generic instantiation below when the
4043       --  variant part has a non static choice.
4044
4045       procedure Process_Declarations (Variant : Node_Id);
4046       --  Analyzes all the declarations associated with a Variant. Needed by
4047       --  the generic instantiation below.
4048
4049       package Variant_Choices_Processing is new
4050         Generic_Choices_Processing
4051           (Get_Alternatives          => Variants,
4052            Get_Choices               => Discrete_Choices,
4053            Process_Empty_Choice      => No_OP,
4054            Process_Non_Static_Choice => Non_Static_Choice_Error,
4055            Process_Associated_Node   => Process_Declarations);
4056       use Variant_Choices_Processing;
4057       --  Instantiation of the generic choice processing package
4058
4059       -----------------------------
4060       -- Non_Static_Choice_Error --
4061       -----------------------------
4062
4063       procedure Non_Static_Choice_Error (Choice : Node_Id) is
4064       begin
4065          Flag_Non_Static_Expr
4066            ("choice given in variant part is not static!", Choice);
4067       end Non_Static_Choice_Error;
4068
4069       --------------------------
4070       -- Process_Declarations --
4071       --------------------------
4072
4073       procedure Process_Declarations (Variant : Node_Id) is
4074       begin
4075          if not Null_Present (Component_List (Variant)) then
4076             Analyze_Declarations (Component_Items (Component_List (Variant)));
4077
4078             if Present (Variant_Part (Component_List (Variant))) then
4079                Analyze (Variant_Part (Component_List (Variant)));
4080             end if;
4081          end if;
4082       end Process_Declarations;
4083
4084       --  Local Variables
4085
4086       Discr_Name : Node_Id;
4087       Discr_Type : Entity_Id;
4088
4089       Case_Table     : Choice_Table_Type (1 .. Number_Of_Choices (N));
4090       Last_Choice    : Nat;
4091       Dont_Care      : Boolean;
4092       Others_Present : Boolean := False;
4093
4094       pragma Warnings (Off, Case_Table);
4095       pragma Warnings (Off, Last_Choice);
4096       pragma Warnings (Off, Dont_Care);
4097       pragma Warnings (Off, Others_Present);
4098       --  We don't care about the assigned values of any of these
4099
4100    --  Start of processing for Analyze_Variant_Part
4101
4102    begin
4103       Discr_Name := Name (N);
4104       Analyze (Discr_Name);
4105
4106       --  If Discr_Name bad, get out (prevent cascaded errors)
4107
4108       if Etype (Discr_Name) = Any_Type then
4109          return;
4110       end if;
4111
4112       --  Check invalid discriminant in variant part
4113
4114       if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4115          Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4116       end if;
4117
4118       Discr_Type := Etype (Entity (Discr_Name));
4119
4120       if not Is_Discrete_Type (Discr_Type) then
4121          Error_Msg_N
4122            ("discriminant in a variant part must be of a discrete type",
4123              Name (N));
4124          return;
4125       end if;
4126
4127       --  Call the instantiated Analyze_Choices which does the rest of the work
4128
4129       Analyze_Choices
4130         (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
4131    end Analyze_Variant_Part;
4132
4133    ----------------------------
4134    -- Array_Type_Declaration --
4135    ----------------------------
4136
4137    procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4138       Component_Def : constant Node_Id := Component_Definition (Def);
4139       Element_Type  : Entity_Id;
4140       Implicit_Base : Entity_Id;
4141       Index         : Node_Id;
4142       Related_Id    : Entity_Id := Empty;
4143       Nb_Index      : Nat;
4144       P             : constant Node_Id := Parent (Def);
4145       Priv          : Entity_Id;
4146
4147    begin
4148       if Nkind (Def) = N_Constrained_Array_Definition then
4149          Index := First (Discrete_Subtype_Definitions (Def));
4150       else
4151          Index := First (Subtype_Marks (Def));
4152       end if;
4153
4154       --  Find proper names for the implicit types which may be public. In case
4155       --  of anonymous arrays we use the name of the first object of that type
4156       --  as prefix.
4157
4158       if No (T) then
4159          Related_Id :=  Defining_Identifier (P);
4160       else
4161          Related_Id := T;
4162       end if;
4163
4164       Nb_Index := 1;
4165       while Present (Index) loop
4166          Analyze (Index);
4167
4168          --  Add a subtype declaration for each index of private array type
4169          --  declaration whose etype is also private. For example:
4170
4171          --     package Pkg is
4172          --        type Index is private;
4173          --     private
4174          --        type Table is array (Index) of ...
4175          --     end;
4176
4177          --  This is currently required by the expander for the internally
4178          --  generated equality subprogram of records with variant parts in
4179          --  which the etype of some component is such private type.
4180
4181          if Ekind (Current_Scope) = E_Package
4182            and then In_Private_Part (Current_Scope)
4183            and then Has_Private_Declaration (Etype (Index))
4184          then
4185             declare
4186                Loc   : constant Source_Ptr := Sloc (Def);
4187                New_E : Entity_Id;
4188                Decl  : Entity_Id;
4189
4190             begin
4191                New_E :=
4192                  Make_Defining_Identifier (Loc,
4193                    Chars => New_Internal_Name ('T'));
4194                Set_Is_Internal (New_E);
4195
4196                Decl :=
4197                  Make_Subtype_Declaration (Loc,
4198                    Defining_Identifier => New_E,
4199                    Subtype_Indication  =>
4200                      New_Occurrence_Of (Etype (Index), Loc));
4201
4202                Insert_Before (Parent (Def), Decl);
4203                Analyze (Decl);
4204                Set_Etype (Index, New_E);
4205
4206                --  If the index is a range the Entity attribute is not
4207                --  available. Example:
4208
4209                --     package Pkg is
4210                --        type T is private;
4211                --     private
4212                --        type T is new Natural;
4213                --        Table : array (T(1) .. T(10)) of Boolean;
4214                --     end Pkg;
4215
4216                if Nkind (Index) /= N_Range then
4217                   Set_Entity (Index, New_E);
4218                end if;
4219             end;
4220          end if;
4221
4222          Make_Index (Index, P, Related_Id, Nb_Index);
4223          Next_Index (Index);
4224          Nb_Index := Nb_Index + 1;
4225       end loop;
4226
4227       --  Process subtype indication if one is present
4228
4229       if Present (Subtype_Indication (Component_Def)) then
4230          Element_Type :=
4231            Process_Subtype
4232              (Subtype_Indication (Component_Def), P, Related_Id, 'C');
4233
4234       --  Ada 2005 (AI-230): Access Definition case
4235
4236       else pragma Assert (Present (Access_Definition (Component_Def)));
4237
4238          --  Indicate that the anonymous access type is created by the
4239          --  array type declaration.
4240
4241          Element_Type := Access_Definition
4242                            (Related_Nod => P,
4243                             N           => Access_Definition (Component_Def));
4244          Set_Is_Local_Anonymous_Access (Element_Type);
4245
4246          --  Propagate the parent. This field is needed if we have to generate
4247          --  the master_id associated with an anonymous access to task type
4248          --  component (see Expand_N_Full_Type_Declaration.Build_Master)
4249
4250          Set_Parent (Element_Type, Parent (T));
4251
4252          --  Ada 2005 (AI-230): In case of components that are anonymous access
4253          --  types the level of accessibility depends on the enclosing type
4254          --  declaration
4255
4256          Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4257
4258          --  Ada 2005 (AI-254)
4259
4260          declare
4261             CD : constant Node_Id :=
4262                    Access_To_Subprogram_Definition
4263                      (Access_Definition (Component_Def));
4264          begin
4265             if Present (CD) and then Protected_Present (CD) then
4266                Element_Type :=
4267                  Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4268             end if;
4269          end;
4270       end if;
4271
4272       --  Constrained array case
4273
4274       if No (T) then
4275          T := Create_Itype (E_Void, P, Related_Id, 'T');
4276       end if;
4277
4278       if Nkind (Def) = N_Constrained_Array_Definition then
4279
4280          --  Establish Implicit_Base as unconstrained base type
4281
4282          Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4283
4284          Set_Etype              (Implicit_Base, Implicit_Base);
4285          Set_Scope              (Implicit_Base, Current_Scope);
4286          Set_Has_Delayed_Freeze (Implicit_Base);
4287
4288          --  The constrained array type is a subtype of the unconstrained one
4289
4290          Set_Ekind          (T, E_Array_Subtype);
4291          Init_Size_Align    (T);
4292          Set_Etype          (T, Implicit_Base);
4293          Set_Scope          (T, Current_Scope);
4294          Set_Is_Constrained (T, True);
4295          Set_First_Index    (T, First (Discrete_Subtype_Definitions (Def)));
4296          Set_Has_Delayed_Freeze (T);
4297
4298          --  Complete setup of implicit base type
4299
4300          Set_First_Index       (Implicit_Base, First_Index (T));
4301          Set_Component_Type    (Implicit_Base, Element_Type);
4302          Set_Has_Task          (Implicit_Base, Has_Task (Element_Type));
4303          Set_Component_Size    (Implicit_Base, Uint_0);
4304          Set_Packed_Array_Type (Implicit_Base, Empty);
4305          Set_Has_Controlled_Component
4306                                (Implicit_Base, Has_Controlled_Component
4307                                                         (Element_Type)
4308                                                  or else Is_Controlled
4309                                                         (Element_Type));
4310          Set_Finalize_Storage_Only
4311                                (Implicit_Base, Finalize_Storage_Only
4312                                                         (Element_Type));
4313
4314       --  Unconstrained array case
4315
4316       else
4317          Set_Ekind                    (T, E_Array_Type);
4318          Init_Size_Align              (T);
4319          Set_Etype                    (T, T);
4320          Set_Scope                    (T, Current_Scope);
4321          Set_Component_Size           (T, Uint_0);
4322          Set_Is_Constrained           (T, False);
4323          Set_First_Index              (T, First (Subtype_Marks (Def)));
4324          Set_Has_Delayed_Freeze       (T, True);
4325          Set_Has_Task                 (T, Has_Task      (Element_Type));
4326          Set_Has_Controlled_Component (T, Has_Controlled_Component
4327                                                         (Element_Type)
4328                                             or else
4329                                           Is_Controlled (Element_Type));
4330          Set_Finalize_Storage_Only    (T, Finalize_Storage_Only
4331                                                         (Element_Type));
4332       end if;
4333
4334       --  Common attributes for both cases
4335
4336       Set_Component_Type (Base_Type (T), Element_Type);
4337       Set_Packed_Array_Type (T, Empty);
4338
4339       if Aliased_Present (Component_Definition (Def)) then
4340          Set_Has_Aliased_Components (Etype (T));
4341       end if;
4342
4343       --  Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4344       --  array type to ensure that objects of this type are initialized.
4345
4346       if Ada_Version >= Ada_05
4347         and then Can_Never_Be_Null (Element_Type)
4348       then
4349          Set_Can_Never_Be_Null (T);
4350
4351          if Null_Exclusion_Present (Component_Definition (Def))
4352
4353             --  No need to check itypes because in their case this check was
4354             --  done at their point of creation
4355
4356            and then not Is_Itype (Element_Type)
4357          then
4358             Error_Msg_N
4359               ("`NOT NULL` not allowed (null already excluded)",
4360                Subtype_Indication (Component_Definition (Def)));
4361          end if;
4362       end if;
4363
4364       Priv := Private_Component (Element_Type);
4365
4366       if Present (Priv) then
4367
4368          --  Check for circular definitions
4369
4370          if Priv = Any_Type then
4371             Set_Component_Type (Etype (T), Any_Type);
4372
4373          --  There is a gap in the visibility of operations on the composite
4374          --  type only if the component type is defined in a different scope.
4375
4376          elsif Scope (Priv) = Current_Scope then
4377             null;
4378
4379          elsif Is_Limited_Type (Priv) then
4380             Set_Is_Limited_Composite (Etype (T));
4381             Set_Is_Limited_Composite (T);
4382          else
4383             Set_Is_Private_Composite (Etype (T));
4384             Set_Is_Private_Composite (T);
4385          end if;
4386       end if;
4387
4388       --  A syntax error in the declaration itself may lead to an empty index
4389       --  list, in which case do a minimal patch.
4390
4391       if No (First_Index (T)) then
4392          Error_Msg_N ("missing index definition in array type declaration", T);
4393
4394          declare
4395             Indices : constant List_Id :=
4396                         New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4397          begin
4398             Set_Discrete_Subtype_Definitions (Def, Indices);
4399             Set_First_Index (T, First (Indices));
4400             return;
4401          end;
4402       end if;
4403
4404       --  Create a concatenation operator for the new type. Internal array
4405       --  types created for packed entities do not need such, they are
4406       --  compatible with the user-defined type.
4407
4408       if Number_Dimensions (T) = 1
4409          and then not Is_Packed_Array_Type (T)
4410       then
4411          New_Concatenation_Op (T);
4412       end if;
4413
4414       --  In the case of an unconstrained array the parser has already verified
4415       --  that all the indices are unconstrained but we still need to make sure
4416       --  that the element type is constrained.
4417
4418       if Is_Indefinite_Subtype (Element_Type) then
4419          Error_Msg_N
4420            ("unconstrained element type in array declaration",
4421             Subtype_Indication (Component_Def));
4422
4423       elsif Is_Abstract_Type (Element_Type) then
4424          Error_Msg_N
4425            ("the type of a component cannot be abstract",
4426             Subtype_Indication (Component_Def));
4427       end if;
4428    end Array_Type_Declaration;
4429
4430    ------------------------------------------------------
4431    -- Replace_Anonymous_Access_To_Protected_Subprogram --
4432    ------------------------------------------------------
4433
4434    function Replace_Anonymous_Access_To_Protected_Subprogram
4435      (N : Node_Id) return Entity_Id
4436    is
4437       Loc : constant Source_Ptr := Sloc (N);
4438
4439       Curr_Scope : constant Scope_Stack_Entry :=
4440                      Scope_Stack.Table (Scope_Stack.Last);
4441
4442       Anon : constant Entity_Id :=
4443                Make_Defining_Identifier (Loc,
4444                  Chars => New_Internal_Name ('S'));
4445
4446       Acc  : Node_Id;
4447       Comp : Node_Id;
4448       Decl : Node_Id;
4449       P    : Node_Id;
4450
4451    begin
4452       Set_Is_Internal (Anon);
4453
4454       case Nkind (N) is
4455          when N_Component_Declaration       |
4456            N_Unconstrained_Array_Definition |
4457            N_Constrained_Array_Definition   =>
4458             Comp := Component_Definition (N);
4459             Acc  := Access_Definition (Comp);
4460
4461          when N_Discriminant_Specification =>
4462             Comp := Discriminant_Type (N);
4463             Acc  := Comp;
4464
4465          when N_Parameter_Specification =>
4466             Comp := Parameter_Type (N);
4467             Acc  := Comp;
4468
4469          when N_Access_Function_Definition  =>
4470             Comp := Result_Definition (N);
4471             Acc  := Comp;
4472
4473          when N_Object_Declaration  =>
4474             Comp := Object_Definition (N);
4475             Acc  := Comp;
4476
4477          when N_Function_Specification =>
4478             Comp := Result_Definition (N);
4479             Acc  := Comp;
4480
4481          when others =>
4482             raise Program_Error;
4483       end case;
4484
4485       Decl := Make_Full_Type_Declaration (Loc,
4486                 Defining_Identifier => Anon,
4487                 Type_Definition   =>
4488                   Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
4489
4490       Mark_Rewrite_Insertion (Decl);
4491
4492       --  Insert the new declaration in the nearest enclosing scope. If the
4493       --  node is a body and N is its return type, the declaration belongs in
4494       --  the enclosing scope.
4495
4496       P := Parent (N);
4497
4498       if Nkind (P) = N_Subprogram_Body
4499         and then Nkind (N) = N_Function_Specification
4500       then
4501          P := Parent (P);
4502       end if;
4503
4504       while Present (P) and then not Has_Declarations (P) loop
4505          P := Parent (P);
4506       end loop;
4507
4508       pragma Assert (Present (P));
4509
4510       if Nkind (P) = N_Package_Specification then
4511          Prepend (Decl, Visible_Declarations (P));
4512       else
4513          Prepend (Decl, Declarations (P));
4514       end if;
4515
4516       --  Replace the anonymous type with an occurrence of the new declaration.
4517       --  In all cases the rewritten node does not have the null-exclusion
4518       --  attribute because (if present) it was already inherited by the
4519       --  anonymous entity (Anon). Thus, in case of components we do not
4520       --  inherit this attribute.
4521
4522       if Nkind (N) = N_Parameter_Specification then
4523          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4524          Set_Etype (Defining_Identifier (N), Anon);
4525          Set_Null_Exclusion_Present (N, False);
4526
4527       elsif Nkind (N) = N_Object_Declaration then
4528          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4529          Set_Etype (Defining_Identifier (N), Anon);
4530
4531       elsif Nkind (N) = N_Access_Function_Definition then
4532          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4533
4534       elsif Nkind (N) = N_Function_Specification then
4535          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4536          Set_Etype (Defining_Unit_Name (N), Anon);
4537
4538       else
4539          Rewrite (Comp,
4540            Make_Component_Definition (Loc,
4541              Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
4542       end if;
4543
4544       Mark_Rewrite_Insertion (Comp);
4545
4546       if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
4547          Analyze (Decl);
4548
4549       else
4550          --  Temporarily remove the current scope (record or subprogram) from
4551          --  the stack to add the new declarations to the enclosing scope.
4552
4553          Scope_Stack.Decrement_Last;
4554          Analyze (Decl);
4555          Set_Is_Itype (Anon);
4556          Scope_Stack.Append (Curr_Scope);
4557       end if;
4558
4559       Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
4560       Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
4561       return Anon;
4562    end Replace_Anonymous_Access_To_Protected_Subprogram;
4563
4564    -------------------------------
4565    -- Build_Derived_Access_Type --
4566    -------------------------------
4567
4568    procedure Build_Derived_Access_Type
4569      (N            : Node_Id;
4570       Parent_Type  : Entity_Id;
4571       Derived_Type : Entity_Id)
4572    is
4573       S : constant Node_Id := Subtype_Indication (Type_Definition (N));
4574
4575       Desig_Type      : Entity_Id;
4576       Discr           : Entity_Id;
4577       Discr_Con_Elist : Elist_Id;
4578       Discr_Con_El    : Elmt_Id;
4579       Subt            : Entity_Id;
4580
4581    begin
4582       --  Set the designated type so it is available in case this is an access
4583       --  to a self-referential type, e.g. a standard list type with a next
4584       --  pointer. Will be reset after subtype is built.
4585
4586       Set_Directly_Designated_Type
4587         (Derived_Type, Designated_Type (Parent_Type));
4588
4589       Subt := Process_Subtype (S, N);
4590
4591       if Nkind (S) /= N_Subtype_Indication
4592         and then Subt /= Base_Type (Subt)
4593       then
4594          Set_Ekind (Derived_Type, E_Access_Subtype);
4595       end if;
4596
4597       if Ekind (Derived_Type) = E_Access_Subtype then
4598          declare
4599             Pbase      : constant Entity_Id := Base_Type (Parent_Type);
4600             Ibase      : constant Entity_Id :=
4601                            Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
4602             Svg_Chars  : constant Name_Id   := Chars (Ibase);
4603             Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
4604
4605          begin
4606             Copy_Node (Pbase, Ibase);
4607
4608             Set_Chars             (Ibase, Svg_Chars);
4609             Set_Next_Entity       (Ibase, Svg_Next_E);
4610             Set_Sloc              (Ibase, Sloc (Derived_Type));
4611             Set_Scope             (Ibase, Scope (Derived_Type));
4612             Set_Freeze_Node       (Ibase, Empty);
4613             Set_Is_Frozen         (Ibase, False);
4614             Set_Comes_From_Source (Ibase, False);
4615             Set_Is_First_Subtype  (Ibase, False);
4616
4617             Set_Etype (Ibase, Pbase);
4618             Set_Etype (Derived_Type, Ibase);
4619          end;
4620       end if;
4621
4622       Set_Directly_Designated_Type
4623         (Derived_Type, Designated_Type (Subt));
4624
4625       Set_Is_Constrained     (Derived_Type, Is_Constrained (Subt));
4626       Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
4627       Set_Size_Info          (Derived_Type,                     Parent_Type);
4628       Set_RM_Size            (Derived_Type, RM_Size            (Parent_Type));
4629       Set_Depends_On_Private (Derived_Type,
4630                               Has_Private_Component (Derived_Type));
4631       Conditional_Delay      (Derived_Type, Subt);
4632
4633       --  Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
4634       --  that it is not redundant.
4635
4636       if Null_Exclusion_Present (Type_Definition (N)) then
4637          Set_Can_Never_Be_Null (Derived_Type);
4638
4639          if Can_Never_Be_Null (Parent_Type)
4640            and then False
4641          then
4642             Error_Msg_NE
4643               ("`NOT NULL` not allowed (& already excludes null)",
4644                 N, Parent_Type);
4645          end if;
4646
4647       elsif Can_Never_Be_Null (Parent_Type) then
4648          Set_Can_Never_Be_Null (Derived_Type);
4649       end if;
4650
4651       --  Note: we do not copy the Storage_Size_Variable, since we always go to
4652       --  the root type for this information.
4653
4654       --  Apply range checks to discriminants for derived record case
4655       --  ??? THIS CODE SHOULD NOT BE HERE REALLY.
4656
4657       Desig_Type := Designated_Type (Derived_Type);
4658       if Is_Composite_Type (Desig_Type)
4659         and then (not Is_Array_Type (Desig_Type))
4660         and then Has_Discriminants (Desig_Type)
4661         and then Base_Type (Desig_Type) /= Desig_Type
4662       then
4663          Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
4664          Discr_Con_El := First_Elmt (Discr_Con_Elist);
4665
4666          Discr := First_Discriminant (Base_Type (Desig_Type));
4667          while Present (Discr_Con_El) loop
4668             Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
4669             Next_Elmt (Discr_Con_El);
4670             Next_Discriminant (Discr);
4671          end loop;
4672       end if;
4673    end Build_Derived_Access_Type;
4674
4675    ------------------------------
4676    -- Build_Derived_Array_Type --
4677    ------------------------------
4678
4679    procedure Build_Derived_Array_Type
4680      (N            : Node_Id;
4681       Parent_Type  : Entity_Id;
4682       Derived_Type : Entity_Id)
4683    is
4684       Loc           : constant Source_Ptr := Sloc (N);
4685       Tdef          : constant Node_Id    := Type_Definition (N);
4686       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
4687       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
4688       Implicit_Base : Entity_Id;
4689       New_Indic     : Node_Id;
4690
4691       procedure Make_Implicit_Base;
4692       --  If the parent subtype is constrained, the derived type is a subtype
4693       --  of an implicit base type derived from the parent base.
4694
4695       ------------------------
4696       -- Make_Implicit_Base --
4697       ------------------------
4698
4699       procedure Make_Implicit_Base is
4700       begin
4701          Implicit_Base :=
4702            Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4703
4704          Set_Ekind (Implicit_Base, Ekind (Parent_Base));
4705          Set_Etype (Implicit_Base, Parent_Base);
4706
4707          Copy_Array_Subtype_Attributes   (Implicit_Base, Parent_Base);
4708          Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
4709
4710          Set_Has_Delayed_Freeze (Implicit_Base, True);
4711       end Make_Implicit_Base;
4712
4713    --  Start of processing for Build_Derived_Array_Type
4714
4715    begin
4716       if not Is_Constrained (Parent_Type) then
4717          if Nkind (Indic) /= N_Subtype_Indication then
4718             Set_Ekind (Derived_Type, E_Array_Type);
4719
4720             Copy_Array_Subtype_Attributes   (Derived_Type, Parent_Type);
4721             Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
4722
4723             Set_Has_Delayed_Freeze (Derived_Type, True);
4724
4725          else
4726             Make_Implicit_Base;
4727             Set_Etype (Derived_Type, Implicit_Base);
4728
4729             New_Indic :=
4730               Make_Subtype_Declaration (Loc,
4731                 Defining_Identifier => Derived_Type,
4732                 Subtype_Indication  =>
4733                   Make_Subtype_Indication (Loc,
4734                     Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
4735                     Constraint => Constraint (Indic)));
4736
4737             Rewrite (N, New_Indic);
4738             Analyze (N);
4739          end if;
4740
4741       else
4742          if Nkind (Indic) /= N_Subtype_Indication then
4743             Make_Implicit_Base;
4744
4745             Set_Ekind             (Derived_Type, Ekind (Parent_Type));
4746             Set_Etype             (Derived_Type, Implicit_Base);
4747             Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4748
4749          else
4750             Error_Msg_N ("illegal constraint on constrained type", Indic);
4751          end if;
4752       end if;
4753
4754       --  If parent type is not a derived type itself, and is declared in
4755       --  closed scope (e.g. a subprogram), then we must explicitly introduce
4756       --  the new type's concatenation operator since Derive_Subprograms
4757       --  will not inherit the parent's operator. If the parent type is
4758       --  unconstrained, the operator is of the unconstrained base type.
4759
4760       if Number_Dimensions (Parent_Type) = 1
4761         and then not Is_Limited_Type (Parent_Type)
4762         and then not Is_Derived_Type (Parent_Type)
4763         and then not Is_Package_Or_Generic_Package
4764                        (Scope (Base_Type (Parent_Type)))
4765       then
4766          if not Is_Constrained (Parent_Type)
4767            and then Is_Constrained (Derived_Type)
4768          then
4769             New_Concatenation_Op (Implicit_Base);
4770          else
4771             New_Concatenation_Op (Derived_Type);
4772          end if;
4773       end if;
4774    end Build_Derived_Array_Type;
4775
4776    -----------------------------------
4777    -- Build_Derived_Concurrent_Type --
4778    -----------------------------------
4779
4780    procedure Build_Derived_Concurrent_Type
4781      (N            : Node_Id;
4782       Parent_Type  : Entity_Id;
4783       Derived_Type : Entity_Id)
4784    is
4785       D_Constraint : Node_Id;
4786       Disc_Spec    : Node_Id;
4787       Old_Disc     : Entity_Id;
4788       New_Disc     : Entity_Id;
4789
4790       Constraint_Present : constant Boolean :=
4791                              Nkind (Subtype_Indication (Type_Definition (N)))
4792                                                      = N_Subtype_Indication;
4793
4794    begin
4795       Set_Stored_Constraint (Derived_Type, No_Elist);
4796
4797       --  Copy Storage_Size and Relative_Deadline variables if task case
4798
4799       if Is_Task_Type (Parent_Type) then
4800          Set_Storage_Size_Variable (Derived_Type,
4801            Storage_Size_Variable (Parent_Type));
4802          Set_Relative_Deadline_Variable (Derived_Type,
4803            Relative_Deadline_Variable (Parent_Type));
4804       end if;
4805
4806       if Present (Discriminant_Specifications (N)) then
4807          Push_Scope (Derived_Type);
4808          Check_Or_Process_Discriminants (N, Derived_Type);
4809          End_Scope;
4810
4811       elsif Constraint_Present then
4812
4813          --  Build constrained subtype and derive from it
4814
4815          declare
4816             Loc  : constant Source_Ptr := Sloc (N);
4817             Anon : constant Entity_Id :=
4818                      Make_Defining_Identifier (Loc,
4819                        New_External_Name (Chars (Derived_Type), 'T'));
4820             Decl : Node_Id;
4821
4822          begin
4823             Decl :=
4824               Make_Subtype_Declaration (Loc,
4825                 Defining_Identifier => Anon,
4826                 Subtype_Indication =>
4827                   Subtype_Indication (Type_Definition (N)));
4828             Insert_Before (N, Decl);
4829             Analyze (Decl);
4830
4831             Rewrite (Subtype_Indication (Type_Definition (N)),
4832               New_Occurrence_Of (Anon, Loc));
4833             Set_Analyzed (Derived_Type, False);
4834             Analyze (N);
4835             return;
4836          end;
4837       end if;
4838
4839       --  All attributes are inherited from parent. In particular,
4840       --  entries and the corresponding record type are the same.
4841       --  Discriminants may be renamed, and must be treated separately.
4842
4843       Set_Has_Discriminants
4844         (Derived_Type, Has_Discriminants         (Parent_Type));
4845       Set_Corresponding_Record_Type
4846         (Derived_Type, Corresponding_Record_Type (Parent_Type));
4847
4848       --  Is_Constrained is set according the parent subtype, but is set to
4849       --  False if the derived type is declared with new discriminants.
4850
4851       Set_Is_Constrained
4852         (Derived_Type,
4853          (Is_Constrained (Parent_Type) or else Constraint_Present)
4854            and then not Present (Discriminant_Specifications (N)));
4855
4856       if Constraint_Present then
4857          if not Has_Discriminants (Parent_Type) then
4858             Error_Msg_N ("untagged parent must have discriminants", N);
4859
4860          elsif Present (Discriminant_Specifications (N)) then
4861
4862             --  Verify that new discriminants are used to constrain old ones
4863
4864             D_Constraint :=
4865               First
4866                 (Constraints
4867                   (Constraint (Subtype_Indication (Type_Definition (N)))));
4868
4869             Old_Disc  := First_Discriminant (Parent_Type);
4870             New_Disc  := First_Discriminant (Derived_Type);
4871             Disc_Spec := First (Discriminant_Specifications (N));
4872             while Present (Old_Disc) and then Present (Disc_Spec) loop
4873                if Nkind (Discriminant_Type (Disc_Spec)) /=
4874                                               N_Access_Definition
4875                then
4876                   Analyze (Discriminant_Type (Disc_Spec));
4877
4878                   if not Subtypes_Statically_Compatible (
4879                              Etype (Discriminant_Type (Disc_Spec)),
4880                                Etype (Old_Disc))
4881                   then
4882                      Error_Msg_N
4883                        ("not statically compatible with parent discriminant",
4884                         Discriminant_Type (Disc_Spec));
4885                   end if;
4886                end if;
4887
4888                if Nkind (D_Constraint) = N_Identifier
4889                  and then Chars (D_Constraint) /=
4890                           Chars (Defining_Identifier (Disc_Spec))
4891                then
4892                   Error_Msg_N ("new discriminants must constrain old ones",
4893                     D_Constraint);
4894                else
4895                   Set_Corresponding_Discriminant (New_Disc, Old_Disc);
4896                end if;
4897
4898                Next_Discriminant (Old_Disc);
4899                Next_Discriminant (New_Disc);
4900                Next (Disc_Spec);
4901             end loop;
4902
4903             if Present (Old_Disc) or else Present (Disc_Spec) then
4904                Error_Msg_N ("discriminant mismatch in derivation", N);
4905             end if;
4906
4907          end if;
4908
4909       elsif Present (Discriminant_Specifications (N)) then
4910          Error_Msg_N
4911            ("missing discriminant constraint in untagged derivation",
4912             N);
4913       end if;
4914
4915       if Present (Discriminant_Specifications (N)) then
4916          Old_Disc := First_Discriminant (Parent_Type);
4917          while Present (Old_Disc) loop
4918
4919             if No (Next_Entity (Old_Disc))
4920               or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
4921             then
4922                Set_Next_Entity (Last_Entity (Derived_Type),
4923                                          Next_Entity (Old_Disc));
4924                exit;
4925             end if;
4926
4927             Next_Discriminant (Old_Disc);
4928          end loop;
4929
4930       else
4931          Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
4932          if Has_Discriminants (Parent_Type) then
4933             Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
4934             Set_Discriminant_Constraint (
4935               Derived_Type, Discriminant_Constraint (Parent_Type));
4936          end if;
4937       end if;
4938
4939       Set_Last_Entity  (Derived_Type, Last_Entity  (Parent_Type));
4940
4941       Set_Has_Completion (Derived_Type);
4942    end Build_Derived_Concurrent_Type;
4943
4944    ------------------------------------
4945    -- Build_Derived_Enumeration_Type --
4946    ------------------------------------
4947
4948    procedure Build_Derived_Enumeration_Type
4949      (N            : Node_Id;
4950       Parent_Type  : Entity_Id;
4951       Derived_Type : Entity_Id)
4952    is
4953       Loc           : constant Source_Ptr := Sloc (N);
4954       Def           : constant Node_Id    := Type_Definition (N);
4955       Indic         : constant Node_Id    := Subtype_Indication (Def);
4956       Implicit_Base : Entity_Id;
4957       Literal       : Entity_Id;
4958       New_Lit       : Entity_Id;
4959       Literals_List : List_Id;
4960       Type_Decl     : Node_Id;
4961       Hi, Lo        : Node_Id;
4962       Rang_Expr     : Node_Id;
4963
4964    begin
4965       --  Since types Standard.Character and Standard.Wide_Character do
4966       --  not have explicit literals lists we need to process types derived
4967       --  from them specially. This is handled by Derived_Standard_Character.
4968       --  If the parent type is a generic type, there are no literals either,
4969       --  and we construct the same skeletal representation as for the generic
4970       --  parent type.
4971
4972       if Is_Standard_Character_Type (Parent_Type) then
4973          Derived_Standard_Character (N, Parent_Type, Derived_Type);
4974
4975       elsif Is_Generic_Type (Root_Type (Parent_Type)) then
4976          declare
4977             Lo : Node_Id;
4978             Hi : Node_Id;
4979
4980          begin
4981             Lo :=
4982                Make_Attribute_Reference (Loc,
4983                  Attribute_Name => Name_First,
4984                  Prefix => New_Reference_To (Derived_Type, Loc));
4985             Set_Etype (Lo, Derived_Type);
4986
4987             Hi :=
4988                Make_Attribute_Reference (Loc,
4989                  Attribute_Name => Name_Last,
4990                  Prefix => New_Reference_To (Derived_Type, Loc));
4991             Set_Etype (Hi, Derived_Type);
4992
4993             Set_Scalar_Range (Derived_Type,
4994                Make_Range (Loc,
4995                  Low_Bound => Lo,
4996                  High_Bound => Hi));
4997          end;
4998
4999       else
5000          --  If a constraint is present, analyze the bounds to catch
5001          --  premature usage of the derived literals.
5002
5003          if Nkind (Indic) = N_Subtype_Indication
5004            and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
5005          then
5006             Analyze (Low_Bound  (Range_Expression (Constraint (Indic))));
5007             Analyze (High_Bound (Range_Expression (Constraint (Indic))));
5008          end if;
5009
5010          --  Introduce an implicit base type for the derived type even if there
5011          --  is no constraint attached to it, since this seems closer to the
5012          --  Ada semantics. Build a full type declaration tree for the derived
5013          --  type using the implicit base type as the defining identifier. The
5014          --  build a subtype declaration tree which applies the constraint (if
5015          --  any) have it replace the derived type declaration.
5016
5017          Literal := First_Literal (Parent_Type);
5018          Literals_List := New_List;
5019          while Present (Literal)
5020            and then Ekind (Literal) = E_Enumeration_Literal
5021          loop
5022             --  Literals of the derived type have the same representation as
5023             --  those of the parent type, but this representation can be
5024             --  overridden by an explicit representation clause. Indicate
5025             --  that there is no explicit representation given yet. These
5026             --  derived literals are implicit operations of the new type,
5027             --  and can be overridden by explicit ones.
5028
5029             if Nkind (Literal) = N_Defining_Character_Literal then
5030                New_Lit :=
5031                  Make_Defining_Character_Literal (Loc, Chars (Literal));
5032             else
5033                New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
5034             end if;
5035
5036             Set_Ekind                (New_Lit, E_Enumeration_Literal);
5037             Set_Enumeration_Pos      (New_Lit, Enumeration_Pos (Literal));
5038             Set_Enumeration_Rep      (New_Lit, Enumeration_Rep (Literal));
5039             Set_Enumeration_Rep_Expr (New_Lit, Empty);
5040             Set_Alias                (New_Lit, Literal);
5041             Set_Is_Known_Valid       (New_Lit, True);
5042
5043             Append (New_Lit, Literals_List);
5044             Next_Literal (Literal);
5045          end loop;
5046
5047          Implicit_Base :=
5048            Make_Defining_Identifier (Sloc (Derived_Type),
5049              New_External_Name (Chars (Derived_Type), 'B'));
5050
5051          --  Indicate the proper nature of the derived type. This must be done
5052          --  before analysis of the literals, to recognize cases when a literal
5053          --  may be hidden by a previous explicit function definition (cf.
5054          --  c83031a).
5055
5056          Set_Ekind (Derived_Type, E_Enumeration_Subtype);
5057          Set_Etype (Derived_Type, Implicit_Base);
5058
5059          Type_Decl :=
5060            Make_Full_Type_Declaration (Loc,
5061              Defining_Identifier => Implicit_Base,
5062              Discriminant_Specifications => No_List,
5063              Type_Definition =>
5064                Make_Enumeration_Type_Definition (Loc, Literals_List));
5065
5066          Mark_Rewrite_Insertion (Type_Decl);
5067          Insert_Before (N, Type_Decl);
5068          Analyze (Type_Decl);
5069
5070          --  After the implicit base is analyzed its Etype needs to be changed
5071          --  to reflect the fact that it is derived from the parent type which
5072          --  was ignored during analysis. We also set the size at this point.
5073
5074          Set_Etype (Implicit_Base, Parent_Type);
5075
5076          Set_Size_Info      (Implicit_Base,                 Parent_Type);
5077          Set_RM_Size        (Implicit_Base, RM_Size        (Parent_Type));
5078          Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5079
5080          Set_Has_Non_Standard_Rep
5081                             (Implicit_Base, Has_Non_Standard_Rep
5082                                                            (Parent_Type));
5083          Set_Has_Delayed_Freeze (Implicit_Base);
5084
5085          --  Process the subtype indication including a validation check on the
5086          --  constraint, if any. If a constraint is given, its bounds must be
5087          --  implicitly converted to the new type.
5088
5089          if Nkind (Indic) = N_Subtype_Indication then
5090             declare
5091                R : constant Node_Id :=
5092                      Range_Expression (Constraint (Indic));
5093
5094             begin
5095                if Nkind (R) = N_Range then
5096                   Hi := Build_Scalar_Bound
5097                           (High_Bound (R), Parent_Type, Implicit_Base);
5098                   Lo := Build_Scalar_Bound
5099                           (Low_Bound  (R), Parent_Type, Implicit_Base);
5100
5101                else
5102                   --  Constraint is a Range attribute. Replace with explicit
5103                   --  mention of the bounds of the prefix, which must be a
5104                   --  subtype.
5105
5106                   Analyze (Prefix (R));
5107                   Hi :=
5108                     Convert_To (Implicit_Base,
5109                       Make_Attribute_Reference (Loc,
5110                         Attribute_Name => Name_Last,
5111                         Prefix =>
5112                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5113
5114                   Lo :=
5115                     Convert_To (Implicit_Base,
5116                       Make_Attribute_Reference (Loc,
5117                         Attribute_Name => Name_First,
5118                         Prefix =>
5119                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5120                end if;
5121             end;
5122
5123          else
5124             Hi :=
5125               Build_Scalar_Bound
5126                 (Type_High_Bound (Parent_Type),
5127                  Parent_Type, Implicit_Base);
5128             Lo :=
5129                Build_Scalar_Bound
5130                  (Type_Low_Bound (Parent_Type),
5131                   Parent_Type, Implicit_Base);
5132          end if;
5133
5134          Rang_Expr :=
5135            Make_Range (Loc,
5136              Low_Bound  => Lo,
5137              High_Bound => Hi);
5138
5139          --  If we constructed a default range for the case where no range
5140          --  was given, then the expressions in the range must not freeze
5141          --  since they do not correspond to expressions in the source.
5142
5143          if Nkind (Indic) /= N_Subtype_Indication then
5144             Set_Must_Not_Freeze (Lo);
5145             Set_Must_Not_Freeze (Hi);
5146             Set_Must_Not_Freeze (Rang_Expr);
5147          end if;
5148
5149          Rewrite (N,
5150            Make_Subtype_Declaration (Loc,
5151              Defining_Identifier => Derived_Type,
5152              Subtype_Indication =>
5153                Make_Subtype_Indication (Loc,
5154                  Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5155                  Constraint =>
5156                    Make_Range_Constraint (Loc,
5157                      Range_Expression => Rang_Expr))));
5158
5159          Analyze (N);
5160
5161          --  If pragma Discard_Names applies on the first subtype of the parent
5162          --  type, then it must be applied on this subtype as well.
5163
5164          if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5165             Set_Discard_Names (Derived_Type);
5166          end if;
5167
5168          --  Apply a range check. Since this range expression doesn't have an
5169          --  Etype, we have to specifically pass the Source_Typ parameter. Is
5170          --  this right???
5171
5172          if Nkind (Indic) = N_Subtype_Indication then
5173             Apply_Range_Check (Range_Expression (Constraint (Indic)),
5174                                Parent_Type,
5175                                Source_Typ => Entity (Subtype_Mark (Indic)));
5176          end if;
5177       end if;
5178    end Build_Derived_Enumeration_Type;
5179
5180    --------------------------------
5181    -- Build_Derived_Numeric_Type --
5182    --------------------------------
5183
5184    procedure Build_Derived_Numeric_Type
5185      (N            : Node_Id;
5186       Parent_Type  : Entity_Id;
5187       Derived_Type : Entity_Id)
5188    is
5189       Loc           : constant Source_Ptr := Sloc (N);
5190       Tdef          : constant Node_Id    := Type_Definition (N);
5191       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
5192       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
5193       No_Constraint : constant Boolean    := Nkind (Indic) /=
5194                                                   N_Subtype_Indication;
5195       Implicit_Base : Entity_Id;
5196
5197       Lo : Node_Id;
5198       Hi : Node_Id;
5199
5200    begin
5201       --  Process the subtype indication including a validation check on
5202       --  the constraint if any.
5203
5204       Discard_Node (Process_Subtype (Indic, N));
5205
5206       --  Introduce an implicit base type for the derived type even if there
5207       --  is no constraint attached to it, since this seems closer to the Ada
5208       --  semantics.
5209
5210       Implicit_Base :=
5211         Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5212
5213       Set_Etype          (Implicit_Base, Parent_Base);
5214       Set_Ekind          (Implicit_Base, Ekind          (Parent_Base));
5215       Set_Size_Info      (Implicit_Base,                 Parent_Base);
5216       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5217       Set_Parent         (Implicit_Base, Parent (Derived_Type));
5218
5219       --  Set RM Size for discrete type or decimal fixed-point type
5220       --  Ordinary fixed-point is excluded, why???
5221
5222       if Is_Discrete_Type (Parent_Base)
5223         or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5224       then
5225          Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5226       end if;
5227
5228       Set_Has_Delayed_Freeze (Implicit_Base);
5229
5230       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
5231       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5232
5233       Set_Scalar_Range (Implicit_Base,
5234         Make_Range (Loc,
5235           Low_Bound  => Lo,
5236           High_Bound => Hi));
5237
5238       if Has_Infinities (Parent_Base) then
5239          Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5240       end if;
5241
5242       --  The Derived_Type, which is the entity of the declaration, is a
5243       --  subtype of the implicit base. Its Ekind is a subtype, even in the
5244       --  absence of an explicit constraint.
5245
5246       Set_Etype (Derived_Type, Implicit_Base);
5247
5248       --  If we did not have a constraint, then the Ekind is set from the
5249       --  parent type (otherwise Process_Subtype has set the bounds)
5250
5251       if No_Constraint then
5252          Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5253       end if;
5254
5255       --  If we did not have a range constraint, then set the range from the
5256       --  parent type. Otherwise, the call to Process_Subtype has set the
5257       --  bounds.
5258
5259       if No_Constraint
5260         or else not Has_Range_Constraint (Indic)
5261       then
5262          Set_Scalar_Range (Derived_Type,
5263            Make_Range (Loc,
5264              Low_Bound  => New_Copy_Tree (Type_Low_Bound  (Parent_Type)),
5265              High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5266          Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5267
5268          if Has_Infinities (Parent_Type) then
5269             Set_Includes_Infinities (Scalar_Range (Derived_Type));
5270          end if;
5271       end if;
5272
5273       Set_Is_Descendent_Of_Address (Derived_Type,
5274         Is_Descendent_Of_Address (Parent_Type));
5275       Set_Is_Descendent_Of_Address (Implicit_Base,
5276         Is_Descendent_Of_Address (Parent_Type));
5277
5278       --  Set remaining type-specific fields, depending on numeric type
5279
5280       if Is_Modular_Integer_Type (Parent_Type) then
5281          Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5282
5283          Set_Non_Binary_Modulus
5284            (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5285
5286       elsif Is_Floating_Point_Type (Parent_Type) then
5287
5288          --  Digits of base type is always copied from the digits value of
5289          --  the parent base type, but the digits of the derived type will
5290          --  already have been set if there was a constraint present.
5291
5292          Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5293          Set_Vax_Float    (Implicit_Base, Vax_Float    (Parent_Base));
5294
5295          if No_Constraint then
5296             Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5297          end if;
5298
5299       elsif Is_Fixed_Point_Type (Parent_Type) then
5300
5301          --  Small of base type and derived type are always copied from the
5302          --  parent base type, since smalls never change. The delta of the
5303          --  base type is also copied from the parent base type. However the
5304          --  delta of the derived type will have been set already if a
5305          --  constraint was present.
5306
5307          Set_Small_Value (Derived_Type,  Small_Value (Parent_Base));
5308          Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5309          Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
5310
5311          if No_Constraint then
5312             Set_Delta_Value (Derived_Type,  Delta_Value (Parent_Type));
5313          end if;
5314
5315          --  The scale and machine radix in the decimal case are always
5316          --  copied from the parent base type.
5317
5318          if Is_Decimal_Fixed_Point_Type (Parent_Type) then
5319             Set_Scale_Value (Derived_Type,  Scale_Value (Parent_Base));
5320             Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
5321
5322             Set_Machine_Radix_10
5323               (Derived_Type,  Machine_Radix_10 (Parent_Base));
5324             Set_Machine_Radix_10
5325               (Implicit_Base, Machine_Radix_10 (Parent_Base));
5326
5327             Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5328
5329             if No_Constraint then
5330                Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
5331
5332             else
5333                --  the analysis of the subtype_indication sets the
5334                --  digits value of the derived type.
5335
5336                null;
5337             end if;
5338          end if;
5339       end if;
5340
5341       --  The type of the bounds is that of the parent type, and they
5342       --  must be converted to the derived type.
5343
5344       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
5345
5346       --  The implicit_base should be frozen when the derived type is frozen,
5347       --  but note that it is used in the conversions of the bounds. For fixed
5348       --  types we delay the determination of the bounds until the proper
5349       --  freezing point. For other numeric types this is rejected by GCC, for
5350       --  reasons that are currently unclear (???), so we choose to freeze the
5351       --  implicit base now. In the case of integers and floating point types
5352       --  this is harmless because subsequent representation clauses cannot
5353       --  affect anything, but it is still baffling that we cannot use the
5354       --  same mechanism for all derived numeric types.
5355
5356       --  There is a further complication: actually *some* representation
5357       --  clauses can affect the implicit base type. Namely, attribute
5358       --  definition clauses for stream-oriented attributes need to set the
5359       --  corresponding TSS entries on the base type, and this normally cannot
5360       --  be done after the base type is frozen, so the circuitry in
5361       --  Sem_Ch13.New_Stream_Subprogram must account for this possibility and
5362       --  not use Set_TSS in this case.
5363
5364       if Is_Fixed_Point_Type (Parent_Type) then
5365          Conditional_Delay (Implicit_Base, Parent_Type);
5366       else
5367          Freeze_Before (N, Implicit_Base);
5368       end if;
5369    end Build_Derived_Numeric_Type;
5370
5371    --------------------------------
5372    -- Build_Derived_Private_Type --
5373    --------------------------------
5374
5375    procedure Build_Derived_Private_Type
5376      (N             : Node_Id;
5377       Parent_Type   : Entity_Id;
5378       Derived_Type  : Entity_Id;
5379       Is_Completion : Boolean;
5380       Derive_Subps  : Boolean := True)
5381    is
5382       Der_Base    : Entity_Id;
5383       Discr       : Entity_Id;
5384       Full_Decl   : Node_Id := Empty;
5385       Full_Der    : Entity_Id;
5386       Full_P      : Entity_Id;
5387       Last_Discr  : Entity_Id;
5388       Par_Scope   : constant Entity_Id := Scope (Base_Type (Parent_Type));
5389       Swapped     : Boolean := False;
5390
5391       procedure Copy_And_Build;
5392       --  Copy derived type declaration, replace parent with its full view,
5393       --  and analyze new declaration.
5394
5395       --------------------
5396       -- Copy_And_Build --
5397       --------------------
5398
5399       procedure Copy_And_Build is
5400          Full_N : Node_Id;
5401
5402       begin
5403          if Ekind (Parent_Type) in Record_Kind
5404            or else
5405              (Ekind (Parent_Type) in Enumeration_Kind
5406                and then not Is_Standard_Character_Type (Parent_Type)
5407                and then not Is_Generic_Type (Root_Type (Parent_Type)))
5408          then
5409             Full_N := New_Copy_Tree (N);
5410             Insert_After (N, Full_N);
5411             Build_Derived_Type (
5412               Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
5413
5414          else
5415             Build_Derived_Type (
5416               N, Parent_Type, Full_Der, True, Derive_Subps => False);
5417          end if;
5418       end Copy_And_Build;
5419
5420    --  Start of processing for Build_Derived_Private_Type
5421
5422    begin
5423       if Is_Tagged_Type (Parent_Type) then
5424          Build_Derived_Record_Type
5425            (N, Parent_Type, Derived_Type, Derive_Subps);
5426          return;
5427
5428       elsif Has_Discriminants (Parent_Type) then
5429          if Present (Full_View (Parent_Type)) then
5430             if not Is_Completion then
5431
5432                --  Copy declaration for subsequent analysis, to provide a
5433                --  completion for what is a private declaration. Indicate that
5434                --  the full type is internally generated.
5435
5436                Full_Decl := New_Copy_Tree (N);
5437                Full_Der  := New_Copy (Derived_Type);
5438                Set_Comes_From_Source (Full_Decl, False);
5439                Set_Comes_From_Source (Full_Der, False);
5440
5441                Insert_After (N, Full_Decl);
5442
5443             else
5444                --  If this is a completion, the full view being built is
5445                --  itself private. We build a subtype of the parent with
5446                --  the same constraints as this full view, to convey to the
5447                --  back end the constrained components and the size of this
5448                --  subtype. If the parent is constrained, its full view can
5449                --  serve as the underlying full view of the derived type.
5450
5451                if No (Discriminant_Specifications (N)) then
5452                   if Nkind (Subtype_Indication (Type_Definition (N))) =
5453                                                         N_Subtype_Indication
5454                   then
5455                      Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
5456
5457                   elsif Is_Constrained (Full_View (Parent_Type)) then
5458                      Set_Underlying_Full_View (Derived_Type,
5459                        Full_View (Parent_Type));
5460                   end if;
5461
5462                else
5463                   --  If there are new discriminants, the parent subtype is
5464                   --  constrained by them, but it is not clear how to build
5465                   --  the underlying_full_view in this case ???
5466
5467                   null;
5468                end if;
5469             end if;
5470          end if;
5471
5472          --  Build partial view of derived type from partial view of parent
5473
5474          Build_Derived_Record_Type
5475            (N, Parent_Type, Derived_Type, Derive_Subps);
5476
5477          if Present (Full_View (Parent_Type))
5478            and then not Is_Completion
5479          then
5480             if not In_Open_Scopes (Par_Scope)
5481               or else not In_Same_Source_Unit (N, Parent_Type)
5482             then
5483                --  Swap partial and full views temporarily
5484
5485                Install_Private_Declarations (Par_Scope);
5486                Install_Visible_Declarations (Par_Scope);
5487                Swapped := True;
5488             end if;
5489
5490             --  Build full view of derived type from full view of parent which
5491             --  is now installed. Subprograms have been derived on the partial
5492             --  view, the completion does not derive them anew.
5493
5494             if not Is_Tagged_Type (Parent_Type) then
5495
5496                --  If the parent is itself derived from another private type,
5497                --  installing the private declarations has not affected its
5498                --  privacy status, so use its own full view explicitly.
5499
5500                if Is_Private_Type (Parent_Type) then
5501                   Build_Derived_Record_Type
5502                     (Full_Decl, Full_View (Parent_Type), Full_Der, False);
5503                else
5504                   Build_Derived_Record_Type
5505                     (Full_Decl, Parent_Type, Full_Der, False);
5506                end if;
5507
5508             else
5509                --  If full view of parent is tagged, the completion
5510                --  inherits the proper primitive operations.
5511
5512                Set_Defining_Identifier (Full_Decl, Full_Der);
5513                Build_Derived_Record_Type
5514                  (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
5515                Set_Analyzed (Full_Decl);
5516             end if;
5517
5518             if Swapped then
5519                Uninstall_Declarations (Par_Scope);
5520
5521                if In_Open_Scopes (Par_Scope) then
5522                   Install_Visible_Declarations (Par_Scope);
5523                end if;
5524             end if;
5525
5526             Der_Base := Base_Type (Derived_Type);
5527             Set_Full_View (Derived_Type, Full_Der);
5528             Set_Full_View (Der_Base, Base_Type (Full_Der));
5529
5530             --  Copy the discriminant list from full view to the partial views
5531             --  (base type and its subtype). Gigi requires that the partial
5532             --  and full views have the same discriminants.
5533
5534             --  Note that since the partial view is pointing to discriminants
5535             --  in the full view, their scope will be that of the full view.
5536             --  This might cause some front end problems and need
5537             --  adjustment???
5538
5539             Discr := First_Discriminant (Base_Type (Full_Der));
5540             Set_First_Entity (Der_Base, Discr);
5541
5542             loop
5543                Last_Discr := Discr;
5544                Next_Discriminant (Discr);
5545                exit when No (Discr);
5546             end loop;
5547
5548             Set_Last_Entity (Der_Base, Last_Discr);
5549
5550             Set_First_Entity (Derived_Type, First_Entity (Der_Base));
5551             Set_Last_Entity  (Derived_Type, Last_Entity  (Der_Base));
5552             Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
5553
5554          else
5555             --  If this is a completion, the derived type stays private
5556             --  and there is no need to create a further full view, except
5557             --  in the unusual case when the derivation is nested within a
5558             --  child unit, see below.
5559
5560             null;
5561          end if;
5562
5563       elsif Present (Full_View (Parent_Type))
5564         and then  Has_Discriminants (Full_View (Parent_Type))
5565       then
5566          if Has_Unknown_Discriminants (Parent_Type)
5567            and then Nkind (Subtype_Indication (Type_Definition (N))) =
5568                                                          N_Subtype_Indication
5569          then
5570             Error_Msg_N
5571               ("cannot constrain type with unknown discriminants",
5572                Subtype_Indication (Type_Definition (N)));
5573             return;
5574          end if;
5575
5576          --  If full view of parent is a record type, Build full view as
5577          --  a derivation from the parent's full view. Partial view remains
5578          --  private. For code generation and linking, the full view must
5579          --  have the same public status as the partial one. This full view
5580          --  is only needed if the parent type is in an enclosing scope, so
5581          --  that the full view may actually become visible, e.g. in a child
5582          --  unit. This is both more efficient, and avoids order of freezing
5583          --  problems with the added entities.
5584
5585          if not Is_Private_Type (Full_View (Parent_Type))
5586            and then (In_Open_Scopes (Scope (Parent_Type)))
5587          then
5588             Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
5589                                               Chars (Derived_Type));
5590             Set_Is_Itype (Full_Der);
5591             Set_Has_Private_Declaration (Full_Der);
5592             Set_Has_Private_Declaration (Derived_Type);
5593             Set_Associated_Node_For_Itype (Full_Der, N);
5594             Set_Parent (Full_Der, Parent (Derived_Type));
5595             Set_Full_View (Derived_Type, Full_Der);
5596             Set_Is_Public (Full_Der, Is_Public (Derived_Type));
5597             Full_P := Full_View (Parent_Type);
5598             Exchange_Declarations (Parent_Type);
5599             Copy_And_Build;
5600             Exchange_Declarations (Full_P);
5601
5602          else
5603             Build_Derived_Record_Type
5604               (N, Full_View (Parent_Type), Derived_Type,
5605                 Derive_Subps => False);
5606          end if;
5607
5608          --  In any case, the primitive operations are inherited from
5609          --  the parent type, not from the internal full view.
5610
5611          Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
5612
5613          if Derive_Subps then
5614             Derive_Subprograms (Parent_Type, Derived_Type);
5615          end if;
5616
5617       else
5618          --  Untagged type, No discriminants on either view
5619
5620          if Nkind (Subtype_Indication (Type_Definition (N))) =
5621                                                    N_Subtype_Indication
5622          then
5623             Error_Msg_N
5624               ("illegal constraint on type without discriminants", N);
5625          end if;
5626
5627          if Present (Discriminant_Specifications (N))
5628            and then Present (Full_View (Parent_Type))
5629            and then not Is_Tagged_Type (Full_View (Parent_Type))
5630          then
5631             Error_Msg_N
5632               ("cannot add discriminants to untagged type", N);
5633          end if;
5634
5635          Set_Stored_Constraint (Derived_Type, No_Elist);
5636          Set_Is_Constrained    (Derived_Type, Is_Constrained (Parent_Type));
5637          Set_Is_Controlled     (Derived_Type, Is_Controlled  (Parent_Type));
5638          Set_Has_Controlled_Component
5639                                (Derived_Type, Has_Controlled_Component
5640                                                              (Parent_Type));
5641
5642          --  Direct controlled types do not inherit Finalize_Storage_Only flag
5643
5644          if not Is_Controlled  (Parent_Type) then
5645             Set_Finalize_Storage_Only
5646               (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
5647          end if;
5648
5649          --  Construct the implicit full view by deriving from full view of
5650          --  the parent type. In order to get proper visibility, we install
5651          --  the parent scope and its declarations.
5652
5653          --  ??? if the parent is untagged private and its completion is
5654          --  tagged, this mechanism will not work because we cannot derive
5655          --  from the tagged full view unless we have an extension
5656
5657          if Present (Full_View (Parent_Type))
5658            and then not Is_Tagged_Type (Full_View (Parent_Type))
5659            and then not Is_Completion
5660          then
5661             Full_Der :=
5662               Make_Defining_Identifier (Sloc (Derived_Type),
5663                 Chars => Chars (Derived_Type));
5664             Set_Is_Itype (Full_Der);
5665             Set_Has_Private_Declaration (Full_Der);
5666             Set_Has_Private_Declaration (Derived_Type);
5667             Set_Associated_Node_For_Itype (Full_Der, N);
5668             Set_Parent (Full_Der, Parent (Derived_Type));
5669             Set_Full_View (Derived_Type, Full_Der);
5670
5671             if not In_Open_Scopes (Par_Scope) then
5672                Install_Private_Declarations (Par_Scope);
5673                Install_Visible_Declarations (Par_Scope);
5674                Copy_And_Build;
5675                Uninstall_Declarations (Par_Scope);
5676
5677             --  If parent scope is open and in another unit, and parent has a
5678             --  completion, then the derivation is taking place in the visible
5679             --  part of a child unit. In that case retrieve the full view of
5680             --  the parent momentarily.
5681
5682             elsif not In_Same_Source_Unit (N, Parent_Type) then
5683                Full_P := Full_View (Parent_Type);
5684                Exchange_Declarations (Parent_Type);
5685                Copy_And_Build;
5686                Exchange_Declarations (Full_P);
5687
5688             --  Otherwise it is a local derivation
5689
5690             else
5691                Copy_And_Build;
5692             end if;
5693
5694             Set_Scope                (Full_Der, Current_Scope);
5695             Set_Is_First_Subtype     (Full_Der,
5696                                        Is_First_Subtype (Derived_Type));
5697             Set_Has_Size_Clause      (Full_Der, False);
5698             Set_Has_Alignment_Clause (Full_Der, False);
5699             Set_Next_Entity          (Full_Der, Empty);
5700             Set_Has_Delayed_Freeze   (Full_Der);
5701             Set_Is_Frozen            (Full_Der, False);
5702             Set_Freeze_Node          (Full_Der, Empty);
5703             Set_Depends_On_Private   (Full_Der,
5704                                         Has_Private_Component    (Full_Der));
5705             Set_Public_Status        (Full_Der);
5706          end if;
5707       end if;
5708
5709       Set_Has_Unknown_Discriminants (Derived_Type,
5710         Has_Unknown_Discriminants (Parent_Type));
5711
5712       if Is_Private_Type (Derived_Type) then
5713          Set_Private_Dependents (Derived_Type, New_Elmt_List);
5714       end if;
5715
5716       if Is_Private_Type (Parent_Type)
5717         and then Base_Type (Parent_Type) = Parent_Type
5718         and then In_Open_Scopes (Scope (Parent_Type))
5719       then
5720          Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
5721
5722          if Is_Child_Unit (Scope (Current_Scope))
5723            and then Is_Completion
5724            and then In_Private_Part (Current_Scope)
5725            and then Scope (Parent_Type) /= Current_Scope
5726          then
5727             --  This is the unusual case where a type completed by a private
5728             --  derivation occurs within a package nested in a child unit,
5729             --  and the parent is declared in an ancestor. In this case, the
5730             --  full view of the parent type will become visible in the body
5731             --  of the enclosing child, and only then will the current type
5732             --  be possibly non-private. We build a underlying full view that
5733             --  will be installed when the enclosing child body is compiled.
5734
5735             Full_Der :=
5736               Make_Defining_Identifier (Sloc (Derived_Type),
5737                 Chars => Chars (Derived_Type));
5738             Set_Is_Itype (Full_Der);
5739             Build_Itype_Reference (Full_Der, N);
5740
5741             --  The full view will be used to swap entities on entry/exit to
5742             --  the body, and must appear in the entity list for the package.
5743
5744             Append_Entity (Full_Der, Scope (Derived_Type));
5745             Set_Has_Private_Declaration (Full_Der);
5746             Set_Has_Private_Declaration (Derived_Type);
5747             Set_Associated_Node_For_Itype (Full_Der, N);
5748             Set_Parent (Full_Der, Parent (Derived_Type));
5749             Full_P := Full_View (Parent_Type);
5750             Exchange_Declarations (Parent_Type);
5751             Copy_And_Build;
5752             Exchange_Declarations (Full_P);
5753             Set_Underlying_Full_View (Derived_Type, Full_Der);
5754          end if;
5755       end if;
5756    end Build_Derived_Private_Type;
5757
5758    -------------------------------
5759    -- Build_Derived_Record_Type --
5760    -------------------------------
5761
5762    --  1. INTRODUCTION
5763
5764    --  Ideally we would like to use the same model of type derivation for
5765    --  tagged and untagged record types. Unfortunately this is not quite
5766    --  possible because the semantics of representation clauses is different
5767    --  for tagged and untagged records under inheritance. Consider the
5768    --  following:
5769
5770    --     type R (...) is [tagged] record ... end record;
5771    --     type T (...) is new R (...) [with ...];
5772
5773    --  The representation clauses for T can specify a completely different
5774    --  record layout from R's. Hence the same component can be placed in two
5775    --  very different positions in objects of type T and R. If R and are tagged
5776    --  types, representation clauses for T can only specify the layout of non
5777    --  inherited components, thus components that are common in R and T have
5778    --  the same position in objects of type R and T.
5779
5780    --  This has two implications. The first is that the entire tree for R's
5781    --  declaration needs to be copied for T in the untagged case, so that T
5782    --  can be viewed as a record type of its own with its own representation
5783    --  clauses. The second implication is the way we handle discriminants.
5784    --  Specifically, in the untagged case we need a way to communicate to Gigi
5785    --  what are the real discriminants in the record, while for the semantics
5786    --  we need to consider those introduced by the user to rename the
5787    --  discriminants in the parent type. This is handled by introducing the
5788    --  notion of stored discriminants. See below for more.
5789
5790    --  Fortunately the way regular components are inherited can be handled in
5791    --  the same way in tagged and untagged types.
5792
5793    --  To complicate things a bit more the private view of a private extension
5794    --  cannot be handled in the same way as the full view (for one thing the
5795    --  semantic rules are somewhat different). We will explain what differs
5796    --  below.
5797
5798    --  2. DISCRIMINANTS UNDER INHERITANCE
5799
5800    --  The semantic rules governing the discriminants of derived types are
5801    --  quite subtle.
5802
5803    --   type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
5804    --      [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
5805
5806    --  If parent type has discriminants, then the discriminants that are
5807    --  declared in the derived type are [3.4 (11)]:
5808
5809    --  o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
5810    --    there is one;
5811
5812    --  o Otherwise, each discriminant of the parent type (implicitly declared
5813    --    in the same order with the same specifications). In this case, the
5814    --    discriminants are said to be "inherited", or if unknown in the parent
5815    --    are also unknown in the derived type.
5816
5817    --  Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
5818
5819    --  o The parent subtype shall be constrained;
5820
5821    --  o If the parent type is not a tagged type, then each discriminant of
5822    --    the derived type shall be used in the constraint defining a parent
5823    --    subtype. [Implementation note: This ensures that the new discriminant
5824    --    can share storage with an existing discriminant.]
5825
5826    --  For the derived type each discriminant of the parent type is either
5827    --  inherited, constrained to equal some new discriminant of the derived
5828    --  type, or constrained to the value of an expression.
5829
5830    --  When inherited or constrained to equal some new discriminant, the
5831    --  parent discriminant and the discriminant of the derived type are said
5832    --  to "correspond".
5833
5834    --  If a discriminant of the parent type is constrained to a specific value
5835    --  in the derived type definition, then the discriminant is said to be
5836    --  "specified" by that derived type definition.
5837
5838    --  3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
5839
5840    --  We have spoken about stored discriminants in point 1 (introduction)
5841    --  above. There are two sort of stored discriminants: implicit and
5842    --  explicit. As long as the derived type inherits the same discriminants as
5843    --  the root record type, stored discriminants are the same as regular
5844    --  discriminants, and are said to be implicit. However, if any discriminant
5845    --  in the root type was renamed in the derived type, then the derived
5846    --  type will contain explicit stored discriminants. Explicit stored
5847    --  discriminants are discriminants in addition to the semantically visible
5848    --  discriminants defined for the derived type. Stored discriminants are
5849    --  used by Gigi to figure out what are the physical discriminants in
5850    --  objects of the derived type (see precise definition in einfo.ads).
5851    --  As an example, consider the following:
5852
5853    --           type R  (D1, D2, D3 : Int) is record ... end record;
5854    --           type T1 is new R;
5855    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
5856    --           type T3 is new T2;
5857    --           type T4 (Y : Int) is new T3 (Y, 99);
5858
5859    --  The following table summarizes the discriminants and stored
5860    --  discriminants in R and T1 through T4.
5861
5862    --   Type      Discrim     Stored Discrim  Comment
5863    --    R      (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in R
5864    --    T1     (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in T1
5865    --    T2     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T2
5866    --    T3     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T3
5867    --    T4     (Y)            (D1, D2, D3)   Girder discrims EXPLICIT in T4
5868
5869    --  Field Corresponding_Discriminant (abbreviated CD below) allows us to
5870    --  find the corresponding discriminant in the parent type, while
5871    --  Original_Record_Component (abbreviated ORC below), the actual physical
5872    --  component that is renamed. Finally the field Is_Completely_Hidden
5873    --  (abbreviated ICH below) is set for all explicit stored discriminants
5874    --  (see einfo.ads for more info). For the above example this gives:
5875
5876    --                 Discrim     CD        ORC     ICH
5877    --                 ^^^^^^^     ^^        ^^^     ^^^
5878    --                 D1 in R    empty     itself    no
5879    --                 D2 in R    empty     itself    no
5880    --                 D3 in R    empty     itself    no
5881
5882    --                 D1 in T1  D1 in R    itself    no
5883    --                 D2 in T1  D2 in R    itself    no
5884    --                 D3 in T1  D3 in R    itself    no
5885
5886    --                 X1 in T2  D3 in T1  D3 in T2   no
5887    --                 X2 in T2  D1 in T1  D1 in T2   no
5888    --                 D1 in T2   empty    itself    yes
5889    --                 D2 in T2   empty    itself    yes
5890    --                 D3 in T2   empty    itself    yes
5891
5892    --                 X1 in T3  X1 in T2  D3 in T3   no
5893    --                 X2 in T3  X2 in T2  D1 in T3   no
5894    --                 D1 in T3   empty    itself    yes
5895    --                 D2 in T3   empty    itself    yes
5896    --                 D3 in T3   empty    itself    yes
5897
5898    --                 Y  in T4  X1 in T3  D3 in T3   no
5899    --                 D1 in T3   empty    itself    yes
5900    --                 D2 in T3   empty    itself    yes
5901    --                 D3 in T3   empty    itself    yes
5902
5903    --  4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
5904
5905    --  Type derivation for tagged types is fairly straightforward. If no
5906    --  discriminants are specified by the derived type, these are inherited
5907    --  from the parent. No explicit stored discriminants are ever necessary.
5908    --  The only manipulation that is done to the tree is that of adding a
5909    --  _parent field with parent type and constrained to the same constraint
5910    --  specified for the parent in the derived type definition. For instance:
5911
5912    --           type R  (D1, D2, D3 : Int) is tagged record ... end record;
5913    --           type T1 is new R with null record;
5914    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
5915
5916    --  are changed into:
5917
5918    --           type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
5919    --              _parent : R (D1, D2, D3);
5920    --           end record;
5921
5922    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
5923    --              _parent : T1 (X2, 88, X1);
5924    --           end record;
5925
5926    --  The discriminants actually present in R, T1 and T2 as well as their CD,
5927    --  ORC and ICH fields are:
5928
5929    --                 Discrim     CD        ORC     ICH
5930    --                 ^^^^^^^     ^^        ^^^     ^^^
5931    --                 D1 in R    empty     itself    no
5932    --                 D2 in R    empty     itself    no
5933    --                 D3 in R    empty     itself    no
5934
5935    --                 D1 in T1  D1 in R    D1 in R   no
5936    --                 D2 in T1  D2 in R    D2 in R   no
5937    --                 D3 in T1  D3 in R    D3 in R   no
5938
5939    --                 X1 in T2  D3 in T1   D3 in R   no
5940    --                 X2 in T2  D1 in T1   D1 in R   no
5941
5942    --  5. FIRST TRANSFORMATION FOR DERIVED RECORDS
5943    --
5944    --  Regardless of whether we dealing with a tagged or untagged type
5945    --  we will transform all derived type declarations of the form
5946    --
5947    --               type T is new R (...) [with ...];
5948    --  or
5949    --               subtype S is R (...);
5950    --               type T is new S [with ...];
5951    --  into
5952    --               type BT is new R [with ...];
5953    --               subtype T is BT (...);
5954    --
5955    --  That is, the base derived type is constrained only if it has no
5956    --  discriminants. The reason for doing this is that GNAT's semantic model
5957    --  assumes that a base type with discriminants is unconstrained.
5958    --
5959    --  Note that, strictly speaking, the above transformation is not always
5960    --  correct. Consider for instance the following excerpt from ACVC b34011a:
5961    --
5962    --       procedure B34011A is
5963    --          type REC (D : integer := 0) is record
5964    --             I : Integer;
5965    --          end record;
5966
5967    --          package P is
5968    --             type T6 is new Rec;
5969    --             function F return T6;
5970    --          end P;
5971
5972    --          use P;
5973    --          package Q6 is
5974    --             type U is new T6 (Q6.F.I);                   -- ERROR: Q6.F.
5975    --          end Q6;
5976    --
5977    --  The definition of Q6.U is illegal. However transforming Q6.U into
5978
5979    --             type BaseU is new T6;
5980    --             subtype U is BaseU (Q6.F.I)
5981
5982    --  turns U into a legal subtype, which is incorrect. To avoid this problem
5983    --  we always analyze the constraint (in this case (Q6.F.I)) before applying
5984    --  the transformation described above.
5985
5986    --  There is another instance where the above transformation is incorrect.
5987    --  Consider:
5988
5989    --          package Pack is
5990    --             type Base (D : Integer) is tagged null record;
5991    --             procedure P (X : Base);
5992
5993    --             type Der is new Base (2) with null record;
5994    --             procedure P (X : Der);
5995    --          end Pack;
5996
5997    --  Then the above transformation turns this into
5998
5999    --             type Der_Base is new Base with null record;
6000    --             --  procedure P (X : Base) is implicitly inherited here
6001    --             --  as procedure P (X : Der_Base).
6002
6003    --             subtype Der is Der_Base (2);
6004    --             procedure P (X : Der);
6005    --             --  The overriding of P (X : Der_Base) is illegal since we
6006    --             --  have a parameter conformance problem.
6007
6008    --  To get around this problem, after having semantically processed Der_Base
6009    --  and the rewritten subtype declaration for Der, we copy Der_Base field
6010    --  Discriminant_Constraint from Der so that when parameter conformance is
6011    --  checked when P is overridden, no semantic errors are flagged.
6012
6013    --  6. SECOND TRANSFORMATION FOR DERIVED RECORDS
6014
6015    --  Regardless of whether we are dealing with a tagged or untagged type
6016    --  we will transform all derived type declarations of the form
6017
6018    --               type R (D1, .., Dn : ...) is [tagged] record ...;
6019    --               type T is new R [with ...];
6020    --  into
6021    --               type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
6022
6023    --  The reason for such transformation is that it allows us to implement a
6024    --  very clean form of component inheritance as explained below.
6025
6026    --  Note that this transformation is not achieved by direct tree rewriting
6027    --  and manipulation, but rather by redoing the semantic actions that the
6028    --  above transformation will entail. This is done directly in routine
6029    --  Inherit_Components.
6030
6031    --  7. TYPE DERIVATION AND COMPONENT INHERITANCE
6032
6033    --  In both tagged and untagged derived types, regular non discriminant
6034    --  components are inherited in the derived type from the parent type. In
6035    --  the absence of discriminants component, inheritance is straightforward
6036    --  as components can simply be copied from the parent.
6037
6038    --  If the parent has discriminants, inheriting components constrained with
6039    --  these discriminants requires caution. Consider the following example:
6040
6041    --      type R  (D1, D2 : Positive) is [tagged] record
6042    --         S : String (D1 .. D2);
6043    --      end record;
6044
6045    --      type T1                is new R        [with null record];
6046    --      type T2 (X : positive) is new R (1, X) [with null record];
6047
6048    --  As explained in 6. above, T1 is rewritten as
6049    --      type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
6050    --  which makes the treatment for T1 and T2 identical.
6051
6052    --  What we want when inheriting S, is that references to D1 and D2 in R are
6053    --  replaced with references to their correct constraints, i.e. D1 and D2 in
6054    --  T1 and 1 and X in T2. So all R's discriminant references are replaced
6055    --  with either discriminant references in the derived type or expressions.
6056    --  This replacement is achieved as follows: before inheriting R's
6057    --  components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
6058    --  created in the scope of T1 (resp. scope of T2) so that discriminants D1
6059    --  and D2 of T1 are visible (resp. discriminant X of T2 is visible).
6060    --  For T2, for instance, this has the effect of replacing String (D1 .. D2)
6061    --  by String (1 .. X).
6062
6063    --  8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
6064
6065    --  We explain here the rules governing private type extensions relevant to
6066    --  type derivation. These rules are explained on the following example:
6067
6068    --      type D [(...)] is new A [(...)] with private;      <-- partial view
6069    --      type D [(...)] is new P [(...)] with null record;  <-- full view
6070
6071    --  Type A is called the ancestor subtype of the private extension.
6072    --  Type P is the parent type of the full view of the private extension. It
6073    --  must be A or a type derived from A.
6074
6075    --  The rules concerning the discriminants of private type extensions are
6076    --  [7.3(10-13)]:
6077
6078    --  o If a private extension inherits known discriminants from the ancestor
6079    --    subtype, then the full view shall also inherit its discriminants from
6080    --    the ancestor subtype and the parent subtype of the full view shall be
6081    --    constrained if and only if the ancestor subtype is constrained.
6082
6083    --  o If a partial view has unknown discriminants, then the full view may
6084    --    define a definite or an indefinite subtype, with or without
6085    --    discriminants.
6086
6087    --  o If a partial view has neither known nor unknown discriminants, then
6088    --    the full view shall define a definite subtype.
6089
6090    --  o If the ancestor subtype of a private extension has constrained
6091    --    discriminants, then the parent subtype of the full view shall impose a
6092    --    statically matching constraint on those discriminants.
6093
6094    --  This means that only the following forms of private extensions are
6095    --  allowed:
6096
6097    --      type D is new A with private;      <-- partial view
6098    --      type D is new P with null record;  <-- full view
6099
6100    --  If A has no discriminants than P has no discriminants, otherwise P must
6101    --  inherit A's discriminants.
6102
6103    --      type D is new A (...) with private;      <-- partial view
6104    --      type D is new P (:::) with null record;  <-- full view
6105
6106    --  P must inherit A's discriminants and (...) and (:::) must statically
6107    --  match.
6108
6109    --      subtype A is R (...);
6110    --      type D is new A with private;      <-- partial view
6111    --      type D is new P with null record;  <-- full view
6112
6113    --  P must have inherited R's discriminants and must be derived from A or
6114    --  any of its subtypes.
6115
6116    --      type D (..) is new A with private;              <-- partial view
6117    --      type D (..) is new P [(:::)] with null record;  <-- full view
6118
6119    --  No specific constraints on P's discriminants or constraint (:::).
6120    --  Note that A can be unconstrained, but the parent subtype P must either
6121    --  be constrained or (:::) must be present.
6122
6123    --      type D (..) is new A [(...)] with private;      <-- partial view
6124    --      type D (..) is new P [(:::)] with null record;  <-- full view
6125
6126    --  P's constraints on A's discriminants must statically match those
6127    --  imposed by (...).
6128
6129    --  9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6130
6131    --  The full view of a private extension is handled exactly as described
6132    --  above. The model chose for the private view of a private extension is
6133    --  the same for what concerns discriminants (i.e. they receive the same
6134    --  treatment as in the tagged case). However, the private view of the
6135    --  private extension always inherits the components of the parent base,
6136    --  without replacing any discriminant reference. Strictly speaking this is
6137    --  incorrect. However, Gigi never uses this view to generate code so this
6138    --  is a purely semantic issue. In theory, a set of transformations similar
6139    --  to those given in 5. and 6. above could be applied to private views of
6140    --  private extensions to have the same model of component inheritance as
6141    --  for non private extensions. However, this is not done because it would
6142    --  further complicate private type processing. Semantically speaking, this
6143    --  leaves us in an uncomfortable situation. As an example consider:
6144
6145    --          package Pack is
6146    --             type R (D : integer) is tagged record
6147    --                S : String (1 .. D);
6148    --             end record;
6149    --             procedure P (X : R);
6150    --             type T is new R (1) with private;
6151    --          private
6152    --             type T is new R (1) with null record;
6153    --          end;
6154
6155    --  This is transformed into:
6156
6157    --          package Pack is
6158    --             type R (D : integer) is tagged record
6159    --                S : String (1 .. D);
6160    --             end record;
6161    --             procedure P (X : R);
6162    --             type T is new R (1) with private;
6163    --          private
6164    --             type BaseT is new R with null record;
6165    --             subtype  T is BaseT (1);
6166    --          end;
6167
6168    --  (strictly speaking the above is incorrect Ada)
6169
6170    --  From the semantic standpoint the private view of private extension T
6171    --  should be flagged as constrained since one can clearly have
6172    --
6173    --             Obj : T;
6174    --
6175    --  in a unit withing Pack. However, when deriving subprograms for the
6176    --  private view of private extension T, T must be seen as unconstrained
6177    --  since T has discriminants (this is a constraint of the current
6178    --  subprogram derivation model). Thus, when processing the private view of
6179    --  a private extension such as T, we first mark T as unconstrained, we
6180    --  process it, we perform program derivation and just before returning from
6181    --  Build_Derived_Record_Type we mark T as constrained.
6182
6183    --  ??? Are there are other uncomfortable cases that we will have to
6184    --      deal with.
6185
6186    --  10. RECORD_TYPE_WITH_PRIVATE complications
6187
6188    --  Types that are derived from a visible record type and have a private
6189    --  extension present other peculiarities. They behave mostly like private
6190    --  types, but if they have primitive operations defined, these will not
6191    --  have the proper signatures for further inheritance, because other
6192    --  primitive operations will use the implicit base that we define for
6193    --  private derivations below. This affect subprogram inheritance (see
6194    --  Derive_Subprograms for details). We also derive the implicit base from
6195    --  the base type of the full view, so that the implicit base is a record
6196    --  type and not another private type, This avoids infinite loops.
6197
6198    procedure Build_Derived_Record_Type
6199      (N            : Node_Id;
6200       Parent_Type  : Entity_Id;
6201       Derived_Type : Entity_Id;
6202       Derive_Subps : Boolean := True)
6203    is
6204       Loc          : constant Source_Ptr := Sloc (N);
6205       Parent_Base  : Entity_Id;
6206       Type_Def     : Node_Id;
6207       Indic        : Node_Id;
6208       Discrim      : Entity_Id;
6209       Last_Discrim : Entity_Id;
6210       Constrs      : Elist_Id;
6211
6212       Discs : Elist_Id := New_Elmt_List;
6213       --  An empty Discs list means that there were no constraints in the
6214       --  subtype indication or that there was an error processing it.
6215
6216       Assoc_List : Elist_Id;
6217       New_Discrs : Elist_Id;
6218       New_Base   : Entity_Id;
6219       New_Decl   : Node_Id;
6220       New_Indic  : Node_Id;
6221
6222       Is_Tagged          : constant Boolean := Is_Tagged_Type (Parent_Type);
6223       Discriminant_Specs : constant Boolean :=
6224                              Present (Discriminant_Specifications (N));
6225       Private_Extension  : constant Boolean :=
6226                              Nkind (N) = N_Private_Extension_Declaration;
6227
6228       Constraint_Present : Boolean;
6229       Inherit_Discrims   : Boolean := False;
6230       Save_Etype         : Entity_Id;
6231       Save_Discr_Constr  : Elist_Id;
6232       Save_Next_Entity   : Entity_Id;
6233
6234    begin
6235       if Ekind (Parent_Type) = E_Record_Type_With_Private
6236         and then Present (Full_View (Parent_Type))
6237         and then Has_Discriminants (Parent_Type)
6238       then
6239          Parent_Base := Base_Type (Full_View (Parent_Type));
6240       else
6241          Parent_Base := Base_Type (Parent_Type);
6242       end if;
6243
6244       --  Before we start the previously documented transformations, here is
6245       --  little fix for size and alignment of tagged types. Normally when we
6246       --  derive type D from type P, we copy the size and alignment of P as the
6247       --  default for D, and in the absence of explicit representation clauses
6248       --  for D, the size and alignment are indeed the same as the parent.
6249
6250       --  But this is wrong for tagged types, since fields may be added, and
6251       --  the default size may need to be larger, and the default alignment may
6252       --  need to be larger.
6253
6254       --  We therefore reset the size and alignment fields in the tagged case.
6255       --  Note that the size and alignment will in any case be at least as
6256       --  large as the parent type (since the derived type has a copy of the
6257       --  parent type in the _parent field)
6258
6259       --  The type is also marked as being tagged here, which is needed when
6260       --  processing components with a self-referential anonymous access type
6261       --  in the call to Check_Anonymous_Access_Components below. Note that
6262       --  this flag is also set later on for completeness.
6263
6264       if Is_Tagged then
6265          Set_Is_Tagged_Type (Derived_Type);
6266          Init_Size_Align    (Derived_Type);
6267       end if;
6268
6269       --  STEP 0a: figure out what kind of derived type declaration we have
6270
6271       if Private_Extension then
6272          Type_Def := N;
6273          Set_Ekind (Derived_Type, E_Record_Type_With_Private);
6274
6275       else
6276          Type_Def := Type_Definition (N);
6277
6278          --  Ekind (Parent_Base) is not necessarily E_Record_Type since
6279          --  Parent_Base can be a private type or private extension. However,
6280          --  for tagged types with an extension the newly added fields are
6281          --  visible and hence the Derived_Type is always an E_Record_Type.
6282          --  (except that the parent may have its own private fields).
6283          --  For untagged types we preserve the Ekind of the Parent_Base.
6284
6285          if Present (Record_Extension_Part (Type_Def)) then
6286             Set_Ekind (Derived_Type, E_Record_Type);
6287
6288             --  Create internal access types for components with anonymous
6289             --  access types.
6290
6291             if Ada_Version >= Ada_05 then
6292                Check_Anonymous_Access_Components
6293                  (N, Derived_Type, Derived_Type,
6294                    Component_List (Record_Extension_Part (Type_Def)));
6295             end if;
6296
6297          else
6298             Set_Ekind (Derived_Type, Ekind (Parent_Base));
6299          end if;
6300       end if;
6301
6302       --  Indic can either be an N_Identifier if the subtype indication
6303       --  contains no constraint or an N_Subtype_Indication if the subtype
6304       --  indication has a constraint.
6305
6306       Indic := Subtype_Indication (Type_Def);
6307       Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
6308
6309       --  Check that the type has visible discriminants. The type may be
6310       --  a private type with unknown discriminants whose full view has
6311       --  discriminants which are invisible.
6312
6313       if Constraint_Present then
6314          if not Has_Discriminants (Parent_Base)
6315            or else
6316              (Has_Unknown_Discriminants (Parent_Base)
6317                 and then Is_Private_Type (Parent_Base))
6318          then
6319             Error_Msg_N
6320               ("invalid constraint: type has no discriminant",
6321                  Constraint (Indic));
6322
6323             Constraint_Present := False;
6324             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6325
6326          elsif Is_Constrained (Parent_Type) then
6327             Error_Msg_N
6328                ("invalid constraint: parent type is already constrained",
6329                   Constraint (Indic));
6330
6331             Constraint_Present := False;
6332             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6333          end if;
6334       end if;
6335
6336       --  STEP 0b: If needed, apply transformation given in point 5. above
6337
6338       if not Private_Extension
6339         and then Has_Discriminants (Parent_Type)
6340         and then not Discriminant_Specs
6341         and then (Is_Constrained (Parent_Type) or else Constraint_Present)
6342       then
6343          --  First, we must analyze the constraint (see comment in point 5.)
6344
6345          if Constraint_Present then
6346             New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
6347
6348             if Has_Discriminants (Derived_Type)
6349               and then Has_Private_Declaration (Derived_Type)
6350               and then Present (Discriminant_Constraint (Derived_Type))
6351             then
6352                --  Verify that constraints of the full view statically match
6353                --  those given in the partial view.
6354
6355                declare
6356                   C1, C2 : Elmt_Id;
6357
6358                begin
6359                   C1 := First_Elmt (New_Discrs);
6360                   C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
6361                   while Present (C1) and then Present (C2) loop
6362                      if Fully_Conformant_Expressions (Node (C1), Node (C2))
6363                        or else
6364                          (Is_OK_Static_Expression (Node (C1))
6365                             and then
6366                           Is_OK_Static_Expression (Node (C2))
6367                             and then
6368                           Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
6369                      then
6370                         null;
6371
6372                      else
6373                         Error_Msg_N (
6374                           "constraint not conformant to previous declaration",
6375                              Node (C1));
6376                      end if;
6377
6378                      Next_Elmt (C1);
6379                      Next_Elmt (C2);
6380                   end loop;
6381                end;
6382             end if;
6383          end if;
6384
6385          --  Insert and analyze the declaration for the unconstrained base type
6386
6387          New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6388
6389          New_Decl :=
6390            Make_Full_Type_Declaration (Loc,
6391               Defining_Identifier => New_Base,
6392               Type_Definition     =>
6393                 Make_Derived_Type_Definition (Loc,
6394                   Abstract_Present      => Abstract_Present (Type_Def),
6395                   Subtype_Indication    =>
6396                     New_Occurrence_Of (Parent_Base, Loc),
6397                   Record_Extension_Part =>
6398                     Relocate_Node (Record_Extension_Part (Type_Def))));
6399
6400          Set_Parent (New_Decl, Parent (N));
6401          Mark_Rewrite_Insertion (New_Decl);
6402          Insert_Before (N, New_Decl);
6403
6404          --  Note that this call passes False for the Derive_Subps parameter
6405          --  because subprogram derivation is deferred until after creating
6406          --  the subtype (see below).
6407
6408          Build_Derived_Type
6409            (New_Decl, Parent_Base, New_Base,
6410             Is_Completion => True, Derive_Subps => False);
6411
6412          --  ??? This needs re-examination to determine whether the
6413          --  above call can simply be replaced by a call to Analyze.
6414
6415          Set_Analyzed (New_Decl);
6416
6417          --  Insert and analyze the declaration for the constrained subtype
6418
6419          if Constraint_Present then
6420             New_Indic :=
6421               Make_Subtype_Indication (Loc,
6422                 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6423                 Constraint   => Relocate_Node (Constraint (Indic)));
6424
6425          else
6426             declare
6427                Constr_List : constant List_Id := New_List;
6428                C           : Elmt_Id;
6429                Expr        : Node_Id;
6430
6431             begin
6432                C := First_Elmt (Discriminant_Constraint (Parent_Type));
6433                while Present (C) loop
6434                   Expr := Node (C);
6435
6436                   --  It is safe here to call New_Copy_Tree since
6437                   --  Force_Evaluation was called on each constraint in
6438                   --  Build_Discriminant_Constraints.
6439
6440                   Append (New_Copy_Tree (Expr), To => Constr_List);
6441
6442                   Next_Elmt (C);
6443                end loop;
6444
6445                New_Indic :=
6446                  Make_Subtype_Indication (Loc,
6447                    Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6448                    Constraint   =>
6449                      Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
6450             end;
6451          end if;
6452
6453          Rewrite (N,
6454            Make_Subtype_Declaration (Loc,
6455              Defining_Identifier => Derived_Type,
6456              Subtype_Indication  => New_Indic));
6457
6458          Analyze (N);
6459
6460          --  Derivation of subprograms must be delayed until the full subtype
6461          --  has been established to ensure proper overriding of subprograms
6462          --  inherited by full types. If the derivations occurred as part of
6463          --  the call to Build_Derived_Type above, then the check for type
6464          --  conformance would fail because earlier primitive subprograms
6465          --  could still refer to the full type prior the change to the new
6466          --  subtype and hence would not match the new base type created here.
6467
6468          Derive_Subprograms (Parent_Type, Derived_Type);
6469
6470          --  For tagged types the Discriminant_Constraint of the new base itype
6471          --  is inherited from the first subtype so that no subtype conformance
6472          --  problem arise when the first subtype overrides primitive
6473          --  operations inherited by the implicit base type.
6474
6475          if Is_Tagged then
6476             Set_Discriminant_Constraint
6477               (New_Base, Discriminant_Constraint (Derived_Type));
6478          end if;
6479
6480          return;
6481       end if;
6482
6483       --  If we get here Derived_Type will have no discriminants or it will be
6484       --  a discriminated unconstrained base type.
6485
6486       --  STEP 1a: perform preliminary actions/checks for derived tagged types
6487
6488       if Is_Tagged then
6489
6490          --  The parent type is frozen for non-private extensions (RM 13.14(7))
6491          --  The declaration of a specific descendant of an interface type
6492          --  freezes the interface type (RM 13.14).
6493
6494          if not Private_Extension
6495            or else Is_Interface (Parent_Base)
6496          then
6497             Freeze_Before (N, Parent_Type);
6498          end if;
6499
6500          --  In Ada 2005 (AI-344), the restriction that a derived tagged type
6501          --  cannot be declared at a deeper level than its parent type is
6502          --  removed. The check on derivation within a generic body is also
6503          --  relaxed, but there's a restriction that a derived tagged type
6504          --  cannot be declared in a generic body if it's derived directly
6505          --  or indirectly from a formal type of that generic.
6506
6507          if Ada_Version >= Ada_05 then
6508             if Present (Enclosing_Generic_Body (Derived_Type)) then
6509                declare
6510                   Ancestor_Type : Entity_Id;
6511
6512                begin
6513                   --  Check to see if any ancestor of the derived type is a
6514                   --  formal type.
6515
6516                   Ancestor_Type := Parent_Type;
6517                   while not Is_Generic_Type (Ancestor_Type)
6518                     and then Etype (Ancestor_Type) /= Ancestor_Type
6519                   loop
6520                      Ancestor_Type := Etype (Ancestor_Type);
6521                   end loop;
6522
6523                   --  If the derived type does have a formal type as an
6524                   --  ancestor, then it's an error if the derived type is
6525                   --  declared within the body of the generic unit that
6526                   --  declares the formal type in its generic formal part. It's
6527                   --  sufficient to check whether the ancestor type is declared
6528                   --  inside the same generic body as the derived type (such as
6529                   --  within a nested generic spec), in which case the
6530                   --  derivation is legal. If the formal type is declared
6531                   --  outside of that generic body, then it's guaranteed that
6532                   --  the derived type is declared within the generic body of
6533                   --  the generic unit declaring the formal type.
6534
6535                   if Is_Generic_Type (Ancestor_Type)
6536                     and then Enclosing_Generic_Body (Ancestor_Type) /=
6537                                Enclosing_Generic_Body (Derived_Type)
6538                   then
6539                      Error_Msg_NE
6540                        ("parent type of& must not be descendant of formal type"
6541                           & " of an enclosing generic body",
6542                             Indic, Derived_Type);
6543                   end if;
6544                end;
6545             end if;
6546
6547          elsif Type_Access_Level (Derived_Type) /=
6548                  Type_Access_Level (Parent_Type)
6549            and then not Is_Generic_Type (Derived_Type)
6550          then
6551             if Is_Controlled (Parent_Type) then
6552                Error_Msg_N
6553                  ("controlled type must be declared at the library level",
6554                   Indic);
6555             else
6556                Error_Msg_N
6557                  ("type extension at deeper accessibility level than parent",
6558                   Indic);
6559             end if;
6560
6561          else
6562             declare
6563                GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
6564
6565             begin
6566                if Present (GB)
6567                  and then GB /= Enclosing_Generic_Body (Parent_Base)
6568                then
6569                   Error_Msg_NE
6570                     ("parent type of& must not be outside generic body"
6571                        & " (RM 3.9.1(4))",
6572                          Indic, Derived_Type);
6573                end if;
6574             end;
6575          end if;
6576       end if;
6577
6578       --  Ada 2005 (AI-251)
6579
6580       if Ada_Version = Ada_05
6581         and then Is_Tagged
6582       then
6583          --  "The declaration of a specific descendant of an interface type
6584          --  freezes the interface type" (RM 13.14).
6585
6586          declare
6587             Iface : Node_Id;
6588          begin
6589             if Is_Non_Empty_List (Interface_List (Type_Def)) then
6590                Iface := First (Interface_List (Type_Def));
6591                while Present (Iface) loop
6592                   Freeze_Before (N, Etype (Iface));
6593                   Next (Iface);
6594                end loop;
6595             end if;
6596          end;
6597       end if;
6598
6599       --  STEP 1b : preliminary cleanup of the full view of private types
6600
6601       --  If the type is already marked as having discriminants, then it's the
6602       --  completion of a private type or private extension and we need to
6603       --  retain the discriminants from the partial view if the current
6604       --  declaration has Discriminant_Specifications so that we can verify
6605       --  conformance. However, we must remove any existing components that
6606       --  were inherited from the parent (and attached in Copy_And_Swap)
6607       --  because the full type inherits all appropriate components anyway, and
6608       --  we do not want the partial view's components interfering.
6609
6610       if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
6611          Discrim := First_Discriminant (Derived_Type);
6612          loop
6613             Last_Discrim := Discrim;
6614             Next_Discriminant (Discrim);
6615             exit when No (Discrim);
6616          end loop;
6617
6618          Set_Last_Entity (Derived_Type, Last_Discrim);
6619
6620       --  In all other cases wipe out the list of inherited components (even
6621       --  inherited discriminants), it will be properly rebuilt here.
6622
6623       else
6624          Set_First_Entity (Derived_Type, Empty);
6625          Set_Last_Entity  (Derived_Type, Empty);
6626       end if;
6627
6628       --  STEP 1c: Initialize some flags for the Derived_Type
6629
6630       --  The following flags must be initialized here so that
6631       --  Process_Discriminants can check that discriminants of tagged types do
6632       --  not have a default initial value and that access discriminants are
6633       --  only specified for limited records. For completeness, these flags are
6634       --  also initialized along with all the other flags below.
6635
6636       --  AI-419: Limitedness is not inherited from an interface parent, so to
6637       --  be limited in that case the type must be explicitly declared as
6638       --  limited. However, task and protected interfaces are always limited.
6639
6640       if Limited_Present (Type_Def) then
6641          Set_Is_Limited_Record (Derived_Type);
6642
6643       elsif Is_Limited_Record (Parent_Type)
6644         or else (Present (Full_View (Parent_Type))
6645                    and then Is_Limited_Record (Full_View (Parent_Type)))
6646       then
6647          if not Is_Interface (Parent_Type)
6648            or else Is_Synchronized_Interface (Parent_Type)
6649            or else Is_Protected_Interface (Parent_Type)
6650            or else Is_Task_Interface (Parent_Type)
6651          then
6652             Set_Is_Limited_Record (Derived_Type);
6653          end if;
6654       end if;
6655
6656       --  STEP 2a: process discriminants of derived type if any
6657
6658       Push_Scope (Derived_Type);
6659
6660       if Discriminant_Specs then
6661          Set_Has_Unknown_Discriminants (Derived_Type, False);
6662
6663          --  The following call initializes fields Has_Discriminants and
6664          --  Discriminant_Constraint, unless we are processing the completion
6665          --  of a private type declaration.
6666
6667          Check_Or_Process_Discriminants (N, Derived_Type);
6668
6669          --  For non-tagged types the constraint on the Parent_Type must be
6670          --  present and is used to rename the discriminants.
6671
6672          if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
6673             Error_Msg_N ("untagged parent must have discriminants", Indic);
6674
6675          elsif not Is_Tagged and then not Constraint_Present then
6676             Error_Msg_N
6677               ("discriminant constraint needed for derived untagged records",
6678                Indic);
6679
6680          --  Otherwise the parent subtype must be constrained unless we have a
6681          --  private extension.
6682
6683          elsif not Constraint_Present
6684            and then not Private_Extension
6685            and then not Is_Constrained (Parent_Type)
6686          then
6687             Error_Msg_N
6688               ("unconstrained type not allowed in this context", Indic);
6689
6690          elsif Constraint_Present then
6691             --  The following call sets the field Corresponding_Discriminant
6692             --  for the discriminants in the Derived_Type.
6693
6694             Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
6695
6696             --  For untagged types all new discriminants must rename
6697             --  discriminants in the parent. For private extensions new
6698             --  discriminants cannot rename old ones (implied by [7.3(13)]).
6699
6700             Discrim := First_Discriminant (Derived_Type);
6701             while Present (Discrim) loop
6702                if not Is_Tagged
6703                  and then No (Corresponding_Discriminant (Discrim))
6704                then
6705                   Error_Msg_N
6706                     ("new discriminants must constrain old ones", Discrim);
6707
6708                elsif Private_Extension
6709                  and then Present (Corresponding_Discriminant (Discrim))
6710                then
6711                   Error_Msg_N
6712                     ("only static constraints allowed for parent"
6713                      & " discriminants in the partial view", Indic);
6714                   exit;
6715                end if;
6716
6717                --  If a new discriminant is used in the constraint, then its
6718                --  subtype must be statically compatible with the parent
6719                --  discriminant's subtype (3.7(15)).
6720
6721                if Present (Corresponding_Discriminant (Discrim))
6722                  and then
6723                    not Subtypes_Statically_Compatible
6724                          (Etype (Discrim),
6725                           Etype (Corresponding_Discriminant (Discrim)))
6726                then
6727                   Error_Msg_N
6728                     ("subtype must be compatible with parent discriminant",
6729                      Discrim);
6730                end if;
6731
6732                Next_Discriminant (Discrim);
6733             end loop;
6734
6735             --  Check whether the constraints of the full view statically
6736             --  match those imposed by the parent subtype [7.3(13)].
6737
6738             if Present (Stored_Constraint (Derived_Type)) then
6739                declare
6740                   C1, C2 : Elmt_Id;
6741
6742                begin
6743                   C1 := First_Elmt (Discs);
6744                   C2 := First_Elmt (Stored_Constraint (Derived_Type));
6745                   while Present (C1) and then Present (C2) loop
6746                      if not
6747                        Fully_Conformant_Expressions (Node (C1), Node (C2))
6748                      then
6749                         Error_Msg_N
6750                           ("not conformant with previous declaration",
6751                            Node (C1));
6752                      end if;
6753
6754                      Next_Elmt (C1);
6755                      Next_Elmt (C2);
6756                   end loop;
6757                end;
6758             end if;
6759          end if;
6760
6761       --  STEP 2b: No new discriminants, inherit discriminants if any
6762
6763       else
6764          if Private_Extension then
6765             Set_Has_Unknown_Discriminants
6766               (Derived_Type,
6767                Has_Unknown_Discriminants (Parent_Type)
6768                  or else Unknown_Discriminants_Present (N));
6769
6770          --  The partial view of the parent may have unknown discriminants,
6771          --  but if the full view has discriminants and the parent type is
6772          --  in scope they must be inherited.
6773
6774          elsif Has_Unknown_Discriminants (Parent_Type)
6775            and then
6776             (not Has_Discriminants (Parent_Type)
6777               or else not In_Open_Scopes (Scope (Parent_Type)))
6778          then
6779             Set_Has_Unknown_Discriminants (Derived_Type);
6780          end if;
6781
6782          if not Has_Unknown_Discriminants (Derived_Type)
6783            and then not Has_Unknown_Discriminants (Parent_Base)
6784            and then Has_Discriminants (Parent_Type)
6785          then
6786             Inherit_Discrims := True;
6787             Set_Has_Discriminants
6788               (Derived_Type, True);
6789             Set_Discriminant_Constraint
6790               (Derived_Type, Discriminant_Constraint (Parent_Base));
6791          end if;
6792
6793          --  The following test is true for private types (remember
6794          --  transformation 5. is not applied to those) and in an error
6795          --  situation.
6796
6797          if Constraint_Present then
6798             Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
6799          end if;
6800
6801          --  For now mark a new derived type as constrained only if it has no
6802          --  discriminants. At the end of Build_Derived_Record_Type we properly
6803          --  set this flag in the case of private extensions. See comments in
6804          --  point 9. just before body of Build_Derived_Record_Type.
6805
6806          Set_Is_Constrained
6807            (Derived_Type,
6808             not (Inherit_Discrims
6809                    or else Has_Unknown_Discriminants (Derived_Type)));
6810       end if;
6811
6812       --  STEP 3: initialize fields of derived type
6813
6814       Set_Is_Tagged_Type    (Derived_Type, Is_Tagged);
6815       Set_Stored_Constraint (Derived_Type, No_Elist);
6816
6817       --  Ada 2005 (AI-251): Private type-declarations can implement interfaces
6818       --  but cannot be interfaces
6819
6820       if not Private_Extension
6821          and then Ekind (Derived_Type) /= E_Private_Type
6822          and then Ekind (Derived_Type) /= E_Limited_Private_Type
6823       then
6824          if Interface_Present (Type_Def) then
6825             Analyze_Interface_Declaration (Derived_Type, Type_Def);
6826          end if;
6827
6828          Set_Interfaces (Derived_Type, No_Elist);
6829       end if;
6830
6831       --  Fields inherited from the Parent_Type
6832
6833       Set_Discard_Names
6834         (Derived_Type, Einfo.Discard_Names      (Parent_Type));
6835       Set_Has_Specified_Layout
6836         (Derived_Type, Has_Specified_Layout     (Parent_Type));
6837       Set_Is_Limited_Composite
6838         (Derived_Type, Is_Limited_Composite     (Parent_Type));
6839       Set_Is_Private_Composite
6840         (Derived_Type, Is_Private_Composite     (Parent_Type));
6841
6842       --  Fields inherited from the Parent_Base
6843
6844       Set_Has_Controlled_Component
6845         (Derived_Type, Has_Controlled_Component (Parent_Base));
6846       Set_Has_Non_Standard_Rep
6847         (Derived_Type, Has_Non_Standard_Rep     (Parent_Base));
6848       Set_Has_Primitive_Operations
6849         (Derived_Type, Has_Primitive_Operations (Parent_Base));
6850
6851       --  Fields inherited from the Parent_Base in the non-private case
6852
6853       if Ekind (Derived_Type) = E_Record_Type then
6854          Set_Has_Complex_Representation
6855            (Derived_Type, Has_Complex_Representation (Parent_Base));
6856       end if;
6857
6858       --  Fields inherited from the Parent_Base for record types
6859
6860       if Is_Record_Type (Derived_Type) then
6861          Set_OK_To_Reorder_Components
6862            (Derived_Type, OK_To_Reorder_Components (Parent_Base));
6863          Set_Reverse_Bit_Order
6864            (Derived_Type, Reverse_Bit_Order (Parent_Base));
6865       end if;
6866
6867       --  Direct controlled types do not inherit Finalize_Storage_Only flag
6868
6869       if not Is_Controlled (Parent_Type) then
6870          Set_Finalize_Storage_Only
6871            (Derived_Type, Finalize_Storage_Only (Parent_Type));
6872       end if;
6873
6874       --  Set fields for private derived types
6875
6876       if Is_Private_Type (Derived_Type) then
6877          Set_Depends_On_Private (Derived_Type, True);
6878          Set_Private_Dependents (Derived_Type, New_Elmt_List);
6879
6880       --  Inherit fields from non private record types. If this is the
6881       --  completion of a derivation from a private type, the parent itself
6882       --  is private, and the attributes come from its full view, which must
6883       --  be present.
6884
6885       else
6886          if Is_Private_Type (Parent_Base)
6887            and then not Is_Record_Type (Parent_Base)
6888          then
6889             Set_Component_Alignment
6890               (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
6891             Set_C_Pass_By_Copy
6892               (Derived_Type, C_Pass_By_Copy      (Full_View (Parent_Base)));
6893          else
6894             Set_Component_Alignment
6895               (Derived_Type, Component_Alignment (Parent_Base));
6896
6897             Set_C_Pass_By_Copy
6898               (Derived_Type, C_Pass_By_Copy      (Parent_Base));
6899          end if;
6900       end if;
6901
6902       --  Set fields for tagged types
6903
6904       if Is_Tagged then
6905          Set_Primitive_Operations (Derived_Type, New_Elmt_List);
6906
6907          --  All tagged types defined in Ada.Finalization are controlled
6908
6909          if Chars (Scope (Derived_Type)) = Name_Finalization
6910            and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
6911            and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
6912          then
6913             Set_Is_Controlled (Derived_Type);
6914          else
6915             Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
6916          end if;
6917
6918          Make_Class_Wide_Type (Derived_Type);
6919          Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
6920
6921          if Has_Discriminants (Derived_Type)
6922            and then Constraint_Present
6923          then
6924             Set_Stored_Constraint
6925               (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
6926          end if;
6927
6928          if Ada_Version >= Ada_05 then
6929             declare
6930                Ifaces_List : Elist_Id;
6931
6932             begin
6933                --  Checks rules 3.9.4 (13/2 and 14/2)
6934
6935                if Comes_From_Source (Derived_Type)
6936                  and then not Is_Private_Type (Derived_Type)
6937                  and then Is_Interface (Parent_Type)
6938                  and then not Is_Interface (Derived_Type)
6939                then
6940                   if Is_Task_Interface (Parent_Type) then
6941                      Error_Msg_N
6942                        ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
6943                         Derived_Type);
6944
6945                   elsif Is_Protected_Interface (Parent_Type) then
6946                      Error_Msg_N
6947                        ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
6948                         Derived_Type);
6949                   end if;
6950                end if;
6951
6952                --  Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
6953
6954                Check_Interfaces (N, Type_Def);
6955
6956                --  Ada 2005 (AI-251): Collect the list of progenitors that are
6957                --  not already in the parents.
6958
6959                Collect_Interfaces
6960                  (T               => Derived_Type,
6961                   Ifaces_List     => Ifaces_List,
6962                   Exclude_Parents => True);
6963
6964                Set_Interfaces (Derived_Type, Ifaces_List);
6965             end;
6966          end if;
6967
6968       else
6969          Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
6970          Set_Has_Non_Standard_Rep
6971                        (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
6972       end if;
6973
6974       --  STEP 4: Inherit components from the parent base and constrain them.
6975       --          Apply the second transformation described in point 6. above.
6976
6977       if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
6978         or else not Has_Discriminants (Parent_Type)
6979         or else not Is_Constrained (Parent_Type)
6980       then
6981          Constrs := Discs;
6982       else
6983          Constrs := Discriminant_Constraint (Parent_Type);
6984       end if;
6985
6986       Assoc_List :=
6987         Inherit_Components
6988           (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
6989
6990       --  STEP 5a: Copy the parent record declaration for untagged types
6991
6992       if not Is_Tagged then
6993
6994          --  Discriminant_Constraint (Derived_Type) has been properly
6995          --  constructed. Save it and temporarily set it to Empty because we
6996          --  do not want the call to New_Copy_Tree below to mess this list.
6997
6998          if Has_Discriminants (Derived_Type) then
6999             Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
7000             Set_Discriminant_Constraint (Derived_Type, No_Elist);
7001          else
7002             Save_Discr_Constr := No_Elist;
7003          end if;
7004
7005          --  Save the Etype field of Derived_Type. It is correctly set now,
7006          --  but the call to New_Copy tree may remap it to point to itself,
7007          --  which is not what we want. Ditto for the Next_Entity field.
7008
7009          Save_Etype       := Etype (Derived_Type);
7010          Save_Next_Entity := Next_Entity (Derived_Type);
7011
7012          --  Assoc_List maps all stored discriminants in the Parent_Base to
7013          --  stored discriminants in the Derived_Type. It is fundamental that
7014          --  no types or itypes with discriminants other than the stored
7015          --  discriminants appear in the entities declared inside
7016          --  Derived_Type, since the back end cannot deal with it.
7017
7018          New_Decl :=
7019            New_Copy_Tree
7020              (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
7021
7022          --  Restore the fields saved prior to the New_Copy_Tree call
7023          --  and compute the stored constraint.
7024
7025          Set_Etype       (Derived_Type, Save_Etype);
7026          Set_Next_Entity (Derived_Type, Save_Next_Entity);
7027
7028          if Has_Discriminants (Derived_Type) then
7029             Set_Discriminant_Constraint
7030               (Derived_Type, Save_Discr_Constr);
7031             Set_Stored_Constraint
7032               (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
7033             Replace_Components (Derived_Type, New_Decl);
7034          end if;
7035
7036          --  Insert the new derived type declaration
7037
7038          Rewrite (N, New_Decl);
7039
7040       --  STEP 5b: Complete the processing for record extensions in generics
7041
7042       --  There is no completion for record extensions declared in the
7043       --  parameter part of a generic, so we need to complete processing for
7044       --  these generic record extensions here. The Record_Type_Definition call
7045       --  will change the Ekind of the components from E_Void to E_Component.
7046
7047       elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
7048          Record_Type_Definition (Empty, Derived_Type);
7049
7050       --  STEP 5c: Process the record extension for non private tagged types
7051
7052       elsif not Private_Extension then
7053
7054          --  Add the _parent field in the derived type
7055
7056          Expand_Record_Extension (Derived_Type, Type_Def);
7057
7058          --  Ada 2005 (AI-251): Addition of the Tag corresponding to all the
7059          --  implemented interfaces if we are in expansion mode
7060
7061          if Expander_Active
7062            and then Has_Interfaces (Derived_Type)
7063          then
7064             Add_Interface_Tag_Components (N, Derived_Type);
7065          end if;
7066
7067          --  Analyze the record extension
7068
7069          Record_Type_Definition
7070            (Record_Extension_Part (Type_Def), Derived_Type);
7071       end if;
7072
7073       End_Scope;
7074
7075       --  Nothing else to do if there is an error in the derivation.
7076       --  An unusual case: the full view may be derived from a type in an
7077       --  instance, when the partial view was used illegally as an actual
7078       --  in that instance, leading to a circular definition.
7079
7080       if Etype (Derived_Type) = Any_Type
7081         or else Etype (Parent_Type) = Derived_Type
7082       then
7083          return;
7084       end if;
7085
7086       --  Set delayed freeze and then derive subprograms, we need to do
7087       --  this in this order so that derived subprograms inherit the
7088       --  derived freeze if necessary.
7089
7090       Set_Has_Delayed_Freeze (Derived_Type);
7091
7092       if Derive_Subps then
7093          Derive_Subprograms (Parent_Type, Derived_Type);
7094       end if;
7095
7096       --  If we have a private extension which defines a constrained derived
7097       --  type mark as constrained here after we have derived subprograms. See
7098       --  comment on point 9. just above the body of Build_Derived_Record_Type.
7099
7100       if Private_Extension and then Inherit_Discrims then
7101          if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7102             Set_Is_Constrained          (Derived_Type, True);
7103             Set_Discriminant_Constraint (Derived_Type, Discs);
7104
7105          elsif Is_Constrained (Parent_Type) then
7106             Set_Is_Constrained
7107               (Derived_Type, True);
7108             Set_Discriminant_Constraint
7109               (Derived_Type, Discriminant_Constraint (Parent_Type));
7110          end if;
7111       end if;
7112
7113       --  Update the class_wide type, which shares the now-completed
7114       --  entity list with its specific type.
7115
7116       if Is_Tagged then
7117          Set_First_Entity
7118            (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
7119          Set_Last_Entity
7120            (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
7121       end if;
7122
7123       --  Update the scope of anonymous access types of discriminants and other
7124       --  components, to prevent scope anomalies in gigi, when the derivation
7125       --  appears in a scope nested within that of the parent.
7126
7127       declare
7128          D : Entity_Id;
7129
7130       begin
7131          D := First_Entity (Derived_Type);
7132          while Present (D) loop
7133             if Ekind (D) = E_Discriminant
7134               or else Ekind (D) = E_Component
7135             then
7136                if Is_Itype (Etype (D))
7137                   and then Ekind (Etype (D)) = E_Anonymous_Access_Type
7138                then
7139                   Set_Scope (Etype (D), Current_Scope);
7140                end if;
7141             end if;
7142
7143             Next_Entity (D);
7144          end loop;
7145       end;
7146    end Build_Derived_Record_Type;
7147
7148    ------------------------
7149    -- Build_Derived_Type --
7150    ------------------------
7151
7152    procedure Build_Derived_Type
7153      (N             : Node_Id;
7154       Parent_Type   : Entity_Id;
7155       Derived_Type  : Entity_Id;
7156       Is_Completion : Boolean;
7157       Derive_Subps  : Boolean := True)
7158    is
7159       Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7160
7161    begin
7162       --  Set common attributes
7163
7164       Set_Scope         (Derived_Type, Current_Scope);
7165
7166       Set_Ekind         (Derived_Type, Ekind    (Parent_Base));
7167       Set_Etype         (Derived_Type,           Parent_Base);
7168       Set_Has_Task      (Derived_Type, Has_Task (Parent_Base));
7169
7170       Set_Size_Info     (Derived_Type,                Parent_Type);
7171       Set_RM_Size       (Derived_Type, RM_Size       (Parent_Type));
7172       Set_Convention    (Derived_Type, Convention    (Parent_Type));
7173       Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7174
7175       --  The derived type inherits the representation clauses of the parent.
7176       --  However, for a private type that is completed by a derivation, there
7177       --  may be operation attributes that have been specified already (stream
7178       --  attributes and External_Tag) and those must be provided. Finally,
7179       --  if the partial view is a private extension, the representation items
7180       --  of the parent have been inherited already, and should not be chained
7181       --  twice to the derived type.
7182
7183       if Is_Tagged_Type (Parent_Type)
7184         and then Present (First_Rep_Item (Derived_Type))
7185       then
7186          --  The existing items are either operational items or items inherited
7187          --  from a private extension declaration.
7188
7189          declare
7190             Rep : Node_Id;
7191             --  Used to iterate over representation items of the derived type
7192
7193             Last_Rep : Node_Id;
7194             --  Last representation item of the (non-empty) representation
7195             --  item list of the derived type.
7196
7197             Found : Boolean := False;
7198
7199          begin
7200             Rep      := First_Rep_Item (Derived_Type);
7201             Last_Rep := Rep;
7202             while Present (Rep) loop
7203                if Rep = First_Rep_Item (Parent_Type) then
7204                   Found := True;
7205                   exit;
7206
7207                else
7208                   Rep := Next_Rep_Item (Rep);
7209
7210                   if Present (Rep) then
7211                      Last_Rep := Rep;
7212                   end if;
7213                end if;
7214             end loop;
7215
7216             --  Here if we either encountered the parent type's first rep
7217             --  item on the derived type's rep item list (in which case
7218             --  Found is True, and we have nothing else to do), or if we
7219             --  reached the last rep item of the derived type, which is
7220             --  Last_Rep, in which case we further chain the parent type's
7221             --  rep items to those of the derived type.
7222
7223             if not Found then
7224                Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
7225             end if;
7226          end;
7227
7228       else
7229          Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
7230       end if;
7231
7232       case Ekind (Parent_Type) is
7233          when Numeric_Kind =>
7234             Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
7235
7236          when Array_Kind =>
7237             Build_Derived_Array_Type (N, Parent_Type,  Derived_Type);
7238
7239          when E_Record_Type
7240             | E_Record_Subtype
7241             | Class_Wide_Kind  =>
7242             Build_Derived_Record_Type
7243               (N, Parent_Type, Derived_Type, Derive_Subps);
7244             return;
7245
7246          when Enumeration_Kind =>
7247             Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
7248
7249          when Access_Kind =>
7250             Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
7251
7252          when Incomplete_Or_Private_Kind =>
7253             Build_Derived_Private_Type
7254               (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
7255
7256             --  For discriminated types, the derivation includes deriving
7257             --  primitive operations. For others it is done below.
7258
7259             if Is_Tagged_Type (Parent_Type)
7260               or else Has_Discriminants (Parent_Type)
7261               or else (Present (Full_View (Parent_Type))
7262                         and then Has_Discriminants (Full_View (Parent_Type)))
7263             then
7264                return;
7265             end if;
7266
7267          when Concurrent_Kind =>
7268             Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
7269
7270          when others =>
7271             raise Program_Error;
7272       end case;
7273
7274       if Etype (Derived_Type) = Any_Type then
7275          return;
7276       end if;
7277
7278       --  Set delayed freeze and then derive subprograms, we need to do this
7279       --  in this order so that derived subprograms inherit the derived freeze
7280       --  if necessary.
7281
7282       Set_Has_Delayed_Freeze (Derived_Type);
7283       if Derive_Subps then
7284          Derive_Subprograms (Parent_Type, Derived_Type);
7285       end if;
7286
7287       Set_Has_Primitive_Operations
7288         (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
7289    end Build_Derived_Type;
7290
7291    -----------------------
7292    -- Build_Discriminal --
7293    -----------------------
7294
7295    procedure Build_Discriminal (Discrim : Entity_Id) is
7296       D_Minal : Entity_Id;
7297       CR_Disc : Entity_Id;
7298
7299    begin
7300       --  A discriminal has the same name as the discriminant
7301
7302       D_Minal :=
7303         Make_Defining_Identifier (Sloc (Discrim),
7304           Chars => Chars (Discrim));
7305
7306       Set_Ekind     (D_Minal, E_In_Parameter);
7307       Set_Mechanism (D_Minal, Default_Mechanism);
7308       Set_Etype     (D_Minal, Etype (Discrim));
7309
7310       Set_Discriminal (Discrim, D_Minal);
7311       Set_Discriminal_Link (D_Minal, Discrim);
7312
7313       --  For task types, build at once the discriminants of the corresponding
7314       --  record, which are needed if discriminants are used in entry defaults
7315       --  and in family bounds.
7316
7317       if Is_Concurrent_Type (Current_Scope)
7318         or else Is_Limited_Type (Current_Scope)
7319       then
7320          CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
7321
7322          Set_Ekind            (CR_Disc, E_In_Parameter);
7323          Set_Mechanism        (CR_Disc, Default_Mechanism);
7324          Set_Etype            (CR_Disc, Etype (Discrim));
7325          Set_Discriminal_Link (CR_Disc, Discrim);
7326          Set_CR_Discriminant  (Discrim, CR_Disc);
7327       end if;
7328    end Build_Discriminal;
7329
7330    ------------------------------------
7331    -- Build_Discriminant_Constraints --
7332    ------------------------------------
7333
7334    function Build_Discriminant_Constraints
7335      (T           : Entity_Id;
7336       Def         : Node_Id;
7337       Derived_Def : Boolean := False) return Elist_Id
7338    is
7339       C        : constant Node_Id := Constraint (Def);
7340       Nb_Discr : constant Nat     := Number_Discriminants (T);
7341
7342       Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
7343       --  Saves the expression corresponding to a given discriminant in T
7344
7345       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
7346       --  Return the Position number within array Discr_Expr of a discriminant
7347       --  D within the discriminant list of the discriminated type T.
7348
7349       ------------------
7350       -- Pos_Of_Discr --
7351       ------------------
7352
7353       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
7354          Disc : Entity_Id;
7355
7356       begin
7357          Disc := First_Discriminant (T);
7358          for J in Discr_Expr'Range loop
7359             if Disc = D then
7360                return J;
7361             end if;
7362
7363             Next_Discriminant (Disc);
7364          end loop;
7365
7366          --  Note: Since this function is called on discriminants that are
7367          --  known to belong to the discriminated type, falling through the
7368          --  loop with no match signals an internal compiler error.
7369
7370          raise Program_Error;
7371       end Pos_Of_Discr;
7372
7373       --  Declarations local to Build_Discriminant_Constraints
7374
7375       Discr : Entity_Id;
7376       E     : Entity_Id;
7377       Elist : constant Elist_Id := New_Elmt_List;
7378
7379       Constr   : Node_Id;
7380       Expr     : Node_Id;
7381       Id       : Node_Id;
7382       Position : Nat;
7383       Found    : Boolean;
7384
7385       Discrim_Present : Boolean := False;
7386
7387    --  Start of processing for Build_Discriminant_Constraints
7388
7389    begin
7390       --  The following loop will process positional associations only.
7391       --  For a positional association, the (single) discriminant is
7392       --  implicitly specified by position, in textual order (RM 3.7.2).
7393
7394       Discr  := First_Discriminant (T);
7395       Constr := First (Constraints (C));
7396       for D in Discr_Expr'Range loop
7397          exit when Nkind (Constr) = N_Discriminant_Association;
7398
7399          if No (Constr) then
7400             Error_Msg_N ("too few discriminants given in constraint", C);
7401             return New_Elmt_List;
7402
7403          elsif Nkind (Constr) = N_Range
7404            or else (Nkind (Constr) = N_Attribute_Reference
7405                      and then
7406                     Attribute_Name (Constr) = Name_Range)
7407          then
7408             Error_Msg_N
7409               ("a range is not a valid discriminant constraint", Constr);
7410             Discr_Expr (D) := Error;
7411
7412          else
7413             Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
7414             Discr_Expr (D) := Constr;
7415          end if;
7416
7417          Next_Discriminant (Discr);
7418          Next (Constr);
7419       end loop;
7420
7421       if No (Discr) and then Present (Constr) then
7422          Error_Msg_N ("too many discriminants given in constraint", Constr);
7423          return New_Elmt_List;
7424       end if;
7425
7426       --  Named associations can be given in any order, but if both positional
7427       --  and named associations are used in the same discriminant constraint,
7428       --  then positional associations must occur first, at their normal
7429       --  position. Hence once a named association is used, the rest of the
7430       --  discriminant constraint must use only named associations.
7431
7432       while Present (Constr) loop
7433
7434          --  Positional association forbidden after a named association
7435
7436          if Nkind (Constr) /= N_Discriminant_Association then
7437             Error_Msg_N ("positional association follows named one", Constr);
7438             return New_Elmt_List;
7439
7440          --  Otherwise it is a named association
7441
7442          else
7443             --  E records the type of the discriminants in the named
7444             --  association. All the discriminants specified in the same name
7445             --  association must have the same type.
7446
7447             E := Empty;
7448
7449             --  Search the list of discriminants in T to see if the simple name
7450             --  given in the constraint matches any of them.
7451
7452             Id := First (Selector_Names (Constr));
7453             while Present (Id) loop
7454                Found := False;
7455
7456                --  If Original_Discriminant is present, we are processing a
7457                --  generic instantiation and this is an instance node. We need
7458                --  to find the name of the corresponding discriminant in the
7459                --  actual record type T and not the name of the discriminant in
7460                --  the generic formal. Example:
7461
7462                --    generic
7463                --       type G (D : int) is private;
7464                --    package P is
7465                --       subtype W is G (D => 1);
7466                --    end package;
7467                --    type Rec (X : int) is record ... end record;
7468                --    package Q is new P (G => Rec);
7469
7470                --  At the point of the instantiation, formal type G is Rec
7471                --  and therefore when reanalyzing "subtype W is G (D => 1);"
7472                --  which really looks like "subtype W is Rec (D => 1);" at
7473                --  the point of instantiation, we want to find the discriminant
7474                --  that corresponds to D in Rec, i.e. X.
7475
7476                if Present (Original_Discriminant (Id)) then
7477                   Discr := Find_Corresponding_Discriminant (Id, T);
7478                   Found := True;
7479
7480                else
7481                   Discr := First_Discriminant (T);
7482                   while Present (Discr) loop
7483                      if Chars (Discr) = Chars (Id) then
7484                         Found := True;
7485                         exit;
7486                      end if;
7487
7488                      Next_Discriminant (Discr);
7489                   end loop;
7490
7491                   if not Found then
7492                      Error_Msg_N ("& does not match any discriminant", Id);
7493                      return New_Elmt_List;
7494
7495                   --  The following is only useful for the benefit of generic
7496                   --  instances but it does not interfere with other
7497                   --  processing for the non-generic case so we do it in all
7498                   --  cases (for generics this statement is executed when
7499                   --  processing the generic definition, see comment at the
7500                   --  beginning of this if statement).
7501
7502                   else
7503                      Set_Original_Discriminant (Id, Discr);
7504                   end if;
7505                end if;
7506
7507                Position := Pos_Of_Discr (T, Discr);
7508
7509                if Present (Discr_Expr (Position)) then
7510                   Error_Msg_N ("duplicate constraint for discriminant&", Id);
7511
7512                else
7513                   --  Each discriminant specified in the same named association
7514                   --  must be associated with a separate copy of the
7515                   --  corresponding expression.
7516
7517                   if Present (Next (Id)) then
7518                      Expr := New_Copy_Tree (Expression (Constr));
7519                      Set_Parent (Expr, Parent (Expression (Constr)));
7520                   else
7521                      Expr := Expression (Constr);
7522                   end if;
7523
7524                   Discr_Expr (Position) := Expr;
7525                   Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
7526                end if;
7527
7528                --  A discriminant association with more than one discriminant
7529                --  name is only allowed if the named discriminants are all of
7530                --  the same type (RM 3.7.1(8)).
7531
7532                if E = Empty then
7533                   E := Base_Type (Etype (Discr));
7534
7535                elsif Base_Type (Etype (Discr)) /= E then
7536                   Error_Msg_N
7537                     ("all discriminants in an association " &
7538                      "must have the same type", Id);
7539                end if;
7540
7541                Next (Id);
7542             end loop;
7543          end if;
7544
7545          Next (Constr);
7546       end loop;
7547
7548       --  A discriminant constraint must provide exactly one value for each
7549       --  discriminant of the type (RM 3.7.1(8)).
7550
7551       for J in Discr_Expr'Range loop
7552          if No (Discr_Expr (J)) then
7553             Error_Msg_N ("too few discriminants given in constraint", C);
7554             return New_Elmt_List;
7555          end if;
7556       end loop;
7557
7558       --  Determine if there are discriminant expressions in the constraint
7559
7560       for J in Discr_Expr'Range loop
7561          if Denotes_Discriminant
7562               (Discr_Expr (J), Check_Concurrent => True)
7563          then
7564             Discrim_Present := True;
7565          end if;
7566       end loop;
7567
7568       --  Build an element list consisting of the expressions given in the
7569       --  discriminant constraint and apply the appropriate checks. The list
7570       --  is constructed after resolving any named discriminant associations
7571       --  and therefore the expressions appear in the textual order of the
7572       --  discriminants.
7573
7574       Discr := First_Discriminant (T);
7575       for J in Discr_Expr'Range loop
7576          if Discr_Expr (J) /= Error then
7577             Append_Elmt (Discr_Expr (J), Elist);
7578
7579             --  If any of the discriminant constraints is given by a
7580             --  discriminant and we are in a derived type declaration we
7581             --  have a discriminant renaming. Establish link between new
7582             --  and old discriminant.
7583
7584             if Denotes_Discriminant (Discr_Expr (J)) then
7585                if Derived_Def then
7586                   Set_Corresponding_Discriminant
7587                     (Entity (Discr_Expr (J)), Discr);
7588                end if;
7589
7590             --  Force the evaluation of non-discriminant expressions.
7591             --  If we have found a discriminant in the constraint 3.4(26)
7592             --  and 3.8(18) demand that no range checks are performed are
7593             --  after evaluation. If the constraint is for a component
7594             --  definition that has a per-object constraint, expressions are
7595             --  evaluated but not checked either. In all other cases perform
7596             --  a range check.
7597
7598             else
7599                if Discrim_Present then
7600                   null;
7601
7602                elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
7603                  and then
7604                    Has_Per_Object_Constraint
7605                      (Defining_Identifier (Parent (Parent (Def))))
7606                then
7607                   null;
7608
7609                elsif Is_Access_Type (Etype (Discr)) then
7610                   Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
7611
7612                else
7613                   Apply_Range_Check (Discr_Expr (J), Etype (Discr));
7614                end if;
7615
7616                Force_Evaluation (Discr_Expr (J));
7617             end if;
7618
7619             --  Check that the designated type of an access discriminant's
7620             --  expression is not a class-wide type unless the discriminant's
7621             --  designated type is also class-wide.
7622
7623             if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
7624               and then not Is_Class_Wide_Type
7625                          (Designated_Type (Etype (Discr)))
7626               and then Etype (Discr_Expr (J)) /= Any_Type
7627               and then Is_Class_Wide_Type
7628                          (Designated_Type (Etype (Discr_Expr (J))))
7629             then
7630                Wrong_Type (Discr_Expr (J), Etype (Discr));
7631
7632             elsif Is_Access_Type (Etype (Discr))
7633               and then not Is_Access_Constant (Etype (Discr))
7634               and then Is_Access_Type (Etype (Discr_Expr (J)))
7635               and then Is_Access_Constant (Etype (Discr_Expr (J)))
7636             then
7637                Error_Msg_NE
7638                  ("constraint for discriminant& must be access to variable",
7639                     Def, Discr);
7640             end if;
7641          end if;
7642
7643          Next_Discriminant (Discr);
7644       end loop;
7645
7646       return Elist;
7647    end Build_Discriminant_Constraints;
7648
7649    ---------------------------------
7650    -- Build_Discriminated_Subtype --
7651    ---------------------------------
7652
7653    procedure Build_Discriminated_Subtype
7654      (T           : Entity_Id;
7655       Def_Id      : Entity_Id;
7656       Elist       : Elist_Id;
7657       Related_Nod : Node_Id;
7658       For_Access  : Boolean := False)
7659    is
7660       Has_Discrs  : constant Boolean := Has_Discriminants (T);
7661       Constrained : constant Boolean :=
7662                       (Has_Discrs
7663                          and then not Is_Empty_Elmt_List (Elist)
7664                          and then not Is_Class_Wide_Type (T))
7665                         or else Is_Constrained (T);
7666
7667    begin
7668       if Ekind (T) = E_Record_Type then
7669          if For_Access then
7670             Set_Ekind (Def_Id, E_Private_Subtype);
7671             Set_Is_For_Access_Subtype (Def_Id, True);
7672          else
7673             Set_Ekind (Def_Id, E_Record_Subtype);
7674          end if;
7675
7676          --  Inherit preelaboration flag from base, for types for which it
7677          --  may have been set: records, private types, protected types.
7678
7679          Set_Known_To_Have_Preelab_Init
7680            (Def_Id, Known_To_Have_Preelab_Init (T));
7681
7682       elsif Ekind (T) = E_Task_Type then
7683          Set_Ekind (Def_Id, E_Task_Subtype);
7684
7685       elsif Ekind (T) = E_Protected_Type then
7686          Set_Ekind (Def_Id, E_Protected_Subtype);
7687          Set_Known_To_Have_Preelab_Init
7688            (Def_Id, Known_To_Have_Preelab_Init (T));
7689
7690       elsif Is_Private_Type (T) then
7691          Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
7692          Set_Known_To_Have_Preelab_Init
7693            (Def_Id, Known_To_Have_Preelab_Init (T));
7694
7695       elsif Is_Class_Wide_Type (T) then
7696          Set_Ekind (Def_Id, E_Class_Wide_Subtype);
7697
7698       else
7699          --  Incomplete type. Attach subtype to list of dependents, to be
7700          --  completed with full view of parent type,  unless is it the
7701          --  designated subtype of a record component within an init_proc.
7702          --  This last case arises for a component of an access type whose
7703          --  designated type is incomplete (e.g. a Taft Amendment type).
7704          --  The designated subtype is within an inner scope, and needs no
7705          --  elaboration, because only the access type is needed in the
7706          --  initialization procedure.
7707
7708          Set_Ekind (Def_Id, Ekind (T));
7709
7710          if For_Access and then Within_Init_Proc then
7711             null;
7712          else
7713             Append_Elmt (Def_Id, Private_Dependents (T));
7714          end if;
7715       end if;
7716
7717       Set_Etype             (Def_Id, T);
7718       Init_Size_Align       (Def_Id);
7719       Set_Has_Discriminants (Def_Id, Has_Discrs);
7720       Set_Is_Constrained    (Def_Id, Constrained);
7721
7722       Set_First_Entity      (Def_Id, First_Entity   (T));
7723       Set_Last_Entity       (Def_Id, Last_Entity    (T));
7724
7725       --  If the subtype is the completion of a private declaration, there may
7726       --  have been representation clauses for the partial view, and they must
7727       --  be preserved. Build_Derived_Type chains the inherited clauses with
7728       --  the ones appearing on the extension. If this comes from a subtype
7729       --  declaration, all clauses are inherited.
7730
7731       if No (First_Rep_Item (Def_Id)) then
7732          Set_First_Rep_Item    (Def_Id, First_Rep_Item (T));
7733       end if;
7734
7735       if Is_Tagged_Type (T) then
7736          Set_Is_Tagged_Type  (Def_Id);
7737          Make_Class_Wide_Type (Def_Id);
7738       end if;
7739
7740       Set_Stored_Constraint (Def_Id, No_Elist);
7741
7742       if Has_Discrs then
7743          Set_Discriminant_Constraint (Def_Id, Elist);
7744          Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
7745       end if;
7746
7747       if Is_Tagged_Type (T) then
7748
7749          --  Ada 2005 (AI-251): In case of concurrent types we inherit the
7750          --  concurrent record type (which has the list of primitive
7751          --  operations).
7752
7753          if Ada_Version >= Ada_05
7754            and then Is_Concurrent_Type (T)
7755          then
7756             Set_Corresponding_Record_Type (Def_Id,
7757                Corresponding_Record_Type (T));
7758          else
7759             Set_Primitive_Operations (Def_Id, Primitive_Operations (T));
7760          end if;
7761
7762          Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
7763       end if;
7764
7765       --  Subtypes introduced by component declarations do not need to be
7766       --  marked as delayed, and do not get freeze nodes, because the semantics
7767       --  verifies that the parents of the subtypes are frozen before the
7768       --  enclosing record is frozen.
7769
7770       if not Is_Type (Scope (Def_Id)) then
7771          Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
7772
7773          if Is_Private_Type (T)
7774            and then Present (Full_View (T))
7775          then
7776             Conditional_Delay (Def_Id, Full_View (T));
7777          else
7778             Conditional_Delay (Def_Id, T);
7779          end if;
7780       end if;
7781
7782       if Is_Record_Type (T) then
7783          Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
7784
7785          if Has_Discrs
7786             and then not Is_Empty_Elmt_List (Elist)
7787             and then not For_Access
7788          then
7789             Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
7790          elsif not For_Access then
7791             Set_Cloned_Subtype (Def_Id, T);
7792          end if;
7793       end if;
7794    end Build_Discriminated_Subtype;
7795
7796    ---------------------------
7797    -- Build_Itype_Reference --
7798    ---------------------------
7799
7800    procedure Build_Itype_Reference
7801      (Ityp : Entity_Id;
7802       Nod  : Node_Id)
7803    is
7804       IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
7805    begin
7806       Set_Itype (IR, Ityp);
7807       Insert_After (Nod, IR);
7808    end Build_Itype_Reference;
7809
7810    ------------------------
7811    -- Build_Scalar_Bound --
7812    ------------------------
7813
7814    function Build_Scalar_Bound
7815      (Bound : Node_Id;
7816       Par_T : Entity_Id;
7817       Der_T : Entity_Id) return Node_Id
7818    is
7819       New_Bound : Entity_Id;
7820
7821    begin
7822       --  Note: not clear why this is needed, how can the original bound
7823       --  be unanalyzed at this point? and if it is, what business do we
7824       --  have messing around with it? and why is the base type of the
7825       --  parent type the right type for the resolution. It probably is
7826       --  not! It is OK for the new bound we are creating, but not for
7827       --  the old one??? Still if it never happens, no problem!
7828
7829       Analyze_And_Resolve (Bound, Base_Type (Par_T));
7830
7831       if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
7832          New_Bound := New_Copy (Bound);
7833          Set_Etype (New_Bound, Der_T);
7834          Set_Analyzed (New_Bound);
7835
7836       elsif Is_Entity_Name (Bound) then
7837          New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
7838
7839       --  The following is almost certainly wrong. What business do we have
7840       --  relocating a node (Bound) that is presumably still attached to
7841       --  the tree elsewhere???
7842
7843       else
7844          New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
7845       end if;
7846
7847       Set_Etype (New_Bound, Der_T);
7848       return New_Bound;
7849    end Build_Scalar_Bound;
7850
7851    --------------------------------
7852    -- Build_Underlying_Full_View --
7853    --------------------------------
7854
7855    procedure Build_Underlying_Full_View
7856      (N   : Node_Id;
7857       Typ : Entity_Id;
7858       Par : Entity_Id)
7859    is
7860       Loc  : constant Source_Ptr := Sloc (N);
7861       Subt : constant Entity_Id :=
7862                Make_Defining_Identifier
7863                  (Loc, New_External_Name (Chars (Typ), 'S'));
7864
7865       Constr : Node_Id;
7866       Indic  : Node_Id;
7867       C      : Node_Id;
7868       Id     : Node_Id;
7869
7870       procedure Set_Discriminant_Name (Id : Node_Id);
7871       --  If the derived type has discriminants, they may rename discriminants
7872       --  of the parent. When building the full view of the parent, we need to
7873       --  recover the names of the original discriminants if the constraint is
7874       --  given by named associations.
7875
7876       ---------------------------
7877       -- Set_Discriminant_Name --
7878       ---------------------------
7879
7880       procedure Set_Discriminant_Name (Id : Node_Id) is
7881          Disc : Entity_Id;
7882
7883       begin
7884          Set_Original_Discriminant (Id, Empty);
7885
7886          if Has_Discriminants (Typ) then
7887             Disc := First_Discriminant (Typ);
7888             while Present (Disc) loop
7889                if Chars (Disc) = Chars (Id)
7890                  and then Present (Corresponding_Discriminant (Disc))
7891                then
7892                   Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
7893                end if;
7894                Next_Discriminant (Disc);
7895             end loop;
7896          end if;
7897       end Set_Discriminant_Name;
7898
7899    --  Start of processing for Build_Underlying_Full_View
7900
7901    begin
7902       if Nkind (N) = N_Full_Type_Declaration then
7903          Constr := Constraint (Subtype_Indication (Type_Definition (N)));
7904
7905       elsif Nkind (N) = N_Subtype_Declaration then
7906          Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
7907
7908       elsif Nkind (N) = N_Component_Declaration then
7909          Constr :=
7910            New_Copy_Tree
7911              (Constraint (Subtype_Indication (Component_Definition (N))));
7912
7913       else
7914          raise Program_Error;
7915       end if;
7916
7917       C := First (Constraints (Constr));
7918       while Present (C) loop
7919          if Nkind (C) = N_Discriminant_Association then
7920             Id := First (Selector_Names (C));
7921             while Present (Id) loop
7922                Set_Discriminant_Name (Id);
7923                Next (Id);
7924             end loop;
7925          end if;
7926
7927          Next (C);
7928       end loop;
7929
7930       Indic :=
7931         Make_Subtype_Declaration (Loc,
7932           Defining_Identifier => Subt,
7933           Subtype_Indication  =>
7934             Make_Subtype_Indication (Loc,
7935               Subtype_Mark => New_Reference_To (Par, Loc),
7936               Constraint   => New_Copy_Tree (Constr)));
7937
7938       --  If this is a component subtype for an outer itype, it is not
7939       --  a list member, so simply set the parent link for analysis: if
7940       --  the enclosing type does not need to be in a declarative list,
7941       --  neither do the components.
7942
7943       if Is_List_Member (N)
7944         and then Nkind (N) /= N_Component_Declaration
7945       then
7946          Insert_Before (N, Indic);
7947       else
7948          Set_Parent (Indic, Parent (N));
7949       end if;
7950
7951       Analyze (Indic);
7952       Set_Underlying_Full_View (Typ, Full_View (Subt));
7953    end Build_Underlying_Full_View;
7954
7955    -------------------------------
7956    -- Check_Abstract_Overriding --
7957    -------------------------------
7958
7959    procedure Check_Abstract_Overriding (T : Entity_Id) is
7960       Alias_Subp : Entity_Id;
7961       Elmt       : Elmt_Id;
7962       Op_List    : Elist_Id;
7963       Subp       : Entity_Id;
7964       Type_Def   : Node_Id;
7965
7966    begin
7967       Op_List := Primitive_Operations (T);
7968
7969       --  Loop to check primitive operations
7970
7971       Elmt := First_Elmt (Op_List);
7972       while Present (Elmt) loop
7973          Subp := Node (Elmt);
7974          Alias_Subp := Alias (Subp);
7975
7976          --  Inherited subprograms are identified by the fact that they do not
7977          --  come from source, and the associated source location is the
7978          --  location of the first subtype of the derived type.
7979
7980          --  Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
7981          --  subprograms that "require overriding".
7982
7983          --  Special exception, do not complain about failure to override the
7984          --  stream routines _Input and _Output, as well as the primitive
7985          --  operations used in dispatching selects since we always provide
7986          --  automatic overridings for these subprograms.
7987
7988          --  Also ignore this rule for convention CIL since .NET libraries
7989          --  do bizarre things with interfaces???
7990
7991          --  The partial view of T may have been a private extension, for
7992          --  which inherited functions dispatching on result are abstract.
7993          --  If the full view is a null extension, there is no need for
7994          --  overriding in Ada2005, but wrappers need to be built for them
7995          --  (see exp_ch3, Build_Controlling_Function_Wrappers).
7996
7997          if Is_Null_Extension (T)
7998            and then Has_Controlling_Result (Subp)
7999            and then Ada_Version >= Ada_05
8000            and then Present (Alias_Subp)
8001            and then not Comes_From_Source (Subp)
8002            and then not Is_Abstract_Subprogram (Alias_Subp)
8003            and then not Is_Access_Type (Etype (Subp))
8004          then
8005             null;
8006
8007          --  Ada 2005 (AI-251): Internal entities of interfaces need no
8008          --  processing because this check is done with the aliased
8009          --  entity
8010
8011          elsif Present (Interface_Alias (Subp)) then
8012             null;
8013
8014          elsif (Is_Abstract_Subprogram (Subp)
8015                  or else Requires_Overriding (Subp)
8016                  or else
8017                    (Has_Controlling_Result (Subp)
8018                      and then Present (Alias_Subp)
8019                      and then not Comes_From_Source (Subp)
8020                      and then Sloc (Subp) = Sloc (First_Subtype (T))))
8021            and then not Is_TSS (Subp, TSS_Stream_Input)
8022            and then not Is_TSS (Subp, TSS_Stream_Output)
8023            and then not Is_Abstract_Type (T)
8024            and then Convention (T) /= Convention_CIL
8025            and then not Is_Predefined_Interface_Primitive (Subp)
8026
8027             --  Ada 2005 (AI-251): Do not consider hidden entities associated
8028             --  with abstract interface types because the check will be done
8029             --  with the aliased entity (otherwise we generate a duplicated
8030             --  error message).
8031
8032            and then not Present (Interface_Alias (Subp))
8033          then
8034             if Present (Alias_Subp) then
8035
8036                --  Only perform the check for a derived subprogram when the
8037                --  type has an explicit record extension. This avoids incorrect
8038                --  flagging of abstract subprograms for the case of a type
8039                --  without an extension that is derived from a formal type
8040                --  with a tagged actual (can occur within a private part).
8041
8042                --  Ada 2005 (AI-391): In the case of an inherited function with
8043                --  a controlling result of the type, the rule does not apply if
8044                --  the type is a null extension (unless the parent function
8045                --  itself is abstract, in which case the function must still be
8046                --  be overridden). The expander will generate an overriding
8047                --  wrapper function calling the parent subprogram (see
8048                --  Exp_Ch3.Make_Controlling_Wrapper_Functions).
8049
8050                Type_Def := Type_Definition (Parent (T));
8051
8052                if Nkind (Type_Def) = N_Derived_Type_Definition
8053                  and then Present (Record_Extension_Part (Type_Def))
8054                  and then
8055                    (Ada_Version < Ada_05
8056                       or else not Is_Null_Extension (T)
8057                       or else Ekind (Subp) = E_Procedure
8058                       or else not Has_Controlling_Result (Subp)
8059                       or else Is_Abstract_Subprogram (Alias_Subp)
8060                       or else Requires_Overriding (Subp)
8061                       or else Is_Access_Type (Etype (Subp)))
8062                then
8063                   --  Avoid reporting error in case of abstract predefined
8064                   --  primitive inherited from interface type because the
8065                   --  body of internally generated predefined primitives
8066                   --  of tagged types are generated later by Freeze_Type
8067
8068                   if Is_Interface (Root_Type (T))
8069                     and then Is_Abstract_Subprogram (Subp)
8070                     and then Is_Predefined_Dispatching_Operation (Subp)
8071                     and then not Comes_From_Source (Ultimate_Alias (Subp))
8072                   then
8073                      null;
8074
8075                   else
8076                      Error_Msg_NE
8077                        ("type must be declared abstract or & overridden",
8078                         T, Subp);
8079
8080                      --  Traverse the whole chain of aliased subprograms to
8081                      --  complete the error notification. This is especially
8082                      --  useful for traceability of the chain of entities when
8083                      --  the subprogram corresponds with an interface
8084                      --  subprogram (which may be defined in another package).
8085
8086                      if Present (Alias_Subp) then
8087                         declare
8088                            E : Entity_Id;
8089
8090                         begin
8091                            E := Subp;
8092                            while Present (Alias (E)) loop
8093                               Error_Msg_Sloc := Sloc (E);
8094                               Error_Msg_NE
8095                                 ("\& has been inherited #", T, Subp);
8096                               E := Alias (E);
8097                            end loop;
8098
8099                            Error_Msg_Sloc := Sloc (E);
8100                            Error_Msg_NE
8101                              ("\& has been inherited from subprogram #",
8102                               T, Subp);
8103                         end;
8104                      end if;
8105                   end if;
8106
8107                --  Ada 2005 (AI-345): Protected or task type implementing
8108                --  abstract interfaces.
8109
8110                elsif Is_Concurrent_Record_Type (T)
8111                  and then Present (Interfaces (T))
8112                then
8113                   --  The controlling formal of Subp must be of mode "out",
8114                   --  "in out" or an access-to-variable to be overridden.
8115
8116                   --  Error message below needs rewording (remember comma
8117                   --  in -gnatj mode) ???
8118
8119                   if Ekind (First_Formal (Subp)) = E_In_Parameter then
8120                      if not Is_Predefined_Dispatching_Operation (Subp) then
8121                         Error_Msg_NE
8122                           ("first formal of & must be of mode `OUT`, " &
8123                            "`IN OUT` or access-to-variable", T, Subp);
8124                         Error_Msg_N
8125                           ("\to be overridden by protected procedure or " &
8126                            "entry (RM 9.4(11.9/2))", T);
8127                      end if;
8128
8129                   --  Some other kind of overriding failure
8130
8131                   else
8132                      Error_Msg_NE
8133                        ("interface subprogram & must be overridden",
8134                         T, Subp);
8135                   end if;
8136                end if;
8137
8138             else
8139                Error_Msg_Node_2 := T;
8140                Error_Msg_N
8141                  ("abstract subprogram& not allowed for type&", Subp);
8142
8143                --  Also post unconditional warning on the type (unconditional
8144                --  so that if there are more than one of these cases, we get
8145                --  them all, and not just the first one).
8146
8147                Error_Msg_Node_2 := Subp;
8148                Error_Msg_N
8149                  ("nonabstract type& has abstract subprogram&!", T);
8150             end if;
8151          end if;
8152
8153          --  Ada 2005 (AI05-0030): Inspect hidden subprograms which provide
8154          --  the mapping between interface and implementing type primitives.
8155          --  If the interface alias is marked as Implemented_By_Entry, the
8156          --  alias must be an entry wrapper.
8157
8158          if Ada_Version >= Ada_05
8159            and then Is_Hidden (Subp)
8160            and then Present (Interface_Alias (Subp))
8161            and then Implemented_By_Entry (Interface_Alias (Subp))
8162            and then Present (Alias_Subp)
8163            and then
8164              (not Is_Primitive_Wrapper (Alias_Subp)
8165                 or else Ekind (Wrapped_Entity (Alias_Subp)) /= E_Entry)
8166          then
8167             declare
8168                Error_Ent : Entity_Id := T;
8169
8170             begin
8171                if Is_Concurrent_Record_Type (Error_Ent) then
8172                   Error_Ent := Corresponding_Concurrent_Type (Error_Ent);
8173                end if;
8174
8175                Error_Msg_Node_2 := Interface_Alias (Subp);
8176                Error_Msg_NE
8177                  ("type & must implement abstract subprogram & with an entry",
8178                   Error_Ent, Error_Ent);
8179             end;
8180          end if;
8181
8182          Next_Elmt (Elmt);
8183       end loop;
8184    end Check_Abstract_Overriding;
8185
8186    ------------------------------------------------
8187    -- Check_Access_Discriminant_Requires_Limited --
8188    ------------------------------------------------
8189
8190    procedure Check_Access_Discriminant_Requires_Limited
8191      (D   : Node_Id;
8192       Loc : Node_Id)
8193    is
8194    begin
8195       --  A discriminant_specification for an access discriminant shall appear
8196       --  only in the declaration for a task or protected type, or for a type
8197       --  with the reserved word 'limited' in its definition or in one of its
8198       --  ancestors. (RM 3.7(10))
8199
8200       if Nkind (Discriminant_Type (D)) = N_Access_Definition
8201         and then not Is_Concurrent_Type (Current_Scope)
8202         and then not Is_Concurrent_Record_Type (Current_Scope)
8203         and then not Is_Limited_Record (Current_Scope)
8204         and then Ekind (Current_Scope) /= E_Limited_Private_Type
8205       then
8206          Error_Msg_N
8207            ("access discriminants allowed only for limited types", Loc);
8208       end if;
8209    end Check_Access_Discriminant_Requires_Limited;
8210
8211    -----------------------------------
8212    -- Check_Aliased_Component_Types --
8213    -----------------------------------
8214
8215    procedure Check_Aliased_Component_Types (T : Entity_Id) is
8216       C : Entity_Id;
8217
8218    begin
8219       --  ??? Also need to check components of record extensions, but not
8220       --  components of protected types (which are always limited).
8221
8222       --  Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
8223       --  types to be unconstrained. This is safe because it is illegal to
8224       --  create access subtypes to such types with explicit discriminant
8225       --  constraints.
8226
8227       if not Is_Limited_Type (T) then
8228          if Ekind (T) = E_Record_Type then
8229             C := First_Component (T);
8230             while Present (C) loop
8231                if Is_Aliased (C)
8232                  and then Has_Discriminants (Etype (C))
8233                  and then not Is_Constrained (Etype (C))
8234                  and then not In_Instance_Body
8235                  and then Ada_Version < Ada_05
8236                then
8237                   Error_Msg_N
8238                     ("aliased component must be constrained (RM 3.6(11))",
8239                       C);
8240                end if;
8241
8242                Next_Component (C);
8243             end loop;
8244
8245          elsif Ekind (T) = E_Array_Type then
8246             if Has_Aliased_Components (T)
8247               and then Has_Discriminants (Component_Type (T))
8248               and then not Is_Constrained (Component_Type (T))
8249               and then not In_Instance_Body
8250               and then Ada_Version < Ada_05
8251             then
8252                Error_Msg_N
8253                  ("aliased component type must be constrained (RM 3.6(11))",
8254                     T);
8255             end if;
8256          end if;
8257       end if;
8258    end Check_Aliased_Component_Types;
8259
8260    ----------------------
8261    -- Check_Completion --
8262    ----------------------
8263
8264    procedure Check_Completion (Body_Id : Node_Id := Empty) is
8265       E : Entity_Id;
8266
8267       procedure Post_Error;
8268       --  Post error message for lack of completion for entity E
8269
8270       ----------------
8271       -- Post_Error --
8272       ----------------
8273
8274       procedure Post_Error is
8275       begin
8276          if not Comes_From_Source (E) then
8277
8278             if Ekind (E) = E_Task_Type
8279               or else Ekind (E) = E_Protected_Type
8280             then
8281                --  It may be an anonymous protected type created for a
8282                --  single variable. Post error on variable, if present.
8283
8284                declare
8285                   Var : Entity_Id;
8286
8287                begin
8288                   Var := First_Entity (Current_Scope);
8289                   while Present (Var) loop
8290                      exit when Etype (Var) = E
8291                        and then Comes_From_Source (Var);
8292
8293                      Next_Entity (Var);
8294                   end loop;
8295
8296                   if Present (Var) then
8297                      E := Var;
8298                   end if;
8299                end;
8300             end if;
8301          end if;
8302
8303          --  If a generated entity has no completion, then either previous
8304          --  semantic errors have disabled the expansion phase, or else we had
8305          --  missing subunits, or else we are compiling without expansion,
8306          --  or else something is very wrong.
8307
8308          if not Comes_From_Source (E) then
8309             pragma Assert
8310               (Serious_Errors_Detected > 0
8311                 or else Configurable_Run_Time_Violations > 0
8312                 or else Subunits_Missing
8313                 or else not Expander_Active);
8314             return;
8315
8316          --  Here for source entity
8317
8318          else
8319             --  Here if no body to post the error message, so we post the error
8320             --  on the declaration that has no completion. This is not really
8321             --  the right place to post it, think about this later ???
8322
8323             if No (Body_Id) then
8324                if Is_Type (E) then
8325                   Error_Msg_NE
8326                     ("missing full declaration for }", Parent (E), E);
8327                else
8328                   Error_Msg_NE
8329                     ("missing body for &", Parent (E), E);
8330                end if;
8331
8332             --  Package body has no completion for a declaration that appears
8333             --  in the corresponding spec. Post error on the body, with a
8334             --  reference to the non-completed declaration.
8335
8336             else
8337                Error_Msg_Sloc := Sloc (E);
8338
8339                if Is_Type (E) then
8340                   Error_Msg_NE
8341                     ("missing full declaration for }!", Body_Id, E);
8342
8343                elsif Is_Overloadable (E)
8344                  and then Current_Entity_In_Scope (E) /= E
8345                then
8346                   --  It may be that the completion is mistyped and appears as
8347                   --  a distinct overloading of the entity.
8348
8349                   declare
8350                      Candidate : constant Entity_Id :=
8351                                    Current_Entity_In_Scope (E);
8352                      Decl      : constant Node_Id :=
8353                                    Unit_Declaration_Node (Candidate);
8354
8355                   begin
8356                      if Is_Overloadable (Candidate)
8357                        and then Ekind (Candidate) = Ekind (E)
8358                        and then Nkind (Decl) = N_Subprogram_Body
8359                        and then Acts_As_Spec (Decl)
8360                      then
8361                         Check_Type_Conformant (Candidate, E);
8362
8363                      else
8364                         Error_Msg_NE ("missing body for & declared#!",
8365                            Body_Id, E);
8366                      end if;
8367                   end;
8368                else
8369                   Error_Msg_NE ("missing body for & declared#!",
8370                      Body_Id, E);
8371                end if;
8372             end if;
8373          end if;
8374       end Post_Error;
8375
8376    --  Start processing for Check_Completion
8377
8378    begin
8379       E := First_Entity (Current_Scope);
8380       while Present (E) loop
8381          if Is_Intrinsic_Subprogram (E) then
8382             null;
8383
8384          --  The following situation requires special handling: a child unit
8385          --  that appears in the context clause of the body of its parent:
8386
8387          --    procedure Parent.Child (...);
8388
8389          --    with Parent.Child;
8390          --    package body Parent is
8391
8392          --  Here Parent.Child appears as a local entity, but should not be
8393          --  flagged as requiring completion, because it is a compilation
8394          --  unit.
8395
8396          --  Ignore missing completion for a subprogram that does not come from
8397          --  source (including the _Call primitive operation of RAS types,
8398          --  which has to have the flag Comes_From_Source for other purposes):
8399          --  we assume that the expander will provide the missing completion.
8400
8401          elsif     Ekind (E) = E_Function
8402            or else Ekind (E) = E_Procedure
8403            or else Ekind (E) = E_Generic_Function
8404            or else Ekind (E) = E_Generic_Procedure
8405          then
8406             if not Has_Completion (E)
8407               and then not (Is_Subprogram (E)
8408                             and then Is_Abstract_Subprogram (E))
8409               and then not (Is_Subprogram (E)
8410                               and then
8411                             (not Comes_From_Source (E)
8412                               or else Chars (E) = Name_uCall))
8413               and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8414                                                        N_Compilation_Unit
8415               and then Chars (E) /= Name_uSize
8416             then
8417                Post_Error;
8418             end if;
8419
8420          elsif Is_Entry (E) then
8421             if not Has_Completion (E) and then
8422               (Ekind (Scope (E)) = E_Protected_Object
8423                 or else Ekind (Scope (E)) = E_Protected_Type)
8424             then
8425                Post_Error;
8426             end if;
8427
8428          elsif Is_Package_Or_Generic_Package (E) then
8429             if Unit_Requires_Body (E) then
8430                if not Has_Completion (E)
8431                  and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8432                                                        N_Compilation_Unit
8433                then
8434                   Post_Error;
8435                end if;
8436
8437             elsif not Is_Child_Unit (E) then
8438                May_Need_Implicit_Body (E);
8439             end if;
8440
8441          elsif Ekind (E) = E_Incomplete_Type
8442            and then No (Underlying_Type (E))
8443          then
8444             Post_Error;
8445
8446          elsif (Ekind (E) = E_Task_Type or else
8447                 Ekind (E) = E_Protected_Type)
8448            and then not Has_Completion (E)
8449          then
8450             Post_Error;
8451
8452          --  A single task declared in the current scope is a constant, verify
8453          --  that the body of its anonymous type is in the same scope. If the
8454          --  task is defined elsewhere, this may be a renaming declaration for
8455          --  which no completion is needed.
8456
8457          elsif Ekind (E) = E_Constant
8458            and then Ekind (Etype (E)) = E_Task_Type
8459            and then not Has_Completion (Etype (E))
8460            and then Scope (Etype (E)) = Current_Scope
8461          then
8462             Post_Error;
8463
8464          elsif Ekind (E) = E_Protected_Object
8465            and then not Has_Completion (Etype (E))
8466          then
8467             Post_Error;
8468
8469          elsif Ekind (E) = E_Record_Type then
8470             if Is_Tagged_Type (E) then
8471                Check_Abstract_Overriding (E);
8472                Check_Conventions (E);
8473             end if;
8474
8475             Check_Aliased_Component_Types (E);
8476
8477          elsif Ekind (E) = E_Array_Type then
8478             Check_Aliased_Component_Types (E);
8479
8480          end if;
8481
8482          Next_Entity (E);
8483       end loop;
8484    end Check_Completion;
8485
8486    ----------------------------
8487    -- Check_Delta_Expression --
8488    ----------------------------
8489
8490    procedure Check_Delta_Expression (E : Node_Id) is
8491    begin
8492       if not (Is_Real_Type (Etype (E))) then
8493          Wrong_Type (E, Any_Real);
8494
8495       elsif not Is_OK_Static_Expression (E) then
8496          Flag_Non_Static_Expr
8497            ("non-static expression used for delta value!", E);
8498
8499       elsif not UR_Is_Positive (Expr_Value_R (E)) then
8500          Error_Msg_N ("delta expression must be positive", E);
8501
8502       else
8503          return;
8504       end if;
8505
8506       --  If any of above errors occurred, then replace the incorrect
8507       --  expression by the real 0.1, which should prevent further errors.
8508
8509       Rewrite (E,
8510         Make_Real_Literal (Sloc (E), Ureal_Tenth));
8511       Analyze_And_Resolve (E, Standard_Float);
8512    end Check_Delta_Expression;
8513
8514    -----------------------------
8515    -- Check_Digits_Expression --
8516    -----------------------------
8517
8518    procedure Check_Digits_Expression (E : Node_Id) is
8519    begin
8520       if not (Is_Integer_Type (Etype (E))) then
8521          Wrong_Type (E, Any_Integer);
8522
8523       elsif not Is_OK_Static_Expression (E) then
8524          Flag_Non_Static_Expr
8525            ("non-static expression used for digits value!", E);
8526
8527       elsif Expr_Value (E) <= 0 then
8528          Error_Msg_N ("digits value must be greater than zero", E);
8529
8530       else
8531          return;
8532       end if;
8533
8534       --  If any of above errors occurred, then replace the incorrect
8535       --  expression by the integer 1, which should prevent further errors.
8536
8537       Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
8538       Analyze_And_Resolve (E, Standard_Integer);
8539
8540    end Check_Digits_Expression;
8541
8542    --------------------------
8543    -- Check_Initialization --
8544    --------------------------
8545
8546    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
8547    begin
8548       if Is_Limited_Type (T)
8549         and then not In_Instance
8550         and then not In_Inlined_Body
8551       then
8552          if not OK_For_Limited_Init (Exp) then
8553
8554             --  In GNAT mode, this is just a warning, to allow it to be evilly
8555             --  turned off. Otherwise it is a real error.
8556
8557             if GNAT_Mode then
8558                Error_Msg_N
8559                  ("?cannot initialize entities of limited type!", Exp);
8560
8561             elsif Ada_Version < Ada_05 then
8562                Error_Msg_N
8563                  ("cannot initialize entities of limited type", Exp);
8564                Explain_Limited_Type (T, Exp);
8565
8566             else
8567                --  Specialize error message according to kind of illegal
8568                --  initial expression.
8569
8570                if Nkind (Exp) = N_Type_Conversion
8571                  and then Nkind (Expression (Exp)) = N_Function_Call
8572                then
8573                   Error_Msg_N
8574                     ("illegal context for call"
8575                       & " to function with limited result", Exp);
8576
8577                else
8578                   Error_Msg_N
8579                     ("initialization of limited object requires aggregate "
8580                       & "or function call",  Exp);
8581                end if;
8582             end if;
8583          end if;
8584       end if;
8585    end Check_Initialization;
8586
8587    ----------------------
8588    -- Check_Interfaces --
8589    ----------------------
8590
8591    procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
8592       Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
8593
8594       Iface       : Node_Id;
8595       Iface_Def   : Node_Id;
8596       Iface_Typ   : Entity_Id;
8597       Parent_Node : Node_Id;
8598
8599       Is_Task : Boolean := False;
8600       --  Set True if parent type or any progenitor is a task interface
8601
8602       Is_Protected : Boolean := False;
8603       --  Set True if parent type or any progenitor is a protected interface
8604
8605       procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
8606       --  Check that a progenitor is compatible with declaration.
8607       --  Error is posted on Error_Node.
8608
8609       ------------------
8610       -- Check_Ifaces --
8611       ------------------
8612
8613       procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
8614          Iface_Id : constant Entity_Id :=
8615                       Defining_Identifier (Parent (Iface_Def));
8616          Type_Def : Node_Id;
8617
8618       begin
8619          if Nkind (N) = N_Private_Extension_Declaration then
8620             Type_Def := N;
8621          else
8622             Type_Def := Type_Definition (N);
8623          end if;
8624
8625          if Is_Task_Interface (Iface_Id) then
8626             Is_Task := True;
8627
8628          elsif Is_Protected_Interface (Iface_Id) then
8629             Is_Protected := True;
8630          end if;
8631
8632          --  Check that the characteristics of the progenitor are compatible
8633          --  with the explicit qualifier in the declaration.
8634          --  The check only applies to qualifiers that come from source.
8635          --  Limited_Present also appears in the declaration of corresponding
8636          --  records, and the check does not apply to them.
8637
8638          if Limited_Present (Type_Def)
8639            and then not
8640              Is_Concurrent_Record_Type (Defining_Identifier (N))
8641          then
8642             if Is_Limited_Interface (Parent_Type)
8643               and then not Is_Limited_Interface (Iface_Id)
8644             then
8645                Error_Msg_NE
8646                  ("progenitor& must be limited interface",
8647                    Error_Node, Iface_Id);
8648
8649             elsif
8650               (Task_Present (Iface_Def)
8651                 or else Protected_Present (Iface_Def)
8652                 or else Synchronized_Present (Iface_Def))
8653               and then Nkind (N) /= N_Private_Extension_Declaration
8654             then
8655                Error_Msg_NE
8656                  ("progenitor& must be limited interface",
8657                    Error_Node, Iface_Id);
8658             end if;
8659
8660          --  Protected interfaces can only inherit from limited, synchronized
8661          --  or protected interfaces.
8662
8663          elsif Nkind (N) = N_Full_Type_Declaration
8664            and then  Protected_Present (Type_Def)
8665          then
8666             if Limited_Present (Iface_Def)
8667               or else Synchronized_Present (Iface_Def)
8668               or else Protected_Present (Iface_Def)
8669             then
8670                null;
8671
8672             elsif Task_Present (Iface_Def) then
8673                Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
8674                             & " from task interface", Error_Node);
8675
8676             else
8677                Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
8678                             & " from non-limited interface", Error_Node);
8679             end if;
8680
8681          --  Ada 2005 (AI-345): Synchronized interfaces can only inherit from
8682          --  limited and synchronized.
8683
8684          elsif Synchronized_Present (Type_Def) then
8685             if Limited_Present (Iface_Def)
8686               or else Synchronized_Present (Iface_Def)
8687             then
8688                null;
8689
8690             elsif Protected_Present (Iface_Def)
8691               and then Nkind (N) /= N_Private_Extension_Declaration
8692             then
8693                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8694                             & " from protected interface", Error_Node);
8695
8696             elsif Task_Present (Iface_Def)
8697               and then Nkind (N) /= N_Private_Extension_Declaration
8698             then
8699                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8700                             & " from task interface", Error_Node);
8701
8702             elsif not Is_Limited_Interface (Iface_Id) then
8703                Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8704                             & " from non-limited interface", Error_Node);
8705             end if;
8706
8707          --  Ada 2005 (AI-345): Task interfaces can only inherit from limited,
8708          --  synchronized or task interfaces.
8709
8710          elsif Nkind (N) = N_Full_Type_Declaration
8711            and then Task_Present (Type_Def)
8712          then
8713             if Limited_Present (Iface_Def)
8714               or else Synchronized_Present (Iface_Def)
8715               or else Task_Present (Iface_Def)
8716             then
8717                null;
8718
8719             elsif Protected_Present (Iface_Def) then
8720                Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
8721                             & " protected interface", Error_Node);
8722
8723             else
8724                Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
8725                             & " non-limited interface", Error_Node);
8726             end if;
8727          end if;
8728       end Check_Ifaces;
8729
8730    --  Start of processing for Check_Interfaces
8731
8732    begin
8733       if Is_Interface (Parent_Type) then
8734          if Is_Task_Interface (Parent_Type) then
8735             Is_Task := True;
8736
8737          elsif Is_Protected_Interface (Parent_Type) then
8738             Is_Protected := True;
8739          end if;
8740       end if;
8741
8742       if Nkind (N) = N_Private_Extension_Declaration then
8743
8744          --  Check that progenitors are compatible with declaration
8745
8746          Iface := First (Interface_List (Def));
8747          while Present (Iface) loop
8748             Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
8749
8750             Parent_Node := Parent (Base_Type (Iface_Typ));
8751             Iface_Def   := Type_Definition (Parent_Node);
8752
8753             if not Is_Interface (Iface_Typ) then
8754                Diagnose_Interface (Iface, Iface_Typ);
8755
8756             else
8757                Check_Ifaces (Iface_Def, Iface);
8758             end if;
8759
8760             Next (Iface);
8761          end loop;
8762
8763          if Is_Task and Is_Protected then
8764             Error_Msg_N
8765               ("type cannot derive from task and protected interface", N);
8766          end if;
8767
8768          return;
8769       end if;
8770
8771       --  Full type declaration of derived type.
8772       --  Check compatibility with parent if it is interface type
8773
8774       if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
8775         and then Is_Interface (Parent_Type)
8776       then
8777          Parent_Node := Parent (Parent_Type);
8778
8779          --  More detailed checks for interface varieties
8780
8781          Check_Ifaces
8782            (Iface_Def  => Type_Definition (Parent_Node),
8783             Error_Node => Subtype_Indication (Type_Definition (N)));
8784       end if;
8785
8786       Iface := First (Interface_List (Def));
8787       while Present (Iface) loop
8788          Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
8789
8790          Parent_Node := Parent (Base_Type (Iface_Typ));
8791          Iface_Def   := Type_Definition (Parent_Node);
8792
8793          if not Is_Interface (Iface_Typ) then
8794             Diagnose_Interface (Iface, Iface_Typ);
8795
8796          else
8797             --  "The declaration of a specific descendant of an interface
8798             --   type freezes the interface type" RM 13.14
8799
8800             Freeze_Before (N, Iface_Typ);
8801             Check_Ifaces (Iface_Def, Error_Node => Iface);
8802          end if;
8803
8804          Next (Iface);
8805       end loop;
8806
8807       if Is_Task and Is_Protected then
8808          Error_Msg_N
8809            ("type cannot derive from task and protected interface", N);
8810       end if;
8811    end Check_Interfaces;
8812
8813    ------------------------------------
8814    -- Check_Or_Process_Discriminants --
8815    ------------------------------------
8816
8817    --  If an incomplete or private type declaration was already given for the
8818    --  type, the discriminants may have already been processed if they were
8819    --  present on the incomplete declaration. In this case a full conformance
8820    --  check is performed otherwise just process them.
8821
8822    procedure Check_Or_Process_Discriminants
8823      (N    : Node_Id;
8824       T    : Entity_Id;
8825       Prev : Entity_Id := Empty)
8826    is
8827    begin
8828       if Has_Discriminants (T) then
8829
8830          --  Make the discriminants visible to component declarations
8831
8832          declare
8833             D    : Entity_Id;
8834             Prev : Entity_Id;
8835
8836          begin
8837             D := First_Discriminant (T);
8838             while Present (D) loop
8839                Prev := Current_Entity (D);
8840                Set_Current_Entity (D);
8841                Set_Is_Immediately_Visible (D);
8842                Set_Homonym (D, Prev);
8843
8844                --  Ada 2005 (AI-230): Access discriminant allowed in
8845                --  non-limited record types.
8846
8847                if Ada_Version < Ada_05 then
8848
8849                   --  This restriction gets applied to the full type here. It
8850                   --  has already been applied earlier to the partial view.
8851
8852                   Check_Access_Discriminant_Requires_Limited (Parent (D), N);
8853                end if;
8854
8855                Next_Discriminant (D);
8856             end loop;
8857          end;
8858
8859       elsif Present (Discriminant_Specifications (N)) then
8860          Process_Discriminants (N, Prev);
8861       end if;
8862    end Check_Or_Process_Discriminants;
8863
8864    ----------------------
8865    -- Check_Real_Bound --
8866    ----------------------
8867
8868    procedure Check_Real_Bound (Bound : Node_Id) is
8869    begin
8870       if not Is_Real_Type (Etype (Bound)) then
8871          Error_Msg_N
8872            ("bound in real type definition must be of real type", Bound);
8873
8874       elsif not Is_OK_Static_Expression (Bound) then
8875          Flag_Non_Static_Expr
8876            ("non-static expression used for real type bound!", Bound);
8877
8878       else
8879          return;
8880       end if;
8881
8882       Rewrite
8883         (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
8884       Analyze (Bound);
8885       Resolve (Bound, Standard_Float);
8886    end Check_Real_Bound;
8887
8888    ------------------------------
8889    -- Complete_Private_Subtype --
8890    ------------------------------
8891
8892    procedure Complete_Private_Subtype
8893      (Priv        : Entity_Id;
8894       Full        : Entity_Id;
8895       Full_Base   : Entity_Id;
8896       Related_Nod : Node_Id)
8897    is
8898       Save_Next_Entity : Entity_Id;
8899       Save_Homonym     : Entity_Id;
8900
8901    begin
8902       --  Set semantic attributes for (implicit) private subtype completion.
8903       --  If the full type has no discriminants, then it is a copy of the full
8904       --  view of the base. Otherwise, it is a subtype of the base with a
8905       --  possible discriminant constraint. Save and restore the original
8906       --  Next_Entity field of full to ensure that the calls to Copy_Node
8907       --  do not corrupt the entity chain.
8908
8909       --  Note that the type of the full view is the same entity as the type of
8910       --  the partial view. In this fashion, the subtype has access to the
8911       --  correct view of the parent.
8912
8913       Save_Next_Entity := Next_Entity (Full);
8914       Save_Homonym     := Homonym (Priv);
8915
8916       case Ekind (Full_Base) is
8917          when E_Record_Type    |
8918               E_Record_Subtype |
8919               Class_Wide_Kind  |
8920               Private_Kind     |
8921               Task_Kind        |
8922               Protected_Kind   =>
8923             Copy_Node (Priv, Full);
8924
8925             Set_Has_Discriminants  (Full, Has_Discriminants (Full_Base));
8926             Set_First_Entity       (Full, First_Entity (Full_Base));
8927             Set_Last_Entity        (Full, Last_Entity (Full_Base));
8928
8929          when others =>
8930             Copy_Node (Full_Base, Full);
8931             Set_Chars          (Full, Chars (Priv));
8932             Conditional_Delay  (Full, Priv);
8933             Set_Sloc           (Full, Sloc (Priv));
8934       end case;
8935
8936       Set_Next_Entity (Full, Save_Next_Entity);
8937       Set_Homonym     (Full, Save_Homonym);
8938       Set_Associated_Node_For_Itype (Full, Related_Nod);
8939
8940       --  Set common attributes for all subtypes
8941
8942       Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
8943
8944       --  The Etype of the full view is inconsistent. Gigi needs to see the
8945       --  structural full view,  which is what the current scheme gives:
8946       --  the Etype of the full view is the etype of the full base. However,
8947       --  if the full base is a derived type, the full view then looks like
8948       --  a subtype of the parent, not a subtype of the full base. If instead
8949       --  we write:
8950
8951       --       Set_Etype (Full, Full_Base);
8952
8953       --  then we get inconsistencies in the front-end (confusion between
8954       --  views). Several outstanding bugs are related to this ???
8955
8956       Set_Is_First_Subtype (Full, False);
8957       Set_Scope            (Full, Scope (Priv));
8958       Set_Size_Info        (Full, Full_Base);
8959       Set_RM_Size          (Full, RM_Size (Full_Base));
8960       Set_Is_Itype         (Full);
8961
8962       --  A subtype of a private-type-without-discriminants, whose full-view
8963       --  has discriminants with default expressions, is not constrained!
8964
8965       if not Has_Discriminants (Priv) then
8966          Set_Is_Constrained (Full, Is_Constrained (Full_Base));
8967
8968          if Has_Discriminants (Full_Base) then
8969             Set_Discriminant_Constraint
8970               (Full, Discriminant_Constraint (Full_Base));
8971
8972             --  The partial view may have been indefinite, the full view
8973             --  might not be.
8974
8975             Set_Has_Unknown_Discriminants
8976               (Full, Has_Unknown_Discriminants (Full_Base));
8977          end if;
8978       end if;
8979
8980       Set_First_Rep_Item     (Full, First_Rep_Item (Full_Base));
8981       Set_Depends_On_Private (Full, Has_Private_Component (Full));
8982
8983       --  Freeze the private subtype entity if its parent is delayed, and not
8984       --  already frozen. We skip this processing if the type is an anonymous
8985       --  subtype of a record component, or is the corresponding record of a
8986       --  protected type, since ???
8987
8988       if not Is_Type (Scope (Full)) then
8989          Set_Has_Delayed_Freeze (Full,
8990            Has_Delayed_Freeze (Full_Base)
8991              and then (not Is_Frozen (Full_Base)));
8992       end if;
8993
8994       Set_Freeze_Node (Full, Empty);
8995       Set_Is_Frozen (Full, False);
8996       Set_Full_View (Priv, Full);
8997
8998       if Has_Discriminants (Full) then
8999          Set_Stored_Constraint_From_Discriminant_Constraint (Full);
9000          Set_Stored_Constraint (Priv, Stored_Constraint (Full));
9001
9002          if Has_Unknown_Discriminants (Full) then
9003             Set_Discriminant_Constraint (Full, No_Elist);
9004          end if;
9005       end if;
9006
9007       if Ekind (Full_Base) = E_Record_Type
9008         and then Has_Discriminants (Full_Base)
9009         and then Has_Discriminants (Priv) -- might not, if errors
9010         and then not Has_Unknown_Discriminants (Priv)
9011         and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
9012       then
9013          Create_Constrained_Components
9014            (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
9015
9016       --  If the full base is itself derived from private, build a congruent
9017       --  subtype of its underlying type, for use by the back end. For a
9018       --  constrained record component, the declaration cannot be placed on
9019       --  the component list, but it must nevertheless be built an analyzed, to
9020       --  supply enough information for Gigi to compute the size of component.
9021
9022       elsif Ekind (Full_Base) in Private_Kind
9023         and then Is_Derived_Type (Full_Base)
9024         and then Has_Discriminants (Full_Base)
9025         and then (Ekind (Current_Scope) /= E_Record_Subtype)
9026       then
9027          if not Is_Itype (Priv)
9028            and then
9029              Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
9030          then
9031             Build_Underlying_Full_View
9032               (Parent (Priv), Full, Etype (Full_Base));
9033
9034          elsif Nkind (Related_Nod) = N_Component_Declaration then
9035             Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
9036          end if;
9037
9038       elsif Is_Record_Type (Full_Base) then
9039
9040          --  Show Full is simply a renaming of Full_Base
9041
9042          Set_Cloned_Subtype (Full, Full_Base);
9043       end if;
9044
9045       --  It is unsafe to share to bounds of a scalar type, because the Itype
9046       --  is elaborated on demand, and if a bound is non-static then different
9047       --  orders of elaboration in different units will lead to different
9048       --  external symbols.
9049
9050       if Is_Scalar_Type (Full_Base) then
9051          Set_Scalar_Range (Full,
9052            Make_Range (Sloc (Related_Nod),
9053              Low_Bound  =>
9054                Duplicate_Subexpr_No_Checks (Type_Low_Bound  (Full_Base)),
9055              High_Bound =>
9056                Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
9057
9058          --  This completion inherits the bounds of the full parent, but if
9059          --  the parent is an unconstrained floating point type, so is the
9060          --  completion.
9061
9062          if Is_Floating_Point_Type (Full_Base) then
9063             Set_Includes_Infinities
9064              (Scalar_Range (Full), Has_Infinities (Full_Base));
9065          end if;
9066       end if;
9067
9068       --  ??? It seems that a lot of fields are missing that should be copied
9069       --  from Full_Base to Full. Here are some that are introduced in a
9070       --  non-disruptive way but a cleanup is necessary.
9071
9072       if Is_Tagged_Type (Full_Base) then
9073          Set_Is_Tagged_Type (Full);
9074          Set_Primitive_Operations (Full, Primitive_Operations (Full_Base));
9075          Set_Class_Wide_Type      (Full, Class_Wide_Type (Full_Base));
9076
9077       --  If this is a subtype of a protected or task type, constrain its
9078       --  corresponding record, unless this is a subtype without constraints,
9079       --  i.e. a simple renaming as with an actual subtype in an instance.
9080
9081       elsif Is_Concurrent_Type (Full_Base) then
9082          if Has_Discriminants (Full)
9083            and then Present (Corresponding_Record_Type (Full_Base))
9084            and then
9085              not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
9086          then
9087             Set_Corresponding_Record_Type (Full,
9088               Constrain_Corresponding_Record
9089                 (Full, Corresponding_Record_Type (Full_Base),
9090                   Related_Nod, Full_Base));
9091
9092          else
9093             Set_Corresponding_Record_Type (Full,
9094               Corresponding_Record_Type (Full_Base));
9095          end if;
9096       end if;
9097    end Complete_Private_Subtype;
9098
9099    ----------------------------
9100    -- Constant_Redeclaration --
9101    ----------------------------
9102
9103    procedure Constant_Redeclaration
9104      (Id : Entity_Id;
9105       N  : Node_Id;
9106       T  : out Entity_Id)
9107    is
9108       Prev    : constant Entity_Id := Current_Entity_In_Scope (Id);
9109       Obj_Def : constant Node_Id := Object_Definition (N);
9110       New_T   : Entity_Id;
9111
9112       procedure Check_Possible_Deferred_Completion
9113         (Prev_Id      : Entity_Id;
9114          Prev_Obj_Def : Node_Id;
9115          Curr_Obj_Def : Node_Id);
9116       --  Determine whether the two object definitions describe the partial
9117       --  and the full view of a constrained deferred constant. Generate
9118       --  a subtype for the full view and verify that it statically matches
9119       --  the subtype of the partial view.
9120
9121       procedure Check_Recursive_Declaration (Typ : Entity_Id);
9122       --  If deferred constant is an access type initialized with an allocator,
9123       --  check whether there is an illegal recursion in the definition,
9124       --  through a default value of some record subcomponent. This is normally
9125       --  detected when generating init procs, but requires this additional
9126       --  mechanism when expansion is disabled.
9127
9128       ----------------------------------------
9129       -- Check_Possible_Deferred_Completion --
9130       ----------------------------------------
9131
9132       procedure Check_Possible_Deferred_Completion
9133         (Prev_Id      : Entity_Id;
9134          Prev_Obj_Def : Node_Id;
9135          Curr_Obj_Def : Node_Id)
9136       is
9137       begin
9138          if Nkind (Prev_Obj_Def) = N_Subtype_Indication
9139            and then Present (Constraint (Prev_Obj_Def))
9140            and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
9141            and then Present (Constraint (Curr_Obj_Def))
9142          then
9143             declare
9144                Loc    : constant Source_Ptr := Sloc (N);
9145                Def_Id : constant Entity_Id :=
9146                           Make_Defining_Identifier (Loc,
9147                             New_Internal_Name ('S'));
9148                Decl   : constant Node_Id :=
9149                           Make_Subtype_Declaration (Loc,
9150                             Defining_Identifier =>
9151                               Def_Id,
9152                             Subtype_Indication =>
9153                               Relocate_Node (Curr_Obj_Def));
9154
9155             begin
9156                Insert_Before_And_Analyze (N, Decl);
9157                Set_Etype (Id, Def_Id);
9158
9159                if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
9160                   Error_Msg_Sloc := Sloc (Prev_Id);
9161                   Error_Msg_N ("subtype does not statically match deferred " &
9162                                "declaration#", N);
9163                end if;
9164             end;
9165          end if;
9166       end Check_Possible_Deferred_Completion;
9167
9168       ---------------------------------
9169       -- Check_Recursive_Declaration --
9170       ---------------------------------
9171
9172       procedure Check_Recursive_Declaration (Typ : Entity_Id) is
9173          Comp : Entity_Id;
9174
9175       begin
9176          if Is_Record_Type (Typ) then
9177             Comp := First_Component (Typ);
9178             while Present (Comp) loop
9179                if Comes_From_Source (Comp) then
9180                   if Present (Expression (Parent (Comp)))
9181                     and then Is_Entity_Name (Expression (Parent (Comp)))
9182                     and then Entity (Expression (Parent (Comp))) = Prev
9183                   then
9184                      Error_Msg_Sloc := Sloc (Parent (Comp));
9185                      Error_Msg_NE
9186                        ("illegal circularity with declaration for&#",
9187                          N, Comp);
9188                      return;
9189
9190                   elsif Is_Record_Type (Etype (Comp)) then
9191                      Check_Recursive_Declaration (Etype (Comp));
9192                   end if;
9193                end if;
9194
9195                Next_Component (Comp);
9196             end loop;
9197          end if;
9198       end Check_Recursive_Declaration;
9199
9200    --  Start of processing for Constant_Redeclaration
9201
9202    begin
9203       if Nkind (Parent (Prev)) = N_Object_Declaration then
9204          if Nkind (Object_Definition
9205                      (Parent (Prev))) = N_Subtype_Indication
9206          then
9207             --  Find type of new declaration. The constraints of the two
9208             --  views must match statically, but there is no point in
9209             --  creating an itype for the full view.
9210
9211             if Nkind (Obj_Def) = N_Subtype_Indication then
9212                Find_Type (Subtype_Mark (Obj_Def));
9213                New_T := Entity (Subtype_Mark (Obj_Def));
9214
9215             else
9216                Find_Type (Obj_Def);
9217                New_T := Entity (Obj_Def);
9218             end if;
9219
9220             T := Etype (Prev);
9221
9222          else
9223             --  The full view may impose a constraint, even if the partial
9224             --  view does not, so construct the subtype.
9225
9226             New_T := Find_Type_Of_Object (Obj_Def, N);
9227             T     := New_T;
9228          end if;
9229
9230       else
9231          --  Current declaration is illegal, diagnosed below in Enter_Name
9232
9233          T := Empty;
9234          New_T := Any_Type;
9235       end if;
9236
9237       --  If previous full declaration exists, or if a homograph is present,
9238       --  let Enter_Name handle it, either with an error, or with the removal
9239       --  of an overridden implicit subprogram.
9240
9241       if Ekind (Prev) /= E_Constant
9242         or else Present (Expression (Parent (Prev)))
9243         or else Present (Full_View (Prev))
9244       then
9245          Enter_Name (Id);
9246
9247       --  Verify that types of both declarations match, or else that both types
9248       --  are anonymous access types whose designated subtypes statically match
9249       --  (as allowed in Ada 2005 by AI-385).
9250
9251       elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
9252         and then
9253           (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
9254              or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
9255              or else Is_Access_Constant (Etype (New_T)) /=
9256                      Is_Access_Constant (Etype (Prev))
9257              or else Can_Never_Be_Null (Etype (New_T)) /=
9258                      Can_Never_Be_Null (Etype (Prev))
9259              or else Null_Exclusion_Present (Parent (Prev)) /=
9260                      Null_Exclusion_Present (Parent (Id))
9261              or else not Subtypes_Statically_Match
9262                            (Designated_Type (Etype (Prev)),
9263                             Designated_Type (Etype (New_T))))
9264       then
9265          Error_Msg_Sloc := Sloc (Prev);
9266          Error_Msg_N ("type does not match declaration#", N);
9267          Set_Full_View (Prev, Id);
9268          Set_Etype (Id, Any_Type);
9269
9270       elsif
9271         Null_Exclusion_Present (Parent (Prev))
9272           and then not Null_Exclusion_Present (N)
9273       then
9274          Error_Msg_Sloc := Sloc (Prev);
9275          Error_Msg_N ("null-exclusion does not match declaration#", N);
9276          Set_Full_View (Prev, Id);
9277          Set_Etype (Id, Any_Type);
9278
9279       --  If so, process the full constant declaration
9280
9281       else
9282          --  RM 7.4 (6): If the subtype defined by the subtype_indication in
9283          --  the deferred declaration is constrained, then the subtype defined
9284          --  by the subtype_indication in the full declaration shall match it
9285          --  statically.
9286
9287          Check_Possible_Deferred_Completion
9288            (Prev_Id      => Prev,
9289             Prev_Obj_Def => Object_Definition (Parent (Prev)),
9290             Curr_Obj_Def => Obj_Def);
9291
9292          Set_Full_View (Prev, Id);
9293          Set_Is_Public (Id, Is_Public (Prev));
9294          Set_Is_Internal (Id);
9295          Append_Entity (Id, Current_Scope);
9296
9297          --  Check ALIASED present if present before (RM 7.4(7))
9298
9299          if Is_Aliased (Prev)
9300            and then not Aliased_Present (N)
9301          then
9302             Error_Msg_Sloc := Sloc (Prev);
9303             Error_Msg_N ("ALIASED required (see declaration#)", N);
9304          end if;
9305
9306          --  Allow incomplete declaration of tags (used to handle forward
9307          --  references to tags). The check on Ada_Tags avoids circularities
9308          --  when rebuilding the compiler.
9309
9310          if RTU_Loaded (Ada_Tags)
9311            and then T = RTE (RE_Tag)
9312          then
9313             null;
9314
9315          --  Check that placement is in private part and that the incomplete
9316          --  declaration appeared in the visible part.
9317
9318          elsif Ekind (Current_Scope) = E_Package
9319            and then not In_Private_Part (Current_Scope)
9320          then
9321             Error_Msg_Sloc := Sloc (Prev);
9322             Error_Msg_N ("full constant for declaration#"
9323                          & " must be in private part", N);
9324
9325          elsif Ekind (Current_Scope) = E_Package
9326            and then List_Containing (Parent (Prev))
9327            /= Visible_Declarations
9328              (Specification (Unit_Declaration_Node (Current_Scope)))
9329          then
9330             Error_Msg_N
9331               ("deferred constant must be declared in visible part",
9332                  Parent (Prev));
9333          end if;
9334
9335          if Is_Access_Type (T)
9336            and then Nkind (Expression (N)) = N_Allocator
9337          then
9338             Check_Recursive_Declaration (Designated_Type (T));
9339          end if;
9340       end if;
9341    end Constant_Redeclaration;
9342
9343    ----------------------
9344    -- Constrain_Access --
9345    ----------------------
9346
9347    procedure Constrain_Access
9348      (Def_Id      : in out Entity_Id;
9349       S           : Node_Id;
9350       Related_Nod : Node_Id)
9351    is
9352       T             : constant Entity_Id := Entity (Subtype_Mark (S));
9353       Desig_Type    : constant Entity_Id := Designated_Type (T);
9354       Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
9355       Constraint_OK : Boolean := True;
9356
9357       function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
9358       --  Simple predicate to test for defaulted discriminants
9359       --  Shouldn't this be in sem_util???
9360
9361       ---------------------------------
9362       -- Has_Defaulted_Discriminants --
9363       ---------------------------------
9364
9365       function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
9366       begin
9367          return Has_Discriminants (Typ)
9368           and then Present (First_Discriminant (Typ))
9369           and then Present
9370             (Discriminant_Default_Value (First_Discriminant (Typ)));
9371       end Has_Defaulted_Discriminants;
9372
9373    --  Start of processing for Constrain_Access
9374
9375    begin
9376       if Is_Array_Type (Desig_Type) then
9377          Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
9378
9379       elsif (Is_Record_Type (Desig_Type)
9380               or else Is_Incomplete_Or_Private_Type (Desig_Type))
9381         and then not Is_Constrained (Desig_Type)
9382       then
9383          --  ??? The following code is a temporary kludge to ignore a
9384          --  discriminant constraint on access type if it is constraining
9385          --  the current record. Avoid creating the implicit subtype of the
9386          --  record we are currently compiling since right now, we cannot
9387          --  handle these. For now, just return the access type itself.
9388
9389          if Desig_Type = Current_Scope
9390            and then No (Def_Id)
9391          then
9392             Set_Ekind (Desig_Subtype, E_Record_Subtype);
9393             Def_Id := Entity (Subtype_Mark (S));
9394
9395             --  This call added to ensure that the constraint is analyzed
9396             --  (needed for a B test). Note that we still return early from
9397             --  this procedure to avoid recursive processing. ???
9398
9399             Constrain_Discriminated_Type
9400               (Desig_Subtype, S, Related_Nod, For_Access => True);
9401             return;
9402          end if;
9403
9404          if (Ekind (T) = E_General_Access_Type
9405               or else Ada_Version >= Ada_05)
9406            and then Has_Private_Declaration (Desig_Type)
9407            and then In_Open_Scopes (Scope (Desig_Type))
9408            and then Has_Discriminants (Desig_Type)
9409          then
9410             --  Enforce rule that the constraint is illegal if there is
9411             --  an unconstrained view of the designated type. This means
9412             --  that the partial view (either a private type declaration or
9413             --  a derivation from a private type) has no discriminants.
9414             --  (Defect Report 8652/0008, Technical Corrigendum 1, checked
9415             --  by ACATS B371001).
9416
9417             --  Rule updated for Ada 2005: the private type is said to have
9418             --  a constrained partial view, given that objects of the type
9419             --  can be declared. Furthermore, the rule applies to all access
9420             --  types, unlike the rule concerning default discriminants.
9421
9422             declare
9423                Pack  : constant Node_Id :=
9424                          Unit_Declaration_Node (Scope (Desig_Type));
9425                Decls : List_Id;
9426                Decl  : Node_Id;
9427
9428             begin
9429                if Nkind (Pack) = N_Package_Declaration then
9430                   Decls := Visible_Declarations (Specification (Pack));
9431                   Decl := First (Decls);
9432                   while Present (Decl) loop
9433                      if (Nkind (Decl) = N_Private_Type_Declaration
9434                           and then
9435                             Chars (Defining_Identifier (Decl)) =
9436                                                      Chars (Desig_Type))
9437
9438                        or else
9439                         (Nkind (Decl) = N_Full_Type_Declaration
9440                           and then
9441                             Chars (Defining_Identifier (Decl)) =
9442                                                      Chars (Desig_Type)
9443                           and then Is_Derived_Type (Desig_Type)
9444                           and then
9445                             Has_Private_Declaration (Etype (Desig_Type)))
9446                      then
9447                         if No (Discriminant_Specifications (Decl)) then
9448                            Error_Msg_N
9449                             ("cannot constrain general access type if " &
9450                                "designated type has constrained partial view",
9451                                 S);
9452                         end if;
9453
9454                         exit;
9455                      end if;
9456
9457                      Next (Decl);
9458                   end loop;
9459                end if;
9460             end;
9461          end if;
9462
9463          Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
9464            For_Access => True);
9465
9466       elsif (Is_Task_Type (Desig_Type)
9467               or else Is_Protected_Type (Desig_Type))
9468         and then not Is_Constrained (Desig_Type)
9469       then
9470          Constrain_Concurrent
9471            (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
9472
9473       else
9474          Error_Msg_N ("invalid constraint on access type", S);
9475          Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
9476          Constraint_OK := False;
9477       end if;
9478
9479       if No (Def_Id) then
9480          Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
9481       else
9482          Set_Ekind (Def_Id, E_Access_Subtype);
9483       end if;
9484
9485       if Constraint_OK then
9486          Set_Etype (Def_Id, Base_Type (T));
9487
9488          if Is_Private_Type (Desig_Type) then
9489             Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
9490          end if;
9491       else
9492          Set_Etype (Def_Id, Any_Type);
9493       end if;
9494
9495       Set_Size_Info                (Def_Id, T);
9496       Set_Is_Constrained           (Def_Id, Constraint_OK);
9497       Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
9498       Set_Depends_On_Private       (Def_Id, Has_Private_Component (Def_Id));
9499       Set_Is_Access_Constant       (Def_Id, Is_Access_Constant (T));
9500
9501       Conditional_Delay (Def_Id, T);
9502
9503       --  AI-363 : Subtypes of general access types whose designated types have
9504       --  default discriminants are disallowed. In instances, the rule has to
9505       --  be checked against the actual, of which T is the subtype. In a
9506       --  generic body, the rule is checked assuming that the actual type has
9507       --  defaulted discriminants.
9508
9509       if Ada_Version >= Ada_05 or else Warn_On_Ada_2005_Compatibility then
9510          if Ekind (Base_Type (T)) = E_General_Access_Type
9511            and then Has_Defaulted_Discriminants (Desig_Type)
9512          then
9513             if Ada_Version < Ada_05 then
9514                Error_Msg_N
9515                  ("access subtype of general access type would not " &
9516                   "be allowed in Ada 2005?", S);
9517             else
9518                Error_Msg_N
9519                  ("access subype of general access type not allowed", S);
9520             end if;
9521
9522             Error_Msg_N ("\discriminants have defaults", S);
9523
9524          elsif Is_Access_Type (T)
9525            and then Is_Generic_Type (Desig_Type)
9526            and then Has_Discriminants (Desig_Type)
9527            and then In_Package_Body (Current_Scope)
9528          then
9529             if Ada_Version < Ada_05 then
9530                Error_Msg_N
9531                  ("access subtype would not be allowed in generic body " &
9532                   "in Ada 2005?", S);
9533             else
9534                Error_Msg_N
9535                  ("access subtype not allowed in generic body", S);
9536             end if;
9537
9538             Error_Msg_N
9539               ("\designated type is a discriminated formal", S);
9540          end if;
9541       end if;
9542    end Constrain_Access;
9543
9544    ---------------------
9545    -- Constrain_Array --
9546    ---------------------
9547
9548    procedure Constrain_Array
9549      (Def_Id      : in out Entity_Id;
9550       SI          : Node_Id;
9551       Related_Nod : Node_Id;
9552       Related_Id  : Entity_Id;
9553       Suffix      : Character)
9554    is
9555       C                     : constant Node_Id := Constraint (SI);
9556       Number_Of_Constraints : Nat := 0;
9557       Index                 : Node_Id;
9558       S, T                  : Entity_Id;
9559       Constraint_OK         : Boolean := True;
9560
9561    begin
9562       T := Entity (Subtype_Mark (SI));
9563
9564       if Ekind (T) in Access_Kind then
9565          T := Designated_Type (T);
9566       end if;
9567
9568       --  If an index constraint follows a subtype mark in a subtype indication
9569       --  then the type or subtype denoted by the subtype mark must not already
9570       --  impose an index constraint. The subtype mark must denote either an
9571       --  unconstrained array type or an access type whose designated type
9572       --  is such an array type... (RM 3.6.1)
9573
9574       if Is_Constrained (T) then
9575          Error_Msg_N
9576            ("array type is already constrained", Subtype_Mark (SI));
9577          Constraint_OK := False;
9578
9579       else
9580          S := First (Constraints (C));
9581          while Present (S) loop
9582             Number_Of_Constraints := Number_Of_Constraints + 1;
9583             Next (S);
9584          end loop;
9585
9586          --  In either case, the index constraint must provide a discrete
9587          --  range for each index of the array type and the type of each
9588          --  discrete range must be the same as that of the corresponding
9589          --  index. (RM 3.6.1)
9590
9591          if Number_Of_Constraints /= Number_Dimensions (T) then
9592             Error_Msg_NE ("incorrect number of index constraints for }", C, T);
9593             Constraint_OK := False;
9594
9595          else
9596             S := First (Constraints (C));
9597             Index := First_Index (T);
9598             Analyze (Index);
9599
9600             --  Apply constraints to each index type
9601
9602             for J in 1 .. Number_Of_Constraints loop
9603                Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
9604                Next (Index);
9605                Next (S);
9606             end loop;
9607
9608          end if;
9609       end if;
9610
9611       if No (Def_Id) then
9612          Def_Id :=
9613            Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
9614          Set_Parent (Def_Id, Related_Nod);
9615
9616       else
9617          Set_Ekind (Def_Id, E_Array_Subtype);
9618       end if;
9619
9620       Set_Size_Info      (Def_Id,                (T));
9621       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9622       Set_Etype          (Def_Id, Base_Type      (T));
9623
9624       if Constraint_OK then
9625          Set_First_Index (Def_Id, First (Constraints (C)));
9626       else
9627          Set_First_Index (Def_Id, First_Index (T));
9628       end if;
9629
9630       Set_Is_Constrained     (Def_Id, True);
9631       Set_Is_Aliased         (Def_Id, Is_Aliased (T));
9632       Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
9633
9634       Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
9635       Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
9636
9637       --  A subtype does not inherit the packed_array_type of is parent. We
9638       --  need to initialize the attribute because if Def_Id is previously
9639       --  analyzed through a limited_with clause, it will have the attributes
9640       --  of an incomplete type, one of which is an Elist that overlaps the
9641       --  Packed_Array_Type field.
9642
9643       Set_Packed_Array_Type (Def_Id, Empty);
9644
9645       --  Build a freeze node if parent still needs one. Also make sure that
9646       --  the Depends_On_Private status is set because the subtype will need
9647       --  reprocessing at the time the base type does, and also we must set a
9648       --  conditional delay.
9649
9650       Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9651       Conditional_Delay (Def_Id, T);
9652    end Constrain_Array;
9653
9654    ------------------------------
9655    -- Constrain_Component_Type --
9656    ------------------------------
9657
9658    function Constrain_Component_Type
9659      (Comp            : Entity_Id;
9660       Constrained_Typ : Entity_Id;
9661       Related_Node    : Node_Id;
9662       Typ             : Entity_Id;
9663       Constraints     : Elist_Id) return Entity_Id
9664    is
9665       Loc         : constant Source_Ptr := Sloc (Constrained_Typ);
9666       Compon_Type : constant Entity_Id := Etype (Comp);
9667
9668       function Build_Constrained_Array_Type
9669         (Old_Type : Entity_Id) return Entity_Id;
9670       --  If Old_Type is an array type, one of whose indices is constrained
9671       --  by a discriminant, build an Itype whose constraint replaces the
9672       --  discriminant with its value in the constraint.
9673
9674       function Build_Constrained_Discriminated_Type
9675         (Old_Type : Entity_Id) return Entity_Id;
9676       --  Ditto for record components
9677
9678       function Build_Constrained_Access_Type
9679         (Old_Type : Entity_Id) return Entity_Id;
9680       --  Ditto for access types. Makes use of previous two functions, to
9681       --  constrain designated type.
9682
9683       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
9684       --  T is an array or discriminated type, C is a list of constraints
9685       --  that apply to T. This routine builds the constrained subtype.
9686
9687       function Is_Discriminant (Expr : Node_Id) return Boolean;
9688       --  Returns True if Expr is a discriminant
9689
9690       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
9691       --  Find the value of discriminant Discrim in Constraint
9692
9693       -----------------------------------
9694       -- Build_Constrained_Access_Type --
9695       -----------------------------------
9696
9697       function Build_Constrained_Access_Type
9698         (Old_Type : Entity_Id) return Entity_Id
9699       is
9700          Desig_Type    : constant Entity_Id := Designated_Type (Old_Type);
9701          Itype         : Entity_Id;
9702          Desig_Subtype : Entity_Id;
9703          Scop          : Entity_Id;
9704
9705       begin
9706          --  if the original access type was not embedded in the enclosing
9707          --  type definition, there is no need to produce a new access
9708          --  subtype. In fact every access type with an explicit constraint
9709          --  generates an itype whose scope is the enclosing record.
9710
9711          if not Is_Type (Scope (Old_Type)) then
9712             return Old_Type;
9713
9714          elsif Is_Array_Type (Desig_Type) then
9715             Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
9716
9717          elsif Has_Discriminants (Desig_Type) then
9718
9719             --  This may be an access type to an enclosing record type for
9720             --  which we are constructing the constrained components. Return
9721             --  the enclosing record subtype. This is not always correct,
9722             --  but avoids infinite recursion. ???
9723
9724             Desig_Subtype := Any_Type;
9725
9726             for J in reverse 0 .. Scope_Stack.Last loop
9727                Scop := Scope_Stack.Table (J).Entity;
9728
9729                if Is_Type (Scop)
9730                  and then Base_Type (Scop) = Base_Type (Desig_Type)
9731                then
9732                   Desig_Subtype := Scop;
9733                end if;
9734
9735                exit when not Is_Type (Scop);
9736             end loop;
9737
9738             if Desig_Subtype = Any_Type then
9739                Desig_Subtype :=
9740                  Build_Constrained_Discriminated_Type (Desig_Type);
9741             end if;
9742
9743          else
9744             return Old_Type;
9745          end if;
9746
9747          if Desig_Subtype /= Desig_Type then
9748
9749             --  The Related_Node better be here or else we won't be able
9750             --  to attach new itypes to a node in the tree.
9751
9752             pragma Assert (Present (Related_Node));
9753
9754             Itype := Create_Itype (E_Access_Subtype, Related_Node);
9755
9756             Set_Etype                    (Itype, Base_Type      (Old_Type));
9757             Set_Size_Info                (Itype,                (Old_Type));
9758             Set_Directly_Designated_Type (Itype, Desig_Subtype);
9759             Set_Depends_On_Private       (Itype, Has_Private_Component
9760                                                                 (Old_Type));
9761             Set_Is_Access_Constant       (Itype, Is_Access_Constant
9762                                                                 (Old_Type));
9763
9764             --  The new itype needs freezing when it depends on a not frozen
9765             --  type and the enclosing subtype needs freezing.
9766
9767             if Has_Delayed_Freeze (Constrained_Typ)
9768               and then not Is_Frozen (Constrained_Typ)
9769             then
9770                Conditional_Delay (Itype, Base_Type (Old_Type));
9771             end if;
9772
9773             return Itype;
9774
9775          else
9776             return Old_Type;
9777          end if;
9778       end Build_Constrained_Access_Type;
9779
9780       ----------------------------------
9781       -- Build_Constrained_Array_Type --
9782       ----------------------------------
9783
9784       function Build_Constrained_Array_Type
9785         (Old_Type : Entity_Id) return Entity_Id
9786       is
9787          Lo_Expr     : Node_Id;
9788          Hi_Expr     : Node_Id;
9789          Old_Index   : Node_Id;
9790          Range_Node  : Node_Id;
9791          Constr_List : List_Id;
9792
9793          Need_To_Create_Itype : Boolean := False;
9794
9795       begin
9796          Old_Index := First_Index (Old_Type);
9797          while Present (Old_Index) loop
9798             Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
9799
9800             if Is_Discriminant (Lo_Expr)
9801               or else Is_Discriminant (Hi_Expr)
9802             then
9803                Need_To_Create_Itype := True;
9804             end if;
9805
9806             Next_Index (Old_Index);
9807          end loop;
9808
9809          if Need_To_Create_Itype then
9810             Constr_List := New_List;
9811
9812             Old_Index := First_Index (Old_Type);
9813             while Present (Old_Index) loop
9814                Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
9815
9816                if Is_Discriminant (Lo_Expr) then
9817                   Lo_Expr := Get_Discr_Value (Lo_Expr);
9818                end if;
9819
9820                if Is_Discriminant (Hi_Expr) then
9821                   Hi_Expr := Get_Discr_Value (Hi_Expr);
9822                end if;
9823
9824                Range_Node :=
9825                  Make_Range
9826                    (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
9827
9828                Append (Range_Node, To => Constr_List);
9829
9830                Next_Index (Old_Index);
9831             end loop;
9832
9833             return Build_Subtype (Old_Type, Constr_List);
9834
9835          else
9836             return Old_Type;
9837          end if;
9838       end Build_Constrained_Array_Type;
9839
9840       ------------------------------------------
9841       -- Build_Constrained_Discriminated_Type --
9842       ------------------------------------------
9843
9844       function Build_Constrained_Discriminated_Type
9845         (Old_Type : Entity_Id) return Entity_Id
9846       is
9847          Expr           : Node_Id;
9848          Constr_List    : List_Id;
9849          Old_Constraint : Elmt_Id;
9850
9851          Need_To_Create_Itype : Boolean := False;
9852
9853       begin
9854          Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
9855          while Present (Old_Constraint) loop
9856             Expr := Node (Old_Constraint);
9857
9858             if Is_Discriminant (Expr) then
9859                Need_To_Create_Itype := True;
9860             end if;
9861
9862             Next_Elmt (Old_Constraint);
9863          end loop;
9864
9865          if Need_To_Create_Itype then
9866             Constr_List := New_List;
9867
9868             Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
9869             while Present (Old_Constraint) loop
9870                Expr := Node (Old_Constraint);
9871
9872                if Is_Discriminant (Expr) then
9873                   Expr := Get_Discr_Value (Expr);
9874                end if;
9875
9876                Append (New_Copy_Tree (Expr), To => Constr_List);
9877
9878                Next_Elmt (Old_Constraint);
9879             end loop;
9880
9881             return Build_Subtype (Old_Type, Constr_List);
9882
9883          else
9884             return Old_Type;
9885          end if;
9886       end Build_Constrained_Discriminated_Type;
9887
9888       -------------------
9889       -- Build_Subtype --
9890       -------------------
9891
9892       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
9893          Indic       : Node_Id;
9894          Subtyp_Decl : Node_Id;
9895          Def_Id      : Entity_Id;
9896          Btyp        : Entity_Id := Base_Type (T);
9897
9898       begin
9899          --  The Related_Node better be here or else we won't be able to
9900          --  attach new itypes to a node in the tree.
9901
9902          pragma Assert (Present (Related_Node));
9903
9904          --  If the view of the component's type is incomplete or private
9905          --  with unknown discriminants, then the constraint must be applied
9906          --  to the full type.
9907
9908          if Has_Unknown_Discriminants (Btyp)
9909            and then Present (Underlying_Type (Btyp))
9910          then
9911             Btyp := Underlying_Type (Btyp);
9912          end if;
9913
9914          Indic :=
9915            Make_Subtype_Indication (Loc,
9916              Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
9917              Constraint   => Make_Index_Or_Discriminant_Constraint (Loc, C));
9918
9919          Def_Id := Create_Itype (Ekind (T), Related_Node);
9920
9921          Subtyp_Decl :=
9922            Make_Subtype_Declaration (Loc,
9923              Defining_Identifier => Def_Id,
9924              Subtype_Indication  => Indic);
9925
9926          Set_Parent (Subtyp_Decl, Parent (Related_Node));
9927
9928          --  Itypes must be analyzed with checks off (see package Itypes)
9929
9930          Analyze (Subtyp_Decl, Suppress => All_Checks);
9931
9932          return Def_Id;
9933       end Build_Subtype;
9934
9935       ---------------------
9936       -- Get_Discr_Value --
9937       ---------------------
9938
9939       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
9940          D : Entity_Id;
9941          E : Elmt_Id;
9942
9943       begin
9944          --  The discriminant may be declared for the type, in which case we
9945          --  find it by iterating over the list of discriminants. If the
9946          --  discriminant is inherited from a parent type, it appears as the
9947          --  corresponding discriminant of the current type. This will be the
9948          --  case when constraining an inherited component whose constraint is
9949          --  given by a discriminant of the parent.
9950
9951          D := First_Discriminant (Typ);
9952          E := First_Elmt (Constraints);
9953
9954          while Present (D) loop
9955             if D = Entity (Discrim)
9956               or else D = CR_Discriminant (Entity (Discrim))
9957               or else Corresponding_Discriminant (D) = Entity (Discrim)
9958             then
9959                return Node (E);
9960             end if;
9961
9962             Next_Discriminant (D);
9963             Next_Elmt (E);
9964          end loop;
9965
9966          --  The corresponding_Discriminant mechanism is incomplete, because
9967          --  the correspondence between new and old discriminants is not one
9968          --  to one: one new discriminant can constrain several old ones. In
9969          --  that case, scan sequentially the stored_constraint, the list of
9970          --  discriminants of the parents, and the constraints.
9971          --  Previous code checked for the present of the Stored_Constraint
9972          --  list for the derived type, but did not use it at all. Should it
9973          --  be present when the component is a discriminated task type?
9974
9975          if Is_Derived_Type (Typ)
9976            and then Scope (Entity (Discrim)) = Etype (Typ)
9977          then
9978             D := First_Discriminant (Etype (Typ));
9979             E := First_Elmt (Constraints);
9980             while Present (D) loop
9981                if D = Entity (Discrim) then
9982                   return Node (E);
9983                end if;
9984
9985                Next_Discriminant (D);
9986                Next_Elmt (E);
9987             end loop;
9988          end if;
9989
9990          --  Something is wrong if we did not find the value
9991
9992          raise Program_Error;
9993       end Get_Discr_Value;
9994
9995       ---------------------
9996       -- Is_Discriminant --
9997       ---------------------
9998
9999       function Is_Discriminant (Expr : Node_Id) return Boolean is
10000          Discrim_Scope : Entity_Id;
10001
10002       begin
10003          if Denotes_Discriminant (Expr) then
10004             Discrim_Scope := Scope (Entity (Expr));
10005
10006             --  Either we have a reference to one of Typ's discriminants,
10007
10008             pragma Assert (Discrim_Scope = Typ
10009
10010                --  or to the discriminants of the parent type, in the case
10011                --  of a derivation of a tagged type with variants.
10012
10013                or else Discrim_Scope = Etype (Typ)
10014                or else Full_View (Discrim_Scope) = Etype (Typ)
10015
10016                --  or same as above for the case where the discriminants
10017                --  were declared in Typ's private view.
10018
10019                or else (Is_Private_Type (Discrim_Scope)
10020                         and then Chars (Discrim_Scope) = Chars (Typ))
10021
10022                --  or else we are deriving from the full view and the
10023                --  discriminant is declared in the private entity.
10024
10025                or else (Is_Private_Type (Typ)
10026                          and then Chars (Discrim_Scope) = Chars (Typ))
10027
10028                --  Or we are constrained the corresponding record of a
10029                --  synchronized type that completes a private declaration.
10030
10031                or else (Is_Concurrent_Record_Type (Typ)
10032                          and then
10033                            Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
10034
10035                --  or we have a class-wide type, in which case make sure the
10036                --  discriminant found belongs to the root type.
10037
10038                or else (Is_Class_Wide_Type (Typ)
10039                          and then Etype (Typ) = Discrim_Scope));
10040
10041             return True;
10042          end if;
10043
10044          --  In all other cases we have something wrong
10045
10046          return False;
10047       end Is_Discriminant;
10048
10049    --  Start of processing for Constrain_Component_Type
10050
10051    begin
10052       if Nkind (Parent (Comp)) = N_Component_Declaration
10053         and then Comes_From_Source (Parent (Comp))
10054         and then Comes_From_Source
10055           (Subtype_Indication (Component_Definition (Parent (Comp))))
10056         and then
10057           Is_Entity_Name
10058             (Subtype_Indication (Component_Definition (Parent (Comp))))
10059       then
10060          return Compon_Type;
10061
10062       elsif Is_Array_Type (Compon_Type) then
10063          return Build_Constrained_Array_Type (Compon_Type);
10064
10065       elsif Has_Discriminants (Compon_Type) then
10066          return Build_Constrained_Discriminated_Type (Compon_Type);
10067
10068       elsif Is_Access_Type (Compon_Type) then
10069          return Build_Constrained_Access_Type (Compon_Type);
10070
10071       else
10072          return Compon_Type;
10073       end if;
10074    end Constrain_Component_Type;
10075
10076    --------------------------
10077    -- Constrain_Concurrent --
10078    --------------------------
10079
10080    --  For concurrent types, the associated record value type carries the same
10081    --  discriminants, so when we constrain a concurrent type, we must constrain
10082    --  the corresponding record type as well.
10083
10084    procedure Constrain_Concurrent
10085      (Def_Id      : in out Entity_Id;
10086       SI          : Node_Id;
10087       Related_Nod : Node_Id;
10088       Related_Id  : Entity_Id;
10089       Suffix      : Character)
10090    is
10091       T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
10092       T_Val : Entity_Id;
10093
10094    begin
10095       if Ekind (T_Ent) in Access_Kind then
10096          T_Ent := Designated_Type (T_Ent);
10097       end if;
10098
10099       T_Val := Corresponding_Record_Type (T_Ent);
10100
10101       if Present (T_Val) then
10102
10103          if No (Def_Id) then
10104             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10105          end if;
10106
10107          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10108
10109          Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10110          Set_Corresponding_Record_Type (Def_Id,
10111            Constrain_Corresponding_Record
10112              (Def_Id, T_Val, Related_Nod, Related_Id));
10113
10114       else
10115          --  If there is no associated record, expansion is disabled and this
10116          --  is a generic context. Create a subtype in any case, so that
10117          --  semantic analysis can proceed.
10118
10119          if No (Def_Id) then
10120             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10121          end if;
10122
10123          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10124       end if;
10125    end Constrain_Concurrent;
10126
10127    ------------------------------------
10128    -- Constrain_Corresponding_Record --
10129    ------------------------------------
10130
10131    function Constrain_Corresponding_Record
10132      (Prot_Subt   : Entity_Id;
10133       Corr_Rec    : Entity_Id;
10134       Related_Nod : Node_Id;
10135       Related_Id  : Entity_Id) return Entity_Id
10136    is
10137       T_Sub : constant Entity_Id :=
10138                 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
10139
10140    begin
10141       Set_Etype             (T_Sub, Corr_Rec);
10142       Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
10143       Set_Is_Constrained    (T_Sub, True);
10144       Set_First_Entity      (T_Sub, First_Entity (Corr_Rec));
10145       Set_Last_Entity       (T_Sub, Last_Entity  (Corr_Rec));
10146
10147       --  As elsewhere, we do not want to create a freeze node for this itype
10148       --  if it is created for a constrained component of an enclosing record
10149       --  because references to outer discriminants will appear out of scope.
10150
10151       if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
10152          Conditional_Delay (T_Sub, Corr_Rec);
10153       else
10154          Set_Is_Frozen (T_Sub);
10155       end if;
10156
10157       if Has_Discriminants (Prot_Subt) then -- False only if errors.
10158          Set_Discriminant_Constraint
10159            (T_Sub, Discriminant_Constraint (Prot_Subt));
10160          Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
10161          Create_Constrained_Components
10162            (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
10163       end if;
10164
10165       Set_Depends_On_Private      (T_Sub, Has_Private_Component (T_Sub));
10166
10167       return T_Sub;
10168    end Constrain_Corresponding_Record;
10169
10170    -----------------------
10171    -- Constrain_Decimal --
10172    -----------------------
10173
10174    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
10175       T           : constant Entity_Id  := Entity (Subtype_Mark (S));
10176       C           : constant Node_Id    := Constraint (S);
10177       Loc         : constant Source_Ptr := Sloc (C);
10178       Range_Expr  : Node_Id;
10179       Digits_Expr : Node_Id;
10180       Digits_Val  : Uint;
10181       Bound_Val   : Ureal;
10182
10183    begin
10184       Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
10185
10186       if Nkind (C) = N_Range_Constraint then
10187          Range_Expr := Range_Expression (C);
10188          Digits_Val := Digits_Value (T);
10189
10190       else
10191          pragma Assert (Nkind (C) = N_Digits_Constraint);
10192          Digits_Expr := Digits_Expression (C);
10193          Analyze_And_Resolve (Digits_Expr, Any_Integer);
10194
10195          Check_Digits_Expression (Digits_Expr);
10196          Digits_Val := Expr_Value (Digits_Expr);
10197
10198          if Digits_Val > Digits_Value (T) then
10199             Error_Msg_N
10200                ("digits expression is incompatible with subtype", C);
10201             Digits_Val := Digits_Value (T);
10202          end if;
10203
10204          if Present (Range_Constraint (C)) then
10205             Range_Expr := Range_Expression (Range_Constraint (C));
10206          else
10207             Range_Expr := Empty;
10208          end if;
10209       end if;
10210
10211       Set_Etype            (Def_Id, Base_Type        (T));
10212       Set_Size_Info        (Def_Id,                  (T));
10213       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
10214       Set_Delta_Value      (Def_Id, Delta_Value      (T));
10215       Set_Scale_Value      (Def_Id, Scale_Value      (T));
10216       Set_Small_Value      (Def_Id, Small_Value      (T));
10217       Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
10218       Set_Digits_Value     (Def_Id, Digits_Val);
10219
10220       --  Manufacture range from given digits value if no range present
10221
10222       if No (Range_Expr) then
10223          Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
10224          Range_Expr :=
10225            Make_Range (Loc,
10226              Low_Bound =>
10227                Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
10228              High_Bound =>
10229                Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
10230       end if;
10231
10232       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
10233       Set_Discrete_RM_Size (Def_Id);
10234
10235       --  Unconditionally delay the freeze, since we cannot set size
10236       --  information in all cases correctly until the freeze point.
10237
10238       Set_Has_Delayed_Freeze (Def_Id);
10239    end Constrain_Decimal;
10240
10241    ----------------------------------
10242    -- Constrain_Discriminated_Type --
10243    ----------------------------------
10244
10245    procedure Constrain_Discriminated_Type
10246      (Def_Id      : Entity_Id;
10247       S           : Node_Id;
10248       Related_Nod : Node_Id;
10249       For_Access  : Boolean := False)
10250    is
10251       E     : constant Entity_Id := Entity (Subtype_Mark (S));
10252       T     : Entity_Id;
10253       C     : Node_Id;
10254       Elist : Elist_Id := New_Elmt_List;
10255
10256       procedure Fixup_Bad_Constraint;
10257       --  This is called after finding a bad constraint, and after having
10258       --  posted an appropriate error message. The mission is to leave the
10259       --  entity T in as reasonable state as possible!
10260
10261       --------------------------
10262       -- Fixup_Bad_Constraint --
10263       --------------------------
10264
10265       procedure Fixup_Bad_Constraint is
10266       begin
10267          --  Set a reasonable Ekind for the entity. For an incomplete type,
10268          --  we can't do much, but for other types, we can set the proper
10269          --  corresponding subtype kind.
10270
10271          if Ekind (T) = E_Incomplete_Type then
10272             Set_Ekind (Def_Id, Ekind (T));
10273          else
10274             Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10275          end if;
10276
10277          --  Set Etype to the known type, to reduce chances of cascaded errors
10278
10279          Set_Etype (Def_Id, E);
10280          Set_Error_Posted (Def_Id);
10281       end Fixup_Bad_Constraint;
10282
10283    --  Start of processing for Constrain_Discriminated_Type
10284
10285    begin
10286       C := Constraint (S);
10287
10288       --  A discriminant constraint is only allowed in a subtype indication,
10289       --  after a subtype mark. This subtype mark must denote either a type
10290       --  with discriminants, or an access type whose designated type is a
10291       --  type with discriminants. A discriminant constraint specifies the
10292       --  values of these discriminants (RM 3.7.2(5)).
10293
10294       T := Base_Type (Entity (Subtype_Mark (S)));
10295
10296       if Ekind (T) in Access_Kind then
10297          T := Designated_Type (T);
10298       end if;
10299
10300       --  Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
10301       --  Avoid generating an error for access-to-incomplete subtypes.
10302
10303       if Ada_Version >= Ada_05
10304         and then Ekind (T) = E_Incomplete_Type
10305         and then Nkind (Parent (S)) = N_Subtype_Declaration
10306         and then not Is_Itype (Def_Id)
10307       then
10308          --  A little sanity check, emit an error message if the type
10309          --  has discriminants to begin with. Type T may be a regular
10310          --  incomplete type or imported via a limited with clause.
10311
10312          if Has_Discriminants (T)
10313            or else
10314              (From_With_Type (T)
10315                 and then Present (Non_Limited_View (T))
10316                 and then Nkind (Parent (Non_Limited_View (T))) =
10317                            N_Full_Type_Declaration
10318                 and then Present (Discriminant_Specifications
10319                           (Parent (Non_Limited_View (T)))))
10320          then
10321             Error_Msg_N
10322               ("(Ada 2005) incomplete subtype may not be constrained", C);
10323          else
10324             Error_Msg_N
10325               ("invalid constraint: type has no discriminant", C);
10326          end if;
10327
10328          Fixup_Bad_Constraint;
10329          return;
10330
10331       --  Check that the type has visible discriminants. The type may be
10332       --  a private type with unknown discriminants whose full view has
10333       --  discriminants which are invisible.
10334
10335       elsif not Has_Discriminants (T)
10336         or else
10337           (Has_Unknown_Discriminants (T)
10338              and then Is_Private_Type (T))
10339       then
10340          Error_Msg_N ("invalid constraint: type has no discriminant", C);
10341          Fixup_Bad_Constraint;
10342          return;
10343
10344       elsif Is_Constrained (E)
10345         or else (Ekind (E) = E_Class_Wide_Subtype
10346                   and then Present (Discriminant_Constraint (E)))
10347       then
10348          Error_Msg_N ("type is already constrained", Subtype_Mark (S));
10349          Fixup_Bad_Constraint;
10350          return;
10351       end if;
10352
10353       --  T may be an unconstrained subtype (e.g. a generic actual).
10354       --  Constraint applies to the base type.
10355
10356       T := Base_Type (T);
10357
10358       Elist := Build_Discriminant_Constraints (T, S);
10359
10360       --  If the list returned was empty we had an error in building the
10361       --  discriminant constraint. We have also already signalled an error
10362       --  in the incomplete type case
10363
10364       if Is_Empty_Elmt_List (Elist) then
10365          Fixup_Bad_Constraint;
10366          return;
10367       end if;
10368
10369       Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
10370    end Constrain_Discriminated_Type;
10371
10372    ---------------------------
10373    -- Constrain_Enumeration --
10374    ---------------------------
10375
10376    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
10377       T : constant Entity_Id := Entity (Subtype_Mark (S));
10378       C : constant Node_Id   := Constraint (S);
10379
10380    begin
10381       Set_Ekind (Def_Id, E_Enumeration_Subtype);
10382
10383       Set_First_Literal     (Def_Id, First_Literal (Base_Type (T)));
10384
10385       Set_Etype             (Def_Id, Base_Type         (T));
10386       Set_Size_Info         (Def_Id,                   (T));
10387       Set_First_Rep_Item    (Def_Id, First_Rep_Item    (T));
10388       Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10389
10390       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10391
10392       Set_Discrete_RM_Size (Def_Id);
10393    end Constrain_Enumeration;
10394
10395    ----------------------
10396    -- Constrain_Float --
10397    ----------------------
10398
10399    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
10400       T    : constant Entity_Id := Entity (Subtype_Mark (S));
10401       C    : Node_Id;
10402       D    : Node_Id;
10403       Rais : Node_Id;
10404
10405    begin
10406       Set_Ekind (Def_Id, E_Floating_Point_Subtype);
10407
10408       Set_Etype          (Def_Id, Base_Type      (T));
10409       Set_Size_Info      (Def_Id,                (T));
10410       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10411
10412       --  Process the constraint
10413
10414       C := Constraint (S);
10415
10416       --  Digits constraint present
10417
10418       if Nkind (C) = N_Digits_Constraint then
10419          Check_Restriction (No_Obsolescent_Features, C);
10420
10421          if Warn_On_Obsolescent_Feature then
10422             Error_Msg_N
10423               ("subtype digits constraint is an " &
10424                "obsolescent feature (RM J.3(8))?", C);
10425          end if;
10426
10427          D := Digits_Expression (C);
10428          Analyze_And_Resolve (D, Any_Integer);
10429          Check_Digits_Expression (D);
10430          Set_Digits_Value (Def_Id, Expr_Value (D));
10431
10432          --  Check that digits value is in range. Obviously we can do this
10433          --  at compile time, but it is strictly a runtime check, and of
10434          --  course there is an ACVC test that checks this!
10435
10436          if Digits_Value (Def_Id) > Digits_Value (T) then
10437             Error_Msg_Uint_1 := Digits_Value (T);
10438             Error_Msg_N ("?digits value is too large, maximum is ^", D);
10439             Rais :=
10440               Make_Raise_Constraint_Error (Sloc (D),
10441                 Reason => CE_Range_Check_Failed);
10442             Insert_Action (Declaration_Node (Def_Id), Rais);
10443          end if;
10444
10445          C := Range_Constraint (C);
10446
10447       --  No digits constraint present
10448
10449       else
10450          Set_Digits_Value (Def_Id, Digits_Value (T));
10451       end if;
10452
10453       --  Range constraint present
10454
10455       if Nkind (C) = N_Range_Constraint then
10456          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10457
10458       --  No range constraint present
10459
10460       else
10461          pragma Assert (No (C));
10462          Set_Scalar_Range (Def_Id, Scalar_Range (T));
10463       end if;
10464
10465       Set_Is_Constrained (Def_Id);
10466    end Constrain_Float;
10467
10468    ---------------------
10469    -- Constrain_Index --
10470    ---------------------
10471
10472    procedure Constrain_Index
10473      (Index        : Node_Id;
10474       S            : Node_Id;
10475       Related_Nod  : Node_Id;
10476       Related_Id   : Entity_Id;
10477       Suffix       : Character;
10478       Suffix_Index : Nat)
10479    is
10480       Def_Id : Entity_Id;
10481       R      : Node_Id := Empty;
10482       T      : constant Entity_Id := Etype (Index);
10483
10484    begin
10485       if Nkind (S) = N_Range
10486         or else
10487           (Nkind (S) = N_Attribute_Reference
10488             and then Attribute_Name (S) = Name_Range)
10489       then
10490          --  A Range attribute will transformed into N_Range by Resolve
10491
10492          Analyze (S);
10493          Set_Etype (S, T);
10494          R := S;
10495
10496          Process_Range_Expr_In_Decl (R, T, Empty_List);
10497
10498          if not Error_Posted (S)
10499            and then
10500              (Nkind (S) /= N_Range
10501                or else not Covers (T, (Etype (Low_Bound (S))))
10502                or else not Covers (T, (Etype (High_Bound (S)))))
10503          then
10504             if Base_Type (T) /= Any_Type
10505               and then Etype (Low_Bound (S)) /= Any_Type
10506               and then Etype (High_Bound (S)) /= Any_Type
10507             then
10508                Error_Msg_N ("range expected", S);
10509             end if;
10510          end if;
10511
10512       elsif Nkind (S) = N_Subtype_Indication then
10513
10514          --  The parser has verified that this is a discrete indication
10515
10516          Resolve_Discrete_Subtype_Indication (S, T);
10517          R := Range_Expression (Constraint (S));
10518
10519       elsif Nkind (S) = N_Discriminant_Association then
10520
10521          --  Syntactically valid in subtype indication
10522
10523          Error_Msg_N ("invalid index constraint", S);
10524          Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10525          return;
10526
10527       --  Subtype_Mark case, no anonymous subtypes to construct
10528
10529       else
10530          Analyze (S);
10531
10532          if Is_Entity_Name (S) then
10533             if not Is_Type (Entity (S)) then
10534                Error_Msg_N ("expect subtype mark for index constraint", S);
10535
10536             elsif Base_Type (Entity (S)) /= Base_Type (T) then
10537                Wrong_Type (S, Base_Type (T));
10538             end if;
10539
10540             return;
10541
10542          else
10543             Error_Msg_N ("invalid index constraint", S);
10544             Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10545             return;
10546          end if;
10547       end if;
10548
10549       Def_Id :=
10550         Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
10551
10552       Set_Etype (Def_Id, Base_Type (T));
10553
10554       if Is_Modular_Integer_Type (T) then
10555          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10556
10557       elsif Is_Integer_Type (T) then
10558          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10559
10560       else
10561          Set_Ekind (Def_Id, E_Enumeration_Subtype);
10562          Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10563       end if;
10564
10565       Set_Size_Info      (Def_Id,                (T));
10566       Set_RM_Size        (Def_Id, RM_Size        (T));
10567       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10568
10569       Set_Scalar_Range   (Def_Id, R);
10570
10571       Set_Etype (S, Def_Id);
10572       Set_Discrete_RM_Size (Def_Id);
10573    end Constrain_Index;
10574
10575    -----------------------
10576    -- Constrain_Integer --
10577    -----------------------
10578
10579    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
10580       T : constant Entity_Id := Entity (Subtype_Mark (S));
10581       C : constant Node_Id   := Constraint (S);
10582
10583    begin
10584       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10585
10586       if Is_Modular_Integer_Type (T) then
10587          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10588       else
10589          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10590       end if;
10591
10592       Set_Etype            (Def_Id, Base_Type        (T));
10593       Set_Size_Info        (Def_Id,                  (T));
10594       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
10595       Set_Discrete_RM_Size (Def_Id);
10596    end Constrain_Integer;
10597
10598    ------------------------------
10599    -- Constrain_Ordinary_Fixed --
10600    ------------------------------
10601
10602    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
10603       T    : constant Entity_Id := Entity (Subtype_Mark (S));
10604       C    : Node_Id;
10605       D    : Node_Id;
10606       Rais : Node_Id;
10607
10608    begin
10609       Set_Ekind          (Def_Id, E_Ordinary_Fixed_Point_Subtype);
10610       Set_Etype          (Def_Id, Base_Type        (T));
10611       Set_Size_Info      (Def_Id,                  (T));
10612       Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
10613       Set_Small_Value    (Def_Id, Small_Value      (T));
10614
10615       --  Process the constraint
10616
10617       C := Constraint (S);
10618
10619       --  Delta constraint present
10620
10621       if Nkind (C) = N_Delta_Constraint then
10622          Check_Restriction (No_Obsolescent_Features, C);
10623
10624          if Warn_On_Obsolescent_Feature then
10625             Error_Msg_S
10626               ("subtype delta constraint is an " &
10627                "obsolescent feature (RM J.3(7))?");
10628          end if;
10629
10630          D := Delta_Expression (C);
10631          Analyze_And_Resolve (D, Any_Real);
10632          Check_Delta_Expression (D);
10633          Set_Delta_Value (Def_Id, Expr_Value_R (D));
10634
10635          --  Check that delta value is in range. Obviously we can do this
10636          --  at compile time, but it is strictly a runtime check, and of
10637          --  course there is an ACVC test that checks this!
10638
10639          if Delta_Value (Def_Id) < Delta_Value (T) then
10640             Error_Msg_N ("?delta value is too small", D);
10641             Rais :=
10642               Make_Raise_Constraint_Error (Sloc (D),
10643                 Reason => CE_Range_Check_Failed);
10644             Insert_Action (Declaration_Node (Def_Id), Rais);
10645          end if;
10646
10647          C := Range_Constraint (C);
10648
10649       --  No delta constraint present
10650
10651       else
10652          Set_Delta_Value (Def_Id, Delta_Value (T));
10653       end if;
10654
10655       --  Range constraint present
10656
10657       if Nkind (C) = N_Range_Constraint then
10658          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10659
10660       --  No range constraint present
10661
10662       else
10663          pragma Assert (No (C));
10664          Set_Scalar_Range (Def_Id, Scalar_Range (T));
10665
10666       end if;
10667
10668       Set_Discrete_RM_Size (Def_Id);
10669
10670       --  Unconditionally delay the freeze, since we cannot set size
10671       --  information in all cases correctly until the freeze point.
10672
10673       Set_Has_Delayed_Freeze (Def_Id);
10674    end Constrain_Ordinary_Fixed;
10675
10676    -----------------------
10677    -- Contain_Interface --
10678    -----------------------
10679
10680    function Contain_Interface
10681      (Iface  : Entity_Id;
10682       Ifaces : Elist_Id) return Boolean
10683    is
10684       Iface_Elmt : Elmt_Id;
10685
10686    begin
10687       if Present (Ifaces) then
10688          Iface_Elmt := First_Elmt (Ifaces);
10689          while Present (Iface_Elmt) loop
10690             if Node (Iface_Elmt) = Iface then
10691                return True;
10692             end if;
10693
10694             Next_Elmt (Iface_Elmt);
10695          end loop;
10696       end if;
10697
10698       return False;
10699    end Contain_Interface;
10700
10701    ---------------------------
10702    -- Convert_Scalar_Bounds --
10703    ---------------------------
10704
10705    procedure Convert_Scalar_Bounds
10706      (N            : Node_Id;
10707       Parent_Type  : Entity_Id;
10708       Derived_Type : Entity_Id;
10709       Loc          : Source_Ptr)
10710    is
10711       Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
10712
10713       Lo  : Node_Id;
10714       Hi  : Node_Id;
10715       Rng : Node_Id;
10716
10717    begin
10718       Lo := Build_Scalar_Bound
10719               (Type_Low_Bound (Derived_Type),
10720                Parent_Type, Implicit_Base);
10721
10722       Hi := Build_Scalar_Bound
10723               (Type_High_Bound (Derived_Type),
10724                Parent_Type, Implicit_Base);
10725
10726       Rng :=
10727         Make_Range (Loc,
10728           Low_Bound  => Lo,
10729           High_Bound => Hi);
10730
10731       Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
10732
10733       Set_Parent (Rng, N);
10734       Set_Scalar_Range (Derived_Type, Rng);
10735
10736       --  Analyze the bounds
10737
10738       Analyze_And_Resolve (Lo, Implicit_Base);
10739       Analyze_And_Resolve (Hi, Implicit_Base);
10740
10741       --  Analyze the range itself, except that we do not analyze it if
10742       --  the bounds are real literals, and we have a fixed-point type.
10743       --  The reason for this is that we delay setting the bounds in this
10744       --  case till we know the final Small and Size values (see circuit
10745       --  in Freeze.Freeze_Fixed_Point_Type for further details).
10746
10747       if Is_Fixed_Point_Type (Parent_Type)
10748         and then Nkind (Lo) = N_Real_Literal
10749         and then Nkind (Hi) = N_Real_Literal
10750       then
10751          return;
10752
10753       --  Here we do the analysis of the range
10754
10755       --  Note: we do this manually, since if we do a normal Analyze and
10756       --  Resolve call, there are problems with the conversions used for
10757       --  the derived type range.
10758
10759       else
10760          Set_Etype    (Rng, Implicit_Base);
10761          Set_Analyzed (Rng, True);
10762       end if;
10763    end Convert_Scalar_Bounds;
10764
10765    -------------------
10766    -- Copy_And_Swap --
10767    -------------------
10768
10769    procedure Copy_And_Swap (Priv, Full : Entity_Id) is
10770    begin
10771       --  Initialize new full declaration entity by copying the pertinent
10772       --  fields of the corresponding private declaration entity.
10773
10774       --  We temporarily set Ekind to a value appropriate for a type to
10775       --  avoid assert failures in Einfo from checking for setting type
10776       --  attributes on something that is not a type. Ekind (Priv) is an
10777       --  appropriate choice, since it allowed the attributes to be set
10778       --  in the first place. This Ekind value will be modified later.
10779
10780       Set_Ekind (Full, Ekind (Priv));
10781
10782       --  Also set Etype temporarily to Any_Type, again, in the absence
10783       --  of errors, it will be properly reset, and if there are errors,
10784       --  then we want a value of Any_Type to remain.
10785
10786       Set_Etype (Full, Any_Type);
10787
10788       --  Now start copying attributes
10789
10790       Set_Has_Discriminants          (Full, Has_Discriminants       (Priv));
10791
10792       if Has_Discriminants (Full) then
10793          Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
10794          Set_Stored_Constraint       (Full, Stored_Constraint       (Priv));
10795       end if;
10796
10797       Set_First_Rep_Item             (Full, First_Rep_Item          (Priv));
10798       Set_Homonym                    (Full, Homonym                 (Priv));
10799       Set_Is_Immediately_Visible     (Full, Is_Immediately_Visible  (Priv));
10800       Set_Is_Public                  (Full, Is_Public               (Priv));
10801       Set_Is_Pure                    (Full, Is_Pure                 (Priv));
10802       Set_Is_Tagged_Type             (Full, Is_Tagged_Type          (Priv));
10803       Set_Has_Pragma_Unreferenced    (Full, Has_Pragma_Unreferenced (Priv));
10804       Set_Has_Pragma_Unreferenced_Objects
10805                                      (Full, Has_Pragma_Unreferenced_Objects
10806                                                                     (Priv));
10807
10808       Conditional_Delay              (Full,                          Priv);
10809
10810       if Is_Tagged_Type (Full) then
10811          Set_Primitive_Operations    (Full, Primitive_Operations    (Priv));
10812
10813          if Priv = Base_Type (Priv) then
10814             Set_Class_Wide_Type      (Full, Class_Wide_Type         (Priv));
10815          end if;
10816       end if;
10817
10818       Set_Is_Volatile                (Full, Is_Volatile             (Priv));
10819       Set_Treat_As_Volatile          (Full, Treat_As_Volatile       (Priv));
10820       Set_Scope                      (Full, Scope                   (Priv));
10821       Set_Next_Entity                (Full, Next_Entity             (Priv));
10822       Set_First_Entity               (Full, First_Entity            (Priv));
10823       Set_Last_Entity                (Full, Last_Entity             (Priv));
10824
10825       --  If access types have been recorded for later handling, keep them in
10826       --  the full view so that they get handled when the full view freeze
10827       --  node is expanded.
10828
10829       if Present (Freeze_Node (Priv))
10830         and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
10831       then
10832          Ensure_Freeze_Node (Full);
10833          Set_Access_Types_To_Process
10834            (Freeze_Node (Full),
10835             Access_Types_To_Process (Freeze_Node (Priv)));
10836       end if;
10837
10838       --  Swap the two entities. Now Privat is the full type entity and
10839       --  Full is the private one. They will be swapped back at the end
10840       --  of the private part. This swapping ensures that the entity that
10841       --  is visible in the private part is the full declaration.
10842
10843       Exchange_Entities (Priv, Full);
10844       Append_Entity (Full, Scope (Full));
10845    end Copy_And_Swap;
10846
10847    -------------------------------------
10848    -- Copy_Array_Base_Type_Attributes --
10849    -------------------------------------
10850
10851    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
10852    begin
10853       Set_Component_Alignment      (T1, Component_Alignment      (T2));
10854       Set_Component_Type           (T1, Component_Type           (T2));
10855       Set_Component_Size           (T1, Component_Size           (T2));
10856       Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
10857       Set_Finalize_Storage_Only    (T1, Finalize_Storage_Only    (T2));
10858       Set_Has_Non_Standard_Rep     (T1, Has_Non_Standard_Rep     (T2));
10859       Set_Has_Task                 (T1, Has_Task                 (T2));
10860       Set_Is_Packed                (T1, Is_Packed                (T2));
10861       Set_Has_Aliased_Components   (T1, Has_Aliased_Components   (T2));
10862       Set_Has_Atomic_Components    (T1, Has_Atomic_Components    (T2));
10863       Set_Has_Volatile_Components  (T1, Has_Volatile_Components  (T2));
10864    end Copy_Array_Base_Type_Attributes;
10865
10866    -----------------------------------
10867    -- Copy_Array_Subtype_Attributes --
10868    -----------------------------------
10869
10870    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
10871    begin
10872       Set_Size_Info (T1, T2);
10873
10874       Set_First_Index          (T1, First_Index           (T2));
10875       Set_Is_Aliased           (T1, Is_Aliased            (T2));
10876       Set_Is_Atomic            (T1, Is_Atomic             (T2));
10877       Set_Is_Volatile          (T1, Is_Volatile           (T2));
10878       Set_Treat_As_Volatile    (T1, Treat_As_Volatile     (T2));
10879       Set_Is_Constrained       (T1, Is_Constrained        (T2));
10880       Set_Depends_On_Private   (T1, Has_Private_Component (T2));
10881       Set_First_Rep_Item       (T1, First_Rep_Item        (T2));
10882       Set_Convention           (T1, Convention            (T2));
10883       Set_Is_Limited_Composite (T1, Is_Limited_Composite  (T2));
10884       Set_Is_Private_Composite (T1, Is_Private_Composite  (T2));
10885    end Copy_Array_Subtype_Attributes;
10886
10887    -----------------------------------
10888    -- Create_Constrained_Components --
10889    -----------------------------------
10890
10891    procedure Create_Constrained_Components
10892      (Subt        : Entity_Id;
10893       Decl_Node   : Node_Id;
10894       Typ         : Entity_Id;
10895       Constraints : Elist_Id)
10896    is
10897       Loc         : constant Source_Ptr := Sloc (Subt);
10898       Comp_List   : constant Elist_Id   := New_Elmt_List;
10899       Parent_Type : constant Entity_Id  := Etype (Typ);
10900       Assoc_List  : constant List_Id    := New_List;
10901       Discr_Val   : Elmt_Id;
10902       Errors      : Boolean;
10903       New_C       : Entity_Id;
10904       Old_C       : Entity_Id;
10905       Is_Static   : Boolean := True;
10906
10907       procedure Collect_Fixed_Components (Typ : Entity_Id);
10908       --  Collect parent type components that do not appear in a variant part
10909
10910       procedure Create_All_Components;
10911       --  Iterate over Comp_List to create the components of the subtype
10912
10913       function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
10914       --  Creates a new component from Old_Compon, copying all the fields from
10915       --  it, including its Etype, inserts the new component in the Subt entity
10916       --  chain and returns the new component.
10917
10918       function Is_Variant_Record (T : Entity_Id) return Boolean;
10919       --  If true, and discriminants are static, collect only components from
10920       --  variants selected by discriminant values.
10921
10922       ------------------------------
10923       -- Collect_Fixed_Components --
10924       ------------------------------
10925
10926       procedure Collect_Fixed_Components (Typ : Entity_Id) is
10927       begin
10928       --  Build association list for discriminants, and find components of the
10929       --  variant part selected by the values of the discriminants.
10930
10931          Old_C := First_Discriminant (Typ);
10932          Discr_Val := First_Elmt (Constraints);
10933          while Present (Old_C) loop
10934             Append_To (Assoc_List,
10935               Make_Component_Association (Loc,
10936                  Choices    => New_List (New_Occurrence_Of (Old_C, Loc)),
10937                  Expression => New_Copy (Node (Discr_Val))));
10938
10939             Next_Elmt (Discr_Val);
10940             Next_Discriminant (Old_C);
10941          end loop;
10942
10943          --  The tag, and the possible parent and controller components
10944          --  are unconditionally in the subtype.
10945
10946          if Is_Tagged_Type (Typ)
10947            or else Has_Controlled_Component (Typ)
10948          then
10949             Old_C := First_Component (Typ);
10950             while Present (Old_C) loop
10951                if Chars ((Old_C)) = Name_uTag
10952                  or else Chars ((Old_C)) = Name_uParent
10953                  or else Chars ((Old_C)) = Name_uController
10954                then
10955                   Append_Elmt (Old_C, Comp_List);
10956                end if;
10957
10958                Next_Component (Old_C);
10959             end loop;
10960          end if;
10961       end Collect_Fixed_Components;
10962
10963       ---------------------------
10964       -- Create_All_Components --
10965       ---------------------------
10966
10967       procedure Create_All_Components is
10968          Comp : Elmt_Id;
10969
10970       begin
10971          Comp := First_Elmt (Comp_List);
10972          while Present (Comp) loop
10973             Old_C := Node (Comp);
10974             New_C := Create_Component (Old_C);
10975
10976             Set_Etype
10977               (New_C,
10978                Constrain_Component_Type
10979                  (Old_C, Subt, Decl_Node, Typ, Constraints));
10980             Set_Is_Public (New_C, Is_Public (Subt));
10981
10982             Next_Elmt (Comp);
10983          end loop;
10984       end Create_All_Components;
10985
10986       ----------------------
10987       -- Create_Component --
10988       ----------------------
10989
10990       function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
10991          New_Compon : constant Entity_Id := New_Copy (Old_Compon);
10992
10993       begin
10994          if Ekind (Old_Compon) = E_Discriminant
10995            and then Is_Completely_Hidden (Old_Compon)
10996          then
10997             --  This is a shadow discriminant created for a discriminant of
10998             --  the parent type that is one of several renamed by the same
10999             --  new discriminant. Give the shadow discriminant an internal
11000             --  name that cannot conflict with that of visible components.
11001
11002             Set_Chars (New_Compon, New_Internal_Name ('C'));
11003          end if;
11004
11005          --  Set the parent so we have a proper link for freezing etc. This is
11006          --  not a real parent pointer, since of course our parent does not own
11007          --  up to us and reference us, we are an illegitimate child of the
11008          --  original parent!
11009
11010          Set_Parent (New_Compon, Parent (Old_Compon));
11011
11012          --  If the old component's Esize was already determined and is a
11013          --  static value, then the new component simply inherits it. Otherwise
11014          --  the old component's size may require run-time determination, but
11015          --  the new component's size still might be statically determinable
11016          --  (if, for example it has a static constraint). In that case we want
11017          --  Layout_Type to recompute the component's size, so we reset its
11018          --  size and positional fields.
11019
11020          if Frontend_Layout_On_Target
11021            and then not Known_Static_Esize (Old_Compon)
11022          then
11023             Set_Esize (New_Compon, Uint_0);
11024             Init_Normalized_First_Bit    (New_Compon);
11025             Init_Normalized_Position     (New_Compon);
11026             Init_Normalized_Position_Max (New_Compon);
11027          end if;
11028
11029          --  We do not want this node marked as Comes_From_Source, since
11030          --  otherwise it would get first class status and a separate cross-
11031          --  reference line would be generated. Illegitimate children do not
11032          --  rate such recognition.
11033
11034          Set_Comes_From_Source (New_Compon, False);
11035
11036          --  But it is a real entity, and a birth certificate must be properly
11037          --  registered by entering it into the entity list.
11038
11039          Enter_Name (New_Compon);
11040
11041          return New_Compon;
11042       end Create_Component;
11043
11044       -----------------------
11045       -- Is_Variant_Record --
11046       -----------------------
11047
11048       function Is_Variant_Record (T : Entity_Id) return Boolean is
11049       begin
11050          return Nkind (Parent (T)) = N_Full_Type_Declaration
11051            and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
11052            and then Present (Component_List (Type_Definition (Parent (T))))
11053            and then
11054              Present
11055                (Variant_Part (Component_List (Type_Definition (Parent (T)))));
11056       end Is_Variant_Record;
11057
11058    --  Start of processing for Create_Constrained_Components
11059
11060    begin
11061       pragma Assert (Subt /= Base_Type (Subt));
11062       pragma Assert (Typ = Base_Type (Typ));
11063
11064       Set_First_Entity (Subt, Empty);
11065       Set_Last_Entity  (Subt, Empty);
11066
11067       --  Check whether constraint is fully static, in which case we can
11068       --  optimize the list of components.
11069
11070       Discr_Val := First_Elmt (Constraints);
11071       while Present (Discr_Val) loop
11072          if not Is_OK_Static_Expression (Node (Discr_Val)) then
11073             Is_Static := False;
11074             exit;
11075          end if;
11076
11077          Next_Elmt (Discr_Val);
11078       end loop;
11079
11080       Set_Has_Static_Discriminants (Subt, Is_Static);
11081
11082       Push_Scope (Subt);
11083
11084       --  Inherit the discriminants of the parent type
11085
11086       Add_Discriminants : declare
11087          Num_Disc : Int;
11088          Num_Gird : Int;
11089
11090       begin
11091          Num_Disc := 0;
11092          Old_C := First_Discriminant (Typ);
11093
11094          while Present (Old_C) loop
11095             Num_Disc := Num_Disc + 1;
11096             New_C := Create_Component (Old_C);
11097             Set_Is_Public (New_C, Is_Public (Subt));
11098             Next_Discriminant (Old_C);
11099          end loop;
11100
11101          --  For an untagged derived subtype, the number of discriminants may
11102          --  be smaller than the number of inherited discriminants, because
11103          --  several of them may be renamed by a single new discriminant.
11104          --  In this case, add the hidden discriminants back into the subtype,
11105          --  because otherwise the size of the subtype is computed incorrectly
11106          --  in GCC 4.1.
11107
11108          Num_Gird := 0;
11109
11110          if Is_Derived_Type (Typ)
11111            and then not Is_Tagged_Type (Typ)
11112          then
11113             Old_C := First_Stored_Discriminant (Typ);
11114
11115             while Present (Old_C) loop
11116                Num_Gird := Num_Gird + 1;
11117                Next_Stored_Discriminant (Old_C);
11118             end loop;
11119          end if;
11120
11121          if Num_Gird > Num_Disc then
11122
11123             --  Find out multiple uses of new discriminants, and add hidden
11124             --  components for the extra renamed discriminants. We recognize
11125             --  multiple uses through the Corresponding_Discriminant of a
11126             --  new discriminant: if it constrains several old discriminants,
11127             --  this field points to the last one in the parent type. The
11128             --  stored discriminants of the derived type have the same name
11129             --  as those of the parent.
11130
11131             declare
11132                Constr    : Elmt_Id;
11133                New_Discr : Entity_Id;
11134                Old_Discr : Entity_Id;
11135
11136             begin
11137                Constr    := First_Elmt (Stored_Constraint (Typ));
11138                Old_Discr := First_Stored_Discriminant (Typ);
11139                while Present (Constr) loop
11140                   if Is_Entity_Name (Node (Constr))
11141                     and then Ekind (Entity (Node (Constr))) = E_Discriminant
11142                   then
11143                      New_Discr := Entity (Node (Constr));
11144
11145                      if Chars (Corresponding_Discriminant (New_Discr)) /=
11146                         Chars (Old_Discr)
11147                      then
11148                         --  The new discriminant has been used to rename a
11149                         --  subsequent old discriminant. Introduce a shadow
11150                         --  component for the current old discriminant.
11151
11152                         New_C := Create_Component (Old_Discr);
11153                         Set_Original_Record_Component  (New_C, Old_Discr);
11154                      end if;
11155                   end if;
11156
11157                   Next_Elmt (Constr);
11158                   Next_Stored_Discriminant (Old_Discr);
11159                end loop;
11160             end;
11161          end if;
11162       end Add_Discriminants;
11163
11164       if Is_Static
11165         and then Is_Variant_Record (Typ)
11166       then
11167          Collect_Fixed_Components (Typ);
11168
11169          Gather_Components (
11170            Typ,
11171            Component_List (Type_Definition (Parent (Typ))),
11172            Governed_By   => Assoc_List,
11173            Into          => Comp_List,
11174            Report_Errors => Errors);
11175          pragma Assert (not Errors);
11176
11177          Create_All_Components;
11178
11179       --  If the subtype declaration is created for a tagged type derivation
11180       --  with constraints, we retrieve the record definition of the parent
11181       --  type to select the components of the proper variant.
11182
11183       elsif Is_Static
11184         and then Is_Tagged_Type (Typ)
11185         and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11186         and then
11187           Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
11188         and then Is_Variant_Record (Parent_Type)
11189       then
11190          Collect_Fixed_Components (Typ);
11191
11192          Gather_Components (
11193            Typ,
11194            Component_List (Type_Definition (Parent (Parent_Type))),
11195            Governed_By   => Assoc_List,
11196            Into          => Comp_List,
11197            Report_Errors => Errors);
11198          pragma Assert (not Errors);
11199
11200          --  If the tagged derivation has a type extension, collect all the
11201          --  new components therein.
11202
11203          if Present
11204               (Record_Extension_Part (Type_Definition (Parent (Typ))))
11205          then
11206             Old_C := First_Component (Typ);
11207             while Present (Old_C) loop
11208                if Original_Record_Component (Old_C) = Old_C
11209                 and then Chars (Old_C) /= Name_uTag
11210                 and then Chars (Old_C) /= Name_uParent
11211                 and then Chars (Old_C) /= Name_uController
11212                then
11213                   Append_Elmt (Old_C, Comp_List);
11214                end if;
11215
11216                Next_Component (Old_C);
11217             end loop;
11218          end if;
11219
11220          Create_All_Components;
11221
11222       else
11223          --  If discriminants are not static, or if this is a multi-level type
11224          --  extension, we have to include all components of the parent type.
11225
11226          Old_C := First_Component (Typ);
11227          while Present (Old_C) loop
11228             New_C := Create_Component (Old_C);
11229
11230             Set_Etype
11231               (New_C,
11232                Constrain_Component_Type
11233                  (Old_C, Subt, Decl_Node, Typ, Constraints));
11234             Set_Is_Public (New_C, Is_Public (Subt));
11235
11236             Next_Component (Old_C);
11237          end loop;
11238       end if;
11239
11240       End_Scope;
11241    end Create_Constrained_Components;
11242
11243    ------------------------------------------
11244    -- Decimal_Fixed_Point_Type_Declaration --
11245    ------------------------------------------
11246
11247    procedure Decimal_Fixed_Point_Type_Declaration
11248      (T   : Entity_Id;
11249       Def : Node_Id)
11250    is
11251       Loc           : constant Source_Ptr := Sloc (Def);
11252       Digs_Expr     : constant Node_Id    := Digits_Expression (Def);
11253       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
11254       Implicit_Base : Entity_Id;
11255       Digs_Val      : Uint;
11256       Delta_Val     : Ureal;
11257       Scale_Val     : Uint;
11258       Bound_Val     : Ureal;
11259
11260    begin
11261       Check_Restriction (No_Fixed_Point, Def);
11262
11263       --  Create implicit base type
11264
11265       Implicit_Base :=
11266         Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
11267       Set_Etype (Implicit_Base, Implicit_Base);
11268
11269       --  Analyze and process delta expression
11270
11271       Analyze_And_Resolve (Delta_Expr, Universal_Real);
11272
11273       Check_Delta_Expression (Delta_Expr);
11274       Delta_Val := Expr_Value_R (Delta_Expr);
11275
11276       --  Check delta is power of 10, and determine scale value from it
11277
11278       declare
11279          Val : Ureal;
11280
11281       begin
11282          Scale_Val := Uint_0;
11283          Val := Delta_Val;
11284
11285          if Val < Ureal_1 then
11286             while Val < Ureal_1 loop
11287                Val := Val * Ureal_10;
11288                Scale_Val := Scale_Val + 1;
11289             end loop;
11290
11291             if Scale_Val > 18 then
11292                Error_Msg_N ("scale exceeds maximum value of 18", Def);
11293                Scale_Val := UI_From_Int (+18);
11294             end if;
11295
11296          else
11297             while Val > Ureal_1 loop
11298                Val := Val / Ureal_10;
11299                Scale_Val := Scale_Val - 1;
11300             end loop;
11301
11302             if Scale_Val < -18 then
11303                Error_Msg_N ("scale is less than minimum value of -18", Def);
11304                Scale_Val := UI_From_Int (-18);
11305             end if;
11306          end if;
11307
11308          if Val /= Ureal_1 then
11309             Error_Msg_N ("delta expression must be a power of 10", Def);
11310             Delta_Val := Ureal_10 ** (-Scale_Val);
11311          end if;
11312       end;
11313
11314       --  Set delta, scale and small (small = delta for decimal type)
11315
11316       Set_Delta_Value (Implicit_Base, Delta_Val);
11317       Set_Scale_Value (Implicit_Base, Scale_Val);
11318       Set_Small_Value (Implicit_Base, Delta_Val);
11319
11320       --  Analyze and process digits expression
11321
11322       Analyze_And_Resolve (Digs_Expr, Any_Integer);
11323       Check_Digits_Expression (Digs_Expr);
11324       Digs_Val := Expr_Value (Digs_Expr);
11325
11326       if Digs_Val > 18 then
11327          Digs_Val := UI_From_Int (+18);
11328          Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
11329       end if;
11330
11331       Set_Digits_Value (Implicit_Base, Digs_Val);
11332       Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
11333
11334       --  Set range of base type from digits value for now. This will be
11335       --  expanded to represent the true underlying base range by Freeze.
11336
11337       Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
11338
11339       --  Note: We leave size as zero for now, size will be set at freeze
11340       --  time. We have to do this for ordinary fixed-point, because the size
11341       --  depends on the specified small, and we might as well do the same for
11342       --  decimal fixed-point.
11343
11344       pragma Assert (Esize (Implicit_Base) = Uint_0);
11345
11346       --  If there are bounds given in the declaration use them as the
11347       --  bounds of the first named subtype.
11348
11349       if Present (Real_Range_Specification (Def)) then
11350          declare
11351             RRS      : constant Node_Id := Real_Range_Specification (Def);
11352             Low      : constant Node_Id := Low_Bound (RRS);
11353             High     : constant Node_Id := High_Bound (RRS);
11354             Low_Val  : Ureal;
11355             High_Val : Ureal;
11356
11357          begin
11358             Analyze_And_Resolve (Low, Any_Real);
11359             Analyze_And_Resolve (High, Any_Real);
11360             Check_Real_Bound (Low);
11361             Check_Real_Bound (High);
11362             Low_Val := Expr_Value_R (Low);
11363             High_Val := Expr_Value_R (High);
11364
11365             if Low_Val < (-Bound_Val) then
11366                Error_Msg_N
11367                  ("range low bound too small for digits value", Low);
11368                Low_Val := -Bound_Val;
11369             end if;
11370
11371             if High_Val > Bound_Val then
11372                Error_Msg_N
11373                  ("range high bound too large for digits value", High);
11374                High_Val := Bound_Val;
11375             end if;
11376
11377             Set_Fixed_Range (T, Loc, Low_Val, High_Val);
11378          end;
11379
11380       --  If no explicit range, use range that corresponds to given
11381       --  digits value. This will end up as the final range for the
11382       --  first subtype.
11383
11384       else
11385          Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
11386       end if;
11387
11388       --  Complete entity for first subtype
11389
11390       Set_Ekind          (T, E_Decimal_Fixed_Point_Subtype);
11391       Set_Etype          (T, Implicit_Base);
11392       Set_Size_Info      (T, Implicit_Base);
11393       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
11394       Set_Digits_Value   (T, Digs_Val);
11395       Set_Delta_Value    (T, Delta_Val);
11396       Set_Small_Value    (T, Delta_Val);
11397       Set_Scale_Value    (T, Scale_Val);
11398       Set_Is_Constrained (T);
11399    end Decimal_Fixed_Point_Type_Declaration;
11400
11401    -----------------------------------
11402    -- Derive_Progenitor_Subprograms --
11403    -----------------------------------
11404
11405    procedure Derive_Progenitor_Subprograms
11406      (Parent_Type : Entity_Id;
11407       Tagged_Type : Entity_Id)
11408    is
11409       E          : Entity_Id;
11410       Elmt       : Elmt_Id;
11411       Iface      : Entity_Id;
11412       Iface_Elmt : Elmt_Id;
11413       Iface_Subp : Entity_Id;
11414       New_Subp   : Entity_Id := Empty;
11415       Prim_Elmt  : Elmt_Id;
11416       Subp       : Entity_Id;
11417       Typ        : Entity_Id;
11418
11419    begin
11420       pragma Assert (Ada_Version >= Ada_05
11421         and then Is_Record_Type (Tagged_Type)
11422         and then Is_Tagged_Type (Tagged_Type)
11423         and then Has_Interfaces (Tagged_Type));
11424
11425       --  Step 1: Transfer to the full-view primitives associated with the
11426       --  partial-view that cover interface primitives. Conceptually this
11427       --  work should be done later by Process_Full_View; done here to
11428       --  simplify its implementation at later stages. It can be safely
11429       --  done here because interfaces must be visible in the partial and
11430       --  private view (RM 7.3(7.3/2)).
11431
11432       --  Small optimization: This work is only required if the parent is
11433       --  abstract. If the tagged type is not abstract, it cannot have
11434       --  abstract primitives (the only entities in the list of primitives of
11435       --  non-abstract tagged types that can reference abstract primitives
11436       --  through its Alias attribute are the internal entities that have
11437       --  attribute Interface_Alias, and these entities are generated later
11438       --  by Freeze_Record_Type).
11439
11440       if In_Private_Part (Current_Scope)
11441         and then Is_Abstract_Type (Parent_Type)
11442       then
11443          Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
11444          while Present (Elmt) loop
11445             Subp := Node (Elmt);
11446
11447             --  At this stage it is not possible to have entities in the list
11448             --  of primitives that have attribute Interface_Alias
11449
11450             pragma Assert (No (Interface_Alias (Subp)));
11451
11452             Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
11453
11454             if Is_Interface (Typ) then
11455                E := Find_Primitive_Covering_Interface
11456                       (Tagged_Type => Tagged_Type,
11457                        Iface_Prim  => Subp);
11458
11459                if Present (E)
11460                  and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
11461                then
11462                   Replace_Elmt (Elmt, E);
11463                   Remove_Homonym (Subp);
11464                end if;
11465             end if;
11466
11467             Next_Elmt (Elmt);
11468          end loop;
11469       end if;
11470
11471       --  Step 2: Add primitives of progenitors that are not implemented by
11472       --  parents of Tagged_Type
11473
11474       if Present (Interfaces (Tagged_Type)) then
11475          Iface_Elmt := First_Elmt (Interfaces (Tagged_Type));
11476          while Present (Iface_Elmt) loop
11477             Iface := Node (Iface_Elmt);
11478
11479             Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
11480             while Present (Prim_Elmt) loop
11481                Iface_Subp := Node (Prim_Elmt);
11482
11483                --  Exclude derivation of predefined primitives except those
11484                --  that come from source. Required to catch declarations of
11485                --  equality operators of interfaces. For example:
11486
11487                --     type Iface is interface;
11488                --     function "=" (Left, Right : Iface) return Boolean;
11489
11490                if not Is_Predefined_Dispatching_Operation (Iface_Subp)
11491                  or else Comes_From_Source (Iface_Subp)
11492                then
11493                   E := Find_Primitive_Covering_Interface
11494                          (Tagged_Type => Tagged_Type,
11495                           Iface_Prim  => Iface_Subp);
11496
11497                   --  If not found we derive a new primitive leaving its alias
11498                   --  attribute referencing the interface primitive
11499
11500                   if No (E) then
11501                      Derive_Subprogram
11502                        (New_Subp, Iface_Subp, Tagged_Type, Iface);
11503
11504                   --  Propagate to the full view interface entities associated
11505                   --  with the partial view
11506
11507                   elsif In_Private_Part (Current_Scope)
11508                     and then Present (Alias (E))
11509                     and then Alias (E) = Iface_Subp
11510                     and then
11511                       List_Containing (Parent (E)) /=
11512                         Private_Declarations
11513                           (Specification
11514                             (Unit_Declaration_Node (Current_Scope)))
11515                   then
11516                      Append_Elmt (E, Primitive_Operations (Tagged_Type));
11517                   end if;
11518                end if;
11519
11520                Next_Elmt (Prim_Elmt);
11521             end loop;
11522
11523             Next_Elmt (Iface_Elmt);
11524          end loop;
11525       end if;
11526    end Derive_Progenitor_Subprograms;
11527
11528    -----------------------
11529    -- Derive_Subprogram --
11530    -----------------------
11531
11532    procedure Derive_Subprogram
11533      (New_Subp     : in out Entity_Id;
11534       Parent_Subp  : Entity_Id;
11535       Derived_Type : Entity_Id;
11536       Parent_Type  : Entity_Id;
11537       Actual_Subp  : Entity_Id := Empty)
11538    is
11539       Formal : Entity_Id;
11540       --  Formal parameter of parent primitive operation
11541
11542       Formal_Of_Actual : Entity_Id;
11543       --  Formal parameter of actual operation, when the derivation is to
11544       --  create a renaming for a primitive operation of an actual in an
11545       --  instantiation.
11546
11547       New_Formal : Entity_Id;
11548       --  Formal of inherited operation
11549
11550       Visible_Subp : Entity_Id := Parent_Subp;
11551
11552       function Is_Private_Overriding return Boolean;
11553       --  If Subp is a private overriding of a visible operation, the inherited
11554       --  operation derives from the overridden op (even though its body is the
11555       --  overriding one) and the inherited operation is visible now. See
11556       --  sem_disp to see the full details of the handling of the overridden
11557       --  subprogram, which is removed from the list of primitive operations of
11558       --  the type. The overridden subprogram is saved locally in Visible_Subp,
11559       --  and used to diagnose abstract operations that need overriding in the
11560       --  derived type.
11561
11562       procedure Replace_Type (Id, New_Id : Entity_Id);
11563       --  When the type is an anonymous access type, create a new access type
11564       --  designating the derived type.
11565
11566       procedure Set_Derived_Name;
11567       --  This procedure sets the appropriate Chars name for New_Subp. This
11568       --  is normally just a copy of the parent name. An exception arises for
11569       --  type support subprograms, where the name is changed to reflect the
11570       --  name of the derived type, e.g. if type foo is derived from type bar,
11571       --  then a procedure barDA is derived with a name fooDA.
11572
11573       ---------------------------
11574       -- Is_Private_Overriding --
11575       ---------------------------
11576
11577       function Is_Private_Overriding return Boolean is
11578          Prev : Entity_Id;
11579
11580       begin
11581          --  If the parent is not a dispatching operation there is no
11582          --  need to investigate overridings
11583
11584          if not Is_Dispatching_Operation (Parent_Subp) then
11585             return False;
11586          end if;
11587
11588          --  The visible operation that is overridden is a homonym of the
11589          --  parent subprogram. We scan the homonym chain to find the one
11590          --  whose alias is the subprogram we are deriving.
11591
11592          Prev := Current_Entity (Parent_Subp);
11593          while Present (Prev) loop
11594             if Ekind (Prev) = Ekind (Parent_Subp)
11595               and then Alias (Prev) = Parent_Subp
11596               and then Scope (Parent_Subp) = Scope (Prev)
11597               and then not Is_Hidden (Prev)
11598             then
11599                Visible_Subp := Prev;
11600                return True;
11601             end if;
11602
11603             Prev := Homonym (Prev);
11604          end loop;
11605
11606          return False;
11607       end Is_Private_Overriding;
11608
11609       ------------------
11610       -- Replace_Type --
11611       ------------------
11612
11613       procedure Replace_Type (Id, New_Id : Entity_Id) is
11614          Acc_Type : Entity_Id;
11615          Par      : constant Node_Id := Parent (Derived_Type);
11616
11617       begin
11618          --  When the type is an anonymous access type, create a new access
11619          --  type designating the derived type. This itype must be elaborated
11620          --  at the point of the derivation, not on subsequent calls that may
11621          --  be out of the proper scope for Gigi, so we insert a reference to
11622          --  it after the derivation.
11623
11624          if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
11625             declare
11626                Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
11627
11628             begin
11629                if Ekind (Desig_Typ) = E_Record_Type_With_Private
11630                  and then Present (Full_View (Desig_Typ))
11631                  and then not Is_Private_Type (Parent_Type)
11632                then
11633                   Desig_Typ := Full_View (Desig_Typ);
11634                end if;
11635
11636                if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
11637
11638                   --  Ada 2005 (AI-251): Handle also derivations of abstract
11639                   --  interface primitives.
11640
11641                  or else (Is_Interface (Desig_Typ)
11642                           and then not Is_Class_Wide_Type (Desig_Typ))
11643                then
11644                   Acc_Type := New_Copy (Etype (Id));
11645                   Set_Etype (Acc_Type, Acc_Type);
11646                   Set_Scope (Acc_Type, New_Subp);
11647
11648                   --  Compute size of anonymous access type
11649
11650                   if Is_Array_Type (Desig_Typ)
11651                     and then not Is_Constrained (Desig_Typ)
11652                   then
11653                      Init_Size (Acc_Type, 2 * System_Address_Size);
11654                   else
11655                      Init_Size (Acc_Type, System_Address_Size);
11656                   end if;
11657
11658                   Init_Alignment (Acc_Type);
11659                   Set_Directly_Designated_Type (Acc_Type, Derived_Type);
11660
11661                   Set_Etype (New_Id, Acc_Type);
11662                   Set_Scope (New_Id, New_Subp);
11663
11664                   --  Create a reference to it
11665                   Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
11666
11667                else
11668                   Set_Etype (New_Id, Etype (Id));
11669                end if;
11670             end;
11671
11672          elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
11673            or else
11674              (Ekind (Etype (Id)) = E_Record_Type_With_Private
11675                and then Present (Full_View (Etype (Id)))
11676                and then
11677                  Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
11678          then
11679             --  Constraint checks on formals are generated during expansion,
11680             --  based on the signature of the original subprogram. The bounds
11681             --  of the derived type are not relevant, and thus we can use
11682             --  the base type for the formals. However, the return type may be
11683             --  used in a context that requires that the proper static bounds
11684             --  be used (a case statement, for example)  and for those cases
11685             --  we must use the derived type (first subtype), not its base.
11686
11687             --  If the derived_type_definition has no constraints, we know that
11688             --  the derived type has the same constraints as the first subtype
11689             --  of the parent, and we can also use it rather than its base,
11690             --  which can lead to more efficient code.
11691
11692             if Etype (Id) = Parent_Type then
11693                if Is_Scalar_Type (Parent_Type)
11694                  and then
11695                    Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
11696                then
11697                   Set_Etype (New_Id, Derived_Type);
11698
11699                elsif Nkind (Par) = N_Full_Type_Declaration
11700                  and then
11701                    Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
11702                  and then
11703                    Is_Entity_Name
11704                      (Subtype_Indication (Type_Definition (Par)))
11705                then
11706                   Set_Etype (New_Id, Derived_Type);
11707
11708                else
11709                   Set_Etype (New_Id, Base_Type (Derived_Type));
11710                end if;
11711
11712             else
11713                Set_Etype (New_Id, Base_Type (Derived_Type));
11714             end if;
11715
11716          --  Ada 2005 (AI-251): Handle derivations of abstract interface
11717          --  primitives.
11718
11719          elsif Is_Interface (Etype (Id))
11720            and then not Is_Class_Wide_Type (Etype (Id))
11721            and then Is_Progenitor (Etype (Id), Derived_Type)
11722          then
11723             Set_Etype (New_Id, Derived_Type);
11724
11725          else
11726             Set_Etype (New_Id, Etype (Id));
11727          end if;
11728       end Replace_Type;
11729
11730       ----------------------
11731       -- Set_Derived_Name --
11732       ----------------------
11733
11734       procedure Set_Derived_Name is
11735          Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
11736       begin
11737          if Nm = TSS_Null then
11738             Set_Chars (New_Subp, Chars (Parent_Subp));
11739          else
11740             Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
11741          end if;
11742       end Set_Derived_Name;
11743
11744       --  Local variables
11745
11746       Parent_Overrides_Interface_Primitive : Boolean := False;
11747
11748    --  Start of processing for Derive_Subprogram
11749
11750    begin
11751       New_Subp :=
11752          New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
11753       Set_Ekind (New_Subp, Ekind (Parent_Subp));
11754
11755       --  Check whether the parent overrides an interface primitive
11756
11757       if Is_Overriding_Operation (Parent_Subp) then
11758          declare
11759             E : Entity_Id := Parent_Subp;
11760          begin
11761             while Present (Overridden_Operation (E)) loop
11762                E := Ultimate_Alias (Overridden_Operation (E));
11763             end loop;
11764
11765             Parent_Overrides_Interface_Primitive :=
11766               Is_Dispatching_Operation (E)
11767                 and then Present (Find_Dispatching_Type (E))
11768                 and then Is_Interface (Find_Dispatching_Type (E));
11769          end;
11770       end if;
11771
11772       --  Check whether the inherited subprogram is a private operation that
11773       --  should be inherited but not yet made visible. Such subprograms can
11774       --  become visible at a later point (e.g., the private part of a public
11775       --  child unit) via Declare_Inherited_Private_Subprograms. If the
11776       --  following predicate is true, then this is not such a private
11777       --  operation and the subprogram simply inherits the name of the parent
11778       --  subprogram. Note the special check for the names of controlled
11779       --  operations, which are currently exempted from being inherited with
11780       --  a hidden name because they must be findable for generation of
11781       --  implicit run-time calls.
11782
11783       if not Is_Hidden (Parent_Subp)
11784         or else Is_Internal (Parent_Subp)
11785         or else Is_Private_Overriding
11786         or else Is_Internal_Name (Chars (Parent_Subp))
11787         or else Chars (Parent_Subp) = Name_Initialize
11788         or else Chars (Parent_Subp) = Name_Adjust
11789         or else Chars (Parent_Subp) = Name_Finalize
11790       then
11791          Set_Derived_Name;
11792
11793       --  If parent is hidden, this can be a regular derivation if the
11794       --  parent is immediately visible in a non-instantiating context,
11795       --  or if we are in the private part of an instance. This test
11796       --  should still be refined ???
11797
11798       --  The test for In_Instance_Not_Visible avoids inheriting the derived
11799       --  operation as a non-visible operation in cases where the parent
11800       --  subprogram might not be visible now, but was visible within the
11801       --  original generic, so it would be wrong to make the inherited
11802       --  subprogram non-visible now. (Not clear if this test is fully
11803       --  correct; are there any cases where we should declare the inherited
11804       --  operation as not visible to avoid it being overridden, e.g., when
11805       --  the parent type is a generic actual with private primitives ???)
11806
11807       --  (they should be treated the same as other private inherited
11808       --  subprograms, but it's not clear how to do this cleanly). ???
11809
11810       elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
11811               and then Is_Immediately_Visible (Parent_Subp)
11812               and then not In_Instance)
11813         or else In_Instance_Not_Visible
11814       then
11815          Set_Derived_Name;
11816
11817       --  Ada 2005 (AI-251): Regular derivation if the parent subprogram
11818       --  overrides an interface primitive because interface primitives
11819       --  must be visible in the partial view of the parent (RM 7.3 (7.3/2))
11820
11821       elsif Parent_Overrides_Interface_Primitive then
11822          Set_Derived_Name;
11823
11824       --  The type is inheriting a private operation, so enter
11825       --  it with a special name so it can't be overridden.
11826
11827       else
11828          Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
11829       end if;
11830
11831       Set_Parent (New_Subp, Parent (Derived_Type));
11832
11833       if Present (Actual_Subp) then
11834          Replace_Type (Actual_Subp, New_Subp);
11835       else
11836          Replace_Type (Parent_Subp, New_Subp);
11837       end if;
11838
11839       Conditional_Delay (New_Subp, Parent_Subp);
11840
11841       --  If we are creating a renaming for a primitive operation of an
11842       --  actual of a generic derived type, we must examine the signature
11843       --  of the actual primitive, not that of the generic formal, which for
11844       --  example may be an interface. However the name and initial value
11845       --  of the inherited operation are those of the formal primitive.
11846
11847       Formal := First_Formal (Parent_Subp);
11848
11849       if Present (Actual_Subp) then
11850          Formal_Of_Actual := First_Formal (Actual_Subp);
11851       else
11852          Formal_Of_Actual := Empty;
11853       end if;
11854
11855       while Present (Formal) loop
11856          New_Formal := New_Copy (Formal);
11857
11858          --  Normally we do not go copying parents, but in the case of
11859          --  formals, we need to link up to the declaration (which is the
11860          --  parameter specification), and it is fine to link up to the
11861          --  original formal's parameter specification in this case.
11862
11863          Set_Parent (New_Formal, Parent (Formal));
11864          Append_Entity (New_Formal, New_Subp);
11865
11866          if Present (Formal_Of_Actual) then
11867             Replace_Type (Formal_Of_Actual, New_Formal);
11868             Next_Formal (Formal_Of_Actual);
11869          else
11870             Replace_Type (Formal, New_Formal);
11871          end if;
11872
11873          Next_Formal (Formal);
11874       end loop;
11875
11876       --  If this derivation corresponds to a tagged generic actual, then
11877       --  primitive operations rename those of the actual. Otherwise the
11878       --  primitive operations rename those of the parent type, If the parent
11879       --  renames an intrinsic operator, so does the new subprogram. We except
11880       --  concatenation, which is always properly typed, and does not get
11881       --  expanded as other intrinsic operations.
11882
11883       if No (Actual_Subp) then
11884          if Is_Intrinsic_Subprogram (Parent_Subp) then
11885             Set_Is_Intrinsic_Subprogram (New_Subp);
11886
11887             if Present (Alias (Parent_Subp))
11888               and then Chars (Parent_Subp) /= Name_Op_Concat
11889             then
11890                Set_Alias (New_Subp, Alias (Parent_Subp));
11891             else
11892                Set_Alias (New_Subp, Parent_Subp);
11893             end if;
11894
11895          else
11896             Set_Alias (New_Subp, Parent_Subp);
11897          end if;
11898
11899       else
11900          Set_Alias (New_Subp, Actual_Subp);
11901       end if;
11902
11903       --  Derived subprograms of a tagged type must inherit the convention
11904       --  of the parent subprogram (a requirement of AI-117). Derived
11905       --  subprograms of untagged types simply get convention Ada by default.
11906
11907       if Is_Tagged_Type (Derived_Type) then
11908          Set_Convention (New_Subp, Convention (Parent_Subp));
11909       end if;
11910
11911       Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
11912       Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
11913
11914       if Ekind (Parent_Subp) = E_Procedure then
11915          Set_Is_Valued_Procedure
11916            (New_Subp, Is_Valued_Procedure (Parent_Subp));
11917       end if;
11918
11919       --  No_Return must be inherited properly. If this is overridden in the
11920       --  case of a dispatching operation, then a check is made in Sem_Disp
11921       --  that the overriding operation is also No_Return (no such check is
11922       --  required for the case of non-dispatching operation.
11923
11924       Set_No_Return (New_Subp, No_Return (Parent_Subp));
11925
11926       --  A derived function with a controlling result is abstract. If the
11927       --  Derived_Type is a nonabstract formal generic derived type, then
11928       --  inherited operations are not abstract: the required check is done at
11929       --  instantiation time. If the derivation is for a generic actual, the
11930       --  function is not abstract unless the actual is.
11931
11932       if Is_Generic_Type (Derived_Type)
11933         and then not Is_Abstract_Type (Derived_Type)
11934       then
11935          null;
11936
11937       --  Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
11938       --  properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
11939
11940       elsif Ada_Version >= Ada_05
11941         and then (Is_Abstract_Subprogram (Alias (New_Subp))
11942                    or else (Is_Tagged_Type (Derived_Type)
11943                             and then Etype (New_Subp) = Derived_Type
11944                             and then not Is_Null_Extension (Derived_Type))
11945                    or else (Is_Tagged_Type (Derived_Type)
11946                             and then Ekind (Etype (New_Subp)) =
11947                                                        E_Anonymous_Access_Type
11948                             and then Designated_Type (Etype (New_Subp)) =
11949                                                        Derived_Type
11950                             and then not Is_Null_Extension (Derived_Type)))
11951         and then No (Actual_Subp)
11952       then
11953          if not Is_Tagged_Type (Derived_Type)
11954            or else Is_Abstract_Type (Derived_Type)
11955            or else Is_Abstract_Subprogram (Alias (New_Subp))
11956          then
11957             Set_Is_Abstract_Subprogram (New_Subp);
11958          else
11959             Set_Requires_Overriding (New_Subp);
11960          end if;
11961
11962       elsif Ada_Version < Ada_05
11963         and then (Is_Abstract_Subprogram (Alias (New_Subp))
11964                    or else (Is_Tagged_Type (Derived_Type)
11965                              and then Etype (New_Subp) = Derived_Type
11966                              and then No (Actual_Subp)))
11967       then
11968          Set_Is_Abstract_Subprogram (New_Subp);
11969
11970       --  Finally, if the parent type is abstract we must verify that all
11971       --  inherited operations are either non-abstract or overridden, or that
11972       --  the derived type itself is abstract (this check is performed at the
11973       --  end of a package declaration, in Check_Abstract_Overriding). A
11974       --  private overriding in the parent type will not be visible in the
11975       --  derivation if we are not in an inner package or in a child unit of
11976       --  the parent type, in which case the abstractness of the inherited
11977       --  operation is carried to the new subprogram.
11978
11979       elsif Is_Abstract_Type (Parent_Type)
11980         and then not In_Open_Scopes (Scope (Parent_Type))
11981         and then Is_Private_Overriding
11982         and then Is_Abstract_Subprogram (Visible_Subp)
11983       then
11984          if No (Actual_Subp) then
11985             Set_Alias (New_Subp, Visible_Subp);
11986             Set_Is_Abstract_Subprogram
11987               (New_Subp, True);
11988          else
11989             --  If this is a derivation for an instance of a formal derived
11990             --  type, abstractness comes from the primitive operation of the
11991             --  actual, not from the operation inherited from the ancestor.
11992
11993             Set_Is_Abstract_Subprogram
11994               (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
11995          end if;
11996       end if;
11997
11998       New_Overloaded_Entity (New_Subp, Derived_Type);
11999
12000       --  Check for case of a derived subprogram for the instantiation of a
12001       --  formal derived tagged type, if so mark the subprogram as dispatching
12002       --  and inherit the dispatching attributes of the parent subprogram. The
12003       --  derived subprogram is effectively renaming of the actual subprogram,
12004       --  so it needs to have the same attributes as the actual.
12005
12006       if Present (Actual_Subp)
12007         and then Is_Dispatching_Operation (Parent_Subp)
12008       then
12009          Set_Is_Dispatching_Operation (New_Subp);
12010
12011          if Present (DTC_Entity (Parent_Subp)) then
12012             Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
12013             Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
12014          end if;
12015       end if;
12016
12017       --  Indicate that a derived subprogram does not require a body and that
12018       --  it does not require processing of default expressions.
12019
12020       Set_Has_Completion (New_Subp);
12021       Set_Default_Expressions_Processed (New_Subp);
12022
12023       if Ekind (New_Subp) = E_Function then
12024          Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
12025       end if;
12026    end Derive_Subprogram;
12027
12028    ------------------------
12029    -- Derive_Subprograms --
12030    ------------------------
12031
12032    procedure Derive_Subprograms
12033      (Parent_Type    : Entity_Id;
12034       Derived_Type   : Entity_Id;
12035       Generic_Actual : Entity_Id := Empty)
12036    is
12037       Op_List : constant Elist_Id :=
12038                   Collect_Primitive_Operations (Parent_Type);
12039
12040       function Check_Derived_Type return Boolean;
12041       --  Check that all primitive inherited from Parent_Type are found in
12042       --  the list of primitives of Derived_Type exactly in the same order.
12043
12044       function Check_Derived_Type return Boolean is
12045          E        : Entity_Id;
12046          Elmt     : Elmt_Id;
12047          List     : Elist_Id;
12048          New_Subp : Entity_Id;
12049          Op_Elmt  : Elmt_Id;
12050          Subp     : Entity_Id;
12051
12052       begin
12053          --  Traverse list of entities in the current scope searching for
12054          --  an incomplete type whose full-view is derived type
12055
12056          E := First_Entity (Scope (Derived_Type));
12057          while Present (E)
12058            and then E /= Derived_Type
12059          loop
12060             if Ekind (E) = E_Incomplete_Type
12061               and then Present (Full_View (E))
12062               and then Full_View (E) = Derived_Type
12063             then
12064                --  Disable this test if Derived_Type completes an incomplete
12065                --  type because in such case more primitives can be added
12066                --  later to the list of primitives of Derived_Type by routine
12067                --  Process_Incomplete_Dependents
12068
12069                return True;
12070             end if;
12071
12072             E := Next_Entity (E);
12073          end loop;
12074
12075          List := Collect_Primitive_Operations (Derived_Type);
12076          Elmt := First_Elmt (List);
12077
12078          Op_Elmt := First_Elmt (Op_List);
12079          while Present (Op_Elmt) loop
12080             Subp     := Node (Op_Elmt);
12081             New_Subp := Node (Elmt);
12082
12083             --  At this early stage Derived_Type has no entities with attribute
12084             --  Interface_Alias. In addition, such primitives are always
12085             --  located at the end of the list of primitives of Parent_Type.
12086             --  Therefore, if found we can safely stop processing pending
12087             --  entities.
12088
12089             exit when Present (Interface_Alias (Subp));
12090
12091             --  Handle hidden entities
12092
12093             if not Is_Predefined_Dispatching_Operation (Subp)
12094               and then Is_Hidden (Subp)
12095             then
12096                if Present (New_Subp)
12097                  and then Primitive_Names_Match (Subp, New_Subp)
12098                then
12099                   Next_Elmt (Elmt);
12100                end if;
12101
12102             else
12103                if not Present (New_Subp)
12104                  or else Ekind (Subp) /= Ekind (New_Subp)
12105                  or else not Primitive_Names_Match (Subp, New_Subp)
12106                then
12107                   return False;
12108                end if;
12109
12110                Next_Elmt (Elmt);
12111             end if;
12112
12113             Next_Elmt (Op_Elmt);
12114          end loop;
12115
12116          return True;
12117       end Check_Derived_Type;
12118
12119       --  Local variables
12120
12121       Alias_Subp   : Entity_Id;
12122       Act_List     : Elist_Id;
12123       Act_Elmt     : Elmt_Id   := No_Elmt;
12124       Act_Subp     : Entity_Id := Empty;
12125       Elmt         : Elmt_Id;
12126       Need_Search  : Boolean   := False;
12127       New_Subp     : Entity_Id := Empty;
12128       Parent_Base  : Entity_Id;
12129       Subp         : Entity_Id;
12130
12131    --  Start of processing for Derive_Subprograms
12132
12133    begin
12134       if Ekind (Parent_Type) = E_Record_Type_With_Private
12135         and then Has_Discriminants (Parent_Type)
12136         and then Present (Full_View (Parent_Type))
12137       then
12138          Parent_Base := Full_View (Parent_Type);
12139       else
12140          Parent_Base := Parent_Type;
12141       end if;
12142
12143       if Present (Generic_Actual) then
12144          Act_List := Collect_Primitive_Operations (Generic_Actual);
12145          Act_Elmt := First_Elmt (Act_List);
12146       end if;
12147
12148       --  Derive primitives inherited from the parent. Note that if the generic
12149       --  actual is present, this is not really a type derivation, it is a
12150       --  completion within an instance.
12151
12152       --  Case 1: Derived_Type does not implement interfaces
12153
12154       if not Is_Tagged_Type (Derived_Type)
12155         or else (not Has_Interfaces (Derived_Type)
12156                   and then not (Present (Generic_Actual)
12157                                   and then
12158                                 Has_Interfaces (Generic_Actual)))
12159       then
12160          Elmt := First_Elmt (Op_List);
12161          while Present (Elmt) loop
12162             Subp := Node (Elmt);
12163
12164             --  Literals are derived earlier in the process of building the
12165             --  derived type, and are skipped here.
12166
12167             if Ekind (Subp) = E_Enumeration_Literal then
12168                null;
12169
12170             --  The actual is a direct descendant and the common primitive
12171             --  operations appear in the same order.
12172
12173             --  If the generic parent type is present, the derived type is an
12174             --  instance of a formal derived type, and within the instance its
12175             --  operations are those of the actual. We derive from the formal
12176             --  type but make the inherited operations aliases of the
12177             --  corresponding operations of the actual.
12178
12179             else
12180                Derive_Subprogram
12181                  (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
12182
12183                if Present (Act_Elmt) then
12184                   Next_Elmt (Act_Elmt);
12185                end if;
12186             end if;
12187
12188             Next_Elmt (Elmt);
12189          end loop;
12190
12191       --  Case 2: Derived_Type implements interfaces
12192
12193       else
12194          --  If the parent type has no predefined primitives we remove
12195          --  predefined primitives from the list of primitives of generic
12196          --  actual to simplify the complexity of this algorithm.
12197
12198          if Present (Generic_Actual) then
12199             declare
12200                Has_Predefined_Primitives : Boolean := False;
12201
12202             begin
12203                --  Check if the parent type has predefined primitives
12204
12205                Elmt := First_Elmt (Op_List);
12206                while Present (Elmt) loop
12207                   Subp := Node (Elmt);
12208
12209                   if Is_Predefined_Dispatching_Operation (Subp)
12210                     and then not Comes_From_Source (Ultimate_Alias (Subp))
12211                   then
12212                      Has_Predefined_Primitives := True;
12213                      exit;
12214                   end if;
12215
12216                   Next_Elmt (Elmt);
12217                end loop;
12218
12219                --  Remove predefined primitives of Generic_Actual. We must use
12220                --  an auxiliary list because in case of tagged types the value
12221                --  returned by Collect_Primitive_Operations is the value stored
12222                --  in its Primitive_Operations attribute (and we don't want to
12223                --  modify its current contents).
12224
12225                if not Has_Predefined_Primitives then
12226                   declare
12227                      Aux_List : constant Elist_Id := New_Elmt_List;
12228
12229                   begin
12230                      Elmt := First_Elmt (Act_List);
12231                      while Present (Elmt) loop
12232                         Subp := Node (Elmt);
12233
12234                         if not Is_Predefined_Dispatching_Operation (Subp)
12235                           or else Comes_From_Source (Subp)
12236                         then
12237                            Append_Elmt (Subp, Aux_List);
12238                         end if;
12239
12240                         Next_Elmt (Elmt);
12241                      end loop;
12242
12243                      Act_List := Aux_List;
12244                   end;
12245                end if;
12246
12247                Act_Elmt := First_Elmt (Act_List);
12248                Act_Subp := Node (Act_Elmt);
12249             end;
12250          end if;
12251
12252          --  Stage 1: If the generic actual is not present we derive the
12253          --  primitives inherited from the parent type. If the generic parent
12254          --  type is present, the derived type is an instance of a formal
12255          --  derived type, and within the instance its operations are those of
12256          --  the actual. We derive from the formal type but make the inherited
12257          --  operations aliases of the corresponding operations of the actual.
12258
12259          Elmt := First_Elmt (Op_List);
12260          while Present (Elmt) loop
12261             Subp       := Node (Elmt);
12262             Alias_Subp := Ultimate_Alias (Subp);
12263
12264             --  At this early stage Derived_Type has no entities with attribute
12265             --  Interface_Alias. In addition, such primitives are always
12266             --  located at the end of the list of primitives of Parent_Type.
12267             --  Therefore, if found we can safely stop processing pending
12268             --  entities.
12269
12270             exit when Present (Interface_Alias (Subp));
12271
12272             --  If the generic actual is present find the corresponding
12273             --  operation in the generic actual. If the parent type is a
12274             --  direct ancestor of the derived type then, even if it is an
12275             --  interface, the operations are inherited from the primary
12276             --  dispatch table and are in the proper order. If we detect here
12277             --  that primitives are not in the same order we traverse the list
12278             --  of primitive operations of the actual to find the one that
12279             --  implements the interface primitive.
12280
12281             if Need_Search
12282               or else
12283                 (Present (Generic_Actual)
12284                    and then Present (Act_Subp)
12285                    and then not Primitive_Names_Match (Subp, Act_Subp))
12286             then
12287                pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual));
12288                pragma Assert (Is_Interface (Parent_Base));
12289
12290                --  Remember that we need searching for all the pending
12291                --  primitives
12292
12293                Need_Search := True;
12294
12295                --  Handle entities associated with interface primitives
12296
12297                if Present (Alias (Subp))
12298                  and then Is_Interface (Find_Dispatching_Type (Alias (Subp)))
12299                  and then not Is_Predefined_Dispatching_Operation (Subp)
12300                then
12301                   Act_Subp :=
12302                     Find_Primitive_Covering_Interface
12303                       (Tagged_Type => Generic_Actual,
12304                        Iface_Prim  => Subp);
12305
12306                --  Handle predefined primitives plus the rest of user-defined
12307                --  primitives
12308
12309                else
12310                   Act_Elmt := First_Elmt (Act_List);
12311                   while Present (Act_Elmt) loop
12312                      Act_Subp := Node (Act_Elmt);
12313
12314                      exit when Primitive_Names_Match (Subp, Act_Subp)
12315                        and then Type_Conformant (Subp, Act_Subp,
12316                                   Skip_Controlling_Formals => True)
12317                        and then No (Interface_Alias (Act_Subp));
12318
12319                      Next_Elmt (Act_Elmt);
12320                   end loop;
12321                end if;
12322             end if;
12323
12324             --   Case 1: If the parent is a limited interface then it has the
12325             --   predefined primitives of synchronized interfaces. However, the
12326             --   actual type may be a non-limited type and hence it does not
12327             --   have such primitives.
12328
12329             if Present (Generic_Actual)
12330               and then not Present (Act_Subp)
12331               and then Is_Limited_Interface (Parent_Base)
12332               and then Is_Predefined_Interface_Primitive (Subp)
12333             then
12334                null;
12335
12336             --  Case 2: Inherit entities associated with interfaces that
12337             --  were not covered by the parent type. We exclude here null
12338             --  interface primitives because they do not need special
12339             --  management.
12340
12341             elsif Present (Alias (Subp))
12342               and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
12343               and then not
12344                 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
12345                    and then Null_Present (Parent (Alias_Subp)))
12346             then
12347                Derive_Subprogram
12348                  (New_Subp     => New_Subp,
12349                   Parent_Subp  => Alias_Subp,
12350                   Derived_Type => Derived_Type,
12351                   Parent_Type  => Find_Dispatching_Type (Alias_Subp),
12352                   Actual_Subp  => Act_Subp);
12353
12354                if No (Generic_Actual) then
12355                   Set_Alias (New_Subp, Subp);
12356                end if;
12357
12358             --  Case 3: Common derivation
12359
12360             else
12361                Derive_Subprogram
12362                  (New_Subp     => New_Subp,
12363                   Parent_Subp  => Subp,
12364                   Derived_Type => Derived_Type,
12365                   Parent_Type  => Parent_Base,
12366                   Actual_Subp  => Act_Subp);
12367             end if;
12368
12369             --  No need to update Act_Elm if we must search for the
12370             --  corresponding operation in the generic actual
12371
12372             if not Need_Search
12373               and then Present (Act_Elmt)
12374             then
12375                Next_Elmt (Act_Elmt);
12376                Act_Subp := Node (Act_Elmt);
12377             end if;
12378
12379             Next_Elmt (Elmt);
12380          end loop;
12381
12382          --  Inherit additional operations from progenitors. If the derived
12383          --  type is a generic actual, there are not new primitive operations
12384          --  for the type because it has those of the actual, and therefore
12385          --  nothing needs to be done. The renamings generated above are not
12386          --  primitive operations, and their purpose is simply to make the
12387          --  proper operations visible within an instantiation.
12388
12389          if No (Generic_Actual) then
12390             Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
12391          end if;
12392       end if;
12393
12394       --  Final check: Direct descendants must have their primitives in the
12395       --  same order. We exclude from this test non-tagged types and instances
12396       --  of formal derived types. We skip this test if we have already
12397       --  reported serious errors in the sources.
12398
12399       pragma Assert (not Is_Tagged_Type (Derived_Type)
12400         or else Present (Generic_Actual)
12401         or else Serious_Errors_Detected > 0
12402         or else Check_Derived_Type);
12403    end Derive_Subprograms;
12404
12405    --------------------------------
12406    -- Derived_Standard_Character --
12407    --------------------------------
12408
12409    procedure Derived_Standard_Character
12410      (N            : Node_Id;
12411       Parent_Type  : Entity_Id;
12412       Derived_Type : Entity_Id)
12413    is
12414       Loc           : constant Source_Ptr := Sloc (N);
12415       Def           : constant Node_Id    := Type_Definition (N);
12416       Indic         : constant Node_Id    := Subtype_Indication (Def);
12417       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
12418       Implicit_Base : constant Entity_Id  :=
12419                         Create_Itype
12420                           (E_Enumeration_Type, N, Derived_Type, 'B');
12421
12422       Lo : Node_Id;
12423       Hi : Node_Id;
12424
12425    begin
12426       Discard_Node (Process_Subtype (Indic, N));
12427
12428       Set_Etype     (Implicit_Base, Parent_Base);
12429       Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
12430       Set_RM_Size   (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
12431
12432       Set_Is_Character_Type  (Implicit_Base, True);
12433       Set_Has_Delayed_Freeze (Implicit_Base);
12434
12435       --  The bounds of the implicit base are the bounds of the parent base.
12436       --  Note that their type is the parent base.
12437
12438       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
12439       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
12440
12441       Set_Scalar_Range (Implicit_Base,
12442         Make_Range (Loc,
12443           Low_Bound  => Lo,
12444           High_Bound => Hi));
12445
12446       Conditional_Delay (Derived_Type, Parent_Type);
12447
12448       Set_Ekind (Derived_Type, E_Enumeration_Subtype);
12449       Set_Etype (Derived_Type, Implicit_Base);
12450       Set_Size_Info         (Derived_Type, Parent_Type);
12451
12452       if Unknown_RM_Size (Derived_Type) then
12453          Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
12454       end if;
12455
12456       Set_Is_Character_Type (Derived_Type, True);
12457
12458       if Nkind (Indic) /= N_Subtype_Indication then
12459
12460          --  If no explicit constraint, the bounds are those
12461          --  of the parent type.
12462
12463          Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Type));
12464          Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
12465          Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
12466       end if;
12467
12468       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
12469
12470       --  Because the implicit base is used in the conversion of the bounds, we
12471       --  have to freeze it now. This is similar to what is done for numeric
12472       --  types, and it equally suspicious, but otherwise a non-static bound
12473       --  will have a reference to an unfrozen type, which is rejected by Gigi
12474       --  (???). This requires specific care for definition of stream
12475       --  attributes. For details, see comments at the end of
12476       --  Build_Derived_Numeric_Type.
12477
12478       Freeze_Before (N, Implicit_Base);
12479    end Derived_Standard_Character;
12480
12481    ------------------------------
12482    -- Derived_Type_Declaration --
12483    ------------------------------
12484
12485    procedure Derived_Type_Declaration
12486      (T             : Entity_Id;
12487       N             : Node_Id;
12488       Is_Completion : Boolean)
12489    is
12490       Parent_Type  : Entity_Id;
12491
12492       function Comes_From_Generic (Typ : Entity_Id) return Boolean;
12493       --  Check whether the parent type is a generic formal, or derives
12494       --  directly or indirectly from one.
12495
12496       ------------------------
12497       -- Comes_From_Generic --
12498       ------------------------
12499
12500       function Comes_From_Generic (Typ : Entity_Id) return Boolean is
12501       begin
12502          if Is_Generic_Type (Typ) then
12503             return True;
12504
12505          elsif Is_Generic_Type (Root_Type (Parent_Type)) then
12506             return True;
12507
12508          elsif Is_Private_Type (Typ)
12509            and then Present (Full_View (Typ))
12510            and then Is_Generic_Type (Root_Type (Full_View (Typ)))
12511          then
12512             return True;
12513
12514          elsif Is_Generic_Actual_Type (Typ) then
12515             return True;
12516
12517          else
12518             return False;
12519          end if;
12520       end Comes_From_Generic;
12521
12522       --  Local variables
12523
12524       Def          : constant Node_Id := Type_Definition (N);
12525       Iface_Def    : Node_Id;
12526       Indic        : constant Node_Id := Subtype_Indication (Def);
12527       Extension    : constant Node_Id := Record_Extension_Part (Def);
12528       Parent_Node  : Node_Id;
12529       Parent_Scope : Entity_Id;
12530       Taggd        : Boolean;
12531
12532    --  Start of processing for Derived_Type_Declaration
12533
12534    begin
12535       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
12536
12537       --  Ada 2005 (AI-251): In case of interface derivation check that the
12538       --  parent is also an interface.
12539
12540       if Interface_Present (Def) then
12541          if not Is_Interface (Parent_Type) then
12542             Diagnose_Interface (Indic, Parent_Type);
12543
12544          else
12545             Parent_Node := Parent (Base_Type (Parent_Type));
12546             Iface_Def   := Type_Definition (Parent_Node);
12547
12548             --  Ada 2005 (AI-251): Limited interfaces can only inherit from
12549             --  other limited interfaces.
12550
12551             if Limited_Present (Def) then
12552                if Limited_Present (Iface_Def) then
12553                   null;
12554
12555                elsif Protected_Present (Iface_Def) then
12556                   Error_Msg_N
12557                     ("(Ada 2005) limited interface cannot "
12558                      & "inherit from protected interface", Indic);
12559
12560                elsif Synchronized_Present (Iface_Def) then
12561                   Error_Msg_N
12562                     ("(Ada 2005) limited interface cannot "
12563                      & "inherit from synchronized interface", Indic);
12564
12565                elsif Task_Present (Iface_Def) then
12566                   Error_Msg_N
12567                     ("(Ada 2005) limited interface cannot "
12568                      & "inherit from task interface", Indic);
12569
12570                else
12571                   Error_Msg_N
12572                     ("(Ada 2005) limited interface cannot "
12573                      & "inherit from non-limited interface", Indic);
12574                end if;
12575
12576             --  Ada 2005 (AI-345): Non-limited interfaces can only inherit
12577             --  from non-limited or limited interfaces.
12578
12579             elsif not Protected_Present (Def)
12580               and then not Synchronized_Present (Def)
12581               and then not Task_Present (Def)
12582             then
12583                if Limited_Present (Iface_Def) then
12584                   null;
12585
12586                elsif Protected_Present (Iface_Def) then
12587                   Error_Msg_N
12588                     ("(Ada 2005) non-limited interface cannot "
12589                      & "inherit from protected interface", Indic);
12590
12591                elsif Synchronized_Present (Iface_Def) then
12592                   Error_Msg_N
12593                     ("(Ada 2005) non-limited interface cannot "
12594                      & "inherit from synchronized interface", Indic);
12595
12596                elsif Task_Present (Iface_Def) then
12597                   Error_Msg_N
12598                     ("(Ada 2005) non-limited interface cannot "
12599                      & "inherit from task interface", Indic);
12600
12601                else
12602                   null;
12603                end if;
12604             end if;
12605          end if;
12606       end if;
12607
12608       if Is_Tagged_Type (Parent_Type)
12609         and then Is_Concurrent_Type (Parent_Type)
12610         and then not Is_Interface (Parent_Type)
12611       then
12612          Error_Msg_N
12613            ("parent type of a record extension cannot be "
12614             & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
12615          Set_Etype (T, Any_Type);
12616          return;
12617       end if;
12618
12619       --  Ada 2005 (AI-251): Decorate all the names in the list of ancestor
12620       --  interfaces
12621
12622       if Is_Tagged_Type (Parent_Type)
12623         and then Is_Non_Empty_List (Interface_List (Def))
12624       then
12625          declare
12626             Intf : Node_Id;
12627             T    : Entity_Id;
12628
12629          begin
12630             Intf := First (Interface_List (Def));
12631             while Present (Intf) loop
12632                T := Find_Type_Of_Subtype_Indic (Intf);
12633
12634                if not Is_Interface (T) then
12635                   Diagnose_Interface (Intf, T);
12636
12637                --  Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
12638                --  a limited type from having a nonlimited progenitor.
12639
12640                elsif (Limited_Present (Def)
12641                        or else (not Is_Interface (Parent_Type)
12642                                  and then Is_Limited_Type (Parent_Type)))
12643                  and then not Is_Limited_Interface (T)
12644                then
12645                   Error_Msg_NE
12646                    ("progenitor interface& of limited type must be limited",
12647                      N, T);
12648                end if;
12649
12650                Next (Intf);
12651             end loop;
12652          end;
12653       end if;
12654
12655       if Parent_Type = Any_Type
12656         or else Etype (Parent_Type) = Any_Type
12657         or else (Is_Class_Wide_Type (Parent_Type)
12658                    and then Etype (Parent_Type) = T)
12659       then
12660          --  If Parent_Type is undefined or illegal, make new type into a
12661          --  subtype of Any_Type, and set a few attributes to prevent cascaded
12662          --  errors. If this is a self-definition, emit error now.
12663
12664          if T = Parent_Type
12665            or else T = Etype (Parent_Type)
12666          then
12667             Error_Msg_N ("type cannot be used in its own definition", Indic);
12668          end if;
12669
12670          Set_Ekind        (T, Ekind (Parent_Type));
12671          Set_Etype        (T, Any_Type);
12672          Set_Scalar_Range (T, Scalar_Range (Any_Type));
12673
12674          if Is_Tagged_Type (T) then
12675             Set_Primitive_Operations (T, New_Elmt_List);
12676          end if;
12677
12678          return;
12679       end if;
12680
12681       --  Ada 2005 (AI-251): The case in which the parent of the full-view is
12682       --  an interface is special because the list of interfaces in the full
12683       --  view can be given in any order. For example:
12684
12685       --     type A is interface;
12686       --     type B is interface and A;
12687       --     type D is new B with private;
12688       --   private
12689       --     type D is new A and B with null record; -- 1 --
12690
12691       --  In this case we perform the following transformation of -1-:
12692
12693       --     type D is new B and A with null record;
12694
12695       --  If the parent of the full-view covers the parent of the partial-view
12696       --  we have two possible cases:
12697
12698       --     1) They have the same parent
12699       --     2) The parent of the full-view implements some further interfaces
12700
12701       --  In both cases we do not need to perform the transformation. In the
12702       --  first case the source program is correct and the transformation is
12703       --  not needed; in the second case the source program does not fulfill
12704       --  the no-hidden interfaces rule (AI-396) and the error will be reported
12705       --  later.
12706
12707       --  This transformation not only simplifies the rest of the analysis of
12708       --  this type declaration but also simplifies the correct generation of
12709       --  the object layout to the expander.
12710
12711       if In_Private_Part (Current_Scope)
12712         and then Is_Interface (Parent_Type)
12713       then
12714          declare
12715             Iface               : Node_Id;
12716             Partial_View        : Entity_Id;
12717             Partial_View_Parent : Entity_Id;
12718             New_Iface           : Node_Id;
12719
12720          begin
12721             --  Look for the associated private type declaration
12722
12723             Partial_View := First_Entity (Current_Scope);
12724             loop
12725                exit when No (Partial_View)
12726                  or else (Has_Private_Declaration (Partial_View)
12727                            and then Full_View (Partial_View) = T);
12728
12729                Next_Entity (Partial_View);
12730             end loop;
12731
12732             --  If the partial view was not found then the source code has
12733             --  errors and the transformation is not needed.
12734
12735             if Present (Partial_View) then
12736                Partial_View_Parent := Etype (Partial_View);
12737
12738                --  If the parent of the full-view covers the parent of the
12739                --  partial-view we have nothing else to do.
12740
12741                if Interface_Present_In_Ancestor
12742                     (Parent_Type, Partial_View_Parent)
12743                then
12744                   null;
12745
12746                --  Traverse the list of interfaces of the full-view to look
12747                --  for the parent of the partial-view and perform the tree
12748                --  transformation.
12749
12750                else
12751                   Iface := First (Interface_List (Def));
12752                   while Present (Iface) loop
12753                      if Etype (Iface) = Etype (Partial_View) then
12754                         Rewrite (Subtype_Indication (Def),
12755                           New_Copy (Subtype_Indication
12756                                      (Parent (Partial_View))));
12757
12758                         New_Iface := Make_Identifier (Sloc (N),
12759                                        Chars (Parent_Type));
12760                         Append (New_Iface, Interface_List (Def));
12761
12762                         --  Analyze the transformed code
12763
12764                         Derived_Type_Declaration (T, N, Is_Completion);
12765                         return;
12766                      end if;
12767
12768                      Next (Iface);
12769                   end loop;
12770                end if;
12771             end if;
12772          end;
12773       end if;
12774
12775       --  Only composite types other than array types are allowed to have
12776       --  discriminants.
12777
12778       if Present (Discriminant_Specifications (N))
12779         and then (Is_Elementary_Type (Parent_Type)
12780                   or else Is_Array_Type (Parent_Type))
12781         and then not Error_Posted (N)
12782       then
12783          Error_Msg_N
12784            ("elementary or array type cannot have discriminants",
12785             Defining_Identifier (First (Discriminant_Specifications (N))));
12786          Set_Has_Discriminants (T, False);
12787       end if;
12788
12789       --  In Ada 83, a derived type defined in a package specification cannot
12790       --  be used for further derivation until the end of its visible part.
12791       --  Note that derivation in the private part of the package is allowed.
12792
12793       if Ada_Version = Ada_83
12794         and then Is_Derived_Type (Parent_Type)
12795         and then In_Visible_Part (Scope (Parent_Type))
12796       then
12797          if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
12798             Error_Msg_N
12799               ("(Ada 83): premature use of type for derivation", Indic);
12800          end if;
12801       end if;
12802
12803       --  Check for early use of incomplete or private type
12804
12805       if Ekind (Parent_Type) = E_Void
12806         or else Ekind (Parent_Type) = E_Incomplete_Type
12807       then
12808          Error_Msg_N ("premature derivation of incomplete type", Indic);
12809          return;
12810
12811       elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
12812               and then not Comes_From_Generic (Parent_Type))
12813         or else Has_Private_Component (Parent_Type)
12814       then
12815          --  The ancestor type of a formal type can be incomplete, in which
12816          --  case only the operations of the partial view are available in
12817          --  the generic. Subsequent checks may be required when the full
12818          --  view is analyzed, to verify that derivation from a tagged type
12819          --  has an extension.
12820
12821          if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
12822             null;
12823
12824          elsif No (Underlying_Type (Parent_Type))
12825            or else Has_Private_Component (Parent_Type)
12826          then
12827             Error_Msg_N
12828               ("premature derivation of derived or private type", Indic);
12829
12830             --  Flag the type itself as being in error, this prevents some
12831             --  nasty problems with subsequent uses of the malformed type.
12832
12833             Set_Error_Posted (T);
12834
12835          --  Check that within the immediate scope of an untagged partial
12836          --  view it's illegal to derive from the partial view if the
12837          --  full view is tagged. (7.3(7))
12838
12839          --  We verify that the Parent_Type is a partial view by checking
12840          --  that it is not a Full_Type_Declaration (i.e. a private type or
12841          --  private extension declaration), to distinguish a partial view
12842          --  from  a derivation from a private type which also appears as
12843          --  E_Private_Type.
12844
12845          elsif Present (Full_View (Parent_Type))
12846            and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
12847            and then not Is_Tagged_Type (Parent_Type)
12848            and then Is_Tagged_Type (Full_View (Parent_Type))
12849          then
12850             Parent_Scope := Scope (T);
12851             while Present (Parent_Scope)
12852               and then Parent_Scope /= Standard_Standard
12853             loop
12854                if Parent_Scope = Scope (Parent_Type) then
12855                   Error_Msg_N
12856                     ("premature derivation from type with tagged full view",
12857                      Indic);
12858                end if;
12859
12860                Parent_Scope := Scope (Parent_Scope);
12861             end loop;
12862          end if;
12863       end if;
12864
12865       --  Check that form of derivation is appropriate
12866
12867       Taggd := Is_Tagged_Type (Parent_Type);
12868
12869       --  Perhaps the parent type should be changed to the class-wide type's
12870       --  specific type in this case to prevent cascading errors ???
12871
12872       if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
12873          Error_Msg_N ("parent type must not be a class-wide type", Indic);
12874          return;
12875       end if;
12876
12877       if Present (Extension) and then not Taggd then
12878          Error_Msg_N
12879            ("type derived from untagged type cannot have extension", Indic);
12880
12881       elsif No (Extension) and then Taggd then
12882
12883          --  If this declaration is within a private part (or body) of a
12884          --  generic instantiation then the derivation is allowed (the parent
12885          --  type can only appear tagged in this case if it's a generic actual
12886          --  type, since it would otherwise have been rejected in the analysis
12887          --  of the generic template).
12888
12889          if not Is_Generic_Actual_Type (Parent_Type)
12890            or else In_Visible_Part (Scope (Parent_Type))
12891          then
12892             Error_Msg_N
12893               ("type derived from tagged type must have extension", Indic);
12894          end if;
12895       end if;
12896
12897       --  AI-443: Synchronized formal derived types require a private
12898       --  extension. There is no point in checking the ancestor type or
12899       --  the progenitors since the construct is wrong to begin with.
12900
12901       if Ada_Version >= Ada_05
12902         and then Is_Generic_Type (T)
12903         and then Present (Original_Node (N))
12904       then
12905          declare
12906             Decl : constant Node_Id := Original_Node (N);
12907
12908          begin
12909             if Nkind (Decl) = N_Formal_Type_Declaration
12910               and then Nkind (Formal_Type_Definition (Decl)) =
12911                          N_Formal_Derived_Type_Definition
12912               and then Synchronized_Present (Formal_Type_Definition (Decl))
12913               and then No (Extension)
12914
12915                --  Avoid emitting a duplicate error message
12916
12917               and then not Error_Posted (Indic)
12918             then
12919                Error_Msg_N
12920                  ("synchronized derived type must have extension", N);
12921             end if;
12922          end;
12923       end if;
12924
12925       if Null_Exclusion_Present (Def)
12926         and then not Is_Access_Type (Parent_Type)
12927       then
12928          Error_Msg_N ("null exclusion can only apply to an access type", N);
12929       end if;
12930
12931       Build_Derived_Type (N, Parent_Type, T, Is_Completion);
12932
12933       --  AI-419: The parent type of an explicitly limited derived type must
12934       --  be a limited type or a limited interface.
12935
12936       if Limited_Present (Def) then
12937          Set_Is_Limited_Record (T);
12938
12939          if Is_Interface (T) then
12940             Set_Is_Limited_Interface (T);
12941          end if;
12942
12943          if not Is_Limited_Type (Parent_Type)
12944            and then
12945              (not Is_Interface (Parent_Type)
12946                or else not Is_Limited_Interface (Parent_Type))
12947          then
12948             Error_Msg_NE ("parent type& of limited type must be limited",
12949               N, Parent_Type);
12950          end if;
12951       end if;
12952    end Derived_Type_Declaration;
12953
12954    ------------------------
12955    -- Diagnose_Interface --
12956    ------------------------
12957
12958    procedure Diagnose_Interface (N : Node_Id;  E : Entity_Id) is
12959    begin
12960       if not Is_Interface (E)
12961         and then  E /= Any_Type
12962       then
12963          Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
12964       end if;
12965    end Diagnose_Interface;
12966
12967    ----------------------------------
12968    -- Enumeration_Type_Declaration --
12969    ----------------------------------
12970
12971    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
12972       Ev     : Uint;
12973       L      : Node_Id;
12974       R_Node : Node_Id;
12975       B_Node : Node_Id;
12976
12977    begin
12978       --  Create identifier node representing lower bound
12979
12980       B_Node := New_Node (N_Identifier, Sloc (Def));
12981       L := First (Literals (Def));
12982       Set_Chars (B_Node, Chars (L));
12983       Set_Entity (B_Node,  L);
12984       Set_Etype (B_Node, T);
12985       Set_Is_Static_Expression (B_Node, True);
12986
12987       R_Node := New_Node (N_Range, Sloc (Def));
12988       Set_Low_Bound  (R_Node, B_Node);
12989
12990       Set_Ekind (T, E_Enumeration_Type);
12991       Set_First_Literal (T, L);
12992       Set_Etype (T, T);
12993       Set_Is_Constrained (T);
12994
12995       Ev := Uint_0;
12996
12997       --  Loop through literals of enumeration type setting pos and rep values
12998       --  except that if the Ekind is already set, then it means that the
12999       --  literal was already constructed (case of a derived type declaration
13000       --  and we should not disturb the Pos and Rep values.
13001
13002       while Present (L) loop
13003          if Ekind (L) /= E_Enumeration_Literal then
13004             Set_Ekind (L, E_Enumeration_Literal);
13005             Set_Enumeration_Pos (L, Ev);
13006             Set_Enumeration_Rep (L, Ev);
13007             Set_Is_Known_Valid  (L, True);
13008          end if;
13009
13010          Set_Etype (L, T);
13011          New_Overloaded_Entity (L);
13012          Generate_Definition (L);
13013          Set_Convention (L, Convention_Intrinsic);
13014
13015          if Nkind (L) = N_Defining_Character_Literal then
13016             Set_Is_Character_Type (T, True);
13017          end if;
13018
13019          Ev := Ev + 1;
13020          Next (L);
13021       end loop;
13022
13023       --  Now create a node representing upper bound
13024
13025       B_Node := New_Node (N_Identifier, Sloc (Def));
13026       Set_Chars (B_Node, Chars (Last (Literals (Def))));
13027       Set_Entity (B_Node,  Last (Literals (Def)));
13028       Set_Etype (B_Node, T);
13029       Set_Is_Static_Expression (B_Node, True);
13030
13031       Set_High_Bound (R_Node, B_Node);
13032
13033       --  Initialize various fields of the type. Some of this information
13034       --  may be overwritten later through rep.clauses.
13035
13036       Set_Scalar_Range    (T, R_Node);
13037       Set_RM_Size         (T, UI_From_Int (Minimum_Size (T)));
13038       Set_Enum_Esize      (T);
13039       Set_Enum_Pos_To_Rep (T, Empty);
13040
13041       --  Set Discard_Names if configuration pragma set, or if there is
13042       --  a parameterless pragma in the current declarative region
13043
13044       if Global_Discard_Names
13045         or else Discard_Names (Scope (T))
13046       then
13047          Set_Discard_Names (T);
13048       end if;
13049
13050       --  Process end label if there is one
13051
13052       if Present (Def) then
13053          Process_End_Label (Def, 'e', T);
13054       end if;
13055    end Enumeration_Type_Declaration;
13056
13057    ---------------------------------
13058    -- Expand_To_Stored_Constraint --
13059    ---------------------------------
13060
13061    function Expand_To_Stored_Constraint
13062      (Typ        : Entity_Id;
13063       Constraint : Elist_Id) return Elist_Id
13064    is
13065       Explicitly_Discriminated_Type : Entity_Id;
13066       Expansion    : Elist_Id;
13067       Discriminant : Entity_Id;
13068
13069       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
13070       --  Find the nearest type that actually specifies discriminants
13071
13072       ---------------------------------
13073       -- Type_With_Explicit_Discrims --
13074       ---------------------------------
13075
13076       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
13077          Typ : constant E := Base_Type (Id);
13078
13079       begin
13080          if Ekind (Typ) in Incomplete_Or_Private_Kind then
13081             if Present (Full_View (Typ)) then
13082                return Type_With_Explicit_Discrims (Full_View (Typ));
13083             end if;
13084
13085          else
13086             if Has_Discriminants (Typ) then
13087                return Typ;
13088             end if;
13089          end if;
13090
13091          if Etype (Typ) = Typ then
13092             return Empty;
13093          elsif Has_Discriminants (Typ) then
13094             return Typ;
13095          else
13096             return Type_With_Explicit_Discrims (Etype (Typ));
13097          end if;
13098
13099       end Type_With_Explicit_Discrims;
13100
13101    --  Start of processing for Expand_To_Stored_Constraint
13102
13103    begin
13104       if No (Constraint)
13105         or else Is_Empty_Elmt_List (Constraint)
13106       then
13107          return No_Elist;
13108       end if;
13109
13110       Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
13111
13112       if No (Explicitly_Discriminated_Type) then
13113          return No_Elist;
13114       end if;
13115
13116       Expansion := New_Elmt_List;
13117
13118       Discriminant :=
13119          First_Stored_Discriminant (Explicitly_Discriminated_Type);
13120       while Present (Discriminant) loop
13121          Append_Elmt (
13122            Get_Discriminant_Value (
13123              Discriminant, Explicitly_Discriminated_Type, Constraint),
13124            Expansion);
13125          Next_Stored_Discriminant (Discriminant);
13126       end loop;
13127
13128       return Expansion;
13129    end Expand_To_Stored_Constraint;
13130
13131    ---------------------------
13132    -- Find_Hidden_Interface --
13133    ---------------------------
13134
13135    function Find_Hidden_Interface
13136      (Src  : Elist_Id;
13137       Dest : Elist_Id) return Entity_Id
13138    is
13139       Iface      : Entity_Id;
13140       Iface_Elmt : Elmt_Id;
13141
13142    begin
13143       if Present (Src) and then Present (Dest) then
13144          Iface_Elmt := First_Elmt (Src);
13145          while Present (Iface_Elmt) loop
13146             Iface := Node (Iface_Elmt);
13147
13148             if Is_Interface (Iface)
13149               and then not Contain_Interface (Iface, Dest)
13150             then
13151                return Iface;
13152             end if;
13153
13154             Next_Elmt (Iface_Elmt);
13155          end loop;
13156       end if;
13157
13158       return Empty;
13159    end Find_Hidden_Interface;
13160
13161    --------------------
13162    -- Find_Type_Name --
13163    --------------------
13164
13165    function Find_Type_Name (N : Node_Id) return Entity_Id is
13166       Id       : constant Entity_Id := Defining_Identifier (N);
13167       Prev     : Entity_Id;
13168       New_Id   : Entity_Id;
13169       Prev_Par : Node_Id;
13170
13171       procedure Tag_Mismatch;
13172       --  Diagnose a tagged partial view whose full view is untagged.
13173       --  We post the message on the full view, with a reference to
13174       --  the previous partial view. The partial view can be private
13175       --  or incomplete, and these are handled in a different manner,
13176       --  so we determine the position of the error message from the
13177       --  respective slocs of both.
13178
13179       ------------------
13180       -- Tag_Mismatch --
13181       ------------------
13182
13183       procedure Tag_Mismatch is
13184       begin
13185          if Sloc (Prev) < Sloc (Id) then
13186             Error_Msg_NE
13187               ("full declaration of } must be a tagged type ", Id, Prev);
13188          else
13189             Error_Msg_NE
13190               ("full declaration of } must be a tagged type ", Prev, Id);
13191          end if;
13192       end Tag_Mismatch;
13193
13194    --  Start processing for Find_Type_Name
13195
13196    begin
13197       --  Find incomplete declaration, if one was given
13198
13199       Prev := Current_Entity_In_Scope (Id);
13200
13201       if Present (Prev) then
13202
13203          --  Previous declaration exists. Error if not incomplete/private case
13204          --  except if previous declaration is implicit, etc. Enter_Name will
13205          --  emit error if appropriate.
13206
13207          Prev_Par := Parent (Prev);
13208
13209          if not Is_Incomplete_Or_Private_Type (Prev) then
13210             Enter_Name (Id);
13211             New_Id := Id;
13212
13213          elsif not Nkind_In (N, N_Full_Type_Declaration,
13214                                 N_Task_Type_Declaration,
13215                                 N_Protected_Type_Declaration)
13216          then
13217             --  Completion must be a full type declarations (RM 7.3(4))
13218
13219             Error_Msg_Sloc := Sloc (Prev);
13220             Error_Msg_NE ("invalid completion of }", Id, Prev);
13221
13222             --  Set scope of Id to avoid cascaded errors. Entity is never
13223             --  examined again, except when saving globals in generics.
13224
13225             Set_Scope (Id, Current_Scope);
13226             New_Id := Id;
13227
13228             --  If this is a repeated incomplete declaration, no further
13229             --  checks are possible.
13230
13231             if Nkind (N) = N_Incomplete_Type_Declaration then
13232                return Prev;
13233             end if;
13234
13235          --  Case of full declaration of incomplete type
13236
13237          elsif Ekind (Prev) = E_Incomplete_Type then
13238
13239             --  Indicate that the incomplete declaration has a matching full
13240             --  declaration. The defining occurrence of the incomplete
13241             --  declaration remains the visible one, and the procedure
13242             --  Get_Full_View dereferences it whenever the type is used.
13243
13244             if Present (Full_View (Prev)) then
13245                Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13246             end if;
13247
13248             Set_Full_View (Prev,  Id);
13249             Append_Entity (Id, Current_Scope);
13250             Set_Is_Public (Id, Is_Public (Prev));
13251             Set_Is_Internal (Id);
13252             New_Id := Prev;
13253
13254          --  Case of full declaration of private type
13255
13256          else
13257             if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
13258                if Etype (Prev) /= Prev then
13259
13260                   --  Prev is a private subtype or a derived type, and needs
13261                   --  no completion.
13262
13263                   Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13264                   New_Id := Id;
13265
13266                elsif Ekind (Prev) = E_Private_Type
13267                  and then Nkind_In (N, N_Task_Type_Declaration,
13268                                        N_Protected_Type_Declaration)
13269                then
13270                   Error_Msg_N
13271                    ("completion of nonlimited type cannot be limited", N);
13272
13273                elsif Ekind (Prev) = E_Record_Type_With_Private
13274                  and then Nkind_In (N, N_Task_Type_Declaration,
13275                                        N_Protected_Type_Declaration)
13276                then
13277                   if not Is_Limited_Record (Prev) then
13278                      Error_Msg_N
13279                         ("completion of nonlimited type cannot be limited", N);
13280
13281                   elsif No (Interface_List (N)) then
13282                      Error_Msg_N
13283                         ("completion of tagged private type must be tagged",
13284                            N);
13285                   end if;
13286                end if;
13287
13288             --  Ada 2005 (AI-251): Private extension declaration of a task
13289             --  type or a protected type. This case arises when covering
13290             --  interface types.
13291
13292             elsif Nkind_In (N, N_Task_Type_Declaration,
13293                                N_Protected_Type_Declaration)
13294             then
13295                null;
13296
13297             elsif Nkind (N) /= N_Full_Type_Declaration
13298               or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
13299             then
13300                Error_Msg_N
13301                  ("full view of private extension must be an extension", N);
13302
13303             elsif not (Abstract_Present (Parent (Prev)))
13304               and then Abstract_Present (Type_Definition (N))
13305             then
13306                Error_Msg_N
13307                  ("full view of non-abstract extension cannot be abstract", N);
13308             end if;
13309
13310             if not In_Private_Part (Current_Scope) then
13311                Error_Msg_N
13312                  ("declaration of full view must appear in private part", N);
13313             end if;
13314
13315             Copy_And_Swap (Prev, Id);
13316             Set_Has_Private_Declaration (Prev);
13317             Set_Has_Private_Declaration (Id);
13318
13319             --  If no error, propagate freeze_node from private to full view.
13320             --  It may have been generated for an early operational item.
13321
13322             if Present (Freeze_Node (Id))
13323               and then Serious_Errors_Detected = 0
13324               and then No (Full_View (Id))
13325             then
13326                Set_Freeze_Node (Prev, Freeze_Node (Id));
13327                Set_Freeze_Node (Id, Empty);
13328                Set_First_Rep_Item (Prev, First_Rep_Item (Id));
13329             end if;
13330
13331             Set_Full_View (Id, Prev);
13332             New_Id := Prev;
13333          end if;
13334
13335          --  Verify that full declaration conforms to partial one
13336
13337          if Is_Incomplete_Or_Private_Type (Prev)
13338            and then Present (Discriminant_Specifications (Prev_Par))
13339          then
13340             if Present (Discriminant_Specifications (N)) then
13341                if Ekind (Prev) = E_Incomplete_Type then
13342                   Check_Discriminant_Conformance (N, Prev, Prev);
13343                else
13344                   Check_Discriminant_Conformance (N, Prev, Id);
13345                end if;
13346
13347             else
13348                Error_Msg_N
13349                  ("missing discriminants in full type declaration", N);
13350
13351                --  To avoid cascaded errors on subsequent use, share the
13352                --  discriminants of the partial view.
13353
13354                Set_Discriminant_Specifications (N,
13355                  Discriminant_Specifications (Prev_Par));
13356             end if;
13357          end if;
13358
13359          --  A prior untagged partial view can have an associated class-wide
13360          --  type due to use of the class attribute, and in this case the full
13361          --  type must also be tagged. This Ada 95 usage is deprecated in favor
13362          --  of incomplete tagged declarations, but we check for it.
13363
13364          if Is_Type (Prev)
13365            and then (Is_Tagged_Type (Prev)
13366                       or else Present (Class_Wide_Type (Prev)))
13367          then
13368             --  The full declaration is either a tagged type (including
13369             --  a synchronized type that implements interfaces) or a
13370             --  type extension, otherwise this is an error.
13371
13372             if Nkind_In (N, N_Task_Type_Declaration,
13373                             N_Protected_Type_Declaration)
13374             then
13375                if No (Interface_List (N))
13376                  and then not Error_Posted (N)
13377                then
13378                   Tag_Mismatch;
13379                end if;
13380
13381             elsif Nkind (Type_Definition (N)) = N_Record_Definition then
13382
13383                --  Indicate that the previous declaration (tagged incomplete
13384                --  or private declaration) requires the same on the full one.
13385
13386                if not Tagged_Present (Type_Definition (N)) then
13387                   Tag_Mismatch;
13388                   Set_Is_Tagged_Type (Id);
13389                   Set_Primitive_Operations (Id, New_Elmt_List);
13390                end if;
13391
13392             elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
13393                if No (Record_Extension_Part (Type_Definition (N))) then
13394                   Error_Msg_NE (
13395                     "full declaration of } must be a record extension",
13396                     Prev, Id);
13397                   Set_Is_Tagged_Type (Id);
13398                   Set_Primitive_Operations (Id, New_Elmt_List);
13399                end if;
13400
13401             else
13402                Tag_Mismatch;
13403             end if;
13404          end if;
13405
13406          return New_Id;
13407
13408       else
13409          --  New type declaration
13410
13411          Enter_Name (Id);
13412          return Id;
13413       end if;
13414    end Find_Type_Name;
13415
13416    -------------------------
13417    -- Find_Type_Of_Object --
13418    -------------------------
13419
13420    function Find_Type_Of_Object
13421      (Obj_Def     : Node_Id;
13422       Related_Nod : Node_Id) return Entity_Id
13423    is
13424       Def_Kind : constant Node_Kind := Nkind (Obj_Def);
13425       P        : Node_Id := Parent (Obj_Def);
13426       T        : Entity_Id;
13427       Nam      : Name_Id;
13428
13429    begin
13430       --  If the parent is a component_definition node we climb to the
13431       --  component_declaration node
13432
13433       if Nkind (P) = N_Component_Definition then
13434          P := Parent (P);
13435       end if;
13436
13437       --  Case of an anonymous array subtype
13438
13439       if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
13440                              N_Unconstrained_Array_Definition)
13441       then
13442          T := Empty;
13443          Array_Type_Declaration (T, Obj_Def);
13444
13445       --  Create an explicit subtype whenever possible
13446
13447       elsif Nkind (P) /= N_Component_Declaration
13448         and then Def_Kind = N_Subtype_Indication
13449       then
13450          --  Base name of subtype on object name, which will be unique in
13451          --  the current scope.
13452
13453          --  If this is a duplicate declaration, return base type, to avoid
13454          --  generating duplicate anonymous types.
13455
13456          if Error_Posted (P) then
13457             Analyze (Subtype_Mark (Obj_Def));
13458             return Entity (Subtype_Mark (Obj_Def));
13459          end if;
13460
13461          Nam :=
13462             New_External_Name
13463              (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
13464
13465          T := Make_Defining_Identifier (Sloc (P), Nam);
13466
13467          Insert_Action (Obj_Def,
13468            Make_Subtype_Declaration (Sloc (P),
13469              Defining_Identifier => T,
13470              Subtype_Indication  => Relocate_Node (Obj_Def)));
13471
13472          --  This subtype may need freezing, and this will not be done
13473          --  automatically if the object declaration is not in declarative
13474          --  part. Since this is an object declaration, the type cannot always
13475          --  be frozen here. Deferred constants do not freeze their type
13476          --  (which often enough will be private).
13477
13478          if Nkind (P) = N_Object_Declaration
13479            and then Constant_Present (P)
13480            and then No (Expression (P))
13481          then
13482             null;
13483          else
13484             Insert_Actions (Obj_Def, Freeze_Entity (T, Sloc (P)));
13485          end if;
13486
13487       --  Ada 2005 AI-406: the object definition in an object declaration
13488       --  can be an access definition.
13489
13490       elsif Def_Kind = N_Access_Definition then
13491          T := Access_Definition (Related_Nod, Obj_Def);
13492          Set_Is_Local_Anonymous_Access (T);
13493
13494       --  Otherwise, the object definition is just a subtype_mark
13495
13496       else
13497          T := Process_Subtype (Obj_Def, Related_Nod);
13498       end if;
13499
13500       return T;
13501    end Find_Type_Of_Object;
13502
13503    --------------------------------
13504    -- Find_Type_Of_Subtype_Indic --
13505    --------------------------------
13506
13507    function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
13508       Typ : Entity_Id;
13509
13510    begin
13511       --  Case of subtype mark with a constraint
13512
13513       if Nkind (S) = N_Subtype_Indication then
13514          Find_Type (Subtype_Mark (S));
13515          Typ := Entity (Subtype_Mark (S));
13516
13517          if not
13518            Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
13519          then
13520             Error_Msg_N
13521               ("incorrect constraint for this kind of type", Constraint (S));
13522             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
13523          end if;
13524
13525       --  Otherwise we have a subtype mark without a constraint
13526
13527       elsif Error_Posted (S) then
13528          Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
13529          return Any_Type;
13530
13531       else
13532          Find_Type (S);
13533          Typ := Entity (S);
13534       end if;
13535
13536       --  Check No_Wide_Characters restriction
13537
13538       if Typ = Standard_Wide_Character
13539         or else Typ = Standard_Wide_Wide_Character
13540         or else Typ = Standard_Wide_String
13541         or else Typ = Standard_Wide_Wide_String
13542       then
13543          Check_Restriction (No_Wide_Characters, S);
13544       end if;
13545
13546       return Typ;
13547    end Find_Type_Of_Subtype_Indic;
13548
13549    -------------------------------------
13550    -- Floating_Point_Type_Declaration --
13551    -------------------------------------
13552
13553    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13554       Digs          : constant Node_Id := Digits_Expression (Def);
13555       Digs_Val      : Uint;
13556       Base_Typ      : Entity_Id;
13557       Implicit_Base : Entity_Id;
13558       Bound         : Node_Id;
13559
13560       function Can_Derive_From (E : Entity_Id) return Boolean;
13561       --  Find if given digits value allows derivation from specified type
13562
13563       ---------------------
13564       -- Can_Derive_From --
13565       ---------------------
13566
13567       function Can_Derive_From (E : Entity_Id) return Boolean is
13568          Spec : constant Entity_Id := Real_Range_Specification (Def);
13569
13570       begin
13571          if Digs_Val > Digits_Value (E) then
13572             return False;
13573          end if;
13574
13575          if Present (Spec) then
13576             if Expr_Value_R (Type_Low_Bound (E)) >
13577                Expr_Value_R (Low_Bound (Spec))
13578             then
13579                return False;
13580             end if;
13581
13582             if Expr_Value_R (Type_High_Bound (E)) <
13583                Expr_Value_R (High_Bound (Spec))
13584             then
13585                return False;
13586             end if;
13587          end if;
13588
13589          return True;
13590       end Can_Derive_From;
13591
13592    --  Start of processing for Floating_Point_Type_Declaration
13593
13594    begin
13595       Check_Restriction (No_Floating_Point, Def);
13596
13597       --  Create an implicit base type
13598
13599       Implicit_Base :=
13600         Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
13601
13602       --  Analyze and verify digits value
13603
13604       Analyze_And_Resolve (Digs, Any_Integer);
13605       Check_Digits_Expression (Digs);
13606       Digs_Val := Expr_Value (Digs);
13607
13608       --  Process possible range spec and find correct type to derive from
13609
13610       Process_Real_Range_Specification (Def);
13611
13612       if Can_Derive_From (Standard_Short_Float) then
13613          Base_Typ := Standard_Short_Float;
13614       elsif Can_Derive_From (Standard_Float) then
13615          Base_Typ := Standard_Float;
13616       elsif Can_Derive_From (Standard_Long_Float) then
13617          Base_Typ := Standard_Long_Float;
13618       elsif Can_Derive_From (Standard_Long_Long_Float) then
13619          Base_Typ := Standard_Long_Long_Float;
13620
13621       --  If we can't derive from any existing type, use long_long_float
13622       --  and give appropriate message explaining the problem.
13623
13624       else
13625          Base_Typ := Standard_Long_Long_Float;
13626
13627          if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
13628             Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
13629             Error_Msg_N ("digits value out of range, maximum is ^", Digs);
13630
13631          else
13632             Error_Msg_N
13633               ("range too large for any predefined type",
13634                Real_Range_Specification (Def));
13635          end if;
13636       end if;
13637
13638       --  If there are bounds given in the declaration use them as the bounds
13639       --  of the type, otherwise use the bounds of the predefined base type
13640       --  that was chosen based on the Digits value.
13641
13642       if Present (Real_Range_Specification (Def)) then
13643          Set_Scalar_Range (T, Real_Range_Specification (Def));
13644          Set_Is_Constrained (T);
13645
13646          --  The bounds of this range must be converted to machine numbers
13647          --  in accordance with RM 4.9(38).
13648
13649          Bound := Type_Low_Bound (T);
13650
13651          if Nkind (Bound) = N_Real_Literal then
13652             Set_Realval
13653               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
13654             Set_Is_Machine_Number (Bound);
13655          end if;
13656
13657          Bound := Type_High_Bound (T);
13658
13659          if Nkind (Bound) = N_Real_Literal then
13660             Set_Realval
13661               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
13662             Set_Is_Machine_Number (Bound);
13663          end if;
13664
13665       else
13666          Set_Scalar_Range (T, Scalar_Range (Base_Typ));
13667       end if;
13668
13669       --  Complete definition of implicit base and declared first subtype
13670
13671       Set_Etype          (Implicit_Base, Base_Typ);
13672
13673       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
13674       Set_Size_Info      (Implicit_Base,                (Base_Typ));
13675       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
13676       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
13677       Set_Digits_Value   (Implicit_Base, Digits_Value   (Base_Typ));
13678       Set_Vax_Float      (Implicit_Base, Vax_Float      (Base_Typ));
13679
13680       Set_Ekind          (T, E_Floating_Point_Subtype);
13681       Set_Etype          (T, Implicit_Base);
13682
13683       Set_Size_Info      (T,                (Implicit_Base));
13684       Set_RM_Size        (T, RM_Size        (Implicit_Base));
13685       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
13686       Set_Digits_Value   (T, Digs_Val);
13687    end Floating_Point_Type_Declaration;
13688
13689    ----------------------------
13690    -- Get_Discriminant_Value --
13691    ----------------------------
13692
13693    --  This is the situation:
13694
13695    --  There is a non-derived type
13696
13697    --       type T0 (Dx, Dy, Dz...)
13698
13699    --  There are zero or more levels of derivation, with each derivation
13700    --  either purely inheriting the discriminants, or defining its own.
13701
13702    --       type Ti      is new Ti-1
13703    --  or
13704    --       type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
13705    --  or
13706    --       subtype Ti is ...
13707
13708    --  The subtype issue is avoided by the use of Original_Record_Component,
13709    --  and the fact that derived subtypes also derive the constraints.
13710
13711    --  This chain leads back from
13712
13713    --       Typ_For_Constraint
13714
13715    --  Typ_For_Constraint has discriminants, and the value for each
13716    --  discriminant is given by its corresponding Elmt of Constraints.
13717
13718    --  Discriminant is some discriminant in this hierarchy
13719
13720    --  We need to return its value
13721
13722    --  We do this by recursively searching each level, and looking for
13723    --  Discriminant. Once we get to the bottom, we start backing up
13724    --  returning the value for it which may in turn be a discriminant
13725    --  further up, so on the backup we continue the substitution.
13726
13727    function Get_Discriminant_Value
13728      (Discriminant       : Entity_Id;
13729       Typ_For_Constraint : Entity_Id;
13730       Constraint         : Elist_Id) return Node_Id
13731    is
13732       function Search_Derivation_Levels
13733         (Ti                    : Entity_Id;
13734          Discrim_Values        : Elist_Id;
13735          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
13736       --  This is the routine that performs the recursive search of levels
13737       --  as described above.
13738
13739       ------------------------------
13740       -- Search_Derivation_Levels --
13741       ------------------------------
13742
13743       function Search_Derivation_Levels
13744         (Ti                    : Entity_Id;
13745          Discrim_Values        : Elist_Id;
13746          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
13747       is
13748          Assoc          : Elmt_Id;
13749          Disc           : Entity_Id;
13750          Result         : Node_Or_Entity_Id;
13751          Result_Entity  : Node_Id;
13752
13753       begin
13754          --  If inappropriate type, return Error, this happens only in
13755          --  cascaded error situations, and we want to avoid a blow up.
13756
13757          if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
13758             return Error;
13759          end if;
13760
13761          --  Look deeper if possible. Use Stored_Constraints only for
13762          --  untagged types. For tagged types use the given constraint.
13763          --  This asymmetry needs explanation???
13764
13765          if not Stored_Discrim_Values
13766            and then Present (Stored_Constraint (Ti))
13767            and then not Is_Tagged_Type (Ti)
13768          then
13769             Result :=
13770               Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
13771          else
13772             declare
13773                Td : constant Entity_Id := Etype (Ti);
13774
13775             begin
13776                if Td = Ti then
13777                   Result := Discriminant;
13778
13779                else
13780                   if Present (Stored_Constraint (Ti)) then
13781                      Result :=
13782                         Search_Derivation_Levels
13783                           (Td, Stored_Constraint (Ti), True);
13784                   else
13785                      Result :=
13786                         Search_Derivation_Levels
13787                           (Td, Discrim_Values, Stored_Discrim_Values);
13788                   end if;
13789                end if;
13790             end;
13791          end if;
13792
13793          --  Extra underlying places to search, if not found above. For
13794          --  concurrent types, the relevant discriminant appears in the
13795          --  corresponding record. For a type derived from a private type
13796          --  without discriminant, the full view inherits the discriminants
13797          --  of the full view of the parent.
13798
13799          if Result = Discriminant then
13800             if Is_Concurrent_Type (Ti)
13801               and then Present (Corresponding_Record_Type (Ti))
13802             then
13803                Result :=
13804                  Search_Derivation_Levels (
13805                    Corresponding_Record_Type (Ti),
13806                    Discrim_Values,
13807                    Stored_Discrim_Values);
13808
13809             elsif Is_Private_Type (Ti)
13810               and then not Has_Discriminants (Ti)
13811               and then Present (Full_View (Ti))
13812               and then Etype (Full_View (Ti)) /= Ti
13813             then
13814                Result :=
13815                  Search_Derivation_Levels (
13816                    Full_View (Ti),
13817                    Discrim_Values,
13818                    Stored_Discrim_Values);
13819             end if;
13820          end if;
13821
13822          --  If Result is not a (reference to a) discriminant, return it,
13823          --  otherwise set Result_Entity to the discriminant.
13824
13825          if Nkind (Result) = N_Defining_Identifier then
13826             pragma Assert (Result = Discriminant);
13827             Result_Entity := Result;
13828
13829          else
13830             if not Denotes_Discriminant (Result) then
13831                return Result;
13832             end if;
13833
13834             Result_Entity := Entity (Result);
13835          end if;
13836
13837          --  See if this level of derivation actually has discriminants
13838          --  because tagged derivations can add them, hence the lower
13839          --  levels need not have any.
13840
13841          if not Has_Discriminants (Ti) then
13842             return Result;
13843          end if;
13844
13845          --  Scan Ti's discriminants for Result_Entity,
13846          --  and return its corresponding value, if any.
13847
13848          Result_Entity := Original_Record_Component (Result_Entity);
13849
13850          Assoc := First_Elmt (Discrim_Values);
13851
13852          if Stored_Discrim_Values then
13853             Disc := First_Stored_Discriminant (Ti);
13854          else
13855             Disc := First_Discriminant (Ti);
13856          end if;
13857
13858          while Present (Disc) loop
13859             pragma Assert (Present (Assoc));
13860
13861             if Original_Record_Component (Disc) = Result_Entity then
13862                return Node (Assoc);
13863             end if;
13864
13865             Next_Elmt (Assoc);
13866
13867             if Stored_Discrim_Values then
13868                Next_Stored_Discriminant (Disc);
13869             else
13870                Next_Discriminant (Disc);
13871             end if;
13872          end loop;
13873
13874          --  Could not find it
13875          --
13876          return Result;
13877       end Search_Derivation_Levels;
13878
13879       --  Local Variables
13880
13881       Result : Node_Or_Entity_Id;
13882
13883    --  Start of processing for Get_Discriminant_Value
13884
13885    begin
13886       --  ??? This routine is a gigantic mess and will be deleted. For the
13887       --  time being just test for the trivial case before calling recurse.
13888
13889       if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
13890          declare
13891             D : Entity_Id;
13892             E : Elmt_Id;
13893
13894          begin
13895             D := First_Discriminant (Typ_For_Constraint);
13896             E := First_Elmt (Constraint);
13897             while Present (D) loop
13898                if Chars (D) = Chars (Discriminant) then
13899                   return Node (E);
13900                end if;
13901
13902                Next_Discriminant (D);
13903                Next_Elmt (E);
13904             end loop;
13905          end;
13906       end if;
13907
13908       Result := Search_Derivation_Levels
13909         (Typ_For_Constraint, Constraint, False);
13910
13911       --  ??? hack to disappear when this routine is gone
13912
13913       if  Nkind (Result) = N_Defining_Identifier then
13914          declare
13915             D : Entity_Id;
13916             E : Elmt_Id;
13917
13918          begin
13919             D := First_Discriminant (Typ_For_Constraint);
13920             E := First_Elmt (Constraint);
13921             while Present (D) loop
13922                if Corresponding_Discriminant (D) = Discriminant then
13923                   return Node (E);
13924                end if;
13925
13926                Next_Discriminant (D);
13927                Next_Elmt (E);
13928             end loop;
13929          end;
13930       end if;
13931
13932       pragma Assert (Nkind (Result) /= N_Defining_Identifier);
13933       return Result;
13934    end Get_Discriminant_Value;
13935
13936    --------------------------
13937    -- Has_Range_Constraint --
13938    --------------------------
13939
13940    function Has_Range_Constraint (N : Node_Id) return Boolean is
13941       C : constant Node_Id := Constraint (N);
13942
13943    begin
13944       if Nkind (C) = N_Range_Constraint then
13945          return True;
13946
13947       elsif Nkind (C) = N_Digits_Constraint then
13948          return
13949             Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
13950               or else
13951             Present (Range_Constraint (C));
13952
13953       elsif Nkind (C) = N_Delta_Constraint then
13954          return Present (Range_Constraint (C));
13955
13956       else
13957          return False;
13958       end if;
13959    end Has_Range_Constraint;
13960
13961    ------------------------
13962    -- Inherit_Components --
13963    ------------------------
13964
13965    function Inherit_Components
13966      (N             : Node_Id;
13967       Parent_Base   : Entity_Id;
13968       Derived_Base  : Entity_Id;
13969       Is_Tagged     : Boolean;
13970       Inherit_Discr : Boolean;
13971       Discs         : Elist_Id) return Elist_Id
13972    is
13973       Assoc_List : constant Elist_Id := New_Elmt_List;
13974
13975       procedure Inherit_Component
13976         (Old_C          : Entity_Id;
13977          Plain_Discrim  : Boolean := False;
13978          Stored_Discrim : Boolean := False);
13979       --  Inherits component Old_C from Parent_Base to the Derived_Base. If
13980       --  Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
13981       --  True, Old_C is a stored discriminant. If they are both false then
13982       --  Old_C is a regular component.
13983
13984       -----------------------
13985       -- Inherit_Component --
13986       -----------------------
13987
13988       procedure Inherit_Component
13989         (Old_C          : Entity_Id;
13990          Plain_Discrim  : Boolean := False;
13991          Stored_Discrim : Boolean := False)
13992       is
13993          New_C : constant Entity_Id := New_Copy (Old_C);
13994
13995          Discrim      : Entity_Id;
13996          Corr_Discrim : Entity_Id;
13997
13998       begin
13999          pragma Assert (not Is_Tagged or else not Stored_Discrim);
14000
14001          Set_Parent (New_C, Parent (Old_C));
14002
14003          --  Regular discriminants and components must be inserted in the scope
14004          --  of the Derived_Base. Do it here.
14005
14006          if not Stored_Discrim then
14007             Enter_Name (New_C);
14008          end if;
14009
14010          --  For tagged types the Original_Record_Component must point to
14011          --  whatever this field was pointing to in the parent type. This has
14012          --  already been achieved by the call to New_Copy above.
14013
14014          if not Is_Tagged then
14015             Set_Original_Record_Component (New_C, New_C);
14016          end if;
14017
14018          --  If we have inherited a component then see if its Etype contains
14019          --  references to Parent_Base discriminants. In this case, replace
14020          --  these references with the constraints given in Discs. We do not
14021          --  do this for the partial view of private types because this is
14022          --  not needed (only the components of the full view will be used
14023          --  for code generation) and cause problem. We also avoid this
14024          --  transformation in some error situations.
14025
14026          if Ekind (New_C) = E_Component then
14027             if (Is_Private_Type (Derived_Base)
14028                  and then not Is_Generic_Type (Derived_Base))
14029               or else (Is_Empty_Elmt_List (Discs)
14030                         and then  not Expander_Active)
14031             then
14032                Set_Etype (New_C, Etype (Old_C));
14033
14034             else
14035                --  The current component introduces a circularity of the
14036                --  following kind:
14037
14038                --     limited with Pack_2;
14039                --     package Pack_1 is
14040                --        type T_1 is tagged record
14041                --           Comp : access Pack_2.T_2;
14042                --           ...
14043                --        end record;
14044                --     end Pack_1;
14045
14046                --     with Pack_1;
14047                --     package Pack_2 is
14048                --        type T_2 is new Pack_1.T_1 with ...;
14049                --     end Pack_2;
14050
14051                Set_Etype
14052                  (New_C,
14053                   Constrain_Component_Type
14054                   (Old_C, Derived_Base, N, Parent_Base, Discs));
14055             end if;
14056          end if;
14057
14058          --  In derived tagged types it is illegal to reference a non
14059          --  discriminant component in the parent type. To catch this, mark
14060          --  these components with an Ekind of E_Void. This will be reset in
14061          --  Record_Type_Definition after processing the record extension of
14062          --  the derived type.
14063
14064          --  If the declaration is a private extension, there is no further
14065          --  record extension to process, and the components retain their
14066          --  current kind, because they are visible at this point.
14067
14068          if Is_Tagged and then Ekind (New_C) = E_Component
14069            and then Nkind (N) /= N_Private_Extension_Declaration
14070          then
14071             Set_Ekind (New_C, E_Void);
14072          end if;
14073
14074          if Plain_Discrim then
14075             Set_Corresponding_Discriminant (New_C, Old_C);
14076             Build_Discriminal (New_C);
14077
14078          --  If we are explicitly inheriting a stored discriminant it will be
14079          --  completely hidden.
14080
14081          elsif Stored_Discrim then
14082             Set_Corresponding_Discriminant (New_C, Empty);
14083             Set_Discriminal (New_C, Empty);
14084             Set_Is_Completely_Hidden (New_C);
14085
14086             --  Set the Original_Record_Component of each discriminant in the
14087             --  derived base to point to the corresponding stored that we just
14088             --  created.
14089
14090             Discrim := First_Discriminant (Derived_Base);
14091             while Present (Discrim) loop
14092                Corr_Discrim := Corresponding_Discriminant (Discrim);
14093
14094                --  Corr_Discrim could be missing in an error situation
14095
14096                if Present (Corr_Discrim)
14097                  and then Original_Record_Component (Corr_Discrim) = Old_C
14098                then
14099                   Set_Original_Record_Component (Discrim, New_C);
14100                end if;
14101
14102                Next_Discriminant (Discrim);
14103             end loop;
14104
14105             Append_Entity (New_C, Derived_Base);
14106          end if;
14107
14108          if not Is_Tagged then
14109             Append_Elmt (Old_C, Assoc_List);
14110             Append_Elmt (New_C, Assoc_List);
14111          end if;
14112       end Inherit_Component;
14113
14114       --  Variables local to Inherit_Component
14115
14116       Loc : constant Source_Ptr := Sloc (N);
14117
14118       Parent_Discrim : Entity_Id;
14119       Stored_Discrim : Entity_Id;
14120       D              : Entity_Id;
14121       Component      : Entity_Id;
14122
14123    --  Start of processing for Inherit_Components
14124
14125    begin
14126       if not Is_Tagged then
14127          Append_Elmt (Parent_Base,  Assoc_List);
14128          Append_Elmt (Derived_Base, Assoc_List);
14129       end if;
14130
14131       --  Inherit parent discriminants if needed
14132
14133       if Inherit_Discr then
14134          Parent_Discrim := First_Discriminant (Parent_Base);
14135          while Present (Parent_Discrim) loop
14136             Inherit_Component (Parent_Discrim, Plain_Discrim => True);
14137             Next_Discriminant (Parent_Discrim);
14138          end loop;
14139       end if;
14140
14141       --  Create explicit stored discrims for untagged types when necessary
14142
14143       if not Has_Unknown_Discriminants (Derived_Base)
14144         and then Has_Discriminants (Parent_Base)
14145         and then not Is_Tagged
14146         and then
14147           (not Inherit_Discr
14148              or else First_Discriminant (Parent_Base) /=
14149                      First_Stored_Discriminant (Parent_Base))
14150       then
14151          Stored_Discrim := First_Stored_Discriminant (Parent_Base);
14152          while Present (Stored_Discrim) loop
14153             Inherit_Component (Stored_Discrim, Stored_Discrim => True);
14154             Next_Stored_Discriminant (Stored_Discrim);
14155          end loop;
14156       end if;
14157
14158       --  See if we can apply the second transformation for derived types, as
14159       --  explained in point 6. in the comments above Build_Derived_Record_Type
14160       --  This is achieved by appending Derived_Base discriminants into Discs,
14161       --  which has the side effect of returning a non empty Discs list to the
14162       --  caller of Inherit_Components, which is what we want. This must be
14163       --  done for private derived types if there are explicit stored
14164       --  discriminants, to ensure that we can retrieve the values of the
14165       --  constraints provided in the ancestors.
14166
14167       if Inherit_Discr
14168         and then Is_Empty_Elmt_List (Discs)
14169         and then Present (First_Discriminant (Derived_Base))
14170         and then
14171           (not Is_Private_Type (Derived_Base)
14172              or else Is_Completely_Hidden
14173                (First_Stored_Discriminant (Derived_Base))
14174              or else Is_Generic_Type (Derived_Base))
14175       then
14176          D := First_Discriminant (Derived_Base);
14177          while Present (D) loop
14178             Append_Elmt (New_Reference_To (D, Loc), Discs);
14179             Next_Discriminant (D);
14180          end loop;
14181       end if;
14182
14183       --  Finally, inherit non-discriminant components unless they are not
14184       --  visible because defined or inherited from the full view of the
14185       --  parent. Don't inherit the _parent field of the parent type.
14186
14187       Component := First_Entity (Parent_Base);
14188       while Present (Component) loop
14189
14190          --  Ada 2005 (AI-251): Do not inherit components associated with
14191          --  secondary tags of the parent.
14192
14193          if Ekind (Component) = E_Component
14194            and then Present (Related_Type (Component))
14195          then
14196             null;
14197
14198          elsif Ekind (Component) /= E_Component
14199            or else Chars (Component) = Name_uParent
14200          then
14201             null;
14202
14203          --  If the derived type is within the parent type's declarative
14204          --  region, then the components can still be inherited even though
14205          --  they aren't visible at this point. This can occur for cases
14206          --  such as within public child units where the components must
14207          --  become visible upon entering the child unit's private part.
14208
14209          elsif not Is_Visible_Component (Component)
14210            and then not In_Open_Scopes (Scope (Parent_Base))
14211          then
14212             null;
14213
14214          elsif Ekind (Derived_Base) = E_Private_Type
14215            or else Ekind (Derived_Base) = E_Limited_Private_Type
14216          then
14217             null;
14218
14219          else
14220             Inherit_Component (Component);
14221          end if;
14222
14223          Next_Entity (Component);
14224       end loop;
14225
14226       --  For tagged derived types, inherited discriminants cannot be used in
14227       --  component declarations of the record extension part. To achieve this
14228       --  we mark the inherited discriminants as not visible.
14229
14230       if Is_Tagged and then Inherit_Discr then
14231          D := First_Discriminant (Derived_Base);
14232          while Present (D) loop
14233             Set_Is_Immediately_Visible (D, False);
14234             Next_Discriminant (D);
14235          end loop;
14236       end if;
14237
14238       return Assoc_List;
14239    end Inherit_Components;
14240
14241    -----------------------
14242    -- Is_Null_Extension --
14243    -----------------------
14244
14245    function Is_Null_Extension (T : Entity_Id) return Boolean is
14246       Type_Decl : constant Node_Id := Parent (T);
14247       Comp_List : Node_Id;
14248       Comp      : Node_Id;
14249
14250    begin
14251       if Nkind (Type_Decl) /= N_Full_Type_Declaration
14252         or else not Is_Tagged_Type (T)
14253         or else Nkind (Type_Definition (Type_Decl)) /=
14254                                               N_Derived_Type_Definition
14255         or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
14256       then
14257          return False;
14258       end if;
14259
14260       Comp_List :=
14261         Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
14262
14263       if Present (Discriminant_Specifications (Type_Decl)) then
14264          return False;
14265
14266       elsif Present (Comp_List)
14267         and then Is_Non_Empty_List (Component_Items (Comp_List))
14268       then
14269          Comp := First (Component_Items (Comp_List));
14270
14271          --  Only user-defined components are relevant. The component list
14272          --  may also contain a parent component and internal components
14273          --  corresponding to secondary tags, but these do not determine
14274          --  whether this is a null extension.
14275
14276          while Present (Comp) loop
14277             if Comes_From_Source (Comp) then
14278                return False;
14279             end if;
14280
14281             Next (Comp);
14282          end loop;
14283
14284          return True;
14285       else
14286          return True;
14287       end if;
14288    end Is_Null_Extension;
14289
14290    --------------------
14291    --  Is_Progenitor --
14292    --------------------
14293
14294    function Is_Progenitor
14295      (Iface : Entity_Id;
14296       Typ   : Entity_Id) return Boolean
14297    is
14298    begin
14299       return Implements_Interface (Typ, Iface,
14300                Exclude_Parents => True);
14301    end Is_Progenitor;
14302
14303    ------------------------------
14304    -- Is_Valid_Constraint_Kind --
14305    ------------------------------
14306
14307    function Is_Valid_Constraint_Kind
14308      (T_Kind          : Type_Kind;
14309       Constraint_Kind : Node_Kind) return Boolean
14310    is
14311    begin
14312       case T_Kind is
14313          when Enumeration_Kind |
14314               Integer_Kind =>
14315             return Constraint_Kind = N_Range_Constraint;
14316
14317          when Decimal_Fixed_Point_Kind =>
14318             return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14319                                               N_Range_Constraint);
14320
14321          when Ordinary_Fixed_Point_Kind =>
14322             return Nkind_In (Constraint_Kind, N_Delta_Constraint,
14323                                               N_Range_Constraint);
14324
14325          when Float_Kind =>
14326             return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14327                                               N_Range_Constraint);
14328
14329          when Access_Kind       |
14330               Array_Kind        |
14331               E_Record_Type     |
14332               E_Record_Subtype  |
14333               Class_Wide_Kind   |
14334               E_Incomplete_Type |
14335               Private_Kind      |
14336               Concurrent_Kind  =>
14337             return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
14338
14339          when others =>
14340             return True; -- Error will be detected later
14341       end case;
14342    end Is_Valid_Constraint_Kind;
14343
14344    --------------------------
14345    -- Is_Visible_Component --
14346    --------------------------
14347
14348    function Is_Visible_Component (C : Entity_Id) return Boolean is
14349       Original_Comp  : Entity_Id := Empty;
14350       Original_Scope : Entity_Id;
14351       Type_Scope     : Entity_Id;
14352
14353       function Is_Local_Type (Typ : Entity_Id) return Boolean;
14354       --  Check whether parent type of inherited component is declared locally,
14355       --  possibly within a nested package or instance. The current scope is
14356       --  the derived record itself.
14357
14358       -------------------
14359       -- Is_Local_Type --
14360       -------------------
14361
14362       function Is_Local_Type (Typ : Entity_Id) return Boolean is
14363          Scop : Entity_Id;
14364
14365       begin
14366          Scop := Scope (Typ);
14367          while Present (Scop)
14368            and then Scop /= Standard_Standard
14369          loop
14370             if Scop = Scope (Current_Scope) then
14371                return True;
14372             end if;
14373
14374             Scop := Scope (Scop);
14375          end loop;
14376
14377          return False;
14378       end Is_Local_Type;
14379
14380    --  Start of processing for Is_Visible_Component
14381
14382    begin
14383       if Ekind (C) = E_Component
14384         or else Ekind (C) = E_Discriminant
14385       then
14386          Original_Comp := Original_Record_Component (C);
14387       end if;
14388
14389       if No (Original_Comp) then
14390
14391          --  Premature usage, or previous error
14392
14393          return False;
14394
14395       else
14396          Original_Scope := Scope (Original_Comp);
14397          Type_Scope     := Scope (Base_Type (Scope (C)));
14398       end if;
14399
14400       --  This test only concerns tagged types
14401
14402       if not Is_Tagged_Type (Original_Scope) then
14403          return True;
14404
14405       --  If it is _Parent or _Tag, there is no visibility issue
14406
14407       elsif not Comes_From_Source (Original_Comp) then
14408          return True;
14409
14410       --  If we are in the body of an instantiation, the component is visible
14411       --  even when the parent type (possibly defined in an enclosing unit or
14412       --  in a parent unit) might not.
14413
14414       elsif In_Instance_Body then
14415          return True;
14416
14417       --  Discriminants are always visible
14418
14419       elsif Ekind (Original_Comp) = E_Discriminant
14420         and then not Has_Unknown_Discriminants (Original_Scope)
14421       then
14422          return True;
14423
14424       --  If the component has been declared in an ancestor which is currently
14425       --  a private type, then it is not visible. The same applies if the
14426       --  component's containing type is not in an open scope and the original
14427       --  component's enclosing type is a visible full view of a private type
14428       --  (which can occur in cases where an attempt is being made to reference
14429       --  a component in a sibling package that is inherited from a visible
14430       --  component of a type in an ancestor package; the component in the
14431       --  sibling package should not be visible even though the component it
14432       --  inherited from is visible). This does not apply however in the case
14433       --  where the scope of the type is a private child unit, or when the
14434       --  parent comes from a local package in which the ancestor is currently
14435       --  visible. The latter suppression of visibility is needed for cases
14436       --  that are tested in B730006.
14437
14438       elsif Is_Private_Type (Original_Scope)
14439         or else
14440           (not Is_Private_Descendant (Type_Scope)
14441             and then not In_Open_Scopes (Type_Scope)
14442             and then Has_Private_Declaration (Original_Scope))
14443       then
14444          --  If the type derives from an entity in a formal package, there
14445          --  are no additional visible components.
14446
14447          if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
14448             N_Formal_Package_Declaration
14449          then
14450             return False;
14451
14452          --  if we are not in the private part of the current package, there
14453          --  are no additional visible components.
14454
14455          elsif Ekind (Scope (Current_Scope)) = E_Package
14456            and then not In_Private_Part (Scope (Current_Scope))
14457          then
14458             return False;
14459          else
14460             return
14461               Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
14462                 and then In_Open_Scopes (Scope (Original_Scope))
14463                 and then Is_Local_Type (Type_Scope);
14464          end if;
14465
14466       --  There is another weird way in which a component may be invisible
14467       --  when the private and the full view are not derived from the same
14468       --  ancestor. Here is an example :
14469
14470       --       type A1 is tagged      record F1 : integer; end record;
14471       --       type A2 is new A1 with record F2 : integer; end record;
14472       --       type T is new A1 with private;
14473       --     private
14474       --       type T is new A2 with null record;
14475
14476       --  In this case, the full view of T inherits F1 and F2 but the private
14477       --  view inherits only F1
14478
14479       else
14480          declare
14481             Ancestor : Entity_Id := Scope (C);
14482
14483          begin
14484             loop
14485                if Ancestor = Original_Scope then
14486                   return True;
14487                elsif Ancestor = Etype (Ancestor) then
14488                   return False;
14489                end if;
14490
14491                Ancestor := Etype (Ancestor);
14492             end loop;
14493          end;
14494       end if;
14495    end Is_Visible_Component;
14496
14497    --------------------------
14498    -- Make_Class_Wide_Type --
14499    --------------------------
14500
14501    procedure Make_Class_Wide_Type (T : Entity_Id) is
14502       CW_Type : Entity_Id;
14503       CW_Name : Name_Id;
14504       Next_E  : Entity_Id;
14505
14506    begin
14507       --  The class wide type can have been defined by the partial view, in
14508       --  which case everything is already done.
14509
14510       if Present (Class_Wide_Type (T)) then
14511          return;
14512       end if;
14513
14514       CW_Type :=
14515         New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
14516
14517       --  Inherit root type characteristics
14518
14519       CW_Name := Chars (CW_Type);
14520       Next_E  := Next_Entity (CW_Type);
14521       Copy_Node (T, CW_Type);
14522       Set_Comes_From_Source (CW_Type, False);
14523       Set_Chars (CW_Type, CW_Name);
14524       Set_Parent (CW_Type, Parent (T));
14525       Set_Next_Entity (CW_Type, Next_E);
14526
14527       --  Ensure we have a new freeze node for the class-wide type. The partial
14528       --  view may have freeze action of its own, requiring a proper freeze
14529       --  node, and the same freeze node cannot be shared between the two
14530       --  types.
14531
14532       Set_Has_Delayed_Freeze (CW_Type);
14533       Set_Freeze_Node (CW_Type, Empty);
14534
14535       --  Customize the class-wide type: It has no prim. op., it cannot be
14536       --  abstract and its Etype points back to the specific root type.
14537
14538       Set_Ekind                (CW_Type, E_Class_Wide_Type);
14539       Set_Is_Tagged_Type       (CW_Type, True);
14540       Set_Primitive_Operations (CW_Type, New_Elmt_List);
14541       Set_Is_Abstract_Type     (CW_Type, False);
14542       Set_Is_Constrained       (CW_Type, False);
14543       Set_Is_First_Subtype     (CW_Type, Is_First_Subtype (T));
14544
14545       if Ekind (T) = E_Class_Wide_Subtype then
14546          Set_Etype             (CW_Type, Etype (Base_Type (T)));
14547       else
14548          Set_Etype             (CW_Type, T);
14549       end if;
14550
14551       --  If this is the class_wide type of a constrained subtype, it does
14552       --  not have discriminants.
14553
14554       Set_Has_Discriminants (CW_Type,
14555         Has_Discriminants (T) and then not Is_Constrained (T));
14556
14557       Set_Has_Unknown_Discriminants (CW_Type, True);
14558       Set_Class_Wide_Type (T, CW_Type);
14559       Set_Equivalent_Type (CW_Type, Empty);
14560
14561       --  The class-wide type of a class-wide type is itself (RM 3.9(14))
14562
14563       Set_Class_Wide_Type (CW_Type, CW_Type);
14564    end Make_Class_Wide_Type;
14565
14566    ----------------
14567    -- Make_Index --
14568    ----------------
14569
14570    procedure Make_Index
14571      (I            : Node_Id;
14572       Related_Nod  : Node_Id;
14573       Related_Id   : Entity_Id := Empty;
14574       Suffix_Index : Nat := 1)
14575    is
14576       R      : Node_Id;
14577       T      : Entity_Id;
14578       Def_Id : Entity_Id := Empty;
14579       Found  : Boolean := False;
14580
14581    begin
14582       --  For a discrete range used in a constrained array definition and
14583       --  defined by a range, an implicit conversion to the predefined type
14584       --  INTEGER is assumed if each bound is either a numeric literal, a named
14585       --  number, or an attribute, and the type of both bounds (prior to the
14586       --  implicit conversion) is the type universal_integer. Otherwise, both
14587       --  bounds must be of the same discrete type, other than universal
14588       --  integer; this type must be determinable independently of the
14589       --  context, but using the fact that the type must be discrete and that
14590       --  both bounds must have the same type.
14591
14592       --  Character literals also have a universal type in the absence of
14593       --  of additional context,  and are resolved to Standard_Character.
14594
14595       if Nkind (I) = N_Range then
14596
14597          --  The index is given by a range constraint. The bounds are known
14598          --  to be of a consistent type.
14599
14600          if not Is_Overloaded (I) then
14601             T := Etype (I);
14602
14603             --  For universal bounds, choose the specific predefined type
14604
14605             if T = Universal_Integer then
14606                T := Standard_Integer;
14607
14608             elsif T = Any_Character then
14609                Ambiguous_Character (Low_Bound (I));
14610
14611                T := Standard_Character;
14612             end if;
14613
14614          --  The node may be overloaded because some user-defined operators
14615          --  are available, but if a universal interpretation exists it is
14616          --  also the selected one.
14617
14618          elsif Universal_Interpretation (I) = Universal_Integer then
14619             T := Standard_Integer;
14620
14621          else
14622             T := Any_Type;
14623
14624             declare
14625                Ind : Interp_Index;
14626                It  : Interp;
14627
14628             begin
14629                Get_First_Interp (I, Ind, It);
14630                while Present (It.Typ) loop
14631                   if Is_Discrete_Type (It.Typ) then
14632
14633                      if Found
14634                        and then not Covers (It.Typ, T)
14635                        and then not Covers (T, It.Typ)
14636                      then
14637                         Error_Msg_N ("ambiguous bounds in discrete range", I);
14638                         exit;
14639                      else
14640                         T := It.Typ;
14641                         Found := True;
14642                      end if;
14643                   end if;
14644
14645                   Get_Next_Interp (Ind, It);
14646                end loop;
14647
14648                if T = Any_Type then
14649                   Error_Msg_N ("discrete type required for range", I);
14650                   Set_Etype (I, Any_Type);
14651                   return;
14652
14653                elsif T = Universal_Integer then
14654                   T := Standard_Integer;
14655                end if;
14656             end;
14657          end if;
14658
14659          if not Is_Discrete_Type (T) then
14660             Error_Msg_N ("discrete type required for range", I);
14661             Set_Etype (I, Any_Type);
14662             return;
14663          end if;
14664
14665          if Nkind (Low_Bound (I)) = N_Attribute_Reference
14666            and then Attribute_Name (Low_Bound (I)) = Name_First
14667            and then Is_Entity_Name (Prefix (Low_Bound (I)))
14668            and then Is_Type (Entity (Prefix (Low_Bound (I))))
14669            and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
14670          then
14671             --  The type of the index will be the type of the prefix, as long
14672             --  as the upper bound is 'Last of the same type.
14673
14674             Def_Id := Entity (Prefix (Low_Bound (I)));
14675
14676             if Nkind (High_Bound (I)) /= N_Attribute_Reference
14677               or else Attribute_Name (High_Bound (I)) /= Name_Last
14678               or else not Is_Entity_Name (Prefix (High_Bound (I)))
14679               or else Entity (Prefix (High_Bound (I))) /= Def_Id
14680             then
14681                Def_Id := Empty;
14682             end if;
14683          end if;
14684
14685          R := I;
14686          Process_Range_Expr_In_Decl (R, T);
14687
14688       elsif Nkind (I) = N_Subtype_Indication then
14689
14690          --  The index is given by a subtype with a range constraint
14691
14692          T :=  Base_Type (Entity (Subtype_Mark (I)));
14693
14694          if not Is_Discrete_Type (T) then
14695             Error_Msg_N ("discrete type required for range", I);
14696             Set_Etype (I, Any_Type);
14697             return;
14698          end if;
14699
14700          R := Range_Expression (Constraint (I));
14701
14702          Resolve (R, T);
14703          Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
14704
14705       elsif Nkind (I) = N_Attribute_Reference then
14706
14707          --  The parser guarantees that the attribute is a RANGE attribute
14708
14709          --  If the node denotes the range of a type mark, that is also the
14710          --  resulting type, and we do no need to create an Itype for it.
14711
14712          if Is_Entity_Name (Prefix (I))
14713            and then Comes_From_Source (I)
14714            and then Is_Type (Entity (Prefix (I)))
14715            and then Is_Discrete_Type (Entity (Prefix (I)))
14716          then
14717             Def_Id := Entity (Prefix (I));
14718          end if;
14719
14720          Analyze_And_Resolve (I);
14721          T := Etype (I);
14722          R := I;
14723
14724       --  If none of the above, must be a subtype. We convert this to a
14725       --  range attribute reference because in the case of declared first
14726       --  named subtypes, the types in the range reference can be different
14727       --  from the type of the entity. A range attribute normalizes the
14728       --  reference and obtains the correct types for the bounds.
14729
14730       --  This transformation is in the nature of an expansion, is only
14731       --  done if expansion is active. In particular, it is not done on
14732       --  formal generic types,  because we need to retain the name of the
14733       --  original index for instantiation purposes.
14734
14735       else
14736          if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
14737             Error_Msg_N ("invalid subtype mark in discrete range ", I);
14738             Set_Etype (I, Any_Integer);
14739             return;
14740
14741          else
14742             --  The type mark may be that of an incomplete type. It is only
14743             --  now that we can get the full view, previous analysis does
14744             --  not look specifically for a type mark.
14745
14746             Set_Entity (I, Get_Full_View (Entity (I)));
14747             Set_Etype  (I, Entity (I));
14748             Def_Id := Entity (I);
14749
14750             if not Is_Discrete_Type (Def_Id) then
14751                Error_Msg_N ("discrete type required for index", I);
14752                Set_Etype (I, Any_Type);
14753                return;
14754             end if;
14755          end if;
14756
14757          if Expander_Active then
14758             Rewrite (I,
14759               Make_Attribute_Reference (Sloc (I),
14760                 Attribute_Name => Name_Range,
14761                 Prefix         => Relocate_Node (I)));
14762
14763             --  The original was a subtype mark that does not freeze. This
14764             --  means that the rewritten version must not freeze either.
14765
14766             Set_Must_Not_Freeze (I);
14767             Set_Must_Not_Freeze (Prefix (I));
14768
14769             --  Is order critical??? if so, document why, if not
14770             --  use Analyze_And_Resolve
14771
14772             Analyze_And_Resolve (I);
14773             T := Etype (I);
14774             R := I;
14775
14776          --  If expander is inactive, type is legal, nothing else to construct
14777
14778          else
14779             return;
14780          end if;
14781       end if;
14782
14783       if not Is_Discrete_Type (T) then
14784          Error_Msg_N ("discrete type required for range", I);
14785          Set_Etype (I, Any_Type);
14786          return;
14787
14788       elsif T = Any_Type then
14789          Set_Etype (I, Any_Type);
14790          return;
14791       end if;
14792
14793       --  We will now create the appropriate Itype to describe the range, but
14794       --  first a check. If we originally had a subtype, then we just label
14795       --  the range with this subtype. Not only is there no need to construct
14796       --  a new subtype, but it is wrong to do so for two reasons:
14797
14798       --    1. A legality concern, if we have a subtype, it must not freeze,
14799       --       and the Itype would cause freezing incorrectly
14800
14801       --    2. An efficiency concern, if we created an Itype, it would not be
14802       --       recognized as the same type for the purposes of eliminating
14803       --       checks in some circumstances.
14804
14805       --  We signal this case by setting the subtype entity in Def_Id
14806
14807       if No (Def_Id) then
14808          Def_Id :=
14809            Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
14810          Set_Etype (Def_Id, Base_Type (T));
14811
14812          if Is_Signed_Integer_Type (T) then
14813             Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14814
14815          elsif Is_Modular_Integer_Type (T) then
14816             Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14817
14818          else
14819             Set_Ekind             (Def_Id, E_Enumeration_Subtype);
14820             Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14821             Set_First_Literal     (Def_Id, First_Literal (T));
14822          end if;
14823
14824          Set_Size_Info      (Def_Id,                  (T));
14825          Set_RM_Size        (Def_Id, RM_Size          (T));
14826          Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
14827
14828          Set_Scalar_Range   (Def_Id, R);
14829          Conditional_Delay  (Def_Id, T);
14830
14831          --  In the subtype indication case, if the immediate parent of the
14832          --  new subtype is non-static, then the subtype we create is non-
14833          --  static, even if its bounds are static.
14834
14835          if Nkind (I) = N_Subtype_Indication
14836            and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
14837          then
14838             Set_Is_Non_Static_Subtype (Def_Id);
14839          end if;
14840       end if;
14841
14842       --  Final step is to label the index with this constructed type
14843
14844       Set_Etype (I, Def_Id);
14845    end Make_Index;
14846
14847    ------------------------------
14848    -- Modular_Type_Declaration --
14849    ------------------------------
14850
14851    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14852       Mod_Expr : constant Node_Id := Expression (Def);
14853       M_Val    : Uint;
14854
14855       procedure Set_Modular_Size (Bits : Int);
14856       --  Sets RM_Size to Bits, and Esize to normal word size above this
14857
14858       ----------------------
14859       -- Set_Modular_Size --
14860       ----------------------
14861
14862       procedure Set_Modular_Size (Bits : Int) is
14863       begin
14864          Set_RM_Size (T, UI_From_Int (Bits));
14865
14866          if Bits <= 8 then
14867             Init_Esize (T, 8);
14868
14869          elsif Bits <= 16 then
14870             Init_Esize (T, 16);
14871
14872          elsif Bits <= 32 then
14873             Init_Esize (T, 32);
14874
14875          else
14876             Init_Esize (T, System_Max_Binary_Modulus_Power);
14877          end if;
14878       end Set_Modular_Size;
14879
14880    --  Start of processing for Modular_Type_Declaration
14881
14882    begin
14883       Analyze_And_Resolve (Mod_Expr, Any_Integer);
14884       Set_Etype (T, T);
14885       Set_Ekind (T, E_Modular_Integer_Type);
14886       Init_Alignment (T);
14887       Set_Is_Constrained (T);
14888
14889       if not Is_OK_Static_Expression (Mod_Expr) then
14890          Flag_Non_Static_Expr
14891            ("non-static expression used for modular type bound!", Mod_Expr);
14892          M_Val := 2 ** System_Max_Binary_Modulus_Power;
14893       else
14894          M_Val := Expr_Value (Mod_Expr);
14895       end if;
14896
14897       if M_Val < 1 then
14898          Error_Msg_N ("modulus value must be positive", Mod_Expr);
14899          M_Val := 2 ** System_Max_Binary_Modulus_Power;
14900       end if;
14901
14902       Set_Modulus (T, M_Val);
14903
14904       --   Create bounds for the modular type based on the modulus given in
14905       --   the type declaration and then analyze and resolve those bounds.
14906
14907       Set_Scalar_Range (T,
14908         Make_Range (Sloc (Mod_Expr),
14909           Low_Bound  =>
14910             Make_Integer_Literal (Sloc (Mod_Expr), 0),
14911           High_Bound =>
14912             Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
14913
14914       --  Properly analyze the literals for the range. We do this manually
14915       --  because we can't go calling Resolve, since we are resolving these
14916       --  bounds with the type, and this type is certainly not complete yet!
14917
14918       Set_Etype (Low_Bound  (Scalar_Range (T)), T);
14919       Set_Etype (High_Bound (Scalar_Range (T)), T);
14920       Set_Is_Static_Expression (Low_Bound  (Scalar_Range (T)));
14921       Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
14922
14923       --  Loop through powers of two to find number of bits required
14924
14925       for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
14926
14927          --  Binary case
14928
14929          if M_Val = 2 ** Bits then
14930             Set_Modular_Size (Bits);
14931             return;
14932
14933          --  Non-binary case
14934
14935          elsif M_Val < 2 ** Bits then
14936             Set_Non_Binary_Modulus (T);
14937
14938             if Bits > System_Max_Nonbinary_Modulus_Power then
14939                Error_Msg_Uint_1 :=
14940                  UI_From_Int (System_Max_Nonbinary_Modulus_Power);
14941                Error_Msg_F
14942                  ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
14943                Set_Modular_Size (System_Max_Binary_Modulus_Power);
14944                return;
14945
14946             else
14947                --  In the non-binary case, set size as per RM 13.3(55)
14948
14949                Set_Modular_Size (Bits);
14950                return;
14951             end if;
14952          end if;
14953
14954       end loop;
14955
14956       --  If we fall through, then the size exceed System.Max_Binary_Modulus
14957       --  so we just signal an error and set the maximum size.
14958
14959       Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
14960       Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
14961
14962       Set_Modular_Size (System_Max_Binary_Modulus_Power);
14963       Init_Alignment (T);
14964
14965    end Modular_Type_Declaration;
14966
14967    --------------------------
14968    -- New_Concatenation_Op --
14969    --------------------------
14970
14971    procedure New_Concatenation_Op (Typ : Entity_Id) is
14972       Loc : constant Source_Ptr := Sloc (Typ);
14973       Op  : Entity_Id;
14974
14975       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
14976       --  Create abbreviated declaration for the formal of a predefined
14977       --  Operator 'Op' of type 'Typ'
14978
14979       --------------------
14980       -- Make_Op_Formal --
14981       --------------------
14982
14983       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
14984          Formal : Entity_Id;
14985       begin
14986          Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
14987          Set_Etype (Formal, Typ);
14988          Set_Mechanism (Formal, Default_Mechanism);
14989          return Formal;
14990       end Make_Op_Formal;
14991
14992    --  Start of processing for New_Concatenation_Op
14993
14994    begin
14995       Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
14996
14997       Set_Ekind                   (Op, E_Operator);
14998       Set_Scope                   (Op, Current_Scope);
14999       Set_Etype                   (Op, Typ);
15000       Set_Homonym                 (Op, Get_Name_Entity_Id (Name_Op_Concat));
15001       Set_Is_Immediately_Visible  (Op);
15002       Set_Is_Intrinsic_Subprogram (Op);
15003       Set_Has_Completion          (Op);
15004       Append_Entity               (Op, Current_Scope);
15005
15006       Set_Name_Entity_Id (Name_Op_Concat, Op);
15007
15008       Append_Entity (Make_Op_Formal (Typ, Op), Op);
15009       Append_Entity (Make_Op_Formal (Typ, Op), Op);
15010    end New_Concatenation_Op;
15011
15012    -------------------------
15013    -- OK_For_Limited_Init --
15014    -------------------------
15015
15016    --  ???Check all calls of this, and compare the conditions under which it's
15017    --  called.
15018
15019    function OK_For_Limited_Init (Exp : Node_Id) return Boolean is
15020    begin
15021       return Ada_Version >= Ada_05
15022         and then not Debug_Flag_Dot_L
15023         and then OK_For_Limited_Init_In_05 (Exp);
15024    end OK_For_Limited_Init;
15025
15026    -------------------------------
15027    -- OK_For_Limited_Init_In_05 --
15028    -------------------------------
15029
15030    function OK_For_Limited_Init_In_05 (Exp : Node_Id) return Boolean is
15031    begin
15032       --  Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
15033       --  case of limited aggregates (including extension aggregates), and
15034       --  function calls. The function call may have been give in prefixed
15035       --  notation, in which case the original node is an indexed component.
15036
15037       case Nkind (Original_Node (Exp)) is
15038          when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
15039             return True;
15040
15041          when N_Qualified_Expression =>
15042             return
15043               OK_For_Limited_Init_In_05 (Expression (Original_Node (Exp)));
15044
15045          --  Ada 2005 (AI-251): If a class-wide interface object is initialized
15046          --  with a function call, the expander has rewritten the call into an
15047          --  N_Type_Conversion node to force displacement of the pointer to
15048          --  reference the component containing the secondary dispatch table.
15049          --  Otherwise a type conversion is not a legal context.
15050
15051          when N_Type_Conversion =>
15052             return not Comes_From_Source (Exp)
15053               and then
15054                 OK_For_Limited_Init_In_05 (Expression (Original_Node (Exp)));
15055
15056          when N_Indexed_Component | N_Selected_Component  =>
15057             return Nkind (Exp) = N_Function_Call;
15058
15059          --  A use of 'Input is a function call, hence allowed. Normally the
15060          --  attribute will be changed to a call, but the attribute by itself
15061          --  can occur with -gnatc.
15062
15063          when N_Attribute_Reference =>
15064             return Attribute_Name (Original_Node (Exp)) = Name_Input;
15065
15066          when others =>
15067             return False;
15068       end case;
15069    end OK_For_Limited_Init_In_05;
15070
15071    -------------------------------------------
15072    -- Ordinary_Fixed_Point_Type_Declaration --
15073    -------------------------------------------
15074
15075    procedure Ordinary_Fixed_Point_Type_Declaration
15076      (T   : Entity_Id;
15077       Def : Node_Id)
15078    is
15079       Loc           : constant Source_Ptr := Sloc (Def);
15080       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
15081       RRS           : constant Node_Id    := Real_Range_Specification (Def);
15082       Implicit_Base : Entity_Id;
15083       Delta_Val     : Ureal;
15084       Small_Val     : Ureal;
15085       Low_Val       : Ureal;
15086       High_Val      : Ureal;
15087
15088    begin
15089       Check_Restriction (No_Fixed_Point, Def);
15090
15091       --  Create implicit base type
15092
15093       Implicit_Base :=
15094         Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
15095       Set_Etype (Implicit_Base, Implicit_Base);
15096
15097       --  Analyze and process delta expression
15098
15099       Analyze_And_Resolve (Delta_Expr, Any_Real);
15100
15101       Check_Delta_Expression (Delta_Expr);
15102       Delta_Val := Expr_Value_R (Delta_Expr);
15103
15104       Set_Delta_Value (Implicit_Base, Delta_Val);
15105
15106       --  Compute default small from given delta, which is the largest power
15107       --  of two that does not exceed the given delta value.
15108
15109       declare
15110          Tmp   : Ureal;
15111          Scale : Int;
15112
15113       begin
15114          Tmp := Ureal_1;
15115          Scale := 0;
15116
15117          if Delta_Val < Ureal_1 then
15118             while Delta_Val < Tmp loop
15119                Tmp := Tmp / Ureal_2;
15120                Scale := Scale + 1;
15121             end loop;
15122
15123          else
15124             loop
15125                Tmp := Tmp * Ureal_2;
15126                exit when Tmp > Delta_Val;
15127                Scale := Scale - 1;
15128             end loop;
15129          end if;
15130
15131          Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
15132       end;
15133
15134       Set_Small_Value (Implicit_Base, Small_Val);
15135
15136       --  If no range was given, set a dummy range
15137
15138       if RRS <= Empty_Or_Error then
15139          Low_Val  := -Small_Val;
15140          High_Val := Small_Val;
15141
15142       --  Otherwise analyze and process given range
15143
15144       else
15145          declare
15146             Low  : constant Node_Id := Low_Bound  (RRS);
15147             High : constant Node_Id := High_Bound (RRS);
15148
15149          begin
15150             Analyze_And_Resolve (Low, Any_Real);
15151             Analyze_And_Resolve (High, Any_Real);
15152             Check_Real_Bound (Low);
15153             Check_Real_Bound (High);
15154
15155             --  Obtain and set the range
15156
15157             Low_Val  := Expr_Value_R (Low);
15158             High_Val := Expr_Value_R (High);
15159
15160             if Low_Val > High_Val then
15161                Error_Msg_NE ("?fixed point type& has null range", Def, T);
15162             end if;
15163          end;
15164       end if;
15165
15166       --  The range for both the implicit base and the declared first subtype
15167       --  cannot be set yet, so we use the special routine Set_Fixed_Range to
15168       --  set a temporary range in place. Note that the bounds of the base
15169       --  type will be widened to be symmetrical and to fill the available
15170       --  bits when the type is frozen.
15171
15172       --  We could do this with all discrete types, and probably should, but
15173       --  we absolutely have to do it for fixed-point, since the end-points
15174       --  of the range and the size are determined by the small value, which
15175       --  could be reset before the freeze point.
15176
15177       Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
15178       Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15179
15180       --  Complete definition of first subtype
15181
15182       Set_Ekind          (T, E_Ordinary_Fixed_Point_Subtype);
15183       Set_Etype          (T, Implicit_Base);
15184       Init_Size_Align    (T);
15185       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15186       Set_Small_Value    (T, Small_Val);
15187       Set_Delta_Value    (T, Delta_Val);
15188       Set_Is_Constrained (T);
15189
15190    end Ordinary_Fixed_Point_Type_Declaration;
15191
15192    ----------------------------------------
15193    -- Prepare_Private_Subtype_Completion --
15194    ----------------------------------------
15195
15196    procedure Prepare_Private_Subtype_Completion
15197      (Id          : Entity_Id;
15198       Related_Nod : Node_Id)
15199    is
15200       Id_B   : constant Entity_Id := Base_Type (Id);
15201       Full_B : constant Entity_Id := Full_View (Id_B);
15202       Full   : Entity_Id;
15203
15204    begin
15205       if Present (Full_B) then
15206
15207          --  The Base_Type is already completed, we can complete the subtype
15208          --  now. We have to create a new entity with the same name, Thus we
15209          --  can't use Create_Itype.
15210
15211          --  This is messy, should be fixed ???
15212
15213          Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
15214          Set_Is_Itype (Full);
15215          Set_Associated_Node_For_Itype (Full, Related_Nod);
15216          Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
15217       end if;
15218
15219       --  The parent subtype may be private, but the base might not, in some
15220       --  nested instances. In that case, the subtype does not need to be
15221       --  exchanged. It would still be nice to make private subtypes and their
15222       --  bases consistent at all times ???
15223
15224       if Is_Private_Type (Id_B) then
15225          Append_Elmt (Id, Private_Dependents (Id_B));
15226       end if;
15227
15228    end Prepare_Private_Subtype_Completion;
15229
15230    ---------------------------
15231    -- Process_Discriminants --
15232    ---------------------------
15233
15234    procedure Process_Discriminants
15235      (N    : Node_Id;
15236       Prev : Entity_Id := Empty)
15237    is
15238       Elist               : constant Elist_Id := New_Elmt_List;
15239       Id                  : Node_Id;
15240       Discr               : Node_Id;
15241       Discr_Number        : Uint;
15242       Discr_Type          : Entity_Id;
15243       Default_Present     : Boolean := False;
15244       Default_Not_Present : Boolean := False;
15245
15246    begin
15247       --  A composite type other than an array type can have discriminants.
15248       --  On entry, the current scope is the composite type.
15249
15250       --  The discriminants are initially entered into the scope of the type
15251       --  via Enter_Name with the default Ekind of E_Void to prevent premature
15252       --  use, as explained at the end of this procedure.
15253
15254       Discr := First (Discriminant_Specifications (N));
15255       while Present (Discr) loop
15256          Enter_Name (Defining_Identifier (Discr));
15257
15258          --  For navigation purposes we add a reference to the discriminant
15259          --  in the entity for the type. If the current declaration is a
15260          --  completion, place references on the partial view. Otherwise the
15261          --  type is the current scope.
15262
15263          if Present (Prev) then
15264
15265             --  The references go on the partial view, if present. If the
15266             --  partial view has discriminants, the references have been
15267             --  generated already.
15268
15269             if not Has_Discriminants (Prev) then
15270                Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
15271             end if;
15272          else
15273             Generate_Reference
15274               (Current_Scope, Defining_Identifier (Discr), 'd');
15275          end if;
15276
15277          if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
15278             Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
15279
15280             --  Ada 2005 (AI-254)
15281
15282             if Present (Access_To_Subprogram_Definition
15283                          (Discriminant_Type (Discr)))
15284               and then Protected_Present (Access_To_Subprogram_Definition
15285                                            (Discriminant_Type (Discr)))
15286             then
15287                Discr_Type :=
15288                  Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
15289             end if;
15290
15291          else
15292             Find_Type (Discriminant_Type (Discr));
15293             Discr_Type := Etype (Discriminant_Type (Discr));
15294
15295             if Error_Posted (Discriminant_Type (Discr)) then
15296                Discr_Type := Any_Type;
15297             end if;
15298          end if;
15299
15300          if Is_Access_Type (Discr_Type) then
15301
15302             --  Ada 2005 (AI-230): Access discriminant allowed in non-limited
15303             --  record types
15304
15305             if Ada_Version < Ada_05 then
15306                Check_Access_Discriminant_Requires_Limited
15307                  (Discr, Discriminant_Type (Discr));
15308             end if;
15309
15310             if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
15311                Error_Msg_N
15312                  ("(Ada 83) access discriminant not allowed", Discr);
15313             end if;
15314
15315          elsif not Is_Discrete_Type (Discr_Type) then
15316             Error_Msg_N ("discriminants must have a discrete or access type",
15317               Discriminant_Type (Discr));
15318          end if;
15319
15320          Set_Etype (Defining_Identifier (Discr), Discr_Type);
15321
15322          --  If a discriminant specification includes the assignment compound
15323          --  delimiter followed by an expression, the expression is the default
15324          --  expression of the discriminant; the default expression must be of
15325          --  the type of the discriminant. (RM 3.7.1) Since this expression is
15326          --  a default expression, we do the special preanalysis, since this
15327          --  expression does not freeze (see "Handling of Default and Per-
15328          --  Object Expressions" in spec of package Sem).
15329
15330          if Present (Expression (Discr)) then
15331             Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
15332
15333             if Nkind (N) = N_Formal_Type_Declaration then
15334                Error_Msg_N
15335                  ("discriminant defaults not allowed for formal type",
15336                   Expression (Discr));
15337
15338             --  Tagged types cannot have defaulted discriminants, but a
15339             --  non-tagged private type with defaulted discriminants
15340             --   can have a tagged completion.
15341
15342             elsif Is_Tagged_Type (Current_Scope)
15343               and then Comes_From_Source (N)
15344             then
15345                Error_Msg_N
15346                  ("discriminants of tagged type cannot have defaults",
15347                   Expression (Discr));
15348
15349             else
15350                Default_Present := True;
15351                Append_Elmt (Expression (Discr), Elist);
15352
15353                --  Tag the defining identifiers for the discriminants with
15354                --  their corresponding default expressions from the tree.
15355
15356                Set_Discriminant_Default_Value
15357                  (Defining_Identifier (Discr), Expression (Discr));
15358             end if;
15359
15360          else
15361             Default_Not_Present := True;
15362          end if;
15363
15364          --  Ada 2005 (AI-231): Create an Itype that is a duplicate of
15365          --  Discr_Type but with the null-exclusion attribute
15366
15367          if Ada_Version >= Ada_05 then
15368
15369             --  Ada 2005 (AI-231): Static checks
15370
15371             if Can_Never_Be_Null (Discr_Type) then
15372                Null_Exclusion_Static_Checks (Discr);
15373
15374             elsif Is_Access_Type (Discr_Type)
15375               and then Null_Exclusion_Present (Discr)
15376
15377                --  No need to check itypes because in their case this check
15378                --  was done at their point of creation
15379
15380               and then not Is_Itype (Discr_Type)
15381             then
15382                if Can_Never_Be_Null (Discr_Type) then
15383                   Error_Msg_NE
15384                     ("`NOT NULL` not allowed (& already excludes null)",
15385                      Discr,
15386                      Discr_Type);
15387                end if;
15388
15389                Set_Etype (Defining_Identifier (Discr),
15390                  Create_Null_Excluding_Itype
15391                    (T           => Discr_Type,
15392                     Related_Nod => Discr));
15393
15394             --  Check for improper null exclusion if the type is otherwise
15395             --  legal for a discriminant.
15396
15397             elsif Null_Exclusion_Present (Discr)
15398               and then Is_Discrete_Type (Discr_Type)
15399             then
15400                Error_Msg_N
15401                  ("null exclusion can only apply to an access type", Discr);
15402             end if;
15403
15404             --  Ada 2005 (AI-402): access discriminants of nonlimited types
15405             --  can't have defaults. Synchronized types, or types that are
15406             --  explicitly limited are fine, but special tests apply to derived
15407             --  types in generics: in a generic body we have to assume the
15408             --  worst, and therefore defaults are not allowed if the parent is
15409             --  a generic formal private type (see ACATS B370001).
15410
15411             if Is_Access_Type (Discr_Type) then
15412                if Ekind (Discr_Type) /= E_Anonymous_Access_Type
15413                  or else not Default_Present
15414                  or else Is_Limited_Record (Current_Scope)
15415                  or else Is_Concurrent_Type (Current_Scope)
15416                  or else Is_Concurrent_Record_Type (Current_Scope)
15417                  or else Ekind (Current_Scope) = E_Limited_Private_Type
15418                then
15419                   if not Is_Derived_Type (Current_Scope)
15420                     or else not Is_Generic_Type (Etype (Current_Scope))
15421                     or else not In_Package_Body (Scope (Etype (Current_Scope)))
15422                     or else Limited_Present
15423                               (Type_Definition (Parent (Current_Scope)))
15424                   then
15425                      null;
15426
15427                   else
15428                      Error_Msg_N ("access discriminants of nonlimited types",
15429                          Expression (Discr));
15430                      Error_Msg_N ("\cannot have defaults", Expression (Discr));
15431                   end if;
15432
15433                elsif Present (Expression (Discr)) then
15434                   Error_Msg_N
15435                     ("(Ada 2005) access discriminants of nonlimited types",
15436                      Expression (Discr));
15437                   Error_Msg_N ("\cannot have defaults", Expression (Discr));
15438                end if;
15439             end if;
15440          end if;
15441
15442          Next (Discr);
15443       end loop;
15444
15445       --  An element list consisting of the default expressions of the
15446       --  discriminants is constructed in the above loop and used to set
15447       --  the Discriminant_Constraint attribute for the type. If an object
15448       --  is declared of this (record or task) type without any explicit
15449       --  discriminant constraint given, this element list will form the
15450       --  actual parameters for the corresponding initialization procedure
15451       --  for the type.
15452
15453       Set_Discriminant_Constraint (Current_Scope, Elist);
15454       Set_Stored_Constraint (Current_Scope, No_Elist);
15455
15456       --  Default expressions must be provided either for all or for none
15457       --  of the discriminants of a discriminant part. (RM 3.7.1)
15458
15459       if Default_Present and then Default_Not_Present then
15460          Error_Msg_N
15461            ("incomplete specification of defaults for discriminants", N);
15462       end if;
15463
15464       --  The use of the name of a discriminant is not allowed in default
15465       --  expressions of a discriminant part if the specification of the
15466       --  discriminant is itself given in the discriminant part. (RM 3.7.1)
15467
15468       --  To detect this, the discriminant names are entered initially with an
15469       --  Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
15470       --  attempt to use a void entity (for example in an expression that is
15471       --  type-checked) produces the error message: premature usage. Now after
15472       --  completing the semantic analysis of the discriminant part, we can set
15473       --  the Ekind of all the discriminants appropriately.
15474
15475       Discr := First (Discriminant_Specifications (N));
15476       Discr_Number := Uint_1;
15477       while Present (Discr) loop
15478          Id := Defining_Identifier (Discr);
15479          Set_Ekind (Id, E_Discriminant);
15480          Init_Component_Location (Id);
15481          Init_Esize (Id);
15482          Set_Discriminant_Number (Id, Discr_Number);
15483
15484          --  Make sure this is always set, even in illegal programs
15485
15486          Set_Corresponding_Discriminant (Id, Empty);
15487
15488          --  Initialize the Original_Record_Component to the entity itself.
15489          --  Inherit_Components will propagate the right value to
15490          --  discriminants in derived record types.
15491
15492          Set_Original_Record_Component (Id, Id);
15493
15494          --  Create the discriminal for the discriminant
15495
15496          Build_Discriminal (Id);
15497
15498          Next (Discr);
15499          Discr_Number := Discr_Number + 1;
15500       end loop;
15501
15502       Set_Has_Discriminants (Current_Scope);
15503    end Process_Discriminants;
15504
15505    -----------------------
15506    -- Process_Full_View --
15507    -----------------------
15508
15509    procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
15510       Priv_Parent : Entity_Id;
15511       Full_Parent : Entity_Id;
15512       Full_Indic  : Node_Id;
15513
15514       procedure Collect_Implemented_Interfaces
15515         (Typ    : Entity_Id;
15516          Ifaces : Elist_Id);
15517       --  Ada 2005: Gather all the interfaces that Typ directly or
15518       --  inherently implements. Duplicate entries are not added to
15519       --  the list Ifaces.
15520
15521       ------------------------------------
15522       -- Collect_Implemented_Interfaces --
15523       ------------------------------------
15524
15525       procedure Collect_Implemented_Interfaces
15526         (Typ    : Entity_Id;
15527          Ifaces : Elist_Id)
15528       is
15529          Iface      : Entity_Id;
15530          Iface_Elmt : Elmt_Id;
15531
15532       begin
15533          --  Abstract interfaces are only associated with tagged record types
15534
15535          if not Is_Tagged_Type (Typ)
15536            or else not Is_Record_Type (Typ)
15537          then
15538             return;
15539          end if;
15540
15541          --  Recursively climb to the ancestors
15542
15543          if Etype (Typ) /= Typ
15544
15545             --  Protect the frontend against wrong cyclic declarations like:
15546
15547             --     type B is new A with private;
15548             --     type C is new A with private;
15549             --  private
15550             --     type B is new C with null record;
15551             --     type C is new B with null record;
15552
15553            and then Etype (Typ) /= Priv_T
15554            and then Etype (Typ) /= Full_T
15555          then
15556             --  Keep separate the management of private type declarations
15557
15558             if Ekind (Typ) = E_Record_Type_With_Private then
15559
15560                --  Handle the following erronous case:
15561                --      type Private_Type is tagged private;
15562                --   private
15563                --      type Private_Type is new Type_Implementing_Iface;
15564
15565                if Present (Full_View (Typ))
15566                  and then Etype (Typ) /= Full_View (Typ)
15567                then
15568                   if Is_Interface (Etype (Typ)) then
15569                      Append_Unique_Elmt (Etype (Typ), Ifaces);
15570                   end if;
15571
15572                   Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15573                end if;
15574
15575             --  Non-private types
15576
15577             else
15578                if Is_Interface (Etype (Typ)) then
15579                   Append_Unique_Elmt (Etype (Typ), Ifaces);
15580                end if;
15581
15582                Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15583             end if;
15584          end if;
15585
15586          --  Handle entities in the list of abstract interfaces
15587
15588          if Present (Interfaces (Typ)) then
15589             Iface_Elmt := First_Elmt (Interfaces (Typ));
15590             while Present (Iface_Elmt) loop
15591                Iface := Node (Iface_Elmt);
15592
15593                pragma Assert (Is_Interface (Iface));
15594
15595                if not Contain_Interface (Iface, Ifaces) then
15596                   Append_Elmt (Iface, Ifaces);
15597                   Collect_Implemented_Interfaces (Iface, Ifaces);
15598                end if;
15599
15600                Next_Elmt (Iface_Elmt);
15601             end loop;
15602          end if;
15603       end Collect_Implemented_Interfaces;
15604
15605    --  Start of processing for Process_Full_View
15606
15607    begin
15608       --  First some sanity checks that must be done after semantic
15609       --  decoration of the full view and thus cannot be placed with other
15610       --  similar checks in Find_Type_Name
15611
15612       if not Is_Limited_Type (Priv_T)
15613         and then (Is_Limited_Type (Full_T)
15614                    or else Is_Limited_Composite (Full_T))
15615       then
15616          Error_Msg_N
15617            ("completion of nonlimited type cannot be limited", Full_T);
15618          Explain_Limited_Type (Full_T, Full_T);
15619
15620       elsif Is_Abstract_Type (Full_T)
15621         and then not Is_Abstract_Type (Priv_T)
15622       then
15623          Error_Msg_N
15624            ("completion of nonabstract type cannot be abstract", Full_T);
15625
15626       elsif Is_Tagged_Type (Priv_T)
15627         and then Is_Limited_Type (Priv_T)
15628         and then not Is_Limited_Type (Full_T)
15629       then
15630          --  If pragma CPP_Class was applied to the private declaration
15631          --  propagate the limitedness to the full-view
15632
15633          if Is_CPP_Class (Priv_T) then
15634             Set_Is_Limited_Record (Full_T);
15635
15636          --  GNAT allow its own definition of Limited_Controlled to disobey
15637          --  this rule in order in ease the implementation. The next test is
15638          --  safe because Root_Controlled is defined in a private system child
15639
15640          elsif Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
15641             Set_Is_Limited_Composite (Full_T);
15642          else
15643             Error_Msg_N
15644               ("completion of limited tagged type must be limited", Full_T);
15645          end if;
15646
15647       elsif Is_Generic_Type (Priv_T) then
15648          Error_Msg_N ("generic type cannot have a completion", Full_T);
15649       end if;
15650
15651       --  Check that ancestor interfaces of private and full views are
15652       --  consistent. We omit this check for synchronized types because
15653       --  they are performed on the corresponding record type when frozen.
15654
15655       if Ada_Version >= Ada_05
15656         and then Is_Tagged_Type (Priv_T)
15657         and then Is_Tagged_Type (Full_T)
15658         and then not Is_Concurrent_Type (Full_T)
15659       then
15660          declare
15661             Iface         : Entity_Id;
15662             Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
15663             Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
15664
15665          begin
15666             Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
15667             Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
15668
15669             --  Ada 2005 (AI-251): The partial view shall be a descendant of
15670             --  an interface type if and only if the full type is descendant
15671             --  of the interface type (AARM 7.3 (7.3/2).
15672
15673             Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
15674
15675             if Present (Iface) then
15676                Error_Msg_NE ("interface & not implemented by full type " &
15677                              "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
15678             end if;
15679
15680             Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
15681
15682             if Present (Iface) then
15683                Error_Msg_NE ("interface & not implemented by partial view " &
15684                              "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
15685             end if;
15686          end;
15687       end if;
15688
15689       if Is_Tagged_Type (Priv_T)
15690         and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15691         and then Is_Derived_Type (Full_T)
15692       then
15693          Priv_Parent := Etype (Priv_T);
15694
15695          --  The full view of a private extension may have been transformed
15696          --  into an unconstrained derived type declaration and a subtype
15697          --  declaration (see build_derived_record_type for details).
15698
15699          if Nkind (N) = N_Subtype_Declaration then
15700             Full_Indic  := Subtype_Indication (N);
15701             Full_Parent := Etype (Base_Type (Full_T));
15702          else
15703             Full_Indic  := Subtype_Indication (Type_Definition (N));
15704             Full_Parent := Etype (Full_T);
15705          end if;
15706
15707          --  Check that the parent type of the full type is a descendant of
15708          --  the ancestor subtype given in the private extension. If either
15709          --  entity has an Etype equal to Any_Type then we had some previous
15710          --  error situation [7.3(8)].
15711
15712          if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
15713             return;
15714
15715          --  Ada 2005 (AI-251): Interfaces in the full-typ can be given in
15716          --  any order. Therefore we don't have to check that its parent must
15717          --  be a descendant of the parent of the private type declaration.
15718
15719          elsif Is_Interface (Priv_Parent)
15720            and then Is_Interface (Full_Parent)
15721          then
15722             null;
15723
15724          --  Ada 2005 (AI-251): If the parent of the private type declaration
15725          --  is an interface there is no need to check that it is an ancestor
15726          --  of the associated full type declaration. The required tests for
15727          --  this case are performed by Build_Derived_Record_Type.
15728
15729          elsif not Is_Interface (Base_Type (Priv_Parent))
15730            and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
15731          then
15732             Error_Msg_N
15733               ("parent of full type must descend from parent"
15734                   & " of private extension", Full_Indic);
15735
15736          --  Check the rules of 7.3(10): if the private extension inherits
15737          --  known discriminants, then the full type must also inherit those
15738          --  discriminants from the same (ancestor) type, and the parent
15739          --  subtype of the full type must be constrained if and only if
15740          --  the ancestor subtype of the private extension is constrained.
15741
15742          elsif No (Discriminant_Specifications (Parent (Priv_T)))
15743            and then not Has_Unknown_Discriminants (Priv_T)
15744            and then Has_Discriminants (Base_Type (Priv_Parent))
15745          then
15746             declare
15747                Priv_Indic  : constant Node_Id :=
15748                                Subtype_Indication (Parent (Priv_T));
15749
15750                Priv_Constr : constant Boolean :=
15751                                Is_Constrained (Priv_Parent)
15752                                  or else
15753                                    Nkind (Priv_Indic) = N_Subtype_Indication
15754                                  or else Is_Constrained (Entity (Priv_Indic));
15755
15756                Full_Constr : constant Boolean :=
15757                                Is_Constrained (Full_Parent)
15758                                  or else
15759                                    Nkind (Full_Indic) = N_Subtype_Indication
15760                                  or else Is_Constrained (Entity (Full_Indic));
15761
15762                Priv_Discr : Entity_Id;
15763                Full_Discr : Entity_Id;
15764
15765             begin
15766                Priv_Discr := First_Discriminant (Priv_Parent);
15767                Full_Discr := First_Discriminant (Full_Parent);
15768                while Present (Priv_Discr) and then Present (Full_Discr) loop
15769                   if Original_Record_Component (Priv_Discr) =
15770                      Original_Record_Component (Full_Discr)
15771                     or else
15772                      Corresponding_Discriminant (Priv_Discr) =
15773                      Corresponding_Discriminant (Full_Discr)
15774                   then
15775                      null;
15776                   else
15777                      exit;
15778                   end if;
15779
15780                   Next_Discriminant (Priv_Discr);
15781                   Next_Discriminant (Full_Discr);
15782                end loop;
15783
15784                if Present (Priv_Discr) or else Present (Full_Discr) then
15785                   Error_Msg_N
15786                     ("full view must inherit discriminants of the parent type"
15787                      & " used in the private extension", Full_Indic);
15788
15789                elsif Priv_Constr and then not Full_Constr then
15790                   Error_Msg_N
15791                     ("parent subtype of full type must be constrained",
15792                      Full_Indic);
15793
15794                elsif Full_Constr and then not Priv_Constr then
15795                   Error_Msg_N
15796                     ("parent subtype of full type must be unconstrained",
15797                      Full_Indic);
15798                end if;
15799             end;
15800
15801          --  Check the rules of 7.3(12): if a partial view has neither known
15802          --  or unknown discriminants, then the full type declaration shall
15803          --  define a definite subtype.
15804
15805          elsif      not Has_Unknown_Discriminants (Priv_T)
15806            and then not Has_Discriminants (Priv_T)
15807            and then not Is_Constrained (Full_T)
15808          then
15809             Error_Msg_N
15810               ("full view must define a constrained type if partial view"
15811                 & " has no discriminants", Full_T);
15812          end if;
15813
15814          --  ??????? Do we implement the following properly ?????
15815          --  If the ancestor subtype of a private extension has constrained
15816          --  discriminants, then the parent subtype of the full view shall
15817          --  impose a statically matching constraint on those discriminants
15818          --  [7.3(13)].
15819
15820       else
15821          --  For untagged types, verify that a type without discriminants
15822          --  is not completed with an unconstrained type.
15823
15824          if not Is_Indefinite_Subtype (Priv_T)
15825            and then Is_Indefinite_Subtype (Full_T)
15826          then
15827             Error_Msg_N ("full view of type must be definite subtype", Full_T);
15828          end if;
15829       end if;
15830
15831       --  AI-419: verify that the use of "limited" is consistent
15832
15833       declare
15834          Orig_Decl : constant Node_Id := Original_Node (N);
15835
15836       begin
15837          if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15838            and then not Limited_Present (Parent (Priv_T))
15839            and then not Synchronized_Present (Parent (Priv_T))
15840            and then Nkind (Orig_Decl) = N_Full_Type_Declaration
15841            and then Nkind
15842              (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
15843            and then Limited_Present (Type_Definition (Orig_Decl))
15844          then
15845             Error_Msg_N
15846               ("full view of non-limited extension cannot be limited", N);
15847          end if;
15848       end;
15849
15850       --  Ada 2005 (AI-443): A synchronized private extension must be
15851       --  completed by a task or protected type.
15852
15853       if Ada_Version >= Ada_05
15854         and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15855         and then Synchronized_Present (Parent (Priv_T))
15856         and then not Is_Concurrent_Type (Full_T)
15857       then
15858          Error_Msg_N ("full view of synchronized extension must " &
15859                       "be synchronized type", N);
15860       end if;
15861
15862       --  Ada 2005 AI-363: if the full view has discriminants with
15863       --  defaults, it is illegal to declare constrained access subtypes
15864       --  whose designated type is the current type. This allows objects
15865       --  of the type that are declared in the heap to be unconstrained.
15866
15867       if not Has_Unknown_Discriminants (Priv_T)
15868         and then not Has_Discriminants (Priv_T)
15869         and then Has_Discriminants (Full_T)
15870         and then
15871           Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
15872       then
15873          Set_Has_Constrained_Partial_View (Full_T);
15874          Set_Has_Constrained_Partial_View (Priv_T);
15875       end if;
15876
15877       --  Create a full declaration for all its subtypes recorded in
15878       --  Private_Dependents and swap them similarly to the base type. These
15879       --  are subtypes that have been define before the full declaration of
15880       --  the private type. We also swap the entry in Private_Dependents list
15881       --  so we can properly restore the private view on exit from the scope.
15882
15883       declare
15884          Priv_Elmt : Elmt_Id;
15885          Priv      : Entity_Id;
15886          Full      : Entity_Id;
15887
15888       begin
15889          Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
15890          while Present (Priv_Elmt) loop
15891             Priv := Node (Priv_Elmt);
15892
15893             if Ekind (Priv) = E_Private_Subtype
15894               or else Ekind (Priv) = E_Limited_Private_Subtype
15895               or else Ekind (Priv) = E_Record_Subtype_With_Private
15896             then
15897                Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
15898                Set_Is_Itype (Full);
15899                Set_Parent (Full, Parent (Priv));
15900                Set_Associated_Node_For_Itype (Full, N);
15901
15902                --  Now we need to complete the private subtype, but since the
15903                --  base type has already been swapped, we must also swap the
15904                --  subtypes (and thus, reverse the arguments in the call to
15905                --  Complete_Private_Subtype).
15906
15907                Copy_And_Swap (Priv, Full);
15908                Complete_Private_Subtype (Full, Priv, Full_T, N);
15909                Replace_Elmt (Priv_Elmt, Full);
15910             end if;
15911
15912             Next_Elmt (Priv_Elmt);
15913          end loop;
15914       end;
15915
15916       --  If the private view was tagged, copy the new primitive operations
15917       --  from the private view to the full view.
15918
15919       if Is_Tagged_Type (Full_T) then
15920          declare
15921             Disp_Typ  : Entity_Id;
15922             Full_List : Elist_Id;
15923             Prim      : Entity_Id;
15924             Prim_Elmt : Elmt_Id;
15925             Priv_List : Elist_Id;
15926
15927             function Contains
15928               (E : Entity_Id;
15929                L : Elist_Id) return Boolean;
15930             --  Determine whether list L contains element E
15931
15932             --------------
15933             -- Contains --
15934             --------------
15935
15936             function Contains
15937               (E : Entity_Id;
15938                L : Elist_Id) return Boolean
15939             is
15940                List_Elmt : Elmt_Id;
15941
15942             begin
15943                List_Elmt := First_Elmt (L);
15944                while Present (List_Elmt) loop
15945                   if Node (List_Elmt) = E then
15946                      return True;
15947                   end if;
15948
15949                   Next_Elmt (List_Elmt);
15950                end loop;
15951
15952                return False;
15953             end Contains;
15954
15955          --  Start of processing
15956
15957          begin
15958             if Is_Tagged_Type (Priv_T) then
15959                Priv_List := Primitive_Operations (Priv_T);
15960                Prim_Elmt := First_Elmt (Priv_List);
15961
15962                --  In the case of a concurrent type completing a private tagged
15963                --  type, primitives may have been declared in between the two
15964                --  views. These subprograms need to be wrapped the same way
15965                --  entries and protected procedures are handled because they
15966                --  cannot be directly shared by the two views.
15967
15968                if Is_Concurrent_Type (Full_T) then
15969                   declare
15970                      Conc_Typ  : constant Entity_Id :=
15971                                    Corresponding_Record_Type (Full_T);
15972                      Loc       : constant Source_Ptr := Sloc (Conc_Typ);
15973                      Curr_Nod  : Node_Id := Parent (Conc_Typ);
15974                      Wrap_Spec : Node_Id;
15975
15976                   begin
15977                      while Present (Prim_Elmt) loop
15978                         Prim := Node (Prim_Elmt);
15979
15980                         if Comes_From_Source (Prim)
15981                           and then not Is_Abstract_Subprogram (Prim)
15982                         then
15983                            Wrap_Spec :=
15984                              Make_Subprogram_Declaration (Loc,
15985                                Specification =>
15986                                  Build_Wrapper_Spec (Loc,
15987                                    Subp_Id => Prim,
15988                                    Obj_Typ => Conc_Typ,
15989                                    Formals =>
15990                                      Parameter_Specifications (
15991                                        Parent (Prim))));
15992
15993                            Insert_After (Curr_Nod, Wrap_Spec);
15994                            Curr_Nod := Wrap_Spec;
15995
15996                            Analyze (Wrap_Spec);
15997                         end if;
15998
15999                         Next_Elmt (Prim_Elmt);
16000                      end loop;
16001
16002                      return;
16003                   end;
16004
16005                --  For non-concurrent types, transfer explicit primitives, but
16006                --  omit those inherited from the parent of the private view
16007                --  since they will be re-inherited later on.
16008
16009                else
16010                   Full_List := Primitive_Operations (Full_T);
16011
16012                   while Present (Prim_Elmt) loop
16013                      Prim := Node (Prim_Elmt);
16014
16015                      if Comes_From_Source (Prim)
16016                        and then not Contains (Prim, Full_List)
16017                      then
16018                         Append_Elmt (Prim, Full_List);
16019                      end if;
16020
16021                      Next_Elmt (Prim_Elmt);
16022                   end loop;
16023                end if;
16024
16025             --  Untagged private view
16026
16027             else
16028                Full_List := Primitive_Operations (Full_T);
16029
16030                --  In this case the partial view is untagged, so here we locate
16031                --  all of the earlier primitives that need to be treated as
16032                --  dispatching (those that appear between the two views). Note
16033                --  that these additional operations must all be new operations
16034                --  (any earlier operations that override inherited operations
16035                --  of the full view will already have been inserted in the
16036                --  primitives list, marked by Check_Operation_From_Private_View
16037                --  as dispatching. Note that implicit "/=" operators are
16038                --  excluded from being added to the primitives list since they
16039                --  shouldn't be treated as dispatching (tagged "/=" is handled
16040                --  specially).
16041
16042                Prim := Next_Entity (Full_T);
16043                while Present (Prim) and then Prim /= Priv_T loop
16044                   if Ekind (Prim) = E_Procedure
16045                        or else
16046                      Ekind (Prim) = E_Function
16047                   then
16048                      Disp_Typ := Find_Dispatching_Type (Prim);
16049
16050                      if Disp_Typ = Full_T
16051                        and then (Chars (Prim) /= Name_Op_Ne
16052                                   or else Comes_From_Source (Prim))
16053                      then
16054                         Check_Controlling_Formals (Full_T, Prim);
16055
16056                         if not Is_Dispatching_Operation (Prim) then
16057                            Append_Elmt (Prim, Full_List);
16058                            Set_Is_Dispatching_Operation (Prim, True);
16059                            Set_DT_Position (Prim, No_Uint);
16060                         end if;
16061
16062                      elsif Is_Dispatching_Operation (Prim)
16063                        and then Disp_Typ  /= Full_T
16064                      then
16065
16066                         --  Verify that it is not otherwise controlled by a
16067                         --  formal or a return value of type T.
16068
16069                         Check_Controlling_Formals (Disp_Typ, Prim);
16070                      end if;
16071                   end if;
16072
16073                   Next_Entity (Prim);
16074                end loop;
16075             end if;
16076
16077             --  For the tagged case, the two views can share the same
16078             --  Primitive Operation list and the same class wide type.
16079             --  Update attributes of the class-wide type which depend on
16080             --  the full declaration.
16081
16082             if Is_Tagged_Type (Priv_T) then
16083                Set_Primitive_Operations (Priv_T, Full_List);
16084                Set_Class_Wide_Type
16085                  (Base_Type (Full_T), Class_Wide_Type (Priv_T));
16086
16087                Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
16088             end if;
16089          end;
16090       end if;
16091
16092       --  Ada 2005 AI 161: Check preelaboratable initialization consistency
16093
16094       if Known_To_Have_Preelab_Init (Priv_T) then
16095
16096          --  Case where there is a pragma Preelaborable_Initialization. We
16097          --  always allow this in predefined units, which is a bit of a kludge,
16098          --  but it means we don't have to struggle to meet the requirements in
16099          --  the RM for having Preelaborable Initialization. Otherwise we
16100          --  require that the type meets the RM rules. But we can't check that
16101          --  yet, because of the rule about overriding Ininitialize, so we
16102          --  simply set a flag that will be checked at freeze time.
16103
16104          if not In_Predefined_Unit (Full_T) then
16105             Set_Must_Have_Preelab_Init (Full_T);
16106          end if;
16107       end if;
16108
16109       --  If pragma CPP_Class was applied to the private type declaration,
16110       --  propagate it now to the full type declaration.
16111
16112       if Is_CPP_Class (Priv_T) then
16113          Set_Is_CPP_Class (Full_T);
16114          Set_Convention   (Full_T, Convention_CPP);
16115       end if;
16116    end Process_Full_View;
16117
16118    -----------------------------------
16119    -- Process_Incomplete_Dependents --
16120    -----------------------------------
16121
16122    procedure Process_Incomplete_Dependents
16123      (N      : Node_Id;
16124       Full_T : Entity_Id;
16125       Inc_T  : Entity_Id)
16126    is
16127       Inc_Elmt : Elmt_Id;
16128       Priv_Dep : Entity_Id;
16129       New_Subt : Entity_Id;
16130
16131       Disc_Constraint : Elist_Id;
16132
16133    begin
16134       if No (Private_Dependents (Inc_T)) then
16135          return;
16136       end if;
16137
16138       --  Itypes that may be generated by the completion of an incomplete
16139       --  subtype are not used by the back-end and not attached to the tree.
16140       --  They are created only for constraint-checking purposes.
16141
16142       Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
16143       while Present (Inc_Elmt) loop
16144          Priv_Dep := Node (Inc_Elmt);
16145
16146          if Ekind (Priv_Dep) = E_Subprogram_Type then
16147
16148             --  An Access_To_Subprogram type may have a return type or a
16149             --  parameter type that is incomplete. Replace with the full view.
16150
16151             if Etype (Priv_Dep) = Inc_T then
16152                Set_Etype (Priv_Dep, Full_T);
16153             end if;
16154
16155             declare
16156                Formal : Entity_Id;
16157
16158             begin
16159                Formal := First_Formal (Priv_Dep);
16160                while Present (Formal) loop
16161                   if Etype (Formal) = Inc_T then
16162                      Set_Etype (Formal, Full_T);
16163                   end if;
16164
16165                   Next_Formal (Formal);
16166                end loop;
16167             end;
16168
16169          elsif Is_Overloadable (Priv_Dep) then
16170
16171             --  A protected operation is never dispatching: only its
16172             --  wrapper operation (which has convention Ada) is.
16173
16174             if Is_Tagged_Type (Full_T)
16175               and then Convention (Priv_Dep) /= Convention_Protected
16176             then
16177
16178                --  Subprogram has an access parameter whose designated type
16179                --  was incomplete. Reexamine declaration now, because it may
16180                --  be a primitive operation of the full type.
16181
16182                Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
16183                Set_Is_Dispatching_Operation (Priv_Dep);
16184                Check_Controlling_Formals (Full_T, Priv_Dep);
16185             end if;
16186
16187          elsif Ekind (Priv_Dep) = E_Subprogram_Body then
16188
16189             --  Can happen during processing of a body before the completion
16190             --  of a TA type. Ignore, because spec is also on dependent list.
16191
16192             return;
16193
16194          --  Ada 2005 (AI-412): Transform a regular incomplete subtype into a
16195          --  corresponding subtype of the full view.
16196
16197          elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
16198             Set_Subtype_Indication
16199               (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
16200             Set_Etype (Priv_Dep, Full_T);
16201             Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
16202             Set_Analyzed (Parent (Priv_Dep), False);
16203
16204             --  Reanalyze the declaration, suppressing the call to
16205             --  Enter_Name to avoid duplicate names.
16206
16207             Analyze_Subtype_Declaration
16208               (N    => Parent (Priv_Dep),
16209                Skip => True);
16210
16211          --  Dependent is a subtype
16212
16213          else
16214             --  We build a new subtype indication using the full view of the
16215             --  incomplete parent. The discriminant constraints have been
16216             --  elaborated already at the point of the subtype declaration.
16217
16218             New_Subt := Create_Itype (E_Void, N);
16219
16220             if Has_Discriminants (Full_T) then
16221                Disc_Constraint := Discriminant_Constraint (Priv_Dep);
16222             else
16223                Disc_Constraint := No_Elist;
16224             end if;
16225
16226             Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
16227             Set_Full_View (Priv_Dep, New_Subt);
16228          end if;
16229
16230          Next_Elmt (Inc_Elmt);
16231       end loop;
16232    end Process_Incomplete_Dependents;
16233
16234    --------------------------------
16235    -- Process_Range_Expr_In_Decl --
16236    --------------------------------
16237
16238    procedure Process_Range_Expr_In_Decl
16239      (R           : Node_Id;
16240       T           : Entity_Id;
16241       Check_List  : List_Id := Empty_List;
16242       R_Check_Off : Boolean := False)
16243    is
16244       Lo, Hi    : Node_Id;
16245       R_Checks  : Check_Result;
16246       Type_Decl : Node_Id;
16247       Def_Id    : Entity_Id;
16248
16249    begin
16250       Analyze_And_Resolve (R, Base_Type (T));
16251
16252       if Nkind (R) = N_Range then
16253          Lo := Low_Bound (R);
16254          Hi := High_Bound (R);
16255
16256          --  We need to ensure validity of the bounds here, because if we
16257          --  go ahead and do the expansion, then the expanded code will get
16258          --  analyzed with range checks suppressed and we miss the check.
16259
16260          Validity_Check_Range (R);
16261
16262          --  If there were errors in the declaration, try and patch up some
16263          --  common mistakes in the bounds. The cases handled are literals
16264          --  which are Integer where the expected type is Real and vice versa.
16265          --  These corrections allow the compilation process to proceed further
16266          --  along since some basic assumptions of the format of the bounds
16267          --  are guaranteed.
16268
16269          if Etype (R) = Any_Type then
16270
16271             if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
16272                Rewrite (Lo,
16273                  Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
16274
16275             elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
16276                Rewrite (Hi,
16277                  Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
16278
16279             elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
16280                Rewrite (Lo,
16281                  Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
16282
16283             elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
16284                Rewrite (Hi,
16285                  Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
16286             end if;
16287
16288             Set_Etype (Lo, T);
16289             Set_Etype (Hi, T);
16290          end if;
16291
16292          --  If the bounds of the range have been mistakenly given as string
16293          --  literals (perhaps in place of character literals), then an error
16294          --  has already been reported, but we rewrite the string literal as a
16295          --  bound of the range's type to avoid blowups in later processing
16296          --  that looks at static values.
16297
16298          if Nkind (Lo) = N_String_Literal then
16299             Rewrite (Lo,
16300               Make_Attribute_Reference (Sloc (Lo),
16301                 Attribute_Name => Name_First,
16302                 Prefix => New_Reference_To (T, Sloc (Lo))));
16303             Analyze_And_Resolve (Lo);
16304          end if;
16305
16306          if Nkind (Hi) = N_String_Literal then
16307             Rewrite (Hi,
16308               Make_Attribute_Reference (Sloc (Hi),
16309                 Attribute_Name => Name_First,
16310                 Prefix => New_Reference_To (T, Sloc (Hi))));
16311             Analyze_And_Resolve (Hi);
16312          end if;
16313
16314          --  If bounds aren't scalar at this point then exit, avoiding
16315          --  problems with further processing of the range in this procedure.
16316
16317          if not Is_Scalar_Type (Etype (Lo)) then
16318             return;
16319          end if;
16320
16321          --  Resolve (actually Sem_Eval) has checked that the bounds are in
16322          --  then range of the base type. Here we check whether the bounds
16323          --  are in the range of the subtype itself. Note that if the bounds
16324          --  represent the null range the Constraint_Error exception should
16325          --  not be raised.
16326
16327          --  ??? The following code should be cleaned up as follows
16328
16329          --  1. The Is_Null_Range (Lo, Hi) test should disappear since it
16330          --     is done in the call to Range_Check (R, T); below
16331
16332          --  2. The use of R_Check_Off should be investigated and possibly
16333          --     removed, this would clean up things a bit.
16334
16335          if Is_Null_Range (Lo, Hi) then
16336             null;
16337
16338          else
16339             --  Capture values of bounds and generate temporaries for them
16340             --  if needed, before applying checks, since checks may cause
16341             --  duplication of the expression without forcing evaluation.
16342
16343             if Expander_Active then
16344                Force_Evaluation (Lo);
16345                Force_Evaluation (Hi);
16346             end if;
16347
16348             --  We use a flag here instead of suppressing checks on the
16349             --  type because the type we check against isn't necessarily
16350             --  the place where we put the check.
16351
16352             if not R_Check_Off then
16353                R_Checks := Get_Range_Checks (R, T);
16354
16355                --  Look up tree to find an appropriate insertion point.
16356                --  This seems really junk code, and very brittle, couldn't
16357                --  we just use an insert actions call of some kind ???
16358
16359                Type_Decl := Parent (R);
16360                while Present (Type_Decl) and then not
16361                  (Nkind_In (Type_Decl, N_Full_Type_Declaration,
16362                                        N_Subtype_Declaration,
16363                                        N_Loop_Statement,
16364                                        N_Task_Type_Declaration)
16365                     or else
16366                   Nkind_In (Type_Decl, N_Single_Task_Declaration,
16367                                        N_Protected_Type_Declaration,
16368                                        N_Single_Protected_Declaration))
16369                loop
16370                   Type_Decl := Parent (Type_Decl);
16371                end loop;
16372
16373                --  Why would Type_Decl not be present???  Without this test,
16374                --  short regression tests fail.
16375
16376                if Present (Type_Decl) then
16377
16378                   --  Case of loop statement (more comments ???)
16379
16380                   if Nkind (Type_Decl) = N_Loop_Statement then
16381                      declare
16382                         Indic : Node_Id;
16383
16384                      begin
16385                         Indic := Parent (R);
16386                         while Present (Indic)
16387                           and then Nkind (Indic) /= N_Subtype_Indication
16388                         loop
16389                            Indic := Parent (Indic);
16390                         end loop;
16391
16392                         if Present (Indic) then
16393                            Def_Id := Etype (Subtype_Mark (Indic));
16394
16395                            Insert_Range_Checks
16396                              (R_Checks,
16397                               Type_Decl,
16398                               Def_Id,
16399                               Sloc (Type_Decl),
16400                               R,
16401                               Do_Before => True);
16402                         end if;
16403                      end;
16404
16405                   --  All other cases (more comments ???)
16406
16407                   else
16408                      Def_Id := Defining_Identifier (Type_Decl);
16409
16410                      if (Ekind (Def_Id) = E_Record_Type
16411                           and then Depends_On_Discriminant (R))
16412                        or else
16413                         (Ekind (Def_Id) = E_Protected_Type
16414                           and then Has_Discriminants (Def_Id))
16415                      then
16416                         Append_Range_Checks
16417                           (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
16418
16419                      else
16420                         Insert_Range_Checks
16421                           (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
16422
16423                      end if;
16424                   end if;
16425                end if;
16426             end if;
16427          end if;
16428
16429       elsif Expander_Active then
16430          Get_Index_Bounds (R, Lo, Hi);
16431          Force_Evaluation (Lo);
16432          Force_Evaluation (Hi);
16433       end if;
16434    end Process_Range_Expr_In_Decl;
16435
16436    --------------------------------------
16437    -- Process_Real_Range_Specification --
16438    --------------------------------------
16439
16440    procedure Process_Real_Range_Specification (Def : Node_Id) is
16441       Spec : constant Node_Id := Real_Range_Specification (Def);
16442       Lo   : Node_Id;
16443       Hi   : Node_Id;
16444       Err  : Boolean := False;
16445
16446       procedure Analyze_Bound (N : Node_Id);
16447       --  Analyze and check one bound
16448
16449       -------------------
16450       -- Analyze_Bound --
16451       -------------------
16452
16453       procedure Analyze_Bound (N : Node_Id) is
16454       begin
16455          Analyze_And_Resolve (N, Any_Real);
16456
16457          if not Is_OK_Static_Expression (N) then
16458             Flag_Non_Static_Expr
16459               ("bound in real type definition is not static!", N);
16460             Err := True;
16461          end if;
16462       end Analyze_Bound;
16463
16464    --  Start of processing for Process_Real_Range_Specification
16465
16466    begin
16467       if Present (Spec) then
16468          Lo := Low_Bound (Spec);
16469          Hi := High_Bound (Spec);
16470          Analyze_Bound (Lo);
16471          Analyze_Bound (Hi);
16472
16473          --  If error, clear away junk range specification
16474
16475          if Err then
16476             Set_Real_Range_Specification (Def, Empty);
16477          end if;
16478       end if;
16479    end Process_Real_Range_Specification;
16480
16481    ---------------------
16482    -- Process_Subtype --
16483    ---------------------
16484
16485    function Process_Subtype
16486      (S           : Node_Id;
16487       Related_Nod : Node_Id;
16488       Related_Id  : Entity_Id := Empty;
16489       Suffix      : Character := ' ') return Entity_Id
16490    is
16491       P               : Node_Id;
16492       Def_Id          : Entity_Id;
16493       Error_Node      : Node_Id;
16494       Full_View_Id    : Entity_Id;
16495       Subtype_Mark_Id : Entity_Id;
16496
16497       May_Have_Null_Exclusion : Boolean;
16498
16499       procedure Check_Incomplete (T : Entity_Id);
16500       --  Called to verify that an incomplete type is not used prematurely
16501
16502       ----------------------
16503       -- Check_Incomplete --
16504       ----------------------
16505
16506       procedure Check_Incomplete (T : Entity_Id) is
16507       begin
16508          --  Ada 2005 (AI-412): Incomplete subtypes are legal
16509
16510          if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
16511            and then
16512              not (Ada_Version >= Ada_05
16513                     and then
16514                        (Nkind (Parent (T)) = N_Subtype_Declaration
16515                           or else
16516                             (Nkind (Parent (T)) = N_Subtype_Indication
16517                                and then Nkind (Parent (Parent (T))) =
16518                                           N_Subtype_Declaration)))
16519          then
16520             Error_Msg_N ("invalid use of type before its full declaration", T);
16521          end if;
16522       end Check_Incomplete;
16523
16524    --  Start of processing for Process_Subtype
16525
16526    begin
16527       --  Case of no constraints present
16528
16529       if Nkind (S) /= N_Subtype_Indication then
16530          Find_Type (S);
16531          Check_Incomplete (S);
16532          P := Parent (S);
16533
16534          --  Ada 2005 (AI-231): Static check
16535
16536          if Ada_Version >= Ada_05
16537            and then Present (P)
16538            and then Null_Exclusion_Present (P)
16539            and then Nkind (P) /= N_Access_To_Object_Definition
16540            and then not Is_Access_Type (Entity (S))
16541          then
16542             Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
16543          end if;
16544
16545          --  The following is ugly, can't we have a range or even a flag???
16546
16547          May_Have_Null_Exclusion :=
16548            Nkind_In (P, N_Access_Definition,
16549                         N_Access_Function_Definition,
16550                         N_Access_Procedure_Definition,
16551                         N_Access_To_Object_Definition,
16552                         N_Allocator,
16553                         N_Component_Definition)
16554              or else
16555            Nkind_In (P, N_Derived_Type_Definition,
16556                         N_Discriminant_Specification,
16557                         N_Formal_Object_Declaration,
16558                         N_Object_Declaration,
16559                         N_Object_Renaming_Declaration,
16560                         N_Parameter_Specification,
16561                         N_Subtype_Declaration);
16562
16563          --  Create an Itype that is a duplicate of Entity (S) but with the
16564          --  null-exclusion attribute
16565
16566          if May_Have_Null_Exclusion
16567            and then Is_Access_Type (Entity (S))
16568            and then Null_Exclusion_Present (P)
16569
16570             --  No need to check the case of an access to object definition.
16571             --  It is correct to define double not-null pointers.
16572
16573             --  Example:
16574             --     type Not_Null_Int_Ptr is not null access Integer;
16575             --     type Acc is not null access Not_Null_Int_Ptr;
16576
16577            and then Nkind (P) /= N_Access_To_Object_Definition
16578          then
16579             if Can_Never_Be_Null (Entity (S)) then
16580                case Nkind (Related_Nod) is
16581                   when N_Full_Type_Declaration =>
16582                      if Nkind (Type_Definition (Related_Nod))
16583                        in N_Array_Type_Definition
16584                      then
16585                         Error_Node :=
16586                           Subtype_Indication
16587                             (Component_Definition
16588                              (Type_Definition (Related_Nod)));
16589                      else
16590                         Error_Node :=
16591                           Subtype_Indication (Type_Definition (Related_Nod));
16592                      end if;
16593
16594                   when N_Subtype_Declaration =>
16595                      Error_Node := Subtype_Indication (Related_Nod);
16596
16597                   when N_Object_Declaration =>
16598                      Error_Node := Object_Definition (Related_Nod);
16599
16600                   when N_Component_Declaration =>
16601                      Error_Node :=
16602                        Subtype_Indication (Component_Definition (Related_Nod));
16603
16604                   when N_Allocator =>
16605                      Error_Node := Expression (Related_Nod);
16606
16607                   when others =>
16608                      pragma Assert (False);
16609                      Error_Node := Related_Nod;
16610                end case;
16611
16612                Error_Msg_NE
16613                  ("`NOT NULL` not allowed (& already excludes null)",
16614                   Error_Node,
16615                   Entity (S));
16616             end if;
16617
16618             Set_Etype  (S,
16619               Create_Null_Excluding_Itype
16620                 (T           => Entity (S),
16621                  Related_Nod => P));
16622             Set_Entity (S, Etype (S));
16623          end if;
16624
16625          return Entity (S);
16626
16627       --  Case of constraint present, so that we have an N_Subtype_Indication
16628       --  node (this node is created only if constraints are present).
16629
16630       else
16631          Find_Type (Subtype_Mark (S));
16632
16633          if Nkind (Parent (S)) /= N_Access_To_Object_Definition
16634            and then not
16635             (Nkind (Parent (S)) = N_Subtype_Declaration
16636               and then Is_Itype (Defining_Identifier (Parent (S))))
16637          then
16638             Check_Incomplete (Subtype_Mark (S));
16639          end if;
16640
16641          P := Parent (S);
16642          Subtype_Mark_Id := Entity (Subtype_Mark (S));
16643
16644          --  Explicit subtype declaration case
16645
16646          if Nkind (P) = N_Subtype_Declaration then
16647             Def_Id := Defining_Identifier (P);
16648
16649          --  Explicit derived type definition case
16650
16651          elsif Nkind (P) = N_Derived_Type_Definition then
16652             Def_Id := Defining_Identifier (Parent (P));
16653
16654          --  Implicit case, the Def_Id must be created as an implicit type.
16655          --  The one exception arises in the case of concurrent types, array
16656          --  and access types, where other subsidiary implicit types may be
16657          --  created and must appear before the main implicit type. In these
16658          --  cases we leave Def_Id set to Empty as a signal that Create_Itype
16659          --  has not yet been called to create Def_Id.
16660
16661          else
16662             if Is_Array_Type (Subtype_Mark_Id)
16663               or else Is_Concurrent_Type (Subtype_Mark_Id)
16664               or else Is_Access_Type (Subtype_Mark_Id)
16665             then
16666                Def_Id := Empty;
16667
16668             --  For the other cases, we create a new unattached Itype,
16669             --  and set the indication to ensure it gets attached later.
16670
16671             else
16672                Def_Id :=
16673                  Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
16674             end if;
16675          end if;
16676
16677          --  If the kind of constraint is invalid for this kind of type,
16678          --  then give an error, and then pretend no constraint was given.
16679
16680          if not Is_Valid_Constraint_Kind
16681                    (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
16682          then
16683             Error_Msg_N
16684               ("incorrect constraint for this kind of type", Constraint (S));
16685
16686             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16687
16688             --  Set Ekind of orphan itype, to prevent cascaded errors
16689
16690             if Present (Def_Id) then
16691                Set_Ekind (Def_Id, Ekind (Any_Type));
16692             end if;
16693
16694             --  Make recursive call, having got rid of the bogus constraint
16695
16696             return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
16697          end if;
16698
16699          --  Remaining processing depends on type
16700
16701          case Ekind (Subtype_Mark_Id) is
16702             when Access_Kind =>
16703                Constrain_Access (Def_Id, S, Related_Nod);
16704
16705                if Expander_Active
16706                  and then  Is_Itype (Designated_Type (Def_Id))
16707                  and then Nkind (Related_Nod) = N_Subtype_Declaration
16708                  and then not Is_Incomplete_Type (Designated_Type (Def_Id))
16709                then
16710                   Build_Itype_Reference
16711                     (Designated_Type (Def_Id), Related_Nod);
16712                end if;
16713
16714             when Array_Kind =>
16715                Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
16716
16717             when Decimal_Fixed_Point_Kind =>
16718                Constrain_Decimal (Def_Id, S);
16719
16720             when Enumeration_Kind =>
16721                Constrain_Enumeration (Def_Id, S);
16722
16723             when Ordinary_Fixed_Point_Kind =>
16724                Constrain_Ordinary_Fixed (Def_Id, S);
16725
16726             when Float_Kind =>
16727                Constrain_Float (Def_Id, S);
16728
16729             when Integer_Kind =>
16730                Constrain_Integer (Def_Id, S);
16731
16732             when E_Record_Type     |
16733                  E_Record_Subtype  |
16734                  Class_Wide_Kind   |
16735                  E_Incomplete_Type =>
16736                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
16737
16738             when Private_Kind =>
16739                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
16740                Set_Private_Dependents (Def_Id, New_Elmt_List);
16741
16742                --  In case of an invalid constraint prevent further processing
16743                --  since the type constructed is missing expected fields.
16744
16745                if Etype (Def_Id) = Any_Type then
16746                   return Def_Id;
16747                end if;
16748
16749                --  If the full view is that of a task with discriminants,
16750                --  we must constrain both the concurrent type and its
16751                --  corresponding record type. Otherwise we will just propagate
16752                --  the constraint to the full view, if available.
16753
16754                if Present (Full_View (Subtype_Mark_Id))
16755                  and then Has_Discriminants (Subtype_Mark_Id)
16756                  and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
16757                then
16758                   Full_View_Id :=
16759                     Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
16760
16761                   Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
16762                   Constrain_Concurrent (Full_View_Id, S,
16763                     Related_Nod, Related_Id, Suffix);
16764                   Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
16765                   Set_Full_View (Def_Id, Full_View_Id);
16766
16767                   --  Introduce an explicit reference to the private subtype,
16768                   --  to prevent scope anomalies in gigi if first use appears
16769                   --  in a nested context, e.g. a later function body.
16770                   --  Should this be generated in other contexts than a full
16771                   --  type declaration?
16772
16773                   if Is_Itype (Def_Id)
16774                     and then
16775                       Nkind (Parent (P)) = N_Full_Type_Declaration
16776                   then
16777                      Build_Itype_Reference (Def_Id, Parent (P));
16778                   end if;
16779
16780                else
16781                   Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
16782                end if;
16783
16784             when Concurrent_Kind  =>
16785                Constrain_Concurrent (Def_Id, S,
16786                  Related_Nod, Related_Id, Suffix);
16787
16788             when others =>
16789                Error_Msg_N ("invalid subtype mark in subtype indication", S);
16790          end case;
16791
16792          --  Size and Convention are always inherited from the base type
16793
16794          Set_Size_Info  (Def_Id,            (Subtype_Mark_Id));
16795          Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
16796
16797          return Def_Id;
16798       end if;
16799    end Process_Subtype;
16800
16801    ---------------------------------------
16802    -- Check_Anonymous_Access_Components --
16803    ---------------------------------------
16804
16805    procedure Check_Anonymous_Access_Components
16806       (Typ_Decl  : Node_Id;
16807        Typ       : Entity_Id;
16808        Prev      : Entity_Id;
16809        Comp_List : Node_Id)
16810    is
16811       Loc         : constant Source_Ptr := Sloc (Typ_Decl);
16812       Anon_Access : Entity_Id;
16813       Acc_Def     : Node_Id;
16814       Comp        : Node_Id;
16815       Comp_Def    : Node_Id;
16816       Decl        : Node_Id;
16817       Type_Def    : Node_Id;
16818
16819       procedure Build_Incomplete_Type_Declaration;
16820       --  If the record type contains components that include an access to the
16821       --  current record, then create an incomplete type declaration for the
16822       --  record, to be used as the designated type of the anonymous access.
16823       --  This is done only once, and only if there is no previous partial
16824       --  view of the type.
16825
16826       function Designates_T (Subt : Node_Id) return Boolean;
16827       --  Check whether a node designates the enclosing record type, or 'Class
16828       --  of that type
16829
16830       function Mentions_T (Acc_Def : Node_Id) return Boolean;
16831       --  Check whether an access definition includes a reference to
16832       --  the enclosing record type. The reference can be a subtype mark
16833       --  in the access definition itself, a 'Class attribute reference, or
16834       --  recursively a reference appearing in a parameter specification
16835       --  or result definition of an access_to_subprogram definition.
16836
16837       --------------------------------------
16838       -- Build_Incomplete_Type_Declaration --
16839       --------------------------------------
16840
16841       procedure Build_Incomplete_Type_Declaration is
16842          Decl  : Node_Id;
16843          Inc_T : Entity_Id;
16844          H     : Entity_Id;
16845
16846          --  Is_Tagged indicates whether the type is tagged. It is tagged if
16847          --  it's "is new ... with record" or else "is tagged record ...".
16848
16849          Is_Tagged : constant Boolean :=
16850              (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
16851                  and then
16852                    Present
16853                      (Record_Extension_Part (Type_Definition (Typ_Decl))))
16854            or else
16855              (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
16856                  and then Tagged_Present (Type_Definition (Typ_Decl)));
16857
16858       begin
16859          --  If there is a previous partial view, no need to create a new one
16860          --  If the partial view, given by Prev, is incomplete,  If Prev is
16861          --  a private declaration, full declaration is flagged accordingly.
16862
16863          if Prev /= Typ then
16864             if Is_Tagged then
16865                Make_Class_Wide_Type (Prev);
16866                Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
16867                Set_Etype (Class_Wide_Type (Typ), Typ);
16868             end if;
16869
16870             return;
16871
16872          elsif Has_Private_Declaration (Typ) then
16873
16874             --  If we refer to T'Class inside T, and T is the completion of a
16875             --  private type, then we need to make sure the class-wide type
16876             --  exists.
16877
16878             if Is_Tagged then
16879                Make_Class_Wide_Type (Typ);
16880             end if;
16881
16882             return;
16883
16884          --  If there was a previous anonymous access type, the incomplete
16885          --  type declaration will have been created already.
16886
16887          elsif Present (Current_Entity (Typ))
16888            and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
16889            and then Full_View (Current_Entity (Typ)) = Typ
16890          then
16891             return;
16892
16893          else
16894             Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
16895             Decl  := Make_Incomplete_Type_Declaration (Loc, Inc_T);
16896
16897             --  Type has already been inserted into the current scope.
16898             --  Remove it, and add incomplete declaration for type, so
16899             --  that subsequent anonymous access types can use it.
16900             --  The entity is unchained from the homonym list and from
16901             --  immediate visibility. After analysis, the entity in the
16902             --  incomplete declaration becomes immediately visible in the
16903             --  record declaration that follows.
16904
16905             H := Current_Entity (Typ);
16906
16907             if H = Typ then
16908                Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
16909             else
16910                while Present (H)
16911                  and then Homonym (H) /= Typ
16912                loop
16913                   H := Homonym (Typ);
16914                end loop;
16915
16916                Set_Homonym (H, Homonym (Typ));
16917             end if;
16918
16919             Insert_Before (Typ_Decl, Decl);
16920             Analyze (Decl);
16921             Set_Full_View (Inc_T, Typ);
16922
16923             if Is_Tagged then
16924                --  Create a common class-wide type for both views, and set
16925                --  the Etype of the class-wide type to the full view.
16926
16927                Make_Class_Wide_Type (Inc_T);
16928                Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
16929                Set_Etype (Class_Wide_Type (Typ), Typ);
16930             end if;
16931          end if;
16932       end Build_Incomplete_Type_Declaration;
16933
16934       ------------------
16935       -- Designates_T --
16936       ------------------
16937
16938       function Designates_T (Subt : Node_Id) return Boolean is
16939          Type_Id : constant Name_Id := Chars (Typ);
16940
16941          function Names_T (Nam : Node_Id) return Boolean;
16942          --  The record type has not been introduced in the current scope
16943          --  yet, so we must examine the name of the type itself, either
16944          --  an identifier T, or an expanded name of the form P.T, where
16945          --  P denotes the current scope.
16946
16947          -------------
16948          -- Names_T --
16949          -------------
16950
16951          function Names_T (Nam : Node_Id) return Boolean is
16952          begin
16953             if Nkind (Nam) = N_Identifier then
16954                return Chars (Nam) = Type_Id;
16955
16956             elsif Nkind (Nam) = N_Selected_Component then
16957                if Chars (Selector_Name (Nam)) = Type_Id then
16958                   if Nkind (Prefix (Nam)) = N_Identifier then
16959                      return Chars (Prefix (Nam)) = Chars (Current_Scope);
16960
16961                   elsif Nkind (Prefix (Nam)) = N_Selected_Component then
16962                      return Chars (Selector_Name (Prefix (Nam))) =
16963                             Chars (Current_Scope);
16964                   else
16965                      return False;
16966                   end if;
16967
16968                else
16969                   return False;
16970                end if;
16971
16972             else
16973                return False;
16974             end if;
16975          end Names_T;
16976
16977       --  Start of processing for Designates_T
16978
16979       begin
16980          if Nkind (Subt) = N_Identifier then
16981             return Chars (Subt) = Type_Id;
16982
16983             --  Reference can be through an expanded name which has not been
16984             --  analyzed yet, and which designates enclosing scopes.
16985
16986          elsif Nkind (Subt) = N_Selected_Component then
16987             if Names_T (Subt) then
16988                return True;
16989
16990             --  Otherwise it must denote an entity that is already visible.
16991             --  The access definition may name a subtype of the enclosing
16992             --  type, if there is a previous incomplete declaration for it.
16993
16994             else
16995                Find_Selected_Component (Subt);
16996                return
16997                  Is_Entity_Name (Subt)
16998                    and then Scope (Entity (Subt)) = Current_Scope
16999                    and then
17000                      (Chars (Base_Type (Entity (Subt))) = Type_Id
17001                        or else
17002                          (Is_Class_Wide_Type (Entity (Subt))
17003                            and then
17004                            Chars (Etype (Base_Type (Entity (Subt)))) =
17005                                                                   Type_Id));
17006             end if;
17007
17008          --  A reference to the current type may appear as the prefix of
17009          --  a 'Class attribute.
17010
17011          elsif Nkind (Subt) = N_Attribute_Reference
17012            and then Attribute_Name (Subt) = Name_Class
17013          then
17014             return Names_T (Prefix (Subt));
17015
17016          else
17017             return False;
17018          end if;
17019       end Designates_T;
17020
17021       ----------------
17022       -- Mentions_T --
17023       ----------------
17024
17025       function Mentions_T (Acc_Def : Node_Id) return Boolean is
17026          Param_Spec : Node_Id;
17027
17028          Acc_Subprg : constant Node_Id :=
17029                         Access_To_Subprogram_Definition (Acc_Def);
17030
17031       begin
17032          if No (Acc_Subprg) then
17033             return Designates_T (Subtype_Mark (Acc_Def));
17034          end if;
17035
17036          --  Component is an access_to_subprogram: examine its formals,
17037          --  and result definition in the case of an access_to_function.
17038
17039          Param_Spec := First (Parameter_Specifications (Acc_Subprg));
17040          while Present (Param_Spec) loop
17041             if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
17042               and then Mentions_T (Parameter_Type (Param_Spec))
17043             then
17044                return True;
17045
17046             elsif Designates_T (Parameter_Type (Param_Spec)) then
17047                return True;
17048             end if;
17049
17050             Next (Param_Spec);
17051          end loop;
17052
17053          if Nkind (Acc_Subprg) = N_Access_Function_Definition then
17054             if Nkind (Result_Definition (Acc_Subprg)) =
17055                  N_Access_Definition
17056             then
17057                return Mentions_T (Result_Definition (Acc_Subprg));
17058             else
17059                return Designates_T (Result_Definition (Acc_Subprg));
17060             end if;
17061          end if;
17062
17063          return False;
17064       end Mentions_T;
17065
17066    --  Start of processing for Check_Anonymous_Access_Components
17067
17068    begin
17069       if No (Comp_List) then
17070          return;
17071       end if;
17072
17073       Comp := First (Component_Items (Comp_List));
17074       while Present (Comp) loop
17075          if Nkind (Comp) = N_Component_Declaration
17076            and then Present
17077              (Access_Definition (Component_Definition (Comp)))
17078            and then
17079              Mentions_T (Access_Definition (Component_Definition (Comp)))
17080          then
17081             Comp_Def := Component_Definition (Comp);
17082             Acc_Def :=
17083               Access_To_Subprogram_Definition
17084                 (Access_Definition (Comp_Def));
17085
17086             Build_Incomplete_Type_Declaration;
17087             Anon_Access :=
17088               Make_Defining_Identifier (Loc,
17089                 Chars => New_Internal_Name ('S'));
17090
17091             --  Create a declaration for the anonymous access type: either
17092             --  an access_to_object or an access_to_subprogram.
17093
17094             if Present (Acc_Def) then
17095                if Nkind  (Acc_Def) = N_Access_Function_Definition then
17096                   Type_Def :=
17097                     Make_Access_Function_Definition (Loc,
17098                       Parameter_Specifications =>
17099                         Parameter_Specifications (Acc_Def),
17100                       Result_Definition => Result_Definition (Acc_Def));
17101                else
17102                   Type_Def :=
17103                     Make_Access_Procedure_Definition (Loc,
17104                       Parameter_Specifications =>
17105                         Parameter_Specifications (Acc_Def));
17106                end if;
17107
17108             else
17109                Type_Def :=
17110                  Make_Access_To_Object_Definition (Loc,
17111                    Subtype_Indication =>
17112                       Relocate_Node
17113                         (Subtype_Mark
17114                           (Access_Definition (Comp_Def))));
17115
17116                Set_Constant_Present
17117                  (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
17118                Set_All_Present
17119                  (Type_Def, All_Present (Access_Definition (Comp_Def)));
17120             end if;
17121
17122             Set_Null_Exclusion_Present
17123               (Type_Def,
17124                Null_Exclusion_Present (Access_Definition (Comp_Def)));
17125
17126             Decl :=
17127               Make_Full_Type_Declaration (Loc,
17128                 Defining_Identifier => Anon_Access,
17129                 Type_Definition     => Type_Def);
17130
17131             Insert_Before (Typ_Decl, Decl);
17132             Analyze (Decl);
17133
17134             --  If an access to object, Preserve entity of designated type,
17135             --  for ASIS use, before rewriting the component definition.
17136
17137             if No (Acc_Def) then
17138                declare
17139                   Desig : Entity_Id;
17140
17141                begin
17142                   Desig := Entity (Subtype_Indication (Type_Def));
17143
17144                   --  If the access definition is to the current  record,
17145                   --  the visible entity at this point is an  incomplete
17146                   --  type. Retrieve the full view to simplify  ASIS queries
17147
17148                   if Ekind (Desig) = E_Incomplete_Type then
17149                      Desig := Full_View (Desig);
17150                   end if;
17151
17152                   Set_Entity
17153                     (Subtype_Mark (Access_Definition  (Comp_Def)), Desig);
17154                end;
17155             end if;
17156
17157             Rewrite (Comp_Def,
17158               Make_Component_Definition (Loc,
17159                 Subtype_Indication =>
17160                New_Occurrence_Of (Anon_Access, Loc)));
17161
17162             if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
17163                Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
17164             else
17165                Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
17166             end if;
17167
17168             Set_Is_Local_Anonymous_Access (Anon_Access);
17169          end if;
17170
17171          Next (Comp);
17172       end loop;
17173
17174       if Present (Variant_Part (Comp_List)) then
17175          declare
17176             V : Node_Id;
17177          begin
17178             V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
17179             while Present (V) loop
17180                Check_Anonymous_Access_Components
17181                  (Typ_Decl, Typ, Prev, Component_List (V));
17182                Next_Non_Pragma (V);
17183             end loop;
17184          end;
17185       end if;
17186    end Check_Anonymous_Access_Components;
17187
17188    --------------------------------
17189    -- Preanalyze_Spec_Expression --
17190    --------------------------------
17191
17192    procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
17193       Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
17194    begin
17195       In_Spec_Expression := True;
17196       Preanalyze_And_Resolve (N, T);
17197       In_Spec_Expression := Save_In_Spec_Expression;
17198    end Preanalyze_Spec_Expression;
17199
17200    -----------------------------
17201    -- Record_Type_Declaration --
17202    -----------------------------
17203
17204    procedure Record_Type_Declaration
17205      (T    : Entity_Id;
17206       N    : Node_Id;
17207       Prev : Entity_Id)
17208    is
17209       Def       : constant Node_Id := Type_Definition (N);
17210       Is_Tagged : Boolean;
17211       Tag_Comp  : Entity_Id;
17212
17213    begin
17214       --  These flags must be initialized before calling Process_Discriminants
17215       --  because this routine makes use of them.
17216
17217       Set_Ekind             (T, E_Record_Type);
17218       Set_Etype             (T, T);
17219       Init_Size_Align       (T);
17220       Set_Interfaces        (T, No_Elist);
17221       Set_Stored_Constraint (T, No_Elist);
17222
17223       --  Normal case
17224
17225       if Ada_Version < Ada_05
17226         or else not Interface_Present (Def)
17227       then
17228          --  The flag Is_Tagged_Type might have already been set by
17229          --  Find_Type_Name if it detected an error for declaration T. This
17230          --  arises in the case of private tagged types where the full view
17231          --  omits the word tagged.
17232
17233          Is_Tagged :=
17234            Tagged_Present (Def)
17235              or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
17236
17237          Set_Is_Tagged_Type      (T, Is_Tagged);
17238          Set_Is_Limited_Record   (T, Limited_Present (Def));
17239
17240          --  Type is abstract if full declaration carries keyword, or if
17241          --  previous partial view did.
17242
17243          Set_Is_Abstract_Type    (T, Is_Abstract_Type (T)
17244                                       or else Abstract_Present (Def));
17245
17246       else
17247          Is_Tagged := True;
17248          Analyze_Interface_Declaration (T, Def);
17249
17250          if Present (Discriminant_Specifications (N)) then
17251             Error_Msg_N
17252               ("interface types cannot have discriminants",
17253                 Defining_Identifier
17254                   (First (Discriminant_Specifications (N))));
17255          end if;
17256       end if;
17257
17258       --  First pass: if there are self-referential access components,
17259       --  create the required anonymous access type declarations, and if
17260       --  need be an incomplete type declaration for T itself.
17261
17262       Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
17263
17264       if Ada_Version >= Ada_05
17265         and then Present (Interface_List (Def))
17266       then
17267          Check_Interfaces (N, Def);
17268
17269          declare
17270             Ifaces_List : Elist_Id;
17271
17272          begin
17273             --  Ada 2005 (AI-251): Collect the list of progenitors that are not
17274             --  already in the parents.
17275
17276             Collect_Interfaces
17277               (T               => T,
17278                Ifaces_List     => Ifaces_List,
17279                Exclude_Parents => True);
17280
17281             Set_Interfaces (T, Ifaces_List);
17282          end;
17283       end if;
17284
17285       --  Records constitute a scope for the component declarations within.
17286       --  The scope is created prior to the processing of these declarations.
17287       --  Discriminants are processed first, so that they are visible when
17288       --  processing the other components. The Ekind of the record type itself
17289       --  is set to E_Record_Type (subtypes appear as E_Record_Subtype).
17290
17291       --  Enter record scope
17292
17293       Push_Scope (T);
17294
17295       --  If an incomplete or private type declaration was already given for
17296       --  the type, then this scope already exists, and the discriminants have
17297       --  been declared within. We must verify that the full declaration
17298       --  matches the incomplete one.
17299
17300       Check_Or_Process_Discriminants (N, T, Prev);
17301
17302       Set_Is_Constrained     (T, not Has_Discriminants (T));
17303       Set_Has_Delayed_Freeze (T, True);
17304
17305       --  For tagged types add a manually analyzed component corresponding
17306       --  to the component _tag, the corresponding piece of tree will be
17307       --  expanded as part of the freezing actions if it is not a CPP_Class.
17308
17309       if Is_Tagged then
17310
17311          --  Do not add the tag unless we are in expansion mode
17312
17313          if Expander_Active then
17314             Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
17315             Enter_Name (Tag_Comp);
17316
17317             Set_Ekind                     (Tag_Comp, E_Component);
17318             Set_Is_Tag                    (Tag_Comp);
17319             Set_Is_Aliased                (Tag_Comp);
17320             Set_Etype                     (Tag_Comp, RTE (RE_Tag));
17321             Set_DT_Entry_Count            (Tag_Comp, No_Uint);
17322             Set_Original_Record_Component (Tag_Comp, Tag_Comp);
17323             Init_Component_Location       (Tag_Comp);
17324
17325             --  Ada 2005 (AI-251): Addition of the Tag corresponding to all the
17326             --  implemented interfaces.
17327
17328             if Has_Interfaces (T) then
17329                Add_Interface_Tag_Components (N, T);
17330             end if;
17331          end if;
17332
17333          Make_Class_Wide_Type (T);
17334          Set_Primitive_Operations (T, New_Elmt_List);
17335       end if;
17336
17337       --  We must suppress range checks when processing the components
17338       --  of a record in the presence of discriminants, since we don't
17339       --  want spurious checks to be generated during their analysis, but
17340       --  must reset the Suppress_Range_Checks flags after having processed
17341       --  the record definition.
17342
17343       --  Note: this is the only use of Kill_Range_Checks, and is a bit odd,
17344       --  couldn't we just use the normal range check suppression method here.
17345       --  That would seem cleaner ???
17346
17347       if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
17348          Set_Kill_Range_Checks (T, True);
17349          Record_Type_Definition (Def, Prev);
17350          Set_Kill_Range_Checks (T, False);
17351       else
17352          Record_Type_Definition (Def, Prev);
17353       end if;
17354
17355       --  Exit from record scope
17356
17357       End_Scope;
17358
17359       --  Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
17360       --  the implemented interfaces and associate them an aliased entity.
17361
17362       if Is_Tagged
17363         and then not Is_Empty_List (Interface_List (Def))
17364       then
17365          Derive_Progenitor_Subprograms (T, T);
17366       end if;
17367    end Record_Type_Declaration;
17368
17369    ----------------------------
17370    -- Record_Type_Definition --
17371    ----------------------------
17372
17373    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
17374       Component          : Entity_Id;
17375       Ctrl_Components    : Boolean := False;
17376       Final_Storage_Only : Boolean;
17377       T                  : Entity_Id;
17378
17379    begin
17380       if Ekind (Prev_T) = E_Incomplete_Type then
17381          T := Full_View (Prev_T);
17382       else
17383          T := Prev_T;
17384       end if;
17385
17386       Final_Storage_Only := not Is_Controlled (T);
17387
17388       --  Ada 2005: check whether an explicit Limited is present in a derived
17389       --  type declaration.
17390
17391       if Nkind (Parent (Def)) = N_Derived_Type_Definition
17392         and then Limited_Present (Parent (Def))
17393       then
17394          Set_Is_Limited_Record (T);
17395       end if;
17396
17397       --  If the component list of a record type is defined by the reserved
17398       --  word null and there is no discriminant part, then the record type has
17399       --  no components and all records of the type are null records (RM 3.7)
17400       --  This procedure is also called to process the extension part of a
17401       --  record extension, in which case the current scope may have inherited
17402       --  components.
17403
17404       if No (Def)
17405         or else No (Component_List (Def))
17406         or else Null_Present (Component_List (Def))
17407       then
17408          null;
17409
17410       else
17411          Analyze_Declarations (Component_Items (Component_List (Def)));
17412
17413          if Present (Variant_Part (Component_List (Def))) then
17414             Analyze (Variant_Part (Component_List (Def)));
17415          end if;
17416       end if;
17417
17418       --  After completing the semantic analysis of the record definition,
17419       --  record components, both new and inherited, are accessible. Set their
17420       --  kind accordingly. Exclude malformed itypes from illegal declarations,
17421       --  whose Ekind may be void.
17422
17423       Component := First_Entity (Current_Scope);
17424       while Present (Component) loop
17425          if Ekind (Component) = E_Void
17426            and then not Is_Itype (Component)
17427          then
17428             Set_Ekind (Component, E_Component);
17429             Init_Component_Location (Component);
17430          end if;
17431
17432          if Has_Task (Etype (Component)) then
17433             Set_Has_Task (T);
17434          end if;
17435
17436          if Ekind (Component) /= E_Component then
17437             null;
17438
17439          elsif Has_Controlled_Component (Etype (Component))
17440            or else (Chars (Component) /= Name_uParent
17441                      and then Is_Controlled (Etype (Component)))
17442          then
17443             Set_Has_Controlled_Component (T, True);
17444             Final_Storage_Only :=
17445               Final_Storage_Only
17446                 and then Finalize_Storage_Only (Etype (Component));
17447             Ctrl_Components := True;
17448          end if;
17449
17450          Next_Entity (Component);
17451       end loop;
17452
17453       --  A Type is Finalize_Storage_Only only if all its controlled components
17454       --  are also.
17455
17456       if Ctrl_Components then
17457          Set_Finalize_Storage_Only (T, Final_Storage_Only);
17458       end if;
17459
17460       --  Place reference to end record on the proper entity, which may
17461       --  be a partial view.
17462
17463       if Present (Def) then
17464          Process_End_Label (Def, 'e', Prev_T);
17465       end if;
17466    end Record_Type_Definition;
17467
17468    ------------------------
17469    -- Replace_Components --
17470    ------------------------
17471
17472    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
17473       function Process (N : Node_Id) return Traverse_Result;
17474
17475       -------------
17476       -- Process --
17477       -------------
17478
17479       function Process (N : Node_Id) return Traverse_Result is
17480          Comp : Entity_Id;
17481
17482       begin
17483          if Nkind (N) = N_Discriminant_Specification then
17484             Comp := First_Discriminant (Typ);
17485             while Present (Comp) loop
17486                if Chars (Comp) = Chars (Defining_Identifier (N)) then
17487                   Set_Defining_Identifier (N, Comp);
17488                   exit;
17489                end if;
17490
17491                Next_Discriminant (Comp);
17492             end loop;
17493
17494          elsif Nkind (N) = N_Component_Declaration then
17495             Comp := First_Component (Typ);
17496             while Present (Comp) loop
17497                if Chars (Comp) = Chars (Defining_Identifier (N)) then
17498                   Set_Defining_Identifier (N, Comp);
17499                   exit;
17500                end if;
17501
17502                Next_Component (Comp);
17503             end loop;
17504          end if;
17505
17506          return OK;
17507       end Process;
17508
17509       procedure Replace is new Traverse_Proc (Process);
17510
17511    --  Start of processing for Replace_Components
17512
17513    begin
17514       Replace (Decl);
17515    end Replace_Components;
17516
17517    -------------------------------
17518    -- Set_Completion_Referenced --
17519    -------------------------------
17520
17521    procedure Set_Completion_Referenced (E : Entity_Id) is
17522    begin
17523       --  If in main unit, mark entity that is a completion as referenced,
17524       --  warnings go on the partial view when needed.
17525
17526       if In_Extended_Main_Source_Unit (E) then
17527          Set_Referenced (E);
17528       end if;
17529    end Set_Completion_Referenced;
17530
17531    ---------------------
17532    -- Set_Fixed_Range --
17533    ---------------------
17534
17535    --  The range for fixed-point types is complicated by the fact that we
17536    --  do not know the exact end points at the time of the declaration. This
17537    --  is true for three reasons:
17538
17539    --     A size clause may affect the fudging of the end-points
17540    --     A small clause may affect the values of the end-points
17541    --     We try to include the end-points if it does not affect the size
17542
17543    --  This means that the actual end-points must be established at the point
17544    --  when the type is frozen. Meanwhile, we first narrow the range as
17545    --  permitted (so that it will fit if necessary in a small specified size),
17546    --  and then build a range subtree with these narrowed bounds.
17547
17548    --  Set_Fixed_Range constructs the range from real literal values, and sets
17549    --  the range as the Scalar_Range of the given fixed-point type entity.
17550
17551    --  The parent of this range is set to point to the entity so that it is
17552    --  properly hooked into the tree (unlike normal Scalar_Range entries for
17553    --  other scalar types, which are just pointers to the range in the
17554    --  original tree, this would otherwise be an orphan).
17555
17556    --  The tree is left unanalyzed. When the type is frozen, the processing
17557    --  in Freeze.Freeze_Fixed_Point_Type notices that the range is not
17558    --  analyzed, and uses this as an indication that it should complete
17559    --  work on the range (it will know the final small and size values).
17560
17561    procedure Set_Fixed_Range
17562      (E   : Entity_Id;
17563       Loc : Source_Ptr;
17564       Lo  : Ureal;
17565       Hi  : Ureal)
17566    is
17567       S : constant Node_Id :=
17568             Make_Range (Loc,
17569               Low_Bound  => Make_Real_Literal (Loc, Lo),
17570               High_Bound => Make_Real_Literal (Loc, Hi));
17571    begin
17572       Set_Scalar_Range (E, S);
17573       Set_Parent (S, E);
17574    end Set_Fixed_Range;
17575
17576    ----------------------------------
17577    -- Set_Scalar_Range_For_Subtype --
17578    ----------------------------------
17579
17580    procedure Set_Scalar_Range_For_Subtype
17581      (Def_Id : Entity_Id;
17582       R      : Node_Id;
17583       Subt   : Entity_Id)
17584    is
17585       Kind : constant Entity_Kind :=  Ekind (Def_Id);
17586
17587    begin
17588       Set_Scalar_Range (Def_Id, R);
17589
17590       --  We need to link the range into the tree before resolving it so
17591       --  that types that are referenced, including importantly the subtype
17592       --  itself, are properly frozen (Freeze_Expression requires that the
17593       --  expression be properly linked into the tree). Of course if it is
17594       --  already linked in, then we do not disturb the current link.
17595
17596       if No (Parent (R)) then
17597          Set_Parent (R, Def_Id);
17598       end if;
17599
17600       --  Reset the kind of the subtype during analysis of the range, to
17601       --  catch possible premature use in the bounds themselves.
17602
17603       Set_Ekind (Def_Id, E_Void);
17604       Process_Range_Expr_In_Decl (R, Subt);
17605       Set_Ekind (Def_Id, Kind);
17606    end Set_Scalar_Range_For_Subtype;
17607
17608    --------------------------------------------------------
17609    -- Set_Stored_Constraint_From_Discriminant_Constraint --
17610    --------------------------------------------------------
17611
17612    procedure Set_Stored_Constraint_From_Discriminant_Constraint
17613      (E : Entity_Id)
17614    is
17615    begin
17616       --  Make sure set if encountered during Expand_To_Stored_Constraint
17617
17618       Set_Stored_Constraint (E, No_Elist);
17619
17620       --  Give it the right value
17621
17622       if Is_Constrained (E) and then Has_Discriminants (E) then
17623          Set_Stored_Constraint (E,
17624            Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
17625       end if;
17626    end Set_Stored_Constraint_From_Discriminant_Constraint;
17627
17628    -------------------------------------
17629    -- Signed_Integer_Type_Declaration --
17630    -------------------------------------
17631
17632    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17633       Implicit_Base : Entity_Id;
17634       Base_Typ      : Entity_Id;
17635       Lo_Val        : Uint;
17636       Hi_Val        : Uint;
17637       Errs          : Boolean := False;
17638       Lo            : Node_Id;
17639       Hi            : Node_Id;
17640
17641       function Can_Derive_From (E : Entity_Id) return Boolean;
17642       --  Determine whether given bounds allow derivation from specified type
17643
17644       procedure Check_Bound (Expr : Node_Id);
17645       --  Check bound to make sure it is integral and static. If not, post
17646       --  appropriate error message and set Errs flag
17647
17648       ---------------------
17649       -- Can_Derive_From --
17650       ---------------------
17651
17652       --  Note we check both bounds against both end values, to deal with
17653       --  strange types like ones with a range of 0 .. -12341234.
17654
17655       function Can_Derive_From (E : Entity_Id) return Boolean is
17656          Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
17657          Hi : constant Uint := Expr_Value (Type_High_Bound (E));
17658       begin
17659          return Lo <= Lo_Val and then Lo_Val <= Hi
17660                   and then
17661                 Lo <= Hi_Val and then Hi_Val <= Hi;
17662       end Can_Derive_From;
17663
17664       -----------------
17665       -- Check_Bound --
17666       -----------------
17667
17668       procedure Check_Bound (Expr : Node_Id) is
17669       begin
17670          --  If a range constraint is used as an integer type definition, each
17671          --  bound of the range must be defined by a static expression of some
17672          --  integer type, but the two bounds need not have the same integer
17673          --  type (Negative bounds are allowed.) (RM 3.5.4)
17674
17675          if not Is_Integer_Type (Etype (Expr)) then
17676             Error_Msg_N
17677               ("integer type definition bounds must be of integer type", Expr);
17678             Errs := True;
17679
17680          elsif not Is_OK_Static_Expression (Expr) then
17681             Flag_Non_Static_Expr
17682               ("non-static expression used for integer type bound!", Expr);
17683             Errs := True;
17684
17685          --  The bounds are folded into literals, and we set their type to be
17686          --  universal, to avoid typing difficulties: we cannot set the type
17687          --  of the literal to the new type, because this would be a forward
17688          --  reference for the back end,  and if the original type is user-
17689          --  defined this can lead to spurious semantic errors (e.g. 2928-003).
17690
17691          else
17692             if Is_Entity_Name (Expr) then
17693                Fold_Uint (Expr, Expr_Value (Expr), True);
17694             end if;
17695
17696             Set_Etype (Expr, Universal_Integer);
17697          end if;
17698       end Check_Bound;
17699
17700    --  Start of processing for Signed_Integer_Type_Declaration
17701
17702    begin
17703       --  Create an anonymous base type
17704
17705       Implicit_Base :=
17706         Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
17707
17708       --  Analyze and check the bounds, they can be of any integer type
17709
17710       Lo := Low_Bound (Def);
17711       Hi := High_Bound (Def);
17712
17713       --  Arbitrarily use Integer as the type if either bound had an error
17714
17715       if Hi = Error or else Lo = Error then
17716          Base_Typ := Any_Integer;
17717          Set_Error_Posted (T, True);
17718
17719       --  Here both bounds are OK expressions
17720
17721       else
17722          Analyze_And_Resolve (Lo, Any_Integer);
17723          Analyze_And_Resolve (Hi, Any_Integer);
17724
17725          Check_Bound (Lo);
17726          Check_Bound (Hi);
17727
17728          if Errs then
17729             Hi := Type_High_Bound (Standard_Long_Long_Integer);
17730             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
17731          end if;
17732
17733          --  Find type to derive from
17734
17735          Lo_Val := Expr_Value (Lo);
17736          Hi_Val := Expr_Value (Hi);
17737
17738          if Can_Derive_From (Standard_Short_Short_Integer) then
17739             Base_Typ := Base_Type (Standard_Short_Short_Integer);
17740
17741          elsif Can_Derive_From (Standard_Short_Integer) then
17742             Base_Typ := Base_Type (Standard_Short_Integer);
17743
17744          elsif Can_Derive_From (Standard_Integer) then
17745             Base_Typ := Base_Type (Standard_Integer);
17746
17747          elsif Can_Derive_From (Standard_Long_Integer) then
17748             Base_Typ := Base_Type (Standard_Long_Integer);
17749
17750          elsif Can_Derive_From (Standard_Long_Long_Integer) then
17751             Base_Typ := Base_Type (Standard_Long_Long_Integer);
17752
17753          else
17754             Base_Typ := Base_Type (Standard_Long_Long_Integer);
17755             Error_Msg_N ("integer type definition bounds out of range", Def);
17756             Hi := Type_High_Bound (Standard_Long_Long_Integer);
17757             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
17758          end if;
17759       end if;
17760
17761       --  Complete both implicit base and declared first subtype entities
17762
17763       Set_Etype          (Implicit_Base, Base_Typ);
17764       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
17765       Set_Size_Info      (Implicit_Base,                (Base_Typ));
17766       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
17767       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17768
17769       Set_Ekind          (T, E_Signed_Integer_Subtype);
17770       Set_Etype          (T, Implicit_Base);
17771
17772       Set_Size_Info      (T,                (Implicit_Base));
17773       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
17774       Set_Scalar_Range   (T, Def);
17775       Set_RM_Size        (T, UI_From_Int (Minimum_Size (T)));
17776       Set_Is_Constrained (T);
17777    end Signed_Integer_Type_Declaration;
17778
17779 end Sem_Ch3;