OSDN Git Service

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