1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2009, Free Software Foundation, Inc. --
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 3, 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 COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Atree; use Atree;
27 with Checks; use Checks;
28 with Debug; use Debug;
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_Ch9; use Exp_Ch9;
35 with Exp_Disp; use Exp_Disp;
36 with Exp_Dist; use Exp_Dist;
37 with Exp_Tss; use Exp_Tss;
38 with Exp_Util; use Exp_Util;
39 with Fname; use Fname;
40 with Freeze; use Freeze;
41 with Itypes; use Itypes;
42 with Layout; use Layout;
44 with Lib.Xref; use Lib.Xref;
45 with Namet; use Namet;
46 with Nmake; use Nmake;
48 with Restrict; use Restrict;
49 with Rident; use Rident;
50 with Rtsfind; use Rtsfind;
52 with Sem_Aux; use Sem_Aux;
53 with Sem_Case; use Sem_Case;
54 with Sem_Cat; use Sem_Cat;
55 with Sem_Ch6; use Sem_Ch6;
56 with Sem_Ch7; use Sem_Ch7;
57 with Sem_Ch8; use Sem_Ch8;
58 with Sem_Ch13; use Sem_Ch13;
59 with Sem_Disp; use Sem_Disp;
60 with Sem_Dist; use Sem_Dist;
61 with Sem_Elim; use Sem_Elim;
62 with Sem_Eval; use Sem_Eval;
63 with Sem_Mech; use Sem_Mech;
64 with Sem_Res; use Sem_Res;
65 with Sem_Smem; use Sem_Smem;
66 with Sem_Type; use Sem_Type;
67 with Sem_Util; use Sem_Util;
68 with Sem_Warn; use Sem_Warn;
69 with Stand; use Stand;
70 with Sinfo; use Sinfo;
71 with Snames; use Snames;
72 with Targparm; use Targparm;
73 with Tbuild; use Tbuild;
74 with Ttypes; use Ttypes;
75 with Uintp; use Uintp;
76 with Urealp; use Urealp;
78 package body Sem_Ch3 is
80 -----------------------
81 -- Local Subprograms --
82 -----------------------
84 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
85 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
86 -- abstract interface types implemented by a record type or a derived
89 procedure Build_Derived_Type
91 Parent_Type : Entity_Id;
92 Derived_Type : Entity_Id;
93 Is_Completion : Boolean;
94 Derive_Subps : Boolean := True);
95 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
96 -- the N_Full_Type_Declaration node containing the derived type definition.
97 -- Parent_Type is the entity for the parent type in the derived type
98 -- definition and Derived_Type the actual derived type. Is_Completion must
99 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
100 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
101 -- completion of a private type declaration. If Is_Completion is set to
102 -- True, N is the completion of a private type declaration and Derived_Type
103 -- is different from the defining identifier inside N (i.e. Derived_Type /=
104 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
105 -- subprograms should be derived. The only case where this parameter is
106 -- False is when Build_Derived_Type is recursively called to process an
107 -- implicit derived full type for a type derived from a private type (in
108 -- that case the subprograms must only be derived for the private view of
111 -- ??? These flags need a bit of re-examination and re-documentation:
112 -- ??? are they both necessary (both seem related to the recursion)?
114 procedure Build_Derived_Access_Type
116 Parent_Type : Entity_Id;
117 Derived_Type : Entity_Id);
118 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
119 -- create an implicit base if the parent type is constrained or if the
120 -- subtype indication has a constraint.
122 procedure Build_Derived_Array_Type
124 Parent_Type : Entity_Id;
125 Derived_Type : Entity_Id);
126 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
127 -- create an implicit base if the parent type is constrained or if the
128 -- subtype indication has a constraint.
130 procedure Build_Derived_Concurrent_Type
132 Parent_Type : Entity_Id;
133 Derived_Type : Entity_Id);
134 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
135 -- protected type, inherit entries and protected subprograms, check
136 -- legality of discriminant constraints if any.
138 procedure Build_Derived_Enumeration_Type
140 Parent_Type : Entity_Id;
141 Derived_Type : Entity_Id);
142 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
143 -- type, we must create a new list of literals. Types derived from
144 -- Character and [Wide_]Wide_Character are special-cased.
146 procedure Build_Derived_Numeric_Type
148 Parent_Type : Entity_Id;
149 Derived_Type : Entity_Id);
150 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
151 -- an anonymous base type, and propagate constraint to subtype if needed.
153 procedure Build_Derived_Private_Type
155 Parent_Type : Entity_Id;
156 Derived_Type : Entity_Id;
157 Is_Completion : Boolean;
158 Derive_Subps : Boolean := True);
159 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
160 -- because the parent may or may not have a completion, and the derivation
161 -- may itself be a completion.
163 procedure Build_Derived_Record_Type
165 Parent_Type : Entity_Id;
166 Derived_Type : Entity_Id;
167 Derive_Subps : Boolean := True);
168 -- Subsidiary procedure for Build_Derived_Type and
169 -- Analyze_Private_Extension_Declaration used for tagged and untagged
170 -- record types. All parameters are as in Build_Derived_Type except that
171 -- N, in addition to being an N_Full_Type_Declaration node, can also be an
172 -- N_Private_Extension_Declaration node. See the definition of this routine
173 -- for much more info. Derive_Subps indicates whether subprograms should
174 -- be derived from the parent type. The only case where Derive_Subps is
175 -- False is for an implicit derived full type for a type derived from a
176 -- private type (see Build_Derived_Type).
178 procedure Build_Discriminal (Discrim : Entity_Id);
179 -- Create the discriminal corresponding to discriminant Discrim, that is
180 -- the parameter corresponding to Discrim to be used in initialization
181 -- procedures for the type where Discrim is a discriminant. Discriminals
182 -- are not used during semantic analysis, and are not fully defined
183 -- entities until expansion. Thus they are not given a scope until
184 -- initialization procedures are built.
186 function Build_Discriminant_Constraints
189 Derived_Def : Boolean := False) return Elist_Id;
190 -- Validate discriminant constraints and return the list of the constraints
191 -- in order of discriminant declarations, where T is the discriminated
192 -- unconstrained type. Def is the N_Subtype_Indication node where the
193 -- discriminants constraints for T are specified. Derived_Def is True
194 -- when building the discriminant constraints in a derived type definition
195 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
196 -- type and Def is the constraint "(xxx)" on T and this routine sets the
197 -- Corresponding_Discriminant field of the discriminants in the derived
198 -- type D to point to the corresponding discriminants in the parent type T.
200 procedure Build_Discriminated_Subtype
204 Related_Nod : Node_Id;
205 For_Access : Boolean := False);
206 -- Subsidiary procedure to Constrain_Discriminated_Type and to
207 -- Process_Incomplete_Dependents. Given
209 -- T (a possibly discriminated base type)
210 -- Def_Id (a very partially built subtype for T),
212 -- the call completes Def_Id to be the appropriate E_*_Subtype.
214 -- The Elist is the list of discriminant constraints if any (it is set
215 -- to No_Elist if T is not a discriminated type, and to an empty list if
216 -- T has discriminants but there are no discriminant constraints). The
217 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
218 -- The For_Access says whether or not this subtype is really constraining
219 -- an access type. That is its sole purpose is the designated type of an
220 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
221 -- is built to avoid freezing T when the access subtype is frozen.
223 function Build_Scalar_Bound
226 Der_T : Entity_Id) return Node_Id;
227 -- The bounds of a derived scalar type are conversions of the bounds of
228 -- the parent type. Optimize the representation if the bounds are literals.
229 -- Needs a more complete spec--what are the parameters exactly, and what
230 -- exactly is the returned value, and how is Bound affected???
232 procedure Build_Itype_Reference
235 -- Create a reference to an internal type, for use by Gigi. The back-end
236 -- elaborates itypes on demand, i.e. when their first use is seen. This
237 -- can lead to scope anomalies if the first use is within a scope that is
238 -- nested within the scope that contains the point of definition of the
239 -- itype. The Itype_Reference node forces the elaboration of the itype
240 -- in the proper scope. The node is inserted after Nod, which is the
241 -- enclosing declaration that generated Ityp.
243 -- A related mechanism is used during expansion, for itypes created in
244 -- branches of conditionals. See Ensure_Defined in exp_util.
245 -- Could both mechanisms be merged ???
247 procedure Build_Underlying_Full_View
251 -- If the completion of a private type is itself derived from a private
252 -- type, or if the full view of a private subtype is itself private, the
253 -- back-end has no way to compute the actual size of this type. We build
254 -- an internal subtype declaration of the proper parent type to convey
255 -- this information. This extra mechanism is needed because a full
256 -- view cannot itself have a full view (it would get clobbered during
259 procedure Check_Access_Discriminant_Requires_Limited
262 -- Check the restriction that the type to which an access discriminant
263 -- belongs must be a concurrent type or a descendant of a type with
264 -- the reserved word 'limited' in its declaration.
266 procedure Check_Anonymous_Access_Components
270 Comp_List : Node_Id);
271 -- Ada 2005 AI-382: an access component in a record definition can refer to
272 -- the enclosing record, in which case it denotes the type itself, and not
273 -- the current instance of the type. We create an anonymous access type for
274 -- the component, and flag it as an access to a component, so accessibility
275 -- checks are properly performed on it. The declaration of the access type
276 -- is placed ahead of that of the record to prevent order-of-elaboration
277 -- circularity issues in Gigi. We create an incomplete type for the record
278 -- declaration, which is the designated type of the anonymous access.
280 procedure Check_Delta_Expression (E : Node_Id);
281 -- Check that the expression represented by E is suitable for use as a
282 -- delta expression, i.e. it is of real type and is static.
284 procedure Check_Digits_Expression (E : Node_Id);
285 -- Check that the expression represented by E is suitable for use as a
286 -- digits expression, i.e. it is of integer type, positive and static.
288 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
289 -- Validate the initialization of an object declaration. T is the required
290 -- type, and Exp is the initialization expression.
292 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
293 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
295 procedure Check_Or_Process_Discriminants
298 Prev : Entity_Id := Empty);
299 -- If T is the full declaration of an incomplete or private type, check the
300 -- conformance of the discriminants, otherwise process them. Prev is the
301 -- entity of the partial declaration, if any.
303 procedure Check_Real_Bound (Bound : Node_Id);
304 -- Check given bound for being of real type and static. If not, post an
305 -- appropriate message, and rewrite the bound with the real literal zero.
307 procedure Constant_Redeclaration
311 -- Various checks on legality of full declaration of deferred constant.
312 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
313 -- node. The caller has not yet set any attributes of this entity.
315 function Contain_Interface
317 Ifaces : Elist_Id) return Boolean;
318 -- Ada 2005: Determine whether Iface is present in the list Ifaces
320 procedure Convert_Scalar_Bounds
322 Parent_Type : Entity_Id;
323 Derived_Type : Entity_Id;
325 -- For derived scalar types, convert the bounds in the type definition to
326 -- the derived type, and complete their analysis. Given a constraint of the
327 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
328 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
329 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
330 -- subtype are conversions of those bounds to the derived_type, so that
331 -- their typing is consistent.
333 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
334 -- Copies attributes from array base type T2 to array base type T1. Copies
335 -- only attributes that apply to base types, but not subtypes.
337 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
338 -- Copies attributes from array subtype T2 to array subtype T1. Copies
339 -- attributes that apply to both subtypes and base types.
341 procedure Create_Constrained_Components
345 Constraints : Elist_Id);
346 -- Build the list of entities for a constrained discriminated record
347 -- subtype. If a component depends on a discriminant, replace its subtype
348 -- using the discriminant values in the discriminant constraint. Subt
349 -- is the defining identifier for the subtype whose list of constrained
350 -- entities we will create. Decl_Node is the type declaration node where
351 -- we will attach all the itypes created. Typ is the base discriminated
352 -- type for the subtype Subt. Constraints is the list of discriminant
353 -- constraints for Typ.
355 function Constrain_Component_Type
357 Constrained_Typ : Entity_Id;
358 Related_Node : Node_Id;
360 Constraints : Elist_Id) return Entity_Id;
361 -- Given a discriminated base type Typ, a list of discriminant constraint
362 -- Constraints for Typ and a component of Typ, with type Compon_Type,
363 -- create and return the type corresponding to Compon_type where all
364 -- discriminant references are replaced with the corresponding constraint.
365 -- If no discriminant references occur in Compon_Typ then return it as is.
366 -- Constrained_Typ is the final constrained subtype to which the
367 -- constrained Compon_Type belongs. Related_Node is the node where we will
368 -- attach all the itypes created.
370 -- Above description is confused, what is Compon_Type???
372 procedure Constrain_Access
373 (Def_Id : in out Entity_Id;
375 Related_Nod : Node_Id);
376 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
377 -- an anonymous type created for a subtype indication. In that case it is
378 -- created in the procedure and attached to Related_Nod.
380 procedure Constrain_Array
381 (Def_Id : in out Entity_Id;
383 Related_Nod : Node_Id;
384 Related_Id : Entity_Id;
386 -- Apply a list of index constraints to an unconstrained array type. The
387 -- first parameter is the entity for the resulting subtype. A value of
388 -- Empty for Def_Id indicates that an implicit type must be created, but
389 -- creation is delayed (and must be done by this procedure) because other
390 -- subsidiary implicit types must be created first (which is why Def_Id
391 -- is an in/out parameter). The second parameter is a subtype indication
392 -- node for the constrained array to be created (e.g. something of the
393 -- form string (1 .. 10)). Related_Nod gives the place where this type
394 -- has to be inserted in the tree. The Related_Id and Suffix parameters
395 -- are used to build the associated Implicit type name.
397 procedure Constrain_Concurrent
398 (Def_Id : in out Entity_Id;
400 Related_Nod : Node_Id;
401 Related_Id : Entity_Id;
403 -- Apply list of discriminant constraints to an unconstrained concurrent
406 -- SI is the N_Subtype_Indication node containing the constraint and
407 -- the unconstrained type to constrain.
409 -- Def_Id is the entity for the resulting constrained subtype. A value
410 -- of Empty for Def_Id indicates that an implicit type must be created,
411 -- but creation is delayed (and must be done by this procedure) because
412 -- other subsidiary implicit types must be created first (which is why
413 -- Def_Id is an in/out parameter).
415 -- Related_Nod gives the place where this type has to be inserted
418 -- The last two arguments are used to create its external name if needed.
420 function Constrain_Corresponding_Record
421 (Prot_Subt : Entity_Id;
422 Corr_Rec : Entity_Id;
423 Related_Nod : Node_Id;
424 Related_Id : Entity_Id) return Entity_Id;
425 -- When constraining a protected type or task type with discriminants,
426 -- constrain the corresponding record with the same discriminant values.
428 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
429 -- Constrain a decimal fixed point type with a digits constraint and/or a
430 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
432 procedure Constrain_Discriminated_Type
435 Related_Nod : Node_Id;
436 For_Access : Boolean := False);
437 -- Process discriminant constraints of composite type. Verify that values
438 -- have been provided for all discriminants, that the original type is
439 -- unconstrained, and that the types of the supplied expressions match
440 -- the discriminant types. The first three parameters are like in routine
441 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
444 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
445 -- Constrain an enumeration type with a range constraint. This is identical
446 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
448 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
449 -- Constrain a floating point type with either a digits constraint
450 -- and/or a range constraint, building a E_Floating_Point_Subtype.
452 procedure Constrain_Index
455 Related_Nod : Node_Id;
456 Related_Id : Entity_Id;
459 -- Process an index constraint in a constrained array declaration. The
460 -- constraint can be a subtype name, or a range with or without an explicit
461 -- subtype mark. The index is the corresponding index of the unconstrained
462 -- array. The Related_Id and Suffix parameters are used to build the
463 -- associated Implicit type name.
465 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
466 -- Build subtype of a signed or modular integer type
468 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
469 -- Constrain an ordinary fixed point type with a range constraint, and
470 -- build an E_Ordinary_Fixed_Point_Subtype entity.
472 procedure Copy_And_Swap (Priv, Full : Entity_Id);
473 -- Copy the Priv entity into the entity of its full declaration then swap
474 -- the two entities in such a manner that the former private type is now
475 -- seen as a full type.
477 procedure Decimal_Fixed_Point_Type_Declaration
480 -- Create a new decimal fixed point type, and apply the constraint to
481 -- obtain a subtype of this new type.
483 procedure Complete_Private_Subtype
486 Full_Base : Entity_Id;
487 Related_Nod : Node_Id);
488 -- Complete the implicit full view of a private subtype by setting the
489 -- appropriate semantic fields. If the full view of the parent is a record
490 -- type, build constrained components of subtype.
492 procedure Derive_Progenitor_Subprograms
493 (Parent_Type : Entity_Id;
494 Tagged_Type : Entity_Id);
495 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
496 -- operations of progenitors of Tagged_Type, and replace the subsidiary
497 -- subtypes with Tagged_Type, to build the specs of the inherited interface
498 -- primitives. The derived primitives are aliased to those of the
499 -- interface. This routine takes care also of transferring to the full-view
500 -- subprograms associated with the partial-view of Tagged_Type that cover
501 -- interface primitives.
503 procedure Derived_Standard_Character
505 Parent_Type : Entity_Id;
506 Derived_Type : Entity_Id);
507 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
508 -- derivations from types Standard.Character and Standard.Wide_Character.
510 procedure Derived_Type_Declaration
513 Is_Completion : Boolean);
514 -- Process a derived type declaration. Build_Derived_Type is invoked
515 -- to process the actual derived type definition. Parameters N and
516 -- Is_Completion have the same meaning as in Build_Derived_Type.
517 -- T is the N_Defining_Identifier for the entity defined in the
518 -- N_Full_Type_Declaration node N, that is T is the derived type.
520 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
521 -- Insert each literal in symbol table, as an overloadable identifier. Each
522 -- enumeration type is mapped into a sequence of integers, and each literal
523 -- is defined as a constant with integer value. If any of the literals are
524 -- character literals, the type is a character type, which means that
525 -- strings are legal aggregates for arrays of components of the type.
527 function Expand_To_Stored_Constraint
529 Constraint : Elist_Id) return Elist_Id;
530 -- Given a constraint (i.e. a list of expressions) on the discriminants of
531 -- Typ, expand it into a constraint on the stored discriminants and return
532 -- the new list of expressions constraining the stored discriminants.
534 function Find_Type_Of_Object
536 Related_Nod : Node_Id) return Entity_Id;
537 -- Get type entity for object referenced by Obj_Def, attaching the
538 -- implicit types generated to Related_Nod
540 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
541 -- Create a new float and apply the constraint to obtain subtype of it
543 function Has_Range_Constraint (N : Node_Id) return Boolean;
544 -- Given an N_Subtype_Indication node N, return True if a range constraint
545 -- is present, either directly, or as part of a digits or delta constraint.
546 -- In addition, a digits constraint in the decimal case returns True, since
547 -- it establishes a default range if no explicit range is present.
549 function Inherit_Components
551 Parent_Base : Entity_Id;
552 Derived_Base : Entity_Id;
554 Inherit_Discr : Boolean;
555 Discs : Elist_Id) return Elist_Id;
556 -- Called from Build_Derived_Record_Type to inherit the components of
557 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
558 -- For more information on derived types and component inheritance please
559 -- consult the comment above the body of Build_Derived_Record_Type.
561 -- N is the original derived type declaration
563 -- Is_Tagged is set if we are dealing with tagged types
565 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
566 -- Parent_Base, otherwise no discriminants are inherited.
568 -- Discs gives the list of constraints that apply to Parent_Base in the
569 -- derived type declaration. If Discs is set to No_Elist, then we have
570 -- the following situation:
572 -- type Parent (D1..Dn : ..) is [tagged] record ...;
573 -- type Derived is new Parent [with ...];
575 -- which gets treated as
577 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
579 -- For untagged types the returned value is an association list. The list
580 -- starts from the association (Parent_Base => Derived_Base), and then it
581 -- contains a sequence of the associations of the form
583 -- (Old_Component => New_Component),
585 -- where Old_Component is the Entity_Id of a component in Parent_Base and
586 -- New_Component is the Entity_Id of the corresponding component in
587 -- Derived_Base. For untagged records, this association list is needed when
588 -- copying the record declaration for the derived base. In the tagged case
589 -- the value returned is irrelevant.
591 function Is_Progenitor
593 Typ : Entity_Id) return Boolean;
594 -- Determine whether type Typ implements interface Iface. This requires
595 -- traversing the list of abstract interfaces of the type, as well as that
596 -- of the ancestor types. The predicate is used to determine when a formal
597 -- in the signature of an inherited operation must carry the derived type.
599 function Is_Valid_Constraint_Kind
601 Constraint_Kind : Node_Kind) return Boolean;
602 -- Returns True if it is legal to apply the given kind of constraint to the
603 -- given kind of type (index constraint to an array type, for example).
605 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
606 -- Create new modular type. Verify that modulus is in bounds and is
607 -- a power of two (implementation restriction).
609 procedure New_Concatenation_Op (Typ : Entity_Id);
610 -- Create an abbreviated declaration for an operator in order to
611 -- materialize concatenation on array types.
613 procedure Ordinary_Fixed_Point_Type_Declaration
616 -- Create a new ordinary fixed point type, and apply the constraint to
617 -- obtain subtype of it.
619 procedure Prepare_Private_Subtype_Completion
621 Related_Nod : Node_Id);
622 -- Id is a subtype of some private type. Creates the full declaration
623 -- associated with Id whenever possible, i.e. when the full declaration
624 -- of the base type is already known. Records each subtype into
625 -- Private_Dependents of the base type.
627 procedure Process_Incomplete_Dependents
631 -- Process all entities that depend on an incomplete type. There include
632 -- subtypes, subprogram types that mention the incomplete type in their
633 -- profiles, and subprogram with access parameters that designate the
636 -- Inc_T is the defining identifier of an incomplete type declaration, its
637 -- Ekind is E_Incomplete_Type.
639 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
641 -- Full_T is N's defining identifier.
643 -- Subtypes of incomplete types with discriminants are completed when the
644 -- parent type is. This is simpler than private subtypes, because they can
645 -- only appear in the same scope, and there is no need to exchange views.
646 -- Similarly, access_to_subprogram types may have a parameter or a return
647 -- type that is an incomplete type, and that must be replaced with the
650 -- If the full type is tagged, subprogram with access parameters that
651 -- designated the incomplete may be primitive operations of the full type,
652 -- and have to be processed accordingly.
654 procedure Process_Real_Range_Specification (Def : Node_Id);
655 -- Given the type definition for a real type, this procedure processes and
656 -- checks the real range specification of this type definition if one is
657 -- present. If errors are found, error messages are posted, and the
658 -- Real_Range_Specification of Def is reset to Empty.
660 procedure Record_Type_Declaration
664 -- Process a record type declaration (for both untagged and tagged
665 -- records). Parameters T and N are exactly like in procedure
666 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
667 -- for this routine. If this is the completion of an incomplete type
668 -- declaration, Prev is the entity of the incomplete declaration, used for
669 -- cross-referencing. Otherwise Prev = T.
671 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
672 -- This routine is used to process the actual record type definition (both
673 -- for untagged and tagged records). Def is a record type definition node.
674 -- This procedure analyzes the components in this record type definition.
675 -- Prev_T is the entity for the enclosing record type. It is provided so
676 -- that its Has_Task flag can be set if any of the component have Has_Task
677 -- set. If the declaration is the completion of an incomplete type
678 -- declaration, Prev_T is the original incomplete type, whose full view is
681 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
682 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
683 -- build a copy of the declaration tree of the parent, and we create
684 -- independently the list of components for the derived type. Semantic
685 -- information uses the component entities, but record representation
686 -- clauses are validated on the declaration tree. This procedure replaces
687 -- discriminants and components in the declaration with those that have
688 -- been created by Inherit_Components.
690 procedure Set_Fixed_Range
695 -- Build a range node with the given bounds and set it as the Scalar_Range
696 -- of the given fixed-point type entity. Loc is the source location used
697 -- for the constructed range. See body for further details.
699 procedure Set_Scalar_Range_For_Subtype
703 -- This routine is used to set the scalar range field for a subtype given
704 -- Def_Id, the entity for the subtype, and R, the range expression for the
705 -- scalar range. Subt provides the parent subtype to be used to analyze,
706 -- resolve, and check the given range.
708 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
709 -- Create a new signed integer entity, and apply the constraint to obtain
710 -- the required first named subtype of this type.
712 procedure Set_Stored_Constraint_From_Discriminant_Constraint
714 -- E is some record type. This routine computes E's Stored_Constraint
715 -- from its Discriminant_Constraint.
717 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
718 -- Check that an entity in a list of progenitors is an interface,
719 -- emit error otherwise.
721 -----------------------
722 -- Access_Definition --
723 -----------------------
725 function Access_Definition
726 (Related_Nod : Node_Id;
727 N : Node_Id) return Entity_Id
729 Loc : constant Source_Ptr := Sloc (Related_Nod);
730 Anon_Type : Entity_Id;
731 Anon_Scope : Entity_Id;
732 Desig_Type : Entity_Id;
734 Enclosing_Prot_Type : Entity_Id := Empty;
737 if Is_Entry (Current_Scope)
738 and then Is_Task_Type (Etype (Scope (Current_Scope)))
740 Error_Msg_N ("task entries cannot have access parameters", N);
744 -- Ada 2005: for an object declaration the corresponding anonymous
745 -- type is declared in the current scope.
747 -- If the access definition is the return type of another access to
748 -- function, scope is the current one, because it is the one of the
749 -- current type declaration.
751 if Nkind_In (Related_Nod, N_Object_Declaration,
752 N_Access_Function_Definition)
754 Anon_Scope := Current_Scope;
756 -- For the anonymous function result case, retrieve the scope of the
757 -- function specification's associated entity rather than using the
758 -- current scope. The current scope will be the function itself if the
759 -- formal part is currently being analyzed, but will be the parent scope
760 -- in the case of a parameterless function, and we always want to use
761 -- the function's parent scope. Finally, if the function is a child
762 -- unit, we must traverse the tree to retrieve the proper entity.
764 elsif Nkind (Related_Nod) = N_Function_Specification
765 and then Nkind (Parent (N)) /= N_Parameter_Specification
767 -- If the current scope is a protected type, the anonymous access
768 -- is associated with one of the protected operations, and must
769 -- be available in the scope that encloses the protected declaration.
770 -- Otherwise the type is in the scope enclosing the subprogram.
771 -- If the function has formals, The return type of a subprogram
772 -- declaration is analyzed in the scope of the subprogram (see
773 -- Process_Formals) and thus the protected type, if present, is
774 -- the scope of the current function scope.
776 if Ekind (Current_Scope) = E_Protected_Type then
777 Enclosing_Prot_Type := Current_Scope;
779 elsif Ekind (Current_Scope) = E_Function
780 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
782 Enclosing_Prot_Type := Scope (Current_Scope);
785 if Present (Enclosing_Prot_Type) then
786 Anon_Scope := Scope (Enclosing_Prot_Type);
789 Anon_Scope := Scope (Defining_Entity (Related_Nod));
793 -- For access formals, access components, and access discriminants,
794 -- the scope is that of the enclosing declaration,
796 Anon_Scope := Scope (Current_Scope);
801 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
804 and then Ada_Version >= Ada_05
806 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
809 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
810 -- the corresponding semantic routine
812 if Present (Access_To_Subprogram_Definition (N)) then
813 Access_Subprogram_Declaration
814 (T_Name => Anon_Type,
815 T_Def => Access_To_Subprogram_Definition (N));
817 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
819 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
822 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
825 Set_Can_Use_Internal_Rep
826 (Anon_Type, not Always_Compatible_Rep_On_Target);
828 -- If the anonymous access is associated with a protected operation
829 -- create a reference to it after the enclosing protected definition
830 -- because the itype will be used in the subsequent bodies.
832 if Ekind (Current_Scope) = E_Protected_Type then
833 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
839 Find_Type (Subtype_Mark (N));
840 Desig_Type := Entity (Subtype_Mark (N));
842 Set_Directly_Designated_Type
843 (Anon_Type, Desig_Type);
844 Set_Etype (Anon_Type, Anon_Type);
846 -- Make sure the anonymous access type has size and alignment fields
847 -- set, as required by gigi. This is necessary in the case of the
848 -- Task_Body_Procedure.
850 if not Has_Private_Component (Desig_Type) then
851 Layout_Type (Anon_Type);
854 -- ???The following makes no sense, because Anon_Type is an access type
855 -- and therefore cannot have components, private or otherwise. Hence
856 -- the assertion. Not sure what was meant, here.
857 Set_Depends_On_Private (Anon_Type, Has_Private_Component (Anon_Type));
858 pragma Assert (not Depends_On_Private (Anon_Type));
860 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
861 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
862 -- the null value is allowed. In Ada 95 the null value is never allowed.
864 if Ada_Version >= Ada_05 then
865 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
867 Set_Can_Never_Be_Null (Anon_Type, True);
870 -- The anonymous access type is as public as the discriminated type or
871 -- subprogram that defines it. It is imported (for back-end purposes)
872 -- if the designated type is.
874 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
876 -- Ada 2005 (AI-50217): Propagate the attribute that indicates that the
877 -- designated type comes from the limited view.
879 Set_From_With_Type (Anon_Type, From_With_Type (Desig_Type));
881 -- Ada 2005 (AI-231): Propagate the access-constant attribute
883 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
885 -- The context is either a subprogram declaration, object declaration,
886 -- or an access discriminant, in a private or a full type declaration.
887 -- In the case of a subprogram, if the designated type is incomplete,
888 -- the operation will be a primitive operation of the full type, to be
889 -- updated subsequently. If the type is imported through a limited_with
890 -- clause, the subprogram is not a primitive operation of the type
891 -- (which is declared elsewhere in some other scope).
893 if Ekind (Desig_Type) = E_Incomplete_Type
894 and then not From_With_Type (Desig_Type)
895 and then Is_Overloadable (Current_Scope)
897 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
898 Set_Has_Delayed_Freeze (Current_Scope);
901 -- Ada 2005: if the designated type is an interface that may contain
902 -- tasks, create a Master entity for the declaration. This must be done
903 -- before expansion of the full declaration, because the declaration may
904 -- include an expression that is an allocator, whose expansion needs the
905 -- proper Master for the created tasks.
907 if Nkind (Related_Nod) = N_Object_Declaration
908 and then Expander_Active
910 if Is_Interface (Desig_Type)
911 and then Is_Limited_Record (Desig_Type)
913 Build_Class_Wide_Master (Anon_Type);
915 -- Similarly, if the type is an anonymous access that designates
916 -- tasks, create a master entity for it in the current context.
918 elsif Has_Task (Desig_Type)
919 and then Comes_From_Source (Related_Nod)
921 if not Has_Master_Entity (Current_Scope) then
923 Make_Object_Declaration (Loc,
924 Defining_Identifier =>
925 Make_Defining_Identifier (Loc, Name_uMaster),
926 Constant_Present => True,
928 New_Reference_To (RTE (RE_Master_Id), Loc),
930 Make_Explicit_Dereference (Loc,
931 New_Reference_To (RTE (RE_Current_Master), Loc)));
933 Insert_Before (Related_Nod, Decl);
936 Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
937 Set_Has_Master_Entity (Current_Scope);
939 Build_Master_Renaming (Related_Nod, Anon_Type);
944 -- For a private component of a protected type, it is imperative that
945 -- the back-end elaborate the type immediately after the protected
946 -- declaration, because this type will be used in the declarations
947 -- created for the component within each protected body, so we must
948 -- create an itype reference for it now.
950 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
951 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
953 -- Similarly, if the access definition is the return result of a
954 -- function, create an itype reference for it because it
955 -- will be used within the function body. For a regular function that
956 -- is not a compilation unit, insert reference after the declaration.
957 -- For a protected operation, insert it after the enclosing protected
958 -- type declaration. In either case, do not create a reference for a
959 -- type obtained through a limited_with clause, because this would
960 -- introduce semantic dependencies.
962 elsif Nkind (Related_Nod) = N_Function_Specification
963 and then not From_With_Type (Anon_Type)
965 if Present (Enclosing_Prot_Type) then
966 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
968 elsif Is_List_Member (Parent (Related_Nod))
969 and then Nkind (Parent (N)) /= N_Parameter_Specification
971 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
974 -- Finally, create an itype reference for an object declaration of
975 -- an anonymous access type. This is strictly necessary only for
976 -- deferred constants, but in any case will avoid out-of-scope
977 -- problems in the back-end.
979 elsif Nkind (Related_Nod) = N_Object_Declaration then
980 Build_Itype_Reference (Anon_Type, Related_Nod);
984 end Access_Definition;
986 -----------------------------------
987 -- Access_Subprogram_Declaration --
988 -----------------------------------
990 procedure Access_Subprogram_Declaration
995 procedure Check_For_Premature_Usage (Def : Node_Id);
996 -- Check that type T_Name is not used, directly or recursively, as a
997 -- parameter or a return type in Def. Def is either a subtype, an
998 -- access_definition, or an access_to_subprogram_definition.
1000 -------------------------------
1001 -- Check_For_Premature_Usage --
1002 -------------------------------
1004 procedure Check_For_Premature_Usage (Def : Node_Id) is
1008 -- Check for a subtype mark
1010 if Nkind (Def) in N_Has_Etype then
1011 if Etype (Def) = T_Name then
1013 ("type& cannot be used before end of its declaration", Def);
1016 -- If this is not a subtype, then this is an access_definition
1018 elsif Nkind (Def) = N_Access_Definition then
1019 if Present (Access_To_Subprogram_Definition (Def)) then
1020 Check_For_Premature_Usage
1021 (Access_To_Subprogram_Definition (Def));
1023 Check_For_Premature_Usage (Subtype_Mark (Def));
1026 -- The only cases left are N_Access_Function_Definition and
1027 -- N_Access_Procedure_Definition.
1030 if Present (Parameter_Specifications (Def)) then
1031 Param := First (Parameter_Specifications (Def));
1032 while Present (Param) loop
1033 Check_For_Premature_Usage (Parameter_Type (Param));
1034 Param := Next (Param);
1038 if Nkind (Def) = N_Access_Function_Definition then
1039 Check_For_Premature_Usage (Result_Definition (Def));
1042 end Check_For_Premature_Usage;
1046 Formals : constant List_Id := Parameter_Specifications (T_Def);
1049 Desig_Type : constant Entity_Id :=
1050 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1052 -- Start of processing for Access_Subprogram_Declaration
1055 -- Associate the Itype node with the inner full-type declaration or
1056 -- subprogram spec. This is required to handle nested anonymous
1057 -- declarations. For example:
1060 -- (X : access procedure
1061 -- (Y : access procedure
1064 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1065 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1066 N_Private_Type_Declaration,
1067 N_Private_Extension_Declaration,
1068 N_Procedure_Specification,
1069 N_Function_Specification)
1071 Nkind_In (D_Ityp, N_Object_Declaration,
1072 N_Object_Renaming_Declaration,
1073 N_Formal_Object_Declaration,
1074 N_Formal_Type_Declaration,
1075 N_Task_Type_Declaration,
1076 N_Protected_Type_Declaration))
1078 D_Ityp := Parent (D_Ityp);
1079 pragma Assert (D_Ityp /= Empty);
1082 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1084 if Nkind_In (D_Ityp, N_Procedure_Specification,
1085 N_Function_Specification)
1087 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1089 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1090 N_Object_Declaration,
1091 N_Object_Renaming_Declaration,
1092 N_Formal_Type_Declaration)
1094 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1097 if Nkind (T_Def) = N_Access_Function_Definition then
1098 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1100 Acc : constant Node_Id := Result_Definition (T_Def);
1103 if Present (Access_To_Subprogram_Definition (Acc))
1105 Protected_Present (Access_To_Subprogram_Definition (Acc))
1109 Replace_Anonymous_Access_To_Protected_Subprogram
1115 Access_Definition (T_Def, Result_Definition (T_Def)));
1120 Analyze (Result_Definition (T_Def));
1123 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1126 -- If a null exclusion is imposed on the result type, then
1127 -- create a null-excluding itype (an access subtype) and use
1128 -- it as the function's Etype.
1130 if Is_Access_Type (Typ)
1131 and then Null_Exclusion_In_Return_Present (T_Def)
1133 Set_Etype (Desig_Type,
1134 Create_Null_Excluding_Itype
1136 Related_Nod => T_Def,
1137 Scope_Id => Current_Scope));
1140 if From_With_Type (Typ) then
1142 ("illegal use of incomplete type&",
1143 Result_Definition (T_Def), Typ);
1145 elsif Ekind (Current_Scope) = E_Package
1146 and then In_Private_Part (Current_Scope)
1148 if Ekind (Typ) = E_Incomplete_Type then
1149 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1151 elsif Is_Class_Wide_Type (Typ)
1152 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1155 (Desig_Type, Private_Dependents (Etype (Typ)));
1159 Set_Etype (Desig_Type, Typ);
1164 if not (Is_Type (Etype (Desig_Type))) then
1166 ("expect type in function specification",
1167 Result_Definition (T_Def));
1171 Set_Etype (Desig_Type, Standard_Void_Type);
1174 if Present (Formals) then
1175 Push_Scope (Desig_Type);
1177 -- A bit of a kludge here. These kludges will be removed when Itypes
1178 -- have proper parent pointers to their declarations???
1180 -- Kludge 1) Link defining_identifier of formals. Required by
1181 -- First_Formal to provide its functionality.
1187 F := First (Formals);
1188 while Present (F) loop
1189 if No (Parent (Defining_Identifier (F))) then
1190 Set_Parent (Defining_Identifier (F), F);
1197 Process_Formals (Formals, Parent (T_Def));
1199 -- Kludge 2) End_Scope requires that the parent pointer be set to
1200 -- something reasonable, but Itypes don't have parent pointers. So
1201 -- we set it and then unset it ???
1203 Set_Parent (Desig_Type, T_Name);
1205 Set_Parent (Desig_Type, Empty);
1208 -- Check for premature usage of the type being defined
1210 Check_For_Premature_Usage (T_Def);
1212 -- The return type and/or any parameter type may be incomplete. Mark
1213 -- the subprogram_type as depending on the incomplete type, so that
1214 -- it can be updated when the full type declaration is seen. This
1215 -- only applies to incomplete types declared in some enclosing scope,
1216 -- not to limited views from other packages.
1218 if Present (Formals) then
1219 Formal := First_Formal (Desig_Type);
1220 while Present (Formal) loop
1221 if Ekind (Formal) /= E_In_Parameter
1222 and then Nkind (T_Def) = N_Access_Function_Definition
1224 Error_Msg_N ("functions can only have IN parameters", Formal);
1227 if Ekind (Etype (Formal)) = E_Incomplete_Type
1228 and then In_Open_Scopes (Scope (Etype (Formal)))
1230 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1231 Set_Has_Delayed_Freeze (Desig_Type);
1234 Next_Formal (Formal);
1238 -- If the return type is incomplete, this is legal as long as the
1239 -- type is declared in the current scope and will be completed in
1240 -- it (rather than being part of limited view).
1242 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1243 and then not Has_Delayed_Freeze (Desig_Type)
1244 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1246 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1247 Set_Has_Delayed_Freeze (Desig_Type);
1250 Check_Delayed_Subprogram (Desig_Type);
1252 if Protected_Present (T_Def) then
1253 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1254 Set_Convention (Desig_Type, Convention_Protected);
1256 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1259 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1261 Set_Etype (T_Name, T_Name);
1262 Init_Size_Align (T_Name);
1263 Set_Directly_Designated_Type (T_Name, Desig_Type);
1265 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1267 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1269 Check_Restriction (No_Access_Subprograms, T_Def);
1270 end Access_Subprogram_Declaration;
1272 ----------------------------
1273 -- Access_Type_Declaration --
1274 ----------------------------
1276 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1277 S : constant Node_Id := Subtype_Indication (Def);
1278 P : constant Node_Id := Parent (Def);
1280 -- Check for permissible use of incomplete type
1282 if Nkind (S) /= N_Subtype_Indication then
1285 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1286 Set_Directly_Designated_Type (T, Entity (S));
1288 Set_Directly_Designated_Type (T,
1289 Process_Subtype (S, P, T, 'P'));
1293 Set_Directly_Designated_Type (T,
1294 Process_Subtype (S, P, T, 'P'));
1297 if All_Present (Def) or Constant_Present (Def) then
1298 Set_Ekind (T, E_General_Access_Type);
1300 Set_Ekind (T, E_Access_Type);
1303 if Base_Type (Designated_Type (T)) = T then
1304 Error_Msg_N ("access type cannot designate itself", S);
1306 -- In Ada 2005, the type may have a limited view through some unit
1307 -- in its own context, allowing the following circularity that cannot
1308 -- be detected earlier
1310 elsif Is_Class_Wide_Type (Designated_Type (T))
1311 and then Etype (Designated_Type (T)) = T
1314 ("access type cannot designate its own classwide type", S);
1316 -- Clean up indication of tagged status to prevent cascaded errors
1318 Set_Is_Tagged_Type (T, False);
1323 -- If the type has appeared already in a with_type clause, it is
1324 -- frozen and the pointer size is already set. Else, initialize.
1326 if not From_With_Type (T) then
1327 Init_Size_Align (T);
1330 -- Note that Has_Task is always false, since the access type itself
1331 -- is not a task type. See Einfo for more description on this point.
1332 -- Exactly the same consideration applies to Has_Controlled_Component.
1334 Set_Has_Task (T, False);
1335 Set_Has_Controlled_Component (T, False);
1337 -- Initialize Associated_Final_Chain explicitly to Empty, to avoid
1338 -- problems where an incomplete view of this entity has been previously
1339 -- established by a limited with and an overlaid version of this field
1340 -- (Stored_Constraint) was initialized for the incomplete view.
1342 Set_Associated_Final_Chain (T, Empty);
1344 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1347 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1348 Set_Is_Access_Constant (T, Constant_Present (Def));
1349 end Access_Type_Declaration;
1351 ----------------------------------
1352 -- Add_Interface_Tag_Components --
1353 ----------------------------------
1355 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1356 Loc : constant Source_Ptr := Sloc (N);
1360 procedure Add_Tag (Iface : Entity_Id);
1361 -- Add tag for one of the progenitor interfaces
1367 procedure Add_Tag (Iface : Entity_Id) is
1374 pragma Assert (Is_Tagged_Type (Iface)
1375 and then Is_Interface (Iface));
1378 Make_Component_Definition (Loc,
1379 Aliased_Present => True,
1380 Subtype_Indication =>
1381 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1383 Tag := Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1386 Make_Component_Declaration (Loc,
1387 Defining_Identifier => Tag,
1388 Component_Definition => Def);
1390 Analyze_Component_Declaration (Decl);
1392 Set_Analyzed (Decl);
1393 Set_Ekind (Tag, E_Component);
1395 Set_Is_Aliased (Tag);
1396 Set_Related_Type (Tag, Iface);
1397 Init_Component_Location (Tag);
1399 pragma Assert (Is_Frozen (Iface));
1401 Set_DT_Entry_Count (Tag,
1402 DT_Entry_Count (First_Entity (Iface)));
1404 if No (Last_Tag) then
1407 Insert_After (Last_Tag, Decl);
1412 -- If the ancestor has discriminants we need to give special support
1413 -- to store the offset_to_top value of the secondary dispatch tables.
1414 -- For this purpose we add a supplementary component just after the
1415 -- field that contains the tag associated with each secondary DT.
1417 if Typ /= Etype (Typ)
1418 and then Has_Discriminants (Etype (Typ))
1421 Make_Component_Definition (Loc,
1422 Subtype_Indication =>
1423 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1426 Make_Defining_Identifier (Loc, New_Internal_Name ('V'));
1429 Make_Component_Declaration (Loc,
1430 Defining_Identifier => Offset,
1431 Component_Definition => Def);
1433 Analyze_Component_Declaration (Decl);
1435 Set_Analyzed (Decl);
1436 Set_Ekind (Offset, E_Component);
1437 Set_Is_Aliased (Offset);
1438 Set_Related_Type (Offset, Iface);
1439 Init_Component_Location (Offset);
1440 Insert_After (Last_Tag, Decl);
1451 -- Start of processing for Add_Interface_Tag_Components
1454 if not RTE_Available (RE_Interface_Tag) then
1456 ("(Ada 2005) interface types not supported by this run-time!",
1461 if Ekind (Typ) /= E_Record_Type
1462 or else (Is_Concurrent_Record_Type (Typ)
1463 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1464 or else (not Is_Concurrent_Record_Type (Typ)
1465 and then No (Interfaces (Typ))
1466 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1471 -- Find the current last tag
1473 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1474 Ext := Record_Extension_Part (Type_Definition (N));
1476 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1477 Ext := Type_Definition (N);
1482 if not (Present (Component_List (Ext))) then
1483 Set_Null_Present (Ext, False);
1485 Set_Component_List (Ext,
1486 Make_Component_List (Loc,
1487 Component_Items => L,
1488 Null_Present => False));
1490 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1491 L := Component_Items
1493 (Record_Extension_Part
1494 (Type_Definition (N))));
1496 L := Component_Items
1498 (Type_Definition (N)));
1501 -- Find the last tag component
1504 while Present (Comp) loop
1505 if Nkind (Comp) = N_Component_Declaration
1506 and then Is_Tag (Defining_Identifier (Comp))
1515 -- At this point L references the list of components and Last_Tag
1516 -- references the current last tag (if any). Now we add the tag
1517 -- corresponding with all the interfaces that are not implemented
1520 if Present (Interfaces (Typ)) then
1521 Elmt := First_Elmt (Interfaces (Typ));
1522 while Present (Elmt) loop
1523 Add_Tag (Node (Elmt));
1527 end Add_Interface_Tag_Components;
1529 -----------------------------------
1530 -- Analyze_Component_Declaration --
1531 -----------------------------------
1533 procedure Analyze_Component_Declaration (N : Node_Id) is
1534 Id : constant Entity_Id := Defining_Identifier (N);
1535 E : constant Node_Id := Expression (N);
1539 function Contains_POC (Constr : Node_Id) return Boolean;
1540 -- Determines whether a constraint uses the discriminant of a record
1541 -- type thus becoming a per-object constraint (POC).
1543 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1544 -- Typ is the type of the current component, check whether this type is
1545 -- a limited type. Used to validate declaration against that of
1546 -- enclosing record.
1552 function Contains_POC (Constr : Node_Id) return Boolean is
1554 -- Prevent cascaded errors
1556 if Error_Posted (Constr) then
1560 case Nkind (Constr) is
1561 when N_Attribute_Reference =>
1563 Attribute_Name (Constr) = Name_Access
1564 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1566 when N_Discriminant_Association =>
1567 return Denotes_Discriminant (Expression (Constr));
1569 when N_Identifier =>
1570 return Denotes_Discriminant (Constr);
1572 when N_Index_Or_Discriminant_Constraint =>
1577 IDC := First (Constraints (Constr));
1578 while Present (IDC) loop
1580 -- One per-object constraint is sufficient
1582 if Contains_POC (IDC) then
1593 return Denotes_Discriminant (Low_Bound (Constr))
1595 Denotes_Discriminant (High_Bound (Constr));
1597 when N_Range_Constraint =>
1598 return Denotes_Discriminant (Range_Expression (Constr));
1606 ----------------------
1607 -- Is_Known_Limited --
1608 ----------------------
1610 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1611 P : constant Entity_Id := Etype (Typ);
1612 R : constant Entity_Id := Root_Type (Typ);
1615 if Is_Limited_Record (Typ) then
1618 -- If the root type is limited (and not a limited interface)
1619 -- so is the current type
1621 elsif Is_Limited_Record (R)
1623 (not Is_Interface (R)
1624 or else not Is_Limited_Interface (R))
1628 -- Else the type may have a limited interface progenitor, but a
1629 -- limited record parent.
1632 and then Is_Limited_Record (P)
1639 end Is_Known_Limited;
1641 -- Start of processing for Analyze_Component_Declaration
1644 Generate_Definition (Id);
1647 if Present (Subtype_Indication (Component_Definition (N))) then
1648 T := Find_Type_Of_Object
1649 (Subtype_Indication (Component_Definition (N)), N);
1651 -- Ada 2005 (AI-230): Access Definition case
1654 pragma Assert (Present
1655 (Access_Definition (Component_Definition (N))));
1657 T := Access_Definition
1659 N => Access_Definition (Component_Definition (N)));
1660 Set_Is_Local_Anonymous_Access (T);
1662 -- Ada 2005 (AI-254)
1664 if Present (Access_To_Subprogram_Definition
1665 (Access_Definition (Component_Definition (N))))
1666 and then Protected_Present (Access_To_Subprogram_Definition
1668 (Component_Definition (N))))
1670 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1674 -- If the subtype is a constrained subtype of the enclosing record,
1675 -- (which must have a partial view) the back-end does not properly
1676 -- handle the recursion. Rewrite the component declaration with an
1677 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1678 -- the tree directly because side effects have already been removed from
1679 -- discriminant constraints.
1681 if Ekind (T) = E_Access_Subtype
1682 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1683 and then Comes_From_Source (T)
1684 and then Nkind (Parent (T)) = N_Subtype_Declaration
1685 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1688 (Subtype_Indication (Component_Definition (N)),
1689 New_Copy_Tree (Subtype_Indication (Parent (T))));
1690 T := Find_Type_Of_Object
1691 (Subtype_Indication (Component_Definition (N)), N);
1694 -- If the component declaration includes a default expression, then we
1695 -- check that the component is not of a limited type (RM 3.7(5)),
1696 -- and do the special preanalysis of the expression (see section on
1697 -- "Handling of Default and Per-Object Expressions" in the spec of
1701 Preanalyze_Spec_Expression (E, T);
1702 Check_Initialization (T, E);
1704 if Ada_Version >= Ada_05
1705 and then Ekind (T) = E_Anonymous_Access_Type
1706 and then Etype (E) /= Any_Type
1708 -- Check RM 3.9.2(9): "if the expected type for an expression is
1709 -- an anonymous access-to-specific tagged type, then the object
1710 -- designated by the expression shall not be dynamically tagged
1711 -- unless it is a controlling operand in a call on a dispatching
1714 if Is_Tagged_Type (Directly_Designated_Type (T))
1716 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1718 Ekind (Directly_Designated_Type (Etype (E))) =
1722 ("access to specific tagged type required (RM 3.9.2(9))", E);
1725 -- (Ada 2005: AI-230): Accessibility check for anonymous
1728 if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1730 ("expression has deeper access level than component " &
1731 "(RM 3.10.2 (12.2))", E);
1734 -- The initialization expression is a reference to an access
1735 -- discriminant. The type of the discriminant is always deeper
1736 -- than any access type.
1738 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1739 and then Is_Entity_Name (E)
1740 and then Ekind (Entity (E)) = E_In_Parameter
1741 and then Present (Discriminal_Link (Entity (E)))
1744 ("discriminant has deeper accessibility level than target",
1750 -- The parent type may be a private view with unknown discriminants,
1751 -- and thus unconstrained. Regular components must be constrained.
1753 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1754 if Is_Class_Wide_Type (T) then
1756 ("class-wide subtype with unknown discriminants" &
1757 " in component declaration",
1758 Subtype_Indication (Component_Definition (N)));
1761 ("unconstrained subtype in component declaration",
1762 Subtype_Indication (Component_Definition (N)));
1765 -- Components cannot be abstract, except for the special case of
1766 -- the _Parent field (case of extending an abstract tagged type)
1768 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1769 Error_Msg_N ("type of a component cannot be abstract", N);
1773 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1775 -- The component declaration may have a per-object constraint, set
1776 -- the appropriate flag in the defining identifier of the subtype.
1778 if Present (Subtype_Indication (Component_Definition (N))) then
1780 Sindic : constant Node_Id :=
1781 Subtype_Indication (Component_Definition (N));
1783 if Nkind (Sindic) = N_Subtype_Indication
1784 and then Present (Constraint (Sindic))
1785 and then Contains_POC (Constraint (Sindic))
1787 Set_Has_Per_Object_Constraint (Id);
1792 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1793 -- out some static checks.
1795 if Ada_Version >= Ada_05
1796 and then Can_Never_Be_Null (T)
1798 Null_Exclusion_Static_Checks (N);
1801 -- If this component is private (or depends on a private type), flag the
1802 -- record type to indicate that some operations are not available.
1804 P := Private_Component (T);
1808 -- Check for circular definitions
1810 if P = Any_Type then
1811 Set_Etype (Id, Any_Type);
1813 -- There is a gap in the visibility of operations only if the
1814 -- component type is not defined in the scope of the record type.
1816 elsif Scope (P) = Scope (Current_Scope) then
1819 elsif Is_Limited_Type (P) then
1820 Set_Is_Limited_Composite (Current_Scope);
1823 Set_Is_Private_Composite (Current_Scope);
1828 and then Is_Limited_Type (T)
1829 and then Chars (Id) /= Name_uParent
1830 and then Is_Tagged_Type (Current_Scope)
1832 if Is_Derived_Type (Current_Scope)
1833 and then not Is_Known_Limited (Current_Scope)
1836 ("extension of nonlimited type cannot have limited components",
1839 if Is_Interface (Root_Type (Current_Scope)) then
1841 ("\limitedness is not inherited from limited interface", N);
1843 ("\add LIMITED to type indication", N);
1846 Explain_Limited_Type (T, N);
1847 Set_Etype (Id, Any_Type);
1848 Set_Is_Limited_Composite (Current_Scope, False);
1850 elsif not Is_Derived_Type (Current_Scope)
1851 and then not Is_Limited_Record (Current_Scope)
1852 and then not Is_Concurrent_Type (Current_Scope)
1855 ("nonlimited tagged type cannot have limited components", N);
1856 Explain_Limited_Type (T, N);
1857 Set_Etype (Id, Any_Type);
1858 Set_Is_Limited_Composite (Current_Scope, False);
1862 Set_Original_Record_Component (Id, Id);
1863 end Analyze_Component_Declaration;
1865 --------------------------
1866 -- Analyze_Declarations --
1867 --------------------------
1869 procedure Analyze_Declarations (L : List_Id) is
1871 Freeze_From : Entity_Id := Empty;
1872 Next_Node : Node_Id;
1875 -- Adjust D not to include implicit label declarations, since these
1876 -- have strange Sloc values that result in elaboration check problems.
1877 -- (They have the sloc of the label as found in the source, and that
1878 -- is ahead of the current declarative part).
1884 procedure Adjust_D is
1886 while Present (Prev (D))
1887 and then Nkind (D) = N_Implicit_Label_Declaration
1893 -- Start of processing for Analyze_Declarations
1897 while Present (D) loop
1899 -- Complete analysis of declaration
1902 Next_Node := Next (D);
1904 if No (Freeze_From) then
1905 Freeze_From := First_Entity (Current_Scope);
1908 -- At the end of a declarative part, freeze remaining entities
1909 -- declared in it. The end of the visible declarations of package
1910 -- specification is not the end of a declarative part if private
1911 -- declarations are present. The end of a package declaration is a
1912 -- freezing point only if it a library package. A task definition or
1913 -- protected type definition is not a freeze point either. Finally,
1914 -- we do not freeze entities in generic scopes, because there is no
1915 -- code generated for them and freeze nodes will be generated for
1918 -- The end of a package instantiation is not a freeze point, but
1919 -- for now we make it one, because the generic body is inserted
1920 -- (currently) immediately after. Generic instantiations will not
1921 -- be a freeze point once delayed freezing of bodies is implemented.
1922 -- (This is needed in any case for early instantiations ???).
1924 if No (Next_Node) then
1925 if Nkind_In (Parent (L), N_Component_List,
1927 N_Protected_Definition)
1931 elsif Nkind (Parent (L)) /= N_Package_Specification then
1932 if Nkind (Parent (L)) = N_Package_Body then
1933 Freeze_From := First_Entity (Current_Scope);
1937 Freeze_All (Freeze_From, D);
1938 Freeze_From := Last_Entity (Current_Scope);
1940 elsif Scope (Current_Scope) /= Standard_Standard
1941 and then not Is_Child_Unit (Current_Scope)
1942 and then No (Generic_Parent (Parent (L)))
1946 elsif L /= Visible_Declarations (Parent (L))
1947 or else No (Private_Declarations (Parent (L)))
1948 or else Is_Empty_List (Private_Declarations (Parent (L)))
1951 Freeze_All (Freeze_From, D);
1952 Freeze_From := Last_Entity (Current_Scope);
1955 -- If next node is a body then freeze all types before the body.
1956 -- An exception occurs for some expander-generated bodies. If these
1957 -- are generated at places where in general language rules would not
1958 -- allow a freeze point, then we assume that the expander has
1959 -- explicitly checked that all required types are properly frozen,
1960 -- and we do not cause general freezing here. This special circuit
1961 -- is used when the encountered body is marked as having already
1964 -- In all other cases (bodies that come from source, and expander
1965 -- generated bodies that have not been analyzed yet), freeze all
1966 -- types now. Note that in the latter case, the expander must take
1967 -- care to attach the bodies at a proper place in the tree so as to
1968 -- not cause unwanted freezing at that point.
1970 elsif not Analyzed (Next_Node)
1971 and then (Nkind_In (Next_Node, N_Subprogram_Body,
1977 Nkind (Next_Node) in N_Body_Stub)
1980 Freeze_All (Freeze_From, D);
1981 Freeze_From := Last_Entity (Current_Scope);
1986 end Analyze_Declarations;
1988 ----------------------------------
1989 -- Analyze_Incomplete_Type_Decl --
1990 ----------------------------------
1992 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
1993 F : constant Boolean := Is_Pure (Current_Scope);
1997 Generate_Definition (Defining_Identifier (N));
1999 -- Process an incomplete declaration. The identifier must not have been
2000 -- declared already in the scope. However, an incomplete declaration may
2001 -- appear in the private part of a package, for a private type that has
2002 -- already been declared.
2004 -- In this case, the discriminants (if any) must match
2006 T := Find_Type_Name (N);
2008 Set_Ekind (T, E_Incomplete_Type);
2009 Init_Size_Align (T);
2010 Set_Is_First_Subtype (T, True);
2013 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2014 -- incomplete types.
2016 if Tagged_Present (N) then
2017 Set_Is_Tagged_Type (T);
2018 Make_Class_Wide_Type (T);
2019 Set_Primitive_Operations (T, New_Elmt_List);
2024 Set_Stored_Constraint (T, No_Elist);
2026 if Present (Discriminant_Specifications (N)) then
2027 Process_Discriminants (N);
2032 -- If the type has discriminants, non-trivial subtypes may be
2033 -- declared before the full view of the type. The full views of those
2034 -- subtypes will be built after the full view of the type.
2036 Set_Private_Dependents (T, New_Elmt_List);
2038 end Analyze_Incomplete_Type_Decl;
2040 -----------------------------------
2041 -- Analyze_Interface_Declaration --
2042 -----------------------------------
2044 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2045 CW : constant Entity_Id := Class_Wide_Type (T);
2048 Set_Is_Tagged_Type (T);
2050 Set_Is_Limited_Record (T, Limited_Present (Def)
2051 or else Task_Present (Def)
2052 or else Protected_Present (Def)
2053 or else Synchronized_Present (Def));
2055 -- Type is abstract if full declaration carries keyword, or if previous
2056 -- partial view did.
2058 Set_Is_Abstract_Type (T);
2059 Set_Is_Interface (T);
2061 -- Type is a limited interface if it includes the keyword limited, task,
2062 -- protected, or synchronized.
2064 Set_Is_Limited_Interface
2065 (T, Limited_Present (Def)
2066 or else Protected_Present (Def)
2067 or else Synchronized_Present (Def)
2068 or else Task_Present (Def));
2070 Set_Is_Protected_Interface (T, Protected_Present (Def));
2071 Set_Is_Task_Interface (T, Task_Present (Def));
2073 -- Type is a synchronized interface if it includes the keyword task,
2074 -- protected, or synchronized.
2076 Set_Is_Synchronized_Interface
2077 (T, Synchronized_Present (Def)
2078 or else Protected_Present (Def)
2079 or else Task_Present (Def));
2081 Set_Interfaces (T, New_Elmt_List);
2082 Set_Primitive_Operations (T, New_Elmt_List);
2084 -- Complete the decoration of the class-wide entity if it was already
2085 -- built (i.e. during the creation of the limited view)
2087 if Present (CW) then
2088 Set_Is_Interface (CW);
2089 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2090 Set_Is_Protected_Interface (CW, Is_Protected_Interface (T));
2091 Set_Is_Synchronized_Interface (CW, Is_Synchronized_Interface (T));
2092 Set_Is_Task_Interface (CW, Is_Task_Interface (T));
2095 -- Check runtime support for synchronized interfaces
2097 if VM_Target = No_VM
2098 and then (Is_Task_Interface (T)
2099 or else Is_Protected_Interface (T)
2100 or else Is_Synchronized_Interface (T))
2101 and then not RTE_Available (RE_Select_Specific_Data)
2103 Error_Msg_CRT ("synchronized interfaces", T);
2105 end Analyze_Interface_Declaration;
2107 -----------------------------
2108 -- Analyze_Itype_Reference --
2109 -----------------------------
2111 -- Nothing to do. This node is placed in the tree only for the benefit of
2112 -- back end processing, and has no effect on the semantic processing.
2114 procedure Analyze_Itype_Reference (N : Node_Id) is
2116 pragma Assert (Is_Itype (Itype (N)));
2118 end Analyze_Itype_Reference;
2120 --------------------------------
2121 -- Analyze_Number_Declaration --
2122 --------------------------------
2124 procedure Analyze_Number_Declaration (N : Node_Id) is
2125 Id : constant Entity_Id := Defining_Identifier (N);
2126 E : constant Node_Id := Expression (N);
2128 Index : Interp_Index;
2132 Generate_Definition (Id);
2135 -- This is an optimization of a common case of an integer literal
2137 if Nkind (E) = N_Integer_Literal then
2138 Set_Is_Static_Expression (E, True);
2139 Set_Etype (E, Universal_Integer);
2141 Set_Etype (Id, Universal_Integer);
2142 Set_Ekind (Id, E_Named_Integer);
2143 Set_Is_Frozen (Id, True);
2147 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2149 -- Process expression, replacing error by integer zero, to avoid
2150 -- cascaded errors or aborts further along in the processing
2152 -- Replace Error by integer zero, which seems least likely to
2153 -- cause cascaded errors.
2156 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2157 Set_Error_Posted (E);
2162 -- Verify that the expression is static and numeric. If
2163 -- the expression is overloaded, we apply the preference
2164 -- rule that favors root numeric types.
2166 if not Is_Overloaded (E) then
2172 Get_First_Interp (E, Index, It);
2173 while Present (It.Typ) loop
2174 if (Is_Integer_Type (It.Typ)
2175 or else Is_Real_Type (It.Typ))
2176 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2178 if T = Any_Type then
2181 elsif It.Typ = Universal_Real
2182 or else It.Typ = Universal_Integer
2184 -- Choose universal interpretation over any other
2191 Get_Next_Interp (Index, It);
2195 if Is_Integer_Type (T) then
2197 Set_Etype (Id, Universal_Integer);
2198 Set_Ekind (Id, E_Named_Integer);
2200 elsif Is_Real_Type (T) then
2202 -- Because the real value is converted to universal_real, this is a
2203 -- legal context for a universal fixed expression.
2205 if T = Universal_Fixed then
2207 Loc : constant Source_Ptr := Sloc (N);
2208 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2210 New_Occurrence_Of (Universal_Real, Loc),
2211 Expression => Relocate_Node (E));
2218 elsif T = Any_Fixed then
2219 Error_Msg_N ("illegal context for mixed mode operation", E);
2221 -- Expression is of the form : universal_fixed * integer. Try to
2222 -- resolve as universal_real.
2224 T := Universal_Real;
2229 Set_Etype (Id, Universal_Real);
2230 Set_Ekind (Id, E_Named_Real);
2233 Wrong_Type (E, Any_Numeric);
2237 Set_Ekind (Id, E_Constant);
2238 Set_Never_Set_In_Source (Id, True);
2239 Set_Is_True_Constant (Id, True);
2243 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2244 Set_Etype (E, Etype (Id));
2247 if not Is_OK_Static_Expression (E) then
2248 Flag_Non_Static_Expr
2249 ("non-static expression used in number declaration!", E);
2250 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2251 Set_Etype (E, Any_Type);
2253 end Analyze_Number_Declaration;
2255 --------------------------------
2256 -- Analyze_Object_Declaration --
2257 --------------------------------
2259 procedure Analyze_Object_Declaration (N : Node_Id) is
2260 Loc : constant Source_Ptr := Sloc (N);
2261 Id : constant Entity_Id := Defining_Identifier (N);
2265 E : Node_Id := Expression (N);
2266 -- E is set to Expression (N) throughout this routine. When
2267 -- Expression (N) is modified, E is changed accordingly.
2269 Prev_Entity : Entity_Id := Empty;
2271 function Count_Tasks (T : Entity_Id) return Uint;
2272 -- This function is called when a non-generic library level object of a
2273 -- task type is declared. Its function is to count the static number of
2274 -- tasks declared within the type (it is only called if Has_Tasks is set
2275 -- for T). As a side effect, if an array of tasks with non-static bounds
2276 -- or a variant record type is encountered, Check_Restrictions is called
2277 -- indicating the count is unknown.
2283 function Count_Tasks (T : Entity_Id) return Uint is
2289 if Is_Task_Type (T) then
2292 elsif Is_Record_Type (T) then
2293 if Has_Discriminants (T) then
2294 Check_Restriction (Max_Tasks, N);
2299 C := First_Component (T);
2300 while Present (C) loop
2301 V := V + Count_Tasks (Etype (C));
2308 elsif Is_Array_Type (T) then
2309 X := First_Index (T);
2310 V := Count_Tasks (Component_Type (T));
2311 while Present (X) loop
2314 if not Is_Static_Subtype (C) then
2315 Check_Restriction (Max_Tasks, N);
2318 V := V * (UI_Max (Uint_0,
2319 Expr_Value (Type_High_Bound (C)) -
2320 Expr_Value (Type_Low_Bound (C)) + Uint_1));
2333 -- Start of processing for Analyze_Object_Declaration
2336 -- There are three kinds of implicit types generated by an
2337 -- object declaration:
2339 -- 1. Those for generated by the original Object Definition
2341 -- 2. Those generated by the Expression
2343 -- 3. Those used to constrained the Object Definition with the
2344 -- expression constraints when it is unconstrained
2346 -- They must be generated in this order to avoid order of elaboration
2347 -- issues. Thus the first step (after entering the name) is to analyze
2348 -- the object definition.
2350 if Constant_Present (N) then
2351 Prev_Entity := Current_Entity_In_Scope (Id);
2353 -- If the homograph is an implicit subprogram, it is overridden by
2354 -- the current declaration.
2356 if Present (Prev_Entity)
2358 ((Is_Overloadable (Prev_Entity)
2359 and then Is_Inherited_Operation (Prev_Entity))
2361 -- The current object is a discriminal generated for an entry
2362 -- family index. Even though the index is a constant, in this
2363 -- particular context there is no true constant redeclaration.
2364 -- Enter_Name will handle the visibility.
2367 (Is_Discriminal (Id)
2368 and then Ekind (Discriminal_Link (Id)) =
2369 E_Entry_Index_Parameter))
2371 Prev_Entity := Empty;
2375 if Present (Prev_Entity) then
2376 Constant_Redeclaration (Id, N, T);
2378 Generate_Reference (Prev_Entity, Id, 'c');
2379 Set_Completion_Referenced (Id);
2381 if Error_Posted (N) then
2383 -- Type mismatch or illegal redeclaration, Do not analyze
2384 -- expression to avoid cascaded errors.
2386 T := Find_Type_Of_Object (Object_Definition (N), N);
2388 Set_Ekind (Id, E_Variable);
2392 -- In the normal case, enter identifier at the start to catch premature
2393 -- usage in the initialization expression.
2396 Generate_Definition (Id);
2399 Mark_Coextensions (N, Object_Definition (N));
2401 T := Find_Type_Of_Object (Object_Definition (N), N);
2403 if Nkind (Object_Definition (N)) = N_Access_Definition
2405 (Access_To_Subprogram_Definition (Object_Definition (N)))
2406 and then Protected_Present
2407 (Access_To_Subprogram_Definition (Object_Definition (N)))
2409 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2412 if Error_Posted (Id) then
2414 Set_Ekind (Id, E_Variable);
2419 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2420 -- out some static checks
2422 if Ada_Version >= Ada_05
2423 and then Can_Never_Be_Null (T)
2425 -- In case of aggregates we must also take care of the correct
2426 -- initialization of nested aggregates bug this is done at the
2427 -- point of the analysis of the aggregate (see sem_aggr.adb)
2429 if Present (Expression (N))
2430 and then Nkind (Expression (N)) = N_Aggregate
2436 Save_Typ : constant Entity_Id := Etype (Id);
2438 Set_Etype (Id, T); -- Temp. decoration for static checks
2439 Null_Exclusion_Static_Checks (N);
2440 Set_Etype (Id, Save_Typ);
2445 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2447 -- If deferred constant, make sure context is appropriate. We detect
2448 -- a deferred constant as a constant declaration with no expression.
2449 -- A deferred constant can appear in a package body if its completion
2450 -- is by means of an interface pragma.
2452 if Constant_Present (N)
2455 -- A deferred constant may appear in the declarative part of the
2456 -- following constructs:
2460 -- extended return statements
2463 -- subprogram bodies
2466 -- When declared inside a package spec, a deferred constant must be
2467 -- completed by a full constant declaration or pragma Import. In all
2468 -- other cases, the only proper completion is pragma Import. Extended
2469 -- return statements are flagged as invalid contexts because they do
2470 -- not have a declarative part and so cannot accommodate the pragma.
2472 if Ekind (Current_Scope) = E_Return_Statement then
2474 ("invalid context for deferred constant declaration (RM 7.4)",
2477 ("\declaration requires an initialization expression",
2479 Set_Constant_Present (N, False);
2481 -- In Ada 83, deferred constant must be of private type
2483 elsif not Is_Private_Type (T) then
2484 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2486 ("(Ada 83) deferred constant must be private type", N);
2490 -- If not a deferred constant, then object declaration freezes its type
2493 Check_Fully_Declared (T, N);
2494 Freeze_Before (N, T);
2497 -- If the object was created by a constrained array definition, then
2498 -- set the link in both the anonymous base type and anonymous subtype
2499 -- that are built to represent the array type to point to the object.
2501 if Nkind (Object_Definition (Declaration_Node (Id))) =
2502 N_Constrained_Array_Definition
2504 Set_Related_Array_Object (T, Id);
2505 Set_Related_Array_Object (Base_Type (T), Id);
2508 -- Special checks for protected objects not at library level
2510 if Is_Protected_Type (T)
2511 and then not Is_Library_Level_Entity (Id)
2513 Check_Restriction (No_Local_Protected_Objects, Id);
2515 -- Protected objects with interrupt handlers must be at library level
2517 -- Ada 2005: this test is not needed (and the corresponding clause
2518 -- in the RM is removed) because accessibility checks are sufficient
2519 -- to make handlers not at the library level illegal.
2521 if Has_Interrupt_Handler (T)
2522 and then Ada_Version < Ada_05
2525 ("interrupt object can only be declared at library level", Id);
2529 -- The actual subtype of the object is the nominal subtype, unless
2530 -- the nominal one is unconstrained and obtained from the expression.
2534 -- Process initialization expression if present and not in error
2536 if Present (E) and then E /= Error then
2538 -- Generate an error in case of CPP class-wide object initialization.
2539 -- Required because otherwise the expansion of the class-wide
2540 -- assignment would try to use 'size to initialize the object
2541 -- (primitive that is not available in CPP tagged types).
2543 if Is_Class_Wide_Type (Act_T)
2545 (Is_CPP_Class (Root_Type (Etype (Act_T)))
2547 (Present (Full_View (Root_Type (Etype (Act_T))))
2549 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
2552 ("predefined assignment not available for 'C'P'P tagged types",
2556 Mark_Coextensions (N, E);
2559 -- In case of errors detected in the analysis of the expression,
2560 -- decorate it with the expected type to avoid cascaded errors
2562 if No (Etype (E)) then
2566 -- If an initialization expression is present, then we set the
2567 -- Is_True_Constant flag. It will be reset if this is a variable
2568 -- and it is indeed modified.
2570 Set_Is_True_Constant (Id, True);
2572 -- If we are analyzing a constant declaration, set its completion
2573 -- flag after analyzing and resolving the expression.
2575 if Constant_Present (N) then
2576 Set_Has_Completion (Id);
2579 -- Set type and resolve (type may be overridden later on)
2584 -- If E is null and has been replaced by an N_Raise_Constraint_Error
2585 -- node (which was marked already-analyzed), we need to set the type
2586 -- to something other than Any_Access in order to keep gigi happy.
2588 if Etype (E) = Any_Access then
2592 -- If the object is an access to variable, the initialization
2593 -- expression cannot be an access to constant.
2595 if Is_Access_Type (T)
2596 and then not Is_Access_Constant (T)
2597 and then Is_Access_Type (Etype (E))
2598 and then Is_Access_Constant (Etype (E))
2601 ("access to variable cannot be initialized " &
2602 "with an access-to-constant expression", E);
2605 if not Assignment_OK (N) then
2606 Check_Initialization (T, E);
2609 Check_Unset_Reference (E);
2611 -- If this is a variable, then set current value
2613 if not Constant_Present (N) then
2614 if Compile_Time_Known_Value (E) then
2615 Set_Current_Value (Id, E);
2619 -- Deal with setting of null flags
2621 if Is_Access_Type (T) then
2622 if Known_Non_Null (E) then
2623 Set_Is_Known_Non_Null (Id, True);
2624 elsif Known_Null (E)
2625 and then not Can_Never_Be_Null (Id)
2627 Set_Is_Known_Null (Id, True);
2631 -- Check incorrect use of dynamically tagged expressions. Note
2632 -- the use of Is_Tagged_Type (T) which seems redundant but is in
2633 -- fact important to avoid spurious errors due to expanded code
2634 -- for dispatching functions over an anonymous access type
2636 if (Is_Class_Wide_Type (Etype (E)) or else Is_Dynamically_Tagged (E))
2637 and then Is_Tagged_Type (T)
2638 and then not Is_Class_Wide_Type (T)
2639 and then not Is_CPP_Constructor_Call (E)
2641 Error_Msg_N ("dynamically tagged expression not allowed!", E);
2644 Apply_Scalar_Range_Check (E, T);
2645 Apply_Static_Length_Check (E, T);
2648 -- If the No_Streams restriction is set, check that the type of the
2649 -- object is not, and does not contain, any subtype derived from
2650 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
2651 -- Has_Stream just for efficiency reasons. There is no point in
2652 -- spending time on a Has_Stream check if the restriction is not set.
2654 if Restrictions.Set (No_Streams) then
2655 if Has_Stream (T) then
2656 Check_Restriction (No_Streams, N);
2660 -- Case of unconstrained type
2662 if Is_Indefinite_Subtype (T) then
2664 -- Nothing to do in deferred constant case
2666 if Constant_Present (N) and then No (E) then
2669 -- Case of no initialization present
2672 if No_Initialization (N) then
2675 elsif Is_Class_Wide_Type (T) then
2677 ("initialization required in class-wide declaration ", N);
2681 ("unconstrained subtype not allowed (need initialization)",
2682 Object_Definition (N));
2684 if Is_Record_Type (T) and then Has_Discriminants (T) then
2686 ("\provide initial value or explicit discriminant values",
2687 Object_Definition (N));
2690 ("\or give default discriminant values for type&",
2691 Object_Definition (N), T);
2693 elsif Is_Array_Type (T) then
2695 ("\provide initial value or explicit array bounds",
2696 Object_Definition (N));
2700 -- Case of initialization present but in error. Set initial
2701 -- expression as absent (but do not make above complaints)
2703 elsif E = Error then
2704 Set_Expression (N, Empty);
2707 -- Case of initialization present
2710 -- Not allowed in Ada 83
2712 if not Constant_Present (N) then
2713 if Ada_Version = Ada_83
2714 and then Comes_From_Source (Object_Definition (N))
2717 ("(Ada 83) unconstrained variable not allowed",
2718 Object_Definition (N));
2722 -- Now we constrain the variable from the initializing expression
2724 -- If the expression is an aggregate, it has been expanded into
2725 -- individual assignments. Retrieve the actual type from the
2726 -- expanded construct.
2728 if Is_Array_Type (T)
2729 and then No_Initialization (N)
2730 and then Nkind (Original_Node (E)) = N_Aggregate
2735 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
2736 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
2739 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
2741 if Aliased_Present (N) then
2742 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2745 Freeze_Before (N, Act_T);
2746 Freeze_Before (N, T);
2749 elsif Is_Array_Type (T)
2750 and then No_Initialization (N)
2751 and then Nkind (Original_Node (E)) = N_Aggregate
2753 if not Is_Entity_Name (Object_Definition (N)) then
2755 Check_Compile_Time_Size (Act_T);
2757 if Aliased_Present (N) then
2758 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
2762 -- When the given object definition and the aggregate are specified
2763 -- independently, and their lengths might differ do a length check.
2764 -- This cannot happen if the aggregate is of the form (others =>...)
2766 if not Is_Constrained (T) then
2769 elsif Nkind (E) = N_Raise_Constraint_Error then
2771 -- Aggregate is statically illegal. Place back in declaration
2773 Set_Expression (N, E);
2774 Set_No_Initialization (N, False);
2776 elsif T = Etype (E) then
2779 elsif Nkind (E) = N_Aggregate
2780 and then Present (Component_Associations (E))
2781 and then Present (Choices (First (Component_Associations (E))))
2782 and then Nkind (First
2783 (Choices (First (Component_Associations (E))))) = N_Others_Choice
2788 Apply_Length_Check (E, T);
2791 -- If the type is limited unconstrained with defaulted discriminants
2792 -- and there is no expression, then the object is constrained by the
2793 -- defaults, so it is worthwhile building the corresponding subtype.
2795 elsif (Is_Limited_Record (T)
2796 or else Is_Concurrent_Type (T))
2797 and then not Is_Constrained (T)
2798 and then Has_Discriminants (T)
2801 Act_T := Build_Default_Subtype (T, N);
2803 -- Ada 2005: a limited object may be initialized by means of an
2804 -- aggregate. If the type has default discriminants it has an
2805 -- unconstrained nominal type, Its actual subtype will be obtained
2806 -- from the aggregate, and not from the default discriminants.
2811 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
2813 elsif Present (Underlying_Type (T))
2814 and then not Is_Constrained (Underlying_Type (T))
2815 and then Has_Discriminants (Underlying_Type (T))
2816 and then Nkind (E) = N_Function_Call
2817 and then Constant_Present (N)
2819 -- The back-end has problems with constants of a discriminated type
2820 -- with defaults, if the initial value is a function call. We
2821 -- generate an intermediate temporary for the result of the call.
2822 -- It is unclear why this should make it acceptable to gcc. ???
2824 Remove_Side_Effects (E);
2827 -- Check No_Wide_Characters restriction
2829 if T = Standard_Wide_Character
2830 or else T = Standard_Wide_Wide_Character
2831 or else Root_Type (T) = Standard_Wide_String
2832 or else Root_Type (T) = Standard_Wide_Wide_String
2834 Check_Restriction (No_Wide_Characters, Object_Definition (N));
2837 -- Indicate this is not set in source. Certainly true for constants,
2838 -- and true for variables so far (will be reset for a variable if and
2839 -- when we encounter a modification in the source).
2841 Set_Never_Set_In_Source (Id, True);
2843 -- Now establish the proper kind and type of the object
2845 if Constant_Present (N) then
2846 Set_Ekind (Id, E_Constant);
2847 Set_Is_True_Constant (Id, True);
2850 Set_Ekind (Id, E_Variable);
2852 -- A variable is set as shared passive if it appears in a shared
2853 -- passive package, and is at the outer level. This is not done
2854 -- for entities generated during expansion, because those are
2855 -- always manipulated locally.
2857 if Is_Shared_Passive (Current_Scope)
2858 and then Is_Library_Level_Entity (Id)
2859 and then Comes_From_Source (Id)
2861 Set_Is_Shared_Passive (Id);
2862 Check_Shared_Var (Id, T, N);
2865 -- Set Has_Initial_Value if initializing expression present. Note
2866 -- that if there is no initializing expression, we leave the state
2867 -- of this flag unchanged (usually it will be False, but notably in
2868 -- the case of exception choice variables, it will already be true).
2871 Set_Has_Initial_Value (Id, True);
2875 -- Initialize alignment and size and capture alignment setting
2877 Init_Alignment (Id);
2879 Set_Optimize_Alignment_Flags (Id);
2881 -- Deal with aliased case
2883 if Aliased_Present (N) then
2884 Set_Is_Aliased (Id);
2886 -- If the object is aliased and the type is unconstrained with
2887 -- defaulted discriminants and there is no expression, then the
2888 -- object is constrained by the defaults, so it is worthwhile
2889 -- building the corresponding subtype.
2891 -- Ada 2005 (AI-363): If the aliased object is discriminated and
2892 -- unconstrained, then only establish an actual subtype if the
2893 -- nominal subtype is indefinite. In definite cases the object is
2894 -- unconstrained in Ada 2005.
2897 and then Is_Record_Type (T)
2898 and then not Is_Constrained (T)
2899 and then Has_Discriminants (T)
2900 and then (Ada_Version < Ada_05 or else Is_Indefinite_Subtype (T))
2902 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
2906 -- Now we can set the type of the object
2908 Set_Etype (Id, Act_T);
2910 -- Deal with controlled types
2912 if Has_Controlled_Component (Etype (Id))
2913 or else Is_Controlled (Etype (Id))
2915 if not Is_Library_Level_Entity (Id) then
2916 Check_Restriction (No_Nested_Finalization, N);
2918 Validate_Controlled_Object (Id);
2921 -- Generate a warning when an initialization causes an obvious ABE
2922 -- violation. If the init expression is a simple aggregate there
2923 -- shouldn't be any initialize/adjust call generated. This will be
2924 -- true as soon as aggregates are built in place when possible.
2926 -- ??? at the moment we do not generate warnings for temporaries
2927 -- created for those aggregates although Program_Error might be
2928 -- generated if compiled with -gnato.
2930 if Is_Controlled (Etype (Id))
2931 and then Comes_From_Source (Id)
2934 BT : constant Entity_Id := Base_Type (Etype (Id));
2936 Implicit_Call : Entity_Id;
2937 pragma Warnings (Off, Implicit_Call);
2938 -- ??? what is this for (never referenced!)
2940 function Is_Aggr (N : Node_Id) return Boolean;
2941 -- Check that N is an aggregate
2947 function Is_Aggr (N : Node_Id) return Boolean is
2949 case Nkind (Original_Node (N)) is
2950 when N_Aggregate | N_Extension_Aggregate =>
2953 when N_Qualified_Expression |
2955 N_Unchecked_Type_Conversion =>
2956 return Is_Aggr (Expression (Original_Node (N)));
2964 -- If no underlying type, we already are in an error situation.
2965 -- Do not try to add a warning since we do not have access to
2968 if No (Underlying_Type (BT)) then
2969 Implicit_Call := Empty;
2971 -- A generic type does not have usable primitive operators.
2972 -- Initialization calls are built for instances.
2974 elsif Is_Generic_Type (BT) then
2975 Implicit_Call := Empty;
2977 -- If the init expression is not an aggregate, an adjust call
2978 -- will be generated
2980 elsif Present (E) and then not Is_Aggr (E) then
2981 Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
2983 -- If no init expression and we are not in the deferred
2984 -- constant case, an Initialize call will be generated
2986 elsif No (E) and then not Constant_Present (N) then
2987 Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
2990 Implicit_Call := Empty;
2996 if Has_Task (Etype (Id)) then
2997 Check_Restriction (No_Tasking, N);
2999 -- Deal with counting max tasks
3001 -- Nothing to do if inside a generic
3003 if Inside_A_Generic then
3006 -- If library level entity, then count tasks
3008 elsif Is_Library_Level_Entity (Id) then
3009 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
3011 -- If not library level entity, then indicate we don't know max
3012 -- tasks and also check task hierarchy restriction and blocking
3013 -- operation (since starting a task is definitely blocking!)
3016 Check_Restriction (Max_Tasks, N);
3017 Check_Restriction (No_Task_Hierarchy, N);
3018 Check_Potentially_Blocking_Operation (N);
3021 -- A rather specialized test. If we see two tasks being declared
3022 -- of the same type in the same object declaration, and the task
3023 -- has an entry with an address clause, we know that program error
3024 -- will be raised at run-time since we can't have two tasks with
3025 -- entries at the same address.
3027 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
3032 E := First_Entity (Etype (Id));
3033 while Present (E) loop
3034 if Ekind (E) = E_Entry
3035 and then Present (Get_Attribute_Definition_Clause
3036 (E, Attribute_Address))
3039 ("?more than one task with same entry address", N);
3041 ("\?Program_Error will be raised at run time", N);
3043 Make_Raise_Program_Error (Loc,
3044 Reason => PE_Duplicated_Entry_Address));
3054 -- Some simple constant-propagation: if the expression is a constant
3055 -- string initialized with a literal, share the literal. This avoids
3059 and then Is_Entity_Name (E)
3060 and then Ekind (Entity (E)) = E_Constant
3061 and then Base_Type (Etype (E)) = Standard_String
3064 Val : constant Node_Id := Constant_Value (Entity (E));
3067 and then Nkind (Val) = N_String_Literal
3069 Rewrite (E, New_Copy (Val));
3074 -- Another optimization: if the nominal subtype is unconstrained and
3075 -- the expression is a function call that returns an unconstrained
3076 -- type, rewrite the declaration as a renaming of the result of the
3077 -- call. The exceptions below are cases where the copy is expected,
3078 -- either by the back end (Aliased case) or by the semantics, as for
3079 -- initializing controlled types or copying tags for classwide types.
3082 and then Nkind (E) = N_Explicit_Dereference
3083 and then Nkind (Original_Node (E)) = N_Function_Call
3084 and then not Is_Library_Level_Entity (Id)
3085 and then not Is_Constrained (Underlying_Type (T))
3086 and then not Is_Aliased (Id)
3087 and then not Is_Class_Wide_Type (T)
3088 and then not Is_Controlled (T)
3089 and then not Has_Controlled_Component (Base_Type (T))
3090 and then Expander_Active
3093 Make_Object_Renaming_Declaration (Loc,
3094 Defining_Identifier => Id,
3095 Access_Definition => Empty,
3096 Subtype_Mark => New_Occurrence_Of
3097 (Base_Type (Etype (Id)), Loc),
3100 Set_Renamed_Object (Id, E);
3102 -- Force generation of debugging information for the constant and for
3103 -- the renamed function call.
3105 Set_Debug_Info_Needed (Id);
3106 Set_Debug_Info_Needed (Entity (Prefix (E)));
3109 if Present (Prev_Entity)
3110 and then Is_Frozen (Prev_Entity)
3111 and then not Error_Posted (Id)
3113 Error_Msg_N ("full constant declaration appears too late", N);
3116 Check_Eliminated (Id);
3118 -- Deal with setting In_Private_Part flag if in private part
3120 if Ekind (Scope (Id)) = E_Package
3121 and then In_Private_Part (Scope (Id))
3123 Set_In_Private_Part (Id);
3126 -- Check for violation of No_Local_Timing_Events
3128 if Is_RTE (Etype (Id), RE_Timing_Event)
3129 and then not Is_Library_Level_Entity (Id)
3131 Check_Restriction (No_Local_Timing_Events, N);
3133 end Analyze_Object_Declaration;
3135 ---------------------------
3136 -- Analyze_Others_Choice --
3137 ---------------------------
3139 -- Nothing to do for the others choice node itself, the semantic analysis
3140 -- of the others choice will occur as part of the processing of the parent
3142 procedure Analyze_Others_Choice (N : Node_Id) is
3143 pragma Warnings (Off, N);
3146 end Analyze_Others_Choice;
3148 -------------------------------------------
3149 -- Analyze_Private_Extension_Declaration --
3150 -------------------------------------------
3152 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3153 T : constant Entity_Id := Defining_Identifier (N);
3154 Indic : constant Node_Id := Subtype_Indication (N);
3155 Parent_Type : Entity_Id;
3156 Parent_Base : Entity_Id;
3159 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3161 if Is_Non_Empty_List (Interface_List (N)) then
3167 Intf := First (Interface_List (N));
3168 while Present (Intf) loop
3169 T := Find_Type_Of_Subtype_Indic (Intf);
3171 Diagnose_Interface (Intf, T);
3177 Generate_Definition (T);
3180 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3181 Parent_Base := Base_Type (Parent_Type);
3183 if Parent_Type = Any_Type
3184 or else Etype (Parent_Type) = Any_Type
3186 Set_Ekind (T, Ekind (Parent_Type));
3187 Set_Etype (T, Any_Type);
3190 elsif not Is_Tagged_Type (Parent_Type) then
3192 ("parent of type extension must be a tagged type ", Indic);
3195 elsif Ekind (Parent_Type) = E_Void
3196 or else Ekind (Parent_Type) = E_Incomplete_Type
3198 Error_Msg_N ("premature derivation of incomplete type", Indic);
3201 elsif Is_Concurrent_Type (Parent_Type) then
3203 ("parent type of a private extension cannot be "
3204 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3206 Set_Etype (T, Any_Type);
3207 Set_Ekind (T, E_Limited_Private_Type);
3208 Set_Private_Dependents (T, New_Elmt_List);
3209 Set_Error_Posted (T);
3213 -- Perhaps the parent type should be changed to the class-wide type's
3214 -- specific type in this case to prevent cascading errors ???
3216 if Is_Class_Wide_Type (Parent_Type) then
3218 ("parent of type extension must not be a class-wide type", Indic);
3222 if (not Is_Package_Or_Generic_Package (Current_Scope)
3223 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3224 or else In_Private_Part (Current_Scope)
3227 Error_Msg_N ("invalid context for private extension", N);
3230 -- Set common attributes
3232 Set_Is_Pure (T, Is_Pure (Current_Scope));
3233 Set_Scope (T, Current_Scope);
3234 Set_Ekind (T, E_Record_Type_With_Private);
3235 Init_Size_Align (T);
3237 Set_Etype (T, Parent_Base);
3238 Set_Has_Task (T, Has_Task (Parent_Base));
3240 Set_Convention (T, Convention (Parent_Type));
3241 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
3242 Set_Is_First_Subtype (T);
3243 Make_Class_Wide_Type (T);
3245 if Unknown_Discriminants_Present (N) then
3246 Set_Discriminant_Constraint (T, No_Elist);
3249 Build_Derived_Record_Type (N, Parent_Type, T);
3251 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
3252 -- synchronized formal derived type.
3254 if Ada_Version >= Ada_05
3255 and then Synchronized_Present (N)
3257 Set_Is_Limited_Record (T);
3259 -- Formal derived type case
3261 if Is_Generic_Type (T) then
3263 -- The parent must be a tagged limited type or a synchronized
3266 if (not Is_Tagged_Type (Parent_Type)
3267 or else not Is_Limited_Type (Parent_Type))
3269 (not Is_Interface (Parent_Type)
3270 or else not Is_Synchronized_Interface (Parent_Type))
3272 Error_Msg_NE ("parent type of & must be tagged limited " &
3273 "or synchronized", N, T);
3276 -- The progenitors (if any) must be limited or synchronized
3279 if Present (Interfaces (T)) then
3282 Iface_Elmt : Elmt_Id;
3285 Iface_Elmt := First_Elmt (Interfaces (T));
3286 while Present (Iface_Elmt) loop
3287 Iface := Node (Iface_Elmt);
3289 if not Is_Limited_Interface (Iface)
3290 and then not Is_Synchronized_Interface (Iface)
3292 Error_Msg_NE ("progenitor & must be limited " &
3293 "or synchronized", N, Iface);
3296 Next_Elmt (Iface_Elmt);
3301 -- Regular derived extension, the parent must be a limited or
3302 -- synchronized interface.
3305 if not Is_Interface (Parent_Type)
3306 or else (not Is_Limited_Interface (Parent_Type)
3308 not Is_Synchronized_Interface (Parent_Type))
3311 ("parent type of & must be limited interface", N, T);
3315 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
3316 -- extension with a synchronized parent must be explicitly declared
3317 -- synchronized, because the full view will be a synchronized type.
3318 -- This must be checked before the check for limited types below,
3319 -- to ensure that types declared limited are not allowed to extend
3320 -- synchronized interfaces.
3322 elsif Is_Interface (Parent_Type)
3323 and then Is_Synchronized_Interface (Parent_Type)
3324 and then not Synchronized_Present (N)
3327 ("private extension of& must be explicitly synchronized",
3330 elsif Limited_Present (N) then
3331 Set_Is_Limited_Record (T);
3333 if not Is_Limited_Type (Parent_Type)
3335 (not Is_Interface (Parent_Type)
3336 or else not Is_Limited_Interface (Parent_Type))
3338 Error_Msg_NE ("parent type& of limited extension must be limited",
3342 end Analyze_Private_Extension_Declaration;
3344 ---------------------------------
3345 -- Analyze_Subtype_Declaration --
3346 ---------------------------------
3348 procedure Analyze_Subtype_Declaration
3350 Skip : Boolean := False)
3352 Id : constant Entity_Id := Defining_Identifier (N);
3354 R_Checks : Check_Result;
3357 Generate_Definition (Id);
3358 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3359 Init_Size_Align (Id);
3361 -- The following guard condition on Enter_Name is to handle cases where
3362 -- the defining identifier has already been entered into the scope but
3363 -- the declaration as a whole needs to be analyzed.
3365 -- This case in particular happens for derived enumeration types. The
3366 -- derived enumeration type is processed as an inserted enumeration type
3367 -- declaration followed by a rewritten subtype declaration. The defining
3368 -- identifier, however, is entered into the name scope very early in the
3369 -- processing of the original type declaration and therefore needs to be
3370 -- avoided here, when the created subtype declaration is analyzed. (See
3371 -- Build_Derived_Types)
3373 -- This also happens when the full view of a private type is derived
3374 -- type with constraints. In this case the entity has been introduced
3375 -- in the private declaration.
3378 or else (Present (Etype (Id))
3379 and then (Is_Private_Type (Etype (Id))
3380 or else Is_Task_Type (Etype (Id))
3381 or else Is_Rewrite_Substitution (N)))
3389 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
3391 -- Inherit common attributes
3393 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
3394 Set_Is_Volatile (Id, Is_Volatile (T));
3395 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
3396 Set_Is_Atomic (Id, Is_Atomic (T));
3397 Set_Is_Ada_2005_Only (Id, Is_Ada_2005_Only (T));
3398 Set_Convention (Id, Convention (T));
3400 -- In the case where there is no constraint given in the subtype
3401 -- indication, Process_Subtype just returns the Subtype_Mark, so its
3402 -- semantic attributes must be established here.
3404 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
3405 Set_Etype (Id, Base_Type (T));
3409 Set_Ekind (Id, E_Array_Subtype);
3410 Copy_Array_Subtype_Attributes (Id, T);
3412 when Decimal_Fixed_Point_Kind =>
3413 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
3414 Set_Digits_Value (Id, Digits_Value (T));
3415 Set_Delta_Value (Id, Delta_Value (T));
3416 Set_Scale_Value (Id, Scale_Value (T));
3417 Set_Small_Value (Id, Small_Value (T));
3418 Set_Scalar_Range (Id, Scalar_Range (T));
3419 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
3420 Set_Is_Constrained (Id, Is_Constrained (T));
3421 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3422 Set_RM_Size (Id, RM_Size (T));
3424 when Enumeration_Kind =>
3425 Set_Ekind (Id, E_Enumeration_Subtype);
3426 Set_First_Literal (Id, First_Literal (Base_Type (T)));
3427 Set_Scalar_Range (Id, Scalar_Range (T));
3428 Set_Is_Character_Type (Id, Is_Character_Type (T));
3429 Set_Is_Constrained (Id, Is_Constrained (T));
3430 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3431 Set_RM_Size (Id, RM_Size (T));
3433 when Ordinary_Fixed_Point_Kind =>
3434 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
3435 Set_Scalar_Range (Id, Scalar_Range (T));
3436 Set_Small_Value (Id, Small_Value (T));
3437 Set_Delta_Value (Id, Delta_Value (T));
3438 Set_Is_Constrained (Id, Is_Constrained (T));
3439 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3440 Set_RM_Size (Id, RM_Size (T));
3443 Set_Ekind (Id, E_Floating_Point_Subtype);
3444 Set_Scalar_Range (Id, Scalar_Range (T));
3445 Set_Digits_Value (Id, Digits_Value (T));
3446 Set_Is_Constrained (Id, Is_Constrained (T));
3448 when Signed_Integer_Kind =>
3449 Set_Ekind (Id, E_Signed_Integer_Subtype);
3450 Set_Scalar_Range (Id, Scalar_Range (T));
3451 Set_Is_Constrained (Id, Is_Constrained (T));
3452 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3453 Set_RM_Size (Id, RM_Size (T));
3455 when Modular_Integer_Kind =>
3456 Set_Ekind (Id, E_Modular_Integer_Subtype);
3457 Set_Scalar_Range (Id, Scalar_Range (T));
3458 Set_Is_Constrained (Id, Is_Constrained (T));
3459 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
3460 Set_RM_Size (Id, RM_Size (T));
3462 when Class_Wide_Kind =>
3463 Set_Ekind (Id, E_Class_Wide_Subtype);
3464 Set_First_Entity (Id, First_Entity (T));
3465 Set_Last_Entity (Id, Last_Entity (T));
3466 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3467 Set_Cloned_Subtype (Id, T);
3468 Set_Is_Tagged_Type (Id, True);
3469 Set_Has_Unknown_Discriminants
3472 if Ekind (T) = E_Class_Wide_Subtype then
3473 Set_Equivalent_Type (Id, Equivalent_Type (T));
3476 when E_Record_Type | E_Record_Subtype =>
3477 Set_Ekind (Id, E_Record_Subtype);
3479 if Ekind (T) = E_Record_Subtype
3480 and then Present (Cloned_Subtype (T))
3482 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
3484 Set_Cloned_Subtype (Id, T);
3487 Set_First_Entity (Id, First_Entity (T));
3488 Set_Last_Entity (Id, Last_Entity (T));
3489 Set_Has_Discriminants (Id, Has_Discriminants (T));
3490 Set_Is_Constrained (Id, Is_Constrained (T));
3491 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
3492 Set_Has_Unknown_Discriminants
3493 (Id, Has_Unknown_Discriminants (T));
3495 if Has_Discriminants (T) then
3496 Set_Discriminant_Constraint
3497 (Id, Discriminant_Constraint (T));
3498 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3500 elsif Has_Unknown_Discriminants (Id) then
3501 Set_Discriminant_Constraint (Id, No_Elist);
3504 if Is_Tagged_Type (T) then
3505 Set_Is_Tagged_Type (Id);
3506 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
3507 Set_Primitive_Operations
3508 (Id, Primitive_Operations (T));
3509 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3511 if Is_Interface (T) then
3512 Set_Is_Interface (Id);
3513 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
3517 when Private_Kind =>
3518 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
3519 Set_Has_Discriminants (Id, Has_Discriminants (T));
3520 Set_Is_Constrained (Id, Is_Constrained (T));
3521 Set_First_Entity (Id, First_Entity (T));
3522 Set_Last_Entity (Id, Last_Entity (T));
3523 Set_Private_Dependents (Id, New_Elmt_List);
3524 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
3525 Set_Has_Unknown_Discriminants
3526 (Id, Has_Unknown_Discriminants (T));
3527 Set_Known_To_Have_Preelab_Init
3528 (Id, Known_To_Have_Preelab_Init (T));
3530 if Is_Tagged_Type (T) then
3531 Set_Is_Tagged_Type (Id);
3532 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
3533 Set_Primitive_Operations (Id, Primitive_Operations (T));
3534 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
3537 -- In general the attributes of the subtype of a private type
3538 -- are the attributes of the partial view of parent. However,
3539 -- the full view may be a discriminated type, and the subtype
3540 -- must share the discriminant constraint to generate correct
3541 -- calls to initialization procedures.
3543 if Has_Discriminants (T) then
3544 Set_Discriminant_Constraint
3545 (Id, Discriminant_Constraint (T));
3546 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3548 elsif Present (Full_View (T))
3549 and then Has_Discriminants (Full_View (T))
3551 Set_Discriminant_Constraint
3552 (Id, Discriminant_Constraint (Full_View (T)));
3553 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3555 -- This would seem semantically correct, but apparently
3556 -- confuses the back-end. To be explained and checked with
3557 -- current version ???
3559 -- Set_Has_Discriminants (Id);
3562 Prepare_Private_Subtype_Completion (Id, N);
3565 Set_Ekind (Id, E_Access_Subtype);
3566 Set_Is_Constrained (Id, Is_Constrained (T));
3567 Set_Is_Access_Constant
3568 (Id, Is_Access_Constant (T));
3569 Set_Directly_Designated_Type
3570 (Id, Designated_Type (T));
3571 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
3573 -- A Pure library_item must not contain the declaration of a
3574 -- named access type, except within a subprogram, generic
3575 -- subprogram, task unit, or protected unit, or if it has
3576 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
3578 if Comes_From_Source (Id)
3579 and then In_Pure_Unit
3580 and then not In_Subprogram_Task_Protected_Unit
3581 and then not No_Pool_Assigned (Id)
3584 ("named access types not allowed in pure unit", N);
3587 when Concurrent_Kind =>
3588 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
3589 Set_Corresponding_Record_Type (Id,
3590 Corresponding_Record_Type (T));
3591 Set_First_Entity (Id, First_Entity (T));
3592 Set_First_Private_Entity (Id, First_Private_Entity (T));
3593 Set_Has_Discriminants (Id, Has_Discriminants (T));
3594 Set_Is_Constrained (Id, Is_Constrained (T));
3595 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
3596 Set_Last_Entity (Id, Last_Entity (T));
3598 if Has_Discriminants (T) then
3599 Set_Discriminant_Constraint (Id,
3600 Discriminant_Constraint (T));
3601 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
3604 when E_Incomplete_Type =>
3605 if Ada_Version >= Ada_05 then
3606 Set_Ekind (Id, E_Incomplete_Subtype);
3608 -- Ada 2005 (AI-412): Decorate an incomplete subtype
3609 -- of an incomplete type visible through a limited
3612 if From_With_Type (T)
3613 and then Present (Non_Limited_View (T))
3615 Set_From_With_Type (Id);
3616 Set_Non_Limited_View (Id, Non_Limited_View (T));
3618 -- Ada 2005 (AI-412): Add the regular incomplete subtype
3619 -- to the private dependents of the original incomplete
3620 -- type for future transformation.
3623 Append_Elmt (Id, Private_Dependents (T));
3626 -- If the subtype name denotes an incomplete type an error
3627 -- was already reported by Process_Subtype.
3630 Set_Etype (Id, Any_Type);
3634 raise Program_Error;
3638 if Etype (Id) = Any_Type then
3642 -- Some common processing on all types
3644 Set_Size_Info (Id, T);
3645 Set_First_Rep_Item (Id, First_Rep_Item (T));
3649 Set_Is_Immediately_Visible (Id, True);
3650 Set_Depends_On_Private (Id, Has_Private_Component (T));
3651 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
3653 if Is_Interface (T) then
3654 Set_Is_Interface (Id);
3657 if Present (Generic_Parent_Type (N))
3660 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
3662 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
3663 /= N_Formal_Private_Type_Definition)
3665 if Is_Tagged_Type (Id) then
3667 -- If this is a generic actual subtype for a synchronized type,
3668 -- the primitive operations are those of the corresponding record
3669 -- for which there is a separate subtype declaration.
3671 if Is_Concurrent_Type (Id) then
3673 elsif Is_Class_Wide_Type (Id) then
3674 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
3676 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
3679 elsif Scope (Etype (Id)) /= Standard_Standard then
3680 Derive_Subprograms (Generic_Parent_Type (N), Id);
3684 if Is_Private_Type (T)
3685 and then Present (Full_View (T))
3687 Conditional_Delay (Id, Full_View (T));
3689 -- The subtypes of components or subcomponents of protected types
3690 -- do not need freeze nodes, which would otherwise appear in the
3691 -- wrong scope (before the freeze node for the protected type). The
3692 -- proper subtypes are those of the subcomponents of the corresponding
3695 elsif Ekind (Scope (Id)) /= E_Protected_Type
3696 and then Present (Scope (Scope (Id))) -- error defense!
3697 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
3699 Conditional_Delay (Id, T);
3702 -- Check that constraint_error is raised for a scalar subtype
3703 -- indication when the lower or upper bound of a non-null range
3704 -- lies outside the range of the type mark.
3706 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
3707 if Is_Scalar_Type (Etype (Id))
3708 and then Scalar_Range (Id) /=
3709 Scalar_Range (Etype (Subtype_Mark
3710 (Subtype_Indication (N))))
3714 Etype (Subtype_Mark (Subtype_Indication (N))));
3716 elsif Is_Array_Type (Etype (Id))
3717 and then Present (First_Index (Id))
3719 -- This really should be a subprogram that finds the indications
3722 if ((Nkind (First_Index (Id)) = N_Identifier
3723 and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
3724 or else Nkind (First_Index (Id)) = N_Subtype_Indication)
3726 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
3729 Target_Typ : constant Entity_Id :=
3732 (Subtype_Mark (Subtype_Indication (N)))));
3736 (Scalar_Range (Etype (First_Index (Id))),
3738 Etype (First_Index (Id)),
3739 Defining_Identifier (N));
3745 Sloc (Defining_Identifier (N)));
3751 Set_Optimize_Alignment_Flags (Id);
3752 Check_Eliminated (Id);
3753 end Analyze_Subtype_Declaration;
3755 --------------------------------
3756 -- Analyze_Subtype_Indication --
3757 --------------------------------
3759 procedure Analyze_Subtype_Indication (N : Node_Id) is
3760 T : constant Entity_Id := Subtype_Mark (N);
3761 R : constant Node_Id := Range_Expression (Constraint (N));
3768 Set_Etype (N, Etype (R));
3769 Resolve (R, Entity (T));
3771 Set_Error_Posted (R);
3772 Set_Error_Posted (T);
3774 end Analyze_Subtype_Indication;
3776 ------------------------------
3777 -- Analyze_Type_Declaration --
3778 ------------------------------
3780 procedure Analyze_Type_Declaration (N : Node_Id) is
3781 Def : constant Node_Id := Type_Definition (N);
3782 Def_Id : constant Entity_Id := Defining_Identifier (N);
3786 Is_Remote : constant Boolean :=
3787 (Is_Remote_Types (Current_Scope)
3788 or else Is_Remote_Call_Interface (Current_Scope))
3789 and then not (In_Private_Part (Current_Scope)
3790 or else In_Package_Body (Current_Scope));
3792 procedure Check_Ops_From_Incomplete_Type;
3793 -- If there is a tagged incomplete partial view of the type, transfer
3794 -- its operations to the full view, and indicate that the type of the
3795 -- controlling parameter (s) is this full view.
3797 ------------------------------------
3798 -- Check_Ops_From_Incomplete_Type --
3799 ------------------------------------
3801 procedure Check_Ops_From_Incomplete_Type is
3808 and then Ekind (Prev) = E_Incomplete_Type
3809 and then Is_Tagged_Type (Prev)
3810 and then Is_Tagged_Type (T)
3812 Elmt := First_Elmt (Primitive_Operations (Prev));
3813 while Present (Elmt) loop
3815 Prepend_Elmt (Op, Primitive_Operations (T));
3817 Formal := First_Formal (Op);
3818 while Present (Formal) loop
3819 if Etype (Formal) = Prev then
3820 Set_Etype (Formal, T);
3823 Next_Formal (Formal);
3826 if Etype (Op) = Prev then
3833 end Check_Ops_From_Incomplete_Type;
3835 -- Start of processing for Analyze_Type_Declaration
3838 Prev := Find_Type_Name (N);
3840 -- The full view, if present, now points to the current type
3842 -- Ada 2005 (AI-50217): If the type was previously decorated when
3843 -- imported through a LIMITED WITH clause, it appears as incomplete
3844 -- but has no full view.
3845 -- If the incomplete view is tagged, a class_wide type has been
3846 -- created already. Use it for the full view as well, to prevent
3847 -- multiple incompatible class-wide types that may be created for
3848 -- self-referential anonymous access components.
3850 if Ekind (Prev) = E_Incomplete_Type
3851 and then Present (Full_View (Prev))
3853 T := Full_View (Prev);
3855 if Is_Tagged_Type (Prev)
3856 and then Present (Class_Wide_Type (Prev))
3858 Set_Ekind (T, Ekind (Prev)); -- will be reset later
3859 Set_Class_Wide_Type (T, Class_Wide_Type (Prev));
3860 Set_Etype (Class_Wide_Type (T), T);
3867 Set_Is_Pure (T, Is_Pure (Current_Scope));
3869 -- We set the flag Is_First_Subtype here. It is needed to set the
3870 -- corresponding flag for the Implicit class-wide-type created
3871 -- during tagged types processing.
3873 Set_Is_First_Subtype (T, True);
3875 -- Only composite types other than array types are allowed to have
3880 -- For derived types, the rule will be checked once we've figured
3881 -- out the parent type.
3883 when N_Derived_Type_Definition =>
3886 -- For record types, discriminants are allowed
3888 when N_Record_Definition =>
3892 if Present (Discriminant_Specifications (N)) then
3894 ("elementary or array type cannot have discriminants",
3896 (First (Discriminant_Specifications (N))));
3900 -- Elaborate the type definition according to kind, and generate
3901 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3902 -- already done (this happens during the reanalysis that follows a call
3903 -- to the high level optimizer).
3905 if not Analyzed (T) then
3910 when N_Access_To_Subprogram_Definition =>
3911 Access_Subprogram_Declaration (T, Def);
3913 -- If this is a remote access to subprogram, we must create the
3914 -- equivalent fat pointer type, and related subprograms.
3917 Process_Remote_AST_Declaration (N);
3920 -- Validate categorization rule against access type declaration
3921 -- usually a violation in Pure unit, Shared_Passive unit.
3923 Validate_Access_Type_Declaration (T, N);
3925 when N_Access_To_Object_Definition =>
3926 Access_Type_Declaration (T, Def);
3928 -- Validate categorization rule against access type declaration
3929 -- usually a violation in Pure unit, Shared_Passive unit.
3931 Validate_Access_Type_Declaration (T, N);
3933 -- If we are in a Remote_Call_Interface package and define a
3934 -- RACW, then calling stubs and specific stream attributes
3938 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3940 Add_RACW_Features (Def_Id);
3943 -- Set no strict aliasing flag if config pragma seen
3945 if Opt.No_Strict_Aliasing then
3946 Set_No_Strict_Aliasing (Base_Type (Def_Id));
3949 when N_Array_Type_Definition =>
3950 Array_Type_Declaration (T, Def);
3952 when N_Derived_Type_Definition =>
3953 Derived_Type_Declaration (T, N, T /= Def_Id);
3955 when N_Enumeration_Type_Definition =>
3956 Enumeration_Type_Declaration (T, Def);
3958 when N_Floating_Point_Definition =>
3959 Floating_Point_Type_Declaration (T, Def);
3961 when N_Decimal_Fixed_Point_Definition =>
3962 Decimal_Fixed_Point_Type_Declaration (T, Def);
3964 when N_Ordinary_Fixed_Point_Definition =>
3965 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3967 when N_Signed_Integer_Type_Definition =>
3968 Signed_Integer_Type_Declaration (T, Def);
3970 when N_Modular_Type_Definition =>
3971 Modular_Type_Declaration (T, Def);
3973 when N_Record_Definition =>
3974 Record_Type_Declaration (T, N, Prev);
3977 raise Program_Error;
3982 if Etype (T) = Any_Type then
3986 -- Some common processing for all types
3988 Set_Depends_On_Private (T, Has_Private_Component (T));
3989 Check_Ops_From_Incomplete_Type;
3991 -- Both the declared entity, and its anonymous base type if one
3992 -- was created, need freeze nodes allocated.
3995 B : constant Entity_Id := Base_Type (T);
3998 -- In the case where the base type differs from the first subtype, we
3999 -- pre-allocate a freeze node, and set the proper link to the first
4000 -- subtype. Freeze_Entity will use this preallocated freeze node when
4001 -- it freezes the entity.
4003 -- This does not apply if the base type is a generic type, whose
4004 -- declaration is independent of the current derived definition.
4006 if B /= T and then not Is_Generic_Type (B) then
4007 Ensure_Freeze_Node (B);
4008 Set_First_Subtype_Link (Freeze_Node (B), T);
4011 -- A type that is imported through a limited_with clause cannot
4012 -- generate any code, and thus need not be frozen. However, an access
4013 -- type with an imported designated type needs a finalization list,
4014 -- which may be referenced in some other package that has non-limited
4015 -- visibility on the designated type. Thus we must create the
4016 -- finalization list at the point the access type is frozen, to
4017 -- prevent unsatisfied references at link time.
4019 if not From_With_Type (T) or else Is_Access_Type (T) then
4020 Set_Has_Delayed_Freeze (T);
4024 -- Case where T is the full declaration of some private type which has
4025 -- been swapped in Defining_Identifier (N).
4027 if T /= Def_Id and then Is_Private_Type (Def_Id) then
4028 Process_Full_View (N, T, Def_Id);
4030 -- Record the reference. The form of this is a little strange, since
4031 -- the full declaration has been swapped in. So the first parameter
4032 -- here represents the entity to which a reference is made which is
4033 -- the "real" entity, i.e. the one swapped in, and the second
4034 -- parameter provides the reference location.
4036 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
4037 -- since we don't want a complaint about the full type being an
4038 -- unwanted reference to the private type
4041 B : constant Boolean := Has_Pragma_Unreferenced (T);
4043 Set_Has_Pragma_Unreferenced (T, False);
4044 Generate_Reference (T, T, 'c');
4045 Set_Has_Pragma_Unreferenced (T, B);
4048 Set_Completion_Referenced (Def_Id);
4050 -- For completion of incomplete type, process incomplete dependents
4051 -- and always mark the full type as referenced (it is the incomplete
4052 -- type that we get for any real reference).
4054 elsif Ekind (Prev) = E_Incomplete_Type then
4055 Process_Incomplete_Dependents (N, T, Prev);
4056 Generate_Reference (Prev, Def_Id, 'c');
4057 Set_Completion_Referenced (Def_Id);
4059 -- If not private type or incomplete type completion, this is a real
4060 -- definition of a new entity, so record it.
4063 Generate_Definition (Def_Id);
4066 if Chars (Scope (Def_Id)) = Name_System
4067 and then Chars (Def_Id) = Name_Address
4068 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
4070 Set_Is_Descendent_Of_Address (Def_Id);
4071 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
4072 Set_Is_Descendent_Of_Address (Prev);
4075 Set_Optimize_Alignment_Flags (Def_Id);
4076 Check_Eliminated (Def_Id);
4077 end Analyze_Type_Declaration;
4079 --------------------------
4080 -- Analyze_Variant_Part --
4081 --------------------------
4083 procedure Analyze_Variant_Part (N : Node_Id) is
4085 procedure Non_Static_Choice_Error (Choice : Node_Id);
4086 -- Error routine invoked by the generic instantiation below when the
4087 -- variant part has a non static choice.
4089 procedure Process_Declarations (Variant : Node_Id);
4090 -- Analyzes all the declarations associated with a Variant. Needed by
4091 -- the generic instantiation below.
4093 package Variant_Choices_Processing is new
4094 Generic_Choices_Processing
4095 (Get_Alternatives => Variants,
4096 Get_Choices => Discrete_Choices,
4097 Process_Empty_Choice => No_OP,
4098 Process_Non_Static_Choice => Non_Static_Choice_Error,
4099 Process_Associated_Node => Process_Declarations);
4100 use Variant_Choices_Processing;
4101 -- Instantiation of the generic choice processing package
4103 -----------------------------
4104 -- Non_Static_Choice_Error --
4105 -----------------------------
4107 procedure Non_Static_Choice_Error (Choice : Node_Id) is
4109 Flag_Non_Static_Expr
4110 ("choice given in variant part is not static!", Choice);
4111 end Non_Static_Choice_Error;
4113 --------------------------
4114 -- Process_Declarations --
4115 --------------------------
4117 procedure Process_Declarations (Variant : Node_Id) is
4119 if not Null_Present (Component_List (Variant)) then
4120 Analyze_Declarations (Component_Items (Component_List (Variant)));
4122 if Present (Variant_Part (Component_List (Variant))) then
4123 Analyze (Variant_Part (Component_List (Variant)));
4126 end Process_Declarations;
4130 Discr_Name : Node_Id;
4131 Discr_Type : Entity_Id;
4133 Case_Table : Choice_Table_Type (1 .. Number_Of_Choices (N));
4135 Dont_Care : Boolean;
4136 Others_Present : Boolean := False;
4138 pragma Warnings (Off, Case_Table);
4139 pragma Warnings (Off, Last_Choice);
4140 pragma Warnings (Off, Dont_Care);
4141 pragma Warnings (Off, Others_Present);
4142 -- We don't care about the assigned values of any of these
4144 -- Start of processing for Analyze_Variant_Part
4147 Discr_Name := Name (N);
4148 Analyze (Discr_Name);
4150 -- If Discr_Name bad, get out (prevent cascaded errors)
4152 if Etype (Discr_Name) = Any_Type then
4156 -- Check invalid discriminant in variant part
4158 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4159 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4162 Discr_Type := Etype (Entity (Discr_Name));
4164 if not Is_Discrete_Type (Discr_Type) then
4166 ("discriminant in a variant part must be of a discrete type",
4171 -- Call the instantiated Analyze_Choices which does the rest of the work
4174 (N, Discr_Type, Case_Table, Last_Choice, Dont_Care, Others_Present);
4175 end Analyze_Variant_Part;
4177 ----------------------------
4178 -- Array_Type_Declaration --
4179 ----------------------------
4181 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4182 Component_Def : constant Node_Id := Component_Definition (Def);
4183 Element_Type : Entity_Id;
4184 Implicit_Base : Entity_Id;
4186 Related_Id : Entity_Id := Empty;
4188 P : constant Node_Id := Parent (Def);
4192 if Nkind (Def) = N_Constrained_Array_Definition then
4193 Index := First (Discrete_Subtype_Definitions (Def));
4195 Index := First (Subtype_Marks (Def));
4198 -- Find proper names for the implicit types which may be public. In case
4199 -- of anonymous arrays we use the name of the first object of that type
4203 Related_Id := Defining_Identifier (P);
4209 while Present (Index) loop
4212 -- Add a subtype declaration for each index of private array type
4213 -- declaration whose etype is also private. For example:
4216 -- type Index is private;
4218 -- type Table is array (Index) of ...
4221 -- This is currently required by the expander for the internally
4222 -- generated equality subprogram of records with variant parts in
4223 -- which the etype of some component is such private type.
4225 if Ekind (Current_Scope) = E_Package
4226 and then In_Private_Part (Current_Scope)
4227 and then Has_Private_Declaration (Etype (Index))
4230 Loc : constant Source_Ptr := Sloc (Def);
4236 Make_Defining_Identifier (Loc,
4237 Chars => New_Internal_Name ('T'));
4238 Set_Is_Internal (New_E);
4241 Make_Subtype_Declaration (Loc,
4242 Defining_Identifier => New_E,
4243 Subtype_Indication =>
4244 New_Occurrence_Of (Etype (Index), Loc));
4246 Insert_Before (Parent (Def), Decl);
4248 Set_Etype (Index, New_E);
4250 -- If the index is a range the Entity attribute is not
4251 -- available. Example:
4254 -- type T is private;
4256 -- type T is new Natural;
4257 -- Table : array (T(1) .. T(10)) of Boolean;
4260 if Nkind (Index) /= N_Range then
4261 Set_Entity (Index, New_E);
4266 Make_Index (Index, P, Related_Id, Nb_Index);
4268 Nb_Index := Nb_Index + 1;
4271 -- Process subtype indication if one is present
4273 if Present (Subtype_Indication (Component_Def)) then
4276 (Subtype_Indication (Component_Def), P, Related_Id, 'C');
4278 -- Ada 2005 (AI-230): Access Definition case
4280 else pragma Assert (Present (Access_Definition (Component_Def)));
4282 -- Indicate that the anonymous access type is created by the
4283 -- array type declaration.
4285 Element_Type := Access_Definition
4287 N => Access_Definition (Component_Def));
4288 Set_Is_Local_Anonymous_Access (Element_Type);
4290 -- Propagate the parent. This field is needed if we have to generate
4291 -- the master_id associated with an anonymous access to task type
4292 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
4294 Set_Parent (Element_Type, Parent (T));
4296 -- Ada 2005 (AI-230): In case of components that are anonymous access
4297 -- types the level of accessibility depends on the enclosing type
4300 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4302 -- Ada 2005 (AI-254)
4305 CD : constant Node_Id :=
4306 Access_To_Subprogram_Definition
4307 (Access_Definition (Component_Def));
4309 if Present (CD) and then Protected_Present (CD) then
4311 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4316 -- Constrained array case
4319 T := Create_Itype (E_Void, P, Related_Id, 'T');
4322 if Nkind (Def) = N_Constrained_Array_Definition then
4324 -- Establish Implicit_Base as unconstrained base type
4326 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4328 Set_Etype (Implicit_Base, Implicit_Base);
4329 Set_Scope (Implicit_Base, Current_Scope);
4330 Set_Has_Delayed_Freeze (Implicit_Base);
4332 -- The constrained array type is a subtype of the unconstrained one
4334 Set_Ekind (T, E_Array_Subtype);
4335 Init_Size_Align (T);
4336 Set_Etype (T, Implicit_Base);
4337 Set_Scope (T, Current_Scope);
4338 Set_Is_Constrained (T, True);
4339 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
4340 Set_Has_Delayed_Freeze (T);
4342 -- Complete setup of implicit base type
4344 Set_First_Index (Implicit_Base, First_Index (T));
4345 Set_Component_Type (Implicit_Base, Element_Type);
4346 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
4347 Set_Component_Size (Implicit_Base, Uint_0);
4348 Set_Packed_Array_Type (Implicit_Base, Empty);
4349 Set_Has_Controlled_Component
4350 (Implicit_Base, Has_Controlled_Component
4352 or else Is_Controlled
4354 Set_Finalize_Storage_Only
4355 (Implicit_Base, Finalize_Storage_Only
4358 -- Unconstrained array case
4361 Set_Ekind (T, E_Array_Type);
4362 Init_Size_Align (T);
4364 Set_Scope (T, Current_Scope);
4365 Set_Component_Size (T, Uint_0);
4366 Set_Is_Constrained (T, False);
4367 Set_First_Index (T, First (Subtype_Marks (Def)));
4368 Set_Has_Delayed_Freeze (T, True);
4369 Set_Has_Task (T, Has_Task (Element_Type));
4370 Set_Has_Controlled_Component (T, Has_Controlled_Component
4373 Is_Controlled (Element_Type));
4374 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
4378 -- Common attributes for both cases
4380 Set_Component_Type (Base_Type (T), Element_Type);
4381 Set_Packed_Array_Type (T, Empty);
4383 if Aliased_Present (Component_Definition (Def)) then
4384 Set_Has_Aliased_Components (Etype (T));
4387 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4388 -- array type to ensure that objects of this type are initialized.
4390 if Ada_Version >= Ada_05
4391 and then Can_Never_Be_Null (Element_Type)
4393 Set_Can_Never_Be_Null (T);
4395 if Null_Exclusion_Present (Component_Definition (Def))
4397 -- No need to check itypes because in their case this check was
4398 -- done at their point of creation
4400 and then not Is_Itype (Element_Type)
4403 ("`NOT NULL` not allowed (null already excluded)",
4404 Subtype_Indication (Component_Definition (Def)));
4408 Priv := Private_Component (Element_Type);
4410 if Present (Priv) then
4412 -- Check for circular definitions
4414 if Priv = Any_Type then
4415 Set_Component_Type (Etype (T), Any_Type);
4417 -- There is a gap in the visibility of operations on the composite
4418 -- type only if the component type is defined in a different scope.
4420 elsif Scope (Priv) = Current_Scope then
4423 elsif Is_Limited_Type (Priv) then
4424 Set_Is_Limited_Composite (Etype (T));
4425 Set_Is_Limited_Composite (T);
4427 Set_Is_Private_Composite (Etype (T));
4428 Set_Is_Private_Composite (T);
4432 -- A syntax error in the declaration itself may lead to an empty index
4433 -- list, in which case do a minimal patch.
4435 if No (First_Index (T)) then
4436 Error_Msg_N ("missing index definition in array type declaration", T);
4439 Indices : constant List_Id :=
4440 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4442 Set_Discrete_Subtype_Definitions (Def, Indices);
4443 Set_First_Index (T, First (Indices));
4448 -- Create a concatenation operator for the new type. Internal array
4449 -- types created for packed entities do not need such, they are
4450 -- compatible with the user-defined type.
4452 if Number_Dimensions (T) = 1
4453 and then not Is_Packed_Array_Type (T)
4455 New_Concatenation_Op (T);
4458 -- In the case of an unconstrained array the parser has already verified
4459 -- that all the indices are unconstrained but we still need to make sure
4460 -- that the element type is constrained.
4462 if Is_Indefinite_Subtype (Element_Type) then
4464 ("unconstrained element type in array declaration",
4465 Subtype_Indication (Component_Def));
4467 elsif Is_Abstract_Type (Element_Type) then
4469 ("the type of a component cannot be abstract",
4470 Subtype_Indication (Component_Def));
4472 end Array_Type_Declaration;
4474 ------------------------------------------------------
4475 -- Replace_Anonymous_Access_To_Protected_Subprogram --
4476 ------------------------------------------------------
4478 function Replace_Anonymous_Access_To_Protected_Subprogram
4479 (N : Node_Id) return Entity_Id
4481 Loc : constant Source_Ptr := Sloc (N);
4483 Curr_Scope : constant Scope_Stack_Entry :=
4484 Scope_Stack.Table (Scope_Stack.Last);
4486 Anon : constant Entity_Id :=
4487 Make_Defining_Identifier (Loc,
4488 Chars => New_Internal_Name ('S'));
4496 Set_Is_Internal (Anon);
4499 when N_Component_Declaration |
4500 N_Unconstrained_Array_Definition |
4501 N_Constrained_Array_Definition =>
4502 Comp := Component_Definition (N);
4503 Acc := Access_Definition (Comp);
4505 when N_Discriminant_Specification =>
4506 Comp := Discriminant_Type (N);
4509 when N_Parameter_Specification =>
4510 Comp := Parameter_Type (N);
4513 when N_Access_Function_Definition =>
4514 Comp := Result_Definition (N);
4517 when N_Object_Declaration =>
4518 Comp := Object_Definition (N);
4521 when N_Function_Specification =>
4522 Comp := Result_Definition (N);
4526 raise Program_Error;
4529 Decl := Make_Full_Type_Declaration (Loc,
4530 Defining_Identifier => Anon,
4532 Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
4534 Mark_Rewrite_Insertion (Decl);
4536 -- Insert the new declaration in the nearest enclosing scope. If the
4537 -- node is a body and N is its return type, the declaration belongs in
4538 -- the enclosing scope.
4542 if Nkind (P) = N_Subprogram_Body
4543 and then Nkind (N) = N_Function_Specification
4548 while Present (P) and then not Has_Declarations (P) loop
4552 pragma Assert (Present (P));
4554 if Nkind (P) = N_Package_Specification then
4555 Prepend (Decl, Visible_Declarations (P));
4557 Prepend (Decl, Declarations (P));
4560 -- Replace the anonymous type with an occurrence of the new declaration.
4561 -- In all cases the rewritten node does not have the null-exclusion
4562 -- attribute because (if present) it was already inherited by the
4563 -- anonymous entity (Anon). Thus, in case of components we do not
4564 -- inherit this attribute.
4566 if Nkind (N) = N_Parameter_Specification then
4567 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4568 Set_Etype (Defining_Identifier (N), Anon);
4569 Set_Null_Exclusion_Present (N, False);
4571 elsif Nkind (N) = N_Object_Declaration then
4572 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4573 Set_Etype (Defining_Identifier (N), Anon);
4575 elsif Nkind (N) = N_Access_Function_Definition then
4576 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4578 elsif Nkind (N) = N_Function_Specification then
4579 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4580 Set_Etype (Defining_Unit_Name (N), Anon);
4584 Make_Component_Definition (Loc,
4585 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
4588 Mark_Rewrite_Insertion (Comp);
4590 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
4594 -- Temporarily remove the current scope (record or subprogram) from
4595 -- the stack to add the new declarations to the enclosing scope.
4597 Scope_Stack.Decrement_Last;
4599 Set_Is_Itype (Anon);
4600 Scope_Stack.Append (Curr_Scope);
4603 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
4604 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
4606 end Replace_Anonymous_Access_To_Protected_Subprogram;
4608 -------------------------------
4609 -- Build_Derived_Access_Type --
4610 -------------------------------
4612 procedure Build_Derived_Access_Type
4614 Parent_Type : Entity_Id;
4615 Derived_Type : Entity_Id)
4617 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
4619 Desig_Type : Entity_Id;
4621 Discr_Con_Elist : Elist_Id;
4622 Discr_Con_El : Elmt_Id;
4626 -- Set the designated type so it is available in case this is an access
4627 -- to a self-referential type, e.g. a standard list type with a next
4628 -- pointer. Will be reset after subtype is built.
4630 Set_Directly_Designated_Type
4631 (Derived_Type, Designated_Type (Parent_Type));
4633 Subt := Process_Subtype (S, N);
4635 if Nkind (S) /= N_Subtype_Indication
4636 and then Subt /= Base_Type (Subt)
4638 Set_Ekind (Derived_Type, E_Access_Subtype);
4641 if Ekind (Derived_Type) = E_Access_Subtype then
4643 Pbase : constant Entity_Id := Base_Type (Parent_Type);
4644 Ibase : constant Entity_Id :=
4645 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
4646 Svg_Chars : constant Name_Id := Chars (Ibase);
4647 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
4650 Copy_Node (Pbase, Ibase);
4652 Set_Chars (Ibase, Svg_Chars);
4653 Set_Next_Entity (Ibase, Svg_Next_E);
4654 Set_Sloc (Ibase, Sloc (Derived_Type));
4655 Set_Scope (Ibase, Scope (Derived_Type));
4656 Set_Freeze_Node (Ibase, Empty);
4657 Set_Is_Frozen (Ibase, False);
4658 Set_Comes_From_Source (Ibase, False);
4659 Set_Is_First_Subtype (Ibase, False);
4661 Set_Etype (Ibase, Pbase);
4662 Set_Etype (Derived_Type, Ibase);
4666 Set_Directly_Designated_Type
4667 (Derived_Type, Designated_Type (Subt));
4669 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
4670 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
4671 Set_Size_Info (Derived_Type, Parent_Type);
4672 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
4673 Set_Depends_On_Private (Derived_Type,
4674 Has_Private_Component (Derived_Type));
4675 Conditional_Delay (Derived_Type, Subt);
4677 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
4678 -- that it is not redundant.
4680 if Null_Exclusion_Present (Type_Definition (N)) then
4681 Set_Can_Never_Be_Null (Derived_Type);
4683 if Can_Never_Be_Null (Parent_Type)
4687 ("`NOT NULL` not allowed (& already excludes null)",
4691 elsif Can_Never_Be_Null (Parent_Type) then
4692 Set_Can_Never_Be_Null (Derived_Type);
4695 -- Note: we do not copy the Storage_Size_Variable, since we always go to
4696 -- the root type for this information.
4698 -- Apply range checks to discriminants for derived record case
4699 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
4701 Desig_Type := Designated_Type (Derived_Type);
4702 if Is_Composite_Type (Desig_Type)
4703 and then (not Is_Array_Type (Desig_Type))
4704 and then Has_Discriminants (Desig_Type)
4705 and then Base_Type (Desig_Type) /= Desig_Type
4707 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
4708 Discr_Con_El := First_Elmt (Discr_Con_Elist);
4710 Discr := First_Discriminant (Base_Type (Desig_Type));
4711 while Present (Discr_Con_El) loop
4712 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
4713 Next_Elmt (Discr_Con_El);
4714 Next_Discriminant (Discr);
4717 end Build_Derived_Access_Type;
4719 ------------------------------
4720 -- Build_Derived_Array_Type --
4721 ------------------------------
4723 procedure Build_Derived_Array_Type
4725 Parent_Type : Entity_Id;
4726 Derived_Type : Entity_Id)
4728 Loc : constant Source_Ptr := Sloc (N);
4729 Tdef : constant Node_Id := Type_Definition (N);
4730 Indic : constant Node_Id := Subtype_Indication (Tdef);
4731 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
4732 Implicit_Base : Entity_Id;
4733 New_Indic : Node_Id;
4735 procedure Make_Implicit_Base;
4736 -- If the parent subtype is constrained, the derived type is a subtype
4737 -- of an implicit base type derived from the parent base.
4739 ------------------------
4740 -- Make_Implicit_Base --
4741 ------------------------
4743 procedure Make_Implicit_Base is
4746 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
4748 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
4749 Set_Etype (Implicit_Base, Parent_Base);
4751 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
4752 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
4754 Set_Has_Delayed_Freeze (Implicit_Base, True);
4755 end Make_Implicit_Base;
4757 -- Start of processing for Build_Derived_Array_Type
4760 if not Is_Constrained (Parent_Type) then
4761 if Nkind (Indic) /= N_Subtype_Indication then
4762 Set_Ekind (Derived_Type, E_Array_Type);
4764 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4765 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
4767 Set_Has_Delayed_Freeze (Derived_Type, True);
4771 Set_Etype (Derived_Type, Implicit_Base);
4774 Make_Subtype_Declaration (Loc,
4775 Defining_Identifier => Derived_Type,
4776 Subtype_Indication =>
4777 Make_Subtype_Indication (Loc,
4778 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
4779 Constraint => Constraint (Indic)));
4781 Rewrite (N, New_Indic);
4786 if Nkind (Indic) /= N_Subtype_Indication then
4789 Set_Ekind (Derived_Type, Ekind (Parent_Type));
4790 Set_Etype (Derived_Type, Implicit_Base);
4791 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
4794 Error_Msg_N ("illegal constraint on constrained type", Indic);
4798 -- If parent type is not a derived type itself, and is declared in
4799 -- closed scope (e.g. a subprogram), then we must explicitly introduce
4800 -- the new type's concatenation operator since Derive_Subprograms
4801 -- will not inherit the parent's operator. If the parent type is
4802 -- unconstrained, the operator is of the unconstrained base type.
4804 if Number_Dimensions (Parent_Type) = 1
4805 and then not Is_Limited_Type (Parent_Type)
4806 and then not Is_Derived_Type (Parent_Type)
4807 and then not Is_Package_Or_Generic_Package
4808 (Scope (Base_Type (Parent_Type)))
4810 if not Is_Constrained (Parent_Type)
4811 and then Is_Constrained (Derived_Type)
4813 New_Concatenation_Op (Implicit_Base);
4815 New_Concatenation_Op (Derived_Type);
4818 end Build_Derived_Array_Type;
4820 -----------------------------------
4821 -- Build_Derived_Concurrent_Type --
4822 -----------------------------------
4824 procedure Build_Derived_Concurrent_Type
4826 Parent_Type : Entity_Id;
4827 Derived_Type : Entity_Id)
4829 D_Constraint : Node_Id;
4830 Disc_Spec : Node_Id;
4831 Old_Disc : Entity_Id;
4832 New_Disc : Entity_Id;
4834 Constraint_Present : constant Boolean :=
4835 Nkind (Subtype_Indication (Type_Definition (N)))
4836 = N_Subtype_Indication;
4839 Set_Stored_Constraint (Derived_Type, No_Elist);
4841 -- Copy Storage_Size and Relative_Deadline variables if task case
4843 if Is_Task_Type (Parent_Type) then
4844 Set_Storage_Size_Variable (Derived_Type,
4845 Storage_Size_Variable (Parent_Type));
4846 Set_Relative_Deadline_Variable (Derived_Type,
4847 Relative_Deadline_Variable (Parent_Type));
4850 if Present (Discriminant_Specifications (N)) then
4851 Push_Scope (Derived_Type);
4852 Check_Or_Process_Discriminants (N, Derived_Type);
4855 elsif Constraint_Present then
4857 -- Build constrained subtype and derive from it
4860 Loc : constant Source_Ptr := Sloc (N);
4861 Anon : constant Entity_Id :=
4862 Make_Defining_Identifier (Loc,
4863 New_External_Name (Chars (Derived_Type), 'T'));
4868 Make_Subtype_Declaration (Loc,
4869 Defining_Identifier => Anon,
4870 Subtype_Indication =>
4871 Subtype_Indication (Type_Definition (N)));
4872 Insert_Before (N, Decl);
4875 Rewrite (Subtype_Indication (Type_Definition (N)),
4876 New_Occurrence_Of (Anon, Loc));
4877 Set_Analyzed (Derived_Type, False);
4883 -- All attributes are inherited from parent. In particular,
4884 -- entries and the corresponding record type are the same.
4885 -- Discriminants may be renamed, and must be treated separately.
4887 Set_Has_Discriminants
4888 (Derived_Type, Has_Discriminants (Parent_Type));
4889 Set_Corresponding_Record_Type
4890 (Derived_Type, Corresponding_Record_Type (Parent_Type));
4892 -- Is_Constrained is set according the parent subtype, but is set to
4893 -- False if the derived type is declared with new discriminants.
4897 (Is_Constrained (Parent_Type) or else Constraint_Present)
4898 and then not Present (Discriminant_Specifications (N)));
4900 if Constraint_Present then
4901 if not Has_Discriminants (Parent_Type) then
4902 Error_Msg_N ("untagged parent must have discriminants", N);
4904 elsif Present (Discriminant_Specifications (N)) then
4906 -- Verify that new discriminants are used to constrain old ones
4911 (Constraint (Subtype_Indication (Type_Definition (N)))));
4913 Old_Disc := First_Discriminant (Parent_Type);
4914 New_Disc := First_Discriminant (Derived_Type);
4915 Disc_Spec := First (Discriminant_Specifications (N));
4916 while Present (Old_Disc) and then Present (Disc_Spec) loop
4917 if Nkind (Discriminant_Type (Disc_Spec)) /=
4920 Analyze (Discriminant_Type (Disc_Spec));
4922 if not Subtypes_Statically_Compatible (
4923 Etype (Discriminant_Type (Disc_Spec)),
4927 ("not statically compatible with parent discriminant",
4928 Discriminant_Type (Disc_Spec));
4932 if Nkind (D_Constraint) = N_Identifier
4933 and then Chars (D_Constraint) /=
4934 Chars (Defining_Identifier (Disc_Spec))
4936 Error_Msg_N ("new discriminants must constrain old ones",
4939 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
4942 Next_Discriminant (Old_Disc);
4943 Next_Discriminant (New_Disc);
4947 if Present (Old_Disc) or else Present (Disc_Spec) then
4948 Error_Msg_N ("discriminant mismatch in derivation", N);
4953 elsif Present (Discriminant_Specifications (N)) then
4955 ("missing discriminant constraint in untagged derivation",
4959 if Present (Discriminant_Specifications (N)) then
4960 Old_Disc := First_Discriminant (Parent_Type);
4961 while Present (Old_Disc) loop
4963 if No (Next_Entity (Old_Disc))
4964 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
4966 Set_Next_Entity (Last_Entity (Derived_Type),
4967 Next_Entity (Old_Disc));
4971 Next_Discriminant (Old_Disc);
4975 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
4976 if Has_Discriminants (Parent_Type) then
4977 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
4978 Set_Discriminant_Constraint (
4979 Derived_Type, Discriminant_Constraint (Parent_Type));
4983 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
4985 Set_Has_Completion (Derived_Type);
4986 end Build_Derived_Concurrent_Type;
4988 ------------------------------------
4989 -- Build_Derived_Enumeration_Type --
4990 ------------------------------------
4992 procedure Build_Derived_Enumeration_Type
4994 Parent_Type : Entity_Id;
4995 Derived_Type : Entity_Id)
4997 Loc : constant Source_Ptr := Sloc (N);
4998 Def : constant Node_Id := Type_Definition (N);
4999 Indic : constant Node_Id := Subtype_Indication (Def);
5000 Implicit_Base : Entity_Id;
5001 Literal : Entity_Id;
5002 New_Lit : Entity_Id;
5003 Literals_List : List_Id;
5004 Type_Decl : Node_Id;
5006 Rang_Expr : Node_Id;
5009 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
5010 -- not have explicit literals lists we need to process types derived
5011 -- from them specially. This is handled by Derived_Standard_Character.
5012 -- If the parent type is a generic type, there are no literals either,
5013 -- and we construct the same skeletal representation as for the generic
5016 if Is_Standard_Character_Type (Parent_Type) then
5017 Derived_Standard_Character (N, Parent_Type, Derived_Type);
5019 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
5025 if Nkind (Indic) /= N_Subtype_Indication then
5027 Make_Attribute_Reference (Loc,
5028 Attribute_Name => Name_First,
5029 Prefix => New_Reference_To (Derived_Type, Loc));
5030 Set_Etype (Lo, Derived_Type);
5033 Make_Attribute_Reference (Loc,
5034 Attribute_Name => Name_Last,
5035 Prefix => New_Reference_To (Derived_Type, Loc));
5036 Set_Etype (Hi, Derived_Type);
5038 Set_Scalar_Range (Derived_Type,
5044 -- Analyze subtype indication and verify compatibility
5045 -- with parent type.
5047 if Base_Type (Process_Subtype (Indic, N)) /=
5048 Base_Type (Parent_Type)
5051 ("illegal constraint for formal discrete type", N);
5057 -- If a constraint is present, analyze the bounds to catch
5058 -- premature usage of the derived literals.
5060 if Nkind (Indic) = N_Subtype_Indication
5061 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
5063 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
5064 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
5067 -- Introduce an implicit base type for the derived type even if there
5068 -- is no constraint attached to it, since this seems closer to the
5069 -- Ada semantics. Build a full type declaration tree for the derived
5070 -- type using the implicit base type as the defining identifier. The
5071 -- build a subtype declaration tree which applies the constraint (if
5072 -- any) have it replace the derived type declaration.
5074 Literal := First_Literal (Parent_Type);
5075 Literals_List := New_List;
5076 while Present (Literal)
5077 and then Ekind (Literal) = E_Enumeration_Literal
5079 -- Literals of the derived type have the same representation as
5080 -- those of the parent type, but this representation can be
5081 -- overridden by an explicit representation clause. Indicate
5082 -- that there is no explicit representation given yet. These
5083 -- derived literals are implicit operations of the new type,
5084 -- and can be overridden by explicit ones.
5086 if Nkind (Literal) = N_Defining_Character_Literal then
5088 Make_Defining_Character_Literal (Loc, Chars (Literal));
5090 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
5093 Set_Ekind (New_Lit, E_Enumeration_Literal);
5094 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
5095 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
5096 Set_Enumeration_Rep_Expr (New_Lit, Empty);
5097 Set_Alias (New_Lit, Literal);
5098 Set_Is_Known_Valid (New_Lit, True);
5100 Append (New_Lit, Literals_List);
5101 Next_Literal (Literal);
5105 Make_Defining_Identifier (Sloc (Derived_Type),
5106 New_External_Name (Chars (Derived_Type), 'B'));
5108 -- Indicate the proper nature of the derived type. This must be done
5109 -- before analysis of the literals, to recognize cases when a literal
5110 -- may be hidden by a previous explicit function definition (cf.
5113 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
5114 Set_Etype (Derived_Type, Implicit_Base);
5117 Make_Full_Type_Declaration (Loc,
5118 Defining_Identifier => Implicit_Base,
5119 Discriminant_Specifications => No_List,
5121 Make_Enumeration_Type_Definition (Loc, Literals_List));
5123 Mark_Rewrite_Insertion (Type_Decl);
5124 Insert_Before (N, Type_Decl);
5125 Analyze (Type_Decl);
5127 -- After the implicit base is analyzed its Etype needs to be changed
5128 -- to reflect the fact that it is derived from the parent type which
5129 -- was ignored during analysis. We also set the size at this point.
5131 Set_Etype (Implicit_Base, Parent_Type);
5133 Set_Size_Info (Implicit_Base, Parent_Type);
5134 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
5135 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5137 Set_Has_Non_Standard_Rep
5138 (Implicit_Base, Has_Non_Standard_Rep
5140 Set_Has_Delayed_Freeze (Implicit_Base);
5142 -- Process the subtype indication including a validation check on the
5143 -- constraint, if any. If a constraint is given, its bounds must be
5144 -- implicitly converted to the new type.
5146 if Nkind (Indic) = N_Subtype_Indication then
5148 R : constant Node_Id :=
5149 Range_Expression (Constraint (Indic));
5152 if Nkind (R) = N_Range then
5153 Hi := Build_Scalar_Bound
5154 (High_Bound (R), Parent_Type, Implicit_Base);
5155 Lo := Build_Scalar_Bound
5156 (Low_Bound (R), Parent_Type, Implicit_Base);
5159 -- Constraint is a Range attribute. Replace with explicit
5160 -- mention of the bounds of the prefix, which must be a
5163 Analyze (Prefix (R));
5165 Convert_To (Implicit_Base,
5166 Make_Attribute_Reference (Loc,
5167 Attribute_Name => Name_Last,
5169 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5172 Convert_To (Implicit_Base,
5173 Make_Attribute_Reference (Loc,
5174 Attribute_Name => Name_First,
5176 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5183 (Type_High_Bound (Parent_Type),
5184 Parent_Type, Implicit_Base);
5187 (Type_Low_Bound (Parent_Type),
5188 Parent_Type, Implicit_Base);
5196 -- If we constructed a default range for the case where no range
5197 -- was given, then the expressions in the range must not freeze
5198 -- since they do not correspond to expressions in the source.
5200 if Nkind (Indic) /= N_Subtype_Indication then
5201 Set_Must_Not_Freeze (Lo);
5202 Set_Must_Not_Freeze (Hi);
5203 Set_Must_Not_Freeze (Rang_Expr);
5207 Make_Subtype_Declaration (Loc,
5208 Defining_Identifier => Derived_Type,
5209 Subtype_Indication =>
5210 Make_Subtype_Indication (Loc,
5211 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5213 Make_Range_Constraint (Loc,
5214 Range_Expression => Rang_Expr))));
5218 -- If pragma Discard_Names applies on the first subtype of the parent
5219 -- type, then it must be applied on this subtype as well.
5221 if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5222 Set_Discard_Names (Derived_Type);
5225 -- Apply a range check. Since this range expression doesn't have an
5226 -- Etype, we have to specifically pass the Source_Typ parameter. Is
5229 if Nkind (Indic) = N_Subtype_Indication then
5230 Apply_Range_Check (Range_Expression (Constraint (Indic)),
5232 Source_Typ => Entity (Subtype_Mark (Indic)));
5235 end Build_Derived_Enumeration_Type;
5237 --------------------------------
5238 -- Build_Derived_Numeric_Type --
5239 --------------------------------
5241 procedure Build_Derived_Numeric_Type
5243 Parent_Type : Entity_Id;
5244 Derived_Type : Entity_Id)
5246 Loc : constant Source_Ptr := Sloc (N);
5247 Tdef : constant Node_Id := Type_Definition (N);
5248 Indic : constant Node_Id := Subtype_Indication (Tdef);
5249 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5250 No_Constraint : constant Boolean := Nkind (Indic) /=
5251 N_Subtype_Indication;
5252 Implicit_Base : Entity_Id;
5258 -- Process the subtype indication including a validation check on
5259 -- the constraint if any.
5261 Discard_Node (Process_Subtype (Indic, N));
5263 -- Introduce an implicit base type for the derived type even if there
5264 -- is no constraint attached to it, since this seems closer to the Ada
5268 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5270 Set_Etype (Implicit_Base, Parent_Base);
5271 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5272 Set_Size_Info (Implicit_Base, Parent_Base);
5273 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5274 Set_Parent (Implicit_Base, Parent (Derived_Type));
5275 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
5277 -- Set RM Size for discrete type or decimal fixed-point type
5278 -- Ordinary fixed-point is excluded, why???
5280 if Is_Discrete_Type (Parent_Base)
5281 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5283 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5286 Set_Has_Delayed_Freeze (Implicit_Base);
5288 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
5289 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5291 Set_Scalar_Range (Implicit_Base,
5296 if Has_Infinities (Parent_Base) then
5297 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5300 -- The Derived_Type, which is the entity of the declaration, is a
5301 -- subtype of the implicit base. Its Ekind is a subtype, even in the
5302 -- absence of an explicit constraint.
5304 Set_Etype (Derived_Type, Implicit_Base);
5306 -- If we did not have a constraint, then the Ekind is set from the
5307 -- parent type (otherwise Process_Subtype has set the bounds)
5309 if No_Constraint then
5310 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5313 -- If we did not have a range constraint, then set the range from the
5314 -- parent type. Otherwise, the call to Process_Subtype has set the
5318 or else not Has_Range_Constraint (Indic)
5320 Set_Scalar_Range (Derived_Type,
5322 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
5323 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5324 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5326 if Has_Infinities (Parent_Type) then
5327 Set_Includes_Infinities (Scalar_Range (Derived_Type));
5330 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
5333 Set_Is_Descendent_Of_Address (Derived_Type,
5334 Is_Descendent_Of_Address (Parent_Type));
5335 Set_Is_Descendent_Of_Address (Implicit_Base,
5336 Is_Descendent_Of_Address (Parent_Type));
5338 -- Set remaining type-specific fields, depending on numeric type
5340 if Is_Modular_Integer_Type (Parent_Type) then
5341 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5343 Set_Non_Binary_Modulus
5344 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5347 (Implicit_Base, Is_Known_Valid (Parent_Base));
5349 elsif Is_Floating_Point_Type (Parent_Type) then
5351 -- Digits of base type is always copied from the digits value of
5352 -- the parent base type, but the digits of the derived type will
5353 -- already have been set if there was a constraint present.
5355 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5356 Set_Vax_Float (Implicit_Base, Vax_Float (Parent_Base));
5358 if No_Constraint then
5359 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5362 elsif Is_Fixed_Point_Type (Parent_Type) then
5364 -- Small of base type and derived type are always copied from the
5365 -- parent base type, since smalls never change. The delta of the
5366 -- base type is also copied from the parent base type. However the
5367 -- delta of the derived type will have been set already if a
5368 -- constraint was present.
5370 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
5371 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5372 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
5374 if No_Constraint then
5375 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
5378 -- The scale and machine radix in the decimal case are always
5379 -- copied from the parent base type.
5381 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
5382 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
5383 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
5385 Set_Machine_Radix_10
5386 (Derived_Type, Machine_Radix_10 (Parent_Base));
5387 Set_Machine_Radix_10
5388 (Implicit_Base, Machine_Radix_10 (Parent_Base));
5390 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5392 if No_Constraint then
5393 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
5396 -- the analysis of the subtype_indication sets the
5397 -- digits value of the derived type.
5404 -- The type of the bounds is that of the parent type, and they
5405 -- must be converted to the derived type.
5407 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
5409 -- The implicit_base should be frozen when the derived type is frozen,
5410 -- but note that it is used in the conversions of the bounds. For fixed
5411 -- types we delay the determination of the bounds until the proper
5412 -- freezing point. For other numeric types this is rejected by GCC, for
5413 -- reasons that are currently unclear (???), so we choose to freeze the
5414 -- implicit base now. In the case of integers and floating point types
5415 -- this is harmless because subsequent representation clauses cannot
5416 -- affect anything, but it is still baffling that we cannot use the
5417 -- same mechanism for all derived numeric types.
5419 -- There is a further complication: actually *some* representation
5420 -- clauses can affect the implicit base type. Namely, attribute
5421 -- definition clauses for stream-oriented attributes need to set the
5422 -- corresponding TSS entries on the base type, and this normally cannot
5423 -- be done after the base type is frozen, so the circuitry in
5424 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility and
5425 -- not use Set_TSS in this case.
5427 if Is_Fixed_Point_Type (Parent_Type) then
5428 Conditional_Delay (Implicit_Base, Parent_Type);
5430 Freeze_Before (N, Implicit_Base);
5432 end Build_Derived_Numeric_Type;
5434 --------------------------------
5435 -- Build_Derived_Private_Type --
5436 --------------------------------
5438 procedure Build_Derived_Private_Type
5440 Parent_Type : Entity_Id;
5441 Derived_Type : Entity_Id;
5442 Is_Completion : Boolean;
5443 Derive_Subps : Boolean := True)
5445 Loc : constant Source_Ptr := Sloc (N);
5446 Der_Base : Entity_Id;
5448 Full_Decl : Node_Id := Empty;
5449 Full_Der : Entity_Id;
5451 Last_Discr : Entity_Id;
5452 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
5453 Swapped : Boolean := False;
5455 procedure Copy_And_Build;
5456 -- Copy derived type declaration, replace parent with its full view,
5457 -- and analyze new declaration.
5459 --------------------
5460 -- Copy_And_Build --
5461 --------------------
5463 procedure Copy_And_Build is
5467 if Ekind (Parent_Type) in Record_Kind
5469 (Ekind (Parent_Type) in Enumeration_Kind
5470 and then not Is_Standard_Character_Type (Parent_Type)
5471 and then not Is_Generic_Type (Root_Type (Parent_Type)))
5473 Full_N := New_Copy_Tree (N);
5474 Insert_After (N, Full_N);
5475 Build_Derived_Type (
5476 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
5479 Build_Derived_Type (
5480 N, Parent_Type, Full_Der, True, Derive_Subps => False);
5484 -- Start of processing for Build_Derived_Private_Type
5487 if Is_Tagged_Type (Parent_Type) then
5488 Full_P := Full_View (Parent_Type);
5490 -- A type extension of a type with unknown discriminants is an
5491 -- indefinite type that the back-end cannot handle directly.
5492 -- We treat it as a private type, and build a completion that is
5493 -- derived from the full view of the parent, and hopefully has
5494 -- known discriminants.
5496 -- If the full view of the parent type has an underlying record view,
5497 -- use it to generate the underlying record view of this derived type
5498 -- (required for chains of derivations with unknown discriminants).
5500 -- Minor optimization: we avoid the generation of useless underlying
5501 -- record view entities if the private type declaration has unknown
5502 -- discriminants but its corresponding full view has no
5505 if Has_Unknown_Discriminants (Parent_Type)
5506 and then Present (Full_P)
5507 and then (Has_Discriminants (Full_P)
5508 or else Present (Underlying_Record_View (Full_P)))
5509 and then not In_Open_Scopes (Par_Scope)
5510 and then Expander_Active
5513 Full_Der : constant Entity_Id :=
5514 Make_Defining_Identifier (Loc,
5515 Chars => New_Internal_Name ('T'));
5516 New_Ext : constant Node_Id :=
5518 (Record_Extension_Part (Type_Definition (N)));
5522 Build_Derived_Record_Type
5523 (N, Parent_Type, Derived_Type, Derive_Subps);
5525 -- Build anonymous completion, as a derivation from the full
5526 -- view of the parent. This is not a completion in the usual
5527 -- sense, because the current type is not private.
5530 Make_Full_Type_Declaration (Loc,
5531 Defining_Identifier => Full_Der,
5533 Make_Derived_Type_Definition (Loc,
5534 Subtype_Indication =>
5536 (Subtype_Indication (Type_Definition (N))),
5537 Record_Extension_Part => New_Ext));
5539 -- If the parent type has an underlying record view, use it
5540 -- here to build the new underlying record view.
5542 if Present (Underlying_Record_View (Full_P)) then
5544 (Nkind (Subtype_Indication (Type_Definition (Decl)))
5546 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
5547 Underlying_Record_View (Full_P));
5550 Install_Private_Declarations (Par_Scope);
5551 Install_Visible_Declarations (Par_Scope);
5552 Insert_Before (N, Decl);
5554 -- Mark entity as an underlying record view before analysis,
5555 -- to avoid generating the list of its primitive operations
5556 -- (which is not really required for this entity) and thus
5557 -- prevent spurious errors associated with missing overriding
5558 -- of abstract primitives (overridden only for Derived_Type).
5560 Set_Ekind (Full_Der, E_Record_Type);
5561 Set_Is_Underlying_Record_View (Full_Der);
5565 pragma Assert (Has_Discriminants (Full_Der)
5566 and then not Has_Unknown_Discriminants (Full_Der));
5568 Uninstall_Declarations (Par_Scope);
5570 -- Freeze the underlying record view, to prevent generation of
5571 -- useless dispatching information, which is simply shared with
5572 -- the real derived type.
5574 Set_Is_Frozen (Full_Der);
5576 -- Set up links between real entity and underlying record view
5578 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
5579 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
5582 -- If discriminants are known, build derived record
5585 Build_Derived_Record_Type
5586 (N, Parent_Type, Derived_Type, Derive_Subps);
5591 elsif Has_Discriminants (Parent_Type) then
5592 if Present (Full_View (Parent_Type)) then
5593 if not Is_Completion then
5595 -- Copy declaration for subsequent analysis, to provide a
5596 -- completion for what is a private declaration. Indicate that
5597 -- the full type is internally generated.
5599 Full_Decl := New_Copy_Tree (N);
5600 Full_Der := New_Copy (Derived_Type);
5601 Set_Comes_From_Source (Full_Decl, False);
5602 Set_Comes_From_Source (Full_Der, False);
5604 Insert_After (N, Full_Decl);
5607 -- If this is a completion, the full view being built is itself
5608 -- private. We build a subtype of the parent with the same
5609 -- constraints as this full view, to convey to the back end the
5610 -- constrained components and the size of this subtype. If the
5611 -- parent is constrained, its full view can serve as the
5612 -- underlying full view of the derived type.
5614 if No (Discriminant_Specifications (N)) then
5615 if Nkind (Subtype_Indication (Type_Definition (N))) =
5616 N_Subtype_Indication
5618 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
5620 elsif Is_Constrained (Full_View (Parent_Type)) then
5621 Set_Underlying_Full_View
5622 (Derived_Type, Full_View (Parent_Type));
5626 -- If there are new discriminants, the parent subtype is
5627 -- constrained by them, but it is not clear how to build
5628 -- the Underlying_Full_View in this case???
5635 -- Build partial view of derived type from partial view of parent
5637 Build_Derived_Record_Type
5638 (N, Parent_Type, Derived_Type, Derive_Subps);
5640 if Present (Full_View (Parent_Type)) and then not Is_Completion then
5641 if not In_Open_Scopes (Par_Scope)
5642 or else not In_Same_Source_Unit (N, Parent_Type)
5644 -- Swap partial and full views temporarily
5646 Install_Private_Declarations (Par_Scope);
5647 Install_Visible_Declarations (Par_Scope);
5651 -- Build full view of derived type from full view of parent which
5652 -- is now installed. Subprograms have been derived on the partial
5653 -- view, the completion does not derive them anew.
5655 if not Is_Tagged_Type (Parent_Type) then
5657 -- If the parent is itself derived from another private type,
5658 -- installing the private declarations has not affected its
5659 -- privacy status, so use its own full view explicitly.
5661 if Is_Private_Type (Parent_Type) then
5662 Build_Derived_Record_Type
5663 (Full_Decl, Full_View (Parent_Type), Full_Der, False);
5665 Build_Derived_Record_Type
5666 (Full_Decl, Parent_Type, Full_Der, False);
5670 -- If full view of parent is tagged, the completion inherits
5671 -- the proper primitive operations.
5673 Set_Defining_Identifier (Full_Decl, Full_Der);
5674 Build_Derived_Record_Type
5675 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
5676 Set_Analyzed (Full_Decl);
5680 Uninstall_Declarations (Par_Scope);
5682 if In_Open_Scopes (Par_Scope) then
5683 Install_Visible_Declarations (Par_Scope);
5687 Der_Base := Base_Type (Derived_Type);
5688 Set_Full_View (Derived_Type, Full_Der);
5689 Set_Full_View (Der_Base, Base_Type (Full_Der));
5691 -- Copy the discriminant list from full view to the partial views
5692 -- (base type and its subtype). Gigi requires that the partial and
5693 -- full views have the same discriminants.
5695 -- Note that since the partial view is pointing to discriminants
5696 -- in the full view, their scope will be that of the full view.
5697 -- This might cause some front end problems and need adjustment???
5699 Discr := First_Discriminant (Base_Type (Full_Der));
5700 Set_First_Entity (Der_Base, Discr);
5703 Last_Discr := Discr;
5704 Next_Discriminant (Discr);
5705 exit when No (Discr);
5708 Set_Last_Entity (Der_Base, Last_Discr);
5710 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
5711 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
5712 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
5715 -- If this is a completion, the derived type stays private and
5716 -- there is no need to create a further full view, except in the
5717 -- unusual case when the derivation is nested within a child unit,
5723 elsif Present (Full_View (Parent_Type))
5724 and then Has_Discriminants (Full_View (Parent_Type))
5726 if Has_Unknown_Discriminants (Parent_Type)
5727 and then Nkind (Subtype_Indication (Type_Definition (N))) =
5728 N_Subtype_Indication
5731 ("cannot constrain type with unknown discriminants",
5732 Subtype_Indication (Type_Definition (N)));
5736 -- If full view of parent is a record type, build full view as a
5737 -- derivation from the parent's full view. Partial view remains
5738 -- private. For code generation and linking, the full view must have
5739 -- the same public status as the partial one. This full view is only
5740 -- needed if the parent type is in an enclosing scope, so that the
5741 -- full view may actually become visible, e.g. in a child unit. This
5742 -- is both more efficient, and avoids order of freezing problems with
5743 -- the added entities.
5745 if not Is_Private_Type (Full_View (Parent_Type))
5746 and then (In_Open_Scopes (Scope (Parent_Type)))
5748 Full_Der := Make_Defining_Identifier (Sloc (Derived_Type),
5749 Chars (Derived_Type));
5750 Set_Is_Itype (Full_Der);
5751 Set_Has_Private_Declaration (Full_Der);
5752 Set_Has_Private_Declaration (Derived_Type);
5753 Set_Associated_Node_For_Itype (Full_Der, N);
5754 Set_Parent (Full_Der, Parent (Derived_Type));
5755 Set_Full_View (Derived_Type, Full_Der);
5756 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
5757 Full_P := Full_View (Parent_Type);
5758 Exchange_Declarations (Parent_Type);
5760 Exchange_Declarations (Full_P);
5763 Build_Derived_Record_Type
5764 (N, Full_View (Parent_Type), Derived_Type,
5765 Derive_Subps => False);
5768 -- In any case, the primitive operations are inherited from the
5769 -- parent type, not from the internal full view.
5771 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
5773 if Derive_Subps then
5774 Derive_Subprograms (Parent_Type, Derived_Type);
5778 -- Untagged type, No discriminants on either view
5780 if Nkind (Subtype_Indication (Type_Definition (N))) =
5781 N_Subtype_Indication
5784 ("illegal constraint on type without discriminants", N);
5787 if Present (Discriminant_Specifications (N))
5788 and then Present (Full_View (Parent_Type))
5789 and then not Is_Tagged_Type (Full_View (Parent_Type))
5791 Error_Msg_N ("cannot add discriminants to untagged type", N);
5794 Set_Stored_Constraint (Derived_Type, No_Elist);
5795 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5796 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
5797 Set_Has_Controlled_Component
5798 (Derived_Type, Has_Controlled_Component
5801 -- Direct controlled types do not inherit Finalize_Storage_Only flag
5803 if not Is_Controlled (Parent_Type) then
5804 Set_Finalize_Storage_Only
5805 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
5808 -- Construct the implicit full view by deriving from full view of the
5809 -- parent type. In order to get proper visibility, we install the
5810 -- parent scope and its declarations.
5812 -- ??? If the parent is untagged private and its completion is
5813 -- tagged, this mechanism will not work because we cannot derive from
5814 -- the tagged full view unless we have an extension.
5816 if Present (Full_View (Parent_Type))
5817 and then not Is_Tagged_Type (Full_View (Parent_Type))
5818 and then not Is_Completion
5821 Make_Defining_Identifier (Sloc (Derived_Type),
5822 Chars => Chars (Derived_Type));
5823 Set_Is_Itype (Full_Der);
5824 Set_Has_Private_Declaration (Full_Der);
5825 Set_Has_Private_Declaration (Derived_Type);
5826 Set_Associated_Node_For_Itype (Full_Der, N);
5827 Set_Parent (Full_Der, Parent (Derived_Type));
5828 Set_Full_View (Derived_Type, Full_Der);
5830 if not In_Open_Scopes (Par_Scope) then
5831 Install_Private_Declarations (Par_Scope);
5832 Install_Visible_Declarations (Par_Scope);
5834 Uninstall_Declarations (Par_Scope);
5836 -- If parent scope is open and in another unit, and parent has a
5837 -- completion, then the derivation is taking place in the visible
5838 -- part of a child unit. In that case retrieve the full view of
5839 -- the parent momentarily.
5841 elsif not In_Same_Source_Unit (N, Parent_Type) then
5842 Full_P := Full_View (Parent_Type);
5843 Exchange_Declarations (Parent_Type);
5845 Exchange_Declarations (Full_P);
5847 -- Otherwise it is a local derivation
5853 Set_Scope (Full_Der, Current_Scope);
5854 Set_Is_First_Subtype (Full_Der,
5855 Is_First_Subtype (Derived_Type));
5856 Set_Has_Size_Clause (Full_Der, False);
5857 Set_Has_Alignment_Clause (Full_Der, False);
5858 Set_Next_Entity (Full_Der, Empty);
5859 Set_Has_Delayed_Freeze (Full_Der);
5860 Set_Is_Frozen (Full_Der, False);
5861 Set_Freeze_Node (Full_Der, Empty);
5862 Set_Depends_On_Private (Full_Der,
5863 Has_Private_Component (Full_Der));
5864 Set_Public_Status (Full_Der);
5868 Set_Has_Unknown_Discriminants (Derived_Type,
5869 Has_Unknown_Discriminants (Parent_Type));
5871 if Is_Private_Type (Derived_Type) then
5872 Set_Private_Dependents (Derived_Type, New_Elmt_List);
5875 if Is_Private_Type (Parent_Type)
5876 and then Base_Type (Parent_Type) = Parent_Type
5877 and then In_Open_Scopes (Scope (Parent_Type))
5879 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
5881 if Is_Child_Unit (Scope (Current_Scope))
5882 and then Is_Completion
5883 and then In_Private_Part (Current_Scope)
5884 and then Scope (Parent_Type) /= Current_Scope
5886 -- This is the unusual case where a type completed by a private
5887 -- derivation occurs within a package nested in a child unit, and
5888 -- the parent is declared in an ancestor. In this case, the full
5889 -- view of the parent type will become visible in the body of
5890 -- the enclosing child, and only then will the current type be
5891 -- possibly non-private. We build a underlying full view that
5892 -- will be installed when the enclosing child body is compiled.
5895 Make_Defining_Identifier (Sloc (Derived_Type),
5896 Chars => Chars (Derived_Type));
5897 Set_Is_Itype (Full_Der);
5898 Build_Itype_Reference (Full_Der, N);
5900 -- The full view will be used to swap entities on entry/exit to
5901 -- the body, and must appear in the entity list for the package.
5903 Append_Entity (Full_Der, Scope (Derived_Type));
5904 Set_Has_Private_Declaration (Full_Der);
5905 Set_Has_Private_Declaration (Derived_Type);
5906 Set_Associated_Node_For_Itype (Full_Der, N);
5907 Set_Parent (Full_Der, Parent (Derived_Type));
5908 Full_P := Full_View (Parent_Type);
5909 Exchange_Declarations (Parent_Type);
5911 Exchange_Declarations (Full_P);
5912 Set_Underlying_Full_View (Derived_Type, Full_Der);
5915 end Build_Derived_Private_Type;
5917 -------------------------------
5918 -- Build_Derived_Record_Type --
5919 -------------------------------
5923 -- Ideally we would like to use the same model of type derivation for
5924 -- tagged and untagged record types. Unfortunately this is not quite
5925 -- possible because the semantics of representation clauses is different
5926 -- for tagged and untagged records under inheritance. Consider the
5929 -- type R (...) is [tagged] record ... end record;
5930 -- type T (...) is new R (...) [with ...];
5932 -- The representation clauses for T can specify a completely different
5933 -- record layout from R's. Hence the same component can be placed in two
5934 -- very different positions in objects of type T and R. If R and T are
5935 -- tagged types, representation clauses for T can only specify the layout
5936 -- of non inherited components, thus components that are common in R and T
5937 -- have the same position in objects of type R and T.
5939 -- This has two implications. The first is that the entire tree for R's
5940 -- declaration needs to be copied for T in the untagged case, so that T
5941 -- can be viewed as a record type of its own with its own representation
5942 -- clauses. The second implication is the way we handle discriminants.
5943 -- Specifically, in the untagged case we need a way to communicate to Gigi
5944 -- what are the real discriminants in the record, while for the semantics
5945 -- we need to consider those introduced by the user to rename the
5946 -- discriminants in the parent type. This is handled by introducing the
5947 -- notion of stored discriminants. See below for more.
5949 -- Fortunately the way regular components are inherited can be handled in
5950 -- the same way in tagged and untagged types.
5952 -- To complicate things a bit more the private view of a private extension
5953 -- cannot be handled in the same way as the full view (for one thing the
5954 -- semantic rules are somewhat different). We will explain what differs
5957 -- 2. DISCRIMINANTS UNDER INHERITANCE
5959 -- The semantic rules governing the discriminants of derived types are
5962 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
5963 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
5965 -- If parent type has discriminants, then the discriminants that are
5966 -- declared in the derived type are [3.4 (11)]:
5968 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
5971 -- o Otherwise, each discriminant of the parent type (implicitly declared
5972 -- in the same order with the same specifications). In this case, the
5973 -- discriminants are said to be "inherited", or if unknown in the parent
5974 -- are also unknown in the derived type.
5976 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
5978 -- o The parent subtype shall be constrained;
5980 -- o If the parent type is not a tagged type, then each discriminant of
5981 -- the derived type shall be used in the constraint defining a parent
5982 -- subtype. [Implementation note: This ensures that the new discriminant
5983 -- can share storage with an existing discriminant.]
5985 -- For the derived type each discriminant of the parent type is either
5986 -- inherited, constrained to equal some new discriminant of the derived
5987 -- type, or constrained to the value of an expression.
5989 -- When inherited or constrained to equal some new discriminant, the
5990 -- parent discriminant and the discriminant of the derived type are said
5993 -- If a discriminant of the parent type is constrained to a specific value
5994 -- in the derived type definition, then the discriminant is said to be
5995 -- "specified" by that derived type definition.
5997 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
5999 -- We have spoken about stored discriminants in point 1 (introduction)
6000 -- above. There are two sort of stored discriminants: implicit and
6001 -- explicit. As long as the derived type inherits the same discriminants as
6002 -- the root record type, stored discriminants are the same as regular
6003 -- discriminants, and are said to be implicit. However, if any discriminant
6004 -- in the root type was renamed in the derived type, then the derived
6005 -- type will contain explicit stored discriminants. Explicit stored
6006 -- discriminants are discriminants in addition to the semantically visible
6007 -- discriminants defined for the derived type. Stored discriminants are
6008 -- used by Gigi to figure out what are the physical discriminants in
6009 -- objects of the derived type (see precise definition in einfo.ads).
6010 -- As an example, consider the following:
6012 -- type R (D1, D2, D3 : Int) is record ... end record;
6013 -- type T1 is new R;
6014 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
6015 -- type T3 is new T2;
6016 -- type T4 (Y : Int) is new T3 (Y, 99);
6018 -- The following table summarizes the discriminants and stored
6019 -- discriminants in R and T1 through T4.
6021 -- Type Discrim Stored Discrim Comment
6022 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
6023 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
6024 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
6025 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
6026 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
6028 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
6029 -- find the corresponding discriminant in the parent type, while
6030 -- Original_Record_Component (abbreviated ORC below), the actual physical
6031 -- component that is renamed. Finally the field Is_Completely_Hidden
6032 -- (abbreviated ICH below) is set for all explicit stored discriminants
6033 -- (see einfo.ads for more info). For the above example this gives:
6035 -- Discrim CD ORC ICH
6036 -- ^^^^^^^ ^^ ^^^ ^^^
6037 -- D1 in R empty itself no
6038 -- D2 in R empty itself no
6039 -- D3 in R empty itself no
6041 -- D1 in T1 D1 in R itself no
6042 -- D2 in T1 D2 in R itself no
6043 -- D3 in T1 D3 in R itself no
6045 -- X1 in T2 D3 in T1 D3 in T2 no
6046 -- X2 in T2 D1 in T1 D1 in T2 no
6047 -- D1 in T2 empty itself yes
6048 -- D2 in T2 empty itself yes
6049 -- D3 in T2 empty itself yes
6051 -- X1 in T3 X1 in T2 D3 in T3 no
6052 -- X2 in T3 X2 in T2 D1 in T3 no
6053 -- D1 in T3 empty itself yes
6054 -- D2 in T3 empty itself yes
6055 -- D3 in T3 empty itself yes
6057 -- Y in T4 X1 in T3 D3 in T3 no
6058 -- D1 in T3 empty itself yes
6059 -- D2 in T3 empty itself yes
6060 -- D3 in T3 empty itself yes
6062 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
6064 -- Type derivation for tagged types is fairly straightforward. If no
6065 -- discriminants are specified by the derived type, these are inherited
6066 -- from the parent. No explicit stored discriminants are ever necessary.
6067 -- The only manipulation that is done to the tree is that of adding a
6068 -- _parent field with parent type and constrained to the same constraint
6069 -- specified for the parent in the derived type definition. For instance:
6071 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
6072 -- type T1 is new R with null record;
6073 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
6075 -- are changed into:
6077 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
6078 -- _parent : R (D1, D2, D3);
6081 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
6082 -- _parent : T1 (X2, 88, X1);
6085 -- The discriminants actually present in R, T1 and T2 as well as their CD,
6086 -- ORC and ICH fields are:
6088 -- Discrim CD ORC ICH
6089 -- ^^^^^^^ ^^ ^^^ ^^^
6090 -- D1 in R empty itself no
6091 -- D2 in R empty itself no
6092 -- D3 in R empty itself no
6094 -- D1 in T1 D1 in R D1 in R no
6095 -- D2 in T1 D2 in R D2 in R no
6096 -- D3 in T1 D3 in R D3 in R no
6098 -- X1 in T2 D3 in T1 D3 in R no
6099 -- X2 in T2 D1 in T1 D1 in R no
6101 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
6103 -- Regardless of whether we dealing with a tagged or untagged type
6104 -- we will transform all derived type declarations of the form
6106 -- type T is new R (...) [with ...];
6108 -- subtype S is R (...);
6109 -- type T is new S [with ...];
6111 -- type BT is new R [with ...];
6112 -- subtype T is BT (...);
6114 -- That is, the base derived type is constrained only if it has no
6115 -- discriminants. The reason for doing this is that GNAT's semantic model
6116 -- assumes that a base type with discriminants is unconstrained.
6118 -- Note that, strictly speaking, the above transformation is not always
6119 -- correct. Consider for instance the following excerpt from ACVC b34011a:
6121 -- procedure B34011A is
6122 -- type REC (D : integer := 0) is record
6127 -- type T6 is new Rec;
6128 -- function F return T6;
6133 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
6136 -- The definition of Q6.U is illegal. However transforming Q6.U into
6138 -- type BaseU is new T6;
6139 -- subtype U is BaseU (Q6.F.I)
6141 -- turns U into a legal subtype, which is incorrect. To avoid this problem
6142 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
6143 -- the transformation described above.
6145 -- There is another instance where the above transformation is incorrect.
6149 -- type Base (D : Integer) is tagged null record;
6150 -- procedure P (X : Base);
6152 -- type Der is new Base (2) with null record;
6153 -- procedure P (X : Der);
6156 -- Then the above transformation turns this into
6158 -- type Der_Base is new Base with null record;
6159 -- -- procedure P (X : Base) is implicitly inherited here
6160 -- -- as procedure P (X : Der_Base).
6162 -- subtype Der is Der_Base (2);
6163 -- procedure P (X : Der);
6164 -- -- The overriding of P (X : Der_Base) is illegal since we
6165 -- -- have a parameter conformance problem.
6167 -- To get around this problem, after having semantically processed Der_Base
6168 -- and the rewritten subtype declaration for Der, we copy Der_Base field
6169 -- Discriminant_Constraint from Der so that when parameter conformance is
6170 -- checked when P is overridden, no semantic errors are flagged.
6172 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
6174 -- Regardless of whether we are dealing with a tagged or untagged type
6175 -- we will transform all derived type declarations of the form
6177 -- type R (D1, .., Dn : ...) is [tagged] record ...;
6178 -- type T is new R [with ...];
6180 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
6182 -- The reason for such transformation is that it allows us to implement a
6183 -- very clean form of component inheritance as explained below.
6185 -- Note that this transformation is not achieved by direct tree rewriting
6186 -- and manipulation, but rather by redoing the semantic actions that the
6187 -- above transformation will entail. This is done directly in routine
6188 -- Inherit_Components.
6190 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
6192 -- In both tagged and untagged derived types, regular non discriminant
6193 -- components are inherited in the derived type from the parent type. In
6194 -- the absence of discriminants component, inheritance is straightforward
6195 -- as components can simply be copied from the parent.
6197 -- If the parent has discriminants, inheriting components constrained with
6198 -- these discriminants requires caution. Consider the following example:
6200 -- type R (D1, D2 : Positive) is [tagged] record
6201 -- S : String (D1 .. D2);
6204 -- type T1 is new R [with null record];
6205 -- type T2 (X : positive) is new R (1, X) [with null record];
6207 -- As explained in 6. above, T1 is rewritten as
6208 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
6209 -- which makes the treatment for T1 and T2 identical.
6211 -- What we want when inheriting S, is that references to D1 and D2 in R are
6212 -- replaced with references to their correct constraints, i.e. D1 and D2 in
6213 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
6214 -- with either discriminant references in the derived type or expressions.
6215 -- This replacement is achieved as follows: before inheriting R's
6216 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
6217 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
6218 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
6219 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
6220 -- by String (1 .. X).
6222 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
6224 -- We explain here the rules governing private type extensions relevant to
6225 -- type derivation. These rules are explained on the following example:
6227 -- type D [(...)] is new A [(...)] with private; <-- partial view
6228 -- type D [(...)] is new P [(...)] with null record; <-- full view
6230 -- Type A is called the ancestor subtype of the private extension.
6231 -- Type P is the parent type of the full view of the private extension. It
6232 -- must be A or a type derived from A.
6234 -- The rules concerning the discriminants of private type extensions are
6237 -- o If a private extension inherits known discriminants from the ancestor
6238 -- subtype, then the full view shall also inherit its discriminants from
6239 -- the ancestor subtype and the parent subtype of the full view shall be
6240 -- constrained if and only if the ancestor subtype is constrained.
6242 -- o If a partial view has unknown discriminants, then the full view may
6243 -- define a definite or an indefinite subtype, with or without
6246 -- o If a partial view has neither known nor unknown discriminants, then
6247 -- the full view shall define a definite subtype.
6249 -- o If the ancestor subtype of a private extension has constrained
6250 -- discriminants, then the parent subtype of the full view shall impose a
6251 -- statically matching constraint on those discriminants.
6253 -- This means that only the following forms of private extensions are
6256 -- type D is new A with private; <-- partial view
6257 -- type D is new P with null record; <-- full view
6259 -- If A has no discriminants than P has no discriminants, otherwise P must
6260 -- inherit A's discriminants.
6262 -- type D is new A (...) with private; <-- partial view
6263 -- type D is new P (:::) with null record; <-- full view
6265 -- P must inherit A's discriminants and (...) and (:::) must statically
6268 -- subtype A is R (...);
6269 -- type D is new A with private; <-- partial view
6270 -- type D is new P with null record; <-- full view
6272 -- P must have inherited R's discriminants and must be derived from A or
6273 -- any of its subtypes.
6275 -- type D (..) is new A with private; <-- partial view
6276 -- type D (..) is new P [(:::)] with null record; <-- full view
6278 -- No specific constraints on P's discriminants or constraint (:::).
6279 -- Note that A can be unconstrained, but the parent subtype P must either
6280 -- be constrained or (:::) must be present.
6282 -- type D (..) is new A [(...)] with private; <-- partial view
6283 -- type D (..) is new P [(:::)] with null record; <-- full view
6285 -- P's constraints on A's discriminants must statically match those
6286 -- imposed by (...).
6288 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6290 -- The full view of a private extension is handled exactly as described
6291 -- above. The model chose for the private view of a private extension is
6292 -- the same for what concerns discriminants (i.e. they receive the same
6293 -- treatment as in the tagged case). However, the private view of the
6294 -- private extension always inherits the components of the parent base,
6295 -- without replacing any discriminant reference. Strictly speaking this is
6296 -- incorrect. However, Gigi never uses this view to generate code so this
6297 -- is a purely semantic issue. In theory, a set of transformations similar
6298 -- to those given in 5. and 6. above could be applied to private views of
6299 -- private extensions to have the same model of component inheritance as
6300 -- for non private extensions. However, this is not done because it would
6301 -- further complicate private type processing. Semantically speaking, this
6302 -- leaves us in an uncomfortable situation. As an example consider:
6305 -- type R (D : integer) is tagged record
6306 -- S : String (1 .. D);
6308 -- procedure P (X : R);
6309 -- type T is new R (1) with private;
6311 -- type T is new R (1) with null record;
6314 -- This is transformed into:
6317 -- type R (D : integer) is tagged record
6318 -- S : String (1 .. D);
6320 -- procedure P (X : R);
6321 -- type T is new R (1) with private;
6323 -- type BaseT is new R with null record;
6324 -- subtype T is BaseT (1);
6327 -- (strictly speaking the above is incorrect Ada)
6329 -- From the semantic standpoint the private view of private extension T
6330 -- should be flagged as constrained since one can clearly have
6334 -- in a unit withing Pack. However, when deriving subprograms for the
6335 -- private view of private extension T, T must be seen as unconstrained
6336 -- since T has discriminants (this is a constraint of the current
6337 -- subprogram derivation model). Thus, when processing the private view of
6338 -- a private extension such as T, we first mark T as unconstrained, we
6339 -- process it, we perform program derivation and just before returning from
6340 -- Build_Derived_Record_Type we mark T as constrained.
6342 -- ??? Are there are other uncomfortable cases that we will have to
6345 -- 10. RECORD_TYPE_WITH_PRIVATE complications
6347 -- Types that are derived from a visible record type and have a private
6348 -- extension present other peculiarities. They behave mostly like private
6349 -- types, but if they have primitive operations defined, these will not
6350 -- have the proper signatures for further inheritance, because other
6351 -- primitive operations will use the implicit base that we define for
6352 -- private derivations below. This affect subprogram inheritance (see
6353 -- Derive_Subprograms for details). We also derive the implicit base from
6354 -- the base type of the full view, so that the implicit base is a record
6355 -- type and not another private type, This avoids infinite loops.
6357 procedure Build_Derived_Record_Type
6359 Parent_Type : Entity_Id;
6360 Derived_Type : Entity_Id;
6361 Derive_Subps : Boolean := True)
6363 Loc : constant Source_Ptr := Sloc (N);
6364 Parent_Base : Entity_Id;
6367 Discrim : Entity_Id;
6368 Last_Discrim : Entity_Id;
6371 Discs : Elist_Id := New_Elmt_List;
6372 -- An empty Discs list means that there were no constraints in the
6373 -- subtype indication or that there was an error processing it.
6375 Assoc_List : Elist_Id;
6376 New_Discrs : Elist_Id;
6377 New_Base : Entity_Id;
6379 New_Indic : Node_Id;
6381 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
6382 Discriminant_Specs : constant Boolean :=
6383 Present (Discriminant_Specifications (N));
6384 Private_Extension : constant Boolean :=
6385 Nkind (N) = N_Private_Extension_Declaration;
6387 Constraint_Present : Boolean;
6388 Inherit_Discrims : Boolean := False;
6389 Save_Etype : Entity_Id;
6390 Save_Discr_Constr : Elist_Id;
6391 Save_Next_Entity : Entity_Id;
6394 if Ekind (Parent_Type) = E_Record_Type_With_Private
6395 and then Present (Full_View (Parent_Type))
6396 and then Has_Discriminants (Parent_Type)
6398 Parent_Base := Base_Type (Full_View (Parent_Type));
6400 Parent_Base := Base_Type (Parent_Type);
6403 -- Before we start the previously documented transformations, here is
6404 -- little fix for size and alignment of tagged types. Normally when we
6405 -- derive type D from type P, we copy the size and alignment of P as the
6406 -- default for D, and in the absence of explicit representation clauses
6407 -- for D, the size and alignment are indeed the same as the parent.
6409 -- But this is wrong for tagged types, since fields may be added, and
6410 -- the default size may need to be larger, and the default alignment may
6411 -- need to be larger.
6413 -- We therefore reset the size and alignment fields in the tagged case.
6414 -- Note that the size and alignment will in any case be at least as
6415 -- large as the parent type (since the derived type has a copy of the
6416 -- parent type in the _parent field)
6418 -- The type is also marked as being tagged here, which is needed when
6419 -- processing components with a self-referential anonymous access type
6420 -- in the call to Check_Anonymous_Access_Components below. Note that
6421 -- this flag is also set later on for completeness.
6424 Set_Is_Tagged_Type (Derived_Type);
6425 Init_Size_Align (Derived_Type);
6428 -- STEP 0a: figure out what kind of derived type declaration we have
6430 if Private_Extension then
6432 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
6435 Type_Def := Type_Definition (N);
6437 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
6438 -- Parent_Base can be a private type or private extension. However,
6439 -- for tagged types with an extension the newly added fields are
6440 -- visible and hence the Derived_Type is always an E_Record_Type.
6441 -- (except that the parent may have its own private fields).
6442 -- For untagged types we preserve the Ekind of the Parent_Base.
6444 if Present (Record_Extension_Part (Type_Def)) then
6445 Set_Ekind (Derived_Type, E_Record_Type);
6447 -- Create internal access types for components with anonymous
6450 if Ada_Version >= Ada_05 then
6451 Check_Anonymous_Access_Components
6452 (N, Derived_Type, Derived_Type,
6453 Component_List (Record_Extension_Part (Type_Def)));
6457 Set_Ekind (Derived_Type, Ekind (Parent_Base));
6461 -- Indic can either be an N_Identifier if the subtype indication
6462 -- contains no constraint or an N_Subtype_Indication if the subtype
6463 -- indication has a constraint.
6465 Indic := Subtype_Indication (Type_Def);
6466 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
6468 -- Check that the type has visible discriminants. The type may be
6469 -- a private type with unknown discriminants whose full view has
6470 -- discriminants which are invisible.
6472 if Constraint_Present then
6473 if not Has_Discriminants (Parent_Base)
6475 (Has_Unknown_Discriminants (Parent_Base)
6476 and then Is_Private_Type (Parent_Base))
6479 ("invalid constraint: type has no discriminant",
6480 Constraint (Indic));
6482 Constraint_Present := False;
6483 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6485 elsif Is_Constrained (Parent_Type) then
6487 ("invalid constraint: parent type is already constrained",
6488 Constraint (Indic));
6490 Constraint_Present := False;
6491 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
6495 -- STEP 0b: If needed, apply transformation given in point 5. above
6497 if not Private_Extension
6498 and then Has_Discriminants (Parent_Type)
6499 and then not Discriminant_Specs
6500 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
6502 -- First, we must analyze the constraint (see comment in point 5.)
6504 if Constraint_Present then
6505 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
6507 if Has_Discriminants (Derived_Type)
6508 and then Has_Private_Declaration (Derived_Type)
6509 and then Present (Discriminant_Constraint (Derived_Type))
6511 -- Verify that constraints of the full view statically match
6512 -- those given in the partial view.
6518 C1 := First_Elmt (New_Discrs);
6519 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
6520 while Present (C1) and then Present (C2) loop
6521 if Fully_Conformant_Expressions (Node (C1), Node (C2))
6523 (Is_OK_Static_Expression (Node (C1))
6525 Is_OK_Static_Expression (Node (C2))
6527 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
6533 "constraint not conformant to previous declaration",
6544 -- Insert and analyze the declaration for the unconstrained base type
6546 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6549 Make_Full_Type_Declaration (Loc,
6550 Defining_Identifier => New_Base,
6552 Make_Derived_Type_Definition (Loc,
6553 Abstract_Present => Abstract_Present (Type_Def),
6554 Limited_Present => Limited_Present (Type_Def),
6555 Subtype_Indication =>
6556 New_Occurrence_Of (Parent_Base, Loc),
6557 Record_Extension_Part =>
6558 Relocate_Node (Record_Extension_Part (Type_Def)),
6559 Interface_List => Interface_List (Type_Def)));
6561 Set_Parent (New_Decl, Parent (N));
6562 Mark_Rewrite_Insertion (New_Decl);
6563 Insert_Before (N, New_Decl);
6565 -- Note that this call passes False for the Derive_Subps parameter
6566 -- because subprogram derivation is deferred until after creating
6567 -- the subtype (see below).
6570 (New_Decl, Parent_Base, New_Base,
6571 Is_Completion => True, Derive_Subps => False);
6573 -- ??? This needs re-examination to determine whether the
6574 -- above call can simply be replaced by a call to Analyze.
6576 Set_Analyzed (New_Decl);
6578 -- Insert and analyze the declaration for the constrained subtype
6580 if Constraint_Present then
6582 Make_Subtype_Indication (Loc,
6583 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6584 Constraint => Relocate_Node (Constraint (Indic)));
6588 Constr_List : constant List_Id := New_List;
6593 C := First_Elmt (Discriminant_Constraint (Parent_Type));
6594 while Present (C) loop
6597 -- It is safe here to call New_Copy_Tree since
6598 -- Force_Evaluation was called on each constraint in
6599 -- Build_Discriminant_Constraints.
6601 Append (New_Copy_Tree (Expr), To => Constr_List);
6607 Make_Subtype_Indication (Loc,
6608 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6610 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
6615 Make_Subtype_Declaration (Loc,
6616 Defining_Identifier => Derived_Type,
6617 Subtype_Indication => New_Indic));
6621 -- Derivation of subprograms must be delayed until the full subtype
6622 -- has been established to ensure proper overriding of subprograms
6623 -- inherited by full types. If the derivations occurred as part of
6624 -- the call to Build_Derived_Type above, then the check for type
6625 -- conformance would fail because earlier primitive subprograms
6626 -- could still refer to the full type prior the change to the new
6627 -- subtype and hence would not match the new base type created here.
6629 Derive_Subprograms (Parent_Type, Derived_Type);
6631 -- For tagged types the Discriminant_Constraint of the new base itype
6632 -- is inherited from the first subtype so that no subtype conformance
6633 -- problem arise when the first subtype overrides primitive
6634 -- operations inherited by the implicit base type.
6637 Set_Discriminant_Constraint
6638 (New_Base, Discriminant_Constraint (Derived_Type));
6644 -- If we get here Derived_Type will have no discriminants or it will be
6645 -- a discriminated unconstrained base type.
6647 -- STEP 1a: perform preliminary actions/checks for derived tagged types
6651 -- The parent type is frozen for non-private extensions (RM 13.14(7))
6652 -- The declaration of a specific descendant of an interface type
6653 -- freezes the interface type (RM 13.14).
6655 if not Private_Extension
6656 or else Is_Interface (Parent_Base)
6658 Freeze_Before (N, Parent_Type);
6661 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
6662 -- cannot be declared at a deeper level than its parent type is
6663 -- removed. The check on derivation within a generic body is also
6664 -- relaxed, but there's a restriction that a derived tagged type
6665 -- cannot be declared in a generic body if it's derived directly
6666 -- or indirectly from a formal type of that generic.
6668 if Ada_Version >= Ada_05 then
6669 if Present (Enclosing_Generic_Body (Derived_Type)) then
6671 Ancestor_Type : Entity_Id;
6674 -- Check to see if any ancestor of the derived type is a
6677 Ancestor_Type := Parent_Type;
6678 while not Is_Generic_Type (Ancestor_Type)
6679 and then Etype (Ancestor_Type) /= Ancestor_Type
6681 Ancestor_Type := Etype (Ancestor_Type);
6684 -- If the derived type does have a formal type as an
6685 -- ancestor, then it's an error if the derived type is
6686 -- declared within the body of the generic unit that
6687 -- declares the formal type in its generic formal part. It's
6688 -- sufficient to check whether the ancestor type is declared
6689 -- inside the same generic body as the derived type (such as
6690 -- within a nested generic spec), in which case the
6691 -- derivation is legal. If the formal type is declared
6692 -- outside of that generic body, then it's guaranteed that
6693 -- the derived type is declared within the generic body of
6694 -- the generic unit declaring the formal type.
6696 if Is_Generic_Type (Ancestor_Type)
6697 and then Enclosing_Generic_Body (Ancestor_Type) /=
6698 Enclosing_Generic_Body (Derived_Type)
6701 ("parent type of& must not be descendant of formal type"
6702 & " of an enclosing generic body",
6703 Indic, Derived_Type);
6708 elsif Type_Access_Level (Derived_Type) /=
6709 Type_Access_Level (Parent_Type)
6710 and then not Is_Generic_Type (Derived_Type)
6712 if Is_Controlled (Parent_Type) then
6714 ("controlled type must be declared at the library level",
6718 ("type extension at deeper accessibility level than parent",
6724 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
6728 and then GB /= Enclosing_Generic_Body (Parent_Base)
6731 ("parent type of& must not be outside generic body"
6733 Indic, Derived_Type);
6739 -- Ada 2005 (AI-251)
6741 if Ada_Version = Ada_05
6744 -- "The declaration of a specific descendant of an interface type
6745 -- freezes the interface type" (RM 13.14).
6750 if Is_Non_Empty_List (Interface_List (Type_Def)) then
6751 Iface := First (Interface_List (Type_Def));
6752 while Present (Iface) loop
6753 Freeze_Before (N, Etype (Iface));
6760 -- STEP 1b : preliminary cleanup of the full view of private types
6762 -- If the type is already marked as having discriminants, then it's the
6763 -- completion of a private type or private extension and we need to
6764 -- retain the discriminants from the partial view if the current
6765 -- declaration has Discriminant_Specifications so that we can verify
6766 -- conformance. However, we must remove any existing components that
6767 -- were inherited from the parent (and attached in Copy_And_Swap)
6768 -- because the full type inherits all appropriate components anyway, and
6769 -- we do not want the partial view's components interfering.
6771 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
6772 Discrim := First_Discriminant (Derived_Type);
6774 Last_Discrim := Discrim;
6775 Next_Discriminant (Discrim);
6776 exit when No (Discrim);
6779 Set_Last_Entity (Derived_Type, Last_Discrim);
6781 -- In all other cases wipe out the list of inherited components (even
6782 -- inherited discriminants), it will be properly rebuilt here.
6785 Set_First_Entity (Derived_Type, Empty);
6786 Set_Last_Entity (Derived_Type, Empty);
6789 -- STEP 1c: Initialize some flags for the Derived_Type
6791 -- The following flags must be initialized here so that
6792 -- Process_Discriminants can check that discriminants of tagged types do
6793 -- not have a default initial value and that access discriminants are
6794 -- only specified for limited records. For completeness, these flags are
6795 -- also initialized along with all the other flags below.
6797 -- AI-419: Limitedness is not inherited from an interface parent, so to
6798 -- be limited in that case the type must be explicitly declared as
6799 -- limited. However, task and protected interfaces are always limited.
6801 if Limited_Present (Type_Def) then
6802 Set_Is_Limited_Record (Derived_Type);
6804 elsif Is_Limited_Record (Parent_Type)
6805 or else (Present (Full_View (Parent_Type))
6806 and then Is_Limited_Record (Full_View (Parent_Type)))
6808 if not Is_Interface (Parent_Type)
6809 or else Is_Synchronized_Interface (Parent_Type)
6810 or else Is_Protected_Interface (Parent_Type)
6811 or else Is_Task_Interface (Parent_Type)
6813 Set_Is_Limited_Record (Derived_Type);
6817 -- STEP 2a: process discriminants of derived type if any
6819 Push_Scope (Derived_Type);
6821 if Discriminant_Specs then
6822 Set_Has_Unknown_Discriminants (Derived_Type, False);
6824 -- The following call initializes fields Has_Discriminants and
6825 -- Discriminant_Constraint, unless we are processing the completion
6826 -- of a private type declaration.
6828 Check_Or_Process_Discriminants (N, Derived_Type);
6830 -- For non-tagged types the constraint on the Parent_Type must be
6831 -- present and is used to rename the discriminants.
6833 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
6834 Error_Msg_N ("untagged parent must have discriminants", Indic);
6836 elsif not Is_Tagged and then not Constraint_Present then
6838 ("discriminant constraint needed for derived untagged records",
6841 -- Otherwise the parent subtype must be constrained unless we have a
6842 -- private extension.
6844 elsif not Constraint_Present
6845 and then not Private_Extension
6846 and then not Is_Constrained (Parent_Type)
6849 ("unconstrained type not allowed in this context", Indic);
6851 elsif Constraint_Present then
6852 -- The following call sets the field Corresponding_Discriminant
6853 -- for the discriminants in the Derived_Type.
6855 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
6857 -- For untagged types all new discriminants must rename
6858 -- discriminants in the parent. For private extensions new
6859 -- discriminants cannot rename old ones (implied by [7.3(13)]).
6861 Discrim := First_Discriminant (Derived_Type);
6862 while Present (Discrim) loop
6864 and then No (Corresponding_Discriminant (Discrim))
6867 ("new discriminants must constrain old ones", Discrim);
6869 elsif Private_Extension
6870 and then Present (Corresponding_Discriminant (Discrim))
6873 ("only static constraints allowed for parent"
6874 & " discriminants in the partial view", Indic);
6878 -- If a new discriminant is used in the constraint, then its
6879 -- subtype must be statically compatible with the parent
6880 -- discriminant's subtype (3.7(15)).
6882 if Present (Corresponding_Discriminant (Discrim))
6884 not Subtypes_Statically_Compatible
6886 Etype (Corresponding_Discriminant (Discrim)))
6889 ("subtype must be compatible with parent discriminant",
6893 Next_Discriminant (Discrim);
6896 -- Check whether the constraints of the full view statically
6897 -- match those imposed by the parent subtype [7.3(13)].
6899 if Present (Stored_Constraint (Derived_Type)) then
6904 C1 := First_Elmt (Discs);
6905 C2 := First_Elmt (Stored_Constraint (Derived_Type));
6906 while Present (C1) and then Present (C2) loop
6908 Fully_Conformant_Expressions (Node (C1), Node (C2))
6911 ("not conformant with previous declaration",
6922 -- STEP 2b: No new discriminants, inherit discriminants if any
6925 if Private_Extension then
6926 Set_Has_Unknown_Discriminants
6928 Has_Unknown_Discriminants (Parent_Type)
6929 or else Unknown_Discriminants_Present (N));
6931 -- The partial view of the parent may have unknown discriminants,
6932 -- but if the full view has discriminants and the parent type is
6933 -- in scope they must be inherited.
6935 elsif Has_Unknown_Discriminants (Parent_Type)
6937 (not Has_Discriminants (Parent_Type)
6938 or else not In_Open_Scopes (Scope (Parent_Type)))
6940 Set_Has_Unknown_Discriminants (Derived_Type);
6943 if not Has_Unknown_Discriminants (Derived_Type)
6944 and then not Has_Unknown_Discriminants (Parent_Base)
6945 and then Has_Discriminants (Parent_Type)
6947 Inherit_Discrims := True;
6948 Set_Has_Discriminants
6949 (Derived_Type, True);
6950 Set_Discriminant_Constraint
6951 (Derived_Type, Discriminant_Constraint (Parent_Base));
6954 -- The following test is true for private types (remember
6955 -- transformation 5. is not applied to those) and in an error
6958 if Constraint_Present then
6959 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
6962 -- For now mark a new derived type as constrained only if it has no
6963 -- discriminants. At the end of Build_Derived_Record_Type we properly
6964 -- set this flag in the case of private extensions. See comments in
6965 -- point 9. just before body of Build_Derived_Record_Type.
6969 not (Inherit_Discrims
6970 or else Has_Unknown_Discriminants (Derived_Type)));
6973 -- STEP 3: initialize fields of derived type
6975 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
6976 Set_Stored_Constraint (Derived_Type, No_Elist);
6978 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
6979 -- but cannot be interfaces
6981 if not Private_Extension
6982 and then Ekind (Derived_Type) /= E_Private_Type
6983 and then Ekind (Derived_Type) /= E_Limited_Private_Type
6985 if Interface_Present (Type_Def) then
6986 Analyze_Interface_Declaration (Derived_Type, Type_Def);
6989 Set_Interfaces (Derived_Type, No_Elist);
6992 -- Fields inherited from the Parent_Type
6995 (Derived_Type, Einfo.Discard_Names (Parent_Type));
6996 Set_Has_Specified_Layout
6997 (Derived_Type, Has_Specified_Layout (Parent_Type));
6998 Set_Is_Limited_Composite
6999 (Derived_Type, Is_Limited_Composite (Parent_Type));
7000 Set_Is_Private_Composite
7001 (Derived_Type, Is_Private_Composite (Parent_Type));
7003 -- Fields inherited from the Parent_Base
7005 Set_Has_Controlled_Component
7006 (Derived_Type, Has_Controlled_Component (Parent_Base));
7007 Set_Has_Non_Standard_Rep
7008 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7009 Set_Has_Primitive_Operations
7010 (Derived_Type, Has_Primitive_Operations (Parent_Base));
7012 -- Fields inherited from the Parent_Base in the non-private case
7014 if Ekind (Derived_Type) = E_Record_Type then
7015 Set_Has_Complex_Representation
7016 (Derived_Type, Has_Complex_Representation (Parent_Base));
7019 -- Fields inherited from the Parent_Base for record types
7021 if Is_Record_Type (Derived_Type) then
7022 Set_OK_To_Reorder_Components
7023 (Derived_Type, OK_To_Reorder_Components (Parent_Base));
7024 Set_Reverse_Bit_Order
7025 (Derived_Type, Reverse_Bit_Order (Parent_Base));
7028 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7030 if not Is_Controlled (Parent_Type) then
7031 Set_Finalize_Storage_Only
7032 (Derived_Type, Finalize_Storage_Only (Parent_Type));
7035 -- Set fields for private derived types
7037 if Is_Private_Type (Derived_Type) then
7038 Set_Depends_On_Private (Derived_Type, True);
7039 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7041 -- Inherit fields from non private record types. If this is the
7042 -- completion of a derivation from a private type, the parent itself
7043 -- is private, and the attributes come from its full view, which must
7047 if Is_Private_Type (Parent_Base)
7048 and then not Is_Record_Type (Parent_Base)
7050 Set_Component_Alignment
7051 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
7053 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
7055 Set_Component_Alignment
7056 (Derived_Type, Component_Alignment (Parent_Base));
7059 (Derived_Type, C_Pass_By_Copy (Parent_Base));
7063 -- Set fields for tagged types
7066 Set_Primitive_Operations (Derived_Type, New_Elmt_List);
7068 -- All tagged types defined in Ada.Finalization are controlled
7070 if Chars (Scope (Derived_Type)) = Name_Finalization
7071 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
7072 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
7074 Set_Is_Controlled (Derived_Type);
7076 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
7079 -- Minor optimization: there is no need to generate the class-wide
7080 -- entity associated with an underlying record view.
7082 if not Is_Underlying_Record_View (Derived_Type) then
7083 Make_Class_Wide_Type (Derived_Type);
7086 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
7088 if Has_Discriminants (Derived_Type)
7089 and then Constraint_Present
7091 Set_Stored_Constraint
7092 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
7095 if Ada_Version >= Ada_05 then
7097 Ifaces_List : Elist_Id;
7100 -- Checks rules 3.9.4 (13/2 and 14/2)
7102 if Comes_From_Source (Derived_Type)
7103 and then not Is_Private_Type (Derived_Type)
7104 and then Is_Interface (Parent_Type)
7105 and then not Is_Interface (Derived_Type)
7107 if Is_Task_Interface (Parent_Type) then
7109 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
7112 elsif Is_Protected_Interface (Parent_Type) then
7114 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
7119 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
7121 Check_Interfaces (N, Type_Def);
7123 -- Ada 2005 (AI-251): Collect the list of progenitors that are
7124 -- not already in the parents.
7128 Ifaces_List => Ifaces_List,
7129 Exclude_Parents => True);
7131 Set_Interfaces (Derived_Type, Ifaces_List);
7136 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
7137 Set_Has_Non_Standard_Rep
7138 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7141 -- STEP 4: Inherit components from the parent base and constrain them.
7142 -- Apply the second transformation described in point 6. above.
7144 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
7145 or else not Has_Discriminants (Parent_Type)
7146 or else not Is_Constrained (Parent_Type)
7150 Constrs := Discriminant_Constraint (Parent_Type);
7155 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
7157 -- STEP 5a: Copy the parent record declaration for untagged types
7159 if not Is_Tagged then
7161 -- Discriminant_Constraint (Derived_Type) has been properly
7162 -- constructed. Save it and temporarily set it to Empty because we
7163 -- do not want the call to New_Copy_Tree below to mess this list.
7165 if Has_Discriminants (Derived_Type) then
7166 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
7167 Set_Discriminant_Constraint (Derived_Type, No_Elist);
7169 Save_Discr_Constr := No_Elist;
7172 -- Save the Etype field of Derived_Type. It is correctly set now,
7173 -- but the call to New_Copy tree may remap it to point to itself,
7174 -- which is not what we want. Ditto for the Next_Entity field.
7176 Save_Etype := Etype (Derived_Type);
7177 Save_Next_Entity := Next_Entity (Derived_Type);
7179 -- Assoc_List maps all stored discriminants in the Parent_Base to
7180 -- stored discriminants in the Derived_Type. It is fundamental that
7181 -- no types or itypes with discriminants other than the stored
7182 -- discriminants appear in the entities declared inside
7183 -- Derived_Type, since the back end cannot deal with it.
7187 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
7189 -- Restore the fields saved prior to the New_Copy_Tree call
7190 -- and compute the stored constraint.
7192 Set_Etype (Derived_Type, Save_Etype);
7193 Set_Next_Entity (Derived_Type, Save_Next_Entity);
7195 if Has_Discriminants (Derived_Type) then
7196 Set_Discriminant_Constraint
7197 (Derived_Type, Save_Discr_Constr);
7198 Set_Stored_Constraint
7199 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
7200 Replace_Components (Derived_Type, New_Decl);
7203 -- Insert the new derived type declaration
7205 Rewrite (N, New_Decl);
7207 -- STEP 5b: Complete the processing for record extensions in generics
7209 -- There is no completion for record extensions declared in the
7210 -- parameter part of a generic, so we need to complete processing for
7211 -- these generic record extensions here. The Record_Type_Definition call
7212 -- will change the Ekind of the components from E_Void to E_Component.
7214 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
7215 Record_Type_Definition (Empty, Derived_Type);
7217 -- STEP 5c: Process the record extension for non private tagged types
7219 elsif not Private_Extension then
7221 -- Add the _parent field in the derived type
7223 Expand_Record_Extension (Derived_Type, Type_Def);
7225 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
7226 -- implemented interfaces if we are in expansion mode
7229 and then Has_Interfaces (Derived_Type)
7231 Add_Interface_Tag_Components (N, Derived_Type);
7234 -- Analyze the record extension
7236 Record_Type_Definition
7237 (Record_Extension_Part (Type_Def), Derived_Type);
7242 -- Nothing else to do if there is an error in the derivation.
7243 -- An unusual case: the full view may be derived from a type in an
7244 -- instance, when the partial view was used illegally as an actual
7245 -- in that instance, leading to a circular definition.
7247 if Etype (Derived_Type) = Any_Type
7248 or else Etype (Parent_Type) = Derived_Type
7253 -- Set delayed freeze and then derive subprograms, we need to do
7254 -- this in this order so that derived subprograms inherit the
7255 -- derived freeze if necessary.
7257 Set_Has_Delayed_Freeze (Derived_Type);
7259 if Derive_Subps then
7260 Derive_Subprograms (Parent_Type, Derived_Type);
7263 -- If we have a private extension which defines a constrained derived
7264 -- type mark as constrained here after we have derived subprograms. See
7265 -- comment on point 9. just above the body of Build_Derived_Record_Type.
7267 if Private_Extension and then Inherit_Discrims then
7268 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7269 Set_Is_Constrained (Derived_Type, True);
7270 Set_Discriminant_Constraint (Derived_Type, Discs);
7272 elsif Is_Constrained (Parent_Type) then
7274 (Derived_Type, True);
7275 Set_Discriminant_Constraint
7276 (Derived_Type, Discriminant_Constraint (Parent_Type));
7280 -- Update the class-wide type, which shares the now-completed entity
7281 -- list with its specific type. In case of underlying record views,
7282 -- we do not generate the corresponding class wide entity.
7285 and then not Is_Underlying_Record_View (Derived_Type)
7288 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
7290 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
7293 -- Update the scope of anonymous access types of discriminants and other
7294 -- components, to prevent scope anomalies in gigi, when the derivation
7295 -- appears in a scope nested within that of the parent.
7301 D := First_Entity (Derived_Type);
7302 while Present (D) loop
7303 if Ekind (D) = E_Discriminant
7304 or else Ekind (D) = E_Component
7306 if Is_Itype (Etype (D))
7307 and then Ekind (Etype (D)) = E_Anonymous_Access_Type
7309 Set_Scope (Etype (D), Current_Scope);
7316 end Build_Derived_Record_Type;
7318 ------------------------
7319 -- Build_Derived_Type --
7320 ------------------------
7322 procedure Build_Derived_Type
7324 Parent_Type : Entity_Id;
7325 Derived_Type : Entity_Id;
7326 Is_Completion : Boolean;
7327 Derive_Subps : Boolean := True)
7329 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7332 -- Set common attributes
7334 Set_Scope (Derived_Type, Current_Scope);
7336 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7337 Set_Etype (Derived_Type, Parent_Base);
7338 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
7340 Set_Size_Info (Derived_Type, Parent_Type);
7341 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
7342 Set_Convention (Derived_Type, Convention (Parent_Type));
7343 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7344 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
7346 -- The derived type inherits the representation clauses of the parent.
7347 -- However, for a private type that is completed by a derivation, there
7348 -- may be operation attributes that have been specified already (stream
7349 -- attributes and External_Tag) and those must be provided. Finally,
7350 -- if the partial view is a private extension, the representation items
7351 -- of the parent have been inherited already, and should not be chained
7352 -- twice to the derived type.
7354 if Is_Tagged_Type (Parent_Type)
7355 and then Present (First_Rep_Item (Derived_Type))
7357 -- The existing items are either operational items or items inherited
7358 -- from a private extension declaration.
7362 -- Used to iterate over representation items of the derived type
7365 -- Last representation item of the (non-empty) representation
7366 -- item list of the derived type.
7368 Found : Boolean := False;
7371 Rep := First_Rep_Item (Derived_Type);
7373 while Present (Rep) loop
7374 if Rep = First_Rep_Item (Parent_Type) then
7379 Rep := Next_Rep_Item (Rep);
7381 if Present (Rep) then
7387 -- Here if we either encountered the parent type's first rep
7388 -- item on the derived type's rep item list (in which case
7389 -- Found is True, and we have nothing else to do), or if we
7390 -- reached the last rep item of the derived type, which is
7391 -- Last_Rep, in which case we further chain the parent type's
7392 -- rep items to those of the derived type.
7395 Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
7400 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
7403 case Ekind (Parent_Type) is
7404 when Numeric_Kind =>
7405 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
7408 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
7412 | Class_Wide_Kind =>
7413 Build_Derived_Record_Type
7414 (N, Parent_Type, Derived_Type, Derive_Subps);
7417 when Enumeration_Kind =>
7418 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
7421 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
7423 when Incomplete_Or_Private_Kind =>
7424 Build_Derived_Private_Type
7425 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
7427 -- For discriminated types, the derivation includes deriving
7428 -- primitive operations. For others it is done below.
7430 if Is_Tagged_Type (Parent_Type)
7431 or else Has_Discriminants (Parent_Type)
7432 or else (Present (Full_View (Parent_Type))
7433 and then Has_Discriminants (Full_View (Parent_Type)))
7438 when Concurrent_Kind =>
7439 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
7442 raise Program_Error;
7445 if Etype (Derived_Type) = Any_Type then
7449 -- Set delayed freeze and then derive subprograms, we need to do this
7450 -- in this order so that derived subprograms inherit the derived freeze
7453 Set_Has_Delayed_Freeze (Derived_Type);
7454 if Derive_Subps then
7455 Derive_Subprograms (Parent_Type, Derived_Type);
7458 Set_Has_Primitive_Operations
7459 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
7460 end Build_Derived_Type;
7462 -----------------------
7463 -- Build_Discriminal --
7464 -----------------------
7466 procedure Build_Discriminal (Discrim : Entity_Id) is
7467 D_Minal : Entity_Id;
7468 CR_Disc : Entity_Id;
7471 -- A discriminal has the same name as the discriminant
7474 Make_Defining_Identifier (Sloc (Discrim),
7475 Chars => Chars (Discrim));
7477 Set_Ekind (D_Minal, E_In_Parameter);
7478 Set_Mechanism (D_Minal, Default_Mechanism);
7479 Set_Etype (D_Minal, Etype (Discrim));
7481 Set_Discriminal (Discrim, D_Minal);
7482 Set_Discriminal_Link (D_Minal, Discrim);
7484 -- For task types, build at once the discriminants of the corresponding
7485 -- record, which are needed if discriminants are used in entry defaults
7486 -- and in family bounds.
7488 if Is_Concurrent_Type (Current_Scope)
7489 or else Is_Limited_Type (Current_Scope)
7491 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
7493 Set_Ekind (CR_Disc, E_In_Parameter);
7494 Set_Mechanism (CR_Disc, Default_Mechanism);
7495 Set_Etype (CR_Disc, Etype (Discrim));
7496 Set_Discriminal_Link (CR_Disc, Discrim);
7497 Set_CR_Discriminant (Discrim, CR_Disc);
7499 end Build_Discriminal;
7501 ------------------------------------
7502 -- Build_Discriminant_Constraints --
7503 ------------------------------------
7505 function Build_Discriminant_Constraints
7508 Derived_Def : Boolean := False) return Elist_Id
7510 C : constant Node_Id := Constraint (Def);
7511 Nb_Discr : constant Nat := Number_Discriminants (T);
7513 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
7514 -- Saves the expression corresponding to a given discriminant in T
7516 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
7517 -- Return the Position number within array Discr_Expr of a discriminant
7518 -- D within the discriminant list of the discriminated type T.
7524 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
7528 Disc := First_Discriminant (T);
7529 for J in Discr_Expr'Range loop
7534 Next_Discriminant (Disc);
7537 -- Note: Since this function is called on discriminants that are
7538 -- known to belong to the discriminated type, falling through the
7539 -- loop with no match signals an internal compiler error.
7541 raise Program_Error;
7544 -- Declarations local to Build_Discriminant_Constraints
7548 Elist : constant Elist_Id := New_Elmt_List;
7556 Discrim_Present : Boolean := False;
7558 -- Start of processing for Build_Discriminant_Constraints
7561 -- The following loop will process positional associations only.
7562 -- For a positional association, the (single) discriminant is
7563 -- implicitly specified by position, in textual order (RM 3.7.2).
7565 Discr := First_Discriminant (T);
7566 Constr := First (Constraints (C));
7567 for D in Discr_Expr'Range loop
7568 exit when Nkind (Constr) = N_Discriminant_Association;
7571 Error_Msg_N ("too few discriminants given in constraint", C);
7572 return New_Elmt_List;
7574 elsif Nkind (Constr) = N_Range
7575 or else (Nkind (Constr) = N_Attribute_Reference
7577 Attribute_Name (Constr) = Name_Range)
7580 ("a range is not a valid discriminant constraint", Constr);
7581 Discr_Expr (D) := Error;
7584 Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
7585 Discr_Expr (D) := Constr;
7588 Next_Discriminant (Discr);
7592 if No (Discr) and then Present (Constr) then
7593 Error_Msg_N ("too many discriminants given in constraint", Constr);
7594 return New_Elmt_List;
7597 -- Named associations can be given in any order, but if both positional
7598 -- and named associations are used in the same discriminant constraint,
7599 -- then positional associations must occur first, at their normal
7600 -- position. Hence once a named association is used, the rest of the
7601 -- discriminant constraint must use only named associations.
7603 while Present (Constr) loop
7605 -- Positional association forbidden after a named association
7607 if Nkind (Constr) /= N_Discriminant_Association then
7608 Error_Msg_N ("positional association follows named one", Constr);
7609 return New_Elmt_List;
7611 -- Otherwise it is a named association
7614 -- E records the type of the discriminants in the named
7615 -- association. All the discriminants specified in the same name
7616 -- association must have the same type.
7620 -- Search the list of discriminants in T to see if the simple name
7621 -- given in the constraint matches any of them.
7623 Id := First (Selector_Names (Constr));
7624 while Present (Id) loop
7627 -- If Original_Discriminant is present, we are processing a
7628 -- generic instantiation and this is an instance node. We need
7629 -- to find the name of the corresponding discriminant in the
7630 -- actual record type T and not the name of the discriminant in
7631 -- the generic formal. Example:
7634 -- type G (D : int) is private;
7636 -- subtype W is G (D => 1);
7638 -- type Rec (X : int) is record ... end record;
7639 -- package Q is new P (G => Rec);
7641 -- At the point of the instantiation, formal type G is Rec
7642 -- and therefore when reanalyzing "subtype W is G (D => 1);"
7643 -- which really looks like "subtype W is Rec (D => 1);" at
7644 -- the point of instantiation, we want to find the discriminant
7645 -- that corresponds to D in Rec, i.e. X.
7647 if Present (Original_Discriminant (Id)) then
7648 Discr := Find_Corresponding_Discriminant (Id, T);
7652 Discr := First_Discriminant (T);
7653 while Present (Discr) loop
7654 if Chars (Discr) = Chars (Id) then
7659 Next_Discriminant (Discr);
7663 Error_Msg_N ("& does not match any discriminant", Id);
7664 return New_Elmt_List;
7666 -- The following is only useful for the benefit of generic
7667 -- instances but it does not interfere with other
7668 -- processing for the non-generic case so we do it in all
7669 -- cases (for generics this statement is executed when
7670 -- processing the generic definition, see comment at the
7671 -- beginning of this if statement).
7674 Set_Original_Discriminant (Id, Discr);
7678 Position := Pos_Of_Discr (T, Discr);
7680 if Present (Discr_Expr (Position)) then
7681 Error_Msg_N ("duplicate constraint for discriminant&", Id);
7684 -- Each discriminant specified in the same named association
7685 -- must be associated with a separate copy of the
7686 -- corresponding expression.
7688 if Present (Next (Id)) then
7689 Expr := New_Copy_Tree (Expression (Constr));
7690 Set_Parent (Expr, Parent (Expression (Constr)));
7692 Expr := Expression (Constr);
7695 Discr_Expr (Position) := Expr;
7696 Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
7699 -- A discriminant association with more than one discriminant
7700 -- name is only allowed if the named discriminants are all of
7701 -- the same type (RM 3.7.1(8)).
7704 E := Base_Type (Etype (Discr));
7706 elsif Base_Type (Etype (Discr)) /= E then
7708 ("all discriminants in an association " &
7709 "must have the same type", Id);
7719 -- A discriminant constraint must provide exactly one value for each
7720 -- discriminant of the type (RM 3.7.1(8)).
7722 for J in Discr_Expr'Range loop
7723 if No (Discr_Expr (J)) then
7724 Error_Msg_N ("too few discriminants given in constraint", C);
7725 return New_Elmt_List;
7729 -- Determine if there are discriminant expressions in the constraint
7731 for J in Discr_Expr'Range loop
7732 if Denotes_Discriminant
7733 (Discr_Expr (J), Check_Concurrent => True)
7735 Discrim_Present := True;
7739 -- Build an element list consisting of the expressions given in the
7740 -- discriminant constraint and apply the appropriate checks. The list
7741 -- is constructed after resolving any named discriminant associations
7742 -- and therefore the expressions appear in the textual order of the
7745 Discr := First_Discriminant (T);
7746 for J in Discr_Expr'Range loop
7747 if Discr_Expr (J) /= Error then
7748 Append_Elmt (Discr_Expr (J), Elist);
7750 -- If any of the discriminant constraints is given by a
7751 -- discriminant and we are in a derived type declaration we
7752 -- have a discriminant renaming. Establish link between new
7753 -- and old discriminant.
7755 if Denotes_Discriminant (Discr_Expr (J)) then
7757 Set_Corresponding_Discriminant
7758 (Entity (Discr_Expr (J)), Discr);
7761 -- Force the evaluation of non-discriminant expressions.
7762 -- If we have found a discriminant in the constraint 3.4(26)
7763 -- and 3.8(18) demand that no range checks are performed are
7764 -- after evaluation. If the constraint is for a component
7765 -- definition that has a per-object constraint, expressions are
7766 -- evaluated but not checked either. In all other cases perform
7770 if Discrim_Present then
7773 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
7775 Has_Per_Object_Constraint
7776 (Defining_Identifier (Parent (Parent (Def))))
7780 elsif Is_Access_Type (Etype (Discr)) then
7781 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
7784 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
7787 Force_Evaluation (Discr_Expr (J));
7790 -- Check that the designated type of an access discriminant's
7791 -- expression is not a class-wide type unless the discriminant's
7792 -- designated type is also class-wide.
7794 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
7795 and then not Is_Class_Wide_Type
7796 (Designated_Type (Etype (Discr)))
7797 and then Etype (Discr_Expr (J)) /= Any_Type
7798 and then Is_Class_Wide_Type
7799 (Designated_Type (Etype (Discr_Expr (J))))
7801 Wrong_Type (Discr_Expr (J), Etype (Discr));
7803 elsif Is_Access_Type (Etype (Discr))
7804 and then not Is_Access_Constant (Etype (Discr))
7805 and then Is_Access_Type (Etype (Discr_Expr (J)))
7806 and then Is_Access_Constant (Etype (Discr_Expr (J)))
7809 ("constraint for discriminant& must be access to variable",
7814 Next_Discriminant (Discr);
7818 end Build_Discriminant_Constraints;
7820 ---------------------------------
7821 -- Build_Discriminated_Subtype --
7822 ---------------------------------
7824 procedure Build_Discriminated_Subtype
7828 Related_Nod : Node_Id;
7829 For_Access : Boolean := False)
7831 Has_Discrs : constant Boolean := Has_Discriminants (T);
7832 Constrained : constant Boolean :=
7834 and then not Is_Empty_Elmt_List (Elist)
7835 and then not Is_Class_Wide_Type (T))
7836 or else Is_Constrained (T);
7839 if Ekind (T) = E_Record_Type then
7841 Set_Ekind (Def_Id, E_Private_Subtype);
7842 Set_Is_For_Access_Subtype (Def_Id, True);
7844 Set_Ekind (Def_Id, E_Record_Subtype);
7847 -- Inherit preelaboration flag from base, for types for which it
7848 -- may have been set: records, private types, protected types.
7850 Set_Known_To_Have_Preelab_Init
7851 (Def_Id, Known_To_Have_Preelab_Init (T));
7853 elsif Ekind (T) = E_Task_Type then
7854 Set_Ekind (Def_Id, E_Task_Subtype);
7856 elsif Ekind (T) = E_Protected_Type then
7857 Set_Ekind (Def_Id, E_Protected_Subtype);
7858 Set_Known_To_Have_Preelab_Init
7859 (Def_Id, Known_To_Have_Preelab_Init (T));
7861 elsif Is_Private_Type (T) then
7862 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
7863 Set_Known_To_Have_Preelab_Init
7864 (Def_Id, Known_To_Have_Preelab_Init (T));
7866 elsif Is_Class_Wide_Type (T) then
7867 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
7870 -- Incomplete type. Attach subtype to list of dependents, to be
7871 -- completed with full view of parent type, unless is it the
7872 -- designated subtype of a record component within an init_proc.
7873 -- This last case arises for a component of an access type whose
7874 -- designated type is incomplete (e.g. a Taft Amendment type).
7875 -- The designated subtype is within an inner scope, and needs no
7876 -- elaboration, because only the access type is needed in the
7877 -- initialization procedure.
7879 Set_Ekind (Def_Id, Ekind (T));
7881 if For_Access and then Within_Init_Proc then
7884 Append_Elmt (Def_Id, Private_Dependents (T));
7888 Set_Etype (Def_Id, T);
7889 Init_Size_Align (Def_Id);
7890 Set_Has_Discriminants (Def_Id, Has_Discrs);
7891 Set_Is_Constrained (Def_Id, Constrained);
7893 Set_First_Entity (Def_Id, First_Entity (T));
7894 Set_Last_Entity (Def_Id, Last_Entity (T));
7896 -- If the subtype is the completion of a private declaration, there may
7897 -- have been representation clauses for the partial view, and they must
7898 -- be preserved. Build_Derived_Type chains the inherited clauses with
7899 -- the ones appearing on the extension. If this comes from a subtype
7900 -- declaration, all clauses are inherited.
7902 if No (First_Rep_Item (Def_Id)) then
7903 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
7906 if Is_Tagged_Type (T) then
7907 Set_Is_Tagged_Type (Def_Id);
7908 Make_Class_Wide_Type (Def_Id);
7911 Set_Stored_Constraint (Def_Id, No_Elist);
7914 Set_Discriminant_Constraint (Def_Id, Elist);
7915 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
7918 if Is_Tagged_Type (T) then
7920 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
7921 -- concurrent record type (which has the list of primitive
7924 if Ada_Version >= Ada_05
7925 and then Is_Concurrent_Type (T)
7927 Set_Corresponding_Record_Type (Def_Id,
7928 Corresponding_Record_Type (T));
7930 Set_Primitive_Operations (Def_Id, Primitive_Operations (T));
7933 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
7936 -- Subtypes introduced by component declarations do not need to be
7937 -- marked as delayed, and do not get freeze nodes, because the semantics
7938 -- verifies that the parents of the subtypes are frozen before the
7939 -- enclosing record is frozen.
7941 if not Is_Type (Scope (Def_Id)) then
7942 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
7944 if Is_Private_Type (T)
7945 and then Present (Full_View (T))
7947 Conditional_Delay (Def_Id, Full_View (T));
7949 Conditional_Delay (Def_Id, T);
7953 if Is_Record_Type (T) then
7954 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
7957 and then not Is_Empty_Elmt_List (Elist)
7958 and then not For_Access
7960 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
7961 elsif not For_Access then
7962 Set_Cloned_Subtype (Def_Id, T);
7965 end Build_Discriminated_Subtype;
7967 ---------------------------
7968 -- Build_Itype_Reference --
7969 ---------------------------
7971 procedure Build_Itype_Reference
7975 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
7977 Set_Itype (IR, Ityp);
7978 Insert_After (Nod, IR);
7979 end Build_Itype_Reference;
7981 ------------------------
7982 -- Build_Scalar_Bound --
7983 ------------------------
7985 function Build_Scalar_Bound
7988 Der_T : Entity_Id) return Node_Id
7990 New_Bound : Entity_Id;
7993 -- Note: not clear why this is needed, how can the original bound
7994 -- be unanalyzed at this point? and if it is, what business do we
7995 -- have messing around with it? and why is the base type of the
7996 -- parent type the right type for the resolution. It probably is
7997 -- not! It is OK for the new bound we are creating, but not for
7998 -- the old one??? Still if it never happens, no problem!
8000 Analyze_And_Resolve (Bound, Base_Type (Par_T));
8002 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
8003 New_Bound := New_Copy (Bound);
8004 Set_Etype (New_Bound, Der_T);
8005 Set_Analyzed (New_Bound);
8007 elsif Is_Entity_Name (Bound) then
8008 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
8010 -- The following is almost certainly wrong. What business do we have
8011 -- relocating a node (Bound) that is presumably still attached to
8012 -- the tree elsewhere???
8015 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
8018 Set_Etype (New_Bound, Der_T);
8020 end Build_Scalar_Bound;
8022 --------------------------------
8023 -- Build_Underlying_Full_View --
8024 --------------------------------
8026 procedure Build_Underlying_Full_View
8031 Loc : constant Source_Ptr := Sloc (N);
8032 Subt : constant Entity_Id :=
8033 Make_Defining_Identifier
8034 (Loc, New_External_Name (Chars (Typ), 'S'));
8041 procedure Set_Discriminant_Name (Id : Node_Id);
8042 -- If the derived type has discriminants, they may rename discriminants
8043 -- of the parent. When building the full view of the parent, we need to
8044 -- recover the names of the original discriminants if the constraint is
8045 -- given by named associations.
8047 ---------------------------
8048 -- Set_Discriminant_Name --
8049 ---------------------------
8051 procedure Set_Discriminant_Name (Id : Node_Id) is
8055 Set_Original_Discriminant (Id, Empty);
8057 if Has_Discriminants (Typ) then
8058 Disc := First_Discriminant (Typ);
8059 while Present (Disc) loop
8060 if Chars (Disc) = Chars (Id)
8061 and then Present (Corresponding_Discriminant (Disc))
8063 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
8065 Next_Discriminant (Disc);
8068 end Set_Discriminant_Name;
8070 -- Start of processing for Build_Underlying_Full_View
8073 if Nkind (N) = N_Full_Type_Declaration then
8074 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
8076 elsif Nkind (N) = N_Subtype_Declaration then
8077 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
8079 elsif Nkind (N) = N_Component_Declaration then
8082 (Constraint (Subtype_Indication (Component_Definition (N))));
8085 raise Program_Error;
8088 C := First (Constraints (Constr));
8089 while Present (C) loop
8090 if Nkind (C) = N_Discriminant_Association then
8091 Id := First (Selector_Names (C));
8092 while Present (Id) loop
8093 Set_Discriminant_Name (Id);
8102 Make_Subtype_Declaration (Loc,
8103 Defining_Identifier => Subt,
8104 Subtype_Indication =>
8105 Make_Subtype_Indication (Loc,
8106 Subtype_Mark => New_Reference_To (Par, Loc),
8107 Constraint => New_Copy_Tree (Constr)));
8109 -- If this is a component subtype for an outer itype, it is not
8110 -- a list member, so simply set the parent link for analysis: if
8111 -- the enclosing type does not need to be in a declarative list,
8112 -- neither do the components.
8114 if Is_List_Member (N)
8115 and then Nkind (N) /= N_Component_Declaration
8117 Insert_Before (N, Indic);
8119 Set_Parent (Indic, Parent (N));
8123 Set_Underlying_Full_View (Typ, Full_View (Subt));
8124 end Build_Underlying_Full_View;
8126 -------------------------------
8127 -- Check_Abstract_Overriding --
8128 -------------------------------
8130 procedure Check_Abstract_Overriding (T : Entity_Id) is
8131 Alias_Subp : Entity_Id;
8138 Op_List := Primitive_Operations (T);
8140 -- Loop to check primitive operations
8142 Elmt := First_Elmt (Op_List);
8143 while Present (Elmt) loop
8144 Subp := Node (Elmt);
8145 Alias_Subp := Alias (Subp);
8147 -- Inherited subprograms are identified by the fact that they do not
8148 -- come from source, and the associated source location is the
8149 -- location of the first subtype of the derived type.
8151 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
8152 -- subprograms that "require overriding".
8154 -- Special exception, do not complain about failure to override the
8155 -- stream routines _Input and _Output, as well as the primitive
8156 -- operations used in dispatching selects since we always provide
8157 -- automatic overridings for these subprograms.
8159 -- Also ignore this rule for convention CIL since .NET libraries
8160 -- do bizarre things with interfaces???
8162 -- The partial view of T may have been a private extension, for
8163 -- which inherited functions dispatching on result are abstract.
8164 -- If the full view is a null extension, there is no need for
8165 -- overriding in Ada2005, but wrappers need to be built for them
8166 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
8168 if Is_Null_Extension (T)
8169 and then Has_Controlling_Result (Subp)
8170 and then Ada_Version >= Ada_05
8171 and then Present (Alias_Subp)
8172 and then not Comes_From_Source (Subp)
8173 and then not Is_Abstract_Subprogram (Alias_Subp)
8174 and then not Is_Access_Type (Etype (Subp))
8178 -- Ada 2005 (AI-251): Internal entities of interfaces need no
8179 -- processing because this check is done with the aliased
8182 elsif Present (Interface_Alias (Subp)) then
8185 elsif (Is_Abstract_Subprogram (Subp)
8186 or else Requires_Overriding (Subp)
8188 (Has_Controlling_Result (Subp)
8189 and then Present (Alias_Subp)
8190 and then not Comes_From_Source (Subp)
8191 and then Sloc (Subp) = Sloc (First_Subtype (T))))
8192 and then not Is_TSS (Subp, TSS_Stream_Input)
8193 and then not Is_TSS (Subp, TSS_Stream_Output)
8194 and then not Is_Abstract_Type (T)
8195 and then Convention (T) /= Convention_CIL
8196 and then not Is_Predefined_Interface_Primitive (Subp)
8198 -- Ada 2005 (AI-251): Do not consider hidden entities associated
8199 -- with abstract interface types because the check will be done
8200 -- with the aliased entity (otherwise we generate a duplicated
8203 and then not Present (Interface_Alias (Subp))
8205 if Present (Alias_Subp) then
8207 -- Only perform the check for a derived subprogram when the
8208 -- type has an explicit record extension. This avoids incorrect
8209 -- flagging of abstract subprograms for the case of a type
8210 -- without an extension that is derived from a formal type
8211 -- with a tagged actual (can occur within a private part).
8213 -- Ada 2005 (AI-391): In the case of an inherited function with
8214 -- a controlling result of the type, the rule does not apply if
8215 -- the type is a null extension (unless the parent function
8216 -- itself is abstract, in which case the function must still be
8217 -- be overridden). The expander will generate an overriding
8218 -- wrapper function calling the parent subprogram (see
8219 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
8221 Type_Def := Type_Definition (Parent (T));
8223 if Nkind (Type_Def) = N_Derived_Type_Definition
8224 and then Present (Record_Extension_Part (Type_Def))
8226 (Ada_Version < Ada_05
8227 or else not Is_Null_Extension (T)
8228 or else Ekind (Subp) = E_Procedure
8229 or else not Has_Controlling_Result (Subp)
8230 or else Is_Abstract_Subprogram (Alias_Subp)
8231 or else Requires_Overriding (Subp)
8232 or else Is_Access_Type (Etype (Subp)))
8234 -- Avoid reporting error in case of abstract predefined
8235 -- primitive inherited from interface type because the
8236 -- body of internally generated predefined primitives
8237 -- of tagged types are generated later by Freeze_Type
8239 if Is_Interface (Root_Type (T))
8240 and then Is_Abstract_Subprogram (Subp)
8241 and then Is_Predefined_Dispatching_Operation (Subp)
8242 and then not Comes_From_Source (Ultimate_Alias (Subp))
8248 ("type must be declared abstract or & overridden",
8251 -- Traverse the whole chain of aliased subprograms to
8252 -- complete the error notification. This is especially
8253 -- useful for traceability of the chain of entities when
8254 -- the subprogram corresponds with an interface
8255 -- subprogram (which may be defined in another package).
8257 if Present (Alias_Subp) then
8263 while Present (Alias (E)) loop
8264 Error_Msg_Sloc := Sloc (E);
8266 ("\& has been inherited #", T, Subp);
8270 Error_Msg_Sloc := Sloc (E);
8272 ("\& has been inherited from subprogram #",
8278 -- Ada 2005 (AI-345): Protected or task type implementing
8279 -- abstract interfaces.
8281 elsif Is_Concurrent_Record_Type (T)
8282 and then Present (Interfaces (T))
8284 -- The controlling formal of Subp must be of mode "out",
8285 -- "in out" or an access-to-variable to be overridden.
8287 -- Error message below needs rewording (remember comma
8288 -- in -gnatj mode) ???
8290 if Ekind (First_Formal (Subp)) = E_In_Parameter
8291 and then Ekind (Subp) /= E_Function
8293 if not Is_Predefined_Dispatching_Operation (Subp) then
8295 ("first formal of & must be of mode `OUT`, " &
8296 "`IN OUT` or access-to-variable", T, Subp);
8298 ("\to be overridden by protected procedure or " &
8299 "entry (RM 9.4(11.9/2))", T);
8302 -- Some other kind of overriding failure
8306 ("interface subprogram & must be overridden",
8309 -- Examine primitive operations of synchronized type,
8310 -- to find homonyms that have the wrong profile.
8317 First_Entity (Corresponding_Concurrent_Type (T));
8318 while Present (Prim) loop
8319 if Chars (Prim) = Chars (Subp) then
8321 ("profile is not type conformant with "
8322 & "prefixed view profile of "
8323 & "inherited operation&", Prim, Subp);
8333 Error_Msg_Node_2 := T;
8335 ("abstract subprogram& not allowed for type&", Subp);
8337 -- Also post unconditional warning on the type (unconditional
8338 -- so that if there are more than one of these cases, we get
8339 -- them all, and not just the first one).
8341 Error_Msg_Node_2 := Subp;
8343 ("nonabstract type& has abstract subprogram&!", T);
8347 -- Ada 2005 (AI05-0030): Inspect hidden subprograms which provide
8348 -- the mapping between interface and implementing type primitives.
8349 -- If the interface alias is marked as Implemented_By_Entry, the
8350 -- alias must be an entry wrapper.
8352 if Ada_Version >= Ada_05
8353 and then Is_Hidden (Subp)
8354 and then Present (Interface_Alias (Subp))
8355 and then Implemented_By_Entry (Interface_Alias (Subp))
8356 and then Present (Alias_Subp)
8358 (not Is_Primitive_Wrapper (Alias_Subp)
8359 or else Ekind (Wrapped_Entity (Alias_Subp)) /= E_Entry)
8362 Error_Ent : Entity_Id := T;
8365 if Is_Concurrent_Record_Type (Error_Ent) then
8366 Error_Ent := Corresponding_Concurrent_Type (Error_Ent);
8369 Error_Msg_Node_2 := Interface_Alias (Subp);
8371 ("type & must implement abstract subprogram & with an entry",
8372 Error_Ent, Error_Ent);
8378 end Check_Abstract_Overriding;
8380 ------------------------------------------------
8381 -- Check_Access_Discriminant_Requires_Limited --
8382 ------------------------------------------------
8384 procedure Check_Access_Discriminant_Requires_Limited
8389 -- A discriminant_specification for an access discriminant shall appear
8390 -- only in the declaration for a task or protected type, or for a type
8391 -- with the reserved word 'limited' in its definition or in one of its
8392 -- ancestors. (RM 3.7(10))
8394 if Nkind (Discriminant_Type (D)) = N_Access_Definition
8395 and then not Is_Concurrent_Type (Current_Scope)
8396 and then not Is_Concurrent_Record_Type (Current_Scope)
8397 and then not Is_Limited_Record (Current_Scope)
8398 and then Ekind (Current_Scope) /= E_Limited_Private_Type
8401 ("access discriminants allowed only for limited types", Loc);
8403 end Check_Access_Discriminant_Requires_Limited;
8405 -----------------------------------
8406 -- Check_Aliased_Component_Types --
8407 -----------------------------------
8409 procedure Check_Aliased_Component_Types (T : Entity_Id) is
8413 -- ??? Also need to check components of record extensions, but not
8414 -- components of protected types (which are always limited).
8416 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
8417 -- types to be unconstrained. This is safe because it is illegal to
8418 -- create access subtypes to such types with explicit discriminant
8421 if not Is_Limited_Type (T) then
8422 if Ekind (T) = E_Record_Type then
8423 C := First_Component (T);
8424 while Present (C) loop
8426 and then Has_Discriminants (Etype (C))
8427 and then not Is_Constrained (Etype (C))
8428 and then not In_Instance_Body
8429 and then Ada_Version < Ada_05
8432 ("aliased component must be constrained (RM 3.6(11))",
8439 elsif Ekind (T) = E_Array_Type then
8440 if Has_Aliased_Components (T)
8441 and then Has_Discriminants (Component_Type (T))
8442 and then not Is_Constrained (Component_Type (T))
8443 and then not In_Instance_Body
8444 and then Ada_Version < Ada_05
8447 ("aliased component type must be constrained (RM 3.6(11))",
8452 end Check_Aliased_Component_Types;
8454 ----------------------
8455 -- Check_Completion --
8456 ----------------------
8458 procedure Check_Completion (Body_Id : Node_Id := Empty) is
8461 procedure Post_Error;
8462 -- Post error message for lack of completion for entity E
8468 procedure Post_Error is
8470 procedure Missing_Body;
8471 -- Output missing body message
8477 procedure Missing_Body is
8479 -- Spec is in same unit, so we can post on spec
8481 if In_Same_Source_Unit (Body_Id, E) then
8482 Error_Msg_N ("missing body for &", E);
8484 -- Spec is in a separate unit, so we have to post on the body
8487 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
8491 -- Start of processing for Post_Error
8494 if not Comes_From_Source (E) then
8496 if Ekind (E) = E_Task_Type
8497 or else Ekind (E) = E_Protected_Type
8499 -- It may be an anonymous protected type created for a
8500 -- single variable. Post error on variable, if present.
8506 Var := First_Entity (Current_Scope);
8507 while Present (Var) loop
8508 exit when Etype (Var) = E
8509 and then Comes_From_Source (Var);
8514 if Present (Var) then
8521 -- If a generated entity has no completion, then either previous
8522 -- semantic errors have disabled the expansion phase, or else we had
8523 -- missing subunits, or else we are compiling without expansion,
8524 -- or else something is very wrong.
8526 if not Comes_From_Source (E) then
8528 (Serious_Errors_Detected > 0
8529 or else Configurable_Run_Time_Violations > 0
8530 or else Subunits_Missing
8531 or else not Expander_Active);
8534 -- Here for source entity
8537 -- Here if no body to post the error message, so we post the error
8538 -- on the declaration that has no completion. This is not really
8539 -- the right place to post it, think about this later ???
8541 if No (Body_Id) then
8544 ("missing full declaration for }", Parent (E), E);
8547 ("missing body for &", Parent (E), E);
8550 -- Package body has no completion for a declaration that appears
8551 -- in the corresponding spec. Post error on the body, with a
8552 -- reference to the non-completed declaration.
8555 Error_Msg_Sloc := Sloc (E);
8559 ("missing full declaration for }!", Body_Id, E);
8561 elsif Is_Overloadable (E)
8562 and then Current_Entity_In_Scope (E) /= E
8564 -- It may be that the completion is mistyped and appears as
8565 -- a distinct overloading of the entity.
8568 Candidate : constant Entity_Id :=
8569 Current_Entity_In_Scope (E);
8570 Decl : constant Node_Id :=
8571 Unit_Declaration_Node (Candidate);
8574 if Is_Overloadable (Candidate)
8575 and then Ekind (Candidate) = Ekind (E)
8576 and then Nkind (Decl) = N_Subprogram_Body
8577 and then Acts_As_Spec (Decl)
8579 Check_Type_Conformant (Candidate, E);
8593 -- Start of processing for Check_Completion
8596 E := First_Entity (Current_Scope);
8597 while Present (E) loop
8598 if Is_Intrinsic_Subprogram (E) then
8601 -- The following situation requires special handling: a child unit
8602 -- that appears in the context clause of the body of its parent:
8604 -- procedure Parent.Child (...);
8606 -- with Parent.Child;
8607 -- package body Parent is
8609 -- Here Parent.Child appears as a local entity, but should not be
8610 -- flagged as requiring completion, because it is a compilation
8613 -- Ignore missing completion for a subprogram that does not come from
8614 -- source (including the _Call primitive operation of RAS types,
8615 -- which has to have the flag Comes_From_Source for other purposes):
8616 -- we assume that the expander will provide the missing completion.
8618 elsif Ekind (E) = E_Function
8619 or else Ekind (E) = E_Procedure
8620 or else Ekind (E) = E_Generic_Function
8621 or else Ekind (E) = E_Generic_Procedure
8623 if not Has_Completion (E)
8624 and then not (Is_Subprogram (E)
8625 and then Is_Abstract_Subprogram (E))
8626 and then not (Is_Subprogram (E)
8628 (not Comes_From_Source (E)
8629 or else Chars (E) = Name_uCall))
8630 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8632 and then Chars (E) /= Name_uSize
8637 elsif Is_Entry (E) then
8638 if not Has_Completion (E) and then
8639 (Ekind (Scope (E)) = E_Protected_Object
8640 or else Ekind (Scope (E)) = E_Protected_Type)
8645 elsif Is_Package_Or_Generic_Package (E) then
8646 if Unit_Requires_Body (E) then
8647 if not Has_Completion (E)
8648 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
8654 elsif not Is_Child_Unit (E) then
8655 May_Need_Implicit_Body (E);
8658 elsif Ekind (E) = E_Incomplete_Type
8659 and then No (Underlying_Type (E))
8663 elsif (Ekind (E) = E_Task_Type or else
8664 Ekind (E) = E_Protected_Type)
8665 and then not Has_Completion (E)
8669 -- A single task declared in the current scope is a constant, verify
8670 -- that the body of its anonymous type is in the same scope. If the
8671 -- task is defined elsewhere, this may be a renaming declaration for
8672 -- which no completion is needed.
8674 elsif Ekind (E) = E_Constant
8675 and then Ekind (Etype (E)) = E_Task_Type
8676 and then not Has_Completion (Etype (E))
8677 and then Scope (Etype (E)) = Current_Scope
8681 elsif Ekind (E) = E_Protected_Object
8682 and then not Has_Completion (Etype (E))
8686 elsif Ekind (E) = E_Record_Type then
8687 if Is_Tagged_Type (E) then
8688 Check_Abstract_Overriding (E);
8689 Check_Conventions (E);
8692 Check_Aliased_Component_Types (E);
8694 elsif Ekind (E) = E_Array_Type then
8695 Check_Aliased_Component_Types (E);
8701 end Check_Completion;
8703 ----------------------------
8704 -- Check_Delta_Expression --
8705 ----------------------------
8707 procedure Check_Delta_Expression (E : Node_Id) is
8709 if not (Is_Real_Type (Etype (E))) then
8710 Wrong_Type (E, Any_Real);
8712 elsif not Is_OK_Static_Expression (E) then
8713 Flag_Non_Static_Expr
8714 ("non-static expression used for delta value!", E);
8716 elsif not UR_Is_Positive (Expr_Value_R (E)) then
8717 Error_Msg_N ("delta expression must be positive", E);
8723 -- If any of above errors occurred, then replace the incorrect
8724 -- expression by the real 0.1, which should prevent further errors.
8727 Make_Real_Literal (Sloc (E), Ureal_Tenth));
8728 Analyze_And_Resolve (E, Standard_Float);
8729 end Check_Delta_Expression;
8731 -----------------------------
8732 -- Check_Digits_Expression --
8733 -----------------------------
8735 procedure Check_Digits_Expression (E : Node_Id) is
8737 if not (Is_Integer_Type (Etype (E))) then
8738 Wrong_Type (E, Any_Integer);
8740 elsif not Is_OK_Static_Expression (E) then
8741 Flag_Non_Static_Expr
8742 ("non-static expression used for digits value!", E);
8744 elsif Expr_Value (E) <= 0 then
8745 Error_Msg_N ("digits value must be greater than zero", E);
8751 -- If any of above errors occurred, then replace the incorrect
8752 -- expression by the integer 1, which should prevent further errors.
8754 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
8755 Analyze_And_Resolve (E, Standard_Integer);
8757 end Check_Digits_Expression;
8759 --------------------------
8760 -- Check_Initialization --
8761 --------------------------
8763 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
8765 if Is_Limited_Type (T)
8766 and then not In_Instance
8767 and then not In_Inlined_Body
8769 if not OK_For_Limited_Init (Exp) then
8771 -- In GNAT mode, this is just a warning, to allow it to be evilly
8772 -- turned off. Otherwise it is a real error.
8776 ("?cannot initialize entities of limited type!", Exp);
8778 elsif Ada_Version < Ada_05 then
8780 ("cannot initialize entities of limited type", Exp);
8781 Explain_Limited_Type (T, Exp);
8784 -- Specialize error message according to kind of illegal
8785 -- initial expression.
8787 if Nkind (Exp) = N_Type_Conversion
8788 and then Nkind (Expression (Exp)) = N_Function_Call
8791 ("illegal context for call"
8792 & " to function with limited result", Exp);
8796 ("initialization of limited object requires aggregate "
8797 & "or function call", Exp);
8802 end Check_Initialization;
8804 ----------------------
8805 -- Check_Interfaces --
8806 ----------------------
8808 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
8809 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
8812 Iface_Def : Node_Id;
8813 Iface_Typ : Entity_Id;
8814 Parent_Node : Node_Id;
8816 Is_Task : Boolean := False;
8817 -- Set True if parent type or any progenitor is a task interface
8819 Is_Protected : Boolean := False;
8820 -- Set True if parent type or any progenitor is a protected interface
8822 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
8823 -- Check that a progenitor is compatible with declaration.
8824 -- Error is posted on Error_Node.
8830 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
8831 Iface_Id : constant Entity_Id :=
8832 Defining_Identifier (Parent (Iface_Def));
8836 if Nkind (N) = N_Private_Extension_Declaration then
8839 Type_Def := Type_Definition (N);
8842 if Is_Task_Interface (Iface_Id) then
8845 elsif Is_Protected_Interface (Iface_Id) then
8846 Is_Protected := True;
8849 if Is_Synchronized_Interface (Iface_Id) then
8851 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
8852 -- extension derived from a synchronized interface must explicitly
8853 -- be declared synchronized, because the full view will be a
8854 -- synchronized type.
8856 if Nkind (N) = N_Private_Extension_Declaration then
8857 if not Synchronized_Present (N) then
8859 ("private extension of& must be explicitly synchronized",
8863 -- However, by 3.9.4(16/2), a full type that is a record extension
8864 -- is never allowed to derive from a synchronized interface (note
8865 -- that interfaces must be excluded from this check, because those
8866 -- are represented by derived type definitions in some cases).
8868 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
8869 and then not Interface_Present (Type_Definition (N))
8871 Error_Msg_N ("record extension cannot derive from synchronized"
8872 & " interface", Error_Node);
8876 -- Check that the characteristics of the progenitor are compatible
8877 -- with the explicit qualifier in the declaration.
8878 -- The check only applies to qualifiers that come from source.
8879 -- Limited_Present also appears in the declaration of corresponding
8880 -- records, and the check does not apply to them.
8882 if Limited_Present (Type_Def)
8884 Is_Concurrent_Record_Type (Defining_Identifier (N))
8886 if Is_Limited_Interface (Parent_Type)
8887 and then not Is_Limited_Interface (Iface_Id)
8890 ("progenitor& must be limited interface",
8891 Error_Node, Iface_Id);
8894 (Task_Present (Iface_Def)
8895 or else Protected_Present (Iface_Def)
8896 or else Synchronized_Present (Iface_Def))
8897 and then Nkind (N) /= N_Private_Extension_Declaration
8898 and then not Error_Posted (N)
8901 ("progenitor& must be limited interface",
8902 Error_Node, Iface_Id);
8905 -- Protected interfaces can only inherit from limited, synchronized
8906 -- or protected interfaces.
8908 elsif Nkind (N) = N_Full_Type_Declaration
8909 and then Protected_Present (Type_Def)
8911 if Limited_Present (Iface_Def)
8912 or else Synchronized_Present (Iface_Def)
8913 or else Protected_Present (Iface_Def)
8917 elsif Task_Present (Iface_Def) then
8918 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
8919 & " from task interface", Error_Node);
8922 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
8923 & " from non-limited interface", Error_Node);
8926 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
8927 -- limited and synchronized.
8929 elsif Synchronized_Present (Type_Def) then
8930 if Limited_Present (Iface_Def)
8931 or else Synchronized_Present (Iface_Def)
8935 elsif Protected_Present (Iface_Def)
8936 and then Nkind (N) /= N_Private_Extension_Declaration
8938 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8939 & " from protected interface", Error_Node);
8941 elsif Task_Present (Iface_Def)
8942 and then Nkind (N) /= N_Private_Extension_Declaration
8944 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8945 & " from task interface", Error_Node);
8947 elsif not Is_Limited_Interface (Iface_Id) then
8948 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
8949 & " from non-limited interface", Error_Node);
8952 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
8953 -- synchronized or task interfaces.
8955 elsif Nkind (N) = N_Full_Type_Declaration
8956 and then Task_Present (Type_Def)
8958 if Limited_Present (Iface_Def)
8959 or else Synchronized_Present (Iface_Def)
8960 or else Task_Present (Iface_Def)
8964 elsif Protected_Present (Iface_Def) then
8965 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
8966 & " protected interface", Error_Node);
8969 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
8970 & " non-limited interface", Error_Node);
8975 -- Start of processing for Check_Interfaces
8978 if Is_Interface (Parent_Type) then
8979 if Is_Task_Interface (Parent_Type) then
8982 elsif Is_Protected_Interface (Parent_Type) then
8983 Is_Protected := True;
8987 if Nkind (N) = N_Private_Extension_Declaration then
8989 -- Check that progenitors are compatible with declaration
8991 Iface := First (Interface_List (Def));
8992 while Present (Iface) loop
8993 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
8995 Parent_Node := Parent (Base_Type (Iface_Typ));
8996 Iface_Def := Type_Definition (Parent_Node);
8998 if not Is_Interface (Iface_Typ) then
8999 Diagnose_Interface (Iface, Iface_Typ);
9002 Check_Ifaces (Iface_Def, Iface);
9008 if Is_Task and Is_Protected then
9010 ("type cannot derive from task and protected interface", N);
9016 -- Full type declaration of derived type.
9017 -- Check compatibility with parent if it is interface type
9019 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9020 and then Is_Interface (Parent_Type)
9022 Parent_Node := Parent (Parent_Type);
9024 -- More detailed checks for interface varieties
9027 (Iface_Def => Type_Definition (Parent_Node),
9028 Error_Node => Subtype_Indication (Type_Definition (N)));
9031 Iface := First (Interface_List (Def));
9032 while Present (Iface) loop
9033 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9035 Parent_Node := Parent (Base_Type (Iface_Typ));
9036 Iface_Def := Type_Definition (Parent_Node);
9038 if not Is_Interface (Iface_Typ) then
9039 Diagnose_Interface (Iface, Iface_Typ);
9042 -- "The declaration of a specific descendant of an interface
9043 -- type freezes the interface type" RM 13.14
9045 Freeze_Before (N, Iface_Typ);
9046 Check_Ifaces (Iface_Def, Error_Node => Iface);
9052 if Is_Task and Is_Protected then
9054 ("type cannot derive from task and protected interface", N);
9056 end Check_Interfaces;
9058 ------------------------------------
9059 -- Check_Or_Process_Discriminants --
9060 ------------------------------------
9062 -- If an incomplete or private type declaration was already given for the
9063 -- type, the discriminants may have already been processed if they were
9064 -- present on the incomplete declaration. In this case a full conformance
9065 -- check is performed otherwise just process them.
9067 procedure Check_Or_Process_Discriminants
9070 Prev : Entity_Id := Empty)
9073 if Has_Discriminants (T) then
9075 -- Make the discriminants visible to component declarations
9082 D := First_Discriminant (T);
9083 while Present (D) loop
9084 Prev := Current_Entity (D);
9085 Set_Current_Entity (D);
9086 Set_Is_Immediately_Visible (D);
9087 Set_Homonym (D, Prev);
9089 -- Ada 2005 (AI-230): Access discriminant allowed in
9090 -- non-limited record types.
9092 if Ada_Version < Ada_05 then
9094 -- This restriction gets applied to the full type here. It
9095 -- has already been applied earlier to the partial view.
9097 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
9100 Next_Discriminant (D);
9104 elsif Present (Discriminant_Specifications (N)) then
9105 Process_Discriminants (N, Prev);
9107 end Check_Or_Process_Discriminants;
9109 ----------------------
9110 -- Check_Real_Bound --
9111 ----------------------
9113 procedure Check_Real_Bound (Bound : Node_Id) is
9115 if not Is_Real_Type (Etype (Bound)) then
9117 ("bound in real type definition must be of real type", Bound);
9119 elsif not Is_OK_Static_Expression (Bound) then
9120 Flag_Non_Static_Expr
9121 ("non-static expression used for real type bound!", Bound);
9128 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
9130 Resolve (Bound, Standard_Float);
9131 end Check_Real_Bound;
9133 ------------------------------
9134 -- Complete_Private_Subtype --
9135 ------------------------------
9137 procedure Complete_Private_Subtype
9140 Full_Base : Entity_Id;
9141 Related_Nod : Node_Id)
9143 Save_Next_Entity : Entity_Id;
9144 Save_Homonym : Entity_Id;
9147 -- Set semantic attributes for (implicit) private subtype completion.
9148 -- If the full type has no discriminants, then it is a copy of the full
9149 -- view of the base. Otherwise, it is a subtype of the base with a
9150 -- possible discriminant constraint. Save and restore the original
9151 -- Next_Entity field of full to ensure that the calls to Copy_Node
9152 -- do not corrupt the entity chain.
9154 -- Note that the type of the full view is the same entity as the type of
9155 -- the partial view. In this fashion, the subtype has access to the
9156 -- correct view of the parent.
9158 Save_Next_Entity := Next_Entity (Full);
9159 Save_Homonym := Homonym (Priv);
9161 case Ekind (Full_Base) is
9162 when E_Record_Type |
9168 Copy_Node (Priv, Full);
9170 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
9171 Set_First_Entity (Full, First_Entity (Full_Base));
9172 Set_Last_Entity (Full, Last_Entity (Full_Base));
9175 Copy_Node (Full_Base, Full);
9176 Set_Chars (Full, Chars (Priv));
9177 Conditional_Delay (Full, Priv);
9178 Set_Sloc (Full, Sloc (Priv));
9181 Set_Next_Entity (Full, Save_Next_Entity);
9182 Set_Homonym (Full, Save_Homonym);
9183 Set_Associated_Node_For_Itype (Full, Related_Nod);
9185 -- Set common attributes for all subtypes
9187 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
9189 -- The Etype of the full view is inconsistent. Gigi needs to see the
9190 -- structural full view, which is what the current scheme gives:
9191 -- the Etype of the full view is the etype of the full base. However,
9192 -- if the full base is a derived type, the full view then looks like
9193 -- a subtype of the parent, not a subtype of the full base. If instead
9196 -- Set_Etype (Full, Full_Base);
9198 -- then we get inconsistencies in the front-end (confusion between
9199 -- views). Several outstanding bugs are related to this ???
9201 Set_Is_First_Subtype (Full, False);
9202 Set_Scope (Full, Scope (Priv));
9203 Set_Size_Info (Full, Full_Base);
9204 Set_RM_Size (Full, RM_Size (Full_Base));
9205 Set_Is_Itype (Full);
9207 -- A subtype of a private-type-without-discriminants, whose full-view
9208 -- has discriminants with default expressions, is not constrained!
9210 if not Has_Discriminants (Priv) then
9211 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
9213 if Has_Discriminants (Full_Base) then
9214 Set_Discriminant_Constraint
9215 (Full, Discriminant_Constraint (Full_Base));
9217 -- The partial view may have been indefinite, the full view
9220 Set_Has_Unknown_Discriminants
9221 (Full, Has_Unknown_Discriminants (Full_Base));
9225 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
9226 Set_Depends_On_Private (Full, Has_Private_Component (Full));
9228 -- Freeze the private subtype entity if its parent is delayed, and not
9229 -- already frozen. We skip this processing if the type is an anonymous
9230 -- subtype of a record component, or is the corresponding record of a
9231 -- protected type, since ???
9233 if not Is_Type (Scope (Full)) then
9234 Set_Has_Delayed_Freeze (Full,
9235 Has_Delayed_Freeze (Full_Base)
9236 and then (not Is_Frozen (Full_Base)));
9239 Set_Freeze_Node (Full, Empty);
9240 Set_Is_Frozen (Full, False);
9241 Set_Full_View (Priv, Full);
9243 if Has_Discriminants (Full) then
9244 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
9245 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
9247 if Has_Unknown_Discriminants (Full) then
9248 Set_Discriminant_Constraint (Full, No_Elist);
9252 if Ekind (Full_Base) = E_Record_Type
9253 and then Has_Discriminants (Full_Base)
9254 and then Has_Discriminants (Priv) -- might not, if errors
9255 and then not Has_Unknown_Discriminants (Priv)
9256 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
9258 Create_Constrained_Components
9259 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
9261 -- If the full base is itself derived from private, build a congruent
9262 -- subtype of its underlying type, for use by the back end. For a
9263 -- constrained record component, the declaration cannot be placed on
9264 -- the component list, but it must nevertheless be built an analyzed, to
9265 -- supply enough information for Gigi to compute the size of component.
9267 elsif Ekind (Full_Base) in Private_Kind
9268 and then Is_Derived_Type (Full_Base)
9269 and then Has_Discriminants (Full_Base)
9270 and then (Ekind (Current_Scope) /= E_Record_Subtype)
9272 if not Is_Itype (Priv)
9274 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
9276 Build_Underlying_Full_View
9277 (Parent (Priv), Full, Etype (Full_Base));
9279 elsif Nkind (Related_Nod) = N_Component_Declaration then
9280 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
9283 elsif Is_Record_Type (Full_Base) then
9285 -- Show Full is simply a renaming of Full_Base
9287 Set_Cloned_Subtype (Full, Full_Base);
9290 -- It is unsafe to share to bounds of a scalar type, because the Itype
9291 -- is elaborated on demand, and if a bound is non-static then different
9292 -- orders of elaboration in different units will lead to different
9293 -- external symbols.
9295 if Is_Scalar_Type (Full_Base) then
9296 Set_Scalar_Range (Full,
9297 Make_Range (Sloc (Related_Nod),
9299 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
9301 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
9303 -- This completion inherits the bounds of the full parent, but if
9304 -- the parent is an unconstrained floating point type, so is the
9307 if Is_Floating_Point_Type (Full_Base) then
9308 Set_Includes_Infinities
9309 (Scalar_Range (Full), Has_Infinities (Full_Base));
9313 -- ??? It seems that a lot of fields are missing that should be copied
9314 -- from Full_Base to Full. Here are some that are introduced in a
9315 -- non-disruptive way but a cleanup is necessary.
9317 if Is_Tagged_Type (Full_Base) then
9318 Set_Is_Tagged_Type (Full);
9319 Set_Primitive_Operations (Full, Primitive_Operations (Full_Base));
9320 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
9322 -- If this is a subtype of a protected or task type, constrain its
9323 -- corresponding record, unless this is a subtype without constraints,
9324 -- i.e. a simple renaming as with an actual subtype in an instance.
9326 elsif Is_Concurrent_Type (Full_Base) then
9327 if Has_Discriminants (Full)
9328 and then Present (Corresponding_Record_Type (Full_Base))
9330 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
9332 Set_Corresponding_Record_Type (Full,
9333 Constrain_Corresponding_Record
9334 (Full, Corresponding_Record_Type (Full_Base),
9335 Related_Nod, Full_Base));
9338 Set_Corresponding_Record_Type (Full,
9339 Corresponding_Record_Type (Full_Base));
9342 end Complete_Private_Subtype;
9344 ----------------------------
9345 -- Constant_Redeclaration --
9346 ----------------------------
9348 procedure Constant_Redeclaration
9353 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
9354 Obj_Def : constant Node_Id := Object_Definition (N);
9357 procedure Check_Possible_Deferred_Completion
9358 (Prev_Id : Entity_Id;
9359 Prev_Obj_Def : Node_Id;
9360 Curr_Obj_Def : Node_Id);
9361 -- Determine whether the two object definitions describe the partial
9362 -- and the full view of a constrained deferred constant. Generate
9363 -- a subtype for the full view and verify that it statically matches
9364 -- the subtype of the partial view.
9366 procedure Check_Recursive_Declaration (Typ : Entity_Id);
9367 -- If deferred constant is an access type initialized with an allocator,
9368 -- check whether there is an illegal recursion in the definition,
9369 -- through a default value of some record subcomponent. This is normally
9370 -- detected when generating init procs, but requires this additional
9371 -- mechanism when expansion is disabled.
9373 ----------------------------------------
9374 -- Check_Possible_Deferred_Completion --
9375 ----------------------------------------
9377 procedure Check_Possible_Deferred_Completion
9378 (Prev_Id : Entity_Id;
9379 Prev_Obj_Def : Node_Id;
9380 Curr_Obj_Def : Node_Id)
9383 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
9384 and then Present (Constraint (Prev_Obj_Def))
9385 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
9386 and then Present (Constraint (Curr_Obj_Def))
9389 Loc : constant Source_Ptr := Sloc (N);
9390 Def_Id : constant Entity_Id :=
9391 Make_Defining_Identifier (Loc,
9392 New_Internal_Name ('S'));
9393 Decl : constant Node_Id :=
9394 Make_Subtype_Declaration (Loc,
9395 Defining_Identifier =>
9397 Subtype_Indication =>
9398 Relocate_Node (Curr_Obj_Def));
9401 Insert_Before_And_Analyze (N, Decl);
9402 Set_Etype (Id, Def_Id);
9404 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
9405 Error_Msg_Sloc := Sloc (Prev_Id);
9406 Error_Msg_N ("subtype does not statically match deferred " &
9411 end Check_Possible_Deferred_Completion;
9413 ---------------------------------
9414 -- Check_Recursive_Declaration --
9415 ---------------------------------
9417 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
9421 if Is_Record_Type (Typ) then
9422 Comp := First_Component (Typ);
9423 while Present (Comp) loop
9424 if Comes_From_Source (Comp) then
9425 if Present (Expression (Parent (Comp)))
9426 and then Is_Entity_Name (Expression (Parent (Comp)))
9427 and then Entity (Expression (Parent (Comp))) = Prev
9429 Error_Msg_Sloc := Sloc (Parent (Comp));
9431 ("illegal circularity with declaration for&#",
9435 elsif Is_Record_Type (Etype (Comp)) then
9436 Check_Recursive_Declaration (Etype (Comp));
9440 Next_Component (Comp);
9443 end Check_Recursive_Declaration;
9445 -- Start of processing for Constant_Redeclaration
9448 if Nkind (Parent (Prev)) = N_Object_Declaration then
9449 if Nkind (Object_Definition
9450 (Parent (Prev))) = N_Subtype_Indication
9452 -- Find type of new declaration. The constraints of the two
9453 -- views must match statically, but there is no point in
9454 -- creating an itype for the full view.
9456 if Nkind (Obj_Def) = N_Subtype_Indication then
9457 Find_Type (Subtype_Mark (Obj_Def));
9458 New_T := Entity (Subtype_Mark (Obj_Def));
9461 Find_Type (Obj_Def);
9462 New_T := Entity (Obj_Def);
9468 -- The full view may impose a constraint, even if the partial
9469 -- view does not, so construct the subtype.
9471 New_T := Find_Type_Of_Object (Obj_Def, N);
9476 -- Current declaration is illegal, diagnosed below in Enter_Name
9482 -- If previous full declaration exists, or if a homograph is present,
9483 -- let Enter_Name handle it, either with an error, or with the removal
9484 -- of an overridden implicit subprogram.
9486 if Ekind (Prev) /= E_Constant
9487 or else Present (Expression (Parent (Prev)))
9488 or else Present (Full_View (Prev))
9492 -- Verify that types of both declarations match, or else that both types
9493 -- are anonymous access types whose designated subtypes statically match
9494 -- (as allowed in Ada 2005 by AI-385).
9496 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
9498 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
9499 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
9500 or else Is_Access_Constant (Etype (New_T)) /=
9501 Is_Access_Constant (Etype (Prev))
9502 or else Can_Never_Be_Null (Etype (New_T)) /=
9503 Can_Never_Be_Null (Etype (Prev))
9504 or else Null_Exclusion_Present (Parent (Prev)) /=
9505 Null_Exclusion_Present (Parent (Id))
9506 or else not Subtypes_Statically_Match
9507 (Designated_Type (Etype (Prev)),
9508 Designated_Type (Etype (New_T))))
9510 Error_Msg_Sloc := Sloc (Prev);
9511 Error_Msg_N ("type does not match declaration#", N);
9512 Set_Full_View (Prev, Id);
9513 Set_Etype (Id, Any_Type);
9516 Null_Exclusion_Present (Parent (Prev))
9517 and then not Null_Exclusion_Present (N)
9519 Error_Msg_Sloc := Sloc (Prev);
9520 Error_Msg_N ("null-exclusion does not match declaration#", N);
9521 Set_Full_View (Prev, Id);
9522 Set_Etype (Id, Any_Type);
9524 -- If so, process the full constant declaration
9527 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
9528 -- the deferred declaration is constrained, then the subtype defined
9529 -- by the subtype_indication in the full declaration shall match it
9532 Check_Possible_Deferred_Completion
9534 Prev_Obj_Def => Object_Definition (Parent (Prev)),
9535 Curr_Obj_Def => Obj_Def);
9537 Set_Full_View (Prev, Id);
9538 Set_Is_Public (Id, Is_Public (Prev));
9539 Set_Is_Internal (Id);
9540 Append_Entity (Id, Current_Scope);
9542 -- Check ALIASED present if present before (RM 7.4(7))
9544 if Is_Aliased (Prev)
9545 and then not Aliased_Present (N)
9547 Error_Msg_Sloc := Sloc (Prev);
9548 Error_Msg_N ("ALIASED required (see declaration#)", N);
9551 -- Check that placement is in private part and that the incomplete
9552 -- declaration appeared in the visible part.
9554 if Ekind (Current_Scope) = E_Package
9555 and then not In_Private_Part (Current_Scope)
9557 Error_Msg_Sloc := Sloc (Prev);
9558 Error_Msg_N ("full constant for declaration#"
9559 & " must be in private part", N);
9561 elsif Ekind (Current_Scope) = E_Package
9562 and then List_Containing (Parent (Prev))
9563 /= Visible_Declarations
9564 (Specification (Unit_Declaration_Node (Current_Scope)))
9567 ("deferred constant must be declared in visible part",
9571 if Is_Access_Type (T)
9572 and then Nkind (Expression (N)) = N_Allocator
9574 Check_Recursive_Declaration (Designated_Type (T));
9577 end Constant_Redeclaration;
9579 ----------------------
9580 -- Constrain_Access --
9581 ----------------------
9583 procedure Constrain_Access
9584 (Def_Id : in out Entity_Id;
9586 Related_Nod : Node_Id)
9588 T : constant Entity_Id := Entity (Subtype_Mark (S));
9589 Desig_Type : constant Entity_Id := Designated_Type (T);
9590 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
9591 Constraint_OK : Boolean := True;
9593 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
9594 -- Simple predicate to test for defaulted discriminants
9595 -- Shouldn't this be in sem_util???
9597 ---------------------------------
9598 -- Has_Defaulted_Discriminants --
9599 ---------------------------------
9601 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
9603 return Has_Discriminants (Typ)
9604 and then Present (First_Discriminant (Typ))
9606 (Discriminant_Default_Value (First_Discriminant (Typ)));
9607 end Has_Defaulted_Discriminants;
9609 -- Start of processing for Constrain_Access
9612 if Is_Array_Type (Desig_Type) then
9613 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
9615 elsif (Is_Record_Type (Desig_Type)
9616 or else Is_Incomplete_Or_Private_Type (Desig_Type))
9617 and then not Is_Constrained (Desig_Type)
9619 -- ??? The following code is a temporary kludge to ignore a
9620 -- discriminant constraint on access type if it is constraining
9621 -- the current record. Avoid creating the implicit subtype of the
9622 -- record we are currently compiling since right now, we cannot
9623 -- handle these. For now, just return the access type itself.
9625 if Desig_Type = Current_Scope
9626 and then No (Def_Id)
9628 Set_Ekind (Desig_Subtype, E_Record_Subtype);
9629 Def_Id := Entity (Subtype_Mark (S));
9631 -- This call added to ensure that the constraint is analyzed
9632 -- (needed for a B test). Note that we still return early from
9633 -- this procedure to avoid recursive processing. ???
9635 Constrain_Discriminated_Type
9636 (Desig_Subtype, S, Related_Nod, For_Access => True);
9640 if (Ekind (T) = E_General_Access_Type
9641 or else Ada_Version >= Ada_05)
9642 and then Has_Private_Declaration (Desig_Type)
9643 and then In_Open_Scopes (Scope (Desig_Type))
9644 and then Has_Discriminants (Desig_Type)
9646 -- Enforce rule that the constraint is illegal if there is
9647 -- an unconstrained view of the designated type. This means
9648 -- that the partial view (either a private type declaration or
9649 -- a derivation from a private type) has no discriminants.
9650 -- (Defect Report 8652/0008, Technical Corrigendum 1, checked
9651 -- by ACATS B371001).
9653 -- Rule updated for Ada 2005: the private type is said to have
9654 -- a constrained partial view, given that objects of the type
9655 -- can be declared. Furthermore, the rule applies to all access
9656 -- types, unlike the rule concerning default discriminants.
9659 Pack : constant Node_Id :=
9660 Unit_Declaration_Node (Scope (Desig_Type));
9665 if Nkind (Pack) = N_Package_Declaration then
9666 Decls := Visible_Declarations (Specification (Pack));
9667 Decl := First (Decls);
9668 while Present (Decl) loop
9669 if (Nkind (Decl) = N_Private_Type_Declaration
9671 Chars (Defining_Identifier (Decl)) =
9675 (Nkind (Decl) = N_Full_Type_Declaration
9677 Chars (Defining_Identifier (Decl)) =
9679 and then Is_Derived_Type (Desig_Type)
9681 Has_Private_Declaration (Etype (Desig_Type)))
9683 if No (Discriminant_Specifications (Decl)) then
9685 ("cannot constrain general access type if " &
9686 "designated type has constrained partial view",
9699 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
9700 For_Access => True);
9702 elsif (Is_Task_Type (Desig_Type)
9703 or else Is_Protected_Type (Desig_Type))
9704 and then not Is_Constrained (Desig_Type)
9706 Constrain_Concurrent
9707 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
9710 Error_Msg_N ("invalid constraint on access type", S);
9711 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
9712 Constraint_OK := False;
9716 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
9718 Set_Ekind (Def_Id, E_Access_Subtype);
9721 if Constraint_OK then
9722 Set_Etype (Def_Id, Base_Type (T));
9724 if Is_Private_Type (Desig_Type) then
9725 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
9728 Set_Etype (Def_Id, Any_Type);
9731 Set_Size_Info (Def_Id, T);
9732 Set_Is_Constrained (Def_Id, Constraint_OK);
9733 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
9734 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
9735 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
9737 Conditional_Delay (Def_Id, T);
9739 -- AI-363 : Subtypes of general access types whose designated types have
9740 -- default discriminants are disallowed. In instances, the rule has to
9741 -- be checked against the actual, of which T is the subtype. In a
9742 -- generic body, the rule is checked assuming that the actual type has
9743 -- defaulted discriminants.
9745 if Ada_Version >= Ada_05 or else Warn_On_Ada_2005_Compatibility then
9746 if Ekind (Base_Type (T)) = E_General_Access_Type
9747 and then Has_Defaulted_Discriminants (Desig_Type)
9749 if Ada_Version < Ada_05 then
9751 ("access subtype of general access type would not " &
9752 "be allowed in Ada 2005?", S);
9755 ("access subype of general access type not allowed", S);
9758 Error_Msg_N ("\discriminants have defaults", S);
9760 elsif Is_Access_Type (T)
9761 and then Is_Generic_Type (Desig_Type)
9762 and then Has_Discriminants (Desig_Type)
9763 and then In_Package_Body (Current_Scope)
9765 if Ada_Version < Ada_05 then
9767 ("access subtype would not be allowed in generic body " &
9771 ("access subtype not allowed in generic body", S);
9775 ("\designated type is a discriminated formal", S);
9778 end Constrain_Access;
9780 ---------------------
9781 -- Constrain_Array --
9782 ---------------------
9784 procedure Constrain_Array
9785 (Def_Id : in out Entity_Id;
9787 Related_Nod : Node_Id;
9788 Related_Id : Entity_Id;
9791 C : constant Node_Id := Constraint (SI);
9792 Number_Of_Constraints : Nat := 0;
9795 Constraint_OK : Boolean := True;
9798 T := Entity (Subtype_Mark (SI));
9800 if Ekind (T) in Access_Kind then
9801 T := Designated_Type (T);
9804 -- If an index constraint follows a subtype mark in a subtype indication
9805 -- then the type or subtype denoted by the subtype mark must not already
9806 -- impose an index constraint. The subtype mark must denote either an
9807 -- unconstrained array type or an access type whose designated type
9808 -- is such an array type... (RM 3.6.1)
9810 if Is_Constrained (T) then
9812 ("array type is already constrained", Subtype_Mark (SI));
9813 Constraint_OK := False;
9816 S := First (Constraints (C));
9817 while Present (S) loop
9818 Number_Of_Constraints := Number_Of_Constraints + 1;
9822 -- In either case, the index constraint must provide a discrete
9823 -- range for each index of the array type and the type of each
9824 -- discrete range must be the same as that of the corresponding
9825 -- index. (RM 3.6.1)
9827 if Number_Of_Constraints /= Number_Dimensions (T) then
9828 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
9829 Constraint_OK := False;
9832 S := First (Constraints (C));
9833 Index := First_Index (T);
9836 -- Apply constraints to each index type
9838 for J in 1 .. Number_Of_Constraints loop
9839 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
9849 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
9850 Set_Parent (Def_Id, Related_Nod);
9853 Set_Ekind (Def_Id, E_Array_Subtype);
9856 Set_Size_Info (Def_Id, (T));
9857 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9858 Set_Etype (Def_Id, Base_Type (T));
9860 if Constraint_OK then
9861 Set_First_Index (Def_Id, First (Constraints (C)));
9863 Set_First_Index (Def_Id, First_Index (T));
9866 Set_Is_Constrained (Def_Id, True);
9867 Set_Is_Aliased (Def_Id, Is_Aliased (T));
9868 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
9870 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
9871 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
9873 -- A subtype does not inherit the packed_array_type of is parent. We
9874 -- need to initialize the attribute because if Def_Id is previously
9875 -- analyzed through a limited_with clause, it will have the attributes
9876 -- of an incomplete type, one of which is an Elist that overlaps the
9877 -- Packed_Array_Type field.
9879 Set_Packed_Array_Type (Def_Id, Empty);
9881 -- Build a freeze node if parent still needs one. Also make sure that
9882 -- the Depends_On_Private status is set because the subtype will need
9883 -- reprocessing at the time the base type does, and also we must set a
9884 -- conditional delay.
9886 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9887 Conditional_Delay (Def_Id, T);
9888 end Constrain_Array;
9890 ------------------------------
9891 -- Constrain_Component_Type --
9892 ------------------------------
9894 function Constrain_Component_Type
9896 Constrained_Typ : Entity_Id;
9897 Related_Node : Node_Id;
9899 Constraints : Elist_Id) return Entity_Id
9901 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
9902 Compon_Type : constant Entity_Id := Etype (Comp);
9904 function Build_Constrained_Array_Type
9905 (Old_Type : Entity_Id) return Entity_Id;
9906 -- If Old_Type is an array type, one of whose indices is constrained
9907 -- by a discriminant, build an Itype whose constraint replaces the
9908 -- discriminant with its value in the constraint.
9910 function Build_Constrained_Discriminated_Type
9911 (Old_Type : Entity_Id) return Entity_Id;
9912 -- Ditto for record components
9914 function Build_Constrained_Access_Type
9915 (Old_Type : Entity_Id) return Entity_Id;
9916 -- Ditto for access types. Makes use of previous two functions, to
9917 -- constrain designated type.
9919 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
9920 -- T is an array or discriminated type, C is a list of constraints
9921 -- that apply to T. This routine builds the constrained subtype.
9923 function Is_Discriminant (Expr : Node_Id) return Boolean;
9924 -- Returns True if Expr is a discriminant
9926 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
9927 -- Find the value of discriminant Discrim in Constraint
9929 -----------------------------------
9930 -- Build_Constrained_Access_Type --
9931 -----------------------------------
9933 function Build_Constrained_Access_Type
9934 (Old_Type : Entity_Id) return Entity_Id
9936 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
9938 Desig_Subtype : Entity_Id;
9942 -- if the original access type was not embedded in the enclosing
9943 -- type definition, there is no need to produce a new access
9944 -- subtype. In fact every access type with an explicit constraint
9945 -- generates an itype whose scope is the enclosing record.
9947 if not Is_Type (Scope (Old_Type)) then
9950 elsif Is_Array_Type (Desig_Type) then
9951 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
9953 elsif Has_Discriminants (Desig_Type) then
9955 -- This may be an access type to an enclosing record type for
9956 -- which we are constructing the constrained components. Return
9957 -- the enclosing record subtype. This is not always correct,
9958 -- but avoids infinite recursion. ???
9960 Desig_Subtype := Any_Type;
9962 for J in reverse 0 .. Scope_Stack.Last loop
9963 Scop := Scope_Stack.Table (J).Entity;
9966 and then Base_Type (Scop) = Base_Type (Desig_Type)
9968 Desig_Subtype := Scop;
9971 exit when not Is_Type (Scop);
9974 if Desig_Subtype = Any_Type then
9976 Build_Constrained_Discriminated_Type (Desig_Type);
9983 if Desig_Subtype /= Desig_Type then
9985 -- The Related_Node better be here or else we won't be able
9986 -- to attach new itypes to a node in the tree.
9988 pragma Assert (Present (Related_Node));
9990 Itype := Create_Itype (E_Access_Subtype, Related_Node);
9992 Set_Etype (Itype, Base_Type (Old_Type));
9993 Set_Size_Info (Itype, (Old_Type));
9994 Set_Directly_Designated_Type (Itype, Desig_Subtype);
9995 Set_Depends_On_Private (Itype, Has_Private_Component
9997 Set_Is_Access_Constant (Itype, Is_Access_Constant
10000 -- The new itype needs freezing when it depends on a not frozen
10001 -- type and the enclosing subtype needs freezing.
10003 if Has_Delayed_Freeze (Constrained_Typ)
10004 and then not Is_Frozen (Constrained_Typ)
10006 Conditional_Delay (Itype, Base_Type (Old_Type));
10014 end Build_Constrained_Access_Type;
10016 ----------------------------------
10017 -- Build_Constrained_Array_Type --
10018 ----------------------------------
10020 function Build_Constrained_Array_Type
10021 (Old_Type : Entity_Id) return Entity_Id
10025 Old_Index : Node_Id;
10026 Range_Node : Node_Id;
10027 Constr_List : List_Id;
10029 Need_To_Create_Itype : Boolean := False;
10032 Old_Index := First_Index (Old_Type);
10033 while Present (Old_Index) loop
10034 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10036 if Is_Discriminant (Lo_Expr)
10037 or else Is_Discriminant (Hi_Expr)
10039 Need_To_Create_Itype := True;
10042 Next_Index (Old_Index);
10045 if Need_To_Create_Itype then
10046 Constr_List := New_List;
10048 Old_Index := First_Index (Old_Type);
10049 while Present (Old_Index) loop
10050 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10052 if Is_Discriminant (Lo_Expr) then
10053 Lo_Expr := Get_Discr_Value (Lo_Expr);
10056 if Is_Discriminant (Hi_Expr) then
10057 Hi_Expr := Get_Discr_Value (Hi_Expr);
10062 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
10064 Append (Range_Node, To => Constr_List);
10066 Next_Index (Old_Index);
10069 return Build_Subtype (Old_Type, Constr_List);
10074 end Build_Constrained_Array_Type;
10076 ------------------------------------------
10077 -- Build_Constrained_Discriminated_Type --
10078 ------------------------------------------
10080 function Build_Constrained_Discriminated_Type
10081 (Old_Type : Entity_Id) return Entity_Id
10084 Constr_List : List_Id;
10085 Old_Constraint : Elmt_Id;
10087 Need_To_Create_Itype : Boolean := False;
10090 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10091 while Present (Old_Constraint) loop
10092 Expr := Node (Old_Constraint);
10094 if Is_Discriminant (Expr) then
10095 Need_To_Create_Itype := True;
10098 Next_Elmt (Old_Constraint);
10101 if Need_To_Create_Itype then
10102 Constr_List := New_List;
10104 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10105 while Present (Old_Constraint) loop
10106 Expr := Node (Old_Constraint);
10108 if Is_Discriminant (Expr) then
10109 Expr := Get_Discr_Value (Expr);
10112 Append (New_Copy_Tree (Expr), To => Constr_List);
10114 Next_Elmt (Old_Constraint);
10117 return Build_Subtype (Old_Type, Constr_List);
10122 end Build_Constrained_Discriminated_Type;
10124 -------------------
10125 -- Build_Subtype --
10126 -------------------
10128 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
10130 Subtyp_Decl : Node_Id;
10131 Def_Id : Entity_Id;
10132 Btyp : Entity_Id := Base_Type (T);
10135 -- The Related_Node better be here or else we won't be able to
10136 -- attach new itypes to a node in the tree.
10138 pragma Assert (Present (Related_Node));
10140 -- If the view of the component's type is incomplete or private
10141 -- with unknown discriminants, then the constraint must be applied
10142 -- to the full type.
10144 if Has_Unknown_Discriminants (Btyp)
10145 and then Present (Underlying_Type (Btyp))
10147 Btyp := Underlying_Type (Btyp);
10151 Make_Subtype_Indication (Loc,
10152 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
10153 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
10155 Def_Id := Create_Itype (Ekind (T), Related_Node);
10158 Make_Subtype_Declaration (Loc,
10159 Defining_Identifier => Def_Id,
10160 Subtype_Indication => Indic);
10162 Set_Parent (Subtyp_Decl, Parent (Related_Node));
10164 -- Itypes must be analyzed with checks off (see package Itypes)
10166 Analyze (Subtyp_Decl, Suppress => All_Checks);
10171 ---------------------
10172 -- Get_Discr_Value --
10173 ---------------------
10175 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
10180 -- The discriminant may be declared for the type, in which case we
10181 -- find it by iterating over the list of discriminants. If the
10182 -- discriminant is inherited from a parent type, it appears as the
10183 -- corresponding discriminant of the current type. This will be the
10184 -- case when constraining an inherited component whose constraint is
10185 -- given by a discriminant of the parent.
10187 D := First_Discriminant (Typ);
10188 E := First_Elmt (Constraints);
10190 while Present (D) loop
10191 if D = Entity (Discrim)
10192 or else D = CR_Discriminant (Entity (Discrim))
10193 or else Corresponding_Discriminant (D) = Entity (Discrim)
10198 Next_Discriminant (D);
10202 -- The corresponding_Discriminant mechanism is incomplete, because
10203 -- the correspondence between new and old discriminants is not one
10204 -- to one: one new discriminant can constrain several old ones. In
10205 -- that case, scan sequentially the stored_constraint, the list of
10206 -- discriminants of the parents, and the constraints.
10207 -- Previous code checked for the present of the Stored_Constraint
10208 -- list for the derived type, but did not use it at all. Should it
10209 -- be present when the component is a discriminated task type?
10211 if Is_Derived_Type (Typ)
10212 and then Scope (Entity (Discrim)) = Etype (Typ)
10214 D := First_Discriminant (Etype (Typ));
10215 E := First_Elmt (Constraints);
10216 while Present (D) loop
10217 if D = Entity (Discrim) then
10221 Next_Discriminant (D);
10226 -- Something is wrong if we did not find the value
10228 raise Program_Error;
10229 end Get_Discr_Value;
10231 ---------------------
10232 -- Is_Discriminant --
10233 ---------------------
10235 function Is_Discriminant (Expr : Node_Id) return Boolean is
10236 Discrim_Scope : Entity_Id;
10239 if Denotes_Discriminant (Expr) then
10240 Discrim_Scope := Scope (Entity (Expr));
10242 -- Either we have a reference to one of Typ's discriminants,
10244 pragma Assert (Discrim_Scope = Typ
10246 -- or to the discriminants of the parent type, in the case
10247 -- of a derivation of a tagged type with variants.
10249 or else Discrim_Scope = Etype (Typ)
10250 or else Full_View (Discrim_Scope) = Etype (Typ)
10252 -- or same as above for the case where the discriminants
10253 -- were declared in Typ's private view.
10255 or else (Is_Private_Type (Discrim_Scope)
10256 and then Chars (Discrim_Scope) = Chars (Typ))
10258 -- or else we are deriving from the full view and the
10259 -- discriminant is declared in the private entity.
10261 or else (Is_Private_Type (Typ)
10262 and then Chars (Discrim_Scope) = Chars (Typ))
10264 -- Or we are constrained the corresponding record of a
10265 -- synchronized type that completes a private declaration.
10267 or else (Is_Concurrent_Record_Type (Typ)
10269 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
10271 -- or we have a class-wide type, in which case make sure the
10272 -- discriminant found belongs to the root type.
10274 or else (Is_Class_Wide_Type (Typ)
10275 and then Etype (Typ) = Discrim_Scope));
10280 -- In all other cases we have something wrong
10283 end Is_Discriminant;
10285 -- Start of processing for Constrain_Component_Type
10288 if Nkind (Parent (Comp)) = N_Component_Declaration
10289 and then Comes_From_Source (Parent (Comp))
10290 and then Comes_From_Source
10291 (Subtype_Indication (Component_Definition (Parent (Comp))))
10294 (Subtype_Indication (Component_Definition (Parent (Comp))))
10296 return Compon_Type;
10298 elsif Is_Array_Type (Compon_Type) then
10299 return Build_Constrained_Array_Type (Compon_Type);
10301 elsif Has_Discriminants (Compon_Type) then
10302 return Build_Constrained_Discriminated_Type (Compon_Type);
10304 elsif Is_Access_Type (Compon_Type) then
10305 return Build_Constrained_Access_Type (Compon_Type);
10308 return Compon_Type;
10310 end Constrain_Component_Type;
10312 --------------------------
10313 -- Constrain_Concurrent --
10314 --------------------------
10316 -- For concurrent types, the associated record value type carries the same
10317 -- discriminants, so when we constrain a concurrent type, we must constrain
10318 -- the corresponding record type as well.
10320 procedure Constrain_Concurrent
10321 (Def_Id : in out Entity_Id;
10323 Related_Nod : Node_Id;
10324 Related_Id : Entity_Id;
10325 Suffix : Character)
10327 T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
10331 if Ekind (T_Ent) in Access_Kind then
10332 T_Ent := Designated_Type (T_Ent);
10335 T_Val := Corresponding_Record_Type (T_Ent);
10337 if Present (T_Val) then
10339 if No (Def_Id) then
10340 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10343 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10345 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10346 Set_Corresponding_Record_Type (Def_Id,
10347 Constrain_Corresponding_Record
10348 (Def_Id, T_Val, Related_Nod, Related_Id));
10351 -- If there is no associated record, expansion is disabled and this
10352 -- is a generic context. Create a subtype in any case, so that
10353 -- semantic analysis can proceed.
10355 if No (Def_Id) then
10356 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
10359 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
10361 end Constrain_Concurrent;
10363 ------------------------------------
10364 -- Constrain_Corresponding_Record --
10365 ------------------------------------
10367 function Constrain_Corresponding_Record
10368 (Prot_Subt : Entity_Id;
10369 Corr_Rec : Entity_Id;
10370 Related_Nod : Node_Id;
10371 Related_Id : Entity_Id) return Entity_Id
10373 T_Sub : constant Entity_Id :=
10374 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
10377 Set_Etype (T_Sub, Corr_Rec);
10378 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
10379 Set_Is_Constrained (T_Sub, True);
10380 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
10381 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
10383 -- As elsewhere, we do not want to create a freeze node for this itype
10384 -- if it is created for a constrained component of an enclosing record
10385 -- because references to outer discriminants will appear out of scope.
10387 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
10388 Conditional_Delay (T_Sub, Corr_Rec);
10390 Set_Is_Frozen (T_Sub);
10393 if Has_Discriminants (Prot_Subt) then -- False only if errors.
10394 Set_Discriminant_Constraint
10395 (T_Sub, Discriminant_Constraint (Prot_Subt));
10396 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
10397 Create_Constrained_Components
10398 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
10401 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
10404 end Constrain_Corresponding_Record;
10406 -----------------------
10407 -- Constrain_Decimal --
10408 -----------------------
10410 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
10411 T : constant Entity_Id := Entity (Subtype_Mark (S));
10412 C : constant Node_Id := Constraint (S);
10413 Loc : constant Source_Ptr := Sloc (C);
10414 Range_Expr : Node_Id;
10415 Digits_Expr : Node_Id;
10420 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
10422 if Nkind (C) = N_Range_Constraint then
10423 Range_Expr := Range_Expression (C);
10424 Digits_Val := Digits_Value (T);
10427 pragma Assert (Nkind (C) = N_Digits_Constraint);
10428 Digits_Expr := Digits_Expression (C);
10429 Analyze_And_Resolve (Digits_Expr, Any_Integer);
10431 Check_Digits_Expression (Digits_Expr);
10432 Digits_Val := Expr_Value (Digits_Expr);
10434 if Digits_Val > Digits_Value (T) then
10436 ("digits expression is incompatible with subtype", C);
10437 Digits_Val := Digits_Value (T);
10440 if Present (Range_Constraint (C)) then
10441 Range_Expr := Range_Expression (Range_Constraint (C));
10443 Range_Expr := Empty;
10447 Set_Etype (Def_Id, Base_Type (T));
10448 Set_Size_Info (Def_Id, (T));
10449 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10450 Set_Delta_Value (Def_Id, Delta_Value (T));
10451 Set_Scale_Value (Def_Id, Scale_Value (T));
10452 Set_Small_Value (Def_Id, Small_Value (T));
10453 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
10454 Set_Digits_Value (Def_Id, Digits_Val);
10456 -- Manufacture range from given digits value if no range present
10458 if No (Range_Expr) then
10459 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
10463 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
10465 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
10468 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
10469 Set_Discrete_RM_Size (Def_Id);
10471 -- Unconditionally delay the freeze, since we cannot set size
10472 -- information in all cases correctly until the freeze point.
10474 Set_Has_Delayed_Freeze (Def_Id);
10475 end Constrain_Decimal;
10477 ----------------------------------
10478 -- Constrain_Discriminated_Type --
10479 ----------------------------------
10481 procedure Constrain_Discriminated_Type
10482 (Def_Id : Entity_Id;
10484 Related_Nod : Node_Id;
10485 For_Access : Boolean := False)
10487 E : constant Entity_Id := Entity (Subtype_Mark (S));
10490 Elist : Elist_Id := New_Elmt_List;
10492 procedure Fixup_Bad_Constraint;
10493 -- This is called after finding a bad constraint, and after having
10494 -- posted an appropriate error message. The mission is to leave the
10495 -- entity T in as reasonable state as possible!
10497 --------------------------
10498 -- Fixup_Bad_Constraint --
10499 --------------------------
10501 procedure Fixup_Bad_Constraint is
10503 -- Set a reasonable Ekind for the entity. For an incomplete type,
10504 -- we can't do much, but for other types, we can set the proper
10505 -- corresponding subtype kind.
10507 if Ekind (T) = E_Incomplete_Type then
10508 Set_Ekind (Def_Id, Ekind (T));
10510 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10513 -- Set Etype to the known type, to reduce chances of cascaded errors
10515 Set_Etype (Def_Id, E);
10516 Set_Error_Posted (Def_Id);
10517 end Fixup_Bad_Constraint;
10519 -- Start of processing for Constrain_Discriminated_Type
10522 C := Constraint (S);
10524 -- A discriminant constraint is only allowed in a subtype indication,
10525 -- after a subtype mark. This subtype mark must denote either a type
10526 -- with discriminants, or an access type whose designated type is a
10527 -- type with discriminants. A discriminant constraint specifies the
10528 -- values of these discriminants (RM 3.7.2(5)).
10530 T := Base_Type (Entity (Subtype_Mark (S)));
10532 if Ekind (T) in Access_Kind then
10533 T := Designated_Type (T);
10536 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
10537 -- Avoid generating an error for access-to-incomplete subtypes.
10539 if Ada_Version >= Ada_05
10540 and then Ekind (T) = E_Incomplete_Type
10541 and then Nkind (Parent (S)) = N_Subtype_Declaration
10542 and then not Is_Itype (Def_Id)
10544 -- A little sanity check, emit an error message if the type
10545 -- has discriminants to begin with. Type T may be a regular
10546 -- incomplete type or imported via a limited with clause.
10548 if Has_Discriminants (T)
10550 (From_With_Type (T)
10551 and then Present (Non_Limited_View (T))
10552 and then Nkind (Parent (Non_Limited_View (T))) =
10553 N_Full_Type_Declaration
10554 and then Present (Discriminant_Specifications
10555 (Parent (Non_Limited_View (T)))))
10558 ("(Ada 2005) incomplete subtype may not be constrained", C);
10561 ("invalid constraint: type has no discriminant", C);
10564 Fixup_Bad_Constraint;
10567 -- Check that the type has visible discriminants. The type may be
10568 -- a private type with unknown discriminants whose full view has
10569 -- discriminants which are invisible.
10571 elsif not Has_Discriminants (T)
10573 (Has_Unknown_Discriminants (T)
10574 and then Is_Private_Type (T))
10576 Error_Msg_N ("invalid constraint: type has no discriminant", C);
10577 Fixup_Bad_Constraint;
10580 elsif Is_Constrained (E)
10581 or else (Ekind (E) = E_Class_Wide_Subtype
10582 and then Present (Discriminant_Constraint (E)))
10584 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
10585 Fixup_Bad_Constraint;
10589 -- T may be an unconstrained subtype (e.g. a generic actual).
10590 -- Constraint applies to the base type.
10592 T := Base_Type (T);
10594 Elist := Build_Discriminant_Constraints (T, S);
10596 -- If the list returned was empty we had an error in building the
10597 -- discriminant constraint. We have also already signalled an error
10598 -- in the incomplete type case
10600 if Is_Empty_Elmt_List (Elist) then
10601 Fixup_Bad_Constraint;
10605 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
10606 end Constrain_Discriminated_Type;
10608 ---------------------------
10609 -- Constrain_Enumeration --
10610 ---------------------------
10612 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
10613 T : constant Entity_Id := Entity (Subtype_Mark (S));
10614 C : constant Node_Id := Constraint (S);
10617 Set_Ekind (Def_Id, E_Enumeration_Subtype);
10619 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
10621 Set_Etype (Def_Id, Base_Type (T));
10622 Set_Size_Info (Def_Id, (T));
10623 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10624 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10626 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10628 Set_Discrete_RM_Size (Def_Id);
10629 end Constrain_Enumeration;
10631 ----------------------
10632 -- Constrain_Float --
10633 ----------------------
10635 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
10636 T : constant Entity_Id := Entity (Subtype_Mark (S));
10642 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
10644 Set_Etype (Def_Id, Base_Type (T));
10645 Set_Size_Info (Def_Id, (T));
10646 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10648 -- Process the constraint
10650 C := Constraint (S);
10652 -- Digits constraint present
10654 if Nkind (C) = N_Digits_Constraint then
10655 Check_Restriction (No_Obsolescent_Features, C);
10657 if Warn_On_Obsolescent_Feature then
10659 ("subtype digits constraint is an " &
10660 "obsolescent feature (RM J.3(8))?", C);
10663 D := Digits_Expression (C);
10664 Analyze_And_Resolve (D, Any_Integer);
10665 Check_Digits_Expression (D);
10666 Set_Digits_Value (Def_Id, Expr_Value (D));
10668 -- Check that digits value is in range. Obviously we can do this
10669 -- at compile time, but it is strictly a runtime check, and of
10670 -- course there is an ACVC test that checks this!
10672 if Digits_Value (Def_Id) > Digits_Value (T) then
10673 Error_Msg_Uint_1 := Digits_Value (T);
10674 Error_Msg_N ("?digits value is too large, maximum is ^", D);
10676 Make_Raise_Constraint_Error (Sloc (D),
10677 Reason => CE_Range_Check_Failed);
10678 Insert_Action (Declaration_Node (Def_Id), Rais);
10681 C := Range_Constraint (C);
10683 -- No digits constraint present
10686 Set_Digits_Value (Def_Id, Digits_Value (T));
10689 -- Range constraint present
10691 if Nkind (C) = N_Range_Constraint then
10692 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10694 -- No range constraint present
10697 pragma Assert (No (C));
10698 Set_Scalar_Range (Def_Id, Scalar_Range (T));
10701 Set_Is_Constrained (Def_Id);
10702 end Constrain_Float;
10704 ---------------------
10705 -- Constrain_Index --
10706 ---------------------
10708 procedure Constrain_Index
10711 Related_Nod : Node_Id;
10712 Related_Id : Entity_Id;
10713 Suffix : Character;
10714 Suffix_Index : Nat)
10716 Def_Id : Entity_Id;
10717 R : Node_Id := Empty;
10718 T : constant Entity_Id := Etype (Index);
10721 if Nkind (S) = N_Range
10723 (Nkind (S) = N_Attribute_Reference
10724 and then Attribute_Name (S) = Name_Range)
10726 -- A Range attribute will transformed into N_Range by Resolve
10732 Process_Range_Expr_In_Decl (R, T, Empty_List);
10734 if not Error_Posted (S)
10736 (Nkind (S) /= N_Range
10737 or else not Covers (T, (Etype (Low_Bound (S))))
10738 or else not Covers (T, (Etype (High_Bound (S)))))
10740 if Base_Type (T) /= Any_Type
10741 and then Etype (Low_Bound (S)) /= Any_Type
10742 and then Etype (High_Bound (S)) /= Any_Type
10744 Error_Msg_N ("range expected", S);
10748 elsif Nkind (S) = N_Subtype_Indication then
10750 -- The parser has verified that this is a discrete indication
10752 Resolve_Discrete_Subtype_Indication (S, T);
10753 R := Range_Expression (Constraint (S));
10755 elsif Nkind (S) = N_Discriminant_Association then
10757 -- Syntactically valid in subtype indication
10759 Error_Msg_N ("invalid index constraint", S);
10760 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10763 -- Subtype_Mark case, no anonymous subtypes to construct
10768 if Is_Entity_Name (S) then
10769 if not Is_Type (Entity (S)) then
10770 Error_Msg_N ("expect subtype mark for index constraint", S);
10772 elsif Base_Type (Entity (S)) /= Base_Type (T) then
10773 Wrong_Type (S, Base_Type (T));
10779 Error_Msg_N ("invalid index constraint", S);
10780 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
10786 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
10788 Set_Etype (Def_Id, Base_Type (T));
10790 if Is_Modular_Integer_Type (T) then
10791 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10793 elsif Is_Integer_Type (T) then
10794 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10797 Set_Ekind (Def_Id, E_Enumeration_Subtype);
10798 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
10801 Set_Size_Info (Def_Id, (T));
10802 Set_RM_Size (Def_Id, RM_Size (T));
10803 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10805 Set_Scalar_Range (Def_Id, R);
10807 Set_Etype (S, Def_Id);
10808 Set_Discrete_RM_Size (Def_Id);
10809 end Constrain_Index;
10811 -----------------------
10812 -- Constrain_Integer --
10813 -----------------------
10815 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
10816 T : constant Entity_Id := Entity (Subtype_Mark (S));
10817 C : constant Node_Id := Constraint (S);
10820 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10822 if Is_Modular_Integer_Type (T) then
10823 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
10825 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
10828 Set_Etype (Def_Id, Base_Type (T));
10829 Set_Size_Info (Def_Id, (T));
10830 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10831 Set_Discrete_RM_Size (Def_Id);
10832 end Constrain_Integer;
10834 ------------------------------
10835 -- Constrain_Ordinary_Fixed --
10836 ------------------------------
10838 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
10839 T : constant Entity_Id := Entity (Subtype_Mark (S));
10845 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
10846 Set_Etype (Def_Id, Base_Type (T));
10847 Set_Size_Info (Def_Id, (T));
10848 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10849 Set_Small_Value (Def_Id, Small_Value (T));
10851 -- Process the constraint
10853 C := Constraint (S);
10855 -- Delta constraint present
10857 if Nkind (C) = N_Delta_Constraint then
10858 Check_Restriction (No_Obsolescent_Features, C);
10860 if Warn_On_Obsolescent_Feature then
10862 ("subtype delta constraint is an " &
10863 "obsolescent feature (RM J.3(7))?");
10866 D := Delta_Expression (C);
10867 Analyze_And_Resolve (D, Any_Real);
10868 Check_Delta_Expression (D);
10869 Set_Delta_Value (Def_Id, Expr_Value_R (D));
10871 -- Check that delta value is in range. Obviously we can do this
10872 -- at compile time, but it is strictly a runtime check, and of
10873 -- course there is an ACVC test that checks this!
10875 if Delta_Value (Def_Id) < Delta_Value (T) then
10876 Error_Msg_N ("?delta value is too small", D);
10878 Make_Raise_Constraint_Error (Sloc (D),
10879 Reason => CE_Range_Check_Failed);
10880 Insert_Action (Declaration_Node (Def_Id), Rais);
10883 C := Range_Constraint (C);
10885 -- No delta constraint present
10888 Set_Delta_Value (Def_Id, Delta_Value (T));
10891 -- Range constraint present
10893 if Nkind (C) = N_Range_Constraint then
10894 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
10896 -- No range constraint present
10899 pragma Assert (No (C));
10900 Set_Scalar_Range (Def_Id, Scalar_Range (T));
10904 Set_Discrete_RM_Size (Def_Id);
10906 -- Unconditionally delay the freeze, since we cannot set size
10907 -- information in all cases correctly until the freeze point.
10909 Set_Has_Delayed_Freeze (Def_Id);
10910 end Constrain_Ordinary_Fixed;
10912 -----------------------
10913 -- Contain_Interface --
10914 -----------------------
10916 function Contain_Interface
10917 (Iface : Entity_Id;
10918 Ifaces : Elist_Id) return Boolean
10920 Iface_Elmt : Elmt_Id;
10923 if Present (Ifaces) then
10924 Iface_Elmt := First_Elmt (Ifaces);
10925 while Present (Iface_Elmt) loop
10926 if Node (Iface_Elmt) = Iface then
10930 Next_Elmt (Iface_Elmt);
10935 end Contain_Interface;
10937 ---------------------------
10938 -- Convert_Scalar_Bounds --
10939 ---------------------------
10941 procedure Convert_Scalar_Bounds
10943 Parent_Type : Entity_Id;
10944 Derived_Type : Entity_Id;
10947 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
10954 Lo := Build_Scalar_Bound
10955 (Type_Low_Bound (Derived_Type),
10956 Parent_Type, Implicit_Base);
10958 Hi := Build_Scalar_Bound
10959 (Type_High_Bound (Derived_Type),
10960 Parent_Type, Implicit_Base);
10967 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
10969 Set_Parent (Rng, N);
10970 Set_Scalar_Range (Derived_Type, Rng);
10972 -- Analyze the bounds
10974 Analyze_And_Resolve (Lo, Implicit_Base);
10975 Analyze_And_Resolve (Hi, Implicit_Base);
10977 -- Analyze the range itself, except that we do not analyze it if
10978 -- the bounds are real literals, and we have a fixed-point type.
10979 -- The reason for this is that we delay setting the bounds in this
10980 -- case till we know the final Small and Size values (see circuit
10981 -- in Freeze.Freeze_Fixed_Point_Type for further details).
10983 if Is_Fixed_Point_Type (Parent_Type)
10984 and then Nkind (Lo) = N_Real_Literal
10985 and then Nkind (Hi) = N_Real_Literal
10989 -- Here we do the analysis of the range
10991 -- Note: we do this manually, since if we do a normal Analyze and
10992 -- Resolve call, there are problems with the conversions used for
10993 -- the derived type range.
10996 Set_Etype (Rng, Implicit_Base);
10997 Set_Analyzed (Rng, True);
10999 end Convert_Scalar_Bounds;
11001 -------------------
11002 -- Copy_And_Swap --
11003 -------------------
11005 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
11007 -- Initialize new full declaration entity by copying the pertinent
11008 -- fields of the corresponding private declaration entity.
11010 -- We temporarily set Ekind to a value appropriate for a type to
11011 -- avoid assert failures in Einfo from checking for setting type
11012 -- attributes on something that is not a type. Ekind (Priv) is an
11013 -- appropriate choice, since it allowed the attributes to be set
11014 -- in the first place. This Ekind value will be modified later.
11016 Set_Ekind (Full, Ekind (Priv));
11018 -- Also set Etype temporarily to Any_Type, again, in the absence
11019 -- of errors, it will be properly reset, and if there are errors,
11020 -- then we want a value of Any_Type to remain.
11022 Set_Etype (Full, Any_Type);
11024 -- Now start copying attributes
11026 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
11028 if Has_Discriminants (Full) then
11029 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
11030 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
11033 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11034 Set_Homonym (Full, Homonym (Priv));
11035 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
11036 Set_Is_Public (Full, Is_Public (Priv));
11037 Set_Is_Pure (Full, Is_Pure (Priv));
11038 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
11039 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
11040 Set_Has_Pragma_Unreferenced_Objects
11041 (Full, Has_Pragma_Unreferenced_Objects
11044 Conditional_Delay (Full, Priv);
11046 if Is_Tagged_Type (Full) then
11047 Set_Primitive_Operations (Full, Primitive_Operations (Priv));
11049 if Priv = Base_Type (Priv) then
11050 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
11054 Set_Is_Volatile (Full, Is_Volatile (Priv));
11055 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
11056 Set_Scope (Full, Scope (Priv));
11057 Set_Next_Entity (Full, Next_Entity (Priv));
11058 Set_First_Entity (Full, First_Entity (Priv));
11059 Set_Last_Entity (Full, Last_Entity (Priv));
11061 -- If access types have been recorded for later handling, keep them in
11062 -- the full view so that they get handled when the full view freeze
11063 -- node is expanded.
11065 if Present (Freeze_Node (Priv))
11066 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
11068 Ensure_Freeze_Node (Full);
11069 Set_Access_Types_To_Process
11070 (Freeze_Node (Full),
11071 Access_Types_To_Process (Freeze_Node (Priv)));
11074 -- Swap the two entities. Now Privat is the full type entity and
11075 -- Full is the private one. They will be swapped back at the end
11076 -- of the private part. This swapping ensures that the entity that
11077 -- is visible in the private part is the full declaration.
11079 Exchange_Entities (Priv, Full);
11080 Append_Entity (Full, Scope (Full));
11083 -------------------------------------
11084 -- Copy_Array_Base_Type_Attributes --
11085 -------------------------------------
11087 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
11089 Set_Component_Alignment (T1, Component_Alignment (T2));
11090 Set_Component_Type (T1, Component_Type (T2));
11091 Set_Component_Size (T1, Component_Size (T2));
11092 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
11093 Set_Finalize_Storage_Only (T1, Finalize_Storage_Only (T2));
11094 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
11095 Set_Has_Task (T1, Has_Task (T2));
11096 Set_Is_Packed (T1, Is_Packed (T2));
11097 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
11098 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
11099 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
11100 end Copy_Array_Base_Type_Attributes;
11102 -----------------------------------
11103 -- Copy_Array_Subtype_Attributes --
11104 -----------------------------------
11106 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
11108 Set_Size_Info (T1, T2);
11110 Set_First_Index (T1, First_Index (T2));
11111 Set_Is_Aliased (T1, Is_Aliased (T2));
11112 Set_Is_Atomic (T1, Is_Atomic (T2));
11113 Set_Is_Volatile (T1, Is_Volatile (T2));
11114 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
11115 Set_Is_Constrained (T1, Is_Constrained (T2));
11116 Set_Depends_On_Private (T1, Has_Private_Component (T2));
11117 Set_First_Rep_Item (T1, First_Rep_Item (T2));
11118 Set_Convention (T1, Convention (T2));
11119 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
11120 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
11121 end Copy_Array_Subtype_Attributes;
11123 -----------------------------------
11124 -- Create_Constrained_Components --
11125 -----------------------------------
11127 procedure Create_Constrained_Components
11129 Decl_Node : Node_Id;
11131 Constraints : Elist_Id)
11133 Loc : constant Source_Ptr := Sloc (Subt);
11134 Comp_List : constant Elist_Id := New_Elmt_List;
11135 Parent_Type : constant Entity_Id := Etype (Typ);
11136 Assoc_List : constant List_Id := New_List;
11137 Discr_Val : Elmt_Id;
11141 Is_Static : Boolean := True;
11143 procedure Collect_Fixed_Components (Typ : Entity_Id);
11144 -- Collect parent type components that do not appear in a variant part
11146 procedure Create_All_Components;
11147 -- Iterate over Comp_List to create the components of the subtype
11149 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
11150 -- Creates a new component from Old_Compon, copying all the fields from
11151 -- it, including its Etype, inserts the new component in the Subt entity
11152 -- chain and returns the new component.
11154 function Is_Variant_Record (T : Entity_Id) return Boolean;
11155 -- If true, and discriminants are static, collect only components from
11156 -- variants selected by discriminant values.
11158 ------------------------------
11159 -- Collect_Fixed_Components --
11160 ------------------------------
11162 procedure Collect_Fixed_Components (Typ : Entity_Id) is
11164 -- Build association list for discriminants, and find components of the
11165 -- variant part selected by the values of the discriminants.
11167 Old_C := First_Discriminant (Typ);
11168 Discr_Val := First_Elmt (Constraints);
11169 while Present (Old_C) loop
11170 Append_To (Assoc_List,
11171 Make_Component_Association (Loc,
11172 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
11173 Expression => New_Copy (Node (Discr_Val))));
11175 Next_Elmt (Discr_Val);
11176 Next_Discriminant (Old_C);
11179 -- The tag, and the possible parent and controller components
11180 -- are unconditionally in the subtype.
11182 if Is_Tagged_Type (Typ)
11183 or else Has_Controlled_Component (Typ)
11185 Old_C := First_Component (Typ);
11186 while Present (Old_C) loop
11187 if Chars ((Old_C)) = Name_uTag
11188 or else Chars ((Old_C)) = Name_uParent
11189 or else Chars ((Old_C)) = Name_uController
11191 Append_Elmt (Old_C, Comp_List);
11194 Next_Component (Old_C);
11197 end Collect_Fixed_Components;
11199 ---------------------------
11200 -- Create_All_Components --
11201 ---------------------------
11203 procedure Create_All_Components is
11207 Comp := First_Elmt (Comp_List);
11208 while Present (Comp) loop
11209 Old_C := Node (Comp);
11210 New_C := Create_Component (Old_C);
11214 Constrain_Component_Type
11215 (Old_C, Subt, Decl_Node, Typ, Constraints));
11216 Set_Is_Public (New_C, Is_Public (Subt));
11220 end Create_All_Components;
11222 ----------------------
11223 -- Create_Component --
11224 ----------------------
11226 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
11227 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
11230 if Ekind (Old_Compon) = E_Discriminant
11231 and then Is_Completely_Hidden (Old_Compon)
11233 -- This is a shadow discriminant created for a discriminant of
11234 -- the parent type, which needs to be present in the subtype.
11235 -- Give the shadow discriminant an internal name that cannot
11236 -- conflict with that of visible components.
11238 Set_Chars (New_Compon, New_Internal_Name ('C'));
11241 -- Set the parent so we have a proper link for freezing etc. This is
11242 -- not a real parent pointer, since of course our parent does not own
11243 -- up to us and reference us, we are an illegitimate child of the
11244 -- original parent!
11246 Set_Parent (New_Compon, Parent (Old_Compon));
11248 -- If the old component's Esize was already determined and is a
11249 -- static value, then the new component simply inherits it. Otherwise
11250 -- the old component's size may require run-time determination, but
11251 -- the new component's size still might be statically determinable
11252 -- (if, for example it has a static constraint). In that case we want
11253 -- Layout_Type to recompute the component's size, so we reset its
11254 -- size and positional fields.
11256 if Frontend_Layout_On_Target
11257 and then not Known_Static_Esize (Old_Compon)
11259 Set_Esize (New_Compon, Uint_0);
11260 Init_Normalized_First_Bit (New_Compon);
11261 Init_Normalized_Position (New_Compon);
11262 Init_Normalized_Position_Max (New_Compon);
11265 -- We do not want this node marked as Comes_From_Source, since
11266 -- otherwise it would get first class status and a separate cross-
11267 -- reference line would be generated. Illegitimate children do not
11268 -- rate such recognition.
11270 Set_Comes_From_Source (New_Compon, False);
11272 -- But it is a real entity, and a birth certificate must be properly
11273 -- registered by entering it into the entity list.
11275 Enter_Name (New_Compon);
11278 end Create_Component;
11280 -----------------------
11281 -- Is_Variant_Record --
11282 -----------------------
11284 function Is_Variant_Record (T : Entity_Id) return Boolean is
11286 return Nkind (Parent (T)) = N_Full_Type_Declaration
11287 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
11288 and then Present (Component_List (Type_Definition (Parent (T))))
11291 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
11292 end Is_Variant_Record;
11294 -- Start of processing for Create_Constrained_Components
11297 pragma Assert (Subt /= Base_Type (Subt));
11298 pragma Assert (Typ = Base_Type (Typ));
11300 Set_First_Entity (Subt, Empty);
11301 Set_Last_Entity (Subt, Empty);
11303 -- Check whether constraint is fully static, in which case we can
11304 -- optimize the list of components.
11306 Discr_Val := First_Elmt (Constraints);
11307 while Present (Discr_Val) loop
11308 if not Is_OK_Static_Expression (Node (Discr_Val)) then
11309 Is_Static := False;
11313 Next_Elmt (Discr_Val);
11316 Set_Has_Static_Discriminants (Subt, Is_Static);
11320 -- Inherit the discriminants of the parent type
11322 Add_Discriminants : declare
11328 Old_C := First_Discriminant (Typ);
11330 while Present (Old_C) loop
11331 Num_Disc := Num_Disc + 1;
11332 New_C := Create_Component (Old_C);
11333 Set_Is_Public (New_C, Is_Public (Subt));
11334 Next_Discriminant (Old_C);
11337 -- For an untagged derived subtype, the number of discriminants may
11338 -- be smaller than the number of inherited discriminants, because
11339 -- several of them may be renamed by a single new discriminant or
11340 -- constrained. In this case, add the hidden discriminants back into
11341 -- the subtype, because they need to be present if the optimizer of
11342 -- the GCC 4.x back-end decides to break apart assignments between
11343 -- objects using the parent view into member-wise assignments.
11347 if Is_Derived_Type (Typ)
11348 and then not Is_Tagged_Type (Typ)
11350 Old_C := First_Stored_Discriminant (Typ);
11352 while Present (Old_C) loop
11353 Num_Gird := Num_Gird + 1;
11354 Next_Stored_Discriminant (Old_C);
11358 if Num_Gird > Num_Disc then
11360 -- Find out multiple uses of new discriminants, and add hidden
11361 -- components for the extra renamed discriminants. We recognize
11362 -- multiple uses through the Corresponding_Discriminant of a
11363 -- new discriminant: if it constrains several old discriminants,
11364 -- this field points to the last one in the parent type. The
11365 -- stored discriminants of the derived type have the same name
11366 -- as those of the parent.
11370 New_Discr : Entity_Id;
11371 Old_Discr : Entity_Id;
11374 Constr := First_Elmt (Stored_Constraint (Typ));
11375 Old_Discr := First_Stored_Discriminant (Typ);
11376 while Present (Constr) loop
11377 if Is_Entity_Name (Node (Constr))
11378 and then Ekind (Entity (Node (Constr))) = E_Discriminant
11380 New_Discr := Entity (Node (Constr));
11382 if Chars (Corresponding_Discriminant (New_Discr)) /=
11385 -- The new discriminant has been used to rename a
11386 -- subsequent old discriminant. Introduce a shadow
11387 -- component for the current old discriminant.
11389 New_C := Create_Component (Old_Discr);
11390 Set_Original_Record_Component (New_C, Old_Discr);
11394 -- The constraint has eliminated the old discriminant.
11395 -- Introduce a shadow component.
11397 New_C := Create_Component (Old_Discr);
11398 Set_Original_Record_Component (New_C, Old_Discr);
11401 Next_Elmt (Constr);
11402 Next_Stored_Discriminant (Old_Discr);
11406 end Add_Discriminants;
11409 and then Is_Variant_Record (Typ)
11411 Collect_Fixed_Components (Typ);
11413 Gather_Components (
11415 Component_List (Type_Definition (Parent (Typ))),
11416 Governed_By => Assoc_List,
11418 Report_Errors => Errors);
11419 pragma Assert (not Errors);
11421 Create_All_Components;
11423 -- If the subtype declaration is created for a tagged type derivation
11424 -- with constraints, we retrieve the record definition of the parent
11425 -- type to select the components of the proper variant.
11428 and then Is_Tagged_Type (Typ)
11429 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
11431 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
11432 and then Is_Variant_Record (Parent_Type)
11434 Collect_Fixed_Components (Typ);
11436 Gather_Components (
11438 Component_List (Type_Definition (Parent (Parent_Type))),
11439 Governed_By => Assoc_List,
11441 Report_Errors => Errors);
11442 pragma Assert (not Errors);
11444 -- If the tagged derivation has a type extension, collect all the
11445 -- new components therein.
11448 (Record_Extension_Part (Type_Definition (Parent (Typ))))
11450 Old_C := First_Component (Typ);
11451 while Present (Old_C) loop
11452 if Original_Record_Component (Old_C) = Old_C
11453 and then Chars (Old_C) /= Name_uTag
11454 and then Chars (Old_C) /= Name_uParent
11455 and then Chars (Old_C) /= Name_uController
11457 Append_Elmt (Old_C, Comp_List);
11460 Next_Component (Old_C);
11464 Create_All_Components;
11467 -- If discriminants are not static, or if this is a multi-level type
11468 -- extension, we have to include all components of the parent type.
11470 Old_C := First_Component (Typ);
11471 while Present (Old_C) loop
11472 New_C := Create_Component (Old_C);
11476 Constrain_Component_Type
11477 (Old_C, Subt, Decl_Node, Typ, Constraints));
11478 Set_Is_Public (New_C, Is_Public (Subt));
11480 Next_Component (Old_C);
11485 end Create_Constrained_Components;
11487 ------------------------------------------
11488 -- Decimal_Fixed_Point_Type_Declaration --
11489 ------------------------------------------
11491 procedure Decimal_Fixed_Point_Type_Declaration
11495 Loc : constant Source_Ptr := Sloc (Def);
11496 Digs_Expr : constant Node_Id := Digits_Expression (Def);
11497 Delta_Expr : constant Node_Id := Delta_Expression (Def);
11498 Implicit_Base : Entity_Id;
11505 Check_Restriction (No_Fixed_Point, Def);
11507 -- Create implicit base type
11510 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
11511 Set_Etype (Implicit_Base, Implicit_Base);
11513 -- Analyze and process delta expression
11515 Analyze_And_Resolve (Delta_Expr, Universal_Real);
11517 Check_Delta_Expression (Delta_Expr);
11518 Delta_Val := Expr_Value_R (Delta_Expr);
11520 -- Check delta is power of 10, and determine scale value from it
11526 Scale_Val := Uint_0;
11529 if Val < Ureal_1 then
11530 while Val < Ureal_1 loop
11531 Val := Val * Ureal_10;
11532 Scale_Val := Scale_Val + 1;
11535 if Scale_Val > 18 then
11536 Error_Msg_N ("scale exceeds maximum value of 18", Def);
11537 Scale_Val := UI_From_Int (+18);
11541 while Val > Ureal_1 loop
11542 Val := Val / Ureal_10;
11543 Scale_Val := Scale_Val - 1;
11546 if Scale_Val < -18 then
11547 Error_Msg_N ("scale is less than minimum value of -18", Def);
11548 Scale_Val := UI_From_Int (-18);
11552 if Val /= Ureal_1 then
11553 Error_Msg_N ("delta expression must be a power of 10", Def);
11554 Delta_Val := Ureal_10 ** (-Scale_Val);
11558 -- Set delta, scale and small (small = delta for decimal type)
11560 Set_Delta_Value (Implicit_Base, Delta_Val);
11561 Set_Scale_Value (Implicit_Base, Scale_Val);
11562 Set_Small_Value (Implicit_Base, Delta_Val);
11564 -- Analyze and process digits expression
11566 Analyze_And_Resolve (Digs_Expr, Any_Integer);
11567 Check_Digits_Expression (Digs_Expr);
11568 Digs_Val := Expr_Value (Digs_Expr);
11570 if Digs_Val > 18 then
11571 Digs_Val := UI_From_Int (+18);
11572 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
11575 Set_Digits_Value (Implicit_Base, Digs_Val);
11576 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
11578 -- Set range of base type from digits value for now. This will be
11579 -- expanded to represent the true underlying base range by Freeze.
11581 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
11583 -- Note: We leave size as zero for now, size will be set at freeze
11584 -- time. We have to do this for ordinary fixed-point, because the size
11585 -- depends on the specified small, and we might as well do the same for
11586 -- decimal fixed-point.
11588 pragma Assert (Esize (Implicit_Base) = Uint_0);
11590 -- If there are bounds given in the declaration use them as the
11591 -- bounds of the first named subtype.
11593 if Present (Real_Range_Specification (Def)) then
11595 RRS : constant Node_Id := Real_Range_Specification (Def);
11596 Low : constant Node_Id := Low_Bound (RRS);
11597 High : constant Node_Id := High_Bound (RRS);
11602 Analyze_And_Resolve (Low, Any_Real);
11603 Analyze_And_Resolve (High, Any_Real);
11604 Check_Real_Bound (Low);
11605 Check_Real_Bound (High);
11606 Low_Val := Expr_Value_R (Low);
11607 High_Val := Expr_Value_R (High);
11609 if Low_Val < (-Bound_Val) then
11611 ("range low bound too small for digits value", Low);
11612 Low_Val := -Bound_Val;
11615 if High_Val > Bound_Val then
11617 ("range high bound too large for digits value", High);
11618 High_Val := Bound_Val;
11621 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
11624 -- If no explicit range, use range that corresponds to given
11625 -- digits value. This will end up as the final range for the
11629 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
11632 -- Complete entity for first subtype
11634 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
11635 Set_Etype (T, Implicit_Base);
11636 Set_Size_Info (T, Implicit_Base);
11637 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
11638 Set_Digits_Value (T, Digs_Val);
11639 Set_Delta_Value (T, Delta_Val);
11640 Set_Small_Value (T, Delta_Val);
11641 Set_Scale_Value (T, Scale_Val);
11642 Set_Is_Constrained (T);
11643 end Decimal_Fixed_Point_Type_Declaration;
11645 -----------------------------------
11646 -- Derive_Progenitor_Subprograms --
11647 -----------------------------------
11649 procedure Derive_Progenitor_Subprograms
11650 (Parent_Type : Entity_Id;
11651 Tagged_Type : Entity_Id)
11656 Iface_Elmt : Elmt_Id;
11657 Iface_Subp : Entity_Id;
11658 New_Subp : Entity_Id := Empty;
11659 Prim_Elmt : Elmt_Id;
11664 pragma Assert (Ada_Version >= Ada_05
11665 and then Is_Record_Type (Tagged_Type)
11666 and then Is_Tagged_Type (Tagged_Type)
11667 and then Has_Interfaces (Tagged_Type));
11669 -- Step 1: Transfer to the full-view primitives associated with the
11670 -- partial-view that cover interface primitives. Conceptually this
11671 -- work should be done later by Process_Full_View; done here to
11672 -- simplify its implementation at later stages. It can be safely
11673 -- done here because interfaces must be visible in the partial and
11674 -- private view (RM 7.3(7.3/2)).
11676 -- Small optimization: This work is only required if the parent is
11677 -- abstract. If the tagged type is not abstract, it cannot have
11678 -- abstract primitives (the only entities in the list of primitives of
11679 -- non-abstract tagged types that can reference abstract primitives
11680 -- through its Alias attribute are the internal entities that have
11681 -- attribute Interface_Alias, and these entities are generated later
11682 -- by Freeze_Record_Type).
11684 if In_Private_Part (Current_Scope)
11685 and then Is_Abstract_Type (Parent_Type)
11687 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
11688 while Present (Elmt) loop
11689 Subp := Node (Elmt);
11691 -- At this stage it is not possible to have entities in the list
11692 -- of primitives that have attribute Interface_Alias
11694 pragma Assert (No (Interface_Alias (Subp)));
11696 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
11698 if Is_Interface (Typ) then
11699 E := Find_Primitive_Covering_Interface
11700 (Tagged_Type => Tagged_Type,
11701 Iface_Prim => Subp);
11704 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
11706 Replace_Elmt (Elmt, E);
11707 Remove_Homonym (Subp);
11715 -- Step 2: Add primitives of progenitors that are not implemented by
11716 -- parents of Tagged_Type
11718 if Present (Interfaces (Base_Type (Tagged_Type))) then
11719 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
11720 while Present (Iface_Elmt) loop
11721 Iface := Node (Iface_Elmt);
11723 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
11724 while Present (Prim_Elmt) loop
11725 Iface_Subp := Node (Prim_Elmt);
11727 -- Exclude derivation of predefined primitives except those
11728 -- that come from source. Required to catch declarations of
11729 -- equality operators of interfaces. For example:
11731 -- type Iface is interface;
11732 -- function "=" (Left, Right : Iface) return Boolean;
11734 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
11735 or else Comes_From_Source (Iface_Subp)
11737 E := Find_Primitive_Covering_Interface
11738 (Tagged_Type => Tagged_Type,
11739 Iface_Prim => Iface_Subp);
11741 -- If not found we derive a new primitive leaving its alias
11742 -- attribute referencing the interface primitive
11746 (New_Subp, Iface_Subp, Tagged_Type, Iface);
11748 -- Propagate to the full view interface entities associated
11749 -- with the partial view
11751 elsif In_Private_Part (Current_Scope)
11752 and then Present (Alias (E))
11753 and then Alias (E) = Iface_Subp
11755 List_Containing (Parent (E)) /=
11756 Private_Declarations
11758 (Unit_Declaration_Node (Current_Scope)))
11760 Append_Elmt (E, Primitive_Operations (Tagged_Type));
11764 Next_Elmt (Prim_Elmt);
11767 Next_Elmt (Iface_Elmt);
11770 end Derive_Progenitor_Subprograms;
11772 -----------------------
11773 -- Derive_Subprogram --
11774 -----------------------
11776 procedure Derive_Subprogram
11777 (New_Subp : in out Entity_Id;
11778 Parent_Subp : Entity_Id;
11779 Derived_Type : Entity_Id;
11780 Parent_Type : Entity_Id;
11781 Actual_Subp : Entity_Id := Empty)
11783 Formal : Entity_Id;
11784 -- Formal parameter of parent primitive operation
11786 Formal_Of_Actual : Entity_Id;
11787 -- Formal parameter of actual operation, when the derivation is to
11788 -- create a renaming for a primitive operation of an actual in an
11791 New_Formal : Entity_Id;
11792 -- Formal of inherited operation
11794 Visible_Subp : Entity_Id := Parent_Subp;
11796 function Is_Private_Overriding return Boolean;
11797 -- If Subp is a private overriding of a visible operation, the inherited
11798 -- operation derives from the overridden op (even though its body is the
11799 -- overriding one) and the inherited operation is visible now. See
11800 -- sem_disp to see the full details of the handling of the overridden
11801 -- subprogram, which is removed from the list of primitive operations of
11802 -- the type. The overridden subprogram is saved locally in Visible_Subp,
11803 -- and used to diagnose abstract operations that need overriding in the
11806 procedure Replace_Type (Id, New_Id : Entity_Id);
11807 -- When the type is an anonymous access type, create a new access type
11808 -- designating the derived type.
11810 procedure Set_Derived_Name;
11811 -- This procedure sets the appropriate Chars name for New_Subp. This
11812 -- is normally just a copy of the parent name. An exception arises for
11813 -- type support subprograms, where the name is changed to reflect the
11814 -- name of the derived type, e.g. if type foo is derived from type bar,
11815 -- then a procedure barDA is derived with a name fooDA.
11817 ---------------------------
11818 -- Is_Private_Overriding --
11819 ---------------------------
11821 function Is_Private_Overriding return Boolean is
11825 -- If the parent is not a dispatching operation there is no
11826 -- need to investigate overridings
11828 if not Is_Dispatching_Operation (Parent_Subp) then
11832 -- The visible operation that is overridden is a homonym of the
11833 -- parent subprogram. We scan the homonym chain to find the one
11834 -- whose alias is the subprogram we are deriving.
11836 Prev := Current_Entity (Parent_Subp);
11837 while Present (Prev) loop
11838 if Ekind (Prev) = Ekind (Parent_Subp)
11839 and then Alias (Prev) = Parent_Subp
11840 and then Scope (Parent_Subp) = Scope (Prev)
11841 and then not Is_Hidden (Prev)
11843 Visible_Subp := Prev;
11847 Prev := Homonym (Prev);
11851 end Is_Private_Overriding;
11857 procedure Replace_Type (Id, New_Id : Entity_Id) is
11858 Acc_Type : Entity_Id;
11859 Par : constant Node_Id := Parent (Derived_Type);
11862 -- When the type is an anonymous access type, create a new access
11863 -- type designating the derived type. This itype must be elaborated
11864 -- at the point of the derivation, not on subsequent calls that may
11865 -- be out of the proper scope for Gigi, so we insert a reference to
11866 -- it after the derivation.
11868 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
11870 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
11873 if Ekind (Desig_Typ) = E_Record_Type_With_Private
11874 and then Present (Full_View (Desig_Typ))
11875 and then not Is_Private_Type (Parent_Type)
11877 Desig_Typ := Full_View (Desig_Typ);
11880 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
11882 -- Ada 2005 (AI-251): Handle also derivations of abstract
11883 -- interface primitives.
11885 or else (Is_Interface (Desig_Typ)
11886 and then not Is_Class_Wide_Type (Desig_Typ))
11888 Acc_Type := New_Copy (Etype (Id));
11889 Set_Etype (Acc_Type, Acc_Type);
11890 Set_Scope (Acc_Type, New_Subp);
11892 -- Compute size of anonymous access type
11894 if Is_Array_Type (Desig_Typ)
11895 and then not Is_Constrained (Desig_Typ)
11897 Init_Size (Acc_Type, 2 * System_Address_Size);
11899 Init_Size (Acc_Type, System_Address_Size);
11902 Init_Alignment (Acc_Type);
11903 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
11905 Set_Etype (New_Id, Acc_Type);
11906 Set_Scope (New_Id, New_Subp);
11908 -- Create a reference to it
11909 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
11912 Set_Etype (New_Id, Etype (Id));
11916 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
11918 (Ekind (Etype (Id)) = E_Record_Type_With_Private
11919 and then Present (Full_View (Etype (Id)))
11921 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
11923 -- Constraint checks on formals are generated during expansion,
11924 -- based on the signature of the original subprogram. The bounds
11925 -- of the derived type are not relevant, and thus we can use
11926 -- the base type for the formals. However, the return type may be
11927 -- used in a context that requires that the proper static bounds
11928 -- be used (a case statement, for example) and for those cases
11929 -- we must use the derived type (first subtype), not its base.
11931 -- If the derived_type_definition has no constraints, we know that
11932 -- the derived type has the same constraints as the first subtype
11933 -- of the parent, and we can also use it rather than its base,
11934 -- which can lead to more efficient code.
11936 if Etype (Id) = Parent_Type then
11937 if Is_Scalar_Type (Parent_Type)
11939 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
11941 Set_Etype (New_Id, Derived_Type);
11943 elsif Nkind (Par) = N_Full_Type_Declaration
11945 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
11948 (Subtype_Indication (Type_Definition (Par)))
11950 Set_Etype (New_Id, Derived_Type);
11953 Set_Etype (New_Id, Base_Type (Derived_Type));
11957 Set_Etype (New_Id, Base_Type (Derived_Type));
11960 -- Ada 2005 (AI-251): Handle derivations of abstract interface
11963 elsif Is_Interface (Etype (Id))
11964 and then not Is_Class_Wide_Type (Etype (Id))
11965 and then Is_Progenitor (Etype (Id), Derived_Type)
11967 Set_Etype (New_Id, Derived_Type);
11970 Set_Etype (New_Id, Etype (Id));
11974 ----------------------
11975 -- Set_Derived_Name --
11976 ----------------------
11978 procedure Set_Derived_Name is
11979 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
11981 if Nm = TSS_Null then
11982 Set_Chars (New_Subp, Chars (Parent_Subp));
11984 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
11986 end Set_Derived_Name;
11990 Parent_Overrides_Interface_Primitive : Boolean := False;
11992 -- Start of processing for Derive_Subprogram
11996 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
11997 Set_Ekind (New_Subp, Ekind (Parent_Subp));
11999 -- Check whether the parent overrides an interface primitive
12001 if Is_Overriding_Operation (Parent_Subp) then
12003 E : Entity_Id := Parent_Subp;
12005 while Present (Overridden_Operation (E)) loop
12006 E := Ultimate_Alias (Overridden_Operation (E));
12009 Parent_Overrides_Interface_Primitive :=
12010 Is_Dispatching_Operation (E)
12011 and then Present (Find_Dispatching_Type (E))
12012 and then Is_Interface (Find_Dispatching_Type (E));
12016 -- Check whether the inherited subprogram is a private operation that
12017 -- should be inherited but not yet made visible. Such subprograms can
12018 -- become visible at a later point (e.g., the private part of a public
12019 -- child unit) via Declare_Inherited_Private_Subprograms. If the
12020 -- following predicate is true, then this is not such a private
12021 -- operation and the subprogram simply inherits the name of the parent
12022 -- subprogram. Note the special check for the names of controlled
12023 -- operations, which are currently exempted from being inherited with
12024 -- a hidden name because they must be findable for generation of
12025 -- implicit run-time calls.
12027 if not Is_Hidden (Parent_Subp)
12028 or else Is_Internal (Parent_Subp)
12029 or else Is_Private_Overriding
12030 or else Is_Internal_Name (Chars (Parent_Subp))
12031 or else Chars (Parent_Subp) = Name_Initialize
12032 or else Chars (Parent_Subp) = Name_Adjust
12033 or else Chars (Parent_Subp) = Name_Finalize
12037 -- If parent is hidden, this can be a regular derivation if the
12038 -- parent is immediately visible in a non-instantiating context,
12039 -- or if we are in the private part of an instance. This test
12040 -- should still be refined ???
12042 -- The test for In_Instance_Not_Visible avoids inheriting the derived
12043 -- operation as a non-visible operation in cases where the parent
12044 -- subprogram might not be visible now, but was visible within the
12045 -- original generic, so it would be wrong to make the inherited
12046 -- subprogram non-visible now. (Not clear if this test is fully
12047 -- correct; are there any cases where we should declare the inherited
12048 -- operation as not visible to avoid it being overridden, e.g., when
12049 -- the parent type is a generic actual with private primitives ???)
12051 -- (they should be treated the same as other private inherited
12052 -- subprograms, but it's not clear how to do this cleanly). ???
12054 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
12055 and then Is_Immediately_Visible (Parent_Subp)
12056 and then not In_Instance)
12057 or else In_Instance_Not_Visible
12061 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
12062 -- overrides an interface primitive because interface primitives
12063 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
12065 elsif Parent_Overrides_Interface_Primitive then
12068 -- The type is inheriting a private operation, so enter
12069 -- it with a special name so it can't be overridden.
12072 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
12075 Set_Parent (New_Subp, Parent (Derived_Type));
12077 if Present (Actual_Subp) then
12078 Replace_Type (Actual_Subp, New_Subp);
12080 Replace_Type (Parent_Subp, New_Subp);
12083 Conditional_Delay (New_Subp, Parent_Subp);
12085 -- If we are creating a renaming for a primitive operation of an
12086 -- actual of a generic derived type, we must examine the signature
12087 -- of the actual primitive, not that of the generic formal, which for
12088 -- example may be an interface. However the name and initial value
12089 -- of the inherited operation are those of the formal primitive.
12091 Formal := First_Formal (Parent_Subp);
12093 if Present (Actual_Subp) then
12094 Formal_Of_Actual := First_Formal (Actual_Subp);
12096 Formal_Of_Actual := Empty;
12099 while Present (Formal) loop
12100 New_Formal := New_Copy (Formal);
12102 -- Normally we do not go copying parents, but in the case of
12103 -- formals, we need to link up to the declaration (which is the
12104 -- parameter specification), and it is fine to link up to the
12105 -- original formal's parameter specification in this case.
12107 Set_Parent (New_Formal, Parent (Formal));
12108 Append_Entity (New_Formal, New_Subp);
12110 if Present (Formal_Of_Actual) then
12111 Replace_Type (Formal_Of_Actual, New_Formal);
12112 Next_Formal (Formal_Of_Actual);
12114 Replace_Type (Formal, New_Formal);
12117 Next_Formal (Formal);
12120 -- If this derivation corresponds to a tagged generic actual, then
12121 -- primitive operations rename those of the actual. Otherwise the
12122 -- primitive operations rename those of the parent type, If the parent
12123 -- renames an intrinsic operator, so does the new subprogram. We except
12124 -- concatenation, which is always properly typed, and does not get
12125 -- expanded as other intrinsic operations.
12127 if No (Actual_Subp) then
12128 if Is_Intrinsic_Subprogram (Parent_Subp) then
12129 Set_Is_Intrinsic_Subprogram (New_Subp);
12131 if Present (Alias (Parent_Subp))
12132 and then Chars (Parent_Subp) /= Name_Op_Concat
12134 Set_Alias (New_Subp, Alias (Parent_Subp));
12136 Set_Alias (New_Subp, Parent_Subp);
12140 Set_Alias (New_Subp, Parent_Subp);
12144 Set_Alias (New_Subp, Actual_Subp);
12147 -- Derived subprograms of a tagged type must inherit the convention
12148 -- of the parent subprogram (a requirement of AI-117). Derived
12149 -- subprograms of untagged types simply get convention Ada by default.
12151 if Is_Tagged_Type (Derived_Type) then
12152 Set_Convention (New_Subp, Convention (Parent_Subp));
12155 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
12156 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
12158 if Ekind (Parent_Subp) = E_Procedure then
12159 Set_Is_Valued_Procedure
12160 (New_Subp, Is_Valued_Procedure (Parent_Subp));
12163 -- No_Return must be inherited properly. If this is overridden in the
12164 -- case of a dispatching operation, then a check is made in Sem_Disp
12165 -- that the overriding operation is also No_Return (no such check is
12166 -- required for the case of non-dispatching operation.
12168 Set_No_Return (New_Subp, No_Return (Parent_Subp));
12170 -- A derived function with a controlling result is abstract. If the
12171 -- Derived_Type is a nonabstract formal generic derived type, then
12172 -- inherited operations are not abstract: the required check is done at
12173 -- instantiation time. If the derivation is for a generic actual, the
12174 -- function is not abstract unless the actual is.
12176 if Is_Generic_Type (Derived_Type)
12177 and then not Is_Abstract_Type (Derived_Type)
12181 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
12182 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
12184 elsif Ada_Version >= Ada_05
12185 and then (Is_Abstract_Subprogram (Alias (New_Subp))
12186 or else (Is_Tagged_Type (Derived_Type)
12187 and then Etype (New_Subp) = Derived_Type
12188 and then not Is_Null_Extension (Derived_Type))
12189 or else (Is_Tagged_Type (Derived_Type)
12190 and then Ekind (Etype (New_Subp)) =
12191 E_Anonymous_Access_Type
12192 and then Designated_Type (Etype (New_Subp)) =
12194 and then not Is_Null_Extension (Derived_Type)))
12195 and then No (Actual_Subp)
12197 if not Is_Tagged_Type (Derived_Type)
12198 or else Is_Abstract_Type (Derived_Type)
12199 or else Is_Abstract_Subprogram (Alias (New_Subp))
12201 Set_Is_Abstract_Subprogram (New_Subp);
12203 Set_Requires_Overriding (New_Subp);
12206 elsif Ada_Version < Ada_05
12207 and then (Is_Abstract_Subprogram (Alias (New_Subp))
12208 or else (Is_Tagged_Type (Derived_Type)
12209 and then Etype (New_Subp) = Derived_Type
12210 and then No (Actual_Subp)))
12212 Set_Is_Abstract_Subprogram (New_Subp);
12214 -- Finally, if the parent type is abstract we must verify that all
12215 -- inherited operations are either non-abstract or overridden, or that
12216 -- the derived type itself is abstract (this check is performed at the
12217 -- end of a package declaration, in Check_Abstract_Overriding). A
12218 -- private overriding in the parent type will not be visible in the
12219 -- derivation if we are not in an inner package or in a child unit of
12220 -- the parent type, in which case the abstractness of the inherited
12221 -- operation is carried to the new subprogram.
12223 elsif Is_Abstract_Type (Parent_Type)
12224 and then not In_Open_Scopes (Scope (Parent_Type))
12225 and then Is_Private_Overriding
12226 and then Is_Abstract_Subprogram (Visible_Subp)
12228 if No (Actual_Subp) then
12229 Set_Alias (New_Subp, Visible_Subp);
12230 Set_Is_Abstract_Subprogram
12233 -- If this is a derivation for an instance of a formal derived
12234 -- type, abstractness comes from the primitive operation of the
12235 -- actual, not from the operation inherited from the ancestor.
12237 Set_Is_Abstract_Subprogram
12238 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
12242 New_Overloaded_Entity (New_Subp, Derived_Type);
12244 -- Check for case of a derived subprogram for the instantiation of a
12245 -- formal derived tagged type, if so mark the subprogram as dispatching
12246 -- and inherit the dispatching attributes of the parent subprogram. The
12247 -- derived subprogram is effectively renaming of the actual subprogram,
12248 -- so it needs to have the same attributes as the actual.
12250 if Present (Actual_Subp)
12251 and then Is_Dispatching_Operation (Parent_Subp)
12253 Set_Is_Dispatching_Operation (New_Subp);
12255 if Present (DTC_Entity (Parent_Subp)) then
12256 Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
12257 Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
12261 -- Indicate that a derived subprogram does not require a body and that
12262 -- it does not require processing of default expressions.
12264 Set_Has_Completion (New_Subp);
12265 Set_Default_Expressions_Processed (New_Subp);
12267 if Ekind (New_Subp) = E_Function then
12268 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
12270 end Derive_Subprogram;
12272 ------------------------
12273 -- Derive_Subprograms --
12274 ------------------------
12276 procedure Derive_Subprograms
12277 (Parent_Type : Entity_Id;
12278 Derived_Type : Entity_Id;
12279 Generic_Actual : Entity_Id := Empty)
12281 Op_List : constant Elist_Id :=
12282 Collect_Primitive_Operations (Parent_Type);
12284 function Check_Derived_Type return Boolean;
12285 -- Check that all primitive inherited from Parent_Type are found in
12286 -- the list of primitives of Derived_Type exactly in the same order.
12288 function Check_Derived_Type return Boolean is
12292 New_Subp : Entity_Id;
12297 -- Traverse list of entities in the current scope searching for
12298 -- an incomplete type whose full-view is derived type
12300 E := First_Entity (Scope (Derived_Type));
12302 and then E /= Derived_Type
12304 if Ekind (E) = E_Incomplete_Type
12305 and then Present (Full_View (E))
12306 and then Full_View (E) = Derived_Type
12308 -- Disable this test if Derived_Type completes an incomplete
12309 -- type because in such case more primitives can be added
12310 -- later to the list of primitives of Derived_Type by routine
12311 -- Process_Incomplete_Dependents
12316 E := Next_Entity (E);
12319 List := Collect_Primitive_Operations (Derived_Type);
12320 Elmt := First_Elmt (List);
12322 Op_Elmt := First_Elmt (Op_List);
12323 while Present (Op_Elmt) loop
12324 Subp := Node (Op_Elmt);
12325 New_Subp := Node (Elmt);
12327 -- At this early stage Derived_Type has no entities with attribute
12328 -- Interface_Alias. In addition, such primitives are always
12329 -- located at the end of the list of primitives of Parent_Type.
12330 -- Therefore, if found we can safely stop processing pending
12333 exit when Present (Interface_Alias (Subp));
12335 -- Handle hidden entities
12337 if not Is_Predefined_Dispatching_Operation (Subp)
12338 and then Is_Hidden (Subp)
12340 if Present (New_Subp)
12341 and then Primitive_Names_Match (Subp, New_Subp)
12347 if not Present (New_Subp)
12348 or else Ekind (Subp) /= Ekind (New_Subp)
12349 or else not Primitive_Names_Match (Subp, New_Subp)
12357 Next_Elmt (Op_Elmt);
12361 end Check_Derived_Type;
12365 Alias_Subp : Entity_Id;
12366 Act_List : Elist_Id;
12367 Act_Elmt : Elmt_Id := No_Elmt;
12368 Act_Subp : Entity_Id := Empty;
12370 Need_Search : Boolean := False;
12371 New_Subp : Entity_Id := Empty;
12372 Parent_Base : Entity_Id;
12375 -- Start of processing for Derive_Subprograms
12378 if Ekind (Parent_Type) = E_Record_Type_With_Private
12379 and then Has_Discriminants (Parent_Type)
12380 and then Present (Full_View (Parent_Type))
12382 Parent_Base := Full_View (Parent_Type);
12384 Parent_Base := Parent_Type;
12387 if Present (Generic_Actual) then
12388 Act_List := Collect_Primitive_Operations (Generic_Actual);
12389 Act_Elmt := First_Elmt (Act_List);
12392 -- Derive primitives inherited from the parent. Note that if the generic
12393 -- actual is present, this is not really a type derivation, it is a
12394 -- completion within an instance.
12396 -- Case 1: Derived_Type does not implement interfaces
12398 if not Is_Tagged_Type (Derived_Type)
12399 or else (not Has_Interfaces (Derived_Type)
12400 and then not (Present (Generic_Actual)
12402 Has_Interfaces (Generic_Actual)))
12404 Elmt := First_Elmt (Op_List);
12405 while Present (Elmt) loop
12406 Subp := Node (Elmt);
12408 -- Literals are derived earlier in the process of building the
12409 -- derived type, and are skipped here.
12411 if Ekind (Subp) = E_Enumeration_Literal then
12414 -- The actual is a direct descendant and the common primitive
12415 -- operations appear in the same order.
12417 -- If the generic parent type is present, the derived type is an
12418 -- instance of a formal derived type, and within the instance its
12419 -- operations are those of the actual. We derive from the formal
12420 -- type but make the inherited operations aliases of the
12421 -- corresponding operations of the actual.
12425 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
12427 if Present (Act_Elmt) then
12428 Next_Elmt (Act_Elmt);
12435 -- Case 2: Derived_Type implements interfaces
12438 -- If the parent type has no predefined primitives we remove
12439 -- predefined primitives from the list of primitives of generic
12440 -- actual to simplify the complexity of this algorithm.
12442 if Present (Generic_Actual) then
12444 Has_Predefined_Primitives : Boolean := False;
12447 -- Check if the parent type has predefined primitives
12449 Elmt := First_Elmt (Op_List);
12450 while Present (Elmt) loop
12451 Subp := Node (Elmt);
12453 if Is_Predefined_Dispatching_Operation (Subp)
12454 and then not Comes_From_Source (Ultimate_Alias (Subp))
12456 Has_Predefined_Primitives := True;
12463 -- Remove predefined primitives of Generic_Actual. We must use
12464 -- an auxiliary list because in case of tagged types the value
12465 -- returned by Collect_Primitive_Operations is the value stored
12466 -- in its Primitive_Operations attribute (and we don't want to
12467 -- modify its current contents).
12469 if not Has_Predefined_Primitives then
12471 Aux_List : constant Elist_Id := New_Elmt_List;
12474 Elmt := First_Elmt (Act_List);
12475 while Present (Elmt) loop
12476 Subp := Node (Elmt);
12478 if not Is_Predefined_Dispatching_Operation (Subp)
12479 or else Comes_From_Source (Subp)
12481 Append_Elmt (Subp, Aux_List);
12487 Act_List := Aux_List;
12491 Act_Elmt := First_Elmt (Act_List);
12492 Act_Subp := Node (Act_Elmt);
12496 -- Stage 1: If the generic actual is not present we derive the
12497 -- primitives inherited from the parent type. If the generic parent
12498 -- type is present, the derived type is an instance of a formal
12499 -- derived type, and within the instance its operations are those of
12500 -- the actual. We derive from the formal type but make the inherited
12501 -- operations aliases of the corresponding operations of the actual.
12503 Elmt := First_Elmt (Op_List);
12504 while Present (Elmt) loop
12505 Subp := Node (Elmt);
12506 Alias_Subp := Ultimate_Alias (Subp);
12508 -- At this early stage Derived_Type has no entities with attribute
12509 -- Interface_Alias. In addition, such primitives are always
12510 -- located at the end of the list of primitives of Parent_Type.
12511 -- Therefore, if found we can safely stop processing pending
12514 exit when Present (Interface_Alias (Subp));
12516 -- If the generic actual is present find the corresponding
12517 -- operation in the generic actual. If the parent type is a
12518 -- direct ancestor of the derived type then, even if it is an
12519 -- interface, the operations are inherited from the primary
12520 -- dispatch table and are in the proper order. If we detect here
12521 -- that primitives are not in the same order we traverse the list
12522 -- of primitive operations of the actual to find the one that
12523 -- implements the interface primitive.
12527 (Present (Generic_Actual)
12528 and then Present (Act_Subp)
12529 and then not Primitive_Names_Match (Subp, Act_Subp))
12531 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual));
12532 pragma Assert (Is_Interface (Parent_Base));
12534 -- Remember that we need searching for all the pending
12537 Need_Search := True;
12539 -- Handle entities associated with interface primitives
12541 if Present (Alias (Subp))
12542 and then Is_Interface (Find_Dispatching_Type (Alias (Subp)))
12543 and then not Is_Predefined_Dispatching_Operation (Subp)
12546 Find_Primitive_Covering_Interface
12547 (Tagged_Type => Generic_Actual,
12548 Iface_Prim => Subp);
12550 -- Handle predefined primitives plus the rest of user-defined
12554 Act_Elmt := First_Elmt (Act_List);
12555 while Present (Act_Elmt) loop
12556 Act_Subp := Node (Act_Elmt);
12558 exit when Primitive_Names_Match (Subp, Act_Subp)
12559 and then Type_Conformant (Subp, Act_Subp,
12560 Skip_Controlling_Formals => True)
12561 and then No (Interface_Alias (Act_Subp));
12563 Next_Elmt (Act_Elmt);
12568 -- Case 1: If the parent is a limited interface then it has the
12569 -- predefined primitives of synchronized interfaces. However, the
12570 -- actual type may be a non-limited type and hence it does not
12571 -- have such primitives.
12573 if Present (Generic_Actual)
12574 and then not Present (Act_Subp)
12575 and then Is_Limited_Interface (Parent_Base)
12576 and then Is_Predefined_Interface_Primitive (Subp)
12580 -- Case 2: Inherit entities associated with interfaces that
12581 -- were not covered by the parent type. We exclude here null
12582 -- interface primitives because they do not need special
12585 elsif Present (Alias (Subp))
12586 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
12588 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
12589 and then Null_Present (Parent (Alias_Subp)))
12592 (New_Subp => New_Subp,
12593 Parent_Subp => Alias_Subp,
12594 Derived_Type => Derived_Type,
12595 Parent_Type => Find_Dispatching_Type (Alias_Subp),
12596 Actual_Subp => Act_Subp);
12598 if No (Generic_Actual) then
12599 Set_Alias (New_Subp, Subp);
12602 -- Case 3: Common derivation
12606 (New_Subp => New_Subp,
12607 Parent_Subp => Subp,
12608 Derived_Type => Derived_Type,
12609 Parent_Type => Parent_Base,
12610 Actual_Subp => Act_Subp);
12613 -- No need to update Act_Elm if we must search for the
12614 -- corresponding operation in the generic actual
12617 and then Present (Act_Elmt)
12619 Next_Elmt (Act_Elmt);
12620 Act_Subp := Node (Act_Elmt);
12626 -- Inherit additional operations from progenitors. If the derived
12627 -- type is a generic actual, there are not new primitive operations
12628 -- for the type because it has those of the actual, and therefore
12629 -- nothing needs to be done. The renamings generated above are not
12630 -- primitive operations, and their purpose is simply to make the
12631 -- proper operations visible within an instantiation.
12633 if No (Generic_Actual) then
12634 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
12638 -- Final check: Direct descendants must have their primitives in the
12639 -- same order. We exclude from this test non-tagged types and instances
12640 -- of formal derived types. We skip this test if we have already
12641 -- reported serious errors in the sources.
12643 pragma Assert (not Is_Tagged_Type (Derived_Type)
12644 or else Present (Generic_Actual)
12645 or else Serious_Errors_Detected > 0
12646 or else Check_Derived_Type);
12647 end Derive_Subprograms;
12649 --------------------------------
12650 -- Derived_Standard_Character --
12651 --------------------------------
12653 procedure Derived_Standard_Character
12655 Parent_Type : Entity_Id;
12656 Derived_Type : Entity_Id)
12658 Loc : constant Source_Ptr := Sloc (N);
12659 Def : constant Node_Id := Type_Definition (N);
12660 Indic : constant Node_Id := Subtype_Indication (Def);
12661 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
12662 Implicit_Base : constant Entity_Id :=
12664 (E_Enumeration_Type, N, Derived_Type, 'B');
12670 Discard_Node (Process_Subtype (Indic, N));
12672 Set_Etype (Implicit_Base, Parent_Base);
12673 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
12674 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
12676 Set_Is_Character_Type (Implicit_Base, True);
12677 Set_Has_Delayed_Freeze (Implicit_Base);
12679 -- The bounds of the implicit base are the bounds of the parent base.
12680 -- Note that their type is the parent base.
12682 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
12683 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
12685 Set_Scalar_Range (Implicit_Base,
12688 High_Bound => Hi));
12690 Conditional_Delay (Derived_Type, Parent_Type);
12692 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
12693 Set_Etype (Derived_Type, Implicit_Base);
12694 Set_Size_Info (Derived_Type, Parent_Type);
12696 if Unknown_RM_Size (Derived_Type) then
12697 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
12700 Set_Is_Character_Type (Derived_Type, True);
12702 if Nkind (Indic) /= N_Subtype_Indication then
12704 -- If no explicit constraint, the bounds are those
12705 -- of the parent type.
12707 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
12708 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
12709 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
12712 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
12714 -- Because the implicit base is used in the conversion of the bounds, we
12715 -- have to freeze it now. This is similar to what is done for numeric
12716 -- types, and it equally suspicious, but otherwise a non-static bound
12717 -- will have a reference to an unfrozen type, which is rejected by Gigi
12718 -- (???). This requires specific care for definition of stream
12719 -- attributes. For details, see comments at the end of
12720 -- Build_Derived_Numeric_Type.
12722 Freeze_Before (N, Implicit_Base);
12723 end Derived_Standard_Character;
12725 ------------------------------
12726 -- Derived_Type_Declaration --
12727 ------------------------------
12729 procedure Derived_Type_Declaration
12732 Is_Completion : Boolean)
12734 Parent_Type : Entity_Id;
12736 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
12737 -- Check whether the parent type is a generic formal, or derives
12738 -- directly or indirectly from one.
12740 ------------------------
12741 -- Comes_From_Generic --
12742 ------------------------
12744 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
12746 if Is_Generic_Type (Typ) then
12749 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
12752 elsif Is_Private_Type (Typ)
12753 and then Present (Full_View (Typ))
12754 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
12758 elsif Is_Generic_Actual_Type (Typ) then
12764 end Comes_From_Generic;
12768 Def : constant Node_Id := Type_Definition (N);
12769 Iface_Def : Node_Id;
12770 Indic : constant Node_Id := Subtype_Indication (Def);
12771 Extension : constant Node_Id := Record_Extension_Part (Def);
12772 Parent_Node : Node_Id;
12773 Parent_Scope : Entity_Id;
12776 -- Start of processing for Derived_Type_Declaration
12779 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
12781 -- Ada 2005 (AI-251): In case of interface derivation check that the
12782 -- parent is also an interface.
12784 if Interface_Present (Def) then
12785 if not Is_Interface (Parent_Type) then
12786 Diagnose_Interface (Indic, Parent_Type);
12789 Parent_Node := Parent (Base_Type (Parent_Type));
12790 Iface_Def := Type_Definition (Parent_Node);
12792 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
12793 -- other limited interfaces.
12795 if Limited_Present (Def) then
12796 if Limited_Present (Iface_Def) then
12799 elsif Protected_Present (Iface_Def) then
12801 ("descendant of& must be declared"
12802 & " as a protected interface",
12805 elsif Synchronized_Present (Iface_Def) then
12807 ("descendant of& must be declared"
12808 & " as a synchronized interface",
12811 elsif Task_Present (Iface_Def) then
12813 ("descendant of& must be declared as a task interface",
12818 ("(Ada 2005) limited interface cannot "
12819 & "inherit from non-limited interface", Indic);
12822 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
12823 -- from non-limited or limited interfaces.
12825 elsif not Protected_Present (Def)
12826 and then not Synchronized_Present (Def)
12827 and then not Task_Present (Def)
12829 if Limited_Present (Iface_Def) then
12832 elsif Protected_Present (Iface_Def) then
12834 ("descendant of& must be declared"
12835 & " as a protected interface",
12838 elsif Synchronized_Present (Iface_Def) then
12840 ("descendant of& must be declared"
12841 & " as a synchronized interface",
12844 elsif Task_Present (Iface_Def) then
12846 ("descendant of& must be declared as a task interface",
12855 if Is_Tagged_Type (Parent_Type)
12856 and then Is_Concurrent_Type (Parent_Type)
12857 and then not Is_Interface (Parent_Type)
12860 ("parent type of a record extension cannot be "
12861 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
12862 Set_Etype (T, Any_Type);
12866 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
12869 if Is_Tagged_Type (Parent_Type)
12870 and then Is_Non_Empty_List (Interface_List (Def))
12877 Intf := First (Interface_List (Def));
12878 while Present (Intf) loop
12879 T := Find_Type_Of_Subtype_Indic (Intf);
12881 if not Is_Interface (T) then
12882 Diagnose_Interface (Intf, T);
12884 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
12885 -- a limited type from having a nonlimited progenitor.
12887 elsif (Limited_Present (Def)
12888 or else (not Is_Interface (Parent_Type)
12889 and then Is_Limited_Type (Parent_Type)))
12890 and then not Is_Limited_Interface (T)
12893 ("progenitor interface& of limited type must be limited",
12902 if Parent_Type = Any_Type
12903 or else Etype (Parent_Type) = Any_Type
12904 or else (Is_Class_Wide_Type (Parent_Type)
12905 and then Etype (Parent_Type) = T)
12907 -- If Parent_Type is undefined or illegal, make new type into a
12908 -- subtype of Any_Type, and set a few attributes to prevent cascaded
12909 -- errors. If this is a self-definition, emit error now.
12912 or else T = Etype (Parent_Type)
12914 Error_Msg_N ("type cannot be used in its own definition", Indic);
12917 Set_Ekind (T, Ekind (Parent_Type));
12918 Set_Etype (T, Any_Type);
12919 Set_Scalar_Range (T, Scalar_Range (Any_Type));
12921 if Is_Tagged_Type (T) then
12922 Set_Primitive_Operations (T, New_Elmt_List);
12928 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
12929 -- an interface is special because the list of interfaces in the full
12930 -- view can be given in any order. For example:
12932 -- type A is interface;
12933 -- type B is interface and A;
12934 -- type D is new B with private;
12936 -- type D is new A and B with null record; -- 1 --
12938 -- In this case we perform the following transformation of -1-:
12940 -- type D is new B and A with null record;
12942 -- If the parent of the full-view covers the parent of the partial-view
12943 -- we have two possible cases:
12945 -- 1) They have the same parent
12946 -- 2) The parent of the full-view implements some further interfaces
12948 -- In both cases we do not need to perform the transformation. In the
12949 -- first case the source program is correct and the transformation is
12950 -- not needed; in the second case the source program does not fulfill
12951 -- the no-hidden interfaces rule (AI-396) and the error will be reported
12954 -- This transformation not only simplifies the rest of the analysis of
12955 -- this type declaration but also simplifies the correct generation of
12956 -- the object layout to the expander.
12958 if In_Private_Part (Current_Scope)
12959 and then Is_Interface (Parent_Type)
12963 Partial_View : Entity_Id;
12964 Partial_View_Parent : Entity_Id;
12965 New_Iface : Node_Id;
12968 -- Look for the associated private type declaration
12970 Partial_View := First_Entity (Current_Scope);
12972 exit when No (Partial_View)
12973 or else (Has_Private_Declaration (Partial_View)
12974 and then Full_View (Partial_View) = T);
12976 Next_Entity (Partial_View);
12979 -- If the partial view was not found then the source code has
12980 -- errors and the transformation is not needed.
12982 if Present (Partial_View) then
12983 Partial_View_Parent := Etype (Partial_View);
12985 -- If the parent of the full-view covers the parent of the
12986 -- partial-view we have nothing else to do.
12988 if Interface_Present_In_Ancestor
12989 (Parent_Type, Partial_View_Parent)
12993 -- Traverse the list of interfaces of the full-view to look
12994 -- for the parent of the partial-view and perform the tree
12998 Iface := First (Interface_List (Def));
12999 while Present (Iface) loop
13000 if Etype (Iface) = Etype (Partial_View) then
13001 Rewrite (Subtype_Indication (Def),
13002 New_Copy (Subtype_Indication
13003 (Parent (Partial_View))));
13005 New_Iface := Make_Identifier (Sloc (N),
13006 Chars (Parent_Type));
13007 Append (New_Iface, Interface_List (Def));
13009 -- Analyze the transformed code
13011 Derived_Type_Declaration (T, N, Is_Completion);
13022 -- Only composite types other than array types are allowed to have
13025 if Present (Discriminant_Specifications (N))
13026 and then (Is_Elementary_Type (Parent_Type)
13027 or else Is_Array_Type (Parent_Type))
13028 and then not Error_Posted (N)
13031 ("elementary or array type cannot have discriminants",
13032 Defining_Identifier (First (Discriminant_Specifications (N))));
13033 Set_Has_Discriminants (T, False);
13036 -- In Ada 83, a derived type defined in a package specification cannot
13037 -- be used for further derivation until the end of its visible part.
13038 -- Note that derivation in the private part of the package is allowed.
13040 if Ada_Version = Ada_83
13041 and then Is_Derived_Type (Parent_Type)
13042 and then In_Visible_Part (Scope (Parent_Type))
13044 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
13046 ("(Ada 83): premature use of type for derivation", Indic);
13050 -- Check for early use of incomplete or private type
13052 if Ekind (Parent_Type) = E_Void
13053 or else Ekind (Parent_Type) = E_Incomplete_Type
13055 Error_Msg_N ("premature derivation of incomplete type", Indic);
13058 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
13059 and then not Comes_From_Generic (Parent_Type))
13060 or else Has_Private_Component (Parent_Type)
13062 -- The ancestor type of a formal type can be incomplete, in which
13063 -- case only the operations of the partial view are available in
13064 -- the generic. Subsequent checks may be required when the full
13065 -- view is analyzed, to verify that derivation from a tagged type
13066 -- has an extension.
13068 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
13071 elsif No (Underlying_Type (Parent_Type))
13072 or else Has_Private_Component (Parent_Type)
13075 ("premature derivation of derived or private type", Indic);
13077 -- Flag the type itself as being in error, this prevents some
13078 -- nasty problems with subsequent uses of the malformed type.
13080 Set_Error_Posted (T);
13082 -- Check that within the immediate scope of an untagged partial
13083 -- view it's illegal to derive from the partial view if the
13084 -- full view is tagged. (7.3(7))
13086 -- We verify that the Parent_Type is a partial view by checking
13087 -- that it is not a Full_Type_Declaration (i.e. a private type or
13088 -- private extension declaration), to distinguish a partial view
13089 -- from a derivation from a private type which also appears as
13092 elsif Present (Full_View (Parent_Type))
13093 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
13094 and then not Is_Tagged_Type (Parent_Type)
13095 and then Is_Tagged_Type (Full_View (Parent_Type))
13097 Parent_Scope := Scope (T);
13098 while Present (Parent_Scope)
13099 and then Parent_Scope /= Standard_Standard
13101 if Parent_Scope = Scope (Parent_Type) then
13103 ("premature derivation from type with tagged full view",
13107 Parent_Scope := Scope (Parent_Scope);
13112 -- Check that form of derivation is appropriate
13114 Taggd := Is_Tagged_Type (Parent_Type);
13116 -- Perhaps the parent type should be changed to the class-wide type's
13117 -- specific type in this case to prevent cascading errors ???
13119 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
13120 Error_Msg_N ("parent type must not be a class-wide type", Indic);
13124 if Present (Extension) and then not Taggd then
13126 ("type derived from untagged type cannot have extension", Indic);
13128 elsif No (Extension) and then Taggd then
13130 -- If this declaration is within a private part (or body) of a
13131 -- generic instantiation then the derivation is allowed (the parent
13132 -- type can only appear tagged in this case if it's a generic actual
13133 -- type, since it would otherwise have been rejected in the analysis
13134 -- of the generic template).
13136 if not Is_Generic_Actual_Type (Parent_Type)
13137 or else In_Visible_Part (Scope (Parent_Type))
13140 ("type derived from tagged type must have extension", Indic);
13144 -- AI-443: Synchronized formal derived types require a private
13145 -- extension. There is no point in checking the ancestor type or
13146 -- the progenitors since the construct is wrong to begin with.
13148 if Ada_Version >= Ada_05
13149 and then Is_Generic_Type (T)
13150 and then Present (Original_Node (N))
13153 Decl : constant Node_Id := Original_Node (N);
13156 if Nkind (Decl) = N_Formal_Type_Declaration
13157 and then Nkind (Formal_Type_Definition (Decl)) =
13158 N_Formal_Derived_Type_Definition
13159 and then Synchronized_Present (Formal_Type_Definition (Decl))
13160 and then No (Extension)
13162 -- Avoid emitting a duplicate error message
13164 and then not Error_Posted (Indic)
13167 ("synchronized derived type must have extension", N);
13172 if Null_Exclusion_Present (Def)
13173 and then not Is_Access_Type (Parent_Type)
13175 Error_Msg_N ("null exclusion can only apply to an access type", N);
13178 -- Avoid deriving parent primitives of underlying record views
13180 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
13181 Derive_Subps => not Is_Underlying_Record_View (T));
13183 -- AI-419: The parent type of an explicitly limited derived type must
13184 -- be a limited type or a limited interface.
13186 if Limited_Present (Def) then
13187 Set_Is_Limited_Record (T);
13189 if Is_Interface (T) then
13190 Set_Is_Limited_Interface (T);
13193 if not Is_Limited_Type (Parent_Type)
13195 (not Is_Interface (Parent_Type)
13196 or else not Is_Limited_Interface (Parent_Type))
13198 Error_Msg_NE ("parent type& of limited type must be limited",
13202 end Derived_Type_Declaration;
13204 ------------------------
13205 -- Diagnose_Interface --
13206 ------------------------
13208 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
13210 if not Is_Interface (E)
13211 and then E /= Any_Type
13213 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
13215 end Diagnose_Interface;
13217 ----------------------------------
13218 -- Enumeration_Type_Declaration --
13219 ----------------------------------
13221 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13228 -- Create identifier node representing lower bound
13230 B_Node := New_Node (N_Identifier, Sloc (Def));
13231 L := First (Literals (Def));
13232 Set_Chars (B_Node, Chars (L));
13233 Set_Entity (B_Node, L);
13234 Set_Etype (B_Node, T);
13235 Set_Is_Static_Expression (B_Node, True);
13237 R_Node := New_Node (N_Range, Sloc (Def));
13238 Set_Low_Bound (R_Node, B_Node);
13240 Set_Ekind (T, E_Enumeration_Type);
13241 Set_First_Literal (T, L);
13243 Set_Is_Constrained (T);
13247 -- Loop through literals of enumeration type setting pos and rep values
13248 -- except that if the Ekind is already set, then it means that the
13249 -- literal was already constructed (case of a derived type declaration
13250 -- and we should not disturb the Pos and Rep values.
13252 while Present (L) loop
13253 if Ekind (L) /= E_Enumeration_Literal then
13254 Set_Ekind (L, E_Enumeration_Literal);
13255 Set_Enumeration_Pos (L, Ev);
13256 Set_Enumeration_Rep (L, Ev);
13257 Set_Is_Known_Valid (L, True);
13261 New_Overloaded_Entity (L);
13262 Generate_Definition (L);
13263 Set_Convention (L, Convention_Intrinsic);
13265 if Nkind (L) = N_Defining_Character_Literal then
13266 Set_Is_Character_Type (T, True);
13273 -- Now create a node representing upper bound
13275 B_Node := New_Node (N_Identifier, Sloc (Def));
13276 Set_Chars (B_Node, Chars (Last (Literals (Def))));
13277 Set_Entity (B_Node, Last (Literals (Def)));
13278 Set_Etype (B_Node, T);
13279 Set_Is_Static_Expression (B_Node, True);
13281 Set_High_Bound (R_Node, B_Node);
13283 -- Initialize various fields of the type. Some of this information
13284 -- may be overwritten later through rep.clauses.
13286 Set_Scalar_Range (T, R_Node);
13287 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
13288 Set_Enum_Esize (T);
13289 Set_Enum_Pos_To_Rep (T, Empty);
13291 -- Set Discard_Names if configuration pragma set, or if there is
13292 -- a parameterless pragma in the current declarative region
13294 if Global_Discard_Names
13295 or else Discard_Names (Scope (T))
13297 Set_Discard_Names (T);
13300 -- Process end label if there is one
13302 if Present (Def) then
13303 Process_End_Label (Def, 'e', T);
13305 end Enumeration_Type_Declaration;
13307 ---------------------------------
13308 -- Expand_To_Stored_Constraint --
13309 ---------------------------------
13311 function Expand_To_Stored_Constraint
13313 Constraint : Elist_Id) return Elist_Id
13315 Explicitly_Discriminated_Type : Entity_Id;
13316 Expansion : Elist_Id;
13317 Discriminant : Entity_Id;
13319 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
13320 -- Find the nearest type that actually specifies discriminants
13322 ---------------------------------
13323 -- Type_With_Explicit_Discrims --
13324 ---------------------------------
13326 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
13327 Typ : constant E := Base_Type (Id);
13330 if Ekind (Typ) in Incomplete_Or_Private_Kind then
13331 if Present (Full_View (Typ)) then
13332 return Type_With_Explicit_Discrims (Full_View (Typ));
13336 if Has_Discriminants (Typ) then
13341 if Etype (Typ) = Typ then
13343 elsif Has_Discriminants (Typ) then
13346 return Type_With_Explicit_Discrims (Etype (Typ));
13349 end Type_With_Explicit_Discrims;
13351 -- Start of processing for Expand_To_Stored_Constraint
13355 or else Is_Empty_Elmt_List (Constraint)
13360 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
13362 if No (Explicitly_Discriminated_Type) then
13366 Expansion := New_Elmt_List;
13369 First_Stored_Discriminant (Explicitly_Discriminated_Type);
13370 while Present (Discriminant) loop
13372 Get_Discriminant_Value (
13373 Discriminant, Explicitly_Discriminated_Type, Constraint),
13375 Next_Stored_Discriminant (Discriminant);
13379 end Expand_To_Stored_Constraint;
13381 ---------------------------
13382 -- Find_Hidden_Interface --
13383 ---------------------------
13385 function Find_Hidden_Interface
13387 Dest : Elist_Id) return Entity_Id
13390 Iface_Elmt : Elmt_Id;
13393 if Present (Src) and then Present (Dest) then
13394 Iface_Elmt := First_Elmt (Src);
13395 while Present (Iface_Elmt) loop
13396 Iface := Node (Iface_Elmt);
13398 if Is_Interface (Iface)
13399 and then not Contain_Interface (Iface, Dest)
13404 Next_Elmt (Iface_Elmt);
13409 end Find_Hidden_Interface;
13411 --------------------
13412 -- Find_Type_Name --
13413 --------------------
13415 function Find_Type_Name (N : Node_Id) return Entity_Id is
13416 Id : constant Entity_Id := Defining_Identifier (N);
13418 New_Id : Entity_Id;
13419 Prev_Par : Node_Id;
13421 procedure Tag_Mismatch;
13422 -- Diagnose a tagged partial view whose full view is untagged.
13423 -- We post the message on the full view, with a reference to
13424 -- the previous partial view. The partial view can be private
13425 -- or incomplete, and these are handled in a different manner,
13426 -- so we determine the position of the error message from the
13427 -- respective slocs of both.
13433 procedure Tag_Mismatch is
13435 if Sloc (Prev) < Sloc (Id) then
13437 ("full declaration of } must be a tagged type ", Id, Prev);
13440 ("full declaration of } must be a tagged type ", Prev, Id);
13444 -- Start of processing for Find_Type_Name
13447 -- Find incomplete declaration, if one was given
13449 Prev := Current_Entity_In_Scope (Id);
13451 if Present (Prev) then
13453 -- Previous declaration exists. Error if not incomplete/private case
13454 -- except if previous declaration is implicit, etc. Enter_Name will
13455 -- emit error if appropriate.
13457 Prev_Par := Parent (Prev);
13459 if not Is_Incomplete_Or_Private_Type (Prev) then
13463 elsif not Nkind_In (N, N_Full_Type_Declaration,
13464 N_Task_Type_Declaration,
13465 N_Protected_Type_Declaration)
13467 -- Completion must be a full type declarations (RM 7.3(4))
13469 Error_Msg_Sloc := Sloc (Prev);
13470 Error_Msg_NE ("invalid completion of }", Id, Prev);
13472 -- Set scope of Id to avoid cascaded errors. Entity is never
13473 -- examined again, except when saving globals in generics.
13475 Set_Scope (Id, Current_Scope);
13478 -- If this is a repeated incomplete declaration, no further
13479 -- checks are possible.
13481 if Nkind (N) = N_Incomplete_Type_Declaration then
13485 -- Case of full declaration of incomplete type
13487 elsif Ekind (Prev) = E_Incomplete_Type then
13489 -- Indicate that the incomplete declaration has a matching full
13490 -- declaration. The defining occurrence of the incomplete
13491 -- declaration remains the visible one, and the procedure
13492 -- Get_Full_View dereferences it whenever the type is used.
13494 if Present (Full_View (Prev)) then
13495 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13498 Set_Full_View (Prev, Id);
13499 Append_Entity (Id, Current_Scope);
13500 Set_Is_Public (Id, Is_Public (Prev));
13501 Set_Is_Internal (Id);
13504 -- Case of full declaration of private type
13507 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
13508 if Etype (Prev) /= Prev then
13510 -- Prev is a private subtype or a derived type, and needs
13513 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
13516 elsif Ekind (Prev) = E_Private_Type
13517 and then Nkind_In (N, N_Task_Type_Declaration,
13518 N_Protected_Type_Declaration)
13521 ("completion of nonlimited type cannot be limited", N);
13523 elsif Ekind (Prev) = E_Record_Type_With_Private
13524 and then Nkind_In (N, N_Task_Type_Declaration,
13525 N_Protected_Type_Declaration)
13527 if not Is_Limited_Record (Prev) then
13529 ("completion of nonlimited type cannot be limited", N);
13531 elsif No (Interface_List (N)) then
13533 ("completion of tagged private type must be tagged",
13537 elsif Nkind (N) = N_Full_Type_Declaration
13539 Nkind (Type_Definition (N)) = N_Record_Definition
13540 and then Interface_Present (Type_Definition (N))
13543 ("completion of private type cannot be an interface", N);
13546 -- Ada 2005 (AI-251): Private extension declaration of a task
13547 -- type or a protected type. This case arises when covering
13548 -- interface types.
13550 elsif Nkind_In (N, N_Task_Type_Declaration,
13551 N_Protected_Type_Declaration)
13555 elsif Nkind (N) /= N_Full_Type_Declaration
13556 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
13559 ("full view of private extension must be an extension", N);
13561 elsif not (Abstract_Present (Parent (Prev)))
13562 and then Abstract_Present (Type_Definition (N))
13565 ("full view of non-abstract extension cannot be abstract", N);
13568 if not In_Private_Part (Current_Scope) then
13570 ("declaration of full view must appear in private part", N);
13573 Copy_And_Swap (Prev, Id);
13574 Set_Has_Private_Declaration (Prev);
13575 Set_Has_Private_Declaration (Id);
13577 -- If no error, propagate freeze_node from private to full view.
13578 -- It may have been generated for an early operational item.
13580 if Present (Freeze_Node (Id))
13581 and then Serious_Errors_Detected = 0
13582 and then No (Full_View (Id))
13584 Set_Freeze_Node (Prev, Freeze_Node (Id));
13585 Set_Freeze_Node (Id, Empty);
13586 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
13589 Set_Full_View (Id, Prev);
13593 -- Verify that full declaration conforms to partial one
13595 if Is_Incomplete_Or_Private_Type (Prev)
13596 and then Present (Discriminant_Specifications (Prev_Par))
13598 if Present (Discriminant_Specifications (N)) then
13599 if Ekind (Prev) = E_Incomplete_Type then
13600 Check_Discriminant_Conformance (N, Prev, Prev);
13602 Check_Discriminant_Conformance (N, Prev, Id);
13607 ("missing discriminants in full type declaration", N);
13609 -- To avoid cascaded errors on subsequent use, share the
13610 -- discriminants of the partial view.
13612 Set_Discriminant_Specifications (N,
13613 Discriminant_Specifications (Prev_Par));
13617 -- A prior untagged partial view can have an associated class-wide
13618 -- type due to use of the class attribute, and in this case the full
13619 -- type must also be tagged. This Ada 95 usage is deprecated in favor
13620 -- of incomplete tagged declarations, but we check for it.
13623 and then (Is_Tagged_Type (Prev)
13624 or else Present (Class_Wide_Type (Prev)))
13626 -- The full declaration is either a tagged type (including
13627 -- a synchronized type that implements interfaces) or a
13628 -- type extension, otherwise this is an error.
13630 if Nkind_In (N, N_Task_Type_Declaration,
13631 N_Protected_Type_Declaration)
13633 if No (Interface_List (N))
13634 and then not Error_Posted (N)
13639 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
13641 -- Indicate that the previous declaration (tagged incomplete
13642 -- or private declaration) requires the same on the full one.
13644 if not Tagged_Present (Type_Definition (N)) then
13646 Set_Is_Tagged_Type (Id);
13647 Set_Primitive_Operations (Id, New_Elmt_List);
13650 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
13651 if No (Record_Extension_Part (Type_Definition (N))) then
13653 "full declaration of } must be a record extension",
13656 -- Set some attributes to produce a usable full view
13658 Set_Is_Tagged_Type (Id);
13659 Set_Primitive_Operations (Id, New_Elmt_List);
13670 -- New type declaration
13675 end Find_Type_Name;
13677 -------------------------
13678 -- Find_Type_Of_Object --
13679 -------------------------
13681 function Find_Type_Of_Object
13682 (Obj_Def : Node_Id;
13683 Related_Nod : Node_Id) return Entity_Id
13685 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
13686 P : Node_Id := Parent (Obj_Def);
13691 -- If the parent is a component_definition node we climb to the
13692 -- component_declaration node
13694 if Nkind (P) = N_Component_Definition then
13698 -- Case of an anonymous array subtype
13700 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
13701 N_Unconstrained_Array_Definition)
13704 Array_Type_Declaration (T, Obj_Def);
13706 -- Create an explicit subtype whenever possible
13708 elsif Nkind (P) /= N_Component_Declaration
13709 and then Def_Kind = N_Subtype_Indication
13711 -- Base name of subtype on object name, which will be unique in
13712 -- the current scope.
13714 -- If this is a duplicate declaration, return base type, to avoid
13715 -- generating duplicate anonymous types.
13717 if Error_Posted (P) then
13718 Analyze (Subtype_Mark (Obj_Def));
13719 return Entity (Subtype_Mark (Obj_Def));
13724 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
13726 T := Make_Defining_Identifier (Sloc (P), Nam);
13728 Insert_Action (Obj_Def,
13729 Make_Subtype_Declaration (Sloc (P),
13730 Defining_Identifier => T,
13731 Subtype_Indication => Relocate_Node (Obj_Def)));
13733 -- This subtype may need freezing, and this will not be done
13734 -- automatically if the object declaration is not in declarative
13735 -- part. Since this is an object declaration, the type cannot always
13736 -- be frozen here. Deferred constants do not freeze their type
13737 -- (which often enough will be private).
13739 if Nkind (P) = N_Object_Declaration
13740 and then Constant_Present (P)
13741 and then No (Expression (P))
13745 Insert_Actions (Obj_Def, Freeze_Entity (T, Sloc (P)));
13748 -- Ada 2005 AI-406: the object definition in an object declaration
13749 -- can be an access definition.
13751 elsif Def_Kind = N_Access_Definition then
13752 T := Access_Definition (Related_Nod, Obj_Def);
13753 Set_Is_Local_Anonymous_Access (T);
13755 -- Otherwise, the object definition is just a subtype_mark
13758 T := Process_Subtype (Obj_Def, Related_Nod);
13762 end Find_Type_Of_Object;
13764 --------------------------------
13765 -- Find_Type_Of_Subtype_Indic --
13766 --------------------------------
13768 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
13772 -- Case of subtype mark with a constraint
13774 if Nkind (S) = N_Subtype_Indication then
13775 Find_Type (Subtype_Mark (S));
13776 Typ := Entity (Subtype_Mark (S));
13779 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
13782 ("incorrect constraint for this kind of type", Constraint (S));
13783 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
13786 -- Otherwise we have a subtype mark without a constraint
13788 elsif Error_Posted (S) then
13789 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
13797 -- Check No_Wide_Characters restriction
13799 if Typ = Standard_Wide_Character
13800 or else Typ = Standard_Wide_Wide_Character
13801 or else Typ = Standard_Wide_String
13802 or else Typ = Standard_Wide_Wide_String
13804 Check_Restriction (No_Wide_Characters, S);
13808 end Find_Type_Of_Subtype_Indic;
13810 -------------------------------------
13811 -- Floating_Point_Type_Declaration --
13812 -------------------------------------
13814 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
13815 Digs : constant Node_Id := Digits_Expression (Def);
13817 Base_Typ : Entity_Id;
13818 Implicit_Base : Entity_Id;
13821 function Can_Derive_From (E : Entity_Id) return Boolean;
13822 -- Find if given digits value allows derivation from specified type
13824 ---------------------
13825 -- Can_Derive_From --
13826 ---------------------
13828 function Can_Derive_From (E : Entity_Id) return Boolean is
13829 Spec : constant Entity_Id := Real_Range_Specification (Def);
13832 if Digs_Val > Digits_Value (E) then
13836 if Present (Spec) then
13837 if Expr_Value_R (Type_Low_Bound (E)) >
13838 Expr_Value_R (Low_Bound (Spec))
13843 if Expr_Value_R (Type_High_Bound (E)) <
13844 Expr_Value_R (High_Bound (Spec))
13851 end Can_Derive_From;
13853 -- Start of processing for Floating_Point_Type_Declaration
13856 Check_Restriction (No_Floating_Point, Def);
13858 -- Create an implicit base type
13861 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
13863 -- Analyze and verify digits value
13865 Analyze_And_Resolve (Digs, Any_Integer);
13866 Check_Digits_Expression (Digs);
13867 Digs_Val := Expr_Value (Digs);
13869 -- Process possible range spec and find correct type to derive from
13871 Process_Real_Range_Specification (Def);
13873 if Can_Derive_From (Standard_Short_Float) then
13874 Base_Typ := Standard_Short_Float;
13875 elsif Can_Derive_From (Standard_Float) then
13876 Base_Typ := Standard_Float;
13877 elsif Can_Derive_From (Standard_Long_Float) then
13878 Base_Typ := Standard_Long_Float;
13879 elsif Can_Derive_From (Standard_Long_Long_Float) then
13880 Base_Typ := Standard_Long_Long_Float;
13882 -- If we can't derive from any existing type, use long_long_float
13883 -- and give appropriate message explaining the problem.
13886 Base_Typ := Standard_Long_Long_Float;
13888 if Digs_Val >= Digits_Value (Standard_Long_Long_Float) then
13889 Error_Msg_Uint_1 := Digits_Value (Standard_Long_Long_Float);
13890 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
13894 ("range too large for any predefined type",
13895 Real_Range_Specification (Def));
13899 -- If there are bounds given in the declaration use them as the bounds
13900 -- of the type, otherwise use the bounds of the predefined base type
13901 -- that was chosen based on the Digits value.
13903 if Present (Real_Range_Specification (Def)) then
13904 Set_Scalar_Range (T, Real_Range_Specification (Def));
13905 Set_Is_Constrained (T);
13907 -- The bounds of this range must be converted to machine numbers
13908 -- in accordance with RM 4.9(38).
13910 Bound := Type_Low_Bound (T);
13912 if Nkind (Bound) = N_Real_Literal then
13914 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
13915 Set_Is_Machine_Number (Bound);
13918 Bound := Type_High_Bound (T);
13920 if Nkind (Bound) = N_Real_Literal then
13922 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
13923 Set_Is_Machine_Number (Bound);
13927 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
13930 -- Complete definition of implicit base and declared first subtype
13932 Set_Etype (Implicit_Base, Base_Typ);
13934 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
13935 Set_Size_Info (Implicit_Base, (Base_Typ));
13936 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
13937 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
13938 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
13939 Set_Vax_Float (Implicit_Base, Vax_Float (Base_Typ));
13941 Set_Ekind (T, E_Floating_Point_Subtype);
13942 Set_Etype (T, Implicit_Base);
13944 Set_Size_Info (T, (Implicit_Base));
13945 Set_RM_Size (T, RM_Size (Implicit_Base));
13946 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
13947 Set_Digits_Value (T, Digs_Val);
13948 end Floating_Point_Type_Declaration;
13950 ----------------------------
13951 -- Get_Discriminant_Value --
13952 ----------------------------
13954 -- This is the situation:
13956 -- There is a non-derived type
13958 -- type T0 (Dx, Dy, Dz...)
13960 -- There are zero or more levels of derivation, with each derivation
13961 -- either purely inheriting the discriminants, or defining its own.
13963 -- type Ti is new Ti-1
13965 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
13967 -- subtype Ti is ...
13969 -- The subtype issue is avoided by the use of Original_Record_Component,
13970 -- and the fact that derived subtypes also derive the constraints.
13972 -- This chain leads back from
13974 -- Typ_For_Constraint
13976 -- Typ_For_Constraint has discriminants, and the value for each
13977 -- discriminant is given by its corresponding Elmt of Constraints.
13979 -- Discriminant is some discriminant in this hierarchy
13981 -- We need to return its value
13983 -- We do this by recursively searching each level, and looking for
13984 -- Discriminant. Once we get to the bottom, we start backing up
13985 -- returning the value for it which may in turn be a discriminant
13986 -- further up, so on the backup we continue the substitution.
13988 function Get_Discriminant_Value
13989 (Discriminant : Entity_Id;
13990 Typ_For_Constraint : Entity_Id;
13991 Constraint : Elist_Id) return Node_Id
13993 function Search_Derivation_Levels
13995 Discrim_Values : Elist_Id;
13996 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
13997 -- This is the routine that performs the recursive search of levels
13998 -- as described above.
14000 ------------------------------
14001 -- Search_Derivation_Levels --
14002 ------------------------------
14004 function Search_Derivation_Levels
14006 Discrim_Values : Elist_Id;
14007 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
14011 Result : Node_Or_Entity_Id;
14012 Result_Entity : Node_Id;
14015 -- If inappropriate type, return Error, this happens only in
14016 -- cascaded error situations, and we want to avoid a blow up.
14018 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
14022 -- Look deeper if possible. Use Stored_Constraints only for
14023 -- untagged types. For tagged types use the given constraint.
14024 -- This asymmetry needs explanation???
14026 if not Stored_Discrim_Values
14027 and then Present (Stored_Constraint (Ti))
14028 and then not Is_Tagged_Type (Ti)
14031 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
14034 Td : constant Entity_Id := Etype (Ti);
14038 Result := Discriminant;
14041 if Present (Stored_Constraint (Ti)) then
14043 Search_Derivation_Levels
14044 (Td, Stored_Constraint (Ti), True);
14047 Search_Derivation_Levels
14048 (Td, Discrim_Values, Stored_Discrim_Values);
14054 -- Extra underlying places to search, if not found above. For
14055 -- concurrent types, the relevant discriminant appears in the
14056 -- corresponding record. For a type derived from a private type
14057 -- without discriminant, the full view inherits the discriminants
14058 -- of the full view of the parent.
14060 if Result = Discriminant then
14061 if Is_Concurrent_Type (Ti)
14062 and then Present (Corresponding_Record_Type (Ti))
14065 Search_Derivation_Levels (
14066 Corresponding_Record_Type (Ti),
14068 Stored_Discrim_Values);
14070 elsif Is_Private_Type (Ti)
14071 and then not Has_Discriminants (Ti)
14072 and then Present (Full_View (Ti))
14073 and then Etype (Full_View (Ti)) /= Ti
14076 Search_Derivation_Levels (
14079 Stored_Discrim_Values);
14083 -- If Result is not a (reference to a) discriminant, return it,
14084 -- otherwise set Result_Entity to the discriminant.
14086 if Nkind (Result) = N_Defining_Identifier then
14087 pragma Assert (Result = Discriminant);
14088 Result_Entity := Result;
14091 if not Denotes_Discriminant (Result) then
14095 Result_Entity := Entity (Result);
14098 -- See if this level of derivation actually has discriminants
14099 -- because tagged derivations can add them, hence the lower
14100 -- levels need not have any.
14102 if not Has_Discriminants (Ti) then
14106 -- Scan Ti's discriminants for Result_Entity,
14107 -- and return its corresponding value, if any.
14109 Result_Entity := Original_Record_Component (Result_Entity);
14111 Assoc := First_Elmt (Discrim_Values);
14113 if Stored_Discrim_Values then
14114 Disc := First_Stored_Discriminant (Ti);
14116 Disc := First_Discriminant (Ti);
14119 while Present (Disc) loop
14120 pragma Assert (Present (Assoc));
14122 if Original_Record_Component (Disc) = Result_Entity then
14123 return Node (Assoc);
14128 if Stored_Discrim_Values then
14129 Next_Stored_Discriminant (Disc);
14131 Next_Discriminant (Disc);
14135 -- Could not find it
14138 end Search_Derivation_Levels;
14142 Result : Node_Or_Entity_Id;
14144 -- Start of processing for Get_Discriminant_Value
14147 -- ??? This routine is a gigantic mess and will be deleted. For the
14148 -- time being just test for the trivial case before calling recurse.
14150 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
14156 D := First_Discriminant (Typ_For_Constraint);
14157 E := First_Elmt (Constraint);
14158 while Present (D) loop
14159 if Chars (D) = Chars (Discriminant) then
14163 Next_Discriminant (D);
14169 Result := Search_Derivation_Levels
14170 (Typ_For_Constraint, Constraint, False);
14172 -- ??? hack to disappear when this routine is gone
14174 if Nkind (Result) = N_Defining_Identifier then
14180 D := First_Discriminant (Typ_For_Constraint);
14181 E := First_Elmt (Constraint);
14182 while Present (D) loop
14183 if Corresponding_Discriminant (D) = Discriminant then
14187 Next_Discriminant (D);
14193 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
14195 end Get_Discriminant_Value;
14197 --------------------------
14198 -- Has_Range_Constraint --
14199 --------------------------
14201 function Has_Range_Constraint (N : Node_Id) return Boolean is
14202 C : constant Node_Id := Constraint (N);
14205 if Nkind (C) = N_Range_Constraint then
14208 elsif Nkind (C) = N_Digits_Constraint then
14210 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
14212 Present (Range_Constraint (C));
14214 elsif Nkind (C) = N_Delta_Constraint then
14215 return Present (Range_Constraint (C));
14220 end Has_Range_Constraint;
14222 ------------------------
14223 -- Inherit_Components --
14224 ------------------------
14226 function Inherit_Components
14228 Parent_Base : Entity_Id;
14229 Derived_Base : Entity_Id;
14230 Is_Tagged : Boolean;
14231 Inherit_Discr : Boolean;
14232 Discs : Elist_Id) return Elist_Id
14234 Assoc_List : constant Elist_Id := New_Elmt_List;
14236 procedure Inherit_Component
14237 (Old_C : Entity_Id;
14238 Plain_Discrim : Boolean := False;
14239 Stored_Discrim : Boolean := False);
14240 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
14241 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
14242 -- True, Old_C is a stored discriminant. If they are both false then
14243 -- Old_C is a regular component.
14245 -----------------------
14246 -- Inherit_Component --
14247 -----------------------
14249 procedure Inherit_Component
14250 (Old_C : Entity_Id;
14251 Plain_Discrim : Boolean := False;
14252 Stored_Discrim : Boolean := False)
14254 New_C : constant Entity_Id := New_Copy (Old_C);
14256 Discrim : Entity_Id;
14257 Corr_Discrim : Entity_Id;
14260 pragma Assert (not Is_Tagged or else not Stored_Discrim);
14262 Set_Parent (New_C, Parent (Old_C));
14264 -- Regular discriminants and components must be inserted in the scope
14265 -- of the Derived_Base. Do it here.
14267 if not Stored_Discrim then
14268 Enter_Name (New_C);
14271 -- For tagged types the Original_Record_Component must point to
14272 -- whatever this field was pointing to in the parent type. This has
14273 -- already been achieved by the call to New_Copy above.
14275 if not Is_Tagged then
14276 Set_Original_Record_Component (New_C, New_C);
14279 -- If we have inherited a component then see if its Etype contains
14280 -- references to Parent_Base discriminants. In this case, replace
14281 -- these references with the constraints given in Discs. We do not
14282 -- do this for the partial view of private types because this is
14283 -- not needed (only the components of the full view will be used
14284 -- for code generation) and cause problem. We also avoid this
14285 -- transformation in some error situations.
14287 if Ekind (New_C) = E_Component then
14288 if (Is_Private_Type (Derived_Base)
14289 and then not Is_Generic_Type (Derived_Base))
14290 or else (Is_Empty_Elmt_List (Discs)
14291 and then not Expander_Active)
14293 Set_Etype (New_C, Etype (Old_C));
14296 -- The current component introduces a circularity of the
14299 -- limited with Pack_2;
14300 -- package Pack_1 is
14301 -- type T_1 is tagged record
14302 -- Comp : access Pack_2.T_2;
14308 -- package Pack_2 is
14309 -- type T_2 is new Pack_1.T_1 with ...;
14314 Constrain_Component_Type
14315 (Old_C, Derived_Base, N, Parent_Base, Discs));
14319 -- In derived tagged types it is illegal to reference a non
14320 -- discriminant component in the parent type. To catch this, mark
14321 -- these components with an Ekind of E_Void. This will be reset in
14322 -- Record_Type_Definition after processing the record extension of
14323 -- the derived type.
14325 -- If the declaration is a private extension, there is no further
14326 -- record extension to process, and the components retain their
14327 -- current kind, because they are visible at this point.
14329 if Is_Tagged and then Ekind (New_C) = E_Component
14330 and then Nkind (N) /= N_Private_Extension_Declaration
14332 Set_Ekind (New_C, E_Void);
14335 if Plain_Discrim then
14336 Set_Corresponding_Discriminant (New_C, Old_C);
14337 Build_Discriminal (New_C);
14339 -- If we are explicitly inheriting a stored discriminant it will be
14340 -- completely hidden.
14342 elsif Stored_Discrim then
14343 Set_Corresponding_Discriminant (New_C, Empty);
14344 Set_Discriminal (New_C, Empty);
14345 Set_Is_Completely_Hidden (New_C);
14347 -- Set the Original_Record_Component of each discriminant in the
14348 -- derived base to point to the corresponding stored that we just
14351 Discrim := First_Discriminant (Derived_Base);
14352 while Present (Discrim) loop
14353 Corr_Discrim := Corresponding_Discriminant (Discrim);
14355 -- Corr_Discrim could be missing in an error situation
14357 if Present (Corr_Discrim)
14358 and then Original_Record_Component (Corr_Discrim) = Old_C
14360 Set_Original_Record_Component (Discrim, New_C);
14363 Next_Discriminant (Discrim);
14366 Append_Entity (New_C, Derived_Base);
14369 if not Is_Tagged then
14370 Append_Elmt (Old_C, Assoc_List);
14371 Append_Elmt (New_C, Assoc_List);
14373 end Inherit_Component;
14375 -- Variables local to Inherit_Component
14377 Loc : constant Source_Ptr := Sloc (N);
14379 Parent_Discrim : Entity_Id;
14380 Stored_Discrim : Entity_Id;
14382 Component : Entity_Id;
14384 -- Start of processing for Inherit_Components
14387 if not Is_Tagged then
14388 Append_Elmt (Parent_Base, Assoc_List);
14389 Append_Elmt (Derived_Base, Assoc_List);
14392 -- Inherit parent discriminants if needed
14394 if Inherit_Discr then
14395 Parent_Discrim := First_Discriminant (Parent_Base);
14396 while Present (Parent_Discrim) loop
14397 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
14398 Next_Discriminant (Parent_Discrim);
14402 -- Create explicit stored discrims for untagged types when necessary
14404 if not Has_Unknown_Discriminants (Derived_Base)
14405 and then Has_Discriminants (Parent_Base)
14406 and then not Is_Tagged
14409 or else First_Discriminant (Parent_Base) /=
14410 First_Stored_Discriminant (Parent_Base))
14412 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
14413 while Present (Stored_Discrim) loop
14414 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
14415 Next_Stored_Discriminant (Stored_Discrim);
14419 -- See if we can apply the second transformation for derived types, as
14420 -- explained in point 6. in the comments above Build_Derived_Record_Type
14421 -- This is achieved by appending Derived_Base discriminants into Discs,
14422 -- which has the side effect of returning a non empty Discs list to the
14423 -- caller of Inherit_Components, which is what we want. This must be
14424 -- done for private derived types if there are explicit stored
14425 -- discriminants, to ensure that we can retrieve the values of the
14426 -- constraints provided in the ancestors.
14429 and then Is_Empty_Elmt_List (Discs)
14430 and then Present (First_Discriminant (Derived_Base))
14432 (not Is_Private_Type (Derived_Base)
14433 or else Is_Completely_Hidden
14434 (First_Stored_Discriminant (Derived_Base))
14435 or else Is_Generic_Type (Derived_Base))
14437 D := First_Discriminant (Derived_Base);
14438 while Present (D) loop
14439 Append_Elmt (New_Reference_To (D, Loc), Discs);
14440 Next_Discriminant (D);
14444 -- Finally, inherit non-discriminant components unless they are not
14445 -- visible because defined or inherited from the full view of the
14446 -- parent. Don't inherit the _parent field of the parent type.
14448 Component := First_Entity (Parent_Base);
14449 while Present (Component) loop
14451 -- Ada 2005 (AI-251): Do not inherit components associated with
14452 -- secondary tags of the parent.
14454 if Ekind (Component) = E_Component
14455 and then Present (Related_Type (Component))
14459 elsif Ekind (Component) /= E_Component
14460 or else Chars (Component) = Name_uParent
14464 -- If the derived type is within the parent type's declarative
14465 -- region, then the components can still be inherited even though
14466 -- they aren't visible at this point. This can occur for cases
14467 -- such as within public child units where the components must
14468 -- become visible upon entering the child unit's private part.
14470 elsif not Is_Visible_Component (Component)
14471 and then not In_Open_Scopes (Scope (Parent_Base))
14475 elsif Ekind (Derived_Base) = E_Private_Type
14476 or else Ekind (Derived_Base) = E_Limited_Private_Type
14481 Inherit_Component (Component);
14484 Next_Entity (Component);
14487 -- For tagged derived types, inherited discriminants cannot be used in
14488 -- component declarations of the record extension part. To achieve this
14489 -- we mark the inherited discriminants as not visible.
14491 if Is_Tagged and then Inherit_Discr then
14492 D := First_Discriminant (Derived_Base);
14493 while Present (D) loop
14494 Set_Is_Immediately_Visible (D, False);
14495 Next_Discriminant (D);
14500 end Inherit_Components;
14502 -----------------------
14503 -- Is_Null_Extension --
14504 -----------------------
14506 function Is_Null_Extension (T : Entity_Id) return Boolean is
14507 Type_Decl : constant Node_Id := Parent (T);
14508 Comp_List : Node_Id;
14512 if Nkind (Type_Decl) /= N_Full_Type_Declaration
14513 or else not Is_Tagged_Type (T)
14514 or else Nkind (Type_Definition (Type_Decl)) /=
14515 N_Derived_Type_Definition
14516 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
14522 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
14524 if Present (Discriminant_Specifications (Type_Decl)) then
14527 elsif Present (Comp_List)
14528 and then Is_Non_Empty_List (Component_Items (Comp_List))
14530 Comp := First (Component_Items (Comp_List));
14532 -- Only user-defined components are relevant. The component list
14533 -- may also contain a parent component and internal components
14534 -- corresponding to secondary tags, but these do not determine
14535 -- whether this is a null extension.
14537 while Present (Comp) loop
14538 if Comes_From_Source (Comp) then
14549 end Is_Null_Extension;
14551 --------------------
14552 -- Is_Progenitor --
14553 --------------------
14555 function Is_Progenitor
14556 (Iface : Entity_Id;
14557 Typ : Entity_Id) return Boolean
14560 return Implements_Interface (Typ, Iface,
14561 Exclude_Parents => True);
14564 ------------------------------
14565 -- Is_Valid_Constraint_Kind --
14566 ------------------------------
14568 function Is_Valid_Constraint_Kind
14569 (T_Kind : Type_Kind;
14570 Constraint_Kind : Node_Kind) return Boolean
14574 when Enumeration_Kind |
14576 return Constraint_Kind = N_Range_Constraint;
14578 when Decimal_Fixed_Point_Kind =>
14579 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14580 N_Range_Constraint);
14582 when Ordinary_Fixed_Point_Kind =>
14583 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
14584 N_Range_Constraint);
14587 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
14588 N_Range_Constraint);
14595 E_Incomplete_Type |
14598 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
14601 return True; -- Error will be detected later
14603 end Is_Valid_Constraint_Kind;
14605 --------------------------
14606 -- Is_Visible_Component --
14607 --------------------------
14609 function Is_Visible_Component (C : Entity_Id) return Boolean is
14610 Original_Comp : Entity_Id := Empty;
14611 Original_Scope : Entity_Id;
14612 Type_Scope : Entity_Id;
14614 function Is_Local_Type (Typ : Entity_Id) return Boolean;
14615 -- Check whether parent type of inherited component is declared locally,
14616 -- possibly within a nested package or instance. The current scope is
14617 -- the derived record itself.
14619 -------------------
14620 -- Is_Local_Type --
14621 -------------------
14623 function Is_Local_Type (Typ : Entity_Id) return Boolean is
14627 Scop := Scope (Typ);
14628 while Present (Scop)
14629 and then Scop /= Standard_Standard
14631 if Scop = Scope (Current_Scope) then
14635 Scop := Scope (Scop);
14641 -- Start of processing for Is_Visible_Component
14644 if Ekind (C) = E_Component
14645 or else Ekind (C) = E_Discriminant
14647 Original_Comp := Original_Record_Component (C);
14650 if No (Original_Comp) then
14652 -- Premature usage, or previous error
14657 Original_Scope := Scope (Original_Comp);
14658 Type_Scope := Scope (Base_Type (Scope (C)));
14661 -- This test only concerns tagged types
14663 if not Is_Tagged_Type (Original_Scope) then
14666 -- If it is _Parent or _Tag, there is no visibility issue
14668 elsif not Comes_From_Source (Original_Comp) then
14671 -- If we are in the body of an instantiation, the component is visible
14672 -- even when the parent type (possibly defined in an enclosing unit or
14673 -- in a parent unit) might not.
14675 elsif In_Instance_Body then
14678 -- Discriminants are always visible
14680 elsif Ekind (Original_Comp) = E_Discriminant
14681 and then not Has_Unknown_Discriminants (Original_Scope)
14685 -- If the component has been declared in an ancestor which is currently
14686 -- a private type, then it is not visible. The same applies if the
14687 -- component's containing type is not in an open scope and the original
14688 -- component's enclosing type is a visible full view of a private type
14689 -- (which can occur in cases where an attempt is being made to reference
14690 -- a component in a sibling package that is inherited from a visible
14691 -- component of a type in an ancestor package; the component in the
14692 -- sibling package should not be visible even though the component it
14693 -- inherited from is visible). This does not apply however in the case
14694 -- where the scope of the type is a private child unit, or when the
14695 -- parent comes from a local package in which the ancestor is currently
14696 -- visible. The latter suppression of visibility is needed for cases
14697 -- that are tested in B730006.
14699 elsif Is_Private_Type (Original_Scope)
14701 (not Is_Private_Descendant (Type_Scope)
14702 and then not In_Open_Scopes (Type_Scope)
14703 and then Has_Private_Declaration (Original_Scope))
14705 -- If the type derives from an entity in a formal package, there
14706 -- are no additional visible components.
14708 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
14709 N_Formal_Package_Declaration
14713 -- if we are not in the private part of the current package, there
14714 -- are no additional visible components.
14716 elsif Ekind (Scope (Current_Scope)) = E_Package
14717 and then not In_Private_Part (Scope (Current_Scope))
14722 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
14723 and then In_Open_Scopes (Scope (Original_Scope))
14724 and then Is_Local_Type (Type_Scope);
14727 -- There is another weird way in which a component may be invisible
14728 -- when the private and the full view are not derived from the same
14729 -- ancestor. Here is an example :
14731 -- type A1 is tagged record F1 : integer; end record;
14732 -- type A2 is new A1 with record F2 : integer; end record;
14733 -- type T is new A1 with private;
14735 -- type T is new A2 with null record;
14737 -- In this case, the full view of T inherits F1 and F2 but the private
14738 -- view inherits only F1
14742 Ancestor : Entity_Id := Scope (C);
14746 if Ancestor = Original_Scope then
14748 elsif Ancestor = Etype (Ancestor) then
14752 Ancestor := Etype (Ancestor);
14756 end Is_Visible_Component;
14758 --------------------------
14759 -- Make_Class_Wide_Type --
14760 --------------------------
14762 procedure Make_Class_Wide_Type (T : Entity_Id) is
14763 CW_Type : Entity_Id;
14765 Next_E : Entity_Id;
14768 -- The class wide type can have been defined by the partial view, in
14769 -- which case everything is already done.
14771 if Present (Class_Wide_Type (T)) then
14776 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
14778 -- Inherit root type characteristics
14780 CW_Name := Chars (CW_Type);
14781 Next_E := Next_Entity (CW_Type);
14782 Copy_Node (T, CW_Type);
14783 Set_Comes_From_Source (CW_Type, False);
14784 Set_Chars (CW_Type, CW_Name);
14785 Set_Parent (CW_Type, Parent (T));
14786 Set_Next_Entity (CW_Type, Next_E);
14788 -- Ensure we have a new freeze node for the class-wide type. The partial
14789 -- view may have freeze action of its own, requiring a proper freeze
14790 -- node, and the same freeze node cannot be shared between the two
14793 Set_Has_Delayed_Freeze (CW_Type);
14794 Set_Freeze_Node (CW_Type, Empty);
14796 -- Customize the class-wide type: It has no prim. op., it cannot be
14797 -- abstract and its Etype points back to the specific root type.
14799 Set_Ekind (CW_Type, E_Class_Wide_Type);
14800 Set_Is_Tagged_Type (CW_Type, True);
14801 Set_Primitive_Operations (CW_Type, New_Elmt_List);
14802 Set_Is_Abstract_Type (CW_Type, False);
14803 Set_Is_Constrained (CW_Type, False);
14804 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
14806 if Ekind (T) = E_Class_Wide_Subtype then
14807 Set_Etype (CW_Type, Etype (Base_Type (T)));
14809 Set_Etype (CW_Type, T);
14812 -- If this is the class_wide type of a constrained subtype, it does
14813 -- not have discriminants.
14815 Set_Has_Discriminants (CW_Type,
14816 Has_Discriminants (T) and then not Is_Constrained (T));
14818 Set_Has_Unknown_Discriminants (CW_Type, True);
14819 Set_Class_Wide_Type (T, CW_Type);
14820 Set_Equivalent_Type (CW_Type, Empty);
14822 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
14824 Set_Class_Wide_Type (CW_Type, CW_Type);
14825 end Make_Class_Wide_Type;
14831 procedure Make_Index
14833 Related_Nod : Node_Id;
14834 Related_Id : Entity_Id := Empty;
14835 Suffix_Index : Nat := 1)
14839 Def_Id : Entity_Id := Empty;
14840 Found : Boolean := False;
14843 -- For a discrete range used in a constrained array definition and
14844 -- defined by a range, an implicit conversion to the predefined type
14845 -- INTEGER is assumed if each bound is either a numeric literal, a named
14846 -- number, or an attribute, and the type of both bounds (prior to the
14847 -- implicit conversion) is the type universal_integer. Otherwise, both
14848 -- bounds must be of the same discrete type, other than universal
14849 -- integer; this type must be determinable independently of the
14850 -- context, but using the fact that the type must be discrete and that
14851 -- both bounds must have the same type.
14853 -- Character literals also have a universal type in the absence of
14854 -- of additional context, and are resolved to Standard_Character.
14856 if Nkind (I) = N_Range then
14858 -- The index is given by a range constraint. The bounds are known
14859 -- to be of a consistent type.
14861 if not Is_Overloaded (I) then
14864 -- For universal bounds, choose the specific predefined type
14866 if T = Universal_Integer then
14867 T := Standard_Integer;
14869 elsif T = Any_Character then
14870 Ambiguous_Character (Low_Bound (I));
14872 T := Standard_Character;
14875 -- The node may be overloaded because some user-defined operators
14876 -- are available, but if a universal interpretation exists it is
14877 -- also the selected one.
14879 elsif Universal_Interpretation (I) = Universal_Integer then
14880 T := Standard_Integer;
14886 Ind : Interp_Index;
14890 Get_First_Interp (I, Ind, It);
14891 while Present (It.Typ) loop
14892 if Is_Discrete_Type (It.Typ) then
14895 and then not Covers (It.Typ, T)
14896 and then not Covers (T, It.Typ)
14898 Error_Msg_N ("ambiguous bounds in discrete range", I);
14906 Get_Next_Interp (Ind, It);
14909 if T = Any_Type then
14910 Error_Msg_N ("discrete type required for range", I);
14911 Set_Etype (I, Any_Type);
14914 elsif T = Universal_Integer then
14915 T := Standard_Integer;
14920 if not Is_Discrete_Type (T) then
14921 Error_Msg_N ("discrete type required for range", I);
14922 Set_Etype (I, Any_Type);
14926 if Nkind (Low_Bound (I)) = N_Attribute_Reference
14927 and then Attribute_Name (Low_Bound (I)) = Name_First
14928 and then Is_Entity_Name (Prefix (Low_Bound (I)))
14929 and then Is_Type (Entity (Prefix (Low_Bound (I))))
14930 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
14932 -- The type of the index will be the type of the prefix, as long
14933 -- as the upper bound is 'Last of the same type.
14935 Def_Id := Entity (Prefix (Low_Bound (I)));
14937 if Nkind (High_Bound (I)) /= N_Attribute_Reference
14938 or else Attribute_Name (High_Bound (I)) /= Name_Last
14939 or else not Is_Entity_Name (Prefix (High_Bound (I)))
14940 or else Entity (Prefix (High_Bound (I))) /= Def_Id
14947 Process_Range_Expr_In_Decl (R, T);
14949 elsif Nkind (I) = N_Subtype_Indication then
14951 -- The index is given by a subtype with a range constraint
14953 T := Base_Type (Entity (Subtype_Mark (I)));
14955 if not Is_Discrete_Type (T) then
14956 Error_Msg_N ("discrete type required for range", I);
14957 Set_Etype (I, Any_Type);
14961 R := Range_Expression (Constraint (I));
14964 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
14966 elsif Nkind (I) = N_Attribute_Reference then
14968 -- The parser guarantees that the attribute is a RANGE attribute
14970 -- If the node denotes the range of a type mark, that is also the
14971 -- resulting type, and we do no need to create an Itype for it.
14973 if Is_Entity_Name (Prefix (I))
14974 and then Comes_From_Source (I)
14975 and then Is_Type (Entity (Prefix (I)))
14976 and then Is_Discrete_Type (Entity (Prefix (I)))
14978 Def_Id := Entity (Prefix (I));
14981 Analyze_And_Resolve (I);
14985 -- If none of the above, must be a subtype. We convert this to a
14986 -- range attribute reference because in the case of declared first
14987 -- named subtypes, the types in the range reference can be different
14988 -- from the type of the entity. A range attribute normalizes the
14989 -- reference and obtains the correct types for the bounds.
14991 -- This transformation is in the nature of an expansion, is only
14992 -- done if expansion is active. In particular, it is not done on
14993 -- formal generic types, because we need to retain the name of the
14994 -- original index for instantiation purposes.
14997 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
14998 Error_Msg_N ("invalid subtype mark in discrete range ", I);
14999 Set_Etype (I, Any_Integer);
15003 -- The type mark may be that of an incomplete type. It is only
15004 -- now that we can get the full view, previous analysis does
15005 -- not look specifically for a type mark.
15007 Set_Entity (I, Get_Full_View (Entity (I)));
15008 Set_Etype (I, Entity (I));
15009 Def_Id := Entity (I);
15011 if not Is_Discrete_Type (Def_Id) then
15012 Error_Msg_N ("discrete type required for index", I);
15013 Set_Etype (I, Any_Type);
15018 if Expander_Active then
15020 Make_Attribute_Reference (Sloc (I),
15021 Attribute_Name => Name_Range,
15022 Prefix => Relocate_Node (I)));
15024 -- The original was a subtype mark that does not freeze. This
15025 -- means that the rewritten version must not freeze either.
15027 Set_Must_Not_Freeze (I);
15028 Set_Must_Not_Freeze (Prefix (I));
15030 -- Is order critical??? if so, document why, if not
15031 -- use Analyze_And_Resolve
15033 Analyze_And_Resolve (I);
15037 -- If expander is inactive, type is legal, nothing else to construct
15044 if not Is_Discrete_Type (T) then
15045 Error_Msg_N ("discrete type required for range", I);
15046 Set_Etype (I, Any_Type);
15049 elsif T = Any_Type then
15050 Set_Etype (I, Any_Type);
15054 -- We will now create the appropriate Itype to describe the range, but
15055 -- first a check. If we originally had a subtype, then we just label
15056 -- the range with this subtype. Not only is there no need to construct
15057 -- a new subtype, but it is wrong to do so for two reasons:
15059 -- 1. A legality concern, if we have a subtype, it must not freeze,
15060 -- and the Itype would cause freezing incorrectly
15062 -- 2. An efficiency concern, if we created an Itype, it would not be
15063 -- recognized as the same type for the purposes of eliminating
15064 -- checks in some circumstances.
15066 -- We signal this case by setting the subtype entity in Def_Id
15068 if No (Def_Id) then
15070 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
15071 Set_Etype (Def_Id, Base_Type (T));
15073 if Is_Signed_Integer_Type (T) then
15074 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
15076 elsif Is_Modular_Integer_Type (T) then
15077 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
15080 Set_Ekind (Def_Id, E_Enumeration_Subtype);
15081 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
15082 Set_First_Literal (Def_Id, First_Literal (T));
15085 Set_Size_Info (Def_Id, (T));
15086 Set_RM_Size (Def_Id, RM_Size (T));
15087 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
15089 Set_Scalar_Range (Def_Id, R);
15090 Conditional_Delay (Def_Id, T);
15092 -- In the subtype indication case, if the immediate parent of the
15093 -- new subtype is non-static, then the subtype we create is non-
15094 -- static, even if its bounds are static.
15096 if Nkind (I) = N_Subtype_Indication
15097 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
15099 Set_Is_Non_Static_Subtype (Def_Id);
15103 -- Final step is to label the index with this constructed type
15105 Set_Etype (I, Def_Id);
15108 ------------------------------
15109 -- Modular_Type_Declaration --
15110 ------------------------------
15112 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15113 Mod_Expr : constant Node_Id := Expression (Def);
15116 procedure Set_Modular_Size (Bits : Int);
15117 -- Sets RM_Size to Bits, and Esize to normal word size above this
15119 ----------------------
15120 -- Set_Modular_Size --
15121 ----------------------
15123 procedure Set_Modular_Size (Bits : Int) is
15125 Set_RM_Size (T, UI_From_Int (Bits));
15130 elsif Bits <= 16 then
15131 Init_Esize (T, 16);
15133 elsif Bits <= 32 then
15134 Init_Esize (T, 32);
15137 Init_Esize (T, System_Max_Binary_Modulus_Power);
15140 if not Non_Binary_Modulus (T)
15141 and then Esize (T) = RM_Size (T)
15143 Set_Is_Known_Valid (T);
15145 end Set_Modular_Size;
15147 -- Start of processing for Modular_Type_Declaration
15150 Analyze_And_Resolve (Mod_Expr, Any_Integer);
15152 Set_Ekind (T, E_Modular_Integer_Type);
15153 Init_Alignment (T);
15154 Set_Is_Constrained (T);
15156 if not Is_OK_Static_Expression (Mod_Expr) then
15157 Flag_Non_Static_Expr
15158 ("non-static expression used for modular type bound!", Mod_Expr);
15159 M_Val := 2 ** System_Max_Binary_Modulus_Power;
15161 M_Val := Expr_Value (Mod_Expr);
15165 Error_Msg_N ("modulus value must be positive", Mod_Expr);
15166 M_Val := 2 ** System_Max_Binary_Modulus_Power;
15169 Set_Modulus (T, M_Val);
15171 -- Create bounds for the modular type based on the modulus given in
15172 -- the type declaration and then analyze and resolve those bounds.
15174 Set_Scalar_Range (T,
15175 Make_Range (Sloc (Mod_Expr),
15177 Make_Integer_Literal (Sloc (Mod_Expr), 0),
15179 Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
15181 -- Properly analyze the literals for the range. We do this manually
15182 -- because we can't go calling Resolve, since we are resolving these
15183 -- bounds with the type, and this type is certainly not complete yet!
15185 Set_Etype (Low_Bound (Scalar_Range (T)), T);
15186 Set_Etype (High_Bound (Scalar_Range (T)), T);
15187 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
15188 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
15190 -- Loop through powers of two to find number of bits required
15192 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
15196 if M_Val = 2 ** Bits then
15197 Set_Modular_Size (Bits);
15202 elsif M_Val < 2 ** Bits then
15203 Set_Non_Binary_Modulus (T);
15205 if Bits > System_Max_Nonbinary_Modulus_Power then
15206 Error_Msg_Uint_1 :=
15207 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
15209 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
15210 Set_Modular_Size (System_Max_Binary_Modulus_Power);
15214 -- In the non-binary case, set size as per RM 13.3(55)
15216 Set_Modular_Size (Bits);
15223 -- If we fall through, then the size exceed System.Max_Binary_Modulus
15224 -- so we just signal an error and set the maximum size.
15226 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
15227 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
15229 Set_Modular_Size (System_Max_Binary_Modulus_Power);
15230 Init_Alignment (T);
15232 end Modular_Type_Declaration;
15234 --------------------------
15235 -- New_Concatenation_Op --
15236 --------------------------
15238 procedure New_Concatenation_Op (Typ : Entity_Id) is
15239 Loc : constant Source_Ptr := Sloc (Typ);
15242 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
15243 -- Create abbreviated declaration for the formal of a predefined
15244 -- Operator 'Op' of type 'Typ'
15246 --------------------
15247 -- Make_Op_Formal --
15248 --------------------
15250 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
15251 Formal : Entity_Id;
15253 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
15254 Set_Etype (Formal, Typ);
15255 Set_Mechanism (Formal, Default_Mechanism);
15257 end Make_Op_Formal;
15259 -- Start of processing for New_Concatenation_Op
15262 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
15264 Set_Ekind (Op, E_Operator);
15265 Set_Scope (Op, Current_Scope);
15266 Set_Etype (Op, Typ);
15267 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
15268 Set_Is_Immediately_Visible (Op);
15269 Set_Is_Intrinsic_Subprogram (Op);
15270 Set_Has_Completion (Op);
15271 Append_Entity (Op, Current_Scope);
15273 Set_Name_Entity_Id (Name_Op_Concat, Op);
15275 Append_Entity (Make_Op_Formal (Typ, Op), Op);
15276 Append_Entity (Make_Op_Formal (Typ, Op), Op);
15277 end New_Concatenation_Op;
15279 -------------------------
15280 -- OK_For_Limited_Init --
15281 -------------------------
15283 -- ???Check all calls of this, and compare the conditions under which it's
15286 function OK_For_Limited_Init (Exp : Node_Id) return Boolean is
15288 return Is_CPP_Constructor_Call (Exp)
15289 or else (Ada_Version >= Ada_05
15290 and then not Debug_Flag_Dot_L
15291 and then OK_For_Limited_Init_In_05 (Exp));
15292 end OK_For_Limited_Init;
15294 -------------------------------
15295 -- OK_For_Limited_Init_In_05 --
15296 -------------------------------
15298 function OK_For_Limited_Init_In_05 (Exp : Node_Id) return Boolean is
15300 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
15301 -- case of limited aggregates (including extension aggregates), and
15302 -- function calls. The function call may have been give in prefixed
15303 -- notation, in which case the original node is an indexed component.
15305 case Nkind (Original_Node (Exp)) is
15306 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
15309 when N_Qualified_Expression =>
15311 OK_For_Limited_Init_In_05 (Expression (Original_Node (Exp)));
15313 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
15314 -- with a function call, the expander has rewritten the call into an
15315 -- N_Type_Conversion node to force displacement of the pointer to
15316 -- reference the component containing the secondary dispatch table.
15317 -- Otherwise a type conversion is not a legal context.
15318 -- A return statement for a build-in-place function returning a
15319 -- synchronized type also introduces an unchecked conversion.
15321 when N_Type_Conversion | N_Unchecked_Type_Conversion =>
15322 return not Comes_From_Source (Exp)
15324 OK_For_Limited_Init_In_05 (Expression (Original_Node (Exp)));
15326 when N_Indexed_Component | N_Selected_Component =>
15327 return Nkind (Exp) = N_Function_Call;
15329 -- A use of 'Input is a function call, hence allowed. Normally the
15330 -- attribute will be changed to a call, but the attribute by itself
15331 -- can occur with -gnatc.
15333 when N_Attribute_Reference =>
15334 return Attribute_Name (Original_Node (Exp)) = Name_Input;
15339 end OK_For_Limited_Init_In_05;
15341 -------------------------------------------
15342 -- Ordinary_Fixed_Point_Type_Declaration --
15343 -------------------------------------------
15345 procedure Ordinary_Fixed_Point_Type_Declaration
15349 Loc : constant Source_Ptr := Sloc (Def);
15350 Delta_Expr : constant Node_Id := Delta_Expression (Def);
15351 RRS : constant Node_Id := Real_Range_Specification (Def);
15352 Implicit_Base : Entity_Id;
15359 Check_Restriction (No_Fixed_Point, Def);
15361 -- Create implicit base type
15364 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
15365 Set_Etype (Implicit_Base, Implicit_Base);
15367 -- Analyze and process delta expression
15369 Analyze_And_Resolve (Delta_Expr, Any_Real);
15371 Check_Delta_Expression (Delta_Expr);
15372 Delta_Val := Expr_Value_R (Delta_Expr);
15374 Set_Delta_Value (Implicit_Base, Delta_Val);
15376 -- Compute default small from given delta, which is the largest power
15377 -- of two that does not exceed the given delta value.
15387 if Delta_Val < Ureal_1 then
15388 while Delta_Val < Tmp loop
15389 Tmp := Tmp / Ureal_2;
15390 Scale := Scale + 1;
15395 Tmp := Tmp * Ureal_2;
15396 exit when Tmp > Delta_Val;
15397 Scale := Scale - 1;
15401 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
15404 Set_Small_Value (Implicit_Base, Small_Val);
15406 -- If no range was given, set a dummy range
15408 if RRS <= Empty_Or_Error then
15409 Low_Val := -Small_Val;
15410 High_Val := Small_Val;
15412 -- Otherwise analyze and process given range
15416 Low : constant Node_Id := Low_Bound (RRS);
15417 High : constant Node_Id := High_Bound (RRS);
15420 Analyze_And_Resolve (Low, Any_Real);
15421 Analyze_And_Resolve (High, Any_Real);
15422 Check_Real_Bound (Low);
15423 Check_Real_Bound (High);
15425 -- Obtain and set the range
15427 Low_Val := Expr_Value_R (Low);
15428 High_Val := Expr_Value_R (High);
15430 if Low_Val > High_Val then
15431 Error_Msg_NE ("?fixed point type& has null range", Def, T);
15436 -- The range for both the implicit base and the declared first subtype
15437 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
15438 -- set a temporary range in place. Note that the bounds of the base
15439 -- type will be widened to be symmetrical and to fill the available
15440 -- bits when the type is frozen.
15442 -- We could do this with all discrete types, and probably should, but
15443 -- we absolutely have to do it for fixed-point, since the end-points
15444 -- of the range and the size are determined by the small value, which
15445 -- could be reset before the freeze point.
15447 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
15448 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15450 -- Complete definition of first subtype
15452 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
15453 Set_Etype (T, Implicit_Base);
15454 Init_Size_Align (T);
15455 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15456 Set_Small_Value (T, Small_Val);
15457 Set_Delta_Value (T, Delta_Val);
15458 Set_Is_Constrained (T);
15460 end Ordinary_Fixed_Point_Type_Declaration;
15462 ----------------------------------------
15463 -- Prepare_Private_Subtype_Completion --
15464 ----------------------------------------
15466 procedure Prepare_Private_Subtype_Completion
15468 Related_Nod : Node_Id)
15470 Id_B : constant Entity_Id := Base_Type (Id);
15471 Full_B : constant Entity_Id := Full_View (Id_B);
15475 if Present (Full_B) then
15477 -- The Base_Type is already completed, we can complete the subtype
15478 -- now. We have to create a new entity with the same name, Thus we
15479 -- can't use Create_Itype.
15481 -- This is messy, should be fixed ???
15483 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
15484 Set_Is_Itype (Full);
15485 Set_Associated_Node_For_Itype (Full, Related_Nod);
15486 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
15489 -- The parent subtype may be private, but the base might not, in some
15490 -- nested instances. In that case, the subtype does not need to be
15491 -- exchanged. It would still be nice to make private subtypes and their
15492 -- bases consistent at all times ???
15494 if Is_Private_Type (Id_B) then
15495 Append_Elmt (Id, Private_Dependents (Id_B));
15498 end Prepare_Private_Subtype_Completion;
15500 ---------------------------
15501 -- Process_Discriminants --
15502 ---------------------------
15504 procedure Process_Discriminants
15506 Prev : Entity_Id := Empty)
15508 Elist : constant Elist_Id := New_Elmt_List;
15511 Discr_Number : Uint;
15512 Discr_Type : Entity_Id;
15513 Default_Present : Boolean := False;
15514 Default_Not_Present : Boolean := False;
15517 -- A composite type other than an array type can have discriminants.
15518 -- On entry, the current scope is the composite type.
15520 -- The discriminants are initially entered into the scope of the type
15521 -- via Enter_Name with the default Ekind of E_Void to prevent premature
15522 -- use, as explained at the end of this procedure.
15524 Discr := First (Discriminant_Specifications (N));
15525 while Present (Discr) loop
15526 Enter_Name (Defining_Identifier (Discr));
15528 -- For navigation purposes we add a reference to the discriminant
15529 -- in the entity for the type. If the current declaration is a
15530 -- completion, place references on the partial view. Otherwise the
15531 -- type is the current scope.
15533 if Present (Prev) then
15535 -- The references go on the partial view, if present. If the
15536 -- partial view has discriminants, the references have been
15537 -- generated already.
15539 if not Has_Discriminants (Prev) then
15540 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
15544 (Current_Scope, Defining_Identifier (Discr), 'd');
15547 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
15548 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
15550 -- Ada 2005 (AI-254)
15552 if Present (Access_To_Subprogram_Definition
15553 (Discriminant_Type (Discr)))
15554 and then Protected_Present (Access_To_Subprogram_Definition
15555 (Discriminant_Type (Discr)))
15558 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
15562 Find_Type (Discriminant_Type (Discr));
15563 Discr_Type := Etype (Discriminant_Type (Discr));
15565 if Error_Posted (Discriminant_Type (Discr)) then
15566 Discr_Type := Any_Type;
15570 if Is_Access_Type (Discr_Type) then
15572 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
15575 if Ada_Version < Ada_05 then
15576 Check_Access_Discriminant_Requires_Limited
15577 (Discr, Discriminant_Type (Discr));
15580 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
15582 ("(Ada 83) access discriminant not allowed", Discr);
15585 elsif not Is_Discrete_Type (Discr_Type) then
15586 Error_Msg_N ("discriminants must have a discrete or access type",
15587 Discriminant_Type (Discr));
15590 Set_Etype (Defining_Identifier (Discr), Discr_Type);
15592 -- If a discriminant specification includes the assignment compound
15593 -- delimiter followed by an expression, the expression is the default
15594 -- expression of the discriminant; the default expression must be of
15595 -- the type of the discriminant. (RM 3.7.1) Since this expression is
15596 -- a default expression, we do the special preanalysis, since this
15597 -- expression does not freeze (see "Handling of Default and Per-
15598 -- Object Expressions" in spec of package Sem).
15600 if Present (Expression (Discr)) then
15601 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
15603 if Nkind (N) = N_Formal_Type_Declaration then
15605 ("discriminant defaults not allowed for formal type",
15606 Expression (Discr));
15608 -- Tagged types cannot have defaulted discriminants, but a
15609 -- non-tagged private type with defaulted discriminants
15610 -- can have a tagged completion.
15612 elsif Is_Tagged_Type (Current_Scope)
15613 and then Comes_From_Source (N)
15616 ("discriminants of tagged type cannot have defaults",
15617 Expression (Discr));
15620 Default_Present := True;
15621 Append_Elmt (Expression (Discr), Elist);
15623 -- Tag the defining identifiers for the discriminants with
15624 -- their corresponding default expressions from the tree.
15626 Set_Discriminant_Default_Value
15627 (Defining_Identifier (Discr), Expression (Discr));
15631 Default_Not_Present := True;
15634 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
15635 -- Discr_Type but with the null-exclusion attribute
15637 if Ada_Version >= Ada_05 then
15639 -- Ada 2005 (AI-231): Static checks
15641 if Can_Never_Be_Null (Discr_Type) then
15642 Null_Exclusion_Static_Checks (Discr);
15644 elsif Is_Access_Type (Discr_Type)
15645 and then Null_Exclusion_Present (Discr)
15647 -- No need to check itypes because in their case this check
15648 -- was done at their point of creation
15650 and then not Is_Itype (Discr_Type)
15652 if Can_Never_Be_Null (Discr_Type) then
15654 ("`NOT NULL` not allowed (& already excludes null)",
15659 Set_Etype (Defining_Identifier (Discr),
15660 Create_Null_Excluding_Itype
15662 Related_Nod => Discr));
15664 -- Check for improper null exclusion if the type is otherwise
15665 -- legal for a discriminant.
15667 elsif Null_Exclusion_Present (Discr)
15668 and then Is_Discrete_Type (Discr_Type)
15671 ("null exclusion can only apply to an access type", Discr);
15674 -- Ada 2005 (AI-402): access discriminants of nonlimited types
15675 -- can't have defaults. Synchronized types, or types that are
15676 -- explicitly limited are fine, but special tests apply to derived
15677 -- types in generics: in a generic body we have to assume the
15678 -- worst, and therefore defaults are not allowed if the parent is
15679 -- a generic formal private type (see ACATS B370001).
15681 if Is_Access_Type (Discr_Type) then
15682 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
15683 or else not Default_Present
15684 or else Is_Limited_Record (Current_Scope)
15685 or else Is_Concurrent_Type (Current_Scope)
15686 or else Is_Concurrent_Record_Type (Current_Scope)
15687 or else Ekind (Current_Scope) = E_Limited_Private_Type
15689 if not Is_Derived_Type (Current_Scope)
15690 or else not Is_Generic_Type (Etype (Current_Scope))
15691 or else not In_Package_Body (Scope (Etype (Current_Scope)))
15692 or else Limited_Present
15693 (Type_Definition (Parent (Current_Scope)))
15698 Error_Msg_N ("access discriminants of nonlimited types",
15699 Expression (Discr));
15700 Error_Msg_N ("\cannot have defaults", Expression (Discr));
15703 elsif Present (Expression (Discr)) then
15705 ("(Ada 2005) access discriminants of nonlimited types",
15706 Expression (Discr));
15707 Error_Msg_N ("\cannot have defaults", Expression (Discr));
15715 -- An element list consisting of the default expressions of the
15716 -- discriminants is constructed in the above loop and used to set
15717 -- the Discriminant_Constraint attribute for the type. If an object
15718 -- is declared of this (record or task) type without any explicit
15719 -- discriminant constraint given, this element list will form the
15720 -- actual parameters for the corresponding initialization procedure
15723 Set_Discriminant_Constraint (Current_Scope, Elist);
15724 Set_Stored_Constraint (Current_Scope, No_Elist);
15726 -- Default expressions must be provided either for all or for none
15727 -- of the discriminants of a discriminant part. (RM 3.7.1)
15729 if Default_Present and then Default_Not_Present then
15731 ("incomplete specification of defaults for discriminants", N);
15734 -- The use of the name of a discriminant is not allowed in default
15735 -- expressions of a discriminant part if the specification of the
15736 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
15738 -- To detect this, the discriminant names are entered initially with an
15739 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
15740 -- attempt to use a void entity (for example in an expression that is
15741 -- type-checked) produces the error message: premature usage. Now after
15742 -- completing the semantic analysis of the discriminant part, we can set
15743 -- the Ekind of all the discriminants appropriately.
15745 Discr := First (Discriminant_Specifications (N));
15746 Discr_Number := Uint_1;
15747 while Present (Discr) loop
15748 Id := Defining_Identifier (Discr);
15749 Set_Ekind (Id, E_Discriminant);
15750 Init_Component_Location (Id);
15752 Set_Discriminant_Number (Id, Discr_Number);
15754 -- Make sure this is always set, even in illegal programs
15756 Set_Corresponding_Discriminant (Id, Empty);
15758 -- Initialize the Original_Record_Component to the entity itself.
15759 -- Inherit_Components will propagate the right value to
15760 -- discriminants in derived record types.
15762 Set_Original_Record_Component (Id, Id);
15764 -- Create the discriminal for the discriminant
15766 Build_Discriminal (Id);
15769 Discr_Number := Discr_Number + 1;
15772 Set_Has_Discriminants (Current_Scope);
15773 end Process_Discriminants;
15775 -----------------------
15776 -- Process_Full_View --
15777 -----------------------
15779 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
15780 Priv_Parent : Entity_Id;
15781 Full_Parent : Entity_Id;
15782 Full_Indic : Node_Id;
15784 procedure Collect_Implemented_Interfaces
15786 Ifaces : Elist_Id);
15787 -- Ada 2005: Gather all the interfaces that Typ directly or
15788 -- inherently implements. Duplicate entries are not added to
15789 -- the list Ifaces.
15791 ------------------------------------
15792 -- Collect_Implemented_Interfaces --
15793 ------------------------------------
15795 procedure Collect_Implemented_Interfaces
15800 Iface_Elmt : Elmt_Id;
15803 -- Abstract interfaces are only associated with tagged record types
15805 if not Is_Tagged_Type (Typ)
15806 or else not Is_Record_Type (Typ)
15811 -- Recursively climb to the ancestors
15813 if Etype (Typ) /= Typ
15815 -- Protect the frontend against wrong cyclic declarations like:
15817 -- type B is new A with private;
15818 -- type C is new A with private;
15820 -- type B is new C with null record;
15821 -- type C is new B with null record;
15823 and then Etype (Typ) /= Priv_T
15824 and then Etype (Typ) /= Full_T
15826 -- Keep separate the management of private type declarations
15828 if Ekind (Typ) = E_Record_Type_With_Private then
15830 -- Handle the following erronous case:
15831 -- type Private_Type is tagged private;
15833 -- type Private_Type is new Type_Implementing_Iface;
15835 if Present (Full_View (Typ))
15836 and then Etype (Typ) /= Full_View (Typ)
15838 if Is_Interface (Etype (Typ)) then
15839 Append_Unique_Elmt (Etype (Typ), Ifaces);
15842 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15845 -- Non-private types
15848 if Is_Interface (Etype (Typ)) then
15849 Append_Unique_Elmt (Etype (Typ), Ifaces);
15852 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
15856 -- Handle entities in the list of abstract interfaces
15858 if Present (Interfaces (Typ)) then
15859 Iface_Elmt := First_Elmt (Interfaces (Typ));
15860 while Present (Iface_Elmt) loop
15861 Iface := Node (Iface_Elmt);
15863 pragma Assert (Is_Interface (Iface));
15865 if not Contain_Interface (Iface, Ifaces) then
15866 Append_Elmt (Iface, Ifaces);
15867 Collect_Implemented_Interfaces (Iface, Ifaces);
15870 Next_Elmt (Iface_Elmt);
15873 end Collect_Implemented_Interfaces;
15875 -- Start of processing for Process_Full_View
15878 -- First some sanity checks that must be done after semantic
15879 -- decoration of the full view and thus cannot be placed with other
15880 -- similar checks in Find_Type_Name
15882 if not Is_Limited_Type (Priv_T)
15883 and then (Is_Limited_Type (Full_T)
15884 or else Is_Limited_Composite (Full_T))
15887 ("completion of nonlimited type cannot be limited", Full_T);
15888 Explain_Limited_Type (Full_T, Full_T);
15890 elsif Is_Abstract_Type (Full_T)
15891 and then not Is_Abstract_Type (Priv_T)
15894 ("completion of nonabstract type cannot be abstract", Full_T);
15896 elsif Is_Tagged_Type (Priv_T)
15897 and then Is_Limited_Type (Priv_T)
15898 and then not Is_Limited_Type (Full_T)
15900 -- If pragma CPP_Class was applied to the private declaration
15901 -- propagate the limitedness to the full-view
15903 if Is_CPP_Class (Priv_T) then
15904 Set_Is_Limited_Record (Full_T);
15906 -- GNAT allow its own definition of Limited_Controlled to disobey
15907 -- this rule in order in ease the implementation. The next test is
15908 -- safe because Root_Controlled is defined in a private system child
15910 elsif Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
15911 Set_Is_Limited_Composite (Full_T);
15914 ("completion of limited tagged type must be limited", Full_T);
15917 elsif Is_Generic_Type (Priv_T) then
15918 Error_Msg_N ("generic type cannot have a completion", Full_T);
15921 -- Check that ancestor interfaces of private and full views are
15922 -- consistent. We omit this check for synchronized types because
15923 -- they are performed on the corresponding record type when frozen.
15925 if Ada_Version >= Ada_05
15926 and then Is_Tagged_Type (Priv_T)
15927 and then Is_Tagged_Type (Full_T)
15928 and then not Is_Concurrent_Type (Full_T)
15932 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
15933 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
15936 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
15937 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
15939 -- Ada 2005 (AI-251): The partial view shall be a descendant of
15940 -- an interface type if and only if the full type is descendant
15941 -- of the interface type (AARM 7.3 (7.3/2).
15943 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
15945 if Present (Iface) then
15946 Error_Msg_NE ("interface & not implemented by full type " &
15947 "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
15950 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
15952 if Present (Iface) then
15953 Error_Msg_NE ("interface & not implemented by partial view " &
15954 "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
15959 if Is_Tagged_Type (Priv_T)
15960 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
15961 and then Is_Derived_Type (Full_T)
15963 Priv_Parent := Etype (Priv_T);
15965 -- The full view of a private extension may have been transformed
15966 -- into an unconstrained derived type declaration and a subtype
15967 -- declaration (see build_derived_record_type for details).
15969 if Nkind (N) = N_Subtype_Declaration then
15970 Full_Indic := Subtype_Indication (N);
15971 Full_Parent := Etype (Base_Type (Full_T));
15973 Full_Indic := Subtype_Indication (Type_Definition (N));
15974 Full_Parent := Etype (Full_T);
15977 -- Check that the parent type of the full type is a descendant of
15978 -- the ancestor subtype given in the private extension. If either
15979 -- entity has an Etype equal to Any_Type then we had some previous
15980 -- error situation [7.3(8)].
15982 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
15985 -- Ada 2005 (AI-251): Interfaces in the full-typ can be given in
15986 -- any order. Therefore we don't have to check that its parent must
15987 -- be a descendant of the parent of the private type declaration.
15989 elsif Is_Interface (Priv_Parent)
15990 and then Is_Interface (Full_Parent)
15994 -- Ada 2005 (AI-251): If the parent of the private type declaration
15995 -- is an interface there is no need to check that it is an ancestor
15996 -- of the associated full type declaration. The required tests for
15997 -- this case are performed by Build_Derived_Record_Type.
15999 elsif not Is_Interface (Base_Type (Priv_Parent))
16000 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
16003 ("parent of full type must descend from parent"
16004 & " of private extension", Full_Indic);
16006 -- Check the rules of 7.3(10): if the private extension inherits
16007 -- known discriminants, then the full type must also inherit those
16008 -- discriminants from the same (ancestor) type, and the parent
16009 -- subtype of the full type must be constrained if and only if
16010 -- the ancestor subtype of the private extension is constrained.
16012 elsif No (Discriminant_Specifications (Parent (Priv_T)))
16013 and then not Has_Unknown_Discriminants (Priv_T)
16014 and then Has_Discriminants (Base_Type (Priv_Parent))
16017 Priv_Indic : constant Node_Id :=
16018 Subtype_Indication (Parent (Priv_T));
16020 Priv_Constr : constant Boolean :=
16021 Is_Constrained (Priv_Parent)
16023 Nkind (Priv_Indic) = N_Subtype_Indication
16024 or else Is_Constrained (Entity (Priv_Indic));
16026 Full_Constr : constant Boolean :=
16027 Is_Constrained (Full_Parent)
16029 Nkind (Full_Indic) = N_Subtype_Indication
16030 or else Is_Constrained (Entity (Full_Indic));
16032 Priv_Discr : Entity_Id;
16033 Full_Discr : Entity_Id;
16036 Priv_Discr := First_Discriminant (Priv_Parent);
16037 Full_Discr := First_Discriminant (Full_Parent);
16038 while Present (Priv_Discr) and then Present (Full_Discr) loop
16039 if Original_Record_Component (Priv_Discr) =
16040 Original_Record_Component (Full_Discr)
16042 Corresponding_Discriminant (Priv_Discr) =
16043 Corresponding_Discriminant (Full_Discr)
16050 Next_Discriminant (Priv_Discr);
16051 Next_Discriminant (Full_Discr);
16054 if Present (Priv_Discr) or else Present (Full_Discr) then
16056 ("full view must inherit discriminants of the parent type"
16057 & " used in the private extension", Full_Indic);
16059 elsif Priv_Constr and then not Full_Constr then
16061 ("parent subtype of full type must be constrained",
16064 elsif Full_Constr and then not Priv_Constr then
16066 ("parent subtype of full type must be unconstrained",
16071 -- Check the rules of 7.3(12): if a partial view has neither known
16072 -- or unknown discriminants, then the full type declaration shall
16073 -- define a definite subtype.
16075 elsif not Has_Unknown_Discriminants (Priv_T)
16076 and then not Has_Discriminants (Priv_T)
16077 and then not Is_Constrained (Full_T)
16080 ("full view must define a constrained type if partial view"
16081 & " has no discriminants", Full_T);
16084 -- ??????? Do we implement the following properly ?????
16085 -- If the ancestor subtype of a private extension has constrained
16086 -- discriminants, then the parent subtype of the full view shall
16087 -- impose a statically matching constraint on those discriminants
16091 -- For untagged types, verify that a type without discriminants
16092 -- is not completed with an unconstrained type.
16094 if not Is_Indefinite_Subtype (Priv_T)
16095 and then Is_Indefinite_Subtype (Full_T)
16097 Error_Msg_N ("full view of type must be definite subtype", Full_T);
16101 -- AI-419: verify that the use of "limited" is consistent
16104 Orig_Decl : constant Node_Id := Original_Node (N);
16107 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
16108 and then not Limited_Present (Parent (Priv_T))
16109 and then not Synchronized_Present (Parent (Priv_T))
16110 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
16112 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
16113 and then Limited_Present (Type_Definition (Orig_Decl))
16116 ("full view of non-limited extension cannot be limited", N);
16120 -- Ada 2005 (AI-443): A synchronized private extension must be
16121 -- completed by a task or protected type.
16123 if Ada_Version >= Ada_05
16124 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
16125 and then Synchronized_Present (Parent (Priv_T))
16126 and then not Is_Concurrent_Type (Full_T)
16128 Error_Msg_N ("full view of synchronized extension must " &
16129 "be synchronized type", N);
16132 -- Ada 2005 AI-363: if the full view has discriminants with
16133 -- defaults, it is illegal to declare constrained access subtypes
16134 -- whose designated type is the current type. This allows objects
16135 -- of the type that are declared in the heap to be unconstrained.
16137 if not Has_Unknown_Discriminants (Priv_T)
16138 and then not Has_Discriminants (Priv_T)
16139 and then Has_Discriminants (Full_T)
16141 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
16143 Set_Has_Constrained_Partial_View (Full_T);
16144 Set_Has_Constrained_Partial_View (Priv_T);
16147 -- Create a full declaration for all its subtypes recorded in
16148 -- Private_Dependents and swap them similarly to the base type. These
16149 -- are subtypes that have been define before the full declaration of
16150 -- the private type. We also swap the entry in Private_Dependents list
16151 -- so we can properly restore the private view on exit from the scope.
16154 Priv_Elmt : Elmt_Id;
16159 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
16160 while Present (Priv_Elmt) loop
16161 Priv := Node (Priv_Elmt);
16163 if Ekind (Priv) = E_Private_Subtype
16164 or else Ekind (Priv) = E_Limited_Private_Subtype
16165 or else Ekind (Priv) = E_Record_Subtype_With_Private
16167 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
16168 Set_Is_Itype (Full);
16169 Set_Parent (Full, Parent (Priv));
16170 Set_Associated_Node_For_Itype (Full, N);
16172 -- Now we need to complete the private subtype, but since the
16173 -- base type has already been swapped, we must also swap the
16174 -- subtypes (and thus, reverse the arguments in the call to
16175 -- Complete_Private_Subtype).
16177 Copy_And_Swap (Priv, Full);
16178 Complete_Private_Subtype (Full, Priv, Full_T, N);
16179 Replace_Elmt (Priv_Elmt, Full);
16182 Next_Elmt (Priv_Elmt);
16186 -- If the private view was tagged, copy the new primitive operations
16187 -- from the private view to the full view.
16189 if Is_Tagged_Type (Full_T) then
16191 Disp_Typ : Entity_Id;
16192 Full_List : Elist_Id;
16194 Prim_Elmt : Elmt_Id;
16195 Priv_List : Elist_Id;
16199 L : Elist_Id) return Boolean;
16200 -- Determine whether list L contains element E
16208 L : Elist_Id) return Boolean
16210 List_Elmt : Elmt_Id;
16213 List_Elmt := First_Elmt (L);
16214 while Present (List_Elmt) loop
16215 if Node (List_Elmt) = E then
16219 Next_Elmt (List_Elmt);
16225 -- Start of processing
16228 if Is_Tagged_Type (Priv_T) then
16229 Priv_List := Primitive_Operations (Priv_T);
16230 Prim_Elmt := First_Elmt (Priv_List);
16232 -- In the case of a concurrent type completing a private tagged
16233 -- type, primitives may have been declared in between the two
16234 -- views. These subprograms need to be wrapped the same way
16235 -- entries and protected procedures are handled because they
16236 -- cannot be directly shared by the two views.
16238 if Is_Concurrent_Type (Full_T) then
16240 Conc_Typ : constant Entity_Id :=
16241 Corresponding_Record_Type (Full_T);
16242 Curr_Nod : Node_Id := Parent (Conc_Typ);
16243 Wrap_Spec : Node_Id;
16246 while Present (Prim_Elmt) loop
16247 Prim := Node (Prim_Elmt);
16249 if Comes_From_Source (Prim)
16250 and then not Is_Abstract_Subprogram (Prim)
16253 Make_Subprogram_Declaration (Sloc (Prim),
16257 Obj_Typ => Conc_Typ,
16259 Parameter_Specifications (
16262 Insert_After (Curr_Nod, Wrap_Spec);
16263 Curr_Nod := Wrap_Spec;
16265 Analyze (Wrap_Spec);
16268 Next_Elmt (Prim_Elmt);
16274 -- For non-concurrent types, transfer explicit primitives, but
16275 -- omit those inherited from the parent of the private view
16276 -- since they will be re-inherited later on.
16279 Full_List := Primitive_Operations (Full_T);
16281 while Present (Prim_Elmt) loop
16282 Prim := Node (Prim_Elmt);
16284 if Comes_From_Source (Prim)
16285 and then not Contains (Prim, Full_List)
16287 Append_Elmt (Prim, Full_List);
16290 Next_Elmt (Prim_Elmt);
16294 -- Untagged private view
16297 Full_List := Primitive_Operations (Full_T);
16299 -- In this case the partial view is untagged, so here we locate
16300 -- all of the earlier primitives that need to be treated as
16301 -- dispatching (those that appear between the two views). Note
16302 -- that these additional operations must all be new operations
16303 -- (any earlier operations that override inherited operations
16304 -- of the full view will already have been inserted in the
16305 -- primitives list, marked by Check_Operation_From_Private_View
16306 -- as dispatching. Note that implicit "/=" operators are
16307 -- excluded from being added to the primitives list since they
16308 -- shouldn't be treated as dispatching (tagged "/=" is handled
16311 Prim := Next_Entity (Full_T);
16312 while Present (Prim) and then Prim /= Priv_T loop
16313 if Ekind (Prim) = E_Procedure
16315 Ekind (Prim) = E_Function
16317 Disp_Typ := Find_Dispatching_Type (Prim);
16319 if Disp_Typ = Full_T
16320 and then (Chars (Prim) /= Name_Op_Ne
16321 or else Comes_From_Source (Prim))
16323 Check_Controlling_Formals (Full_T, Prim);
16325 if not Is_Dispatching_Operation (Prim) then
16326 Append_Elmt (Prim, Full_List);
16327 Set_Is_Dispatching_Operation (Prim, True);
16328 Set_DT_Position (Prim, No_Uint);
16331 elsif Is_Dispatching_Operation (Prim)
16332 and then Disp_Typ /= Full_T
16335 -- Verify that it is not otherwise controlled by a
16336 -- formal or a return value of type T.
16338 Check_Controlling_Formals (Disp_Typ, Prim);
16342 Next_Entity (Prim);
16346 -- For the tagged case, the two views can share the same
16347 -- Primitive Operation list and the same class wide type.
16348 -- Update attributes of the class-wide type which depend on
16349 -- the full declaration.
16351 if Is_Tagged_Type (Priv_T) then
16352 Set_Primitive_Operations (Priv_T, Full_List);
16353 Set_Class_Wide_Type
16354 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
16356 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
16361 -- Ada 2005 AI 161: Check preelaboratable initialization consistency
16363 if Known_To_Have_Preelab_Init (Priv_T) then
16365 -- Case where there is a pragma Preelaborable_Initialization. We
16366 -- always allow this in predefined units, which is a bit of a kludge,
16367 -- but it means we don't have to struggle to meet the requirements in
16368 -- the RM for having Preelaborable Initialization. Otherwise we
16369 -- require that the type meets the RM rules. But we can't check that
16370 -- yet, because of the rule about overriding Ininitialize, so we
16371 -- simply set a flag that will be checked at freeze time.
16373 if not In_Predefined_Unit (Full_T) then
16374 Set_Must_Have_Preelab_Init (Full_T);
16378 -- If pragma CPP_Class was applied to the private type declaration,
16379 -- propagate it now to the full type declaration.
16381 if Is_CPP_Class (Priv_T) then
16382 Set_Is_CPP_Class (Full_T);
16383 Set_Convention (Full_T, Convention_CPP);
16385 end Process_Full_View;
16387 -----------------------------------
16388 -- Process_Incomplete_Dependents --
16389 -----------------------------------
16391 procedure Process_Incomplete_Dependents
16393 Full_T : Entity_Id;
16396 Inc_Elmt : Elmt_Id;
16397 Priv_Dep : Entity_Id;
16398 New_Subt : Entity_Id;
16400 Disc_Constraint : Elist_Id;
16403 if No (Private_Dependents (Inc_T)) then
16407 -- Itypes that may be generated by the completion of an incomplete
16408 -- subtype are not used by the back-end and not attached to the tree.
16409 -- They are created only for constraint-checking purposes.
16411 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
16412 while Present (Inc_Elmt) loop
16413 Priv_Dep := Node (Inc_Elmt);
16415 if Ekind (Priv_Dep) = E_Subprogram_Type then
16417 -- An Access_To_Subprogram type may have a return type or a
16418 -- parameter type that is incomplete. Replace with the full view.
16420 if Etype (Priv_Dep) = Inc_T then
16421 Set_Etype (Priv_Dep, Full_T);
16425 Formal : Entity_Id;
16428 Formal := First_Formal (Priv_Dep);
16429 while Present (Formal) loop
16430 if Etype (Formal) = Inc_T then
16431 Set_Etype (Formal, Full_T);
16434 Next_Formal (Formal);
16438 elsif Is_Overloadable (Priv_Dep) then
16440 -- A protected operation is never dispatching: only its
16441 -- wrapper operation (which has convention Ada) is.
16443 if Is_Tagged_Type (Full_T)
16444 and then Convention (Priv_Dep) /= Convention_Protected
16447 -- Subprogram has an access parameter whose designated type
16448 -- was incomplete. Reexamine declaration now, because it may
16449 -- be a primitive operation of the full type.
16451 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
16452 Set_Is_Dispatching_Operation (Priv_Dep);
16453 Check_Controlling_Formals (Full_T, Priv_Dep);
16456 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
16458 -- Can happen during processing of a body before the completion
16459 -- of a TA type. Ignore, because spec is also on dependent list.
16463 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
16464 -- corresponding subtype of the full view.
16466 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
16467 Set_Subtype_Indication
16468 (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
16469 Set_Etype (Priv_Dep, Full_T);
16470 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
16471 Set_Analyzed (Parent (Priv_Dep), False);
16473 -- Reanalyze the declaration, suppressing the call to
16474 -- Enter_Name to avoid duplicate names.
16476 Analyze_Subtype_Declaration
16477 (N => Parent (Priv_Dep),
16480 -- Dependent is a subtype
16483 -- We build a new subtype indication using the full view of the
16484 -- incomplete parent. The discriminant constraints have been
16485 -- elaborated already at the point of the subtype declaration.
16487 New_Subt := Create_Itype (E_Void, N);
16489 if Has_Discriminants (Full_T) then
16490 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
16492 Disc_Constraint := No_Elist;
16495 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
16496 Set_Full_View (Priv_Dep, New_Subt);
16499 Next_Elmt (Inc_Elmt);
16501 end Process_Incomplete_Dependents;
16503 --------------------------------
16504 -- Process_Range_Expr_In_Decl --
16505 --------------------------------
16507 procedure Process_Range_Expr_In_Decl
16510 Check_List : List_Id := Empty_List;
16511 R_Check_Off : Boolean := False)
16514 R_Checks : Check_Result;
16515 Type_Decl : Node_Id;
16516 Def_Id : Entity_Id;
16519 Analyze_And_Resolve (R, Base_Type (T));
16521 if Nkind (R) = N_Range then
16522 Lo := Low_Bound (R);
16523 Hi := High_Bound (R);
16525 -- We need to ensure validity of the bounds here, because if we
16526 -- go ahead and do the expansion, then the expanded code will get
16527 -- analyzed with range checks suppressed and we miss the check.
16529 Validity_Check_Range (R);
16531 -- If there were errors in the declaration, try and patch up some
16532 -- common mistakes in the bounds. The cases handled are literals
16533 -- which are Integer where the expected type is Real and vice versa.
16534 -- These corrections allow the compilation process to proceed further
16535 -- along since some basic assumptions of the format of the bounds
16538 if Etype (R) = Any_Type then
16540 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
16542 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
16544 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
16546 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
16548 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
16550 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
16552 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
16554 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
16561 -- If the bounds of the range have been mistakenly given as string
16562 -- literals (perhaps in place of character literals), then an error
16563 -- has already been reported, but we rewrite the string literal as a
16564 -- bound of the range's type to avoid blowups in later processing
16565 -- that looks at static values.
16567 if Nkind (Lo) = N_String_Literal then
16569 Make_Attribute_Reference (Sloc (Lo),
16570 Attribute_Name => Name_First,
16571 Prefix => New_Reference_To (T, Sloc (Lo))));
16572 Analyze_And_Resolve (Lo);
16575 if Nkind (Hi) = N_String_Literal then
16577 Make_Attribute_Reference (Sloc (Hi),
16578 Attribute_Name => Name_First,
16579 Prefix => New_Reference_To (T, Sloc (Hi))));
16580 Analyze_And_Resolve (Hi);
16583 -- If bounds aren't scalar at this point then exit, avoiding
16584 -- problems with further processing of the range in this procedure.
16586 if not Is_Scalar_Type (Etype (Lo)) then
16590 -- Resolve (actually Sem_Eval) has checked that the bounds are in
16591 -- then range of the base type. Here we check whether the bounds
16592 -- are in the range of the subtype itself. Note that if the bounds
16593 -- represent the null range the Constraint_Error exception should
16596 -- ??? The following code should be cleaned up as follows
16598 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
16599 -- is done in the call to Range_Check (R, T); below
16601 -- 2. The use of R_Check_Off should be investigated and possibly
16602 -- removed, this would clean up things a bit.
16604 if Is_Null_Range (Lo, Hi) then
16608 -- Capture values of bounds and generate temporaries for them
16609 -- if needed, before applying checks, since checks may cause
16610 -- duplication of the expression without forcing evaluation.
16612 if Expander_Active then
16613 Force_Evaluation (Lo);
16614 Force_Evaluation (Hi);
16617 -- We use a flag here instead of suppressing checks on the
16618 -- type because the type we check against isn't necessarily
16619 -- the place where we put the check.
16621 if not R_Check_Off then
16622 R_Checks := Get_Range_Checks (R, T);
16624 -- Look up tree to find an appropriate insertion point.
16625 -- This seems really junk code, and very brittle, couldn't
16626 -- we just use an insert actions call of some kind ???
16628 Type_Decl := Parent (R);
16629 while Present (Type_Decl) and then not
16630 (Nkind_In (Type_Decl, N_Full_Type_Declaration,
16631 N_Subtype_Declaration,
16633 N_Task_Type_Declaration)
16635 Nkind_In (Type_Decl, N_Single_Task_Declaration,
16636 N_Protected_Type_Declaration,
16637 N_Single_Protected_Declaration))
16639 Type_Decl := Parent (Type_Decl);
16642 -- Why would Type_Decl not be present??? Without this test,
16643 -- short regression tests fail.
16645 if Present (Type_Decl) then
16647 -- Case of loop statement (more comments ???)
16649 if Nkind (Type_Decl) = N_Loop_Statement then
16654 Indic := Parent (R);
16655 while Present (Indic)
16656 and then Nkind (Indic) /= N_Subtype_Indication
16658 Indic := Parent (Indic);
16661 if Present (Indic) then
16662 Def_Id := Etype (Subtype_Mark (Indic));
16664 Insert_Range_Checks
16670 Do_Before => True);
16674 -- All other cases (more comments ???)
16677 Def_Id := Defining_Identifier (Type_Decl);
16679 if (Ekind (Def_Id) = E_Record_Type
16680 and then Depends_On_Discriminant (R))
16682 (Ekind (Def_Id) = E_Protected_Type
16683 and then Has_Discriminants (Def_Id))
16685 Append_Range_Checks
16686 (R_Checks, Check_List, Def_Id, Sloc (Type_Decl), R);
16689 Insert_Range_Checks
16690 (R_Checks, Type_Decl, Def_Id, Sloc (Type_Decl), R);
16698 elsif Expander_Active then
16699 Get_Index_Bounds (R, Lo, Hi);
16700 Force_Evaluation (Lo);
16701 Force_Evaluation (Hi);
16703 end Process_Range_Expr_In_Decl;
16705 --------------------------------------
16706 -- Process_Real_Range_Specification --
16707 --------------------------------------
16709 procedure Process_Real_Range_Specification (Def : Node_Id) is
16710 Spec : constant Node_Id := Real_Range_Specification (Def);
16713 Err : Boolean := False;
16715 procedure Analyze_Bound (N : Node_Id);
16716 -- Analyze and check one bound
16718 -------------------
16719 -- Analyze_Bound --
16720 -------------------
16722 procedure Analyze_Bound (N : Node_Id) is
16724 Analyze_And_Resolve (N, Any_Real);
16726 if not Is_OK_Static_Expression (N) then
16727 Flag_Non_Static_Expr
16728 ("bound in real type definition is not static!", N);
16733 -- Start of processing for Process_Real_Range_Specification
16736 if Present (Spec) then
16737 Lo := Low_Bound (Spec);
16738 Hi := High_Bound (Spec);
16739 Analyze_Bound (Lo);
16740 Analyze_Bound (Hi);
16742 -- If error, clear away junk range specification
16745 Set_Real_Range_Specification (Def, Empty);
16748 end Process_Real_Range_Specification;
16750 ---------------------
16751 -- Process_Subtype --
16752 ---------------------
16754 function Process_Subtype
16756 Related_Nod : Node_Id;
16757 Related_Id : Entity_Id := Empty;
16758 Suffix : Character := ' ') return Entity_Id
16761 Def_Id : Entity_Id;
16762 Error_Node : Node_Id;
16763 Full_View_Id : Entity_Id;
16764 Subtype_Mark_Id : Entity_Id;
16766 May_Have_Null_Exclusion : Boolean;
16768 procedure Check_Incomplete (T : Entity_Id);
16769 -- Called to verify that an incomplete type is not used prematurely
16771 ----------------------
16772 -- Check_Incomplete --
16773 ----------------------
16775 procedure Check_Incomplete (T : Entity_Id) is
16777 -- Ada 2005 (AI-412): Incomplete subtypes are legal
16779 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
16781 not (Ada_Version >= Ada_05
16783 (Nkind (Parent (T)) = N_Subtype_Declaration
16785 (Nkind (Parent (T)) = N_Subtype_Indication
16786 and then Nkind (Parent (Parent (T))) =
16787 N_Subtype_Declaration)))
16789 Error_Msg_N ("invalid use of type before its full declaration", T);
16791 end Check_Incomplete;
16793 -- Start of processing for Process_Subtype
16796 -- Case of no constraints present
16798 if Nkind (S) /= N_Subtype_Indication then
16800 Check_Incomplete (S);
16803 -- Ada 2005 (AI-231): Static check
16805 if Ada_Version >= Ada_05
16806 and then Present (P)
16807 and then Null_Exclusion_Present (P)
16808 and then Nkind (P) /= N_Access_To_Object_Definition
16809 and then not Is_Access_Type (Entity (S))
16811 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
16814 -- The following is ugly, can't we have a range or even a flag???
16816 May_Have_Null_Exclusion :=
16817 Nkind_In (P, N_Access_Definition,
16818 N_Access_Function_Definition,
16819 N_Access_Procedure_Definition,
16820 N_Access_To_Object_Definition,
16822 N_Component_Definition)
16824 Nkind_In (P, N_Derived_Type_Definition,
16825 N_Discriminant_Specification,
16826 N_Formal_Object_Declaration,
16827 N_Object_Declaration,
16828 N_Object_Renaming_Declaration,
16829 N_Parameter_Specification,
16830 N_Subtype_Declaration);
16832 -- Create an Itype that is a duplicate of Entity (S) but with the
16833 -- null-exclusion attribute
16835 if May_Have_Null_Exclusion
16836 and then Is_Access_Type (Entity (S))
16837 and then Null_Exclusion_Present (P)
16839 -- No need to check the case of an access to object definition.
16840 -- It is correct to define double not-null pointers.
16843 -- type Not_Null_Int_Ptr is not null access Integer;
16844 -- type Acc is not null access Not_Null_Int_Ptr;
16846 and then Nkind (P) /= N_Access_To_Object_Definition
16848 if Can_Never_Be_Null (Entity (S)) then
16849 case Nkind (Related_Nod) is
16850 when N_Full_Type_Declaration =>
16851 if Nkind (Type_Definition (Related_Nod))
16852 in N_Array_Type_Definition
16856 (Component_Definition
16857 (Type_Definition (Related_Nod)));
16860 Subtype_Indication (Type_Definition (Related_Nod));
16863 when N_Subtype_Declaration =>
16864 Error_Node := Subtype_Indication (Related_Nod);
16866 when N_Object_Declaration =>
16867 Error_Node := Object_Definition (Related_Nod);
16869 when N_Component_Declaration =>
16871 Subtype_Indication (Component_Definition (Related_Nod));
16873 when N_Allocator =>
16874 Error_Node := Expression (Related_Nod);
16877 pragma Assert (False);
16878 Error_Node := Related_Nod;
16882 ("`NOT NULL` not allowed (& already excludes null)",
16888 Create_Null_Excluding_Itype
16890 Related_Nod => P));
16891 Set_Entity (S, Etype (S));
16896 -- Case of constraint present, so that we have an N_Subtype_Indication
16897 -- node (this node is created only if constraints are present).
16900 Find_Type (Subtype_Mark (S));
16902 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
16904 (Nkind (Parent (S)) = N_Subtype_Declaration
16905 and then Is_Itype (Defining_Identifier (Parent (S))))
16907 Check_Incomplete (Subtype_Mark (S));
16911 Subtype_Mark_Id := Entity (Subtype_Mark (S));
16913 -- Explicit subtype declaration case
16915 if Nkind (P) = N_Subtype_Declaration then
16916 Def_Id := Defining_Identifier (P);
16918 -- Explicit derived type definition case
16920 elsif Nkind (P) = N_Derived_Type_Definition then
16921 Def_Id := Defining_Identifier (Parent (P));
16923 -- Implicit case, the Def_Id must be created as an implicit type.
16924 -- The one exception arises in the case of concurrent types, array
16925 -- and access types, where other subsidiary implicit types may be
16926 -- created and must appear before the main implicit type. In these
16927 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
16928 -- has not yet been called to create Def_Id.
16931 if Is_Array_Type (Subtype_Mark_Id)
16932 or else Is_Concurrent_Type (Subtype_Mark_Id)
16933 or else Is_Access_Type (Subtype_Mark_Id)
16937 -- For the other cases, we create a new unattached Itype,
16938 -- and set the indication to ensure it gets attached later.
16942 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
16946 -- If the kind of constraint is invalid for this kind of type,
16947 -- then give an error, and then pretend no constraint was given.
16949 if not Is_Valid_Constraint_Kind
16950 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
16953 ("incorrect constraint for this kind of type", Constraint (S));
16955 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16957 -- Set Ekind of orphan itype, to prevent cascaded errors
16959 if Present (Def_Id) then
16960 Set_Ekind (Def_Id, Ekind (Any_Type));
16963 -- Make recursive call, having got rid of the bogus constraint
16965 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
16968 -- Remaining processing depends on type
16970 case Ekind (Subtype_Mark_Id) is
16971 when Access_Kind =>
16972 Constrain_Access (Def_Id, S, Related_Nod);
16975 and then Is_Itype (Designated_Type (Def_Id))
16976 and then Nkind (Related_Nod) = N_Subtype_Declaration
16977 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
16979 Build_Itype_Reference
16980 (Designated_Type (Def_Id), Related_Nod);
16984 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
16986 when Decimal_Fixed_Point_Kind =>
16987 Constrain_Decimal (Def_Id, S);
16989 when Enumeration_Kind =>
16990 Constrain_Enumeration (Def_Id, S);
16992 when Ordinary_Fixed_Point_Kind =>
16993 Constrain_Ordinary_Fixed (Def_Id, S);
16996 Constrain_Float (Def_Id, S);
16998 when Integer_Kind =>
16999 Constrain_Integer (Def_Id, S);
17001 when E_Record_Type |
17004 E_Incomplete_Type =>
17005 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
17007 if Ekind (Def_Id) = E_Incomplete_Type then
17008 Set_Private_Dependents (Def_Id, New_Elmt_List);
17011 when Private_Kind =>
17012 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
17013 Set_Private_Dependents (Def_Id, New_Elmt_List);
17015 -- In case of an invalid constraint prevent further processing
17016 -- since the type constructed is missing expected fields.
17018 if Etype (Def_Id) = Any_Type then
17022 -- If the full view is that of a task with discriminants,
17023 -- we must constrain both the concurrent type and its
17024 -- corresponding record type. Otherwise we will just propagate
17025 -- the constraint to the full view, if available.
17027 if Present (Full_View (Subtype_Mark_Id))
17028 and then Has_Discriminants (Subtype_Mark_Id)
17029 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
17032 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
17034 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
17035 Constrain_Concurrent (Full_View_Id, S,
17036 Related_Nod, Related_Id, Suffix);
17037 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
17038 Set_Full_View (Def_Id, Full_View_Id);
17040 -- Introduce an explicit reference to the private subtype,
17041 -- to prevent scope anomalies in gigi if first use appears
17042 -- in a nested context, e.g. a later function body.
17043 -- Should this be generated in other contexts than a full
17044 -- type declaration?
17046 if Is_Itype (Def_Id)
17048 Nkind (Parent (P)) = N_Full_Type_Declaration
17050 Build_Itype_Reference (Def_Id, Parent (P));
17054 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
17057 when Concurrent_Kind =>
17058 Constrain_Concurrent (Def_Id, S,
17059 Related_Nod, Related_Id, Suffix);
17062 Error_Msg_N ("invalid subtype mark in subtype indication", S);
17065 -- Size and Convention are always inherited from the base type
17067 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
17068 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
17072 end Process_Subtype;
17074 ---------------------------------------
17075 -- Check_Anonymous_Access_Components --
17076 ---------------------------------------
17078 procedure Check_Anonymous_Access_Components
17079 (Typ_Decl : Node_Id;
17082 Comp_List : Node_Id)
17084 Loc : constant Source_Ptr := Sloc (Typ_Decl);
17085 Anon_Access : Entity_Id;
17088 Comp_Def : Node_Id;
17090 Type_Def : Node_Id;
17092 procedure Build_Incomplete_Type_Declaration;
17093 -- If the record type contains components that include an access to the
17094 -- current record, then create an incomplete type declaration for the
17095 -- record, to be used as the designated type of the anonymous access.
17096 -- This is done only once, and only if there is no previous partial
17097 -- view of the type.
17099 function Designates_T (Subt : Node_Id) return Boolean;
17100 -- Check whether a node designates the enclosing record type, or 'Class
17103 function Mentions_T (Acc_Def : Node_Id) return Boolean;
17104 -- Check whether an access definition includes a reference to
17105 -- the enclosing record type. The reference can be a subtype mark
17106 -- in the access definition itself, a 'Class attribute reference, or
17107 -- recursively a reference appearing in a parameter specification
17108 -- or result definition of an access_to_subprogram definition.
17110 --------------------------------------
17111 -- Build_Incomplete_Type_Declaration --
17112 --------------------------------------
17114 procedure Build_Incomplete_Type_Declaration is
17119 -- Is_Tagged indicates whether the type is tagged. It is tagged if
17120 -- it's "is new ... with record" or else "is tagged record ...".
17122 Is_Tagged : constant Boolean :=
17123 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
17126 (Record_Extension_Part (Type_Definition (Typ_Decl))))
17128 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
17129 and then Tagged_Present (Type_Definition (Typ_Decl)));
17132 -- If there is a previous partial view, no need to create a new one
17133 -- If the partial view, given by Prev, is incomplete, If Prev is
17134 -- a private declaration, full declaration is flagged accordingly.
17136 if Prev /= Typ then
17138 Make_Class_Wide_Type (Prev);
17139 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
17140 Set_Etype (Class_Wide_Type (Typ), Typ);
17145 elsif Has_Private_Declaration (Typ) then
17147 -- If we refer to T'Class inside T, and T is the completion of a
17148 -- private type, then we need to make sure the class-wide type
17152 Make_Class_Wide_Type (Typ);
17157 -- If there was a previous anonymous access type, the incomplete
17158 -- type declaration will have been created already.
17160 elsif Present (Current_Entity (Typ))
17161 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
17162 and then Full_View (Current_Entity (Typ)) = Typ
17167 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
17168 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
17170 -- Type has already been inserted into the current scope.
17171 -- Remove it, and add incomplete declaration for type, so
17172 -- that subsequent anonymous access types can use it.
17173 -- The entity is unchained from the homonym list and from
17174 -- immediate visibility. After analysis, the entity in the
17175 -- incomplete declaration becomes immediately visible in the
17176 -- record declaration that follows.
17178 H := Current_Entity (Typ);
17181 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
17184 and then Homonym (H) /= Typ
17186 H := Homonym (Typ);
17189 Set_Homonym (H, Homonym (Typ));
17192 Insert_Before (Typ_Decl, Decl);
17194 Set_Full_View (Inc_T, Typ);
17197 -- Create a common class-wide type for both views, and set
17198 -- the Etype of the class-wide type to the full view.
17200 Make_Class_Wide_Type (Inc_T);
17201 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
17202 Set_Etype (Class_Wide_Type (Typ), Typ);
17205 end Build_Incomplete_Type_Declaration;
17211 function Designates_T (Subt : Node_Id) return Boolean is
17212 Type_Id : constant Name_Id := Chars (Typ);
17214 function Names_T (Nam : Node_Id) return Boolean;
17215 -- The record type has not been introduced in the current scope
17216 -- yet, so we must examine the name of the type itself, either
17217 -- an identifier T, or an expanded name of the form P.T, where
17218 -- P denotes the current scope.
17224 function Names_T (Nam : Node_Id) return Boolean is
17226 if Nkind (Nam) = N_Identifier then
17227 return Chars (Nam) = Type_Id;
17229 elsif Nkind (Nam) = N_Selected_Component then
17230 if Chars (Selector_Name (Nam)) = Type_Id then
17231 if Nkind (Prefix (Nam)) = N_Identifier then
17232 return Chars (Prefix (Nam)) = Chars (Current_Scope);
17234 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
17235 return Chars (Selector_Name (Prefix (Nam))) =
17236 Chars (Current_Scope);
17250 -- Start of processing for Designates_T
17253 if Nkind (Subt) = N_Identifier then
17254 return Chars (Subt) = Type_Id;
17256 -- Reference can be through an expanded name which has not been
17257 -- analyzed yet, and which designates enclosing scopes.
17259 elsif Nkind (Subt) = N_Selected_Component then
17260 if Names_T (Subt) then
17263 -- Otherwise it must denote an entity that is already visible.
17264 -- The access definition may name a subtype of the enclosing
17265 -- type, if there is a previous incomplete declaration for it.
17268 Find_Selected_Component (Subt);
17270 Is_Entity_Name (Subt)
17271 and then Scope (Entity (Subt)) = Current_Scope
17273 (Chars (Base_Type (Entity (Subt))) = Type_Id
17275 (Is_Class_Wide_Type (Entity (Subt))
17277 Chars (Etype (Base_Type (Entity (Subt)))) =
17281 -- A reference to the current type may appear as the prefix of
17282 -- a 'Class attribute.
17284 elsif Nkind (Subt) = N_Attribute_Reference
17285 and then Attribute_Name (Subt) = Name_Class
17287 return Names_T (Prefix (Subt));
17298 function Mentions_T (Acc_Def : Node_Id) return Boolean is
17299 Param_Spec : Node_Id;
17301 Acc_Subprg : constant Node_Id :=
17302 Access_To_Subprogram_Definition (Acc_Def);
17305 if No (Acc_Subprg) then
17306 return Designates_T (Subtype_Mark (Acc_Def));
17309 -- Component is an access_to_subprogram: examine its formals,
17310 -- and result definition in the case of an access_to_function.
17312 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
17313 while Present (Param_Spec) loop
17314 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
17315 and then Mentions_T (Parameter_Type (Param_Spec))
17319 elsif Designates_T (Parameter_Type (Param_Spec)) then
17326 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
17327 if Nkind (Result_Definition (Acc_Subprg)) =
17328 N_Access_Definition
17330 return Mentions_T (Result_Definition (Acc_Subprg));
17332 return Designates_T (Result_Definition (Acc_Subprg));
17339 -- Start of processing for Check_Anonymous_Access_Components
17342 if No (Comp_List) then
17346 Comp := First (Component_Items (Comp_List));
17347 while Present (Comp) loop
17348 if Nkind (Comp) = N_Component_Declaration
17350 (Access_Definition (Component_Definition (Comp)))
17352 Mentions_T (Access_Definition (Component_Definition (Comp)))
17354 Comp_Def := Component_Definition (Comp);
17356 Access_To_Subprogram_Definition
17357 (Access_Definition (Comp_Def));
17359 Build_Incomplete_Type_Declaration;
17361 Make_Defining_Identifier (Loc,
17362 Chars => New_Internal_Name ('S'));
17364 -- Create a declaration for the anonymous access type: either
17365 -- an access_to_object or an access_to_subprogram.
17367 if Present (Acc_Def) then
17368 if Nkind (Acc_Def) = N_Access_Function_Definition then
17370 Make_Access_Function_Definition (Loc,
17371 Parameter_Specifications =>
17372 Parameter_Specifications (Acc_Def),
17373 Result_Definition => Result_Definition (Acc_Def));
17376 Make_Access_Procedure_Definition (Loc,
17377 Parameter_Specifications =>
17378 Parameter_Specifications (Acc_Def));
17383 Make_Access_To_Object_Definition (Loc,
17384 Subtype_Indication =>
17387 (Access_Definition (Comp_Def))));
17389 Set_Constant_Present
17390 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
17392 (Type_Def, All_Present (Access_Definition (Comp_Def)));
17395 Set_Null_Exclusion_Present
17397 Null_Exclusion_Present (Access_Definition (Comp_Def)));
17400 Make_Full_Type_Declaration (Loc,
17401 Defining_Identifier => Anon_Access,
17402 Type_Definition => Type_Def);
17404 Insert_Before (Typ_Decl, Decl);
17407 -- If an access to object, Preserve entity of designated type,
17408 -- for ASIS use, before rewriting the component definition.
17410 if No (Acc_Def) then
17415 Desig := Entity (Subtype_Indication (Type_Def));
17417 -- If the access definition is to the current record,
17418 -- the visible entity at this point is an incomplete
17419 -- type. Retrieve the full view to simplify ASIS queries
17421 if Ekind (Desig) = E_Incomplete_Type then
17422 Desig := Full_View (Desig);
17426 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
17431 Make_Component_Definition (Loc,
17432 Subtype_Indication =>
17433 New_Occurrence_Of (Anon_Access, Loc)));
17435 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
17436 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
17438 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
17441 Set_Is_Local_Anonymous_Access (Anon_Access);
17447 if Present (Variant_Part (Comp_List)) then
17451 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
17452 while Present (V) loop
17453 Check_Anonymous_Access_Components
17454 (Typ_Decl, Typ, Prev, Component_List (V));
17455 Next_Non_Pragma (V);
17459 end Check_Anonymous_Access_Components;
17461 --------------------------------
17462 -- Preanalyze_Spec_Expression --
17463 --------------------------------
17465 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
17466 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
17468 In_Spec_Expression := True;
17469 Preanalyze_And_Resolve (N, T);
17470 In_Spec_Expression := Save_In_Spec_Expression;
17471 end Preanalyze_Spec_Expression;
17473 -----------------------------
17474 -- Record_Type_Declaration --
17475 -----------------------------
17477 procedure Record_Type_Declaration
17482 Def : constant Node_Id := Type_Definition (N);
17483 Is_Tagged : Boolean;
17484 Tag_Comp : Entity_Id;
17487 -- These flags must be initialized before calling Process_Discriminants
17488 -- because this routine makes use of them.
17490 Set_Ekind (T, E_Record_Type);
17492 Init_Size_Align (T);
17493 Set_Interfaces (T, No_Elist);
17494 Set_Stored_Constraint (T, No_Elist);
17498 if Ada_Version < Ada_05
17499 or else not Interface_Present (Def)
17501 -- The flag Is_Tagged_Type might have already been set by
17502 -- Find_Type_Name if it detected an error for declaration T. This
17503 -- arises in the case of private tagged types where the full view
17504 -- omits the word tagged.
17507 Tagged_Present (Def)
17508 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
17510 Set_Is_Tagged_Type (T, Is_Tagged);
17511 Set_Is_Limited_Record (T, Limited_Present (Def));
17513 -- Type is abstract if full declaration carries keyword, or if
17514 -- previous partial view did.
17516 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
17517 or else Abstract_Present (Def));
17521 Analyze_Interface_Declaration (T, Def);
17523 if Present (Discriminant_Specifications (N)) then
17525 ("interface types cannot have discriminants",
17526 Defining_Identifier
17527 (First (Discriminant_Specifications (N))));
17531 -- First pass: if there are self-referential access components,
17532 -- create the required anonymous access type declarations, and if
17533 -- need be an incomplete type declaration for T itself.
17535 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
17537 if Ada_Version >= Ada_05
17538 and then Present (Interface_List (Def))
17540 Check_Interfaces (N, Def);
17543 Ifaces_List : Elist_Id;
17546 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
17547 -- already in the parents.
17551 Ifaces_List => Ifaces_List,
17552 Exclude_Parents => True);
17554 Set_Interfaces (T, Ifaces_List);
17558 -- Records constitute a scope for the component declarations within.
17559 -- The scope is created prior to the processing of these declarations.
17560 -- Discriminants are processed first, so that they are visible when
17561 -- processing the other components. The Ekind of the record type itself
17562 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
17564 -- Enter record scope
17568 -- If an incomplete or private type declaration was already given for
17569 -- the type, then this scope already exists, and the discriminants have
17570 -- been declared within. We must verify that the full declaration
17571 -- matches the incomplete one.
17573 Check_Or_Process_Discriminants (N, T, Prev);
17575 Set_Is_Constrained (T, not Has_Discriminants (T));
17576 Set_Has_Delayed_Freeze (T, True);
17578 -- For tagged types add a manually analyzed component corresponding
17579 -- to the component _tag, the corresponding piece of tree will be
17580 -- expanded as part of the freezing actions if it is not a CPP_Class.
17584 -- Do not add the tag unless we are in expansion mode
17586 if Expander_Active then
17587 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
17588 Enter_Name (Tag_Comp);
17590 Set_Ekind (Tag_Comp, E_Component);
17591 Set_Is_Tag (Tag_Comp);
17592 Set_Is_Aliased (Tag_Comp);
17593 Set_Etype (Tag_Comp, RTE (RE_Tag));
17594 Set_DT_Entry_Count (Tag_Comp, No_Uint);
17595 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
17596 Init_Component_Location (Tag_Comp);
17598 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
17599 -- implemented interfaces.
17601 if Has_Interfaces (T) then
17602 Add_Interface_Tag_Components (N, T);
17606 Make_Class_Wide_Type (T);
17607 Set_Primitive_Operations (T, New_Elmt_List);
17610 -- We must suppress range checks when processing the components
17611 -- of a record in the presence of discriminants, since we don't
17612 -- want spurious checks to be generated during their analysis, but
17613 -- must reset the Suppress_Range_Checks flags after having processed
17614 -- the record definition.
17616 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
17617 -- couldn't we just use the normal range check suppression method here.
17618 -- That would seem cleaner ???
17620 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
17621 Set_Kill_Range_Checks (T, True);
17622 Record_Type_Definition (Def, Prev);
17623 Set_Kill_Range_Checks (T, False);
17625 Record_Type_Definition (Def, Prev);
17628 -- Exit from record scope
17632 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
17633 -- the implemented interfaces and associate them an aliased entity.
17636 and then not Is_Empty_List (Interface_List (Def))
17638 Derive_Progenitor_Subprograms (T, T);
17640 end Record_Type_Declaration;
17642 ----------------------------
17643 -- Record_Type_Definition --
17644 ----------------------------
17646 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
17647 Component : Entity_Id;
17648 Ctrl_Components : Boolean := False;
17649 Final_Storage_Only : Boolean;
17653 if Ekind (Prev_T) = E_Incomplete_Type then
17654 T := Full_View (Prev_T);
17659 Final_Storage_Only := not Is_Controlled (T);
17661 -- Ada 2005: check whether an explicit Limited is present in a derived
17662 -- type declaration.
17664 if Nkind (Parent (Def)) = N_Derived_Type_Definition
17665 and then Limited_Present (Parent (Def))
17667 Set_Is_Limited_Record (T);
17670 -- If the component list of a record type is defined by the reserved
17671 -- word null and there is no discriminant part, then the record type has
17672 -- no components and all records of the type are null records (RM 3.7)
17673 -- This procedure is also called to process the extension part of a
17674 -- record extension, in which case the current scope may have inherited
17678 or else No (Component_List (Def))
17679 or else Null_Present (Component_List (Def))
17684 Analyze_Declarations (Component_Items (Component_List (Def)));
17686 if Present (Variant_Part (Component_List (Def))) then
17687 Analyze (Variant_Part (Component_List (Def)));
17691 -- After completing the semantic analysis of the record definition,
17692 -- record components, both new and inherited, are accessible. Set their
17693 -- kind accordingly. Exclude malformed itypes from illegal declarations,
17694 -- whose Ekind may be void.
17696 Component := First_Entity (Current_Scope);
17697 while Present (Component) loop
17698 if Ekind (Component) = E_Void
17699 and then not Is_Itype (Component)
17701 Set_Ekind (Component, E_Component);
17702 Init_Component_Location (Component);
17705 if Has_Task (Etype (Component)) then
17709 if Ekind (Component) /= E_Component then
17712 elsif Has_Controlled_Component (Etype (Component))
17713 or else (Chars (Component) /= Name_uParent
17714 and then Is_Controlled (Etype (Component)))
17716 Set_Has_Controlled_Component (T, True);
17717 Final_Storage_Only :=
17719 and then Finalize_Storage_Only (Etype (Component));
17720 Ctrl_Components := True;
17723 Next_Entity (Component);
17726 -- A Type is Finalize_Storage_Only only if all its controlled components
17729 if Ctrl_Components then
17730 Set_Finalize_Storage_Only (T, Final_Storage_Only);
17733 -- Place reference to end record on the proper entity, which may
17734 -- be a partial view.
17736 if Present (Def) then
17737 Process_End_Label (Def, 'e', Prev_T);
17739 end Record_Type_Definition;
17741 ------------------------
17742 -- Replace_Components --
17743 ------------------------
17745 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
17746 function Process (N : Node_Id) return Traverse_Result;
17752 function Process (N : Node_Id) return Traverse_Result is
17756 if Nkind (N) = N_Discriminant_Specification then
17757 Comp := First_Discriminant (Typ);
17758 while Present (Comp) loop
17759 if Chars (Comp) = Chars (Defining_Identifier (N)) then
17760 Set_Defining_Identifier (N, Comp);
17764 Next_Discriminant (Comp);
17767 elsif Nkind (N) = N_Component_Declaration then
17768 Comp := First_Component (Typ);
17769 while Present (Comp) loop
17770 if Chars (Comp) = Chars (Defining_Identifier (N)) then
17771 Set_Defining_Identifier (N, Comp);
17775 Next_Component (Comp);
17782 procedure Replace is new Traverse_Proc (Process);
17784 -- Start of processing for Replace_Components
17788 end Replace_Components;
17790 -------------------------------
17791 -- Set_Completion_Referenced --
17792 -------------------------------
17794 procedure Set_Completion_Referenced (E : Entity_Id) is
17796 -- If in main unit, mark entity that is a completion as referenced,
17797 -- warnings go on the partial view when needed.
17799 if In_Extended_Main_Source_Unit (E) then
17800 Set_Referenced (E);
17802 end Set_Completion_Referenced;
17804 ---------------------
17805 -- Set_Fixed_Range --
17806 ---------------------
17808 -- The range for fixed-point types is complicated by the fact that we
17809 -- do not know the exact end points at the time of the declaration. This
17810 -- is true for three reasons:
17812 -- A size clause may affect the fudging of the end-points
17813 -- A small clause may affect the values of the end-points
17814 -- We try to include the end-points if it does not affect the size
17816 -- This means that the actual end-points must be established at the point
17817 -- when the type is frozen. Meanwhile, we first narrow the range as
17818 -- permitted (so that it will fit if necessary in a small specified size),
17819 -- and then build a range subtree with these narrowed bounds.
17821 -- Set_Fixed_Range constructs the range from real literal values, and sets
17822 -- the range as the Scalar_Range of the given fixed-point type entity.
17824 -- The parent of this range is set to point to the entity so that it is
17825 -- properly hooked into the tree (unlike normal Scalar_Range entries for
17826 -- other scalar types, which are just pointers to the range in the
17827 -- original tree, this would otherwise be an orphan).
17829 -- The tree is left unanalyzed. When the type is frozen, the processing
17830 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
17831 -- analyzed, and uses this as an indication that it should complete
17832 -- work on the range (it will know the final small and size values).
17834 procedure Set_Fixed_Range
17840 S : constant Node_Id :=
17842 Low_Bound => Make_Real_Literal (Loc, Lo),
17843 High_Bound => Make_Real_Literal (Loc, Hi));
17845 Set_Scalar_Range (E, S);
17847 end Set_Fixed_Range;
17849 ----------------------------------
17850 -- Set_Scalar_Range_For_Subtype --
17851 ----------------------------------
17853 procedure Set_Scalar_Range_For_Subtype
17854 (Def_Id : Entity_Id;
17858 Kind : constant Entity_Kind := Ekind (Def_Id);
17861 Set_Scalar_Range (Def_Id, R);
17863 -- We need to link the range into the tree before resolving it so
17864 -- that types that are referenced, including importantly the subtype
17865 -- itself, are properly frozen (Freeze_Expression requires that the
17866 -- expression be properly linked into the tree). Of course if it is
17867 -- already linked in, then we do not disturb the current link.
17869 if No (Parent (R)) then
17870 Set_Parent (R, Def_Id);
17873 -- Reset the kind of the subtype during analysis of the range, to
17874 -- catch possible premature use in the bounds themselves.
17876 Set_Ekind (Def_Id, E_Void);
17877 Process_Range_Expr_In_Decl (R, Subt);
17878 Set_Ekind (Def_Id, Kind);
17879 end Set_Scalar_Range_For_Subtype;
17881 --------------------------------------------------------
17882 -- Set_Stored_Constraint_From_Discriminant_Constraint --
17883 --------------------------------------------------------
17885 procedure Set_Stored_Constraint_From_Discriminant_Constraint
17889 -- Make sure set if encountered during Expand_To_Stored_Constraint
17891 Set_Stored_Constraint (E, No_Elist);
17893 -- Give it the right value
17895 if Is_Constrained (E) and then Has_Discriminants (E) then
17896 Set_Stored_Constraint (E,
17897 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
17899 end Set_Stored_Constraint_From_Discriminant_Constraint;
17901 -------------------------------------
17902 -- Signed_Integer_Type_Declaration --
17903 -------------------------------------
17905 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17906 Implicit_Base : Entity_Id;
17907 Base_Typ : Entity_Id;
17910 Errs : Boolean := False;
17914 function Can_Derive_From (E : Entity_Id) return Boolean;
17915 -- Determine whether given bounds allow derivation from specified type
17917 procedure Check_Bound (Expr : Node_Id);
17918 -- Check bound to make sure it is integral and static. If not, post
17919 -- appropriate error message and set Errs flag
17921 ---------------------
17922 -- Can_Derive_From --
17923 ---------------------
17925 -- Note we check both bounds against both end values, to deal with
17926 -- strange types like ones with a range of 0 .. -12341234.
17928 function Can_Derive_From (E : Entity_Id) return Boolean is
17929 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
17930 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
17932 return Lo <= Lo_Val and then Lo_Val <= Hi
17934 Lo <= Hi_Val and then Hi_Val <= Hi;
17935 end Can_Derive_From;
17941 procedure Check_Bound (Expr : Node_Id) is
17943 -- If a range constraint is used as an integer type definition, each
17944 -- bound of the range must be defined by a static expression of some
17945 -- integer type, but the two bounds need not have the same integer
17946 -- type (Negative bounds are allowed.) (RM 3.5.4)
17948 if not Is_Integer_Type (Etype (Expr)) then
17950 ("integer type definition bounds must be of integer type", Expr);
17953 elsif not Is_OK_Static_Expression (Expr) then
17954 Flag_Non_Static_Expr
17955 ("non-static expression used for integer type bound!", Expr);
17958 -- The bounds are folded into literals, and we set their type to be
17959 -- universal, to avoid typing difficulties: we cannot set the type
17960 -- of the literal to the new type, because this would be a forward
17961 -- reference for the back end, and if the original type is user-
17962 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
17965 if Is_Entity_Name (Expr) then
17966 Fold_Uint (Expr, Expr_Value (Expr), True);
17969 Set_Etype (Expr, Universal_Integer);
17973 -- Start of processing for Signed_Integer_Type_Declaration
17976 -- Create an anonymous base type
17979 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
17981 -- Analyze and check the bounds, they can be of any integer type
17983 Lo := Low_Bound (Def);
17984 Hi := High_Bound (Def);
17986 -- Arbitrarily use Integer as the type if either bound had an error
17988 if Hi = Error or else Lo = Error then
17989 Base_Typ := Any_Integer;
17990 Set_Error_Posted (T, True);
17992 -- Here both bounds are OK expressions
17995 Analyze_And_Resolve (Lo, Any_Integer);
17996 Analyze_And_Resolve (Hi, Any_Integer);
18002 Hi := Type_High_Bound (Standard_Long_Long_Integer);
18003 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
18006 -- Find type to derive from
18008 Lo_Val := Expr_Value (Lo);
18009 Hi_Val := Expr_Value (Hi);
18011 if Can_Derive_From (Standard_Short_Short_Integer) then
18012 Base_Typ := Base_Type (Standard_Short_Short_Integer);
18014 elsif Can_Derive_From (Standard_Short_Integer) then
18015 Base_Typ := Base_Type (Standard_Short_Integer);
18017 elsif Can_Derive_From (Standard_Integer) then
18018 Base_Typ := Base_Type (Standard_Integer);
18020 elsif Can_Derive_From (Standard_Long_Integer) then
18021 Base_Typ := Base_Type (Standard_Long_Integer);
18023 elsif Can_Derive_From (Standard_Long_Long_Integer) then
18024 Base_Typ := Base_Type (Standard_Long_Long_Integer);
18027 Base_Typ := Base_Type (Standard_Long_Long_Integer);
18028 Error_Msg_N ("integer type definition bounds out of range", Def);
18029 Hi := Type_High_Bound (Standard_Long_Long_Integer);
18030 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
18034 -- Complete both implicit base and declared first subtype entities
18036 Set_Etype (Implicit_Base, Base_Typ);
18037 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
18038 Set_Size_Info (Implicit_Base, (Base_Typ));
18039 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
18040 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18042 Set_Ekind (T, E_Signed_Integer_Subtype);
18043 Set_Etype (T, Implicit_Base);
18045 Set_Size_Info (T, (Implicit_Base));
18046 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
18047 Set_Scalar_Range (T, Def);
18048 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
18049 Set_Is_Constrained (T);
18050 end Signed_Integer_Type_Declaration;