OSDN Git Service

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