OSDN Git Service

2005-06-14 Gary Dismukes <dismukes@adacore.com>
[pf3gnuchains/gcc-fork.git] / gcc / ada / sem_ch3.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --                              S E M _ C H 3                               --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --          Copyright (C) 1992-2005 Free Software Foundation, Inc.          --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, --
20 -- MA 02111-1307, USA.                                                      --
21 --                                                                          --
22 -- GNAT was originally developed  by the GNAT team at  New York University. --
23 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
24 --                                                                          --
25 ------------------------------------------------------------------------------
26
27 with Atree;    use Atree;
28 with Checks;   use Checks;
29 with Elists;   use Elists;
30 with Einfo;    use Einfo;
31 with Errout;   use Errout;
32 with Eval_Fat; use Eval_Fat;
33 with Exp_Ch3;  use Exp_Ch3;
34 with Exp_Dist; use Exp_Dist;
35 with Exp_Tss;  use Exp_Tss;
36 with Exp_Util; use Exp_Util;
37 with Freeze;   use Freeze;
38 with Itypes;   use Itypes;
39 with Layout;   use Layout;
40 with Lib;      use Lib;
41 with Lib.Xref; use Lib.Xref;
42 with Namet;    use Namet;
43 with Nmake;    use Nmake;
44 with Opt;      use Opt;
45 with Restrict; use Restrict;
46 with Rident;   use Rident;
47 with Rtsfind;  use Rtsfind;
48 with Sem;      use Sem;
49 with Sem_Case; use Sem_Case;
50 with Sem_Cat;  use Sem_Cat;
51 with Sem_Ch6;  use Sem_Ch6;
52 with Sem_Ch7;  use Sem_Ch7;
53 with Sem_Ch8;  use Sem_Ch8;
54 with Sem_Ch13; use Sem_Ch13;
55 with Sem_Disp; use Sem_Disp;
56 with Sem_Dist; use Sem_Dist;
57 with Sem_Elim; use Sem_Elim;
58 with Sem_Eval; use Sem_Eval;
59 with Sem_Mech; use Sem_Mech;
60 with Sem_Res;  use Sem_Res;
61 with Sem_Smem; use Sem_Smem;
62 with Sem_Type; use Sem_Type;
63 with Sem_Util; use Sem_Util;
64 with Sem_Warn; use Sem_Warn;
65 with Stand;    use Stand;
66 with Sinfo;    use Sinfo;
67 with Snames;   use Snames;
68 with Tbuild;   use Tbuild;
69 with Ttypes;   use Ttypes;
70 with Uintp;    use Uintp;
71 with Urealp;   use Urealp;
72
73 package body Sem_Ch3 is
74
75    -----------------------
76    -- Local Subprograms --
77    -----------------------
78
79    procedure Add_Interface_Tag_Components
80      (N : Node_Id; Typ : Entity_Id);
81    --  Ada 2005 (AI-251): Add the tag components corresponding to all the
82    --  abstract interface types implemented by a record type or a derived
83    --  record type.
84
85    procedure Build_Derived_Type
86      (N             : Node_Id;
87       Parent_Type   : Entity_Id;
88       Derived_Type  : Entity_Id;
89       Is_Completion : Boolean;
90       Derive_Subps  : Boolean := True);
91    --  Create and decorate a Derived_Type given the Parent_Type entity.
92    --  N is the N_Full_Type_Declaration node containing the derived type
93    --  definition. Parent_Type is the entity for the parent type in the derived
94    --  type definition and Derived_Type the actual derived type. Is_Completion
95    --  must be set to False if Derived_Type is the N_Defining_Identifier node
96    --  in N (ie Derived_Type = Defining_Identifier (N)). In this case N is not
97    --  the completion of a private type declaration. If Is_Completion is
98    --  set to True, N is the completion of a private type declaration and
99    --  Derived_Type is different from the defining identifier inside N (i.e.
100    --  Derived_Type /= Defining_Identifier (N)). Derive_Subps indicates whether
101    --  the parent subprograms should be derived. The only case where this
102    --  parameter is False is when Build_Derived_Type is recursively called to
103    --  process an implicit derived full type for a type derived from a private
104    --  type (in that case the subprograms must only be derived for the private
105    --  view of the type).
106    --  ??? These flags need a bit of re-examination and re-documentation:
107    --  ???  are they both necessary (both seem related to the recursion)?
108
109    procedure Build_Derived_Access_Type
110      (N            : Node_Id;
111       Parent_Type  : Entity_Id;
112       Derived_Type : Entity_Id);
113    --  Subsidiary procedure to Build_Derived_Type. For a derived access type,
114    --  create an implicit base if the parent type is constrained or if the
115    --  subtype indication has a constraint.
116
117    procedure Build_Derived_Array_Type
118      (N            : Node_Id;
119       Parent_Type  : Entity_Id;
120       Derived_Type : Entity_Id);
121    --  Subsidiary procedure to Build_Derived_Type. For a derived array type,
122    --  create an implicit base if the parent type is constrained or if the
123    --  subtype indication has a constraint.
124
125    procedure Build_Derived_Concurrent_Type
126      (N            : Node_Id;
127       Parent_Type  : Entity_Id;
128       Derived_Type : Entity_Id);
129    --  Subsidiary procedure to Build_Derived_Type. For a derived task or pro-
130    --  tected type, inherit entries and protected subprograms, check legality
131    --  of discriminant constraints if any.
132
133    procedure Build_Derived_Enumeration_Type
134      (N            : Node_Id;
135       Parent_Type  : Entity_Id;
136       Derived_Type : Entity_Id);
137    --  Subsidiary procedure to Build_Derived_Type. For a derived enumeration
138    --  type, we must create a new list of literals. Types derived from
139    --  Character and Wide_Character are special-cased.
140
141    procedure Build_Derived_Numeric_Type
142      (N            : Node_Id;
143       Parent_Type  : Entity_Id;
144       Derived_Type : Entity_Id);
145    --  Subsidiary procedure to Build_Derived_Type. For numeric types, create
146    --  an anonymous base type, and propagate constraint to subtype if needed.
147
148    procedure Build_Derived_Private_Type
149      (N             : Node_Id;
150       Parent_Type   : Entity_Id;
151       Derived_Type  : Entity_Id;
152       Is_Completion : Boolean;
153       Derive_Subps  : Boolean := True);
154    --  Subsidiary procedure to Build_Derived_Type. This procedure is complex
155    --  because the parent may or may not have a completion, and the derivation
156    --  may itself be a completion.
157
158    procedure Build_Derived_Record_Type
159      (N            : Node_Id;
160       Parent_Type  : Entity_Id;
161       Derived_Type : Entity_Id;
162       Derive_Subps : Boolean := True);
163    --  Subsidiary procedure to Build_Derived_Type and
164    --  Analyze_Private_Extension_Declaration used for tagged and untagged
165    --  record types. All parameters are as in Build_Derived_Type except that
166    --  N, in addition to being an N_Full_Type_Declaration node, can also be an
167    --  N_Private_Extension_Declaration node. See the definition of this routine
168    --  for much more info. Derive_Subps indicates whether subprograms should
169    --  be derived from the parent type. The only case where Derive_Subps is
170    --  False is for an implicit derived full type for a type derived from a
171    --  private type (see Build_Derived_Type).
172
173    procedure Collect_Interfaces
174      (N            : Node_Id;
175       Derived_Type : Entity_Id);
176    --  Ada 2005 (AI-251): Subsidiary procedure to Build_Derived_Record_Type.
177    --  Collect the list of interfaces that are not already implemented by the
178    --  ancestors. This is the list of interfaces for which we must provide
179    --  additional tag components.
180
181    procedure Complete_Subprograms_Derivation
182      (Partial_View : Entity_Id;
183       Derived_Type : Entity_Id);
184    --  Ada 2005 (AI-251): Used to complete type derivation of private tagged
185    --  types implementing interfaces. In this case some interface primitives
186    --  may have been overriden with the partial-view and, instead of
187    --  re-calculating them, they are included in the list of primitive
188    --  operations of the full-view.
189
190    function Inherit_Components
191      (N             : Node_Id;
192       Parent_Base   : Entity_Id;
193       Derived_Base  : Entity_Id;
194       Is_Tagged     : Boolean;
195       Inherit_Discr : Boolean;
196       Discs         : Elist_Id) return Elist_Id;
197    --  Called from Build_Derived_Record_Type to inherit the components of
198    --  Parent_Base (a base type) into the Derived_Base (the derived base type).
199    --  For more information on derived types and component inheritance please
200    --  consult the comment above the body of Build_Derived_Record_Type.
201    --
202    --    N is the original derived type declaration.
203    --
204    --    Is_Tagged is set if we are dealing with tagged types.
205    --
206    --    If Inherit_Discr is set, Derived_Base inherits its discriminants
207    --    from Parent_Base, otherwise no discriminants are inherited.
208    --
209    --    Discs gives the list of constraints that apply to Parent_Base in the
210    --    derived type declaration. If Discs is set to No_Elist, then we have
211    --    the following situation:
212    --
213    --      type Parent (D1..Dn : ..) is [tagged] record ...;
214    --      type Derived is new Parent [with ...];
215    --
216    --    which gets treated as
217    --
218    --      type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
219    --
220    --  For untagged types the returned value is an association list. The list
221    --  starts from the association (Parent_Base => Derived_Base), and then it
222    --  contains a sequence of the associations of the form
223    --
224    --    (Old_Component => New_Component),
225    --
226    --  where Old_Component is the Entity_Id of a component in Parent_Base
227    --  and New_Component is the Entity_Id of the corresponding component
228    --  in Derived_Base. For untagged records, this association list is
229    --  needed when copying the record declaration for the derived base.
230    --  In the tagged case the value returned is irrelevant.
231
232    procedure Build_Discriminal (Discrim : Entity_Id);
233    --  Create the discriminal corresponding to discriminant Discrim, that is
234    --  the parameter corresponding to Discrim to be used in initialization
235    --  procedures for the type where Discrim is a discriminant. Discriminals
236    --  are not used during semantic analysis, and are not fully defined
237    --  entities until expansion. Thus they are not given a scope until
238    --  initialization procedures are built.
239
240    function Build_Discriminant_Constraints
241      (T           : Entity_Id;
242       Def         : Node_Id;
243       Derived_Def : Boolean := False) return Elist_Id;
244    --  Validate discriminant constraints, and return the list of the
245    --  constraints in order of discriminant declarations. T is the
246    --  discriminated unconstrained type. Def is the N_Subtype_Indication
247    --  node where the discriminants constraints for T are specified.
248    --  Derived_Def is True if we are building the discriminant constraints
249    --  in a derived type definition of the form "type D (...) is new T (xxx)".
250    --  In this case T is the parent type and Def is the constraint "(xxx)" on
251    --  T and this routine sets the Corresponding_Discriminant field of the
252    --  discriminants in the derived type D to point to the corresponding
253    --  discriminants in the parent type T.
254
255    procedure Build_Discriminated_Subtype
256      (T           : Entity_Id;
257       Def_Id      : Entity_Id;
258       Elist       : Elist_Id;
259       Related_Nod : Node_Id;
260       For_Access  : Boolean := False);
261    --  Subsidiary procedure to Constrain_Discriminated_Type and to
262    --  Process_Incomplete_Dependents. Given
263    --
264    --     T (a possibly discriminated base type)
265    --     Def_Id (a very partially built subtype for T),
266    --
267    --  the call completes Def_Id to be the appropriate E_*_Subtype.
268    --
269    --  The Elist is the list of discriminant constraints if any (it is set to
270    --  No_Elist if T is not a discriminated type, and to an empty list if
271    --  T has discriminants but there are no discriminant constraints). The
272    --  Related_Nod is the same as Decl_Node in Create_Constrained_Components.
273    --  The For_Access says whether or not this subtype is really constraining
274    --  an access type. That is its sole purpose is the designated type of an
275    --  access type -- in which case a Private_Subtype Is_For_Access_Subtype
276    --  is built to avoid freezing T when the access subtype is frozen.
277
278    function Build_Scalar_Bound
279      (Bound : Node_Id;
280       Par_T : Entity_Id;
281       Der_T : Entity_Id) return Node_Id;
282    --  The bounds of a derived scalar type are conversions of the bounds of
283    --  the parent type. Optimize the representation if the bounds are literals.
284    --  Needs a more complete spec--what are the parameters exactly, and what
285    --  exactly is the returned value, and how is Bound affected???
286
287    procedure Build_Underlying_Full_View
288      (N   : Node_Id;
289       Typ : Entity_Id;
290       Par : Entity_Id);
291    --  If the completion of a private type is itself derived from a private
292    --  type, or if the full view of a private subtype is itself private, the
293    --  back-end has no way to compute the actual size of this type. We build
294    --  an internal subtype declaration of the proper parent type to convey
295    --  this information. This extra mechanism is needed because a full
296    --  view cannot itself have a full view (it would get clobbered during
297    --  view exchanges).
298
299    procedure Check_Access_Discriminant_Requires_Limited
300      (D   : Node_Id;
301       Loc : Node_Id);
302    --  Check the restriction that the type to which an access discriminant
303    --  belongs must be a concurrent type or a descendant of a type with
304    --  the reserved word 'limited' in its declaration.
305
306    procedure Check_Delta_Expression (E : Node_Id);
307    --  Check that the expression represented by E is suitable for use
308    --  as a delta expression, i.e. it is of real type and is static.
309
310    procedure Check_Digits_Expression (E : Node_Id);
311    --  Check that the expression represented by E is suitable for use as
312    --  a digits expression, i.e. it is of integer type, positive and static.
313
314    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
315    --  Validate the initialization of an object declaration. T is the
316    --  required type, and Exp is the initialization expression.
317
318    procedure Check_Or_Process_Discriminants
319      (N    : Node_Id;
320       T    : Entity_Id;
321       Prev : Entity_Id := Empty);
322    --  If T is the full declaration of an incomplete or private type, check
323    --  the conformance of the discriminants, otherwise process them. Prev
324    --  is the entity of the partial declaration, if any.
325
326    procedure Check_Real_Bound (Bound : Node_Id);
327    --  Check given bound for being of real type and static. If not, post an
328    --  appropriate message, and rewrite the bound with the real literal zero.
329
330    procedure Constant_Redeclaration
331      (Id : Entity_Id;
332       N  : Node_Id;
333       T  : out Entity_Id);
334    --  Various checks on legality of full declaration of deferred constant.
335    --  Id is the entity for the redeclaration, N is the N_Object_Declaration,
336    --  node. The caller has not yet set any attributes of this entity.
337
338    procedure Convert_Scalar_Bounds
339      (N            : Node_Id;
340       Parent_Type  : Entity_Id;
341       Derived_Type : Entity_Id;
342       Loc          : Source_Ptr);
343    --  For derived scalar types, convert the bounds in the type definition
344    --  to the derived type, and complete their analysis. Given a constraint
345    --  of the form:
346    --                   ..  new T range Lo .. Hi;
347    --  Lo and Hi are analyzed and resolved with T'Base, the parent_type.
348    --  The bounds of the derived type (the anonymous base) are copies of
349    --  Lo and Hi.  Finally, the bounds of the derived subtype are conversions
350    --  of those bounds to the derived_type, so that their typing is
351    --  consistent.
352
353    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
354    --  Copies attributes from array base type T2 to array base type T1.
355    --  Copies only attributes that apply to base types, but not subtypes.
356
357    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
358    --  Copies attributes from array subtype T2 to array subtype T1. Copies
359    --  attributes that apply to both subtypes and base types.
360
361    procedure Create_Constrained_Components
362      (Subt        : Entity_Id;
363       Decl_Node   : Node_Id;
364       Typ         : Entity_Id;
365       Constraints : Elist_Id);
366    --  Build the list of entities for a constrained discriminated record
367    --  subtype. If a component depends on a discriminant, replace its subtype
368    --  using the discriminant values in the discriminant constraint.
369    --  Subt is the defining identifier for the subtype whose list of
370    --  constrained entities we will create. Decl_Node is the type declaration
371    --  node where we will attach all the itypes created. Typ is the base
372    --  discriminated type for the subtype Subt. Constraints is the list of
373    --  discriminant constraints for Typ.
374
375    function Constrain_Component_Type
376      (Comp            : Entity_Id;
377       Constrained_Typ : Entity_Id;
378       Related_Node    : Node_Id;
379       Typ             : Entity_Id;
380       Constraints     : Elist_Id) return Entity_Id;
381    --  Given a discriminated base type Typ, a list of discriminant constraint
382    --  Constraints for Typ and a component of Typ, with type Compon_Type,
383    --  create and return the type corresponding to Compon_type where all
384    --  discriminant references are replaced with the corresponding
385    --  constraint. If no discriminant references occur in Compon_Typ then
386    --  return it as is. Constrained_Typ is the final constrained subtype to
387    --  which the constrained Compon_Type belongs. Related_Node is the node
388    --  where we will attach all the itypes created.
389
390    procedure Constrain_Access
391      (Def_Id      : in out Entity_Id;
392       S           : Node_Id;
393       Related_Nod : Node_Id);
394    --  Apply a list of constraints to an access type. If Def_Id is empty,
395    --  it is an anonymous type created for a subtype indication. In that
396    --  case it is created in the procedure and attached to Related_Nod.
397
398    procedure Constrain_Array
399      (Def_Id      : in out Entity_Id;
400       SI          : Node_Id;
401       Related_Nod : Node_Id;
402       Related_Id  : Entity_Id;
403       Suffix      : Character);
404    --  Apply a list of index constraints to an unconstrained array type. The
405    --  first parameter is the entity for the resulting subtype. A value of
406    --  Empty for Def_Id indicates that an implicit type must be created, but
407    --  creation is delayed (and must be done by this procedure) because other
408    --  subsidiary implicit types must be created first (which is why Def_Id
409    --  is an in/out parameter). The second parameter is a subtype indication
410    --  node for the constrained array to be created (e.g. something of the
411    --  form string (1 .. 10)). Related_Nod gives the place where this type
412    --  has to be inserted in the tree. The Related_Id and Suffix parameters
413    --  are used to build the associated Implicit type name.
414
415    procedure Constrain_Concurrent
416      (Def_Id      : in out Entity_Id;
417       SI          : Node_Id;
418       Related_Nod : Node_Id;
419       Related_Id  : Entity_Id;
420       Suffix      : Character);
421    --  Apply list of discriminant constraints to an unconstrained concurrent
422    --  type.
423    --
424    --    SI is the N_Subtype_Indication node containing the constraint and
425    --    the unconstrained type to constrain.
426    --
427    --    Def_Id is the entity for the resulting constrained subtype. A value
428    --    of Empty for Def_Id indicates that an implicit type must be created,
429    --    but creation is delayed (and must be done by this procedure) because
430    --    other subsidiary implicit types must be created first (which is why
431    --    Def_Id is an in/out parameter).
432    --
433    --    Related_Nod gives the place where this type has to be inserted
434    --    in the tree
435    --
436    --  The last two arguments are used to create its external name if needed.
437
438    function Constrain_Corresponding_Record
439      (Prot_Subt   : Entity_Id;
440       Corr_Rec    : Entity_Id;
441       Related_Nod : Node_Id;
442       Related_Id  : Entity_Id) return Entity_Id;
443    --  When constraining a protected type or task type with discriminants,
444    --  constrain the corresponding record with the same discriminant values.
445
446    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
447    --  Constrain a decimal fixed point type with a digits constraint and/or a
448    --  range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
449
450    procedure Constrain_Discriminated_Type
451      (Def_Id      : Entity_Id;
452       S           : Node_Id;
453       Related_Nod : Node_Id;
454       For_Access  : Boolean := False);
455    --  Process discriminant constraints of composite type. Verify that values
456    --  have been provided for all discriminants, that the original type is
457    --  unconstrained, and that the types of the supplied expressions match
458    --  the discriminant types. The first three parameters are like in routine
459    --  Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
460    --  of For_Access.
461
462    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
463    --  Constrain an enumeration type with a range constraint. This is
464    --  identical to Constrain_Integer, but for the Ekind of the
465    --  resulting subtype.
466
467    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
468    --  Constrain a floating point type with either a digits constraint
469    --  and/or a range constraint, building a E_Floating_Point_Subtype.
470
471    procedure Constrain_Index
472      (Index        : Node_Id;
473       S            : Node_Id;
474       Related_Nod  : Node_Id;
475       Related_Id   : Entity_Id;
476       Suffix       : Character;
477       Suffix_Index : Nat);
478    --  Process an index constraint in a constrained array declaration. The
479    --  constraint can be a subtype name, or a range with or without an
480    --  explicit subtype mark. The index is the corresponding index of the
481    --  unconstrained array. The Related_Id and Suffix parameters are used to
482    --  build the associated Implicit type name.
483
484    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
485    --  Build subtype of a signed or modular integer type
486
487    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
488    --  Constrain an ordinary fixed point type with a range constraint, and
489    --  build an E_Ordinary_Fixed_Point_Subtype entity.
490
491    procedure Copy_And_Swap (Priv, Full : Entity_Id);
492    --  Copy the Priv entity into the entity of its full declaration
493    --  then swap the two entities in such a manner that the former private
494    --  type is now seen as a full type.
495
496    procedure Decimal_Fixed_Point_Type_Declaration
497      (T   : Entity_Id;
498       Def : Node_Id);
499    --  Create a new decimal fixed point type, and apply the constraint to
500    --  obtain a subtype of this new type.
501
502    procedure Complete_Private_Subtype
503      (Priv        : Entity_Id;
504       Full        : Entity_Id;
505       Full_Base   : Entity_Id;
506       Related_Nod : Node_Id);
507    --  Complete the implicit full view of a private subtype by setting
508    --  the appropriate semantic fields. If the full view of the parent is
509    --  a record type, build constrained components of subtype.
510
511    procedure Derive_Interface_Subprograms
512      (Derived_Type : Entity_Id);
513    --  Ada 2005 (AI-251): Subsidiary procedure to Build_Derived_Record_Type.
514    --  Traverse the list of implemented interfaces and derive all their
515    --  subprograms.
516
517    procedure Derived_Standard_Character
518      (N             : Node_Id;
519       Parent_Type   : Entity_Id;
520       Derived_Type  : Entity_Id);
521    --  Subsidiary procedure to Build_Derived_Enumeration_Type which handles
522    --  derivations from types Standard.Character and Standard.Wide_Character.
523
524    procedure Derived_Type_Declaration
525      (T             : Entity_Id;
526       N             : Node_Id;
527       Is_Completion : Boolean);
528    --  Process a derived type declaration. This routine will invoke
529    --  Build_Derived_Type to process the actual derived type definition.
530    --  Parameters N and Is_Completion have the same meaning as in
531    --  Build_Derived_Type. T is the N_Defining_Identifier for the entity
532    --  defined in the N_Full_Type_Declaration node N, that is T is the
533    --  derived type.
534
535    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
536    --  Insert each literal in symbol table, as an overloadable identifier
537    --  Each enumeration type is mapped into a sequence of integers, and
538    --  each literal is defined as a constant with integer value. If any
539    --  of the literals are character literals, the type is a character
540    --  type, which means that strings are legal aggregates for arrays of
541    --  components of the type.
542
543    function Expand_To_Stored_Constraint
544      (Typ        : Entity_Id;
545       Constraint : Elist_Id) return Elist_Id;
546    --  Given a Constraint (ie a list of expressions) on the discriminants of
547    --  Typ, expand it into a constraint on the stored discriminants and
548    --  return the new list of expressions constraining the stored
549    --  discriminants.
550
551    function Find_Type_Of_Object
552      (Obj_Def     : Node_Id;
553       Related_Nod : Node_Id) return Entity_Id;
554    --  Get type entity for object referenced by Obj_Def, attaching the
555    --  implicit types generated to Related_Nod
556
557    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
558    --  Create a new float, and apply the constraint to obtain subtype of it
559
560    function Has_Range_Constraint (N : Node_Id) return Boolean;
561    --  Given an N_Subtype_Indication node N, return True if a range constraint
562    --  is present, either directly, or as part of a digits or delta constraint.
563    --  In addition, a digits constraint in the decimal case returns True, since
564    --  it establishes a default range if no explicit range is present.
565
566    function Is_Valid_Constraint_Kind
567      (T_Kind          : Type_Kind;
568       Constraint_Kind : Node_Kind) return Boolean;
569    --  Returns True if it is legal to apply the given kind of constraint
570    --  to the given kind of type (index constraint to an array type,
571    --  for example).
572
573    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
574    --  Create new modular type. Verify that modulus is in  bounds and is
575    --  a power of two (implementation restriction).
576
577    procedure New_Concatenation_Op (Typ : Entity_Id);
578    --  Create an abbreviated declaration for an operator in order to
579    --  materialize concatenation on array types.
580
581    procedure Ordinary_Fixed_Point_Type_Declaration
582      (T   : Entity_Id;
583       Def : Node_Id);
584    --  Create a new ordinary fixed point type, and apply the constraint
585    --  to obtain subtype of it.
586
587    procedure Prepare_Private_Subtype_Completion
588      (Id          : Entity_Id;
589       Related_Nod : Node_Id);
590    --  Id is a subtype of some private type. Creates the full declaration
591    --  associated with Id whenever possible, i.e. when the full declaration
592    --  of the base type is already known. Records each subtype into
593    --  Private_Dependents of the base type.
594
595    procedure Process_Incomplete_Dependents
596      (N      : Node_Id;
597       Full_T : Entity_Id;
598       Inc_T  : Entity_Id);
599    --  Process all entities that depend on an incomplete type. There include
600    --  subtypes, subprogram types that mention the incomplete type in their
601    --  profiles, and subprogram with access parameters that designate the
602    --  incomplete type.
603
604    --  Inc_T is the defining identifier of an incomplete type declaration, its
605    --  Ekind is E_Incomplete_Type.
606    --
607    --    N is the corresponding N_Full_Type_Declaration for Inc_T.
608    --
609    --    Full_T is N's defining identifier.
610    --
611    --  Subtypes of incomplete types with discriminants are completed when the
612    --  parent type is. This is simpler than private subtypes, because they can
613    --  only appear in the same scope, and there is no need to exchange views.
614    --  Similarly, access_to_subprogram types may have a parameter or a return
615    --  type that is an incomplete type, and that must be replaced with the
616    --  full type.
617
618    --  If the full type is tagged, subprogram with access parameters that
619    --  designated the incomplete may be primitive operations of the full type,
620    --  and have to be processed accordingly.
621
622    procedure Process_Real_Range_Specification (Def : Node_Id);
623    --  Given the type definition for a real type, this procedure processes
624    --  and checks the real range specification of this type definition if
625    --  one is present. If errors are found, error messages are posted, and
626    --  the Real_Range_Specification of Def is reset to Empty.
627
628    procedure Record_Type_Declaration
629      (T    : Entity_Id;
630       N    : Node_Id;
631       Prev : Entity_Id);
632    --  Process a record type declaration (for both untagged and tagged
633    --  records). Parameters T and N are exactly like in procedure
634    --  Derived_Type_Declaration, except that no flag Is_Completion is
635    --  needed for this routine. If this is the completion of an incomplete
636    --  type declaration, Prev is the entity of the incomplete declaration,
637    --  used for cross-referencing. Otherwise Prev = T.
638
639    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
640    --  This routine is used to process the actual record type definition
641    --  (both for untagged and tagged records). Def is a record type
642    --  definition node. This procedure analyzes the components in this
643    --  record type definition. Prev_T is the entity for the enclosing record
644    --  type. It is provided so that its Has_Task flag can be set if any of
645    --  the component have Has_Task set. If the declaration is the completion
646    --  of an incomplete type declaration, Prev_T is the original incomplete
647    --  type, whose full view is the record type.
648
649    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
650    --  Subsidiary to Build_Derived_Record_Type. For untagged records, we
651    --  build a copy of the declaration tree of the parent, and we create
652    --  independently the list of components for the derived type. Semantic
653    --  information uses the component entities, but record representation
654    --  clauses are validated on the declaration tree. This procedure replaces
655    --  discriminants and components in the declaration with those that have
656    --  been created by Inherit_Components.
657
658    procedure Set_Fixed_Range
659      (E   : Entity_Id;
660       Loc : Source_Ptr;
661       Lo  : Ureal;
662       Hi  : Ureal);
663    --  Build a range node with the given bounds and set it as the Scalar_Range
664    --  of the given fixed-point type entity. Loc is the source location used
665    --  for the constructed range. See body for further details.
666
667    procedure Set_Scalar_Range_For_Subtype
668      (Def_Id : Entity_Id;
669       R      : Node_Id;
670       Subt   : Entity_Id);
671    --  This routine is used to set the scalar range field for a subtype
672    --  given Def_Id, the entity for the subtype, and R, the range expression
673    --  for the scalar range. Subt provides the parent subtype to be used
674    --  to analyze, resolve, and check the given range.
675
676    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
677    --  Create a new signed integer entity, and apply the constraint to obtain
678    --  the required first named subtype of this type.
679
680    procedure Set_Stored_Constraint_From_Discriminant_Constraint
681      (E : Entity_Id);
682    --  E is some record type. This routine computes E's Stored_Constraint
683    --  from its Discriminant_Constraint.
684
685    -----------------------
686    -- Access_Definition --
687    -----------------------
688
689    function Access_Definition
690      (Related_Nod : Node_Id;
691       N           : Node_Id) return Entity_Id
692    is
693       Anon_Type : constant Entity_Id :=
694                     Create_Itype (E_Anonymous_Access_Type, Related_Nod,
695                                   Scope_Id => Scope (Current_Scope));
696       Desig_Type : Entity_Id;
697
698    begin
699       if Is_Entry (Current_Scope)
700         and then Is_Task_Type (Etype (Scope (Current_Scope)))
701       then
702          Error_Msg_N ("task entries cannot have access parameters", N);
703       end if;
704
705       --  Ada 2005: for an object declaration, the corresponding anonymous
706       --  type is declared in the current scope. For access formals, access
707       --  components, and access discriminants, the scope is that of the
708       --  enclosing declaration, as set above.
709
710       if Nkind (Related_Nod) = N_Object_Declaration then
711          Set_Scope (Anon_Type, Current_Scope);
712       end if;
713
714       if All_Present (N)
715         and then Ada_Version >= Ada_05
716       then
717          Error_Msg_N ("ALL is not permitted for anonymous access types", N);
718       end if;
719
720       --  Ada 2005 (AI-254): In case of anonymous access to subprograms
721       --  call the corresponding semantic routine
722
723       if Present (Access_To_Subprogram_Definition (N)) then
724          Access_Subprogram_Declaration
725            (T_Name => Anon_Type,
726             T_Def  => Access_To_Subprogram_Definition (N));
727
728          if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
729             Set_Ekind
730               (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
731          else
732             Set_Ekind
733               (Anon_Type, E_Anonymous_Access_Subprogram_Type);
734          end if;
735
736          return Anon_Type;
737       end if;
738
739       Find_Type (Subtype_Mark (N));
740       Desig_Type := Entity (Subtype_Mark (N));
741
742       Set_Directly_Designated_Type
743                              (Anon_Type, Desig_Type);
744       Set_Etype              (Anon_Type, Anon_Type);
745       Init_Size_Align        (Anon_Type);
746       Set_Depends_On_Private (Anon_Type, Has_Private_Component (Anon_Type));
747
748       --  Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
749       --  from Ada 95 semantics. In Ada 2005, anonymous access must specify
750       --  if the null value is allowed. In Ada 95 the null value is never
751       --  allowed.
752
753       if Ada_Version >= Ada_05 then
754          Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
755       else
756          Set_Can_Never_Be_Null (Anon_Type, True);
757       end if;
758
759       --  The anonymous access type is as public as the discriminated type or
760       --  subprogram that defines it. It is imported (for back-end purposes)
761       --  if the designated type is.
762
763       Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
764
765       --  Ada 2005 (AI-50217): Propagate the attribute that indicates that the
766       --  designated type comes from the limited view (for back-end purposes).
767
768       Set_From_With_Type (Anon_Type, From_With_Type (Desig_Type));
769
770       --  Ada 2005 (AI-231): Propagate the access-constant attribute
771
772       Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
773
774       --  The context is either a subprogram declaration, object declaration,
775       --  or an access discriminant, in a private or a full type declaration.
776       --  In the case of a subprogram, if the designated type is incomplete,
777       --  the operation will be a primitive operation of the full type, to be
778       --  updated subsequently. If the type is imported through a limited_with
779       --  clause, the subprogram is not a primitive operation of the type
780       --  (which is declared elsewhere in some other scope).
781
782       if Ekind (Desig_Type) = E_Incomplete_Type
783         and then not From_With_Type (Desig_Type)
784         and then Is_Overloadable (Current_Scope)
785       then
786          Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
787          Set_Has_Delayed_Freeze (Current_Scope);
788       end if;
789
790       return Anon_Type;
791    end Access_Definition;
792
793    -----------------------------------
794    -- Access_Subprogram_Declaration --
795    -----------------------------------
796
797    procedure Access_Subprogram_Declaration
798      (T_Name : Entity_Id;
799       T_Def  : Node_Id)
800    is
801       Formals : constant List_Id := Parameter_Specifications (T_Def);
802       Formal  : Entity_Id;
803
804       Desig_Type : constant Entity_Id :=
805                      Create_Itype (E_Subprogram_Type, Parent (T_Def));
806       D_Ityp     : Node_Id := Associated_Node_For_Itype (Desig_Type);
807
808    begin
809       --  Associate the Itype node with the inner full-type declaration
810       --  or subprogram spec. This is required to handle nested anonymous
811       --  declarations. For example:
812
813       --      procedure P
814       --       (X : access procedure
815       --                     (Y : access procedure
816       --                                   (Z : access T)))
817
818       while Nkind (D_Ityp) /= N_Full_Type_Declaration
819          and then Nkind (D_Ityp) /= N_Procedure_Specification
820          and then Nkind (D_Ityp) /= N_Function_Specification
821          and then Nkind (D_Ityp) /= N_Object_Renaming_Declaration
822          and then Nkind (D_Ityp) /= N_Formal_Type_Declaration
823       loop
824          D_Ityp := Parent (D_Ityp);
825          pragma Assert (D_Ityp /= Empty);
826       end loop;
827
828       Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
829
830       if Nkind (D_Ityp) = N_Procedure_Specification
831         or else Nkind (D_Ityp) = N_Function_Specification
832       then
833          Set_Scope (Desig_Type, Scope (Defining_Unit_Name (D_Ityp)));
834
835       elsif Nkind (D_Ityp) = N_Full_Type_Declaration
836         or else Nkind (D_Ityp) = N_Object_Renaming_Declaration
837         or else Nkind (D_Ityp) = N_Formal_Type_Declaration
838       then
839          Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
840       end if;
841
842       if Nkind (T_Def) = N_Access_Function_Definition then
843          Analyze (Subtype_Mark (T_Def));
844          Set_Etype (Desig_Type, Entity (Subtype_Mark (T_Def)));
845
846          if not (Is_Type (Etype (Desig_Type))) then
847             Error_Msg_N
848              ("expect type in function specification", Subtype_Mark (T_Def));
849          end if;
850
851       else
852          Set_Etype (Desig_Type, Standard_Void_Type);
853       end if;
854
855       if Present (Formals) then
856          New_Scope (Desig_Type);
857          Process_Formals (Formals, Parent (T_Def));
858
859          --  A bit of a kludge here, End_Scope requires that the parent
860          --  pointer be set to something reasonable, but Itypes don't have
861          --  parent pointers. So we set it and then unset it ??? If and when
862          --  Itypes have proper parent pointers to their declarations, this
863          --  kludge can be removed.
864
865          Set_Parent (Desig_Type, T_Name);
866          End_Scope;
867          Set_Parent (Desig_Type, Empty);
868       end if;
869
870       --  The return type and/or any parameter type may be incomplete. Mark
871       --  the subprogram_type as depending on the incomplete type, so that
872       --  it can be updated when the full type declaration is seen.
873
874       if Present (Formals) then
875          Formal := First_Formal (Desig_Type);
876
877          while Present (Formal) loop
878             if Ekind (Formal) /= E_In_Parameter
879               and then Nkind (T_Def) = N_Access_Function_Definition
880             then
881                Error_Msg_N ("functions can only have IN parameters", Formal);
882             end if;
883
884             if Ekind (Etype (Formal)) = E_Incomplete_Type then
885                Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
886                Set_Has_Delayed_Freeze (Desig_Type);
887             end if;
888
889             Next_Formal (Formal);
890          end loop;
891       end if;
892
893       if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
894         and then not Has_Delayed_Freeze (Desig_Type)
895       then
896          Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
897          Set_Has_Delayed_Freeze (Desig_Type);
898       end if;
899
900       Check_Delayed_Subprogram (Desig_Type);
901
902       if Protected_Present (T_Def) then
903          Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
904          Set_Convention (Desig_Type, Convention_Protected);
905       else
906          Set_Ekind (T_Name, E_Access_Subprogram_Type);
907       end if;
908
909       Set_Etype                    (T_Name, T_Name);
910       Init_Size_Align              (T_Name);
911       Set_Directly_Designated_Type (T_Name, Desig_Type);
912
913       --  Ada 2005 (AI-231): Propagate the null-excluding attribute
914
915       Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
916
917       Check_Restriction (No_Access_Subprograms, T_Def);
918    end Access_Subprogram_Declaration;
919
920    ----------------------------
921    -- Access_Type_Declaration --
922    ----------------------------
923
924    procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
925       S : constant Node_Id := Subtype_Indication (Def);
926       P : constant Node_Id := Parent (Def);
927
928       Desig : Entity_Id;
929       --  Designated type
930
931    begin
932       --  Check for permissible use of incomplete type
933
934       if Nkind (S) /= N_Subtype_Indication then
935          Analyze (S);
936
937          if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
938             Set_Directly_Designated_Type (T, Entity (S));
939          else
940             Set_Directly_Designated_Type (T,
941               Process_Subtype (S, P, T, 'P'));
942          end if;
943
944       else
945          Set_Directly_Designated_Type (T,
946            Process_Subtype (S, P, T, 'P'));
947       end if;
948
949       if All_Present (Def) or Constant_Present (Def) then
950          Set_Ekind (T, E_General_Access_Type);
951       else
952          Set_Ekind (T, E_Access_Type);
953       end if;
954
955       if Base_Type (Designated_Type (T)) = T then
956          Error_Msg_N ("access type cannot designate itself", S);
957       end if;
958
959       Set_Etype (T, T);
960
961       --  If the type has appeared already in a with_type clause, it is
962       --  frozen and the pointer size is already set. Else, initialize.
963
964       if not From_With_Type (T) then
965          Init_Size_Align (T);
966       end if;
967
968       Set_Is_Access_Constant (T, Constant_Present (Def));
969
970       Desig := Designated_Type (T);
971
972       --  If designated type is an imported tagged type, indicate that the
973       --  access type is also imported, and therefore restricted in its use.
974       --  The access type may already be imported, so keep setting otherwise.
975
976       --  Ada 2005 (AI-50217): If the non-limited view of the designated type
977       --  is available, use it as the designated type of the access type, so
978       --  that the back-end gets a usable entity.
979
980       declare
981          N_Desig : Entity_Id;
982
983       begin
984          if From_With_Type (Desig) then
985             Set_From_With_Type (T);
986
987             if Ekind (Desig) = E_Incomplete_Type then
988                N_Desig := Non_Limited_View (Desig);
989
990             else pragma Assert (Ekind (Desig) = E_Class_Wide_Type);
991                if From_With_Type (Etype (Desig)) then
992                   N_Desig := Non_Limited_View (Etype (Desig));
993                else
994                   N_Desig := Etype (Desig);
995                end if;
996             end if;
997
998             pragma Assert (Present (N_Desig));
999             Set_Directly_Designated_Type (T, N_Desig);
1000          end if;
1001       end;
1002
1003       --  Note that Has_Task is always false, since the access type itself
1004       --  is not a task type. See Einfo for more description on this point.
1005       --  Exactly the same consideration applies to Has_Controlled_Component.
1006
1007       Set_Has_Task (T, False);
1008       Set_Has_Controlled_Component (T, False);
1009
1010       --  Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1011       --  attributes
1012
1013       Set_Can_Never_Be_Null  (T, Null_Exclusion_Present (Def));
1014       Set_Is_Access_Constant (T, Constant_Present (Def));
1015    end Access_Type_Declaration;
1016
1017    ----------------------------------
1018    -- Add_Interface_Tag_Components --
1019    ----------------------------------
1020
1021    procedure Add_Interface_Tag_Components
1022      (N        : Node_Id;
1023       Typ      : Entity_Id)
1024    is
1025       Loc      : constant Source_Ptr := Sloc (N);
1026       Elmt     : Elmt_Id;
1027       Ext      : Node_Id;
1028       L        : List_Id;
1029       Last_Tag : Node_Id;
1030       Comp     : Node_Id;
1031
1032       procedure Add_Tag (Iface : Entity_Id);
1033       --  Comment required ???
1034
1035       -------------
1036       -- Add_Tag --
1037       -------------
1038
1039       procedure Add_Tag (Iface : Entity_Id) is
1040          Def      : Node_Id;
1041          Tag      : Entity_Id;
1042          Decl     : Node_Id;
1043
1044       begin
1045          pragma Assert (Is_Tagged_Type (Iface)
1046            and then Is_Interface (Iface));
1047
1048          Def :=
1049            Make_Component_Definition (Loc,
1050              Aliased_Present    => True,
1051              Subtype_Indication =>
1052                New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1053
1054          Tag := Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1055
1056          Decl :=
1057            Make_Component_Declaration (Loc,
1058              Defining_Identifier  => Tag,
1059              Component_Definition => Def);
1060
1061          Analyze_Component_Declaration (Decl);
1062
1063          Set_Analyzed (Decl);
1064          Set_Ekind               (Tag, E_Component);
1065          Set_Is_Limited_Record   (Tag);
1066          Set_Is_Tag              (Tag);
1067          Init_Component_Location (Tag);
1068
1069          pragma Assert (Is_Frozen (Iface));
1070
1071          Set_DT_Entry_Count    (Tag,
1072            DT_Entry_Count (First_Entity (Iface)));
1073
1074          if not Present (Last_Tag) then
1075             Prepend (Decl, L);
1076          else
1077             Insert_After (Last_Tag, Decl);
1078          end if;
1079
1080          Last_Tag := Decl;
1081       end Add_Tag;
1082
1083    --  Start of procesing for Add_Interface_Tag_Components
1084
1085    begin
1086       if Ekind (Typ) /= E_Record_Type
1087         or else not Present (Abstract_Interfaces (Typ))
1088         or else Is_Empty_Elmt_List (Abstract_Interfaces (Typ))
1089       then
1090          return;
1091       end if;
1092
1093       if Present (Abstract_Interfaces (Typ)) then
1094
1095          --  Find the current last tag
1096
1097          if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1098             Ext := Record_Extension_Part (Type_Definition (N));
1099          else
1100             pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1101             Ext := Type_Definition (N);
1102          end if;
1103
1104          Last_Tag := Empty;
1105
1106          if not (Present (Component_List (Ext))) then
1107             Set_Null_Present (Ext, False);
1108             L := New_List;
1109             Set_Component_List (Ext,
1110               Make_Component_List (Loc,
1111                 Component_Items => L,
1112                 Null_Present => False));
1113          else
1114             if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1115                L := Component_Items
1116                       (Component_List
1117                         (Record_Extension_Part
1118                           (Type_Definition (N))));
1119             else
1120                L := Component_Items
1121                       (Component_List
1122                         (Type_Definition (N)));
1123             end if;
1124
1125             --  Find the last tag component
1126
1127             Comp := First (L);
1128
1129             while Present (Comp) loop
1130                if Is_Tag (Defining_Identifier (Comp)) then
1131                   Last_Tag := Comp;
1132                end if;
1133
1134                Next (Comp);
1135             end loop;
1136          end if;
1137
1138          --  At this point L references the list of components and Last_Tag
1139          --  references the current last tag (if any). Now we add the tag
1140          --  corresponding with all the interfaces that are not implemented
1141          --  by the parent.
1142
1143          pragma Assert (Present
1144                         (First_Elmt (Abstract_Interfaces (Typ))));
1145
1146          Elmt := First_Elmt (Abstract_Interfaces (Typ));
1147          while Present (Elmt) loop
1148             Add_Tag (Node (Elmt));
1149             Next_Elmt (Elmt);
1150          end loop;
1151       end if;
1152    end Add_Interface_Tag_Components;
1153
1154    -----------------------------------
1155    -- Analyze_Component_Declaration --
1156    -----------------------------------
1157
1158    procedure Analyze_Component_Declaration (N : Node_Id) is
1159       Id : constant Entity_Id := Defining_Identifier (N);
1160       T  : Entity_Id;
1161       P  : Entity_Id;
1162
1163       function Contains_POC (Constr : Node_Id) return Boolean;
1164       --  Determines whether a constraint uses the discriminant of a record
1165       --  type thus becoming a per-object constraint (POC).
1166
1167       ------------------
1168       -- Contains_POC --
1169       ------------------
1170
1171       function Contains_POC (Constr : Node_Id) return Boolean is
1172       begin
1173          case Nkind (Constr) is
1174             when N_Attribute_Reference =>
1175                return Attribute_Name (Constr) = Name_Access
1176                         and
1177                       Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1178
1179             when N_Discriminant_Association =>
1180                return Denotes_Discriminant (Expression (Constr));
1181
1182             when N_Identifier =>
1183                return Denotes_Discriminant (Constr);
1184
1185             when N_Index_Or_Discriminant_Constraint =>
1186                declare
1187                   IDC : Node_Id := First (Constraints (Constr));
1188
1189                begin
1190                   while Present (IDC) loop
1191
1192                      --  One per-object constraint is sufficent
1193
1194                      if Contains_POC (IDC) then
1195                         return True;
1196                      end if;
1197
1198                      Next (IDC);
1199                   end loop;
1200
1201                   return False;
1202                end;
1203
1204             when N_Range =>
1205                return Denotes_Discriminant (Low_Bound (Constr))
1206                         or else
1207                       Denotes_Discriminant (High_Bound (Constr));
1208
1209             when N_Range_Constraint =>
1210                return Denotes_Discriminant (Range_Expression (Constr));
1211
1212             when others =>
1213                return False;
1214
1215          end case;
1216       end Contains_POC;
1217
1218    --  Start of processing for Analyze_Component_Declaration
1219
1220    begin
1221       Generate_Definition (Id);
1222       Enter_Name (Id);
1223
1224       if Present (Subtype_Indication (Component_Definition (N))) then
1225          T := Find_Type_Of_Object
1226                 (Subtype_Indication (Component_Definition (N)), N);
1227
1228       --  Ada 2005 (AI-230): Access Definition case
1229
1230       else
1231          pragma Assert (Present
1232                           (Access_Definition (Component_Definition (N))));
1233
1234          T := Access_Definition
1235                 (Related_Nod => N,
1236                  N => Access_Definition (Component_Definition (N)));
1237          Set_Is_Local_Anonymous_Access (T);
1238
1239          --  Ada 2005 (AI-254)
1240
1241          if Present (Access_To_Subprogram_Definition
1242                       (Access_Definition (Component_Definition (N))))
1243            and then Protected_Present (Access_To_Subprogram_Definition
1244                                         (Access_Definition
1245                                           (Component_Definition (N))))
1246          then
1247             T := Replace_Anonymous_Access_To_Protected_Subprogram (N, T);
1248          end if;
1249       end if;
1250
1251       --  If the subtype is a constrained subtype of the enclosing record,
1252       --  (which must have a partial view) the back-end does not handle
1253       --  properly the recursion. Rewrite the component declaration with an
1254       --  explicit subtype indication, which is acceptable to Gigi. We can copy
1255       --  the tree directly because side effects have already been removed from
1256       --  discriminant constraints.
1257
1258       if Ekind (T) = E_Access_Subtype
1259         and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1260         and then Comes_From_Source (T)
1261         and then Nkind (Parent (T)) = N_Subtype_Declaration
1262         and then Etype (Directly_Designated_Type (T)) = Current_Scope
1263       then
1264          Rewrite
1265            (Subtype_Indication (Component_Definition (N)),
1266              New_Copy_Tree (Subtype_Indication (Parent (T))));
1267          T := Find_Type_Of_Object
1268                  (Subtype_Indication (Component_Definition (N)), N);
1269       end if;
1270
1271       --  If the component declaration includes a default expression, then we
1272       --  check that the component is not of a limited type (RM 3.7(5)),
1273       --  and do the special preanalysis of the expression (see section on
1274       --  "Handling of Default and Per-Object Expressions" in the spec of
1275       --  package Sem).
1276
1277       if Present (Expression (N)) then
1278          Analyze_Per_Use_Expression (Expression (N), T);
1279          Check_Initialization (T, Expression (N));
1280       end if;
1281
1282       --  The parent type may be a private view with unknown discriminants,
1283       --  and thus unconstrained. Regular components must be constrained.
1284
1285       if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1286          if Is_Class_Wide_Type (T) then
1287             Error_Msg_N
1288                ("class-wide subtype with unknown discriminants" &
1289                  " in component declaration",
1290                  Subtype_Indication (Component_Definition (N)));
1291          else
1292             Error_Msg_N
1293               ("unconstrained subtype in component declaration",
1294                Subtype_Indication (Component_Definition (N)));
1295          end if;
1296
1297       --  Components cannot be abstract, except for the special case of
1298       --  the _Parent field (case of extending an abstract tagged type)
1299
1300       elsif Is_Abstract (T) and then Chars (Id) /= Name_uParent then
1301          Error_Msg_N ("type of a component cannot be abstract", N);
1302       end if;
1303
1304       Set_Etype (Id, T);
1305       Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1306
1307       --  The component declaration may have a per-object constraint, set
1308       --  the appropriate flag in the defining identifier of the subtype.
1309
1310       if Present (Subtype_Indication (Component_Definition (N))) then
1311          declare
1312             Sindic : constant Node_Id :=
1313                        Subtype_Indication (Component_Definition (N));
1314
1315          begin
1316             if Nkind (Sindic) = N_Subtype_Indication
1317               and then Present (Constraint (Sindic))
1318               and then Contains_POC (Constraint (Sindic))
1319             then
1320                Set_Has_Per_Object_Constraint (Id);
1321             end if;
1322          end;
1323       end if;
1324
1325       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1326       --  out some static checks.
1327
1328       if Ada_Version >= Ada_05
1329         and then (Null_Exclusion_Present (Component_Definition (N))
1330                     or else Can_Never_Be_Null (T))
1331       then
1332          Set_Can_Never_Be_Null (Id);
1333          Null_Exclusion_Static_Checks (N);
1334       end if;
1335
1336       --  If this component is private (or depends on a private type), flag the
1337       --  record type to indicate that some operations are not available.
1338
1339       P := Private_Component (T);
1340
1341       if Present (P) then
1342          --  Check for circular definitions
1343
1344          if P = Any_Type then
1345             Set_Etype (Id, Any_Type);
1346
1347          --  There is a gap in the visibility of operations only if the
1348          --  component type is not defined in the scope of the record type.
1349
1350          elsif Scope (P) = Scope (Current_Scope) then
1351             null;
1352
1353          elsif Is_Limited_Type (P) then
1354             Set_Is_Limited_Composite (Current_Scope);
1355
1356          else
1357             Set_Is_Private_Composite (Current_Scope);
1358          end if;
1359       end if;
1360
1361       if P /= Any_Type
1362         and then Is_Limited_Type (T)
1363         and then Chars (Id) /= Name_uParent
1364         and then Is_Tagged_Type (Current_Scope)
1365       then
1366          if Is_Derived_Type (Current_Scope)
1367            and then not Is_Limited_Record (Root_Type (Current_Scope))
1368          then
1369             Error_Msg_N
1370               ("extension of nonlimited type cannot have limited components",
1371                N);
1372             Explain_Limited_Type (T, N);
1373             Set_Etype (Id, Any_Type);
1374             Set_Is_Limited_Composite (Current_Scope, False);
1375
1376          elsif not Is_Derived_Type (Current_Scope)
1377            and then not Is_Limited_Record (Current_Scope)
1378          then
1379             Error_Msg_N
1380               ("nonlimited tagged type cannot have limited components", N);
1381             Explain_Limited_Type (T, N);
1382             Set_Etype (Id, Any_Type);
1383             Set_Is_Limited_Composite (Current_Scope, False);
1384          end if;
1385       end if;
1386
1387       Set_Original_Record_Component (Id, Id);
1388    end Analyze_Component_Declaration;
1389
1390    --------------------------
1391    -- Analyze_Declarations --
1392    --------------------------
1393
1394    procedure Analyze_Declarations (L : List_Id) is
1395       D           : Node_Id;
1396       Next_Node   : Node_Id;
1397       Freeze_From : Entity_Id := Empty;
1398
1399       procedure Adjust_D;
1400       --  Adjust D not to include implicit label declarations, since these
1401       --  have strange Sloc values that result in elaboration check problems.
1402       --  (They have the sloc of the label as found in the source, and that
1403       --  is ahead of the current declarative part).
1404
1405       --------------
1406       -- Adjust_D --
1407       --------------
1408
1409       procedure Adjust_D is
1410       begin
1411          while Present (Prev (D))
1412            and then Nkind (D) = N_Implicit_Label_Declaration
1413          loop
1414             Prev (D);
1415          end loop;
1416       end Adjust_D;
1417
1418    --  Start of processing for Analyze_Declarations
1419
1420    begin
1421       D := First (L);
1422       while Present (D) loop
1423
1424          --  Complete analysis of declaration
1425
1426          Analyze (D);
1427          Next_Node := Next (D);
1428
1429          if No (Freeze_From) then
1430             Freeze_From := First_Entity (Current_Scope);
1431          end if;
1432
1433          --  At the end of a declarative part, freeze remaining entities
1434          --  declared in it. The end of the visible declarations of package
1435          --  specification is not the end of a declarative part if private
1436          --  declarations are present. The end of a package declaration is a
1437          --  freezing point only if it a library package. A task definition or
1438          --  protected type definition is not a freeze point either. Finally,
1439          --  we do not freeze entities in generic scopes, because there is no
1440          --  code generated for them and freeze nodes will be generated for
1441          --  the instance.
1442
1443          --  The end of a package instantiation is not a freeze point, but
1444          --  for now we make it one, because the generic body is inserted
1445          --  (currently) immediately after. Generic instantiations will not
1446          --  be a freeze point once delayed freezing of bodies is implemented.
1447          --  (This is needed in any case for early instantiations ???).
1448
1449          if No (Next_Node) then
1450             if Nkind (Parent (L)) = N_Component_List
1451               or else Nkind (Parent (L)) = N_Task_Definition
1452               or else Nkind (Parent (L)) = N_Protected_Definition
1453             then
1454                null;
1455
1456             elsif Nkind (Parent (L)) /= N_Package_Specification then
1457                if Nkind (Parent (L)) = N_Package_Body then
1458                   Freeze_From := First_Entity (Current_Scope);
1459                end if;
1460
1461                Adjust_D;
1462                Freeze_All (Freeze_From, D);
1463                Freeze_From := Last_Entity (Current_Scope);
1464
1465             elsif Scope (Current_Scope) /= Standard_Standard
1466               and then not Is_Child_Unit (Current_Scope)
1467               and then No (Generic_Parent (Parent (L)))
1468             then
1469                null;
1470
1471             elsif L /= Visible_Declarations (Parent (L))
1472                or else No (Private_Declarations (Parent (L)))
1473                or else Is_Empty_List (Private_Declarations (Parent (L)))
1474             then
1475                Adjust_D;
1476                Freeze_All (Freeze_From, D);
1477                Freeze_From := Last_Entity (Current_Scope);
1478             end if;
1479
1480          --  If next node is a body then freeze all types before the body.
1481          --  An exception occurs for expander generated bodies, which can
1482          --  be recognized by their already being analyzed. The expander
1483          --  ensures that all types needed by these bodies have been frozen
1484          --  but it is not necessary to freeze all types (and would be wrong
1485          --  since it would not correspond to an RM defined freeze point).
1486
1487          elsif not Analyzed (Next_Node)
1488            and then (Nkind (Next_Node) = N_Subprogram_Body
1489              or else Nkind (Next_Node) = N_Entry_Body
1490              or else Nkind (Next_Node) = N_Package_Body
1491              or else Nkind (Next_Node) = N_Protected_Body
1492              or else Nkind (Next_Node) = N_Task_Body
1493              or else Nkind (Next_Node) in N_Body_Stub)
1494          then
1495             Adjust_D;
1496             Freeze_All (Freeze_From, D);
1497             Freeze_From := Last_Entity (Current_Scope);
1498          end if;
1499
1500          D := Next_Node;
1501       end loop;
1502    end Analyze_Declarations;
1503
1504    ----------------------------------
1505    -- Analyze_Incomplete_Type_Decl --
1506    ----------------------------------
1507
1508    procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
1509       F : constant Boolean := Is_Pure (Current_Scope);
1510       T : Entity_Id;
1511
1512    begin
1513       Generate_Definition (Defining_Identifier (N));
1514
1515       --  Process an incomplete declaration. The identifier must not have been
1516       --  declared already in the scope. However, an incomplete declaration may
1517       --  appear in the private part of a package, for a private type that has
1518       --  already been declared.
1519
1520       --  In this case, the discriminants (if any) must match
1521
1522       T := Find_Type_Name (N);
1523
1524       Set_Ekind (T, E_Incomplete_Type);
1525       Init_Size_Align (T);
1526       Set_Is_First_Subtype (T, True);
1527       Set_Etype (T, T);
1528       New_Scope (T);
1529
1530       Set_Stored_Constraint (T, No_Elist);
1531
1532       if Present (Discriminant_Specifications (N)) then
1533          Process_Discriminants (N);
1534       end if;
1535
1536       End_Scope;
1537
1538       --  If the type has discriminants, non-trivial subtypes may be be
1539       --  declared before the full view of the type. The full views of those
1540       --  subtypes will be built after the full view of the type.
1541
1542       Set_Private_Dependents (T, New_Elmt_List);
1543       Set_Is_Pure (T, F);
1544    end Analyze_Incomplete_Type_Decl;
1545
1546    -----------------------------
1547    -- Analyze_Itype_Reference --
1548    -----------------------------
1549
1550    --  Nothing to do. This node is placed in the tree only for the benefit
1551    --  of Gigi processing, and has no effect on the semantic processing.
1552
1553    procedure Analyze_Itype_Reference (N : Node_Id) is
1554    begin
1555       pragma Assert (Is_Itype (Itype (N)));
1556       null;
1557    end Analyze_Itype_Reference;
1558
1559    --------------------------------
1560    -- Analyze_Number_Declaration --
1561    --------------------------------
1562
1563    procedure Analyze_Number_Declaration (N : Node_Id) is
1564       Id    : constant Entity_Id := Defining_Identifier (N);
1565       E     : constant Node_Id   := Expression (N);
1566       T     : Entity_Id;
1567       Index : Interp_Index;
1568       It    : Interp;
1569
1570    begin
1571       Generate_Definition (Id);
1572       Enter_Name (Id);
1573
1574       --  This is an optimization of a common case of an integer literal
1575
1576       if Nkind (E) = N_Integer_Literal then
1577          Set_Is_Static_Expression (E, True);
1578          Set_Etype                (E, Universal_Integer);
1579
1580          Set_Etype     (Id, Universal_Integer);
1581          Set_Ekind     (Id, E_Named_Integer);
1582          Set_Is_Frozen (Id, True);
1583          return;
1584       end if;
1585
1586       Set_Is_Pure (Id, Is_Pure (Current_Scope));
1587
1588       --  Process expression, replacing error by integer zero, to avoid
1589       --  cascaded errors or aborts further along in the processing
1590
1591       --  Replace Error by integer zero, which seems least likely to
1592       --  cause cascaded errors.
1593
1594       if E = Error then
1595          Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
1596          Set_Error_Posted (E);
1597       end if;
1598
1599       Analyze (E);
1600
1601       --  Verify that the expression is static and numeric. If
1602       --  the expression is overloaded, we apply the preference
1603       --  rule that favors root numeric types.
1604
1605       if not Is_Overloaded (E) then
1606          T := Etype (E);
1607
1608       else
1609          T := Any_Type;
1610          Get_First_Interp (E, Index, It);
1611
1612          while Present (It.Typ) loop
1613             if (Is_Integer_Type (It.Typ)
1614                  or else Is_Real_Type (It.Typ))
1615               and then (Scope (Base_Type (It.Typ))) = Standard_Standard
1616             then
1617                if T = Any_Type then
1618                   T := It.Typ;
1619
1620                elsif It.Typ = Universal_Real
1621                  or else It.Typ = Universal_Integer
1622                then
1623                   --  Choose universal interpretation over any other
1624
1625                   T := It.Typ;
1626                   exit;
1627                end if;
1628             end if;
1629
1630             Get_Next_Interp (Index, It);
1631          end loop;
1632       end if;
1633
1634       if Is_Integer_Type (T)  then
1635          Resolve (E, T);
1636          Set_Etype (Id, Universal_Integer);
1637          Set_Ekind (Id, E_Named_Integer);
1638
1639       elsif Is_Real_Type (T) then
1640
1641          --  Because the real value is converted to universal_real, this
1642          --  is a legal context for a universal fixed expression.
1643
1644          if T = Universal_Fixed then
1645             declare
1646                Loc  : constant Source_Ptr := Sloc (N);
1647                Conv : constant Node_Id := Make_Type_Conversion (Loc,
1648                         Subtype_Mark =>
1649                           New_Occurrence_Of (Universal_Real, Loc),
1650                         Expression => Relocate_Node (E));
1651
1652             begin
1653                Rewrite (E, Conv);
1654                Analyze (E);
1655             end;
1656
1657          elsif T = Any_Fixed then
1658             Error_Msg_N ("illegal context for mixed mode operation", E);
1659
1660             --  Expression is of the form : universal_fixed * integer.
1661             --  Try to resolve as universal_real.
1662
1663             T := Universal_Real;
1664             Set_Etype (E, T);
1665          end if;
1666
1667          Resolve (E, T);
1668          Set_Etype (Id, Universal_Real);
1669          Set_Ekind (Id, E_Named_Real);
1670
1671       else
1672          Wrong_Type (E, Any_Numeric);
1673          Resolve (E, T);
1674
1675          Set_Etype               (Id, T);
1676          Set_Ekind               (Id, E_Constant);
1677          Set_Never_Set_In_Source (Id, True);
1678          Set_Is_True_Constant    (Id, True);
1679          return;
1680       end if;
1681
1682       if Nkind (E) = N_Integer_Literal
1683         or else Nkind (E) = N_Real_Literal
1684       then
1685          Set_Etype (E, Etype (Id));
1686       end if;
1687
1688       if not Is_OK_Static_Expression (E) then
1689          Flag_Non_Static_Expr
1690            ("non-static expression used in number declaration!", E);
1691          Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
1692          Set_Etype (E, Any_Type);
1693       end if;
1694    end Analyze_Number_Declaration;
1695
1696    --------------------------------
1697    -- Analyze_Object_Declaration --
1698    --------------------------------
1699
1700    procedure Analyze_Object_Declaration (N : Node_Id) is
1701       Loc   : constant Source_Ptr := Sloc (N);
1702       Id    : constant Entity_Id  := Defining_Identifier (N);
1703       T     : Entity_Id;
1704       Act_T : Entity_Id;
1705
1706       E : Node_Id := Expression (N);
1707       --  E is set to Expression (N) throughout this routine. When
1708       --  Expression (N) is modified, E is changed accordingly.
1709
1710       Prev_Entity : Entity_Id := Empty;
1711
1712       function Build_Default_Subtype return Entity_Id;
1713       --  If the object is limited or aliased, and if the type is unconstrained
1714       --  and there is no expression, the discriminants cannot be modified and
1715       --  the subtype of the object is constrained by the defaults, so it is
1716       --  worthile building the corresponding subtype.
1717
1718       function Count_Tasks (T : Entity_Id) return Uint;
1719       --  This function is called when a library level object of type is
1720       --  declared. It's function is to count the static number of tasks
1721       --  declared within the type (it is only called if Has_Tasks is set for
1722       --  T). As a side effect, if an array of tasks with non-static bounds or
1723       --  a variant record type is encountered, Check_Restrictions is called
1724       --  indicating the count is unknown.
1725
1726       ---------------------------
1727       -- Build_Default_Subtype --
1728       ---------------------------
1729
1730       function Build_Default_Subtype return Entity_Id is
1731          Constraints : constant List_Id := New_List;
1732          Act         : Entity_Id;
1733          Decl        : Node_Id;
1734          Disc        : Entity_Id;
1735
1736       begin
1737          Disc  := First_Discriminant (T);
1738
1739          if No (Discriminant_Default_Value (Disc)) then
1740             return T;   --   previous error.
1741          end if;
1742
1743          Act := Make_Defining_Identifier (Loc, New_Internal_Name ('S'));
1744          while Present (Disc) loop
1745             Append (
1746               New_Copy_Tree (
1747                 Discriminant_Default_Value (Disc)), Constraints);
1748             Next_Discriminant (Disc);
1749          end loop;
1750
1751          Decl :=
1752            Make_Subtype_Declaration (Loc,
1753              Defining_Identifier => Act,
1754              Subtype_Indication =>
1755                Make_Subtype_Indication (Loc,
1756                  Subtype_Mark => New_Occurrence_Of (T, Loc),
1757                  Constraint =>
1758                    Make_Index_Or_Discriminant_Constraint
1759                      (Loc, Constraints)));
1760
1761          Insert_Before (N, Decl);
1762          Analyze (Decl);
1763          return Act;
1764       end Build_Default_Subtype;
1765
1766       -----------------
1767       -- Count_Tasks --
1768       -----------------
1769
1770       function Count_Tasks (T : Entity_Id) return Uint is
1771          C : Entity_Id;
1772          X : Node_Id;
1773          V : Uint;
1774
1775       begin
1776          if Is_Task_Type (T) then
1777             return Uint_1;
1778
1779          elsif Is_Record_Type (T) then
1780             if Has_Discriminants (T) then
1781                Check_Restriction (Max_Tasks, N);
1782                return Uint_0;
1783
1784             else
1785                V := Uint_0;
1786                C := First_Component (T);
1787                while Present (C) loop
1788                   V := V + Count_Tasks (Etype (C));
1789                   Next_Component (C);
1790                end loop;
1791
1792                return V;
1793             end if;
1794
1795          elsif Is_Array_Type (T) then
1796             X := First_Index (T);
1797             V := Count_Tasks (Component_Type (T));
1798             while Present (X) loop
1799                C := Etype (X);
1800
1801                if not Is_Static_Subtype (C) then
1802                   Check_Restriction (Max_Tasks, N);
1803                   return Uint_0;
1804                else
1805                   V := V * (UI_Max (Uint_0,
1806                                     Expr_Value (Type_High_Bound (C)) -
1807                                     Expr_Value (Type_Low_Bound (C)) + Uint_1));
1808                end if;
1809
1810                Next_Index (X);
1811             end loop;
1812
1813             return V;
1814
1815          else
1816             return Uint_0;
1817          end if;
1818       end Count_Tasks;
1819
1820    --  Start of processing for Analyze_Object_Declaration
1821
1822    begin
1823       --  There are three kinds of implicit types generated by an
1824       --  object declaration:
1825
1826       --   1. Those for generated by the original Object Definition
1827
1828       --   2. Those generated by the Expression
1829
1830       --   3. Those used to constrained the Object Definition with the
1831       --       expression constraints when it is unconstrained
1832
1833       --  They must be generated in this order to avoid order of elaboration
1834       --  issues. Thus the first step (after entering the name) is to analyze
1835       --  the object definition.
1836
1837       if Constant_Present (N) then
1838          Prev_Entity := Current_Entity_In_Scope (Id);
1839
1840          --  If homograph is an implicit subprogram, it is overridden by the
1841          --  current declaration.
1842
1843          if Present (Prev_Entity)
1844            and then Is_Overloadable (Prev_Entity)
1845            and then Is_Inherited_Operation (Prev_Entity)
1846          then
1847             Prev_Entity := Empty;
1848          end if;
1849       end if;
1850
1851       if Present (Prev_Entity) then
1852          Constant_Redeclaration (Id, N, T);
1853
1854          Generate_Reference (Prev_Entity, Id, 'c');
1855          Set_Completion_Referenced (Id);
1856
1857          if Error_Posted (N) then
1858
1859             --  Type mismatch or illegal redeclaration, Do not analyze
1860             --  expression to avoid cascaded errors.
1861
1862             T := Find_Type_Of_Object (Object_Definition (N), N);
1863             Set_Etype (Id, T);
1864             Set_Ekind (Id, E_Variable);
1865             return;
1866          end if;
1867
1868       --  In the normal case, enter identifier at the start to catch
1869       --  premature usage in the initialization expression.
1870
1871       else
1872          Generate_Definition (Id);
1873          Enter_Name (Id);
1874
1875          T := Find_Type_Of_Object (Object_Definition (N), N);
1876
1877          if Error_Posted (Id) then
1878             Set_Etype (Id, T);
1879             Set_Ekind (Id, E_Variable);
1880             return;
1881          end if;
1882       end if;
1883
1884       --  Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1885       --  out some static checks
1886
1887       if Ada_Version >= Ada_05
1888         and then (Null_Exclusion_Present (N)
1889                     or else Can_Never_Be_Null (T))
1890       then
1891          Set_Can_Never_Be_Null (Id);
1892          Null_Exclusion_Static_Checks (N);
1893       end if;
1894
1895       Set_Is_Pure (Id, Is_Pure (Current_Scope));
1896
1897       --  If deferred constant, make sure context is appropriate. We detect
1898       --  a deferred constant as a constant declaration with no expression.
1899       --  A deferred constant can appear in a package body if its completion
1900       --  is by means of an interface pragma.
1901
1902       if Constant_Present (N)
1903         and then No (E)
1904       then
1905          if not Is_Package (Current_Scope) then
1906             Error_Msg_N
1907               ("invalid context for deferred constant declaration ('R'M 7.4)",
1908                 N);
1909             Error_Msg_N
1910               ("\declaration requires an initialization expression",
1911                 N);
1912             Set_Constant_Present (N, False);
1913
1914          --  In Ada 83, deferred constant must be of private type
1915
1916          elsif not Is_Private_Type (T) then
1917             if Ada_Version = Ada_83 and then Comes_From_Source (N) then
1918                Error_Msg_N
1919                  ("(Ada 83) deferred constant must be private type", N);
1920             end if;
1921          end if;
1922
1923       --  If not a deferred constant, then object declaration freezes its type
1924
1925       else
1926          Check_Fully_Declared (T, N);
1927          Freeze_Before (N, T);
1928       end if;
1929
1930       --  If the object was created by a constrained array definition, then
1931       --  set the link in both the anonymous base type and anonymous subtype
1932       --  that are built to represent the array type to point to the object.
1933
1934       if Nkind (Object_Definition (Declaration_Node (Id))) =
1935                         N_Constrained_Array_Definition
1936       then
1937          Set_Related_Array_Object (T, Id);
1938          Set_Related_Array_Object (Base_Type (T), Id);
1939       end if;
1940
1941       --  Special checks for protected objects not at library level
1942
1943       if Is_Protected_Type (T)
1944         and then not Is_Library_Level_Entity (Id)
1945       then
1946          Check_Restriction (No_Local_Protected_Objects, Id);
1947
1948          --  Protected objects with interrupt handlers must be at library level
1949
1950          --  Ada 2005: this test is not needed (and the corresponding clause
1951          --  in the RM is removed) because accessibility checks are sufficient
1952          --  to make handlers not at the library level illegal.
1953
1954          if Has_Interrupt_Handler (T)
1955            and then Ada_Version < Ada_05
1956          then
1957             Error_Msg_N
1958               ("interrupt object can only be declared at library level", Id);
1959          end if;
1960       end if;
1961
1962       --  The actual subtype of the object is the nominal subtype, unless
1963       --  the nominal one is unconstrained and obtained from the expression.
1964
1965       Act_T := T;
1966
1967       --  Process initialization expression if present and not in error
1968
1969       if Present (E) and then E /= Error then
1970          Analyze (E);
1971
1972          --  In case of errors detected in the analysis of the expression,
1973          --  decorate it with the expected type to avoid cascade errors
1974
1975          if not Present (Etype (E)) then
1976             Set_Etype (E, T);
1977          end if;
1978
1979          --  If an initialization expression is present, then we set the
1980          --  Is_True_Constant flag. It will be reset if this is a variable
1981          --  and it is indeed modified.
1982
1983          Set_Is_True_Constant (Id, True);
1984
1985          --  If we are analyzing a constant declaration, set its completion
1986          --  flag after analyzing the expression.
1987
1988          if Constant_Present (N) then
1989             Set_Has_Completion (Id);
1990          end if;
1991
1992          if not Assignment_OK (N) then
1993             Check_Initialization (T, E);
1994          end if;
1995
1996          Set_Etype (Id, T);             --  may be overridden later on
1997          Resolve (E, T);
1998          Check_Unset_Reference (E);
1999
2000          if Compile_Time_Known_Value (E) then
2001             Set_Current_Value (Id, E);
2002          end if;
2003
2004          --  Check incorrect use of dynamically tagged expressions. Note
2005          --  the use of Is_Tagged_Type (T) which seems redundant but is in
2006          --  fact important to avoid spurious errors due to expanded code
2007          --  for dispatching functions over an anonymous access type
2008
2009          if (Is_Class_Wide_Type (Etype (E)) or else Is_Dynamically_Tagged (E))
2010            and then Is_Tagged_Type (T)
2011            and then not Is_Class_Wide_Type (T)
2012          then
2013             Error_Msg_N ("dynamically tagged expression not allowed!", E);
2014          end if;
2015
2016          Apply_Scalar_Range_Check (E, T);
2017          Apply_Static_Length_Check (E, T);
2018       end if;
2019
2020       --  If the No_Streams restriction is set, check that the type of the
2021       --  object is not, and does not contain, any subtype derived from
2022       --  Ada.Streams.Root_Stream_Type. Note that we guard the call to
2023       --  Has_Stream just for efficiency reasons. There is no point in
2024       --  spending time on a Has_Stream check if the restriction is not set.
2025
2026       if Restrictions.Set (No_Streams) then
2027          if Has_Stream (T) then
2028             Check_Restriction (No_Streams, N);
2029          end if;
2030       end if;
2031
2032       --  Abstract type is never permitted for a variable or constant.
2033       --  Note: we inhibit this check for objects that do not come from
2034       --  source because there is at least one case (the expansion of
2035       --  x'class'input where x is abstract) where we legitimately
2036       --  generate an abstract object.
2037
2038       if Is_Abstract (T) and then Comes_From_Source (N) then
2039          Error_Msg_N ("type of object cannot be abstract",
2040                       Object_Definition (N));
2041
2042          if Is_CPP_Class (T) then
2043             Error_Msg_NE ("\} may need a cpp_constructor",
2044               Object_Definition (N), T);
2045          end if;
2046
2047       --  Case of unconstrained type
2048
2049       elsif Is_Indefinite_Subtype (T) then
2050
2051          --  Nothing to do in deferred constant case
2052
2053          if Constant_Present (N) and then No (E) then
2054             null;
2055
2056          --  Case of no initialization present
2057
2058          elsif No (E) then
2059             if No_Initialization (N) then
2060                null;
2061
2062             elsif Is_Class_Wide_Type (T) then
2063                Error_Msg_N
2064                  ("initialization required in class-wide declaration ", N);
2065
2066             else
2067                Error_Msg_N
2068                  ("unconstrained subtype not allowed (need initialization)",
2069                   Object_Definition (N));
2070             end if;
2071
2072          --  Case of initialization present but in error. Set initial
2073          --  expression as absent (but do not make above complaints)
2074
2075          elsif E = Error then
2076             Set_Expression (N, Empty);
2077             E := Empty;
2078
2079          --  Case of initialization present
2080
2081          else
2082             --  Not allowed in Ada 83
2083
2084             if not Constant_Present (N) then
2085                if Ada_Version = Ada_83
2086                  and then Comes_From_Source (Object_Definition (N))
2087                then
2088                   Error_Msg_N
2089                     ("(Ada 83) unconstrained variable not allowed",
2090                      Object_Definition (N));
2091                end if;
2092             end if;
2093
2094             --  Now we constrain the variable from the initializing expression
2095
2096             --  If the expression is an aggregate, it has been expanded into
2097             --  individual assignments. Retrieve the actual type from the
2098             --  expanded construct.
2099
2100             if Is_Array_Type (T)
2101               and then No_Initialization (N)
2102               and then Nkind (Original_Node (E)) = N_Aggregate
2103             then
2104                Act_T := Etype (E);
2105
2106             else
2107                Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
2108                Act_T := Find_Type_Of_Object (Object_Definition (N), N);
2109             end if;
2110
2111             Set_Is_Constr_Subt_For_U_Nominal (Act_T);
2112
2113             if Aliased_Present (N) then
2114                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2115             end if;
2116
2117             Freeze_Before (N, Act_T);
2118             Freeze_Before (N, T);
2119          end if;
2120
2121       elsif Is_Array_Type (T)
2122         and then No_Initialization (N)
2123         and then Nkind (Original_Node (E)) = N_Aggregate
2124       then
2125          if not Is_Entity_Name (Object_Definition (N)) then
2126             Act_T := Etype (E);
2127             Check_Compile_Time_Size (Act_T);
2128
2129             if Aliased_Present (N) then
2130                Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2131             end if;
2132          end if;
2133
2134          --  When the given object definition and the aggregate are specified
2135          --  independently, and their lengths might differ do a length check.
2136          --  This cannot happen if the aggregate is of the form (others =>...)
2137
2138          if not Is_Constrained (T) then
2139             null;
2140
2141          elsif Nkind (E) = N_Raise_Constraint_Error then
2142
2143             --  Aggregate is statically illegal. Place back in declaration
2144
2145             Set_Expression (N, E);
2146             Set_No_Initialization (N, False);
2147
2148          elsif T = Etype (E) then
2149             null;
2150
2151          elsif Nkind (E) = N_Aggregate
2152            and then Present (Component_Associations (E))
2153            and then Present (Choices (First (Component_Associations (E))))
2154            and then Nkind (First
2155             (Choices (First (Component_Associations (E))))) = N_Others_Choice
2156          then
2157             null;
2158
2159          else
2160             Apply_Length_Check (E, T);
2161          end if;
2162
2163       elsif (Is_Limited_Record (T)
2164                or else Is_Concurrent_Type (T))
2165         and then not Is_Constrained (T)
2166         and then Has_Discriminants (T)
2167       then
2168          Act_T := Build_Default_Subtype;
2169          Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
2170
2171       elsif not Is_Constrained (T)
2172         and then Has_Discriminants (T)
2173         and then Constant_Present (N)
2174         and then Nkind (E) = N_Function_Call
2175       then
2176          --  The back-end has problems with constants of a discriminated type
2177          --  with defaults, if the initial value is a function call. We
2178          --  generate an intermediate temporary for the result of the call.
2179          --  It is unclear why this should make it acceptable to gcc. ???
2180
2181          Remove_Side_Effects (E);
2182       end if;
2183
2184       if T = Standard_Wide_Character or else T = Standard_Wide_Wide_Character
2185         or else Root_Type (T) = Standard_Wide_String
2186         or else Root_Type (T) = Standard_Wide_Wide_String
2187       then
2188          Check_Restriction (No_Wide_Characters, Object_Definition (N));
2189       end if;
2190
2191       --  Now establish the proper kind and type of the object
2192
2193       if Constant_Present (N) then
2194          Set_Ekind               (Id, E_Constant);
2195          Set_Never_Set_In_Source (Id, True);
2196          Set_Is_True_Constant    (Id, True);
2197
2198       else
2199          Set_Ekind (Id, E_Variable);
2200
2201          --  A variable is set as shared passive if it appears in a shared
2202          --  passive package, and is at the outer level. This is not done
2203          --  for entities generated during expansion, because those are
2204          --  always manipulated locally.
2205
2206          if Is_Shared_Passive (Current_Scope)
2207            and then Is_Library_Level_Entity (Id)
2208            and then Comes_From_Source (Id)
2209          then
2210             Set_Is_Shared_Passive (Id);
2211             Check_Shared_Var (Id, T, N);
2212          end if;
2213
2214          --  Case of no initializing expression present. If the type is not
2215          --  fully initialized, then we set Never_Set_In_Source, since this
2216          --  is a case of a potentially uninitialized object. Note that we
2217          --  do not consider access variables to be fully initialized for
2218          --  this purpose, since it still seems dubious if someone declares
2219
2220          --  Note that we only do this for source declarations. If the object
2221          --  is declared by a generated declaration, we assume that it is not
2222          --  appropriate to generate warnings in that case.
2223
2224          if No (E) then
2225             if (Is_Access_Type (T)
2226                  or else not Is_Fully_Initialized_Type (T))
2227               and then Comes_From_Source (N)
2228             then
2229                Set_Never_Set_In_Source (Id);
2230             end if;
2231          end if;
2232       end if;
2233
2234       Init_Alignment (Id);
2235       Init_Esize     (Id);
2236
2237       if Aliased_Present (N) then
2238          Set_Is_Aliased (Id);
2239
2240          if No (E)
2241            and then Is_Record_Type (T)
2242            and then not Is_Constrained (T)
2243            and then Has_Discriminants (T)
2244          then
2245             Set_Actual_Subtype (Id, Build_Default_Subtype);
2246          end if;
2247       end if;
2248
2249       Set_Etype (Id, Act_T);
2250
2251       if Has_Controlled_Component (Etype (Id))
2252         or else Is_Controlled (Etype (Id))
2253       then
2254          if not Is_Library_Level_Entity (Id) then
2255             Check_Restriction (No_Nested_Finalization, N);
2256          else
2257             Validate_Controlled_Object (Id);
2258          end if;
2259
2260          --  Generate a warning when an initialization causes an obvious
2261          --  ABE violation. If the init expression is a simple aggregate
2262          --  there shouldn't be any initialize/adjust call generated. This
2263          --  will be true as soon as aggregates are built in place when
2264          --  possible. ??? at the moment we do not generate warnings for
2265          --  temporaries created for those aggregates although a
2266          --  Program_Error might be generated if compiled with -gnato
2267
2268          if Is_Controlled (Etype (Id))
2269             and then Comes_From_Source (Id)
2270          then
2271             declare
2272                BT : constant Entity_Id := Base_Type (Etype (Id));
2273
2274                Implicit_Call : Entity_Id;
2275                pragma Warnings (Off, Implicit_Call);
2276                --  What is this about, it is never referenced ???
2277
2278                function Is_Aggr (N : Node_Id) return Boolean;
2279                --  Check that N is an aggregate
2280
2281                -------------
2282                -- Is_Aggr --
2283                -------------
2284
2285                function Is_Aggr (N : Node_Id) return Boolean is
2286                begin
2287                   case Nkind (Original_Node (N)) is
2288                      when N_Aggregate | N_Extension_Aggregate =>
2289                         return True;
2290
2291                      when N_Qualified_Expression |
2292                           N_Type_Conversion      |
2293                           N_Unchecked_Type_Conversion =>
2294                         return Is_Aggr (Expression (Original_Node (N)));
2295
2296                      when others =>
2297                         return False;
2298                   end case;
2299                end Is_Aggr;
2300
2301             begin
2302                --  If no underlying type, we already are in an error situation
2303                --  don't try to add a warning since we do not have access
2304                --  prim-op list.
2305
2306                if No (Underlying_Type (BT)) then
2307                   Implicit_Call := Empty;
2308
2309                --  A generic type does not have usable primitive operators.
2310                --  Initialization calls are built for instances.
2311
2312                elsif Is_Generic_Type (BT) then
2313                   Implicit_Call := Empty;
2314
2315                --  if the init expression is not an aggregate, an adjust
2316                --  call will be generated
2317
2318                elsif Present (E) and then not Is_Aggr (E) then
2319                   Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
2320
2321                --  if no init expression and we are not in the deferred
2322                --  constant case, an Initialize call will be generated
2323
2324                elsif No (E) and then not Constant_Present (N) then
2325                   Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
2326
2327                else
2328                   Implicit_Call := Empty;
2329                end if;
2330             end;
2331          end if;
2332       end if;
2333
2334       if Has_Task (Etype (Id)) then
2335          Check_Restriction (No_Tasking, N);
2336
2337          if Is_Library_Level_Entity (Id) then
2338             Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
2339          else
2340             Check_Restriction (Max_Tasks, N);
2341             Check_Restriction (No_Task_Hierarchy, N);
2342             Check_Potentially_Blocking_Operation (N);
2343          end if;
2344
2345          --  A rather specialized test. If we see two tasks being declared
2346          --  of the same type in the same object declaration, and the task
2347          --  has an entry with an address clause, we know that program error
2348          --  will be raised at run-time since we can't have two tasks with
2349          --  entries at the same address.
2350
2351          if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
2352             declare
2353                E : Entity_Id;
2354
2355             begin
2356                E := First_Entity (Etype (Id));
2357                while Present (E) loop
2358                   if Ekind (E) = E_Entry
2359                     and then Present (Get_Attribute_Definition_Clause
2360                                         (E, Attribute_Address))
2361                   then
2362                      Error_Msg_N
2363                        ("?more than one task with same entry address", N);
2364                      Error_Msg_N
2365                        ("\?Program_Error will be raised at run time", N);
2366                      Insert_Action (N,
2367                        Make_Raise_Program_Error (Loc,
2368                          Reason => PE_Duplicated_Entry_Address));
2369                      exit;
2370                   end if;
2371
2372                   Next_Entity (E);
2373                end loop;
2374             end;
2375          end if;
2376       end if;
2377
2378       --  Some simple constant-propagation: if the expression is a constant
2379       --  string initialized with a literal, share the literal. This avoids
2380       --  a run-time copy.
2381
2382       if Present (E)
2383         and then Is_Entity_Name (E)
2384         and then Ekind (Entity (E)) = E_Constant
2385         and then Base_Type (Etype (E)) = Standard_String
2386       then
2387          declare
2388             Val : constant Node_Id := Constant_Value (Entity (E));
2389          begin
2390             if Present (Val)
2391               and then Nkind (Val) = N_String_Literal
2392             then
2393                Rewrite (E, New_Copy (Val));
2394             end if;
2395          end;
2396       end if;
2397
2398       --  Another optimization: if the nominal subtype is unconstrained and
2399       --  the expression is a function call that returns an unconstrained
2400       --  type, rewrite the declaration as a renaming of the result of the
2401       --  call. The exceptions below are cases where the copy is expected,
2402       --  either by the back end (Aliased case) or by the semantics, as for
2403       --  initializing controlled types or copying tags for classwide types.
2404
2405       if Present (E)
2406         and then Nkind (E) = N_Explicit_Dereference
2407         and then Nkind (Original_Node (E)) = N_Function_Call
2408         and then not Is_Library_Level_Entity (Id)
2409         and then not Is_Constrained (T)
2410         and then not Is_Aliased (Id)
2411         and then not Is_Class_Wide_Type (T)
2412         and then not Is_Controlled (T)
2413         and then not Has_Controlled_Component (Base_Type (T))
2414         and then Expander_Active
2415       then
2416          Rewrite (N,
2417            Make_Object_Renaming_Declaration (Loc,
2418              Defining_Identifier => Id,
2419              Access_Definition   => Empty,
2420              Subtype_Mark        => New_Occurrence_Of
2421                                       (Base_Type (Etype (Id)), Loc),
2422              Name                => E));
2423
2424          Set_Renamed_Object (Id, E);
2425
2426          --  Force generation of debugging information for the constant
2427          --  and for the renamed function call.
2428
2429          Set_Needs_Debug_Info (Id);
2430          Set_Needs_Debug_Info (Entity (Prefix (E)));
2431       end if;
2432
2433       if Present (Prev_Entity)
2434         and then Is_Frozen (Prev_Entity)
2435         and then not Error_Posted (Id)
2436       then
2437          Error_Msg_N ("full constant declaration appears too late", N);
2438       end if;
2439
2440       Check_Eliminated (Id);
2441    end Analyze_Object_Declaration;
2442
2443    ---------------------------
2444    -- Analyze_Others_Choice --
2445    ---------------------------
2446
2447    --  Nothing to do for the others choice node itself, the semantic analysis
2448    --  of the others choice will occur as part of the processing of the parent
2449
2450    procedure Analyze_Others_Choice (N : Node_Id) is
2451       pragma Warnings (Off, N);
2452    begin
2453       null;
2454    end Analyze_Others_Choice;
2455
2456    --------------------------------
2457    -- Analyze_Per_Use_Expression --
2458    --------------------------------
2459
2460    procedure Analyze_Per_Use_Expression (N : Node_Id; T : Entity_Id) is
2461       Save_In_Default_Expression : constant Boolean := In_Default_Expression;
2462    begin
2463       In_Default_Expression := True;
2464       Pre_Analyze_And_Resolve (N, T);
2465       In_Default_Expression := Save_In_Default_Expression;
2466    end Analyze_Per_Use_Expression;
2467
2468    -------------------------------------------
2469    -- Analyze_Private_Extension_Declaration --
2470    -------------------------------------------
2471
2472    procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
2473       T           : constant Entity_Id := Defining_Identifier (N);
2474       Indic       : constant Node_Id   := Subtype_Indication (N);
2475       Parent_Type : Entity_Id;
2476       Parent_Base : Entity_Id;
2477
2478    begin
2479       --  Ada 2005 (AI-251): Decorate all the names in the list of ancestor
2480       --  interfaces
2481
2482       if Is_Non_Empty_List (Interface_List (N)) then
2483          declare
2484             I : Node_Id := First (Interface_List (N));
2485             T : Entity_Id;
2486          begin
2487             while Present (I) loop
2488                T := Find_Type_Of_Subtype_Indic (I);
2489
2490                if not Is_Interface (T) then
2491                   Error_Msg_NE ("(Ada 2005) & must be an interface", I, T);
2492                end if;
2493
2494                Next (I);
2495             end loop;
2496          end;
2497       end if;
2498
2499       Generate_Definition (T);
2500       Enter_Name (T);
2501
2502       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
2503       Parent_Base := Base_Type (Parent_Type);
2504
2505       if Parent_Type = Any_Type
2506         or else Etype (Parent_Type) = Any_Type
2507       then
2508          Set_Ekind (T, Ekind (Parent_Type));
2509          Set_Etype (T, Any_Type);
2510          return;
2511
2512       elsif not Is_Tagged_Type (Parent_Type) then
2513          Error_Msg_N
2514            ("parent of type extension must be a tagged type ", Indic);
2515          return;
2516
2517       elsif Ekind (Parent_Type) = E_Void
2518         or else Ekind (Parent_Type) = E_Incomplete_Type
2519       then
2520          Error_Msg_N ("premature derivation of incomplete type", Indic);
2521          return;
2522       end if;
2523
2524       --  Perhaps the parent type should be changed to the class-wide type's
2525       --  specific type in this case to prevent cascading errors ???
2526
2527       if Is_Class_Wide_Type (Parent_Type) then
2528          Error_Msg_N
2529            ("parent of type extension must not be a class-wide type", Indic);
2530          return;
2531       end if;
2532
2533       if (not Is_Package (Current_Scope)
2534            and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
2535         or else In_Private_Part (Current_Scope)
2536
2537       then
2538          Error_Msg_N ("invalid context for private extension", N);
2539       end if;
2540
2541       --  Set common attributes
2542
2543       Set_Is_Pure          (T, Is_Pure (Current_Scope));
2544       Set_Scope            (T, Current_Scope);
2545       Set_Ekind            (T, E_Record_Type_With_Private);
2546       Init_Size_Align      (T);
2547
2548       Set_Etype            (T,            Parent_Base);
2549       Set_Has_Task         (T, Has_Task  (Parent_Base));
2550
2551       Set_Convention       (T, Convention     (Parent_Type));
2552       Set_First_Rep_Item   (T, First_Rep_Item (Parent_Type));
2553       Set_Is_First_Subtype (T);
2554       Make_Class_Wide_Type (T);
2555
2556       if Unknown_Discriminants_Present (N) then
2557          Set_Discriminant_Constraint (T, No_Elist);
2558       end if;
2559
2560       Build_Derived_Record_Type (N, Parent_Type, T);
2561    end Analyze_Private_Extension_Declaration;
2562
2563    ---------------------------------
2564    -- Analyze_Subtype_Declaration --
2565    ---------------------------------
2566
2567    procedure Analyze_Subtype_Declaration (N : Node_Id) is
2568       Id       : constant Entity_Id := Defining_Identifier (N);
2569       T        : Entity_Id;
2570       R_Checks : Check_Result;
2571
2572    begin
2573       Generate_Definition (Id);
2574       Set_Is_Pure (Id, Is_Pure (Current_Scope));
2575       Init_Size_Align (Id);
2576
2577       --  The following guard condition on Enter_Name is to handle cases
2578       --  where the defining identifier has already been entered into the
2579       --  scope but the declaration as a whole needs to be analyzed.
2580
2581       --  This case in particular happens for derived enumeration types. The
2582       --  derived enumeration type is processed as an inserted enumeration
2583       --  type declaration followed by a rewritten subtype declaration. The
2584       --  defining identifier, however, is entered into the name scope very
2585       --  early in the processing of the original type declaration and
2586       --  therefore needs to be avoided here, when the created subtype
2587       --  declaration is analyzed. (See Build_Derived_Types)
2588
2589       --  This also happens when the full view of a private type is derived
2590       --  type with constraints. In this case the entity has been introduced
2591       --  in the private declaration.
2592
2593       if Present (Etype (Id))
2594         and then (Is_Private_Type (Etype (Id))
2595                    or else Is_Task_Type (Etype (Id))
2596                    or else Is_Rewrite_Substitution (N))
2597       then
2598          null;
2599
2600       else
2601          Enter_Name (Id);
2602       end if;
2603
2604       T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
2605
2606       --  Inherit common attributes
2607
2608       Set_Is_Generic_Type   (Id, Is_Generic_Type   (Base_Type (T)));
2609       Set_Is_Volatile       (Id, Is_Volatile       (T));
2610       Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
2611       Set_Is_Atomic         (Id, Is_Atomic         (T));
2612       Set_Is_Ada_2005       (Id, Is_Ada_2005       (T));
2613
2614       --  In the case where there is no constraint given in the subtype
2615       --  indication, Process_Subtype just returns the Subtype_Mark,
2616       --  so its semantic attributes must be established here.
2617
2618       if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
2619          Set_Etype (Id, Base_Type (T));
2620
2621          case Ekind (T) is
2622             when Array_Kind =>
2623                Set_Ekind                       (Id, E_Array_Subtype);
2624                Copy_Array_Subtype_Attributes   (Id, T);
2625
2626             when Decimal_Fixed_Point_Kind =>
2627                Set_Ekind                (Id, E_Decimal_Fixed_Point_Subtype);
2628                Set_Digits_Value         (Id, Digits_Value       (T));
2629                Set_Delta_Value          (Id, Delta_Value        (T));
2630                Set_Scale_Value          (Id, Scale_Value        (T));
2631                Set_Small_Value          (Id, Small_Value        (T));
2632                Set_Scalar_Range         (Id, Scalar_Range       (T));
2633                Set_Machine_Radix_10     (Id, Machine_Radix_10   (T));
2634                Set_Is_Constrained       (Id, Is_Constrained     (T));
2635                Set_RM_Size              (Id, RM_Size            (T));
2636
2637             when Enumeration_Kind =>
2638                Set_Ekind                (Id, E_Enumeration_Subtype);
2639                Set_First_Literal        (Id, First_Literal (Base_Type (T)));
2640                Set_Scalar_Range         (Id, Scalar_Range       (T));
2641                Set_Is_Character_Type    (Id, Is_Character_Type  (T));
2642                Set_Is_Constrained       (Id, Is_Constrained     (T));
2643                Set_RM_Size              (Id, RM_Size            (T));
2644
2645             when Ordinary_Fixed_Point_Kind =>
2646                Set_Ekind                (Id, E_Ordinary_Fixed_Point_Subtype);
2647                Set_Scalar_Range         (Id, Scalar_Range       (T));
2648                Set_Small_Value          (Id, Small_Value        (T));
2649                Set_Delta_Value          (Id, Delta_Value        (T));
2650                Set_Is_Constrained       (Id, Is_Constrained     (T));
2651                Set_RM_Size              (Id, RM_Size            (T));
2652
2653             when Float_Kind =>
2654                Set_Ekind                (Id, E_Floating_Point_Subtype);
2655                Set_Scalar_Range         (Id, Scalar_Range       (T));
2656                Set_Digits_Value         (Id, Digits_Value       (T));
2657                Set_Is_Constrained       (Id, Is_Constrained     (T));
2658
2659             when Signed_Integer_Kind =>
2660                Set_Ekind                (Id, E_Signed_Integer_Subtype);
2661                Set_Scalar_Range         (Id, Scalar_Range       (T));
2662                Set_Is_Constrained       (Id, Is_Constrained     (T));
2663                Set_RM_Size              (Id, RM_Size            (T));
2664
2665             when Modular_Integer_Kind =>
2666                Set_Ekind                (Id, E_Modular_Integer_Subtype);
2667                Set_Scalar_Range         (Id, Scalar_Range       (T));
2668                Set_Is_Constrained       (Id, Is_Constrained     (T));
2669                Set_RM_Size              (Id, RM_Size            (T));
2670
2671             when Class_Wide_Kind =>
2672                Set_Ekind                (Id, E_Class_Wide_Subtype);
2673                Set_First_Entity         (Id, First_Entity       (T));
2674                Set_Last_Entity          (Id, Last_Entity        (T));
2675                Set_Class_Wide_Type      (Id, Class_Wide_Type    (T));
2676                Set_Cloned_Subtype       (Id, T);
2677                Set_Is_Tagged_Type       (Id, True);
2678                Set_Has_Unknown_Discriminants
2679                                         (Id, True);
2680
2681                if Ekind (T) = E_Class_Wide_Subtype then
2682                   Set_Equivalent_Type   (Id, Equivalent_Type    (T));
2683                end if;
2684
2685             when E_Record_Type | E_Record_Subtype =>
2686                Set_Ekind                (Id, E_Record_Subtype);
2687
2688                if Ekind (T) = E_Record_Subtype
2689                  and then Present (Cloned_Subtype (T))
2690                then
2691                   Set_Cloned_Subtype    (Id, Cloned_Subtype (T));
2692                else
2693                   Set_Cloned_Subtype    (Id, T);
2694                end if;
2695
2696                Set_First_Entity         (Id, First_Entity       (T));
2697                Set_Last_Entity          (Id, Last_Entity        (T));
2698                Set_Has_Discriminants    (Id, Has_Discriminants  (T));
2699                Set_Is_Constrained       (Id, Is_Constrained     (T));
2700                Set_Is_Limited_Record    (Id, Is_Limited_Record  (T));
2701                Set_Has_Unknown_Discriminants
2702                                         (Id, Has_Unknown_Discriminants (T));
2703
2704                if Has_Discriminants (T) then
2705                   Set_Discriminant_Constraint
2706                                         (Id, Discriminant_Constraint (T));
2707                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
2708
2709                elsif Has_Unknown_Discriminants (Id) then
2710                   Set_Discriminant_Constraint (Id, No_Elist);
2711                end if;
2712
2713                if Is_Tagged_Type (T) then
2714                   Set_Is_Tagged_Type    (Id);
2715                   Set_Is_Abstract       (Id, Is_Abstract (T));
2716                   Set_Primitive_Operations
2717                                         (Id, Primitive_Operations (T));
2718                   Set_Class_Wide_Type   (Id, Class_Wide_Type (T));
2719                end if;
2720
2721             when Private_Kind =>
2722                Set_Ekind              (Id, Subtype_Kind (Ekind   (T)));
2723                Set_Has_Discriminants  (Id, Has_Discriminants     (T));
2724                Set_Is_Constrained     (Id, Is_Constrained        (T));
2725                Set_First_Entity       (Id, First_Entity          (T));
2726                Set_Last_Entity        (Id, Last_Entity           (T));
2727                Set_Private_Dependents (Id, New_Elmt_List);
2728                Set_Is_Limited_Record  (Id, Is_Limited_Record     (T));
2729                Set_Has_Unknown_Discriminants
2730                                       (Id, Has_Unknown_Discriminants (T));
2731
2732                if Is_Tagged_Type (T) then
2733                   Set_Is_Tagged_Type  (Id);
2734                   Set_Is_Abstract     (Id, Is_Abstract (T));
2735                   Set_Primitive_Operations
2736                                         (Id, Primitive_Operations (T));
2737                   Set_Class_Wide_Type (Id, Class_Wide_Type (T));
2738                end if;
2739
2740                --  In general the attributes of the subtype of a private
2741                --  type are the attributes of the partial view of parent.
2742                --  However, the full view may be a discriminated type,
2743                --  and the subtype must share the discriminant constraint
2744                --  to generate correct calls to initialization procedures.
2745
2746                if Has_Discriminants (T) then
2747                   Set_Discriminant_Constraint
2748                                      (Id, Discriminant_Constraint (T));
2749                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
2750
2751                elsif Present (Full_View (T))
2752                  and then Has_Discriminants (Full_View (T))
2753                then
2754                   Set_Discriminant_Constraint
2755                                (Id, Discriminant_Constraint (Full_View (T)));
2756                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
2757
2758                   --  This would seem semantically correct, but apparently
2759                   --  confuses the back-end (4412-009). To be explained ???
2760
2761                   --  Set_Has_Discriminants (Id);
2762                end if;
2763
2764                Prepare_Private_Subtype_Completion (Id, N);
2765
2766             when Access_Kind =>
2767                Set_Ekind             (Id, E_Access_Subtype);
2768                Set_Is_Constrained    (Id, Is_Constrained        (T));
2769                Set_Is_Access_Constant
2770                                      (Id, Is_Access_Constant    (T));
2771                Set_Directly_Designated_Type
2772                                      (Id, Designated_Type       (T));
2773
2774                --  Ada 2005 (AI-231): Propagate the null-excluding attribute
2775                --  and carry out some static checks
2776
2777                if Null_Exclusion_Present (N)
2778                  or else Can_Never_Be_Null (T)
2779                then
2780                   Set_Can_Never_Be_Null (Id);
2781
2782                   if Null_Exclusion_Present (N)
2783                     and then Can_Never_Be_Null (T)
2784                   then
2785                      Error_Msg_N
2786                        ("(Ada 2005) null exclusion not allowed if parent "
2787                         & "is already non-null", Subtype_Indication (N));
2788                   end if;
2789                end if;
2790
2791                --  A Pure library_item must not contain the declaration of a
2792                --  named access type, except within a subprogram, generic
2793                --  subprogram, task unit, or protected unit (RM 10.2.1(16)).
2794
2795                if Comes_From_Source (Id)
2796                  and then In_Pure_Unit
2797                  and then not In_Subprogram_Task_Protected_Unit
2798                then
2799                   Error_Msg_N
2800                     ("named access types not allowed in pure unit", N);
2801                end if;
2802
2803             when Concurrent_Kind =>
2804                Set_Ekind                (Id, Subtype_Kind (Ekind   (T)));
2805                Set_Corresponding_Record_Type (Id,
2806                                          Corresponding_Record_Type (T));
2807                Set_First_Entity         (Id, First_Entity          (T));
2808                Set_First_Private_Entity (Id, First_Private_Entity  (T));
2809                Set_Has_Discriminants    (Id, Has_Discriminants     (T));
2810                Set_Is_Constrained       (Id, Is_Constrained        (T));
2811                Set_Last_Entity          (Id, Last_Entity           (T));
2812
2813                if Has_Discriminants (T) then
2814                   Set_Discriminant_Constraint (Id,
2815                                            Discriminant_Constraint (T));
2816                   Set_Stored_Constraint_From_Discriminant_Constraint (Id);
2817                end if;
2818
2819             --  If the subtype name denotes an incomplete type
2820             --  an error was already reported by Process_Subtype.
2821
2822             when E_Incomplete_Type =>
2823                Set_Etype (Id, Any_Type);
2824
2825             when others =>
2826                raise Program_Error;
2827          end case;
2828       end if;
2829
2830       if Etype (Id) = Any_Type then
2831          return;
2832       end if;
2833
2834       --  Some common processing on all types
2835
2836       Set_Size_Info      (Id,                 T);
2837       Set_First_Rep_Item (Id, First_Rep_Item (T));
2838
2839       T := Etype (Id);
2840
2841       Set_Is_Immediately_Visible (Id, True);
2842       Set_Depends_On_Private     (Id, Has_Private_Component (T));
2843
2844       if Present (Generic_Parent_Type (N))
2845         and then
2846           (Nkind
2847              (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
2848             or else Nkind
2849               (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
2850                 /=  N_Formal_Private_Type_Definition)
2851       then
2852          if Is_Tagged_Type (Id) then
2853             if Is_Class_Wide_Type (Id) then
2854                Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
2855             else
2856                Derive_Subprograms (Generic_Parent_Type (N), Id, T);
2857             end if;
2858
2859          elsif Scope (Etype (Id)) /= Standard_Standard then
2860             Derive_Subprograms (Generic_Parent_Type (N), Id);
2861          end if;
2862       end if;
2863
2864       if Is_Private_Type (T)
2865         and then Present (Full_View (T))
2866       then
2867          Conditional_Delay (Id, Full_View (T));
2868
2869       --  The subtypes of components or subcomponents of protected types
2870       --  do not need freeze nodes, which would otherwise appear in the
2871       --  wrong scope (before the freeze node for the protected type). The
2872       --  proper subtypes are those of the subcomponents of the corresponding
2873       --  record.
2874
2875       elsif Ekind (Scope (Id)) /= E_Protected_Type
2876         and then Present (Scope (Scope (Id))) -- error defense!
2877         and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
2878       then
2879          Conditional_Delay (Id, T);
2880       end if;
2881
2882       --  Check that constraint_error is raised for a scalar subtype
2883       --  indication when the lower or upper bound of a non-null range
2884       --  lies outside the range of the type mark.
2885
2886       if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
2887          if Is_Scalar_Type (Etype (Id))
2888             and then Scalar_Range (Id) /=
2889                      Scalar_Range (Etype (Subtype_Mark
2890                                            (Subtype_Indication (N))))
2891          then
2892             Apply_Range_Check
2893               (Scalar_Range (Id),
2894                Etype (Subtype_Mark (Subtype_Indication (N))));
2895
2896          elsif Is_Array_Type (Etype (Id))
2897            and then Present (First_Index (Id))
2898          then
2899             --  This really should be a subprogram that finds the indications
2900             --  to check???
2901
2902             if ((Nkind (First_Index (Id)) = N_Identifier
2903                    and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
2904                  or else Nkind (First_Index (Id)) = N_Subtype_Indication)
2905               and then
2906                 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
2907             then
2908                declare
2909                   Target_Typ : constant Entity_Id :=
2910                                  Etype
2911                                    (First_Index (Etype
2912                                      (Subtype_Mark (Subtype_Indication (N)))));
2913                begin
2914                   R_Checks :=
2915                     Range_Check
2916                       (Scalar_Range (Etype (First_Index (Id))),
2917                        Target_Typ,
2918                        Etype (First_Index (Id)),
2919                        Defining_Identifier (N));
2920
2921                   Insert_Range_Checks
2922                     (R_Checks,
2923                      N,
2924                      Target_Typ,
2925                      Sloc (Defining_Identifier (N)));
2926                end;
2927             end if;
2928          end if;
2929       end if;
2930
2931       Check_Eliminated (Id);
2932    end Analyze_Subtype_Declaration;
2933
2934    --------------------------------
2935    -- Analyze_Subtype_Indication --
2936    --------------------------------
2937
2938    procedure Analyze_Subtype_Indication (N : Node_Id) is
2939       T : constant Entity_Id := Subtype_Mark (N);
2940       R : constant Node_Id   := Range_Expression (Constraint (N));
2941
2942    begin
2943       Analyze (T);
2944
2945       if R /= Error then
2946          Analyze (R);
2947          Set_Etype (N, Etype (R));
2948       else
2949          Set_Error_Posted (R);
2950          Set_Error_Posted (T);
2951       end if;
2952    end Analyze_Subtype_Indication;
2953
2954    ------------------------------
2955    -- Analyze_Type_Declaration --
2956    ------------------------------
2957
2958    procedure Analyze_Type_Declaration (N : Node_Id) is
2959       Def    : constant Node_Id   := Type_Definition (N);
2960       Def_Id : constant Entity_Id := Defining_Identifier (N);
2961       T      : Entity_Id;
2962       Prev   : Entity_Id;
2963
2964       Is_Remote : constant Boolean :=
2965                     (Is_Remote_Types (Current_Scope)
2966                           or else Is_Remote_Call_Interface (Current_Scope))
2967                        and then not (In_Private_Part (Current_Scope)
2968                                        or else
2969                                      In_Package_Body (Current_Scope));
2970
2971    begin
2972       Prev := Find_Type_Name (N);
2973
2974       --  The full view, if present, now points to the current type
2975
2976       --  Ada 2005 (AI-50217): If the type was previously decorated when
2977       --  imported through a LIMITED WITH clause, it appears as incomplete
2978       --  but has no full view.
2979
2980       if Ekind (Prev) = E_Incomplete_Type
2981         and then Present (Full_View (Prev))
2982       then
2983          T := Full_View (Prev);
2984       else
2985          T := Prev;
2986       end if;
2987
2988       Set_Is_Pure (T, Is_Pure (Current_Scope));
2989
2990       --  We set the flag Is_First_Subtype here. It is needed to set the
2991       --  corresponding flag for the Implicit class-wide-type created
2992       --  during tagged types processing.
2993
2994       Set_Is_First_Subtype (T, True);
2995
2996       --  Only composite types other than array types are allowed to have
2997       --  discriminants.
2998
2999       case Nkind (Def) is
3000
3001          --  For derived types, the rule will be checked once we've figured
3002          --  out the parent type.
3003
3004          when N_Derived_Type_Definition =>
3005             null;
3006
3007          --  For record types, discriminants are allowed
3008
3009          when N_Record_Definition =>
3010             null;
3011
3012          when others =>
3013             if Present (Discriminant_Specifications (N)) then
3014                Error_Msg_N
3015                  ("elementary or array type cannot have discriminants",
3016                   Defining_Identifier
3017                   (First (Discriminant_Specifications (N))));
3018             end if;
3019       end case;
3020
3021       --  Elaborate the type definition according to kind, and generate
3022       --  subsidiary (implicit) subtypes where needed. We skip this if
3023       --  it was already done (this happens during the reanalysis that
3024       --  follows a call to the high level optimizer).
3025
3026       if not Analyzed (T) then
3027          Set_Analyzed (T);
3028
3029          case Nkind (Def) is
3030
3031             when N_Access_To_Subprogram_Definition =>
3032                Access_Subprogram_Declaration (T, Def);
3033
3034                --  If this is a remote access to subprogram, we must create
3035                --  the equivalent fat pointer type, and related subprograms.
3036
3037                if Is_Remote then
3038                   Process_Remote_AST_Declaration (N);
3039                end if;
3040
3041                --  Validate categorization rule against access type declaration
3042                --  usually a violation in Pure unit, Shared_Passive unit.
3043
3044                Validate_Access_Type_Declaration (T, N);
3045
3046             when N_Access_To_Object_Definition =>
3047                Access_Type_Declaration (T, Def);
3048
3049                --  Validate categorization rule against access type declaration
3050                --  usually a violation in Pure unit, Shared_Passive unit.
3051
3052                Validate_Access_Type_Declaration (T, N);
3053
3054                --  If we are in a Remote_Call_Interface package and define
3055                --  a RACW, Read and Write attribute must be added.
3056
3057                if Is_Remote
3058                  and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3059                then
3060                   Add_RACW_Features (Def_Id);
3061                end if;
3062
3063                --  Set no strict aliasing flag if config pragma seen
3064
3065                if Opt.No_Strict_Aliasing then
3066                   Set_No_Strict_Aliasing (Base_Type (Def_Id));
3067                end if;
3068
3069             when N_Array_Type_Definition =>
3070                Array_Type_Declaration (T, Def);
3071
3072             when N_Derived_Type_Definition =>
3073                Derived_Type_Declaration (T, N, T /= Def_Id);
3074
3075             when N_Enumeration_Type_Definition =>
3076                Enumeration_Type_Declaration (T, Def);
3077
3078             when N_Floating_Point_Definition =>
3079                Floating_Point_Type_Declaration (T, Def);
3080
3081             when N_Decimal_Fixed_Point_Definition =>
3082                Decimal_Fixed_Point_Type_Declaration (T, Def);
3083
3084             when N_Ordinary_Fixed_Point_Definition =>
3085                Ordinary_Fixed_Point_Type_Declaration (T, Def);
3086
3087             when N_Signed_Integer_Type_Definition =>
3088                Signed_Integer_Type_Declaration (T, Def);
3089
3090             when N_Modular_Type_Definition =>
3091                Modular_Type_Declaration (T, Def);
3092
3093             when N_Record_Definition =>
3094                Record_Type_Declaration (T, N, Prev);
3095
3096             when others =>
3097                raise Program_Error;
3098
3099          end case;
3100       end if;
3101
3102       if Etype (T) = Any_Type then
3103          return;
3104       end if;
3105
3106       --  Some common processing for all types
3107
3108       Set_Depends_On_Private (T, Has_Private_Component (T));
3109
3110       --  Both the declared entity, and its anonymous base type if one
3111       --  was created, need freeze nodes allocated.
3112
3113       declare
3114          B : constant Entity_Id := Base_Type (T);
3115
3116       begin
3117          --  In the case where the base type is different from the first
3118          --  subtype, we pre-allocate a freeze node, and set the proper link
3119          --  to the first subtype. Freeze_Entity will use this preallocated
3120          --  freeze node when it freezes the entity.
3121
3122          if B /= T then
3123             Ensure_Freeze_Node (B);
3124             Set_First_Subtype_Link (Freeze_Node (B), T);
3125          end if;
3126
3127          if not From_With_Type (T) then
3128             Set_Has_Delayed_Freeze (T);
3129          end if;
3130       end;
3131
3132       --  Case of T is the full declaration of some private type which has
3133       --  been swapped in Defining_Identifier (N).
3134
3135       if T /= Def_Id and then Is_Private_Type (Def_Id) then
3136          Process_Full_View (N, T, Def_Id);
3137
3138          --  Record the reference. The form of this is a little strange,
3139          --  since the full declaration has been swapped in. So the first
3140          --  parameter here represents the entity to which a reference is
3141          --  made which is the "real" entity, i.e. the one swapped in,
3142          --  and the second parameter provides the reference location.
3143
3144          Generate_Reference (T, T, 'c');
3145          Set_Completion_Referenced (Def_Id);
3146
3147       --  For completion of incomplete type, process incomplete dependents
3148       --  and always mark the full type as referenced (it is the incomplete
3149       --  type that we get for any real reference).
3150
3151       elsif Ekind (Prev) = E_Incomplete_Type then
3152          Process_Incomplete_Dependents (N, T, Prev);
3153          Generate_Reference (Prev, Def_Id, 'c');
3154          Set_Completion_Referenced (Def_Id);
3155
3156       --  If not private type or incomplete type completion, this is a real
3157       --  definition of a new entity, so record it.
3158
3159       else
3160          Generate_Definition (Def_Id);
3161       end if;
3162
3163       Check_Eliminated (Def_Id);
3164    end Analyze_Type_Declaration;
3165
3166    --------------------------
3167    -- Analyze_Variant_Part --
3168    --------------------------
3169
3170    procedure Analyze_Variant_Part (N : Node_Id) is
3171
3172       procedure Non_Static_Choice_Error (Choice : Node_Id);
3173       --  Error routine invoked by the generic instantiation below when
3174       --  the variant part has a non static choice.
3175
3176       procedure Process_Declarations (Variant : Node_Id);
3177       --  Analyzes all the declarations associated with a Variant.
3178       --  Needed by the generic instantiation below.
3179
3180       package Variant_Choices_Processing is new
3181         Generic_Choices_Processing
3182           (Get_Alternatives          => Variants,
3183            Get_Choices               => Discrete_Choices,
3184            Process_Empty_Choice      => No_OP,
3185            Process_Non_Static_Choice => Non_Static_Choice_Error,
3186            Process_Associated_Node   => Process_Declarations);
3187       use Variant_Choices_Processing;
3188       --  Instantiation of the generic choice processing package
3189
3190       -----------------------------
3191       -- Non_Static_Choice_Error --
3192       -----------------------------
3193
3194       procedure Non_Static_Choice_Error (Choice : Node_Id) is
3195       begin
3196          Flag_Non_Static_Expr
3197            ("choice given in variant part is not static!", Choice);
3198       end Non_Static_Choice_Error;
3199
3200       --------------------------
3201       -- Process_Declarations --
3202       --------------------------
3203
3204       procedure Process_Declarations (Variant : Node_Id) is
3205       begin
3206          if not Null_Present (Component_List (Variant)) then
3207             Analyze_Declarations (Component_Items (Component_List (Variant)));
3208
3209             if Present (Variant_Part (Component_List (Variant))) then
3210                Analyze (Variant_Part (Component_List (Variant)));
3211             end if;
3212          end if;
3213       end Process_Declarations;
3214
3215       --  Variables local to Analyze_Case_Statement
3216
3217       Discr_Name : Node_Id;
3218       Discr_Type : Entity_Id;
3219
3220       Case_Table     : Choice_Table_Type (1 .. Number_Of_Choices (N));
3221       Last_Choice    : Nat;
3222       Dont_Care      : Boolean;
3223       Others_Present : Boolean := False;
3224
3225    --  Start of processing for Analyze_Variant_Part
3226
3227    begin
3228       Discr_Name := Name (N);
3229       Analyze (Discr_Name);
3230
3231       if Ekind (Entity (Discr_Name)) /= E_Discriminant then
3232          Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
3233       end if;
3234
3235       Discr_Type := Etype (Entity (Discr_Name));
3236
3237       if not Is_Discrete_Type (Discr_Type) then
3238          Error_Msg_N
3239            ("discriminant in a variant part must be of a discrete type",
3240              Name (N));
3241          return;
3242       end if;
3243
3244       --  Call the instantiated Analyze_Choices which does the rest of the work
3245
3246       Analyze_Choices
3247         (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
3248    end Analyze_Variant_Part;
3249
3250    ----------------------------
3251    -- Array_Type_Declaration --
3252    ----------------------------
3253
3254    procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
3255       Component_Def : constant Node_Id := Component_Definition (Def);
3256       Element_Type  : Entity_Id;
3257       Implicit_Base : Entity_Id;
3258       Index         : Node_Id;
3259       Related_Id    : Entity_Id := Empty;
3260       Nb_Index      : Nat;
3261       P             : constant Node_Id := Parent (Def);
3262       Priv          : Entity_Id;
3263
3264    begin
3265       if Nkind (Def) = N_Constrained_Array_Definition then
3266          Index := First (Discrete_Subtype_Definitions (Def));
3267       else
3268          Index := First (Subtype_Marks (Def));
3269       end if;
3270
3271       --  Find proper names for the implicit types which may be public.
3272       --  in case of anonymous arrays we use the name of the first object
3273       --  of that type as prefix.
3274
3275       if No (T) then
3276          Related_Id :=  Defining_Identifier (P);
3277       else
3278          Related_Id := T;
3279       end if;
3280
3281       Nb_Index := 1;
3282       while Present (Index) loop
3283          Analyze (Index);
3284          Make_Index (Index, P, Related_Id, Nb_Index);
3285          Next_Index (Index);
3286          Nb_Index := Nb_Index + 1;
3287       end loop;
3288
3289       if Present (Subtype_Indication (Component_Def)) then
3290          Element_Type := Process_Subtype (Subtype_Indication (Component_Def),
3291                                           P, Related_Id, 'C');
3292
3293       --  Ada 2005 (AI-230): Access Definition case
3294
3295       else pragma Assert (Present (Access_Definition (Component_Def)));
3296          Element_Type := Access_Definition
3297                            (Related_Nod => Related_Id,
3298                             N           => Access_Definition (Component_Def));
3299          Set_Is_Local_Anonymous_Access (Element_Type);
3300
3301          --  Ada 2005 (AI-230): In case of components that are anonymous
3302          --  access types the level of accessibility depends on the enclosing
3303          --  type declaration
3304
3305          Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
3306
3307          --  Ada 2005 (AI-254)
3308
3309          declare
3310             CD : constant Node_Id :=
3311                    Access_To_Subprogram_Definition
3312                      (Access_Definition (Component_Def));
3313          begin
3314             if Present (CD) and then Protected_Present (CD) then
3315                Element_Type :=
3316                  Replace_Anonymous_Access_To_Protected_Subprogram
3317                    (Def, Element_Type);
3318             end if;
3319          end;
3320       end if;
3321
3322       --  Constrained array case
3323
3324       if No (T) then
3325          T := Create_Itype (E_Void, P, Related_Id, 'T');
3326       end if;
3327
3328       if Nkind (Def) = N_Constrained_Array_Definition then
3329
3330          --  Establish Implicit_Base as unconstrained base type
3331
3332          Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
3333
3334          Init_Size_Align        (Implicit_Base);
3335          Set_Etype              (Implicit_Base, Implicit_Base);
3336          Set_Scope              (Implicit_Base, Current_Scope);
3337          Set_Has_Delayed_Freeze (Implicit_Base);
3338
3339          --  The constrained array type is a subtype of the unconstrained one
3340
3341          Set_Ekind          (T, E_Array_Subtype);
3342          Init_Size_Align    (T);
3343          Set_Etype          (T, Implicit_Base);
3344          Set_Scope          (T, Current_Scope);
3345          Set_Is_Constrained (T, True);
3346          Set_First_Index    (T, First (Discrete_Subtype_Definitions (Def)));
3347          Set_Has_Delayed_Freeze (T);
3348
3349          --  Complete setup of implicit base type
3350
3351          Set_First_Index    (Implicit_Base, First_Index (T));
3352          Set_Component_Type (Implicit_Base, Element_Type);
3353          Set_Has_Task       (Implicit_Base, Has_Task      (Element_Type));
3354          Set_Component_Size (Implicit_Base, Uint_0);
3355          Set_Has_Controlled_Component
3356                             (Implicit_Base, Has_Controlled_Component
3357                                                           (Element_Type)
3358                                               or else
3359                                             Is_Controlled (Element_Type));
3360          Set_Finalize_Storage_Only
3361                             (Implicit_Base, Finalize_Storage_Only
3362                                                           (Element_Type));
3363
3364       --  Unconstrained array case
3365
3366       else
3367          Set_Ekind                    (T, E_Array_Type);
3368          Init_Size_Align              (T);
3369          Set_Etype                    (T, T);
3370          Set_Scope                    (T, Current_Scope);
3371          Set_Component_Size           (T, Uint_0);
3372          Set_Is_Constrained           (T, False);
3373          Set_First_Index              (T, First (Subtype_Marks (Def)));
3374          Set_Has_Delayed_Freeze       (T, True);
3375          Set_Has_Task                 (T, Has_Task      (Element_Type));
3376          Set_Has_Controlled_Component (T, Has_Controlled_Component
3377                                                         (Element_Type)
3378                                             or else
3379                                           Is_Controlled (Element_Type));
3380          Set_Finalize_Storage_Only    (T, Finalize_Storage_Only
3381                                                         (Element_Type));
3382       end if;
3383
3384       Set_Component_Type (Base_Type (T), Element_Type);
3385
3386       if Aliased_Present (Component_Definition (Def)) then
3387          Set_Has_Aliased_Components (Etype (T));
3388       end if;
3389
3390       --  Ada 2005 (AI-231): Propagate the null-excluding attribute to the
3391       --  array to ensure that objects of this type are initialized.
3392
3393       if Ada_Version >= Ada_05
3394         and then (Null_Exclusion_Present (Component_Definition (Def))
3395                     or else Can_Never_Be_Null (Element_Type))
3396       then
3397          Set_Can_Never_Be_Null (T);
3398
3399          if Null_Exclusion_Present (Component_Definition (Def))
3400            and then Can_Never_Be_Null (Element_Type)
3401          then
3402             Error_Msg_N
3403               ("(Ada 2005) already a null-excluding type",
3404                Subtype_Indication (Component_Definition (Def)));
3405          end if;
3406       end if;
3407
3408       Priv := Private_Component (Element_Type);
3409
3410       if Present (Priv) then
3411
3412          --  Check for circular definitions
3413
3414          if Priv = Any_Type then
3415             Set_Component_Type (Etype (T), Any_Type);
3416
3417          --  There is a gap in the visibility of operations on the composite
3418          --  type only if the component type is defined in a different scope.
3419
3420          elsif Scope (Priv) = Current_Scope then
3421             null;
3422
3423          elsif Is_Limited_Type (Priv) then
3424             Set_Is_Limited_Composite (Etype (T));
3425             Set_Is_Limited_Composite (T);
3426          else
3427             Set_Is_Private_Composite (Etype (T));
3428             Set_Is_Private_Composite (T);
3429          end if;
3430       end if;
3431
3432       --  Create a concatenation operator for the new type. Internal
3433       --  array types created for packed entities do not need such, they
3434       --  are compatible with the user-defined type.
3435
3436       if Number_Dimensions (T) = 1
3437          and then not Is_Packed_Array_Type (T)
3438       then
3439          New_Concatenation_Op (T);
3440       end if;
3441
3442       --  In the case of an unconstrained array the parser has already
3443       --  verified that all the indices are unconstrained but we still
3444       --  need to make sure that the element type is constrained.
3445
3446       if Is_Indefinite_Subtype (Element_Type) then
3447          Error_Msg_N
3448            ("unconstrained element type in array declaration",
3449             Subtype_Indication (Component_Def));
3450
3451       elsif Is_Abstract (Element_Type) then
3452          Error_Msg_N
3453            ("the type of a component cannot be abstract",
3454             Subtype_Indication (Component_Def));
3455       end if;
3456
3457    end Array_Type_Declaration;
3458
3459    ------------------------------------------------------
3460    -- Replace_Anonymous_Access_To_Protected_Subprogram --
3461    ------------------------------------------------------
3462
3463    function Replace_Anonymous_Access_To_Protected_Subprogram
3464      (N      : Node_Id;
3465       Prev_E : Entity_Id) return Entity_Id
3466    is
3467       Loc : constant Source_Ptr := Sloc (N);
3468
3469       Curr_Scope : constant Scope_Stack_Entry :=
3470                      Scope_Stack.Table (Scope_Stack.Last);
3471
3472       Anon : constant Entity_Id :=
3473                Make_Defining_Identifier (Loc,
3474                  Chars => New_Internal_Name ('S'));
3475
3476       Acc  : Node_Id;
3477       Comp : Node_Id;
3478       Decl : Node_Id;
3479       P    : Node_Id := Parent (N);
3480
3481    begin
3482       Set_Is_Internal (Anon);
3483
3484       case Nkind (N) is
3485          when N_Component_Declaration       |
3486            N_Unconstrained_Array_Definition |
3487            N_Constrained_Array_Definition   =>
3488             Comp := Component_Definition (N);
3489             Acc  := Access_Definition (Component_Definition (N));
3490
3491          when N_Discriminant_Specification =>
3492             Comp := Discriminant_Type (N);
3493             Acc  := Discriminant_Type (N);
3494
3495          when N_Parameter_Specification =>
3496             Comp := Parameter_Type (N);
3497             Acc  := Parameter_Type (N);
3498
3499          when others =>
3500             raise Program_Error;
3501       end case;
3502
3503       Decl := Make_Full_Type_Declaration (Loc,
3504                 Defining_Identifier => Anon,
3505                 Type_Definition   =>
3506                   Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
3507
3508       Mark_Rewrite_Insertion (Decl);
3509
3510       --  Insert the new declaration in the nearest enclosing scope
3511
3512       while Present (P) and then not Has_Declarations (P) loop
3513          P := Parent (P);
3514       end loop;
3515
3516       pragma Assert (Present (P));
3517
3518       if Nkind (P) = N_Package_Specification then
3519          Prepend (Decl, Visible_Declarations (P));
3520       else
3521          Prepend (Decl, Declarations (P));
3522       end if;
3523
3524       --  Replace the anonymous type with an occurrence of the new declaration.
3525       --  In all cases the rewriten node does not have the null-exclusion
3526       --  attribute because (if present) it was already inherited by the
3527       --  anonymous entity (Anon). Thus, in case of components we do not
3528       --  inherit this attribute.
3529
3530       if Nkind (N) = N_Parameter_Specification then
3531          Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
3532          Set_Etype (Defining_Identifier (N), Anon);
3533          Set_Null_Exclusion_Present (N, False);
3534       else
3535          Rewrite (Comp,
3536            Make_Component_Definition (Loc,
3537              Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
3538       end if;
3539
3540       Mark_Rewrite_Insertion (Comp);
3541
3542       --  Temporarily remove the current scope from the stack to add the new
3543       --  declarations to the enclosing scope
3544
3545       Scope_Stack.Decrement_Last;
3546       Analyze (Decl);
3547       Scope_Stack.Append (Curr_Scope);
3548
3549       Set_Original_Access_Type (Anon, Prev_E);
3550       return Anon;
3551    end Replace_Anonymous_Access_To_Protected_Subprogram;
3552
3553    -------------------------------
3554    -- Build_Derived_Access_Type --
3555    -------------------------------
3556
3557    procedure Build_Derived_Access_Type
3558      (N            : Node_Id;
3559       Parent_Type  : Entity_Id;
3560       Derived_Type : Entity_Id)
3561    is
3562       S : constant Node_Id := Subtype_Indication (Type_Definition (N));
3563
3564       Desig_Type      : Entity_Id;
3565       Discr           : Entity_Id;
3566       Discr_Con_Elist : Elist_Id;
3567       Discr_Con_El    : Elmt_Id;
3568       Subt            : Entity_Id;
3569
3570    begin
3571       --  Set the designated type so it is available in case this is
3572       --  an access to a self-referential type, e.g. a standard list
3573       --  type with a next pointer. Will be reset after subtype is built.
3574
3575       Set_Directly_Designated_Type
3576         (Derived_Type, Designated_Type (Parent_Type));
3577
3578       Subt := Process_Subtype (S, N);
3579
3580       if Nkind (S) /= N_Subtype_Indication
3581         and then Subt /= Base_Type (Subt)
3582       then
3583          Set_Ekind (Derived_Type, E_Access_Subtype);
3584       end if;
3585
3586       if Ekind (Derived_Type) = E_Access_Subtype then
3587          declare
3588             Pbase      : constant Entity_Id := Base_Type (Parent_Type);
3589             Ibase      : constant Entity_Id :=
3590                            Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
3591             Svg_Chars  : constant Name_Id   := Chars (Ibase);
3592             Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
3593
3594          begin
3595             Copy_Node (Pbase, Ibase);
3596
3597             Set_Chars             (Ibase, Svg_Chars);
3598             Set_Next_Entity       (Ibase, Svg_Next_E);
3599             Set_Sloc              (Ibase, Sloc (Derived_Type));
3600             Set_Scope             (Ibase, Scope (Derived_Type));
3601             Set_Freeze_Node       (Ibase, Empty);
3602             Set_Is_Frozen         (Ibase, False);
3603             Set_Comes_From_Source (Ibase, False);
3604             Set_Is_First_Subtype  (Ibase, False);
3605
3606             Set_Etype (Ibase, Pbase);
3607             Set_Etype (Derived_Type, Ibase);
3608          end;
3609       end if;
3610
3611       Set_Directly_Designated_Type
3612         (Derived_Type, Designated_Type (Subt));
3613
3614       Set_Is_Constrained     (Derived_Type, Is_Constrained (Subt));
3615       Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
3616       Set_Size_Info          (Derived_Type,                     Parent_Type);
3617       Set_RM_Size            (Derived_Type, RM_Size            (Parent_Type));
3618       Set_Depends_On_Private (Derived_Type,
3619                               Has_Private_Component (Derived_Type));
3620       Conditional_Delay      (Derived_Type, Subt);
3621
3622       --  Ada 2005 (AI-231). Set the null-exclusion attribute
3623
3624       if Null_Exclusion_Present (Type_Definition (N))
3625         or else Can_Never_Be_Null (Parent_Type)
3626       then
3627          Set_Can_Never_Be_Null (Derived_Type);
3628       end if;
3629
3630       --  Note: we do not copy the Storage_Size_Variable, since
3631       --  we always go to the root type for this information.
3632
3633       --  Apply range checks to discriminants for derived record case
3634       --  ??? THIS CODE SHOULD NOT BE HERE REALLY.
3635
3636       Desig_Type := Designated_Type (Derived_Type);
3637       if Is_Composite_Type (Desig_Type)
3638         and then (not Is_Array_Type (Desig_Type))
3639         and then Has_Discriminants (Desig_Type)
3640         and then Base_Type (Desig_Type) /= Desig_Type
3641       then
3642          Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
3643          Discr_Con_El := First_Elmt (Discr_Con_Elist);
3644
3645          Discr := First_Discriminant (Base_Type (Desig_Type));
3646          while Present (Discr_Con_El) loop
3647             Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
3648             Next_Elmt (Discr_Con_El);
3649             Next_Discriminant (Discr);
3650          end loop;
3651       end if;
3652    end Build_Derived_Access_Type;
3653
3654    ------------------------------
3655    -- Build_Derived_Array_Type --
3656    ------------------------------
3657
3658    procedure Build_Derived_Array_Type
3659      (N            : Node_Id;
3660       Parent_Type  : Entity_Id;
3661       Derived_Type : Entity_Id)
3662    is
3663       Loc           : constant Source_Ptr := Sloc (N);
3664       Tdef          : constant Node_Id    := Type_Definition (N);
3665       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
3666       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
3667       Implicit_Base : Entity_Id;
3668       New_Indic     : Node_Id;
3669
3670       procedure Make_Implicit_Base;
3671       --  If the parent subtype is constrained, the derived type is a
3672       --  subtype of an implicit base type derived from the parent base.
3673
3674       ------------------------
3675       -- Make_Implicit_Base --
3676       ------------------------
3677
3678       procedure Make_Implicit_Base is
3679       begin
3680          Implicit_Base :=
3681            Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
3682
3683          Set_Ekind (Implicit_Base, Ekind (Parent_Base));
3684          Set_Etype (Implicit_Base, Parent_Base);
3685
3686          Copy_Array_Subtype_Attributes   (Implicit_Base, Parent_Base);
3687          Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
3688
3689          Set_Has_Delayed_Freeze (Implicit_Base, True);
3690       end Make_Implicit_Base;
3691
3692    --  Start of processing for Build_Derived_Array_Type
3693
3694    begin
3695       if not Is_Constrained (Parent_Type) then
3696          if Nkind (Indic) /= N_Subtype_Indication then
3697             Set_Ekind (Derived_Type, E_Array_Type);
3698
3699             Copy_Array_Subtype_Attributes   (Derived_Type, Parent_Type);
3700             Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
3701
3702             Set_Has_Delayed_Freeze (Derived_Type, True);
3703
3704          else
3705             Make_Implicit_Base;
3706             Set_Etype (Derived_Type, Implicit_Base);
3707
3708             New_Indic :=
3709               Make_Subtype_Declaration (Loc,
3710                 Defining_Identifier => Derived_Type,
3711                 Subtype_Indication  =>
3712                   Make_Subtype_Indication (Loc,
3713                     Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
3714                     Constraint => Constraint (Indic)));
3715
3716             Rewrite (N, New_Indic);
3717             Analyze (N);
3718          end if;
3719
3720       else
3721          if Nkind (Indic) /= N_Subtype_Indication then
3722             Make_Implicit_Base;
3723
3724             Set_Ekind             (Derived_Type, Ekind (Parent_Type));
3725             Set_Etype             (Derived_Type, Implicit_Base);
3726             Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
3727
3728          else
3729             Error_Msg_N ("illegal constraint on constrained type", Indic);
3730          end if;
3731       end if;
3732
3733       --  If the parent type is not a derived type itself, and is
3734       --  declared in a closed scope (e.g., a subprogram), then we
3735       --  need to explicitly introduce the new type's concatenation
3736       --  operator since Derive_Subprograms will not inherit the
3737       --  parent's operator. If the parent type is unconstrained, the
3738       --  operator is of the unconstrained base type.
3739
3740       if Number_Dimensions (Parent_Type) = 1
3741         and then not Is_Limited_Type (Parent_Type)
3742         and then not Is_Derived_Type (Parent_Type)
3743         and then not Is_Package (Scope (Base_Type (Parent_Type)))
3744       then
3745          if not Is_Constrained (Parent_Type)
3746            and then Is_Constrained (Derived_Type)
3747          then
3748             New_Concatenation_Op (Implicit_Base);
3749          else
3750             New_Concatenation_Op (Derived_Type);
3751          end if;
3752       end if;
3753    end Build_Derived_Array_Type;
3754
3755    -----------------------------------
3756    -- Build_Derived_Concurrent_Type --
3757    -----------------------------------
3758
3759    procedure Build_Derived_Concurrent_Type
3760      (N            : Node_Id;
3761       Parent_Type  : Entity_Id;
3762       Derived_Type : Entity_Id)
3763    is
3764       D_Constraint : Node_Id;
3765       Disc_Spec    : Node_Id;
3766       Old_Disc     : Entity_Id;
3767       New_Disc     : Entity_Id;
3768
3769       Constraint_Present : constant Boolean :=
3770                              Nkind (Subtype_Indication (Type_Definition (N)))
3771                                                      = N_Subtype_Indication;
3772
3773    begin
3774       Set_Stored_Constraint (Derived_Type, No_Elist);
3775
3776       if Is_Task_Type (Parent_Type) then
3777          Set_Storage_Size_Variable (Derived_Type,
3778            Storage_Size_Variable (Parent_Type));
3779       end if;
3780
3781       if Present (Discriminant_Specifications (N)) then
3782          New_Scope (Derived_Type);
3783          Check_Or_Process_Discriminants (N, Derived_Type);
3784          End_Scope;
3785
3786       elsif Constraint_Present then
3787
3788          --  Build constrained subtype and derive from it
3789
3790          declare
3791             Loc  : constant Source_Ptr := Sloc (N);
3792             Anon : constant Entity_Id :=
3793                      Make_Defining_Identifier (Loc,
3794                        New_External_Name (Chars (Derived_Type), 'T'));
3795             Decl : Node_Id;
3796
3797          begin
3798             Decl :=
3799               Make_Subtype_Declaration (Loc,
3800                 Defining_Identifier => Anon,
3801                 Subtype_Indication =>
3802                   New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
3803             Insert_Before (N, Decl);
3804             Rewrite (Subtype_Indication (Type_Definition (N)),
3805               New_Occurrence_Of (Anon, Loc));
3806             Analyze (Decl);
3807             Set_Analyzed (Derived_Type, False);
3808             Analyze (N);
3809             return;
3810          end;
3811       end if;
3812
3813       --  All attributes are inherited from parent. In particular,
3814       --  entries and the corresponding record type are the same.
3815       --  Discriminants may be renamed, and must be treated separately.
3816
3817       Set_Has_Discriminants
3818         (Derived_Type, Has_Discriminants         (Parent_Type));
3819       Set_Corresponding_Record_Type
3820         (Derived_Type, Corresponding_Record_Type (Parent_Type));
3821
3822       if Constraint_Present then
3823          if not Has_Discriminants (Parent_Type) then
3824             Error_Msg_N ("untagged parent must have discriminants", N);
3825
3826          elsif Present (Discriminant_Specifications (N)) then
3827
3828             --  Verify that new discriminants are used to constrain
3829             --  the old ones.
3830
3831             Old_Disc   := First_Discriminant (Parent_Type);
3832             New_Disc   := First_Discriminant (Derived_Type);
3833             Disc_Spec  := First (Discriminant_Specifications (N));
3834             D_Constraint :=
3835               First
3836                 (Constraints
3837                   (Constraint (Subtype_Indication (Type_Definition (N)))));
3838
3839             while Present (Old_Disc) and then Present (Disc_Spec) loop
3840
3841                if Nkind (Discriminant_Type (Disc_Spec)) /=
3842                                               N_Access_Definition
3843                then
3844                   Analyze (Discriminant_Type (Disc_Spec));
3845
3846                   if not Subtypes_Statically_Compatible (
3847                              Etype (Discriminant_Type (Disc_Spec)),
3848                                Etype (Old_Disc))
3849                   then
3850                      Error_Msg_N
3851                        ("not statically compatible with parent discriminant",
3852                         Discriminant_Type (Disc_Spec));
3853                   end if;
3854                end if;
3855
3856                if Nkind (D_Constraint) = N_Identifier
3857                  and then Chars (D_Constraint) /=
3858                    Chars (Defining_Identifier (Disc_Spec))
3859                then
3860                   Error_Msg_N ("new discriminants must constrain old ones",
3861                     D_Constraint);
3862                else
3863                   Set_Corresponding_Discriminant (New_Disc, Old_Disc);
3864                end if;
3865
3866                Next_Discriminant (Old_Disc);
3867                Next_Discriminant (New_Disc);
3868                Next (Disc_Spec);
3869             end loop;
3870
3871             if Present (Old_Disc) or else Present (Disc_Spec) then
3872                Error_Msg_N ("discriminant mismatch in derivation", N);
3873             end if;
3874
3875          end if;
3876
3877       elsif Present (Discriminant_Specifications (N)) then
3878          Error_Msg_N
3879            ("missing discriminant constraint in untagged derivation",
3880             N);
3881       end if;
3882
3883       if Present (Discriminant_Specifications (N)) then
3884          Old_Disc := First_Discriminant (Parent_Type);
3885          while Present (Old_Disc) loop
3886
3887             if No (Next_Entity (Old_Disc))
3888               or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
3889             then
3890                Set_Next_Entity (Last_Entity (Derived_Type),
3891                                          Next_Entity (Old_Disc));
3892                exit;
3893             end if;
3894
3895             Next_Discriminant (Old_Disc);
3896          end loop;
3897
3898       else
3899          Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
3900          if Has_Discriminants (Parent_Type) then
3901             Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
3902             Set_Discriminant_Constraint (
3903               Derived_Type, Discriminant_Constraint (Parent_Type));
3904          end if;
3905       end if;
3906
3907       Set_Last_Entity  (Derived_Type, Last_Entity  (Parent_Type));
3908
3909       Set_Has_Completion (Derived_Type);
3910    end Build_Derived_Concurrent_Type;
3911
3912    ------------------------------------
3913    -- Build_Derived_Enumeration_Type --
3914    ------------------------------------
3915
3916    procedure Build_Derived_Enumeration_Type
3917      (N            : Node_Id;
3918       Parent_Type  : Entity_Id;
3919       Derived_Type : Entity_Id)
3920    is
3921       Loc           : constant Source_Ptr := Sloc (N);
3922       Def           : constant Node_Id    := Type_Definition (N);
3923       Indic         : constant Node_Id    := Subtype_Indication (Def);
3924       Implicit_Base : Entity_Id;
3925       Literal       : Entity_Id;
3926       New_Lit       : Entity_Id;
3927       Literals_List : List_Id;
3928       Type_Decl     : Node_Id;
3929       Hi, Lo        : Node_Id;
3930       Rang_Expr     : Node_Id;
3931
3932    begin
3933       --  Since types Standard.Character and Standard.Wide_Character do
3934       --  not have explicit literals lists we need to process types derived
3935       --  from them specially. This is handled by Derived_Standard_Character.
3936       --  If the parent type is a generic type, there are no literals either,
3937       --  and we construct the same skeletal representation as for the generic
3938       --  parent type.
3939
3940       if Root_Type (Parent_Type) = Standard_Character
3941         or else Root_Type (Parent_Type) = Standard_Wide_Character
3942         or else Root_Type (Parent_Type) = Standard_Wide_Wide_Character
3943       then
3944          Derived_Standard_Character (N, Parent_Type, Derived_Type);
3945
3946       elsif Is_Generic_Type (Root_Type (Parent_Type)) then
3947          declare
3948             Lo : Node_Id;
3949             Hi : Node_Id;
3950
3951          begin
3952             Lo :=
3953                Make_Attribute_Reference (Loc,
3954                  Attribute_Name => Name_First,
3955                  Prefix => New_Reference_To (Derived_Type, Loc));
3956             Set_Etype (Lo, Derived_Type);
3957
3958             Hi :=
3959                Make_Attribute_Reference (Loc,
3960                  Attribute_Name => Name_Last,
3961                  Prefix => New_Reference_To (Derived_Type, Loc));
3962             Set_Etype (Hi, Derived_Type);
3963
3964             Set_Scalar_Range (Derived_Type,
3965                Make_Range (Loc,
3966                  Low_Bound => Lo,
3967                  High_Bound => Hi));
3968          end;
3969
3970       else
3971          --  If a constraint is present, analyze the bounds to catch
3972          --  premature usage of the derived literals.
3973
3974          if Nkind (Indic) = N_Subtype_Indication
3975            and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
3976          then
3977             Analyze (Low_Bound  (Range_Expression (Constraint (Indic))));
3978             Analyze (High_Bound (Range_Expression (Constraint (Indic))));
3979          end if;
3980
3981          --  Introduce an implicit base type for the derived type even
3982          --  if there is no constraint attached to it, since this seems
3983          --  closer to the Ada semantics. Build a full type declaration
3984          --  tree for the derived type using the implicit base type as
3985          --  the defining identifier. The build a subtype declaration
3986          --  tree which applies the constraint (if any) have it replace
3987          --  the derived type declaration.
3988
3989          Literal := First_Literal (Parent_Type);
3990          Literals_List := New_List;
3991
3992          while Present (Literal)
3993            and then Ekind (Literal) = E_Enumeration_Literal
3994          loop
3995             --  Literals of the derived type have the same representation as
3996             --  those of the parent type, but this representation can be
3997             --  overridden by an explicit representation clause. Indicate
3998             --  that there is no explicit representation given yet. These
3999             --  derived literals are implicit operations of the new type,
4000             --  and can be overriden by explicit ones.
4001
4002             if Nkind (Literal) = N_Defining_Character_Literal then
4003                New_Lit :=
4004                  Make_Defining_Character_Literal (Loc, Chars (Literal));
4005             else
4006                New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
4007             end if;
4008
4009             Set_Ekind                (New_Lit, E_Enumeration_Literal);
4010             Set_Enumeration_Pos      (New_Lit, Enumeration_Pos (Literal));
4011             Set_Enumeration_Rep      (New_Lit, Enumeration_Rep (Literal));
4012             Set_Enumeration_Rep_Expr (New_Lit, Empty);
4013             Set_Alias                (New_Lit, Literal);
4014             Set_Is_Known_Valid       (New_Lit, True);
4015
4016             Append (New_Lit, Literals_List);
4017             Next_Literal (Literal);
4018          end loop;
4019
4020          Implicit_Base :=
4021            Make_Defining_Identifier (Sloc (Derived_Type),
4022              New_External_Name (Chars (Derived_Type), 'B'));
4023
4024          --  Indicate the proper nature of the derived type. This must
4025          --  be done before analysis of the literals, to recognize cases
4026          --  when a literal may be hidden by a previous explicit function
4027          --  definition (cf. c83031a).
4028
4029          Set_Ekind (Derived_Type, E_Enumeration_Subtype);
4030          Set_Etype (Derived_Type, Implicit_Base);
4031
4032          Type_Decl :=
4033            Make_Full_Type_Declaration (Loc,
4034              Defining_Identifier => Implicit_Base,
4035              Discriminant_Specifications => No_List,
4036              Type_Definition =>
4037                Make_Enumeration_Type_Definition (Loc, Literals_List));
4038
4039          Mark_Rewrite_Insertion (Type_Decl);
4040          Insert_Before (N, Type_Decl);
4041          Analyze (Type_Decl);
4042
4043          --  After the implicit base is analyzed its Etype needs to be changed
4044          --  to reflect the fact that it is derived from the parent type which
4045          --  was ignored during analysis. We also set the size at this point.
4046
4047          Set_Etype (Implicit_Base, Parent_Type);
4048
4049          Set_Size_Info      (Implicit_Base,                 Parent_Type);
4050          Set_RM_Size        (Implicit_Base, RM_Size        (Parent_Type));
4051          Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
4052
4053          Set_Has_Non_Standard_Rep
4054                             (Implicit_Base, Has_Non_Standard_Rep
4055                                                            (Parent_Type));
4056          Set_Has_Delayed_Freeze (Implicit_Base);
4057
4058          --  Process the subtype indication including a validation check
4059          --  on the constraint, if any. If a constraint is given, its bounds
4060          --  must be implicitly converted to the new type.
4061
4062          if Nkind (Indic) = N_Subtype_Indication then
4063             declare
4064                R : constant Node_Id :=
4065                      Range_Expression (Constraint (Indic));
4066
4067             begin
4068                if Nkind (R) = N_Range then
4069                   Hi := Build_Scalar_Bound
4070                           (High_Bound (R), Parent_Type, Implicit_Base);
4071                   Lo := Build_Scalar_Bound
4072                           (Low_Bound  (R), Parent_Type, Implicit_Base);
4073
4074                else
4075                   --  Constraint is a Range attribute. Replace with the
4076                   --  explicit mention of the bounds of the prefix, which must
4077                   --  be a subtype.
4078
4079                   Analyze (Prefix (R));
4080                   Hi :=
4081                     Convert_To (Implicit_Base,
4082                       Make_Attribute_Reference (Loc,
4083                         Attribute_Name => Name_Last,
4084                         Prefix =>
4085                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
4086
4087                   Lo :=
4088                     Convert_To (Implicit_Base,
4089                       Make_Attribute_Reference (Loc,
4090                         Attribute_Name => Name_First,
4091                         Prefix =>
4092                           New_Occurrence_Of (Entity (Prefix (R)), Loc)));
4093                end if;
4094             end;
4095
4096          else
4097             Hi :=
4098               Build_Scalar_Bound
4099                 (Type_High_Bound (Parent_Type),
4100                  Parent_Type, Implicit_Base);
4101             Lo :=
4102                Build_Scalar_Bound
4103                  (Type_Low_Bound (Parent_Type),
4104                   Parent_Type, Implicit_Base);
4105          end if;
4106
4107          Rang_Expr :=
4108            Make_Range (Loc,
4109              Low_Bound  => Lo,
4110              High_Bound => Hi);
4111
4112          --  If we constructed a default range for the case where no range
4113          --  was given, then the expressions in the range must not freeze
4114          --  since they do not correspond to expressions in the source.
4115
4116          if Nkind (Indic) /= N_Subtype_Indication then
4117             Set_Must_Not_Freeze (Lo);
4118             Set_Must_Not_Freeze (Hi);
4119             Set_Must_Not_Freeze (Rang_Expr);
4120          end if;
4121
4122          Rewrite (N,
4123            Make_Subtype_Declaration (Loc,
4124              Defining_Identifier => Derived_Type,
4125              Subtype_Indication =>
4126                Make_Subtype_Indication (Loc,
4127                  Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
4128                  Constraint =>
4129                    Make_Range_Constraint (Loc,
4130                      Range_Expression => Rang_Expr))));
4131
4132          Analyze (N);
4133
4134          --  If pragma Discard_Names applies on the first subtype of the
4135          --  parent type, then it must be applied on this subtype as well.
4136
4137          if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
4138             Set_Discard_Names (Derived_Type);
4139          end if;
4140
4141          --  Apply a range check. Since this range expression doesn't have an
4142          --  Etype, we have to specifically pass the Source_Typ parameter. Is
4143          --  this right???
4144
4145          if Nkind (Indic) = N_Subtype_Indication then
4146             Apply_Range_Check (Range_Expression (Constraint (Indic)),
4147                                Parent_Type,
4148                                Source_Typ => Entity (Subtype_Mark (Indic)));
4149          end if;
4150       end if;
4151    end Build_Derived_Enumeration_Type;
4152
4153    --------------------------------
4154    -- Build_Derived_Numeric_Type --
4155    --------------------------------
4156
4157    procedure Build_Derived_Numeric_Type
4158      (N            : Node_Id;
4159       Parent_Type  : Entity_Id;
4160       Derived_Type : Entity_Id)
4161    is
4162       Loc           : constant Source_Ptr := Sloc (N);
4163       Tdef          : constant Node_Id    := Type_Definition (N);
4164       Indic         : constant Node_Id    := Subtype_Indication (Tdef);
4165       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
4166       No_Constraint : constant Boolean    := Nkind (Indic) /=
4167                                                   N_Subtype_Indication;
4168       Implicit_Base : Entity_Id;
4169
4170       Lo : Node_Id;
4171       Hi : Node_Id;
4172
4173    begin
4174       --  Process the subtype indication including a validation check on
4175       --  the constraint if any.
4176
4177       Discard_Node (Process_Subtype (Indic, N));
4178
4179       --  Introduce an implicit base type for the derived type even if there
4180       --  is no constraint attached to it, since this seems closer to the Ada
4181       --  semantics.
4182
4183       Implicit_Base :=
4184         Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4185
4186       Set_Etype          (Implicit_Base, Parent_Base);
4187       Set_Ekind          (Implicit_Base, Ekind          (Parent_Base));
4188       Set_Size_Info      (Implicit_Base,                 Parent_Base);
4189       Set_RM_Size        (Implicit_Base, RM_Size        (Parent_Base));
4190       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
4191       Set_Parent         (Implicit_Base, Parent (Derived_Type));
4192
4193       if Is_Discrete_Or_Fixed_Point_Type (Parent_Base) then
4194          Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
4195       end if;
4196
4197       Set_Has_Delayed_Freeze (Implicit_Base);
4198
4199       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
4200       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
4201
4202       Set_Scalar_Range (Implicit_Base,
4203         Make_Range (Loc,
4204           Low_Bound  => Lo,
4205           High_Bound => Hi));
4206
4207       if Has_Infinities (Parent_Base) then
4208          Set_Includes_Infinities (Scalar_Range (Implicit_Base));
4209       end if;
4210
4211       --  The Derived_Type, which is the entity of the declaration, is a
4212       --  subtype of the implicit base. Its Ekind is a subtype, even in the
4213       --  absence of an explicit constraint.
4214
4215       Set_Etype (Derived_Type, Implicit_Base);
4216
4217       --  If we did not have a constraint, then the Ekind is set from the
4218       --  parent type (otherwise Process_Subtype has set the bounds)
4219
4220       if No_Constraint then
4221          Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
4222       end if;
4223
4224       --  If we did not have a range constraint, then set the range from the
4225       --  parent type. Otherwise, the call to Process_Subtype has set the
4226       --  bounds.
4227
4228       if No_Constraint
4229         or else not Has_Range_Constraint (Indic)
4230       then
4231          Set_Scalar_Range (Derived_Type,
4232            Make_Range (Loc,
4233              Low_Bound  => New_Copy_Tree (Type_Low_Bound  (Parent_Type)),
4234              High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
4235          Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
4236
4237          if Has_Infinities (Parent_Type) then
4238             Set_Includes_Infinities (Scalar_Range (Derived_Type));
4239          end if;
4240       end if;
4241
4242       --  Set remaining type-specific fields, depending on numeric type
4243
4244       if Is_Modular_Integer_Type (Parent_Type) then
4245          Set_Modulus (Implicit_Base, Modulus (Parent_Base));
4246
4247          Set_Non_Binary_Modulus
4248            (Implicit_Base, Non_Binary_Modulus (Parent_Base));
4249
4250       elsif Is_Floating_Point_Type (Parent_Type) then
4251
4252          --  Digits of base type is always copied from the digits value of
4253          --  the parent base type, but the digits of the derived type will
4254          --  already have been set if there was a constraint present.
4255
4256          Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
4257          Set_Vax_Float    (Implicit_Base, Vax_Float    (Parent_Base));
4258
4259          if No_Constraint then
4260             Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
4261          end if;
4262
4263       elsif Is_Fixed_Point_Type (Parent_Type) then
4264
4265          --  Small of base type and derived type are always copied from the
4266          --  parent base type, since smalls never change. The delta of the
4267          --  base type is also copied from the parent base type. However the
4268          --  delta of the derived type will have been set already if a
4269          --  constraint was present.
4270
4271          Set_Small_Value (Derived_Type,  Small_Value (Parent_Base));
4272          Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
4273          Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
4274
4275          if No_Constraint then
4276             Set_Delta_Value (Derived_Type,  Delta_Value (Parent_Type));
4277          end if;
4278
4279          --  The scale and machine radix in the decimal case are always
4280          --  copied from the parent base type.
4281
4282          if Is_Decimal_Fixed_Point_Type (Parent_Type) then
4283             Set_Scale_Value (Derived_Type,  Scale_Value (Parent_Base));
4284             Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
4285
4286             Set_Machine_Radix_10
4287               (Derived_Type,  Machine_Radix_10 (Parent_Base));
4288             Set_Machine_Radix_10
4289               (Implicit_Base, Machine_Radix_10 (Parent_Base));
4290
4291             Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
4292
4293             if No_Constraint then
4294                Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
4295
4296             else
4297                --  the analysis of the subtype_indication sets the
4298                --  digits value of the derived type.
4299
4300                null;
4301             end if;
4302          end if;
4303       end if;
4304
4305       --  The type of the bounds is that of the parent type, and they
4306       --  must be converted to the derived type.
4307
4308       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
4309
4310       --  The implicit_base should be frozen when the derived type is frozen,
4311       --  but note that it is used in the conversions of the bounds. For fixed
4312       --  types we delay the determination of the bounds until the proper
4313       --  freezing point. For other numeric types this is rejected by GCC, for
4314       --  reasons that are currently unclear (???), so we choose to freeze the
4315       --  implicit base now. In the case of integers and floating point types
4316       --  this is harmless because subsequent representation clauses cannot
4317       --  affect anything, but it is still baffling that we cannot use the
4318       --  same mechanism for all derived numeric types.
4319
4320       if Is_Fixed_Point_Type (Parent_Type) then
4321          Conditional_Delay (Implicit_Base, Parent_Type);
4322       else
4323          Freeze_Before (N, Implicit_Base);
4324       end if;
4325    end Build_Derived_Numeric_Type;
4326
4327    --------------------------------
4328    -- Build_Derived_Private_Type --
4329    --------------------------------
4330
4331    procedure Build_Derived_Private_Type
4332      (N             : Node_Id;
4333       Parent_Type   : Entity_Id;
4334       Derived_Type  : Entity_Id;
4335       Is_Completion : Boolean;
4336       Derive_Subps  : Boolean := True)
4337    is
4338       Der_Base    : Entity_Id;
4339       Discr       : Entity_Id;
4340       Full_Decl   : Node_Id := Empty;
4341       Full_Der    : Entity_Id;
4342       Full_P      : Entity_Id;
4343       Last_Discr  : Entity_Id;
4344       Par_Scope   : constant Entity_Id := Scope (Base_Type (Parent_Type));
4345       Swapped     : Boolean := False;
4346
4347       procedure Copy_And_Build;
4348       --  Copy derived type declaration, replace parent with its full view,
4349       --  and analyze new declaration.
4350
4351       --------------------
4352       -- Copy_And_Build --
4353       --------------------
4354
4355       procedure Copy_And_Build is
4356          Full_N : Node_Id;
4357
4358       begin
4359          if Ekind (Parent_Type) in Record_Kind
4360            or else
4361              (Ekind (Parent_Type) in Enumeration_Kind
4362                and then Root_Type (Parent_Type) /= Standard_Character
4363                and then Root_Type (Parent_Type) /= Standard_Wide_Character
4364                and then Root_Type (Parent_Type) /= Standard_Wide_Wide_Character
4365                and then not Is_Generic_Type (Root_Type (Parent_Type)))
4366          then
4367             Full_N := New_Copy_Tree (N);
4368             Insert_After (N, Full_N);
4369             Build_Derived_Type (
4370               Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
4371
4372          else
4373             Build_Derived_Type (
4374               N, Parent_Type, Full_Der, True, Derive_Subps => False);
4375          end if;
4376       end Copy_And_Build;
4377
4378    --  Start of processing for Build_Derived_Private_Type
4379
4380    begin
4381       if Is_Tagged_Type (Parent_Type) then
4382          Build_Derived_Record_Type
4383            (N, Parent_Type, Derived_Type, Derive_Subps);
4384          return;
4385
4386       elsif Has_Discriminants (Parent_Type) then
4387          if Present (Full_View (Parent_Type)) then
4388             if not Is_Completion then
4389
4390                --  Copy declaration for subsequent analysis, to provide a
4391                --  completion for what is a private declaration. Indicate that
4392                --  the full type is internally generated.
4393
4394                Full_Decl := New_Copy_Tree (N);
4395                Full_Der  := New_Copy (Derived_Type);
4396                Set_Comes_From_Source (Full_Decl, False);
4397
4398                Insert_After (N, Full_Decl);
4399
4400             else
4401                --  If this is a completion, the full view being built is
4402                --  itself private. We build a subtype of the parent with
4403                --  the same constraints as this full view, to convey to the
4404                --  back end the constrained components and the size of this
4405                --  subtype. If the parent is constrained, its full view can
4406                --  serve as the underlying full view of the derived type.
4407
4408                if No (Discriminant_Specifications (N)) then
4409                   if Nkind (Subtype_Indication (Type_Definition (N))) =
4410                                                         N_Subtype_Indication
4411                   then
4412                      Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
4413
4414                   elsif Is_Constrained (Full_View (Parent_Type)) then
4415                      Set_Underlying_Full_View (Derived_Type,
4416                        Full_View (Parent_Type));
4417                   end if;
4418
4419                else
4420                   --  If there are new discriminants, the parent subtype is
4421                   --  constrained by them, but it is not clear how to build
4422                   --  the underlying_full_view in this case ???
4423
4424                   null;
4425                end if;
4426             end if;
4427          end if;
4428
4429          --  Build partial view of derived type from partial view of parent
4430
4431          Build_Derived_Record_Type
4432            (N, Parent_Type, Derived_Type, Derive_Subps);
4433
4434          if Present (Full_View (Parent_Type))
4435            and then not Is_Completion
4436          then
4437             if not In_Open_Scopes (Par_Scope)
4438               or else not In_Same_Source_Unit (N, Parent_Type)
4439             then
4440                --  Swap partial and full views temporarily
4441
4442                Install_Private_Declarations (Par_Scope);
4443                Install_Visible_Declarations (Par_Scope);
4444                Swapped := True;
4445             end if;
4446
4447             --  Build full view of derived type from full view of parent which
4448             --  is now installed. Subprograms have been derived on the partial
4449             --  view, the completion does not derive them anew.
4450
4451             if not Is_Tagged_Type (Parent_Type) then
4452                Build_Derived_Record_Type
4453                  (Full_Decl, Parent_Type, Full_Der, False);
4454
4455             else
4456                --  If full view of parent is tagged, the completion
4457                --  inherits the proper primitive operations.
4458
4459                Set_Defining_Identifier (Full_Decl, Full_Der);
4460                Build_Derived_Record_Type
4461                  (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
4462                Set_Analyzed (Full_Decl);
4463             end if;
4464
4465             if Swapped then
4466                Uninstall_Declarations (Par_Scope);
4467
4468                if In_Open_Scopes (Par_Scope) then
4469                   Install_Visible_Declarations (Par_Scope);
4470                end if;
4471             end if;
4472
4473             Der_Base := Base_Type (Derived_Type);
4474             Set_Full_View (Derived_Type, Full_Der);
4475             Set_Full_View (Der_Base, Base_Type (Full_Der));
4476
4477             --  Copy the discriminant list from full view to the partial views
4478             --  (base type and its subtype). Gigi requires that the partial
4479             --  and full views have the same discriminants.
4480
4481             --  Note that since the partial view is pointing to discriminants
4482             --  in the full view, their scope will be that of the full view.
4483             --  This might cause some front end problems and need
4484             --  adjustment???
4485
4486             Discr := First_Discriminant (Base_Type (Full_Der));
4487             Set_First_Entity (Der_Base, Discr);
4488
4489             loop
4490                Last_Discr := Discr;
4491                Next_Discriminant (Discr);
4492                exit when No (Discr);
4493             end loop;
4494
4495             Set_Last_Entity (Der_Base, Last_Discr);
4496
4497             Set_First_Entity (Derived_Type, First_Entity (Der_Base));
4498             Set_Last_Entity  (Derived_Type, Last_Entity  (Der_Base));
4499             Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
4500
4501          else
4502             --  If this is a completion, the derived type stays private
4503             --  and there is no need to create a further full view, except
4504             --  in the unusual case when the derivation is nested within a
4505             --  child unit, see below.
4506
4507             null;
4508          end if;
4509
4510       elsif Present (Full_View (Parent_Type))
4511         and then  Has_Discriminants (Full_View (Parent_Type))
4512       then
4513          if Has_Unknown_Discriminants (Parent_Type)
4514            and then Nkind (Subtype_Indication (Type_Definition (N)))
4515              = N_Subtype_Indication
4516          then
4517             Error_Msg_N
4518               ("cannot constrain type with unknown discriminants",
4519                Subtype_Indication (Type_Definition (N)));
4520             return;
4521          end if;
4522
4523          --  If full view of parent is a record type, Build full view as
4524          --  a derivation from the parent's full view. Partial view remains
4525          --  private. For code generation and linking, the full view must
4526          --  have the same public status as the partial one. This full view
4527          --  is only needed if the parent type is in an enclosing scope, so
4528          --  that the full view may actually become visible, e.g. in a child
4529          --  unit. This is both more efficient, and avoids order of freezing
4530          --  problems with the added entities.
4531
4532          if not Is_Private_Type (Full_View (Parent_Type))
4533            and then (In_Open_Scopes (Scope (Parent_Type)))
4534          then
4535             Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
4536                                               Chars (Derived_Type));
4537             Set_Is_Itype (Full_Der);
4538             Set_Has_Private_Declaration (Full_Der);
4539             Set_Has_Private_Declaration (Derived_Type);
4540             Set_Associated_Node_For_Itype (Full_Der, N);
4541             Set_Parent (Full_Der, Parent (Derived_Type));
4542             Set_Full_View (Derived_Type, Full_Der);
4543             Set_Is_Public (Full_Der, Is_Public (Derived_Type));
4544             Full_P := Full_View (Parent_Type);
4545             Exchange_Declarations (Parent_Type);
4546             Copy_And_Build;
4547             Exchange_Declarations (Full_P);
4548
4549          else
4550             Build_Derived_Record_Type
4551               (N, Full_View (Parent_Type), Derived_Type,
4552                 Derive_Subps => False);
4553          end if;
4554
4555          --  In any case, the primitive operations are inherited from
4556          --  the parent type, not from the internal full view.
4557
4558          Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
4559
4560          if Derive_Subps then
4561             Derive_Subprograms (Parent_Type, Derived_Type);
4562          end if;
4563
4564       else
4565          --  Untagged type, No discriminants on either view
4566
4567          if Nkind (Subtype_Indication (Type_Definition (N))) =
4568                                                    N_Subtype_Indication
4569          then
4570             Error_Msg_N
4571               ("illegal constraint on type without discriminants", N);
4572          end if;
4573
4574          if Present (Discriminant_Specifications (N))
4575            and then Present (Full_View (Parent_Type))
4576            and then not Is_Tagged_Type (Full_View (Parent_Type))
4577          then
4578             Error_Msg_N
4579               ("cannot add discriminants to untagged type", N);
4580          end if;
4581
4582          Set_Stored_Constraint (Derived_Type, No_Elist);
4583          Set_Is_Constrained    (Derived_Type, Is_Constrained (Parent_Type));
4584          Set_Is_Controlled     (Derived_Type, Is_Controlled  (Parent_Type));
4585          Set_Has_Controlled_Component
4586                                (Derived_Type, Has_Controlled_Component
4587                                                              (Parent_Type));
4588
4589          --  Direct controlled types do not inherit Finalize_Storage_Only flag
4590
4591          if not Is_Controlled  (Parent_Type) then
4592             Set_Finalize_Storage_Only
4593               (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
4594          end if;
4595
4596          --  Construct the implicit full view by deriving from full view of
4597          --  the parent type. In order to get proper visibility, we install
4598          --  the parent scope and its declarations.
4599
4600          --  ??? if the parent is untagged private and its completion is
4601          --  tagged, this mechanism will not work because we cannot derive
4602          --  from the tagged full view unless we have an extension
4603
4604          if Present (Full_View (Parent_Type))
4605            and then not Is_Tagged_Type (Full_View (Parent_Type))
4606            and then not Is_Completion
4607          then
4608             Full_Der :=
4609               Make_Defining_Identifier (Sloc (Derived_Type),
4610                 Chars => Chars (Derived_Type));
4611             Set_Is_Itype (Full_Der);
4612             Set_Has_Private_Declaration (Full_Der);
4613             Set_Has_Private_Declaration (Derived_Type);
4614             Set_Associated_Node_For_Itype (Full_Der, N);
4615             Set_Parent (Full_Der, Parent (Derived_Type));
4616             Set_Full_View (Derived_Type, Full_Der);
4617
4618             if not In_Open_Scopes (Par_Scope) then
4619                Install_Private_Declarations (Par_Scope);
4620                Install_Visible_Declarations (Par_Scope);
4621                Copy_And_Build;
4622                Uninstall_Declarations (Par_Scope);
4623
4624             --  If parent scope is open and in another unit, and parent has a
4625             --  completion, then the derivation is taking place in the visible
4626             --  part of a child unit. In that case retrieve the full view of
4627             --  the parent momentarily.
4628
4629             elsif not In_Same_Source_Unit (N, Parent_Type) then
4630                Full_P := Full_View (Parent_Type);
4631                Exchange_Declarations (Parent_Type);
4632                Copy_And_Build;
4633                Exchange_Declarations (Full_P);
4634
4635             --  Otherwise it is a local derivation
4636
4637             else
4638                Copy_And_Build;
4639             end if;
4640
4641             Set_Scope                (Full_Der, Current_Scope);
4642             Set_Is_First_Subtype     (Full_Der,
4643                                        Is_First_Subtype (Derived_Type));
4644             Set_Has_Size_Clause      (Full_Der, False);
4645             Set_Has_Alignment_Clause (Full_Der, False);
4646             Set_Next_Entity          (Full_Der, Empty);
4647             Set_Has_Delayed_Freeze   (Full_Der);
4648             Set_Is_Frozen            (Full_Der, False);
4649             Set_Freeze_Node          (Full_Der, Empty);
4650             Set_Depends_On_Private   (Full_Der,
4651                                         Has_Private_Component    (Full_Der));
4652             Set_Public_Status        (Full_Der);
4653          end if;
4654       end if;
4655
4656       Set_Has_Unknown_Discriminants (Derived_Type,
4657         Has_Unknown_Discriminants (Parent_Type));
4658
4659       if Is_Private_Type (Derived_Type) then
4660          Set_Private_Dependents (Derived_Type, New_Elmt_List);
4661       end if;
4662
4663       if Is_Private_Type (Parent_Type)
4664         and then Base_Type (Parent_Type) = Parent_Type
4665         and then In_Open_Scopes (Scope (Parent_Type))
4666       then
4667          Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
4668
4669          if Is_Child_Unit (Scope (Current_Scope))
4670            and then Is_Completion
4671            and then In_Private_Part (Current_Scope)
4672            and then Scope (Parent_Type) /= Current_Scope
4673          then
4674             --  This is the unusual case where a type completed by a private
4675             --  derivation occurs within a package nested in a child unit,
4676             --  and the parent is declared in an ancestor. In this case, the
4677             --  full view of the parent type will become visible in the body
4678             --  of the enclosing child, and only then will the current type
4679             --  be possibly non-private. We build a underlying full view that
4680             --  will be installed when the enclosing child body is compiled.
4681
4682             declare
4683                IR : constant Node_Id := Make_Itype_Reference (Sloc (N));
4684
4685             begin
4686                Full_Der :=
4687                  Make_Defining_Identifier (Sloc (Derived_Type),
4688                    Chars (Derived_Type));
4689                Set_Is_Itype (Full_Der);
4690                Set_Itype (IR, Full_Der);
4691                Insert_After (N, IR);
4692
4693                --  The full view will be used to swap entities on entry/exit
4694                --  to the body, and must appear in the entity list for the
4695                --  package.
4696
4697                Append_Entity (Full_Der, Scope (Derived_Type));
4698                Set_Has_Private_Declaration (Full_Der);
4699                Set_Has_Private_Declaration (Derived_Type);
4700                Set_Associated_Node_For_Itype (Full_Der, N);
4701                Set_Parent (Full_Der, Parent (Derived_Type));
4702                Full_P := Full_View (Parent_Type);
4703                Exchange_Declarations (Parent_Type);
4704                Copy_And_Build;
4705                Exchange_Declarations (Full_P);
4706                Set_Underlying_Full_View (Derived_Type, Full_Der);
4707             end;
4708          end if;
4709       end if;
4710    end Build_Derived_Private_Type;
4711
4712    -------------------------------
4713    -- Build_Derived_Record_Type --
4714    -------------------------------
4715
4716    --  1. INTRODUCTION
4717
4718    --  Ideally we would like to use the same model of type derivation for
4719    --  tagged and untagged record types. Unfortunately this is not quite
4720    --  possible because the semantics of representation clauses is different
4721    --  for tagged and untagged records under inheritance. Consider the
4722    --  following:
4723
4724    --     type R (...) is [tagged] record ... end record;
4725    --     type T (...) is new R (...) [with ...];
4726
4727    --  The representation clauses of T can specify a completely different
4728    --  record layout from R's. Hence the same component can be placed in
4729    --  two very different positions in objects of type T and R. If R and T
4730    --  are tagged types, representation clauses for T can only specify the
4731    --  layout of non inherited components, thus components that are common
4732    --  in R and T have the same position in objects of type R and T.
4733
4734    --  This has two implications. The first is that the entire tree for R's
4735    --  declaration needs to be copied for T in the untagged case, so that T
4736    --  can be viewed as a record type of its own with its own representation
4737    --  clauses. The second implication is the way we handle discriminants.
4738    --  Specifically, in the untagged case we need a way to communicate to Gigi
4739    --  what are the real discriminants in the record, while for the semantics
4740    --  we need to consider those introduced by the user to rename the
4741    --  discriminants in the parent type. This is handled by introducing the
4742    --  notion of stored discriminants. See below for more.
4743
4744    --  Fortunately the way regular components are inherited can be handled in
4745    --  the same way in tagged and untagged types.
4746
4747    --  To complicate things a bit more the private view of a private extension
4748    --  cannot be handled in the same way as the full view (for one thing the
4749    --  semantic rules are somewhat different). We will explain what differs
4750    --  below.
4751
4752    --  2. DISCRIMINANTS UNDER INHERITANCE
4753
4754    --  The semantic rules governing the discriminants of derived types are
4755    --  quite subtle.
4756
4757    --   type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
4758    --      [abstract]  Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
4759
4760    --  If parent type has discriminants, then the discriminants that are
4761    --  declared in the derived type are [3.4 (11)]:
4762
4763    --  o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
4764    --    there is one;
4765
4766    --  o Otherwise, each discriminant of the parent type (implicitly declared
4767    --    in the same order with the same specifications). In this case, the
4768    --    discriminants are said to be "inherited", or if unknown in the parent
4769    --    are also unknown in the derived type.
4770
4771    --  Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
4772
4773    --  o The parent subtype shall be constrained;
4774
4775    --  o If the parent type is not a tagged type, then each discriminant of
4776    --    the derived type shall be used in the constraint defining a parent
4777    --    subtype [Implementation note: this ensures that the new discriminant
4778    --    can share storage with an existing discriminant.].
4779
4780    --  For the derived type each discriminant of the parent type is either
4781    --  inherited, constrained to equal some new discriminant of the derived
4782    --  type, or constrained to the value of an expression.
4783
4784    --  When inherited or constrained to equal some new discriminant, the
4785    --  parent discriminant and the discriminant of the derived type are said
4786    --  to "correspond".
4787
4788    --  If a discriminant of the parent type is constrained to a specific value
4789    --  in the derived type definition, then the discriminant is said to be
4790    --  "specified" by that derived type definition.
4791
4792    --  3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
4793
4794    --  We have spoken about stored discriminants in point 1 (introduction)
4795    --  above. There are two sort of stored discriminants: implicit and
4796    --  explicit. As long as the derived type inherits the same discriminants as
4797    --  the root record type, stored discriminants are the same as regular
4798    --  discriminants, and are said to be implicit. However, if any discriminant
4799    --  in the root type was renamed in the derived type, then the derived
4800    --  type will contain explicit stored discriminants. Explicit stored
4801    --  discriminants are discriminants in addition to the semantically visible
4802    --  discriminants defined for the derived type. Stored discriminants are
4803    --  used by Gigi to figure out what are the physical discriminants in
4804    --  objects of the derived type (see precise definition in einfo.ads).
4805    --  As an example, consider the following:
4806
4807    --           type R  (D1, D2, D3 : Int) is record ... end record;
4808    --           type T1 is new R;
4809    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
4810    --           type T3 is new T2;
4811    --           type T4 (Y : Int) is new T3 (Y, 99);
4812
4813    --  The following table summarizes the discriminants and stored
4814    --  discriminants in R and T1 through T4.
4815
4816    --   Type      Discrim     Stored Discrim  Comment
4817    --    R      (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in R
4818    --    T1     (D1, D2, D3)   (D1, D2, D3)   Girder discrims implicit in T1
4819    --    T2     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T2
4820    --    T3     (X1, X2)       (D1, D2, D3)   Girder discrims EXPLICIT in T3
4821    --    T4     (Y)            (D1, D2, D3)   Girder discrims EXPLICIT in T4
4822
4823    --  Field Corresponding_Discriminant (abbreviated CD below) allows us to
4824    --  find the corresponding discriminant in the parent type, while
4825    --  Original_Record_Component (abbreviated ORC below), the actual physical
4826    --  component that is renamed. Finally the field Is_Completely_Hidden
4827    --  (abbreviated ICH below) is set for all explicit stored discriminants
4828    --  (see einfo.ads for more info). For the above example this gives:
4829
4830    --                 Discrim     CD        ORC     ICH
4831    --                 ^^^^^^^     ^^        ^^^     ^^^
4832    --                 D1 in R    empty     itself    no
4833    --                 D2 in R    empty     itself    no
4834    --                 D3 in R    empty     itself    no
4835
4836    --                 D1 in T1  D1 in R    itself    no
4837    --                 D2 in T1  D2 in R    itself    no
4838    --                 D3 in T1  D3 in R    itself    no
4839
4840    --                 X1 in T2  D3 in T1  D3 in T2   no
4841    --                 X2 in T2  D1 in T1  D1 in T2   no
4842    --                 D1 in T2   empty    itself    yes
4843    --                 D2 in T2   empty    itself    yes
4844    --                 D3 in T2   empty    itself    yes
4845
4846    --                 X1 in T3  X1 in T2  D3 in T3   no
4847    --                 X2 in T3  X2 in T2  D1 in T3   no
4848    --                 D1 in T3   empty    itself    yes
4849    --                 D2 in T3   empty    itself    yes
4850    --                 D3 in T3   empty    itself    yes
4851
4852    --                 Y  in T4  X1 in T3  D3 in T3   no
4853    --                 D1 in T3   empty    itself    yes
4854    --                 D2 in T3   empty    itself    yes
4855    --                 D3 in T3   empty    itself    yes
4856
4857    --  4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
4858
4859    --  Type derivation for tagged types is fairly straightforward. if no
4860    --  discriminants are specified by the derived type, these are inherited
4861    --  from the parent. No explicit stored discriminants are ever necessary.
4862    --  The only manipulation that is done to the tree is that of adding a
4863    --  _parent field with parent type and constrained to the same constraint
4864    --  specified for the parent in the derived type definition. For instance:
4865
4866    --           type R  (D1, D2, D3 : Int) is tagged record ... end record;
4867    --           type T1 is new R with null record;
4868    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
4869
4870    --  are changed into:
4871
4872    --           type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
4873    --              _parent : R (D1, D2, D3);
4874    --           end record;
4875
4876    --           type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
4877    --              _parent : T1 (X2, 88, X1);
4878    --           end record;
4879
4880    --  The discriminants actually present in R, T1 and T2 as well as their CD,
4881    --  ORC and ICH fields are:
4882
4883    --                 Discrim     CD        ORC     ICH
4884    --                 ^^^^^^^     ^^        ^^^     ^^^
4885    --                 D1 in R    empty     itself    no
4886    --                 D2 in R    empty     itself    no
4887    --                 D3 in R    empty     itself    no
4888
4889    --                 D1 in T1  D1 in R    D1 in R   no
4890    --                 D2 in T1  D2 in R    D2 in R   no
4891    --                 D3 in T1  D3 in R    D3 in R   no
4892
4893    --                 X1 in T2  D3 in T1   D3 in R   no
4894    --                 X2 in T2  D1 in T1   D1 in R   no
4895
4896    --  5. FIRST TRANSFORMATION FOR DERIVED RECORDS
4897    --
4898    --  Regardless of whether we dealing with a tagged or untagged type
4899    --  we will transform all derived type declarations of the form
4900    --
4901    --               type T is new R (...) [with ...];
4902    --  or
4903    --               subtype S is R (...);
4904    --               type T is new S [with ...];
4905    --  into
4906    --               type BT is new R [with ...];
4907    --               subtype T is BT (...);
4908    --
4909    --  That is, the base derived type is constrained only if it has no
4910    --  discriminants. The reason for doing this is that GNAT's semantic model
4911    --  assumes that a base type with discriminants is unconstrained.
4912    --
4913    --  Note that, strictly speaking, the above transformation is not always
4914    --  correct. Consider for instance the following excerpt from ACVC b34011a:
4915    --
4916    --       procedure B34011A is
4917    --          type REC (D : integer := 0) is record
4918    --             I : Integer;
4919    --          end record;
4920
4921    --          package P is
4922    --             type T6 is new Rec;
4923    --             function F return T6;
4924    --          end P;
4925
4926    --          use P;
4927    --          package Q6 is
4928    --             type U is new T6 (Q6.F.I);                   -- ERROR: Q6.F.
4929    --          end Q6;
4930    --
4931    --  The definition of Q6.U is illegal. However transforming Q6.U into
4932
4933    --             type BaseU is new T6;
4934    --             subtype U is BaseU (Q6.F.I)
4935
4936    --  turns U into a legal subtype, which is incorrect. To avoid this problem
4937    --  we always analyze the constraint (in this case (Q6.F.I)) before applying
4938    --  the transformation described above.
4939
4940    --  There is another instance where the above transformation is incorrect.
4941    --  Consider:
4942
4943    --          package Pack is
4944    --             type Base (D : Integer) is tagged null record;
4945    --             procedure P (X : Base);
4946
4947    --             type Der is new Base (2) with null record;
4948    --             procedure P (X : Der);
4949    --          end Pack;
4950
4951    --  Then the above transformation turns this into
4952
4953    --             type Der_Base is new Base with null record;
4954    --             --  procedure P (X : Base) is implicitly inherited here
4955    --             --  as procedure P (X : Der_Base).
4956
4957    --             subtype Der is Der_Base (2);
4958    --             procedure P (X : Der);
4959    --             --  The overriding of P (X : Der_Base) is illegal since we
4960    --             --  have a parameter conformance problem.
4961
4962    --  To get around this problem, after having semantically processed Der_Base
4963    --  and the rewritten subtype declaration for Der, we copy Der_Base field
4964    --  Discriminant_Constraint from Der so that when parameter conformance is
4965    --  checked when P is overridden, no semantic errors are flagged.
4966
4967    --  6. SECOND TRANSFORMATION FOR DERIVED RECORDS
4968
4969    --  Regardless of whether we are dealing with a tagged or untagged type
4970    --  we will transform all derived type declarations of the form
4971
4972    --               type R (D1, .., Dn : ...) is [tagged] record ...;
4973    --               type T is new R [with ...];
4974    --  into
4975    --               type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
4976
4977    --  The reason for such transformation is that it allows us to implement a
4978    --  very clean form of component inheritance as explained below.
4979
4980    --  Note that this transformation is not achieved by direct tree rewriting
4981    --  and manipulation, but rather by redoing the semantic actions that the
4982    --  above transformation will entail. This is done directly in routine
4983    --  Inherit_Components.
4984
4985    --  7. TYPE DERIVATION AND COMPONENT INHERITANCE
4986
4987    --  In both tagged and untagged derived types, regular non discriminant
4988    --  components are inherited in the derived type from the parent type. In
4989    --  the absence of discriminants component, inheritance is straightforward
4990    --  as components can simply be copied from the parent.
4991
4992    --  If the parent has discriminants, inheriting components constrained with
4993    --  these discriminants requires caution. Consider the following example:
4994
4995    --      type R  (D1, D2 : Positive) is [tagged] record
4996    --         S : String (D1 .. D2);
4997    --      end record;
4998
4999    --      type T1                is new R        [with null record];
5000    --      type T2 (X : positive) is new R (1, X) [with null record];
5001
5002    --  As explained in 6. above, T1 is rewritten as
5003    --      type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
5004    --  which makes the treatment for T1 and T2 identical.
5005
5006    --  What we want when inheriting S, is that references to D1 and D2 in R are
5007    --  replaced with references to their correct constraints, ie D1 and D2 in
5008    --  T1 and 1 and X in T2. So all R's discriminant references are replaced
5009    --  with either discriminant references in the derived type or expressions.
5010    --  This replacement is achieved as follows: before inheriting R's
5011    --  components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
5012    --  created in the scope of T1 (resp. scope of T2) so that discriminants D1
5013    --  and D2 of T1 are visible (resp. discriminant X of T2 is visible).
5014    --  For T2, for instance, this has the effect of replacing String (D1 .. D2)
5015    --  by String (1 .. X).
5016
5017    --  8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
5018
5019    --  We explain here the rules governing private type extensions relevant to
5020    --  type derivation. These rules are explained on the following example:
5021
5022    --      type D [(...)] is new A [(...)] with private;      <-- partial view
5023    --      type D [(...)] is new P [(...)] with null record;  <-- full view
5024
5025    --  Type A is called the ancestor subtype of the private extension.
5026    --  Type P is the parent type of the full view of the private extension. It
5027    --  must be A or a type derived from A.
5028
5029    --  The rules concerning the discriminants of private type extensions are
5030    --  [7.3(10-13)]:
5031
5032    --  o If a private extension inherits known discriminants from the ancestor
5033    --    subtype, then the full view shall also inherit its discriminants from
5034    --    the ancestor subtype and the parent subtype of the full view shall be
5035    --    constrained if and only if the ancestor subtype is constrained.
5036
5037    --  o If a partial view has unknown discriminants, then the full view may
5038    --    define a definite or an indefinite subtype, with or without
5039    --    discriminants.
5040
5041    --  o If a partial view has neither known nor unknown discriminants, then
5042    --    the full view shall define a definite subtype.
5043
5044    --  o If the ancestor subtype of a private extension has constrained
5045    --    discriminants, then the parent subtype of the full view shall impose a
5046    --    statically matching constraint on those discriminants.
5047
5048    --  This means that only the following forms of private extensions are
5049    --  allowed:
5050
5051    --      type D is new A with private;      <-- partial view
5052    --      type D is new P with null record;  <-- full view
5053
5054    --  If A has no discriminants than P has no discriminants, otherwise P must
5055    --  inherit A's discriminants.
5056
5057    --      type D is new A (...) with private;      <-- partial view
5058    --      type D is new P (:::) with null record;  <-- full view
5059
5060    --  P must inherit A's discriminants and (...) and (:::) must statically
5061    --  match.
5062
5063    --      subtype A is R (...);
5064    --      type D is new A with private;      <-- partial view
5065    --      type D is new P with null record;  <-- full view
5066
5067    --  P must have inherited R's discriminants and must be derived from A or
5068    --  any of its subtypes.
5069
5070    --      type D (..) is new A with private;              <-- partial view
5071    --      type D (..) is new P [(:::)] with null record;  <-- full view
5072
5073    --  No specific constraints on P's discriminants or constraint (:::).
5074    --  Note that A can be unconstrained, but the parent subtype P must either
5075    --  be constrained or (:::) must be present.
5076
5077    --      type D (..) is new A [(...)] with private;      <-- partial view
5078    --      type D (..) is new P [(:::)] with null record;  <-- full view
5079
5080    --  P's constraints on A's discriminants must statically match those
5081    --  imposed by (...).
5082
5083    --  9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
5084
5085    --  The full view of a private extension is handled exactly as described
5086    --  above. The model chose for the private view of a private extension is
5087    --  the same for what concerns discriminants (ie they receive the same
5088    --  treatment as in the tagged case). However, the private view of the
5089    --  private extension always inherits the components of the parent base,
5090    --  without replacing any discriminant reference. Strictly speaking this is
5091    --  incorrect. However, Gigi never uses this view to generate code so this
5092    --  is a purely semantic issue. In theory, a set of transformations similar
5093    --  to those given in 5. and 6. above could be applied to private views of
5094    --  private extensions to have the same model of component inheritance as
5095    --  for non private extensions. However, this is not done because it would
5096    --  further complicate private type processing. Semantically speaking, this
5097    --  leaves us in an uncomfortable situation. As an example consider:
5098
5099    --          package Pack is
5100    --             type R (D : integer) is tagged record
5101    --                S : String (1 .. D);
5102    --             end record;
5103    --             procedure P (X : R);
5104    --             type T is new R (1) with private;
5105    --          private
5106    --             type T is new R (1) with null record;
5107    --          end;
5108
5109    --  This is transformed into:
5110
5111    --          package Pack is
5112    --             type R (D : integer) is tagged record
5113    --                S : String (1 .. D);
5114    --             end record;
5115    --             procedure P (X : R);
5116    --             type T is new R (1) with private;
5117    --          private
5118    --             type BaseT is new R with null record;
5119    --             subtype  T is BaseT (1);
5120    --          end;
5121
5122    --  (strictly speaking the above is incorrect Ada)
5123
5124    --  From the semantic standpoint the private view of private extension T
5125    --  should be flagged as constrained since one can clearly have
5126    --
5127    --             Obj : T;
5128    --
5129    --  in a unit withing Pack. However, when deriving subprograms for the
5130    --  private view of private extension T, T must be seen as unconstrained
5131    --  since T has discriminants (this is a constraint of the current
5132    --  subprogram derivation model). Thus, when processing the private view of
5133    --  a private extension such as T, we first mark T as unconstrained, we
5134    --  process it, we perform program derivation and just before returning from
5135    --  Build_Derived_Record_Type we mark T as constrained.
5136
5137    --  ??? Are there are other uncomfortable cases that we will have to
5138    --      deal with.
5139
5140    --  10. RECORD_TYPE_WITH_PRIVATE complications
5141
5142    --  Types that are derived from a visible record type and have a private
5143    --  extension present other peculiarities. They behave mostly like private
5144    --  types, but if they have primitive operations defined, these will not
5145    --  have the proper signatures for further inheritance, because other
5146    --  primitive operations will use the implicit base that we define for
5147    --  private derivations below. This affect subprogram inheritance (see
5148    --  Derive_Subprograms for details). We also derive the implicit base from
5149    --  the base type of the full view, so that the implicit base is a record
5150    --  type and not another private type, This avoids infinite loops.
5151
5152    procedure Build_Derived_Record_Type
5153      (N            : Node_Id;
5154       Parent_Type  : Entity_Id;
5155       Derived_Type : Entity_Id;
5156       Derive_Subps : Boolean := True)
5157    is
5158       Loc          : constant Source_Ptr := Sloc (N);
5159       Parent_Base  : Entity_Id;
5160       Type_Def     : Node_Id;
5161       Indic        : Node_Id;
5162       Discrim      : Entity_Id;
5163       Last_Discrim : Entity_Id;
5164       Constrs      : Elist_Id;
5165
5166       Discs        : Elist_Id := New_Elmt_List;
5167       --  An empty Discs list means that there were no constraints in the
5168       --  subtype indication or that there was an error processing it.
5169
5170       Assoc_List         : Elist_Id;
5171       New_Discrs         : Elist_Id;
5172       New_Base           : Entity_Id;
5173       New_Decl           : Node_Id;
5174       New_Indic          : Node_Id;
5175
5176       Is_Tagged          : constant Boolean := Is_Tagged_Type (Parent_Type);
5177       Discriminant_Specs : constant Boolean :=
5178                              Present (Discriminant_Specifications (N));
5179       Private_Extension  : constant Boolean :=
5180                              (Nkind (N) = N_Private_Extension_Declaration);
5181
5182       Constraint_Present     : Boolean;
5183       Has_Interfaces         : Boolean := False;
5184       Inherit_Discrims       : Boolean := False;
5185       Last_Inherited_Prim_Op : Elmt_Id;
5186       Tagged_Partial_View    : Entity_Id;
5187       Save_Etype             : Entity_Id;
5188       Save_Discr_Constr      : Elist_Id;
5189       Save_Next_Entity       : Entity_Id;
5190
5191    begin
5192       if Ekind (Parent_Type) = E_Record_Type_With_Private
5193         and then Present (Full_View (Parent_Type))
5194         and then Has_Discriminants (Parent_Type)
5195       then
5196          Parent_Base := Base_Type (Full_View (Parent_Type));
5197       else
5198          Parent_Base := Base_Type (Parent_Type);
5199       end if;
5200
5201       --  Before we start the previously documented transformations, here is
5202       --  a little fix for size and alignment of tagged types. Normally when
5203       --  we derive type D from type P, we copy the size and alignment of P
5204       --  as the default for D, and in the absence of explicit representation
5205       --  clauses for D, the size and alignment are indeed the same as the
5206       --  parent.
5207
5208       --  But this is wrong for tagged types, since fields may be added,
5209       --  and the default size may need to be larger, and the default
5210       --  alignment may need to be larger.
5211
5212       --  We therefore reset the size and alignment fields in the tagged
5213       --  case. Note that the size and alignment will in any case be at
5214       --  least as large as the parent type (since the derived type has
5215       --  a copy of the parent type in the _parent field)
5216
5217       if Is_Tagged then
5218          Init_Size_Align (Derived_Type);
5219       end if;
5220
5221       --  STEP 0a: figure out what kind of derived type declaration we have
5222
5223       if Private_Extension then
5224          Type_Def := N;
5225          Set_Ekind (Derived_Type, E_Record_Type_With_Private);
5226
5227       else
5228          Type_Def := Type_Definition (N);
5229
5230          --  Ekind (Parent_Base) in not necessarily E_Record_Type since
5231          --  Parent_Base can be a private type or private extension. However,
5232          --  for tagged types with an extension the newly added fields are
5233          --  visible and hence the Derived_Type is always an E_Record_Type.
5234          --  (except that the parent may have its own private fields).
5235          --  For untagged types we preserve the Ekind of the Parent_Base.
5236
5237          if Present (Record_Extension_Part (Type_Def)) then
5238             Set_Ekind (Derived_Type, E_Record_Type);
5239          else
5240             Set_Ekind (Derived_Type, Ekind (Parent_Base));
5241          end if;
5242       end if;
5243
5244       --  Indic can either be an N_Identifier if the subtype indication
5245       --  contains no constraint or an N_Subtype_Indication if the subtype
5246       --  indication has a constraint.
5247
5248       Indic := Subtype_Indication (Type_Def);
5249       Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
5250
5251       --  Check that the type has visible discriminants. The type may be
5252       --  a private type with unknown discriminants whose full view has
5253       --  discriminants which are invisible.
5254
5255       if Constraint_Present then
5256          if not Has_Discriminants (Parent_Base)
5257            or else
5258              (Has_Unknown_Discriminants (Parent_Base)
5259                 and then Is_Private_Type (Parent_Base))
5260          then
5261             Error_Msg_N
5262               ("invalid constraint: type has no discriminant",
5263                  Constraint (Indic));
5264
5265             Constraint_Present := False;
5266             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
5267
5268          elsif Is_Constrained (Parent_Type) then
5269             Error_Msg_N
5270                ("invalid constraint: parent type is already constrained",
5271                   Constraint (Indic));
5272
5273             Constraint_Present := False;
5274             Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
5275          end if;
5276       end if;
5277
5278       --  STEP 0b: If needed, apply transformation given in point 5. above
5279
5280       if not Private_Extension
5281         and then Has_Discriminants (Parent_Type)
5282         and then not Discriminant_Specs
5283         and then (Is_Constrained (Parent_Type) or else Constraint_Present)
5284       then
5285          --  First, we must analyze the constraint (see comment in point 5.)
5286
5287          if Constraint_Present then
5288             New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
5289
5290             if Has_Discriminants (Derived_Type)
5291               and then Has_Private_Declaration (Derived_Type)
5292               and then Present (Discriminant_Constraint (Derived_Type))
5293             then
5294                --  Verify that constraints of the full view conform to those
5295                --  given in partial view.
5296
5297                declare
5298                   C1, C2 : Elmt_Id;
5299
5300                begin
5301                   C1 := First_Elmt (New_Discrs);
5302                   C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
5303
5304                   while Present (C1) and then Present (C2) loop
5305                      if not
5306                        Fully_Conformant_Expressions (Node (C1), Node (C2))
5307                      then
5308                         Error_Msg_N (
5309                           "constraint not conformant to previous declaration",
5310                              Node (C1));
5311                      end if;
5312                      Next_Elmt (C1);
5313                      Next_Elmt (C2);
5314                   end loop;
5315                end;
5316             end if;
5317          end if;
5318
5319          --  Insert and analyze the declaration for the unconstrained base type
5320
5321          New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
5322
5323          New_Decl :=
5324            Make_Full_Type_Declaration (Loc,
5325               Defining_Identifier => New_Base,
5326               Type_Definition     =>
5327                 Make_Derived_Type_Definition (Loc,
5328                   Abstract_Present      => Abstract_Present (Type_Def),
5329                   Subtype_Indication    =>
5330                     New_Occurrence_Of (Parent_Base, Loc),
5331                   Record_Extension_Part =>
5332                     Relocate_Node (Record_Extension_Part (Type_Def))));
5333
5334          Set_Parent (New_Decl, Parent (N));
5335          Mark_Rewrite_Insertion (New_Decl);
5336          Insert_Before (N, New_Decl);
5337
5338          --  Note that this call passes False for the Derive_Subps parameter
5339          --  because subprogram derivation is deferred until after creating
5340          --  the subtype (see below).
5341
5342          Build_Derived_Type
5343            (New_Decl, Parent_Base, New_Base,
5344             Is_Completion => True, Derive_Subps => False);
5345
5346          --  ??? This needs re-examination to determine whether the
5347          --  above call can simply be replaced by a call to Analyze.
5348
5349          Set_Analyzed (New_Decl);
5350
5351          --  Insert and analyze the declaration for the constrained subtype
5352
5353          if Constraint_Present then
5354             New_Indic :=
5355               Make_Subtype_Indication (Loc,
5356                 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
5357                 Constraint   => Relocate_Node (Constraint (Indic)));
5358
5359          else
5360             declare
5361                Constr_List : constant List_Id := New_List;
5362                C           : Elmt_Id;
5363                Expr        : Node_Id;
5364
5365             begin
5366                C := First_Elmt (Discriminant_Constraint (Parent_Type));
5367                while Present (C) loop
5368                   Expr := Node (C);
5369
5370                   --  It is safe here to call New_Copy_Tree since
5371                   --  Force_Evaluation was called on each constraint in
5372                   --  Build_Discriminant_Constraints.
5373
5374                   Append (New_Copy_Tree (Expr), To => Constr_List);
5375
5376                   Next_Elmt (C);
5377                end loop;
5378
5379                New_Indic :=
5380                  Make_Subtype_Indication (Loc,
5381                    Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
5382                    Constraint   =>
5383                      Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
5384             end;
5385          end if;
5386
5387          Rewrite (N,
5388            Make_Subtype_Declaration (Loc,
5389              Defining_Identifier => Derived_Type,
5390              Subtype_Indication  => New_Indic));
5391
5392          Analyze (N);
5393
5394          --  Derivation of subprograms must be delayed until the full subtype
5395          --  has been established to ensure proper overriding of subprograms
5396          --  inherited by full types. If the derivations occurred as part of
5397          --  the call to Build_Derived_Type above, then the check for type
5398          --  conformance would fail because earlier primitive subprograms
5399          --  could still refer to the full type prior the change to the new
5400          --  subtype and hence would not match the new base type created here.
5401
5402          Derive_Subprograms (Parent_Type, Derived_Type);
5403
5404          --  For tagged types the Discriminant_Constraint of the new base itype
5405          --  is inherited from the first subtype so that no subtype conformance
5406          --  problem arise when the first subtype overrides primitive
5407          --  operations inherited by the implicit base type.
5408
5409          if Is_Tagged then
5410             Set_Discriminant_Constraint
5411               (New_Base, Discriminant_Constraint (Derived_Type));
5412          end if;
5413
5414          return;
5415       end if;
5416
5417       --  If we get here Derived_Type will have no discriminants or it will be
5418       --  a discriminated unconstrained base type.
5419
5420       --  STEP 1a: perform preliminary actions/checks for derived tagged types
5421
5422       if Is_Tagged then
5423
5424          --  The parent type is frozen for non-private extensions (RM 13.14(7))
5425
5426          if not Private_Extension then
5427             Freeze_Before (N, Parent_Type);
5428          end if;
5429
5430          --  In Ada 2005 (AI-344), the restriction that a derived tagged type
5431          --  cannot be declared at a deeper level than its parent type is
5432          --  removed. The check on derivation within a generic body is also
5433          --  relaxed, but there's a restriction that a derived tagged type
5434          --  cannot be declared in a generic body if it's derived directly
5435          --  or indirectly from a formal type of that generic.
5436
5437          if Ada_Version >= Ada_05 then
5438             if Present (Enclosing_Generic_Body (Derived_Type)) then
5439                declare
5440                   Ancestor_Type : Entity_Id := Parent_Type;
5441
5442                begin
5443                   --  Check to see if any ancestor of the derived type is a
5444                   --  formal type.
5445
5446                   while not Is_Generic_Type (Ancestor_Type)
5447                     and then Etype (Ancestor_Type) /= Ancestor_Type
5448                   loop
5449                      Ancestor_Type := Etype (Ancestor_Type);
5450                   end loop;
5451
5452                   --  If the derived type does have a formal type as an
5453                   --  ancestor, then it's an error if the derived type is
5454                   --  declared within the body of the generic unit that
5455                   --  declares the formal type in its generic formal part. It's
5456                   --  sufficient to check whether the ancestor type is declared
5457                   --  inside the same generic body as the derived type (such as
5458                   --  within a nested generic spec), in which case the
5459                   --  derivation is legal. If the formal type is declared
5460                   --  outside of that generic body, then it's guaranteed that
5461                   --  the derived type is declared within the generic body of
5462                   --  the generic unit declaring the formal type.
5463
5464                   if Is_Generic_Type (Ancestor_Type)
5465                     and then Enclosing_Generic_Body (Ancestor_Type) /=
5466                                Enclosing_Generic_Body (Derived_Type)
5467                   then
5468                      Error_Msg_NE
5469                        ("parent type of& must not be descendant of formal type"
5470                           & " of an enclosing generic body",
5471                             Indic, Derived_Type);
5472                   end if;
5473                end;
5474             end if;
5475
5476          elsif Type_Access_Level (Derived_Type) /=
5477                  Type_Access_Level (Parent_Type)
5478            and then not Is_Generic_Type (Derived_Type)
5479          then
5480             if Is_Controlled (Parent_Type) then
5481                Error_Msg_N
5482                  ("controlled type must be declared at the library level",
5483                   Indic);
5484             else
5485                Error_Msg_N
5486                  ("type extension at deeper accessibility level than parent",
5487                   Indic);
5488             end if;
5489
5490          else
5491             declare
5492                GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
5493
5494             begin
5495                if Present (GB)
5496                  and then GB /= Enclosing_Generic_Body (Parent_Base)
5497                then
5498                   Error_Msg_NE
5499                     ("parent type of& must not be outside generic body"
5500                        & " ('R'M 3.9.1(4))",
5501                          Indic, Derived_Type);
5502                end if;
5503             end;
5504          end if;
5505       end if;
5506
5507       --  Ada 2005 (AI-251)
5508
5509       if Ada_Version = Ada_05
5510         and then Is_Tagged
5511       then
5512
5513          --  "The declaration of a specific descendant of an interface type
5514          --  freezes the interface type" (RM 13.14).
5515
5516          declare
5517             Iface : Node_Id;
5518          begin
5519             if Is_Non_Empty_List (Interface_List (Type_Def)) then
5520                Iface := First (Interface_List (Type_Def));
5521
5522                while Present (Iface) loop
5523                   Freeze_Before (N, Etype (Iface));
5524                   Next (Iface);
5525                end loop;
5526             end if;
5527          end;
5528       end if;
5529
5530       --  STEP 1b : preliminary cleanup of the full view of private types
5531
5532       --  If the type is already marked as having discriminants, then it's the
5533       --  completion of a private type or private extension and we need to
5534       --  retain the discriminants from the partial view if the current
5535       --  declaration has Discriminant_Specifications so that we can verify
5536       --  conformance. However, we must remove any existing components that
5537       --  were inherited from the parent (and attached in Copy_And_Swap)
5538       --  because the full type inherits all appropriate components anyway, and
5539       --  we do not want the partial view's components interfering.
5540
5541       if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
5542          Discrim := First_Discriminant (Derived_Type);
5543          loop
5544             Last_Discrim := Discrim;
5545             Next_Discriminant (Discrim);
5546             exit when No (Discrim);
5547          end loop;
5548
5549          Set_Last_Entity (Derived_Type, Last_Discrim);
5550
5551       --  In all other cases wipe out the list of inherited components (even
5552       --  inherited discriminants), it will be properly rebuilt here.
5553
5554       else
5555          Set_First_Entity (Derived_Type, Empty);
5556          Set_Last_Entity  (Derived_Type, Empty);
5557       end if;
5558
5559       --  STEP 1c: Initialize some flags for the Derived_Type
5560
5561       --  The following flags must be initialized here so that
5562       --  Process_Discriminants can check that discriminants of tagged types
5563       --  do not have a default initial value and that access discriminants
5564       --  are only specified for limited records. For completeness, these
5565       --  flags are also initialized along with all the other flags below.
5566
5567       Set_Is_Tagged_Type    (Derived_Type, Is_Tagged);
5568       Set_Is_Limited_Record (Derived_Type, Is_Limited_Record (Parent_Type));
5569
5570       --  STEP 2a: process discriminants of derived type if any
5571
5572       New_Scope (Derived_Type);
5573
5574       if Discriminant_Specs then
5575          Set_Has_Unknown_Discriminants (Derived_Type, False);
5576
5577          --  The following call initializes fields Has_Discriminants and
5578          --  Discriminant_Constraint, unless we are processing the completion
5579          --  of a private type declaration.
5580
5581          Check_Or_Process_Discriminants (N, Derived_Type);
5582
5583          --  For non-tagged types the constraint on the Parent_Type must be
5584          --  present and is used to rename the discriminants.
5585
5586          if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
5587             Error_Msg_N ("untagged parent must have discriminants", Indic);
5588
5589          elsif not Is_Tagged and then not Constraint_Present then
5590             Error_Msg_N
5591               ("discriminant constraint needed for derived untagged records",
5592                Indic);
5593
5594          --  Otherwise the parent subtype must be constrained unless we have a
5595          --  private extension.
5596
5597          elsif not Constraint_Present
5598            and then not Private_Extension
5599            and then not Is_Constrained (Parent_Type)
5600          then
5601             Error_Msg_N
5602               ("unconstrained type not allowed in this context", Indic);
5603
5604          elsif Constraint_Present then
5605             --  The following call sets the field Corresponding_Discriminant
5606             --  for the discriminants in the Derived_Type.
5607
5608             Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
5609
5610             --  For untagged types all new discriminants must rename
5611             --  discriminants in the parent. For private extensions new
5612             --  discriminants cannot rename old ones (implied by [7.3(13)]).
5613
5614             Discrim := First_Discriminant (Derived_Type);
5615             while Present (Discrim) loop
5616                if not Is_Tagged
5617                  and then not Present (Corresponding_Discriminant (Discrim))
5618                then
5619                   Error_Msg_N
5620                     ("new discriminants must constrain old ones", Discrim);
5621
5622                elsif Private_Extension
5623                  and then Present (Corresponding_Discriminant (Discrim))
5624                then
5625                   Error_Msg_N
5626                     ("only static constraints allowed for parent"
5627                      & " discriminants in the partial view", Indic);
5628                   exit;
5629                end if;
5630
5631                --  If a new discriminant is used in the constraint, then its
5632                --  subtype must be statically compatible with the parent
5633                --  discriminant's subtype (3.7(15)).
5634
5635                if Present (Corresponding_Discriminant (Discrim))
5636                  and then
5637                    not Subtypes_Statically_Compatible
5638                          (Etype (Discrim),
5639                           Etype (Corresponding_Discriminant (Discrim)))
5640                then
5641                   Error_Msg_N
5642                     ("subtype must be compatible with parent discriminant",
5643                      Discrim);
5644                end if;
5645
5646                Next_Discriminant (Discrim);
5647             end loop;
5648
5649             --  Check whether the constraints of the full view statically
5650             --  match those imposed by the parent subtype [7.3(13)].
5651
5652             if Present (Stored_Constraint (Derived_Type)) then
5653                declare
5654                   C1, C2 : Elmt_Id;
5655
5656                begin
5657                   C1 := First_Elmt (Discs);
5658                   C2 := First_Elmt (Stored_Constraint (Derived_Type));
5659                   while Present (C1) and then Present (C2) loop
5660                      if not
5661                        Fully_Conformant_Expressions (Node (C1), Node (C2))
5662                      then
5663                         Error_Msg_N (
5664                           "not conformant with previous declaration",
5665                              Node (C1));
5666                      end if;
5667
5668                      Next_Elmt (C1);
5669                      Next_Elmt (C2);
5670                   end loop;
5671                end;
5672             end if;
5673          end if;
5674
5675       --  STEP 2b: No new discriminants, inherit discriminants if any
5676
5677       else
5678          if Private_Extension then
5679             Set_Has_Unknown_Discriminants
5680               (Derived_Type,
5681                Has_Unknown_Discriminants (Parent_Type)
5682                  or else Unknown_Discriminants_Present (N));
5683
5684          --  The partial view of the parent may have unknown discriminants,
5685          --  but if the full view has discriminants and the parent type is
5686          --  in scope they must be inherited.
5687
5688          elsif Has_Unknown_Discriminants (Parent_Type)
5689            and then
5690             (not Has_Discriminants (Parent_Type)
5691               or else not In_Open_Scopes (Scope (Parent_Type)))
5692          then
5693             Set_Has_Unknown_Discriminants (Derived_Type);
5694          end if;
5695
5696          if not Has_Unknown_Discriminants (Derived_Type)
5697            and then not Has_Unknown_Discriminants (Parent_Base)
5698            and then Has_Discriminants (Parent_Type)
5699          then
5700             Inherit_Discrims := True;
5701             Set_Has_Discriminants
5702               (Derived_Type, True);
5703             Set_Discriminant_Constraint
5704               (Derived_Type, Discriminant_Constraint (Parent_Base));
5705          end if;
5706
5707          --  The following test is true for private types (remember
5708          --  transformation 5. is not applied to those) and in an error
5709          --  situation.
5710
5711          if Constraint_Present then
5712             Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
5713          end if;
5714
5715          --  For now mark a new derived type as constrained only if it has no
5716          --  discriminants. At the end of Build_Derived_Record_Type we properly
5717          --  set this flag in the case of private extensions. See comments in
5718          --  point 9. just before body of Build_Derived_Record_Type.
5719
5720          Set_Is_Constrained
5721            (Derived_Type,
5722             not (Inherit_Discrims
5723                    or else Has_Unknown_Discriminants (Derived_Type)));
5724       end if;
5725
5726       --  STEP 3: initialize fields of derived type
5727
5728       Set_Is_Tagged_Type    (Derived_Type, Is_Tagged);
5729       Set_Stored_Constraint (Derived_Type, No_Elist);
5730
5731       --  Ada 2005 (AI-251): Private type-declarations can implement interfaces
5732       --  but cannot be interfaces
5733
5734       if not Private_Extension
5735          and then Ekind (Derived_Type) /= E_Private_Type
5736          and then Ekind (Derived_Type) /= E_Limited_Private_Type
5737       then
5738          Set_Is_Interface (Derived_Type, Interface_Present (Type_Def));
5739          Set_Abstract_Interfaces (Derived_Type, No_Elist);
5740       end if;
5741
5742       --  Fields inherited from the Parent_Type
5743
5744       Set_Discard_Names
5745         (Derived_Type, Einfo.Discard_Names      (Parent_Type));
5746       Set_Has_Specified_Layout
5747         (Derived_Type, Has_Specified_Layout     (Parent_Type));
5748       Set_Is_Limited_Composite
5749         (Derived_Type, Is_Limited_Composite     (Parent_Type));
5750       Set_Is_Limited_Record
5751         (Derived_Type, Is_Limited_Record        (Parent_Type));
5752       Set_Is_Private_Composite
5753         (Derived_Type, Is_Private_Composite     (Parent_Type));
5754
5755       --  Fields inherited from the Parent_Base
5756
5757       Set_Has_Controlled_Component
5758         (Derived_Type, Has_Controlled_Component (Parent_Base));
5759       Set_Has_Non_Standard_Rep
5760         (Derived_Type, Has_Non_Standard_Rep     (Parent_Base));
5761       Set_Has_Primitive_Operations
5762         (Derived_Type, Has_Primitive_Operations (Parent_Base));
5763
5764       --  Direct controlled types do not inherit Finalize_Storage_Only flag
5765
5766       if not Is_Controlled  (Parent_Type) then
5767          Set_Finalize_Storage_Only
5768            (Derived_Type, Finalize_Storage_Only (Parent_Type));
5769       end if;
5770
5771       --  Set fields for private derived types
5772
5773       if Is_Private_Type (Derived_Type) then
5774          Set_Depends_On_Private (Derived_Type, True);
5775          Set_Private_Dependents (Derived_Type, New_Elmt_List);
5776
5777       --  Inherit fields from non private record types. If this is the
5778       --  completion of a derivation from a private type, the parent itself
5779       --  is private, and the attributes come from its full view, which must
5780       --  be present.
5781
5782       else
5783          if Is_Private_Type (Parent_Base)
5784            and then not Is_Record_Type (Parent_Base)
5785          then
5786             Set_Component_Alignment
5787               (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
5788             Set_C_Pass_By_Copy
5789               (Derived_Type, C_Pass_By_Copy      (Full_View (Parent_Base)));
5790          else
5791             Set_Component_Alignment
5792               (Derived_Type, Component_Alignment (Parent_Base));
5793
5794             Set_C_Pass_By_Copy
5795               (Derived_Type, C_Pass_By_Copy      (Parent_Base));
5796          end if;
5797       end if;
5798
5799       --  Set fields for tagged types
5800
5801       if Is_Tagged then
5802          Set_Primitive_Operations (Derived_Type, New_Elmt_List);
5803
5804          --  All tagged types defined in Ada.Finalization are controlled
5805
5806          if Chars (Scope (Derived_Type)) = Name_Finalization
5807            and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
5808            and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
5809          then
5810             Set_Is_Controlled (Derived_Type);
5811          else
5812             Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
5813          end if;
5814
5815          Make_Class_Wide_Type (Derived_Type);
5816          Set_Is_Abstract      (Derived_Type, Abstract_Present (Type_Def));
5817
5818          if Has_Discriminants (Derived_Type)
5819            and then Constraint_Present
5820          then
5821             Set_Stored_Constraint
5822               (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
5823          end if;
5824
5825          --  Ada 2005 (AI-251): Look for the partial view of tagged types
5826          --  declared in the private part. This will be used 1) to check that
5827          --  the set of interfaces in both views is equal, and 2) to complete
5828          --  the derivation of subprograms covering interfaces.
5829
5830          Tagged_Partial_View := Empty;
5831
5832          if Has_Private_Declaration (Derived_Type) then
5833             Tagged_Partial_View := Next_Entity (Derived_Type);
5834             loop
5835                exit when Has_Private_Declaration (Tagged_Partial_View)
5836                  and then Full_View (Tagged_Partial_View) = Derived_Type;
5837
5838                Next_Entity (Tagged_Partial_View);
5839             end loop;
5840          end if;
5841
5842          --  Ada 2005 (AI-251): Collect the whole list of implemented
5843          --  interfaces.
5844
5845          if Ada_Version >= Ada_05 then
5846             Set_Abstract_Interfaces (Derived_Type, New_Elmt_List);
5847
5848             if Nkind (N) = N_Private_Extension_Declaration then
5849                Collect_Interfaces (N, Derived_Type);
5850             else
5851                Collect_Interfaces (Type_Definition (N), Derived_Type);
5852             end if;
5853
5854             --  Check that the full view and the partial view agree
5855             --  in the set of implemented interfaces
5856
5857             if Has_Private_Declaration (Derived_Type)
5858               and then Present (Abstract_Interfaces (Derived_Type))
5859               and then not Is_Empty_Elmt_List
5860                              (Abstract_Interfaces (Derived_Type))
5861             then
5862                declare
5863                   N_Partial : constant Node_Id := Parent (Tagged_Partial_View);
5864                   N_Full    : constant Node_Id := Parent (Derived_Type);
5865
5866                   Iface_Partial      : Entity_Id;
5867                   Iface_Full         : Entity_Id;
5868                   Num_Ifaces_Partial : Natural := 0;
5869                   Num_Ifaces_Full    : Natural := 0;
5870                   Same_Interfaces    : Boolean := True;
5871
5872                begin
5873                   --  Count the interfaces implemented by the partial view
5874
5875                   if not Is_Empty_List (Interface_List (N_Partial)) then
5876                      Iface_Partial := First (Interface_List (N_Partial));
5877
5878                      while Present (Iface_Partial) loop
5879                         Num_Ifaces_Partial := Num_Ifaces_Partial + 1;
5880                         Next (Iface_Partial);
5881                      end loop;
5882                   end if;
5883
5884                   --  Take into account the case in which the partial
5885                   --  view is a directly derived from an interface
5886
5887                   if Is_Interface (Etype
5888                                    (Defining_Identifier (N_Partial)))
5889                   then
5890                      Num_Ifaces_Partial := Num_Ifaces_Partial + 1;
5891                   end if;
5892
5893                   --  Count the interfaces implemented by the full view
5894
5895                   if not Is_Empty_List (Interface_List
5896                                         (Type_Definition (N_Full)))
5897                   then
5898                      Iface_Full := First (Interface_List
5899                                           (Type_Definition (N_Full)));
5900
5901                      while Present (Iface_Full) loop
5902                         Num_Ifaces_Full := Num_Ifaces_Full + 1;
5903                         Next (Iface_Full);
5904                      end loop;
5905                   end if;
5906
5907                   --  Take into account the case in which the full
5908                   --  view is a directly derived from an interface
5909
5910                   if Is_Interface (Etype
5911                                    (Defining_Identifier (N_Full)))
5912                   then
5913                      Num_Ifaces_Full := Num_Ifaces_Full + 1;
5914                   end if;
5915
5916                   if Num_Ifaces_Full > 0
5917                     and then Num_Ifaces_Full = Num_Ifaces_Partial
5918                   then
5919
5920                      --  Check that the full-view and the private-view have
5921                      --  the same list of interfaces
5922
5923                      Iface_Full := First (Interface_List
5924                                            (Type_Definition (N_Full)));
5925
5926                      while Present (Iface_Full) loop
5927                         Iface_Partial := First (Interface_List (N_Partial));
5928
5929                         while Present (Iface_Partial)
5930                           and then Etype (Iface_Partial) /= Etype (Iface_Full)
5931                         loop
5932                            Next (Iface_Partial);
5933                         end loop;
5934
5935                         --  If not found we check if the partial view is a
5936                         --  direct derivation of the interface.
5937
5938                         if not Present (Iface_Partial)
5939                              and then
5940                            Etype (Tagged_Partial_View) /= Etype (Iface_Full)
5941                         then
5942                            Same_Interfaces := False;
5943                            exit;
5944                         end if;
5945
5946                         Next (Iface_Full);
5947                      end loop;
5948                   end if;
5949
5950                   if Num_Ifaces_Partial /= Num_Ifaces_Full
5951                     or else not Same_Interfaces
5952                   then
5953                      Error_Msg_N
5954                        ("(Ada 2005) full declaration and private declaration"
5955                         & " must have the same list of interfaces",
5956                         Derived_Type);
5957                   end if;
5958                end;
5959             end if;
5960          end if;
5961
5962       else
5963          Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
5964          Set_Has_Non_Standard_Rep
5965                        (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
5966       end if;
5967
5968       --  STEP 4: Inherit components from the parent base and constrain them.
5969       --          Apply the second transformation described in point 6. above.
5970
5971       if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
5972         or else not Has_Discriminants (Parent_Type)
5973         or else not Is_Constrained (Parent_Type)
5974       then
5975          Constrs := Discs;
5976       else
5977          Constrs := Discriminant_Constraint (Parent_Type);
5978       end if;
5979
5980       Assoc_List := Inherit_Components (N,
5981         Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
5982
5983       --  STEP 5a: Copy the parent record declaration for untagged types
5984
5985       if not Is_Tagged then
5986
5987          --  Discriminant_Constraint (Derived_Type) has been properly
5988          --  constructed. Save it and temporarily set it to Empty because we
5989          --  do not want the call to New_Copy_Tree below to mess this list.
5990
5991          if Has_Discriminants (Derived_Type) then
5992             Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
5993             Set_Discriminant_Constraint (Derived_Type, No_Elist);
5994          else
5995             Save_Discr_Constr := No_Elist;
5996          end if;
5997
5998          --  Save the Etype field of Derived_Type. It is correctly set now,
5999          --  but the call to New_Copy tree may remap it to point to itself,
6000          --  which is not what we want. Ditto for the Next_Entity field.
6001
6002          Save_Etype       := Etype (Derived_Type);
6003          Save_Next_Entity := Next_Entity (Derived_Type);
6004
6005          --  Assoc_List maps all stored discriminants in the Parent_Base to
6006          --  stored discriminants in the Derived_Type. It is fundamental that
6007          --  no types or itypes with discriminants other than the stored
6008          --  discriminants appear in the entities declared inside
6009          --  Derived_Type, since the back end cannot deal with it.
6010
6011          New_Decl :=
6012            New_Copy_Tree
6013              (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
6014
6015          --  Restore the fields saved prior to the New_Copy_Tree call
6016          --  and compute the stored constraint.
6017
6018          Set_Etype       (Derived_Type, Save_Etype);
6019          Set_Next_Entity (Derived_Type, Save_Next_Entity);
6020
6021          if Has_Discriminants (Derived_Type) then
6022             Set_Discriminant_Constraint
6023               (Derived_Type, Save_Discr_Constr);
6024             Set_Stored_Constraint
6025               (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
6026             Replace_Components (Derived_Type, New_Decl);
6027          end if;
6028
6029          --  Insert the new derived type declaration
6030
6031          Rewrite (N, New_Decl);
6032
6033       --  STEP 5b: Complete the processing for record extensions in generics
6034
6035       --  There is no completion for record extensions declared in the
6036       --  parameter part of a generic, so we need to complete processing for
6037       --  these generic record extensions here. The Record_Type_Definition call
6038       --  will change the Ekind of the components from E_Void to E_Component.
6039
6040       elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
6041          Record_Type_Definition (Empty, Derived_Type);
6042
6043       --  STEP 5c: Process the record extension for non private tagged types
6044
6045       elsif not Private_Extension then
6046
6047          --  Add the _parent field in the derived type
6048
6049          Expand_Record_Extension (Derived_Type, Type_Def);
6050
6051          --  Ada 2005 (AI-251): Addition of the Tag corresponding to all the
6052          --  implemented interfaces if we are in expansion mode
6053
6054          if Expander_Active then
6055             Add_Interface_Tag_Components (N, Derived_Type);
6056          end if;
6057
6058          --  Analyze the record extension
6059
6060          Record_Type_Definition
6061            (Record_Extension_Part (Type_Def), Derived_Type);
6062       end if;
6063
6064       End_Scope;
6065
6066       if Etype (Derived_Type) = Any_Type then
6067          return;
6068       end if;
6069
6070       --  Set delayed freeze and then derive subprograms, we need to do
6071       --  this in this order so that derived subprograms inherit the
6072       --  derived freeze if necessary.
6073
6074       Set_Has_Delayed_Freeze (Derived_Type);
6075
6076       if Derive_Subps then
6077          Derive_Subprograms (Parent_Type, Derived_Type);
6078
6079          --  Ada 2005 (AI-251): Check if this tagged type implements abstract
6080          --  interfaces
6081
6082          Has_Interfaces := False;
6083
6084          if Is_Tagged_Type (Derived_Type) then
6085             declare
6086                E : Entity_Id;
6087
6088             begin
6089                E := Derived_Type;
6090                loop
6091                   if Is_Interface (E)
6092                     or else (Present (Abstract_Interfaces (E))
6093                                and then
6094                              not Is_Empty_Elmt_List (Abstract_Interfaces (E)))
6095                   then
6096                      Has_Interfaces := True;
6097                      exit;
6098                   end if;
6099
6100                   exit when Etype (E) = E
6101
6102                      --  Protect the frontend against wrong source
6103
6104                     or else Etype (E) = Derived_Type;
6105
6106                   E := Etype (E);
6107                end loop;
6108             end;
6109          end if;
6110
6111          --  Ada 2005 (AI-251): Keep separate the management of tagged types
6112          --  implementing interfaces
6113
6114          if Is_Tagged_Type (Derived_Type)
6115            and then Has_Interfaces
6116          then
6117             --  Complete the decoration of private tagged types
6118
6119             if Present (Tagged_Partial_View) then
6120                Complete_Subprograms_Derivation
6121                  (Partial_View => Tagged_Partial_View,
6122                   Derived_Type => Derived_Type);
6123             end if;
6124
6125             --  Ada 2005 (AI-251): Derive the interface subprograms of all the
6126             --  implemented interfaces and check if some of the subprograms
6127             --  inherited from the ancestor cover some interface subprogram.
6128
6129             if not Present (Tagged_Partial_View) then
6130                declare
6131                   Subp_Elmt         : Elmt_Id := First_Elmt
6132                                                    (Primitive_Operations
6133                                                      (Derived_Type));
6134                   Iface_Subp_Elmt   : Elmt_Id;
6135                   Subp              : Entity_Id;
6136                   Iface_Subp        : Entity_Id;
6137                   Is_Interface_Subp : Boolean;
6138
6139                begin
6140                   --  Ada 2005 (AI-251): Remember the entity corresponding to
6141                   --  the last inherited primitive operation. This is required
6142                   --  to check if some of the inherited subprograms covers some
6143                   --  of the new interfaces.
6144
6145                   Last_Inherited_Prim_Op := No_Elmt;
6146
6147                   while Present (Subp_Elmt) loop
6148                      Last_Inherited_Prim_Op := Subp_Elmt;
6149                      Next_Elmt (Subp_Elmt);
6150                   end loop;
6151
6152                   --  Ada 2005 (AI-251): Derive subprograms in abstract
6153                   --  interfaces
6154
6155                   Derive_Interface_Subprograms (Derived_Type);
6156
6157                   --  Ada 2005 (AI-251): Check if some of the inherited
6158                   --  subprograms cover some of the new interfaces.
6159
6160                   if Present (Last_Inherited_Prim_Op) then
6161                      Iface_Subp_Elmt := Next_Elmt (Last_Inherited_Prim_Op);
6162                      while Present (Iface_Subp_Elmt) loop
6163                         Subp_Elmt := First_Elmt (Primitive_Operations
6164                                                   (Derived_Type));
6165                         while Subp_Elmt /= Last_Inherited_Prim_Op loop
6166                            Subp       := Node (Subp_Elmt);
6167                            Iface_Subp := Node (Iface_Subp_Elmt);
6168
6169                            Is_Interface_Subp :=
6170                              Present (Alias (Subp))
6171                                and then Present (DTC_Entity (Alias (Subp)))
6172                                and then Is_Interface (Scope
6173                                                       (DTC_Entity
6174                                                        (Alias (Subp))));
6175
6176                            if Chars (Subp) = Chars (Iface_Subp)
6177                              and then not Is_Interface_Subp
6178                              and then not Is_Abstract (Subp)
6179                              and then Type_Conformant (Iface_Subp, Subp)
6180                            then
6181                               Check_Dispatching_Operation
6182                                 (Subp     => Subp,
6183                                  Old_Subp => Iface_Subp);
6184
6185                               --  Traverse the list of aliased subprograms
6186
6187                               declare
6188                                  E : Entity_Id := Alias (Subp);
6189                               begin
6190                                  while Present (Alias (E)) loop
6191                                     E := Alias (E);
6192                                  end loop;
6193                                  Set_Alias (Subp, E);
6194                               end;
6195
6196                               Set_Has_Delayed_Freeze (Subp);
6197                               exit;
6198                            end if;
6199
6200                            Next_Elmt (Subp_Elmt);
6201                         end loop;
6202
6203                         Next_Elmt (Iface_Subp_Elmt);
6204                      end loop;
6205                   end if;
6206                end;
6207             end if;
6208          end if;
6209       end if;
6210
6211       --  If we have a private extension which defines a constrained derived
6212       --  type mark as constrained here after we have derived subprograms. See
6213       --  comment on point 9. just above the body of Build_Derived_Record_Type.
6214
6215       if Private_Extension and then Inherit_Discrims then
6216          if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
6217             Set_Is_Constrained          (Derived_Type, True);
6218             Set_Discriminant_Constraint (Derived_Type, Discs);
6219
6220          elsif Is_Constrained (Parent_Type) then
6221             Set_Is_Constrained
6222               (Derived_Type, True);
6223             Set_Discriminant_Constraint
6224               (Derived_Type, Discriminant_Constraint (Parent_Type));
6225          end if;
6226       end if;
6227
6228       --  Update the class_wide type, which shares the now-completed
6229       --  entity list with its specific type.
6230
6231       if Is_Tagged then
6232          Set_First_Entity
6233            (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
6234          Set_Last_Entity
6235            (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
6236       end if;
6237
6238    end Build_Derived_Record_Type;
6239
6240    ------------------------
6241    -- Build_Derived_Type --
6242    ------------------------
6243
6244    procedure Build_Derived_Type
6245      (N             : Node_Id;
6246       Parent_Type   : Entity_Id;
6247       Derived_Type  : Entity_Id;
6248       Is_Completion : Boolean;
6249       Derive_Subps  : Boolean := True)
6250    is
6251       Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6252
6253    begin
6254       --  Set common attributes
6255
6256       Set_Scope          (Derived_Type, Current_Scope);
6257
6258       Set_Ekind          (Derived_Type, Ekind     (Parent_Base));
6259       Set_Etype          (Derived_Type,            Parent_Base);
6260       Set_Has_Task       (Derived_Type, Has_Task  (Parent_Base));
6261
6262       Set_Size_Info      (Derived_Type,                 Parent_Type);
6263       Set_RM_Size        (Derived_Type, RM_Size        (Parent_Type));
6264       Set_Convention     (Derived_Type, Convention     (Parent_Type));
6265       Set_Is_Controlled  (Derived_Type, Is_Controlled  (Parent_Type));
6266
6267       --  The derived type inherits the representation clauses of the parent.
6268       --  However, for a private type that is completed by a derivation, there
6269       --  may be operation attributes that have been specified already (stream
6270       --  attributes and External_Tag) and those must be provided. Finally,
6271       --  if the partial view is a private extension, the representation items
6272       --  of the parent have been inherited already, and should not be chained
6273       --  twice to the derived type.
6274
6275       if Is_Tagged_Type (Parent_Type)
6276         and then Present (First_Rep_Item (Derived_Type))
6277       then
6278          --  The existing items are either operational items or items inherited
6279          --  from a private extension declaration.
6280
6281          declare
6282             Rep   : Node_Id := First_Rep_Item (Derived_Type);
6283             Found : Boolean := False;
6284
6285          begin
6286             while Present (Rep) loop
6287                if Rep = First_Rep_Item (Parent_Type) then
6288                   Found := True;
6289                   exit;
6290                else
6291                   Rep := Next_Rep_Item (Rep);
6292                end if;
6293             end loop;
6294
6295             if not Found then
6296                Set_Next_Rep_Item
6297                  (First_Rep_Item (Derived_Type), First_Rep_Item (Parent_Type));
6298             end if;
6299          end;
6300
6301       else
6302          Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
6303       end if;
6304
6305       case Ekind (Parent_Type) is
6306          when Numeric_Kind =>
6307             Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
6308
6309          when Array_Kind =>
6310             Build_Derived_Array_Type (N, Parent_Type,  Derived_Type);
6311
6312          when E_Record_Type
6313             | E_Record_Subtype
6314             | Class_Wide_Kind  =>
6315             Build_Derived_Record_Type
6316               (N, Parent_Type, Derived_Type, Derive_Subps);
6317             return;
6318
6319          when Enumeration_Kind =>
6320             Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
6321
6322          when Access_Kind =>
6323             Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
6324
6325          when Incomplete_Or_Private_Kind =>
6326             Build_Derived_Private_Type
6327               (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
6328
6329             --  For discriminated types, the derivation includes deriving
6330             --  primitive operations. For others it is done below.
6331
6332             if Is_Tagged_Type (Parent_Type)
6333               or else Has_Discriminants (Parent_Type)
6334               or else (Present (Full_View (Parent_Type))
6335                         and then Has_Discriminants (Full_View (Parent_Type)))
6336             then
6337                return;
6338             end if;
6339
6340          when Concurrent_Kind =>
6341             Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
6342
6343          when others =>
6344             raise Program_Error;
6345       end case;
6346
6347       if Etype (Derived_Type) = Any_Type then
6348          return;
6349       end if;
6350
6351       --  Set delayed freeze and then derive subprograms, we need to do this
6352       --  in this order so that derived subprograms inherit the derived freeze
6353       --  if necessary.
6354
6355       Set_Has_Delayed_Freeze (Derived_Type);
6356       if Derive_Subps then
6357          Derive_Subprograms (Parent_Type, Derived_Type);
6358       end if;
6359
6360       Set_Has_Primitive_Operations
6361         (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
6362    end Build_Derived_Type;
6363
6364    -----------------------
6365    -- Build_Discriminal --
6366    -----------------------
6367
6368    procedure Build_Discriminal (Discrim : Entity_Id) is
6369       D_Minal : Entity_Id;
6370       CR_Disc : Entity_Id;
6371
6372    begin
6373       --  A discriminal has the same name as the discriminant
6374
6375       D_Minal :=
6376         Make_Defining_Identifier (Sloc (Discrim),
6377           Chars => Chars (Discrim));
6378
6379       Set_Ekind     (D_Minal, E_In_Parameter);
6380       Set_Mechanism (D_Minal, Default_Mechanism);
6381       Set_Etype     (D_Minal, Etype (Discrim));
6382
6383       Set_Discriminal (Discrim, D_Minal);
6384       Set_Discriminal_Link (D_Minal, Discrim);
6385
6386       --  For task types, build at once the discriminants of the corresponding
6387       --  record, which are needed if discriminants are used in entry defaults
6388       --  and in family bounds.
6389
6390       if Is_Concurrent_Type (Current_Scope)
6391         or else Is_Limited_Type (Current_Scope)
6392       then
6393          CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
6394
6395          Set_Ekind     (CR_Disc, E_In_Parameter);
6396          Set_Mechanism (CR_Disc, Default_Mechanism);
6397          Set_Etype     (CR_Disc, Etype (Discrim));
6398          Set_CR_Discriminant (Discrim, CR_Disc);
6399       end if;
6400    end Build_Discriminal;
6401
6402    ------------------------------------
6403    -- Build_Discriminant_Constraints --
6404    ------------------------------------
6405
6406    function Build_Discriminant_Constraints
6407      (T           : Entity_Id;
6408       Def         : Node_Id;
6409       Derived_Def : Boolean := False) return Elist_Id
6410    is
6411       C        : constant Node_Id := Constraint (Def);
6412       Nb_Discr : constant Nat     := Number_Discriminants (T);
6413
6414       Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
6415       --  Saves the expression corresponding to a given discriminant in T
6416
6417       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
6418       --  Return the Position number within array Discr_Expr of a discriminant
6419       --  D within the discriminant list of the discriminated type T.
6420
6421       ------------------
6422       -- Pos_Of_Discr --
6423       ------------------
6424
6425       function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
6426          Disc : Entity_Id;
6427
6428       begin
6429          Disc := First_Discriminant (T);
6430          for J in Discr_Expr'Range loop
6431             if Disc = D then
6432                return J;
6433             end if;
6434
6435             Next_Discriminant (Disc);
6436          end loop;
6437
6438          --  Note: Since this function is called on discriminants that are
6439          --  known to belong to the discriminated type, falling through the
6440          --  loop with no match signals an internal compiler error.
6441
6442          raise Program_Error;
6443       end Pos_Of_Discr;
6444
6445       --  Declarations local to Build_Discriminant_Constraints
6446
6447       Discr : Entity_Id;
6448       E     : Entity_Id;
6449       Elist : constant Elist_Id := New_Elmt_List;
6450
6451       Constr   : Node_Id;
6452       Expr     : Node_Id;
6453       Id       : Node_Id;
6454       Position : Nat;
6455       Found    : Boolean;
6456
6457       Discrim_Present : Boolean := False;
6458
6459    --  Start of processing for Build_Discriminant_Constraints
6460
6461    begin
6462       --  The following loop will process positional associations only.
6463       --  For a positional association, the (single) discriminant is
6464       --  implicitly specified by position, in textual order (RM 3.7.2).
6465
6466       Discr  := First_Discriminant (T);
6467       Constr := First (Constraints (C));
6468
6469       for D in Discr_Expr'Range loop
6470          exit when Nkind (Constr) = N_Discriminant_Association;
6471
6472          if No (Constr) then
6473             Error_Msg_N ("too few discriminants given in constraint", C);
6474             return New_Elmt_List;
6475
6476          elsif Nkind (Constr) = N_Range
6477            or else (Nkind (Constr) = N_Attribute_Reference
6478                      and then
6479                     Attribute_Name (Constr) = Name_Range)
6480          then
6481             Error_Msg_N
6482               ("a range is not a valid discriminant constraint", Constr);
6483             Discr_Expr (D) := Error;
6484
6485          else
6486             Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
6487             Discr_Expr (D) := Constr;
6488          end if;
6489
6490          Next_Discriminant (Discr);
6491          Next (Constr);
6492       end loop;
6493
6494       if No (Discr) and then Present (Constr) then
6495          Error_Msg_N ("too many discriminants given in constraint", Constr);
6496          return New_Elmt_List;
6497       end if;
6498
6499       --  Named associations can be given in any order, but if both positional
6500       --  and named associations are used in the same discriminant constraint,
6501       --  then positional associations must occur first, at their normal
6502       --  position. Hence once a named association is used, the rest of the
6503       --  discriminant constraint must use only named associations.
6504
6505       while Present (Constr) loop
6506
6507          --  Positional association forbidden after a named association
6508
6509          if Nkind (Constr) /= N_Discriminant_Association then
6510             Error_Msg_N ("positional association follows named one", Constr);
6511             return New_Elmt_List;
6512
6513          --  Otherwise it is a named association
6514
6515          else
6516             --  E records the type of the discriminants in the named
6517             --  association. All the discriminants specified in the same name
6518             --  association must have the same type.
6519
6520             E := Empty;
6521
6522             --  Search the list of discriminants in T to see if the simple name
6523             --  given in the constraint matches any of them.
6524
6525             Id := First (Selector_Names (Constr));
6526             while Present (Id) loop
6527                Found := False;
6528
6529                --  If Original_Discriminant is present, we are processing a
6530                --  generic instantiation and this is an instance node. We need
6531                --  to find the name of the corresponding discriminant in the
6532                --  actual record type T and not the name of the discriminant in
6533                --  the generic formal. Example:
6534                --
6535                --    generic
6536                --       type G (D : int) is private;
6537                --    package P is
6538                --       subtype W is G (D => 1);
6539                --    end package;
6540                --    type Rec (X : int) is record ... end record;
6541                --    package Q is new P (G => Rec);
6542                --
6543                --  At the point of the instantiation, formal type G is Rec
6544                --  and therefore when reanalyzing "subtype W is G (D => 1);"
6545                --  which really looks like "subtype W is Rec (D => 1);" at
6546                --  the point of instantiation, we want to find the discriminant
6547                --  that corresponds to D in Rec, ie X.
6548
6549                if Present (Original_Discriminant (Id)) then
6550                   Discr := Find_Corresponding_Discriminant (Id, T);
6551                   Found := True;
6552
6553                else
6554                   Discr := First_Discriminant (T);
6555                   while Present (Discr) loop
6556                      if Chars (Discr) = Chars (Id) then
6557                         Found := True;
6558                         exit;
6559                      end if;
6560
6561                      Next_Discriminant (Discr);
6562                   end loop;
6563
6564                   if not Found then
6565                      Error_Msg_N ("& does not match any discriminant", Id);
6566                      return New_Elmt_List;
6567
6568                   --  The following is only useful for the benefit of generic
6569                   --  instances but it does not interfere with other
6570                   --  processing for the non-generic case so we do it in all
6571                   --  cases (for generics this statement is executed when
6572                   --  processing the generic definition, see comment at the
6573                   --  beginning of this if statement).
6574
6575                   else
6576                      Set_Original_Discriminant (Id, Discr);
6577                   end if;
6578                end if;
6579
6580                Position := Pos_Of_Discr (T, Discr);
6581
6582                if Present (Discr_Expr (Position)) then
6583                   Error_Msg_N ("duplicate constraint for discriminant&", Id);
6584
6585                else
6586                   --  Each discriminant specified in the same named association
6587                   --  must be associated with a separate copy of the
6588                   --  corresponding expression.
6589
6590                   if Present (Next (Id)) then
6591                      Expr := New_Copy_Tree (Expression (Constr));
6592                      Set_Parent (Expr, Parent (Expression (Constr)));
6593                   else
6594                      Expr := Expression (Constr);
6595                   end if;
6596
6597                   Discr_Expr (Position) := Expr;
6598                   Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
6599                end if;
6600
6601                --  A discriminant association with more than one discriminant
6602                --  name is only allowed if the named discriminants are all of
6603                --  the same type (RM 3.7.1(8)).
6604
6605                if E = Empty then
6606                   E := Base_Type (Etype (Discr));
6607
6608                elsif Base_Type (Etype (Discr)) /= E then
6609                   Error_Msg_N
6610                     ("all discriminants in an association " &
6611                      "must have the same type", Id);
6612                end if;
6613
6614                Next (Id);
6615             end loop;
6616          end if;
6617
6618          Next (Constr);
6619       end loop;
6620
6621       --  A discriminant constraint must provide exactly one value for each
6622       --  discriminant of the type (RM 3.7.1(8)).
6623
6624       for J in Discr_Expr'Range loop
6625          if No (Discr_Expr (J)) then
6626             Error_Msg_N ("too few discriminants given in constraint", C);
6627             return New_Elmt_List;
6628          end if;
6629       end loop;
6630
6631       --  Determine if there are discriminant expressions in the constraint
6632
6633       for J in Discr_Expr'Range loop
6634          if Denotes_Discriminant (Discr_Expr (J), Check_Protected => True) then
6635             Discrim_Present := True;
6636          end if;
6637       end loop;
6638
6639       --  Build an element list consisting of the expressions given in the
6640       --  discriminant constraint and apply the appropriate checks. The list
6641       --  is constructed after resolving any named discriminant associations
6642       --  and therefore the expressions appear in the textual order of the
6643       --  discriminants.
6644
6645       Discr := First_Discriminant (T);
6646       for J in Discr_Expr'Range loop
6647          if Discr_Expr (J) /= Error then
6648
6649             Append_Elmt (Discr_Expr (J), Elist);
6650
6651             --  If any of the discriminant constraints is given by a
6652             --  discriminant and we are in a derived type declaration we
6653             --  have a discriminant renaming. Establish link between new
6654             --  and old discriminant.
6655
6656             if Denotes_Discriminant (Discr_Expr (J)) then
6657                if Derived_Def then
6658                   Set_Corresponding_Discriminant
6659                     (Entity (Discr_Expr (J)), Discr);
6660                end if;
6661
6662             --  Force the evaluation of non-discriminant expressions.
6663             --  If we have found a discriminant in the constraint 3.4(26)
6664             --  and 3.8(18) demand that no range checks are performed are
6665             --  after evaluation. If the constraint is for a component
6666             --  definition that has a per-object constraint, expressions are
6667             --  evaluated but not checked either. In all other cases perform
6668             --  a range check.
6669
6670             else
6671                if Discrim_Present then
6672                   null;
6673
6674                elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
6675                  and then
6676                    Has_Per_Object_Constraint
6677                      (Defining_Identifier (Parent (Parent (Def))))
6678                then
6679                   null;
6680
6681                elsif Is_Access_Type (Etype (Discr)) then
6682                   Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
6683
6684                else
6685                   Apply_Range_Check (Discr_Expr (J), Etype (Discr));
6686                end if;
6687
6688                Force_Evaluation (Discr_Expr (J));
6689             end if;
6690
6691          --  Check that the designated type of an access discriminant's
6692          --  expression is not a class-wide type unless the discriminant's
6693          --  designated type is also class-wide.
6694
6695             if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
6696               and then not Is_Class_Wide_Type
6697                          (Designated_Type (Etype (Discr)))
6698               and then Etype (Discr_Expr (J)) /= Any_Type
6699               and then Is_Class_Wide_Type
6700                          (Designated_Type (Etype (Discr_Expr (J))))
6701             then
6702                Wrong_Type (Discr_Expr (J), Etype (Discr));
6703             end if;
6704          end if;
6705
6706          Next_Discriminant (Discr);
6707       end loop;
6708
6709       return Elist;
6710    end Build_Discriminant_Constraints;
6711
6712    ---------------------------------
6713    -- Build_Discriminated_Subtype --
6714    ---------------------------------
6715
6716    procedure Build_Discriminated_Subtype
6717      (T           : Entity_Id;
6718       Def_Id      : Entity_Id;
6719       Elist       : Elist_Id;
6720       Related_Nod : Node_Id;
6721       For_Access  : Boolean := False)
6722    is
6723       Has_Discrs  : constant Boolean := Has_Discriminants (T);
6724       Constrained : constant Boolean
6725                       := (Has_Discrs
6726                             and then not Is_Empty_Elmt_List (Elist)
6727                             and then not Is_Class_Wide_Type (T))
6728                            or else Is_Constrained (T);
6729
6730    begin
6731       if Ekind (T) = E_Record_Type then
6732          if For_Access then
6733             Set_Ekind (Def_Id, E_Private_Subtype);
6734             Set_Is_For_Access_Subtype (Def_Id, True);
6735          else
6736             Set_Ekind (Def_Id, E_Record_Subtype);
6737          end if;
6738
6739       elsif Ekind (T) = E_Task_Type then
6740          Set_Ekind (Def_Id, E_Task_Subtype);
6741
6742       elsif Ekind (T) = E_Protected_Type then
6743          Set_Ekind (Def_Id, E_Protected_Subtype);
6744
6745       elsif Is_Private_Type (T) then
6746          Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
6747
6748       elsif Is_Class_Wide_Type (T) then
6749          Set_Ekind (Def_Id, E_Class_Wide_Subtype);
6750
6751       else
6752          --  Incomplete type.  attach subtype to list of dependents, to be
6753          --  completed with full view of parent type,  unless is it the
6754          --  designated subtype of a record component within an init_proc.
6755          --  This last case arises for a component of an access type whose
6756          --  designated type is incomplete (e.g. a Taft Amendment type).
6757          --  The designated subtype is within an inner scope, and needs no
6758          --  elaboration, because only the access type is needed in the
6759          --  initialization procedure.
6760
6761          Set_Ekind (Def_Id, Ekind (T));
6762
6763          if For_Access and then Within_Init_Proc then
6764             null;
6765          else
6766             Append_Elmt (Def_Id, Private_Dependents (T));
6767          end if;
6768       end if;
6769
6770       Set_Etype             (Def_Id, T);
6771       Init_Size_Align       (Def_Id);
6772       Set_Has_Discriminants (Def_Id, Has_Discrs);
6773       Set_Is_Constrained    (Def_Id, Constrained);
6774
6775       Set_First_Entity      (Def_Id, First_Entity   (T));
6776       Set_Last_Entity       (Def_Id, Last_Entity    (T));
6777       Set_First_Rep_Item    (Def_Id, First_Rep_Item (T));
6778
6779       if Is_Tagged_Type (T) then
6780          Set_Is_Tagged_Type  (Def_Id);
6781          Make_Class_Wide_Type (Def_Id);
6782       end if;
6783
6784       Set_Stored_Constraint (Def_Id, No_Elist);
6785
6786       if Has_Discrs then
6787          Set_Discriminant_Constraint (Def_Id, Elist);
6788          Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
6789       end if;
6790
6791       if Is_Tagged_Type (T) then
6792          Set_Primitive_Operations (Def_Id, Primitive_Operations (T));
6793          Set_Is_Abstract (Def_Id, Is_Abstract (T));
6794       end if;
6795
6796       --  Subtypes introduced by component declarations do not need to be
6797       --  marked as delayed, and do not get freeze nodes, because the semantics
6798       --  verifies that the parents of the subtypes are frozen before the
6799       --  enclosing record is frozen.
6800
6801       if not Is_Type (Scope (Def_Id)) then
6802          Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
6803
6804          if Is_Private_Type (T)
6805            and then Present (Full_View (T))
6806          then
6807             Conditional_Delay (Def_Id, Full_View (T));
6808          else
6809             Conditional_Delay (Def_Id, T);
6810          end if;
6811       end if;
6812
6813       if Is_Record_Type (T) then
6814          Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
6815
6816          if Has_Discrs
6817             and then not Is_Empty_Elmt_List (Elist)
6818             and then not For_Access
6819          then
6820             Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
6821          elsif not For_Access then
6822             Set_Cloned_Subtype (Def_Id, T);
6823          end if;
6824       end if;
6825
6826    end Build_Discriminated_Subtype;
6827
6828    ------------------------
6829    -- Build_Scalar_Bound --
6830    ------------------------
6831
6832    function Build_Scalar_Bound
6833      (Bound : Node_Id;
6834       Par_T : Entity_Id;
6835       Der_T : Entity_Id) return Node_Id
6836    is
6837       New_Bound : Entity_Id;
6838
6839    begin
6840       --  Note: not clear why this is needed, how can the original bound
6841       --  be unanalyzed at this point? and if it is, what business do we
6842       --  have messing around with it? and why is the base type of the
6843       --  parent type the right type for the resolution. It probably is
6844       --  not! It is OK for the new bound we are creating, but not for
6845       --  the old one??? Still if it never happens, no problem!
6846
6847       Analyze_And_Resolve (Bound, Base_Type (Par_T));
6848
6849       if Nkind (Bound) = N_Integer_Literal
6850         or else Nkind (Bound) = N_Real_Literal
6851       then
6852          New_Bound := New_Copy (Bound);
6853          Set_Etype (New_Bound, Der_T);
6854          Set_Analyzed (New_Bound);
6855
6856       elsif Is_Entity_Name (Bound) then
6857          New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
6858
6859       --  The following is almost certainly wrong. What business do we have
6860       --  relocating a node (Bound) that is presumably still attached to
6861       --  the tree elsewhere???
6862
6863       else
6864          New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
6865       end if;
6866
6867       Set_Etype (New_Bound, Der_T);
6868       return New_Bound;
6869    end Build_Scalar_Bound;
6870
6871    --------------------------------
6872    -- Build_Underlying_Full_View --
6873    --------------------------------
6874
6875    procedure Build_Underlying_Full_View
6876      (N   : Node_Id;
6877       Typ : Entity_Id;
6878       Par : Entity_Id)
6879    is
6880       Loc  : constant Source_Ptr := Sloc (N);
6881       Subt : constant Entity_Id :=
6882                Make_Defining_Identifier
6883                  (Loc, New_External_Name (Chars (Typ), 'S'));
6884
6885       Constr : Node_Id;
6886       Indic  : Node_Id;
6887       C      : Node_Id;
6888       Id     : Node_Id;
6889
6890       procedure Set_Discriminant_Name (Id : Node_Id);
6891       --  If the derived type has discriminants, they may rename discriminants
6892       --  of the parent. When building the full view of the parent, we need to
6893       --  recover the names of the original discriminants if the constraint is
6894       --  given by named associations.
6895
6896       ---------------------------
6897       -- Set_Discriminant_Name --
6898       ---------------------------
6899
6900       procedure Set_Discriminant_Name (Id : Node_Id) is
6901          Disc : Entity_Id;
6902
6903       begin
6904          Set_Original_Discriminant (Id, Empty);
6905
6906          if Has_Discriminants (Typ) then
6907             Disc := First_Discriminant (Typ);
6908
6909             while Present (Disc) loop
6910                if Chars (Disc) = Chars (Id)
6911                  and then Present (Corresponding_Discriminant (Disc))
6912                then
6913                   Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
6914                end if;
6915                Next_Discriminant (Disc);
6916             end loop;
6917          end if;
6918       end Set_Discriminant_Name;
6919
6920    --  Start of processing for Build_Underlying_Full_View
6921
6922    begin
6923       if Nkind (N) = N_Full_Type_Declaration then
6924          Constr := Constraint (Subtype_Indication (Type_Definition (N)));
6925
6926       elsif Nkind (N) = N_Subtype_Declaration then
6927          Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
6928
6929       elsif Nkind (N) = N_Component_Declaration then
6930          Constr :=
6931            New_Copy_Tree
6932              (Constraint (Subtype_Indication (Component_Definition (N))));
6933
6934       else
6935          raise Program_Error;
6936       end if;
6937
6938       C := First (Constraints (Constr));
6939       while Present (C) loop
6940          if Nkind (C) = N_Discriminant_Association then
6941             Id := First (Selector_Names (C));
6942             while Present (Id) loop
6943                Set_Discriminant_Name (Id);
6944                Next (Id);
6945             end loop;
6946          end if;
6947
6948          Next (C);
6949       end loop;
6950
6951       Indic :=
6952         Make_Subtype_Declaration (Loc,
6953           Defining_Identifier => Subt,
6954           Subtype_Indication  =>
6955             Make_Subtype_Indication (Loc,
6956               Subtype_Mark => New_Reference_To (Par, Loc),
6957               Constraint   => New_Copy_Tree (Constr)));
6958
6959       --  If this is a component subtype for an outer itype, it is not
6960       --  a list member, so simply set the parent link for analysis: if
6961       --  the enclosing type does not need to be in a declarative list,
6962       --  neither do the components.
6963
6964       if Is_List_Member (N)
6965         and then Nkind (N) /= N_Component_Declaration
6966       then
6967          Insert_Before (N, Indic);
6968       else
6969          Set_Parent (Indic, Parent (N));
6970       end if;
6971
6972       Analyze (Indic);
6973       Set_Underlying_Full_View (Typ, Full_View (Subt));
6974    end Build_Underlying_Full_View;
6975
6976    -------------------------------
6977    -- Check_Abstract_Overriding --
6978    -------------------------------
6979
6980    procedure Check_Abstract_Overriding (T : Entity_Id) is
6981       Op_List  : Elist_Id;
6982       Elmt     : Elmt_Id;
6983       Subp     : Entity_Id;
6984       Type_Def : Node_Id;
6985
6986    begin
6987       Op_List := Primitive_Operations (T);
6988
6989       --  Loop to check primitive operations
6990
6991       Elmt := First_Elmt (Op_List);
6992       while Present (Elmt) loop
6993          Subp := Node (Elmt);
6994
6995          --  Special exception, do not complain about failure to override the
6996          --  stream routines _Input and _Output, since we always provide
6997          --  automatic overridings for these subprograms.
6998
6999          if Is_Abstract (Subp)
7000            and then not Is_TSS (Subp, TSS_Stream_Input)
7001            and then not Is_TSS (Subp, TSS_Stream_Output)
7002            and then not Is_Abstract (T)
7003          then
7004             if Present (Alias (Subp)) then
7005                --  Only perform the check for a derived subprogram when
7006                --  the type has an explicit record extension. This avoids
7007                --  incorrectly flagging abstract subprograms for the case
7008                --  of a type without an extension derived from a formal type
7009                --  with a tagged actual (can occur within a private part).
7010
7011                Type_Def := Type_Definition (Parent (T));
7012                if Nkind (Type_Def) = N_Derived_Type_Definition
7013                  and then Present (Record_Extension_Part (Type_Def))
7014                then
7015                   Error_Msg_NE
7016                     ("type must be declared abstract or & overridden",
7017                      T, Subp);
7018
7019                --  Ada 2005 (AI-345): Protected or task type implementing
7020                --  abstract interfaces
7021
7022                elsif Is_Concurrent_Record_Type (T)
7023                    and then Present (Abstract_Interfaces (T))
7024                then
7025                   Error_Msg_NE
7026                     ("interface subprogram & must be overridden",
7027                      T, Subp);
7028                end if;
7029             else
7030                Error_Msg_NE
7031                  ("abstract subprogram not allowed for type&",
7032                   Subp, T);
7033                Error_Msg_NE
7034                  ("nonabstract type has abstract subprogram&",
7035                   T, Subp);
7036             end if;
7037          end if;
7038
7039          Next_Elmt (Elmt);
7040       end loop;
7041    end Check_Abstract_Overriding;
7042
7043    ------------------------------------------------
7044    -- Check_Access_Discriminant_Requires_Limited --
7045    ------------------------------------------------
7046
7047    procedure Check_Access_Discriminant_Requires_Limited
7048      (D   : Node_Id;
7049       Loc : Node_Id)
7050    is
7051    begin
7052       --  A discriminant_specification for an access discriminant
7053       --  shall appear only in the declaration for a task or protected
7054       --  type, or for a type with the reserved word 'limited' in
7055       --  its definition or in one of its ancestors. (RM 3.7(10))
7056
7057       if Nkind (Discriminant_Type (D)) = N_Access_Definition
7058         and then not Is_Concurrent_Type (Current_Scope)
7059         and then not Is_Concurrent_Record_Type (Current_Scope)
7060         and then not Is_Limited_Record (Current_Scope)
7061         and then Ekind (Current_Scope) /= E_Limited_Private_Type
7062       then
7063          Error_Msg_N
7064            ("access discriminants allowed only for limited types", Loc);
7065       end if;
7066    end Check_Access_Discriminant_Requires_Limited;
7067
7068    -----------------------------------
7069    -- Check_Aliased_Component_Types --
7070    -----------------------------------
7071
7072    procedure Check_Aliased_Component_Types (T : Entity_Id) is
7073       C : Entity_Id;
7074
7075    begin
7076       --  ??? Also need to check components of record extensions, but not
7077       --  components of protected types (which are always limited).
7078
7079       --  Ada 2005: AI-363 relaxes this rule, to allow heap objects
7080       --  of such types to be unconstrained. This is safe because it is
7081       --  illegal to create access subtypes to such types with explicit
7082       --  discriminant constraints.
7083
7084       if not Is_Limited_Type (T) then
7085          if Ekind (T) = E_Record_Type then
7086             C := First_Component (T);
7087             while Present (C) loop
7088                if Is_Aliased (C)
7089                  and then Has_Discriminants (Etype (C))
7090                  and then not Is_Constrained (Etype (C))
7091                  and then not In_Instance
7092                  and then Ada_Version < Ada_05
7093                then
7094                   Error_Msg_N
7095                     ("aliased component must be constrained ('R'M 3.6(11))",
7096                       C);
7097                end if;
7098
7099                Next_Component (C);
7100             end loop;
7101
7102          elsif Ekind (T) = E_Array_Type then
7103             if Has_Aliased_Components (T)
7104               and then Has_Discriminants (Component_Type (T))
7105               and then not Is_Constrained (Component_Type (T))
7106               and then not In_Instance
7107             then
7108                Error_Msg_N
7109                  ("aliased component type must be constrained ('R'M 3.6(11))",
7110                     T);
7111             end if;
7112          end if;
7113       end if;
7114    end Check_Aliased_Component_Types;
7115
7116    ----------------------
7117    -- Check_Completion --
7118    ----------------------
7119
7120    procedure Check_Completion (Body_Id : Node_Id := Empty) is
7121       E : Entity_Id;
7122
7123       procedure Post_Error;
7124       --  Post error message for lack of completion for entity E
7125
7126       ----------------
7127       -- Post_Error --
7128       ----------------
7129
7130       procedure Post_Error is
7131       begin
7132          if not Comes_From_Source (E) then
7133
7134             if Ekind (E) = E_Task_Type
7135               or else Ekind (E) = E_Protected_Type
7136             then
7137                --  It may be an anonymous protected type created for a
7138                --  single variable. Post error on variable, if present.
7139
7140                declare
7141                   Var : Entity_Id;
7142
7143                begin
7144                   Var := First_Entity (Current_Scope);
7145
7146                   while Present (Var) loop
7147                      exit when Etype (Var) = E
7148                        and then Comes_From_Source (Var);
7149
7150                      Next_Entity (Var);
7151                   end loop;
7152
7153                   if Present (Var) then
7154                      E := Var;
7155                   end if;
7156                end;
7157             end if;
7158          end if;
7159
7160          --  If a generated entity has no completion, then either previous
7161          --  semantic errors have disabled the expansion phase, or else we had
7162          --  missing subunits, or else we are compiling without expan- sion,
7163          --  or else something is very wrong.
7164
7165          if not Comes_From_Source (E) then
7166             pragma Assert
7167               (Serious_Errors_Detected > 0
7168                 or else Configurable_Run_Time_Violations > 0
7169                 or else Subunits_Missing
7170                 or else not Expander_Active);
7171             return;
7172
7173          --  Here for source entity
7174
7175          else
7176             --  Here if no body to post the error message, so we post the error
7177             --  on the declaration that has no completion. This is not really
7178             --  the right place to post it, think about this later ???
7179
7180             if No (Body_Id) then
7181                if Is_Type (E) then
7182                   Error_Msg_NE
7183                     ("missing full declaration for }", Parent (E), E);
7184                else
7185                   Error_Msg_NE
7186                     ("missing body for &", Parent (E), E);
7187                end if;
7188
7189             --  Package body has no completion for a declaration that appears
7190             --  in the corresponding spec. Post error on the body, with a
7191             --  reference to the non-completed declaration.
7192
7193             else
7194                Error_Msg_Sloc := Sloc (E);
7195
7196                if Is_Type (E) then
7197                   Error_Msg_NE
7198                     ("missing full declaration for }!", Body_Id, E);
7199
7200                elsif Is_Overloadable (E)
7201                  and then Current_Entity_In_Scope (E) /= E
7202                then
7203                   --  It may be that the completion is mistyped and appears
7204                   --  as a  distinct overloading of the entity.
7205
7206                   declare
7207                      Candidate : constant Entity_Id :=
7208                                    Current_Entity_In_Scope (E);
7209                      Decl      : constant Node_Id :=
7210                                    Unit_Declaration_Node (Candidate);
7211
7212                   begin
7213                      if Is_Overloadable (Candidate)
7214                        and then Ekind (Candidate) = Ekind (E)
7215                        and then Nkind (Decl) = N_Subprogram_Body
7216                        and then Acts_As_Spec (Decl)
7217                      then
7218                         Check_Type_Conformant (Candidate, E);
7219
7220                      else
7221                         Error_Msg_NE ("missing body for & declared#!",
7222                            Body_Id, E);
7223                      end if;
7224                   end;
7225                else
7226                   Error_Msg_NE ("missing body for & declared#!",
7227                      Body_Id, E);
7228                end if;
7229             end if;
7230          end if;
7231       end Post_Error;
7232
7233    --  Start processing for Check_Completion
7234
7235    begin
7236       E := First_Entity (Current_Scope);
7237       while Present (E) loop
7238          if Is_Intrinsic_Subprogram (E) then
7239             null;
7240
7241          --  The following situation requires special handling: a child
7242          --  unit that appears in the context clause of the body of its
7243          --  parent:
7244
7245          --    procedure Parent.Child (...);
7246
7247          --    with Parent.Child;
7248          --    package body Parent is
7249
7250          --  Here Parent.Child appears as a local entity, but should not
7251          --  be flagged as requiring completion, because it is a
7252          --  compilation unit.
7253
7254          elsif     Ekind (E) = E_Function
7255            or else Ekind (E) = E_Procedure
7256            or else Ekind (E) = E_Generic_Function
7257            or else Ekind (E) = E_Generic_Procedure
7258          then
7259             if not Has_Completion (E)
7260               and then not Is_Abstract (E)
7261               and then Nkind (Parent (Unit_Declaration_Node (E))) /=
7262                                                        N_Compilation_Unit
7263               and then Chars (E) /= Name_uSize
7264             then
7265                Post_Error;
7266             end if;
7267
7268          elsif Is_Entry (E) then
7269             if not Has_Completion (E) and then
7270               (Ekind (Scope (E)) = E_Protected_Object
7271                 or else Ekind (Scope (E)) = E_Protected_Type)
7272             then
7273                Post_Error;
7274             end if;
7275
7276          elsif Is_Package (E) then
7277             if Unit_Requires_Body (E) then
7278                if not Has_Completion (E)
7279                  and then Nkind (Parent (Unit_Declaration_Node (E))) /=
7280                                                        N_Compilation_Unit
7281                then
7282                   Post_Error;
7283                end if;
7284
7285             elsif not Is_Child_Unit (E) then
7286                May_Need_Implicit_Body (E);
7287             end if;
7288
7289          elsif Ekind (E) = E_Incomplete_Type
7290            and then No (Underlying_Type (E))
7291          then
7292             Post_Error;
7293
7294          elsif (Ekind (E) = E_Task_Type or else
7295                 Ekind (E) = E_Protected_Type)
7296            and then not Has_Completion (E)
7297          then
7298             Post_Error;
7299
7300          --  A single task declared in the current scope is a constant, verify
7301          --  that the body of its anonymous type is in the same scope. If the
7302          --  task is defined elsewhere, this may be a renaming declaration for
7303          --  which no completion is needed.
7304
7305          elsif Ekind (E) = E_Constant
7306            and then Ekind (Etype (E)) = E_Task_Type
7307            and then not Has_Completion (Etype (E))
7308            and then Scope (Etype (E)) = Current_Scope
7309          then
7310             Post_Error;
7311
7312          elsif Ekind (E) = E_Protected_Object
7313            and then not Has_Completion (Etype (E))
7314          then
7315             Post_Error;
7316
7317          elsif Ekind (E) = E_Record_Type then
7318             if Is_Tagged_Type (E) then
7319                Check_Abstract_Overriding (E);
7320             end if;
7321
7322             Check_Aliased_Component_Types (E);
7323
7324          elsif Ekind (E) = E_Array_Type then
7325             Check_Aliased_Component_Types (E);
7326
7327          end if;
7328
7329          Next_Entity (E);
7330       end loop;
7331    end Check_Completion;
7332
7333    ----------------------------
7334    -- Check_Delta_Expression --
7335    ----------------------------
7336
7337    procedure Check_Delta_Expression (E : Node_Id) is
7338    begin
7339       if not (Is_Real_Type (Etype (E))) then
7340          Wrong_Type (E, Any_Real);
7341
7342       elsif not Is_OK_Static_Expression (E) then
7343          Flag_Non_Static_Expr
7344            ("non-static expression used for delta value!", E);
7345
7346       elsif not UR_Is_Positive (Expr_Value_R (E)) then
7347          Error_Msg_N ("delta expression must be positive", E);
7348
7349       else
7350          return;
7351       end if;
7352
7353       --  If any of above errors occurred, then replace the incorrect
7354       --  expression by the real 0.1, which should prevent further errors.
7355
7356       Rewrite (E,
7357         Make_Real_Literal (Sloc (E), Ureal_Tenth));
7358       Analyze_And_Resolve (E, Standard_Float);
7359    end Check_Delta_Expression;
7360
7361    -----------------------------
7362    -- Check_Digits_Expression --
7363    -----------------------------
7364
7365    procedure Check_Digits_Expression (E : Node_Id) is
7366    begin
7367       if not (Is_Integer_Type (Etype (E))) then
7368          Wrong_Type (E, Any_Integer);
7369
7370       elsif not Is_OK_Static_Expression (E) then
7371          Flag_Non_Static_Expr
7372            ("non-static expression used for digits value!", E);
7373
7374       elsif Expr_Value (E) <= 0 then
7375          Error_Msg_N ("digits value must be greater than zero", E);
7376
7377       else
7378          return;
7379       end if;
7380
7381       --  If any of above errors occurred, then replace the incorrect
7382       --  expression by the integer 1, which should prevent further errors.
7383
7384       Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
7385       Analyze_And_Resolve (E, Standard_Integer);
7386
7387    end Check_Digits_Expression;
7388
7389    --------------------------
7390    -- Check_Initialization --
7391    --------------------------
7392
7393    procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
7394    begin
7395       if (Is_Limited_Type (T)
7396            or else Is_Limited_Composite (T))
7397         and then not In_Instance
7398         and then not In_Inlined_Body
7399       then
7400          --  Ada 2005 (AI-287): Relax the strictness of the front-end in
7401          --  case of limited aggregates and extension aggregates.
7402
7403          if Ada_Version >= Ada_05
7404            and then (Nkind (Exp) = N_Aggregate
7405                       or else Nkind (Exp) = N_Extension_Aggregate)
7406          then
7407             null;
7408          else
7409             Error_Msg_N
7410               ("cannot initialize entities of limited type", Exp);
7411             Explain_Limited_Type (T, Exp);
7412          end if;
7413       end if;
7414    end Check_Initialization;
7415
7416    ------------------------------------
7417    -- Check_Or_Process_Discriminants --
7418    ------------------------------------
7419
7420    --  If an incomplete or private type declaration was already given for
7421    --  the type, the discriminants may have already been processed if they
7422    --  were present on the incomplete declaration. In this case a full
7423    --  conformance check is performed otherwise just process them.
7424
7425    procedure Check_Or_Process_Discriminants
7426      (N    : Node_Id;
7427       T    : Entity_Id;
7428       Prev : Entity_Id := Empty)
7429    is
7430    begin
7431       if Has_Discriminants (T) then
7432
7433          --  Make the discriminants visible to component declarations
7434
7435          declare
7436             D    : Entity_Id := First_Discriminant (T);
7437             Prev : Entity_Id;
7438
7439          begin
7440             while Present (D) loop
7441                Prev := Current_Entity (D);
7442                Set_Current_Entity (D);
7443                Set_Is_Immediately_Visible (D);
7444                Set_Homonym (D, Prev);
7445
7446                --  Ada 2005 (AI-230): Access discriminant allowed in
7447                --  non-limited record types.
7448
7449                if Ada_Version < Ada_05 then
7450
7451                   --  This restriction gets applied to the full type here; it
7452                   --  has already been applied earlier to the partial view
7453
7454                   Check_Access_Discriminant_Requires_Limited (Parent (D), N);
7455                end if;
7456
7457                Next_Discriminant (D);
7458             end loop;
7459          end;
7460
7461       elsif Present (Discriminant_Specifications (N)) then
7462          Process_Discriminants (N, Prev);
7463       end if;
7464    end Check_Or_Process_Discriminants;
7465
7466    ----------------------
7467    -- Check_Real_Bound --
7468    ----------------------
7469
7470    procedure Check_Real_Bound (Bound : Node_Id) is
7471    begin
7472       if not Is_Real_Type (Etype (Bound)) then
7473          Error_Msg_N
7474            ("bound in real type definition must be of real type", Bound);
7475
7476       elsif not Is_OK_Static_Expression (Bound) then
7477          Flag_Non_Static_Expr
7478            ("non-static expression used for real type bound!", Bound);
7479
7480       else
7481          return;
7482       end if;
7483
7484       Rewrite
7485         (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
7486       Analyze (Bound);
7487       Resolve (Bound, Standard_Float);
7488    end Check_Real_Bound;
7489
7490    ------------------------
7491    -- Collect_Interfaces --
7492    ------------------------
7493
7494    procedure Collect_Interfaces (N : Node_Id; Derived_Type : Entity_Id) is
7495       I          : Node_Id;
7496
7497       procedure Add_Interface (Iface : Entity_Id);
7498
7499       procedure Add_Interface (Iface : Entity_Id) is
7500          Elmt : Elmt_Id := First_Elmt (Abstract_Interfaces (Derived_Type));
7501
7502       begin
7503          while Present (Elmt) and then Node (Elmt) /= Iface loop
7504             Next_Elmt (Elmt);
7505          end loop;
7506
7507          if not Present (Elmt) then
7508             Append_Elmt (Node => Iface,
7509                          To   => Abstract_Interfaces (Derived_Type));
7510          end if;
7511       end Add_Interface;
7512
7513    begin
7514       pragma Assert (False
7515          or else Nkind (N) = N_Derived_Type_Definition
7516          or else Nkind (N) = N_Record_Definition
7517          or else Nkind (N) = N_Private_Extension_Declaration);
7518
7519       --  Traverse the graph of ancestor interfaces
7520
7521       if Is_Non_Empty_List (Interface_List (N)) then
7522          I := First (Interface_List (N));
7523
7524          while Present (I) loop
7525
7526             --  Protect against wrong usages. Example:
7527             --    type I is interface;
7528             --    type O is tagged null record;
7529             --    type Wrong is new I and O with null record;
7530
7531             if Is_Interface (Etype (I)) then
7532
7533                --  Do not add the interface when the derived type already
7534                --  implements this interface
7535
7536                if not Interface_Present_In_Ancestor (Derived_Type,
7537                                                      Etype (I))
7538                then
7539                   Collect_Interfaces
7540                      (Type_Definition (Parent (Etype (I))),
7541                       Derived_Type);
7542                   Add_Interface (Etype (I));
7543                end if;
7544             end if;
7545
7546             Next (I);
7547          end loop;
7548       end if;
7549    end Collect_Interfaces;
7550
7551    ------------------------------
7552    -- Complete_Private_Subtype --
7553    ------------------------------
7554
7555    procedure Complete_Private_Subtype
7556      (Priv        : Entity_Id;
7557       Full        : Entity_Id;
7558       Full_Base   : Entity_Id;
7559       Related_Nod : Node_Id)
7560    is
7561       Save_Next_Entity : Entity_Id;
7562       Save_Homonym     : Entity_Id;
7563
7564    begin
7565       --  Set semantic attributes for (implicit) private subtype completion.
7566       --  If the full type has no discriminants, then it is a copy of the full
7567       --  view of the base. Otherwise, it is a subtype of the base with a
7568       --  possible discriminant constraint. Save and restore the original
7569       --  Next_Entity field of full to ensure that the calls to Copy_Node
7570       --  do not corrupt the entity chain.
7571
7572       --  Note that the type of the full view is the same entity as the
7573       --  type of the partial view. In this fashion, the subtype has
7574       --  access to the correct view of the parent.
7575
7576       Save_Next_Entity := Next_Entity (Full);
7577       Save_Homonym     := Homonym (Priv);
7578
7579       case Ekind (Full_Base) is
7580          when E_Record_Type    |
7581               E_Record_Subtype |
7582               Class_Wide_Kind  |
7583               Private_Kind     |
7584               Task_Kind        |
7585               Protected_Kind   =>
7586             Copy_Node (Priv, Full);
7587
7588             Set_Has_Discriminants  (Full, Has_Discriminants (Full_Base));
7589             Set_First_Entity       (Full, First_Entity (Full_Base));
7590             Set_Last_Entity        (Full, Last_Entity (Full_Base));
7591
7592          when others =>
7593             Copy_Node (Full_Base, Full);
7594             Set_Chars          (Full, Chars (Priv));
7595             Conditional_Delay  (Full, Priv);
7596             Set_Sloc           (Full, Sloc (Priv));
7597       end case;
7598
7599       Set_Next_Entity (Full, Save_Next_Entity);
7600       Set_Homonym     (Full, Save_Homonym);
7601       Set_Associated_Node_For_Itype (Full, Related_Nod);
7602
7603       --  Set common attributes for all subtypes
7604
7605       Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
7606
7607       --  The Etype of the full view is inconsistent. Gigi needs to see the
7608       --  structural full view,  which is what the current scheme gives:
7609       --  the Etype of the full view is the etype of the full base. However,
7610       --  if the full base is a derived type, the full view then looks like
7611       --  a subtype of the parent, not a subtype of the full base. If instead
7612       --  we write:
7613
7614       --       Set_Etype (Full, Full_Base);
7615
7616       --  then we get inconsistencies in the front-end (confusion between
7617       --  views). Several outstanding bugs are related to this ???
7618
7619       Set_Is_First_Subtype (Full, False);
7620       Set_Scope            (Full, Scope (Priv));
7621       Set_Size_Info        (Full, Full_Base);
7622       Set_RM_Size          (Full, RM_Size (Full_Base));
7623       Set_Is_Itype         (Full);
7624
7625       --  A subtype of a private-type-without-discriminants, whose full-view
7626       --  has discriminants with default expressions, is not constrained!
7627
7628       if not Has_Discriminants (Priv) then
7629          Set_Is_Constrained (Full, Is_Constrained (Full_Base));
7630
7631          if Has_Discriminants (Full_Base) then
7632             Set_Discriminant_Constraint
7633               (Full, Discriminant_Constraint (Full_Base));
7634
7635             --  The partial view may have been indefinite, the full view
7636             --  might not be.
7637
7638             Set_Has_Unknown_Discriminants
7639               (Full, Has_Unknown_Discriminants (Full_Base));
7640          end if;
7641       end if;
7642
7643       Set_First_Rep_Item     (Full, First_Rep_Item (Full_Base));
7644       Set_Depends_On_Private (Full, Has_Private_Component (Full));
7645
7646       --  Freeze the private subtype entity if its parent is delayed, and not
7647       --  already frozen. We skip this processing if the type is an anonymous
7648       --  subtype of a record component, or is the corresponding record of a
7649       --  protected type, since ???
7650
7651       if not Is_Type (Scope (Full)) then
7652          Set_Has_Delayed_Freeze (Full,
7653            Has_Delayed_Freeze (Full_Base)
7654              and then (not Is_Frozen (Full_Base)));
7655       end if;
7656
7657       Set_Freeze_Node (Full, Empty);
7658       Set_Is_Frozen (Full, False);
7659       Set_Full_View (Priv, Full);
7660
7661       if Has_Discriminants (Full) then
7662          Set_Stored_Constraint_From_Discriminant_Constraint (Full);
7663          Set_Stored_Constraint (Priv, Stored_Constraint (Full));
7664
7665          if Has_Unknown_Discriminants (Full) then
7666             Set_Discriminant_Constraint (Full, No_Elist);
7667          end if;
7668       end if;
7669
7670       if Ekind (Full_Base) = E_Record_Type
7671         and then Has_Discriminants (Full_Base)
7672         and then Has_Discriminants (Priv) -- might not, if errors
7673         and then not Has_Unknown_Discriminants (Priv)
7674         and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
7675       then
7676          Create_Constrained_Components
7677            (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
7678
7679       --  If the full base is itself derived from private, build a congruent
7680       --  subtype of its underlying type, for use by the back end. For a
7681       --  constrained record component, the declaration cannot be placed on
7682       --  the component list, but it must neverthess be built an analyzed, to
7683       --  supply enough information for gigi to compute the size of component.
7684
7685       elsif Ekind (Full_Base) in Private_Kind
7686         and then Is_Derived_Type (Full_Base)
7687         and then Has_Discriminants (Full_Base)
7688         and then (Ekind (Current_Scope) /= E_Record_Subtype)
7689       then
7690          if not Is_Itype (Priv)
7691            and then
7692              Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
7693          then
7694             Build_Underlying_Full_View
7695               (Parent (Priv), Full, Etype (Full_Base));
7696
7697          elsif Nkind (Related_Nod) = N_Component_Declaration then
7698             Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
7699          end if;
7700
7701       elsif Is_Record_Type (Full_Base) then
7702
7703          --  Show Full is simply a renaming of Full_Base
7704
7705          Set_Cloned_Subtype (Full, Full_Base);
7706       end if;
7707
7708       --  It is unsafe to share to bounds of a scalar type, because the Itype
7709       --  is elaborated on demand, and if a bound is non-static then different
7710       --  orders of elaboration in different units will lead to different
7711       --  external symbols.
7712
7713       if Is_Scalar_Type (Full_Base) then
7714          Set_Scalar_Range (Full,
7715            Make_Range (Sloc (Related_Nod),
7716              Low_Bound  =>
7717                Duplicate_Subexpr_No_Checks (Type_Low_Bound  (Full_Base)),
7718              High_Bound =>
7719                Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
7720
7721          --  This completion inherits the bounds of the full parent, but if
7722          --  the parent is an unconstrained floating point type, so is the
7723          --  completion.
7724
7725          if Is_Floating_Point_Type (Full_Base) then
7726             Set_Includes_Infinities
7727              (Scalar_Range (Full), Has_Infinities (Full_Base));
7728          end if;
7729       end if;
7730
7731       --  ??? It seems that a lot of fields are missing that should be copied
7732       --  from Full_Base to Full. Here are some that are introduced in a
7733       --  non-disruptive way but a cleanup is necessary.
7734
7735       if Is_Tagged_Type (Full_Base) then
7736          Set_Is_Tagged_Type (Full);
7737          Set_Primitive_Operations (Full, Primitive_Operations (Full_Base));
7738          Set_Class_Wide_Type      (Full, Class_Wide_Type (Full_Base));
7739
7740       --  If this is a subtype of a protected or task type, constrain its
7741       --  corresponding record, unless this is a subtype without constraints,
7742       --  i.e. a simple renaming as with an actual subtype in an instance.
7743
7744       elsif Is_Concurrent_Type (Full_Base) then
7745          if Has_Discriminants (Full)
7746            and then Present (Corresponding_Record_Type (Full_Base))
7747            and then
7748              not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
7749          then
7750             Set_Corresponding_Record_Type (Full,
7751               Constrain_Corresponding_Record
7752                 (Full, Corresponding_Record_Type (Full_Base),
7753                   Related_Nod, Full_Base));
7754
7755          else
7756             Set_Corresponding_Record_Type (Full,
7757               Corresponding_Record_Type (Full_Base));
7758          end if;
7759       end if;
7760    end Complete_Private_Subtype;
7761
7762    -------------------------------------
7763    -- Complete_Subprograms_Derivation --
7764    -------------------------------------
7765
7766    procedure Complete_Subprograms_Derivation
7767      (Partial_View : Entity_Id;
7768       Derived_Type : Entity_Id)
7769    is
7770       Result  : constant Elist_Id := New_Elmt_List;
7771       Elmt_P  : Elmt_Id := No_Elmt;
7772       Elmt_D  : Elmt_Id;
7773       Found   : Boolean;
7774       Prim_Op : Entity_Id;
7775       E       : Entity_Id;
7776
7777    begin
7778       if Is_Tagged_Type (Partial_View) then
7779          Elmt_P := First_Elmt (Primitive_Operations (Partial_View));
7780       end if;
7781
7782       --  Inherit primitives declared with the partial-view
7783
7784       while Present (Elmt_P) loop
7785          Prim_Op := Node (Elmt_P);
7786          Found   := False;
7787          Elmt_D  := First_Elmt (Primitive_Operations (Derived_Type));
7788          while Present (Elmt_D) loop
7789             if Node (Elmt_D) = Prim_Op then
7790                Found := True;
7791                exit;
7792             end if;
7793
7794             Next_Elmt (Elmt_D);
7795          end loop;
7796
7797          if not Found then
7798             Append_Elmt (Prim_Op, Result);
7799
7800             --  Search for entries associated with abstract interfaces that
7801             --  have been covered by this primitive
7802
7803             Elmt_D  := First_Elmt (Primitive_Operations (Derived_Type));
7804             while Present (Elmt_D) loop
7805                E := Node (Elmt_D);
7806
7807                if Chars (E) = Chars (Prim_Op)
7808                  and then Is_Abstract (E)
7809                  and then Present (Alias (E))
7810                  and then Present (DTC_Entity (Alias (E)))
7811                  and then Is_Interface (Scope (DTC_Entity (Alias (E))))
7812                then
7813                   Remove_Elmt (Primitive_Operations (Derived_Type), Elmt_D);
7814                end if;
7815
7816                Next_Elmt (Elmt_D);
7817             end loop;
7818          end if;
7819
7820          Next_Elmt (Elmt_P);
7821       end loop;
7822
7823       --  Append the entities of the full-view to the list of primitives
7824       --  of derived_type
7825
7826       Elmt_D  := First_Elmt (Result);
7827       while Present (Elmt_D) loop
7828          Append_Elmt (Node (Elmt_D), Primitive_Operations (Derived_Type));
7829          Next_Elmt (Elmt_D);
7830       end loop;
7831    end Complete_Subprograms_Derivation;
7832
7833    ----------------------------
7834    -- Constant_Redeclaration --
7835    ----------------------------
7836
7837    procedure Constant_Redeclaration
7838      (Id : Entity_Id;
7839       N  : Node_Id;
7840       T  : out Entity_Id)
7841    is
7842       Prev    : constant Entity_Id := Current_Entity_In_Scope (Id);
7843       Obj_Def : constant Node_Id := Object_Definition (N);
7844       New_T   : Entity_Id;
7845
7846       procedure Check_Recursive_Declaration (Typ : Entity_Id);
7847       --  If deferred constant is an access type initialized with an
7848       --  allocator, check whether there is an illegal recursion in the
7849       --  definition, through a default value of some record subcomponent.
7850       --  This is normally detected when generating init procs, but requires
7851       --  this additional mechanism when expansion is disabled.
7852
7853       ---------------------------------
7854       -- Check_Recursive_Declaration --
7855       ---------------------------------
7856
7857       procedure Check_Recursive_Declaration (Typ : Entity_Id) is
7858          Comp : Entity_Id;
7859
7860       begin
7861          if Is_Record_Type (Typ) then
7862             Comp := First_Component (Typ);
7863             while Present (Comp) loop
7864                if Comes_From_Source (Comp) then
7865                   if Present (Expression (Parent (Comp)))
7866                     and then Is_Entity_Name (Expression (Parent (Comp)))
7867                     and then Entity (Expression (Parent (Comp))) = Prev
7868                   then
7869                      Error_Msg_Sloc := Sloc (Parent (Comp));
7870                      Error_Msg_NE
7871                        ("illegal circularity with declaration for&#",
7872                          N, Comp);
7873                      return;
7874
7875                   elsif Is_Record_Type (Etype (Comp)) then
7876                      Check_Recursive_Declaration (Etype (Comp));
7877                   end if;
7878                end if;
7879
7880                Next_Component (Comp);
7881             end loop;
7882          end if;
7883       end Check_Recursive_Declaration;
7884
7885    --  Start of processing for Constant_Redeclaration
7886
7887    begin
7888       if Nkind (Parent (Prev)) = N_Object_Declaration then
7889          if Nkind (Object_Definition
7890                      (Parent (Prev))) = N_Subtype_Indication
7891          then
7892             --  Find type of new declaration. The constraints of the two
7893             --  views must match statically, but there is no point in
7894             --  creating an itype for the full view.
7895
7896             if Nkind (Obj_Def) = N_Subtype_Indication then
7897                Find_Type (Subtype_Mark (Obj_Def));
7898                New_T := Entity (Subtype_Mark (Obj_Def));
7899
7900             else
7901                Find_Type (Obj_Def);
7902                New_T := Entity (Obj_Def);
7903             end if;
7904
7905             T := Etype (Prev);
7906
7907          else
7908             --  The full view may impose a constraint, even if the partial
7909             --  view does not, so construct the subtype.
7910
7911             New_T := Find_Type_Of_Object (Obj_Def, N);
7912             T     := New_T;
7913          end if;
7914
7915       else
7916          --  Current declaration is illegal, diagnosed below in Enter_Name
7917
7918          T := Empty;
7919          New_T := Any_Type;
7920       end if;
7921
7922       --  If previous full declaration exists, or if a homograph is present,
7923       --  let Enter_Name handle it, either with an error, or with the removal
7924       --  of an overridden implicit subprogram.
7925
7926       if Ekind (Prev) /= E_Constant
7927         or else Present (Expression (Parent (Prev)))
7928         or else Present (Full_View (Prev))
7929       then
7930          Enter_Name (Id);
7931
7932       --  Verify that types of both declarations match, or else that both types
7933       --  are anonymous access types whose designated subtypes statically match
7934       --  (as allowed in Ada 2005 by AI-385).
7935
7936       elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
7937         and then
7938           (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
7939              or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
7940              or else not Subtypes_Statically_Match
7941                            (Designated_Type (Etype (Prev)),
7942                             Designated_Type (Etype (New_T))))
7943       then
7944          Error_Msg_Sloc := Sloc (Prev);
7945          Error_Msg_N ("type does not match declaration#", N);
7946          Set_Full_View (Prev, Id);
7947          Set_Etype (Id, Any_Type);
7948
7949       --  If so, process the full constant declaration
7950
7951       else
7952          Set_Full_View (Prev, Id);
7953          Set_Is_Public (Id, Is_Public (Prev));
7954          Set_Is_Internal (Id);
7955          Append_Entity (Id, Current_Scope);
7956
7957          --  Check ALIASED present if present before (RM 7.4(7))
7958
7959          if Is_Aliased (Prev)
7960            and then not Aliased_Present (N)
7961          then
7962             Error_Msg_Sloc := Sloc (Prev);
7963             Error_Msg_N ("ALIASED required (see declaration#)", N);
7964          end if;
7965
7966          --  Check that placement is in private part and that the incomplete
7967          --  declaration appeared in the visible part.
7968
7969          if Ekind (Current_Scope) = E_Package
7970            and then not In_Private_Part (Current_Scope)
7971          then
7972             Error_Msg_Sloc := Sloc (Prev);
7973             Error_Msg_N ("full constant for declaration#"
7974                          & " must be in private part", N);
7975
7976          elsif Ekind (Current_Scope) = E_Package
7977            and then List_Containing (Parent (Prev))
7978            /= Visible_Declarations
7979              (Specification (Unit_Declaration_Node (Current_Scope)))
7980          then
7981             Error_Msg_N
7982               ("deferred constant must be declared in visible part",
7983                  Parent (Prev));
7984          end if;
7985
7986          if Is_Access_Type (T)
7987            and then Nkind (Expression (N)) = N_Allocator
7988          then
7989             Check_Recursive_Declaration (Designated_Type (T));
7990          end if;
7991       end if;
7992    end Constant_Redeclaration;
7993
7994    ----------------------
7995    -- Constrain_Access --
7996    ----------------------
7997
7998    procedure Constrain_Access
7999      (Def_Id      : in out Entity_Id;
8000       S           : Node_Id;
8001       Related_Nod : Node_Id)
8002    is
8003       T             : constant Entity_Id := Entity (Subtype_Mark (S));
8004       Desig_Type    : constant Entity_Id := Designated_Type (T);
8005       Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
8006       Constraint_OK : Boolean := True;
8007
8008       function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
8009       --  Simple predicate to test for defaulted discriminants
8010       --  Shouldn't this be in sem_util???
8011
8012       ---------------------------------
8013       -- Has_Defaulted_Discriminants --
8014       ---------------------------------
8015
8016       function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
8017       begin
8018          return Has_Discriminants (Typ)
8019           and then Present (First_Discriminant (Typ))
8020           and then Present
8021             (Discriminant_Default_Value (First_Discriminant (Typ)));
8022       end Has_Defaulted_Discriminants;
8023
8024    --  Start of processing for Constrain_Access
8025
8026    begin
8027       if Is_Array_Type (Desig_Type) then
8028          Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
8029
8030       elsif (Is_Record_Type (Desig_Type)
8031               or else Is_Incomplete_Or_Private_Type (Desig_Type))
8032         and then not Is_Constrained (Desig_Type)
8033       then
8034          --  ??? The following code is a temporary kludge to ignore a
8035          --  discriminant constraint on access type if it is constraining
8036          --  the current record. Avoid creating the implicit subtype of the
8037          --  record we are currently compiling since right now, we cannot
8038          --  handle these. For now, just return the access type itself.
8039
8040          if Desig_Type = Current_Scope
8041            and then No (Def_Id)
8042          then
8043             Set_Ekind (Desig_Subtype, E_Record_Subtype);
8044             Def_Id := Entity (Subtype_Mark (S));
8045
8046             --  This call added to ensure that the constraint is analyzed
8047             --  (needed for a B test). Note that we still return early from
8048             --  this procedure to avoid recursive processing. ???
8049
8050             Constrain_Discriminated_Type
8051               (Desig_Subtype, S, Related_Nod, For_Access => True);
8052             return;
8053          end if;
8054
8055          if Ekind (T) = E_General_Access_Type
8056            and then Has_Private_Declaration (Desig_Type)
8057            and then In_Open_Scopes (Scope (Desig_Type))
8058          then
8059             --  Enforce rule that the constraint is illegal if there is
8060             --  an unconstrained view of the designated type. This means
8061             --  that the partial view (either a private type declaration or
8062             --  a derivation from a private type) has no discriminants.
8063             --  (Defect Report 8652/0008, Technical Corrigendum 1, checked
8064             --  by ACATS B371001).
8065             --  Rule updated for Ada 2005: the private type is said to have
8066             --  a constrained partial view, given that objects of the type
8067             --  can be declared.
8068
8069             declare
8070                Pack  : constant Node_Id :=
8071                          Unit_Declaration_Node (Scope (Desig_Type));
8072                Decls : List_Id;
8073                Decl  : Node_Id;
8074
8075             begin
8076                if Nkind (Pack) = N_Package_Declaration then
8077                   Decls := Visible_Declarations (Specification (Pack));
8078                   Decl := First (Decls);
8079                   while Present (Decl) loop
8080                      if (Nkind (Decl) = N_Private_Type_Declaration
8081                           and then
8082                             Chars (Defining_Identifier (Decl)) =
8083                                                      Chars (Desig_Type))
8084
8085                        or else
8086                         (Nkind (Decl) = N_Full_Type_Declaration
8087                           and then
8088                             Chars (Defining_Identifier (Decl)) =
8089                                                      Chars (Desig_Type)
8090                           and then Is_Derived_Type (Desig_Type)
8091                           and then
8092                             Has_Private_Declaration (Etype (Desig_Type)))
8093                      then
8094                         if No (Discriminant_Specifications (Decl)) then
8095                            Error_Msg_N
8096                             ("cannot constrain general access type if " &
8097                                "designated type has constrained partial view",
8098                                 S);
8099                         end if;
8100
8101                         exit;
8102                      end if;
8103
8104                      Next (Decl);
8105                   end loop;
8106                end if;
8107             end;
8108          end if;
8109
8110          Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
8111            For_Access => True);
8112
8113       elsif (Is_Task_Type (Desig_Type)
8114               or else Is_Protected_Type (Desig_Type))
8115         and then not Is_Constrained (Desig_Type)
8116       then
8117          Constrain_Concurrent
8118            (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
8119
8120       else
8121          Error_Msg_N ("invalid constraint on access type", S);
8122          Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
8123          Constraint_OK := False;
8124       end if;
8125
8126       if No (Def_Id) then
8127          Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
8128       else
8129          Set_Ekind (Def_Id, E_Access_Subtype);
8130       end if;
8131
8132       if Constraint_OK then
8133          Set_Etype (Def_Id, Base_Type (T));
8134
8135          if Is_Private_Type (Desig_Type) then
8136             Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
8137          end if;
8138       else
8139          Set_Etype (Def_Id, Any_Type);
8140       end if;
8141
8142       Set_Size_Info                (Def_Id, T);
8143       Set_Is_Constrained           (Def_Id, Constraint_OK);
8144       Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
8145       Set_Depends_On_Private       (Def_Id, Has_Private_Component (Def_Id));
8146       Set_Is_Access_Constant       (Def_Id, Is_Access_Constant (T));
8147
8148       Conditional_Delay (Def_Id, T);
8149
8150       --  AI-363 : Subtypes of general access types whose designated
8151       --  types have default discriminants are disallowed. In instances,
8152       --  the rule has to be checked against the actual, of which T is
8153       --  the subtype. In a generic body, the rule is checked assuming
8154       --  that the actual type has defaulted discriminants.
8155
8156       if Ada_Version >=  Ada_05 then
8157          if Ekind (Base_Type (T)) = E_General_Access_Type
8158            and then Has_Defaulted_Discriminants (Desig_Type)
8159          then
8160             Error_Msg_N
8161               ("access subype of general access type not allowed", S);
8162             Error_Msg_N ("\ when discriminants have defaults", S);
8163
8164          elsif Is_Access_Type (T)
8165            and then Is_Generic_Type (Desig_Type)
8166            and then Has_Discriminants (Desig_Type)
8167            and then In_Package_Body (Current_Scope)
8168          then
8169             Error_Msg_N ("access subtype not allowed in generic body", S);
8170             Error_Msg_N
8171               ("\ wben designated type is a discriminated formal", S);
8172          end if;
8173       end if;
8174    end Constrain_Access;
8175
8176    ---------------------
8177    -- Constrain_Array --
8178    ---------------------
8179
8180    procedure Constrain_Array
8181      (Def_Id      : in out Entity_Id;
8182       SI          : Node_Id;
8183       Related_Nod : Node_Id;
8184       Related_Id  : Entity_Id;
8185       Suffix      : Character)
8186    is
8187       C                     : constant Node_Id := Constraint (SI);
8188       Number_Of_Constraints : Nat := 0;
8189       Index                 : Node_Id;
8190       S, T                  : Entity_Id;
8191       Constraint_OK         : Boolean := True;
8192
8193    begin
8194       T := Entity (Subtype_Mark (SI));
8195
8196       if Ekind (T) in Access_Kind then
8197          T := Designated_Type (T);
8198       end if;
8199
8200       --  If an index constraint follows a subtype mark in a subtype indication
8201       --  then the type or subtype denoted by the subtype mark must not already
8202       --  impose an index constraint. The subtype mark must denote either an
8203       --  unconstrained array type or an access type whose designated type
8204       --  is such an array type... (RM 3.6.1)
8205
8206       if Is_Constrained (T) then
8207          Error_Msg_N
8208            ("array type is already constrained", Subtype_Mark (SI));
8209          Constraint_OK := False;
8210
8211       else
8212          S := First (Constraints (C));
8213
8214          while Present (S) loop
8215             Number_Of_Constraints := Number_Of_Constraints + 1;
8216             Next (S);
8217          end loop;
8218
8219          --  In either case, the index constraint must provide a discrete
8220          --  range for each index of the array type and the type of each
8221          --  discrete range must be the same as that of the corresponding
8222          --  index. (RM 3.6.1)
8223
8224          if Number_Of_Constraints /= Number_Dimensions (T) then
8225             Error_Msg_NE ("incorrect number of index constraints for }", C, T);
8226             Constraint_OK := False;
8227
8228          else
8229             S := First (Constraints (C));
8230             Index := First_Index (T);
8231             Analyze (Index);
8232
8233             --  Apply constraints to each index type
8234
8235             for J in 1 .. Number_Of_Constraints loop
8236                Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
8237                Next (Index);
8238                Next (S);
8239             end loop;
8240
8241          end if;
8242       end if;
8243
8244       if No (Def_Id) then
8245          Def_Id :=
8246            Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
8247          Set_Parent (Def_Id, Related_Nod);
8248
8249       else
8250          Set_Ekind (Def_Id, E_Array_Subtype);
8251       end if;
8252
8253       Set_Size_Info      (Def_Id,                (T));
8254       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8255       Set_Etype          (Def_Id, Base_Type      (T));
8256
8257       if Constraint_OK then
8258          Set_First_Index (Def_Id, First (Constraints (C)));
8259       else
8260          Set_First_Index (Def_Id, First_Index (T));
8261       end if;
8262
8263       Set_Is_Constrained     (Def_Id, True);
8264       Set_Is_Aliased         (Def_Id, Is_Aliased (T));
8265       Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
8266
8267       Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
8268       Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
8269
8270       --  Build a freeze node if parent still needs one.  Also, make sure
8271       --  that the Depends_On_Private status is set (explanation ???)
8272       --  and also that a conditional delay is set.
8273
8274       Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
8275       Conditional_Delay (Def_Id, T);
8276
8277    end Constrain_Array;
8278
8279    ------------------------------
8280    -- Constrain_Component_Type --
8281    ------------------------------
8282
8283    function Constrain_Component_Type
8284      (Comp            : Entity_Id;
8285       Constrained_Typ : Entity_Id;
8286       Related_Node    : Node_Id;
8287       Typ             : Entity_Id;
8288       Constraints     : Elist_Id) return Entity_Id
8289    is
8290       Loc         : constant Source_Ptr := Sloc (Constrained_Typ);
8291       Compon_Type : constant Entity_Id := Etype (Comp);
8292
8293       function Build_Constrained_Array_Type
8294         (Old_Type : Entity_Id) return Entity_Id;
8295       --  If Old_Type is an array type, one of whose indices is constrained
8296       --  by a discriminant, build an Itype whose constraint replaces the
8297       --  discriminant with its value in the constraint.
8298
8299       function Build_Constrained_Discriminated_Type
8300         (Old_Type : Entity_Id) return Entity_Id;
8301       --  Ditto for record components
8302
8303       function Build_Constrained_Access_Type
8304         (Old_Type : Entity_Id) return Entity_Id;
8305       --  Ditto for access types. Makes use of previous two functions, to
8306       --  constrain designated type.
8307
8308       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
8309       --  T is an array or discriminated type, C is a list of constraints
8310       --  that apply to T. This routine builds the constrained subtype.
8311
8312       function Is_Discriminant (Expr : Node_Id) return Boolean;
8313       --  Returns True if Expr is a discriminant
8314
8315       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
8316       --  Find the value of discriminant Discrim in Constraint
8317
8318       -----------------------------------
8319       -- Build_Constrained_Access_Type --
8320       -----------------------------------
8321
8322       function Build_Constrained_Access_Type
8323         (Old_Type : Entity_Id) return Entity_Id
8324       is
8325          Desig_Type    : constant Entity_Id := Designated_Type (Old_Type);
8326          Itype         : Entity_Id;
8327          Desig_Subtype : Entity_Id;
8328          Scop          : Entity_Id;
8329
8330       begin
8331          --  if the original access type was not embedded in the enclosing
8332          --  type definition, there is no need to produce a new access
8333          --  subtype. In fact every access type with an explicit constraint
8334          --  generates an itype whose scope is the enclosing record.
8335
8336          if not Is_Type (Scope (Old_Type)) then
8337             return Old_Type;
8338
8339          elsif Is_Array_Type (Desig_Type) then
8340             Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
8341
8342          elsif Has_Discriminants (Desig_Type) then
8343
8344             --  This may be an access type to an enclosing record type for
8345             --  which we are constructing the constrained components. Return
8346             --  the enclosing record subtype. This is not always correct,
8347             --  but avoids infinite recursion. ???
8348
8349             Desig_Subtype := Any_Type;
8350
8351             for J in reverse 0 .. Scope_Stack.Last loop
8352                Scop := Scope_Stack.Table (J).Entity;
8353
8354                if Is_Type (Scop)
8355                  and then Base_Type (Scop) = Base_Type (Desig_Type)
8356                then
8357                   Desig_Subtype := Scop;
8358                end if;
8359
8360                exit when not Is_Type (Scop);
8361             end loop;
8362
8363             if Desig_Subtype = Any_Type then
8364                Desig_Subtype :=
8365                  Build_Constrained_Discriminated_Type (Desig_Type);
8366             end if;
8367
8368          else
8369             return Old_Type;
8370          end if;
8371
8372          if Desig_Subtype /= Desig_Type then
8373
8374             --  The Related_Node better be here or else we won't be able
8375             --  to attach new itypes to a node in the tree.
8376
8377             pragma Assert (Present (Related_Node));
8378
8379             Itype := Create_Itype (E_Access_Subtype, Related_Node);
8380
8381             Set_Etype                    (Itype, Base_Type      (Old_Type));
8382             Set_Size_Info                (Itype,                (Old_Type));
8383             Set_Directly_Designated_Type (Itype, Desig_Subtype);
8384             Set_Depends_On_Private       (Itype, Has_Private_Component
8385                                                                 (Old_Type));
8386             Set_Is_Access_Constant       (Itype, Is_Access_Constant
8387                                                                 (Old_Type));
8388
8389             --  The new itype needs freezing when it depends on a not frozen
8390             --  type and the enclosing subtype needs freezing.
8391
8392             if Has_Delayed_Freeze (Constrained_Typ)
8393               and then not Is_Frozen (Constrained_Typ)
8394             then
8395                Conditional_Delay (Itype, Base_Type (Old_Type));
8396             end if;
8397
8398             return Itype;
8399
8400          else
8401             return Old_Type;
8402          end if;
8403       end Build_Constrained_Access_Type;
8404
8405       ----------------------------------
8406       -- Build_Constrained_Array_Type --
8407       ----------------------------------
8408
8409       function Build_Constrained_Array_Type
8410         (Old_Type : Entity_Id) return Entity_Id
8411       is
8412          Lo_Expr     : Node_Id;
8413          Hi_Expr     : Node_Id;
8414          Old_Index   : Node_Id;
8415          Range_Node  : Node_Id;
8416          Constr_List : List_Id;
8417
8418          Need_To_Create_Itype : Boolean := False;
8419
8420       begin
8421          Old_Index := First_Index (Old_Type);
8422          while Present (Old_Index) loop
8423             Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
8424
8425             if Is_Discriminant (Lo_Expr)
8426               or else Is_Discriminant (Hi_Expr)
8427             then
8428                Need_To_Create_Itype := True;
8429             end if;
8430
8431             Next_Index (Old_Index);
8432          end loop;
8433
8434          if Need_To_Create_Itype then
8435             Constr_List := New_List;
8436
8437             Old_Index := First_Index (Old_Type);
8438             while Present (Old_Index) loop
8439                Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
8440
8441                if Is_Discriminant (Lo_Expr) then
8442                   Lo_Expr := Get_Discr_Value (Lo_Expr);
8443                end if;
8444
8445                if Is_Discriminant (Hi_Expr) then
8446                   Hi_Expr := Get_Discr_Value (Hi_Expr);
8447                end if;
8448
8449                Range_Node :=
8450                  Make_Range
8451                    (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
8452
8453                Append (Range_Node, To => Constr_List);
8454
8455                Next_Index (Old_Index);
8456             end loop;
8457
8458             return Build_Subtype (Old_Type, Constr_List);
8459
8460          else
8461             return Old_Type;
8462          end if;
8463       end Build_Constrained_Array_Type;
8464
8465       ------------------------------------------
8466       -- Build_Constrained_Discriminated_Type --
8467       ------------------------------------------
8468
8469       function Build_Constrained_Discriminated_Type
8470         (Old_Type : Entity_Id) return Entity_Id
8471       is
8472          Expr           : Node_Id;
8473          Constr_List    : List_Id;
8474          Old_Constraint : Elmt_Id;
8475
8476          Need_To_Create_Itype : Boolean := False;
8477
8478       begin
8479          Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
8480          while Present (Old_Constraint) loop
8481             Expr := Node (Old_Constraint);
8482
8483             if Is_Discriminant (Expr) then
8484                Need_To_Create_Itype := True;
8485             end if;
8486
8487             Next_Elmt (Old_Constraint);
8488          end loop;
8489
8490          if Need_To_Create_Itype then
8491             Constr_List := New_List;
8492
8493             Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
8494             while Present (Old_Constraint) loop
8495                Expr := Node (Old_Constraint);
8496
8497                if Is_Discriminant (Expr) then
8498                   Expr := Get_Discr_Value (Expr);
8499                end if;
8500
8501                Append (New_Copy_Tree (Expr), To => Constr_List);
8502
8503                Next_Elmt (Old_Constraint);
8504             end loop;
8505
8506             return Build_Subtype (Old_Type, Constr_List);
8507
8508          else
8509             return Old_Type;
8510          end if;
8511       end Build_Constrained_Discriminated_Type;
8512
8513       -------------------
8514       -- Build_Subtype --
8515       -------------------
8516
8517       function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
8518          Indic       : Node_Id;
8519          Subtyp_Decl : Node_Id;
8520          Def_Id      : Entity_Id;
8521          Btyp        : Entity_Id := Base_Type (T);
8522
8523       begin
8524          --  The Related_Node better be here or else we won't be able to
8525          --  attach new itypes to a node in the tree.
8526
8527          pragma Assert (Present (Related_Node));
8528
8529          --  If the view of the component's type is incomplete or private
8530          --  with unknown discriminants, then the constraint must be applied
8531          --  to the full type.
8532
8533          if Has_Unknown_Discriminants (Btyp)
8534            and then Present (Underlying_Type (Btyp))
8535          then
8536             Btyp := Underlying_Type (Btyp);
8537          end if;
8538
8539          Indic :=
8540            Make_Subtype_Indication (Loc,
8541              Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
8542              Constraint   => Make_Index_Or_Discriminant_Constraint (Loc, C));
8543
8544          Def_Id := Create_Itype (Ekind (T), Related_Node);
8545
8546          Subtyp_Decl :=
8547            Make_Subtype_Declaration (Loc,
8548              Defining_Identifier => Def_Id,
8549              Subtype_Indication  => Indic);
8550
8551          Set_Parent (Subtyp_Decl, Parent (Related_Node));
8552
8553          --  Itypes must be analyzed with checks off (see package Itypes)
8554
8555          Analyze (Subtyp_Decl, Suppress => All_Checks);
8556
8557          return Def_Id;
8558       end Build_Subtype;
8559
8560       ---------------------
8561       -- Get_Discr_Value --
8562       ---------------------
8563
8564       function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
8565          D : Entity_Id := First_Discriminant (Typ);
8566          E : Elmt_Id   := First_Elmt (Constraints);
8567          G : Elmt_Id;
8568
8569       begin
8570          --  The discriminant may be declared for the type, in which case we
8571          --  find it by iterating over the list of discriminants. If the
8572          --  discriminant is inherited from a parent type, it appears as the
8573          --  corresponding discriminant of the current type. This will be the
8574          --  case when constraining an inherited component whose constraint is
8575          --  given by a discriminant of the parent.
8576
8577          while Present (D) loop
8578             if D = Entity (Discrim)
8579               or else Corresponding_Discriminant (D) = Entity (Discrim)
8580             then
8581                return Node (E);
8582             end if;
8583
8584             Next_Discriminant (D);
8585             Next_Elmt (E);
8586          end loop;
8587
8588          --  The corresponding_Discriminant mechanism is incomplete, because
8589          --  the correspondence between new and old discriminants is not one
8590          --  to one: one new discriminant can constrain several old ones. In
8591          --  that case, scan sequentially the stored_constraint, the list of
8592          --  discriminants of the parents, and the constraints.
8593
8594          if Is_Derived_Type (Typ)
8595            and then Present (Stored_Constraint (Typ))
8596            and then Scope (Entity (Discrim)) = Etype (Typ)
8597          then
8598             D := First_Discriminant (Etype (Typ));
8599             E := First_Elmt (Constraints);
8600             G := First_Elmt (Stored_Constraint (Typ));
8601
8602             while Present (D) loop
8603                if D = Entity (Discrim) then
8604                   return Node (E);
8605                end if;
8606
8607                Next_Discriminant (D);
8608                Next_Elmt (E);
8609                Next_Elmt (G);
8610             end loop;
8611          end if;
8612
8613          --  Something is wrong if we did not find the value
8614
8615          raise Program_Error;
8616       end Get_Discr_Value;
8617
8618       ---------------------
8619       -- Is_Discriminant --
8620       ---------------------
8621
8622       function Is_Discriminant (Expr : Node_Id) return Boolean is
8623          Discrim_Scope : Entity_Id;
8624
8625       begin
8626          if Denotes_Discriminant (Expr) then
8627             Discrim_Scope := Scope (Entity (Expr));
8628
8629             --  Either we have a reference to one of Typ's discriminants,
8630
8631             pragma Assert (Discrim_Scope = Typ
8632
8633                --  or to the discriminants of the parent type, in the case
8634                --  of a derivation of a tagged type with variants.
8635
8636                or else Discrim_Scope = Etype (Typ)
8637                or else Full_View (Discrim_Scope) = Etype (Typ)
8638
8639                --  or same as above for the case where the discriminants
8640                --  were declared in Typ's private view.
8641
8642                or else (Is_Private_Type (Discrim_Scope)
8643                         and then Chars (Discrim_Scope) = Chars (Typ))
8644
8645                --  or else we are deriving from the full view and the
8646                --  discriminant is declared in the private entity.
8647
8648                or else (Is_Private_Type (Typ)
8649                         and then Chars (Discrim_Scope) = Chars (Typ))
8650
8651                --  or we have a class-wide type, in which case make sure the
8652                --  discriminant found belongs to the root type.
8653
8654                or else (Is_Class_Wide_Type (Typ)
8655                         and then Etype (Typ) = Discrim_Scope));
8656
8657             return True;
8658          end if;
8659
8660          --  In all other cases we have something wrong
8661
8662          return False;
8663       end Is_Discriminant;
8664
8665    --  Start of processing for Constrain_Component_Type
8666
8667    begin
8668       if Nkind (Parent (Comp)) = N_Component_Declaration
8669         and then Comes_From_Source (Parent (Comp))
8670         and then Comes_From_Source
8671           (Subtype_Indication (Component_Definition (Parent (Comp))))
8672         and then
8673           Is_Entity_Name
8674             (Subtype_Indication (Component_Definition (Parent (Comp))))
8675       then
8676          return Compon_Type;
8677
8678       elsif Is_Array_Type (Compon_Type) then
8679          return Build_Constrained_Array_Type (Compon_Type);
8680
8681       elsif Has_Discriminants (Compon_Type) then
8682          return Build_Constrained_Discriminated_Type (Compon_Type);
8683
8684       elsif Is_Access_Type (Compon_Type) then
8685          return Build_Constrained_Access_Type (Compon_Type);
8686
8687       else
8688          return Compon_Type;
8689       end if;
8690    end Constrain_Component_Type;
8691
8692    --------------------------
8693    -- Constrain_Concurrent --
8694    --------------------------
8695
8696    --  For concurrent types, the associated record value type carries the same
8697    --  discriminants, so when we constrain a concurrent type, we must constrain
8698    --  the value type as well.
8699
8700    procedure Constrain_Concurrent
8701      (Def_Id      : in out Entity_Id;
8702       SI          : Node_Id;
8703       Related_Nod : Node_Id;
8704       Related_Id  : Entity_Id;
8705       Suffix      : Character)
8706    is
8707       T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
8708       T_Val : Entity_Id;
8709
8710    begin
8711       if Ekind (T_Ent) in Access_Kind then
8712          T_Ent := Designated_Type (T_Ent);
8713       end if;
8714
8715       T_Val := Corresponding_Record_Type (T_Ent);
8716
8717       if Present (T_Val) then
8718
8719          if No (Def_Id) then
8720             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
8721          end if;
8722
8723          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
8724
8725          Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
8726          Set_Corresponding_Record_Type (Def_Id,
8727            Constrain_Corresponding_Record
8728              (Def_Id, T_Val, Related_Nod, Related_Id));
8729
8730       else
8731          --  If there is no associated record, expansion is disabled and this
8732          --  is a generic context. Create a subtype in any case, so that
8733          --  semantic analysis can proceed.
8734
8735          if No (Def_Id) then
8736             Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
8737          end if;
8738
8739          Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
8740       end if;
8741    end Constrain_Concurrent;
8742
8743    ------------------------------------
8744    -- Constrain_Corresponding_Record --
8745    ------------------------------------
8746
8747    function Constrain_Corresponding_Record
8748      (Prot_Subt   : Entity_Id;
8749       Corr_Rec    : Entity_Id;
8750       Related_Nod : Node_Id;
8751       Related_Id  : Entity_Id) return Entity_Id
8752    is
8753       T_Sub : constant Entity_Id :=
8754                 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
8755
8756    begin
8757       Set_Etype             (T_Sub, Corr_Rec);
8758       Init_Size_Align       (T_Sub);
8759       Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
8760       Set_Is_Constrained    (T_Sub, True);
8761       Set_First_Entity      (T_Sub, First_Entity (Corr_Rec));
8762       Set_Last_Entity       (T_Sub, Last_Entity  (Corr_Rec));
8763
8764       Conditional_Delay (T_Sub, Corr_Rec);
8765
8766       if Has_Discriminants (Prot_Subt) then -- False only if errors.
8767          Set_Discriminant_Constraint
8768            (T_Sub, Discriminant_Constraint (Prot_Subt));
8769          Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
8770          Create_Constrained_Components
8771            (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
8772       end if;
8773
8774       Set_Depends_On_Private      (T_Sub, Has_Private_Component (T_Sub));
8775
8776       return T_Sub;
8777    end Constrain_Corresponding_Record;
8778
8779    -----------------------
8780    -- Constrain_Decimal --
8781    -----------------------
8782
8783    procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
8784       T           : constant Entity_Id  := Entity (Subtype_Mark (S));
8785       C           : constant Node_Id    := Constraint (S);
8786       Loc         : constant Source_Ptr := Sloc (C);
8787       Range_Expr  : Node_Id;
8788       Digits_Expr : Node_Id;
8789       Digits_Val  : Uint;
8790       Bound_Val   : Ureal;
8791
8792    begin
8793       Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
8794
8795       if Nkind (C) = N_Range_Constraint then
8796          Range_Expr := Range_Expression (C);
8797          Digits_Val := Digits_Value (T);
8798
8799       else
8800          pragma Assert (Nkind (C) = N_Digits_Constraint);
8801          Digits_Expr := Digits_Expression (C);
8802          Analyze_And_Resolve (Digits_Expr, Any_Integer);
8803
8804          Check_Digits_Expression (Digits_Expr);
8805          Digits_Val := Expr_Value (Digits_Expr);
8806
8807          if Digits_Val > Digits_Value (T) then
8808             Error_Msg_N
8809                ("digits expression is incompatible with subtype", C);
8810             Digits_Val := Digits_Value (T);
8811          end if;
8812
8813          if Present (Range_Constraint (C)) then
8814             Range_Expr := Range_Expression (Range_Constraint (C));
8815          else
8816             Range_Expr := Empty;
8817          end if;
8818       end if;
8819
8820       Set_Etype            (Def_Id, Base_Type        (T));
8821       Set_Size_Info        (Def_Id,                  (T));
8822       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
8823       Set_Delta_Value      (Def_Id, Delta_Value      (T));
8824       Set_Scale_Value      (Def_Id, Scale_Value      (T));
8825       Set_Small_Value      (Def_Id, Small_Value      (T));
8826       Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
8827       Set_Digits_Value     (Def_Id, Digits_Val);
8828
8829       --  Manufacture range from given digits value if no range present
8830
8831       if No (Range_Expr) then
8832          Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
8833          Range_Expr :=
8834            Make_Range (Loc,
8835              Low_Bound =>
8836                Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
8837              High_Bound =>
8838                Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
8839       end if;
8840
8841       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
8842       Set_Discrete_RM_Size (Def_Id);
8843
8844       --  Unconditionally delay the freeze, since we cannot set size
8845       --  information in all cases correctly until the freeze point.
8846
8847       Set_Has_Delayed_Freeze (Def_Id);
8848    end Constrain_Decimal;
8849
8850    ----------------------------------
8851    -- Constrain_Discriminated_Type --
8852    ----------------------------------
8853
8854    procedure Constrain_Discriminated_Type
8855      (Def_Id      : Entity_Id;
8856       S           : Node_Id;
8857       Related_Nod : Node_Id;
8858       For_Access  : Boolean := False)
8859    is
8860       E     : constant Entity_Id := Entity (Subtype_Mark (S));
8861       T     : Entity_Id;
8862       C     : Node_Id;
8863       Elist : Elist_Id := New_Elmt_List;
8864
8865       procedure Fixup_Bad_Constraint;
8866       --  This is called after finding a bad constraint, and after having
8867       --  posted an appropriate error message. The mission is to leave the
8868       --  entity T in as reasonable state as possible!
8869
8870       --------------------------
8871       -- Fixup_Bad_Constraint --
8872       --------------------------
8873
8874       procedure Fixup_Bad_Constraint is
8875       begin
8876          --  Set a reasonable Ekind for the entity. For an incomplete type,
8877          --  we can't do much, but for other types, we can set the proper
8878          --  corresponding subtype kind.
8879
8880          if Ekind (T) = E_Incomplete_Type then
8881             Set_Ekind (Def_Id, Ekind (T));
8882          else
8883             Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
8884          end if;
8885
8886          Set_Etype (Def_Id, Any_Type);
8887          Set_Error_Posted (Def_Id);
8888       end Fixup_Bad_Constraint;
8889
8890    --  Start of processing for Constrain_Discriminated_Type
8891
8892    begin
8893       C := Constraint (S);
8894
8895       --  A discriminant constraint is only allowed in a subtype indication,
8896       --  after a subtype mark. This subtype mark must denote either a type
8897       --  with discriminants, or an access type whose designated type is a
8898       --  type with discriminants. A discriminant constraint specifies the
8899       --  values of these discriminants (RM 3.7.2(5)).
8900
8901       T := Base_Type (Entity (Subtype_Mark (S)));
8902
8903       if Ekind (T) in Access_Kind then
8904          T := Designated_Type (T);
8905       end if;
8906
8907       --  Check that the type has visible discriminants. The type may be
8908       --  a private type with unknown discriminants whose full view has
8909       --  discriminants which are invisible.
8910
8911       if not Has_Discriminants (T)
8912         or else
8913           (Has_Unknown_Discriminants (T)
8914              and then Is_Private_Type (T))
8915       then
8916          Error_Msg_N ("invalid constraint: type has no discriminant", C);
8917          Fixup_Bad_Constraint;
8918          return;
8919
8920       elsif Is_Constrained (E)
8921         or else (Ekind (E) = E_Class_Wide_Subtype
8922                   and then Present (Discriminant_Constraint (E)))
8923       then
8924          Error_Msg_N ("type is already constrained", Subtype_Mark (S));
8925          Fixup_Bad_Constraint;
8926          return;
8927       end if;
8928
8929       --  T may be an unconstrained subtype (e.g. a generic actual).
8930       --  Constraint applies to the base type.
8931
8932       T := Base_Type (T);
8933
8934       Elist := Build_Discriminant_Constraints (T, S);
8935
8936       --  If the list returned was empty we had an error in building the
8937       --  discriminant constraint. We have also already signalled an error
8938       --  in the incomplete type case
8939
8940       if Is_Empty_Elmt_List (Elist) then
8941          Fixup_Bad_Constraint;
8942          return;
8943       end if;
8944
8945       Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
8946    end Constrain_Discriminated_Type;
8947
8948    ---------------------------
8949    -- Constrain_Enumeration --
8950    ---------------------------
8951
8952    procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
8953       T : constant Entity_Id := Entity (Subtype_Mark (S));
8954       C : constant Node_Id   := Constraint (S);
8955
8956    begin
8957       Set_Ekind (Def_Id, E_Enumeration_Subtype);
8958
8959       Set_First_Literal     (Def_Id, First_Literal (Base_Type (T)));
8960
8961       Set_Etype             (Def_Id, Base_Type         (T));
8962       Set_Size_Info         (Def_Id,                   (T));
8963       Set_First_Rep_Item    (Def_Id, First_Rep_Item    (T));
8964       Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
8965
8966       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
8967
8968       Set_Discrete_RM_Size (Def_Id);
8969    end Constrain_Enumeration;
8970
8971    ----------------------
8972    -- Constrain_Float --
8973    ----------------------
8974
8975    procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
8976       T    : constant Entity_Id := Entity (Subtype_Mark (S));
8977       C    : Node_Id;
8978       D    : Node_Id;
8979       Rais : Node_Id;
8980
8981    begin
8982       Set_Ekind (Def_Id, E_Floating_Point_Subtype);
8983
8984       Set_Etype          (Def_Id, Base_Type      (T));
8985       Set_Size_Info      (Def_Id,                (T));
8986       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8987
8988       --  Process the constraint
8989
8990       C := Constraint (S);
8991
8992       --  Digits constraint present
8993
8994       if Nkind (C) = N_Digits_Constraint then
8995          Check_Restriction (No_Obsolescent_Features, C);
8996
8997          if Warn_On_Obsolescent_Feature then
8998             Error_Msg_N
8999               ("subtype digits constraint is an " &
9000                "obsolescent feature ('R'M 'J.3(8))?", C);
9001          end if;
9002
9003          D := Digits_Expression (C);
9004          Analyze_And_Resolve (D, Any_Integer);
9005          Check_Digits_Expression (D);
9006          Set_Digits_Value (Def_Id, Expr_Value (D));
9007
9008          --  Check that digits value is in range. Obviously we can do this
9009          --  at compile time, but it is strictly a runtime check, and of
9010          --  course there is an ACVC test that checks this!
9011
9012          if Digits_Value (Def_Id) > Digits_Value (T) then
9013             Error_Msg_Uint_1 := Digits_Value (T);
9014             Error_Msg_N ("?digits value is too large, maximum is ^", D);
9015             Rais :=
9016               Make_Raise_Constraint_Error (Sloc (D),
9017                 Reason => CE_Range_Check_Failed);
9018             Insert_Action (Declaration_Node (Def_Id), Rais);
9019          end if;
9020
9021          C := Range_Constraint (C);
9022
9023       --  No digits constraint present
9024
9025       else
9026          Set_Digits_Value (Def_Id, Digits_Value (T));
9027       end if;
9028
9029       --  Range constraint present
9030
9031       if Nkind (C) = N_Range_Constraint then
9032          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
9033
9034       --  No range constraint present
9035
9036       else
9037          pragma Assert (No (C));
9038          Set_Scalar_Range (Def_Id, Scalar_Range (T));
9039       end if;
9040
9041       Set_Is_Constrained (Def_Id);
9042    end Constrain_Float;
9043
9044    ---------------------
9045    -- Constrain_Index --
9046    ---------------------
9047
9048    procedure Constrain_Index
9049      (Index        : Node_Id;
9050       S            : Node_Id;
9051       Related_Nod  : Node_Id;
9052       Related_Id   : Entity_Id;
9053       Suffix       : Character;
9054       Suffix_Index : Nat)
9055    is
9056       Def_Id : Entity_Id;
9057       R      : Node_Id := Empty;
9058       T      : constant Entity_Id := Etype (Index);
9059
9060    begin
9061       if Nkind (S) = N_Range
9062         or else
9063           (Nkind (S) = N_Attribute_Reference
9064             and then Attribute_Name (S) = Name_Range)
9065       then
9066          --  A Range attribute will transformed into N_Range by Resolve
9067
9068          Analyze (S);
9069          Set_Etype (S, T);
9070          R := S;
9071
9072          Process_Range_Expr_In_Decl (R, T, Empty_List);
9073
9074          if not Error_Posted (S)
9075            and then
9076              (Nkind (S) /= N_Range
9077                or else not Covers (T, (Etype (Low_Bound (S))))
9078                or else not Covers (T, (Etype (High_Bound (S)))))
9079          then
9080             if Base_Type (T) /= Any_Type
9081               and then Etype (Low_Bound (S)) /= Any_Type
9082               and then Etype (High_Bound (S)) /= Any_Type
9083             then
9084                Error_Msg_N ("range expected", S);
9085             end if;
9086          end if;
9087
9088       elsif Nkind (S) = N_Subtype_Indication then
9089
9090          --  The parser has verified that this is a discrete indication
9091
9092          Resolve_Discrete_Subtype_Indication (S, T);
9093          R := Range_Expression (Constraint (S));
9094
9095       elsif Nkind (S) = N_Discriminant_Association then
9096
9097          --  Syntactically valid in subtype indication
9098
9099          Error_Msg_N ("invalid index constraint", S);
9100          Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
9101          return;
9102
9103       --  Subtype_Mark case, no anonymous subtypes to construct
9104
9105       else
9106          Analyze (S);
9107
9108          if Is_Entity_Name (S) then
9109             if not Is_Type (Entity (S)) then
9110                Error_Msg_N ("expect subtype mark for index constraint", S);
9111
9112             elsif Base_Type (Entity (S)) /= Base_Type (T) then
9113                Wrong_Type (S, Base_Type (T));
9114             end if;
9115
9116             return;
9117
9118          else
9119             Error_Msg_N ("invalid index constraint", S);
9120             Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
9121             return;
9122          end if;
9123       end if;
9124
9125       Def_Id :=
9126         Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
9127
9128       Set_Etype (Def_Id, Base_Type (T));
9129
9130       if Is_Modular_Integer_Type (T) then
9131          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
9132
9133       elsif Is_Integer_Type (T) then
9134          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
9135
9136       else
9137          Set_Ekind (Def_Id, E_Enumeration_Subtype);
9138          Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
9139       end if;
9140
9141       Set_Size_Info      (Def_Id,                (T));
9142       Set_RM_Size        (Def_Id, RM_Size        (T));
9143       Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9144
9145       Set_Scalar_Range   (Def_Id, R);
9146
9147       Set_Etype (S, Def_Id);
9148       Set_Discrete_RM_Size (Def_Id);
9149    end Constrain_Index;
9150
9151    -----------------------
9152    -- Constrain_Integer --
9153    -----------------------
9154
9155    procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
9156       T : constant Entity_Id := Entity (Subtype_Mark (S));
9157       C : constant Node_Id   := Constraint (S);
9158
9159    begin
9160       Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
9161
9162       if Is_Modular_Integer_Type (T) then
9163          Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
9164       else
9165          Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
9166       end if;
9167
9168       Set_Etype            (Def_Id, Base_Type        (T));
9169       Set_Size_Info        (Def_Id,                  (T));
9170       Set_First_Rep_Item   (Def_Id, First_Rep_Item   (T));
9171       Set_Discrete_RM_Size (Def_Id);
9172    end Constrain_Integer;
9173
9174    ------------------------------
9175    -- Constrain_Ordinary_Fixed --
9176    ------------------------------
9177
9178    procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
9179       T    : constant Entity_Id := Entity (Subtype_Mark (S));
9180       C    : Node_Id;
9181       D    : Node_Id;
9182       Rais : Node_Id;
9183
9184    begin
9185       Set_Ekind          (Def_Id, E_Ordinary_Fixed_Point_Subtype);
9186       Set_Etype          (Def_Id, Base_Type        (T));
9187       Set_Size_Info      (Def_Id,                  (T));
9188       Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
9189       Set_Small_Value    (Def_Id, Small_Value      (T));
9190
9191       --  Process the constraint
9192
9193       C := Constraint (S);
9194
9195       --  Delta constraint present
9196
9197       if Nkind (C) = N_Delta_Constraint then
9198          Check_Restriction (No_Obsolescent_Features, C);
9199
9200          if Warn_On_Obsolescent_Feature then
9201             Error_Msg_S
9202               ("subtype delta constraint is an " &
9203                "obsolescent feature ('R'M 'J.3(7))?");
9204          end if;
9205
9206          D := Delta_Expression (C);
9207          Analyze_And_Resolve (D, Any_Real);
9208          Check_Delta_Expression (D);
9209          Set_Delta_Value (Def_Id, Expr_Value_R (D));
9210
9211          --  Check that delta value is in range. Obviously we can do this
9212          --  at compile time, but it is strictly a runtime check, and of
9213          --  course there is an ACVC test that checks this!
9214
9215          if Delta_Value (Def_Id) < Delta_Value (T) then
9216             Error_Msg_N ("?delta value is too small", D);
9217             Rais :=
9218               Make_Raise_Constraint_Error (Sloc (D),
9219                 Reason => CE_Range_Check_Failed);
9220             Insert_Action (Declaration_Node (Def_Id), Rais);
9221          end if;
9222
9223          C := Range_Constraint (C);
9224
9225       --  No delta constraint present
9226
9227       else
9228          Set_Delta_Value (Def_Id, Delta_Value (T));
9229       end if;
9230
9231       --  Range constraint present
9232
9233       if Nkind (C) = N_Range_Constraint then
9234          Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
9235
9236       --  No range constraint present
9237
9238       else
9239          pragma Assert (No (C));
9240          Set_Scalar_Range (Def_Id, Scalar_Range (T));
9241
9242       end if;
9243
9244       Set_Discrete_RM_Size (Def_Id);
9245
9246       --  Unconditionally delay the freeze, since we cannot set size
9247       --  information in all cases correctly until the freeze point.
9248
9249       Set_Has_Delayed_Freeze (Def_Id);
9250    end Constrain_Ordinary_Fixed;
9251
9252    ---------------------------
9253    -- Convert_Scalar_Bounds --
9254    ---------------------------
9255
9256    procedure Convert_Scalar_Bounds
9257      (N            : Node_Id;
9258       Parent_Type  : Entity_Id;
9259       Derived_Type : Entity_Id;
9260       Loc          : Source_Ptr)
9261    is
9262       Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
9263
9264       Lo  : Node_Id;
9265       Hi  : Node_Id;
9266       Rng : Node_Id;
9267
9268    begin
9269       Lo := Build_Scalar_Bound
9270               (Type_Low_Bound (Derived_Type),
9271                Parent_Type, Implicit_Base);
9272
9273       Hi := Build_Scalar_Bound
9274               (Type_High_Bound (Derived_Type),
9275                Parent_Type, Implicit_Base);
9276
9277       Rng :=
9278         Make_Range (Loc,
9279           Low_Bound  => Lo,
9280           High_Bound => Hi);
9281
9282       Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
9283
9284       Set_Parent (Rng, N);
9285       Set_Scalar_Range (Derived_Type, Rng);
9286
9287       --  Analyze the bounds
9288
9289       Analyze_And_Resolve (Lo, Implicit_Base);
9290       Analyze_And_Resolve (Hi, Implicit_Base);
9291
9292       --  Analyze the range itself, except that we do not analyze it if
9293       --  the bounds are real literals, and we have a fixed-point type.
9294       --  The reason for this is that we delay setting the bounds in this
9295       --  case till we know the final Small and Size values (see circuit
9296       --  in Freeze.Freeze_Fixed_Point_Type for further details).
9297
9298       if Is_Fixed_Point_Type (Parent_Type)
9299         and then Nkind (Lo) = N_Real_Literal
9300         and then Nkind (Hi) = N_Real_Literal
9301       then
9302          return;
9303
9304       --  Here we do the analysis of the range
9305
9306       --  Note: we do this manually, since if we do a normal Analyze and
9307       --  Resolve call, there are problems with the conversions used for
9308       --  the derived type range.
9309
9310       else
9311          Set_Etype    (Rng, Implicit_Base);
9312          Set_Analyzed (Rng, True);
9313       end if;
9314    end Convert_Scalar_Bounds;
9315
9316    -------------------
9317    -- Copy_And_Swap --
9318    -------------------
9319
9320    procedure Copy_And_Swap (Priv, Full : Entity_Id) is
9321    begin
9322       --  Initialize new full declaration entity by copying the pertinent
9323       --  fields of the corresponding private declaration entity.
9324
9325       --  We temporarily set Ekind to a value appropriate for a type to
9326       --  avoid assert failures in Einfo from checking for setting type
9327       --  attributes on something that is not a type. Ekind (Priv) is an
9328       --  appropriate choice, since it allowed the attributes to be set
9329       --  in the first place. This Ekind value will be modified later.
9330
9331       Set_Ekind (Full, Ekind (Priv));
9332
9333       --  Also set Etype temporarily to Any_Type, again, in the absence
9334       --  of errors, it will be properly reset, and if there are errors,
9335       --  then we want a value of Any_Type to remain.
9336
9337       Set_Etype (Full, Any_Type);
9338
9339       --  Now start copying attributes
9340
9341       Set_Has_Discriminants          (Full, Has_Discriminants       (Priv));
9342
9343       if Has_Discriminants (Full) then
9344          Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
9345          Set_Stored_Constraint       (Full, Stored_Constraint       (Priv));
9346       end if;
9347
9348       Set_First_Rep_Item             (Full, First_Rep_Item          (Priv));
9349       Set_Homonym                    (Full, Homonym                 (Priv));
9350       Set_Is_Immediately_Visible     (Full, Is_Immediately_Visible  (Priv));
9351       Set_Is_Public                  (Full, Is_Public               (Priv));
9352       Set_Is_Pure                    (Full, Is_Pure                 (Priv));
9353       Set_Is_Tagged_Type             (Full, Is_Tagged_Type          (Priv));
9354
9355       Conditional_Delay              (Full,                          Priv);
9356
9357       if Is_Tagged_Type (Full) then
9358          Set_Primitive_Operations    (Full, Primitive_Operations    (Priv));
9359
9360          if Priv = Base_Type (Priv) then
9361             Set_Class_Wide_Type      (Full, Class_Wide_Type         (Priv));
9362          end if;
9363       end if;
9364
9365       Set_Is_Volatile                (Full, Is_Volatile             (Priv));
9366       Set_Treat_As_Volatile          (Full, Treat_As_Volatile       (Priv));
9367       Set_Scope                      (Full, Scope                   (Priv));
9368       Set_Next_Entity                (Full, Next_Entity             (Priv));
9369       Set_First_Entity               (Full, First_Entity            (Priv));
9370       Set_Last_Entity                (Full, Last_Entity             (Priv));
9371
9372       --  If access types have been recorded for later handling, keep them in
9373       --  the full view so that they get handled when the full view freeze
9374       --  node is expanded.
9375
9376       if Present (Freeze_Node (Priv))
9377         and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
9378       then
9379          Ensure_Freeze_Node (Full);
9380          Set_Access_Types_To_Process
9381            (Freeze_Node (Full),
9382             Access_Types_To_Process (Freeze_Node (Priv)));
9383       end if;
9384
9385       --  Swap the two entities. Now Privat is the full type entity and
9386       --  Full is the private one. They will be swapped back at the end
9387       --  of the private part. This swapping ensures that the entity that
9388       --  is visible in the private part is the full declaration.
9389
9390       Exchange_Entities (Priv, Full);
9391       Append_Entity (Full, Scope (Full));
9392    end Copy_And_Swap;
9393
9394    -------------------------------------
9395    -- Copy_Array_Base_Type_Attributes --
9396    -------------------------------------
9397
9398    procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
9399    begin
9400       Set_Component_Alignment      (T1, Component_Alignment      (T2));
9401       Set_Component_Type           (T1, Component_Type           (T2));
9402       Set_Component_Size           (T1, Component_Size           (T2));
9403       Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
9404       Set_Finalize_Storage_Only    (T1, Finalize_Storage_Only    (T2));
9405       Set_Has_Non_Standard_Rep     (T1, Has_Non_Standard_Rep     (T2));
9406       Set_Has_Task                 (T1, Has_Task                 (T2));
9407       Set_Is_Packed                (T1, Is_Packed                (T2));
9408       Set_Has_Aliased_Components   (T1, Has_Aliased_Components   (T2));
9409       Set_Has_Atomic_Components    (T1, Has_Atomic_Components    (T2));
9410       Set_Has_Volatile_Components  (T1, Has_Volatile_Components  (T2));
9411    end Copy_Array_Base_Type_Attributes;
9412
9413    -----------------------------------
9414    -- Copy_Array_Subtype_Attributes --
9415    -----------------------------------
9416
9417    procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
9418    begin
9419       Set_Size_Info (T1, T2);
9420
9421       Set_First_Index          (T1, First_Index           (T2));
9422       Set_Is_Aliased           (T1, Is_Aliased            (T2));
9423       Set_Is_Atomic            (T1, Is_Atomic             (T2));
9424       Set_Is_Volatile          (T1, Is_Volatile           (T2));
9425       Set_Treat_As_Volatile    (T1, Treat_As_Volatile     (T2));
9426       Set_Is_Constrained       (T1, Is_Constrained        (T2));
9427       Set_Depends_On_Private   (T1, Has_Private_Component (T2));
9428       Set_First_Rep_Item       (T1, First_Rep_Item        (T2));
9429       Set_Convention           (T1, Convention            (T2));
9430       Set_Is_Limited_Composite (T1, Is_Limited_Composite  (T2));
9431       Set_Is_Private_Composite (T1, Is_Private_Composite  (T2));
9432    end Copy_Array_Subtype_Attributes;
9433
9434    -----------------------------------
9435    -- Create_Constrained_Components --
9436    -----------------------------------
9437
9438    procedure Create_Constrained_Components
9439      (Subt        : Entity_Id;
9440       Decl_Node   : Node_Id;
9441       Typ         : Entity_Id;
9442       Constraints : Elist_Id)
9443    is
9444       Loc         : constant Source_Ptr := Sloc (Subt);
9445       Comp_List   : constant Elist_Id   := New_Elmt_List;
9446       Parent_Type : constant Entity_Id  := Etype (Typ);
9447       Assoc_List  : constant List_Id    := New_List;
9448       Discr_Val   : Elmt_Id;
9449       Errors      : Boolean;
9450       New_C       : Entity_Id;
9451       Old_C       : Entity_Id;
9452       Is_Static   : Boolean := True;
9453
9454       procedure Collect_Fixed_Components (Typ : Entity_Id);
9455       --  Collect parent type components that do not appear in a variant part
9456
9457       procedure Create_All_Components;
9458       --  Iterate over Comp_List to create the components of the subtype
9459
9460       function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
9461       --  Creates a new component from Old_Compon, copying all the fields from
9462       --  it, including its Etype, inserts the new component in the Subt entity
9463       --  chain and returns the new component.
9464
9465       function Is_Variant_Record (T : Entity_Id) return Boolean;
9466       --  If true, and discriminants are static, collect only components from
9467       --  variants selected by discriminant values.
9468
9469       ------------------------------
9470       -- Collect_Fixed_Components --
9471       ------------------------------
9472
9473       procedure Collect_Fixed_Components (Typ : Entity_Id) is
9474       begin
9475       --  Build association list for discriminants, and find components of the
9476       --  variant part selected by the values of the discriminants.
9477
9478          Old_C := First_Discriminant (Typ);
9479          Discr_Val := First_Elmt (Constraints);
9480          while Present (Old_C) loop
9481             Append_To (Assoc_List,
9482               Make_Component_Association (Loc,
9483                  Choices    => New_List (New_Occurrence_Of (Old_C, Loc)),
9484                  Expression => New_Copy (Node (Discr_Val))));
9485
9486             Next_Elmt (Discr_Val);
9487             Next_Discriminant (Old_C);
9488          end loop;
9489
9490          --  The tag, and the possible parent and controller components
9491          --  are unconditionally in the subtype.
9492
9493          if Is_Tagged_Type (Typ)
9494            or else Has_Controlled_Component (Typ)
9495          then
9496             Old_C := First_Component (Typ);
9497             while Present (Old_C) loop
9498                if Chars ((Old_C)) = Name_uTag
9499                  or else Chars ((Old_C)) = Name_uParent
9500                  or else Chars ((Old_C)) = Name_uController
9501                then
9502                   Append_Elmt (Old_C, Comp_List);
9503                end if;
9504
9505                Next_Component (Old_C);
9506             end loop;
9507          end if;
9508       end Collect_Fixed_Components;
9509
9510       ---------------------------
9511       -- Create_All_Components --
9512       ---------------------------
9513
9514       procedure Create_All_Components is
9515          Comp : Elmt_Id;
9516
9517       begin
9518          Comp := First_Elmt (Comp_List);
9519          while Present (Comp) loop
9520             Old_C := Node (Comp);
9521             New_C := Create_Component (Old_C);
9522
9523             Set_Etype
9524               (New_C,
9525                Constrain_Component_Type
9526                  (Old_C, Subt, Decl_Node, Typ, Constraints));
9527             Set_Is_Public (New_C, Is_Public (Subt));
9528
9529             Next_Elmt (Comp);
9530          end loop;
9531       end Create_All_Components;
9532
9533       ----------------------
9534       -- Create_Component --
9535       ----------------------
9536
9537       function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
9538          New_Compon : constant Entity_Id := New_Copy (Old_Compon);
9539
9540       begin
9541          --  Set the parent so we have a proper link for freezing etc. This
9542          --  is not a real parent pointer, since of course our parent does
9543          --  not own up to us and reference us, we are an illegitimate
9544          --  child of the original parent!
9545
9546          Set_Parent (New_Compon, Parent (Old_Compon));
9547
9548          --  We do not want this node marked as Comes_From_Source, since
9549          --  otherwise it would get first class status and a separate
9550          --  cross-reference line would be generated. Illegitimate
9551          --  children do not rate such recognition.
9552
9553          Set_Comes_From_Source (New_Compon, False);
9554
9555          --  But it is a real entity, and a birth certificate must be
9556          --  properly registered by entering it into the entity list.
9557
9558          Enter_Name (New_Compon);
9559          return New_Compon;
9560       end Create_Component;
9561
9562       -----------------------
9563       -- Is_Variant_Record --
9564       -----------------------
9565
9566       function Is_Variant_Record (T : Entity_Id) return Boolean is
9567       begin
9568          return Nkind (Parent (T)) = N_Full_Type_Declaration
9569            and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
9570            and then Present (Component_List (Type_Definition (Parent (T))))
9571            and then Present (
9572              Variant_Part (Component_List (Type_Definition (Parent (T)))));
9573       end Is_Variant_Record;
9574
9575    --  Start of processing for Create_Constrained_Components
9576
9577    begin
9578       pragma Assert (Subt /= Base_Type (Subt));
9579       pragma Assert (Typ = Base_Type (Typ));
9580
9581       Set_First_Entity (Subt, Empty);
9582       Set_Last_Entity  (Subt, Empty);
9583
9584       --  Check whether constraint is fully static, in which case we can
9585       --  optimize the list of components.
9586
9587       Discr_Val := First_Elmt (Constraints);
9588       while Present (Discr_Val) loop
9589          if not Is_OK_Static_Expression (Node (Discr_Val)) then
9590             Is_Static := False;
9591             exit;
9592          end if;
9593
9594          Next_Elmt (Discr_Val);
9595       end loop;
9596
9597       New_Scope (Subt);
9598
9599       --  Inherit the discriminants of the parent type
9600
9601       Old_C := First_Discriminant (Typ);
9602       while Present (Old_C) loop
9603          New_C := Create_Component (Old_C);
9604          Set_Is_Public (New_C, Is_Public (Subt));
9605          Next_Discriminant (Old_C);
9606       end loop;
9607
9608       if Is_Static
9609         and then Is_Variant_Record (Typ)
9610       then
9611          Collect_Fixed_Components (Typ);
9612
9613          Gather_Components (
9614            Typ,
9615            Component_List (Type_Definition (Parent (Typ))),
9616            Governed_By   => Assoc_List,
9617            Into          => Comp_List,
9618            Report_Errors => Errors);
9619          pragma Assert (not Errors);
9620
9621          Create_All_Components;
9622
9623       --  If the subtype declaration is created for a tagged type derivation
9624       --  with constraints, we retrieve the record definition of the parent
9625       --  type to select the components of the proper variant.
9626
9627       elsif Is_Static
9628         and then Is_Tagged_Type (Typ)
9629         and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
9630         and then
9631           Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
9632         and then Is_Variant_Record (Parent_Type)
9633       then
9634          Collect_Fixed_Components (Typ);
9635
9636          Gather_Components (
9637            Typ,
9638            Component_List (Type_Definition (Parent (Parent_Type))),
9639            Governed_By   => Assoc_List,
9640            Into          => Comp_List,
9641            Report_Errors => Errors);
9642          pragma Assert (not Errors);
9643
9644          --  If the tagged derivation has a type extension, collect all the
9645          --  new components therein.
9646
9647          if Present
9648               (Record_Extension_Part (Type_Definition (Parent (Typ))))
9649          then
9650             Old_C := First_Component (Typ);
9651             while Present (Old_C) loop
9652                if Original_Record_Component (Old_C) = Old_C
9653                 and then Chars (Old_C) /= Name_uTag
9654                 and then Chars (Old_C) /= Name_uParent
9655                 and then Chars (Old_C) /= Name_uController
9656                then
9657                   Append_Elmt (Old_C, Comp_List);
9658                end if;
9659
9660                Next_Component (Old_C);
9661             end loop;
9662          end if;
9663
9664          Create_All_Components;
9665
9666       else
9667          --  If the discriminants are not static, or if this is a multi-level
9668          --  type extension, we have to include all the components of the
9669          --  parent type.
9670
9671          Old_C := First_Component (Typ);
9672          while Present (Old_C) loop
9673             New_C := Create_Component (Old_C);
9674
9675             Set_Etype
9676               (New_C,
9677                Constrain_Component_Type
9678                  (Old_C, Subt, Decl_Node, Typ, Constraints));
9679             Set_Is_Public (New_C, Is_Public (Subt));
9680
9681             Next_Component (Old_C);
9682          end loop;
9683       end if;
9684
9685       End_Scope;
9686    end Create_Constrained_Components;
9687
9688    ------------------------------------------
9689    -- Decimal_Fixed_Point_Type_Declaration --
9690    ------------------------------------------
9691
9692    procedure Decimal_Fixed_Point_Type_Declaration
9693      (T   : Entity_Id;
9694       Def : Node_Id)
9695    is
9696       Loc           : constant Source_Ptr := Sloc (Def);
9697       Digs_Expr     : constant Node_Id    := Digits_Expression (Def);
9698       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
9699       Implicit_Base : Entity_Id;
9700       Digs_Val      : Uint;
9701       Delta_Val     : Ureal;
9702       Scale_Val     : Uint;
9703       Bound_Val     : Ureal;
9704
9705    --  Start of processing for Decimal_Fixed_Point_Type_Declaration
9706
9707    begin
9708       Check_Restriction (No_Fixed_Point, Def);
9709
9710       --  Create implicit base type
9711
9712       Implicit_Base :=
9713         Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
9714       Set_Etype (Implicit_Base, Implicit_Base);
9715
9716       --  Analyze and process delta expression
9717
9718       Analyze_And_Resolve (Delta_Expr, Universal_Real);
9719
9720       Check_Delta_Expression (Delta_Expr);
9721       Delta_Val := Expr_Value_R (Delta_Expr);
9722
9723       --  Check delta is power of 10, and determine scale value from it
9724
9725       declare
9726          Val : Ureal := Delta_Val;
9727
9728       begin
9729          Scale_Val := Uint_0;
9730
9731          if Val < Ureal_1 then
9732             while Val < Ureal_1 loop
9733                Val := Val * Ureal_10;
9734                Scale_Val := Scale_Val + 1;
9735             end loop;
9736
9737             if Scale_Val > 18 then
9738                Error_Msg_N ("scale exceeds maximum value of 18", Def);
9739                Scale_Val := UI_From_Int (+18);
9740             end if;
9741
9742          else
9743             while Val > Ureal_1 loop
9744                Val := Val / Ureal_10;
9745                Scale_Val := Scale_Val - 1;
9746             end loop;
9747
9748             if Scale_Val < -18 then
9749                Error_Msg_N ("scale is less than minimum value of -18", Def);
9750                Scale_Val := UI_From_Int (-18);
9751             end if;
9752          end if;
9753
9754          if Val /= Ureal_1 then
9755             Error_Msg_N ("delta expression must be a power of 10", Def);
9756             Delta_Val := Ureal_10 ** (-Scale_Val);
9757          end if;
9758       end;
9759
9760       --  Set delta, scale and small (small = delta for decimal type)
9761
9762       Set_Delta_Value (Implicit_Base, Delta_Val);
9763       Set_Scale_Value (Implicit_Base, Scale_Val);
9764       Set_Small_Value (Implicit_Base, Delta_Val);
9765
9766       --  Analyze and process digits expression
9767
9768       Analyze_And_Resolve (Digs_Expr, Any_Integer);
9769       Check_Digits_Expression (Digs_Expr);
9770       Digs_Val := Expr_Value (Digs_Expr);
9771
9772       if Digs_Val > 18 then
9773          Digs_Val := UI_From_Int (+18);
9774          Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
9775       end if;
9776
9777       Set_Digits_Value (Implicit_Base, Digs_Val);
9778       Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
9779
9780       --  Set range of base type from digits value for now. This will be
9781       --  expanded to represent the true underlying base range by Freeze.
9782
9783       Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
9784
9785       --  Set size to zero for now, size will be set at freeze time. We have
9786       --  to do this for ordinary fixed-point, because the size depends on
9787       --  the specified small, and we might as well do the same for decimal
9788       --  fixed-point.
9789
9790       Init_Size_Align (Implicit_Base);
9791
9792       --  If there are bounds given in the declaration use them as the
9793       --  bounds of the first named subtype.
9794
9795       if Present (Real_Range_Specification (Def)) then
9796          declare
9797             RRS      : constant Node_Id := Real_Range_Specification (Def);
9798             Low      : constant Node_Id := Low_Bound (RRS);
9799             High     : constant Node_Id := High_Bound (RRS);
9800             Low_Val  : Ureal;
9801             High_Val : Ureal;
9802
9803          begin
9804             Analyze_And_Resolve (Low, Any_Real);
9805             Analyze_And_Resolve (High, Any_Real);
9806             Check_Real_Bound (Low);
9807             Check_Real_Bound (High);
9808             Low_Val := Expr_Value_R (Low);
9809             High_Val := Expr_Value_R (High);
9810
9811             if Low_Val < (-Bound_Val) then
9812                Error_Msg_N
9813                  ("range low bound too small for digits value", Low);
9814                Low_Val := -Bound_Val;
9815             end if;
9816
9817             if High_Val > Bound_Val then
9818                Error_Msg_N
9819                  ("range high bound too large for digits value", High);
9820                High_Val := Bound_Val;
9821             end if;
9822
9823             Set_Fixed_Range (T, Loc, Low_Val, High_Val);
9824          end;
9825
9826       --  If no explicit range, use range that corresponds to given
9827       --  digits value. This will end up as the final range for the
9828       --  first subtype.
9829
9830       else
9831          Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
9832       end if;
9833
9834       --  Complete entity for first subtype
9835
9836       Set_Ekind          (T, E_Decimal_Fixed_Point_Subtype);
9837       Set_Etype          (T, Implicit_Base);
9838       Set_Size_Info      (T, Implicit_Base);
9839       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
9840       Set_Digits_Value   (T, Digs_Val);
9841       Set_Delta_Value    (T, Delta_Val);
9842       Set_Small_Value    (T, Delta_Val);
9843       Set_Scale_Value    (T, Scale_Val);
9844       Set_Is_Constrained (T);
9845    end Decimal_Fixed_Point_Type_Declaration;
9846
9847    ---------------------------------
9848    -- Derive_Interface_Subprogram --
9849    ---------------------------------
9850
9851    procedure Derive_Interface_Subprograms (Derived_Type : Entity_Id) is
9852
9853       procedure Do_Derivation (T : Entity_Id);
9854       --  This inner subprograms is used to climb to the ancestors.
9855       --  It is needed to add the derivations to the Derived_Type.
9856
9857       procedure Do_Derivation (T : Entity_Id) is
9858          Etyp : constant Entity_Id := Etype (T);
9859          AI   : Elmt_Id;
9860
9861       begin
9862          if Etyp /= T
9863            and then Is_Interface (Etyp)
9864          then
9865             Do_Derivation (Etyp);
9866          end if;
9867
9868          if Present (Abstract_Interfaces (T))
9869            and then not Is_Empty_Elmt_List (Abstract_Interfaces (T))
9870          then
9871             AI := First_Elmt (Abstract_Interfaces (T));
9872
9873             while Present (AI) loop
9874                Derive_Subprograms
9875                  (Parent_Type             => Node (AI),
9876                   Derived_Type            => Derived_Type,
9877                   Is_Interface_Derivation => True);
9878
9879                Next_Elmt (AI);
9880             end loop;
9881          end if;
9882       end Do_Derivation;
9883
9884    begin
9885       Do_Derivation (Derived_Type);
9886
9887       --  At this point the list of primitive operations of Derived_Type
9888       --  contains the entities corresponding to all the subprograms of all the
9889       --  implemented interfaces. If N interfaces have subprograms with the
9890       --  same profile we have N entities in this list because each one must be
9891       --  allocated in its corresponding virtual table.
9892
9893       --  Its alias attribute references its original interface subprogram.
9894       --  When overriden, the alias attribute is later saved in the
9895       --  Abstract_Interface_Alias attribute.
9896
9897    end Derive_Interface_Subprograms;
9898
9899    -----------------------
9900    -- Derive_Subprogram --
9901    -----------------------
9902
9903    procedure Derive_Subprogram
9904      (New_Subp     : in out Entity_Id;
9905       Parent_Subp  : Entity_Id;
9906       Derived_Type : Entity_Id;
9907       Parent_Type  : Entity_Id;
9908       Actual_Subp  : Entity_Id := Empty)
9909    is
9910       Formal       : Entity_Id;
9911       New_Formal   : Entity_Id;
9912       Visible_Subp : Entity_Id := Parent_Subp;
9913
9914       function Is_Private_Overriding return Boolean;
9915       --  If Subp is a private overriding of a visible operation, the in-
9916       --  herited operation derives from the overridden op (even though
9917       --  its body is the overriding one) and the inherited operation is
9918       --  visible now. See sem_disp to see the details of the handling of
9919       --  the overridden subprogram, which is removed from the list of
9920       --  primitive operations of the type. The overridden subprogram is
9921       --  saved locally in Visible_Subp, and used to diagnose abstract
9922       --  operations that need overriding in the derived type.
9923
9924       procedure Replace_Type (Id, New_Id : Entity_Id);
9925       --  When the type is an anonymous access type, create a new access type
9926       --  designating the derived type.
9927
9928       procedure Set_Derived_Name;
9929       --  This procedure sets the appropriate Chars name for New_Subp. This
9930       --  is normally just a copy of the parent name. An exception arises for
9931       --  type support subprograms, where the name is changed to reflect the
9932       --  name of the derived type, e.g. if type foo is derived from type bar,
9933       --  then a procedure barDA is derived with a name fooDA.
9934
9935       ---------------------------
9936       -- Is_Private_Overriding --
9937       ---------------------------
9938
9939       function Is_Private_Overriding return Boolean is
9940          Prev : Entity_Id;
9941
9942       begin
9943          --  The visible operation that is overriden is a homonym of the
9944          --  parent subprogram. We scan the homonym chain to find the one
9945          --  whose alias is the subprogram we are deriving.
9946
9947          Prev := Homonym (Parent_Subp);
9948          while Present (Prev) loop
9949             if Is_Dispatching_Operation (Parent_Subp)
9950               and then Present (Prev)
9951               and then Ekind (Prev) = Ekind (Parent_Subp)
9952               and then Alias (Prev) = Parent_Subp
9953               and then Scope (Parent_Subp) = Scope (Prev)
9954               and then not Is_Hidden (Prev)
9955             then
9956                Visible_Subp := Prev;
9957                return True;
9958             end if;
9959
9960             Prev := Homonym (Prev);
9961          end loop;
9962
9963          return False;
9964       end Is_Private_Overriding;
9965
9966       ------------------
9967       -- Replace_Type --
9968       ------------------
9969
9970       procedure Replace_Type (Id, New_Id : Entity_Id) is
9971          Acc_Type : Entity_Id;
9972          IR       : Node_Id;
9973          Par      : constant Node_Id := Parent (Derived_Type);
9974
9975       begin
9976          --  When the type is an anonymous access type, create a new access
9977          --  type designating the derived type. This itype must be elaborated
9978          --  at the point of the derivation, not on subsequent calls that may
9979          --  be out of the proper scope for Gigi, so we insert a reference to
9980          --  it after the derivation.
9981
9982          if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
9983             declare
9984                Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
9985
9986             begin
9987                if Ekind (Desig_Typ) = E_Record_Type_With_Private
9988                  and then Present (Full_View (Desig_Typ))
9989                  and then not Is_Private_Type (Parent_Type)
9990                then
9991                   Desig_Typ := Full_View (Desig_Typ);
9992                end if;
9993
9994                if Base_Type (Desig_Typ) = Base_Type (Parent_Type) then
9995                   Acc_Type := New_Copy (Etype (Id));
9996                   Set_Etype (Acc_Type, Acc_Type);
9997                   Set_Scope (Acc_Type, New_Subp);
9998
9999                   --  Compute size of anonymous access type
10000
10001                   if Is_Array_Type (Desig_Typ)
10002                     and then not Is_Constrained (Desig_Typ)
10003                   then
10004                      Init_Size (Acc_Type, 2 * System_Address_Size);
10005                   else
10006                      Init_Size (Acc_Type, System_Address_Size);
10007                   end if;
10008
10009                   Init_Alignment (Acc_Type);
10010                   Set_Directly_Designated_Type (Acc_Type, Derived_Type);
10011
10012                   Set_Etype (New_Id, Acc_Type);
10013                   Set_Scope (New_Id, New_Subp);
10014
10015                   --  Create a reference to it
10016
10017                   IR := Make_Itype_Reference (Sloc (Parent (Derived_Type)));
10018                   Set_Itype (IR, Acc_Type);
10019                   Insert_After (Parent (Derived_Type), IR);
10020
10021                else
10022                   Set_Etype (New_Id, Etype (Id));
10023                end if;
10024             end;
10025
10026          elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
10027            or else
10028              (Ekind (Etype (Id)) = E_Record_Type_With_Private
10029                and then Present (Full_View (Etype (Id)))
10030                and then
10031                  Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
10032          then
10033             --  Constraint checks on formals are generated during expansion,
10034             --  based on the signature of the original subprogram. The bounds
10035             --  of the derived type are not relevant, and thus we can use
10036             --  the base type for the formals. However, the return type may be
10037             --  used in a context that requires that the proper static bounds
10038             --  be used (a case statement, for example)  and for those cases
10039             --  we must use the derived type (first subtype), not its base.
10040
10041             --  If the derived_type_definition has no constraints, we know that
10042             --  the derived type has the same constraints as the first subtype
10043             --  of the parent, and we can also use it rather than its base,
10044             --  which can lead to more efficient code.
10045
10046             if Etype (Id) = Parent_Type then
10047                if Is_Scalar_Type (Parent_Type)
10048                  and then
10049                    Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
10050                then
10051                   Set_Etype (New_Id, Derived_Type);
10052
10053                elsif Nkind (Par) = N_Full_Type_Declaration
10054                  and then
10055                    Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
10056                  and then
10057                    Is_Entity_Name
10058                      (Subtype_Indication (Type_Definition (Par)))
10059                then
10060                   Set_Etype (New_Id, Derived_Type);
10061
10062                else
10063                   Set_Etype (New_Id, Base_Type (Derived_Type));
10064                end if;
10065
10066             else
10067                Set_Etype (New_Id, Base_Type (Derived_Type));
10068             end if;
10069
10070          else
10071             Set_Etype (New_Id, Etype (Id));
10072          end if;
10073       end Replace_Type;
10074
10075       ----------------------
10076       -- Set_Derived_Name --
10077       ----------------------
10078
10079       procedure Set_Derived_Name is
10080          Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
10081       begin
10082          if Nm = TSS_Null then
10083             Set_Chars (New_Subp, Chars (Parent_Subp));
10084          else
10085             Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
10086          end if;
10087       end Set_Derived_Name;
10088
10089    --  Start of processing for Derive_Subprogram
10090
10091    begin
10092       New_Subp :=
10093          New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
10094       Set_Ekind (New_Subp, Ekind (Parent_Subp));
10095
10096       --  Check whether the inherited subprogram is a private operation that
10097       --  should be inherited but not yet made visible. Such subprograms can
10098       --  become visible at a later point (e.g., the private part of a public
10099       --  child unit) via Declare_Inherited_Private_Subprograms. If the
10100       --  following predicate is true, then this is not such a private
10101       --  operation and the subprogram simply inherits the name of the parent
10102       --  subprogram. Note the special check for the names of controlled
10103       --  operations, which are currently exempted from being inherited with
10104       --  a hidden name because they must be findable for generation of
10105       --  implicit run-time calls.
10106
10107       if not Is_Hidden (Parent_Subp)
10108         or else Is_Internal (Parent_Subp)
10109         or else Is_Private_Overriding
10110         or else Is_Internal_Name (Chars (Parent_Subp))
10111         or else Chars (Parent_Subp) = Name_Initialize
10112         or else Chars (Parent_Subp) = Name_Adjust
10113         or else Chars (Parent_Subp) = Name_Finalize
10114       then
10115          Set_Derived_Name;
10116
10117       --  If parent is hidden, this can be a regular derivation if the
10118       --  parent is immediately visible in a non-instantiating context,
10119       --  or if we are in the private part of an instance. This test
10120       --  should still be refined ???
10121
10122       --  The test for In_Instance_Not_Visible avoids inheriting the derived
10123       --  operation as a non-visible operation in cases where the parent
10124       --  subprogram might not be visible now, but was visible within the
10125       --  original generic, so it would be wrong to make the inherited
10126       --  subprogram non-visible now. (Not clear if this test is fully
10127       --  correct; are there any cases where we should declare the inherited
10128       --  operation as not visible to avoid it being overridden, e.g., when
10129       --  the parent type is a generic actual with private primitives ???)
10130
10131       --  (they should be treated the same as other private inherited
10132       --  subprograms, but it's not clear how to do this cleanly). ???
10133
10134       elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
10135               and then Is_Immediately_Visible (Parent_Subp)
10136               and then not In_Instance)
10137         or else In_Instance_Not_Visible
10138       then
10139          Set_Derived_Name;
10140
10141       --  The type is inheriting a private operation, so enter
10142       --  it with a special name so it can't be overridden.
10143
10144       else
10145          Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
10146       end if;
10147
10148       Set_Parent (New_Subp, Parent (Derived_Type));
10149       Replace_Type (Parent_Subp, New_Subp);
10150       Conditional_Delay (New_Subp, Parent_Subp);
10151
10152       Formal := First_Formal (Parent_Subp);
10153       while Present (Formal) loop
10154          New_Formal := New_Copy (Formal);
10155
10156          --  Normally we do not go copying parents, but in the case of
10157          --  formals, we need to link up to the declaration (which is the
10158          --  parameter specification), and it is fine to link up to the
10159          --  original formal's parameter specification in this case.
10160
10161          Set_Parent (New_Formal, Parent (Formal));
10162
10163          Append_Entity (New_Formal, New_Subp);
10164
10165          Replace_Type (Formal, New_Formal);
10166          Next_Formal (Formal);
10167       end loop;
10168
10169       --  If this derivation corresponds to a tagged generic actual, then
10170       --  primitive operations rename those of the actual. Otherwise the
10171       --  primitive operations rename those of the parent type, If the
10172       --  parent renames an intrinsic operator, so does the new subprogram.
10173       --  We except concatenation, which is always properly typed, and does
10174       --  not get expanded as other intrinsic operations.
10175
10176       if No (Actual_Subp) then
10177          if Is_Intrinsic_Subprogram (Parent_Subp) then
10178             Set_Is_Intrinsic_Subprogram (New_Subp);
10179
10180             if Present (Alias (Parent_Subp))
10181               and then Chars (Parent_Subp) /= Name_Op_Concat
10182             then
10183                Set_Alias (New_Subp, Alias (Parent_Subp));
10184             else
10185                Set_Alias (New_Subp, Parent_Subp);
10186             end if;
10187
10188          else
10189             Set_Alias (New_Subp, Parent_Subp);
10190          end if;
10191
10192       else
10193          Set_Alias (New_Subp, Actual_Subp);
10194       end if;
10195
10196       --  Derived subprograms of a tagged type must inherit the convention
10197       --  of the parent subprogram (a requirement of AI-117). Derived
10198       --  subprograms of untagged types simply get convention Ada by default.
10199
10200       if Is_Tagged_Type (Derived_Type) then
10201          Set_Convention  (New_Subp, Convention  (Parent_Subp));
10202       end if;
10203
10204       Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
10205       Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
10206
10207       if Ekind (Parent_Subp) = E_Procedure then
10208          Set_Is_Valued_Procedure
10209            (New_Subp, Is_Valued_Procedure (Parent_Subp));
10210       end if;
10211
10212       --  A derived function with a controlling result is abstract. If the
10213       --  Derived_Type is a nonabstract formal generic derived type, then
10214       --  inherited operations are not abstract: the required check is done at
10215       --  instantiation time. If the derivation is for a generic actual, the
10216       --  function is not abstract unless the actual is.
10217
10218       if Is_Generic_Type (Derived_Type)
10219         and then not Is_Abstract (Derived_Type)
10220       then
10221          null;
10222
10223       elsif Is_Abstract (Alias (New_Subp))
10224         or else (Is_Tagged_Type (Derived_Type)
10225                    and then Etype (New_Subp) = Derived_Type
10226                    and then No (Actual_Subp))
10227       then
10228          Set_Is_Abstract (New_Subp);
10229
10230       --  Finally, if the parent type is abstract  we must verify that all
10231       --  inherited operations are either non-abstract or overridden, or
10232       --  that the derived type itself is abstract (this check is performed
10233       --  at the end of a package declaration, in Check_Abstract_Overriding).
10234       --  A private overriding in the parent type will not be visible in the
10235       --  derivation if we are not in an inner package or in a child unit of
10236       --  the parent type, in which case the abstractness of the inherited
10237       --  operation is carried to the new subprogram.
10238
10239       elsif Is_Abstract (Parent_Type)
10240         and then not In_Open_Scopes (Scope (Parent_Type))
10241         and then Is_Private_Overriding
10242         and then Is_Abstract (Visible_Subp)
10243       then
10244          Set_Alias (New_Subp, Visible_Subp);
10245          Set_Is_Abstract (New_Subp);
10246       end if;
10247
10248       New_Overloaded_Entity (New_Subp, Derived_Type);
10249
10250       --  Check for case of a derived subprogram for the instantiation of a
10251       --  formal derived tagged type, if so mark the subprogram as dispatching
10252       --  and inherit the dispatching attributes of the parent subprogram. The
10253       --  derived subprogram is effectively renaming of the actual subprogram,
10254       --  so it needs to have the same attributes as the actual.
10255
10256       if Present (Actual_Subp)
10257         and then Is_Dispatching_Operation (Parent_Subp)
10258       then
10259          Set_Is_Dispatching_Operation (New_Subp);
10260          if Present (DTC_Entity (Parent_Subp)) then
10261             Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
10262             Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
10263          end if;
10264       end if;
10265
10266       --  Indicate that a derived subprogram does not require a body and that
10267       --  it does not require processing of default expressions.
10268
10269       Set_Has_Completion (New_Subp);
10270       Set_Default_Expressions_Processed (New_Subp);
10271
10272       if Ekind (New_Subp) = E_Function then
10273          Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
10274       end if;
10275    end Derive_Subprogram;
10276
10277    ------------------------
10278    -- Derive_Subprograms --
10279    ------------------------
10280
10281    procedure Derive_Subprograms
10282      (Parent_Type             : Entity_Id;
10283       Derived_Type            : Entity_Id;
10284       Generic_Actual          : Entity_Id := Empty;
10285       Is_Interface_Derivation : Boolean   := False)
10286    is
10287       Op_List     : constant Elist_Id :=
10288                       Collect_Primitive_Operations (Parent_Type);
10289       Act_List    : Elist_Id;
10290       Act_Elmt    : Elmt_Id;
10291       Elmt        : Elmt_Id;
10292       Subp        : Entity_Id;
10293       New_Subp    : Entity_Id := Empty;
10294       Parent_Base : Entity_Id;
10295
10296    begin
10297       if Ekind (Parent_Type) = E_Record_Type_With_Private
10298         and then Has_Discriminants (Parent_Type)
10299         and then Present (Full_View (Parent_Type))
10300       then
10301          Parent_Base := Full_View (Parent_Type);
10302       else
10303          Parent_Base := Parent_Type;
10304       end if;
10305
10306       if Present (Generic_Actual) then
10307          Act_List := Collect_Primitive_Operations (Generic_Actual);
10308          Act_Elmt := First_Elmt (Act_List);
10309       else
10310          Act_Elmt := No_Elmt;
10311       end if;
10312
10313       --  Literals are derived earlier in the process of building the derived
10314       --  type, and are skipped here.
10315
10316       Elmt := First_Elmt (Op_List);
10317       while Present (Elmt) loop
10318          Subp := Node (Elmt);
10319
10320          if Ekind (Subp) /= E_Enumeration_Literal then
10321             if Is_Interface_Derivation then
10322                if not Is_Predefined_Dispatching_Operation (Subp) then
10323                   Derive_Subprogram
10324                     (New_Subp, Subp, Derived_Type, Parent_Base);
10325                end if;
10326
10327             elsif No (Generic_Actual) then
10328                Derive_Subprogram
10329                  (New_Subp, Subp, Derived_Type, Parent_Base);
10330
10331             else
10332                Derive_Subprogram (New_Subp, Subp,
10333                  Derived_Type, Parent_Base, Node (Act_Elmt));
10334                Next_Elmt (Act_Elmt);
10335             end if;
10336          end if;
10337
10338          Next_Elmt (Elmt);
10339       end loop;
10340    end Derive_Subprograms;
10341
10342    --------------------------------
10343    -- Derived_Standard_Character --
10344    --------------------------------
10345
10346    procedure Derived_Standard_Character
10347      (N             : Node_Id;
10348       Parent_Type   : Entity_Id;
10349       Derived_Type  : Entity_Id)
10350    is
10351       Loc           : constant Source_Ptr := Sloc (N);
10352       Def           : constant Node_Id    := Type_Definition (N);
10353       Indic         : constant Node_Id    := Subtype_Indication (Def);
10354       Parent_Base   : constant Entity_Id  := Base_Type (Parent_Type);
10355       Implicit_Base : constant Entity_Id  :=
10356                         Create_Itype
10357                           (E_Enumeration_Type, N, Derived_Type, 'B');
10358
10359       Lo : Node_Id;
10360       Hi : Node_Id;
10361
10362    begin
10363       Discard_Node (Process_Subtype (Indic, N));
10364
10365       Set_Etype     (Implicit_Base, Parent_Base);
10366       Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
10367       Set_RM_Size   (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
10368
10369       Set_Is_Character_Type  (Implicit_Base, True);
10370       Set_Has_Delayed_Freeze (Implicit_Base);
10371
10372       --  The bounds of the implicit base are the bounds of the parent base.
10373       --  Note that their type is the parent base.
10374
10375       Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Base));
10376       Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
10377
10378       Set_Scalar_Range (Implicit_Base,
10379         Make_Range (Loc,
10380           Low_Bound  => Lo,
10381           High_Bound => Hi));
10382
10383       Conditional_Delay (Derived_Type, Parent_Type);
10384
10385       Set_Ekind (Derived_Type, E_Enumeration_Subtype);
10386       Set_Etype (Derived_Type, Implicit_Base);
10387       Set_Size_Info         (Derived_Type, Parent_Type);
10388
10389       if Unknown_RM_Size (Derived_Type) then
10390          Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
10391       end if;
10392
10393       Set_Is_Character_Type (Derived_Type, True);
10394
10395       if Nkind (Indic) /= N_Subtype_Indication then
10396
10397          --  If no explicit constraint, the bounds are those
10398          --  of the parent type.
10399
10400          Lo := New_Copy_Tree (Type_Low_Bound  (Parent_Type));
10401          Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
10402          Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
10403       end if;
10404
10405       Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
10406
10407       --  Because the implicit base is used in the conversion of the bounds,
10408       --  we have to freeze it now. This is similar to what is done for
10409       --  numeric types, and it equally suspicious, but otherwise a non-
10410       --  static bound will have a reference to an unfrozen type, which is
10411       --  rejected by Gigi (???).
10412
10413       Freeze_Before (N, Implicit_Base);
10414    end Derived_Standard_Character;
10415
10416    ------------------------------
10417    -- Derived_Type_Declaration --
10418    ------------------------------
10419
10420    procedure Derived_Type_Declaration
10421      (T             : Entity_Id;
10422       N             : Node_Id;
10423       Is_Completion : Boolean)
10424    is
10425       Def          : constant Node_Id := Type_Definition (N);
10426       Iface_Def    : Node_Id;
10427       Indic        : constant Node_Id := Subtype_Indication (Def);
10428       Extension    : constant Node_Id := Record_Extension_Part (Def);
10429       Parent_Type  : Entity_Id;
10430       Parent_Scope : Entity_Id;
10431       Taggd        : Boolean;
10432
10433       function Comes_From_Generic (Typ : Entity_Id) return Boolean;
10434       --  Check whether the parent type is a generic formal, or derives
10435       --  directly or indirectly from one.
10436
10437       ------------------------
10438       -- Comes_From_Generic --
10439       ------------------------
10440
10441       function Comes_From_Generic (Typ : Entity_Id) return Boolean is
10442       begin
10443          if Is_Generic_Type (Typ) then
10444             return True;
10445
10446          elsif Is_Generic_Type (Root_Type (Parent_Type)) then
10447             return True;
10448
10449          elsif Is_Private_Type (Typ)
10450            and then Present (Full_View (Typ))
10451            and then Is_Generic_Type (Root_Type (Full_View (Typ)))
10452          then
10453             return True;
10454
10455          elsif Is_Generic_Actual_Type (Typ) then
10456             return True;
10457
10458          else
10459             return False;
10460          end if;
10461       end Comes_From_Generic;
10462
10463    --  Start of processing for Derived_Type_Declaration
10464
10465    begin
10466       Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
10467
10468       --  Ada 2005 (AI-251): In case of interface derivation check that the
10469       --  parent is also an interface.
10470
10471       if Interface_Present (Def) then
10472          if not Is_Interface (Parent_Type) then
10473             Error_Msg_NE ("(Ada 2005) & must be an interface",
10474                           Indic, Parent_Type);
10475
10476          else
10477             Iface_Def := Type_Definition (Parent (Parent_Type));
10478
10479             --  Ada 2005 (AI-251): Limited interfaces can only inherit from
10480             --  other limited interfaces.
10481
10482             if Limited_Present (Def) then
10483                if Limited_Present (Iface_Def) then
10484                   null;
10485
10486                elsif Protected_Present (Iface_Def) then
10487                   Error_Msg_N ("(Ada 2005) limited interface cannot" &
10488                     " inherit from protected interface", Indic);
10489
10490                elsif Synchronized_Present (Iface_Def) then
10491                   Error_Msg_N ("(Ada 2005) limited interface cannot" &
10492                     " inherit from synchronized interface", Indic);
10493
10494                elsif Task_Present (Iface_Def) then
10495                   Error_Msg_N ("(Ada 2005) limited interface cannot" &
10496                     " inherit from task interface", Indic);
10497
10498                else
10499                   Error_Msg_N ("(Ada 2005) limited interface cannot" &
10500                     " inherit from non-limited interface", Indic);
10501                end if;
10502
10503             --  Ada 2005 (AI-345): Non-limited interfaces can only inherit
10504             --  from non-limited or limited interfaces.
10505
10506             elsif not Protected_Present (Def)
10507               and then not Synchronized_Present (Def)
10508               and then not Task_Present (Def)
10509             then
10510                if Limited_Present (Iface_Def) then
10511                   null;
10512
10513                elsif Protected_Present (Iface_Def) then
10514                   Error_Msg_N ("(Ada 2005) non-limited interface cannot" &
10515                     " inherit from protected interface", Indic);
10516
10517                elsif Synchronized_Present (Iface_Def) then
10518                   Error_Msg_N ("(Ada 2005) non-limited interface cannot" &
10519                     " inherit from synchronized interface", Indic);
10520
10521                elsif Task_Present (Iface_Def) then
10522                   Error_Msg_N ("(Ada 2005) non-limited interface cannot" &
10523                     " inherit from task interface", Indic);
10524
10525                else
10526                   null;
10527                end if;
10528             end if;
10529          end if;
10530       end if;
10531
10532       --  Ada 2005 (AI-251): Decorate all the names in the list of ancestor
10533       --  interfaces
10534
10535       if Is_Tagged_Type (Parent_Type)
10536         and then Is_Non_Empty_List (Interface_List (Def))
10537       then
10538          declare
10539             I : Node_Id := First (Interface_List (Def));
10540             T : Entity_Id;
10541          begin
10542             while Present (I) loop
10543                T := Find_Type_Of_Subtype_Indic (I);
10544
10545                if not Is_Interface (T) then
10546                   Error_Msg_NE ("(Ada 2005) & must be an interface", I, T);
10547                end if;
10548
10549                Next (I);
10550             end loop;
10551          end;
10552       end if;
10553
10554       if Parent_Type = Any_Type
10555         or else Etype (Parent_Type) = Any_Type
10556         or else (Is_Class_Wide_Type (Parent_Type)
10557                    and then Etype (Parent_Type) = T)
10558       then
10559          --  If Parent_Type is undefined or illegal, make new type into a
10560          --  subtype of Any_Type, and set a few attributes to prevent cascaded
10561          --  errors. If this is a self-definition, emit error now.
10562
10563          if T = Parent_Type
10564            or else T = Etype (Parent_Type)
10565          then
10566             Error_Msg_N ("type cannot be used in its own definition", Indic);
10567          end if;
10568
10569          Set_Ekind        (T, Ekind (Parent_Type));
10570          Set_Etype        (T, Any_Type);
10571          Set_Scalar_Range (T, Scalar_Range (Any_Type));
10572
10573          if Is_Tagged_Type (T) then
10574             Set_Primitive_Operations (T, New_Elmt_List);
10575          end if;
10576
10577          return;
10578
10579       --  Ada 2005 (AI-231): Static check
10580
10581       elsif Is_Access_Type (Parent_Type)
10582         and then Null_Exclusion_Present (Type_Definition (N))
10583         and then Can_Never_Be_Null (Parent_Type)
10584       then
10585          Error_Msg_N ("(Ada 2005) null exclusion not allowed if parent is "
10586                       & "already non-null", Type_Definition (N));
10587       end if;
10588
10589       --  Only composite types other than array types are allowed to have
10590       --  discriminants.
10591
10592       if Present (Discriminant_Specifications (N))
10593         and then (Is_Elementary_Type (Parent_Type)
10594                   or else Is_Array_Type (Parent_Type))
10595         and then not Error_Posted (N)
10596       then
10597          Error_Msg_N
10598            ("elementary or array type cannot have discriminants",
10599             Defining_Identifier (First (Discriminant_Specifications (N))));
10600          Set_Has_Discriminants (T, False);
10601       end if;
10602
10603       --  In Ada 83, a derived type defined in a package specification cannot
10604       --  be used for further derivation until the end of its visible part.
10605       --  Note that derivation in the private part of the package is allowed.
10606
10607       if Ada_Version = Ada_83
10608         and then Is_Derived_Type (Parent_Type)
10609         and then In_Visible_Part (Scope (Parent_Type))
10610       then
10611          if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
10612             Error_Msg_N
10613               ("(Ada 83): premature use of type for derivation", Indic);
10614          end if;
10615       end if;
10616
10617       --  Check for early use of incomplete or private type
10618
10619       if Ekind (Parent_Type) = E_Void
10620         or else Ekind (Parent_Type) = E_Incomplete_Type
10621       then
10622          Error_Msg_N ("premature derivation of incomplete type", Indic);
10623          return;
10624
10625       elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
10626               and then not Comes_From_Generic (Parent_Type))
10627         or else Has_Private_Component (Parent_Type)
10628       then
10629          --  The ancestor type of a formal type can be incomplete, in which
10630          --  case only the operations of the partial view are available in
10631          --  the generic. Subsequent checks may be required when the full
10632          --  view is analyzed, to verify that derivation from a tagged type
10633          --  has an extension.
10634
10635          if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
10636             null;
10637
10638          elsif No (Underlying_Type (Parent_Type))
10639            or else Has_Private_Component (Parent_Type)
10640          then
10641             Error_Msg_N
10642               ("premature derivation of derived or private type", Indic);
10643
10644             --  Flag the type itself as being in error, this prevents some
10645             --  nasty problems with subsequent uses of the malformed type.
10646
10647             Set_Error_Posted (T);
10648
10649          --  Check that within the immediate scope of an untagged partial
10650          --  view it's illegal to derive from the partial view if the
10651          --  full view is tagged. (7.3(7))
10652
10653          --  We verify that the Parent_Type is a partial view by checking
10654          --  that it is not a Full_Type_Declaration (i.e. a private type or
10655          --  private extension declaration), to distinguish a partial view
10656          --  from  a derivation from a private type which also appears as
10657          --  E_Private_Type.
10658
10659          elsif Present (Full_View (Parent_Type))
10660            and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
10661            and then not Is_Tagged_Type (Parent_Type)
10662            and then Is_Tagged_Type (Full_View (Parent_Type))
10663          then
10664             Parent_Scope := Scope (T);
10665             while Present (Parent_Scope)
10666               and then Parent_Scope /= Standard_Standard
10667             loop
10668                if Parent_Scope = Scope (Parent_Type) then
10669                   Error_Msg_N
10670                     ("premature derivation from type with tagged full view",
10671                      Indic);
10672                end if;
10673
10674                Parent_Scope := Scope (Parent_Scope);
10675             end loop;
10676          end if;
10677       end if;
10678
10679       --  Check that form of derivation is appropriate
10680
10681       Taggd := Is_Tagged_Type (Parent_Type);
10682
10683       --  Perhaps the parent type should be changed to the class-wide type's
10684       --  specific type in this case to prevent cascading errors ???
10685
10686       if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
10687          Error_Msg_N ("parent type must not be a class-wide type", Indic);
10688          return;
10689       end if;
10690
10691       if Present (Extension) and then not Taggd then
10692          Error_Msg_N
10693            ("type derived from untagged type cannot have extension", Indic);
10694
10695       elsif No (Extension) and then Taggd then
10696
10697          --  If this declaration is within a private part (or body) of a
10698          --  generic instantiation then the derivation is allowed (the parent
10699          --  type can only appear tagged in this case if it's a generic actual
10700          --  type, since it would otherwise have been rejected in the analysis
10701          --  of the generic template).
10702
10703          if not Is_Generic_Actual_Type (Parent_Type)
10704            or else In_Visible_Part (Scope (Parent_Type))
10705          then
10706             Error_Msg_N
10707               ("type derived from tagged type must have extension", Indic);
10708          end if;
10709       end if;
10710
10711       Build_Derived_Type (N, Parent_Type, T, Is_Completion);
10712    end Derived_Type_Declaration;
10713
10714    ----------------------------------
10715    -- Enumeration_Type_Declaration --
10716    ----------------------------------
10717
10718    procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
10719       Ev     : Uint;
10720       L      : Node_Id;
10721       R_Node : Node_Id;
10722       B_Node : Node_Id;
10723
10724    begin
10725       --  Create identifier node representing lower bound
10726
10727       B_Node := New_Node (N_Identifier, Sloc (Def));
10728       L := First (Literals (Def));
10729       Set_Chars (B_Node, Chars (L));
10730       Set_Entity (B_Node,  L);
10731       Set_Etype (B_Node, T);
10732       Set_Is_Static_Expression (B_Node, True);
10733
10734       R_Node := New_Node (N_Range, Sloc (Def));
10735       Set_Low_Bound  (R_Node, B_Node);
10736
10737       Set_Ekind (T, E_Enumeration_Type);
10738       Set_First_Literal (T, L);
10739       Set_Etype (T, T);
10740       Set_Is_Constrained (T);
10741
10742       Ev := Uint_0;
10743
10744       --  Loop through literals of enumeration type setting pos and rep values
10745       --  except that if the Ekind is already set, then it means that the
10746       --  literal was already constructed (case of a derived type declaration
10747       --  and we should not disturb the Pos and Rep values.
10748
10749       while Present (L) loop
10750          if Ekind (L) /= E_Enumeration_Literal then
10751             Set_Ekind (L, E_Enumeration_Literal);
10752             Set_Enumeration_Pos (L, Ev);
10753             Set_Enumeration_Rep (L, Ev);
10754             Set_Is_Known_Valid  (L, True);
10755          end if;
10756
10757          Set_Etype (L, T);
10758          New_Overloaded_Entity (L);
10759          Generate_Definition (L);
10760          Set_Convention (L, Convention_Intrinsic);
10761
10762          if Nkind (L) = N_Defining_Character_Literal then
10763             Set_Is_Character_Type (T, True);
10764          end if;
10765
10766          Ev := Ev + 1;
10767          Next (L);
10768       end loop;
10769
10770       --  Now create a node representing upper bound
10771
10772       B_Node := New_Node (N_Identifier, Sloc (Def));
10773       Set_Chars (B_Node, Chars (Last (Literals (Def))));
10774       Set_Entity (B_Node,  Last (Literals (Def)));
10775       Set_Etype (B_Node, T);
10776       Set_Is_Static_Expression (B_Node, True);
10777
10778       Set_High_Bound (R_Node, B_Node);
10779       Set_Scalar_Range (T, R_Node);
10780       Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
10781       Set_Enum_Esize (T);
10782
10783       --  Set Discard_Names if configuration pragma set, or if there is
10784       --  a parameterless pragma in the current declarative region
10785
10786       if Global_Discard_Names
10787         or else Discard_Names (Scope (T))
10788       then
10789          Set_Discard_Names (T);
10790       end if;
10791
10792       --  Process end label if there is one
10793
10794       if Present (Def) then
10795          Process_End_Label (Def, 'e', T);
10796       end if;
10797    end Enumeration_Type_Declaration;
10798
10799    ---------------------------------
10800    -- Expand_To_Stored_Constraint --
10801    ---------------------------------
10802
10803    function Expand_To_Stored_Constraint
10804      (Typ        : Entity_Id;
10805       Constraint : Elist_Id) return Elist_Id
10806    is
10807       Explicitly_Discriminated_Type : Entity_Id;
10808       Expansion    : Elist_Id;
10809       Discriminant : Entity_Id;
10810
10811       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
10812       --  Find the nearest type that actually specifies discriminants
10813
10814       ---------------------------------
10815       -- Type_With_Explicit_Discrims --
10816       ---------------------------------
10817
10818       function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
10819          Typ : constant E := Base_Type (Id);
10820
10821       begin
10822          if Ekind (Typ) in Incomplete_Or_Private_Kind then
10823             if Present (Full_View (Typ)) then
10824                return Type_With_Explicit_Discrims (Full_View (Typ));
10825             end if;
10826
10827          else
10828             if Has_Discriminants (Typ) then
10829                return Typ;
10830             end if;
10831          end if;
10832
10833          if Etype (Typ) = Typ then
10834             return Empty;
10835          elsif Has_Discriminants (Typ) then
10836             return Typ;
10837          else
10838             return Type_With_Explicit_Discrims (Etype (Typ));
10839          end if;
10840
10841       end Type_With_Explicit_Discrims;
10842
10843    --  Start of processing for Expand_To_Stored_Constraint
10844
10845    begin
10846       if No (Constraint)
10847         or else Is_Empty_Elmt_List (Constraint)
10848       then
10849          return No_Elist;
10850       end if;
10851
10852       Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
10853
10854       if No (Explicitly_Discriminated_Type) then
10855          return No_Elist;
10856       end if;
10857
10858       Expansion := New_Elmt_List;
10859
10860       Discriminant :=
10861          First_Stored_Discriminant (Explicitly_Discriminated_Type);
10862       while Present (Discriminant) loop
10863          Append_Elmt (
10864            Get_Discriminant_Value (
10865              Discriminant, Explicitly_Discriminated_Type, Constraint),
10866            Expansion);
10867          Next_Stored_Discriminant (Discriminant);
10868       end loop;
10869
10870       return Expansion;
10871    end Expand_To_Stored_Constraint;
10872
10873    --------------------
10874    -- Find_Type_Name --
10875    --------------------
10876
10877    function Find_Type_Name (N : Node_Id) return Entity_Id is
10878       Id       : constant Entity_Id := Defining_Identifier (N);
10879       Prev     : Entity_Id;
10880       New_Id   : Entity_Id;
10881       Prev_Par : Node_Id;
10882
10883    begin
10884       --  Find incomplete declaration, if one was given
10885
10886       Prev := Current_Entity_In_Scope (Id);
10887
10888       if Present (Prev) then
10889
10890          --  Previous declaration exists. Error if not incomplete/private case
10891          --  except if previous declaration is implicit, etc. Enter_Name will
10892          --  emit error if appropriate.
10893
10894          Prev_Par := Parent (Prev);
10895
10896          if not Is_Incomplete_Or_Private_Type (Prev) then
10897             Enter_Name (Id);
10898             New_Id := Id;
10899
10900          elsif Nkind (N) /= N_Full_Type_Declaration
10901            and then Nkind (N) /= N_Task_Type_Declaration
10902            and then Nkind (N) /= N_Protected_Type_Declaration
10903          then
10904             --  Completion must be a full type declarations (RM 7.3(4))
10905
10906             Error_Msg_Sloc := Sloc (Prev);
10907             Error_Msg_NE ("invalid completion of }", Id, Prev);
10908
10909             --  Set scope of Id to avoid cascaded errors. Entity is never
10910             --  examined again, except when saving globals in generics.
10911
10912             Set_Scope (Id, Current_Scope);
10913             New_Id := Id;
10914
10915          --  Case of full declaration of incomplete type
10916
10917          elsif Ekind (Prev) = E_Incomplete_Type then
10918
10919             --  Indicate that the incomplete declaration has a matching full
10920             --  declaration. The defining occurrence of the incomplete
10921             --  declaration remains the visible one, and the procedure
10922             --  Get_Full_View dereferences it whenever the type is used.
10923
10924             if Present (Full_View (Prev)) then
10925                Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
10926             end if;
10927
10928             Set_Full_View (Prev,  Id);
10929             Append_Entity (Id, Current_Scope);
10930             Set_Is_Public (Id, Is_Public (Prev));
10931             Set_Is_Internal (Id);
10932             New_Id := Prev;
10933
10934          --  Case of full declaration of private type
10935
10936          else
10937             if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
10938                if Etype (Prev) /= Prev then
10939
10940                   --  Prev is a private subtype or a derived type, and needs
10941                   --  no completion.
10942
10943                   Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
10944                   New_Id := Id;
10945
10946                elsif Ekind (Prev) = E_Private_Type
10947                  and then
10948                    (Nkind (N) = N_Task_Type_Declaration
10949                      or else Nkind (N) = N_Protected_Type_Declaration)
10950                then
10951                   Error_Msg_N
10952                    ("completion of nonlimited type cannot be limited", N);
10953                end if;
10954
10955             --  Ada 2005 (AI-251): Private extension declaration of a
10956             --  task type. This case arises with tasks implementing interfaces
10957
10958             elsif Nkind (N) = N_Task_Type_Declaration
10959               or else Nkind (N) = N_Protected_Type_Declaration
10960             then
10961                null;
10962
10963             elsif Nkind (N) /= N_Full_Type_Declaration
10964               or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
10965             then
10966                Error_Msg_N
10967                  ("full view of private extension must be an extension", N);
10968
10969             elsif not (Abstract_Present (Parent (Prev)))
10970               and then Abstract_Present (Type_Definition (N))
10971             then
10972                Error_Msg_N
10973                  ("full view of non-abstract extension cannot be abstract", N);
10974             end if;
10975
10976             if not In_Private_Part (Current_Scope) then
10977                Error_Msg_N
10978                  ("declaration of full view must appear in private part", N);
10979             end if;
10980
10981             Copy_And_Swap (Prev, Id);
10982             Set_Has_Private_Declaration (Prev);
10983             Set_Has_Private_Declaration (Id);
10984
10985             --  If no error, propagate freeze_node from private to full view.
10986             --  It may have been generated for an early operational item.
10987
10988             if Present (Freeze_Node (Id))
10989               and then Serious_Errors_Detected = 0
10990               and then No (Full_View (Id))
10991             then
10992                Set_Freeze_Node (Prev, Freeze_Node (Id));
10993                Set_Freeze_Node (Id, Empty);
10994                Set_First_Rep_Item (Prev, First_Rep_Item (Id));
10995             end if;
10996
10997             Set_Full_View (Id, Prev);
10998             New_Id := Prev;
10999          end if;
11000
11001          --  Verify that full declaration conforms to incomplete one
11002
11003          if Is_Incomplete_Or_Private_Type (Prev)
11004            and then Present (Discriminant_Specifications (Prev_Par))
11005          then
11006             if Present (Discriminant_Specifications (N)) then
11007                if Ekind (Prev) = E_Incomplete_Type then
11008                   Check_Discriminant_Conformance (N, Prev, Prev);
11009                else
11010                   Check_Discriminant_Conformance (N, Prev, Id);
11011                end if;
11012
11013             else
11014                Error_Msg_N
11015                  ("missing discriminants in full type declaration", N);
11016
11017                --  To avoid cascaded errors on subsequent use, share the
11018                --  discriminants of the partial view.
11019
11020                Set_Discriminant_Specifications (N,
11021                  Discriminant_Specifications (Prev_Par));
11022             end if;
11023          end if;
11024
11025          --  A prior untagged private type can have an associated class-wide
11026          --  type due to use of the class attribute, and in this case also the
11027          --  full type is required to be tagged.
11028
11029          if Is_Type (Prev)
11030            and then (Is_Tagged_Type (Prev)
11031                       or else Present (Class_Wide_Type (Prev)))
11032            and then (Nkind (N) /= N_Task_Type_Declaration
11033                       and then Nkind (N) /= N_Protected_Type_Declaration)
11034          then
11035             --  The full declaration is either a tagged record or an
11036             --  extension otherwise this is an error
11037
11038             if Nkind (Type_Definition (N)) = N_Record_Definition then
11039                if not Tagged_Present (Type_Definition (N)) then
11040                   Error_Msg_NE
11041                     ("full declaration of } must be tagged", Prev, Id);
11042                   Set_Is_Tagged_Type (Id);
11043                   Set_Primitive_Operations (Id, New_Elmt_List);
11044                end if;
11045
11046             elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
11047                if No (Record_Extension_Part (Type_Definition (N))) then
11048                   Error_Msg_NE (
11049                     "full declaration of } must be a record extension",
11050                     Prev, Id);
11051                   Set_Is_Tagged_Type (Id);
11052                   Set_Primitive_Operations (Id, New_Elmt_List);
11053                end if;
11054
11055             else
11056                Error_Msg_NE
11057                  ("full declaration of } must be a tagged type", Prev, Id);
11058
11059             end if;
11060          end if;
11061
11062          return New_Id;
11063
11064       else
11065          --  New type declaration
11066
11067          Enter_Name (Id);
11068          return Id;
11069       end if;
11070    end Find_Type_Name;
11071
11072    -------------------------
11073    -- Find_Type_Of_Object --
11074    -------------------------
11075
11076    function Find_Type_Of_Object
11077      (Obj_Def     : Node_Id;
11078       Related_Nod : Node_Id) return Entity_Id
11079    is
11080       Def_Kind : constant Node_Kind := Nkind (Obj_Def);
11081       P        : Node_Id := Parent (Obj_Def);
11082       T        : Entity_Id;
11083       Nam      : Name_Id;
11084
11085    begin
11086       --  If the parent is a component_definition node we climb to the
11087       --  component_declaration node
11088
11089       if Nkind (P) = N_Component_Definition then
11090          P := Parent (P);
11091       end if;
11092
11093       --  Case of an anonymous array subtype
11094
11095       if Def_Kind = N_Constrained_Array_Definition
11096         or else Def_Kind = N_Unconstrained_Array_Definition
11097       then
11098          T := Empty;
11099          Array_Type_Declaration (T, Obj_Def);
11100
11101       --  Create an explicit subtype whenever possible
11102
11103       elsif Nkind (P) /= N_Component_Declaration
11104         and then Def_Kind = N_Subtype_Indication
11105       then
11106          --  Base name of subtype on object name, which will be unique in
11107          --  the current scope.
11108
11109          --  If this is a duplicate declaration, return base type, to avoid
11110          --  generating duplicate anonymous types.
11111
11112          if Error_Posted (P) then
11113             Analyze (Subtype_Mark (Obj_Def));
11114             return Entity (Subtype_Mark (Obj_Def));
11115          end if;
11116
11117          Nam :=
11118             New_External_Name
11119              (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
11120
11121          T := Make_Defining_Identifier (Sloc (P), Nam);
11122
11123          Insert_Action (Obj_Def,
11124            Make_Subtype_Declaration (Sloc (P),
11125              Defining_Identifier => T,
11126              Subtype_Indication  => Relocate_Node (Obj_Def)));
11127
11128          --  This subtype may need freezing, and this will not be done
11129          --  automatically if the object declaration is not in declarative
11130          --  part. Since this is an object declaration, the type cannot always
11131          --  be frozen here. Deferred constants do not freeze their type
11132          --  (which often enough will be private).
11133
11134          if Nkind (P) = N_Object_Declaration
11135            and then Constant_Present (P)
11136            and then No (Expression (P))
11137          then
11138             null;
11139          else
11140             Insert_Actions (Obj_Def, Freeze_Entity (T, Sloc (P)));
11141          end if;
11142
11143       --  Ada 2005 AI-406: the object definition in an object declaration
11144       --  can be an access definition.
11145
11146       elsif Def_Kind = N_Access_Definition then
11147          T := Access_Definition (Related_Nod, Obj_Def);
11148          Set_Is_Local_Anonymous_Access (T);
11149
11150       --  comment here, what cases ???
11151
11152       else
11153          T := Process_Subtype (Obj_Def, Related_Nod);
11154       end if;
11155
11156       return T;
11157    end Find_Type_Of_Object;
11158
11159    --------------------------------
11160    -- Find_Type_Of_Subtype_Indic --
11161    --------------------------------
11162
11163    function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
11164       Typ : Entity_Id;
11165
11166    begin
11167       --  Case of subtype mark with a constraint
11168
11169       if Nkind (S) = N_Subtype_Indication then
11170          Find_Type (Subtype_Mark (S));
11171          Typ := Entity (Subtype_Mark (S));
11172
11173          if not
11174            Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
11175          then
11176             Error_Msg_N
11177               ("incorrect constraint for this kind of type", Constraint (S));
11178             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
11179          end if;
11180
11181       --  Otherwise we have a subtype mark without a constraint
11182
11183       elsif Error_Posted (S) then
11184          Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
11185          return Any_Type;
11186
11187       else
11188          Find_Type (S);
11189          Typ := Entity (S);
11190       end if;
11191
11192       if Typ = Standard_Wide_Character
11193         or else Typ = Standard_Wide_Wide_Character
11194         or else Typ = Standard_Wide_String
11195         or else Typ = Standard_Wide_Wide_String
11196       then
11197          Check_Restriction (No_Wide_Characters, S);
11198       end if;
11199
11200       return Typ;
11201    end Find_Type_Of_Subtype_Indic;
11202
11203    -------------------------------------
11204    -- Floating_Point_Type_Declaration --
11205    -------------------------------------
11206
11207    procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
11208       Digs          : constant Node_Id := Digits_Expression (Def);
11209       Digs_Val      : Uint;
11210       Base_Typ      : Entity_Id;
11211       Implicit_Base : Entity_Id;
11212       Bound         : Node_Id;
11213
11214       function Can_Derive_From (E : Entity_Id) return Boolean;
11215       --  Find if given digits value allows derivation from specified type
11216
11217       ---------------------
11218       -- Can_Derive_From --
11219       ---------------------
11220
11221       function Can_Derive_From (E : Entity_Id) return Boolean is
11222          Spec : constant Entity_Id := Real_Range_Specification (Def);
11223
11224       begin
11225          if Digs_Val > Digits_Value (E) then
11226             return False;
11227          end if;
11228
11229          if Present (Spec) then
11230             if Expr_Value_R (Type_Low_Bound (E)) >
11231                Expr_Value_R (Low_Bound (Spec))
11232             then
11233                return False;
11234             end if;
11235
11236             if Expr_Value_R (Type_High_Bound (E)) <
11237                Expr_Value_R (High_Bound (Spec))
11238             then
11239                return False;
11240             end if;
11241          end if;
11242
11243          return True;
11244       end Can_Derive_From;
11245
11246    --  Start of processing for Floating_Point_Type_Declaration
11247
11248    begin
11249       Check_Restriction (No_Floating_Point, Def);
11250
11251       --  Create an implicit base type
11252
11253       Implicit_Base :=
11254         Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
11255
11256       --  Analyze and verify digits value
11257
11258       Analyze_And_Resolve (Digs, Any_Integer);
11259       Check_Digits_Expression (Digs);
11260       Digs_Val := Expr_Value (Digs);
11261
11262       --  Process possible range spec and find correct type to derive from
11263
11264       Process_Real_Range_Specification (Def);
11265
11266       if Can_Derive_From (Standard_Short_Float) then
11267          Base_Typ := Standard_Short_Float;
11268       elsif Can_Derive_From (Standard_Float) then
11269          Base_Typ := Standard_Float;
11270       elsif Can_Derive_From (Standard_Long_Float) then
11271          Base_Typ := Standard_Long_Float;
11272       elsif Can_Derive_From (Standard_Long_Long_Float) then
11273          Base_Typ := Standard_Long_Long_Float;
11274
11275       --  If we can't derive from any existing type, use long_long_float
11276       --  and give appropriate message explaining the problem.
11277
11278       else
11279          Base_Typ := Standard_Long_Long_Float;
11280
11281          if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
11282             Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
11283             Error_Msg_N ("digits value out of range, maximum is ^", Digs);
11284
11285          else
11286             Error_Msg_N
11287               ("range too large for any predefined type",
11288                Real_Range_Specification (Def));
11289          end if;
11290       end if;
11291
11292       --  If there are bounds given in the declaration use them as the bounds
11293       --  of the type, otherwise use the bounds of the predefined base type
11294       --  that was chosen based on the Digits value.
11295
11296       if Present (Real_Range_Specification (Def)) then
11297          Set_Scalar_Range (T, Real_Range_Specification (Def));
11298          Set_Is_Constrained (T);
11299
11300          --  The bounds of this range must be converted to machine numbers
11301          --  in accordance with RM 4.9(38).
11302
11303          Bound := Type_Low_Bound (T);
11304
11305          if Nkind (Bound) = N_Real_Literal then
11306             Set_Realval
11307               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
11308             Set_Is_Machine_Number (Bound);
11309          end if;
11310
11311          Bound := Type_High_Bound (T);
11312
11313          if Nkind (Bound) = N_Real_Literal then
11314             Set_Realval
11315               (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
11316             Set_Is_Machine_Number (Bound);
11317          end if;
11318
11319       else
11320          Set_Scalar_Range (T, Scalar_Range (Base_Typ));
11321       end if;
11322
11323       --  Complete definition of implicit base and declared first subtype
11324
11325       Set_Etype          (Implicit_Base, Base_Typ);
11326
11327       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
11328       Set_Size_Info      (Implicit_Base,                (Base_Typ));
11329       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
11330       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
11331       Set_Digits_Value   (Implicit_Base, Digits_Value   (Base_Typ));
11332       Set_Vax_Float      (Implicit_Base, Vax_Float      (Base_Typ));
11333
11334       Set_Ekind          (T, E_Floating_Point_Subtype);
11335       Set_Etype          (T, Implicit_Base);
11336
11337       Set_Size_Info      (T,                (Implicit_Base));
11338       Set_RM_Size        (T, RM_Size        (Implicit_Base));
11339       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
11340       Set_Digits_Value   (T, Digs_Val);
11341    end Floating_Point_Type_Declaration;
11342
11343    ----------------------------
11344    -- Get_Discriminant_Value --
11345    ----------------------------
11346
11347    --  This is the situation:
11348
11349    --  There is a non-derived type
11350
11351    --       type T0 (Dx, Dy, Dz...)
11352
11353    --  There are zero or more levels of derivation, with each derivation
11354    --  either purely inheriting the discriminants, or defining its own.
11355
11356    --       type Ti      is new Ti-1
11357    --  or
11358    --       type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
11359    --  or
11360    --       subtype Ti is ...
11361
11362    --  The subtype issue is avoided by the use of Original_Record_Component,
11363    --  and the fact that derived subtypes also derive the constraints.
11364
11365    --  This chain leads back from
11366
11367    --       Typ_For_Constraint
11368
11369    --  Typ_For_Constraint has discriminants, and the value for each
11370    --  discriminant is given by its corresponding Elmt of Constraints.
11371
11372    --  Discriminant is some discriminant in this hierarchy
11373
11374    --  We need to return its value
11375
11376    --  We do this by recursively searching each level, and looking for
11377    --  Discriminant. Once we get to the bottom, we start backing up
11378    --  returning the value for it which may in turn be a discriminant
11379    --  further up, so on the backup we continue the substitution.
11380
11381    function Get_Discriminant_Value
11382      (Discriminant       : Entity_Id;
11383       Typ_For_Constraint : Entity_Id;
11384       Constraint         : Elist_Id) return Node_Id
11385    is
11386       function Search_Derivation_Levels
11387         (Ti                    : Entity_Id;
11388          Discrim_Values        : Elist_Id;
11389          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
11390       --  This is the routine that performs the recursive search of levels
11391       --  as described above.
11392
11393       ------------------------------
11394       -- Search_Derivation_Levels --
11395       ------------------------------
11396
11397       function Search_Derivation_Levels
11398         (Ti                    : Entity_Id;
11399          Discrim_Values        : Elist_Id;
11400          Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
11401       is
11402          Assoc          : Elmt_Id;
11403          Disc           : Entity_Id;
11404          Result         : Node_Or_Entity_Id;
11405          Result_Entity  : Node_Id;
11406
11407       begin
11408          --  If inappropriate type, return Error, this happens only in
11409          --  cascaded error situations, and we want to avoid a blow up.
11410
11411          if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
11412             return Error;
11413          end if;
11414
11415          --  Look deeper if possible. Use Stored_Constraints only for
11416          --  untagged types. For tagged types use the given constraint.
11417          --  This asymmetry needs explanation???
11418
11419          if not Stored_Discrim_Values
11420            and then Present (Stored_Constraint (Ti))
11421            and then not Is_Tagged_Type (Ti)
11422          then
11423             Result :=
11424               Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
11425          else
11426             declare
11427                Td : constant Entity_Id := Etype (Ti);
11428
11429             begin
11430                if Td = Ti then
11431                   Result := Discriminant;
11432
11433                else
11434                   if Present (Stored_Constraint (Ti)) then
11435                      Result :=
11436                         Search_Derivation_Levels
11437                           (Td, Stored_Constraint (Ti), True);
11438                   else
11439                      Result :=
11440                         Search_Derivation_Levels
11441                           (Td, Discrim_Values, Stored_Discrim_Values);
11442                   end if;
11443                end if;
11444             end;
11445          end if;
11446
11447          --  Extra underlying places to search, if not found above. For
11448          --  concurrent types, the relevant discriminant appears in the
11449          --  corresponding record. For a type derived from a private type
11450          --  without discriminant, the full view inherits the discriminants
11451          --  of the full view of the parent.
11452
11453          if Result = Discriminant then
11454             if Is_Concurrent_Type (Ti)
11455               and then Present (Corresponding_Record_Type (Ti))
11456             then
11457                Result :=
11458                  Search_Derivation_Levels (
11459                    Corresponding_Record_Type (Ti),
11460                    Discrim_Values,
11461                    Stored_Discrim_Values);
11462
11463             elsif Is_Private_Type (Ti)
11464               and then not Has_Discriminants (Ti)
11465               and then Present (Full_View (Ti))
11466               and then Etype (Full_View (Ti)) /= Ti
11467             then
11468                Result :=
11469                  Search_Derivation_Levels (
11470                    Full_View (Ti),
11471                    Discrim_Values,
11472                    Stored_Discrim_Values);
11473             end if;
11474          end if;
11475
11476          --  If Result is not a (reference to a) discriminant, return it,
11477          --  otherwise set Result_Entity to the discriminant.
11478
11479          if Nkind (Result) = N_Defining_Identifier then
11480             pragma Assert (Result = Discriminant);
11481             Result_Entity := Result;
11482
11483          else
11484             if not Denotes_Discriminant (Result) then
11485                return Result;
11486             end if;
11487
11488             Result_Entity := Entity (Result);
11489          end if;
11490
11491          --  See if this level of derivation actually has discriminants
11492          --  because tagged derivations can add them, hence the lower
11493          --  levels need not have any.
11494
11495          if not Has_Discriminants (Ti) then
11496             return Result;
11497          end if;
11498
11499          --  Scan Ti's discriminants for Result_Entity,
11500          --  and return its corresponding value, if any.
11501
11502          Result_Entity := Original_Record_Component (Result_Entity);
11503
11504          Assoc := First_Elmt (Discrim_Values);
11505
11506          if Stored_Discrim_Values then
11507             Disc := First_Stored_Discriminant (Ti);
11508          else
11509             Disc := First_Discriminant (Ti);
11510          end if;
11511
11512          while Present (Disc) loop
11513             pragma Assert (Present (Assoc));
11514
11515             if Original_Record_Component (Disc) = Result_Entity then
11516                return Node (Assoc);
11517             end if;
11518
11519             Next_Elmt (Assoc);
11520
11521             if Stored_Discrim_Values then
11522                Next_Stored_Discriminant (Disc);
11523             else
11524                Next_Discriminant (Disc);
11525             end if;
11526          end loop;
11527
11528          --  Could not find it
11529          --
11530          return Result;
11531       end Search_Derivation_Levels;
11532
11533       Result : Node_Or_Entity_Id;
11534
11535    --  Start of processing for Get_Discriminant_Value
11536
11537    begin
11538       --  ??? This routine is a gigantic mess and will be deleted. For the
11539       --  time being just test for the trivial case before calling recurse.
11540
11541       if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
11542          declare
11543             D : Entity_Id := First_Discriminant (Typ_For_Constraint);
11544             E : Elmt_Id   := First_Elmt (Constraint);
11545
11546          begin
11547             while Present (D) loop
11548                if Chars (D) = Chars (Discriminant) then
11549                   return Node (E);
11550                end if;
11551
11552                Next_Discriminant (D);
11553                Next_Elmt (E);
11554             end loop;
11555          end;
11556       end if;
11557
11558       Result := Search_Derivation_Levels
11559         (Typ_For_Constraint, Constraint, False);
11560
11561       --  ??? hack to disappear when this routine is gone
11562
11563       if  Nkind (Result) = N_Defining_Identifier then
11564          declare
11565             D : Entity_Id := First_Discriminant (Typ_For_Constraint);
11566             E : Elmt_Id   := First_Elmt (Constraint);
11567
11568          begin
11569             while Present (D) loop
11570                if Corresponding_Discriminant (D) = Discriminant then
11571                   return Node (E);
11572                end if;
11573
11574                Next_Discriminant (D);
11575                Next_Elmt (E);
11576             end loop;
11577          end;
11578       end if;
11579
11580       pragma Assert (Nkind (Result) /= N_Defining_Identifier);
11581       return Result;
11582    end Get_Discriminant_Value;
11583
11584    --------------------------
11585    -- Has_Range_Constraint --
11586    --------------------------
11587
11588    function Has_Range_Constraint (N : Node_Id) return Boolean is
11589       C : constant Node_Id := Constraint (N);
11590
11591    begin
11592       if Nkind (C) = N_Range_Constraint then
11593          return True;
11594
11595       elsif Nkind (C) = N_Digits_Constraint then
11596          return
11597             Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
11598               or else
11599             Present (Range_Constraint (C));
11600
11601       elsif Nkind (C) = N_Delta_Constraint then
11602          return Present (Range_Constraint (C));
11603
11604       else
11605          return False;
11606       end if;
11607    end Has_Range_Constraint;
11608
11609    ------------------------
11610    -- Inherit_Components --
11611    ------------------------
11612
11613    function Inherit_Components
11614      (N             : Node_Id;
11615       Parent_Base   : Entity_Id;
11616       Derived_Base  : Entity_Id;
11617       Is_Tagged     : Boolean;
11618       Inherit_Discr : Boolean;
11619       Discs         : Elist_Id) return Elist_Id
11620    is
11621       Assoc_List : constant Elist_Id := New_Elmt_List;
11622
11623       procedure Inherit_Component
11624         (Old_C          : Entity_Id;
11625          Plain_Discrim  : Boolean := False;
11626          Stored_Discrim : Boolean := False);
11627       --  Inherits component Old_C from Parent_Base to the Derived_Base. If
11628       --  Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
11629       --  True, Old_C is a stored discriminant. If they are both false then
11630       --  Old_C is a regular component.
11631
11632       -----------------------
11633       -- Inherit_Component --
11634       -----------------------
11635
11636       procedure Inherit_Component
11637         (Old_C          : Entity_Id;
11638          Plain_Discrim  : Boolean := False;
11639          Stored_Discrim : Boolean := False)
11640       is
11641          New_C : constant Entity_Id := New_Copy (Old_C);
11642
11643          Discrim      : Entity_Id;
11644          Corr_Discrim : Entity_Id;
11645
11646       begin
11647          pragma Assert (not Is_Tagged or else not Stored_Discrim);
11648
11649          Set_Parent (New_C, Parent (Old_C));
11650
11651          --  Regular discriminants and components must be inserted
11652          --  in the scope of the Derived_Base. Do it here.
11653
11654          if not Stored_Discrim then
11655             Enter_Name (New_C);
11656          end if;
11657
11658          --  For tagged types the Original_Record_Component must point to
11659          --  whatever this field was pointing to in the parent type. This has
11660          --  already been achieved by the call to New_Copy above.
11661
11662          if not Is_Tagged then
11663             Set_Original_Record_Component (New_C, New_C);
11664          end if;
11665
11666          --  If we have inherited a component then see if its Etype contains
11667          --  references to Parent_Base discriminants. In this case, replace
11668          --  these references with the constraints given in Discs. We do not
11669          --  do this for the partial view of private types because this is
11670          --  not needed (only the components of the full view will be used
11671          --  for code generation) and cause problem. We also avoid this
11672          --  transformation in some error situations.
11673
11674          if Ekind (New_C) = E_Component then
11675             if (Is_Private_Type (Derived_Base)
11676                   and then not Is_Generic_Type (Derived_Base))
11677               or else (Is_Empty_Elmt_List (Discs)
11678                          and then  not Expander_Active)
11679             then
11680                Set_Etype (New_C, Etype (Old_C));
11681             else
11682                Set_Etype
11683                  (New_C,
11684                   Constrain_Component_Type
11685                     (Old_C, Derived_Base, N, Parent_Base, Discs));
11686             end if;
11687          end if;
11688
11689          --  In derived tagged types it is illegal to reference a non
11690          --  discriminant component in the parent type. To catch this, mark
11691          --  these components with an Ekind of E_Void. This will be reset in
11692          --  Record_Type_Definition after processing the record extension of
11693          --  the derived type.
11694
11695          if Is_Tagged and then Ekind (New_C) = E_Component then
11696             Set_Ekind (New_C, E_Void);
11697          end if;
11698
11699          if Plain_Discrim then
11700             Set_Corresponding_Discriminant (New_C, Old_C);
11701             Build_Discriminal (New_C);
11702
11703          --  If we are explicitly inheriting a stored discriminant it will be
11704          --  completely hidden.
11705
11706          elsif Stored_Discrim then
11707             Set_Corresponding_Discriminant (New_C, Empty);
11708             Set_Discriminal (New_C, Empty);
11709             Set_Is_Completely_Hidden (New_C);
11710
11711             --  Set the Original_Record_Component of each discriminant in the
11712             --  derived base to point to the corresponding stored that we just
11713             --  created.
11714
11715             Discrim := First_Discriminant (Derived_Base);
11716             while Present (Discrim) loop
11717                Corr_Discrim := Corresponding_Discriminant (Discrim);
11718
11719                --  Corr_Discrimm could be missing in an error situation
11720
11721                if Present (Corr_Discrim)
11722                  and then Original_Record_Component (Corr_Discrim) = Old_C
11723                then
11724                   Set_Original_Record_Component (Discrim, New_C);
11725                end if;
11726
11727                Next_Discriminant (Discrim);
11728             end loop;
11729
11730             Append_Entity (New_C, Derived_Base);
11731          end if;
11732
11733          if not Is_Tagged then
11734             Append_Elmt (Old_C, Assoc_List);
11735             Append_Elmt (New_C, Assoc_List);
11736          end if;
11737       end Inherit_Component;
11738
11739       --  Variables local to Inherit_Component
11740
11741       Loc : constant Source_Ptr := Sloc (N);
11742
11743       Parent_Discrim : Entity_Id;
11744       Stored_Discrim : Entity_Id;
11745       D              : Entity_Id;
11746       Component      : Entity_Id;
11747
11748    --  Start of processing for Inherit_Components
11749
11750    begin
11751       if not Is_Tagged then
11752          Append_Elmt (Parent_Base,  Assoc_List);
11753          Append_Elmt (Derived_Base, Assoc_List);
11754       end if;
11755
11756       --  Inherit parent discriminants if needed
11757
11758       if Inherit_Discr then
11759          Parent_Discrim := First_Discriminant (Parent_Base);
11760          while Present (Parent_Discrim) loop
11761             Inherit_Component (Parent_Discrim, Plain_Discrim => True);
11762             Next_Discriminant (Parent_Discrim);
11763          end loop;
11764       end if;
11765
11766       --  Create explicit stored discrims for untagged types when necessary
11767
11768       if not Has_Unknown_Discriminants (Derived_Base)
11769         and then Has_Discriminants (Parent_Base)
11770         and then not Is_Tagged
11771         and then
11772           (not Inherit_Discr
11773              or else First_Discriminant (Parent_Base) /=
11774                      First_Stored_Discriminant (Parent_Base))
11775       then
11776          Stored_Discrim := First_Stored_Discriminant (Parent_Base);
11777          while Present (Stored_Discrim) loop
11778             Inherit_Component (Stored_Discrim, Stored_Discrim => True);
11779             Next_Stored_Discriminant (Stored_Discrim);
11780          end loop;
11781       end if;
11782
11783       --  See if we can apply the second transformation for derived types, as
11784       --  explained in point 6. in the comments above Build_Derived_Record_Type
11785       --  This is achieved by appending Derived_Base discriminants into Discs,
11786       --  which has the side effect of returning a non empty Discs list to the
11787       --  caller of Inherit_Components, which is what we want. This must be
11788       --  done for private derived types if there are explicit stored
11789       --  discriminants, to ensure that we can retrieve the values of the
11790       --  constraints provided in the ancestors.
11791
11792       if Inherit_Discr
11793         and then Is_Empty_Elmt_List (Discs)
11794         and then Present (First_Discriminant (Derived_Base))
11795         and then
11796           (not Is_Private_Type (Derived_Base)
11797              or else Is_Completely_Hidden
11798                (First_Stored_Discriminant (Derived_Base))
11799              or else Is_Generic_Type (Derived_Base))
11800       then
11801          D := First_Discriminant (Derived_Base);
11802          while Present (D) loop
11803             Append_Elmt (New_Reference_To (D, Loc), Discs);
11804             Next_Discriminant (D);
11805          end loop;
11806       end if;
11807
11808       --  Finally, inherit non-discriminant components unless they are not
11809       --  visible because defined or inherited from the full view of the
11810       --  parent. Don't inherit the _parent field of the parent type.
11811
11812       Component := First_Entity (Parent_Base);
11813       while Present (Component) loop
11814
11815          --  Ada 2005 (AI-251): Do not inherit tags corresponding with the
11816          --  interfaces of the parent
11817
11818          if Ekind (Component) = E_Component
11819            and then Is_Tag (Component)
11820            and then Etype  (Component) = RTE (RE_Interface_Tag)
11821          then
11822             null;
11823
11824          elsif Ekind (Component) /= E_Component
11825            or else Chars (Component) = Name_uParent
11826          then
11827             null;
11828
11829          --  If the derived type is within the parent type's declarative
11830          --  region, then the components can still be inherited even though
11831          --  they aren't visible at this point. This can occur for cases
11832          --  such as within public child units where the components must
11833          --  become visible upon entering the child unit's private part.
11834
11835          elsif not Is_Visible_Component (Component)
11836            and then not In_Open_Scopes (Scope (Parent_Base))
11837          then
11838             null;
11839
11840          elsif Ekind (Derived_Base) = E_Private_Type
11841            or else Ekind (Derived_Base) = E_Limited_Private_Type
11842          then
11843             null;
11844
11845          else
11846             Inherit_Component (Component);
11847          end if;
11848
11849          Next_Entity (Component);
11850       end loop;
11851
11852       --  For tagged derived types, inherited discriminants cannot be used in
11853       --  component declarations of the record extension part. To achieve this
11854       --  we mark the inherited discriminants as not visible.
11855
11856       if Is_Tagged and then Inherit_Discr then
11857          D := First_Discriminant (Derived_Base);
11858          while Present (D) loop
11859             Set_Is_Immediately_Visible (D, False);
11860             Next_Discriminant (D);
11861          end loop;
11862       end if;
11863
11864       return Assoc_List;
11865    end Inherit_Components;
11866
11867    ------------------------------
11868    -- Is_Valid_Constraint_Kind --
11869    ------------------------------
11870
11871    function Is_Valid_Constraint_Kind
11872      (T_Kind          : Type_Kind;
11873       Constraint_Kind : Node_Kind) return Boolean
11874    is
11875    begin
11876       case T_Kind is
11877          when Enumeration_Kind |
11878               Integer_Kind =>
11879             return Constraint_Kind = N_Range_Constraint;
11880
11881          when Decimal_Fixed_Point_Kind =>
11882             return
11883               Constraint_Kind = N_Digits_Constraint
11884                 or else
11885               Constraint_Kind = N_Range_Constraint;
11886
11887          when Ordinary_Fixed_Point_Kind =>
11888             return
11889               Constraint_Kind = N_Delta_Constraint
11890                 or else
11891               Constraint_Kind = N_Range_Constraint;
11892
11893          when Float_Kind =>
11894             return
11895               Constraint_Kind = N_Digits_Constraint
11896                 or else
11897               Constraint_Kind = N_Range_Constraint;
11898
11899          when Access_Kind       |
11900               Array_Kind        |
11901               E_Record_Type     |
11902               E_Record_Subtype  |
11903               Class_Wide_Kind   |
11904               E_Incomplete_Type |
11905               Private_Kind      |
11906               Concurrent_Kind  =>
11907             return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
11908
11909          when others =>
11910             return True; -- Error will be detected later
11911       end case;
11912    end Is_Valid_Constraint_Kind;
11913
11914    --------------------------
11915    -- Is_Visible_Component --
11916    --------------------------
11917
11918    function Is_Visible_Component (C : Entity_Id) return Boolean is
11919       Original_Comp  : Entity_Id := Empty;
11920       Original_Scope : Entity_Id;
11921       Type_Scope     : Entity_Id;
11922
11923       function Is_Local_Type (Typ : Entity_Id) return Boolean;
11924       --  Check whether parent type of inherited component is declared locally,
11925       --  possibly within a nested package or instance. The current scope is
11926       --  the derived record itself.
11927
11928       -------------------
11929       -- Is_Local_Type --
11930       -------------------
11931
11932       function Is_Local_Type (Typ : Entity_Id) return Boolean is
11933          Scop : Entity_Id := Scope (Typ);
11934
11935       begin
11936          while Present (Scop)
11937            and then Scop /= Standard_Standard
11938          loop
11939             if Scop = Scope (Current_Scope) then
11940                return True;
11941             end if;
11942
11943             Scop := Scope (Scop);
11944          end loop;
11945
11946          return False;
11947       end Is_Local_Type;
11948
11949    --  Start of processing for Is_Visible_Component
11950
11951    begin
11952       if Ekind (C) = E_Component
11953         or else Ekind (C) = E_Discriminant
11954       then
11955          Original_Comp := Original_Record_Component (C);
11956       end if;
11957
11958       if No (Original_Comp) then
11959
11960          --  Premature usage, or previous error
11961
11962          return False;
11963
11964       else
11965          Original_Scope := Scope (Original_Comp);
11966          Type_Scope     := Scope (Base_Type (Scope (C)));
11967       end if;
11968
11969       --  This test only concerns tagged types
11970
11971       if not Is_Tagged_Type (Original_Scope) then
11972          return True;
11973
11974       --  If it is _Parent or _Tag, there is no visibility issue
11975
11976       elsif not Comes_From_Source (Original_Comp) then
11977          return True;
11978
11979       --  If we are in the body of an instantiation, the component is visible
11980       --  even when the parent type (possibly defined in an enclosing unit or
11981       --  in a parent unit) might not.
11982
11983       elsif In_Instance_Body then
11984          return True;
11985
11986       --  Discriminants are always visible
11987
11988       elsif Ekind (Original_Comp) = E_Discriminant
11989         and then not Has_Unknown_Discriminants (Original_Scope)
11990       then
11991          return True;
11992
11993       --  If the component has been declared in an ancestor which is currently
11994       --  a private type, then it is not visible. The same applies if the
11995       --  component's containing type is not in an open scope and the original
11996       --  component's enclosing type is a visible full type of a private type
11997       --  (which can occur in cases where an attempt is being made to reference
11998       --  a component in a sibling package that is inherited from a visible
11999       --  component of a type in an ancestor package; the component in the
12000       --  sibling package should not be visible even though the component it
12001       --  inherited from is visible). This does not apply however in the case
12002       --  where the scope of the type is a private child unit, or when the
12003       --  parent comes from a local package in which the ancestor is currently
12004       --  visible. The latter suppression of visibility is needed for cases
12005       --  that are tested in B730006.
12006
12007       elsif Is_Private_Type (Original_Scope)
12008         or else
12009           (not Is_Private_Descendant (Type_Scope)
12010             and then not In_Open_Scopes (Type_Scope)
12011             and then Has_Private_Declaration (Original_Scope))
12012       then
12013          --  If the type derives from an entity in a formal package, there
12014          --  are no additional visible components.
12015
12016          if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
12017             N_Formal_Package_Declaration
12018          then
12019             return False;
12020
12021          --  if we are not in the private part of the current package, there
12022          --  are no additional visible components.
12023
12024          elsif Ekind (Scope (Current_Scope)) = E_Package
12025            and then not In_Private_Part (Scope (Current_Scope))
12026          then
12027             return False;
12028          else
12029             return
12030               Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
12031                 and then Is_Local_Type (Type_Scope);
12032          end if;
12033
12034       --  There is another weird way in which a component may be invisible
12035       --  when the private and the full view are not derived from the same
12036       --  ancestor. Here is an example :
12037
12038       --       type A1 is tagged      record F1 : integer; end record;
12039       --       type A2 is new A1 with record F2 : integer; end record;
12040       --       type T is new A1 with private;
12041       --     private
12042       --       type T is new A2 with null record;
12043
12044       --  In this case, the full view of T inherits F1 and F2 but the private
12045       --  view inherits only F1
12046
12047       else
12048          declare
12049             Ancestor : Entity_Id := Scope (C);
12050
12051          begin
12052             loop
12053                if Ancestor = Original_Scope then
12054                   return True;
12055                elsif Ancestor = Etype (Ancestor) then
12056                   return False;
12057                end if;
12058
12059                Ancestor := Etype (Ancestor);
12060             end loop;
12061
12062             return True;
12063          end;
12064       end if;
12065    end Is_Visible_Component;
12066
12067    --------------------------
12068    -- Make_Class_Wide_Type --
12069    --------------------------
12070
12071    procedure Make_Class_Wide_Type (T : Entity_Id) is
12072       CW_Type : Entity_Id;
12073       CW_Name : Name_Id;
12074       Next_E  : Entity_Id;
12075
12076    begin
12077       --  The class wide type can have been defined by the partial view in
12078       --  which case everything is already done
12079
12080       if Present (Class_Wide_Type (T)) then
12081          return;
12082       end if;
12083
12084       CW_Type :=
12085         New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
12086
12087       --  Inherit root type characteristics
12088
12089       CW_Name := Chars (CW_Type);
12090       Next_E  := Next_Entity (CW_Type);
12091       Copy_Node (T, CW_Type);
12092       Set_Comes_From_Source (CW_Type, False);
12093       Set_Chars (CW_Type, CW_Name);
12094       Set_Parent (CW_Type, Parent (T));
12095       Set_Next_Entity (CW_Type, Next_E);
12096       Set_Has_Delayed_Freeze (CW_Type);
12097
12098       --  Customize the class-wide type: It has no prim. op., it cannot be
12099       --  abstract and its Etype points back to the specific root type.
12100
12101       Set_Ekind                (CW_Type, E_Class_Wide_Type);
12102       Set_Is_Tagged_Type       (CW_Type, True);
12103       Set_Primitive_Operations (CW_Type, New_Elmt_List);
12104       Set_Is_Abstract          (CW_Type, False);
12105       Set_Is_Constrained       (CW_Type, False);
12106       Set_Is_First_Subtype     (CW_Type, Is_First_Subtype (T));
12107       Init_Size_Align          (CW_Type);
12108
12109       if Ekind (T) = E_Class_Wide_Subtype then
12110          Set_Etype             (CW_Type, Etype (Base_Type (T)));
12111       else
12112          Set_Etype             (CW_Type, T);
12113       end if;
12114
12115       --  If this is the class_wide type of a constrained subtype, it does
12116       --  not have discriminants.
12117
12118       Set_Has_Discriminants (CW_Type,
12119         Has_Discriminants (T) and then not Is_Constrained (T));
12120
12121       Set_Has_Unknown_Discriminants (CW_Type, True);
12122       Set_Class_Wide_Type (T, CW_Type);
12123       Set_Equivalent_Type (CW_Type, Empty);
12124
12125       --  The class-wide type of a class-wide type is itself (RM 3.9(14))
12126
12127       Set_Class_Wide_Type (CW_Type, CW_Type);
12128    end Make_Class_Wide_Type;
12129
12130    ----------------
12131    -- Make_Index --
12132    ----------------
12133
12134    procedure Make_Index
12135      (I            : Node_Id;
12136       Related_Nod  : Node_Id;
12137       Related_Id   : Entity_Id := Empty;
12138       Suffix_Index : Nat := 1)
12139    is
12140       R      : Node_Id;
12141       T      : Entity_Id;
12142       Def_Id : Entity_Id := Empty;
12143       Found  : Boolean := False;
12144
12145    begin
12146       --  For a discrete range used in a constrained array definition and
12147       --  defined by a range, an implicit conversion to the predefined type
12148       --  INTEGER is assumed if each bound is either a numeric literal, a named
12149       --  number, or an attribute, and the type of both bounds (prior to the
12150       --  implicit conversion) is the type universal_integer. Otherwise, both
12151       --  bounds must be of the same discrete type, other than universal
12152       --  integer; this type must be determinable independently of the
12153       --  context, but using the fact that the type must be discrete and that
12154       --  both bounds must have the same type.
12155
12156       --  Character literals also have a universal type in the absence of
12157       --  of additional context,  and are resolved to Standard_Character.
12158
12159       if Nkind (I) = N_Range then
12160
12161          --  The index is given by a range constraint. The bounds are known
12162          --  to be of a consistent type.
12163
12164          if not Is_Overloaded (I) then
12165             T := Etype (I);
12166
12167             --  If the bounds are universal, choose the specific predefined
12168             --  type.
12169
12170             if T = Universal_Integer then
12171                T := Standard_Integer;
12172
12173             elsif T = Any_Character then
12174
12175                if Ada_Version >= Ada_95 then
12176                   Error_Msg_N
12177                     ("ambiguous character literals (could be Wide_Character)",
12178                       I);
12179                end if;
12180
12181                T := Standard_Character;
12182             end if;
12183
12184          else
12185             T := Any_Type;
12186
12187             declare
12188                Ind : Interp_Index;
12189                It  : Interp;
12190
12191             begin
12192                Get_First_Interp (I, Ind, It);
12193
12194                while Present (It.Typ) loop
12195                   if Is_Discrete_Type (It.Typ) then
12196
12197                      if Found
12198                        and then not Covers (It.Typ, T)
12199                        and then not Covers (T, It.Typ)
12200                      then
12201                         Error_Msg_N ("ambiguous bounds in discrete range", I);
12202                         exit;
12203                      else
12204                         T := It.Typ;
12205                         Found := True;
12206                      end if;
12207                   end if;
12208
12209                   Get_Next_Interp (Ind, It);
12210                end loop;
12211
12212                if T = Any_Type then
12213                   Error_Msg_N ("discrete type required for range", I);
12214                   Set_Etype (I, Any_Type);
12215                   return;
12216
12217                elsif T = Universal_Integer then
12218                   T := Standard_Integer;
12219                end if;
12220             end;
12221          end if;
12222
12223          if not Is_Discrete_Type (T) then
12224             Error_Msg_N ("discrete type required for range", I);
12225             Set_Etype (I, Any_Type);
12226             return;
12227          end if;
12228
12229          if Nkind (Low_Bound (I)) = N_Attribute_Reference
12230            and then Attribute_Name (Low_Bound (I)) = Name_First
12231            and then Is_Entity_Name (Prefix (Low_Bound (I)))
12232            and then Is_Type (Entity (Prefix (Low_Bound (I))))
12233            and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
12234          then
12235             --  The type of the index will be the type of the prefix, as long
12236             --  as the upper bound is 'Last of the same type.
12237
12238             Def_Id := Entity (Prefix (Low_Bound (I)));
12239
12240             if Nkind (High_Bound (I)) /= N_Attribute_Reference
12241               or else Attribute_Name (High_Bound (I)) /= Name_Last
12242               or else not Is_Entity_Name (Prefix (High_Bound (I)))
12243               or else Entity (Prefix (High_Bound (I))) /= Def_Id
12244             then
12245                Def_Id := Empty;
12246             end if;
12247          end if;
12248
12249          R := I;
12250          Process_Range_Expr_In_Decl (R, T);
12251
12252       elsif Nkind (I) = N_Subtype_Indication then
12253
12254          --  The index is given by a subtype with a range constraint
12255
12256          T :=  Base_Type (Entity (Subtype_Mark (I)));
12257
12258          if not Is_Discrete_Type (T) then
12259             Error_Msg_N ("discrete type required for range", I);
12260             Set_Etype (I, Any_Type);
12261             return;
12262          end if;
12263
12264          R := Range_Expression (Constraint (I));
12265
12266          Resolve (R, T);
12267          Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
12268
12269       elsif Nkind (I) = N_Attribute_Reference then
12270
12271          --  The parser guarantees that the attribute is a RANGE attribute
12272
12273          --  If the node denotes the range of a type mark, that is also the
12274          --  resulting type, and we do no need to create an Itype for it.
12275
12276          if Is_Entity_Name (Prefix (I))
12277            and then Comes_From_Source (I)
12278            and then Is_Type (Entity (Prefix (I)))
12279            and then Is_Discrete_Type (Entity (Prefix (I)))
12280          then
12281             Def_Id := Entity (Prefix (I));
12282          end if;
12283
12284          Analyze_And_Resolve (I);
12285          T := Etype (I);
12286          R := I;
12287
12288       --  If none of the above, must be a subtype. We convert this to a
12289       --  range attribute reference because in the case of declared first
12290       --  named subtypes, the types in the range reference can be different
12291       --  from the type of the entity. A range attribute normalizes the
12292       --  reference and obtains the correct types for the bounds.
12293
12294       --  This transformation is in the nature of an expansion, is only
12295       --  done if expansion is active. In particular, it is not done on
12296       --  formal generic types,  because we need to retain the name of the
12297       --  original index for instantiation purposes.
12298
12299       else
12300          if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
12301             Error_Msg_N ("invalid subtype mark in discrete range ", I);
12302             Set_Etype (I, Any_Integer);
12303             return;
12304
12305          else
12306             --  The type mark may be that of an incomplete type. It is only
12307             --  now that we can get the full view, previous analysis does
12308             --  not look specifically for a type mark.
12309
12310             Set_Entity (I, Get_Full_View (Entity (I)));
12311             Set_Etype  (I, Entity (I));
12312             Def_Id := Entity (I);
12313
12314             if not Is_Discrete_Type (Def_Id) then
12315                Error_Msg_N ("discrete type required for index", I);
12316                Set_Etype (I, Any_Type);
12317                return;
12318             end if;
12319          end if;
12320
12321          if Expander_Active then
12322             Rewrite (I,
12323               Make_Attribute_Reference (Sloc (I),
12324                 Attribute_Name => Name_Range,
12325                 Prefix         => Relocate_Node (I)));
12326
12327             --  The original was a subtype mark that does not freeze. This
12328             --  means that the rewritten version must not freeze either.
12329
12330             Set_Must_Not_Freeze (I);
12331             Set_Must_Not_Freeze (Prefix (I));
12332
12333             --  Is order critical??? if so, document why, if not
12334             --  use Analyze_And_Resolve
12335
12336             Analyze (I);
12337             T := Etype (I);
12338             Resolve (I);
12339             R := I;
12340
12341          --  If expander is inactive, type is legal, nothing else to construct
12342
12343          else
12344             return;
12345          end if;
12346       end if;
12347
12348       if not Is_Discrete_Type (T) then
12349          Error_Msg_N ("discrete type required for range", I);
12350          Set_Etype (I, Any_Type);
12351          return;
12352
12353       elsif T = Any_Type then
12354          Set_Etype (I, Any_Type);
12355          return;
12356       end if;
12357
12358       --  We will now create the appropriate Itype to describe the range, but
12359       --  first a check. If we originally had a subtype, then we just label
12360       --  the range with this subtype. Not only is there no need to construct
12361       --  a new subtype, but it is wrong to do so for two reasons:
12362
12363       --    1. A legality concern, if we have a subtype, it must not freeze,
12364       --       and the Itype would cause freezing incorrectly
12365
12366       --    2. An efficiency concern, if we created an Itype, it would not be
12367       --       recognized as the same type for the purposes of eliminating
12368       --       checks in some circumstances.
12369
12370       --  We signal this case by setting the subtype entity in Def_Id
12371
12372       if No (Def_Id) then
12373          Def_Id :=
12374            Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
12375          Set_Etype (Def_Id, Base_Type (T));
12376
12377          if Is_Signed_Integer_Type (T) then
12378             Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
12379
12380          elsif Is_Modular_Integer_Type (T) then
12381             Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
12382
12383          else
12384             Set_Ekind             (Def_Id, E_Enumeration_Subtype);
12385             Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
12386             Set_First_Literal     (Def_Id, First_Literal (T));
12387          end if;
12388
12389          Set_Size_Info      (Def_Id,                  (T));
12390          Set_RM_Size        (Def_Id, RM_Size          (T));
12391          Set_First_Rep_Item (Def_Id, First_Rep_Item   (T));
12392
12393          Set_Scalar_Range   (Def_Id, R);
12394          Conditional_Delay  (Def_Id, T);
12395
12396          --  In the subtype indication case, if the immediate parent of the
12397          --  new subtype is non-static, then the subtype we create is non-
12398          --  static, even if its bounds are static.
12399
12400          if Nkind (I) = N_Subtype_Indication
12401            and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
12402          then
12403             Set_Is_Non_Static_Subtype (Def_Id);
12404          end if;
12405       end if;
12406
12407       --  Final step is to label the index with this constructed type
12408
12409       Set_Etype (I, Def_Id);
12410    end Make_Index;
12411
12412    ------------------------------
12413    -- Modular_Type_Declaration --
12414    ------------------------------
12415
12416    procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
12417       Mod_Expr : constant Node_Id := Expression (Def);
12418       M_Val    : Uint;
12419
12420       procedure Set_Modular_Size (Bits : Int);
12421       --  Sets RM_Size to Bits, and Esize to normal word size above this
12422
12423       ----------------------
12424       -- Set_Modular_Size --
12425       ----------------------
12426
12427       procedure Set_Modular_Size (Bits : Int) is
12428       begin
12429          Set_RM_Size (T, UI_From_Int (Bits));
12430
12431          if Bits <= 8 then
12432             Init_Esize (T, 8);
12433
12434          elsif Bits <= 16 then
12435             Init_Esize (T, 16);
12436
12437          elsif Bits <= 32 then
12438             Init_Esize (T, 32);
12439
12440          else
12441             Init_Esize (T, System_Max_Binary_Modulus_Power);
12442          end if;
12443       end Set_Modular_Size;
12444
12445    --  Start of processing for Modular_Type_Declaration
12446
12447    begin
12448       Analyze_And_Resolve (Mod_Expr, Any_Integer);
12449       Set_Etype (T, T);
12450       Set_Ekind (T, E_Modular_Integer_Type);
12451       Init_Alignment (T);
12452       Set_Is_Constrained (T);
12453
12454       if not Is_OK_Static_Expression (Mod_Expr) then
12455          Flag_Non_Static_Expr
12456            ("non-static expression used for modular type bound!", Mod_Expr);
12457          M_Val := 2 ** System_Max_Binary_Modulus_Power;
12458       else
12459          M_Val := Expr_Value (Mod_Expr);
12460       end if;
12461
12462       if M_Val < 1 then
12463          Error_Msg_N ("modulus value must be positive", Mod_Expr);
12464          M_Val := 2 ** System_Max_Binary_Modulus_Power;
12465       end if;
12466
12467       Set_Modulus (T, M_Val);
12468
12469       --   Create bounds for the modular type based on the modulus given in
12470       --   the type declaration and then analyze and resolve those bounds.
12471
12472       Set_Scalar_Range (T,
12473         Make_Range (Sloc (Mod_Expr),
12474           Low_Bound  =>
12475             Make_Integer_Literal (Sloc (Mod_Expr), 0),
12476           High_Bound =>
12477             Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
12478
12479       --  Properly analyze the literals for the range. We do this manually
12480       --  because we can't go calling Resolve, since we are resolving these
12481       --  bounds with the type, and this type is certainly not complete yet!
12482
12483       Set_Etype (Low_Bound  (Scalar_Range (T)), T);
12484       Set_Etype (High_Bound (Scalar_Range (T)), T);
12485       Set_Is_Static_Expression (Low_Bound  (Scalar_Range (T)));
12486       Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
12487
12488       --  Loop through powers of two to find number of bits required
12489
12490       for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
12491
12492          --  Binary case
12493
12494          if M_Val = 2 ** Bits then
12495             Set_Modular_Size (Bits);
12496             return;
12497
12498          --  Non-binary case
12499
12500          elsif M_Val < 2 ** Bits then
12501             Set_Non_Binary_Modulus (T);
12502
12503             if Bits > System_Max_Nonbinary_Modulus_Power then
12504                Error_Msg_Uint_1 :=
12505                  UI_From_Int (System_Max_Nonbinary_Modulus_Power);
12506                Error_Msg_N
12507                  ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
12508                Set_Modular_Size (System_Max_Binary_Modulus_Power);
12509                return;
12510
12511             else
12512                --  In the non-binary case, set size as per RM 13.3(55)
12513
12514                Set_Modular_Size (Bits);
12515                return;
12516             end if;
12517          end if;
12518
12519       end loop;
12520
12521       --  If we fall through, then the size exceed System.Max_Binary_Modulus
12522       --  so we just signal an error and set the maximum size.
12523
12524       Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
12525       Error_Msg_N ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
12526
12527       Set_Modular_Size (System_Max_Binary_Modulus_Power);
12528       Init_Alignment (T);
12529
12530    end Modular_Type_Declaration;
12531
12532    --------------------------
12533    -- New_Concatenation_Op --
12534    --------------------------
12535
12536    procedure New_Concatenation_Op (Typ : Entity_Id) is
12537       Loc : constant Source_Ptr := Sloc (Typ);
12538       Op  : Entity_Id;
12539
12540       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
12541       --  Create abbreviated declaration for the formal of a predefined
12542       --  Operator 'Op' of type 'Typ'
12543
12544       --------------------
12545       -- Make_Op_Formal --
12546       --------------------
12547
12548       function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
12549          Formal : Entity_Id;
12550       begin
12551          Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
12552          Set_Etype (Formal, Typ);
12553          Set_Mechanism (Formal, Default_Mechanism);
12554          return Formal;
12555       end Make_Op_Formal;
12556
12557    --  Start of processing for New_Concatenation_Op
12558
12559    begin
12560       Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
12561
12562       Set_Ekind                   (Op, E_Operator);
12563       Set_Scope                   (Op, Current_Scope);
12564       Set_Etype                   (Op, Typ);
12565       Set_Homonym                 (Op, Get_Name_Entity_Id (Name_Op_Concat));
12566       Set_Is_Immediately_Visible  (Op);
12567       Set_Is_Intrinsic_Subprogram (Op);
12568       Set_Has_Completion          (Op);
12569       Append_Entity               (Op, Current_Scope);
12570
12571       Set_Name_Entity_Id (Name_Op_Concat, Op);
12572
12573       Append_Entity (Make_Op_Formal (Typ, Op), Op);
12574       Append_Entity (Make_Op_Formal (Typ, Op), Op);
12575    end New_Concatenation_Op;
12576
12577    -------------------------------------------
12578    -- Ordinary_Fixed_Point_Type_Declaration --
12579    -------------------------------------------
12580
12581    procedure Ordinary_Fixed_Point_Type_Declaration
12582      (T   : Entity_Id;
12583       Def : Node_Id)
12584    is
12585       Loc           : constant Source_Ptr := Sloc (Def);
12586       Delta_Expr    : constant Node_Id    := Delta_Expression (Def);
12587       RRS           : constant Node_Id    := Real_Range_Specification (Def);
12588       Implicit_Base : Entity_Id;
12589       Delta_Val     : Ureal;
12590       Small_Val     : Ureal;
12591       Low_Val       : Ureal;
12592       High_Val      : Ureal;
12593
12594    begin
12595       Check_Restriction (No_Fixed_Point, Def);
12596
12597       --  Create implicit base type
12598
12599       Implicit_Base :=
12600         Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
12601       Set_Etype (Implicit_Base, Implicit_Base);
12602
12603       --  Analyze and process delta expression
12604
12605       Analyze_And_Resolve (Delta_Expr, Any_Real);
12606
12607       Check_Delta_Expression (Delta_Expr);
12608       Delta_Val := Expr_Value_R (Delta_Expr);
12609
12610       Set_Delta_Value (Implicit_Base, Delta_Val);
12611
12612       --  Compute default small from given delta, which is the largest power
12613       --  of two that does not exceed the given delta value.
12614
12615       declare
12616          Tmp   : Ureal := Ureal_1;
12617          Scale : Int   := 0;
12618
12619       begin
12620          if Delta_Val < Ureal_1 then
12621             while Delta_Val < Tmp loop
12622                Tmp := Tmp / Ureal_2;
12623                Scale := Scale + 1;
12624             end loop;
12625
12626          else
12627             loop
12628                Tmp := Tmp * Ureal_2;
12629                exit when Tmp > Delta_Val;
12630                Scale := Scale - 1;
12631             end loop;
12632          end if;
12633
12634          Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
12635       end;
12636
12637       Set_Small_Value (Implicit_Base, Small_Val);
12638
12639       --  If no range was given, set a dummy range
12640
12641       if RRS <= Empty_Or_Error then
12642          Low_Val  := -Small_Val;
12643          High_Val := Small_Val;
12644
12645       --  Otherwise analyze and process given range
12646
12647       else
12648          declare
12649             Low  : constant Node_Id := Low_Bound  (RRS);
12650             High : constant Node_Id := High_Bound (RRS);
12651
12652          begin
12653             Analyze_And_Resolve (Low, Any_Real);
12654             Analyze_And_Resolve (High, Any_Real);
12655             Check_Real_Bound (Low);
12656             Check_Real_Bound (High);
12657
12658             --  Obtain and set the range
12659
12660             Low_Val  := Expr_Value_R (Low);
12661             High_Val := Expr_Value_R (High);
12662
12663             if Low_Val > High_Val then
12664                Error_Msg_NE ("?fixed point type& has null range", Def, T);
12665             end if;
12666          end;
12667       end if;
12668
12669       --  The range for both the implicit base and the declared first subtype
12670       --  cannot be set yet, so we use the special routine Set_Fixed_Range to
12671       --  set a temporary range in place. Note that the bounds of the base
12672       --  type will be widened to be symmetrical and to fill the available
12673       --  bits when the type is frozen.
12674
12675       --  We could do this with all discrete types, and probably should, but
12676       --  we absolutely have to do it for fixed-point, since the end-points
12677       --  of the range and the size are determined by the small value, which
12678       --  could be reset before the freeze point.
12679
12680       Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
12681       Set_Fixed_Range (T, Loc, Low_Val, High_Val);
12682
12683       Init_Size_Align (Implicit_Base);
12684
12685       --  Complete definition of first subtype
12686
12687       Set_Ekind          (T, E_Ordinary_Fixed_Point_Subtype);
12688       Set_Etype          (T, Implicit_Base);
12689       Init_Size_Align    (T);
12690       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
12691       Set_Small_Value    (T, Small_Val);
12692       Set_Delta_Value    (T, Delta_Val);
12693       Set_Is_Constrained (T);
12694
12695    end Ordinary_Fixed_Point_Type_Declaration;
12696
12697    ----------------------------------------
12698    -- Prepare_Private_Subtype_Completion --
12699    ----------------------------------------
12700
12701    procedure Prepare_Private_Subtype_Completion
12702      (Id          : Entity_Id;
12703       Related_Nod : Node_Id)
12704    is
12705       Id_B   : constant Entity_Id := Base_Type (Id);
12706       Full_B : constant Entity_Id := Full_View (Id_B);
12707       Full   : Entity_Id;
12708
12709    begin
12710       if Present (Full_B) then
12711
12712          --  The Base_Type is already completed, we can complete the subtype
12713          --  now. We have to create a new entity with the same name, Thus we
12714          --  can't use Create_Itype.
12715
12716          --  This is messy, should be fixed ???
12717
12718          Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
12719          Set_Is_Itype (Full);
12720          Set_Associated_Node_For_Itype (Full, Related_Nod);
12721          Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
12722       end if;
12723
12724       --  The parent subtype may be private, but the base might not, in some
12725       --  nested instances. In that case, the subtype does not need to be
12726       --  exchanged. It would still be nice to make private subtypes and their
12727       --  bases consistent at all times ???
12728
12729       if Is_Private_Type (Id_B) then
12730          Append_Elmt (Id, Private_Dependents (Id_B));
12731       end if;
12732
12733    end Prepare_Private_Subtype_Completion;
12734
12735    ---------------------------
12736    -- Process_Discriminants --
12737    ---------------------------
12738
12739    procedure Process_Discriminants
12740      (N    : Node_Id;
12741       Prev : Entity_Id := Empty)
12742    is
12743       Elist               : constant Elist_Id := New_Elmt_List;
12744       Id                  : Node_Id;
12745       Discr               : Node_Id;
12746       Discr_Number        : Uint;
12747       Discr_Type          : Entity_Id;
12748       Default_Present     : Boolean := False;
12749       Default_Not_Present : Boolean := False;
12750
12751    begin
12752       --  A composite type other than an array type can have discriminants.
12753       --  Discriminants of non-limited types must have a discrete type.
12754       --  On entry, the current scope is the composite type.
12755
12756       --  The discriminants are initially entered into the scope of the type
12757       --  via Enter_Name with the default Ekind of E_Void to prevent premature
12758       --  use, as explained at the end of this procedure.
12759
12760       Discr := First (Discriminant_Specifications (N));
12761       while Present (Discr) loop
12762          Enter_Name (Defining_Identifier (Discr));
12763
12764          --  For navigation purposes we add a reference to the discriminant
12765          --  in the entity for the type. If the current declaration is a
12766          --  completion, place references on the partial view. Otherwise the
12767          --  type is the current scope.
12768
12769          if Present (Prev) then
12770
12771             --  The references go on the partial view, if present. If the
12772             --  partial view has discriminants, the references have been
12773             --  generated already.
12774
12775             if not Has_Discriminants (Prev) then
12776                Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
12777             end if;
12778          else
12779             Generate_Reference
12780               (Current_Scope, Defining_Identifier (Discr), 'd');
12781          end if;
12782
12783          if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
12784             Discr_Type := Access_Definition (N, Discriminant_Type (Discr));
12785
12786             --  Ada 2005 (AI-230): Access discriminants are now allowed for
12787             --  nonlimited types, and are treated like other components of
12788             --  anonymous access types in terms of accessibility.
12789
12790             if not Is_Concurrent_Type (Current_Scope)
12791               and then not Is_Concurrent_Record_Type (Current_Scope)
12792               and then not Is_Limited_Record (Current_Scope)
12793               and then Ekind (Current_Scope) /= E_Limited_Private_Type
12794             then
12795                Set_Is_Local_Anonymous_Access (Discr_Type);
12796             end if;
12797
12798             --  Ada 2005 (AI-254)
12799
12800             if Present (Access_To_Subprogram_Definition
12801                          (Discriminant_Type (Discr)))
12802               and then Protected_Present (Access_To_Subprogram_Definition
12803                                            (Discriminant_Type (Discr)))
12804             then
12805                Discr_Type :=
12806                  Replace_Anonymous_Access_To_Protected_Subprogram
12807                    (Discr, Discr_Type);
12808             end if;
12809
12810          else
12811             Find_Type (Discriminant_Type (Discr));
12812             Discr_Type := Etype (Discriminant_Type (Discr));
12813
12814             if Error_Posted (Discriminant_Type (Discr)) then
12815                Discr_Type := Any_Type;
12816             end if;
12817          end if;
12818
12819          if Is_Access_Type (Discr_Type) then
12820
12821             --  Ada 2005 (AI-230): Access discriminant allowed in non-limited
12822             --  record types
12823
12824             if Ada_Version < Ada_05 then
12825                Check_Access_Discriminant_Requires_Limited
12826                  (Discr, Discriminant_Type (Discr));
12827             end if;
12828
12829             if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
12830                Error_Msg_N
12831                  ("(Ada 83) access discriminant not allowed", Discr);
12832             end if;
12833
12834          elsif not Is_Discrete_Type (Discr_Type) then
12835             Error_Msg_N ("discriminants must have a discrete or access type",
12836               Discriminant_Type (Discr));
12837          end if;
12838
12839          Set_Etype (Defining_Identifier (Discr), Discr_Type);
12840
12841          --  If a discriminant specification includes the assignment compound
12842          --  delimiter followed by an expression, the expression is the default
12843          --  expression of the discriminant; the default expression must be of
12844          --  the type of the discriminant. (RM 3.7.1) Since this expression is
12845          --  a default expression, we do the special preanalysis, since this
12846          --  expression does not freeze (see "Handling of Default and Per-
12847          --  Object Expressions" in spec of package Sem).
12848
12849          if Present (Expression (Discr)) then
12850             Analyze_Per_Use_Expression (Expression (Discr), Discr_Type);
12851
12852             if Nkind (N) = N_Formal_Type_Declaration then
12853                Error_Msg_N
12854                  ("discriminant defaults not allowed for formal type",
12855                   Expression (Discr));
12856
12857             --  Tagged types cannot have defaulted discriminants, but a
12858             --  non-tagged private type with defaulted discriminants
12859             --   can have a tagged completion.
12860
12861             elsif Is_Tagged_Type (Current_Scope)
12862               and then Comes_From_Source (N)
12863             then
12864                Error_Msg_N
12865                  ("discriminants of tagged type cannot have defaults",
12866                   Expression (Discr));
12867
12868             else
12869                Default_Present := True;
12870                Append_Elmt (Expression (Discr), Elist);
12871
12872                --  Tag the defining identifiers for the discriminants with
12873                --  their corresponding default expressions from the tree.
12874
12875                Set_Discriminant_Default_Value
12876                  (Defining_Identifier (Discr), Expression (Discr));
12877             end if;
12878
12879          else
12880             Default_Not_Present := True;
12881          end if;
12882
12883          --  Ada 2005 (AI-231): Set the null-excluding attribute and carry
12884          --  out some static checks.
12885
12886          if Ada_Version >= Ada_05
12887            and then (Null_Exclusion_Present (Discr)
12888                        or else Can_Never_Be_Null (Discr_Type))
12889          then
12890             Set_Can_Never_Be_Null (Defining_Identifier (Discr));
12891             Null_Exclusion_Static_Checks (Discr);
12892          end if;
12893
12894          Next (Discr);
12895       end loop;
12896
12897       --  An element list consisting of the default expressions of the
12898       --  discriminants is constructed in the above loop and used to set
12899       --  the Discriminant_Constraint attribute for the type. If an object
12900       --  is declared of this (record or task) type without any explicit
12901       --  discriminant constraint given, this element list will form the
12902       --  actual parameters for the corresponding initialization procedure
12903       --  for the type.
12904
12905       Set_Discriminant_Constraint (Current_Scope, Elist);
12906       Set_Stored_Constraint (Current_Scope, No_Elist);
12907
12908       --  Default expressions must be provided either for all or for none
12909       --  of the discriminants of a discriminant part. (RM 3.7.1)
12910
12911       if Default_Present and then Default_Not_Present then
12912          Error_Msg_N
12913            ("incomplete specification of defaults for discriminants", N);
12914       end if;
12915
12916       --  The use of the name of a discriminant is not allowed in default
12917       --  expressions of a discriminant part if the specification of the
12918       --  discriminant is itself given in the discriminant part. (RM 3.7.1)
12919
12920       --  To detect this, the discriminant names are entered initially with an
12921       --  Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
12922       --  attempt to use a void entity (for example in an expression that is
12923       --  type-checked) produces the error message: premature usage. Now after
12924       --  completing the semantic analysis of the discriminant part, we can set
12925       --  the Ekind of all the discriminants appropriately.
12926
12927       Discr := First (Discriminant_Specifications (N));
12928       Discr_Number := Uint_1;
12929
12930       while Present (Discr) loop
12931          Id := Defining_Identifier (Discr);
12932          Set_Ekind (Id, E_Discriminant);
12933          Init_Component_Location (Id);
12934          Init_Esize (Id);
12935          Set_Discriminant_Number (Id, Discr_Number);
12936
12937          --  Make sure this is always set, even in illegal programs
12938
12939          Set_Corresponding_Discriminant (Id, Empty);
12940
12941          --  Initialize the Original_Record_Component to the entity itself.
12942          --  Inherit_Components will propagate the right value to
12943          --  discriminants in derived record types.
12944
12945          Set_Original_Record_Component (Id, Id);
12946
12947          --  Create the discriminal for the discriminant
12948
12949          Build_Discriminal (Id);
12950
12951          Next (Discr);
12952          Discr_Number := Discr_Number + 1;
12953       end loop;
12954
12955       Set_Has_Discriminants (Current_Scope);
12956    end Process_Discriminants;
12957
12958    -----------------------
12959    -- Process_Full_View --
12960    -----------------------
12961
12962    procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
12963       Priv_Parent : Entity_Id;
12964       Full_Parent : Entity_Id;
12965       Full_Indic  : Node_Id;
12966
12967       function Find_Interface_In_Descendant
12968         (Typ : Entity_Id) return Entity_Id;
12969       --  Find an implemented interface in the derivation chain of Typ
12970
12971       ----------------------------------
12972       -- Find_Interface_In_Descendant --
12973       ----------------------------------
12974
12975       function Find_Interface_In_Descendant
12976         (Typ : Entity_Id) return Entity_Id
12977       is
12978          T : Entity_Id;
12979
12980       begin
12981          T := Typ;
12982          while T /= Etype (T) loop
12983             if Is_Interface (Etype (T)) then
12984                return Etype (T);
12985             end if;
12986
12987             T := Etype (T);
12988          end loop;
12989
12990          return Empty;
12991       end Find_Interface_In_Descendant;
12992
12993    --  Start of processing for Process_Full_View
12994
12995    begin
12996       --  First some sanity checks that must be done after semantic
12997       --  decoration of the full view and thus cannot be placed with other
12998       --  similar checks in Find_Type_Name
12999
13000       if not Is_Limited_Type (Priv_T)
13001         and then (Is_Limited_Type (Full_T)
13002                    or else Is_Limited_Composite (Full_T))
13003       then
13004          Error_Msg_N
13005            ("completion of nonlimited type cannot be limited", Full_T);
13006          Explain_Limited_Type (Full_T, Full_T);
13007
13008       elsif Is_Abstract (Full_T) and then not Is_Abstract (Priv_T) then
13009          Error_Msg_N
13010            ("completion of nonabstract type cannot be abstract", Full_T);
13011
13012       elsif Is_Tagged_Type (Priv_T)
13013         and then Is_Limited_Type (Priv_T)
13014         and then not Is_Limited_Type (Full_T)
13015       then
13016          --  GNAT allow its own definition of Limited_Controlled to disobey
13017          --  this rule in order in ease the implementation. The next test is
13018          --  safe because Root_Controlled is defined in a private system child
13019
13020          if Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
13021             Set_Is_Limited_Composite (Full_T);
13022          else
13023             Error_Msg_N
13024               ("completion of limited tagged type must be limited", Full_T);
13025          end if;
13026
13027       elsif Is_Generic_Type (Priv_T) then
13028          Error_Msg_N ("generic type cannot have a completion", Full_T);
13029       end if;
13030
13031       --  Ada 2005 (AI-396): A full view shall be a descendant of an
13032       --  interface type if and only if the corresponding partial view
13033       --  (if any) is also a descendant of the interface type, or if
13034       --  the partial view is untagged.
13035
13036       if Ada_Version >= Ada_05
13037         and then Is_Tagged_Type (Full_T)
13038       then
13039          declare
13040             Iface     : Entity_Id;
13041             Iface_Def : Node_Id;
13042
13043          begin
13044             Iface := Find_Interface_In_Descendant (Full_T);
13045
13046             if Present (Iface) then
13047                Iface_Def := Type_Definition (Parent (Iface));
13048             end if;
13049
13050             --  The full view derives from an interface descendant, but the
13051             --  partial view does not share the same tagged type.
13052
13053             if Present (Iface)
13054               and then Is_Tagged_Type (Priv_T)
13055               and then Etype (Full_T) /= Etype (Priv_T)
13056             then
13057                Error_Msg_N ("(Ada 2005) tagged partial view cannot be " &
13058                             "completed by a type that implements an " &
13059                             "interface", Priv_T);
13060             end if;
13061
13062             --  The full view derives from a limited, protected,
13063             --  synchronized or task interface descendant, but the
13064             --  partial view is not labeled as limited.
13065
13066             if Present (Iface)
13067               and then (Limited_Present      (Iface_Def)
13068                      or Protected_Present    (Iface_Def)
13069                      or Synchronized_Present (Iface_Def)
13070                      or Task_Present         (Iface_Def))
13071               and then not Limited_Present (Parent (Priv_T))
13072             then
13073                Error_Msg_N ("(Ada 2005) non-limited private type cannot be " &
13074                             "completed by a limited type", Priv_T);
13075             end if;
13076          end;
13077       end if;
13078
13079       if Is_Tagged_Type (Priv_T)
13080         and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
13081         and then Is_Derived_Type (Full_T)
13082       then
13083          Priv_Parent := Etype (Priv_T);
13084
13085          --  The full view of a private extension may have been transformed
13086          --  into an unconstrained derived type declaration and a subtype
13087          --  declaration (see build_derived_record_type for details).
13088
13089          if Nkind (N) = N_Subtype_Declaration then
13090             Full_Indic  := Subtype_Indication (N);
13091             Full_Parent := Etype (Base_Type (Full_T));
13092          else
13093             Full_Indic  := Subtype_Indication (Type_Definition (N));
13094             Full_Parent := Etype (Full_T);
13095          end if;
13096
13097          --  Check that the parent type of the full type is a descendant of
13098          --  the ancestor subtype given in the private extension. If either
13099          --  entity has an Etype equal to Any_Type then we had some previous
13100          --  error situation [7.3(8)].
13101
13102          if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
13103             return;
13104
13105          elsif not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent) then
13106
13107             --  Ada 2005 (AI-251): No error needed if the immediate
13108             --  ancestor of the partial view is an interface
13109             --
13110             --  Example:
13111             --
13112             --       type PT1 is new I1 with private;
13113             --    private
13114             --       type PT1 is new T and I1 with null record;
13115
13116             if Is_Interface (Base_Type (Priv_Parent)) then
13117                null;
13118
13119             else
13120                Error_Msg_N
13121                  ("parent of full type must descend from parent"
13122                      & " of private extension", Full_Indic);
13123             end if;
13124
13125          --  Check the rules of 7.3(10): if the private extension inherits
13126          --  known discriminants, then the full type must also inherit those
13127          --  discriminants from the same (ancestor) type, and the parent
13128          --  subtype of the full type must be constrained if and only if
13129          --  the ancestor subtype of the private extension is constrained.
13130
13131          elsif not Present (Discriminant_Specifications (Parent (Priv_T)))
13132            and then not Has_Unknown_Discriminants (Priv_T)
13133            and then Has_Discriminants (Base_Type (Priv_Parent))
13134          then
13135             declare
13136                Priv_Indic  : constant Node_Id :=
13137                                Subtype_Indication (Parent (Priv_T));
13138
13139                Priv_Constr : constant Boolean :=
13140                                Is_Constrained (Priv_Parent)
13141                                  or else
13142                                    Nkind (Priv_Indic) = N_Subtype_Indication
13143                                  or else Is_Constrained (Entity (Priv_Indic));
13144
13145                Full_Constr : constant Boolean :=
13146                                Is_Constrained (Full_Parent)
13147                                  or else
13148                                    Nkind (Full_Indic) = N_Subtype_Indication
13149                                  or else Is_Constrained (Entity (Full_Indic));
13150
13151                Priv_Discr : Entity_Id;
13152                Full_Discr : Entity_Id;
13153
13154             begin
13155                Priv_Discr := First_Discriminant (Priv_Parent);
13156                Full_Discr := First_Discriminant (Full_Parent);
13157
13158                while Present (Priv_Discr) and then Present (Full_Discr) loop
13159                   if Original_Record_Component (Priv_Discr) =
13160                      Original_Record_Component (Full_Discr)
13161                     or else
13162                      Corresponding_Discriminant (Priv_Discr) =
13163                      Corresponding_Discriminant (Full_Discr)
13164                   then
13165                      null;
13166                   else
13167                      exit;
13168                   end if;
13169
13170                   Next_Discriminant (Priv_Discr);
13171                   Next_Discriminant (Full_Discr);
13172                end loop;
13173
13174                if Present (Priv_Discr) or else Present (Full_Discr) then
13175                   Error_Msg_N
13176                     ("full view must inherit discriminants of the parent type"
13177                      & " used in the private extension", Full_Indic);
13178
13179                elsif Priv_Constr and then not Full_Constr then
13180                   Error_Msg_N
13181                     ("parent subtype of full type must be constrained",
13182                      Full_Indic);
13183
13184                elsif Full_Constr and then not Priv_Constr then
13185                   Error_Msg_N
13186                     ("parent subtype of full type must be unconstrained",
13187                      Full_Indic);
13188                end if;
13189             end;
13190
13191          --  Check the rules of 7.3(12): if a partial view has neither known
13192          --  or unknown discriminants, then the full type declaration shall
13193          --  define a definite subtype.
13194
13195          elsif      not Has_Unknown_Discriminants (Priv_T)
13196            and then not Has_Discriminants (Priv_T)
13197            and then not Is_Constrained (Full_T)
13198          then
13199             Error_Msg_N
13200               ("full view must define a constrained type if partial view"
13201                 & " has no discriminants", Full_T);
13202          end if;
13203
13204          --  ??????? Do we implement the following properly ?????
13205          --  If the ancestor subtype of a private extension has constrained
13206          --  discriminants, then the parent subtype of the full view shall
13207          --  impose a statically matching constraint on those discriminants
13208          --  [7.3(13)].
13209
13210       else
13211          --  For untagged types, verify that a type without discriminants
13212          --  is not completed with an unconstrained type.
13213
13214          if not Is_Indefinite_Subtype (Priv_T)
13215            and then Is_Indefinite_Subtype (Full_T)
13216          then
13217             Error_Msg_N ("full view of type must be definite subtype", Full_T);
13218          end if;
13219       end if;
13220
13221       --  Ada 2005 AI-363: if the full view has discriminants with
13222       --  defaults, it is illegal to declare constrained access subtypes
13223       --  whose designated type is the current type. This allows objects
13224       --  of the type that are declared in the heap to be unconstrained.
13225
13226       if not Has_Unknown_Discriminants (Priv_T)
13227         and then not Has_Discriminants (Priv_T)
13228         and then Has_Discriminants (Full_T)
13229         and then
13230           Present
13231             (Discriminant_Default_Value (First_Discriminant (Full_T)))
13232       then
13233          Set_Has_Constrained_Partial_View (Full_T);
13234          Set_Has_Constrained_Partial_View (Priv_T);
13235       end if;
13236
13237       --  Create a full declaration for all its subtypes recorded in
13238       --  Private_Dependents and swap them similarly to the base type. These
13239       --  are subtypes that have been define before the full declaration of
13240       --  the private type. We also swap the entry in Private_Dependents list
13241       --  so we can properly restore the private view on exit from the scope.
13242
13243       declare
13244          Priv_Elmt : Elmt_Id;
13245          Priv      : Entity_Id;
13246          Full      : Entity_Id;
13247
13248       begin
13249          Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
13250          while Present (Priv_Elmt) loop
13251             Priv := Node (Priv_Elmt);
13252
13253             if Ekind (Priv) = E_Private_Subtype
13254               or else Ekind (Priv) = E_Limited_Private_Subtype
13255               or else Ekind (Priv) = E_Record_Subtype_With_Private
13256             then
13257                Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
13258                Set_Is_Itype (Full);
13259                Set_Parent (Full, Parent (Priv));
13260                Set_Associated_Node_For_Itype (Full, N);
13261
13262                --  Now we need to complete the private subtype, but since the
13263                --  base type has already been swapped, we must also swap the
13264                --  subtypes (and thus, reverse the arguments in the call to
13265                --  Complete_Private_Subtype).
13266
13267                Copy_And_Swap (Priv, Full);
13268                Complete_Private_Subtype (Full, Priv, Full_T, N);
13269                Replace_Elmt (Priv_Elmt, Full);
13270             end if;
13271
13272             Next_Elmt (Priv_Elmt);
13273          end loop;
13274       end;
13275
13276       --  If the private view was tagged, copy the new Primitive
13277       --  operations from the private view to the full view.
13278
13279       if Is_Tagged_Type (Full_T) then
13280          declare
13281             Priv_List : Elist_Id;
13282             Full_List : constant Elist_Id := Primitive_Operations (Full_T);
13283             P1, P2    : Elmt_Id;
13284             Prim      : Entity_Id;
13285             D_Type    : Entity_Id;
13286
13287          begin
13288             if Is_Tagged_Type (Priv_T) then
13289                Priv_List := Primitive_Operations (Priv_T);
13290
13291                P1 := First_Elmt (Priv_List);
13292                while Present (P1) loop
13293                   Prim := Node (P1);
13294
13295                   --  Transfer explicit primitives, not those inherited from
13296                   --  parent of partial view, which will be re-inherited on
13297                   --  the full view.
13298
13299                   if Comes_From_Source (Prim) then
13300                      P2 := First_Elmt (Full_List);
13301                      while Present (P2) and then Node (P2) /= Prim loop
13302                         Next_Elmt (P2);
13303                      end loop;
13304
13305                      --  If not found, that is a new one
13306
13307                      if No (P2) then
13308                         Append_Elmt (Prim, Full_List);
13309                      end if;
13310                   end if;
13311
13312                   Next_Elmt (P1);
13313                end loop;
13314
13315             else
13316                --  In this case the partial view is untagged, so here we
13317                --  locate all of the earlier primitives that need to be
13318                --  treated as dispatching (those that appear between the two
13319                --  views). Note that these additional operations must all be
13320                --  new operations (any earlier operations that override
13321                --  inherited operations of the full view will already have
13322                --  been inserted in the primitives list and marked as
13323                --  dispatching by Check_Operation_From_Private_View. Note that
13324                --  implicit "/=" operators are excluded from being added to
13325                --  the primitives list since they shouldn't be treated as
13326                --  dispatching (tagged "/=" is handled specially).
13327
13328                Prim := Next_Entity (Full_T);
13329                while Present (Prim) and then Prim /= Priv_T loop
13330                   if Ekind (Prim) = E_Procedure
13331                        or else
13332                      Ekind (Prim) = E_Function
13333                   then
13334
13335                      D_Type := Find_Dispatching_Type (Prim);
13336
13337                      if D_Type = Full_T
13338                        and then (Chars (Prim) /= Name_Op_Ne
13339                                   or else Comes_From_Source (Prim))
13340                      then
13341                         Check_Controlling_Formals (Full_T, Prim);
13342
13343                         if not Is_Dispatching_Operation (Prim) then
13344                            Append_Elmt (Prim, Full_List);
13345                            Set_Is_Dispatching_Operation (Prim, True);
13346                            Set_DT_Position (Prim, No_Uint);
13347                         end if;
13348
13349                      elsif Is_Dispatching_Operation (Prim)
13350                        and then D_Type  /= Full_T
13351                      then
13352
13353                         --  Verify that it is not otherwise controlled by
13354                         --  a formal or a return value ot type T.
13355
13356                         Check_Controlling_Formals (D_Type, Prim);
13357                      end if;
13358                   end if;
13359
13360                   Next_Entity (Prim);
13361                end loop;
13362             end if;
13363
13364             --  For the tagged case, the two views can share the same
13365             --  Primitive Operation list and the same class wide type.
13366             --  Update attributes of the class-wide type which depend on
13367             --  the full declaration.
13368
13369             if Is_Tagged_Type (Priv_T) then
13370                Set_Primitive_Operations (Priv_T, Full_List);
13371                Set_Class_Wide_Type
13372                  (Base_Type (Full_T), Class_Wide_Type (Priv_T));
13373
13374                --  Any other attributes should be propagated to C_W ???
13375
13376                Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
13377
13378             end if;
13379          end;
13380       end if;
13381    end Process_Full_View;
13382
13383    -----------------------------------
13384    -- Process_Incomplete_Dependents --
13385    -----------------------------------
13386
13387    procedure Process_Incomplete_Dependents
13388      (N      : Node_Id;
13389       Full_T : Entity_Id;
13390       Inc_T  : Entity_Id)
13391    is
13392       Inc_Elmt : Elmt_Id;
13393       Priv_Dep : Entity_Id;
13394       New_Subt : Entity_Id;
13395
13396       Disc_Constraint : Elist_Id;
13397
13398    begin
13399       if No (Private_Dependents (Inc_T)) then
13400          return;
13401
13402       else
13403          Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
13404
13405          --  Itypes that may be generated by the completion of an incomplete
13406          --  subtype are not used by the back-end and not attached to the tree.
13407          --  They are created only for constraint-checking purposes.
13408       end if;
13409
13410       while Present (Inc_Elmt) loop
13411          Priv_Dep := Node (Inc_Elmt);
13412
13413          if Ekind (Priv_Dep) = E_Subprogram_Type then
13414
13415             --  An Access_To_Subprogram type may have a return type or a
13416             --  parameter type that is incomplete. Replace with the full view.
13417
13418             if Etype (Priv_Dep) = Inc_T then
13419                Set_Etype (Priv_Dep, Full_T);
13420             end if;
13421
13422             declare
13423                Formal : Entity_Id;
13424
13425             begin
13426                Formal := First_Formal (Priv_Dep);
13427
13428                while Present (Formal) loop
13429
13430                   if Etype (Formal) = Inc_T then
13431                      Set_Etype (Formal, Full_T);
13432                   end if;
13433
13434                   Next_Formal (Formal);
13435                end loop;
13436             end;
13437
13438          elsif  Is_Overloadable (Priv_Dep) then
13439
13440             if Is_Tagged_Type (Full_T) then
13441
13442                --  Subprogram has an access parameter whose designated type
13443                --  was incomplete. Reexamine declaration now, because it may
13444                --  be a primitive operation of the full type.
13445
13446                Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
13447                Set_Is_Dispatching_Operation (Priv_Dep);
13448                Check_Controlling_Formals (Full_T, Priv_Dep);
13449             end if;
13450
13451          elsif Ekind (Priv_Dep) = E_Subprogram_Body then
13452
13453             --  Can happen during processing of a body before the completion
13454             --  of a TA type. Ignore, because spec is also on dependent list.
13455
13456             return;
13457
13458          --  Dependent is a subtype
13459
13460          else
13461             --  We build a new subtype indication using the full view of the
13462             --  incomplete parent. The discriminant constraints have been
13463             --  elaborated already at the point of the subtype declaration.
13464
13465             New_Subt := Create_Itype (E_Void, N);
13466
13467             if Has_Discriminants (Full_T) then
13468                Disc_Constraint := Discriminant_Constraint (Priv_Dep);
13469             else
13470                Disc_Constraint := No_Elist;
13471             end if;
13472
13473             Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
13474             Set_Full_View (Priv_Dep, New_Subt);
13475          end if;
13476
13477          Next_Elmt (Inc_Elmt);
13478       end loop;
13479    end Process_Incomplete_Dependents;
13480
13481    --------------------------------
13482    -- Process_Range_Expr_In_Decl --
13483    --------------------------------
13484
13485    procedure Process_Range_Expr_In_Decl
13486      (R           : Node_Id;
13487       T           : Entity_Id;
13488       Check_List  : List_Id := Empty_List;
13489       R_Check_Off : Boolean := False)
13490    is
13491       Lo, Hi    : Node_Id;
13492       R_Checks  : Check_Result;
13493       Type_Decl : Node_Id;
13494       Def_Id    : Entity_Id;
13495
13496    begin
13497       Analyze_And_Resolve (R, Base_Type (T));
13498
13499       if Nkind (R) = N_Range then
13500          Lo := Low_Bound (R);
13501          Hi := High_Bound (R);
13502
13503          --  If there were errors in the declaration, try and patch up some
13504          --  common mistakes in the bounds. The cases handled are literals
13505          --  which are Integer where the expected type is Real and vice versa.
13506          --  These corrections allow the compilation process to proceed further
13507          --  along since some basic assumptions of the format of the bounds
13508          --  are guaranteed.
13509
13510          if Etype (R) = Any_Type then
13511
13512             if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
13513                Rewrite (Lo,
13514                  Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
13515
13516             elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
13517                Rewrite (Hi,
13518                  Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
13519
13520             elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
13521                Rewrite (Lo,
13522                  Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
13523
13524             elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
13525                Rewrite (Hi,
13526                  Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
13527             end if;
13528
13529             Set_Etype (Lo, T);
13530             Set_Etype (Hi, T);
13531          end if;
13532
13533          --  If the bounds of the range have been mistakenly given as string
13534          --  literals (perhaps in place of character literals), then an error
13535          --  has already been reported, but we rewrite the string literal as a
13536          --  bound of the range's type to avoid blowups in later processing
13537          --  that looks at static values.
13538
13539          if Nkind (Lo) = N_String_Literal then
13540             Rewrite (Lo,
13541               Make_Attribute_Reference (Sloc (Lo),
13542                 Attribute_Name => Name_First,
13543                 Prefix => New_Reference_To (T, Sloc (Lo))));
13544             Analyze_And_Resolve (Lo);
13545          end if;
13546
13547          if Nkind (Hi) = N_String_Literal then
13548             Rewrite (Hi,
13549               Make_Attribute_Reference (Sloc (Hi),
13550                 Attribute_Name => Name_First,
13551                 Prefix => New_Reference_To (T, Sloc (Hi))));
13552             Analyze_And_Resolve (Hi);
13553          end if;
13554
13555          --  If bounds aren't scalar at this point then exit, avoiding
13556          --  problems with further processing of the range in this procedure.
13557
13558          if not Is_Scalar_Type (Etype (Lo)) then
13559             return;
13560          end if;
13561
13562          --  Resolve (actually Sem_Eval) has checked that the bounds are in
13563          --  then range of the base type. Here we check whether the bounds
13564          --  are in the range of the subtype itself. Note that if the bounds
13565          --  represent the null range the Constraint_Error exception should
13566          --  not be raised.
13567
13568          --  ??? The following code should be cleaned up as follows
13569
13570          --  1. The Is_Null_Range (Lo, Hi) test should disappear since it
13571          --     is done in the call to Range_Check (R, T); below
13572
13573          --  2. The use of R_Check_Off should be investigated and possibly
13574          --     removed, this would clean up things a bit.
13575
13576          if Is_Null_Range (Lo, Hi) then
13577             null;
13578
13579          else
13580             --  Capture values of bounds and generate temporaries for them
13581             --  if needed, before applying checks, since checks may cause
13582             --  duplication of the expression without forcing evaluation.
13583
13584             if Expander_Active then
13585                Force_Evaluation (Lo);
13586                Force_Evaluation (Hi);
13587             end if;
13588
13589             --  We use a flag here instead of suppressing checks on the
13590             --  type because the type we check against isn't necessarily
13591             --  the place where we put the check.
13592
13593             if not R_Check_Off then
13594                R_Checks := Range_Check (R, T);
13595                Type_Decl := Parent (R);
13596
13597                --  Look up tree to find an appropriate insertion point.
13598                --  This seems really junk code, and very brittle, couldn't
13599                --  we just use an insert actions call of some kind ???
13600
13601                while Present (Type_Decl) and then not
13602                  (Nkind (Type_Decl) = N_Full_Type_Declaration
13603                     or else
13604                   Nkind (Type_Decl) = N_Subtype_Declaration
13605                     or else
13606                   Nkind (Type_Decl) = N_Loop_Statement
13607                     or else
13608                   Nkind (Type_Decl) = N_Task_Type_Declaration
13609                     or else
13610                   Nkind (Type_Decl) = N_Single_Task_Declaration
13611                     or else
13612                   Nkind (Type_Decl) = N_Protected_Type_Declaration
13613                     or else
13614                   Nkind (Type_Decl) = N_Single_Protected_Declaration)
13615                loop
13616                   Type_Decl := Parent (Type_Decl);
13617                end loop;
13618
13619                --  Why would Type_Decl not be present???  Without this test,
13620                --  short regression tests fail.
13621
13622                if Present (Type_Decl) then
13623
13624                   --  Case of loop statement (more comments ???)
13625
13626                   if Nkind (Type_Decl) = N_Loop_Statement then
13627                      declare
13628                         Indic : Node_Id := Parent (R);
13629
13630                      begin
13631                         while Present (Indic) and then not
13632                           (Nkind (Indic) = N_Subtype_Indication)
13633                         loop
13634                            Indic := Parent (Indic);
13635                         end loop;
13636
13637                         if Present (Indic) then
13638                            Def_Id := Etype (Subtype_Mark (Indic));
13639
13640                            Insert_Range_Checks
13641                              (R_Checks,
13642                               Type_Decl,
13643                               Def_Id,
13644                               Sloc (Type_Decl),
13645                               R,
13646                               Do_Before => True);
13647                         end if;
13648                      end;
13649
13650                   --  All other cases (more comments ???)
13651
13652                   else
13653                      Def_Id := Defining_Identifier (Type_Decl);
13654
13655                      if (Ekind (Def_Id) = E_Record_Type
13656                           and then Depends_On_Discriminant (R))
13657                        or else
13658                         (Ekind (Def_Id) = E_Protected_Type
13659                           and then Has_Discriminants (Def_Id))
13660                      then
13661                         Append_Range_Checks
13662                           (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
13663
13664                      else
13665                         Insert_Range_Checks
13666                           (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
13667
13668                      end if;
13669                   end if;
13670                end if;
13671             end if;
13672          end if;
13673
13674       elsif Expander_Active then
13675          Get_Index_Bounds (R, Lo, Hi);
13676          Force_Evaluation (Lo);
13677          Force_Evaluation (Hi);
13678       end if;
13679    end Process_Range_Expr_In_Decl;
13680
13681    --------------------------------------
13682    -- Process_Real_Range_Specification --
13683    --------------------------------------
13684
13685    procedure Process_Real_Range_Specification (Def : Node_Id) is
13686       Spec : constant Node_Id := Real_Range_Specification (Def);
13687       Lo   : Node_Id;
13688       Hi   : Node_Id;
13689       Err  : Boolean := False;
13690
13691       procedure Analyze_Bound (N : Node_Id);
13692       --  Analyze and check one bound
13693
13694       -------------------
13695       -- Analyze_Bound --
13696       -------------------
13697
13698       procedure Analyze_Bound (N : Node_Id) is
13699       begin
13700          Analyze_And_Resolve (N, Any_Real);
13701
13702          if not Is_OK_Static_Expression (N) then
13703             Flag_Non_Static_Expr
13704               ("bound in real type definition is not static!", N);
13705             Err := True;
13706          end if;
13707       end Analyze_Bound;
13708
13709    --  Start of processing for Process_Real_Range_Specification
13710
13711    begin
13712       if Present (Spec) then
13713          Lo := Low_Bound (Spec);
13714          Hi := High_Bound (Spec);
13715          Analyze_Bound (Lo);
13716          Analyze_Bound (Hi);
13717
13718          --  If error, clear away junk range specification
13719
13720          if Err then
13721             Set_Real_Range_Specification (Def, Empty);
13722          end if;
13723       end if;
13724    end Process_Real_Range_Specification;
13725
13726    ---------------------
13727    -- Process_Subtype --
13728    ---------------------
13729
13730    function Process_Subtype
13731      (S           : Node_Id;
13732       Related_Nod : Node_Id;
13733       Related_Id  : Entity_Id := Empty;
13734       Suffix      : Character := ' ') return Entity_Id
13735    is
13736       P               : Node_Id;
13737       Def_Id          : Entity_Id;
13738       Full_View_Id    : Entity_Id;
13739       Subtype_Mark_Id : Entity_Id;
13740
13741       procedure Check_Incomplete (T : Entity_Id);
13742       --  Called to verify that an incomplete type is not used prematurely
13743
13744       ----------------------
13745       -- Check_Incomplete --
13746       ----------------------
13747
13748       procedure Check_Incomplete (T : Entity_Id) is
13749       begin
13750          if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type then
13751             Error_Msg_N ("invalid use of type before its full declaration", T);
13752          end if;
13753       end Check_Incomplete;
13754
13755    --  Start of processing for Process_Subtype
13756
13757    begin
13758       --  Case of no constraints present
13759
13760       if Nkind (S) /= N_Subtype_Indication then
13761
13762          Find_Type (S);
13763          Check_Incomplete (S);
13764
13765          --  Ada 2005 (AI-231): Static check
13766
13767          if Ada_Version >= Ada_05
13768            and then Present (Parent (S))
13769            and then Null_Exclusion_Present (Parent (S))
13770            and then Nkind (Parent (S)) /= N_Access_To_Object_Definition
13771            and then not Is_Access_Type (Entity (S))
13772          then
13773             Error_Msg_N
13774               ("(Ada 2005) null-exclusion part requires an access type", S);
13775          end if;
13776          return Entity (S);
13777
13778       --  Case of constraint present, so that we have an N_Subtype_Indication
13779       --  node (this node is created only if constraints are present).
13780
13781       else
13782
13783          Find_Type (Subtype_Mark (S));
13784
13785          if Nkind (Parent (S)) /= N_Access_To_Object_Definition
13786            and then not
13787             (Nkind (Parent (S)) = N_Subtype_Declaration
13788               and then
13789              Is_Itype (Defining_Identifier (Parent (S))))
13790          then
13791             Check_Incomplete (Subtype_Mark (S));
13792          end if;
13793
13794          P := Parent (S);
13795          Subtype_Mark_Id := Entity (Subtype_Mark (S));
13796
13797          --  Explicit subtype declaration case
13798
13799          if Nkind (P) = N_Subtype_Declaration then
13800             Def_Id := Defining_Identifier (P);
13801
13802          --  Explicit derived type definition case
13803
13804          elsif Nkind (P) = N_Derived_Type_Definition then
13805             Def_Id := Defining_Identifier (Parent (P));
13806
13807          --  Implicit case, the Def_Id must be created as an implicit type.
13808          --  The one exception arises in the case of concurrent types, array
13809          --  and access types, where other subsidiary implicit types may be
13810          --  created and must appear before the main implicit type. In these
13811          --  cases we leave Def_Id set to Empty as a signal that Create_Itype
13812          --  has not yet been called to create Def_Id.
13813
13814          else
13815             if Is_Array_Type (Subtype_Mark_Id)
13816               or else Is_Concurrent_Type (Subtype_Mark_Id)
13817               or else Is_Access_Type (Subtype_Mark_Id)
13818             then
13819                Def_Id := Empty;
13820
13821             --  For the other cases, we create a new unattached Itype,
13822             --  and set the indication to ensure it gets attached later.
13823
13824             else
13825                Def_Id :=
13826                  Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13827             end if;
13828          end if;
13829
13830          --  If the kind of constraint is invalid for this kind of type,
13831          --  then give an error, and then pretend no constraint was given.
13832
13833          if not Is_Valid_Constraint_Kind
13834                    (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
13835          then
13836             Error_Msg_N
13837               ("incorrect constraint for this kind of type", Constraint (S));
13838
13839             Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
13840
13841             --  Set Ekind of orphan itype, to prevent cascaded errors
13842
13843             if Present (Def_Id) then
13844                Set_Ekind (Def_Id, Ekind (Any_Type));
13845             end if;
13846
13847             --  Make recursive call, having got rid of the bogus constraint
13848
13849             return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
13850          end if;
13851
13852          --  Remaining processing depends on type
13853
13854          case Ekind (Subtype_Mark_Id) is
13855             when Access_Kind =>
13856                Constrain_Access (Def_Id, S, Related_Nod);
13857
13858             when Array_Kind =>
13859                Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
13860
13861             when Decimal_Fixed_Point_Kind =>
13862                Constrain_Decimal (Def_Id, S);
13863
13864             when Enumeration_Kind =>
13865                Constrain_Enumeration (Def_Id, S);
13866
13867             when Ordinary_Fixed_Point_Kind =>
13868                Constrain_Ordinary_Fixed (Def_Id, S);
13869
13870             when Float_Kind =>
13871                Constrain_Float (Def_Id, S);
13872
13873             when Integer_Kind =>
13874                Constrain_Integer (Def_Id, S);
13875
13876             when E_Record_Type     |
13877                  E_Record_Subtype  |
13878                  Class_Wide_Kind   |
13879                  E_Incomplete_Type =>
13880                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
13881
13882             when Private_Kind =>
13883                Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
13884                Set_Private_Dependents (Def_Id, New_Elmt_List);
13885
13886                --  In case of an invalid constraint prevent further processing
13887                --  since the type constructed is missing expected fields.
13888
13889                if Etype (Def_Id) = Any_Type then
13890                   return Def_Id;
13891                end if;
13892
13893                --  If the full view is that of a task with discriminants,
13894                --  we must constrain both the concurrent type and its
13895                --  corresponding record type. Otherwise we will just propagate
13896                --  the constraint to the full view, if available.
13897
13898                if Present (Full_View (Subtype_Mark_Id))
13899                  and then Has_Discriminants (Subtype_Mark_Id)
13900                  and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
13901                then
13902                   Full_View_Id :=
13903                     Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13904
13905                   Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
13906                   Constrain_Concurrent (Full_View_Id, S,
13907                     Related_Nod, Related_Id, Suffix);
13908                   Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
13909                   Set_Full_View (Def_Id, Full_View_Id);
13910
13911                else
13912                   Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
13913                end if;
13914
13915             when Concurrent_Kind  =>
13916                Constrain_Concurrent (Def_Id, S,
13917                  Related_Nod, Related_Id, Suffix);
13918
13919             when others =>
13920                Error_Msg_N ("invalid subtype mark in subtype indication", S);
13921          end case;
13922
13923          --  Size and Convention are always inherited from the base type
13924
13925          Set_Size_Info  (Def_Id,            (Subtype_Mark_Id));
13926          Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
13927
13928          return Def_Id;
13929       end if;
13930    end Process_Subtype;
13931
13932    -----------------------------
13933    -- Record_Type_Declaration --
13934    -----------------------------
13935
13936    procedure Record_Type_Declaration
13937      (T    : Entity_Id;
13938       N    : Node_Id;
13939       Prev : Entity_Id)
13940    is
13941       Loc   : constant Source_Ptr := Sloc (N);
13942       Def   : constant Node_Id    := Type_Definition (N);
13943       Inc_T : Entity_Id := Empty;
13944
13945       Is_Tagged : Boolean;
13946       Tag_Comp  : Entity_Id;
13947
13948       procedure Check_Anonymous_Access_Types (Comp_List : Node_Id);
13949       --  Ada 2005 AI-382: an access component in a record declaration can
13950       --  refer to the enclosing record, in which case it denotes the type
13951       --  itself, and not the current instance of the type. We create an
13952       --  anonymous access type for the component, and flag it as an access
13953       --  to a component, so that accessibility checks are properly performed
13954       --  on it. The declaration of the access type is placed ahead of that
13955       --  of the record, to prevent circular order-of-elaboration issues in
13956       --  gigi. We create an incomplete type for the record declaration, which
13957       --  is the designated type of the anonymous access.
13958
13959       procedure Make_Incomplete_Type_Declaration;
13960       --  If the record type contains components that include an access to the
13961       --  current record, create an incomplete type declaration for the record,
13962       --  to be used as the designated type of the anonymous access. This is
13963       --  done only once, and only if there is no previous partial view of the
13964       --  type.
13965
13966       ----------------------------------
13967       -- Check_Anonymous_Access_Types --
13968       ----------------------------------
13969
13970       procedure Check_Anonymous_Access_Types (Comp_List : Node_Id) is
13971          Anon_Access : Entity_Id;
13972          Acc_Def     : Node_Id;
13973          Comp        : Node_Id;
13974          Decl        : Node_Id;
13975          Type_Def    : Node_Id;
13976
13977          function Mentions_T (Acc_Def : Node_Id) return Boolean;
13978          --  Check whether an access definition includes a reference to
13979          --  the enclosing record type. The reference can be a subtype
13980          --  mark in the access definition itself, or a 'Class attribute
13981          --  reference, or recursively a reference appearing in a parameter
13982          --  type in an access_to_subprogram definition.
13983
13984          ----------------
13985          -- Mentions_T --
13986          ----------------
13987
13988          function Mentions_T (Acc_Def : Node_Id) return Boolean is
13989             Subt : Node_Id;
13990
13991          begin
13992             if No (Access_To_Subprogram_Definition (Acc_Def)) then
13993                Subt := Subtype_Mark (Acc_Def);
13994
13995                if Nkind (Subt) = N_Identifier then
13996                   return Chars (Subt) = Chars (T);
13997                elsif Nkind (Subt) = N_Attribute_Reference
13998                   and then Attribute_Name (Subt) = Name_Class
13999                then
14000                   return (Chars (Prefix (Subt))) = Chars (T);
14001                else
14002                   return False;
14003                end if;
14004
14005             else
14006                --  Component is an access_to_subprogram: examine its formals
14007
14008                declare
14009                   Param_Spec : Node_Id;
14010
14011                begin
14012                   Param_Spec :=
14013                     First
14014                       (Parameter_Specifications
14015                         (Access_To_Subprogram_Definition (Acc_Def)));
14016                   while Present (Param_Spec) loop
14017                      if Nkind (Parameter_Type (Param_Spec))
14018                           = N_Access_Definition
14019                        and then Mentions_T (Parameter_Type (Param_Spec))
14020                      then
14021                         return True;
14022                      end if;
14023
14024                      Next (Param_Spec);
14025                   end loop;
14026
14027                   return False;
14028                end;
14029             end if;
14030          end Mentions_T;
14031
14032       --  Start of processing for Check_Anonymous_Access_Types
14033
14034       begin
14035          if No (Comp_List) then
14036             return;
14037          end if;
14038
14039          Comp := First (Component_Items (Comp_List));
14040          while Present (Comp) loop
14041             if Nkind (Comp) = N_Component_Declaration
14042               and then
14043                 Present (Access_Definition (Component_Definition (Comp)))
14044               and then
14045                 Mentions_T (Access_Definition (Component_Definition (Comp)))
14046             then
14047                Acc_Def :=
14048                  Access_To_Subprogram_Definition
14049                    (Access_Definition (Component_Definition (Comp)));
14050
14051                Make_Incomplete_Type_Declaration;
14052                Anon_Access :=
14053                  Make_Defining_Identifier (Loc,
14054                    Chars => New_Internal_Name ('S'));
14055
14056                --  Create a declaration for the anonymous access type: either
14057                --  an access_to_object or an access_to_subprogram.
14058
14059                if Present (Acc_Def) then
14060                   if Nkind  (Acc_Def) = N_Access_Function_Definition then
14061                      Type_Def :=
14062                        Make_Access_Function_Definition (Loc,
14063                          Parameter_Specifications =>
14064                            Parameter_Specifications (Acc_Def),
14065                          Subtype_Mark => Subtype_Mark (Acc_Def));
14066                   else
14067                      Type_Def :=
14068                        Make_Access_Procedure_Definition (Loc,
14069                          Parameter_Specifications =>
14070                            Parameter_Specifications (Acc_Def));
14071                   end if;
14072
14073                else
14074                   Type_Def :=
14075                     Make_Access_To_Object_Definition (Loc,
14076                       Subtype_Indication =>
14077                          Relocate_Node
14078                            (Subtype_Mark
14079                              (Access_Definition
14080                                (Component_Definition (Comp)))));
14081                end if;
14082
14083                Decl := Make_Full_Type_Declaration (Loc,
14084                   Defining_Identifier => Anon_Access,
14085                   Type_Definition => Type_Def);
14086
14087                Insert_Before (N, Decl);
14088                Analyze (Decl);
14089
14090                Set_Access_Definition (Component_Definition (Comp), Empty);
14091                Set_Subtype_Indication (Component_Definition (Comp),
14092                   New_Occurrence_Of (Anon_Access, Loc));
14093                Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
14094                Set_Is_Local_Anonymous_Access (Anon_Access);
14095             end if;
14096
14097             Next (Comp);
14098          end loop;
14099
14100          if Present (Variant_Part (Comp_List)) then
14101             declare
14102                V : Node_Id;
14103             begin
14104                V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
14105                while Present (V) loop
14106                   Check_Anonymous_Access_Types (Component_List (V));
14107                   Next_Non_Pragma (V);
14108                end loop;
14109             end;
14110          end if;
14111       end Check_Anonymous_Access_Types;
14112
14113       --------------------------------------
14114       -- Make_Incomplete_Type_Declaration --
14115       --------------------------------------
14116
14117       procedure Make_Incomplete_Type_Declaration is
14118          Decl : Node_Id;
14119          H    : Entity_Id;
14120
14121       begin
14122          --  If there is a previous partial view, no need to create a new one.
14123
14124          if Prev /= T then
14125             return;
14126
14127          elsif No (Inc_T) then
14128             Inc_T  := Make_Defining_Identifier (Loc, Chars (T));
14129             Decl   := Make_Incomplete_Type_Declaration (Loc, Inc_T);
14130
14131             --  Type has already been inserted into the current scope.
14132             --  Remove it, and add incomplete declaration for type, so
14133             --  that subsequent anonymous access types can use it.
14134
14135             H := Current_Entity (T);
14136
14137             if H = T then
14138                Set_Name_Entity_Id (Chars (T), Empty);
14139             else
14140                while Present (H)
14141                  and then Homonym (H) /= T
14142                loop
14143                   H := Homonym (T);
14144                end loop;
14145
14146                Set_Homonym (H, Homonym (T));
14147             end if;
14148
14149             Insert_Before (N, Decl);
14150             Analyze (Decl);
14151             Set_Full_View (Inc_T, T);
14152
14153             if Tagged_Present (Def) then
14154                Make_Class_Wide_Type (Inc_T);
14155                Set_Class_Wide_Type (T, Class_Wide_Type (Inc_T));
14156             end if;
14157          end if;
14158       end Make_Incomplete_Type_Declaration;
14159
14160    --  Start of processing for Record_Type_Declaration
14161
14162    begin
14163       --  These flags must be initialized before calling Process_Discriminants
14164       --  because this routine makes use of them.
14165
14166       Set_Ekind               (T, E_Record_Type);
14167       Set_Etype               (T, T);
14168       Init_Size_Align         (T);
14169       Set_Abstract_Interfaces (T, No_Elist);
14170       Set_Stored_Constraint   (T, No_Elist);
14171
14172       --  Normal case
14173
14174       if Ada_Version < Ada_05
14175         or else not Interface_Present (Def)
14176       then
14177          --  The flag Is_Tagged_Type might have already been set by
14178          --  Find_Type_Name if it detected an error for declaration T. This
14179          --  arises in the case of private tagged types where the full view
14180          --  omits the word tagged.
14181
14182          Is_Tagged :=
14183            Tagged_Present (Def)
14184              or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
14185
14186          Set_Is_Tagged_Type      (T, Is_Tagged);
14187          Set_Is_Limited_Record   (T, Limited_Present (Def));
14188
14189          --  Type is abstract if full declaration carries keyword, or if
14190          --  previous partial view did.
14191
14192          Set_Is_Abstract         (T, Is_Abstract (T)
14193                                       or else Abstract_Present (Def));
14194
14195       else
14196          Is_Tagged := True;
14197          Set_Is_Tagged_Type      (T);
14198
14199          Set_Is_Limited_Record   (T, Limited_Present (Def)
14200                                       or else Task_Present (Def)
14201                                       or else Protected_Present (Def));
14202
14203          --  Type is abstract if full declaration carries keyword, or if
14204          --  previous partial view did.
14205
14206          Set_Is_Abstract  (T);
14207          Set_Is_Interface (T);
14208       end if;
14209
14210       --  First pass: if there are self-referential access components,
14211       --  create the required anonymous access type declarations, and if
14212       --  need be an incomplete type declaration for T itself.
14213
14214       Check_Anonymous_Access_Types (Component_List (Def));
14215
14216       --  Ada 2005 (AI-251): Complete the initialization of attributes
14217       --  associated with abstract interfaces and decorate the names in the
14218       --  list of ancestor interfaces (if any).
14219
14220       if Ada_Version >= Ada_05
14221         and then Present (Interface_List (Def))
14222       then
14223          declare
14224             Iface     : Node_Id;
14225             Iface_Def : Node_Id;
14226             Iface_Typ : Entity_Id;
14227          begin
14228             Iface := First (Interface_List (Def));
14229
14230             while Present (Iface) loop
14231                Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
14232                Iface_Def := Type_Definition (Parent (Iface_Typ));
14233
14234                if not Is_Interface (Iface_Typ) then
14235                   Error_Msg_NE ("(Ada 2005) & must be an interface",
14236                                 Iface, Iface_Typ);
14237
14238                else
14239                   --  "The declaration of a specific descendant of an
14240                   --  interface type freezes the interface type" RM 13.14
14241
14242                   Freeze_Before (N, Iface_Typ);
14243
14244                   --  Ada 2005 (AI-345): Protected interfaces can only
14245                   --  inherit from limited, synchronized or protected
14246                   --  interfaces.
14247
14248                   if Protected_Present (Def) then
14249                      if Limited_Present (Iface_Def)
14250                        or else Synchronized_Present (Iface_Def)
14251                        or else Protected_Present (Iface_Def)
14252                      then
14253                         null;
14254
14255                      elsif Task_Present (Iface_Def) then
14256                         Error_Msg_N ("(Ada 2005) protected interface cannot"
14257                           & " inherit from task interface", Iface);
14258
14259                      else
14260                         Error_Msg_N ("(Ada 2005) protected interface cannot"
14261                           & " inherit from non-limited interface", Iface);
14262                      end if;
14263
14264                   --  Ada 2005 (AI-345): Synchronized interfaces can only
14265                   --  inherit from limited and synchronized.
14266
14267                   elsif Synchronized_Present (Def) then
14268                      if Limited_Present (Iface_Def)
14269                        or else Synchronized_Present (Iface_Def)
14270                      then
14271                         null;
14272
14273                      elsif Protected_Present (Iface_Def) then
14274                         Error_Msg_N ("(Ada 2005) synchronized interface " &
14275                           "cannot inherit from protected interface", Iface);
14276
14277                      elsif Task_Present (Iface_Def) then
14278                         Error_Msg_N ("(Ada 2005) synchronized interface " &
14279                           "cannot inherit from task interface", Iface);
14280
14281                      else
14282                         Error_Msg_N ("(Ada 2005) synchronized interface " &
14283                           "cannot inherit from non-limited interface",
14284                           Iface);
14285                      end if;
14286
14287                   --  Ada 2005 (AI-345): Task interfaces can only inherit
14288                   --  from limited, synchronized or task interfaces.
14289
14290                   elsif Task_Present (Def) then
14291                      if Limited_Present (Iface_Def)
14292                        or else Synchronized_Present (Iface_Def)
14293                        or else Task_Present (Iface_Def)
14294                      then
14295                         null;
14296
14297                      elsif Protected_Present (Iface_Def) then
14298                         Error_Msg_N ("(Ada 2005) task interface cannot" &
14299                           " inherit from protected interface", Iface);
14300
14301                      else
14302                         Error_Msg_N ("(Ada 2005) task interface cannot" &
14303                           " inherit from non-limited interface", Iface);
14304                      end if;
14305                   end if;
14306                end if;
14307
14308                Next (Iface);
14309             end loop;
14310
14311             Set_Abstract_Interfaces (T, New_Elmt_List);
14312             Collect_Interfaces (Type_Definition (N), T);
14313          end;
14314       end if;
14315
14316       --  Records constitute a scope for the component declarations within.
14317       --  The scope is created prior to the processing of these declarations.
14318       --  Discriminants are processed first, so that they are visible when
14319       --  processing the other components. The Ekind of the record type itself
14320       --  is set to E_Record_Type (subtypes appear as E_Record_Subtype).
14321
14322       --  Enter record scope
14323
14324       New_Scope (T);
14325
14326       --  If an incomplete or private type declaration was already given for
14327       --  the type, then this scope already exists, and the discriminants have
14328       --  been declared within. We must verify that the full declaration
14329       --  matches the incomplete one.
14330
14331       Check_Or_Process_Discriminants (N, T, Prev);
14332
14333       Set_Is_Constrained     (T, not Has_Discriminants (T));
14334       Set_Has_Delayed_Freeze (T, True);
14335
14336       --  For tagged types add a manually analyzed component corresponding
14337       --  to the component _tag, the corresponding piece of tree will be
14338       --  expanded as part of the freezing actions if it is not a CPP_Class.
14339
14340       if Is_Tagged then
14341
14342          --  Do not add the tag unless we are in expansion mode
14343
14344          if Expander_Active then
14345             Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
14346             Enter_Name (Tag_Comp);
14347
14348             Set_Is_Tag                    (Tag_Comp);
14349             Set_Is_Aliased                (Tag_Comp);
14350             Set_Ekind                     (Tag_Comp, E_Component);
14351             Set_Etype                     (Tag_Comp, RTE (RE_Tag));
14352             Set_DT_Entry_Count            (Tag_Comp, No_Uint);
14353             Set_Original_Record_Component (Tag_Comp, Tag_Comp);
14354             Init_Component_Location       (Tag_Comp);
14355
14356             --  Ada 2005 (AI-251): Addition of the Tag corresponding to all the
14357             --  implemented interfaces
14358
14359             Add_Interface_Tag_Components (N, T);
14360          end if;
14361
14362          Make_Class_Wide_Type (T);
14363          Set_Primitive_Operations (T, New_Elmt_List);
14364       end if;
14365
14366       --  We must suppress range checks when processing the components
14367       --  of a record in the presence of discriminants, since we don't
14368       --  want spurious checks to be generated during their analysis, but
14369       --  must reset the Suppress_Range_Checks flags after having processed
14370       --  the record definition.
14371
14372       if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
14373          Set_Kill_Range_Checks (T, True);
14374          Record_Type_Definition (Def, Prev);
14375          Set_Kill_Range_Checks (T, False);
14376       else
14377          Record_Type_Definition (Def, Prev);
14378       end if;
14379
14380       --  Exit from record scope
14381
14382       End_Scope;
14383
14384       if Expander_Active
14385         and then Is_Tagged
14386         and then not Is_Empty_List (Interface_List (Def))
14387       then
14388          --  Ada 2005 (AI-251): Derive the interface subprograms of all the
14389          --  implemented interfaces and check if some of the subprograms
14390          --  inherited from the ancestor cover some interface subprogram.
14391
14392          Derive_Interface_Subprograms (T);
14393       end if;
14394    end Record_Type_Declaration;
14395
14396    ----------------------------
14397    -- Record_Type_Definition --
14398    ----------------------------
14399
14400    procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
14401       Component          : Entity_Id;
14402       Ctrl_Components    : Boolean := False;
14403       Final_Storage_Only : Boolean;
14404       T                  : Entity_Id;
14405
14406    begin
14407       if Ekind (Prev_T) = E_Incomplete_Type then
14408          T := Full_View (Prev_T);
14409       else
14410          T := Prev_T;
14411       end if;
14412
14413       Final_Storage_Only := not Is_Controlled (T);
14414
14415       --  If the component list of a record type is defined by the reserved
14416       --  word null and there is no discriminant part, then the record type has
14417       --  no components and all records of the type are null records (RM 3.7)
14418       --  This procedure is also called to process the extension part of a
14419       --  record extension, in which case the current scope may have inherited
14420       --  components.
14421
14422       if No (Def)
14423         or else No (Component_List (Def))
14424         or else Null_Present (Component_List (Def))
14425       then
14426          null;
14427
14428       else
14429          Analyze_Declarations (Component_Items (Component_List (Def)));
14430
14431          if Present (Variant_Part (Component_List (Def))) then
14432             Analyze (Variant_Part (Component_List (Def)));
14433          end if;
14434       end if;
14435
14436       --  After completing the semantic analysis of the record definition,
14437       --  record components, both new and inherited, are accessible. Set
14438       --  their kind accordingly.
14439
14440       Component := First_Entity (Current_Scope);
14441       while Present (Component) loop
14442          if Ekind (Component) = E_Void then
14443             Set_Ekind (Component, E_Component);
14444             Init_Component_Location (Component);
14445          end if;
14446
14447          if Has_Task (Etype (Component)) then
14448             Set_Has_Task (T);
14449          end if;
14450
14451          if Ekind (Component) /= E_Component then
14452             null;
14453
14454          elsif Has_Controlled_Component (Etype (Component))
14455            or else (Chars (Component) /= Name_uParent
14456                     and then Is_Controlled (Etype (Component)))
14457          then
14458             Set_Has_Controlled_Component (T, True);
14459             Final_Storage_Only := Final_Storage_Only
14460               and then Finalize_Storage_Only (Etype (Component));
14461             Ctrl_Components := True;
14462          end if;
14463
14464          Next_Entity (Component);
14465       end loop;
14466
14467       --  A type is Finalize_Storage_Only only if all its controlled
14468       --  components are so.
14469
14470       if Ctrl_Components then
14471          Set_Finalize_Storage_Only (T, Final_Storage_Only);
14472       end if;
14473
14474       --  Place reference to end record on the proper entity, which may
14475       --  be a partial view.
14476
14477       if Present (Def) then
14478          Process_End_Label (Def, 'e', Prev_T);
14479       end if;
14480    end Record_Type_Definition;
14481
14482    ------------------------
14483    -- Replace_Components --
14484    ------------------------
14485
14486    procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
14487       function Process (N : Node_Id) return Traverse_Result;
14488
14489       -------------
14490       -- Process --
14491       -------------
14492
14493       function Process (N : Node_Id) return Traverse_Result is
14494          Comp : Entity_Id;
14495
14496       begin
14497          if Nkind (N) = N_Discriminant_Specification then
14498             Comp := First_Discriminant (Typ);
14499
14500             while Present (Comp) loop
14501                if Chars (Comp) = Chars (Defining_Identifier (N)) then
14502                   Set_Defining_Identifier (N, Comp);
14503                   exit;
14504                end if;
14505
14506                Next_Discriminant (Comp);
14507             end loop;
14508
14509          elsif Nkind (N) = N_Component_Declaration then
14510             Comp := First_Component (Typ);
14511
14512             while Present (Comp) loop
14513                if Chars (Comp) = Chars (Defining_Identifier (N)) then
14514                   Set_Defining_Identifier (N, Comp);
14515                   exit;
14516                end if;
14517
14518                Next_Component (Comp);
14519             end loop;
14520          end if;
14521
14522          return OK;
14523       end Process;
14524
14525       procedure Replace is new Traverse_Proc (Process);
14526
14527    --  Start of processing for Replace_Components
14528
14529    begin
14530       Replace (Decl);
14531    end Replace_Components;
14532
14533    -------------------------------
14534    -- Set_Completion_Referenced --
14535    -------------------------------
14536
14537    procedure Set_Completion_Referenced (E : Entity_Id) is
14538    begin
14539       --  If in main unit, mark entity that is a completion as referenced,
14540       --  warnings go on the partial view when needed.
14541
14542       if In_Extended_Main_Source_Unit (E) then
14543          Set_Referenced (E);
14544       end if;
14545    end Set_Completion_Referenced;
14546
14547    ---------------------
14548    -- Set_Fixed_Range --
14549    ---------------------
14550
14551    --  The range for fixed-point types is complicated by the fact that we
14552    --  do not know the exact end points at the time of the declaration. This
14553    --  is true for three reasons:
14554
14555    --     A size clause may affect the fudging of the end-points
14556    --     A small clause may affect the values of the end-points
14557    --     We try to include the end-points if it does not affect the size
14558
14559    --  This means that the actual end-points must be established at the point
14560    --  when the type is frozen. Meanwhile, we first narrow the range as
14561    --  permitted (so that it will fit if necessary in a small specified size),
14562    --  and then build a range subtree with these narrowed bounds.
14563
14564    --  Set_Fixed_Range constructs the range from real literal values, and sets
14565    --  the range as the Scalar_Range of the given fixed-point type entity.
14566
14567    --  The parent of this range is set to point to the entity so that it is
14568    --  properly hooked into the tree (unlike normal Scalar_Range entries for
14569    --  other scalar types, which are just pointers to the range in the
14570    --  original tree, this would otherwise be an orphan).
14571
14572    --  The tree is left unanalyzed. When the type is frozen, the processing
14573    --  in Freeze.Freeze_Fixed_Point_Type notices that the range is not
14574    --  analyzed, and uses this as an indication that it should complete
14575    --  work on the range (it will know the final small and size values).
14576
14577    procedure Set_Fixed_Range
14578      (E   : Entity_Id;
14579       Loc : Source_Ptr;
14580       Lo  : Ureal;
14581       Hi  : Ureal)
14582    is
14583       S : constant Node_Id :=
14584             Make_Range (Loc,
14585               Low_Bound  => Make_Real_Literal (Loc, Lo),
14586               High_Bound => Make_Real_Literal (Loc, Hi));
14587
14588    begin
14589       Set_Scalar_Range (E, S);
14590       Set_Parent (S, E);
14591    end Set_Fixed_Range;
14592
14593    ----------------------------------
14594    -- Set_Scalar_Range_For_Subtype --
14595    ----------------------------------
14596
14597    procedure Set_Scalar_Range_For_Subtype
14598      (Def_Id : Entity_Id;
14599       R      : Node_Id;
14600       Subt   : Entity_Id)
14601    is
14602       Kind : constant Entity_Kind :=  Ekind (Def_Id);
14603
14604    begin
14605       Set_Scalar_Range (Def_Id, R);
14606
14607       --  We need to link the range into the tree before resolving it so
14608       --  that types that are referenced, including importantly the subtype
14609       --  itself, are properly frozen (Freeze_Expression requires that the
14610       --  expression be properly linked into the tree). Of course if it is
14611       --  already linked in, then we do not disturb the current link.
14612
14613       if No (Parent (R)) then
14614          Set_Parent (R, Def_Id);
14615       end if;
14616
14617       --  Reset the kind of the subtype during analysis of the range, to
14618       --  catch possible premature use in the bounds themselves.
14619
14620       Set_Ekind (Def_Id, E_Void);
14621       Process_Range_Expr_In_Decl (R, Subt);
14622       Set_Ekind (Def_Id, Kind);
14623
14624    end Set_Scalar_Range_For_Subtype;
14625
14626    --------------------------------------------------------
14627    -- Set_Stored_Constraint_From_Discriminant_Constraint --
14628    --------------------------------------------------------
14629
14630    procedure Set_Stored_Constraint_From_Discriminant_Constraint
14631      (E : Entity_Id)
14632    is
14633    begin
14634       --  Make sure set if encountered during Expand_To_Stored_Constraint
14635
14636       Set_Stored_Constraint (E, No_Elist);
14637
14638       --  Give it the right value
14639
14640       if Is_Constrained (E) and then Has_Discriminants (E) then
14641          Set_Stored_Constraint (E,
14642            Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
14643       end if;
14644    end Set_Stored_Constraint_From_Discriminant_Constraint;
14645
14646    -------------------------------------
14647    -- Signed_Integer_Type_Declaration --
14648    -------------------------------------
14649
14650    procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14651       Implicit_Base : Entity_Id;
14652       Base_Typ      : Entity_Id;
14653       Lo_Val        : Uint;
14654       Hi_Val        : Uint;
14655       Errs          : Boolean := False;
14656       Lo            : Node_Id;
14657       Hi            : Node_Id;
14658
14659       function Can_Derive_From (E : Entity_Id) return Boolean;
14660       --  Determine whether given bounds allow derivation from specified type
14661
14662       procedure Check_Bound (Expr : Node_Id);
14663       --  Check bound to make sure it is integral and static. If not, post
14664       --  appropriate error message and set Errs flag
14665
14666       ---------------------
14667       -- Can_Derive_From --
14668       ---------------------
14669
14670       --  Note we check both bounds against both end values, to deal with
14671       --  strange types like ones with a range of 0 .. -12341234.
14672
14673       function Can_Derive_From (E : Entity_Id) return Boolean is
14674          Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
14675          Hi : constant Uint := Expr_Value (Type_High_Bound (E));
14676       begin
14677          return Lo <= Lo_Val and then Lo_Val <= Hi
14678                   and then
14679                 Lo <= Hi_Val and then Hi_Val <= Hi;
14680       end Can_Derive_From;
14681
14682       -----------------
14683       -- Check_Bound --
14684       -----------------
14685
14686       procedure Check_Bound (Expr : Node_Id) is
14687       begin
14688          --  If a range constraint is used as an integer type definition, each
14689          --  bound of the range must be defined by a static expression of some
14690          --  integer type, but the two bounds need not have the same integer
14691          --  type (Negative bounds are allowed.) (RM 3.5.4)
14692
14693          if not Is_Integer_Type (Etype (Expr)) then
14694             Error_Msg_N
14695               ("integer type definition bounds must be of integer type", Expr);
14696             Errs := True;
14697
14698          elsif not Is_OK_Static_Expression (Expr) then
14699             Flag_Non_Static_Expr
14700               ("non-static expression used for integer type bound!", Expr);
14701             Errs := True;
14702
14703          --  The bounds are folded into literals, and we set their type to be
14704          --  universal, to avoid typing difficulties: we cannot set the type
14705          --  of the literal to the new type, because this would be a forward
14706          --  reference for the back end,  and if the original type is user-
14707          --  defined this can lead to spurious semantic errors (e.g. 2928-003).
14708
14709          else
14710             if Is_Entity_Name (Expr) then
14711                Fold_Uint (Expr, Expr_Value (Expr), True);
14712             end if;
14713
14714             Set_Etype (Expr, Universal_Integer);
14715          end if;
14716       end Check_Bound;
14717
14718    --  Start of processing for Signed_Integer_Type_Declaration
14719
14720    begin
14721       --  Create an anonymous base type
14722
14723       Implicit_Base :=
14724         Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
14725
14726       --  Analyze and check the bounds, they can be of any integer type
14727
14728       Lo := Low_Bound (Def);
14729       Hi := High_Bound (Def);
14730
14731       --  Arbitrarily use Integer as the type if either bound had an error
14732
14733       if Hi = Error or else Lo = Error then
14734          Base_Typ := Any_Integer;
14735          Set_Error_Posted (T, True);
14736
14737       --  Here both bounds are OK expressions
14738
14739       else
14740          Analyze_And_Resolve (Lo, Any_Integer);
14741          Analyze_And_Resolve (Hi, Any_Integer);
14742
14743          Check_Bound (Lo);
14744          Check_Bound (Hi);
14745
14746          if Errs then
14747             Hi := Type_High_Bound (Standard_Long_Long_Integer);
14748             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
14749          end if;
14750
14751          --  Find type to derive from
14752
14753          Lo_Val := Expr_Value (Lo);
14754          Hi_Val := Expr_Value (Hi);
14755
14756          if Can_Derive_From (Standard_Short_Short_Integer) then
14757             Base_Typ := Base_Type (Standard_Short_Short_Integer);
14758
14759          elsif Can_Derive_From (Standard_Short_Integer) then
14760             Base_Typ := Base_Type (Standard_Short_Integer);
14761
14762          elsif Can_Derive_From (Standard_Integer) then
14763             Base_Typ := Base_Type (Standard_Integer);
14764
14765          elsif Can_Derive_From (Standard_Long_Integer) then
14766             Base_Typ := Base_Type (Standard_Long_Integer);
14767
14768          elsif Can_Derive_From (Standard_Long_Long_Integer) then
14769             Base_Typ := Base_Type (Standard_Long_Long_Integer);
14770
14771          else
14772             Base_Typ := Base_Type (Standard_Long_Long_Integer);
14773             Error_Msg_N ("integer type definition bounds out of range", Def);
14774             Hi := Type_High_Bound (Standard_Long_Long_Integer);
14775             Lo := Type_Low_Bound (Standard_Long_Long_Integer);
14776          end if;
14777       end if;
14778
14779       --  Complete both implicit base and declared first subtype entities
14780
14781       Set_Etype          (Implicit_Base, Base_Typ);
14782       Set_Scalar_Range   (Implicit_Base, Scalar_Range   (Base_Typ));
14783       Set_Size_Info      (Implicit_Base,                (Base_Typ));
14784       Set_RM_Size        (Implicit_Base, RM_Size        (Base_Typ));
14785       Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
14786
14787       Set_Ekind          (T, E_Signed_Integer_Subtype);
14788       Set_Etype          (T, Implicit_Base);
14789
14790       Set_Size_Info      (T,                (Implicit_Base));
14791       Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
14792       Set_Scalar_Range   (T, Def);
14793       Set_RM_Size        (T, UI_From_Int (Minimum_Size (T)));
14794       Set_Is_Constrained (T);
14795    end Signed_Integer_Type_Declaration;
14796
14797 end Sem_Ch3;