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