OSDN Git Service

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