OSDN Git Service

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