OSDN Git Service

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