OSDN Git Service

2004-07-26 Arnaud Charlet <charlet@act-europe.fr>
[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-2004, Free Software Foundation, Inc.         --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Checks;   use Checks;
29 with Elists;   use Elists;
30 with Einfo;    use Einfo;
31 with Errout;   use Errout;
32 with Eval_Fat; use Eval_Fat;
33 with Exp_Ch3;  use Exp_Ch3;
34 with Exp_Dist; use Exp_Dist;
35 with Exp_Tss;  use Exp_Tss;
36 with Exp_Util; use Exp_Util;
37 with Freeze;   use Freeze;
38 with Itypes;   use Itypes;
39 with Layout;   use Layout;
40 with Lib;      use Lib;
41 with Lib.Xref; use Lib.Xref;
42 with Namet;    use Namet;
43 with Nmake;    use Nmake;
44 with Opt;      use Opt;
45 with Restrict; use Restrict;
46 with Rident;   use Rident;
47 with Rtsfind;  use Rtsfind;
48 with Sem;      use Sem;
49 with Sem_Case; use Sem_Case;
50 with Sem_Cat;  use Sem_Cat;
51 with Sem_Ch6;  use Sem_Ch6;
52 with Sem_Ch7;  use Sem_Ch7;
53 with Sem_Ch8;  use Sem_Ch8;
54 with Sem_Ch13; use Sem_Ch13;
55 with Sem_Disp; use Sem_Disp;
56 with Sem_Dist; use Sem_Dist;
57 with Sem_Elim; use Sem_Elim;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Mech; use Sem_Mech;
60 with Sem_Res;  use Sem_Res;
61 with Sem_Smem; use Sem_Smem;
62 with Sem_Type; use Sem_Type;
63 with Sem_Util; use Sem_Util;
64 with Sem_Warn; use Sem_Warn;
65 with Stand;    use Stand;
66 with Sinfo;    use Sinfo;
67 with Snames;   use Snames;
68 with Tbuild;   use Tbuild;
69 with Ttypes;   use Ttypes;
70 with Uintp;    use Uintp;
71 with Urealp;   use Urealp;
72
73 package body Sem_Ch3 is
74
75    -----------------------
76    -- Local Subprograms --
77    -----------------------
78
79    procedure Build_Derived_Type
80      (N             : Node_Id;
81       Parent_Type   : Entity_Id;
82       Derived_Type  : Entity_Id;
83       Is_Completion : Boolean;
84       Derive_Subps  : Boolean := True);
85    --  Create and decorate a Derived_Type given the Parent_Type entity.
86    --  N is the N_Full_Type_Declaration node containing the derived type
87    --  definition. Parent_Type is the entity for the parent type in the derived
88    --  type definition and Derived_Type the actual derived type. Is_Completion
89    --  must be set to False if Derived_Type is the N_Defining_Identifier node
90    --  in N (ie Derived_Type = Defining_Identifier (N)). In this case N is not
91    --  the completion of a private type declaration. If Is_Completion is
92    --  set to True, N is the completion of a private type declaration and
93    --  Derived_Type is different from the defining identifier inside N (i.e.
94    --  Derived_Type /= Defining_Identifier (N)). Derive_Subps indicates whether
95    --  the parent subprograms should be derived. The only case where this
96    --  parameter is False is when Build_Derived_Type is recursively called to
97    --  process an implicit derived full type for a type derived from a private
98    --  type (in that case the subprograms must only be derived for the private
99    --  view of the type).
100    --  ??? These flags need a bit of re-examination and re-documentation:
101    --  ???  are they both necessary (both seem related to the recursion)?
102
103    procedure Build_Derived_Access_Type
104      (N            : Node_Id;
105       Parent_Type  : Entity_Id;
106       Derived_Type : Entity_Id);
107    --  Subsidiary procedure to Build_Derived_Type. For a derived access type,
108    --  create an implicit base if the parent type is constrained or if the
109    --  subtype indication has a constraint.
110
111    procedure Build_Derived_Array_Type
112      (N            : Node_Id;
113       Parent_Type  : Entity_Id;
114       Derived_Type : Entity_Id);
115    --  Subsidiary procedure to Build_Derived_Type. For a derived array type,
116    --  create an implicit base if the parent type is constrained or if the
117    --  subtype indication has a constraint.
118
119    procedure Build_Derived_Concurrent_Type
120      (N            : Node_Id;
121       Parent_Type  : Entity_Id;
122       Derived_Type : Entity_Id);
123    --  Subsidiary procedure to Build_Derived_Type. For a derived task or pro-
124    --  tected type, inherit entries and protected subprograms, check legality
125    --  of discriminant constraints if any.
126
127    procedure Build_Derived_Enumeration_Type
128      (N            : Node_Id;
129       Parent_Type  : Entity_Id;
130       Derived_Type : Entity_Id);
131    --  Subsidiary procedure to Build_Derived_Type. For a derived enumeration
132    --  type, we must create a new list of literals. Types derived from
133    --  Character and Wide_Character are special-cased.
134
135    procedure Build_Derived_Numeric_Type
136      (N            : Node_Id;
137       Parent_Type  : Entity_Id;
138       Derived_Type : Entity_Id);
139    --  Subsidiary procedure to Build_Derived_Type. For numeric types, create
140    --  an anonymous base type, and propagate constraint to subtype if needed.
141
142    procedure Build_Derived_Private_Type
143      (N            : Node_Id;
144       Parent_Type  : Entity_Id;
145       Derived_Type : Entity_Id;
146       Is_Completion : Boolean;
147       Derive_Subps  : Boolean := True);
148    --  Subsidiary procedure to Build_Derived_Type. This procedure is complex
149    --  because the parent may or may not have a completion, and the derivation
150    --  may itself be a completion.
151
152    procedure Build_Derived_Record_Type
153      (N            : Node_Id;
154       Parent_Type  : Entity_Id;
155       Derived_Type : Entity_Id;
156       Derive_Subps : Boolean := True);
157    --  Subsidiary procedure to Build_Derived_Type and
158    --  Analyze_Private_Extension_Declaration used for tagged and untagged
159    --  record types. All parameters are as in Build_Derived_Type except that
160    --  N, in addition to being an N_Full_Type_Declaration node, can also be an
161    --  N_Private_Extension_Declaration node. See the definition of this routine
162    --  for much more info. Derive_Subps indicates whether subprograms should
163    --  be derived from the parent type. The only case where Derive_Subps is
164    --  False is for an implicit derived full type for a type derived from a
165    --  private type (see Build_Derived_Type).
166
167    function Inherit_Components
168      (N             : Node_Id;
169       Parent_Base   : Entity_Id;
170       Derived_Base  : Entity_Id;
171       Is_Tagged     : Boolean;
172       Inherit_Discr : Boolean;
173       Discs         : Elist_Id) return Elist_Id;
174    --  Called from Build_Derived_Record_Type to inherit the components of
175    --  Parent_Base (a base type) into the Derived_Base (the derived base type).
176    --  For more information on derived types and component inheritance please
177    --  consult the comment above the body of Build_Derived_Record_Type.
178    --
179    --    N is the original derived type declaration.
180    --
181    --    Is_Tagged is set if we are dealing with tagged types.
182    --
183    --    If Inherit_Discr is set, Derived_Base inherits its discriminants
184    --    from Parent_Base, otherwise no discriminants are inherited.
185    --
186    --    Discs gives the list of constraints that apply to Parent_Base in the
187    --    derived type declaration. If Discs is set to No_Elist, then we have
188    --    the following situation:
189    --
190    --      type Parent (D1..Dn : ..) is [tagged] record ...;
191    --      type Derived is new Parent [with ...];
192    --
193    --    which gets treated as
194    --
195    --      type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
196    --
197    --  For untagged types the returned value is an association list. The list
198    --  starts from the association (Parent_Base => Derived_Base), and then it
199    --  contains a sequence of the associations of the form
200    --
201    --    (Old_Component => New_Component),
202    --
203    --  where Old_Component is the Entity_Id of a component in Parent_Base
204    --  and New_Component is the Entity_Id of the corresponding component
205    --  in Derived_Base. For untagged records, this association list is
206    --  needed when copying the record declaration for the derived base.
207    --  In the tagged case the value returned is irrelevant.
208
209    procedure Build_Discriminal (Discrim : Entity_Id);
210    --  Create the discriminal corresponding to discriminant Discrim, that is
211    --  the parameter corresponding to Discrim to be used in initialization
212    --  procedures for the type where Discrim is a discriminant. Discriminals
213    --  are not used during semantic analysis, and are not fully defined
214    --  entities until expansion. Thus they are not given a scope until
215    --  initialization procedures are built.
216
217    function Build_Discriminant_Constraints
218      (T           : Entity_Id;
219       Def         : Node_Id;
220       Derived_Def : Boolean := False) return Elist_Id;
221    --  Validate discriminant constraints, and return the list of the
222    --  constraints in order of discriminant declarations. T is the
223    --  discriminated unconstrained type. Def is the N_Subtype_Indication
224    --  node where the discriminants constraints for T are specified.
225    --  Derived_Def is True if we are building the discriminant constraints
226    --  in a derived type definition of the form "type D (...) is new T (xxx)".
227    --  In this case T is the parent type and Def is the constraint "(xxx)" on
228    --  T and this routine sets the Corresponding_Discriminant field of the
229    --  discriminants in the derived type D to point to the corresponding
230    --  discriminants in the parent type T.
231
232    procedure Build_Discriminated_Subtype
233      (T           : Entity_Id;
234       Def_Id      : Entity_Id;
235       Elist       : Elist_Id;
236       Related_Nod : Node_Id;
237       For_Access  : Boolean := False);
238    --  Subsidiary procedure to Constrain_Discriminated_Type and to
239    --  Process_Incomplete_Dependents. Given
240    --
241    --     T (a possibly discriminated base type)
242    --     Def_Id (a very partially built subtype for T),
243    --
244    --  the call completes Def_Id to be the appropriate E_*_Subtype.
245    --
246    --  The Elist is the list of discriminant constraints if any (it is set to
247    --  No_Elist if T is not a discriminated type, and to an empty list if
248    --  T has discriminants but there are no discriminant constraints). The
249    --  Related_Nod is the same as Decl_Node in Create_Constrained_Components.
250    --  The For_Access says whether or not this subtype is really constraining
251    --  an access type. That is its sole purpose is the designated type of an
252    --  access type -- in which case a Private_Subtype Is_For_Access_Subtype
253    --  is built to avoid freezing T when the access subtype is frozen.
254
255    function Build_Scalar_Bound
256      (Bound : Node_Id;
257       Par_T : Entity_Id;
258       Der_T : Entity_Id) return Node_Id;
259    --  The bounds of a derived scalar type are conversions of the bounds of
260    --  the parent type. Optimize the representation if the bounds are literals.
261    --  Needs a more complete spec--what are the parameters exactly, and what
262    --  exactly is the returned value, and how is Bound affected???
263
264    procedure Build_Underlying_Full_View
265      (N   : Node_Id;
266       Typ : Entity_Id;
267       Par : Entity_Id);
268    --  If the completion of a private type is itself derived from a private
269    --  type, or if the full view of a private subtype is itself private, the
270    --  back-end has no way to compute the actual size of this type. We build
271    --  an internal subtype declaration of the proper parent type to convey
272    --  this information. This extra mechanism is needed because a full
273    --  view cannot itself have a full view (it would get clobbered during
274    --  view exchanges).
275
276    procedure Check_Access_Discriminant_Requires_Limited
277      (D   : Node_Id;
278       Loc : Node_Id);
279    --  Check the restriction that the type to which an access discriminant
280    --  belongs must be a concurrent type or a descendant of a type with
281    --  the reserved word 'limited' in its declaration.
282
283    procedure Check_Delta_Expression (E : Node_Id);
284    --  Check that the expression represented by E is suitable for use
285    --  as a delta expression, i.e. it is of real type and is static.
286
287    procedure Check_Digits_Expression (E : Node_Id);
288    --  Check that the expression represented by E is suitable for use as
289    --  a digits expression, i.e. it is of integer type, positive and static.
290
291    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
292    --  Validate the initialization of an object declaration. T is the
293    --  required type, and Exp is the initialization expression.
294
295    procedure Check_Or_Process_Discriminants
296      (N    : Node_Id;
297       T    : Entity_Id;
298       Prev : Entity_Id := Empty);
299    --  If T is the full declaration of an incomplete or private type, check
300    --  the conformance of the discriminants, otherwise process them. Prev
301    --  is the entity of the partial declaration, if any.
302
303    procedure Check_Real_Bound (Bound : Node_Id);
304    --  Check given bound for being of real type and static. If not, post an
305    --  appropriate message, and rewrite the bound with the real literal zero.
306
307    procedure Constant_Redeclaration
308      (Id : Entity_Id;
309       N  : Node_Id;
310       T  : out Entity_Id);
311    --  Various checks on legality of full declaration of deferred constant.
312    --  Id is the entity for the redeclaration, N is the N_Object_Declaration,
313    --  node. The caller has not yet set any attributes of this entity.
314
315    procedure Convert_Scalar_Bounds
316      (N            : Node_Id;
317       Parent_Type  : Entity_Id;
318       Derived_Type : Entity_Id;
319       Loc          : Source_Ptr);
320    --  For derived scalar types, convert the bounds in the type definition
321    --  to the derived type, and complete their analysis. Given a constraint
322    --  of the form:
323    --                   ..  new T range Lo .. Hi;
324    --  Lo and Hi are analyzed and resolved with T'Base, the parent_type.
325    --  The bounds of the derived type (the anonymous base) are copies of
326    --  Lo and Hi.  Finally, the bounds of the derived subtype are conversions
327    --  of those bounds to the derived_type, so that their typing is
328    --  consistent.
329
330    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
331    --  Copies attributes from array base type T2 to array base type T1.
332    --  Copies only attributes that apply to base types, but not subtypes.
333
334    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
335    --  Copies attributes from array subtype T2 to array subtype T1. Copies
336    --  attributes that apply to both subtypes and base types.
337
338    procedure Create_Constrained_Components
339      (Subt        : Entity_Id;
340       Decl_Node   : Node_Id;
341       Typ         : Entity_Id;
342       Constraints : Elist_Id);
343    --  Build the list of entities for a constrained discriminated record
344    --  subtype. If a component depends on a discriminant, replace its subtype
345    --  using the discriminant values in the discriminant constraint.
346    --  Subt is the defining identifier for the subtype whose list of
347    --  constrained entities we will create. Decl_Node is the type declaration
348    --  node where we will attach all the itypes created. Typ is the base
349    --  discriminated type for the subtype Subt. Constraints is the list of
350    --  discriminant constraints for Typ.
351
352    function Constrain_Component_Type
353      (Compon_Type     : Entity_Id;
354       Constrained_Typ : Entity_Id;
355       Related_Node    : Node_Id;
356       Typ             : Entity_Id;
357       Constraints     : Elist_Id) return Entity_Id;
358    --  Given a discriminated base type Typ, a list of discriminant constraint
359    --  Constraints for Typ and the type of a component of Typ, Compon_Type,
360    --  create and return the type corresponding to Compon_type where all
361    --  discriminant references are replaced with the corresponding
362    --  constraint. If no discriminant references occur in Compon_Typ then
363    --  return it as is. Constrained_Typ is the final constrained subtype to
364    --  which the constrained Compon_Type belongs. Related_Node is the node
365    --  where we will attach all the itypes created.
366
367    procedure Constrain_Access
368      (Def_Id      : in out Entity_Id;
369       S           : Node_Id;
370       Related_Nod : Node_Id);
371    --  Apply a list of constraints to an access type. If Def_Id is empty,
372    --  it is an anonymous type created for a subtype indication. In that
373    --  case it is created in the procedure and attached to Related_Nod.
374
375    procedure Constrain_Array
376      (Def_Id      : in out Entity_Id;
377       SI          : Node_Id;
378       Related_Nod : Node_Id;
379       Related_Id  : Entity_Id;
380       Suffix      : Character);
381    --  Apply a list of index constraints to an unconstrained array type. The
382    --  first parameter is the entity for the resulting subtype. A value of
383    --  Empty for Def_Id indicates that an implicit type must be created, but
384    --  creation is delayed (and must be done by this procedure) because other
385    --  subsidiary implicit types must be created first (which is why Def_Id
386    --  is an in/out parameter). The second parameter is a subtype indication
387    --  node for the constrained array to be created (e.g. something of the
388    --  form string (1 .. 10)). Related_Nod gives the place where this type
389    --  has to be inserted in the tree. The Related_Id and Suffix parameters
390    --  are used to build the associated Implicit type name.
391
392    procedure Constrain_Concurrent
393      (Def_Id      : in out Entity_Id;
394       SI          : Node_Id;
395       Related_Nod : Node_Id;
396       Related_Id  : Entity_Id;
397       Suffix      : Character);
398    --  Apply list of discriminant constraints to an unconstrained concurrent
399    --  type.
400    --
401    --    SI is the N_Subtype_Indication node containing the constraint and
402    --    the unconstrained type to constrain.
403    --
404    --    Def_Id is the entity for the resulting constrained subtype. A
405    --    value of Empty for Def_Id indicates that an implicit type must be
406    --    created, but creation is delayed (and must be done by this procedure)
407    --    because other subsidiary implicit types must be created first (which
408    --    is why Def_Id is an in/out parameter).
409    --
410    --    Related_Nod gives the place where this type has to be inserted
411    --    in the tree
412    --
413    --  The last two arguments are used to create its external name if needed.
414
415    function Constrain_Corresponding_Record
416      (Prot_Subt   : Entity_Id;
417       Corr_Rec    : Entity_Id;
418       Related_Nod : Node_Id;
419       Related_Id  : Entity_Id) return Entity_Id;
420    --  When constraining a protected type or task type with discriminants,
421    --  constrain the corresponding record with the same discriminant values.
422
423    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
424    --  Constrain a decimal fixed point type with a digits constraint and/or a
425    --  range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
426
427    procedure Constrain_Discriminated_Type
428      (Def_Id      : Entity_Id;
429       S           : Node_Id;
430       Related_Nod : Node_Id;
431       For_Access  : Boolean := False);
432    --  Process discriminant constraints of composite type. Verify that values
433    --  have been provided for all discriminants, that the original type is
434    --  unconstrained, and that the types of the supplied expressions match
435    --  the discriminant types. The first three parameters are like in routine
436    --  Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
437    --  of For_Access.
438
439    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
440    --  Constrain an enumeration type with a range constraint. This is
441    --  identical to Constrain_Integer, but for the Ekind of the
442    --  resulting subtype.
443
444    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
445    --  Constrain a floating point type with either a digits constraint
446    --  and/or a range constraint, building a E_Floating_Point_Subtype.
447
448    procedure Constrain_Index
449      (Index        : Node_Id;
450       S            : Node_Id;
451       Related_Nod  : Node_Id;
452       Related_Id   : Entity_Id;
453       Suffix       : Character;
454       Suffix_Index : Nat);
455    --  Process an index constraint in a constrained array declaration.
456    --  The constraint can be a subtype name, or a range with or without
457    --  an explicit subtype mark. The index is the corresponding index of the
458    --  unconstrained array. The Related_Id and Suffix parameters are used to
459    --  build the associated Implicit type name.
460
461    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
462    --  Build subtype of a signed or modular integer type.
463
464    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
465    --  Constrain an ordinary fixed point type with a range constraint, and
466    --  build an E_Ordinary_Fixed_Point_Subtype entity.
467
468    procedure Copy_And_Swap (Priv, Full : Entity_Id);
469    --  Copy the Priv entity into the entity of its full declaration
470    --  then swap the two entities in such a manner that the former private
471    --  type is now seen as a full type.
472
473    procedure Decimal_Fixed_Point_Type_Declaration
474      (T   : Entity_Id;
475       Def : Node_Id);
476    --  Create a new decimal fixed point type, and apply the constraint to
477    --  obtain a subtype of this new type.
478
479    procedure Complete_Private_Subtype
480      (Priv        : Entity_Id;
481       Full        : Entity_Id;
482       Full_Base   : Entity_Id;
483       Related_Nod : Node_Id);
484    --  Complete the implicit full view of a private subtype by setting
485    --  the appropriate semantic fields. If the full view of the parent is
486    --  a record type, build constrained components of subtype.
487
488    procedure Derived_Standard_Character
489      (N             : Node_Id;
490       Parent_Type   : Entity_Id;
491       Derived_Type  : Entity_Id);
492    --  Subsidiary procedure to Build_Derived_Enumeration_Type which handles
493    --  derivations from types Standard.Character and Standard.Wide_Character.
494
495    procedure Derived_Type_Declaration
496      (T             : Entity_Id;
497       N             : Node_Id;
498       Is_Completion : Boolean);
499    --  Process a derived type declaration. This routine will invoke
500    --  Build_Derived_Type to process the actual derived type definition.
501    --  Parameters N and Is_Completion have the same meaning as in
502    --  Build_Derived_Type. T is the N_Defining_Identifier for the entity
503    --  defined in the N_Full_Type_Declaration node N, that is T is the
504    --  derived type.
505
506    function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id;
507    --  Given a subtype indication S (which is really an N_Subtype_Indication
508    --  node or a plain N_Identifier), find the type of the subtype mark.
509
510    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
511    --  Insert each literal in symbol table, as an overloadable identifier
512    --  Each enumeration type is mapped into a sequence of integers, and
513    --  each literal is defined as a constant with integer value. If any
514    --  of the literals are character literals, the type is a character
515    --  type, which means that strings are legal aggregates for arrays of
516    --  components of the type.
517
518    function Expand_To_Stored_Constraint
519      (Typ        : Entity_Id;
520       Constraint : Elist_Id) return Elist_Id;
521    --  Given a Constraint (ie a list of expressions) on the discriminants of
522    --  Typ, expand it into a constraint on the stored discriminants and
523    --  return the new list of expressions constraining the stored
524    --  discriminants.
525
526    function Find_Type_Of_Object
527      (Obj_Def     : Node_Id;
528       Related_Nod : Node_Id) return Entity_Id;
529    --  Get type entity for object referenced by Obj_Def, attaching the
530    --  implicit types generated to Related_Nod
531
532    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
533    --  Create a new float, and apply the constraint to obtain subtype of it
534
535    function Has_Range_Constraint (N : Node_Id) return Boolean;
536    --  Given an N_Subtype_Indication node N, return True if a range constraint
537    --  is present, either directly, or as part of a digits or delta constraint.
538    --  In addition, a digits constraint in the decimal case returns True, since
539    --  it establishes a default range if no explicit range is present.
540
541    function Is_Valid_Constraint_Kind
542      (T_Kind          : Type_Kind;
543       Constraint_Kind : Node_Kind) return Boolean;
544    --  Returns True if it is legal to apply the given kind of constraint
545    --  to the given kind of type (index constraint to an array type,
546    --  for example).
547
548    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
549    --  Create new modular type. Verify that modulus is in  bounds and is
550    --  a power of two (implementation restriction).
551
552    procedure New_Concatenation_Op (Typ : Entity_Id);
553    --  Create an abbreviated declaration for an operator in order to
554    --  materialize concatenation on array types.
555
556    procedure Ordinary_Fixed_Point_Type_Declaration
557      (T   : Entity_Id;
558       Def : Node_Id);
559    --  Create a new ordinary fixed point type, and apply the constraint
560    --  to obtain subtype of it.
561
562    procedure Prepare_Private_Subtype_Completion
563      (Id          : Entity_Id;
564       Related_Nod : Node_Id);
565    --  Id is a subtype of some private type. Creates the full declaration
566    --  associated with Id whenever possible, i.e. when the full declaration
567    --  of the base type is already known. Records each subtype into
568    --  Private_Dependents of the base type.
569
570    procedure Process_Incomplete_Dependents
571      (N      : Node_Id;
572       Full_T : Entity_Id;
573       Inc_T  : Entity_Id);
574    --  Process all entities that depend on an incomplete type. There include
575    --  subtypes, subprogram types that mention the incomplete type in their
576    --  profiles, and subprogram with access parameters that designate the
577    --  incomplete type.
578
579    --  Inc_T is the defining identifier of an incomplete type declaration, its
580    --  Ekind is E_Incomplete_Type.
581    --
582    --    N is the corresponding N_Full_Type_Declaration for Inc_T.
583    --
584    --    Full_T is N's defining identifier.
585    --
586    --  Subtypes of incomplete types with discriminants are completed when the
587    --  parent type is. This is simpler than private subtypes, because they can
588    --  only appear in the same scope, and there is no need to exchange views.
589    --  Similarly, access_to_subprogram types may have a parameter or a return
590    --  type that is an incomplete type, and that must be replaced with the
591    --  full type.
592
593    --  If the full type is tagged, subprogram with access parameters that
594    --  designated the incomplete may be primitive operations of the full type,
595    --  and have to be processed accordingly.
596
597    procedure Process_Real_Range_Specification (Def : Node_Id);
598    --  Given the type definition for a real type, this procedure processes
599    --  and checks the real range specification of this type definition if
600    --  one is present. If errors are found, error messages are posted, and
601    --  the Real_Range_Specification of Def is reset to Empty.
602
603    procedure Record_Type_Declaration
604      (T    : Entity_Id;
605       N    : Node_Id;
606       Prev : Entity_Id);
607    --  Process a record type declaration (for both untagged and tagged
608    --  records). Parameters T and N are exactly like in procedure
609    --  Derived_Type_Declaration, except that no flag Is_Completion is
610    --  needed for this routine. If this is the completion of an incomplete
611    --  type declaration, Prev is the entity of the incomplete declaration,
612    --  used for cross-referencing. Otherwise Prev = T.
613
614    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
615    --  This routine is used to process the actual record type definition
616    --  (both for untagged and tagged records). Def is a record type
617    --  definition node. This procedure analyzes the components in this
618    --  record type definition. Prev_T is the entity for the enclosing record
619    --  type. It is provided so that its Has_Task flag can be set if any of
620    --  the component have Has_Task set. If the declaration is the completion
621    --  of an incomplete type declaration, Prev_T is the original incomplete
622    --  type, whose full view is the record type.
623
624    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
625    --  Subsidiary to Build_Derived_Record_Type. For untagged records, we
626    --  build a copy of the declaration tree of the parent, and we create
627    --  independently the list of components for the derived type. Semantic
628    --  information uses the component entities, but record representation
629    --  clauses are validated on the declaration tree. This procedure replaces
630    --  discriminants and components in the declaration with those that have
631    --  been created by Inherit_Components.
632
633    procedure Set_Fixed_Range
634      (E   : Entity_Id;
635       Loc : Source_Ptr;
636       Lo  : Ureal;
637       Hi  : Ureal);
638    --  Build a range node with the given bounds and set it as the Scalar_Range
639    --  of the given fixed-point type entity. Loc is the source location used
640    --  for the constructed range. See body for further details.
641
642    procedure Set_Scalar_Range_For_Subtype
643      (Def_Id : Entity_Id;
644       R      : Node_Id;
645       Subt   : Entity_Id);
646    --  This routine is used to set the scalar range field for a subtype
647    --  given Def_Id, the entity for the subtype, and R, the range expression
648    --  for the scalar range. Subt provides the parent subtype to be used
649    --  to analyze, resolve, and check the given range.
650
651    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
652    --  Create a new signed integer entity, and apply the constraint to obtain
653    --  the required first named subtype of this type.
654
655    procedure Set_Stored_Constraint_From_Discriminant_Constraint
656      (E : Entity_Id);
657    --  E is some record type. This routine computes E's Stored_Constraint
658    --  from its Discriminant_Constraint.
659
660    -----------------------
661    -- Access_Definition --
662    -----------------------
663
664    function Access_Definition
665      (Related_Nod : Node_Id;
666       N           : Node_Id) return Entity_Id
667    is
668       Anon_Type : constant Entity_Id :=
669                     Create_Itype (E_Anonymous_Access_Type, Related_Nod,
670                                   Scope_Id => Scope (Current_Scope));
671       Desig_Type : Entity_Id;
672
673    begin
674       if Is_Entry (Current_Scope)
675         and then Is_Task_Type (Etype (Scope (Current_Scope)))
676       then
677          Error_Msg_N ("task entries cannot have access parameters", N);
678       end if;
679
680       --  Ada 2005 (AI-254): In case of anonymous access to subprograms
681       --  call the corresponding semantic routine
682
683       if Present (Access_To_Subprogram_Definition (N)) then
684          Access_Subprogram_Declaration
685            (T_Name => Anon_Type,
686             T_Def  => Access_To_Subprogram_Definition (N));
687
688          if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
689             Set_Ekind
690               (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
691          else
692             Set_Ekind
693               (Anon_Type, E_Anonymous_Access_Subprogram_Type);
694          end if;
695
696          return Anon_Type;
697       end if;
698
699       Find_Type (Subtype_Mark (N));
700       Desig_Type := Entity (Subtype_Mark (N));
701
702       Set_Directly_Designated_Type
703                              (Anon_Type, Desig_Type);
704       Set_Etype              (Anon_Type, Anon_Type);
705       Init_Size_Align        (Anon_Type);
706       Set_Depends_On_Private (Anon_Type, Has_Private_Component (Anon_Type));
707
708       --  Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
709       --  from Ada 95 semantics. In Ada 2005, anonymous access must specify
710       --  if the null value is allowed. In Ada 95 the null value is never
711       --  allowed.
712
713       if Ada_Version >= Ada_05 then
714          Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
715       else
716          Set_Can_Never_Be_Null (Anon_Type, True);
717       end if;
718
719       --  The anonymous access type is as public as the discriminated type or
720       --  subprogram that defines it. It is imported (for back-end purposes)
721       --  if the designated type is.
722
723       Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
724
725       --  Ada 2005 (AI-50217): Propagate the attribute that indicates that the
726       --  designated type comes from the limited view (for back-end purposes).
727
728       Set_From_With_Type (Anon_Type, From_With_Type (Desig_Type));
729
730       --  Ada 2005 (AI-231): Propagate the access-constant attribute
731
732       Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
733
734       --  The context is either a subprogram declaration or an access
735       --  discriminant, in a private or a full type declaration. In
736       --  the case of a subprogram, If the designated type is incomplete,
737       --  the operation will be a primitive operation of the full type, to
738       --  be updated subsequently. If the type is imported through a limited
739       --  with clause, it is not a primitive operation of the type (which
740       --  is declared elsewhere in some other scope).
741
742       if Ekind (Desig_Type) = E_Incomplete_Type
743         and then not From_With_Type (Desig_Type)
744         and then Is_Overloadable (Current_Scope)
745       then
746          Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
747          Set_Has_Delayed_Freeze (Current_Scope);
748       end if;
749
750       return Anon_Type;
751    end Access_Definition;
752
753    -----------------------------------
754    -- Access_Subprogram_Declaration --
755    -----------------------------------
756
757    procedure Access_Subprogram_Declaration
758      (T_Name : Entity_Id;
759       T_Def  : Node_Id)
760    is
761       Formals : constant List_Id   := Parameter_Specifications (T_Def);
762       Formal  : Entity_Id;
763
764       Desig_Type : constant Entity_Id :=
765                      Create_Itype (E_Subprogram_Type, Parent (T_Def));
766
767    begin
768       if Nkind (T_Def) = N_Access_Function_Definition then
769          Analyze (Subtype_Mark (T_Def));
770          Set_Etype (Desig_Type, Entity (Subtype_Mark (T_Def)));
771
772          if not (Is_Type (Etype (Desig_Type))) then
773             Error_Msg_N
774              ("expect type in function specification", Subtype_Mark (T_Def));
775          end if;
776
777       else
778          Set_Etype (Desig_Type, Standard_Void_Type);
779       end if;
780
781       if Present (Formals) then
782          New_Scope (Desig_Type);
783          Process_Formals (Formals, Parent (T_Def));
784
785          --  A bit of a kludge here, End_Scope requires that the parent
786          --  pointer be set to something reasonable, but Itypes don't
787          --  have parent pointers. So we set it and then unset it ???
788          --  If and when Itypes have proper parent pointers to their
789          --  declarations, this kludge can be removed.
790
791          Set_Parent (Desig_Type, T_Name);
792          End_Scope;
793          Set_Parent (Desig_Type, Empty);
794       end if;
795
796       --  The return type and/or any parameter type may be incomplete. Mark
797       --  the subprogram_type as depending on the incomplete type, so that
798       --  it can be updated when the full type declaration is seen.
799
800       if Present (Formals) then
801          Formal := First_Formal (Desig_Type);
802
803          while Present (Formal) loop
804
805             if Ekind (Formal) /= E_In_Parameter
806               and then Nkind (T_Def) = N_Access_Function_Definition
807             then
808                Error_Msg_N ("functions can only have IN parameters", Formal);
809             end if;
810
811             if Ekind (Etype (Formal)) = E_Incomplete_Type then
812                Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
813                Set_Has_Delayed_Freeze (Desig_Type);
814             end if;
815
816             Next_Formal (Formal);
817          end loop;
818       end if;
819
820       if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
821         and then not Has_Delayed_Freeze (Desig_Type)
822       then
823          Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
824          Set_Has_Delayed_Freeze (Desig_Type);
825       end if;
826
827       Check_Delayed_Subprogram (Desig_Type);
828
829       if Protected_Present (T_Def) then
830          Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
831          Set_Convention (Desig_Type, Convention_Protected);
832       else
833          Set_Ekind (T_Name, E_Access_Subprogram_Type);
834       end if;
835
836       Set_Etype                    (T_Name, T_Name);
837       Init_Size_Align              (T_Name);
838       Set_Directly_Designated_Type (T_Name, Desig_Type);
839
840       --  Ada 2005 (AI-231): Propagate the null-excluding attribute
841
842       Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
843
844       Check_Restriction (No_Access_Subprograms, T_Def);
845    end Access_Subprogram_Declaration;
846
847    ----------------------------
848    -- Access_Type_Declaration --
849    ----------------------------
850
851    procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
852       S : constant Node_Id := Subtype_Indication (Def);
853       P : constant Node_Id := Parent (Def);
854
855       Desig : Entity_Id;
856       --  Designated type
857
858    begin
859       --  Check for permissible use of incomplete type
860
861       if Nkind (S) /= N_Subtype_Indication then
862          Analyze (S);
863
864          if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
865             Set_Directly_Designated_Type (T, Entity (S));
866          else
867             Set_Directly_Designated_Type (T,
868               Process_Subtype (S, P, T, 'P'));
869          end if;
870
871       else
872          Set_Directly_Designated_Type (T,
873            Process_Subtype (S, P, T, 'P'));
874       end if;
875
876       if All_Present (Def) or Constant_Present (Def) then
877          Set_Ekind (T, E_General_Access_Type);
878       else
879          Set_Ekind (T, E_Access_Type);
880       end if;
881
882       if Base_Type (Designated_Type (T)) = T then
883          Error_Msg_N ("access type cannot designate itself", S);
884       end if;
885
886       Set_Etype (T, T);
887
888       --  If the type has appeared already in a with_type clause, it is
889       --  frozen and the pointer size is already set. Else, initialize.
890
891       if not From_With_Type (T) then
892          Init_Size_Align (T);
893       end if;
894
895       Set_Is_Access_Constant (T, Constant_Present (Def));
896
897       Desig := Designated_Type (T);
898
899       --  If designated type is an imported tagged type, indicate that the
900       --  access type is also imported, and therefore restricted in its use.
901       --  The access type may already be imported, so keep setting otherwise.
902
903       --  Ada 2005 (AI-50217): If the non-limited view of the designated type
904       --  is available, use it as the designated type of the access type, so
905       --  that the back-end gets a usable entity.
906
907       declare
908          N_Desig : Entity_Id;
909
910       begin
911          if From_With_Type (Desig) then
912             Set_From_With_Type (T);
913
914             if Ekind (Desig) = E_Incomplete_Type then
915                N_Desig := Non_Limited_View (Desig);
916
917             else pragma Assert (Ekind (Desig) = E_Class_Wide_Type);
918                if From_With_Type (Etype (Desig)) then
919                   N_Desig := Non_Limited_View (Etype (Desig));
920                else
921                   N_Desig := Etype (Desig);
922                end if;
923             end if;
924
925             pragma Assert (Present (N_Desig));
926             Set_Directly_Designated_Type (T, N_Desig);
927          end if;
928       end;
929
930       --  Note that Has_Task is always false, since the access type itself
931       --  is not a task type. See Einfo for more description on this point.
932       --  Exactly the same consideration applies to Has_Controlled_Component.
933
934       Set_Has_Task (T, False);
935       Set_Has_Controlled_Component (T, False);
936
937       --  Ada 2005 (AI-231): Propagate the null-excluding and access-constant
938       --  attributes
939
940       Set_Can_Never_Be_Null  (T, Null_Exclusion_Present (Def));
941       Set_Is_Access_Constant (T, Constant_Present (Def));
942    end Access_Type_Declaration;
943
944    -----------------------------------
945    -- Analyze_Component_Declaration --
946    -----------------------------------
947
948    procedure Analyze_Component_Declaration (N : Node_Id) is
949       Id : constant Entity_Id := Defining_Identifier (N);
950       T  : Entity_Id;
951       P  : Entity_Id;
952
953    begin
954       Generate_Definition (Id);
955       Enter_Name (Id);
956
957       if Present (Subtype_Indication (Component_Definition (N))) then
958          T := Find_Type_Of_Object
959                 (Subtype_Indication (Component_Definition (N)), N);
960
961       --  Ada 2005 (AI-230): Access Definition case
962
963       else
964          pragma Assert (Present
965                           (Access_Definition (Component_Definition (N))));
966
967          T := Access_Definition
968                 (Related_Nod => N,
969                  N => Access_Definition (Component_Definition (N)));
970
971          --  Ada 2005 (AI-230): In case of components that are anonymous
972          --  access types the level of accessibility depends on the enclosing
973          --  type declaration
974
975          Set_Scope (T, Current_Scope); -- Ada 2005 (AI-230)
976
977          --  Ada 2005 (AI-254)
978
979          if Present (Access_To_Subprogram_Definition
980                       (Access_Definition (Component_Definition (N))))
981            and then Protected_Present (Access_To_Subprogram_Definition
982                                         (Access_Definition
983                                           (Component_Definition (N))))
984          then
985             T := Replace_Anonymous_Access_To_Protected_Subprogram (N, T);
986          end if;
987       end if;
988
989       --  If the subtype is a constrained subtype of the enclosing record,
990       --  (which must have a partial view) the back-end does not handle
991       --  properly the recursion. Rewrite the component declaration with
992       --  an explicit subtype indication, which is acceptable to Gigi. We
993       --  can copy the tree directly because side effects have already been
994       --  removed from discriminant constraints.
995
996       if Ekind (T) = E_Access_Subtype
997         and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
998         and then Comes_From_Source (T)
999         and then Nkind (Parent (T)) = N_Subtype_Declaration
1000         and then Etype (Directly_Designated_Type (T)) = Current_Scope
1001       then
1002          Rewrite
1003            (Subtype_Indication (Component_Definition (N)),
1004              New_Copy_Tree (Subtype_Indication (Parent (T))));
1005          T := Find_Type_Of_Object
1006                  (Subtype_Indication (Component_Definition (N)), N);
1007       end if;
1008
1009       --  If the component declaration includes a default expression, then we
1010       --  check that the component is not of a limited type (RM 3.7(5)),
1011       --  and do the special preanalysis of the expression (see section on
1012       --  "Handling of Default and Per-Object Expressions" in the spec of
1013       --  package Sem).
1014
1015       if Present (Expression (N)) then
1016          Analyze_Per_Use_Expression (Expression (N), T);
1017          Check_Initialization (T, Expression (N));
1018       end if;
1019
1020       --  The parent type may be a private view with unknown discriminants,
1021       --  and thus unconstrained. Regular components must be constrained.
1022
1023       if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1024          if Is_Class_Wide_Type (T) then
1025             Error_Msg_N
1026                ("class-wide subtype with unknown discriminants" &
1027                  " in component declaration",
1028                  Subtype_Indication (Component_Definition (N)));
1029          else
1030             Error_Msg_N
1031               ("unconstrained subtype in component declaration",
1032                Subtype_Indication (Component_Definition (N)));
1033          end if;
1034
1035       --  Components cannot be abstract, except for the special case of
1036       --  the _Parent field (case of extending an abstract tagged type)
1037
1038       elsif Is_Abstract (T) and then Chars (Id) /= Name_uParent then
1039          Error_Msg_N ("type of a component cannot be abstract", N);
1040       end if;
1041
1042       Set_Etype (Id, T);
1043       Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1044
1045       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1046       --  out some static checks
1047
1048       if Ada_Version >= Ada_05
1049         and then (Null_Exclusion_Present (Component_Definition (N))
1050                     or else Can_Never_Be_Null (T))
1051       then
1052          Set_Can_Never_Be_Null (Id);
1053          Null_Exclusion_Static_Checks (N);
1054       end if;
1055
1056       --  If this component is private (or depends on a private type),
1057       --  flag the record type to indicate that some operations are not
1058       --  available.
1059
1060       P := Private_Component (T);
1061
1062       if Present (P) then
1063          --  Check for circular definitions.
1064
1065          if P = Any_Type then
1066             Set_Etype (Id, Any_Type);
1067
1068          --  There is a gap in the visibility of operations only if the
1069          --  component type is not defined in the scope of the record type.
1070
1071          elsif Scope (P) = Scope (Current_Scope) then
1072             null;
1073
1074          elsif Is_Limited_Type (P) then
1075             Set_Is_Limited_Composite (Current_Scope);
1076
1077          else
1078             Set_Is_Private_Composite (Current_Scope);
1079          end if;
1080       end if;
1081
1082       if P /= Any_Type
1083         and then Is_Limited_Type (T)
1084         and then Chars (Id) /= Name_uParent
1085         and then Is_Tagged_Type (Current_Scope)
1086       then
1087          if Is_Derived_Type (Current_Scope)
1088            and then not Is_Limited_Record (Root_Type (Current_Scope))
1089          then
1090             Error_Msg_N
1091               ("extension of nonlimited type cannot have limited components",
1092                N);
1093             Explain_Limited_Type (T, N);
1094             Set_Etype (Id, Any_Type);
1095             Set_Is_Limited_Composite (Current_Scope, False);
1096
1097          elsif not Is_Derived_Type (Current_Scope)
1098            and then not Is_Limited_Record (Current_Scope)
1099          then
1100             Error_Msg_N
1101               ("nonlimited tagged type cannot have limited components", N);
1102             Explain_Limited_Type (T, N);
1103             Set_Etype (Id, Any_Type);
1104             Set_Is_Limited_Composite (Current_Scope, False);
1105          end if;
1106       end if;
1107
1108       Set_Original_Record_Component (Id, Id);
1109    end Analyze_Component_Declaration;
1110
1111    --------------------------
1112    -- Analyze_Declarations --
1113    --------------------------
1114
1115    procedure Analyze_Declarations (L : List_Id) is
1116       D           : Node_Id;
1117       Next_Node   : Node_Id;
1118       Freeze_From : Entity_Id := Empty;
1119
1120       procedure Adjust_D;
1121       --  Adjust D not to include implicit label declarations, since these
1122       --  have strange Sloc values that result in elaboration check problems.
1123       --  (They have the sloc of the label as found in the source, and that
1124       --  is ahead of the current declarative part).
1125
1126       --------------
1127       -- Adjust_D --
1128       --------------
1129
1130       procedure Adjust_D is
1131       begin
1132          while Present (Prev (D))
1133            and then Nkind (D) = N_Implicit_Label_Declaration
1134          loop
1135             Prev (D);
1136          end loop;
1137       end Adjust_D;
1138
1139    --  Start of processing for Analyze_Declarations
1140
1141    begin
1142       D := First (L);
1143       while Present (D) loop
1144
1145          --  Complete analysis of declaration
1146
1147          Analyze (D);
1148          Next_Node := Next (D);
1149
1150          if No (Freeze_From) then
1151             Freeze_From := First_Entity (Current_Scope);
1152          end if;
1153
1154          --  At the end of a declarative part, freeze remaining entities
1155          --  declared in it. The end of the visible declarations of a
1156          --  package specification is not the end of a declarative part
1157          --  if private declarations are present. The end of a package
1158          --  declaration is a freezing point only if it a library package.
1159          --  A task definition or protected type definition is not a freeze
1160          --  point either. Finally, we do not freeze entities in generic
1161          --  scopes, because there is no code generated for them and freeze
1162          --  nodes will be generated for the instance.
1163
1164          --  The end of a package instantiation is not a freeze point, but
1165          --  for now we make it one, because the generic body is inserted
1166          --  (currently) immediately after. Generic instantiations will not
1167          --  be a freeze point once delayed freezing of bodies is implemented.
1168          --  (This is needed in any case for early instantiations ???).
1169
1170          if No (Next_Node) then
1171             if Nkind (Parent (L)) = N_Component_List
1172               or else Nkind (Parent (L)) = N_Task_Definition
1173               or else Nkind (Parent (L)) = N_Protected_Definition
1174             then
1175                null;
1176
1177             elsif Nkind (Parent (L)) /= N_Package_Specification then
1178                if Nkind (Parent (L)) = N_Package_Body then
1179                   Freeze_From := First_Entity (Current_Scope);
1180                end if;
1181
1182                Adjust_D;
1183                Freeze_All (Freeze_From, D);
1184                Freeze_From := Last_Entity (Current_Scope);
1185
1186             elsif Scope (Current_Scope) /= Standard_Standard
1187               and then not Is_Child_Unit (Current_Scope)
1188               and then No (Generic_Parent (Parent (L)))
1189             then
1190                null;
1191
1192             elsif L /= Visible_Declarations (Parent (L))
1193                or else No (Private_Declarations (Parent (L)))
1194                or else Is_Empty_List (Private_Declarations (Parent (L)))
1195             then
1196                Adjust_D;
1197                Freeze_All (Freeze_From, D);
1198                Freeze_From := Last_Entity (Current_Scope);
1199             end if;
1200
1201          --  If next node is a body then freeze all types before the body.
1202          --  An exception occurs for expander generated bodies, which can
1203          --  be recognized by their already being analyzed. The expander
1204          --  ensures that all types needed by these bodies have been frozen
1205          --  but it is not necessary to freeze all types (and would be wrong
1206          --  since it would not correspond to an RM defined freeze point).
1207
1208          elsif not Analyzed (Next_Node)
1209            and then (Nkind (Next_Node) = N_Subprogram_Body
1210              or else Nkind (Next_Node) = N_Entry_Body
1211              or else Nkind (Next_Node) = N_Package_Body
1212              or else Nkind (Next_Node) = N_Protected_Body
1213              or else Nkind (Next_Node) = N_Task_Body
1214              or else Nkind (Next_Node) in N_Body_Stub)
1215          then
1216             Adjust_D;
1217             Freeze_All (Freeze_From, D);
1218             Freeze_From := Last_Entity (Current_Scope);
1219          end if;
1220
1221          D := Next_Node;
1222       end loop;
1223    end Analyze_Declarations;
1224
1225    ----------------------------------
1226    -- Analyze_Incomplete_Type_Decl --
1227    ----------------------------------
1228
1229    procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
1230       F : constant Boolean := Is_Pure (Current_Scope);
1231       T : Entity_Id;
1232
1233    begin
1234       Generate_Definition (Defining_Identifier (N));
1235
1236       --  Process an incomplete declaration. The identifier must not have been
1237       --  declared already in the scope. However, an incomplete declaration may
1238       --  appear in the private part of a package, for a private type that has
1239       --  already been declared.
1240
1241       --  In this case, the discriminants (if any) must match
1242
1243       T := Find_Type_Name (N);
1244
1245       Set_Ekind (T, E_Incomplete_Type);
1246       Init_Size_Align (T);
1247       Set_Is_First_Subtype (T, True);
1248       Set_Etype (T, T);
1249       New_Scope (T);
1250
1251       Set_Stored_Constraint (T, No_Elist);
1252
1253       if Present (Discriminant_Specifications (N)) then
1254          Process_Discriminants (N);
1255       end if;
1256
1257       End_Scope;
1258
1259       --  If the type has discriminants, non-trivial subtypes may be
1260       --  be declared before the full view of the type. The full views
1261       --  of those subtypes will be built after the full view of the type.
1262
1263       Set_Private_Dependents (T, New_Elmt_List);
1264       Set_Is_Pure (T, F);
1265    end Analyze_Incomplete_Type_Decl;
1266
1267    -----------------------------
1268    -- Analyze_Itype_Reference --
1269    -----------------------------
1270
1271    --  Nothing to do. This node is placed in the tree only for the benefit
1272    --  of Gigi processing, and has no effect on the semantic processing.
1273
1274    procedure Analyze_Itype_Reference (N : Node_Id) is
1275    begin
1276       pragma Assert (Is_Itype (Itype (N)));
1277       null;
1278    end Analyze_Itype_Reference;
1279
1280    --------------------------------
1281    -- Analyze_Number_Declaration --
1282    --------------------------------
1283
1284    procedure Analyze_Number_Declaration (N : Node_Id) is
1285       Id    : constant Entity_Id := Defining_Identifier (N);
1286       E     : constant Node_Id   := Expression (N);
1287       T     : Entity_Id;
1288       Index : Interp_Index;
1289       It    : Interp;
1290
1291    begin
1292       Generate_Definition (Id);
1293       Enter_Name (Id);
1294
1295       --  This is an optimization of a common case of an integer literal
1296
1297       if Nkind (E) = N_Integer_Literal then
1298          Set_Is_Static_Expression (E, True);
1299          Set_Etype                (E, Universal_Integer);
1300
1301          Set_Etype     (Id, Universal_Integer);
1302          Set_Ekind     (Id, E_Named_Integer);
1303          Set_Is_Frozen (Id, True);
1304          return;
1305       end if;
1306
1307       Set_Is_Pure (Id, Is_Pure (Current_Scope));
1308
1309       --  Process expression, replacing error by integer zero, to avoid
1310       --  cascaded errors or aborts further along in the processing
1311
1312       --  Replace Error by integer zero, which seems least likely to
1313       --  cause cascaded errors.
1314
1315       if E = Error then
1316          Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
1317          Set_Error_Posted (E);
1318       end if;
1319
1320       Analyze (E);
1321
1322       --  Verify that the expression is static and numeric. If
1323       --  the expression is overloaded, we apply the preference
1324       --  rule that favors root numeric types.
1325
1326       if not Is_Overloaded (E) then
1327          T := Etype (E);
1328
1329       else
1330          T := Any_Type;
1331          Get_First_Interp (E, Index, It);
1332
1333          while Present (It.Typ) loop
1334             if (Is_Integer_Type (It.Typ)
1335                  or else Is_Real_Type (It.Typ))
1336               and then (Scope (Base_Type (It.Typ))) = Standard_Standard
1337             then
1338                if T = Any_Type then
1339                   T := It.Typ;
1340
1341                elsif It.Typ = Universal_Real
1342                  or else It.Typ = Universal_Integer
1343                then
1344                   --  Choose universal interpretation over any other.
1345
1346                   T := It.Typ;
1347                   exit;
1348                end if;
1349             end if;
1350
1351             Get_Next_Interp (Index, It);
1352          end loop;
1353       end if;
1354
1355       if Is_Integer_Type (T)  then
1356          Resolve (E, T);
1357          Set_Etype (Id, Universal_Integer);
1358          Set_Ekind (Id, E_Named_Integer);
1359
1360       elsif Is_Real_Type (T) then
1361
1362          --  Because the real value is converted to universal_real, this
1363          --  is a legal context for a universal fixed expression.
1364
1365          if T = Universal_Fixed then
1366             declare
1367                Loc  : constant Source_Ptr := Sloc (N);
1368                Conv : constant Node_Id := Make_Type_Conversion (Loc,
1369                         Subtype_Mark =>
1370                           New_Occurrence_Of (Universal_Real, Loc),
1371                         Expression => Relocate_Node (E));
1372
1373             begin
1374                Rewrite (E, Conv);
1375                Analyze (E);
1376             end;
1377
1378          elsif T = Any_Fixed then
1379             Error_Msg_N ("illegal context for mixed mode operation", E);
1380
1381             --  Expression is of the form : universal_fixed * integer.
1382             --  Try to resolve as universal_real.
1383
1384             T := Universal_Real;
1385             Set_Etype (E, T);
1386          end if;
1387
1388          Resolve (E, T);
1389          Set_Etype (Id, Universal_Real);
1390          Set_Ekind (Id, E_Named_Real);
1391
1392       else
1393          Wrong_Type (E, Any_Numeric);
1394          Resolve (E, T);
1395
1396          Set_Etype               (Id, T);
1397          Set_Ekind               (Id, E_Constant);
1398          Set_Never_Set_In_Source (Id, True);
1399          Set_Is_True_Constant    (Id, True);
1400          return;
1401       end if;
1402
1403       if Nkind (E) = N_Integer_Literal
1404         or else Nkind (E) = N_Real_Literal
1405       then
1406          Set_Etype (E, Etype (Id));
1407       end if;
1408
1409       if not Is_OK_Static_Expression (E) then
1410          Flag_Non_Static_Expr
1411            ("non-static expression used in number declaration!", E);
1412          Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
1413          Set_Etype (E, Any_Type);
1414       end if;
1415    end Analyze_Number_Declaration;
1416
1417    --------------------------------
1418    -- Analyze_Object_Declaration --
1419    --------------------------------
1420
1421    procedure Analyze_Object_Declaration (N : Node_Id) is
1422       Loc   : constant Source_Ptr := Sloc (N);
1423       Id    : constant Entity_Id  := Defining_Identifier (N);
1424       T     : Entity_Id;
1425       Act_T : Entity_Id;
1426
1427       E : Node_Id := Expression (N);
1428       --  E is set to Expression (N) throughout this routine. When
1429       --  Expression (N) is modified, E is changed accordingly.
1430
1431       Prev_Entity : Entity_Id := Empty;
1432
1433       function Build_Default_Subtype return Entity_Id;
1434       --  If the object is limited or aliased, and if the type is unconstrained
1435       --  and there is no expression, the discriminants cannot be modified and
1436       --  the subtype of the object is constrained by the defaults, so it is
1437       --  worthile building the corresponding subtype.
1438
1439       function Count_Tasks (T : Entity_Id) return Uint;
1440       --  This function is called when a library level object of type T
1441       --  is declared. It's function is to count the static number of
1442       --  tasks declared within the type (it is only called if Has_Tasks
1443       --  is set for T). As a side effect, if an array of tasks with
1444       --  non-static bounds or a variant record type is encountered,
1445       --  Check_Restrictions is called indicating the count is unknown.
1446
1447       ---------------------------
1448       -- Build_Default_Subtype --
1449       ---------------------------
1450
1451       function Build_Default_Subtype return Entity_Id is
1452          Constraints : constant List_Id := New_List;
1453          Act         : Entity_Id;
1454          Decl        : Node_Id;
1455          Disc        : Entity_Id;
1456
1457       begin
1458          Disc  := First_Discriminant (T);
1459
1460          if No (Discriminant_Default_Value (Disc)) then
1461             return T;   --   previous error.
1462          end if;
1463
1464          Act := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
1465          while Present (Disc) loop
1466             Append (
1467               New_Copy_Tree (
1468                 Discriminant_Default_Value (Disc)), Constraints);
1469             Next_Discriminant (Disc);
1470          end loop;
1471
1472          Decl :=
1473            Make_Subtype_Declaration (Loc,
1474              Defining_Identifier => Act,
1475              Subtype_Indication =>
1476                Make_Subtype_Indication (Loc,
1477                  Subtype_Mark => New_Occurrence_Of (T, Loc),
1478                  Constraint =>
1479                    Make_Index_Or_Discriminant_Constraint
1480                      (Loc, Constraints)));
1481
1482          Insert_Before (N, Decl);
1483          Analyze (Decl);
1484          return Act;
1485       end Build_Default_Subtype;
1486
1487       -----------------
1488       -- Count_Tasks --
1489       -----------------
1490
1491       function Count_Tasks (T : Entity_Id) return Uint is
1492          C : Entity_Id;
1493          X : Node_Id;
1494          V : Uint;
1495
1496       begin
1497          if Is_Task_Type (T) then
1498             return Uint_1;
1499
1500          elsif Is_Record_Type (T) then
1501             if Has_Discriminants (T) then
1502                Check_Restriction (Max_Tasks, N);
1503                return Uint_0;
1504
1505             else
1506                V := Uint_0;
1507                C := First_Component (T);
1508                while Present (C) loop
1509                   V := V + Count_Tasks (Etype (C));
1510                   Next_Component (C);
1511                end loop;
1512
1513                return V;
1514             end if;
1515
1516          elsif Is_Array_Type (T) then
1517             X := First_Index (T);
1518             V := Count_Tasks (Component_Type (T));
1519             while Present (X) loop
1520                C := Etype (X);
1521
1522                if not Is_Static_Subtype (C) then
1523                   Check_Restriction (Max_Tasks, N);
1524                   return Uint_0;
1525                else
1526                   V := V * (UI_Max (Uint_0,
1527                                     Expr_Value (Type_High_Bound (C)) -
1528                                     Expr_Value (Type_Low_Bound (C)) + Uint_1));
1529                end if;
1530
1531                Next_Index (X);
1532             end loop;
1533
1534             return V;
1535
1536          else
1537             return Uint_0;
1538          end if;
1539       end Count_Tasks;
1540
1541    --  Start of processing for Analyze_Object_Declaration
1542
1543    begin
1544       --  There are three kinds of implicit types generated by an
1545       --  object declaration:
1546
1547       --   1. Those for generated by the original Object Definition
1548
1549       --   2. Those generated by the Expression
1550
1551       --   3. Those used to constrained the Object Definition with the
1552       --       expression constraints when it is unconstrained
1553
1554       --  They must be generated in this order to avoid order of elaboration
1555       --  issues. Thus the first step (after entering the name) is to analyze
1556       --  the object definition.
1557
1558       if Constant_Present (N) then
1559          Prev_Entity := Current_Entity_In_Scope (Id);
1560
1561          --  If homograph is an implicit subprogram, it is overridden by the
1562          --  current declaration.
1563
1564          if Present (Prev_Entity)
1565            and then Is_Overloadable (Prev_Entity)
1566            and then Is_Inherited_Operation (Prev_Entity)
1567          then
1568             Prev_Entity := Empty;
1569          end if;
1570       end if;
1571
1572       if Present (Prev_Entity) then
1573          Constant_Redeclaration (Id, N, T);
1574
1575          Generate_Reference (Prev_Entity, Id, 'c');
1576          Set_Completion_Referenced (Id);
1577
1578          if Error_Posted (N) then
1579             --  Type mismatch or illegal redeclaration, Do not analyze
1580             --  expression to avoid cascaded errors.
1581
1582             T := Find_Type_Of_Object (Object_Definition (N), N);
1583             Set_Etype (Id, T);
1584             Set_Ekind (Id, E_Variable);
1585             return;
1586          end if;
1587
1588       --  In the normal case, enter identifier at the start to catch
1589       --  premature usage in the initialization expression.
1590
1591       else
1592          Generate_Definition (Id);
1593          Enter_Name (Id);
1594
1595          T := Find_Type_Of_Object (Object_Definition (N), N);
1596
1597          if Error_Posted (Id) then
1598             Set_Etype (Id, T);
1599             Set_Ekind (Id, E_Variable);
1600             return;
1601          end if;
1602       end if;
1603
1604       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1605       --  out some static checks
1606
1607       if Ada_Version >= Ada_05
1608         and then (Null_Exclusion_Present (N)
1609                     or else Can_Never_Be_Null (T))
1610       then
1611          Set_Can_Never_Be_Null (Id);
1612          Null_Exclusion_Static_Checks (N);
1613       end if;
1614
1615       Set_Is_Pure (Id, Is_Pure (Current_Scope));
1616
1617       --  If deferred constant, make sure context is appropriate. We detect
1618       --  a deferred constant as a constant declaration with no expression.
1619       --  A deferred constant can appear in a package body if its completion
1620       --  is by means of an interface pragma.
1621
1622       if Constant_Present (N)
1623         and then No (E)
1624       then
1625          if not Is_Package (Current_Scope) then
1626             Error_Msg_N
1627               ("invalid context for deferred constant declaration ('R'M 7.4)",
1628                 N);
1629             Error_Msg_N
1630               ("\declaration requires an initialization expression",
1631                 N);
1632             Set_Constant_Present (N, False);
1633
1634          --  In Ada 83, deferred constant must be of private type
1635
1636          elsif not Is_Private_Type (T) then
1637             if Ada_Version = Ada_83 and then Comes_From_Source (N) then
1638                Error_Msg_N
1639                  ("(Ada 83) deferred constant must be private type", N);
1640             end if;
1641          end if;
1642
1643       --  If not a deferred constant, then object declaration freezes its type
1644
1645       else
1646          Check_Fully_Declared (T, N);
1647          Freeze_Before (N, T);
1648       end if;
1649
1650       --  If the object was created by a constrained array definition, then
1651       --  set the link in both the anonymous base type and anonymous subtype
1652       --  that are built to represent the array type to point to the object.
1653
1654       if Nkind (Object_Definition (Declaration_Node (Id))) =
1655                         N_Constrained_Array_Definition
1656       then
1657          Set_Related_Array_Object (T, Id);
1658          Set_Related_Array_Object (Base_Type (T), Id);
1659       end if;
1660
1661       --  Special checks for protected objects not at library level
1662
1663       if Is_Protected_Type (T)
1664         and then not Is_Library_Level_Entity (Id)
1665       then
1666          Check_Restriction (No_Local_Protected_Objects, Id);
1667
1668          --  Protected objects with interrupt handlers must be at library level
1669
1670          if Has_Interrupt_Handler (T) then
1671             Error_Msg_N
1672               ("interrupt object can only be declared at library level", Id);
1673          end if;
1674       end if;
1675
1676       --  The actual subtype of the object is the nominal subtype, unless
1677       --  the nominal one is unconstrained and obtained from the expression.
1678
1679       Act_T := T;
1680
1681       --  Process initialization expression if present and not in error
1682
1683       if Present (E) and then E /= Error then
1684          Analyze (E);
1685
1686          --  In case of errors detected in the analysis of the expression,
1687          --  decorate it with the expected type to avoid cascade errors
1688
1689          if not Present (Etype (E)) then
1690             Set_Etype (E, T);
1691          end if;
1692
1693          --  If an initialization expression is present, then we set the
1694          --  Is_True_Constant flag. It will be reset if this is a variable
1695          --  and it is indeed modified.
1696
1697          Set_Is_True_Constant (Id, True);
1698
1699          --  If we are analyzing a constant declaration, set its completion
1700          --  flag after analyzing the expression.
1701
1702          if Constant_Present (N) then
1703             Set_Has_Completion (Id);
1704          end if;
1705
1706          if not Assignment_OK (N) then
1707             Check_Initialization (T, E);
1708          end if;
1709
1710          Set_Etype (Id, T);             --  may be overridden later on.
1711          Resolve (E, T);
1712          Check_Unset_Reference (E);
1713
1714          if Compile_Time_Known_Value (E) then
1715             Set_Current_Value (Id, E);
1716          end if;
1717
1718          --  Check incorrect use of dynamically tagged expressions. Note
1719          --  the use of Is_Tagged_Type (T) which seems redundant but is in
1720          --  fact important to avoid spurious errors due to expanded code
1721          --  for dispatching functions over an anonymous access type
1722
1723          if (Is_Class_Wide_Type (Etype (E)) or else Is_Dynamically_Tagged (E))
1724            and then Is_Tagged_Type (T)
1725            and then not Is_Class_Wide_Type (T)
1726          then
1727             Error_Msg_N ("dynamically tagged expression not allowed!", E);
1728          end if;
1729
1730          Apply_Scalar_Range_Check (E, T);
1731          Apply_Static_Length_Check (E, T);
1732       end if;
1733
1734       --  Abstract type is never permitted for a variable or constant.
1735       --  Note: we inhibit this check for objects that do not come from
1736       --  source because there is at least one case (the expansion of
1737       --  x'class'input where x is abstract) where we legitimately
1738       --  generate an abstract object.
1739
1740       if Is_Abstract (T) and then Comes_From_Source (N) then
1741          Error_Msg_N ("type of object cannot be abstract",
1742            Object_Definition (N));
1743          if Is_CPP_Class (T) then
1744             Error_Msg_NE ("\} may need a cpp_constructor",
1745               Object_Definition (N), T);
1746          end if;
1747
1748       --  Case of unconstrained type
1749
1750       elsif Is_Indefinite_Subtype (T) then
1751
1752          --  Nothing to do in deferred constant case
1753
1754          if Constant_Present (N) and then No (E) then
1755             null;
1756
1757          --  Case of no initialization present
1758
1759          elsif No (E) then
1760             if No_Initialization (N) then
1761                null;
1762
1763             elsif Is_Class_Wide_Type (T) then
1764                Error_Msg_N
1765                  ("initialization required in class-wide declaration ", N);
1766
1767             else
1768                Error_Msg_N
1769                  ("unconstrained subtype not allowed (need initialization)",
1770                   Object_Definition (N));
1771             end if;
1772
1773          --  Case of initialization present but in error. Set initial
1774          --  expression as absent (but do not make above complaints)
1775
1776          elsif E = Error then
1777             Set_Expression (N, Empty);
1778             E := Empty;
1779
1780          --  Case of initialization present
1781
1782          else
1783             --  Not allowed in Ada 83
1784
1785             if not Constant_Present (N) then
1786                if Ada_Version = Ada_83
1787                  and then Comes_From_Source (Object_Definition (N))
1788                then
1789                   Error_Msg_N
1790                     ("(Ada 83) unconstrained variable not allowed",
1791                      Object_Definition (N));
1792                end if;
1793             end if;
1794
1795             --  Now we constrain the variable from the initializing expression
1796
1797             --  If the expression is an aggregate, it has been expanded into
1798             --  individual assignments. Retrieve the actual type from the
1799             --  expanded construct.
1800
1801             if Is_Array_Type (T)
1802               and then No_Initialization (N)
1803               and then Nkind (Original_Node (E)) = N_Aggregate
1804             then
1805                Act_T := Etype (E);
1806
1807             else
1808                Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
1809                Act_T := Find_Type_Of_Object (Object_Definition (N), N);
1810             end if;
1811
1812             Set_Is_Constr_Subt_For_U_Nominal (Act_T);
1813
1814             if Aliased_Present (N) then
1815                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
1816             end if;
1817
1818             Freeze_Before (N, Act_T);
1819             Freeze_Before (N, T);
1820          end if;
1821
1822       elsif Is_Array_Type (T)
1823         and then No_Initialization (N)
1824         and then Nkind (Original_Node (E)) = N_Aggregate
1825       then
1826          if not Is_Entity_Name (Object_Definition (N)) then
1827             Act_T := Etype (E);
1828             Check_Compile_Time_Size (Act_T);
1829
1830             if Aliased_Present (N) then
1831                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
1832             end if;
1833          end if;
1834
1835          --  When the given object definition and the aggregate are specified
1836          --  independently, and their lengths might differ do a length check.
1837          --  This cannot happen if the aggregate is of the form (others =>...)
1838
1839          if not Is_Constrained (T) then
1840             null;
1841
1842          elsif Nkind (E) = N_Raise_Constraint_Error then
1843
1844             --  Aggregate is statically illegal. Place back in declaration
1845
1846             Set_Expression (N, E);
1847             Set_No_Initialization (N, False);
1848
1849          elsif T = Etype (E) then
1850             null;
1851
1852          elsif Nkind (E) = N_Aggregate
1853            and then Present (Component_Associations (E))
1854            and then Present (Choices (First (Component_Associations (E))))
1855            and then Nkind (First
1856             (Choices (First (Component_Associations (E))))) = N_Others_Choice
1857          then
1858             null;
1859
1860          else
1861             Apply_Length_Check (E, T);
1862          end if;
1863
1864       elsif (Is_Limited_Record (T)
1865                or else Is_Concurrent_Type (T))
1866         and then not Is_Constrained (T)
1867         and then Has_Discriminants (T)
1868       then
1869          Act_T := Build_Default_Subtype;
1870          Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
1871
1872       elsif not Is_Constrained (T)
1873         and then Has_Discriminants (T)
1874         and then Constant_Present (N)
1875         and then Nkind (E) = N_Function_Call
1876       then
1877          --  The back-end has problems with constants of a discriminated type
1878          --  with defaults, if the initial value is a function call. We
1879          --  generate an intermediate temporary for the result of the call.
1880          --  It is unclear why this should make it acceptable to gcc. ???
1881
1882          Remove_Side_Effects (E);
1883       end if;
1884
1885       if T = Standard_Wide_Character
1886         or else Root_Type (T) = Standard_Wide_String
1887       then
1888          Check_Restriction (No_Wide_Characters, Object_Definition (N));
1889       end if;
1890
1891       --  Now establish the proper kind and type of the object
1892
1893       if Constant_Present (N) then
1894          Set_Ekind               (Id, E_Constant);
1895          Set_Never_Set_In_Source (Id, True);
1896          Set_Is_True_Constant    (Id, True);
1897
1898       else
1899          Set_Ekind (Id, E_Variable);
1900
1901          --  A variable is set as shared passive if it appears in a shared
1902          --  passive package, and is at the outer level. This is not done
1903          --  for entities generated during expansion, because those are
1904          --  always manipulated locally.
1905
1906          if Is_Shared_Passive (Current_Scope)
1907            and then Is_Library_Level_Entity (Id)
1908            and then Comes_From_Source (Id)
1909          then
1910             Set_Is_Shared_Passive (Id);
1911             Check_Shared_Var (Id, T, N);
1912          end if;
1913
1914          --  Case of no initializing expression present. If the type is not
1915          --  fully initialized, then we set Never_Set_In_Source, since this
1916          --  is a case of a potentially uninitialized object. Note that we
1917          --  do not consider access variables to be fully initialized for
1918          --  this purpose, since it still seems dubious if someone declares
1919
1920          --  Note that we only do this for source declarations. If the object
1921          --  is declared by a generated declaration, we assume that it is not
1922          --  appropriate to generate warnings in that case.
1923
1924          if No (E) then
1925             if (Is_Access_Type (T)
1926                  or else not Is_Fully_Initialized_Type (T))
1927               and then Comes_From_Source (N)
1928             then
1929                Set_Never_Set_In_Source (Id);
1930             end if;
1931          end if;
1932       end if;
1933
1934       Init_Alignment (Id);
1935       Init_Esize     (Id);
1936
1937       if Aliased_Present (N) then
1938          Set_Is_Aliased (Id);
1939
1940          if No (E)
1941            and then Is_Record_Type (T)
1942            and then not Is_Constrained (T)
1943            and then Has_Discriminants (T)
1944          then
1945             Set_Actual_Subtype (Id, Build_Default_Subtype);
1946          end if;
1947       end if;
1948
1949       Set_Etype (Id, Act_T);
1950
1951       if Has_Controlled_Component (Etype (Id))
1952         or else Is_Controlled (Etype (Id))
1953       then
1954          if not Is_Library_Level_Entity (Id) then
1955             Check_Restriction (No_Nested_Finalization, N);
1956
1957          else
1958             Validate_Controlled_Object (Id);
1959          end if;
1960
1961          --  Generate a warning when an initialization causes an obvious
1962          --  ABE violation. If the init expression is a simple aggregate
1963          --  there shouldn't be any initialize/adjust call generated. This
1964          --  will be true as soon as aggregates are built in place when
1965          --  possible. ??? at the moment we do not generate warnings for
1966          --  temporaries created for those aggregates although a
1967          --  Program_Error might be generated if compiled with -gnato
1968
1969          if Is_Controlled (Etype (Id))
1970             and then Comes_From_Source (Id)
1971          then
1972             declare
1973                BT : constant Entity_Id := Base_Type (Etype (Id));
1974
1975                Implicit_Call : Entity_Id;
1976                pragma Warnings (Off, Implicit_Call);
1977                --  What is this about, it is never referenced ???
1978
1979                function Is_Aggr (N : Node_Id) return Boolean;
1980                --  Check that N is an aggregate
1981
1982                -------------
1983                -- Is_Aggr --
1984                -------------
1985
1986                function Is_Aggr (N : Node_Id) return Boolean is
1987                begin
1988                   case Nkind (Original_Node (N)) is
1989                      when N_Aggregate | N_Extension_Aggregate =>
1990                         return True;
1991
1992                      when N_Qualified_Expression |
1993                           N_Type_Conversion      |
1994                           N_Unchecked_Type_Conversion =>
1995                         return Is_Aggr (Expression (Original_Node (N)));
1996
1997                      when others =>
1998                         return False;
1999                   end case;
2000                end Is_Aggr;
2001
2002             begin
2003                --  If no underlying type, we already are in an error situation
2004                --  don't try to add a warning since we do not have access
2005                --  prim-op list.
2006
2007                if No (Underlying_Type (BT)) then
2008                   Implicit_Call := Empty;
2009
2010                --  A generic type does not have usable primitive operators.
2011                --  Initialization calls are built for instances.
2012
2013                elsif Is_Generic_Type (BT) then
2014                   Implicit_Call := Empty;
2015
2016                --  if the init expression is not an aggregate, an adjust
2017                --  call will be generated
2018
2019                elsif Present (E) and then not Is_Aggr (E) then
2020                   Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
2021
2022                --  if no init expression and we are not in the deferred
2023                --  constant case, an Initialize call will be generated
2024
2025                elsif No (E) and then not Constant_Present (N) then
2026                   Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
2027
2028                else
2029                   Implicit_Call := Empty;
2030                end if;
2031             end;
2032          end if;
2033       end if;
2034
2035       if Has_Task (Etype (Id)) then
2036          Check_Restriction (No_Tasking, N);
2037
2038          if Is_Library_Level_Entity (Id) then
2039             Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
2040
2041          else
2042             Check_Restriction (Max_Tasks, N);
2043             Check_Restriction (No_Task_Hierarchy, N);
2044             Check_Potentially_Blocking_Operation (N);
2045          end if;
2046
2047          --  A rather specialized test. If we see two tasks being declared
2048          --  of the same type in the same object declaration, and the task
2049          --  has an entry with an address clause, we know that program error
2050          --  will be raised at run-time since we can't have two tasks with
2051          --  entries at the same address.
2052
2053          if Is_Task_Type (Etype (Id))
2054            and then More_Ids (N)
2055          then
2056             declare
2057                E : Entity_Id;
2058
2059             begin
2060                E := First_Entity (Etype (Id));
2061                while Present (E) loop
2062                   if Ekind (E) = E_Entry
2063                     and then Present (Get_Attribute_Definition_Clause
2064                                         (E, Attribute_Address))
2065                   then
2066                      Error_Msg_N
2067                        ("?more than one task with same entry address", N);
2068                      Error_Msg_N
2069                        ("\?Program_Error will be raised at run time", N);
2070                      Insert_Action (N,
2071                        Make_Raise_Program_Error (Loc,
2072                          Reason => PE_Duplicated_Entry_Address));
2073                      exit;
2074                   end if;
2075
2076                   Next_Entity (E);
2077                end loop;
2078             end;
2079          end if;
2080       end if;
2081
2082       --  Some simple constant-propagation: if the expression is a constant
2083       --  string initialized with a literal, share the literal. This avoids
2084       --  a run-time copy.
2085
2086       if Present (E)
2087         and then Is_Entity_Name (E)
2088         and then Ekind (Entity (E)) = E_Constant
2089         and then Base_Type (Etype (E)) = Standard_String
2090       then
2091          declare
2092             Val : constant Node_Id := Constant_Value (Entity (E));
2093
2094          begin
2095             if Present (Val)
2096               and then Nkind (Val) = N_String_Literal
2097             then
2098                Rewrite (E, New_Copy (Val));
2099             end if;
2100          end;
2101       end if;
2102
2103       --  Another optimization: if the nominal subtype is unconstrained and
2104       --  the expression is a function call that returns an unconstrained
2105       --  type, rewrite the declaration as a renaming of the result of the
2106       --  call. The exceptions below are cases where the copy is expected,
2107       --  either by the back end (Aliased case) or by the semantics, as for
2108       --  initializing controlled types or copying tags for classwide types.
2109
2110       if Present (E)
2111         and then Nkind (E) = N_Explicit_Dereference
2112         and then Nkind (Original_Node (E)) = N_Function_Call
2113         and then not Is_Library_Level_Entity (Id)
2114         and then not Is_Constrained (T)
2115         and then not Is_Aliased (Id)
2116         and then not Is_Class_Wide_Type (T)
2117         and then not Is_Controlled (T)
2118         and then not Has_Controlled_Component (Base_Type (T))
2119         and then Expander_Active
2120       then
2121          Rewrite (N,
2122            Make_Object_Renaming_Declaration (Loc,
2123              Defining_Identifier => Id,
2124              Access_Definition   => Empty,
2125              Subtype_Mark        => New_Occurrence_Of
2126                                       (Base_Type (Etype (Id)), Loc),
2127              Name                => E));
2128
2129          Set_Renamed_Object (Id, E);
2130
2131          --  Force generation of debugging information for the constant
2132          --  and for the renamed function call.
2133
2134          Set_Needs_Debug_Info (Id);
2135          Set_Needs_Debug_Info (Entity (Prefix (E)));
2136       end if;
2137
2138       if Present (Prev_Entity)
2139         and then Is_Frozen (Prev_Entity)
2140         and then not Error_Posted (Id)
2141       then
2142          Error_Msg_N ("full constant declaration appears too late", N);
2143       end if;
2144
2145       Check_Eliminated (Id);
2146    end Analyze_Object_Declaration;
2147
2148    ---------------------------
2149    -- Analyze_Others_Choice --
2150    ---------------------------
2151
2152    --  Nothing to do for the others choice node itself, the semantic analysis
2153    --  of the others choice will occur as part of the processing of the parent
2154
2155    procedure Analyze_Others_Choice (N : Node_Id) is
2156       pragma Warnings (Off, N);
2157
2158    begin
2159       null;
2160    end Analyze_Others_Choice;
2161
2162    --------------------------------
2163    -- Analyze_Per_Use_Expression --
2164    --------------------------------
2165
2166    procedure Analyze_Per_Use_Expression (N : Node_Id; T : Entity_Id) is
2167       Save_In_Default_Expression : constant Boolean := In_Default_Expression;
2168
2169    begin
2170       In_Default_Expression := True;
2171       Pre_Analyze_And_Resolve (N, T);
2172       In_Default_Expression := Save_In_Default_Expression;
2173    end Analyze_Per_Use_Expression;
2174
2175    -------------------------------------------
2176    -- Analyze_Private_Extension_Declaration --
2177    -------------------------------------------
2178
2179    procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
2180       T           : constant Entity_Id := Defining_Identifier (N);
2181       Indic       : constant Node_Id   := Subtype_Indication (N);
2182       Parent_Type : Entity_Id;
2183       Parent_Base : Entity_Id;
2184
2185    begin
2186       Generate_Definition (T);
2187       Enter_Name (T);
2188
2189       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
2190       Parent_Base := Base_Type (Parent_Type);
2191
2192       if Parent_Type = Any_Type
2193         or else Etype (Parent_Type) = Any_Type
2194       then
2195          Set_Ekind (T, Ekind (Parent_Type));
2196          Set_Etype (T, Any_Type);
2197          return;
2198
2199       elsif not Is_Tagged_Type (Parent_Type) then
2200          Error_Msg_N
2201            ("parent of type extension must be a tagged type ", Indic);
2202          return;
2203
2204       elsif Ekind (Parent_Type) = E_Void
2205         or else Ekind (Parent_Type) = E_Incomplete_Type
2206       then
2207          Error_Msg_N ("premature derivation of incomplete type", Indic);
2208          return;
2209       end if;
2210
2211       --  Perhaps the parent type should be changed to the class-wide type's
2212       --  specific type in this case to prevent cascading errors ???
2213
2214       if Is_Class_Wide_Type (Parent_Type) then
2215          Error_Msg_N
2216            ("parent of type extension must not be a class-wide type", Indic);
2217          return;
2218       end if;
2219
2220       if (not Is_Package (Current_Scope)
2221            and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
2222         or else In_Private_Part (Current_Scope)
2223
2224       then
2225          Error_Msg_N ("invalid context for private extension", N);
2226       end if;
2227
2228       --  Set common attributes
2229
2230       Set_Is_Pure          (T, Is_Pure (Current_Scope));
2231       Set_Scope            (T, Current_Scope);
2232       Set_Ekind            (T, E_Record_Type_With_Private);
2233       Init_Size_Align      (T);
2234
2235       Set_Etype            (T,            Parent_Base);
2236       Set_Has_Task         (T, Has_Task  (Parent_Base));
2237
2238       Set_Convention       (T, Convention     (Parent_Type));
2239       Set_First_Rep_Item   (T, First_Rep_Item (Parent_Type));
2240       Set_Is_First_Subtype (T);
2241       Make_Class_Wide_Type (T);
2242
2243       if Unknown_Discriminants_Present (N) then
2244          Set_Discriminant_Constraint (T, No_Elist);
2245       end if;
2246
2247       Build_Derived_Record_Type (N, Parent_Type, T);
2248    end Analyze_Private_Extension_Declaration;
2249
2250    ---------------------------------
2251    -- Analyze_Subtype_Declaration --
2252    ---------------------------------
2253
2254    procedure Analyze_Subtype_Declaration (N : Node_Id) is
2255       Id       : constant Entity_Id := Defining_Identifier (N);
2256       T        : Entity_Id;
2257       R_Checks : Check_Result;
2258
2259    begin
2260       Generate_Definition (Id);
2261       Set_Is_Pure (Id, Is_Pure (Current_Scope));
2262       Init_Size_Align (Id);
2263
2264       --  The following guard condition on Enter_Name is to handle cases
2265       --  where the defining identifier has already been entered into the
2266       --  scope but the declaration as a whole needs to be analyzed.
2267
2268       --  This case in particular happens for derived enumeration types.
2269       --  The derived enumeration type is processed as an inserted enumeration
2270       --  type declaration followed by a rewritten subtype declaration. The
2271       --  defining identifier, however, is entered into the name scope very
2272       --  early in the processing of the original type declaration and
2273       --  therefore needs to be avoided here, when the created subtype
2274       --  declaration is analyzed. (See Build_Derived_Types)
2275
2276       --  This also happens when the full view of a private type is a
2277       --  derived type with constraints. In this case the entity has been
2278       --  introduced in the private declaration.
2279
2280       if Present (Etype (Id))
2281         and then (Is_Private_Type (Etype (Id))
2282                    or else Is_Task_Type (Etype (Id))
2283                    or else Is_Rewrite_Substitution (N))
2284       then
2285          null;
2286
2287       else
2288          Enter_Name (Id);
2289       end if;
2290
2291       T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
2292
2293       --  Inherit common attributes
2294
2295       Set_Is_Generic_Type   (Id, Is_Generic_Type   (Base_Type (T)));
2296       Set_Is_Volatile       (Id, Is_Volatile       (T));
2297       Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
2298       Set_Is_Atomic         (Id, Is_Atomic         (T));
2299
2300       --  In the case where there is no constraint given in the subtype
2301       --  indication, Process_Subtype just returns the Subtype_Mark,
2302       --  so its semantic attributes must be established here.
2303
2304       if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
2305          Set_Etype (Id, Base_Type (T));
2306
2307          case Ekind (T) is
2308             when Array_Kind =>
2309                Set_Ekind                       (Id, E_Array_Subtype);
2310                Copy_Array_Subtype_Attributes   (Id, T);
2311
2312             when Decimal_Fixed_Point_Kind =>
2313                Set_Ekind                (Id, E_Decimal_Fixed_Point_Subtype);
2314                Set_Digits_Value         (Id, Digits_Value       (T));
2315                Set_Delta_Value          (Id, Delta_Value        (T));
2316                Set_Scale_Value          (Id, Scale_Value        (T));
2317                Set_Small_Value          (Id, Small_Value        (T));
2318                Set_Scalar_Range         (Id, Scalar_Range       (T));
2319                Set_Machine_Radix_10     (Id, Machine_Radix_10   (T));
2320                Set_Is_Constrained       (Id, Is_Constrained     (T));
2321                Set_RM_Size              (Id, RM_Size            (T));
2322
2323             when Enumeration_Kind =>
2324                Set_Ekind                (Id, E_Enumeration_Subtype);
2325                Set_First_Literal        (Id, First_Literal (Base_Type (T)));
2326                Set_Scalar_Range         (Id, Scalar_Range       (T));
2327                Set_Is_Character_Type    (Id, Is_Character_Type  (T));
2328                Set_Is_Constrained       (Id, Is_Constrained     (T));
2329                Set_RM_Size              (Id, RM_Size            (T));
2330
2331             when Ordinary_Fixed_Point_Kind =>
2332                Set_Ekind                (Id, E_Ordinary_Fixed_Point_Subtype);
2333                Set_Scalar_Range         (Id, Scalar_Range       (T));
2334                Set_Small_Value          (Id, Small_Value        (T));
2335                Set_Delta_Value          (Id, Delta_Value        (T));
2336                Set_Is_Constrained       (Id, Is_Constrained     (T));
2337                Set_RM_Size              (Id, RM_Size            (T));
2338
2339             when Float_Kind =>
2340                Set_Ekind                (Id, E_Floating_Point_Subtype);
2341                Set_Scalar_Range         (Id, Scalar_Range       (T));
2342                Set_Digits_Value         (Id, Digits_Value       (T));
2343                Set_Is_Constrained       (Id, Is_Constrained     (T));
2344
2345             when Signed_Integer_Kind =>
2346                Set_Ekind                (Id, E_Signed_Integer_Subtype);
2347                Set_Scalar_Range         (Id, Scalar_Range       (T));
2348                Set_Is_Constrained       (Id, Is_Constrained     (T));
2349                Set_RM_Size              (Id, RM_Size            (T));
2350
2351             when Modular_Integer_Kind =>
2352                Set_Ekind                (Id, E_Modular_Integer_Subtype);
2353                Set_Scalar_Range         (Id, Scalar_Range       (T));
2354                Set_Is_Constrained       (Id, Is_Constrained     (T));
2355                Set_RM_Size              (Id, RM_Size            (T));
2356
2357             when Class_Wide_Kind =>
2358                Set_Ekind                (Id, E_Class_Wide_Subtype);
2359                Set_First_Entity         (Id, First_Entity       (T));
2360                Set_Last_Entity          (Id, Last_Entity        (T));
2361                Set_Class_Wide_Type      (Id, Class_Wide_Type    (T));
2362                Set_Cloned_Subtype       (Id, T);
2363                Set_Is_Tagged_Type       (Id, True);
2364                Set_Has_Unknown_Discriminants
2365                                         (Id, True);
2366
2367                if Ekind (T) = E_Class_Wide_Subtype then
2368                   Set_Equivalent_Type   (Id, Equivalent_Type    (T));
2369                end if;
2370
2371             when E_Record_Type | E_Record_Subtype =>
2372                Set_Ekind                (Id, E_Record_Subtype);
2373
2374                if Ekind (T) = E_Record_Subtype
2375                  and then Present (Cloned_Subtype (T))
2376                then
2377                   Set_Cloned_Subtype    (Id, Cloned_Subtype (T));
2378                else
2379                   Set_Cloned_Subtype    (Id, T);
2380                end if;
2381
2382                Set_First_Entity         (Id, First_Entity       (T));
2383                Set_Last_Entity          (Id, Last_Entity        (T));
2384                Set_Has_Discriminants    (Id, Has_Discriminants  (T));
2385                Set_Is_Constrained       (Id, Is_Constrained     (T));
2386                Set_Is_Limited_Record    (Id, Is_Limited_Record  (T));
2387                Set_Has_Unknown_Discriminants
2388                                         (Id, Has_Unknown_Discriminants (T));
2389
2390                if Has_Discriminants (T) then
2391                   Set_Discriminant_Constraint
2392                                         (Id, Discriminant_Constraint (T));
2393                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
2394
2395                elsif Has_Unknown_Discriminants (Id) then
2396                   Set_Discriminant_Constraint (Id, No_Elist);
2397                end if;
2398
2399                if Is_Tagged_Type (T) then
2400                   Set_Is_Tagged_Type    (Id);
2401                   Set_Is_Abstract       (Id, Is_Abstract (T));
2402                   Set_Primitive_Operations
2403                                         (Id, Primitive_Operations (T));
2404                   Set_Class_Wide_Type   (Id, Class_Wide_Type (T));
2405                end if;
2406
2407             when Private_Kind =>
2408                Set_Ekind              (Id, Subtype_Kind (Ekind   (T)));
2409                Set_Has_Discriminants  (Id, Has_Discriminants     (T));
2410                Set_Is_Constrained     (Id, Is_Constrained        (T));
2411                Set_First_Entity       (Id, First_Entity          (T));
2412                Set_Last_Entity        (Id, Last_Entity           (T));
2413                Set_Private_Dependents (Id, New_Elmt_List);
2414                Set_Is_Limited_Record  (Id, Is_Limited_Record     (T));
2415                Set_Has_Unknown_Discriminants
2416                                       (Id, Has_Unknown_Discriminants (T));
2417
2418                if Is_Tagged_Type (T) then
2419                   Set_Is_Tagged_Type  (Id);
2420                   Set_Is_Abstract     (Id, Is_Abstract (T));
2421                   Set_Primitive_Operations
2422                                         (Id, Primitive_Operations (T));
2423                   Set_Class_Wide_Type (Id, Class_Wide_Type (T));
2424                end if;
2425
2426                --  In general the attributes of the subtype of a private
2427                --  type are the attributes of the partial view of parent.
2428                --  However, the full view may be a discriminated type,
2429                --  and the subtype must share the discriminant constraint
2430                --  to generate correct calls to initialization procedures.
2431
2432                if Has_Discriminants (T) then
2433                   Set_Discriminant_Constraint
2434                                      (Id, Discriminant_Constraint (T));
2435                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
2436
2437                elsif Present (Full_View (T))
2438                  and then Has_Discriminants (Full_View (T))
2439                then
2440                   Set_Discriminant_Constraint
2441                                (Id, Discriminant_Constraint (Full_View (T)));
2442                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
2443
2444                   --  This would seem semantically correct, but apparently
2445                   --  confuses the back-end (4412-009). To be explained ???
2446
2447                   --  Set_Has_Discriminants (Id);
2448                end if;
2449
2450                Prepare_Private_Subtype_Completion (Id, N);
2451
2452             when Access_Kind =>
2453                Set_Ekind             (Id, E_Access_Subtype);
2454                Set_Is_Constrained    (Id, Is_Constrained        (T));
2455                Set_Is_Access_Constant
2456                                      (Id, Is_Access_Constant    (T));
2457                Set_Directly_Designated_Type
2458                                      (Id, Designated_Type       (T));
2459
2460                --  Ada 2005 (AI-231): Propagate the null-excluding attribute
2461                --  and carry out some static checks
2462
2463                if Null_Exclusion_Present (N)
2464                  or else Can_Never_Be_Null (T)
2465                then
2466                   Set_Can_Never_Be_Null (Id);
2467
2468                   if Null_Exclusion_Present (N)
2469                     and then Can_Never_Be_Null (T)
2470                   then
2471                      Error_Msg_N
2472                        ("(Ada 2005) null exclusion not allowed if parent "
2473                         & "is already non-null", Subtype_Indication (N));
2474                   end if;
2475                end if;
2476
2477                --  A Pure library_item must not contain the declaration of a
2478                --  named access type, except within a subprogram, generic
2479                --  subprogram, task unit, or protected unit (RM 10.2.1(16)).
2480
2481                if Comes_From_Source (Id)
2482                  and then In_Pure_Unit
2483                  and then not In_Subprogram_Task_Protected_Unit
2484                then
2485                   Error_Msg_N
2486                     ("named access types not allowed in pure unit", N);
2487                end if;
2488
2489             when Concurrent_Kind =>
2490                Set_Ekind                (Id, Subtype_Kind (Ekind   (T)));
2491                Set_Corresponding_Record_Type (Id,
2492                                          Corresponding_Record_Type (T));
2493                Set_First_Entity         (Id, First_Entity          (T));
2494                Set_First_Private_Entity (Id, First_Private_Entity  (T));
2495                Set_Has_Discriminants    (Id, Has_Discriminants     (T));
2496                Set_Is_Constrained       (Id, Is_Constrained        (T));
2497                Set_Last_Entity          (Id, Last_Entity           (T));
2498
2499                if Has_Discriminants (T) then
2500                   Set_Discriminant_Constraint (Id,
2501                                            Discriminant_Constraint (T));
2502                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
2503                end if;
2504
2505             --  If the subtype name denotes an incomplete type
2506             --  an error was already reported by Process_Subtype.
2507
2508             when E_Incomplete_Type =>
2509                Set_Etype (Id, Any_Type);
2510
2511             when others =>
2512                raise Program_Error;
2513          end case;
2514       end if;
2515
2516       if Etype (Id) = Any_Type then
2517          return;
2518       end if;
2519
2520       --  Some common processing on all types
2521
2522       Set_Size_Info      (Id,                 T);
2523       Set_First_Rep_Item (Id, First_Rep_Item (T));
2524
2525       T := Etype (Id);
2526
2527       Set_Is_Immediately_Visible (Id, True);
2528       Set_Depends_On_Private     (Id, Has_Private_Component (T));
2529
2530       if Present (Generic_Parent_Type (N))
2531         and then
2532           (Nkind
2533              (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
2534             or else Nkind
2535               (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
2536                 /=  N_Formal_Private_Type_Definition)
2537       then
2538          if Is_Tagged_Type (Id) then
2539             if Is_Class_Wide_Type (Id) then
2540                Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
2541             else
2542                Derive_Subprograms (Generic_Parent_Type (N), Id, T);
2543             end if;
2544
2545          elsif Scope (Etype (Id)) /= Standard_Standard then
2546             Derive_Subprograms (Generic_Parent_Type (N), Id);
2547          end if;
2548       end if;
2549
2550       if Is_Private_Type (T)
2551         and then Present (Full_View (T))
2552       then
2553          Conditional_Delay (Id, Full_View (T));
2554
2555       --  The subtypes of components or subcomponents of protected types
2556       --  do not need freeze nodes, which would otherwise appear in the
2557       --  wrong scope (before the freeze node for the protected type). The
2558       --  proper subtypes are those of the subcomponents of the corresponding
2559       --  record.
2560
2561       elsif Ekind (Scope (Id)) /= E_Protected_Type
2562         and then Present (Scope (Scope (Id))) -- error defense!
2563         and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
2564       then
2565          Conditional_Delay (Id, T);
2566       end if;
2567
2568       --  Check that constraint_error is raised for a scalar subtype
2569       --  indication when the lower or upper bound of a non-null range
2570       --  lies outside the range of the type mark.
2571
2572       if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
2573          if Is_Scalar_Type (Etype (Id))
2574             and then Scalar_Range (Id) /=
2575                      Scalar_Range (Etype (Subtype_Mark
2576                                            (Subtype_Indication (N))))
2577          then
2578             Apply_Range_Check
2579               (Scalar_Range (Id),
2580                Etype (Subtype_Mark (Subtype_Indication (N))));
2581
2582          elsif Is_Array_Type (Etype (Id))
2583            and then Present (First_Index (Id))
2584          then
2585             --  This really should be a subprogram that finds the indications
2586             --  to check???
2587
2588             if ((Nkind (First_Index (Id)) = N_Identifier
2589                    and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
2590                  or else Nkind (First_Index (Id)) = N_Subtype_Indication)
2591               and then
2592                 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
2593             then
2594                declare
2595                   Target_Typ : constant Entity_Id :=
2596                                  Etype
2597                                    (First_Index (Etype
2598                                      (Subtype_Mark (Subtype_Indication (N)))));
2599                begin
2600                   R_Checks :=
2601                     Range_Check
2602                       (Scalar_Range (Etype (First_Index (Id))),
2603                        Target_Typ,
2604                        Etype (First_Index (Id)),
2605                        Defining_Identifier (N));
2606
2607                   Insert_Range_Checks
2608                     (R_Checks,
2609                      N,
2610                      Target_Typ,
2611                      Sloc (Defining_Identifier (N)));
2612                end;
2613             end if;
2614          end if;
2615       end if;
2616
2617       Check_Eliminated (Id);
2618    end Analyze_Subtype_Declaration;
2619
2620    --------------------------------
2621    -- Analyze_Subtype_Indication --
2622    --------------------------------
2623
2624    procedure Analyze_Subtype_Indication (N : Node_Id) is
2625       T : constant Entity_Id := Subtype_Mark (N);
2626       R : constant Node_Id   := Range_Expression (Constraint (N));
2627
2628    begin
2629       Analyze (T);
2630
2631       if R /= Error then
2632          Analyze (R);
2633          Set_Etype (N, Etype (R));
2634       else
2635          Set_Error_Posted (R);
2636          Set_Error_Posted (T);
2637       end if;
2638    end Analyze_Subtype_Indication;
2639
2640    ------------------------------
2641    -- Analyze_Type_Declaration --
2642    ------------------------------
2643
2644    procedure Analyze_Type_Declaration (N : Node_Id) is
2645       Def    : constant Node_Id   := Type_Definition (N);
2646       Def_Id : constant Entity_Id := Defining_Identifier (N);
2647       T      : Entity_Id;
2648       Prev   : Entity_Id;
2649
2650       Is_Remote : constant Boolean :=
2651                     (Is_Remote_Types (Current_Scope)
2652                           or else Is_Remote_Call_Interface (Current_Scope))
2653                        and then not (In_Private_Part (Current_Scope)
2654                                        or else
2655                                      In_Package_Body (Current_Scope));
2656
2657    begin
2658       Prev := Find_Type_Name (N);
2659
2660       --  The full view, if present, now points to the current type
2661
2662       --  Ada 2005 (AI-50217): If the type was previously decorated when
2663       --  imported through a LIMITED WITH clause, it appears as incomplete
2664       --  but has no full view.
2665
2666       if Ekind (Prev) = E_Incomplete_Type
2667         and then Present (Full_View (Prev))
2668       then
2669          T := Full_View (Prev);
2670       else
2671          T := Prev;
2672       end if;
2673
2674       Set_Is_Pure (T, Is_Pure (Current_Scope));
2675
2676       --  We set the flag Is_First_Subtype here. It is needed to set the
2677       --  corresponding flag for the Implicit class-wide-type created
2678       --  during tagged types processing.
2679
2680       Set_Is_First_Subtype (T, True);
2681
2682       --  Only composite types other than array types are allowed to have
2683       --  discriminants.
2684
2685       case Nkind (Def) is
2686
2687          --  For derived types, the rule will be checked once we've figured
2688          --  out the parent type.
2689
2690          when N_Derived_Type_Definition =>
2691             null;
2692
2693          --  For record types, discriminants are allowed.
2694
2695          when N_Record_Definition =>
2696             null;
2697
2698          when others =>
2699             if Present (Discriminant_Specifications (N)) then
2700                Error_Msg_N
2701                  ("elementary or array type cannot have discriminants",
2702                   Defining_Identifier
2703                   (First (Discriminant_Specifications (N))));
2704             end if;
2705       end case;
2706
2707       --  Elaborate the type definition according to kind, and generate
2708       --  subsidiary (implicit) subtypes where needed. We skip this if
2709       --  it was already done (this happens during the reanalysis that
2710       --  follows a call to the high level optimizer).
2711
2712       if not Analyzed (T) then
2713          Set_Analyzed (T);
2714
2715          case Nkind (Def) is
2716
2717             when N_Access_To_Subprogram_Definition =>
2718                Access_Subprogram_Declaration (T, Def);
2719
2720                --  If this is a remote access to subprogram, we must create
2721                --  the equivalent fat pointer type, and related subprograms.
2722
2723                if Is_Remote then
2724                   Process_Remote_AST_Declaration (N);
2725                end if;
2726
2727                --  Validate categorization rule against access type declaration
2728                --  usually a violation in Pure unit, Shared_Passive unit.
2729
2730                Validate_Access_Type_Declaration (T, N);
2731
2732             when N_Access_To_Object_Definition =>
2733                Access_Type_Declaration (T, Def);
2734
2735                --  Validate categorization rule against access type declaration
2736                --  usually a violation in Pure unit, Shared_Passive unit.
2737
2738                Validate_Access_Type_Declaration (T, N);
2739
2740                --  If we are in a Remote_Call_Interface package and define
2741                --  a RACW, Read and Write attribute must be added.
2742
2743                if Is_Remote
2744                  and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2745                then
2746                   Add_RACW_Features (Def_Id);
2747                end if;
2748
2749                --  Set no strict aliasing flag if config pragma seen
2750
2751                if Opt.No_Strict_Aliasing then
2752                   Set_No_Strict_Aliasing (Base_Type (Def_Id));
2753                end if;
2754
2755             when N_Array_Type_Definition =>
2756                Array_Type_Declaration (T, Def);
2757
2758             when N_Derived_Type_Definition =>
2759                Derived_Type_Declaration (T, N, T /= Def_Id);
2760
2761             when N_Enumeration_Type_Definition =>
2762                Enumeration_Type_Declaration (T, Def);
2763
2764             when N_Floating_Point_Definition =>
2765                Floating_Point_Type_Declaration (T, Def);
2766
2767             when N_Decimal_Fixed_Point_Definition =>
2768                Decimal_Fixed_Point_Type_Declaration (T, Def);
2769
2770             when N_Ordinary_Fixed_Point_Definition =>
2771                Ordinary_Fixed_Point_Type_Declaration (T, Def);
2772
2773             when N_Signed_Integer_Type_Definition =>
2774                Signed_Integer_Type_Declaration (T, Def);
2775
2776             when N_Modular_Type_Definition =>
2777                Modular_Type_Declaration (T, Def);
2778
2779             when N_Record_Definition =>
2780                Record_Type_Declaration (T, N, Prev);
2781
2782             when others =>
2783                raise Program_Error;
2784
2785          end case;
2786       end if;
2787
2788       if Etype (T) = Any_Type then
2789          return;
2790       end if;
2791
2792       --  Some common processing for all types
2793
2794       Set_Depends_On_Private (T, Has_Private_Component (T));
2795
2796       --  Both the declared entity, and its anonymous base type if one
2797       --  was created, need freeze nodes allocated.
2798
2799       declare
2800          B : constant Entity_Id := Base_Type (T);
2801
2802       begin
2803          --  In the case where the base type is different from the first
2804          --  subtype, we pre-allocate a freeze node, and set the proper
2805          --  link to the first subtype. Freeze_Entity will use this
2806          --  preallocated freeze node when it freezes the entity.
2807
2808          if B /= T then
2809             Ensure_Freeze_Node (B);
2810             Set_First_Subtype_Link (Freeze_Node (B), T);
2811          end if;
2812
2813          if not From_With_Type (T) then
2814             Set_Has_Delayed_Freeze (T);
2815          end if;
2816       end;
2817
2818       --  Case of T is the full declaration of some private type which has
2819       --  been swapped in Defining_Identifier (N).
2820
2821       if T /= Def_Id and then Is_Private_Type (Def_Id) then
2822          Process_Full_View (N, T, Def_Id);
2823
2824          --  Record the reference. The form of this is a little strange,
2825          --  since the full declaration has been swapped in. So the first
2826          --  parameter here represents the entity to which a reference is
2827          --  made which is the "real" entity, i.e. the one swapped in,
2828          --  and the second parameter provides the reference location.
2829
2830          Generate_Reference (T, T, 'c');
2831          Set_Completion_Referenced (Def_Id);
2832
2833       --  For completion of incomplete type, process incomplete dependents
2834       --  and always mark the full type as referenced (it is the incomplete
2835       --  type that we get for any real reference).
2836
2837       elsif Ekind (Prev) = E_Incomplete_Type then
2838          Process_Incomplete_Dependents (N, T, Prev);
2839          Generate_Reference (Prev, Def_Id, 'c');
2840          Set_Completion_Referenced (Def_Id);
2841
2842       --  If not private type or incomplete type completion, this is a real
2843       --  definition of a new entity, so record it.
2844
2845       else
2846          Generate_Definition (Def_Id);
2847       end if;
2848
2849       Check_Eliminated (Def_Id);
2850    end Analyze_Type_Declaration;
2851
2852    --------------------------
2853    -- Analyze_Variant_Part --
2854    --------------------------
2855
2856    procedure Analyze_Variant_Part (N : Node_Id) is
2857
2858       procedure Non_Static_Choice_Error (Choice : Node_Id);
2859       --  Error routine invoked by the generic instantiation below when
2860       --  the variant part has a non static choice.
2861
2862       procedure Process_Declarations (Variant : Node_Id);
2863       --  Analyzes all the declarations associated with a Variant.
2864       --  Needed by the generic instantiation below.
2865
2866       package Variant_Choices_Processing is new
2867         Generic_Choices_Processing
2868           (Get_Alternatives          => Variants,
2869            Get_Choices               => Discrete_Choices,
2870            Process_Empty_Choice      => No_OP,
2871            Process_Non_Static_Choice => Non_Static_Choice_Error,
2872            Process_Associated_Node   => Process_Declarations);
2873       use Variant_Choices_Processing;
2874       --  Instantiation of the generic choice processing package.
2875
2876       -----------------------------
2877       -- Non_Static_Choice_Error --
2878       -----------------------------
2879
2880       procedure Non_Static_Choice_Error (Choice : Node_Id) is
2881       begin
2882          Flag_Non_Static_Expr
2883            ("choice given in variant part is not static!", Choice);
2884       end Non_Static_Choice_Error;
2885
2886       --------------------------
2887       -- Process_Declarations --
2888       --------------------------
2889
2890       procedure Process_Declarations (Variant : Node_Id) is
2891       begin
2892          if not Null_Present (Component_List (Variant)) then
2893             Analyze_Declarations (Component_Items (Component_List (Variant)));
2894
2895             if Present (Variant_Part (Component_List (Variant))) then
2896                Analyze (Variant_Part (Component_List (Variant)));
2897             end if;
2898          end if;
2899       end Process_Declarations;
2900
2901       --  Variables local to Analyze_Case_Statement.
2902
2903       Discr_Name : Node_Id;
2904       Discr_Type : Entity_Id;
2905
2906       Case_Table     : Choice_Table_Type (1 .. Number_Of_Choices (N));
2907       Last_Choice    : Nat;
2908       Dont_Care      : Boolean;
2909       Others_Present : Boolean := False;
2910
2911    --  Start of processing for Analyze_Variant_Part
2912
2913    begin
2914       Discr_Name := Name (N);
2915       Analyze (Discr_Name);
2916
2917       if Ekind (Entity (Discr_Name)) /= E_Discriminant then
2918          Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
2919       end if;
2920
2921       Discr_Type := Etype (Entity (Discr_Name));
2922
2923       if not Is_Discrete_Type (Discr_Type) then
2924          Error_Msg_N
2925            ("discriminant in a variant part must be of a discrete type",
2926              Name (N));
2927          return;
2928       end if;
2929
2930       --  Call the instantiated Analyze_Choices which does the rest of the work
2931
2932       Analyze_Choices
2933         (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
2934    end Analyze_Variant_Part;
2935
2936    ----------------------------
2937    -- Array_Type_Declaration --
2938    ----------------------------
2939
2940    procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
2941       Component_Def : constant Node_Id := Component_Definition (Def);
2942       Element_Type  : Entity_Id;
2943       Implicit_Base : Entity_Id;
2944       Index         : Node_Id;
2945       Related_Id    : Entity_Id := Empty;
2946       Nb_Index      : Nat;
2947       P             : constant Node_Id := Parent (Def);
2948       Priv          : Entity_Id;
2949
2950    begin
2951       if Nkind (Def) = N_Constrained_Array_Definition then
2952          Index := First (Discrete_Subtype_Definitions (Def));
2953       else
2954          Index := First (Subtype_Marks (Def));
2955       end if;
2956
2957       --  Find proper names for the implicit types which may be public.
2958       --  in case of anonymous arrays we use the name of the first object
2959       --  of that type as prefix.
2960
2961       if No (T) then
2962          Related_Id :=  Defining_Identifier (P);
2963       else
2964          Related_Id := T;
2965       end if;
2966
2967       Nb_Index := 1;
2968
2969       while Present (Index) loop
2970          Analyze (Index);
2971          Make_Index (Index, P, Related_Id, Nb_Index);
2972          Next_Index (Index);
2973          Nb_Index := Nb_Index + 1;
2974       end loop;
2975
2976       if Present (Subtype_Indication (Component_Def)) then
2977          Element_Type := Process_Subtype (Subtype_Indication (Component_Def),
2978                                           P, Related_Id, 'C');
2979
2980       --  Ada 2005 (AI-230): Access Definition case
2981
2982       else pragma Assert (Present (Access_Definition (Component_Def)));
2983          Element_Type := Access_Definition
2984                            (Related_Nod => Related_Id,
2985                             N           => Access_Definition (Component_Def));
2986
2987          --  Ada 2005 (AI-230): In case of components that are anonymous
2988          --  access types the level of accessibility depends on the enclosing
2989          --  type declaration
2990
2991          Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
2992
2993          --  Ada 2005 (AI-254)
2994
2995          declare
2996             CD : constant Node_Id :=
2997                    Access_To_Subprogram_Definition
2998                      (Access_Definition (Component_Def));
2999          begin
3000             if Present (CD) and then Protected_Present (CD) then
3001                Element_Type :=
3002                  Replace_Anonymous_Access_To_Protected_Subprogram
3003                    (Def, Element_Type);
3004             end if;
3005          end;
3006       end if;
3007
3008       --  Constrained array case
3009
3010       if No (T) then
3011          T := Create_Itype (E_Void, P, Related_Id, 'T');
3012       end if;
3013
3014       if Nkind (Def) = N_Constrained_Array_Definition then
3015
3016          --  Establish Implicit_Base as unconstrained base type
3017
3018          Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
3019
3020          Init_Size_Align        (Implicit_Base);
3021          Set_Etype              (Implicit_Base, Implicit_Base);
3022          Set_Scope              (Implicit_Base, Current_Scope);
3023          Set_Has_Delayed_Freeze (Implicit_Base);
3024
3025          --  The constrained array type is a subtype of the unconstrained one
3026
3027          Set_Ekind          (T, E_Array_Subtype);
3028          Init_Size_Align    (T);
3029          Set_Etype          (T, Implicit_Base);
3030          Set_Scope          (T, Current_Scope);
3031          Set_Is_Constrained (T, True);
3032          Set_First_Index    (T, First (Discrete_Subtype_Definitions (Def)));
3033          Set_Has_Delayed_Freeze (T);
3034
3035          --  Complete setup of implicit base type
3036
3037          Set_First_Index    (Implicit_Base, First_Index (T));
3038          Set_Component_Type (Implicit_Base, Element_Type);
3039          Set_Has_Task       (Implicit_Base, Has_Task      (Element_Type));
3040          Set_Component_Size (Implicit_Base, Uint_0);
3041          Set_Has_Controlled_Component
3042                             (Implicit_Base, Has_Controlled_Component
3043                                                           (Element_Type)
3044                                               or else
3045                                             Is_Controlled (Element_Type));
3046          Set_Finalize_Storage_Only
3047                             (Implicit_Base, Finalize_Storage_Only
3048                                                           (Element_Type));
3049
3050       --  Unconstrained array case
3051
3052       else
3053          Set_Ekind                    (T, E_Array_Type);
3054          Init_Size_Align              (T);
3055          Set_Etype                    (T, T);
3056          Set_Scope                    (T, Current_Scope);
3057          Set_Component_Size           (T, Uint_0);
3058          Set_Is_Constrained           (T, False);
3059          Set_First_Index              (T, First (Subtype_Marks (Def)));
3060          Set_Has_Delayed_Freeze       (T, True);
3061          Set_Has_Task                 (T, Has_Task      (Element_Type));
3062          Set_Has_Controlled_Component (T, Has_Controlled_Component
3063                                                         (Element_Type)
3064                                             or else
3065                                           Is_Controlled (Element_Type));
3066          Set_Finalize_Storage_Only    (T, Finalize_Storage_Only
3067                                                         (Element_Type));
3068       end if;
3069
3070       Set_Component_Type (Base_Type (T), Element_Type);
3071
3072       if Aliased_Present (Component_Definition (Def)) then
3073          Set_Has_Aliased_Components (Etype (T));
3074       end if;
3075
3076       --  Ada 2005 (AI-231): Propagate the null-excluding attribute to the
3077       --  array to ensure that objects of this type are initialized.
3078
3079       if Ada_Version >= Ada_05
3080         and then (Null_Exclusion_Present (Component_Definition (Def))
3081                     or else Can_Never_Be_Null (Element_Type))
3082       then
3083          Set_Can_Never_Be_Null (T);
3084
3085          if Null_Exclusion_Present (Component_Definition (Def))
3086            and then Can_Never_Be_Null (Element_Type)
3087          then
3088             Error_Msg_N
3089               ("(Ada 2005) already a null-excluding type",
3090                Subtype_Indication (Component_Definition (Def)));
3091          end if;
3092       end if;
3093
3094       Priv := Private_Component (Element_Type);
3095
3096       if Present (Priv) then
3097
3098          --  Check for circular definitions
3099
3100          if Priv = Any_Type then
3101             Set_Component_Type (Etype (T), Any_Type);
3102
3103          --  There is a gap in the visibility of operations on the composite
3104          --  type only if the component type is defined in a different scope.
3105
3106          elsif Scope (Priv) = Current_Scope then
3107             null;
3108
3109          elsif Is_Limited_Type (Priv) then
3110             Set_Is_Limited_Composite (Etype (T));
3111             Set_Is_Limited_Composite (T);
3112          else
3113             Set_Is_Private_Composite (Etype (T));
3114             Set_Is_Private_Composite (T);
3115          end if;
3116       end if;
3117
3118       --  Create a concatenation operator for the new type. Internal
3119       --  array types created for packed entities do not need such, they
3120       --  are compatible with the user-defined type.
3121
3122       if Number_Dimensions (T) = 1
3123          and then not Is_Packed_Array_Type (T)
3124       then
3125          New_Concatenation_Op (T);
3126       end if;
3127
3128       --  In the case of an unconstrained array the parser has already
3129       --  verified that all the indices are unconstrained but we still
3130       --  need to make sure that the element type is constrained.
3131
3132       if Is_Indefinite_Subtype (Element_Type) then
3133          Error_Msg_N
3134            ("unconstrained element type in array declaration",
3135             Subtype_Indication (Component_Def));
3136
3137       elsif Is_Abstract (Element_Type) then
3138          Error_Msg_N
3139            ("The type of a component cannot be abstract",
3140             Subtype_Indication (Component_Def));
3141       end if;
3142
3143    end Array_Type_Declaration;
3144
3145    ------------------------------------------------------
3146    -- Replace_Anonymous_Access_To_Protected_Subprogram --
3147    ------------------------------------------------------
3148
3149    function Replace_Anonymous_Access_To_Protected_Subprogram
3150      (N      : Node_Id;
3151       Prev_E : Entity_Id) return Entity_Id
3152    is
3153       Loc : constant Source_Ptr := Sloc (N);
3154
3155       Curr_Scope : constant Scope_Stack_Entry :=
3156                      Scope_Stack.Table (Scope_Stack.Last);
3157
3158       Anon : constant Entity_Id :=
3159                Make_Defining_Identifier (Loc,
3160                  Chars => New_Internal_Name ('S'));
3161
3162       Acc  : Node_Id;
3163       Comp : Node_Id;
3164       Decl : Node_Id;
3165       P    : Node_Id := Parent (N);
3166
3167    begin
3168       Set_Is_Internal (Anon);
3169
3170       case Nkind (N) is
3171          when N_Component_Declaration       |
3172            N_Unconstrained_Array_Definition |
3173            N_Constrained_Array_Definition   =>
3174             Comp := Component_Definition (N);
3175             Acc  := Access_Definition (Component_Definition (N));
3176
3177          when N_Discriminant_Specification =>
3178             Comp := Discriminant_Type (N);
3179             Acc  := Discriminant_Type (N);
3180
3181          when N_Parameter_Specification =>
3182             Comp := Parameter_Type (N);
3183             Acc  := Parameter_Type (N);
3184
3185          when others =>
3186             raise Program_Error;
3187       end case;
3188
3189       Decl := Make_Full_Type_Declaration (Loc,
3190                 Defining_Identifier => Anon,
3191                 Type_Definition   =>
3192                   Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
3193
3194       Mark_Rewrite_Insertion (Decl);
3195
3196       --  Insert the new declaration in the nearest enclosing scope
3197
3198       while Present (P) and then not Has_Declarations (P) loop
3199          P := Parent (P);
3200       end loop;
3201
3202       pragma Assert (Present (P));
3203
3204       if Nkind (P) = N_Package_Specification then
3205          Prepend (Decl, Visible_Declarations (P));
3206       else
3207          Prepend (Decl, Declarations (P));
3208       end if;
3209
3210       --  Replace the anonymous type with an occurrence of the new declaration.
3211       --  In all cases the rewriten node does not have the null-exclusion
3212       --  attribute because (if present) it was already inherited by the
3213       --  anonymous entity (Anon). Thus, in case of components we do not
3214       --  inherit this attribute.
3215
3216       if Nkind (N) = N_Parameter_Specification then
3217          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
3218          Set_Etype (Defining_Identifier (N), Anon);
3219          Set_Null_Exclusion_Present (N, False);
3220       else
3221          Rewrite (Comp,
3222            Make_Component_Definition (Loc,
3223              Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
3224       end if;
3225
3226       Mark_Rewrite_Insertion (Comp);
3227
3228       --  Temporarily remove the current scope from the stack to add the new
3229       --  declarations to the enclosing scope
3230
3231       Scope_Stack.Decrement_Last;
3232       Analyze (Decl);
3233       Scope_Stack.Append (Curr_Scope);
3234
3235       Set_Original_Access_Type (Anon, Prev_E);
3236       return Anon;
3237    end Replace_Anonymous_Access_To_Protected_Subprogram;
3238
3239    -------------------------------
3240    -- Build_Derived_Access_Type --
3241    -------------------------------
3242
3243    procedure Build_Derived_Access_Type
3244      (N            : Node_Id;
3245       Parent_Type  : Entity_Id;
3246       Derived_Type : Entity_Id)
3247    is
3248       S : constant Node_Id := Subtype_Indication (Type_Definition (N));
3249
3250       Desig_Type      : Entity_Id;
3251       Discr           : Entity_Id;
3252       Discr_Con_Elist : Elist_Id;
3253       Discr_Con_El    : Elmt_Id;
3254       Subt            : Entity_Id;
3255
3256    begin
3257       --  Set the designated type so it is available in case this is
3258       --  an access to a self-referential type, e.g. a standard list
3259       --  type with a next pointer. Will be reset after subtype is built.
3260
3261       Set_Directly_Designated_Type
3262         (Derived_Type, Designated_Type (Parent_Type));
3263
3264       Subt := Process_Subtype (S, N);
3265
3266       if Nkind (S) /= N_Subtype_Indication
3267         and then Subt /= Base_Type (Subt)
3268       then
3269          Set_Ekind (Derived_Type, E_Access_Subtype);
3270       end if;
3271
3272       if Ekind (Derived_Type) = E_Access_Subtype then
3273          declare
3274             Pbase      : constant Entity_Id := Base_Type (Parent_Type);
3275             Ibase      : constant Entity_Id :=
3276                            Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
3277             Svg_Chars  : constant Name_Id   := Chars (Ibase);
3278             Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
3279
3280          begin
3281             Copy_Node (Pbase, Ibase);
3282
3283             Set_Chars             (Ibase, Svg_Chars);
3284             Set_Next_Entity       (Ibase, Svg_Next_E);
3285             Set_Sloc              (Ibase, Sloc (Derived_Type));
3286             Set_Scope             (Ibase, Scope (Derived_Type));
3287             Set_Freeze_Node       (Ibase, Empty);
3288             Set_Is_Frozen         (Ibase, False);
3289             Set_Comes_From_Source (Ibase, False);
3290             Set_Is_First_Subtype  (Ibase, False);
3291
3292             Set_Etype (Ibase, Pbase);
3293             Set_Etype (Derived_Type, Ibase);
3294          end;
3295       end if;
3296
3297       Set_Directly_Designated_Type
3298         (Derived_Type, Designated_Type (Subt));
3299
3300       Set_Is_Constrained     (Derived_Type, Is_Constrained (Subt));
3301       Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
3302       Set_Size_Info          (Derived_Type,                     Parent_Type);
3303       Set_RM_Size            (Derived_Type, RM_Size            (Parent_Type));
3304       Set_Depends_On_Private (Derived_Type,
3305                               Has_Private_Component (Derived_Type));
3306       Conditional_Delay      (Derived_Type, Subt);
3307
3308       --  Ada 2005 (AI-231). Set the null-exclusion attribute
3309
3310       if Null_Exclusion_Present (Type_Definition (N))
3311         or else Can_Never_Be_Null (Parent_Type)
3312       then
3313          Set_Can_Never_Be_Null (Derived_Type);
3314       end if;
3315
3316       --  Note: we do not copy the Storage_Size_Variable, since
3317       --  we always go to the root type for this information.
3318
3319       --  Apply range checks to discriminants for derived record case
3320       --  ??? THIS CODE SHOULD NOT BE HERE REALLY.
3321
3322       Desig_Type := Designated_Type (Derived_Type);
3323       if Is_Composite_Type (Desig_Type)
3324         and then (not Is_Array_Type (Desig_Type))
3325         and then Has_Discriminants (Desig_Type)
3326         and then Base_Type (Desig_Type) /= Desig_Type
3327       then
3328          Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
3329          Discr_Con_El := First_Elmt (Discr_Con_Elist);
3330
3331          Discr := First_Discriminant (Base_Type (Desig_Type));
3332          while Present (Discr_Con_El) loop
3333             Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
3334             Next_Elmt (Discr_Con_El);
3335             Next_Discriminant (Discr);
3336          end loop;
3337       end if;
3338    end Build_Derived_Access_Type;
3339
3340    ------------------------------
3341    -- Build_Derived_Array_Type --
3342    ------------------------------
3343
3344    procedure Build_Derived_Array_Type
3345      (N            : Node_Id;
3346       Parent_Type  : Entity_Id;
3347       Derived_Type : Entity_Id)
3348    is
3349       Loc           : constant Source_Ptr := Sloc (N);
3350       Tdef          : constant Node_Id    := Type_Definition (N);
3351       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
3352       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
3353       Implicit_Base : Entity_Id;
3354       New_Indic     : Node_Id;
3355
3356       procedure Make_Implicit_Base;
3357       --  If the parent subtype is constrained, the derived type is a
3358       --  subtype of an implicit base type derived from the parent base.
3359
3360       ------------------------
3361       -- Make_Implicit_Base --
3362       ------------------------
3363
3364       procedure Make_Implicit_Base is
3365       begin
3366          Implicit_Base :=
3367            Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
3368
3369          Set_Ekind (Implicit_Base, Ekind (Parent_Base));
3370          Set_Etype (Implicit_Base, Parent_Base);
3371
3372          Copy_Array_Subtype_Attributes   (Implicit_Base, Parent_Base);
3373          Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
3374
3375          Set_Has_Delayed_Freeze (Implicit_Base, True);
3376       end Make_Implicit_Base;
3377
3378    --  Start of processing for Build_Derived_Array_Type
3379
3380    begin
3381       if not Is_Constrained (Parent_Type) then
3382          if Nkind (Indic) /= N_Subtype_Indication then
3383             Set_Ekind (Derived_Type, E_Array_Type);
3384
3385             Copy_Array_Subtype_Attributes   (Derived_Type, Parent_Type);
3386             Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
3387
3388             Set_Has_Delayed_Freeze (Derived_Type, True);
3389
3390          else
3391             Make_Implicit_Base;
3392             Set_Etype (Derived_Type, Implicit_Base);
3393
3394             New_Indic :=
3395               Make_Subtype_Declaration (Loc,
3396                 Defining_Identifier => Derived_Type,
3397                 Subtype_Indication  =>
3398                   Make_Subtype_Indication (Loc,
3399                     Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
3400                     Constraint => Constraint (Indic)));
3401
3402             Rewrite (N, New_Indic);
3403             Analyze (N);
3404          end if;
3405
3406       else
3407          if Nkind (Indic) /= N_Subtype_Indication then
3408             Make_Implicit_Base;
3409
3410             Set_Ekind             (Derived_Type, Ekind (Parent_Type));
3411             Set_Etype             (Derived_Type, Implicit_Base);
3412             Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
3413
3414          else
3415             Error_Msg_N ("illegal constraint on constrained type", Indic);
3416          end if;
3417       end if;
3418
3419       --  If the parent type is not a derived type itself, and is
3420       --  declared in a closed scope (e.g., a subprogram), then we
3421       --  need to explicitly introduce the new type's concatenation
3422       --  operator since Derive_Subprograms will not inherit the
3423       --  parent's operator. If the parent type is unconstrained, the
3424       --  operator is of the unconstrained base type.
3425
3426       if Number_Dimensions (Parent_Type) = 1
3427         and then not Is_Limited_Type (Parent_Type)
3428         and then not Is_Derived_Type (Parent_Type)
3429         and then not Is_Package (Scope (Base_Type (Parent_Type)))
3430       then
3431          if not Is_Constrained (Parent_Type)
3432            and then Is_Constrained (Derived_Type)
3433          then
3434             New_Concatenation_Op (Implicit_Base);
3435          else
3436             New_Concatenation_Op (Derived_Type);
3437          end if;
3438       end if;
3439    end Build_Derived_Array_Type;
3440
3441    -----------------------------------
3442    -- Build_Derived_Concurrent_Type --
3443    -----------------------------------
3444
3445    procedure Build_Derived_Concurrent_Type
3446      (N            : Node_Id;
3447       Parent_Type  : Entity_Id;
3448       Derived_Type : Entity_Id)
3449    is
3450       D_Constraint : Node_Id;
3451       Disc_Spec    : Node_Id;
3452       Old_Disc     : Entity_Id;
3453       New_Disc     : Entity_Id;
3454
3455       Constraint_Present : constant Boolean :=
3456                              Nkind (Subtype_Indication (Type_Definition (N)))
3457                                                      = N_Subtype_Indication;
3458
3459    begin
3460       Set_Stored_Constraint (Derived_Type, No_Elist);
3461
3462       if Is_Task_Type (Parent_Type) then
3463          Set_Storage_Size_Variable (Derived_Type,
3464            Storage_Size_Variable (Parent_Type));
3465       end if;
3466
3467       if Present (Discriminant_Specifications (N)) then
3468          New_Scope (Derived_Type);
3469          Check_Or_Process_Discriminants (N, Derived_Type);
3470          End_Scope;
3471
3472       elsif Constraint_Present then
3473
3474          --  Build constrained subtype and derive from it
3475
3476          declare
3477             Loc  : constant Source_Ptr := Sloc (N);
3478             Anon : constant Entity_Id :=
3479                      Make_Defining_Identifier (Loc,
3480                        New_External_Name (Chars (Derived_Type), 'T'));
3481             Decl : Node_Id;
3482
3483          begin
3484             Decl :=
3485               Make_Subtype_Declaration (Loc,
3486                 Defining_Identifier => Anon,
3487                 Subtype_Indication =>
3488                   New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
3489             Insert_Before (N, Decl);
3490             Rewrite (Subtype_Indication (Type_Definition (N)),
3491               New_Occurrence_Of (Anon, Loc));
3492             Analyze (Decl);
3493             Set_Analyzed (Derived_Type, False);
3494             Analyze (N);
3495             return;
3496          end;
3497       end if;
3498
3499       --  All attributes are inherited from parent. In particular,
3500       --  entries and the corresponding record type are the same.
3501       --  Discriminants may be renamed, and must be treated separately.
3502
3503       Set_Has_Discriminants
3504         (Derived_Type, Has_Discriminants         (Parent_Type));
3505       Set_Corresponding_Record_Type
3506         (Derived_Type, Corresponding_Record_Type (Parent_Type));
3507
3508       if Constraint_Present then
3509
3510          if not Has_Discriminants (Parent_Type) then
3511             Error_Msg_N ("untagged parent must have discriminants", N);
3512
3513          elsif Present (Discriminant_Specifications (N)) then
3514
3515             --  Verify that new discriminants are used to constrain
3516             --  the old ones.
3517
3518             Old_Disc   := First_Discriminant (Parent_Type);
3519             New_Disc   := First_Discriminant (Derived_Type);
3520             Disc_Spec  := First (Discriminant_Specifications (N));
3521             D_Constraint :=
3522               First
3523                 (Constraints
3524                   (Constraint (Subtype_Indication (Type_Definition (N)))));
3525
3526             while Present (Old_Disc) and then Present (Disc_Spec) loop
3527
3528                if Nkind (Discriminant_Type (Disc_Spec)) /=
3529                                               N_Access_Definition
3530                then
3531                   Analyze (Discriminant_Type (Disc_Spec));
3532
3533                   if not Subtypes_Statically_Compatible (
3534                              Etype (Discriminant_Type (Disc_Spec)),
3535                                Etype (Old_Disc))
3536                   then
3537                      Error_Msg_N
3538                        ("not statically compatible with parent discriminant",
3539                         Discriminant_Type (Disc_Spec));
3540                   end if;
3541                end if;
3542
3543                if Nkind (D_Constraint) = N_Identifier
3544                  and then Chars (D_Constraint) /=
3545                    Chars (Defining_Identifier (Disc_Spec))
3546                then
3547                   Error_Msg_N ("new discriminants must constrain old ones",
3548                     D_Constraint);
3549                else
3550                   Set_Corresponding_Discriminant (New_Disc, Old_Disc);
3551                end if;
3552
3553                Next_Discriminant (Old_Disc);
3554                Next_Discriminant (New_Disc);
3555                Next (Disc_Spec);
3556             end loop;
3557
3558             if Present (Old_Disc) or else Present (Disc_Spec) then
3559                Error_Msg_N ("discriminant mismatch in derivation", N);
3560             end if;
3561
3562          end if;
3563
3564       elsif Present (Discriminant_Specifications (N)) then
3565          Error_Msg_N
3566            ("missing discriminant constraint in untagged derivation",
3567             N);
3568       end if;
3569
3570       if Present (Discriminant_Specifications (N)) then
3571
3572          Old_Disc := First_Discriminant (Parent_Type);
3573
3574          while Present (Old_Disc) loop
3575
3576             if No (Next_Entity (Old_Disc))
3577               or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
3578             then
3579                Set_Next_Entity (Last_Entity (Derived_Type),
3580                                          Next_Entity (Old_Disc));
3581                exit;
3582             end if;
3583
3584             Next_Discriminant (Old_Disc);
3585          end loop;
3586
3587       else
3588          Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
3589          if Has_Discriminants (Parent_Type) then
3590             Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
3591             Set_Discriminant_Constraint (
3592               Derived_Type, Discriminant_Constraint (Parent_Type));
3593          end if;
3594       end if;
3595
3596       Set_Last_Entity  (Derived_Type, Last_Entity  (Parent_Type));
3597
3598       Set_Has_Completion (Derived_Type);
3599    end Build_Derived_Concurrent_Type;
3600
3601    ------------------------------------
3602    -- Build_Derived_Enumeration_Type --
3603    ------------------------------------
3604
3605    procedure Build_Derived_Enumeration_Type
3606      (N            : Node_Id;
3607       Parent_Type  : Entity_Id;
3608       Derived_Type : Entity_Id)
3609    is
3610       Loc           : constant Source_Ptr := Sloc (N);
3611       Def           : constant Node_Id    := Type_Definition (N);
3612       Indic         : constant Node_Id    := Subtype_Indication (Def);
3613       Implicit_Base : Entity_Id;
3614       Literal       : Entity_Id;
3615       New_Lit       : Entity_Id;
3616       Literals_List : List_Id;
3617       Type_Decl     : Node_Id;
3618       Hi, Lo        : Node_Id;
3619       Rang_Expr     : Node_Id;
3620
3621    begin
3622       --  Since types Standard.Character and Standard.Wide_Character do
3623       --  not have explicit literals lists we need to process types derived
3624       --  from them specially. This is handled by Derived_Standard_Character.
3625       --  If the parent type is a generic type, there are no literals either,
3626       --  and we construct the same skeletal representation as for the generic
3627       --  parent type.
3628
3629       if Root_Type (Parent_Type) = Standard_Character
3630         or else Root_Type (Parent_Type) = Standard_Wide_Character
3631       then
3632          Derived_Standard_Character (N, Parent_Type, Derived_Type);
3633
3634       elsif Is_Generic_Type (Root_Type (Parent_Type)) then
3635          declare
3636             Lo : Node_Id;
3637             Hi : Node_Id;
3638
3639          begin
3640             Lo :=
3641                Make_Attribute_Reference (Loc,
3642                  Attribute_Name => Name_First,
3643                  Prefix => New_Reference_To (Derived_Type, Loc));
3644             Set_Etype (Lo, Derived_Type);
3645
3646             Hi :=
3647                Make_Attribute_Reference (Loc,
3648                  Attribute_Name => Name_Last,
3649                  Prefix => New_Reference_To (Derived_Type, Loc));
3650             Set_Etype (Hi, Derived_Type);
3651
3652             Set_Scalar_Range (Derived_Type,
3653                Make_Range (Loc,
3654                  Low_Bound => Lo,
3655                  High_Bound => Hi));
3656          end;
3657
3658       else
3659          --  If a constraint is present, analyze the bounds to catch
3660          --  premature usage of the derived literals.
3661
3662          if Nkind (Indic) = N_Subtype_Indication
3663            and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
3664          then
3665             Analyze (Low_Bound  (Range_Expression (Constraint (Indic))));
3666             Analyze (High_Bound (Range_Expression (Constraint (Indic))));
3667          end if;
3668
3669          --  Introduce an implicit base type for the derived type even
3670          --  if there is no constraint attached to it, since this seems
3671          --  closer to the Ada semantics. Build a full type declaration
3672          --  tree for the derived type using the implicit base type as
3673          --  the defining identifier. The build a subtype declaration
3674          --  tree which applies the constraint (if any) have it replace
3675          --  the derived type declaration.
3676
3677          Literal := First_Literal (Parent_Type);
3678          Literals_List := New_List;
3679
3680          while Present (Literal)
3681            and then Ekind (Literal) = E_Enumeration_Literal
3682          loop
3683             --  Literals of the derived type have the same representation as
3684             --  those of the parent type, but this representation can be
3685             --  overridden by an explicit representation clause. Indicate
3686             --  that there is no explicit representation given yet. These
3687             --  derived literals are implicit operations of the new type,
3688             --  and can be overriden by explicit ones.
3689
3690             if Nkind (Literal) = N_Defining_Character_Literal then
3691                New_Lit :=
3692                  Make_Defining_Character_Literal (Loc, Chars (Literal));
3693             else
3694                New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
3695             end if;
3696
3697             Set_Ekind                (New_Lit, E_Enumeration_Literal);
3698             Set_Enumeration_Pos      (New_Lit, Enumeration_Pos (Literal));
3699             Set_Enumeration_Rep      (New_Lit, Enumeration_Rep (Literal));
3700             Set_Enumeration_Rep_Expr (New_Lit, Empty);
3701             Set_Alias                (New_Lit, Literal);
3702             Set_Is_Known_Valid       (New_Lit, True);
3703
3704             Append (New_Lit, Literals_List);
3705             Next_Literal (Literal);
3706          end loop;
3707
3708          Implicit_Base :=
3709            Make_Defining_Identifier (Sloc (Derived_Type),
3710              New_External_Name (Chars (Derived_Type), 'B'));
3711
3712          --  Indicate the proper nature of the derived type. This must
3713          --  be done before analysis of the literals, to recognize cases
3714          --  when a literal may be hidden by a previous explicit function
3715          --  definition (cf. c83031a).
3716
3717          Set_Ekind (Derived_Type, E_Enumeration_Subtype);
3718          Set_Etype (Derived_Type, Implicit_Base);
3719
3720          Type_Decl :=
3721            Make_Full_Type_Declaration (Loc,
3722              Defining_Identifier => Implicit_Base,
3723              Discriminant_Specifications => No_List,
3724              Type_Definition =>
3725                Make_Enumeration_Type_Definition (Loc, Literals_List));
3726
3727          Mark_Rewrite_Insertion (Type_Decl);
3728          Insert_Before (N, Type_Decl);
3729          Analyze (Type_Decl);
3730
3731          --  After the implicit base is analyzed its Etype needs to be
3732          --  changed to reflect the fact that it is derived from the
3733          --  parent type which was ignored during analysis. We also set
3734          --  the size at this point.
3735
3736          Set_Etype (Implicit_Base, Parent_Type);
3737
3738          Set_Size_Info      (Implicit_Base,                 Parent_Type);
3739          Set_RM_Size        (Implicit_Base, RM_Size        (Parent_Type));
3740          Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
3741
3742          Set_Has_Non_Standard_Rep
3743                             (Implicit_Base, Has_Non_Standard_Rep
3744                                                            (Parent_Type));
3745          Set_Has_Delayed_Freeze (Implicit_Base);
3746
3747          --  Process the subtype indication including a validation check
3748          --  on the constraint, if any. If a constraint is given, its bounds
3749          --  must be implicitly converted to the new type.
3750
3751          if Nkind (Indic) = N_Subtype_Indication then
3752
3753             declare
3754                R   : constant Node_Id :=
3755                        Range_Expression (Constraint (Indic));
3756
3757             begin
3758                if Nkind (R) = N_Range then
3759                   Hi := Build_Scalar_Bound
3760                           (High_Bound (R), Parent_Type, Implicit_Base);
3761                   Lo := Build_Scalar_Bound
3762                           (Low_Bound  (R), Parent_Type, Implicit_Base);
3763
3764                else
3765                   --  Constraint is a Range attribute. Replace with the
3766                   --  explicit mention of the bounds of the prefix, which
3767                   --  must be a subtype.
3768
3769                   Analyze (Prefix (R));
3770                   Hi :=
3771                     Convert_To (Implicit_Base,
3772                       Make_Attribute_Reference (Loc,
3773                         Attribute_Name => Name_Last,
3774                         Prefix =>
3775                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
3776
3777                   Lo :=
3778                     Convert_To (Implicit_Base,
3779                       Make_Attribute_Reference (Loc,
3780                         Attribute_Name => Name_First,
3781                         Prefix =>
3782                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
3783                end if;
3784
3785             end;
3786
3787          else
3788             Hi :=
3789               Build_Scalar_Bound
3790                 (Type_High_Bound (Parent_Type),
3791                  Parent_Type, Implicit_Base);
3792             Lo :=
3793                Build_Scalar_Bound
3794                  (Type_Low_Bound (Parent_Type),
3795                   Parent_Type, Implicit_Base);
3796          end if;
3797
3798          Rang_Expr :=
3799            Make_Range (Loc,
3800              Low_Bound  => Lo,
3801              High_Bound => Hi);
3802
3803          --  If we constructed a default range for the case where no range
3804          --  was given, then the expressions in the range must not freeze
3805          --  since they do not correspond to expressions in the source.
3806
3807          if Nkind (Indic) /= N_Subtype_Indication then
3808             Set_Must_Not_Freeze (Lo);
3809             Set_Must_Not_Freeze (Hi);
3810             Set_Must_Not_Freeze (Rang_Expr);
3811          end if;
3812
3813          Rewrite (N,
3814            Make_Subtype_Declaration (Loc,
3815              Defining_Identifier => Derived_Type,
3816              Subtype_Indication =>
3817                Make_Subtype_Indication (Loc,
3818                  Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
3819                  Constraint =>
3820                    Make_Range_Constraint (Loc,
3821                      Range_Expression => Rang_Expr))));
3822
3823          Analyze (N);
3824
3825          --  If pragma Discard_Names applies on the first subtype
3826          --  of the parent type, then it must be applied on this
3827          --  subtype as well.
3828
3829          if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
3830             Set_Discard_Names (Derived_Type);
3831          end if;
3832
3833          --  Apply a range check. Since this range expression doesn't
3834          --  have an Etype, we have to specifically pass the Source_Typ
3835          --  parameter. Is this right???
3836
3837          if Nkind (Indic) = N_Subtype_Indication then
3838             Apply_Range_Check (Range_Expression (Constraint (Indic)),
3839                                Parent_Type,
3840                                Source_Typ => Entity (Subtype_Mark (Indic)));
3841          end if;
3842       end if;
3843    end Build_Derived_Enumeration_Type;
3844
3845    --------------------------------
3846    -- Build_Derived_Numeric_Type --
3847    --------------------------------
3848
3849    procedure Build_Derived_Numeric_Type
3850      (N            : Node_Id;
3851       Parent_Type  : Entity_Id;
3852       Derived_Type : Entity_Id)
3853    is
3854       Loc           : constant Source_Ptr := Sloc (N);
3855       Tdef          : constant Node_Id    := Type_Definition (N);
3856       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
3857       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
3858       No_Constraint : constant Boolean    := Nkind (Indic) /=
3859                                                   N_Subtype_Indication;
3860       Implicit_Base    : Entity_Id;
3861
3862       Lo : Node_Id;
3863       Hi : Node_Id;
3864
3865    begin
3866       --  Process the subtype indication including a validation check on
3867       --  the constraint if any.
3868
3869       Discard_Node (Process_Subtype (Indic, N));
3870
3871       --  Introduce an implicit base type for the derived type even if
3872       --  there is no constraint attached to it, since this seems closer
3873       --  to the Ada semantics.
3874
3875       Implicit_Base :=
3876         Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
3877
3878       Set_Etype          (Implicit_Base, Parent_Base);
3879       Set_Ekind          (Implicit_Base, Ekind          (Parent_Base));
3880       Set_Size_Info      (Implicit_Base,                 Parent_Base);
3881       Set_RM_Size        (Implicit_Base, RM_Size        (Parent_Base));
3882       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
3883       Set_Parent         (Implicit_Base, Parent (Derived_Type));
3884
3885       if Is_Discrete_Or_Fixed_Point_Type (Parent_Base) then
3886          Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
3887       end if;
3888
3889       Set_Has_Delayed_Freeze (Implicit_Base);
3890
3891       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
3892       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
3893
3894       Set_Scalar_Range (Implicit_Base,
3895         Make_Range (Loc,
3896           Low_Bound  => Lo,
3897           High_Bound => Hi));
3898
3899       if Has_Infinities (Parent_Base) then
3900          Set_Includes_Infinities (Scalar_Range (Implicit_Base));
3901       end if;
3902
3903       --  The Derived_Type, which is the entity of the declaration, is
3904       --  a subtype of the implicit base. Its Ekind is a subtype, even
3905       --  in the absence of an explicit constraint.
3906
3907       Set_Etype (Derived_Type, Implicit_Base);
3908
3909       --  If we did not have a constraint, then the Ekind is set from the
3910       --  parent type (otherwise Process_Subtype has set the bounds)
3911
3912       if No_Constraint then
3913          Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
3914       end if;
3915
3916       --  If we did not have a range constraint, then set the range
3917       --  from the parent type. Otherwise, the call to Process_Subtype
3918       --  has set the bounds.
3919
3920       if No_Constraint
3921         or else not Has_Range_Constraint (Indic)
3922       then
3923          Set_Scalar_Range (Derived_Type,
3924            Make_Range (Loc,
3925              Low_Bound  => New_Copy_Tree (Type_Low_Bound  (Parent_Type)),
3926              High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
3927          Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
3928
3929          if Has_Infinities (Parent_Type) then
3930             Set_Includes_Infinities (Scalar_Range (Derived_Type));
3931          end if;
3932       end if;
3933
3934       --  Set remaining type-specific fields, depending on numeric type
3935
3936       if Is_Modular_Integer_Type (Parent_Type) then
3937          Set_Modulus (Implicit_Base, Modulus (Parent_Base));
3938
3939          Set_Non_Binary_Modulus
3940            (Implicit_Base, Non_Binary_Modulus (Parent_Base));
3941
3942       elsif Is_Floating_Point_Type (Parent_Type) then
3943
3944          --  Digits of base type is always copied from the digits value of
3945          --  the parent base type, but the digits of the derived type will
3946          --  already have been set if there was a constraint present.
3947
3948          Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
3949          Set_Vax_Float    (Implicit_Base, Vax_Float    (Parent_Base));
3950
3951          if No_Constraint then
3952             Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
3953          end if;
3954
3955       elsif Is_Fixed_Point_Type (Parent_Type) then
3956
3957          --  Small of base type and derived type are always copied from
3958          --  the parent base type, since smalls never change. The delta
3959          --  of the base type is also copied from the parent base type.
3960          --  However the delta of the derived type will have been set
3961          --  already if a constraint was present.
3962
3963          Set_Small_Value (Derived_Type,  Small_Value (Parent_Base));
3964          Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
3965          Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
3966
3967          if No_Constraint then
3968             Set_Delta_Value (Derived_Type,  Delta_Value (Parent_Type));
3969          end if;
3970
3971          --  The scale and machine radix in the decimal case are always
3972          --  copied from the parent base type.
3973
3974          if Is_Decimal_Fixed_Point_Type (Parent_Type) then
3975             Set_Scale_Value (Derived_Type,  Scale_Value (Parent_Base));
3976             Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
3977
3978             Set_Machine_Radix_10
3979               (Derived_Type,  Machine_Radix_10 (Parent_Base));
3980             Set_Machine_Radix_10
3981               (Implicit_Base, Machine_Radix_10 (Parent_Base));
3982
3983             Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
3984
3985             if No_Constraint then
3986                Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
3987
3988             else
3989                --  the analysis of the subtype_indication sets the
3990                --  digits value of the derived type.
3991
3992                null;
3993             end if;
3994          end if;
3995       end if;
3996
3997       --  The type of the bounds is that of the parent type, and they
3998       --  must be converted to the derived type.
3999
4000       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
4001
4002       --  The implicit_base should be frozen when the derived type is frozen,
4003       --  but note that it is used in the conversions of the bounds. For
4004       --  fixed types we delay the determination of the bounds until the proper
4005       --  freezing point. For other numeric types this is rejected by GCC, for
4006       --  reasons that are currently unclear (???), so we choose to freeze the
4007       --  implicit base now. In the case of integers and floating point types
4008       --  this is harmless because subsequent representation clauses cannot
4009       --  affect anything, but it is still baffling that we cannot use the
4010       --  same mechanism for all derived numeric types.
4011
4012       if Is_Fixed_Point_Type (Parent_Type) then
4013          Conditional_Delay (Implicit_Base, Parent_Type);
4014       else
4015          Freeze_Before (N, Implicit_Base);
4016       end if;
4017    end Build_Derived_Numeric_Type;
4018
4019    --------------------------------
4020    -- Build_Derived_Private_Type --
4021    --------------------------------
4022
4023    procedure Build_Derived_Private_Type
4024      (N             : Node_Id;
4025       Parent_Type   : Entity_Id;
4026       Derived_Type  : Entity_Id;
4027       Is_Completion : Boolean;
4028       Derive_Subps  : Boolean := True)
4029    is
4030       Der_Base    : Entity_Id;
4031       Discr       : Entity_Id;
4032       Full_Decl   : Node_Id := Empty;
4033       Full_Der    : Entity_Id;
4034       Full_P      : Entity_Id;
4035       Last_Discr  : Entity_Id;
4036       Par_Scope   : constant Entity_Id := Scope (Base_Type (Parent_Type));
4037       Swapped     : Boolean := False;
4038
4039       procedure Copy_And_Build;
4040       --  Copy derived type declaration, replace parent with its full view,
4041       --  and analyze new declaration.
4042
4043       --------------------
4044       -- Copy_And_Build --
4045       --------------------
4046
4047       procedure Copy_And_Build is
4048          Full_N  : Node_Id;
4049
4050       begin
4051          if Ekind (Parent_Type) in Record_Kind
4052            or else (Ekind (Parent_Type) in Enumeration_Kind
4053              and then Root_Type (Parent_Type) /= Standard_Character
4054              and then Root_Type (Parent_Type) /= Standard_Wide_Character
4055              and then not Is_Generic_Type (Root_Type (Parent_Type)))
4056          then
4057             Full_N := New_Copy_Tree (N);
4058             Insert_After (N, Full_N);
4059             Build_Derived_Type (
4060               Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
4061
4062          else
4063             Build_Derived_Type (
4064               N, Parent_Type, Full_Der, True, Derive_Subps => False);
4065          end if;
4066       end Copy_And_Build;
4067
4068    --  Start of processing for Build_Derived_Private_Type
4069
4070    begin
4071       if Is_Tagged_Type (Parent_Type) then
4072          Build_Derived_Record_Type
4073            (N, Parent_Type, Derived_Type, Derive_Subps);
4074          return;
4075
4076       elsif Has_Discriminants (Parent_Type) then
4077
4078          if Present (Full_View (Parent_Type)) then
4079             if not Is_Completion then
4080
4081                --  Copy declaration for subsequent analysis, to
4082                --  provide a completion for what is a private
4083                --  declaration. Indicate that the full type is
4084                --  internally generated.
4085
4086                Full_Decl := New_Copy_Tree (N);
4087                Full_Der  := New_Copy (Derived_Type);
4088                Set_Comes_From_Source (Full_Decl, False);
4089
4090                Insert_After (N, Full_Decl);
4091
4092             else
4093                --  If this is a completion, the full view being built is
4094                --  itself private. We build a subtype of the parent with
4095                --  the same constraints as this full view, to convey to the
4096                --  back end the constrained components and the size of this
4097                --  subtype. If the parent is constrained, its full view can
4098                --  serve as the underlying full view of the derived type.
4099
4100                if No (Discriminant_Specifications (N)) then
4101
4102                   if Nkind (Subtype_Indication (Type_Definition (N)))
4103                     = N_Subtype_Indication
4104                   then
4105                      Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
4106
4107                   elsif Is_Constrained (Full_View (Parent_Type)) then
4108                      Set_Underlying_Full_View (Derived_Type,
4109                        Full_View (Parent_Type));
4110                   end if;
4111
4112                else
4113                   --  If there are new discriminants, the parent subtype is
4114                   --  constrained by them, but it is not clear how to build
4115                   --  the underlying_full_view in this case ???
4116
4117                   null;
4118                end if;
4119             end if;
4120          end if;
4121
4122          --  Build partial view of derived type from partial view of parent.
4123
4124          Build_Derived_Record_Type
4125            (N, Parent_Type, Derived_Type, Derive_Subps);
4126
4127          if Present (Full_View (Parent_Type))
4128            and then not Is_Completion
4129          then
4130             if not In_Open_Scopes (Par_Scope)
4131               or else not In_Same_Source_Unit (N, Parent_Type)
4132             then
4133                --  Swap partial and full views temporarily
4134
4135                Install_Private_Declarations (Par_Scope);
4136                Install_Visible_Declarations (Par_Scope);
4137                Swapped := True;
4138             end if;
4139
4140             --  Build full view of derived type from full view of
4141             --  parent which is now installed.
4142             --  Subprograms have been derived on the partial view,
4143             --  the completion does not derive them anew.
4144
4145             if not Is_Tagged_Type (Parent_Type) then
4146                Build_Derived_Record_Type
4147                  (Full_Decl, Parent_Type, Full_Der, False);
4148             else
4149
4150                --  If full view of parent is tagged, the completion
4151                --  inherits the proper primitive operations.
4152
4153                Set_Defining_Identifier (Full_Decl, Full_Der);
4154                Build_Derived_Record_Type
4155                  (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
4156                Set_Analyzed (Full_Decl);
4157             end if;
4158
4159             if Swapped then
4160                Uninstall_Declarations (Par_Scope);
4161
4162                if In_Open_Scopes (Par_Scope) then
4163                   Install_Visible_Declarations (Par_Scope);
4164                end if;
4165             end if;
4166
4167             Der_Base := Base_Type (Derived_Type);
4168             Set_Full_View (Derived_Type, Full_Der);
4169             Set_Full_View (Der_Base, Base_Type (Full_Der));
4170
4171             --  Copy the discriminant list from full view to
4172             --  the partial views (base type and its subtype).
4173             --  Gigi requires that the partial and full views
4174             --  have the same discriminants.
4175             --  ??? Note that since the partial view is pointing
4176             --  to discriminants in the full view, their scope
4177             --  will be that of the full view. This might
4178             --  cause some front end problems and need
4179             --  adjustment?
4180
4181             Discr := First_Discriminant (Base_Type (Full_Der));
4182             Set_First_Entity (Der_Base, Discr);
4183
4184             loop
4185                Last_Discr := Discr;
4186                Next_Discriminant (Discr);
4187                exit when No (Discr);
4188             end loop;
4189
4190             Set_Last_Entity (Der_Base, Last_Discr);
4191
4192             Set_First_Entity (Derived_Type, First_Entity (Der_Base));
4193             Set_Last_Entity  (Derived_Type, Last_Entity  (Der_Base));
4194             Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
4195
4196          else
4197             --  If this is a completion, the derived type stays private
4198             --  and there is no need to create a further full view, except
4199             --  in the unusual case when the derivation is nested within a
4200             --  child unit, see below.
4201
4202             null;
4203          end if;
4204
4205       elsif Present (Full_View (Parent_Type))
4206         and then  Has_Discriminants (Full_View (Parent_Type))
4207       then
4208          if Has_Unknown_Discriminants (Parent_Type)
4209            and then Nkind (Subtype_Indication (Type_Definition (N)))
4210              = N_Subtype_Indication
4211          then
4212             Error_Msg_N
4213               ("cannot constrain type with unknown discriminants",
4214                Subtype_Indication (Type_Definition (N)));
4215             return;
4216          end if;
4217
4218          --  If full view of parent is a record type, Build full view as
4219          --  a derivation from the parent's full view. Partial view remains
4220          --  private. For code generation and linking, the full view must
4221          --  have the same public status as the partial one. This full view
4222          --  is only needed if the parent type is in an enclosing scope, so
4223          --  that the full view may actually become visible, e.g. in a child
4224          --  unit. This is both more efficient, and avoids order of freezing
4225          --  problems with the added entities.
4226
4227          if not Is_Private_Type (Full_View (Parent_Type))
4228            and then (In_Open_Scopes (Scope (Parent_Type)))
4229          then
4230             Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
4231                                               Chars (Derived_Type));
4232             Set_Is_Itype (Full_Der);
4233             Set_Has_Private_Declaration (Full_Der);
4234             Set_Has_Private_Declaration (Derived_Type);
4235             Set_Associated_Node_For_Itype (Full_Der, N);
4236             Set_Parent (Full_Der, Parent (Derived_Type));
4237             Set_Full_View (Derived_Type, Full_Der);
4238             Set_Is_Public (Full_Der, Is_Public (Derived_Type));
4239             Full_P := Full_View (Parent_Type);
4240             Exchange_Declarations (Parent_Type);
4241             Copy_And_Build;
4242             Exchange_Declarations (Full_P);
4243
4244          else
4245             Build_Derived_Record_Type
4246               (N, Full_View (Parent_Type), Derived_Type,
4247                 Derive_Subps => False);
4248          end if;
4249
4250          --  In any case, the primitive operations are inherited from
4251          --  the parent type, not from the internal full view.
4252
4253          Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
4254
4255          if Derive_Subps then
4256             Derive_Subprograms (Parent_Type, Derived_Type);
4257          end if;
4258
4259       else
4260          --  Untagged type, No discriminants on either view
4261
4262          if Nkind (Subtype_Indication (Type_Definition (N)))
4263            = N_Subtype_Indication
4264          then
4265             Error_Msg_N
4266               ("illegal constraint on type without discriminants", N);
4267          end if;
4268
4269          if Present (Discriminant_Specifications (N))
4270            and then Present (Full_View (Parent_Type))
4271            and then not Is_Tagged_Type (Full_View (Parent_Type))
4272          then
4273             Error_Msg_N
4274               ("cannot add discriminants to untagged type", N);
4275          end if;
4276
4277          Set_Stored_Constraint (Derived_Type, No_Elist);
4278          Set_Is_Constrained    (Derived_Type, Is_Constrained (Parent_Type));
4279          Set_Is_Controlled     (Derived_Type, Is_Controlled  (Parent_Type));
4280          Set_Has_Controlled_Component
4281                                (Derived_Type, Has_Controlled_Component
4282                                                              (Parent_Type));
4283
4284          --  Direct controlled types do not inherit Finalize_Storage_Only flag
4285
4286          if not Is_Controlled  (Parent_Type) then
4287             Set_Finalize_Storage_Only
4288               (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
4289          end if;
4290
4291          --  Construct the implicit full view by deriving from full
4292          --  view of the parent type. In order to get proper visibility,
4293          --  we install the parent scope and its declarations.
4294
4295          --  ??? if the parent is untagged private and its
4296          --  completion is tagged, this mechanism will not
4297          --  work because we cannot derive from the tagged
4298          --  full view unless we have an extension
4299
4300          if Present (Full_View (Parent_Type))
4301            and then not Is_Tagged_Type (Full_View (Parent_Type))
4302            and then not Is_Completion
4303          then
4304             Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
4305                                               Chars (Derived_Type));
4306             Set_Is_Itype (Full_Der);
4307             Set_Has_Private_Declaration (Full_Der);
4308             Set_Has_Private_Declaration (Derived_Type);
4309             Set_Associated_Node_For_Itype (Full_Der, N);
4310             Set_Parent (Full_Der, Parent (Derived_Type));
4311             Set_Full_View (Derived_Type, Full_Der);
4312
4313             if not In_Open_Scopes (Par_Scope) then
4314                Install_Private_Declarations (Par_Scope);
4315                Install_Visible_Declarations (Par_Scope);
4316                Copy_And_Build;
4317                Uninstall_Declarations (Par_Scope);
4318
4319             --  If parent scope is open and in another unit, and
4320             --  parent has a completion, then the derivation is taking
4321             --  place in the visible part of a child unit. In that
4322             --  case retrieve the full view of the parent momentarily.
4323
4324             elsif not In_Same_Source_Unit (N, Parent_Type) then
4325                Full_P := Full_View (Parent_Type);
4326                Exchange_Declarations (Parent_Type);
4327                Copy_And_Build;
4328                Exchange_Declarations (Full_P);
4329
4330             --  Otherwise it is a local derivation.
4331
4332             else
4333                Copy_And_Build;
4334             end if;
4335
4336             Set_Scope                (Full_Der, Current_Scope);
4337             Set_Is_First_Subtype     (Full_Der,
4338                                        Is_First_Subtype (Derived_Type));
4339             Set_Has_Size_Clause      (Full_Der, False);
4340             Set_Has_Alignment_Clause (Full_Der, False);
4341             Set_Next_Entity          (Full_Der, Empty);
4342             Set_Has_Delayed_Freeze   (Full_Der);
4343             Set_Is_Frozen            (Full_Der, False);
4344             Set_Freeze_Node          (Full_Der, Empty);
4345             Set_Depends_On_Private   (Full_Der,
4346                                         Has_Private_Component    (Full_Der));
4347             Set_Public_Status        (Full_Der);
4348          end if;
4349       end if;
4350
4351       Set_Has_Unknown_Discriminants (Derived_Type,
4352         Has_Unknown_Discriminants (Parent_Type));
4353
4354       if Is_Private_Type (Derived_Type) then
4355          Set_Private_Dependents (Derived_Type, New_Elmt_List);
4356       end if;
4357
4358       if Is_Private_Type (Parent_Type)
4359         and then Base_Type (Parent_Type) = Parent_Type
4360         and then In_Open_Scopes (Scope (Parent_Type))
4361       then
4362          Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
4363
4364          if Is_Child_Unit (Scope (Current_Scope))
4365            and then Is_Completion
4366            and then In_Private_Part (Current_Scope)
4367            and then Scope (Parent_Type) /= Current_Scope
4368          then
4369             --  This is the unusual case where a type completed by a private
4370             --  derivation occurs within a package nested in a child unit,
4371             --  and the parent is declared in an ancestor. In this case, the
4372             --  full view of the parent type will become visible in the body
4373             --  of the enclosing child, and only then will the current type
4374             --  be possibly non-private. We build a underlying full view that
4375             --  will be installed when the enclosing child body is compiled.
4376
4377             declare
4378                IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
4379
4380             begin
4381                Full_Der :=
4382                  Make_Defining_Identifier (Sloc (Derived_Type),
4383                    Chars (Derived_Type));
4384                Set_Is_Itype (Full_Der);
4385                Set_Itype (IR, Full_Der);
4386                Insert_After (N, IR);
4387
4388                --  The full view will be used to swap entities on entry/exit
4389                --  to the body, and must appear in the entity list for the
4390                --  package.
4391
4392                Append_Entity (Full_Der, Scope (Derived_Type));
4393                Set_Has_Private_Declaration (Full_Der);
4394                Set_Has_Private_Declaration (Derived_Type);
4395                Set_Associated_Node_For_Itype (Full_Der, N);
4396                Set_Parent (Full_Der, Parent (Derived_Type));
4397                Full_P := Full_View (Parent_Type);
4398                Exchange_Declarations (Parent_Type);
4399                Copy_And_Build;
4400                Exchange_Declarations (Full_P);
4401                Set_Underlying_Full_View (Derived_Type, Full_Der);
4402             end;
4403          end if;
4404       end if;
4405    end Build_Derived_Private_Type;
4406
4407    -------------------------------
4408    -- Build_Derived_Record_Type --
4409    -------------------------------
4410
4411    --  1. INTRODUCTION.
4412
4413    --  Ideally we would like to use the same model of type derivation for
4414    --  tagged and untagged record types. Unfortunately this is not quite
4415    --  possible because the semantics of representation clauses is different
4416    --  for tagged and untagged records under inheritance. Consider the
4417    --  following:
4418
4419    --     type R (...) is [tagged] record ... end record;
4420    --     type T (...) is new R (...) [with ...];
4421
4422    --  The representation clauses of T can specify a completely different
4423    --  record layout from R's. Hence the same component can be placed in
4424    --  two very different positions in objects of type T and R. If R and T
4425    --  are tagged types, representation clauses for T can only specify the
4426    --  layout of non inherited components, thus components that are common
4427    --  in R and T have the same position in objects of type R and T.
4428
4429    --  This has two implications. The first is that the entire tree for R's
4430    --  declaration needs to be copied for T in the untagged case, so that
4431    --  T can be viewed as a record type of its own with its own representation
4432    --  clauses. The second implication is the way we handle discriminants.
4433    --  Specifically, in the untagged case we need a way to communicate to Gigi
4434    --  what are the real discriminants in the record, while for the semantics
4435    --  we need to consider those introduced by the user to rename the
4436    --  discriminants in the parent type. This is handled by introducing the
4437    --  notion of stored discriminants. See below for more.
4438
4439    --  Fortunately the way regular components are inherited can be handled in
4440    --  the same way in tagged and untagged types.
4441
4442    --  To complicate things a bit more the private view of a private extension
4443    --  cannot be handled in the same way as the full view (for one thing the
4444    --  semantic rules are somewhat different). We will explain what differs
4445    --  below.
4446
4447    --  2. DISCRIMINANTS UNDER INHERITANCE.
4448
4449    --  The semantic rules governing the discriminants of derived types are
4450    --  quite subtle.
4451
4452    --   type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
4453    --      [abstract]  Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
4454
4455    --  If parent type has discriminants, then the discriminants that are
4456    --  declared in the derived type are [3.4 (11)]:
4457
4458    --  o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
4459    --    there is one;
4460
4461    --  o Otherwise, each discriminant of the parent type (implicitly
4462    --    declared in the same order with the same specifications). In this
4463    --    case, the discriminants are said to be "inherited", or if unknown in
4464    --    the parent are also unknown in the derived type.
4465
4466    --  Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
4467
4468    --  o The parent subtype shall be constrained;
4469
4470    --  o If the parent type is not a tagged type, then each discriminant of
4471    --    the derived type shall be used in the constraint defining a parent
4472    --    subtype [Implementation note: this ensures that the new discriminant
4473    --    can share storage with an existing discriminant.].
4474
4475    --  For the derived type each discriminant of the parent type is either
4476    --  inherited, constrained to equal some new discriminant of the derived
4477    --  type, or constrained to the value of an expression.
4478
4479    --  When inherited or constrained to equal some new discriminant, the
4480    --  parent discriminant and the discriminant of the derived type are said
4481    --  to "correspond".
4482
4483    --  If a discriminant of the parent type is constrained to a specific value
4484    --  in the derived type definition, then the discriminant is said to be
4485    --  "specified" by that derived type definition.
4486
4487    --  3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES.
4488
4489    --  We have spoken about stored discriminants in point 1 (introduction)
4490    --  above. There are two sort of stored discriminants: implicit and
4491    --  explicit. As long as the derived type inherits the same discriminants as
4492    --  the root record type, stored discriminants are the same as regular
4493    --  discriminants, and are said to be implicit. However, if any discriminant
4494    --  in the root type was renamed in the derived type, then the derived
4495    --  type will contain explicit stored discriminants. Explicit stored
4496    --  discriminants are discriminants in addition to the semantically visible
4497    --  discriminants defined for the derived type. Stored discriminants are
4498    --  used by Gigi to figure out what are the physical discriminants in
4499    --  objects of the derived type (see precise definition in einfo.ads).
4500    --  As an example, consider the following:
4501
4502    --           type R  (D1, D2, D3 : Int) is record ... end record;
4503    --           type T1 is new R;
4504    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
4505    --           type T3 is new T2;
4506    --           type T4 (Y : Int) is new T3 (Y, 99);
4507
4508    --  The following table summarizes the discriminants and stored
4509    --  discriminants in R and T1 through T4.
4510
4511    --   Type      Discrim     Stored Discrim  Comment
4512    --    R      (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in R
4513    --    T1     (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in T1
4514    --    T2     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T2
4515    --    T3     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T3
4516    --    T4     (Y)            (D1, D2, D3)   Girder discrims EXPLICIT in T4
4517
4518    --  Field Corresponding_Discriminant (abbreviated CD below) allows us to
4519    --  find the corresponding discriminant in the parent type, while
4520    --  Original_Record_Component (abbreviated ORC below), the actual physical
4521    --  component that is renamed. Finally the field Is_Completely_Hidden
4522    --  (abbreviated ICH below) is set for all explicit stored discriminants
4523    --  (see einfo.ads for more info). For the above example this gives:
4524
4525    --                 Discrim     CD        ORC     ICH
4526    --                 ^^^^^^^     ^^        ^^^     ^^^
4527    --                 D1 in R    empty     itself    no
4528    --                 D2 in R    empty     itself    no
4529    --                 D3 in R    empty     itself    no
4530
4531    --                 D1 in T1  D1 in R    itself    no
4532    --                 D2 in T1  D2 in R    itself    no
4533    --                 D3 in T1  D3 in R    itself    no
4534
4535    --                 X1 in T2  D3 in T1  D3 in T2   no
4536    --                 X2 in T2  D1 in T1  D1 in T2   no
4537    --                 D1 in T2   empty    itself    yes
4538    --                 D2 in T2   empty    itself    yes
4539    --                 D3 in T2   empty    itself    yes
4540
4541    --                 X1 in T3  X1 in T2  D3 in T3   no
4542    --                 X2 in T3  X2 in T2  D1 in T3   no
4543    --                 D1 in T3   empty    itself    yes
4544    --                 D2 in T3   empty    itself    yes
4545    --                 D3 in T3   empty    itself    yes
4546
4547    --                 Y  in T4  X1 in T3  D3 in T3   no
4548    --                 D1 in T3   empty    itself    yes
4549    --                 D2 in T3   empty    itself    yes
4550    --                 D3 in T3   empty    itself    yes
4551
4552    --  4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES.
4553
4554    --  Type derivation for tagged types is fairly straightforward. if no
4555    --  discriminants are specified by the derived type, these are inherited
4556    --  from the parent. No explicit stored discriminants are ever necessary.
4557    --  The only manipulation that is done to the tree is that of adding a
4558    --  _parent field with parent type and constrained to the same constraint
4559    --  specified for the parent in the derived type definition. For instance:
4560
4561    --           type R  (D1, D2, D3 : Int) is tagged record ... end record;
4562    --           type T1 is new R with null record;
4563    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
4564
4565    --  are changed into :
4566
4567    --           type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
4568    --              _parent : R (D1, D2, D3);
4569    --           end record;
4570
4571    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
4572    --              _parent : T1 (X2, 88, X1);
4573    --           end record;
4574
4575    --  The discriminants actually present in R, T1 and T2 as well as their CD,
4576    --  ORC and ICH fields are:
4577
4578    --                 Discrim     CD        ORC     ICH
4579    --                 ^^^^^^^     ^^        ^^^     ^^^
4580    --                 D1 in R    empty     itself    no
4581    --                 D2 in R    empty     itself    no
4582    --                 D3 in R    empty     itself    no
4583
4584    --                 D1 in T1  D1 in R    D1 in R   no
4585    --                 D2 in T1  D2 in R    D2 in R   no
4586    --                 D3 in T1  D3 in R    D3 in R   no
4587
4588    --                 X1 in T2  D3 in T1   D3 in R   no
4589    --                 X2 in T2  D1 in T1   D1 in R   no
4590
4591    --  5. FIRST TRANSFORMATION FOR DERIVED RECORDS.
4592    --
4593    --  Regardless of whether we dealing with a tagged or untagged type
4594    --  we will transform all derived type declarations of the form
4595    --
4596    --               type T is new R (...) [with ...];
4597    --  or
4598    --               subtype S is R (...);
4599    --               type T is new S [with ...];
4600    --  into
4601    --               type BT is new R [with ...];
4602    --               subtype T is BT (...);
4603    --
4604    --  That is, the base derived type is constrained only if it has no
4605    --  discriminants. The reason for doing this is that GNAT's semantic model
4606    --  assumes that a base type with discriminants is unconstrained.
4607    --
4608    --  Note that, strictly speaking, the above transformation is not always
4609    --  correct. Consider for instance the following excerpt from ACVC b34011a:
4610    --
4611    --       procedure B34011A is
4612    --          type REC (D : integer := 0) is record
4613    --             I : Integer;
4614    --          end record;
4615
4616    --          package P is
4617    --             type T6 is new Rec;
4618    --             function F return T6;
4619    --          end P;
4620
4621    --          use P;
4622    --          package Q6 is
4623    --             type U is new T6 (Q6.F.I);                   -- ERROR: Q6.F.
4624    --          end Q6;
4625    --
4626    --  The definition of Q6.U is illegal. However transforming Q6.U into
4627
4628    --             type BaseU is new T6;
4629    --             subtype U is BaseU (Q6.F.I)
4630
4631    --  turns U into a legal subtype, which is incorrect. To avoid this problem
4632    --  we always analyze the constraint (in this case (Q6.F.I)) before applying
4633    --  the transformation described above.
4634
4635    --  There is another instance where the above transformation is incorrect.
4636    --  Consider:
4637
4638    --          package Pack is
4639    --             type Base (D : Integer) is tagged null record;
4640    --             procedure P (X : Base);
4641
4642    --             type Der is new Base (2) with null record;
4643    --             procedure P (X : Der);
4644    --          end Pack;
4645
4646    --  Then the above transformation turns this into
4647
4648    --             type Der_Base is new Base with null record;
4649    --             --  procedure P (X : Base) is implicitly inherited here
4650    --             --  as procedure P (X : Der_Base).
4651
4652    --             subtype Der is Der_Base (2);
4653    --             procedure P (X : Der);
4654    --             --  The overriding of P (X : Der_Base) is illegal since we
4655    --             --  have a parameter conformance problem.
4656
4657    --  To get around this problem, after having semantically processed Der_Base
4658    --  and the rewritten subtype declaration for Der, we copy Der_Base field
4659    --  Discriminant_Constraint from Der so that when parameter conformance is
4660    --  checked when P is overridden, no semantic errors are flagged.
4661
4662    --  6. SECOND TRANSFORMATION FOR DERIVED RECORDS.
4663
4664    --  Regardless of whether we are dealing with a tagged or untagged type
4665    --  we will transform all derived type declarations of the form
4666
4667    --               type R (D1, .., Dn : ...) is [tagged] record ...;
4668    --               type T is new R [with ...];
4669    --  into
4670    --               type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
4671
4672    --  The reason for such transformation is that it allows us to implement a
4673    --  very clean form of component inheritance as explained below.
4674
4675    --  Note that this transformation is not achieved by direct tree rewriting
4676    --  and manipulation, but rather by redoing the semantic actions that the
4677    --  above transformation will entail. This is done directly in routine
4678    --  Inherit_Components.
4679
4680    --  7. TYPE DERIVATION AND COMPONENT INHERITANCE.
4681
4682    --  In both tagged and untagged derived types, regular non discriminant
4683    --  components are inherited in the derived type from the parent type. In
4684    --  the absence of discriminants component, inheritance is straightforward
4685    --  as components can simply be copied from the parent.
4686    --  If the parent has discriminants, inheriting components constrained with
4687    --  these discriminants requires caution. Consider the following example:
4688
4689    --      type R  (D1, D2 : Positive) is [tagged] record
4690    --         S : String (D1 .. D2);
4691    --      end record;
4692
4693    --      type T1                is new R        [with null record];
4694    --      type T2 (X : positive) is new R (1, X) [with null record];
4695
4696    --  As explained in 6. above, T1 is rewritten as
4697
4698    --      type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
4699
4700    --  which makes the treatment for T1 and T2 identical.
4701
4702    --  What we want when inheriting S, is that references to D1 and D2 in R are
4703    --  replaced with references to their correct constraints, ie D1 and D2 in
4704    --  T1 and 1 and X in T2. So all R's discriminant references are replaced
4705    --  with either discriminant references in the derived type or expressions.
4706    --  This replacement is achieved as follows: before inheriting R's
4707    --  components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
4708    --  created in the scope of T1 (resp. scope of T2) so that discriminants D1
4709    --  and D2 of T1 are visible (resp. discriminant X of T2 is visible).
4710    --  For T2, for instance, this has the effect of replacing String (D1 .. D2)
4711    --  by String (1 .. X).
4712
4713    --  8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS.
4714
4715    --  We explain here the rules governing private type extensions relevant to
4716    --  type derivation. These rules are explained on the following example:
4717
4718    --      type D [(...)] is new A [(...)] with private;      <-- partial view
4719    --      type D [(...)] is new P [(...)] with null record;  <-- full view
4720
4721    --  Type A is called the ancestor subtype of the private extension.
4722    --  Type P is the parent type of the full view of the private extension. It
4723    --  must be A or a type derived from A.
4724
4725    --  The rules concerning the discriminants of private type extensions are
4726    --  [7.3(10-13)]:
4727
4728    --  o If a private extension inherits known discriminants from the ancestor
4729    --    subtype, then the full view shall also inherit its discriminants from
4730    --    the ancestor subtype and the parent subtype of the full view shall be
4731    --    constrained if and only if the ancestor subtype is constrained.
4732
4733    --  o If a partial view has unknown discriminants, then the full view may
4734    --    define a definite or an indefinite subtype, with or without
4735    --    discriminants.
4736
4737    --  o If a partial view has neither known nor unknown discriminants, then
4738    --    the full view shall define a definite subtype.
4739
4740    --  o If the ancestor subtype of a private extension has constrained
4741    --    discriminants, then the parent subtype of the full view shall impose a
4742    --    statically matching constraint on those discriminants.
4743
4744    --  This means that only the following forms of private extensions are
4745    --  allowed:
4746
4747    --      type D is new A with private;      <-- partial view
4748    --      type D is new P with null record;  <-- full view
4749
4750    --  If A has no discriminants than P has no discriminants, otherwise P must
4751    --  inherit A's discriminants.
4752
4753    --      type D is new A (...) with private;      <-- partial view
4754    --      type D is new P (:::) with null record;  <-- full view
4755
4756    --  P must inherit A's discriminants and (...) and (:::) must statically
4757    --  match.
4758
4759    --      subtype A is R (...);
4760    --      type D is new A with private;      <-- partial view
4761    --      type D is new P with null record;  <-- full view
4762
4763    --  P must have inherited R's discriminants and must be derived from A or
4764    --  any of its subtypes.
4765
4766    --      type D (..) is new A with private;              <-- partial view
4767    --      type D (..) is new P [(:::)] with null record;  <-- full view
4768
4769    --  No specific constraints on P's discriminants or constraint (:::).
4770    --  Note that A can be unconstrained, but the parent subtype P must either
4771    --  be constrained or (:::) must be present.
4772
4773    --      type D (..) is new A [(...)] with private;      <-- partial view
4774    --      type D (..) is new P [(:::)] with null record;  <-- full view
4775
4776    --  P's constraints on A's discriminants must statically match those
4777    --  imposed by (...).
4778
4779    --  9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS.
4780
4781    --  The full view of a private extension is handled exactly as described
4782    --  above. The model chose for the private view of a private extension
4783    --  is the same for what concerns discriminants (ie they receive the same
4784    --  treatment as in the tagged case). However, the private view of the
4785    --  private extension always inherits the components of the parent base,
4786    --  without replacing any discriminant reference. Strictly speaking this
4787    --  is incorrect. However, Gigi never uses this view to generate code so
4788    --  this is a purely semantic issue. In theory, a set of transformations
4789    --  similar to those given in 5. and 6. above could be applied to private
4790    --  views of private extensions to have the same model of component
4791    --  inheritance as for non private extensions. However, this is not done
4792    --  because it would further complicate private type processing.
4793    --  Semantically speaking, this leaves us in an uncomfortable
4794    --  situation. As an example consider:
4795
4796    --          package Pack is
4797    --             type R (D : integer) is tagged record
4798    --                S : String (1 .. D);
4799    --             end record;
4800    --             procedure P (X : R);
4801    --             type T is new R (1) with private;
4802    --          private
4803    --             type T is new R (1) with null record;
4804    --          end;
4805
4806    --  This is transformed into:
4807
4808    --          package Pack is
4809    --             type R (D : integer) is tagged record
4810    --                S : String (1 .. D);
4811    --             end record;
4812    --             procedure P (X : R);
4813    --             type T is new R (1) with private;
4814    --          private
4815    --             type BaseT is new R with null record;
4816    --             subtype  T is BaseT (1);
4817    --          end;
4818
4819    --  (strictly speaking the above is incorrect Ada).
4820
4821    --  From the semantic standpoint the private view of private extension T
4822    --  should be flagged as constrained since one can clearly have
4823    --
4824    --             Obj : T;
4825    --
4826    --  in a unit withing Pack. However, when deriving subprograms for the
4827    --  private view of private extension T, T must be seen as unconstrained
4828    --  since T has discriminants (this is a constraint of the current
4829    --  subprogram derivation model). Thus, when processing the private view of
4830    --  a private extension such as T, we first mark T as unconstrained, we
4831    --  process it, we perform program derivation and just before returning from
4832    --  Build_Derived_Record_Type we mark T as constrained.
4833    --  ??? Are there are other uncomfortable cases that we will have to
4834    --      deal with.
4835
4836    --  10. RECORD_TYPE_WITH_PRIVATE complications.
4837
4838    --  Types that are derived from a visible record type and have a private
4839    --  extension present other peculiarities. They behave mostly like private
4840    --  types, but if they have primitive operations defined, these will not
4841    --  have the proper signatures for further inheritance, because other
4842    --  primitive operations will use the implicit base that we define for
4843    --  private derivations below. This affect subprogram inheritance (see
4844    --  Derive_Subprograms for details). We also derive the implicit base from
4845    --  the base type of the full view, so that the implicit base is a record
4846    --  type and not another private type, This avoids infinite loops.
4847
4848    procedure Build_Derived_Record_Type
4849      (N            : Node_Id;
4850       Parent_Type  : Entity_Id;
4851       Derived_Type : Entity_Id;
4852       Derive_Subps : Boolean := True)
4853    is
4854       Loc          : constant Source_Ptr := Sloc (N);
4855       Parent_Base  : Entity_Id;
4856
4857       Type_Def     : Node_Id;
4858       Indic        : Node_Id;
4859
4860       Discrim      : Entity_Id;
4861       Last_Discrim : Entity_Id;
4862       Constrs      : Elist_Id;
4863       Discs        : Elist_Id := New_Elmt_List;
4864       --  An empty Discs list means that there were no constraints in the
4865       --  subtype indication or that there was an error processing it.
4866
4867       Assoc_List   : Elist_Id;
4868       New_Discrs   : Elist_Id;
4869
4870       New_Base     : Entity_Id;
4871       New_Decl     : Node_Id;
4872       New_Indic    : Node_Id;
4873
4874       Is_Tagged          : constant Boolean := Is_Tagged_Type (Parent_Type);
4875       Discriminant_Specs : constant Boolean :=
4876                              Present (Discriminant_Specifications (N));
4877       Private_Extension  : constant Boolean :=
4878                              (Nkind (N) = N_Private_Extension_Declaration);
4879
4880       Constraint_Present : Boolean;
4881       Inherit_Discrims   : Boolean := False;
4882
4883       Save_Etype        : Entity_Id;
4884       Save_Discr_Constr : Elist_Id;
4885       Save_Next_Entity  : Entity_Id;
4886
4887    begin
4888       if Ekind (Parent_Type) = E_Record_Type_With_Private
4889         and then Present (Full_View (Parent_Type))
4890         and then Has_Discriminants (Parent_Type)
4891       then
4892          Parent_Base := Base_Type (Full_View (Parent_Type));
4893       else
4894          Parent_Base := Base_Type (Parent_Type);
4895       end if;
4896
4897       --  Before we start the previously documented transformations, here is
4898       --  a little fix for size and alignment of tagged types. Normally when
4899       --  we derive type D from type P, we copy the size and alignment of P
4900       --  as the default for D, and in the absence of explicit representation
4901       --  clauses for D, the size and alignment are indeed the same as the
4902       --  parent.
4903
4904       --  But this is wrong for tagged types, since fields may be added,
4905       --  and the default size may need to be larger, and the default
4906       --  alignment may need to be larger.
4907
4908       --  We therefore reset the size and alignment fields in the tagged
4909       --  case. Note that the size and alignment will in any case be at
4910       --  least as large as the parent type (since the derived type has
4911       --  a copy of the parent type in the _parent field)
4912
4913       if Is_Tagged then
4914          Init_Size_Align (Derived_Type);
4915       end if;
4916
4917       --  STEP 0a: figure out what kind of derived type declaration we have.
4918
4919       if Private_Extension then
4920          Type_Def := N;
4921          Set_Ekind (Derived_Type, E_Record_Type_With_Private);
4922
4923       else
4924          Type_Def := Type_Definition (N);
4925
4926          --  Ekind (Parent_Base) in not necessarily E_Record_Type since
4927          --  Parent_Base can be a private type or private extension. However,
4928          --  for tagged types with an extension the newly added fields are
4929          --  visible and hence the Derived_Type is always an E_Record_Type.
4930          --  (except that the parent may have its own private fields).
4931          --  For untagged types we preserve the Ekind of the Parent_Base.
4932
4933          if Present (Record_Extension_Part (Type_Def)) then
4934             Set_Ekind (Derived_Type, E_Record_Type);
4935          else
4936             Set_Ekind (Derived_Type, Ekind (Parent_Base));
4937          end if;
4938       end if;
4939
4940       --  Indic can either be an N_Identifier if the subtype indication
4941       --  contains no constraint or an N_Subtype_Indication if the subtype
4942       --  indication has a constraint.
4943
4944       Indic := Subtype_Indication (Type_Def);
4945       Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
4946
4947       --  Check that the type has visible discriminants. The type may be
4948       --  a private type with unknown discriminants whose full view has
4949       --  discriminants which are invisible.
4950
4951       if Constraint_Present then
4952          if not Has_Discriminants (Parent_Base)
4953            or else
4954              (Has_Unknown_Discriminants (Parent_Base)
4955                 and then Is_Private_Type (Parent_Base))
4956          then
4957             Error_Msg_N
4958               ("invalid constraint: type has no discriminant",
4959                  Constraint (Indic));
4960
4961             Constraint_Present := False;
4962             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
4963
4964          elsif Is_Constrained (Parent_Type) then
4965             Error_Msg_N
4966                ("invalid constraint: parent type is already constrained",
4967                   Constraint (Indic));
4968
4969             Constraint_Present := False;
4970             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
4971          end if;
4972       end if;
4973
4974       --  STEP 0b: If needed, apply transformation given in point 5. above.
4975
4976       if not Private_Extension
4977         and then Has_Discriminants (Parent_Type)
4978         and then not Discriminant_Specs
4979         and then (Is_Constrained (Parent_Type) or else Constraint_Present)
4980       then
4981          --  First, we must analyze the constraint (see comment in point 5.).
4982
4983          if Constraint_Present then
4984             New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
4985
4986             if Has_Discriminants (Derived_Type)
4987               and then Has_Private_Declaration (Derived_Type)
4988               and then Present (Discriminant_Constraint (Derived_Type))
4989             then
4990                --  Verify that constraints of the full view conform to those
4991                --  given in partial view.
4992
4993                declare
4994                   C1, C2 : Elmt_Id;
4995
4996                begin
4997                   C1 := First_Elmt (New_Discrs);
4998                   C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
4999
5000                   while Present (C1) and then Present (C2) loop
5001                      if not
5002                        Fully_Conformant_Expressions (Node (C1), Node (C2))
5003                      then
5004                         Error_Msg_N (
5005                           "constraint not conformant to previous declaration",
5006                              Node (C1));
5007                      end if;
5008                      Next_Elmt (C1);
5009                      Next_Elmt (C2);
5010                   end loop;
5011                end;
5012             end if;
5013          end if;
5014
5015          --  Insert and analyze the declaration for the unconstrained base type
5016
5017          New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
5018
5019          New_Decl :=
5020            Make_Full_Type_Declaration (Loc,
5021               Defining_Identifier => New_Base,
5022               Type_Definition     =>
5023                 Make_Derived_Type_Definition (Loc,
5024                   Abstract_Present      => Abstract_Present (Type_Def),
5025                   Subtype_Indication    =>
5026                     New_Occurrence_Of (Parent_Base, Loc),
5027                   Record_Extension_Part =>
5028                     Relocate_Node (Record_Extension_Part (Type_Def))));
5029
5030          Set_Parent (New_Decl, Parent (N));
5031          Mark_Rewrite_Insertion (New_Decl);
5032          Insert_Before (N, New_Decl);
5033
5034          --  Note that this call passes False for the Derive_Subps
5035          --  parameter because subprogram derivation is deferred until
5036          --  after creating the subtype (see below).
5037
5038          Build_Derived_Type
5039            (New_Decl, Parent_Base, New_Base,
5040             Is_Completion => True, Derive_Subps => False);
5041
5042          --  ??? This needs re-examination to determine whether the
5043          --  above call can simply be replaced by a call to Analyze.
5044
5045          Set_Analyzed (New_Decl);
5046
5047          --  Insert and analyze the declaration for the constrained subtype
5048
5049          if Constraint_Present then
5050             New_Indic :=
5051               Make_Subtype_Indication (Loc,
5052                 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
5053                 Constraint   => Relocate_Node (Constraint (Indic)));
5054
5055          else
5056             declare
5057                Constr_List : constant List_Id := New_List;
5058                C           : Elmt_Id;
5059                Expr        : Node_Id;
5060
5061             begin
5062                C := First_Elmt (Discriminant_Constraint (Parent_Type));
5063                while Present (C) loop
5064                   Expr := Node (C);
5065
5066                   --  It is safe here to call New_Copy_Tree since
5067                   --  Force_Evaluation was called on each constraint in
5068                   --  Build_Discriminant_Constraints.
5069
5070                   Append (New_Copy_Tree (Expr), To => Constr_List);
5071
5072                   Next_Elmt (C);
5073                end loop;
5074
5075                New_Indic :=
5076                  Make_Subtype_Indication (Loc,
5077                    Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
5078                    Constraint   =>
5079                      Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
5080             end;
5081          end if;
5082
5083          Rewrite (N,
5084            Make_Subtype_Declaration (Loc,
5085              Defining_Identifier => Derived_Type,
5086              Subtype_Indication  => New_Indic));
5087
5088          Analyze (N);
5089
5090          --  Derivation of subprograms must be delayed until the
5091          --  full subtype has been established to ensure proper
5092          --  overriding of subprograms inherited by full types.
5093          --  If the derivations occurred as part of the call to
5094          --  Build_Derived_Type above, then the check for type
5095          --  conformance would fail because earlier primitive
5096          --  subprograms could still refer to the full type prior
5097          --  the change to the new subtype and hence wouldn't
5098          --  match the new base type created here.
5099
5100          Derive_Subprograms (Parent_Type, Derived_Type);
5101
5102          --  For tagged types the Discriminant_Constraint of the new base itype
5103          --  is inherited from the first subtype so that no subtype conformance
5104          --  problem arise when the first subtype overrides primitive
5105          --  operations inherited by the implicit base type.
5106
5107          if Is_Tagged then
5108             Set_Discriminant_Constraint
5109               (New_Base, Discriminant_Constraint (Derived_Type));
5110          end if;
5111
5112          return;
5113       end if;
5114
5115       --  If we get here Derived_Type will have no discriminants or it will be
5116       --  a discriminated unconstrained base type.
5117
5118       --  STEP 1a: perform preliminary actions/checks for derived tagged types
5119
5120       if Is_Tagged then
5121          --  The parent type is frozen for non-private extensions (RM 13.14(7))
5122
5123          if not Private_Extension then
5124             Freeze_Before (N, Parent_Type);
5125          end if;
5126
5127          if Type_Access_Level (Derived_Type) /= Type_Access_Level (Parent_Type)
5128            and then not Is_Generic_Type (Derived_Type)
5129          then
5130             if Is_Controlled (Parent_Type) then
5131                Error_Msg_N
5132                  ("controlled type must be declared at the library level",
5133                   Indic);
5134             else
5135                Error_Msg_N
5136                  ("type extension at deeper accessibility level than parent",
5137                   Indic);
5138             end if;
5139
5140          else
5141             declare
5142                GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
5143
5144             begin
5145                if Present (GB)
5146                  and then GB /= Enclosing_Generic_Body (Parent_Base)
5147                then
5148                   Error_Msg_NE
5149                     ("parent type of& must not be outside generic body"
5150                        & " ('R'M 3.9.1(4))",
5151                          Indic, Derived_Type);
5152                end if;
5153             end;
5154          end if;
5155       end if;
5156
5157       --  STEP 1b : preliminary cleanup of the full view of private types
5158
5159       --  If the type is already marked as having discriminants, then it's the
5160       --  completion of a private type or private extension and we need to
5161       --  retain the discriminants from the partial view if the current
5162       --  declaration has Discriminant_Specifications so that we can verify
5163       --  conformance. However, we must remove any existing components that
5164       --  were inherited from the parent (and attached in Copy_And_Swap)
5165       --  because the full type inherits all appropriate components anyway, and
5166       --  we don't want the partial view's components interfering.
5167
5168       if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
5169          Discrim := First_Discriminant (Derived_Type);
5170          loop
5171             Last_Discrim := Discrim;
5172             Next_Discriminant (Discrim);
5173             exit when No (Discrim);
5174          end loop;
5175
5176          Set_Last_Entity (Derived_Type, Last_Discrim);
5177
5178       --  In all other cases wipe out the list of inherited components (even
5179       --  inherited discriminants), it will be properly rebuilt here.
5180
5181       else
5182          Set_First_Entity (Derived_Type, Empty);
5183          Set_Last_Entity  (Derived_Type, Empty);
5184       end if;
5185
5186       --  STEP 1c: Initialize some flags for the Derived_Type
5187
5188       --  The following flags must be initialized here so that
5189       --  Process_Discriminants can check that discriminants of tagged types
5190       --  do not have a default initial value and that access discriminants
5191       --  are only specified for limited records. For completeness, these
5192       --  flags are also initialized along with all the other flags below.
5193
5194       Set_Is_Tagged_Type    (Derived_Type, Is_Tagged);
5195       Set_Is_Limited_Record (Derived_Type, Is_Limited_Record (Parent_Type));
5196
5197       --  STEP 2a: process discriminants of derived type if any.
5198
5199       New_Scope (Derived_Type);
5200
5201       if Discriminant_Specs then
5202          Set_Has_Unknown_Discriminants (Derived_Type, False);
5203
5204          --  The following call initializes fields Has_Discriminants and
5205          --  Discriminant_Constraint, unless we are processing the completion
5206          --  of a private type declaration.
5207
5208          Check_Or_Process_Discriminants (N, Derived_Type);
5209
5210          --  For non-tagged types the constraint on the Parent_Type must be
5211          --  present and is used to rename the discriminants.
5212
5213          if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
5214             Error_Msg_N ("untagged parent must have discriminants", Indic);
5215
5216          elsif not Is_Tagged and then not Constraint_Present then
5217             Error_Msg_N
5218               ("discriminant constraint needed for derived untagged records",
5219                Indic);
5220
5221          --  Otherwise the parent subtype must be constrained unless we have a
5222          --  private extension.
5223
5224          elsif not Constraint_Present
5225            and then not Private_Extension
5226            and then not Is_Constrained (Parent_Type)
5227          then
5228             Error_Msg_N
5229               ("unconstrained type not allowed in this context", Indic);
5230
5231          elsif Constraint_Present then
5232             --  The following call sets the field Corresponding_Discriminant
5233             --  for the discriminants in the Derived_Type.
5234
5235             Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
5236
5237             --  For untagged types all new discriminants must rename
5238             --  discriminants in the parent. For private extensions new
5239             --  discriminants cannot rename old ones (implied by [7.3(13)]).
5240
5241             Discrim := First_Discriminant (Derived_Type);
5242
5243             while Present (Discrim) loop
5244                if not Is_Tagged
5245                  and then not Present (Corresponding_Discriminant (Discrim))
5246                then
5247                   Error_Msg_N
5248                     ("new discriminants must constrain old ones", Discrim);
5249
5250                elsif Private_Extension
5251                  and then Present (Corresponding_Discriminant (Discrim))
5252                then
5253                   Error_Msg_N
5254                     ("only static constraints allowed for parent"
5255                      & " discriminants in the partial view", Indic);
5256                   exit;
5257                end if;
5258
5259                --  If a new discriminant is used in the constraint,
5260                --  then its subtype must be statically compatible
5261                --  with the parent discriminant's subtype (3.7(15)).
5262
5263                if Present (Corresponding_Discriminant (Discrim))
5264                  and then
5265                    not Subtypes_Statically_Compatible
5266                          (Etype (Discrim),
5267                           Etype (Corresponding_Discriminant (Discrim)))
5268                then
5269                   Error_Msg_N
5270                     ("subtype must be compatible with parent discriminant",
5271                      Discrim);
5272                end if;
5273
5274                Next_Discriminant (Discrim);
5275             end loop;
5276
5277             --  Check whether the constraints of the full view statically
5278             --  match those imposed by the parent subtype [7.3(13)].
5279
5280             if Present (Stored_Constraint (Derived_Type)) then
5281                declare
5282                   C1, C2 : Elmt_Id;
5283
5284                begin
5285                   C1 := First_Elmt (Discs);
5286                   C2 := First_Elmt (Stored_Constraint (Derived_Type));
5287                   while Present (C1) and then Present (C2) loop
5288                      if not
5289                        Fully_Conformant_Expressions (Node (C1), Node (C2))
5290                      then
5291                         Error_Msg_N (
5292                           "not conformant with previous declaration",
5293                              Node (C1));
5294                      end if;
5295
5296                      Next_Elmt (C1);
5297                      Next_Elmt (C2);
5298                   end loop;
5299                end;
5300             end if;
5301          end if;
5302
5303       --  STEP 2b: No new discriminants, inherit discriminants if any
5304
5305       else
5306          if Private_Extension then
5307             Set_Has_Unknown_Discriminants
5308               (Derived_Type,
5309                Has_Unknown_Discriminants (Parent_Type)
5310                  or else Unknown_Discriminants_Present (N));
5311
5312          --  The partial view of the parent may have unknown discriminants,
5313          --  but if the full view has discriminants and the parent type is
5314          --  in scope they must be inherited.
5315
5316          elsif Has_Unknown_Discriminants (Parent_Type)
5317            and then
5318             (not Has_Discriminants (Parent_Type)
5319               or else not In_Open_Scopes (Scope (Parent_Type)))
5320          then
5321             Set_Has_Unknown_Discriminants (Derived_Type);
5322          end if;
5323
5324          if not Has_Unknown_Discriminants (Derived_Type)
5325            and then Has_Discriminants (Parent_Type)
5326          then
5327             Inherit_Discrims := True;
5328             Set_Has_Discriminants
5329               (Derived_Type, True);
5330             Set_Discriminant_Constraint
5331               (Derived_Type, Discriminant_Constraint (Parent_Base));
5332          end if;
5333
5334          --  The following test is true for private types (remember
5335          --  transformation 5. is not applied to those) and in an error
5336          --  situation.
5337
5338          if Constraint_Present then
5339             Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
5340          end if;
5341
5342          --  For now mark a new derived type as constrained only if it has no
5343          --  discriminants. At the end of Build_Derived_Record_Type we properly
5344          --  set this flag in the case of private extensions. See comments in
5345          --  point 9. just before body of Build_Derived_Record_Type.
5346
5347          Set_Is_Constrained
5348            (Derived_Type,
5349             not (Inherit_Discrims
5350                  or else Has_Unknown_Discriminants (Derived_Type)));
5351       end if;
5352
5353       --  STEP 3: initialize fields of derived type.
5354
5355       Set_Is_Tagged_Type    (Derived_Type, Is_Tagged);
5356       Set_Stored_Constraint (Derived_Type, No_Elist);
5357
5358       --  Fields inherited from the Parent_Type
5359
5360       Set_Discard_Names
5361         (Derived_Type, Einfo.Discard_Names      (Parent_Type));
5362       Set_Has_Specified_Layout
5363         (Derived_Type, Has_Specified_Layout     (Parent_Type));
5364       Set_Is_Limited_Composite
5365         (Derived_Type, Is_Limited_Composite     (Parent_Type));
5366       Set_Is_Limited_Record
5367         (Derived_Type, Is_Limited_Record        (Parent_Type));
5368       Set_Is_Private_Composite
5369         (Derived_Type, Is_Private_Composite     (Parent_Type));
5370
5371       --  Fields inherited from the Parent_Base
5372
5373       Set_Has_Controlled_Component
5374         (Derived_Type, Has_Controlled_Component (Parent_Base));
5375       Set_Has_Non_Standard_Rep
5376         (Derived_Type, Has_Non_Standard_Rep     (Parent_Base));
5377       Set_Has_Primitive_Operations
5378         (Derived_Type, Has_Primitive_Operations (Parent_Base));
5379
5380       --  Direct controlled types do not inherit Finalize_Storage_Only flag
5381
5382       if not Is_Controlled  (Parent_Type) then
5383          Set_Finalize_Storage_Only
5384            (Derived_Type, Finalize_Storage_Only (Parent_Type));
5385       end if;
5386
5387       --  Set fields for private derived types.
5388
5389       if Is_Private_Type (Derived_Type) then
5390          Set_Depends_On_Private (Derived_Type, True);
5391          Set_Private_Dependents (Derived_Type, New_Elmt_List);
5392
5393       --  Inherit fields from non private record types. If this is the
5394       --  completion of a derivation from a private type, the parent itself
5395       --  is private, and the attributes come from its full view, which must
5396       --  be present.
5397
5398       else
5399          if Is_Private_Type (Parent_Base)
5400            and then not Is_Record_Type (Parent_Base)
5401          then
5402             Set_Component_Alignment
5403               (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
5404             Set_C_Pass_By_Copy
5405               (Derived_Type, C_Pass_By_Copy      (Full_View (Parent_Base)));
5406          else
5407             Set_Component_Alignment
5408               (Derived_Type, Component_Alignment (Parent_Base));
5409
5410             Set_C_Pass_By_Copy
5411               (Derived_Type, C_Pass_By_Copy      (Parent_Base));
5412          end if;
5413       end if;
5414
5415       --  Set fields for tagged types
5416
5417       if Is_Tagged then
5418          Set_Primitive_Operations (Derived_Type, New_Elmt_List);
5419
5420          --  All tagged types defined in Ada.Finalization are controlled
5421
5422          if Chars (Scope (Derived_Type)) = Name_Finalization
5423            and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
5424            and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
5425          then
5426             Set_Is_Controlled (Derived_Type);
5427          else
5428             Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
5429          end if;
5430
5431          Make_Class_Wide_Type (Derived_Type);
5432          Set_Is_Abstract      (Derived_Type, Abstract_Present (Type_Def));
5433
5434          if Has_Discriminants (Derived_Type)
5435            and then Constraint_Present
5436          then
5437             Set_Stored_Constraint
5438               (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
5439          end if;
5440
5441       else
5442          Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
5443          Set_Has_Non_Standard_Rep
5444                        (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
5445       end if;
5446
5447       --  STEP 4: Inherit components from the parent base and constrain them.
5448       --          Apply the second transformation described in point 6. above.
5449
5450       if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
5451         or else not Has_Discriminants (Parent_Type)
5452         or else not Is_Constrained (Parent_Type)
5453       then
5454          Constrs := Discs;
5455       else
5456          Constrs := Discriminant_Constraint (Parent_Type);
5457       end if;
5458
5459       Assoc_List := Inherit_Components (N,
5460         Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
5461
5462       --  STEP 5a: Copy the parent record declaration for untagged types
5463
5464       if not Is_Tagged then
5465
5466          --  Discriminant_Constraint (Derived_Type) has been properly
5467          --  constructed. Save it and temporarily set it to Empty because we do
5468          --  not want the call to New_Copy_Tree below to mess this list.
5469
5470          if Has_Discriminants (Derived_Type) then
5471             Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
5472             Set_Discriminant_Constraint (Derived_Type, No_Elist);
5473          else
5474             Save_Discr_Constr := No_Elist;
5475          end if;
5476
5477          --  Save the Etype field of Derived_Type. It is correctly set now, but
5478          --  the call to New_Copy tree may remap it to point to itself, which
5479          --  is not what we want. Ditto for the Next_Entity field.
5480
5481          Save_Etype       := Etype (Derived_Type);
5482          Save_Next_Entity := Next_Entity (Derived_Type);
5483
5484          --  Assoc_List maps all stored discriminants in the Parent_Base to
5485          --  stored discriminants in the Derived_Type. It is fundamental that
5486          --  no types or itypes with discriminants other than the stored
5487          --  discriminants appear in the entities declared inside
5488          --  Derived_Type. Gigi won't like it.
5489
5490          New_Decl :=
5491            New_Copy_Tree
5492              (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
5493
5494          --  Restore the fields saved prior to the New_Copy_Tree call
5495          --  and compute the stored constraint.
5496
5497          Set_Etype       (Derived_Type, Save_Etype);
5498          Set_Next_Entity (Derived_Type, Save_Next_Entity);
5499
5500          if Has_Discriminants (Derived_Type) then
5501             Set_Discriminant_Constraint
5502               (Derived_Type, Save_Discr_Constr);
5503             Set_Stored_Constraint
5504               (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
5505             Replace_Components (Derived_Type, New_Decl);
5506          end if;
5507
5508          --  Insert the new derived type declaration
5509
5510          Rewrite (N, New_Decl);
5511
5512       --  STEP 5b: Complete the processing for record extensions in generics
5513
5514       --  There is no completion for record extensions declared in the
5515       --  parameter part of a generic, so we need to complete processing for
5516       --  these generic record extensions here. The Record_Type_Definition call
5517       --  will change the Ekind of the components from E_Void to E_Component.
5518
5519       elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
5520          Record_Type_Definition (Empty, Derived_Type);
5521
5522       --  STEP 5c: Process the record extension for non private tagged types.
5523
5524       elsif not Private_Extension then
5525          --  Add the _parent field in the derived type.
5526
5527          Expand_Derived_Record (Derived_Type, Type_Def);
5528
5529          --  Analyze the record extension
5530
5531          Record_Type_Definition
5532            (Record_Extension_Part (Type_Def), Derived_Type);
5533       end if;
5534
5535       End_Scope;
5536
5537       if Etype (Derived_Type) = Any_Type then
5538          return;
5539       end if;
5540
5541       --  Set delayed freeze and then derive subprograms, we need to do
5542       --  this in this order so that derived subprograms inherit the
5543       --  derived freeze if necessary.
5544
5545       Set_Has_Delayed_Freeze (Derived_Type);
5546       if Derive_Subps then
5547          Derive_Subprograms (Parent_Type, Derived_Type);
5548       end if;
5549
5550       --  If we have a private extension which defines a constrained derived
5551       --  type mark as constrained here after we have derived subprograms. See
5552       --  comment on point 9. just above the body of Build_Derived_Record_Type.
5553
5554       if Private_Extension and then Inherit_Discrims then
5555          if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
5556             Set_Is_Constrained          (Derived_Type, True);
5557             Set_Discriminant_Constraint (Derived_Type, Discs);
5558
5559          elsif Is_Constrained (Parent_Type) then
5560             Set_Is_Constrained
5561               (Derived_Type, True);
5562             Set_Discriminant_Constraint
5563               (Derived_Type, Discriminant_Constraint (Parent_Type));
5564          end if;
5565       end if;
5566
5567    end Build_Derived_Record_Type;
5568
5569    ------------------------
5570    -- Build_Derived_Type --
5571    ------------------------
5572
5573    procedure Build_Derived_Type
5574      (N             : Node_Id;
5575       Parent_Type   : Entity_Id;
5576       Derived_Type  : Entity_Id;
5577       Is_Completion : Boolean;
5578       Derive_Subps  : Boolean := True)
5579    is
5580       Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5581
5582    begin
5583       --  Set common attributes
5584
5585       Set_Scope          (Derived_Type, Current_Scope);
5586
5587       Set_Ekind          (Derived_Type, Ekind     (Parent_Base));
5588       Set_Etype          (Derived_Type,            Parent_Base);
5589       Set_Has_Task       (Derived_Type, Has_Task  (Parent_Base));
5590
5591       Set_Size_Info      (Derived_Type,                 Parent_Type);
5592       Set_RM_Size        (Derived_Type, RM_Size        (Parent_Type));
5593       Set_Convention     (Derived_Type, Convention     (Parent_Type));
5594       Set_Is_Controlled  (Derived_Type, Is_Controlled  (Parent_Type));
5595
5596       --  The derived type inherits the representation clauses of the parent.
5597       --  However, for a private type that is completed by a derivation, there
5598       --  may be operation attributes that have been specified already (stream
5599       --  attributes and External_Tag) and those must be provided. Finally,
5600       --  if the partial view is a private extension, the representation items
5601       --  of the parent have been inherited already, and should not be chained
5602       --  twice to the derived type.
5603
5604       if Is_Tagged_Type (Parent_Type)
5605         and then Present (First_Rep_Item (Derived_Type))
5606       then
5607          --  The existing items are either operational items or items inherited
5608          --  from a private extension declaration.
5609
5610          declare
5611             Rep   : Node_Id := First_Rep_Item (Derived_Type);
5612             Found : Boolean := False;
5613
5614          begin
5615             while Present (Rep) loop
5616                if Rep = First_Rep_Item (Parent_Type) then
5617                   Found := True;
5618                   exit;
5619                else
5620                   Rep := Next_Rep_Item (Rep);
5621                end if;
5622             end loop;
5623
5624             if not Found then
5625                Set_Next_Rep_Item
5626                  (First_Rep_Item (Derived_Type), First_Rep_Item (Parent_Type));
5627             end if;
5628          end;
5629
5630       else
5631          Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
5632       end if;
5633
5634       case Ekind (Parent_Type) is
5635          when Numeric_Kind =>
5636             Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
5637
5638          when Array_Kind =>
5639             Build_Derived_Array_Type (N, Parent_Type,  Derived_Type);
5640
5641          when E_Record_Type
5642             | E_Record_Subtype
5643             | Class_Wide_Kind  =>
5644             Build_Derived_Record_Type
5645               (N, Parent_Type, Derived_Type, Derive_Subps);
5646             return;
5647
5648          when Enumeration_Kind =>
5649             Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
5650
5651          when Access_Kind =>
5652             Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
5653
5654          when Incomplete_Or_Private_Kind =>
5655             Build_Derived_Private_Type
5656               (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
5657
5658             --  For discriminated types, the derivation includes deriving
5659             --  primitive operations. For others it is done below.
5660
5661             if Is_Tagged_Type (Parent_Type)
5662               or else Has_Discriminants (Parent_Type)
5663               or else (Present (Full_View (Parent_Type))
5664                         and then Has_Discriminants (Full_View (Parent_Type)))
5665             then
5666                return;
5667             end if;
5668
5669          when Concurrent_Kind =>
5670             Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
5671
5672          when others =>
5673             raise Program_Error;
5674       end case;
5675
5676       if Etype (Derived_Type) = Any_Type then
5677          return;
5678       end if;
5679
5680       --  Set delayed freeze and then derive subprograms, we need to do
5681       --  this in this order so that derived subprograms inherit the
5682       --  derived freeze if necessary.
5683
5684       Set_Has_Delayed_Freeze (Derived_Type);
5685       if Derive_Subps then
5686          Derive_Subprograms (Parent_Type, Derived_Type);
5687       end if;
5688
5689       Set_Has_Primitive_Operations
5690         (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
5691    end Build_Derived_Type;
5692
5693    -----------------------
5694    -- Build_Discriminal --
5695    -----------------------
5696
5697    procedure Build_Discriminal (Discrim : Entity_Id) is
5698       D_Minal : Entity_Id;
5699       CR_Disc : Entity_Id;
5700
5701    begin
5702       --  A discriminal has the same names as the discriminant.
5703
5704       D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
5705
5706       Set_Ekind     (D_Minal, E_In_Parameter);
5707       Set_Mechanism (D_Minal, Default_Mechanism);
5708       Set_Etype     (D_Minal, Etype (Discrim));
5709
5710       Set_Discriminal (Discrim, D_Minal);
5711       Set_Discriminal_Link (D_Minal, Discrim);
5712
5713       --  For task types, build at once the discriminants of the corresponding
5714       --  record, which are needed if discriminants are used in entry defaults
5715       --  and in family bounds.
5716
5717       if Is_Concurrent_Type (Current_Scope)
5718         or else Is_Limited_Type (Current_Scope)
5719       then
5720          CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
5721
5722          Set_Ekind     (CR_Disc, E_In_Parameter);
5723          Set_Mechanism (CR_Disc, Default_Mechanism);
5724          Set_Etype     (CR_Disc, Etype (Discrim));
5725          Set_CR_Discriminant (Discrim, CR_Disc);
5726       end if;
5727    end Build_Discriminal;
5728
5729    ------------------------------------
5730    -- Build_Discriminant_Constraints --
5731    ------------------------------------
5732
5733    function Build_Discriminant_Constraints
5734      (T           : Entity_Id;
5735       Def         : Node_Id;
5736       Derived_Def : Boolean := False) return Elist_Id
5737    is
5738       C          : constant Node_Id := Constraint (Def);
5739       Nb_Discr   : constant Nat     := Number_Discriminants (T);
5740       Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
5741       --  Saves the expression corresponding to a given discriminant in T.
5742
5743       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
5744       --  Return the Position number within array Discr_Expr of a discriminant
5745       --  D within the discriminant list of the discriminated type T.
5746
5747       ------------------
5748       -- Pos_Of_Discr --
5749       ------------------
5750
5751       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
5752          Disc : Entity_Id;
5753
5754       begin
5755          Disc := First_Discriminant (T);
5756          for J in Discr_Expr'Range loop
5757             if Disc = D then
5758                return J;
5759             end if;
5760
5761             Next_Discriminant (Disc);
5762          end loop;
5763
5764          --  Note: Since this function is called on discriminants that are
5765          --  known to belong to the discriminated type, falling through the
5766          --  loop with no match signals an internal compiler error.
5767
5768          raise Program_Error;
5769       end Pos_Of_Discr;
5770
5771       --  Declarations local to Build_Discriminant_Constraints
5772
5773       Discr : Entity_Id;
5774       E     : Entity_Id;
5775       Elist : constant Elist_Id := New_Elmt_List;
5776
5777       Constr    : Node_Id;
5778       Expr      : Node_Id;
5779       Id        : Node_Id;
5780       Position  : Nat;
5781       Found     : Boolean;
5782
5783       Discrim_Present : Boolean := False;
5784
5785    --  Start of processing for Build_Discriminant_Constraints
5786
5787    begin
5788       --  The following loop will process positional associations only.
5789       --  For a positional association, the (single) discriminant is
5790       --  implicitly specified by position, in textual order (RM 3.7.2).
5791
5792       Discr  := First_Discriminant (T);
5793       Constr := First (Constraints (C));
5794
5795       for D in Discr_Expr'Range loop
5796          exit when Nkind (Constr) = N_Discriminant_Association;
5797
5798          if No (Constr) then
5799             Error_Msg_N ("too few discriminants given in constraint", C);
5800             return New_Elmt_List;
5801
5802          elsif Nkind (Constr) = N_Range
5803            or else (Nkind (Constr) = N_Attribute_Reference
5804                      and then
5805                     Attribute_Name (Constr) = Name_Range)
5806          then
5807             Error_Msg_N
5808               ("a range is not a valid discriminant constraint", Constr);
5809             Discr_Expr (D) := Error;
5810
5811          else
5812             Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
5813             Discr_Expr (D) := Constr;
5814          end if;
5815
5816          Next_Discriminant (Discr);
5817          Next (Constr);
5818       end loop;
5819
5820       if No (Discr) and then Present (Constr) then
5821          Error_Msg_N ("too many discriminants given in constraint", Constr);
5822          return New_Elmt_List;
5823       end if;
5824
5825       --  Named associations can be given in any order, but if both positional
5826       --  and named associations are used in the same discriminant constraint,
5827       --  then positional associations must occur first, at their normal
5828       --  position. Hence once a named association is used, the rest of the
5829       --  discriminant constraint must use only named associations.
5830
5831       while Present (Constr) loop
5832
5833          --  Positional association forbidden after a named association.
5834
5835          if Nkind (Constr) /= N_Discriminant_Association then
5836             Error_Msg_N ("positional association follows named one", Constr);
5837             return New_Elmt_List;
5838
5839          --  Otherwise it is a named association
5840
5841          else
5842             --  E records the type of the discriminants in the named
5843             --  association. All the discriminants specified in the same name
5844             --  association must have the same type.
5845
5846             E := Empty;
5847
5848             --  Search the list of discriminants in T to see if the simple name
5849             --  given in the constraint matches any of them.
5850
5851             Id := First (Selector_Names (Constr));
5852             while Present (Id) loop
5853                Found := False;
5854
5855                --  If Original_Discriminant is present, we are processing a
5856                --  generic instantiation and this is an instance node. We need
5857                --  to find the name of the corresponding discriminant in the
5858                --  actual record type T and not the name of the discriminant in
5859                --  the generic formal. Example:
5860                --
5861                --    generic
5862                --       type G (D : int) is private;
5863                --    package P is
5864                --       subtype W is G (D => 1);
5865                --    end package;
5866                --    type Rec (X : int) is record ... end record;
5867                --    package Q is new P (G => Rec);
5868                --
5869                --  At the point of the instantiation, formal type G is Rec
5870                --  and therefore when reanalyzing "subtype W is G (D => 1);"
5871                --  which really looks like "subtype W is Rec (D => 1);" at
5872                --  the point of instantiation, we want to find the discriminant
5873                --  that corresponds to D in Rec, ie X.
5874
5875                if Present (Original_Discriminant (Id)) then
5876                   Discr := Find_Corresponding_Discriminant (Id, T);
5877                   Found := True;
5878
5879                else
5880                   Discr := First_Discriminant (T);
5881                   while Present (Discr) loop
5882                      if Chars (Discr) = Chars (Id) then
5883                         Found := True;
5884                         exit;
5885                      end if;
5886
5887                      Next_Discriminant (Discr);
5888                   end loop;
5889
5890                   if not Found then
5891                      Error_Msg_N ("& does not match any discriminant", Id);
5892                      return New_Elmt_List;
5893
5894                   --  The following is only useful for the benefit of generic
5895                   --  instances but it does not interfere with other
5896                   --  processing for the non-generic case so we do it in all
5897                   --  cases (for generics this statement is executed when
5898                   --  processing the generic definition, see comment at the
5899                   --  beginning of this if statement).
5900
5901                   else
5902                      Set_Original_Discriminant (Id, Discr);
5903                   end if;
5904                end if;
5905
5906                Position := Pos_Of_Discr (T, Discr);
5907
5908                if Present (Discr_Expr (Position)) then
5909                   Error_Msg_N ("duplicate constraint for discriminant&", Id);
5910
5911                else
5912                   --  Each discriminant specified in the same named association
5913                   --  must be associated with a separate copy of the
5914                   --  corresponding expression.
5915
5916                   if Present (Next (Id)) then
5917                      Expr := New_Copy_Tree (Expression (Constr));
5918                      Set_Parent (Expr, Parent (Expression (Constr)));
5919                   else
5920                      Expr := Expression (Constr);
5921                   end if;
5922
5923                   Discr_Expr (Position) := Expr;
5924                   Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
5925                end if;
5926
5927                --  A discriminant association with more than one discriminant
5928                --  name is only allowed if the named discriminants are all of
5929                --  the same type (RM 3.7.1(8)).
5930
5931                if E = Empty then
5932                   E := Base_Type (Etype (Discr));
5933
5934                elsif Base_Type (Etype (Discr)) /= E then
5935                   Error_Msg_N
5936                     ("all discriminants in an association " &
5937                      "must have the same type", Id);
5938                end if;
5939
5940                Next (Id);
5941             end loop;
5942          end if;
5943
5944          Next (Constr);
5945       end loop;
5946
5947       --  A discriminant constraint must provide exactly one value for each
5948       --  discriminant of the type (RM 3.7.1(8)).
5949
5950       for J in Discr_Expr'Range loop
5951          if No (Discr_Expr (J)) then
5952             Error_Msg_N ("too few discriminants given in constraint", C);
5953             return New_Elmt_List;
5954          end if;
5955       end loop;
5956
5957       --  Determine if there are discriminant expressions in the constraint.
5958
5959       for J in Discr_Expr'Range loop
5960          if Denotes_Discriminant (Discr_Expr (J), Check_Protected => True) then
5961             Discrim_Present := True;
5962          end if;
5963       end loop;
5964
5965       --  Build an element list consisting of the expressions given in the
5966       --  discriminant constraint and apply the appropriate checks. The list
5967       --  is constructed after resolving any named discriminant associations
5968       --  and therefore the expressions appear in the textual order of the
5969       --  discriminants.
5970
5971       Discr := First_Discriminant (T);
5972       for J in Discr_Expr'Range loop
5973          if Discr_Expr (J) /= Error then
5974
5975             Append_Elmt (Discr_Expr (J), Elist);
5976
5977             --  If any of the discriminant constraints is given by a
5978             --  discriminant and we are in a derived type declaration we
5979             --  have a discriminant renaming. Establish link between new
5980             --  and old discriminant.
5981
5982             if Denotes_Discriminant (Discr_Expr (J)) then
5983                if Derived_Def then
5984                   Set_Corresponding_Discriminant
5985                     (Entity (Discr_Expr (J)), Discr);
5986                end if;
5987
5988             --  Force the evaluation of non-discriminant expressions.
5989             --  If we have found a discriminant in the constraint 3.4(26)
5990             --  and 3.8(18) demand that no range checks are performed are
5991             --  after evaluation. If the constraint is for a component
5992             --  definition that has a per-object constraint, expressions are
5993             --  evaluated but not checked either. In all other cases perform
5994             --  a range check.
5995
5996             else
5997                if Discrim_Present then
5998                   null;
5999
6000                elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
6001                  and then
6002                    Has_Per_Object_Constraint
6003                      (Defining_Identifier (Parent (Parent (Def))))
6004                then
6005                   null;
6006
6007                elsif Is_Access_Type (Etype (Discr)) then
6008                   Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
6009
6010                else
6011                   Apply_Range_Check (Discr_Expr (J), Etype (Discr));
6012                end if;
6013
6014                Force_Evaluation (Discr_Expr (J));
6015             end if;
6016
6017          --  Check that the designated type of an access discriminant's
6018          --  expression is not a class-wide type unless the discriminant's
6019          --  designated type is also class-wide.
6020
6021             if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
6022               and then not Is_Class_Wide_Type
6023                          (Designated_Type (Etype (Discr)))
6024               and then Etype (Discr_Expr (J)) /= Any_Type
6025               and then Is_Class_Wide_Type
6026                          (Designated_Type (Etype (Discr_Expr (J))))
6027             then
6028                Wrong_Type (Discr_Expr (J), Etype (Discr));
6029             end if;
6030          end if;
6031
6032          Next_Discriminant (Discr);
6033       end loop;
6034
6035       return Elist;
6036    end Build_Discriminant_Constraints;
6037
6038    ---------------------------------
6039    -- Build_Discriminated_Subtype --
6040    ---------------------------------
6041
6042    procedure Build_Discriminated_Subtype
6043      (T           : Entity_Id;
6044       Def_Id      : Entity_Id;
6045       Elist       : Elist_Id;
6046       Related_Nod : Node_Id;
6047       For_Access  : Boolean := False)
6048    is
6049       Has_Discrs  : constant Boolean := Has_Discriminants (T);
6050       Constrained : constant Boolean
6051                       := (Has_Discrs
6052                             and then not Is_Empty_Elmt_List (Elist)
6053                             and then not Is_Class_Wide_Type (T))
6054                            or else Is_Constrained (T);
6055
6056    begin
6057       if Ekind (T) = E_Record_Type then
6058          if For_Access then
6059             Set_Ekind (Def_Id, E_Private_Subtype);
6060             Set_Is_For_Access_Subtype (Def_Id, True);
6061          else
6062             Set_Ekind (Def_Id, E_Record_Subtype);
6063          end if;
6064
6065       elsif Ekind (T) = E_Task_Type then
6066          Set_Ekind (Def_Id, E_Task_Subtype);
6067
6068       elsif Ekind (T) = E_Protected_Type then
6069          Set_Ekind (Def_Id, E_Protected_Subtype);
6070
6071       elsif Is_Private_Type (T) then
6072          Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
6073
6074       elsif Is_Class_Wide_Type (T) then
6075          Set_Ekind (Def_Id, E_Class_Wide_Subtype);
6076
6077       else
6078          --  Incomplete type.  attach subtype to list of dependents, to be
6079          --  completed with full view of parent type,  unless is it the
6080          --  designated subtype of a record component within an init_proc.
6081          --  This last case arises for a component of an access type whose
6082          --  designated type is incomplete (e.g. a Taft Amendment type).
6083          --  The designated subtype is within an inner scope, and needs no
6084          --  elaboration, because only the access type is needed in the
6085          --  initialization procedure.
6086
6087          Set_Ekind (Def_Id, Ekind (T));
6088
6089          if For_Access and then Within_Init_Proc then
6090             null;
6091          else
6092             Append_Elmt (Def_Id, Private_Dependents (T));
6093          end if;
6094       end if;
6095
6096       Set_Etype             (Def_Id, T);
6097       Init_Size_Align       (Def_Id);
6098       Set_Has_Discriminants (Def_Id, Has_Discrs);
6099       Set_Is_Constrained    (Def_Id, Constrained);
6100
6101       Set_First_Entity      (Def_Id, First_Entity   (T));
6102       Set_Last_Entity       (Def_Id, Last_Entity    (T));
6103       Set_First_Rep_Item    (Def_Id, First_Rep_Item (T));
6104
6105       if Is_Tagged_Type (T) then
6106          Set_Is_Tagged_Type  (Def_Id);
6107          Make_Class_Wide_Type (Def_Id);
6108       end if;
6109
6110       Set_Stored_Constraint (Def_Id, No_Elist);
6111
6112       if Has_Discrs then
6113          Set_Discriminant_Constraint (Def_Id, Elist);
6114          Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
6115       end if;
6116
6117       if Is_Tagged_Type (T) then
6118          Set_Primitive_Operations (Def_Id, Primitive_Operations (T));
6119          Set_Is_Abstract (Def_Id, Is_Abstract (T));
6120       end if;
6121
6122       --  Subtypes introduced by component declarations do not need to be
6123       --  marked as delayed, and do not get freeze nodes, because the semantics
6124       --  verifies that the parents of the subtypes are frozen before the
6125       --  enclosing record is frozen.
6126
6127       if not Is_Type (Scope (Def_Id)) then
6128          Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
6129
6130          if Is_Private_Type (T)
6131            and then Present (Full_View (T))
6132          then
6133             Conditional_Delay (Def_Id, Full_View (T));
6134          else
6135             Conditional_Delay (Def_Id, T);
6136          end if;
6137       end if;
6138
6139       if Is_Record_Type (T) then
6140          Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
6141
6142          if Has_Discrs
6143             and then not Is_Empty_Elmt_List (Elist)
6144             and then not For_Access
6145          then
6146             Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
6147          elsif not For_Access then
6148             Set_Cloned_Subtype (Def_Id, T);
6149          end if;
6150       end if;
6151
6152    end Build_Discriminated_Subtype;
6153
6154    ------------------------
6155    -- Build_Scalar_Bound --
6156    ------------------------
6157
6158    function Build_Scalar_Bound
6159      (Bound : Node_Id;
6160       Par_T : Entity_Id;
6161       Der_T : Entity_Id) return Node_Id
6162    is
6163       New_Bound : Entity_Id;
6164
6165    begin
6166       --  Note: not clear why this is needed, how can the original bound
6167       --  be unanalyzed at this point? and if it is, what business do we
6168       --  have messing around with it? and why is the base type of the
6169       --  parent type the right type for the resolution. It probably is
6170       --  not! It is OK for the new bound we are creating, but not for
6171       --  the old one??? Still if it never happens, no problem!
6172
6173       Analyze_And_Resolve (Bound, Base_Type (Par_T));
6174
6175       if Nkind (Bound) = N_Integer_Literal
6176         or else Nkind (Bound) = N_Real_Literal
6177       then
6178          New_Bound := New_Copy (Bound);
6179          Set_Etype (New_Bound, Der_T);
6180          Set_Analyzed (New_Bound);
6181
6182       elsif Is_Entity_Name (Bound) then
6183          New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
6184
6185       --  The following is almost certainly wrong. What business do we have
6186       --  relocating a node (Bound) that is presumably still attached to
6187       --  the tree elsewhere???
6188
6189       else
6190          New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
6191       end if;
6192
6193       Set_Etype (New_Bound, Der_T);
6194       return New_Bound;
6195    end Build_Scalar_Bound;
6196
6197    --------------------------------
6198    -- Build_Underlying_Full_View --
6199    --------------------------------
6200
6201    procedure Build_Underlying_Full_View
6202      (N   : Node_Id;
6203       Typ : Entity_Id;
6204       Par : Entity_Id)
6205    is
6206       Loc  : constant Source_Ptr := Sloc (N);
6207       Subt : constant Entity_Id :=
6208                Make_Defining_Identifier
6209                  (Loc, New_External_Name (Chars (Typ), 'S'));
6210
6211       Constr : Node_Id;
6212       Indic  : Node_Id;
6213       C      : Node_Id;
6214       Id     : Node_Id;
6215
6216    begin
6217       if Nkind (N) = N_Full_Type_Declaration then
6218          Constr := Constraint (Subtype_Indication (Type_Definition (N)));
6219
6220       --  ??? ??? is this assert right, I assume so otherwise Constr
6221       --  would not be defined below (this used to be an elsif)
6222
6223       else pragma Assert (Nkind (N) = N_Subtype_Declaration);
6224          Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
6225       end if;
6226
6227       --  If the constraint has discriminant associations, the discriminant
6228       --  entity is already set, but it denotes a discriminant of the new
6229       --  type, not the original parent, so it must be found anew.
6230
6231       C := First (Constraints (Constr));
6232
6233       while Present (C) loop
6234
6235          if Nkind (C) = N_Discriminant_Association then
6236             Id := First (Selector_Names (C));
6237
6238             while Present (Id) loop
6239                Set_Original_Discriminant (Id, Empty);
6240                Next (Id);
6241             end loop;
6242          end if;
6243
6244          Next (C);
6245       end loop;
6246
6247       Indic := Make_Subtype_Declaration (Loc,
6248          Defining_Identifier => Subt,
6249          Subtype_Indication  =>
6250            Make_Subtype_Indication (Loc,
6251              Subtype_Mark => New_Reference_To (Par, Loc),
6252              Constraint   => New_Copy_Tree (Constr)));
6253
6254       --  If this is a component subtype for an outer itype, it is not
6255       --  a list member, so simply set the parent link for analysis: if
6256       --  the enclosing type does not need to be in a declarative list,
6257       --  neither do the components.
6258
6259       if Is_List_Member (N) then
6260          Insert_Before (N, Indic);
6261       else
6262          Set_Parent (Indic, Parent (N));
6263       end if;
6264
6265       Analyze (Indic);
6266       Set_Underlying_Full_View (Typ, Full_View (Subt));
6267    end Build_Underlying_Full_View;
6268
6269    -------------------------------
6270    -- Check_Abstract_Overriding --
6271    -------------------------------
6272
6273    procedure Check_Abstract_Overriding (T : Entity_Id) is
6274       Op_List  : Elist_Id;
6275       Elmt     : Elmt_Id;
6276       Subp     : Entity_Id;
6277       Type_Def : Node_Id;
6278
6279    begin
6280       Op_List := Primitive_Operations (T);
6281
6282       --  Loop to check primitive operations
6283
6284       Elmt := First_Elmt (Op_List);
6285       while Present (Elmt) loop
6286          Subp := Node (Elmt);
6287
6288          --  Special exception, do not complain about failure to
6289          --  override _Input and _Output, since we always provide
6290          --  automatic overridings for these subprograms.
6291
6292          if Is_Abstract (Subp)
6293            and then not Is_TSS (Subp, TSS_Stream_Input)
6294            and then not Is_TSS (Subp, TSS_Stream_Output)
6295            and then not Is_Abstract (T)
6296          then
6297             if Present (Alias (Subp)) then
6298                --  Only perform the check for a derived subprogram when
6299                --  the type has an explicit record extension. This avoids
6300                --  incorrectly flagging abstract subprograms for the case
6301                --  of a type without an extension derived from a formal type
6302                --  with a tagged actual (can occur within a private part).
6303
6304                Type_Def := Type_Definition (Parent (T));
6305                if Nkind (Type_Def) = N_Derived_Type_Definition
6306                  and then Present (Record_Extension_Part (Type_Def))
6307                then
6308                   Error_Msg_NE
6309                     ("type must be declared abstract or & overridden",
6310                      T, Subp);
6311                end if;
6312             else
6313                Error_Msg_NE
6314                  ("abstract subprogram not allowed for type&",
6315                   Subp, T);
6316                Error_Msg_NE
6317                  ("nonabstract type has abstract subprogram&",
6318                   T, Subp);
6319             end if;
6320          end if;
6321
6322          Next_Elmt (Elmt);
6323       end loop;
6324    end Check_Abstract_Overriding;
6325
6326    ------------------------------------------------
6327    -- Check_Access_Discriminant_Requires_Limited --
6328    ------------------------------------------------
6329
6330    procedure Check_Access_Discriminant_Requires_Limited
6331      (D   : Node_Id;
6332       Loc : Node_Id)
6333    is
6334    begin
6335       --  A discriminant_specification for an access discriminant
6336       --  shall appear only in the declaration for a task or protected
6337       --  type, or for a type with the reserved word 'limited' in
6338       --  its definition or in one of its ancestors. (RM 3.7(10))
6339
6340       if Nkind (Discriminant_Type (D)) = N_Access_Definition
6341         and then not Is_Concurrent_Type (Current_Scope)
6342         and then not Is_Concurrent_Record_Type (Current_Scope)
6343         and then not Is_Limited_Record (Current_Scope)
6344         and then Ekind (Current_Scope) /= E_Limited_Private_Type
6345       then
6346          Error_Msg_N
6347            ("access discriminants allowed only for limited types", Loc);
6348       end if;
6349    end Check_Access_Discriminant_Requires_Limited;
6350
6351    -----------------------------------
6352    -- Check_Aliased_Component_Types --
6353    -----------------------------------
6354
6355    procedure Check_Aliased_Component_Types (T : Entity_Id) is
6356       C : Entity_Id;
6357
6358    begin
6359       --  ??? Also need to check components of record extensions,
6360       --  but not components of protected types (which are always
6361       --  limited).
6362
6363       if not Is_Limited_Type (T) then
6364          if Ekind (T) = E_Record_Type then
6365             C := First_Component (T);
6366             while Present (C) loop
6367                if Is_Aliased (C)
6368                  and then Has_Discriminants (Etype (C))
6369                  and then not Is_Constrained (Etype (C))
6370                  and then not In_Instance
6371                then
6372                   Error_Msg_N
6373                     ("aliased component must be constrained ('R'M 3.6(11))",
6374                       C);
6375                end if;
6376
6377                Next_Component (C);
6378             end loop;
6379
6380          elsif Ekind (T) = E_Array_Type then
6381             if Has_Aliased_Components (T)
6382               and then Has_Discriminants (Component_Type (T))
6383               and then not Is_Constrained (Component_Type (T))
6384               and then not In_Instance
6385             then
6386                Error_Msg_N
6387                  ("aliased component type must be constrained ('R'M 3.6(11))",
6388                     T);
6389             end if;
6390          end if;
6391       end if;
6392    end Check_Aliased_Component_Types;
6393
6394    ----------------------
6395    -- Check_Completion --
6396    ----------------------
6397
6398    procedure Check_Completion (Body_Id : Node_Id := Empty) is
6399       E : Entity_Id;
6400
6401       procedure Post_Error;
6402       --  Post error message for lack of completion for entity E
6403
6404       ----------------
6405       -- Post_Error --
6406       ----------------
6407
6408       procedure Post_Error is
6409       begin
6410          if not Comes_From_Source (E) then
6411
6412             if Ekind (E) = E_Task_Type
6413               or else Ekind (E) = E_Protected_Type
6414             then
6415                --  It may be an anonymous protected type created for a
6416                --  single variable. Post error on variable, if present.
6417
6418                declare
6419                   Var : Entity_Id;
6420
6421                begin
6422                   Var := First_Entity (Current_Scope);
6423
6424                   while Present (Var) loop
6425                      exit when Etype (Var) = E
6426                        and then Comes_From_Source (Var);
6427
6428                      Next_Entity (Var);
6429                   end loop;
6430
6431                   if Present (Var) then
6432                      E := Var;
6433                   end if;
6434                end;
6435             end if;
6436          end if;
6437
6438          --  If a generated entity has no completion, then either previous
6439          --  semantic errors have disabled the expansion phase, or else
6440          --  we had missing subunits, or else we are compiling without expan-
6441          --  sion, or else something is very wrong.
6442
6443          if not Comes_From_Source (E) then
6444             pragma Assert
6445               (Serious_Errors_Detected > 0
6446                 or else Configurable_Run_Time_Violations > 0
6447                 or else Subunits_Missing
6448                 or else not Expander_Active);
6449             return;
6450
6451          --  Here for source entity
6452
6453          else
6454             --  Here if no body to post the error message, so we post the error
6455             --  on the declaration that has no completion. This is not really
6456             --  the right place to post it, think about this later ???
6457
6458             if No (Body_Id) then
6459                if Is_Type (E) then
6460                   Error_Msg_NE
6461                     ("missing full declaration for }", Parent (E), E);
6462                else
6463                   Error_Msg_NE
6464                     ("missing body for &", Parent (E), E);
6465                end if;
6466
6467             --  Package body has no completion for a declaration that appears
6468             --  in the corresponding spec. Post error on the body, with a
6469             --  reference to the non-completed declaration.
6470
6471             else
6472                Error_Msg_Sloc := Sloc (E);
6473
6474                if Is_Type (E) then
6475                   Error_Msg_NE
6476                     ("missing full declaration for }!", Body_Id, E);
6477
6478                elsif Is_Overloadable (E)
6479                  and then Current_Entity_In_Scope (E) /= E
6480                then
6481                   --  It may be that the completion is mistyped and appears
6482                   --  as a  distinct overloading of the entity.
6483
6484                   declare
6485                      Candidate : constant Entity_Id :=
6486                                    Current_Entity_In_Scope (E);
6487                      Decl      : constant Node_Id :=
6488                                    Unit_Declaration_Node (Candidate);
6489
6490                   begin
6491                      if Is_Overloadable (Candidate)
6492                        and then Ekind (Candidate) = Ekind (E)
6493                        and then Nkind (Decl) = N_Subprogram_Body
6494                        and then Acts_As_Spec (Decl)
6495                      then
6496                         Check_Type_Conformant (Candidate, E);
6497
6498                      else
6499                         Error_Msg_NE ("missing body for & declared#!",
6500                            Body_Id, E);
6501                      end if;
6502                   end;
6503                else
6504                   Error_Msg_NE ("missing body for & declared#!",
6505                      Body_Id, E);
6506                end if;
6507             end if;
6508          end if;
6509       end Post_Error;
6510
6511    --  Start processing for Check_Completion
6512
6513    begin
6514       E := First_Entity (Current_Scope);
6515       while Present (E) loop
6516          if Is_Intrinsic_Subprogram (E) then
6517             null;
6518
6519          --  The following situation requires special handling: a child
6520          --  unit that appears in the context clause of the body of its
6521          --  parent:
6522
6523          --    procedure Parent.Child (...);
6524          --
6525          --    with Parent.Child;
6526          --    package body Parent is
6527
6528          --  Here Parent.Child appears as a local entity, but should not
6529          --  be flagged as requiring completion, because it is a
6530          --  compilation unit.
6531
6532          elsif     Ekind (E) = E_Function
6533            or else Ekind (E) = E_Procedure
6534            or else Ekind (E) = E_Generic_Function
6535            or else Ekind (E) = E_Generic_Procedure
6536          then
6537             if not Has_Completion (E)
6538               and then not Is_Abstract (E)
6539               and then Nkind (Parent (Unit_Declaration_Node (E))) /=
6540                                                        N_Compilation_Unit
6541               and then Chars (E) /= Name_uSize
6542             then
6543                Post_Error;
6544             end if;
6545
6546          elsif Is_Entry (E) then
6547             if not Has_Completion (E) and then
6548               (Ekind (Scope (E)) = E_Protected_Object
6549                 or else Ekind (Scope (E)) = E_Protected_Type)
6550             then
6551                Post_Error;
6552             end if;
6553
6554          elsif Is_Package (E) then
6555             if Unit_Requires_Body (E) then
6556                if not Has_Completion (E)
6557                  and then Nkind (Parent (Unit_Declaration_Node (E))) /=
6558                                                        N_Compilation_Unit
6559                then
6560                   Post_Error;
6561                end if;
6562
6563             elsif not Is_Child_Unit (E) then
6564                May_Need_Implicit_Body (E);
6565             end if;
6566
6567          elsif Ekind (E) = E_Incomplete_Type
6568            and then No (Underlying_Type (E))
6569          then
6570             Post_Error;
6571
6572          elsif (Ekind (E) = E_Task_Type or else
6573                 Ekind (E) = E_Protected_Type)
6574            and then not Has_Completion (E)
6575          then
6576             Post_Error;
6577
6578          --  A single task declared in the current scope is
6579          --  a constant, verify that the body of its anonymous
6580          --  type is in the same scope. If the task is defined
6581          --  elsewhere, this may be a renaming declaration for
6582          --  which no completion is needed.
6583
6584          elsif Ekind (E) = E_Constant
6585            and then Ekind (Etype (E)) = E_Task_Type
6586            and then not Has_Completion (Etype (E))
6587            and then Scope (Etype (E)) = Current_Scope
6588          then
6589             Post_Error;
6590
6591          elsif Ekind (E) = E_Protected_Object
6592            and then not Has_Completion (Etype (E))
6593          then
6594             Post_Error;
6595
6596          elsif Ekind (E) = E_Record_Type then
6597             if Is_Tagged_Type (E) then
6598                Check_Abstract_Overriding (E);
6599             end if;
6600
6601             Check_Aliased_Component_Types (E);
6602
6603          elsif Ekind (E) = E_Array_Type then
6604             Check_Aliased_Component_Types (E);
6605
6606          end if;
6607
6608          Next_Entity (E);
6609       end loop;
6610    end Check_Completion;
6611
6612    ----------------------------
6613    -- Check_Delta_Expression --
6614    ----------------------------
6615
6616    procedure Check_Delta_Expression (E : Node_Id) is
6617    begin
6618       if not (Is_Real_Type (Etype (E))) then
6619          Wrong_Type (E, Any_Real);
6620
6621       elsif not Is_OK_Static_Expression (E) then
6622          Flag_Non_Static_Expr
6623            ("non-static expression used for delta value!", E);
6624
6625       elsif not UR_Is_Positive (Expr_Value_R (E)) then
6626          Error_Msg_N ("delta expression must be positive", E);
6627
6628       else
6629          return;
6630       end if;
6631
6632       --  If any of above errors occurred, then replace the incorrect
6633       --  expression by the real 0.1, which should prevent further errors.
6634
6635       Rewrite (E,
6636         Make_Real_Literal (Sloc (E), Ureal_Tenth));
6637       Analyze_And_Resolve (E, Standard_Float);
6638
6639    end Check_Delta_Expression;
6640
6641    -----------------------------
6642    -- Check_Digits_Expression --
6643    -----------------------------
6644
6645    procedure Check_Digits_Expression (E : Node_Id) is
6646    begin
6647       if not (Is_Integer_Type (Etype (E))) then
6648          Wrong_Type (E, Any_Integer);
6649
6650       elsif not Is_OK_Static_Expression (E) then
6651          Flag_Non_Static_Expr
6652            ("non-static expression used for digits value!", E);
6653
6654       elsif Expr_Value (E) <= 0 then
6655          Error_Msg_N ("digits value must be greater than zero", E);
6656
6657       else
6658          return;
6659       end if;
6660
6661       --  If any of above errors occurred, then replace the incorrect
6662       --  expression by the integer 1, which should prevent further errors.
6663
6664       Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
6665       Analyze_And_Resolve (E, Standard_Integer);
6666
6667    end Check_Digits_Expression;
6668
6669    --------------------------
6670    -- Check_Initialization --
6671    --------------------------
6672
6673    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
6674    begin
6675       if (Is_Limited_Type (T)
6676            or else Is_Limited_Composite (T))
6677         and then not In_Instance
6678         and then not In_Inlined_Body
6679       then
6680          --  Ada 2005 (AI-287): Relax the strictness of the front-end in
6681          --  case of limited aggregates and extension aggregates.
6682
6683          if Ada_Version >= Ada_05
6684            and then (Nkind (Exp) = N_Aggregate
6685                       or else Nkind (Exp) = N_Extension_Aggregate)
6686          then
6687             null;
6688          else
6689             Error_Msg_N
6690               ("cannot initialize entities of limited type", Exp);
6691             Explain_Limited_Type (T, Exp);
6692          end if;
6693       end if;
6694    end Check_Initialization;
6695
6696    ------------------------------------
6697    -- Check_Or_Process_Discriminants --
6698    ------------------------------------
6699
6700    --  If an incomplete or private type declaration was already given for
6701    --  the type, the discriminants may have already been processed if they
6702    --  were present on the incomplete declaration. In this case a full
6703    --  conformance check is performed otherwise just process them.
6704
6705    procedure Check_Or_Process_Discriminants
6706      (N    : Node_Id;
6707       T    : Entity_Id;
6708       Prev : Entity_Id := Empty)
6709    is
6710    begin
6711       if Has_Discriminants (T) then
6712
6713          --  Make the discriminants visible to component declarations.
6714
6715          declare
6716             D    : Entity_Id := First_Discriminant (T);
6717             Prev : Entity_Id;
6718
6719          begin
6720             while Present (D) loop
6721                Prev := Current_Entity (D);
6722                Set_Current_Entity (D);
6723                Set_Is_Immediately_Visible (D);
6724                Set_Homonym (D, Prev);
6725
6726                --  Ada 2005 (AI-230): Access discriminant allowed in
6727                --  non-limited record types.
6728
6729                if Ada_Version < Ada_05 then
6730
6731                   --  This restriction gets applied to the full type here; it
6732                   --  has already been applied earlier to the partial view
6733
6734                   Check_Access_Discriminant_Requires_Limited (Parent (D), N);
6735                end if;
6736
6737                Next_Discriminant (D);
6738             end loop;
6739          end;
6740
6741       elsif Present (Discriminant_Specifications (N)) then
6742          Process_Discriminants (N, Prev);
6743       end if;
6744    end Check_Or_Process_Discriminants;
6745
6746    ----------------------
6747    -- Check_Real_Bound --
6748    ----------------------
6749
6750    procedure Check_Real_Bound (Bound : Node_Id) is
6751    begin
6752       if not Is_Real_Type (Etype (Bound)) then
6753          Error_Msg_N
6754            ("bound in real type definition must be of real type", Bound);
6755
6756       elsif not Is_OK_Static_Expression (Bound) then
6757          Flag_Non_Static_Expr
6758            ("non-static expression used for real type bound!", Bound);
6759
6760       else
6761          return;
6762       end if;
6763
6764       Rewrite
6765         (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
6766       Analyze (Bound);
6767       Resolve (Bound, Standard_Float);
6768    end Check_Real_Bound;
6769
6770    ------------------------------
6771    -- Complete_Private_Subtype --
6772    ------------------------------
6773
6774    procedure Complete_Private_Subtype
6775      (Priv        : Entity_Id;
6776       Full        : Entity_Id;
6777       Full_Base   : Entity_Id;
6778       Related_Nod : Node_Id)
6779    is
6780       Save_Next_Entity : Entity_Id;
6781       Save_Homonym     : Entity_Id;
6782
6783    begin
6784       --  Set semantic attributes for (implicit) private subtype completion.
6785       --  If the full type has no discriminants, then it is a copy of the full
6786       --  view of the base. Otherwise, it is a subtype of the base with a
6787       --  possible discriminant constraint. Save and restore the original
6788       --  Next_Entity field of full to ensure that the calls to Copy_Node
6789       --  do not corrupt the entity chain.
6790
6791       --  Note that the type of the full view is the same entity as the
6792       --  type of the partial view. In this fashion, the subtype has
6793       --  access to the correct view of the parent.
6794
6795       Save_Next_Entity := Next_Entity (Full);
6796       Save_Homonym     := Homonym (Priv);
6797
6798       case Ekind (Full_Base) is
6799
6800          when E_Record_Type    |
6801               E_Record_Subtype |
6802               Class_Wide_Kind  |
6803               Private_Kind     |
6804               Task_Kind        |
6805               Protected_Kind   =>
6806             Copy_Node (Priv, Full);
6807
6808             Set_Has_Discriminants  (Full, Has_Discriminants (Full_Base));
6809             Set_First_Entity       (Full, First_Entity (Full_Base));
6810             Set_Last_Entity        (Full, Last_Entity (Full_Base));
6811
6812          when others =>
6813             Copy_Node (Full_Base, Full);
6814             Set_Chars          (Full, Chars (Priv));
6815             Conditional_Delay  (Full, Priv);
6816             Set_Sloc           (Full, Sloc (Priv));
6817
6818       end case;
6819
6820       Set_Next_Entity (Full, Save_Next_Entity);
6821       Set_Homonym     (Full, Save_Homonym);
6822       Set_Associated_Node_For_Itype (Full, Related_Nod);
6823
6824       --  Set common attributes for all subtypes.
6825
6826       Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
6827
6828       --  The Etype of the full view is inconsistent. Gigi needs to see the
6829       --  structural full view,  which is what the current scheme gives:
6830       --  the Etype of the full view is the etype of the full base. However,
6831       --  if the full base is a derived type, the full view then looks like
6832       --  a subtype of the parent, not a subtype of the full base. If instead
6833       --  we write:
6834
6835       --       Set_Etype (Full, Full_Base);
6836
6837       --  then we get inconsistencies in the front-end (confusion between
6838       --  views). Several outstanding bugs are related to this.
6839
6840       Set_Is_First_Subtype (Full, False);
6841       Set_Scope            (Full, Scope (Priv));
6842       Set_Size_Info        (Full, Full_Base);
6843       Set_RM_Size          (Full, RM_Size (Full_Base));
6844       Set_Is_Itype         (Full);
6845
6846       --  A subtype of a private-type-without-discriminants, whose full-view
6847       --  has discriminants with default expressions, is not constrained!
6848
6849       if not Has_Discriminants (Priv) then
6850          Set_Is_Constrained (Full, Is_Constrained (Full_Base));
6851
6852          if Has_Discriminants (Full_Base) then
6853             Set_Discriminant_Constraint
6854               (Full, Discriminant_Constraint (Full_Base));
6855
6856             --  The partial view may have been indefinite, the full view
6857             --  might not be.
6858
6859             Set_Has_Unknown_Discriminants
6860               (Full, Has_Unknown_Discriminants (Full_Base));
6861          end if;
6862       end if;
6863
6864       Set_First_Rep_Item     (Full, First_Rep_Item (Full_Base));
6865       Set_Depends_On_Private (Full, Has_Private_Component (Full));
6866
6867       --  Freeze the private subtype entity if its parent is delayed,
6868       --  and not already frozen. We skip this processing if the type
6869       --  is an anonymous subtype of a record component, or is the
6870       --  corresponding record of a protected type, since ???
6871
6872       if not Is_Type (Scope (Full)) then
6873          Set_Has_Delayed_Freeze (Full,
6874            Has_Delayed_Freeze (Full_Base)
6875                and then (not Is_Frozen (Full_Base)));
6876       end if;
6877
6878       Set_Freeze_Node (Full, Empty);
6879       Set_Is_Frozen (Full, False);
6880       Set_Full_View (Priv, Full);
6881
6882       if Has_Discriminants (Full) then
6883          Set_Stored_Constraint_From_Discriminant_Constraint (Full);
6884          Set_Stored_Constraint (Priv, Stored_Constraint (Full));
6885          if Has_Unknown_Discriminants (Full) then
6886             Set_Discriminant_Constraint (Full, No_Elist);
6887          end if;
6888       end if;
6889
6890       if Ekind (Full_Base) = E_Record_Type
6891         and then Has_Discriminants (Full_Base)
6892         and then Has_Discriminants (Priv) -- might not, if errors
6893         and then not Has_Unknown_Discriminants (Priv)
6894         and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
6895       then
6896          Create_Constrained_Components
6897            (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
6898
6899       --  If the full base is itself derived from private, build a congruent
6900       --  subtype of its underlying type, for use by the back end. Do not
6901       --  do this for a constrained record component, where the back-end has
6902       --  the proper information and there is no place for the declaration.
6903
6904       elsif Ekind (Full_Base) in Private_Kind
6905         and then Is_Derived_Type (Full_Base)
6906         and then Has_Discriminants (Full_Base)
6907         and then Nkind (Related_Nod) /= N_Component_Declaration
6908         and then (Ekind (Current_Scope) /= E_Record_Subtype)
6909         and then
6910           Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
6911       then
6912          Build_Underlying_Full_View (Parent (Priv), Full, Etype (Full_Base));
6913
6914       elsif Is_Record_Type (Full_Base) then
6915
6916          --  Show Full is simply a renaming of Full_Base.
6917
6918          Set_Cloned_Subtype (Full, Full_Base);
6919       end if;
6920
6921       --  It is unsafe to share to bounds of a scalar type, because the
6922       --  Itype is elaborated on demand, and if a bound is non-static
6923       --  then different orders of elaboration in different units will
6924       --  lead to different external symbols.
6925
6926       if Is_Scalar_Type (Full_Base) then
6927          Set_Scalar_Range (Full,
6928            Make_Range (Sloc (Related_Nod),
6929              Low_Bound  =>
6930                Duplicate_Subexpr_No_Checks (Type_Low_Bound  (Full_Base)),
6931              High_Bound =>
6932                Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
6933
6934          --  This completion inherits the bounds of the full parent, but if
6935          --  the parent is an unconstrained floating point type, so is the
6936          --  completion.
6937
6938          if Is_Floating_Point_Type (Full_Base) then
6939             Set_Includes_Infinities
6940              (Scalar_Range (Full), Has_Infinities (Full_Base));
6941          end if;
6942       end if;
6943
6944       --  ??? It seems that a lot of fields are missing that should be
6945       --  copied from  Full_Base to Full. Here are some that are introduced
6946       --  in a non-disruptive way but a cleanup is necessary.
6947
6948       if Is_Tagged_Type (Full_Base) then
6949          Set_Is_Tagged_Type (Full);
6950          Set_Primitive_Operations (Full, Primitive_Operations (Full_Base));
6951          Set_Class_Wide_Type      (Full, Class_Wide_Type (Full_Base));
6952
6953       elsif Is_Concurrent_Type (Full_Base) then
6954          if Has_Discriminants (Full)
6955            and then Present (Corresponding_Record_Type (Full_Base))
6956          then
6957             Set_Corresponding_Record_Type (Full,
6958               Constrain_Corresponding_Record
6959                 (Full, Corresponding_Record_Type (Full_Base),
6960                   Related_Nod, Full_Base));
6961
6962          else
6963             Set_Corresponding_Record_Type (Full,
6964               Corresponding_Record_Type (Full_Base));
6965          end if;
6966       end if;
6967
6968    end Complete_Private_Subtype;
6969
6970    ----------------------------
6971    -- Constant_Redeclaration --
6972    ----------------------------
6973
6974    procedure Constant_Redeclaration
6975      (Id : Entity_Id;
6976       N  : Node_Id;
6977       T  : out Entity_Id)
6978    is
6979       Prev    : constant Entity_Id := Current_Entity_In_Scope (Id);
6980       Obj_Def : constant Node_Id := Object_Definition (N);
6981       New_T   : Entity_Id;
6982
6983       procedure Check_Recursive_Declaration (Typ : Entity_Id);
6984       --  If deferred constant is an access type initialized with an
6985       --  allocator, check whether there is an illegal recursion in the
6986       --  definition, through a default value of some record subcomponent.
6987       --  This is normally detected when generating init procs, but requires
6988       --  this additional mechanism when expansion is disabled.
6989
6990       ---------------------------------
6991       -- Check_Recursive_Declaration --
6992       ---------------------------------
6993
6994       procedure Check_Recursive_Declaration (Typ : Entity_Id) is
6995          Comp : Entity_Id;
6996
6997       begin
6998          if Is_Record_Type (Typ) then
6999             Comp := First_Component (Typ);
7000
7001             while Present (Comp) loop
7002                if Comes_From_Source (Comp) then
7003                   if Present (Expression (Parent (Comp)))
7004                     and then Is_Entity_Name (Expression (Parent (Comp)))
7005                     and then Entity (Expression (Parent (Comp))) = Prev
7006                   then
7007                      Error_Msg_Sloc := Sloc (Parent (Comp));
7008                      Error_Msg_NE
7009                        ("illegal circularity with declaration for&#",
7010                          N, Comp);
7011                      return;
7012
7013                   elsif Is_Record_Type (Etype (Comp)) then
7014                      Check_Recursive_Declaration (Etype (Comp));
7015                   end if;
7016                end if;
7017
7018                Next_Component (Comp);
7019             end loop;
7020          end if;
7021       end Check_Recursive_Declaration;
7022
7023    --  Start of processing for Constant_Redeclaration
7024
7025    begin
7026       if Nkind (Parent (Prev)) = N_Object_Declaration then
7027          if Nkind (Object_Definition
7028                      (Parent (Prev))) = N_Subtype_Indication
7029          then
7030             --  Find type of new declaration. The constraints of the two
7031             --  views must match statically, but there is no point in
7032             --  creating an itype for the full view.
7033
7034             if Nkind (Obj_Def) = N_Subtype_Indication then
7035                Find_Type (Subtype_Mark (Obj_Def));
7036                New_T := Entity (Subtype_Mark (Obj_Def));
7037
7038             else
7039                Find_Type (Obj_Def);
7040                New_T := Entity (Obj_Def);
7041             end if;
7042
7043             T := Etype (Prev);
7044
7045          else
7046             --  The full view may impose a constraint, even if the partial
7047             --  view does not, so construct the subtype.
7048
7049             New_T := Find_Type_Of_Object (Obj_Def, N);
7050             T     := New_T;
7051          end if;
7052
7053       else
7054          --  Current declaration is illegal, diagnosed below in Enter_Name.
7055
7056          T := Empty;
7057          New_T := Any_Type;
7058       end if;
7059
7060       --  If previous full declaration exists, or if a homograph is present,
7061       --  let Enter_Name handle it, either with an error, or with the removal
7062       --  of an overridden implicit subprogram.
7063
7064       if Ekind (Prev) /= E_Constant
7065         or else Present (Expression (Parent (Prev)))
7066         or else Present (Full_View (Prev))
7067       then
7068          Enter_Name (Id);
7069
7070       --  Verify that types of both declarations match.
7071
7072       elsif Base_Type (Etype (Prev)) /= Base_Type (New_T) then
7073          Error_Msg_Sloc := Sloc (Prev);
7074          Error_Msg_N ("type does not match declaration#", N);
7075          Set_Full_View (Prev, Id);
7076          Set_Etype (Id, Any_Type);
7077
7078       --  If so, process the full constant declaration
7079
7080       else
7081          Set_Full_View (Prev, Id);
7082          Set_Is_Public (Id, Is_Public (Prev));
7083          Set_Is_Internal (Id);
7084          Append_Entity (Id, Current_Scope);
7085
7086          --  Check ALIASED present if present before (RM 7.4(7))
7087
7088          if Is_Aliased (Prev)
7089            and then not Aliased_Present (N)
7090          then
7091             Error_Msg_Sloc := Sloc (Prev);
7092             Error_Msg_N ("ALIASED required (see declaration#)", N);
7093          end if;
7094
7095          --  Check that placement is in private part and that the incomplete
7096          --  declaration appeared in the visible part.
7097
7098          if Ekind (Current_Scope) = E_Package
7099            and then not In_Private_Part (Current_Scope)
7100          then
7101             Error_Msg_Sloc := Sloc (Prev);
7102             Error_Msg_N ("full constant for declaration#"
7103                          & " must be in private part", N);
7104
7105          elsif Ekind (Current_Scope) = E_Package
7106            and then List_Containing (Parent (Prev))
7107            /= Visible_Declarations
7108              (Specification (Unit_Declaration_Node (Current_Scope)))
7109          then
7110             Error_Msg_N
7111               ("deferred constant must be declared in visible part",
7112                  Parent (Prev));
7113          end if;
7114
7115          if Is_Access_Type (T)
7116            and then Nkind (Expression (N)) = N_Allocator
7117          then
7118             Check_Recursive_Declaration (Designated_Type (T));
7119          end if;
7120       end if;
7121    end Constant_Redeclaration;
7122
7123    ----------------------
7124    -- Constrain_Access --
7125    ----------------------
7126
7127    procedure Constrain_Access
7128      (Def_Id      : in out Entity_Id;
7129       S           : Node_Id;
7130       Related_Nod : Node_Id)
7131    is
7132       T             : constant Entity_Id := Entity (Subtype_Mark (S));
7133       Desig_Type    : constant Entity_Id := Designated_Type (T);
7134       Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
7135       Constraint_OK : Boolean := True;
7136
7137    begin
7138       if Is_Array_Type (Desig_Type) then
7139          Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
7140
7141       elsif (Is_Record_Type (Desig_Type)
7142               or else Is_Incomplete_Or_Private_Type (Desig_Type))
7143         and then not Is_Constrained (Desig_Type)
7144       then
7145          --  ??? The following code is a temporary kludge to ignore
7146          --  discriminant constraint on access type if
7147          --  it is constraining the current record. Avoid creating the
7148          --  implicit subtype of the record we are currently compiling
7149          --  since right now, we cannot handle these.
7150          --  For now, just return the access type itself.
7151
7152          if Desig_Type = Current_Scope
7153            and then No (Def_Id)
7154          then
7155             Set_Ekind (Desig_Subtype, E_Record_Subtype);
7156             Def_Id := Entity (Subtype_Mark (S));
7157
7158             --  This call added to ensure that the constraint is
7159             --  analyzed (needed for a B test). Note that we
7160             --  still return early from this procedure to avoid
7161             --  recursive processing. ???
7162
7163             Constrain_Discriminated_Type
7164               (Desig_Subtype, S, Related_Nod, For_Access => True);
7165
7166             return;
7167          end if;
7168
7169          if Ekind (T) = E_General_Access_Type
7170            and then Has_Private_Declaration (Desig_Type)
7171            and then In_Open_Scopes (Scope (Desig_Type))
7172          then
7173             --  Enforce rule that the constraint is illegal if there is
7174             --  an unconstrained view of the designated type. This means
7175             --  that the partial view (either a private type declaration or
7176             --  a derivation from a private type) has no discriminants.
7177             --  (Defect Report 8652/0008, Technical Corrigendum 1, checked
7178             --  by ACATS B371001).
7179
7180             declare
7181                Pack  : constant Node_Id :=
7182                          Unit_Declaration_Node (Scope (Desig_Type));
7183                Decls : List_Id;
7184                Decl  : Node_Id;
7185
7186             begin
7187                if Nkind (Pack) = N_Package_Declaration then
7188                   Decls := Visible_Declarations (Specification (Pack));
7189                   Decl := First (Decls);
7190
7191                   while Present (Decl) loop
7192                      if (Nkind (Decl) = N_Private_Type_Declaration
7193                           and then
7194                             Chars (Defining_Identifier (Decl)) =
7195                                                      Chars (Desig_Type))
7196
7197                        or else
7198                         (Nkind (Decl) = N_Full_Type_Declaration
7199                           and then
7200                             Chars (Defining_Identifier (Decl)) =
7201                                                      Chars (Desig_Type)
7202                           and then Is_Derived_Type (Desig_Type)
7203                           and then
7204                             Has_Private_Declaration (Etype (Desig_Type)))
7205                      then
7206                         if No (Discriminant_Specifications (Decl)) then
7207                            Error_Msg_N
7208                             ("cannot constrain general access type " &
7209                                "if designated type has unconstrained view", S);
7210                         end if;
7211
7212                         exit;
7213                      end if;
7214
7215                      Next (Decl);
7216                   end loop;
7217                end if;
7218             end;
7219          end if;
7220
7221          Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
7222            For_Access => True);
7223
7224       elsif (Is_Task_Type (Desig_Type)
7225               or else Is_Protected_Type (Desig_Type))
7226         and then not Is_Constrained (Desig_Type)
7227       then
7228          Constrain_Concurrent
7229            (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
7230
7231       else
7232          Error_Msg_N ("invalid constraint on access type", S);
7233          Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
7234          Constraint_OK := False;
7235       end if;
7236
7237       if No (Def_Id) then
7238          Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
7239       else
7240          Set_Ekind (Def_Id, E_Access_Subtype);
7241       end if;
7242
7243       if Constraint_OK then
7244          Set_Etype (Def_Id, Base_Type (T));
7245
7246          if Is_Private_Type (Desig_Type) then
7247             Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
7248          end if;
7249       else
7250          Set_Etype (Def_Id, Any_Type);
7251       end if;
7252
7253       Set_Size_Info                (Def_Id, T);
7254       Set_Is_Constrained           (Def_Id, Constraint_OK);
7255       Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
7256       Set_Depends_On_Private       (Def_Id, Has_Private_Component (Def_Id));
7257       Set_Is_Access_Constant       (Def_Id, Is_Access_Constant (T));
7258
7259       --  Itypes created for constrained record components do not receive
7260       --  a freeze node, they are elaborated when first seen.
7261
7262       if not Is_Record_Type (Current_Scope) then
7263          Conditional_Delay (Def_Id, T);
7264       end if;
7265    end Constrain_Access;
7266
7267    ---------------------
7268    -- Constrain_Array --
7269    ---------------------
7270
7271    procedure Constrain_Array
7272      (Def_Id      : in out Entity_Id;
7273       SI          : Node_Id;
7274       Related_Nod : Node_Id;
7275       Related_Id  : Entity_Id;
7276       Suffix      : Character)
7277    is
7278       C                     : constant Node_Id := Constraint (SI);
7279       Number_Of_Constraints : Nat := 0;
7280       Index                 : Node_Id;
7281       S, T                  : Entity_Id;
7282       Constraint_OK         : Boolean := True;
7283
7284    begin
7285       T := Entity (Subtype_Mark (SI));
7286
7287       if Ekind (T) in Access_Kind then
7288          T := Designated_Type (T);
7289       end if;
7290
7291       --  If an index constraint follows a subtype mark in a subtype indication
7292       --  then the type or subtype denoted by the subtype mark must not already
7293       --  impose an index constraint. The subtype mark must denote either an
7294       --  unconstrained array type or an access type whose designated type
7295       --  is such an array type... (RM 3.6.1)
7296
7297       if Is_Constrained (T) then
7298          Error_Msg_N
7299            ("array type is already constrained", Subtype_Mark (SI));
7300          Constraint_OK := False;
7301
7302       else
7303          S := First (Constraints (C));
7304
7305          while Present (S) loop
7306             Number_Of_Constraints := Number_Of_Constraints + 1;
7307             Next (S);
7308          end loop;
7309
7310          --  In either case, the index constraint must provide a discrete
7311          --  range for each index of the array type and the type of each
7312          --  discrete range must be the same as that of the corresponding
7313          --  index. (RM 3.6.1)
7314
7315          if Number_Of_Constraints /= Number_Dimensions (T) then
7316             Error_Msg_NE ("incorrect number of index constraints for }", C, T);
7317             Constraint_OK := False;
7318
7319          else
7320             S := First (Constraints (C));
7321             Index := First_Index (T);
7322             Analyze (Index);
7323
7324             --  Apply constraints to each index type
7325
7326             for J in 1 .. Number_Of_Constraints loop
7327                Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
7328                Next (Index);
7329                Next (S);
7330             end loop;
7331
7332          end if;
7333       end if;
7334
7335       if No (Def_Id) then
7336          Def_Id :=
7337            Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
7338          Set_Parent (Def_Id, Related_Nod);
7339
7340       else
7341          Set_Ekind (Def_Id, E_Array_Subtype);
7342       end if;
7343
7344       Set_Size_Info      (Def_Id,                (T));
7345       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
7346       Set_Etype          (Def_Id, Base_Type      (T));
7347
7348       if Constraint_OK then
7349          Set_First_Index (Def_Id, First (Constraints (C)));
7350       end if;
7351
7352       Set_Is_Constrained     (Def_Id, True);
7353       Set_Is_Aliased         (Def_Id, Is_Aliased (T));
7354       Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
7355
7356       Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
7357       Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
7358
7359       --  If the subtype is not that of a record component, build a freeze
7360       --  node if parent still needs one.
7361
7362       --  If the subtype is not that of a record component, make sure
7363       --  that the Depends_On_Private status is set (explanation ???)
7364       --  and also that a conditional delay is set.
7365
7366       if not Is_Type (Scope (Def_Id)) then
7367          Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
7368          Conditional_Delay (Def_Id, T);
7369       end if;
7370
7371    end Constrain_Array;
7372
7373    ------------------------------
7374    -- Constrain_Component_Type --
7375    ------------------------------
7376
7377    function Constrain_Component_Type
7378      (Compon_Type     : Entity_Id;
7379       Constrained_Typ : Entity_Id;
7380       Related_Node    : Node_Id;
7381       Typ             : Entity_Id;
7382       Constraints     : Elist_Id) return Entity_Id
7383    is
7384       Loc : constant Source_Ptr := Sloc (Constrained_Typ);
7385
7386       function Build_Constrained_Array_Type
7387         (Old_Type : Entity_Id) return Entity_Id;
7388       --  If Old_Type is an array type, one of whose indices is
7389       --  constrained by a discriminant, build an Itype whose constraint
7390       --  replaces the discriminant with its value in the constraint.
7391
7392       function Build_Constrained_Discriminated_Type
7393         (Old_Type : Entity_Id) return Entity_Id;
7394       --  Ditto for record components.
7395
7396       function Build_Constrained_Access_Type
7397         (Old_Type : Entity_Id) return Entity_Id;
7398       --  Ditto for access types. Makes use of previous two functions, to
7399       --  constrain designated type.
7400
7401       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
7402       --  T is an array or discriminated type, C is a list of constraints
7403       --  that apply to T. This routine builds the constrained subtype.
7404
7405       function Is_Discriminant (Expr : Node_Id) return Boolean;
7406       --  Returns True if Expr is a discriminant.
7407
7408       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
7409       --  Find the value of discriminant Discrim in Constraint.
7410
7411       -----------------------------------
7412       -- Build_Constrained_Access_Type --
7413       -----------------------------------
7414
7415       function Build_Constrained_Access_Type
7416         (Old_Type : Entity_Id) return Entity_Id
7417       is
7418          Desig_Type    : constant Entity_Id := Designated_Type (Old_Type);
7419          Itype         : Entity_Id;
7420          Desig_Subtype : Entity_Id;
7421          Scop          : Entity_Id;
7422
7423       begin
7424          --  if the original access type was not embedded in the enclosing
7425          --  type definition, there is no need to produce a new access
7426          --  subtype. In fact every access type with an explicit constraint
7427          --  generates an itype whose scope is the enclosing record.
7428
7429          if not Is_Type (Scope (Old_Type)) then
7430             return Old_Type;
7431
7432          elsif Is_Array_Type (Desig_Type) then
7433             Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
7434
7435          elsif Has_Discriminants (Desig_Type) then
7436
7437             --  This may be an access type to an enclosing record type for
7438             --  which we are constructing the constrained components. Return
7439             --  the enclosing record subtype. This is not always correct,
7440             --  but avoids infinite recursion. ???
7441
7442             Desig_Subtype := Any_Type;
7443
7444             for J in reverse 0 .. Scope_Stack.Last loop
7445                Scop := Scope_Stack.Table (J).Entity;
7446
7447                if Is_Type (Scop)
7448                  and then Base_Type (Scop) = Base_Type (Desig_Type)
7449                then
7450                   Desig_Subtype := Scop;
7451                end if;
7452
7453                exit when not Is_Type (Scop);
7454             end loop;
7455
7456             if Desig_Subtype = Any_Type then
7457                Desig_Subtype :=
7458                  Build_Constrained_Discriminated_Type (Desig_Type);
7459             end if;
7460
7461          else
7462             return Old_Type;
7463          end if;
7464
7465          if Desig_Subtype /= Desig_Type then
7466             --  The Related_Node better be here or else we won't be able
7467             --  to attach new itypes to a node in the tree.
7468
7469             pragma Assert (Present (Related_Node));
7470
7471             Itype := Create_Itype (E_Access_Subtype, Related_Node);
7472
7473             Set_Etype                    (Itype, Base_Type      (Old_Type));
7474             Set_Size_Info                (Itype,                (Old_Type));
7475             Set_Directly_Designated_Type (Itype, Desig_Subtype);
7476             Set_Depends_On_Private       (Itype, Has_Private_Component
7477                                                                 (Old_Type));
7478             Set_Is_Access_Constant       (Itype, Is_Access_Constant
7479                                                                 (Old_Type));
7480
7481             --  The new itype needs freezing when it depends on a not frozen
7482             --  type and the enclosing subtype needs freezing.
7483
7484             if Has_Delayed_Freeze (Constrained_Typ)
7485               and then not Is_Frozen (Constrained_Typ)
7486             then
7487                Conditional_Delay (Itype, Base_Type (Old_Type));
7488             end if;
7489
7490             return Itype;
7491
7492          else
7493             return Old_Type;
7494          end if;
7495       end Build_Constrained_Access_Type;
7496
7497       ----------------------------------
7498       -- Build_Constrained_Array_Type --
7499       ----------------------------------
7500
7501       function Build_Constrained_Array_Type
7502         (Old_Type : Entity_Id) return Entity_Id
7503       is
7504          Lo_Expr     : Node_Id;
7505          Hi_Expr     : Node_Id;
7506          Old_Index   : Node_Id;
7507          Range_Node  : Node_Id;
7508          Constr_List : List_Id;
7509
7510          Need_To_Create_Itype : Boolean := False;
7511
7512       begin
7513          Old_Index := First_Index (Old_Type);
7514          while Present (Old_Index) loop
7515             Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
7516
7517             if Is_Discriminant (Lo_Expr)
7518               or else Is_Discriminant (Hi_Expr)
7519             then
7520                Need_To_Create_Itype := True;
7521             end if;
7522
7523             Next_Index (Old_Index);
7524          end loop;
7525
7526          if Need_To_Create_Itype then
7527             Constr_List := New_List;
7528
7529             Old_Index := First_Index (Old_Type);
7530             while Present (Old_Index) loop
7531                Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
7532
7533                if Is_Discriminant (Lo_Expr) then
7534                   Lo_Expr := Get_Discr_Value (Lo_Expr);
7535                end if;
7536
7537                if Is_Discriminant (Hi_Expr) then
7538                   Hi_Expr := Get_Discr_Value (Hi_Expr);
7539                end if;
7540
7541                Range_Node :=
7542                  Make_Range
7543                    (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
7544
7545                Append (Range_Node, To => Constr_List);
7546
7547                Next_Index (Old_Index);
7548             end loop;
7549
7550             return Build_Subtype (Old_Type, Constr_List);
7551
7552          else
7553             return Old_Type;
7554          end if;
7555       end Build_Constrained_Array_Type;
7556
7557       ------------------------------------------
7558       -- Build_Constrained_Discriminated_Type --
7559       ------------------------------------------
7560
7561       function Build_Constrained_Discriminated_Type
7562         (Old_Type : Entity_Id) return Entity_Id
7563       is
7564          Expr           : Node_Id;
7565          Constr_List    : List_Id;
7566          Old_Constraint : Elmt_Id;
7567
7568          Need_To_Create_Itype : Boolean := False;
7569
7570       begin
7571          Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
7572          while Present (Old_Constraint) loop
7573             Expr := Node (Old_Constraint);
7574
7575             if Is_Discriminant (Expr) then
7576                Need_To_Create_Itype := True;
7577             end if;
7578
7579             Next_Elmt (Old_Constraint);
7580          end loop;
7581
7582          if Need_To_Create_Itype then
7583             Constr_List := New_List;
7584
7585             Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
7586             while Present (Old_Constraint) loop
7587                Expr := Node (Old_Constraint);
7588
7589                if Is_Discriminant (Expr) then
7590                   Expr := Get_Discr_Value (Expr);
7591                end if;
7592
7593                Append (New_Copy_Tree (Expr), To => Constr_List);
7594
7595                Next_Elmt (Old_Constraint);
7596             end loop;
7597
7598             return Build_Subtype (Old_Type, Constr_List);
7599
7600          else
7601             return Old_Type;
7602          end if;
7603       end Build_Constrained_Discriminated_Type;
7604
7605       -------------------
7606       -- Build_Subtype --
7607       -------------------
7608
7609       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
7610          Indic       : Node_Id;
7611          Subtyp_Decl : Node_Id;
7612          Def_Id      : Entity_Id;
7613          Btyp        : Entity_Id := Base_Type (T);
7614
7615       begin
7616          --  The Related_Node better be here or else we won't be able
7617          --  to attach new itypes to a node in the tree.
7618
7619          pragma Assert (Present (Related_Node));
7620
7621          --  If the view of the component's type is incomplete or private
7622          --  with unknown discriminants, then the constraint must be applied
7623          --  to the full type.
7624
7625          if Has_Unknown_Discriminants (Btyp)
7626            and then Present (Underlying_Type (Btyp))
7627          then
7628             Btyp := Underlying_Type (Btyp);
7629          end if;
7630
7631          Indic :=
7632            Make_Subtype_Indication (Loc,
7633              Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
7634              Constraint   => Make_Index_Or_Discriminant_Constraint (Loc, C));
7635
7636          Def_Id := Create_Itype (Ekind (T), Related_Node);
7637
7638          Subtyp_Decl :=
7639            Make_Subtype_Declaration (Loc,
7640              Defining_Identifier => Def_Id,
7641              Subtype_Indication  => Indic);
7642
7643          Set_Parent (Subtyp_Decl, Parent (Related_Node));
7644
7645          --  Itypes must be analyzed with checks off (see itypes.ads).
7646
7647          Analyze (Subtyp_Decl, Suppress => All_Checks);
7648
7649          return Def_Id;
7650       end Build_Subtype;
7651
7652       ---------------------
7653       -- Get_Discr_Value --
7654       ---------------------
7655
7656       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
7657          D : Entity_Id := First_Discriminant (Typ);
7658          E : Elmt_Id   := First_Elmt (Constraints);
7659          G : Elmt_Id;
7660
7661       begin
7662          --  The discriminant may be declared for the type, in which case we
7663          --  find it by iterating over the list of discriminants. If the
7664          --  discriminant is inherited from a parent type, it appears as the
7665          --  corresponding discriminant of the current type. This will be the
7666          --  case when constraining an inherited component whose constraint is
7667          --  given by a discriminant of the parent.
7668
7669          while Present (D) loop
7670             if D = Entity (Discrim)
7671               or else Corresponding_Discriminant (D) = Entity (Discrim)
7672             then
7673                return Node (E);
7674             end if;
7675
7676             Next_Discriminant (D);
7677             Next_Elmt (E);
7678          end loop;
7679
7680          --  The corresponding_Discriminant mechanism is incomplete, because
7681          --  the correspondence between new and old discriminants is not one
7682          --  to one: one new discriminant can constrain several old ones.
7683          --  In that case, scan sequentially the stored_constraint, the list
7684          --  of discriminants of the parents, and the constraints.
7685
7686          if Is_Derived_Type (Typ)
7687            and then Present (Stored_Constraint (Typ))
7688            and then Scope (Entity (Discrim)) = Etype (Typ)
7689          then
7690             D := First_Discriminant (Etype (Typ));
7691             E := First_Elmt (Constraints);
7692             G := First_Elmt (Stored_Constraint (Typ));
7693
7694             while Present (D) loop
7695                if D = Entity (Discrim) then
7696                   return Node (E);
7697                end if;
7698
7699                Next_Discriminant (D);
7700                Next_Elmt (E);
7701                Next_Elmt (G);
7702             end loop;
7703          end if;
7704
7705          --  Something is wrong if we did not find the value
7706
7707          raise Program_Error;
7708       end Get_Discr_Value;
7709
7710       ---------------------
7711       -- Is_Discriminant --
7712       ---------------------
7713
7714       function Is_Discriminant (Expr : Node_Id) return Boolean is
7715          Discrim_Scope : Entity_Id;
7716
7717       begin
7718          if Denotes_Discriminant (Expr) then
7719             Discrim_Scope := Scope (Entity (Expr));
7720
7721             --  Either we have a reference to one of Typ's discriminants,
7722
7723             pragma Assert (Discrim_Scope = Typ
7724
7725                --  or to the discriminants of the parent type, in the case
7726                --  of a derivation of a tagged type with variants.
7727
7728                or else Discrim_Scope = Etype (Typ)
7729                or else Full_View (Discrim_Scope) = Etype (Typ)
7730
7731                --  or same as above for the case where the discriminants
7732                --  were declared in Typ's private view.
7733
7734                or else (Is_Private_Type (Discrim_Scope)
7735                         and then Chars (Discrim_Scope) = Chars (Typ))
7736
7737                --  or else we are deriving from the full view and the
7738                --  discriminant is declared in the private entity.
7739
7740                or else (Is_Private_Type (Typ)
7741                         and then Chars (Discrim_Scope) = Chars (Typ))
7742
7743                --  or we have a class-wide type, in which case make sure the
7744                --  discriminant found belongs to the root type.
7745
7746                or else (Is_Class_Wide_Type (Typ)
7747                         and then Etype (Typ) = Discrim_Scope));
7748
7749             return True;
7750          end if;
7751
7752          --  In all other cases we have something wrong.
7753
7754          return False;
7755       end Is_Discriminant;
7756
7757    --  Start of processing for Constrain_Component_Type
7758
7759    begin
7760       if Is_Array_Type (Compon_Type) then
7761          return Build_Constrained_Array_Type (Compon_Type);
7762
7763       elsif Has_Discriminants (Compon_Type) then
7764          return Build_Constrained_Discriminated_Type (Compon_Type);
7765
7766       elsif Is_Access_Type (Compon_Type) then
7767          return Build_Constrained_Access_Type (Compon_Type);
7768       end if;
7769
7770       return Compon_Type;
7771    end Constrain_Component_Type;
7772
7773    --------------------------
7774    -- Constrain_Concurrent --
7775    --------------------------
7776
7777    --  For concurrent types, the associated record value type carries the same
7778    --  discriminants, so when we constrain a concurrent type, we must constrain
7779    --  the value type as well.
7780
7781    procedure Constrain_Concurrent
7782      (Def_Id      : in out Entity_Id;
7783       SI          : Node_Id;
7784       Related_Nod : Node_Id;
7785       Related_Id  : Entity_Id;
7786       Suffix      : Character)
7787    is
7788       T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
7789       T_Val : Entity_Id;
7790
7791    begin
7792       if Ekind (T_Ent) in Access_Kind then
7793          T_Ent := Designated_Type (T_Ent);
7794       end if;
7795
7796       T_Val := Corresponding_Record_Type (T_Ent);
7797
7798       if Present (T_Val) then
7799
7800          if No (Def_Id) then
7801             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
7802          end if;
7803
7804          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
7805
7806          Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
7807          Set_Corresponding_Record_Type (Def_Id,
7808            Constrain_Corresponding_Record
7809              (Def_Id, T_Val, Related_Nod, Related_Id));
7810
7811       else
7812          --  If there is no associated record, expansion is disabled and this
7813          --  is a generic context. Create a subtype in any case, so that
7814          --  semantic analysis can proceed.
7815
7816          if No (Def_Id) then
7817             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
7818          end if;
7819
7820          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
7821       end if;
7822    end Constrain_Concurrent;
7823
7824    ------------------------------------
7825    -- Constrain_Corresponding_Record --
7826    ------------------------------------
7827
7828    function Constrain_Corresponding_Record
7829      (Prot_Subt   : Entity_Id;
7830       Corr_Rec    : Entity_Id;
7831       Related_Nod : Node_Id;
7832       Related_Id  : Entity_Id) return Entity_Id
7833    is
7834       T_Sub : constant Entity_Id
7835         := Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
7836
7837    begin
7838       Set_Etype                   (T_Sub, Corr_Rec);
7839       Init_Size_Align             (T_Sub);
7840       Set_Has_Discriminants       (T_Sub, Has_Discriminants (Prot_Subt));
7841       Set_Is_Constrained          (T_Sub, True);
7842       Set_First_Entity            (T_Sub, First_Entity (Corr_Rec));
7843       Set_Last_Entity             (T_Sub, Last_Entity  (Corr_Rec));
7844
7845       Conditional_Delay (T_Sub, Corr_Rec);
7846
7847       if Has_Discriminants (Prot_Subt) then -- False only if errors.
7848          Set_Discriminant_Constraint (T_Sub,
7849                                       Discriminant_Constraint (Prot_Subt));
7850          Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
7851          Create_Constrained_Components (T_Sub, Related_Nod, Corr_Rec,
7852                                         Discriminant_Constraint (T_Sub));
7853       end if;
7854
7855       Set_Depends_On_Private      (T_Sub, Has_Private_Component (T_Sub));
7856
7857       return T_Sub;
7858    end Constrain_Corresponding_Record;
7859
7860    -----------------------
7861    -- Constrain_Decimal --
7862    -----------------------
7863
7864    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
7865       T           : constant Entity_Id  := Entity (Subtype_Mark (S));
7866       C           : constant Node_Id    := Constraint (S);
7867       Loc         : constant Source_Ptr := Sloc (C);
7868       Range_Expr  : Node_Id;
7869       Digits_Expr : Node_Id;
7870       Digits_Val  : Uint;
7871       Bound_Val   : Ureal;
7872
7873    begin
7874       Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
7875
7876       if Nkind (C) = N_Range_Constraint then
7877          Range_Expr := Range_Expression (C);
7878          Digits_Val := Digits_Value (T);
7879
7880       else
7881          pragma Assert (Nkind (C) = N_Digits_Constraint);
7882          Digits_Expr := Digits_Expression (C);
7883          Analyze_And_Resolve (Digits_Expr, Any_Integer);
7884
7885          Check_Digits_Expression (Digits_Expr);
7886          Digits_Val := Expr_Value (Digits_Expr);
7887
7888          if Digits_Val > Digits_Value (T) then
7889             Error_Msg_N
7890                ("digits expression is incompatible with subtype", C);
7891             Digits_Val := Digits_Value (T);
7892          end if;
7893
7894          if Present (Range_Constraint (C)) then
7895             Range_Expr := Range_Expression (Range_Constraint (C));
7896          else
7897             Range_Expr := Empty;
7898          end if;
7899       end if;
7900
7901       Set_Etype            (Def_Id, Base_Type        (T));
7902       Set_Size_Info        (Def_Id,                  (T));
7903       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
7904       Set_Delta_Value      (Def_Id, Delta_Value      (T));
7905       Set_Scale_Value      (Def_Id, Scale_Value      (T));
7906       Set_Small_Value      (Def_Id, Small_Value      (T));
7907       Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
7908       Set_Digits_Value     (Def_Id, Digits_Val);
7909
7910       --  Manufacture range from given digits value if no range present
7911
7912       if No (Range_Expr) then
7913          Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
7914          Range_Expr :=
7915             Make_Range (Loc,
7916                Low_Bound =>
7917                  Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
7918                High_Bound =>
7919                  Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
7920
7921       end if;
7922
7923       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
7924       Set_Discrete_RM_Size (Def_Id);
7925
7926       --  Unconditionally delay the freeze, since we cannot set size
7927       --  information in all cases correctly until the freeze point.
7928
7929       Set_Has_Delayed_Freeze (Def_Id);
7930    end Constrain_Decimal;
7931
7932    ----------------------------------
7933    -- Constrain_Discriminated_Type --
7934    ----------------------------------
7935
7936    procedure Constrain_Discriminated_Type
7937      (Def_Id      : Entity_Id;
7938       S           : Node_Id;
7939       Related_Nod : Node_Id;
7940       For_Access  : Boolean := False)
7941    is
7942       E     : constant Entity_Id := Entity (Subtype_Mark (S));
7943       T     : Entity_Id;
7944       C     : Node_Id;
7945       Elist : Elist_Id := New_Elmt_List;
7946
7947       procedure Fixup_Bad_Constraint;
7948       --  This is called after finding a bad constraint, and after having
7949       --  posted an appropriate error message. The mission is to leave the
7950       --  entity T in as reasonable state as possible!
7951
7952       --------------------------
7953       -- Fixup_Bad_Constraint --
7954       --------------------------
7955
7956       procedure Fixup_Bad_Constraint is
7957       begin
7958          --  Set a reasonable Ekind for the entity. For an incomplete type,
7959          --  we can't do much, but for other types, we can set the proper
7960          --  corresponding subtype kind.
7961
7962          if Ekind (T) = E_Incomplete_Type then
7963             Set_Ekind (Def_Id, Ekind (T));
7964          else
7965             Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
7966          end if;
7967
7968          Set_Etype (Def_Id, Any_Type);
7969          Set_Error_Posted (Def_Id);
7970       end Fixup_Bad_Constraint;
7971
7972    --  Start of processing for Constrain_Discriminated_Type
7973
7974    begin
7975       C := Constraint (S);
7976
7977       --  A discriminant constraint is only allowed in a subtype indication,
7978       --  after a subtype mark. This subtype mark must denote either a type
7979       --  with discriminants, or an access type whose designated type is a
7980       --  type with discriminants. A discriminant constraint specifies the
7981       --  values of these discriminants (RM 3.7.2(5)).
7982
7983       T := Base_Type (Entity (Subtype_Mark (S)));
7984
7985       if Ekind (T) in Access_Kind then
7986          T := Designated_Type (T);
7987       end if;
7988
7989       --  Check that the type has visible discriminants. The type may be
7990       --  a private type with unknown discriminants whose full view has
7991       --  discriminants which are invisible.
7992
7993       if not Has_Discriminants (T)
7994         or else
7995           (Has_Unknown_Discriminants (T)
7996              and then Is_Private_Type (T))
7997       then
7998          Error_Msg_N ("invalid constraint: type has no discriminant", C);
7999          Fixup_Bad_Constraint;
8000          return;
8001
8002       elsif Is_Constrained (E)
8003         or else (Ekind (E) = E_Class_Wide_Subtype
8004                   and then Present (Discriminant_Constraint (E)))
8005       then
8006          Error_Msg_N ("type is already constrained", Subtype_Mark (S));
8007          Fixup_Bad_Constraint;
8008          return;
8009       end if;
8010
8011       --  T may be an unconstrained subtype (e.g. a generic actual).
8012       --  Constraint applies to the base type.
8013
8014       T := Base_Type (T);
8015
8016       Elist := Build_Discriminant_Constraints (T, S);
8017
8018       --  If the list returned was empty we had an error in building the
8019       --  discriminant constraint. We have also already signalled an error
8020       --  in the incomplete type case
8021
8022       if Is_Empty_Elmt_List (Elist) then
8023          Fixup_Bad_Constraint;
8024          return;
8025       end if;
8026
8027       Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
8028    end Constrain_Discriminated_Type;
8029
8030    ---------------------------
8031    -- Constrain_Enumeration --
8032    ---------------------------
8033
8034    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
8035       T : constant Entity_Id := Entity (Subtype_Mark (S));
8036       C : constant Node_Id   := Constraint (S);
8037
8038    begin
8039       Set_Ekind (Def_Id, E_Enumeration_Subtype);
8040
8041       Set_First_Literal     (Def_Id, First_Literal (Base_Type (T)));
8042
8043       Set_Etype             (Def_Id, Base_Type         (T));
8044       Set_Size_Info         (Def_Id,                   (T));
8045       Set_First_Rep_Item    (Def_Id, First_Rep_Item    (T));
8046       Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
8047
8048       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
8049
8050       Set_Discrete_RM_Size (Def_Id);
8051
8052    end Constrain_Enumeration;
8053
8054    ----------------------
8055    -- Constrain_Float --
8056    ----------------------
8057
8058    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
8059       T    : constant Entity_Id := Entity (Subtype_Mark (S));
8060       C    : Node_Id;
8061       D    : Node_Id;
8062       Rais : Node_Id;
8063
8064    begin
8065       Set_Ekind (Def_Id, E_Floating_Point_Subtype);
8066
8067       Set_Etype          (Def_Id, Base_Type      (T));
8068       Set_Size_Info      (Def_Id,                (T));
8069       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8070
8071       --  Process the constraint
8072
8073       C := Constraint (S);
8074
8075       --  Digits constraint present
8076
8077       if Nkind (C) = N_Digits_Constraint then
8078          if Warn_On_Obsolescent_Feature then
8079             Error_Msg_N
8080               ("subtype digits constraint is an " &
8081                "obsolescent feature ('R'M 'J.3(8))?", C);
8082          end if;
8083
8084          D := Digits_Expression (C);
8085          Analyze_And_Resolve (D, Any_Integer);
8086          Check_Digits_Expression (D);
8087          Set_Digits_Value (Def_Id, Expr_Value (D));
8088
8089          --  Check that digits value is in range. Obviously we can do this
8090          --  at compile time, but it is strictly a runtime check, and of
8091          --  course there is an ACVC test that checks this!
8092
8093          if Digits_Value (Def_Id) > Digits_Value (T) then
8094             Error_Msg_Uint_1 := Digits_Value (T);
8095             Error_Msg_N ("?digits value is too large, maximum is ^", D);
8096             Rais :=
8097               Make_Raise_Constraint_Error (Sloc (D),
8098                 Reason => CE_Range_Check_Failed);
8099             Insert_Action (Declaration_Node (Def_Id), Rais);
8100          end if;
8101
8102          C := Range_Constraint (C);
8103
8104       --  No digits constraint present
8105
8106       else
8107          Set_Digits_Value (Def_Id, Digits_Value (T));
8108       end if;
8109
8110       --  Range constraint present
8111
8112       if Nkind (C) = N_Range_Constraint then
8113          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
8114
8115       --  No range constraint present
8116
8117       else
8118          pragma Assert (No (C));
8119          Set_Scalar_Range (Def_Id, Scalar_Range (T));
8120       end if;
8121
8122       Set_Is_Constrained (Def_Id);
8123    end Constrain_Float;
8124
8125    ---------------------
8126    -- Constrain_Index --
8127    ---------------------
8128
8129    procedure Constrain_Index
8130      (Index        : Node_Id;
8131       S            : Node_Id;
8132       Related_Nod  : Node_Id;
8133       Related_Id   : Entity_Id;
8134       Suffix       : Character;
8135       Suffix_Index : Nat)
8136    is
8137       Def_Id : Entity_Id;
8138       R      : Node_Id := Empty;
8139       T      : constant Entity_Id := Etype (Index);
8140
8141    begin
8142       if Nkind (S) = N_Range
8143         or else
8144           (Nkind (S) = N_Attribute_Reference
8145             and then Attribute_Name (S) = Name_Range)
8146       then
8147          --  A Range attribute will transformed into N_Range by Resolve.
8148
8149          Analyze (S);
8150          Set_Etype (S, T);
8151          R := S;
8152
8153          Process_Range_Expr_In_Decl (R, T, Empty_List);
8154
8155          if not Error_Posted (S)
8156            and then
8157              (Nkind (S) /= N_Range
8158                or else not Covers (T, (Etype (Low_Bound (S))))
8159                or else not Covers (T, (Etype (High_Bound (S)))))
8160          then
8161             if Base_Type (T) /= Any_Type
8162               and then Etype (Low_Bound (S)) /= Any_Type
8163               and then Etype (High_Bound (S)) /= Any_Type
8164             then
8165                Error_Msg_N ("range expected", S);
8166             end if;
8167          end if;
8168
8169       elsif Nkind (S) = N_Subtype_Indication then
8170          --  the parser has verified that this is a discrete indication.
8171
8172          Resolve_Discrete_Subtype_Indication (S, T);
8173          R := Range_Expression (Constraint (S));
8174
8175       elsif Nkind (S) = N_Discriminant_Association then
8176
8177          --  syntactically valid in subtype indication.
8178
8179          Error_Msg_N ("invalid index constraint", S);
8180          Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
8181          return;
8182
8183       --  Subtype_Mark case, no anonymous subtypes to construct
8184
8185       else
8186          Analyze (S);
8187
8188          if Is_Entity_Name (S) then
8189
8190             if not Is_Type (Entity (S)) then
8191                Error_Msg_N ("expect subtype mark for index constraint", S);
8192
8193             elsif Base_Type (Entity (S)) /= Base_Type (T) then
8194                Wrong_Type (S, Base_Type (T));
8195             end if;
8196
8197             return;
8198
8199          else
8200             Error_Msg_N ("invalid index constraint", S);
8201             Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
8202             return;
8203          end if;
8204       end if;
8205
8206       Def_Id :=
8207         Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
8208
8209       Set_Etype (Def_Id, Base_Type (T));
8210
8211       if Is_Modular_Integer_Type (T) then
8212          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
8213
8214       elsif Is_Integer_Type (T) then
8215          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
8216
8217       else
8218          Set_Ekind (Def_Id, E_Enumeration_Subtype);
8219          Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
8220       end if;
8221
8222       Set_Size_Info      (Def_Id,                (T));
8223       Set_RM_Size        (Def_Id, RM_Size        (T));
8224       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8225
8226       Set_Scalar_Range   (Def_Id, R);
8227
8228       Set_Etype (S, Def_Id);
8229       Set_Discrete_RM_Size (Def_Id);
8230    end Constrain_Index;
8231
8232    -----------------------
8233    -- Constrain_Integer --
8234    -----------------------
8235
8236    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
8237       T : constant Entity_Id := Entity (Subtype_Mark (S));
8238       C : constant Node_Id   := Constraint (S);
8239
8240    begin
8241       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
8242
8243       if Is_Modular_Integer_Type (T) then
8244          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
8245       else
8246          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
8247       end if;
8248
8249       Set_Etype            (Def_Id, Base_Type        (T));
8250       Set_Size_Info        (Def_Id,                  (T));
8251       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
8252       Set_Discrete_RM_Size (Def_Id);
8253
8254    end Constrain_Integer;
8255
8256    ------------------------------
8257    -- Constrain_Ordinary_Fixed --
8258    ------------------------------
8259
8260    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
8261       T    : constant Entity_Id := Entity (Subtype_Mark (S));
8262       C    : Node_Id;
8263       D    : Node_Id;
8264       Rais : Node_Id;
8265
8266    begin
8267       Set_Ekind          (Def_Id, E_Ordinary_Fixed_Point_Subtype);
8268       Set_Etype          (Def_Id, Base_Type        (T));
8269       Set_Size_Info      (Def_Id,                  (T));
8270       Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
8271       Set_Small_Value    (Def_Id, Small_Value      (T));
8272
8273       --  Process the constraint
8274
8275       C := Constraint (S);
8276
8277       --  Delta constraint present
8278
8279       if Nkind (C) = N_Delta_Constraint then
8280          if Warn_On_Obsolescent_Feature then
8281             Error_Msg_S
8282               ("subtype delta constraint is an " &
8283                "obsolescent feature ('R'M 'J.3(7))?");
8284          end if;
8285
8286          D := Delta_Expression (C);
8287          Analyze_And_Resolve (D, Any_Real);
8288          Check_Delta_Expression (D);
8289          Set_Delta_Value (Def_Id, Expr_Value_R (D));
8290
8291          --  Check that delta value is in range. Obviously we can do this
8292          --  at compile time, but it is strictly a runtime check, and of
8293          --  course there is an ACVC test that checks this!
8294
8295          if Delta_Value (Def_Id) < Delta_Value (T) then
8296             Error_Msg_N ("?delta value is too small", D);
8297             Rais :=
8298               Make_Raise_Constraint_Error (Sloc (D),
8299                 Reason => CE_Range_Check_Failed);
8300             Insert_Action (Declaration_Node (Def_Id), Rais);
8301          end if;
8302
8303          C := Range_Constraint (C);
8304
8305       --  No delta constraint present
8306
8307       else
8308          Set_Delta_Value (Def_Id, Delta_Value (T));
8309       end if;
8310
8311       --  Range constraint present
8312
8313       if Nkind (C) = N_Range_Constraint then
8314          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
8315
8316       --  No range constraint present
8317
8318       else
8319          pragma Assert (No (C));
8320          Set_Scalar_Range (Def_Id, Scalar_Range (T));
8321
8322       end if;
8323
8324       Set_Discrete_RM_Size (Def_Id);
8325
8326       --  Unconditionally delay the freeze, since we cannot set size
8327       --  information in all cases correctly until the freeze point.
8328
8329       Set_Has_Delayed_Freeze (Def_Id);
8330    end Constrain_Ordinary_Fixed;
8331
8332    ---------------------------
8333    -- Convert_Scalar_Bounds --
8334    ---------------------------
8335
8336    procedure Convert_Scalar_Bounds
8337      (N            : Node_Id;
8338       Parent_Type  : Entity_Id;
8339       Derived_Type : Entity_Id;
8340       Loc          : Source_Ptr)
8341    is
8342       Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
8343
8344       Lo  : Node_Id;
8345       Hi  : Node_Id;
8346       Rng : Node_Id;
8347
8348    begin
8349       Lo := Build_Scalar_Bound
8350               (Type_Low_Bound (Derived_Type),
8351                Parent_Type, Implicit_Base);
8352
8353       Hi := Build_Scalar_Bound
8354               (Type_High_Bound (Derived_Type),
8355                Parent_Type, Implicit_Base);
8356
8357       Rng :=
8358         Make_Range (Loc,
8359           Low_Bound  => Lo,
8360           High_Bound => Hi);
8361
8362       Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
8363
8364       Set_Parent (Rng, N);
8365       Set_Scalar_Range (Derived_Type, Rng);
8366
8367       --  Analyze the bounds
8368
8369       Analyze_And_Resolve (Lo, Implicit_Base);
8370       Analyze_And_Resolve (Hi, Implicit_Base);
8371
8372       --  Analyze the range itself, except that we do not analyze it if
8373       --  the bounds are real literals, and we have a fixed-point type.
8374       --  The reason for this is that we delay setting the bounds in this
8375       --  case till we know the final Small and Size values (see circuit
8376       --  in Freeze.Freeze_Fixed_Point_Type for further details).
8377
8378       if Is_Fixed_Point_Type (Parent_Type)
8379         and then Nkind (Lo) = N_Real_Literal
8380         and then Nkind (Hi) = N_Real_Literal
8381       then
8382          return;
8383
8384       --  Here we do the analysis of the range.
8385
8386       --  Note: we do this manually, since if we do a normal Analyze and
8387       --  Resolve call, there are problems with the conversions used for
8388       --  the derived type range.
8389
8390       else
8391          Set_Etype    (Rng, Implicit_Base);
8392          Set_Analyzed (Rng, True);
8393       end if;
8394    end Convert_Scalar_Bounds;
8395
8396    -------------------
8397    -- Copy_And_Swap --
8398    -------------------
8399
8400    procedure Copy_And_Swap (Priv, Full : Entity_Id) is
8401
8402    begin
8403       --  Initialize new full declaration entity by copying the pertinent
8404       --  fields of the corresponding private declaration entity.
8405
8406       --  We temporarily set Ekind to a value appropriate for a type to
8407       --  avoid assert failures in Einfo from checking for setting type
8408       --  attributes on something that is not a type. Ekind (Priv) is an
8409       --  appropriate choice, since it allowed the attributes to be set
8410       --  in the first place. This Ekind value will be modified later.
8411
8412       Set_Ekind (Full, Ekind (Priv));
8413
8414       --  Also set Etype temporarily to Any_Type, again, in the absence
8415       --  of errors, it will be properly reset, and if there are errors,
8416       --  then we want a value of Any_Type to remain.
8417
8418       Set_Etype (Full, Any_Type);
8419
8420       --  Now start copying attributes
8421
8422       Set_Has_Discriminants          (Full, Has_Discriminants       (Priv));
8423
8424       if Has_Discriminants (Full) then
8425          Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
8426          Set_Stored_Constraint       (Full, Stored_Constraint       (Priv));
8427       end if;
8428
8429       Set_First_Rep_Item             (Full, First_Rep_Item          (Priv));
8430       Set_Homonym                    (Full, Homonym                 (Priv));
8431       Set_Is_Immediately_Visible     (Full, Is_Immediately_Visible  (Priv));
8432       Set_Is_Public                  (Full, Is_Public               (Priv));
8433       Set_Is_Pure                    (Full, Is_Pure                 (Priv));
8434       Set_Is_Tagged_Type             (Full, Is_Tagged_Type          (Priv));
8435
8436       Conditional_Delay              (Full,                          Priv);
8437
8438       if Is_Tagged_Type (Full) then
8439          Set_Primitive_Operations    (Full, Primitive_Operations    (Priv));
8440
8441          if Priv = Base_Type (Priv) then
8442             Set_Class_Wide_Type      (Full, Class_Wide_Type         (Priv));
8443          end if;
8444       end if;
8445
8446       Set_Is_Volatile                (Full, Is_Volatile             (Priv));
8447       Set_Treat_As_Volatile          (Full, Treat_As_Volatile       (Priv));
8448       Set_Scope                      (Full, Scope                   (Priv));
8449       Set_Next_Entity                (Full, Next_Entity             (Priv));
8450       Set_First_Entity               (Full, First_Entity            (Priv));
8451       Set_Last_Entity                (Full, Last_Entity             (Priv));
8452
8453       --  If access types have been recorded for later handling, keep them
8454       --  in the full view so that they get handled when the full view
8455       --  freeze node is expanded.
8456
8457       if Present (Freeze_Node (Priv))
8458         and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
8459       then
8460          Ensure_Freeze_Node (Full);
8461          Set_Access_Types_To_Process
8462            (Freeze_Node (Full),
8463             Access_Types_To_Process (Freeze_Node (Priv)));
8464       end if;
8465
8466       --  Swap the two entities. Now Privat is the full type entity and
8467       --  Full is the private one. They will be swapped back at the end
8468       --  of the private part. This swapping ensures that the entity that
8469       --  is visible in the private part is the full declaration.
8470
8471       Exchange_Entities (Priv, Full);
8472       Append_Entity (Full, Scope (Full));
8473    end Copy_And_Swap;
8474
8475    -------------------------------------
8476    -- Copy_Array_Base_Type_Attributes --
8477    -------------------------------------
8478
8479    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
8480    begin
8481       Set_Component_Alignment      (T1, Component_Alignment      (T2));
8482       Set_Component_Type           (T1, Component_Type           (T2));
8483       Set_Component_Size           (T1, Component_Size           (T2));
8484       Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
8485       Set_Finalize_Storage_Only    (T1, Finalize_Storage_Only    (T2));
8486       Set_Has_Non_Standard_Rep     (T1, Has_Non_Standard_Rep     (T2));
8487       Set_Has_Task                 (T1, Has_Task                 (T2));
8488       Set_Is_Packed                (T1, Is_Packed                (T2));
8489       Set_Has_Aliased_Components   (T1, Has_Aliased_Components   (T2));
8490       Set_Has_Atomic_Components    (T1, Has_Atomic_Components    (T2));
8491       Set_Has_Volatile_Components  (T1, Has_Volatile_Components  (T2));
8492    end Copy_Array_Base_Type_Attributes;
8493
8494    -----------------------------------
8495    -- Copy_Array_Subtype_Attributes --
8496    -----------------------------------
8497
8498    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
8499    begin
8500       Set_Size_Info (T1, T2);
8501
8502       Set_First_Index          (T1, First_Index           (T2));
8503       Set_Is_Aliased           (T1, Is_Aliased            (T2));
8504       Set_Is_Atomic            (T1, Is_Atomic             (T2));
8505       Set_Is_Volatile          (T1, Is_Volatile           (T2));
8506       Set_Treat_As_Volatile    (T1, Treat_As_Volatile     (T2));
8507       Set_Is_Constrained       (T1, Is_Constrained        (T2));
8508       Set_Depends_On_Private   (T1, Has_Private_Component (T2));
8509       Set_First_Rep_Item       (T1, First_Rep_Item        (T2));
8510       Set_Convention           (T1, Convention            (T2));
8511       Set_Is_Limited_Composite (T1, Is_Limited_Composite  (T2));
8512       Set_Is_Private_Composite (T1, Is_Private_Composite  (T2));
8513    end Copy_Array_Subtype_Attributes;
8514
8515    -----------------------------------
8516    -- Create_Constrained_Components --
8517    -----------------------------------
8518
8519    procedure Create_Constrained_Components
8520      (Subt        : Entity_Id;
8521       Decl_Node   : Node_Id;
8522       Typ         : Entity_Id;
8523       Constraints : Elist_Id)
8524    is
8525       Loc         : constant Source_Ptr := Sloc (Subt);
8526       Comp_List   : constant Elist_Id   := New_Elmt_List;
8527       Parent_Type : constant Entity_Id  := Etype (Typ);
8528       Assoc_List  : constant List_Id    := New_List;
8529       Discr_Val   : Elmt_Id;
8530       Errors      : Boolean;
8531       New_C       : Entity_Id;
8532       Old_C       : Entity_Id;
8533       Is_Static   : Boolean := True;
8534
8535       procedure Collect_Fixed_Components (Typ : Entity_Id);
8536       --  Collect parent type components that do not appear in a variant part
8537
8538       procedure Create_All_Components;
8539       --  Iterate over Comp_List to create the components of the subtype.
8540
8541       function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
8542       --  Creates a new component from Old_Compon, copying all the fields from
8543       --  it, including its Etype, inserts the new component in the Subt entity
8544       --  chain and returns the new component.
8545
8546       function Is_Variant_Record (T : Entity_Id) return Boolean;
8547       --  If true, and discriminants are static, collect only components from
8548       --  variants selected by discriminant values.
8549
8550       ------------------------------
8551       -- Collect_Fixed_Components --
8552       ------------------------------
8553
8554       procedure Collect_Fixed_Components (Typ : Entity_Id) is
8555       begin
8556       --  Build association list for discriminants, and find components of
8557       --  the variant part selected by the values of the discriminants.
8558
8559          Old_C := First_Discriminant (Typ);
8560          Discr_Val := First_Elmt (Constraints);
8561
8562          while Present (Old_C) loop
8563             Append_To (Assoc_List,
8564               Make_Component_Association (Loc,
8565                  Choices    => New_List (New_Occurrence_Of (Old_C, Loc)),
8566                  Expression => New_Copy (Node (Discr_Val))));
8567
8568             Next_Elmt (Discr_Val);
8569             Next_Discriminant (Old_C);
8570          end loop;
8571
8572          --  The tag, and the possible parent and controller components
8573          --  are unconditionally in the subtype.
8574
8575          if Is_Tagged_Type (Typ)
8576            or else Has_Controlled_Component (Typ)
8577          then
8578             Old_C := First_Component (Typ);
8579
8580             while Present (Old_C) loop
8581                if Chars ((Old_C)) = Name_uTag
8582                  or else Chars ((Old_C)) = Name_uParent
8583                  or else Chars ((Old_C)) = Name_uController
8584                then
8585                   Append_Elmt (Old_C, Comp_List);
8586                end if;
8587
8588                Next_Component (Old_C);
8589             end loop;
8590          end if;
8591       end Collect_Fixed_Components;
8592
8593       ---------------------------
8594       -- Create_All_Components --
8595       ---------------------------
8596
8597       procedure Create_All_Components is
8598          Comp : Elmt_Id;
8599
8600       begin
8601          Comp := First_Elmt (Comp_List);
8602
8603          while Present (Comp) loop
8604             Old_C := Node (Comp);
8605             New_C := Create_Component (Old_C);
8606
8607             Set_Etype
8608               (New_C,
8609                Constrain_Component_Type
8610                  (Etype (Old_C), Subt, Decl_Node, Typ, Constraints));
8611             Set_Is_Public (New_C, Is_Public (Subt));
8612
8613             Next_Elmt (Comp);
8614          end loop;
8615       end Create_All_Components;
8616
8617       ----------------------
8618       -- Create_Component --
8619       ----------------------
8620
8621       function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
8622          New_Compon : constant Entity_Id := New_Copy (Old_Compon);
8623
8624       begin
8625          --  Set the parent so we have a proper link for freezing etc. This
8626          --  is not a real parent pointer, since of course our parent does
8627          --  not own up to us and reference us, we are an illegitimate
8628          --  child of the original parent!
8629
8630          Set_Parent (New_Compon, Parent (Old_Compon));
8631
8632          --  We do not want this node marked as Comes_From_Source, since
8633          --  otherwise it would get first class status and a separate
8634          --  cross-reference line would be generated. Illegitimate
8635          --  children do not rate such recognition.
8636
8637          Set_Comes_From_Source (New_Compon, False);
8638
8639          --  But it is a real entity, and a birth certificate must be
8640          --  properly registered by entering it into the entity list.
8641
8642          Enter_Name (New_Compon);
8643          return New_Compon;
8644       end Create_Component;
8645
8646       -----------------------
8647       -- Is_Variant_Record --
8648       -----------------------
8649
8650       function Is_Variant_Record (T : Entity_Id) return Boolean is
8651       begin
8652          return Nkind (Parent (T)) = N_Full_Type_Declaration
8653            and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
8654            and then Present (Component_List (Type_Definition (Parent (T))))
8655            and then Present (
8656              Variant_Part (Component_List (Type_Definition (Parent (T)))));
8657       end Is_Variant_Record;
8658
8659    --  Start of processing for Create_Constrained_Components
8660
8661    begin
8662       pragma Assert (Subt /= Base_Type (Subt));
8663       pragma Assert (Typ = Base_Type (Typ));
8664
8665       Set_First_Entity (Subt, Empty);
8666       Set_Last_Entity  (Subt, Empty);
8667
8668       --  Check whether constraint is fully static, in which case we can
8669       --  optimize the list of components.
8670
8671       Discr_Val := First_Elmt (Constraints);
8672
8673       while Present (Discr_Val) loop
8674
8675          if not Is_OK_Static_Expression (Node (Discr_Val)) then
8676             Is_Static := False;
8677             exit;
8678          end if;
8679
8680          Next_Elmt (Discr_Val);
8681       end loop;
8682
8683       New_Scope (Subt);
8684
8685       --  Inherit the discriminants of the parent type.
8686
8687       Old_C := First_Discriminant (Typ);
8688
8689       while Present (Old_C) loop
8690          New_C := Create_Component (Old_C);
8691          Set_Is_Public (New_C, Is_Public (Subt));
8692          Next_Discriminant (Old_C);
8693       end loop;
8694
8695       if Is_Static
8696         and then Is_Variant_Record (Typ)
8697       then
8698          Collect_Fixed_Components (Typ);
8699
8700          Gather_Components (
8701            Typ,
8702            Component_List (Type_Definition (Parent (Typ))),
8703            Governed_By   => Assoc_List,
8704            Into          => Comp_List,
8705            Report_Errors => Errors);
8706          pragma Assert (not Errors);
8707
8708          Create_All_Components;
8709
8710       --  If the subtype declaration is created for a tagged type derivation
8711       --  with constraints, we retrieve the record definition of the parent
8712       --  type to select the components of the proper variant.
8713
8714       elsif Is_Static
8715         and then Is_Tagged_Type (Typ)
8716         and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
8717         and then
8718           Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
8719         and then Is_Variant_Record (Parent_Type)
8720       then
8721          Collect_Fixed_Components (Typ);
8722
8723          Gather_Components (
8724            Typ,
8725            Component_List (Type_Definition (Parent (Parent_Type))),
8726            Governed_By   => Assoc_List,
8727            Into          => Comp_List,
8728            Report_Errors => Errors);
8729          pragma Assert (not Errors);
8730
8731          --  If the tagged derivation has a type extension, collect all the
8732          --  new components therein.
8733
8734          if Present
8735               (Record_Extension_Part (Type_Definition (Parent (Typ))))
8736          then
8737             Old_C := First_Component (Typ);
8738
8739             while Present (Old_C) loop
8740                if Original_Record_Component (Old_C) = Old_C
8741                 and then Chars (Old_C) /= Name_uTag
8742                 and then Chars (Old_C) /= Name_uParent
8743                 and then Chars (Old_C) /= Name_uController
8744                then
8745                   Append_Elmt (Old_C, Comp_List);
8746                end if;
8747
8748                Next_Component (Old_C);
8749             end loop;
8750          end if;
8751
8752          Create_All_Components;
8753
8754       else
8755          --  If the discriminants are not static, or if this is a multi-level
8756          --  type extension, we have to include all the components of the
8757          --  parent type.
8758
8759          Old_C := First_Component (Typ);
8760
8761          while Present (Old_C) loop
8762             New_C := Create_Component (Old_C);
8763
8764             Set_Etype
8765               (New_C,
8766                Constrain_Component_Type
8767                  (Etype (Old_C), Subt, Decl_Node, Typ, Constraints));
8768             Set_Is_Public (New_C, Is_Public (Subt));
8769
8770             Next_Component (Old_C);
8771          end loop;
8772       end if;
8773
8774       End_Scope;
8775    end Create_Constrained_Components;
8776
8777    ------------------------------------------
8778    -- Decimal_Fixed_Point_Type_Declaration --
8779    ------------------------------------------
8780
8781    procedure Decimal_Fixed_Point_Type_Declaration
8782      (T   : Entity_Id;
8783       Def : Node_Id)
8784    is
8785       Loc           : constant Source_Ptr := Sloc (Def);
8786       Digs_Expr     : constant Node_Id    := Digits_Expression (Def);
8787       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
8788       Implicit_Base : Entity_Id;
8789       Digs_Val      : Uint;
8790       Delta_Val     : Ureal;
8791       Scale_Val     : Uint;
8792       Bound_Val     : Ureal;
8793
8794    --  Start of processing for Decimal_Fixed_Point_Type_Declaration
8795
8796    begin
8797       Check_Restriction (No_Fixed_Point, Def);
8798
8799       --  Create implicit base type
8800
8801       Implicit_Base :=
8802         Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
8803       Set_Etype (Implicit_Base, Implicit_Base);
8804
8805       --  Analyze and process delta expression
8806
8807       Analyze_And_Resolve (Delta_Expr, Universal_Real);
8808
8809       Check_Delta_Expression (Delta_Expr);
8810       Delta_Val := Expr_Value_R (Delta_Expr);
8811
8812       --  Check delta is power of 10, and determine scale value from it
8813
8814       declare
8815          Val : Ureal := Delta_Val;
8816
8817       begin
8818          Scale_Val := Uint_0;
8819
8820          if Val < Ureal_1 then
8821             while Val < Ureal_1 loop
8822                Val := Val * Ureal_10;
8823                Scale_Val := Scale_Val + 1;
8824             end loop;
8825
8826             if Scale_Val > 18 then
8827                Error_Msg_N ("scale exceeds maximum value of 18", Def);
8828                Scale_Val := UI_From_Int (+18);
8829             end if;
8830
8831          else
8832             while Val > Ureal_1 loop
8833                Val := Val / Ureal_10;
8834                Scale_Val := Scale_Val - 1;
8835             end loop;
8836
8837             if Scale_Val < -18 then
8838                Error_Msg_N ("scale is less than minimum value of -18", Def);
8839                Scale_Val := UI_From_Int (-18);
8840             end if;
8841          end if;
8842
8843          if Val /= Ureal_1 then
8844             Error_Msg_N ("delta expression must be a power of 10", Def);
8845             Delta_Val := Ureal_10 ** (-Scale_Val);
8846          end if;
8847       end;
8848
8849       --  Set delta, scale and small (small = delta for decimal type)
8850
8851       Set_Delta_Value (Implicit_Base, Delta_Val);
8852       Set_Scale_Value (Implicit_Base, Scale_Val);
8853       Set_Small_Value (Implicit_Base, Delta_Val);
8854
8855       --  Analyze and process digits expression
8856
8857       Analyze_And_Resolve (Digs_Expr, Any_Integer);
8858       Check_Digits_Expression (Digs_Expr);
8859       Digs_Val := Expr_Value (Digs_Expr);
8860
8861       if Digs_Val > 18 then
8862          Digs_Val := UI_From_Int (+18);
8863          Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
8864       end if;
8865
8866       Set_Digits_Value (Implicit_Base, Digs_Val);
8867       Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
8868
8869       --  Set range of base type from digits value for now. This will be
8870       --  expanded to represent the true underlying base range by Freeze.
8871
8872       Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
8873
8874       --  Set size to zero for now, size will be set at freeze time. We have
8875       --  to do this for ordinary fixed-point, because the size depends on
8876       --  the specified small, and we might as well do the same for decimal
8877       --  fixed-point.
8878
8879       Init_Size_Align (Implicit_Base);
8880
8881       --  If there are bounds given in the declaration use them as the
8882       --  bounds of the first named subtype.
8883
8884       if Present (Real_Range_Specification (Def)) then
8885          declare
8886             RRS      : constant Node_Id := Real_Range_Specification (Def);
8887             Low      : constant Node_Id := Low_Bound (RRS);
8888             High     : constant Node_Id := High_Bound (RRS);
8889             Low_Val  : Ureal;
8890             High_Val : Ureal;
8891
8892          begin
8893             Analyze_And_Resolve (Low, Any_Real);
8894             Analyze_And_Resolve (High, Any_Real);
8895             Check_Real_Bound (Low);
8896             Check_Real_Bound (High);
8897             Low_Val := Expr_Value_R (Low);
8898             High_Val := Expr_Value_R (High);
8899
8900             if Low_Val < (-Bound_Val) then
8901                Error_Msg_N
8902                  ("range low bound too small for digits value", Low);
8903                Low_Val := -Bound_Val;
8904             end if;
8905
8906             if High_Val > Bound_Val then
8907                Error_Msg_N
8908                  ("range high bound too large for digits value", High);
8909                High_Val := Bound_Val;
8910             end if;
8911
8912             Set_Fixed_Range (T, Loc, Low_Val, High_Val);
8913          end;
8914
8915       --  If no explicit range, use range that corresponds to given
8916       --  digits value. This will end up as the final range for the
8917       --  first subtype.
8918
8919       else
8920          Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
8921       end if;
8922
8923       --  Complete entity for first subtype
8924
8925       Set_Ekind          (T, E_Decimal_Fixed_Point_Subtype);
8926       Set_Etype          (T, Implicit_Base);
8927       Set_Size_Info      (T, Implicit_Base);
8928       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
8929       Set_Digits_Value   (T, Digs_Val);
8930       Set_Delta_Value    (T, Delta_Val);
8931       Set_Small_Value    (T, Delta_Val);
8932       Set_Scale_Value    (T, Scale_Val);
8933       Set_Is_Constrained (T);
8934    end Decimal_Fixed_Point_Type_Declaration;
8935
8936    -----------------------
8937    -- Derive_Subprogram --
8938    -----------------------
8939
8940    procedure Derive_Subprogram
8941      (New_Subp     : in out Entity_Id;
8942       Parent_Subp  : Entity_Id;
8943       Derived_Type : Entity_Id;
8944       Parent_Type  : Entity_Id;
8945       Actual_Subp  : Entity_Id := Empty)
8946    is
8947       Formal     : Entity_Id;
8948       New_Formal : Entity_Id;
8949       Visible_Subp : Entity_Id := Parent_Subp;
8950
8951       function Is_Private_Overriding return Boolean;
8952       --  If Subp is a private overriding of a visible operation, the in-
8953       --  herited operation derives from the overridden op (even though
8954       --  its body is the overriding one) and the inherited operation is
8955       --  visible now. See sem_disp to see the details of the handling of
8956       --  the overridden subprogram, which is removed from the list of
8957       --  primitive operations of the type. The overridden subprogram is
8958       --  saved locally in Visible_Subp, and used to diagnose abstract
8959       --  operations that need overriding in the derived type.
8960
8961       procedure Replace_Type (Id, New_Id : Entity_Id);
8962       --  When the type is an anonymous access type, create a new access type
8963       --  designating the derived type.
8964
8965       procedure Set_Derived_Name;
8966       --  This procedure sets the appropriate Chars name for New_Subp. This
8967       --  is normally just a copy of the parent name. An exception arises for
8968       --  type support subprograms, where the name is changed to reflect the
8969       --  name of the derived type, e.g. if type foo is derived from type bar,
8970       --  then a procedure barDA is derived with a name fooDA.
8971
8972       ---------------------------
8973       -- Is_Private_Overriding --
8974       ---------------------------
8975
8976       function Is_Private_Overriding return Boolean is
8977          Prev : Entity_Id;
8978
8979       begin
8980          Prev := Homonym (Parent_Subp);
8981
8982          --  The visible operation that is overriden is a homonym of
8983          --  the parent subprogram. We scan the homonym chain to find
8984          --  the one whose alias is the subprogram we are deriving.
8985
8986          while Present (Prev) loop
8987             if Is_Dispatching_Operation (Parent_Subp)
8988               and then Present (Prev)
8989               and then Ekind (Prev) = Ekind (Parent_Subp)
8990               and then Alias (Prev) = Parent_Subp
8991               and then Scope (Parent_Subp) = Scope (Prev)
8992               and then not Is_Hidden (Prev)
8993             then
8994                Visible_Subp := Prev;
8995                return True;
8996             end if;
8997
8998             Prev := Homonym (Prev);
8999          end loop;
9000
9001          return False;
9002       end Is_Private_Overriding;
9003
9004       ------------------
9005       -- Replace_Type --
9006       ------------------
9007
9008       procedure Replace_Type (Id, New_Id : Entity_Id) is
9009          Acc_Type : Entity_Id;
9010          IR       : Node_Id;
9011          Par      : constant Node_Id := Parent (Derived_Type);
9012
9013       begin
9014          --  When the type is an anonymous access type, create a new access
9015          --  type designating the derived type. This itype must be elaborated
9016          --  at the point of the derivation, not on subsequent calls that may
9017          --  be out of the proper scope for Gigi, so we insert a reference to
9018          --  it after the derivation.
9019
9020          if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
9021             declare
9022                Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
9023
9024             begin
9025                if Ekind (Desig_Typ) = E_Record_Type_With_Private
9026                  and then Present (Full_View (Desig_Typ))
9027                  and then not Is_Private_Type (Parent_Type)
9028                then
9029                   Desig_Typ := Full_View (Desig_Typ);
9030                end if;
9031
9032                if Base_Type (Desig_Typ) = Base_Type (Parent_Type) then
9033                   Acc_Type := New_Copy (Etype (Id));
9034                   Set_Etype (Acc_Type, Acc_Type);
9035                   Set_Scope (Acc_Type, New_Subp);
9036
9037                   --  Compute size of anonymous access type.
9038
9039                   if Is_Array_Type (Desig_Typ)
9040                     and then not Is_Constrained (Desig_Typ)
9041                   then
9042                      Init_Size (Acc_Type, 2 * System_Address_Size);
9043                   else
9044                      Init_Size (Acc_Type, System_Address_Size);
9045                   end if;
9046
9047                   Init_Alignment (Acc_Type);
9048
9049                   Set_Directly_Designated_Type (Acc_Type, Derived_Type);
9050
9051                   Set_Etype (New_Id, Acc_Type);
9052                   Set_Scope (New_Id, New_Subp);
9053
9054                   --  Create a reference to it
9055
9056                   IR := Make_Itype_Reference (Sloc (Parent (Derived_Type)));
9057                   Set_Itype (IR, Acc_Type);
9058                   Insert_After (Parent (Derived_Type), IR);
9059
9060                else
9061                   Set_Etype (New_Id, Etype (Id));
9062                end if;
9063             end;
9064
9065          elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
9066            or else
9067              (Ekind (Etype (Id)) = E_Record_Type_With_Private
9068                and then Present (Full_View (Etype (Id)))
9069                and then
9070                  Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
9071          then
9072             --  Constraint checks on formals are generated during expansion,
9073             --  based on the signature of the original subprogram. The bounds
9074             --  of the derived type are not relevant, and thus we can use
9075             --  the base type for the formals. However, the return type may be
9076             --  used in a context that requires that the proper static bounds
9077             --  be used (a case statement, for example)  and for those cases
9078             --  we must use the derived type (first subtype), not its base.
9079
9080             --  If the derived_type_definition has no constraints, we know that
9081             --  the derived type has the same constraints as the first subtype
9082             --  of the parent, and we can also use it rather than its base,
9083             --  which can lead to more efficient code.
9084
9085             if Etype (Id) = Parent_Type then
9086                if Is_Scalar_Type (Parent_Type)
9087                  and then
9088                    Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
9089                then
9090                   Set_Etype (New_Id, Derived_Type);
9091
9092                elsif Nkind (Par) = N_Full_Type_Declaration
9093                  and then
9094                    Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
9095                  and then
9096                    Is_Entity_Name
9097                      (Subtype_Indication (Type_Definition (Par)))
9098                then
9099                   Set_Etype (New_Id, Derived_Type);
9100
9101                else
9102                   Set_Etype (New_Id, Base_Type (Derived_Type));
9103                end if;
9104
9105             else
9106                Set_Etype (New_Id, Base_Type (Derived_Type));
9107             end if;
9108
9109          else
9110             Set_Etype (New_Id, Etype (Id));
9111          end if;
9112       end Replace_Type;
9113
9114       ----------------------
9115       -- Set_Derived_Name --
9116       ----------------------
9117
9118       procedure Set_Derived_Name is
9119          Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
9120       begin
9121          if Nm = TSS_Null then
9122             Set_Chars (New_Subp, Chars (Parent_Subp));
9123          else
9124             Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
9125          end if;
9126       end Set_Derived_Name;
9127
9128    --  Start of processing for Derive_Subprogram
9129
9130    begin
9131       New_Subp :=
9132          New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
9133       Set_Ekind (New_Subp, Ekind (Parent_Subp));
9134
9135       --  Check whether the inherited subprogram is a private operation that
9136       --  should be inherited but not yet made visible. Such subprograms can
9137       --  become visible at a later point (e.g., the private part of a public
9138       --  child unit) via Declare_Inherited_Private_Subprograms. If the
9139       --  following predicate is true, then this is not such a private
9140       --  operation and the subprogram simply inherits the name of the parent
9141       --  subprogram. Note the special check for the names of controlled
9142       --  operations, which are currently exempted from being inherited with
9143       --  a hidden name because they must be findable for generation of
9144       --  implicit run-time calls.
9145
9146       if not Is_Hidden (Parent_Subp)
9147         or else Is_Internal (Parent_Subp)
9148         or else Is_Private_Overriding
9149         or else Is_Internal_Name (Chars (Parent_Subp))
9150         or else Chars (Parent_Subp) = Name_Initialize
9151         or else Chars (Parent_Subp) = Name_Adjust
9152         or else Chars (Parent_Subp) = Name_Finalize
9153       then
9154          Set_Derived_Name;
9155
9156       --  If parent is hidden, this can be a regular derivation if the
9157       --  parent is immediately visible in a non-instantiating context,
9158       --  or if we are in the private part of an instance. This test
9159       --  should still be refined ???
9160
9161       --  The test for In_Instance_Not_Visible avoids inheriting the
9162       --  derived operation as a non-visible operation in cases where
9163       --  the parent subprogram might not be visible now, but was
9164       --  visible within the original generic, so it would be wrong
9165       --  to make the inherited subprogram non-visible now. (Not
9166       --  clear if this test is fully correct; are there any cases
9167       --  where we should declare the inherited operation as not
9168       --  visible to avoid it being overridden, e.g., when the
9169       --  parent type is a generic actual with private primitives ???)
9170
9171       --  (they should be treated the same as other private inherited
9172       --  subprograms, but it's not clear how to do this cleanly). ???
9173
9174       elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
9175               and then Is_Immediately_Visible (Parent_Subp)
9176               and then not In_Instance)
9177         or else In_Instance_Not_Visible
9178       then
9179          Set_Derived_Name;
9180
9181       --  The type is inheriting a private operation, so enter
9182       --  it with a special name so it can't be overridden.
9183
9184       else
9185          Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
9186       end if;
9187
9188       Set_Parent (New_Subp, Parent (Derived_Type));
9189       Replace_Type (Parent_Subp, New_Subp);
9190       Conditional_Delay (New_Subp, Parent_Subp);
9191
9192       Formal := First_Formal (Parent_Subp);
9193       while Present (Formal) loop
9194          New_Formal := New_Copy (Formal);
9195
9196          --  Normally we do not go copying parents, but in the case of
9197          --  formals, we need to link up to the declaration (which is
9198          --  the parameter specification), and it is fine to link up to
9199          --  the original formal's parameter specification in this case.
9200
9201          Set_Parent (New_Formal, Parent (Formal));
9202
9203          Append_Entity (New_Formal, New_Subp);
9204
9205          Replace_Type (Formal, New_Formal);
9206          Next_Formal (Formal);
9207       end loop;
9208
9209       --  If this derivation corresponds to a tagged generic actual, then
9210       --  primitive operations rename those of the actual. Otherwise the
9211       --  primitive operations rename those of the parent type, If the
9212       --  parent renames an intrinsic operator, so does the new subprogram.
9213       --  We except concatenation, which is always properly typed, and does
9214       --  not get expanded as other intrinsic operations.
9215
9216       if No (Actual_Subp) then
9217          if Is_Intrinsic_Subprogram (Parent_Subp) then
9218             Set_Is_Intrinsic_Subprogram (New_Subp);
9219
9220             if Present (Alias (Parent_Subp))
9221               and then Chars (Parent_Subp) /= Name_Op_Concat
9222             then
9223                Set_Alias (New_Subp, Alias (Parent_Subp));
9224             else
9225                Set_Alias (New_Subp, Parent_Subp);
9226             end if;
9227
9228          else
9229             Set_Alias (New_Subp, Parent_Subp);
9230          end if;
9231
9232       else
9233          Set_Alias (New_Subp, Actual_Subp);
9234       end if;
9235
9236       --  Derived subprograms of a tagged type must inherit the convention
9237       --  of the parent subprogram (a requirement of AI-117). Derived
9238       --  subprograms of untagged types simply get convention Ada by default.
9239
9240       if Is_Tagged_Type (Derived_Type) then
9241          Set_Convention  (New_Subp, Convention  (Parent_Subp));
9242       end if;
9243
9244       Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
9245       Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
9246
9247       if Ekind (Parent_Subp) = E_Procedure then
9248          Set_Is_Valued_Procedure
9249            (New_Subp, Is_Valued_Procedure (Parent_Subp));
9250       end if;
9251
9252       --  A derived function with a controlling result is abstract.
9253       --  If the Derived_Type is a nonabstract formal generic derived
9254       --  type, then inherited operations are not abstract: check is
9255       --  done at instantiation time. If the derivation is for a generic
9256       --  actual, the function is not abstract unless the actual is.
9257
9258       if Is_Generic_Type (Derived_Type)
9259         and then not Is_Abstract (Derived_Type)
9260       then
9261          null;
9262
9263       elsif Is_Abstract (Alias (New_Subp))
9264         or else (Is_Tagged_Type (Derived_Type)
9265                    and then Etype (New_Subp) = Derived_Type
9266                    and then No (Actual_Subp))
9267       then
9268          Set_Is_Abstract (New_Subp);
9269
9270       --  Finally, if the parent type is abstract  we must verify that all
9271       --  inherited operations are either non-abstract or overridden, or
9272       --  that the derived type itself is abstract (this check is performed
9273       --  at the end of a package declaration, in Check_Abstract_Overriding).
9274       --  A private overriding in the parent type will not be visible in the
9275       --  derivation if we are not in an inner package or in a child unit of
9276       --  the parent type, in which case the abstractness of the inherited
9277       --  operation is carried to the new subprogram.
9278
9279       elsif Is_Abstract (Parent_Type)
9280         and then not In_Open_Scopes (Scope (Parent_Type))
9281         and then Is_Private_Overriding
9282         and then Is_Abstract (Visible_Subp)
9283       then
9284          Set_Alias (New_Subp, Visible_Subp);
9285          Set_Is_Abstract (New_Subp);
9286       end if;
9287
9288       New_Overloaded_Entity (New_Subp, Derived_Type);
9289
9290       --  Check for case of a derived subprogram for the instantiation
9291       --  of a formal derived tagged type, if so mark the subprogram as
9292       --  dispatching and inherit the dispatching attributes of the
9293       --  parent subprogram. The derived subprogram is effectively a
9294       --  renaming of the actual subprogram, so it needs to have the
9295       --  same attributes as the actual.
9296
9297       if Present (Actual_Subp)
9298         and then Is_Dispatching_Operation (Parent_Subp)
9299       then
9300          Set_Is_Dispatching_Operation (New_Subp);
9301          if Present (DTC_Entity (Parent_Subp)) then
9302             Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
9303             Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
9304          end if;
9305       end if;
9306
9307       --  Indicate that a derived subprogram does not require a body
9308       --  and that it does not require processing of default expressions.
9309
9310       Set_Has_Completion (New_Subp);
9311       Set_Default_Expressions_Processed (New_Subp);
9312
9313       if Ekind (New_Subp) = E_Function then
9314          Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
9315       end if;
9316    end Derive_Subprogram;
9317
9318    ------------------------
9319    -- Derive_Subprograms --
9320    ------------------------
9321
9322    procedure Derive_Subprograms
9323      (Parent_Type    : Entity_Id;
9324       Derived_Type   : Entity_Id;
9325       Generic_Actual : Entity_Id := Empty)
9326    is
9327       Op_List     : constant Elist_Id :=
9328                       Collect_Primitive_Operations (Parent_Type);
9329       Act_List    : Elist_Id;
9330       Act_Elmt    : Elmt_Id;
9331       Elmt        : Elmt_Id;
9332       Subp        : Entity_Id;
9333       New_Subp    : Entity_Id := Empty;
9334       Parent_Base : Entity_Id;
9335
9336    begin
9337       if Ekind (Parent_Type) = E_Record_Type_With_Private
9338         and then Has_Discriminants (Parent_Type)
9339         and then Present (Full_View (Parent_Type))
9340       then
9341          Parent_Base := Full_View (Parent_Type);
9342       else
9343          Parent_Base := Parent_Type;
9344       end if;
9345
9346       Elmt := First_Elmt (Op_List);
9347
9348       if Present (Generic_Actual) then
9349          Act_List := Collect_Primitive_Operations (Generic_Actual);
9350          Act_Elmt := First_Elmt (Act_List);
9351       else
9352          Act_Elmt := No_Elmt;
9353       end if;
9354
9355       --  Literals are derived earlier in the process of building the
9356       --  derived type, and are skipped here.
9357
9358       while Present (Elmt) loop
9359          Subp := Node (Elmt);
9360
9361          if Ekind (Subp) /= E_Enumeration_Literal then
9362             if No (Generic_Actual) then
9363                Derive_Subprogram
9364                  (New_Subp, Subp, Derived_Type, Parent_Base);
9365
9366             else
9367                Derive_Subprogram (New_Subp, Subp,
9368                  Derived_Type, Parent_Base, Node (Act_Elmt));
9369                Next_Elmt (Act_Elmt);
9370             end if;
9371          end if;
9372
9373          Next_Elmt (Elmt);
9374       end loop;
9375    end Derive_Subprograms;
9376
9377    --------------------------------
9378    -- Derived_Standard_Character --
9379    --------------------------------
9380
9381    procedure Derived_Standard_Character
9382      (N             : Node_Id;
9383       Parent_Type   : Entity_Id;
9384       Derived_Type  : Entity_Id)
9385    is
9386       Loc           : constant Source_Ptr := Sloc (N);
9387       Def           : constant Node_Id    := Type_Definition (N);
9388       Indic         : constant Node_Id    := Subtype_Indication (Def);
9389       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
9390       Implicit_Base : constant Entity_Id  :=
9391                         Create_Itype
9392                           (E_Enumeration_Type, N, Derived_Type, 'B');
9393
9394       Lo : Node_Id;
9395       Hi : Node_Id;
9396
9397    begin
9398       Discard_Node (Process_Subtype (Indic, N));
9399
9400       Set_Etype     (Implicit_Base, Parent_Base);
9401       Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
9402       Set_RM_Size   (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
9403
9404       Set_Is_Character_Type  (Implicit_Base, True);
9405       Set_Has_Delayed_Freeze (Implicit_Base);
9406
9407       --  The bounds of the implicit base are the bounds of the parent base.
9408       --  Note that their type is the parent base.
9409
9410       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
9411       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
9412
9413       Set_Scalar_Range (Implicit_Base,
9414         Make_Range (Loc,
9415           Low_Bound  => Lo,
9416           High_Bound => Hi));
9417
9418       Conditional_Delay (Derived_Type, Parent_Type);
9419
9420       Set_Ekind (Derived_Type, E_Enumeration_Subtype);
9421       Set_Etype (Derived_Type, Implicit_Base);
9422       Set_Size_Info         (Derived_Type, Parent_Type);
9423
9424       if Unknown_RM_Size (Derived_Type) then
9425          Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9426       end if;
9427
9428       Set_Is_Character_Type (Derived_Type, True);
9429
9430       if Nkind (Indic) /= N_Subtype_Indication then
9431
9432          --  If no explicit constraint, the bounds are those
9433          --  of the parent type.
9434
9435          Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Type));
9436          Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
9437          Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
9438       end if;
9439
9440       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
9441
9442       --  Because the implicit base is used in the conversion of the bounds,
9443       --  we have to freeze it now. This is similar to what is done for
9444       --  numeric types, and it equally suspicious, but otherwise a non-
9445       --  static bound will have a reference to an unfrozen type, which is
9446       --  rejected by Gigi (???).
9447
9448       Freeze_Before (N, Implicit_Base);
9449    end Derived_Standard_Character;
9450
9451    ------------------------------
9452    -- Derived_Type_Declaration --
9453    ------------------------------
9454
9455    procedure Derived_Type_Declaration
9456      (T             : Entity_Id;
9457       N             : Node_Id;
9458       Is_Completion : Boolean)
9459    is
9460       Def          : constant Node_Id := Type_Definition (N);
9461       Indic        : constant Node_Id := Subtype_Indication (Def);
9462       Extension    : constant Node_Id := Record_Extension_Part (Def);
9463       Parent_Type  : Entity_Id;
9464       Parent_Scope : Entity_Id;
9465       Taggd        : Boolean;
9466
9467    begin
9468       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
9469
9470       if Parent_Type = Any_Type
9471         or else Etype (Parent_Type) = Any_Type
9472         or else (Is_Class_Wide_Type (Parent_Type)
9473                   and then Etype (Parent_Type) = T)
9474       then
9475          --  If Parent_Type is undefined or illegal, make new type into
9476          --  a subtype of Any_Type, and set a few attributes to prevent
9477          --  cascaded errors. If this is a self-definition, emit error now.
9478
9479          if T = Parent_Type
9480            or else T = Etype (Parent_Type)
9481          then
9482             Error_Msg_N ("type cannot be used in its own definition", Indic);
9483          end if;
9484
9485          Set_Ekind        (T, Ekind (Parent_Type));
9486          Set_Etype        (T, Any_Type);
9487          Set_Scalar_Range (T, Scalar_Range (Any_Type));
9488
9489          if Is_Tagged_Type (T) then
9490             Set_Primitive_Operations (T, New_Elmt_List);
9491          end if;
9492
9493          return;
9494
9495       elsif Is_Unchecked_Union (Parent_Type) then
9496          Error_Msg_N ("cannot derive from Unchecked_Union type", N);
9497
9498       --  Ada 2005 (AI-231): Static check
9499
9500       elsif Is_Access_Type (Parent_Type)
9501         and then Null_Exclusion_Present (Type_Definition (N))
9502         and then Can_Never_Be_Null (Parent_Type)
9503       then
9504          Error_Msg_N ("(Ada 2005) null exclusion not allowed if parent is "
9505                       & "already non-null", Type_Definition (N));
9506       end if;
9507
9508       --  Only composite types other than array types are allowed to have
9509       --  discriminants.
9510
9511       if Present (Discriminant_Specifications (N))
9512         and then (Is_Elementary_Type (Parent_Type)
9513                   or else Is_Array_Type (Parent_Type))
9514         and then not Error_Posted (N)
9515       then
9516          Error_Msg_N
9517            ("elementary or array type cannot have discriminants",
9518             Defining_Identifier (First (Discriminant_Specifications (N))));
9519          Set_Has_Discriminants (T, False);
9520       end if;
9521
9522       --  In Ada 83, a derived type defined in a package specification cannot
9523       --  be used for further derivation until the end of its visible part.
9524       --  Note that derivation in the private part of the package is allowed.
9525
9526       if Ada_Version = Ada_83
9527         and then Is_Derived_Type (Parent_Type)
9528         and then In_Visible_Part (Scope (Parent_Type))
9529       then
9530          if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
9531             Error_Msg_N
9532               ("(Ada 83): premature use of type for derivation", Indic);
9533          end if;
9534       end if;
9535
9536       --  Check for early use of incomplete or private type
9537
9538       if Ekind (Parent_Type) = E_Void
9539         or else Ekind (Parent_Type) = E_Incomplete_Type
9540       then
9541          Error_Msg_N ("premature derivation of incomplete type", Indic);
9542          return;
9543
9544       elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
9545               and then not Is_Generic_Type (Parent_Type)
9546               and then not Is_Generic_Type (Root_Type (Parent_Type))
9547               and then not Is_Generic_Actual_Type (Parent_Type))
9548         or else Has_Private_Component (Parent_Type)
9549       then
9550          --  The ancestor type of a formal type can be incomplete, in which
9551          --  case only the operations of the partial view are available in
9552          --  the generic. Subsequent checks may be required when the full
9553          --  view is analyzed, to verify that derivation from a tagged type
9554          --  has an extension.
9555
9556          if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
9557             null;
9558
9559          elsif No (Underlying_Type (Parent_Type))
9560            or else Has_Private_Component (Parent_Type)
9561          then
9562             Error_Msg_N
9563               ("premature derivation of derived or private type", Indic);
9564
9565             --  Flag the type itself as being in error, this prevents some
9566             --  nasty problems with people looking at the malformed type.
9567
9568             Set_Error_Posted (T);
9569
9570          --  Check that within the immediate scope of an untagged partial
9571          --  view it's illegal to derive from the partial view if the
9572          --  full view is tagged. (7.3(7))
9573
9574          --  We verify that the Parent_Type is a partial view by checking
9575          --  that it is not a Full_Type_Declaration (i.e. a private type or
9576          --  private extension declaration), to distinguish a partial view
9577          --  from  a derivation from a private type which also appears as
9578          --  E_Private_Type.
9579
9580          elsif Present (Full_View (Parent_Type))
9581            and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
9582            and then not Is_Tagged_Type (Parent_Type)
9583            and then Is_Tagged_Type (Full_View (Parent_Type))
9584          then
9585             Parent_Scope := Scope (T);
9586             while Present (Parent_Scope)
9587               and then Parent_Scope /= Standard_Standard
9588             loop
9589                if Parent_Scope = Scope (Parent_Type) then
9590                   Error_Msg_N
9591                     ("premature derivation from type with tagged full view",
9592                      Indic);
9593                end if;
9594
9595                Parent_Scope := Scope (Parent_Scope);
9596             end loop;
9597          end if;
9598       end if;
9599
9600       --  Check that form of derivation is appropriate
9601
9602       Taggd := Is_Tagged_Type (Parent_Type);
9603
9604       --  Perhaps the parent type should be changed to the class-wide type's
9605       --  specific type in this case to prevent cascading errors ???
9606
9607       if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
9608          Error_Msg_N ("parent type must not be a class-wide type", Indic);
9609          return;
9610       end if;
9611
9612       if Present (Extension) and then not Taggd then
9613          Error_Msg_N
9614            ("type derived from untagged type cannot have extension", Indic);
9615
9616       elsif No (Extension) and then Taggd then
9617          --  If this is within a private part (or body) of a generic
9618          --  instantiation then the derivation is allowed (the parent
9619          --  type can only appear tagged in this case if it's a generic
9620          --  actual type, since it would otherwise have been rejected
9621          --  in the analysis of the generic template).
9622
9623          if not Is_Generic_Actual_Type (Parent_Type)
9624            or else In_Visible_Part (Scope (Parent_Type))
9625          then
9626             Error_Msg_N
9627               ("type derived from tagged type must have extension", Indic);
9628          end if;
9629       end if;
9630
9631       Build_Derived_Type (N, Parent_Type, T, Is_Completion);
9632    end Derived_Type_Declaration;
9633
9634    ----------------------------------
9635    -- Enumeration_Type_Declaration --
9636    ----------------------------------
9637
9638    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
9639       Ev     : Uint;
9640       L      : Node_Id;
9641       R_Node : Node_Id;
9642       B_Node : Node_Id;
9643
9644    begin
9645       --  Create identifier node representing lower bound
9646
9647       B_Node := New_Node (N_Identifier, Sloc (Def));
9648       L := First (Literals (Def));
9649       Set_Chars (B_Node, Chars (L));
9650       Set_Entity (B_Node,  L);
9651       Set_Etype (B_Node, T);
9652       Set_Is_Static_Expression (B_Node, True);
9653
9654       R_Node := New_Node (N_Range, Sloc (Def));
9655       Set_Low_Bound  (R_Node, B_Node);
9656
9657       Set_Ekind (T, E_Enumeration_Type);
9658       Set_First_Literal (T, L);
9659       Set_Etype (T, T);
9660       Set_Is_Constrained (T);
9661
9662       Ev := Uint_0;
9663
9664       --  Loop through literals of enumeration type setting pos and rep values
9665       --  except that if the Ekind is already set, then it means that the
9666       --  literal was already constructed (case of a derived type declaration
9667       --  and we should not disturb the Pos and Rep values.
9668
9669       while Present (L) loop
9670          if Ekind (L) /= E_Enumeration_Literal then
9671             Set_Ekind (L, E_Enumeration_Literal);
9672             Set_Enumeration_Pos (L, Ev);
9673             Set_Enumeration_Rep (L, Ev);
9674             Set_Is_Known_Valid  (L, True);
9675          end if;
9676
9677          Set_Etype (L, T);
9678          New_Overloaded_Entity (L);
9679          Generate_Definition (L);
9680          Set_Convention (L, Convention_Intrinsic);
9681
9682          if Nkind (L) = N_Defining_Character_Literal then
9683             Set_Is_Character_Type (T, True);
9684          end if;
9685
9686          Ev := Ev + 1;
9687          Next (L);
9688       end loop;
9689
9690       --  Now create a node representing upper bound
9691
9692       B_Node := New_Node (N_Identifier, Sloc (Def));
9693       Set_Chars (B_Node, Chars (Last (Literals (Def))));
9694       Set_Entity (B_Node,  Last (Literals (Def)));
9695       Set_Etype (B_Node, T);
9696       Set_Is_Static_Expression (B_Node, True);
9697
9698       Set_High_Bound (R_Node, B_Node);
9699       Set_Scalar_Range (T, R_Node);
9700       Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
9701       Set_Enum_Esize (T);
9702
9703       --  Set Discard_Names if configuration pragma set, or if there is
9704       --  a parameterless pragma in the current declarative region
9705
9706       if Global_Discard_Names
9707         or else Discard_Names (Scope (T))
9708       then
9709          Set_Discard_Names (T);
9710       end if;
9711
9712       --  Process end label if there is one
9713
9714       if Present (Def) then
9715          Process_End_Label (Def, 'e', T);
9716       end if;
9717    end Enumeration_Type_Declaration;
9718
9719    ---------------------------------
9720    -- Expand_To_Stored_Constraint --
9721    ---------------------------------
9722
9723    function Expand_To_Stored_Constraint
9724      (Typ        : Entity_Id;
9725       Constraint : Elist_Id) return Elist_Id
9726    is
9727       Explicitly_Discriminated_Type : Entity_Id;
9728       Expansion    : Elist_Id;
9729       Discriminant : Entity_Id;
9730
9731       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
9732       --  Find the nearest type that actually specifies discriminants.
9733
9734       ---------------------------------
9735       -- Type_With_Explicit_Discrims --
9736       ---------------------------------
9737
9738       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
9739          Typ : constant E := Base_Type (Id);
9740
9741       begin
9742          if Ekind (Typ) in Incomplete_Or_Private_Kind then
9743             if Present (Full_View (Typ)) then
9744                return Type_With_Explicit_Discrims (Full_View (Typ));
9745             end if;
9746
9747          else
9748             if Has_Discriminants (Typ) then
9749                return Typ;
9750             end if;
9751          end if;
9752
9753          if Etype (Typ) = Typ then
9754             return Empty;
9755          elsif Has_Discriminants (Typ) then
9756             return Typ;
9757          else
9758             return Type_With_Explicit_Discrims (Etype (Typ));
9759          end if;
9760
9761       end Type_With_Explicit_Discrims;
9762
9763    --  Start of processing for Expand_To_Stored_Constraint
9764
9765    begin
9766       if No (Constraint)
9767         or else Is_Empty_Elmt_List (Constraint)
9768       then
9769          return No_Elist;
9770       end if;
9771
9772       Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
9773
9774       if No (Explicitly_Discriminated_Type) then
9775          return No_Elist;
9776       end if;
9777
9778       Expansion := New_Elmt_List;
9779
9780       Discriminant :=
9781          First_Stored_Discriminant (Explicitly_Discriminated_Type);
9782
9783       while Present (Discriminant) loop
9784
9785          Append_Elmt (
9786            Get_Discriminant_Value (
9787              Discriminant, Explicitly_Discriminated_Type, Constraint),
9788            Expansion);
9789
9790          Next_Stored_Discriminant (Discriminant);
9791       end loop;
9792
9793       return Expansion;
9794    end Expand_To_Stored_Constraint;
9795
9796    --------------------
9797    -- Find_Type_Name --
9798    --------------------
9799
9800    function Find_Type_Name (N : Node_Id) return Entity_Id is
9801       Id       : constant Entity_Id := Defining_Identifier (N);
9802       Prev     : Entity_Id;
9803       New_Id   : Entity_Id;
9804       Prev_Par : Node_Id;
9805
9806    begin
9807       --  Find incomplete declaration, if some was given.
9808
9809       Prev := Current_Entity_In_Scope (Id);
9810
9811       if Present (Prev) then
9812
9813          --  Previous declaration exists. Error if not incomplete/private case
9814          --  except if previous declaration is implicit, etc. Enter_Name will
9815          --  emit error if appropriate.
9816
9817          Prev_Par := Parent (Prev);
9818
9819          if not Is_Incomplete_Or_Private_Type (Prev) then
9820             Enter_Name (Id);
9821             New_Id := Id;
9822
9823          elsif Nkind (N) /= N_Full_Type_Declaration
9824            and then Nkind (N) /= N_Task_Type_Declaration
9825            and then Nkind (N) /= N_Protected_Type_Declaration
9826          then
9827             --  Completion must be a full type declarations (RM 7.3(4))
9828
9829             Error_Msg_Sloc := Sloc (Prev);
9830             Error_Msg_NE ("invalid completion of }", Id, Prev);
9831
9832             --  Set scope of Id to avoid cascaded errors. Entity is never
9833             --  examined again, except when saving globals in generics.
9834
9835             Set_Scope (Id, Current_Scope);
9836             New_Id := Id;
9837
9838          --  Case of full declaration of incomplete type
9839
9840          elsif Ekind (Prev) = E_Incomplete_Type then
9841
9842             --  Indicate that the incomplete declaration has a matching
9843             --  full declaration. The defining occurrence of the incomplete
9844             --  declaration remains the visible one, and the procedure
9845             --  Get_Full_View dereferences it whenever the type is used.
9846
9847             if Present (Full_View (Prev)) then
9848                Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
9849             end if;
9850
9851             Set_Full_View (Prev,  Id);
9852             Append_Entity (Id, Current_Scope);
9853             Set_Is_Public (Id, Is_Public (Prev));
9854             Set_Is_Internal (Id);
9855             New_Id := Prev;
9856
9857          --  Case of full declaration of private type
9858
9859          else
9860             if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
9861                if Etype (Prev) /= Prev then
9862
9863                   --  Prev is a private subtype or a derived type, and needs
9864                   --  no completion.
9865
9866                   Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
9867                   New_Id := Id;
9868
9869                elsif Ekind (Prev) = E_Private_Type
9870                  and then
9871                    (Nkind (N) = N_Task_Type_Declaration
9872                      or else Nkind (N) = N_Protected_Type_Declaration)
9873                then
9874                   Error_Msg_N
9875                    ("completion of nonlimited type cannot be limited", N);
9876                end if;
9877
9878             elsif Nkind (N) /= N_Full_Type_Declaration
9879               or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
9880             then
9881                Error_Msg_N ("full view of private extension must be"
9882                  & " an extension", N);
9883
9884             elsif not (Abstract_Present (Parent (Prev)))
9885               and then Abstract_Present (Type_Definition (N))
9886             then
9887                Error_Msg_N ("full view of non-abstract extension cannot"
9888                  & " be abstract", N);
9889             end if;
9890
9891             if not In_Private_Part (Current_Scope) then
9892                Error_Msg_N
9893                  ("declaration of full view must appear in private part",  N);
9894             end if;
9895
9896             Copy_And_Swap (Prev, Id);
9897             Set_Has_Private_Declaration (Prev);
9898             Set_Has_Private_Declaration (Id);
9899
9900             --  If no error, propagate freeze_node from private to full view.
9901             --  It may have been generated for an early operational item.
9902
9903             if Present (Freeze_Node (Id))
9904               and then Serious_Errors_Detected = 0
9905               and then No (Full_View (Id))
9906             then
9907                Set_Freeze_Node (Prev, Freeze_Node (Id));
9908                Set_Freeze_Node (Id, Empty);
9909                Set_First_Rep_Item (Prev, First_Rep_Item (Id));
9910             end if;
9911
9912             Set_Full_View (Id, Prev);
9913             New_Id := Prev;
9914          end if;
9915
9916          --  Verify that full declaration conforms to incomplete one
9917
9918          if Is_Incomplete_Or_Private_Type (Prev)
9919            and then Present (Discriminant_Specifications (Prev_Par))
9920          then
9921             if Present (Discriminant_Specifications (N)) then
9922                if Ekind (Prev) = E_Incomplete_Type then
9923                   Check_Discriminant_Conformance (N, Prev, Prev);
9924                else
9925                   Check_Discriminant_Conformance (N, Prev, Id);
9926                end if;
9927
9928             else
9929                Error_Msg_N
9930                  ("missing discriminants in full type declaration", N);
9931
9932                --  To avoid cascaded errors on subsequent use, share the
9933                --  discriminants of the partial view.
9934
9935                Set_Discriminant_Specifications (N,
9936                  Discriminant_Specifications (Prev_Par));
9937             end if;
9938          end if;
9939
9940          --  A prior untagged private type can have an associated
9941          --  class-wide type due to use of the class attribute,
9942          --  and in this case also the full type is required to
9943          --  be tagged.
9944
9945          if Is_Type (Prev)
9946            and then (Is_Tagged_Type (Prev)
9947                       or else Present (Class_Wide_Type (Prev)))
9948          then
9949             --  The full declaration is either a tagged record or an
9950             --  extension otherwise this is an error
9951
9952             if Nkind (Type_Definition (N)) = N_Record_Definition then
9953                if not Tagged_Present (Type_Definition (N)) then
9954                   Error_Msg_NE
9955                     ("full declaration of } must be tagged", Prev, Id);
9956                   Set_Is_Tagged_Type (Id);
9957                   Set_Primitive_Operations (Id, New_Elmt_List);
9958                end if;
9959
9960             elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
9961                if No (Record_Extension_Part (Type_Definition (N))) then
9962                   Error_Msg_NE (
9963                     "full declaration of } must be a record extension",
9964                     Prev, Id);
9965                   Set_Is_Tagged_Type (Id);
9966                   Set_Primitive_Operations (Id, New_Elmt_List);
9967                end if;
9968
9969             else
9970                Error_Msg_NE
9971                  ("full declaration of } must be a tagged type", Prev, Id);
9972
9973             end if;
9974          end if;
9975
9976          return New_Id;
9977
9978       else
9979          --  New type declaration
9980
9981          Enter_Name (Id);
9982          return Id;
9983       end if;
9984    end Find_Type_Name;
9985
9986    -------------------------
9987    -- Find_Type_Of_Object --
9988    -------------------------
9989
9990    function Find_Type_Of_Object
9991      (Obj_Def     : Node_Id;
9992       Related_Nod : Node_Id) return Entity_Id
9993    is
9994       Def_Kind : constant Node_Kind := Nkind (Obj_Def);
9995       P        : Node_Id := Parent (Obj_Def);
9996       T        : Entity_Id;
9997       Nam      : Name_Id;
9998
9999    begin
10000       --  If the parent is a component_definition node we climb to the
10001       --  component_declaration node
10002
10003       if Nkind (P) = N_Component_Definition then
10004          P := Parent (P);
10005       end if;
10006
10007       --  Case of an anonymous array subtype
10008
10009       if Def_Kind = N_Constrained_Array_Definition
10010         or else Def_Kind = N_Unconstrained_Array_Definition
10011       then
10012          T := Empty;
10013          Array_Type_Declaration (T, Obj_Def);
10014
10015       --  Create an explicit subtype whenever possible.
10016
10017       elsif Nkind (P) /= N_Component_Declaration
10018         and then Def_Kind = N_Subtype_Indication
10019       then
10020          --  Base name of subtype on object name, which will be unique in
10021          --  the current scope.
10022
10023          --  If this is a duplicate declaration, return base type, to avoid
10024          --  generating duplicate anonymous types.
10025
10026          if Error_Posted (P) then
10027             Analyze (Subtype_Mark (Obj_Def));
10028             return Entity (Subtype_Mark (Obj_Def));
10029          end if;
10030
10031          Nam :=
10032             New_External_Name
10033              (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
10034
10035          T := Make_Defining_Identifier (Sloc (P), Nam);
10036
10037          Insert_Action (Obj_Def,
10038            Make_Subtype_Declaration (Sloc (P),
10039              Defining_Identifier => T,
10040              Subtype_Indication  => Relocate_Node (Obj_Def)));
10041
10042          --  This subtype may need freezing, and this will not be done
10043          --  automatically if the object declaration is not in a
10044          --  declarative part. Since this is an object declaration, the
10045          --  type cannot always be frozen here. Deferred constants do not
10046          --  freeze their type (which often enough will be private).
10047
10048          if Nkind (P) = N_Object_Declaration
10049            and then Constant_Present (P)
10050            and then No (Expression (P))
10051          then
10052             null;
10053
10054          else
10055             Insert_Actions (Obj_Def, Freeze_Entity (T, Sloc (P)));
10056          end if;
10057
10058       else
10059          T := Process_Subtype (Obj_Def, Related_Nod);
10060       end if;
10061
10062       return T;
10063    end Find_Type_Of_Object;
10064
10065    --------------------------------
10066    -- Find_Type_Of_Subtype_Indic --
10067    --------------------------------
10068
10069    function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
10070       Typ : Entity_Id;
10071
10072    begin
10073       --  Case of subtype mark with a constraint
10074
10075       if Nkind (S) = N_Subtype_Indication then
10076          Find_Type (Subtype_Mark (S));
10077          Typ := Entity (Subtype_Mark (S));
10078
10079          if not
10080            Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
10081          then
10082             Error_Msg_N
10083               ("incorrect constraint for this kind of type", Constraint (S));
10084             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
10085          end if;
10086
10087       --  Otherwise we have a subtype mark without a constraint
10088
10089       elsif Error_Posted (S) then
10090          Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
10091          return Any_Type;
10092
10093       else
10094          Find_Type (S);
10095          Typ := Entity (S);
10096       end if;
10097
10098       if Typ = Standard_Wide_Character
10099         or else Typ = Standard_Wide_String
10100       then
10101          Check_Restriction (No_Wide_Characters, S);
10102       end if;
10103
10104       return Typ;
10105    end Find_Type_Of_Subtype_Indic;
10106
10107    -------------------------------------
10108    -- Floating_Point_Type_Declaration --
10109    -------------------------------------
10110
10111    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
10112       Digs          : constant Node_Id := Digits_Expression (Def);
10113       Digs_Val      : Uint;
10114       Base_Typ      : Entity_Id;
10115       Implicit_Base : Entity_Id;
10116       Bound         : Node_Id;
10117
10118       function Can_Derive_From (E : Entity_Id) return Boolean;
10119       --  Find if given digits value allows derivation from specified type
10120
10121       ---------------------
10122       -- Can_Derive_From --
10123       ---------------------
10124
10125       function Can_Derive_From (E : Entity_Id) return Boolean is
10126          Spec : constant Entity_Id := Real_Range_Specification (Def);
10127
10128       begin
10129          if Digs_Val > Digits_Value (E) then
10130             return False;
10131          end if;
10132
10133          if Present (Spec) then
10134             if Expr_Value_R (Type_Low_Bound (E)) >
10135                Expr_Value_R (Low_Bound (Spec))
10136             then
10137                return False;
10138             end if;
10139
10140             if Expr_Value_R (Type_High_Bound (E)) <
10141                Expr_Value_R (High_Bound (Spec))
10142             then
10143                return False;
10144             end if;
10145          end if;
10146
10147          return True;
10148       end Can_Derive_From;
10149
10150    --  Start of processing for Floating_Point_Type_Declaration
10151
10152    begin
10153       Check_Restriction (No_Floating_Point, Def);
10154
10155       --  Create an implicit base type
10156
10157       Implicit_Base :=
10158         Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
10159
10160       --  Analyze and verify digits value
10161
10162       Analyze_And_Resolve (Digs, Any_Integer);
10163       Check_Digits_Expression (Digs);
10164       Digs_Val := Expr_Value (Digs);
10165
10166       --  Process possible range spec and find correct type to derive from
10167
10168       Process_Real_Range_Specification (Def);
10169
10170       if Can_Derive_From (Standard_Short_Float) then
10171          Base_Typ := Standard_Short_Float;
10172       elsif Can_Derive_From (Standard_Float) then
10173          Base_Typ := Standard_Float;
10174       elsif Can_Derive_From (Standard_Long_Float) then
10175          Base_Typ := Standard_Long_Float;
10176       elsif Can_Derive_From (Standard_Long_Long_Float) then
10177          Base_Typ := Standard_Long_Long_Float;
10178
10179       --  If we can't derive from any existing type, use long_long_float
10180       --  and give appropriate message explaining the problem.
10181
10182       else
10183          Base_Typ := Standard_Long_Long_Float;
10184
10185          if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
10186             Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
10187             Error_Msg_N ("digits value out of range, maximum is ^", Digs);
10188
10189          else
10190             Error_Msg_N
10191               ("range too large for any predefined type",
10192                Real_Range_Specification (Def));
10193          end if;
10194       end if;
10195
10196       --  If there are bounds given in the declaration use them as the bounds
10197       --  of the type, otherwise use the bounds of the predefined base type
10198       --  that was chosen based on the Digits value.
10199
10200       if Present (Real_Range_Specification (Def)) then
10201          Set_Scalar_Range (T, Real_Range_Specification (Def));
10202          Set_Is_Constrained (T);
10203
10204          --  The bounds of this range must be converted to machine numbers
10205          --  in accordance with RM 4.9(38).
10206
10207          Bound := Type_Low_Bound (T);
10208
10209          if Nkind (Bound) = N_Real_Literal then
10210             Set_Realval
10211               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
10212             Set_Is_Machine_Number (Bound);
10213          end if;
10214
10215          Bound := Type_High_Bound (T);
10216
10217          if Nkind (Bound) = N_Real_Literal then
10218             Set_Realval
10219               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
10220             Set_Is_Machine_Number (Bound);
10221          end if;
10222
10223       else
10224          Set_Scalar_Range (T, Scalar_Range (Base_Typ));
10225       end if;
10226
10227       --  Complete definition of implicit base and declared first subtype
10228
10229       Set_Etype          (Implicit_Base, Base_Typ);
10230
10231       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
10232       Set_Size_Info      (Implicit_Base,                (Base_Typ));
10233       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
10234       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
10235       Set_Digits_Value   (Implicit_Base, Digits_Value   (Base_Typ));
10236       Set_Vax_Float      (Implicit_Base, Vax_Float      (Base_Typ));
10237
10238       Set_Ekind          (T, E_Floating_Point_Subtype);
10239       Set_Etype          (T, Implicit_Base);
10240
10241       Set_Size_Info      (T,                (Implicit_Base));
10242       Set_RM_Size        (T, RM_Size        (Implicit_Base));
10243       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
10244       Set_Digits_Value   (T, Digs_Val);
10245
10246    end Floating_Point_Type_Declaration;
10247
10248    ----------------------------
10249    -- Get_Discriminant_Value --
10250    ----------------------------
10251
10252    --  This is the situation...
10253
10254    --  There is a non-derived type
10255
10256    --       type T0 (Dx, Dy, Dz...)
10257
10258    --  There are zero or more levels of derivation, with each
10259    --  derivation either purely inheriting the discriminants, or
10260    --  defining its own.
10261
10262    --       type Ti      is new Ti-1
10263    --  or
10264    --       type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
10265    --  or
10266    --       subtype Ti is ...
10267
10268    --  The subtype issue is avoided by the use of
10269    --    Original_Record_Component, and the fact that derived subtypes
10270    --    also derive the constraints.
10271
10272    --  This chain leads back from
10273
10274    --       Typ_For_Constraint
10275
10276    --  Typ_For_Constraint has discriminants, and the value for each
10277    --  discriminant is given by its corresponding Elmt of Constraints.
10278
10279    --  Discriminant is some discriminant in this hierarchy.
10280
10281    --  We need to return its value.
10282
10283    --  We do this by recursively searching each level, and looking for
10284    --  Discriminant. Once we get to the bottom, we start backing up
10285    --  returning the value for it which may in turn be a discriminant
10286    --  further up, so on the backup we continue the substitution.
10287
10288    function Get_Discriminant_Value
10289      (Discriminant       : Entity_Id;
10290       Typ_For_Constraint : Entity_Id;
10291       Constraint         : Elist_Id) return Node_Id
10292    is
10293       function Search_Derivation_Levels
10294         (Ti                    : Entity_Id;
10295          Discrim_Values        : Elist_Id;
10296          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
10297       --  This is the routine that performs the recursive search of levels
10298       --  as described above.
10299
10300       ------------------------------
10301       -- Search_Derivation_Levels --
10302       ------------------------------
10303
10304       function Search_Derivation_Levels
10305         (Ti                    : Entity_Id;
10306          Discrim_Values        : Elist_Id;
10307          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
10308       is
10309          Assoc          : Elmt_Id;
10310          Disc           : Entity_Id;
10311          Result         : Node_Or_Entity_Id;
10312          Result_Entity  : Node_Id;
10313
10314       begin
10315          --  If inappropriate type, return Error, this happens only in
10316          --  cascaded error situations, and we want to avoid a blow up.
10317
10318          if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
10319             return Error;
10320          end if;
10321
10322          --  Look deeper if possible. Use Stored_Constraints only for
10323          --  untagged types. For tagged types use the given constraint.
10324          --  This asymmetry needs explanation???
10325
10326          if not Stored_Discrim_Values
10327            and then Present (Stored_Constraint (Ti))
10328            and then not Is_Tagged_Type (Ti)
10329          then
10330             Result :=
10331               Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
10332          else
10333             declare
10334                Td : constant Entity_Id := Etype (Ti);
10335
10336             begin
10337                if Td = Ti then
10338                   Result := Discriminant;
10339
10340                else
10341                   if Present (Stored_Constraint (Ti)) then
10342                      Result :=
10343                         Search_Derivation_Levels
10344                           (Td, Stored_Constraint (Ti), True);
10345                   else
10346                      Result :=
10347                         Search_Derivation_Levels
10348                           (Td, Discrim_Values, Stored_Discrim_Values);
10349                   end if;
10350                end if;
10351             end;
10352          end if;
10353
10354          --  Extra underlying places to search, if not found above. For
10355          --  concurrent types, the relevant discriminant appears in the
10356          --  corresponding record. For a type derived from a private type
10357          --  without discriminant, the full view inherits the discriminants
10358          --  of the full view of the parent.
10359
10360          if Result = Discriminant then
10361             if Is_Concurrent_Type (Ti)
10362               and then Present (Corresponding_Record_Type (Ti))
10363             then
10364                Result :=
10365                  Search_Derivation_Levels (
10366                    Corresponding_Record_Type (Ti),
10367                    Discrim_Values,
10368                    Stored_Discrim_Values);
10369
10370             elsif Is_Private_Type (Ti)
10371               and then not Has_Discriminants (Ti)
10372               and then Present (Full_View (Ti))
10373               and then Etype (Full_View (Ti)) /= Ti
10374             then
10375                Result :=
10376                  Search_Derivation_Levels (
10377                    Full_View (Ti),
10378                    Discrim_Values,
10379                    Stored_Discrim_Values);
10380             end if;
10381          end if;
10382
10383          --  If Result is not a (reference to a) discriminant,
10384          --  return it, otherwise set Result_Entity to the discriminant.
10385
10386          if Nkind (Result) = N_Defining_Identifier then
10387
10388             pragma Assert (Result = Discriminant);
10389
10390             Result_Entity := Result;
10391
10392          else
10393             if not Denotes_Discriminant (Result) then
10394                return Result;
10395             end if;
10396
10397             Result_Entity := Entity (Result);
10398          end if;
10399
10400          --  See if this level of derivation actually has discriminants
10401          --  because tagged derivations can add them, hence the lower
10402          --  levels need not have any.
10403
10404          if not Has_Discriminants (Ti) then
10405             return Result;
10406          end if;
10407
10408          --  Scan Ti's discriminants for Result_Entity,
10409          --  and return its corresponding value, if any.
10410
10411          Result_Entity := Original_Record_Component (Result_Entity);
10412
10413          Assoc := First_Elmt (Discrim_Values);
10414
10415          if Stored_Discrim_Values then
10416             Disc := First_Stored_Discriminant (Ti);
10417          else
10418             Disc := First_Discriminant (Ti);
10419          end if;
10420
10421          while Present (Disc) loop
10422
10423             pragma Assert (Present (Assoc));
10424
10425             if Original_Record_Component (Disc) = Result_Entity then
10426                return Node (Assoc);
10427             end if;
10428
10429             Next_Elmt (Assoc);
10430
10431             if Stored_Discrim_Values then
10432                Next_Stored_Discriminant (Disc);
10433             else
10434                Next_Discriminant (Disc);
10435             end if;
10436          end loop;
10437
10438          --  Could not find it
10439          --
10440          return Result;
10441       end Search_Derivation_Levels;
10442
10443       Result : Node_Or_Entity_Id;
10444
10445    --  Start of processing for Get_Discriminant_Value
10446
10447    begin
10448       --  ??? this routine is a gigantic mess and will be deleted.
10449       --  for the time being just test for the trivial case before calling
10450       --  recurse.
10451
10452       if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
10453          declare
10454             D : Entity_Id := First_Discriminant (Typ_For_Constraint);
10455             E : Elmt_Id   := First_Elmt (Constraint);
10456          begin
10457             while Present (D) loop
10458                if Chars (D) = Chars (Discriminant) then
10459                   return Node (E);
10460                end if;
10461
10462                Next_Discriminant (D);
10463                Next_Elmt (E);
10464             end loop;
10465          end;
10466       end if;
10467
10468       Result := Search_Derivation_Levels
10469         (Typ_For_Constraint, Constraint, False);
10470
10471       --  ??? hack to disappear when this routine is gone
10472
10473       if  Nkind (Result) = N_Defining_Identifier then
10474          declare
10475             D : Entity_Id := First_Discriminant (Typ_For_Constraint);
10476             E : Elmt_Id   := First_Elmt (Constraint);
10477
10478          begin
10479             while Present (D) loop
10480                if Corresponding_Discriminant (D) = Discriminant then
10481                   return Node (E);
10482                end if;
10483
10484                Next_Discriminant (D);
10485                Next_Elmt (E);
10486             end loop;
10487          end;
10488       end if;
10489
10490       pragma Assert (Nkind (Result) /= N_Defining_Identifier);
10491       return Result;
10492    end Get_Discriminant_Value;
10493
10494    --------------------------
10495    -- Has_Range_Constraint --
10496    --------------------------
10497
10498    function Has_Range_Constraint (N : Node_Id) return Boolean is
10499       C : constant Node_Id := Constraint (N);
10500
10501    begin
10502       if Nkind (C) = N_Range_Constraint then
10503          return True;
10504
10505       elsif Nkind (C) = N_Digits_Constraint then
10506          return
10507             Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
10508               or else
10509             Present (Range_Constraint (C));
10510
10511       elsif Nkind (C) = N_Delta_Constraint then
10512          return Present (Range_Constraint (C));
10513
10514       else
10515          return False;
10516       end if;
10517    end Has_Range_Constraint;
10518
10519    ------------------------
10520    -- Inherit_Components --
10521    ------------------------
10522
10523    function Inherit_Components
10524      (N             : Node_Id;
10525       Parent_Base   : Entity_Id;
10526       Derived_Base  : Entity_Id;
10527       Is_Tagged     : Boolean;
10528       Inherit_Discr : Boolean;
10529       Discs         : Elist_Id) return Elist_Id
10530    is
10531       Assoc_List : constant Elist_Id := New_Elmt_List;
10532
10533       procedure Inherit_Component
10534         (Old_C          : Entity_Id;
10535          Plain_Discrim  : Boolean := False;
10536          Stored_Discrim : Boolean := False);
10537       --  Inherits component Old_C from Parent_Base to the Derived_Base.
10538       --  If Plain_Discrim is True, Old_C is a discriminant.
10539       --  If Stored_Discrim is True, Old_C is a stored discriminant.
10540       --  If they are both false then Old_C is a regular component.
10541
10542       -----------------------
10543       -- Inherit_Component --
10544       -----------------------
10545
10546       procedure Inherit_Component
10547         (Old_C          : Entity_Id;
10548          Plain_Discrim  : Boolean := False;
10549          Stored_Discrim : Boolean := False)
10550       is
10551          New_C : constant Entity_Id := New_Copy (Old_C);
10552
10553          Discrim      : Entity_Id;
10554          Corr_Discrim : Entity_Id;
10555
10556       begin
10557          pragma Assert (not Is_Tagged or else not Stored_Discrim);
10558
10559          Set_Parent (New_C, Parent (Old_C));
10560
10561          --  Regular discriminants and components must be inserted
10562          --  in the scope of the Derived_Base. Do it here.
10563
10564          if not Stored_Discrim then
10565             Enter_Name (New_C);
10566          end if;
10567
10568          --  For tagged types the Original_Record_Component must point to
10569          --  whatever this field was pointing to in the parent type. This has
10570          --  already been achieved by the call to New_Copy above.
10571
10572          if not Is_Tagged then
10573             Set_Original_Record_Component (New_C, New_C);
10574          end if;
10575
10576          --  If we have inherited a component then see if its Etype contains
10577          --  references to Parent_Base discriminants. In this case, replace
10578          --  these references with the constraints given in Discs. We do not
10579          --  do this for the partial view of private types because this is
10580          --  not needed (only the components of the full view will be used
10581          --  for code generation) and cause problem. We also avoid this
10582          --  transformation in some error situations.
10583
10584          if Ekind (New_C) = E_Component then
10585             if (Is_Private_Type (Derived_Base)
10586                   and then not Is_Generic_Type (Derived_Base))
10587               or else (Is_Empty_Elmt_List (Discs)
10588                        and then  not Expander_Active)
10589             then
10590                Set_Etype (New_C, Etype (Old_C));
10591             else
10592                Set_Etype (New_C, Constrain_Component_Type (Etype (Old_C),
10593                  Derived_Base, N, Parent_Base, Discs));
10594             end if;
10595          end if;
10596
10597          --  In derived tagged types it is illegal to reference a non
10598          --  discriminant component in the parent type. To catch this, mark
10599          --  these components with an Ekind of E_Void. This will be reset in
10600          --  Record_Type_Definition after processing the record extension of
10601          --  the derived type.
10602
10603          if Is_Tagged and then Ekind (New_C) = E_Component then
10604             Set_Ekind (New_C, E_Void);
10605          end if;
10606
10607          if Plain_Discrim then
10608             Set_Corresponding_Discriminant (New_C, Old_C);
10609             Build_Discriminal (New_C);
10610
10611          --  If we are explicitly inheriting a stored discriminant it will be
10612          --  completely hidden.
10613
10614          elsif Stored_Discrim then
10615             Set_Corresponding_Discriminant (New_C, Empty);
10616             Set_Discriminal (New_C, Empty);
10617             Set_Is_Completely_Hidden (New_C);
10618
10619             --  Set the Original_Record_Component of each discriminant in the
10620             --  derived base to point to the corresponding stored that we just
10621             --  created.
10622
10623             Discrim := First_Discriminant (Derived_Base);
10624             while Present (Discrim) loop
10625                Corr_Discrim := Corresponding_Discriminant (Discrim);
10626
10627                --  Corr_Discrimm could be missing in an error situation.
10628
10629                if Present (Corr_Discrim)
10630                  and then Original_Record_Component (Corr_Discrim) = Old_C
10631                then
10632                   Set_Original_Record_Component (Discrim, New_C);
10633                end if;
10634
10635                Next_Discriminant (Discrim);
10636             end loop;
10637
10638             Append_Entity (New_C, Derived_Base);
10639          end if;
10640
10641          if not Is_Tagged then
10642             Append_Elmt (Old_C, Assoc_List);
10643             Append_Elmt (New_C, Assoc_List);
10644          end if;
10645       end Inherit_Component;
10646
10647       --  Variables local to Inherit_Components.
10648
10649       Loc : constant Source_Ptr := Sloc (N);
10650
10651       Parent_Discrim : Entity_Id;
10652       Stored_Discrim : Entity_Id;
10653       D              : Entity_Id;
10654
10655       Component        : Entity_Id;
10656
10657    --  Start of processing for Inherit_Components
10658
10659    begin
10660       if not Is_Tagged then
10661          Append_Elmt (Parent_Base,  Assoc_List);
10662          Append_Elmt (Derived_Base, Assoc_List);
10663       end if;
10664
10665       --  Inherit parent discriminants if needed.
10666
10667       if Inherit_Discr then
10668          Parent_Discrim := First_Discriminant (Parent_Base);
10669          while Present (Parent_Discrim) loop
10670             Inherit_Component (Parent_Discrim, Plain_Discrim => True);
10671             Next_Discriminant (Parent_Discrim);
10672          end loop;
10673       end if;
10674
10675       --  Create explicit stored discrims for untagged types when necessary.
10676
10677       if not Has_Unknown_Discriminants (Derived_Base)
10678         and then Has_Discriminants (Parent_Base)
10679         and then not Is_Tagged
10680         and then
10681           (not Inherit_Discr
10682            or else First_Discriminant (Parent_Base) /=
10683                    First_Stored_Discriminant (Parent_Base))
10684       then
10685          Stored_Discrim := First_Stored_Discriminant (Parent_Base);
10686          while Present (Stored_Discrim) loop
10687             Inherit_Component (Stored_Discrim, Stored_Discrim => True);
10688             Next_Stored_Discriminant (Stored_Discrim);
10689          end loop;
10690       end if;
10691
10692       --  See if we can apply the second transformation for derived types, as
10693       --  explained in point 6. in the comments above Build_Derived_Record_Type
10694       --  This is achieved by appending Derived_Base discriminants into
10695       --  Discs, which has the side effect of returning a non empty Discs
10696       --  list to the caller of Inherit_Components, which is what we want.
10697       --  This must be done for private derived types if there are explicit
10698       --  stored discriminants, to ensure that we can retrieve the values of
10699       --  the constraints provided in the ancestors.
10700
10701       if Inherit_Discr
10702         and then Is_Empty_Elmt_List (Discs)
10703         and then Present (First_Discriminant (Derived_Base))
10704         and then
10705           (not Is_Private_Type (Derived_Base)
10706            or else Is_Completely_Hidden
10707              (First_Stored_Discriminant (Derived_Base))
10708            or else Is_Generic_Type (Derived_Base))
10709       then
10710          D := First_Discriminant (Derived_Base);
10711          while Present (D) loop
10712             Append_Elmt (New_Reference_To (D, Loc), Discs);
10713             Next_Discriminant (D);
10714          end loop;
10715       end if;
10716
10717       --  Finally, inherit non-discriminant components unless they are not
10718       --  visible because defined or inherited from the full view of the
10719       --  parent. Don't inherit the _parent field of the parent type.
10720
10721       Component := First_Entity (Parent_Base);
10722       while Present (Component) loop
10723          if Ekind (Component) /= E_Component
10724            or else Chars (Component) = Name_uParent
10725          then
10726             null;
10727
10728          --  If the derived type is within the parent type's declarative
10729          --  region, then the components can still be inherited even though
10730          --  they aren't visible at this point. This can occur for cases
10731          --  such as within public child units where the components must
10732          --  become visible upon entering the child unit's private part.
10733
10734          elsif not Is_Visible_Component (Component)
10735            and then not In_Open_Scopes (Scope (Parent_Base))
10736          then
10737             null;
10738
10739          elsif Ekind (Derived_Base) = E_Private_Type
10740            or else Ekind (Derived_Base) = E_Limited_Private_Type
10741          then
10742             null;
10743
10744          else
10745             Inherit_Component (Component);
10746          end if;
10747
10748          Next_Entity (Component);
10749       end loop;
10750
10751       --  For tagged derived types, inherited discriminants cannot be used in
10752       --  component declarations of the record extension part. To achieve this
10753       --  we mark the inherited discriminants as not visible.
10754
10755       if Is_Tagged and then Inherit_Discr then
10756          D := First_Discriminant (Derived_Base);
10757          while Present (D) loop
10758             Set_Is_Immediately_Visible (D, False);
10759             Next_Discriminant (D);
10760          end loop;
10761       end if;
10762
10763       return Assoc_List;
10764    end Inherit_Components;
10765
10766    ------------------------------
10767    -- Is_Valid_Constraint_Kind --
10768    ------------------------------
10769
10770    function Is_Valid_Constraint_Kind
10771      (T_Kind          : Type_Kind;
10772       Constraint_Kind : Node_Kind) return Boolean
10773    is
10774    begin
10775       case T_Kind is
10776
10777          when Enumeration_Kind |
10778               Integer_Kind =>
10779             return Constraint_Kind = N_Range_Constraint;
10780
10781          when Decimal_Fixed_Point_Kind =>
10782             return
10783               Constraint_Kind = N_Digits_Constraint
10784                 or else
10785               Constraint_Kind = N_Range_Constraint;
10786
10787          when Ordinary_Fixed_Point_Kind =>
10788             return
10789               Constraint_Kind = N_Delta_Constraint
10790                 or else
10791               Constraint_Kind = N_Range_Constraint;
10792
10793          when Float_Kind =>
10794             return
10795               Constraint_Kind = N_Digits_Constraint
10796                 or else
10797               Constraint_Kind = N_Range_Constraint;
10798
10799          when Access_Kind       |
10800               Array_Kind        |
10801               E_Record_Type     |
10802               E_Record_Subtype  |
10803               Class_Wide_Kind   |
10804               E_Incomplete_Type |
10805               Private_Kind      |
10806               Concurrent_Kind  =>
10807             return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
10808
10809          when others =>
10810             return True; -- Error will be detected later.
10811       end case;
10812
10813    end Is_Valid_Constraint_Kind;
10814
10815    --------------------------
10816    -- Is_Visible_Component --
10817    --------------------------
10818
10819    function Is_Visible_Component (C : Entity_Id) return Boolean is
10820       Original_Comp  : Entity_Id := Empty;
10821       Original_Scope : Entity_Id;
10822       Type_Scope     : Entity_Id;
10823
10824       function Is_Local_Type (Typ : Entity_Id) return Boolean;
10825       --  Check whether parent type of inherited component is declared
10826       --  locally, possibly within a nested package or instance. The
10827       --  current scope is the derived record itself.
10828
10829       -------------------
10830       -- Is_Local_Type --
10831       -------------------
10832
10833       function Is_Local_Type (Typ : Entity_Id) return Boolean is
10834          Scop : Entity_Id := Scope (Typ);
10835
10836       begin
10837          while Present (Scop)
10838            and then Scop /= Standard_Standard
10839          loop
10840             if Scop = Scope (Current_Scope) then
10841                return True;
10842             end if;
10843
10844             Scop := Scope (Scop);
10845          end loop;
10846          return False;
10847       end Is_Local_Type;
10848
10849    --  Start of processing for Is_Visible_Component
10850
10851    begin
10852       if Ekind (C) = E_Component
10853         or else Ekind (C) = E_Discriminant
10854       then
10855          Original_Comp := Original_Record_Component (C);
10856       end if;
10857
10858       if No (Original_Comp) then
10859
10860          --  Premature usage, or previous error
10861
10862          return False;
10863
10864       else
10865          Original_Scope := Scope (Original_Comp);
10866          Type_Scope     := Scope (Base_Type (Scope (C)));
10867       end if;
10868
10869       --  This test only concerns tagged types
10870
10871       if not Is_Tagged_Type (Original_Scope) then
10872          return True;
10873
10874       --  If it is _Parent or _Tag, there is no visibility issue
10875
10876       elsif not Comes_From_Source (Original_Comp) then
10877          return True;
10878
10879       --  If we are in the body of an instantiation, the component is
10880       --  visible even when the parent type (possibly defined in an
10881       --  enclosing unit or in a parent unit) might not.
10882
10883       elsif In_Instance_Body then
10884          return True;
10885
10886       --  Discriminants are always visible.
10887
10888       elsif Ekind (Original_Comp) = E_Discriminant
10889         and then not Has_Unknown_Discriminants (Original_Scope)
10890       then
10891          return True;
10892
10893       --  If the component has been declared in an ancestor which is
10894       --  currently a private type, then it is not visible. The same
10895       --  applies if the component's containing type is not in an
10896       --  open scope and the original component's enclosing type
10897       --  is a visible full type of a private type (which can occur
10898       --  in cases where an attempt is being made to reference a
10899       --  component in a sibling package that is inherited from a
10900       --  visible component of a type in an ancestor package; the
10901       --  component in the sibling package should not be visible
10902       --  even though the component it inherited from is visible).
10903       --  This does not apply however in the case where the scope
10904       --  of the type is a private child unit, or when the parent
10905       --  comes from a local package in which the ancestor is
10906       --  currently visible. The latter suppression of visibility
10907       --  is needed for cases that are tested in B730006.
10908
10909       elsif Is_Private_Type (Original_Scope)
10910         or else
10911           (not Is_Private_Descendant (Type_Scope)
10912             and then not In_Open_Scopes (Type_Scope)
10913             and then Has_Private_Declaration (Original_Scope))
10914       then
10915          --  If the type derives from an entity in a formal package, there
10916          --  are no additional visible components.
10917
10918          if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
10919             N_Formal_Package_Declaration
10920          then
10921             return False;
10922
10923          --  if we are not in the private part of the current package, there
10924          --  are no additional visible components.
10925
10926          elsif Ekind (Scope (Current_Scope)) = E_Package
10927            and then not In_Private_Part (Scope (Current_Scope))
10928          then
10929             return False;
10930          else
10931             return
10932               Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
10933                 and then Is_Local_Type (Type_Scope);
10934          end if;
10935
10936       --  There is another weird way in which a component may be invisible
10937       --  when the private and the full view are not derived from the same
10938       --  ancestor. Here is an example :
10939
10940       --       type A1 is tagged      record F1 : integer; end record;
10941       --       type A2 is new A1 with record F2 : integer; end record;
10942       --       type T is new A1 with private;
10943       --     private
10944       --       type T is new A2 with null record;
10945
10946       --  In this case, the full view of T inherits F1 and F2 but the
10947       --  private view inherits only F1
10948
10949       else
10950          declare
10951             Ancestor : Entity_Id := Scope (C);
10952
10953          begin
10954             loop
10955                if Ancestor = Original_Scope then
10956                   return True;
10957                elsif Ancestor = Etype (Ancestor) then
10958                   return False;
10959                end if;
10960
10961                Ancestor := Etype (Ancestor);
10962             end loop;
10963
10964             return True;
10965          end;
10966       end if;
10967    end Is_Visible_Component;
10968
10969    --------------------------
10970    -- Make_Class_Wide_Type --
10971    --------------------------
10972
10973    procedure Make_Class_Wide_Type (T : Entity_Id) is
10974       CW_Type : Entity_Id;
10975       CW_Name : Name_Id;
10976       Next_E  : Entity_Id;
10977
10978    begin
10979       --  The class wide type can have been defined by the partial view in
10980       --  which case everything is already done
10981
10982       if Present (Class_Wide_Type (T)) then
10983          return;
10984       end if;
10985
10986       CW_Type :=
10987         New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
10988
10989       --  Inherit root type characteristics
10990
10991       CW_Name := Chars (CW_Type);
10992       Next_E  := Next_Entity (CW_Type);
10993       Copy_Node (T, CW_Type);
10994       Set_Comes_From_Source (CW_Type, False);
10995       Set_Chars (CW_Type, CW_Name);
10996       Set_Parent (CW_Type, Parent (T));
10997       Set_Next_Entity (CW_Type, Next_E);
10998       Set_Has_Delayed_Freeze (CW_Type);
10999
11000       --  Customize the class-wide type: It has no prim. op., it cannot be
11001       --  abstract and its Etype points back to the specific root type.
11002
11003       Set_Ekind                (CW_Type, E_Class_Wide_Type);
11004       Set_Is_Tagged_Type       (CW_Type, True);
11005       Set_Primitive_Operations (CW_Type, New_Elmt_List);
11006       Set_Is_Abstract          (CW_Type, False);
11007       Set_Is_Constrained       (CW_Type, False);
11008       Set_Is_First_Subtype     (CW_Type, Is_First_Subtype (T));
11009       Init_Size_Align          (CW_Type);
11010
11011       if Ekind (T) = E_Class_Wide_Subtype then
11012          Set_Etype             (CW_Type, Etype (Base_Type (T)));
11013       else
11014          Set_Etype             (CW_Type, T);
11015       end if;
11016
11017       --  If this is the class_wide type of a constrained subtype, it does
11018       --  not have discriminants.
11019
11020       Set_Has_Discriminants (CW_Type,
11021         Has_Discriminants (T) and then not Is_Constrained (T));
11022
11023       Set_Has_Unknown_Discriminants (CW_Type, True);
11024       Set_Class_Wide_Type (T, CW_Type);
11025       Set_Equivalent_Type (CW_Type, Empty);
11026
11027       --  The class-wide type of a class-wide type is itself (RM 3.9(14))
11028
11029       Set_Class_Wide_Type (CW_Type, CW_Type);
11030
11031    end Make_Class_Wide_Type;
11032
11033    ----------------
11034    -- Make_Index --
11035    ----------------
11036
11037    procedure Make_Index
11038      (I            : Node_Id;
11039       Related_Nod  : Node_Id;
11040       Related_Id   : Entity_Id := Empty;
11041       Suffix_Index : Nat := 1)
11042    is
11043       R      : Node_Id;
11044       T      : Entity_Id;
11045       Def_Id : Entity_Id := Empty;
11046       Found  : Boolean := False;
11047
11048    begin
11049       --  For a discrete range used in a constrained array definition and
11050       --  defined by a range, an implicit conversion to the predefined type
11051       --  INTEGER is assumed if each bound is either a numeric literal, a named
11052       --  number, or an attribute, and the type of both bounds (prior to the
11053       --  implicit conversion) is the type universal_integer. Otherwise, both
11054       --  bounds must be of the same discrete type, other than universal
11055       --  integer; this type must be determinable independently of the
11056       --  context, but using the fact that the type must be discrete and that
11057       --  both bounds must have the same type.
11058
11059       --  Character literals also have a universal type in the absence of
11060       --  of additional context,  and are resolved to Standard_Character.
11061
11062       if Nkind (I) = N_Range then
11063
11064          --  The index is given by a range constraint. The bounds are known
11065          --  to be of a consistent type.
11066
11067          if not Is_Overloaded (I) then
11068             T := Etype (I);
11069
11070             --  If the bounds are universal, choose the specific predefined
11071             --  type.
11072
11073             if T = Universal_Integer then
11074                T := Standard_Integer;
11075
11076             elsif T = Any_Character then
11077
11078                if Ada_Version >= Ada_95 then
11079                   Error_Msg_N
11080                     ("ambiguous character literals (could be Wide_Character)",
11081                       I);
11082                end if;
11083
11084                T := Standard_Character;
11085             end if;
11086
11087          else
11088             T := Any_Type;
11089
11090             declare
11091                Ind : Interp_Index;
11092                It  : Interp;
11093
11094             begin
11095                Get_First_Interp (I, Ind, It);
11096
11097                while Present (It.Typ) loop
11098                   if Is_Discrete_Type (It.Typ) then
11099
11100                      if Found
11101                        and then not Covers (It.Typ, T)
11102                        and then not Covers (T, It.Typ)
11103                      then
11104                         Error_Msg_N ("ambiguous bounds in discrete range", I);
11105                         exit;
11106                      else
11107                         T := It.Typ;
11108                         Found := True;
11109                      end if;
11110                   end if;
11111
11112                   Get_Next_Interp (Ind, It);
11113                end loop;
11114
11115                if T = Any_Type then
11116                   Error_Msg_N ("discrete type required for range", I);
11117                   Set_Etype (I, Any_Type);
11118                   return;
11119
11120                elsif T = Universal_Integer then
11121                   T := Standard_Integer;
11122                end if;
11123             end;
11124          end if;
11125
11126          if not Is_Discrete_Type (T) then
11127             Error_Msg_N ("discrete type required for range", I);
11128             Set_Etype (I, Any_Type);
11129             return;
11130          end if;
11131
11132          if Nkind (Low_Bound (I)) = N_Attribute_Reference
11133            and then Attribute_Name (Low_Bound (I)) = Name_First
11134            and then Is_Entity_Name (Prefix (Low_Bound (I)))
11135            and then Is_Type (Entity (Prefix (Low_Bound (I))))
11136            and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
11137          then
11138             --  The type of the index will be the type of the prefix,
11139             --  as long as the upper bound is 'Last of the same type.
11140
11141             Def_Id := Entity (Prefix (Low_Bound (I)));
11142
11143             if Nkind (High_Bound (I)) /= N_Attribute_Reference
11144               or else Attribute_Name (High_Bound (I)) /= Name_Last
11145               or else not Is_Entity_Name (Prefix (High_Bound (I)))
11146               or else Entity (Prefix (High_Bound (I))) /= Def_Id
11147             then
11148                Def_Id := Empty;
11149             end if;
11150          end if;
11151
11152          R := I;
11153          Process_Range_Expr_In_Decl (R, T);
11154
11155       elsif Nkind (I) = N_Subtype_Indication then
11156
11157          --  The index is given by a subtype with a range constraint.
11158
11159          T :=  Base_Type (Entity (Subtype_Mark (I)));
11160
11161          if not Is_Discrete_Type (T) then
11162             Error_Msg_N ("discrete type required for range", I);
11163             Set_Etype (I, Any_Type);
11164             return;
11165          end if;
11166
11167          R := Range_Expression (Constraint (I));
11168
11169          Resolve (R, T);
11170          Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
11171
11172       elsif Nkind (I) = N_Attribute_Reference then
11173
11174          --  The parser guarantees that the attribute is a RANGE attribute
11175
11176          --  If the node denotes the range of a type mark, that is also the
11177          --  resulting type, and we do no need to create an Itype for it.
11178
11179          if Is_Entity_Name (Prefix (I))
11180            and then Comes_From_Source (I)
11181            and then Is_Type (Entity (Prefix (I)))
11182            and then Is_Discrete_Type (Entity (Prefix (I)))
11183          then
11184             Def_Id := Entity (Prefix (I));
11185          end if;
11186
11187          Analyze_And_Resolve (I);
11188          T := Etype (I);
11189          R := I;
11190
11191       --  If none of the above, must be a subtype. We convert this to a
11192       --  range attribute reference because in the case of declared first
11193       --  named subtypes, the types in the range reference can be different
11194       --  from the type of the entity. A range attribute normalizes the
11195       --  reference and obtains the correct types for the bounds.
11196
11197       --  This transformation is in the nature of an expansion, is only
11198       --  done if expansion is active. In particular, it is not done on
11199       --  formal generic types,  because we need to retain the name of the
11200       --  original index for instantiation purposes.
11201
11202       else
11203          if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
11204             Error_Msg_N ("invalid subtype mark in discrete range ", I);
11205             Set_Etype (I, Any_Integer);
11206             return;
11207          else
11208             --  The type mark may be that of an incomplete type. It is only
11209             --  now that we can get the full view, previous analysis does
11210             --  not look specifically for a type mark.
11211
11212             Set_Entity (I, Get_Full_View (Entity (I)));
11213             Set_Etype  (I, Entity (I));
11214             Def_Id := Entity (I);
11215
11216             if not Is_Discrete_Type (Def_Id) then
11217                Error_Msg_N ("discrete type required for index", I);
11218                Set_Etype (I, Any_Type);
11219                return;
11220             end if;
11221          end if;
11222
11223          if Expander_Active then
11224             Rewrite (I,
11225               Make_Attribute_Reference (Sloc (I),
11226                 Attribute_Name => Name_Range,
11227                 Prefix         => Relocate_Node (I)));
11228
11229             --  The original was a subtype mark that does not freeze. This
11230             --  means that the rewritten version must not freeze either.
11231
11232             Set_Must_Not_Freeze (I);
11233             Set_Must_Not_Freeze (Prefix (I));
11234
11235             --  Is order critical??? if so, document why, if not
11236             --  use Analyze_And_Resolve
11237
11238             Analyze (I);
11239             T := Etype (I);
11240             Resolve (I);
11241             R := I;
11242
11243          --  If expander is inactive, type is legal, nothing else to construct
11244
11245          else
11246             return;
11247          end if;
11248       end if;
11249
11250       if not Is_Discrete_Type (T) then
11251          Error_Msg_N ("discrete type required for range", I);
11252          Set_Etype (I, Any_Type);
11253          return;
11254
11255       elsif T = Any_Type then
11256          Set_Etype (I, Any_Type);
11257          return;
11258       end if;
11259
11260       --  We will now create the appropriate Itype to describe the
11261       --  range, but first a check. If we originally had a subtype,
11262       --  then we just label the range with this subtype. Not only
11263       --  is there no need to construct a new subtype, but it is wrong
11264       --  to do so for two reasons:
11265
11266       --    1. A legality concern, if we have a subtype, it must not
11267       --       freeze, and the Itype would cause freezing incorrectly
11268
11269       --    2. An efficiency concern, if we created an Itype, it would
11270       --       not be recognized as the same type for the purposes of
11271       --       eliminating checks in some circumstances.
11272
11273       --  We signal this case by setting the subtype entity in Def_Id.
11274
11275       if No (Def_Id) then
11276
11277          Def_Id :=
11278            Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
11279          Set_Etype (Def_Id, Base_Type (T));
11280
11281          if Is_Signed_Integer_Type (T) then
11282             Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11283
11284          elsif Is_Modular_Integer_Type (T) then
11285             Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11286
11287          else
11288             Set_Ekind             (Def_Id, E_Enumeration_Subtype);
11289             Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11290             Set_First_Literal     (Def_Id, First_Literal (T));
11291          end if;
11292
11293          Set_Size_Info      (Def_Id,                  (T));
11294          Set_RM_Size        (Def_Id, RM_Size          (T));
11295          Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
11296
11297          Set_Scalar_Range   (Def_Id, R);
11298          Conditional_Delay  (Def_Id, T);
11299
11300          --  In the subtype indication case, if the immediate parent of the
11301          --  new subtype is non-static, then the subtype we create is non-
11302          --  static, even if its bounds are static.
11303
11304          if Nkind (I) = N_Subtype_Indication
11305            and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
11306          then
11307             Set_Is_Non_Static_Subtype (Def_Id);
11308          end if;
11309       end if;
11310
11311       --  Final step is to label the index with this constructed type
11312
11313       Set_Etype (I, Def_Id);
11314    end Make_Index;
11315
11316    ------------------------------
11317    -- Modular_Type_Declaration --
11318    ------------------------------
11319
11320    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
11321       Mod_Expr : constant Node_Id := Expression (Def);
11322       M_Val    : Uint;
11323
11324       procedure Set_Modular_Size (Bits : Int);
11325       --  Sets RM_Size to Bits, and Esize to normal word size above this
11326
11327       ----------------------
11328       -- Set_Modular_Size --
11329       ----------------------
11330
11331       procedure Set_Modular_Size (Bits : Int) is
11332       begin
11333          Set_RM_Size (T, UI_From_Int (Bits));
11334
11335          if Bits <= 8 then
11336             Init_Esize (T, 8);
11337
11338          elsif Bits <= 16 then
11339             Init_Esize (T, 16);
11340
11341          elsif Bits <= 32 then
11342             Init_Esize (T, 32);
11343
11344          else
11345             Init_Esize (T, System_Max_Binary_Modulus_Power);
11346          end if;
11347       end Set_Modular_Size;
11348
11349    --  Start of processing for Modular_Type_Declaration
11350
11351    begin
11352       Analyze_And_Resolve (Mod_Expr, Any_Integer);
11353       Set_Etype (T, T);
11354       Set_Ekind (T, E_Modular_Integer_Type);
11355       Init_Alignment (T);
11356       Set_Is_Constrained (T);
11357
11358       if not Is_OK_Static_Expression (Mod_Expr) then
11359          Flag_Non_Static_Expr
11360            ("non-static expression used for modular type bound!", Mod_Expr);
11361          M_Val := 2 ** System_Max_Binary_Modulus_Power;
11362       else
11363          M_Val := Expr_Value (Mod_Expr);
11364       end if;
11365
11366       if M_Val < 1 then
11367          Error_Msg_N ("modulus value must be positive", Mod_Expr);
11368          M_Val := 2 ** System_Max_Binary_Modulus_Power;
11369       end if;
11370
11371       Set_Modulus (T, M_Val);
11372
11373       --   Create bounds for the modular type based on the modulus given in
11374       --   the type declaration and then analyze and resolve those bounds.
11375
11376       Set_Scalar_Range (T,
11377         Make_Range (Sloc (Mod_Expr),
11378           Low_Bound  =>
11379             Make_Integer_Literal (Sloc (Mod_Expr), 0),
11380           High_Bound =>
11381             Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
11382
11383       --  Properly analyze the literals for the range. We do this manually
11384       --  because we can't go calling Resolve, since we are resolving these
11385       --  bounds with the type, and this type is certainly not complete yet!
11386
11387       Set_Etype (Low_Bound  (Scalar_Range (T)), T);
11388       Set_Etype (High_Bound (Scalar_Range (T)), T);
11389       Set_Is_Static_Expression (Low_Bound  (Scalar_Range (T)));
11390       Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
11391
11392       --  Loop through powers of two to find number of bits required
11393
11394       for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
11395
11396          --  Binary case
11397
11398          if M_Val = 2 ** Bits then
11399             Set_Modular_Size (Bits);
11400             return;
11401
11402          --  Non-binary case
11403
11404          elsif M_Val < 2 ** Bits then
11405             Set_Non_Binary_Modulus (T);
11406
11407             if Bits > System_Max_Nonbinary_Modulus_Power then
11408                Error_Msg_Uint_1 :=
11409                  UI_From_Int (System_Max_Nonbinary_Modulus_Power);
11410                Error_Msg_N
11411                  ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
11412                Set_Modular_Size (System_Max_Binary_Modulus_Power);
11413                return;
11414
11415             else
11416                --  In the non-binary case, set size as per RM 13.3(55).
11417
11418                Set_Modular_Size (Bits);
11419                return;
11420             end if;
11421          end if;
11422
11423       end loop;
11424
11425       --  If we fall through, then the size exceed System.Max_Binary_Modulus
11426       --  so we just signal an error and set the maximum size.
11427
11428       Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
11429       Error_Msg_N ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
11430
11431       Set_Modular_Size (System_Max_Binary_Modulus_Power);
11432       Init_Alignment (T);
11433
11434    end Modular_Type_Declaration;
11435
11436    --------------------------
11437    -- New_Concatenation_Op --
11438    --------------------------
11439
11440    procedure New_Concatenation_Op (Typ : Entity_Id) is
11441       Loc : constant Source_Ptr := Sloc (Typ);
11442       Op  : Entity_Id;
11443
11444       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
11445       --  Create abbreviated declaration for the formal of a predefined
11446       --  Operator 'Op' of type 'Typ'
11447
11448       --------------------
11449       -- Make_Op_Formal --
11450       --------------------
11451
11452       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
11453          Formal : Entity_Id;
11454
11455       begin
11456          Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
11457          Set_Etype (Formal, Typ);
11458          Set_Mechanism (Formal, Default_Mechanism);
11459          return Formal;
11460       end Make_Op_Formal;
11461
11462    --  Start of processing for New_Concatenation_Op
11463
11464    begin
11465       Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
11466
11467       Set_Ekind                   (Op, E_Operator);
11468       Set_Scope                   (Op, Current_Scope);
11469       Set_Etype                   (Op, Typ);
11470       Set_Homonym                 (Op, Get_Name_Entity_Id (Name_Op_Concat));
11471       Set_Is_Immediately_Visible  (Op);
11472       Set_Is_Intrinsic_Subprogram (Op);
11473       Set_Has_Completion          (Op);
11474       Append_Entity               (Op, Current_Scope);
11475
11476       Set_Name_Entity_Id (Name_Op_Concat, Op);
11477
11478       Append_Entity (Make_Op_Formal (Typ, Op), Op);
11479       Append_Entity (Make_Op_Formal (Typ, Op), Op);
11480
11481    end New_Concatenation_Op;
11482
11483    -------------------------------------------
11484    -- Ordinary_Fixed_Point_Type_Declaration --
11485    -------------------------------------------
11486
11487    procedure Ordinary_Fixed_Point_Type_Declaration
11488      (T   : Entity_Id;
11489       Def : Node_Id)
11490    is
11491       Loc           : constant Source_Ptr := Sloc (Def);
11492       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
11493       RRS           : constant Node_Id    := Real_Range_Specification (Def);
11494       Implicit_Base : Entity_Id;
11495       Delta_Val     : Ureal;
11496       Small_Val     : Ureal;
11497       Low_Val       : Ureal;
11498       High_Val      : Ureal;
11499
11500    begin
11501       Check_Restriction (No_Fixed_Point, Def);
11502
11503       --  Create implicit base type
11504
11505       Implicit_Base :=
11506         Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
11507       Set_Etype (Implicit_Base, Implicit_Base);
11508
11509       --  Analyze and process delta expression
11510
11511       Analyze_And_Resolve (Delta_Expr, Any_Real);
11512
11513       Check_Delta_Expression (Delta_Expr);
11514       Delta_Val := Expr_Value_R (Delta_Expr);
11515
11516       Set_Delta_Value (Implicit_Base, Delta_Val);
11517
11518       --  Compute default small from given delta, which is the largest
11519       --  power of two that does not exceed the given delta value.
11520
11521       declare
11522          Tmp   : Ureal := Ureal_1;
11523          Scale : Int   := 0;
11524
11525       begin
11526          if Delta_Val < Ureal_1 then
11527             while Delta_Val < Tmp loop
11528                Tmp := Tmp / Ureal_2;
11529                Scale := Scale + 1;
11530             end loop;
11531
11532          else
11533             loop
11534                Tmp := Tmp * Ureal_2;
11535                exit when Tmp > Delta_Val;
11536                Scale := Scale - 1;
11537             end loop;
11538          end if;
11539
11540          Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
11541       end;
11542
11543       Set_Small_Value (Implicit_Base, Small_Val);
11544
11545       --  If no range was given, set a dummy range
11546
11547       if RRS <= Empty_Or_Error then
11548          Low_Val  := -Small_Val;
11549          High_Val := Small_Val;
11550
11551       --  Otherwise analyze and process given range
11552
11553       else
11554          declare
11555             Low  : constant Node_Id := Low_Bound  (RRS);
11556             High : constant Node_Id := High_Bound (RRS);
11557
11558          begin
11559             Analyze_And_Resolve (Low, Any_Real);
11560             Analyze_And_Resolve (High, Any_Real);
11561             Check_Real_Bound (Low);
11562             Check_Real_Bound (High);
11563
11564             --  Obtain and set the range
11565
11566             Low_Val  := Expr_Value_R (Low);
11567             High_Val := Expr_Value_R (High);
11568
11569             if Low_Val > High_Val then
11570                Error_Msg_NE ("?fixed point type& has null range", Def, T);
11571             end if;
11572          end;
11573       end if;
11574
11575       --  The range for both the implicit base and the declared first
11576       --  subtype cannot be set yet, so we use the special routine
11577       --  Set_Fixed_Range to set a temporary range in place. Note that
11578       --  the bounds of the base type will be widened to be symmetrical
11579       --  and to fill the available bits when the type is frozen.
11580
11581       --  We could do this with all discrete types, and probably should, but
11582       --  we absolutely have to do it for fixed-point, since the end-points
11583       --  of the range and the size are determined by the small value, which
11584       --  could be reset before the freeze point.
11585
11586       Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
11587       Set_Fixed_Range (T, Loc, Low_Val, High_Val);
11588
11589       Init_Size_Align (Implicit_Base);
11590
11591       --  Complete definition of first subtype
11592
11593       Set_Ekind          (T, E_Ordinary_Fixed_Point_Subtype);
11594       Set_Etype          (T, Implicit_Base);
11595       Init_Size_Align    (T);
11596       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
11597       Set_Small_Value    (T, Small_Val);
11598       Set_Delta_Value    (T, Delta_Val);
11599       Set_Is_Constrained (T);
11600
11601    end Ordinary_Fixed_Point_Type_Declaration;
11602
11603    ----------------------------------------
11604    -- Prepare_Private_Subtype_Completion --
11605    ----------------------------------------
11606
11607    procedure Prepare_Private_Subtype_Completion
11608      (Id          : Entity_Id;
11609       Related_Nod : Node_Id)
11610    is
11611       Id_B   : constant Entity_Id := Base_Type (Id);
11612       Full_B : constant Entity_Id := Full_View (Id_B);
11613       Full   : Entity_Id;
11614
11615    begin
11616       if Present (Full_B) then
11617
11618          --  The Base_Type is already completed, we can complete the
11619          --  subtype now. We have to create a new entity with the same name,
11620          --  Thus we can't use Create_Itype.
11621          --  This is messy, should be fixed ???
11622
11623          Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
11624          Set_Is_Itype (Full);
11625          Set_Associated_Node_For_Itype (Full, Related_Nod);
11626          Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
11627       end if;
11628
11629       --  The parent subtype may be private, but the base might not, in some
11630       --  nested instances. In that case, the subtype does not need to be
11631       --  exchanged. It would still be nice to make private subtypes and their
11632       --  bases consistent at all times ???
11633
11634       if Is_Private_Type (Id_B) then
11635          Append_Elmt (Id, Private_Dependents (Id_B));
11636       end if;
11637
11638    end Prepare_Private_Subtype_Completion;
11639
11640    ---------------------------
11641    -- Process_Discriminants --
11642    ---------------------------
11643
11644    procedure Process_Discriminants
11645      (N    : Node_Id;
11646       Prev : Entity_Id := Empty)
11647    is
11648       Elist               : constant Elist_Id := New_Elmt_List;
11649       Id                  : Node_Id;
11650       Discr               : Node_Id;
11651       Discr_Number        : Uint;
11652       Discr_Type          : Entity_Id;
11653       Default_Present     : Boolean := False;
11654       Default_Not_Present : Boolean := False;
11655
11656    begin
11657       --  A composite type other than an array type can have discriminants.
11658       --  Discriminants of non-limited types must have a discrete type.
11659       --  On entry, the current scope is the composite type.
11660
11661       --  The discriminants are initially entered into the scope of the type
11662       --  via Enter_Name with the default Ekind of E_Void to prevent premature
11663       --  use, as explained at the end of this procedure.
11664
11665       Discr := First (Discriminant_Specifications (N));
11666       while Present (Discr) loop
11667          Enter_Name (Defining_Identifier (Discr));
11668
11669          --  For navigation purposes we add a reference to the discriminant
11670          --  in the entity for the type. If the current declaration is a
11671          --  completion, place references on the partial view. Otherwise the
11672          --  type is the current scope.
11673
11674          if Present (Prev) then
11675
11676             --  The references go on the partial view, if present. If the
11677             --  partial view has discriminants, the references have been
11678             --  generated already.
11679
11680             if not Has_Discriminants (Prev) then
11681                Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
11682             end if;
11683          else
11684             Generate_Reference
11685               (Current_Scope, Defining_Identifier (Discr), 'd');
11686          end if;
11687
11688          if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
11689             Discr_Type := Access_Definition (N, Discriminant_Type (Discr));
11690
11691             --  Ada 2005 (AI-254)
11692
11693             if Present (Access_To_Subprogram_Definition
11694                          (Discriminant_Type (Discr)))
11695               and then Protected_Present (Access_To_Subprogram_Definition
11696                                            (Discriminant_Type (Discr)))
11697             then
11698                Discr_Type :=
11699                  Replace_Anonymous_Access_To_Protected_Subprogram
11700                    (Discr, Discr_Type);
11701             end if;
11702
11703          else
11704             Find_Type (Discriminant_Type (Discr));
11705             Discr_Type := Etype (Discriminant_Type (Discr));
11706
11707             if Error_Posted (Discriminant_Type (Discr)) then
11708                Discr_Type := Any_Type;
11709             end if;
11710          end if;
11711
11712          if Is_Access_Type (Discr_Type) then
11713
11714             --  Ada 2005 (AI-230): Access discriminant allowed in non-limited
11715             --  record types
11716
11717             if Ada_Version < Ada_05 then
11718                Check_Access_Discriminant_Requires_Limited
11719                  (Discr, Discriminant_Type (Discr));
11720             end if;
11721
11722             if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
11723                Error_Msg_N
11724                  ("(Ada 83) access discriminant not allowed", Discr);
11725             end if;
11726
11727          elsif not Is_Discrete_Type (Discr_Type) then
11728             Error_Msg_N ("discriminants must have a discrete or access type",
11729               Discriminant_Type (Discr));
11730          end if;
11731
11732          Set_Etype (Defining_Identifier (Discr), Discr_Type);
11733
11734          --  If a discriminant specification includes the assignment compound
11735          --  delimiter followed by an expression, the expression is the default
11736          --  expression of the discriminant; the default expression must be of
11737          --  the type of the discriminant. (RM 3.7.1) Since this expression is
11738          --  a default expression, we do the special preanalysis, since this
11739          --  expression does not freeze (see "Handling of Default and Per-
11740          --  Object Expressions" in spec of package Sem).
11741
11742          if Present (Expression (Discr)) then
11743             Analyze_Per_Use_Expression (Expression (Discr), Discr_Type);
11744
11745             if Nkind (N) = N_Formal_Type_Declaration then
11746                Error_Msg_N
11747                  ("discriminant defaults not allowed for formal type",
11748                   Expression (Discr));
11749
11750             --  Tagged types cannot have defaulted discriminants, but a
11751             --  non-tagged private type with defaulted discriminants
11752             --   can have a tagged completion.
11753
11754             elsif Is_Tagged_Type (Current_Scope)
11755               and then Comes_From_Source (N)
11756             then
11757                Error_Msg_N
11758                  ("discriminants of tagged type cannot have defaults",
11759                   Expression (Discr));
11760
11761             else
11762                Default_Present := True;
11763                Append_Elmt (Expression (Discr), Elist);
11764
11765                --  Tag the defining identifiers for the discriminants with
11766                --  their corresponding default expressions from the tree.
11767
11768                Set_Discriminant_Default_Value
11769                  (Defining_Identifier (Discr), Expression (Discr));
11770             end if;
11771
11772          else
11773             Default_Not_Present := True;
11774          end if;
11775
11776          --  Ada 2005 (AI-231): Set the null-excluding attribute and carry
11777          --  out some static checks.
11778
11779          if Ada_Version >= Ada_05
11780            and then (Null_Exclusion_Present (Discr)
11781                        or else Can_Never_Be_Null (Discr_Type))
11782          then
11783             Set_Can_Never_Be_Null (Defining_Identifier (Discr));
11784             Null_Exclusion_Static_Checks (Discr);
11785          end if;
11786
11787          Next (Discr);
11788       end loop;
11789
11790       --  An element list consisting of the default expressions of the
11791       --  discriminants is constructed in the above loop and used to set
11792       --  the Discriminant_Constraint attribute for the type. If an object
11793       --  is declared of this (record or task) type without any explicit
11794       --  discriminant constraint given, this element list will form the
11795       --  actual parameters for the corresponding initialization procedure
11796       --  for the type.
11797
11798       Set_Discriminant_Constraint (Current_Scope, Elist);
11799       Set_Stored_Constraint (Current_Scope, No_Elist);
11800
11801       --  Default expressions must be provided either for all or for none
11802       --  of the discriminants of a discriminant part. (RM 3.7.1)
11803
11804       if Default_Present and then Default_Not_Present then
11805          Error_Msg_N
11806            ("incomplete specification of defaults for discriminants", N);
11807       end if;
11808
11809       --  The use of the name of a discriminant is not allowed in default
11810       --  expressions of a discriminant part if the specification of the
11811       --  discriminant is itself given in the discriminant part. (RM 3.7.1)
11812
11813       --  To detect this, the discriminant names are entered initially with an
11814       --  Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
11815       --  attempt to use a void entity (for example in an expression that is
11816       --  type-checked) produces the error message: premature usage. Now after
11817       --  completing the semantic analysis of the discriminant part, we can set
11818       --  the Ekind of all the discriminants appropriately.
11819
11820       Discr := First (Discriminant_Specifications (N));
11821       Discr_Number := Uint_1;
11822
11823       while Present (Discr) loop
11824          Id := Defining_Identifier (Discr);
11825          Set_Ekind (Id, E_Discriminant);
11826          Init_Component_Location (Id);
11827          Init_Esize (Id);
11828          Set_Discriminant_Number (Id, Discr_Number);
11829
11830          --  Make sure this is always set, even in illegal programs
11831
11832          Set_Corresponding_Discriminant (Id, Empty);
11833
11834          --  Initialize the Original_Record_Component to the entity itself.
11835          --  Inherit_Components will propagate the right value to
11836          --  discriminants in derived record types.
11837
11838          Set_Original_Record_Component (Id, Id);
11839
11840          --  Create the discriminal for the discriminant.
11841
11842          Build_Discriminal (Id);
11843
11844          Next (Discr);
11845          Discr_Number := Discr_Number + 1;
11846       end loop;
11847
11848       Set_Has_Discriminants (Current_Scope);
11849    end Process_Discriminants;
11850
11851    -----------------------
11852    -- Process_Full_View --
11853    -----------------------
11854
11855    procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
11856       Priv_Parent : Entity_Id;
11857       Full_Parent : Entity_Id;
11858       Full_Indic  : Node_Id;
11859
11860    begin
11861       --  First some sanity checks that must be done after semantic
11862       --  decoration of the full view and thus cannot be placed with other
11863       --  similar checks in Find_Type_Name
11864
11865       if not Is_Limited_Type (Priv_T)
11866         and then (Is_Limited_Type (Full_T)
11867                    or else Is_Limited_Composite (Full_T))
11868       then
11869          Error_Msg_N
11870            ("completion of nonlimited type cannot be limited", Full_T);
11871          Explain_Limited_Type (Full_T, Full_T);
11872
11873       elsif Is_Abstract (Full_T) and then not Is_Abstract (Priv_T) then
11874          Error_Msg_N
11875            ("completion of nonabstract type cannot be abstract", Full_T);
11876
11877       elsif Is_Tagged_Type (Priv_T)
11878         and then Is_Limited_Type (Priv_T)
11879         and then not Is_Limited_Type (Full_T)
11880       then
11881          --  GNAT allow its own definition of Limited_Controlled to disobey
11882          --  this rule in order in ease the implementation. The next test is
11883          --  safe because Root_Controlled is defined in a private system child
11884
11885          if Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
11886             Set_Is_Limited_Composite (Full_T);
11887          else
11888             Error_Msg_N
11889               ("completion of limited tagged type must be limited", Full_T);
11890          end if;
11891
11892       elsif Is_Generic_Type (Priv_T) then
11893          Error_Msg_N ("generic type cannot have a completion", Full_T);
11894       end if;
11895
11896       if Is_Tagged_Type (Priv_T)
11897         and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
11898         and then Is_Derived_Type (Full_T)
11899       then
11900          Priv_Parent := Etype (Priv_T);
11901
11902          --  The full view of a private extension may have been transformed
11903          --  into an unconstrained derived type declaration and a subtype
11904          --  declaration (see build_derived_record_type for details).
11905
11906          if Nkind (N) = N_Subtype_Declaration then
11907             Full_Indic  := Subtype_Indication (N);
11908             Full_Parent := Etype (Base_Type (Full_T));
11909          else
11910             Full_Indic  := Subtype_Indication (Type_Definition (N));
11911             Full_Parent := Etype (Full_T);
11912          end if;
11913
11914          --  Check that the parent type of the full type is a descendant of
11915          --  the ancestor subtype given in the private extension. If either
11916          --  entity has an Etype equal to Any_Type then we had some previous
11917          --  error situation [7.3(8)].
11918
11919          if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
11920             return;
11921
11922          elsif not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent) then
11923             Error_Msg_N
11924               ("parent of full type must descend from parent"
11925                   & " of private extension", Full_Indic);
11926
11927          --  Check the rules of 7.3(10): if the private extension inherits
11928          --  known discriminants, then the full type must also inherit those
11929          --  discriminants from the same (ancestor) type, and the parent
11930          --  subtype of the full type must be constrained if and only if
11931          --  the ancestor subtype of the private extension is constrained.
11932
11933          elsif not Present (Discriminant_Specifications (Parent (Priv_T)))
11934            and then not Has_Unknown_Discriminants (Priv_T)
11935            and then Has_Discriminants (Base_Type (Priv_Parent))
11936          then
11937             declare
11938                Priv_Indic  : constant Node_Id :=
11939                                Subtype_Indication (Parent (Priv_T));
11940
11941                Priv_Constr : constant Boolean :=
11942                                Is_Constrained (Priv_Parent)
11943                                  or else
11944                                    Nkind (Priv_Indic) = N_Subtype_Indication
11945                                  or else Is_Constrained (Entity (Priv_Indic));
11946
11947                Full_Constr : constant Boolean :=
11948                                Is_Constrained (Full_Parent)
11949                                  or else
11950                                    Nkind (Full_Indic) = N_Subtype_Indication
11951                                  or else Is_Constrained (Entity (Full_Indic));
11952
11953                Priv_Discr : Entity_Id;
11954                Full_Discr : Entity_Id;
11955
11956             begin
11957                Priv_Discr := First_Discriminant (Priv_Parent);
11958                Full_Discr := First_Discriminant (Full_Parent);
11959
11960                while Present (Priv_Discr) and then Present (Full_Discr) loop
11961                   if Original_Record_Component (Priv_Discr) =
11962                      Original_Record_Component (Full_Discr)
11963                     or else
11964                      Corresponding_Discriminant (Priv_Discr) =
11965                      Corresponding_Discriminant (Full_Discr)
11966                   then
11967                      null;
11968                   else
11969                      exit;
11970                   end if;
11971
11972                   Next_Discriminant (Priv_Discr);
11973                   Next_Discriminant (Full_Discr);
11974                end loop;
11975
11976                if Present (Priv_Discr) or else Present (Full_Discr) then
11977                   Error_Msg_N
11978                     ("full view must inherit discriminants of the parent type"
11979                      & " used in the private extension", Full_Indic);
11980
11981                elsif Priv_Constr and then not Full_Constr then
11982                   Error_Msg_N
11983                     ("parent subtype of full type must be constrained",
11984                      Full_Indic);
11985
11986                elsif Full_Constr and then not Priv_Constr then
11987                   Error_Msg_N
11988                     ("parent subtype of full type must be unconstrained",
11989                      Full_Indic);
11990                end if;
11991             end;
11992
11993          --  Check the rules of 7.3(12): if a partial view has neither known
11994          --  or unknown discriminants, then the full type declaration shall
11995          --  define a definite subtype.
11996
11997          elsif      not Has_Unknown_Discriminants (Priv_T)
11998            and then not Has_Discriminants (Priv_T)
11999            and then not Is_Constrained (Full_T)
12000          then
12001             Error_Msg_N
12002               ("full view must define a constrained type if partial view"
12003                & " has no discriminants", Full_T);
12004          end if;
12005
12006          --  ??????? Do we implement the following properly ?????
12007          --  If the ancestor subtype of a private extension has constrained
12008          --  discriminants, then the parent subtype of the full view shall
12009          --  impose a statically matching constraint on those discriminants
12010          --  [7.3(13)].
12011
12012       else
12013          --  For untagged types, verify that a type without discriminants
12014          --  is not completed with an unconstrained type.
12015
12016          if not Is_Indefinite_Subtype (Priv_T)
12017            and then Is_Indefinite_Subtype (Full_T)
12018          then
12019             Error_Msg_N ("full view of type must be definite subtype", Full_T);
12020          end if;
12021       end if;
12022
12023       --  Create a full declaration for all its subtypes recorded in
12024       --  Private_Dependents and swap them similarly to the base type.
12025       --  These are subtypes that have been define before the full
12026       --  declaration of the private type. We also swap the entry in
12027       --  Private_Dependents list so we can properly restore the
12028       --  private view on exit from the scope.
12029
12030       declare
12031          Priv_Elmt : Elmt_Id;
12032          Priv      : Entity_Id;
12033          Full      : Entity_Id;
12034
12035       begin
12036          Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
12037          while Present (Priv_Elmt) loop
12038             Priv := Node (Priv_Elmt);
12039
12040             if Ekind (Priv) = E_Private_Subtype
12041               or else Ekind (Priv) = E_Limited_Private_Subtype
12042               or else Ekind (Priv) = E_Record_Subtype_With_Private
12043             then
12044                Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
12045                Set_Is_Itype (Full);
12046                Set_Parent (Full, Parent (Priv));
12047                Set_Associated_Node_For_Itype (Full, N);
12048
12049                --  Now we need to complete the private subtype, but since the
12050                --  base type has already been swapped, we must also swap the
12051                --  subtypes (and thus, reverse the arguments in the call to
12052                --  Complete_Private_Subtype).
12053
12054                Copy_And_Swap (Priv, Full);
12055                Complete_Private_Subtype (Full, Priv, Full_T, N);
12056                Replace_Elmt (Priv_Elmt, Full);
12057             end if;
12058
12059             Next_Elmt (Priv_Elmt);
12060          end loop;
12061       end;
12062
12063       --  If the private view was tagged, copy the new Primitive
12064       --  operations from the private view to the full view.
12065
12066       if Is_Tagged_Type (Full_T) then
12067          declare
12068             Priv_List : Elist_Id;
12069             Full_List : constant Elist_Id := Primitive_Operations (Full_T);
12070             P1, P2    : Elmt_Id;
12071             Prim      : Entity_Id;
12072             D_Type    : Entity_Id;
12073
12074          begin
12075             if Is_Tagged_Type (Priv_T) then
12076                Priv_List := Primitive_Operations (Priv_T);
12077
12078                P1 := First_Elmt (Priv_List);
12079                while Present (P1) loop
12080                   Prim := Node (P1);
12081
12082                   --  Transfer explicit primitives, not those inherited from
12083                   --  parent of partial view, which will be re-inherited on
12084                   --  the full view.
12085
12086                   if Comes_From_Source (Prim) then
12087                      P2 := First_Elmt (Full_List);
12088                      while Present (P2) and then Node (P2) /= Prim loop
12089                         Next_Elmt (P2);
12090                      end loop;
12091
12092                      --  If not found, that is a new one
12093
12094                      if No (P2) then
12095                         Append_Elmt (Prim, Full_List);
12096                      end if;
12097                   end if;
12098
12099                   Next_Elmt (P1);
12100                end loop;
12101
12102             else
12103                --  In this case the partial view is untagged, so here we
12104                --  locate all of the earlier primitives that need to be
12105                --  treated as dispatching (those that appear between the
12106                --  two views). Note that these additional operations must
12107                --  all be new operations (any earlier operations that
12108                --  override inherited operations of the full view will
12109                --  already have been inserted in the primitives list and
12110                --  marked as dispatching by Check_Operation_From_Private_View.
12111                --  Note that implicit "/=" operators are excluded from being
12112                --  added to the primitives list since they shouldn't be
12113                --  treated as dispatching (tagged "/=" is handled specially).
12114
12115                Prim := Next_Entity (Full_T);
12116                while Present (Prim) and then Prim /= Priv_T loop
12117                   if Ekind (Prim) = E_Procedure
12118                        or else
12119                      Ekind (Prim) = E_Function
12120                   then
12121
12122                      D_Type := Find_Dispatching_Type (Prim);
12123
12124                      if D_Type = Full_T
12125                        and then (Chars (Prim) /= Name_Op_Ne
12126                                   or else Comes_From_Source (Prim))
12127                      then
12128                         Check_Controlling_Formals (Full_T, Prim);
12129
12130                         if not Is_Dispatching_Operation (Prim) then
12131                            Append_Elmt (Prim, Full_List);
12132                            Set_Is_Dispatching_Operation (Prim, True);
12133                            Set_DT_Position (Prim, No_Uint);
12134                         end if;
12135
12136                      elsif Is_Dispatching_Operation (Prim)
12137                        and then D_Type  /= Full_T
12138                      then
12139
12140                         --  Verify that it is not otherwise controlled by
12141                         --  a formal or a return value ot type T.
12142
12143                         Check_Controlling_Formals (D_Type, Prim);
12144                      end if;
12145                   end if;
12146
12147                   Next_Entity (Prim);
12148                end loop;
12149             end if;
12150
12151             --  For the tagged case, the two views can share the same
12152             --  Primitive Operation list and the same class wide type.
12153             --  Update attributes of the class-wide type which depend on
12154             --  the full declaration.
12155
12156             if Is_Tagged_Type (Priv_T) then
12157                Set_Primitive_Operations (Priv_T, Full_List);
12158                Set_Class_Wide_Type
12159                  (Base_Type (Full_T), Class_Wide_Type (Priv_T));
12160
12161                --  Any other attributes should be propagated to C_W ???
12162
12163                Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
12164
12165             end if;
12166          end;
12167       end if;
12168    end Process_Full_View;
12169
12170    -----------------------------------
12171    -- Process_Incomplete_Dependents --
12172    -----------------------------------
12173
12174    procedure Process_Incomplete_Dependents
12175      (N      : Node_Id;
12176       Full_T : Entity_Id;
12177       Inc_T  : Entity_Id)
12178    is
12179       Inc_Elmt : Elmt_Id;
12180       Priv_Dep : Entity_Id;
12181       New_Subt : Entity_Id;
12182
12183       Disc_Constraint : Elist_Id;
12184
12185    begin
12186       if No (Private_Dependents (Inc_T)) then
12187          return;
12188
12189       else
12190          Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
12191
12192          --  Itypes that may be generated by the completion of an incomplete
12193          --  subtype are not used by the back-end and not attached to the tree.
12194          --  They are created only for constraint-checking purposes.
12195       end if;
12196
12197       while Present (Inc_Elmt) loop
12198          Priv_Dep := Node (Inc_Elmt);
12199
12200          if Ekind (Priv_Dep) = E_Subprogram_Type then
12201
12202             --  An Access_To_Subprogram type may have a return type or a
12203             --  parameter type that is incomplete. Replace with the full view.
12204
12205             if Etype (Priv_Dep) = Inc_T then
12206                Set_Etype (Priv_Dep, Full_T);
12207             end if;
12208
12209             declare
12210                Formal : Entity_Id;
12211
12212             begin
12213                Formal := First_Formal (Priv_Dep);
12214
12215                while Present (Formal) loop
12216
12217                   if Etype (Formal) = Inc_T then
12218                      Set_Etype (Formal, Full_T);
12219                   end if;
12220
12221                   Next_Formal (Formal);
12222                end loop;
12223             end;
12224
12225          elsif  Is_Overloadable (Priv_Dep) then
12226
12227             if Is_Tagged_Type (Full_T) then
12228
12229                --  Subprogram has an access parameter whose designated type
12230                --  was incomplete. Reexamine declaration now, because it may
12231                --  be a primitive operation of the full type.
12232
12233                Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
12234                Set_Is_Dispatching_Operation (Priv_Dep);
12235                Check_Controlling_Formals (Full_T, Priv_Dep);
12236             end if;
12237
12238          elsif Ekind (Priv_Dep) = E_Subprogram_Body then
12239
12240             --  Can happen during processing of a body before the completion
12241             --  of a TA type. Ignore, because spec is also on dependent list.
12242
12243             return;
12244
12245          --  Dependent is a subtype
12246
12247          else
12248             --  We build a new subtype indication using the full view of the
12249             --  incomplete parent. The discriminant constraints have been
12250             --  elaborated already at the point of the subtype declaration.
12251
12252             New_Subt := Create_Itype (E_Void, N);
12253
12254             if Has_Discriminants (Full_T) then
12255                Disc_Constraint := Discriminant_Constraint (Priv_Dep);
12256             else
12257                Disc_Constraint := No_Elist;
12258             end if;
12259
12260             Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
12261             Set_Full_View (Priv_Dep, New_Subt);
12262          end if;
12263
12264          Next_Elmt (Inc_Elmt);
12265       end loop;
12266
12267    end Process_Incomplete_Dependents;
12268
12269    --------------------------------
12270    -- Process_Range_Expr_In_Decl --
12271    --------------------------------
12272
12273    procedure Process_Range_Expr_In_Decl
12274      (R           : Node_Id;
12275       T           : Entity_Id;
12276       Check_List  : List_Id := Empty_List;
12277       R_Check_Off : Boolean := False)
12278    is
12279       Lo, Hi    : Node_Id;
12280       R_Checks  : Check_Result;
12281       Type_Decl : Node_Id;
12282       Def_Id    : Entity_Id;
12283
12284    begin
12285       Analyze_And_Resolve (R, Base_Type (T));
12286
12287       if Nkind (R) = N_Range then
12288          Lo := Low_Bound (R);
12289          Hi := High_Bound (R);
12290
12291          --  If there were errors in the declaration, try and patch up some
12292          --  common mistakes in the bounds. The cases handled are literals
12293          --  which are Integer where the expected type is Real and vice versa.
12294          --  These corrections allow the compilation process to proceed further
12295          --  along since some basic assumptions of the format of the bounds
12296          --  are guaranteed.
12297
12298          if Etype (R) = Any_Type then
12299
12300             if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
12301                Rewrite (Lo,
12302                  Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
12303
12304             elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
12305                Rewrite (Hi,
12306                  Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
12307
12308             elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
12309                Rewrite (Lo,
12310                  Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
12311
12312             elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
12313                Rewrite (Hi,
12314                  Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
12315             end if;
12316
12317             Set_Etype (Lo, T);
12318             Set_Etype (Hi, T);
12319          end if;
12320
12321          --  If the bounds of the range have been mistakenly given as
12322          --  string literals (perhaps in place of character literals),
12323          --  then an error has already been reported, but we rewrite
12324          --  the string literal as a bound of the range's type to
12325          --  avoid blowups in later processing that looks at static
12326          --  values.
12327
12328          if Nkind (Lo) = N_String_Literal then
12329             Rewrite (Lo,
12330               Make_Attribute_Reference (Sloc (Lo),
12331                 Attribute_Name => Name_First,
12332                 Prefix => New_Reference_To (T, Sloc (Lo))));
12333             Analyze_And_Resolve (Lo);
12334          end if;
12335
12336          if Nkind (Hi) = N_String_Literal then
12337             Rewrite (Hi,
12338               Make_Attribute_Reference (Sloc (Hi),
12339                 Attribute_Name => Name_First,
12340                 Prefix => New_Reference_To (T, Sloc (Hi))));
12341             Analyze_And_Resolve (Hi);
12342          end if;
12343
12344          --  If bounds aren't scalar at this point then exit, avoiding
12345          --  problems with further processing of the range in this procedure.
12346
12347          if not Is_Scalar_Type (Etype (Lo)) then
12348             return;
12349          end if;
12350
12351          --  Resolve (actually Sem_Eval) has checked that the bounds are in
12352          --  then range of the base type. Here we check whether the bounds
12353          --  are in the range of the subtype itself. Note that if the bounds
12354          --  represent the null range the Constraint_Error exception should
12355          --  not be raised.
12356
12357          --  ??? The following code should be cleaned up as follows
12358          --  1. The Is_Null_Range (Lo, Hi) test should disappear since it
12359          --     is done in the call to Range_Check (R, T); below
12360          --  2. The use of R_Check_Off should be investigated and possibly
12361          --     removed, this would clean up things a bit.
12362
12363          if Is_Null_Range (Lo, Hi) then
12364             null;
12365
12366          else
12367             --  Capture values of bounds and generate temporaries for them
12368             --  if needed, before applying checks, since checks may cause
12369             --  duplication of the expression without forcing evaluation.
12370
12371             if Expander_Active then
12372                Force_Evaluation (Lo);
12373                Force_Evaluation (Hi);
12374             end if;
12375
12376             --  We use a flag here instead of suppressing checks on the
12377             --  type because the type we check against isn't necessarily
12378             --  the place where we put the check.
12379
12380             if not R_Check_Off then
12381                R_Checks := Range_Check (R, T);
12382                Type_Decl := Parent (R);
12383
12384                --  Look up tree to find an appropriate insertion point.
12385                --  This seems really junk code, and very brittle, couldn't
12386                --  we just use an insert actions call of some kind ???
12387
12388                while Present (Type_Decl) and then not
12389                  (Nkind (Type_Decl) = N_Full_Type_Declaration
12390                     or else
12391                   Nkind (Type_Decl) = N_Subtype_Declaration
12392                     or else
12393                   Nkind (Type_Decl) = N_Loop_Statement
12394                     or else
12395                   Nkind (Type_Decl) = N_Task_Type_Declaration
12396                     or else
12397                   Nkind (Type_Decl) = N_Single_Task_Declaration
12398                     or else
12399                   Nkind (Type_Decl) = N_Protected_Type_Declaration
12400                     or else
12401                   Nkind (Type_Decl) = N_Single_Protected_Declaration)
12402                loop
12403                   Type_Decl := Parent (Type_Decl);
12404                end loop;
12405
12406                --  Why would Type_Decl not be present???  Without this test,
12407                --  short regression tests fail.
12408
12409                if Present (Type_Decl) then
12410
12411                   --  Case of loop statement (more comments ???)
12412
12413                   if Nkind (Type_Decl) = N_Loop_Statement then
12414                      declare
12415                         Indic : Node_Id := Parent (R);
12416
12417                      begin
12418                         while Present (Indic) and then not
12419                           (Nkind (Indic) = N_Subtype_Indication)
12420                         loop
12421                            Indic := Parent (Indic);
12422                         end loop;
12423
12424                         if Present (Indic) then
12425                            Def_Id := Etype (Subtype_Mark (Indic));
12426
12427                            Insert_Range_Checks
12428                              (R_Checks,
12429                               Type_Decl,
12430                               Def_Id,
12431                               Sloc (Type_Decl),
12432                               R,
12433                               Do_Before => True);
12434                         end if;
12435                      end;
12436
12437                   --  All other cases (more comments ???)
12438
12439                   else
12440                      Def_Id := Defining_Identifier (Type_Decl);
12441
12442                      if (Ekind (Def_Id) = E_Record_Type
12443                           and then Depends_On_Discriminant (R))
12444                        or else
12445                         (Ekind (Def_Id) = E_Protected_Type
12446                           and then Has_Discriminants (Def_Id))
12447                      then
12448                         Append_Range_Checks
12449                           (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
12450
12451                      else
12452                         Insert_Range_Checks
12453                           (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
12454
12455                      end if;
12456                   end if;
12457                end if;
12458             end if;
12459          end if;
12460
12461       elsif Expander_Active then
12462          Get_Index_Bounds (R, Lo, Hi);
12463          Force_Evaluation (Lo);
12464          Force_Evaluation (Hi);
12465       end if;
12466    end Process_Range_Expr_In_Decl;
12467
12468    --------------------------------------
12469    -- Process_Real_Range_Specification --
12470    --------------------------------------
12471
12472    procedure Process_Real_Range_Specification (Def : Node_Id) is
12473       Spec : constant Node_Id := Real_Range_Specification (Def);
12474       Lo   : Node_Id;
12475       Hi   : Node_Id;
12476       Err  : Boolean := False;
12477
12478       procedure Analyze_Bound (N : Node_Id);
12479       --  Analyze and check one bound
12480
12481       -------------------
12482       -- Analyze_Bound --
12483       -------------------
12484
12485       procedure Analyze_Bound (N : Node_Id) is
12486       begin
12487          Analyze_And_Resolve (N, Any_Real);
12488
12489          if not Is_OK_Static_Expression (N) then
12490             Flag_Non_Static_Expr
12491               ("bound in real type definition is not static!", N);
12492             Err := True;
12493          end if;
12494       end Analyze_Bound;
12495
12496    --  Start of processing for Process_Real_Range_Specification
12497
12498    begin
12499       if Present (Spec) then
12500          Lo := Low_Bound (Spec);
12501          Hi := High_Bound (Spec);
12502          Analyze_Bound (Lo);
12503          Analyze_Bound (Hi);
12504
12505          --  If error, clear away junk range specification
12506
12507          if Err then
12508             Set_Real_Range_Specification (Def, Empty);
12509          end if;
12510       end if;
12511    end Process_Real_Range_Specification;
12512
12513    ---------------------
12514    -- Process_Subtype --
12515    ---------------------
12516
12517    function Process_Subtype
12518      (S           : Node_Id;
12519       Related_Nod : Node_Id;
12520       Related_Id  : Entity_Id := Empty;
12521       Suffix      : Character := ' ') return Entity_Id
12522    is
12523       P               : Node_Id;
12524       Def_Id          : Entity_Id;
12525       Full_View_Id    : Entity_Id;
12526       Subtype_Mark_Id : Entity_Id;
12527
12528       procedure Check_Incomplete (T : Entity_Id);
12529       --  Called to verify that an incomplete type is not used prematurely
12530
12531       ----------------------
12532       -- Check_Incomplete --
12533       ----------------------
12534
12535       procedure Check_Incomplete (T : Entity_Id) is
12536       begin
12537          if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type then
12538             Error_Msg_N ("invalid use of type before its full declaration", T);
12539          end if;
12540       end Check_Incomplete;
12541
12542    --  Start of processing for Process_Subtype
12543
12544    begin
12545       --  Case of no constraints present
12546
12547       if Nkind (S) /= N_Subtype_Indication then
12548
12549          Find_Type (S);
12550          Check_Incomplete (S);
12551
12552          --  Ada 2005 (AI-231): Static check
12553
12554          if Ada_Version >= Ada_05
12555            and then Present (Parent (S))
12556            and then Null_Exclusion_Present (Parent (S))
12557            and then Nkind (Parent (S)) /= N_Access_To_Object_Definition
12558            and then not Is_Access_Type (Entity (S))
12559          then
12560             Error_Msg_N
12561               ("(Ada 2005) null-exclusion part requires an access type", S);
12562          end if;
12563          return Entity (S);
12564
12565       --  Case of constraint present, so that we have an N_Subtype_Indication
12566       --  node (this node is created only if constraints are present).
12567
12568       else
12569
12570          Find_Type (Subtype_Mark (S));
12571
12572          if Nkind (Parent (S)) /= N_Access_To_Object_Definition
12573            and then not
12574             (Nkind (Parent (S)) = N_Subtype_Declaration
12575               and then
12576              Is_Itype (Defining_Identifier (Parent (S))))
12577          then
12578             Check_Incomplete (Subtype_Mark (S));
12579          end if;
12580
12581          P := Parent (S);
12582          Subtype_Mark_Id := Entity (Subtype_Mark (S));
12583
12584          if Is_Unchecked_Union (Subtype_Mark_Id)
12585            and then Comes_From_Source (Related_Nod)
12586          then
12587             Error_Msg_N
12588               ("cannot create subtype of Unchecked_Union", Related_Nod);
12589          end if;
12590
12591          --  Explicit subtype declaration case
12592
12593          if Nkind (P) = N_Subtype_Declaration then
12594             Def_Id := Defining_Identifier (P);
12595
12596          --  Explicit derived type definition case
12597
12598          elsif Nkind (P) = N_Derived_Type_Definition then
12599             Def_Id := Defining_Identifier (Parent (P));
12600
12601          --  Implicit case, the Def_Id must be created as an implicit type.
12602          --  The one exception arises in the case of concurrent types,
12603          --  array and access types, where other subsidiary implicit types
12604          --  may be created and must appear before the main implicit type.
12605          --  In these cases we leave Def_Id set to Empty as a signal that
12606          --  Create_Itype has not yet been called to create Def_Id.
12607
12608          else
12609             if Is_Array_Type (Subtype_Mark_Id)
12610               or else Is_Concurrent_Type (Subtype_Mark_Id)
12611               or else Is_Access_Type (Subtype_Mark_Id)
12612             then
12613                Def_Id := Empty;
12614
12615             --  For the other cases, we create a new unattached Itype,
12616             --  and set the indication to ensure it gets attached later.
12617
12618             else
12619                Def_Id :=
12620                  Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12621             end if;
12622          end if;
12623
12624          --  If the kind of constraint is invalid for this kind of type,
12625          --  then give an error, and then pretend no constraint was given.
12626
12627          if not Is_Valid_Constraint_Kind
12628                    (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
12629          then
12630             Error_Msg_N
12631               ("incorrect constraint for this kind of type", Constraint (S));
12632
12633             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
12634
12635             --  Make recursive call, having got rid of the bogus constraint
12636
12637             return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
12638          end if;
12639
12640          --  Remaining processing depends on type
12641
12642          case Ekind (Subtype_Mark_Id) is
12643
12644             when Access_Kind =>
12645                Constrain_Access (Def_Id, S, Related_Nod);
12646
12647             when Array_Kind =>
12648                Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
12649
12650             when Decimal_Fixed_Point_Kind =>
12651                Constrain_Decimal (Def_Id, S);
12652
12653             when Enumeration_Kind =>
12654                Constrain_Enumeration (Def_Id, S);
12655
12656             when Ordinary_Fixed_Point_Kind =>
12657                Constrain_Ordinary_Fixed (Def_Id, S);
12658
12659             when Float_Kind =>
12660                Constrain_Float (Def_Id, S);
12661
12662             when Integer_Kind =>
12663                Constrain_Integer (Def_Id, S);
12664
12665             when E_Record_Type     |
12666                  E_Record_Subtype  |
12667                  Class_Wide_Kind   |
12668                  E_Incomplete_Type =>
12669                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
12670
12671             when Private_Kind =>
12672                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
12673                Set_Private_Dependents (Def_Id, New_Elmt_List);
12674
12675                --  In case of an invalid constraint prevent further processing
12676                --  since the type constructed is missing expected fields.
12677
12678                if Etype (Def_Id) = Any_Type then
12679                   return Def_Id;
12680                end if;
12681
12682                --  If the full view is that of a task with discriminants,
12683                --  we must constrain both the concurrent type and its
12684                --  corresponding record type. Otherwise we will just propagate
12685                --  the constraint to the full view, if available.
12686
12687                if Present (Full_View (Subtype_Mark_Id))
12688                  and then Has_Discriminants (Subtype_Mark_Id)
12689                  and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
12690                then
12691                   Full_View_Id :=
12692                     Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12693
12694                   Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
12695                   Constrain_Concurrent (Full_View_Id, S,
12696                     Related_Nod, Related_Id, Suffix);
12697                   Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
12698                   Set_Full_View (Def_Id, Full_View_Id);
12699
12700                else
12701                   Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
12702                end if;
12703
12704             when Concurrent_Kind  =>
12705                Constrain_Concurrent (Def_Id, S,
12706                  Related_Nod, Related_Id, Suffix);
12707
12708             when others =>
12709                Error_Msg_N ("invalid subtype mark in subtype indication", S);
12710          end case;
12711
12712          --  Size and Convention are always inherited from the base type
12713
12714          Set_Size_Info  (Def_Id,            (Subtype_Mark_Id));
12715          Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
12716
12717          return Def_Id;
12718
12719       end if;
12720    end Process_Subtype;
12721
12722    -----------------------------
12723    -- Record_Type_Declaration --
12724    -----------------------------
12725
12726    procedure Record_Type_Declaration
12727      (T    : Entity_Id;
12728       N    : Node_Id;
12729       Prev : Entity_Id)
12730    is
12731       Def : constant Node_Id := Type_Definition (N);
12732
12733       Is_Tagged : Boolean;
12734       Tag_Comp  : Entity_Id;
12735
12736    begin
12737       --  The flag Is_Tagged_Type might have already been set by Find_Type_Name
12738       --  if it detected an error for declaration T. This arises in the case of
12739       --  private tagged types where the full view omits the word tagged.
12740
12741       Is_Tagged := Tagged_Present (Def)
12742         or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
12743
12744       --  Records constitute a scope for the component declarations within.
12745       --  The scope is created prior to the processing of these declarations.
12746       --  Discriminants are processed first, so that they are visible when
12747       --  processing the other components. The Ekind of the record type itself
12748       --  is set to E_Record_Type (subtypes appear as E_Record_Subtype).
12749
12750       --  Enter record scope
12751
12752       New_Scope (T);
12753
12754       --  These flags must be initialized before calling Process_Discriminants
12755       --  because this routine makes use of them.
12756
12757       Set_Is_Tagged_Type     (T, Is_Tagged);
12758       Set_Is_Limited_Record  (T, Limited_Present (Def));
12759
12760       --  Type is abstract if full declaration carries keyword, or if
12761       --  previous partial view did.
12762
12763       Set_Is_Abstract (T, Is_Abstract (T) or else Abstract_Present (Def));
12764
12765       Set_Ekind       (T, E_Record_Type);
12766       Set_Etype       (T, T);
12767       Init_Size_Align (T);
12768
12769       Set_Stored_Constraint (T, No_Elist);
12770
12771       --  If an incomplete or private type declaration was already given for
12772       --  the type, then this scope already exists, and the discriminants have
12773       --  been declared within. We must verify that the full declaration
12774       --  matches the incomplete one.
12775
12776       Check_Or_Process_Discriminants (N, T, Prev);
12777
12778       Set_Is_Constrained     (T, not Has_Discriminants (T));
12779       Set_Has_Delayed_Freeze (T, True);
12780
12781       --  For tagged types add a manually analyzed component corresponding
12782       --  to the component _tag, the corresponding piece of tree will be
12783       --  expanded as part of the freezing actions if it is not a CPP_Class.
12784
12785       if Is_Tagged then
12786          --  Do not add the tag unless we are in expansion mode.
12787
12788          if Expander_Active then
12789             Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
12790             Enter_Name (Tag_Comp);
12791
12792             Set_Is_Tag                    (Tag_Comp);
12793             Set_Ekind                     (Tag_Comp, E_Component);
12794             Set_Etype                     (Tag_Comp, RTE (RE_Tag));
12795             Set_DT_Entry_Count            (Tag_Comp, No_Uint);
12796             Set_Original_Record_Component (Tag_Comp, Tag_Comp);
12797             Init_Component_Location       (Tag_Comp);
12798          end if;
12799
12800          Make_Class_Wide_Type (T);
12801          Set_Primitive_Operations (T, New_Elmt_List);
12802       end if;
12803
12804       --  We must suppress range checks when processing the components
12805       --  of a record in the presence of discriminants, since we don't
12806       --  want spurious checks to be generated during their analysis, but
12807       --  must reset the Suppress_Range_Checks flags after having processed
12808       --  the record definition.
12809
12810       if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
12811          Set_Kill_Range_Checks (T, True);
12812          Record_Type_Definition (Def, Prev);
12813          Set_Kill_Range_Checks (T, False);
12814       else
12815          Record_Type_Definition (Def, Prev);
12816       end if;
12817
12818       --  Exit from record scope
12819
12820       End_Scope;
12821    end Record_Type_Declaration;
12822
12823    ----------------------------
12824    -- Record_Type_Definition --
12825    ----------------------------
12826
12827    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
12828       Component          : Entity_Id;
12829       Ctrl_Components    : Boolean := False;
12830       Final_Storage_Only : Boolean;
12831       T                  : Entity_Id;
12832
12833    begin
12834       if Ekind (Prev_T) = E_Incomplete_Type then
12835          T := Full_View (Prev_T);
12836       else
12837          T := Prev_T;
12838       end if;
12839
12840       Final_Storage_Only := not Is_Controlled (T);
12841
12842       --  If the component list of a record type is defined by the reserved
12843       --  word null and there is no discriminant part, then the record type has
12844       --  no components and all records of the type are null records (RM 3.7)
12845       --  This procedure is also called to process the extension part of a
12846       --  record extension, in which case the current scope may have inherited
12847       --  components.
12848
12849       if No (Def)
12850         or else No (Component_List (Def))
12851         or else Null_Present (Component_List (Def))
12852       then
12853          null;
12854
12855       else
12856          Analyze_Declarations (Component_Items (Component_List (Def)));
12857
12858          if Present (Variant_Part (Component_List (Def))) then
12859             Analyze (Variant_Part (Component_List (Def)));
12860          end if;
12861       end if;
12862
12863       --  After completing the semantic analysis of the record definition,
12864       --  record components, both new and inherited, are accessible. Set
12865       --  their kind accordingly.
12866
12867       Component := First_Entity (Current_Scope);
12868       while Present (Component) loop
12869
12870          if Ekind (Component) = E_Void then
12871             Set_Ekind (Component, E_Component);
12872             Init_Component_Location (Component);
12873          end if;
12874
12875          if Has_Task (Etype (Component)) then
12876             Set_Has_Task (T);
12877          end if;
12878
12879          if Ekind (Component) /= E_Component then
12880             null;
12881
12882          elsif Has_Controlled_Component (Etype (Component))
12883            or else (Chars (Component) /= Name_uParent
12884                     and then Is_Controlled (Etype (Component)))
12885          then
12886             Set_Has_Controlled_Component (T, True);
12887             Final_Storage_Only := Final_Storage_Only
12888               and then Finalize_Storage_Only (Etype (Component));
12889             Ctrl_Components := True;
12890          end if;
12891
12892          Next_Entity (Component);
12893       end loop;
12894
12895       --  A type is Finalize_Storage_Only only if all its controlled
12896       --  components are so.
12897
12898       if Ctrl_Components then
12899          Set_Finalize_Storage_Only (T, Final_Storage_Only);
12900       end if;
12901
12902       --  Place reference to end record on the proper entity, which may
12903       --  be a partial view.
12904
12905       if Present (Def) then
12906          Process_End_Label (Def, 'e', Prev_T);
12907       end if;
12908    end Record_Type_Definition;
12909
12910    ------------------------
12911    -- Replace_Components --
12912    ------------------------
12913
12914    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
12915       function Process (N : Node_Id) return Traverse_Result;
12916
12917       -------------
12918       -- Process --
12919       -------------
12920
12921       function Process (N : Node_Id) return Traverse_Result is
12922          Comp : Entity_Id;
12923
12924       begin
12925          if Nkind (N) = N_Discriminant_Specification then
12926             Comp := First_Discriminant (Typ);
12927
12928             while Present (Comp) loop
12929                if Chars (Comp) = Chars (Defining_Identifier (N)) then
12930                   Set_Defining_Identifier (N, Comp);
12931                   exit;
12932                end if;
12933
12934                Next_Discriminant (Comp);
12935             end loop;
12936
12937          elsif Nkind (N) = N_Component_Declaration then
12938             Comp := First_Component (Typ);
12939
12940             while Present (Comp) loop
12941                if Chars (Comp) = Chars (Defining_Identifier (N)) then
12942                   Set_Defining_Identifier (N, Comp);
12943                   exit;
12944                end if;
12945
12946                Next_Component (Comp);
12947             end loop;
12948          end if;
12949
12950          return OK;
12951       end Process;
12952
12953       procedure Replace is new Traverse_Proc (Process);
12954
12955    --  Start of processing for Replace_Components
12956
12957    begin
12958       Replace (Decl);
12959    end Replace_Components;
12960
12961    -------------------------------
12962    -- Set_Completion_Referenced --
12963    -------------------------------
12964
12965    procedure Set_Completion_Referenced (E : Entity_Id) is
12966    begin
12967       --  If in main unit, mark entity that is a completion as referenced,
12968       --  warnings go on the partial view when needed.
12969
12970       if In_Extended_Main_Source_Unit (E) then
12971          Set_Referenced (E);
12972       end if;
12973    end Set_Completion_Referenced;
12974
12975    ---------------------
12976    -- Set_Fixed_Range --
12977    ---------------------
12978
12979    --  The range for fixed-point types is complicated by the fact that we
12980    --  do not know the exact end points at the time of the declaration. This
12981    --  is true for three reasons:
12982
12983    --     A size clause may affect the fudging of the end-points
12984    --     A small clause may affect the values of the end-points
12985    --     We try to include the end-points if it does not affect the size
12986
12987    --  This means that the actual end-points must be established at the
12988    --  point when the type is frozen. Meanwhile, we first narrow the range
12989    --  as permitted (so that it will fit if necessary in a small specified
12990    --  size), and then build a range subtree with these narrowed bounds.
12991
12992    --  Set_Fixed_Range constructs the range from real literal values, and
12993    --  sets the range as the Scalar_Range of the given fixed-point type
12994    --  entity.
12995
12996    --  The parent of this range is set to point to the entity so that it
12997    --  is properly hooked into the tree (unlike normal Scalar_Range entries
12998    --  for other scalar types, which are just pointers to the range in the
12999    --  original tree, this would otherwise be an orphan).
13000
13001    --  The tree is left unanalyzed. When the type is frozen, the processing
13002    --  in Freeze.Freeze_Fixed_Point_Type notices that the range is not
13003    --  analyzed, and uses this as an indication that it should complete
13004    --  work on the range (it will know the final small and size values).
13005
13006    procedure Set_Fixed_Range
13007      (E   : Entity_Id;
13008       Loc : Source_Ptr;
13009       Lo  : Ureal;
13010       Hi  : Ureal)
13011    is
13012       S : constant Node_Id :=
13013             Make_Range (Loc,
13014               Low_Bound  => Make_Real_Literal (Loc, Lo),
13015               High_Bound => Make_Real_Literal (Loc, Hi));
13016
13017    begin
13018       Set_Scalar_Range (E, S);
13019       Set_Parent (S, E);
13020    end Set_Fixed_Range;
13021
13022    ----------------------------------
13023    -- Set_Scalar_Range_For_Subtype --
13024    ----------------------------------
13025
13026    procedure Set_Scalar_Range_For_Subtype
13027      (Def_Id : Entity_Id;
13028       R      : Node_Id;
13029       Subt   : Entity_Id)
13030    is
13031       Kind : constant Entity_Kind :=  Ekind (Def_Id);
13032    begin
13033       Set_Scalar_Range (Def_Id, R);
13034
13035       --  We need to link the range into the tree before resolving it so
13036       --  that types that are referenced, including importantly the subtype
13037       --  itself, are properly frozen (Freeze_Expression requires that the
13038       --  expression be properly linked into the tree). Of course if it is
13039       --  already linked in, then we do not disturb the current link.
13040
13041       if No (Parent (R)) then
13042          Set_Parent (R, Def_Id);
13043       end if;
13044
13045       --  Reset the kind of the subtype during analysis of the range, to
13046       --  catch possible premature use in the bounds themselves.
13047
13048       Set_Ekind (Def_Id, E_Void);
13049       Process_Range_Expr_In_Decl (R, Subt);
13050       Set_Ekind (Def_Id, Kind);
13051
13052    end Set_Scalar_Range_For_Subtype;
13053
13054    --------------------------------------------------------
13055    -- Set_Stored_Constraint_From_Discriminant_Constraint --
13056    --------------------------------------------------------
13057
13058    procedure Set_Stored_Constraint_From_Discriminant_Constraint
13059      (E : Entity_Id)
13060    is
13061    begin
13062       --  Make sure set if encountered during
13063       --  Expand_To_Stored_Constraint
13064
13065       Set_Stored_Constraint (E, No_Elist);
13066
13067       --  Give it the right value
13068
13069       if Is_Constrained (E) and then Has_Discriminants (E) then
13070          Set_Stored_Constraint (E,
13071            Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
13072       end if;
13073
13074    end Set_Stored_Constraint_From_Discriminant_Constraint;
13075
13076    -------------------------------------
13077    -- Signed_Integer_Type_Declaration --
13078    -------------------------------------
13079
13080    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13081       Implicit_Base : Entity_Id;
13082       Base_Typ      : Entity_Id;
13083       Lo_Val        : Uint;
13084       Hi_Val        : Uint;
13085       Errs          : Boolean := False;
13086       Lo            : Node_Id;
13087       Hi            : Node_Id;
13088
13089       function Can_Derive_From (E : Entity_Id) return Boolean;
13090       --  Determine whether given bounds allow derivation from specified type
13091
13092       procedure Check_Bound (Expr : Node_Id);
13093       --  Check bound to make sure it is integral and static. If not, post
13094       --  appropriate error message and set Errs flag
13095
13096       ---------------------
13097       -- Can_Derive_From --
13098       ---------------------
13099
13100       function Can_Derive_From (E : Entity_Id) return Boolean is
13101          Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
13102          Hi : constant Uint := Expr_Value (Type_High_Bound (E));
13103
13104       begin
13105          --  Note we check both bounds against both end values, to deal with
13106          --  strange types like ones with a range of 0 .. -12341234.
13107
13108          return Lo <= Lo_Val and then Lo_Val <= Hi
13109                   and then
13110                 Lo <= Hi_Val and then Hi_Val <= Hi;
13111       end Can_Derive_From;
13112
13113       -----------------
13114       -- Check_Bound --
13115       -----------------
13116
13117       procedure Check_Bound (Expr : Node_Id) is
13118       begin
13119          --  If a range constraint is used as an integer type definition, each
13120          --  bound of the range must be defined by a static expression of some
13121          --  integer type, but the two bounds need not have the same integer
13122          --  type (Negative bounds are allowed.) (RM 3.5.4)
13123
13124          if not Is_Integer_Type (Etype (Expr)) then
13125             Error_Msg_N
13126               ("integer type definition bounds must be of integer type", Expr);
13127             Errs := True;
13128
13129          elsif not Is_OK_Static_Expression (Expr) then
13130             Flag_Non_Static_Expr
13131               ("non-static expression used for integer type bound!", Expr);
13132             Errs := True;
13133
13134          --  The bounds are folded into literals, and we set their type to be
13135          --  universal, to avoid typing difficulties: we cannot set the type
13136          --  of the literal to the new type, because this would be a forward
13137          --  reference for the back end,  and if the original type is user-
13138          --  defined this can lead to spurious semantic errors (e.g. 2928-003).
13139
13140          else
13141             if Is_Entity_Name (Expr) then
13142                Fold_Uint (Expr, Expr_Value (Expr), True);
13143             end if;
13144
13145             Set_Etype (Expr, Universal_Integer);
13146          end if;
13147       end Check_Bound;
13148
13149    --  Start of processing for Signed_Integer_Type_Declaration
13150
13151    begin
13152       --  Create an anonymous base type
13153
13154       Implicit_Base :=
13155         Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
13156
13157       --  Analyze and check the bounds, they can be of any integer type
13158
13159       Lo := Low_Bound (Def);
13160       Hi := High_Bound (Def);
13161
13162       --  Arbitrarily use Integer as the type if either bound had an error
13163
13164       if Hi = Error or else Lo = Error then
13165          Base_Typ := Any_Integer;
13166          Set_Error_Posted (T, True);
13167
13168       --  Here both bounds are OK expressions
13169
13170       else
13171          Analyze_And_Resolve (Lo, Any_Integer);
13172          Analyze_And_Resolve (Hi, Any_Integer);
13173
13174          Check_Bound (Lo);
13175          Check_Bound (Hi);
13176
13177          if Errs then
13178             Hi := Type_High_Bound (Standard_Long_Long_Integer);
13179             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
13180          end if;
13181
13182          --  Find type to derive from
13183
13184          Lo_Val := Expr_Value (Lo);
13185          Hi_Val := Expr_Value (Hi);
13186
13187          if Can_Derive_From (Standard_Short_Short_Integer) then
13188             Base_Typ := Base_Type (Standard_Short_Short_Integer);
13189
13190          elsif Can_Derive_From (Standard_Short_Integer) then
13191             Base_Typ := Base_Type (Standard_Short_Integer);
13192
13193          elsif Can_Derive_From (Standard_Integer) then
13194             Base_Typ := Base_Type (Standard_Integer);
13195
13196          elsif Can_Derive_From (Standard_Long_Integer) then
13197             Base_Typ := Base_Type (Standard_Long_Integer);
13198
13199          elsif Can_Derive_From (Standard_Long_Long_Integer) then
13200             Base_Typ := Base_Type (Standard_Long_Long_Integer);
13201
13202          else
13203             Base_Typ := Base_Type (Standard_Long_Long_Integer);
13204             Error_Msg_N ("integer type definition bounds out of range", Def);
13205             Hi := Type_High_Bound (Standard_Long_Long_Integer);
13206             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
13207          end if;
13208       end if;
13209
13210       --  Complete both implicit base and declared first subtype entities
13211
13212       Set_Etype          (Implicit_Base, Base_Typ);
13213       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
13214       Set_Size_Info      (Implicit_Base,                (Base_Typ));
13215       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
13216       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
13217
13218       Set_Ekind          (T, E_Signed_Integer_Subtype);
13219       Set_Etype          (T, Implicit_Base);
13220
13221       Set_Size_Info      (T,                (Implicit_Base));
13222       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
13223       Set_Scalar_Range   (T, Def);
13224       Set_RM_Size        (T, UI_From_Int (Minimum_Size (T)));
13225       Set_Is_Constrained (T);
13226    end Signed_Integer_Type_Declaration;
13227
13228 end Sem_Ch3;