1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2010, 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_Prag; use Sem_Prag;
65 with Sem_Res; use Sem_Res;
66 with Sem_Smem; use Sem_Smem;
67 with Sem_Type; use Sem_Type;
68 with Sem_Util; use Sem_Util;
69 with Sem_Warn; use Sem_Warn;
70 with Stand; use Stand;
71 with Sinfo; use Sinfo;
72 with Sinput; use Sinput;
73 with Snames; use Snames;
74 with Targparm; use Targparm;
75 with Tbuild; use Tbuild;
76 with Ttypes; use Ttypes;
77 with Uintp; use Uintp;
78 with Urealp; use Urealp;
80 package body Sem_Ch3 is
82 -----------------------
83 -- Local Subprograms --
84 -----------------------
86 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
87 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
88 -- abstract interface types implemented by a record type or a derived
91 procedure Build_Derived_Type
93 Parent_Type : Entity_Id;
94 Derived_Type : Entity_Id;
95 Is_Completion : Boolean;
96 Derive_Subps : Boolean := True);
97 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
98 -- the N_Full_Type_Declaration node containing the derived type definition.
99 -- Parent_Type is the entity for the parent type in the derived type
100 -- definition and Derived_Type the actual derived type. Is_Completion must
101 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
102 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
103 -- completion of a private type declaration. If Is_Completion is set to
104 -- True, N is the completion of a private type declaration and Derived_Type
105 -- is different from the defining identifier inside N (i.e. Derived_Type /=
106 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
107 -- subprograms should be derived. The only case where this parameter is
108 -- False is when Build_Derived_Type is recursively called to process an
109 -- implicit derived full type for a type derived from a private type (in
110 -- that case the subprograms must only be derived for the private view of
113 -- ??? These flags need a bit of re-examination and re-documentation:
114 -- ??? are they both necessary (both seem related to the recursion)?
116 procedure Build_Derived_Access_Type
118 Parent_Type : Entity_Id;
119 Derived_Type : Entity_Id);
120 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
121 -- create an implicit base if the parent type is constrained or if the
122 -- subtype indication has a constraint.
124 procedure Build_Derived_Array_Type
126 Parent_Type : Entity_Id;
127 Derived_Type : Entity_Id);
128 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
129 -- create an implicit base if the parent type is constrained or if the
130 -- subtype indication has a constraint.
132 procedure Build_Derived_Concurrent_Type
134 Parent_Type : Entity_Id;
135 Derived_Type : Entity_Id);
136 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
137 -- protected type, inherit entries and protected subprograms, check
138 -- legality of discriminant constraints if any.
140 procedure Build_Derived_Enumeration_Type
142 Parent_Type : Entity_Id;
143 Derived_Type : Entity_Id);
144 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
145 -- type, we must create a new list of literals. Types derived from
146 -- Character and [Wide_]Wide_Character are special-cased.
148 procedure Build_Derived_Numeric_Type
150 Parent_Type : Entity_Id;
151 Derived_Type : Entity_Id);
152 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
153 -- an anonymous base type, and propagate constraint to subtype if needed.
155 procedure Build_Derived_Private_Type
157 Parent_Type : Entity_Id;
158 Derived_Type : Entity_Id;
159 Is_Completion : Boolean;
160 Derive_Subps : Boolean := True);
161 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
162 -- because the parent may or may not have a completion, and the derivation
163 -- may itself be a completion.
165 procedure Build_Derived_Record_Type
167 Parent_Type : Entity_Id;
168 Derived_Type : Entity_Id;
169 Derive_Subps : Boolean := True);
170 -- Subsidiary procedure for Build_Derived_Type and
171 -- Analyze_Private_Extension_Declaration used for tagged and untagged
172 -- record types. All parameters are as in Build_Derived_Type except that
173 -- N, in addition to being an N_Full_Type_Declaration node, can also be an
174 -- N_Private_Extension_Declaration node. See the definition of this routine
175 -- for much more info. Derive_Subps indicates whether subprograms should
176 -- be derived from the parent type. The only case where Derive_Subps is
177 -- False is for an implicit derived full type for a type derived from a
178 -- private type (see Build_Derived_Type).
180 procedure Build_Discriminal (Discrim : Entity_Id);
181 -- Create the discriminal corresponding to discriminant Discrim, that is
182 -- the parameter corresponding to Discrim to be used in initialization
183 -- procedures for the type where Discrim is a discriminant. Discriminals
184 -- are not used during semantic analysis, and are not fully defined
185 -- entities until expansion. Thus they are not given a scope until
186 -- initialization procedures are built.
188 function Build_Discriminant_Constraints
191 Derived_Def : Boolean := False) return Elist_Id;
192 -- Validate discriminant constraints and return the list of the constraints
193 -- in order of discriminant declarations, where T is the discriminated
194 -- unconstrained type. Def is the N_Subtype_Indication node where the
195 -- discriminants constraints for T are specified. Derived_Def is True
196 -- when building the discriminant constraints in a derived type definition
197 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
198 -- type and Def is the constraint "(xxx)" on T and this routine sets the
199 -- Corresponding_Discriminant field of the discriminants in the derived
200 -- type D to point to the corresponding discriminants in the parent type T.
202 procedure Build_Discriminated_Subtype
206 Related_Nod : Node_Id;
207 For_Access : Boolean := False);
208 -- Subsidiary procedure to Constrain_Discriminated_Type and to
209 -- Process_Incomplete_Dependents. Given
211 -- T (a possibly discriminated base type)
212 -- Def_Id (a very partially built subtype for T),
214 -- the call completes Def_Id to be the appropriate E_*_Subtype.
216 -- The Elist is the list of discriminant constraints if any (it is set
217 -- to No_Elist if T is not a discriminated type, and to an empty list if
218 -- T has discriminants but there are no discriminant constraints). The
219 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
220 -- The For_Access says whether or not this subtype is really constraining
221 -- an access type. That is its sole purpose is the designated type of an
222 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
223 -- is built to avoid freezing T when the access subtype is frozen.
225 function Build_Scalar_Bound
228 Der_T : Entity_Id) return Node_Id;
229 -- The bounds of a derived scalar type are conversions of the bounds of
230 -- the parent type. Optimize the representation if the bounds are literals.
231 -- Needs a more complete spec--what are the parameters exactly, and what
232 -- exactly is the returned value, and how is Bound affected???
234 procedure Build_Underlying_Full_View
238 -- If the completion of a private type is itself derived from a private
239 -- type, or if the full view of a private subtype is itself private, the
240 -- back-end has no way to compute the actual size of this type. We build
241 -- an internal subtype declaration of the proper parent type to convey
242 -- this information. This extra mechanism is needed because a full
243 -- view cannot itself have a full view (it would get clobbered during
246 procedure Check_Access_Discriminant_Requires_Limited
249 -- Check the restriction that the type to which an access discriminant
250 -- belongs must be a concurrent type or a descendant of a type with
251 -- the reserved word 'limited' in its declaration.
253 procedure Check_Anonymous_Access_Components
257 Comp_List : Node_Id);
258 -- Ada 2005 AI-382: an access component in a record definition can refer to
259 -- the enclosing record, in which case it denotes the type itself, and not
260 -- the current instance of the type. We create an anonymous access type for
261 -- the component, and flag it as an access to a component, so accessibility
262 -- checks are properly performed on it. The declaration of the access type
263 -- is placed ahead of that of the record to prevent order-of-elaboration
264 -- circularity issues in Gigi. We create an incomplete type for the record
265 -- declaration, which is the designated type of the anonymous access.
267 procedure Check_Delta_Expression (E : Node_Id);
268 -- Check that the expression represented by E is suitable for use as a
269 -- delta expression, i.e. it is of real type and is static.
271 procedure Check_Digits_Expression (E : Node_Id);
272 -- Check that the expression represented by E is suitable for use as a
273 -- digits expression, i.e. it is of integer type, positive and static.
275 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
276 -- Validate the initialization of an object declaration. T is the required
277 -- type, and Exp is the initialization expression.
279 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
280 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
282 procedure Check_Or_Process_Discriminants
285 Prev : Entity_Id := Empty);
286 -- If N is the full declaration of the completion T of an incomplete or
287 -- private type, check its discriminants (which are already known to be
288 -- conformant with those of the partial view, see Find_Type_Name),
289 -- otherwise process them. Prev is the entity of the partial declaration,
292 procedure Check_Real_Bound (Bound : Node_Id);
293 -- Check given bound for being of real type and static. If not, post an
294 -- appropriate message, and rewrite the bound with the real literal zero.
296 procedure Constant_Redeclaration
300 -- Various checks on legality of full declaration of deferred constant.
301 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
302 -- node. The caller has not yet set any attributes of this entity.
304 function Contain_Interface
306 Ifaces : Elist_Id) return Boolean;
307 -- Ada 2005: Determine whether Iface is present in the list Ifaces
309 procedure Convert_Scalar_Bounds
311 Parent_Type : Entity_Id;
312 Derived_Type : Entity_Id;
314 -- For derived scalar types, convert the bounds in the type definition to
315 -- the derived type, and complete their analysis. Given a constraint of the
316 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
317 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
318 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
319 -- subtype are conversions of those bounds to the derived_type, so that
320 -- their typing is consistent.
322 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
323 -- Copies attributes from array base type T2 to array base type T1. Copies
324 -- only attributes that apply to base types, but not subtypes.
326 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
327 -- Copies attributes from array subtype T2 to array subtype T1. Copies
328 -- attributes that apply to both subtypes and base types.
330 procedure Create_Constrained_Components
334 Constraints : Elist_Id);
335 -- Build the list of entities for a constrained discriminated record
336 -- subtype. If a component depends on a discriminant, replace its subtype
337 -- using the discriminant values in the discriminant constraint. Subt
338 -- is the defining identifier for the subtype whose list of constrained
339 -- entities we will create. Decl_Node is the type declaration node where
340 -- we will attach all the itypes created. Typ is the base discriminated
341 -- type for the subtype Subt. Constraints is the list of discriminant
342 -- constraints for Typ.
344 function Constrain_Component_Type
346 Constrained_Typ : Entity_Id;
347 Related_Node : Node_Id;
349 Constraints : Elist_Id) return Entity_Id;
350 -- Given a discriminated base type Typ, a list of discriminant constraint
351 -- Constraints for Typ and a component of Typ, with type Compon_Type,
352 -- create and return the type corresponding to Compon_type where all
353 -- discriminant references are replaced with the corresponding constraint.
354 -- If no discriminant references occur in Compon_Typ then return it as is.
355 -- Constrained_Typ is the final constrained subtype to which the
356 -- constrained Compon_Type belongs. Related_Node is the node where we will
357 -- attach all the itypes created.
359 -- Above description is confused, what is Compon_Type???
361 procedure Constrain_Access
362 (Def_Id : in out Entity_Id;
364 Related_Nod : Node_Id);
365 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
366 -- an anonymous type created for a subtype indication. In that case it is
367 -- created in the procedure and attached to Related_Nod.
369 procedure Constrain_Array
370 (Def_Id : in out Entity_Id;
372 Related_Nod : Node_Id;
373 Related_Id : Entity_Id;
375 -- Apply a list of index constraints to an unconstrained array type. The
376 -- first parameter is the entity for the resulting subtype. A value of
377 -- Empty for Def_Id indicates that an implicit type must be created, but
378 -- creation is delayed (and must be done by this procedure) because other
379 -- subsidiary implicit types must be created first (which is why Def_Id
380 -- is an in/out parameter). The second parameter is a subtype indication
381 -- node for the constrained array to be created (e.g. something of the
382 -- form string (1 .. 10)). Related_Nod gives the place where this type
383 -- has to be inserted in the tree. The Related_Id and Suffix parameters
384 -- are used to build the associated Implicit type name.
386 procedure Constrain_Concurrent
387 (Def_Id : in out Entity_Id;
389 Related_Nod : Node_Id;
390 Related_Id : Entity_Id;
392 -- Apply list of discriminant constraints to an unconstrained concurrent
395 -- SI is the N_Subtype_Indication node containing the constraint and
396 -- the unconstrained type to constrain.
398 -- Def_Id is the entity for the resulting constrained subtype. A value
399 -- of Empty for Def_Id indicates that an implicit type must be created,
400 -- but creation is delayed (and must be done by this procedure) because
401 -- other subsidiary implicit types must be created first (which is why
402 -- Def_Id is an in/out parameter).
404 -- Related_Nod gives the place where this type has to be inserted
407 -- The last two arguments are used to create its external name if needed.
409 function Constrain_Corresponding_Record
410 (Prot_Subt : Entity_Id;
411 Corr_Rec : Entity_Id;
412 Related_Nod : Node_Id;
413 Related_Id : Entity_Id) return Entity_Id;
414 -- When constraining a protected type or task type with discriminants,
415 -- constrain the corresponding record with the same discriminant values.
417 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
418 -- Constrain a decimal fixed point type with a digits constraint and/or a
419 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
421 procedure Constrain_Discriminated_Type
424 Related_Nod : Node_Id;
425 For_Access : Boolean := False);
426 -- Process discriminant constraints of composite type. Verify that values
427 -- have been provided for all discriminants, that the original type is
428 -- unconstrained, and that the types of the supplied expressions match
429 -- the discriminant types. The first three parameters are like in routine
430 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
433 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
434 -- Constrain an enumeration type with a range constraint. This is identical
435 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
437 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
438 -- Constrain a floating point type with either a digits constraint
439 -- and/or a range constraint, building a E_Floating_Point_Subtype.
441 procedure Constrain_Index
444 Related_Nod : Node_Id;
445 Related_Id : Entity_Id;
448 -- Process an index constraint S in a constrained array declaration. The
449 -- constraint can be a subtype name, or a range with or without an explicit
450 -- subtype mark. The index is the corresponding index of the unconstrained
451 -- array. The Related_Id and Suffix parameters are used to build the
452 -- associated Implicit type name.
454 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
455 -- Build subtype of a signed or modular integer type
457 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
458 -- Constrain an ordinary fixed point type with a range constraint, and
459 -- build an E_Ordinary_Fixed_Point_Subtype entity.
461 procedure Copy_And_Swap (Priv, Full : Entity_Id);
462 -- Copy the Priv entity into the entity of its full declaration then swap
463 -- the two entities in such a manner that the former private type is now
464 -- seen as a full type.
466 procedure Decimal_Fixed_Point_Type_Declaration
469 -- Create a new decimal fixed point type, and apply the constraint to
470 -- obtain a subtype of this new type.
472 procedure Complete_Private_Subtype
475 Full_Base : Entity_Id;
476 Related_Nod : Node_Id);
477 -- Complete the implicit full view of a private subtype by setting the
478 -- appropriate semantic fields. If the full view of the parent is a record
479 -- type, build constrained components of subtype.
481 procedure Derive_Progenitor_Subprograms
482 (Parent_Type : Entity_Id;
483 Tagged_Type : Entity_Id);
484 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
485 -- operations of progenitors of Tagged_Type, and replace the subsidiary
486 -- subtypes with Tagged_Type, to build the specs of the inherited interface
487 -- primitives. The derived primitives are aliased to those of the
488 -- interface. This routine takes care also of transferring to the full view
489 -- subprograms associated with the partial view of Tagged_Type that cover
490 -- interface primitives.
492 procedure Derived_Standard_Character
494 Parent_Type : Entity_Id;
495 Derived_Type : Entity_Id);
496 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
497 -- derivations from types Standard.Character and Standard.Wide_Character.
499 procedure Derived_Type_Declaration
502 Is_Completion : Boolean);
503 -- Process a derived type declaration. Build_Derived_Type is invoked
504 -- to process the actual derived type definition. Parameters N and
505 -- Is_Completion have the same meaning as in Build_Derived_Type.
506 -- T is the N_Defining_Identifier for the entity defined in the
507 -- N_Full_Type_Declaration node N, that is T is the derived type.
509 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
510 -- Insert each literal in symbol table, as an overloadable identifier. Each
511 -- enumeration type is mapped into a sequence of integers, and each literal
512 -- is defined as a constant with integer value. If any of the literals are
513 -- character literals, the type is a character type, which means that
514 -- strings are legal aggregates for arrays of components of the type.
516 function Expand_To_Stored_Constraint
518 Constraint : Elist_Id) return Elist_Id;
519 -- Given a constraint (i.e. a list of expressions) on the discriminants of
520 -- Typ, expand it into a constraint on the stored discriminants and return
521 -- the new list of expressions constraining the stored discriminants.
523 function Find_Type_Of_Object
525 Related_Nod : Node_Id) return Entity_Id;
526 -- Get type entity for object referenced by Obj_Def, attaching the
527 -- implicit types generated to Related_Nod
529 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
530 -- Create a new float and apply the constraint to obtain subtype of it
532 function Has_Range_Constraint (N : Node_Id) return Boolean;
533 -- Given an N_Subtype_Indication node N, return True if a range constraint
534 -- is present, either directly, or as part of a digits or delta constraint.
535 -- In addition, a digits constraint in the decimal case returns True, since
536 -- it establishes a default range if no explicit range is present.
538 function Inherit_Components
540 Parent_Base : Entity_Id;
541 Derived_Base : Entity_Id;
543 Inherit_Discr : Boolean;
544 Discs : Elist_Id) return Elist_Id;
545 -- Called from Build_Derived_Record_Type to inherit the components of
546 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
547 -- For more information on derived types and component inheritance please
548 -- consult the comment above the body of Build_Derived_Record_Type.
550 -- N is the original derived type declaration
552 -- Is_Tagged is set if we are dealing with tagged types
554 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
555 -- Parent_Base, otherwise no discriminants are inherited.
557 -- Discs gives the list of constraints that apply to Parent_Base in the
558 -- derived type declaration. If Discs is set to No_Elist, then we have
559 -- the following situation:
561 -- type Parent (D1..Dn : ..) is [tagged] record ...;
562 -- type Derived is new Parent [with ...];
564 -- which gets treated as
566 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
568 -- For untagged types the returned value is an association list. The list
569 -- starts from the association (Parent_Base => Derived_Base), and then it
570 -- contains a sequence of the associations of the form
572 -- (Old_Component => New_Component),
574 -- where Old_Component is the Entity_Id of a component in Parent_Base and
575 -- New_Component is the Entity_Id of the corresponding component in
576 -- Derived_Base. For untagged records, this association list is needed when
577 -- copying the record declaration for the derived base. In the tagged case
578 -- the value returned is irrelevant.
580 function Is_Valid_Constraint_Kind
582 Constraint_Kind : Node_Kind) return Boolean;
583 -- Returns True if it is legal to apply the given kind of constraint to the
584 -- given kind of type (index constraint to an array type, for example).
586 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
587 -- Create new modular type. Verify that modulus is in bounds
589 procedure New_Concatenation_Op (Typ : Entity_Id);
590 -- Create an abbreviated declaration for an operator in order to
591 -- materialize concatenation on array types.
593 procedure Ordinary_Fixed_Point_Type_Declaration
596 -- Create a new ordinary fixed point type, and apply the constraint to
597 -- obtain subtype of it.
599 procedure Prepare_Private_Subtype_Completion
601 Related_Nod : Node_Id);
602 -- Id is a subtype of some private type. Creates the full declaration
603 -- associated with Id whenever possible, i.e. when the full declaration
604 -- of the base type is already known. Records each subtype into
605 -- Private_Dependents of the base type.
607 procedure Process_Incomplete_Dependents
611 -- Process all entities that depend on an incomplete type. There include
612 -- subtypes, subprogram types that mention the incomplete type in their
613 -- profiles, and subprogram with access parameters that designate the
616 -- Inc_T is the defining identifier of an incomplete type declaration, its
617 -- Ekind is E_Incomplete_Type.
619 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
621 -- Full_T is N's defining identifier.
623 -- Subtypes of incomplete types with discriminants are completed when the
624 -- parent type is. This is simpler than private subtypes, because they can
625 -- only appear in the same scope, and there is no need to exchange views.
626 -- Similarly, access_to_subprogram types may have a parameter or a return
627 -- type that is an incomplete type, and that must be replaced with the
630 -- If the full type is tagged, subprogram with access parameters that
631 -- designated the incomplete may be primitive operations of the full type,
632 -- and have to be processed accordingly.
634 procedure Process_Real_Range_Specification (Def : Node_Id);
635 -- Given the type definition for a real type, this procedure processes and
636 -- checks the real range specification of this type definition if one is
637 -- present. If errors are found, error messages are posted, and the
638 -- Real_Range_Specification of Def is reset to Empty.
640 procedure Record_Type_Declaration
644 -- Process a record type declaration (for both untagged and tagged
645 -- records). Parameters T and N are exactly like in procedure
646 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
647 -- for this routine. If this is the completion of an incomplete type
648 -- declaration, Prev is the entity of the incomplete declaration, used for
649 -- cross-referencing. Otherwise Prev = T.
651 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
652 -- This routine is used to process the actual record type definition (both
653 -- for untagged and tagged records). Def is a record type definition node.
654 -- This procedure analyzes the components in this record type definition.
655 -- Prev_T is the entity for the enclosing record type. It is provided so
656 -- that its Has_Task flag can be set if any of the component have Has_Task
657 -- set. If the declaration is the completion of an incomplete type
658 -- declaration, Prev_T is the original incomplete type, whose full view is
661 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
662 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
663 -- build a copy of the declaration tree of the parent, and we create
664 -- independently the list of components for the derived type. Semantic
665 -- information uses the component entities, but record representation
666 -- clauses are validated on the declaration tree. This procedure replaces
667 -- discriminants and components in the declaration with those that have
668 -- been created by Inherit_Components.
670 procedure Set_Fixed_Range
675 -- Build a range node with the given bounds and set it as the Scalar_Range
676 -- of the given fixed-point type entity. Loc is the source location used
677 -- for the constructed range. See body for further details.
679 procedure Set_Scalar_Range_For_Subtype
683 -- This routine is used to set the scalar range field for a subtype given
684 -- Def_Id, the entity for the subtype, and R, the range expression for the
685 -- scalar range. Subt provides the parent subtype to be used to analyze,
686 -- resolve, and check the given range.
688 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
689 -- Create a new signed integer entity, and apply the constraint to obtain
690 -- the required first named subtype of this type.
692 procedure Set_Stored_Constraint_From_Discriminant_Constraint
694 -- E is some record type. This routine computes E's Stored_Constraint
695 -- from its Discriminant_Constraint.
697 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
698 -- Check that an entity in a list of progenitors is an interface,
699 -- emit error otherwise.
701 -----------------------
702 -- Access_Definition --
703 -----------------------
705 function Access_Definition
706 (Related_Nod : Node_Id;
707 N : Node_Id) return Entity_Id
709 Loc : constant Source_Ptr := Sloc (Related_Nod);
710 Anon_Type : Entity_Id;
711 Anon_Scope : Entity_Id;
712 Desig_Type : Entity_Id;
714 Enclosing_Prot_Type : Entity_Id := Empty;
717 Check_Formal_Restriction ("access type is not allowed", N);
719 if Is_Entry (Current_Scope)
720 and then Is_Task_Type (Etype (Scope (Current_Scope)))
722 Error_Msg_N ("task entries cannot have access parameters", N);
726 -- Ada 2005: for an object declaration the corresponding anonymous
727 -- type is declared in the current scope.
729 -- If the access definition is the return type of another access to
730 -- function, scope is the current one, because it is the one of the
731 -- current type declaration.
733 if Nkind_In (Related_Nod, N_Object_Declaration,
734 N_Access_Function_Definition)
736 Anon_Scope := Current_Scope;
738 -- For the anonymous function result case, retrieve the scope of the
739 -- function specification's associated entity rather than using the
740 -- current scope. The current scope will be the function itself if the
741 -- formal part is currently being analyzed, but will be the parent scope
742 -- in the case of a parameterless function, and we always want to use
743 -- the function's parent scope. Finally, if the function is a child
744 -- unit, we must traverse the tree to retrieve the proper entity.
746 elsif Nkind (Related_Nod) = N_Function_Specification
747 and then Nkind (Parent (N)) /= N_Parameter_Specification
749 -- If the current scope is a protected type, the anonymous access
750 -- is associated with one of the protected operations, and must
751 -- be available in the scope that encloses the protected declaration.
752 -- Otherwise the type is in the scope enclosing the subprogram.
754 -- If the function has formals, The return type of a subprogram
755 -- declaration is analyzed in the scope of the subprogram (see
756 -- Process_Formals) and thus the protected type, if present, is
757 -- the scope of the current function scope.
759 if Ekind (Current_Scope) = E_Protected_Type then
760 Enclosing_Prot_Type := Current_Scope;
762 elsif Ekind (Current_Scope) = E_Function
763 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
765 Enclosing_Prot_Type := Scope (Current_Scope);
768 if Present (Enclosing_Prot_Type) then
769 Anon_Scope := Scope (Enclosing_Prot_Type);
772 Anon_Scope := Scope (Defining_Entity (Related_Nod));
776 -- For access formals, access components, and access discriminants,
777 -- the scope is that of the enclosing declaration,
779 Anon_Scope := Scope (Current_Scope);
784 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
787 and then Ada_Version >= Ada_2005
789 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
792 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
793 -- the corresponding semantic routine
795 if Present (Access_To_Subprogram_Definition (N)) then
796 Access_Subprogram_Declaration
797 (T_Name => Anon_Type,
798 T_Def => Access_To_Subprogram_Definition (N));
800 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
802 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
805 (Anon_Type, E_Anonymous_Access_Subprogram_Type);
808 Set_Can_Use_Internal_Rep
809 (Anon_Type, not Always_Compatible_Rep_On_Target);
811 -- If the anonymous access is associated with a protected operation
812 -- create a reference to it after the enclosing protected definition
813 -- because the itype will be used in the subsequent bodies.
815 if Ekind (Current_Scope) = E_Protected_Type then
816 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
822 Find_Type (Subtype_Mark (N));
823 Desig_Type := Entity (Subtype_Mark (N));
825 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
826 Set_Etype (Anon_Type, Anon_Type);
828 -- Make sure the anonymous access type has size and alignment fields
829 -- set, as required by gigi. This is necessary in the case of the
830 -- Task_Body_Procedure.
832 if not Has_Private_Component (Desig_Type) then
833 Layout_Type (Anon_Type);
836 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
837 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
838 -- the null value is allowed. In Ada 95 the null value is never allowed.
840 if Ada_Version >= Ada_2005 then
841 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
843 Set_Can_Never_Be_Null (Anon_Type, True);
846 -- The anonymous access type is as public as the discriminated type or
847 -- subprogram that defines it. It is imported (for back-end purposes)
848 -- if the designated type is.
850 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
852 -- Ada 2005 (AI-231): Propagate the access-constant attribute
854 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
856 -- The context is either a subprogram declaration, object declaration,
857 -- or an access discriminant, in a private or a full type declaration.
858 -- In the case of a subprogram, if the designated type is incomplete,
859 -- the operation will be a primitive operation of the full type, to be
860 -- updated subsequently. If the type is imported through a limited_with
861 -- clause, the subprogram is not a primitive operation of the type
862 -- (which is declared elsewhere in some other scope).
864 if Ekind (Desig_Type) = E_Incomplete_Type
865 and then not From_With_Type (Desig_Type)
866 and then Is_Overloadable (Current_Scope)
868 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
869 Set_Has_Delayed_Freeze (Current_Scope);
872 -- Ada 2005: if the designated type is an interface that may contain
873 -- tasks, create a Master entity for the declaration. This must be done
874 -- before expansion of the full declaration, because the declaration may
875 -- include an expression that is an allocator, whose expansion needs the
876 -- proper Master for the created tasks.
878 if Nkind (Related_Nod) = N_Object_Declaration
879 and then Expander_Active
881 if Is_Interface (Desig_Type)
882 and then Is_Limited_Record (Desig_Type)
884 Build_Class_Wide_Master (Anon_Type);
886 -- Similarly, if the type is an anonymous access that designates
887 -- tasks, create a master entity for it in the current context.
889 elsif Has_Task (Desig_Type)
890 and then Comes_From_Source (Related_Nod)
891 and then not Restriction_Active (No_Task_Hierarchy)
893 if not Has_Master_Entity (Current_Scope) then
895 Make_Object_Declaration (Loc,
896 Defining_Identifier =>
897 Make_Defining_Identifier (Loc, Name_uMaster),
898 Constant_Present => True,
900 New_Reference_To (RTE (RE_Master_Id), Loc),
902 Make_Explicit_Dereference (Loc,
903 New_Reference_To (RTE (RE_Current_Master), Loc)));
905 Insert_Before (Related_Nod, Decl);
908 Set_Master_Id (Anon_Type, Defining_Identifier (Decl));
909 Set_Has_Master_Entity (Current_Scope);
911 Build_Master_Renaming (Related_Nod, Anon_Type);
916 -- For a private component of a protected type, it is imperative that
917 -- the back-end elaborate the type immediately after the protected
918 -- declaration, because this type will be used in the declarations
919 -- created for the component within each protected body, so we must
920 -- create an itype reference for it now.
922 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
923 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
925 -- Similarly, if the access definition is the return result of a
926 -- function, create an itype reference for it because it will be used
927 -- within the function body. For a regular function that is not a
928 -- compilation unit, insert reference after the declaration. For a
929 -- protected operation, insert it after the enclosing protected type
930 -- declaration. In either case, do not create a reference for a type
931 -- obtained through a limited_with clause, because this would introduce
932 -- semantic dependencies.
934 -- Similarly, do not create a reference if the designated type is a
935 -- generic formal, because no use of it will reach the backend.
937 elsif Nkind (Related_Nod) = N_Function_Specification
938 and then not From_With_Type (Desig_Type)
939 and then not Is_Generic_Type (Desig_Type)
941 if Present (Enclosing_Prot_Type) then
942 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
944 elsif Is_List_Member (Parent (Related_Nod))
945 and then Nkind (Parent (N)) /= N_Parameter_Specification
947 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
950 -- Finally, create an itype reference for an object declaration of an
951 -- anonymous access type. This is strictly necessary only for deferred
952 -- constants, but in any case will avoid out-of-scope problems in the
955 elsif Nkind (Related_Nod) = N_Object_Declaration then
956 Build_Itype_Reference (Anon_Type, Related_Nod);
960 end Access_Definition;
962 -----------------------------------
963 -- Access_Subprogram_Declaration --
964 -----------------------------------
966 procedure Access_Subprogram_Declaration
971 procedure Check_For_Premature_Usage (Def : Node_Id);
972 -- Check that type T_Name is not used, directly or recursively, as a
973 -- parameter or a return type in Def. Def is either a subtype, an
974 -- access_definition, or an access_to_subprogram_definition.
976 -------------------------------
977 -- Check_For_Premature_Usage --
978 -------------------------------
980 procedure Check_For_Premature_Usage (Def : Node_Id) is
984 -- Check for a subtype mark
986 if Nkind (Def) in N_Has_Etype then
987 if Etype (Def) = T_Name then
989 ("type& cannot be used before end of its declaration", Def);
992 -- If this is not a subtype, then this is an access_definition
994 elsif Nkind (Def) = N_Access_Definition then
995 if Present (Access_To_Subprogram_Definition (Def)) then
996 Check_For_Premature_Usage
997 (Access_To_Subprogram_Definition (Def));
999 Check_For_Premature_Usage (Subtype_Mark (Def));
1002 -- The only cases left are N_Access_Function_Definition and
1003 -- N_Access_Procedure_Definition.
1006 if Present (Parameter_Specifications (Def)) then
1007 Param := First (Parameter_Specifications (Def));
1008 while Present (Param) loop
1009 Check_For_Premature_Usage (Parameter_Type (Param));
1010 Param := Next (Param);
1014 if Nkind (Def) = N_Access_Function_Definition then
1015 Check_For_Premature_Usage (Result_Definition (Def));
1018 end Check_For_Premature_Usage;
1022 Formals : constant List_Id := Parameter_Specifications (T_Def);
1025 Desig_Type : constant Entity_Id :=
1026 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1028 -- Start of processing for Access_Subprogram_Declaration
1031 Check_Formal_Restriction ("access type is not allowed", T_Def);
1033 -- Associate the Itype node with the inner full-type declaration or
1034 -- subprogram spec or entry body. This is required to handle nested
1035 -- anonymous declarations. For example:
1038 -- (X : access procedure
1039 -- (Y : access procedure
1042 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1043 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1044 N_Private_Type_Declaration,
1045 N_Private_Extension_Declaration,
1046 N_Procedure_Specification,
1047 N_Function_Specification,
1051 Nkind_In (D_Ityp, N_Object_Declaration,
1052 N_Object_Renaming_Declaration,
1053 N_Formal_Object_Declaration,
1054 N_Formal_Type_Declaration,
1055 N_Task_Type_Declaration,
1056 N_Protected_Type_Declaration))
1058 D_Ityp := Parent (D_Ityp);
1059 pragma Assert (D_Ityp /= Empty);
1062 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1064 if Nkind_In (D_Ityp, N_Procedure_Specification,
1065 N_Function_Specification)
1067 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1069 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1070 N_Object_Declaration,
1071 N_Object_Renaming_Declaration,
1072 N_Formal_Type_Declaration)
1074 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1077 if Nkind (T_Def) = N_Access_Function_Definition then
1078 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1080 Acc : constant Node_Id := Result_Definition (T_Def);
1083 if Present (Access_To_Subprogram_Definition (Acc))
1085 Protected_Present (Access_To_Subprogram_Definition (Acc))
1089 Replace_Anonymous_Access_To_Protected_Subprogram
1095 Access_Definition (T_Def, Result_Definition (T_Def)));
1100 Analyze (Result_Definition (T_Def));
1103 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1106 -- If a null exclusion is imposed on the result type, then
1107 -- create a null-excluding itype (an access subtype) and use
1108 -- it as the function's Etype.
1110 if Is_Access_Type (Typ)
1111 and then Null_Exclusion_In_Return_Present (T_Def)
1113 Set_Etype (Desig_Type,
1114 Create_Null_Excluding_Itype
1116 Related_Nod => T_Def,
1117 Scope_Id => Current_Scope));
1120 if From_With_Type (Typ) then
1122 -- AI05-151: Incomplete types are allowed in all basic
1123 -- declarations, including access to subprograms.
1125 if Ada_Version >= Ada_2012 then
1130 ("illegal use of incomplete type&",
1131 Result_Definition (T_Def), Typ);
1134 elsif Ekind (Current_Scope) = E_Package
1135 and then In_Private_Part (Current_Scope)
1137 if Ekind (Typ) = E_Incomplete_Type then
1138 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1140 elsif Is_Class_Wide_Type (Typ)
1141 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1144 (Desig_Type, Private_Dependents (Etype (Typ)));
1148 Set_Etype (Desig_Type, Typ);
1153 if not (Is_Type (Etype (Desig_Type))) then
1155 ("expect type in function specification",
1156 Result_Definition (T_Def));
1160 Set_Etype (Desig_Type, Standard_Void_Type);
1163 if Present (Formals) then
1164 Push_Scope (Desig_Type);
1166 -- A bit of a kludge here. These kludges will be removed when Itypes
1167 -- have proper parent pointers to their declarations???
1169 -- Kludge 1) Link defining_identifier of formals. Required by
1170 -- First_Formal to provide its functionality.
1176 F := First (Formals);
1178 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1179 -- when it is part of an unconstrained type and subtype expansion
1180 -- is disabled. To avoid back-end problems with shared profiles,
1181 -- use previous subprogram type as the designated type.
1184 and then Present (Scope (Defining_Identifier (F)))
1186 Set_Etype (T_Name, T_Name);
1187 Init_Size_Align (T_Name);
1188 Set_Directly_Designated_Type (T_Name,
1189 Scope (Defining_Identifier (F)));
1193 while Present (F) loop
1194 if No (Parent (Defining_Identifier (F))) then
1195 Set_Parent (Defining_Identifier (F), F);
1202 Process_Formals (Formals, Parent (T_Def));
1204 -- Kludge 2) End_Scope requires that the parent pointer be set to
1205 -- something reasonable, but Itypes don't have parent pointers. So
1206 -- we set it and then unset it ???
1208 Set_Parent (Desig_Type, T_Name);
1210 Set_Parent (Desig_Type, Empty);
1213 -- Check for premature usage of the type being defined
1215 Check_For_Premature_Usage (T_Def);
1217 -- The return type and/or any parameter type may be incomplete. Mark
1218 -- the subprogram_type as depending on the incomplete type, so that
1219 -- it can be updated when the full type declaration is seen. This
1220 -- only applies to incomplete types declared in some enclosing scope,
1221 -- not to limited views from other packages.
1223 if Present (Formals) then
1224 Formal := First_Formal (Desig_Type);
1225 while Present (Formal) loop
1226 if Ekind (Formal) /= E_In_Parameter
1227 and then Nkind (T_Def) = N_Access_Function_Definition
1229 Error_Msg_N ("functions can only have IN parameters", Formal);
1232 if Ekind (Etype (Formal)) = E_Incomplete_Type
1233 and then In_Open_Scopes (Scope (Etype (Formal)))
1235 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1236 Set_Has_Delayed_Freeze (Desig_Type);
1239 Next_Formal (Formal);
1243 -- If the return type is incomplete, this is legal as long as the
1244 -- type is declared in the current scope and will be completed in
1245 -- it (rather than being part of limited view).
1247 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1248 and then not Has_Delayed_Freeze (Desig_Type)
1249 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1251 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1252 Set_Has_Delayed_Freeze (Desig_Type);
1255 Check_Delayed_Subprogram (Desig_Type);
1257 if Protected_Present (T_Def) then
1258 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1259 Set_Convention (Desig_Type, Convention_Protected);
1261 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1264 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1266 Set_Etype (T_Name, T_Name);
1267 Init_Size_Align (T_Name);
1268 Set_Directly_Designated_Type (T_Name, Desig_Type);
1270 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1272 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1274 Check_Restriction (No_Access_Subprograms, T_Def);
1275 end Access_Subprogram_Declaration;
1277 ----------------------------
1278 -- Access_Type_Declaration --
1279 ----------------------------
1281 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1282 S : constant Node_Id := Subtype_Indication (Def);
1283 P : constant Node_Id := Parent (Def);
1285 Check_Formal_Restriction ("access type is not allowed", Def);
1287 -- Check for permissible use of incomplete type
1289 if Nkind (S) /= N_Subtype_Indication then
1292 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1293 Set_Directly_Designated_Type (T, Entity (S));
1295 Set_Directly_Designated_Type (T,
1296 Process_Subtype (S, P, T, 'P'));
1300 Set_Directly_Designated_Type (T,
1301 Process_Subtype (S, P, T, 'P'));
1304 if All_Present (Def) or Constant_Present (Def) then
1305 Set_Ekind (T, E_General_Access_Type);
1307 Set_Ekind (T, E_Access_Type);
1310 if Base_Type (Designated_Type (T)) = T then
1311 Error_Msg_N ("access type cannot designate itself", S);
1313 -- In Ada 2005, the type may have a limited view through some unit
1314 -- in its own context, allowing the following circularity that cannot
1315 -- be detected earlier
1317 elsif Is_Class_Wide_Type (Designated_Type (T))
1318 and then Etype (Designated_Type (T)) = T
1321 ("access type cannot designate its own classwide type", S);
1323 -- Clean up indication of tagged status to prevent cascaded errors
1325 Set_Is_Tagged_Type (T, False);
1330 -- If the type has appeared already in a with_type clause, it is
1331 -- frozen and the pointer size is already set. Else, initialize.
1333 if not From_With_Type (T) then
1334 Init_Size_Align (T);
1337 -- Note that Has_Task is always false, since the access type itself
1338 -- is not a task type. See Einfo for more description on this point.
1339 -- Exactly the same consideration applies to Has_Controlled_Component.
1341 Set_Has_Task (T, False);
1342 Set_Has_Controlled_Component (T, False);
1344 -- Initialize Associated_Final_Chain explicitly to Empty, to avoid
1345 -- problems where an incomplete view of this entity has been previously
1346 -- established by a limited with and an overlaid version of this field
1347 -- (Stored_Constraint) was initialized for the incomplete view.
1349 Set_Associated_Final_Chain (T, Empty);
1351 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1354 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1355 Set_Is_Access_Constant (T, Constant_Present (Def));
1356 end Access_Type_Declaration;
1358 ----------------------------------
1359 -- Add_Interface_Tag_Components --
1360 ----------------------------------
1362 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1363 Loc : constant Source_Ptr := Sloc (N);
1367 procedure Add_Tag (Iface : Entity_Id);
1368 -- Add tag for one of the progenitor interfaces
1374 procedure Add_Tag (Iface : Entity_Id) is
1381 pragma Assert (Is_Tagged_Type (Iface)
1382 and then Is_Interface (Iface));
1384 -- This is a reasonable place to propagate predicates
1386 if Has_Predicates (Iface) then
1387 Set_Has_Predicates (Typ);
1391 Make_Component_Definition (Loc,
1392 Aliased_Present => True,
1393 Subtype_Indication =>
1394 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1396 Tag := Make_Temporary (Loc, 'V');
1399 Make_Component_Declaration (Loc,
1400 Defining_Identifier => Tag,
1401 Component_Definition => Def);
1403 Analyze_Component_Declaration (Decl);
1405 Set_Analyzed (Decl);
1406 Set_Ekind (Tag, E_Component);
1408 Set_Is_Aliased (Tag);
1409 Set_Related_Type (Tag, Iface);
1410 Init_Component_Location (Tag);
1412 pragma Assert (Is_Frozen (Iface));
1414 Set_DT_Entry_Count (Tag,
1415 DT_Entry_Count (First_Entity (Iface)));
1417 if No (Last_Tag) then
1420 Insert_After (Last_Tag, Decl);
1425 -- If the ancestor has discriminants we need to give special support
1426 -- to store the offset_to_top value of the secondary dispatch tables.
1427 -- For this purpose we add a supplementary component just after the
1428 -- field that contains the tag associated with each secondary DT.
1430 if Typ /= Etype (Typ)
1431 and then Has_Discriminants (Etype (Typ))
1434 Make_Component_Definition (Loc,
1435 Subtype_Indication =>
1436 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1438 Offset := Make_Temporary (Loc, 'V');
1441 Make_Component_Declaration (Loc,
1442 Defining_Identifier => Offset,
1443 Component_Definition => Def);
1445 Analyze_Component_Declaration (Decl);
1447 Set_Analyzed (Decl);
1448 Set_Ekind (Offset, E_Component);
1449 Set_Is_Aliased (Offset);
1450 Set_Related_Type (Offset, Iface);
1451 Init_Component_Location (Offset);
1452 Insert_After (Last_Tag, Decl);
1463 -- Start of processing for Add_Interface_Tag_Components
1466 if not RTE_Available (RE_Interface_Tag) then
1468 ("(Ada 2005) interface types not supported by this run-time!",
1473 if Ekind (Typ) /= E_Record_Type
1474 or else (Is_Concurrent_Record_Type (Typ)
1475 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1476 or else (not Is_Concurrent_Record_Type (Typ)
1477 and then No (Interfaces (Typ))
1478 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1483 -- Find the current last tag
1485 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1486 Ext := Record_Extension_Part (Type_Definition (N));
1488 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1489 Ext := Type_Definition (N);
1494 if not (Present (Component_List (Ext))) then
1495 Set_Null_Present (Ext, False);
1497 Set_Component_List (Ext,
1498 Make_Component_List (Loc,
1499 Component_Items => L,
1500 Null_Present => False));
1502 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1503 L := Component_Items
1505 (Record_Extension_Part
1506 (Type_Definition (N))));
1508 L := Component_Items
1510 (Type_Definition (N)));
1513 -- Find the last tag component
1516 while Present (Comp) loop
1517 if Nkind (Comp) = N_Component_Declaration
1518 and then Is_Tag (Defining_Identifier (Comp))
1527 -- At this point L references the list of components and Last_Tag
1528 -- references the current last tag (if any). Now we add the tag
1529 -- corresponding with all the interfaces that are not implemented
1532 if Present (Interfaces (Typ)) then
1533 Elmt := First_Elmt (Interfaces (Typ));
1534 while Present (Elmt) loop
1535 Add_Tag (Node (Elmt));
1539 end Add_Interface_Tag_Components;
1541 -------------------------------------
1542 -- Add_Internal_Interface_Entities --
1543 -------------------------------------
1545 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1548 Iface_Elmt : Elmt_Id;
1549 Iface_Prim : Entity_Id;
1550 Ifaces_List : Elist_Id;
1551 New_Subp : Entity_Id := Empty;
1553 Restore_Scope : Boolean := False;
1556 pragma Assert (Ada_Version >= Ada_2005
1557 and then Is_Record_Type (Tagged_Type)
1558 and then Is_Tagged_Type (Tagged_Type)
1559 and then Has_Interfaces (Tagged_Type)
1560 and then not Is_Interface (Tagged_Type));
1562 -- Ensure that the internal entities are added to the scope of the type
1564 if Scope (Tagged_Type) /= Current_Scope then
1565 Push_Scope (Scope (Tagged_Type));
1566 Restore_Scope := True;
1569 Collect_Interfaces (Tagged_Type, Ifaces_List);
1571 Iface_Elmt := First_Elmt (Ifaces_List);
1572 while Present (Iface_Elmt) loop
1573 Iface := Node (Iface_Elmt);
1575 -- Originally we excluded here from this processing interfaces that
1576 -- are parents of Tagged_Type because their primitives are located
1577 -- in the primary dispatch table (and hence no auxiliary internal
1578 -- entities are required to handle secondary dispatch tables in such
1579 -- case). However, these auxiliary entities are also required to
1580 -- handle derivations of interfaces in formals of generics (see
1581 -- Derive_Subprograms).
1583 Elmt := First_Elmt (Primitive_Operations (Iface));
1584 while Present (Elmt) loop
1585 Iface_Prim := Node (Elmt);
1587 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1589 Find_Primitive_Covering_Interface
1590 (Tagged_Type => Tagged_Type,
1591 Iface_Prim => Iface_Prim);
1593 pragma Assert (Present (Prim));
1595 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1596 -- differs from the name of the interface primitive then it is
1597 -- a private primitive inherited from a parent type. In such
1598 -- case, given that Tagged_Type covers the interface, the
1599 -- inherited private primitive becomes visible. For such
1600 -- purpose we add a new entity that renames the inherited
1601 -- private primitive.
1603 if Chars (Prim) /= Chars (Iface_Prim) then
1604 pragma Assert (Has_Suffix (Prim, 'P'));
1606 (New_Subp => New_Subp,
1607 Parent_Subp => Iface_Prim,
1608 Derived_Type => Tagged_Type,
1609 Parent_Type => Iface);
1610 Set_Alias (New_Subp, Prim);
1611 Set_Is_Abstract_Subprogram
1612 (New_Subp, Is_Abstract_Subprogram (Prim));
1616 (New_Subp => New_Subp,
1617 Parent_Subp => Iface_Prim,
1618 Derived_Type => Tagged_Type,
1619 Parent_Type => Iface);
1621 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1622 -- associated with interface types. These entities are
1623 -- only registered in the list of primitives of its
1624 -- corresponding tagged type because they are only used
1625 -- to fill the contents of the secondary dispatch tables.
1626 -- Therefore they are removed from the homonym chains.
1628 Set_Is_Hidden (New_Subp);
1629 Set_Is_Internal (New_Subp);
1630 Set_Alias (New_Subp, Prim);
1631 Set_Is_Abstract_Subprogram
1632 (New_Subp, Is_Abstract_Subprogram (Prim));
1633 Set_Interface_Alias (New_Subp, Iface_Prim);
1635 -- Internal entities associated with interface types are
1636 -- only registered in the list of primitives of the tagged
1637 -- type. They are only used to fill the contents of the
1638 -- secondary dispatch tables. Therefore they are not needed
1639 -- in the homonym chains.
1641 Remove_Homonym (New_Subp);
1643 -- Hidden entities associated with interfaces must have set
1644 -- the Has_Delay_Freeze attribute to ensure that, in case of
1645 -- locally defined tagged types (or compiling with static
1646 -- dispatch tables generation disabled) the corresponding
1647 -- entry of the secondary dispatch table is filled when
1648 -- such an entity is frozen.
1650 Set_Has_Delayed_Freeze (New_Subp);
1656 Next_Elmt (Iface_Elmt);
1659 if Restore_Scope then
1662 end Add_Internal_Interface_Entities;
1664 -----------------------------------
1665 -- Analyze_Component_Declaration --
1666 -----------------------------------
1668 procedure Analyze_Component_Declaration (N : Node_Id) is
1669 Id : constant Entity_Id := Defining_Identifier (N);
1670 E : constant Node_Id := Expression (N);
1671 Typ : constant Node_Id :=
1672 Subtype_Indication (Component_Definition (N));
1676 function Contains_POC (Constr : Node_Id) return Boolean;
1677 -- Determines whether a constraint uses the discriminant of a record
1678 -- type thus becoming a per-object constraint (POC).
1680 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1681 -- Typ is the type of the current component, check whether this type is
1682 -- a limited type. Used to validate declaration against that of
1683 -- enclosing record.
1689 function Contains_POC (Constr : Node_Id) return Boolean is
1691 -- Prevent cascaded errors
1693 if Error_Posted (Constr) then
1697 case Nkind (Constr) is
1698 when N_Attribute_Reference =>
1700 Attribute_Name (Constr) = Name_Access
1701 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1703 when N_Discriminant_Association =>
1704 return Denotes_Discriminant (Expression (Constr));
1706 when N_Identifier =>
1707 return Denotes_Discriminant (Constr);
1709 when N_Index_Or_Discriminant_Constraint =>
1714 IDC := First (Constraints (Constr));
1715 while Present (IDC) loop
1717 -- One per-object constraint is sufficient
1719 if Contains_POC (IDC) then
1730 return Denotes_Discriminant (Low_Bound (Constr))
1732 Denotes_Discriminant (High_Bound (Constr));
1734 when N_Range_Constraint =>
1735 return Denotes_Discriminant (Range_Expression (Constr));
1743 ----------------------
1744 -- Is_Known_Limited --
1745 ----------------------
1747 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1748 P : constant Entity_Id := Etype (Typ);
1749 R : constant Entity_Id := Root_Type (Typ);
1752 if Is_Limited_Record (Typ) then
1755 -- If the root type is limited (and not a limited interface)
1756 -- so is the current type
1758 elsif Is_Limited_Record (R)
1760 (not Is_Interface (R)
1761 or else not Is_Limited_Interface (R))
1765 -- Else the type may have a limited interface progenitor, but a
1766 -- limited record parent.
1769 and then Is_Limited_Record (P)
1776 end Is_Known_Limited;
1778 -- Start of processing for Analyze_Component_Declaration
1781 Generate_Definition (Id);
1784 if Present (Typ) then
1785 if Nkind (Typ) /= N_Identifier then
1786 Check_Formal_Restriction ("subtype mark required", Typ);
1789 T := Find_Type_Of_Object
1790 (Subtype_Indication (Component_Definition (N)), N);
1792 -- Ada 2005 (AI-230): Access Definition case
1795 pragma Assert (Present
1796 (Access_Definition (Component_Definition (N))));
1798 T := Access_Definition
1800 N => Access_Definition (Component_Definition (N)));
1801 Set_Is_Local_Anonymous_Access (T);
1803 -- Ada 2005 (AI-254)
1805 if Present (Access_To_Subprogram_Definition
1806 (Access_Definition (Component_Definition (N))))
1807 and then Protected_Present (Access_To_Subprogram_Definition
1809 (Component_Definition (N))))
1811 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1815 -- If the subtype is a constrained subtype of the enclosing record,
1816 -- (which must have a partial view) the back-end does not properly
1817 -- handle the recursion. Rewrite the component declaration with an
1818 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1819 -- the tree directly because side effects have already been removed from
1820 -- discriminant constraints.
1822 if Ekind (T) = E_Access_Subtype
1823 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1824 and then Comes_From_Source (T)
1825 and then Nkind (Parent (T)) = N_Subtype_Declaration
1826 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1829 (Subtype_Indication (Component_Definition (N)),
1830 New_Copy_Tree (Subtype_Indication (Parent (T))));
1831 T := Find_Type_Of_Object
1832 (Subtype_Indication (Component_Definition (N)), N);
1835 -- If the component declaration includes a default expression, then we
1836 -- check that the component is not of a limited type (RM 3.7(5)),
1837 -- and do the special preanalysis of the expression (see section on
1838 -- "Handling of Default and Per-Object Expressions" in the spec of
1842 Check_Formal_Restriction ("default expression is not allowed", E);
1843 Preanalyze_Spec_Expression (E, T);
1844 Check_Initialization (T, E);
1846 if Ada_Version >= Ada_2005
1847 and then Ekind (T) = E_Anonymous_Access_Type
1848 and then Etype (E) /= Any_Type
1850 -- Check RM 3.9.2(9): "if the expected type for an expression is
1851 -- an anonymous access-to-specific tagged type, then the object
1852 -- designated by the expression shall not be dynamically tagged
1853 -- unless it is a controlling operand in a call on a dispatching
1856 if Is_Tagged_Type (Directly_Designated_Type (T))
1858 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1860 Ekind (Directly_Designated_Type (Etype (E))) =
1864 ("access to specific tagged type required (RM 3.9.2(9))", E);
1867 -- (Ada 2005: AI-230): Accessibility check for anonymous
1870 if Type_Access_Level (Etype (E)) > Type_Access_Level (T) then
1872 ("expression has deeper access level than component " &
1873 "(RM 3.10.2 (12.2))", E);
1876 -- The initialization expression is a reference to an access
1877 -- discriminant. The type of the discriminant is always deeper
1878 -- than any access type.
1880 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1881 and then Is_Entity_Name (E)
1882 and then Ekind (Entity (E)) = E_In_Parameter
1883 and then Present (Discriminal_Link (Entity (E)))
1886 ("discriminant has deeper accessibility level than target",
1892 -- The parent type may be a private view with unknown discriminants,
1893 -- and thus unconstrained. Regular components must be constrained.
1895 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
1896 if Is_Class_Wide_Type (T) then
1898 ("class-wide subtype with unknown discriminants" &
1899 " in component declaration",
1900 Subtype_Indication (Component_Definition (N)));
1903 ("unconstrained subtype in component declaration",
1904 Subtype_Indication (Component_Definition (N)));
1907 -- Components cannot be abstract, except for the special case of
1908 -- the _Parent field (case of extending an abstract tagged type)
1910 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
1911 Error_Msg_N ("type of a component cannot be abstract", N);
1915 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
1917 -- The component declaration may have a per-object constraint, set
1918 -- the appropriate flag in the defining identifier of the subtype.
1920 if Present (Subtype_Indication (Component_Definition (N))) then
1922 Sindic : constant Node_Id :=
1923 Subtype_Indication (Component_Definition (N));
1925 if Nkind (Sindic) = N_Subtype_Indication
1926 and then Present (Constraint (Sindic))
1927 and then Contains_POC (Constraint (Sindic))
1929 Set_Has_Per_Object_Constraint (Id);
1934 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
1935 -- out some static checks.
1937 if Ada_Version >= Ada_2005
1938 and then Can_Never_Be_Null (T)
1940 Null_Exclusion_Static_Checks (N);
1943 -- If this component is private (or depends on a private type), flag the
1944 -- record type to indicate that some operations are not available.
1946 P := Private_Component (T);
1950 -- Check for circular definitions
1952 if P = Any_Type then
1953 Set_Etype (Id, Any_Type);
1955 -- There is a gap in the visibility of operations only if the
1956 -- component type is not defined in the scope of the record type.
1958 elsif Scope (P) = Scope (Current_Scope) then
1961 elsif Is_Limited_Type (P) then
1962 Set_Is_Limited_Composite (Current_Scope);
1965 Set_Is_Private_Composite (Current_Scope);
1970 and then Is_Limited_Type (T)
1971 and then Chars (Id) /= Name_uParent
1972 and then Is_Tagged_Type (Current_Scope)
1974 if Is_Derived_Type (Current_Scope)
1975 and then not Is_Known_Limited (Current_Scope)
1978 ("extension of nonlimited type cannot have limited components",
1981 if Is_Interface (Root_Type (Current_Scope)) then
1983 ("\limitedness is not inherited from limited interface", N);
1984 Error_Msg_N ("\add LIMITED to type indication", N);
1987 Explain_Limited_Type (T, N);
1988 Set_Etype (Id, Any_Type);
1989 Set_Is_Limited_Composite (Current_Scope, False);
1991 elsif not Is_Derived_Type (Current_Scope)
1992 and then not Is_Limited_Record (Current_Scope)
1993 and then not Is_Concurrent_Type (Current_Scope)
1996 ("nonlimited tagged type cannot have limited components", N);
1997 Explain_Limited_Type (T, N);
1998 Set_Etype (Id, Any_Type);
1999 Set_Is_Limited_Composite (Current_Scope, False);
2003 Set_Original_Record_Component (Id, Id);
2005 if Has_Aspects (N) then
2006 Analyze_Aspect_Specifications (N, Id);
2008 end Analyze_Component_Declaration;
2010 --------------------------
2011 -- Analyze_Declarations --
2012 --------------------------
2014 procedure Analyze_Declarations (L : List_Id) is
2016 Freeze_From : Entity_Id := Empty;
2017 Next_Node : Node_Id;
2020 -- Adjust D not to include implicit label declarations, since these
2021 -- have strange Sloc values that result in elaboration check problems.
2022 -- (They have the sloc of the label as found in the source, and that
2023 -- is ahead of the current declarative part).
2029 procedure Adjust_D is
2031 while Present (Prev (D))
2032 and then Nkind (D) = N_Implicit_Label_Declaration
2038 -- Start of processing for Analyze_Declarations
2042 while Present (D) loop
2044 -- Package specification cannot contain a package declaration in
2047 if Nkind (D) = N_Package_Declaration
2048 and then Nkind (Parent (L)) = N_Package_Specification
2050 Check_Formal_Restriction ("package specification cannot contain "
2051 & "a package declaration", D);
2054 -- Complete analysis of declaration
2057 Next_Node := Next (D);
2059 if No (Freeze_From) then
2060 Freeze_From := First_Entity (Current_Scope);
2063 -- At the end of a declarative part, freeze remaining entities
2064 -- declared in it. The end of the visible declarations of package
2065 -- specification is not the end of a declarative part if private
2066 -- declarations are present. The end of a package declaration is a
2067 -- freezing point only if it a library package. A task definition or
2068 -- protected type definition is not a freeze point either. Finally,
2069 -- we do not freeze entities in generic scopes, because there is no
2070 -- code generated for them and freeze nodes will be generated for
2073 -- The end of a package instantiation is not a freeze point, but
2074 -- for now we make it one, because the generic body is inserted
2075 -- (currently) immediately after. Generic instantiations will not
2076 -- be a freeze point once delayed freezing of bodies is implemented.
2077 -- (This is needed in any case for early instantiations ???).
2079 if No (Next_Node) then
2080 if Nkind_In (Parent (L), N_Component_List,
2082 N_Protected_Definition)
2086 elsif Nkind (Parent (L)) /= N_Package_Specification then
2087 if Nkind (Parent (L)) = N_Package_Body then
2088 Freeze_From := First_Entity (Current_Scope);
2092 Freeze_All (Freeze_From, D);
2093 Freeze_From := Last_Entity (Current_Scope);
2095 elsif Scope (Current_Scope) /= Standard_Standard
2096 and then not Is_Child_Unit (Current_Scope)
2097 and then No (Generic_Parent (Parent (L)))
2101 elsif L /= Visible_Declarations (Parent (L))
2102 or else No (Private_Declarations (Parent (L)))
2103 or else Is_Empty_List (Private_Declarations (Parent (L)))
2106 Freeze_All (Freeze_From, D);
2107 Freeze_From := Last_Entity (Current_Scope);
2110 -- If next node is a body then freeze all types before the body.
2111 -- An exception occurs for some expander-generated bodies. If these
2112 -- are generated at places where in general language rules would not
2113 -- allow a freeze point, then we assume that the expander has
2114 -- explicitly checked that all required types are properly frozen,
2115 -- and we do not cause general freezing here. This special circuit
2116 -- is used when the encountered body is marked as having already
2119 -- In all other cases (bodies that come from source, and expander
2120 -- generated bodies that have not been analyzed yet), freeze all
2121 -- types now. Note that in the latter case, the expander must take
2122 -- care to attach the bodies at a proper place in the tree so as to
2123 -- not cause unwanted freezing at that point.
2125 elsif not Analyzed (Next_Node)
2126 and then (Nkind_In (Next_Node, N_Subprogram_Body,
2132 Nkind (Next_Node) in N_Body_Stub)
2135 Freeze_All (Freeze_From, D);
2136 Freeze_From := Last_Entity (Current_Scope);
2142 -- One more thing to do, we need to scan the declarations to check
2143 -- for any precondition/postcondition pragmas (Pre/Post aspects have
2144 -- by this stage been converted into corresponding pragmas). It is
2145 -- at this point that we analyze the expressions in such pragmas,
2146 -- to implement the delayed visibility requirement.
2156 while Present (Decl) loop
2157 if Nkind (Original_Node (Decl)) = N_Subprogram_Declaration then
2158 Spec := Specification (Original_Node (Decl));
2159 Sent := Defining_Unit_Name (Spec);
2160 Prag := Spec_PPC_List (Sent);
2161 while Present (Prag) loop
2162 Analyze_PPC_In_Decl_Part (Prag, Sent);
2163 Prag := Next_Pragma (Prag);
2170 end Analyze_Declarations;
2172 -----------------------------------
2173 -- Analyze_Full_Type_Declaration --
2174 -----------------------------------
2176 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2177 Def : constant Node_Id := Type_Definition (N);
2178 Def_Id : constant Entity_Id := Defining_Identifier (N);
2182 Is_Remote : constant Boolean :=
2183 (Is_Remote_Types (Current_Scope)
2184 or else Is_Remote_Call_Interface (Current_Scope))
2185 and then not (In_Private_Part (Current_Scope)
2186 or else In_Package_Body (Current_Scope));
2188 procedure Check_Ops_From_Incomplete_Type;
2189 -- If there is a tagged incomplete partial view of the type, transfer
2190 -- its operations to the full view, and indicate that the type of the
2191 -- controlling parameter (s) is this full view.
2193 ------------------------------------
2194 -- Check_Ops_From_Incomplete_Type --
2195 ------------------------------------
2197 procedure Check_Ops_From_Incomplete_Type is
2204 and then Ekind (Prev) = E_Incomplete_Type
2205 and then Is_Tagged_Type (Prev)
2206 and then Is_Tagged_Type (T)
2208 Elmt := First_Elmt (Primitive_Operations (Prev));
2209 while Present (Elmt) loop
2211 Prepend_Elmt (Op, Primitive_Operations (T));
2213 Formal := First_Formal (Op);
2214 while Present (Formal) loop
2215 if Etype (Formal) = Prev then
2216 Set_Etype (Formal, T);
2219 Next_Formal (Formal);
2222 if Etype (Op) = Prev then
2229 end Check_Ops_From_Incomplete_Type;
2231 -- Start of processing for Analyze_Full_Type_Declaration
2234 Prev := Find_Type_Name (N);
2236 -- The full view, if present, now points to the current type
2238 -- Ada 2005 (AI-50217): If the type was previously decorated when
2239 -- imported through a LIMITED WITH clause, it appears as incomplete
2240 -- but has no full view.
2242 if Ekind (Prev) = E_Incomplete_Type
2243 and then Present (Full_View (Prev))
2245 T := Full_View (Prev);
2250 Set_Is_Pure (T, Is_Pure (Current_Scope));
2252 -- We set the flag Is_First_Subtype here. It is needed to set the
2253 -- corresponding flag for the Implicit class-wide-type created
2254 -- during tagged types processing.
2256 Set_Is_First_Subtype (T, True);
2258 -- Only composite types other than array types are allowed to have
2263 -- For derived types, the rule will be checked once we've figured
2264 -- out the parent type.
2266 when N_Derived_Type_Definition =>
2269 -- For record types, discriminants are allowed, unless we are in
2272 when N_Record_Definition =>
2273 if Present (Discriminant_Specifications (N)) then
2274 Check_Formal_Restriction
2275 ("discriminant type is not allowed",
2277 (First (Discriminant_Specifications (N))));
2281 if Present (Discriminant_Specifications (N)) then
2283 ("elementary or array type cannot have discriminants",
2285 (First (Discriminant_Specifications (N))));
2289 -- Elaborate the type definition according to kind, and generate
2290 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2291 -- already done (this happens during the reanalysis that follows a call
2292 -- to the high level optimizer).
2294 if not Analyzed (T) then
2299 when N_Access_To_Subprogram_Definition =>
2300 Access_Subprogram_Declaration (T, Def);
2302 -- If this is a remote access to subprogram, we must create the
2303 -- equivalent fat pointer type, and related subprograms.
2306 Process_Remote_AST_Declaration (N);
2309 -- Validate categorization rule against access type declaration
2310 -- usually a violation in Pure unit, Shared_Passive unit.
2312 Validate_Access_Type_Declaration (T, N);
2314 when N_Access_To_Object_Definition =>
2315 Access_Type_Declaration (T, Def);
2317 -- Validate categorization rule against access type declaration
2318 -- usually a violation in Pure unit, Shared_Passive unit.
2320 Validate_Access_Type_Declaration (T, N);
2322 -- If we are in a Remote_Call_Interface package and define a
2323 -- RACW, then calling stubs and specific stream attributes
2327 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2329 Add_RACW_Features (Def_Id);
2332 -- Set no strict aliasing flag if config pragma seen
2334 if Opt.No_Strict_Aliasing then
2335 Set_No_Strict_Aliasing (Base_Type (Def_Id));
2338 when N_Array_Type_Definition =>
2339 Array_Type_Declaration (T, Def);
2341 when N_Derived_Type_Definition =>
2342 Derived_Type_Declaration (T, N, T /= Def_Id);
2344 when N_Enumeration_Type_Definition =>
2345 Enumeration_Type_Declaration (T, Def);
2347 when N_Floating_Point_Definition =>
2348 Floating_Point_Type_Declaration (T, Def);
2350 when N_Decimal_Fixed_Point_Definition =>
2351 Decimal_Fixed_Point_Type_Declaration (T, Def);
2353 when N_Ordinary_Fixed_Point_Definition =>
2354 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2356 when N_Signed_Integer_Type_Definition =>
2357 Signed_Integer_Type_Declaration (T, Def);
2359 when N_Modular_Type_Definition =>
2360 Modular_Type_Declaration (T, Def);
2362 when N_Record_Definition =>
2363 Record_Type_Declaration (T, N, Prev);
2365 -- If declaration has a parse error, nothing to elaborate.
2371 raise Program_Error;
2376 if Etype (T) = Any_Type then
2380 -- Controlled type is not allowed in SPARK and ALFA
2382 if Is_Visibly_Controlled (T) then
2383 Check_Formal_Restriction ("controlled type is not allowed", N);
2386 -- Some common processing for all types
2388 Set_Depends_On_Private (T, Has_Private_Component (T));
2389 Check_Ops_From_Incomplete_Type;
2391 -- Both the declared entity, and its anonymous base type if one
2392 -- was created, need freeze nodes allocated.
2395 B : constant Entity_Id := Base_Type (T);
2398 -- In the case where the base type differs from the first subtype, we
2399 -- pre-allocate a freeze node, and set the proper link to the first
2400 -- subtype. Freeze_Entity will use this preallocated freeze node when
2401 -- it freezes the entity.
2403 -- This does not apply if the base type is a generic type, whose
2404 -- declaration is independent of the current derived definition.
2406 if B /= T and then not Is_Generic_Type (B) then
2407 Ensure_Freeze_Node (B);
2408 Set_First_Subtype_Link (Freeze_Node (B), T);
2411 -- A type that is imported through a limited_with clause cannot
2412 -- generate any code, and thus need not be frozen. However, an access
2413 -- type with an imported designated type needs a finalization list,
2414 -- which may be referenced in some other package that has non-limited
2415 -- visibility on the designated type. Thus we must create the
2416 -- finalization list at the point the access type is frozen, to
2417 -- prevent unsatisfied references at link time.
2419 if not From_With_Type (T) or else Is_Access_Type (T) then
2420 Set_Has_Delayed_Freeze (T);
2424 -- Case where T is the full declaration of some private type which has
2425 -- been swapped in Defining_Identifier (N).
2427 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2428 Process_Full_View (N, T, Def_Id);
2430 -- Record the reference. The form of this is a little strange, since
2431 -- the full declaration has been swapped in. So the first parameter
2432 -- here represents the entity to which a reference is made which is
2433 -- the "real" entity, i.e. the one swapped in, and the second
2434 -- parameter provides the reference location.
2436 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2437 -- since we don't want a complaint about the full type being an
2438 -- unwanted reference to the private type
2441 B : constant Boolean := Has_Pragma_Unreferenced (T);
2443 Set_Has_Pragma_Unreferenced (T, False);
2444 Generate_Reference (T, T, 'c');
2445 Set_Has_Pragma_Unreferenced (T, B);
2448 Set_Completion_Referenced (Def_Id);
2450 -- For completion of incomplete type, process incomplete dependents
2451 -- and always mark the full type as referenced (it is the incomplete
2452 -- type that we get for any real reference).
2454 elsif Ekind (Prev) = E_Incomplete_Type then
2455 Process_Incomplete_Dependents (N, T, Prev);
2456 Generate_Reference (Prev, Def_Id, 'c');
2457 Set_Completion_Referenced (Def_Id);
2459 -- If not private type or incomplete type completion, this is a real
2460 -- definition of a new entity, so record it.
2463 Generate_Definition (Def_Id);
2466 if Chars (Scope (Def_Id)) = Name_System
2467 and then Chars (Def_Id) = Name_Address
2468 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2470 Set_Is_Descendent_Of_Address (Def_Id);
2471 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2472 Set_Is_Descendent_Of_Address (Prev);
2475 Set_Optimize_Alignment_Flags (Def_Id);
2476 Check_Eliminated (Def_Id);
2478 if Has_Aspects (N) then
2479 Analyze_Aspect_Specifications (N, Def_Id);
2481 end Analyze_Full_Type_Declaration;
2483 ----------------------------------
2484 -- Analyze_Incomplete_Type_Decl --
2485 ----------------------------------
2487 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2488 F : constant Boolean := Is_Pure (Current_Scope);
2492 Check_Formal_Restriction ("incomplete type is not allowed", N);
2494 Generate_Definition (Defining_Identifier (N));
2496 -- Process an incomplete declaration. The identifier must not have been
2497 -- declared already in the scope. However, an incomplete declaration may
2498 -- appear in the private part of a package, for a private type that has
2499 -- already been declared.
2501 -- In this case, the discriminants (if any) must match
2503 T := Find_Type_Name (N);
2505 Set_Ekind (T, E_Incomplete_Type);
2506 Init_Size_Align (T);
2507 Set_Is_First_Subtype (T, True);
2510 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2511 -- incomplete types.
2513 if Tagged_Present (N) then
2514 Set_Is_Tagged_Type (T);
2515 Make_Class_Wide_Type (T);
2516 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2521 Set_Stored_Constraint (T, No_Elist);
2523 if Present (Discriminant_Specifications (N)) then
2524 Process_Discriminants (N);
2529 -- If the type has discriminants, non-trivial subtypes may be
2530 -- declared before the full view of the type. The full views of those
2531 -- subtypes will be built after the full view of the type.
2533 Set_Private_Dependents (T, New_Elmt_List);
2535 end Analyze_Incomplete_Type_Decl;
2537 -----------------------------------
2538 -- Analyze_Interface_Declaration --
2539 -----------------------------------
2541 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2542 CW : constant Entity_Id := Class_Wide_Type (T);
2545 Set_Is_Tagged_Type (T);
2547 Set_Is_Limited_Record (T, Limited_Present (Def)
2548 or else Task_Present (Def)
2549 or else Protected_Present (Def)
2550 or else Synchronized_Present (Def));
2552 -- Type is abstract if full declaration carries keyword, or if previous
2553 -- partial view did.
2555 Set_Is_Abstract_Type (T);
2556 Set_Is_Interface (T);
2558 -- Type is a limited interface if it includes the keyword limited, task,
2559 -- protected, or synchronized.
2561 Set_Is_Limited_Interface
2562 (T, Limited_Present (Def)
2563 or else Protected_Present (Def)
2564 or else Synchronized_Present (Def)
2565 or else Task_Present (Def));
2567 Set_Interfaces (T, New_Elmt_List);
2568 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2570 -- Complete the decoration of the class-wide entity if it was already
2571 -- built (i.e. during the creation of the limited view)
2573 if Present (CW) then
2574 Set_Is_Interface (CW);
2575 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2578 -- Check runtime support for synchronized interfaces
2580 if VM_Target = No_VM
2581 and then (Is_Task_Interface (T)
2582 or else Is_Protected_Interface (T)
2583 or else Is_Synchronized_Interface (T))
2584 and then not RTE_Available (RE_Select_Specific_Data)
2586 Error_Msg_CRT ("synchronized interfaces", T);
2588 end Analyze_Interface_Declaration;
2590 -----------------------------
2591 -- Analyze_Itype_Reference --
2592 -----------------------------
2594 -- Nothing to do. This node is placed in the tree only for the benefit of
2595 -- back end processing, and has no effect on the semantic processing.
2597 procedure Analyze_Itype_Reference (N : Node_Id) is
2599 pragma Assert (Is_Itype (Itype (N)));
2601 end Analyze_Itype_Reference;
2603 --------------------------------
2604 -- Analyze_Number_Declaration --
2605 --------------------------------
2607 procedure Analyze_Number_Declaration (N : Node_Id) is
2608 Id : constant Entity_Id := Defining_Identifier (N);
2609 E : constant Node_Id := Expression (N);
2611 Index : Interp_Index;
2615 Generate_Definition (Id);
2618 -- This is an optimization of a common case of an integer literal
2620 if Nkind (E) = N_Integer_Literal then
2621 Set_Is_Static_Expression (E, True);
2622 Set_Etype (E, Universal_Integer);
2624 Set_Etype (Id, Universal_Integer);
2625 Set_Ekind (Id, E_Named_Integer);
2626 Set_Is_Frozen (Id, True);
2630 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2632 -- Process expression, replacing error by integer zero, to avoid
2633 -- cascaded errors or aborts further along in the processing
2635 -- Replace Error by integer zero, which seems least likely to
2636 -- cause cascaded errors.
2639 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
2640 Set_Error_Posted (E);
2645 -- Verify that the expression is static and numeric. If
2646 -- the expression is overloaded, we apply the preference
2647 -- rule that favors root numeric types.
2649 if not Is_Overloaded (E) then
2655 Get_First_Interp (E, Index, It);
2656 while Present (It.Typ) loop
2657 if (Is_Integer_Type (It.Typ)
2658 or else Is_Real_Type (It.Typ))
2659 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
2661 if T = Any_Type then
2664 elsif It.Typ = Universal_Real
2665 or else It.Typ = Universal_Integer
2667 -- Choose universal interpretation over any other
2674 Get_Next_Interp (Index, It);
2678 if Is_Integer_Type (T) then
2680 Set_Etype (Id, Universal_Integer);
2681 Set_Ekind (Id, E_Named_Integer);
2683 elsif Is_Real_Type (T) then
2685 -- Because the real value is converted to universal_real, this is a
2686 -- legal context for a universal fixed expression.
2688 if T = Universal_Fixed then
2690 Loc : constant Source_Ptr := Sloc (N);
2691 Conv : constant Node_Id := Make_Type_Conversion (Loc,
2693 New_Occurrence_Of (Universal_Real, Loc),
2694 Expression => Relocate_Node (E));
2701 elsif T = Any_Fixed then
2702 Error_Msg_N ("illegal context for mixed mode operation", E);
2704 -- Expression is of the form : universal_fixed * integer. Try to
2705 -- resolve as universal_real.
2707 T := Universal_Real;
2712 Set_Etype (Id, Universal_Real);
2713 Set_Ekind (Id, E_Named_Real);
2716 Wrong_Type (E, Any_Numeric);
2720 Set_Ekind (Id, E_Constant);
2721 Set_Never_Set_In_Source (Id, True);
2722 Set_Is_True_Constant (Id, True);
2726 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
2727 Set_Etype (E, Etype (Id));
2730 if not Is_OK_Static_Expression (E) then
2731 Flag_Non_Static_Expr
2732 ("non-static expression used in number declaration!", E);
2733 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
2734 Set_Etype (E, Any_Type);
2736 end Analyze_Number_Declaration;
2738 --------------------------------
2739 -- Analyze_Object_Declaration --
2740 --------------------------------
2742 procedure Analyze_Object_Declaration (N : Node_Id) is
2743 Loc : constant Source_Ptr := Sloc (N);
2744 Id : constant Entity_Id := Defining_Identifier (N);
2748 E : Node_Id := Expression (N);
2749 -- E is set to Expression (N) throughout this routine. When
2750 -- Expression (N) is modified, E is changed accordingly.
2752 Prev_Entity : Entity_Id := Empty;
2754 function Count_Tasks (T : Entity_Id) return Uint;
2755 -- This function is called when a non-generic library level object of a
2756 -- task type is declared. Its function is to count the static number of
2757 -- tasks declared within the type (it is only called if Has_Tasks is set
2758 -- for T). As a side effect, if an array of tasks with non-static bounds
2759 -- or a variant record type is encountered, Check_Restrictions is called
2760 -- indicating the count is unknown.
2766 function Count_Tasks (T : Entity_Id) return Uint is
2772 if Is_Task_Type (T) then
2775 elsif Is_Record_Type (T) then
2776 if Has_Discriminants (T) then
2777 Check_Restriction (Max_Tasks, N);
2782 C := First_Component (T);
2783 while Present (C) loop
2784 V := V + Count_Tasks (Etype (C));
2791 elsif Is_Array_Type (T) then
2792 X := First_Index (T);
2793 V := Count_Tasks (Component_Type (T));
2794 while Present (X) loop
2797 if not Is_Static_Subtype (C) then
2798 Check_Restriction (Max_Tasks, N);
2801 V := V * (UI_Max (Uint_0,
2802 Expr_Value (Type_High_Bound (C)) -
2803 Expr_Value (Type_Low_Bound (C)) + Uint_1));
2816 -- Start of processing for Analyze_Object_Declaration
2819 -- There are three kinds of implicit types generated by an
2820 -- object declaration:
2822 -- 1. Those generated by the original Object Definition
2824 -- 2. Those generated by the Expression
2826 -- 3. Those used to constrained the Object Definition with the
2827 -- expression constraints when it is unconstrained
2829 -- They must be generated in this order to avoid order of elaboration
2830 -- issues. Thus the first step (after entering the name) is to analyze
2831 -- the object definition.
2833 if Constant_Present (N) then
2834 Prev_Entity := Current_Entity_In_Scope (Id);
2836 if Present (Prev_Entity)
2838 -- If the homograph is an implicit subprogram, it is overridden
2839 -- by the current declaration.
2841 ((Is_Overloadable (Prev_Entity)
2842 and then Is_Inherited_Operation (Prev_Entity))
2844 -- The current object is a discriminal generated for an entry
2845 -- family index. Even though the index is a constant, in this
2846 -- particular context there is no true constant redeclaration.
2847 -- Enter_Name will handle the visibility.
2850 (Is_Discriminal (Id)
2851 and then Ekind (Discriminal_Link (Id)) =
2852 E_Entry_Index_Parameter)
2854 -- The current object is the renaming for a generic declared
2855 -- within the instance.
2858 (Ekind (Prev_Entity) = E_Package
2859 and then Nkind (Parent (Prev_Entity)) =
2860 N_Package_Renaming_Declaration
2861 and then not Comes_From_Source (Prev_Entity)
2862 and then Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
2864 Prev_Entity := Empty;
2868 if Present (Prev_Entity) then
2869 Constant_Redeclaration (Id, N, T);
2871 Generate_Reference (Prev_Entity, Id, 'c');
2872 Set_Completion_Referenced (Id);
2874 if Error_Posted (N) then
2876 -- Type mismatch or illegal redeclaration, Do not analyze
2877 -- expression to avoid cascaded errors.
2879 T := Find_Type_Of_Object (Object_Definition (N), N);
2881 Set_Ekind (Id, E_Variable);
2885 -- In the normal case, enter identifier at the start to catch premature
2886 -- usage in the initialization expression.
2889 Generate_Definition (Id);
2892 Mark_Coextensions (N, Object_Definition (N));
2894 T := Find_Type_Of_Object (Object_Definition (N), N);
2896 if Nkind (Object_Definition (N)) = N_Access_Definition
2898 (Access_To_Subprogram_Definition (Object_Definition (N)))
2899 and then Protected_Present
2900 (Access_To_Subprogram_Definition (Object_Definition (N)))
2902 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2905 if Error_Posted (Id) then
2907 Set_Ekind (Id, E_Variable);
2912 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2913 -- out some static checks
2915 if Ada_Version >= Ada_2005
2916 and then Can_Never_Be_Null (T)
2918 -- In case of aggregates we must also take care of the correct
2919 -- initialization of nested aggregates bug this is done at the
2920 -- point of the analysis of the aggregate (see sem_aggr.adb)
2922 if Present (Expression (N))
2923 and then Nkind (Expression (N)) = N_Aggregate
2929 Save_Typ : constant Entity_Id := Etype (Id);
2931 Set_Etype (Id, T); -- Temp. decoration for static checks
2932 Null_Exclusion_Static_Checks (N);
2933 Set_Etype (Id, Save_Typ);
2938 Set_Is_Pure (Id, Is_Pure (Current_Scope));
2940 -- If deferred constant, make sure context is appropriate. We detect
2941 -- a deferred constant as a constant declaration with no expression.
2942 -- A deferred constant can appear in a package body if its completion
2943 -- is by means of an interface pragma.
2945 if Constant_Present (N)
2948 -- A deferred constant may appear in the declarative part of the
2949 -- following constructs:
2953 -- extended return statements
2956 -- subprogram bodies
2959 -- When declared inside a package spec, a deferred constant must be
2960 -- completed by a full constant declaration or pragma Import. In all
2961 -- other cases, the only proper completion is pragma Import. Extended
2962 -- return statements are flagged as invalid contexts because they do
2963 -- not have a declarative part and so cannot accommodate the pragma.
2965 if Ekind (Current_Scope) = E_Return_Statement then
2967 ("invalid context for deferred constant declaration (RM 7.4)",
2970 ("\declaration requires an initialization expression",
2972 Set_Constant_Present (N, False);
2974 -- In Ada 83, deferred constant must be of private type
2976 elsif not Is_Private_Type (T) then
2977 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
2979 ("(Ada 83) deferred constant must be private type", N);
2983 -- If not a deferred constant, then object declaration freezes its type
2986 Check_Fully_Declared (T, N);
2987 Freeze_Before (N, T);
2990 -- If the object was created by a constrained array definition, then
2991 -- set the link in both the anonymous base type and anonymous subtype
2992 -- that are built to represent the array type to point to the object.
2994 if Nkind (Object_Definition (Declaration_Node (Id))) =
2995 N_Constrained_Array_Definition
2997 Set_Related_Array_Object (T, Id);
2998 Set_Related_Array_Object (Base_Type (T), Id);
3001 -- Special checks for protected objects not at library level
3003 if Is_Protected_Type (T)
3004 and then not Is_Library_Level_Entity (Id)
3006 Check_Restriction (No_Local_Protected_Objects, Id);
3008 -- Protected objects with interrupt handlers must be at library level
3010 -- Ada 2005: this test is not needed (and the corresponding clause
3011 -- in the RM is removed) because accessibility checks are sufficient
3012 -- to make handlers not at the library level illegal.
3014 if Has_Interrupt_Handler (T)
3015 and then Ada_Version < Ada_2005
3018 ("interrupt object can only be declared at library level", Id);
3022 -- The actual subtype of the object is the nominal subtype, unless
3023 -- the nominal one is unconstrained and obtained from the expression.
3027 -- These checks should be performed before the initialization expression
3028 -- is considered, so that the Object_Definition node is still the same
3029 -- as in source code.
3031 -- In SPARK or ALFA, the nominal subtype shall be given by a subtype
3032 -- mark and shall not be unconstrained. (The only exception to this
3033 -- is the admission of declarations of constants of type String.)
3035 if not Nkind_In (Object_Definition (N), N_Identifier,
3038 Check_Formal_Restriction
3039 ("subtype mark expected", Object_Definition (N));
3041 elsif Is_Array_Type (T)
3042 and then not Is_Constrained (T)
3043 and then T /= Standard_String
3045 Check_Formal_Restriction
3046 ("subtype mark of constrained type expected",
3047 Object_Definition (N));
3050 -- There are no aliased objects in SPARK or ALFA
3052 if Aliased_Present (N) then
3053 Check_Formal_Restriction ("aliased object is not allowed", N);
3056 -- Process initialization expression if present and not in error
3058 if Present (E) and then E /= Error then
3060 -- Generate an error in case of CPP class-wide object initialization.
3061 -- Required because otherwise the expansion of the class-wide
3062 -- assignment would try to use 'size to initialize the object
3063 -- (primitive that is not available in CPP tagged types).
3065 if Is_Class_Wide_Type (Act_T)
3067 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3069 (Present (Full_View (Root_Type (Etype (Act_T))))
3071 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3074 ("predefined assignment not available for 'C'P'P tagged types",
3078 Mark_Coextensions (N, E);
3081 -- In case of errors detected in the analysis of the expression,
3082 -- decorate it with the expected type to avoid cascaded errors
3084 if No (Etype (E)) then
3088 -- If an initialization expression is present, then we set the
3089 -- Is_True_Constant flag. It will be reset if this is a variable
3090 -- and it is indeed modified.
3092 Set_Is_True_Constant (Id, True);
3094 -- If we are analyzing a constant declaration, set its completion
3095 -- flag after analyzing and resolving the expression.
3097 if Constant_Present (N) then
3098 Set_Has_Completion (Id);
3101 -- Set type and resolve (type may be overridden later on)
3106 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3107 -- node (which was marked already-analyzed), we need to set the type
3108 -- to something other than Any_Access in order to keep gigi happy.
3110 if Etype (E) = Any_Access then
3114 -- If the object is an access to variable, the initialization
3115 -- expression cannot be an access to constant.
3117 if Is_Access_Type (T)
3118 and then not Is_Access_Constant (T)
3119 and then Is_Access_Type (Etype (E))
3120 and then Is_Access_Constant (Etype (E))
3123 ("access to variable cannot be initialized "
3124 & "with an access-to-constant expression", E);
3127 if not Assignment_OK (N) then
3128 Check_Initialization (T, E);
3131 Check_Unset_Reference (E);
3133 -- If this is a variable, then set current value. If this is a
3134 -- declared constant of a scalar type with a static expression,
3135 -- indicate that it is always valid.
3137 if not Constant_Present (N) then
3138 if Compile_Time_Known_Value (E) then
3139 Set_Current_Value (Id, E);
3142 elsif Is_Scalar_Type (T)
3143 and then Is_OK_Static_Expression (E)
3145 Set_Is_Known_Valid (Id);
3148 -- Deal with setting of null flags
3150 if Is_Access_Type (T) then
3151 if Known_Non_Null (E) then
3152 Set_Is_Known_Non_Null (Id, True);
3153 elsif Known_Null (E)
3154 and then not Can_Never_Be_Null (Id)
3156 Set_Is_Known_Null (Id, True);
3160 -- Check incorrect use of dynamically tagged expressions.
3162 if Is_Tagged_Type (T) then
3163 Check_Dynamically_Tagged_Expression
3169 Apply_Scalar_Range_Check (E, T);
3170 Apply_Static_Length_Check (E, T);
3172 if Nkind (Original_Node (N)) = N_Object_Declaration
3173 and then Comes_From_Source (Original_Node (N))
3174 and then Formal_Verification_Mode -- only call test if needed
3175 and then not Is_SPARK_Initialization_Expr (E)
3177 Check_Formal_Restriction
3178 ("initialization expression is not appropriate", E);
3182 -- If the No_Streams restriction is set, check that the type of the
3183 -- object is not, and does not contain, any subtype derived from
3184 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3185 -- Has_Stream just for efficiency reasons. There is no point in
3186 -- spending time on a Has_Stream check if the restriction is not set.
3188 if Restriction_Check_Required (No_Streams) then
3189 if Has_Stream (T) then
3190 Check_Restriction (No_Streams, N);
3194 -- Deal with predicate check before we start to do major rewriting.
3195 -- it is OK to initialize and then check the initialized value, since
3196 -- the object goes out of scope if we get a predicate failure. Note
3197 -- that we do this in the analyzer and not the expander because the
3198 -- analyzer does some substantial rewriting in some cases.
3200 -- We need a predicate check if the type has predicates, and if either
3201 -- there is an initializing expression, or for default initialization
3202 -- when we have at least one case of an explicit default initial value.
3204 if not Suppress_Assignment_Checks (N)
3205 and then Present (Predicate_Function (T))
3209 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3212 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3215 -- Case of unconstrained type
3217 if Is_Indefinite_Subtype (T) then
3219 -- Nothing to do in deferred constant case
3221 if Constant_Present (N) and then No (E) then
3224 -- Case of no initialization present
3227 if No_Initialization (N) then
3230 elsif Is_Class_Wide_Type (T) then
3232 ("initialization required in class-wide declaration ", N);
3236 ("unconstrained subtype not allowed (need initialization)",
3237 Object_Definition (N));
3239 if Is_Record_Type (T) and then Has_Discriminants (T) then
3241 ("\provide initial value or explicit discriminant values",
3242 Object_Definition (N));
3245 ("\or give default discriminant values for type&",
3246 Object_Definition (N), T);
3248 elsif Is_Array_Type (T) then
3250 ("\provide initial value or explicit array bounds",
3251 Object_Definition (N));
3255 -- Case of initialization present but in error. Set initial
3256 -- expression as absent (but do not make above complaints)
3258 elsif E = Error then
3259 Set_Expression (N, Empty);
3262 -- Case of initialization present
3265 -- Not allowed in Ada 83
3267 if not Constant_Present (N) then
3268 if Ada_Version = Ada_83
3269 and then Comes_From_Source (Object_Definition (N))
3272 ("(Ada 83) unconstrained variable not allowed",
3273 Object_Definition (N));
3277 -- Now we constrain the variable from the initializing expression
3279 -- If the expression is an aggregate, it has been expanded into
3280 -- individual assignments. Retrieve the actual type from the
3281 -- expanded construct.
3283 if Is_Array_Type (T)
3284 and then No_Initialization (N)
3285 and then Nkind (Original_Node (E)) = N_Aggregate
3289 -- In case of class-wide interface object declarations we delay
3290 -- the generation of the equivalent record type declarations until
3291 -- its expansion because there are cases in they are not required.
3293 elsif Is_Interface (T) then
3297 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
3298 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
3301 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
3303 if Aliased_Present (N) then
3304 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3307 Freeze_Before (N, Act_T);
3308 Freeze_Before (N, T);
3311 elsif Is_Array_Type (T)
3312 and then No_Initialization (N)
3313 and then Nkind (Original_Node (E)) = N_Aggregate
3315 if not Is_Entity_Name (Object_Definition (N)) then
3317 Check_Compile_Time_Size (Act_T);
3319 if Aliased_Present (N) then
3320 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
3324 -- When the given object definition and the aggregate are specified
3325 -- independently, and their lengths might differ do a length check.
3326 -- This cannot happen if the aggregate is of the form (others =>...)
3328 if not Is_Constrained (T) then
3331 elsif Nkind (E) = N_Raise_Constraint_Error then
3333 -- Aggregate is statically illegal. Place back in declaration
3335 Set_Expression (N, E);
3336 Set_No_Initialization (N, False);
3338 elsif T = Etype (E) then
3341 elsif Nkind (E) = N_Aggregate
3342 and then Present (Component_Associations (E))
3343 and then Present (Choices (First (Component_Associations (E))))
3344 and then Nkind (First
3345 (Choices (First (Component_Associations (E))))) = N_Others_Choice
3350 Apply_Length_Check (E, T);
3353 -- If the type is limited unconstrained with defaulted discriminants and
3354 -- there is no expression, then the object is constrained by the
3355 -- defaults, so it is worthwhile building the corresponding subtype.
3357 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
3358 and then not Is_Constrained (T)
3359 and then Has_Discriminants (T)
3362 Act_T := Build_Default_Subtype (T, N);
3364 -- Ada 2005: a limited object may be initialized by means of an
3365 -- aggregate. If the type has default discriminants it has an
3366 -- unconstrained nominal type, Its actual subtype will be obtained
3367 -- from the aggregate, and not from the default discriminants.
3372 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
3374 elsif Present (Underlying_Type (T))
3375 and then not Is_Constrained (Underlying_Type (T))
3376 and then Has_Discriminants (Underlying_Type (T))
3377 and then Nkind (E) = N_Function_Call
3378 and then Constant_Present (N)
3380 -- The back-end has problems with constants of a discriminated type
3381 -- with defaults, if the initial value is a function call. We
3382 -- generate an intermediate temporary for the result of the call.
3383 -- It is unclear why this should make it acceptable to gcc. ???
3385 Remove_Side_Effects (E);
3388 -- Check No_Wide_Characters restriction
3390 Check_Wide_Character_Restriction (T, Object_Definition (N));
3392 -- Indicate this is not set in source. Certainly true for constants,
3393 -- and true for variables so far (will be reset for a variable if and
3394 -- when we encounter a modification in the source).
3396 Set_Never_Set_In_Source (Id, True);
3398 -- Now establish the proper kind and type of the object
3400 if Constant_Present (N) then
3401 Set_Ekind (Id, E_Constant);
3402 Set_Is_True_Constant (Id, True);
3405 Set_Ekind (Id, E_Variable);
3407 -- A variable is set as shared passive if it appears in a shared
3408 -- passive package, and is at the outer level. This is not done
3409 -- for entities generated during expansion, because those are
3410 -- always manipulated locally.
3412 if Is_Shared_Passive (Current_Scope)
3413 and then Is_Library_Level_Entity (Id)
3414 and then Comes_From_Source (Id)
3416 Set_Is_Shared_Passive (Id);
3417 Check_Shared_Var (Id, T, N);
3420 -- Set Has_Initial_Value if initializing expression present. Note
3421 -- that if there is no initializing expression, we leave the state
3422 -- of this flag unchanged (usually it will be False, but notably in
3423 -- the case of exception choice variables, it will already be true).
3426 Set_Has_Initial_Value (Id, True);
3430 -- Initialize alignment and size and capture alignment setting
3432 Init_Alignment (Id);
3434 Set_Optimize_Alignment_Flags (Id);
3436 -- Deal with aliased case
3438 if Aliased_Present (N) then
3439 Set_Is_Aliased (Id);
3441 -- If the object is aliased and the type is unconstrained with
3442 -- defaulted discriminants and there is no expression, then the
3443 -- object is constrained by the defaults, so it is worthwhile
3444 -- building the corresponding subtype.
3446 -- Ada 2005 (AI-363): If the aliased object is discriminated and
3447 -- unconstrained, then only establish an actual subtype if the
3448 -- nominal subtype is indefinite. In definite cases the object is
3449 -- unconstrained in Ada 2005.
3452 and then Is_Record_Type (T)
3453 and then not Is_Constrained (T)
3454 and then Has_Discriminants (T)
3455 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
3457 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
3461 -- Now we can set the type of the object
3463 Set_Etype (Id, Act_T);
3465 -- Deal with controlled types
3467 if Has_Controlled_Component (Etype (Id))
3468 or else Is_Controlled (Etype (Id))
3470 if not Is_Library_Level_Entity (Id) then
3471 Check_Restriction (No_Nested_Finalization, N);
3473 Validate_Controlled_Object (Id);
3476 -- Generate a warning when an initialization causes an obvious ABE
3477 -- violation. If the init expression is a simple aggregate there
3478 -- shouldn't be any initialize/adjust call generated. This will be
3479 -- true as soon as aggregates are built in place when possible.
3481 -- ??? at the moment we do not generate warnings for temporaries
3482 -- created for those aggregates although Program_Error might be
3483 -- generated if compiled with -gnato.
3485 if Is_Controlled (Etype (Id))
3486 and then Comes_From_Source (Id)
3489 BT : constant Entity_Id := Base_Type (Etype (Id));
3491 Implicit_Call : Entity_Id;
3492 pragma Warnings (Off, Implicit_Call);
3493 -- ??? what is this for (never referenced!)
3495 function Is_Aggr (N : Node_Id) return Boolean;
3496 -- Check that N is an aggregate
3502 function Is_Aggr (N : Node_Id) return Boolean is
3504 case Nkind (Original_Node (N)) is
3505 when N_Aggregate | N_Extension_Aggregate =>
3508 when N_Qualified_Expression |
3510 N_Unchecked_Type_Conversion =>
3511 return Is_Aggr (Expression (Original_Node (N)));
3519 -- If no underlying type, we already are in an error situation.
3520 -- Do not try to add a warning since we do not have access to
3523 if No (Underlying_Type (BT)) then
3524 Implicit_Call := Empty;
3526 -- A generic type does not have usable primitive operators.
3527 -- Initialization calls are built for instances.
3529 elsif Is_Generic_Type (BT) then
3530 Implicit_Call := Empty;
3532 -- If the init expression is not an aggregate, an adjust call
3533 -- will be generated
3535 elsif Present (E) and then not Is_Aggr (E) then
3536 Implicit_Call := Find_Prim_Op (BT, Name_Adjust);
3538 -- If no init expression and we are not in the deferred
3539 -- constant case, an Initialize call will be generated
3541 elsif No (E) and then not Constant_Present (N) then
3542 Implicit_Call := Find_Prim_Op (BT, Name_Initialize);
3545 Implicit_Call := Empty;
3551 if Has_Task (Etype (Id)) then
3552 Check_Restriction (No_Tasking, N);
3554 -- Deal with counting max tasks
3556 -- Nothing to do if inside a generic
3558 if Inside_A_Generic then
3561 -- If library level entity, then count tasks
3563 elsif Is_Library_Level_Entity (Id) then
3564 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
3566 -- If not library level entity, then indicate we don't know max
3567 -- tasks and also check task hierarchy restriction and blocking
3568 -- operation (since starting a task is definitely blocking!)
3571 Check_Restriction (Max_Tasks, N);
3572 Check_Restriction (No_Task_Hierarchy, N);
3573 Check_Potentially_Blocking_Operation (N);
3576 -- A rather specialized test. If we see two tasks being declared
3577 -- of the same type in the same object declaration, and the task
3578 -- has an entry with an address clause, we know that program error
3579 -- will be raised at run time since we can't have two tasks with
3580 -- entries at the same address.
3582 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
3587 E := First_Entity (Etype (Id));
3588 while Present (E) loop
3589 if Ekind (E) = E_Entry
3590 and then Present (Get_Attribute_Definition_Clause
3591 (E, Attribute_Address))
3594 ("?more than one task with same entry address", N);
3596 ("\?Program_Error will be raised at run time", N);
3598 Make_Raise_Program_Error (Loc,
3599 Reason => PE_Duplicated_Entry_Address));
3609 -- Some simple constant-propagation: if the expression is a constant
3610 -- string initialized with a literal, share the literal. This avoids
3614 and then Is_Entity_Name (E)
3615 and then Ekind (Entity (E)) = E_Constant
3616 and then Base_Type (Etype (E)) = Standard_String
3619 Val : constant Node_Id := Constant_Value (Entity (E));
3622 and then Nkind (Val) = N_String_Literal
3624 Rewrite (E, New_Copy (Val));
3629 -- Another optimization: if the nominal subtype is unconstrained and
3630 -- the expression is a function call that returns an unconstrained
3631 -- type, rewrite the declaration as a renaming of the result of the
3632 -- call. The exceptions below are cases where the copy is expected,
3633 -- either by the back end (Aliased case) or by the semantics, as for
3634 -- initializing controlled types or copying tags for classwide types.
3637 and then Nkind (E) = N_Explicit_Dereference
3638 and then Nkind (Original_Node (E)) = N_Function_Call
3639 and then not Is_Library_Level_Entity (Id)
3640 and then not Is_Constrained (Underlying_Type (T))
3641 and then not Is_Aliased (Id)
3642 and then not Is_Class_Wide_Type (T)
3643 and then not Is_Controlled (T)
3644 and then not Has_Controlled_Component (Base_Type (T))
3645 and then Expander_Active
3648 Make_Object_Renaming_Declaration (Loc,
3649 Defining_Identifier => Id,
3650 Access_Definition => Empty,
3651 Subtype_Mark => New_Occurrence_Of
3652 (Base_Type (Etype (Id)), Loc),
3655 Set_Renamed_Object (Id, E);
3657 -- Force generation of debugging information for the constant and for
3658 -- the renamed function call.
3660 Set_Debug_Info_Needed (Id);
3661 Set_Debug_Info_Needed (Entity (Prefix (E)));
3664 if Present (Prev_Entity)
3665 and then Is_Frozen (Prev_Entity)
3666 and then not Error_Posted (Id)
3668 Error_Msg_N ("full constant declaration appears too late", N);
3671 Check_Eliminated (Id);
3673 -- Deal with setting In_Private_Part flag if in private part
3675 if Ekind (Scope (Id)) = E_Package
3676 and then In_Private_Part (Scope (Id))
3678 Set_In_Private_Part (Id);
3681 -- Check for violation of No_Local_Timing_Events
3683 if Restriction_Check_Required (No_Local_Timing_Events)
3684 and then not Is_Library_Level_Entity (Id)
3685 and then Is_RTE (Etype (Id), RE_Timing_Event)
3687 Check_Restriction (No_Local_Timing_Events, N);
3691 if Has_Aspects (N) then
3692 Analyze_Aspect_Specifications (N, Id);
3694 end Analyze_Object_Declaration;
3696 ---------------------------
3697 -- Analyze_Others_Choice --
3698 ---------------------------
3700 -- Nothing to do for the others choice node itself, the semantic analysis
3701 -- of the others choice will occur as part of the processing of the parent
3703 procedure Analyze_Others_Choice (N : Node_Id) is
3704 pragma Warnings (Off, N);
3707 end Analyze_Others_Choice;
3709 -------------------------------------------
3710 -- Analyze_Private_Extension_Declaration --
3711 -------------------------------------------
3713 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
3714 T : constant Entity_Id := Defining_Identifier (N);
3715 Indic : constant Node_Id := Subtype_Indication (N);
3716 Parent_Type : Entity_Id;
3717 Parent_Base : Entity_Id;
3720 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
3722 if Is_Non_Empty_List (Interface_List (N)) then
3728 Intf := First (Interface_List (N));
3729 while Present (Intf) loop
3730 T := Find_Type_Of_Subtype_Indic (Intf);
3732 Diagnose_Interface (Intf, T);
3738 Generate_Definition (T);
3740 -- For other than Ada 2012, just enter the name in the current scope
3742 if Ada_Version < Ada_2012 then
3745 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
3746 -- case of private type that completes an incomplete type.
3753 Prev := Find_Type_Name (N);
3755 pragma Assert (Prev = T
3756 or else (Ekind (Prev) = E_Incomplete_Type
3757 and then Present (Full_View (Prev))
3758 and then Full_View (Prev) = T));
3762 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
3763 Parent_Base := Base_Type (Parent_Type);
3765 if Parent_Type = Any_Type
3766 or else Etype (Parent_Type) = Any_Type
3768 Set_Ekind (T, Ekind (Parent_Type));
3769 Set_Etype (T, Any_Type);
3772 elsif not Is_Tagged_Type (Parent_Type) then
3774 ("parent of type extension must be a tagged type ", Indic);
3777 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
3778 Error_Msg_N ("premature derivation of incomplete type", Indic);
3781 elsif Is_Concurrent_Type (Parent_Type) then
3783 ("parent type of a private extension cannot be "
3784 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
3786 Set_Etype (T, Any_Type);
3787 Set_Ekind (T, E_Limited_Private_Type);
3788 Set_Private_Dependents (T, New_Elmt_List);
3789 Set_Error_Posted (T);
3793 -- Perhaps the parent type should be changed to the class-wide type's
3794 -- specific type in this case to prevent cascading errors ???
3796 if Is_Class_Wide_Type (Parent_Type) then
3798 ("parent of type extension must not be a class-wide type", Indic);
3802 if (not Is_Package_Or_Generic_Package (Current_Scope)
3803 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
3804 or else In_Private_Part (Current_Scope)
3807 Error_Msg_N ("invalid context for private extension", N);
3810 -- Set common attributes
3812 Set_Is_Pure (T, Is_Pure (Current_Scope));
3813 Set_Scope (T, Current_Scope);
3814 Set_Ekind (T, E_Record_Type_With_Private);
3815 Init_Size_Align (T);
3817 Set_Etype (T, Parent_Base);
3818 Set_Has_Task (T, Has_Task (Parent_Base));
3820 Set_Convention (T, Convention (Parent_Type));
3821 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
3822 Set_Is_First_Subtype (T);
3823 Make_Class_Wide_Type (T);
3825 if Unknown_Discriminants_Present (N) then
3826 Set_Discriminant_Constraint (T, No_Elist);
3829 Build_Derived_Record_Type (N, Parent_Type, T);
3831 -- Propagate inherited invariant information. The new type has
3832 -- invariants, if the parent type has inheritable invariants,
3833 -- and these invariants can in turn be inherited.
3835 if Has_Inheritable_Invariants (Parent_Type) then
3836 Set_Has_Inheritable_Invariants (T);
3837 Set_Has_Invariants (T);
3840 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
3841 -- synchronized formal derived type.
3843 if Ada_Version >= Ada_2005
3844 and then Synchronized_Present (N)
3846 Set_Is_Limited_Record (T);
3848 -- Formal derived type case
3850 if Is_Generic_Type (T) then
3852 -- The parent must be a tagged limited type or a synchronized
3855 if (not Is_Tagged_Type (Parent_Type)
3856 or else not Is_Limited_Type (Parent_Type))
3858 (not Is_Interface (Parent_Type)
3859 or else not Is_Synchronized_Interface (Parent_Type))
3861 Error_Msg_NE ("parent type of & must be tagged limited " &
3862 "or synchronized", N, T);
3865 -- The progenitors (if any) must be limited or synchronized
3868 if Present (Interfaces (T)) then
3871 Iface_Elmt : Elmt_Id;
3874 Iface_Elmt := First_Elmt (Interfaces (T));
3875 while Present (Iface_Elmt) loop
3876 Iface := Node (Iface_Elmt);
3878 if not Is_Limited_Interface (Iface)
3879 and then not Is_Synchronized_Interface (Iface)
3881 Error_Msg_NE ("progenitor & must be limited " &
3882 "or synchronized", N, Iface);
3885 Next_Elmt (Iface_Elmt);
3890 -- Regular derived extension, the parent must be a limited or
3891 -- synchronized interface.
3894 if not Is_Interface (Parent_Type)
3895 or else (not Is_Limited_Interface (Parent_Type)
3897 not Is_Synchronized_Interface (Parent_Type))
3900 ("parent type of & must be limited interface", N, T);
3904 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
3905 -- extension with a synchronized parent must be explicitly declared
3906 -- synchronized, because the full view will be a synchronized type.
3907 -- This must be checked before the check for limited types below,
3908 -- to ensure that types declared limited are not allowed to extend
3909 -- synchronized interfaces.
3911 elsif Is_Interface (Parent_Type)
3912 and then Is_Synchronized_Interface (Parent_Type)
3913 and then not Synchronized_Present (N)
3916 ("private extension of& must be explicitly synchronized",
3919 elsif Limited_Present (N) then
3920 Set_Is_Limited_Record (T);
3922 if not Is_Limited_Type (Parent_Type)
3924 (not Is_Interface (Parent_Type)
3925 or else not Is_Limited_Interface (Parent_Type))
3927 Error_Msg_NE ("parent type& of limited extension must be limited",
3933 if Has_Aspects (N) then
3934 Analyze_Aspect_Specifications (N, T);
3936 end Analyze_Private_Extension_Declaration;
3938 ---------------------------------
3939 -- Analyze_Subtype_Declaration --
3940 ---------------------------------
3942 procedure Analyze_Subtype_Declaration
3944 Skip : Boolean := False)
3946 Id : constant Entity_Id := Defining_Identifier (N);
3948 R_Checks : Check_Result;
3951 Generate_Definition (Id);
3952 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3953 Init_Size_Align (Id);
3955 -- The following guard condition on Enter_Name is to handle cases where
3956 -- the defining identifier has already been entered into the scope but
3957 -- the declaration as a whole needs to be analyzed.
3959 -- This case in particular happens for derived enumeration types. The
3960 -- derived enumeration type is processed as an inserted enumeration type
3961 -- declaration followed by a rewritten subtype declaration. The defining
3962 -- identifier, however, is entered into the name scope very early in the
3963 -- processing of the original type declaration and therefore needs to be
3964 -- avoided here, when the created subtype declaration is analyzed. (See
3965 -- Build_Derived_Types)
3967 -- This also happens when the full view of a private type is derived
3968 -- type with constraints. In this case the entity has been introduced
3969 -- in the private declaration.
3972 or else (Present (Etype (Id))
3973 and then (Is_Private_Type (Etype (Id))
3974 or else Is_Task_Type (Etype (Id))
3975 or else Is_Rewrite_Substitution (N)))
3983 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
3985 -- Inherit common attributes
3987 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
3988 Set_Is_Volatile (Id, Is_Volatile (T));
3989 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
3990 Set_Is_Atomic (Id, Is_Atomic (T));
3991 Set_Is_Ada_2005_Only (Id, Is_Ada_2005_Only (T));
3992 Set_Is_Ada_2012_Only (Id, Is_Ada_2012_Only (T));
3993 Set_Convention (Id, Convention (T));
3995 -- If ancestor has predicates then so does the subtype, and in addition
3996 -- we must delay the freeze to properly arrange predicate inheritance.
3998 -- The Ancestor_Type test is a big kludge, there seem to be cases in
3999 -- which T = ID, so the above tests and assignments do nothing???
4001 if Has_Predicates (T)
4002 or else (Present (Ancestor_Subtype (T))
4003 and then Has_Predicates (Ancestor_Subtype (T)))
4005 Set_Has_Predicates (Id);
4006 Set_Has_Delayed_Freeze (Id);
4009 -- Subtype of Boolean cannot have a constraint in SPARK or ALFA
4011 if Is_Boolean_Type (T)
4012 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4014 Check_Formal_Restriction
4015 ("subtype of Boolean cannot have constraint", N);
4018 -- String subtype must have a lower bound of 1 in SPARK/ALFA. Note that
4019 -- we do not need to test for the non-static case here, since that was
4020 -- already taken care of in Process_Range_Expr_In_Decl.
4022 if Base_Type (T) = Standard_String
4023 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4026 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4031 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
4032 and then List_Length (Constraints (Cstr)) = 1
4034 Drange := First (Constraints (Cstr));
4036 if Nkind (Drange) = N_Range then
4037 Low := Low_Bound (Drange);
4039 if Is_OK_Static_Expression (Low)
4040 and then Expr_Value (Low) /= 1
4042 Check_Formal_Restriction
4043 ("String subtype must have lower bound of 1", N);
4050 -- In the case where there is no constraint given in the subtype
4051 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4052 -- semantic attributes must be established here.
4054 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4055 Set_Etype (Id, Base_Type (T));
4057 -- Subtype of unconstrained array without constraint is not allowed
4058 -- in SPARK or ALFA.
4060 if Is_Array_Type (T)
4061 and then not Is_Constrained (T)
4063 Check_Formal_Restriction
4064 ("subtype of unconstrained array must have constraint", N);
4069 Set_Ekind (Id, E_Array_Subtype);
4070 Copy_Array_Subtype_Attributes (Id, T);
4072 when Decimal_Fixed_Point_Kind =>
4073 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4074 Set_Digits_Value (Id, Digits_Value (T));
4075 Set_Delta_Value (Id, Delta_Value (T));
4076 Set_Scale_Value (Id, Scale_Value (T));
4077 Set_Small_Value (Id, Small_Value (T));
4078 Set_Scalar_Range (Id, Scalar_Range (T));
4079 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4080 Set_Is_Constrained (Id, Is_Constrained (T));
4081 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4082 Set_RM_Size (Id, RM_Size (T));
4084 when Enumeration_Kind =>
4085 Set_Ekind (Id, E_Enumeration_Subtype);
4086 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4087 Set_Scalar_Range (Id, Scalar_Range (T));
4088 Set_Is_Character_Type (Id, Is_Character_Type (T));
4089 Set_Is_Constrained (Id, Is_Constrained (T));
4090 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4091 Set_RM_Size (Id, RM_Size (T));
4093 when Ordinary_Fixed_Point_Kind =>
4094 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4095 Set_Scalar_Range (Id, Scalar_Range (T));
4096 Set_Small_Value (Id, Small_Value (T));
4097 Set_Delta_Value (Id, Delta_Value (T));
4098 Set_Is_Constrained (Id, Is_Constrained (T));
4099 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4100 Set_RM_Size (Id, RM_Size (T));
4103 Set_Ekind (Id, E_Floating_Point_Subtype);
4104 Set_Scalar_Range (Id, Scalar_Range (T));
4105 Set_Digits_Value (Id, Digits_Value (T));
4106 Set_Is_Constrained (Id, Is_Constrained (T));
4108 when Signed_Integer_Kind =>
4109 Set_Ekind (Id, E_Signed_Integer_Subtype);
4110 Set_Scalar_Range (Id, Scalar_Range (T));
4111 Set_Is_Constrained (Id, Is_Constrained (T));
4112 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4113 Set_RM_Size (Id, RM_Size (T));
4115 when Modular_Integer_Kind =>
4116 Set_Ekind (Id, E_Modular_Integer_Subtype);
4117 Set_Scalar_Range (Id, Scalar_Range (T));
4118 Set_Is_Constrained (Id, Is_Constrained (T));
4119 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4120 Set_RM_Size (Id, RM_Size (T));
4122 when Class_Wide_Kind =>
4123 Set_Ekind (Id, E_Class_Wide_Subtype);
4124 Set_First_Entity (Id, First_Entity (T));
4125 Set_Last_Entity (Id, Last_Entity (T));
4126 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4127 Set_Cloned_Subtype (Id, T);
4128 Set_Is_Tagged_Type (Id, True);
4129 Set_Has_Unknown_Discriminants
4132 if Ekind (T) = E_Class_Wide_Subtype then
4133 Set_Equivalent_Type (Id, Equivalent_Type (T));
4136 when E_Record_Type | E_Record_Subtype =>
4137 Set_Ekind (Id, E_Record_Subtype);
4139 if Ekind (T) = E_Record_Subtype
4140 and then Present (Cloned_Subtype (T))
4142 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4144 Set_Cloned_Subtype (Id, T);
4147 Set_First_Entity (Id, First_Entity (T));
4148 Set_Last_Entity (Id, Last_Entity (T));
4149 Set_Has_Discriminants (Id, Has_Discriminants (T));
4150 Set_Is_Constrained (Id, Is_Constrained (T));
4151 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4152 Set_Has_Unknown_Discriminants
4153 (Id, Has_Unknown_Discriminants (T));
4155 if Has_Discriminants (T) then
4156 Set_Discriminant_Constraint
4157 (Id, Discriminant_Constraint (T));
4158 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4160 elsif Has_Unknown_Discriminants (Id) then
4161 Set_Discriminant_Constraint (Id, No_Elist);
4164 if Is_Tagged_Type (T) then
4165 Set_Is_Tagged_Type (Id);
4166 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4167 Set_Direct_Primitive_Operations
4168 (Id, Direct_Primitive_Operations (T));
4169 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4171 if Is_Interface (T) then
4172 Set_Is_Interface (Id);
4173 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4177 when Private_Kind =>
4178 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4179 Set_Has_Discriminants (Id, Has_Discriminants (T));
4180 Set_Is_Constrained (Id, Is_Constrained (T));
4181 Set_First_Entity (Id, First_Entity (T));
4182 Set_Last_Entity (Id, Last_Entity (T));
4183 Set_Private_Dependents (Id, New_Elmt_List);
4184 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4185 Set_Has_Unknown_Discriminants
4186 (Id, Has_Unknown_Discriminants (T));
4187 Set_Known_To_Have_Preelab_Init
4188 (Id, Known_To_Have_Preelab_Init (T));
4190 if Is_Tagged_Type (T) then
4191 Set_Is_Tagged_Type (Id);
4192 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4193 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4194 Set_Direct_Primitive_Operations (Id,
4195 Direct_Primitive_Operations (T));
4198 -- In general the attributes of the subtype of a private type
4199 -- are the attributes of the partial view of parent. However,
4200 -- the full view may be a discriminated type, and the subtype
4201 -- must share the discriminant constraint to generate correct
4202 -- calls to initialization procedures.
4204 if Has_Discriminants (T) then
4205 Set_Discriminant_Constraint
4206 (Id, Discriminant_Constraint (T));
4207 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4209 elsif Present (Full_View (T))
4210 and then Has_Discriminants (Full_View (T))
4212 Set_Discriminant_Constraint
4213 (Id, Discriminant_Constraint (Full_View (T)));
4214 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4216 -- This would seem semantically correct, but apparently
4217 -- confuses the back-end. To be explained and checked with
4218 -- current version ???
4220 -- Set_Has_Discriminants (Id);
4223 Prepare_Private_Subtype_Completion (Id, N);
4226 Set_Ekind (Id, E_Access_Subtype);
4227 Set_Is_Constrained (Id, Is_Constrained (T));
4228 Set_Is_Access_Constant
4229 (Id, Is_Access_Constant (T));
4230 Set_Directly_Designated_Type
4231 (Id, Designated_Type (T));
4232 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
4234 -- A Pure library_item must not contain the declaration of a
4235 -- named access type, except within a subprogram, generic
4236 -- subprogram, task unit, or protected unit, or if it has
4237 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
4239 if Comes_From_Source (Id)
4240 and then In_Pure_Unit
4241 and then not In_Subprogram_Task_Protected_Unit
4242 and then not No_Pool_Assigned (Id)
4245 ("named access types not allowed in pure unit", N);
4248 when Concurrent_Kind =>
4249 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4250 Set_Corresponding_Record_Type (Id,
4251 Corresponding_Record_Type (T));
4252 Set_First_Entity (Id, First_Entity (T));
4253 Set_First_Private_Entity (Id, First_Private_Entity (T));
4254 Set_Has_Discriminants (Id, Has_Discriminants (T));
4255 Set_Is_Constrained (Id, Is_Constrained (T));
4256 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
4257 Set_Last_Entity (Id, Last_Entity (T));
4259 if Has_Discriminants (T) then
4260 Set_Discriminant_Constraint (Id,
4261 Discriminant_Constraint (T));
4262 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4265 when E_Incomplete_Type =>
4266 if Ada_Version >= Ada_2005 then
4267 Set_Ekind (Id, E_Incomplete_Subtype);
4269 -- Ada 2005 (AI-412): Decorate an incomplete subtype
4270 -- of an incomplete type visible through a limited
4273 if From_With_Type (T)
4274 and then Present (Non_Limited_View (T))
4276 Set_From_With_Type (Id);
4277 Set_Non_Limited_View (Id, Non_Limited_View (T));
4279 -- Ada 2005 (AI-412): Add the regular incomplete subtype
4280 -- to the private dependents of the original incomplete
4281 -- type for future transformation.
4284 Append_Elmt (Id, Private_Dependents (T));
4287 -- If the subtype name denotes an incomplete type an error
4288 -- was already reported by Process_Subtype.
4291 Set_Etype (Id, Any_Type);
4295 raise Program_Error;
4299 if Etype (Id) = Any_Type then
4303 -- Some common processing on all types
4305 Set_Size_Info (Id, T);
4306 Set_First_Rep_Item (Id, First_Rep_Item (T));
4310 Set_Is_Immediately_Visible (Id, True);
4311 Set_Depends_On_Private (Id, Has_Private_Component (T));
4312 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
4314 if Is_Interface (T) then
4315 Set_Is_Interface (Id);
4318 if Present (Generic_Parent_Type (N))
4321 (Parent (Generic_Parent_Type (N))) /= N_Formal_Type_Declaration
4323 (Formal_Type_Definition (Parent (Generic_Parent_Type (N))))
4324 /= N_Formal_Private_Type_Definition)
4326 if Is_Tagged_Type (Id) then
4328 -- If this is a generic actual subtype for a synchronized type,
4329 -- the primitive operations are those of the corresponding record
4330 -- for which there is a separate subtype declaration.
4332 if Is_Concurrent_Type (Id) then
4334 elsif Is_Class_Wide_Type (Id) then
4335 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
4337 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
4340 elsif Scope (Etype (Id)) /= Standard_Standard then
4341 Derive_Subprograms (Generic_Parent_Type (N), Id);
4345 if Is_Private_Type (T)
4346 and then Present (Full_View (T))
4348 Conditional_Delay (Id, Full_View (T));
4350 -- The subtypes of components or subcomponents of protected types
4351 -- do not need freeze nodes, which would otherwise appear in the
4352 -- wrong scope (before the freeze node for the protected type). The
4353 -- proper subtypes are those of the subcomponents of the corresponding
4356 elsif Ekind (Scope (Id)) /= E_Protected_Type
4357 and then Present (Scope (Scope (Id))) -- error defense!
4358 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
4360 Conditional_Delay (Id, T);
4363 -- Check that constraint_error is raised for a scalar subtype
4364 -- indication when the lower or upper bound of a non-null range
4365 -- lies outside the range of the type mark.
4367 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4368 if Is_Scalar_Type (Etype (Id))
4369 and then Scalar_Range (Id) /=
4370 Scalar_Range (Etype (Subtype_Mark
4371 (Subtype_Indication (N))))
4375 Etype (Subtype_Mark (Subtype_Indication (N))));
4377 elsif Is_Array_Type (Etype (Id))
4378 and then Present (First_Index (Id))
4380 -- This really should be a subprogram that finds the indications
4383 if ((Nkind (First_Index (Id)) = N_Identifier
4384 and then Ekind (Entity (First_Index (Id))) in Scalar_Kind)
4385 or else Nkind (First_Index (Id)) = N_Subtype_Indication)
4387 Nkind (Scalar_Range (Etype (First_Index (Id)))) = N_Range
4390 Target_Typ : constant Entity_Id :=
4393 (Subtype_Mark (Subtype_Indication (N)))));
4397 (Scalar_Range (Etype (First_Index (Id))),
4399 Etype (First_Index (Id)),
4400 Defining_Identifier (N));
4406 Sloc (Defining_Identifier (N)));
4412 -- Make sure that generic actual types are properly frozen. The subtype
4413 -- is marked as a generic actual type when the enclosing instance is
4414 -- analyzed, so here we identify the subtype from the tree structure.
4417 and then Is_Generic_Actual_Type (Id)
4418 and then In_Instance
4419 and then not Comes_From_Source (N)
4420 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
4421 and then Is_Frozen (T)
4423 Freeze_Before (N, Id);
4426 Set_Optimize_Alignment_Flags (Id);
4427 Check_Eliminated (Id);
4430 if Has_Aspects (N) then
4431 Analyze_Aspect_Specifications (N, Id);
4433 end Analyze_Subtype_Declaration;
4435 --------------------------------
4436 -- Analyze_Subtype_Indication --
4437 --------------------------------
4439 procedure Analyze_Subtype_Indication (N : Node_Id) is
4440 T : constant Entity_Id := Subtype_Mark (N);
4441 R : constant Node_Id := Range_Expression (Constraint (N));
4448 Set_Etype (N, Etype (R));
4449 Resolve (R, Entity (T));
4451 Set_Error_Posted (R);
4452 Set_Error_Posted (T);
4454 end Analyze_Subtype_Indication;
4456 --------------------------
4457 -- Analyze_Variant_Part --
4458 --------------------------
4460 procedure Analyze_Variant_Part (N : Node_Id) is
4462 procedure Non_Static_Choice_Error (Choice : Node_Id);
4463 -- Error routine invoked by the generic instantiation below when the
4464 -- variant part has a non static choice.
4466 procedure Process_Declarations (Variant : Node_Id);
4467 -- Analyzes all the declarations associated with a Variant. Needed by
4468 -- the generic instantiation below.
4470 package Variant_Choices_Processing is new
4471 Generic_Choices_Processing
4472 (Get_Alternatives => Variants,
4473 Get_Choices => Discrete_Choices,
4474 Process_Empty_Choice => No_OP,
4475 Process_Non_Static_Choice => Non_Static_Choice_Error,
4476 Process_Associated_Node => Process_Declarations);
4477 use Variant_Choices_Processing;
4478 -- Instantiation of the generic choice processing package
4480 -----------------------------
4481 -- Non_Static_Choice_Error --
4482 -----------------------------
4484 procedure Non_Static_Choice_Error (Choice : Node_Id) is
4486 Flag_Non_Static_Expr
4487 ("choice given in variant part is not static!", Choice);
4488 end Non_Static_Choice_Error;
4490 --------------------------
4491 -- Process_Declarations --
4492 --------------------------
4494 procedure Process_Declarations (Variant : Node_Id) is
4496 if not Null_Present (Component_List (Variant)) then
4497 Analyze_Declarations (Component_Items (Component_List (Variant)));
4499 if Present (Variant_Part (Component_List (Variant))) then
4500 Analyze (Variant_Part (Component_List (Variant)));
4503 end Process_Declarations;
4507 Discr_Name : Node_Id;
4508 Discr_Type : Entity_Id;
4510 Dont_Care : Boolean;
4511 Others_Present : Boolean := False;
4513 pragma Warnings (Off, Dont_Care);
4514 pragma Warnings (Off, Others_Present);
4515 -- We don't care about the assigned values of any of these
4517 -- Start of processing for Analyze_Variant_Part
4520 Discr_Name := Name (N);
4521 Analyze (Discr_Name);
4523 -- If Discr_Name bad, get out (prevent cascaded errors)
4525 if Etype (Discr_Name) = Any_Type then
4529 -- Check invalid discriminant in variant part
4531 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
4532 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
4535 Discr_Type := Etype (Entity (Discr_Name));
4537 if not Is_Discrete_Type (Discr_Type) then
4539 ("discriminant in a variant part must be of a discrete type",
4544 -- Call the instantiated Analyze_Choices which does the rest of the work
4546 Analyze_Choices (N, Discr_Type, Dont_Care, Others_Present);
4547 end Analyze_Variant_Part;
4549 ----------------------------
4550 -- Array_Type_Declaration --
4551 ----------------------------
4553 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
4554 Component_Def : constant Node_Id := Component_Definition (Def);
4555 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
4556 Element_Type : Entity_Id;
4557 Implicit_Base : Entity_Id;
4559 Related_Id : Entity_Id := Empty;
4561 P : constant Node_Id := Parent (Def);
4565 if Nkind (Def) = N_Constrained_Array_Definition then
4566 Index := First (Discrete_Subtype_Definitions (Def));
4568 Index := First (Subtype_Marks (Def));
4571 -- Find proper names for the implicit types which may be public. In case
4572 -- of anonymous arrays we use the name of the first object of that type
4576 Related_Id := Defining_Identifier (P);
4582 while Present (Index) loop
4583 if Nkind (Index) /= N_Identifier then
4584 Check_Formal_Restriction ("subtype mark required", Index);
4589 -- Add a subtype declaration for each index of private array type
4590 -- declaration whose etype is also private. For example:
4593 -- type Index is private;
4595 -- type Table is array (Index) of ...
4598 -- This is currently required by the expander for the internally
4599 -- generated equality subprogram of records with variant parts in
4600 -- which the etype of some component is such private type.
4602 if Ekind (Current_Scope) = E_Package
4603 and then In_Private_Part (Current_Scope)
4604 and then Has_Private_Declaration (Etype (Index))
4607 Loc : constant Source_Ptr := Sloc (Def);
4612 New_E := Make_Temporary (Loc, 'T');
4613 Set_Is_Internal (New_E);
4616 Make_Subtype_Declaration (Loc,
4617 Defining_Identifier => New_E,
4618 Subtype_Indication =>
4619 New_Occurrence_Of (Etype (Index), Loc));
4621 Insert_Before (Parent (Def), Decl);
4623 Set_Etype (Index, New_E);
4625 -- If the index is a range the Entity attribute is not
4626 -- available. Example:
4629 -- type T is private;
4631 -- type T is new Natural;
4632 -- Table : array (T(1) .. T(10)) of Boolean;
4635 if Nkind (Index) /= N_Range then
4636 Set_Entity (Index, New_E);
4641 Make_Index (Index, P, Related_Id, Nb_Index);
4643 -- Check error of subtype with predicate for index type
4645 Bad_Predicated_Subtype_Use
4646 ("subtype& has predicate, not allowed as index subtype",
4647 Index, Etype (Index));
4649 -- Move to next index
4652 Nb_Index := Nb_Index + 1;
4655 -- Process subtype indication if one is present
4657 if Present (Component_Typ) then
4658 if Nkind (Component_Typ) /= N_Identifier then
4659 Check_Formal_Restriction ("subtype mark required", Component_Typ);
4662 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
4664 -- Ada 2005 (AI-230): Access Definition case
4666 else pragma Assert (Present (Access_Definition (Component_Def)));
4668 -- Indicate that the anonymous access type is created by the
4669 -- array type declaration.
4671 Element_Type := Access_Definition
4673 N => Access_Definition (Component_Def));
4674 Set_Is_Local_Anonymous_Access (Element_Type);
4676 -- Propagate the parent. This field is needed if we have to generate
4677 -- the master_id associated with an anonymous access to task type
4678 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
4680 Set_Parent (Element_Type, Parent (T));
4682 -- Ada 2005 (AI-230): In case of components that are anonymous access
4683 -- types the level of accessibility depends on the enclosing type
4686 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
4688 -- Ada 2005 (AI-254)
4691 CD : constant Node_Id :=
4692 Access_To_Subprogram_Definition
4693 (Access_Definition (Component_Def));
4695 if Present (CD) and then Protected_Present (CD) then
4697 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
4702 -- Constrained array case
4705 T := Create_Itype (E_Void, P, Related_Id, 'T');
4708 if Nkind (Def) = N_Constrained_Array_Definition then
4710 -- Establish Implicit_Base as unconstrained base type
4712 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
4714 Set_Etype (Implicit_Base, Implicit_Base);
4715 Set_Scope (Implicit_Base, Current_Scope);
4716 Set_Has_Delayed_Freeze (Implicit_Base);
4718 -- The constrained array type is a subtype of the unconstrained one
4720 Set_Ekind (T, E_Array_Subtype);
4721 Init_Size_Align (T);
4722 Set_Etype (T, Implicit_Base);
4723 Set_Scope (T, Current_Scope);
4724 Set_Is_Constrained (T, True);
4725 Set_First_Index (T, First (Discrete_Subtype_Definitions (Def)));
4726 Set_Has_Delayed_Freeze (T);
4728 -- Complete setup of implicit base type
4730 Set_First_Index (Implicit_Base, First_Index (T));
4731 Set_Component_Type (Implicit_Base, Element_Type);
4732 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
4733 Set_Component_Size (Implicit_Base, Uint_0);
4734 Set_Packed_Array_Type (Implicit_Base, Empty);
4735 Set_Has_Controlled_Component
4736 (Implicit_Base, Has_Controlled_Component
4738 or else Is_Controlled
4740 Set_Finalize_Storage_Only
4741 (Implicit_Base, Finalize_Storage_Only
4744 -- Unconstrained array case
4747 Set_Ekind (T, E_Array_Type);
4748 Init_Size_Align (T);
4750 Set_Scope (T, Current_Scope);
4751 Set_Component_Size (T, Uint_0);
4752 Set_Is_Constrained (T, False);
4753 Set_First_Index (T, First (Subtype_Marks (Def)));
4754 Set_Has_Delayed_Freeze (T, True);
4755 Set_Has_Task (T, Has_Task (Element_Type));
4756 Set_Has_Controlled_Component (T, Has_Controlled_Component
4759 Is_Controlled (Element_Type));
4760 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
4764 -- Common attributes for both cases
4766 Set_Component_Type (Base_Type (T), Element_Type);
4767 Set_Packed_Array_Type (T, Empty);
4769 if Aliased_Present (Component_Definition (Def)) then
4770 Check_Formal_Restriction
4771 ("aliased is not allowed", Component_Definition (Def));
4772 Set_Has_Aliased_Components (Etype (T));
4775 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
4776 -- array type to ensure that objects of this type are initialized.
4778 if Ada_Version >= Ada_2005
4779 and then Can_Never_Be_Null (Element_Type)
4781 Set_Can_Never_Be_Null (T);
4783 if Null_Exclusion_Present (Component_Definition (Def))
4785 -- No need to check itypes because in their case this check was
4786 -- done at their point of creation
4788 and then not Is_Itype (Element_Type)
4791 ("`NOT NULL` not allowed (null already excluded)",
4792 Subtype_Indication (Component_Definition (Def)));
4796 Priv := Private_Component (Element_Type);
4798 if Present (Priv) then
4800 -- Check for circular definitions
4802 if Priv = Any_Type then
4803 Set_Component_Type (Etype (T), Any_Type);
4805 -- There is a gap in the visibility of operations on the composite
4806 -- type only if the component type is defined in a different scope.
4808 elsif Scope (Priv) = Current_Scope then
4811 elsif Is_Limited_Type (Priv) then
4812 Set_Is_Limited_Composite (Etype (T));
4813 Set_Is_Limited_Composite (T);
4815 Set_Is_Private_Composite (Etype (T));
4816 Set_Is_Private_Composite (T);
4820 -- A syntax error in the declaration itself may lead to an empty index
4821 -- list, in which case do a minimal patch.
4823 if No (First_Index (T)) then
4824 Error_Msg_N ("missing index definition in array type declaration", T);
4827 Indexes : constant List_Id :=
4828 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
4830 Set_Discrete_Subtype_Definitions (Def, Indexes);
4831 Set_First_Index (T, First (Indexes));
4836 -- Create a concatenation operator for the new type. Internal array
4837 -- types created for packed entities do not need such, they are
4838 -- compatible with the user-defined type.
4840 if Number_Dimensions (T) = 1
4841 and then not Is_Packed_Array_Type (T)
4843 New_Concatenation_Op (T);
4846 -- In the case of an unconstrained array the parser has already verified
4847 -- that all the indexes are unconstrained but we still need to make sure
4848 -- that the element type is constrained.
4850 if Is_Indefinite_Subtype (Element_Type) then
4852 ("unconstrained element type in array declaration",
4853 Subtype_Indication (Component_Def));
4855 elsif Is_Abstract_Type (Element_Type) then
4857 ("the type of a component cannot be abstract",
4858 Subtype_Indication (Component_Def));
4860 end Array_Type_Declaration;
4862 ------------------------------------------------------
4863 -- Replace_Anonymous_Access_To_Protected_Subprogram --
4864 ------------------------------------------------------
4866 function Replace_Anonymous_Access_To_Protected_Subprogram
4867 (N : Node_Id) return Entity_Id
4869 Loc : constant Source_Ptr := Sloc (N);
4871 Curr_Scope : constant Scope_Stack_Entry :=
4872 Scope_Stack.Table (Scope_Stack.Last);
4874 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
4881 Set_Is_Internal (Anon);
4884 when N_Component_Declaration |
4885 N_Unconstrained_Array_Definition |
4886 N_Constrained_Array_Definition =>
4887 Comp := Component_Definition (N);
4888 Acc := Access_Definition (Comp);
4890 when N_Discriminant_Specification =>
4891 Comp := Discriminant_Type (N);
4894 when N_Parameter_Specification =>
4895 Comp := Parameter_Type (N);
4898 when N_Access_Function_Definition =>
4899 Comp := Result_Definition (N);
4902 when N_Object_Declaration =>
4903 Comp := Object_Definition (N);
4906 when N_Function_Specification =>
4907 Comp := Result_Definition (N);
4911 raise Program_Error;
4914 Decl := Make_Full_Type_Declaration (Loc,
4915 Defining_Identifier => Anon,
4917 Copy_Separate_Tree (Access_To_Subprogram_Definition (Acc)));
4919 Mark_Rewrite_Insertion (Decl);
4921 -- Insert the new declaration in the nearest enclosing scope. If the
4922 -- node is a body and N is its return type, the declaration belongs in
4923 -- the enclosing scope.
4927 if Nkind (P) = N_Subprogram_Body
4928 and then Nkind (N) = N_Function_Specification
4933 while Present (P) and then not Has_Declarations (P) loop
4937 pragma Assert (Present (P));
4939 if Nkind (P) = N_Package_Specification then
4940 Prepend (Decl, Visible_Declarations (P));
4942 Prepend (Decl, Declarations (P));
4945 -- Replace the anonymous type with an occurrence of the new declaration.
4946 -- In all cases the rewritten node does not have the null-exclusion
4947 -- attribute because (if present) it was already inherited by the
4948 -- anonymous entity (Anon). Thus, in case of components we do not
4949 -- inherit this attribute.
4951 if Nkind (N) = N_Parameter_Specification then
4952 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4953 Set_Etype (Defining_Identifier (N), Anon);
4954 Set_Null_Exclusion_Present (N, False);
4956 elsif Nkind (N) = N_Object_Declaration then
4957 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4958 Set_Etype (Defining_Identifier (N), Anon);
4960 elsif Nkind (N) = N_Access_Function_Definition then
4961 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4963 elsif Nkind (N) = N_Function_Specification then
4964 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
4965 Set_Etype (Defining_Unit_Name (N), Anon);
4969 Make_Component_Definition (Loc,
4970 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
4973 Mark_Rewrite_Insertion (Comp);
4975 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
4979 -- Temporarily remove the current scope (record or subprogram) from
4980 -- the stack to add the new declarations to the enclosing scope.
4982 Scope_Stack.Decrement_Last;
4984 Set_Is_Itype (Anon);
4985 Scope_Stack.Append (Curr_Scope);
4988 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
4989 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
4991 end Replace_Anonymous_Access_To_Protected_Subprogram;
4993 -------------------------------
4994 -- Build_Derived_Access_Type --
4995 -------------------------------
4997 procedure Build_Derived_Access_Type
4999 Parent_Type : Entity_Id;
5000 Derived_Type : Entity_Id)
5002 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5004 Desig_Type : Entity_Id;
5006 Discr_Con_Elist : Elist_Id;
5007 Discr_Con_El : Elmt_Id;
5011 -- Set the designated type so it is available in case this is an access
5012 -- to a self-referential type, e.g. a standard list type with a next
5013 -- pointer. Will be reset after subtype is built.
5015 Set_Directly_Designated_Type
5016 (Derived_Type, Designated_Type (Parent_Type));
5018 Subt := Process_Subtype (S, N);
5020 if Nkind (S) /= N_Subtype_Indication
5021 and then Subt /= Base_Type (Subt)
5023 Set_Ekind (Derived_Type, E_Access_Subtype);
5026 if Ekind (Derived_Type) = E_Access_Subtype then
5028 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5029 Ibase : constant Entity_Id :=
5030 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5031 Svg_Chars : constant Name_Id := Chars (Ibase);
5032 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5035 Copy_Node (Pbase, Ibase);
5037 Set_Chars (Ibase, Svg_Chars);
5038 Set_Next_Entity (Ibase, Svg_Next_E);
5039 Set_Sloc (Ibase, Sloc (Derived_Type));
5040 Set_Scope (Ibase, Scope (Derived_Type));
5041 Set_Freeze_Node (Ibase, Empty);
5042 Set_Is_Frozen (Ibase, False);
5043 Set_Comes_From_Source (Ibase, False);
5044 Set_Is_First_Subtype (Ibase, False);
5046 Set_Etype (Ibase, Pbase);
5047 Set_Etype (Derived_Type, Ibase);
5051 Set_Directly_Designated_Type
5052 (Derived_Type, Designated_Type (Subt));
5054 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5055 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5056 Set_Size_Info (Derived_Type, Parent_Type);
5057 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5058 Set_Depends_On_Private (Derived_Type,
5059 Has_Private_Component (Derived_Type));
5060 Conditional_Delay (Derived_Type, Subt);
5062 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
5063 -- that it is not redundant.
5065 if Null_Exclusion_Present (Type_Definition (N)) then
5066 Set_Can_Never_Be_Null (Derived_Type);
5068 if Can_Never_Be_Null (Parent_Type)
5072 ("`NOT NULL` not allowed (& already excludes null)",
5076 elsif Can_Never_Be_Null (Parent_Type) then
5077 Set_Can_Never_Be_Null (Derived_Type);
5080 -- Note: we do not copy the Storage_Size_Variable, since we always go to
5081 -- the root type for this information.
5083 -- Apply range checks to discriminants for derived record case
5084 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
5086 Desig_Type := Designated_Type (Derived_Type);
5087 if Is_Composite_Type (Desig_Type)
5088 and then (not Is_Array_Type (Desig_Type))
5089 and then Has_Discriminants (Desig_Type)
5090 and then Base_Type (Desig_Type) /= Desig_Type
5092 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
5093 Discr_Con_El := First_Elmt (Discr_Con_Elist);
5095 Discr := First_Discriminant (Base_Type (Desig_Type));
5096 while Present (Discr_Con_El) loop
5097 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
5098 Next_Elmt (Discr_Con_El);
5099 Next_Discriminant (Discr);
5102 end Build_Derived_Access_Type;
5104 ------------------------------
5105 -- Build_Derived_Array_Type --
5106 ------------------------------
5108 procedure Build_Derived_Array_Type
5110 Parent_Type : Entity_Id;
5111 Derived_Type : Entity_Id)
5113 Loc : constant Source_Ptr := Sloc (N);
5114 Tdef : constant Node_Id := Type_Definition (N);
5115 Indic : constant Node_Id := Subtype_Indication (Tdef);
5116 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5117 Implicit_Base : Entity_Id;
5118 New_Indic : Node_Id;
5120 procedure Make_Implicit_Base;
5121 -- If the parent subtype is constrained, the derived type is a subtype
5122 -- of an implicit base type derived from the parent base.
5124 ------------------------
5125 -- Make_Implicit_Base --
5126 ------------------------
5128 procedure Make_Implicit_Base is
5131 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5133 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5134 Set_Etype (Implicit_Base, Parent_Base);
5136 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
5137 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
5139 Set_Has_Delayed_Freeze (Implicit_Base, True);
5140 end Make_Implicit_Base;
5142 -- Start of processing for Build_Derived_Array_Type
5145 if not Is_Constrained (Parent_Type) then
5146 if Nkind (Indic) /= N_Subtype_Indication then
5147 Set_Ekind (Derived_Type, E_Array_Type);
5149 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5150 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
5152 Set_Has_Delayed_Freeze (Derived_Type, True);
5156 Set_Etype (Derived_Type, Implicit_Base);
5159 Make_Subtype_Declaration (Loc,
5160 Defining_Identifier => Derived_Type,
5161 Subtype_Indication =>
5162 Make_Subtype_Indication (Loc,
5163 Subtype_Mark => New_Reference_To (Implicit_Base, Loc),
5164 Constraint => Constraint (Indic)));
5166 Rewrite (N, New_Indic);
5171 if Nkind (Indic) /= N_Subtype_Indication then
5174 Set_Ekind (Derived_Type, Ekind (Parent_Type));
5175 Set_Etype (Derived_Type, Implicit_Base);
5176 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
5179 Error_Msg_N ("illegal constraint on constrained type", Indic);
5183 -- If parent type is not a derived type itself, and is declared in
5184 -- closed scope (e.g. a subprogram), then we must explicitly introduce
5185 -- the new type's concatenation operator since Derive_Subprograms
5186 -- will not inherit the parent's operator. If the parent type is
5187 -- unconstrained, the operator is of the unconstrained base type.
5189 if Number_Dimensions (Parent_Type) = 1
5190 and then not Is_Limited_Type (Parent_Type)
5191 and then not Is_Derived_Type (Parent_Type)
5192 and then not Is_Package_Or_Generic_Package
5193 (Scope (Base_Type (Parent_Type)))
5195 if not Is_Constrained (Parent_Type)
5196 and then Is_Constrained (Derived_Type)
5198 New_Concatenation_Op (Implicit_Base);
5200 New_Concatenation_Op (Derived_Type);
5203 end Build_Derived_Array_Type;
5205 -----------------------------------
5206 -- Build_Derived_Concurrent_Type --
5207 -----------------------------------
5209 procedure Build_Derived_Concurrent_Type
5211 Parent_Type : Entity_Id;
5212 Derived_Type : Entity_Id)
5214 Loc : constant Source_Ptr := Sloc (N);
5216 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
5217 Corr_Decl : Node_Id;
5218 Corr_Decl_Needed : Boolean;
5219 -- If the derived type has fewer discriminants than its parent, the
5220 -- corresponding record is also a derived type, in order to account for
5221 -- the bound discriminants. We create a full type declaration for it in
5224 Constraint_Present : constant Boolean :=
5225 Nkind (Subtype_Indication (Type_Definition (N))) =
5226 N_Subtype_Indication;
5228 D_Constraint : Node_Id;
5229 New_Constraint : Elist_Id;
5230 Old_Disc : Entity_Id;
5231 New_Disc : Entity_Id;
5235 Set_Stored_Constraint (Derived_Type, No_Elist);
5236 Corr_Decl_Needed := False;
5239 if Present (Discriminant_Specifications (N))
5240 and then Constraint_Present
5242 Old_Disc := First_Discriminant (Parent_Type);
5243 New_Disc := First (Discriminant_Specifications (N));
5244 while Present (New_Disc) and then Present (Old_Disc) loop
5245 Next_Discriminant (Old_Disc);
5250 if Present (Old_Disc) and then Expander_Active then
5252 -- The new type has fewer discriminants, so we need to create a new
5253 -- corresponding record, which is derived from the corresponding
5254 -- record of the parent, and has a stored constraint that captures
5255 -- the values of the discriminant constraints. The corresponding
5256 -- record is needed only if expander is active and code generation is
5259 -- The type declaration for the derived corresponding record has the
5260 -- same discriminant part and constraints as the current declaration.
5261 -- Copy the unanalyzed tree to build declaration.
5263 Corr_Decl_Needed := True;
5264 New_N := Copy_Separate_Tree (N);
5267 Make_Full_Type_Declaration (Loc,
5268 Defining_Identifier => Corr_Record,
5269 Discriminant_Specifications =>
5270 Discriminant_Specifications (New_N),
5272 Make_Derived_Type_Definition (Loc,
5273 Subtype_Indication =>
5274 Make_Subtype_Indication (Loc,
5277 (Corresponding_Record_Type (Parent_Type), Loc),
5280 (Subtype_Indication (Type_Definition (New_N))))));
5283 -- Copy Storage_Size and Relative_Deadline variables if task case
5285 if Is_Task_Type (Parent_Type) then
5286 Set_Storage_Size_Variable (Derived_Type,
5287 Storage_Size_Variable (Parent_Type));
5288 Set_Relative_Deadline_Variable (Derived_Type,
5289 Relative_Deadline_Variable (Parent_Type));
5292 if Present (Discriminant_Specifications (N)) then
5293 Push_Scope (Derived_Type);
5294 Check_Or_Process_Discriminants (N, Derived_Type);
5296 if Constraint_Present then
5298 Expand_To_Stored_Constraint
5300 Build_Discriminant_Constraints
5302 Subtype_Indication (Type_Definition (N)), True));
5307 elsif Constraint_Present then
5309 -- Build constrained subtype and derive from it
5312 Loc : constant Source_Ptr := Sloc (N);
5313 Anon : constant Entity_Id :=
5314 Make_Defining_Identifier (Loc,
5315 Chars => New_External_Name (Chars (Derived_Type), 'T'));
5320 Make_Subtype_Declaration (Loc,
5321 Defining_Identifier => Anon,
5322 Subtype_Indication =>
5323 Subtype_Indication (Type_Definition (N)));
5324 Insert_Before (N, Decl);
5327 Rewrite (Subtype_Indication (Type_Definition (N)),
5328 New_Occurrence_Of (Anon, Loc));
5329 Set_Analyzed (Derived_Type, False);
5335 -- By default, operations and private data are inherited from parent.
5336 -- However, in the presence of bound discriminants, a new corresponding
5337 -- record will be created, see below.
5339 Set_Has_Discriminants
5340 (Derived_Type, Has_Discriminants (Parent_Type));
5341 Set_Corresponding_Record_Type
5342 (Derived_Type, Corresponding_Record_Type (Parent_Type));
5344 -- Is_Constrained is set according the parent subtype, but is set to
5345 -- False if the derived type is declared with new discriminants.
5349 (Is_Constrained (Parent_Type) or else Constraint_Present)
5350 and then not Present (Discriminant_Specifications (N)));
5352 if Constraint_Present then
5353 if not Has_Discriminants (Parent_Type) then
5354 Error_Msg_N ("untagged parent must have discriminants", N);
5356 elsif Present (Discriminant_Specifications (N)) then
5358 -- Verify that new discriminants are used to constrain old ones
5363 (Constraint (Subtype_Indication (Type_Definition (N)))));
5365 Old_Disc := First_Discriminant (Parent_Type);
5367 while Present (D_Constraint) loop
5368 if Nkind (D_Constraint) /= N_Discriminant_Association then
5370 -- Positional constraint. If it is a reference to a new
5371 -- discriminant, it constrains the corresponding old one.
5373 if Nkind (D_Constraint) = N_Identifier then
5374 New_Disc := First_Discriminant (Derived_Type);
5375 while Present (New_Disc) loop
5376 exit when Chars (New_Disc) = Chars (D_Constraint);
5377 Next_Discriminant (New_Disc);
5380 if Present (New_Disc) then
5381 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
5385 Next_Discriminant (Old_Disc);
5387 -- if this is a named constraint, search by name for the old
5388 -- discriminants constrained by the new one.
5390 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
5392 -- Find new discriminant with that name
5394 New_Disc := First_Discriminant (Derived_Type);
5395 while Present (New_Disc) loop
5397 Chars (New_Disc) = Chars (Expression (D_Constraint));
5398 Next_Discriminant (New_Disc);
5401 if Present (New_Disc) then
5403 -- Verify that new discriminant renames some discriminant
5404 -- of the parent type, and associate the new discriminant
5405 -- with one or more old ones that it renames.
5411 Selector := First (Selector_Names (D_Constraint));
5412 while Present (Selector) loop
5413 Old_Disc := First_Discriminant (Parent_Type);
5414 while Present (Old_Disc) loop
5415 exit when Chars (Old_Disc) = Chars (Selector);
5416 Next_Discriminant (Old_Disc);
5419 if Present (Old_Disc) then
5420 Set_Corresponding_Discriminant
5421 (New_Disc, Old_Disc);
5430 Next (D_Constraint);
5433 New_Disc := First_Discriminant (Derived_Type);
5434 while Present (New_Disc) loop
5435 if No (Corresponding_Discriminant (New_Disc)) then
5437 ("new discriminant& must constrain old one", N, New_Disc);
5440 Subtypes_Statically_Compatible
5442 Etype (Corresponding_Discriminant (New_Disc)))
5445 ("& not statically compatible with parent discriminant",
5449 Next_Discriminant (New_Disc);
5453 elsif Present (Discriminant_Specifications (N)) then
5455 ("missing discriminant constraint in untagged derivation", N);
5458 -- The entity chain of the derived type includes the new discriminants
5459 -- but shares operations with the parent.
5461 if Present (Discriminant_Specifications (N)) then
5462 Old_Disc := First_Discriminant (Parent_Type);
5463 while Present (Old_Disc) loop
5464 if No (Next_Entity (Old_Disc))
5465 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
5468 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
5472 Next_Discriminant (Old_Disc);
5476 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
5477 if Has_Discriminants (Parent_Type) then
5478 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5479 Set_Discriminant_Constraint (
5480 Derived_Type, Discriminant_Constraint (Parent_Type));
5484 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
5486 Set_Has_Completion (Derived_Type);
5488 if Corr_Decl_Needed then
5489 Set_Stored_Constraint (Derived_Type, New_Constraint);
5490 Insert_After (N, Corr_Decl);
5491 Analyze (Corr_Decl);
5492 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
5494 end Build_Derived_Concurrent_Type;
5496 ------------------------------------
5497 -- Build_Derived_Enumeration_Type --
5498 ------------------------------------
5500 procedure Build_Derived_Enumeration_Type
5502 Parent_Type : Entity_Id;
5503 Derived_Type : Entity_Id)
5505 Loc : constant Source_Ptr := Sloc (N);
5506 Def : constant Node_Id := Type_Definition (N);
5507 Indic : constant Node_Id := Subtype_Indication (Def);
5508 Implicit_Base : Entity_Id;
5509 Literal : Entity_Id;
5510 New_Lit : Entity_Id;
5511 Literals_List : List_Id;
5512 Type_Decl : Node_Id;
5514 Rang_Expr : Node_Id;
5517 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
5518 -- not have explicit literals lists we need to process types derived
5519 -- from them specially. This is handled by Derived_Standard_Character.
5520 -- If the parent type is a generic type, there are no literals either,
5521 -- and we construct the same skeletal representation as for the generic
5524 if Is_Standard_Character_Type (Parent_Type) then
5525 Derived_Standard_Character (N, Parent_Type, Derived_Type);
5527 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
5533 if Nkind (Indic) /= N_Subtype_Indication then
5535 Make_Attribute_Reference (Loc,
5536 Attribute_Name => Name_First,
5537 Prefix => New_Reference_To (Derived_Type, Loc));
5538 Set_Etype (Lo, Derived_Type);
5541 Make_Attribute_Reference (Loc,
5542 Attribute_Name => Name_Last,
5543 Prefix => New_Reference_To (Derived_Type, Loc));
5544 Set_Etype (Hi, Derived_Type);
5546 Set_Scalar_Range (Derived_Type,
5552 -- Analyze subtype indication and verify compatibility
5553 -- with parent type.
5555 if Base_Type (Process_Subtype (Indic, N)) /=
5556 Base_Type (Parent_Type)
5559 ("illegal constraint for formal discrete type", N);
5565 -- If a constraint is present, analyze the bounds to catch
5566 -- premature usage of the derived literals.
5568 if Nkind (Indic) = N_Subtype_Indication
5569 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
5571 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
5572 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
5575 -- Introduce an implicit base type for the derived type even if there
5576 -- is no constraint attached to it, since this seems closer to the
5577 -- Ada semantics. Build a full type declaration tree for the derived
5578 -- type using the implicit base type as the defining identifier. The
5579 -- build a subtype declaration tree which applies the constraint (if
5580 -- any) have it replace the derived type declaration.
5582 Literal := First_Literal (Parent_Type);
5583 Literals_List := New_List;
5584 while Present (Literal)
5585 and then Ekind (Literal) = E_Enumeration_Literal
5587 -- Literals of the derived type have the same representation as
5588 -- those of the parent type, but this representation can be
5589 -- overridden by an explicit representation clause. Indicate
5590 -- that there is no explicit representation given yet. These
5591 -- derived literals are implicit operations of the new type,
5592 -- and can be overridden by explicit ones.
5594 if Nkind (Literal) = N_Defining_Character_Literal then
5596 Make_Defining_Character_Literal (Loc, Chars (Literal));
5598 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
5601 Set_Ekind (New_Lit, E_Enumeration_Literal);
5602 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
5603 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
5604 Set_Enumeration_Rep_Expr (New_Lit, Empty);
5605 Set_Alias (New_Lit, Literal);
5606 Set_Is_Known_Valid (New_Lit, True);
5608 Append (New_Lit, Literals_List);
5609 Next_Literal (Literal);
5613 Make_Defining_Identifier (Sloc (Derived_Type),
5614 Chars => New_External_Name (Chars (Derived_Type), 'B'));
5616 -- Indicate the proper nature of the derived type. This must be done
5617 -- before analysis of the literals, to recognize cases when a literal
5618 -- may be hidden by a previous explicit function definition (cf.
5621 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
5622 Set_Etype (Derived_Type, Implicit_Base);
5625 Make_Full_Type_Declaration (Loc,
5626 Defining_Identifier => Implicit_Base,
5627 Discriminant_Specifications => No_List,
5629 Make_Enumeration_Type_Definition (Loc, Literals_List));
5631 Mark_Rewrite_Insertion (Type_Decl);
5632 Insert_Before (N, Type_Decl);
5633 Analyze (Type_Decl);
5635 -- After the implicit base is analyzed its Etype needs to be changed
5636 -- to reflect the fact that it is derived from the parent type which
5637 -- was ignored during analysis. We also set the size at this point.
5639 Set_Etype (Implicit_Base, Parent_Type);
5641 Set_Size_Info (Implicit_Base, Parent_Type);
5642 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
5643 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
5645 -- Copy other flags from parent type
5647 Set_Has_Non_Standard_Rep
5648 (Implicit_Base, Has_Non_Standard_Rep
5650 Set_Has_Pragma_Ordered
5651 (Implicit_Base, Has_Pragma_Ordered
5653 Set_Has_Delayed_Freeze (Implicit_Base);
5655 -- Process the subtype indication including a validation check on the
5656 -- constraint, if any. If a constraint is given, its bounds must be
5657 -- implicitly converted to the new type.
5659 if Nkind (Indic) = N_Subtype_Indication then
5661 R : constant Node_Id :=
5662 Range_Expression (Constraint (Indic));
5665 if Nkind (R) = N_Range then
5666 Hi := Build_Scalar_Bound
5667 (High_Bound (R), Parent_Type, Implicit_Base);
5668 Lo := Build_Scalar_Bound
5669 (Low_Bound (R), Parent_Type, Implicit_Base);
5672 -- Constraint is a Range attribute. Replace with explicit
5673 -- mention of the bounds of the prefix, which must be a
5676 Analyze (Prefix (R));
5678 Convert_To (Implicit_Base,
5679 Make_Attribute_Reference (Loc,
5680 Attribute_Name => Name_Last,
5682 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5685 Convert_To (Implicit_Base,
5686 Make_Attribute_Reference (Loc,
5687 Attribute_Name => Name_First,
5689 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
5696 (Type_High_Bound (Parent_Type),
5697 Parent_Type, Implicit_Base);
5700 (Type_Low_Bound (Parent_Type),
5701 Parent_Type, Implicit_Base);
5709 -- If we constructed a default range for the case where no range
5710 -- was given, then the expressions in the range must not freeze
5711 -- since they do not correspond to expressions in the source.
5713 if Nkind (Indic) /= N_Subtype_Indication then
5714 Set_Must_Not_Freeze (Lo);
5715 Set_Must_Not_Freeze (Hi);
5716 Set_Must_Not_Freeze (Rang_Expr);
5720 Make_Subtype_Declaration (Loc,
5721 Defining_Identifier => Derived_Type,
5722 Subtype_Indication =>
5723 Make_Subtype_Indication (Loc,
5724 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
5726 Make_Range_Constraint (Loc,
5727 Range_Expression => Rang_Expr))));
5731 -- If pragma Discard_Names applies on the first subtype of the parent
5732 -- type, then it must be applied on this subtype as well.
5734 if Einfo.Discard_Names (First_Subtype (Parent_Type)) then
5735 Set_Discard_Names (Derived_Type);
5738 -- Apply a range check. Since this range expression doesn't have an
5739 -- Etype, we have to specifically pass the Source_Typ parameter. Is
5742 if Nkind (Indic) = N_Subtype_Indication then
5743 Apply_Range_Check (Range_Expression (Constraint (Indic)),
5745 Source_Typ => Entity (Subtype_Mark (Indic)));
5748 end Build_Derived_Enumeration_Type;
5750 --------------------------------
5751 -- Build_Derived_Numeric_Type --
5752 --------------------------------
5754 procedure Build_Derived_Numeric_Type
5756 Parent_Type : Entity_Id;
5757 Derived_Type : Entity_Id)
5759 Loc : constant Source_Ptr := Sloc (N);
5760 Tdef : constant Node_Id := Type_Definition (N);
5761 Indic : constant Node_Id := Subtype_Indication (Tdef);
5762 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
5763 No_Constraint : constant Boolean := Nkind (Indic) /=
5764 N_Subtype_Indication;
5765 Implicit_Base : Entity_Id;
5771 -- Process the subtype indication including a validation check on
5772 -- the constraint if any.
5774 Discard_Node (Process_Subtype (Indic, N));
5776 -- Introduce an implicit base type for the derived type even if there
5777 -- is no constraint attached to it, since this seems closer to the Ada
5781 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
5783 Set_Etype (Implicit_Base, Parent_Base);
5784 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
5785 Set_Size_Info (Implicit_Base, Parent_Base);
5786 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
5787 Set_Parent (Implicit_Base, Parent (Derived_Type));
5788 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
5790 -- Set RM Size for discrete type or decimal fixed-point type
5791 -- Ordinary fixed-point is excluded, why???
5793 if Is_Discrete_Type (Parent_Base)
5794 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
5796 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
5799 Set_Has_Delayed_Freeze (Implicit_Base);
5801 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
5802 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
5804 Set_Scalar_Range (Implicit_Base,
5809 if Has_Infinities (Parent_Base) then
5810 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
5813 -- The Derived_Type, which is the entity of the declaration, is a
5814 -- subtype of the implicit base. Its Ekind is a subtype, even in the
5815 -- absence of an explicit constraint.
5817 Set_Etype (Derived_Type, Implicit_Base);
5819 -- If we did not have a constraint, then the Ekind is set from the
5820 -- parent type (otherwise Process_Subtype has set the bounds)
5822 if No_Constraint then
5823 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
5826 -- If we did not have a range constraint, then set the range from the
5827 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
5830 or else not Has_Range_Constraint (Indic)
5832 Set_Scalar_Range (Derived_Type,
5834 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
5835 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
5836 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
5838 if Has_Infinities (Parent_Type) then
5839 Set_Includes_Infinities (Scalar_Range (Derived_Type));
5842 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
5845 Set_Is_Descendent_Of_Address (Derived_Type,
5846 Is_Descendent_Of_Address (Parent_Type));
5847 Set_Is_Descendent_Of_Address (Implicit_Base,
5848 Is_Descendent_Of_Address (Parent_Type));
5850 -- Set remaining type-specific fields, depending on numeric type
5852 if Is_Modular_Integer_Type (Parent_Type) then
5853 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
5855 Set_Non_Binary_Modulus
5856 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
5859 (Implicit_Base, Is_Known_Valid (Parent_Base));
5861 elsif Is_Floating_Point_Type (Parent_Type) then
5863 -- Digits of base type is always copied from the digits value of
5864 -- the parent base type, but the digits of the derived type will
5865 -- already have been set if there was a constraint present.
5867 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5868 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
5870 if No_Constraint then
5871 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
5874 elsif Is_Fixed_Point_Type (Parent_Type) then
5876 -- Small of base type and derived type are always copied from the
5877 -- parent base type, since smalls never change. The delta of the
5878 -- base type is also copied from the parent base type. However the
5879 -- delta of the derived type will have been set already if a
5880 -- constraint was present.
5882 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
5883 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
5884 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
5886 if No_Constraint then
5887 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
5890 -- The scale and machine radix in the decimal case are always
5891 -- copied from the parent base type.
5893 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
5894 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
5895 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
5897 Set_Machine_Radix_10
5898 (Derived_Type, Machine_Radix_10 (Parent_Base));
5899 Set_Machine_Radix_10
5900 (Implicit_Base, Machine_Radix_10 (Parent_Base));
5902 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
5904 if No_Constraint then
5905 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
5908 -- the analysis of the subtype_indication sets the
5909 -- digits value of the derived type.
5916 -- The type of the bounds is that of the parent type, and they
5917 -- must be converted to the derived type.
5919 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
5921 -- The implicit_base should be frozen when the derived type is frozen,
5922 -- but note that it is used in the conversions of the bounds. For fixed
5923 -- types we delay the determination of the bounds until the proper
5924 -- freezing point. For other numeric types this is rejected by GCC, for
5925 -- reasons that are currently unclear (???), so we choose to freeze the
5926 -- implicit base now. In the case of integers and floating point types
5927 -- this is harmless because subsequent representation clauses cannot
5928 -- affect anything, but it is still baffling that we cannot use the
5929 -- same mechanism for all derived numeric types.
5931 -- There is a further complication: actually *some* representation
5932 -- clauses can affect the implicit base type. Namely, attribute
5933 -- definition clauses for stream-oriented attributes need to set the
5934 -- corresponding TSS entries on the base type, and this normally cannot
5935 -- be done after the base type is frozen, so the circuitry in
5936 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility and
5937 -- not use Set_TSS in this case.
5939 if Is_Fixed_Point_Type (Parent_Type) then
5940 Conditional_Delay (Implicit_Base, Parent_Type);
5942 Freeze_Before (N, Implicit_Base);
5944 end Build_Derived_Numeric_Type;
5946 --------------------------------
5947 -- Build_Derived_Private_Type --
5948 --------------------------------
5950 procedure Build_Derived_Private_Type
5952 Parent_Type : Entity_Id;
5953 Derived_Type : Entity_Id;
5954 Is_Completion : Boolean;
5955 Derive_Subps : Boolean := True)
5957 Loc : constant Source_Ptr := Sloc (N);
5958 Der_Base : Entity_Id;
5960 Full_Decl : Node_Id := Empty;
5961 Full_Der : Entity_Id;
5963 Last_Discr : Entity_Id;
5964 Par_Scope : constant Entity_Id := Scope (Base_Type (Parent_Type));
5965 Swapped : Boolean := False;
5967 procedure Copy_And_Build;
5968 -- Copy derived type declaration, replace parent with its full view,
5969 -- and analyze new declaration.
5971 --------------------
5972 -- Copy_And_Build --
5973 --------------------
5975 procedure Copy_And_Build is
5979 if Ekind (Parent_Type) in Record_Kind
5981 (Ekind (Parent_Type) in Enumeration_Kind
5982 and then not Is_Standard_Character_Type (Parent_Type)
5983 and then not Is_Generic_Type (Root_Type (Parent_Type)))
5985 Full_N := New_Copy_Tree (N);
5986 Insert_After (N, Full_N);
5987 Build_Derived_Type (
5988 Full_N, Parent_Type, Full_Der, True, Derive_Subps => False);
5991 Build_Derived_Type (
5992 N, Parent_Type, Full_Der, True, Derive_Subps => False);
5996 -- Start of processing for Build_Derived_Private_Type
5999 if Is_Tagged_Type (Parent_Type) then
6000 Full_P := Full_View (Parent_Type);
6002 -- A type extension of a type with unknown discriminants is an
6003 -- indefinite type that the back-end cannot handle directly.
6004 -- We treat it as a private type, and build a completion that is
6005 -- derived from the full view of the parent, and hopefully has
6006 -- known discriminants.
6008 -- If the full view of the parent type has an underlying record view,
6009 -- use it to generate the underlying record view of this derived type
6010 -- (required for chains of derivations with unknown discriminants).
6012 -- Minor optimization: we avoid the generation of useless underlying
6013 -- record view entities if the private type declaration has unknown
6014 -- discriminants but its corresponding full view has no
6017 if Has_Unknown_Discriminants (Parent_Type)
6018 and then Present (Full_P)
6019 and then (Has_Discriminants (Full_P)
6020 or else Present (Underlying_Record_View (Full_P)))
6021 and then not In_Open_Scopes (Par_Scope)
6022 and then Expander_Active
6025 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
6026 New_Ext : constant Node_Id :=
6028 (Record_Extension_Part (Type_Definition (N)));
6032 Build_Derived_Record_Type
6033 (N, Parent_Type, Derived_Type, Derive_Subps);
6035 -- Build anonymous completion, as a derivation from the full
6036 -- view of the parent. This is not a completion in the usual
6037 -- sense, because the current type is not private.
6040 Make_Full_Type_Declaration (Loc,
6041 Defining_Identifier => Full_Der,
6043 Make_Derived_Type_Definition (Loc,
6044 Subtype_Indication =>
6046 (Subtype_Indication (Type_Definition (N))),
6047 Record_Extension_Part => New_Ext));
6049 -- If the parent type has an underlying record view, use it
6050 -- here to build the new underlying record view.
6052 if Present (Underlying_Record_View (Full_P)) then
6054 (Nkind (Subtype_Indication (Type_Definition (Decl)))
6056 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
6057 Underlying_Record_View (Full_P));
6060 Install_Private_Declarations (Par_Scope);
6061 Install_Visible_Declarations (Par_Scope);
6062 Insert_Before (N, Decl);
6064 -- Mark entity as an underlying record view before analysis,
6065 -- to avoid generating the list of its primitive operations
6066 -- (which is not really required for this entity) and thus
6067 -- prevent spurious errors associated with missing overriding
6068 -- of abstract primitives (overridden only for Derived_Type).
6070 Set_Ekind (Full_Der, E_Record_Type);
6071 Set_Is_Underlying_Record_View (Full_Der);
6075 pragma Assert (Has_Discriminants (Full_Der)
6076 and then not Has_Unknown_Discriminants (Full_Der));
6078 Uninstall_Declarations (Par_Scope);
6080 -- Freeze the underlying record view, to prevent generation of
6081 -- useless dispatching information, which is simply shared with
6082 -- the real derived type.
6084 Set_Is_Frozen (Full_Der);
6086 -- Set up links between real entity and underlying record view
6088 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
6089 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
6092 -- If discriminants are known, build derived record
6095 Build_Derived_Record_Type
6096 (N, Parent_Type, Derived_Type, Derive_Subps);
6101 elsif Has_Discriminants (Parent_Type) then
6102 if Present (Full_View (Parent_Type)) then
6103 if not Is_Completion then
6105 -- Copy declaration for subsequent analysis, to provide a
6106 -- completion for what is a private declaration. Indicate that
6107 -- the full type is internally generated.
6109 Full_Decl := New_Copy_Tree (N);
6110 Full_Der := New_Copy (Derived_Type);
6111 Set_Comes_From_Source (Full_Decl, False);
6112 Set_Comes_From_Source (Full_Der, False);
6113 Set_Parent (Full_Der, Full_Decl);
6115 Insert_After (N, Full_Decl);
6118 -- If this is a completion, the full view being built is itself
6119 -- private. We build a subtype of the parent with the same
6120 -- constraints as this full view, to convey to the back end the
6121 -- constrained components and the size of this subtype. If the
6122 -- parent is constrained, its full view can serve as the
6123 -- underlying full view of the derived type.
6125 if No (Discriminant_Specifications (N)) then
6126 if Nkind (Subtype_Indication (Type_Definition (N))) =
6127 N_Subtype_Indication
6129 Build_Underlying_Full_View (N, Derived_Type, Parent_Type);
6131 elsif Is_Constrained (Full_View (Parent_Type)) then
6132 Set_Underlying_Full_View
6133 (Derived_Type, Full_View (Parent_Type));
6137 -- If there are new discriminants, the parent subtype is
6138 -- constrained by them, but it is not clear how to build
6139 -- the Underlying_Full_View in this case???
6146 -- Build partial view of derived type from partial view of parent
6148 Build_Derived_Record_Type
6149 (N, Parent_Type, Derived_Type, Derive_Subps);
6151 if Present (Full_View (Parent_Type)) and then not Is_Completion then
6152 if not In_Open_Scopes (Par_Scope)
6153 or else not In_Same_Source_Unit (N, Parent_Type)
6155 -- Swap partial and full views temporarily
6157 Install_Private_Declarations (Par_Scope);
6158 Install_Visible_Declarations (Par_Scope);
6162 -- Build full view of derived type from full view of parent which
6163 -- is now installed. Subprograms have been derived on the partial
6164 -- view, the completion does not derive them anew.
6166 if not Is_Tagged_Type (Parent_Type) then
6168 -- If the parent is itself derived from another private type,
6169 -- installing the private declarations has not affected its
6170 -- privacy status, so use its own full view explicitly.
6172 if Is_Private_Type (Parent_Type) then
6173 Build_Derived_Record_Type
6174 (Full_Decl, Full_View (Parent_Type), Full_Der, False);
6176 Build_Derived_Record_Type
6177 (Full_Decl, Parent_Type, Full_Der, False);
6181 -- If full view of parent is tagged, the completion inherits
6182 -- the proper primitive operations.
6184 Set_Defining_Identifier (Full_Decl, Full_Der);
6185 Build_Derived_Record_Type
6186 (Full_Decl, Parent_Type, Full_Der, Derive_Subps);
6189 -- The full declaration has been introduced into the tree and
6190 -- processed in the step above. It should not be analyzed again
6191 -- (when encountered later in the current list of declarations)
6192 -- to prevent spurious name conflicts. The full entity remains
6195 Set_Analyzed (Full_Decl);
6198 Uninstall_Declarations (Par_Scope);
6200 if In_Open_Scopes (Par_Scope) then
6201 Install_Visible_Declarations (Par_Scope);
6205 Der_Base := Base_Type (Derived_Type);
6206 Set_Full_View (Derived_Type, Full_Der);
6207 Set_Full_View (Der_Base, Base_Type (Full_Der));
6209 -- Copy the discriminant list from full view to the partial views
6210 -- (base type and its subtype). Gigi requires that the partial and
6211 -- full views have the same discriminants.
6213 -- Note that since the partial view is pointing to discriminants
6214 -- in the full view, their scope will be that of the full view.
6215 -- This might cause some front end problems and need adjustment???
6217 Discr := First_Discriminant (Base_Type (Full_Der));
6218 Set_First_Entity (Der_Base, Discr);
6221 Last_Discr := Discr;
6222 Next_Discriminant (Discr);
6223 exit when No (Discr);
6226 Set_Last_Entity (Der_Base, Last_Discr);
6228 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
6229 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
6230 Set_Stored_Constraint (Full_Der, Stored_Constraint (Derived_Type));
6233 -- If this is a completion, the derived type stays private and
6234 -- there is no need to create a further full view, except in the
6235 -- unusual case when the derivation is nested within a child unit,
6241 elsif Present (Full_View (Parent_Type))
6242 and then Has_Discriminants (Full_View (Parent_Type))
6244 if Has_Unknown_Discriminants (Parent_Type)
6245 and then Nkind (Subtype_Indication (Type_Definition (N))) =
6246 N_Subtype_Indication
6249 ("cannot constrain type with unknown discriminants",
6250 Subtype_Indication (Type_Definition (N)));
6254 -- If full view of parent is a record type, build full view as a
6255 -- derivation from the parent's full view. Partial view remains
6256 -- private. For code generation and linking, the full view must have
6257 -- the same public status as the partial one. This full view is only
6258 -- needed if the parent type is in an enclosing scope, so that the
6259 -- full view may actually become visible, e.g. in a child unit. This
6260 -- is both more efficient, and avoids order of freezing problems with
6261 -- the added entities.
6263 if not Is_Private_Type (Full_View (Parent_Type))
6264 and then (In_Open_Scopes (Scope (Parent_Type)))
6267 Make_Defining_Identifier
6268 (Sloc (Derived_Type), Chars (Derived_Type));
6269 Set_Is_Itype (Full_Der);
6270 Set_Has_Private_Declaration (Full_Der);
6271 Set_Has_Private_Declaration (Derived_Type);
6272 Set_Associated_Node_For_Itype (Full_Der, N);
6273 Set_Parent (Full_Der, Parent (Derived_Type));
6274 Set_Full_View (Derived_Type, Full_Der);
6275 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
6276 Full_P := Full_View (Parent_Type);
6277 Exchange_Declarations (Parent_Type);
6279 Exchange_Declarations (Full_P);
6282 Build_Derived_Record_Type
6283 (N, Full_View (Parent_Type), Derived_Type,
6284 Derive_Subps => False);
6287 -- In any case, the primitive operations are inherited from the
6288 -- parent type, not from the internal full view.
6290 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
6292 if Derive_Subps then
6293 Derive_Subprograms (Parent_Type, Derived_Type);
6297 -- Untagged type, No discriminants on either view
6299 if Nkind (Subtype_Indication (Type_Definition (N))) =
6300 N_Subtype_Indication
6303 ("illegal constraint on type without discriminants", N);
6306 if Present (Discriminant_Specifications (N))
6307 and then Present (Full_View (Parent_Type))
6308 and then not Is_Tagged_Type (Full_View (Parent_Type))
6310 Error_Msg_N ("cannot add discriminants to untagged type", N);
6313 Set_Stored_Constraint (Derived_Type, No_Elist);
6314 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6315 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
6316 Set_Has_Controlled_Component
6317 (Derived_Type, Has_Controlled_Component
6320 -- Direct controlled types do not inherit Finalize_Storage_Only flag
6322 if not Is_Controlled (Parent_Type) then
6323 Set_Finalize_Storage_Only
6324 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
6327 -- Construct the implicit full view by deriving from full view of the
6328 -- parent type. In order to get proper visibility, we install the
6329 -- parent scope and its declarations.
6331 -- ??? If the parent is untagged private and its completion is
6332 -- tagged, this mechanism will not work because we cannot derive from
6333 -- the tagged full view unless we have an extension.
6335 if Present (Full_View (Parent_Type))
6336 and then not Is_Tagged_Type (Full_View (Parent_Type))
6337 and then not Is_Completion
6340 Make_Defining_Identifier
6341 (Sloc (Derived_Type), Chars (Derived_Type));
6342 Set_Is_Itype (Full_Der);
6343 Set_Has_Private_Declaration (Full_Der);
6344 Set_Has_Private_Declaration (Derived_Type);
6345 Set_Associated_Node_For_Itype (Full_Der, N);
6346 Set_Parent (Full_Der, Parent (Derived_Type));
6347 Set_Full_View (Derived_Type, Full_Der);
6349 if not In_Open_Scopes (Par_Scope) then
6350 Install_Private_Declarations (Par_Scope);
6351 Install_Visible_Declarations (Par_Scope);
6353 Uninstall_Declarations (Par_Scope);
6355 -- If parent scope is open and in another unit, and parent has a
6356 -- completion, then the derivation is taking place in the visible
6357 -- part of a child unit. In that case retrieve the full view of
6358 -- the parent momentarily.
6360 elsif not In_Same_Source_Unit (N, Parent_Type) then
6361 Full_P := Full_View (Parent_Type);
6362 Exchange_Declarations (Parent_Type);
6364 Exchange_Declarations (Full_P);
6366 -- Otherwise it is a local derivation
6372 Set_Scope (Full_Der, Current_Scope);
6373 Set_Is_First_Subtype (Full_Der,
6374 Is_First_Subtype (Derived_Type));
6375 Set_Has_Size_Clause (Full_Der, False);
6376 Set_Has_Alignment_Clause (Full_Der, False);
6377 Set_Next_Entity (Full_Der, Empty);
6378 Set_Has_Delayed_Freeze (Full_Der);
6379 Set_Is_Frozen (Full_Der, False);
6380 Set_Freeze_Node (Full_Der, Empty);
6381 Set_Depends_On_Private (Full_Der,
6382 Has_Private_Component (Full_Der));
6383 Set_Public_Status (Full_Der);
6387 Set_Has_Unknown_Discriminants (Derived_Type,
6388 Has_Unknown_Discriminants (Parent_Type));
6390 if Is_Private_Type (Derived_Type) then
6391 Set_Private_Dependents (Derived_Type, New_Elmt_List);
6394 if Is_Private_Type (Parent_Type)
6395 and then Base_Type (Parent_Type) = Parent_Type
6396 and then In_Open_Scopes (Scope (Parent_Type))
6398 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
6400 if Is_Child_Unit (Scope (Current_Scope))
6401 and then Is_Completion
6402 and then In_Private_Part (Current_Scope)
6403 and then Scope (Parent_Type) /= Current_Scope
6405 -- This is the unusual case where a type completed by a private
6406 -- derivation occurs within a package nested in a child unit, and
6407 -- the parent is declared in an ancestor. In this case, the full
6408 -- view of the parent type will become visible in the body of
6409 -- the enclosing child, and only then will the current type be
6410 -- possibly non-private. We build a underlying full view that
6411 -- will be installed when the enclosing child body is compiled.
6414 Make_Defining_Identifier
6415 (Sloc (Derived_Type), Chars (Derived_Type));
6416 Set_Is_Itype (Full_Der);
6417 Build_Itype_Reference (Full_Der, N);
6419 -- The full view will be used to swap entities on entry/exit to
6420 -- the body, and must appear in the entity list for the package.
6422 Append_Entity (Full_Der, Scope (Derived_Type));
6423 Set_Has_Private_Declaration (Full_Der);
6424 Set_Has_Private_Declaration (Derived_Type);
6425 Set_Associated_Node_For_Itype (Full_Der, N);
6426 Set_Parent (Full_Der, Parent (Derived_Type));
6427 Full_P := Full_View (Parent_Type);
6428 Exchange_Declarations (Parent_Type);
6430 Exchange_Declarations (Full_P);
6431 Set_Underlying_Full_View (Derived_Type, Full_Der);
6434 end Build_Derived_Private_Type;
6436 -------------------------------
6437 -- Build_Derived_Record_Type --
6438 -------------------------------
6442 -- Ideally we would like to use the same model of type derivation for
6443 -- tagged and untagged record types. Unfortunately this is not quite
6444 -- possible because the semantics of representation clauses is different
6445 -- for tagged and untagged records under inheritance. Consider the
6448 -- type R (...) is [tagged] record ... end record;
6449 -- type T (...) is new R (...) [with ...];
6451 -- The representation clauses for T can specify a completely different
6452 -- record layout from R's. Hence the same component can be placed in two
6453 -- very different positions in objects of type T and R. If R and T are
6454 -- tagged types, representation clauses for T can only specify the layout
6455 -- of non inherited components, thus components that are common in R and T
6456 -- have the same position in objects of type R and T.
6458 -- This has two implications. The first is that the entire tree for R's
6459 -- declaration needs to be copied for T in the untagged case, so that T
6460 -- can be viewed as a record type of its own with its own representation
6461 -- clauses. The second implication is the way we handle discriminants.
6462 -- Specifically, in the untagged case we need a way to communicate to Gigi
6463 -- what are the real discriminants in the record, while for the semantics
6464 -- we need to consider those introduced by the user to rename the
6465 -- discriminants in the parent type. This is handled by introducing the
6466 -- notion of stored discriminants. See below for more.
6468 -- Fortunately the way regular components are inherited can be handled in
6469 -- the same way in tagged and untagged types.
6471 -- To complicate things a bit more the private view of a private extension
6472 -- cannot be handled in the same way as the full view (for one thing the
6473 -- semantic rules are somewhat different). We will explain what differs
6476 -- 2. DISCRIMINANTS UNDER INHERITANCE
6478 -- The semantic rules governing the discriminants of derived types are
6481 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
6482 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
6484 -- If parent type has discriminants, then the discriminants that are
6485 -- declared in the derived type are [3.4 (11)]:
6487 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
6490 -- o Otherwise, each discriminant of the parent type (implicitly declared
6491 -- in the same order with the same specifications). In this case, the
6492 -- discriminants are said to be "inherited", or if unknown in the parent
6493 -- are also unknown in the derived type.
6495 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
6497 -- o The parent subtype shall be constrained;
6499 -- o If the parent type is not a tagged type, then each discriminant of
6500 -- the derived type shall be used in the constraint defining a parent
6501 -- subtype. [Implementation note: This ensures that the new discriminant
6502 -- can share storage with an existing discriminant.]
6504 -- For the derived type each discriminant of the parent type is either
6505 -- inherited, constrained to equal some new discriminant of the derived
6506 -- type, or constrained to the value of an expression.
6508 -- When inherited or constrained to equal some new discriminant, the
6509 -- parent discriminant and the discriminant of the derived type are said
6512 -- If a discriminant of the parent type is constrained to a specific value
6513 -- in the derived type definition, then the discriminant is said to be
6514 -- "specified" by that derived type definition.
6516 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
6518 -- We have spoken about stored discriminants in point 1 (introduction)
6519 -- above. There are two sort of stored discriminants: implicit and
6520 -- explicit. As long as the derived type inherits the same discriminants as
6521 -- the root record type, stored discriminants are the same as regular
6522 -- discriminants, and are said to be implicit. However, if any discriminant
6523 -- in the root type was renamed in the derived type, then the derived
6524 -- type will contain explicit stored discriminants. Explicit stored
6525 -- discriminants are discriminants in addition to the semantically visible
6526 -- discriminants defined for the derived type. Stored discriminants are
6527 -- used by Gigi to figure out what are the physical discriminants in
6528 -- objects of the derived type (see precise definition in einfo.ads).
6529 -- As an example, consider the following:
6531 -- type R (D1, D2, D3 : Int) is record ... end record;
6532 -- type T1 is new R;
6533 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
6534 -- type T3 is new T2;
6535 -- type T4 (Y : Int) is new T3 (Y, 99);
6537 -- The following table summarizes the discriminants and stored
6538 -- discriminants in R and T1 through T4.
6540 -- Type Discrim Stored Discrim Comment
6541 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
6542 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
6543 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
6544 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
6545 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
6547 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
6548 -- find the corresponding discriminant in the parent type, while
6549 -- Original_Record_Component (abbreviated ORC below), the actual physical
6550 -- component that is renamed. Finally the field Is_Completely_Hidden
6551 -- (abbreviated ICH below) is set for all explicit stored discriminants
6552 -- (see einfo.ads for more info). For the above example this gives:
6554 -- Discrim CD ORC ICH
6555 -- ^^^^^^^ ^^ ^^^ ^^^
6556 -- D1 in R empty itself no
6557 -- D2 in R empty itself no
6558 -- D3 in R empty itself no
6560 -- D1 in T1 D1 in R itself no
6561 -- D2 in T1 D2 in R itself no
6562 -- D3 in T1 D3 in R itself no
6564 -- X1 in T2 D3 in T1 D3 in T2 no
6565 -- X2 in T2 D1 in T1 D1 in T2 no
6566 -- D1 in T2 empty itself yes
6567 -- D2 in T2 empty itself yes
6568 -- D3 in T2 empty itself yes
6570 -- X1 in T3 X1 in T2 D3 in T3 no
6571 -- X2 in T3 X2 in T2 D1 in T3 no
6572 -- D1 in T3 empty itself yes
6573 -- D2 in T3 empty itself yes
6574 -- D3 in T3 empty itself yes
6576 -- Y in T4 X1 in T3 D3 in T3 no
6577 -- D1 in T3 empty itself yes
6578 -- D2 in T3 empty itself yes
6579 -- D3 in T3 empty itself yes
6581 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
6583 -- Type derivation for tagged types is fairly straightforward. If no
6584 -- discriminants are specified by the derived type, these are inherited
6585 -- from the parent. No explicit stored discriminants are ever necessary.
6586 -- The only manipulation that is done to the tree is that of adding a
6587 -- _parent field with parent type and constrained to the same constraint
6588 -- specified for the parent in the derived type definition. For instance:
6590 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
6591 -- type T1 is new R with null record;
6592 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
6594 -- are changed into:
6596 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
6597 -- _parent : R (D1, D2, D3);
6600 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
6601 -- _parent : T1 (X2, 88, X1);
6604 -- The discriminants actually present in R, T1 and T2 as well as their CD,
6605 -- ORC and ICH fields are:
6607 -- Discrim CD ORC ICH
6608 -- ^^^^^^^ ^^ ^^^ ^^^
6609 -- D1 in R empty itself no
6610 -- D2 in R empty itself no
6611 -- D3 in R empty itself no
6613 -- D1 in T1 D1 in R D1 in R no
6614 -- D2 in T1 D2 in R D2 in R no
6615 -- D3 in T1 D3 in R D3 in R no
6617 -- X1 in T2 D3 in T1 D3 in R no
6618 -- X2 in T2 D1 in T1 D1 in R no
6620 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
6622 -- Regardless of whether we dealing with a tagged or untagged type
6623 -- we will transform all derived type declarations of the form
6625 -- type T is new R (...) [with ...];
6627 -- subtype S is R (...);
6628 -- type T is new S [with ...];
6630 -- type BT is new R [with ...];
6631 -- subtype T is BT (...);
6633 -- That is, the base derived type is constrained only if it has no
6634 -- discriminants. The reason for doing this is that GNAT's semantic model
6635 -- assumes that a base type with discriminants is unconstrained.
6637 -- Note that, strictly speaking, the above transformation is not always
6638 -- correct. Consider for instance the following excerpt from ACVC b34011a:
6640 -- procedure B34011A is
6641 -- type REC (D : integer := 0) is record
6646 -- type T6 is new Rec;
6647 -- function F return T6;
6652 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
6655 -- The definition of Q6.U is illegal. However transforming Q6.U into
6657 -- type BaseU is new T6;
6658 -- subtype U is BaseU (Q6.F.I)
6660 -- turns U into a legal subtype, which is incorrect. To avoid this problem
6661 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
6662 -- the transformation described above.
6664 -- There is another instance where the above transformation is incorrect.
6668 -- type Base (D : Integer) is tagged null record;
6669 -- procedure P (X : Base);
6671 -- type Der is new Base (2) with null record;
6672 -- procedure P (X : Der);
6675 -- Then the above transformation turns this into
6677 -- type Der_Base is new Base with null record;
6678 -- -- procedure P (X : Base) is implicitly inherited here
6679 -- -- as procedure P (X : Der_Base).
6681 -- subtype Der is Der_Base (2);
6682 -- procedure P (X : Der);
6683 -- -- The overriding of P (X : Der_Base) is illegal since we
6684 -- -- have a parameter conformance problem.
6686 -- To get around this problem, after having semantically processed Der_Base
6687 -- and the rewritten subtype declaration for Der, we copy Der_Base field
6688 -- Discriminant_Constraint from Der so that when parameter conformance is
6689 -- checked when P is overridden, no semantic errors are flagged.
6691 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
6693 -- Regardless of whether we are dealing with a tagged or untagged type
6694 -- we will transform all derived type declarations of the form
6696 -- type R (D1, .., Dn : ...) is [tagged] record ...;
6697 -- type T is new R [with ...];
6699 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
6701 -- The reason for such transformation is that it allows us to implement a
6702 -- very clean form of component inheritance as explained below.
6704 -- Note that this transformation is not achieved by direct tree rewriting
6705 -- and manipulation, but rather by redoing the semantic actions that the
6706 -- above transformation will entail. This is done directly in routine
6707 -- Inherit_Components.
6709 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
6711 -- In both tagged and untagged derived types, regular non discriminant
6712 -- components are inherited in the derived type from the parent type. In
6713 -- the absence of discriminants component, inheritance is straightforward
6714 -- as components can simply be copied from the parent.
6716 -- If the parent has discriminants, inheriting components constrained with
6717 -- these discriminants requires caution. Consider the following example:
6719 -- type R (D1, D2 : Positive) is [tagged] record
6720 -- S : String (D1 .. D2);
6723 -- type T1 is new R [with null record];
6724 -- type T2 (X : positive) is new R (1, X) [with null record];
6726 -- As explained in 6. above, T1 is rewritten as
6727 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
6728 -- which makes the treatment for T1 and T2 identical.
6730 -- What we want when inheriting S, is that references to D1 and D2 in R are
6731 -- replaced with references to their correct constraints, i.e. D1 and D2 in
6732 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
6733 -- with either discriminant references in the derived type or expressions.
6734 -- This replacement is achieved as follows: before inheriting R's
6735 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
6736 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
6737 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
6738 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
6739 -- by String (1 .. X).
6741 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
6743 -- We explain here the rules governing private type extensions relevant to
6744 -- type derivation. These rules are explained on the following example:
6746 -- type D [(...)] is new A [(...)] with private; <-- partial view
6747 -- type D [(...)] is new P [(...)] with null record; <-- full view
6749 -- Type A is called the ancestor subtype of the private extension.
6750 -- Type P is the parent type of the full view of the private extension. It
6751 -- must be A or a type derived from A.
6753 -- The rules concerning the discriminants of private type extensions are
6756 -- o If a private extension inherits known discriminants from the ancestor
6757 -- subtype, then the full view shall also inherit its discriminants from
6758 -- the ancestor subtype and the parent subtype of the full view shall be
6759 -- constrained if and only if the ancestor subtype is constrained.
6761 -- o If a partial view has unknown discriminants, then the full view may
6762 -- define a definite or an indefinite subtype, with or without
6765 -- o If a partial view has neither known nor unknown discriminants, then
6766 -- the full view shall define a definite subtype.
6768 -- o If the ancestor subtype of a private extension has constrained
6769 -- discriminants, then the parent subtype of the full view shall impose a
6770 -- statically matching constraint on those discriminants.
6772 -- This means that only the following forms of private extensions are
6775 -- type D is new A with private; <-- partial view
6776 -- type D is new P with null record; <-- full view
6778 -- If A has no discriminants than P has no discriminants, otherwise P must
6779 -- inherit A's discriminants.
6781 -- type D is new A (...) with private; <-- partial view
6782 -- type D is new P (:::) with null record; <-- full view
6784 -- P must inherit A's discriminants and (...) and (:::) must statically
6787 -- subtype A is R (...);
6788 -- type D is new A with private; <-- partial view
6789 -- type D is new P with null record; <-- full view
6791 -- P must have inherited R's discriminants and must be derived from A or
6792 -- any of its subtypes.
6794 -- type D (..) is new A with private; <-- partial view
6795 -- type D (..) is new P [(:::)] with null record; <-- full view
6797 -- No specific constraints on P's discriminants or constraint (:::).
6798 -- Note that A can be unconstrained, but the parent subtype P must either
6799 -- be constrained or (:::) must be present.
6801 -- type D (..) is new A [(...)] with private; <-- partial view
6802 -- type D (..) is new P [(:::)] with null record; <-- full view
6804 -- P's constraints on A's discriminants must statically match those
6805 -- imposed by (...).
6807 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
6809 -- The full view of a private extension is handled exactly as described
6810 -- above. The model chose for the private view of a private extension is
6811 -- the same for what concerns discriminants (i.e. they receive the same
6812 -- treatment as in the tagged case). However, the private view of the
6813 -- private extension always inherits the components of the parent base,
6814 -- without replacing any discriminant reference. Strictly speaking this is
6815 -- incorrect. However, Gigi never uses this view to generate code so this
6816 -- is a purely semantic issue. In theory, a set of transformations similar
6817 -- to those given in 5. and 6. above could be applied to private views of
6818 -- private extensions to have the same model of component inheritance as
6819 -- for non private extensions. However, this is not done because it would
6820 -- further complicate private type processing. Semantically speaking, this
6821 -- leaves us in an uncomfortable situation. As an example consider:
6824 -- type R (D : integer) is tagged record
6825 -- S : String (1 .. D);
6827 -- procedure P (X : R);
6828 -- type T is new R (1) with private;
6830 -- type T is new R (1) with null record;
6833 -- This is transformed into:
6836 -- type R (D : integer) is tagged record
6837 -- S : String (1 .. D);
6839 -- procedure P (X : R);
6840 -- type T is new R (1) with private;
6842 -- type BaseT is new R with null record;
6843 -- subtype T is BaseT (1);
6846 -- (strictly speaking the above is incorrect Ada)
6848 -- From the semantic standpoint the private view of private extension T
6849 -- should be flagged as constrained since one can clearly have
6853 -- in a unit withing Pack. However, when deriving subprograms for the
6854 -- private view of private extension T, T must be seen as unconstrained
6855 -- since T has discriminants (this is a constraint of the current
6856 -- subprogram derivation model). Thus, when processing the private view of
6857 -- a private extension such as T, we first mark T as unconstrained, we
6858 -- process it, we perform program derivation and just before returning from
6859 -- Build_Derived_Record_Type we mark T as constrained.
6861 -- ??? Are there are other uncomfortable cases that we will have to
6864 -- 10. RECORD_TYPE_WITH_PRIVATE complications
6866 -- Types that are derived from a visible record type and have a private
6867 -- extension present other peculiarities. They behave mostly like private
6868 -- types, but if they have primitive operations defined, these will not
6869 -- have the proper signatures for further inheritance, because other
6870 -- primitive operations will use the implicit base that we define for
6871 -- private derivations below. This affect subprogram inheritance (see
6872 -- Derive_Subprograms for details). We also derive the implicit base from
6873 -- the base type of the full view, so that the implicit base is a record
6874 -- type and not another private type, This avoids infinite loops.
6876 procedure Build_Derived_Record_Type
6878 Parent_Type : Entity_Id;
6879 Derived_Type : Entity_Id;
6880 Derive_Subps : Boolean := True)
6882 Loc : constant Source_Ptr := Sloc (N);
6883 Parent_Base : Entity_Id;
6886 Discrim : Entity_Id;
6887 Last_Discrim : Entity_Id;
6890 Discs : Elist_Id := New_Elmt_List;
6891 -- An empty Discs list means that there were no constraints in the
6892 -- subtype indication or that there was an error processing it.
6894 Assoc_List : Elist_Id;
6895 New_Discrs : Elist_Id;
6896 New_Base : Entity_Id;
6898 New_Indic : Node_Id;
6900 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
6901 Discriminant_Specs : constant Boolean :=
6902 Present (Discriminant_Specifications (N));
6903 Private_Extension : constant Boolean :=
6904 Nkind (N) = N_Private_Extension_Declaration;
6906 Constraint_Present : Boolean;
6907 Inherit_Discrims : Boolean := False;
6908 Save_Etype : Entity_Id;
6909 Save_Discr_Constr : Elist_Id;
6910 Save_Next_Entity : Entity_Id;
6913 if Ekind (Parent_Type) = E_Record_Type_With_Private
6914 and then Present (Full_View (Parent_Type))
6915 and then Has_Discriminants (Parent_Type)
6917 Parent_Base := Base_Type (Full_View (Parent_Type));
6919 Parent_Base := Base_Type (Parent_Type);
6922 -- Before we start the previously documented transformations, here is
6923 -- little fix for size and alignment of tagged types. Normally when we
6924 -- derive type D from type P, we copy the size and alignment of P as the
6925 -- default for D, and in the absence of explicit representation clauses
6926 -- for D, the size and alignment are indeed the same as the parent.
6928 -- But this is wrong for tagged types, since fields may be added, and
6929 -- the default size may need to be larger, and the default alignment may
6930 -- need to be larger.
6932 -- We therefore reset the size and alignment fields in the tagged case.
6933 -- Note that the size and alignment will in any case be at least as
6934 -- large as the parent type (since the derived type has a copy of the
6935 -- parent type in the _parent field)
6937 -- The type is also marked as being tagged here, which is needed when
6938 -- processing components with a self-referential anonymous access type
6939 -- in the call to Check_Anonymous_Access_Components below. Note that
6940 -- this flag is also set later on for completeness.
6943 Set_Is_Tagged_Type (Derived_Type);
6944 Init_Size_Align (Derived_Type);
6947 -- STEP 0a: figure out what kind of derived type declaration we have
6949 if Private_Extension then
6951 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
6954 Type_Def := Type_Definition (N);
6956 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
6957 -- Parent_Base can be a private type or private extension. However,
6958 -- for tagged types with an extension the newly added fields are
6959 -- visible and hence the Derived_Type is always an E_Record_Type.
6960 -- (except that the parent may have its own private fields).
6961 -- For untagged types we preserve the Ekind of the Parent_Base.
6963 if Present (Record_Extension_Part (Type_Def)) then
6964 Set_Ekind (Derived_Type, E_Record_Type);
6966 -- Create internal access types for components with anonymous
6969 if Ada_Version >= Ada_2005 then
6970 Check_Anonymous_Access_Components
6971 (N, Derived_Type, Derived_Type,
6972 Component_List (Record_Extension_Part (Type_Def)));
6976 Set_Ekind (Derived_Type, Ekind (Parent_Base));
6980 -- Indic can either be an N_Identifier if the subtype indication
6981 -- contains no constraint or an N_Subtype_Indication if the subtype
6982 -- indication has a constraint.
6984 Indic := Subtype_Indication (Type_Def);
6985 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
6987 -- Check that the type has visible discriminants. The type may be
6988 -- a private type with unknown discriminants whose full view has
6989 -- discriminants which are invisible.
6991 if Constraint_Present then
6992 if not Has_Discriminants (Parent_Base)
6994 (Has_Unknown_Discriminants (Parent_Base)
6995 and then Is_Private_Type (Parent_Base))
6998 ("invalid constraint: type has no discriminant",
6999 Constraint (Indic));
7001 Constraint_Present := False;
7002 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7004 elsif Is_Constrained (Parent_Type) then
7006 ("invalid constraint: parent type is already constrained",
7007 Constraint (Indic));
7009 Constraint_Present := False;
7010 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7014 -- STEP 0b: If needed, apply transformation given in point 5. above
7016 if not Private_Extension
7017 and then Has_Discriminants (Parent_Type)
7018 and then not Discriminant_Specs
7019 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
7021 -- First, we must analyze the constraint (see comment in point 5.)
7023 if Constraint_Present then
7024 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
7026 if Has_Discriminants (Derived_Type)
7027 and then Has_Private_Declaration (Derived_Type)
7028 and then Present (Discriminant_Constraint (Derived_Type))
7030 -- Verify that constraints of the full view statically match
7031 -- those given in the partial view.
7037 C1 := First_Elmt (New_Discrs);
7038 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
7039 while Present (C1) and then Present (C2) loop
7040 if Fully_Conformant_Expressions (Node (C1), Node (C2))
7042 (Is_OK_Static_Expression (Node (C1))
7044 Is_OK_Static_Expression (Node (C2))
7046 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
7052 "constraint not conformant to previous declaration",
7063 -- Insert and analyze the declaration for the unconstrained base type
7065 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7068 Make_Full_Type_Declaration (Loc,
7069 Defining_Identifier => New_Base,
7071 Make_Derived_Type_Definition (Loc,
7072 Abstract_Present => Abstract_Present (Type_Def),
7073 Limited_Present => Limited_Present (Type_Def),
7074 Subtype_Indication =>
7075 New_Occurrence_Of (Parent_Base, Loc),
7076 Record_Extension_Part =>
7077 Relocate_Node (Record_Extension_Part (Type_Def)),
7078 Interface_List => Interface_List (Type_Def)));
7080 Set_Parent (New_Decl, Parent (N));
7081 Mark_Rewrite_Insertion (New_Decl);
7082 Insert_Before (N, New_Decl);
7084 -- In the extension case, make sure ancestor is frozen appropriately
7085 -- (see also non-discriminated case below).
7087 if Present (Record_Extension_Part (Type_Def))
7088 or else Is_Interface (Parent_Base)
7090 Freeze_Before (New_Decl, Parent_Type);
7093 -- Note that this call passes False for the Derive_Subps parameter
7094 -- because subprogram derivation is deferred until after creating
7095 -- the subtype (see below).
7098 (New_Decl, Parent_Base, New_Base,
7099 Is_Completion => True, Derive_Subps => False);
7101 -- ??? This needs re-examination to determine whether the
7102 -- above call can simply be replaced by a call to Analyze.
7104 Set_Analyzed (New_Decl);
7106 -- Insert and analyze the declaration for the constrained subtype
7108 if Constraint_Present then
7110 Make_Subtype_Indication (Loc,
7111 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7112 Constraint => Relocate_Node (Constraint (Indic)));
7116 Constr_List : constant List_Id := New_List;
7121 C := First_Elmt (Discriminant_Constraint (Parent_Type));
7122 while Present (C) loop
7125 -- It is safe here to call New_Copy_Tree since
7126 -- Force_Evaluation was called on each constraint in
7127 -- Build_Discriminant_Constraints.
7129 Append (New_Copy_Tree (Expr), To => Constr_List);
7135 Make_Subtype_Indication (Loc,
7136 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7138 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
7143 Make_Subtype_Declaration (Loc,
7144 Defining_Identifier => Derived_Type,
7145 Subtype_Indication => New_Indic));
7149 -- Derivation of subprograms must be delayed until the full subtype
7150 -- has been established to ensure proper overriding of subprograms
7151 -- inherited by full types. If the derivations occurred as part of
7152 -- the call to Build_Derived_Type above, then the check for type
7153 -- conformance would fail because earlier primitive subprograms
7154 -- could still refer to the full type prior the change to the new
7155 -- subtype and hence would not match the new base type created here.
7157 Derive_Subprograms (Parent_Type, Derived_Type);
7159 -- For tagged types the Discriminant_Constraint of the new base itype
7160 -- is inherited from the first subtype so that no subtype conformance
7161 -- problem arise when the first subtype overrides primitive
7162 -- operations inherited by the implicit base type.
7165 Set_Discriminant_Constraint
7166 (New_Base, Discriminant_Constraint (Derived_Type));
7172 -- If we get here Derived_Type will have no discriminants or it will be
7173 -- a discriminated unconstrained base type.
7175 -- STEP 1a: perform preliminary actions/checks for derived tagged types
7179 -- The parent type is frozen for non-private extensions (RM 13.14(7))
7180 -- The declaration of a specific descendant of an interface type
7181 -- freezes the interface type (RM 13.14).
7183 if not Private_Extension or else Is_Interface (Parent_Base) then
7184 Freeze_Before (N, Parent_Type);
7187 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
7188 -- cannot be declared at a deeper level than its parent type is
7189 -- removed. The check on derivation within a generic body is also
7190 -- relaxed, but there's a restriction that a derived tagged type
7191 -- cannot be declared in a generic body if it's derived directly
7192 -- or indirectly from a formal type of that generic.
7194 if Ada_Version >= Ada_2005 then
7195 if Present (Enclosing_Generic_Body (Derived_Type)) then
7197 Ancestor_Type : Entity_Id;
7200 -- Check to see if any ancestor of the derived type is a
7203 Ancestor_Type := Parent_Type;
7204 while not Is_Generic_Type (Ancestor_Type)
7205 and then Etype (Ancestor_Type) /= Ancestor_Type
7207 Ancestor_Type := Etype (Ancestor_Type);
7210 -- If the derived type does have a formal type as an
7211 -- ancestor, then it's an error if the derived type is
7212 -- declared within the body of the generic unit that
7213 -- declares the formal type in its generic formal part. It's
7214 -- sufficient to check whether the ancestor type is declared
7215 -- inside the same generic body as the derived type (such as
7216 -- within a nested generic spec), in which case the
7217 -- derivation is legal. If the formal type is declared
7218 -- outside of that generic body, then it's guaranteed that
7219 -- the derived type is declared within the generic body of
7220 -- the generic unit declaring the formal type.
7222 if Is_Generic_Type (Ancestor_Type)
7223 and then Enclosing_Generic_Body (Ancestor_Type) /=
7224 Enclosing_Generic_Body (Derived_Type)
7227 ("parent type of& must not be descendant of formal type"
7228 & " of an enclosing generic body",
7229 Indic, Derived_Type);
7234 elsif Type_Access_Level (Derived_Type) /=
7235 Type_Access_Level (Parent_Type)
7236 and then not Is_Generic_Type (Derived_Type)
7238 if Is_Controlled (Parent_Type) then
7240 ("controlled type must be declared at the library level",
7244 ("type extension at deeper accessibility level than parent",
7250 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
7254 and then GB /= Enclosing_Generic_Body (Parent_Base)
7257 ("parent type of& must not be outside generic body"
7259 Indic, Derived_Type);
7265 -- Ada 2005 (AI-251)
7267 if Ada_Version >= Ada_2005 and then Is_Tagged then
7269 -- "The declaration of a specific descendant of an interface type
7270 -- freezes the interface type" (RM 13.14).
7275 if Is_Non_Empty_List (Interface_List (Type_Def)) then
7276 Iface := First (Interface_List (Type_Def));
7277 while Present (Iface) loop
7278 Freeze_Before (N, Etype (Iface));
7285 -- STEP 1b : preliminary cleanup of the full view of private types
7287 -- If the type is already marked as having discriminants, then it's the
7288 -- completion of a private type or private extension and we need to
7289 -- retain the discriminants from the partial view if the current
7290 -- declaration has Discriminant_Specifications so that we can verify
7291 -- conformance. However, we must remove any existing components that
7292 -- were inherited from the parent (and attached in Copy_And_Swap)
7293 -- because the full type inherits all appropriate components anyway, and
7294 -- we do not want the partial view's components interfering.
7296 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
7297 Discrim := First_Discriminant (Derived_Type);
7299 Last_Discrim := Discrim;
7300 Next_Discriminant (Discrim);
7301 exit when No (Discrim);
7304 Set_Last_Entity (Derived_Type, Last_Discrim);
7306 -- In all other cases wipe out the list of inherited components (even
7307 -- inherited discriminants), it will be properly rebuilt here.
7310 Set_First_Entity (Derived_Type, Empty);
7311 Set_Last_Entity (Derived_Type, Empty);
7314 -- STEP 1c: Initialize some flags for the Derived_Type
7316 -- The following flags must be initialized here so that
7317 -- Process_Discriminants can check that discriminants of tagged types do
7318 -- not have a default initial value and that access discriminants are
7319 -- only specified for limited records. For completeness, these flags are
7320 -- also initialized along with all the other flags below.
7322 -- AI-419: Limitedness is not inherited from an interface parent, so to
7323 -- be limited in that case the type must be explicitly declared as
7324 -- limited. However, task and protected interfaces are always limited.
7326 if Limited_Present (Type_Def) then
7327 Set_Is_Limited_Record (Derived_Type);
7329 elsif Is_Limited_Record (Parent_Type)
7330 or else (Present (Full_View (Parent_Type))
7331 and then Is_Limited_Record (Full_View (Parent_Type)))
7333 if not Is_Interface (Parent_Type)
7334 or else Is_Synchronized_Interface (Parent_Type)
7335 or else Is_Protected_Interface (Parent_Type)
7336 or else Is_Task_Interface (Parent_Type)
7338 Set_Is_Limited_Record (Derived_Type);
7342 -- STEP 2a: process discriminants of derived type if any
7344 Push_Scope (Derived_Type);
7346 if Discriminant_Specs then
7347 Set_Has_Unknown_Discriminants (Derived_Type, False);
7349 -- The following call initializes fields Has_Discriminants and
7350 -- Discriminant_Constraint, unless we are processing the completion
7351 -- of a private type declaration.
7353 Check_Or_Process_Discriminants (N, Derived_Type);
7355 -- For untagged types, the constraint on the Parent_Type must be
7356 -- present and is used to rename the discriminants.
7358 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
7359 Error_Msg_N ("untagged parent must have discriminants", Indic);
7361 elsif not Is_Tagged and then not Constraint_Present then
7363 ("discriminant constraint needed for derived untagged records",
7366 -- Otherwise the parent subtype must be constrained unless we have a
7367 -- private extension.
7369 elsif not Constraint_Present
7370 and then not Private_Extension
7371 and then not Is_Constrained (Parent_Type)
7374 ("unconstrained type not allowed in this context", Indic);
7376 elsif Constraint_Present then
7377 -- The following call sets the field Corresponding_Discriminant
7378 -- for the discriminants in the Derived_Type.
7380 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
7382 -- For untagged types all new discriminants must rename
7383 -- discriminants in the parent. For private extensions new
7384 -- discriminants cannot rename old ones (implied by [7.3(13)]).
7386 Discrim := First_Discriminant (Derived_Type);
7387 while Present (Discrim) loop
7389 and then No (Corresponding_Discriminant (Discrim))
7392 ("new discriminants must constrain old ones", Discrim);
7394 elsif Private_Extension
7395 and then Present (Corresponding_Discriminant (Discrim))
7398 ("only static constraints allowed for parent"
7399 & " discriminants in the partial view", Indic);
7403 -- If a new discriminant is used in the constraint, then its
7404 -- subtype must be statically compatible with the parent
7405 -- discriminant's subtype (3.7(15)).
7407 if Present (Corresponding_Discriminant (Discrim))
7409 not Subtypes_Statically_Compatible
7411 Etype (Corresponding_Discriminant (Discrim)))
7414 ("subtype must be compatible with parent discriminant",
7418 Next_Discriminant (Discrim);
7421 -- Check whether the constraints of the full view statically
7422 -- match those imposed by the parent subtype [7.3(13)].
7424 if Present (Stored_Constraint (Derived_Type)) then
7429 C1 := First_Elmt (Discs);
7430 C2 := First_Elmt (Stored_Constraint (Derived_Type));
7431 while Present (C1) and then Present (C2) loop
7433 Fully_Conformant_Expressions (Node (C1), Node (C2))
7436 ("not conformant with previous declaration",
7447 -- STEP 2b: No new discriminants, inherit discriminants if any
7450 if Private_Extension then
7451 Set_Has_Unknown_Discriminants
7453 Has_Unknown_Discriminants (Parent_Type)
7454 or else Unknown_Discriminants_Present (N));
7456 -- The partial view of the parent may have unknown discriminants,
7457 -- but if the full view has discriminants and the parent type is
7458 -- in scope they must be inherited.
7460 elsif Has_Unknown_Discriminants (Parent_Type)
7462 (not Has_Discriminants (Parent_Type)
7463 or else not In_Open_Scopes (Scope (Parent_Type)))
7465 Set_Has_Unknown_Discriminants (Derived_Type);
7468 if not Has_Unknown_Discriminants (Derived_Type)
7469 and then not Has_Unknown_Discriminants (Parent_Base)
7470 and then Has_Discriminants (Parent_Type)
7472 Inherit_Discrims := True;
7473 Set_Has_Discriminants
7474 (Derived_Type, True);
7475 Set_Discriminant_Constraint
7476 (Derived_Type, Discriminant_Constraint (Parent_Base));
7479 -- The following test is true for private types (remember
7480 -- transformation 5. is not applied to those) and in an error
7483 if Constraint_Present then
7484 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
7487 -- For now mark a new derived type as constrained only if it has no
7488 -- discriminants. At the end of Build_Derived_Record_Type we properly
7489 -- set this flag in the case of private extensions. See comments in
7490 -- point 9. just before body of Build_Derived_Record_Type.
7494 not (Inherit_Discrims
7495 or else Has_Unknown_Discriminants (Derived_Type)));
7498 -- STEP 3: initialize fields of derived type
7500 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
7501 Set_Stored_Constraint (Derived_Type, No_Elist);
7503 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
7504 -- but cannot be interfaces
7506 if not Private_Extension
7507 and then Ekind (Derived_Type) /= E_Private_Type
7508 and then Ekind (Derived_Type) /= E_Limited_Private_Type
7510 if Interface_Present (Type_Def) then
7511 Analyze_Interface_Declaration (Derived_Type, Type_Def);
7514 Set_Interfaces (Derived_Type, No_Elist);
7517 -- Fields inherited from the Parent_Type
7520 (Derived_Type, Einfo.Discard_Names (Parent_Type));
7521 Set_Has_Specified_Layout
7522 (Derived_Type, Has_Specified_Layout (Parent_Type));
7523 Set_Is_Limited_Composite
7524 (Derived_Type, Is_Limited_Composite (Parent_Type));
7525 Set_Is_Private_Composite
7526 (Derived_Type, Is_Private_Composite (Parent_Type));
7528 -- Fields inherited from the Parent_Base
7530 Set_Has_Controlled_Component
7531 (Derived_Type, Has_Controlled_Component (Parent_Base));
7532 Set_Has_Non_Standard_Rep
7533 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7534 Set_Has_Primitive_Operations
7535 (Derived_Type, Has_Primitive_Operations (Parent_Base));
7537 -- Fields inherited from the Parent_Base in the non-private case
7539 if Ekind (Derived_Type) = E_Record_Type then
7540 Set_Has_Complex_Representation
7541 (Derived_Type, Has_Complex_Representation (Parent_Base));
7544 -- Fields inherited from the Parent_Base for record types
7546 if Is_Record_Type (Derived_Type) then
7548 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7549 -- Parent_Base can be a private type or private extension.
7551 if Present (Full_View (Parent_Base)) then
7552 Set_OK_To_Reorder_Components
7554 OK_To_Reorder_Components (Full_View (Parent_Base)));
7555 Set_Reverse_Bit_Order
7556 (Derived_Type, Reverse_Bit_Order (Full_View (Parent_Base)));
7558 Set_OK_To_Reorder_Components
7559 (Derived_Type, OK_To_Reorder_Components (Parent_Base));
7560 Set_Reverse_Bit_Order
7561 (Derived_Type, Reverse_Bit_Order (Parent_Base));
7565 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7567 if not Is_Controlled (Parent_Type) then
7568 Set_Finalize_Storage_Only
7569 (Derived_Type, Finalize_Storage_Only (Parent_Type));
7572 -- Set fields for private derived types
7574 if Is_Private_Type (Derived_Type) then
7575 Set_Depends_On_Private (Derived_Type, True);
7576 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7578 -- Inherit fields from non private record types. If this is the
7579 -- completion of a derivation from a private type, the parent itself
7580 -- is private, and the attributes come from its full view, which must
7584 if Is_Private_Type (Parent_Base)
7585 and then not Is_Record_Type (Parent_Base)
7587 Set_Component_Alignment
7588 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
7590 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
7592 Set_Component_Alignment
7593 (Derived_Type, Component_Alignment (Parent_Base));
7595 (Derived_Type, C_Pass_By_Copy (Parent_Base));
7599 -- Set fields for tagged types
7602 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
7604 -- All tagged types defined in Ada.Finalization are controlled
7606 if Chars (Scope (Derived_Type)) = Name_Finalization
7607 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
7608 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
7610 Set_Is_Controlled (Derived_Type);
7612 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
7615 -- Minor optimization: there is no need to generate the class-wide
7616 -- entity associated with an underlying record view.
7618 if not Is_Underlying_Record_View (Derived_Type) then
7619 Make_Class_Wide_Type (Derived_Type);
7622 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
7624 if Has_Discriminants (Derived_Type)
7625 and then Constraint_Present
7627 Set_Stored_Constraint
7628 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
7631 if Ada_Version >= Ada_2005 then
7633 Ifaces_List : Elist_Id;
7636 -- Checks rules 3.9.4 (13/2 and 14/2)
7638 if Comes_From_Source (Derived_Type)
7639 and then not Is_Private_Type (Derived_Type)
7640 and then Is_Interface (Parent_Type)
7641 and then not Is_Interface (Derived_Type)
7643 if Is_Task_Interface (Parent_Type) then
7645 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
7648 elsif Is_Protected_Interface (Parent_Type) then
7650 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
7655 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
7657 Check_Interfaces (N, Type_Def);
7659 -- Ada 2005 (AI-251): Collect the list of progenitors that are
7660 -- not already in the parents.
7664 Ifaces_List => Ifaces_List,
7665 Exclude_Parents => True);
7667 Set_Interfaces (Derived_Type, Ifaces_List);
7669 -- If the derived type is the anonymous type created for
7670 -- a declaration whose parent has a constraint, propagate
7671 -- the interface list to the source type. This must be done
7672 -- prior to the completion of the analysis of the source type
7673 -- because the components in the extension may contain current
7674 -- instances whose legality depends on some ancestor.
7676 if Is_Itype (Derived_Type) then
7678 Def : constant Node_Id :=
7679 Associated_Node_For_Itype (Derived_Type);
7682 and then Nkind (Def) = N_Full_Type_Declaration
7685 (Defining_Identifier (Def), Ifaces_List);
7693 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
7694 Set_Has_Non_Standard_Rep
7695 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
7698 -- STEP 4: Inherit components from the parent base and constrain them.
7699 -- Apply the second transformation described in point 6. above.
7701 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
7702 or else not Has_Discriminants (Parent_Type)
7703 or else not Is_Constrained (Parent_Type)
7707 Constrs := Discriminant_Constraint (Parent_Type);
7712 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
7714 -- STEP 5a: Copy the parent record declaration for untagged types
7716 if not Is_Tagged then
7718 -- Discriminant_Constraint (Derived_Type) has been properly
7719 -- constructed. Save it and temporarily set it to Empty because we
7720 -- do not want the call to New_Copy_Tree below to mess this list.
7722 if Has_Discriminants (Derived_Type) then
7723 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
7724 Set_Discriminant_Constraint (Derived_Type, No_Elist);
7726 Save_Discr_Constr := No_Elist;
7729 -- Save the Etype field of Derived_Type. It is correctly set now,
7730 -- but the call to New_Copy tree may remap it to point to itself,
7731 -- which is not what we want. Ditto for the Next_Entity field.
7733 Save_Etype := Etype (Derived_Type);
7734 Save_Next_Entity := Next_Entity (Derived_Type);
7736 -- Assoc_List maps all stored discriminants in the Parent_Base to
7737 -- stored discriminants in the Derived_Type. It is fundamental that
7738 -- no types or itypes with discriminants other than the stored
7739 -- discriminants appear in the entities declared inside
7740 -- Derived_Type, since the back end cannot deal with it.
7744 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
7746 -- Restore the fields saved prior to the New_Copy_Tree call
7747 -- and compute the stored constraint.
7749 Set_Etype (Derived_Type, Save_Etype);
7750 Set_Next_Entity (Derived_Type, Save_Next_Entity);
7752 if Has_Discriminants (Derived_Type) then
7753 Set_Discriminant_Constraint
7754 (Derived_Type, Save_Discr_Constr);
7755 Set_Stored_Constraint
7756 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
7757 Replace_Components (Derived_Type, New_Decl);
7760 -- Insert the new derived type declaration
7762 Rewrite (N, New_Decl);
7764 -- STEP 5b: Complete the processing for record extensions in generics
7766 -- There is no completion for record extensions declared in the
7767 -- parameter part of a generic, so we need to complete processing for
7768 -- these generic record extensions here. The Record_Type_Definition call
7769 -- will change the Ekind of the components from E_Void to E_Component.
7771 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
7772 Record_Type_Definition (Empty, Derived_Type);
7774 -- STEP 5c: Process the record extension for non private tagged types
7776 elsif not Private_Extension then
7778 -- Add the _parent field in the derived type
7780 Expand_Record_Extension (Derived_Type, Type_Def);
7782 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
7783 -- implemented interfaces if we are in expansion mode
7786 and then Has_Interfaces (Derived_Type)
7788 Add_Interface_Tag_Components (N, Derived_Type);
7791 -- Analyze the record extension
7793 Record_Type_Definition
7794 (Record_Extension_Part (Type_Def), Derived_Type);
7799 -- Nothing else to do if there is an error in the derivation.
7800 -- An unusual case: the full view may be derived from a type in an
7801 -- instance, when the partial view was used illegally as an actual
7802 -- in that instance, leading to a circular definition.
7804 if Etype (Derived_Type) = Any_Type
7805 or else Etype (Parent_Type) = Derived_Type
7810 -- Set delayed freeze and then derive subprograms, we need to do
7811 -- this in this order so that derived subprograms inherit the
7812 -- derived freeze if necessary.
7814 Set_Has_Delayed_Freeze (Derived_Type);
7816 if Derive_Subps then
7817 Derive_Subprograms (Parent_Type, Derived_Type);
7820 -- If we have a private extension which defines a constrained derived
7821 -- type mark as constrained here after we have derived subprograms. See
7822 -- comment on point 9. just above the body of Build_Derived_Record_Type.
7824 if Private_Extension and then Inherit_Discrims then
7825 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
7826 Set_Is_Constrained (Derived_Type, True);
7827 Set_Discriminant_Constraint (Derived_Type, Discs);
7829 elsif Is_Constrained (Parent_Type) then
7831 (Derived_Type, True);
7832 Set_Discriminant_Constraint
7833 (Derived_Type, Discriminant_Constraint (Parent_Type));
7837 -- Update the class-wide type, which shares the now-completed entity
7838 -- list with its specific type. In case of underlying record views,
7839 -- we do not generate the corresponding class wide entity.
7842 and then not Is_Underlying_Record_View (Derived_Type)
7845 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
7847 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
7850 -- Update the scope of anonymous access types of discriminants and other
7851 -- components, to prevent scope anomalies in gigi, when the derivation
7852 -- appears in a scope nested within that of the parent.
7858 D := First_Entity (Derived_Type);
7859 while Present (D) loop
7860 if Ekind_In (D, E_Discriminant, E_Component) then
7861 if Is_Itype (Etype (D))
7862 and then Ekind (Etype (D)) = E_Anonymous_Access_Type
7864 Set_Scope (Etype (D), Current_Scope);
7871 end Build_Derived_Record_Type;
7873 ------------------------
7874 -- Build_Derived_Type --
7875 ------------------------
7877 procedure Build_Derived_Type
7879 Parent_Type : Entity_Id;
7880 Derived_Type : Entity_Id;
7881 Is_Completion : Boolean;
7882 Derive_Subps : Boolean := True)
7884 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7887 -- Set common attributes
7889 Set_Scope (Derived_Type, Current_Scope);
7891 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7892 Set_Etype (Derived_Type, Parent_Base);
7893 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
7895 Set_Size_Info (Derived_Type, Parent_Type);
7896 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
7897 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7898 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
7900 -- If the parent type is a private subtype, the convention on the base
7901 -- type may be set in the private part, and not propagated to the
7902 -- subtype until later, so we obtain the convention from the base type.
7904 Set_Convention (Derived_Type, Convention (Parent_Base));
7906 -- Propagate invariant information. The new type has invariants if
7907 -- they are inherited from the parent type, and these invariants can
7908 -- be further inherited, so both flags are set.
7910 if Has_Inheritable_Invariants (Parent_Type) then
7911 Set_Has_Inheritable_Invariants (Derived_Type);
7912 Set_Has_Invariants (Derived_Type);
7915 -- We similarly inherit predicates
7917 if Has_Predicates (Parent_Type) then
7918 Set_Has_Predicates (Derived_Type);
7921 -- The derived type inherits the representation clauses of the parent.
7922 -- However, for a private type that is completed by a derivation, there
7923 -- may be operation attributes that have been specified already (stream
7924 -- attributes and External_Tag) and those must be provided. Finally,
7925 -- if the partial view is a private extension, the representation items
7926 -- of the parent have been inherited already, and should not be chained
7927 -- twice to the derived type.
7929 if Is_Tagged_Type (Parent_Type)
7930 and then Present (First_Rep_Item (Derived_Type))
7932 -- The existing items are either operational items or items inherited
7933 -- from a private extension declaration.
7937 -- Used to iterate over representation items of the derived type
7940 -- Last representation item of the (non-empty) representation
7941 -- item list of the derived type.
7943 Found : Boolean := False;
7946 Rep := First_Rep_Item (Derived_Type);
7948 while Present (Rep) loop
7949 if Rep = First_Rep_Item (Parent_Type) then
7954 Rep := Next_Rep_Item (Rep);
7956 if Present (Rep) then
7962 -- Here if we either encountered the parent type's first rep
7963 -- item on the derived type's rep item list (in which case
7964 -- Found is True, and we have nothing else to do), or if we
7965 -- reached the last rep item of the derived type, which is
7966 -- Last_Rep, in which case we further chain the parent type's
7967 -- rep items to those of the derived type.
7970 Set_Next_Rep_Item (Last_Rep, First_Rep_Item (Parent_Type));
7975 Set_First_Rep_Item (Derived_Type, First_Rep_Item (Parent_Type));
7978 case Ekind (Parent_Type) is
7979 when Numeric_Kind =>
7980 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
7983 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
7987 | Class_Wide_Kind =>
7988 Build_Derived_Record_Type
7989 (N, Parent_Type, Derived_Type, Derive_Subps);
7992 when Enumeration_Kind =>
7993 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
7996 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
7998 when Incomplete_Or_Private_Kind =>
7999 Build_Derived_Private_Type
8000 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
8002 -- For discriminated types, the derivation includes deriving
8003 -- primitive operations. For others it is done below.
8005 if Is_Tagged_Type (Parent_Type)
8006 or else Has_Discriminants (Parent_Type)
8007 or else (Present (Full_View (Parent_Type))
8008 and then Has_Discriminants (Full_View (Parent_Type)))
8013 when Concurrent_Kind =>
8014 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
8017 raise Program_Error;
8020 if Etype (Derived_Type) = Any_Type then
8024 -- Set delayed freeze and then derive subprograms, we need to do this
8025 -- in this order so that derived subprograms inherit the derived freeze
8028 Set_Has_Delayed_Freeze (Derived_Type);
8029 if Derive_Subps then
8030 Derive_Subprograms (Parent_Type, Derived_Type);
8033 Set_Has_Primitive_Operations
8034 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
8035 end Build_Derived_Type;
8037 -----------------------
8038 -- Build_Discriminal --
8039 -----------------------
8041 procedure Build_Discriminal (Discrim : Entity_Id) is
8042 D_Minal : Entity_Id;
8043 CR_Disc : Entity_Id;
8046 -- A discriminal has the same name as the discriminant
8048 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8050 Set_Ekind (D_Minal, E_In_Parameter);
8051 Set_Mechanism (D_Minal, Default_Mechanism);
8052 Set_Etype (D_Minal, Etype (Discrim));
8053 Set_Scope (D_Minal, Current_Scope);
8055 Set_Discriminal (Discrim, D_Minal);
8056 Set_Discriminal_Link (D_Minal, Discrim);
8058 -- For task types, build at once the discriminants of the corresponding
8059 -- record, which are needed if discriminants are used in entry defaults
8060 -- and in family bounds.
8062 if Is_Concurrent_Type (Current_Scope)
8063 or else Is_Limited_Type (Current_Scope)
8065 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
8067 Set_Ekind (CR_Disc, E_In_Parameter);
8068 Set_Mechanism (CR_Disc, Default_Mechanism);
8069 Set_Etype (CR_Disc, Etype (Discrim));
8070 Set_Scope (CR_Disc, Current_Scope);
8071 Set_Discriminal_Link (CR_Disc, Discrim);
8072 Set_CR_Discriminant (Discrim, CR_Disc);
8074 end Build_Discriminal;
8076 ------------------------------------
8077 -- Build_Discriminant_Constraints --
8078 ------------------------------------
8080 function Build_Discriminant_Constraints
8083 Derived_Def : Boolean := False) return Elist_Id
8085 C : constant Node_Id := Constraint (Def);
8086 Nb_Discr : constant Nat := Number_Discriminants (T);
8088 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
8089 -- Saves the expression corresponding to a given discriminant in T
8091 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
8092 -- Return the Position number within array Discr_Expr of a discriminant
8093 -- D within the discriminant list of the discriminated type T.
8099 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
8103 Disc := First_Discriminant (T);
8104 for J in Discr_Expr'Range loop
8109 Next_Discriminant (Disc);
8112 -- Note: Since this function is called on discriminants that are
8113 -- known to belong to the discriminated type, falling through the
8114 -- loop with no match signals an internal compiler error.
8116 raise Program_Error;
8119 -- Declarations local to Build_Discriminant_Constraints
8123 Elist : constant Elist_Id := New_Elmt_List;
8131 Discrim_Present : Boolean := False;
8133 -- Start of processing for Build_Discriminant_Constraints
8136 -- The following loop will process positional associations only.
8137 -- For a positional association, the (single) discriminant is
8138 -- implicitly specified by position, in textual order (RM 3.7.2).
8140 Discr := First_Discriminant (T);
8141 Constr := First (Constraints (C));
8142 for D in Discr_Expr'Range loop
8143 exit when Nkind (Constr) = N_Discriminant_Association;
8146 Error_Msg_N ("too few discriminants given in constraint", C);
8147 return New_Elmt_List;
8149 elsif Nkind (Constr) = N_Range
8150 or else (Nkind (Constr) = N_Attribute_Reference
8152 Attribute_Name (Constr) = Name_Range)
8155 ("a range is not a valid discriminant constraint", Constr);
8156 Discr_Expr (D) := Error;
8159 Analyze_And_Resolve (Constr, Base_Type (Etype (Discr)));
8160 Discr_Expr (D) := Constr;
8163 Next_Discriminant (Discr);
8167 if No (Discr) and then Present (Constr) then
8168 Error_Msg_N ("too many discriminants given in constraint", Constr);
8169 return New_Elmt_List;
8172 -- Named associations can be given in any order, but if both positional
8173 -- and named associations are used in the same discriminant constraint,
8174 -- then positional associations must occur first, at their normal
8175 -- position. Hence once a named association is used, the rest of the
8176 -- discriminant constraint must use only named associations.
8178 while Present (Constr) loop
8180 -- Positional association forbidden after a named association
8182 if Nkind (Constr) /= N_Discriminant_Association then
8183 Error_Msg_N ("positional association follows named one", Constr);
8184 return New_Elmt_List;
8186 -- Otherwise it is a named association
8189 -- E records the type of the discriminants in the named
8190 -- association. All the discriminants specified in the same name
8191 -- association must have the same type.
8195 -- Search the list of discriminants in T to see if the simple name
8196 -- given in the constraint matches any of them.
8198 Id := First (Selector_Names (Constr));
8199 while Present (Id) loop
8202 -- If Original_Discriminant is present, we are processing a
8203 -- generic instantiation and this is an instance node. We need
8204 -- to find the name of the corresponding discriminant in the
8205 -- actual record type T and not the name of the discriminant in
8206 -- the generic formal. Example:
8209 -- type G (D : int) is private;
8211 -- subtype W is G (D => 1);
8213 -- type Rec (X : int) is record ... end record;
8214 -- package Q is new P (G => Rec);
8216 -- At the point of the instantiation, formal type G is Rec
8217 -- and therefore when reanalyzing "subtype W is G (D => 1);"
8218 -- which really looks like "subtype W is Rec (D => 1);" at
8219 -- the point of instantiation, we want to find the discriminant
8220 -- that corresponds to D in Rec, i.e. X.
8222 if Present (Original_Discriminant (Id)) then
8223 Discr := Find_Corresponding_Discriminant (Id, T);
8227 Discr := First_Discriminant (T);
8228 while Present (Discr) loop
8229 if Chars (Discr) = Chars (Id) then
8234 Next_Discriminant (Discr);
8238 Error_Msg_N ("& does not match any discriminant", Id);
8239 return New_Elmt_List;
8241 -- The following is only useful for the benefit of generic
8242 -- instances but it does not interfere with other
8243 -- processing for the non-generic case so we do it in all
8244 -- cases (for generics this statement is executed when
8245 -- processing the generic definition, see comment at the
8246 -- beginning of this if statement).
8249 Set_Original_Discriminant (Id, Discr);
8253 Position := Pos_Of_Discr (T, Discr);
8255 if Present (Discr_Expr (Position)) then
8256 Error_Msg_N ("duplicate constraint for discriminant&", Id);
8259 -- Each discriminant specified in the same named association
8260 -- must be associated with a separate copy of the
8261 -- corresponding expression.
8263 if Present (Next (Id)) then
8264 Expr := New_Copy_Tree (Expression (Constr));
8265 Set_Parent (Expr, Parent (Expression (Constr)));
8267 Expr := Expression (Constr);
8270 Discr_Expr (Position) := Expr;
8271 Analyze_And_Resolve (Expr, Base_Type (Etype (Discr)));
8274 -- A discriminant association with more than one discriminant
8275 -- name is only allowed if the named discriminants are all of
8276 -- the same type (RM 3.7.1(8)).
8279 E := Base_Type (Etype (Discr));
8281 elsif Base_Type (Etype (Discr)) /= E then
8283 ("all discriminants in an association " &
8284 "must have the same type", Id);
8294 -- A discriminant constraint must provide exactly one value for each
8295 -- discriminant of the type (RM 3.7.1(8)).
8297 for J in Discr_Expr'Range loop
8298 if No (Discr_Expr (J)) then
8299 Error_Msg_N ("too few discriminants given in constraint", C);
8300 return New_Elmt_List;
8304 -- Determine if there are discriminant expressions in the constraint
8306 for J in Discr_Expr'Range loop
8307 if Denotes_Discriminant
8308 (Discr_Expr (J), Check_Concurrent => True)
8310 Discrim_Present := True;
8314 -- Build an element list consisting of the expressions given in the
8315 -- discriminant constraint and apply the appropriate checks. The list
8316 -- is constructed after resolving any named discriminant associations
8317 -- and therefore the expressions appear in the textual order of the
8320 Discr := First_Discriminant (T);
8321 for J in Discr_Expr'Range loop
8322 if Discr_Expr (J) /= Error then
8323 Append_Elmt (Discr_Expr (J), Elist);
8325 -- If any of the discriminant constraints is given by a
8326 -- discriminant and we are in a derived type declaration we
8327 -- have a discriminant renaming. Establish link between new
8328 -- and old discriminant.
8330 if Denotes_Discriminant (Discr_Expr (J)) then
8332 Set_Corresponding_Discriminant
8333 (Entity (Discr_Expr (J)), Discr);
8336 -- Force the evaluation of non-discriminant expressions.
8337 -- If we have found a discriminant in the constraint 3.4(26)
8338 -- and 3.8(18) demand that no range checks are performed are
8339 -- after evaluation. If the constraint is for a component
8340 -- definition that has a per-object constraint, expressions are
8341 -- evaluated but not checked either. In all other cases perform
8345 if Discrim_Present then
8348 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
8350 Has_Per_Object_Constraint
8351 (Defining_Identifier (Parent (Parent (Def))))
8355 elsif Is_Access_Type (Etype (Discr)) then
8356 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
8359 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
8362 Force_Evaluation (Discr_Expr (J));
8365 -- Check that the designated type of an access discriminant's
8366 -- expression is not a class-wide type unless the discriminant's
8367 -- designated type is also class-wide.
8369 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
8370 and then not Is_Class_Wide_Type
8371 (Designated_Type (Etype (Discr)))
8372 and then Etype (Discr_Expr (J)) /= Any_Type
8373 and then Is_Class_Wide_Type
8374 (Designated_Type (Etype (Discr_Expr (J))))
8376 Wrong_Type (Discr_Expr (J), Etype (Discr));
8378 elsif Is_Access_Type (Etype (Discr))
8379 and then not Is_Access_Constant (Etype (Discr))
8380 and then Is_Access_Type (Etype (Discr_Expr (J)))
8381 and then Is_Access_Constant (Etype (Discr_Expr (J)))
8384 ("constraint for discriminant& must be access to variable",
8389 Next_Discriminant (Discr);
8393 end Build_Discriminant_Constraints;
8395 ---------------------------------
8396 -- Build_Discriminated_Subtype --
8397 ---------------------------------
8399 procedure Build_Discriminated_Subtype
8403 Related_Nod : Node_Id;
8404 For_Access : Boolean := False)
8406 Has_Discrs : constant Boolean := Has_Discriminants (T);
8407 Constrained : constant Boolean :=
8409 and then not Is_Empty_Elmt_List (Elist)
8410 and then not Is_Class_Wide_Type (T))
8411 or else Is_Constrained (T);
8414 if Ekind (T) = E_Record_Type then
8416 Set_Ekind (Def_Id, E_Private_Subtype);
8417 Set_Is_For_Access_Subtype (Def_Id, True);
8419 Set_Ekind (Def_Id, E_Record_Subtype);
8422 -- Inherit preelaboration flag from base, for types for which it
8423 -- may have been set: records, private types, protected types.
8425 Set_Known_To_Have_Preelab_Init
8426 (Def_Id, Known_To_Have_Preelab_Init (T));
8428 elsif Ekind (T) = E_Task_Type then
8429 Set_Ekind (Def_Id, E_Task_Subtype);
8431 elsif Ekind (T) = E_Protected_Type then
8432 Set_Ekind (Def_Id, E_Protected_Subtype);
8433 Set_Known_To_Have_Preelab_Init
8434 (Def_Id, Known_To_Have_Preelab_Init (T));
8436 elsif Is_Private_Type (T) then
8437 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
8438 Set_Known_To_Have_Preelab_Init
8439 (Def_Id, Known_To_Have_Preelab_Init (T));
8441 elsif Is_Class_Wide_Type (T) then
8442 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
8445 -- Incomplete type. Attach subtype to list of dependents, to be
8446 -- completed with full view of parent type, unless is it the
8447 -- designated subtype of a record component within an init_proc.
8448 -- This last case arises for a component of an access type whose
8449 -- designated type is incomplete (e.g. a Taft Amendment type).
8450 -- The designated subtype is within an inner scope, and needs no
8451 -- elaboration, because only the access type is needed in the
8452 -- initialization procedure.
8454 Set_Ekind (Def_Id, Ekind (T));
8456 if For_Access and then Within_Init_Proc then
8459 Append_Elmt (Def_Id, Private_Dependents (T));
8463 Set_Etype (Def_Id, T);
8464 Init_Size_Align (Def_Id);
8465 Set_Has_Discriminants (Def_Id, Has_Discrs);
8466 Set_Is_Constrained (Def_Id, Constrained);
8468 Set_First_Entity (Def_Id, First_Entity (T));
8469 Set_Last_Entity (Def_Id, Last_Entity (T));
8471 -- If the subtype is the completion of a private declaration, there may
8472 -- have been representation clauses for the partial view, and they must
8473 -- be preserved. Build_Derived_Type chains the inherited clauses with
8474 -- the ones appearing on the extension. If this comes from a subtype
8475 -- declaration, all clauses are inherited.
8477 if No (First_Rep_Item (Def_Id)) then
8478 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
8481 if Is_Tagged_Type (T) then
8482 Set_Is_Tagged_Type (Def_Id);
8483 Make_Class_Wide_Type (Def_Id);
8486 Set_Stored_Constraint (Def_Id, No_Elist);
8489 Set_Discriminant_Constraint (Def_Id, Elist);
8490 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
8493 if Is_Tagged_Type (T) then
8495 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
8496 -- concurrent record type (which has the list of primitive
8499 if Ada_Version >= Ada_2005
8500 and then Is_Concurrent_Type (T)
8502 Set_Corresponding_Record_Type (Def_Id,
8503 Corresponding_Record_Type (T));
8505 Set_Direct_Primitive_Operations (Def_Id,
8506 Direct_Primitive_Operations (T));
8509 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
8512 -- Subtypes introduced by component declarations do not need to be
8513 -- marked as delayed, and do not get freeze nodes, because the semantics
8514 -- verifies that the parents of the subtypes are frozen before the
8515 -- enclosing record is frozen.
8517 if not Is_Type (Scope (Def_Id)) then
8518 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
8520 if Is_Private_Type (T)
8521 and then Present (Full_View (T))
8523 Conditional_Delay (Def_Id, Full_View (T));
8525 Conditional_Delay (Def_Id, T);
8529 if Is_Record_Type (T) then
8530 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
8533 and then not Is_Empty_Elmt_List (Elist)
8534 and then not For_Access
8536 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
8537 elsif not For_Access then
8538 Set_Cloned_Subtype (Def_Id, T);
8541 end Build_Discriminated_Subtype;
8543 ---------------------------
8544 -- Build_Itype_Reference --
8545 ---------------------------
8547 procedure Build_Itype_Reference
8551 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
8553 Set_Itype (IR, Ityp);
8554 Insert_After (Nod, IR);
8555 end Build_Itype_Reference;
8557 ------------------------
8558 -- Build_Scalar_Bound --
8559 ------------------------
8561 function Build_Scalar_Bound
8564 Der_T : Entity_Id) return Node_Id
8566 New_Bound : Entity_Id;
8569 -- Note: not clear why this is needed, how can the original bound
8570 -- be unanalyzed at this point? and if it is, what business do we
8571 -- have messing around with it? and why is the base type of the
8572 -- parent type the right type for the resolution. It probably is
8573 -- not! It is OK for the new bound we are creating, but not for
8574 -- the old one??? Still if it never happens, no problem!
8576 Analyze_And_Resolve (Bound, Base_Type (Par_T));
8578 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
8579 New_Bound := New_Copy (Bound);
8580 Set_Etype (New_Bound, Der_T);
8581 Set_Analyzed (New_Bound);
8583 elsif Is_Entity_Name (Bound) then
8584 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
8586 -- The following is almost certainly wrong. What business do we have
8587 -- relocating a node (Bound) that is presumably still attached to
8588 -- the tree elsewhere???
8591 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
8594 Set_Etype (New_Bound, Der_T);
8596 end Build_Scalar_Bound;
8598 --------------------------------
8599 -- Build_Underlying_Full_View --
8600 --------------------------------
8602 procedure Build_Underlying_Full_View
8607 Loc : constant Source_Ptr := Sloc (N);
8608 Subt : constant Entity_Id :=
8609 Make_Defining_Identifier
8610 (Loc, New_External_Name (Chars (Typ), 'S'));
8617 procedure Set_Discriminant_Name (Id : Node_Id);
8618 -- If the derived type has discriminants, they may rename discriminants
8619 -- of the parent. When building the full view of the parent, we need to
8620 -- recover the names of the original discriminants if the constraint is
8621 -- given by named associations.
8623 ---------------------------
8624 -- Set_Discriminant_Name --
8625 ---------------------------
8627 procedure Set_Discriminant_Name (Id : Node_Id) is
8631 Set_Original_Discriminant (Id, Empty);
8633 if Has_Discriminants (Typ) then
8634 Disc := First_Discriminant (Typ);
8635 while Present (Disc) loop
8636 if Chars (Disc) = Chars (Id)
8637 and then Present (Corresponding_Discriminant (Disc))
8639 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
8641 Next_Discriminant (Disc);
8644 end Set_Discriminant_Name;
8646 -- Start of processing for Build_Underlying_Full_View
8649 if Nkind (N) = N_Full_Type_Declaration then
8650 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
8652 elsif Nkind (N) = N_Subtype_Declaration then
8653 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
8655 elsif Nkind (N) = N_Component_Declaration then
8658 (Constraint (Subtype_Indication (Component_Definition (N))));
8661 raise Program_Error;
8664 C := First (Constraints (Constr));
8665 while Present (C) loop
8666 if Nkind (C) = N_Discriminant_Association then
8667 Id := First (Selector_Names (C));
8668 while Present (Id) loop
8669 Set_Discriminant_Name (Id);
8678 Make_Subtype_Declaration (Loc,
8679 Defining_Identifier => Subt,
8680 Subtype_Indication =>
8681 Make_Subtype_Indication (Loc,
8682 Subtype_Mark => New_Reference_To (Par, Loc),
8683 Constraint => New_Copy_Tree (Constr)));
8685 -- If this is a component subtype for an outer itype, it is not
8686 -- a list member, so simply set the parent link for analysis: if
8687 -- the enclosing type does not need to be in a declarative list,
8688 -- neither do the components.
8690 if Is_List_Member (N)
8691 and then Nkind (N) /= N_Component_Declaration
8693 Insert_Before (N, Indic);
8695 Set_Parent (Indic, Parent (N));
8699 Set_Underlying_Full_View (Typ, Full_View (Subt));
8700 end Build_Underlying_Full_View;
8702 -------------------------------
8703 -- Check_Abstract_Overriding --
8704 -------------------------------
8706 procedure Check_Abstract_Overriding (T : Entity_Id) is
8707 Alias_Subp : Entity_Id;
8713 procedure Check_Pragma_Implemented (Subp : Entity_Id);
8714 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
8715 -- which has pragma Implemented already set. Check whether Subp's entity
8716 -- kind conforms to the implementation kind of the overridden routine.
8718 procedure Check_Pragma_Implemented
8720 Iface_Subp : Entity_Id);
8721 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
8722 -- Iface_Subp and both entities have pragma Implemented already set on
8723 -- them. Check whether the two implementation kinds are conforming.
8725 procedure Inherit_Pragma_Implemented
8727 Iface_Subp : Entity_Id);
8728 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
8729 -- subprogram Iface_Subp which has been marked by pragma Implemented.
8730 -- Propagate the implementation kind of Iface_Subp to Subp.
8732 ------------------------------
8733 -- Check_Pragma_Implemented --
8734 ------------------------------
8736 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
8737 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
8738 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
8739 Contr_Typ : Entity_Id;
8742 -- Subp must have an alias since it is a hidden entity used to link
8743 -- an interface subprogram to its overriding counterpart.
8745 pragma Assert (Present (Alias (Subp)));
8747 -- Extract the type of the controlling formal
8749 Contr_Typ := Etype (First_Formal (Alias (Subp)));
8751 if Is_Concurrent_Record_Type (Contr_Typ) then
8752 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
8755 -- An interface subprogram whose implementation kind is By_Entry must
8756 -- be implemented by an entry.
8758 if Impl_Kind = Name_By_Entry
8759 and then Ekind (Wrapped_Entity (Alias (Subp))) /= E_Entry
8761 Error_Msg_Node_2 := Iface_Alias;
8763 ("type & must implement abstract subprogram & with an entry",
8764 Alias (Subp), Contr_Typ);
8766 elsif Impl_Kind = Name_By_Protected_Procedure then
8768 -- An interface subprogram whose implementation kind is By_
8769 -- Protected_Procedure cannot be implemented by a primitive
8770 -- procedure of a task type.
8772 if Ekind (Contr_Typ) /= E_Protected_Type then
8773 Error_Msg_Node_2 := Contr_Typ;
8775 ("interface subprogram & cannot be implemented by a " &
8776 "primitive procedure of task type &", Alias (Subp),
8779 -- An interface subprogram whose implementation kind is By_
8780 -- Protected_Procedure must be implemented by a procedure.
8782 elsif Is_Primitive_Wrapper (Alias (Subp))
8783 and then Ekind (Wrapped_Entity (Alias (Subp))) /= E_Procedure
8785 Error_Msg_Node_2 := Iface_Alias;
8787 ("type & must implement abstract subprogram & with a " &
8788 "procedure", Alias (Subp), Contr_Typ);
8791 end Check_Pragma_Implemented;
8793 ------------------------------
8794 -- Check_Pragma_Implemented --
8795 ------------------------------
8797 procedure Check_Pragma_Implemented
8799 Iface_Subp : Entity_Id)
8801 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
8802 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
8805 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
8806 -- and overriding subprogram are different. In general this is an
8807 -- error except when the implementation kind of the overridden
8808 -- subprograms is By_Any.
8810 if Iface_Kind /= Subp_Kind
8811 and then Iface_Kind /= Name_By_Any
8813 if Iface_Kind = Name_By_Entry then
8815 ("incompatible implementation kind, overridden subprogram " &
8816 "is marked By_Entry", Subp);
8819 ("incompatible implementation kind, overridden subprogram " &
8820 "is marked By_Protected_Procedure", Subp);
8823 end Check_Pragma_Implemented;
8825 --------------------------------
8826 -- Inherit_Pragma_Implemented --
8827 --------------------------------
8829 procedure Inherit_Pragma_Implemented
8831 Iface_Subp : Entity_Id)
8833 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
8834 Loc : constant Source_Ptr := Sloc (Subp);
8835 Impl_Prag : Node_Id;
8838 -- Since the implementation kind is stored as a representation item
8839 -- rather than a flag, create a pragma node.
8843 Chars => Name_Implemented,
8844 Pragma_Argument_Associations => New_List (
8845 Make_Pragma_Argument_Association (Loc,
8847 New_Reference_To (Subp, Loc)),
8849 Make_Pragma_Argument_Association (Loc,
8850 Expression => Make_Identifier (Loc, Iface_Kind))));
8852 -- The pragma doesn't need to be analyzed because it is internally
8853 -- build. It is safe to directly register it as a rep item since we
8854 -- are only interested in the characters of the implementation kind.
8856 Record_Rep_Item (Subp, Impl_Prag);
8857 end Inherit_Pragma_Implemented;
8859 -- Start of processing for Check_Abstract_Overriding
8862 Op_List := Primitive_Operations (T);
8864 -- Loop to check primitive operations
8866 Elmt := First_Elmt (Op_List);
8867 while Present (Elmt) loop
8868 Subp := Node (Elmt);
8869 Alias_Subp := Alias (Subp);
8871 -- Inherited subprograms are identified by the fact that they do not
8872 -- come from source, and the associated source location is the
8873 -- location of the first subtype of the derived type.
8875 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
8876 -- subprograms that "require overriding".
8878 -- Special exception, do not complain about failure to override the
8879 -- stream routines _Input and _Output, as well as the primitive
8880 -- operations used in dispatching selects since we always provide
8881 -- automatic overridings for these subprograms.
8883 -- Also ignore this rule for convention CIL since .NET libraries
8884 -- do bizarre things with interfaces???
8886 -- The partial view of T may have been a private extension, for
8887 -- which inherited functions dispatching on result are abstract.
8888 -- If the full view is a null extension, there is no need for
8889 -- overriding in Ada2005, but wrappers need to be built for them
8890 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
8892 if Is_Null_Extension (T)
8893 and then Has_Controlling_Result (Subp)
8894 and then Ada_Version >= Ada_2005
8895 and then Present (Alias_Subp)
8896 and then not Comes_From_Source (Subp)
8897 and then not Is_Abstract_Subprogram (Alias_Subp)
8898 and then not Is_Access_Type (Etype (Subp))
8902 -- Ada 2005 (AI-251): Internal entities of interfaces need no
8903 -- processing because this check is done with the aliased
8906 elsif Present (Interface_Alias (Subp)) then
8909 elsif (Is_Abstract_Subprogram (Subp)
8910 or else Requires_Overriding (Subp)
8912 (Has_Controlling_Result (Subp)
8913 and then Present (Alias_Subp)
8914 and then not Comes_From_Source (Subp)
8915 and then Sloc (Subp) = Sloc (First_Subtype (T))))
8916 and then not Is_TSS (Subp, TSS_Stream_Input)
8917 and then not Is_TSS (Subp, TSS_Stream_Output)
8918 and then not Is_Abstract_Type (T)
8919 and then Convention (T) /= Convention_CIL
8920 and then not Is_Predefined_Interface_Primitive (Subp)
8922 -- Ada 2005 (AI-251): Do not consider hidden entities associated
8923 -- with abstract interface types because the check will be done
8924 -- with the aliased entity (otherwise we generate a duplicated
8927 and then not Present (Interface_Alias (Subp))
8929 if Present (Alias_Subp) then
8931 -- Only perform the check for a derived subprogram when the
8932 -- type has an explicit record extension. This avoids incorrect
8933 -- flagging of abstract subprograms for the case of a type
8934 -- without an extension that is derived from a formal type
8935 -- with a tagged actual (can occur within a private part).
8937 -- Ada 2005 (AI-391): In the case of an inherited function with
8938 -- a controlling result of the type, the rule does not apply if
8939 -- the type is a null extension (unless the parent function
8940 -- itself is abstract, in which case the function must still be
8941 -- be overridden). The expander will generate an overriding
8942 -- wrapper function calling the parent subprogram (see
8943 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
8945 Type_Def := Type_Definition (Parent (T));
8947 if Nkind (Type_Def) = N_Derived_Type_Definition
8948 and then Present (Record_Extension_Part (Type_Def))
8950 (Ada_Version < Ada_2005
8951 or else not Is_Null_Extension (T)
8952 or else Ekind (Subp) = E_Procedure
8953 or else not Has_Controlling_Result (Subp)
8954 or else Is_Abstract_Subprogram (Alias_Subp)
8955 or else Requires_Overriding (Subp)
8956 or else Is_Access_Type (Etype (Subp)))
8958 -- Avoid reporting error in case of abstract predefined
8959 -- primitive inherited from interface type because the
8960 -- body of internally generated predefined primitives
8961 -- of tagged types are generated later by Freeze_Type
8963 if Is_Interface (Root_Type (T))
8964 and then Is_Abstract_Subprogram (Subp)
8965 and then Is_Predefined_Dispatching_Operation (Subp)
8966 and then not Comes_From_Source (Ultimate_Alias (Subp))
8972 ("type must be declared abstract or & overridden",
8975 -- Traverse the whole chain of aliased subprograms to
8976 -- complete the error notification. This is especially
8977 -- useful for traceability of the chain of entities when
8978 -- the subprogram corresponds with an interface
8979 -- subprogram (which may be defined in another package).
8981 if Present (Alias_Subp) then
8987 while Present (Alias (E)) loop
8988 Error_Msg_Sloc := Sloc (E);
8990 ("\& has been inherited #", T, Subp);
8994 Error_Msg_Sloc := Sloc (E);
8996 ("\& has been inherited from subprogram #",
9002 -- Ada 2005 (AI-345): Protected or task type implementing
9003 -- abstract interfaces.
9005 elsif Is_Concurrent_Record_Type (T)
9006 and then Present (Interfaces (T))
9008 -- The controlling formal of Subp must be of mode "out",
9009 -- "in out" or an access-to-variable to be overridden.
9011 -- Error message below needs rewording (remember comma
9012 -- in -gnatj mode) ???
9014 if Ekind (First_Formal (Subp)) = E_In_Parameter
9015 and then Ekind (Subp) /= E_Function
9017 if not Is_Predefined_Dispatching_Operation (Subp) then
9019 ("first formal of & must be of mode `OUT`, " &
9020 "`IN OUT` or access-to-variable", T, Subp);
9022 ("\to be overridden by protected procedure or " &
9023 "entry (RM 9.4(11.9/2))", T);
9026 -- Some other kind of overriding failure
9030 ("interface subprogram & must be overridden",
9033 -- Examine primitive operations of synchronized type,
9034 -- to find homonyms that have the wrong profile.
9041 First_Entity (Corresponding_Concurrent_Type (T));
9042 while Present (Prim) loop
9043 if Chars (Prim) = Chars (Subp) then
9045 ("profile is not type conformant with "
9046 & "prefixed view profile of "
9047 & "inherited operation&", Prim, Subp);
9057 Error_Msg_Node_2 := T;
9059 ("abstract subprogram& not allowed for type&", Subp);
9061 -- Also post unconditional warning on the type (unconditional
9062 -- so that if there are more than one of these cases, we get
9063 -- them all, and not just the first one).
9065 Error_Msg_Node_2 := Subp;
9066 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
9070 -- Ada 2012 (AI05-0030): Perform some checks related to pragma
9073 -- Subp is an expander-generated procedure which maps an interface
9074 -- alias to a protected wrapper. The interface alias is flagged by
9075 -- pragma Implemented. Ensure that Subp is a procedure when the
9076 -- implementation kind is By_Protected_Procedure or an entry when
9079 if Ada_Version >= Ada_2012
9080 and then Is_Hidden (Subp)
9081 and then Present (Interface_Alias (Subp))
9082 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
9084 Check_Pragma_Implemented (Subp);
9087 -- Subp is an interface primitive which overrides another interface
9088 -- primitive marked with pragma Implemented.
9090 if Ada_Version >= Ada_2012
9091 and then Present (Overridden_Operation (Subp))
9092 and then Has_Rep_Pragma
9093 (Overridden_Operation (Subp), Name_Implemented)
9095 -- If the overriding routine is also marked by Implemented, check
9096 -- that the two implementation kinds are conforming.
9098 if Has_Rep_Pragma (Subp, Name_Implemented) then
9099 Check_Pragma_Implemented
9101 Iface_Subp => Overridden_Operation (Subp));
9103 -- Otherwise the overriding routine inherits the implementation
9104 -- kind from the overridden subprogram.
9107 Inherit_Pragma_Implemented
9109 Iface_Subp => Overridden_Operation (Subp));
9115 end Check_Abstract_Overriding;
9117 ------------------------------------------------
9118 -- Check_Access_Discriminant_Requires_Limited --
9119 ------------------------------------------------
9121 procedure Check_Access_Discriminant_Requires_Limited
9126 -- A discriminant_specification for an access discriminant shall appear
9127 -- only in the declaration for a task or protected type, or for a type
9128 -- with the reserved word 'limited' in its definition or in one of its
9129 -- ancestors (RM 3.7(10)).
9131 -- AI-0063: The proper condition is that type must be immutably limited,
9132 -- or else be a partial view.
9134 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
9135 if Is_Immutably_Limited_Type (Current_Scope)
9137 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
9138 and then Limited_Present (Parent (Current_Scope)))
9144 ("access discriminants allowed only for limited types", Loc);
9147 end Check_Access_Discriminant_Requires_Limited;
9149 -----------------------------------
9150 -- Check_Aliased_Component_Types --
9151 -----------------------------------
9153 procedure Check_Aliased_Component_Types (T : Entity_Id) is
9157 -- ??? Also need to check components of record extensions, but not
9158 -- components of protected types (which are always limited).
9160 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
9161 -- types to be unconstrained. This is safe because it is illegal to
9162 -- create access subtypes to such types with explicit discriminant
9165 if not Is_Limited_Type (T) then
9166 if Ekind (T) = E_Record_Type then
9167 C := First_Component (T);
9168 while Present (C) loop
9170 and then Has_Discriminants (Etype (C))
9171 and then not Is_Constrained (Etype (C))
9172 and then not In_Instance_Body
9173 and then Ada_Version < Ada_2005
9176 ("aliased component must be constrained (RM 3.6(11))",
9183 elsif Ekind (T) = E_Array_Type then
9184 if Has_Aliased_Components (T)
9185 and then Has_Discriminants (Component_Type (T))
9186 and then not Is_Constrained (Component_Type (T))
9187 and then not In_Instance_Body
9188 and then Ada_Version < Ada_2005
9191 ("aliased component type must be constrained (RM 3.6(11))",
9196 end Check_Aliased_Component_Types;
9198 ----------------------
9199 -- Check_Completion --
9200 ----------------------
9202 procedure Check_Completion (Body_Id : Node_Id := Empty) is
9205 procedure Post_Error;
9206 -- Post error message for lack of completion for entity E
9212 procedure Post_Error is
9214 procedure Missing_Body;
9215 -- Output missing body message
9221 procedure Missing_Body is
9223 -- Spec is in same unit, so we can post on spec
9225 if In_Same_Source_Unit (Body_Id, E) then
9226 Error_Msg_N ("missing body for &", E);
9228 -- Spec is in a separate unit, so we have to post on the body
9231 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
9235 -- Start of processing for Post_Error
9238 if not Comes_From_Source (E) then
9240 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
9241 -- It may be an anonymous protected type created for a
9242 -- single variable. Post error on variable, if present.
9248 Var := First_Entity (Current_Scope);
9249 while Present (Var) loop
9250 exit when Etype (Var) = E
9251 and then Comes_From_Source (Var);
9256 if Present (Var) then
9263 -- If a generated entity has no completion, then either previous
9264 -- semantic errors have disabled the expansion phase, or else we had
9265 -- missing subunits, or else we are compiling without expansion,
9266 -- or else something is very wrong.
9268 if not Comes_From_Source (E) then
9270 (Serious_Errors_Detected > 0
9271 or else Configurable_Run_Time_Violations > 0
9272 or else Subunits_Missing
9273 or else not Expander_Active);
9276 -- Here for source entity
9279 -- Here if no body to post the error message, so we post the error
9280 -- on the declaration that has no completion. This is not really
9281 -- the right place to post it, think about this later ???
9283 if No (Body_Id) then
9286 ("missing full declaration for }", Parent (E), E);
9288 Error_Msg_NE ("missing body for &", Parent (E), E);
9291 -- Package body has no completion for a declaration that appears
9292 -- in the corresponding spec. Post error on the body, with a
9293 -- reference to the non-completed declaration.
9296 Error_Msg_Sloc := Sloc (E);
9299 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
9301 elsif Is_Overloadable (E)
9302 and then Current_Entity_In_Scope (E) /= E
9304 -- It may be that the completion is mistyped and appears as
9305 -- a distinct overloading of the entity.
9308 Candidate : constant Entity_Id :=
9309 Current_Entity_In_Scope (E);
9310 Decl : constant Node_Id :=
9311 Unit_Declaration_Node (Candidate);
9314 if Is_Overloadable (Candidate)
9315 and then Ekind (Candidate) = Ekind (E)
9316 and then Nkind (Decl) = N_Subprogram_Body
9317 and then Acts_As_Spec (Decl)
9319 Check_Type_Conformant (Candidate, E);
9333 -- Start of processing for Check_Completion
9336 E := First_Entity (Current_Scope);
9337 while Present (E) loop
9338 if Is_Intrinsic_Subprogram (E) then
9341 -- The following situation requires special handling: a child unit
9342 -- that appears in the context clause of the body of its parent:
9344 -- procedure Parent.Child (...);
9346 -- with Parent.Child;
9347 -- package body Parent is
9349 -- Here Parent.Child appears as a local entity, but should not be
9350 -- flagged as requiring completion, because it is a compilation
9353 -- Ignore missing completion for a subprogram that does not come from
9354 -- source (including the _Call primitive operation of RAS types,
9355 -- which has to have the flag Comes_From_Source for other purposes):
9356 -- we assume that the expander will provide the missing completion.
9357 -- In case of previous errors, other expansion actions that provide
9358 -- bodies for null procedures with not be invoked, so inhibit message
9360 -- Note that E_Operator is not in the list that follows, because
9361 -- this kind is reserved for predefined operators, that are
9362 -- intrinsic and do not need completion.
9364 elsif Ekind (E) = E_Function
9365 or else Ekind (E) = E_Procedure
9366 or else Ekind (E) = E_Generic_Function
9367 or else Ekind (E) = E_Generic_Procedure
9369 if Has_Completion (E) then
9372 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
9375 elsif Is_Subprogram (E)
9376 and then (not Comes_From_Source (E)
9377 or else Chars (E) = Name_uCall)
9382 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
9386 elsif Nkind (Parent (E)) = N_Procedure_Specification
9387 and then Null_Present (Parent (E))
9388 and then Serious_Errors_Detected > 0
9396 elsif Is_Entry (E) then
9397 if not Has_Completion (E) and then
9398 (Ekind (Scope (E)) = E_Protected_Object
9399 or else Ekind (Scope (E)) = E_Protected_Type)
9404 elsif Is_Package_Or_Generic_Package (E) then
9405 if Unit_Requires_Body (E) then
9406 if not Has_Completion (E)
9407 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
9413 elsif not Is_Child_Unit (E) then
9414 May_Need_Implicit_Body (E);
9417 elsif Ekind (E) = E_Incomplete_Type
9418 and then No (Underlying_Type (E))
9422 elsif (Ekind (E) = E_Task_Type or else
9423 Ekind (E) = E_Protected_Type)
9424 and then not Has_Completion (E)
9428 -- A single task declared in the current scope is a constant, verify
9429 -- that the body of its anonymous type is in the same scope. If the
9430 -- task is defined elsewhere, this may be a renaming declaration for
9431 -- which no completion is needed.
9433 elsif Ekind (E) = E_Constant
9434 and then Ekind (Etype (E)) = E_Task_Type
9435 and then not Has_Completion (Etype (E))
9436 and then Scope (Etype (E)) = Current_Scope
9440 elsif Ekind (E) = E_Protected_Object
9441 and then not Has_Completion (Etype (E))
9445 elsif Ekind (E) = E_Record_Type then
9446 if Is_Tagged_Type (E) then
9447 Check_Abstract_Overriding (E);
9448 Check_Conventions (E);
9451 Check_Aliased_Component_Types (E);
9453 elsif Ekind (E) = E_Array_Type then
9454 Check_Aliased_Component_Types (E);
9460 end Check_Completion;
9462 ----------------------------
9463 -- Check_Delta_Expression --
9464 ----------------------------
9466 procedure Check_Delta_Expression (E : Node_Id) is
9468 if not (Is_Real_Type (Etype (E))) then
9469 Wrong_Type (E, Any_Real);
9471 elsif not Is_OK_Static_Expression (E) then
9472 Flag_Non_Static_Expr
9473 ("non-static expression used for delta value!", E);
9475 elsif not UR_Is_Positive (Expr_Value_R (E)) then
9476 Error_Msg_N ("delta expression must be positive", E);
9482 -- If any of above errors occurred, then replace the incorrect
9483 -- expression by the real 0.1, which should prevent further errors.
9486 Make_Real_Literal (Sloc (E), Ureal_Tenth));
9487 Analyze_And_Resolve (E, Standard_Float);
9488 end Check_Delta_Expression;
9490 -----------------------------
9491 -- Check_Digits_Expression --
9492 -----------------------------
9494 procedure Check_Digits_Expression (E : Node_Id) is
9496 if not (Is_Integer_Type (Etype (E))) then
9497 Wrong_Type (E, Any_Integer);
9499 elsif not Is_OK_Static_Expression (E) then
9500 Flag_Non_Static_Expr
9501 ("non-static expression used for digits value!", E);
9503 elsif Expr_Value (E) <= 0 then
9504 Error_Msg_N ("digits value must be greater than zero", E);
9510 -- If any of above errors occurred, then replace the incorrect
9511 -- expression by the integer 1, which should prevent further errors.
9513 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
9514 Analyze_And_Resolve (E, Standard_Integer);
9516 end Check_Digits_Expression;
9518 --------------------------
9519 -- Check_Initialization --
9520 --------------------------
9522 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
9524 if Is_Limited_Type (T)
9525 and then not In_Instance
9526 and then not In_Inlined_Body
9528 if not OK_For_Limited_Init (T, Exp) then
9530 -- In GNAT mode, this is just a warning, to allow it to be evilly
9531 -- turned off. Otherwise it is a real error.
9535 ("?cannot initialize entities of limited type!", Exp);
9537 elsif Ada_Version < Ada_2005 then
9539 ("cannot initialize entities of limited type", Exp);
9540 Explain_Limited_Type (T, Exp);
9543 -- Specialize error message according to kind of illegal
9544 -- initial expression.
9546 if Nkind (Exp) = N_Type_Conversion
9547 and then Nkind (Expression (Exp)) = N_Function_Call
9550 ("illegal context for call"
9551 & " to function with limited result", Exp);
9555 ("initialization of limited object requires aggregate "
9556 & "or function call", Exp);
9561 end Check_Initialization;
9563 ----------------------
9564 -- Check_Interfaces --
9565 ----------------------
9567 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
9568 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
9571 Iface_Def : Node_Id;
9572 Iface_Typ : Entity_Id;
9573 Parent_Node : Node_Id;
9575 Is_Task : Boolean := False;
9576 -- Set True if parent type or any progenitor is a task interface
9578 Is_Protected : Boolean := False;
9579 -- Set True if parent type or any progenitor is a protected interface
9581 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
9582 -- Check that a progenitor is compatible with declaration.
9583 -- Error is posted on Error_Node.
9589 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
9590 Iface_Id : constant Entity_Id :=
9591 Defining_Identifier (Parent (Iface_Def));
9595 if Nkind (N) = N_Private_Extension_Declaration then
9598 Type_Def := Type_Definition (N);
9601 if Is_Task_Interface (Iface_Id) then
9604 elsif Is_Protected_Interface (Iface_Id) then
9605 Is_Protected := True;
9608 if Is_Synchronized_Interface (Iface_Id) then
9610 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
9611 -- extension derived from a synchronized interface must explicitly
9612 -- be declared synchronized, because the full view will be a
9613 -- synchronized type.
9615 if Nkind (N) = N_Private_Extension_Declaration then
9616 if not Synchronized_Present (N) then
9618 ("private extension of& must be explicitly synchronized",
9622 -- However, by 3.9.4(16/2), a full type that is a record extension
9623 -- is never allowed to derive from a synchronized interface (note
9624 -- that interfaces must be excluded from this check, because those
9625 -- are represented by derived type definitions in some cases).
9627 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9628 and then not Interface_Present (Type_Definition (N))
9630 Error_Msg_N ("record extension cannot derive from synchronized"
9631 & " interface", Error_Node);
9635 -- Check that the characteristics of the progenitor are compatible
9636 -- with the explicit qualifier in the declaration.
9637 -- The check only applies to qualifiers that come from source.
9638 -- Limited_Present also appears in the declaration of corresponding
9639 -- records, and the check does not apply to them.
9641 if Limited_Present (Type_Def)
9643 Is_Concurrent_Record_Type (Defining_Identifier (N))
9645 if Is_Limited_Interface (Parent_Type)
9646 and then not Is_Limited_Interface (Iface_Id)
9649 ("progenitor& must be limited interface",
9650 Error_Node, Iface_Id);
9653 (Task_Present (Iface_Def)
9654 or else Protected_Present (Iface_Def)
9655 or else Synchronized_Present (Iface_Def))
9656 and then Nkind (N) /= N_Private_Extension_Declaration
9657 and then not Error_Posted (N)
9660 ("progenitor& must be limited interface",
9661 Error_Node, Iface_Id);
9664 -- Protected interfaces can only inherit from limited, synchronized
9665 -- or protected interfaces.
9667 elsif Nkind (N) = N_Full_Type_Declaration
9668 and then Protected_Present (Type_Def)
9670 if Limited_Present (Iface_Def)
9671 or else Synchronized_Present (Iface_Def)
9672 or else Protected_Present (Iface_Def)
9676 elsif Task_Present (Iface_Def) then
9677 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9678 & " from task interface", Error_Node);
9681 Error_Msg_N ("(Ada 2005) protected interface cannot inherit"
9682 & " from non-limited interface", Error_Node);
9685 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
9686 -- limited and synchronized.
9688 elsif Synchronized_Present (Type_Def) then
9689 if Limited_Present (Iface_Def)
9690 or else Synchronized_Present (Iface_Def)
9694 elsif Protected_Present (Iface_Def)
9695 and then Nkind (N) /= N_Private_Extension_Declaration
9697 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9698 & " from protected interface", Error_Node);
9700 elsif Task_Present (Iface_Def)
9701 and then Nkind (N) /= N_Private_Extension_Declaration
9703 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9704 & " from task interface", Error_Node);
9706 elsif not Is_Limited_Interface (Iface_Id) then
9707 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit"
9708 & " from non-limited interface", Error_Node);
9711 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
9712 -- synchronized or task interfaces.
9714 elsif Nkind (N) = N_Full_Type_Declaration
9715 and then Task_Present (Type_Def)
9717 if Limited_Present (Iface_Def)
9718 or else Synchronized_Present (Iface_Def)
9719 or else Task_Present (Iface_Def)
9723 elsif Protected_Present (Iface_Def) then
9724 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9725 & " protected interface", Error_Node);
9728 Error_Msg_N ("(Ada 2005) task interface cannot inherit from"
9729 & " non-limited interface", Error_Node);
9734 -- Start of processing for Check_Interfaces
9737 if Is_Interface (Parent_Type) then
9738 if Is_Task_Interface (Parent_Type) then
9741 elsif Is_Protected_Interface (Parent_Type) then
9742 Is_Protected := True;
9746 if Nkind (N) = N_Private_Extension_Declaration then
9748 -- Check that progenitors are compatible with declaration
9750 Iface := First (Interface_List (Def));
9751 while Present (Iface) loop
9752 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9754 Parent_Node := Parent (Base_Type (Iface_Typ));
9755 Iface_Def := Type_Definition (Parent_Node);
9757 if not Is_Interface (Iface_Typ) then
9758 Diagnose_Interface (Iface, Iface_Typ);
9761 Check_Ifaces (Iface_Def, Iface);
9767 if Is_Task and Is_Protected then
9769 ("type cannot derive from task and protected interface", N);
9775 -- Full type declaration of derived type.
9776 -- Check compatibility with parent if it is interface type
9778 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
9779 and then Is_Interface (Parent_Type)
9781 Parent_Node := Parent (Parent_Type);
9783 -- More detailed checks for interface varieties
9786 (Iface_Def => Type_Definition (Parent_Node),
9787 Error_Node => Subtype_Indication (Type_Definition (N)));
9790 Iface := First (Interface_List (Def));
9791 while Present (Iface) loop
9792 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
9794 Parent_Node := Parent (Base_Type (Iface_Typ));
9795 Iface_Def := Type_Definition (Parent_Node);
9797 if not Is_Interface (Iface_Typ) then
9798 Diagnose_Interface (Iface, Iface_Typ);
9801 -- "The declaration of a specific descendant of an interface
9802 -- type freezes the interface type" RM 13.14
9804 Freeze_Before (N, Iface_Typ);
9805 Check_Ifaces (Iface_Def, Error_Node => Iface);
9811 if Is_Task and Is_Protected then
9813 ("type cannot derive from task and protected interface", N);
9815 end Check_Interfaces;
9817 ------------------------------------
9818 -- Check_Or_Process_Discriminants --
9819 ------------------------------------
9821 -- If an incomplete or private type declaration was already given for the
9822 -- type, the discriminants may have already been processed if they were
9823 -- present on the incomplete declaration. In this case a full conformance
9824 -- check has been performed in Find_Type_Name, and we then recheck here
9825 -- some properties that can't be checked on the partial view alone.
9826 -- Otherwise we call Process_Discriminants.
9828 procedure Check_Or_Process_Discriminants
9831 Prev : Entity_Id := Empty)
9834 if Has_Discriminants (T) then
9836 -- Discriminants are already set on T if they were already present
9837 -- on the partial view. Make them visible to component declarations.
9841 -- Discriminant on T (full view) referencing expr on partial view
9844 -- Entity of corresponding discriminant on partial view
9847 -- Discriminant specification for full view, expression is the
9848 -- syntactic copy on full view (which has been checked for
9849 -- conformance with partial view), only used here to post error
9853 D := First_Discriminant (T);
9854 New_D := First (Discriminant_Specifications (N));
9855 while Present (D) loop
9856 Prev_D := Current_Entity (D);
9857 Set_Current_Entity (D);
9858 Set_Is_Immediately_Visible (D);
9859 Set_Homonym (D, Prev_D);
9861 -- Handle the case where there is an untagged partial view and
9862 -- the full view is tagged: must disallow discriminants with
9863 -- defaults, unless compiling for Ada 2012, which allows a
9864 -- limited tagged type to have defaulted discriminants (see
9865 -- AI05-0214). However, suppress the error here if it was
9866 -- already reported on the default expression of the partial
9869 if Is_Tagged_Type (T)
9870 and then Present (Expression (Parent (D)))
9871 and then (not Is_Limited_Type (Current_Scope)
9872 or else Ada_Version < Ada_2012)
9873 and then not Error_Posted (Expression (Parent (D)))
9875 if Ada_Version >= Ada_2012 then
9877 ("discriminants of nonlimited tagged type cannot have"
9879 Expression (New_D));
9882 ("discriminants of tagged type cannot have defaults",
9883 Expression (New_D));
9887 -- Ada 2005 (AI-230): Access discriminant allowed in
9888 -- non-limited record types.
9890 if Ada_Version < Ada_2005 then
9892 -- This restriction gets applied to the full type here. It
9893 -- has already been applied earlier to the partial view.
9895 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
9898 Next_Discriminant (D);
9903 elsif Present (Discriminant_Specifications (N)) then
9904 Process_Discriminants (N, Prev);
9906 end Check_Or_Process_Discriminants;
9908 ----------------------
9909 -- Check_Real_Bound --
9910 ----------------------
9912 procedure Check_Real_Bound (Bound : Node_Id) is
9914 if not Is_Real_Type (Etype (Bound)) then
9916 ("bound in real type definition must be of real type", Bound);
9918 elsif not Is_OK_Static_Expression (Bound) then
9919 Flag_Non_Static_Expr
9920 ("non-static expression used for real type bound!", Bound);
9927 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
9929 Resolve (Bound, Standard_Float);
9930 end Check_Real_Bound;
9932 ------------------------------
9933 -- Complete_Private_Subtype --
9934 ------------------------------
9936 procedure Complete_Private_Subtype
9939 Full_Base : Entity_Id;
9940 Related_Nod : Node_Id)
9942 Save_Next_Entity : Entity_Id;
9943 Save_Homonym : Entity_Id;
9946 -- Set semantic attributes for (implicit) private subtype completion.
9947 -- If the full type has no discriminants, then it is a copy of the full
9948 -- view of the base. Otherwise, it is a subtype of the base with a
9949 -- possible discriminant constraint. Save and restore the original
9950 -- Next_Entity field of full to ensure that the calls to Copy_Node
9951 -- do not corrupt the entity chain.
9953 -- Note that the type of the full view is the same entity as the type of
9954 -- the partial view. In this fashion, the subtype has access to the
9955 -- correct view of the parent.
9957 Save_Next_Entity := Next_Entity (Full);
9958 Save_Homonym := Homonym (Priv);
9960 case Ekind (Full_Base) is
9961 when E_Record_Type |
9967 Copy_Node (Priv, Full);
9969 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
9970 Set_First_Entity (Full, First_Entity (Full_Base));
9971 Set_Last_Entity (Full, Last_Entity (Full_Base));
9974 Copy_Node (Full_Base, Full);
9975 Set_Chars (Full, Chars (Priv));
9976 Conditional_Delay (Full, Priv);
9977 Set_Sloc (Full, Sloc (Priv));
9980 Set_Next_Entity (Full, Save_Next_Entity);
9981 Set_Homonym (Full, Save_Homonym);
9982 Set_Associated_Node_For_Itype (Full, Related_Nod);
9984 -- Set common attributes for all subtypes: kind, convention, etc.
9986 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
9987 Set_Convention (Full, Convention (Full_Base));
9989 -- The Etype of the full view is inconsistent. Gigi needs to see the
9990 -- structural full view, which is what the current scheme gives:
9991 -- the Etype of the full view is the etype of the full base. However,
9992 -- if the full base is a derived type, the full view then looks like
9993 -- a subtype of the parent, not a subtype of the full base. If instead
9996 -- Set_Etype (Full, Full_Base);
9998 -- then we get inconsistencies in the front-end (confusion between
9999 -- views). Several outstanding bugs are related to this ???
10001 Set_Is_First_Subtype (Full, False);
10002 Set_Scope (Full, Scope (Priv));
10003 Set_Size_Info (Full, Full_Base);
10004 Set_RM_Size (Full, RM_Size (Full_Base));
10005 Set_Is_Itype (Full);
10007 -- A subtype of a private-type-without-discriminants, whose full-view
10008 -- has discriminants with default expressions, is not constrained!
10010 if not Has_Discriminants (Priv) then
10011 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
10013 if Has_Discriminants (Full_Base) then
10014 Set_Discriminant_Constraint
10015 (Full, Discriminant_Constraint (Full_Base));
10017 -- The partial view may have been indefinite, the full view
10020 Set_Has_Unknown_Discriminants
10021 (Full, Has_Unknown_Discriminants (Full_Base));
10025 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
10026 Set_Depends_On_Private (Full, Has_Private_Component (Full));
10028 -- Freeze the private subtype entity if its parent is delayed, and not
10029 -- already frozen. We skip this processing if the type is an anonymous
10030 -- subtype of a record component, or is the corresponding record of a
10031 -- protected type, since ???
10033 if not Is_Type (Scope (Full)) then
10034 Set_Has_Delayed_Freeze (Full,
10035 Has_Delayed_Freeze (Full_Base)
10036 and then (not Is_Frozen (Full_Base)));
10039 Set_Freeze_Node (Full, Empty);
10040 Set_Is_Frozen (Full, False);
10041 Set_Full_View (Priv, Full);
10043 if Has_Discriminants (Full) then
10044 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
10045 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
10047 if Has_Unknown_Discriminants (Full) then
10048 Set_Discriminant_Constraint (Full, No_Elist);
10052 if Ekind (Full_Base) = E_Record_Type
10053 and then Has_Discriminants (Full_Base)
10054 and then Has_Discriminants (Priv) -- might not, if errors
10055 and then not Has_Unknown_Discriminants (Priv)
10056 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
10058 Create_Constrained_Components
10059 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
10061 -- If the full base is itself derived from private, build a congruent
10062 -- subtype of its underlying type, for use by the back end. For a
10063 -- constrained record component, the declaration cannot be placed on
10064 -- the component list, but it must nevertheless be built an analyzed, to
10065 -- supply enough information for Gigi to compute the size of component.
10067 elsif Ekind (Full_Base) in Private_Kind
10068 and then Is_Derived_Type (Full_Base)
10069 and then Has_Discriminants (Full_Base)
10070 and then (Ekind (Current_Scope) /= E_Record_Subtype)
10072 if not Is_Itype (Priv)
10074 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
10076 Build_Underlying_Full_View
10077 (Parent (Priv), Full, Etype (Full_Base));
10079 elsif Nkind (Related_Nod) = N_Component_Declaration then
10080 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
10083 elsif Is_Record_Type (Full_Base) then
10085 -- Show Full is simply a renaming of Full_Base
10087 Set_Cloned_Subtype (Full, Full_Base);
10090 -- It is unsafe to share to bounds of a scalar type, because the Itype
10091 -- is elaborated on demand, and if a bound is non-static then different
10092 -- orders of elaboration in different units will lead to different
10093 -- external symbols.
10095 if Is_Scalar_Type (Full_Base) then
10096 Set_Scalar_Range (Full,
10097 Make_Range (Sloc (Related_Nod),
10099 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
10101 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
10103 -- This completion inherits the bounds of the full parent, but if
10104 -- the parent is an unconstrained floating point type, so is the
10107 if Is_Floating_Point_Type (Full_Base) then
10108 Set_Includes_Infinities
10109 (Scalar_Range (Full), Has_Infinities (Full_Base));
10113 -- ??? It seems that a lot of fields are missing that should be copied
10114 -- from Full_Base to Full. Here are some that are introduced in a
10115 -- non-disruptive way but a cleanup is necessary.
10117 if Is_Tagged_Type (Full_Base) then
10118 Set_Is_Tagged_Type (Full);
10119 Set_Direct_Primitive_Operations (Full,
10120 Direct_Primitive_Operations (Full_Base));
10122 -- Inherit class_wide type of full_base in case the partial view was
10123 -- not tagged. Otherwise it has already been created when the private
10124 -- subtype was analyzed.
10126 if No (Class_Wide_Type (Full)) then
10127 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
10130 -- If this is a subtype of a protected or task type, constrain its
10131 -- corresponding record, unless this is a subtype without constraints,
10132 -- i.e. a simple renaming as with an actual subtype in an instance.
10134 elsif Is_Concurrent_Type (Full_Base) then
10135 if Has_Discriminants (Full)
10136 and then Present (Corresponding_Record_Type (Full_Base))
10138 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
10140 Set_Corresponding_Record_Type (Full,
10141 Constrain_Corresponding_Record
10142 (Full, Corresponding_Record_Type (Full_Base),
10143 Related_Nod, Full_Base));
10146 Set_Corresponding_Record_Type (Full,
10147 Corresponding_Record_Type (Full_Base));
10151 -- Link rep item chain, and also setting of Has_Predicates from private
10152 -- subtype to full subtype, since we will need these on the full subtype
10153 -- to create the predicate function. Note that the full subtype may
10154 -- already have rep items, inherited from the full view of the base
10155 -- type, so we must be sure not to overwrite these entries.
10159 Next_Item : Node_Id;
10162 Item := First_Rep_Item (Full);
10164 -- If no existing rep items on full type, we can just link directly
10165 -- to the list of items on the private type.
10168 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
10170 -- Else search to end of items currently linked to the full subtype
10174 Next_Item := Next_Rep_Item (Item);
10175 exit when No (Next_Item);
10179 -- And link the private type items at the end of the chain
10181 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
10185 -- Make sure Has_Predicates is set on full type if it is set on the
10186 -- private type. Note that it may already be set on the full type and
10187 -- if so, we don't want to unset it.
10189 if Has_Predicates (Priv) then
10190 Set_Has_Predicates (Full);
10192 end Complete_Private_Subtype;
10194 ----------------------------
10195 -- Constant_Redeclaration --
10196 ----------------------------
10198 procedure Constant_Redeclaration
10203 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
10204 Obj_Def : constant Node_Id := Object_Definition (N);
10207 procedure Check_Possible_Deferred_Completion
10208 (Prev_Id : Entity_Id;
10209 Prev_Obj_Def : Node_Id;
10210 Curr_Obj_Def : Node_Id);
10211 -- Determine whether the two object definitions describe the partial
10212 -- and the full view of a constrained deferred constant. Generate
10213 -- a subtype for the full view and verify that it statically matches
10214 -- the subtype of the partial view.
10216 procedure Check_Recursive_Declaration (Typ : Entity_Id);
10217 -- If deferred constant is an access type initialized with an allocator,
10218 -- check whether there is an illegal recursion in the definition,
10219 -- through a default value of some record subcomponent. This is normally
10220 -- detected when generating init procs, but requires this additional
10221 -- mechanism when expansion is disabled.
10223 ----------------------------------------
10224 -- Check_Possible_Deferred_Completion --
10225 ----------------------------------------
10227 procedure Check_Possible_Deferred_Completion
10228 (Prev_Id : Entity_Id;
10229 Prev_Obj_Def : Node_Id;
10230 Curr_Obj_Def : Node_Id)
10233 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
10234 and then Present (Constraint (Prev_Obj_Def))
10235 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
10236 and then Present (Constraint (Curr_Obj_Def))
10239 Loc : constant Source_Ptr := Sloc (N);
10240 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
10241 Decl : constant Node_Id :=
10242 Make_Subtype_Declaration (Loc,
10243 Defining_Identifier => Def_Id,
10244 Subtype_Indication =>
10245 Relocate_Node (Curr_Obj_Def));
10248 Insert_Before_And_Analyze (N, Decl);
10249 Set_Etype (Id, Def_Id);
10251 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
10252 Error_Msg_Sloc := Sloc (Prev_Id);
10253 Error_Msg_N ("subtype does not statically match deferred " &
10254 "declaration#", N);
10258 end Check_Possible_Deferred_Completion;
10260 ---------------------------------
10261 -- Check_Recursive_Declaration --
10262 ---------------------------------
10264 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
10268 if Is_Record_Type (Typ) then
10269 Comp := First_Component (Typ);
10270 while Present (Comp) loop
10271 if Comes_From_Source (Comp) then
10272 if Present (Expression (Parent (Comp)))
10273 and then Is_Entity_Name (Expression (Parent (Comp)))
10274 and then Entity (Expression (Parent (Comp))) = Prev
10276 Error_Msg_Sloc := Sloc (Parent (Comp));
10278 ("illegal circularity with declaration for&#",
10282 elsif Is_Record_Type (Etype (Comp)) then
10283 Check_Recursive_Declaration (Etype (Comp));
10287 Next_Component (Comp);
10290 end Check_Recursive_Declaration;
10292 -- Start of processing for Constant_Redeclaration
10295 if Nkind (Parent (Prev)) = N_Object_Declaration then
10296 if Nkind (Object_Definition
10297 (Parent (Prev))) = N_Subtype_Indication
10299 -- Find type of new declaration. The constraints of the two
10300 -- views must match statically, but there is no point in
10301 -- creating an itype for the full view.
10303 if Nkind (Obj_Def) = N_Subtype_Indication then
10304 Find_Type (Subtype_Mark (Obj_Def));
10305 New_T := Entity (Subtype_Mark (Obj_Def));
10308 Find_Type (Obj_Def);
10309 New_T := Entity (Obj_Def);
10315 -- The full view may impose a constraint, even if the partial
10316 -- view does not, so construct the subtype.
10318 New_T := Find_Type_Of_Object (Obj_Def, N);
10323 -- Current declaration is illegal, diagnosed below in Enter_Name
10329 -- If previous full declaration or a renaming declaration exists, or if
10330 -- a homograph is present, let Enter_Name handle it, either with an
10331 -- error or with the removal of an overridden implicit subprogram.
10333 if Ekind (Prev) /= E_Constant
10334 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
10335 or else Present (Expression (Parent (Prev)))
10336 or else Present (Full_View (Prev))
10340 -- Verify that types of both declarations match, or else that both types
10341 -- are anonymous access types whose designated subtypes statically match
10342 -- (as allowed in Ada 2005 by AI-385).
10344 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
10346 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
10347 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
10348 or else Is_Access_Constant (Etype (New_T)) /=
10349 Is_Access_Constant (Etype (Prev))
10350 or else Can_Never_Be_Null (Etype (New_T)) /=
10351 Can_Never_Be_Null (Etype (Prev))
10352 or else Null_Exclusion_Present (Parent (Prev)) /=
10353 Null_Exclusion_Present (Parent (Id))
10354 or else not Subtypes_Statically_Match
10355 (Designated_Type (Etype (Prev)),
10356 Designated_Type (Etype (New_T))))
10358 Error_Msg_Sloc := Sloc (Prev);
10359 Error_Msg_N ("type does not match declaration#", N);
10360 Set_Full_View (Prev, Id);
10361 Set_Etype (Id, Any_Type);
10364 Null_Exclusion_Present (Parent (Prev))
10365 and then not Null_Exclusion_Present (N)
10367 Error_Msg_Sloc := Sloc (Prev);
10368 Error_Msg_N ("null-exclusion does not match declaration#", N);
10369 Set_Full_View (Prev, Id);
10370 Set_Etype (Id, Any_Type);
10372 -- If so, process the full constant declaration
10375 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
10376 -- the deferred declaration is constrained, then the subtype defined
10377 -- by the subtype_indication in the full declaration shall match it
10380 Check_Possible_Deferred_Completion
10382 Prev_Obj_Def => Object_Definition (Parent (Prev)),
10383 Curr_Obj_Def => Obj_Def);
10385 Set_Full_View (Prev, Id);
10386 Set_Is_Public (Id, Is_Public (Prev));
10387 Set_Is_Internal (Id);
10388 Append_Entity (Id, Current_Scope);
10390 -- Check ALIASED present if present before (RM 7.4(7))
10392 if Is_Aliased (Prev)
10393 and then not Aliased_Present (N)
10395 Error_Msg_Sloc := Sloc (Prev);
10396 Error_Msg_N ("ALIASED required (see declaration#)", N);
10399 -- Check that placement is in private part and that the incomplete
10400 -- declaration appeared in the visible part.
10402 if Ekind (Current_Scope) = E_Package
10403 and then not In_Private_Part (Current_Scope)
10405 Error_Msg_Sloc := Sloc (Prev);
10407 ("full constant for declaration#"
10408 & " must be in private part", N);
10410 elsif Ekind (Current_Scope) = E_Package
10412 List_Containing (Parent (Prev)) /=
10413 Visible_Declarations
10414 (Specification (Unit_Declaration_Node (Current_Scope)))
10417 ("deferred constant must be declared in visible part",
10421 if Is_Access_Type (T)
10422 and then Nkind (Expression (N)) = N_Allocator
10424 Check_Recursive_Declaration (Designated_Type (T));
10427 end Constant_Redeclaration;
10429 ----------------------
10430 -- Constrain_Access --
10431 ----------------------
10433 procedure Constrain_Access
10434 (Def_Id : in out Entity_Id;
10436 Related_Nod : Node_Id)
10438 T : constant Entity_Id := Entity (Subtype_Mark (S));
10439 Desig_Type : constant Entity_Id := Designated_Type (T);
10440 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
10441 Constraint_OK : Boolean := True;
10443 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean;
10444 -- Simple predicate to test for defaulted discriminants
10445 -- Shouldn't this be in sem_util???
10447 ---------------------------------
10448 -- Has_Defaulted_Discriminants --
10449 ---------------------------------
10451 function Has_Defaulted_Discriminants (Typ : Entity_Id) return Boolean is
10453 return Has_Discriminants (Typ)
10454 and then Present (First_Discriminant (Typ))
10456 (Discriminant_Default_Value (First_Discriminant (Typ)));
10457 end Has_Defaulted_Discriminants;
10459 -- Start of processing for Constrain_Access
10462 if Is_Array_Type (Desig_Type) then
10463 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
10465 elsif (Is_Record_Type (Desig_Type)
10466 or else Is_Incomplete_Or_Private_Type (Desig_Type))
10467 and then not Is_Constrained (Desig_Type)
10469 -- ??? The following code is a temporary kludge to ignore a
10470 -- discriminant constraint on access type if it is constraining
10471 -- the current record. Avoid creating the implicit subtype of the
10472 -- record we are currently compiling since right now, we cannot
10473 -- handle these. For now, just return the access type itself.
10475 if Desig_Type = Current_Scope
10476 and then No (Def_Id)
10478 Set_Ekind (Desig_Subtype, E_Record_Subtype);
10479 Def_Id := Entity (Subtype_Mark (S));
10481 -- This call added to ensure that the constraint is analyzed
10482 -- (needed for a B test). Note that we still return early from
10483 -- this procedure to avoid recursive processing. ???
10485 Constrain_Discriminated_Type
10486 (Desig_Subtype, S, Related_Nod, For_Access => True);
10490 if (Ekind (T) = E_General_Access_Type
10491 or else Ada_Version >= Ada_2005)
10492 and then Has_Private_Declaration (Desig_Type)
10493 and then In_Open_Scopes (Scope (Desig_Type))
10494 and then Has_Discriminants (Desig_Type)
10496 -- Enforce rule that the constraint is illegal if there is
10497 -- an unconstrained view of the designated type. This means
10498 -- that the partial view (either a private type declaration or
10499 -- a derivation from a private type) has no discriminants.
10500 -- (Defect Report 8652/0008, Technical Corrigendum 1, checked
10501 -- by ACATS B371001).
10503 -- Rule updated for Ada 2005: the private type is said to have
10504 -- a constrained partial view, given that objects of the type
10505 -- can be declared. Furthermore, the rule applies to all access
10506 -- types, unlike the rule concerning default discriminants.
10509 Pack : constant Node_Id :=
10510 Unit_Declaration_Node (Scope (Desig_Type));
10515 if Nkind (Pack) = N_Package_Declaration then
10516 Decls := Visible_Declarations (Specification (Pack));
10517 Decl := First (Decls);
10518 while Present (Decl) loop
10519 if (Nkind (Decl) = N_Private_Type_Declaration
10521 Chars (Defining_Identifier (Decl)) =
10522 Chars (Desig_Type))
10525 (Nkind (Decl) = N_Full_Type_Declaration
10527 Chars (Defining_Identifier (Decl)) =
10529 and then Is_Derived_Type (Desig_Type)
10531 Has_Private_Declaration (Etype (Desig_Type)))
10533 if No (Discriminant_Specifications (Decl)) then
10535 ("cannot constrain general access type if " &
10536 "designated type has constrained partial view",
10549 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
10550 For_Access => True);
10552 elsif (Is_Task_Type (Desig_Type)
10553 or else Is_Protected_Type (Desig_Type))
10554 and then not Is_Constrained (Desig_Type)
10556 Constrain_Concurrent
10557 (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
10560 Error_Msg_N ("invalid constraint on access type", S);
10561 Desig_Subtype := Desig_Type; -- Ignore invalid constraint.
10562 Constraint_OK := False;
10565 if No (Def_Id) then
10566 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
10568 Set_Ekind (Def_Id, E_Access_Subtype);
10571 if Constraint_OK then
10572 Set_Etype (Def_Id, Base_Type (T));
10574 if Is_Private_Type (Desig_Type) then
10575 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
10578 Set_Etype (Def_Id, Any_Type);
10581 Set_Size_Info (Def_Id, T);
10582 Set_Is_Constrained (Def_Id, Constraint_OK);
10583 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
10584 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10585 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
10587 Conditional_Delay (Def_Id, T);
10589 -- AI-363 : Subtypes of general access types whose designated types have
10590 -- default discriminants are disallowed. In instances, the rule has to
10591 -- be checked against the actual, of which T is the subtype. In a
10592 -- generic body, the rule is checked assuming that the actual type has
10593 -- defaulted discriminants.
10595 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
10596 if Ekind (Base_Type (T)) = E_General_Access_Type
10597 and then Has_Defaulted_Discriminants (Desig_Type)
10599 if Ada_Version < Ada_2005 then
10601 ("access subtype of general access type would not " &
10602 "be allowed in Ada 2005?", S);
10605 ("access subtype of general access type not allowed", S);
10608 Error_Msg_N ("\discriminants have defaults", S);
10610 elsif Is_Access_Type (T)
10611 and then Is_Generic_Type (Desig_Type)
10612 and then Has_Discriminants (Desig_Type)
10613 and then In_Package_Body (Current_Scope)
10615 if Ada_Version < Ada_2005 then
10617 ("access subtype would not be allowed in generic body " &
10618 "in Ada 2005?", S);
10621 ("access subtype not allowed in generic body", S);
10625 ("\designated type is a discriminated formal", S);
10628 end Constrain_Access;
10630 ---------------------
10631 -- Constrain_Array --
10632 ---------------------
10634 procedure Constrain_Array
10635 (Def_Id : in out Entity_Id;
10637 Related_Nod : Node_Id;
10638 Related_Id : Entity_Id;
10639 Suffix : Character)
10641 C : constant Node_Id := Constraint (SI);
10642 Number_Of_Constraints : Nat := 0;
10645 Constraint_OK : Boolean := True;
10648 T := Entity (Subtype_Mark (SI));
10650 if Ekind (T) in Access_Kind then
10651 T := Designated_Type (T);
10654 -- If an index constraint follows a subtype mark in a subtype indication
10655 -- then the type or subtype denoted by the subtype mark must not already
10656 -- impose an index constraint. The subtype mark must denote either an
10657 -- unconstrained array type or an access type whose designated type
10658 -- is such an array type... (RM 3.6.1)
10660 if Is_Constrained (T) then
10661 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
10662 Constraint_OK := False;
10665 S := First (Constraints (C));
10666 while Present (S) loop
10667 Number_Of_Constraints := Number_Of_Constraints + 1;
10671 -- In either case, the index constraint must provide a discrete
10672 -- range for each index of the array type and the type of each
10673 -- discrete range must be the same as that of the corresponding
10674 -- index. (RM 3.6.1)
10676 if Number_Of_Constraints /= Number_Dimensions (T) then
10677 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
10678 Constraint_OK := False;
10681 S := First (Constraints (C));
10682 Index := First_Index (T);
10685 -- Apply constraints to each index type
10687 for J in 1 .. Number_Of_Constraints loop
10688 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
10696 if No (Def_Id) then
10698 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
10699 Set_Parent (Def_Id, Related_Nod);
10702 Set_Ekind (Def_Id, E_Array_Subtype);
10705 Set_Size_Info (Def_Id, (T));
10706 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10707 Set_Etype (Def_Id, Base_Type (T));
10709 if Constraint_OK then
10710 Set_First_Index (Def_Id, First (Constraints (C)));
10712 Set_First_Index (Def_Id, First_Index (T));
10715 Set_Is_Constrained (Def_Id, True);
10716 Set_Is_Aliased (Def_Id, Is_Aliased (T));
10717 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
10719 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
10720 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
10722 -- A subtype does not inherit the packed_array_type of is parent. We
10723 -- need to initialize the attribute because if Def_Id is previously
10724 -- analyzed through a limited_with clause, it will have the attributes
10725 -- of an incomplete type, one of which is an Elist that overlaps the
10726 -- Packed_Array_Type field.
10728 Set_Packed_Array_Type (Def_Id, Empty);
10730 -- Build a freeze node if parent still needs one. Also make sure that
10731 -- the Depends_On_Private status is set because the subtype will need
10732 -- reprocessing at the time the base type does, and also we must set a
10733 -- conditional delay.
10735 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10736 Conditional_Delay (Def_Id, T);
10737 end Constrain_Array;
10739 ------------------------------
10740 -- Constrain_Component_Type --
10741 ------------------------------
10743 function Constrain_Component_Type
10745 Constrained_Typ : Entity_Id;
10746 Related_Node : Node_Id;
10748 Constraints : Elist_Id) return Entity_Id
10750 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
10751 Compon_Type : constant Entity_Id := Etype (Comp);
10753 function Build_Constrained_Array_Type
10754 (Old_Type : Entity_Id) return Entity_Id;
10755 -- If Old_Type is an array type, one of whose indexes is constrained
10756 -- by a discriminant, build an Itype whose constraint replaces the
10757 -- discriminant with its value in the constraint.
10759 function Build_Constrained_Discriminated_Type
10760 (Old_Type : Entity_Id) return Entity_Id;
10761 -- Ditto for record components
10763 function Build_Constrained_Access_Type
10764 (Old_Type : Entity_Id) return Entity_Id;
10765 -- Ditto for access types. Makes use of previous two functions, to
10766 -- constrain designated type.
10768 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
10769 -- T is an array or discriminated type, C is a list of constraints
10770 -- that apply to T. This routine builds the constrained subtype.
10772 function Is_Discriminant (Expr : Node_Id) return Boolean;
10773 -- Returns True if Expr is a discriminant
10775 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
10776 -- Find the value of discriminant Discrim in Constraint
10778 -----------------------------------
10779 -- Build_Constrained_Access_Type --
10780 -----------------------------------
10782 function Build_Constrained_Access_Type
10783 (Old_Type : Entity_Id) return Entity_Id
10785 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
10787 Desig_Subtype : Entity_Id;
10791 -- if the original access type was not embedded in the enclosing
10792 -- type definition, there is no need to produce a new access
10793 -- subtype. In fact every access type with an explicit constraint
10794 -- generates an itype whose scope is the enclosing record.
10796 if not Is_Type (Scope (Old_Type)) then
10799 elsif Is_Array_Type (Desig_Type) then
10800 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
10802 elsif Has_Discriminants (Desig_Type) then
10804 -- This may be an access type to an enclosing record type for
10805 -- which we are constructing the constrained components. Return
10806 -- the enclosing record subtype. This is not always correct,
10807 -- but avoids infinite recursion. ???
10809 Desig_Subtype := Any_Type;
10811 for J in reverse 0 .. Scope_Stack.Last loop
10812 Scop := Scope_Stack.Table (J).Entity;
10815 and then Base_Type (Scop) = Base_Type (Desig_Type)
10817 Desig_Subtype := Scop;
10820 exit when not Is_Type (Scop);
10823 if Desig_Subtype = Any_Type then
10825 Build_Constrained_Discriminated_Type (Desig_Type);
10832 if Desig_Subtype /= Desig_Type then
10834 -- The Related_Node better be here or else we won't be able
10835 -- to attach new itypes to a node in the tree.
10837 pragma Assert (Present (Related_Node));
10839 Itype := Create_Itype (E_Access_Subtype, Related_Node);
10841 Set_Etype (Itype, Base_Type (Old_Type));
10842 Set_Size_Info (Itype, (Old_Type));
10843 Set_Directly_Designated_Type (Itype, Desig_Subtype);
10844 Set_Depends_On_Private (Itype, Has_Private_Component
10846 Set_Is_Access_Constant (Itype, Is_Access_Constant
10849 -- The new itype needs freezing when it depends on a not frozen
10850 -- type and the enclosing subtype needs freezing.
10852 if Has_Delayed_Freeze (Constrained_Typ)
10853 and then not Is_Frozen (Constrained_Typ)
10855 Conditional_Delay (Itype, Base_Type (Old_Type));
10863 end Build_Constrained_Access_Type;
10865 ----------------------------------
10866 -- Build_Constrained_Array_Type --
10867 ----------------------------------
10869 function Build_Constrained_Array_Type
10870 (Old_Type : Entity_Id) return Entity_Id
10874 Old_Index : Node_Id;
10875 Range_Node : Node_Id;
10876 Constr_List : List_Id;
10878 Need_To_Create_Itype : Boolean := False;
10881 Old_Index := First_Index (Old_Type);
10882 while Present (Old_Index) loop
10883 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10885 if Is_Discriminant (Lo_Expr)
10886 or else Is_Discriminant (Hi_Expr)
10888 Need_To_Create_Itype := True;
10891 Next_Index (Old_Index);
10894 if Need_To_Create_Itype then
10895 Constr_List := New_List;
10897 Old_Index := First_Index (Old_Type);
10898 while Present (Old_Index) loop
10899 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
10901 if Is_Discriminant (Lo_Expr) then
10902 Lo_Expr := Get_Discr_Value (Lo_Expr);
10905 if Is_Discriminant (Hi_Expr) then
10906 Hi_Expr := Get_Discr_Value (Hi_Expr);
10911 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
10913 Append (Range_Node, To => Constr_List);
10915 Next_Index (Old_Index);
10918 return Build_Subtype (Old_Type, Constr_List);
10923 end Build_Constrained_Array_Type;
10925 ------------------------------------------
10926 -- Build_Constrained_Discriminated_Type --
10927 ------------------------------------------
10929 function Build_Constrained_Discriminated_Type
10930 (Old_Type : Entity_Id) return Entity_Id
10933 Constr_List : List_Id;
10934 Old_Constraint : Elmt_Id;
10936 Need_To_Create_Itype : Boolean := False;
10939 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10940 while Present (Old_Constraint) loop
10941 Expr := Node (Old_Constraint);
10943 if Is_Discriminant (Expr) then
10944 Need_To_Create_Itype := True;
10947 Next_Elmt (Old_Constraint);
10950 if Need_To_Create_Itype then
10951 Constr_List := New_List;
10953 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
10954 while Present (Old_Constraint) loop
10955 Expr := Node (Old_Constraint);
10957 if Is_Discriminant (Expr) then
10958 Expr := Get_Discr_Value (Expr);
10961 Append (New_Copy_Tree (Expr), To => Constr_List);
10963 Next_Elmt (Old_Constraint);
10966 return Build_Subtype (Old_Type, Constr_List);
10971 end Build_Constrained_Discriminated_Type;
10973 -------------------
10974 -- Build_Subtype --
10975 -------------------
10977 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
10979 Subtyp_Decl : Node_Id;
10980 Def_Id : Entity_Id;
10981 Btyp : Entity_Id := Base_Type (T);
10984 -- The Related_Node better be here or else we won't be able to
10985 -- attach new itypes to a node in the tree.
10987 pragma Assert (Present (Related_Node));
10989 -- If the view of the component's type is incomplete or private
10990 -- with unknown discriminants, then the constraint must be applied
10991 -- to the full type.
10993 if Has_Unknown_Discriminants (Btyp)
10994 and then Present (Underlying_Type (Btyp))
10996 Btyp := Underlying_Type (Btyp);
11000 Make_Subtype_Indication (Loc,
11001 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
11002 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
11004 Def_Id := Create_Itype (Ekind (T), Related_Node);
11007 Make_Subtype_Declaration (Loc,
11008 Defining_Identifier => Def_Id,
11009 Subtype_Indication => Indic);
11011 Set_Parent (Subtyp_Decl, Parent (Related_Node));
11013 -- Itypes must be analyzed with checks off (see package Itypes)
11015 Analyze (Subtyp_Decl, Suppress => All_Checks);
11020 ---------------------
11021 -- Get_Discr_Value --
11022 ---------------------
11024 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
11029 -- The discriminant may be declared for the type, in which case we
11030 -- find it by iterating over the list of discriminants. If the
11031 -- discriminant is inherited from a parent type, it appears as the
11032 -- corresponding discriminant of the current type. This will be the
11033 -- case when constraining an inherited component whose constraint is
11034 -- given by a discriminant of the parent.
11036 D := First_Discriminant (Typ);
11037 E := First_Elmt (Constraints);
11039 while Present (D) loop
11040 if D = Entity (Discrim)
11041 or else D = CR_Discriminant (Entity (Discrim))
11042 or else Corresponding_Discriminant (D) = Entity (Discrim)
11047 Next_Discriminant (D);
11051 -- The Corresponding_Discriminant mechanism is incomplete, because
11052 -- the correspondence between new and old discriminants is not one
11053 -- to one: one new discriminant can constrain several old ones. In
11054 -- that case, scan sequentially the stored_constraint, the list of
11055 -- discriminants of the parents, and the constraints.
11056 -- Previous code checked for the present of the Stored_Constraint
11057 -- list for the derived type, but did not use it at all. Should it
11058 -- be present when the component is a discriminated task type?
11060 if Is_Derived_Type (Typ)
11061 and then Scope (Entity (Discrim)) = Etype (Typ)
11063 D := First_Discriminant (Etype (Typ));
11064 E := First_Elmt (Constraints);
11065 while Present (D) loop
11066 if D = Entity (Discrim) then
11070 Next_Discriminant (D);
11075 -- Something is wrong if we did not find the value
11077 raise Program_Error;
11078 end Get_Discr_Value;
11080 ---------------------
11081 -- Is_Discriminant --
11082 ---------------------
11084 function Is_Discriminant (Expr : Node_Id) return Boolean is
11085 Discrim_Scope : Entity_Id;
11088 if Denotes_Discriminant (Expr) then
11089 Discrim_Scope := Scope (Entity (Expr));
11091 -- Either we have a reference to one of Typ's discriminants,
11093 pragma Assert (Discrim_Scope = Typ
11095 -- or to the discriminants of the parent type, in the case
11096 -- of a derivation of a tagged type with variants.
11098 or else Discrim_Scope = Etype (Typ)
11099 or else Full_View (Discrim_Scope) = Etype (Typ)
11101 -- or same as above for the case where the discriminants
11102 -- were declared in Typ's private view.
11104 or else (Is_Private_Type (Discrim_Scope)
11105 and then Chars (Discrim_Scope) = Chars (Typ))
11107 -- or else we are deriving from the full view and the
11108 -- discriminant is declared in the private entity.
11110 or else (Is_Private_Type (Typ)
11111 and then Chars (Discrim_Scope) = Chars (Typ))
11113 -- Or we are constrained the corresponding record of a
11114 -- synchronized type that completes a private declaration.
11116 or else (Is_Concurrent_Record_Type (Typ)
11118 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
11120 -- or we have a class-wide type, in which case make sure the
11121 -- discriminant found belongs to the root type.
11123 or else (Is_Class_Wide_Type (Typ)
11124 and then Etype (Typ) = Discrim_Scope));
11129 -- In all other cases we have something wrong
11132 end Is_Discriminant;
11134 -- Start of processing for Constrain_Component_Type
11137 if Nkind (Parent (Comp)) = N_Component_Declaration
11138 and then Comes_From_Source (Parent (Comp))
11139 and then Comes_From_Source
11140 (Subtype_Indication (Component_Definition (Parent (Comp))))
11143 (Subtype_Indication (Component_Definition (Parent (Comp))))
11145 return Compon_Type;
11147 elsif Is_Array_Type (Compon_Type) then
11148 return Build_Constrained_Array_Type (Compon_Type);
11150 elsif Has_Discriminants (Compon_Type) then
11151 return Build_Constrained_Discriminated_Type (Compon_Type);
11153 elsif Is_Access_Type (Compon_Type) then
11154 return Build_Constrained_Access_Type (Compon_Type);
11157 return Compon_Type;
11159 end Constrain_Component_Type;
11161 --------------------------
11162 -- Constrain_Concurrent --
11163 --------------------------
11165 -- For concurrent types, the associated record value type carries the same
11166 -- discriminants, so when we constrain a concurrent type, we must constrain
11167 -- the corresponding record type as well.
11169 procedure Constrain_Concurrent
11170 (Def_Id : in out Entity_Id;
11172 Related_Nod : Node_Id;
11173 Related_Id : Entity_Id;
11174 Suffix : Character)
11176 T_Ent : Entity_Id := Entity (Subtype_Mark (SI));
11180 if Ekind (T_Ent) in Access_Kind then
11181 T_Ent := Designated_Type (T_Ent);
11184 T_Val := Corresponding_Record_Type (T_Ent);
11186 if Present (T_Val) then
11188 if No (Def_Id) then
11189 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11192 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11194 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
11195 Set_Corresponding_Record_Type (Def_Id,
11196 Constrain_Corresponding_Record
11197 (Def_Id, T_Val, Related_Nod, Related_Id));
11200 -- If there is no associated record, expansion is disabled and this
11201 -- is a generic context. Create a subtype in any case, so that
11202 -- semantic analysis can proceed.
11204 if No (Def_Id) then
11205 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
11208 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
11210 end Constrain_Concurrent;
11212 ------------------------------------
11213 -- Constrain_Corresponding_Record --
11214 ------------------------------------
11216 function Constrain_Corresponding_Record
11217 (Prot_Subt : Entity_Id;
11218 Corr_Rec : Entity_Id;
11219 Related_Nod : Node_Id;
11220 Related_Id : Entity_Id) return Entity_Id
11222 T_Sub : constant Entity_Id :=
11223 Create_Itype (E_Record_Subtype, Related_Nod, Related_Id, 'V');
11226 Set_Etype (T_Sub, Corr_Rec);
11227 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
11228 Set_Is_Constrained (T_Sub, True);
11229 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
11230 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
11232 -- As elsewhere, we do not want to create a freeze node for this itype
11233 -- if it is created for a constrained component of an enclosing record
11234 -- because references to outer discriminants will appear out of scope.
11236 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
11237 Conditional_Delay (T_Sub, Corr_Rec);
11239 Set_Is_Frozen (T_Sub);
11242 if Has_Discriminants (Prot_Subt) then -- False only if errors.
11243 Set_Discriminant_Constraint
11244 (T_Sub, Discriminant_Constraint (Prot_Subt));
11245 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
11246 Create_Constrained_Components
11247 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
11250 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
11253 end Constrain_Corresponding_Record;
11255 -----------------------
11256 -- Constrain_Decimal --
11257 -----------------------
11259 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
11260 T : constant Entity_Id := Entity (Subtype_Mark (S));
11261 C : constant Node_Id := Constraint (S);
11262 Loc : constant Source_Ptr := Sloc (C);
11263 Range_Expr : Node_Id;
11264 Digits_Expr : Node_Id;
11269 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
11271 if Nkind (C) = N_Range_Constraint then
11272 Range_Expr := Range_Expression (C);
11273 Digits_Val := Digits_Value (T);
11276 pragma Assert (Nkind (C) = N_Digits_Constraint);
11278 Check_Formal_Restriction ("digits constraint is not allowed", S);
11280 Digits_Expr := Digits_Expression (C);
11281 Analyze_And_Resolve (Digits_Expr, Any_Integer);
11283 Check_Digits_Expression (Digits_Expr);
11284 Digits_Val := Expr_Value (Digits_Expr);
11286 if Digits_Val > Digits_Value (T) then
11288 ("digits expression is incompatible with subtype", C);
11289 Digits_Val := Digits_Value (T);
11292 if Present (Range_Constraint (C)) then
11293 Range_Expr := Range_Expression (Range_Constraint (C));
11295 Range_Expr := Empty;
11299 Set_Etype (Def_Id, Base_Type (T));
11300 Set_Size_Info (Def_Id, (T));
11301 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11302 Set_Delta_Value (Def_Id, Delta_Value (T));
11303 Set_Scale_Value (Def_Id, Scale_Value (T));
11304 Set_Small_Value (Def_Id, Small_Value (T));
11305 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
11306 Set_Digits_Value (Def_Id, Digits_Val);
11308 -- Manufacture range from given digits value if no range present
11310 if No (Range_Expr) then
11311 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
11315 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
11317 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
11320 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
11321 Set_Discrete_RM_Size (Def_Id);
11323 -- Unconditionally delay the freeze, since we cannot set size
11324 -- information in all cases correctly until the freeze point.
11326 Set_Has_Delayed_Freeze (Def_Id);
11327 end Constrain_Decimal;
11329 ----------------------------------
11330 -- Constrain_Discriminated_Type --
11331 ----------------------------------
11333 procedure Constrain_Discriminated_Type
11334 (Def_Id : Entity_Id;
11336 Related_Nod : Node_Id;
11337 For_Access : Boolean := False)
11339 E : constant Entity_Id := Entity (Subtype_Mark (S));
11342 Elist : Elist_Id := New_Elmt_List;
11344 procedure Fixup_Bad_Constraint;
11345 -- This is called after finding a bad constraint, and after having
11346 -- posted an appropriate error message. The mission is to leave the
11347 -- entity T in as reasonable state as possible!
11349 --------------------------
11350 -- Fixup_Bad_Constraint --
11351 --------------------------
11353 procedure Fixup_Bad_Constraint is
11355 -- Set a reasonable Ekind for the entity. For an incomplete type,
11356 -- we can't do much, but for other types, we can set the proper
11357 -- corresponding subtype kind.
11359 if Ekind (T) = E_Incomplete_Type then
11360 Set_Ekind (Def_Id, Ekind (T));
11362 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
11365 -- Set Etype to the known type, to reduce chances of cascaded errors
11367 Set_Etype (Def_Id, E);
11368 Set_Error_Posted (Def_Id);
11369 end Fixup_Bad_Constraint;
11371 -- Start of processing for Constrain_Discriminated_Type
11374 C := Constraint (S);
11376 -- A discriminant constraint is only allowed in a subtype indication,
11377 -- after a subtype mark. This subtype mark must denote either a type
11378 -- with discriminants, or an access type whose designated type is a
11379 -- type with discriminants. A discriminant constraint specifies the
11380 -- values of these discriminants (RM 3.7.2(5)).
11382 T := Base_Type (Entity (Subtype_Mark (S)));
11384 if Ekind (T) in Access_Kind then
11385 T := Designated_Type (T);
11388 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
11389 -- Avoid generating an error for access-to-incomplete subtypes.
11391 if Ada_Version >= Ada_2005
11392 and then Ekind (T) = E_Incomplete_Type
11393 and then Nkind (Parent (S)) = N_Subtype_Declaration
11394 and then not Is_Itype (Def_Id)
11396 -- A little sanity check, emit an error message if the type
11397 -- has discriminants to begin with. Type T may be a regular
11398 -- incomplete type or imported via a limited with clause.
11400 if Has_Discriminants (T)
11402 (From_With_Type (T)
11403 and then Present (Non_Limited_View (T))
11404 and then Nkind (Parent (Non_Limited_View (T))) =
11405 N_Full_Type_Declaration
11406 and then Present (Discriminant_Specifications
11407 (Parent (Non_Limited_View (T)))))
11410 ("(Ada 2005) incomplete subtype may not be constrained", C);
11412 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11415 Fixup_Bad_Constraint;
11418 -- Check that the type has visible discriminants. The type may be
11419 -- a private type with unknown discriminants whose full view has
11420 -- discriminants which are invisible.
11422 elsif not Has_Discriminants (T)
11424 (Has_Unknown_Discriminants (T)
11425 and then Is_Private_Type (T))
11427 Error_Msg_N ("invalid constraint: type has no discriminant", C);
11428 Fixup_Bad_Constraint;
11431 elsif Is_Constrained (E)
11432 or else (Ekind (E) = E_Class_Wide_Subtype
11433 and then Present (Discriminant_Constraint (E)))
11435 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
11436 Fixup_Bad_Constraint;
11440 -- T may be an unconstrained subtype (e.g. a generic actual).
11441 -- Constraint applies to the base type.
11443 T := Base_Type (T);
11445 Elist := Build_Discriminant_Constraints (T, S);
11447 -- If the list returned was empty we had an error in building the
11448 -- discriminant constraint. We have also already signalled an error
11449 -- in the incomplete type case
11451 if Is_Empty_Elmt_List (Elist) then
11452 Fixup_Bad_Constraint;
11456 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
11457 end Constrain_Discriminated_Type;
11459 ---------------------------
11460 -- Constrain_Enumeration --
11461 ---------------------------
11463 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
11464 T : constant Entity_Id := Entity (Subtype_Mark (S));
11465 C : constant Node_Id := Constraint (S);
11468 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11470 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
11472 Set_Etype (Def_Id, Base_Type (T));
11473 Set_Size_Info (Def_Id, (T));
11474 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11475 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11477 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11479 Set_Discrete_RM_Size (Def_Id);
11480 end Constrain_Enumeration;
11482 ----------------------
11483 -- Constrain_Float --
11484 ----------------------
11486 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
11487 T : constant Entity_Id := Entity (Subtype_Mark (S));
11493 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
11495 Set_Etype (Def_Id, Base_Type (T));
11496 Set_Size_Info (Def_Id, (T));
11497 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11499 -- Process the constraint
11501 C := Constraint (S);
11503 -- Digits constraint present
11505 if Nkind (C) = N_Digits_Constraint then
11507 Check_Formal_Restriction ("digits constraint is not allowed", S);
11508 Check_Restriction (No_Obsolescent_Features, C);
11510 if Warn_On_Obsolescent_Feature then
11512 ("subtype digits constraint is an " &
11513 "obsolescent feature (RM J.3(8))?", C);
11516 D := Digits_Expression (C);
11517 Analyze_And_Resolve (D, Any_Integer);
11518 Check_Digits_Expression (D);
11519 Set_Digits_Value (Def_Id, Expr_Value (D));
11521 -- Check that digits value is in range. Obviously we can do this
11522 -- at compile time, but it is strictly a runtime check, and of
11523 -- course there is an ACVC test that checks this!
11525 if Digits_Value (Def_Id) > Digits_Value (T) then
11526 Error_Msg_Uint_1 := Digits_Value (T);
11527 Error_Msg_N ("?digits value is too large, maximum is ^", D);
11529 Make_Raise_Constraint_Error (Sloc (D),
11530 Reason => CE_Range_Check_Failed);
11531 Insert_Action (Declaration_Node (Def_Id), Rais);
11534 C := Range_Constraint (C);
11536 -- No digits constraint present
11539 Set_Digits_Value (Def_Id, Digits_Value (T));
11542 -- Range constraint present
11544 if Nkind (C) = N_Range_Constraint then
11545 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11547 -- No range constraint present
11550 pragma Assert (No (C));
11551 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11554 Set_Is_Constrained (Def_Id);
11555 end Constrain_Float;
11557 ---------------------
11558 -- Constrain_Index --
11559 ---------------------
11561 procedure Constrain_Index
11564 Related_Nod : Node_Id;
11565 Related_Id : Entity_Id;
11566 Suffix : Character;
11567 Suffix_Index : Nat)
11569 Def_Id : Entity_Id;
11570 R : Node_Id := Empty;
11571 T : constant Entity_Id := Etype (Index);
11574 if Nkind (S) = N_Range
11576 (Nkind (S) = N_Attribute_Reference
11577 and then Attribute_Name (S) = Name_Range)
11579 -- A Range attribute will be transformed into N_Range by Resolve
11585 Process_Range_Expr_In_Decl (R, T, Empty_List);
11587 if not Error_Posted (S)
11589 (Nkind (S) /= N_Range
11590 or else not Covers (T, (Etype (Low_Bound (S))))
11591 or else not Covers (T, (Etype (High_Bound (S)))))
11593 if Base_Type (T) /= Any_Type
11594 and then Etype (Low_Bound (S)) /= Any_Type
11595 and then Etype (High_Bound (S)) /= Any_Type
11597 Error_Msg_N ("range expected", S);
11601 elsif Nkind (S) = N_Subtype_Indication then
11603 -- The parser has verified that this is a discrete indication
11605 Resolve_Discrete_Subtype_Indication (S, T);
11606 R := Range_Expression (Constraint (S));
11608 -- Capture values of bounds and generate temporaries for them if
11609 -- needed, since checks may cause duplication of the expressions
11610 -- which must not be reevaluated.
11612 if Expander_Active then
11613 Force_Evaluation (Low_Bound (R));
11614 Force_Evaluation (High_Bound (R));
11617 elsif Nkind (S) = N_Discriminant_Association then
11619 -- Syntactically valid in subtype indication
11621 Error_Msg_N ("invalid index constraint", S);
11622 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11625 -- Subtype_Mark case, no anonymous subtypes to construct
11630 if Is_Entity_Name (S) then
11631 if not Is_Type (Entity (S)) then
11632 Error_Msg_N ("expect subtype mark for index constraint", S);
11634 elsif Base_Type (Entity (S)) /= Base_Type (T) then
11635 Wrong_Type (S, Base_Type (T));
11637 -- Check error of subtype with predicate in index constraint
11640 Bad_Predicated_Subtype_Use
11641 ("subtype& has predicate, not allowed in index constraint",
11648 Error_Msg_N ("invalid index constraint", S);
11649 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
11655 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
11657 Set_Etype (Def_Id, Base_Type (T));
11659 if Is_Modular_Integer_Type (T) then
11660 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11662 elsif Is_Integer_Type (T) then
11663 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11666 Set_Ekind (Def_Id, E_Enumeration_Subtype);
11667 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
11668 Set_First_Literal (Def_Id, First_Literal (T));
11671 Set_Size_Info (Def_Id, (T));
11672 Set_RM_Size (Def_Id, RM_Size (T));
11673 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11675 Set_Scalar_Range (Def_Id, R);
11677 Set_Etype (S, Def_Id);
11678 Set_Discrete_RM_Size (Def_Id);
11679 end Constrain_Index;
11681 -----------------------
11682 -- Constrain_Integer --
11683 -----------------------
11685 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
11686 T : constant Entity_Id := Entity (Subtype_Mark (S));
11687 C : constant Node_Id := Constraint (S);
11690 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11692 if Is_Modular_Integer_Type (T) then
11693 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
11695 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
11698 Set_Etype (Def_Id, Base_Type (T));
11699 Set_Size_Info (Def_Id, (T));
11700 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11701 Set_Discrete_RM_Size (Def_Id);
11702 end Constrain_Integer;
11704 ------------------------------
11705 -- Constrain_Ordinary_Fixed --
11706 ------------------------------
11708 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
11709 T : constant Entity_Id := Entity (Subtype_Mark (S));
11715 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
11716 Set_Etype (Def_Id, Base_Type (T));
11717 Set_Size_Info (Def_Id, (T));
11718 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
11719 Set_Small_Value (Def_Id, Small_Value (T));
11721 -- Process the constraint
11723 C := Constraint (S);
11725 -- Delta constraint present
11727 if Nkind (C) = N_Delta_Constraint then
11729 Check_Formal_Restriction ("delta constraint is not allowed", S);
11730 Check_Restriction (No_Obsolescent_Features, C);
11732 if Warn_On_Obsolescent_Feature then
11734 ("subtype delta constraint is an " &
11735 "obsolescent feature (RM J.3(7))?");
11738 D := Delta_Expression (C);
11739 Analyze_And_Resolve (D, Any_Real);
11740 Check_Delta_Expression (D);
11741 Set_Delta_Value (Def_Id, Expr_Value_R (D));
11743 -- Check that delta value is in range. Obviously we can do this
11744 -- at compile time, but it is strictly a runtime check, and of
11745 -- course there is an ACVC test that checks this!
11747 if Delta_Value (Def_Id) < Delta_Value (T) then
11748 Error_Msg_N ("?delta value is too small", D);
11750 Make_Raise_Constraint_Error (Sloc (D),
11751 Reason => CE_Range_Check_Failed);
11752 Insert_Action (Declaration_Node (Def_Id), Rais);
11755 C := Range_Constraint (C);
11757 -- No delta constraint present
11760 Set_Delta_Value (Def_Id, Delta_Value (T));
11763 -- Range constraint present
11765 if Nkind (C) = N_Range_Constraint then
11766 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
11768 -- No range constraint present
11771 pragma Assert (No (C));
11772 Set_Scalar_Range (Def_Id, Scalar_Range (T));
11776 Set_Discrete_RM_Size (Def_Id);
11778 -- Unconditionally delay the freeze, since we cannot set size
11779 -- information in all cases correctly until the freeze point.
11781 Set_Has_Delayed_Freeze (Def_Id);
11782 end Constrain_Ordinary_Fixed;
11784 -----------------------
11785 -- Contain_Interface --
11786 -----------------------
11788 function Contain_Interface
11789 (Iface : Entity_Id;
11790 Ifaces : Elist_Id) return Boolean
11792 Iface_Elmt : Elmt_Id;
11795 if Present (Ifaces) then
11796 Iface_Elmt := First_Elmt (Ifaces);
11797 while Present (Iface_Elmt) loop
11798 if Node (Iface_Elmt) = Iface then
11802 Next_Elmt (Iface_Elmt);
11807 end Contain_Interface;
11809 ---------------------------
11810 -- Convert_Scalar_Bounds --
11811 ---------------------------
11813 procedure Convert_Scalar_Bounds
11815 Parent_Type : Entity_Id;
11816 Derived_Type : Entity_Id;
11819 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
11826 -- Defend against previous errors
11828 if No (Scalar_Range (Derived_Type)) then
11832 Lo := Build_Scalar_Bound
11833 (Type_Low_Bound (Derived_Type),
11834 Parent_Type, Implicit_Base);
11836 Hi := Build_Scalar_Bound
11837 (Type_High_Bound (Derived_Type),
11838 Parent_Type, Implicit_Base);
11845 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
11847 Set_Parent (Rng, N);
11848 Set_Scalar_Range (Derived_Type, Rng);
11850 -- Analyze the bounds
11852 Analyze_And_Resolve (Lo, Implicit_Base);
11853 Analyze_And_Resolve (Hi, Implicit_Base);
11855 -- Analyze the range itself, except that we do not analyze it if
11856 -- the bounds are real literals, and we have a fixed-point type.
11857 -- The reason for this is that we delay setting the bounds in this
11858 -- case till we know the final Small and Size values (see circuit
11859 -- in Freeze.Freeze_Fixed_Point_Type for further details).
11861 if Is_Fixed_Point_Type (Parent_Type)
11862 and then Nkind (Lo) = N_Real_Literal
11863 and then Nkind (Hi) = N_Real_Literal
11867 -- Here we do the analysis of the range
11869 -- Note: we do this manually, since if we do a normal Analyze and
11870 -- Resolve call, there are problems with the conversions used for
11871 -- the derived type range.
11874 Set_Etype (Rng, Implicit_Base);
11875 Set_Analyzed (Rng, True);
11877 end Convert_Scalar_Bounds;
11879 -------------------
11880 -- Copy_And_Swap --
11881 -------------------
11883 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
11885 -- Initialize new full declaration entity by copying the pertinent
11886 -- fields of the corresponding private declaration entity.
11888 -- We temporarily set Ekind to a value appropriate for a type to
11889 -- avoid assert failures in Einfo from checking for setting type
11890 -- attributes on something that is not a type. Ekind (Priv) is an
11891 -- appropriate choice, since it allowed the attributes to be set
11892 -- in the first place. This Ekind value will be modified later.
11894 Set_Ekind (Full, Ekind (Priv));
11896 -- Also set Etype temporarily to Any_Type, again, in the absence
11897 -- of errors, it will be properly reset, and if there are errors,
11898 -- then we want a value of Any_Type to remain.
11900 Set_Etype (Full, Any_Type);
11902 -- Now start copying attributes
11904 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
11906 if Has_Discriminants (Full) then
11907 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
11908 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
11911 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11912 Set_Homonym (Full, Homonym (Priv));
11913 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
11914 Set_Is_Public (Full, Is_Public (Priv));
11915 Set_Is_Pure (Full, Is_Pure (Priv));
11916 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
11917 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
11918 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
11919 Set_Has_Pragma_Unreferenced_Objects
11920 (Full, Has_Pragma_Unreferenced_Objects
11923 Conditional_Delay (Full, Priv);
11925 if Is_Tagged_Type (Full) then
11926 Set_Direct_Primitive_Operations (Full,
11927 Direct_Primitive_Operations (Priv));
11929 if Is_Base_Type (Priv) then
11930 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
11934 Set_Is_Volatile (Full, Is_Volatile (Priv));
11935 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
11936 Set_Scope (Full, Scope (Priv));
11937 Set_Next_Entity (Full, Next_Entity (Priv));
11938 Set_First_Entity (Full, First_Entity (Priv));
11939 Set_Last_Entity (Full, Last_Entity (Priv));
11941 -- If access types have been recorded for later handling, keep them in
11942 -- the full view so that they get handled when the full view freeze
11943 -- node is expanded.
11945 if Present (Freeze_Node (Priv))
11946 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
11948 Ensure_Freeze_Node (Full);
11949 Set_Access_Types_To_Process
11950 (Freeze_Node (Full),
11951 Access_Types_To_Process (Freeze_Node (Priv)));
11954 -- Swap the two entities. Now Private is the full type entity and Full
11955 -- is the private one. They will be swapped back at the end of the
11956 -- private part. This swapping ensures that the entity that is visible
11957 -- in the private part is the full declaration.
11959 Exchange_Entities (Priv, Full);
11960 Append_Entity (Full, Scope (Full));
11963 -------------------------------------
11964 -- Copy_Array_Base_Type_Attributes --
11965 -------------------------------------
11967 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
11969 Set_Component_Alignment (T1, Component_Alignment (T2));
11970 Set_Component_Type (T1, Component_Type (T2));
11971 Set_Component_Size (T1, Component_Size (T2));
11972 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
11973 Set_Finalize_Storage_Only (T1, Finalize_Storage_Only (T2));
11974 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
11975 Set_Has_Task (T1, Has_Task (T2));
11976 Set_Is_Packed (T1, Is_Packed (T2));
11977 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
11978 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
11979 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
11980 end Copy_Array_Base_Type_Attributes;
11982 -----------------------------------
11983 -- Copy_Array_Subtype_Attributes --
11984 -----------------------------------
11986 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
11988 Set_Size_Info (T1, T2);
11990 Set_First_Index (T1, First_Index (T2));
11991 Set_Is_Aliased (T1, Is_Aliased (T2));
11992 Set_Is_Atomic (T1, Is_Atomic (T2));
11993 Set_Is_Volatile (T1, Is_Volatile (T2));
11994 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
11995 Set_Is_Constrained (T1, Is_Constrained (T2));
11996 Set_Depends_On_Private (T1, Has_Private_Component (T2));
11997 Set_First_Rep_Item (T1, First_Rep_Item (T2));
11998 Set_Convention (T1, Convention (T2));
11999 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
12000 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
12001 Set_Packed_Array_Type (T1, Packed_Array_Type (T2));
12002 end Copy_Array_Subtype_Attributes;
12004 -----------------------------------
12005 -- Create_Constrained_Components --
12006 -----------------------------------
12008 procedure Create_Constrained_Components
12010 Decl_Node : Node_Id;
12012 Constraints : Elist_Id)
12014 Loc : constant Source_Ptr := Sloc (Subt);
12015 Comp_List : constant Elist_Id := New_Elmt_List;
12016 Parent_Type : constant Entity_Id := Etype (Typ);
12017 Assoc_List : constant List_Id := New_List;
12018 Discr_Val : Elmt_Id;
12022 Is_Static : Boolean := True;
12024 procedure Collect_Fixed_Components (Typ : Entity_Id);
12025 -- Collect parent type components that do not appear in a variant part
12027 procedure Create_All_Components;
12028 -- Iterate over Comp_List to create the components of the subtype
12030 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
12031 -- Creates a new component from Old_Compon, copying all the fields from
12032 -- it, including its Etype, inserts the new component in the Subt entity
12033 -- chain and returns the new component.
12035 function Is_Variant_Record (T : Entity_Id) return Boolean;
12036 -- If true, and discriminants are static, collect only components from
12037 -- variants selected by discriminant values.
12039 ------------------------------
12040 -- Collect_Fixed_Components --
12041 ------------------------------
12043 procedure Collect_Fixed_Components (Typ : Entity_Id) is
12045 -- Build association list for discriminants, and find components of the
12046 -- variant part selected by the values of the discriminants.
12048 Old_C := First_Discriminant (Typ);
12049 Discr_Val := First_Elmt (Constraints);
12050 while Present (Old_C) loop
12051 Append_To (Assoc_List,
12052 Make_Component_Association (Loc,
12053 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
12054 Expression => New_Copy (Node (Discr_Val))));
12056 Next_Elmt (Discr_Val);
12057 Next_Discriminant (Old_C);
12060 -- The tag, and the possible parent and controller components
12061 -- are unconditionally in the subtype.
12063 if Is_Tagged_Type (Typ)
12064 or else Has_Controlled_Component (Typ)
12066 Old_C := First_Component (Typ);
12067 while Present (Old_C) loop
12068 if Chars ((Old_C)) = Name_uTag
12069 or else Chars ((Old_C)) = Name_uParent
12070 or else Chars ((Old_C)) = Name_uController
12072 Append_Elmt (Old_C, Comp_List);
12075 Next_Component (Old_C);
12078 end Collect_Fixed_Components;
12080 ---------------------------
12081 -- Create_All_Components --
12082 ---------------------------
12084 procedure Create_All_Components is
12088 Comp := First_Elmt (Comp_List);
12089 while Present (Comp) loop
12090 Old_C := Node (Comp);
12091 New_C := Create_Component (Old_C);
12095 Constrain_Component_Type
12096 (Old_C, Subt, Decl_Node, Typ, Constraints));
12097 Set_Is_Public (New_C, Is_Public (Subt));
12101 end Create_All_Components;
12103 ----------------------
12104 -- Create_Component --
12105 ----------------------
12107 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
12108 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
12111 if Ekind (Old_Compon) = E_Discriminant
12112 and then Is_Completely_Hidden (Old_Compon)
12114 -- This is a shadow discriminant created for a discriminant of
12115 -- the parent type, which needs to be present in the subtype.
12116 -- Give the shadow discriminant an internal name that cannot
12117 -- conflict with that of visible components.
12119 Set_Chars (New_Compon, New_Internal_Name ('C'));
12122 -- Set the parent so we have a proper link for freezing etc. This is
12123 -- not a real parent pointer, since of course our parent does not own
12124 -- up to us and reference us, we are an illegitimate child of the
12125 -- original parent!
12127 Set_Parent (New_Compon, Parent (Old_Compon));
12129 -- If the old component's Esize was already determined and is a
12130 -- static value, then the new component simply inherits it. Otherwise
12131 -- the old component's size may require run-time determination, but
12132 -- the new component's size still might be statically determinable
12133 -- (if, for example it has a static constraint). In that case we want
12134 -- Layout_Type to recompute the component's size, so we reset its
12135 -- size and positional fields.
12137 if Frontend_Layout_On_Target
12138 and then not Known_Static_Esize (Old_Compon)
12140 Set_Esize (New_Compon, Uint_0);
12141 Init_Normalized_First_Bit (New_Compon);
12142 Init_Normalized_Position (New_Compon);
12143 Init_Normalized_Position_Max (New_Compon);
12146 -- We do not want this node marked as Comes_From_Source, since
12147 -- otherwise it would get first class status and a separate cross-
12148 -- reference line would be generated. Illegitimate children do not
12149 -- rate such recognition.
12151 Set_Comes_From_Source (New_Compon, False);
12153 -- But it is a real entity, and a birth certificate must be properly
12154 -- registered by entering it into the entity list.
12156 Enter_Name (New_Compon);
12159 end Create_Component;
12161 -----------------------
12162 -- Is_Variant_Record --
12163 -----------------------
12165 function Is_Variant_Record (T : Entity_Id) return Boolean is
12167 return Nkind (Parent (T)) = N_Full_Type_Declaration
12168 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
12169 and then Present (Component_List (Type_Definition (Parent (T))))
12172 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
12173 end Is_Variant_Record;
12175 -- Start of processing for Create_Constrained_Components
12178 pragma Assert (Subt /= Base_Type (Subt));
12179 pragma Assert (Typ = Base_Type (Typ));
12181 Set_First_Entity (Subt, Empty);
12182 Set_Last_Entity (Subt, Empty);
12184 -- Check whether constraint is fully static, in which case we can
12185 -- optimize the list of components.
12187 Discr_Val := First_Elmt (Constraints);
12188 while Present (Discr_Val) loop
12189 if not Is_OK_Static_Expression (Node (Discr_Val)) then
12190 Is_Static := False;
12194 Next_Elmt (Discr_Val);
12197 Set_Has_Static_Discriminants (Subt, Is_Static);
12201 -- Inherit the discriminants of the parent type
12203 Add_Discriminants : declare
12209 Old_C := First_Discriminant (Typ);
12211 while Present (Old_C) loop
12212 Num_Disc := Num_Disc + 1;
12213 New_C := Create_Component (Old_C);
12214 Set_Is_Public (New_C, Is_Public (Subt));
12215 Next_Discriminant (Old_C);
12218 -- For an untagged derived subtype, the number of discriminants may
12219 -- be smaller than the number of inherited discriminants, because
12220 -- several of them may be renamed by a single new discriminant or
12221 -- constrained. In this case, add the hidden discriminants back into
12222 -- the subtype, because they need to be present if the optimizer of
12223 -- the GCC 4.x back-end decides to break apart assignments between
12224 -- objects using the parent view into member-wise assignments.
12228 if Is_Derived_Type (Typ)
12229 and then not Is_Tagged_Type (Typ)
12231 Old_C := First_Stored_Discriminant (Typ);
12233 while Present (Old_C) loop
12234 Num_Gird := Num_Gird + 1;
12235 Next_Stored_Discriminant (Old_C);
12239 if Num_Gird > Num_Disc then
12241 -- Find out multiple uses of new discriminants, and add hidden
12242 -- components for the extra renamed discriminants. We recognize
12243 -- multiple uses through the Corresponding_Discriminant of a
12244 -- new discriminant: if it constrains several old discriminants,
12245 -- this field points to the last one in the parent type. The
12246 -- stored discriminants of the derived type have the same name
12247 -- as those of the parent.
12251 New_Discr : Entity_Id;
12252 Old_Discr : Entity_Id;
12255 Constr := First_Elmt (Stored_Constraint (Typ));
12256 Old_Discr := First_Stored_Discriminant (Typ);
12257 while Present (Constr) loop
12258 if Is_Entity_Name (Node (Constr))
12259 and then Ekind (Entity (Node (Constr))) = E_Discriminant
12261 New_Discr := Entity (Node (Constr));
12263 if Chars (Corresponding_Discriminant (New_Discr)) /=
12266 -- The new discriminant has been used to rename a
12267 -- subsequent old discriminant. Introduce a shadow
12268 -- component for the current old discriminant.
12270 New_C := Create_Component (Old_Discr);
12271 Set_Original_Record_Component (New_C, Old_Discr);
12275 -- The constraint has eliminated the old discriminant.
12276 -- Introduce a shadow component.
12278 New_C := Create_Component (Old_Discr);
12279 Set_Original_Record_Component (New_C, Old_Discr);
12282 Next_Elmt (Constr);
12283 Next_Stored_Discriminant (Old_Discr);
12287 end Add_Discriminants;
12290 and then Is_Variant_Record (Typ)
12292 Collect_Fixed_Components (Typ);
12294 Gather_Components (
12296 Component_List (Type_Definition (Parent (Typ))),
12297 Governed_By => Assoc_List,
12299 Report_Errors => Errors);
12300 pragma Assert (not Errors);
12302 Create_All_Components;
12304 -- If the subtype declaration is created for a tagged type derivation
12305 -- with constraints, we retrieve the record definition of the parent
12306 -- type to select the components of the proper variant.
12309 and then Is_Tagged_Type (Typ)
12310 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
12312 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
12313 and then Is_Variant_Record (Parent_Type)
12315 Collect_Fixed_Components (Typ);
12317 Gather_Components (
12319 Component_List (Type_Definition (Parent (Parent_Type))),
12320 Governed_By => Assoc_List,
12322 Report_Errors => Errors);
12323 pragma Assert (not Errors);
12325 -- If the tagged derivation has a type extension, collect all the
12326 -- new components therein.
12329 (Record_Extension_Part (Type_Definition (Parent (Typ))))
12331 Old_C := First_Component (Typ);
12332 while Present (Old_C) loop
12333 if Original_Record_Component (Old_C) = Old_C
12334 and then Chars (Old_C) /= Name_uTag
12335 and then Chars (Old_C) /= Name_uParent
12336 and then Chars (Old_C) /= Name_uController
12338 Append_Elmt (Old_C, Comp_List);
12341 Next_Component (Old_C);
12345 Create_All_Components;
12348 -- If discriminants are not static, or if this is a multi-level type
12349 -- extension, we have to include all components of the parent type.
12351 Old_C := First_Component (Typ);
12352 while Present (Old_C) loop
12353 New_C := Create_Component (Old_C);
12357 Constrain_Component_Type
12358 (Old_C, Subt, Decl_Node, Typ, Constraints));
12359 Set_Is_Public (New_C, Is_Public (Subt));
12361 Next_Component (Old_C);
12366 end Create_Constrained_Components;
12368 ------------------------------------------
12369 -- Decimal_Fixed_Point_Type_Declaration --
12370 ------------------------------------------
12372 procedure Decimal_Fixed_Point_Type_Declaration
12376 Loc : constant Source_Ptr := Sloc (Def);
12377 Digs_Expr : constant Node_Id := Digits_Expression (Def);
12378 Delta_Expr : constant Node_Id := Delta_Expression (Def);
12379 Implicit_Base : Entity_Id;
12386 Check_Formal_Restriction
12387 ("decimal fixed point type is not allowed", Def);
12388 Check_Restriction (No_Fixed_Point, Def);
12390 -- Create implicit base type
12393 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
12394 Set_Etype (Implicit_Base, Implicit_Base);
12396 -- Analyze and process delta expression
12398 Analyze_And_Resolve (Delta_Expr, Universal_Real);
12400 Check_Delta_Expression (Delta_Expr);
12401 Delta_Val := Expr_Value_R (Delta_Expr);
12403 -- Check delta is power of 10, and determine scale value from it
12409 Scale_Val := Uint_0;
12412 if Val < Ureal_1 then
12413 while Val < Ureal_1 loop
12414 Val := Val * Ureal_10;
12415 Scale_Val := Scale_Val + 1;
12418 if Scale_Val > 18 then
12419 Error_Msg_N ("scale exceeds maximum value of 18", Def);
12420 Scale_Val := UI_From_Int (+18);
12424 while Val > Ureal_1 loop
12425 Val := Val / Ureal_10;
12426 Scale_Val := Scale_Val - 1;
12429 if Scale_Val < -18 then
12430 Error_Msg_N ("scale is less than minimum value of -18", Def);
12431 Scale_Val := UI_From_Int (-18);
12435 if Val /= Ureal_1 then
12436 Error_Msg_N ("delta expression must be a power of 10", Def);
12437 Delta_Val := Ureal_10 ** (-Scale_Val);
12441 -- Set delta, scale and small (small = delta for decimal type)
12443 Set_Delta_Value (Implicit_Base, Delta_Val);
12444 Set_Scale_Value (Implicit_Base, Scale_Val);
12445 Set_Small_Value (Implicit_Base, Delta_Val);
12447 -- Analyze and process digits expression
12449 Analyze_And_Resolve (Digs_Expr, Any_Integer);
12450 Check_Digits_Expression (Digs_Expr);
12451 Digs_Val := Expr_Value (Digs_Expr);
12453 if Digs_Val > 18 then
12454 Digs_Val := UI_From_Int (+18);
12455 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
12458 Set_Digits_Value (Implicit_Base, Digs_Val);
12459 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
12461 -- Set range of base type from digits value for now. This will be
12462 -- expanded to represent the true underlying base range by Freeze.
12464 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
12466 -- Note: We leave size as zero for now, size will be set at freeze
12467 -- time. We have to do this for ordinary fixed-point, because the size
12468 -- depends on the specified small, and we might as well do the same for
12469 -- decimal fixed-point.
12471 pragma Assert (Esize (Implicit_Base) = Uint_0);
12473 -- If there are bounds given in the declaration use them as the
12474 -- bounds of the first named subtype.
12476 if Present (Real_Range_Specification (Def)) then
12478 RRS : constant Node_Id := Real_Range_Specification (Def);
12479 Low : constant Node_Id := Low_Bound (RRS);
12480 High : constant Node_Id := High_Bound (RRS);
12485 Analyze_And_Resolve (Low, Any_Real);
12486 Analyze_And_Resolve (High, Any_Real);
12487 Check_Real_Bound (Low);
12488 Check_Real_Bound (High);
12489 Low_Val := Expr_Value_R (Low);
12490 High_Val := Expr_Value_R (High);
12492 if Low_Val < (-Bound_Val) then
12494 ("range low bound too small for digits value", Low);
12495 Low_Val := -Bound_Val;
12498 if High_Val > Bound_Val then
12500 ("range high bound too large for digits value", High);
12501 High_Val := Bound_Val;
12504 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
12507 -- If no explicit range, use range that corresponds to given
12508 -- digits value. This will end up as the final range for the
12512 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
12515 -- Complete entity for first subtype
12517 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
12518 Set_Etype (T, Implicit_Base);
12519 Set_Size_Info (T, Implicit_Base);
12520 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
12521 Set_Digits_Value (T, Digs_Val);
12522 Set_Delta_Value (T, Delta_Val);
12523 Set_Small_Value (T, Delta_Val);
12524 Set_Scale_Value (T, Scale_Val);
12525 Set_Is_Constrained (T);
12526 end Decimal_Fixed_Point_Type_Declaration;
12528 -----------------------------------
12529 -- Derive_Progenitor_Subprograms --
12530 -----------------------------------
12532 procedure Derive_Progenitor_Subprograms
12533 (Parent_Type : Entity_Id;
12534 Tagged_Type : Entity_Id)
12539 Iface_Elmt : Elmt_Id;
12540 Iface_Subp : Entity_Id;
12541 New_Subp : Entity_Id := Empty;
12542 Prim_Elmt : Elmt_Id;
12547 pragma Assert (Ada_Version >= Ada_2005
12548 and then Is_Record_Type (Tagged_Type)
12549 and then Is_Tagged_Type (Tagged_Type)
12550 and then Has_Interfaces (Tagged_Type));
12552 -- Step 1: Transfer to the full-view primitives associated with the
12553 -- partial-view that cover interface primitives. Conceptually this
12554 -- work should be done later by Process_Full_View; done here to
12555 -- simplify its implementation at later stages. It can be safely
12556 -- done here because interfaces must be visible in the partial and
12557 -- private view (RM 7.3(7.3/2)).
12559 -- Small optimization: This work is only required if the parent is
12560 -- abstract. If the tagged type is not abstract, it cannot have
12561 -- abstract primitives (the only entities in the list of primitives of
12562 -- non-abstract tagged types that can reference abstract primitives
12563 -- through its Alias attribute are the internal entities that have
12564 -- attribute Interface_Alias, and these entities are generated later
12565 -- by Add_Internal_Interface_Entities).
12567 if In_Private_Part (Current_Scope)
12568 and then Is_Abstract_Type (Parent_Type)
12570 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
12571 while Present (Elmt) loop
12572 Subp := Node (Elmt);
12574 -- At this stage it is not possible to have entities in the list
12575 -- of primitives that have attribute Interface_Alias
12577 pragma Assert (No (Interface_Alias (Subp)));
12579 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
12581 if Is_Interface (Typ) then
12582 E := Find_Primitive_Covering_Interface
12583 (Tagged_Type => Tagged_Type,
12584 Iface_Prim => Subp);
12587 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
12589 Replace_Elmt (Elmt, E);
12590 Remove_Homonym (Subp);
12598 -- Step 2: Add primitives of progenitors that are not implemented by
12599 -- parents of Tagged_Type
12601 if Present (Interfaces (Base_Type (Tagged_Type))) then
12602 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
12603 while Present (Iface_Elmt) loop
12604 Iface := Node (Iface_Elmt);
12606 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
12607 while Present (Prim_Elmt) loop
12608 Iface_Subp := Node (Prim_Elmt);
12610 -- Exclude derivation of predefined primitives except those
12611 -- that come from source. Required to catch declarations of
12612 -- equality operators of interfaces. For example:
12614 -- type Iface is interface;
12615 -- function "=" (Left, Right : Iface) return Boolean;
12617 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
12618 or else Comes_From_Source (Iface_Subp)
12620 E := Find_Primitive_Covering_Interface
12621 (Tagged_Type => Tagged_Type,
12622 Iface_Prim => Iface_Subp);
12624 -- If not found we derive a new primitive leaving its alias
12625 -- attribute referencing the interface primitive
12629 (New_Subp, Iface_Subp, Tagged_Type, Iface);
12631 -- Ada 2012 (AI05-0197): If the covering primitive's name
12632 -- differs from the name of the interface primitive then it
12633 -- is a private primitive inherited from a parent type. In
12634 -- such case, given that Tagged_Type covers the interface,
12635 -- the inherited private primitive becomes visible. For such
12636 -- purpose we add a new entity that renames the inherited
12637 -- private primitive.
12639 elsif Chars (E) /= Chars (Iface_Subp) then
12640 pragma Assert (Has_Suffix (E, 'P'));
12642 (New_Subp, Iface_Subp, Tagged_Type, Iface);
12643 Set_Alias (New_Subp, E);
12644 Set_Is_Abstract_Subprogram (New_Subp,
12645 Is_Abstract_Subprogram (E));
12647 -- Propagate to the full view interface entities associated
12648 -- with the partial view
12650 elsif In_Private_Part (Current_Scope)
12651 and then Present (Alias (E))
12652 and then Alias (E) = Iface_Subp
12654 List_Containing (Parent (E)) /=
12655 Private_Declarations
12657 (Unit_Declaration_Node (Current_Scope)))
12659 Append_Elmt (E, Primitive_Operations (Tagged_Type));
12663 Next_Elmt (Prim_Elmt);
12666 Next_Elmt (Iface_Elmt);
12669 end Derive_Progenitor_Subprograms;
12671 -----------------------
12672 -- Derive_Subprogram --
12673 -----------------------
12675 procedure Derive_Subprogram
12676 (New_Subp : in out Entity_Id;
12677 Parent_Subp : Entity_Id;
12678 Derived_Type : Entity_Id;
12679 Parent_Type : Entity_Id;
12680 Actual_Subp : Entity_Id := Empty)
12682 Formal : Entity_Id;
12683 -- Formal parameter of parent primitive operation
12685 Formal_Of_Actual : Entity_Id;
12686 -- Formal parameter of actual operation, when the derivation is to
12687 -- create a renaming for a primitive operation of an actual in an
12690 New_Formal : Entity_Id;
12691 -- Formal of inherited operation
12693 Visible_Subp : Entity_Id := Parent_Subp;
12695 function Is_Private_Overriding return Boolean;
12696 -- If Subp is a private overriding of a visible operation, the inherited
12697 -- operation derives from the overridden op (even though its body is the
12698 -- overriding one) and the inherited operation is visible now. See
12699 -- sem_disp to see the full details of the handling of the overridden
12700 -- subprogram, which is removed from the list of primitive operations of
12701 -- the type. The overridden subprogram is saved locally in Visible_Subp,
12702 -- and used to diagnose abstract operations that need overriding in the
12705 procedure Replace_Type (Id, New_Id : Entity_Id);
12706 -- When the type is an anonymous access type, create a new access type
12707 -- designating the derived type.
12709 procedure Set_Derived_Name;
12710 -- This procedure sets the appropriate Chars name for New_Subp. This
12711 -- is normally just a copy of the parent name. An exception arises for
12712 -- type support subprograms, where the name is changed to reflect the
12713 -- name of the derived type, e.g. if type foo is derived from type bar,
12714 -- then a procedure barDA is derived with a name fooDA.
12716 ---------------------------
12717 -- Is_Private_Overriding --
12718 ---------------------------
12720 function Is_Private_Overriding return Boolean is
12724 -- If the parent is not a dispatching operation there is no
12725 -- need to investigate overridings
12727 if not Is_Dispatching_Operation (Parent_Subp) then
12731 -- The visible operation that is overridden is a homonym of the
12732 -- parent subprogram. We scan the homonym chain to find the one
12733 -- whose alias is the subprogram we are deriving.
12735 Prev := Current_Entity (Parent_Subp);
12736 while Present (Prev) loop
12737 if Ekind (Prev) = Ekind (Parent_Subp)
12738 and then Alias (Prev) = Parent_Subp
12739 and then Scope (Parent_Subp) = Scope (Prev)
12740 and then not Is_Hidden (Prev)
12742 Visible_Subp := Prev;
12746 Prev := Homonym (Prev);
12750 end Is_Private_Overriding;
12756 procedure Replace_Type (Id, New_Id : Entity_Id) is
12757 Acc_Type : Entity_Id;
12758 Par : constant Node_Id := Parent (Derived_Type);
12761 -- When the type is an anonymous access type, create a new access
12762 -- type designating the derived type. This itype must be elaborated
12763 -- at the point of the derivation, not on subsequent calls that may
12764 -- be out of the proper scope for Gigi, so we insert a reference to
12765 -- it after the derivation.
12767 if Ekind (Etype (Id)) = E_Anonymous_Access_Type then
12769 Desig_Typ : Entity_Id := Designated_Type (Etype (Id));
12772 if Ekind (Desig_Typ) = E_Record_Type_With_Private
12773 and then Present (Full_View (Desig_Typ))
12774 and then not Is_Private_Type (Parent_Type)
12776 Desig_Typ := Full_View (Desig_Typ);
12779 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
12781 -- Ada 2005 (AI-251): Handle also derivations of abstract
12782 -- interface primitives.
12784 or else (Is_Interface (Desig_Typ)
12785 and then not Is_Class_Wide_Type (Desig_Typ))
12787 Acc_Type := New_Copy (Etype (Id));
12788 Set_Etype (Acc_Type, Acc_Type);
12789 Set_Scope (Acc_Type, New_Subp);
12791 -- Compute size of anonymous access type
12793 if Is_Array_Type (Desig_Typ)
12794 and then not Is_Constrained (Desig_Typ)
12796 Init_Size (Acc_Type, 2 * System_Address_Size);
12798 Init_Size (Acc_Type, System_Address_Size);
12801 Init_Alignment (Acc_Type);
12802 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
12804 Set_Etype (New_Id, Acc_Type);
12805 Set_Scope (New_Id, New_Subp);
12807 -- Create a reference to it
12808 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
12811 Set_Etype (New_Id, Etype (Id));
12815 elsif Base_Type (Etype (Id)) = Base_Type (Parent_Type)
12817 (Ekind (Etype (Id)) = E_Record_Type_With_Private
12818 and then Present (Full_View (Etype (Id)))
12820 Base_Type (Full_View (Etype (Id))) = Base_Type (Parent_Type))
12822 -- Constraint checks on formals are generated during expansion,
12823 -- based on the signature of the original subprogram. The bounds
12824 -- of the derived type are not relevant, and thus we can use
12825 -- the base type for the formals. However, the return type may be
12826 -- used in a context that requires that the proper static bounds
12827 -- be used (a case statement, for example) and for those cases
12828 -- we must use the derived type (first subtype), not its base.
12830 -- If the derived_type_definition has no constraints, we know that
12831 -- the derived type has the same constraints as the first subtype
12832 -- of the parent, and we can also use it rather than its base,
12833 -- which can lead to more efficient code.
12835 if Etype (Id) = Parent_Type then
12836 if Is_Scalar_Type (Parent_Type)
12838 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
12840 Set_Etype (New_Id, Derived_Type);
12842 elsif Nkind (Par) = N_Full_Type_Declaration
12844 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
12847 (Subtype_Indication (Type_Definition (Par)))
12849 Set_Etype (New_Id, Derived_Type);
12852 Set_Etype (New_Id, Base_Type (Derived_Type));
12856 Set_Etype (New_Id, Base_Type (Derived_Type));
12860 Set_Etype (New_Id, Etype (Id));
12864 ----------------------
12865 -- Set_Derived_Name --
12866 ----------------------
12868 procedure Set_Derived_Name is
12869 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
12871 if Nm = TSS_Null then
12872 Set_Chars (New_Subp, Chars (Parent_Subp));
12874 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
12876 end Set_Derived_Name;
12878 -- Start of processing for Derive_Subprogram
12882 New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
12883 Set_Ekind (New_Subp, Ekind (Parent_Subp));
12885 -- Check whether the inherited subprogram is a private operation that
12886 -- should be inherited but not yet made visible. Such subprograms can
12887 -- become visible at a later point (e.g., the private part of a public
12888 -- child unit) via Declare_Inherited_Private_Subprograms. If the
12889 -- following predicate is true, then this is not such a private
12890 -- operation and the subprogram simply inherits the name of the parent
12891 -- subprogram. Note the special check for the names of controlled
12892 -- operations, which are currently exempted from being inherited with
12893 -- a hidden name because they must be findable for generation of
12894 -- implicit run-time calls.
12896 if not Is_Hidden (Parent_Subp)
12897 or else Is_Internal (Parent_Subp)
12898 or else Is_Private_Overriding
12899 or else Is_Internal_Name (Chars (Parent_Subp))
12900 or else Chars (Parent_Subp) = Name_Initialize
12901 or else Chars (Parent_Subp) = Name_Adjust
12902 or else Chars (Parent_Subp) = Name_Finalize
12906 -- An inherited dispatching equality will be overridden by an internally
12907 -- generated one, or by an explicit one, so preserve its name and thus
12908 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
12909 -- private operation it may become invisible if the full view has
12910 -- progenitors, and the dispatch table will be malformed.
12911 -- We check that the type is limited to handle the anomalous declaration
12912 -- of Limited_Controlled, which is derived from a non-limited type, and
12913 -- which is handled specially elsewhere as well.
12915 elsif Chars (Parent_Subp) = Name_Op_Eq
12916 and then Is_Dispatching_Operation (Parent_Subp)
12917 and then Etype (Parent_Subp) = Standard_Boolean
12918 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
12920 Etype (First_Formal (Parent_Subp)) =
12921 Etype (Next_Formal (First_Formal (Parent_Subp)))
12925 -- If parent is hidden, this can be a regular derivation if the
12926 -- parent is immediately visible in a non-instantiating context,
12927 -- or if we are in the private part of an instance. This test
12928 -- should still be refined ???
12930 -- The test for In_Instance_Not_Visible avoids inheriting the derived
12931 -- operation as a non-visible operation in cases where the parent
12932 -- subprogram might not be visible now, but was visible within the
12933 -- original generic, so it would be wrong to make the inherited
12934 -- subprogram non-visible now. (Not clear if this test is fully
12935 -- correct; are there any cases where we should declare the inherited
12936 -- operation as not visible to avoid it being overridden, e.g., when
12937 -- the parent type is a generic actual with private primitives ???)
12939 -- (they should be treated the same as other private inherited
12940 -- subprograms, but it's not clear how to do this cleanly). ???
12942 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
12943 and then Is_Immediately_Visible (Parent_Subp)
12944 and then not In_Instance)
12945 or else In_Instance_Not_Visible
12949 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
12950 -- overrides an interface primitive because interface primitives
12951 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
12953 elsif Ada_Version >= Ada_2005
12954 and then Is_Dispatching_Operation (Parent_Subp)
12955 and then Covers_Some_Interface (Parent_Subp)
12959 -- Otherwise, the type is inheriting a private operation, so enter
12960 -- it with a special name so it can't be overridden.
12963 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
12966 Set_Parent (New_Subp, Parent (Derived_Type));
12968 if Present (Actual_Subp) then
12969 Replace_Type (Actual_Subp, New_Subp);
12971 Replace_Type (Parent_Subp, New_Subp);
12974 Conditional_Delay (New_Subp, Parent_Subp);
12976 -- If we are creating a renaming for a primitive operation of an
12977 -- actual of a generic derived type, we must examine the signature
12978 -- of the actual primitive, not that of the generic formal, which for
12979 -- example may be an interface. However the name and initial value
12980 -- of the inherited operation are those of the formal primitive.
12982 Formal := First_Formal (Parent_Subp);
12984 if Present (Actual_Subp) then
12985 Formal_Of_Actual := First_Formal (Actual_Subp);
12987 Formal_Of_Actual := Empty;
12990 while Present (Formal) loop
12991 New_Formal := New_Copy (Formal);
12993 -- Normally we do not go copying parents, but in the case of
12994 -- formals, we need to link up to the declaration (which is the
12995 -- parameter specification), and it is fine to link up to the
12996 -- original formal's parameter specification in this case.
12998 Set_Parent (New_Formal, Parent (Formal));
12999 Append_Entity (New_Formal, New_Subp);
13001 if Present (Formal_Of_Actual) then
13002 Replace_Type (Formal_Of_Actual, New_Formal);
13003 Next_Formal (Formal_Of_Actual);
13005 Replace_Type (Formal, New_Formal);
13008 Next_Formal (Formal);
13011 -- If this derivation corresponds to a tagged generic actual, then
13012 -- primitive operations rename those of the actual. Otherwise the
13013 -- primitive operations rename those of the parent type, If the parent
13014 -- renames an intrinsic operator, so does the new subprogram. We except
13015 -- concatenation, which is always properly typed, and does not get
13016 -- expanded as other intrinsic operations.
13018 if No (Actual_Subp) then
13019 if Is_Intrinsic_Subprogram (Parent_Subp) then
13020 Set_Is_Intrinsic_Subprogram (New_Subp);
13022 if Present (Alias (Parent_Subp))
13023 and then Chars (Parent_Subp) /= Name_Op_Concat
13025 Set_Alias (New_Subp, Alias (Parent_Subp));
13027 Set_Alias (New_Subp, Parent_Subp);
13031 Set_Alias (New_Subp, Parent_Subp);
13035 Set_Alias (New_Subp, Actual_Subp);
13038 -- Derived subprograms of a tagged type must inherit the convention
13039 -- of the parent subprogram (a requirement of AI-117). Derived
13040 -- subprograms of untagged types simply get convention Ada by default.
13042 if Is_Tagged_Type (Derived_Type) then
13043 Set_Convention (New_Subp, Convention (Parent_Subp));
13046 -- Predefined controlled operations retain their name even if the parent
13047 -- is hidden (see above), but they are not primitive operations if the
13048 -- ancestor is not visible, for example if the parent is a private
13049 -- extension completed with a controlled extension. Note that a full
13050 -- type that is controlled can break privacy: the flag Is_Controlled is
13051 -- set on both views of the type.
13053 if Is_Controlled (Parent_Type)
13055 (Chars (Parent_Subp) = Name_Initialize
13056 or else Chars (Parent_Subp) = Name_Adjust
13057 or else Chars (Parent_Subp) = Name_Finalize)
13058 and then Is_Hidden (Parent_Subp)
13059 and then not Is_Visibly_Controlled (Parent_Type)
13061 Set_Is_Hidden (New_Subp);
13064 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
13065 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
13067 if Ekind (Parent_Subp) = E_Procedure then
13068 Set_Is_Valued_Procedure
13069 (New_Subp, Is_Valued_Procedure (Parent_Subp));
13071 Set_Has_Controlling_Result
13072 (New_Subp, Has_Controlling_Result (Parent_Subp));
13075 -- No_Return must be inherited properly. If this is overridden in the
13076 -- case of a dispatching operation, then a check is made in Sem_Disp
13077 -- that the overriding operation is also No_Return (no such check is
13078 -- required for the case of non-dispatching operation.
13080 Set_No_Return (New_Subp, No_Return (Parent_Subp));
13082 -- A derived function with a controlling result is abstract. If the
13083 -- Derived_Type is a nonabstract formal generic derived type, then
13084 -- inherited operations are not abstract: the required check is done at
13085 -- instantiation time. If the derivation is for a generic actual, the
13086 -- function is not abstract unless the actual is.
13088 if Is_Generic_Type (Derived_Type)
13089 and then not Is_Abstract_Type (Derived_Type)
13093 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
13094 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
13096 elsif Ada_Version >= Ada_2005
13097 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13098 or else (Is_Tagged_Type (Derived_Type)
13099 and then Etype (New_Subp) = Derived_Type
13100 and then not Is_Null_Extension (Derived_Type))
13101 or else (Is_Tagged_Type (Derived_Type)
13102 and then Ekind (Etype (New_Subp)) =
13103 E_Anonymous_Access_Type
13104 and then Designated_Type (Etype (New_Subp)) =
13106 and then not Is_Null_Extension (Derived_Type)))
13107 and then No (Actual_Subp)
13109 if not Is_Tagged_Type (Derived_Type)
13110 or else Is_Abstract_Type (Derived_Type)
13111 or else Is_Abstract_Subprogram (Alias (New_Subp))
13113 Set_Is_Abstract_Subprogram (New_Subp);
13115 Set_Requires_Overriding (New_Subp);
13118 elsif Ada_Version < Ada_2005
13119 and then (Is_Abstract_Subprogram (Alias (New_Subp))
13120 or else (Is_Tagged_Type (Derived_Type)
13121 and then Etype (New_Subp) = Derived_Type
13122 and then No (Actual_Subp)))
13124 Set_Is_Abstract_Subprogram (New_Subp);
13126 -- AI05-0097 : an inherited operation that dispatches on result is
13127 -- abstract if the derived type is abstract, even if the parent type
13128 -- is concrete and the derived type is a null extension.
13130 elsif Has_Controlling_Result (Alias (New_Subp))
13131 and then Is_Abstract_Type (Etype (New_Subp))
13133 Set_Is_Abstract_Subprogram (New_Subp);
13135 -- Finally, if the parent type is abstract we must verify that all
13136 -- inherited operations are either non-abstract or overridden, or that
13137 -- the derived type itself is abstract (this check is performed at the
13138 -- end of a package declaration, in Check_Abstract_Overriding). A
13139 -- private overriding in the parent type will not be visible in the
13140 -- derivation if we are not in an inner package or in a child unit of
13141 -- the parent type, in which case the abstractness of the inherited
13142 -- operation is carried to the new subprogram.
13144 elsif Is_Abstract_Type (Parent_Type)
13145 and then not In_Open_Scopes (Scope (Parent_Type))
13146 and then Is_Private_Overriding
13147 and then Is_Abstract_Subprogram (Visible_Subp)
13149 if No (Actual_Subp) then
13150 Set_Alias (New_Subp, Visible_Subp);
13151 Set_Is_Abstract_Subprogram (New_Subp, True);
13154 -- If this is a derivation for an instance of a formal derived
13155 -- type, abstractness comes from the primitive operation of the
13156 -- actual, not from the operation inherited from the ancestor.
13158 Set_Is_Abstract_Subprogram
13159 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
13163 New_Overloaded_Entity (New_Subp, Derived_Type);
13165 -- Check for case of a derived subprogram for the instantiation of a
13166 -- formal derived tagged type, if so mark the subprogram as dispatching
13167 -- and inherit the dispatching attributes of the parent subprogram. The
13168 -- derived subprogram is effectively renaming of the actual subprogram,
13169 -- so it needs to have the same attributes as the actual.
13171 if Present (Actual_Subp)
13172 and then Is_Dispatching_Operation (Parent_Subp)
13174 Set_Is_Dispatching_Operation (New_Subp);
13176 if Present (DTC_Entity (Parent_Subp)) then
13177 Set_DTC_Entity (New_Subp, DTC_Entity (Parent_Subp));
13178 Set_DT_Position (New_Subp, DT_Position (Parent_Subp));
13182 -- Indicate that a derived subprogram does not require a body and that
13183 -- it does not require processing of default expressions.
13185 Set_Has_Completion (New_Subp);
13186 Set_Default_Expressions_Processed (New_Subp);
13188 if Ekind (New_Subp) = E_Function then
13189 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
13191 end Derive_Subprogram;
13193 ------------------------
13194 -- Derive_Subprograms --
13195 ------------------------
13197 procedure Derive_Subprograms
13198 (Parent_Type : Entity_Id;
13199 Derived_Type : Entity_Id;
13200 Generic_Actual : Entity_Id := Empty)
13202 Op_List : constant Elist_Id :=
13203 Collect_Primitive_Operations (Parent_Type);
13205 function Check_Derived_Type return Boolean;
13206 -- Check that all the entities derived from Parent_Type are found in
13207 -- the list of primitives of Derived_Type exactly in the same order.
13209 procedure Derive_Interface_Subprogram
13210 (New_Subp : in out Entity_Id;
13212 Actual_Subp : Entity_Id);
13213 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
13214 -- (which is an interface primitive). If Generic_Actual is present then
13215 -- Actual_Subp is the actual subprogram corresponding with the generic
13216 -- subprogram Subp.
13218 function Check_Derived_Type return Boolean is
13222 New_Subp : Entity_Id;
13227 -- Traverse list of entities in the current scope searching for
13228 -- an incomplete type whose full-view is derived type
13230 E := First_Entity (Scope (Derived_Type));
13232 and then E /= Derived_Type
13234 if Ekind (E) = E_Incomplete_Type
13235 and then Present (Full_View (E))
13236 and then Full_View (E) = Derived_Type
13238 -- Disable this test if Derived_Type completes an incomplete
13239 -- type because in such case more primitives can be added
13240 -- later to the list of primitives of Derived_Type by routine
13241 -- Process_Incomplete_Dependents
13246 E := Next_Entity (E);
13249 List := Collect_Primitive_Operations (Derived_Type);
13250 Elmt := First_Elmt (List);
13252 Op_Elmt := First_Elmt (Op_List);
13253 while Present (Op_Elmt) loop
13254 Subp := Node (Op_Elmt);
13255 New_Subp := Node (Elmt);
13257 -- At this early stage Derived_Type has no entities with attribute
13258 -- Interface_Alias. In addition, such primitives are always
13259 -- located at the end of the list of primitives of Parent_Type.
13260 -- Therefore, if found we can safely stop processing pending
13263 exit when Present (Interface_Alias (Subp));
13265 -- Handle hidden entities
13267 if not Is_Predefined_Dispatching_Operation (Subp)
13268 and then Is_Hidden (Subp)
13270 if Present (New_Subp)
13271 and then Primitive_Names_Match (Subp, New_Subp)
13277 if not Present (New_Subp)
13278 or else Ekind (Subp) /= Ekind (New_Subp)
13279 or else not Primitive_Names_Match (Subp, New_Subp)
13287 Next_Elmt (Op_Elmt);
13291 end Check_Derived_Type;
13293 ---------------------------------
13294 -- Derive_Interface_Subprogram --
13295 ---------------------------------
13297 procedure Derive_Interface_Subprogram
13298 (New_Subp : in out Entity_Id;
13300 Actual_Subp : Entity_Id)
13302 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
13303 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
13306 pragma Assert (Is_Interface (Iface_Type));
13309 (New_Subp => New_Subp,
13310 Parent_Subp => Iface_Subp,
13311 Derived_Type => Derived_Type,
13312 Parent_Type => Iface_Type,
13313 Actual_Subp => Actual_Subp);
13315 -- Given that this new interface entity corresponds with a primitive
13316 -- of the parent that was not overridden we must leave it associated
13317 -- with its parent primitive to ensure that it will share the same
13318 -- dispatch table slot when overridden.
13320 if No (Actual_Subp) then
13321 Set_Alias (New_Subp, Subp);
13323 -- For instantiations this is not needed since the previous call to
13324 -- Derive_Subprogram leaves the entity well decorated.
13327 pragma Assert (Alias (New_Subp) = Actual_Subp);
13330 end Derive_Interface_Subprogram;
13334 Alias_Subp : Entity_Id;
13335 Act_List : Elist_Id;
13336 Act_Elmt : Elmt_Id := No_Elmt;
13337 Act_Subp : Entity_Id := Empty;
13339 Need_Search : Boolean := False;
13340 New_Subp : Entity_Id := Empty;
13341 Parent_Base : Entity_Id;
13344 -- Start of processing for Derive_Subprograms
13347 if Ekind (Parent_Type) = E_Record_Type_With_Private
13348 and then Has_Discriminants (Parent_Type)
13349 and then Present (Full_View (Parent_Type))
13351 Parent_Base := Full_View (Parent_Type);
13353 Parent_Base := Parent_Type;
13356 if Present (Generic_Actual) then
13357 Act_List := Collect_Primitive_Operations (Generic_Actual);
13358 Act_Elmt := First_Elmt (Act_List);
13361 -- Derive primitives inherited from the parent. Note that if the generic
13362 -- actual is present, this is not really a type derivation, it is a
13363 -- completion within an instance.
13365 -- Case 1: Derived_Type does not implement interfaces
13367 if not Is_Tagged_Type (Derived_Type)
13368 or else (not Has_Interfaces (Derived_Type)
13369 and then not (Present (Generic_Actual)
13371 Has_Interfaces (Generic_Actual)))
13373 Elmt := First_Elmt (Op_List);
13374 while Present (Elmt) loop
13375 Subp := Node (Elmt);
13377 -- Literals are derived earlier in the process of building the
13378 -- derived type, and are skipped here.
13380 if Ekind (Subp) = E_Enumeration_Literal then
13383 -- The actual is a direct descendant and the common primitive
13384 -- operations appear in the same order.
13386 -- If the generic parent type is present, the derived type is an
13387 -- instance of a formal derived type, and within the instance its
13388 -- operations are those of the actual. We derive from the formal
13389 -- type but make the inherited operations aliases of the
13390 -- corresponding operations of the actual.
13393 pragma Assert (No (Node (Act_Elmt))
13394 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
13396 Type_Conformant (Subp, Node (Act_Elmt),
13397 Skip_Controlling_Formals => True)));
13400 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
13402 if Present (Act_Elmt) then
13403 Next_Elmt (Act_Elmt);
13410 -- Case 2: Derived_Type implements interfaces
13413 -- If the parent type has no predefined primitives we remove
13414 -- predefined primitives from the list of primitives of generic
13415 -- actual to simplify the complexity of this algorithm.
13417 if Present (Generic_Actual) then
13419 Has_Predefined_Primitives : Boolean := False;
13422 -- Check if the parent type has predefined primitives
13424 Elmt := First_Elmt (Op_List);
13425 while Present (Elmt) loop
13426 Subp := Node (Elmt);
13428 if Is_Predefined_Dispatching_Operation (Subp)
13429 and then not Comes_From_Source (Ultimate_Alias (Subp))
13431 Has_Predefined_Primitives := True;
13438 -- Remove predefined primitives of Generic_Actual. We must use
13439 -- an auxiliary list because in case of tagged types the value
13440 -- returned by Collect_Primitive_Operations is the value stored
13441 -- in its Primitive_Operations attribute (and we don't want to
13442 -- modify its current contents).
13444 if not Has_Predefined_Primitives then
13446 Aux_List : constant Elist_Id := New_Elmt_List;
13449 Elmt := First_Elmt (Act_List);
13450 while Present (Elmt) loop
13451 Subp := Node (Elmt);
13453 if not Is_Predefined_Dispatching_Operation (Subp)
13454 or else Comes_From_Source (Subp)
13456 Append_Elmt (Subp, Aux_List);
13462 Act_List := Aux_List;
13466 Act_Elmt := First_Elmt (Act_List);
13467 Act_Subp := Node (Act_Elmt);
13471 -- Stage 1: If the generic actual is not present we derive the
13472 -- primitives inherited from the parent type. If the generic parent
13473 -- type is present, the derived type is an instance of a formal
13474 -- derived type, and within the instance its operations are those of
13475 -- the actual. We derive from the formal type but make the inherited
13476 -- operations aliases of the corresponding operations of the actual.
13478 Elmt := First_Elmt (Op_List);
13479 while Present (Elmt) loop
13480 Subp := Node (Elmt);
13481 Alias_Subp := Ultimate_Alias (Subp);
13483 -- Do not derive internal entities of the parent that link
13484 -- interface primitives with their covering primitive. These
13485 -- entities will be added to this type when frozen.
13487 if Present (Interface_Alias (Subp)) then
13491 -- If the generic actual is present find the corresponding
13492 -- operation in the generic actual. If the parent type is a
13493 -- direct ancestor of the derived type then, even if it is an
13494 -- interface, the operations are inherited from the primary
13495 -- dispatch table and are in the proper order. If we detect here
13496 -- that primitives are not in the same order we traverse the list
13497 -- of primitive operations of the actual to find the one that
13498 -- implements the interface primitive.
13502 (Present (Generic_Actual)
13503 and then Present (Act_Subp)
13505 (Primitive_Names_Match (Subp, Act_Subp)
13507 Type_Conformant (Subp, Act_Subp,
13508 Skip_Controlling_Formals => True)))
13510 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual));
13512 -- Remember that we need searching for all pending primitives
13514 Need_Search := True;
13516 -- Handle entities associated with interface primitives
13518 if Present (Alias_Subp)
13519 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13520 and then not Is_Predefined_Dispatching_Operation (Subp)
13522 -- Search for the primitive in the homonym chain
13525 Find_Primitive_Covering_Interface
13526 (Tagged_Type => Generic_Actual,
13527 Iface_Prim => Alias_Subp);
13529 -- Previous search may not locate primitives covering
13530 -- interfaces defined in generics units or instantiations.
13531 -- (it fails if the covering primitive has formals whose
13532 -- type is also defined in generics or instantiations).
13533 -- In such case we search in the list of primitives of the
13534 -- generic actual for the internal entity that links the
13535 -- interface primitive and the covering primitive.
13538 and then Is_Generic_Type (Parent_Type)
13540 -- This code has been designed to handle only generic
13541 -- formals that implement interfaces that are defined
13542 -- in a generic unit or instantiation. If this code is
13543 -- needed for other cases we must review it because
13544 -- (given that it relies on Original_Location to locate
13545 -- the primitive of Generic_Actual that covers the
13546 -- interface) it could leave linked through attribute
13547 -- Alias entities of unrelated instantiations).
13551 (Scope (Find_Dispatching_Type (Alias_Subp)))
13553 Instantiation_Depth
13554 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
13557 Iface_Prim_Loc : constant Source_Ptr :=
13558 Original_Location (Sloc (Alias_Subp));
13563 First_Elmt (Primitive_Operations (Generic_Actual));
13565 Search : while Present (Elmt) loop
13566 Prim := Node (Elmt);
13568 if Present (Interface_Alias (Prim))
13569 and then Original_Location
13570 (Sloc (Interface_Alias (Prim)))
13573 Act_Subp := Alias (Prim);
13582 pragma Assert (Present (Act_Subp)
13583 or else Is_Abstract_Type (Generic_Actual)
13584 or else Serious_Errors_Detected > 0);
13586 -- Handle predefined primitives plus the rest of user-defined
13590 Act_Elmt := First_Elmt (Act_List);
13591 while Present (Act_Elmt) loop
13592 Act_Subp := Node (Act_Elmt);
13594 exit when Primitive_Names_Match (Subp, Act_Subp)
13595 and then Type_Conformant
13597 Skip_Controlling_Formals => True)
13598 and then No (Interface_Alias (Act_Subp));
13600 Next_Elmt (Act_Elmt);
13603 if No (Act_Elmt) then
13609 -- Case 1: If the parent is a limited interface then it has the
13610 -- predefined primitives of synchronized interfaces. However, the
13611 -- actual type may be a non-limited type and hence it does not
13612 -- have such primitives.
13614 if Present (Generic_Actual)
13615 and then not Present (Act_Subp)
13616 and then Is_Limited_Interface (Parent_Base)
13617 and then Is_Predefined_Interface_Primitive (Subp)
13621 -- Case 2: Inherit entities associated with interfaces that were
13622 -- not covered by the parent type. We exclude here null interface
13623 -- primitives because they do not need special management.
13625 -- We also exclude interface operations that are renamings. If the
13626 -- subprogram is an explicit renaming of an interface primitive,
13627 -- it is a regular primitive operation, and the presence of its
13628 -- alias is not relevant: it has to be derived like any other
13631 elsif Present (Alias (Subp))
13632 and then Nkind (Unit_Declaration_Node (Subp)) /=
13633 N_Subprogram_Renaming_Declaration
13634 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
13636 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
13637 and then Null_Present (Parent (Alias_Subp)))
13639 -- If this is an abstract private type then we transfer the
13640 -- derivation of the interface primitive from the partial view
13641 -- to the full view. This is safe because all the interfaces
13642 -- must be visible in the partial view. Done to avoid adding
13643 -- a new interface derivation to the private part of the
13644 -- enclosing package; otherwise this new derivation would be
13645 -- decorated as hidden when the analysis of the enclosing
13646 -- package completes.
13648 if Is_Abstract_Type (Derived_Type)
13649 and then In_Private_Part (Current_Scope)
13650 and then Has_Private_Declaration (Derived_Type)
13653 Partial_View : Entity_Id;
13658 Partial_View := First_Entity (Current_Scope);
13660 exit when No (Partial_View)
13661 or else (Has_Private_Declaration (Partial_View)
13663 Full_View (Partial_View) = Derived_Type);
13665 Next_Entity (Partial_View);
13668 -- If the partial view was not found then the source code
13669 -- has errors and the derivation is not needed.
13671 if Present (Partial_View) then
13673 First_Elmt (Primitive_Operations (Partial_View));
13674 while Present (Elmt) loop
13675 Ent := Node (Elmt);
13677 if Present (Alias (Ent))
13678 and then Ultimate_Alias (Ent) = Alias (Subp)
13681 (Ent, Primitive_Operations (Derived_Type));
13688 -- If the interface primitive was not found in the
13689 -- partial view then this interface primitive was
13690 -- overridden. We add a derivation to activate in
13691 -- Derive_Progenitor_Subprograms the machinery to
13695 Derive_Interface_Subprogram
13696 (New_Subp => New_Subp,
13698 Actual_Subp => Act_Subp);
13703 Derive_Interface_Subprogram
13704 (New_Subp => New_Subp,
13706 Actual_Subp => Act_Subp);
13709 -- Case 3: Common derivation
13713 (New_Subp => New_Subp,
13714 Parent_Subp => Subp,
13715 Derived_Type => Derived_Type,
13716 Parent_Type => Parent_Base,
13717 Actual_Subp => Act_Subp);
13720 -- No need to update Act_Elm if we must search for the
13721 -- corresponding operation in the generic actual
13724 and then Present (Act_Elmt)
13726 Next_Elmt (Act_Elmt);
13727 Act_Subp := Node (Act_Elmt);
13734 -- Inherit additional operations from progenitors. If the derived
13735 -- type is a generic actual, there are not new primitive operations
13736 -- for the type because it has those of the actual, and therefore
13737 -- nothing needs to be done. The renamings generated above are not
13738 -- primitive operations, and their purpose is simply to make the
13739 -- proper operations visible within an instantiation.
13741 if No (Generic_Actual) then
13742 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
13746 -- Final check: Direct descendants must have their primitives in the
13747 -- same order. We exclude from this test untagged types and instances
13748 -- of formal derived types. We skip this test if we have already
13749 -- reported serious errors in the sources.
13751 pragma Assert (not Is_Tagged_Type (Derived_Type)
13752 or else Present (Generic_Actual)
13753 or else Serious_Errors_Detected > 0
13754 or else Check_Derived_Type);
13755 end Derive_Subprograms;
13757 --------------------------------
13758 -- Derived_Standard_Character --
13759 --------------------------------
13761 procedure Derived_Standard_Character
13763 Parent_Type : Entity_Id;
13764 Derived_Type : Entity_Id)
13766 Loc : constant Source_Ptr := Sloc (N);
13767 Def : constant Node_Id := Type_Definition (N);
13768 Indic : constant Node_Id := Subtype_Indication (Def);
13769 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
13770 Implicit_Base : constant Entity_Id :=
13772 (E_Enumeration_Type, N, Derived_Type, 'B');
13778 Discard_Node (Process_Subtype (Indic, N));
13780 Set_Etype (Implicit_Base, Parent_Base);
13781 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
13782 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
13784 Set_Is_Character_Type (Implicit_Base, True);
13785 Set_Has_Delayed_Freeze (Implicit_Base);
13787 -- The bounds of the implicit base are the bounds of the parent base.
13788 -- Note that their type is the parent base.
13790 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
13791 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
13793 Set_Scalar_Range (Implicit_Base,
13796 High_Bound => Hi));
13798 Conditional_Delay (Derived_Type, Parent_Type);
13800 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
13801 Set_Etype (Derived_Type, Implicit_Base);
13802 Set_Size_Info (Derived_Type, Parent_Type);
13804 if Unknown_RM_Size (Derived_Type) then
13805 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
13808 Set_Is_Character_Type (Derived_Type, True);
13810 if Nkind (Indic) /= N_Subtype_Indication then
13812 -- If no explicit constraint, the bounds are those
13813 -- of the parent type.
13815 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
13816 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
13817 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
13820 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
13822 -- Because the implicit base is used in the conversion of the bounds, we
13823 -- have to freeze it now. This is similar to what is done for numeric
13824 -- types, and it equally suspicious, but otherwise a non-static bound
13825 -- will have a reference to an unfrozen type, which is rejected by Gigi
13826 -- (???). This requires specific care for definition of stream
13827 -- attributes. For details, see comments at the end of
13828 -- Build_Derived_Numeric_Type.
13830 Freeze_Before (N, Implicit_Base);
13831 end Derived_Standard_Character;
13833 ------------------------------
13834 -- Derived_Type_Declaration --
13835 ------------------------------
13837 procedure Derived_Type_Declaration
13840 Is_Completion : Boolean)
13842 Parent_Type : Entity_Id;
13844 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
13845 -- Check whether the parent type is a generic formal, or derives
13846 -- directly or indirectly from one.
13848 ------------------------
13849 -- Comes_From_Generic --
13850 ------------------------
13852 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
13854 if Is_Generic_Type (Typ) then
13857 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
13860 elsif Is_Private_Type (Typ)
13861 and then Present (Full_View (Typ))
13862 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
13866 elsif Is_Generic_Actual_Type (Typ) then
13872 end Comes_From_Generic;
13876 Def : constant Node_Id := Type_Definition (N);
13877 Iface_Def : Node_Id;
13878 Indic : constant Node_Id := Subtype_Indication (Def);
13879 Extension : constant Node_Id := Record_Extension_Part (Def);
13880 Parent_Node : Node_Id;
13881 Parent_Scope : Entity_Id;
13884 -- Start of processing for Derived_Type_Declaration
13887 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
13889 -- Ada 2005 (AI-251): In case of interface derivation check that the
13890 -- parent is also an interface.
13892 if Interface_Present (Def) then
13893 Check_Formal_Restriction ("interface is not allowed", Def);
13895 if not Is_Interface (Parent_Type) then
13896 Diagnose_Interface (Indic, Parent_Type);
13899 Parent_Node := Parent (Base_Type (Parent_Type));
13900 Iface_Def := Type_Definition (Parent_Node);
13902 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
13903 -- other limited interfaces.
13905 if Limited_Present (Def) then
13906 if Limited_Present (Iface_Def) then
13909 elsif Protected_Present (Iface_Def) then
13911 ("descendant of& must be declared"
13912 & " as a protected interface",
13915 elsif Synchronized_Present (Iface_Def) then
13917 ("descendant of& must be declared"
13918 & " as a synchronized interface",
13921 elsif Task_Present (Iface_Def) then
13923 ("descendant of& must be declared as a task interface",
13928 ("(Ada 2005) limited interface cannot "
13929 & "inherit from non-limited interface", Indic);
13932 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
13933 -- from non-limited or limited interfaces.
13935 elsif not Protected_Present (Def)
13936 and then not Synchronized_Present (Def)
13937 and then not Task_Present (Def)
13939 if Limited_Present (Iface_Def) then
13942 elsif Protected_Present (Iface_Def) then
13944 ("descendant of& must be declared"
13945 & " as a protected interface",
13948 elsif Synchronized_Present (Iface_Def) then
13950 ("descendant of& must be declared"
13951 & " as a synchronized interface",
13954 elsif Task_Present (Iface_Def) then
13956 ("descendant of& must be declared as a task interface",
13965 if Is_Tagged_Type (Parent_Type)
13966 and then Is_Concurrent_Type (Parent_Type)
13967 and then not Is_Interface (Parent_Type)
13970 ("parent type of a record extension cannot be "
13971 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
13972 Set_Etype (T, Any_Type);
13976 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
13979 if Is_Tagged_Type (Parent_Type)
13980 and then Is_Non_Empty_List (Interface_List (Def))
13987 Intf := First (Interface_List (Def));
13988 while Present (Intf) loop
13989 T := Find_Type_Of_Subtype_Indic (Intf);
13991 if not Is_Interface (T) then
13992 Diagnose_Interface (Intf, T);
13994 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
13995 -- a limited type from having a nonlimited progenitor.
13997 elsif (Limited_Present (Def)
13998 or else (not Is_Interface (Parent_Type)
13999 and then Is_Limited_Type (Parent_Type)))
14000 and then not Is_Limited_Interface (T)
14003 ("progenitor interface& of limited type must be limited",
14012 if Parent_Type = Any_Type
14013 or else Etype (Parent_Type) = Any_Type
14014 or else (Is_Class_Wide_Type (Parent_Type)
14015 and then Etype (Parent_Type) = T)
14017 -- If Parent_Type is undefined or illegal, make new type into a
14018 -- subtype of Any_Type, and set a few attributes to prevent cascaded
14019 -- errors. If this is a self-definition, emit error now.
14022 or else T = Etype (Parent_Type)
14024 Error_Msg_N ("type cannot be used in its own definition", Indic);
14027 Set_Ekind (T, Ekind (Parent_Type));
14028 Set_Etype (T, Any_Type);
14029 Set_Scalar_Range (T, Scalar_Range (Any_Type));
14031 if Is_Tagged_Type (T)
14032 and then Is_Record_Type (T)
14034 Set_Direct_Primitive_Operations (T, New_Elmt_List);
14040 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
14041 -- an interface is special because the list of interfaces in the full
14042 -- view can be given in any order. For example:
14044 -- type A is interface;
14045 -- type B is interface and A;
14046 -- type D is new B with private;
14048 -- type D is new A and B with null record; -- 1 --
14050 -- In this case we perform the following transformation of -1-:
14052 -- type D is new B and A with null record;
14054 -- If the parent of the full-view covers the parent of the partial-view
14055 -- we have two possible cases:
14057 -- 1) They have the same parent
14058 -- 2) The parent of the full-view implements some further interfaces
14060 -- In both cases we do not need to perform the transformation. In the
14061 -- first case the source program is correct and the transformation is
14062 -- not needed; in the second case the source program does not fulfill
14063 -- the no-hidden interfaces rule (AI-396) and the error will be reported
14066 -- This transformation not only simplifies the rest of the analysis of
14067 -- this type declaration but also simplifies the correct generation of
14068 -- the object layout to the expander.
14070 if In_Private_Part (Current_Scope)
14071 and then Is_Interface (Parent_Type)
14075 Partial_View : Entity_Id;
14076 Partial_View_Parent : Entity_Id;
14077 New_Iface : Node_Id;
14080 -- Look for the associated private type declaration
14082 Partial_View := First_Entity (Current_Scope);
14084 exit when No (Partial_View)
14085 or else (Has_Private_Declaration (Partial_View)
14086 and then Full_View (Partial_View) = T);
14088 Next_Entity (Partial_View);
14091 -- If the partial view was not found then the source code has
14092 -- errors and the transformation is not needed.
14094 if Present (Partial_View) then
14095 Partial_View_Parent := Etype (Partial_View);
14097 -- If the parent of the full-view covers the parent of the
14098 -- partial-view we have nothing else to do.
14100 if Interface_Present_In_Ancestor
14101 (Parent_Type, Partial_View_Parent)
14105 -- Traverse the list of interfaces of the full-view to look
14106 -- for the parent of the partial-view and perform the tree
14110 Iface := First (Interface_List (Def));
14111 while Present (Iface) loop
14112 if Etype (Iface) = Etype (Partial_View) then
14113 Rewrite (Subtype_Indication (Def),
14114 New_Copy (Subtype_Indication
14115 (Parent (Partial_View))));
14118 Make_Identifier (Sloc (N), Chars (Parent_Type));
14119 Append (New_Iface, Interface_List (Def));
14121 -- Analyze the transformed code
14123 Derived_Type_Declaration (T, N, Is_Completion);
14134 -- Only composite types other than array types are allowed to have
14135 -- discriminants. In SPARK and in ALFA, no types are allowed to have
14138 if Present (Discriminant_Specifications (N)) then
14139 if (Is_Elementary_Type (Parent_Type)
14140 or else Is_Array_Type (Parent_Type))
14141 and then not Error_Posted (N)
14144 ("elementary or array type cannot have discriminants",
14145 Defining_Identifier (First (Discriminant_Specifications (N))));
14146 Set_Has_Discriminants (T, False);
14148 Check_Formal_Restriction ("discriminant type is not allowed", N);
14152 -- In Ada 83, a derived type defined in a package specification cannot
14153 -- be used for further derivation until the end of its visible part.
14154 -- Note that derivation in the private part of the package is allowed.
14156 if Ada_Version = Ada_83
14157 and then Is_Derived_Type (Parent_Type)
14158 and then In_Visible_Part (Scope (Parent_Type))
14160 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
14162 ("(Ada 83): premature use of type for derivation", Indic);
14166 -- Check for early use of incomplete or private type
14168 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
14169 Error_Msg_N ("premature derivation of incomplete type", Indic);
14172 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
14173 and then not Comes_From_Generic (Parent_Type))
14174 or else Has_Private_Component (Parent_Type)
14176 -- The ancestor type of a formal type can be incomplete, in which
14177 -- case only the operations of the partial view are available in the
14178 -- generic. Subsequent checks may be required when the full view is
14179 -- analyzed to verify that a derivation from a tagged type has an
14182 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
14185 elsif No (Underlying_Type (Parent_Type))
14186 or else Has_Private_Component (Parent_Type)
14189 ("premature derivation of derived or private type", Indic);
14191 -- Flag the type itself as being in error, this prevents some
14192 -- nasty problems with subsequent uses of the malformed type.
14194 Set_Error_Posted (T);
14196 -- Check that within the immediate scope of an untagged partial
14197 -- view it's illegal to derive from the partial view if the
14198 -- full view is tagged. (7.3(7))
14200 -- We verify that the Parent_Type is a partial view by checking
14201 -- that it is not a Full_Type_Declaration (i.e. a private type or
14202 -- private extension declaration), to distinguish a partial view
14203 -- from a derivation from a private type which also appears as
14206 elsif Present (Full_View (Parent_Type))
14207 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
14208 and then not Is_Tagged_Type (Parent_Type)
14209 and then Is_Tagged_Type (Full_View (Parent_Type))
14211 Parent_Scope := Scope (T);
14212 while Present (Parent_Scope)
14213 and then Parent_Scope /= Standard_Standard
14215 if Parent_Scope = Scope (Parent_Type) then
14217 ("premature derivation from type with tagged full view",
14221 Parent_Scope := Scope (Parent_Scope);
14226 -- Check that form of derivation is appropriate
14228 Taggd := Is_Tagged_Type (Parent_Type);
14230 -- Perhaps the parent type should be changed to the class-wide type's
14231 -- specific type in this case to prevent cascading errors ???
14233 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
14234 Error_Msg_N ("parent type must not be a class-wide type", Indic);
14238 if Present (Extension) and then not Taggd then
14240 ("type derived from untagged type cannot have extension", Indic);
14242 elsif No (Extension) and then Taggd then
14244 -- If this declaration is within a private part (or body) of a
14245 -- generic instantiation then the derivation is allowed (the parent
14246 -- type can only appear tagged in this case if it's a generic actual
14247 -- type, since it would otherwise have been rejected in the analysis
14248 -- of the generic template).
14250 if not Is_Generic_Actual_Type (Parent_Type)
14251 or else In_Visible_Part (Scope (Parent_Type))
14253 if Is_Class_Wide_Type (Parent_Type) then
14255 ("parent type must not be a class-wide type", Indic);
14257 -- Use specific type to prevent cascaded errors.
14259 Parent_Type := Etype (Parent_Type);
14263 ("type derived from tagged type must have extension", Indic);
14268 -- AI-443: Synchronized formal derived types require a private
14269 -- extension. There is no point in checking the ancestor type or
14270 -- the progenitors since the construct is wrong to begin with.
14272 if Ada_Version >= Ada_2005
14273 and then Is_Generic_Type (T)
14274 and then Present (Original_Node (N))
14277 Decl : constant Node_Id := Original_Node (N);
14280 if Nkind (Decl) = N_Formal_Type_Declaration
14281 and then Nkind (Formal_Type_Definition (Decl)) =
14282 N_Formal_Derived_Type_Definition
14283 and then Synchronized_Present (Formal_Type_Definition (Decl))
14284 and then No (Extension)
14286 -- Avoid emitting a duplicate error message
14288 and then not Error_Posted (Indic)
14291 ("synchronized derived type must have extension", N);
14296 if Null_Exclusion_Present (Def)
14297 and then not Is_Access_Type (Parent_Type)
14299 Error_Msg_N ("null exclusion can only apply to an access type", N);
14302 -- Avoid deriving parent primitives of underlying record views
14304 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
14305 Derive_Subps => not Is_Underlying_Record_View (T));
14307 -- AI-419: The parent type of an explicitly limited derived type must
14308 -- be a limited type or a limited interface.
14310 if Limited_Present (Def) then
14311 Set_Is_Limited_Record (T);
14313 if Is_Interface (T) then
14314 Set_Is_Limited_Interface (T);
14317 if not Is_Limited_Type (Parent_Type)
14319 (not Is_Interface (Parent_Type)
14320 or else not Is_Limited_Interface (Parent_Type))
14322 -- AI05-0096: a derivation in the private part of an instance is
14323 -- legal if the generic formal is untagged limited, and the actual
14326 if Is_Generic_Actual_Type (Parent_Type)
14327 and then In_Private_Part (Current_Scope)
14330 (Generic_Parent_Type (Parent (Parent_Type)))
14336 ("parent type& of limited type must be limited",
14342 -- In SPARK or ALFA, there are no derived type definitions other than
14343 -- type extensions of tagged record types.
14345 if No (Extension) then
14346 Check_Formal_Restriction ("derived type is not allowed", N);
14348 end Derived_Type_Declaration;
14350 ------------------------
14351 -- Diagnose_Interface --
14352 ------------------------
14354 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
14356 if not Is_Interface (E)
14357 and then E /= Any_Type
14359 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
14361 end Diagnose_Interface;
14363 ----------------------------------
14364 -- Enumeration_Type_Declaration --
14365 ----------------------------------
14367 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
14374 -- Create identifier node representing lower bound
14376 B_Node := New_Node (N_Identifier, Sloc (Def));
14377 L := First (Literals (Def));
14378 Set_Chars (B_Node, Chars (L));
14379 Set_Entity (B_Node, L);
14380 Set_Etype (B_Node, T);
14381 Set_Is_Static_Expression (B_Node, True);
14383 R_Node := New_Node (N_Range, Sloc (Def));
14384 Set_Low_Bound (R_Node, B_Node);
14386 Set_Ekind (T, E_Enumeration_Type);
14387 Set_First_Literal (T, L);
14389 Set_Is_Constrained (T);
14393 -- Loop through literals of enumeration type setting pos and rep values
14394 -- except that if the Ekind is already set, then it means the literal
14395 -- was already constructed (case of a derived type declaration and we
14396 -- should not disturb the Pos and Rep values.
14398 while Present (L) loop
14399 if Ekind (L) /= E_Enumeration_Literal then
14400 Set_Ekind (L, E_Enumeration_Literal);
14401 Set_Enumeration_Pos (L, Ev);
14402 Set_Enumeration_Rep (L, Ev);
14403 Set_Is_Known_Valid (L, True);
14407 New_Overloaded_Entity (L);
14408 Generate_Definition (L);
14409 Set_Convention (L, Convention_Intrinsic);
14411 -- Case of character literal
14413 if Nkind (L) = N_Defining_Character_Literal then
14414 Set_Is_Character_Type (T, True);
14416 -- Check violation of No_Wide_Characters
14418 if Restriction_Check_Required (No_Wide_Characters) then
14419 Get_Name_String (Chars (L));
14421 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
14422 Check_Restriction (No_Wide_Characters, L);
14431 -- Now create a node representing upper bound
14433 B_Node := New_Node (N_Identifier, Sloc (Def));
14434 Set_Chars (B_Node, Chars (Last (Literals (Def))));
14435 Set_Entity (B_Node, Last (Literals (Def)));
14436 Set_Etype (B_Node, T);
14437 Set_Is_Static_Expression (B_Node, True);
14439 Set_High_Bound (R_Node, B_Node);
14441 -- Initialize various fields of the type. Some of this information
14442 -- may be overwritten later through rep.clauses.
14444 Set_Scalar_Range (T, R_Node);
14445 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
14446 Set_Enum_Esize (T);
14447 Set_Enum_Pos_To_Rep (T, Empty);
14449 -- Set Discard_Names if configuration pragma set, or if there is
14450 -- a parameterless pragma in the current declarative region
14452 if Global_Discard_Names
14453 or else Discard_Names (Scope (T))
14455 Set_Discard_Names (T);
14458 -- Process end label if there is one
14460 if Present (Def) then
14461 Process_End_Label (Def, 'e', T);
14463 end Enumeration_Type_Declaration;
14465 ---------------------------------
14466 -- Expand_To_Stored_Constraint --
14467 ---------------------------------
14469 function Expand_To_Stored_Constraint
14471 Constraint : Elist_Id) return Elist_Id
14473 Explicitly_Discriminated_Type : Entity_Id;
14474 Expansion : Elist_Id;
14475 Discriminant : Entity_Id;
14477 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
14478 -- Find the nearest type that actually specifies discriminants
14480 ---------------------------------
14481 -- Type_With_Explicit_Discrims --
14482 ---------------------------------
14484 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
14485 Typ : constant E := Base_Type (Id);
14488 if Ekind (Typ) in Incomplete_Or_Private_Kind then
14489 if Present (Full_View (Typ)) then
14490 return Type_With_Explicit_Discrims (Full_View (Typ));
14494 if Has_Discriminants (Typ) then
14499 if Etype (Typ) = Typ then
14501 elsif Has_Discriminants (Typ) then
14504 return Type_With_Explicit_Discrims (Etype (Typ));
14507 end Type_With_Explicit_Discrims;
14509 -- Start of processing for Expand_To_Stored_Constraint
14513 or else Is_Empty_Elmt_List (Constraint)
14518 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
14520 if No (Explicitly_Discriminated_Type) then
14524 Expansion := New_Elmt_List;
14527 First_Stored_Discriminant (Explicitly_Discriminated_Type);
14528 while Present (Discriminant) loop
14530 Get_Discriminant_Value (
14531 Discriminant, Explicitly_Discriminated_Type, Constraint),
14533 Next_Stored_Discriminant (Discriminant);
14537 end Expand_To_Stored_Constraint;
14539 ---------------------------
14540 -- Find_Hidden_Interface --
14541 ---------------------------
14543 function Find_Hidden_Interface
14545 Dest : Elist_Id) return Entity_Id
14548 Iface_Elmt : Elmt_Id;
14551 if Present (Src) and then Present (Dest) then
14552 Iface_Elmt := First_Elmt (Src);
14553 while Present (Iface_Elmt) loop
14554 Iface := Node (Iface_Elmt);
14556 if Is_Interface (Iface)
14557 and then not Contain_Interface (Iface, Dest)
14562 Next_Elmt (Iface_Elmt);
14567 end Find_Hidden_Interface;
14569 --------------------
14570 -- Find_Type_Name --
14571 --------------------
14573 function Find_Type_Name (N : Node_Id) return Entity_Id is
14574 Id : constant Entity_Id := Defining_Identifier (N);
14576 New_Id : Entity_Id;
14577 Prev_Par : Node_Id;
14579 procedure Tag_Mismatch;
14580 -- Diagnose a tagged partial view whose full view is untagged.
14581 -- We post the message on the full view, with a reference to
14582 -- the previous partial view. The partial view can be private
14583 -- or incomplete, and these are handled in a different manner,
14584 -- so we determine the position of the error message from the
14585 -- respective slocs of both.
14591 procedure Tag_Mismatch is
14593 if Sloc (Prev) < Sloc (Id) then
14594 if Ada_Version >= Ada_2012
14595 and then Nkind (N) = N_Private_Type_Declaration
14598 ("declaration of private } must be a tagged type ", Id, Prev);
14601 ("full declaration of } must be a tagged type ", Id, Prev);
14604 if Ada_Version >= Ada_2012
14605 and then Nkind (N) = N_Private_Type_Declaration
14608 ("declaration of private } must be a tagged type ", Prev, Id);
14611 ("full declaration of } must be a tagged type ", Prev, Id);
14616 -- Start of processing for Find_Type_Name
14619 -- Find incomplete declaration, if one was given
14621 Prev := Current_Entity_In_Scope (Id);
14623 -- New type declaration
14629 -- Previous declaration exists
14632 Prev_Par := Parent (Prev);
14634 -- Error if not incomplete/private case except if previous
14635 -- declaration is implicit, etc. Enter_Name will emit error if
14638 if not Is_Incomplete_Or_Private_Type (Prev) then
14642 -- Check invalid completion of private or incomplete type
14644 elsif not Nkind_In (N, N_Full_Type_Declaration,
14645 N_Task_Type_Declaration,
14646 N_Protected_Type_Declaration)
14648 (Ada_Version < Ada_2012
14649 or else not Is_Incomplete_Type (Prev)
14650 or else not Nkind_In (N, N_Private_Type_Declaration,
14651 N_Private_Extension_Declaration))
14653 -- Completion must be a full type declarations (RM 7.3(4))
14655 Error_Msg_Sloc := Sloc (Prev);
14656 Error_Msg_NE ("invalid completion of }", Id, Prev);
14658 -- Set scope of Id to avoid cascaded errors. Entity is never
14659 -- examined again, except when saving globals in generics.
14661 Set_Scope (Id, Current_Scope);
14664 -- If this is a repeated incomplete declaration, no further
14665 -- checks are possible.
14667 if Nkind (N) = N_Incomplete_Type_Declaration then
14671 -- Case of full declaration of incomplete type
14673 elsif Ekind (Prev) = E_Incomplete_Type
14674 and then (Ada_Version < Ada_2012
14675 or else No (Full_View (Prev))
14676 or else not Is_Private_Type (Full_View (Prev)))
14679 -- Indicate that the incomplete declaration has a matching full
14680 -- declaration. The defining occurrence of the incomplete
14681 -- declaration remains the visible one, and the procedure
14682 -- Get_Full_View dereferences it whenever the type is used.
14684 if Present (Full_View (Prev)) then
14685 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
14688 Set_Full_View (Prev, Id);
14689 Append_Entity (Id, Current_Scope);
14690 Set_Is_Public (Id, Is_Public (Prev));
14691 Set_Is_Internal (Id);
14694 -- If the incomplete view is tagged, a class_wide type has been
14695 -- created already. Use it for the private type as well, in order
14696 -- to prevent multiple incompatible class-wide types that may be
14697 -- created for self-referential anonymous access components.
14699 if Is_Tagged_Type (Prev)
14700 and then Present (Class_Wide_Type (Prev))
14702 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
14703 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
14704 Set_Etype (Class_Wide_Type (Id), Id);
14707 -- Case of full declaration of private type
14710 -- If the private type was a completion of an incomplete type then
14711 -- update Prev to reference the private type
14713 if Ada_Version >= Ada_2012
14714 and then Ekind (Prev) = E_Incomplete_Type
14715 and then Present (Full_View (Prev))
14716 and then Is_Private_Type (Full_View (Prev))
14718 Prev := Full_View (Prev);
14719 Prev_Par := Parent (Prev);
14722 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
14723 if Etype (Prev) /= Prev then
14725 -- Prev is a private subtype or a derived type, and needs
14728 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
14731 elsif Ekind (Prev) = E_Private_Type
14732 and then Nkind_In (N, N_Task_Type_Declaration,
14733 N_Protected_Type_Declaration)
14736 ("completion of nonlimited type cannot be limited", N);
14738 elsif Ekind (Prev) = E_Record_Type_With_Private
14739 and then Nkind_In (N, N_Task_Type_Declaration,
14740 N_Protected_Type_Declaration)
14742 if not Is_Limited_Record (Prev) then
14744 ("completion of nonlimited type cannot be limited", N);
14746 elsif No (Interface_List (N)) then
14748 ("completion of tagged private type must be tagged",
14752 elsif Nkind (N) = N_Full_Type_Declaration
14754 Nkind (Type_Definition (N)) = N_Record_Definition
14755 and then Interface_Present (Type_Definition (N))
14758 ("completion of private type cannot be an interface", N);
14761 -- Ada 2005 (AI-251): Private extension declaration of a task
14762 -- type or a protected type. This case arises when covering
14763 -- interface types.
14765 elsif Nkind_In (N, N_Task_Type_Declaration,
14766 N_Protected_Type_Declaration)
14770 elsif Nkind (N) /= N_Full_Type_Declaration
14771 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
14774 ("full view of private extension must be an extension", N);
14776 elsif not (Abstract_Present (Parent (Prev)))
14777 and then Abstract_Present (Type_Definition (N))
14780 ("full view of non-abstract extension cannot be abstract", N);
14783 if not In_Private_Part (Current_Scope) then
14785 ("declaration of full view must appear in private part", N);
14788 Copy_And_Swap (Prev, Id);
14789 Set_Has_Private_Declaration (Prev);
14790 Set_Has_Private_Declaration (Id);
14792 -- If no error, propagate freeze_node from private to full view.
14793 -- It may have been generated for an early operational item.
14795 if Present (Freeze_Node (Id))
14796 and then Serious_Errors_Detected = 0
14797 and then No (Full_View (Id))
14799 Set_Freeze_Node (Prev, Freeze_Node (Id));
14800 Set_Freeze_Node (Id, Empty);
14801 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
14804 Set_Full_View (Id, Prev);
14808 -- Verify that full declaration conforms to partial one
14810 if Is_Incomplete_Or_Private_Type (Prev)
14811 and then Present (Discriminant_Specifications (Prev_Par))
14813 if Present (Discriminant_Specifications (N)) then
14814 if Ekind (Prev) = E_Incomplete_Type then
14815 Check_Discriminant_Conformance (N, Prev, Prev);
14817 Check_Discriminant_Conformance (N, Prev, Id);
14822 ("missing discriminants in full type declaration", N);
14824 -- To avoid cascaded errors on subsequent use, share the
14825 -- discriminants of the partial view.
14827 Set_Discriminant_Specifications (N,
14828 Discriminant_Specifications (Prev_Par));
14832 -- A prior untagged partial view can have an associated class-wide
14833 -- type due to use of the class attribute, and in this case the full
14834 -- type must also be tagged. This Ada 95 usage is deprecated in favor
14835 -- of incomplete tagged declarations, but we check for it.
14838 and then (Is_Tagged_Type (Prev)
14839 or else Present (Class_Wide_Type (Prev)))
14841 -- Ada 2012 (AI05-0162): A private type may be the completion of
14842 -- an incomplete type
14844 if Ada_Version >= Ada_2012
14845 and then Is_Incomplete_Type (Prev)
14846 and then Nkind_In (N, N_Private_Type_Declaration,
14847 N_Private_Extension_Declaration)
14849 -- No need to check private extensions since they are tagged
14851 if Nkind (N) = N_Private_Type_Declaration
14852 and then not Tagged_Present (N)
14857 -- The full declaration is either a tagged type (including
14858 -- a synchronized type that implements interfaces) or a
14859 -- type extension, otherwise this is an error.
14861 elsif Nkind_In (N, N_Task_Type_Declaration,
14862 N_Protected_Type_Declaration)
14864 if No (Interface_List (N))
14865 and then not Error_Posted (N)
14870 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
14872 -- Indicate that the previous declaration (tagged incomplete
14873 -- or private declaration) requires the same on the full one.
14875 if not Tagged_Present (Type_Definition (N)) then
14877 Set_Is_Tagged_Type (Id);
14880 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
14881 if No (Record_Extension_Part (Type_Definition (N))) then
14883 ("full declaration of } must be a record extension",
14886 -- Set some attributes to produce a usable full view
14888 Set_Is_Tagged_Type (Id);
14898 end Find_Type_Name;
14900 -------------------------
14901 -- Find_Type_Of_Object --
14902 -------------------------
14904 function Find_Type_Of_Object
14905 (Obj_Def : Node_Id;
14906 Related_Nod : Node_Id) return Entity_Id
14908 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
14909 P : Node_Id := Parent (Obj_Def);
14914 -- If the parent is a component_definition node we climb to the
14915 -- component_declaration node
14917 if Nkind (P) = N_Component_Definition then
14921 -- Case of an anonymous array subtype
14923 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
14924 N_Unconstrained_Array_Definition)
14927 Array_Type_Declaration (T, Obj_Def);
14929 -- Create an explicit subtype whenever possible
14931 elsif Nkind (P) /= N_Component_Declaration
14932 and then Def_Kind = N_Subtype_Indication
14934 -- Base name of subtype on object name, which will be unique in
14935 -- the current scope.
14937 -- If this is a duplicate declaration, return base type, to avoid
14938 -- generating duplicate anonymous types.
14940 if Error_Posted (P) then
14941 Analyze (Subtype_Mark (Obj_Def));
14942 return Entity (Subtype_Mark (Obj_Def));
14947 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
14949 T := Make_Defining_Identifier (Sloc (P), Nam);
14951 Insert_Action (Obj_Def,
14952 Make_Subtype_Declaration (Sloc (P),
14953 Defining_Identifier => T,
14954 Subtype_Indication => Relocate_Node (Obj_Def)));
14956 -- This subtype may need freezing, and this will not be done
14957 -- automatically if the object declaration is not in declarative
14958 -- part. Since this is an object declaration, the type cannot always
14959 -- be frozen here. Deferred constants do not freeze their type
14960 -- (which often enough will be private).
14962 if Nkind (P) = N_Object_Declaration
14963 and then Constant_Present (P)
14964 and then No (Expression (P))
14968 Insert_Actions (Obj_Def, Freeze_Entity (T, P));
14971 -- Ada 2005 AI-406: the object definition in an object declaration
14972 -- can be an access definition.
14974 elsif Def_Kind = N_Access_Definition then
14975 T := Access_Definition (Related_Nod, Obj_Def);
14976 Set_Is_Local_Anonymous_Access (T);
14978 -- Otherwise, the object definition is just a subtype_mark
14981 T := Process_Subtype (Obj_Def, Related_Nod);
14983 -- If expansion is disabled an object definition that is an aggregate
14984 -- will not get expanded and may lead to scoping problems in the back
14985 -- end, if the object is referenced in an inner scope. In that case
14986 -- create an itype reference for the object definition now. This
14987 -- may be redundant in some cases, but harmless.
14990 and then Nkind (Related_Nod) = N_Object_Declaration
14993 Build_Itype_Reference (T, Related_Nod);
14998 end Find_Type_Of_Object;
15000 --------------------------------
15001 -- Find_Type_Of_Subtype_Indic --
15002 --------------------------------
15004 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
15008 -- Case of subtype mark with a constraint
15010 if Nkind (S) = N_Subtype_Indication then
15011 Find_Type (Subtype_Mark (S));
15012 Typ := Entity (Subtype_Mark (S));
15015 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
15018 ("incorrect constraint for this kind of type", Constraint (S));
15019 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
15022 -- Otherwise we have a subtype mark without a constraint
15024 elsif Error_Posted (S) then
15025 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
15033 -- Check No_Wide_Characters restriction
15035 Check_Wide_Character_Restriction (Typ, S);
15038 end Find_Type_Of_Subtype_Indic;
15040 -------------------------------------
15041 -- Floating_Point_Type_Declaration --
15042 -------------------------------------
15044 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
15045 Digs : constant Node_Id := Digits_Expression (Def);
15046 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
15048 Base_Typ : Entity_Id;
15049 Implicit_Base : Entity_Id;
15052 function Can_Derive_From (E : Entity_Id) return Boolean;
15053 -- Find if given digits value, and possibly a specified range, allows
15054 -- derivation from specified type
15056 ---------------------
15057 -- Can_Derive_From --
15058 ---------------------
15060 function Can_Derive_From (E : Entity_Id) return Boolean is
15061 Spec : constant Entity_Id := Real_Range_Specification (Def);
15064 if Digs_Val > Digits_Value (E) then
15068 if Present (Spec) then
15069 if Expr_Value_R (Type_Low_Bound (E)) >
15070 Expr_Value_R (Low_Bound (Spec))
15075 if Expr_Value_R (Type_High_Bound (E)) <
15076 Expr_Value_R (High_Bound (Spec))
15083 end Can_Derive_From;
15085 -- Start of processing for Floating_Point_Type_Declaration
15088 Check_Restriction (No_Floating_Point, Def);
15090 -- Create an implicit base type
15093 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
15095 -- Analyze and verify digits value
15097 Analyze_And_Resolve (Digs, Any_Integer);
15098 Check_Digits_Expression (Digs);
15099 Digs_Val := Expr_Value (Digs);
15101 -- Process possible range spec and find correct type to derive from
15103 Process_Real_Range_Specification (Def);
15105 -- Check that requested number of digits is not too high.
15107 if Digs_Val > Max_Digs_Val then
15108 -- The check for Max_Base_Digits may be somewhat expensive, as it
15109 -- requires reading System, so only do it when necessary.
15112 Max_Base_Digits : constant Uint :=
15113 Expr_Value (Expression (Parent (RTE (RE_Max_Base_Digits))));
15115 if Digs_Val > Max_Base_Digits then
15116 Error_Msg_Uint_1 := Max_Base_Digits;
15117 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
15119 elsif No (Real_Range_Specification (Def)) then
15120 Error_Msg_Uint_1 := Max_Digs_Val;
15121 Error_Msg_N ("types with more than ^ digits need range spec "
15122 & "('R'M 3.5.7(6))", Digs);
15127 Base_Typ := First (Predefined_Float_Types);
15129 while Present (Base_Typ) and then not Can_Derive_From (Base_Typ) loop
15133 -- If we can't derive from any existing type, use Long_Long_Float
15134 -- and give appropriate message explaining the problem.
15136 if No (Base_Typ) then
15137 Base_Typ := Standard_Long_Long_Float;
15139 if Digs_Val > Max_Digs_Val then
15140 -- It might be the case that there is a type with the requested
15141 -- range, just not the combination of digits and range.
15144 ("no predefined type has requested range and precision",
15145 Real_Range_Specification (Def));
15149 ("range too large for any predefined type",
15150 Real_Range_Specification (Def));
15154 -- If there are bounds given in the declaration use them as the bounds
15155 -- of the type, otherwise use the bounds of the predefined base type
15156 -- that was chosen based on the Digits value.
15158 if Present (Real_Range_Specification (Def)) then
15159 Set_Scalar_Range (T, Real_Range_Specification (Def));
15160 Set_Is_Constrained (T);
15162 -- The bounds of this range must be converted to machine numbers
15163 -- in accordance with RM 4.9(38).
15165 Bound := Type_Low_Bound (T);
15167 if Nkind (Bound) = N_Real_Literal then
15169 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
15170 Set_Is_Machine_Number (Bound);
15173 Bound := Type_High_Bound (T);
15175 if Nkind (Bound) = N_Real_Literal then
15177 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
15178 Set_Is_Machine_Number (Bound);
15182 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
15185 -- Complete definition of implicit base and declared first subtype
15187 Set_Etype (Implicit_Base, Base_Typ);
15189 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
15190 Set_Size_Info (Implicit_Base, (Base_Typ));
15191 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
15192 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
15193 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
15194 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
15196 Set_Ekind (T, E_Floating_Point_Subtype);
15197 Set_Etype (T, Implicit_Base);
15199 Set_Size_Info (T, (Implicit_Base));
15200 Set_RM_Size (T, RM_Size (Implicit_Base));
15201 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
15202 Set_Digits_Value (T, Digs_Val);
15203 end Floating_Point_Type_Declaration;
15205 ----------------------------
15206 -- Get_Discriminant_Value --
15207 ----------------------------
15209 -- This is the situation:
15211 -- There is a non-derived type
15213 -- type T0 (Dx, Dy, Dz...)
15215 -- There are zero or more levels of derivation, with each derivation
15216 -- either purely inheriting the discriminants, or defining its own.
15218 -- type Ti is new Ti-1
15220 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
15222 -- subtype Ti is ...
15224 -- The subtype issue is avoided by the use of Original_Record_Component,
15225 -- and the fact that derived subtypes also derive the constraints.
15227 -- This chain leads back from
15229 -- Typ_For_Constraint
15231 -- Typ_For_Constraint has discriminants, and the value for each
15232 -- discriminant is given by its corresponding Elmt of Constraints.
15234 -- Discriminant is some discriminant in this hierarchy
15236 -- We need to return its value
15238 -- We do this by recursively searching each level, and looking for
15239 -- Discriminant. Once we get to the bottom, we start backing up
15240 -- returning the value for it which may in turn be a discriminant
15241 -- further up, so on the backup we continue the substitution.
15243 function Get_Discriminant_Value
15244 (Discriminant : Entity_Id;
15245 Typ_For_Constraint : Entity_Id;
15246 Constraint : Elist_Id) return Node_Id
15248 function Search_Derivation_Levels
15250 Discrim_Values : Elist_Id;
15251 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
15252 -- This is the routine that performs the recursive search of levels
15253 -- as described above.
15255 ------------------------------
15256 -- Search_Derivation_Levels --
15257 ------------------------------
15259 function Search_Derivation_Levels
15261 Discrim_Values : Elist_Id;
15262 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
15266 Result : Node_Or_Entity_Id;
15267 Result_Entity : Node_Id;
15270 -- If inappropriate type, return Error, this happens only in
15271 -- cascaded error situations, and we want to avoid a blow up.
15273 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
15277 -- Look deeper if possible. Use Stored_Constraints only for
15278 -- untagged types. For tagged types use the given constraint.
15279 -- This asymmetry needs explanation???
15281 if not Stored_Discrim_Values
15282 and then Present (Stored_Constraint (Ti))
15283 and then not Is_Tagged_Type (Ti)
15286 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
15289 Td : constant Entity_Id := Etype (Ti);
15293 Result := Discriminant;
15296 if Present (Stored_Constraint (Ti)) then
15298 Search_Derivation_Levels
15299 (Td, Stored_Constraint (Ti), True);
15302 Search_Derivation_Levels
15303 (Td, Discrim_Values, Stored_Discrim_Values);
15309 -- Extra underlying places to search, if not found above. For
15310 -- concurrent types, the relevant discriminant appears in the
15311 -- corresponding record. For a type derived from a private type
15312 -- without discriminant, the full view inherits the discriminants
15313 -- of the full view of the parent.
15315 if Result = Discriminant then
15316 if Is_Concurrent_Type (Ti)
15317 and then Present (Corresponding_Record_Type (Ti))
15320 Search_Derivation_Levels (
15321 Corresponding_Record_Type (Ti),
15323 Stored_Discrim_Values);
15325 elsif Is_Private_Type (Ti)
15326 and then not Has_Discriminants (Ti)
15327 and then Present (Full_View (Ti))
15328 and then Etype (Full_View (Ti)) /= Ti
15331 Search_Derivation_Levels (
15334 Stored_Discrim_Values);
15338 -- If Result is not a (reference to a) discriminant, return it,
15339 -- otherwise set Result_Entity to the discriminant.
15341 if Nkind (Result) = N_Defining_Identifier then
15342 pragma Assert (Result = Discriminant);
15343 Result_Entity := Result;
15346 if not Denotes_Discriminant (Result) then
15350 Result_Entity := Entity (Result);
15353 -- See if this level of derivation actually has discriminants
15354 -- because tagged derivations can add them, hence the lower
15355 -- levels need not have any.
15357 if not Has_Discriminants (Ti) then
15361 -- Scan Ti's discriminants for Result_Entity,
15362 -- and return its corresponding value, if any.
15364 Result_Entity := Original_Record_Component (Result_Entity);
15366 Assoc := First_Elmt (Discrim_Values);
15368 if Stored_Discrim_Values then
15369 Disc := First_Stored_Discriminant (Ti);
15371 Disc := First_Discriminant (Ti);
15374 while Present (Disc) loop
15375 pragma Assert (Present (Assoc));
15377 if Original_Record_Component (Disc) = Result_Entity then
15378 return Node (Assoc);
15383 if Stored_Discrim_Values then
15384 Next_Stored_Discriminant (Disc);
15386 Next_Discriminant (Disc);
15390 -- Could not find it
15393 end Search_Derivation_Levels;
15397 Result : Node_Or_Entity_Id;
15399 -- Start of processing for Get_Discriminant_Value
15402 -- ??? This routine is a gigantic mess and will be deleted. For the
15403 -- time being just test for the trivial case before calling recurse.
15405 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
15411 D := First_Discriminant (Typ_For_Constraint);
15412 E := First_Elmt (Constraint);
15413 while Present (D) loop
15414 if Chars (D) = Chars (Discriminant) then
15418 Next_Discriminant (D);
15424 Result := Search_Derivation_Levels
15425 (Typ_For_Constraint, Constraint, False);
15427 -- ??? hack to disappear when this routine is gone
15429 if Nkind (Result) = N_Defining_Identifier then
15435 D := First_Discriminant (Typ_For_Constraint);
15436 E := First_Elmt (Constraint);
15437 while Present (D) loop
15438 if Corresponding_Discriminant (D) = Discriminant then
15442 Next_Discriminant (D);
15448 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
15450 end Get_Discriminant_Value;
15452 --------------------------
15453 -- Has_Range_Constraint --
15454 --------------------------
15456 function Has_Range_Constraint (N : Node_Id) return Boolean is
15457 C : constant Node_Id := Constraint (N);
15460 if Nkind (C) = N_Range_Constraint then
15463 elsif Nkind (C) = N_Digits_Constraint then
15465 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
15467 Present (Range_Constraint (C));
15469 elsif Nkind (C) = N_Delta_Constraint then
15470 return Present (Range_Constraint (C));
15475 end Has_Range_Constraint;
15477 ------------------------
15478 -- Inherit_Components --
15479 ------------------------
15481 function Inherit_Components
15483 Parent_Base : Entity_Id;
15484 Derived_Base : Entity_Id;
15485 Is_Tagged : Boolean;
15486 Inherit_Discr : Boolean;
15487 Discs : Elist_Id) return Elist_Id
15489 Assoc_List : constant Elist_Id := New_Elmt_List;
15491 procedure Inherit_Component
15492 (Old_C : Entity_Id;
15493 Plain_Discrim : Boolean := False;
15494 Stored_Discrim : Boolean := False);
15495 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
15496 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
15497 -- True, Old_C is a stored discriminant. If they are both false then
15498 -- Old_C is a regular component.
15500 -----------------------
15501 -- Inherit_Component --
15502 -----------------------
15504 procedure Inherit_Component
15505 (Old_C : Entity_Id;
15506 Plain_Discrim : Boolean := False;
15507 Stored_Discrim : Boolean := False)
15509 New_C : constant Entity_Id := New_Copy (Old_C);
15511 Discrim : Entity_Id;
15512 Corr_Discrim : Entity_Id;
15515 pragma Assert (not Is_Tagged or else not Stored_Discrim);
15517 Set_Parent (New_C, Parent (Old_C));
15519 -- Regular discriminants and components must be inserted in the scope
15520 -- of the Derived_Base. Do it here.
15522 if not Stored_Discrim then
15523 Enter_Name (New_C);
15526 -- For tagged types the Original_Record_Component must point to
15527 -- whatever this field was pointing to in the parent type. This has
15528 -- already been achieved by the call to New_Copy above.
15530 if not Is_Tagged then
15531 Set_Original_Record_Component (New_C, New_C);
15534 -- If we have inherited a component then see if its Etype contains
15535 -- references to Parent_Base discriminants. In this case, replace
15536 -- these references with the constraints given in Discs. We do not
15537 -- do this for the partial view of private types because this is
15538 -- not needed (only the components of the full view will be used
15539 -- for code generation) and cause problem. We also avoid this
15540 -- transformation in some error situations.
15542 if Ekind (New_C) = E_Component then
15543 if (Is_Private_Type (Derived_Base)
15544 and then not Is_Generic_Type (Derived_Base))
15545 or else (Is_Empty_Elmt_List (Discs)
15546 and then not Expander_Active)
15548 Set_Etype (New_C, Etype (Old_C));
15551 -- The current component introduces a circularity of the
15554 -- limited with Pack_2;
15555 -- package Pack_1 is
15556 -- type T_1 is tagged record
15557 -- Comp : access Pack_2.T_2;
15563 -- package Pack_2 is
15564 -- type T_2 is new Pack_1.T_1 with ...;
15569 Constrain_Component_Type
15570 (Old_C, Derived_Base, N, Parent_Base, Discs));
15574 -- In derived tagged types it is illegal to reference a non
15575 -- discriminant component in the parent type. To catch this, mark
15576 -- these components with an Ekind of E_Void. This will be reset in
15577 -- Record_Type_Definition after processing the record extension of
15578 -- the derived type.
15580 -- If the declaration is a private extension, there is no further
15581 -- record extension to process, and the components retain their
15582 -- current kind, because they are visible at this point.
15584 if Is_Tagged and then Ekind (New_C) = E_Component
15585 and then Nkind (N) /= N_Private_Extension_Declaration
15587 Set_Ekind (New_C, E_Void);
15590 if Plain_Discrim then
15591 Set_Corresponding_Discriminant (New_C, Old_C);
15592 Build_Discriminal (New_C);
15594 -- If we are explicitly inheriting a stored discriminant it will be
15595 -- completely hidden.
15597 elsif Stored_Discrim then
15598 Set_Corresponding_Discriminant (New_C, Empty);
15599 Set_Discriminal (New_C, Empty);
15600 Set_Is_Completely_Hidden (New_C);
15602 -- Set the Original_Record_Component of each discriminant in the
15603 -- derived base to point to the corresponding stored that we just
15606 Discrim := First_Discriminant (Derived_Base);
15607 while Present (Discrim) loop
15608 Corr_Discrim := Corresponding_Discriminant (Discrim);
15610 -- Corr_Discrim could be missing in an error situation
15612 if Present (Corr_Discrim)
15613 and then Original_Record_Component (Corr_Discrim) = Old_C
15615 Set_Original_Record_Component (Discrim, New_C);
15618 Next_Discriminant (Discrim);
15621 Append_Entity (New_C, Derived_Base);
15624 if not Is_Tagged then
15625 Append_Elmt (Old_C, Assoc_List);
15626 Append_Elmt (New_C, Assoc_List);
15628 end Inherit_Component;
15630 -- Variables local to Inherit_Component
15632 Loc : constant Source_Ptr := Sloc (N);
15634 Parent_Discrim : Entity_Id;
15635 Stored_Discrim : Entity_Id;
15637 Component : Entity_Id;
15639 -- Start of processing for Inherit_Components
15642 if not Is_Tagged then
15643 Append_Elmt (Parent_Base, Assoc_List);
15644 Append_Elmt (Derived_Base, Assoc_List);
15647 -- Inherit parent discriminants if needed
15649 if Inherit_Discr then
15650 Parent_Discrim := First_Discriminant (Parent_Base);
15651 while Present (Parent_Discrim) loop
15652 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
15653 Next_Discriminant (Parent_Discrim);
15657 -- Create explicit stored discrims for untagged types when necessary
15659 if not Has_Unknown_Discriminants (Derived_Base)
15660 and then Has_Discriminants (Parent_Base)
15661 and then not Is_Tagged
15664 or else First_Discriminant (Parent_Base) /=
15665 First_Stored_Discriminant (Parent_Base))
15667 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
15668 while Present (Stored_Discrim) loop
15669 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
15670 Next_Stored_Discriminant (Stored_Discrim);
15674 -- See if we can apply the second transformation for derived types, as
15675 -- explained in point 6. in the comments above Build_Derived_Record_Type
15676 -- This is achieved by appending Derived_Base discriminants into Discs,
15677 -- which has the side effect of returning a non empty Discs list to the
15678 -- caller of Inherit_Components, which is what we want. This must be
15679 -- done for private derived types if there are explicit stored
15680 -- discriminants, to ensure that we can retrieve the values of the
15681 -- constraints provided in the ancestors.
15684 and then Is_Empty_Elmt_List (Discs)
15685 and then Present (First_Discriminant (Derived_Base))
15687 (not Is_Private_Type (Derived_Base)
15688 or else Is_Completely_Hidden
15689 (First_Stored_Discriminant (Derived_Base))
15690 or else Is_Generic_Type (Derived_Base))
15692 D := First_Discriminant (Derived_Base);
15693 while Present (D) loop
15694 Append_Elmt (New_Reference_To (D, Loc), Discs);
15695 Next_Discriminant (D);
15699 -- Finally, inherit non-discriminant components unless they are not
15700 -- visible because defined or inherited from the full view of the
15701 -- parent. Don't inherit the _parent field of the parent type.
15703 Component := First_Entity (Parent_Base);
15704 while Present (Component) loop
15706 -- Ada 2005 (AI-251): Do not inherit components associated with
15707 -- secondary tags of the parent.
15709 if Ekind (Component) = E_Component
15710 and then Present (Related_Type (Component))
15714 elsif Ekind (Component) /= E_Component
15715 or else Chars (Component) = Name_uParent
15719 -- If the derived type is within the parent type's declarative
15720 -- region, then the components can still be inherited even though
15721 -- they aren't visible at this point. This can occur for cases
15722 -- such as within public child units where the components must
15723 -- become visible upon entering the child unit's private part.
15725 elsif not Is_Visible_Component (Component)
15726 and then not In_Open_Scopes (Scope (Parent_Base))
15730 elsif Ekind_In (Derived_Base, E_Private_Type,
15731 E_Limited_Private_Type)
15736 Inherit_Component (Component);
15739 Next_Entity (Component);
15742 -- For tagged derived types, inherited discriminants cannot be used in
15743 -- component declarations of the record extension part. To achieve this
15744 -- we mark the inherited discriminants as not visible.
15746 if Is_Tagged and then Inherit_Discr then
15747 D := First_Discriminant (Derived_Base);
15748 while Present (D) loop
15749 Set_Is_Immediately_Visible (D, False);
15750 Next_Discriminant (D);
15755 end Inherit_Components;
15757 -----------------------
15758 -- Is_Constant_Bound --
15759 -----------------------
15761 function Is_Constant_Bound (Exp : Node_Id) return Boolean is
15763 if Compile_Time_Known_Value (Exp) then
15766 elsif Is_Entity_Name (Exp)
15767 and then Present (Entity (Exp))
15769 return Is_Constant_Object (Entity (Exp))
15770 or else Ekind (Entity (Exp)) = E_Enumeration_Literal;
15772 elsif Nkind (Exp) in N_Binary_Op then
15773 return Is_Constant_Bound (Left_Opnd (Exp))
15774 and then Is_Constant_Bound (Right_Opnd (Exp))
15775 and then Scope (Entity (Exp)) = Standard_Standard;
15780 end Is_Constant_Bound;
15782 -----------------------
15783 -- Is_Null_Extension --
15784 -----------------------
15786 function Is_Null_Extension (T : Entity_Id) return Boolean is
15787 Type_Decl : constant Node_Id := Parent (Base_Type (T));
15788 Comp_List : Node_Id;
15792 if Nkind (Type_Decl) /= N_Full_Type_Declaration
15793 or else not Is_Tagged_Type (T)
15794 or else Nkind (Type_Definition (Type_Decl)) /=
15795 N_Derived_Type_Definition
15796 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
15802 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
15804 if Present (Discriminant_Specifications (Type_Decl)) then
15807 elsif Present (Comp_List)
15808 and then Is_Non_Empty_List (Component_Items (Comp_List))
15810 Comp := First (Component_Items (Comp_List));
15812 -- Only user-defined components are relevant. The component list
15813 -- may also contain a parent component and internal components
15814 -- corresponding to secondary tags, but these do not determine
15815 -- whether this is a null extension.
15817 while Present (Comp) loop
15818 if Comes_From_Source (Comp) then
15829 end Is_Null_Extension;
15831 ------------------------------
15832 -- Is_Valid_Constraint_Kind --
15833 ------------------------------
15835 function Is_Valid_Constraint_Kind
15836 (T_Kind : Type_Kind;
15837 Constraint_Kind : Node_Kind) return Boolean
15841 when Enumeration_Kind |
15843 return Constraint_Kind = N_Range_Constraint;
15845 when Decimal_Fixed_Point_Kind =>
15846 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
15847 N_Range_Constraint);
15849 when Ordinary_Fixed_Point_Kind =>
15850 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
15851 N_Range_Constraint);
15854 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
15855 N_Range_Constraint);
15862 E_Incomplete_Type |
15865 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
15868 return True; -- Error will be detected later
15870 end Is_Valid_Constraint_Kind;
15872 --------------------------
15873 -- Is_Visible_Component --
15874 --------------------------
15876 function Is_Visible_Component (C : Entity_Id) return Boolean is
15877 Original_Comp : Entity_Id := Empty;
15878 Original_Scope : Entity_Id;
15879 Type_Scope : Entity_Id;
15881 function Is_Local_Type (Typ : Entity_Id) return Boolean;
15882 -- Check whether parent type of inherited component is declared locally,
15883 -- possibly within a nested package or instance. The current scope is
15884 -- the derived record itself.
15886 -------------------
15887 -- Is_Local_Type --
15888 -------------------
15890 function Is_Local_Type (Typ : Entity_Id) return Boolean is
15894 Scop := Scope (Typ);
15895 while Present (Scop)
15896 and then Scop /= Standard_Standard
15898 if Scop = Scope (Current_Scope) then
15902 Scop := Scope (Scop);
15908 -- Start of processing for Is_Visible_Component
15911 if Ekind_In (C, E_Component, E_Discriminant) then
15912 Original_Comp := Original_Record_Component (C);
15915 if No (Original_Comp) then
15917 -- Premature usage, or previous error
15922 Original_Scope := Scope (Original_Comp);
15923 Type_Scope := Scope (Base_Type (Scope (C)));
15926 -- This test only concerns tagged types
15928 if not Is_Tagged_Type (Original_Scope) then
15931 -- If it is _Parent or _Tag, there is no visibility issue
15933 elsif not Comes_From_Source (Original_Comp) then
15936 -- If we are in the body of an instantiation, the component is visible
15937 -- even when the parent type (possibly defined in an enclosing unit or
15938 -- in a parent unit) might not.
15940 elsif In_Instance_Body then
15943 -- Discriminants are always visible
15945 elsif Ekind (Original_Comp) = E_Discriminant
15946 and then not Has_Unknown_Discriminants (Original_Scope)
15950 -- If the component has been declared in an ancestor which is currently
15951 -- a private type, then it is not visible. The same applies if the
15952 -- component's containing type is not in an open scope and the original
15953 -- component's enclosing type is a visible full view of a private type
15954 -- (which can occur in cases where an attempt is being made to reference
15955 -- a component in a sibling package that is inherited from a visible
15956 -- component of a type in an ancestor package; the component in the
15957 -- sibling package should not be visible even though the component it
15958 -- inherited from is visible). This does not apply however in the case
15959 -- where the scope of the type is a private child unit, or when the
15960 -- parent comes from a local package in which the ancestor is currently
15961 -- visible. The latter suppression of visibility is needed for cases
15962 -- that are tested in B730006.
15964 elsif Is_Private_Type (Original_Scope)
15966 (not Is_Private_Descendant (Type_Scope)
15967 and then not In_Open_Scopes (Type_Scope)
15968 and then Has_Private_Declaration (Original_Scope))
15970 -- If the type derives from an entity in a formal package, there
15971 -- are no additional visible components.
15973 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
15974 N_Formal_Package_Declaration
15978 -- if we are not in the private part of the current package, there
15979 -- are no additional visible components.
15981 elsif Ekind (Scope (Current_Scope)) = E_Package
15982 and then not In_Private_Part (Scope (Current_Scope))
15987 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
15988 and then In_Open_Scopes (Scope (Original_Scope))
15989 and then Is_Local_Type (Type_Scope);
15992 -- There is another weird way in which a component may be invisible
15993 -- when the private and the full view are not derived from the same
15994 -- ancestor. Here is an example :
15996 -- type A1 is tagged record F1 : integer; end record;
15997 -- type A2 is new A1 with record F2 : integer; end record;
15998 -- type T is new A1 with private;
16000 -- type T is new A2 with null record;
16002 -- In this case, the full view of T inherits F1 and F2 but the private
16003 -- view inherits only F1
16007 Ancestor : Entity_Id := Scope (C);
16011 if Ancestor = Original_Scope then
16013 elsif Ancestor = Etype (Ancestor) then
16017 Ancestor := Etype (Ancestor);
16021 end Is_Visible_Component;
16023 --------------------------
16024 -- Make_Class_Wide_Type --
16025 --------------------------
16027 procedure Make_Class_Wide_Type (T : Entity_Id) is
16028 CW_Type : Entity_Id;
16030 Next_E : Entity_Id;
16033 -- The class wide type can have been defined by the partial view, in
16034 -- which case everything is already done.
16036 if Present (Class_Wide_Type (T)) then
16041 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
16043 -- Inherit root type characteristics
16045 CW_Name := Chars (CW_Type);
16046 Next_E := Next_Entity (CW_Type);
16047 Copy_Node (T, CW_Type);
16048 Set_Comes_From_Source (CW_Type, False);
16049 Set_Chars (CW_Type, CW_Name);
16050 Set_Parent (CW_Type, Parent (T));
16051 Set_Next_Entity (CW_Type, Next_E);
16053 -- Ensure we have a new freeze node for the class-wide type. The partial
16054 -- view may have freeze action of its own, requiring a proper freeze
16055 -- node, and the same freeze node cannot be shared between the two
16058 Set_Has_Delayed_Freeze (CW_Type);
16059 Set_Freeze_Node (CW_Type, Empty);
16061 -- Customize the class-wide type: It has no prim. op., it cannot be
16062 -- abstract and its Etype points back to the specific root type.
16064 Set_Ekind (CW_Type, E_Class_Wide_Type);
16065 Set_Is_Tagged_Type (CW_Type, True);
16066 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
16067 Set_Is_Abstract_Type (CW_Type, False);
16068 Set_Is_Constrained (CW_Type, False);
16069 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
16071 if Ekind (T) = E_Class_Wide_Subtype then
16072 Set_Etype (CW_Type, Etype (Base_Type (T)));
16074 Set_Etype (CW_Type, T);
16077 -- If this is the class_wide type of a constrained subtype, it does
16078 -- not have discriminants.
16080 Set_Has_Discriminants (CW_Type,
16081 Has_Discriminants (T) and then not Is_Constrained (T));
16083 Set_Has_Unknown_Discriminants (CW_Type, True);
16084 Set_Class_Wide_Type (T, CW_Type);
16085 Set_Equivalent_Type (CW_Type, Empty);
16087 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
16089 Set_Class_Wide_Type (CW_Type, CW_Type);
16090 end Make_Class_Wide_Type;
16096 procedure Make_Index
16098 Related_Nod : Node_Id;
16099 Related_Id : Entity_Id := Empty;
16100 Suffix_Index : Nat := 1)
16104 Def_Id : Entity_Id := Empty;
16105 Found : Boolean := False;
16108 -- For a discrete range used in a constrained array definition and
16109 -- defined by a range, an implicit conversion to the predefined type
16110 -- INTEGER is assumed if each bound is either a numeric literal, a named
16111 -- number, or an attribute, and the type of both bounds (prior to the
16112 -- implicit conversion) is the type universal_integer. Otherwise, both
16113 -- bounds must be of the same discrete type, other than universal
16114 -- integer; this type must be determinable independently of the
16115 -- context, but using the fact that the type must be discrete and that
16116 -- both bounds must have the same type.
16118 -- Character literals also have a universal type in the absence of
16119 -- of additional context, and are resolved to Standard_Character.
16121 if Nkind (I) = N_Range then
16123 -- The index is given by a range constraint. The bounds are known
16124 -- to be of a consistent type.
16126 if not Is_Overloaded (I) then
16129 -- For universal bounds, choose the specific predefined type
16131 if T = Universal_Integer then
16132 T := Standard_Integer;
16134 elsif T = Any_Character then
16135 Ambiguous_Character (Low_Bound (I));
16137 T := Standard_Character;
16140 -- The node may be overloaded because some user-defined operators
16141 -- are available, but if a universal interpretation exists it is
16142 -- also the selected one.
16144 elsif Universal_Interpretation (I) = Universal_Integer then
16145 T := Standard_Integer;
16151 Ind : Interp_Index;
16155 Get_First_Interp (I, Ind, It);
16156 while Present (It.Typ) loop
16157 if Is_Discrete_Type (It.Typ) then
16160 and then not Covers (It.Typ, T)
16161 and then not Covers (T, It.Typ)
16163 Error_Msg_N ("ambiguous bounds in discrete range", I);
16171 Get_Next_Interp (Ind, It);
16174 if T = Any_Type then
16175 Error_Msg_N ("discrete type required for range", I);
16176 Set_Etype (I, Any_Type);
16179 elsif T = Universal_Integer then
16180 T := Standard_Integer;
16185 if not Is_Discrete_Type (T) then
16186 Error_Msg_N ("discrete type required for range", I);
16187 Set_Etype (I, Any_Type);
16191 if Nkind (Low_Bound (I)) = N_Attribute_Reference
16192 and then Attribute_Name (Low_Bound (I)) = Name_First
16193 and then Is_Entity_Name (Prefix (Low_Bound (I)))
16194 and then Is_Type (Entity (Prefix (Low_Bound (I))))
16195 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (I))))
16197 -- The type of the index will be the type of the prefix, as long
16198 -- as the upper bound is 'Last of the same type.
16200 Def_Id := Entity (Prefix (Low_Bound (I)));
16202 if Nkind (High_Bound (I)) /= N_Attribute_Reference
16203 or else Attribute_Name (High_Bound (I)) /= Name_Last
16204 or else not Is_Entity_Name (Prefix (High_Bound (I)))
16205 or else Entity (Prefix (High_Bound (I))) /= Def_Id
16212 Process_Range_Expr_In_Decl (R, T);
16214 elsif Nkind (I) = N_Subtype_Indication then
16216 -- The index is given by a subtype with a range constraint
16218 T := Base_Type (Entity (Subtype_Mark (I)));
16220 if not Is_Discrete_Type (T) then
16221 Error_Msg_N ("discrete type required for range", I);
16222 Set_Etype (I, Any_Type);
16226 R := Range_Expression (Constraint (I));
16229 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (I)));
16231 elsif Nkind (I) = N_Attribute_Reference then
16233 -- The parser guarantees that the attribute is a RANGE attribute
16235 -- If the node denotes the range of a type mark, that is also the
16236 -- resulting type, and we do no need to create an Itype for it.
16238 if Is_Entity_Name (Prefix (I))
16239 and then Comes_From_Source (I)
16240 and then Is_Type (Entity (Prefix (I)))
16241 and then Is_Discrete_Type (Entity (Prefix (I)))
16243 Def_Id := Entity (Prefix (I));
16246 Analyze_And_Resolve (I);
16250 -- If none of the above, must be a subtype. We convert this to a
16251 -- range attribute reference because in the case of declared first
16252 -- named subtypes, the types in the range reference can be different
16253 -- from the type of the entity. A range attribute normalizes the
16254 -- reference and obtains the correct types for the bounds.
16256 -- This transformation is in the nature of an expansion, is only
16257 -- done if expansion is active. In particular, it is not done on
16258 -- formal generic types, because we need to retain the name of the
16259 -- original index for instantiation purposes.
16262 if not Is_Entity_Name (I) or else not Is_Type (Entity (I)) then
16263 Error_Msg_N ("invalid subtype mark in discrete range ", I);
16264 Set_Etype (I, Any_Integer);
16268 -- The type mark may be that of an incomplete type. It is only
16269 -- now that we can get the full view, previous analysis does
16270 -- not look specifically for a type mark.
16272 Set_Entity (I, Get_Full_View (Entity (I)));
16273 Set_Etype (I, Entity (I));
16274 Def_Id := Entity (I);
16276 if not Is_Discrete_Type (Def_Id) then
16277 Error_Msg_N ("discrete type required for index", I);
16278 Set_Etype (I, Any_Type);
16283 if Expander_Active then
16285 Make_Attribute_Reference (Sloc (I),
16286 Attribute_Name => Name_Range,
16287 Prefix => Relocate_Node (I)));
16289 -- The original was a subtype mark that does not freeze. This
16290 -- means that the rewritten version must not freeze either.
16292 Set_Must_Not_Freeze (I);
16293 Set_Must_Not_Freeze (Prefix (I));
16295 -- Is order critical??? if so, document why, if not
16296 -- use Analyze_And_Resolve
16298 Analyze_And_Resolve (I);
16302 -- If expander is inactive, type is legal, nothing else to construct
16309 if not Is_Discrete_Type (T) then
16310 Error_Msg_N ("discrete type required for range", I);
16311 Set_Etype (I, Any_Type);
16314 elsif T = Any_Type then
16315 Set_Etype (I, Any_Type);
16319 -- We will now create the appropriate Itype to describe the range, but
16320 -- first a check. If we originally had a subtype, then we just label
16321 -- the range with this subtype. Not only is there no need to construct
16322 -- a new subtype, but it is wrong to do so for two reasons:
16324 -- 1. A legality concern, if we have a subtype, it must not freeze,
16325 -- and the Itype would cause freezing incorrectly
16327 -- 2. An efficiency concern, if we created an Itype, it would not be
16328 -- recognized as the same type for the purposes of eliminating
16329 -- checks in some circumstances.
16331 -- We signal this case by setting the subtype entity in Def_Id
16333 if No (Def_Id) then
16335 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
16336 Set_Etype (Def_Id, Base_Type (T));
16338 if Is_Signed_Integer_Type (T) then
16339 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
16341 elsif Is_Modular_Integer_Type (T) then
16342 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
16345 Set_Ekind (Def_Id, E_Enumeration_Subtype);
16346 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
16347 Set_First_Literal (Def_Id, First_Literal (T));
16350 Set_Size_Info (Def_Id, (T));
16351 Set_RM_Size (Def_Id, RM_Size (T));
16352 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
16354 Set_Scalar_Range (Def_Id, R);
16355 Conditional_Delay (Def_Id, T);
16357 -- In the subtype indication case, if the immediate parent of the
16358 -- new subtype is non-static, then the subtype we create is non-
16359 -- static, even if its bounds are static.
16361 if Nkind (I) = N_Subtype_Indication
16362 and then not Is_Static_Subtype (Entity (Subtype_Mark (I)))
16364 Set_Is_Non_Static_Subtype (Def_Id);
16368 -- Final step is to label the index with this constructed type
16370 Set_Etype (I, Def_Id);
16373 ------------------------------
16374 -- Modular_Type_Declaration --
16375 ------------------------------
16377 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16378 Mod_Expr : constant Node_Id := Expression (Def);
16381 procedure Set_Modular_Size (Bits : Int);
16382 -- Sets RM_Size to Bits, and Esize to normal word size above this
16384 ----------------------
16385 -- Set_Modular_Size --
16386 ----------------------
16388 procedure Set_Modular_Size (Bits : Int) is
16390 Set_RM_Size (T, UI_From_Int (Bits));
16395 elsif Bits <= 16 then
16396 Init_Esize (T, 16);
16398 elsif Bits <= 32 then
16399 Init_Esize (T, 32);
16402 Init_Esize (T, System_Max_Binary_Modulus_Power);
16405 if not Non_Binary_Modulus (T)
16406 and then Esize (T) = RM_Size (T)
16408 Set_Is_Known_Valid (T);
16410 end Set_Modular_Size;
16412 -- Start of processing for Modular_Type_Declaration
16415 Analyze_And_Resolve (Mod_Expr, Any_Integer);
16417 Set_Ekind (T, E_Modular_Integer_Type);
16418 Init_Alignment (T);
16419 Set_Is_Constrained (T);
16421 if not Is_OK_Static_Expression (Mod_Expr) then
16422 Flag_Non_Static_Expr
16423 ("non-static expression used for modular type bound!", Mod_Expr);
16424 M_Val := 2 ** System_Max_Binary_Modulus_Power;
16426 M_Val := Expr_Value (Mod_Expr);
16430 Error_Msg_N ("modulus value must be positive", Mod_Expr);
16431 M_Val := 2 ** System_Max_Binary_Modulus_Power;
16434 Set_Modulus (T, M_Val);
16436 -- Create bounds for the modular type based on the modulus given in
16437 -- the type declaration and then analyze and resolve those bounds.
16439 Set_Scalar_Range (T,
16440 Make_Range (Sloc (Mod_Expr),
16441 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
16442 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
16444 -- Properly analyze the literals for the range. We do this manually
16445 -- because we can't go calling Resolve, since we are resolving these
16446 -- bounds with the type, and this type is certainly not complete yet!
16448 Set_Etype (Low_Bound (Scalar_Range (T)), T);
16449 Set_Etype (High_Bound (Scalar_Range (T)), T);
16450 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
16451 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
16453 -- Loop through powers of two to find number of bits required
16455 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
16459 if M_Val = 2 ** Bits then
16460 Set_Modular_Size (Bits);
16465 elsif M_Val < 2 ** Bits then
16466 Check_Formal_Restriction ("modulus should be a power of 2", T);
16467 Set_Non_Binary_Modulus (T);
16469 if Bits > System_Max_Nonbinary_Modulus_Power then
16470 Error_Msg_Uint_1 :=
16471 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
16473 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
16474 Set_Modular_Size (System_Max_Binary_Modulus_Power);
16478 -- In the non-binary case, set size as per RM 13.3(55)
16480 Set_Modular_Size (Bits);
16487 -- If we fall through, then the size exceed System.Max_Binary_Modulus
16488 -- so we just signal an error and set the maximum size.
16490 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
16491 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
16493 Set_Modular_Size (System_Max_Binary_Modulus_Power);
16494 Init_Alignment (T);
16496 end Modular_Type_Declaration;
16498 --------------------------
16499 -- New_Concatenation_Op --
16500 --------------------------
16502 procedure New_Concatenation_Op (Typ : Entity_Id) is
16503 Loc : constant Source_Ptr := Sloc (Typ);
16506 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
16507 -- Create abbreviated declaration for the formal of a predefined
16508 -- Operator 'Op' of type 'Typ'
16510 --------------------
16511 -- Make_Op_Formal --
16512 --------------------
16514 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
16515 Formal : Entity_Id;
16517 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
16518 Set_Etype (Formal, Typ);
16519 Set_Mechanism (Formal, Default_Mechanism);
16521 end Make_Op_Formal;
16523 -- Start of processing for New_Concatenation_Op
16526 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
16528 Set_Ekind (Op, E_Operator);
16529 Set_Scope (Op, Current_Scope);
16530 Set_Etype (Op, Typ);
16531 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
16532 Set_Is_Immediately_Visible (Op);
16533 Set_Is_Intrinsic_Subprogram (Op);
16534 Set_Has_Completion (Op);
16535 Append_Entity (Op, Current_Scope);
16537 Set_Name_Entity_Id (Name_Op_Concat, Op);
16539 Append_Entity (Make_Op_Formal (Typ, Op), Op);
16540 Append_Entity (Make_Op_Formal (Typ, Op), Op);
16541 end New_Concatenation_Op;
16543 -------------------------
16544 -- OK_For_Limited_Init --
16545 -------------------------
16547 -- ???Check all calls of this, and compare the conditions under which it's
16550 function OK_For_Limited_Init
16552 Exp : Node_Id) return Boolean
16555 return Is_CPP_Constructor_Call (Exp)
16556 or else (Ada_Version >= Ada_2005
16557 and then not Debug_Flag_Dot_L
16558 and then OK_For_Limited_Init_In_05 (Typ, Exp));
16559 end OK_For_Limited_Init;
16561 -------------------------------
16562 -- OK_For_Limited_Init_In_05 --
16563 -------------------------------
16565 function OK_For_Limited_Init_In_05
16567 Exp : Node_Id) return Boolean
16570 -- An object of a limited interface type can be initialized with any
16571 -- expression of a nonlimited descendant type.
16573 if Is_Class_Wide_Type (Typ)
16574 and then Is_Limited_Interface (Typ)
16575 and then not Is_Limited_Type (Etype (Exp))
16580 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
16581 -- case of limited aggregates (including extension aggregates), and
16582 -- function calls. The function call may have been given in prefixed
16583 -- notation, in which case the original node is an indexed component.
16584 -- If the function is parameterless, the original node was an explicit
16587 case Nkind (Original_Node (Exp)) is
16588 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
16591 when N_Qualified_Expression =>
16593 OK_For_Limited_Init_In_05
16594 (Typ, Expression (Original_Node (Exp)));
16596 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
16597 -- with a function call, the expander has rewritten the call into an
16598 -- N_Type_Conversion node to force displacement of the pointer to
16599 -- reference the component containing the secondary dispatch table.
16600 -- Otherwise a type conversion is not a legal context.
16601 -- A return statement for a build-in-place function returning a
16602 -- synchronized type also introduces an unchecked conversion.
16604 when N_Type_Conversion |
16605 N_Unchecked_Type_Conversion =>
16606 return not Comes_From_Source (Exp)
16608 OK_For_Limited_Init_In_05
16609 (Typ, Expression (Original_Node (Exp)));
16611 when N_Indexed_Component |
16612 N_Selected_Component |
16613 N_Explicit_Dereference =>
16614 return Nkind (Exp) = N_Function_Call;
16616 -- A use of 'Input is a function call, hence allowed. Normally the
16617 -- attribute will be changed to a call, but the attribute by itself
16618 -- can occur with -gnatc.
16620 when N_Attribute_Reference =>
16621 return Attribute_Name (Original_Node (Exp)) = Name_Input;
16626 end OK_For_Limited_Init_In_05;
16628 -------------------------------------------
16629 -- Ordinary_Fixed_Point_Type_Declaration --
16630 -------------------------------------------
16632 procedure Ordinary_Fixed_Point_Type_Declaration
16636 Loc : constant Source_Ptr := Sloc (Def);
16637 Delta_Expr : constant Node_Id := Delta_Expression (Def);
16638 RRS : constant Node_Id := Real_Range_Specification (Def);
16639 Implicit_Base : Entity_Id;
16646 Check_Restriction (No_Fixed_Point, Def);
16648 -- Create implicit base type
16651 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
16652 Set_Etype (Implicit_Base, Implicit_Base);
16654 -- Analyze and process delta expression
16656 Analyze_And_Resolve (Delta_Expr, Any_Real);
16658 Check_Delta_Expression (Delta_Expr);
16659 Delta_Val := Expr_Value_R (Delta_Expr);
16661 Set_Delta_Value (Implicit_Base, Delta_Val);
16663 -- Compute default small from given delta, which is the largest power
16664 -- of two that does not exceed the given delta value.
16674 if Delta_Val < Ureal_1 then
16675 while Delta_Val < Tmp loop
16676 Tmp := Tmp / Ureal_2;
16677 Scale := Scale + 1;
16682 Tmp := Tmp * Ureal_2;
16683 exit when Tmp > Delta_Val;
16684 Scale := Scale - 1;
16688 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
16691 Set_Small_Value (Implicit_Base, Small_Val);
16693 -- If no range was given, set a dummy range
16695 if RRS <= Empty_Or_Error then
16696 Low_Val := -Small_Val;
16697 High_Val := Small_Val;
16699 -- Otherwise analyze and process given range
16703 Low : constant Node_Id := Low_Bound (RRS);
16704 High : constant Node_Id := High_Bound (RRS);
16707 Analyze_And_Resolve (Low, Any_Real);
16708 Analyze_And_Resolve (High, Any_Real);
16709 Check_Real_Bound (Low);
16710 Check_Real_Bound (High);
16712 -- Obtain and set the range
16714 Low_Val := Expr_Value_R (Low);
16715 High_Val := Expr_Value_R (High);
16717 if Low_Val > High_Val then
16718 Error_Msg_NE ("?fixed point type& has null range", Def, T);
16723 -- The range for both the implicit base and the declared first subtype
16724 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
16725 -- set a temporary range in place. Note that the bounds of the base
16726 -- type will be widened to be symmetrical and to fill the available
16727 -- bits when the type is frozen.
16729 -- We could do this with all discrete types, and probably should, but
16730 -- we absolutely have to do it for fixed-point, since the end-points
16731 -- of the range and the size are determined by the small value, which
16732 -- could be reset before the freeze point.
16734 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
16735 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
16737 -- Complete definition of first subtype
16739 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
16740 Set_Etype (T, Implicit_Base);
16741 Init_Size_Align (T);
16742 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
16743 Set_Small_Value (T, Small_Val);
16744 Set_Delta_Value (T, Delta_Val);
16745 Set_Is_Constrained (T);
16747 end Ordinary_Fixed_Point_Type_Declaration;
16749 ----------------------------------------
16750 -- Prepare_Private_Subtype_Completion --
16751 ----------------------------------------
16753 procedure Prepare_Private_Subtype_Completion
16755 Related_Nod : Node_Id)
16757 Id_B : constant Entity_Id := Base_Type (Id);
16758 Full_B : constant Entity_Id := Full_View (Id_B);
16762 if Present (Full_B) then
16764 -- The Base_Type is already completed, we can complete the subtype
16765 -- now. We have to create a new entity with the same name, Thus we
16766 -- can't use Create_Itype.
16768 -- This is messy, should be fixed ???
16770 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
16771 Set_Is_Itype (Full);
16772 Set_Associated_Node_For_Itype (Full, Related_Nod);
16773 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
16776 -- The parent subtype may be private, but the base might not, in some
16777 -- nested instances. In that case, the subtype does not need to be
16778 -- exchanged. It would still be nice to make private subtypes and their
16779 -- bases consistent at all times ???
16781 if Is_Private_Type (Id_B) then
16782 Append_Elmt (Id, Private_Dependents (Id_B));
16785 end Prepare_Private_Subtype_Completion;
16787 ---------------------------
16788 -- Process_Discriminants --
16789 ---------------------------
16791 procedure Process_Discriminants
16793 Prev : Entity_Id := Empty)
16795 Elist : constant Elist_Id := New_Elmt_List;
16798 Discr_Number : Uint;
16799 Discr_Type : Entity_Id;
16800 Default_Present : Boolean := False;
16801 Default_Not_Present : Boolean := False;
16804 -- A composite type other than an array type can have discriminants.
16805 -- On entry, the current scope is the composite type.
16807 -- The discriminants are initially entered into the scope of the type
16808 -- via Enter_Name with the default Ekind of E_Void to prevent premature
16809 -- use, as explained at the end of this procedure.
16811 Discr := First (Discriminant_Specifications (N));
16812 while Present (Discr) loop
16813 Enter_Name (Defining_Identifier (Discr));
16815 -- For navigation purposes we add a reference to the discriminant
16816 -- in the entity for the type. If the current declaration is a
16817 -- completion, place references on the partial view. Otherwise the
16818 -- type is the current scope.
16820 if Present (Prev) then
16822 -- The references go on the partial view, if present. If the
16823 -- partial view has discriminants, the references have been
16824 -- generated already.
16826 if not Has_Discriminants (Prev) then
16827 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
16831 (Current_Scope, Defining_Identifier (Discr), 'd');
16834 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
16835 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
16837 -- Ada 2005 (AI-254)
16839 if Present (Access_To_Subprogram_Definition
16840 (Discriminant_Type (Discr)))
16841 and then Protected_Present (Access_To_Subprogram_Definition
16842 (Discriminant_Type (Discr)))
16845 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
16849 Find_Type (Discriminant_Type (Discr));
16850 Discr_Type := Etype (Discriminant_Type (Discr));
16852 if Error_Posted (Discriminant_Type (Discr)) then
16853 Discr_Type := Any_Type;
16857 if Is_Access_Type (Discr_Type) then
16859 -- Ada 2005 (AI-230): Access discriminant allowed in non-limited
16862 if Ada_Version < Ada_2005 then
16863 Check_Access_Discriminant_Requires_Limited
16864 (Discr, Discriminant_Type (Discr));
16867 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
16869 ("(Ada 83) access discriminant not allowed", Discr);
16872 elsif not Is_Discrete_Type (Discr_Type) then
16873 Error_Msg_N ("discriminants must have a discrete or access type",
16874 Discriminant_Type (Discr));
16877 Set_Etype (Defining_Identifier (Discr), Discr_Type);
16879 -- If a discriminant specification includes the assignment compound
16880 -- delimiter followed by an expression, the expression is the default
16881 -- expression of the discriminant; the default expression must be of
16882 -- the type of the discriminant. (RM 3.7.1) Since this expression is
16883 -- a default expression, we do the special preanalysis, since this
16884 -- expression does not freeze (see "Handling of Default and Per-
16885 -- Object Expressions" in spec of package Sem).
16887 if Present (Expression (Discr)) then
16888 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
16890 if Nkind (N) = N_Formal_Type_Declaration then
16892 ("discriminant defaults not allowed for formal type",
16893 Expression (Discr));
16895 -- Flag an error for a tagged type with defaulted discriminants,
16896 -- excluding limited tagged types when compiling for Ada 2012
16897 -- (see AI05-0214).
16899 elsif Is_Tagged_Type (Current_Scope)
16900 and then (not Is_Limited_Type (Current_Scope)
16901 or else Ada_Version < Ada_2012)
16902 and then Comes_From_Source (N)
16904 -- Note: see similar test in Check_Or_Process_Discriminants, to
16905 -- handle the (illegal) case of the completion of an untagged
16906 -- view with discriminants with defaults by a tagged full view.
16907 -- We skip the check if Discr does not come from source, to
16908 -- account for the case of an untagged derived type providing
16909 -- defaults for a renamed discriminant from a private untagged
16910 -- ancestor with a tagged full view (ACATS B460006).
16912 if Ada_Version >= Ada_2012 then
16914 ("discriminants of nonlimited tagged type cannot have"
16916 Expression (Discr));
16919 ("discriminants of tagged type cannot have defaults",
16920 Expression (Discr));
16924 Default_Present := True;
16925 Append_Elmt (Expression (Discr), Elist);
16927 -- Tag the defining identifiers for the discriminants with
16928 -- their corresponding default expressions from the tree.
16930 Set_Discriminant_Default_Value
16931 (Defining_Identifier (Discr), Expression (Discr));
16935 Default_Not_Present := True;
16938 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
16939 -- Discr_Type but with the null-exclusion attribute
16941 if Ada_Version >= Ada_2005 then
16943 -- Ada 2005 (AI-231): Static checks
16945 if Can_Never_Be_Null (Discr_Type) then
16946 Null_Exclusion_Static_Checks (Discr);
16948 elsif Is_Access_Type (Discr_Type)
16949 and then Null_Exclusion_Present (Discr)
16951 -- No need to check itypes because in their case this check
16952 -- was done at their point of creation
16954 and then not Is_Itype (Discr_Type)
16956 if Can_Never_Be_Null (Discr_Type) then
16958 ("`NOT NULL` not allowed (& already excludes null)",
16963 Set_Etype (Defining_Identifier (Discr),
16964 Create_Null_Excluding_Itype
16966 Related_Nod => Discr));
16968 -- Check for improper null exclusion if the type is otherwise
16969 -- legal for a discriminant.
16971 elsif Null_Exclusion_Present (Discr)
16972 and then Is_Discrete_Type (Discr_Type)
16975 ("null exclusion can only apply to an access type", Discr);
16978 -- Ada 2005 (AI-402): access discriminants of nonlimited types
16979 -- can't have defaults. Synchronized types, or types that are
16980 -- explicitly limited are fine, but special tests apply to derived
16981 -- types in generics: in a generic body we have to assume the
16982 -- worst, and therefore defaults are not allowed if the parent is
16983 -- a generic formal private type (see ACATS B370001).
16985 if Is_Access_Type (Discr_Type) then
16986 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
16987 or else not Default_Present
16988 or else Is_Limited_Record (Current_Scope)
16989 or else Is_Concurrent_Type (Current_Scope)
16990 or else Is_Concurrent_Record_Type (Current_Scope)
16991 or else Ekind (Current_Scope) = E_Limited_Private_Type
16993 if not Is_Derived_Type (Current_Scope)
16994 or else not Is_Generic_Type (Etype (Current_Scope))
16995 or else not In_Package_Body (Scope (Etype (Current_Scope)))
16996 or else Limited_Present
16997 (Type_Definition (Parent (Current_Scope)))
17002 Error_Msg_N ("access discriminants of nonlimited types",
17003 Expression (Discr));
17004 Error_Msg_N ("\cannot have defaults", Expression (Discr));
17007 elsif Present (Expression (Discr)) then
17009 ("(Ada 2005) access discriminants of nonlimited types",
17010 Expression (Discr));
17011 Error_Msg_N ("\cannot have defaults", Expression (Discr));
17019 -- An element list consisting of the default expressions of the
17020 -- discriminants is constructed in the above loop and used to set
17021 -- the Discriminant_Constraint attribute for the type. If an object
17022 -- is declared of this (record or task) type without any explicit
17023 -- discriminant constraint given, this element list will form the
17024 -- actual parameters for the corresponding initialization procedure
17027 Set_Discriminant_Constraint (Current_Scope, Elist);
17028 Set_Stored_Constraint (Current_Scope, No_Elist);
17030 -- Default expressions must be provided either for all or for none
17031 -- of the discriminants of a discriminant part. (RM 3.7.1)
17033 if Default_Present and then Default_Not_Present then
17035 ("incomplete specification of defaults for discriminants", N);
17038 -- The use of the name of a discriminant is not allowed in default
17039 -- expressions of a discriminant part if the specification of the
17040 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
17042 -- To detect this, the discriminant names are entered initially with an
17043 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
17044 -- attempt to use a void entity (for example in an expression that is
17045 -- type-checked) produces the error message: premature usage. Now after
17046 -- completing the semantic analysis of the discriminant part, we can set
17047 -- the Ekind of all the discriminants appropriately.
17049 Discr := First (Discriminant_Specifications (N));
17050 Discr_Number := Uint_1;
17051 while Present (Discr) loop
17052 Id := Defining_Identifier (Discr);
17053 Set_Ekind (Id, E_Discriminant);
17054 Init_Component_Location (Id);
17056 Set_Discriminant_Number (Id, Discr_Number);
17058 -- Make sure this is always set, even in illegal programs
17060 Set_Corresponding_Discriminant (Id, Empty);
17062 -- Initialize the Original_Record_Component to the entity itself.
17063 -- Inherit_Components will propagate the right value to
17064 -- discriminants in derived record types.
17066 Set_Original_Record_Component (Id, Id);
17068 -- Create the discriminal for the discriminant
17070 Build_Discriminal (Id);
17073 Discr_Number := Discr_Number + 1;
17076 Set_Has_Discriminants (Current_Scope);
17077 end Process_Discriminants;
17079 -----------------------
17080 -- Process_Full_View --
17081 -----------------------
17083 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
17084 Priv_Parent : Entity_Id;
17085 Full_Parent : Entity_Id;
17086 Full_Indic : Node_Id;
17088 procedure Collect_Implemented_Interfaces
17090 Ifaces : Elist_Id);
17091 -- Ada 2005: Gather all the interfaces that Typ directly or
17092 -- inherently implements. Duplicate entries are not added to
17093 -- the list Ifaces.
17095 ------------------------------------
17096 -- Collect_Implemented_Interfaces --
17097 ------------------------------------
17099 procedure Collect_Implemented_Interfaces
17104 Iface_Elmt : Elmt_Id;
17107 -- Abstract interfaces are only associated with tagged record types
17109 if not Is_Tagged_Type (Typ)
17110 or else not Is_Record_Type (Typ)
17115 -- Recursively climb to the ancestors
17117 if Etype (Typ) /= Typ
17119 -- Protect the frontend against wrong cyclic declarations like:
17121 -- type B is new A with private;
17122 -- type C is new A with private;
17124 -- type B is new C with null record;
17125 -- type C is new B with null record;
17127 and then Etype (Typ) /= Priv_T
17128 and then Etype (Typ) /= Full_T
17130 -- Keep separate the management of private type declarations
17132 if Ekind (Typ) = E_Record_Type_With_Private then
17134 -- Handle the following erroneous case:
17135 -- type Private_Type is tagged private;
17137 -- type Private_Type is new Type_Implementing_Iface;
17139 if Present (Full_View (Typ))
17140 and then Etype (Typ) /= Full_View (Typ)
17142 if Is_Interface (Etype (Typ)) then
17143 Append_Unique_Elmt (Etype (Typ), Ifaces);
17146 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
17149 -- Non-private types
17152 if Is_Interface (Etype (Typ)) then
17153 Append_Unique_Elmt (Etype (Typ), Ifaces);
17156 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
17160 -- Handle entities in the list of abstract interfaces
17162 if Present (Interfaces (Typ)) then
17163 Iface_Elmt := First_Elmt (Interfaces (Typ));
17164 while Present (Iface_Elmt) loop
17165 Iface := Node (Iface_Elmt);
17167 pragma Assert (Is_Interface (Iface));
17169 if not Contain_Interface (Iface, Ifaces) then
17170 Append_Elmt (Iface, Ifaces);
17171 Collect_Implemented_Interfaces (Iface, Ifaces);
17174 Next_Elmt (Iface_Elmt);
17177 end Collect_Implemented_Interfaces;
17179 -- Start of processing for Process_Full_View
17182 -- First some sanity checks that must be done after semantic
17183 -- decoration of the full view and thus cannot be placed with other
17184 -- similar checks in Find_Type_Name
17186 if not Is_Limited_Type (Priv_T)
17187 and then (Is_Limited_Type (Full_T)
17188 or else Is_Limited_Composite (Full_T))
17191 ("completion of nonlimited type cannot be limited", Full_T);
17192 Explain_Limited_Type (Full_T, Full_T);
17194 elsif Is_Abstract_Type (Full_T)
17195 and then not Is_Abstract_Type (Priv_T)
17198 ("completion of nonabstract type cannot be abstract", Full_T);
17200 elsif Is_Tagged_Type (Priv_T)
17201 and then Is_Limited_Type (Priv_T)
17202 and then not Is_Limited_Type (Full_T)
17204 -- If pragma CPP_Class was applied to the private declaration
17205 -- propagate the limitedness to the full-view
17207 if Is_CPP_Class (Priv_T) then
17208 Set_Is_Limited_Record (Full_T);
17210 -- GNAT allow its own definition of Limited_Controlled to disobey
17211 -- this rule in order in ease the implementation. The next test is
17212 -- safe because Root_Controlled is defined in a private system child
17214 elsif Etype (Full_T) = Full_View (RTE (RE_Root_Controlled)) then
17215 Set_Is_Limited_Composite (Full_T);
17218 ("completion of limited tagged type must be limited", Full_T);
17221 elsif Is_Generic_Type (Priv_T) then
17222 Error_Msg_N ("generic type cannot have a completion", Full_T);
17225 -- Check that ancestor interfaces of private and full views are
17226 -- consistent. We omit this check for synchronized types because
17227 -- they are performed on the corresponding record type when frozen.
17229 if Ada_Version >= Ada_2005
17230 and then Is_Tagged_Type (Priv_T)
17231 and then Is_Tagged_Type (Full_T)
17232 and then not Is_Concurrent_Type (Full_T)
17236 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
17237 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
17240 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
17241 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
17243 -- Ada 2005 (AI-251): The partial view shall be a descendant of
17244 -- an interface type if and only if the full type is descendant
17245 -- of the interface type (AARM 7.3 (7.3/2).
17247 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
17249 if Present (Iface) then
17251 ("interface & not implemented by full type " &
17252 "(RM-2005 7.3 (7.3/2))", Priv_T, Iface);
17255 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
17257 if Present (Iface) then
17259 ("interface & not implemented by partial view " &
17260 "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
17265 if Is_Tagged_Type (Priv_T)
17266 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17267 and then Is_Derived_Type (Full_T)
17269 Priv_Parent := Etype (Priv_T);
17271 -- The full view of a private extension may have been transformed
17272 -- into an unconstrained derived type declaration and a subtype
17273 -- declaration (see build_derived_record_type for details).
17275 if Nkind (N) = N_Subtype_Declaration then
17276 Full_Indic := Subtype_Indication (N);
17277 Full_Parent := Etype (Base_Type (Full_T));
17279 Full_Indic := Subtype_Indication (Type_Definition (N));
17280 Full_Parent := Etype (Full_T);
17283 -- Check that the parent type of the full type is a descendant of
17284 -- the ancestor subtype given in the private extension. If either
17285 -- entity has an Etype equal to Any_Type then we had some previous
17286 -- error situation [7.3(8)].
17288 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
17291 -- Ada 2005 (AI-251): Interfaces in the full-typ can be given in
17292 -- any order. Therefore we don't have to check that its parent must
17293 -- be a descendant of the parent of the private type declaration.
17295 elsif Is_Interface (Priv_Parent)
17296 and then Is_Interface (Full_Parent)
17300 -- Ada 2005 (AI-251): If the parent of the private type declaration
17301 -- is an interface there is no need to check that it is an ancestor
17302 -- of the associated full type declaration. The required tests for
17303 -- this case are performed by Build_Derived_Record_Type.
17305 elsif not Is_Interface (Base_Type (Priv_Parent))
17306 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
17309 ("parent of full type must descend from parent"
17310 & " of private extension", Full_Indic);
17312 -- First check a formal restriction, and then proceed with checking
17313 -- Ada rules. Since the formal restriction is not a serious error, we
17314 -- don't prevent further error detection for this check, hence the
17319 -- In formal mode, when completing a private extension the type
17320 -- named in the private part must be exactly the same as that
17321 -- named in the visible part.
17323 if Priv_Parent /= Full_Parent then
17324 Error_Msg_Name_1 := Chars (Priv_Parent);
17325 Check_Formal_Restriction ("% expected", Full_Indic);
17328 -- Check the rules of 7.3(10): if the private extension inherits
17329 -- known discriminants, then the full type must also inherit those
17330 -- discriminants from the same (ancestor) type, and the parent
17331 -- subtype of the full type must be constrained if and only if
17332 -- the ancestor subtype of the private extension is constrained.
17334 if No (Discriminant_Specifications (Parent (Priv_T)))
17335 and then not Has_Unknown_Discriminants (Priv_T)
17336 and then Has_Discriminants (Base_Type (Priv_Parent))
17339 Priv_Indic : constant Node_Id :=
17340 Subtype_Indication (Parent (Priv_T));
17342 Priv_Constr : constant Boolean :=
17343 Is_Constrained (Priv_Parent)
17345 Nkind (Priv_Indic) = N_Subtype_Indication
17347 Is_Constrained (Entity (Priv_Indic));
17349 Full_Constr : constant Boolean :=
17350 Is_Constrained (Full_Parent)
17352 Nkind (Full_Indic) = N_Subtype_Indication
17354 Is_Constrained (Entity (Full_Indic));
17356 Priv_Discr : Entity_Id;
17357 Full_Discr : Entity_Id;
17360 Priv_Discr := First_Discriminant (Priv_Parent);
17361 Full_Discr := First_Discriminant (Full_Parent);
17362 while Present (Priv_Discr) and then Present (Full_Discr) loop
17363 if Original_Record_Component (Priv_Discr) =
17364 Original_Record_Component (Full_Discr)
17366 Corresponding_Discriminant (Priv_Discr) =
17367 Corresponding_Discriminant (Full_Discr)
17374 Next_Discriminant (Priv_Discr);
17375 Next_Discriminant (Full_Discr);
17378 if Present (Priv_Discr) or else Present (Full_Discr) then
17380 ("full view must inherit discriminants of the parent"
17381 & " type used in the private extension", Full_Indic);
17383 elsif Priv_Constr and then not Full_Constr then
17385 ("parent subtype of full type must be constrained",
17388 elsif Full_Constr and then not Priv_Constr then
17390 ("parent subtype of full type must be unconstrained",
17395 -- Check the rules of 7.3(12): if a partial view has neither
17396 -- known or unknown discriminants, then the full type
17397 -- declaration shall define a definite subtype.
17399 elsif not Has_Unknown_Discriminants (Priv_T)
17400 and then not Has_Discriminants (Priv_T)
17401 and then not Is_Constrained (Full_T)
17404 ("full view must define a constrained type if partial view"
17405 & " has no discriminants", Full_T);
17408 -- ??????? Do we implement the following properly ?????
17409 -- If the ancestor subtype of a private extension has constrained
17410 -- discriminants, then the parent subtype of the full view shall
17411 -- impose a statically matching constraint on those discriminants
17416 -- For untagged types, verify that a type without discriminants
17417 -- is not completed with an unconstrained type.
17419 if not Is_Indefinite_Subtype (Priv_T)
17420 and then Is_Indefinite_Subtype (Full_T)
17422 Error_Msg_N ("full view of type must be definite subtype", Full_T);
17426 -- AI-419: verify that the use of "limited" is consistent
17429 Orig_Decl : constant Node_Id := Original_Node (N);
17432 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17433 and then not Limited_Present (Parent (Priv_T))
17434 and then not Synchronized_Present (Parent (Priv_T))
17435 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
17437 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
17438 and then Limited_Present (Type_Definition (Orig_Decl))
17441 ("full view of non-limited extension cannot be limited", N);
17445 -- Ada 2005 (AI-443): A synchronized private extension must be
17446 -- completed by a task or protected type.
17448 if Ada_Version >= Ada_2005
17449 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
17450 and then Synchronized_Present (Parent (Priv_T))
17451 and then not Is_Concurrent_Type (Full_T)
17453 Error_Msg_N ("full view of synchronized extension must " &
17454 "be synchronized type", N);
17457 -- Ada 2005 AI-363: if the full view has discriminants with
17458 -- defaults, it is illegal to declare constrained access subtypes
17459 -- whose designated type is the current type. This allows objects
17460 -- of the type that are declared in the heap to be unconstrained.
17462 if not Has_Unknown_Discriminants (Priv_T)
17463 and then not Has_Discriminants (Priv_T)
17464 and then Has_Discriminants (Full_T)
17466 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
17468 Set_Has_Constrained_Partial_View (Full_T);
17469 Set_Has_Constrained_Partial_View (Priv_T);
17472 -- Create a full declaration for all its subtypes recorded in
17473 -- Private_Dependents and swap them similarly to the base type. These
17474 -- are subtypes that have been define before the full declaration of
17475 -- the private type. We also swap the entry in Private_Dependents list
17476 -- so we can properly restore the private view on exit from the scope.
17479 Priv_Elmt : Elmt_Id;
17484 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
17485 while Present (Priv_Elmt) loop
17486 Priv := Node (Priv_Elmt);
17488 if Ekind_In (Priv, E_Private_Subtype,
17489 E_Limited_Private_Subtype,
17490 E_Record_Subtype_With_Private)
17492 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
17493 Set_Is_Itype (Full);
17494 Set_Parent (Full, Parent (Priv));
17495 Set_Associated_Node_For_Itype (Full, N);
17497 -- Now we need to complete the private subtype, but since the
17498 -- base type has already been swapped, we must also swap the
17499 -- subtypes (and thus, reverse the arguments in the call to
17500 -- Complete_Private_Subtype).
17502 Copy_And_Swap (Priv, Full);
17503 Complete_Private_Subtype (Full, Priv, Full_T, N);
17504 Replace_Elmt (Priv_Elmt, Full);
17507 Next_Elmt (Priv_Elmt);
17511 -- If the private view was tagged, copy the new primitive operations
17512 -- from the private view to the full view.
17514 if Is_Tagged_Type (Full_T) then
17516 Disp_Typ : Entity_Id;
17517 Full_List : Elist_Id;
17519 Prim_Elmt : Elmt_Id;
17520 Priv_List : Elist_Id;
17524 L : Elist_Id) return Boolean;
17525 -- Determine whether list L contains element E
17533 L : Elist_Id) return Boolean
17535 List_Elmt : Elmt_Id;
17538 List_Elmt := First_Elmt (L);
17539 while Present (List_Elmt) loop
17540 if Node (List_Elmt) = E then
17544 Next_Elmt (List_Elmt);
17550 -- Start of processing
17553 if Is_Tagged_Type (Priv_T) then
17554 Priv_List := Primitive_Operations (Priv_T);
17555 Prim_Elmt := First_Elmt (Priv_List);
17557 -- In the case of a concurrent type completing a private tagged
17558 -- type, primitives may have been declared in between the two
17559 -- views. These subprograms need to be wrapped the same way
17560 -- entries and protected procedures are handled because they
17561 -- cannot be directly shared by the two views.
17563 if Is_Concurrent_Type (Full_T) then
17565 Conc_Typ : constant Entity_Id :=
17566 Corresponding_Record_Type (Full_T);
17567 Curr_Nod : Node_Id := Parent (Conc_Typ);
17568 Wrap_Spec : Node_Id;
17571 while Present (Prim_Elmt) loop
17572 Prim := Node (Prim_Elmt);
17574 if Comes_From_Source (Prim)
17575 and then not Is_Abstract_Subprogram (Prim)
17578 Make_Subprogram_Declaration (Sloc (Prim),
17582 Obj_Typ => Conc_Typ,
17584 Parameter_Specifications (
17587 Insert_After (Curr_Nod, Wrap_Spec);
17588 Curr_Nod := Wrap_Spec;
17590 Analyze (Wrap_Spec);
17593 Next_Elmt (Prim_Elmt);
17599 -- For non-concurrent types, transfer explicit primitives, but
17600 -- omit those inherited from the parent of the private view
17601 -- since they will be re-inherited later on.
17604 Full_List := Primitive_Operations (Full_T);
17606 while Present (Prim_Elmt) loop
17607 Prim := Node (Prim_Elmt);
17609 if Comes_From_Source (Prim)
17610 and then not Contains (Prim, Full_List)
17612 Append_Elmt (Prim, Full_List);
17615 Next_Elmt (Prim_Elmt);
17619 -- Untagged private view
17622 Full_List := Primitive_Operations (Full_T);
17624 -- In this case the partial view is untagged, so here we locate
17625 -- all of the earlier primitives that need to be treated as
17626 -- dispatching (those that appear between the two views). Note
17627 -- that these additional operations must all be new operations
17628 -- (any earlier operations that override inherited operations
17629 -- of the full view will already have been inserted in the
17630 -- primitives list, marked by Check_Operation_From_Private_View
17631 -- as dispatching. Note that implicit "/=" operators are
17632 -- excluded from being added to the primitives list since they
17633 -- shouldn't be treated as dispatching (tagged "/=" is handled
17636 Prim := Next_Entity (Full_T);
17637 while Present (Prim) and then Prim /= Priv_T loop
17638 if Ekind_In (Prim, E_Procedure, E_Function) then
17639 Disp_Typ := Find_Dispatching_Type (Prim);
17641 if Disp_Typ = Full_T
17642 and then (Chars (Prim) /= Name_Op_Ne
17643 or else Comes_From_Source (Prim))
17645 Check_Controlling_Formals (Full_T, Prim);
17647 if not Is_Dispatching_Operation (Prim) then
17648 Append_Elmt (Prim, Full_List);
17649 Set_Is_Dispatching_Operation (Prim, True);
17650 Set_DT_Position (Prim, No_Uint);
17653 elsif Is_Dispatching_Operation (Prim)
17654 and then Disp_Typ /= Full_T
17657 -- Verify that it is not otherwise controlled by a
17658 -- formal or a return value of type T.
17660 Check_Controlling_Formals (Disp_Typ, Prim);
17664 Next_Entity (Prim);
17668 -- For the tagged case, the two views can share the same primitive
17669 -- operations list and the same class-wide type. Update attributes
17670 -- of the class-wide type which depend on the full declaration.
17672 if Is_Tagged_Type (Priv_T) then
17673 Set_Direct_Primitive_Operations (Priv_T, Full_List);
17674 Set_Class_Wide_Type
17675 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
17677 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
17682 -- Ada 2005 AI 161: Check preelaboratable initialization consistency
17684 if Known_To_Have_Preelab_Init (Priv_T) then
17686 -- Case where there is a pragma Preelaborable_Initialization. We
17687 -- always allow this in predefined units, which is a bit of a kludge,
17688 -- but it means we don't have to struggle to meet the requirements in
17689 -- the RM for having Preelaborable Initialization. Otherwise we
17690 -- require that the type meets the RM rules. But we can't check that
17691 -- yet, because of the rule about overriding Initialize, so we simply
17692 -- set a flag that will be checked at freeze time.
17694 if not In_Predefined_Unit (Full_T) then
17695 Set_Must_Have_Preelab_Init (Full_T);
17699 -- If pragma CPP_Class was applied to the private type declaration,
17700 -- propagate it now to the full type declaration.
17702 if Is_CPP_Class (Priv_T) then
17703 Set_Is_CPP_Class (Full_T);
17704 Set_Convention (Full_T, Convention_CPP);
17707 -- If the private view has user specified stream attributes, then so has
17710 -- Why the test, how could these flags be already set in Full_T ???
17712 if Has_Specified_Stream_Read (Priv_T) then
17713 Set_Has_Specified_Stream_Read (Full_T);
17716 if Has_Specified_Stream_Write (Priv_T) then
17717 Set_Has_Specified_Stream_Write (Full_T);
17720 if Has_Specified_Stream_Input (Priv_T) then
17721 Set_Has_Specified_Stream_Input (Full_T);
17724 if Has_Specified_Stream_Output (Priv_T) then
17725 Set_Has_Specified_Stream_Output (Full_T);
17728 -- Propagate invariants to full type
17730 if Has_Invariants (Priv_T) then
17731 Set_Has_Invariants (Full_T);
17732 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
17735 if Has_Inheritable_Invariants (Priv_T) then
17736 Set_Has_Inheritable_Invariants (Full_T);
17739 -- Propagate predicates to full type
17741 if Has_Predicates (Priv_T) then
17742 Set_Predicate_Function (Priv_T, Predicate_Function (Full_T));
17743 Set_Has_Predicates (Priv_T);
17745 end Process_Full_View;
17747 -----------------------------------
17748 -- Process_Incomplete_Dependents --
17749 -----------------------------------
17751 procedure Process_Incomplete_Dependents
17753 Full_T : Entity_Id;
17756 Inc_Elmt : Elmt_Id;
17757 Priv_Dep : Entity_Id;
17758 New_Subt : Entity_Id;
17760 Disc_Constraint : Elist_Id;
17763 if No (Private_Dependents (Inc_T)) then
17767 -- Itypes that may be generated by the completion of an incomplete
17768 -- subtype are not used by the back-end and not attached to the tree.
17769 -- They are created only for constraint-checking purposes.
17771 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
17772 while Present (Inc_Elmt) loop
17773 Priv_Dep := Node (Inc_Elmt);
17775 if Ekind (Priv_Dep) = E_Subprogram_Type then
17777 -- An Access_To_Subprogram type may have a return type or a
17778 -- parameter type that is incomplete. Replace with the full view.
17780 if Etype (Priv_Dep) = Inc_T then
17781 Set_Etype (Priv_Dep, Full_T);
17785 Formal : Entity_Id;
17788 Formal := First_Formal (Priv_Dep);
17789 while Present (Formal) loop
17790 if Etype (Formal) = Inc_T then
17791 Set_Etype (Formal, Full_T);
17794 Next_Formal (Formal);
17798 elsif Is_Overloadable (Priv_Dep) then
17800 -- A protected operation is never dispatching: only its
17801 -- wrapper operation (which has convention Ada) is.
17803 if Is_Tagged_Type (Full_T)
17804 and then Convention (Priv_Dep) /= Convention_Protected
17807 -- Subprogram has an access parameter whose designated type
17808 -- was incomplete. Reexamine declaration now, because it may
17809 -- be a primitive operation of the full type.
17811 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
17812 Set_Is_Dispatching_Operation (Priv_Dep);
17813 Check_Controlling_Formals (Full_T, Priv_Dep);
17816 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
17818 -- Can happen during processing of a body before the completion
17819 -- of a TA type. Ignore, because spec is also on dependent list.
17823 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
17824 -- corresponding subtype of the full view.
17826 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
17827 Set_Subtype_Indication
17828 (Parent (Priv_Dep), New_Reference_To (Full_T, Sloc (Priv_Dep)));
17829 Set_Etype (Priv_Dep, Full_T);
17830 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
17831 Set_Analyzed (Parent (Priv_Dep), False);
17833 -- Reanalyze the declaration, suppressing the call to
17834 -- Enter_Name to avoid duplicate names.
17836 Analyze_Subtype_Declaration
17837 (N => Parent (Priv_Dep),
17840 -- Dependent is a subtype
17843 -- We build a new subtype indication using the full view of the
17844 -- incomplete parent. The discriminant constraints have been
17845 -- elaborated already at the point of the subtype declaration.
17847 New_Subt := Create_Itype (E_Void, N);
17849 if Has_Discriminants (Full_T) then
17850 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
17852 Disc_Constraint := No_Elist;
17855 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
17856 Set_Full_View (Priv_Dep, New_Subt);
17859 Next_Elmt (Inc_Elmt);
17861 end Process_Incomplete_Dependents;
17863 --------------------------------
17864 -- Process_Range_Expr_In_Decl --
17865 --------------------------------
17867 procedure Process_Range_Expr_In_Decl
17870 Check_List : List_Id := Empty_List;
17871 R_Check_Off : Boolean := False)
17874 R_Checks : Check_Result;
17875 Insert_Node : Node_Id;
17876 Def_Id : Entity_Id;
17879 Analyze_And_Resolve (R, Base_Type (T));
17881 if Nkind (R) = N_Range then
17882 if not Is_Static_Range (R) then
17883 Check_Formal_Restriction ("range should be static", R);
17886 Lo := Low_Bound (R);
17887 Hi := High_Bound (R);
17889 -- We need to ensure validity of the bounds here, because if we
17890 -- go ahead and do the expansion, then the expanded code will get
17891 -- analyzed with range checks suppressed and we miss the check.
17893 Validity_Check_Range (R);
17895 -- If there were errors in the declaration, try and patch up some
17896 -- common mistakes in the bounds. The cases handled are literals
17897 -- which are Integer where the expected type is Real and vice versa.
17898 -- These corrections allow the compilation process to proceed further
17899 -- along since some basic assumptions of the format of the bounds
17902 if Etype (R) = Any_Type then
17904 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
17906 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
17908 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
17910 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
17912 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
17914 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
17916 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
17918 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
17925 -- If the bounds of the range have been mistakenly given as string
17926 -- literals (perhaps in place of character literals), then an error
17927 -- has already been reported, but we rewrite the string literal as a
17928 -- bound of the range's type to avoid blowups in later processing
17929 -- that looks at static values.
17931 if Nkind (Lo) = N_String_Literal then
17933 Make_Attribute_Reference (Sloc (Lo),
17934 Attribute_Name => Name_First,
17935 Prefix => New_Reference_To (T, Sloc (Lo))));
17936 Analyze_And_Resolve (Lo);
17939 if Nkind (Hi) = N_String_Literal then
17941 Make_Attribute_Reference (Sloc (Hi),
17942 Attribute_Name => Name_First,
17943 Prefix => New_Reference_To (T, Sloc (Hi))));
17944 Analyze_And_Resolve (Hi);
17947 -- If bounds aren't scalar at this point then exit, avoiding
17948 -- problems with further processing of the range in this procedure.
17950 if not Is_Scalar_Type (Etype (Lo)) then
17954 -- Resolve (actually Sem_Eval) has checked that the bounds are in
17955 -- then range of the base type. Here we check whether the bounds
17956 -- are in the range of the subtype itself. Note that if the bounds
17957 -- represent the null range the Constraint_Error exception should
17960 -- ??? The following code should be cleaned up as follows
17962 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
17963 -- is done in the call to Range_Check (R, T); below
17965 -- 2. The use of R_Check_Off should be investigated and possibly
17966 -- removed, this would clean up things a bit.
17968 if Is_Null_Range (Lo, Hi) then
17972 -- Capture values of bounds and generate temporaries for them
17973 -- if needed, before applying checks, since checks may cause
17974 -- duplication of the expression without forcing evaluation.
17976 if Expander_Active then
17977 Force_Evaluation (Lo);
17978 Force_Evaluation (Hi);
17981 -- We use a flag here instead of suppressing checks on the
17982 -- type because the type we check against isn't necessarily
17983 -- the place where we put the check.
17985 if not R_Check_Off then
17986 R_Checks := Get_Range_Checks (R, T);
17988 -- Look up tree to find an appropriate insertion point. We
17989 -- can't just use insert_actions because later processing
17990 -- depends on the insertion node. Prior to Ada2012 the
17991 -- insertion point could only be a declaration or a loop, but
17992 -- quantified expressions can appear within any context in an
17993 -- expression, and the insertion point can be any statement,
17994 -- pragma, or declaration.
17996 Insert_Node := Parent (R);
17997 while Present (Insert_Node) loop
17999 Nkind (Insert_Node) in N_Declaration
18002 (Insert_Node, N_Component_Declaration,
18003 N_Loop_Parameter_Specification,
18004 N_Function_Specification,
18005 N_Procedure_Specification);
18007 exit when Nkind (Insert_Node) in N_Later_Decl_Item
18008 or else Nkind (Insert_Node) in
18009 N_Statement_Other_Than_Procedure_Call
18010 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
18013 Insert_Node := Parent (Insert_Node);
18016 -- Why would Type_Decl not be present??? Without this test,
18017 -- short regression tests fail.
18019 if Present (Insert_Node) then
18021 -- Case of loop statement. Verify that the range is part
18022 -- of the subtype indication of the iteration scheme.
18024 if Nkind (Insert_Node) = N_Loop_Statement then
18029 Indic := Parent (R);
18030 while Present (Indic)
18031 and then Nkind (Indic) /= N_Subtype_Indication
18033 Indic := Parent (Indic);
18036 if Present (Indic) then
18037 Def_Id := Etype (Subtype_Mark (Indic));
18039 Insert_Range_Checks
18043 Sloc (Insert_Node),
18045 Do_Before => True);
18049 -- Insertion before a declaration. If the declaration
18050 -- includes discriminants, the list of applicable checks
18051 -- is given by the caller.
18053 elsif Nkind (Insert_Node) in N_Declaration then
18054 Def_Id := Defining_Identifier (Insert_Node);
18056 if (Ekind (Def_Id) = E_Record_Type
18057 and then Depends_On_Discriminant (R))
18059 (Ekind (Def_Id) = E_Protected_Type
18060 and then Has_Discriminants (Def_Id))
18062 Append_Range_Checks
18064 Check_List, Def_Id, Sloc (Insert_Node), R);
18067 Insert_Range_Checks
18069 Insert_Node, Def_Id, Sloc (Insert_Node), R);
18073 -- Insertion before a statement. Range appears in the
18074 -- context of a quantified expression. Insertion will
18075 -- take place when expression is expanded.
18084 -- Case of other than an explicit N_Range node
18086 elsif Expander_Active then
18087 Get_Index_Bounds (R, Lo, Hi);
18088 Force_Evaluation (Lo);
18089 Force_Evaluation (Hi);
18091 end Process_Range_Expr_In_Decl;
18093 --------------------------------------
18094 -- Process_Real_Range_Specification --
18095 --------------------------------------
18097 procedure Process_Real_Range_Specification (Def : Node_Id) is
18098 Spec : constant Node_Id := Real_Range_Specification (Def);
18101 Err : Boolean := False;
18103 procedure Analyze_Bound (N : Node_Id);
18104 -- Analyze and check one bound
18106 -------------------
18107 -- Analyze_Bound --
18108 -------------------
18110 procedure Analyze_Bound (N : Node_Id) is
18112 Analyze_And_Resolve (N, Any_Real);
18114 if not Is_OK_Static_Expression (N) then
18115 Flag_Non_Static_Expr
18116 ("bound in real type definition is not static!", N);
18121 -- Start of processing for Process_Real_Range_Specification
18124 if Present (Spec) then
18125 Lo := Low_Bound (Spec);
18126 Hi := High_Bound (Spec);
18127 Analyze_Bound (Lo);
18128 Analyze_Bound (Hi);
18130 -- If error, clear away junk range specification
18133 Set_Real_Range_Specification (Def, Empty);
18136 end Process_Real_Range_Specification;
18138 ---------------------
18139 -- Process_Subtype --
18140 ---------------------
18142 function Process_Subtype
18144 Related_Nod : Node_Id;
18145 Related_Id : Entity_Id := Empty;
18146 Suffix : Character := ' ') return Entity_Id
18149 Def_Id : Entity_Id;
18150 Error_Node : Node_Id;
18151 Full_View_Id : Entity_Id;
18152 Subtype_Mark_Id : Entity_Id;
18154 May_Have_Null_Exclusion : Boolean;
18156 procedure Check_Incomplete (T : Entity_Id);
18157 -- Called to verify that an incomplete type is not used prematurely
18159 ----------------------
18160 -- Check_Incomplete --
18161 ----------------------
18163 procedure Check_Incomplete (T : Entity_Id) is
18165 -- Ada 2005 (AI-412): Incomplete subtypes are legal
18167 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
18169 not (Ada_Version >= Ada_2005
18171 (Nkind (Parent (T)) = N_Subtype_Declaration
18173 (Nkind (Parent (T)) = N_Subtype_Indication
18174 and then Nkind (Parent (Parent (T))) =
18175 N_Subtype_Declaration)))
18177 Error_Msg_N ("invalid use of type before its full declaration", T);
18179 end Check_Incomplete;
18181 -- Start of processing for Process_Subtype
18184 -- Case of no constraints present
18186 if Nkind (S) /= N_Subtype_Indication then
18188 Check_Incomplete (S);
18191 -- Ada 2005 (AI-231): Static check
18193 if Ada_Version >= Ada_2005
18194 and then Present (P)
18195 and then Null_Exclusion_Present (P)
18196 and then Nkind (P) /= N_Access_To_Object_Definition
18197 and then not Is_Access_Type (Entity (S))
18199 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
18202 -- The following is ugly, can't we have a range or even a flag???
18204 May_Have_Null_Exclusion :=
18205 Nkind_In (P, N_Access_Definition,
18206 N_Access_Function_Definition,
18207 N_Access_Procedure_Definition,
18208 N_Access_To_Object_Definition,
18210 N_Component_Definition)
18212 Nkind_In (P, N_Derived_Type_Definition,
18213 N_Discriminant_Specification,
18214 N_Formal_Object_Declaration,
18215 N_Object_Declaration,
18216 N_Object_Renaming_Declaration,
18217 N_Parameter_Specification,
18218 N_Subtype_Declaration);
18220 -- Create an Itype that is a duplicate of Entity (S) but with the
18221 -- null-exclusion attribute.
18223 if May_Have_Null_Exclusion
18224 and then Is_Access_Type (Entity (S))
18225 and then Null_Exclusion_Present (P)
18227 -- No need to check the case of an access to object definition.
18228 -- It is correct to define double not-null pointers.
18231 -- type Not_Null_Int_Ptr is not null access Integer;
18232 -- type Acc is not null access Not_Null_Int_Ptr;
18234 and then Nkind (P) /= N_Access_To_Object_Definition
18236 if Can_Never_Be_Null (Entity (S)) then
18237 case Nkind (Related_Nod) is
18238 when N_Full_Type_Declaration =>
18239 if Nkind (Type_Definition (Related_Nod))
18240 in N_Array_Type_Definition
18244 (Component_Definition
18245 (Type_Definition (Related_Nod)));
18248 Subtype_Indication (Type_Definition (Related_Nod));
18251 when N_Subtype_Declaration =>
18252 Error_Node := Subtype_Indication (Related_Nod);
18254 when N_Object_Declaration =>
18255 Error_Node := Object_Definition (Related_Nod);
18257 when N_Component_Declaration =>
18259 Subtype_Indication (Component_Definition (Related_Nod));
18261 when N_Allocator =>
18262 Error_Node := Expression (Related_Nod);
18265 pragma Assert (False);
18266 Error_Node := Related_Nod;
18270 ("`NOT NULL` not allowed (& already excludes null)",
18276 Create_Null_Excluding_Itype
18278 Related_Nod => P));
18279 Set_Entity (S, Etype (S));
18284 -- Case of constraint present, so that we have an N_Subtype_Indication
18285 -- node (this node is created only if constraints are present).
18288 Find_Type (Subtype_Mark (S));
18290 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
18292 (Nkind (Parent (S)) = N_Subtype_Declaration
18293 and then Is_Itype (Defining_Identifier (Parent (S))))
18295 Check_Incomplete (Subtype_Mark (S));
18299 Subtype_Mark_Id := Entity (Subtype_Mark (S));
18301 -- Explicit subtype declaration case
18303 if Nkind (P) = N_Subtype_Declaration then
18304 Def_Id := Defining_Identifier (P);
18306 -- Explicit derived type definition case
18308 elsif Nkind (P) = N_Derived_Type_Definition then
18309 Def_Id := Defining_Identifier (Parent (P));
18311 -- Implicit case, the Def_Id must be created as an implicit type.
18312 -- The one exception arises in the case of concurrent types, array
18313 -- and access types, where other subsidiary implicit types may be
18314 -- created and must appear before the main implicit type. In these
18315 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
18316 -- has not yet been called to create Def_Id.
18319 if Is_Array_Type (Subtype_Mark_Id)
18320 or else Is_Concurrent_Type (Subtype_Mark_Id)
18321 or else Is_Access_Type (Subtype_Mark_Id)
18325 -- For the other cases, we create a new unattached Itype,
18326 -- and set the indication to ensure it gets attached later.
18330 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
18334 -- If the kind of constraint is invalid for this kind of type,
18335 -- then give an error, and then pretend no constraint was given.
18337 if not Is_Valid_Constraint_Kind
18338 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
18341 ("incorrect constraint for this kind of type", Constraint (S));
18343 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18345 -- Set Ekind of orphan itype, to prevent cascaded errors
18347 if Present (Def_Id) then
18348 Set_Ekind (Def_Id, Ekind (Any_Type));
18351 -- Make recursive call, having got rid of the bogus constraint
18353 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
18356 -- Remaining processing depends on type
18358 case Ekind (Subtype_Mark_Id) is
18359 when Access_Kind =>
18360 Constrain_Access (Def_Id, S, Related_Nod);
18363 and then Is_Itype (Designated_Type (Def_Id))
18364 and then Nkind (Related_Nod) = N_Subtype_Declaration
18365 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
18367 Build_Itype_Reference
18368 (Designated_Type (Def_Id), Related_Nod);
18372 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
18374 when Decimal_Fixed_Point_Kind =>
18375 Constrain_Decimal (Def_Id, S);
18377 when Enumeration_Kind =>
18378 Constrain_Enumeration (Def_Id, S);
18380 when Ordinary_Fixed_Point_Kind =>
18381 Constrain_Ordinary_Fixed (Def_Id, S);
18384 Constrain_Float (Def_Id, S);
18386 when Integer_Kind =>
18387 Constrain_Integer (Def_Id, S);
18389 when E_Record_Type |
18392 E_Incomplete_Type =>
18393 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
18395 if Ekind (Def_Id) = E_Incomplete_Type then
18396 Set_Private_Dependents (Def_Id, New_Elmt_List);
18399 when Private_Kind =>
18400 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
18401 Set_Private_Dependents (Def_Id, New_Elmt_List);
18403 -- In case of an invalid constraint prevent further processing
18404 -- since the type constructed is missing expected fields.
18406 if Etype (Def_Id) = Any_Type then
18410 -- If the full view is that of a task with discriminants,
18411 -- we must constrain both the concurrent type and its
18412 -- corresponding record type. Otherwise we will just propagate
18413 -- the constraint to the full view, if available.
18415 if Present (Full_View (Subtype_Mark_Id))
18416 and then Has_Discriminants (Subtype_Mark_Id)
18417 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
18420 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
18422 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
18423 Constrain_Concurrent (Full_View_Id, S,
18424 Related_Nod, Related_Id, Suffix);
18425 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
18426 Set_Full_View (Def_Id, Full_View_Id);
18428 -- Introduce an explicit reference to the private subtype,
18429 -- to prevent scope anomalies in gigi if first use appears
18430 -- in a nested context, e.g. a later function body.
18431 -- Should this be generated in other contexts than a full
18432 -- type declaration?
18434 if Is_Itype (Def_Id)
18436 Nkind (Parent (P)) = N_Full_Type_Declaration
18438 Build_Itype_Reference (Def_Id, Parent (P));
18442 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
18445 when Concurrent_Kind =>
18446 Constrain_Concurrent (Def_Id, S,
18447 Related_Nod, Related_Id, Suffix);
18450 Error_Msg_N ("invalid subtype mark in subtype indication", S);
18453 -- Size and Convention are always inherited from the base type
18455 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
18456 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
18460 end Process_Subtype;
18462 ---------------------------------------
18463 -- Check_Anonymous_Access_Components --
18464 ---------------------------------------
18466 procedure Check_Anonymous_Access_Components
18467 (Typ_Decl : Node_Id;
18470 Comp_List : Node_Id)
18472 Loc : constant Source_Ptr := Sloc (Typ_Decl);
18473 Anon_Access : Entity_Id;
18476 Comp_Def : Node_Id;
18478 Type_Def : Node_Id;
18480 procedure Build_Incomplete_Type_Declaration;
18481 -- If the record type contains components that include an access to the
18482 -- current record, then create an incomplete type declaration for the
18483 -- record, to be used as the designated type of the anonymous access.
18484 -- This is done only once, and only if there is no previous partial
18485 -- view of the type.
18487 function Designates_T (Subt : Node_Id) return Boolean;
18488 -- Check whether a node designates the enclosing record type, or 'Class
18491 function Mentions_T (Acc_Def : Node_Id) return Boolean;
18492 -- Check whether an access definition includes a reference to
18493 -- the enclosing record type. The reference can be a subtype mark
18494 -- in the access definition itself, a 'Class attribute reference, or
18495 -- recursively a reference appearing in a parameter specification
18496 -- or result definition of an access_to_subprogram definition.
18498 --------------------------------------
18499 -- Build_Incomplete_Type_Declaration --
18500 --------------------------------------
18502 procedure Build_Incomplete_Type_Declaration is
18507 -- Is_Tagged indicates whether the type is tagged. It is tagged if
18508 -- it's "is new ... with record" or else "is tagged record ...".
18510 Is_Tagged : constant Boolean :=
18511 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
18514 (Record_Extension_Part (Type_Definition (Typ_Decl))))
18516 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
18517 and then Tagged_Present (Type_Definition (Typ_Decl)));
18520 -- If there is a previous partial view, no need to create a new one
18521 -- If the partial view, given by Prev, is incomplete, If Prev is
18522 -- a private declaration, full declaration is flagged accordingly.
18524 if Prev /= Typ then
18526 Make_Class_Wide_Type (Prev);
18527 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
18528 Set_Etype (Class_Wide_Type (Typ), Typ);
18533 elsif Has_Private_Declaration (Typ) then
18535 -- If we refer to T'Class inside T, and T is the completion of a
18536 -- private type, then we need to make sure the class-wide type
18540 Make_Class_Wide_Type (Typ);
18545 -- If there was a previous anonymous access type, the incomplete
18546 -- type declaration will have been created already.
18548 elsif Present (Current_Entity (Typ))
18549 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
18550 and then Full_View (Current_Entity (Typ)) = Typ
18553 and then Comes_From_Source (Current_Entity (Typ))
18554 and then not Is_Tagged_Type (Current_Entity (Typ))
18556 Make_Class_Wide_Type (Typ);
18558 ("incomplete view of tagged type should be declared tagged?",
18559 Parent (Current_Entity (Typ)));
18564 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
18565 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
18567 -- Type has already been inserted into the current scope. Remove
18568 -- it, and add incomplete declaration for type, so that subsequent
18569 -- anonymous access types can use it. The entity is unchained from
18570 -- the homonym list and from immediate visibility. After analysis,
18571 -- the entity in the incomplete declaration becomes immediately
18572 -- visible in the record declaration that follows.
18574 H := Current_Entity (Typ);
18577 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
18580 and then Homonym (H) /= Typ
18582 H := Homonym (Typ);
18585 Set_Homonym (H, Homonym (Typ));
18588 Insert_Before (Typ_Decl, Decl);
18590 Set_Full_View (Inc_T, Typ);
18594 -- Create a common class-wide type for both views, and set the
18595 -- Etype of the class-wide type to the full view.
18597 Make_Class_Wide_Type (Inc_T);
18598 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
18599 Set_Etype (Class_Wide_Type (Typ), Typ);
18602 end Build_Incomplete_Type_Declaration;
18608 function Designates_T (Subt : Node_Id) return Boolean is
18609 Type_Id : constant Name_Id := Chars (Typ);
18611 function Names_T (Nam : Node_Id) return Boolean;
18612 -- The record type has not been introduced in the current scope
18613 -- yet, so we must examine the name of the type itself, either
18614 -- an identifier T, or an expanded name of the form P.T, where
18615 -- P denotes the current scope.
18621 function Names_T (Nam : Node_Id) return Boolean is
18623 if Nkind (Nam) = N_Identifier then
18624 return Chars (Nam) = Type_Id;
18626 elsif Nkind (Nam) = N_Selected_Component then
18627 if Chars (Selector_Name (Nam)) = Type_Id then
18628 if Nkind (Prefix (Nam)) = N_Identifier then
18629 return Chars (Prefix (Nam)) = Chars (Current_Scope);
18631 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
18632 return Chars (Selector_Name (Prefix (Nam))) =
18633 Chars (Current_Scope);
18647 -- Start of processing for Designates_T
18650 if Nkind (Subt) = N_Identifier then
18651 return Chars (Subt) = Type_Id;
18653 -- Reference can be through an expanded name which has not been
18654 -- analyzed yet, and which designates enclosing scopes.
18656 elsif Nkind (Subt) = N_Selected_Component then
18657 if Names_T (Subt) then
18660 -- Otherwise it must denote an entity that is already visible.
18661 -- The access definition may name a subtype of the enclosing
18662 -- type, if there is a previous incomplete declaration for it.
18665 Find_Selected_Component (Subt);
18667 Is_Entity_Name (Subt)
18668 and then Scope (Entity (Subt)) = Current_Scope
18670 (Chars (Base_Type (Entity (Subt))) = Type_Id
18672 (Is_Class_Wide_Type (Entity (Subt))
18674 Chars (Etype (Base_Type (Entity (Subt)))) =
18678 -- A reference to the current type may appear as the prefix of
18679 -- a 'Class attribute.
18681 elsif Nkind (Subt) = N_Attribute_Reference
18682 and then Attribute_Name (Subt) = Name_Class
18684 return Names_T (Prefix (Subt));
18695 function Mentions_T (Acc_Def : Node_Id) return Boolean is
18696 Param_Spec : Node_Id;
18698 Acc_Subprg : constant Node_Id :=
18699 Access_To_Subprogram_Definition (Acc_Def);
18702 if No (Acc_Subprg) then
18703 return Designates_T (Subtype_Mark (Acc_Def));
18706 -- Component is an access_to_subprogram: examine its formals,
18707 -- and result definition in the case of an access_to_function.
18709 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
18710 while Present (Param_Spec) loop
18711 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
18712 and then Mentions_T (Parameter_Type (Param_Spec))
18716 elsif Designates_T (Parameter_Type (Param_Spec)) then
18723 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
18724 if Nkind (Result_Definition (Acc_Subprg)) =
18725 N_Access_Definition
18727 return Mentions_T (Result_Definition (Acc_Subprg));
18729 return Designates_T (Result_Definition (Acc_Subprg));
18736 -- Start of processing for Check_Anonymous_Access_Components
18739 if No (Comp_List) then
18743 Comp := First (Component_Items (Comp_List));
18744 while Present (Comp) loop
18745 if Nkind (Comp) = N_Component_Declaration
18747 (Access_Definition (Component_Definition (Comp)))
18749 Mentions_T (Access_Definition (Component_Definition (Comp)))
18751 Comp_Def := Component_Definition (Comp);
18753 Access_To_Subprogram_Definition
18754 (Access_Definition (Comp_Def));
18756 Build_Incomplete_Type_Declaration;
18757 Anon_Access := Make_Temporary (Loc, 'S');
18759 -- Create a declaration for the anonymous access type: either
18760 -- an access_to_object or an access_to_subprogram.
18762 if Present (Acc_Def) then
18763 if Nkind (Acc_Def) = N_Access_Function_Definition then
18765 Make_Access_Function_Definition (Loc,
18766 Parameter_Specifications =>
18767 Parameter_Specifications (Acc_Def),
18768 Result_Definition => Result_Definition (Acc_Def));
18771 Make_Access_Procedure_Definition (Loc,
18772 Parameter_Specifications =>
18773 Parameter_Specifications (Acc_Def));
18778 Make_Access_To_Object_Definition (Loc,
18779 Subtype_Indication =>
18782 (Access_Definition (Comp_Def))));
18784 Set_Constant_Present
18785 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
18787 (Type_Def, All_Present (Access_Definition (Comp_Def)));
18790 Set_Null_Exclusion_Present
18792 Null_Exclusion_Present (Access_Definition (Comp_Def)));
18795 Make_Full_Type_Declaration (Loc,
18796 Defining_Identifier => Anon_Access,
18797 Type_Definition => Type_Def);
18799 Insert_Before (Typ_Decl, Decl);
18802 -- If an access to object, Preserve entity of designated type,
18803 -- for ASIS use, before rewriting the component definition.
18805 if No (Acc_Def) then
18810 Desig := Entity (Subtype_Indication (Type_Def));
18812 -- If the access definition is to the current record,
18813 -- the visible entity at this point is an incomplete
18814 -- type. Retrieve the full view to simplify ASIS queries
18816 if Ekind (Desig) = E_Incomplete_Type then
18817 Desig := Full_View (Desig);
18821 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
18826 Make_Component_Definition (Loc,
18827 Subtype_Indication =>
18828 New_Occurrence_Of (Anon_Access, Loc)));
18830 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
18831 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
18833 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
18836 Set_Is_Local_Anonymous_Access (Anon_Access);
18842 if Present (Variant_Part (Comp_List)) then
18846 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
18847 while Present (V) loop
18848 Check_Anonymous_Access_Components
18849 (Typ_Decl, Typ, Prev, Component_List (V));
18850 Next_Non_Pragma (V);
18854 end Check_Anonymous_Access_Components;
18856 --------------------------------
18857 -- Preanalyze_Spec_Expression --
18858 --------------------------------
18860 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18861 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18863 In_Spec_Expression := True;
18864 Preanalyze_And_Resolve (N, T);
18865 In_Spec_Expression := Save_In_Spec_Expression;
18866 end Preanalyze_Spec_Expression;
18868 -----------------------------
18869 -- Record_Type_Declaration --
18870 -----------------------------
18872 procedure Record_Type_Declaration
18877 Def : constant Node_Id := Type_Definition (N);
18878 Is_Tagged : Boolean;
18879 Tag_Comp : Entity_Id;
18882 -- These flags must be initialized before calling Process_Discriminants
18883 -- because this routine makes use of them.
18885 Set_Ekind (T, E_Record_Type);
18887 Init_Size_Align (T);
18888 Set_Interfaces (T, No_Elist);
18889 Set_Stored_Constraint (T, No_Elist);
18893 if Ada_Version < Ada_2005
18894 or else not Interface_Present (Def)
18896 if Limited_Present (Def) then
18897 Check_Formal_Restriction ("limited is not allowed", N);
18900 if Abstract_Present (Def) then
18901 Check_Formal_Restriction ("abstract is not allowed", N);
18904 -- The flag Is_Tagged_Type might have already been set by
18905 -- Find_Type_Name if it detected an error for declaration T. This
18906 -- arises in the case of private tagged types where the full view
18907 -- omits the word tagged.
18910 Tagged_Present (Def)
18911 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
18913 Set_Is_Tagged_Type (T, Is_Tagged);
18914 Set_Is_Limited_Record (T, Limited_Present (Def));
18916 -- Type is abstract if full declaration carries keyword, or if
18917 -- previous partial view did.
18919 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
18920 or else Abstract_Present (Def));
18923 Check_Formal_Restriction ("interface is not allowed", N);
18926 Analyze_Interface_Declaration (T, Def);
18928 if Present (Discriminant_Specifications (N)) then
18930 ("interface types cannot have discriminants",
18931 Defining_Identifier
18932 (First (Discriminant_Specifications (N))));
18936 -- First pass: if there are self-referential access components,
18937 -- create the required anonymous access type declarations, and if
18938 -- need be an incomplete type declaration for T itself.
18940 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
18942 if Ada_Version >= Ada_2005
18943 and then Present (Interface_List (Def))
18945 Check_Interfaces (N, Def);
18948 Ifaces_List : Elist_Id;
18951 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
18952 -- already in the parents.
18956 Ifaces_List => Ifaces_List,
18957 Exclude_Parents => True);
18959 Set_Interfaces (T, Ifaces_List);
18963 -- Records constitute a scope for the component declarations within.
18964 -- The scope is created prior to the processing of these declarations.
18965 -- Discriminants are processed first, so that they are visible when
18966 -- processing the other components. The Ekind of the record type itself
18967 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
18969 -- Enter record scope
18973 -- If an incomplete or private type declaration was already given for
18974 -- the type, then this scope already exists, and the discriminants have
18975 -- been declared within. We must verify that the full declaration
18976 -- matches the incomplete one.
18978 Check_Or_Process_Discriminants (N, T, Prev);
18980 Set_Is_Constrained (T, not Has_Discriminants (T));
18981 Set_Has_Delayed_Freeze (T, True);
18983 -- For tagged types add a manually analyzed component corresponding
18984 -- to the component _tag, the corresponding piece of tree will be
18985 -- expanded as part of the freezing actions if it is not a CPP_Class.
18989 -- Do not add the tag unless we are in expansion mode
18991 if Expander_Active then
18992 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
18993 Enter_Name (Tag_Comp);
18995 Set_Ekind (Tag_Comp, E_Component);
18996 Set_Is_Tag (Tag_Comp);
18997 Set_Is_Aliased (Tag_Comp);
18998 Set_Etype (Tag_Comp, RTE (RE_Tag));
18999 Set_DT_Entry_Count (Tag_Comp, No_Uint);
19000 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
19001 Init_Component_Location (Tag_Comp);
19003 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
19004 -- implemented interfaces.
19006 if Has_Interfaces (T) then
19007 Add_Interface_Tag_Components (N, T);
19011 Make_Class_Wide_Type (T);
19012 Set_Direct_Primitive_Operations (T, New_Elmt_List);
19015 -- We must suppress range checks when processing record components in
19016 -- the presence of discriminants, since we don't want spurious checks to
19017 -- be generated during their analysis, but Suppress_Range_Checks flags
19018 -- must be reset the after processing the record definition.
19020 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
19021 -- couldn't we just use the normal range check suppression method here.
19022 -- That would seem cleaner ???
19024 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
19025 Set_Kill_Range_Checks (T, True);
19026 Record_Type_Definition (Def, Prev);
19027 Set_Kill_Range_Checks (T, False);
19029 Record_Type_Definition (Def, Prev);
19032 -- Exit from record scope
19036 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
19037 -- the implemented interfaces and associate them an aliased entity.
19040 and then not Is_Empty_List (Interface_List (Def))
19042 Derive_Progenitor_Subprograms (T, T);
19044 end Record_Type_Declaration;
19046 ----------------------------
19047 -- Record_Type_Definition --
19048 ----------------------------
19050 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
19051 Component : Entity_Id;
19052 Ctrl_Components : Boolean := False;
19053 Final_Storage_Only : Boolean;
19057 if Ekind (Prev_T) = E_Incomplete_Type then
19058 T := Full_View (Prev_T);
19063 -- In SPARK or ALFA, tagged types and type extensions may only be
19064 -- declared in the specification of library unit packages.
19066 if Present (Def) and then Is_Tagged_Type (T) then
19072 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
19073 Typ := Parent (Def);
19076 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
19077 Typ := Parent (Parent (Def));
19080 Ctxt := Parent (Typ);
19082 if Nkind (Ctxt) = N_Package_Body
19083 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
19085 Check_Formal_Restriction
19086 ("type should be defined in package specification", Typ);
19088 elsif Nkind (Ctxt) /= N_Package_Specification
19089 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
19091 Check_Formal_Restriction
19092 ("type should be defined in library unit package", Typ);
19097 Final_Storage_Only := not Is_Controlled (T);
19099 -- Ada 2005: check whether an explicit Limited is present in a derived
19100 -- type declaration.
19102 if Nkind (Parent (Def)) = N_Derived_Type_Definition
19103 and then Limited_Present (Parent (Def))
19105 Set_Is_Limited_Record (T);
19108 -- If the component list of a record type is defined by the reserved
19109 -- word null and there is no discriminant part, then the record type has
19110 -- no components and all records of the type are null records (RM 3.7)
19111 -- This procedure is also called to process the extension part of a
19112 -- record extension, in which case the current scope may have inherited
19116 or else No (Component_List (Def))
19117 or else Null_Present (Component_List (Def))
19119 if not Is_Tagged_Type (T) then
19120 Check_Formal_Restriction ("non-tagged record cannot be null", Def);
19124 Analyze_Declarations (Component_Items (Component_List (Def)));
19126 if Present (Variant_Part (Component_List (Def))) then
19127 Check_Formal_Restriction ("variant part is not allowed", Def);
19128 Analyze (Variant_Part (Component_List (Def)));
19132 -- After completing the semantic analysis of the record definition,
19133 -- record components, both new and inherited, are accessible. Set their
19134 -- kind accordingly. Exclude malformed itypes from illegal declarations,
19135 -- whose Ekind may be void.
19137 Component := First_Entity (Current_Scope);
19138 while Present (Component) loop
19139 if Ekind (Component) = E_Void
19140 and then not Is_Itype (Component)
19142 Set_Ekind (Component, E_Component);
19143 Init_Component_Location (Component);
19146 if Has_Task (Etype (Component)) then
19150 if Ekind (Component) /= E_Component then
19153 -- Do not set Has_Controlled_Component on a class-wide equivalent
19154 -- type. See Make_CW_Equivalent_Type.
19156 elsif not Is_Class_Wide_Equivalent_Type (T)
19157 and then (Has_Controlled_Component (Etype (Component))
19158 or else (Chars (Component) /= Name_uParent
19159 and then Is_Controlled (Etype (Component))))
19161 Set_Has_Controlled_Component (T, True);
19162 Final_Storage_Only :=
19164 and then Finalize_Storage_Only (Etype (Component));
19165 Ctrl_Components := True;
19168 Next_Entity (Component);
19171 -- A Type is Finalize_Storage_Only only if all its controlled components
19174 if Ctrl_Components then
19175 Set_Finalize_Storage_Only (T, Final_Storage_Only);
19178 -- Place reference to end record on the proper entity, which may
19179 -- be a partial view.
19181 if Present (Def) then
19182 Process_End_Label (Def, 'e', Prev_T);
19184 end Record_Type_Definition;
19186 ------------------------
19187 -- Replace_Components --
19188 ------------------------
19190 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
19191 function Process (N : Node_Id) return Traverse_Result;
19197 function Process (N : Node_Id) return Traverse_Result is
19201 if Nkind (N) = N_Discriminant_Specification then
19202 Comp := First_Discriminant (Typ);
19203 while Present (Comp) loop
19204 if Chars (Comp) = Chars (Defining_Identifier (N)) then
19205 Set_Defining_Identifier (N, Comp);
19209 Next_Discriminant (Comp);
19212 elsif Nkind (N) = N_Component_Declaration then
19213 Comp := First_Component (Typ);
19214 while Present (Comp) loop
19215 if Chars (Comp) = Chars (Defining_Identifier (N)) then
19216 Set_Defining_Identifier (N, Comp);
19220 Next_Component (Comp);
19227 procedure Replace is new Traverse_Proc (Process);
19229 -- Start of processing for Replace_Components
19233 end Replace_Components;
19235 -------------------------------
19236 -- Set_Completion_Referenced --
19237 -------------------------------
19239 procedure Set_Completion_Referenced (E : Entity_Id) is
19241 -- If in main unit, mark entity that is a completion as referenced,
19242 -- warnings go on the partial view when needed.
19244 if In_Extended_Main_Source_Unit (E) then
19245 Set_Referenced (E);
19247 end Set_Completion_Referenced;
19249 ---------------------
19250 -- Set_Fixed_Range --
19251 ---------------------
19253 -- The range for fixed-point types is complicated by the fact that we
19254 -- do not know the exact end points at the time of the declaration. This
19255 -- is true for three reasons:
19257 -- A size clause may affect the fudging of the end-points
19258 -- A small clause may affect the values of the end-points
19259 -- We try to include the end-points if it does not affect the size
19261 -- This means that the actual end-points must be established at the point
19262 -- when the type is frozen. Meanwhile, we first narrow the range as
19263 -- permitted (so that it will fit if necessary in a small specified size),
19264 -- and then build a range subtree with these narrowed bounds.
19266 -- Set_Fixed_Range constructs the range from real literal values, and sets
19267 -- the range as the Scalar_Range of the given fixed-point type entity.
19269 -- The parent of this range is set to point to the entity so that it is
19270 -- properly hooked into the tree (unlike normal Scalar_Range entries for
19271 -- other scalar types, which are just pointers to the range in the
19272 -- original tree, this would otherwise be an orphan).
19274 -- The tree is left unanalyzed. When the type is frozen, the processing
19275 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
19276 -- analyzed, and uses this as an indication that it should complete
19277 -- work on the range (it will know the final small and size values).
19279 procedure Set_Fixed_Range
19285 S : constant Node_Id :=
19287 Low_Bound => Make_Real_Literal (Loc, Lo),
19288 High_Bound => Make_Real_Literal (Loc, Hi));
19290 Set_Scalar_Range (E, S);
19292 end Set_Fixed_Range;
19294 ----------------------------------
19295 -- Set_Scalar_Range_For_Subtype --
19296 ----------------------------------
19298 procedure Set_Scalar_Range_For_Subtype
19299 (Def_Id : Entity_Id;
19303 Kind : constant Entity_Kind := Ekind (Def_Id);
19306 -- Defend against previous error
19308 if Nkind (R) = N_Error then
19312 Set_Scalar_Range (Def_Id, R);
19314 -- We need to link the range into the tree before resolving it so
19315 -- that types that are referenced, including importantly the subtype
19316 -- itself, are properly frozen (Freeze_Expression requires that the
19317 -- expression be properly linked into the tree). Of course if it is
19318 -- already linked in, then we do not disturb the current link.
19320 if No (Parent (R)) then
19321 Set_Parent (R, Def_Id);
19324 -- Reset the kind of the subtype during analysis of the range, to
19325 -- catch possible premature use in the bounds themselves.
19327 Set_Ekind (Def_Id, E_Void);
19328 Process_Range_Expr_In_Decl (R, Subt);
19329 Set_Ekind (Def_Id, Kind);
19330 end Set_Scalar_Range_For_Subtype;
19332 --------------------------------------------------------
19333 -- Set_Stored_Constraint_From_Discriminant_Constraint --
19334 --------------------------------------------------------
19336 procedure Set_Stored_Constraint_From_Discriminant_Constraint
19340 -- Make sure set if encountered during Expand_To_Stored_Constraint
19342 Set_Stored_Constraint (E, No_Elist);
19344 -- Give it the right value
19346 if Is_Constrained (E) and then Has_Discriminants (E) then
19347 Set_Stored_Constraint (E,
19348 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
19350 end Set_Stored_Constraint_From_Discriminant_Constraint;
19352 -------------------------------------
19353 -- Signed_Integer_Type_Declaration --
19354 -------------------------------------
19356 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19357 Implicit_Base : Entity_Id;
19358 Base_Typ : Entity_Id;
19361 Errs : Boolean := False;
19365 function Can_Derive_From (E : Entity_Id) return Boolean;
19366 -- Determine whether given bounds allow derivation from specified type
19368 procedure Check_Bound (Expr : Node_Id);
19369 -- Check bound to make sure it is integral and static. If not, post
19370 -- appropriate error message and set Errs flag
19372 ---------------------
19373 -- Can_Derive_From --
19374 ---------------------
19376 -- Note we check both bounds against both end values, to deal with
19377 -- strange types like ones with a range of 0 .. -12341234.
19379 function Can_Derive_From (E : Entity_Id) return Boolean is
19380 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
19381 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
19383 return Lo <= Lo_Val and then Lo_Val <= Hi
19385 Lo <= Hi_Val and then Hi_Val <= Hi;
19386 end Can_Derive_From;
19392 procedure Check_Bound (Expr : Node_Id) is
19394 -- If a range constraint is used as an integer type definition, each
19395 -- bound of the range must be defined by a static expression of some
19396 -- integer type, but the two bounds need not have the same integer
19397 -- type (Negative bounds are allowed.) (RM 3.5.4)
19399 if not Is_Integer_Type (Etype (Expr)) then
19401 ("integer type definition bounds must be of integer type", Expr);
19404 elsif not Is_OK_Static_Expression (Expr) then
19405 Flag_Non_Static_Expr
19406 ("non-static expression used for integer type bound!", Expr);
19409 -- The bounds are folded into literals, and we set their type to be
19410 -- universal, to avoid typing difficulties: we cannot set the type
19411 -- of the literal to the new type, because this would be a forward
19412 -- reference for the back end, and if the original type is user-
19413 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
19416 if Is_Entity_Name (Expr) then
19417 Fold_Uint (Expr, Expr_Value (Expr), True);
19420 Set_Etype (Expr, Universal_Integer);
19424 -- Start of processing for Signed_Integer_Type_Declaration
19427 -- Create an anonymous base type
19430 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
19432 -- Analyze and check the bounds, they can be of any integer type
19434 Lo := Low_Bound (Def);
19435 Hi := High_Bound (Def);
19437 -- Arbitrarily use Integer as the type if either bound had an error
19439 if Hi = Error or else Lo = Error then
19440 Base_Typ := Any_Integer;
19441 Set_Error_Posted (T, True);
19443 -- Here both bounds are OK expressions
19446 Analyze_And_Resolve (Lo, Any_Integer);
19447 Analyze_And_Resolve (Hi, Any_Integer);
19453 Hi := Type_High_Bound (Standard_Long_Long_Integer);
19454 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
19457 -- Find type to derive from
19459 Lo_Val := Expr_Value (Lo);
19460 Hi_Val := Expr_Value (Hi);
19462 if Can_Derive_From (Standard_Short_Short_Integer) then
19463 Base_Typ := Base_Type (Standard_Short_Short_Integer);
19465 elsif Can_Derive_From (Standard_Short_Integer) then
19466 Base_Typ := Base_Type (Standard_Short_Integer);
19468 elsif Can_Derive_From (Standard_Integer) then
19469 Base_Typ := Base_Type (Standard_Integer);
19471 elsif Can_Derive_From (Standard_Long_Integer) then
19472 Base_Typ := Base_Type (Standard_Long_Integer);
19474 elsif Can_Derive_From (Standard_Long_Long_Integer) then
19475 Base_Typ := Base_Type (Standard_Long_Long_Integer);
19478 Base_Typ := Base_Type (Standard_Long_Long_Integer);
19479 Error_Msg_N ("integer type definition bounds out of range", Def);
19480 Hi := Type_High_Bound (Standard_Long_Long_Integer);
19481 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
19485 -- Complete both implicit base and declared first subtype entities
19487 Set_Etype (Implicit_Base, Base_Typ);
19488 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
19489 Set_Size_Info (Implicit_Base, (Base_Typ));
19490 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
19491 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
19493 Set_Ekind (T, E_Signed_Integer_Subtype);
19494 Set_Etype (T, Implicit_Base);
19496 Set_Size_Info (T, (Implicit_Base));
19497 Set_First_Rep_Item (T, First_Rep_Item (Implicit_Base));
19498 Set_Scalar_Range (T, Def);
19499 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
19500 Set_Is_Constrained (T);
19501 end Signed_Integer_Type_Declaration;