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