1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2020, 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 Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Einfo; use Einfo;
33 with Errout; use Errout;
34 with Eval_Fat; use Eval_Fat;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dist; use Exp_Dist;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nlists; use Nlists;
49 with Nmake; use Nmake;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Case; use Sem_Case;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch7; use Sem_Ch7;
60 with Sem_Ch8; use Sem_Ch8;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Dim; use Sem_Dim;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Elab; use Sem_Elab;
66 with Sem_Elim; use Sem_Elim;
67 with Sem_Eval; use Sem_Eval;
68 with Sem_Mech; use Sem_Mech;
69 with Sem_Res; use Sem_Res;
70 with Sem_Smem; use Sem_Smem;
71 with Sem_Type; use Sem_Type;
72 with Sem_Util; use Sem_Util;
73 with Sem_Warn; use Sem_Warn;
74 with Stand; use Stand;
75 with Sinfo; use Sinfo;
76 with Sinput; use Sinput;
77 with Snames; use Snames;
78 with Targparm; use Targparm;
79 with Tbuild; use Tbuild;
80 with Ttypes; use Ttypes;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
84 package body Sem_Ch3 is
86 -----------------------
87 -- Local Subprograms --
88 -----------------------
90 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
91 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
92 -- abstract interface types implemented by a record type or a derived
95 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id);
96 -- When an access-to-subprogram type has pre/postconditions, we build a
97 -- subprogram that includes these contracts and is invoked by an indirect
98 -- call through the corresponding access type.
100 procedure Build_Derived_Type
102 Parent_Type : Entity_Id;
103 Derived_Type : Entity_Id;
104 Is_Completion : Boolean;
105 Derive_Subps : Boolean := True);
106 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
107 -- the N_Full_Type_Declaration node containing the derived type definition.
108 -- Parent_Type is the entity for the parent type in the derived type
109 -- definition and Derived_Type the actual derived type. Is_Completion must
110 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
111 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
112 -- completion of a private type declaration. If Is_Completion is set to
113 -- True, N is the completion of a private type declaration and Derived_Type
114 -- is different from the defining identifier inside N (i.e. Derived_Type /=
115 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
116 -- subprograms should be derived. The only case where this parameter is
117 -- False is when Build_Derived_Type is recursively called to process an
118 -- implicit derived full type for a type derived from a private type (in
119 -- that case the subprograms must only be derived for the private view of
122 -- ??? These flags need a bit of re-examination and re-documentation:
123 -- ??? are they both necessary (both seem related to the recursion)?
125 procedure Build_Derived_Access_Type
127 Parent_Type : Entity_Id;
128 Derived_Type : Entity_Id);
129 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
130 -- create an implicit base if the parent type is constrained or if the
131 -- subtype indication has a constraint.
133 procedure Build_Derived_Array_Type
135 Parent_Type : Entity_Id;
136 Derived_Type : Entity_Id);
137 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
138 -- create an implicit base if the parent type is constrained or if the
139 -- subtype indication has a constraint.
141 procedure Build_Derived_Concurrent_Type
143 Parent_Type : Entity_Id;
144 Derived_Type : Entity_Id);
145 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
146 -- protected type, inherit entries and protected subprograms, check
147 -- legality of discriminant constraints if any.
149 procedure Build_Derived_Enumeration_Type
151 Parent_Type : Entity_Id;
152 Derived_Type : Entity_Id);
153 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
154 -- type, we must create a new list of literals. Types derived from
155 -- Character and [Wide_]Wide_Character are special-cased.
157 procedure Build_Derived_Numeric_Type
159 Parent_Type : Entity_Id;
160 Derived_Type : Entity_Id);
161 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
162 -- an anonymous base type, and propagate constraint to subtype if needed.
164 procedure Build_Derived_Private_Type
166 Parent_Type : Entity_Id;
167 Derived_Type : Entity_Id;
168 Is_Completion : Boolean;
169 Derive_Subps : Boolean := True);
170 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
171 -- because the parent may or may not have a completion, and the derivation
172 -- may itself be a completion.
174 procedure Build_Derived_Record_Type
176 Parent_Type : Entity_Id;
177 Derived_Type : Entity_Id;
178 Derive_Subps : Boolean := True);
179 -- Subsidiary procedure used for tagged and untagged record types
180 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
181 -- All parameters are as in Build_Derived_Type except that N, in
182 -- addition to being an N_Full_Type_Declaration node, can also be an
183 -- N_Private_Extension_Declaration node. See the definition of this routine
184 -- for much more info. Derive_Subps indicates whether subprograms should be
185 -- derived from the parent type. The only case where Derive_Subps is False
186 -- is for an implicit derived full type for a type derived from a private
187 -- type (see Build_Derived_Type).
189 procedure Build_Discriminal (Discrim : Entity_Id);
190 -- Create the discriminal corresponding to discriminant Discrim, that is
191 -- the parameter corresponding to Discrim to be used in initialization
192 -- procedures for the type where Discrim is a discriminant. Discriminals
193 -- are not used during semantic analysis, and are not fully defined
194 -- entities until expansion. Thus they are not given a scope until
195 -- initialization procedures are built.
197 function Build_Discriminant_Constraints
200 Derived_Def : Boolean := False) return Elist_Id;
201 -- Validate discriminant constraints and return the list of the constraints
202 -- in order of discriminant declarations, where T is the discriminated
203 -- unconstrained type. Def is the N_Subtype_Indication node where the
204 -- discriminants constraints for T are specified. Derived_Def is True
205 -- when building the discriminant constraints in a derived type definition
206 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
207 -- type and Def is the constraint "(xxx)" on T and this routine sets the
208 -- Corresponding_Discriminant field of the discriminants in the derived
209 -- type D to point to the corresponding discriminants in the parent type T.
211 procedure Build_Discriminated_Subtype
215 Related_Nod : Node_Id;
216 For_Access : Boolean := False);
217 -- Subsidiary procedure to Constrain_Discriminated_Type and to
218 -- Process_Incomplete_Dependents. Given
220 -- T (a possibly discriminated base type)
221 -- Def_Id (a very partially built subtype for T),
223 -- the call completes Def_Id to be the appropriate E_*_Subtype.
225 -- The Elist is the list of discriminant constraints if any (it is set
226 -- to No_Elist if T is not a discriminated type, and to an empty list if
227 -- T has discriminants but there are no discriminant constraints). The
228 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
229 -- The For_Access says whether or not this subtype is really constraining
232 function Build_Scalar_Bound
235 Der_T : Entity_Id) return Node_Id;
236 -- The bounds of a derived scalar type are conversions of the bounds of
237 -- the parent type. Optimize the representation if the bounds are literals.
238 -- Needs a more complete spec--what are the parameters exactly, and what
239 -- exactly is the returned value, and how is Bound affected???
241 procedure Check_Access_Discriminant_Requires_Limited
244 -- Check the restriction that the type to which an access discriminant
245 -- belongs must be a concurrent type or a descendant of a type with
246 -- the reserved word 'limited' in its declaration.
248 procedure Check_Anonymous_Access_Components
252 Comp_List : Node_Id);
253 -- Ada 2005 AI-382: an access component in a record definition can refer to
254 -- the enclosing record, in which case it denotes the type itself, and not
255 -- the current instance of the type. We create an anonymous access type for
256 -- the component, and flag it as an access to a component, so accessibility
257 -- checks are properly performed on it. The declaration of the access type
258 -- is placed ahead of that of the record to prevent order-of-elaboration
259 -- circularity issues in Gigi. We create an incomplete type for the record
260 -- declaration, which is the designated type of the anonymous access.
262 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id);
263 -- Check that, if a new discriminant is used in a constraint defining the
264 -- parent subtype of a derivation, its subtype is statically compatible
265 -- with the subtype of the corresponding parent discriminant (RM 3.7(15)).
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 constraints,
351 -- Constraints, for Typ and a component Comp of Typ, create and return the
352 -- type corresponding to Etype (Comp) where all discriminant references
353 -- are replaced with the corresponding constraint. If Etype (Comp) contains
354 -- no discriminant references then it is returned as-is. Constrained_Typ
355 -- is the final constrained subtype to which the constrained component
356 -- belongs. Related_Node is the node where we attach all created itypes.
358 procedure Constrain_Access
359 (Def_Id : in out Entity_Id;
361 Related_Nod : Node_Id);
362 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
363 -- an anonymous type created for a subtype indication. In that case it is
364 -- created in the procedure and attached to Related_Nod.
366 procedure Constrain_Array
367 (Def_Id : in out Entity_Id;
369 Related_Nod : Node_Id;
370 Related_Id : Entity_Id;
372 -- Apply a list of index constraints to an unconstrained array type. The
373 -- first parameter is the entity for the resulting subtype. A value of
374 -- Empty for Def_Id indicates that an implicit type must be created, but
375 -- creation is delayed (and must be done by this procedure) because other
376 -- subsidiary implicit types must be created first (which is why Def_Id
377 -- is an in/out parameter). The second parameter is a subtype indication
378 -- node for the constrained array to be created (e.g. something of the
379 -- form string (1 .. 10)). Related_Nod gives the place where this type
380 -- has to be inserted in the tree. The Related_Id and Suffix parameters
381 -- are used to build the associated Implicit type name.
383 procedure Constrain_Concurrent
384 (Def_Id : in out Entity_Id;
386 Related_Nod : Node_Id;
387 Related_Id : Entity_Id;
389 -- Apply list of discriminant constraints to an unconstrained concurrent
392 -- SI is the N_Subtype_Indication node containing the constraint and
393 -- the unconstrained type to constrain.
395 -- Def_Id is the entity for the resulting constrained subtype. A value
396 -- of Empty for Def_Id indicates that an implicit type must be created,
397 -- but creation is delayed (and must be done by this procedure) because
398 -- other subsidiary implicit types must be created first (which is why
399 -- Def_Id is an in/out parameter).
401 -- Related_Nod gives the place where this type has to be inserted
404 -- The last two arguments are used to create its external name if needed.
406 function Constrain_Corresponding_Record
407 (Prot_Subt : Entity_Id;
408 Corr_Rec : Entity_Id;
409 Related_Nod : Node_Id) return Entity_Id;
410 -- When constraining a protected type or task type with discriminants,
411 -- constrain the corresponding record with the same discriminant values.
413 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
414 -- Constrain a decimal fixed point type with a digits constraint and/or a
415 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
417 procedure Constrain_Discriminated_Type
420 Related_Nod : Node_Id;
421 For_Access : Boolean := False);
422 -- Process discriminant constraints of composite type. Verify that values
423 -- have been provided for all discriminants, that the original type is
424 -- unconstrained, and that the types of the supplied expressions match
425 -- the discriminant types. The first three parameters are like in routine
426 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
429 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
430 -- Constrain an enumeration type with a range constraint. This is identical
431 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
433 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
434 -- Constrain a floating point type with either a digits constraint
435 -- and/or a range constraint, building a E_Floating_Point_Subtype.
437 procedure Constrain_Index
440 Related_Nod : Node_Id;
441 Related_Id : Entity_Id;
444 -- Process an index constraint S in a constrained array declaration. The
445 -- constraint can be a subtype name, or a range with or without an explicit
446 -- subtype mark. The index is the corresponding index of the unconstrained
447 -- array. The Related_Id and Suffix parameters are used to build the
448 -- associated Implicit type name.
450 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
451 -- Build subtype of a signed or modular integer type
453 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
454 -- Constrain an ordinary fixed point type with a range constraint, and
455 -- build an E_Ordinary_Fixed_Point_Subtype entity.
457 procedure Copy_And_Swap (Priv, Full : Entity_Id);
458 -- Copy the Priv entity into the entity of its full declaration then swap
459 -- the two entities in such a manner that the former private type is now
460 -- seen as a full type.
462 procedure Decimal_Fixed_Point_Type_Declaration
465 -- Create a new decimal fixed point type, and apply the constraint to
466 -- obtain a subtype of this new type.
468 procedure Complete_Private_Subtype
471 Full_Base : Entity_Id;
472 Related_Nod : Node_Id);
473 -- Complete the implicit full view of a private subtype by setting the
474 -- appropriate semantic fields. If the full view of the parent is a record
475 -- type, build constrained components of subtype.
477 procedure Derive_Progenitor_Subprograms
478 (Parent_Type : Entity_Id;
479 Tagged_Type : Entity_Id);
480 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
481 -- operations of progenitors of Tagged_Type, and replace the subsidiary
482 -- subtypes with Tagged_Type, to build the specs of the inherited interface
483 -- primitives. The derived primitives are aliased to those of the
484 -- interface. This routine takes care also of transferring to the full view
485 -- subprograms associated with the partial view of Tagged_Type that cover
486 -- interface primitives.
488 procedure Derived_Standard_Character
490 Parent_Type : Entity_Id;
491 Derived_Type : Entity_Id);
492 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
493 -- derivations from types Standard.Character and Standard.Wide_Character.
495 procedure Derived_Type_Declaration
498 Is_Completion : Boolean);
499 -- Process a derived type declaration. Build_Derived_Type is invoked
500 -- to process the actual derived type definition. Parameters N and
501 -- Is_Completion have the same meaning as in Build_Derived_Type.
502 -- T is the N_Defining_Identifier for the entity defined in the
503 -- N_Full_Type_Declaration node N, that is T is the derived type.
505 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
506 -- Insert each literal in symbol table, as an overloadable identifier. Each
507 -- enumeration type is mapped into a sequence of integers, and each literal
508 -- is defined as a constant with integer value. If any of the literals are
509 -- character literals, the type is a character type, which means that
510 -- strings are legal aggregates for arrays of components of the type.
512 function Expand_To_Stored_Constraint
514 Constraint : Elist_Id) return Elist_Id;
515 -- Given a constraint (i.e. a list of expressions) on the discriminants of
516 -- Typ, expand it into a constraint on the stored discriminants and return
517 -- the new list of expressions constraining the stored discriminants.
519 function Find_Type_Of_Object
521 Related_Nod : Node_Id) return Entity_Id;
522 -- Get type entity for object referenced by Obj_Def, attaching the implicit
523 -- types generated to Related_Nod.
525 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
526 -- Create a new float and apply the constraint to obtain subtype of it
528 function Has_Range_Constraint (N : Node_Id) return Boolean;
529 -- Given an N_Subtype_Indication node N, return True if a range constraint
530 -- is present, either directly, or as part of a digits or delta constraint.
531 -- In addition, a digits constraint in the decimal case returns True, since
532 -- it establishes a default range if no explicit range is present.
534 function Inherit_Components
536 Parent_Base : Entity_Id;
537 Derived_Base : Entity_Id;
539 Inherit_Discr : Boolean;
540 Discs : Elist_Id) return Elist_Id;
541 -- Called from Build_Derived_Record_Type to inherit the components of
542 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
543 -- For more information on derived types and component inheritance please
544 -- consult the comment above the body of Build_Derived_Record_Type.
546 -- N is the original derived type declaration
548 -- Is_Tagged is set if we are dealing with tagged types
550 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
551 -- Parent_Base, otherwise no discriminants are inherited.
553 -- Discs gives the list of constraints that apply to Parent_Base in the
554 -- derived type declaration. If Discs is set to No_Elist, then we have
555 -- the following situation:
557 -- type Parent (D1..Dn : ..) is [tagged] record ...;
558 -- type Derived is new Parent [with ...];
560 -- which gets treated as
562 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
564 -- For untagged types the returned value is an association list. The list
565 -- starts from the association (Parent_Base => Derived_Base), and then it
566 -- contains a sequence of the associations of the form
568 -- (Old_Component => New_Component),
570 -- where Old_Component is the Entity_Id of a component in Parent_Base and
571 -- New_Component is the Entity_Id of the corresponding component in
572 -- Derived_Base. For untagged records, this association list is needed when
573 -- copying the record declaration for the derived base. In the tagged case
574 -- the value returned is irrelevant.
576 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
577 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
578 -- Determine whether subprogram Subp is a procedure subject to pragma
579 -- Extensions_Visible with value False and has at least one controlling
580 -- parameter of mode OUT.
582 function Is_Valid_Constraint_Kind
584 Constraint_Kind : Node_Kind) return Boolean;
585 -- Returns True if it is legal to apply the given kind of constraint to the
586 -- given kind of type (index constraint to an array type, for example).
588 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
589 -- Create new modular type. Verify that modulus is in bounds
591 procedure New_Concatenation_Op (Typ : Entity_Id);
592 -- Create an abbreviated declaration for an operator in order to
593 -- materialize concatenation on array types.
595 procedure Ordinary_Fixed_Point_Type_Declaration
598 -- Create a new ordinary fixed point type, and apply the constraint to
599 -- obtain subtype of it.
601 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id);
602 -- Wrapper on Preanalyze_Spec_Expression for default expressions, so that
603 -- In_Default_Expr can be properly adjusted.
605 procedure Prepare_Private_Subtype_Completion
607 Related_Nod : Node_Id);
608 -- Id is a subtype of some private type. Creates the full declaration
609 -- associated with Id whenever possible, i.e. when the full declaration
610 -- of the base type is already known. Records each subtype into
611 -- Private_Dependents of the base type.
613 procedure Process_Incomplete_Dependents
617 -- Process all entities that depend on an incomplete type. There include
618 -- subtypes, subprogram types that mention the incomplete type in their
619 -- profiles, and subprogram with access parameters that designate the
622 -- Inc_T is the defining identifier of an incomplete type declaration, its
623 -- Ekind is E_Incomplete_Type.
625 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
627 -- Full_T is N's defining identifier.
629 -- Subtypes of incomplete types with discriminants are completed when the
630 -- parent type is. This is simpler than private subtypes, because they can
631 -- only appear in the same scope, and there is no need to exchange views.
632 -- Similarly, access_to_subprogram types may have a parameter or a return
633 -- type that is an incomplete type, and that must be replaced with the
636 -- If the full type is tagged, subprogram with access parameters that
637 -- designated the incomplete may be primitive operations of the full type,
638 -- and have to be processed accordingly.
640 procedure Process_Real_Range_Specification (Def : Node_Id);
641 -- Given the type definition for a real type, this procedure processes and
642 -- checks the real range specification of this type definition if one is
643 -- present. If errors are found, error messages are posted, and the
644 -- Real_Range_Specification of Def is reset to Empty.
646 procedure Record_Type_Declaration
650 -- Process a record type declaration (for both untagged and tagged
651 -- records). Parameters T and N are exactly like in procedure
652 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
653 -- for this routine. If this is the completion of an incomplete type
654 -- declaration, Prev is the entity of the incomplete declaration, used for
655 -- cross-referencing. Otherwise Prev = T.
657 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
658 -- This routine is used to process the actual record type definition (both
659 -- for untagged and tagged records). Def is a record type definition node.
660 -- This procedure analyzes the components in this record type definition.
661 -- Prev_T is the entity for the enclosing record type. It is provided so
662 -- that its Has_Task flag can be set if any of the component have Has_Task
663 -- set. If the declaration is the completion of an incomplete type
664 -- declaration, Prev_T is the original incomplete type, whose full view is
667 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id);
668 -- Subsidiary to Build_Derived_Record_Type. For untagged record types, we
669 -- first create the list of components for the derived type from that of
670 -- the parent by means of Inherit_Components and then build a copy of the
671 -- declaration tree of the parent with the help of the mapping returned by
672 -- Inherit_Components, which will for example be used to validate record
673 -- representation clauses given for the derived type. If the parent type
674 -- is private and has discriminants, the ancestor discriminants used in the
675 -- inheritance are that of the private declaration, whereas the ancestor
676 -- discriminants present in the declaration tree of the parent are that of
677 -- the full declaration; as a consequence, the remapping done during the
678 -- copy will leave the references to the ancestor discriminants unchanged
679 -- in the declaration tree and they need to be fixed up. If the derived
680 -- type has a known discriminant part, then the remapping done during the
681 -- copy will only create references to the girder discriminants and they
682 -- need to be replaced with references to the non-girder discriminants.
684 procedure Set_Fixed_Range
689 -- Build a range node with the given bounds and set it as the Scalar_Range
690 -- of the given fixed-point type entity. Loc is the source location used
691 -- for the constructed range. See body for further details.
693 procedure Set_Scalar_Range_For_Subtype
697 -- This routine is used to set the scalar range field for a subtype given
698 -- Def_Id, the entity for the subtype, and R, the range expression for the
699 -- scalar range. Subt provides the parent subtype to be used to analyze,
700 -- resolve, and check the given range.
702 procedure Set_Default_SSO (T : Entity_Id);
703 -- T is the entity for an array or record being declared. This procedure
704 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
705 -- to the setting of Opt.Default_SSO.
707 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
708 -- Create a new signed integer entity, and apply the constraint to obtain
709 -- the required first named subtype of this type.
711 procedure Set_Stored_Constraint_From_Discriminant_Constraint
713 -- E is some record type. This routine computes E's Stored_Constraint
714 -- from its Discriminant_Constraint.
716 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
717 -- Check that an entity in a list of progenitors is an interface,
718 -- emit error otherwise.
720 -----------------------
721 -- Access_Definition --
722 -----------------------
724 function Access_Definition
725 (Related_Nod : Node_Id;
726 N : Node_Id) return Entity_Id
728 Anon_Type : Entity_Id;
729 Anon_Scope : Entity_Id;
730 Desig_Type : Entity_Id;
731 Enclosing_Prot_Type : Entity_Id := Empty;
734 if Is_Entry (Current_Scope)
735 and then Is_Task_Type (Etype (Scope (Current_Scope)))
737 Error_Msg_N ("task entries cannot have access parameters", N);
741 -- Ada 2005: For an object declaration the corresponding anonymous
742 -- type is declared in the current scope.
744 -- If the access definition is the return type of another access to
745 -- function, scope is the current one, because it is the one of the
746 -- current type declaration, except for the pathological case below.
748 if Nkind_In (Related_Nod, N_Object_Declaration,
749 N_Access_Function_Definition)
751 Anon_Scope := Current_Scope;
753 -- A pathological case: function returning access functions that
754 -- return access functions, etc. Each anonymous access type created
755 -- is in the enclosing scope of the outermost function.
762 while Nkind_In (Par, N_Access_Function_Definition,
768 if Nkind (Par) = N_Function_Specification then
769 Anon_Scope := Scope (Defining_Entity (Par));
773 -- For the anonymous function result case, retrieve the scope of the
774 -- function specification's associated entity rather than using the
775 -- current scope. The current scope will be the function itself if the
776 -- formal part is currently being analyzed, but will be the parent scope
777 -- in the case of a parameterless function, and we always want to use
778 -- the function's parent scope. Finally, if the function is a child
779 -- unit, we must traverse the tree to retrieve the proper entity.
781 elsif Nkind (Related_Nod) = N_Function_Specification
782 and then Nkind (Parent (N)) /= N_Parameter_Specification
784 -- If the current scope is a protected type, the anonymous access
785 -- is associated with one of the protected operations, and must
786 -- be available in the scope that encloses the protected declaration.
787 -- Otherwise the type is in the scope enclosing the subprogram.
789 -- If the function has formals, the return type of a subprogram
790 -- declaration is analyzed in the scope of the subprogram (see
791 -- Process_Formals) and thus the protected type, if present, is
792 -- the scope of the current function scope.
794 if Ekind (Current_Scope) = E_Protected_Type then
795 Enclosing_Prot_Type := Current_Scope;
797 elsif Ekind (Current_Scope) = E_Function
798 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
800 Enclosing_Prot_Type := Scope (Current_Scope);
803 if Present (Enclosing_Prot_Type) then
804 Anon_Scope := Scope (Enclosing_Prot_Type);
807 Anon_Scope := Scope (Defining_Entity (Related_Nod));
810 -- For an access type definition, if the current scope is a child
811 -- unit it is the scope of the type.
813 elsif Is_Compilation_Unit (Current_Scope) then
814 Anon_Scope := Current_Scope;
816 -- For access formals, access components, and access discriminants, the
817 -- scope is that of the enclosing declaration,
820 Anon_Scope := Scope (Current_Scope);
825 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
828 and then Ada_Version >= Ada_2005
830 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
833 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
834 -- the corresponding semantic routine
836 if Present (Access_To_Subprogram_Definition (N)) then
838 -- Compiler runtime units are compiled in Ada 2005 mode when building
839 -- the runtime library but must also be compilable in Ada 95 mode
840 -- (when bootstrapping the compiler).
842 Check_Compiler_Unit ("anonymous access to subprogram", N);
844 Access_Subprogram_Declaration
845 (T_Name => Anon_Type,
846 T_Def => Access_To_Subprogram_Definition (N));
848 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
850 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
852 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
855 Set_Can_Use_Internal_Rep
856 (Anon_Type, not Always_Compatible_Rep_On_Target);
858 -- If the anonymous access is associated with a protected operation,
859 -- create a reference to it after the enclosing protected definition
860 -- because the itype will be used in the subsequent bodies.
862 -- If the anonymous access itself is protected, a full type
863 -- declaratiton will be created for it, so that the equivalent
864 -- record type can be constructed. For further details, see
865 -- Replace_Anonymous_Access_To_Protected-Subprogram.
867 if Ekind (Current_Scope) = E_Protected_Type
868 and then not Protected_Present (Access_To_Subprogram_Definition (N))
870 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
876 Find_Type (Subtype_Mark (N));
877 Desig_Type := Entity (Subtype_Mark (N));
879 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
880 Set_Etype (Anon_Type, Anon_Type);
882 -- Make sure the anonymous access type has size and alignment fields
883 -- set, as required by gigi. This is necessary in the case of the
884 -- Task_Body_Procedure.
886 if not Has_Private_Component (Desig_Type) then
887 Layout_Type (Anon_Type);
890 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
891 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
892 -- the null value is allowed. In Ada 95 the null value is never allowed.
894 if Ada_Version >= Ada_2005 then
895 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
897 Set_Can_Never_Be_Null (Anon_Type, True);
900 -- The anonymous access type is as public as the discriminated type or
901 -- subprogram that defines it. It is imported (for back-end purposes)
902 -- if the designated type is.
904 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
906 -- Ada 2005 (AI-231): Propagate the access-constant attribute
908 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
910 -- The context is either a subprogram declaration, object declaration,
911 -- or an access discriminant, in a private or a full type declaration.
912 -- In the case of a subprogram, if the designated type is incomplete,
913 -- the operation will be a primitive operation of the full type, to be
914 -- updated subsequently. If the type is imported through a limited_with
915 -- clause, the subprogram is not a primitive operation of the type
916 -- (which is declared elsewhere in some other scope).
918 if Ekind (Desig_Type) = E_Incomplete_Type
919 and then not From_Limited_With (Desig_Type)
920 and then Is_Overloadable (Current_Scope)
922 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
923 Set_Has_Delayed_Freeze (Current_Scope);
926 -- If the designated type is limited and class-wide, the object might
927 -- contain tasks, so we create a Master entity for the declaration. This
928 -- must be done before expansion of the full declaration, because the
929 -- declaration may include an expression that is an allocator, whose
930 -- expansion needs the proper Master for the created tasks.
933 and then Nkind (Related_Nod) = N_Object_Declaration
935 if Is_Limited_Record (Desig_Type)
936 and then Is_Class_Wide_Type (Desig_Type)
938 Build_Class_Wide_Master (Anon_Type);
940 -- Similarly, if the type is an anonymous access that designates
941 -- tasks, create a master entity for it in the current context.
943 elsif Has_Task (Desig_Type)
944 and then Comes_From_Source (Related_Nod)
946 Build_Master_Entity (Defining_Identifier (Related_Nod));
947 Build_Master_Renaming (Anon_Type);
951 -- For a private component of a protected type, it is imperative that
952 -- the back-end elaborate the type immediately after the protected
953 -- declaration, because this type will be used in the declarations
954 -- created for the component within each protected body, so we must
955 -- create an itype reference for it now.
957 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
958 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
960 -- Similarly, if the access definition is the return result of a
961 -- function, create an itype reference for it because it will be used
962 -- within the function body. For a regular function that is not a
963 -- compilation unit, insert reference after the declaration. For a
964 -- protected operation, insert it after the enclosing protected type
965 -- declaration. In either case, do not create a reference for a type
966 -- obtained through a limited_with clause, because this would introduce
967 -- semantic dependencies.
969 -- Similarly, do not create a reference if the designated type is a
970 -- generic formal, because no use of it will reach the backend.
972 elsif Nkind (Related_Nod) = N_Function_Specification
973 and then not From_Limited_With (Desig_Type)
974 and then not Is_Generic_Type (Desig_Type)
976 if Present (Enclosing_Prot_Type) then
977 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
979 elsif Is_List_Member (Parent (Related_Nod))
980 and then Nkind (Parent (N)) /= N_Parameter_Specification
982 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
985 -- Finally, create an itype reference for an object declaration of an
986 -- anonymous access type. This is strictly necessary only for deferred
987 -- constants, but in any case will avoid out-of-scope problems in the
990 elsif Nkind (Related_Nod) = N_Object_Declaration then
991 Build_Itype_Reference (Anon_Type, Related_Nod);
995 end Access_Definition;
997 -----------------------------------
998 -- Access_Subprogram_Declaration --
999 -----------------------------------
1001 procedure Access_Subprogram_Declaration
1002 (T_Name : Entity_Id;
1005 procedure Check_For_Premature_Usage (Def : Node_Id);
1006 -- Check that type T_Name is not used, directly or recursively, as a
1007 -- parameter or a return type in Def. Def is either a subtype, an
1008 -- access_definition, or an access_to_subprogram_definition.
1010 -------------------------------
1011 -- Check_For_Premature_Usage --
1012 -------------------------------
1014 procedure Check_For_Premature_Usage (Def : Node_Id) is
1018 -- Check for a subtype mark
1020 if Nkind (Def) in N_Has_Etype then
1021 if Etype (Def) = T_Name then
1023 ("type& cannot be used before end of its declaration", Def);
1026 -- If this is not a subtype, then this is an access_definition
1028 elsif Nkind (Def) = N_Access_Definition then
1029 if Present (Access_To_Subprogram_Definition (Def)) then
1030 Check_For_Premature_Usage
1031 (Access_To_Subprogram_Definition (Def));
1033 Check_For_Premature_Usage (Subtype_Mark (Def));
1036 -- The only cases left are N_Access_Function_Definition and
1037 -- N_Access_Procedure_Definition.
1040 if Present (Parameter_Specifications (Def)) then
1041 Param := First (Parameter_Specifications (Def));
1042 while Present (Param) loop
1043 Check_For_Premature_Usage (Parameter_Type (Param));
1048 if Nkind (Def) = N_Access_Function_Definition then
1049 Check_For_Premature_Usage (Result_Definition (Def));
1052 end Check_For_Premature_Usage;
1056 Formals : constant List_Id := Parameter_Specifications (T_Def);
1059 Desig_Type : constant Entity_Id :=
1060 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1062 -- Start of processing for Access_Subprogram_Declaration
1065 -- Associate the Itype node with the inner full-type declaration or
1066 -- subprogram spec or entry body. This is required to handle nested
1067 -- anonymous declarations. For example:
1070 -- (X : access procedure
1071 -- (Y : access procedure
1074 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1075 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1076 N_Private_Type_Declaration,
1077 N_Private_Extension_Declaration,
1078 N_Procedure_Specification,
1079 N_Function_Specification,
1083 Nkind_In (D_Ityp, N_Object_Declaration,
1084 N_Object_Renaming_Declaration,
1085 N_Formal_Object_Declaration,
1086 N_Formal_Type_Declaration,
1087 N_Task_Type_Declaration,
1088 N_Protected_Type_Declaration))
1090 D_Ityp := Parent (D_Ityp);
1091 pragma Assert (D_Ityp /= Empty);
1094 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1096 if Nkind_In (D_Ityp, N_Procedure_Specification,
1097 N_Function_Specification)
1099 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1101 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1102 N_Object_Declaration,
1103 N_Object_Renaming_Declaration,
1104 N_Formal_Type_Declaration)
1106 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1109 if Nkind (T_Def) = N_Access_Function_Definition then
1110 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1112 Acc : constant Node_Id := Result_Definition (T_Def);
1115 if Present (Access_To_Subprogram_Definition (Acc))
1117 Protected_Present (Access_To_Subprogram_Definition (Acc))
1121 Replace_Anonymous_Access_To_Protected_Subprogram
1127 Access_Definition (T_Def, Result_Definition (T_Def)));
1132 Analyze (Result_Definition (T_Def));
1135 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1138 -- If a null exclusion is imposed on the result type, then
1139 -- create a null-excluding itype (an access subtype) and use
1140 -- it as the function's Etype.
1142 if Is_Access_Type (Typ)
1143 and then Null_Exclusion_In_Return_Present (T_Def)
1145 Set_Etype (Desig_Type,
1146 Create_Null_Excluding_Itype
1148 Related_Nod => T_Def,
1149 Scope_Id => Current_Scope));
1152 if From_Limited_With (Typ) then
1154 -- AI05-151: Incomplete types are allowed in all basic
1155 -- declarations, including access to subprograms.
1157 if Ada_Version >= Ada_2012 then
1162 ("illegal use of incomplete type&",
1163 Result_Definition (T_Def), Typ);
1166 elsif Ekind (Current_Scope) = E_Package
1167 and then In_Private_Part (Current_Scope)
1169 if Ekind (Typ) = E_Incomplete_Type then
1170 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1172 elsif Is_Class_Wide_Type (Typ)
1173 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1176 (Desig_Type, Private_Dependents (Etype (Typ)));
1180 Set_Etype (Desig_Type, Typ);
1185 if not (Is_Type (Etype (Desig_Type))) then
1187 ("expect type in function specification",
1188 Result_Definition (T_Def));
1192 Set_Etype (Desig_Type, Standard_Void_Type);
1195 if Present (Formals) then
1196 Push_Scope (Desig_Type);
1198 -- Some special tests here. These special tests can be removed
1199 -- if and when Itypes always have proper parent pointers to their
1202 -- Special test 1) Link defining_identifier of formals. Required by
1203 -- First_Formal to provide its functionality.
1209 F := First (Formals);
1211 while Present (F) loop
1212 if No (Parent (Defining_Identifier (F))) then
1213 Set_Parent (Defining_Identifier (F), F);
1220 Process_Formals (Formals, Parent (T_Def));
1222 -- Special test 2) End_Scope requires that the parent pointer be set
1223 -- to something reasonable, but Itypes don't have parent pointers. So
1224 -- we set it and then unset it ???
1226 Set_Parent (Desig_Type, T_Name);
1228 Set_Parent (Desig_Type, Empty);
1231 -- Check for premature usage of the type being defined
1233 Check_For_Premature_Usage (T_Def);
1235 -- The return type and/or any parameter type may be incomplete. Mark the
1236 -- subprogram_type as depending on the incomplete type, so that it can
1237 -- be updated when the full type declaration is seen. This only applies
1238 -- to incomplete types declared in some enclosing scope, not to limited
1239 -- views from other packages.
1241 -- Prior to Ada 2012, access to functions can only have in_parameters.
1243 if Present (Formals) then
1244 Formal := First_Formal (Desig_Type);
1245 while Present (Formal) loop
1246 if Ekind (Formal) /= E_In_Parameter
1247 and then Nkind (T_Def) = N_Access_Function_Definition
1248 and then Ada_Version < Ada_2012
1250 Error_Msg_N ("functions can only have IN parameters", Formal);
1253 if Ekind (Etype (Formal)) = E_Incomplete_Type
1254 and then In_Open_Scopes (Scope (Etype (Formal)))
1256 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1257 Set_Has_Delayed_Freeze (Desig_Type);
1260 Next_Formal (Formal);
1264 -- Check whether an indirect call without actuals may be possible. This
1265 -- is used when resolving calls whose result is then indexed.
1267 May_Need_Actuals (Desig_Type);
1269 -- If the return type is incomplete, this is legal as long as the type
1270 -- is declared in the current scope and will be completed in it (rather
1271 -- than being part of limited view).
1273 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1274 and then not Has_Delayed_Freeze (Desig_Type)
1275 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1277 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1278 Set_Has_Delayed_Freeze (Desig_Type);
1281 Check_Delayed_Subprogram (Desig_Type);
1283 if Protected_Present (T_Def) then
1284 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1285 Set_Convention (Desig_Type, Convention_Protected);
1287 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1290 Set_Can_Use_Internal_Rep (T_Name,
1291 not Always_Compatible_Rep_On_Target);
1292 Set_Etype (T_Name, T_Name);
1293 Init_Size_Align (T_Name);
1294 Set_Directly_Designated_Type (T_Name, Desig_Type);
1296 -- If the access_to_subprogram is not declared at the library level,
1297 -- it can only point to subprograms that are at the same or deeper
1298 -- accessibility level. The corresponding subprogram type might
1299 -- require an activation record when compiling for C.
1301 Set_Needs_Activation_Record (Desig_Type,
1302 not Is_Library_Level_Entity (T_Name));
1304 Generate_Reference_To_Formals (T_Name);
1306 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1308 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1310 Check_Restriction (No_Access_Subprograms, T_Def);
1311 end Access_Subprogram_Declaration;
1313 ----------------------------
1314 -- Access_Type_Declaration --
1315 ----------------------------
1317 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1318 P : constant Node_Id := Parent (Def);
1319 S : constant Node_Id := Subtype_Indication (Def);
1321 Full_Desig : Entity_Id;
1324 -- Check for permissible use of incomplete type
1326 if Nkind (S) /= N_Subtype_Indication then
1329 if Present (Entity (S))
1330 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1332 Set_Directly_Designated_Type (T, Entity (S));
1334 -- If the designated type is a limited view, we cannot tell if
1335 -- the full view contains tasks, and there is no way to handle
1336 -- that full view in a client. We create a master entity for the
1337 -- scope, which will be used when a client determines that one
1340 if From_Limited_With (Entity (S))
1341 and then not Is_Class_Wide_Type (Entity (S))
1343 Set_Ekind (T, E_Access_Type);
1344 Build_Master_Entity (T);
1345 Build_Master_Renaming (T);
1349 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1352 -- If the access definition is of the form: ACCESS NOT NULL ..
1353 -- the subtype indication must be of an access type. Create
1354 -- a null-excluding subtype of it.
1356 if Null_Excluding_Subtype (Def) then
1357 if not Is_Access_Type (Entity (S)) then
1358 Error_Msg_N ("null exclusion must apply to access type", Def);
1362 Loc : constant Source_Ptr := Sloc (S);
1364 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1368 Make_Subtype_Declaration (Loc,
1369 Defining_Identifier => Nam,
1370 Subtype_Indication =>
1371 New_Occurrence_Of (Entity (S), Loc));
1372 Set_Null_Exclusion_Present (Decl);
1373 Insert_Before (Parent (Def), Decl);
1375 Set_Entity (S, Nam);
1381 Set_Directly_Designated_Type (T,
1382 Process_Subtype (S, P, T, 'P'));
1385 if All_Present (Def) or Constant_Present (Def) then
1386 Set_Ekind (T, E_General_Access_Type);
1388 Set_Ekind (T, E_Access_Type);
1391 Full_Desig := Designated_Type (T);
1393 if Base_Type (Full_Desig) = T then
1394 Error_Msg_N ("access type cannot designate itself", S);
1396 -- In Ada 2005, the type may have a limited view through some unit in
1397 -- its own context, allowing the following circularity that cannot be
1398 -- detected earlier.
1400 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1403 ("access type cannot designate its own class-wide type", S);
1405 -- Clean up indication of tagged status to prevent cascaded errors
1407 Set_Is_Tagged_Type (T, False);
1410 -- For SPARK, check that the designated type is compatible with
1411 -- respect to volatility with the access type.
1413 if SPARK_Mode /= Off
1414 and then Comes_From_Source (T)
1416 -- ??? UNIMPLEMENTED
1417 -- In the case where the designated type is incomplete at this point,
1418 -- performing this check here is harmless but the check will need to
1419 -- be repeated when the designated type is complete.
1421 -- The preceding call to Comes_From_Source is needed because the
1422 -- FE sometimes introduces implicitly declared access types. See,
1423 -- for example, the expansion of nested_po.ads in OA28-015.
1425 Check_Volatility_Compatibility
1426 (Full_Desig, T, "designated type", "access type",
1427 Srcpos_Bearer => T);
1432 -- If the type has appeared already in a with_type clause, it is frozen
1433 -- and the pointer size is already set. Else, initialize.
1435 if not From_Limited_With (T) then
1436 Init_Size_Align (T);
1439 -- Note that Has_Task is always false, since the access type itself
1440 -- is not a task type. See Einfo for more description on this point.
1441 -- Exactly the same consideration applies to Has_Controlled_Component
1442 -- and to Has_Protected.
1444 Set_Has_Task (T, False);
1445 Set_Has_Protected (T, False);
1446 Set_Has_Timing_Event (T, False);
1447 Set_Has_Controlled_Component (T, False);
1449 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1450 -- problems where an incomplete view of this entity has been previously
1451 -- established by a limited with and an overlaid version of this field
1452 -- (Stored_Constraint) was initialized for the incomplete view.
1454 -- This reset is performed in most cases except where the access type
1455 -- has been created for the purposes of allocating or deallocating a
1456 -- build-in-place object. Such access types have explicitly set pools
1457 -- and finalization masters.
1459 if No (Associated_Storage_Pool (T)) then
1460 Set_Finalization_Master (T, Empty);
1463 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1466 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1467 Set_Is_Access_Constant (T, Constant_Present (Def));
1468 end Access_Type_Declaration;
1470 ----------------------------------
1471 -- Add_Interface_Tag_Components --
1472 ----------------------------------
1474 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1475 Loc : constant Source_Ptr := Sloc (N);
1479 procedure Add_Tag (Iface : Entity_Id);
1480 -- Add tag for one of the progenitor interfaces
1486 procedure Add_Tag (Iface : Entity_Id) is
1493 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1495 -- This is a reasonable place to propagate predicates
1497 if Has_Predicates (Iface) then
1498 Set_Has_Predicates (Typ);
1502 Make_Component_Definition (Loc,
1503 Aliased_Present => True,
1504 Subtype_Indication =>
1505 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1507 Tag := Make_Temporary (Loc, 'V');
1510 Make_Component_Declaration (Loc,
1511 Defining_Identifier => Tag,
1512 Component_Definition => Def);
1514 Analyze_Component_Declaration (Decl);
1516 Set_Analyzed (Decl);
1517 Set_Ekind (Tag, E_Component);
1519 Set_Is_Aliased (Tag);
1520 Set_Is_Independent (Tag);
1521 Set_Related_Type (Tag, Iface);
1522 Init_Component_Location (Tag);
1524 pragma Assert (Is_Frozen (Iface));
1526 Set_DT_Entry_Count (Tag,
1527 DT_Entry_Count (First_Entity (Iface)));
1529 if No (Last_Tag) then
1532 Insert_After (Last_Tag, Decl);
1537 -- If the ancestor has discriminants we need to give special support
1538 -- to store the offset_to_top value of the secondary dispatch tables.
1539 -- For this purpose we add a supplementary component just after the
1540 -- field that contains the tag associated with each secondary DT.
1542 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1544 Make_Component_Definition (Loc,
1545 Subtype_Indication =>
1546 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1548 Offset := Make_Temporary (Loc, 'V');
1551 Make_Component_Declaration (Loc,
1552 Defining_Identifier => Offset,
1553 Component_Definition => Def);
1555 Analyze_Component_Declaration (Decl);
1557 Set_Analyzed (Decl);
1558 Set_Ekind (Offset, E_Component);
1559 Set_Is_Aliased (Offset);
1560 Set_Is_Independent (Offset);
1561 Set_Related_Type (Offset, Iface);
1562 Init_Component_Location (Offset);
1563 Insert_After (Last_Tag, Decl);
1574 -- Start of processing for Add_Interface_Tag_Components
1577 if not RTE_Available (RE_Interface_Tag) then
1579 ("(Ada 2005) interface types not supported by this run-time!",
1584 if Ekind (Typ) /= E_Record_Type
1585 or else (Is_Concurrent_Record_Type (Typ)
1586 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1587 or else (not Is_Concurrent_Record_Type (Typ)
1588 and then No (Interfaces (Typ))
1589 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1594 -- Find the current last tag
1596 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1597 Ext := Record_Extension_Part (Type_Definition (N));
1599 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1600 Ext := Type_Definition (N);
1605 if not (Present (Component_List (Ext))) then
1606 Set_Null_Present (Ext, False);
1608 Set_Component_List (Ext,
1609 Make_Component_List (Loc,
1610 Component_Items => L,
1611 Null_Present => False));
1613 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1614 L := Component_Items
1616 (Record_Extension_Part
1617 (Type_Definition (N))));
1619 L := Component_Items
1621 (Type_Definition (N)));
1624 -- Find the last tag component
1627 while Present (Comp) loop
1628 if Nkind (Comp) = N_Component_Declaration
1629 and then Is_Tag (Defining_Identifier (Comp))
1638 -- At this point L references the list of components and Last_Tag
1639 -- references the current last tag (if any). Now we add the tag
1640 -- corresponding with all the interfaces that are not implemented
1643 if Present (Interfaces (Typ)) then
1644 Elmt := First_Elmt (Interfaces (Typ));
1645 while Present (Elmt) loop
1646 Add_Tag (Node (Elmt));
1650 end Add_Interface_Tag_Components;
1652 -------------------------------------
1653 -- Add_Internal_Interface_Entities --
1654 -------------------------------------
1656 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1659 Iface_Elmt : Elmt_Id;
1660 Iface_Prim : Entity_Id;
1661 Ifaces_List : Elist_Id;
1662 New_Subp : Entity_Id := Empty;
1664 Restore_Scope : Boolean := False;
1667 pragma Assert (Ada_Version >= Ada_2005
1668 and then Is_Record_Type (Tagged_Type)
1669 and then Is_Tagged_Type (Tagged_Type)
1670 and then Has_Interfaces (Tagged_Type)
1671 and then not Is_Interface (Tagged_Type));
1673 -- Ensure that the internal entities are added to the scope of the type
1675 if Scope (Tagged_Type) /= Current_Scope then
1676 Push_Scope (Scope (Tagged_Type));
1677 Restore_Scope := True;
1680 Collect_Interfaces (Tagged_Type, Ifaces_List);
1682 Iface_Elmt := First_Elmt (Ifaces_List);
1683 while Present (Iface_Elmt) loop
1684 Iface := Node (Iface_Elmt);
1686 -- Originally we excluded here from this processing interfaces that
1687 -- are parents of Tagged_Type because their primitives are located
1688 -- in the primary dispatch table (and hence no auxiliary internal
1689 -- entities are required to handle secondary dispatch tables in such
1690 -- case). However, these auxiliary entities are also required to
1691 -- handle derivations of interfaces in formals of generics (see
1692 -- Derive_Subprograms).
1694 Elmt := First_Elmt (Primitive_Operations (Iface));
1695 while Present (Elmt) loop
1696 Iface_Prim := Node (Elmt);
1698 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1700 Find_Primitive_Covering_Interface
1701 (Tagged_Type => Tagged_Type,
1702 Iface_Prim => Iface_Prim);
1704 if No (Prim) and then Serious_Errors_Detected > 0 then
1708 pragma Assert (Present (Prim));
1710 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1711 -- differs from the name of the interface primitive then it is
1712 -- a private primitive inherited from a parent type. In such
1713 -- case, given that Tagged_Type covers the interface, the
1714 -- inherited private primitive becomes visible. For such
1715 -- purpose we add a new entity that renames the inherited
1716 -- private primitive.
1718 if Chars (Prim) /= Chars (Iface_Prim) then
1719 pragma Assert (Has_Suffix (Prim, 'P'));
1721 (New_Subp => New_Subp,
1722 Parent_Subp => Iface_Prim,
1723 Derived_Type => Tagged_Type,
1724 Parent_Type => Iface);
1725 Set_Alias (New_Subp, Prim);
1726 Set_Is_Abstract_Subprogram
1727 (New_Subp, Is_Abstract_Subprogram (Prim));
1731 (New_Subp => New_Subp,
1732 Parent_Subp => Iface_Prim,
1733 Derived_Type => Tagged_Type,
1734 Parent_Type => Iface);
1739 if Is_Inherited_Operation (Prim)
1740 and then Present (Alias (Prim))
1742 Anc := Alias (Prim);
1744 Anc := Overridden_Operation (Prim);
1747 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1748 -- nonconforming preconditions in both an ancestor and
1749 -- a progenitor operation.
1751 -- If the operation is a primitive wrapper it is an explicit
1752 -- (overriding) operqtion and all is fine.
1755 and then Has_Non_Trivial_Precondition (Anc)
1756 and then Has_Non_Trivial_Precondition (Iface_Prim)
1758 if Is_Abstract_Subprogram (Prim)
1760 (Ekind (Prim) = E_Procedure
1761 and then Nkind (Parent (Prim)) =
1762 N_Procedure_Specification
1763 and then Null_Present (Parent (Prim)))
1764 or else Is_Primitive_Wrapper (Prim)
1768 -- The operation is inherited and must be overridden
1770 elsif not Comes_From_Source (Prim) then
1772 ("&inherits non-conforming preconditions and must "
1773 & "be overridden (RM 6.1.1 (10-16)",
1774 Parent (Tagged_Type), Prim);
1779 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1780 -- associated with interface types. These entities are
1781 -- only registered in the list of primitives of its
1782 -- corresponding tagged type because they are only used
1783 -- to fill the contents of the secondary dispatch tables.
1784 -- Therefore they are removed from the homonym chains.
1786 Set_Is_Hidden (New_Subp);
1787 Set_Is_Internal (New_Subp);
1788 Set_Alias (New_Subp, Prim);
1789 Set_Is_Abstract_Subprogram
1790 (New_Subp, Is_Abstract_Subprogram (Prim));
1791 Set_Interface_Alias (New_Subp, Iface_Prim);
1793 -- If the returned type is an interface then propagate it to
1794 -- the returned type. Needed by the thunk to generate the code
1795 -- which displaces "this" to reference the corresponding
1796 -- secondary dispatch table in the returned object.
1798 if Is_Interface (Etype (Iface_Prim)) then
1799 Set_Etype (New_Subp, Etype (Iface_Prim));
1802 -- Internal entities associated with interface types are only
1803 -- registered in the list of primitives of the tagged type.
1804 -- They are only used to fill the contents of the secondary
1805 -- dispatch tables. Therefore they are not needed in the
1808 Remove_Homonym (New_Subp);
1810 -- Hidden entities associated with interfaces must have set
1811 -- the Has_Delay_Freeze attribute to ensure that, in case
1812 -- of locally defined tagged types (or compiling with static
1813 -- dispatch tables generation disabled) the corresponding
1814 -- entry of the secondary dispatch table is filled when such
1815 -- an entity is frozen.
1817 Set_Has_Delayed_Freeze (New_Subp);
1824 Next_Elmt (Iface_Elmt);
1827 if Restore_Scope then
1830 end Add_Internal_Interface_Entities;
1832 -----------------------------------
1833 -- Analyze_Component_Declaration --
1834 -----------------------------------
1836 procedure Analyze_Component_Declaration (N : Node_Id) is
1837 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1838 Id : constant Entity_Id := Defining_Identifier (N);
1839 E : constant Node_Id := Expression (N);
1840 Typ : constant Node_Id :=
1841 Subtype_Indication (Component_Definition (N));
1845 function Contains_POC (Constr : Node_Id) return Boolean;
1846 -- Determines whether a constraint uses the discriminant of a record
1847 -- type thus becoming a per-object constraint (POC).
1849 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1850 -- Typ is the type of the current component, check whether this type is
1851 -- a limited type. Used to validate declaration against that of
1852 -- enclosing record.
1858 function Contains_POC (Constr : Node_Id) return Boolean is
1860 -- Prevent cascaded errors
1862 if Error_Posted (Constr) then
1866 case Nkind (Constr) is
1867 when N_Attribute_Reference =>
1868 return Attribute_Name (Constr) = Name_Access
1869 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1871 when N_Discriminant_Association =>
1872 return Denotes_Discriminant (Expression (Constr));
1874 when N_Identifier =>
1875 return Denotes_Discriminant (Constr);
1877 when N_Index_Or_Discriminant_Constraint =>
1882 IDC := First (Constraints (Constr));
1883 while Present (IDC) loop
1885 -- One per-object constraint is sufficient
1887 if Contains_POC (IDC) then
1898 return Denotes_Discriminant (Low_Bound (Constr))
1900 Denotes_Discriminant (High_Bound (Constr));
1902 when N_Range_Constraint =>
1903 return Denotes_Discriminant (Range_Expression (Constr));
1910 ----------------------
1911 -- Is_Known_Limited --
1912 ----------------------
1914 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1915 P : constant Entity_Id := Etype (Typ);
1916 R : constant Entity_Id := Root_Type (Typ);
1919 if Is_Limited_Record (Typ) then
1922 -- If the root type is limited (and not a limited interface) so is
1923 -- the current type.
1925 elsif Is_Limited_Record (R)
1926 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1930 -- Else the type may have a limited interface progenitor, but a
1931 -- limited record parent that is not an interface.
1934 and then Is_Limited_Record (P)
1935 and then not Is_Interface (P)
1942 end Is_Known_Limited;
1944 -- Start of processing for Analyze_Component_Declaration
1947 Generate_Definition (Id);
1950 if Present (Typ) then
1951 T := Find_Type_Of_Object
1952 (Subtype_Indication (Component_Definition (N)), N);
1954 -- Ada 2005 (AI-230): Access Definition case
1957 pragma Assert (Present
1958 (Access_Definition (Component_Definition (N))));
1960 T := Access_Definition
1962 N => Access_Definition (Component_Definition (N)));
1963 Set_Is_Local_Anonymous_Access (T);
1965 -- Ada 2005 (AI-254)
1967 if Present (Access_To_Subprogram_Definition
1968 (Access_Definition (Component_Definition (N))))
1969 and then Protected_Present (Access_To_Subprogram_Definition
1971 (Component_Definition (N))))
1973 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1977 -- If the subtype is a constrained subtype of the enclosing record,
1978 -- (which must have a partial view) the back-end does not properly
1979 -- handle the recursion. Rewrite the component declaration with an
1980 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1981 -- the tree directly because side effects have already been removed from
1982 -- discriminant constraints.
1984 if Ekind (T) = E_Access_Subtype
1985 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1986 and then Comes_From_Source (T)
1987 and then Nkind (Parent (T)) = N_Subtype_Declaration
1988 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1991 (Subtype_Indication (Component_Definition (N)),
1992 New_Copy_Tree (Subtype_Indication (Parent (T))));
1993 T := Find_Type_Of_Object
1994 (Subtype_Indication (Component_Definition (N)), N);
1997 -- If the component declaration includes a default expression, then we
1998 -- check that the component is not of a limited type (RM 3.7(5)),
1999 -- and do the special preanalysis of the expression (see section on
2000 -- "Handling of Default and Per-Object Expressions" in the spec of
2004 Preanalyze_Default_Expression (E, T);
2005 Check_Initialization (T, E);
2007 if Ada_Version >= Ada_2005
2008 and then Ekind (T) = E_Anonymous_Access_Type
2009 and then Etype (E) /= Any_Type
2011 -- Check RM 3.9.2(9): "if the expected type for an expression is
2012 -- an anonymous access-to-specific tagged type, then the object
2013 -- designated by the expression shall not be dynamically tagged
2014 -- unless it is a controlling operand in a call on a dispatching
2017 if Is_Tagged_Type (Directly_Designated_Type (T))
2019 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2021 Ekind (Directly_Designated_Type (Etype (E))) =
2025 ("access to specific tagged type required (RM 3.9.2(9))", E);
2028 -- (Ada 2005: AI-230): Accessibility check for anonymous
2031 if Type_Access_Level (Etype (E)) >
2032 Deepest_Type_Access_Level (T)
2035 ("expression has deeper access level than component " &
2036 "(RM 3.10.2 (12.2))", E);
2039 -- The initialization expression is a reference to an access
2040 -- discriminant. The type of the discriminant is always deeper
2041 -- than any access type.
2043 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2044 and then Is_Entity_Name (E)
2045 and then Ekind (Entity (E)) = E_In_Parameter
2046 and then Present (Discriminal_Link (Entity (E)))
2049 ("discriminant has deeper accessibility level than target",
2055 -- Avoid reporting spurious errors if the component is initialized with
2056 -- a raise expression (which is legal in any expression context)
2060 (Nkind (E) = N_Raise_Expression
2061 or else (Nkind (E) = N_Qualified_Expression
2062 and then Nkind (Expression (E)) = N_Raise_Expression))
2066 -- The parent type may be a private view with unknown discriminants,
2067 -- and thus unconstrained. Regular components must be constrained.
2069 elsif not Is_Definite_Subtype (T)
2070 and then Chars (Id) /= Name_uParent
2072 if Is_Class_Wide_Type (T) then
2074 ("class-wide subtype with unknown discriminants" &
2075 " in component declaration",
2076 Subtype_Indication (Component_Definition (N)));
2079 ("unconstrained subtype in component declaration",
2080 Subtype_Indication (Component_Definition (N)));
2083 -- Components cannot be abstract, except for the special case of
2084 -- the _Parent field (case of extending an abstract tagged type)
2086 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2087 Error_Msg_N ("type of a component cannot be abstract", N);
2092 if Aliased_Present (Component_Definition (N)) then
2093 Set_Is_Aliased (Id);
2095 -- AI12-001: All aliased objects are considered to be specified as
2096 -- independently addressable (RM C.6(8.1/4)).
2098 Set_Is_Independent (Id);
2101 -- The component declaration may have a per-object constraint, set
2102 -- the appropriate flag in the defining identifier of the subtype.
2104 if Present (Subtype_Indication (Component_Definition (N))) then
2106 Sindic : constant Node_Id :=
2107 Subtype_Indication (Component_Definition (N));
2109 if Nkind (Sindic) = N_Subtype_Indication
2110 and then Present (Constraint (Sindic))
2111 and then Contains_POC (Constraint (Sindic))
2113 Set_Has_Per_Object_Constraint (Id);
2118 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2119 -- out some static checks.
2121 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2122 Null_Exclusion_Static_Checks (N);
2125 -- If this component is private (or depends on a private type), flag the
2126 -- record type to indicate that some operations are not available.
2128 P := Private_Component (T);
2132 -- Check for circular definitions
2134 if P = Any_Type then
2135 Set_Etype (Id, Any_Type);
2137 -- There is a gap in the visibility of operations only if the
2138 -- component type is not defined in the scope of the record type.
2140 elsif Scope (P) = Scope (Current_Scope) then
2143 elsif Is_Limited_Type (P) then
2144 Set_Is_Limited_Composite (Current_Scope);
2147 Set_Is_Private_Composite (Current_Scope);
2152 and then Is_Limited_Type (T)
2153 and then Chars (Id) /= Name_uParent
2154 and then Is_Tagged_Type (Current_Scope)
2156 if Is_Derived_Type (Current_Scope)
2157 and then not Is_Known_Limited (Current_Scope)
2160 ("extension of nonlimited type cannot have limited components",
2163 if Is_Interface (Root_Type (Current_Scope)) then
2165 ("\limitedness is not inherited from limited interface", N);
2166 Error_Msg_N ("\add LIMITED to type indication", N);
2169 Explain_Limited_Type (T, N);
2170 Set_Etype (Id, Any_Type);
2171 Set_Is_Limited_Composite (Current_Scope, False);
2173 elsif not Is_Derived_Type (Current_Scope)
2174 and then not Is_Limited_Record (Current_Scope)
2175 and then not Is_Concurrent_Type (Current_Scope)
2178 ("nonlimited tagged type cannot have limited components", N);
2179 Explain_Limited_Type (T, N);
2180 Set_Etype (Id, Any_Type);
2181 Set_Is_Limited_Composite (Current_Scope, False);
2185 -- If the component is an unconstrained task or protected type with
2186 -- discriminants, the component and the enclosing record are limited
2187 -- and the component is constrained by its default values. Compute
2188 -- its actual subtype, else it may be allocated the maximum size by
2189 -- the backend, and possibly overflow.
2191 if Is_Concurrent_Type (T)
2192 and then not Is_Constrained (T)
2193 and then Has_Discriminants (T)
2194 and then not Has_Discriminants (Current_Scope)
2197 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2200 Set_Etype (Id, Act_T);
2202 -- Rewrite component definition to use the constrained subtype
2204 Rewrite (Component_Definition (N),
2205 Make_Component_Definition (Loc,
2206 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2210 Set_Original_Record_Component (Id, Id);
2212 if Has_Aspects (N) then
2213 Analyze_Aspect_Specifications (N, Id);
2216 Analyze_Dimension (N);
2217 end Analyze_Component_Declaration;
2219 --------------------------
2220 -- Analyze_Declarations --
2221 --------------------------
2223 procedure Analyze_Declarations (L : List_Id) is
2226 procedure Adjust_Decl;
2227 -- Adjust Decl not to include implicit label declarations, since these
2228 -- have strange Sloc values that result in elaboration check problems.
2229 -- (They have the sloc of the label as found in the source, and that
2230 -- is ahead of the current declarative part).
2232 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2233 -- Create the subprogram bodies which verify the run-time semantics of
2234 -- the pragmas listed below for each elibigle type found in declarative
2235 -- list Decls. The pragmas are:
2237 -- Default_Initial_Condition
2241 -- Context denotes the owner of the declarative list.
2243 procedure Check_Entry_Contracts;
2244 -- Perform a preanalysis of the pre- and postconditions of an entry
2245 -- declaration. This must be done before full resolution and creation
2246 -- of the parameter block, etc. to catch illegal uses within the
2247 -- contract expression. Full analysis of the expression is done when
2248 -- the contract is processed.
2250 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2251 -- Check if a nested package has entities within it that rely on library
2252 -- level private types where the full view has not been completed for
2253 -- the purposes of checking if it is acceptable to freeze an expression
2254 -- function at the point of declaration.
2256 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2257 -- Determine whether Body_Decl denotes the body of a late controlled
2258 -- primitive (either Initialize, Adjust or Finalize). If this is the
2259 -- case, add a proper spec if the body lacks one. The spec is inserted
2260 -- before Body_Decl and immediately analyzed.
2262 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2263 -- Spec_Id is the entity of a package that may define abstract states,
2264 -- and in the case of a child unit, whose ancestors may define abstract
2265 -- states. If the states have partial visible refinement, remove the
2266 -- partial visibility of each constituent at the end of the package
2267 -- spec and body declarations.
2269 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2270 -- Spec_Id is the entity of a package that may define abstract states.
2271 -- If the states have visible refinement, remove the visibility of each
2272 -- constituent at the end of the package body declaration.
2274 procedure Resolve_Aspects;
2275 -- Utility to resolve the expressions of aspects at the end of a list of
2276 -- declarations, or before a declaration that freezes previous entities,
2277 -- such as in a subprogram body.
2283 procedure Adjust_Decl is
2285 while Present (Prev (Decl))
2286 and then Nkind (Decl) = N_Implicit_Label_Declaration
2292 ----------------------------
2293 -- Build_Assertion_Bodies --
2294 ----------------------------
2296 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2297 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2298 -- Create the subprogram bodies which verify the run-time semantics
2299 -- of the pragmas listed below for type Typ. The pragmas are:
2301 -- Default_Initial_Condition
2305 -------------------------------------
2306 -- Build_Assertion_Bodies_For_Type --
2307 -------------------------------------
2309 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2311 -- Preanalyze and resolve the Default_Initial_Condition assertion
2312 -- expression at the end of the declarations to catch any errors.
2314 if Has_DIC (Typ) then
2315 Build_DIC_Procedure_Body (Typ);
2318 if Nkind (Context) = N_Package_Specification then
2320 -- Preanalyze and resolve the class-wide invariants of an
2321 -- interface at the end of whichever declarative part has the
2322 -- interface type. Note that an interface may be declared in
2323 -- any non-package declarative part, but reaching the end of
2324 -- such a declarative part will always freeze the type and
2325 -- generate the invariant procedure (see Freeze_Type).
2327 if Is_Interface (Typ) then
2329 -- Interfaces are treated as the partial view of a private
2330 -- type, in order to achieve uniformity with the general
2331 -- case. As a result, an interface receives only a "partial"
2332 -- invariant procedure, which is never called.
2334 if Has_Own_Invariants (Typ) then
2335 Build_Invariant_Procedure_Body
2337 Partial_Invariant => True);
2340 -- Preanalyze and resolve the invariants of a private type
2341 -- at the end of the visible declarations to catch potential
2342 -- errors. Inherited class-wide invariants are not included
2343 -- because they have already been resolved.
2345 elsif Decls = Visible_Declarations (Context)
2346 and then Ekind_In (Typ, E_Limited_Private_Type,
2348 E_Record_Type_With_Private)
2349 and then Has_Own_Invariants (Typ)
2351 Build_Invariant_Procedure_Body
2353 Partial_Invariant => True);
2355 -- Preanalyze and resolve the invariants of a private type's
2356 -- full view at the end of the private declarations to catch
2357 -- potential errors.
2359 elsif Decls = Private_Declarations (Context)
2360 and then (not Is_Private_Type (Typ)
2361 or else Present (Underlying_Full_View (Typ)))
2362 and then Has_Private_Declaration (Typ)
2363 and then Has_Invariants (Typ)
2365 Build_Invariant_Procedure_Body (Typ);
2368 end Build_Assertion_Bodies_For_Type;
2373 Decl_Id : Entity_Id;
2375 -- Start of processing for Build_Assertion_Bodies
2378 Decl := First (Decls);
2379 while Present (Decl) loop
2380 if Is_Declaration (Decl) then
2381 Decl_Id := Defining_Entity (Decl);
2383 if Is_Type (Decl_Id) then
2384 Build_Assertion_Bodies_For_Type (Decl_Id);
2390 end Build_Assertion_Bodies;
2392 ---------------------------
2393 -- Check_Entry_Contracts --
2394 ---------------------------
2396 procedure Check_Entry_Contracts is
2402 Ent := First_Entity (Current_Scope);
2403 while Present (Ent) loop
2405 -- This only concerns entries with pre/postconditions
2407 if Ekind (Ent) = E_Entry
2408 and then Present (Contract (Ent))
2409 and then Present (Pre_Post_Conditions (Contract (Ent)))
2411 ASN := Pre_Post_Conditions (Contract (Ent));
2413 Install_Formals (Ent);
2415 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2416 -- is performed on a copy of the pragma expression, to prevent
2417 -- modifying the original expression.
2419 while Present (ASN) loop
2420 if Nkind (ASN) = N_Pragma then
2424 (First (Pragma_Argument_Associations (ASN))));
2425 Set_Parent (Exp, ASN);
2427 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2430 ASN := Next_Pragma (ASN);
2438 end Check_Entry_Contracts;
2440 ----------------------------------
2441 -- Contains_Lib_Incomplete_Type --
2442 ----------------------------------
2444 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2448 -- Avoid looking through scopes that do not meet the precondition of
2449 -- Pkg not being within a library unit spec.
2451 if not Is_Compilation_Unit (Pkg)
2452 and then not Is_Generic_Instance (Pkg)
2453 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2455 -- Loop through all entities in the current scope to identify
2456 -- an entity that depends on a private type.
2458 Curr := First_Entity (Pkg);
2460 if Nkind (Curr) in N_Entity
2461 and then Depends_On_Private (Curr)
2466 exit when Last_Entity (Current_Scope) = Curr;
2472 end Contains_Lib_Incomplete_Type;
2474 --------------------------------------
2475 -- Handle_Late_Controlled_Primitive --
2476 --------------------------------------
2478 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2479 Body_Spec : constant Node_Id := Specification (Body_Decl);
2480 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2481 Loc : constant Source_Ptr := Sloc (Body_Id);
2482 Params : constant List_Id :=
2483 Parameter_Specifications (Body_Spec);
2485 Spec_Id : Entity_Id;
2489 -- Consider only procedure bodies whose name matches one of the three
2490 -- controlled primitives.
2492 if Nkind (Body_Spec) /= N_Procedure_Specification
2493 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2499 -- A controlled primitive must have exactly one formal which is not
2500 -- an anonymous access type.
2502 elsif List_Length (Params) /= 1 then
2506 Typ := Parameter_Type (First (Params));
2508 if Nkind (Typ) = N_Access_Definition then
2514 -- The type of the formal must be derived from [Limited_]Controlled
2516 if not Is_Controlled (Entity (Typ)) then
2520 -- Check whether a specification exists for this body. We do not
2521 -- analyze the spec of the body in full, because it will be analyzed
2522 -- again when the body is properly analyzed, and we cannot create
2523 -- duplicate entries in the formals chain. We look for an explicit
2524 -- specification because the body may be an overriding operation and
2525 -- an inherited spec may be present.
2527 Spec_Id := Current_Entity (Body_Id);
2529 while Present (Spec_Id) loop
2530 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2531 and then Scope (Spec_Id) = Current_Scope
2532 and then Present (First_Formal (Spec_Id))
2533 and then No (Next_Formal (First_Formal (Spec_Id)))
2534 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2535 and then Comes_From_Source (Spec_Id)
2540 Spec_Id := Homonym (Spec_Id);
2543 -- At this point the body is known to be a late controlled primitive.
2544 -- Generate a matching spec and insert it before the body. Note the
2545 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2546 -- tree in this case.
2548 Spec := Copy_Separate_Tree (Body_Spec);
2550 -- Ensure that the subprogram declaration does not inherit the null
2551 -- indicator from the body as we now have a proper spec/body pair.
2553 Set_Null_Present (Spec, False);
2555 -- Ensure that the freeze node is inserted after the declaration of
2556 -- the primitive since its expansion will freeze the primitive.
2558 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2560 Insert_Before_And_Analyze (Body_Decl, Decl);
2561 end Handle_Late_Controlled_Primitive;
2563 ----------------------------------------
2564 -- Remove_Partial_Visible_Refinements --
2565 ----------------------------------------
2567 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2568 State_Elmt : Elmt_Id;
2570 if Present (Abstract_States (Spec_Id)) then
2571 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2572 while Present (State_Elmt) loop
2573 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2574 Next_Elmt (State_Elmt);
2578 -- For a child unit, also hide the partial state refinement from
2579 -- ancestor packages.
2581 if Is_Child_Unit (Spec_Id) then
2582 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2584 end Remove_Partial_Visible_Refinements;
2586 --------------------------------
2587 -- Remove_Visible_Refinements --
2588 --------------------------------
2590 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2591 State_Elmt : Elmt_Id;
2593 if Present (Abstract_States (Spec_Id)) then
2594 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2595 while Present (State_Elmt) loop
2596 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2597 Next_Elmt (State_Elmt);
2600 end Remove_Visible_Refinements;
2602 ---------------------
2603 -- Resolve_Aspects --
2604 ---------------------
2606 procedure Resolve_Aspects is
2610 E := First_Entity (Current_Scope);
2611 while Present (E) loop
2612 Resolve_Aspect_Expressions (E);
2615 end Resolve_Aspects;
2619 Context : Node_Id := Empty;
2620 Ctrl_Typ : Entity_Id := Empty;
2621 Freeze_From : Entity_Id := Empty;
2622 Next_Decl : Node_Id;
2624 -- Start of processing for Analyze_Declarations
2628 while Present (Decl) loop
2630 -- Complete analysis of declaration
2633 Next_Decl := Next (Decl);
2635 if No (Freeze_From) then
2636 Freeze_From := First_Entity (Current_Scope);
2639 -- Remember if the declaration we just processed is the full type
2640 -- declaration of a controlled type (to handle late overriding of
2641 -- initialize, adjust or finalize).
2643 if Nkind (Decl) = N_Full_Type_Declaration
2644 and then Is_Controlled (Defining_Identifier (Decl))
2646 Ctrl_Typ := Defining_Identifier (Decl);
2649 -- At the end of a declarative part, freeze remaining entities
2650 -- declared in it. The end of the visible declarations of package
2651 -- specification is not the end of a declarative part if private
2652 -- declarations are present. The end of a package declaration is a
2653 -- freezing point only if it a library package. A task definition or
2654 -- protected type definition is not a freeze point either. Finally,
2655 -- we do not freeze entities in generic scopes, because there is no
2656 -- code generated for them and freeze nodes will be generated for
2659 -- The end of a package instantiation is not a freeze point, but
2660 -- for now we make it one, because the generic body is inserted
2661 -- (currently) immediately after. Generic instantiations will not
2662 -- be a freeze point once delayed freezing of bodies is implemented.
2663 -- (This is needed in any case for early instantiations ???).
2665 if No (Next_Decl) then
2666 if Nkind (Parent (L)) = N_Component_List then
2669 elsif Nkind_In (Parent (L), N_Protected_Definition,
2672 Check_Entry_Contracts;
2674 elsif Nkind (Parent (L)) /= N_Package_Specification then
2675 if Nkind (Parent (L)) = N_Package_Body then
2676 Freeze_From := First_Entity (Current_Scope);
2679 -- There may have been several freezing points previously,
2680 -- for example object declarations or subprogram bodies, but
2681 -- at the end of a declarative part we check freezing from
2682 -- the beginning, even though entities may already be frozen,
2683 -- in order to perform visibility checks on delayed aspects.
2687 -- If the current scope is a generic subprogram body. Skip the
2688 -- generic formal parameters that are not frozen here.
2690 if Is_Subprogram (Current_Scope)
2691 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2692 N_Generic_Subprogram_Declaration
2693 and then Present (First_Entity (Current_Scope))
2695 while Is_Generic_Formal (Freeze_From) loop
2696 Next_Entity (Freeze_From);
2699 Freeze_All (Freeze_From, Decl);
2700 Freeze_From := Last_Entity (Current_Scope);
2703 -- For declarations in a subprogram body there is no issue
2704 -- with name resolution in aspect specifications.
2706 Freeze_All (First_Entity (Current_Scope), Decl);
2707 Freeze_From := Last_Entity (Current_Scope);
2710 -- Current scope is a package specification
2712 elsif Scope (Current_Scope) /= Standard_Standard
2713 and then not Is_Child_Unit (Current_Scope)
2714 and then No (Generic_Parent (Parent (L)))
2716 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2717 -- resolved at the end of the immediately enclosing declaration
2718 -- list (AI05-0183-1).
2722 elsif L /= Visible_Declarations (Parent (L))
2723 or else No (Private_Declarations (Parent (L)))
2724 or else Is_Empty_List (Private_Declarations (Parent (L)))
2728 -- End of a package declaration
2730 -- This is a freeze point because it is the end of a
2731 -- compilation unit.
2733 Freeze_All (First_Entity (Current_Scope), Decl);
2734 Freeze_From := Last_Entity (Current_Scope);
2736 -- At the end of the visible declarations the expressions in
2737 -- aspects of all entities declared so far must be resolved.
2738 -- The entities themselves might be frozen later, and the
2739 -- generated pragmas and attribute definition clauses analyzed
2740 -- in full at that point, but name resolution must take place
2742 -- In addition to being the proper semantics, this is mandatory
2743 -- within generic units, because global name capture requires
2744 -- those expressions to be analyzed, given that the generated
2745 -- pragmas do not appear in the original generic tree.
2747 elsif Serious_Errors_Detected = 0 then
2751 -- If next node is a body then freeze all types before the body.
2752 -- An exception occurs for some expander-generated bodies. If these
2753 -- are generated at places where in general language rules would not
2754 -- allow a freeze point, then we assume that the expander has
2755 -- explicitly checked that all required types are properly frozen,
2756 -- and we do not cause general freezing here. This special circuit
2757 -- is used when the encountered body is marked as having already
2760 -- In all other cases (bodies that come from source, and expander
2761 -- generated bodies that have not been analyzed yet), freeze all
2762 -- types now. Note that in the latter case, the expander must take
2763 -- care to attach the bodies at a proper place in the tree so as to
2764 -- not cause unwanted freezing at that point.
2766 -- It is also necessary to check for a case where both an expression
2767 -- function is used and the current scope depends on an incomplete
2768 -- private type from a library unit, otherwise premature freezing of
2769 -- the private type will occur.
2771 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2772 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2773 or else not Was_Expression_Function (Next_Decl))
2774 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2775 and then not Contains_Lib_Incomplete_Type
2778 -- When a controlled type is frozen, the expander generates stream
2779 -- and controlled-type support routines. If the freeze is caused
2780 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2781 -- expander will end up using the wrong version of these routines,
2782 -- as the body has not been processed yet. To remedy this, detect
2783 -- a late controlled primitive and create a proper spec for it.
2784 -- This ensures that the primitive will override its inherited
2785 -- counterpart before the freeze takes place.
2787 -- If the declaration we just processed is a body, do not attempt
2788 -- to examine Next_Decl as the late primitive idiom can only apply
2789 -- to the first encountered body.
2791 -- ??? A cleaner approach may be possible and/or this solution
2792 -- could be extended to general-purpose late primitives, TBD.
2794 if Present (Ctrl_Typ) then
2796 -- No need to continue searching for late body overriding if
2797 -- the controlled type is already frozen.
2799 if Is_Frozen (Ctrl_Typ) then
2802 elsif Nkind (Next_Decl) = N_Subprogram_Body then
2803 Handle_Late_Controlled_Primitive (Next_Decl);
2809 -- The generated body of an expression function does not freeze,
2810 -- unless it is a completion, in which case only the expression
2811 -- itself freezes. This is handled when the body itself is
2812 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2814 Freeze_All (Freeze_From, Decl);
2815 Freeze_From := Last_Entity (Current_Scope);
2821 -- Post-freezing actions
2824 Context := Parent (L);
2826 -- Certain contract annotations have forward visibility semantics and
2827 -- must be analyzed after all declarative items have been processed.
2828 -- This timing ensures that entities referenced by such contracts are
2831 -- Analyze the contract of an immediately enclosing package spec or
2832 -- body first because other contracts may depend on its information.
2834 if Nkind (Context) = N_Package_Body then
2835 Analyze_Package_Body_Contract (Defining_Entity (Context));
2837 elsif Nkind (Context) = N_Package_Specification then
2838 Analyze_Package_Contract (Defining_Entity (Context));
2841 -- Analyze the contracts of various constructs in the declarative
2844 Analyze_Contracts (L);
2846 if Nkind (Context) = N_Package_Body then
2848 -- Ensure that all abstract states and objects declared in the
2849 -- state space of a package body are utilized as constituents.
2851 Check_Unused_Body_States (Defining_Entity (Context));
2853 -- State refinements are visible up to the end of the package body
2854 -- declarations. Hide the state refinements from visibility to
2855 -- restore the original state conditions.
2857 Remove_Visible_Refinements (Corresponding_Spec (Context));
2858 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2860 elsif Nkind (Context) = N_Package_Specification then
2862 -- Partial state refinements are visible up to the end of the
2863 -- package spec declarations. Hide the partial state refinements
2864 -- from visibility to restore the original state conditions.
2866 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2869 -- Verify that all abstract states found in any package declared in
2870 -- the input declarative list have proper refinements. The check is
2871 -- performed only when the context denotes a block, entry, package,
2872 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2874 Check_State_Refinements (Context);
2876 -- Create the subprogram bodies which verify the run-time semantics
2877 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2878 -- types within the current declarative list. This ensures that all
2879 -- assertion expressions are preanalyzed and resolved at the end of
2880 -- the declarative part. Note that the resolution happens even when
2881 -- freezing does not take place.
2883 Build_Assertion_Bodies (L, Context);
2885 end Analyze_Declarations;
2887 -----------------------------------
2888 -- Analyze_Full_Type_Declaration --
2889 -----------------------------------
2891 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2892 Def : constant Node_Id := Type_Definition (N);
2893 Def_Id : constant Entity_Id := Defining_Identifier (N);
2897 Is_Remote : constant Boolean :=
2898 (Is_Remote_Types (Current_Scope)
2899 or else Is_Remote_Call_Interface (Current_Scope))
2900 and then not (In_Private_Part (Current_Scope)
2901 or else In_Package_Body (Current_Scope));
2903 procedure Check_Nonoverridable_Aspects;
2904 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2905 -- be overridden, and can only be confirmed on derivation.
2907 procedure Check_Ops_From_Incomplete_Type;
2908 -- If there is a tagged incomplete partial view of the type, traverse
2909 -- the primitives of the incomplete view and change the type of any
2910 -- controlling formals and result to indicate the full view. The
2911 -- primitives will be added to the full type's primitive operations
2912 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2913 -- is called from Process_Incomplete_Dependents).
2915 ----------------------------------
2916 -- Check_Nonoverridable_Aspects --
2917 ----------------------------------
2919 procedure Check_Nonoverridable_Aspects is
2920 function Get_Aspect_Spec
2922 Aspect_Name : Name_Id) return Node_Id;
2923 -- Check whether a list of aspect specifications includes an entry
2924 -- for a specific aspect. The list is either that of a partial or
2927 ---------------------
2928 -- Get_Aspect_Spec --
2929 ---------------------
2931 function Get_Aspect_Spec
2933 Aspect_Name : Name_Id) return Node_Id
2938 Spec := First (Specs);
2939 while Present (Spec) loop
2940 if Chars (Identifier (Spec)) = Aspect_Name then
2947 end Get_Aspect_Spec;
2951 Prev_Aspects : constant List_Id :=
2952 Aspect_Specifications (Parent (Def_Id));
2953 Par_Type : Entity_Id;
2954 Prev_Aspect : Node_Id;
2956 -- Start of processing for Check_Nonoverridable_Aspects
2959 -- Get parent type of derived type. Note that Prev is the entity in
2960 -- the partial declaration, but its contents are now those of full
2961 -- view, while Def_Id reflects the partial view.
2963 if Is_Private_Type (Def_Id) then
2964 Par_Type := Etype (Full_View (Def_Id));
2966 Par_Type := Etype (Def_Id);
2969 -- If there is an inherited Implicit_Dereference, verify that it is
2970 -- made explicit in the partial view.
2972 if Has_Discriminants (Base_Type (Par_Type))
2973 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2974 and then Present (Discriminant_Specifications (Parent (Prev)))
2975 and then Present (Get_Reference_Discriminant (Par_Type))
2978 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2982 (Discriminant_Specifications
2983 (Original_Node (Parent (Prev))))
2986 ("type does not inherit implicit dereference", Prev);
2989 -- If one of the views has the aspect specified, verify that it
2990 -- is consistent with that of the parent.
2993 Cur_Discr : constant Entity_Id :=
2994 Get_Reference_Discriminant (Prev);
2995 Par_Discr : constant Entity_Id :=
2996 Get_Reference_Discriminant (Par_Type);
2999 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3001 ("aspect inconsistent with that of parent", N);
3004 -- Check that specification in partial view matches the
3005 -- inherited aspect. Compare names directly because aspect
3006 -- expression may not be analyzed.
3008 if Present (Prev_Aspect)
3009 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3010 and then Chars (Expression (Prev_Aspect)) /=
3014 ("aspect inconsistent with that of parent", N);
3020 -- TBD : other nonoverridable aspects.
3021 end Check_Nonoverridable_Aspects;
3023 ------------------------------------
3024 -- Check_Ops_From_Incomplete_Type --
3025 ------------------------------------
3027 procedure Check_Ops_From_Incomplete_Type is
3034 and then Ekind (Prev) = E_Incomplete_Type
3035 and then Is_Tagged_Type (Prev)
3036 and then Is_Tagged_Type (T)
3038 Elmt := First_Elmt (Primitive_Operations (Prev));
3039 while Present (Elmt) loop
3042 Formal := First_Formal (Op);
3043 while Present (Formal) loop
3044 if Etype (Formal) = Prev then
3045 Set_Etype (Formal, T);
3048 Next_Formal (Formal);
3051 if Etype (Op) = Prev then
3058 end Check_Ops_From_Incomplete_Type;
3060 -- Start of processing for Analyze_Full_Type_Declaration
3063 Prev := Find_Type_Name (N);
3065 -- The full view, if present, now points to the current type. If there
3066 -- is an incomplete partial view, set a link to it, to simplify the
3067 -- retrieval of primitive operations of the type.
3069 -- Ada 2005 (AI-50217): If the type was previously decorated when
3070 -- imported through a LIMITED WITH clause, it appears as incomplete
3071 -- but has no full view.
3073 if Ekind (Prev) = E_Incomplete_Type
3074 and then Present (Full_View (Prev))
3076 T := Full_View (Prev);
3077 Set_Incomplete_View (N, Parent (Prev));
3082 Set_Is_Pure (T, Is_Pure (Current_Scope));
3084 -- We set the flag Is_First_Subtype here. It is needed to set the
3085 -- corresponding flag for the Implicit class-wide-type created
3086 -- during tagged types processing.
3088 Set_Is_First_Subtype (T, True);
3090 -- Only composite types other than array types are allowed to have
3095 -- For derived types, the rule will be checked once we've figured
3096 -- out the parent type.
3098 when N_Derived_Type_Definition =>
3101 -- For record types, discriminants are allowed.
3103 when N_Record_Definition =>
3107 if Present (Discriminant_Specifications (N)) then
3109 ("elementary or array type cannot have discriminants",
3111 (First (Discriminant_Specifications (N))));
3115 -- Elaborate the type definition according to kind, and generate
3116 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3117 -- already done (this happens during the reanalysis that follows a call
3118 -- to the high level optimizer).
3120 if not Analyzed (T) then
3123 -- Set the SPARK mode from the current context
3125 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3126 Set_SPARK_Pragma_Inherited (T);
3129 when N_Access_To_Subprogram_Definition =>
3130 Access_Subprogram_Declaration (T, Def);
3132 -- If this is a remote access to subprogram, we must create the
3133 -- equivalent fat pointer type, and related subprograms.
3136 Process_Remote_AST_Declaration (N);
3139 -- Validate categorization rule against access type declaration
3140 -- usually a violation in Pure unit, Shared_Passive unit.
3142 Validate_Access_Type_Declaration (T, N);
3144 -- If the type has contracts, we create the corresponding
3145 -- wrapper at once, before analyzing the aspect specifications,
3146 -- so that pre/postconditions can be handled directly on the
3147 -- generated wrapper.
3149 if Ada_Version >= Ada_2020
3150 and then Present (Aspect_Specifications (N))
3152 Build_Access_Subprogram_Wrapper (N);
3155 when N_Access_To_Object_Definition =>
3156 Access_Type_Declaration (T, Def);
3158 -- Validate categorization rule against access type declaration
3159 -- usually a violation in Pure unit, Shared_Passive unit.
3161 Validate_Access_Type_Declaration (T, N);
3163 -- If we are in a Remote_Call_Interface package and define a
3164 -- RACW, then calling stubs and specific stream attributes
3168 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3170 Add_RACW_Features (Def_Id);
3173 when N_Array_Type_Definition =>
3174 Array_Type_Declaration (T, Def);
3176 when N_Derived_Type_Definition =>
3177 Derived_Type_Declaration (T, N, T /= Def_Id);
3179 -- Inherit predicates from parent, and protect against illegal
3182 if Is_Type (T) and then Has_Predicates (T) then
3183 Set_Has_Predicates (Def_Id);
3186 -- Save the scenario for examination by the ABE Processing
3189 Record_Elaboration_Scenario (N);
3191 when N_Enumeration_Type_Definition =>
3192 Enumeration_Type_Declaration (T, Def);
3194 when N_Floating_Point_Definition =>
3195 Floating_Point_Type_Declaration (T, Def);
3197 when N_Decimal_Fixed_Point_Definition =>
3198 Decimal_Fixed_Point_Type_Declaration (T, Def);
3200 when N_Ordinary_Fixed_Point_Definition =>
3201 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3203 when N_Signed_Integer_Type_Definition =>
3204 Signed_Integer_Type_Declaration (T, Def);
3206 when N_Modular_Type_Definition =>
3207 Modular_Type_Declaration (T, Def);
3209 when N_Record_Definition =>
3210 Record_Type_Declaration (T, N, Prev);
3212 -- If declaration has a parse error, nothing to elaborate.
3218 raise Program_Error;
3222 if Etype (T) = Any_Type then
3226 -- Some common processing for all types
3228 Set_Depends_On_Private (T, Has_Private_Component (T));
3229 Check_Ops_From_Incomplete_Type;
3231 -- Both the declared entity, and its anonymous base type if one was
3232 -- created, need freeze nodes allocated.
3235 B : constant Entity_Id := Base_Type (T);
3238 -- In the case where the base type differs from the first subtype, we
3239 -- pre-allocate a freeze node, and set the proper link to the first
3240 -- subtype. Freeze_Entity will use this preallocated freeze node when
3241 -- it freezes the entity.
3243 -- This does not apply if the base type is a generic type, whose
3244 -- declaration is independent of the current derived definition.
3246 if B /= T and then not Is_Generic_Type (B) then
3247 Ensure_Freeze_Node (B);
3248 Set_First_Subtype_Link (Freeze_Node (B), T);
3251 -- A type that is imported through a limited_with clause cannot
3252 -- generate any code, and thus need not be frozen. However, an access
3253 -- type with an imported designated type needs a finalization list,
3254 -- which may be referenced in some other package that has non-limited
3255 -- visibility on the designated type. Thus we must create the
3256 -- finalization list at the point the access type is frozen, to
3257 -- prevent unsatisfied references at link time.
3259 if not From_Limited_With (T) or else Is_Access_Type (T) then
3260 Set_Has_Delayed_Freeze (T);
3264 -- Case where T is the full declaration of some private type which has
3265 -- been swapped in Defining_Identifier (N).
3267 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3268 Process_Full_View (N, T, Def_Id);
3270 -- Record the reference. The form of this is a little strange, since
3271 -- the full declaration has been swapped in. So the first parameter
3272 -- here represents the entity to which a reference is made which is
3273 -- the "real" entity, i.e. the one swapped in, and the second
3274 -- parameter provides the reference location.
3276 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3277 -- since we don't want a complaint about the full type being an
3278 -- unwanted reference to the private type
3281 B : constant Boolean := Has_Pragma_Unreferenced (T);
3283 Set_Has_Pragma_Unreferenced (T, False);
3284 Generate_Reference (T, T, 'c');
3285 Set_Has_Pragma_Unreferenced (T, B);
3288 Set_Completion_Referenced (Def_Id);
3290 -- For completion of incomplete type, process incomplete dependents
3291 -- and always mark the full type as referenced (it is the incomplete
3292 -- type that we get for any real reference).
3294 elsif Ekind (Prev) = E_Incomplete_Type then
3295 Process_Incomplete_Dependents (N, T, Prev);
3296 Generate_Reference (Prev, Def_Id, 'c');
3297 Set_Completion_Referenced (Def_Id);
3299 -- If not private type or incomplete type completion, this is a real
3300 -- definition of a new entity, so record it.
3303 Generate_Definition (Def_Id);
3306 -- Propagate any pending access types whose finalization masters need to
3307 -- be fully initialized from the partial to the full view. Guard against
3308 -- an illegal full view that remains unanalyzed.
3310 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3311 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3314 if Chars (Scope (Def_Id)) = Name_System
3315 and then Chars (Def_Id) = Name_Address
3316 and then In_Predefined_Unit (N)
3318 Set_Is_Descendant_Of_Address (Def_Id);
3319 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3320 Set_Is_Descendant_Of_Address (Prev);
3323 Set_Optimize_Alignment_Flags (Def_Id);
3324 Check_Eliminated (Def_Id);
3326 -- If the declaration is a completion and aspects are present, apply
3327 -- them to the entity for the type which is currently the partial
3328 -- view, but which is the one that will be frozen.
3330 if Has_Aspects (N) then
3332 -- In most cases the partial view is a private type, and both views
3333 -- appear in different declarative parts. In the unusual case where
3334 -- the partial view is incomplete, perform the analysis on the
3335 -- full view, to prevent freezing anomalies with the corresponding
3336 -- class-wide type, which otherwise might be frozen before the
3337 -- dispatch table is built.
3340 and then Ekind (Prev) /= E_Incomplete_Type
3342 Analyze_Aspect_Specifications (N, Prev);
3347 Analyze_Aspect_Specifications (N, Def_Id);
3351 if Is_Derived_Type (Prev)
3352 and then Def_Id /= Prev
3354 Check_Nonoverridable_Aspects;
3356 end Analyze_Full_Type_Declaration;
3358 ----------------------------------
3359 -- Analyze_Incomplete_Type_Decl --
3360 ----------------------------------
3362 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3363 F : constant Boolean := Is_Pure (Current_Scope);
3367 Generate_Definition (Defining_Identifier (N));
3369 -- Process an incomplete declaration. The identifier must not have been
3370 -- declared already in the scope. However, an incomplete declaration may
3371 -- appear in the private part of a package, for a private type that has
3372 -- already been declared.
3374 -- In this case, the discriminants (if any) must match
3376 T := Find_Type_Name (N);
3378 Set_Ekind (T, E_Incomplete_Type);
3380 Set_Is_First_Subtype (T);
3381 Init_Size_Align (T);
3383 -- Set the SPARK mode from the current context
3385 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3386 Set_SPARK_Pragma_Inherited (T);
3388 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3389 -- incomplete types.
3391 if Tagged_Present (N) then
3392 Set_Is_Tagged_Type (T, True);
3393 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3394 Make_Class_Wide_Type (T);
3395 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3398 Set_Stored_Constraint (T, No_Elist);
3400 if Present (Discriminant_Specifications (N)) then
3402 Process_Discriminants (N);
3406 -- If the type has discriminants, nontrivial subtypes may be declared
3407 -- before the full view of the type. The full views of those subtypes
3408 -- will be built after the full view of the type.
3410 Set_Private_Dependents (T, New_Elmt_List);
3412 end Analyze_Incomplete_Type_Decl;
3414 -----------------------------------
3415 -- Analyze_Interface_Declaration --
3416 -----------------------------------
3418 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3419 CW : constant Entity_Id := Class_Wide_Type (T);
3422 Set_Is_Tagged_Type (T);
3423 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3425 Set_Is_Limited_Record (T, Limited_Present (Def)
3426 or else Task_Present (Def)
3427 or else Protected_Present (Def)
3428 or else Synchronized_Present (Def));
3430 -- Type is abstract if full declaration carries keyword, or if previous
3431 -- partial view did.
3433 Set_Is_Abstract_Type (T);
3434 Set_Is_Interface (T);
3436 -- Type is a limited interface if it includes the keyword limited, task,
3437 -- protected, or synchronized.
3439 Set_Is_Limited_Interface
3440 (T, Limited_Present (Def)
3441 or else Protected_Present (Def)
3442 or else Synchronized_Present (Def)
3443 or else Task_Present (Def));
3445 Set_Interfaces (T, New_Elmt_List);
3446 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3448 -- Complete the decoration of the class-wide entity if it was already
3449 -- built (i.e. during the creation of the limited view)
3451 if Present (CW) then
3452 Set_Is_Interface (CW);
3453 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3456 -- Check runtime support for synchronized interfaces
3458 if (Is_Task_Interface (T)
3459 or else Is_Protected_Interface (T)
3460 or else Is_Synchronized_Interface (T))
3461 and then not RTE_Available (RE_Select_Specific_Data)
3463 Error_Msg_CRT ("synchronized interfaces", T);
3465 end Analyze_Interface_Declaration;
3467 -----------------------------
3468 -- Analyze_Itype_Reference --
3469 -----------------------------
3471 -- Nothing to do. This node is placed in the tree only for the benefit of
3472 -- back end processing, and has no effect on the semantic processing.
3474 procedure Analyze_Itype_Reference (N : Node_Id) is
3476 pragma Assert (Is_Itype (Itype (N)));
3478 end Analyze_Itype_Reference;
3480 --------------------------------
3481 -- Analyze_Number_Declaration --
3482 --------------------------------
3484 procedure Analyze_Number_Declaration (N : Node_Id) is
3485 E : constant Node_Id := Expression (N);
3486 Id : constant Entity_Id := Defining_Identifier (N);
3487 Index : Interp_Index;
3492 Generate_Definition (Id);
3495 -- This is an optimization of a common case of an integer literal
3497 if Nkind (E) = N_Integer_Literal then
3498 Set_Is_Static_Expression (E, True);
3499 Set_Etype (E, Universal_Integer);
3501 Set_Etype (Id, Universal_Integer);
3502 Set_Ekind (Id, E_Named_Integer);
3503 Set_Is_Frozen (Id, True);
3505 Set_Debug_Info_Needed (Id);
3509 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3511 -- Process expression, replacing error by integer zero, to avoid
3512 -- cascaded errors or aborts further along in the processing
3514 -- Replace Error by integer zero, which seems least likely to cause
3518 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3519 Set_Error_Posted (E);
3524 -- Verify that the expression is static and numeric. If
3525 -- the expression is overloaded, we apply the preference
3526 -- rule that favors root numeric types.
3528 if not Is_Overloaded (E) then
3530 if Has_Dynamic_Predicate_Aspect (T) then
3532 ("subtype has dynamic predicate, "
3533 & "not allowed in number declaration", N);
3539 Get_First_Interp (E, Index, It);
3540 while Present (It.Typ) loop
3541 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3542 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3544 if T = Any_Type then
3547 elsif It.Typ = Universal_Real
3549 It.Typ = Universal_Integer
3551 -- Choose universal interpretation over any other
3558 Get_Next_Interp (Index, It);
3562 if Is_Integer_Type (T) then
3564 Set_Etype (Id, Universal_Integer);
3565 Set_Ekind (Id, E_Named_Integer);
3567 elsif Is_Real_Type (T) then
3569 -- Because the real value is converted to universal_real, this is a
3570 -- legal context for a universal fixed expression.
3572 if T = Universal_Fixed then
3574 Loc : constant Source_Ptr := Sloc (N);
3575 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3577 New_Occurrence_Of (Universal_Real, Loc),
3578 Expression => Relocate_Node (E));
3585 elsif T = Any_Fixed then
3586 Error_Msg_N ("illegal context for mixed mode operation", E);
3588 -- Expression is of the form : universal_fixed * integer. Try to
3589 -- resolve as universal_real.
3591 T := Universal_Real;
3596 Set_Etype (Id, Universal_Real);
3597 Set_Ekind (Id, E_Named_Real);
3600 Wrong_Type (E, Any_Numeric);
3604 Set_Ekind (Id, E_Constant);
3605 Set_Never_Set_In_Source (Id, True);
3606 Set_Is_True_Constant (Id, True);
3610 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3611 Set_Etype (E, Etype (Id));
3614 if not Is_OK_Static_Expression (E) then
3615 Flag_Non_Static_Expr
3616 ("non-static expression used in number declaration!", E);
3617 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3618 Set_Etype (E, Any_Type);
3621 Analyze_Dimension (N);
3622 end Analyze_Number_Declaration;
3624 --------------------------------
3625 -- Analyze_Object_Declaration --
3626 --------------------------------
3628 -- WARNING: This routine manages Ghost regions. Return statements must be
3629 -- replaced by gotos which jump to the end of the routine and restore the
3632 procedure Analyze_Object_Declaration (N : Node_Id) is
3633 Loc : constant Source_Ptr := Sloc (N);
3634 Id : constant Entity_Id := Defining_Identifier (N);
3635 Next_Decl : constant Node_Id := Next (N);
3640 E : Node_Id := Expression (N);
3641 -- E is set to Expression (N) throughout this routine. When Expression
3642 -- (N) is modified, E is changed accordingly.
3644 Prev_Entity : Entity_Id := Empty;
3646 procedure Check_Dynamic_Object (Typ : Entity_Id);
3647 -- A library-level object with nonstatic discriminant constraints may
3648 -- require dynamic allocation. The declaration is illegal if the
3649 -- profile includes the restriction No_Implicit_Heap_Allocations.
3651 procedure Check_For_Null_Excluding_Components
3652 (Obj_Typ : Entity_Id;
3653 Obj_Decl : Node_Id);
3654 -- Verify that each null-excluding component of object declaration
3655 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3656 -- a compile-time warning if this is not the case.
3658 function Count_Tasks (T : Entity_Id) return Uint;
3659 -- This function is called when a non-generic library level object of a
3660 -- task type is declared. Its function is to count the static number of
3661 -- tasks declared within the type (it is only called if Has_Task is set
3662 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3663 -- or a variant record type is encountered, Check_Restriction is called
3664 -- indicating the count is unknown.
3666 function Delayed_Aspect_Present return Boolean;
3667 -- If the declaration has an expression that is an aggregate, and it
3668 -- has aspects that require delayed analysis, the resolution of the
3669 -- aggregate must be deferred to the freeze point of the object. This
3670 -- special processing was created for address clauses, but it must
3671 -- also apply to Alignment. This must be done before the aspect
3672 -- specifications are analyzed because we must handle the aggregate
3673 -- before the analysis of the object declaration is complete.
3675 -- Any other relevant delayed aspects on object declarations ???
3677 --------------------------
3678 -- Check_Dynamic_Object --
3679 --------------------------
3681 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3683 Obj_Type : Entity_Id;
3688 if Is_Private_Type (Obj_Type)
3689 and then Present (Full_View (Obj_Type))
3691 Obj_Type := Full_View (Obj_Type);
3694 if Known_Static_Esize (Obj_Type) then
3698 if Restriction_Active (No_Implicit_Heap_Allocations)
3699 and then Expander_Active
3700 and then Has_Discriminants (Obj_Type)
3702 Comp := First_Component (Obj_Type);
3703 while Present (Comp) loop
3704 if Known_Static_Esize (Etype (Comp))
3705 or else Size_Known_At_Compile_Time (Etype (Comp))
3709 elsif not Discriminated_Size (Comp)
3710 and then Comes_From_Source (Comp)
3713 ("component& of non-static size will violate restriction "
3714 & "No_Implicit_Heap_Allocation?", N, Comp);
3716 elsif Is_Record_Type (Etype (Comp)) then
3717 Check_Dynamic_Object (Etype (Comp));
3720 Next_Component (Comp);
3723 end Check_Dynamic_Object;
3725 -----------------------------------------
3726 -- Check_For_Null_Excluding_Components --
3727 -----------------------------------------
3729 procedure Check_For_Null_Excluding_Components
3730 (Obj_Typ : Entity_Id;
3733 procedure Check_Component
3734 (Comp_Typ : Entity_Id;
3735 Comp_Decl : Node_Id := Empty;
3736 Array_Comp : Boolean := False);
3737 -- Apply a compile-time null-exclusion check on a component denoted
3738 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3739 -- subcomponents (if any).
3741 ---------------------
3742 -- Check_Component --
3743 ---------------------
3745 procedure Check_Component
3746 (Comp_Typ : Entity_Id;
3747 Comp_Decl : Node_Id := Empty;
3748 Array_Comp : Boolean := False)
3754 -- Do not consider internally-generated components or those that
3755 -- are already initialized.
3757 if Present (Comp_Decl)
3758 and then (not Comes_From_Source (Comp_Decl)
3759 or else Present (Expression (Comp_Decl)))
3764 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3765 and then Present (Full_View (Comp_Typ))
3767 T := Full_View (Comp_Typ);
3772 -- Verify a component of a null-excluding access type
3774 if Is_Access_Type (T)
3775 and then Can_Never_Be_Null (T)
3777 if Comp_Decl = Obj_Decl then
3778 Null_Exclusion_Static_Checks
3781 Array_Comp => Array_Comp);
3784 Null_Exclusion_Static_Checks
3787 Array_Comp => Array_Comp);
3790 -- Check array components
3792 elsif Is_Array_Type (T) then
3794 -- There is no suitable component when the object is of an
3795 -- array type. However, a namable component may appear at some
3796 -- point during the recursive inspection, but not at the top
3797 -- level. At the top level just indicate array component case.
3799 if Comp_Decl = Obj_Decl then
3800 Check_Component (Component_Type (T), Array_Comp => True);
3802 Check_Component (Component_Type (T), Comp_Decl);
3805 -- Verify all components of type T
3807 -- Note: No checks are performed on types with discriminants due
3808 -- to complexities involving variants. ???
3810 elsif (Is_Concurrent_Type (T)
3811 or else Is_Incomplete_Or_Private_Type (T)
3812 or else Is_Record_Type (T))
3813 and then not Has_Discriminants (T)
3815 Comp := First_Component (T);
3816 while Present (Comp) loop
3817 Check_Component (Etype (Comp), Parent (Comp));
3819 Next_Component (Comp);
3822 end Check_Component;
3824 -- Start processing for Check_For_Null_Excluding_Components
3827 Check_Component (Obj_Typ, Obj_Decl);
3828 end Check_For_Null_Excluding_Components;
3834 function Count_Tasks (T : Entity_Id) return Uint is
3840 if Is_Task_Type (T) then
3843 elsif Is_Record_Type (T) then
3844 if Has_Discriminants (T) then
3845 Check_Restriction (Max_Tasks, N);
3850 C := First_Component (T);
3851 while Present (C) loop
3852 V := V + Count_Tasks (Etype (C));
3859 elsif Is_Array_Type (T) then
3860 X := First_Index (T);
3861 V := Count_Tasks (Component_Type (T));
3862 while Present (X) loop
3865 if not Is_OK_Static_Subtype (C) then
3866 Check_Restriction (Max_Tasks, N);
3869 V := V * (UI_Max (Uint_0,
3870 Expr_Value (Type_High_Bound (C)) -
3871 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3884 ----------------------------
3885 -- Delayed_Aspect_Present --
3886 ----------------------------
3888 function Delayed_Aspect_Present return Boolean is
3893 if Present (Aspect_Specifications (N)) then
3894 A := First (Aspect_Specifications (N));
3895 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3896 while Present (A) loop
3897 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3899 -- Set flag on object entity, for later processing at
3900 -- the freeze point.
3902 Set_Has_Delayed_Aspects (Id);
3911 end Delayed_Aspect_Present;
3915 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3916 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3917 -- Save the Ghost-related attributes to restore on exit
3919 Related_Id : Entity_Id;
3920 Full_View_Present : Boolean := False;
3922 -- Start of processing for Analyze_Object_Declaration
3925 -- There are three kinds of implicit types generated by an
3926 -- object declaration:
3928 -- 1. Those generated by the original Object Definition
3930 -- 2. Those generated by the Expression
3932 -- 3. Those used to constrain the Object Definition with the
3933 -- expression constraints when the definition is unconstrained.
3935 -- They must be generated in this order to avoid order of elaboration
3936 -- issues. Thus the first step (after entering the name) is to analyze
3937 -- the object definition.
3939 if Constant_Present (N) then
3940 Prev_Entity := Current_Entity_In_Scope (Id);
3942 if Present (Prev_Entity)
3944 -- If the homograph is an implicit subprogram, it is overridden
3945 -- by the current declaration.
3947 ((Is_Overloadable (Prev_Entity)
3948 and then Is_Inherited_Operation (Prev_Entity))
3950 -- The current object is a discriminal generated for an entry
3951 -- family index. Even though the index is a constant, in this
3952 -- particular context there is no true constant redeclaration.
3953 -- Enter_Name will handle the visibility.
3956 (Is_Discriminal (Id)
3957 and then Ekind (Discriminal_Link (Id)) =
3958 E_Entry_Index_Parameter)
3960 -- The current object is the renaming for a generic declared
3961 -- within the instance.
3964 (Ekind (Prev_Entity) = E_Package
3965 and then Nkind (Parent (Prev_Entity)) =
3966 N_Package_Renaming_Declaration
3967 and then not Comes_From_Source (Prev_Entity)
3969 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3971 -- The entity may be a homonym of a private component of the
3972 -- enclosing protected object, for which we create a local
3973 -- renaming declaration. The declaration is legal, even if
3974 -- useless when it just captures that component.
3977 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3978 and then Nkind (Parent (Prev_Entity)) =
3979 N_Object_Renaming_Declaration))
3981 Prev_Entity := Empty;
3985 if Present (Prev_Entity) then
3987 -- The object declaration is Ghost when it completes a deferred Ghost
3990 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
3992 Constant_Redeclaration (Id, N, T);
3994 Generate_Reference (Prev_Entity, Id, 'c');
3995 Set_Completion_Referenced (Id);
3997 if Error_Posted (N) then
3999 -- Type mismatch or illegal redeclaration; do not analyze
4000 -- expression to avoid cascaded errors.
4002 T := Find_Type_Of_Object (Object_Definition (N), N);
4004 Set_Ekind (Id, E_Variable);
4008 -- In the normal case, enter identifier at the start to catch premature
4009 -- usage in the initialization expression.
4012 Generate_Definition (Id);
4015 Mark_Coextensions (N, Object_Definition (N));
4017 T := Find_Type_Of_Object (Object_Definition (N), N);
4019 if Nkind (Object_Definition (N)) = N_Access_Definition
4021 (Access_To_Subprogram_Definition (Object_Definition (N)))
4022 and then Protected_Present
4023 (Access_To_Subprogram_Definition (Object_Definition (N)))
4025 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4028 if Error_Posted (Id) then
4030 Set_Ekind (Id, E_Variable);
4035 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4036 -- out some static checks.
4038 if Ada_Version >= Ada_2005 then
4040 -- In case of aggregates we must also take care of the correct
4041 -- initialization of nested aggregates bug this is done at the
4042 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4044 if Can_Never_Be_Null (T) then
4045 if Present (Expression (N))
4046 and then Nkind (Expression (N)) = N_Aggregate
4052 Save_Typ : constant Entity_Id := Etype (Id);
4054 Set_Etype (Id, T); -- Temp. decoration for static checks
4055 Null_Exclusion_Static_Checks (N);
4056 Set_Etype (Id, Save_Typ);
4060 -- We might be dealing with an object of a composite type containing
4061 -- null-excluding components without an aggregate, so we must verify
4062 -- that such components have default initialization.
4065 Check_For_Null_Excluding_Components (T, N);
4069 -- Object is marked pure if it is in a pure scope
4071 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4073 -- If deferred constant, make sure context is appropriate. We detect
4074 -- a deferred constant as a constant declaration with no expression.
4075 -- A deferred constant can appear in a package body if its completion
4076 -- is by means of an interface pragma.
4078 if Constant_Present (N) and then No (E) then
4080 -- A deferred constant may appear in the declarative part of the
4081 -- following constructs:
4085 -- extended return statements
4088 -- subprogram bodies
4091 -- When declared inside a package spec, a deferred constant must be
4092 -- completed by a full constant declaration or pragma Import. In all
4093 -- other cases, the only proper completion is pragma Import. Extended
4094 -- return statements are flagged as invalid contexts because they do
4095 -- not have a declarative part and so cannot accommodate the pragma.
4097 if Ekind (Current_Scope) = E_Return_Statement then
4099 ("invalid context for deferred constant declaration (RM 7.4)",
4102 ("\declaration requires an initialization expression",
4104 Set_Constant_Present (N, False);
4106 -- In Ada 83, deferred constant must be of private type
4108 elsif not Is_Private_Type (T) then
4109 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4111 ("(Ada 83) deferred constant must be private type", N);
4115 -- If not a deferred constant, then the object declaration freezes
4116 -- its type, unless the object is of an anonymous type and has delayed
4117 -- aspects. In that case the type is frozen when the object itself is.
4120 Check_Fully_Declared (T, N);
4122 if Has_Delayed_Aspects (Id)
4123 and then Is_Array_Type (T)
4124 and then Is_Itype (T)
4126 Set_Has_Delayed_Freeze (T);
4128 Freeze_Before (N, T);
4132 -- If the object was created by a constrained array definition, then
4133 -- set the link in both the anonymous base type and anonymous subtype
4134 -- that are built to represent the array type to point to the object.
4136 if Nkind (Object_Definition (Declaration_Node (Id))) =
4137 N_Constrained_Array_Definition
4139 Set_Related_Array_Object (T, Id);
4140 Set_Related_Array_Object (Base_Type (T), Id);
4143 -- Special checks for protected objects not at library level
4145 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4146 Check_Restriction (No_Local_Protected_Objects, Id);
4148 -- Protected objects with interrupt handlers must be at library level
4150 -- Ada 2005: This test is not needed (and the corresponding clause
4151 -- in the RM is removed) because accessibility checks are sufficient
4152 -- to make handlers not at the library level illegal.
4154 -- AI05-0303: The AI is in fact a binding interpretation, and thus
4155 -- applies to the '95 version of the language as well.
4157 if Is_Protected_Type (T)
4158 and then Has_Interrupt_Handler (T)
4159 and then Ada_Version < Ada_95
4162 ("interrupt object can only be declared at library level", Id);
4166 -- Check for violation of No_Local_Timing_Events
4168 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4169 Check_Restriction (No_Local_Timing_Events, Id);
4172 -- The actual subtype of the object is the nominal subtype, unless
4173 -- the nominal one is unconstrained and obtained from the expression.
4177 if Is_Library_Level_Entity (Id) then
4178 Check_Dynamic_Object (T);
4181 -- Process initialization expression if present and not in error
4183 if Present (E) and then E /= Error then
4185 -- Generate an error in case of CPP class-wide object initialization.
4186 -- Required because otherwise the expansion of the class-wide
4187 -- assignment would try to use 'size to initialize the object
4188 -- (primitive that is not available in CPP tagged types).
4190 if Is_Class_Wide_Type (Act_T)
4192 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4194 (Present (Full_View (Root_Type (Etype (Act_T))))
4196 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4199 ("predefined assignment not available for 'C'P'P tagged types",
4203 Mark_Coextensions (N, E);
4206 -- In case of errors detected in the analysis of the expression,
4207 -- decorate it with the expected type to avoid cascaded errors
4209 if No (Etype (E)) then
4213 -- If an initialization expression is present, then we set the
4214 -- Is_True_Constant flag. It will be reset if this is a variable
4215 -- and it is indeed modified.
4217 Set_Is_True_Constant (Id, True);
4219 -- If we are analyzing a constant declaration, set its completion
4220 -- flag after analyzing and resolving the expression.
4222 if Constant_Present (N) then
4223 Set_Has_Completion (Id);
4226 -- Set type and resolve (type may be overridden later on). Note:
4227 -- Ekind (Id) must still be E_Void at this point so that incorrect
4228 -- early usage within E is properly diagnosed.
4232 -- If the expression is an aggregate we must look ahead to detect
4233 -- the possible presence of an address clause, and defer resolution
4234 -- and expansion of the aggregate to the freeze point of the entity.
4236 -- This is not always legal because the aggregate may contain other
4237 -- references that need freezing, e.g. references to other entities
4238 -- with address clauses. In any case, when compiling with -gnatI the
4239 -- presence of the address clause must be ignored.
4241 if Comes_From_Source (N)
4242 and then Expander_Active
4243 and then Nkind (E) = N_Aggregate
4245 ((Present (Following_Address_Clause (N))
4246 and then not Ignore_Rep_Clauses)
4247 or else Delayed_Aspect_Present)
4251 -- If the aggregate is limited it will be built in place, and its
4252 -- expansion is deferred until the object declaration is expanded.
4254 -- This is also required when generating C code to ensure that an
4255 -- object with an alignment or address clause can be initialized
4256 -- by means of component by component assignments.
4258 if Is_Limited_Type (T) or else Modify_Tree_For_C then
4259 Set_Expansion_Delayed (E);
4263 -- If the expression is a formal that is a "subprogram pointer"
4264 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4265 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4266 -- the corresponding check, as is done for assignments.
4268 if Is_Entity_Name (E)
4269 and then Present (Entity (E))
4270 and then Is_Formal (Entity (E))
4272 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4273 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4275 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4281 -- No further action needed if E is a call to an inlined function
4282 -- which returns an unconstrained type and it has been expanded into
4283 -- a procedure call. In that case N has been replaced by an object
4284 -- declaration without initializing expression and it has been
4285 -- analyzed (see Expand_Inlined_Call).
4287 if Back_End_Inlining
4288 and then Expander_Active
4289 and then Nkind (E) = N_Function_Call
4290 and then Nkind (Name (E)) in N_Has_Entity
4291 and then Is_Inlined (Entity (Name (E)))
4292 and then not Is_Constrained (Etype (E))
4293 and then Analyzed (N)
4294 and then No (Expression (N))
4299 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4300 -- node (which was marked already-analyzed), we need to set the type
4301 -- to something other than Any_Access in order to keep gigi happy.
4303 if Etype (E) = Any_Access then
4307 -- If the object is an access to variable, the initialization
4308 -- expression cannot be an access to constant.
4310 if Is_Access_Type (T)
4311 and then not Is_Access_Constant (T)
4312 and then Is_Access_Type (Etype (E))
4313 and then Is_Access_Constant (Etype (E))
4316 ("access to variable cannot be initialized with an "
4317 & "access-to-constant expression", E);
4320 if not Assignment_OK (N) then
4321 Check_Initialization (T, E);
4324 Check_Unset_Reference (E);
4326 -- If this is a variable, then set current value. If this is a
4327 -- declared constant of a scalar type with a static expression,
4328 -- indicate that it is always valid.
4330 if not Constant_Present (N) then
4331 if Compile_Time_Known_Value (E) then
4332 Set_Current_Value (Id, E);
4335 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4336 Set_Is_Known_Valid (Id);
4338 -- If it is a constant initialized with a valid nonstatic entity,
4339 -- the constant is known valid as well, and can inherit the subtype
4340 -- of the entity if it is a subtype of the given type. This info
4341 -- is preserved on the actual subtype of the constant.
4343 elsif Is_Scalar_Type (T)
4344 and then Is_Entity_Name (E)
4345 and then Is_Known_Valid (Entity (E))
4346 and then In_Subrange_Of (Etype (Entity (E)), T)
4348 Set_Is_Known_Valid (Id);
4349 Set_Ekind (Id, E_Constant);
4350 Set_Actual_Subtype (Id, Etype (Entity (E)));
4353 -- Deal with setting of null flags
4355 if Is_Access_Type (T) then
4356 if Known_Non_Null (E) then
4357 Set_Is_Known_Non_Null (Id, True);
4358 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4359 Set_Is_Known_Null (Id, True);
4363 -- Check incorrect use of dynamically tagged expressions
4365 if Is_Tagged_Type (T) then
4366 Check_Dynamically_Tagged_Expression
4372 Apply_Scalar_Range_Check (E, T);
4373 Apply_Static_Length_Check (E, T);
4375 -- A formal parameter of a specific tagged type whose related
4376 -- subprogram is subject to pragma Extensions_Visible with value
4377 -- "False" cannot be implicitly converted to a class-wide type by
4378 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4379 -- not consider internally generated expressions.
4381 if Is_Class_Wide_Type (T)
4382 and then Comes_From_Source (E)
4383 and then Is_EVF_Expression (E)
4386 ("formal parameter cannot be implicitly converted to "
4387 & "class-wide type when Extensions_Visible is False", E);
4391 -- If the No_Streams restriction is set, check that the type of the
4392 -- object is not, and does not contain, any subtype derived from
4393 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4394 -- Has_Stream just for efficiency reasons. There is no point in
4395 -- spending time on a Has_Stream check if the restriction is not set.
4397 if Restriction_Check_Required (No_Streams) then
4398 if Has_Stream (T) then
4399 Check_Restriction (No_Streams, N);
4403 -- Deal with predicate check before we start to do major rewriting. It
4404 -- is OK to initialize and then check the initialized value, since the
4405 -- object goes out of scope if we get a predicate failure. Note that we
4406 -- do this in the analyzer and not the expander because the analyzer
4407 -- does some substantial rewriting in some cases.
4409 -- We need a predicate check if the type has predicates that are not
4410 -- ignored, and if either there is an initializing expression, or for
4411 -- default initialization when we have at least one case of an explicit
4412 -- default initial value (including via a Default_Value or
4413 -- Default_Component_Value aspect, see AI12-0301) and then this is not
4414 -- an internal declaration whose initialization comes later (as for an
4415 -- aggregate expansion).
4416 -- If expression is an aggregate it may be expanded into assignments
4417 -- and the declaration itself is marked with No_Initialization, but
4418 -- the predicate still applies.
4420 if not Suppress_Assignment_Checks (N)
4421 and then Present (Predicate_Function (T))
4422 and then not Predicates_Ignored (T)
4424 (not No_Initialization (N)
4425 or else (Present (E) and then Nkind (E) = N_Aggregate))
4429 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4431 -- If the type has a static predicate and the expression is known at
4432 -- compile time, see if the expression satisfies the predicate.
4435 Check_Expression_Against_Static_Predicate (E, T);
4438 -- If the type is a null record and there is no explicit initial
4439 -- expression, no predicate check applies.
4441 if No (E) and then Is_Null_Record_Type (T) then
4444 -- Do not generate a predicate check if the initialization expression
4445 -- is a type conversion because the conversion has been subjected to
4446 -- the same check. This is a small optimization which avoid redundant
4449 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4453 -- The check must be inserted after the expanded aggregate
4454 -- expansion code, if any.
4457 Check : constant Node_Id :=
4458 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4461 if No (Next_Decl) then
4462 Append_To (List_Containing (N), Check);
4464 Insert_Before (Next_Decl, Check);
4470 -- Case of unconstrained type
4472 if not Is_Definite_Subtype (T) then
4474 -- Nothing to do in deferred constant case
4476 if Constant_Present (N) and then No (E) then
4479 -- Case of no initialization present
4482 if No_Initialization (N) then
4485 elsif Is_Class_Wide_Type (T) then
4487 ("initialization required in class-wide declaration ", N);
4491 ("unconstrained subtype not allowed (need initialization)",
4492 Object_Definition (N));
4494 if Is_Record_Type (T) and then Has_Discriminants (T) then
4496 ("\provide initial value or explicit discriminant values",
4497 Object_Definition (N));
4500 ("\or give default discriminant values for type&",
4501 Object_Definition (N), T);
4503 elsif Is_Array_Type (T) then
4505 ("\provide initial value or explicit array bounds",
4506 Object_Definition (N));
4510 -- Case of initialization present but in error. Set initial
4511 -- expression as absent (but do not make above complaints)
4513 elsif E = Error then
4514 Set_Expression (N, Empty);
4517 -- Case of initialization present
4520 -- Check restrictions in Ada 83
4522 if not Constant_Present (N) then
4524 -- Unconstrained variables not allowed in Ada 83 mode
4526 if Ada_Version = Ada_83
4527 and then Comes_From_Source (Object_Definition (N))
4530 ("(Ada 83) unconstrained variable not allowed",
4531 Object_Definition (N));
4535 -- Now we constrain the variable from the initializing expression
4537 -- If the expression is an aggregate, it has been expanded into
4538 -- individual assignments. Retrieve the actual type from the
4539 -- expanded construct.
4541 if Is_Array_Type (T)
4542 and then No_Initialization (N)
4543 and then Nkind (Original_Node (E)) = N_Aggregate
4547 -- In case of class-wide interface object declarations we delay
4548 -- the generation of the equivalent record type declarations until
4549 -- its expansion because there are cases in they are not required.
4551 elsif Is_Interface (T) then
4554 -- If the type is an unchecked union, no subtype can be built from
4555 -- the expression. Rewrite declaration as a renaming, which the
4556 -- back-end can handle properly. This is a rather unusual case,
4557 -- because most unchecked_union declarations have default values
4558 -- for discriminants and are thus not indefinite.
4560 elsif Is_Unchecked_Union (T) then
4561 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4562 Set_Ekind (Id, E_Constant);
4564 Set_Ekind (Id, E_Variable);
4568 Make_Object_Renaming_Declaration (Loc,
4569 Defining_Identifier => Id,
4570 Subtype_Mark => New_Occurrence_Of (T, Loc),
4573 Set_Renamed_Object (Id, E);
4574 Freeze_Before (N, T);
4579 -- Ensure that the generated subtype has a unique external name
4580 -- when the related object is public. This guarantees that the
4581 -- subtype and its bounds will not be affected by switches or
4582 -- pragmas that may offset the internal counter due to extra
4585 if Is_Public (Id) then
4588 Related_Id := Empty;
4591 Expand_Subtype_From_Expr
4594 Subtype_Indic => Object_Definition (N),
4596 Related_Id => Related_Id);
4598 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4601 -- Propagate attributes to full view when needed.
4603 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4605 if Is_Private_Type (Act_T) and then Present (Full_View (Act_T))
4607 Full_View_Present := True;
4610 if Full_View_Present then
4611 Set_Is_Constr_Subt_For_U_Nominal (Full_View (Act_T));
4614 if Aliased_Present (N) then
4615 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4617 if Full_View_Present then
4618 Set_Is_Constr_Subt_For_UN_Aliased (Full_View (Act_T));
4622 Freeze_Before (N, Act_T);
4623 Freeze_Before (N, T);
4626 elsif Is_Array_Type (T)
4627 and then No_Initialization (N)
4628 and then (Nkind (Original_Node (E)) = N_Aggregate
4629 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4630 and then Nkind (Original_Node (Expression
4631 (Original_Node (E)))) = N_Aggregate))
4633 if not Is_Entity_Name (Object_Definition (N)) then
4635 Check_Compile_Time_Size (Act_T);
4637 if Aliased_Present (N) then
4638 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4642 -- When the given object definition and the aggregate are specified
4643 -- independently, and their lengths might differ do a length check.
4644 -- This cannot happen if the aggregate is of the form (others =>...)
4646 if not Is_Constrained (T) then
4649 elsif Nkind (E) = N_Raise_Constraint_Error then
4651 -- Aggregate is statically illegal. Place back in declaration
4653 Set_Expression (N, E);
4654 Set_No_Initialization (N, False);
4656 elsif T = Etype (E) then
4659 elsif Nkind (E) = N_Aggregate
4660 and then Present (Component_Associations (E))
4661 and then Present (Choice_List (First (Component_Associations (E))))
4663 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4669 Apply_Length_Check (E, T);
4672 -- If the type is limited unconstrained with defaulted discriminants and
4673 -- there is no expression, then the object is constrained by the
4674 -- defaults, so it is worthwhile building the corresponding subtype.
4676 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4677 and then not Is_Constrained (T)
4678 and then Has_Discriminants (T)
4681 Act_T := Build_Default_Subtype (T, N);
4683 -- Ada 2005: A limited object may be initialized by means of an
4684 -- aggregate. If the type has default discriminants it has an
4685 -- unconstrained nominal type, Its actual subtype will be obtained
4686 -- from the aggregate, and not from the default discriminants.
4691 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4693 elsif Nkind (E) = N_Function_Call
4694 and then Constant_Present (N)
4695 and then Has_Unconstrained_Elements (Etype (E))
4697 -- The back-end has problems with constants of a discriminated type
4698 -- with defaults, if the initial value is a function call. We
4699 -- generate an intermediate temporary that will receive a reference
4700 -- to the result of the call. The initialization expression then
4701 -- becomes a dereference of that temporary.
4703 Remove_Side_Effects (E);
4705 -- If this is a constant declaration of an unconstrained type and
4706 -- the initialization is an aggregate, we can use the subtype of the
4707 -- aggregate for the declared entity because it is immutable.
4709 elsif not Is_Constrained (T)
4710 and then Has_Discriminants (T)
4711 and then Constant_Present (N)
4712 and then not Has_Unchecked_Union (T)
4713 and then Nkind (E) = N_Aggregate
4718 -- Check No_Wide_Characters restriction
4720 Check_Wide_Character_Restriction (T, Object_Definition (N));
4722 -- Indicate this is not set in source. Certainly true for constants, and
4723 -- true for variables so far (will be reset for a variable if and when
4724 -- we encounter a modification in the source).
4726 Set_Never_Set_In_Source (Id);
4728 -- Now establish the proper kind and type of the object
4730 if Constant_Present (N) then
4731 Set_Ekind (Id, E_Constant);
4732 Set_Is_True_Constant (Id);
4735 Set_Ekind (Id, E_Variable);
4737 -- A variable is set as shared passive if it appears in a shared
4738 -- passive package, and is at the outer level. This is not done for
4739 -- entities generated during expansion, because those are always
4740 -- manipulated locally.
4742 if Is_Shared_Passive (Current_Scope)
4743 and then Is_Library_Level_Entity (Id)
4744 and then Comes_From_Source (Id)
4746 Set_Is_Shared_Passive (Id);
4747 Check_Shared_Var (Id, T, N);
4750 -- Set Has_Initial_Value if initializing expression present. Note
4751 -- that if there is no initializing expression, we leave the state
4752 -- of this flag unchanged (usually it will be False, but notably in
4753 -- the case of exception choice variables, it will already be true).
4756 Set_Has_Initial_Value (Id);
4760 -- Set the SPARK mode from the current context (may be overwritten later
4761 -- with explicit pragma).
4763 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4764 Set_SPARK_Pragma_Inherited (Id);
4766 -- Preserve relevant elaboration-related attributes of the context which
4767 -- are no longer available or very expensive to recompute once analysis,
4768 -- resolution, and expansion are over.
4770 Mark_Elaboration_Attributes
4775 -- Initialize alignment and size and capture alignment setting
4777 Init_Alignment (Id);
4779 Set_Optimize_Alignment_Flags (Id);
4781 -- Deal with aliased case
4783 if Aliased_Present (N) then
4784 Set_Is_Aliased (Id);
4786 -- AI12-001: All aliased objects are considered to be specified as
4787 -- independently addressable (RM C.6(8.1/4)).
4789 Set_Is_Independent (Id);
4791 -- If the object is aliased and the type is unconstrained with
4792 -- defaulted discriminants and there is no expression, then the
4793 -- object is constrained by the defaults, so it is worthwhile
4794 -- building the corresponding subtype.
4796 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4797 -- unconstrained, then only establish an actual subtype if the
4798 -- nominal subtype is indefinite. In definite cases the object is
4799 -- unconstrained in Ada 2005.
4802 and then Is_Record_Type (T)
4803 and then not Is_Constrained (T)
4804 and then Has_Discriminants (T)
4805 and then (Ada_Version < Ada_2005
4806 or else not Is_Definite_Subtype (T))
4808 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4812 -- Now we can set the type of the object
4814 Set_Etype (Id, Act_T);
4816 -- Non-constant object is marked to be treated as volatile if type is
4817 -- volatile and we clear the Current_Value setting that may have been
4818 -- set above. Doing so for constants isn't required and might interfere
4819 -- with possible uses of the object as a static expression in contexts
4820 -- incompatible with volatility (e.g. as a case-statement alternative).
4822 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4823 Set_Treat_As_Volatile (Id);
4824 Set_Current_Value (Id, Empty);
4827 -- Deal with controlled types
4829 if Has_Controlled_Component (Etype (Id))
4830 or else Is_Controlled (Etype (Id))
4832 if not Is_Library_Level_Entity (Id) then
4833 Check_Restriction (No_Nested_Finalization, N);
4835 Validate_Controlled_Object (Id);
4839 if Has_Task (Etype (Id)) then
4840 Check_Restriction (No_Tasking, N);
4842 -- Deal with counting max tasks
4844 -- Nothing to do if inside a generic
4846 if Inside_A_Generic then
4849 -- If library level entity, then count tasks
4851 elsif Is_Library_Level_Entity (Id) then
4852 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4854 -- If not library level entity, then indicate we don't know max
4855 -- tasks and also check task hierarchy restriction and blocking
4856 -- operation (since starting a task is definitely blocking).
4859 Check_Restriction (Max_Tasks, N);
4860 Check_Restriction (No_Task_Hierarchy, N);
4861 Check_Potentially_Blocking_Operation (N);
4864 -- A rather specialized test. If we see two tasks being declared
4865 -- of the same type in the same object declaration, and the task
4866 -- has an entry with an address clause, we know that program error
4867 -- will be raised at run time since we can't have two tasks with
4868 -- entries at the same address.
4870 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4875 E := First_Entity (Etype (Id));
4876 while Present (E) loop
4877 if Ekind (E) = E_Entry
4878 and then Present (Get_Attribute_Definition_Clause
4879 (E, Attribute_Address))
4881 Error_Msg_Warn := SPARK_Mode /= On;
4883 ("more than one task with same entry address<<", N);
4884 Error_Msg_N ("\Program_Error [<<", N);
4886 Make_Raise_Program_Error (Loc,
4887 Reason => PE_Duplicated_Entry_Address));
4897 -- Some simple constant-propagation: if the expression is a constant
4898 -- string initialized with a literal, share the literal. This avoids
4902 and then Is_Entity_Name (E)
4903 and then Ekind (Entity (E)) = E_Constant
4904 and then Base_Type (Etype (E)) = Standard_String
4907 Val : constant Node_Id := Constant_Value (Entity (E));
4909 if Present (Val) and then Nkind (Val) = N_String_Literal then
4910 Rewrite (E, New_Copy (Val));
4915 -- Another optimization: if the nominal subtype is unconstrained and
4916 -- the expression is a function call that returns an unconstrained
4917 -- type, rewrite the declaration as a renaming of the result of the
4918 -- call. The exceptions below are cases where the copy is expected,
4919 -- either by the back end (Aliased case) or by the semantics, as for
4920 -- initializing controlled types or copying tags for class-wide types.
4923 and then Nkind (E) = N_Explicit_Dereference
4924 and then Nkind (Original_Node (E)) = N_Function_Call
4925 and then not Is_Library_Level_Entity (Id)
4926 and then not Is_Constrained (Underlying_Type (T))
4927 and then not Is_Aliased (Id)
4928 and then not Is_Class_Wide_Type (T)
4929 and then not Is_Controlled (T)
4930 and then not Has_Controlled_Component (Base_Type (T))
4931 and then Expander_Active
4934 Make_Object_Renaming_Declaration (Loc,
4935 Defining_Identifier => Id,
4936 Access_Definition => Empty,
4937 Subtype_Mark => New_Occurrence_Of
4938 (Base_Type (Etype (Id)), Loc),
4941 Set_Renamed_Object (Id, E);
4943 -- Force generation of debugging information for the constant and for
4944 -- the renamed function call.
4946 Set_Debug_Info_Needed (Id);
4947 Set_Debug_Info_Needed (Entity (Prefix (E)));
4950 if Present (Prev_Entity)
4951 and then Is_Frozen (Prev_Entity)
4952 and then not Error_Posted (Id)
4954 Error_Msg_N ("full constant declaration appears too late", N);
4957 Check_Eliminated (Id);
4959 -- Deal with setting In_Private_Part flag if in private part
4961 if Ekind (Scope (Id)) = E_Package
4962 and then In_Private_Part (Scope (Id))
4964 Set_In_Private_Part (Id);
4968 -- Initialize the refined state of a variable here because this is a
4969 -- common destination for legal and illegal object declarations.
4971 if Ekind (Id) = E_Variable then
4972 Set_Encapsulating_State (Id, Empty);
4975 if Has_Aspects (N) then
4976 Analyze_Aspect_Specifications (N, Id);
4979 Analyze_Dimension (N);
4981 -- Verify whether the object declaration introduces an illegal hidden
4982 -- state within a package subject to a null abstract state.
4984 if Ekind (Id) = E_Variable then
4985 Check_No_Hidden_State (Id);
4988 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4989 end Analyze_Object_Declaration;
4991 ---------------------------
4992 -- Analyze_Others_Choice --
4993 ---------------------------
4995 -- Nothing to do for the others choice node itself, the semantic analysis
4996 -- of the others choice will occur as part of the processing of the parent
4998 procedure Analyze_Others_Choice (N : Node_Id) is
4999 pragma Warnings (Off, N);
5002 end Analyze_Others_Choice;
5004 -------------------------------------------
5005 -- Analyze_Private_Extension_Declaration --
5006 -------------------------------------------
5008 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5009 Indic : constant Node_Id := Subtype_Indication (N);
5010 T : constant Entity_Id := Defining_Identifier (N);
5012 Iface_Elmt : Elmt_Id;
5013 Parent_Base : Entity_Id;
5014 Parent_Type : Entity_Id;
5017 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5019 if Is_Non_Empty_List (Interface_List (N)) then
5025 Intf := First (Interface_List (N));
5026 while Present (Intf) loop
5027 T := Find_Type_Of_Subtype_Indic (Intf);
5029 Diagnose_Interface (Intf, T);
5035 Generate_Definition (T);
5037 -- For other than Ada 2012, just enter the name in the current scope
5039 if Ada_Version < Ada_2012 then
5042 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5043 -- case of private type that completes an incomplete type.
5050 Prev := Find_Type_Name (N);
5052 pragma Assert (Prev = T
5053 or else (Ekind (Prev) = E_Incomplete_Type
5054 and then Present (Full_View (Prev))
5055 and then Full_View (Prev) = T));
5059 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5060 Parent_Base := Base_Type (Parent_Type);
5062 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5063 Set_Ekind (T, Ekind (Parent_Type));
5064 Set_Etype (T, Any_Type);
5067 elsif not Is_Tagged_Type (Parent_Type) then
5069 ("parent of type extension must be a tagged type ", Indic);
5072 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
5073 Error_Msg_N ("premature derivation of incomplete type", Indic);
5076 elsif Is_Concurrent_Type (Parent_Type) then
5078 ("parent type of a private extension cannot be a synchronized "
5079 & "tagged type (RM 3.9.1 (3/1))", N);
5081 Set_Etype (T, Any_Type);
5082 Set_Ekind (T, E_Limited_Private_Type);
5083 Set_Private_Dependents (T, New_Elmt_List);
5084 Set_Error_Posted (T);
5088 -- Perhaps the parent type should be changed to the class-wide type's
5089 -- specific type in this case to prevent cascading errors ???
5091 if Is_Class_Wide_Type (Parent_Type) then
5093 ("parent of type extension must not be a class-wide type", Indic);
5097 if (not Is_Package_Or_Generic_Package (Current_Scope)
5098 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5099 or else In_Private_Part (Current_Scope)
5101 Error_Msg_N ("invalid context for private extension", N);
5104 -- Set common attributes
5106 Set_Is_Pure (T, Is_Pure (Current_Scope));
5107 Set_Scope (T, Current_Scope);
5108 Set_Ekind (T, E_Record_Type_With_Private);
5109 Init_Size_Align (T);
5110 Set_Default_SSO (T);
5111 Set_No_Reordering (T, No_Component_Reordering);
5113 Set_Etype (T, Parent_Base);
5114 Propagate_Concurrent_Flags (T, Parent_Base);
5116 Set_Convention (T, Convention (Parent_Type));
5117 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5118 Set_Is_First_Subtype (T);
5119 Make_Class_Wide_Type (T);
5121 -- Set the SPARK mode from the current context
5123 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5124 Set_SPARK_Pragma_Inherited (T);
5126 if Unknown_Discriminants_Present (N) then
5127 Set_Discriminant_Constraint (T, No_Elist);
5130 Build_Derived_Record_Type (N, Parent_Type, T);
5132 -- A private extension inherits the Default_Initial_Condition pragma
5133 -- coming from any parent type within the derivation chain.
5135 if Has_DIC (Parent_Type) then
5136 Set_Has_Inherited_DIC (T);
5139 -- A private extension inherits any class-wide invariants coming from a
5140 -- parent type or an interface. Note that the invariant procedure of the
5141 -- parent type should not be inherited because the private extension may
5142 -- define invariants of its own.
5144 if Has_Inherited_Invariants (Parent_Type)
5145 or else Has_Inheritable_Invariants (Parent_Type)
5147 Set_Has_Inherited_Invariants (T);
5149 elsif Present (Interfaces (T)) then
5150 Iface_Elmt := First_Elmt (Interfaces (T));
5151 while Present (Iface_Elmt) loop
5152 Iface := Node (Iface_Elmt);
5154 if Has_Inheritable_Invariants (Iface) then
5155 Set_Has_Inherited_Invariants (T);
5159 Next_Elmt (Iface_Elmt);
5163 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5164 -- synchronized formal derived type.
5166 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5167 Set_Is_Limited_Record (T);
5169 -- Formal derived type case
5171 if Is_Generic_Type (T) then
5173 -- The parent must be a tagged limited type or a synchronized
5176 if (not Is_Tagged_Type (Parent_Type)
5177 or else not Is_Limited_Type (Parent_Type))
5179 (not Is_Interface (Parent_Type)
5180 or else not Is_Synchronized_Interface (Parent_Type))
5183 ("parent type of & must be tagged limited or synchronized",
5187 -- The progenitors (if any) must be limited or synchronized
5190 if Present (Interfaces (T)) then
5191 Iface_Elmt := First_Elmt (Interfaces (T));
5192 while Present (Iface_Elmt) loop
5193 Iface := Node (Iface_Elmt);
5195 if not Is_Limited_Interface (Iface)
5196 and then not Is_Synchronized_Interface (Iface)
5199 ("progenitor & must be limited or synchronized",
5203 Next_Elmt (Iface_Elmt);
5207 -- Regular derived extension, the parent must be a limited or
5208 -- synchronized interface.
5211 if not Is_Interface (Parent_Type)
5212 or else (not Is_Limited_Interface (Parent_Type)
5213 and then not Is_Synchronized_Interface (Parent_Type))
5216 ("parent type of & must be limited interface", N, T);
5220 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5221 -- extension with a synchronized parent must be explicitly declared
5222 -- synchronized, because the full view will be a synchronized type.
5223 -- This must be checked before the check for limited types below,
5224 -- to ensure that types declared limited are not allowed to extend
5225 -- synchronized interfaces.
5227 elsif Is_Interface (Parent_Type)
5228 and then Is_Synchronized_Interface (Parent_Type)
5229 and then not Synchronized_Present (N)
5232 ("private extension of& must be explicitly synchronized",
5235 elsif Limited_Present (N) then
5236 Set_Is_Limited_Record (T);
5238 if not Is_Limited_Type (Parent_Type)
5240 (not Is_Interface (Parent_Type)
5241 or else not Is_Limited_Interface (Parent_Type))
5243 Error_Msg_NE ("parent type& of limited extension must be limited",
5248 -- Remember that its parent type has a private extension. Used to warn
5249 -- on public primitives of the parent type defined after its private
5250 -- extensions (see Check_Dispatching_Operation).
5252 Set_Has_Private_Extension (Parent_Type);
5255 if Has_Aspects (N) then
5256 Analyze_Aspect_Specifications (N, T);
5258 end Analyze_Private_Extension_Declaration;
5260 ---------------------------------
5261 -- Analyze_Subtype_Declaration --
5262 ---------------------------------
5264 procedure Analyze_Subtype_Declaration
5266 Skip : Boolean := False)
5268 Id : constant Entity_Id := Defining_Identifier (N);
5269 R_Checks : Check_Result;
5273 Generate_Definition (Id);
5274 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5275 Init_Size_Align (Id);
5277 -- The following guard condition on Enter_Name is to handle cases where
5278 -- the defining identifier has already been entered into the scope but
5279 -- the declaration as a whole needs to be analyzed.
5281 -- This case in particular happens for derived enumeration types. The
5282 -- derived enumeration type is processed as an inserted enumeration type
5283 -- declaration followed by a rewritten subtype declaration. The defining
5284 -- identifier, however, is entered into the name scope very early in the
5285 -- processing of the original type declaration and therefore needs to be
5286 -- avoided here, when the created subtype declaration is analyzed. (See
5287 -- Build_Derived_Types)
5289 -- This also happens when the full view of a private type is derived
5290 -- type with constraints. In this case the entity has been introduced
5291 -- in the private declaration.
5293 -- Finally this happens in some complex cases when validity checks are
5294 -- enabled, where the same subtype declaration may be analyzed twice.
5295 -- This can happen if the subtype is created by the preanalysis of
5296 -- an attribute tht gives the range of a loop statement, and the loop
5297 -- itself appears within an if_statement that will be rewritten during
5301 or else (Present (Etype (Id))
5302 and then (Is_Private_Type (Etype (Id))
5303 or else Is_Task_Type (Etype (Id))
5304 or else Is_Rewrite_Substitution (N)))
5308 elsif Current_Entity (Id) = Id then
5315 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5317 -- Class-wide equivalent types of records with unknown discriminants
5318 -- involve the generation of an itype which serves as the private view
5319 -- of a constrained record subtype. In such cases the base type of the
5320 -- current subtype we are processing is the private itype. Use the full
5321 -- of the private itype when decorating various attributes.
5324 and then Is_Private_Type (T)
5325 and then Present (Full_View (T))
5330 -- Inherit common attributes
5332 Set_Is_Volatile (Id, Is_Volatile (T));
5333 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5334 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5335 Set_Convention (Id, Convention (T));
5337 -- If ancestor has predicates then so does the subtype, and in addition
5338 -- we must delay the freeze to properly arrange predicate inheritance.
5340 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5341 -- in which T = ID, so the above tests and assignments do nothing???
5343 if Has_Predicates (T)
5344 or else (Present (Ancestor_Subtype (T))
5345 and then Has_Predicates (Ancestor_Subtype (T)))
5347 Set_Has_Predicates (Id);
5348 Set_Has_Delayed_Freeze (Id);
5350 -- Generated subtypes inherit the predicate function from the parent
5351 -- (no aspects to examine on the generated declaration).
5353 if not Comes_From_Source (N) then
5354 Set_Ekind (Id, Ekind (T));
5356 if Present (Predicate_Function (Id)) then
5359 elsif Present (Predicate_Function (T)) then
5360 Set_Predicate_Function (Id, Predicate_Function (T));
5362 elsif Present (Ancestor_Subtype (T))
5363 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5365 Set_Predicate_Function (Id,
5366 Predicate_Function (Ancestor_Subtype (T)));
5371 -- In the case where there is no constraint given in the subtype
5372 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5373 -- semantic attributes must be established here.
5375 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5376 Set_Etype (Id, Base_Type (T));
5380 Set_Ekind (Id, E_Array_Subtype);
5381 Copy_Array_Subtype_Attributes (Id, T);
5383 when Decimal_Fixed_Point_Kind =>
5384 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5385 Set_Digits_Value (Id, Digits_Value (T));
5386 Set_Delta_Value (Id, Delta_Value (T));
5387 Set_Scale_Value (Id, Scale_Value (T));
5388 Set_Small_Value (Id, Small_Value (T));
5389 Set_Scalar_Range (Id, Scalar_Range (T));
5390 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5391 Set_Is_Constrained (Id, Is_Constrained (T));
5392 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5393 Set_RM_Size (Id, RM_Size (T));
5395 when Enumeration_Kind =>
5396 Set_Ekind (Id, E_Enumeration_Subtype);
5397 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5398 Set_Scalar_Range (Id, Scalar_Range (T));
5399 Set_Is_Character_Type (Id, Is_Character_Type (T));
5400 Set_Is_Constrained (Id, Is_Constrained (T));
5401 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5402 Set_RM_Size (Id, RM_Size (T));
5404 when Ordinary_Fixed_Point_Kind =>
5405 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5406 Set_Scalar_Range (Id, Scalar_Range (T));
5407 Set_Small_Value (Id, Small_Value (T));
5408 Set_Delta_Value (Id, Delta_Value (T));
5409 Set_Is_Constrained (Id, Is_Constrained (T));
5410 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5411 Set_RM_Size (Id, RM_Size (T));
5414 Set_Ekind (Id, E_Floating_Point_Subtype);
5415 Set_Scalar_Range (Id, Scalar_Range (T));
5416 Set_Digits_Value (Id, Digits_Value (T));
5417 Set_Is_Constrained (Id, Is_Constrained (T));
5419 -- If the floating point type has dimensions, these will be
5420 -- inherited subsequently when Analyze_Dimensions is called.
5422 when Signed_Integer_Kind =>
5423 Set_Ekind (Id, E_Signed_Integer_Subtype);
5424 Set_Scalar_Range (Id, Scalar_Range (T));
5425 Set_Is_Constrained (Id, Is_Constrained (T));
5426 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5427 Set_RM_Size (Id, RM_Size (T));
5429 when Modular_Integer_Kind =>
5430 Set_Ekind (Id, E_Modular_Integer_Subtype);
5431 Set_Scalar_Range (Id, Scalar_Range (T));
5432 Set_Is_Constrained (Id, Is_Constrained (T));
5433 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5434 Set_RM_Size (Id, RM_Size (T));
5436 when Class_Wide_Kind =>
5437 Set_Ekind (Id, E_Class_Wide_Subtype);
5438 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5439 Set_Cloned_Subtype (Id, T);
5440 Set_Is_Tagged_Type (Id, True);
5441 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5442 Set_Has_Unknown_Discriminants
5444 Set_No_Tagged_Streams_Pragma
5445 (Id, No_Tagged_Streams_Pragma (T));
5447 if Ekind (T) = E_Class_Wide_Subtype then
5448 Set_Equivalent_Type (Id, Equivalent_Type (T));
5451 when E_Record_Subtype
5454 Set_Ekind (Id, E_Record_Subtype);
5456 -- Subtype declarations introduced for formal type parameters
5457 -- in generic instantiations should inherit the Size value of
5458 -- the type they rename.
5460 if Present (Generic_Parent_Type (N)) then
5461 Set_RM_Size (Id, RM_Size (T));
5464 if Ekind (T) = E_Record_Subtype
5465 and then Present (Cloned_Subtype (T))
5467 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5469 Set_Cloned_Subtype (Id, T);
5472 Set_First_Entity (Id, First_Entity (T));
5473 Set_Last_Entity (Id, Last_Entity (T));
5474 Set_Has_Discriminants (Id, Has_Discriminants (T));
5475 Set_Is_Constrained (Id, Is_Constrained (T));
5476 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5477 Set_Has_Implicit_Dereference
5478 (Id, Has_Implicit_Dereference (T));
5479 Set_Has_Unknown_Discriminants
5480 (Id, Has_Unknown_Discriminants (T));
5482 if Has_Discriminants (T) then
5483 Set_Discriminant_Constraint
5484 (Id, Discriminant_Constraint (T));
5485 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5487 elsif Has_Unknown_Discriminants (Id) then
5488 Set_Discriminant_Constraint (Id, No_Elist);
5491 if Is_Tagged_Type (T) then
5492 Set_Is_Tagged_Type (Id, True);
5493 Set_No_Tagged_Streams_Pragma
5494 (Id, No_Tagged_Streams_Pragma (T));
5495 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5496 Set_Direct_Primitive_Operations
5497 (Id, Direct_Primitive_Operations (T));
5498 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5500 if Is_Interface (T) then
5501 Set_Is_Interface (Id);
5502 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5506 when Private_Kind =>
5507 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5508 Set_Has_Discriminants (Id, Has_Discriminants (T));
5509 Set_Is_Constrained (Id, Is_Constrained (T));
5510 Set_First_Entity (Id, First_Entity (T));
5511 Set_Last_Entity (Id, Last_Entity (T));
5512 Set_Private_Dependents (Id, New_Elmt_List);
5513 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5514 Set_Has_Implicit_Dereference
5515 (Id, Has_Implicit_Dereference (T));
5516 Set_Has_Unknown_Discriminants
5517 (Id, Has_Unknown_Discriminants (T));
5518 Set_Known_To_Have_Preelab_Init
5519 (Id, Known_To_Have_Preelab_Init (T));
5521 if Is_Tagged_Type (T) then
5522 Set_Is_Tagged_Type (Id);
5523 Set_No_Tagged_Streams_Pragma (Id,
5524 No_Tagged_Streams_Pragma (T));
5525 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5526 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5527 Set_Direct_Primitive_Operations (Id,
5528 Direct_Primitive_Operations (T));
5531 -- In general the attributes of the subtype of a private type
5532 -- are the attributes of the partial view of parent. However,
5533 -- the full view may be a discriminated type, and the subtype
5534 -- must share the discriminant constraint to generate correct
5535 -- calls to initialization procedures.
5537 if Has_Discriminants (T) then
5538 Set_Discriminant_Constraint
5539 (Id, Discriminant_Constraint (T));
5540 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5542 elsif Present (Full_View (T))
5543 and then Has_Discriminants (Full_View (T))
5545 Set_Discriminant_Constraint
5546 (Id, Discriminant_Constraint (Full_View (T)));
5547 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5549 -- This would seem semantically correct, but apparently
5550 -- generates spurious errors about missing components ???
5552 -- Set_Has_Discriminants (Id);
5555 Prepare_Private_Subtype_Completion (Id, N);
5557 -- If this is the subtype of a constrained private type with
5558 -- discriminants that has got a full view and we also have
5559 -- built a completion just above, show that the completion
5560 -- is a clone of the full view to the back-end.
5562 if Has_Discriminants (T)
5563 and then not Has_Unknown_Discriminants (T)
5564 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5565 and then Present (Full_View (T))
5566 and then Present (Full_View (Id))
5568 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5572 Set_Ekind (Id, E_Access_Subtype);
5573 Set_Is_Constrained (Id, Is_Constrained (T));
5574 Set_Is_Access_Constant
5575 (Id, Is_Access_Constant (T));
5576 Set_Directly_Designated_Type
5577 (Id, Designated_Type (T));
5578 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5580 -- A Pure library_item must not contain the declaration of a
5581 -- named access type, except within a subprogram, generic
5582 -- subprogram, task unit, or protected unit, or if it has
5583 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5585 if Comes_From_Source (Id)
5586 and then In_Pure_Unit
5587 and then not In_Subprogram_Task_Protected_Unit
5588 and then not No_Pool_Assigned (Id)
5591 ("named access types not allowed in pure unit", N);
5594 when Concurrent_Kind =>
5595 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5596 Set_Corresponding_Record_Type (Id,
5597 Corresponding_Record_Type (T));
5598 Set_First_Entity (Id, First_Entity (T));
5599 Set_First_Private_Entity (Id, First_Private_Entity (T));
5600 Set_Has_Discriminants (Id, Has_Discriminants (T));
5601 Set_Is_Constrained (Id, Is_Constrained (T));
5602 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5603 Set_Last_Entity (Id, Last_Entity (T));
5605 if Is_Tagged_Type (T) then
5606 Set_No_Tagged_Streams_Pragma
5607 (Id, No_Tagged_Streams_Pragma (T));
5610 if Has_Discriminants (T) then
5611 Set_Discriminant_Constraint
5612 (Id, Discriminant_Constraint (T));
5613 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5616 when Incomplete_Kind =>
5617 if Ada_Version >= Ada_2005 then
5619 -- In Ada 2005 an incomplete type can be explicitly tagged:
5620 -- propagate indication. Note that we also have to include
5621 -- subtypes for Ada 2012 extended use of incomplete types.
5623 Set_Ekind (Id, E_Incomplete_Subtype);
5624 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5625 Set_Private_Dependents (Id, New_Elmt_List);
5627 if Is_Tagged_Type (Id) then
5628 Set_No_Tagged_Streams_Pragma
5629 (Id, No_Tagged_Streams_Pragma (T));
5630 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5633 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5634 -- incomplete type visible through a limited with clause.
5636 if From_Limited_With (T)
5637 and then Present (Non_Limited_View (T))
5639 Set_From_Limited_With (Id);
5640 Set_Non_Limited_View (Id, Non_Limited_View (T));
5642 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5643 -- to the private dependents of the original incomplete
5644 -- type for future transformation.
5647 Append_Elmt (Id, Private_Dependents (T));
5650 -- If the subtype name denotes an incomplete type an error
5651 -- was already reported by Process_Subtype.
5654 Set_Etype (Id, Any_Type);
5658 raise Program_Error;
5661 -- If there is no constraint in the subtype indication, the
5662 -- declared entity inherits predicates from the parent.
5664 Inherit_Predicate_Flags (Id, T);
5667 if Etype (Id) = Any_Type then
5671 -- Some common processing on all types
5673 Set_Size_Info (Id, T);
5674 Set_First_Rep_Item (Id, First_Rep_Item (T));
5676 -- If the parent type is a generic actual, so is the subtype. This may
5677 -- happen in a nested instance. Why Comes_From_Source test???
5679 if not Comes_From_Source (N) then
5680 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5683 -- If this is a subtype declaration for an actual in an instance,
5684 -- inherit static and dynamic predicates if any.
5686 -- If declaration has no aspect specifications, inherit predicate
5687 -- info as well. Unclear how to handle the case of both specified
5688 -- and inherited predicates ??? Other inherited aspects, such as
5689 -- invariants, should be OK, but the combination with later pragmas
5690 -- may also require special merging.
5692 if Has_Predicates (T)
5693 and then Present (Predicate_Function (T))
5695 ((In_Instance and then not Comes_From_Source (N))
5696 or else No (Aspect_Specifications (N)))
5698 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5700 if Has_Static_Predicate (T) then
5701 Set_Has_Static_Predicate (Id);
5702 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5706 -- If the base type is a scalar type, or else if there is no
5707 -- constraint, the atomic flag is inherited by the subtype.
5709 if Is_Scalar_Type (Id)
5710 or else Is_Entity_Name (Subtype_Indication (N))
5712 Set_Is_Atomic (Id, Is_Atomic (T));
5715 -- Remaining processing depends on characteristics of base type
5719 Set_Is_Immediately_Visible (Id, True);
5720 Set_Depends_On_Private (Id, Has_Private_Component (T));
5721 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5723 if Is_Interface (T) then
5724 Set_Is_Interface (Id);
5725 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5728 if Present (Generic_Parent_Type (N))
5730 (Nkind (Parent (Generic_Parent_Type (N))) /=
5731 N_Formal_Type_Declaration
5732 or else Nkind (Formal_Type_Definition
5733 (Parent (Generic_Parent_Type (N)))) /=
5734 N_Formal_Private_Type_Definition)
5736 if Is_Tagged_Type (Id) then
5738 -- If this is a generic actual subtype for a synchronized type,
5739 -- the primitive operations are those of the corresponding record
5740 -- for which there is a separate subtype declaration.
5742 if Is_Concurrent_Type (Id) then
5744 elsif Is_Class_Wide_Type (Id) then
5745 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5747 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5750 elsif Scope (Etype (Id)) /= Standard_Standard then
5751 Derive_Subprograms (Generic_Parent_Type (N), Id);
5755 if Is_Private_Type (T) and then Present (Full_View (T)) then
5756 Conditional_Delay (Id, Full_View (T));
5758 -- The subtypes of components or subcomponents of protected types
5759 -- do not need freeze nodes, which would otherwise appear in the
5760 -- wrong scope (before the freeze node for the protected type). The
5761 -- proper subtypes are those of the subcomponents of the corresponding
5764 elsif Ekind (Scope (Id)) /= E_Protected_Type
5765 and then Present (Scope (Scope (Id))) -- error defense
5766 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5768 Conditional_Delay (Id, T);
5771 -- If we have a subtype of an incomplete type whose full type is a
5772 -- derived numeric type, we need to have a freeze node for the subtype.
5773 -- Otherwise gigi will complain while computing the (static) bounds of
5777 and then Is_Elementary_Type (Id)
5778 and then Etype (Id) /= Id
5781 Partial : constant Entity_Id :=
5782 Incomplete_Or_Partial_View (First_Subtype (Id));
5784 if Present (Partial)
5785 and then Ekind (Partial) = E_Incomplete_Type
5787 Set_Has_Delayed_Freeze (Id);
5792 -- Check that Constraint_Error is raised for a scalar subtype indication
5793 -- when the lower or upper bound of a non-null range lies outside the
5794 -- range of the type mark.
5796 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5797 if Is_Scalar_Type (Etype (Id))
5798 and then Scalar_Range (Id) /=
5800 (Etype (Subtype_Mark (Subtype_Indication (N))))
5804 Etype (Subtype_Mark (Subtype_Indication (N))));
5806 -- In the array case, check compatibility for each index
5808 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5810 -- This really should be a subprogram that finds the indications
5814 Subt_Index : Node_Id := First_Index (Id);
5815 Target_Index : Node_Id :=
5817 (Subtype_Mark (Subtype_Indication (N))));
5820 while Present (Subt_Index) loop
5821 if ((Nkind (Subt_Index) = N_Identifier
5822 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5823 or else Nkind (Subt_Index) = N_Subtype_Indication)
5825 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5828 Target_Typ : constant Entity_Id :=
5829 Etype (Target_Index);
5833 (Scalar_Range (Etype (Subt_Index)),
5836 Defining_Identifier (N));
5842 Sloc (Defining_Identifier (N)));
5846 Next_Index (Subt_Index);
5847 Next_Index (Target_Index);
5853 Set_Optimize_Alignment_Flags (Id);
5854 Check_Eliminated (Id);
5857 if Has_Aspects (N) then
5858 Analyze_Aspect_Specifications (N, Id);
5861 Analyze_Dimension (N);
5863 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5864 -- indications on composite types where the constraints are dynamic.
5865 -- Note that object declarations and aggregates generate implicit
5866 -- subtype declarations, which this covers. One special case is that the
5867 -- implicitly generated "=" for discriminated types includes an
5868 -- offending subtype declaration, which is harmless, so we ignore it
5871 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5873 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5875 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5876 and then not (Is_Internal (Id)
5877 and then Is_TSS (Scope (Id),
5878 TSS_Composite_Equality))
5879 and then not Within_Init_Proc
5880 and then not All_Composite_Constraints_Static (Cstr)
5882 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5886 end Analyze_Subtype_Declaration;
5888 --------------------------------
5889 -- Analyze_Subtype_Indication --
5890 --------------------------------
5892 procedure Analyze_Subtype_Indication (N : Node_Id) is
5893 T : constant Entity_Id := Subtype_Mark (N);
5894 R : constant Node_Id := Range_Expression (Constraint (N));
5901 Set_Etype (N, Etype (R));
5902 Resolve (R, Entity (T));
5904 Set_Error_Posted (R);
5905 Set_Error_Posted (T);
5907 end Analyze_Subtype_Indication;
5909 --------------------------
5910 -- Analyze_Variant_Part --
5911 --------------------------
5913 procedure Analyze_Variant_Part (N : Node_Id) is
5914 Discr_Name : Node_Id;
5915 Discr_Type : Entity_Id;
5917 procedure Process_Variant (A : Node_Id);
5918 -- Analyze declarations for a single variant
5920 package Analyze_Variant_Choices is
5921 new Generic_Analyze_Choices (Process_Variant);
5922 use Analyze_Variant_Choices;
5924 ---------------------
5925 -- Process_Variant --
5926 ---------------------
5928 procedure Process_Variant (A : Node_Id) is
5929 CL : constant Node_Id := Component_List (A);
5931 if not Null_Present (CL) then
5932 Analyze_Declarations (Component_Items (CL));
5934 if Present (Variant_Part (CL)) then
5935 Analyze (Variant_Part (CL));
5938 end Process_Variant;
5940 -- Start of processing for Analyze_Variant_Part
5943 Discr_Name := Name (N);
5944 Analyze (Discr_Name);
5946 -- If Discr_Name bad, get out (prevent cascaded errors)
5948 if Etype (Discr_Name) = Any_Type then
5952 -- Check invalid discriminant in variant part
5954 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5955 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5958 Discr_Type := Etype (Entity (Discr_Name));
5960 if not Is_Discrete_Type (Discr_Type) then
5962 ("discriminant in a variant part must be of a discrete type",
5967 -- Now analyze the choices, which also analyzes the declarations that
5968 -- are associated with each choice.
5970 Analyze_Choices (Variants (N), Discr_Type);
5972 -- Note: we used to instantiate and call Check_Choices here to check
5973 -- that the choices covered the discriminant, but it's too early to do
5974 -- that because of statically predicated subtypes, whose analysis may
5975 -- be deferred to their freeze point which may be as late as the freeze
5976 -- point of the containing record. So this call is now to be found in
5977 -- Freeze_Record_Declaration.
5979 end Analyze_Variant_Part;
5981 ----------------------------
5982 -- Array_Type_Declaration --
5983 ----------------------------
5985 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5986 Component_Def : constant Node_Id := Component_Definition (Def);
5987 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5988 P : constant Node_Id := Parent (Def);
5989 Element_Type : Entity_Id;
5990 Implicit_Base : Entity_Id;
5994 Related_Id : Entity_Id := Empty;
5997 if Nkind (Def) = N_Constrained_Array_Definition then
5998 Index := First (Discrete_Subtype_Definitions (Def));
6000 Index := First (Subtype_Marks (Def));
6003 -- Find proper names for the implicit types which may be public. In case
6004 -- of anonymous arrays we use the name of the first object of that type
6008 Related_Id := Defining_Identifier (P);
6014 while Present (Index) loop
6017 -- Test for odd case of trying to index a type by the type itself
6019 if Is_Entity_Name (Index) and then Entity (Index) = T then
6020 Error_Msg_N ("type& cannot be indexed by itself", Index);
6021 Set_Entity (Index, Standard_Boolean);
6022 Set_Etype (Index, Standard_Boolean);
6025 -- Add a subtype declaration for each index of private array type
6026 -- declaration whose type is also private. For example:
6029 -- type Index is private;
6031 -- type Table is array (Index) of ...
6034 -- This is currently required by the expander for the internally
6035 -- generated equality subprogram of records with variant parts in
6036 -- which the type of some component is such a private type. And it
6037 -- also helps semantic analysis in peculiar cases where the array
6038 -- type is referenced from an instance but not the index directly.
6040 if Is_Package_Or_Generic_Package (Current_Scope)
6041 and then In_Private_Part (Current_Scope)
6042 and then Has_Private_Declaration (Etype (Index))
6043 and then Scope (Etype (Index)) = Current_Scope
6046 Loc : constant Source_Ptr := Sloc (Def);
6051 New_E := Make_Temporary (Loc, 'T');
6052 Set_Is_Internal (New_E);
6055 Make_Subtype_Declaration (Loc,
6056 Defining_Identifier => New_E,
6057 Subtype_Indication =>
6058 New_Occurrence_Of (Etype (Index), Loc));
6060 Insert_Before (Parent (Def), Decl);
6062 Set_Etype (Index, New_E);
6064 -- If the index is a range or a subtype indication it carries
6065 -- no entity. Example:
6068 -- type T is private;
6070 -- type T is new Natural;
6071 -- Table : array (T(1) .. T(10)) of Boolean;
6074 -- Otherwise the type of the reference is its entity.
6076 if Is_Entity_Name (Index) then
6077 Set_Entity (Index, New_E);
6082 Make_Index (Index, P, Related_Id, Nb_Index);
6084 -- Check error of subtype with predicate for index type
6086 Bad_Predicated_Subtype_Use
6087 ("subtype& has predicate, not allowed as index subtype",
6088 Index, Etype (Index));
6090 -- Move to next index
6093 Nb_Index := Nb_Index + 1;
6096 -- Process subtype indication if one is present
6098 if Present (Component_Typ) then
6099 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6100 Set_Etype (Component_Typ, Element_Type);
6102 -- Ada 2005 (AI-230): Access Definition case
6104 else pragma Assert (Present (Access_Definition (Component_Def)));
6106 -- Indicate that the anonymous access type is created by the
6107 -- array type declaration.
6109 Element_Type := Access_Definition
6111 N => Access_Definition (Component_Def));
6112 Set_Is_Local_Anonymous_Access (Element_Type);
6114 -- Propagate the parent. This field is needed if we have to generate
6115 -- the master_id associated with an anonymous access to task type
6116 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6118 Set_Parent (Element_Type, Parent (T));
6120 -- Ada 2005 (AI-230): In case of components that are anonymous access
6121 -- types the level of accessibility depends on the enclosing type
6124 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6126 -- Ada 2005 (AI-254)
6129 CD : constant Node_Id :=
6130 Access_To_Subprogram_Definition
6131 (Access_Definition (Component_Def));
6133 if Present (CD) and then Protected_Present (CD) then
6135 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6140 -- Constrained array case
6143 T := Create_Itype (E_Void, P, Related_Id, 'T');
6146 if Nkind (Def) = N_Constrained_Array_Definition then
6148 -- Establish Implicit_Base as unconstrained base type
6150 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6152 Set_Etype (Implicit_Base, Implicit_Base);
6153 Set_Scope (Implicit_Base, Current_Scope);
6154 Set_Has_Delayed_Freeze (Implicit_Base);
6155 Set_Default_SSO (Implicit_Base);
6157 -- The constrained array type is a subtype of the unconstrained one
6159 Set_Ekind (T, E_Array_Subtype);
6160 Init_Size_Align (T);
6161 Set_Etype (T, Implicit_Base);
6162 Set_Scope (T, Current_Scope);
6163 Set_Is_Constrained (T);
6165 First (Discrete_Subtype_Definitions (Def)));
6166 Set_Has_Delayed_Freeze (T);
6168 -- Complete setup of implicit base type
6170 Set_Component_Size (Implicit_Base, Uint_0);
6171 Set_Component_Type (Implicit_Base, Element_Type);
6172 Set_Finalize_Storage_Only
6174 Finalize_Storage_Only (Element_Type));
6175 Set_First_Index (Implicit_Base, First_Index (T));
6176 Set_Has_Controlled_Component
6178 Has_Controlled_Component (Element_Type)
6179 or else Is_Controlled (Element_Type));
6180 Set_Packed_Array_Impl_Type
6181 (Implicit_Base, Empty);
6183 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6185 -- Unconstrained array case
6188 Set_Ekind (T, E_Array_Type);
6189 Init_Size_Align (T);
6191 Set_Scope (T, Current_Scope);
6192 Set_Component_Size (T, Uint_0);
6193 Set_Is_Constrained (T, False);
6194 Set_First_Index (T, First (Subtype_Marks (Def)));
6195 Set_Has_Delayed_Freeze (T, True);
6196 Propagate_Concurrent_Flags (T, Element_Type);
6197 Set_Has_Controlled_Component (T, Has_Controlled_Component
6200 Is_Controlled (Element_Type));
6201 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6203 Set_Default_SSO (T);
6206 -- Common attributes for both cases
6208 Set_Component_Type (Base_Type (T), Element_Type);
6209 Set_Packed_Array_Impl_Type (T, Empty);
6211 if Aliased_Present (Component_Definition (Def)) then
6212 Set_Has_Aliased_Components (Etype (T));
6214 -- AI12-001: All aliased objects are considered to be specified as
6215 -- independently addressable (RM C.6(8.1/4)).
6217 Set_Has_Independent_Components (Etype (T));
6220 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6221 -- array type to ensure that objects of this type are initialized.
6223 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6224 Set_Can_Never_Be_Null (T);
6226 if Null_Exclusion_Present (Component_Definition (Def))
6228 -- No need to check itypes because in their case this check was
6229 -- done at their point of creation
6231 and then not Is_Itype (Element_Type)
6234 ("`NOT NULL` not allowed (null already excluded)",
6235 Subtype_Indication (Component_Definition (Def)));
6239 Priv := Private_Component (Element_Type);
6241 if Present (Priv) then
6243 -- Check for circular definitions
6245 if Priv = Any_Type then
6246 Set_Component_Type (Etype (T), Any_Type);
6248 -- There is a gap in the visibility of operations on the composite
6249 -- type only if the component type is defined in a different scope.
6251 elsif Scope (Priv) = Current_Scope then
6254 elsif Is_Limited_Type (Priv) then
6255 Set_Is_Limited_Composite (Etype (T));
6256 Set_Is_Limited_Composite (T);
6258 Set_Is_Private_Composite (Etype (T));
6259 Set_Is_Private_Composite (T);
6263 -- A syntax error in the declaration itself may lead to an empty index
6264 -- list, in which case do a minimal patch.
6266 if No (First_Index (T)) then
6267 Error_Msg_N ("missing index definition in array type declaration", T);
6270 Indexes : constant List_Id :=
6271 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6273 Set_Discrete_Subtype_Definitions (Def, Indexes);
6274 Set_First_Index (T, First (Indexes));
6279 -- Create a concatenation operator for the new type. Internal array
6280 -- types created for packed entities do not need such, they are
6281 -- compatible with the user-defined type.
6283 if Number_Dimensions (T) = 1
6284 and then not Is_Packed_Array_Impl_Type (T)
6286 New_Concatenation_Op (T);
6289 -- In the case of an unconstrained array the parser has already verified
6290 -- that all the indexes are unconstrained but we still need to make sure
6291 -- that the element type is constrained.
6293 if not Is_Definite_Subtype (Element_Type) then
6295 ("unconstrained element type in array declaration",
6296 Subtype_Indication (Component_Def));
6298 elsif Is_Abstract_Type (Element_Type) then
6300 ("the type of a component cannot be abstract",
6301 Subtype_Indication (Component_Def));
6304 -- There may be an invariant declared for the component type, but
6305 -- the construction of the component invariant checking procedure
6306 -- takes place during expansion.
6307 end Array_Type_Declaration;
6309 ------------------------------------------------------
6310 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6311 ------------------------------------------------------
6313 function Replace_Anonymous_Access_To_Protected_Subprogram
6314 (N : Node_Id) return Entity_Id
6316 Loc : constant Source_Ptr := Sloc (N);
6318 Curr_Scope : constant Scope_Stack_Entry :=
6319 Scope_Stack.Table (Scope_Stack.Last);
6321 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6324 -- Access definition in declaration
6327 -- Object definition or formal definition with an access definition
6330 -- Declaration of anonymous access to subprogram type
6333 -- Original specification in access to subprogram
6338 Set_Is_Internal (Anon);
6341 when N_Constrained_Array_Definition
6342 | N_Component_Declaration
6343 | N_Unconstrained_Array_Definition
6345 Comp := Component_Definition (N);
6346 Acc := Access_Definition (Comp);
6348 when N_Discriminant_Specification =>
6349 Comp := Discriminant_Type (N);
6352 when N_Parameter_Specification =>
6353 Comp := Parameter_Type (N);
6356 when N_Access_Function_Definition =>
6357 Comp := Result_Definition (N);
6360 when N_Object_Declaration =>
6361 Comp := Object_Definition (N);
6364 when N_Function_Specification =>
6365 Comp := Result_Definition (N);
6369 raise Program_Error;
6372 Spec := Access_To_Subprogram_Definition (Acc);
6375 Make_Full_Type_Declaration (Loc,
6376 Defining_Identifier => Anon,
6377 Type_Definition => Copy_Separate_Tree (Spec));
6379 Mark_Rewrite_Insertion (Decl);
6381 -- Insert the new declaration in the nearest enclosing scope. If the
6382 -- parent is a body and N is its return type, the declaration belongs
6383 -- in the enclosing scope. Likewise if N is the type of a parameter.
6387 if Nkind (N) = N_Function_Specification
6388 and then Nkind (P) = N_Subprogram_Body
6391 elsif Nkind (N) = N_Parameter_Specification
6392 and then Nkind (P) in N_Subprogram_Specification
6393 and then Nkind (Parent (P)) = N_Subprogram_Body
6395 P := Parent (Parent (P));
6398 while Present (P) and then not Has_Declarations (P) loop
6402 pragma Assert (Present (P));
6404 if Nkind (P) = N_Package_Specification then
6405 Prepend (Decl, Visible_Declarations (P));
6407 Prepend (Decl, Declarations (P));
6410 -- Replace the anonymous type with an occurrence of the new declaration.
6411 -- In all cases the rewritten node does not have the null-exclusion
6412 -- attribute because (if present) it was already inherited by the
6413 -- anonymous entity (Anon). Thus, in case of components we do not
6414 -- inherit this attribute.
6416 if Nkind (N) = N_Parameter_Specification then
6417 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6418 Set_Etype (Defining_Identifier (N), Anon);
6419 Set_Null_Exclusion_Present (N, False);
6421 elsif Nkind (N) = N_Object_Declaration then
6422 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6423 Set_Etype (Defining_Identifier (N), Anon);
6425 elsif Nkind (N) = N_Access_Function_Definition then
6426 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6428 elsif Nkind (N) = N_Function_Specification then
6429 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6430 Set_Etype (Defining_Unit_Name (N), Anon);
6434 Make_Component_Definition (Loc,
6435 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6438 Mark_Rewrite_Insertion (Comp);
6440 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6441 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6442 and then not Is_Type (Current_Scope))
6445 -- Declaration can be analyzed in the current scope.
6450 -- Temporarily remove the current scope (record or subprogram) from
6451 -- the stack to add the new declarations to the enclosing scope.
6452 -- The anonymous entity is an Itype with the proper attributes.
6454 Scope_Stack.Decrement_Last;
6456 Set_Is_Itype (Anon);
6457 Set_Associated_Node_For_Itype (Anon, N);
6458 Scope_Stack.Append (Curr_Scope);
6461 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6462 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6464 end Replace_Anonymous_Access_To_Protected_Subprogram;
6466 -------------------------------------
6467 -- Build_Access_Subprogram_Wrapper --
6468 -------------------------------------
6470 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id) is
6471 Loc : constant Source_Ptr := Sloc (Decl);
6472 Id : constant Entity_Id := Defining_Identifier (Decl);
6473 Type_Def : constant Node_Id := Type_Definition (Decl);
6474 Specs : constant List_Id :=
6475 Parameter_Specifications (Type_Def);
6476 Profile : constant List_Id := New_List;
6477 Subp : constant Entity_Id := Make_Temporary (Loc, 'A');
6479 Contracts : constant List_Id := New_List;
6485 procedure Replace_Type_Name (Expr : Node_Id);
6486 -- In the expressions for contract aspects, replace occurrences of the
6487 -- access type with the name of the subprogram entity, as needed, e.g.
6488 -- for 'Result. Aspects that are not contracts, e.g. Size or Alignment)
6489 -- remain on the original access type declaration. What about expanded
6490 -- names denoting formals, whose prefix in source is the type name ???
6492 -----------------------
6493 -- Replace_Type_Name --
6494 -----------------------
6496 procedure Replace_Type_Name (Expr : Node_Id) is
6497 function Process (N : Node_Id) return Traverse_Result;
6498 function Process (N : Node_Id) return Traverse_Result is
6500 if Nkind (N) = N_Attribute_Reference
6501 and then Is_Entity_Name (Prefix (N))
6502 and then Chars (Prefix (N)) = Chars (Id)
6504 Set_Prefix (N, Make_Identifier (Sloc (N), Chars (Subp)));
6510 procedure Traverse is new Traverse_Proc (Process);
6513 end Replace_Type_Name;
6516 if Ekind_In (Id, E_Access_Subprogram_Type,
6517 E_Access_Protected_Subprogram_Type,
6518 E_Anonymous_Access_Protected_Subprogram_Type,
6519 E_Anonymous_Access_Subprogram_Type)
6525 ("illegal pre/postcondition on access type", Decl);
6536 Asp := First (Aspect_Specifications (Decl));
6537 while Present (Asp) loop
6538 A_Id := Get_Aspect_Id (Chars (Identifier (Asp)));
6539 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
6541 Expr := Expression (Cond);
6542 Replace_Type_Name (Expr);
6546 Append (Cond, Contracts);
6554 -- If there are no contract aspects, no need for a wrapper.
6556 if Is_Empty_List (Contracts) then
6560 Form_P := First (Specs);
6562 while Present (Form_P) loop
6563 New_P := New_Copy_Tree (Form_P);
6564 Set_Defining_Identifier (New_P,
6565 Make_Defining_Identifier
6566 (Loc, Chars (Defining_Identifier (Form_P))));
6567 Append (New_P, Profile);
6571 -- Add to parameter specifications the access parameter that is passed
6572 -- in from an indirect call.
6575 Make_Parameter_Specification (Loc,
6576 Defining_Identifier => Make_Temporary (Loc, 'P'),
6577 Parameter_Type => New_Occurrence_Of (Id, Loc)),
6580 if Nkind (Type_Def) = N_Access_Procedure_Definition then
6582 Make_Procedure_Specification (Loc,
6583 Defining_Unit_Name => Subp,
6584 Parameter_Specifications => Profile);
6587 Make_Function_Specification (Loc,
6588 Defining_Unit_Name => Subp,
6589 Parameter_Specifications => Profile,
6590 Result_Definition =>
6592 (Result_Definition (Type_Definition (Decl))));
6596 Make_Subprogram_Declaration (Loc, Specification => Spec);
6597 Set_Aspect_Specifications (New_Decl, Contracts);
6599 Insert_After (Decl, New_Decl);
6600 Set_Access_Subprogram_Wrapper (Designated_Type (Id), Subp);
6601 Build_Access_Subprogram_Wrapper_Body (Decl, New_Decl);
6602 end Build_Access_Subprogram_Wrapper;
6604 -------------------------------
6605 -- Build_Derived_Access_Type --
6606 -------------------------------
6608 procedure Build_Derived_Access_Type
6610 Parent_Type : Entity_Id;
6611 Derived_Type : Entity_Id)
6613 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6615 Desig_Type : Entity_Id;
6617 Discr_Con_Elist : Elist_Id;
6618 Discr_Con_El : Elmt_Id;
6622 -- Set the designated type so it is available in case this is an access
6623 -- to a self-referential type, e.g. a standard list type with a next
6624 -- pointer. Will be reset after subtype is built.
6626 Set_Directly_Designated_Type
6627 (Derived_Type, Designated_Type (Parent_Type));
6629 Subt := Process_Subtype (S, N);
6631 if Nkind (S) /= N_Subtype_Indication
6632 and then Subt /= Base_Type (Subt)
6634 Set_Ekind (Derived_Type, E_Access_Subtype);
6637 if Ekind (Derived_Type) = E_Access_Subtype then
6639 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6640 Ibase : constant Entity_Id :=
6641 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6642 Svg_Chars : constant Name_Id := Chars (Ibase);
6643 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6644 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
6647 Copy_Node (Pbase, Ibase);
6649 -- Restore Itype status after Copy_Node
6651 Set_Is_Itype (Ibase);
6652 Set_Associated_Node_For_Itype (Ibase, N);
6654 Set_Chars (Ibase, Svg_Chars);
6655 Set_Prev_Entity (Ibase, Svg_Prev_E);
6656 Set_Next_Entity (Ibase, Svg_Next_E);
6657 Set_Sloc (Ibase, Sloc (Derived_Type));
6658 Set_Scope (Ibase, Scope (Derived_Type));
6659 Set_Freeze_Node (Ibase, Empty);
6660 Set_Is_Frozen (Ibase, False);
6661 Set_Comes_From_Source (Ibase, False);
6662 Set_Is_First_Subtype (Ibase, False);
6664 Set_Etype (Ibase, Pbase);
6665 Set_Etype (Derived_Type, Ibase);
6669 Set_Directly_Designated_Type
6670 (Derived_Type, Designated_Type (Subt));
6672 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6673 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6674 Set_Size_Info (Derived_Type, Parent_Type);
6675 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6676 Set_Depends_On_Private (Derived_Type,
6677 Has_Private_Component (Derived_Type));
6678 Conditional_Delay (Derived_Type, Subt);
6680 if Is_Access_Subprogram_Type (Derived_Type) then
6681 Set_Can_Use_Internal_Rep
6682 (Derived_Type, Can_Use_Internal_Rep (Parent_Type));
6685 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6686 -- that it is not redundant.
6688 if Null_Exclusion_Present (Type_Definition (N)) then
6689 Set_Can_Never_Be_Null (Derived_Type);
6691 elsif Can_Never_Be_Null (Parent_Type) then
6692 Set_Can_Never_Be_Null (Derived_Type);
6695 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6696 -- the root type for this information.
6698 -- Apply range checks to discriminants for derived record case
6699 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6701 Desig_Type := Designated_Type (Derived_Type);
6703 if Is_Composite_Type (Desig_Type)
6704 and then (not Is_Array_Type (Desig_Type))
6705 and then Has_Discriminants (Desig_Type)
6706 and then Base_Type (Desig_Type) /= Desig_Type
6708 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6709 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6711 Discr := First_Discriminant (Base_Type (Desig_Type));
6712 while Present (Discr_Con_El) loop
6713 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6714 Next_Elmt (Discr_Con_El);
6715 Next_Discriminant (Discr);
6718 end Build_Derived_Access_Type;
6720 ------------------------------
6721 -- Build_Derived_Array_Type --
6722 ------------------------------
6724 procedure Build_Derived_Array_Type
6726 Parent_Type : Entity_Id;
6727 Derived_Type : Entity_Id)
6729 Loc : constant Source_Ptr := Sloc (N);
6730 Tdef : constant Node_Id := Type_Definition (N);
6731 Indic : constant Node_Id := Subtype_Indication (Tdef);
6732 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6733 Implicit_Base : Entity_Id := Empty;
6734 New_Indic : Node_Id;
6736 procedure Make_Implicit_Base;
6737 -- If the parent subtype is constrained, the derived type is a subtype
6738 -- of an implicit base type derived from the parent base.
6740 ------------------------
6741 -- Make_Implicit_Base --
6742 ------------------------
6744 procedure Make_Implicit_Base is
6747 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6749 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6750 Set_Etype (Implicit_Base, Parent_Base);
6752 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6753 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6755 Set_Has_Delayed_Freeze (Implicit_Base, True);
6756 end Make_Implicit_Base;
6758 -- Start of processing for Build_Derived_Array_Type
6761 if not Is_Constrained (Parent_Type) then
6762 if Nkind (Indic) /= N_Subtype_Indication then
6763 Set_Ekind (Derived_Type, E_Array_Type);
6765 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6766 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6768 Set_Has_Delayed_Freeze (Derived_Type, True);
6772 Set_Etype (Derived_Type, Implicit_Base);
6775 Make_Subtype_Declaration (Loc,
6776 Defining_Identifier => Derived_Type,
6777 Subtype_Indication =>
6778 Make_Subtype_Indication (Loc,
6779 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6780 Constraint => Constraint (Indic)));
6782 Rewrite (N, New_Indic);
6787 if Nkind (Indic) /= N_Subtype_Indication then
6790 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6791 Set_Etype (Derived_Type, Implicit_Base);
6792 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6795 Error_Msg_N ("illegal constraint on constrained type", Indic);
6799 -- If parent type is not a derived type itself, and is declared in
6800 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6801 -- the new type's concatenation operator since Derive_Subprograms
6802 -- will not inherit the parent's operator. If the parent type is
6803 -- unconstrained, the operator is of the unconstrained base type.
6805 if Number_Dimensions (Parent_Type) = 1
6806 and then not Is_Limited_Type (Parent_Type)
6807 and then not Is_Derived_Type (Parent_Type)
6808 and then not Is_Package_Or_Generic_Package
6809 (Scope (Base_Type (Parent_Type)))
6811 if not Is_Constrained (Parent_Type)
6812 and then Is_Constrained (Derived_Type)
6814 New_Concatenation_Op (Implicit_Base);
6816 New_Concatenation_Op (Derived_Type);
6819 end Build_Derived_Array_Type;
6821 -----------------------------------
6822 -- Build_Derived_Concurrent_Type --
6823 -----------------------------------
6825 procedure Build_Derived_Concurrent_Type
6827 Parent_Type : Entity_Id;
6828 Derived_Type : Entity_Id)
6830 Loc : constant Source_Ptr := Sloc (N);
6831 Def : constant Node_Id := Type_Definition (N);
6832 Indic : constant Node_Id := Subtype_Indication (Def);
6834 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6835 Corr_Decl : Node_Id;
6836 Corr_Decl_Needed : Boolean;
6837 -- If the derived type has fewer discriminants than its parent, the
6838 -- corresponding record is also a derived type, in order to account for
6839 -- the bound discriminants. We create a full type declaration for it in
6842 Constraint_Present : constant Boolean :=
6843 Nkind (Indic) = N_Subtype_Indication;
6845 D_Constraint : Node_Id;
6846 New_Constraint : Elist_Id := No_Elist;
6847 Old_Disc : Entity_Id;
6848 New_Disc : Entity_Id;
6852 Set_Stored_Constraint (Derived_Type, No_Elist);
6853 Corr_Decl_Needed := False;
6856 if Present (Discriminant_Specifications (N))
6857 and then Constraint_Present
6859 Old_Disc := First_Discriminant (Parent_Type);
6860 New_Disc := First (Discriminant_Specifications (N));
6861 while Present (New_Disc) and then Present (Old_Disc) loop
6862 Next_Discriminant (Old_Disc);
6867 if Present (Old_Disc) and then Expander_Active then
6869 -- The new type has fewer discriminants, so we need to create a new
6870 -- corresponding record, which is derived from the corresponding
6871 -- record of the parent, and has a stored constraint that captures
6872 -- the values of the discriminant constraints. The corresponding
6873 -- record is needed only if expander is active and code generation is
6876 -- The type declaration for the derived corresponding record has the
6877 -- same discriminant part and constraints as the current declaration.
6878 -- Copy the unanalyzed tree to build declaration.
6880 Corr_Decl_Needed := True;
6881 New_N := Copy_Separate_Tree (N);
6884 Make_Full_Type_Declaration (Loc,
6885 Defining_Identifier => Corr_Record,
6886 Discriminant_Specifications =>
6887 Discriminant_Specifications (New_N),
6889 Make_Derived_Type_Definition (Loc,
6890 Subtype_Indication =>
6891 Make_Subtype_Indication (Loc,
6894 (Corresponding_Record_Type (Parent_Type), Loc),
6897 (Subtype_Indication (Type_Definition (New_N))))));
6900 -- Copy Storage_Size and Relative_Deadline variables if task case
6902 if Is_Task_Type (Parent_Type) then
6903 Set_Storage_Size_Variable (Derived_Type,
6904 Storage_Size_Variable (Parent_Type));
6905 Set_Relative_Deadline_Variable (Derived_Type,
6906 Relative_Deadline_Variable (Parent_Type));
6909 if Present (Discriminant_Specifications (N)) then
6910 Push_Scope (Derived_Type);
6911 Check_Or_Process_Discriminants (N, Derived_Type);
6913 if Constraint_Present then
6915 Expand_To_Stored_Constraint
6917 Build_Discriminant_Constraints
6918 (Parent_Type, Indic, True));
6923 elsif Constraint_Present then
6925 -- Build an unconstrained derived type and rewrite the derived type
6926 -- as a subtype of this new base type.
6929 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6930 New_Base : Entity_Id;
6932 New_Indic : Node_Id;
6936 Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6939 Make_Full_Type_Declaration (Loc,
6940 Defining_Identifier => New_Base,
6942 Make_Derived_Type_Definition (Loc,
6943 Abstract_Present => Abstract_Present (Def),
6944 Limited_Present => Limited_Present (Def),
6945 Subtype_Indication =>
6946 New_Occurrence_Of (Parent_Base, Loc)));
6948 Mark_Rewrite_Insertion (New_Decl);
6949 Insert_Before (N, New_Decl);
6953 Make_Subtype_Indication (Loc,
6954 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6955 Constraint => Relocate_Node (Constraint (Indic)));
6958 Make_Subtype_Declaration (Loc,
6959 Defining_Identifier => Derived_Type,
6960 Subtype_Indication => New_Indic));
6967 -- By default, operations and private data are inherited from parent.
6968 -- However, in the presence of bound discriminants, a new corresponding
6969 -- record will be created, see below.
6971 Set_Has_Discriminants
6972 (Derived_Type, Has_Discriminants (Parent_Type));
6973 Set_Corresponding_Record_Type
6974 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6976 -- Is_Constrained is set according the parent subtype, but is set to
6977 -- False if the derived type is declared with new discriminants.
6981 (Is_Constrained (Parent_Type) or else Constraint_Present)
6982 and then not Present (Discriminant_Specifications (N)));
6984 if Constraint_Present then
6985 if not Has_Discriminants (Parent_Type) then
6986 Error_Msg_N ("untagged parent must have discriminants", N);
6988 elsif Present (Discriminant_Specifications (N)) then
6990 -- Verify that new discriminants are used to constrain old ones
6992 D_Constraint := First (Constraints (Constraint (Indic)));
6994 Old_Disc := First_Discriminant (Parent_Type);
6996 while Present (D_Constraint) loop
6997 if Nkind (D_Constraint) /= N_Discriminant_Association then
6999 -- Positional constraint. If it is a reference to a new
7000 -- discriminant, it constrains the corresponding old one.
7002 if Nkind (D_Constraint) = N_Identifier then
7003 New_Disc := First_Discriminant (Derived_Type);
7004 while Present (New_Disc) loop
7005 exit when Chars (New_Disc) = Chars (D_Constraint);
7006 Next_Discriminant (New_Disc);
7009 if Present (New_Disc) then
7010 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
7014 Next_Discriminant (Old_Disc);
7016 -- if this is a named constraint, search by name for the old
7017 -- discriminants constrained by the new one.
7019 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7021 -- Find new discriminant with that name
7023 New_Disc := First_Discriminant (Derived_Type);
7024 while Present (New_Disc) loop
7026 Chars (New_Disc) = Chars (Expression (D_Constraint));
7027 Next_Discriminant (New_Disc);
7030 if Present (New_Disc) then
7032 -- Verify that new discriminant renames some discriminant
7033 -- of the parent type, and associate the new discriminant
7034 -- with one or more old ones that it renames.
7040 Selector := First (Selector_Names (D_Constraint));
7041 while Present (Selector) loop
7042 Old_Disc := First_Discriminant (Parent_Type);
7043 while Present (Old_Disc) loop
7044 exit when Chars (Old_Disc) = Chars (Selector);
7045 Next_Discriminant (Old_Disc);
7048 if Present (Old_Disc) then
7049 Set_Corresponding_Discriminant
7050 (New_Disc, Old_Disc);
7059 Next (D_Constraint);
7062 New_Disc := First_Discriminant (Derived_Type);
7063 while Present (New_Disc) loop
7064 if No (Corresponding_Discriminant (New_Disc)) then
7066 ("new discriminant& must constrain old one", N, New_Disc);
7068 -- If a new discriminant is used in the constraint, then its
7069 -- subtype must be statically compatible with the subtype of
7070 -- the parent discriminant (RM 3.7(15)).
7073 Check_Constraining_Discriminant
7074 (New_Disc, Corresponding_Discriminant (New_Disc));
7077 Next_Discriminant (New_Disc);
7081 elsif Present (Discriminant_Specifications (N)) then
7083 ("missing discriminant constraint in untagged derivation", N);
7086 -- The entity chain of the derived type includes the new discriminants
7087 -- but shares operations with the parent.
7089 if Present (Discriminant_Specifications (N)) then
7090 Old_Disc := First_Discriminant (Parent_Type);
7091 while Present (Old_Disc) loop
7092 if No (Next_Entity (Old_Disc))
7093 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7096 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7100 Next_Discriminant (Old_Disc);
7104 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7105 if Has_Discriminants (Parent_Type) then
7106 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7107 Set_Discriminant_Constraint (
7108 Derived_Type, Discriminant_Constraint (Parent_Type));
7112 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7114 Set_Has_Completion (Derived_Type);
7116 if Corr_Decl_Needed then
7117 Set_Stored_Constraint (Derived_Type, New_Constraint);
7118 Insert_After (N, Corr_Decl);
7119 Analyze (Corr_Decl);
7120 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7122 end Build_Derived_Concurrent_Type;
7124 ------------------------------------
7125 -- Build_Derived_Enumeration_Type --
7126 ------------------------------------
7128 procedure Build_Derived_Enumeration_Type
7130 Parent_Type : Entity_Id;
7131 Derived_Type : Entity_Id)
7133 function Bound_Belongs_To_Type (B : Node_Id) return Boolean;
7134 -- When the type declaration includes a constraint, we generate
7135 -- a subtype declaration of an anonymous base type, with the constraint
7136 -- given in the original type declaration. Conceptually, the bounds
7137 -- are converted to the new base type, and this conversion freezes
7138 -- (prematurely) that base type, when the bounds are simply literals.
7139 -- As a result, a representation clause for the derived type is then
7140 -- rejected or ignored. This procedure recognizes the simple case of
7141 -- literal bounds, which allows us to indicate that the conversions
7142 -- are not freeze points, and the subsequent representation clause
7144 -- A similar approach might be used to resolve the long-standing
7145 -- problem of premature freezing of derived numeric types ???
7147 function Bound_Belongs_To_Type (B : Node_Id) return Boolean is
7149 return Nkind (B) = N_Type_Conversion
7150 and then Is_Entity_Name (Expression (B))
7151 and then Ekind (Entity (Expression (B))) = E_Enumeration_Literal;
7152 end Bound_Belongs_To_Type;
7154 Loc : constant Source_Ptr := Sloc (N);
7155 Def : constant Node_Id := Type_Definition (N);
7156 Indic : constant Node_Id := Subtype_Indication (Def);
7157 Implicit_Base : Entity_Id;
7158 Literal : Entity_Id;
7159 New_Lit : Entity_Id;
7160 Literals_List : List_Id;
7161 Type_Decl : Node_Id;
7163 Rang_Expr : Node_Id;
7166 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7167 -- not have explicit literals lists we need to process types derived
7168 -- from them specially. This is handled by Derived_Standard_Character.
7169 -- If the parent type is a generic type, there are no literals either,
7170 -- and we construct the same skeletal representation as for the generic
7173 if Is_Standard_Character_Type (Parent_Type) then
7174 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7176 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7182 if Nkind (Indic) /= N_Subtype_Indication then
7184 Make_Attribute_Reference (Loc,
7185 Attribute_Name => Name_First,
7186 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7187 Set_Etype (Lo, Derived_Type);
7190 Make_Attribute_Reference (Loc,
7191 Attribute_Name => Name_Last,
7192 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7193 Set_Etype (Hi, Derived_Type);
7195 Set_Scalar_Range (Derived_Type,
7201 -- Analyze subtype indication and verify compatibility
7202 -- with parent type.
7204 if Base_Type (Process_Subtype (Indic, N)) /=
7205 Base_Type (Parent_Type)
7208 ("illegal constraint for formal discrete type", N);
7214 -- If a constraint is present, analyze the bounds to catch
7215 -- premature usage of the derived literals.
7217 if Nkind (Indic) = N_Subtype_Indication
7218 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7220 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7221 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7224 -- Introduce an implicit base type for the derived type even if there
7225 -- is no constraint attached to it, since this seems closer to the
7226 -- Ada semantics. Build a full type declaration tree for the derived
7227 -- type using the implicit base type as the defining identifier. The
7228 -- build a subtype declaration tree which applies the constraint (if
7229 -- any) have it replace the derived type declaration.
7231 Literal := First_Literal (Parent_Type);
7232 Literals_List := New_List;
7233 while Present (Literal)
7234 and then Ekind (Literal) = E_Enumeration_Literal
7236 -- Literals of the derived type have the same representation as
7237 -- those of the parent type, but this representation can be
7238 -- overridden by an explicit representation clause. Indicate
7239 -- that there is no explicit representation given yet. These
7240 -- derived literals are implicit operations of the new type,
7241 -- and can be overridden by explicit ones.
7243 if Nkind (Literal) = N_Defining_Character_Literal then
7245 Make_Defining_Character_Literal (Loc, Chars (Literal));
7247 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7250 Set_Ekind (New_Lit, E_Enumeration_Literal);
7251 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7252 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7253 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7254 Set_Alias (New_Lit, Literal);
7255 Set_Is_Known_Valid (New_Lit, True);
7257 Append (New_Lit, Literals_List);
7258 Next_Literal (Literal);
7262 Make_Defining_Identifier (Sloc (Derived_Type),
7263 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7265 -- Indicate the proper nature of the derived type. This must be done
7266 -- before analysis of the literals, to recognize cases when a literal
7267 -- may be hidden by a previous explicit function definition (cf.
7270 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
7271 Set_Etype (Derived_Type, Implicit_Base);
7274 Make_Full_Type_Declaration (Loc,
7275 Defining_Identifier => Implicit_Base,
7276 Discriminant_Specifications => No_List,
7278 Make_Enumeration_Type_Definition (Loc, Literals_List));
7280 Mark_Rewrite_Insertion (Type_Decl);
7281 Insert_Before (N, Type_Decl);
7282 Analyze (Type_Decl);
7284 -- The anonymous base now has a full declaration, but this base
7285 -- is not a first subtype.
7287 Set_Is_First_Subtype (Implicit_Base, False);
7289 -- After the implicit base is analyzed its Etype needs to be changed
7290 -- to reflect the fact that it is derived from the parent type which
7291 -- was ignored during analysis. We also set the size at this point.
7293 Set_Etype (Implicit_Base, Parent_Type);
7295 Set_Size_Info (Implicit_Base, Parent_Type);
7296 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7297 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7299 -- Copy other flags from parent type
7301 Set_Has_Non_Standard_Rep
7302 (Implicit_Base, Has_Non_Standard_Rep
7304 Set_Has_Pragma_Ordered
7305 (Implicit_Base, Has_Pragma_Ordered
7307 Set_Has_Delayed_Freeze (Implicit_Base);
7309 -- Process the subtype indication including a validation check on the
7310 -- constraint, if any. If a constraint is given, its bounds must be
7311 -- implicitly converted to the new type.
7313 if Nkind (Indic) = N_Subtype_Indication then
7315 R : constant Node_Id :=
7316 Range_Expression (Constraint (Indic));
7319 if Nkind (R) = N_Range then
7320 Hi := Build_Scalar_Bound
7321 (High_Bound (R), Parent_Type, Implicit_Base);
7322 Lo := Build_Scalar_Bound
7323 (Low_Bound (R), Parent_Type, Implicit_Base);
7326 -- Constraint is a Range attribute. Replace with explicit
7327 -- mention of the bounds of the prefix, which must be a
7330 Analyze (Prefix (R));
7332 Convert_To (Implicit_Base,
7333 Make_Attribute_Reference (Loc,
7334 Attribute_Name => Name_Last,
7336 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7339 Convert_To (Implicit_Base,
7340 Make_Attribute_Reference (Loc,
7341 Attribute_Name => Name_First,
7343 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7350 (Type_High_Bound (Parent_Type),
7351 Parent_Type, Implicit_Base);
7354 (Type_Low_Bound (Parent_Type),
7355 Parent_Type, Implicit_Base);
7363 -- If we constructed a default range for the case where no range
7364 -- was given, then the expressions in the range must not freeze
7365 -- since they do not correspond to expressions in the source.
7366 -- However, if the type inherits predicates the expressions will
7367 -- be elaborated earlier and must freeze.
7369 if (Nkind (Indic) /= N_Subtype_Indication
7371 (Bound_Belongs_To_Type (Lo) and then Bound_Belongs_To_Type (Hi)))
7372 and then not Has_Predicates (Derived_Type)
7374 Set_Must_Not_Freeze (Lo);
7375 Set_Must_Not_Freeze (Hi);
7376 Set_Must_Not_Freeze (Rang_Expr);
7380 Make_Subtype_Declaration (Loc,
7381 Defining_Identifier => Derived_Type,
7382 Subtype_Indication =>
7383 Make_Subtype_Indication (Loc,
7384 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7386 Make_Range_Constraint (Loc,
7387 Range_Expression => Rang_Expr))));
7391 -- Propagate the aspects from the original type declaration to the
7392 -- declaration of the implicit base.
7394 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7396 -- Apply a range check. Since this range expression doesn't have an
7397 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7400 if Nkind (Indic) = N_Subtype_Indication then
7402 (Range_Expression (Constraint (Indic)), Parent_Type,
7403 Source_Typ => Entity (Subtype_Mark (Indic)));
7406 end Build_Derived_Enumeration_Type;
7408 --------------------------------
7409 -- Build_Derived_Numeric_Type --
7410 --------------------------------
7412 procedure Build_Derived_Numeric_Type
7414 Parent_Type : Entity_Id;
7415 Derived_Type : Entity_Id)
7417 Loc : constant Source_Ptr := Sloc (N);
7418 Tdef : constant Node_Id := Type_Definition (N);
7419 Indic : constant Node_Id := Subtype_Indication (Tdef);
7420 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7421 No_Constraint : constant Boolean := Nkind (Indic) /=
7422 N_Subtype_Indication;
7423 Implicit_Base : Entity_Id;
7429 -- Process the subtype indication including a validation check on
7430 -- the constraint if any.
7432 Discard_Node (Process_Subtype (Indic, N));
7434 -- Introduce an implicit base type for the derived type even if there
7435 -- is no constraint attached to it, since this seems closer to the Ada
7439 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7441 Set_Etype (Implicit_Base, Parent_Base);
7442 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7443 Set_Size_Info (Implicit_Base, Parent_Base);
7444 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7445 Set_Parent (Implicit_Base, Parent (Derived_Type));
7446 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7447 Set_Is_Volatile (Implicit_Base, Is_Volatile (Parent_Base));
7449 -- Set RM Size for discrete type or decimal fixed-point type
7450 -- Ordinary fixed-point is excluded, why???
7452 if Is_Discrete_Type (Parent_Base)
7453 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7455 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7458 Set_Has_Delayed_Freeze (Implicit_Base);
7460 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7461 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7463 Set_Scalar_Range (Implicit_Base,
7468 if Has_Infinities (Parent_Base) then
7469 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7472 -- The Derived_Type, which is the entity of the declaration, is a
7473 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7474 -- absence of an explicit constraint.
7476 Set_Etype (Derived_Type, Implicit_Base);
7478 -- If we did not have a constraint, then the Ekind is set from the
7479 -- parent type (otherwise Process_Subtype has set the bounds)
7481 if No_Constraint then
7482 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7485 -- If we did not have a range constraint, then set the range from the
7486 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7488 if No_Constraint or else not Has_Range_Constraint (Indic) then
7489 Set_Scalar_Range (Derived_Type,
7491 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7492 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7493 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7495 if Has_Infinities (Parent_Type) then
7496 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7499 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7502 Set_Is_Descendant_Of_Address (Derived_Type,
7503 Is_Descendant_Of_Address (Parent_Type));
7504 Set_Is_Descendant_Of_Address (Implicit_Base,
7505 Is_Descendant_Of_Address (Parent_Type));
7507 -- Set remaining type-specific fields, depending on numeric type
7509 if Is_Modular_Integer_Type (Parent_Type) then
7510 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7512 Set_Non_Binary_Modulus
7513 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7516 (Implicit_Base, Is_Known_Valid (Parent_Base));
7518 elsif Is_Floating_Point_Type (Parent_Type) then
7520 -- Digits of base type is always copied from the digits value of
7521 -- the parent base type, but the digits of the derived type will
7522 -- already have been set if there was a constraint present.
7524 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7525 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7527 if No_Constraint then
7528 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7531 elsif Is_Fixed_Point_Type (Parent_Type) then
7533 -- Small of base type and derived type are always copied from the
7534 -- parent base type, since smalls never change. The delta of the
7535 -- base type is also copied from the parent base type. However the
7536 -- delta of the derived type will have been set already if a
7537 -- constraint was present.
7539 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7540 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7541 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7543 if No_Constraint then
7544 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7547 -- The scale and machine radix in the decimal case are always
7548 -- copied from the parent base type.
7550 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7551 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7552 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7554 Set_Machine_Radix_10
7555 (Derived_Type, Machine_Radix_10 (Parent_Base));
7556 Set_Machine_Radix_10
7557 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7559 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7561 if No_Constraint then
7562 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7565 -- the analysis of the subtype_indication sets the
7566 -- digits value of the derived type.
7573 if Is_Integer_Type (Parent_Type) then
7574 Set_Has_Shift_Operator
7575 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7578 -- The type of the bounds is that of the parent type, and they
7579 -- must be converted to the derived type.
7581 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7583 -- The implicit_base should be frozen when the derived type is frozen,
7584 -- but note that it is used in the conversions of the bounds. For fixed
7585 -- types we delay the determination of the bounds until the proper
7586 -- freezing point. For other numeric types this is rejected by GCC, for
7587 -- reasons that are currently unclear (???), so we choose to freeze the
7588 -- implicit base now. In the case of integers and floating point types
7589 -- this is harmless because subsequent representation clauses cannot
7590 -- affect anything, but it is still baffling that we cannot use the
7591 -- same mechanism for all derived numeric types.
7593 -- There is a further complication: actually some representation
7594 -- clauses can affect the implicit base type. For example, attribute
7595 -- definition clauses for stream-oriented attributes need to set the
7596 -- corresponding TSS entries on the base type, and this normally
7597 -- cannot be done after the base type is frozen, so the circuitry in
7598 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7599 -- and not use Set_TSS in this case.
7601 -- There are also consequences for the case of delayed representation
7602 -- aspects for some cases. For example, a Size aspect is delayed and
7603 -- should not be evaluated to the freeze point. This early freezing
7604 -- means that the size attribute evaluation happens too early???
7606 if Is_Fixed_Point_Type (Parent_Type) then
7607 Conditional_Delay (Implicit_Base, Parent_Type);
7609 Freeze_Before (N, Implicit_Base);
7611 end Build_Derived_Numeric_Type;
7613 --------------------------------
7614 -- Build_Derived_Private_Type --
7615 --------------------------------
7617 procedure Build_Derived_Private_Type
7619 Parent_Type : Entity_Id;
7620 Derived_Type : Entity_Id;
7621 Is_Completion : Boolean;
7622 Derive_Subps : Boolean := True)
7624 Loc : constant Source_Ptr := Sloc (N);
7625 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7626 Par_Scope : constant Entity_Id := Scope (Par_Base);
7627 Full_N : constant Node_Id := New_Copy_Tree (N);
7628 Full_Der : Entity_Id := New_Copy (Derived_Type);
7631 function Available_Full_View (Typ : Entity_Id) return Entity_Id;
7632 -- Return the Full_View or Underlying_Full_View of Typ, whichever is
7633 -- present (they cannot be both present for the same type), or Empty.
7635 procedure Build_Full_Derivation;
7636 -- Build full derivation, i.e. derive from the full view
7638 procedure Copy_And_Build;
7639 -- Copy derived type declaration, replace parent with its full view,
7640 -- and build derivation
7642 -------------------------
7643 -- Available_Full_View --
7644 -------------------------
7646 function Available_Full_View (Typ : Entity_Id) return Entity_Id is
7648 if Present (Full_View (Typ)) then
7649 return Full_View (Typ);
7651 elsif Present (Underlying_Full_View (Typ)) then
7653 -- We should be called on a type with an underlying full view
7654 -- only by means of the recursive call made in Copy_And_Build
7655 -- through the first call to Build_Derived_Type, or else if
7656 -- the parent scope is being analyzed because we are deriving
7659 pragma Assert (Is_Completion or else In_Private_Part (Par_Scope));
7661 return Underlying_Full_View (Typ);
7666 end Available_Full_View;
7668 ---------------------------
7669 -- Build_Full_Derivation --
7670 ---------------------------
7672 procedure Build_Full_Derivation is
7674 -- If parent scope is not open, install the declarations
7676 if not In_Open_Scopes (Par_Scope) then
7677 Install_Private_Declarations (Par_Scope);
7678 Install_Visible_Declarations (Par_Scope);
7680 Uninstall_Declarations (Par_Scope);
7682 -- If parent scope is open and in another unit, and parent has a
7683 -- completion, then the derivation is taking place in the visible
7684 -- part of a child unit. In that case retrieve the full view of
7685 -- the parent momentarily.
7687 elsif not In_Same_Source_Unit (N, Parent_Type)
7688 and then Present (Full_View (Parent_Type))
7690 Full_P := Full_View (Parent_Type);
7691 Exchange_Declarations (Parent_Type);
7693 Exchange_Declarations (Full_P);
7695 -- Otherwise it is a local derivation
7700 end Build_Full_Derivation;
7702 --------------------
7703 -- Copy_And_Build --
7704 --------------------
7706 procedure Copy_And_Build is
7707 Full_Parent : Entity_Id := Parent_Type;
7710 -- If the parent is itself derived from another private type,
7711 -- installing the private declarations has not affected its
7712 -- privacy status, so use its own full view explicitly.
7714 if Is_Private_Type (Full_Parent)
7715 and then Present (Full_View (Full_Parent))
7717 Full_Parent := Full_View (Full_Parent);
7720 -- If the full view is itself derived from another private type
7721 -- and has got an underlying full view, and this is done for a
7722 -- completion, i.e. to build the underlying full view of the type,
7723 -- then use this underlying full view. We cannot do that if this
7724 -- is not a completion, i.e. to build the full view of the type,
7725 -- because this would break the privacy of the parent type, except
7726 -- if the parent scope is being analyzed because we are deriving a
7729 if Is_Private_Type (Full_Parent)
7730 and then Present (Underlying_Full_View (Full_Parent))
7731 and then (Is_Completion or else In_Private_Part (Par_Scope))
7733 Full_Parent := Underlying_Full_View (Full_Parent);
7736 -- For private, record, concurrent, access and almost all enumeration
7737 -- types, the derivation from the full view requires a fully-fledged
7738 -- declaration. In the other cases, just use an itype.
7740 if Is_Private_Type (Full_Parent)
7741 or else Is_Record_Type (Full_Parent)
7742 or else Is_Concurrent_Type (Full_Parent)
7743 or else Is_Access_Type (Full_Parent)
7745 (Is_Enumeration_Type (Full_Parent)
7746 and then not Is_Standard_Character_Type (Full_Parent)
7747 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7749 -- Copy and adjust declaration to provide a completion for what
7750 -- is originally a private declaration. Indicate that full view
7751 -- is internally generated.
7753 Set_Comes_From_Source (Full_N, False);
7754 Set_Comes_From_Source (Full_Der, False);
7755 Set_Parent (Full_Der, Full_N);
7756 Set_Defining_Identifier (Full_N, Full_Der);
7758 -- If there are no constraints, adjust the subtype mark
7760 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7761 N_Subtype_Indication
7763 Set_Subtype_Indication
7764 (Type_Definition (Full_N),
7765 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7768 Insert_After (N, Full_N);
7770 -- Build full view of derived type from full view of parent which
7771 -- is now installed. Subprograms have been derived on the partial
7772 -- view, the completion does not derive them anew.
7774 if Is_Record_Type (Full_Parent) then
7776 -- If parent type is tagged, the completion inherits the proper
7777 -- primitive operations.
7779 if Is_Tagged_Type (Parent_Type) then
7780 Build_Derived_Record_Type
7781 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7783 Build_Derived_Record_Type
7784 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7788 -- If the parent type is private, this is not a completion and
7789 -- we build the full derivation recursively as a completion.
7792 (Full_N, Full_Parent, Full_Der,
7793 Is_Completion => Is_Private_Type (Full_Parent),
7794 Derive_Subps => False);
7797 -- The full declaration has been introduced into the tree and
7798 -- processed in the step above. It should not be analyzed again
7799 -- (when encountered later in the current list of declarations)
7800 -- to prevent spurious name conflicts. The full entity remains
7803 Set_Analyzed (Full_N);
7807 Make_Defining_Identifier (Sloc (Derived_Type),
7808 Chars => Chars (Derived_Type));
7809 Set_Is_Itype (Full_Der);
7810 Set_Associated_Node_For_Itype (Full_Der, N);
7811 Set_Parent (Full_Der, N);
7813 (N, Full_Parent, Full_Der,
7814 Is_Completion => False, Derive_Subps => False);
7817 Set_Has_Private_Declaration (Full_Der);
7818 Set_Has_Private_Declaration (Derived_Type);
7820 Set_Scope (Full_Der, Scope (Derived_Type));
7821 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7822 Set_Has_Size_Clause (Full_Der, False);
7823 Set_Has_Alignment_Clause (Full_Der, False);
7824 Set_Has_Delayed_Freeze (Full_Der);
7825 Set_Is_Frozen (Full_Der, False);
7826 Set_Freeze_Node (Full_Der, Empty);
7827 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7828 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7830 -- The convention on the base type may be set in the private part
7831 -- and not propagated to the subtype until later, so we obtain the
7832 -- convention from the base type of the parent.
7834 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7837 -- Start of processing for Build_Derived_Private_Type
7840 if Is_Tagged_Type (Parent_Type) then
7841 Full_P := Full_View (Parent_Type);
7843 -- A type extension of a type with unknown discriminants is an
7844 -- indefinite type that the back-end cannot handle directly.
7845 -- We treat it as a private type, and build a completion that is
7846 -- derived from the full view of the parent, and hopefully has
7847 -- known discriminants.
7849 -- If the full view of the parent type has an underlying record view,
7850 -- use it to generate the underlying record view of this derived type
7851 -- (required for chains of derivations with unknown discriminants).
7853 -- Minor optimization: we avoid the generation of useless underlying
7854 -- record view entities if the private type declaration has unknown
7855 -- discriminants but its corresponding full view has no
7858 if Has_Unknown_Discriminants (Parent_Type)
7859 and then Present (Full_P)
7860 and then (Has_Discriminants (Full_P)
7861 or else Present (Underlying_Record_View (Full_P)))
7862 and then not In_Open_Scopes (Par_Scope)
7863 and then Expander_Active
7866 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7867 New_Ext : constant Node_Id :=
7869 (Record_Extension_Part (Type_Definition (N)));
7873 Build_Derived_Record_Type
7874 (N, Parent_Type, Derived_Type, Derive_Subps);
7876 -- Build anonymous completion, as a derivation from the full
7877 -- view of the parent. This is not a completion in the usual
7878 -- sense, because the current type is not private.
7881 Make_Full_Type_Declaration (Loc,
7882 Defining_Identifier => Full_Der,
7884 Make_Derived_Type_Definition (Loc,
7885 Subtype_Indication =>
7887 (Subtype_Indication (Type_Definition (N))),
7888 Record_Extension_Part => New_Ext));
7890 -- If the parent type has an underlying record view, use it
7891 -- here to build the new underlying record view.
7893 if Present (Underlying_Record_View (Full_P)) then
7895 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7897 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7898 Underlying_Record_View (Full_P));
7901 Install_Private_Declarations (Par_Scope);
7902 Install_Visible_Declarations (Par_Scope);
7903 Insert_Before (N, Decl);
7905 -- Mark entity as an underlying record view before analysis,
7906 -- to avoid generating the list of its primitive operations
7907 -- (which is not really required for this entity) and thus
7908 -- prevent spurious errors associated with missing overriding
7909 -- of abstract primitives (overridden only for Derived_Type).
7911 Set_Ekind (Full_Der, E_Record_Type);
7912 Set_Is_Underlying_Record_View (Full_Der);
7913 Set_Default_SSO (Full_Der);
7914 Set_No_Reordering (Full_Der, No_Component_Reordering);
7918 pragma Assert (Has_Discriminants (Full_Der)
7919 and then not Has_Unknown_Discriminants (Full_Der));
7921 Uninstall_Declarations (Par_Scope);
7923 -- Freeze the underlying record view, to prevent generation of
7924 -- useless dispatching information, which is simply shared with
7925 -- the real derived type.
7927 Set_Is_Frozen (Full_Der);
7929 -- If the derived type has access discriminants, create
7930 -- references to their anonymous types now, to prevent
7931 -- back-end problems when their first use is in generated
7932 -- bodies of primitives.
7938 E := First_Entity (Full_Der);
7940 while Present (E) loop
7941 if Ekind (E) = E_Discriminant
7942 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7944 Build_Itype_Reference (Etype (E), Decl);
7951 -- Set up links between real entity and underlying record view
7953 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7954 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7957 -- If discriminants are known, build derived record
7960 Build_Derived_Record_Type
7961 (N, Parent_Type, Derived_Type, Derive_Subps);
7966 elsif Has_Discriminants (Parent_Type) then
7968 -- Build partial view of derived type from partial view of parent.
7969 -- This must be done before building the full derivation because the
7970 -- second derivation will modify the discriminants of the first and
7971 -- the discriminants are chained with the rest of the components in
7972 -- the full derivation.
7974 Build_Derived_Record_Type
7975 (N, Parent_Type, Derived_Type, Derive_Subps);
7977 -- Build the full derivation if this is not the anonymous derived
7978 -- base type created by Build_Derived_Record_Type in the constrained
7979 -- case (see point 5. of its head comment) since we build it for the
7982 if Present (Available_Full_View (Parent_Type))
7983 and then not Is_Itype (Derived_Type)
7986 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7988 Last_Discr : Entity_Id;
7991 -- If this is not a completion, construct the implicit full
7992 -- view by deriving from the full view of the parent type.
7993 -- But if this is a completion, the derived private type
7994 -- being built is a full view and the full derivation can
7995 -- only be its underlying full view.
7997 Build_Full_Derivation;
7999 if not Is_Completion then
8000 Set_Full_View (Derived_Type, Full_Der);
8002 Set_Underlying_Full_View (Derived_Type, Full_Der);
8003 Set_Is_Underlying_Full_View (Full_Der);
8006 if not Is_Base_Type (Derived_Type) then
8007 Set_Full_View (Der_Base, Base_Type (Full_Der));
8010 -- Copy the discriminant list from full view to the partial
8011 -- view (base type and its subtype). Gigi requires that the
8012 -- partial and full views have the same discriminants.
8014 -- Note that since the partial view points to discriminants
8015 -- in the full view, their scope will be that of the full
8016 -- view. This might cause some front end problems and need
8019 Discr := First_Discriminant (Base_Type (Full_Der));
8020 Set_First_Entity (Der_Base, Discr);
8023 Last_Discr := Discr;
8024 Next_Discriminant (Discr);
8025 exit when No (Discr);
8028 Set_Last_Entity (Der_Base, Last_Discr);
8029 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
8030 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
8034 elsif Present (Available_Full_View (Parent_Type))
8035 and then Has_Discriminants (Available_Full_View (Parent_Type))
8037 if Has_Unknown_Discriminants (Parent_Type)
8038 and then Nkind (Subtype_Indication (Type_Definition (N))) =
8039 N_Subtype_Indication
8042 ("cannot constrain type with unknown discriminants",
8043 Subtype_Indication (Type_Definition (N)));
8047 -- If this is not a completion, construct the implicit full view by
8048 -- deriving from the full view of the parent type. But if this is a
8049 -- completion, the derived private type being built is a full view
8050 -- and the full derivation can only be its underlying full view.
8052 Build_Full_Derivation;
8054 if not Is_Completion then
8055 Set_Full_View (Derived_Type, Full_Der);
8057 Set_Underlying_Full_View (Derived_Type, Full_Der);
8058 Set_Is_Underlying_Full_View (Full_Der);
8061 -- In any case, the primitive operations are inherited from the
8062 -- parent type, not from the internal full view.
8064 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8066 if Derive_Subps then
8067 Derive_Subprograms (Parent_Type, Derived_Type);
8070 Set_Stored_Constraint (Derived_Type, No_Elist);
8072 (Derived_Type, Is_Constrained (Available_Full_View (Parent_Type)));
8075 -- Untagged type, No discriminants on either view
8077 if Nkind (Subtype_Indication (Type_Definition (N))) =
8078 N_Subtype_Indication
8081 ("illegal constraint on type without discriminants", N);
8084 if Present (Discriminant_Specifications (N))
8085 and then Present (Available_Full_View (Parent_Type))
8086 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8088 Error_Msg_N ("cannot add discriminants to untagged type", N);
8091 Set_Stored_Constraint (Derived_Type, No_Elist);
8092 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8094 Set_Is_Controlled_Active
8095 (Derived_Type, Is_Controlled_Active (Parent_Type));
8097 Set_Disable_Controlled
8098 (Derived_Type, Disable_Controlled (Parent_Type));
8100 Set_Has_Controlled_Component
8101 (Derived_Type, Has_Controlled_Component (Parent_Type));
8103 -- Direct controlled types do not inherit Finalize_Storage_Only flag
8105 if not Is_Controlled (Parent_Type) then
8106 Set_Finalize_Storage_Only
8107 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8110 -- If this is not a completion, construct the implicit full view by
8111 -- deriving from the full view of the parent type. But if this is a
8112 -- completion, the derived private type being built is a full view
8113 -- and the full derivation can only be its underlying full view.
8115 -- ??? If the parent type is untagged private and its completion is
8116 -- tagged, this mechanism will not work because we cannot derive from
8117 -- the tagged full view unless we have an extension.
8119 if Present (Available_Full_View (Parent_Type))
8120 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8121 and then not Error_Posted (N)
8123 Build_Full_Derivation;
8125 if not Is_Completion then
8126 Set_Full_View (Derived_Type, Full_Der);
8128 Set_Underlying_Full_View (Derived_Type, Full_Der);
8129 Set_Is_Underlying_Full_View (Full_Der);
8134 Set_Has_Unknown_Discriminants (Derived_Type,
8135 Has_Unknown_Discriminants (Parent_Type));
8137 if Is_Private_Type (Derived_Type) then
8138 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8141 -- If the parent base type is in scope, add the derived type to its
8142 -- list of private dependents, because its full view may become
8143 -- visible subsequently (in a nested private part, a body, or in a
8144 -- further child unit).
8146 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8147 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8149 -- Check for unusual case where a type completed by a private
8150 -- derivation occurs within a package nested in a child unit, and
8151 -- the parent is declared in an ancestor.
8153 if Is_Child_Unit (Scope (Current_Scope))
8154 and then Is_Completion
8155 and then In_Private_Part (Current_Scope)
8156 and then Scope (Parent_Type) /= Current_Scope
8158 -- Note that if the parent has a completion in the private part,
8159 -- (which is itself a derivation from some other private type)
8160 -- it is that completion that is visible, there is no full view
8161 -- available, and no special processing is needed.
8163 and then Present (Full_View (Parent_Type))
8165 -- In this case, the full view of the parent type will become
8166 -- visible in the body of the enclosing child, and only then will
8167 -- the current type be possibly non-private. Build an underlying
8168 -- full view that will be installed when the enclosing child body
8171 if Present (Underlying_Full_View (Derived_Type)) then
8172 Full_Der := Underlying_Full_View (Derived_Type);
8174 Build_Full_Derivation;
8175 Set_Underlying_Full_View (Derived_Type, Full_Der);
8176 Set_Is_Underlying_Full_View (Full_Der);
8179 -- The full view will be used to swap entities on entry/exit to
8180 -- the body, and must appear in the entity list for the package.
8182 Append_Entity (Full_Der, Scope (Derived_Type));
8185 end Build_Derived_Private_Type;
8187 -------------------------------
8188 -- Build_Derived_Record_Type --
8189 -------------------------------
8193 -- Ideally we would like to use the same model of type derivation for
8194 -- tagged and untagged record types. Unfortunately this is not quite
8195 -- possible because the semantics of representation clauses is different
8196 -- for tagged and untagged records under inheritance. Consider the
8199 -- type R (...) is [tagged] record ... end record;
8200 -- type T (...) is new R (...) [with ...];
8202 -- The representation clauses for T can specify a completely different
8203 -- record layout from R's. Hence the same component can be placed in two
8204 -- very different positions in objects of type T and R. If R and T are
8205 -- tagged types, representation clauses for T can only specify the layout
8206 -- of non inherited components, thus components that are common in R and T
8207 -- have the same position in objects of type R and T.
8209 -- This has two implications. The first is that the entire tree for R's
8210 -- declaration needs to be copied for T in the untagged case, so that T
8211 -- can be viewed as a record type of its own with its own representation
8212 -- clauses. The second implication is the way we handle discriminants.
8213 -- Specifically, in the untagged case we need a way to communicate to Gigi
8214 -- what are the real discriminants in the record, while for the semantics
8215 -- we need to consider those introduced by the user to rename the
8216 -- discriminants in the parent type. This is handled by introducing the
8217 -- notion of stored discriminants. See below for more.
8219 -- Fortunately the way regular components are inherited can be handled in
8220 -- the same way in tagged and untagged types.
8222 -- To complicate things a bit more the private view of a private extension
8223 -- cannot be handled in the same way as the full view (for one thing the
8224 -- semantic rules are somewhat different). We will explain what differs
8227 -- 2. DISCRIMINANTS UNDER INHERITANCE
8229 -- The semantic rules governing the discriminants of derived types are
8232 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8233 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8235 -- If parent type has discriminants, then the discriminants that are
8236 -- declared in the derived type are [3.4 (11)]:
8238 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8241 -- o Otherwise, each discriminant of the parent type (implicitly declared
8242 -- in the same order with the same specifications). In this case, the
8243 -- discriminants are said to be "inherited", or if unknown in the parent
8244 -- are also unknown in the derived type.
8246 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8248 -- o The parent subtype must be constrained;
8250 -- o If the parent type is not a tagged type, then each discriminant of
8251 -- the derived type must be used in the constraint defining a parent
8252 -- subtype. [Implementation note: This ensures that the new discriminant
8253 -- can share storage with an existing discriminant.]
8255 -- For the derived type each discriminant of the parent type is either
8256 -- inherited, constrained to equal some new discriminant of the derived
8257 -- type, or constrained to the value of an expression.
8259 -- When inherited or constrained to equal some new discriminant, the
8260 -- parent discriminant and the discriminant of the derived type are said
8263 -- If a discriminant of the parent type is constrained to a specific value
8264 -- in the derived type definition, then the discriminant is said to be
8265 -- "specified" by that derived type definition.
8267 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8269 -- We have spoken about stored discriminants in point 1 (introduction)
8270 -- above. There are two sorts of stored discriminants: implicit and
8271 -- explicit. As long as the derived type inherits the same discriminants as
8272 -- the root record type, stored discriminants are the same as regular
8273 -- discriminants, and are said to be implicit. However, if any discriminant
8274 -- in the root type was renamed in the derived type, then the derived
8275 -- type will contain explicit stored discriminants. Explicit stored
8276 -- discriminants are discriminants in addition to the semantically visible
8277 -- discriminants defined for the derived type. Stored discriminants are
8278 -- used by Gigi to figure out what are the physical discriminants in
8279 -- objects of the derived type (see precise definition in einfo.ads).
8280 -- As an example, consider the following:
8282 -- type R (D1, D2, D3 : Int) is record ... end record;
8283 -- type T1 is new R;
8284 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8285 -- type T3 is new T2;
8286 -- type T4 (Y : Int) is new T3 (Y, 99);
8288 -- The following table summarizes the discriminants and stored
8289 -- discriminants in R and T1 through T4:
8291 -- Type Discrim Stored Discrim Comment
8292 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8293 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8294 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8295 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8296 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8298 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8299 -- find the corresponding discriminant in the parent type, while
8300 -- Original_Record_Component (abbreviated ORC below) the actual physical
8301 -- component that is renamed. Finally the field Is_Completely_Hidden
8302 -- (abbreviated ICH below) is set for all explicit stored discriminants
8303 -- (see einfo.ads for more info). For the above example this gives:
8305 -- Discrim CD ORC ICH
8306 -- ^^^^^^^ ^^ ^^^ ^^^
8307 -- D1 in R empty itself no
8308 -- D2 in R empty itself no
8309 -- D3 in R empty itself no
8311 -- D1 in T1 D1 in R itself no
8312 -- D2 in T1 D2 in R itself no
8313 -- D3 in T1 D3 in R itself no
8315 -- X1 in T2 D3 in T1 D3 in T2 no
8316 -- X2 in T2 D1 in T1 D1 in T2 no
8317 -- D1 in T2 empty itself yes
8318 -- D2 in T2 empty itself yes
8319 -- D3 in T2 empty itself yes
8321 -- X1 in T3 X1 in T2 D3 in T3 no
8322 -- X2 in T3 X2 in T2 D1 in T3 no
8323 -- D1 in T3 empty itself yes
8324 -- D2 in T3 empty itself yes
8325 -- D3 in T3 empty itself yes
8327 -- Y in T4 X1 in T3 D3 in T4 no
8328 -- D1 in T4 empty itself yes
8329 -- D2 in T4 empty itself yes
8330 -- D3 in T4 empty itself yes
8332 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8334 -- Type derivation for tagged types is fairly straightforward. If no
8335 -- discriminants are specified by the derived type, these are inherited
8336 -- from the parent. No explicit stored discriminants are ever necessary.
8337 -- The only manipulation that is done to the tree is that of adding a
8338 -- _parent field with parent type and constrained to the same constraint
8339 -- specified for the parent in the derived type definition. For instance:
8341 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8342 -- type T1 is new R with null record;
8343 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8345 -- are changed into:
8347 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8348 -- _parent : R (D1, D2, D3);
8351 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8352 -- _parent : T1 (X2, 88, X1);
8355 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8356 -- ORC and ICH fields are:
8358 -- Discrim CD ORC ICH
8359 -- ^^^^^^^ ^^ ^^^ ^^^
8360 -- D1 in R empty itself no
8361 -- D2 in R empty itself no
8362 -- D3 in R empty itself no
8364 -- D1 in T1 D1 in R D1 in R no
8365 -- D2 in T1 D2 in R D2 in R no
8366 -- D3 in T1 D3 in R D3 in R no
8368 -- X1 in T2 D3 in T1 D3 in R no
8369 -- X2 in T2 D1 in T1 D1 in R no
8371 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8373 -- Regardless of whether we dealing with a tagged or untagged type
8374 -- we will transform all derived type declarations of the form
8376 -- type T is new R (...) [with ...];
8378 -- subtype S is R (...);
8379 -- type T is new S [with ...];
8381 -- type BT is new R [with ...];
8382 -- subtype T is BT (...);
8384 -- That is, the base derived type is constrained only if it has no
8385 -- discriminants. The reason for doing this is that GNAT's semantic model
8386 -- assumes that a base type with discriminants is unconstrained.
8388 -- Note that, strictly speaking, the above transformation is not always
8389 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8391 -- procedure B34011A is
8392 -- type REC (D : integer := 0) is record
8397 -- type T6 is new Rec;
8398 -- function F return T6;
8403 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8406 -- The definition of Q6.U is illegal. However transforming Q6.U into
8408 -- type BaseU is new T6;
8409 -- subtype U is BaseU (Q6.F.I)
8411 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8412 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8413 -- the transformation described above.
8415 -- There is another instance where the above transformation is incorrect.
8419 -- type Base (D : Integer) is tagged null record;
8420 -- procedure P (X : Base);
8422 -- type Der is new Base (2) with null record;
8423 -- procedure P (X : Der);
8426 -- Then the above transformation turns this into
8428 -- type Der_Base is new Base with null record;
8429 -- -- procedure P (X : Base) is implicitly inherited here
8430 -- -- as procedure P (X : Der_Base).
8432 -- subtype Der is Der_Base (2);
8433 -- procedure P (X : Der);
8434 -- -- The overriding of P (X : Der_Base) is illegal since we
8435 -- -- have a parameter conformance problem.
8437 -- To get around this problem, after having semantically processed Der_Base
8438 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8439 -- Discriminant_Constraint from Der so that when parameter conformance is
8440 -- checked when P is overridden, no semantic errors are flagged.
8442 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8444 -- Regardless of whether we are dealing with a tagged or untagged type
8445 -- we will transform all derived type declarations of the form
8447 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8448 -- type T is new R [with ...];
8450 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8452 -- The reason for such transformation is that it allows us to implement a
8453 -- very clean form of component inheritance as explained below.
8455 -- Note that this transformation is not achieved by direct tree rewriting
8456 -- and manipulation, but rather by redoing the semantic actions that the
8457 -- above transformation will entail. This is done directly in routine
8458 -- Inherit_Components.
8460 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8462 -- In both tagged and untagged derived types, regular non discriminant
8463 -- components are inherited in the derived type from the parent type. In
8464 -- the absence of discriminants component, inheritance is straightforward
8465 -- as components can simply be copied from the parent.
8467 -- If the parent has discriminants, inheriting components constrained with
8468 -- these discriminants requires caution. Consider the following example:
8470 -- type R (D1, D2 : Positive) is [tagged] record
8471 -- S : String (D1 .. D2);
8474 -- type T1 is new R [with null record];
8475 -- type T2 (X : positive) is new R (1, X) [with null record];
8477 -- As explained in 6. above, T1 is rewritten as
8478 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8479 -- which makes the treatment for T1 and T2 identical.
8481 -- What we want when inheriting S, is that references to D1 and D2 in R are
8482 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8483 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8484 -- with either discriminant references in the derived type or expressions.
8485 -- This replacement is achieved as follows: before inheriting R's
8486 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8487 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8488 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8489 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8490 -- by String (1 .. X).
8492 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8494 -- We explain here the rules governing private type extensions relevant to
8495 -- type derivation. These rules are explained on the following example:
8497 -- type D [(...)] is new A [(...)] with private; <-- partial view
8498 -- type D [(...)] is new P [(...)] with null record; <-- full view
8500 -- Type A is called the ancestor subtype of the private extension.
8501 -- Type P is the parent type of the full view of the private extension. It
8502 -- must be A or a type derived from A.
8504 -- The rules concerning the discriminants of private type extensions are
8507 -- o If a private extension inherits known discriminants from the ancestor
8508 -- subtype, then the full view must also inherit its discriminants from
8509 -- the ancestor subtype and the parent subtype of the full view must be
8510 -- constrained if and only if the ancestor subtype is constrained.
8512 -- o If a partial view has unknown discriminants, then the full view may
8513 -- define a definite or an indefinite subtype, with or without
8516 -- o If a partial view has neither known nor unknown discriminants, then
8517 -- the full view must define a definite subtype.
8519 -- o If the ancestor subtype of a private extension has constrained
8520 -- discriminants, then the parent subtype of the full view must impose a
8521 -- statically matching constraint on those discriminants.
8523 -- This means that only the following forms of private extensions are
8526 -- type D is new A with private; <-- partial view
8527 -- type D is new P with null record; <-- full view
8529 -- If A has no discriminants than P has no discriminants, otherwise P must
8530 -- inherit A's discriminants.
8532 -- type D is new A (...) with private; <-- partial view
8533 -- type D is new P (:::) with null record; <-- full view
8535 -- P must inherit A's discriminants and (...) and (:::) must statically
8538 -- subtype A is R (...);
8539 -- type D is new A with private; <-- partial view
8540 -- type D is new P with null record; <-- full view
8542 -- P must have inherited R's discriminants and must be derived from A or
8543 -- any of its subtypes.
8545 -- type D (..) is new A with private; <-- partial view
8546 -- type D (..) is new P [(:::)] with null record; <-- full view
8548 -- No specific constraints on P's discriminants or constraint (:::).
8549 -- Note that A can be unconstrained, but the parent subtype P must either
8550 -- be constrained or (:::) must be present.
8552 -- type D (..) is new A [(...)] with private; <-- partial view
8553 -- type D (..) is new P [(:::)] with null record; <-- full view
8555 -- P's constraints on A's discriminants must statically match those
8556 -- imposed by (...).
8558 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8560 -- The full view of a private extension is handled exactly as described
8561 -- above. The model chose for the private view of a private extension is
8562 -- the same for what concerns discriminants (i.e. they receive the same
8563 -- treatment as in the tagged case). However, the private view of the
8564 -- private extension always inherits the components of the parent base,
8565 -- without replacing any discriminant reference. Strictly speaking this is
8566 -- incorrect. However, Gigi never uses this view to generate code so this
8567 -- is a purely semantic issue. In theory, a set of transformations similar
8568 -- to those given in 5. and 6. above could be applied to private views of
8569 -- private extensions to have the same model of component inheritance as
8570 -- for non private extensions. However, this is not done because it would
8571 -- further complicate private type processing. Semantically speaking, this
8572 -- leaves us in an uncomfortable situation. As an example consider:
8575 -- type R (D : integer) is tagged record
8576 -- S : String (1 .. D);
8578 -- procedure P (X : R);
8579 -- type T is new R (1) with private;
8581 -- type T is new R (1) with null record;
8584 -- This is transformed into:
8587 -- type R (D : integer) is tagged record
8588 -- S : String (1 .. D);
8590 -- procedure P (X : R);
8591 -- type T is new R (1) with private;
8593 -- type BaseT is new R with null record;
8594 -- subtype T is BaseT (1);
8597 -- (strictly speaking the above is incorrect Ada)
8599 -- From the semantic standpoint the private view of private extension T
8600 -- should be flagged as constrained since one can clearly have
8604 -- in a unit withing Pack. However, when deriving subprograms for the
8605 -- private view of private extension T, T must be seen as unconstrained
8606 -- since T has discriminants (this is a constraint of the current
8607 -- subprogram derivation model). Thus, when processing the private view of
8608 -- a private extension such as T, we first mark T as unconstrained, we
8609 -- process it, we perform program derivation and just before returning from
8610 -- Build_Derived_Record_Type we mark T as constrained.
8612 -- ??? Are there are other uncomfortable cases that we will have to
8615 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8617 -- Types that are derived from a visible record type and have a private
8618 -- extension present other peculiarities. They behave mostly like private
8619 -- types, but if they have primitive operations defined, these will not
8620 -- have the proper signatures for further inheritance, because other
8621 -- primitive operations will use the implicit base that we define for
8622 -- private derivations below. This affect subprogram inheritance (see
8623 -- Derive_Subprograms for details). We also derive the implicit base from
8624 -- the base type of the full view, so that the implicit base is a record
8625 -- type and not another private type, This avoids infinite loops.
8627 procedure Build_Derived_Record_Type
8629 Parent_Type : Entity_Id;
8630 Derived_Type : Entity_Id;
8631 Derive_Subps : Boolean := True)
8633 Discriminant_Specs : constant Boolean :=
8634 Present (Discriminant_Specifications (N));
8635 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8636 Loc : constant Source_Ptr := Sloc (N);
8637 Private_Extension : constant Boolean :=
8638 Nkind (N) = N_Private_Extension_Declaration;
8639 Assoc_List : Elist_Id;
8640 Constraint_Present : Boolean;
8642 Discrim : Entity_Id;
8644 Inherit_Discrims : Boolean := False;
8645 Last_Discrim : Entity_Id;
8646 New_Base : Entity_Id;
8648 New_Discrs : Elist_Id;
8649 New_Indic : Node_Id;
8650 Parent_Base : Entity_Id;
8651 Save_Etype : Entity_Id;
8652 Save_Discr_Constr : Elist_Id;
8653 Save_Next_Entity : Entity_Id;
8656 Discs : Elist_Id := New_Elmt_List;
8657 -- An empty Discs list means that there were no constraints in the
8658 -- subtype indication or that there was an error processing it.
8660 procedure Check_Generic_Ancestors;
8661 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8662 -- cannot be declared at a deeper level than its parent type is
8663 -- removed. The check on derivation within a generic body is also
8664 -- relaxed, but there's a restriction that a derived tagged type
8665 -- cannot be declared in a generic body if it's derived directly
8666 -- or indirectly from a formal type of that generic. This applies
8667 -- to progenitors as well.
8669 -----------------------------
8670 -- Check_Generic_Ancestors --
8671 -----------------------------
8673 procedure Check_Generic_Ancestors is
8674 Ancestor_Type : Entity_Id;
8675 Intf_List : List_Id;
8676 Intf_Name : Node_Id;
8678 procedure Check_Ancestor;
8679 -- For parent and progenitors.
8681 --------------------
8682 -- Check_Ancestor --
8683 --------------------
8685 procedure Check_Ancestor is
8687 -- If the derived type does have a formal type as an ancestor
8688 -- then it's an error if the derived type is declared within
8689 -- the body of the generic unit that declares the formal type
8690 -- in its generic formal part. It's sufficient to check whether
8691 -- the ancestor type is declared inside the same generic body
8692 -- as the derived type (such as within a nested generic spec),
8693 -- in which case the derivation is legal. If the formal type is
8694 -- declared outside of that generic body, then it's certain
8695 -- that the derived type is declared within the generic body
8696 -- of the generic unit declaring the formal type.
8698 if Is_Generic_Type (Ancestor_Type)
8699 and then Enclosing_Generic_Body (Ancestor_Type) /=
8700 Enclosing_Generic_Body (Derived_Type)
8703 ("ancestor type& is formal type of enclosing"
8704 & " generic unit (RM 3.9.1 (4/2))",
8705 Indic, Ancestor_Type);
8710 if Nkind (N) = N_Private_Extension_Declaration then
8711 Intf_List := Interface_List (N);
8713 Intf_List := Interface_List (Type_Definition (N));
8716 if Present (Enclosing_Generic_Body (Derived_Type)) then
8717 Ancestor_Type := Parent_Type;
8719 while not Is_Generic_Type (Ancestor_Type)
8720 and then Etype (Ancestor_Type) /= Ancestor_Type
8722 Ancestor_Type := Etype (Ancestor_Type);
8727 if Present (Intf_List) then
8728 Intf_Name := First (Intf_List);
8729 while Present (Intf_Name) loop
8730 Ancestor_Type := Entity (Intf_Name);
8736 end Check_Generic_Ancestors;
8738 -- Start of processing for Build_Derived_Record_Type
8741 if Ekind (Parent_Type) = E_Record_Type_With_Private
8742 and then Present (Full_View (Parent_Type))
8743 and then Has_Discriminants (Parent_Type)
8745 Parent_Base := Base_Type (Full_View (Parent_Type));
8747 Parent_Base := Base_Type (Parent_Type);
8750 -- If the parent type is declared as a subtype of another private
8751 -- type with inherited discriminants, its generated base type is
8752 -- itself a record subtype. To further inherit the constraint we
8753 -- need to use its own base to have an unconstrained type on which
8754 -- to apply the inherited constraint.
8756 if Ekind (Parent_Base) = E_Record_Subtype then
8757 Parent_Base := Base_Type (Parent_Base);
8760 -- AI05-0115: if this is a derivation from a private type in some
8761 -- other scope that may lead to invisible components for the derived
8762 -- type, mark it accordingly.
8764 if Is_Private_Type (Parent_Type) then
8765 if Scope (Parent_Base) = Scope (Derived_Type) then
8768 elsif In_Open_Scopes (Scope (Parent_Base))
8769 and then In_Private_Part (Scope (Parent_Base))
8774 Set_Has_Private_Ancestor (Derived_Type);
8778 Set_Has_Private_Ancestor
8779 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8782 -- Before we start the previously documented transformations, here is
8783 -- little fix for size and alignment of tagged types. Normally when we
8784 -- derive type D from type P, we copy the size and alignment of P as the
8785 -- default for D, and in the absence of explicit representation clauses
8786 -- for D, the size and alignment are indeed the same as the parent.
8788 -- But this is wrong for tagged types, since fields may be added, and
8789 -- the default size may need to be larger, and the default alignment may
8790 -- need to be larger.
8792 -- We therefore reset the size and alignment fields in the tagged case.
8793 -- Note that the size and alignment will in any case be at least as
8794 -- large as the parent type (since the derived type has a copy of the
8795 -- parent type in the _parent field)
8797 -- The type is also marked as being tagged here, which is needed when
8798 -- processing components with a self-referential anonymous access type
8799 -- in the call to Check_Anonymous_Access_Components below. Note that
8800 -- this flag is also set later on for completeness.
8803 Set_Is_Tagged_Type (Derived_Type);
8804 Init_Size_Align (Derived_Type);
8807 -- STEP 0a: figure out what kind of derived type declaration we have
8809 if Private_Extension then
8811 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8812 Set_Default_SSO (Derived_Type);
8813 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8816 Type_Def := Type_Definition (N);
8818 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8819 -- Parent_Base can be a private type or private extension. However,
8820 -- for tagged types with an extension the newly added fields are
8821 -- visible and hence the Derived_Type is always an E_Record_Type.
8822 -- (except that the parent may have its own private fields).
8823 -- For untagged types we preserve the Ekind of the Parent_Base.
8825 if Present (Record_Extension_Part (Type_Def)) then
8826 Set_Ekind (Derived_Type, E_Record_Type);
8827 Set_Default_SSO (Derived_Type);
8828 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8830 -- Create internal access types for components with anonymous
8833 if Ada_Version >= Ada_2005 then
8834 Check_Anonymous_Access_Components
8835 (N, Derived_Type, Derived_Type,
8836 Component_List (Record_Extension_Part (Type_Def)));
8840 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8844 -- Indic can either be an N_Identifier if the subtype indication
8845 -- contains no constraint or an N_Subtype_Indication if the subtype
8846 -- indication has a constraint. In either case it can include an
8849 Indic := Subtype_Indication (Type_Def);
8850 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8852 -- Check that the type has visible discriminants. The type may be
8853 -- a private type with unknown discriminants whose full view has
8854 -- discriminants which are invisible.
8856 if Constraint_Present then
8857 if not Has_Discriminants (Parent_Base)
8859 (Has_Unknown_Discriminants (Parent_Base)
8860 and then Is_Private_Type (Parent_Base))
8863 ("invalid constraint: type has no discriminant",
8864 Constraint (Indic));
8866 Constraint_Present := False;
8867 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8869 elsif Is_Constrained (Parent_Type) then
8871 ("invalid constraint: parent type is already constrained",
8872 Constraint (Indic));
8874 Constraint_Present := False;
8875 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8879 -- STEP 0b: If needed, apply transformation given in point 5. above
8881 if not Private_Extension
8882 and then Has_Discriminants (Parent_Type)
8883 and then not Discriminant_Specs
8884 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8886 -- First, we must analyze the constraint (see comment in point 5.)
8887 -- The constraint may come from the subtype indication of the full
8890 if Constraint_Present then
8891 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8893 -- If there is no explicit constraint, there might be one that is
8894 -- inherited from a constrained parent type. In that case verify that
8895 -- it conforms to the constraint in the partial view. In perverse
8896 -- cases the parent subtypes of the partial and full view can have
8897 -- different constraints.
8899 elsif Present (Stored_Constraint (Parent_Type)) then
8900 New_Discrs := Stored_Constraint (Parent_Type);
8903 New_Discrs := No_Elist;
8906 if Has_Discriminants (Derived_Type)
8907 and then Has_Private_Declaration (Derived_Type)
8908 and then Present (Discriminant_Constraint (Derived_Type))
8909 and then Present (New_Discrs)
8911 -- Verify that constraints of the full view statically match
8912 -- those given in the partial view.
8918 C1 := First_Elmt (New_Discrs);
8919 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8920 while Present (C1) and then Present (C2) loop
8921 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8923 (Is_OK_Static_Expression (Node (C1))
8924 and then Is_OK_Static_Expression (Node (C2))
8926 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8931 if Constraint_Present then
8933 ("constraint not conformant to previous declaration",
8937 ("constraint of full view is incompatible "
8938 & "with partial view", N);
8948 -- Insert and analyze the declaration for the unconstrained base type
8950 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8953 Make_Full_Type_Declaration (Loc,
8954 Defining_Identifier => New_Base,
8956 Make_Derived_Type_Definition (Loc,
8957 Abstract_Present => Abstract_Present (Type_Def),
8958 Limited_Present => Limited_Present (Type_Def),
8959 Subtype_Indication =>
8960 New_Occurrence_Of (Parent_Base, Loc),
8961 Record_Extension_Part =>
8962 Relocate_Node (Record_Extension_Part (Type_Def)),
8963 Interface_List => Interface_List (Type_Def)));
8965 Set_Parent (New_Decl, Parent (N));
8966 Mark_Rewrite_Insertion (New_Decl);
8967 Insert_Before (N, New_Decl);
8969 -- In the extension case, make sure ancestor is frozen appropriately
8970 -- (see also non-discriminated case below).
8972 if Present (Record_Extension_Part (Type_Def))
8973 or else Is_Interface (Parent_Base)
8975 Freeze_Before (New_Decl, Parent_Type);
8978 -- Note that this call passes False for the Derive_Subps parameter
8979 -- because subprogram derivation is deferred until after creating
8980 -- the subtype (see below).
8983 (New_Decl, Parent_Base, New_Base,
8984 Is_Completion => False, Derive_Subps => False);
8986 -- ??? This needs re-examination to determine whether the
8987 -- above call can simply be replaced by a call to Analyze.
8989 Set_Analyzed (New_Decl);
8991 -- Insert and analyze the declaration for the constrained subtype
8993 if Constraint_Present then
8995 Make_Subtype_Indication (Loc,
8996 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8997 Constraint => Relocate_Node (Constraint (Indic)));
9001 Constr_List : constant List_Id := New_List;
9006 C := First_Elmt (Discriminant_Constraint (Parent_Type));
9007 while Present (C) loop
9010 -- It is safe here to call New_Copy_Tree since we called
9011 -- Force_Evaluation on each constraint previously
9012 -- in Build_Discriminant_Constraints.
9014 Append (New_Copy_Tree (Expr), To => Constr_List);
9020 Make_Subtype_Indication (Loc,
9021 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9023 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
9028 Make_Subtype_Declaration (Loc,
9029 Defining_Identifier => Derived_Type,
9030 Subtype_Indication => New_Indic));
9034 -- Derivation of subprograms must be delayed until the full subtype
9035 -- has been established, to ensure proper overriding of subprograms
9036 -- inherited by full types. If the derivations occurred as part of
9037 -- the call to Build_Derived_Type above, then the check for type
9038 -- conformance would fail because earlier primitive subprograms
9039 -- could still refer to the full type prior the change to the new
9040 -- subtype and hence would not match the new base type created here.
9041 -- Subprograms are not derived, however, when Derive_Subps is False
9042 -- (since otherwise there could be redundant derivations).
9044 if Derive_Subps then
9045 Derive_Subprograms (Parent_Type, Derived_Type);
9048 -- For tagged types the Discriminant_Constraint of the new base itype
9049 -- is inherited from the first subtype so that no subtype conformance
9050 -- problem arise when the first subtype overrides primitive
9051 -- operations inherited by the implicit base type.
9054 Set_Discriminant_Constraint
9055 (New_Base, Discriminant_Constraint (Derived_Type));
9061 -- If we get here Derived_Type will have no discriminants or it will be
9062 -- a discriminated unconstrained base type.
9064 -- STEP 1a: perform preliminary actions/checks for derived tagged types
9068 -- The parent type is frozen for non-private extensions (RM 13.14(7))
9069 -- The declaration of a specific descendant of an interface type
9070 -- freezes the interface type (RM 13.14).
9072 if not Private_Extension or else Is_Interface (Parent_Base) then
9073 Freeze_Before (N, Parent_Type);
9076 if Ada_Version >= Ada_2005 then
9077 Check_Generic_Ancestors;
9079 elsif Type_Access_Level (Derived_Type) /=
9080 Type_Access_Level (Parent_Type)
9081 and then not Is_Generic_Type (Derived_Type)
9083 if Is_Controlled (Parent_Type) then
9085 ("controlled type must be declared at the library level",
9089 ("type extension at deeper accessibility level than parent",
9095 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
9098 and then GB /= Enclosing_Generic_Body (Parent_Base)
9101 ("parent type of& must not be outside generic body"
9103 Indic, Derived_Type);
9109 -- Ada 2005 (AI-251)
9111 if Ada_Version >= Ada_2005 and then Is_Tagged then
9113 -- "The declaration of a specific descendant of an interface type
9114 -- freezes the interface type" (RM 13.14).
9119 if Is_Non_Empty_List (Interface_List (Type_Def)) then
9120 Iface := First (Interface_List (Type_Def));
9121 while Present (Iface) loop
9122 Freeze_Before (N, Etype (Iface));
9129 -- STEP 1b : preliminary cleanup of the full view of private types
9131 -- If the type is already marked as having discriminants, then it's the
9132 -- completion of a private type or private extension and we need to
9133 -- retain the discriminants from the partial view if the current
9134 -- declaration has Discriminant_Specifications so that we can verify
9135 -- conformance. However, we must remove any existing components that
9136 -- were inherited from the parent (and attached in Copy_And_Swap)
9137 -- because the full type inherits all appropriate components anyway, and
9138 -- we do not want the partial view's components interfering.
9140 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9141 Discrim := First_Discriminant (Derived_Type);
9143 Last_Discrim := Discrim;
9144 Next_Discriminant (Discrim);
9145 exit when No (Discrim);
9148 Set_Last_Entity (Derived_Type, Last_Discrim);
9150 -- In all other cases wipe out the list of inherited components (even
9151 -- inherited discriminants), it will be properly rebuilt here.
9154 Set_First_Entity (Derived_Type, Empty);
9155 Set_Last_Entity (Derived_Type, Empty);
9158 -- STEP 1c: Initialize some flags for the Derived_Type
9160 -- The following flags must be initialized here so that
9161 -- Process_Discriminants can check that discriminants of tagged types do
9162 -- not have a default initial value and that access discriminants are
9163 -- only specified for limited records. For completeness, these flags are
9164 -- also initialized along with all the other flags below.
9166 -- AI-419: Limitedness is not inherited from an interface parent, so to
9167 -- be limited in that case the type must be explicitly declared as
9168 -- limited. However, task and protected interfaces are always limited.
9170 if Limited_Present (Type_Def) then
9171 Set_Is_Limited_Record (Derived_Type);
9173 elsif Is_Limited_Record (Parent_Type)
9174 or else (Present (Full_View (Parent_Type))
9175 and then Is_Limited_Record (Full_View (Parent_Type)))
9177 if not Is_Interface (Parent_Type)
9178 or else Is_Synchronized_Interface (Parent_Type)
9179 or else Is_Protected_Interface (Parent_Type)
9180 or else Is_Task_Interface (Parent_Type)
9182 Set_Is_Limited_Record (Derived_Type);
9186 -- STEP 2a: process discriminants of derived type if any
9188 Push_Scope (Derived_Type);
9190 if Discriminant_Specs then
9191 Set_Has_Unknown_Discriminants (Derived_Type, False);
9193 -- The following call initializes fields Has_Discriminants and
9194 -- Discriminant_Constraint, unless we are processing the completion
9195 -- of a private type declaration.
9197 Check_Or_Process_Discriminants (N, Derived_Type);
9199 -- For untagged types, the constraint on the Parent_Type must be
9200 -- present and is used to rename the discriminants.
9202 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9203 Error_Msg_N ("untagged parent must have discriminants", Indic);
9205 elsif not Is_Tagged and then not Constraint_Present then
9207 ("discriminant constraint needed for derived untagged records",
9210 -- Otherwise the parent subtype must be constrained unless we have a
9211 -- private extension.
9213 elsif not Constraint_Present
9214 and then not Private_Extension
9215 and then not Is_Constrained (Parent_Type)
9218 ("unconstrained type not allowed in this context", Indic);
9220 elsif Constraint_Present then
9221 -- The following call sets the field Corresponding_Discriminant
9222 -- for the discriminants in the Derived_Type.
9224 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9226 -- For untagged types all new discriminants must rename
9227 -- discriminants in the parent. For private extensions new
9228 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9230 Discrim := First_Discriminant (Derived_Type);
9231 while Present (Discrim) loop
9233 and then No (Corresponding_Discriminant (Discrim))
9236 ("new discriminants must constrain old ones", Discrim);
9238 elsif Private_Extension
9239 and then Present (Corresponding_Discriminant (Discrim))
9242 ("only static constraints allowed for parent"
9243 & " discriminants in the partial view", Indic);
9247 -- If a new discriminant is used in the constraint, then its
9248 -- subtype must be statically compatible with the subtype of
9249 -- the parent discriminant (RM 3.7(15)).
9251 if Present (Corresponding_Discriminant (Discrim)) then
9252 Check_Constraining_Discriminant
9253 (Discrim, Corresponding_Discriminant (Discrim));
9256 Next_Discriminant (Discrim);
9259 -- Check whether the constraints of the full view statically
9260 -- match those imposed by the parent subtype [7.3(13)].
9262 if Present (Stored_Constraint (Derived_Type)) then
9267 C1 := First_Elmt (Discs);
9268 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9269 while Present (C1) and then Present (C2) loop
9271 Fully_Conformant_Expressions (Node (C1), Node (C2))
9274 ("not conformant with previous declaration",
9285 -- STEP 2b: No new discriminants, inherit discriminants if any
9288 if Private_Extension then
9289 Set_Has_Unknown_Discriminants
9291 Has_Unknown_Discriminants (Parent_Type)
9292 or else Unknown_Discriminants_Present (N));
9294 -- The partial view of the parent may have unknown discriminants,
9295 -- but if the full view has discriminants and the parent type is
9296 -- in scope they must be inherited.
9298 elsif Has_Unknown_Discriminants (Parent_Type)
9300 (not Has_Discriminants (Parent_Type)
9301 or else not In_Open_Scopes (Scope (Parent_Base)))
9303 Set_Has_Unknown_Discriminants (Derived_Type);
9306 if not Has_Unknown_Discriminants (Derived_Type)
9307 and then not Has_Unknown_Discriminants (Parent_Base)
9308 and then Has_Discriminants (Parent_Type)
9310 Inherit_Discrims := True;
9311 Set_Has_Discriminants
9312 (Derived_Type, True);
9313 Set_Discriminant_Constraint
9314 (Derived_Type, Discriminant_Constraint (Parent_Base));
9317 -- The following test is true for private types (remember
9318 -- transformation 5. is not applied to those) and in an error
9321 if Constraint_Present then
9322 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9325 -- For now mark a new derived type as constrained only if it has no
9326 -- discriminants. At the end of Build_Derived_Record_Type we properly
9327 -- set this flag in the case of private extensions. See comments in
9328 -- point 9. just before body of Build_Derived_Record_Type.
9332 not (Inherit_Discrims
9333 or else Has_Unknown_Discriminants (Derived_Type)));
9336 -- STEP 3: initialize fields of derived type
9338 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9339 Set_Stored_Constraint (Derived_Type, No_Elist);
9341 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9342 -- but cannot be interfaces
9344 if not Private_Extension
9345 and then Ekind (Derived_Type) /= E_Private_Type
9346 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9348 if Interface_Present (Type_Def) then
9349 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9352 Set_Interfaces (Derived_Type, No_Elist);
9355 -- Fields inherited from the Parent_Type
9357 Set_Has_Specified_Layout
9358 (Derived_Type, Has_Specified_Layout (Parent_Type));
9359 Set_Is_Limited_Composite
9360 (Derived_Type, Is_Limited_Composite (Parent_Type));
9361 Set_Is_Private_Composite
9362 (Derived_Type, Is_Private_Composite (Parent_Type));
9364 if Is_Tagged_Type (Parent_Type) then
9365 Set_No_Tagged_Streams_Pragma
9366 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9369 -- Fields inherited from the Parent_Base
9371 Set_Has_Controlled_Component
9372 (Derived_Type, Has_Controlled_Component (Parent_Base));
9373 Set_Has_Non_Standard_Rep
9374 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9375 Set_Has_Primitive_Operations
9376 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9378 -- Set fields for private derived types
9380 if Is_Private_Type (Derived_Type) then
9381 Set_Depends_On_Private (Derived_Type, True);
9382 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9385 -- Inherit fields for non-private types. If this is the completion of a
9386 -- derivation from a private type, the parent itself is private and the
9387 -- attributes come from its full view, which must be present.
9389 if Is_Record_Type (Derived_Type) then
9391 Parent_Full : Entity_Id;
9394 if Is_Private_Type (Parent_Base)
9395 and then not Is_Record_Type (Parent_Base)
9397 Parent_Full := Full_View (Parent_Base);
9399 Parent_Full := Parent_Base;
9402 Set_Component_Alignment
9403 (Derived_Type, Component_Alignment (Parent_Full));
9405 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9406 Set_Has_Complex_Representation
9407 (Derived_Type, Has_Complex_Representation (Parent_Full));
9409 -- For untagged types, inherit the layout by default to avoid
9410 -- costly changes of representation for type conversions.
9412 if not Is_Tagged then
9413 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9414 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9419 -- Set fields for tagged types
9422 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9424 -- All tagged types defined in Ada.Finalization are controlled
9426 if Chars (Scope (Derived_Type)) = Name_Finalization
9427 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9428 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9430 Set_Is_Controlled_Active (Derived_Type);
9432 Set_Is_Controlled_Active
9433 (Derived_Type, Is_Controlled_Active (Parent_Base));
9436 -- Minor optimization: there is no need to generate the class-wide
9437 -- entity associated with an underlying record view.
9439 if not Is_Underlying_Record_View (Derived_Type) then
9440 Make_Class_Wide_Type (Derived_Type);
9443 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9445 if Has_Discriminants (Derived_Type)
9446 and then Constraint_Present
9448 Set_Stored_Constraint
9449 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9452 if Ada_Version >= Ada_2005 then
9454 Ifaces_List : Elist_Id;
9457 -- Checks rules 3.9.4 (13/2 and 14/2)
9459 if Comes_From_Source (Derived_Type)
9460 and then not Is_Private_Type (Derived_Type)
9461 and then Is_Interface (Parent_Type)
9462 and then not Is_Interface (Derived_Type)
9464 if Is_Task_Interface (Parent_Type) then
9466 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9469 elsif Is_Protected_Interface (Parent_Type) then
9471 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9476 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9478 Check_Interfaces (N, Type_Def);
9480 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9481 -- not already in the parents.
9485 Ifaces_List => Ifaces_List,
9486 Exclude_Parents => True);
9488 Set_Interfaces (Derived_Type, Ifaces_List);
9490 -- If the derived type is the anonymous type created for
9491 -- a declaration whose parent has a constraint, propagate
9492 -- the interface list to the source type. This must be done
9493 -- prior to the completion of the analysis of the source type
9494 -- because the components in the extension may contain current
9495 -- instances whose legality depends on some ancestor.
9497 if Is_Itype (Derived_Type) then
9499 Def : constant Node_Id :=
9500 Associated_Node_For_Itype (Derived_Type);
9503 and then Nkind (Def) = N_Full_Type_Declaration
9506 (Defining_Identifier (Def), Ifaces_List);
9511 -- A type extension is automatically Ghost when one of its
9512 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9513 -- also inherited when the parent type is Ghost, but this is
9514 -- done in Build_Derived_Type as the mechanism also handles
9515 -- untagged derivations.
9517 if Implements_Ghost_Interface (Derived_Type) then
9518 Set_Is_Ghost_Entity (Derived_Type);
9524 -- STEP 4: Inherit components from the parent base and constrain them.
9525 -- Apply the second transformation described in point 6. above.
9527 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9528 or else not Has_Discriminants (Parent_Type)
9529 or else not Is_Constrained (Parent_Type)
9533 Constrs := Discriminant_Constraint (Parent_Type);
9538 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9540 -- STEP 5a: Copy the parent record declaration for untagged types
9542 Set_Has_Implicit_Dereference
9543 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9545 if not Is_Tagged then
9547 -- Discriminant_Constraint (Derived_Type) has been properly
9548 -- constructed. Save it and temporarily set it to Empty because we
9549 -- do not want the call to New_Copy_Tree below to mess this list.
9551 if Has_Discriminants (Derived_Type) then
9552 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9553 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9555 Save_Discr_Constr := No_Elist;
9558 -- Save the Etype field of Derived_Type. It is correctly set now,
9559 -- but the call to New_Copy tree may remap it to point to itself,
9560 -- which is not what we want. Ditto for the Next_Entity field.
9562 Save_Etype := Etype (Derived_Type);
9563 Save_Next_Entity := Next_Entity (Derived_Type);
9565 -- Assoc_List maps all stored discriminants in the Parent_Base to
9566 -- stored discriminants in the Derived_Type. It is fundamental that
9567 -- no types or itypes with discriminants other than the stored
9568 -- discriminants appear in the entities declared inside
9569 -- Derived_Type, since the back end cannot deal with it.
9573 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9574 Copy_Dimensions_Of_Components (Derived_Type);
9576 -- Restore the fields saved prior to the New_Copy_Tree call
9577 -- and compute the stored constraint.
9579 Set_Etype (Derived_Type, Save_Etype);
9580 Link_Entities (Derived_Type, Save_Next_Entity);
9582 if Has_Discriminants (Derived_Type) then
9583 Set_Discriminant_Constraint
9584 (Derived_Type, Save_Discr_Constr);
9585 Set_Stored_Constraint
9586 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9588 Replace_Discriminants (Derived_Type, New_Decl);
9591 -- Insert the new derived type declaration
9593 Rewrite (N, New_Decl);
9595 -- STEP 5b: Complete the processing for record extensions in generics
9597 -- There is no completion for record extensions declared in the
9598 -- parameter part of a generic, so we need to complete processing for
9599 -- these generic record extensions here. The Record_Type_Definition call
9600 -- will change the Ekind of the components from E_Void to E_Component.
9602 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9603 Record_Type_Definition (Empty, Derived_Type);
9605 -- STEP 5c: Process the record extension for non private tagged types
9607 elsif not Private_Extension then
9608 Expand_Record_Extension (Derived_Type, Type_Def);
9610 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9611 -- implemented interfaces if we are in expansion mode
9614 and then Has_Interfaces (Derived_Type)
9616 Add_Interface_Tag_Components (N, Derived_Type);
9619 -- Analyze the record extension
9621 Record_Type_Definition
9622 (Record_Extension_Part (Type_Def), Derived_Type);
9627 -- Nothing else to do if there is an error in the derivation.
9628 -- An unusual case: the full view may be derived from a type in an
9629 -- instance, when the partial view was used illegally as an actual
9630 -- in that instance, leading to a circular definition.
9632 if Etype (Derived_Type) = Any_Type
9633 or else Etype (Parent_Type) = Derived_Type
9638 -- Set delayed freeze and then derive subprograms, we need to do
9639 -- this in this order so that derived subprograms inherit the
9640 -- derived freeze if necessary.
9642 Set_Has_Delayed_Freeze (Derived_Type);
9644 if Derive_Subps then
9645 Derive_Subprograms (Parent_Type, Derived_Type);
9648 -- If we have a private extension which defines a constrained derived
9649 -- type mark as constrained here after we have derived subprograms. See
9650 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9652 if Private_Extension and then Inherit_Discrims then
9653 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9654 Set_Is_Constrained (Derived_Type, True);
9655 Set_Discriminant_Constraint (Derived_Type, Discs);
9657 elsif Is_Constrained (Parent_Type) then
9659 (Derived_Type, True);
9660 Set_Discriminant_Constraint
9661 (Derived_Type, Discriminant_Constraint (Parent_Type));
9665 -- Update the class-wide type, which shares the now-completed entity
9666 -- list with its specific type. In case of underlying record views,
9667 -- we do not generate the corresponding class wide entity.
9670 and then not Is_Underlying_Record_View (Derived_Type)
9673 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9675 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9678 Check_Function_Writable_Actuals (N);
9679 end Build_Derived_Record_Type;
9681 ------------------------
9682 -- Build_Derived_Type --
9683 ------------------------
9685 procedure Build_Derived_Type
9687 Parent_Type : Entity_Id;
9688 Derived_Type : Entity_Id;
9689 Is_Completion : Boolean;
9690 Derive_Subps : Boolean := True)
9692 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9695 -- Set common attributes
9697 Set_Scope (Derived_Type, Current_Scope);
9698 Set_Etype (Derived_Type, Parent_Base);
9699 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9700 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9702 Set_Size_Info (Derived_Type, Parent_Type);
9703 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9705 Set_Is_Controlled_Active
9706 (Derived_Type, Is_Controlled_Active (Parent_Type));
9708 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9709 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9710 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9712 if Is_Tagged_Type (Derived_Type) then
9713 Set_No_Tagged_Streams_Pragma
9714 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9717 -- If the parent has primitive routines and may have not-seen-yet aspect
9718 -- specifications (e.g., a Pack pragma), then set the derived type link
9719 -- in order to later diagnose "early derivation" issues. If in different
9720 -- compilation units, then "early derivation" cannot be an issue (and we
9721 -- don't like interunit references that go in the opposite direction of
9722 -- semantic dependencies).
9724 if Has_Primitive_Operations (Parent_Type)
9725 and then Enclosing_Comp_Unit_Node (Parent_Type) =
9726 Enclosing_Comp_Unit_Node (Derived_Type)
9728 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9731 -- If the parent type is a private subtype, the convention on the base
9732 -- type may be set in the private part, and not propagated to the
9733 -- subtype until later, so we obtain the convention from the base type.
9735 Set_Convention (Derived_Type, Convention (Parent_Base));
9737 -- Set SSO default for record or array type
9739 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9740 and then Is_Base_Type (Derived_Type)
9742 Set_Default_SSO (Derived_Type);
9745 -- A derived type inherits the Default_Initial_Condition pragma coming
9746 -- from any parent type within the derivation chain.
9748 if Has_DIC (Parent_Type) then
9749 Set_Has_Inherited_DIC (Derived_Type);
9752 -- A derived type inherits any class-wide invariants coming from a
9753 -- parent type or an interface. Note that the invariant procedure of
9754 -- the parent type should not be inherited because the derived type may
9755 -- define invariants of its own.
9757 if not Is_Interface (Derived_Type) then
9758 if Has_Inherited_Invariants (Parent_Type)
9759 or else Has_Inheritable_Invariants (Parent_Type)
9761 Set_Has_Inherited_Invariants (Derived_Type);
9763 elsif Is_Concurrent_Type (Derived_Type)
9764 or else Is_Tagged_Type (Derived_Type)
9769 Iface_Elmt : Elmt_Id;
9774 Ifaces_List => Ifaces,
9775 Exclude_Parents => True);
9777 if Present (Ifaces) then
9778 Iface_Elmt := First_Elmt (Ifaces);
9779 while Present (Iface_Elmt) loop
9780 Iface := Node (Iface_Elmt);
9782 if Has_Inheritable_Invariants (Iface) then
9783 Set_Has_Inherited_Invariants (Derived_Type);
9787 Next_Elmt (Iface_Elmt);
9794 -- We similarly inherit predicates. Note that for scalar derived types
9795 -- the predicate is inherited from the first subtype, and not from its
9796 -- (anonymous) base type.
9798 if Has_Predicates (Parent_Type)
9799 or else Has_Predicates (First_Subtype (Parent_Type))
9801 Set_Has_Predicates (Derived_Type);
9804 -- The derived type inherits representation clauses from the parent
9805 -- type, and from any interfaces.
9807 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9810 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
9812 while Present (Iface) loop
9813 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
9818 -- If the parent type has delayed rep aspects, then mark the derived
9819 -- type as possibly inheriting a delayed rep aspect.
9821 if Has_Delayed_Rep_Aspects (Parent_Type) then
9822 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9825 -- A derived type becomes Ghost when its parent type is also Ghost
9826 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9827 -- directly inherited because the Ghost policy in effect may differ.
9829 if Is_Ghost_Entity (Parent_Type) then
9830 Set_Is_Ghost_Entity (Derived_Type);
9833 -- Type dependent processing
9835 case Ekind (Parent_Type) is
9836 when Numeric_Kind =>
9837 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9840 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9842 when Class_Wide_Kind
9846 Build_Derived_Record_Type
9847 (N, Parent_Type, Derived_Type, Derive_Subps);
9850 when Enumeration_Kind =>
9851 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9854 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9856 when Incomplete_Or_Private_Kind =>
9857 Build_Derived_Private_Type
9858 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9860 -- For discriminated types, the derivation includes deriving
9861 -- primitive operations. For others it is done below.
9863 if Is_Tagged_Type (Parent_Type)
9864 or else Has_Discriminants (Parent_Type)
9865 or else (Present (Full_View (Parent_Type))
9866 and then Has_Discriminants (Full_View (Parent_Type)))
9871 when Concurrent_Kind =>
9872 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9875 raise Program_Error;
9878 -- Nothing more to do if some error occurred
9880 if Etype (Derived_Type) = Any_Type then
9884 -- Set delayed freeze and then derive subprograms, we need to do this
9885 -- in this order so that derived subprograms inherit the derived freeze
9888 Set_Has_Delayed_Freeze (Derived_Type);
9890 if Derive_Subps then
9891 Derive_Subprograms (Parent_Type, Derived_Type);
9894 Set_Has_Primitive_Operations
9895 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9896 end Build_Derived_Type;
9898 -----------------------
9899 -- Build_Discriminal --
9900 -----------------------
9902 procedure Build_Discriminal (Discrim : Entity_Id) is
9903 D_Minal : Entity_Id;
9904 CR_Disc : Entity_Id;
9907 -- A discriminal has the same name as the discriminant
9909 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9911 Set_Ekind (D_Minal, E_In_Parameter);
9912 Set_Mechanism (D_Minal, Default_Mechanism);
9913 Set_Etype (D_Minal, Etype (Discrim));
9914 Set_Scope (D_Minal, Current_Scope);
9915 Set_Parent (D_Minal, Parent (Discrim));
9917 Set_Discriminal (Discrim, D_Minal);
9918 Set_Discriminal_Link (D_Minal, Discrim);
9920 -- For task types, build at once the discriminants of the corresponding
9921 -- record, which are needed if discriminants are used in entry defaults
9922 -- and in family bounds.
9924 if Is_Concurrent_Type (Current_Scope)
9926 Is_Limited_Type (Current_Scope)
9928 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9930 Set_Ekind (CR_Disc, E_In_Parameter);
9931 Set_Mechanism (CR_Disc, Default_Mechanism);
9932 Set_Etype (CR_Disc, Etype (Discrim));
9933 Set_Scope (CR_Disc, Current_Scope);
9934 Set_Discriminal_Link (CR_Disc, Discrim);
9935 Set_CR_Discriminant (Discrim, CR_Disc);
9937 end Build_Discriminal;
9939 ------------------------------------
9940 -- Build_Discriminant_Constraints --
9941 ------------------------------------
9943 function Build_Discriminant_Constraints
9946 Derived_Def : Boolean := False) return Elist_Id
9948 C : constant Node_Id := Constraint (Def);
9949 Nb_Discr : constant Nat := Number_Discriminants (T);
9951 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9952 -- Saves the expression corresponding to a given discriminant in T
9954 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9955 -- Return the Position number within array Discr_Expr of a discriminant
9956 -- D within the discriminant list of the discriminated type T.
9958 procedure Process_Discriminant_Expression
9961 -- If this is a discriminant constraint on a partial view, do not
9962 -- generate an overflow check on the discriminant expression. The check
9963 -- will be generated when constraining the full view. Otherwise the
9964 -- backend creates duplicate symbols for the temporaries corresponding
9965 -- to the expressions to be checked, causing spurious assembler errors.
9971 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9975 Disc := First_Discriminant (T);
9976 for J in Discr_Expr'Range loop
9981 Next_Discriminant (Disc);
9984 -- Note: Since this function is called on discriminants that are
9985 -- known to belong to the discriminated type, falling through the
9986 -- loop with no match signals an internal compiler error.
9988 raise Program_Error;
9991 -------------------------------------
9992 -- Process_Discriminant_Expression --
9993 -------------------------------------
9995 procedure Process_Discriminant_Expression
9999 BDT : constant Entity_Id := Base_Type (Etype (D));
10002 -- If this is a discriminant constraint on a partial view, do
10003 -- not generate an overflow on the discriminant expression. The
10004 -- check will be generated when constraining the full view.
10006 if Is_Private_Type (T)
10007 and then Present (Full_View (T))
10009 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
10011 Analyze_And_Resolve (Expr, BDT);
10013 end Process_Discriminant_Expression;
10015 -- Declarations local to Build_Discriminant_Constraints
10019 Elist : constant Elist_Id := New_Elmt_List;
10027 Discrim_Present : Boolean := False;
10029 -- Start of processing for Build_Discriminant_Constraints
10032 -- The following loop will process positional associations only.
10033 -- For a positional association, the (single) discriminant is
10034 -- implicitly specified by position, in textual order (RM 3.7.2).
10036 Discr := First_Discriminant (T);
10037 Constr := First (Constraints (C));
10038 for D in Discr_Expr'Range loop
10039 exit when Nkind (Constr) = N_Discriminant_Association;
10041 if No (Constr) then
10042 Error_Msg_N ("too few discriminants given in constraint", C);
10043 return New_Elmt_List;
10045 elsif Nkind (Constr) = N_Range
10046 or else (Nkind (Constr) = N_Attribute_Reference
10047 and then Attribute_Name (Constr) = Name_Range)
10050 ("a range is not a valid discriminant constraint", Constr);
10051 Discr_Expr (D) := Error;
10053 elsif Nkind (Constr) = N_Subtype_Indication then
10055 ("a subtype indication is not a valid discriminant constraint",
10057 Discr_Expr (D) := Error;
10060 Process_Discriminant_Expression (Constr, Discr);
10061 Discr_Expr (D) := Constr;
10064 Next_Discriminant (Discr);
10068 if No (Discr) and then Present (Constr) then
10069 Error_Msg_N ("too many discriminants given in constraint", Constr);
10070 return New_Elmt_List;
10073 -- Named associations can be given in any order, but if both positional
10074 -- and named associations are used in the same discriminant constraint,
10075 -- then positional associations must occur first, at their normal
10076 -- position. Hence once a named association is used, the rest of the
10077 -- discriminant constraint must use only named associations.
10079 while Present (Constr) loop
10081 -- Positional association forbidden after a named association
10083 if Nkind (Constr) /= N_Discriminant_Association then
10084 Error_Msg_N ("positional association follows named one", Constr);
10085 return New_Elmt_List;
10087 -- Otherwise it is a named association
10090 -- E records the type of the discriminants in the named
10091 -- association. All the discriminants specified in the same name
10092 -- association must have the same type.
10096 -- Search the list of discriminants in T to see if the simple name
10097 -- given in the constraint matches any of them.
10099 Id := First (Selector_Names (Constr));
10100 while Present (Id) loop
10103 -- If Original_Discriminant is present, we are processing a
10104 -- generic instantiation and this is an instance node. We need
10105 -- to find the name of the corresponding discriminant in the
10106 -- actual record type T and not the name of the discriminant in
10107 -- the generic formal. Example:
10110 -- type G (D : int) is private;
10112 -- subtype W is G (D => 1);
10114 -- type Rec (X : int) is record ... end record;
10115 -- package Q is new P (G => Rec);
10117 -- At the point of the instantiation, formal type G is Rec
10118 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10119 -- which really looks like "subtype W is Rec (D => 1);" at
10120 -- the point of instantiation, we want to find the discriminant
10121 -- that corresponds to D in Rec, i.e. X.
10123 if Present (Original_Discriminant (Id))
10124 and then In_Instance
10126 Discr := Find_Corresponding_Discriminant (Id, T);
10130 Discr := First_Discriminant (T);
10131 while Present (Discr) loop
10132 if Chars (Discr) = Chars (Id) then
10137 Next_Discriminant (Discr);
10141 Error_Msg_N ("& does not match any discriminant", Id);
10142 return New_Elmt_List;
10144 -- If the parent type is a generic formal, preserve the
10145 -- name of the discriminant for subsequent instances.
10146 -- see comment at the beginning of this if statement.
10148 elsif Is_Generic_Type (Root_Type (T)) then
10149 Set_Original_Discriminant (Id, Discr);
10153 Position := Pos_Of_Discr (T, Discr);
10155 if Present (Discr_Expr (Position)) then
10156 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10159 -- Each discriminant specified in the same named association
10160 -- must be associated with a separate copy of the
10161 -- corresponding expression.
10163 if Present (Next (Id)) then
10164 Expr := New_Copy_Tree (Expression (Constr));
10165 Set_Parent (Expr, Parent (Expression (Constr)));
10167 Expr := Expression (Constr);
10170 Discr_Expr (Position) := Expr;
10171 Process_Discriminant_Expression (Expr, Discr);
10174 -- A discriminant association with more than one discriminant
10175 -- name is only allowed if the named discriminants are all of
10176 -- the same type (RM 3.7.1(8)).
10179 E := Base_Type (Etype (Discr));
10181 elsif Base_Type (Etype (Discr)) /= E then
10183 ("all discriminants in an association " &
10184 "must have the same type", Id);
10194 -- A discriminant constraint must provide exactly one value for each
10195 -- discriminant of the type (RM 3.7.1(8)).
10197 for J in Discr_Expr'Range loop
10198 if No (Discr_Expr (J)) then
10199 Error_Msg_N ("too few discriminants given in constraint", C);
10200 return New_Elmt_List;
10204 -- Determine if there are discriminant expressions in the constraint
10206 for J in Discr_Expr'Range loop
10207 if Denotes_Discriminant
10208 (Discr_Expr (J), Check_Concurrent => True)
10210 Discrim_Present := True;
10214 -- Build an element list consisting of the expressions given in the
10215 -- discriminant constraint and apply the appropriate checks. The list
10216 -- is constructed after resolving any named discriminant associations
10217 -- and therefore the expressions appear in the textual order of the
10220 Discr := First_Discriminant (T);
10221 for J in Discr_Expr'Range loop
10222 if Discr_Expr (J) /= Error then
10223 Append_Elmt (Discr_Expr (J), Elist);
10225 -- If any of the discriminant constraints is given by a
10226 -- discriminant and we are in a derived type declaration we
10227 -- have a discriminant renaming. Establish link between new
10228 -- and old discriminant. The new discriminant has an implicit
10229 -- dereference if the old one does.
10231 if Denotes_Discriminant (Discr_Expr (J)) then
10232 if Derived_Def then
10234 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10237 Set_Corresponding_Discriminant (New_Discr, Discr);
10238 Set_Has_Implicit_Dereference (New_Discr,
10239 Has_Implicit_Dereference (Discr));
10243 -- Force the evaluation of non-discriminant expressions.
10244 -- If we have found a discriminant in the constraint 3.4(26)
10245 -- and 3.8(18) demand that no range checks are performed are
10246 -- after evaluation. If the constraint is for a component
10247 -- definition that has a per-object constraint, expressions are
10248 -- evaluated but not checked either. In all other cases perform
10252 if Discrim_Present then
10255 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
10256 and then Has_Per_Object_Constraint
10257 (Defining_Identifier (Parent (Parent (Def))))
10261 elsif Is_Access_Type (Etype (Discr)) then
10262 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10265 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10268 Force_Evaluation (Discr_Expr (J));
10271 -- Check that the designated type of an access discriminant's
10272 -- expression is not a class-wide type unless the discriminant's
10273 -- designated type is also class-wide.
10275 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10276 and then not Is_Class_Wide_Type
10277 (Designated_Type (Etype (Discr)))
10278 and then Etype (Discr_Expr (J)) /= Any_Type
10279 and then Is_Class_Wide_Type
10280 (Designated_Type (Etype (Discr_Expr (J))))
10282 Wrong_Type (Discr_Expr (J), Etype (Discr));
10284 elsif Is_Access_Type (Etype (Discr))
10285 and then not Is_Access_Constant (Etype (Discr))
10286 and then Is_Access_Type (Etype (Discr_Expr (J)))
10287 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10290 ("constraint for discriminant& must be access to variable",
10295 Next_Discriminant (Discr);
10299 end Build_Discriminant_Constraints;
10301 ---------------------------------
10302 -- Build_Discriminated_Subtype --
10303 ---------------------------------
10305 procedure Build_Discriminated_Subtype
10307 Def_Id : Entity_Id;
10309 Related_Nod : Node_Id;
10310 For_Access : Boolean := False)
10312 Has_Discrs : constant Boolean := Has_Discriminants (T);
10313 Constrained : constant Boolean :=
10315 and then not Is_Empty_Elmt_List (Elist)
10316 and then not Is_Class_Wide_Type (T))
10317 or else Is_Constrained (T);
10320 if Ekind (T) = E_Record_Type then
10321 Set_Ekind (Def_Id, E_Record_Subtype);
10323 -- Inherit preelaboration flag from base, for types for which it
10324 -- may have been set: records, private types, protected types.
10326 Set_Known_To_Have_Preelab_Init
10327 (Def_Id, Known_To_Have_Preelab_Init (T));
10329 elsif Ekind (T) = E_Task_Type then
10330 Set_Ekind (Def_Id, E_Task_Subtype);
10332 elsif Ekind (T) = E_Protected_Type then
10333 Set_Ekind (Def_Id, E_Protected_Subtype);
10334 Set_Known_To_Have_Preelab_Init
10335 (Def_Id, Known_To_Have_Preelab_Init (T));
10337 elsif Is_Private_Type (T) then
10338 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10339 Set_Known_To_Have_Preelab_Init
10340 (Def_Id, Known_To_Have_Preelab_Init (T));
10342 -- Private subtypes may have private dependents
10344 Set_Private_Dependents (Def_Id, New_Elmt_List);
10346 elsif Is_Class_Wide_Type (T) then
10347 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
10350 -- Incomplete type. Attach subtype to list of dependents, to be
10351 -- completed with full view of parent type, unless is it the
10352 -- designated subtype of a record component within an init_proc.
10353 -- This last case arises for a component of an access type whose
10354 -- designated type is incomplete (e.g. a Taft Amendment type).
10355 -- The designated subtype is within an inner scope, and needs no
10356 -- elaboration, because only the access type is needed in the
10357 -- initialization procedure.
10359 if Ekind (T) = E_Incomplete_Type then
10360 Set_Ekind (Def_Id, E_Incomplete_Subtype);
10362 Set_Ekind (Def_Id, Ekind (T));
10365 if For_Access and then Within_Init_Proc then
10368 Append_Elmt (Def_Id, Private_Dependents (T));
10372 Set_Etype (Def_Id, T);
10373 Init_Size_Align (Def_Id);
10374 Set_Has_Discriminants (Def_Id, Has_Discrs);
10375 Set_Is_Constrained (Def_Id, Constrained);
10377 Set_First_Entity (Def_Id, First_Entity (T));
10378 Set_Last_Entity (Def_Id, Last_Entity (T));
10379 Set_Has_Implicit_Dereference
10380 (Def_Id, Has_Implicit_Dereference (T));
10381 Set_Has_Pragma_Unreferenced_Objects
10382 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10384 -- If the subtype is the completion of a private declaration, there may
10385 -- have been representation clauses for the partial view, and they must
10386 -- be preserved. Build_Derived_Type chains the inherited clauses with
10387 -- the ones appearing on the extension. If this comes from a subtype
10388 -- declaration, all clauses are inherited.
10390 if No (First_Rep_Item (Def_Id)) then
10391 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10394 if Is_Tagged_Type (T) then
10395 Set_Is_Tagged_Type (Def_Id);
10396 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10397 Make_Class_Wide_Type (Def_Id);
10400 Set_Stored_Constraint (Def_Id, No_Elist);
10403 Set_Discriminant_Constraint (Def_Id, Elist);
10404 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10407 if Is_Tagged_Type (T) then
10409 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10410 -- concurrent record type (which has the list of primitive
10413 if Ada_Version >= Ada_2005
10414 and then Is_Concurrent_Type (T)
10416 Set_Corresponding_Record_Type (Def_Id,
10417 Corresponding_Record_Type (T));
10419 Set_Direct_Primitive_Operations (Def_Id,
10420 Direct_Primitive_Operations (T));
10423 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10426 -- Subtypes introduced by component declarations do not need to be
10427 -- marked as delayed, and do not get freeze nodes, because the semantics
10428 -- verifies that the parents of the subtypes are frozen before the
10429 -- enclosing record is frozen.
10431 if not Is_Type (Scope (Def_Id)) then
10432 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10434 if Is_Private_Type (T)
10435 and then Present (Full_View (T))
10437 Conditional_Delay (Def_Id, Full_View (T));
10439 Conditional_Delay (Def_Id, T);
10443 if Is_Record_Type (T) then
10444 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10447 and then not Is_Empty_Elmt_List (Elist)
10448 and then not For_Access
10450 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10453 Set_Cloned_Subtype (Def_Id, T);
10456 end Build_Discriminated_Subtype;
10458 ---------------------------
10459 -- Build_Itype_Reference --
10460 ---------------------------
10462 procedure Build_Itype_Reference
10466 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10469 -- Itype references are only created for use by the back-end
10471 if Inside_A_Generic then
10474 Set_Itype (IR, Ityp);
10476 -- If Nod is a library unit entity, then Insert_After won't work,
10477 -- because Nod is not a member of any list. Therefore, we use
10478 -- Add_Global_Declaration in this case. This can happen if we have a
10479 -- build-in-place library function, child unit or not.
10481 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10482 or else (Nkind_In (Nod, N_Defining_Program_Unit_Name,
10483 N_Subprogram_Declaration)
10484 and then Is_Compilation_Unit (Defining_Entity (Nod)))
10486 Add_Global_Declaration (IR);
10488 Insert_After (Nod, IR);
10491 end Build_Itype_Reference;
10493 ------------------------
10494 -- Build_Scalar_Bound --
10495 ------------------------
10497 function Build_Scalar_Bound
10500 Der_T : Entity_Id) return Node_Id
10502 New_Bound : Entity_Id;
10505 -- Note: not clear why this is needed, how can the original bound
10506 -- be unanalyzed at this point? and if it is, what business do we
10507 -- have messing around with it? and why is the base type of the
10508 -- parent type the right type for the resolution. It probably is
10509 -- not. It is OK for the new bound we are creating, but not for
10510 -- the old one??? Still if it never happens, no problem.
10512 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10514 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10515 New_Bound := New_Copy (Bound);
10516 Set_Etype (New_Bound, Der_T);
10517 Set_Analyzed (New_Bound);
10519 elsif Is_Entity_Name (Bound) then
10520 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10522 -- The following is almost certainly wrong. What business do we have
10523 -- relocating a node (Bound) that is presumably still attached to
10524 -- the tree elsewhere???
10527 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10530 Set_Etype (New_Bound, Der_T);
10532 end Build_Scalar_Bound;
10534 -------------------------------
10535 -- Check_Abstract_Overriding --
10536 -------------------------------
10538 procedure Check_Abstract_Overriding (T : Entity_Id) is
10539 Alias_Subp : Entity_Id;
10541 Op_List : Elist_Id;
10543 Type_Def : Node_Id;
10545 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10546 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10547 -- which has pragma Implemented already set. Check whether Subp's entity
10548 -- kind conforms to the implementation kind of the overridden routine.
10550 procedure Check_Pragma_Implemented
10552 Iface_Subp : Entity_Id);
10553 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10554 -- Iface_Subp and both entities have pragma Implemented already set on
10555 -- them. Check whether the two implementation kinds are conforming.
10557 procedure Inherit_Pragma_Implemented
10559 Iface_Subp : Entity_Id);
10560 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10561 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10562 -- Propagate the implementation kind of Iface_Subp to Subp.
10564 ------------------------------
10565 -- Check_Pragma_Implemented --
10566 ------------------------------
10568 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10569 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10570 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10571 Subp_Alias : constant Entity_Id := Alias (Subp);
10572 Contr_Typ : Entity_Id;
10573 Impl_Subp : Entity_Id;
10576 -- Subp must have an alias since it is a hidden entity used to link
10577 -- an interface subprogram to its overriding counterpart.
10579 pragma Assert (Present (Subp_Alias));
10581 -- Handle aliases to synchronized wrappers
10583 Impl_Subp := Subp_Alias;
10585 if Is_Primitive_Wrapper (Impl_Subp) then
10586 Impl_Subp := Wrapped_Entity (Impl_Subp);
10589 -- Extract the type of the controlling formal
10591 Contr_Typ := Etype (First_Formal (Subp_Alias));
10593 if Is_Concurrent_Record_Type (Contr_Typ) then
10594 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10597 -- An interface subprogram whose implementation kind is By_Entry must
10598 -- be implemented by an entry.
10600 if Impl_Kind = Name_By_Entry
10601 and then Ekind (Impl_Subp) /= E_Entry
10603 Error_Msg_Node_2 := Iface_Alias;
10605 ("type & must implement abstract subprogram & with an entry",
10606 Subp_Alias, Contr_Typ);
10608 elsif Impl_Kind = Name_By_Protected_Procedure then
10610 -- An interface subprogram whose implementation kind is By_
10611 -- Protected_Procedure cannot be implemented by a primitive
10612 -- procedure of a task type.
10614 if Ekind (Contr_Typ) /= E_Protected_Type then
10615 Error_Msg_Node_2 := Contr_Typ;
10617 ("interface subprogram & cannot be implemented by a "
10618 & "primitive procedure of task type &",
10619 Subp_Alias, Iface_Alias);
10621 -- An interface subprogram whose implementation kind is By_
10622 -- Protected_Procedure must be implemented by a procedure.
10624 elsif Ekind (Impl_Subp) /= E_Procedure then
10625 Error_Msg_Node_2 := Iface_Alias;
10627 ("type & must implement abstract subprogram & with a "
10628 & "procedure", Subp_Alias, Contr_Typ);
10630 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10631 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10633 Error_Msg_Name_1 := Impl_Kind;
10635 ("overriding operation& must have synchronization%",
10639 -- If primitive has Optional synchronization, overriding operation
10640 -- must match if it has an explicit synchronization.
10642 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10643 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10645 Error_Msg_Name_1 := Impl_Kind;
10647 ("overriding operation& must have synchronization%", Subp_Alias);
10649 end Check_Pragma_Implemented;
10651 ------------------------------
10652 -- Check_Pragma_Implemented --
10653 ------------------------------
10655 procedure Check_Pragma_Implemented
10657 Iface_Subp : Entity_Id)
10659 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10660 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10663 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10664 -- and overriding subprogram are different. In general this is an
10665 -- error except when the implementation kind of the overridden
10666 -- subprograms is By_Any or Optional.
10668 if Iface_Kind /= Subp_Kind
10669 and then Iface_Kind /= Name_By_Any
10670 and then Iface_Kind /= Name_Optional
10672 if Iface_Kind = Name_By_Entry then
10674 ("incompatible implementation kind, overridden subprogram " &
10675 "is marked By_Entry", Subp);
10678 ("incompatible implementation kind, overridden subprogram " &
10679 "is marked By_Protected_Procedure", Subp);
10682 end Check_Pragma_Implemented;
10684 --------------------------------
10685 -- Inherit_Pragma_Implemented --
10686 --------------------------------
10688 procedure Inherit_Pragma_Implemented
10690 Iface_Subp : Entity_Id)
10692 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10693 Loc : constant Source_Ptr := Sloc (Subp);
10694 Impl_Prag : Node_Id;
10697 -- Since the implementation kind is stored as a representation item
10698 -- rather than a flag, create a pragma node.
10702 Chars => Name_Implemented,
10703 Pragma_Argument_Associations => New_List (
10704 Make_Pragma_Argument_Association (Loc,
10705 Expression => New_Occurrence_Of (Subp, Loc)),
10707 Make_Pragma_Argument_Association (Loc,
10708 Expression => Make_Identifier (Loc, Iface_Kind))));
10710 -- The pragma doesn't need to be analyzed because it is internally
10711 -- built. It is safe to directly register it as a rep item since we
10712 -- are only interested in the characters of the implementation kind.
10714 Record_Rep_Item (Subp, Impl_Prag);
10715 end Inherit_Pragma_Implemented;
10717 -- Start of processing for Check_Abstract_Overriding
10720 Op_List := Primitive_Operations (T);
10722 -- Loop to check primitive operations
10724 Elmt := First_Elmt (Op_List);
10725 while Present (Elmt) loop
10726 Subp := Node (Elmt);
10727 Alias_Subp := Alias (Subp);
10729 -- Inherited subprograms are identified by the fact that they do not
10730 -- come from source, and the associated source location is the
10731 -- location of the first subtype of the derived type.
10733 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10734 -- subprograms that "require overriding".
10736 -- Special exception, do not complain about failure to override the
10737 -- stream routines _Input and _Output, as well as the primitive
10738 -- operations used in dispatching selects since we always provide
10739 -- automatic overridings for these subprograms.
10741 -- The partial view of T may have been a private extension, for
10742 -- which inherited functions dispatching on result are abstract.
10743 -- If the full view is a null extension, there is no need for
10744 -- overriding in Ada 2005, but wrappers need to be built for them
10745 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10747 if Is_Null_Extension (T)
10748 and then Has_Controlling_Result (Subp)
10749 and then Ada_Version >= Ada_2005
10750 and then Present (Alias_Subp)
10751 and then not Comes_From_Source (Subp)
10752 and then not Is_Abstract_Subprogram (Alias_Subp)
10753 and then not Is_Access_Type (Etype (Subp))
10757 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10758 -- processing because this check is done with the aliased
10761 elsif Present (Interface_Alias (Subp)) then
10764 elsif (Is_Abstract_Subprogram (Subp)
10765 or else Requires_Overriding (Subp)
10767 (Has_Controlling_Result (Subp)
10768 and then Present (Alias_Subp)
10769 and then not Comes_From_Source (Subp)
10770 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10771 and then not Is_TSS (Subp, TSS_Stream_Input)
10772 and then not Is_TSS (Subp, TSS_Stream_Output)
10773 and then not Is_Abstract_Type (T)
10774 and then not Is_Predefined_Interface_Primitive (Subp)
10776 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10777 -- with abstract interface types because the check will be done
10778 -- with the aliased entity (otherwise we generate a duplicated
10781 and then not Present (Interface_Alias (Subp))
10783 if Present (Alias_Subp) then
10785 -- Only perform the check for a derived subprogram when the
10786 -- type has an explicit record extension. This avoids incorrect
10787 -- flagging of abstract subprograms for the case of a type
10788 -- without an extension that is derived from a formal type
10789 -- with a tagged actual (can occur within a private part).
10791 -- Ada 2005 (AI-391): In the case of an inherited function with
10792 -- a controlling result of the type, the rule does not apply if
10793 -- the type is a null extension (unless the parent function
10794 -- itself is abstract, in which case the function must still be
10795 -- be overridden). The expander will generate an overriding
10796 -- wrapper function calling the parent subprogram (see
10797 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10799 Type_Def := Type_Definition (Parent (T));
10801 if Nkind (Type_Def) = N_Derived_Type_Definition
10802 and then Present (Record_Extension_Part (Type_Def))
10804 (Ada_Version < Ada_2005
10805 or else not Is_Null_Extension (T)
10806 or else Ekind (Subp) = E_Procedure
10807 or else not Has_Controlling_Result (Subp)
10808 or else Is_Abstract_Subprogram (Alias_Subp)
10809 or else Requires_Overriding (Subp)
10810 or else Is_Access_Type (Etype (Subp)))
10812 -- Avoid reporting error in case of abstract predefined
10813 -- primitive inherited from interface type because the
10814 -- body of internally generated predefined primitives
10815 -- of tagged types are generated later by Freeze_Type
10817 if Is_Interface (Root_Type (T))
10818 and then Is_Abstract_Subprogram (Subp)
10819 and then Is_Predefined_Dispatching_Operation (Subp)
10820 and then not Comes_From_Source (Ultimate_Alias (Subp))
10824 -- A null extension is not obliged to override an inherited
10825 -- procedure subject to pragma Extensions_Visible with value
10826 -- False and at least one controlling OUT parameter
10827 -- (SPARK RM 6.1.7(6)).
10829 elsif Is_Null_Extension (T)
10830 and then Is_EVF_Procedure (Subp)
10836 ("type must be declared abstract or & overridden",
10839 -- Traverse the whole chain of aliased subprograms to
10840 -- complete the error notification. This is especially
10841 -- useful for traceability of the chain of entities when
10842 -- the subprogram corresponds with an interface
10843 -- subprogram (which may be defined in another package).
10845 if Present (Alias_Subp) then
10851 while Present (Alias (E)) loop
10853 -- Avoid reporting redundant errors on entities
10854 -- inherited from interfaces
10856 if Sloc (E) /= Sloc (T) then
10857 Error_Msg_Sloc := Sloc (E);
10859 ("\& has been inherited #", T, Subp);
10865 Error_Msg_Sloc := Sloc (E);
10867 -- AI05-0068: report if there is an overriding
10868 -- non-abstract subprogram that is invisible.
10871 and then not Is_Abstract_Subprogram (E)
10874 ("\& subprogram# is not visible",
10877 -- Clarify the case where a non-null extension must
10878 -- override inherited procedure subject to pragma
10879 -- Extensions_Visible with value False and at least
10880 -- one controlling OUT param.
10882 elsif Is_EVF_Procedure (E) then
10884 ("\& # is subject to Extensions_Visible False",
10889 ("\& has been inherited from subprogram #",
10896 -- Ada 2005 (AI-345): Protected or task type implementing
10897 -- abstract interfaces.
10899 elsif Is_Concurrent_Record_Type (T)
10900 and then Present (Interfaces (T))
10902 -- There is no need to check here RM 9.4(11.9/3) since we
10903 -- are processing the corresponding record type and the
10904 -- mode of the overriding subprograms was verified by
10905 -- Check_Conformance when the corresponding concurrent
10906 -- type declaration was analyzed.
10909 ("interface subprogram & must be overridden", T, Subp);
10911 -- Examine primitive operations of synchronized type to find
10912 -- homonyms that have the wrong profile.
10918 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10919 while Present (Prim) loop
10920 if Chars (Prim) = Chars (Subp) then
10922 ("profile is not type conformant with prefixed "
10923 & "view profile of inherited operation&",
10927 Next_Entity (Prim);
10933 Error_Msg_Node_2 := T;
10935 ("abstract subprogram& not allowed for type&", Subp);
10937 -- Also post unconditional warning on the type (unconditional
10938 -- so that if there are more than one of these cases, we get
10939 -- them all, and not just the first one).
10941 Error_Msg_Node_2 := Subp;
10942 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10945 -- A subprogram subject to pragma Extensions_Visible with value
10946 -- "True" cannot override a subprogram subject to the same pragma
10947 -- with value "False" (SPARK RM 6.1.7(5)).
10949 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10950 and then Present (Overridden_Operation (Subp))
10951 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10952 Extensions_Visible_False
10954 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10956 ("subprogram & with Extensions_Visible True cannot override "
10957 & "subprogram # with Extensions_Visible False", Subp);
10960 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10962 -- Subp is an expander-generated procedure which maps an interface
10963 -- alias to a protected wrapper. The interface alias is flagged by
10964 -- pragma Implemented. Ensure that Subp is a procedure when the
10965 -- implementation kind is By_Protected_Procedure or an entry when
10968 if Ada_Version >= Ada_2012
10969 and then Is_Hidden (Subp)
10970 and then Present (Interface_Alias (Subp))
10971 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10973 Check_Pragma_Implemented (Subp);
10976 -- Subp is an interface primitive which overrides another interface
10977 -- primitive marked with pragma Implemented.
10979 if Ada_Version >= Ada_2012
10980 and then Present (Overridden_Operation (Subp))
10981 and then Has_Rep_Pragma
10982 (Overridden_Operation (Subp), Name_Implemented)
10984 -- If the overriding routine is also marked by Implemented, check
10985 -- that the two implementation kinds are conforming.
10987 if Has_Rep_Pragma (Subp, Name_Implemented) then
10988 Check_Pragma_Implemented
10990 Iface_Subp => Overridden_Operation (Subp));
10992 -- Otherwise the overriding routine inherits the implementation
10993 -- kind from the overridden subprogram.
10996 Inherit_Pragma_Implemented
10998 Iface_Subp => Overridden_Operation (Subp));
11002 -- Ada 2005 (AI95-0414) and Ada 2020 (AI12-0269): Diagnose failure to
11003 -- match No_Return in parent, but do it unconditionally in Ada 95 too
11004 -- for procedures, since this is our pragma.
11006 if Present (Overridden_Operation (Subp))
11007 and then No_Return (Overridden_Operation (Subp))
11008 and then not No_Return (Subp)
11010 Error_Msg_N ("overriding subprogram & must be No_Return", Subp);
11012 ("\since overridden subprogram is No_Return (RM 6.5.1(6/2))",
11016 -- If the operation is a wrapper for a synchronized primitive, it
11017 -- may be called indirectly through a dispatching select. We assume
11018 -- that it will be referenced elsewhere indirectly, and suppress
11019 -- warnings about an unused entity.
11021 if Is_Primitive_Wrapper (Subp)
11022 and then Present (Wrapped_Entity (Subp))
11024 Set_Referenced (Wrapped_Entity (Subp));
11029 end Check_Abstract_Overriding;
11031 ------------------------------------------------
11032 -- Check_Access_Discriminant_Requires_Limited --
11033 ------------------------------------------------
11035 procedure Check_Access_Discriminant_Requires_Limited
11040 -- A discriminant_specification for an access discriminant shall appear
11041 -- only in the declaration for a task or protected type, or for a type
11042 -- with the reserved word 'limited' in its definition or in one of its
11043 -- ancestors (RM 3.7(10)).
11045 -- AI-0063: The proper condition is that type must be immutably limited,
11046 -- or else be a partial view.
11048 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11049 if Is_Limited_View (Current_Scope)
11051 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11052 and then Limited_Present (Parent (Current_Scope)))
11058 ("access discriminants allowed only for limited types", Loc);
11061 end Check_Access_Discriminant_Requires_Limited;
11063 -----------------------------------
11064 -- Check_Aliased_Component_Types --
11065 -----------------------------------
11067 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11071 -- ??? Also need to check components of record extensions, but not
11072 -- components of protected types (which are always limited).
11074 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11075 -- types to be unconstrained. This is safe because it is illegal to
11076 -- create access subtypes to such types with explicit discriminant
11079 if not Is_Limited_Type (T) then
11080 if Ekind (T) = E_Record_Type then
11081 C := First_Component (T);
11082 while Present (C) loop
11084 and then Has_Discriminants (Etype (C))
11085 and then not Is_Constrained (Etype (C))
11086 and then not In_Instance_Body
11087 and then Ada_Version < Ada_2005
11090 ("aliased component must be constrained (RM 3.6(11))",
11094 Next_Component (C);
11097 elsif Ekind (T) = E_Array_Type then
11098 if Has_Aliased_Components (T)
11099 and then Has_Discriminants (Component_Type (T))
11100 and then not Is_Constrained (Component_Type (T))
11101 and then not In_Instance_Body
11102 and then Ada_Version < Ada_2005
11105 ("aliased component type must be constrained (RM 3.6(11))",
11110 end Check_Aliased_Component_Types;
11112 ---------------------------------------
11113 -- Check_Anonymous_Access_Components --
11114 ---------------------------------------
11116 procedure Check_Anonymous_Access_Components
11117 (Typ_Decl : Node_Id;
11120 Comp_List : Node_Id)
11122 Loc : constant Source_Ptr := Sloc (Typ_Decl);
11123 Anon_Access : Entity_Id;
11126 Comp_Def : Node_Id;
11128 Type_Def : Node_Id;
11130 procedure Build_Incomplete_Type_Declaration;
11131 -- If the record type contains components that include an access to the
11132 -- current record, then create an incomplete type declaration for the
11133 -- record, to be used as the designated type of the anonymous access.
11134 -- This is done only once, and only if there is no previous partial
11135 -- view of the type.
11137 function Designates_T (Subt : Node_Id) return Boolean;
11138 -- Check whether a node designates the enclosing record type, or 'Class
11141 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11142 -- Check whether an access definition includes a reference to
11143 -- the enclosing record type. The reference can be a subtype mark
11144 -- in the access definition itself, a 'Class attribute reference, or
11145 -- recursively a reference appearing in a parameter specification
11146 -- or result definition of an access_to_subprogram definition.
11148 --------------------------------------
11149 -- Build_Incomplete_Type_Declaration --
11150 --------------------------------------
11152 procedure Build_Incomplete_Type_Declaration is
11157 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11158 -- it's "is new ... with record" or else "is tagged record ...".
11160 Is_Tagged : constant Boolean :=
11161 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
11163 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
11165 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
11166 and then Tagged_Present (Type_Definition (Typ_Decl)));
11169 -- If there is a previous partial view, no need to create a new one
11170 -- If the partial view, given by Prev, is incomplete, If Prev is
11171 -- a private declaration, full declaration is flagged accordingly.
11173 if Prev /= Typ then
11175 Make_Class_Wide_Type (Prev);
11176 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11177 Set_Etype (Class_Wide_Type (Typ), Typ);
11182 elsif Has_Private_Declaration (Typ) then
11184 -- If we refer to T'Class inside T, and T is the completion of a
11185 -- private type, then make sure the class-wide type exists.
11188 Make_Class_Wide_Type (Typ);
11193 -- If there was a previous anonymous access type, the incomplete
11194 -- type declaration will have been created already.
11196 elsif Present (Current_Entity (Typ))
11197 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11198 and then Full_View (Current_Entity (Typ)) = Typ
11201 and then Comes_From_Source (Current_Entity (Typ))
11202 and then not Is_Tagged_Type (Current_Entity (Typ))
11204 Make_Class_Wide_Type (Typ);
11206 ("incomplete view of tagged type should be declared tagged??",
11207 Parent (Current_Entity (Typ)));
11212 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11213 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11215 -- Type has already been inserted into the current scope. Remove
11216 -- it, and add incomplete declaration for type, so that subsequent
11217 -- anonymous access types can use it. The entity is unchained from
11218 -- the homonym list and from immediate visibility. After analysis,
11219 -- the entity in the incomplete declaration becomes immediately
11220 -- visible in the record declaration that follows.
11222 H := Current_Entity (Typ);
11225 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11228 and then Homonym (H) /= Typ
11230 H := Homonym (Typ);
11233 Set_Homonym (H, Homonym (Typ));
11236 Insert_Before (Typ_Decl, Decl);
11238 Set_Full_View (Inc_T, Typ);
11242 -- Create a common class-wide type for both views, and set the
11243 -- Etype of the class-wide type to the full view.
11245 Make_Class_Wide_Type (Inc_T);
11246 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11247 Set_Etype (Class_Wide_Type (Typ), Typ);
11250 end Build_Incomplete_Type_Declaration;
11256 function Designates_T (Subt : Node_Id) return Boolean is
11257 Type_Id : constant Name_Id := Chars (Typ);
11259 function Names_T (Nam : Node_Id) return Boolean;
11260 -- The record type has not been introduced in the current scope
11261 -- yet, so we must examine the name of the type itself, either
11262 -- an identifier T, or an expanded name of the form P.T, where
11263 -- P denotes the current scope.
11269 function Names_T (Nam : Node_Id) return Boolean is
11271 if Nkind (Nam) = N_Identifier then
11272 return Chars (Nam) = Type_Id;
11274 elsif Nkind (Nam) = N_Selected_Component then
11275 if Chars (Selector_Name (Nam)) = Type_Id then
11276 if Nkind (Prefix (Nam)) = N_Identifier then
11277 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11279 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11280 return Chars (Selector_Name (Prefix (Nam))) =
11281 Chars (Current_Scope);
11295 -- Start of processing for Designates_T
11298 if Nkind (Subt) = N_Identifier then
11299 return Chars (Subt) = Type_Id;
11301 -- Reference can be through an expanded name which has not been
11302 -- analyzed yet, and which designates enclosing scopes.
11304 elsif Nkind (Subt) = N_Selected_Component then
11305 if Names_T (Subt) then
11308 -- Otherwise it must denote an entity that is already visible.
11309 -- The access definition may name a subtype of the enclosing
11310 -- type, if there is a previous incomplete declaration for it.
11313 Find_Selected_Component (Subt);
11315 Is_Entity_Name (Subt)
11316 and then Scope (Entity (Subt)) = Current_Scope
11318 (Chars (Base_Type (Entity (Subt))) = Type_Id
11320 (Is_Class_Wide_Type (Entity (Subt))
11322 Chars (Etype (Base_Type (Entity (Subt)))) =
11326 -- A reference to the current type may appear as the prefix of
11327 -- a 'Class attribute.
11329 elsif Nkind (Subt) = N_Attribute_Reference
11330 and then Attribute_Name (Subt) = Name_Class
11332 return Names_T (Prefix (Subt));
11343 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11344 Param_Spec : Node_Id;
11346 Acc_Subprg : constant Node_Id :=
11347 Access_To_Subprogram_Definition (Acc_Def);
11350 if No (Acc_Subprg) then
11351 return Designates_T (Subtype_Mark (Acc_Def));
11354 -- Component is an access_to_subprogram: examine its formals,
11355 -- and result definition in the case of an access_to_function.
11357 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11358 while Present (Param_Spec) loop
11359 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11360 and then Mentions_T (Parameter_Type (Param_Spec))
11364 elsif Designates_T (Parameter_Type (Param_Spec)) then
11371 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11372 if Nkind (Result_Definition (Acc_Subprg)) =
11373 N_Access_Definition
11375 return Mentions_T (Result_Definition (Acc_Subprg));
11377 return Designates_T (Result_Definition (Acc_Subprg));
11384 -- Start of processing for Check_Anonymous_Access_Components
11387 if No (Comp_List) then
11391 Comp := First (Component_Items (Comp_List));
11392 while Present (Comp) loop
11393 if Nkind (Comp) = N_Component_Declaration
11395 (Access_Definition (Component_Definition (Comp)))
11397 Mentions_T (Access_Definition (Component_Definition (Comp)))
11399 Comp_Def := Component_Definition (Comp);
11401 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11403 Build_Incomplete_Type_Declaration;
11404 Anon_Access := Make_Temporary (Loc, 'S');
11406 -- Create a declaration for the anonymous access type: either
11407 -- an access_to_object or an access_to_subprogram.
11409 if Present (Acc_Def) then
11410 if Nkind (Acc_Def) = N_Access_Function_Definition then
11412 Make_Access_Function_Definition (Loc,
11413 Parameter_Specifications =>
11414 Parameter_Specifications (Acc_Def),
11415 Result_Definition => Result_Definition (Acc_Def));
11418 Make_Access_Procedure_Definition (Loc,
11419 Parameter_Specifications =>
11420 Parameter_Specifications (Acc_Def));
11425 Make_Access_To_Object_Definition (Loc,
11426 Subtype_Indication =>
11428 (Subtype_Mark (Access_Definition (Comp_Def))));
11430 Set_Constant_Present
11431 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11433 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11436 Set_Null_Exclusion_Present
11438 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11441 Make_Full_Type_Declaration (Loc,
11442 Defining_Identifier => Anon_Access,
11443 Type_Definition => Type_Def);
11445 Insert_Before (Typ_Decl, Decl);
11448 -- If an access to subprogram, create the extra formals
11450 if Present (Acc_Def) then
11451 Create_Extra_Formals (Designated_Type (Anon_Access));
11455 Make_Component_Definition (Loc,
11456 Subtype_Indication =>
11457 New_Occurrence_Of (Anon_Access, Loc)));
11459 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11460 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11462 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11465 Set_Is_Local_Anonymous_Access (Anon_Access);
11471 if Present (Variant_Part (Comp_List)) then
11475 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11476 while Present (V) loop
11477 Check_Anonymous_Access_Components
11478 (Typ_Decl, Typ, Prev, Component_List (V));
11479 Next_Non_Pragma (V);
11483 end Check_Anonymous_Access_Components;
11485 ----------------------
11486 -- Check_Completion --
11487 ----------------------
11489 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11492 procedure Post_Error;
11493 -- Post error message for lack of completion for entity E
11499 procedure Post_Error is
11500 procedure Missing_Body;
11501 -- Output missing body message
11507 procedure Missing_Body is
11509 -- Spec is in same unit, so we can post on spec
11511 if In_Same_Source_Unit (Body_Id, E) then
11512 Error_Msg_N ("missing body for &", E);
11514 -- Spec is in a separate unit, so we have to post on the body
11517 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11521 -- Start of processing for Post_Error
11524 if not Comes_From_Source (E) then
11525 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11527 -- It may be an anonymous protected type created for a
11528 -- single variable. Post error on variable, if present.
11534 Var := First_Entity (Current_Scope);
11535 while Present (Var) loop
11536 exit when Etype (Var) = E
11537 and then Comes_From_Source (Var);
11542 if Present (Var) then
11549 -- If a generated entity has no completion, then either previous
11550 -- semantic errors have disabled the expansion phase, or else we had
11551 -- missing subunits, or else we are compiling without expansion,
11552 -- or else something is very wrong.
11554 if not Comes_From_Source (E) then
11556 (Serious_Errors_Detected > 0
11557 or else Configurable_Run_Time_Violations > 0
11558 or else Subunits_Missing
11559 or else not Expander_Active);
11562 -- Here for source entity
11565 -- Here if no body to post the error message, so we post the error
11566 -- on the declaration that has no completion. This is not really
11567 -- the right place to post it, think about this later ???
11569 if No (Body_Id) then
11570 if Is_Type (E) then
11572 ("missing full declaration for }", Parent (E), E);
11574 Error_Msg_NE ("missing body for &", Parent (E), E);
11577 -- Package body has no completion for a declaration that appears
11578 -- in the corresponding spec. Post error on the body, with a
11579 -- reference to the non-completed declaration.
11582 Error_Msg_Sloc := Sloc (E);
11584 if Is_Type (E) then
11585 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11587 elsif Is_Overloadable (E)
11588 and then Current_Entity_In_Scope (E) /= E
11590 -- It may be that the completion is mistyped and appears as
11591 -- a distinct overloading of the entity.
11594 Candidate : constant Entity_Id :=
11595 Current_Entity_In_Scope (E);
11596 Decl : constant Node_Id :=
11597 Unit_Declaration_Node (Candidate);
11600 if Is_Overloadable (Candidate)
11601 and then Ekind (Candidate) = Ekind (E)
11602 and then Nkind (Decl) = N_Subprogram_Body
11603 and then Acts_As_Spec (Decl)
11605 Check_Type_Conformant (Candidate, E);
11621 Pack_Id : constant Entity_Id := Current_Scope;
11623 -- Start of processing for Check_Completion
11626 E := First_Entity (Pack_Id);
11627 while Present (E) loop
11628 if Is_Intrinsic_Subprogram (E) then
11631 -- The following situation requires special handling: a child unit
11632 -- that appears in the context clause of the body of its parent:
11634 -- procedure Parent.Child (...);
11636 -- with Parent.Child;
11637 -- package body Parent is
11639 -- Here Parent.Child appears as a local entity, but should not be
11640 -- flagged as requiring completion, because it is a compilation
11643 -- Ignore missing completion for a subprogram that does not come from
11644 -- source (including the _Call primitive operation of RAS types,
11645 -- which has to have the flag Comes_From_Source for other purposes):
11646 -- we assume that the expander will provide the missing completion.
11647 -- In case of previous errors, other expansion actions that provide
11648 -- bodies for null procedures with not be invoked, so inhibit message
11651 -- Note that E_Operator is not in the list that follows, because
11652 -- this kind is reserved for predefined operators, that are
11653 -- intrinsic and do not need completion.
11655 elsif Ekind_In (E, E_Function,
11657 E_Generic_Function,
11658 E_Generic_Procedure)
11660 if Has_Completion (E) then
11663 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11666 elsif Is_Subprogram (E)
11667 and then (not Comes_From_Source (E)
11668 or else Chars (E) = Name_uCall)
11673 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11677 elsif Nkind (Parent (E)) = N_Procedure_Specification
11678 and then Null_Present (Parent (E))
11679 and then Serious_Errors_Detected > 0
11687 elsif Is_Entry (E) then
11688 if not Has_Completion (E) and then
11689 (Ekind (Scope (E)) = E_Protected_Object
11690 or else Ekind (Scope (E)) = E_Protected_Type)
11695 elsif Is_Package_Or_Generic_Package (E) then
11696 if Unit_Requires_Body (E) then
11697 if not Has_Completion (E)
11698 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11704 elsif not Is_Child_Unit (E) then
11705 May_Need_Implicit_Body (E);
11708 -- A formal incomplete type (Ada 2012) does not require a completion;
11709 -- other incomplete type declarations do.
11711 elsif Ekind (E) = E_Incomplete_Type
11712 and then No (Underlying_Type (E))
11713 and then not Is_Generic_Type (E)
11717 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11718 and then not Has_Completion (E)
11722 -- A single task declared in the current scope is a constant, verify
11723 -- that the body of its anonymous type is in the same scope. If the
11724 -- task is defined elsewhere, this may be a renaming declaration for
11725 -- which no completion is needed.
11727 elsif Ekind (E) = E_Constant
11728 and then Ekind (Etype (E)) = E_Task_Type
11729 and then not Has_Completion (Etype (E))
11730 and then Scope (Etype (E)) = Current_Scope
11734 elsif Ekind (E) = E_Protected_Object
11735 and then not Has_Completion (Etype (E))
11739 elsif Ekind (E) = E_Record_Type then
11740 if Is_Tagged_Type (E) then
11741 Check_Abstract_Overriding (E);
11742 Check_Conventions (E);
11745 Check_Aliased_Component_Types (E);
11747 elsif Ekind (E) = E_Array_Type then
11748 Check_Aliased_Component_Types (E);
11754 end Check_Completion;
11756 -------------------------------------
11757 -- Check_Constraining_Discriminant --
11758 -------------------------------------
11760 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id)
11762 New_Type : constant Entity_Id := Etype (New_Disc);
11763 Old_Type : Entity_Id;
11766 -- If the record type contains an array constrained by the discriminant
11767 -- but with some different bound, the compiler tries to create a smaller
11768 -- range for the discriminant type (see exp_ch3.Adjust_Discriminants).
11769 -- In this case, where the discriminant type is a scalar type, the check
11770 -- must use the original discriminant type in the parent declaration.
11772 if Is_Scalar_Type (New_Type) then
11773 Old_Type := Entity (Discriminant_Type (Parent (Old_Disc)));
11775 Old_Type := Etype (Old_Disc);
11778 if not Subtypes_Statically_Compatible (New_Type, Old_Type) then
11780 ("subtype must be statically compatible with parent discriminant",
11783 if not Predicates_Compatible (New_Type, Old_Type) then
11785 ("\subtype predicate is not compatible with parent discriminant",
11789 end Check_Constraining_Discriminant;
11791 ------------------------------------
11792 -- Check_CPP_Type_Has_No_Defaults --
11793 ------------------------------------
11795 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11796 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11801 -- Obtain the component list
11803 if Nkind (Tdef) = N_Record_Definition then
11804 Clist := Component_List (Tdef);
11805 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11806 Clist := Component_List (Record_Extension_Part (Tdef));
11809 -- Check all components to ensure no default expressions
11811 if Present (Clist) then
11812 Comp := First (Component_Items (Clist));
11813 while Present (Comp) loop
11814 if Present (Expression (Comp)) then
11816 ("component of imported 'C'P'P type cannot have "
11817 & "default expression", Expression (Comp));
11823 end Check_CPP_Type_Has_No_Defaults;
11825 ----------------------------
11826 -- Check_Delta_Expression --
11827 ----------------------------
11829 procedure Check_Delta_Expression (E : Node_Id) is
11831 if not (Is_Real_Type (Etype (E))) then
11832 Wrong_Type (E, Any_Real);
11834 elsif not Is_OK_Static_Expression (E) then
11835 Flag_Non_Static_Expr
11836 ("non-static expression used for delta value!", E);
11838 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11839 Error_Msg_N ("delta expression must be positive", E);
11845 -- If any of above errors occurred, then replace the incorrect
11846 -- expression by the real 0.1, which should prevent further errors.
11849 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11850 Analyze_And_Resolve (E, Standard_Float);
11851 end Check_Delta_Expression;
11853 -----------------------------
11854 -- Check_Digits_Expression --
11855 -----------------------------
11857 procedure Check_Digits_Expression (E : Node_Id) is
11859 if not (Is_Integer_Type (Etype (E))) then
11860 Wrong_Type (E, Any_Integer);
11862 elsif not Is_OK_Static_Expression (E) then
11863 Flag_Non_Static_Expr
11864 ("non-static expression used for digits value!", E);
11866 elsif Expr_Value (E) <= 0 then
11867 Error_Msg_N ("digits value must be greater than zero", E);
11873 -- If any of above errors occurred, then replace the incorrect
11874 -- expression by the integer 1, which should prevent further errors.
11876 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11877 Analyze_And_Resolve (E, Standard_Integer);
11879 end Check_Digits_Expression;
11881 --------------------------
11882 -- Check_Initialization --
11883 --------------------------
11885 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11887 -- Special processing for limited types
11889 if Is_Limited_Type (T)
11890 and then not In_Instance
11891 and then not In_Inlined_Body
11893 if not OK_For_Limited_Init (T, Exp) then
11895 -- In GNAT mode, this is just a warning, to allow it to be evilly
11896 -- turned off. Otherwise it is a real error.
11900 ("??cannot initialize entities of limited type!", Exp);
11902 elsif Ada_Version < Ada_2005 then
11904 -- The side effect removal machinery may generate illegal Ada
11905 -- code to avoid the usage of access types and 'reference in
11906 -- SPARK mode. Since this is legal code with respect to theorem
11907 -- proving, do not emit the error.
11910 and then Nkind (Exp) = N_Function_Call
11911 and then Nkind (Parent (Exp)) = N_Object_Declaration
11912 and then not Comes_From_Source
11913 (Defining_Identifier (Parent (Exp)))
11919 ("cannot initialize entities of limited type", Exp);
11920 Explain_Limited_Type (T, Exp);
11924 -- Specialize error message according to kind of illegal
11925 -- initial expression. We check the Original_Node to cover
11926 -- cases where the initialization expression of an object
11927 -- declaration generated by the compiler has been rewritten
11928 -- (such as for dispatching calls).
11930 if Nkind (Original_Node (Exp)) = N_Type_Conversion
11932 Nkind (Expression (Original_Node (Exp))) = N_Function_Call
11934 -- No error for internally-generated object declarations,
11935 -- which can come from build-in-place assignment statements.
11937 if Nkind (Parent (Exp)) = N_Object_Declaration
11938 and then not Comes_From_Source
11939 (Defining_Identifier (Parent (Exp)))
11945 ("illegal context for call to function with limited "
11951 ("initialization of limited object requires aggregate or "
11952 & "function call", Exp);
11958 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11959 -- set unless we can be sure that no range check is required.
11961 if not Expander_Active
11962 and then Is_Scalar_Type (T)
11963 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11965 Set_Do_Range_Check (Exp);
11967 end Check_Initialization;
11969 ----------------------
11970 -- Check_Interfaces --
11971 ----------------------
11973 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11974 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11977 Iface_Def : Node_Id;
11978 Iface_Typ : Entity_Id;
11979 Parent_Node : Node_Id;
11981 Is_Task : Boolean := False;
11982 -- Set True if parent type or any progenitor is a task interface
11984 Is_Protected : Boolean := False;
11985 -- Set True if parent type or any progenitor is a protected interface
11987 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11988 -- Check that a progenitor is compatible with declaration. If an error
11989 -- message is output, it is posted on Error_Node.
11995 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11996 Iface_Id : constant Entity_Id :=
11997 Defining_Identifier (Parent (Iface_Def));
11998 Type_Def : Node_Id;
12001 if Nkind (N) = N_Private_Extension_Declaration then
12004 Type_Def := Type_Definition (N);
12007 if Is_Task_Interface (Iface_Id) then
12010 elsif Is_Protected_Interface (Iface_Id) then
12011 Is_Protected := True;
12014 if Is_Synchronized_Interface (Iface_Id) then
12016 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
12017 -- extension derived from a synchronized interface must explicitly
12018 -- be declared synchronized, because the full view will be a
12019 -- synchronized type.
12021 if Nkind (N) = N_Private_Extension_Declaration then
12022 if not Synchronized_Present (N) then
12024 ("private extension of& must be explicitly synchronized",
12028 -- However, by 3.9.4(16/2), a full type that is a record extension
12029 -- is never allowed to derive from a synchronized interface (note
12030 -- that interfaces must be excluded from this check, because those
12031 -- are represented by derived type definitions in some cases).
12033 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12034 and then not Interface_Present (Type_Definition (N))
12036 Error_Msg_N ("record extension cannot derive from synchronized "
12037 & "interface", Error_Node);
12041 -- Check that the characteristics of the progenitor are compatible
12042 -- with the explicit qualifier in the declaration.
12043 -- The check only applies to qualifiers that come from source.
12044 -- Limited_Present also appears in the declaration of corresponding
12045 -- records, and the check does not apply to them.
12047 if Limited_Present (Type_Def)
12049 Is_Concurrent_Record_Type (Defining_Identifier (N))
12051 if Is_Limited_Interface (Parent_Type)
12052 and then not Is_Limited_Interface (Iface_Id)
12055 ("progenitor & must be limited interface",
12056 Error_Node, Iface_Id);
12059 (Task_Present (Iface_Def)
12060 or else Protected_Present (Iface_Def)
12061 or else Synchronized_Present (Iface_Def))
12062 and then Nkind (N) /= N_Private_Extension_Declaration
12063 and then not Error_Posted (N)
12066 ("progenitor & must be limited interface",
12067 Error_Node, Iface_Id);
12070 -- Protected interfaces can only inherit from limited, synchronized
12071 -- or protected interfaces.
12073 elsif Nkind (N) = N_Full_Type_Declaration
12074 and then Protected_Present (Type_Def)
12076 if Limited_Present (Iface_Def)
12077 or else Synchronized_Present (Iface_Def)
12078 or else Protected_Present (Iface_Def)
12082 elsif Task_Present (Iface_Def) then
12083 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12084 & "from task interface", Error_Node);
12087 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12088 & "from non-limited interface", Error_Node);
12091 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12092 -- limited and synchronized.
12094 elsif Synchronized_Present (Type_Def) then
12095 if Limited_Present (Iface_Def)
12096 or else Synchronized_Present (Iface_Def)
12100 elsif Protected_Present (Iface_Def)
12101 and then Nkind (N) /= N_Private_Extension_Declaration
12103 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12104 & "from protected interface", Error_Node);
12106 elsif Task_Present (Iface_Def)
12107 and then Nkind (N) /= N_Private_Extension_Declaration
12109 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12110 & "from task interface", Error_Node);
12112 elsif not Is_Limited_Interface (Iface_Id) then
12113 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12114 & "from non-limited interface", Error_Node);
12117 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12118 -- synchronized or task interfaces.
12120 elsif Nkind (N) = N_Full_Type_Declaration
12121 and then Task_Present (Type_Def)
12123 if Limited_Present (Iface_Def)
12124 or else Synchronized_Present (Iface_Def)
12125 or else Task_Present (Iface_Def)
12129 elsif Protected_Present (Iface_Def) then
12130 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12131 & "protected interface", Error_Node);
12134 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12135 & "non-limited interface", Error_Node);
12140 -- Start of processing for Check_Interfaces
12143 if Is_Interface (Parent_Type) then
12144 if Is_Task_Interface (Parent_Type) then
12147 elsif Is_Protected_Interface (Parent_Type) then
12148 Is_Protected := True;
12152 if Nkind (N) = N_Private_Extension_Declaration then
12154 -- Check that progenitors are compatible with declaration
12156 Iface := First (Interface_List (Def));
12157 while Present (Iface) loop
12158 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12160 Parent_Node := Parent (Base_Type (Iface_Typ));
12161 Iface_Def := Type_Definition (Parent_Node);
12163 if not Is_Interface (Iface_Typ) then
12164 Diagnose_Interface (Iface, Iface_Typ);
12166 Check_Ifaces (Iface_Def, Iface);
12172 if Is_Task and Is_Protected then
12174 ("type cannot derive from task and protected interface", N);
12180 -- Full type declaration of derived type.
12181 -- Check compatibility with parent if it is interface type
12183 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12184 and then Is_Interface (Parent_Type)
12186 Parent_Node := Parent (Parent_Type);
12188 -- More detailed checks for interface varieties
12191 (Iface_Def => Type_Definition (Parent_Node),
12192 Error_Node => Subtype_Indication (Type_Definition (N)));
12195 Iface := First (Interface_List (Def));
12196 while Present (Iface) loop
12197 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12199 Parent_Node := Parent (Base_Type (Iface_Typ));
12200 Iface_Def := Type_Definition (Parent_Node);
12202 if not Is_Interface (Iface_Typ) then
12203 Diagnose_Interface (Iface, Iface_Typ);
12206 -- "The declaration of a specific descendant of an interface
12207 -- type freezes the interface type" RM 13.14
12209 Freeze_Before (N, Iface_Typ);
12210 Check_Ifaces (Iface_Def, Error_Node => Iface);
12216 if Is_Task and Is_Protected then
12218 ("type cannot derive from task and protected interface", N);
12220 end Check_Interfaces;
12222 ------------------------------------
12223 -- Check_Or_Process_Discriminants --
12224 ------------------------------------
12226 -- If an incomplete or private type declaration was already given for the
12227 -- type, the discriminants may have already been processed if they were
12228 -- present on the incomplete declaration. In this case a full conformance
12229 -- check has been performed in Find_Type_Name, and we then recheck here
12230 -- some properties that can't be checked on the partial view alone.
12231 -- Otherwise we call Process_Discriminants.
12233 procedure Check_Or_Process_Discriminants
12236 Prev : Entity_Id := Empty)
12239 if Has_Discriminants (T) then
12241 -- Discriminants are already set on T if they were already present
12242 -- on the partial view. Make them visible to component declarations.
12246 -- Discriminant on T (full view) referencing expr on partial view
12248 Prev_D : Entity_Id;
12249 -- Entity of corresponding discriminant on partial view
12252 -- Discriminant specification for full view, expression is
12253 -- the syntactic copy on full view (which has been checked for
12254 -- conformance with partial view), only used here to post error
12258 D := First_Discriminant (T);
12259 New_D := First (Discriminant_Specifications (N));
12260 while Present (D) loop
12261 Prev_D := Current_Entity (D);
12262 Set_Current_Entity (D);
12263 Set_Is_Immediately_Visible (D);
12264 Set_Homonym (D, Prev_D);
12266 -- Handle the case where there is an untagged partial view and
12267 -- the full view is tagged: must disallow discriminants with
12268 -- defaults, unless compiling for Ada 2012, which allows a
12269 -- limited tagged type to have defaulted discriminants (see
12270 -- AI05-0214). However, suppress error here if it was already
12271 -- reported on the default expression of the partial view.
12273 if Is_Tagged_Type (T)
12274 and then Present (Expression (Parent (D)))
12275 and then (not Is_Limited_Type (Current_Scope)
12276 or else Ada_Version < Ada_2012)
12277 and then not Error_Posted (Expression (Parent (D)))
12279 if Ada_Version >= Ada_2012 then
12281 ("discriminants of nonlimited tagged type cannot have "
12283 Expression (New_D));
12286 ("discriminants of tagged type cannot have defaults",
12287 Expression (New_D));
12291 -- Ada 2005 (AI-230): Access discriminant allowed in
12292 -- non-limited record types.
12294 if Ada_Version < Ada_2005 then
12296 -- This restriction gets applied to the full type here. It
12297 -- has already been applied earlier to the partial view.
12299 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12302 Next_Discriminant (D);
12307 elsif Present (Discriminant_Specifications (N)) then
12308 Process_Discriminants (N, Prev);
12310 end Check_Or_Process_Discriminants;
12312 ----------------------
12313 -- Check_Real_Bound --
12314 ----------------------
12316 procedure Check_Real_Bound (Bound : Node_Id) is
12318 if not Is_Real_Type (Etype (Bound)) then
12320 ("bound in real type definition must be of real type", Bound);
12322 elsif not Is_OK_Static_Expression (Bound) then
12323 Flag_Non_Static_Expr
12324 ("non-static expression used for real type bound!", Bound);
12331 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12333 Resolve (Bound, Standard_Float);
12334 end Check_Real_Bound;
12336 ------------------------------
12337 -- Complete_Private_Subtype --
12338 ------------------------------
12340 procedure Complete_Private_Subtype
12343 Full_Base : Entity_Id;
12344 Related_Nod : Node_Id)
12346 Save_Next_Entity : Entity_Id;
12347 Save_Homonym : Entity_Id;
12350 -- Set semantic attributes for (implicit) private subtype completion.
12351 -- If the full type has no discriminants, then it is a copy of the
12352 -- full view of the base. Otherwise, it is a subtype of the base with
12353 -- a possible discriminant constraint. Save and restore the original
12354 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12355 -- not corrupt the entity chain.
12357 Save_Next_Entity := Next_Entity (Full);
12358 Save_Homonym := Homonym (Priv);
12360 if Is_Private_Type (Full_Base)
12361 or else Is_Record_Type (Full_Base)
12362 or else Is_Concurrent_Type (Full_Base)
12364 Copy_Node (Priv, Full);
12366 -- Note that the Etype of the full view is the same as the Etype of
12367 -- the partial view. In this fashion, the subtype has access to the
12368 -- correct view of the parent.
12370 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
12371 Set_Has_Unknown_Discriminants
12372 (Full, Has_Unknown_Discriminants (Full_Base));
12373 Set_First_Entity (Full, First_Entity (Full_Base));
12374 Set_Last_Entity (Full, Last_Entity (Full_Base));
12376 -- If the underlying base type is constrained, we know that the
12377 -- full view of the subtype is constrained as well (the converse
12378 -- is not necessarily true).
12380 if Is_Constrained (Full_Base) then
12381 Set_Is_Constrained (Full);
12385 Copy_Node (Full_Base, Full);
12387 -- The following subtlety with the Etype of the full view needs to be
12388 -- taken into account here. One could think that it must naturally be
12389 -- set to the base type of the full base:
12391 -- Set_Etype (Full, Base_Type (Full_Base));
12393 -- so that the full view becomes a subtype of the full base when the
12394 -- latter is a base type, which must for example happen when the full
12395 -- base is declared as derived type. That's also correct if the full
12396 -- base is declared as an array type, or a floating-point type, or a
12397 -- fixed-point type, or a signed integer type, as these declarations
12398 -- create an implicit base type and a first subtype so the Etype of
12399 -- the full views must be the implicit base type. But that's wrong
12400 -- if the full base is declared as an access type, or an enumeration
12401 -- type, or a modular integer type, as these declarations directly
12402 -- create a base type, i.e. with Etype pointing to itself. Moreover
12403 -- the full base being declared in the private part, i.e. when the
12404 -- views are swapped, the end result is that the Etype of the full
12405 -- base is set to its private view in this case and that we need to
12406 -- propagate this setting to the full view in order for the subtype
12407 -- to be compatible with the base type.
12409 if Is_Base_Type (Full_Base)
12410 and then (Is_Derived_Type (Full_Base)
12411 or else Ekind (Full_Base) in Array_Kind
12412 or else Ekind (Full_Base) in Fixed_Point_Kind
12413 or else Ekind (Full_Base) in Float_Kind
12414 or else Ekind (Full_Base) in Signed_Integer_Kind)
12416 Set_Etype (Full, Full_Base);
12419 Set_Chars (Full, Chars (Priv));
12420 Set_Sloc (Full, Sloc (Priv));
12421 Conditional_Delay (Full, Priv);
12424 Link_Entities (Full, Save_Next_Entity);
12425 Set_Homonym (Full, Save_Homonym);
12426 Set_Associated_Node_For_Itype (Full, Related_Nod);
12428 -- Set common attributes for all subtypes: kind, convention, etc.
12430 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12431 Set_Convention (Full, Convention (Full_Base));
12432 Set_Is_First_Subtype (Full, False);
12433 Set_Scope (Full, Scope (Priv));
12434 Set_Size_Info (Full, Full_Base);
12435 Set_RM_Size (Full, RM_Size (Full_Base));
12436 Set_Is_Itype (Full);
12438 -- A subtype of a private-type-without-discriminants, whose full-view
12439 -- has discriminants with default expressions, is not constrained.
12441 if not Has_Discriminants (Priv) then
12442 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12444 if Has_Discriminants (Full_Base) then
12445 Set_Discriminant_Constraint
12446 (Full, Discriminant_Constraint (Full_Base));
12448 -- The partial view may have been indefinite, the full view
12451 Set_Has_Unknown_Discriminants
12452 (Full, Has_Unknown_Discriminants (Full_Base));
12456 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12457 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12459 -- Freeze the private subtype entity if its parent is delayed, and not
12460 -- already frozen. We skip this processing if the type is an anonymous
12461 -- subtype of a record component, or is the corresponding record of a
12462 -- protected type, since these are processed when the enclosing type
12463 -- is frozen. If the parent type is declared in a nested package then
12464 -- the freezing of the private and full views also happens later.
12466 if not Is_Type (Scope (Full)) then
12468 and then In_Same_Source_Unit (Full, Full_Base)
12469 and then Scope (Full_Base) /= Scope (Full)
12471 Set_Has_Delayed_Freeze (Full);
12472 Set_Has_Delayed_Freeze (Priv);
12475 Set_Has_Delayed_Freeze (Full,
12476 Has_Delayed_Freeze (Full_Base)
12477 and then not Is_Frozen (Full_Base));
12481 Set_Freeze_Node (Full, Empty);
12482 Set_Is_Frozen (Full, False);
12484 if Has_Discriminants (Full) then
12485 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12486 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12488 if Has_Unknown_Discriminants (Full) then
12489 Set_Discriminant_Constraint (Full, No_Elist);
12493 if Ekind (Full_Base) = E_Record_Type
12494 and then Has_Discriminants (Full_Base)
12495 and then Has_Discriminants (Priv) -- might not, if errors
12496 and then not Has_Unknown_Discriminants (Priv)
12497 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12499 Create_Constrained_Components
12500 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12502 -- If the full base is itself derived from private, build a congruent
12503 -- subtype of its underlying full view, for use by the back end.
12505 elsif Is_Private_Type (Full_Base)
12506 and then Present (Underlying_Full_View (Full_Base))
12509 Underlying_Full_Base : constant Entity_Id
12510 := Underlying_Full_View (Full_Base);
12511 Underlying_Full : constant Entity_Id
12512 := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
12514 Set_Is_Itype (Underlying_Full);
12515 Set_Associated_Node_For_Itype (Underlying_Full, Related_Nod);
12516 Complete_Private_Subtype
12517 (Priv, Underlying_Full, Underlying_Full_Base, Related_Nod);
12518 Set_Underlying_Full_View (Full, Underlying_Full);
12519 Set_Is_Underlying_Full_View (Underlying_Full);
12522 elsif Is_Record_Type (Full_Base) then
12524 -- Show Full is simply a renaming of Full_Base
12526 Set_Cloned_Subtype (Full, Full_Base);
12527 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
12529 -- Propagate predicates
12531 Propagate_Predicate_Attributes (Full, Full_Base);
12534 -- It is unsafe to share the bounds of a scalar type, because the Itype
12535 -- is elaborated on demand, and if a bound is nonstatic, then different
12536 -- orders of elaboration in different units will lead to different
12537 -- external symbols.
12539 if Is_Scalar_Type (Full_Base) then
12540 Set_Scalar_Range (Full,
12541 Make_Range (Sloc (Related_Nod),
12543 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12545 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12547 -- This completion inherits the bounds of the full parent, but if
12548 -- the parent is an unconstrained floating point type, so is the
12551 if Is_Floating_Point_Type (Full_Base) then
12552 Set_Includes_Infinities
12553 (Scalar_Range (Full), Has_Infinities (Full_Base));
12557 -- ??? It seems that a lot of fields are missing that should be copied
12558 -- from Full_Base to Full. Here are some that are introduced in a
12559 -- non-disruptive way but a cleanup is necessary.
12561 if Is_Tagged_Type (Full_Base) then
12562 Set_Is_Tagged_Type (Full);
12563 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
12565 Set_Direct_Primitive_Operations
12566 (Full, Direct_Primitive_Operations (Full_Base));
12567 Set_No_Tagged_Streams_Pragma
12568 (Full, No_Tagged_Streams_Pragma (Full_Base));
12570 if Is_Interface (Full_Base) then
12571 Set_Is_Interface (Full);
12572 Set_Is_Limited_Interface (Full, Is_Limited_Interface (Full_Base));
12575 -- Inherit class_wide type of full_base in case the partial view was
12576 -- not tagged. Otherwise it has already been created when the private
12577 -- subtype was analyzed.
12579 if No (Class_Wide_Type (Full)) then
12580 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12583 -- If this is a subtype of a protected or task type, constrain its
12584 -- corresponding record, unless this is a subtype without constraints,
12585 -- i.e. a simple renaming as with an actual subtype in an instance.
12587 elsif Is_Concurrent_Type (Full_Base) then
12588 if Has_Discriminants (Full)
12589 and then Present (Corresponding_Record_Type (Full_Base))
12591 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12593 Set_Corresponding_Record_Type (Full,
12594 Constrain_Corresponding_Record
12595 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12598 Set_Corresponding_Record_Type (Full,
12599 Corresponding_Record_Type (Full_Base));
12603 -- Link rep item chain, and also setting of Has_Predicates from private
12604 -- subtype to full subtype, since we will need these on the full subtype
12605 -- to create the predicate function. Note that the full subtype may
12606 -- already have rep items, inherited from the full view of the base
12607 -- type, so we must be sure not to overwrite these entries.
12612 Next_Item : Node_Id;
12613 Priv_Item : Node_Id;
12616 Item := First_Rep_Item (Full);
12617 Priv_Item := First_Rep_Item (Priv);
12619 -- If no existing rep items on full type, we can just link directly
12620 -- to the list of items on the private type, if any exist.. Same if
12621 -- the rep items are only those inherited from the base
12624 or else Nkind (Item) /= N_Aspect_Specification
12625 or else Entity (Item) = Full_Base)
12626 and then Present (First_Rep_Item (Priv))
12628 Set_First_Rep_Item (Full, Priv_Item);
12630 -- Otherwise, search to the end of items currently linked to the full
12631 -- subtype and append the private items to the end. However, if Priv
12632 -- and Full already have the same list of rep items, then the append
12633 -- is not done, as that would create a circularity.
12635 -- The partial view may have a predicate and the rep item lists of
12636 -- both views agree when inherited from the same ancestor. In that
12637 -- case, simply propagate the list from one view to the other.
12638 -- A more complex analysis needed here ???
12640 elsif Present (Priv_Item)
12641 and then Item = Next_Rep_Item (Priv_Item)
12643 Set_First_Rep_Item (Full, Priv_Item);
12645 elsif Item /= Priv_Item then
12648 Next_Item := Next_Rep_Item (Item);
12649 exit when No (Next_Item);
12652 -- If the private view has aspect specifications, the full view
12653 -- inherits them. Since these aspects may already have been
12654 -- attached to the full view during derivation, do not append
12655 -- them if already present.
12657 if Item = First_Rep_Item (Priv) then
12663 -- And link the private type items at the end of the chain
12666 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12671 -- Make sure Has_Predicates is set on full type if it is set on the
12672 -- private type. Note that it may already be set on the full type and
12673 -- if so, we don't want to unset it. Similarly, propagate information
12674 -- about delayed aspects, because the corresponding pragmas must be
12675 -- analyzed when one of the views is frozen. This last step is needed
12676 -- in particular when the full type is a scalar type for which an
12677 -- anonymous base type is constructed.
12679 -- The predicate functions are generated either at the freeze point
12680 -- of the type or at the end of the visible part, and we must avoid
12681 -- generating them twice.
12683 Propagate_Predicate_Attributes (Full, Priv);
12685 if Has_Delayed_Aspects (Priv) then
12686 Set_Has_Delayed_Aspects (Full);
12688 end Complete_Private_Subtype;
12690 ----------------------------
12691 -- Constant_Redeclaration --
12692 ----------------------------
12694 procedure Constant_Redeclaration
12699 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12700 Obj_Def : constant Node_Id := Object_Definition (N);
12703 procedure Check_Possible_Deferred_Completion
12704 (Prev_Id : Entity_Id;
12705 Prev_Obj_Def : Node_Id;
12706 Curr_Obj_Def : Node_Id);
12707 -- Determine whether the two object definitions describe the partial
12708 -- and the full view of a constrained deferred constant. Generate
12709 -- a subtype for the full view and verify that it statically matches
12710 -- the subtype of the partial view.
12712 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12713 -- If deferred constant is an access type initialized with an allocator,
12714 -- check whether there is an illegal recursion in the definition,
12715 -- through a default value of some record subcomponent. This is normally
12716 -- detected when generating init procs, but requires this additional
12717 -- mechanism when expansion is disabled.
12719 ----------------------------------------
12720 -- Check_Possible_Deferred_Completion --
12721 ----------------------------------------
12723 procedure Check_Possible_Deferred_Completion
12724 (Prev_Id : Entity_Id;
12725 Prev_Obj_Def : Node_Id;
12726 Curr_Obj_Def : Node_Id)
12729 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12730 and then Present (Constraint (Prev_Obj_Def))
12731 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12732 and then Present (Constraint (Curr_Obj_Def))
12735 Loc : constant Source_Ptr := Sloc (N);
12736 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12737 Decl : constant Node_Id :=
12738 Make_Subtype_Declaration (Loc,
12739 Defining_Identifier => Def_Id,
12740 Subtype_Indication =>
12741 Relocate_Node (Curr_Obj_Def));
12744 Insert_Before_And_Analyze (N, Decl);
12745 Set_Etype (Id, Def_Id);
12747 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12748 Error_Msg_Sloc := Sloc (Prev_Id);
12749 Error_Msg_N ("subtype does not statically match deferred "
12750 & "declaration #", N);
12754 end Check_Possible_Deferred_Completion;
12756 ---------------------------------
12757 -- Check_Recursive_Declaration --
12758 ---------------------------------
12760 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12764 if Is_Record_Type (Typ) then
12765 Comp := First_Component (Typ);
12766 while Present (Comp) loop
12767 if Comes_From_Source (Comp) then
12768 if Present (Expression (Parent (Comp)))
12769 and then Is_Entity_Name (Expression (Parent (Comp)))
12770 and then Entity (Expression (Parent (Comp))) = Prev
12772 Error_Msg_Sloc := Sloc (Parent (Comp));
12774 ("illegal circularity with declaration for & #",
12778 elsif Is_Record_Type (Etype (Comp)) then
12779 Check_Recursive_Declaration (Etype (Comp));
12783 Next_Component (Comp);
12786 end Check_Recursive_Declaration;
12788 -- Start of processing for Constant_Redeclaration
12791 if Nkind (Parent (Prev)) = N_Object_Declaration then
12792 if Nkind (Object_Definition
12793 (Parent (Prev))) = N_Subtype_Indication
12795 -- Find type of new declaration. The constraints of the two
12796 -- views must match statically, but there is no point in
12797 -- creating an itype for the full view.
12799 if Nkind (Obj_Def) = N_Subtype_Indication then
12800 Find_Type (Subtype_Mark (Obj_Def));
12801 New_T := Entity (Subtype_Mark (Obj_Def));
12804 Find_Type (Obj_Def);
12805 New_T := Entity (Obj_Def);
12811 -- The full view may impose a constraint, even if the partial
12812 -- view does not, so construct the subtype.
12814 New_T := Find_Type_Of_Object (Obj_Def, N);
12819 -- Current declaration is illegal, diagnosed below in Enter_Name
12825 -- If previous full declaration or a renaming declaration exists, or if
12826 -- a homograph is present, let Enter_Name handle it, either with an
12827 -- error or with the removal of an overridden implicit subprogram.
12828 -- The previous one is a full declaration if it has an expression
12829 -- (which in the case of an aggregate is indicated by the Init flag).
12831 if Ekind (Prev) /= E_Constant
12832 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12833 or else Present (Expression (Parent (Prev)))
12834 or else Has_Init_Expression (Parent (Prev))
12835 or else Present (Full_View (Prev))
12839 -- Verify that types of both declarations match, or else that both types
12840 -- are anonymous access types whose designated subtypes statically match
12841 -- (as allowed in Ada 2005 by AI-385).
12843 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12845 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12846 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12847 or else Is_Access_Constant (Etype (New_T)) /=
12848 Is_Access_Constant (Etype (Prev))
12849 or else Can_Never_Be_Null (Etype (New_T)) /=
12850 Can_Never_Be_Null (Etype (Prev))
12851 or else Null_Exclusion_Present (Parent (Prev)) /=
12852 Null_Exclusion_Present (Parent (Id))
12853 or else not Subtypes_Statically_Match
12854 (Designated_Type (Etype (Prev)),
12855 Designated_Type (Etype (New_T))))
12857 Error_Msg_Sloc := Sloc (Prev);
12858 Error_Msg_N ("type does not match declaration#", N);
12859 Set_Full_View (Prev, Id);
12860 Set_Etype (Id, Any_Type);
12862 -- A deferred constant whose type is an anonymous array is always
12863 -- illegal (unless imported). A detailed error message might be
12864 -- helpful for Ada beginners.
12866 if Nkind (Object_Definition (Parent (Prev)))
12867 = N_Constrained_Array_Definition
12868 and then Nkind (Object_Definition (N))
12869 = N_Constrained_Array_Definition
12871 Error_Msg_N ("\each anonymous array is a distinct type", N);
12872 Error_Msg_N ("a deferred constant must have a named type",
12873 Object_Definition (Parent (Prev)));
12877 Null_Exclusion_Present (Parent (Prev))
12878 and then not Null_Exclusion_Present (N)
12880 Error_Msg_Sloc := Sloc (Prev);
12881 Error_Msg_N ("null-exclusion does not match declaration#", N);
12882 Set_Full_View (Prev, Id);
12883 Set_Etype (Id, Any_Type);
12885 -- If so, process the full constant declaration
12888 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12889 -- the deferred declaration is constrained, then the subtype defined
12890 -- by the subtype_indication in the full declaration shall match it
12893 Check_Possible_Deferred_Completion
12895 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12896 Curr_Obj_Def => Obj_Def);
12898 Set_Full_View (Prev, Id);
12899 Set_Is_Public (Id, Is_Public (Prev));
12900 Set_Is_Internal (Id);
12901 Append_Entity (Id, Current_Scope);
12903 -- Check ALIASED present if present before (RM 7.4(7))
12905 if Is_Aliased (Prev)
12906 and then not Aliased_Present (N)
12908 Error_Msg_Sloc := Sloc (Prev);
12909 Error_Msg_N ("ALIASED required (see declaration #)", N);
12912 -- Check that placement is in private part and that the incomplete
12913 -- declaration appeared in the visible part.
12915 if Ekind (Current_Scope) = E_Package
12916 and then not In_Private_Part (Current_Scope)
12918 Error_Msg_Sloc := Sloc (Prev);
12920 ("full constant for declaration # must be in private part", N);
12922 elsif Ekind (Current_Scope) = E_Package
12924 List_Containing (Parent (Prev)) /=
12925 Visible_Declarations (Package_Specification (Current_Scope))
12928 ("deferred constant must be declared in visible part",
12932 if Is_Access_Type (T)
12933 and then Nkind (Expression (N)) = N_Allocator
12935 Check_Recursive_Declaration (Designated_Type (T));
12938 -- A deferred constant is a visible entity. If type has invariants,
12939 -- verify that the initial value satisfies them. This is not done in
12940 -- GNATprove mode, as GNATprove handles invariant checks itself.
12942 if Has_Invariants (T)
12943 and then Present (Invariant_Procedure (T))
12944 and then not GNATprove_Mode
12947 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12950 end Constant_Redeclaration;
12952 ----------------------
12953 -- Constrain_Access --
12954 ----------------------
12956 procedure Constrain_Access
12957 (Def_Id : in out Entity_Id;
12959 Related_Nod : Node_Id)
12961 T : constant Entity_Id := Entity (Subtype_Mark (S));
12962 Desig_Type : constant Entity_Id := Designated_Type (T);
12963 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12964 Constraint_OK : Boolean := True;
12967 if Is_Array_Type (Desig_Type) then
12968 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12970 elsif (Is_Record_Type (Desig_Type)
12971 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12972 and then not Is_Constrained (Desig_Type)
12974 -- If this is a constrained access definition for a record
12975 -- component, we leave the type as an unconstrained access,
12976 -- and mark the component so that its actual type is built
12977 -- at a point of use (e.g., an assignment statement). This
12978 -- is handled in Sem_Util.Build_Actual_Subtype_Of_Component.
12980 if Desig_Type = Current_Scope
12981 and then No (Def_Id)
12985 (E_Void, Related_Nod, Scope_Id => Scope (Desig_Type));
12986 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12987 Def_Id := Entity (Subtype_Mark (S));
12989 -- We indicate that the component has a per-object constraint
12990 -- for treatment at a point of use, even though the constraint
12991 -- may be independent of discriminants of the enclosing type.
12993 if Nkind (Related_Nod) = N_Component_Declaration then
12994 Set_Has_Per_Object_Constraint
12995 (Defining_Identifier (Related_Nod));
12998 -- This call added to ensure that the constraint is analyzed
12999 -- (needed for a B test). Note that we still return early from
13000 -- this procedure to avoid recursive processing.
13002 Constrain_Discriminated_Type
13003 (Desig_Subtype, S, Related_Nod, For_Access => True);
13007 -- Enforce rule that the constraint is illegal if there is an
13008 -- unconstrained view of the designated type. This means that the
13009 -- partial view (either a private type declaration or a derivation
13010 -- from a private type) has no discriminants. (Defect Report
13011 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
13013 -- Rule updated for Ada 2005: The private type is said to have
13014 -- a constrained partial view, given that objects of the type
13015 -- can be declared. Furthermore, the rule applies to all access
13016 -- types, unlike the rule concerning default discriminants (see
13019 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
13020 and then Has_Private_Declaration (Desig_Type)
13021 and then In_Open_Scopes (Scope (Desig_Type))
13022 and then Has_Discriminants (Desig_Type)
13025 Pack : constant Node_Id :=
13026 Unit_Declaration_Node (Scope (Desig_Type));
13031 if Nkind (Pack) = N_Package_Declaration then
13032 Decls := Visible_Declarations (Specification (Pack));
13033 Decl := First (Decls);
13034 while Present (Decl) loop
13035 if (Nkind (Decl) = N_Private_Type_Declaration
13036 and then Chars (Defining_Identifier (Decl)) =
13037 Chars (Desig_Type))
13040 (Nkind (Decl) = N_Full_Type_Declaration
13042 Chars (Defining_Identifier (Decl)) =
13044 and then Is_Derived_Type (Desig_Type)
13046 Has_Private_Declaration (Etype (Desig_Type)))
13048 if No (Discriminant_Specifications (Decl)) then
13050 ("cannot constrain access type if designated "
13051 & "type has constrained partial view", S);
13063 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13064 For_Access => True);
13066 elsif Is_Concurrent_Type (Desig_Type)
13067 and then not Is_Constrained (Desig_Type)
13069 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13072 Error_Msg_N ("invalid constraint on access type", S);
13074 -- We simply ignore an invalid constraint
13076 Desig_Subtype := Desig_Type;
13077 Constraint_OK := False;
13080 if No (Def_Id) then
13081 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13083 Set_Ekind (Def_Id, E_Access_Subtype);
13086 if Constraint_OK then
13087 Set_Etype (Def_Id, Base_Type (T));
13089 if Is_Private_Type (Desig_Type) then
13090 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13093 Set_Etype (Def_Id, Any_Type);
13096 Set_Size_Info (Def_Id, T);
13097 Set_Is_Constrained (Def_Id, Constraint_OK);
13098 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13099 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13100 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13102 Conditional_Delay (Def_Id, T);
13104 -- AI-363 : Subtypes of general access types whose designated types have
13105 -- default discriminants are disallowed. In instances, the rule has to
13106 -- be checked against the actual, of which T is the subtype. In a
13107 -- generic body, the rule is checked assuming that the actual type has
13108 -- defaulted discriminants.
13110 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13111 if Ekind (Base_Type (T)) = E_General_Access_Type
13112 and then Has_Defaulted_Discriminants (Desig_Type)
13114 if Ada_Version < Ada_2005 then
13116 ("access subtype of general access type would not " &
13117 "be allowed in Ada 2005?y?", S);
13120 ("access subtype of general access type not allowed", S);
13123 Error_Msg_N ("\discriminants have defaults", S);
13125 elsif Is_Access_Type (T)
13126 and then Is_Generic_Type (Desig_Type)
13127 and then Has_Discriminants (Desig_Type)
13128 and then In_Package_Body (Current_Scope)
13130 if Ada_Version < Ada_2005 then
13132 ("access subtype would not be allowed in generic body "
13133 & "in Ada 2005?y?", S);
13136 ("access subtype not allowed in generic body", S);
13140 ("\designated type is a discriminated formal", S);
13143 end Constrain_Access;
13145 ---------------------
13146 -- Constrain_Array --
13147 ---------------------
13149 procedure Constrain_Array
13150 (Def_Id : in out Entity_Id;
13152 Related_Nod : Node_Id;
13153 Related_Id : Entity_Id;
13154 Suffix : Character)
13156 C : constant Node_Id := Constraint (SI);
13157 Number_Of_Constraints : Nat := 0;
13160 Constraint_OK : Boolean := True;
13163 T := Entity (Subtype_Mark (SI));
13165 if Is_Access_Type (T) then
13166 T := Designated_Type (T);
13169 -- If an index constraint follows a subtype mark in a subtype indication
13170 -- then the type or subtype denoted by the subtype mark must not already
13171 -- impose an index constraint. The subtype mark must denote either an
13172 -- unconstrained array type or an access type whose designated type
13173 -- is such an array type... (RM 3.6.1)
13175 if Is_Constrained (T) then
13176 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13177 Constraint_OK := False;
13180 S := First (Constraints (C));
13181 while Present (S) loop
13182 Number_Of_Constraints := Number_Of_Constraints + 1;
13186 -- In either case, the index constraint must provide a discrete
13187 -- range for each index of the array type and the type of each
13188 -- discrete range must be the same as that of the corresponding
13189 -- index. (RM 3.6.1)
13191 if Number_Of_Constraints /= Number_Dimensions (T) then
13192 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13193 Constraint_OK := False;
13196 S := First (Constraints (C));
13197 Index := First_Index (T);
13200 -- Apply constraints to each index type
13202 for J in 1 .. Number_Of_Constraints loop
13203 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13211 if No (Def_Id) then
13213 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13214 Set_Parent (Def_Id, Related_Nod);
13217 Set_Ekind (Def_Id, E_Array_Subtype);
13220 Set_Size_Info (Def_Id, (T));
13221 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13222 Set_Etype (Def_Id, Base_Type (T));
13224 if Constraint_OK then
13225 Set_First_Index (Def_Id, First (Constraints (C)));
13227 Set_First_Index (Def_Id, First_Index (T));
13230 Set_Is_Constrained (Def_Id, True);
13231 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13232 Set_Is_Independent (Def_Id, Is_Independent (T));
13233 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13235 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13236 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13238 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13239 -- We need to initialize the attribute because if Def_Id is previously
13240 -- analyzed through a limited_with clause, it will have the attributes
13241 -- of an incomplete type, one of which is an Elist that overlaps the
13242 -- Packed_Array_Impl_Type field.
13244 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13246 -- Build a freeze node if parent still needs one. Also make sure that
13247 -- the Depends_On_Private status is set because the subtype will need
13248 -- reprocessing at the time the base type does, and also we must set a
13249 -- conditional delay.
13251 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13252 Conditional_Delay (Def_Id, T);
13253 end Constrain_Array;
13255 ------------------------------
13256 -- Constrain_Component_Type --
13257 ------------------------------
13259 function Constrain_Component_Type
13261 Constrained_Typ : Entity_Id;
13262 Related_Node : Node_Id;
13264 Constraints : Elist_Id) return Entity_Id
13266 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13267 Compon_Type : constant Entity_Id := Etype (Comp);
13269 function Build_Constrained_Array_Type
13270 (Old_Type : Entity_Id) return Entity_Id;
13271 -- If Old_Type is an array type, one of whose indexes is constrained
13272 -- by a discriminant, build an Itype whose constraint replaces the
13273 -- discriminant with its value in the constraint.
13275 function Build_Constrained_Discriminated_Type
13276 (Old_Type : Entity_Id) return Entity_Id;
13277 -- Ditto for record components. Handle the case where the constraint
13278 -- is a conversion of the discriminant value, introduced during
13281 function Build_Constrained_Access_Type
13282 (Old_Type : Entity_Id) return Entity_Id;
13283 -- Ditto for access types. Makes use of previous two functions, to
13284 -- constrain designated type.
13286 function Is_Discriminant (Expr : Node_Id) return Boolean;
13287 -- Returns True if Expr is a discriminant
13289 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id;
13290 -- Find the value of a discriminant named by Discr_Expr in Constraints
13292 -----------------------------------
13293 -- Build_Constrained_Access_Type --
13294 -----------------------------------
13296 function Build_Constrained_Access_Type
13297 (Old_Type : Entity_Id) return Entity_Id
13299 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13301 Desig_Subtype : Entity_Id;
13305 -- If the original access type was not embedded in the enclosing
13306 -- type definition, there is no need to produce a new access
13307 -- subtype. In fact every access type with an explicit constraint
13308 -- generates an itype whose scope is the enclosing record.
13310 if not Is_Type (Scope (Old_Type)) then
13313 elsif Is_Array_Type (Desig_Type) then
13314 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13316 elsif Has_Discriminants (Desig_Type) then
13318 -- This may be an access type to an enclosing record type for
13319 -- which we are constructing the constrained components. Return
13320 -- the enclosing record subtype. This is not always correct,
13321 -- but avoids infinite recursion. ???
13323 Desig_Subtype := Any_Type;
13325 for J in reverse 0 .. Scope_Stack.Last loop
13326 Scop := Scope_Stack.Table (J).Entity;
13329 and then Base_Type (Scop) = Base_Type (Desig_Type)
13331 Desig_Subtype := Scop;
13334 exit when not Is_Type (Scop);
13337 if Desig_Subtype = Any_Type then
13339 Build_Constrained_Discriminated_Type (Desig_Type);
13346 if Desig_Subtype /= Desig_Type then
13348 -- The Related_Node better be here or else we won't be able
13349 -- to attach new itypes to a node in the tree.
13351 pragma Assert (Present (Related_Node));
13353 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13355 Set_Etype (Itype, Base_Type (Old_Type));
13356 Set_Size_Info (Itype, (Old_Type));
13357 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13358 Set_Depends_On_Private (Itype, Has_Private_Component
13360 Set_Is_Access_Constant (Itype, Is_Access_Constant
13363 -- The new itype needs freezing when it depends on a not frozen
13364 -- type and the enclosing subtype needs freezing.
13366 if Has_Delayed_Freeze (Constrained_Typ)
13367 and then not Is_Frozen (Constrained_Typ)
13369 Conditional_Delay (Itype, Base_Type (Old_Type));
13377 end Build_Constrained_Access_Type;
13379 ----------------------------------
13380 -- Build_Constrained_Array_Type --
13381 ----------------------------------
13383 function Build_Constrained_Array_Type
13384 (Old_Type : Entity_Id) return Entity_Id
13388 Old_Index : Node_Id;
13389 Range_Node : Node_Id;
13390 Constr_List : List_Id;
13392 Need_To_Create_Itype : Boolean := False;
13395 Old_Index := First_Index (Old_Type);
13396 while Present (Old_Index) loop
13397 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13399 if Is_Discriminant (Lo_Expr)
13401 Is_Discriminant (Hi_Expr)
13403 Need_To_Create_Itype := True;
13407 Next_Index (Old_Index);
13410 if Need_To_Create_Itype then
13411 Constr_List := New_List;
13413 Old_Index := First_Index (Old_Type);
13414 while Present (Old_Index) loop
13415 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13417 if Is_Discriminant (Lo_Expr) then
13418 Lo_Expr := Get_Discr_Value (Lo_Expr);
13421 if Is_Discriminant (Hi_Expr) then
13422 Hi_Expr := Get_Discr_Value (Hi_Expr);
13427 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13429 Append (Range_Node, To => Constr_List);
13431 Next_Index (Old_Index);
13434 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
13439 end Build_Constrained_Array_Type;
13441 ------------------------------------------
13442 -- Build_Constrained_Discriminated_Type --
13443 ------------------------------------------
13445 function Build_Constrained_Discriminated_Type
13446 (Old_Type : Entity_Id) return Entity_Id
13449 Constr_List : List_Id;
13450 Old_Constraint : Elmt_Id;
13452 Need_To_Create_Itype : Boolean := False;
13455 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13456 while Present (Old_Constraint) loop
13457 Expr := Node (Old_Constraint);
13459 if Is_Discriminant (Expr) then
13460 Need_To_Create_Itype := True;
13463 -- After expansion of discriminated task types, the value
13464 -- of the discriminant may be converted to a run-time type
13465 -- for restricted run-times. Propagate the value of the
13466 -- discriminant as well, so that e.g. the secondary stack
13467 -- component has a static constraint. Necessary for LLVM.
13469 elsif Nkind (Expr) = N_Type_Conversion
13470 and then Is_Discriminant (Expression (Expr))
13472 Need_To_Create_Itype := True;
13476 Next_Elmt (Old_Constraint);
13479 if Need_To_Create_Itype then
13480 Constr_List := New_List;
13482 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13483 while Present (Old_Constraint) loop
13484 Expr := Node (Old_Constraint);
13486 if Is_Discriminant (Expr) then
13487 Expr := Get_Discr_Value (Expr);
13489 elsif Nkind (Expr) = N_Type_Conversion
13490 and then Is_Discriminant (Expression (Expr))
13492 Expr := New_Copy_Tree (Expr);
13493 Set_Expression (Expr, Get_Discr_Value (Expression (Expr)));
13496 Append (New_Copy_Tree (Expr), To => Constr_List);
13498 Next_Elmt (Old_Constraint);
13501 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
13506 end Build_Constrained_Discriminated_Type;
13508 ---------------------
13509 -- Get_Discr_Value --
13510 ---------------------
13512 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id is
13513 Discr_Id : constant Entity_Id := Entity (Discr_Expr);
13514 -- Entity of a discriminant that appear as a standalone expression in
13515 -- the constraint of a component.
13521 -- The discriminant may be declared for the type, in which case we
13522 -- find it by iterating over the list of discriminants. If the
13523 -- discriminant is inherited from a parent type, it appears as the
13524 -- corresponding discriminant of the current type. This will be the
13525 -- case when constraining an inherited component whose constraint is
13526 -- given by a discriminant of the parent.
13528 D := First_Discriminant (Typ);
13529 E := First_Elmt (Constraints);
13531 while Present (D) loop
13533 or else D = CR_Discriminant (Discr_Id)
13534 or else Corresponding_Discriminant (D) = Discr_Id
13539 Next_Discriminant (D);
13543 -- The Corresponding_Discriminant mechanism is incomplete, because
13544 -- the correspondence between new and old discriminants is not one
13545 -- to one: one new discriminant can constrain several old ones. In
13546 -- that case, scan sequentially the stored_constraint, the list of
13547 -- discriminants of the parents, and the constraints.
13549 -- Previous code checked for the present of the Stored_Constraint
13550 -- list for the derived type, but did not use it at all. Should it
13551 -- be present when the component is a discriminated task type?
13553 if Is_Derived_Type (Typ)
13554 and then Scope (Discr_Id) = Etype (Typ)
13556 D := First_Discriminant (Etype (Typ));
13557 E := First_Elmt (Constraints);
13558 while Present (D) loop
13559 if D = Discr_Id then
13563 Next_Discriminant (D);
13568 -- Something is wrong if we did not find the value
13570 raise Program_Error;
13571 end Get_Discr_Value;
13573 ---------------------
13574 -- Is_Discriminant --
13575 ---------------------
13577 function Is_Discriminant (Expr : Node_Id) return Boolean is
13578 Discrim_Scope : Entity_Id;
13581 if Denotes_Discriminant (Expr) then
13582 Discrim_Scope := Scope (Entity (Expr));
13584 -- Either we have a reference to one of Typ's discriminants,
13586 pragma Assert (Discrim_Scope = Typ
13588 -- or to the discriminants of the parent type, in the case
13589 -- of a derivation of a tagged type with variants.
13591 or else Discrim_Scope = Etype (Typ)
13592 or else Full_View (Discrim_Scope) = Etype (Typ)
13594 -- or same as above for the case where the discriminants
13595 -- were declared in Typ's private view.
13597 or else (Is_Private_Type (Discrim_Scope)
13598 and then Chars (Discrim_Scope) = Chars (Typ))
13600 -- or else we are deriving from the full view and the
13601 -- discriminant is declared in the private entity.
13603 or else (Is_Private_Type (Typ)
13604 and then Chars (Discrim_Scope) = Chars (Typ))
13606 -- Or we are constrained the corresponding record of a
13607 -- synchronized type that completes a private declaration.
13609 or else (Is_Concurrent_Record_Type (Typ)
13611 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13613 -- or we have a class-wide type, in which case make sure the
13614 -- discriminant found belongs to the root type.
13616 or else (Is_Class_Wide_Type (Typ)
13617 and then Etype (Typ) = Discrim_Scope));
13622 -- In all other cases we have something wrong
13625 end Is_Discriminant;
13627 -- Start of processing for Constrain_Component_Type
13630 if Nkind (Parent (Comp)) = N_Component_Declaration
13631 and then Comes_From_Source (Parent (Comp))
13632 and then Comes_From_Source
13633 (Subtype_Indication (Component_Definition (Parent (Comp))))
13636 (Subtype_Indication (Component_Definition (Parent (Comp))))
13638 return Compon_Type;
13640 elsif Is_Array_Type (Compon_Type) then
13641 return Build_Constrained_Array_Type (Compon_Type);
13643 elsif Has_Discriminants (Compon_Type) then
13644 return Build_Constrained_Discriminated_Type (Compon_Type);
13646 elsif Is_Access_Type (Compon_Type) then
13647 return Build_Constrained_Access_Type (Compon_Type);
13650 return Compon_Type;
13652 end Constrain_Component_Type;
13654 --------------------------
13655 -- Constrain_Concurrent --
13656 --------------------------
13658 -- For concurrent types, the associated record value type carries the same
13659 -- discriminants, so when we constrain a concurrent type, we must constrain
13660 -- the corresponding record type as well.
13662 procedure Constrain_Concurrent
13663 (Def_Id : in out Entity_Id;
13665 Related_Nod : Node_Id;
13666 Related_Id : Entity_Id;
13667 Suffix : Character)
13669 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13670 -- case of a private subtype (needed when only doing semantic analysis).
13672 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13676 if Is_Access_Type (T_Ent) then
13677 T_Ent := Designated_Type (T_Ent);
13680 T_Val := Corresponding_Record_Type (T_Ent);
13682 if Present (T_Val) then
13684 if No (Def_Id) then
13685 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13687 -- Elaborate itype now, as it may be used in a subsequent
13688 -- synchronized operation in another scope.
13690 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13691 Build_Itype_Reference (Def_Id, Related_Nod);
13695 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13696 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
13698 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13699 Set_Corresponding_Record_Type (Def_Id,
13700 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13703 -- If there is no associated record, expansion is disabled and this
13704 -- is a generic context. Create a subtype in any case, so that
13705 -- semantic analysis can proceed.
13707 if No (Def_Id) then
13708 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13711 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13713 end Constrain_Concurrent;
13715 ------------------------------------
13716 -- Constrain_Corresponding_Record --
13717 ------------------------------------
13719 function Constrain_Corresponding_Record
13720 (Prot_Subt : Entity_Id;
13721 Corr_Rec : Entity_Id;
13722 Related_Nod : Node_Id) return Entity_Id
13724 T_Sub : constant Entity_Id :=
13726 (Ekind => E_Record_Subtype,
13727 Related_Nod => Related_Nod,
13728 Related_Id => Corr_Rec,
13730 Suffix_Index => -1);
13733 Set_Etype (T_Sub, Corr_Rec);
13734 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13735 Set_Is_Tagged_Type (T_Sub, Is_Tagged_Type (Corr_Rec));
13736 Set_Is_Constrained (T_Sub, True);
13737 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13738 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13740 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13741 Set_Discriminant_Constraint
13742 (T_Sub, Discriminant_Constraint (Prot_Subt));
13743 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13744 Create_Constrained_Components
13745 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13748 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13750 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13751 Conditional_Delay (T_Sub, Corr_Rec);
13754 -- This is a component subtype: it will be frozen in the context of
13755 -- the enclosing record's init_proc, so that discriminant references
13756 -- are resolved to discriminals. (Note: we used to skip freezing
13757 -- altogether in that case, which caused errors downstream for
13758 -- components of a bit packed array type).
13760 Set_Has_Delayed_Freeze (T_Sub);
13764 end Constrain_Corresponding_Record;
13766 -----------------------
13767 -- Constrain_Decimal --
13768 -----------------------
13770 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13771 T : constant Entity_Id := Entity (Subtype_Mark (S));
13772 C : constant Node_Id := Constraint (S);
13773 Loc : constant Source_Ptr := Sloc (C);
13774 Range_Expr : Node_Id;
13775 Digits_Expr : Node_Id;
13780 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13782 if Nkind (C) = N_Range_Constraint then
13783 Range_Expr := Range_Expression (C);
13784 Digits_Val := Digits_Value (T);
13787 pragma Assert (Nkind (C) = N_Digits_Constraint);
13789 Digits_Expr := Digits_Expression (C);
13790 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13792 Check_Digits_Expression (Digits_Expr);
13793 Digits_Val := Expr_Value (Digits_Expr);
13795 if Digits_Val > Digits_Value (T) then
13797 ("digits expression is incompatible with subtype", C);
13798 Digits_Val := Digits_Value (T);
13801 if Present (Range_Constraint (C)) then
13802 Range_Expr := Range_Expression (Range_Constraint (C));
13804 Range_Expr := Empty;
13808 Set_Etype (Def_Id, Base_Type (T));
13809 Set_Size_Info (Def_Id, (T));
13810 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13811 Set_Delta_Value (Def_Id, Delta_Value (T));
13812 Set_Scale_Value (Def_Id, Scale_Value (T));
13813 Set_Small_Value (Def_Id, Small_Value (T));
13814 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13815 Set_Digits_Value (Def_Id, Digits_Val);
13817 -- Manufacture range from given digits value if no range present
13819 if No (Range_Expr) then
13820 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13824 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13826 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13829 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13830 Set_Discrete_RM_Size (Def_Id);
13832 -- Unconditionally delay the freeze, since we cannot set size
13833 -- information in all cases correctly until the freeze point.
13835 Set_Has_Delayed_Freeze (Def_Id);
13836 end Constrain_Decimal;
13838 ----------------------------------
13839 -- Constrain_Discriminated_Type --
13840 ----------------------------------
13842 procedure Constrain_Discriminated_Type
13843 (Def_Id : Entity_Id;
13845 Related_Nod : Node_Id;
13846 For_Access : Boolean := False)
13848 E : Entity_Id := Entity (Subtype_Mark (S));
13851 procedure Fixup_Bad_Constraint;
13852 -- Called after finding a bad constraint, and after having posted an
13853 -- appropriate error message. The goal is to leave type Def_Id in as
13854 -- reasonable state as possible.
13856 --------------------------
13857 -- Fixup_Bad_Constraint --
13858 --------------------------
13860 procedure Fixup_Bad_Constraint is
13862 -- Set a reasonable Ekind for the entity, including incomplete types.
13864 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13866 -- Set Etype to the known type, to reduce chances of cascaded errors
13868 Set_Etype (Def_Id, E);
13869 Set_Error_Posted (Def_Id);
13870 end Fixup_Bad_Constraint;
13875 Constr : Elist_Id := New_Elmt_List;
13877 -- Start of processing for Constrain_Discriminated_Type
13880 C := Constraint (S);
13882 -- A discriminant constraint is only allowed in a subtype indication,
13883 -- after a subtype mark. This subtype mark must denote either a type
13884 -- with discriminants, or an access type whose designated type is a
13885 -- type with discriminants. A discriminant constraint specifies the
13886 -- values of these discriminants (RM 3.7.2(5)).
13888 T := Base_Type (Entity (Subtype_Mark (S)));
13890 if Is_Access_Type (T) then
13891 T := Designated_Type (T);
13894 -- In an instance it may be necessary to retrieve the full view of a
13895 -- type with unknown discriminants, or a full view with defaulted
13896 -- discriminants. In other contexts the constraint is illegal.
13899 and then Is_Private_Type (T)
13900 and then Present (Full_View (T))
13902 (Has_Unknown_Discriminants (T)
13904 (not Has_Discriminants (T)
13905 and then Has_Discriminants (Full_View (T))
13906 and then Present (Discriminant_Default_Value
13907 (First_Discriminant (Full_View (T))))))
13909 T := Full_View (T);
13910 E := Full_View (E);
13913 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13914 -- generating an error for access-to-incomplete subtypes.
13916 if Ada_Version >= Ada_2005
13917 and then Ekind (T) = E_Incomplete_Type
13918 and then Nkind (Parent (S)) = N_Subtype_Declaration
13919 and then not Is_Itype (Def_Id)
13921 -- A little sanity check: emit an error message if the type has
13922 -- discriminants to begin with. Type T may be a regular incomplete
13923 -- type or imported via a limited with clause.
13925 if Has_Discriminants (T)
13926 or else (From_Limited_With (T)
13927 and then Present (Non_Limited_View (T))
13928 and then Nkind (Parent (Non_Limited_View (T))) =
13929 N_Full_Type_Declaration
13930 and then Present (Discriminant_Specifications
13931 (Parent (Non_Limited_View (T)))))
13934 ("(Ada 2005) incomplete subtype may not be constrained", C);
13936 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13939 Fixup_Bad_Constraint;
13942 -- Check that the type has visible discriminants. The type may be
13943 -- a private type with unknown discriminants whose full view has
13944 -- discriminants which are invisible.
13946 elsif not Has_Discriminants (T)
13948 (Has_Unknown_Discriminants (T)
13949 and then Is_Private_Type (T))
13951 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13952 Fixup_Bad_Constraint;
13955 elsif Is_Constrained (E)
13956 or else (Ekind (E) = E_Class_Wide_Subtype
13957 and then Present (Discriminant_Constraint (E)))
13959 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13960 Fixup_Bad_Constraint;
13964 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13965 -- applies to the base type.
13967 T := Base_Type (T);
13969 Constr := Build_Discriminant_Constraints (T, S);
13971 -- If the list returned was empty we had an error in building the
13972 -- discriminant constraint. We have also already signalled an error
13973 -- in the incomplete type case
13975 if Is_Empty_Elmt_List (Constr) then
13976 Fixup_Bad_Constraint;
13980 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13981 end Constrain_Discriminated_Type;
13983 ---------------------------
13984 -- Constrain_Enumeration --
13985 ---------------------------
13987 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13988 T : constant Entity_Id := Entity (Subtype_Mark (S));
13989 C : constant Node_Id := Constraint (S);
13992 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13994 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13996 Set_Etype (Def_Id, Base_Type (T));
13997 Set_Size_Info (Def_Id, (T));
13998 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13999 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14001 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14003 Set_Discrete_RM_Size (Def_Id);
14004 end Constrain_Enumeration;
14006 ----------------------
14007 -- Constrain_Float --
14008 ----------------------
14010 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
14011 T : constant Entity_Id := Entity (Subtype_Mark (S));
14017 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
14019 Set_Etype (Def_Id, Base_Type (T));
14020 Set_Size_Info (Def_Id, (T));
14021 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14023 -- Process the constraint
14025 C := Constraint (S);
14027 -- Digits constraint present
14029 if Nkind (C) = N_Digits_Constraint then
14030 Check_Restriction (No_Obsolescent_Features, C);
14032 if Warn_On_Obsolescent_Feature then
14034 ("subtype digits constraint is an " &
14035 "obsolescent feature (RM J.3(8))?j?", C);
14038 D := Digits_Expression (C);
14039 Analyze_And_Resolve (D, Any_Integer);
14040 Check_Digits_Expression (D);
14041 Set_Digits_Value (Def_Id, Expr_Value (D));
14043 -- Check that digits value is in range. Obviously we can do this
14044 -- at compile time, but it is strictly a runtime check, and of
14045 -- course there is an ACVC test that checks this.
14047 if Digits_Value (Def_Id) > Digits_Value (T) then
14048 Error_Msg_Uint_1 := Digits_Value (T);
14049 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14051 Make_Raise_Constraint_Error (Sloc (D),
14052 Reason => CE_Range_Check_Failed);
14053 Insert_Action (Declaration_Node (Def_Id), Rais);
14056 C := Range_Constraint (C);
14058 -- No digits constraint present
14061 Set_Digits_Value (Def_Id, Digits_Value (T));
14064 -- Range constraint present
14066 if Nkind (C) = N_Range_Constraint then
14067 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14069 -- No range constraint present
14072 pragma Assert (No (C));
14073 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14076 Set_Is_Constrained (Def_Id);
14077 end Constrain_Float;
14079 ---------------------
14080 -- Constrain_Index --
14081 ---------------------
14083 procedure Constrain_Index
14086 Related_Nod : Node_Id;
14087 Related_Id : Entity_Id;
14088 Suffix : Character;
14089 Suffix_Index : Nat)
14091 Def_Id : Entity_Id;
14092 R : Node_Id := Empty;
14093 T : constant Entity_Id := Etype (Index);
14097 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14098 Set_Etype (Def_Id, Base_Type (T));
14100 if Nkind (S) = N_Range
14102 (Nkind (S) = N_Attribute_Reference
14103 and then Attribute_Name (S) = Name_Range)
14105 -- A Range attribute will be transformed into N_Range by Resolve
14111 Process_Range_Expr_In_Decl (R, T);
14113 if not Error_Posted (S)
14115 (Nkind (S) /= N_Range
14116 or else not Covers (T, (Etype (Low_Bound (S))))
14117 or else not Covers (T, (Etype (High_Bound (S)))))
14119 if Base_Type (T) /= Any_Type
14120 and then Etype (Low_Bound (S)) /= Any_Type
14121 and then Etype (High_Bound (S)) /= Any_Type
14123 Error_Msg_N ("range expected", S);
14127 elsif Nkind (S) = N_Subtype_Indication then
14129 -- The parser has verified that this is a discrete indication
14131 Resolve_Discrete_Subtype_Indication (S, T);
14132 Bad_Predicated_Subtype_Use
14133 ("subtype& has predicate, not allowed in index constraint",
14134 S, Entity (Subtype_Mark (S)));
14136 R := Range_Expression (Constraint (S));
14138 -- Capture values of bounds and generate temporaries for them if
14139 -- needed, since checks may cause duplication of the expressions
14140 -- which must not be reevaluated.
14142 -- The forced evaluation removes side effects from expressions, which
14143 -- should occur also in GNATprove mode. Otherwise, we end up with
14144 -- unexpected insertions of actions at places where this is not
14145 -- supposed to occur, e.g. on default parameters of a call.
14147 if Expander_Active or GNATprove_Mode then
14149 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14151 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14154 elsif Nkind (S) = N_Discriminant_Association then
14156 -- Syntactically valid in subtype indication
14158 Error_Msg_N ("invalid index constraint", S);
14159 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14162 -- Subtype_Mark case, no anonymous subtypes to construct
14167 if Is_Entity_Name (S) then
14168 if not Is_Type (Entity (S)) then
14169 Error_Msg_N ("expect subtype mark for index constraint", S);
14171 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14172 Wrong_Type (S, Base_Type (T));
14174 -- Check error of subtype with predicate in index constraint
14177 Bad_Predicated_Subtype_Use
14178 ("subtype& has predicate, not allowed in index constraint",
14185 Error_Msg_N ("invalid index constraint", S);
14186 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14191 -- Complete construction of the Itype
14193 if Is_Modular_Integer_Type (T) then
14194 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14196 elsif Is_Integer_Type (T) then
14197 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14200 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14201 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14202 Set_First_Literal (Def_Id, First_Literal (T));
14205 Set_Size_Info (Def_Id, (T));
14206 Set_RM_Size (Def_Id, RM_Size (T));
14207 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14209 Set_Scalar_Range (Def_Id, R);
14211 Set_Etype (S, Def_Id);
14212 Set_Discrete_RM_Size (Def_Id);
14213 end Constrain_Index;
14215 -----------------------
14216 -- Constrain_Integer --
14217 -----------------------
14219 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
14220 T : constant Entity_Id := Entity (Subtype_Mark (S));
14221 C : constant Node_Id := Constraint (S);
14224 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14226 if Is_Modular_Integer_Type (T) then
14227 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14229 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14232 Set_Etype (Def_Id, Base_Type (T));
14233 Set_Size_Info (Def_Id, (T));
14234 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14235 Set_Discrete_RM_Size (Def_Id);
14236 end Constrain_Integer;
14238 ------------------------------
14239 -- Constrain_Ordinary_Fixed --
14240 ------------------------------
14242 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
14243 T : constant Entity_Id := Entity (Subtype_Mark (S));
14249 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14250 Set_Etype (Def_Id, Base_Type (T));
14251 Set_Size_Info (Def_Id, (T));
14252 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14253 Set_Small_Value (Def_Id, Small_Value (T));
14255 -- Process the constraint
14257 C := Constraint (S);
14259 -- Delta constraint present
14261 if Nkind (C) = N_Delta_Constraint then
14262 Check_Restriction (No_Obsolescent_Features, C);
14264 if Warn_On_Obsolescent_Feature then
14266 ("subtype delta constraint is an " &
14267 "obsolescent feature (RM J.3(7))?j?");
14270 D := Delta_Expression (C);
14271 Analyze_And_Resolve (D, Any_Real);
14272 Check_Delta_Expression (D);
14273 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14275 -- Check that delta value is in range. Obviously we can do this
14276 -- at compile time, but it is strictly a runtime check, and of
14277 -- course there is an ACVC test that checks this.
14279 if Delta_Value (Def_Id) < Delta_Value (T) then
14280 Error_Msg_N ("??delta value is too small", D);
14282 Make_Raise_Constraint_Error (Sloc (D),
14283 Reason => CE_Range_Check_Failed);
14284 Insert_Action (Declaration_Node (Def_Id), Rais);
14287 C := Range_Constraint (C);
14289 -- No delta constraint present
14292 Set_Delta_Value (Def_Id, Delta_Value (T));
14295 -- Range constraint present
14297 if Nkind (C) = N_Range_Constraint then
14298 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14300 -- No range constraint present
14303 pragma Assert (No (C));
14304 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14307 Set_Discrete_RM_Size (Def_Id);
14309 -- Unconditionally delay the freeze, since we cannot set size
14310 -- information in all cases correctly until the freeze point.
14312 Set_Has_Delayed_Freeze (Def_Id);
14313 end Constrain_Ordinary_Fixed;
14315 -----------------------
14316 -- Contain_Interface --
14317 -----------------------
14319 function Contain_Interface
14320 (Iface : Entity_Id;
14321 Ifaces : Elist_Id) return Boolean
14323 Iface_Elmt : Elmt_Id;
14326 if Present (Ifaces) then
14327 Iface_Elmt := First_Elmt (Ifaces);
14328 while Present (Iface_Elmt) loop
14329 if Node (Iface_Elmt) = Iface then
14333 Next_Elmt (Iface_Elmt);
14338 end Contain_Interface;
14340 ---------------------------
14341 -- Convert_Scalar_Bounds --
14342 ---------------------------
14344 procedure Convert_Scalar_Bounds
14346 Parent_Type : Entity_Id;
14347 Derived_Type : Entity_Id;
14350 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14357 -- Defend against previous errors
14359 if No (Scalar_Range (Derived_Type)) then
14360 Check_Error_Detected;
14364 Lo := Build_Scalar_Bound
14365 (Type_Low_Bound (Derived_Type),
14366 Parent_Type, Implicit_Base);
14368 Hi := Build_Scalar_Bound
14369 (Type_High_Bound (Derived_Type),
14370 Parent_Type, Implicit_Base);
14377 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14379 Set_Parent (Rng, N);
14380 Set_Scalar_Range (Derived_Type, Rng);
14382 -- Analyze the bounds
14384 Analyze_And_Resolve (Lo, Implicit_Base);
14385 Analyze_And_Resolve (Hi, Implicit_Base);
14387 -- Analyze the range itself, except that we do not analyze it if
14388 -- the bounds are real literals, and we have a fixed-point type.
14389 -- The reason for this is that we delay setting the bounds in this
14390 -- case till we know the final Small and Size values (see circuit
14391 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14393 if Is_Fixed_Point_Type (Parent_Type)
14394 and then Nkind (Lo) = N_Real_Literal
14395 and then Nkind (Hi) = N_Real_Literal
14399 -- Here we do the analysis of the range
14401 -- Note: we do this manually, since if we do a normal Analyze and
14402 -- Resolve call, there are problems with the conversions used for
14403 -- the derived type range.
14406 Set_Etype (Rng, Implicit_Base);
14407 Set_Analyzed (Rng, True);
14409 end Convert_Scalar_Bounds;
14411 -------------------
14412 -- Copy_And_Swap --
14413 -------------------
14415 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14417 -- Initialize new full declaration entity by copying the pertinent
14418 -- fields of the corresponding private declaration entity.
14420 -- We temporarily set Ekind to a value appropriate for a type to
14421 -- avoid assert failures in Einfo from checking for setting type
14422 -- attributes on something that is not a type. Ekind (Priv) is an
14423 -- appropriate choice, since it allowed the attributes to be set
14424 -- in the first place. This Ekind value will be modified later.
14426 Set_Ekind (Full, Ekind (Priv));
14428 -- Also set Etype temporarily to Any_Type, again, in the absence
14429 -- of errors, it will be properly reset, and if there are errors,
14430 -- then we want a value of Any_Type to remain.
14432 Set_Etype (Full, Any_Type);
14434 -- Now start copying attributes
14436 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14438 if Has_Discriminants (Full) then
14439 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14440 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14443 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14444 Set_Homonym (Full, Homonym (Priv));
14445 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14446 Set_Is_Public (Full, Is_Public (Priv));
14447 Set_Is_Pure (Full, Is_Pure (Priv));
14448 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14449 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14450 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14451 Set_Has_Pragma_Unreferenced_Objects
14452 (Full, Has_Pragma_Unreferenced_Objects
14455 Conditional_Delay (Full, Priv);
14457 if Is_Tagged_Type (Full) then
14458 Set_Direct_Primitive_Operations
14459 (Full, Direct_Primitive_Operations (Priv));
14460 Set_No_Tagged_Streams_Pragma
14461 (Full, No_Tagged_Streams_Pragma (Priv));
14463 if Is_Base_Type (Priv) then
14464 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14468 Set_Is_Volatile (Full, Is_Volatile (Priv));
14469 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14470 Set_Scope (Full, Scope (Priv));
14471 Set_Prev_Entity (Full, Prev_Entity (Priv));
14472 Set_Next_Entity (Full, Next_Entity (Priv));
14473 Set_First_Entity (Full, First_Entity (Priv));
14474 Set_Last_Entity (Full, Last_Entity (Priv));
14476 -- If access types have been recorded for later handling, keep them in
14477 -- the full view so that they get handled when the full view freeze
14478 -- node is expanded.
14480 if Present (Freeze_Node (Priv))
14481 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14483 Ensure_Freeze_Node (Full);
14484 Set_Access_Types_To_Process
14485 (Freeze_Node (Full),
14486 Access_Types_To_Process (Freeze_Node (Priv)));
14489 -- Swap the two entities. Now Private is the full type entity and Full
14490 -- is the private one. They will be swapped back at the end of the
14491 -- private part. This swapping ensures that the entity that is visible
14492 -- in the private part is the full declaration.
14494 Exchange_Entities (Priv, Full);
14495 Append_Entity (Full, Scope (Full));
14498 -------------------------------------
14499 -- Copy_Array_Base_Type_Attributes --
14500 -------------------------------------
14502 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14504 Set_Component_Alignment (T1, Component_Alignment (T2));
14505 Set_Component_Type (T1, Component_Type (T2));
14506 Set_Component_Size (T1, Component_Size (T2));
14507 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14508 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14509 Propagate_Concurrent_Flags (T1, T2);
14510 Set_Is_Packed (T1, Is_Packed (T2));
14511 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14512 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14513 Set_Has_Independent_Components (T1, Has_Independent_Components (T2));
14514 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14515 end Copy_Array_Base_Type_Attributes;
14517 -----------------------------------
14518 -- Copy_Array_Subtype_Attributes --
14519 -----------------------------------
14521 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14523 Set_Size_Info (T1, T2);
14525 Set_First_Index (T1, First_Index (T2));
14526 Set_Is_Aliased (T1, Is_Aliased (T2));
14527 Set_Is_Atomic (T1, Is_Atomic (T2));
14528 Set_Is_Independent (T1, Is_Independent (T2));
14529 Set_Is_Volatile (T1, Is_Volatile (T2));
14530 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
14531 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14532 Set_Is_Constrained (T1, Is_Constrained (T2));
14533 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14534 Inherit_Rep_Item_Chain (T1, T2);
14535 Set_Convention (T1, Convention (T2));
14536 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14537 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14538 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14539 end Copy_Array_Subtype_Attributes;
14541 -----------------------------------
14542 -- Create_Constrained_Components --
14543 -----------------------------------
14545 procedure Create_Constrained_Components
14547 Decl_Node : Node_Id;
14549 Constraints : Elist_Id)
14551 Loc : constant Source_Ptr := Sloc (Subt);
14552 Comp_List : constant Elist_Id := New_Elmt_List;
14553 Parent_Type : constant Entity_Id := Etype (Typ);
14554 Assoc_List : constant List_Id := New_List;
14555 Discr_Val : Elmt_Id;
14559 Is_Static : Boolean := True;
14561 procedure Collect_Fixed_Components (Typ : Entity_Id);
14562 -- Collect parent type components that do not appear in a variant part
14564 procedure Create_All_Components;
14565 -- Iterate over Comp_List to create the components of the subtype
14567 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14568 -- Creates a new component from Old_Compon, copying all the fields from
14569 -- it, including its Etype, inserts the new component in the Subt entity
14570 -- chain and returns the new component.
14572 function Is_Variant_Record (T : Entity_Id) return Boolean;
14573 -- If true, and discriminants are static, collect only components from
14574 -- variants selected by discriminant values.
14576 ------------------------------
14577 -- Collect_Fixed_Components --
14578 ------------------------------
14580 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14582 -- Build association list for discriminants, and find components of the
14583 -- variant part selected by the values of the discriminants.
14585 Old_C := First_Discriminant (Typ);
14586 Discr_Val := First_Elmt (Constraints);
14587 while Present (Old_C) loop
14588 Append_To (Assoc_List,
14589 Make_Component_Association (Loc,
14590 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14591 Expression => New_Copy (Node (Discr_Val))));
14593 Next_Elmt (Discr_Val);
14594 Next_Discriminant (Old_C);
14597 -- The tag and the possible parent component are unconditionally in
14600 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14601 Old_C := First_Component (Typ);
14602 while Present (Old_C) loop
14603 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14604 Append_Elmt (Old_C, Comp_List);
14607 Next_Component (Old_C);
14610 end Collect_Fixed_Components;
14612 ---------------------------
14613 -- Create_All_Components --
14614 ---------------------------
14616 procedure Create_All_Components is
14620 Comp := First_Elmt (Comp_List);
14621 while Present (Comp) loop
14622 Old_C := Node (Comp);
14623 New_C := Create_Component (Old_C);
14627 Constrain_Component_Type
14628 (Old_C, Subt, Decl_Node, Typ, Constraints));
14629 Set_Is_Public (New_C, Is_Public (Subt));
14633 end Create_All_Components;
14635 ----------------------
14636 -- Create_Component --
14637 ----------------------
14639 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14640 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14643 if Ekind (Old_Compon) = E_Discriminant
14644 and then Is_Completely_Hidden (Old_Compon)
14646 -- This is a shadow discriminant created for a discriminant of
14647 -- the parent type, which needs to be present in the subtype.
14648 -- Give the shadow discriminant an internal name that cannot
14649 -- conflict with that of visible components.
14651 Set_Chars (New_Compon, New_Internal_Name ('C'));
14654 -- Set the parent so we have a proper link for freezing etc. This is
14655 -- not a real parent pointer, since of course our parent does not own
14656 -- up to us and reference us, we are an illegitimate child of the
14657 -- original parent.
14659 Set_Parent (New_Compon, Parent (Old_Compon));
14661 -- We do not want this node marked as Comes_From_Source, since
14662 -- otherwise it would get first class status and a separate cross-
14663 -- reference line would be generated. Illegitimate children do not
14664 -- rate such recognition.
14666 Set_Comes_From_Source (New_Compon, False);
14668 -- But it is a real entity, and a birth certificate must be properly
14669 -- registered by entering it into the entity list, and setting its
14670 -- scope to the given subtype. This turns out to be useful for the
14671 -- LLVM code generator, but that scope is not used otherwise.
14673 Enter_Name (New_Compon);
14674 Set_Scope (New_Compon, Subt);
14677 end Create_Component;
14679 -----------------------
14680 -- Is_Variant_Record --
14681 -----------------------
14683 function Is_Variant_Record (T : Entity_Id) return Boolean is
14685 return Nkind (Parent (T)) = N_Full_Type_Declaration
14686 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14687 and then Present (Component_List (Type_Definition (Parent (T))))
14690 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14691 end Is_Variant_Record;
14693 -- Start of processing for Create_Constrained_Components
14696 pragma Assert (Subt /= Base_Type (Subt));
14697 pragma Assert (Typ = Base_Type (Typ));
14699 Set_First_Entity (Subt, Empty);
14700 Set_Last_Entity (Subt, Empty);
14702 -- Check whether constraint is fully static, in which case we can
14703 -- optimize the list of components.
14705 Discr_Val := First_Elmt (Constraints);
14706 while Present (Discr_Val) loop
14707 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14708 Is_Static := False;
14712 Next_Elmt (Discr_Val);
14715 Set_Has_Static_Discriminants (Subt, Is_Static);
14719 -- Inherit the discriminants of the parent type
14721 Add_Discriminants : declare
14727 Old_C := First_Discriminant (Typ);
14729 while Present (Old_C) loop
14730 Num_Disc := Num_Disc + 1;
14731 New_C := Create_Component (Old_C);
14732 Set_Is_Public (New_C, Is_Public (Subt));
14733 Next_Discriminant (Old_C);
14736 -- For an untagged derived subtype, the number of discriminants may
14737 -- be smaller than the number of inherited discriminants, because
14738 -- several of them may be renamed by a single new discriminant or
14739 -- constrained. In this case, add the hidden discriminants back into
14740 -- the subtype, because they need to be present if the optimizer of
14741 -- the GCC 4.x back-end decides to break apart assignments between
14742 -- objects using the parent view into member-wise assignments.
14746 if Is_Derived_Type (Typ)
14747 and then not Is_Tagged_Type (Typ)
14749 Old_C := First_Stored_Discriminant (Typ);
14751 while Present (Old_C) loop
14752 Num_Gird := Num_Gird + 1;
14753 Next_Stored_Discriminant (Old_C);
14757 if Num_Gird > Num_Disc then
14759 -- Find out multiple uses of new discriminants, and add hidden
14760 -- components for the extra renamed discriminants. We recognize
14761 -- multiple uses through the Corresponding_Discriminant of a
14762 -- new discriminant: if it constrains several old discriminants,
14763 -- this field points to the last one in the parent type. The
14764 -- stored discriminants of the derived type have the same name
14765 -- as those of the parent.
14769 New_Discr : Entity_Id;
14770 Old_Discr : Entity_Id;
14773 Constr := First_Elmt (Stored_Constraint (Typ));
14774 Old_Discr := First_Stored_Discriminant (Typ);
14775 while Present (Constr) loop
14776 if Is_Entity_Name (Node (Constr))
14777 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14779 New_Discr := Entity (Node (Constr));
14781 if Chars (Corresponding_Discriminant (New_Discr)) /=
14784 -- The new discriminant has been used to rename a
14785 -- subsequent old discriminant. Introduce a shadow
14786 -- component for the current old discriminant.
14788 New_C := Create_Component (Old_Discr);
14789 Set_Original_Record_Component (New_C, Old_Discr);
14793 -- The constraint has eliminated the old discriminant.
14794 -- Introduce a shadow component.
14796 New_C := Create_Component (Old_Discr);
14797 Set_Original_Record_Component (New_C, Old_Discr);
14800 Next_Elmt (Constr);
14801 Next_Stored_Discriminant (Old_Discr);
14805 end Add_Discriminants;
14808 and then Is_Variant_Record (Typ)
14810 Collect_Fixed_Components (Typ);
14812 Gather_Components (
14814 Component_List (Type_Definition (Parent (Typ))),
14815 Governed_By => Assoc_List,
14817 Report_Errors => Errors);
14818 pragma Assert (not Errors
14819 or else Serious_Errors_Detected > 0);
14821 Create_All_Components;
14823 -- If the subtype declaration is created for a tagged type derivation
14824 -- with constraints, we retrieve the record definition of the parent
14825 -- type to select the components of the proper variant.
14828 and then Is_Tagged_Type (Typ)
14829 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14831 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14832 and then Is_Variant_Record (Parent_Type)
14834 Collect_Fixed_Components (Typ);
14838 Component_List (Type_Definition (Parent (Parent_Type))),
14839 Governed_By => Assoc_List,
14841 Report_Errors => Errors);
14843 -- Note: previously there was a check at this point that no errors
14844 -- were detected. As a consequence of AI05-220 there may be an error
14845 -- if an inherited discriminant that controls a variant has a non-
14846 -- static constraint.
14848 -- If the tagged derivation has a type extension, collect all the
14849 -- new components therein.
14851 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14853 Old_C := First_Component (Typ);
14854 while Present (Old_C) loop
14855 if Original_Record_Component (Old_C) = Old_C
14856 and then Chars (Old_C) /= Name_uTag
14857 and then Chars (Old_C) /= Name_uParent
14859 Append_Elmt (Old_C, Comp_List);
14862 Next_Component (Old_C);
14866 Create_All_Components;
14869 -- If discriminants are not static, or if this is a multi-level type
14870 -- extension, we have to include all components of the parent type.
14872 Old_C := First_Component (Typ);
14873 while Present (Old_C) loop
14874 New_C := Create_Component (Old_C);
14878 Constrain_Component_Type
14879 (Old_C, Subt, Decl_Node, Typ, Constraints));
14880 Set_Is_Public (New_C, Is_Public (Subt));
14882 Next_Component (Old_C);
14887 end Create_Constrained_Components;
14889 ------------------------------------------
14890 -- Decimal_Fixed_Point_Type_Declaration --
14891 ------------------------------------------
14893 procedure Decimal_Fixed_Point_Type_Declaration
14897 Loc : constant Source_Ptr := Sloc (Def);
14898 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14899 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14900 Implicit_Base : Entity_Id;
14907 Check_Restriction (No_Fixed_Point, Def);
14909 -- Create implicit base type
14912 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14913 Set_Etype (Implicit_Base, Implicit_Base);
14915 -- Analyze and process delta expression
14917 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14919 Check_Delta_Expression (Delta_Expr);
14920 Delta_Val := Expr_Value_R (Delta_Expr);
14922 -- Check delta is power of 10, and determine scale value from it
14928 Scale_Val := Uint_0;
14931 if Val < Ureal_1 then
14932 while Val < Ureal_1 loop
14933 Val := Val * Ureal_10;
14934 Scale_Val := Scale_Val + 1;
14937 if Scale_Val > 18 then
14938 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14939 Scale_Val := UI_From_Int (+18);
14943 while Val > Ureal_1 loop
14944 Val := Val / Ureal_10;
14945 Scale_Val := Scale_Val - 1;
14948 if Scale_Val < -18 then
14949 Error_Msg_N ("scale is less than minimum value of -18", Def);
14950 Scale_Val := UI_From_Int (-18);
14954 if Val /= Ureal_1 then
14955 Error_Msg_N ("delta expression must be a power of 10", Def);
14956 Delta_Val := Ureal_10 ** (-Scale_Val);
14960 -- Set delta, scale and small (small = delta for decimal type)
14962 Set_Delta_Value (Implicit_Base, Delta_Val);
14963 Set_Scale_Value (Implicit_Base, Scale_Val);
14964 Set_Small_Value (Implicit_Base, Delta_Val);
14966 -- Analyze and process digits expression
14968 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14969 Check_Digits_Expression (Digs_Expr);
14970 Digs_Val := Expr_Value (Digs_Expr);
14972 if Digs_Val > 18 then
14973 Digs_Val := UI_From_Int (+18);
14974 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14977 Set_Digits_Value (Implicit_Base, Digs_Val);
14978 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14980 -- Set range of base type from digits value for now. This will be
14981 -- expanded to represent the true underlying base range by Freeze.
14983 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14985 -- Note: We leave size as zero for now, size will be set at freeze
14986 -- time. We have to do this for ordinary fixed-point, because the size
14987 -- depends on the specified small, and we might as well do the same for
14988 -- decimal fixed-point.
14990 pragma Assert (Esize (Implicit_Base) = Uint_0);
14992 -- If there are bounds given in the declaration use them as the
14993 -- bounds of the first named subtype.
14995 if Present (Real_Range_Specification (Def)) then
14997 RRS : constant Node_Id := Real_Range_Specification (Def);
14998 Low : constant Node_Id := Low_Bound (RRS);
14999 High : constant Node_Id := High_Bound (RRS);
15004 Analyze_And_Resolve (Low, Any_Real);
15005 Analyze_And_Resolve (High, Any_Real);
15006 Check_Real_Bound (Low);
15007 Check_Real_Bound (High);
15008 Low_Val := Expr_Value_R (Low);
15009 High_Val := Expr_Value_R (High);
15011 if Low_Val < (-Bound_Val) then
15013 ("range low bound too small for digits value", Low);
15014 Low_Val := -Bound_Val;
15017 if High_Val > Bound_Val then
15019 ("range high bound too large for digits value", High);
15020 High_Val := Bound_Val;
15023 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15026 -- If no explicit range, use range that corresponds to given
15027 -- digits value. This will end up as the final range for the
15031 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15034 -- Complete entity for first subtype. The inheritance of the rep item
15035 -- chain ensures that SPARK-related pragmas are not clobbered when the
15036 -- decimal fixed point type acts as a full view of a private type.
15038 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15039 Set_Etype (T, Implicit_Base);
15040 Set_Size_Info (T, Implicit_Base);
15041 Inherit_Rep_Item_Chain (T, Implicit_Base);
15042 Set_Digits_Value (T, Digs_Val);
15043 Set_Delta_Value (T, Delta_Val);
15044 Set_Small_Value (T, Delta_Val);
15045 Set_Scale_Value (T, Scale_Val);
15046 Set_Is_Constrained (T);
15047 end Decimal_Fixed_Point_Type_Declaration;
15049 -----------------------------------
15050 -- Derive_Progenitor_Subprograms --
15051 -----------------------------------
15053 procedure Derive_Progenitor_Subprograms
15054 (Parent_Type : Entity_Id;
15055 Tagged_Type : Entity_Id)
15060 Iface_Alias : Entity_Id;
15061 Iface_Elmt : Elmt_Id;
15062 Iface_Subp : Entity_Id;
15063 New_Subp : Entity_Id := Empty;
15064 Prim_Elmt : Elmt_Id;
15069 pragma Assert (Ada_Version >= Ada_2005
15070 and then Is_Record_Type (Tagged_Type)
15071 and then Is_Tagged_Type (Tagged_Type)
15072 and then Has_Interfaces (Tagged_Type));
15074 -- Step 1: Transfer to the full-view primitives associated with the
15075 -- partial-view that cover interface primitives. Conceptually this
15076 -- work should be done later by Process_Full_View; done here to
15077 -- simplify its implementation at later stages. It can be safely
15078 -- done here because interfaces must be visible in the partial and
15079 -- private view (RM 7.3(7.3/2)).
15081 -- Small optimization: This work is only required if the parent may
15082 -- have entities whose Alias attribute reference an interface primitive.
15083 -- Such a situation may occur if the parent is an abstract type and the
15084 -- primitive has not been yet overridden or if the parent is a generic
15085 -- formal type covering interfaces.
15087 -- If the tagged type is not abstract, it cannot have abstract
15088 -- primitives (the only entities in the list of primitives of
15089 -- non-abstract tagged types that can reference abstract primitives
15090 -- through its Alias attribute are the internal entities that have
15091 -- attribute Interface_Alias, and these entities are generated later
15092 -- by Add_Internal_Interface_Entities).
15094 if In_Private_Part (Current_Scope)
15095 and then (Is_Abstract_Type (Parent_Type)
15097 Is_Generic_Type (Parent_Type))
15099 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15100 while Present (Elmt) loop
15101 Subp := Node (Elmt);
15103 -- At this stage it is not possible to have entities in the list
15104 -- of primitives that have attribute Interface_Alias.
15106 pragma Assert (No (Interface_Alias (Subp)));
15108 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15110 if Is_Interface (Typ) then
15111 E := Find_Primitive_Covering_Interface
15112 (Tagged_Type => Tagged_Type,
15113 Iface_Prim => Subp);
15116 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15118 Replace_Elmt (Elmt, E);
15119 Remove_Homonym (Subp);
15127 -- Step 2: Add primitives of progenitors that are not implemented by
15128 -- parents of Tagged_Type.
15130 if Present (Interfaces (Base_Type (Tagged_Type))) then
15131 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15132 while Present (Iface_Elmt) loop
15133 Iface := Node (Iface_Elmt);
15135 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15136 while Present (Prim_Elmt) loop
15137 Iface_Subp := Node (Prim_Elmt);
15138 Iface_Alias := Ultimate_Alias (Iface_Subp);
15140 -- Exclude derivation of predefined primitives except those
15141 -- that come from source, or are inherited from one that comes
15142 -- from source. Required to catch declarations of equality
15143 -- operators of interfaces. For example:
15145 -- type Iface is interface;
15146 -- function "=" (Left, Right : Iface) return Boolean;
15148 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15149 or else Comes_From_Source (Iface_Alias)
15152 Find_Primitive_Covering_Interface
15153 (Tagged_Type => Tagged_Type,
15154 Iface_Prim => Iface_Subp);
15156 -- If not found we derive a new primitive leaving its alias
15157 -- attribute referencing the interface primitive.
15161 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15163 -- Ada 2012 (AI05-0197): If the covering primitive's name
15164 -- differs from the name of the interface primitive then it
15165 -- is a private primitive inherited from a parent type. In
15166 -- such case, given that Tagged_Type covers the interface,
15167 -- the inherited private primitive becomes visible. For such
15168 -- purpose we add a new entity that renames the inherited
15169 -- private primitive.
15171 elsif Chars (E) /= Chars (Iface_Subp) then
15172 pragma Assert (Has_Suffix (E, 'P'));
15174 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15175 Set_Alias (New_Subp, E);
15176 Set_Is_Abstract_Subprogram (New_Subp,
15177 Is_Abstract_Subprogram (E));
15179 -- Propagate to the full view interface entities associated
15180 -- with the partial view.
15182 elsif In_Private_Part (Current_Scope)
15183 and then Present (Alias (E))
15184 and then Alias (E) = Iface_Subp
15186 List_Containing (Parent (E)) /=
15187 Private_Declarations
15189 (Unit_Declaration_Node (Current_Scope)))
15191 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15195 Next_Elmt (Prim_Elmt);
15198 Next_Elmt (Iface_Elmt);
15201 end Derive_Progenitor_Subprograms;
15203 -----------------------
15204 -- Derive_Subprogram --
15205 -----------------------
15207 procedure Derive_Subprogram
15208 (New_Subp : out Entity_Id;
15209 Parent_Subp : Entity_Id;
15210 Derived_Type : Entity_Id;
15211 Parent_Type : Entity_Id;
15212 Actual_Subp : Entity_Id := Empty)
15214 Formal : Entity_Id;
15215 -- Formal parameter of parent primitive operation
15217 Formal_Of_Actual : Entity_Id;
15218 -- Formal parameter of actual operation, when the derivation is to
15219 -- create a renaming for a primitive operation of an actual in an
15222 New_Formal : Entity_Id;
15223 -- Formal of inherited operation
15225 Visible_Subp : Entity_Id := Parent_Subp;
15227 function Is_Private_Overriding return Boolean;
15228 -- If Subp is a private overriding of a visible operation, the inherited
15229 -- operation derives from the overridden op (even though its body is the
15230 -- overriding one) and the inherited operation is visible now. See
15231 -- sem_disp to see the full details of the handling of the overridden
15232 -- subprogram, which is removed from the list of primitive operations of
15233 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15234 -- and used to diagnose abstract operations that need overriding in the
15237 procedure Replace_Type (Id, New_Id : Entity_Id);
15238 -- When the type is an anonymous access type, create a new access type
15239 -- designating the derived type.
15241 procedure Set_Derived_Name;
15242 -- This procedure sets the appropriate Chars name for New_Subp. This
15243 -- is normally just a copy of the parent name. An exception arises for
15244 -- type support subprograms, where the name is changed to reflect the
15245 -- name of the derived type, e.g. if type foo is derived from type bar,
15246 -- then a procedure barDA is derived with a name fooDA.
15248 ---------------------------
15249 -- Is_Private_Overriding --
15250 ---------------------------
15252 function Is_Private_Overriding return Boolean is
15256 -- If the parent is not a dispatching operation there is no
15257 -- need to investigate overridings
15259 if not Is_Dispatching_Operation (Parent_Subp) then
15263 -- The visible operation that is overridden is a homonym of the
15264 -- parent subprogram. We scan the homonym chain to find the one
15265 -- whose alias is the subprogram we are deriving.
15267 Prev := Current_Entity (Parent_Subp);
15268 while Present (Prev) loop
15269 if Ekind (Prev) = Ekind (Parent_Subp)
15270 and then Alias (Prev) = Parent_Subp
15271 and then Scope (Parent_Subp) = Scope (Prev)
15272 and then not Is_Hidden (Prev)
15274 Visible_Subp := Prev;
15278 Prev := Homonym (Prev);
15282 end Is_Private_Overriding;
15288 procedure Replace_Type (Id, New_Id : Entity_Id) is
15289 Id_Type : constant Entity_Id := Etype (Id);
15290 Acc_Type : Entity_Id;
15291 Par : constant Node_Id := Parent (Derived_Type);
15294 -- When the type is an anonymous access type, create a new access
15295 -- type designating the derived type. This itype must be elaborated
15296 -- at the point of the derivation, not on subsequent calls that may
15297 -- be out of the proper scope for Gigi, so we insert a reference to
15298 -- it after the derivation.
15300 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15302 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15305 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15306 and then Present (Full_View (Desig_Typ))
15307 and then not Is_Private_Type (Parent_Type)
15309 Desig_Typ := Full_View (Desig_Typ);
15312 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15314 -- Ada 2005 (AI-251): Handle also derivations of abstract
15315 -- interface primitives.
15317 or else (Is_Interface (Desig_Typ)
15318 and then not Is_Class_Wide_Type (Desig_Typ))
15320 Acc_Type := New_Copy (Id_Type);
15321 Set_Etype (Acc_Type, Acc_Type);
15322 Set_Scope (Acc_Type, New_Subp);
15324 -- Set size of anonymous access type. If we have an access
15325 -- to an unconstrained array, this is a fat pointer, so it
15326 -- is sizes at twice addtress size.
15328 if Is_Array_Type (Desig_Typ)
15329 and then not Is_Constrained (Desig_Typ)
15331 Init_Size (Acc_Type, 2 * System_Address_Size);
15333 -- Other cases use a thin pointer
15336 Init_Size (Acc_Type, System_Address_Size);
15339 -- Set remaining characterstics of anonymous access type
15341 Init_Alignment (Acc_Type);
15342 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15344 Set_Etype (New_Id, Acc_Type);
15345 Set_Scope (New_Id, New_Subp);
15347 -- Create a reference to it
15349 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15352 Set_Etype (New_Id, Id_Type);
15356 -- In Ada2012, a formal may have an incomplete type but the type
15357 -- derivation that inherits the primitive follows the full view.
15359 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15361 (Ekind (Id_Type) = E_Record_Type_With_Private
15362 and then Present (Full_View (Id_Type))
15364 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15366 (Ada_Version >= Ada_2012
15367 and then Ekind (Id_Type) = E_Incomplete_Type
15368 and then Full_View (Id_Type) = Parent_Type)
15370 -- Constraint checks on formals are generated during expansion,
15371 -- based on the signature of the original subprogram. The bounds
15372 -- of the derived type are not relevant, and thus we can use
15373 -- the base type for the formals. However, the return type may be
15374 -- used in a context that requires that the proper static bounds
15375 -- be used (a case statement, for example) and for those cases
15376 -- we must use the derived type (first subtype), not its base.
15378 -- If the derived_type_definition has no constraints, we know that
15379 -- the derived type has the same constraints as the first subtype
15380 -- of the parent, and we can also use it rather than its base,
15381 -- which can lead to more efficient code.
15383 if Etype (Id) = Parent_Type then
15384 if Is_Scalar_Type (Parent_Type)
15386 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15388 Set_Etype (New_Id, Derived_Type);
15390 elsif Nkind (Par) = N_Full_Type_Declaration
15392 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15395 (Subtype_Indication (Type_Definition (Par)))
15397 Set_Etype (New_Id, Derived_Type);
15400 Set_Etype (New_Id, Base_Type (Derived_Type));
15404 Set_Etype (New_Id, Base_Type (Derived_Type));
15408 Set_Etype (New_Id, Etype (Id));
15412 ----------------------
15413 -- Set_Derived_Name --
15414 ----------------------
15416 procedure Set_Derived_Name is
15417 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15419 if Nm = TSS_Null then
15420 Set_Chars (New_Subp, Chars (Parent_Subp));
15422 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15424 end Set_Derived_Name;
15426 -- Start of processing for Derive_Subprogram
15429 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15430 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15432 -- Check whether the inherited subprogram is a private operation that
15433 -- should be inherited but not yet made visible. Such subprograms can
15434 -- become visible at a later point (e.g., the private part of a public
15435 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15436 -- following predicate is true, then this is not such a private
15437 -- operation and the subprogram simply inherits the name of the parent
15438 -- subprogram. Note the special check for the names of controlled
15439 -- operations, which are currently exempted from being inherited with
15440 -- a hidden name because they must be findable for generation of
15441 -- implicit run-time calls.
15443 if not Is_Hidden (Parent_Subp)
15444 or else Is_Internal (Parent_Subp)
15445 or else Is_Private_Overriding
15446 or else Is_Internal_Name (Chars (Parent_Subp))
15447 or else (Is_Controlled (Parent_Type)
15448 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15454 -- An inherited dispatching equality will be overridden by an internally
15455 -- generated one, or by an explicit one, so preserve its name and thus
15456 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15457 -- private operation it may become invisible if the full view has
15458 -- progenitors, and the dispatch table will be malformed.
15459 -- We check that the type is limited to handle the anomalous declaration
15460 -- of Limited_Controlled, which is derived from a non-limited type, and
15461 -- which is handled specially elsewhere as well.
15463 elsif Chars (Parent_Subp) = Name_Op_Eq
15464 and then Is_Dispatching_Operation (Parent_Subp)
15465 and then Etype (Parent_Subp) = Standard_Boolean
15466 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15468 Etype (First_Formal (Parent_Subp)) =
15469 Etype (Next_Formal (First_Formal (Parent_Subp)))
15473 -- If parent is hidden, this can be a regular derivation if the
15474 -- parent is immediately visible in a non-instantiating context,
15475 -- or if we are in the private part of an instance. This test
15476 -- should still be refined ???
15478 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15479 -- operation as a non-visible operation in cases where the parent
15480 -- subprogram might not be visible now, but was visible within the
15481 -- original generic, so it would be wrong to make the inherited
15482 -- subprogram non-visible now. (Not clear if this test is fully
15483 -- correct; are there any cases where we should declare the inherited
15484 -- operation as not visible to avoid it being overridden, e.g., when
15485 -- the parent type is a generic actual with private primitives ???)
15487 -- (they should be treated the same as other private inherited
15488 -- subprograms, but it's not clear how to do this cleanly). ???
15490 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15491 and then Is_Immediately_Visible (Parent_Subp)
15492 and then not In_Instance)
15493 or else In_Instance_Not_Visible
15497 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15498 -- overrides an interface primitive because interface primitives
15499 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15501 elsif Ada_Version >= Ada_2005
15502 and then Is_Dispatching_Operation (Parent_Subp)
15503 and then Present (Covered_Interface_Op (Parent_Subp))
15507 -- Otherwise, the type is inheriting a private operation, so enter it
15508 -- with a special name so it can't be overridden. See also below, where
15509 -- we check for this case, and if so avoid setting Requires_Overriding.
15512 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15515 Set_Parent (New_Subp, Parent (Derived_Type));
15517 if Present (Actual_Subp) then
15518 Replace_Type (Actual_Subp, New_Subp);
15520 Replace_Type (Parent_Subp, New_Subp);
15523 Conditional_Delay (New_Subp, Parent_Subp);
15525 -- If we are creating a renaming for a primitive operation of an
15526 -- actual of a generic derived type, we must examine the signature
15527 -- of the actual primitive, not that of the generic formal, which for
15528 -- example may be an interface. However the name and initial value
15529 -- of the inherited operation are those of the formal primitive.
15531 Formal := First_Formal (Parent_Subp);
15533 if Present (Actual_Subp) then
15534 Formal_Of_Actual := First_Formal (Actual_Subp);
15536 Formal_Of_Actual := Empty;
15539 while Present (Formal) loop
15540 New_Formal := New_Copy (Formal);
15542 -- Normally we do not go copying parents, but in the case of
15543 -- formals, we need to link up to the declaration (which is the
15544 -- parameter specification), and it is fine to link up to the
15545 -- original formal's parameter specification in this case.
15547 Set_Parent (New_Formal, Parent (Formal));
15548 Append_Entity (New_Formal, New_Subp);
15550 if Present (Formal_Of_Actual) then
15551 Replace_Type (Formal_Of_Actual, New_Formal);
15552 Next_Formal (Formal_Of_Actual);
15554 Replace_Type (Formal, New_Formal);
15557 Next_Formal (Formal);
15560 -- Extra formals are shared between the parent subprogram and the
15561 -- derived subprogram (implicit in the above copy of formals), and
15562 -- hence we must inherit also the reference to the first extra formal.
15564 Set_Extra_Formals (New_Subp, Extra_Formals (Parent_Subp));
15566 -- If this derivation corresponds to a tagged generic actual, then
15567 -- primitive operations rename those of the actual. Otherwise the
15568 -- primitive operations rename those of the parent type, If the parent
15569 -- renames an intrinsic operator, so does the new subprogram. We except
15570 -- concatenation, which is always properly typed, and does not get
15571 -- expanded as other intrinsic operations.
15573 if No (Actual_Subp) then
15574 if Is_Intrinsic_Subprogram (Parent_Subp) then
15575 Set_Is_Intrinsic_Subprogram (New_Subp);
15577 if Present (Alias (Parent_Subp))
15578 and then Chars (Parent_Subp) /= Name_Op_Concat
15580 Set_Alias (New_Subp, Alias (Parent_Subp));
15582 Set_Alias (New_Subp, Parent_Subp);
15586 Set_Alias (New_Subp, Parent_Subp);
15590 Set_Alias (New_Subp, Actual_Subp);
15593 -- Derived subprograms of a tagged type must inherit the convention
15594 -- of the parent subprogram (a requirement of AI-117). Derived
15595 -- subprograms of untagged types simply get convention Ada by default.
15597 -- If the derived type is a tagged generic formal type with unknown
15598 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15600 -- However, if the type is derived from a generic formal, the further
15601 -- inherited subprogram has the convention of the non-generic ancestor.
15602 -- Otherwise there would be no way to override the operation.
15603 -- (This is subject to forthcoming ARG discussions).
15605 if Is_Tagged_Type (Derived_Type) then
15606 if Is_Generic_Type (Derived_Type)
15607 and then Has_Unknown_Discriminants (Derived_Type)
15609 Set_Convention (New_Subp, Convention_Intrinsic);
15612 if Is_Generic_Type (Parent_Type)
15613 and then Has_Unknown_Discriminants (Parent_Type)
15615 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15617 Set_Convention (New_Subp, Convention (Parent_Subp));
15622 -- Predefined controlled operations retain their name even if the parent
15623 -- is hidden (see above), but they are not primitive operations if the
15624 -- ancestor is not visible, for example if the parent is a private
15625 -- extension completed with a controlled extension. Note that a full
15626 -- type that is controlled can break privacy: the flag Is_Controlled is
15627 -- set on both views of the type.
15629 if Is_Controlled (Parent_Type)
15630 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15633 and then Is_Hidden (Parent_Subp)
15634 and then not Is_Visibly_Controlled (Parent_Type)
15636 Set_Is_Hidden (New_Subp);
15639 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15640 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15642 if Ekind (Parent_Subp) = E_Procedure then
15643 Set_Is_Valued_Procedure
15644 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15646 Set_Has_Controlling_Result
15647 (New_Subp, Has_Controlling_Result (Parent_Subp));
15650 -- No_Return must be inherited properly. If this is overridden in the
15651 -- case of a dispatching operation, then the check is made later in
15652 -- Check_Abstract_Overriding that the overriding operation is also
15653 -- No_Return (no such check is required for the nondispatching case).
15655 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15657 -- A derived function with a controlling result is abstract. If the
15658 -- Derived_Type is a nonabstract formal generic derived type, then
15659 -- inherited operations are not abstract: the required check is done at
15660 -- instantiation time. If the derivation is for a generic actual, the
15661 -- function is not abstract unless the actual is.
15663 if Is_Generic_Type (Derived_Type)
15664 and then not Is_Abstract_Type (Derived_Type)
15668 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15669 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15671 -- A subprogram subject to pragma Extensions_Visible with value False
15672 -- requires overriding if the subprogram has at least one controlling
15673 -- OUT parameter (SPARK RM 6.1.7(6)).
15675 elsif Ada_Version >= Ada_2005
15676 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15677 or else (Is_Tagged_Type (Derived_Type)
15678 and then Etype (New_Subp) = Derived_Type
15679 and then not Is_Null_Extension (Derived_Type))
15680 or else (Is_Tagged_Type (Derived_Type)
15681 and then Ekind (Etype (New_Subp)) =
15682 E_Anonymous_Access_Type
15683 and then Designated_Type (Etype (New_Subp)) =
15685 and then not Is_Null_Extension (Derived_Type))
15686 or else (Comes_From_Source (Alias (New_Subp))
15687 and then Is_EVF_Procedure (Alias (New_Subp))))
15688 and then No (Actual_Subp)
15690 if not Is_Tagged_Type (Derived_Type)
15691 or else Is_Abstract_Type (Derived_Type)
15692 or else Is_Abstract_Subprogram (Alias (New_Subp))
15694 Set_Is_Abstract_Subprogram (New_Subp);
15696 -- If the Chars of the new subprogram is different from that of the
15697 -- parent's one, it means that we entered it with a special name so
15698 -- it can't be overridden (see above). In that case we had better not
15699 -- *require* it to be overridden. This is the case where the parent
15700 -- type inherited the operation privately, so there's no danger of
15701 -- dangling dispatching.
15703 elsif Chars (New_Subp) = Chars (Alias (New_Subp)) then
15704 Set_Requires_Overriding (New_Subp);
15707 elsif Ada_Version < Ada_2005
15708 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15709 or else (Is_Tagged_Type (Derived_Type)
15710 and then Etype (New_Subp) = Derived_Type
15711 and then No (Actual_Subp)))
15713 Set_Is_Abstract_Subprogram (New_Subp);
15715 -- AI05-0097 : an inherited operation that dispatches on result is
15716 -- abstract if the derived type is abstract, even if the parent type
15717 -- is concrete and the derived type is a null extension.
15719 elsif Has_Controlling_Result (Alias (New_Subp))
15720 and then Is_Abstract_Type (Etype (New_Subp))
15722 Set_Is_Abstract_Subprogram (New_Subp);
15724 -- Finally, if the parent type is abstract we must verify that all
15725 -- inherited operations are either non-abstract or overridden, or that
15726 -- the derived type itself is abstract (this check is performed at the
15727 -- end of a package declaration, in Check_Abstract_Overriding). A
15728 -- private overriding in the parent type will not be visible in the
15729 -- derivation if we are not in an inner package or in a child unit of
15730 -- the parent type, in which case the abstractness of the inherited
15731 -- operation is carried to the new subprogram.
15733 elsif Is_Abstract_Type (Parent_Type)
15734 and then not In_Open_Scopes (Scope (Parent_Type))
15735 and then Is_Private_Overriding
15736 and then Is_Abstract_Subprogram (Visible_Subp)
15738 if No (Actual_Subp) then
15739 Set_Alias (New_Subp, Visible_Subp);
15740 Set_Is_Abstract_Subprogram (New_Subp, True);
15743 -- If this is a derivation for an instance of a formal derived
15744 -- type, abstractness comes from the primitive operation of the
15745 -- actual, not from the operation inherited from the ancestor.
15747 Set_Is_Abstract_Subprogram
15748 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15752 New_Overloaded_Entity (New_Subp, Derived_Type);
15754 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15755 -- preconditions and the derived type is abstract, the derived operation
15756 -- is abstract as well if parent subprogram is not abstract or null.
15758 if Is_Abstract_Type (Derived_Type)
15759 and then Has_Non_Trivial_Precondition (Parent_Subp)
15760 and then Present (Interfaces (Derived_Type))
15763 -- Add useful attributes of subprogram before the freeze point,
15764 -- in case freezing is delayed or there are previous errors.
15766 Set_Is_Dispatching_Operation (New_Subp);
15769 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15772 if Present (Iface_Prim)
15773 and then Has_Non_Trivial_Precondition (Iface_Prim)
15775 Set_Is_Abstract_Subprogram (New_Subp);
15780 -- Check for case of a derived subprogram for the instantiation of a
15781 -- formal derived tagged type, if so mark the subprogram as dispatching
15782 -- and inherit the dispatching attributes of the actual subprogram. The
15783 -- derived subprogram is effectively renaming of the actual subprogram,
15784 -- so it needs to have the same attributes as the actual.
15786 if Present (Actual_Subp)
15787 and then Is_Dispatching_Operation (Actual_Subp)
15789 Set_Is_Dispatching_Operation (New_Subp);
15791 if Present (DTC_Entity (Actual_Subp)) then
15792 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15793 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15797 -- Indicate that a derived subprogram does not require a body and that
15798 -- it does not require processing of default expressions.
15800 Set_Has_Completion (New_Subp);
15801 Set_Default_Expressions_Processed (New_Subp);
15803 if Ekind (New_Subp) = E_Function then
15804 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15806 end Derive_Subprogram;
15808 ------------------------
15809 -- Derive_Subprograms --
15810 ------------------------
15812 procedure Derive_Subprograms
15813 (Parent_Type : Entity_Id;
15814 Derived_Type : Entity_Id;
15815 Generic_Actual : Entity_Id := Empty)
15817 Op_List : constant Elist_Id :=
15818 Collect_Primitive_Operations (Parent_Type);
15820 function Check_Derived_Type return Boolean;
15821 -- Check that all the entities derived from Parent_Type are found in
15822 -- the list of primitives of Derived_Type exactly in the same order.
15824 procedure Derive_Interface_Subprogram
15825 (New_Subp : out Entity_Id;
15827 Actual_Subp : Entity_Id);
15828 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15829 -- (which is an interface primitive). If Generic_Actual is present then
15830 -- Actual_Subp is the actual subprogram corresponding with the generic
15831 -- subprogram Subp.
15833 ------------------------
15834 -- Check_Derived_Type --
15835 ------------------------
15837 function Check_Derived_Type return Boolean is
15841 New_Subp : Entity_Id;
15846 -- Traverse list of entities in the current scope searching for
15847 -- an incomplete type whose full-view is derived type.
15849 E := First_Entity (Scope (Derived_Type));
15850 while Present (E) and then E /= Derived_Type loop
15851 if Ekind (E) = E_Incomplete_Type
15852 and then Present (Full_View (E))
15853 and then Full_View (E) = Derived_Type
15855 -- Disable this test if Derived_Type completes an incomplete
15856 -- type because in such case more primitives can be added
15857 -- later to the list of primitives of Derived_Type by routine
15858 -- Process_Incomplete_Dependents
15866 List := Collect_Primitive_Operations (Derived_Type);
15867 Elmt := First_Elmt (List);
15869 Op_Elmt := First_Elmt (Op_List);
15870 while Present (Op_Elmt) loop
15871 Subp := Node (Op_Elmt);
15872 New_Subp := Node (Elmt);
15874 -- At this early stage Derived_Type has no entities with attribute
15875 -- Interface_Alias. In addition, such primitives are always
15876 -- located at the end of the list of primitives of Parent_Type.
15877 -- Therefore, if found we can safely stop processing pending
15880 exit when Present (Interface_Alias (Subp));
15882 -- Handle hidden entities
15884 if not Is_Predefined_Dispatching_Operation (Subp)
15885 and then Is_Hidden (Subp)
15887 if Present (New_Subp)
15888 and then Primitive_Names_Match (Subp, New_Subp)
15894 if not Present (New_Subp)
15895 or else Ekind (Subp) /= Ekind (New_Subp)
15896 or else not Primitive_Names_Match (Subp, New_Subp)
15904 Next_Elmt (Op_Elmt);
15908 end Check_Derived_Type;
15910 ---------------------------------
15911 -- Derive_Interface_Subprogram --
15912 ---------------------------------
15914 procedure Derive_Interface_Subprogram
15915 (New_Subp : out Entity_Id;
15917 Actual_Subp : Entity_Id)
15919 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15920 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15923 pragma Assert (Is_Interface (Iface_Type));
15926 (New_Subp => New_Subp,
15927 Parent_Subp => Iface_Subp,
15928 Derived_Type => Derived_Type,
15929 Parent_Type => Iface_Type,
15930 Actual_Subp => Actual_Subp);
15932 -- Given that this new interface entity corresponds with a primitive
15933 -- of the parent that was not overridden we must leave it associated
15934 -- with its parent primitive to ensure that it will share the same
15935 -- dispatch table slot when overridden. We must set the Alias to Subp
15936 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15937 -- (in case we inherited Subp from Iface_Type via a nonabstract
15938 -- generic formal type).
15940 if No (Actual_Subp) then
15941 Set_Alias (New_Subp, Subp);
15944 T : Entity_Id := Find_Dispatching_Type (Subp);
15946 while Etype (T) /= T loop
15947 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15948 Set_Is_Abstract_Subprogram (New_Subp, False);
15956 -- For instantiations this is not needed since the previous call to
15957 -- Derive_Subprogram leaves the entity well decorated.
15960 pragma Assert (Alias (New_Subp) = Actual_Subp);
15963 end Derive_Interface_Subprogram;
15967 Alias_Subp : Entity_Id;
15968 Act_List : Elist_Id;
15969 Act_Elmt : Elmt_Id;
15970 Act_Subp : Entity_Id := Empty;
15972 Need_Search : Boolean := False;
15973 New_Subp : Entity_Id := Empty;
15974 Parent_Base : Entity_Id;
15977 -- Start of processing for Derive_Subprograms
15980 if Ekind (Parent_Type) = E_Record_Type_With_Private
15981 and then Has_Discriminants (Parent_Type)
15982 and then Present (Full_View (Parent_Type))
15984 Parent_Base := Full_View (Parent_Type);
15986 Parent_Base := Parent_Type;
15989 if Present (Generic_Actual) then
15990 Act_List := Collect_Primitive_Operations (Generic_Actual);
15991 Act_Elmt := First_Elmt (Act_List);
15993 Act_List := No_Elist;
15994 Act_Elmt := No_Elmt;
15997 -- Derive primitives inherited from the parent. Note that if the generic
15998 -- actual is present, this is not really a type derivation, it is a
15999 -- completion within an instance.
16001 -- Case 1: Derived_Type does not implement interfaces
16003 if not Is_Tagged_Type (Derived_Type)
16004 or else (not Has_Interfaces (Derived_Type)
16005 and then not (Present (Generic_Actual)
16006 and then Has_Interfaces (Generic_Actual)))
16008 Elmt := First_Elmt (Op_List);
16009 while Present (Elmt) loop
16010 Subp := Node (Elmt);
16012 -- Literals are derived earlier in the process of building the
16013 -- derived type, and are skipped here.
16015 if Ekind (Subp) = E_Enumeration_Literal then
16018 -- The actual is a direct descendant and the common primitive
16019 -- operations appear in the same order.
16021 -- If the generic parent type is present, the derived type is an
16022 -- instance of a formal derived type, and within the instance its
16023 -- operations are those of the actual. We derive from the formal
16024 -- type but make the inherited operations aliases of the
16025 -- corresponding operations of the actual.
16028 pragma Assert (No (Node (Act_Elmt))
16029 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
16032 (Subp, Node (Act_Elmt),
16033 Skip_Controlling_Formals => True)));
16036 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
16038 if Present (Act_Elmt) then
16039 Next_Elmt (Act_Elmt);
16046 -- Case 2: Derived_Type implements interfaces
16049 -- If the parent type has no predefined primitives we remove
16050 -- predefined primitives from the list of primitives of generic
16051 -- actual to simplify the complexity of this algorithm.
16053 if Present (Generic_Actual) then
16055 Has_Predefined_Primitives : Boolean := False;
16058 -- Check if the parent type has predefined primitives
16060 Elmt := First_Elmt (Op_List);
16061 while Present (Elmt) loop
16062 Subp := Node (Elmt);
16064 if Is_Predefined_Dispatching_Operation (Subp)
16065 and then not Comes_From_Source (Ultimate_Alias (Subp))
16067 Has_Predefined_Primitives := True;
16074 -- Remove predefined primitives of Generic_Actual. We must use
16075 -- an auxiliary list because in case of tagged types the value
16076 -- returned by Collect_Primitive_Operations is the value stored
16077 -- in its Primitive_Operations attribute (and we don't want to
16078 -- modify its current contents).
16080 if not Has_Predefined_Primitives then
16082 Aux_List : constant Elist_Id := New_Elmt_List;
16085 Elmt := First_Elmt (Act_List);
16086 while Present (Elmt) loop
16087 Subp := Node (Elmt);
16089 if not Is_Predefined_Dispatching_Operation (Subp)
16090 or else Comes_From_Source (Subp)
16092 Append_Elmt (Subp, Aux_List);
16098 Act_List := Aux_List;
16102 Act_Elmt := First_Elmt (Act_List);
16103 Act_Subp := Node (Act_Elmt);
16107 -- Stage 1: If the generic actual is not present we derive the
16108 -- primitives inherited from the parent type. If the generic parent
16109 -- type is present, the derived type is an instance of a formal
16110 -- derived type, and within the instance its operations are those of
16111 -- the actual. We derive from the formal type but make the inherited
16112 -- operations aliases of the corresponding operations of the actual.
16114 Elmt := First_Elmt (Op_List);
16115 while Present (Elmt) loop
16116 Subp := Node (Elmt);
16117 Alias_Subp := Ultimate_Alias (Subp);
16119 -- Do not derive internal entities of the parent that link
16120 -- interface primitives with their covering primitive. These
16121 -- entities will be added to this type when frozen.
16123 if Present (Interface_Alias (Subp)) then
16127 -- If the generic actual is present find the corresponding
16128 -- operation in the generic actual. If the parent type is a
16129 -- direct ancestor of the derived type then, even if it is an
16130 -- interface, the operations are inherited from the primary
16131 -- dispatch table and are in the proper order. If we detect here
16132 -- that primitives are not in the same order we traverse the list
16133 -- of primitive operations of the actual to find the one that
16134 -- implements the interface primitive.
16138 (Present (Generic_Actual)
16139 and then Present (Act_Subp)
16141 (Primitive_Names_Match (Subp, Act_Subp)
16143 Type_Conformant (Subp, Act_Subp,
16144 Skip_Controlling_Formals => True)))
16146 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16147 Use_Full_View => True));
16149 -- Remember that we need searching for all pending primitives
16151 Need_Search := True;
16153 -- Handle entities associated with interface primitives
16155 if Present (Alias_Subp)
16156 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16157 and then not Is_Predefined_Dispatching_Operation (Subp)
16159 -- Search for the primitive in the homonym chain
16162 Find_Primitive_Covering_Interface
16163 (Tagged_Type => Generic_Actual,
16164 Iface_Prim => Alias_Subp);
16166 -- Previous search may not locate primitives covering
16167 -- interfaces defined in generics units or instantiations.
16168 -- (it fails if the covering primitive has formals whose
16169 -- type is also defined in generics or instantiations).
16170 -- In such case we search in the list of primitives of the
16171 -- generic actual for the internal entity that links the
16172 -- interface primitive and the covering primitive.
16175 and then Is_Generic_Type (Parent_Type)
16177 -- This code has been designed to handle only generic
16178 -- formals that implement interfaces that are defined
16179 -- in a generic unit or instantiation. If this code is
16180 -- needed for other cases we must review it because
16181 -- (given that it relies on Original_Location to locate
16182 -- the primitive of Generic_Actual that covers the
16183 -- interface) it could leave linked through attribute
16184 -- Alias entities of unrelated instantiations).
16188 (Scope (Find_Dispatching_Type (Alias_Subp)))
16190 Instantiation_Depth
16191 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16194 Iface_Prim_Loc : constant Source_Ptr :=
16195 Original_Location (Sloc (Alias_Subp));
16202 First_Elmt (Primitive_Operations (Generic_Actual));
16204 Search : while Present (Elmt) loop
16205 Prim := Node (Elmt);
16207 if Present (Interface_Alias (Prim))
16208 and then Original_Location
16209 (Sloc (Interface_Alias (Prim))) =
16212 Act_Subp := Alias (Prim);
16221 pragma Assert (Present (Act_Subp)
16222 or else Is_Abstract_Type (Generic_Actual)
16223 or else Serious_Errors_Detected > 0);
16225 -- Handle predefined primitives plus the rest of user-defined
16229 Act_Elmt := First_Elmt (Act_List);
16230 while Present (Act_Elmt) loop
16231 Act_Subp := Node (Act_Elmt);
16233 exit when Primitive_Names_Match (Subp, Act_Subp)
16234 and then Type_Conformant
16236 Skip_Controlling_Formals => True)
16237 and then No (Interface_Alias (Act_Subp));
16239 Next_Elmt (Act_Elmt);
16242 if No (Act_Elmt) then
16248 -- Case 1: If the parent is a limited interface then it has the
16249 -- predefined primitives of synchronized interfaces. However, the
16250 -- actual type may be a non-limited type and hence it does not
16251 -- have such primitives.
16253 if Present (Generic_Actual)
16254 and then not Present (Act_Subp)
16255 and then Is_Limited_Interface (Parent_Base)
16256 and then Is_Predefined_Interface_Primitive (Subp)
16260 -- Case 2: Inherit entities associated with interfaces that were
16261 -- not covered by the parent type. We exclude here null interface
16262 -- primitives because they do not need special management.
16264 -- We also exclude interface operations that are renamings. If the
16265 -- subprogram is an explicit renaming of an interface primitive,
16266 -- it is a regular primitive operation, and the presence of its
16267 -- alias is not relevant: it has to be derived like any other
16270 elsif Present (Alias (Subp))
16271 and then Nkind (Unit_Declaration_Node (Subp)) /=
16272 N_Subprogram_Renaming_Declaration
16273 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16275 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16276 and then Null_Present (Parent (Alias_Subp)))
16278 -- If this is an abstract private type then we transfer the
16279 -- derivation of the interface primitive from the partial view
16280 -- to the full view. This is safe because all the interfaces
16281 -- must be visible in the partial view. Done to avoid adding
16282 -- a new interface derivation to the private part of the
16283 -- enclosing package; otherwise this new derivation would be
16284 -- decorated as hidden when the analysis of the enclosing
16285 -- package completes.
16287 if Is_Abstract_Type (Derived_Type)
16288 and then In_Private_Part (Current_Scope)
16289 and then Has_Private_Declaration (Derived_Type)
16292 Partial_View : Entity_Id;
16297 Partial_View := First_Entity (Current_Scope);
16299 exit when No (Partial_View)
16300 or else (Has_Private_Declaration (Partial_View)
16302 Full_View (Partial_View) = Derived_Type);
16304 Next_Entity (Partial_View);
16307 -- If the partial view was not found then the source code
16308 -- has errors and the derivation is not needed.
16310 if Present (Partial_View) then
16312 First_Elmt (Primitive_Operations (Partial_View));
16313 while Present (Elmt) loop
16314 Ent := Node (Elmt);
16316 if Present (Alias (Ent))
16317 and then Ultimate_Alias (Ent) = Alias (Subp)
16320 (Ent, Primitive_Operations (Derived_Type));
16327 -- If the interface primitive was not found in the
16328 -- partial view then this interface primitive was
16329 -- overridden. We add a derivation to activate in
16330 -- Derive_Progenitor_Subprograms the machinery to
16334 Derive_Interface_Subprogram
16335 (New_Subp => New_Subp,
16337 Actual_Subp => Act_Subp);
16342 Derive_Interface_Subprogram
16343 (New_Subp => New_Subp,
16345 Actual_Subp => Act_Subp);
16348 -- Case 3: Common derivation
16352 (New_Subp => New_Subp,
16353 Parent_Subp => Subp,
16354 Derived_Type => Derived_Type,
16355 Parent_Type => Parent_Base,
16356 Actual_Subp => Act_Subp);
16359 -- No need to update Act_Elm if we must search for the
16360 -- corresponding operation in the generic actual
16363 and then Present (Act_Elmt)
16365 Next_Elmt (Act_Elmt);
16366 Act_Subp := Node (Act_Elmt);
16373 -- Inherit additional operations from progenitors. If the derived
16374 -- type is a generic actual, there are not new primitive operations
16375 -- for the type because it has those of the actual, and therefore
16376 -- nothing needs to be done. The renamings generated above are not
16377 -- primitive operations, and their purpose is simply to make the
16378 -- proper operations visible within an instantiation.
16380 if No (Generic_Actual) then
16381 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16385 -- Final check: Direct descendants must have their primitives in the
16386 -- same order. We exclude from this test untagged types and instances
16387 -- of formal derived types. We skip this test if we have already
16388 -- reported serious errors in the sources.
16390 pragma Assert (not Is_Tagged_Type (Derived_Type)
16391 or else Present (Generic_Actual)
16392 or else Serious_Errors_Detected > 0
16393 or else Check_Derived_Type);
16394 end Derive_Subprograms;
16396 --------------------------------
16397 -- Derived_Standard_Character --
16398 --------------------------------
16400 procedure Derived_Standard_Character
16402 Parent_Type : Entity_Id;
16403 Derived_Type : Entity_Id)
16405 Loc : constant Source_Ptr := Sloc (N);
16406 Def : constant Node_Id := Type_Definition (N);
16407 Indic : constant Node_Id := Subtype_Indication (Def);
16408 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16409 Implicit_Base : constant Entity_Id :=
16411 (E_Enumeration_Type, N, Derived_Type, 'B');
16417 Discard_Node (Process_Subtype (Indic, N));
16419 Set_Etype (Implicit_Base, Parent_Base);
16420 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16421 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16423 Set_Is_Character_Type (Implicit_Base, True);
16424 Set_Has_Delayed_Freeze (Implicit_Base);
16426 -- The bounds of the implicit base are the bounds of the parent base.
16427 -- Note that their type is the parent base.
16429 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16430 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16432 Set_Scalar_Range (Implicit_Base,
16435 High_Bound => Hi));
16437 Conditional_Delay (Derived_Type, Parent_Type);
16439 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16440 Set_Etype (Derived_Type, Implicit_Base);
16441 Set_Size_Info (Derived_Type, Parent_Type);
16443 if Unknown_RM_Size (Derived_Type) then
16444 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16447 Set_Is_Character_Type (Derived_Type, True);
16449 if Nkind (Indic) /= N_Subtype_Indication then
16451 -- If no explicit constraint, the bounds are those
16452 -- of the parent type.
16454 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16455 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16456 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16459 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16461 -- Because the implicit base is used in the conversion of the bounds, we
16462 -- have to freeze it now. This is similar to what is done for numeric
16463 -- types, and it equally suspicious, but otherwise a nonstatic bound
16464 -- will have a reference to an unfrozen type, which is rejected by Gigi
16465 -- (???). This requires specific care for definition of stream
16466 -- attributes. For details, see comments at the end of
16467 -- Build_Derived_Numeric_Type.
16469 Freeze_Before (N, Implicit_Base);
16470 end Derived_Standard_Character;
16472 ------------------------------
16473 -- Derived_Type_Declaration --
16474 ------------------------------
16476 procedure Derived_Type_Declaration
16479 Is_Completion : Boolean)
16481 Parent_Type : Entity_Id;
16483 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16484 -- Check whether the parent type is a generic formal, or derives
16485 -- directly or indirectly from one.
16487 ------------------------
16488 -- Comes_From_Generic --
16489 ------------------------
16491 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16493 if Is_Generic_Type (Typ) then
16496 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16499 elsif Is_Private_Type (Typ)
16500 and then Present (Full_View (Typ))
16501 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16505 elsif Is_Generic_Actual_Type (Typ) then
16511 end Comes_From_Generic;
16515 Def : constant Node_Id := Type_Definition (N);
16516 Iface_Def : Node_Id;
16517 Indic : constant Node_Id := Subtype_Indication (Def);
16518 Extension : constant Node_Id := Record_Extension_Part (Def);
16519 Parent_Node : Node_Id;
16522 -- Start of processing for Derived_Type_Declaration
16525 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16528 and then Is_Tagged_Type (Parent_Type)
16531 Partial_View : constant Entity_Id :=
16532 Incomplete_Or_Partial_View (Parent_Type);
16535 -- If the partial view was not found then the parent type is not
16536 -- a private type. Otherwise check if the partial view is a tagged
16539 if Present (Partial_View)
16540 and then Is_Private_Type (Partial_View)
16541 and then not Is_Tagged_Type (Partial_View)
16544 ("cannot derive from & declared as untagged private "
16545 & "(SPARK RM 3.4(1))", N, Partial_View);
16550 -- Ada 2005 (AI-251): In case of interface derivation check that the
16551 -- parent is also an interface.
16553 if Interface_Present (Def) then
16554 if not Is_Interface (Parent_Type) then
16555 Diagnose_Interface (Indic, Parent_Type);
16558 Parent_Node := Parent (Base_Type (Parent_Type));
16559 Iface_Def := Type_Definition (Parent_Node);
16561 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16562 -- other limited interfaces.
16564 if Limited_Present (Def) then
16565 if Limited_Present (Iface_Def) then
16568 elsif Protected_Present (Iface_Def) then
16570 ("descendant of & must be declared as a protected "
16571 & "interface", N, Parent_Type);
16573 elsif Synchronized_Present (Iface_Def) then
16575 ("descendant of & must be declared as a synchronized "
16576 & "interface", N, Parent_Type);
16578 elsif Task_Present (Iface_Def) then
16580 ("descendant of & must be declared as a task interface",
16585 ("(Ada 2005) limited interface cannot inherit from "
16586 & "non-limited interface", Indic);
16589 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16590 -- from non-limited or limited interfaces.
16592 elsif not Protected_Present (Def)
16593 and then not Synchronized_Present (Def)
16594 and then not Task_Present (Def)
16596 if Limited_Present (Iface_Def) then
16599 elsif Protected_Present (Iface_Def) then
16601 ("descendant of & must be declared as a protected "
16602 & "interface", N, Parent_Type);
16604 elsif Synchronized_Present (Iface_Def) then
16606 ("descendant of & must be declared as a synchronized "
16607 & "interface", N, Parent_Type);
16609 elsif Task_Present (Iface_Def) then
16611 ("descendant of & must be declared as a task interface",
16620 if Is_Tagged_Type (Parent_Type)
16621 and then Is_Concurrent_Type (Parent_Type)
16622 and then not Is_Interface (Parent_Type)
16625 ("parent type of a record extension cannot be a synchronized "
16626 & "tagged type (RM 3.9.1 (3/1))", N);
16627 Set_Etype (T, Any_Type);
16631 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16634 if Is_Tagged_Type (Parent_Type)
16635 and then Is_Non_Empty_List (Interface_List (Def))
16642 Intf := First (Interface_List (Def));
16643 while Present (Intf) loop
16644 T := Find_Type_Of_Subtype_Indic (Intf);
16646 if not Is_Interface (T) then
16647 Diagnose_Interface (Intf, T);
16649 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16650 -- a limited type from having a nonlimited progenitor.
16652 elsif (Limited_Present (Def)
16653 or else (not Is_Interface (Parent_Type)
16654 and then Is_Limited_Type (Parent_Type)))
16655 and then not Is_Limited_Interface (T)
16658 ("progenitor interface& of limited type must be limited",
16667 if Parent_Type = Any_Type
16668 or else Etype (Parent_Type) = Any_Type
16669 or else (Is_Class_Wide_Type (Parent_Type)
16670 and then Etype (Parent_Type) = T)
16672 -- If Parent_Type is undefined or illegal, make new type into a
16673 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16674 -- errors. If this is a self-definition, emit error now.
16676 if T = Parent_Type or else T = Etype (Parent_Type) then
16677 Error_Msg_N ("type cannot be used in its own definition", Indic);
16680 Set_Ekind (T, Ekind (Parent_Type));
16681 Set_Etype (T, Any_Type);
16682 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16684 if Is_Tagged_Type (T)
16685 and then Is_Record_Type (T)
16687 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16693 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16694 -- an interface is special because the list of interfaces in the full
16695 -- view can be given in any order. For example:
16697 -- type A is interface;
16698 -- type B is interface and A;
16699 -- type D is new B with private;
16701 -- type D is new A and B with null record; -- 1 --
16703 -- In this case we perform the following transformation of -1-:
16705 -- type D is new B and A with null record;
16707 -- If the parent of the full-view covers the parent of the partial-view
16708 -- we have two possible cases:
16710 -- 1) They have the same parent
16711 -- 2) The parent of the full-view implements some further interfaces
16713 -- In both cases we do not need to perform the transformation. In the
16714 -- first case the source program is correct and the transformation is
16715 -- not needed; in the second case the source program does not fulfill
16716 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16719 -- This transformation not only simplifies the rest of the analysis of
16720 -- this type declaration but also simplifies the correct generation of
16721 -- the object layout to the expander.
16723 if In_Private_Part (Current_Scope)
16724 and then Is_Interface (Parent_Type)
16728 Partial_View : Entity_Id;
16729 Partial_View_Parent : Entity_Id;
16730 New_Iface : Node_Id;
16733 -- Look for the associated private type declaration
16735 Partial_View := Incomplete_Or_Partial_View (T);
16737 -- If the partial view was not found then the source code has
16738 -- errors and the transformation is not needed.
16740 if Present (Partial_View) then
16741 Partial_View_Parent := Etype (Partial_View);
16743 -- If the parent of the full-view covers the parent of the
16744 -- partial-view we have nothing else to do.
16746 if Interface_Present_In_Ancestor
16747 (Parent_Type, Partial_View_Parent)
16751 -- Traverse the list of interfaces of the full-view to look
16752 -- for the parent of the partial-view and perform the tree
16756 Iface := First (Interface_List (Def));
16757 while Present (Iface) loop
16758 if Etype (Iface) = Etype (Partial_View) then
16759 Rewrite (Subtype_Indication (Def),
16760 New_Copy (Subtype_Indication
16761 (Parent (Partial_View))));
16764 Make_Identifier (Sloc (N), Chars (Parent_Type));
16765 Append (New_Iface, Interface_List (Def));
16767 -- Analyze the transformed code
16769 Derived_Type_Declaration (T, N, Is_Completion);
16780 -- Only composite types other than array types are allowed to have
16783 if Present (Discriminant_Specifications (N)) then
16784 if (Is_Elementary_Type (Parent_Type)
16786 Is_Array_Type (Parent_Type))
16787 and then not Error_Posted (N)
16790 ("elementary or array type cannot have discriminants",
16791 Defining_Identifier (First (Discriminant_Specifications (N))));
16793 -- Unset Has_Discriminants flag to prevent cascaded errors, but
16794 -- only if we are not already processing a malformed syntax tree.
16796 if Is_Type (T) then
16797 Set_Has_Discriminants (T, False);
16802 -- In Ada 83, a derived type defined in a package specification cannot
16803 -- be used for further derivation until the end of its visible part.
16804 -- Note that derivation in the private part of the package is allowed.
16806 if Ada_Version = Ada_83
16807 and then Is_Derived_Type (Parent_Type)
16808 and then In_Visible_Part (Scope (Parent_Type))
16810 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16812 ("(Ada 83): premature use of type for derivation", Indic);
16816 -- Check for early use of incomplete or private type
16818 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16819 Error_Msg_N ("premature derivation of incomplete type", Indic);
16822 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16823 and then not Comes_From_Generic (Parent_Type))
16824 or else Has_Private_Component (Parent_Type)
16826 -- The ancestor type of a formal type can be incomplete, in which
16827 -- case only the operations of the partial view are available in the
16828 -- generic. Subsequent checks may be required when the full view is
16829 -- analyzed to verify that a derivation from a tagged type has an
16832 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16835 elsif No (Underlying_Type (Parent_Type))
16836 or else Has_Private_Component (Parent_Type)
16839 ("premature derivation of derived or private type", Indic);
16841 -- Flag the type itself as being in error, this prevents some
16842 -- nasty problems with subsequent uses of the malformed type.
16844 Set_Error_Posted (T);
16846 -- Check that within the immediate scope of an untagged partial
16847 -- view it's illegal to derive from the partial view if the
16848 -- full view is tagged. (7.3(7))
16850 -- We verify that the Parent_Type is a partial view by checking
16851 -- that it is not a Full_Type_Declaration (i.e. a private type or
16852 -- private extension declaration), to distinguish a partial view
16853 -- from a derivation from a private type which also appears as
16854 -- E_Private_Type. If the parent base type is not declared in an
16855 -- enclosing scope there is no need to check.
16857 elsif Present (Full_View (Parent_Type))
16858 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16859 and then not Is_Tagged_Type (Parent_Type)
16860 and then Is_Tagged_Type (Full_View (Parent_Type))
16861 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16864 ("premature derivation from type with tagged full view",
16869 -- Check that form of derivation is appropriate
16871 Taggd := Is_Tagged_Type (Parent_Type);
16873 -- Set the parent type to the class-wide type's specific type in this
16874 -- case to prevent cascading errors
16876 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16877 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16878 Set_Etype (T, Etype (Parent_Type));
16882 if Present (Extension) and then not Taggd then
16884 ("type derived from untagged type cannot have extension", Indic);
16886 elsif No (Extension) and then Taggd then
16888 -- If this declaration is within a private part (or body) of a
16889 -- generic instantiation then the derivation is allowed (the parent
16890 -- type can only appear tagged in this case if it's a generic actual
16891 -- type, since it would otherwise have been rejected in the analysis
16892 -- of the generic template).
16894 if not Is_Generic_Actual_Type (Parent_Type)
16895 or else In_Visible_Part (Scope (Parent_Type))
16897 if Is_Class_Wide_Type (Parent_Type) then
16899 ("parent type must not be a class-wide type", Indic);
16901 -- Use specific type to prevent cascaded errors.
16903 Parent_Type := Etype (Parent_Type);
16907 ("type derived from tagged type must have extension", Indic);
16912 -- AI-443: Synchronized formal derived types require a private
16913 -- extension. There is no point in checking the ancestor type or
16914 -- the progenitors since the construct is wrong to begin with.
16916 if Ada_Version >= Ada_2005
16917 and then Is_Generic_Type (T)
16918 and then Present (Original_Node (N))
16921 Decl : constant Node_Id := Original_Node (N);
16924 if Nkind (Decl) = N_Formal_Type_Declaration
16925 and then Nkind (Formal_Type_Definition (Decl)) =
16926 N_Formal_Derived_Type_Definition
16927 and then Synchronized_Present (Formal_Type_Definition (Decl))
16928 and then No (Extension)
16930 -- Avoid emitting a duplicate error message
16932 and then not Error_Posted (Indic)
16935 ("synchronized derived type must have extension", N);
16940 if Null_Exclusion_Present (Def)
16941 and then not Is_Access_Type (Parent_Type)
16943 Error_Msg_N ("null exclusion can only apply to an access type", N);
16946 -- Avoid deriving parent primitives of underlying record views
16948 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16949 Derive_Subps => not Is_Underlying_Record_View (T));
16951 -- AI-419: The parent type of an explicitly limited derived type must
16952 -- be a limited type or a limited interface.
16954 if Limited_Present (Def) then
16955 Set_Is_Limited_Record (T);
16957 if Is_Interface (T) then
16958 Set_Is_Limited_Interface (T);
16961 if not Is_Limited_Type (Parent_Type)
16963 (not Is_Interface (Parent_Type)
16964 or else not Is_Limited_Interface (Parent_Type))
16966 -- AI05-0096: a derivation in the private part of an instance is
16967 -- legal if the generic formal is untagged limited, and the actual
16970 if Is_Generic_Actual_Type (Parent_Type)
16971 and then In_Private_Part (Current_Scope)
16974 (Generic_Parent_Type (Parent (Parent_Type)))
16980 ("parent type& of limited type must be limited",
16985 end Derived_Type_Declaration;
16987 ------------------------
16988 -- Diagnose_Interface --
16989 ------------------------
16991 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16993 if not Is_Interface (E) and then E /= Any_Type then
16994 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16996 end Diagnose_Interface;
16998 ----------------------------------
16999 -- Enumeration_Type_Declaration --
17000 ----------------------------------
17002 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17009 -- Create identifier node representing lower bound
17011 B_Node := New_Node (N_Identifier, Sloc (Def));
17012 L := First (Literals (Def));
17013 Set_Chars (B_Node, Chars (L));
17014 Set_Entity (B_Node, L);
17015 Set_Etype (B_Node, T);
17016 Set_Is_Static_Expression (B_Node, True);
17018 R_Node := New_Node (N_Range, Sloc (Def));
17019 Set_Low_Bound (R_Node, B_Node);
17021 Set_Ekind (T, E_Enumeration_Type);
17022 Set_First_Literal (T, L);
17024 Set_Is_Constrained (T);
17028 -- Loop through literals of enumeration type setting pos and rep values
17029 -- except that if the Ekind is already set, then it means the literal
17030 -- was already constructed (case of a derived type declaration and we
17031 -- should not disturb the Pos and Rep values.
17033 while Present (L) loop
17034 if Ekind (L) /= E_Enumeration_Literal then
17035 Set_Ekind (L, E_Enumeration_Literal);
17036 Set_Enumeration_Pos (L, Ev);
17037 Set_Enumeration_Rep (L, Ev);
17038 Set_Is_Known_Valid (L, True);
17042 New_Overloaded_Entity (L);
17043 Generate_Definition (L);
17044 Set_Convention (L, Convention_Intrinsic);
17046 -- Case of character literal
17048 if Nkind (L) = N_Defining_Character_Literal then
17049 Set_Is_Character_Type (T, True);
17051 -- Check violation of No_Wide_Characters
17053 if Restriction_Check_Required (No_Wide_Characters) then
17054 Get_Name_String (Chars (L));
17056 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17057 Check_Restriction (No_Wide_Characters, L);
17066 -- Now create a node representing upper bound
17068 B_Node := New_Node (N_Identifier, Sloc (Def));
17069 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17070 Set_Entity (B_Node, Last (Literals (Def)));
17071 Set_Etype (B_Node, T);
17072 Set_Is_Static_Expression (B_Node, True);
17074 Set_High_Bound (R_Node, B_Node);
17076 -- Initialize various fields of the type. Some of this information
17077 -- may be overwritten later through rep.clauses.
17079 Set_Scalar_Range (T, R_Node);
17080 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17081 Set_Enum_Esize (T);
17082 Set_Enum_Pos_To_Rep (T, Empty);
17084 -- Set Discard_Names if configuration pragma set, or if there is
17085 -- a parameterless pragma in the current declarative region
17087 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17088 Set_Discard_Names (T);
17091 -- Process end label if there is one
17093 if Present (Def) then
17094 Process_End_Label (Def, 'e', T);
17096 end Enumeration_Type_Declaration;
17098 ---------------------------------
17099 -- Expand_To_Stored_Constraint --
17100 ---------------------------------
17102 function Expand_To_Stored_Constraint
17104 Constraint : Elist_Id) return Elist_Id
17106 Explicitly_Discriminated_Type : Entity_Id;
17107 Expansion : Elist_Id;
17108 Discriminant : Entity_Id;
17110 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17111 -- Find the nearest type that actually specifies discriminants
17113 ---------------------------------
17114 -- Type_With_Explicit_Discrims --
17115 ---------------------------------
17117 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17118 Typ : constant E := Base_Type (Id);
17121 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17122 if Present (Full_View (Typ)) then
17123 return Type_With_Explicit_Discrims (Full_View (Typ));
17127 if Has_Discriminants (Typ) then
17132 if Etype (Typ) = Typ then
17134 elsif Has_Discriminants (Typ) then
17137 return Type_With_Explicit_Discrims (Etype (Typ));
17140 end Type_With_Explicit_Discrims;
17142 -- Start of processing for Expand_To_Stored_Constraint
17145 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17149 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17151 if No (Explicitly_Discriminated_Type) then
17155 Expansion := New_Elmt_List;
17158 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17159 while Present (Discriminant) loop
17161 (Get_Discriminant_Value
17162 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17164 Next_Stored_Discriminant (Discriminant);
17168 end Expand_To_Stored_Constraint;
17170 ---------------------------
17171 -- Find_Hidden_Interface --
17172 ---------------------------
17174 function Find_Hidden_Interface
17176 Dest : Elist_Id) return Entity_Id
17179 Iface_Elmt : Elmt_Id;
17182 if Present (Src) and then Present (Dest) then
17183 Iface_Elmt := First_Elmt (Src);
17184 while Present (Iface_Elmt) loop
17185 Iface := Node (Iface_Elmt);
17187 if Is_Interface (Iface)
17188 and then not Contain_Interface (Iface, Dest)
17193 Next_Elmt (Iface_Elmt);
17198 end Find_Hidden_Interface;
17200 --------------------
17201 -- Find_Type_Name --
17202 --------------------
17204 function Find_Type_Name (N : Node_Id) return Entity_Id is
17205 Id : constant Entity_Id := Defining_Identifier (N);
17206 New_Id : Entity_Id;
17208 Prev_Par : Node_Id;
17210 procedure Check_Duplicate_Aspects;
17211 -- Check that aspects specified in a completion have not been specified
17212 -- already in the partial view.
17214 procedure Tag_Mismatch;
17215 -- Diagnose a tagged partial view whose full view is untagged. We post
17216 -- the message on the full view, with a reference to the previous
17217 -- partial view. The partial view can be private or incomplete, and
17218 -- these are handled in a different manner, so we determine the position
17219 -- of the error message from the respective slocs of both.
17221 -----------------------------
17222 -- Check_Duplicate_Aspects --
17223 -----------------------------
17225 procedure Check_Duplicate_Aspects is
17226 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17227 -- Return the corresponding aspect of the partial view which matches
17228 -- the aspect id of Asp. Return Empty is no such aspect exists.
17230 -----------------------------
17231 -- Get_Partial_View_Aspect --
17232 -----------------------------
17234 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17235 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17236 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17237 Prev_Asp : Node_Id;
17240 if Present (Prev_Asps) then
17241 Prev_Asp := First (Prev_Asps);
17242 while Present (Prev_Asp) loop
17243 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17252 end Get_Partial_View_Aspect;
17256 Full_Asps : constant List_Id := Aspect_Specifications (N);
17257 Full_Asp : Node_Id;
17258 Part_Asp : Node_Id;
17260 -- Start of processing for Check_Duplicate_Aspects
17263 if Present (Full_Asps) then
17264 Full_Asp := First (Full_Asps);
17265 while Present (Full_Asp) loop
17266 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17268 -- An aspect and its class-wide counterpart are two distinct
17269 -- aspects and may apply to both views of an entity.
17271 if Present (Part_Asp)
17272 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17275 ("aspect already specified in private declaration",
17282 if Has_Discriminants (Prev)
17283 and then not Has_Unknown_Discriminants (Prev)
17284 and then Get_Aspect_Id (Full_Asp) =
17285 Aspect_Implicit_Dereference
17288 ("cannot specify aspect if partial view has known "
17289 & "discriminants", Full_Asp);
17295 end Check_Duplicate_Aspects;
17301 procedure Tag_Mismatch is
17303 if Sloc (Prev) < Sloc (Id) then
17304 if Ada_Version >= Ada_2012
17305 and then Nkind (N) = N_Private_Type_Declaration
17308 ("declaration of private } must be a tagged type ", Id, Prev);
17311 ("full declaration of } must be a tagged type ", Id, Prev);
17315 if Ada_Version >= Ada_2012
17316 and then Nkind (N) = N_Private_Type_Declaration
17319 ("declaration of private } must be a tagged type ", Prev, Id);
17322 ("full declaration of } must be a tagged type ", Prev, Id);
17327 -- Start of processing for Find_Type_Name
17330 -- Find incomplete declaration, if one was given
17332 Prev := Current_Entity_In_Scope (Id);
17334 -- New type declaration
17340 -- Previous declaration exists
17343 Prev_Par := Parent (Prev);
17345 -- Error if not incomplete/private case except if previous
17346 -- declaration is implicit, etc. Enter_Name will emit error if
17349 if not Is_Incomplete_Or_Private_Type (Prev) then
17353 -- Check invalid completion of private or incomplete type
17355 elsif not Nkind_In (N, N_Full_Type_Declaration,
17356 N_Task_Type_Declaration,
17357 N_Protected_Type_Declaration)
17359 (Ada_Version < Ada_2012
17360 or else not Is_Incomplete_Type (Prev)
17361 or else not Nkind_In (N, N_Private_Type_Declaration,
17362 N_Private_Extension_Declaration))
17364 -- Completion must be a full type declarations (RM 7.3(4))
17366 Error_Msg_Sloc := Sloc (Prev);
17367 Error_Msg_NE ("invalid completion of }", Id, Prev);
17369 -- Set scope of Id to avoid cascaded errors. Entity is never
17370 -- examined again, except when saving globals in generics.
17372 Set_Scope (Id, Current_Scope);
17375 -- If this is a repeated incomplete declaration, no further
17376 -- checks are possible.
17378 if Nkind (N) = N_Incomplete_Type_Declaration then
17382 -- Case of full declaration of incomplete type
17384 elsif Ekind (Prev) = E_Incomplete_Type
17385 and then (Ada_Version < Ada_2012
17386 or else No (Full_View (Prev))
17387 or else not Is_Private_Type (Full_View (Prev)))
17389 -- Indicate that the incomplete declaration has a matching full
17390 -- declaration. The defining occurrence of the incomplete
17391 -- declaration remains the visible one, and the procedure
17392 -- Get_Full_View dereferences it whenever the type is used.
17394 if Present (Full_View (Prev)) then
17395 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17398 Set_Full_View (Prev, Id);
17399 Append_Entity (Id, Current_Scope);
17400 Set_Is_Public (Id, Is_Public (Prev));
17401 Set_Is_Internal (Id);
17404 -- If the incomplete view is tagged, a class_wide type has been
17405 -- created already. Use it for the private type as well, in order
17406 -- to prevent multiple incompatible class-wide types that may be
17407 -- created for self-referential anonymous access components.
17409 if Is_Tagged_Type (Prev)
17410 and then Present (Class_Wide_Type (Prev))
17412 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17413 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17415 -- Type of the class-wide type is the current Id. Previously
17416 -- this was not done for private declarations because of order-
17417 -- of-elaboration issues in the back end, but gigi now handles
17420 Set_Etype (Class_Wide_Type (Id), Id);
17423 -- Case of full declaration of private type
17426 -- If the private type was a completion of an incomplete type then
17427 -- update Prev to reference the private type
17429 if Ada_Version >= Ada_2012
17430 and then Ekind (Prev) = E_Incomplete_Type
17431 and then Present (Full_View (Prev))
17432 and then Is_Private_Type (Full_View (Prev))
17434 Prev := Full_View (Prev);
17435 Prev_Par := Parent (Prev);
17438 if Nkind (N) = N_Full_Type_Declaration
17440 (Type_Definition (N), N_Record_Definition,
17441 N_Derived_Type_Definition)
17442 and then Interface_Present (Type_Definition (N))
17445 ("completion of private type cannot be an interface", N);
17448 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17449 if Etype (Prev) /= Prev then
17451 -- Prev is a private subtype or a derived type, and needs
17454 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17457 elsif Ekind (Prev) = E_Private_Type
17458 and then Nkind_In (N, N_Task_Type_Declaration,
17459 N_Protected_Type_Declaration)
17462 ("completion of nonlimited type cannot be limited", N);
17464 elsif Ekind (Prev) = E_Record_Type_With_Private
17465 and then Nkind_In (N, N_Task_Type_Declaration,
17466 N_Protected_Type_Declaration)
17468 if not Is_Limited_Record (Prev) then
17470 ("completion of nonlimited type cannot be limited", N);
17472 elsif No (Interface_List (N)) then
17474 ("completion of tagged private type must be tagged",
17479 -- Ada 2005 (AI-251): Private extension declaration of a task
17480 -- type or a protected type. This case arises when covering
17481 -- interface types.
17483 elsif Nkind_In (N, N_Task_Type_Declaration,
17484 N_Protected_Type_Declaration)
17488 elsif Nkind (N) /= N_Full_Type_Declaration
17489 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17492 ("full view of private extension must be an extension", N);
17494 elsif not (Abstract_Present (Parent (Prev)))
17495 and then Abstract_Present (Type_Definition (N))
17498 ("full view of non-abstract extension cannot be abstract", N);
17501 if not In_Private_Part (Current_Scope) then
17503 ("declaration of full view must appear in private part", N);
17506 if Ada_Version >= Ada_2012 then
17507 Check_Duplicate_Aspects;
17510 Copy_And_Swap (Prev, Id);
17511 Set_Has_Private_Declaration (Prev);
17512 Set_Has_Private_Declaration (Id);
17514 -- AI12-0133: Indicate whether we have a partial view with
17515 -- unknown discriminants, in which case initialization of objects
17516 -- of the type do not receive an invariant check.
17518 Set_Partial_View_Has_Unknown_Discr
17519 (Prev, Has_Unknown_Discriminants (Id));
17521 -- Preserve aspect and iterator flags that may have been set on
17522 -- the partial view.
17524 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17525 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17527 -- If no error, propagate freeze_node from private to full view.
17528 -- It may have been generated for an early operational item.
17530 if Present (Freeze_Node (Id))
17531 and then Serious_Errors_Detected = 0
17532 and then No (Full_View (Id))
17534 Set_Freeze_Node (Prev, Freeze_Node (Id));
17535 Set_Freeze_Node (Id, Empty);
17536 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17539 Set_Full_View (Id, Prev);
17543 -- Verify that full declaration conforms to partial one
17545 if Is_Incomplete_Or_Private_Type (Prev)
17546 and then Present (Discriminant_Specifications (Prev_Par))
17548 if Present (Discriminant_Specifications (N)) then
17549 if Ekind (Prev) = E_Incomplete_Type then
17550 Check_Discriminant_Conformance (N, Prev, Prev);
17552 Check_Discriminant_Conformance (N, Prev, Id);
17557 ("missing discriminants in full type declaration", N);
17559 -- To avoid cascaded errors on subsequent use, share the
17560 -- discriminants of the partial view.
17562 Set_Discriminant_Specifications (N,
17563 Discriminant_Specifications (Prev_Par));
17567 -- A prior untagged partial view can have an associated class-wide
17568 -- type due to use of the class attribute, and in this case the full
17569 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17570 -- of incomplete tagged declarations, but we check for it.
17573 and then (Is_Tagged_Type (Prev)
17574 or else Present (Class_Wide_Type (Prev)))
17576 -- Ada 2012 (AI05-0162): A private type may be the completion of
17577 -- an incomplete type.
17579 if Ada_Version >= Ada_2012
17580 and then Is_Incomplete_Type (Prev)
17581 and then Nkind_In (N, N_Private_Type_Declaration,
17582 N_Private_Extension_Declaration)
17584 -- No need to check private extensions since they are tagged
17586 if Nkind (N) = N_Private_Type_Declaration
17587 and then not Tagged_Present (N)
17592 -- The full declaration is either a tagged type (including
17593 -- a synchronized type that implements interfaces) or a
17594 -- type extension, otherwise this is an error.
17596 elsif Nkind_In (N, N_Task_Type_Declaration,
17597 N_Protected_Type_Declaration)
17599 if No (Interface_List (N)) and then not Error_Posted (N) then
17603 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17605 -- Indicate that the previous declaration (tagged incomplete
17606 -- or private declaration) requires the same on the full one.
17608 if not Tagged_Present (Type_Definition (N)) then
17610 Set_Is_Tagged_Type (Id);
17613 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17614 if No (Record_Extension_Part (Type_Definition (N))) then
17616 ("full declaration of } must be a record extension",
17619 -- Set some attributes to produce a usable full view
17621 Set_Is_Tagged_Type (Id);
17630 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17631 and then Present (Premature_Use (Parent (Prev)))
17633 Error_Msg_Sloc := Sloc (N);
17635 ("\full declaration #", Premature_Use (Parent (Prev)));
17640 end Find_Type_Name;
17642 -------------------------
17643 -- Find_Type_Of_Object --
17644 -------------------------
17646 function Find_Type_Of_Object
17647 (Obj_Def : Node_Id;
17648 Related_Nod : Node_Id) return Entity_Id
17650 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17651 P : Node_Id := Parent (Obj_Def);
17656 -- If the parent is a component_definition node we climb to the
17657 -- component_declaration node
17659 if Nkind (P) = N_Component_Definition then
17663 -- Case of an anonymous array subtype
17665 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17666 N_Unconstrained_Array_Definition)
17669 Array_Type_Declaration (T, Obj_Def);
17671 -- Create an explicit subtype whenever possible
17673 elsif Nkind (P) /= N_Component_Declaration
17674 and then Def_Kind = N_Subtype_Indication
17676 -- Base name of subtype on object name, which will be unique in
17677 -- the current scope.
17679 -- If this is a duplicate declaration, return base type, to avoid
17680 -- generating duplicate anonymous types.
17682 if Error_Posted (P) then
17683 Analyze (Subtype_Mark (Obj_Def));
17684 return Entity (Subtype_Mark (Obj_Def));
17689 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17691 T := Make_Defining_Identifier (Sloc (P), Nam);
17693 Insert_Action (Obj_Def,
17694 Make_Subtype_Declaration (Sloc (P),
17695 Defining_Identifier => T,
17696 Subtype_Indication => Relocate_Node (Obj_Def)));
17698 -- This subtype may need freezing, and this will not be done
17699 -- automatically if the object declaration is not in declarative
17700 -- part. Since this is an object declaration, the type cannot always
17701 -- be frozen here. Deferred constants do not freeze their type
17702 -- (which often enough will be private).
17704 if Nkind (P) = N_Object_Declaration
17705 and then Constant_Present (P)
17706 and then No (Expression (P))
17710 -- Here we freeze the base type of object type to catch premature use
17711 -- of discriminated private type without a full view.
17714 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17717 -- Ada 2005 AI-406: the object definition in an object declaration
17718 -- can be an access definition.
17720 elsif Def_Kind = N_Access_Definition then
17721 T := Access_Definition (Related_Nod, Obj_Def);
17723 Set_Is_Local_Anonymous_Access
17725 V => (Ada_Version < Ada_2012)
17726 or else (Nkind (P) /= N_Object_Declaration)
17727 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17729 -- Otherwise, the object definition is just a subtype_mark
17732 T := Process_Subtype (Obj_Def, Related_Nod);
17736 end Find_Type_Of_Object;
17738 --------------------------------
17739 -- Find_Type_Of_Subtype_Indic --
17740 --------------------------------
17742 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17746 -- Case of subtype mark with a constraint
17748 if Nkind (S) = N_Subtype_Indication then
17749 Find_Type (Subtype_Mark (S));
17750 Typ := Entity (Subtype_Mark (S));
17753 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17756 ("incorrect constraint for this kind of type", Constraint (S));
17757 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17760 -- Otherwise we have a subtype mark without a constraint
17762 elsif Error_Posted (S) then
17763 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17771 -- Check No_Wide_Characters restriction
17773 Check_Wide_Character_Restriction (Typ, S);
17776 end Find_Type_Of_Subtype_Indic;
17778 -------------------------------------
17779 -- Floating_Point_Type_Declaration --
17780 -------------------------------------
17782 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17783 Digs : constant Node_Id := Digits_Expression (Def);
17784 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17786 Base_Typ : Entity_Id;
17787 Implicit_Base : Entity_Id;
17789 function Can_Derive_From (E : Entity_Id) return Boolean;
17790 -- Find if given digits value, and possibly a specified range, allows
17791 -- derivation from specified type
17793 procedure Convert_Bound (B : Node_Id);
17794 -- If specified, the bounds must be static but may be of different
17795 -- types. They must be converted into machine numbers of the base type,
17796 -- in accordance with RM 4.9(38).
17798 function Find_Base_Type return Entity_Id;
17799 -- Find a predefined base type that Def can derive from, or generate
17800 -- an error and substitute Long_Long_Float if none exists.
17802 ---------------------
17803 -- Can_Derive_From --
17804 ---------------------
17806 function Can_Derive_From (E : Entity_Id) return Boolean is
17807 Spec : constant Entity_Id := Real_Range_Specification (Def);
17810 -- Check specified "digits" constraint
17812 if Digs_Val > Digits_Value (E) then
17816 -- Check for matching range, if specified
17818 if Present (Spec) then
17819 if Expr_Value_R (Type_Low_Bound (E)) >
17820 Expr_Value_R (Low_Bound (Spec))
17825 if Expr_Value_R (Type_High_Bound (E)) <
17826 Expr_Value_R (High_Bound (Spec))
17833 end Can_Derive_From;
17835 -------------------
17836 -- Convert_Bound --
17837 --------------------
17839 procedure Convert_Bound (B : Node_Id) is
17841 -- If the bound is not a literal it can only be static if it is
17842 -- a static constant, possibly of a specified type.
17844 if Is_Entity_Name (B)
17845 and then Ekind (Entity (B)) = E_Constant
17847 Rewrite (B, Constant_Value (Entity (B)));
17850 if Nkind (B) = N_Real_Literal then
17851 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
17852 Set_Is_Machine_Number (B);
17853 Set_Etype (B, Base_Typ);
17857 --------------------
17858 -- Find_Base_Type --
17859 --------------------
17861 function Find_Base_Type return Entity_Id is
17862 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17865 -- Iterate over the predefined types in order, returning the first
17866 -- one that Def can derive from.
17868 while Present (Choice) loop
17869 if Can_Derive_From (Node (Choice)) then
17870 return Node (Choice);
17873 Next_Elmt (Choice);
17876 -- If we can't derive from any existing type, use Long_Long_Float
17877 -- and give appropriate message explaining the problem.
17879 if Digs_Val > Max_Digs_Val then
17880 -- It might be the case that there is a type with the requested
17881 -- range, just not the combination of digits and range.
17884 ("no predefined type has requested range and precision",
17885 Real_Range_Specification (Def));
17889 ("range too large for any predefined type",
17890 Real_Range_Specification (Def));
17893 return Standard_Long_Long_Float;
17894 end Find_Base_Type;
17896 -- Start of processing for Floating_Point_Type_Declaration
17899 Check_Restriction (No_Floating_Point, Def);
17901 -- Create an implicit base type
17904 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17906 -- Analyze and verify digits value
17908 Analyze_And_Resolve (Digs, Any_Integer);
17909 Check_Digits_Expression (Digs);
17910 Digs_Val := Expr_Value (Digs);
17912 -- Process possible range spec and find correct type to derive from
17914 Process_Real_Range_Specification (Def);
17916 -- Check that requested number of digits is not too high.
17918 if Digs_Val > Max_Digs_Val then
17920 -- The check for Max_Base_Digits may be somewhat expensive, as it
17921 -- requires reading System, so only do it when necessary.
17924 Max_Base_Digits : constant Uint :=
17927 (Parent (RTE (RE_Max_Base_Digits))));
17930 if Digs_Val > Max_Base_Digits then
17931 Error_Msg_Uint_1 := Max_Base_Digits;
17932 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17934 elsif No (Real_Range_Specification (Def)) then
17935 Error_Msg_Uint_1 := Max_Digs_Val;
17936 Error_Msg_N ("types with more than ^ digits need range spec "
17937 & "(RM 3.5.7(6))", Digs);
17942 -- Find a suitable type to derive from or complain and use a substitute
17944 Base_Typ := Find_Base_Type;
17946 -- If there are bounds given in the declaration use them as the bounds
17947 -- of the type, otherwise use the bounds of the predefined base type
17948 -- that was chosen based on the Digits value.
17950 if Present (Real_Range_Specification (Def)) then
17951 Set_Scalar_Range (T, Real_Range_Specification (Def));
17952 Set_Is_Constrained (T);
17954 Convert_Bound (Type_Low_Bound (T));
17955 Convert_Bound (Type_High_Bound (T));
17958 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17961 -- Complete definition of implicit base and declared first subtype. The
17962 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17963 -- are not clobbered when the floating point type acts as a full view of
17966 Set_Etype (Implicit_Base, Base_Typ);
17967 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17968 Set_Size_Info (Implicit_Base, Base_Typ);
17969 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17970 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17971 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17972 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17974 Set_Ekind (T, E_Floating_Point_Subtype);
17975 Set_Etype (T, Implicit_Base);
17976 Set_Size_Info (T, Implicit_Base);
17977 Set_RM_Size (T, RM_Size (Implicit_Base));
17978 Inherit_Rep_Item_Chain (T, Implicit_Base);
17979 Set_Digits_Value (T, Digs_Val);
17980 end Floating_Point_Type_Declaration;
17982 ----------------------------
17983 -- Get_Discriminant_Value --
17984 ----------------------------
17986 -- This is the situation:
17988 -- There is a non-derived type
17990 -- type T0 (Dx, Dy, Dz...)
17992 -- There are zero or more levels of derivation, with each derivation
17993 -- either purely inheriting the discriminants, or defining its own.
17995 -- type Ti is new Ti-1
17997 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17999 -- subtype Ti is ...
18001 -- The subtype issue is avoided by the use of Original_Record_Component,
18002 -- and the fact that derived subtypes also derive the constraints.
18004 -- This chain leads back from
18006 -- Typ_For_Constraint
18008 -- Typ_For_Constraint has discriminants, and the value for each
18009 -- discriminant is given by its corresponding Elmt of Constraints.
18011 -- Discriminant is some discriminant in this hierarchy
18013 -- We need to return its value
18015 -- We do this by recursively searching each level, and looking for
18016 -- Discriminant. Once we get to the bottom, we start backing up
18017 -- returning the value for it which may in turn be a discriminant
18018 -- further up, so on the backup we continue the substitution.
18020 function Get_Discriminant_Value
18021 (Discriminant : Entity_Id;
18022 Typ_For_Constraint : Entity_Id;
18023 Constraint : Elist_Id) return Node_Id
18025 function Root_Corresponding_Discriminant
18026 (Discr : Entity_Id) return Entity_Id;
18027 -- Given a discriminant, traverse the chain of inherited discriminants
18028 -- and return the topmost discriminant.
18030 function Search_Derivation_Levels
18032 Discrim_Values : Elist_Id;
18033 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18034 -- This is the routine that performs the recursive search of levels
18035 -- as described above.
18037 -------------------------------------
18038 -- Root_Corresponding_Discriminant --
18039 -------------------------------------
18041 function Root_Corresponding_Discriminant
18042 (Discr : Entity_Id) return Entity_Id
18048 while Present (Corresponding_Discriminant (D)) loop
18049 D := Corresponding_Discriminant (D);
18053 end Root_Corresponding_Discriminant;
18055 ------------------------------
18056 -- Search_Derivation_Levels --
18057 ------------------------------
18059 function Search_Derivation_Levels
18061 Discrim_Values : Elist_Id;
18062 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18066 Result : Node_Or_Entity_Id;
18067 Result_Entity : Node_Id;
18070 -- If inappropriate type, return Error, this happens only in
18071 -- cascaded error situations, and we want to avoid a blow up.
18073 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18077 -- Look deeper if possible. Use Stored_Constraints only for
18078 -- untagged types. For tagged types use the given constraint.
18079 -- This asymmetry needs explanation???
18081 if not Stored_Discrim_Values
18082 and then Present (Stored_Constraint (Ti))
18083 and then not Is_Tagged_Type (Ti)
18086 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18090 Td : Entity_Id := Etype (Ti);
18093 -- If the parent type is private, the full view may include
18094 -- renamed discriminants, and it is those stored values that
18095 -- may be needed (the partial view never has more information
18096 -- than the full view).
18098 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18099 Td := Full_View (Td);
18103 Result := Discriminant;
18106 if Present (Stored_Constraint (Ti)) then
18108 Search_Derivation_Levels
18109 (Td, Stored_Constraint (Ti), True);
18112 Search_Derivation_Levels
18113 (Td, Discrim_Values, Stored_Discrim_Values);
18119 -- Extra underlying places to search, if not found above. For
18120 -- concurrent types, the relevant discriminant appears in the
18121 -- corresponding record. For a type derived from a private type
18122 -- without discriminant, the full view inherits the discriminants
18123 -- of the full view of the parent.
18125 if Result = Discriminant then
18126 if Is_Concurrent_Type (Ti)
18127 and then Present (Corresponding_Record_Type (Ti))
18130 Search_Derivation_Levels (
18131 Corresponding_Record_Type (Ti),
18133 Stored_Discrim_Values);
18135 elsif Is_Private_Type (Ti)
18136 and then not Has_Discriminants (Ti)
18137 and then Present (Full_View (Ti))
18138 and then Etype (Full_View (Ti)) /= Ti
18141 Search_Derivation_Levels (
18144 Stored_Discrim_Values);
18148 -- If Result is not a (reference to a) discriminant, return it,
18149 -- otherwise set Result_Entity to the discriminant.
18151 if Nkind (Result) = N_Defining_Identifier then
18152 pragma Assert (Result = Discriminant);
18153 Result_Entity := Result;
18156 if not Denotes_Discriminant (Result) then
18160 Result_Entity := Entity (Result);
18163 -- See if this level of derivation actually has discriminants because
18164 -- tagged derivations can add them, hence the lower levels need not
18167 if not Has_Discriminants (Ti) then
18171 -- Scan Ti's discriminants for Result_Entity, and return its
18172 -- corresponding value, if any.
18174 Result_Entity := Original_Record_Component (Result_Entity);
18176 Assoc := First_Elmt (Discrim_Values);
18178 if Stored_Discrim_Values then
18179 Disc := First_Stored_Discriminant (Ti);
18181 Disc := First_Discriminant (Ti);
18184 while Present (Disc) loop
18186 -- If no further associations return the discriminant, value will
18187 -- be found on the second pass.
18193 if Original_Record_Component (Disc) = Result_Entity then
18194 return Node (Assoc);
18199 if Stored_Discrim_Values then
18200 Next_Stored_Discriminant (Disc);
18202 Next_Discriminant (Disc);
18206 -- Could not find it
18209 end Search_Derivation_Levels;
18213 Result : Node_Or_Entity_Id;
18215 -- Start of processing for Get_Discriminant_Value
18218 -- ??? This routine is a gigantic mess and will be deleted. For the
18219 -- time being just test for the trivial case before calling recurse.
18221 -- We are now celebrating the 20th anniversary of this comment!
18223 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18229 D := First_Discriminant (Typ_For_Constraint);
18230 E := First_Elmt (Constraint);
18231 while Present (D) loop
18232 if Chars (D) = Chars (Discriminant) then
18236 Next_Discriminant (D);
18242 Result := Search_Derivation_Levels
18243 (Typ_For_Constraint, Constraint, False);
18245 -- ??? hack to disappear when this routine is gone
18247 if Nkind (Result) = N_Defining_Identifier then
18253 D := First_Discriminant (Typ_For_Constraint);
18254 E := First_Elmt (Constraint);
18255 while Present (D) loop
18256 if Root_Corresponding_Discriminant (D) = Discriminant then
18260 Next_Discriminant (D);
18266 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18268 end Get_Discriminant_Value;
18270 --------------------------
18271 -- Has_Range_Constraint --
18272 --------------------------
18274 function Has_Range_Constraint (N : Node_Id) return Boolean is
18275 C : constant Node_Id := Constraint (N);
18278 if Nkind (C) = N_Range_Constraint then
18281 elsif Nkind (C) = N_Digits_Constraint then
18283 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18284 or else Present (Range_Constraint (C));
18286 elsif Nkind (C) = N_Delta_Constraint then
18287 return Present (Range_Constraint (C));
18292 end Has_Range_Constraint;
18294 ------------------------
18295 -- Inherit_Components --
18296 ------------------------
18298 function Inherit_Components
18300 Parent_Base : Entity_Id;
18301 Derived_Base : Entity_Id;
18302 Is_Tagged : Boolean;
18303 Inherit_Discr : Boolean;
18304 Discs : Elist_Id) return Elist_Id
18306 Assoc_List : constant Elist_Id := New_Elmt_List;
18308 procedure Inherit_Component
18309 (Old_C : Entity_Id;
18310 Plain_Discrim : Boolean := False;
18311 Stored_Discrim : Boolean := False);
18312 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18313 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18314 -- True, Old_C is a stored discriminant. If they are both false then
18315 -- Old_C is a regular component.
18317 -----------------------
18318 -- Inherit_Component --
18319 -----------------------
18321 procedure Inherit_Component
18322 (Old_C : Entity_Id;
18323 Plain_Discrim : Boolean := False;
18324 Stored_Discrim : Boolean := False)
18326 procedure Set_Anonymous_Type (Id : Entity_Id);
18327 -- Id denotes the entity of an access discriminant or anonymous
18328 -- access component. Set the type of Id to either the same type of
18329 -- Old_C or create a new one depending on whether the parent and
18330 -- the child types are in the same scope.
18332 ------------------------
18333 -- Set_Anonymous_Type --
18334 ------------------------
18336 procedure Set_Anonymous_Type (Id : Entity_Id) is
18337 Old_Typ : constant Entity_Id := Etype (Old_C);
18340 if Scope (Parent_Base) = Scope (Derived_Base) then
18341 Set_Etype (Id, Old_Typ);
18343 -- The parent and the derived type are in two different scopes.
18344 -- Reuse the type of the original discriminant / component by
18345 -- copying it in order to preserve all attributes.
18349 Typ : constant Entity_Id := New_Copy (Old_Typ);
18352 Set_Etype (Id, Typ);
18354 -- Since we do not generate component declarations for
18355 -- inherited components, associate the itype with the
18358 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18359 Set_Scope (Typ, Derived_Base);
18362 end Set_Anonymous_Type;
18364 -- Local variables and constants
18366 New_C : constant Entity_Id := New_Copy (Old_C);
18368 Corr_Discrim : Entity_Id;
18369 Discrim : Entity_Id;
18371 -- Start of processing for Inherit_Component
18374 pragma Assert (not Is_Tagged or not Stored_Discrim);
18376 Set_Parent (New_C, Parent (Old_C));
18378 -- Regular discriminants and components must be inserted in the scope
18379 -- of the Derived_Base. Do it here.
18381 if not Stored_Discrim then
18382 Enter_Name (New_C);
18385 -- For tagged types the Original_Record_Component must point to
18386 -- whatever this field was pointing to in the parent type. This has
18387 -- already been achieved by the call to New_Copy above.
18389 if not Is_Tagged then
18390 Set_Original_Record_Component (New_C, New_C);
18391 Set_Corresponding_Record_Component (New_C, Old_C);
18394 -- Set the proper type of an access discriminant
18396 if Ekind (New_C) = E_Discriminant
18397 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18399 Set_Anonymous_Type (New_C);
18402 -- If we have inherited a component then see if its Etype contains
18403 -- references to Parent_Base discriminants. In this case, replace
18404 -- these references with the constraints given in Discs. We do not
18405 -- do this for the partial view of private types because this is
18406 -- not needed (only the components of the full view will be used
18407 -- for code generation) and cause problem. We also avoid this
18408 -- transformation in some error situations.
18410 if Ekind (New_C) = E_Component then
18412 -- Set the proper type of an anonymous access component
18414 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18415 Set_Anonymous_Type (New_C);
18417 elsif (Is_Private_Type (Derived_Base)
18418 and then not Is_Generic_Type (Derived_Base))
18419 or else (Is_Empty_Elmt_List (Discs)
18420 and then not Expander_Active)
18422 Set_Etype (New_C, Etype (Old_C));
18425 -- The current component introduces a circularity of the
18428 -- limited with Pack_2;
18429 -- package Pack_1 is
18430 -- type T_1 is tagged record
18431 -- Comp : access Pack_2.T_2;
18437 -- package Pack_2 is
18438 -- type T_2 is new Pack_1.T_1 with ...;
18443 Constrain_Component_Type
18444 (Old_C, Derived_Base, N, Parent_Base, Discs));
18448 -- In derived tagged types it is illegal to reference a non
18449 -- discriminant component in the parent type. To catch this, mark
18450 -- these components with an Ekind of E_Void. This will be reset in
18451 -- Record_Type_Definition after processing the record extension of
18452 -- the derived type.
18454 -- If the declaration is a private extension, there is no further
18455 -- record extension to process, and the components retain their
18456 -- current kind, because they are visible at this point.
18458 if Is_Tagged and then Ekind (New_C) = E_Component
18459 and then Nkind (N) /= N_Private_Extension_Declaration
18461 Set_Ekind (New_C, E_Void);
18464 if Plain_Discrim then
18465 Set_Corresponding_Discriminant (New_C, Old_C);
18466 Build_Discriminal (New_C);
18468 -- If we are explicitly inheriting a stored discriminant it will be
18469 -- completely hidden.
18471 elsif Stored_Discrim then
18472 Set_Corresponding_Discriminant (New_C, Empty);
18473 Set_Discriminal (New_C, Empty);
18474 Set_Is_Completely_Hidden (New_C);
18476 -- Set the Original_Record_Component of each discriminant in the
18477 -- derived base to point to the corresponding stored that we just
18480 Discrim := First_Discriminant (Derived_Base);
18481 while Present (Discrim) loop
18482 Corr_Discrim := Corresponding_Discriminant (Discrim);
18484 -- Corr_Discrim could be missing in an error situation
18486 if Present (Corr_Discrim)
18487 and then Original_Record_Component (Corr_Discrim) = Old_C
18489 Set_Original_Record_Component (Discrim, New_C);
18490 Set_Corresponding_Record_Component (Discrim, Empty);
18493 Next_Discriminant (Discrim);
18496 Append_Entity (New_C, Derived_Base);
18499 if not Is_Tagged then
18500 Append_Elmt (Old_C, Assoc_List);
18501 Append_Elmt (New_C, Assoc_List);
18503 end Inherit_Component;
18505 -- Variables local to Inherit_Component
18507 Loc : constant Source_Ptr := Sloc (N);
18509 Parent_Discrim : Entity_Id;
18510 Stored_Discrim : Entity_Id;
18512 Component : Entity_Id;
18514 -- Start of processing for Inherit_Components
18517 if not Is_Tagged then
18518 Append_Elmt (Parent_Base, Assoc_List);
18519 Append_Elmt (Derived_Base, Assoc_List);
18522 -- Inherit parent discriminants if needed
18524 if Inherit_Discr then
18525 Parent_Discrim := First_Discriminant (Parent_Base);
18526 while Present (Parent_Discrim) loop
18527 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18528 Next_Discriminant (Parent_Discrim);
18532 -- Create explicit stored discrims for untagged types when necessary
18534 if not Has_Unknown_Discriminants (Derived_Base)
18535 and then Has_Discriminants (Parent_Base)
18536 and then not Is_Tagged
18539 or else First_Discriminant (Parent_Base) /=
18540 First_Stored_Discriminant (Parent_Base))
18542 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18543 while Present (Stored_Discrim) loop
18544 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18545 Next_Stored_Discriminant (Stored_Discrim);
18549 -- See if we can apply the second transformation for derived types, as
18550 -- explained in point 6. in the comments above Build_Derived_Record_Type
18551 -- This is achieved by appending Derived_Base discriminants into Discs,
18552 -- which has the side effect of returning a non empty Discs list to the
18553 -- caller of Inherit_Components, which is what we want. This must be
18554 -- done for private derived types if there are explicit stored
18555 -- discriminants, to ensure that we can retrieve the values of the
18556 -- constraints provided in the ancestors.
18559 and then Is_Empty_Elmt_List (Discs)
18560 and then Present (First_Discriminant (Derived_Base))
18562 (not Is_Private_Type (Derived_Base)
18563 or else Is_Completely_Hidden
18564 (First_Stored_Discriminant (Derived_Base))
18565 or else Is_Generic_Type (Derived_Base))
18567 D := First_Discriminant (Derived_Base);
18568 while Present (D) loop
18569 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18570 Next_Discriminant (D);
18574 -- Finally, inherit non-discriminant components unless they are not
18575 -- visible because defined or inherited from the full view of the
18576 -- parent. Don't inherit the _parent field of the parent type.
18578 Component := First_Entity (Parent_Base);
18579 while Present (Component) loop
18581 -- Ada 2005 (AI-251): Do not inherit components associated with
18582 -- secondary tags of the parent.
18584 if Ekind (Component) = E_Component
18585 and then Present (Related_Type (Component))
18589 elsif Ekind (Component) /= E_Component
18590 or else Chars (Component) = Name_uParent
18594 -- If the derived type is within the parent type's declarative
18595 -- region, then the components can still be inherited even though
18596 -- they aren't visible at this point. This can occur for cases
18597 -- such as within public child units where the components must
18598 -- become visible upon entering the child unit's private part.
18600 elsif not Is_Visible_Component (Component)
18601 and then not In_Open_Scopes (Scope (Parent_Base))
18605 elsif Ekind_In (Derived_Base, E_Private_Type,
18606 E_Limited_Private_Type)
18611 Inherit_Component (Component);
18614 Next_Entity (Component);
18617 -- For tagged derived types, inherited discriminants cannot be used in
18618 -- component declarations of the record extension part. To achieve this
18619 -- we mark the inherited discriminants as not visible.
18621 if Is_Tagged and then Inherit_Discr then
18622 D := First_Discriminant (Derived_Base);
18623 while Present (D) loop
18624 Set_Is_Immediately_Visible (D, False);
18625 Next_Discriminant (D);
18630 end Inherit_Components;
18632 ----------------------
18633 -- Is_EVF_Procedure --
18634 ----------------------
18636 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18637 Formal : Entity_Id;
18640 -- Examine the formals of an Extensions_Visible False procedure looking
18641 -- for a controlling OUT parameter.
18643 if Ekind (Subp) = E_Procedure
18644 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18646 Formal := First_Formal (Subp);
18647 while Present (Formal) loop
18648 if Ekind (Formal) = E_Out_Parameter
18649 and then Is_Controlling_Formal (Formal)
18654 Next_Formal (Formal);
18659 end Is_EVF_Procedure;
18661 -----------------------
18662 -- Is_Null_Extension --
18663 -----------------------
18665 function Is_Null_Extension (T : Entity_Id) return Boolean is
18666 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18667 Comp_List : Node_Id;
18671 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18672 or else not Is_Tagged_Type (T)
18673 or else Nkind (Type_Definition (Type_Decl)) /=
18674 N_Derived_Type_Definition
18675 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18681 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18683 if Present (Discriminant_Specifications (Type_Decl)) then
18686 elsif Present (Comp_List)
18687 and then Is_Non_Empty_List (Component_Items (Comp_List))
18689 Comp := First (Component_Items (Comp_List));
18691 -- Only user-defined components are relevant. The component list
18692 -- may also contain a parent component and internal components
18693 -- corresponding to secondary tags, but these do not determine
18694 -- whether this is a null extension.
18696 while Present (Comp) loop
18697 if Comes_From_Source (Comp) then
18709 end Is_Null_Extension;
18711 ------------------------------
18712 -- Is_Valid_Constraint_Kind --
18713 ------------------------------
18715 function Is_Valid_Constraint_Kind
18716 (T_Kind : Type_Kind;
18717 Constraint_Kind : Node_Kind) return Boolean
18721 when Enumeration_Kind
18724 return Constraint_Kind = N_Range_Constraint;
18726 when Decimal_Fixed_Point_Kind =>
18727 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18728 N_Range_Constraint);
18730 when Ordinary_Fixed_Point_Kind =>
18731 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18732 N_Range_Constraint);
18735 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18736 N_Range_Constraint);
18743 | E_Incomplete_Type
18747 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18750 return True; -- Error will be detected later
18752 end Is_Valid_Constraint_Kind;
18754 --------------------------
18755 -- Is_Visible_Component --
18756 --------------------------
18758 function Is_Visible_Component
18760 N : Node_Id := Empty) return Boolean
18762 Original_Comp : Entity_Id := Empty;
18763 Original_Type : Entity_Id;
18764 Type_Scope : Entity_Id;
18766 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18767 -- Check whether parent type of inherited component is declared locally,
18768 -- possibly within a nested package or instance. The current scope is
18769 -- the derived record itself.
18771 -------------------
18772 -- Is_Local_Type --
18773 -------------------
18775 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18779 Scop := Scope (Typ);
18780 while Present (Scop)
18781 and then Scop /= Standard_Standard
18783 if Scop = Scope (Current_Scope) then
18787 Scop := Scope (Scop);
18793 -- Start of processing for Is_Visible_Component
18796 if Ekind_In (C, E_Component, E_Discriminant) then
18797 Original_Comp := Original_Record_Component (C);
18800 if No (Original_Comp) then
18802 -- Premature usage, or previous error
18807 Original_Type := Scope (Original_Comp);
18808 Type_Scope := Scope (Base_Type (Scope (C)));
18811 -- This test only concerns tagged types
18813 if not Is_Tagged_Type (Original_Type) then
18815 -- Check if this is a renamed discriminant (hidden either by the
18816 -- derived type or by some ancestor), unless we are analyzing code
18817 -- generated by the expander since it may reference such components
18818 -- (for example see the expansion of Deep_Adjust).
18820 if Ekind (C) = E_Discriminant and then Present (N) then
18822 not Comes_From_Source (N)
18823 or else not Is_Completely_Hidden (C);
18828 -- If it is _Parent or _Tag, there is no visibility issue
18830 elsif not Comes_From_Source (Original_Comp) then
18833 -- Discriminants are visible unless the (private) type has unknown
18834 -- discriminants. If the discriminant reference is inserted for a
18835 -- discriminant check on a full view it is also visible.
18837 elsif Ekind (Original_Comp) = E_Discriminant
18839 (not Has_Unknown_Discriminants (Original_Type)
18840 or else (Present (N)
18841 and then Nkind (N) = N_Selected_Component
18842 and then Nkind (Prefix (N)) = N_Type_Conversion
18843 and then not Comes_From_Source (Prefix (N))))
18847 -- If the component has been declared in an ancestor which is currently
18848 -- a private type, then it is not visible. The same applies if the
18849 -- component's containing type is not in an open scope and the original
18850 -- component's enclosing type is a visible full view of a private type
18851 -- (which can occur in cases where an attempt is being made to reference
18852 -- a component in a sibling package that is inherited from a visible
18853 -- component of a type in an ancestor package; the component in the
18854 -- sibling package should not be visible even though the component it
18855 -- inherited from is visible). This does not apply however in the case
18856 -- where the scope of the type is a private child unit, or when the
18857 -- parent comes from a local package in which the ancestor is currently
18858 -- visible. The latter suppression of visibility is needed for cases
18859 -- that are tested in B730006.
18861 elsif Is_Private_Type (Original_Type)
18863 (not Is_Private_Descendant (Type_Scope)
18864 and then not In_Open_Scopes (Type_Scope)
18865 and then Has_Private_Declaration (Original_Type))
18867 -- If the type derives from an entity in a formal package, there
18868 -- are no additional visible components.
18870 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18871 N_Formal_Package_Declaration
18875 -- if we are not in the private part of the current package, there
18876 -- are no additional visible components.
18878 elsif Ekind (Scope (Current_Scope)) = E_Package
18879 and then not In_Private_Part (Scope (Current_Scope))
18884 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18885 and then In_Open_Scopes (Scope (Original_Type))
18886 and then Is_Local_Type (Type_Scope);
18889 -- There is another weird way in which a component may be invisible when
18890 -- the private and the full view are not derived from the same ancestor.
18891 -- Here is an example :
18893 -- type A1 is tagged record F1 : integer; end record;
18894 -- type A2 is new A1 with record F2 : integer; end record;
18895 -- type T is new A1 with private;
18897 -- type T is new A2 with null record;
18899 -- In this case, the full view of T inherits F1 and F2 but the private
18900 -- view inherits only F1
18904 Ancestor : Entity_Id := Scope (C);
18908 if Ancestor = Original_Type then
18911 -- The ancestor may have a partial view of the original type,
18912 -- but if the full view is in scope, as in a child body, the
18913 -- component is visible.
18915 elsif In_Private_Part (Scope (Original_Type))
18916 and then Full_View (Ancestor) = Original_Type
18920 elsif Ancestor = Etype (Ancestor) then
18922 -- No further ancestors to examine
18927 Ancestor := Etype (Ancestor);
18931 end Is_Visible_Component;
18933 --------------------------
18934 -- Make_Class_Wide_Type --
18935 --------------------------
18937 procedure Make_Class_Wide_Type (T : Entity_Id) is
18938 CW_Type : Entity_Id;
18940 Next_E : Entity_Id;
18941 Prev_E : Entity_Id;
18944 if Present (Class_Wide_Type (T)) then
18946 -- The class-wide type is a partially decorated entity created for a
18947 -- unanalyzed tagged type referenced through a limited with clause.
18948 -- When the tagged type is analyzed, its class-wide type needs to be
18949 -- redecorated. Note that we reuse the entity created by Decorate_
18950 -- Tagged_Type in order to preserve all links.
18952 if Materialize_Entity (Class_Wide_Type (T)) then
18953 CW_Type := Class_Wide_Type (T);
18954 Set_Materialize_Entity (CW_Type, False);
18956 -- The class wide type can have been defined by the partial view, in
18957 -- which case everything is already done.
18963 -- Default case, we need to create a new class-wide type
18967 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18970 -- Inherit root type characteristics
18972 CW_Name := Chars (CW_Type);
18973 Next_E := Next_Entity (CW_Type);
18974 Prev_E := Prev_Entity (CW_Type);
18975 Copy_Node (T, CW_Type);
18976 Set_Comes_From_Source (CW_Type, False);
18977 Set_Chars (CW_Type, CW_Name);
18978 Set_Parent (CW_Type, Parent (T));
18979 Set_Prev_Entity (CW_Type, Prev_E);
18980 Set_Next_Entity (CW_Type, Next_E);
18982 -- Ensure we have a new freeze node for the class-wide type. The partial
18983 -- view may have freeze action of its own, requiring a proper freeze
18984 -- node, and the same freeze node cannot be shared between the two
18987 Set_Has_Delayed_Freeze (CW_Type);
18988 Set_Freeze_Node (CW_Type, Empty);
18990 -- Customize the class-wide type: It has no prim. op., it cannot be
18991 -- abstract, its Etype points back to the specific root type, and it
18992 -- cannot have any invariants.
18994 Set_Ekind (CW_Type, E_Class_Wide_Type);
18995 Set_Is_Tagged_Type (CW_Type, True);
18996 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18997 Set_Is_Abstract_Type (CW_Type, False);
18998 Set_Is_Constrained (CW_Type, False);
18999 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19000 Set_Default_SSO (CW_Type);
19001 Set_Has_Inheritable_Invariants (CW_Type, False);
19002 Set_Has_Inherited_Invariants (CW_Type, False);
19003 Set_Has_Own_Invariants (CW_Type, False);
19005 if Ekind (T) = E_Class_Wide_Subtype then
19006 Set_Etype (CW_Type, Etype (Base_Type (T)));
19008 Set_Etype (CW_Type, T);
19011 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19013 -- If this is the class_wide type of a constrained subtype, it does
19014 -- not have discriminants.
19016 Set_Has_Discriminants (CW_Type,
19017 Has_Discriminants (T) and then not Is_Constrained (T));
19019 Set_Has_Unknown_Discriminants (CW_Type, True);
19020 Set_Class_Wide_Type (T, CW_Type);
19021 Set_Equivalent_Type (CW_Type, Empty);
19023 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19025 Set_Class_Wide_Type (CW_Type, CW_Type);
19026 end Make_Class_Wide_Type;
19032 procedure Make_Index
19034 Related_Nod : Node_Id;
19035 Related_Id : Entity_Id := Empty;
19036 Suffix_Index : Nat := 1)
19040 Def_Id : Entity_Id := Empty;
19041 Found : Boolean := False;
19044 -- For a discrete range used in a constrained array definition and
19045 -- defined by a range, an implicit conversion to the predefined type
19046 -- INTEGER is assumed if each bound is either a numeric literal, a named
19047 -- number, or an attribute, and the type of both bounds (prior to the
19048 -- implicit conversion) is the type universal_integer. Otherwise, both
19049 -- bounds must be of the same discrete type, other than universal
19050 -- integer; this type must be determinable independently of the
19051 -- context, but using the fact that the type must be discrete and that
19052 -- both bounds must have the same type.
19054 -- Character literals also have a universal type in the absence of
19055 -- of additional context, and are resolved to Standard_Character.
19057 if Nkind (N) = N_Range then
19059 -- The index is given by a range constraint. The bounds are known
19060 -- to be of a consistent type.
19062 if not Is_Overloaded (N) then
19065 -- For universal bounds, choose the specific predefined type
19067 if T = Universal_Integer then
19068 T := Standard_Integer;
19070 elsif T = Any_Character then
19071 Ambiguous_Character (Low_Bound (N));
19073 T := Standard_Character;
19076 -- The node may be overloaded because some user-defined operators
19077 -- are available, but if a universal interpretation exists it is
19078 -- also the selected one.
19080 elsif Universal_Interpretation (N) = Universal_Integer then
19081 T := Standard_Integer;
19087 Ind : Interp_Index;
19091 Get_First_Interp (N, Ind, It);
19092 while Present (It.Typ) loop
19093 if Is_Discrete_Type (It.Typ) then
19096 and then not Covers (It.Typ, T)
19097 and then not Covers (T, It.Typ)
19099 Error_Msg_N ("ambiguous bounds in discrete range", N);
19107 Get_Next_Interp (Ind, It);
19110 if T = Any_Type then
19111 Error_Msg_N ("discrete type required for range", N);
19112 Set_Etype (N, Any_Type);
19115 elsif T = Universal_Integer then
19116 T := Standard_Integer;
19121 if not Is_Discrete_Type (T) then
19122 Error_Msg_N ("discrete type required for range", N);
19123 Set_Etype (N, Any_Type);
19127 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19128 and then Attribute_Name (Low_Bound (N)) = Name_First
19129 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19130 and then Is_Type (Entity (Prefix (Low_Bound (N))))
19131 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19133 -- The type of the index will be the type of the prefix, as long
19134 -- as the upper bound is 'Last of the same type.
19136 Def_Id := Entity (Prefix (Low_Bound (N)));
19138 if Nkind (High_Bound (N)) /= N_Attribute_Reference
19139 or else Attribute_Name (High_Bound (N)) /= Name_Last
19140 or else not Is_Entity_Name (Prefix (High_Bound (N)))
19141 or else Entity (Prefix (High_Bound (N))) /= Def_Id
19148 Process_Range_Expr_In_Decl (R, T);
19150 elsif Nkind (N) = N_Subtype_Indication then
19152 -- The index is given by a subtype with a range constraint
19154 T := Base_Type (Entity (Subtype_Mark (N)));
19156 if not Is_Discrete_Type (T) then
19157 Error_Msg_N ("discrete type required for range", N);
19158 Set_Etype (N, Any_Type);
19162 R := Range_Expression (Constraint (N));
19165 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (N)));
19167 elsif Nkind (N) = N_Attribute_Reference then
19169 -- Catch beginner's error (use of attribute other than 'Range)
19171 if Attribute_Name (N) /= Name_Range then
19172 Error_Msg_N ("expect attribute ''Range", N);
19173 Set_Etype (N, Any_Type);
19177 -- If the node denotes the range of a type mark, that is also the
19178 -- resulting type, and we do not need to create an Itype for it.
19180 if Is_Entity_Name (Prefix (N))
19181 and then Comes_From_Source (N)
19182 and then Is_Type (Entity (Prefix (N)))
19183 and then Is_Discrete_Type (Entity (Prefix (N)))
19185 Def_Id := Entity (Prefix (N));
19188 Analyze_And_Resolve (N);
19192 -- If none of the above, must be a subtype. We convert this to a
19193 -- range attribute reference because in the case of declared first
19194 -- named subtypes, the types in the range reference can be different
19195 -- from the type of the entity. A range attribute normalizes the
19196 -- reference and obtains the correct types for the bounds.
19198 -- This transformation is in the nature of an expansion, is only
19199 -- done if expansion is active. In particular, it is not done on
19200 -- formal generic types, because we need to retain the name of the
19201 -- original index for instantiation purposes.
19204 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19205 Error_Msg_N ("invalid subtype mark in discrete range ", N);
19206 Set_Etype (N, Any_Integer);
19210 -- The type mark may be that of an incomplete type. It is only
19211 -- now that we can get the full view, previous analysis does
19212 -- not look specifically for a type mark.
19214 Set_Entity (N, Get_Full_View (Entity (N)));
19215 Set_Etype (N, Entity (N));
19216 Def_Id := Entity (N);
19218 if not Is_Discrete_Type (Def_Id) then
19219 Error_Msg_N ("discrete type required for index", N);
19220 Set_Etype (N, Any_Type);
19225 if Expander_Active then
19227 Make_Attribute_Reference (Sloc (N),
19228 Attribute_Name => Name_Range,
19229 Prefix => Relocate_Node (N)));
19231 -- The original was a subtype mark that does not freeze. This
19232 -- means that the rewritten version must not freeze either.
19234 Set_Must_Not_Freeze (N);
19235 Set_Must_Not_Freeze (Prefix (N));
19236 Analyze_And_Resolve (N);
19240 -- If expander is inactive, type is legal, nothing else to construct
19247 if not Is_Discrete_Type (T) then
19248 Error_Msg_N ("discrete type required for range", N);
19249 Set_Etype (N, Any_Type);
19252 elsif T = Any_Type then
19253 Set_Etype (N, Any_Type);
19257 -- We will now create the appropriate Itype to describe the range, but
19258 -- first a check. If we originally had a subtype, then we just label
19259 -- the range with this subtype. Not only is there no need to construct
19260 -- a new subtype, but it is wrong to do so for two reasons:
19262 -- 1. A legality concern, if we have a subtype, it must not freeze,
19263 -- and the Itype would cause freezing incorrectly
19265 -- 2. An efficiency concern, if we created an Itype, it would not be
19266 -- recognized as the same type for the purposes of eliminating
19267 -- checks in some circumstances.
19269 -- We signal this case by setting the subtype entity in Def_Id
19271 if No (Def_Id) then
19273 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19274 Set_Etype (Def_Id, Base_Type (T));
19276 if Is_Signed_Integer_Type (T) then
19277 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19279 elsif Is_Modular_Integer_Type (T) then
19280 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19283 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19284 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19285 Set_First_Literal (Def_Id, First_Literal (T));
19288 Set_Size_Info (Def_Id, (T));
19289 Set_RM_Size (Def_Id, RM_Size (T));
19290 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19292 Set_Scalar_Range (Def_Id, R);
19293 Conditional_Delay (Def_Id, T);
19295 if Nkind (N) = N_Subtype_Indication then
19296 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19299 -- In the subtype indication case, if the immediate parent of the
19300 -- new subtype is nonstatic, then the subtype we create is nonstatic,
19301 -- even if its bounds are static.
19303 if Nkind (N) = N_Subtype_Indication
19304 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19306 Set_Is_Non_Static_Subtype (Def_Id);
19310 -- Final step is to label the index with this constructed type
19312 Set_Etype (N, Def_Id);
19315 ------------------------------
19316 -- Modular_Type_Declaration --
19317 ------------------------------
19319 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19320 Mod_Expr : constant Node_Id := Expression (Def);
19323 procedure Set_Modular_Size (Bits : Int);
19324 -- Sets RM_Size to Bits, and Esize to normal word size above this
19326 ----------------------
19327 -- Set_Modular_Size --
19328 ----------------------
19330 procedure Set_Modular_Size (Bits : Int) is
19332 Set_RM_Size (T, UI_From_Int (Bits));
19337 elsif Bits <= 16 then
19338 Init_Esize (T, 16);
19340 elsif Bits <= 32 then
19341 Init_Esize (T, 32);
19344 Init_Esize (T, System_Max_Binary_Modulus_Power);
19347 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19348 Set_Is_Known_Valid (T);
19350 end Set_Modular_Size;
19352 -- Start of processing for Modular_Type_Declaration
19355 -- If the mod expression is (exactly) 2 * literal, where literal is
19356 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19358 if Warn_On_Suspicious_Modulus_Value
19359 and then Nkind (Mod_Expr) = N_Op_Multiply
19360 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19361 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19362 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19363 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19366 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19369 -- Proceed with analysis of mod expression
19371 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19373 Set_Ekind (T, E_Modular_Integer_Type);
19374 Init_Alignment (T);
19375 Set_Is_Constrained (T);
19377 if not Is_OK_Static_Expression (Mod_Expr) then
19378 Flag_Non_Static_Expr
19379 ("non-static expression used for modular type bound!", Mod_Expr);
19380 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19382 M_Val := Expr_Value (Mod_Expr);
19386 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19387 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19390 if M_Val > 2 ** Standard_Long_Integer_Size then
19391 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19394 Set_Modulus (T, M_Val);
19396 -- Create bounds for the modular type based on the modulus given in
19397 -- the type declaration and then analyze and resolve those bounds.
19399 Set_Scalar_Range (T,
19400 Make_Range (Sloc (Mod_Expr),
19401 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19402 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19404 -- Properly analyze the literals for the range. We do this manually
19405 -- because we can't go calling Resolve, since we are resolving these
19406 -- bounds with the type, and this type is certainly not complete yet.
19408 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19409 Set_Etype (High_Bound (Scalar_Range (T)), T);
19410 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19411 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19413 -- Loop through powers of two to find number of bits required
19415 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19419 if M_Val = 2 ** Bits then
19420 Set_Modular_Size (Bits);
19425 elsif M_Val < 2 ** Bits then
19426 Set_Non_Binary_Modulus (T);
19428 if Bits > System_Max_Nonbinary_Modulus_Power then
19429 Error_Msg_Uint_1 :=
19430 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19432 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19433 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19437 -- In the nonbinary case, set size as per RM 13.3(55)
19439 Set_Modular_Size (Bits);
19446 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19447 -- so we just signal an error and set the maximum size.
19449 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19450 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19452 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19453 Init_Alignment (T);
19455 end Modular_Type_Declaration;
19457 --------------------------
19458 -- New_Concatenation_Op --
19459 --------------------------
19461 procedure New_Concatenation_Op (Typ : Entity_Id) is
19462 Loc : constant Source_Ptr := Sloc (Typ);
19465 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19466 -- Create abbreviated declaration for the formal of a predefined
19467 -- Operator 'Op' of type 'Typ'
19469 --------------------
19470 -- Make_Op_Formal --
19471 --------------------
19473 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19474 Formal : Entity_Id;
19476 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19477 Set_Etype (Formal, Typ);
19478 Set_Mechanism (Formal, Default_Mechanism);
19480 end Make_Op_Formal;
19482 -- Start of processing for New_Concatenation_Op
19485 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19487 Set_Ekind (Op, E_Operator);
19488 Set_Scope (Op, Current_Scope);
19489 Set_Etype (Op, Typ);
19490 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19491 Set_Is_Immediately_Visible (Op);
19492 Set_Is_Intrinsic_Subprogram (Op);
19493 Set_Has_Completion (Op);
19494 Append_Entity (Op, Current_Scope);
19496 Set_Name_Entity_Id (Name_Op_Concat, Op);
19498 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19499 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19500 end New_Concatenation_Op;
19502 -------------------------
19503 -- OK_For_Limited_Init --
19504 -------------------------
19506 -- ???Check all calls of this, and compare the conditions under which it's
19509 function OK_For_Limited_Init
19511 Exp : Node_Id) return Boolean
19514 return Is_CPP_Constructor_Call (Exp)
19515 or else (Ada_Version >= Ada_2005
19516 and then not Debug_Flag_Dot_L
19517 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19518 end OK_For_Limited_Init;
19520 -------------------------------
19521 -- OK_For_Limited_Init_In_05 --
19522 -------------------------------
19524 function OK_For_Limited_Init_In_05
19526 Exp : Node_Id) return Boolean
19529 -- An object of a limited interface type can be initialized with any
19530 -- expression of a nonlimited descendant type. However this does not
19531 -- apply if this is a view conversion of some other expression. This
19532 -- is checked below.
19534 if Is_Class_Wide_Type (Typ)
19535 and then Is_Limited_Interface (Typ)
19536 and then not Is_Limited_Type (Etype (Exp))
19537 and then Nkind (Exp) /= N_Type_Conversion
19542 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19543 -- case of limited aggregates (including extension aggregates), and
19544 -- function calls. The function call may have been given in prefixed
19545 -- notation, in which case the original node is an indexed component.
19546 -- If the function is parameterless, the original node was an explicit
19547 -- dereference. The function may also be parameterless, in which case
19548 -- the source node is just an identifier.
19550 -- A branch of a conditional expression may have been removed if the
19551 -- condition is statically known. This happens during expansion, and
19552 -- thus will not happen if previous errors were encountered. The check
19553 -- will have been performed on the chosen branch, which replaces the
19554 -- original conditional expression.
19560 case Nkind (Original_Node (Exp)) is
19562 | N_Extension_Aggregate
19568 when N_Identifier =>
19569 return Present (Entity (Original_Node (Exp)))
19570 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19572 when N_Qualified_Expression =>
19574 OK_For_Limited_Init_In_05
19575 (Typ, Expression (Original_Node (Exp)));
19577 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19578 -- with a function call, the expander has rewritten the call into an
19579 -- N_Type_Conversion node to force displacement of the pointer to
19580 -- reference the component containing the secondary dispatch table.
19581 -- Otherwise a type conversion is not a legal context.
19582 -- A return statement for a build-in-place function returning a
19583 -- synchronized type also introduces an unchecked conversion.
19585 when N_Type_Conversion
19586 | N_Unchecked_Type_Conversion
19588 return not Comes_From_Source (Exp)
19590 -- If the conversion has been rewritten, check Original_Node
19592 ((Original_Node (Exp) /= Exp
19594 OK_For_Limited_Init_In_05 (Typ, Original_Node (Exp)))
19596 -- Otherwise, check the expression of the compiler-generated
19597 -- conversion (which is a conversion that we want to ignore
19598 -- for purposes of the limited-initialization restrictions).
19601 (Original_Node (Exp) = Exp
19603 OK_For_Limited_Init_In_05 (Typ, Expression (Exp))));
19605 when N_Explicit_Dereference
19606 | N_Indexed_Component
19607 | N_Selected_Component
19609 return Nkind (Exp) = N_Function_Call;
19611 -- A use of 'Input is a function call, hence allowed. Normally the
19612 -- attribute will be changed to a call, but the attribute by itself
19613 -- can occur with -gnatc.
19615 when N_Attribute_Reference =>
19616 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19618 -- "return raise ..." is OK
19620 when N_Raise_Expression =>
19623 -- For a case expression, all dependent expressions must be legal
19625 when N_Case_Expression =>
19630 Alt := First (Alternatives (Original_Node (Exp)));
19631 while Present (Alt) loop
19632 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19642 -- For an if expression, all dependent expressions must be legal
19644 when N_If_Expression =>
19646 Then_Expr : constant Node_Id :=
19647 Next (First (Expressions (Original_Node (Exp))));
19648 Else_Expr : constant Node_Id := Next (Then_Expr);
19650 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19652 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19658 end OK_For_Limited_Init_In_05;
19660 -------------------------------------------
19661 -- Ordinary_Fixed_Point_Type_Declaration --
19662 -------------------------------------------
19664 procedure Ordinary_Fixed_Point_Type_Declaration
19668 Loc : constant Source_Ptr := Sloc (Def);
19669 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19670 RRS : constant Node_Id := Real_Range_Specification (Def);
19671 Implicit_Base : Entity_Id;
19678 Check_Restriction (No_Fixed_Point, Def);
19680 -- Create implicit base type
19683 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19684 Set_Etype (Implicit_Base, Implicit_Base);
19686 -- Analyze and process delta expression
19688 Analyze_And_Resolve (Delta_Expr, Any_Real);
19690 Check_Delta_Expression (Delta_Expr);
19691 Delta_Val := Expr_Value_R (Delta_Expr);
19693 Set_Delta_Value (Implicit_Base, Delta_Val);
19695 -- Compute default small from given delta, which is the largest power
19696 -- of two that does not exceed the given delta value.
19706 if Delta_Val < Ureal_1 then
19707 while Delta_Val < Tmp loop
19708 Tmp := Tmp / Ureal_2;
19709 Scale := Scale + 1;
19714 Tmp := Tmp * Ureal_2;
19715 exit when Tmp > Delta_Val;
19716 Scale := Scale - 1;
19720 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19723 Set_Small_Value (Implicit_Base, Small_Val);
19725 -- If no range was given, set a dummy range
19727 if RRS <= Empty_Or_Error then
19728 Low_Val := -Small_Val;
19729 High_Val := Small_Val;
19731 -- Otherwise analyze and process given range
19735 Low : constant Node_Id := Low_Bound (RRS);
19736 High : constant Node_Id := High_Bound (RRS);
19739 Analyze_And_Resolve (Low, Any_Real);
19740 Analyze_And_Resolve (High, Any_Real);
19741 Check_Real_Bound (Low);
19742 Check_Real_Bound (High);
19744 -- Obtain and set the range
19746 Low_Val := Expr_Value_R (Low);
19747 High_Val := Expr_Value_R (High);
19749 if Low_Val > High_Val then
19750 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19755 -- The range for both the implicit base and the declared first subtype
19756 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19757 -- set a temporary range in place. Note that the bounds of the base
19758 -- type will be widened to be symmetrical and to fill the available
19759 -- bits when the type is frozen.
19761 -- We could do this with all discrete types, and probably should, but
19762 -- we absolutely have to do it for fixed-point, since the end-points
19763 -- of the range and the size are determined by the small value, which
19764 -- could be reset before the freeze point.
19766 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19767 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19769 -- Complete definition of first subtype. The inheritance of the rep item
19770 -- chain ensures that SPARK-related pragmas are not clobbered when the
19771 -- ordinary fixed point type acts as a full view of a private type.
19773 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19774 Set_Etype (T, Implicit_Base);
19775 Init_Size_Align (T);
19776 Inherit_Rep_Item_Chain (T, Implicit_Base);
19777 Set_Small_Value (T, Small_Val);
19778 Set_Delta_Value (T, Delta_Val);
19779 Set_Is_Constrained (T);
19780 end Ordinary_Fixed_Point_Type_Declaration;
19782 ----------------------------------
19783 -- Preanalyze_Assert_Expression --
19784 ----------------------------------
19786 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19788 In_Assertion_Expr := In_Assertion_Expr + 1;
19789 Preanalyze_Spec_Expression (N, T);
19790 In_Assertion_Expr := In_Assertion_Expr - 1;
19791 end Preanalyze_Assert_Expression;
19793 -----------------------------------
19794 -- Preanalyze_Default_Expression --
19795 -----------------------------------
19797 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19798 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19799 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19802 In_Default_Expr := True;
19803 In_Spec_Expression := True;
19805 Preanalyze_With_Freezing_And_Resolve (N, T);
19807 In_Default_Expr := Save_In_Default_Expr;
19808 In_Spec_Expression := Save_In_Spec_Expression;
19809 end Preanalyze_Default_Expression;
19811 --------------------------------
19812 -- Preanalyze_Spec_Expression --
19813 --------------------------------
19815 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19816 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19818 In_Spec_Expression := True;
19819 Preanalyze_And_Resolve (N, T);
19820 In_Spec_Expression := Save_In_Spec_Expression;
19821 end Preanalyze_Spec_Expression;
19823 ----------------------------------------
19824 -- Prepare_Private_Subtype_Completion --
19825 ----------------------------------------
19827 procedure Prepare_Private_Subtype_Completion
19829 Related_Nod : Node_Id)
19831 Id_B : constant Entity_Id := Base_Type (Id);
19832 Full_B : constant Entity_Id := Full_View (Id_B);
19836 if Present (Full_B) then
19838 -- The Base_Type is already completed, we can complete the subtype
19839 -- now. We have to create a new entity with the same name, Thus we
19840 -- can't use Create_Itype.
19842 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19843 Set_Is_Itype (Full);
19844 Set_Associated_Node_For_Itype (Full, Related_Nod);
19845 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19846 Set_Full_View (Id, Full);
19849 -- The parent subtype may be private, but the base might not, in some
19850 -- nested instances. In that case, the subtype does not need to be
19851 -- exchanged. It would still be nice to make private subtypes and their
19852 -- bases consistent at all times ???
19854 if Is_Private_Type (Id_B) then
19855 Append_Elmt (Id, Private_Dependents (Id_B));
19857 end Prepare_Private_Subtype_Completion;
19859 ---------------------------
19860 -- Process_Discriminants --
19861 ---------------------------
19863 procedure Process_Discriminants
19865 Prev : Entity_Id := Empty)
19867 Elist : constant Elist_Id := New_Elmt_List;
19870 Discr_Number : Uint;
19871 Discr_Type : Entity_Id;
19872 Default_Present : Boolean := False;
19873 Default_Not_Present : Boolean := False;
19876 -- A composite type other than an array type can have discriminants.
19877 -- On entry, the current scope is the composite type.
19879 -- The discriminants are initially entered into the scope of the type
19880 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19881 -- use, as explained at the end of this procedure.
19883 Discr := First (Discriminant_Specifications (N));
19884 while Present (Discr) loop
19885 Enter_Name (Defining_Identifier (Discr));
19887 -- For navigation purposes we add a reference to the discriminant
19888 -- in the entity for the type. If the current declaration is a
19889 -- completion, place references on the partial view. Otherwise the
19890 -- type is the current scope.
19892 if Present (Prev) then
19894 -- The references go on the partial view, if present. If the
19895 -- partial view has discriminants, the references have been
19896 -- generated already.
19898 if not Has_Discriminants (Prev) then
19899 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19903 (Current_Scope, Defining_Identifier (Discr), 'd');
19906 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19907 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19909 -- Ada 2005 (AI-254)
19911 if Present (Access_To_Subprogram_Definition
19912 (Discriminant_Type (Discr)))
19913 and then Protected_Present (Access_To_Subprogram_Definition
19914 (Discriminant_Type (Discr)))
19917 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19921 Find_Type (Discriminant_Type (Discr));
19922 Discr_Type := Etype (Discriminant_Type (Discr));
19924 if Error_Posted (Discriminant_Type (Discr)) then
19925 Discr_Type := Any_Type;
19929 -- Handling of discriminants that are access types
19931 if Is_Access_Type (Discr_Type) then
19933 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19934 -- limited record types
19936 if Ada_Version < Ada_2005 then
19937 Check_Access_Discriminant_Requires_Limited
19938 (Discr, Discriminant_Type (Discr));
19941 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19943 ("(Ada 83) access discriminant not allowed", Discr);
19946 -- If not access type, must be a discrete type
19948 elsif not Is_Discrete_Type (Discr_Type) then
19950 ("discriminants must have a discrete or access type",
19951 Discriminant_Type (Discr));
19954 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19956 -- If a discriminant specification includes the assignment compound
19957 -- delimiter followed by an expression, the expression is the default
19958 -- expression of the discriminant; the default expression must be of
19959 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19960 -- a default expression, we do the special preanalysis, since this
19961 -- expression does not freeze (see section "Handling of Default and
19962 -- Per-Object Expressions" in spec of package Sem).
19964 if Present (Expression (Discr)) then
19965 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19969 if Nkind (N) = N_Formal_Type_Declaration then
19971 ("discriminant defaults not allowed for formal type",
19972 Expression (Discr));
19974 -- Flag an error for a tagged type with defaulted discriminants,
19975 -- excluding limited tagged types when compiling for Ada 2012
19976 -- (see AI05-0214).
19978 elsif Is_Tagged_Type (Current_Scope)
19979 and then (not Is_Limited_Type (Current_Scope)
19980 or else Ada_Version < Ada_2012)
19981 and then Comes_From_Source (N)
19983 -- Note: see similar test in Check_Or_Process_Discriminants, to
19984 -- handle the (illegal) case of the completion of an untagged
19985 -- view with discriminants with defaults by a tagged full view.
19986 -- We skip the check if Discr does not come from source, to
19987 -- account for the case of an untagged derived type providing
19988 -- defaults for a renamed discriminant from a private untagged
19989 -- ancestor with a tagged full view (ACATS B460006).
19991 if Ada_Version >= Ada_2012 then
19993 ("discriminants of nonlimited tagged type cannot have"
19995 Expression (Discr));
19998 ("discriminants of tagged type cannot have defaults",
19999 Expression (Discr));
20003 Default_Present := True;
20004 Append_Elmt (Expression (Discr), Elist);
20006 -- Tag the defining identifiers for the discriminants with
20007 -- their corresponding default expressions from the tree.
20009 Set_Discriminant_Default_Value
20010 (Defining_Identifier (Discr), Expression (Discr));
20013 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
20014 -- gets set unless we can be sure that no range check is required.
20016 if not Expander_Active
20019 (Expression (Discr), Discr_Type, Assume_Valid => True)
20021 Set_Do_Range_Check (Expression (Discr));
20024 -- No default discriminant value given
20027 Default_Not_Present := True;
20030 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20031 -- Discr_Type but with the null-exclusion attribute
20033 if Ada_Version >= Ada_2005 then
20035 -- Ada 2005 (AI-231): Static checks
20037 if Can_Never_Be_Null (Discr_Type) then
20038 Null_Exclusion_Static_Checks (Discr);
20040 elsif Is_Access_Type (Discr_Type)
20041 and then Null_Exclusion_Present (Discr)
20043 -- No need to check itypes because in their case this check
20044 -- was done at their point of creation
20046 and then not Is_Itype (Discr_Type)
20048 if Can_Never_Be_Null (Discr_Type) then
20050 ("`NOT NULL` not allowed (& already excludes null)",
20055 Set_Etype (Defining_Identifier (Discr),
20056 Create_Null_Excluding_Itype
20058 Related_Nod => Discr));
20060 -- Check for improper null exclusion if the type is otherwise
20061 -- legal for a discriminant.
20063 elsif Null_Exclusion_Present (Discr)
20064 and then Is_Discrete_Type (Discr_Type)
20067 ("null exclusion can only apply to an access type", Discr);
20070 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20071 -- can't have defaults. Synchronized types, or types that are
20072 -- explicitly limited are fine, but special tests apply to derived
20073 -- types in generics: in a generic body we have to assume the
20074 -- worst, and therefore defaults are not allowed if the parent is
20075 -- a generic formal private type (see ACATS B370001).
20077 if Is_Access_Type (Discr_Type) and then Default_Present then
20078 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20079 or else Is_Limited_Record (Current_Scope)
20080 or else Is_Concurrent_Type (Current_Scope)
20081 or else Is_Concurrent_Record_Type (Current_Scope)
20082 or else Ekind (Current_Scope) = E_Limited_Private_Type
20084 if not Is_Derived_Type (Current_Scope)
20085 or else not Is_Generic_Type (Etype (Current_Scope))
20086 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20087 or else Limited_Present
20088 (Type_Definition (Parent (Current_Scope)))
20094 ("access discriminants of nonlimited types cannot "
20095 & "have defaults", Expression (Discr));
20098 elsif Present (Expression (Discr)) then
20100 ("(Ada 2005) access discriminants of nonlimited types "
20101 & "cannot have defaults", Expression (Discr));
20106 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(4)).
20107 -- This check is relevant only when SPARK_Mode is on as it is not a
20108 -- standard Ada legality rule.
20111 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
20113 Error_Msg_N ("discriminant cannot be volatile", Discr);
20119 -- An element list consisting of the default expressions of the
20120 -- discriminants is constructed in the above loop and used to set
20121 -- the Discriminant_Constraint attribute for the type. If an object
20122 -- is declared of this (record or task) type without any explicit
20123 -- discriminant constraint given, this element list will form the
20124 -- actual parameters for the corresponding initialization procedure
20127 Set_Discriminant_Constraint (Current_Scope, Elist);
20128 Set_Stored_Constraint (Current_Scope, No_Elist);
20130 -- Default expressions must be provided either for all or for none
20131 -- of the discriminants of a discriminant part. (RM 3.7.1)
20133 if Default_Present and then Default_Not_Present then
20135 ("incomplete specification of defaults for discriminants", N);
20138 -- The use of the name of a discriminant is not allowed in default
20139 -- expressions of a discriminant part if the specification of the
20140 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20142 -- To detect this, the discriminant names are entered initially with an
20143 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20144 -- attempt to use a void entity (for example in an expression that is
20145 -- type-checked) produces the error message: premature usage. Now after
20146 -- completing the semantic analysis of the discriminant part, we can set
20147 -- the Ekind of all the discriminants appropriately.
20149 Discr := First (Discriminant_Specifications (N));
20150 Discr_Number := Uint_1;
20151 while Present (Discr) loop
20152 Id := Defining_Identifier (Discr);
20153 Set_Ekind (Id, E_Discriminant);
20154 Init_Component_Location (Id);
20156 Set_Discriminant_Number (Id, Discr_Number);
20158 -- Make sure this is always set, even in illegal programs
20160 Set_Corresponding_Discriminant (Id, Empty);
20162 -- Initialize the Original_Record_Component to the entity itself.
20163 -- Inherit_Components will propagate the right value to
20164 -- discriminants in derived record types.
20166 Set_Original_Record_Component (Id, Id);
20168 -- Create the discriminal for the discriminant
20170 Build_Discriminal (Id);
20173 Discr_Number := Discr_Number + 1;
20176 Set_Has_Discriminants (Current_Scope);
20177 end Process_Discriminants;
20179 -----------------------
20180 -- Process_Full_View --
20181 -----------------------
20183 -- WARNING: This routine manages Ghost regions. Return statements must be
20184 -- replaced by gotos which jump to the end of the routine and restore the
20187 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20188 procedure Collect_Implemented_Interfaces
20190 Ifaces : Elist_Id);
20191 -- Ada 2005: Gather all the interfaces that Typ directly or
20192 -- inherently implements. Duplicate entries are not added to
20193 -- the list Ifaces.
20195 ------------------------------------
20196 -- Collect_Implemented_Interfaces --
20197 ------------------------------------
20199 procedure Collect_Implemented_Interfaces
20204 Iface_Elmt : Elmt_Id;
20207 -- Abstract interfaces are only associated with tagged record types
20209 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20213 -- Recursively climb to the ancestors
20215 if Etype (Typ) /= Typ
20217 -- Protect the frontend against wrong cyclic declarations like:
20219 -- type B is new A with private;
20220 -- type C is new A with private;
20222 -- type B is new C with null record;
20223 -- type C is new B with null record;
20225 and then Etype (Typ) /= Priv_T
20226 and then Etype (Typ) /= Full_T
20228 -- Keep separate the management of private type declarations
20230 if Ekind (Typ) = E_Record_Type_With_Private then
20232 -- Handle the following illegal usage:
20233 -- type Private_Type is tagged private;
20235 -- type Private_Type is new Type_Implementing_Iface;
20237 if Present (Full_View (Typ))
20238 and then Etype (Typ) /= Full_View (Typ)
20240 if Is_Interface (Etype (Typ)) then
20241 Append_Unique_Elmt (Etype (Typ), Ifaces);
20244 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20247 -- Non-private types
20250 if Is_Interface (Etype (Typ)) then
20251 Append_Unique_Elmt (Etype (Typ), Ifaces);
20254 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20258 -- Handle entities in the list of abstract interfaces
20260 if Present (Interfaces (Typ)) then
20261 Iface_Elmt := First_Elmt (Interfaces (Typ));
20262 while Present (Iface_Elmt) loop
20263 Iface := Node (Iface_Elmt);
20265 pragma Assert (Is_Interface (Iface));
20267 if not Contain_Interface (Iface, Ifaces) then
20268 Append_Elmt (Iface, Ifaces);
20269 Collect_Implemented_Interfaces (Iface, Ifaces);
20272 Next_Elmt (Iface_Elmt);
20275 end Collect_Implemented_Interfaces;
20279 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20280 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
20281 -- Save the Ghost-related attributes to restore on exit
20283 Full_Indic : Node_Id;
20284 Full_Parent : Entity_Id;
20285 Priv_Parent : Entity_Id;
20287 -- Start of processing for Process_Full_View
20290 Mark_And_Set_Ghost_Completion (N, Priv_T);
20292 -- First some sanity checks that must be done after semantic
20293 -- decoration of the full view and thus cannot be placed with other
20294 -- similar checks in Find_Type_Name
20296 if not Is_Limited_Type (Priv_T)
20297 and then (Is_Limited_Type (Full_T)
20298 or else Is_Limited_Composite (Full_T))
20300 if In_Instance then
20304 ("completion of nonlimited type cannot be limited", Full_T);
20305 Explain_Limited_Type (Full_T, Full_T);
20308 elsif Is_Abstract_Type (Full_T)
20309 and then not Is_Abstract_Type (Priv_T)
20312 ("completion of nonabstract type cannot be abstract", Full_T);
20314 elsif Is_Tagged_Type (Priv_T)
20315 and then Is_Limited_Type (Priv_T)
20316 and then not Is_Limited_Type (Full_T)
20318 -- If pragma CPP_Class was applied to the private declaration
20319 -- propagate the limitedness to the full-view
20321 if Is_CPP_Class (Priv_T) then
20322 Set_Is_Limited_Record (Full_T);
20324 -- GNAT allow its own definition of Limited_Controlled to disobey
20325 -- this rule in order in ease the implementation. This test is safe
20326 -- because Root_Controlled is defined in a child of System that
20327 -- normal programs are not supposed to use.
20329 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20330 Set_Is_Limited_Composite (Full_T);
20333 ("completion of limited tagged type must be limited", Full_T);
20336 elsif Is_Generic_Type (Priv_T) then
20337 Error_Msg_N ("generic type cannot have a completion", Full_T);
20340 -- Check that ancestor interfaces of private and full views are
20341 -- consistent. We omit this check for synchronized types because
20342 -- they are performed on the corresponding record type when frozen.
20344 if Ada_Version >= Ada_2005
20345 and then Is_Tagged_Type (Priv_T)
20346 and then Is_Tagged_Type (Full_T)
20347 and then not Is_Concurrent_Type (Full_T)
20351 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20352 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20355 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20356 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20358 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20359 -- an interface type if and only if the full type is descendant
20360 -- of the interface type (AARM 7.3 (7.3/2)).
20362 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20364 if Present (Iface) then
20366 ("interface in partial view& not implemented by full type "
20367 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20370 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20372 if Present (Iface) then
20374 ("interface & not implemented by partial view "
20375 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20380 if Is_Tagged_Type (Priv_T)
20381 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20382 and then Is_Derived_Type (Full_T)
20384 Priv_Parent := Etype (Priv_T);
20386 -- The full view of a private extension may have been transformed
20387 -- into an unconstrained derived type declaration and a subtype
20388 -- declaration (see build_derived_record_type for details).
20390 if Nkind (N) = N_Subtype_Declaration then
20391 Full_Indic := Subtype_Indication (N);
20392 Full_Parent := Etype (Base_Type (Full_T));
20394 Full_Indic := Subtype_Indication (Type_Definition (N));
20395 Full_Parent := Etype (Full_T);
20398 -- Check that the parent type of the full type is a descendant of
20399 -- the ancestor subtype given in the private extension. If either
20400 -- entity has an Etype equal to Any_Type then we had some previous
20401 -- error situation [7.3(8)].
20403 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20406 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20407 -- any order. Therefore we don't have to check that its parent must
20408 -- be a descendant of the parent of the private type declaration.
20410 elsif Is_Interface (Priv_Parent)
20411 and then Is_Interface (Full_Parent)
20415 -- Ada 2005 (AI-251): If the parent of the private type declaration
20416 -- is an interface there is no need to check that it is an ancestor
20417 -- of the associated full type declaration. The required tests for
20418 -- this case are performed by Build_Derived_Record_Type.
20420 elsif not Is_Interface (Base_Type (Priv_Parent))
20421 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20424 ("parent of full type must descend from parent of private "
20425 & "extension", Full_Indic);
20427 -- First check a formal restriction, and then proceed with checking
20428 -- Ada rules. Since the formal restriction is not a serious error, we
20429 -- don't prevent further error detection for this check, hence the
20433 -- Check the rules of 7.3(10): if the private extension inherits
20434 -- known discriminants, then the full type must also inherit those
20435 -- discriminants from the same (ancestor) type, and the parent
20436 -- subtype of the full type must be constrained if and only if
20437 -- the ancestor subtype of the private extension is constrained.
20439 if No (Discriminant_Specifications (Parent (Priv_T)))
20440 and then not Has_Unknown_Discriminants (Priv_T)
20441 and then Has_Discriminants (Base_Type (Priv_Parent))
20444 Priv_Indic : constant Node_Id :=
20445 Subtype_Indication (Parent (Priv_T));
20447 Priv_Constr : constant Boolean :=
20448 Is_Constrained (Priv_Parent)
20450 Nkind (Priv_Indic) = N_Subtype_Indication
20452 Is_Constrained (Entity (Priv_Indic));
20454 Full_Constr : constant Boolean :=
20455 Is_Constrained (Full_Parent)
20457 Nkind (Full_Indic) = N_Subtype_Indication
20459 Is_Constrained (Entity (Full_Indic));
20461 Priv_Discr : Entity_Id;
20462 Full_Discr : Entity_Id;
20465 Priv_Discr := First_Discriminant (Priv_Parent);
20466 Full_Discr := First_Discriminant (Full_Parent);
20467 while Present (Priv_Discr) and then Present (Full_Discr) loop
20468 if Original_Record_Component (Priv_Discr) =
20469 Original_Record_Component (Full_Discr)
20471 Corresponding_Discriminant (Priv_Discr) =
20472 Corresponding_Discriminant (Full_Discr)
20479 Next_Discriminant (Priv_Discr);
20480 Next_Discriminant (Full_Discr);
20483 if Present (Priv_Discr) or else Present (Full_Discr) then
20485 ("full view must inherit discriminants of the parent "
20486 & "type used in the private extension", Full_Indic);
20488 elsif Priv_Constr and then not Full_Constr then
20490 ("parent subtype of full type must be constrained",
20493 elsif Full_Constr and then not Priv_Constr then
20495 ("parent subtype of full type must be unconstrained",
20500 -- Check the rules of 7.3(12): if a partial view has neither
20501 -- known or unknown discriminants, then the full type
20502 -- declaration shall define a definite subtype.
20504 elsif not Has_Unknown_Discriminants (Priv_T)
20505 and then not Has_Discriminants (Priv_T)
20506 and then not Is_Constrained (Full_T)
20509 ("full view must define a constrained type if partial view "
20510 & "has no discriminants", Full_T);
20513 -- ??????? Do we implement the following properly ?????
20514 -- If the ancestor subtype of a private extension has constrained
20515 -- discriminants, then the parent subtype of the full view shall
20516 -- impose a statically matching constraint on those discriminants
20521 -- For untagged types, verify that a type without discriminants is
20522 -- not completed with an unconstrained type. A separate error message
20523 -- is produced if the full type has defaulted discriminants.
20525 if Is_Definite_Subtype (Priv_T)
20526 and then not Is_Definite_Subtype (Full_T)
20528 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20530 ("full view of& not compatible with declaration#",
20533 if not Is_Tagged_Type (Full_T) then
20535 ("\one is constrained, the other unconstrained", Full_T);
20540 -- AI-419: verify that the use of "limited" is consistent
20543 Orig_Decl : constant Node_Id := Original_Node (N);
20546 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20547 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20549 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20551 if not Limited_Present (Parent (Priv_T))
20552 and then not Synchronized_Present (Parent (Priv_T))
20553 and then Limited_Present (Type_Definition (Orig_Decl))
20556 ("full view of non-limited extension cannot be limited", N);
20558 -- Conversely, if the partial view carries the limited keyword,
20559 -- the full view must as well, even if it may be redundant.
20561 elsif Limited_Present (Parent (Priv_T))
20562 and then not Limited_Present (Type_Definition (Orig_Decl))
20565 ("full view of limited extension must be explicitly limited",
20571 -- Ada 2005 (AI-443): A synchronized private extension must be
20572 -- completed by a task or protected type.
20574 if Ada_Version >= Ada_2005
20575 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20576 and then Synchronized_Present (Parent (Priv_T))
20577 and then not Is_Concurrent_Type (Full_T)
20579 Error_Msg_N ("full view of synchronized extension must " &
20580 "be synchronized type", N);
20583 -- Ada 2005 AI-363: if the full view has discriminants with
20584 -- defaults, it is illegal to declare constrained access subtypes
20585 -- whose designated type is the current type. This allows objects
20586 -- of the type that are declared in the heap to be unconstrained.
20588 if not Has_Unknown_Discriminants (Priv_T)
20589 and then not Has_Discriminants (Priv_T)
20590 and then Has_Discriminants (Full_T)
20592 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20594 Set_Has_Constrained_Partial_View (Full_T);
20595 Set_Has_Constrained_Partial_View (Priv_T);
20598 -- Create a full declaration for all its subtypes recorded in
20599 -- Private_Dependents and swap them similarly to the base type. These
20600 -- are subtypes that have been define before the full declaration of
20601 -- the private type. We also swap the entry in Private_Dependents list
20602 -- so we can properly restore the private view on exit from the scope.
20605 Priv_Elmt : Elmt_Id;
20606 Priv_Scop : Entity_Id;
20611 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20612 while Present (Priv_Elmt) loop
20613 Priv := Node (Priv_Elmt);
20614 Priv_Scop := Scope (Priv);
20616 if Ekind_In (Priv, E_Private_Subtype,
20617 E_Limited_Private_Subtype,
20618 E_Record_Subtype_With_Private)
20620 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20621 Set_Is_Itype (Full);
20622 Set_Parent (Full, Parent (Priv));
20623 Set_Associated_Node_For_Itype (Full, N);
20625 -- Now we need to complete the private subtype, but since the
20626 -- base type has already been swapped, we must also swap the
20627 -- subtypes (and thus, reverse the arguments in the call to
20628 -- Complete_Private_Subtype). Also note that we may need to
20629 -- re-establish the scope of the private subtype.
20631 Copy_And_Swap (Priv, Full);
20633 if not In_Open_Scopes (Priv_Scop) then
20634 Push_Scope (Priv_Scop);
20637 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20639 Priv_Scop := Empty;
20642 Complete_Private_Subtype (Full, Priv, Full_T, N);
20643 Set_Full_View (Full, Priv);
20645 if Present (Priv_Scop) then
20649 Replace_Elmt (Priv_Elmt, Full);
20652 Next_Elmt (Priv_Elmt);
20656 -- If the private view was tagged, copy the new primitive operations
20657 -- from the private view to the full view.
20659 if Is_Tagged_Type (Full_T) then
20661 Disp_Typ : Entity_Id;
20662 Full_List : Elist_Id;
20664 Prim_Elmt : Elmt_Id;
20665 Priv_List : Elist_Id;
20669 L : Elist_Id) return Boolean;
20670 -- Determine whether list L contains element E
20678 L : Elist_Id) return Boolean
20680 List_Elmt : Elmt_Id;
20683 List_Elmt := First_Elmt (L);
20684 while Present (List_Elmt) loop
20685 if Node (List_Elmt) = E then
20689 Next_Elmt (List_Elmt);
20695 -- Start of processing
20698 if Is_Tagged_Type (Priv_T) then
20699 Priv_List := Primitive_Operations (Priv_T);
20700 Prim_Elmt := First_Elmt (Priv_List);
20702 -- In the case of a concurrent type completing a private tagged
20703 -- type, primitives may have been declared in between the two
20704 -- views. These subprograms need to be wrapped the same way
20705 -- entries and protected procedures are handled because they
20706 -- cannot be directly shared by the two views.
20708 if Is_Concurrent_Type (Full_T) then
20710 Conc_Typ : constant Entity_Id :=
20711 Corresponding_Record_Type (Full_T);
20712 Curr_Nod : Node_Id := Parent (Conc_Typ);
20713 Wrap_Spec : Node_Id;
20716 while Present (Prim_Elmt) loop
20717 Prim := Node (Prim_Elmt);
20719 if Comes_From_Source (Prim)
20720 and then not Is_Abstract_Subprogram (Prim)
20723 Make_Subprogram_Declaration (Sloc (Prim),
20727 Obj_Typ => Conc_Typ,
20729 Parameter_Specifications
20732 Insert_After (Curr_Nod, Wrap_Spec);
20733 Curr_Nod := Wrap_Spec;
20735 Analyze (Wrap_Spec);
20737 -- Remove the wrapper from visibility to avoid
20738 -- spurious conflict with the wrapped entity.
20740 Set_Is_Immediately_Visible
20741 (Defining_Entity (Specification (Wrap_Spec)),
20745 Next_Elmt (Prim_Elmt);
20751 -- For non-concurrent types, transfer explicit primitives, but
20752 -- omit those inherited from the parent of the private view
20753 -- since they will be re-inherited later on.
20756 Full_List := Primitive_Operations (Full_T);
20757 while Present (Prim_Elmt) loop
20758 Prim := Node (Prim_Elmt);
20760 if Comes_From_Source (Prim)
20761 and then not Contains (Prim, Full_List)
20763 Append_Elmt (Prim, Full_List);
20766 Next_Elmt (Prim_Elmt);
20770 -- Untagged private view
20773 Full_List := Primitive_Operations (Full_T);
20775 -- In this case the partial view is untagged, so here we locate
20776 -- all of the earlier primitives that need to be treated as
20777 -- dispatching (those that appear between the two views). Note
20778 -- that these additional operations must all be new operations
20779 -- (any earlier operations that override inherited operations
20780 -- of the full view will already have been inserted in the
20781 -- primitives list, marked by Check_Operation_From_Private_View
20782 -- as dispatching. Note that implicit "/=" operators are
20783 -- excluded from being added to the primitives list since they
20784 -- shouldn't be treated as dispatching (tagged "/=" is handled
20787 Prim := Next_Entity (Full_T);
20788 while Present (Prim) and then Prim /= Priv_T loop
20789 if Ekind_In (Prim, E_Procedure, E_Function) then
20790 Disp_Typ := Find_Dispatching_Type (Prim);
20792 if Disp_Typ = Full_T
20793 and then (Chars (Prim) /= Name_Op_Ne
20794 or else Comes_From_Source (Prim))
20796 Check_Controlling_Formals (Full_T, Prim);
20798 if Is_Suitable_Primitive (Prim)
20799 and then not Is_Dispatching_Operation (Prim)
20801 Append_Elmt (Prim, Full_List);
20802 Set_Is_Dispatching_Operation (Prim);
20803 Set_DT_Position_Value (Prim, No_Uint);
20806 elsif Is_Dispatching_Operation (Prim)
20807 and then Disp_Typ /= Full_T
20809 -- Verify that it is not otherwise controlled by a
20810 -- formal or a return value of type T.
20812 Check_Controlling_Formals (Disp_Typ, Prim);
20816 Next_Entity (Prim);
20820 -- For the tagged case, the two views can share the same primitive
20821 -- operations list and the same class-wide type. Update attributes
20822 -- of the class-wide type which depend on the full declaration.
20824 if Is_Tagged_Type (Priv_T) then
20825 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20826 Set_Class_Wide_Type
20827 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20829 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20834 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20836 if Known_To_Have_Preelab_Init (Priv_T) then
20838 -- Case where there is a pragma Preelaborable_Initialization. We
20839 -- always allow this in predefined units, which is cheating a bit,
20840 -- but it means we don't have to struggle to meet the requirements in
20841 -- the RM for having Preelaborable Initialization. Otherwise we
20842 -- require that the type meets the RM rules. But we can't check that
20843 -- yet, because of the rule about overriding Initialize, so we simply
20844 -- set a flag that will be checked at freeze time.
20846 if not In_Predefined_Unit (Full_T) then
20847 Set_Must_Have_Preelab_Init (Full_T);
20851 -- If pragma CPP_Class was applied to the private type declaration,
20852 -- propagate it now to the full type declaration.
20854 if Is_CPP_Class (Priv_T) then
20855 Set_Is_CPP_Class (Full_T);
20856 Set_Convention (Full_T, Convention_CPP);
20858 -- Check that components of imported CPP types do not have default
20861 Check_CPP_Type_Has_No_Defaults (Full_T);
20864 -- If the private view has user specified stream attributes, then so has
20867 -- Why the test, how could these flags be already set in Full_T ???
20869 if Has_Specified_Stream_Read (Priv_T) then
20870 Set_Has_Specified_Stream_Read (Full_T);
20873 if Has_Specified_Stream_Write (Priv_T) then
20874 Set_Has_Specified_Stream_Write (Full_T);
20877 if Has_Specified_Stream_Input (Priv_T) then
20878 Set_Has_Specified_Stream_Input (Full_T);
20881 if Has_Specified_Stream_Output (Priv_T) then
20882 Set_Has_Specified_Stream_Output (Full_T);
20885 -- Propagate Default_Initial_Condition-related attributes from the
20886 -- partial view to the full view.
20888 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20890 -- And to the underlying full view, if any
20892 if Is_Private_Type (Full_T)
20893 and then Present (Underlying_Full_View (Full_T))
20895 Propagate_DIC_Attributes
20896 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
20899 -- Propagate invariant-related attributes from the partial view to the
20902 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20904 -- And to the underlying full view, if any
20906 if Is_Private_Type (Full_T)
20907 and then Present (Underlying_Full_View (Full_T))
20909 Propagate_Invariant_Attributes
20910 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
20913 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20914 -- in the full view without advertising the inheritance in the partial
20915 -- view. This can only occur when the partial view has no parent type
20916 -- and the full view has an interface as a parent. Any other scenarios
20917 -- are illegal because implemented interfaces must match between the
20920 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20922 Full_Par : constant Entity_Id := Etype (Full_T);
20923 Priv_Par : constant Entity_Id := Etype (Priv_T);
20926 if not Is_Interface (Priv_Par)
20927 and then Is_Interface (Full_Par)
20928 and then Has_Inheritable_Invariants (Full_Par)
20931 ("hidden inheritance of class-wide type invariants not "
20937 -- Propagate predicates to full type, and predicate function if already
20938 -- defined. It is not clear that this can actually happen? the partial
20939 -- view cannot be frozen yet, and the predicate function has not been
20940 -- built. Still it is a cheap check and seems safer to make it.
20942 Propagate_Predicate_Attributes (Full_T, Priv_T);
20944 if Is_Private_Type (Full_T)
20945 and then Present (Underlying_Full_View (Full_T))
20947 Propagate_Predicate_Attributes
20948 (Underlying_Full_View (Full_T), Priv_T);
20952 Restore_Ghost_Region (Saved_GM, Saved_IGR);
20953 end Process_Full_View;
20955 -----------------------------------
20956 -- Process_Incomplete_Dependents --
20957 -----------------------------------
20959 procedure Process_Incomplete_Dependents
20961 Full_T : Entity_Id;
20964 Inc_Elmt : Elmt_Id;
20965 Priv_Dep : Entity_Id;
20966 New_Subt : Entity_Id;
20968 Disc_Constraint : Elist_Id;
20971 if No (Private_Dependents (Inc_T)) then
20975 -- Itypes that may be generated by the completion of an incomplete
20976 -- subtype are not used by the back-end and not attached to the tree.
20977 -- They are created only for constraint-checking purposes.
20979 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20980 while Present (Inc_Elmt) loop
20981 Priv_Dep := Node (Inc_Elmt);
20983 if Ekind (Priv_Dep) = E_Subprogram_Type then
20985 -- An Access_To_Subprogram type may have a return type or a
20986 -- parameter type that is incomplete. Replace with the full view.
20988 if Etype (Priv_Dep) = Inc_T then
20989 Set_Etype (Priv_Dep, Full_T);
20993 Formal : Entity_Id;
20996 Formal := First_Formal (Priv_Dep);
20997 while Present (Formal) loop
20998 if Etype (Formal) = Inc_T then
20999 Set_Etype (Formal, Full_T);
21002 Next_Formal (Formal);
21006 elsif Is_Overloadable (Priv_Dep) then
21008 -- If a subprogram in the incomplete dependents list is primitive
21009 -- for a tagged full type then mark it as a dispatching operation,
21010 -- check whether it overrides an inherited subprogram, and check
21011 -- restrictions on its controlling formals. Note that a protected
21012 -- operation is never dispatching: only its wrapper operation
21013 -- (which has convention Ada) is.
21015 if Is_Tagged_Type (Full_T)
21016 and then Is_Primitive (Priv_Dep)
21017 and then Convention (Priv_Dep) /= Convention_Protected
21019 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21020 Set_Is_Dispatching_Operation (Priv_Dep);
21021 Check_Controlling_Formals (Full_T, Priv_Dep);
21024 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21026 -- Can happen during processing of a body before the completion
21027 -- of a TA type. Ignore, because spec is also on dependent list.
21031 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21032 -- corresponding subtype of the full view.
21034 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21035 and then Comes_From_Source (Priv_Dep)
21037 Set_Subtype_Indication
21038 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21039 Set_Etype (Priv_Dep, Full_T);
21040 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21041 Set_Analyzed (Parent (Priv_Dep), False);
21043 -- Reanalyze the declaration, suppressing the call to Enter_Name
21044 -- to avoid duplicate names.
21046 Analyze_Subtype_Declaration
21047 (N => Parent (Priv_Dep),
21050 -- Dependent is a subtype
21053 -- We build a new subtype indication using the full view of the
21054 -- incomplete parent. The discriminant constraints have been
21055 -- elaborated already at the point of the subtype declaration.
21057 New_Subt := Create_Itype (E_Void, N);
21059 if Has_Discriminants (Full_T) then
21060 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21062 Disc_Constraint := No_Elist;
21065 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21066 Set_Full_View (Priv_Dep, New_Subt);
21069 Next_Elmt (Inc_Elmt);
21071 end Process_Incomplete_Dependents;
21073 --------------------------------
21074 -- Process_Range_Expr_In_Decl --
21075 --------------------------------
21077 procedure Process_Range_Expr_In_Decl
21080 Subtyp : Entity_Id := Empty;
21081 Check_List : List_Id := No_List;
21082 R_Check_Off : Boolean := False)
21085 R_Checks : Check_Result;
21086 Insert_Node : Node_Id;
21087 Def_Id : Entity_Id;
21090 Analyze_And_Resolve (R, Base_Type (T));
21092 if Nkind (R) = N_Range then
21093 Lo := Low_Bound (R);
21094 Hi := High_Bound (R);
21096 -- Validity checks on the range of a quantified expression are
21097 -- delayed until the construct is transformed into a loop.
21099 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21100 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21104 -- We need to ensure validity of the bounds here, because if we
21105 -- go ahead and do the expansion, then the expanded code will get
21106 -- analyzed with range checks suppressed and we miss the check.
21108 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21109 -- the temporaries generated by routine Remove_Side_Effects by means
21110 -- of validity checks must use the same names. When a range appears
21111 -- in the parent of a generic, the range is processed with checks
21112 -- disabled as part of the generic context and with checks enabled
21113 -- for code generation purposes. This leads to link issues as the
21114 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21115 -- template sees the temporaries generated by Remove_Side_Effects.
21118 Validity_Check_Range (R, Subtyp);
21121 -- If there were errors in the declaration, try and patch up some
21122 -- common mistakes in the bounds. The cases handled are literals
21123 -- which are Integer where the expected type is Real and vice versa.
21124 -- These corrections allow the compilation process to proceed further
21125 -- along since some basic assumptions of the format of the bounds
21128 if Etype (R) = Any_Type then
21129 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21131 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21133 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21135 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21137 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21139 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21141 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21143 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21150 -- If the bounds of the range have been mistakenly given as string
21151 -- literals (perhaps in place of character literals), then an error
21152 -- has already been reported, but we rewrite the string literal as a
21153 -- bound of the range's type to avoid blowups in later processing
21154 -- that looks at static values.
21156 if Nkind (Lo) = N_String_Literal then
21158 Make_Attribute_Reference (Sloc (Lo),
21159 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21160 Attribute_Name => Name_First));
21161 Analyze_And_Resolve (Lo);
21164 if Nkind (Hi) = N_String_Literal then
21166 Make_Attribute_Reference (Sloc (Hi),
21167 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21168 Attribute_Name => Name_First));
21169 Analyze_And_Resolve (Hi);
21172 -- If bounds aren't scalar at this point then exit, avoiding
21173 -- problems with further processing of the range in this procedure.
21175 if not Is_Scalar_Type (Etype (Lo)) then
21179 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21180 -- then range of the base type. Here we check whether the bounds
21181 -- are in the range of the subtype itself. Note that if the bounds
21182 -- represent the null range the Constraint_Error exception should
21185 -- ??? The following code should be cleaned up as follows
21187 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21188 -- is done in the call to Range_Check (R, T); below
21190 -- 2. The use of R_Check_Off should be investigated and possibly
21191 -- removed, this would clean up things a bit.
21193 if Is_Null_Range (Lo, Hi) then
21197 -- Capture values of bounds and generate temporaries for them
21198 -- if needed, before applying checks, since checks may cause
21199 -- duplication of the expression without forcing evaluation.
21201 -- The forced evaluation removes side effects from expressions,
21202 -- which should occur also in GNATprove mode. Otherwise, we end up
21203 -- with unexpected insertions of actions at places where this is
21204 -- not supposed to occur, e.g. on default parameters of a call.
21206 if Expander_Active or GNATprove_Mode then
21208 -- Call Force_Evaluation to create declarations as needed to
21209 -- deal with side effects, and also create typ_FIRST/LAST
21210 -- entities for bounds if we have a subtype name.
21212 -- Note: we do this transformation even if expansion is not
21213 -- active if we are in GNATprove_Mode since the transformation
21214 -- is in general required to ensure that the resulting tree has
21215 -- proper Ada semantics.
21218 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21220 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21223 -- We use a flag here instead of suppressing checks on the type
21224 -- because the type we check against isn't necessarily the place
21225 -- where we put the check.
21227 if not R_Check_Off then
21228 R_Checks := Get_Range_Checks (R, T);
21230 -- Look up tree to find an appropriate insertion point. We
21231 -- can't just use insert_actions because later processing
21232 -- depends on the insertion node. Prior to Ada 2012 the
21233 -- insertion point could only be a declaration or a loop, but
21234 -- quantified expressions can appear within any context in an
21235 -- expression, and the insertion point can be any statement,
21236 -- pragma, or declaration.
21238 Insert_Node := Parent (R);
21239 while Present (Insert_Node) loop
21241 Nkind (Insert_Node) in N_Declaration
21244 (Insert_Node, N_Component_Declaration,
21245 N_Loop_Parameter_Specification,
21246 N_Function_Specification,
21247 N_Procedure_Specification);
21249 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21250 or else Nkind (Insert_Node) in
21251 N_Statement_Other_Than_Procedure_Call
21252 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21255 Insert_Node := Parent (Insert_Node);
21258 -- Why would Type_Decl not be present??? Without this test,
21259 -- short regression tests fail.
21261 if Present (Insert_Node) then
21263 -- Case of loop statement. Verify that the range is part
21264 -- of the subtype indication of the iteration scheme.
21266 if Nkind (Insert_Node) = N_Loop_Statement then
21271 Indic := Parent (R);
21272 while Present (Indic)
21273 and then Nkind (Indic) /= N_Subtype_Indication
21275 Indic := Parent (Indic);
21278 if Present (Indic) then
21279 Def_Id := Etype (Subtype_Mark (Indic));
21281 Insert_Range_Checks
21285 Sloc (Insert_Node),
21286 Do_Before => True);
21290 -- Case of declarations. If the declaration is for a type
21291 -- and involves discriminants, the checks are premature at
21292 -- the declaration point and need to wait for the expansion
21293 -- of the initialization procedure, which will pass in the
21294 -- list to put them on; otherwise, the checks are done at
21295 -- the declaration point and there is no need to do them
21296 -- again in the initialization procedure.
21298 elsif Nkind (Insert_Node) in N_Declaration then
21299 Def_Id := Defining_Identifier (Insert_Node);
21301 if (Ekind (Def_Id) = E_Record_Type
21302 and then Depends_On_Discriminant (R))
21304 (Ekind (Def_Id) = E_Protected_Type
21305 and then Has_Discriminants (Def_Id))
21307 if Present (Check_List) then
21308 Append_Range_Checks
21310 Check_List, Def_Id, Sloc (Insert_Node));
21314 if No (Check_List) then
21315 Insert_Range_Checks
21317 Insert_Node, Def_Id, Sloc (Insert_Node));
21321 -- Case of statements. Drop the checks, as the range appears
21322 -- in the context of a quantified expression. Insertion will
21323 -- take place when expression is expanded.
21332 -- Case of other than an explicit N_Range node
21334 -- The forced evaluation removes side effects from expressions, which
21335 -- should occur also in GNATprove mode. Otherwise, we end up with
21336 -- unexpected insertions of actions at places where this is not
21337 -- supposed to occur, e.g. on default parameters of a call.
21339 elsif Expander_Active or GNATprove_Mode then
21340 Get_Index_Bounds (R, Lo, Hi);
21341 Force_Evaluation (Lo);
21342 Force_Evaluation (Hi);
21344 end Process_Range_Expr_In_Decl;
21346 --------------------------------------
21347 -- Process_Real_Range_Specification --
21348 --------------------------------------
21350 procedure Process_Real_Range_Specification (Def : Node_Id) is
21351 Spec : constant Node_Id := Real_Range_Specification (Def);
21354 Err : Boolean := False;
21356 procedure Analyze_Bound (N : Node_Id);
21357 -- Analyze and check one bound
21359 -------------------
21360 -- Analyze_Bound --
21361 -------------------
21363 procedure Analyze_Bound (N : Node_Id) is
21365 Analyze_And_Resolve (N, Any_Real);
21367 if not Is_OK_Static_Expression (N) then
21368 Flag_Non_Static_Expr
21369 ("bound in real type definition is not static!", N);
21374 -- Start of processing for Process_Real_Range_Specification
21377 if Present (Spec) then
21378 Lo := Low_Bound (Spec);
21379 Hi := High_Bound (Spec);
21380 Analyze_Bound (Lo);
21381 Analyze_Bound (Hi);
21383 -- If error, clear away junk range specification
21386 Set_Real_Range_Specification (Def, Empty);
21389 end Process_Real_Range_Specification;
21391 ---------------------
21392 -- Process_Subtype --
21393 ---------------------
21395 function Process_Subtype
21397 Related_Nod : Node_Id;
21398 Related_Id : Entity_Id := Empty;
21399 Suffix : Character := ' ') return Entity_Id
21402 Def_Id : Entity_Id;
21403 Error_Node : Node_Id;
21404 Full_View_Id : Entity_Id;
21405 Subtype_Mark_Id : Entity_Id;
21407 May_Have_Null_Exclusion : Boolean;
21409 procedure Check_Incomplete (T : Node_Id);
21410 -- Called to verify that an incomplete type is not used prematurely
21412 ----------------------
21413 -- Check_Incomplete --
21414 ----------------------
21416 procedure Check_Incomplete (T : Node_Id) is
21418 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21420 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21422 not (Ada_Version >= Ada_2005
21424 (Nkind (Parent (T)) = N_Subtype_Declaration
21425 or else (Nkind (Parent (T)) = N_Subtype_Indication
21426 and then Nkind (Parent (Parent (T))) =
21427 N_Subtype_Declaration)))
21429 Error_Msg_N ("invalid use of type before its full declaration", T);
21431 end Check_Incomplete;
21433 -- Start of processing for Process_Subtype
21436 -- Case of no constraints present
21438 if Nkind (S) /= N_Subtype_Indication then
21441 -- No way to proceed if the subtype indication is malformed. This
21442 -- will happen for example when the subtype indication in an object
21443 -- declaration is missing altogether and the expression is analyzed
21444 -- as if it were that indication.
21446 if not Is_Entity_Name (S) then
21450 Check_Incomplete (S);
21453 -- Ada 2005 (AI-231): Static check
21455 if Ada_Version >= Ada_2005
21456 and then Present (P)
21457 and then Null_Exclusion_Present (P)
21458 and then Nkind (P) /= N_Access_To_Object_Definition
21459 and then not Is_Access_Type (Entity (S))
21461 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21464 -- The following is ugly, can't we have a range or even a flag???
21466 May_Have_Null_Exclusion :=
21467 Nkind_In (P, N_Access_Definition,
21468 N_Access_Function_Definition,
21469 N_Access_Procedure_Definition,
21470 N_Access_To_Object_Definition,
21472 N_Component_Definition)
21474 Nkind_In (P, N_Derived_Type_Definition,
21475 N_Discriminant_Specification,
21476 N_Formal_Object_Declaration,
21477 N_Object_Declaration,
21478 N_Object_Renaming_Declaration,
21479 N_Parameter_Specification,
21480 N_Subtype_Declaration);
21482 -- Create an Itype that is a duplicate of Entity (S) but with the
21483 -- null-exclusion attribute.
21485 if May_Have_Null_Exclusion
21486 and then Is_Access_Type (Entity (S))
21487 and then Null_Exclusion_Present (P)
21489 -- No need to check the case of an access to object definition.
21490 -- It is correct to define double not-null pointers.
21493 -- type Not_Null_Int_Ptr is not null access Integer;
21494 -- type Acc is not null access Not_Null_Int_Ptr;
21496 and then Nkind (P) /= N_Access_To_Object_Definition
21498 if Can_Never_Be_Null (Entity (S)) then
21499 case Nkind (Related_Nod) is
21500 when N_Full_Type_Declaration =>
21501 if Nkind (Type_Definition (Related_Nod))
21502 in N_Array_Type_Definition
21506 (Component_Definition
21507 (Type_Definition (Related_Nod)));
21510 Subtype_Indication (Type_Definition (Related_Nod));
21513 when N_Subtype_Declaration =>
21514 Error_Node := Subtype_Indication (Related_Nod);
21516 when N_Object_Declaration =>
21517 Error_Node := Object_Definition (Related_Nod);
21519 when N_Component_Declaration =>
21521 Subtype_Indication (Component_Definition (Related_Nod));
21523 when N_Allocator =>
21524 Error_Node := Expression (Related_Nod);
21527 pragma Assert (False);
21528 Error_Node := Related_Nod;
21532 ("`NOT NULL` not allowed (& already excludes null)",
21538 Create_Null_Excluding_Itype
21540 Related_Nod => P));
21541 Set_Entity (S, Etype (S));
21546 -- Case of constraint present, so that we have an N_Subtype_Indication
21547 -- node (this node is created only if constraints are present).
21550 Find_Type (Subtype_Mark (S));
21552 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21554 (Nkind (Parent (S)) = N_Subtype_Declaration
21555 and then Is_Itype (Defining_Identifier (Parent (S))))
21557 Check_Incomplete (Subtype_Mark (S));
21561 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21563 -- Explicit subtype declaration case
21565 if Nkind (P) = N_Subtype_Declaration then
21566 Def_Id := Defining_Identifier (P);
21568 -- Explicit derived type definition case
21570 elsif Nkind (P) = N_Derived_Type_Definition then
21571 Def_Id := Defining_Identifier (Parent (P));
21573 -- Implicit case, the Def_Id must be created as an implicit type.
21574 -- The one exception arises in the case of concurrent types, array
21575 -- and access types, where other subsidiary implicit types may be
21576 -- created and must appear before the main implicit type. In these
21577 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21578 -- has not yet been called to create Def_Id.
21581 if Is_Array_Type (Subtype_Mark_Id)
21582 or else Is_Concurrent_Type (Subtype_Mark_Id)
21583 or else Is_Access_Type (Subtype_Mark_Id)
21587 -- For the other cases, we create a new unattached Itype,
21588 -- and set the indication to ensure it gets attached later.
21592 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21596 -- If the kind of constraint is invalid for this kind of type,
21597 -- then give an error, and then pretend no constraint was given.
21599 if not Is_Valid_Constraint_Kind
21600 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21603 ("incorrect constraint for this kind of type", Constraint (S));
21605 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21607 -- Set Ekind of orphan itype, to prevent cascaded errors
21609 if Present (Def_Id) then
21610 Set_Ekind (Def_Id, Ekind (Any_Type));
21613 -- Make recursive call, having got rid of the bogus constraint
21615 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21618 -- Remaining processing depends on type. Select on Base_Type kind to
21619 -- ensure getting to the concrete type kind in the case of a private
21620 -- subtype (needed when only doing semantic analysis).
21622 case Ekind (Base_Type (Subtype_Mark_Id)) is
21623 when Access_Kind =>
21625 -- If this is a constraint on a class-wide type, discard it.
21626 -- There is currently no way to express a partial discriminant
21627 -- constraint on a type with unknown discriminants. This is
21628 -- a pathology that the ACATS wisely decides not to test.
21630 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21631 if Comes_From_Source (S) then
21633 ("constraint on class-wide type ignored??",
21637 if Nkind (P) = N_Subtype_Declaration then
21638 Set_Subtype_Indication (P,
21639 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21642 return Subtype_Mark_Id;
21645 Constrain_Access (Def_Id, S, Related_Nod);
21648 and then Is_Itype (Designated_Type (Def_Id))
21649 and then Nkind (Related_Nod) = N_Subtype_Declaration
21650 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21652 Build_Itype_Reference
21653 (Designated_Type (Def_Id), Related_Nod);
21657 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21659 when Decimal_Fixed_Point_Kind =>
21660 Constrain_Decimal (Def_Id, S);
21662 when Enumeration_Kind =>
21663 Constrain_Enumeration (Def_Id, S);
21665 when Ordinary_Fixed_Point_Kind =>
21666 Constrain_Ordinary_Fixed (Def_Id, S);
21669 Constrain_Float (Def_Id, S);
21671 when Integer_Kind =>
21672 Constrain_Integer (Def_Id, S);
21674 when Class_Wide_Kind
21675 | E_Incomplete_Type
21679 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21681 if Ekind (Def_Id) = E_Incomplete_Type then
21682 Set_Private_Dependents (Def_Id, New_Elmt_List);
21685 when Private_Kind =>
21687 -- A private type with unknown discriminants may be completed
21688 -- by an unconstrained array type.
21690 if Has_Unknown_Discriminants (Subtype_Mark_Id)
21691 and then Present (Full_View (Subtype_Mark_Id))
21692 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
21694 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21696 -- ... but more commonly is completed by a discriminated record
21700 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21703 -- The base type may be private but Def_Id may be a full view
21706 if Is_Private_Type (Def_Id) then
21707 Set_Private_Dependents (Def_Id, New_Elmt_List);
21710 -- In case of an invalid constraint prevent further processing
21711 -- since the type constructed is missing expected fields.
21713 if Etype (Def_Id) = Any_Type then
21717 -- If the full view is that of a task with discriminants,
21718 -- we must constrain both the concurrent type and its
21719 -- corresponding record type. Otherwise we will just propagate
21720 -- the constraint to the full view, if available.
21722 if Present (Full_View (Subtype_Mark_Id))
21723 and then Has_Discriminants (Subtype_Mark_Id)
21724 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21727 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21729 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21730 Constrain_Concurrent (Full_View_Id, S,
21731 Related_Nod, Related_Id, Suffix);
21732 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21733 Set_Full_View (Def_Id, Full_View_Id);
21735 -- Introduce an explicit reference to the private subtype,
21736 -- to prevent scope anomalies in gigi if first use appears
21737 -- in a nested context, e.g. a later function body.
21738 -- Should this be generated in other contexts than a full
21739 -- type declaration?
21741 if Is_Itype (Def_Id)
21743 Nkind (Parent (P)) = N_Full_Type_Declaration
21745 Build_Itype_Reference (Def_Id, Parent (P));
21749 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21752 when Concurrent_Kind =>
21753 Constrain_Concurrent (Def_Id, S,
21754 Related_Nod, Related_Id, Suffix);
21757 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21760 -- Size, Alignment, Representation aspects and Convention are always
21761 -- inherited from the base type.
21763 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21764 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
21765 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21767 -- The anonymous subtype created for the subtype indication
21768 -- inherits the predicates of the parent.
21770 if Has_Predicates (Subtype_Mark_Id) then
21771 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21773 -- Indicate where the predicate function may be found
21775 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
21776 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
21782 end Process_Subtype;
21784 -----------------------------
21785 -- Record_Type_Declaration --
21786 -----------------------------
21788 procedure Record_Type_Declaration
21793 Def : constant Node_Id := Type_Definition (N);
21794 Is_Tagged : Boolean;
21795 Tag_Comp : Entity_Id;
21798 -- These flags must be initialized before calling Process_Discriminants
21799 -- because this routine makes use of them.
21801 Set_Ekind (T, E_Record_Type);
21803 Init_Size_Align (T);
21804 Set_Interfaces (T, No_Elist);
21805 Set_Stored_Constraint (T, No_Elist);
21806 Set_Default_SSO (T);
21807 Set_No_Reordering (T, No_Component_Reordering);
21811 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21812 -- The flag Is_Tagged_Type might have already been set by
21813 -- Find_Type_Name if it detected an error for declaration T. This
21814 -- arises in the case of private tagged types where the full view
21815 -- omits the word tagged.
21818 Tagged_Present (Def)
21819 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21821 Set_Is_Limited_Record (T, Limited_Present (Def));
21824 Set_Is_Tagged_Type (T, True);
21825 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21828 -- Type is abstract if full declaration carries keyword, or if
21829 -- previous partial view did.
21831 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21832 or else Abstract_Present (Def));
21836 Analyze_Interface_Declaration (T, Def);
21838 if Present (Discriminant_Specifications (N)) then
21840 ("interface types cannot have discriminants",
21841 Defining_Identifier
21842 (First (Discriminant_Specifications (N))));
21846 -- First pass: if there are self-referential access components,
21847 -- create the required anonymous access type declarations, and if
21848 -- need be an incomplete type declaration for T itself.
21850 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21852 if Ada_Version >= Ada_2005
21853 and then Present (Interface_List (Def))
21855 Check_Interfaces (N, Def);
21858 Ifaces_List : Elist_Id;
21861 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21862 -- already in the parents.
21866 Ifaces_List => Ifaces_List,
21867 Exclude_Parents => True);
21869 Set_Interfaces (T, Ifaces_List);
21873 -- Records constitute a scope for the component declarations within.
21874 -- The scope is created prior to the processing of these declarations.
21875 -- Discriminants are processed first, so that they are visible when
21876 -- processing the other components. The Ekind of the record type itself
21877 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21879 -- Enter record scope
21883 -- If an incomplete or private type declaration was already given for
21884 -- the type, then this scope already exists, and the discriminants have
21885 -- been declared within. We must verify that the full declaration
21886 -- matches the incomplete one.
21888 Check_Or_Process_Discriminants (N, T, Prev);
21890 Set_Is_Constrained (T, not Has_Discriminants (T));
21891 Set_Has_Delayed_Freeze (T, True);
21893 -- For tagged types add a manually analyzed component corresponding
21894 -- to the component _tag, the corresponding piece of tree will be
21895 -- expanded as part of the freezing actions if it is not a CPP_Class.
21899 -- Do not add the tag unless we are in expansion mode
21901 if Expander_Active then
21902 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21903 Enter_Name (Tag_Comp);
21905 Set_Ekind (Tag_Comp, E_Component);
21906 Set_Is_Tag (Tag_Comp);
21907 Set_Is_Aliased (Tag_Comp);
21908 Set_Is_Independent (Tag_Comp);
21909 Set_Etype (Tag_Comp, RTE (RE_Tag));
21910 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21911 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21912 Init_Component_Location (Tag_Comp);
21914 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21915 -- implemented interfaces.
21917 if Has_Interfaces (T) then
21918 Add_Interface_Tag_Components (N, T);
21922 Make_Class_Wide_Type (T);
21923 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21926 -- We must suppress range checks when processing record components in
21927 -- the presence of discriminants, since we don't want spurious checks to
21928 -- be generated during their analysis, but Suppress_Range_Checks flags
21929 -- must be reset the after processing the record definition.
21931 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21932 -- couldn't we just use the normal range check suppression method here.
21933 -- That would seem cleaner ???
21935 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21936 Set_Kill_Range_Checks (T, True);
21937 Record_Type_Definition (Def, Prev);
21938 Set_Kill_Range_Checks (T, False);
21940 Record_Type_Definition (Def, Prev);
21943 -- Exit from record scope
21947 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21948 -- the implemented interfaces and associate them an aliased entity.
21951 and then not Is_Empty_List (Interface_List (Def))
21953 Derive_Progenitor_Subprograms (T, T);
21956 Check_Function_Writable_Actuals (N);
21957 end Record_Type_Declaration;
21959 ----------------------------
21960 -- Record_Type_Definition --
21961 ----------------------------
21963 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21964 Component : Entity_Id;
21965 Ctrl_Components : Boolean := False;
21966 Final_Storage_Only : Boolean;
21970 if Ekind (Prev_T) = E_Incomplete_Type then
21971 T := Full_View (Prev_T);
21976 Final_Storage_Only := not Is_Controlled (T);
21978 -- Ada 2005: Check whether an explicit Limited is present in a derived
21979 -- type declaration.
21981 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21982 and then Limited_Present (Parent (Def))
21984 Set_Is_Limited_Record (T);
21987 -- If the component list of a record type is defined by the reserved
21988 -- word null and there is no discriminant part, then the record type has
21989 -- no components and all records of the type are null records (RM 3.7)
21990 -- This procedure is also called to process the extension part of a
21991 -- record extension, in which case the current scope may have inherited
21995 and then Present (Component_List (Def))
21996 and then not Null_Present (Component_List (Def))
21998 Analyze_Declarations (Component_Items (Component_List (Def)));
22000 if Present (Variant_Part (Component_List (Def))) then
22001 Analyze (Variant_Part (Component_List (Def)));
22005 -- After completing the semantic analysis of the record definition,
22006 -- record components, both new and inherited, are accessible. Set their
22007 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22008 -- whose Ekind may be void.
22010 Component := First_Entity (Current_Scope);
22011 while Present (Component) loop
22012 if Ekind (Component) = E_Void
22013 and then not Is_Itype (Component)
22015 Set_Ekind (Component, E_Component);
22016 Init_Component_Location (Component);
22019 Propagate_Concurrent_Flags (T, Etype (Component));
22021 if Ekind (Component) /= E_Component then
22024 -- Do not set Has_Controlled_Component on a class-wide equivalent
22025 -- type. See Make_CW_Equivalent_Type.
22027 elsif not Is_Class_Wide_Equivalent_Type (T)
22028 and then (Has_Controlled_Component (Etype (Component))
22029 or else (Chars (Component) /= Name_uParent
22030 and then Is_Controlled (Etype (Component))))
22032 Set_Has_Controlled_Component (T, True);
22033 Final_Storage_Only :=
22035 and then Finalize_Storage_Only (Etype (Component));
22036 Ctrl_Components := True;
22039 Next_Entity (Component);
22042 -- A Type is Finalize_Storage_Only only if all its controlled components
22045 if Ctrl_Components then
22046 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22049 -- Place reference to end record on the proper entity, which may
22050 -- be a partial view.
22052 if Present (Def) then
22053 Process_End_Label (Def, 'e', Prev_T);
22055 end Record_Type_Definition;
22057 ---------------------------
22058 -- Replace_Discriminants --
22059 ---------------------------
22061 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id) is
22062 function Process (N : Node_Id) return Traverse_Result;
22068 function Process (N : Node_Id) return Traverse_Result is
22072 if Nkind (N) = N_Discriminant_Specification then
22073 Comp := First_Discriminant (Typ);
22074 while Present (Comp) loop
22075 if Original_Record_Component (Comp) = Defining_Identifier (N)
22076 or else Chars (Comp) = Chars (Defining_Identifier (N))
22078 Set_Defining_Identifier (N, Comp);
22082 Next_Discriminant (Comp);
22085 elsif Nkind (N) = N_Variant_Part then
22086 Comp := First_Discriminant (Typ);
22087 while Present (Comp) loop
22088 if Original_Record_Component (Comp) = Entity (Name (N))
22089 or else Chars (Comp) = Chars (Name (N))
22091 -- Make sure to preserve the type coming from the parent on
22092 -- the Name, even if the subtype of the discriminant can be
22093 -- constrained, so that discrete choices inherited from the
22094 -- parent in the variant part are not flagged as violating
22095 -- the constraints of the subtype.
22098 Typ : constant Entity_Id := Etype (Name (N));
22100 Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
22101 Set_Etype (Name (N), Typ);
22106 Next_Discriminant (Comp);
22113 procedure Replace is new Traverse_Proc (Process);
22115 -- Start of processing for Replace_Discriminants
22119 end Replace_Discriminants;
22121 -------------------------------
22122 -- Set_Completion_Referenced --
22123 -------------------------------
22125 procedure Set_Completion_Referenced (E : Entity_Id) is
22127 -- If in main unit, mark entity that is a completion as referenced,
22128 -- warnings go on the partial view when needed.
22130 if In_Extended_Main_Source_Unit (E) then
22131 Set_Referenced (E);
22133 end Set_Completion_Referenced;
22135 ---------------------
22136 -- Set_Default_SSO --
22137 ---------------------
22139 procedure Set_Default_SSO (T : Entity_Id) is
22141 case Opt.Default_SSO is
22145 Set_SSO_Set_Low_By_Default (T, True);
22147 Set_SSO_Set_High_By_Default (T, True);
22149 raise Program_Error;
22151 end Set_Default_SSO;
22153 ---------------------
22154 -- Set_Fixed_Range --
22155 ---------------------
22157 -- The range for fixed-point types is complicated by the fact that we
22158 -- do not know the exact end points at the time of the declaration. This
22159 -- is true for three reasons:
22161 -- A size clause may affect the fudging of the end-points.
22162 -- A small clause may affect the values of the end-points.
22163 -- We try to include the end-points if it does not affect the size.
22165 -- This means that the actual end-points must be established at the
22166 -- point when the type is frozen. Meanwhile, we first narrow the range
22167 -- as permitted (so that it will fit if necessary in a small specified
22168 -- size), and then build a range subtree with these narrowed bounds.
22169 -- Set_Fixed_Range constructs the range from real literal values, and
22170 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22172 -- The parent of this range is set to point to the entity so that it is
22173 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22174 -- other scalar types, which are just pointers to the range in the
22175 -- original tree, this would otherwise be an orphan).
22177 -- The tree is left unanalyzed. When the type is frozen, the processing
22178 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22179 -- analyzed, and uses this as an indication that it should complete
22180 -- work on the range (it will know the final small and size values).
22182 procedure Set_Fixed_Range
22188 S : constant Node_Id :=
22190 Low_Bound => Make_Real_Literal (Loc, Lo),
22191 High_Bound => Make_Real_Literal (Loc, Hi));
22193 Set_Scalar_Range (E, S);
22196 -- Before the freeze point, the bounds of a fixed point are universal
22197 -- and carry the corresponding type.
22199 Set_Etype (Low_Bound (S), Universal_Real);
22200 Set_Etype (High_Bound (S), Universal_Real);
22201 end Set_Fixed_Range;
22203 ----------------------------------
22204 -- Set_Scalar_Range_For_Subtype --
22205 ----------------------------------
22207 procedure Set_Scalar_Range_For_Subtype
22208 (Def_Id : Entity_Id;
22212 Kind : constant Entity_Kind := Ekind (Def_Id);
22215 -- Defend against previous error
22217 if Nkind (R) = N_Error then
22221 Set_Scalar_Range (Def_Id, R);
22223 -- We need to link the range into the tree before resolving it so
22224 -- that types that are referenced, including importantly the subtype
22225 -- itself, are properly frozen (Freeze_Expression requires that the
22226 -- expression be properly linked into the tree). Of course if it is
22227 -- already linked in, then we do not disturb the current link.
22229 if No (Parent (R)) then
22230 Set_Parent (R, Def_Id);
22233 -- Reset the kind of the subtype during analysis of the range, to
22234 -- catch possible premature use in the bounds themselves.
22236 Set_Ekind (Def_Id, E_Void);
22237 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22238 Set_Ekind (Def_Id, Kind);
22239 end Set_Scalar_Range_For_Subtype;
22241 --------------------------------------------------------
22242 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22243 --------------------------------------------------------
22245 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22249 -- Make sure set if encountered during Expand_To_Stored_Constraint
22251 Set_Stored_Constraint (E, No_Elist);
22253 -- Give it the right value
22255 if Is_Constrained (E) and then Has_Discriminants (E) then
22256 Set_Stored_Constraint (E,
22257 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22259 end Set_Stored_Constraint_From_Discriminant_Constraint;
22261 -------------------------------------
22262 -- Signed_Integer_Type_Declaration --
22263 -------------------------------------
22265 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22266 Implicit_Base : Entity_Id;
22267 Base_Typ : Entity_Id;
22270 Errs : Boolean := False;
22274 function Can_Derive_From (E : Entity_Id) return Boolean;
22275 -- Determine whether given bounds allow derivation from specified type
22277 procedure Check_Bound (Expr : Node_Id);
22278 -- Check bound to make sure it is integral and static. If not, post
22279 -- appropriate error message and set Errs flag
22281 ---------------------
22282 -- Can_Derive_From --
22283 ---------------------
22285 -- Note we check both bounds against both end values, to deal with
22286 -- strange types like ones with a range of 0 .. -12341234.
22288 function Can_Derive_From (E : Entity_Id) return Boolean is
22289 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22290 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22292 return Lo <= Lo_Val and then Lo_Val <= Hi
22294 Lo <= Hi_Val and then Hi_Val <= Hi;
22295 end Can_Derive_From;
22301 procedure Check_Bound (Expr : Node_Id) is
22303 -- If a range constraint is used as an integer type definition, each
22304 -- bound of the range must be defined by a static expression of some
22305 -- integer type, but the two bounds need not have the same integer
22306 -- type (Negative bounds are allowed.) (RM 3.5.4)
22308 if not Is_Integer_Type (Etype (Expr)) then
22310 ("integer type definition bounds must be of integer type", Expr);
22313 elsif not Is_OK_Static_Expression (Expr) then
22314 Flag_Non_Static_Expr
22315 ("non-static expression used for integer type bound!", Expr);
22318 -- Otherwise the bounds are folded into literals
22320 elsif Is_Entity_Name (Expr) then
22321 Fold_Uint (Expr, Expr_Value (Expr), True);
22325 -- Start of processing for Signed_Integer_Type_Declaration
22328 -- Create an anonymous base type
22331 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22333 -- Analyze and check the bounds, they can be of any integer type
22335 Lo := Low_Bound (Def);
22336 Hi := High_Bound (Def);
22338 -- Arbitrarily use Integer as the type if either bound had an error
22340 if Hi = Error or else Lo = Error then
22341 Base_Typ := Any_Integer;
22342 Set_Error_Posted (T, True);
22345 -- Here both bounds are OK expressions
22348 Analyze_And_Resolve (Lo, Any_Integer);
22349 Analyze_And_Resolve (Hi, Any_Integer);
22355 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22356 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22359 -- Find type to derive from
22361 Lo_Val := Expr_Value (Lo);
22362 Hi_Val := Expr_Value (Hi);
22364 if Can_Derive_From (Standard_Short_Short_Integer) then
22365 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22367 elsif Can_Derive_From (Standard_Short_Integer) then
22368 Base_Typ := Base_Type (Standard_Short_Integer);
22370 elsif Can_Derive_From (Standard_Integer) then
22371 Base_Typ := Base_Type (Standard_Integer);
22373 elsif Can_Derive_From (Standard_Long_Integer) then
22374 Base_Typ := Base_Type (Standard_Long_Integer);
22376 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22377 Check_Restriction (No_Long_Long_Integers, Def);
22378 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22381 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22382 Error_Msg_N ("integer type definition bounds out of range", Def);
22383 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22384 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22388 -- Set the type of the bounds to the implicit base: we cannot set it to
22389 -- the new type, because this would be a forward reference for the code
22390 -- generator and, if the original type is user-defined, this could even
22391 -- lead to spurious semantic errors. Furthermore we do not set it to be
22392 -- universal, because this could make it much larger than needed here.
22395 Set_Etype (Lo, Implicit_Base);
22396 Set_Etype (Hi, Implicit_Base);
22399 -- Complete both implicit base and declared first subtype entities. The
22400 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22401 -- are not clobbered when the signed integer type acts as a full view of
22404 Set_Etype (Implicit_Base, Base_Typ);
22405 Set_Size_Info (Implicit_Base, Base_Typ);
22406 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22407 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22408 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22410 Set_Ekind (T, E_Signed_Integer_Subtype);
22411 Set_Etype (T, Implicit_Base);
22412 Set_Size_Info (T, Implicit_Base);
22413 Inherit_Rep_Item_Chain (T, Implicit_Base);
22414 Set_Scalar_Range (T, Def);
22415 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22416 Set_Is_Constrained (T);
22417 end Signed_Integer_Type_Declaration;