1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2021, 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 Einfo.Entities; use Einfo.Entities;
34 with Einfo.Utils; use Einfo.Utils;
35 with Errout; use Errout;
36 with Eval_Fat; use Eval_Fat;
37 with Exp_Ch3; use Exp_Ch3;
38 with Exp_Ch9; use Exp_Ch9;
39 with Exp_Disp; use Exp_Disp;
40 with Exp_Dist; use Exp_Dist;
41 with Exp_Tss; use Exp_Tss;
42 with Exp_Util; use Exp_Util;
43 with Freeze; use Freeze;
44 with Ghost; use Ghost;
45 with Itypes; use Itypes;
46 with Layout; use Layout;
48 with Lib.Xref; use Lib.Xref;
49 with Namet; use Namet;
50 with Nlists; use Nlists;
51 with Nmake; use Nmake;
53 with Restrict; use Restrict;
54 with Rident; use Rident;
55 with Rtsfind; use Rtsfind;
57 with Sem_Aux; use Sem_Aux;
58 with Sem_Case; use Sem_Case;
59 with Sem_Cat; use Sem_Cat;
60 with Sem_Ch6; use Sem_Ch6;
61 with Sem_Ch7; use Sem_Ch7;
62 with Sem_Ch8; use Sem_Ch8;
63 with Sem_Ch13; use Sem_Ch13;
64 with Sem_Dim; use Sem_Dim;
65 with Sem_Disp; use Sem_Disp;
66 with Sem_Dist; use Sem_Dist;
67 with Sem_Elab; use Sem_Elab;
68 with Sem_Elim; use Sem_Elim;
69 with Sem_Eval; use Sem_Eval;
70 with Sem_Mech; use Sem_Mech;
71 with Sem_Res; use Sem_Res;
72 with Sem_Smem; use Sem_Smem;
73 with Sem_Type; use Sem_Type;
74 with Sem_Util; use Sem_Util;
75 with Sem_Warn; use Sem_Warn;
76 with Stand; use Stand;
77 with Sinfo; use Sinfo;
78 with Sinfo.Nodes; use Sinfo.Nodes;
79 with Sinfo.Utils; use Sinfo.Utils;
80 with Sinput; use Sinput;
81 with Snames; use Snames;
82 with Targparm; use Targparm;
83 with Tbuild; use Tbuild;
84 with Ttypes; use Ttypes;
85 with Uintp; use Uintp;
86 with Urealp; use Urealp;
88 package body Sem_Ch3 is
90 -----------------------
91 -- Local Subprograms --
92 -----------------------
94 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
95 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
96 -- abstract interface types implemented by a record type or a derived
99 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id);
100 -- When an access-to-subprogram type has pre/postconditions, we build a
101 -- subprogram that includes these contracts and is invoked by an indirect
102 -- call through the corresponding access type.
104 procedure Build_Derived_Type
106 Parent_Type : Entity_Id;
107 Derived_Type : Entity_Id;
108 Is_Completion : Boolean;
109 Derive_Subps : Boolean := True);
110 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
111 -- the N_Full_Type_Declaration node containing the derived type definition.
112 -- Parent_Type is the entity for the parent type in the derived type
113 -- definition and Derived_Type the actual derived type. Is_Completion must
114 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
115 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
116 -- completion of a private type declaration. If Is_Completion is set to
117 -- True, N is the completion of a private type declaration and Derived_Type
118 -- is different from the defining identifier inside N (i.e. Derived_Type /=
119 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
120 -- subprograms should be derived. The only case where this parameter is
121 -- False is when Build_Derived_Type is recursively called to process an
122 -- implicit derived full type for a type derived from a private type (in
123 -- that case the subprograms must only be derived for the private view of
126 -- ??? These flags need a bit of re-examination and re-documentation:
127 -- ??? are they both necessary (both seem related to the recursion)?
129 procedure Build_Derived_Access_Type
131 Parent_Type : Entity_Id;
132 Derived_Type : Entity_Id);
133 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
134 -- create an implicit base if the parent type is constrained or if the
135 -- subtype indication has a constraint.
137 procedure Build_Derived_Array_Type
139 Parent_Type : Entity_Id;
140 Derived_Type : Entity_Id);
141 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
142 -- create an implicit base if the parent type is constrained or if the
143 -- subtype indication has a constraint.
145 procedure Build_Derived_Concurrent_Type
147 Parent_Type : Entity_Id;
148 Derived_Type : Entity_Id);
149 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
150 -- protected type, inherit entries and protected subprograms, check
151 -- legality of discriminant constraints if any.
153 procedure Build_Derived_Enumeration_Type
155 Parent_Type : Entity_Id;
156 Derived_Type : Entity_Id);
157 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
158 -- type, we must create a new list of literals. Types derived from
159 -- Character and [Wide_]Wide_Character are special-cased.
161 procedure Build_Derived_Numeric_Type
163 Parent_Type : Entity_Id;
164 Derived_Type : Entity_Id);
165 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
166 -- an anonymous base type, and propagate constraint to subtype if needed.
168 procedure Build_Derived_Private_Type
170 Parent_Type : Entity_Id;
171 Derived_Type : Entity_Id;
172 Is_Completion : Boolean;
173 Derive_Subps : Boolean := True);
174 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
175 -- because the parent may or may not have a completion, and the derivation
176 -- may itself be a completion.
178 procedure Build_Derived_Record_Type
180 Parent_Type : Entity_Id;
181 Derived_Type : Entity_Id;
182 Derive_Subps : Boolean := True);
183 -- Subsidiary procedure used for tagged and untagged record types
184 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
185 -- All parameters are as in Build_Derived_Type except that N, in
186 -- addition to being an N_Full_Type_Declaration node, can also be an
187 -- N_Private_Extension_Declaration node. See the definition of this routine
188 -- for much more info. Derive_Subps indicates whether subprograms should be
189 -- derived from the parent type. The only case where Derive_Subps is False
190 -- is for an implicit derived full type for a type derived from a private
191 -- type (see Build_Derived_Type).
193 procedure Build_Discriminal (Discrim : Entity_Id);
194 -- Create the discriminal corresponding to discriminant Discrim, that is
195 -- the parameter corresponding to Discrim to be used in initialization
196 -- procedures for the type where Discrim is a discriminant. Discriminals
197 -- are not used during semantic analysis, and are not fully defined
198 -- entities until expansion. Thus they are not given a scope until
199 -- initialization procedures are built.
201 function Build_Discriminant_Constraints
204 Derived_Def : Boolean := False) return Elist_Id;
205 -- Validate discriminant constraints and return the list of the constraints
206 -- in order of discriminant declarations, where T is the discriminated
207 -- unconstrained type. Def is the N_Subtype_Indication node where the
208 -- discriminants constraints for T are specified. Derived_Def is True
209 -- when building the discriminant constraints in a derived type definition
210 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
211 -- type and Def is the constraint "(xxx)" on T and this routine sets the
212 -- Corresponding_Discriminant field of the discriminants in the derived
213 -- type D to point to the corresponding discriminants in the parent type T.
215 procedure Build_Discriminated_Subtype
219 Related_Nod : Node_Id;
220 For_Access : Boolean := False);
221 -- Subsidiary procedure to Constrain_Discriminated_Type and to
222 -- Process_Incomplete_Dependents. Given
224 -- T (a possibly discriminated base type)
225 -- Def_Id (a very partially built subtype for T),
227 -- the call completes Def_Id to be the appropriate E_*_Subtype.
229 -- The Elist is the list of discriminant constraints if any (it is set
230 -- to No_Elist if T is not a discriminated type, and to an empty list if
231 -- T has discriminants but there are no discriminant constraints). The
232 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
233 -- The For_Access says whether or not this subtype is really constraining
236 function Build_Scalar_Bound
239 Der_T : Entity_Id) return Node_Id;
240 -- The bounds of a derived scalar type are conversions of the bounds of
241 -- the parent type. Optimize the representation if the bounds are literals.
242 -- Needs a more complete spec--what are the parameters exactly, and what
243 -- exactly is the returned value, and how is Bound affected???
245 procedure Check_Access_Discriminant_Requires_Limited
248 -- Check the restriction that the type to which an access discriminant
249 -- belongs must be a concurrent type or a descendant of a type with
250 -- the reserved word 'limited' in its declaration.
252 procedure Check_Anonymous_Access_Component
257 Access_Def : Node_Id);
258 -- Ada 2005 AI-382: an access component in a record definition can refer to
259 -- the enclosing record, in which case it denotes the type itself, and not
260 -- the current instance of the type. We create an anonymous access type for
261 -- the component, and flag it as an access to a component, so accessibility
262 -- checks are properly performed on it. The declaration of the access type
263 -- is placed ahead of that of the record to prevent order-of-elaboration
264 -- circularity issues in Gigi. We create an incomplete type for the record
265 -- declaration, which is the designated type of the anonymous access.
267 procedure Check_Anonymous_Access_Components
271 Comp_List : Node_Id);
272 -- Call Check_Anonymous_Access_Component on Comp_List
274 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id);
275 -- Check that, if a new discriminant is used in a constraint defining the
276 -- parent subtype of a derivation, its subtype is statically compatible
277 -- with the subtype of the corresponding parent discriminant (RM 3.7(15)).
279 procedure Check_Delta_Expression (E : Node_Id);
280 -- Check that the expression represented by E is suitable for use as a
281 -- delta expression, i.e. it is of real type and is static.
283 procedure Check_Digits_Expression (E : Node_Id);
284 -- Check that the expression represented by E is suitable for use as a
285 -- digits expression, i.e. it is of integer type, positive and static.
287 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
288 -- Validate the initialization of an object declaration. T is the required
289 -- type, and Exp is the initialization expression.
291 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
292 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
294 procedure Check_Or_Process_Discriminants
297 Prev : Entity_Id := Empty);
298 -- If N is the full declaration of the completion T of an incomplete or
299 -- private type, check its discriminants (which are already known to be
300 -- conformant with those of the partial view, see Find_Type_Name),
301 -- otherwise process them. Prev is the entity of the partial declaration,
304 procedure Check_Real_Bound (Bound : Node_Id);
305 -- Check given bound for being of real type and static. If not, post an
306 -- appropriate message, and rewrite the bound with the real literal zero.
308 procedure Constant_Redeclaration
312 -- Various checks on legality of full declaration of deferred constant.
313 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
314 -- node. The caller has not yet set any attributes of this entity.
316 function Contain_Interface
318 Ifaces : Elist_Id) return Boolean;
319 -- Ada 2005: Determine whether Iface is present in the list Ifaces
321 procedure Convert_Scalar_Bounds
323 Parent_Type : Entity_Id;
324 Derived_Type : Entity_Id;
326 -- For derived scalar types, convert the bounds in the type definition to
327 -- the derived type, and complete their analysis. Given a constraint of the
328 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
329 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
330 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
331 -- subtype are conversions of those bounds to the derived_type, so that
332 -- their typing is consistent.
334 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
335 -- Copies attributes from array base type T2 to array base type T1. Copies
336 -- only attributes that apply to base types, but not subtypes.
338 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
339 -- Copies attributes from array subtype T2 to array subtype T1. Copies
340 -- attributes that apply to both subtypes and base types.
342 procedure Create_Constrained_Components
346 Constraints : Elist_Id);
347 -- Build the list of entities for a constrained discriminated record
348 -- subtype. If a component depends on a discriminant, replace its subtype
349 -- using the discriminant values in the discriminant constraint. Subt
350 -- is the defining identifier for the subtype whose list of constrained
351 -- entities we will create. Decl_Node is the type declaration node where
352 -- we will attach all the itypes created. Typ is the base discriminated
353 -- type for the subtype Subt. Constraints is the list of discriminant
354 -- constraints for Typ.
356 function Constrain_Component_Type
358 Constrained_Typ : Entity_Id;
359 Related_Node : Node_Id;
361 Constraints : Elist_Id) return Entity_Id;
362 -- Given a discriminated base type Typ, a list of discriminant constraints,
363 -- Constraints, for Typ and a component Comp of Typ, create and return the
364 -- type corresponding to Etype (Comp) where all discriminant references
365 -- are replaced with the corresponding constraint. If Etype (Comp) contains
366 -- no discriminant references then it is returned as-is. Constrained_Typ
367 -- is the final constrained subtype to which the constrained component
368 -- belongs. Related_Node is the node where we attach all created itypes.
370 procedure Constrain_Access
371 (Def_Id : in out Entity_Id;
373 Related_Nod : Node_Id);
374 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
375 -- an anonymous type created for a subtype indication. In that case it is
376 -- created in the procedure and attached to Related_Nod.
378 procedure Constrain_Array
379 (Def_Id : in out Entity_Id;
381 Related_Nod : Node_Id;
382 Related_Id : Entity_Id;
384 -- Apply a list of index constraints to an unconstrained array type. The
385 -- first parameter is the entity for the resulting subtype. A value of
386 -- Empty for Def_Id indicates that an implicit type must be created, but
387 -- creation is delayed (and must be done by this procedure) because other
388 -- subsidiary implicit types must be created first (which is why Def_Id
389 -- is an in/out parameter). The second parameter is a subtype indication
390 -- node for the constrained array to be created (e.g. something of the
391 -- form string (1 .. 10)). Related_Nod gives the place where this type
392 -- has to be inserted in the tree. The Related_Id and Suffix parameters
393 -- are used to build the associated Implicit type name.
395 procedure Constrain_Concurrent
396 (Def_Id : in out Entity_Id;
398 Related_Nod : Node_Id;
399 Related_Id : Entity_Id;
401 -- Apply list of discriminant constraints to an unconstrained concurrent
404 -- SI is the N_Subtype_Indication node containing the constraint and
405 -- the unconstrained type to constrain.
407 -- Def_Id is the entity for the resulting constrained subtype. A value
408 -- of Empty for Def_Id indicates that an implicit type must be created,
409 -- but creation is delayed (and must be done by this procedure) because
410 -- other subsidiary implicit types must be created first (which is why
411 -- Def_Id is an in/out parameter).
413 -- Related_Nod gives the place where this type has to be inserted
416 -- The last two arguments are used to create its external name if needed.
418 function Constrain_Corresponding_Record
419 (Prot_Subt : Entity_Id;
420 Corr_Rec : Entity_Id;
421 Related_Nod : Node_Id) return Entity_Id;
422 -- When constraining a protected type or task type with discriminants,
423 -- constrain the corresponding record with the same discriminant values.
425 procedure Constrain_Decimal (Def_Id : Entity_Id; S : Node_Id);
426 -- Constrain a decimal fixed point type with a digits constraint and/or a
427 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
429 procedure Constrain_Discriminated_Type
432 Related_Nod : Node_Id;
433 For_Access : Boolean := False);
434 -- Process discriminant constraints of composite type. Verify that values
435 -- have been provided for all discriminants, that the original type is
436 -- unconstrained, and that the types of the supplied expressions match
437 -- the discriminant types. The first three parameters are like in routine
438 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
441 procedure Constrain_Enumeration (Def_Id : Entity_Id; S : Node_Id);
442 -- Constrain an enumeration type with a range constraint. This is identical
443 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
445 procedure Constrain_Float (Def_Id : Entity_Id; S : Node_Id);
446 -- Constrain a floating point type with either a digits constraint
447 -- and/or a range constraint, building a E_Floating_Point_Subtype.
449 procedure Constrain_Index
452 Related_Nod : Node_Id;
453 Related_Id : Entity_Id;
456 -- Process an index constraint S in a constrained array declaration. The
457 -- constraint can be a subtype name, or a range with or without an explicit
458 -- subtype mark. The index is the corresponding index of the unconstrained
459 -- array. The Related_Id and Suffix parameters are used to build the
460 -- associated Implicit type name.
462 procedure Constrain_Integer (Def_Id : Entity_Id; S : Node_Id);
463 -- Build subtype of a signed or modular integer type
465 procedure Constrain_Ordinary_Fixed (Def_Id : Entity_Id; S : Node_Id);
466 -- Constrain an ordinary fixed point type with a range constraint, and
467 -- build an E_Ordinary_Fixed_Point_Subtype entity.
469 procedure Copy_And_Swap (Priv, Full : Entity_Id);
470 -- Copy the Priv entity into the entity of its full declaration then swap
471 -- the two entities in such a manner that the former private type is now
472 -- seen as a full type.
474 procedure Decimal_Fixed_Point_Type_Declaration
477 -- Create a new decimal fixed point type, and apply the constraint to
478 -- obtain a subtype of this new type.
480 procedure Complete_Private_Subtype
483 Full_Base : Entity_Id;
484 Related_Nod : Node_Id);
485 -- Complete the implicit full view of a private subtype by setting the
486 -- appropriate semantic fields. If the full view of the parent is a record
487 -- type, build constrained components of subtype.
489 procedure Derive_Progenitor_Subprograms
490 (Parent_Type : Entity_Id;
491 Tagged_Type : Entity_Id);
492 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
493 -- operations of progenitors of Tagged_Type, and replace the subsidiary
494 -- subtypes with Tagged_Type, to build the specs of the inherited interface
495 -- primitives. The derived primitives are aliased to those of the
496 -- interface. This routine takes care also of transferring to the full view
497 -- subprograms associated with the partial view of Tagged_Type that cover
498 -- interface primitives.
500 procedure Derived_Standard_Character
502 Parent_Type : Entity_Id;
503 Derived_Type : Entity_Id);
504 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
505 -- derivations from types Standard.Character and Standard.Wide_Character.
507 procedure Derived_Type_Declaration
510 Is_Completion : Boolean);
511 -- Process a derived type declaration. Build_Derived_Type is invoked
512 -- to process the actual derived type definition. Parameters N and
513 -- Is_Completion have the same meaning as in Build_Derived_Type.
514 -- T is the N_Defining_Identifier for the entity defined in the
515 -- N_Full_Type_Declaration node N, that is T is the derived type.
517 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
518 -- Insert each literal in symbol table, as an overloadable identifier. Each
519 -- enumeration type is mapped into a sequence of integers, and each literal
520 -- is defined as a constant with integer value. If any of the literals are
521 -- character literals, the type is a character type, which means that
522 -- strings are legal aggregates for arrays of components of the type.
524 function Expand_To_Stored_Constraint
526 Constraint : Elist_Id) return Elist_Id;
527 -- Given a constraint (i.e. a list of expressions) on the discriminants of
528 -- Typ, expand it into a constraint on the stored discriminants and return
529 -- the new list of expressions constraining the stored discriminants.
531 function Find_Type_Of_Object
533 Related_Nod : Node_Id) return Entity_Id;
534 -- Get type entity for object referenced by Obj_Def, attaching the implicit
535 -- types generated to Related_Nod.
537 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
538 -- Create a new float and apply the constraint to obtain subtype of it
540 function Has_Range_Constraint (N : Node_Id) return Boolean;
541 -- Given an N_Subtype_Indication node N, return True if a range constraint
542 -- is present, either directly, or as part of a digits or delta constraint.
543 -- In addition, a digits constraint in the decimal case returns True, since
544 -- it establishes a default range if no explicit range is present.
546 function Inherit_Components
548 Parent_Base : Entity_Id;
549 Derived_Base : Entity_Id;
551 Inherit_Discr : Boolean;
552 Discs : Elist_Id) return Elist_Id;
553 -- Called from Build_Derived_Record_Type to inherit the components of
554 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
555 -- For more information on derived types and component inheritance please
556 -- consult the comment above the body of Build_Derived_Record_Type.
558 -- N is the original derived type declaration
560 -- Is_Tagged is set if we are dealing with tagged types
562 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
563 -- Parent_Base, otherwise no discriminants are inherited.
565 -- Discs gives the list of constraints that apply to Parent_Base in the
566 -- derived type declaration. If Discs is set to No_Elist, then we have
567 -- the following situation:
569 -- type Parent (D1..Dn : ..) is [tagged] record ...;
570 -- type Derived is new Parent [with ...];
572 -- which gets treated as
574 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
576 -- For untagged types the returned value is an association list. The list
577 -- starts from the association (Parent_Base => Derived_Base), and then it
578 -- contains a sequence of the associations of the form
580 -- (Old_Component => New_Component),
582 -- where Old_Component is the Entity_Id of a component in Parent_Base and
583 -- New_Component is the Entity_Id of the corresponding component in
584 -- Derived_Base. For untagged records, this association list is needed when
585 -- copying the record declaration for the derived base. In the tagged case
586 -- the value returned is irrelevant.
588 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
589 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
590 -- Determine whether subprogram Subp is a procedure subject to pragma
591 -- Extensions_Visible with value False and has at least one controlling
592 -- parameter of mode OUT.
594 function Is_Private_Primitive (Prim : Entity_Id) return Boolean;
595 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
596 -- When applied to a primitive subprogram Prim, returns True if Prim is
597 -- declared as a private operation within a package or generic package,
598 -- and returns False otherwise.
600 function Is_Valid_Constraint_Kind
602 Constraint_Kind : Node_Kind) return Boolean;
603 -- Returns True if it is legal to apply the given kind of constraint to the
604 -- given kind of type (index constraint to an array type, for example).
606 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
607 -- Create new modular type. Verify that modulus is in bounds
609 procedure New_Concatenation_Op (Typ : Entity_Id);
610 -- Create an abbreviated declaration for an operator in order to
611 -- materialize concatenation on array types.
613 procedure Ordinary_Fixed_Point_Type_Declaration
616 -- Create a new ordinary fixed point type, and apply the constraint to
617 -- obtain subtype of it.
619 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id);
620 -- Wrapper on Preanalyze_Spec_Expression for default expressions, so that
621 -- In_Default_Expr can be properly adjusted.
623 procedure Prepare_Private_Subtype_Completion
625 Related_Nod : Node_Id);
626 -- Id is a subtype of some private type. Creates the full declaration
627 -- associated with Id whenever possible, i.e. when the full declaration
628 -- of the base type is already known. Records each subtype into
629 -- Private_Dependents of the base type.
631 procedure Process_Incomplete_Dependents
635 -- Process all entities that depend on an incomplete type. There include
636 -- subtypes, subprogram types that mention the incomplete type in their
637 -- profiles, and subprogram with access parameters that designate the
640 -- Inc_T is the defining identifier of an incomplete type declaration, its
641 -- Ekind is E_Incomplete_Type.
643 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
645 -- Full_T is N's defining identifier.
647 -- Subtypes of incomplete types with discriminants are completed when the
648 -- parent type is. This is simpler than private subtypes, because they can
649 -- only appear in the same scope, and there is no need to exchange views.
650 -- Similarly, access_to_subprogram types may have a parameter or a return
651 -- type that is an incomplete type, and that must be replaced with the
654 -- If the full type is tagged, subprogram with access parameters that
655 -- designated the incomplete may be primitive operations of the full type,
656 -- and have to be processed accordingly.
658 procedure Process_Real_Range_Specification (Def : Node_Id);
659 -- Given the type definition for a real type, this procedure processes and
660 -- checks the real range specification of this type definition if one is
661 -- present. If errors are found, error messages are posted, and the
662 -- Real_Range_Specification of Def is reset to Empty.
664 procedure Record_Type_Declaration
668 -- Process a record type declaration (for both untagged and tagged
669 -- records). Parameters T and N are exactly like in procedure
670 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
671 -- for this routine. If this is the completion of an incomplete type
672 -- declaration, Prev is the entity of the incomplete declaration, used for
673 -- cross-referencing. Otherwise Prev = T.
675 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
676 -- This routine is used to process the actual record type definition (both
677 -- for untagged and tagged records). Def is a record type definition node.
678 -- This procedure analyzes the components in this record type definition.
679 -- Prev_T is the entity for the enclosing record type. It is provided so
680 -- that its Has_Task flag can be set if any of the component have Has_Task
681 -- set. If the declaration is the completion of an incomplete type
682 -- declaration, Prev_T is the original incomplete type, whose full view is
685 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id);
686 -- Subsidiary to Build_Derived_Record_Type. For untagged record types, we
687 -- first create the list of components for the derived type from that of
688 -- the parent by means of Inherit_Components and then build a copy of the
689 -- declaration tree of the parent with the help of the mapping returned by
690 -- Inherit_Components, which will for example be used to validate record
691 -- representation clauses given for the derived type. If the parent type
692 -- is private and has discriminants, the ancestor discriminants used in the
693 -- inheritance are that of the private declaration, whereas the ancestor
694 -- discriminants present in the declaration tree of the parent are that of
695 -- the full declaration; as a consequence, the remapping done during the
696 -- copy will leave the references to the ancestor discriminants unchanged
697 -- in the declaration tree and they need to be fixed up. If the derived
698 -- type has a known discriminant part, then the remapping done during the
699 -- copy will only create references to the girder discriminants and they
700 -- need to be replaced with references to the non-girder discriminants.
702 procedure Set_Fixed_Range
707 -- Build a range node with the given bounds and set it as the Scalar_Range
708 -- of the given fixed-point type entity. Loc is the source location used
709 -- for the constructed range. See body for further details.
711 procedure Set_Scalar_Range_For_Subtype
715 -- This routine is used to set the scalar range field for a subtype given
716 -- Def_Id, the entity for the subtype, and R, the range expression for the
717 -- scalar range. Subt provides the parent subtype to be used to analyze,
718 -- resolve, and check the given range.
720 procedure Set_Default_SSO (T : Entity_Id);
721 -- T is the entity for an array or record being declared. This procedure
722 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
723 -- to the setting of Opt.Default_SSO.
725 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
726 -- Create a new signed integer entity, and apply the constraint to obtain
727 -- the required first named subtype of this type.
729 procedure Set_Stored_Constraint_From_Discriminant_Constraint
731 -- E is some record type. This routine computes E's Stored_Constraint
732 -- from its Discriminant_Constraint.
734 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
735 -- Check that an entity in a list of progenitors is an interface,
736 -- emit error otherwise.
738 -----------------------
739 -- Access_Definition --
740 -----------------------
742 function Access_Definition
743 (Related_Nod : Node_Id;
744 N : Node_Id) return Entity_Id
746 Anon_Type : Entity_Id;
747 Anon_Scope : Entity_Id;
748 Desig_Type : Entity_Id;
749 Enclosing_Prot_Type : Entity_Id := Empty;
752 if Is_Entry (Current_Scope)
753 and then Is_Task_Type (Etype (Scope (Current_Scope)))
755 Error_Msg_N ("task entries cannot have access parameters", N);
759 -- Ada 2005: For an object declaration the corresponding anonymous
760 -- type is declared in the current scope.
762 -- If the access definition is the return type of another access to
763 -- function, scope is the current one, because it is the one of the
764 -- current type declaration, except for the pathological case below.
766 if Nkind (Related_Nod) in
767 N_Object_Declaration | N_Access_Function_Definition
769 Anon_Scope := Current_Scope;
771 -- A pathological case: function returning access functions that
772 -- return access functions, etc. Each anonymous access type created
773 -- is in the enclosing scope of the outermost function.
781 N_Access_Function_Definition | N_Access_Definition
786 if Nkind (Par) = N_Function_Specification then
787 Anon_Scope := Scope (Defining_Entity (Par));
791 -- For the anonymous function result case, retrieve the scope of the
792 -- function specification's associated entity rather than using the
793 -- current scope. The current scope will be the function itself if the
794 -- formal part is currently being analyzed, but will be the parent scope
795 -- in the case of a parameterless function, and we always want to use
796 -- the function's parent scope. Finally, if the function is a child
797 -- unit, we must traverse the tree to retrieve the proper entity.
799 elsif Nkind (Related_Nod) = N_Function_Specification
800 and then Nkind (Parent (N)) /= N_Parameter_Specification
802 -- If the current scope is a protected type, the anonymous access
803 -- is associated with one of the protected operations, and must
804 -- be available in the scope that encloses the protected declaration.
805 -- Otherwise the type is in the scope enclosing the subprogram.
807 -- If the function has formals, the return type of a subprogram
808 -- declaration is analyzed in the scope of the subprogram (see
809 -- Process_Formals) and thus the protected type, if present, is
810 -- the scope of the current function scope.
812 if Ekind (Current_Scope) = E_Protected_Type then
813 Enclosing_Prot_Type := Current_Scope;
815 elsif Ekind (Current_Scope) = E_Function
816 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
818 Enclosing_Prot_Type := Scope (Current_Scope);
821 if Present (Enclosing_Prot_Type) then
822 Anon_Scope := Scope (Enclosing_Prot_Type);
825 Anon_Scope := Scope (Defining_Entity (Related_Nod));
828 -- For an access type definition, if the current scope is a child
829 -- unit it is the scope of the type.
831 elsif Is_Compilation_Unit (Current_Scope) then
832 Anon_Scope := Current_Scope;
834 -- For access formals, access components, and access discriminants, the
835 -- scope is that of the enclosing declaration,
838 Anon_Scope := Scope (Current_Scope);
843 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
846 and then Ada_Version >= Ada_2005
848 Error_Msg_N ("ALL not permitted for anonymous access types", N);
851 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
852 -- the corresponding semantic routine
854 if Present (Access_To_Subprogram_Definition (N)) then
855 Access_Subprogram_Declaration
856 (T_Name => Anon_Type,
857 T_Def => Access_To_Subprogram_Definition (N));
859 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
861 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
863 Mutate_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
866 Set_Can_Use_Internal_Rep
867 (Anon_Type, not Always_Compatible_Rep_On_Target);
869 -- If the anonymous access is associated with a protected operation,
870 -- create a reference to it after the enclosing protected definition
871 -- because the itype will be used in the subsequent bodies.
873 -- If the anonymous access itself is protected, a full type
874 -- declaratiton will be created for it, so that the equivalent
875 -- record type can be constructed. For further details, see
876 -- Replace_Anonymous_Access_To_Protected-Subprogram.
878 if Ekind (Current_Scope) = E_Protected_Type
879 and then not Protected_Present (Access_To_Subprogram_Definition (N))
881 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
887 Find_Type (Subtype_Mark (N));
888 Desig_Type := Entity (Subtype_Mark (N));
890 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
891 Set_Etype (Anon_Type, Anon_Type);
893 -- Make sure the anonymous access type has size and alignment fields
894 -- set, as required by gigi. This is necessary in the case of the
895 -- Task_Body_Procedure.
897 if not Has_Private_Component (Desig_Type) then
898 Layout_Type (Anon_Type);
901 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
902 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
903 -- the null value is allowed. In Ada 95 the null value is never allowed.
905 if Ada_Version >= Ada_2005 then
906 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
908 Set_Can_Never_Be_Null (Anon_Type, True);
911 -- The anonymous access type is as public as the discriminated type or
912 -- subprogram that defines it. It is imported (for back-end purposes)
913 -- if the designated type is.
915 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
917 -- Ada 2005 (AI-231): Propagate the access-constant attribute
919 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
921 -- The context is either a subprogram declaration, object declaration,
922 -- or an access discriminant, in a private or a full type declaration.
923 -- In the case of a subprogram, if the designated type is incomplete,
924 -- the operation will be a primitive operation of the full type, to be
925 -- updated subsequently. If the type is imported through a limited_with
926 -- clause, the subprogram is not a primitive operation of the type
927 -- (which is declared elsewhere in some other scope).
929 if Ekind (Desig_Type) = E_Incomplete_Type
930 and then not From_Limited_With (Desig_Type)
931 and then Is_Overloadable (Current_Scope)
933 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
934 Set_Has_Delayed_Freeze (Current_Scope);
937 -- If the designated type is limited and class-wide, the object might
938 -- contain tasks, so we create a Master entity for the declaration. This
939 -- must be done before expansion of the full declaration, because the
940 -- declaration may include an expression that is an allocator, whose
941 -- expansion needs the proper Master for the created tasks.
944 and then Nkind (Related_Nod) = N_Object_Declaration
946 if Is_Limited_Record (Desig_Type)
947 and then Is_Class_Wide_Type (Desig_Type)
949 Build_Class_Wide_Master (Anon_Type);
951 -- Similarly, if the type is an anonymous access that designates
952 -- tasks, create a master entity for it in the current context.
954 elsif Has_Task (Desig_Type)
955 and then Comes_From_Source (Related_Nod)
957 Build_Master_Entity (Defining_Identifier (Related_Nod));
958 Build_Master_Renaming (Anon_Type);
962 -- For a private component of a protected type, it is imperative that
963 -- the back-end elaborate the type immediately after the protected
964 -- declaration, because this type will be used in the declarations
965 -- created for the component within each protected body, so we must
966 -- create an itype reference for it now.
968 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
969 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
971 -- Similarly, if the access definition is the return result of a
972 -- function, create an itype reference for it because it will be used
973 -- within the function body. For a regular function that is not a
974 -- compilation unit, insert reference after the declaration. For a
975 -- protected operation, insert it after the enclosing protected type
976 -- declaration. In either case, do not create a reference for a type
977 -- obtained through a limited_with clause, because this would introduce
978 -- semantic dependencies.
980 -- Similarly, do not create a reference if the designated type is a
981 -- generic formal, because no use of it will reach the backend.
983 elsif Nkind (Related_Nod) = N_Function_Specification
984 and then not From_Limited_With (Desig_Type)
985 and then not Is_Generic_Type (Desig_Type)
987 if Present (Enclosing_Prot_Type) then
988 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
990 elsif Is_List_Member (Parent (Related_Nod))
991 and then Nkind (Parent (N)) /= N_Parameter_Specification
993 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
996 -- Finally, create an itype reference for an object declaration of an
997 -- anonymous access type. This is strictly necessary only for deferred
998 -- constants, but in any case will avoid out-of-scope problems in the
1001 elsif Nkind (Related_Nod) = N_Object_Declaration then
1002 Build_Itype_Reference (Anon_Type, Related_Nod);
1006 end Access_Definition;
1008 -----------------------------------
1009 -- Access_Subprogram_Declaration --
1010 -----------------------------------
1012 procedure Access_Subprogram_Declaration
1013 (T_Name : Entity_Id;
1016 procedure Check_For_Premature_Usage (Def : Node_Id);
1017 -- Check that type T_Name is not used, directly or recursively, as a
1018 -- parameter or a return type in Def. Def is either a subtype, an
1019 -- access_definition, or an access_to_subprogram_definition.
1021 -------------------------------
1022 -- Check_For_Premature_Usage --
1023 -------------------------------
1025 procedure Check_For_Premature_Usage (Def : Node_Id) is
1029 -- Check for a subtype mark
1031 if Nkind (Def) in N_Has_Etype then
1032 if Etype (Def) = T_Name then
1034 ("type& cannot be used before the end of its declaration",
1038 -- If this is not a subtype, then this is an access_definition
1040 elsif Nkind (Def) = N_Access_Definition then
1041 if Present (Access_To_Subprogram_Definition (Def)) then
1042 Check_For_Premature_Usage
1043 (Access_To_Subprogram_Definition (Def));
1045 Check_For_Premature_Usage (Subtype_Mark (Def));
1048 -- The only cases left are N_Access_Function_Definition and
1049 -- N_Access_Procedure_Definition.
1052 if Present (Parameter_Specifications (Def)) then
1053 Param := First (Parameter_Specifications (Def));
1054 while Present (Param) loop
1055 Check_For_Premature_Usage (Parameter_Type (Param));
1060 if Nkind (Def) = N_Access_Function_Definition then
1061 Check_For_Premature_Usage (Result_Definition (Def));
1064 end Check_For_Premature_Usage;
1068 Formals : constant List_Id := Parameter_Specifications (T_Def);
1071 Desig_Type : constant Entity_Id :=
1072 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1074 -- Start of processing for Access_Subprogram_Declaration
1077 -- Associate the Itype node with the inner full-type declaration or
1078 -- subprogram spec or entry body. This is required to handle nested
1079 -- anonymous declarations. For example:
1082 -- (X : access procedure
1083 -- (Y : access procedure
1086 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1087 while Nkind (D_Ityp) not in N_Full_Type_Declaration
1088 | N_Private_Type_Declaration
1089 | N_Private_Extension_Declaration
1090 | N_Procedure_Specification
1091 | N_Function_Specification
1093 | N_Object_Declaration
1094 | N_Object_Renaming_Declaration
1095 | N_Formal_Object_Declaration
1096 | N_Formal_Type_Declaration
1097 | N_Task_Type_Declaration
1098 | N_Protected_Type_Declaration
1100 D_Ityp := Parent (D_Ityp);
1101 pragma Assert (D_Ityp /= Empty);
1104 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1106 if Nkind (D_Ityp) in N_Procedure_Specification | N_Function_Specification
1108 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1110 elsif Nkind (D_Ityp) in N_Full_Type_Declaration
1111 | N_Object_Declaration
1112 | N_Object_Renaming_Declaration
1113 | N_Formal_Type_Declaration
1115 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1118 if Nkind (T_Def) = N_Access_Function_Definition then
1119 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1121 Acc : constant Node_Id := Result_Definition (T_Def);
1124 if Present (Access_To_Subprogram_Definition (Acc))
1126 Protected_Present (Access_To_Subprogram_Definition (Acc))
1130 Replace_Anonymous_Access_To_Protected_Subprogram
1136 Access_Definition (T_Def, Result_Definition (T_Def)));
1141 Analyze (Result_Definition (T_Def));
1144 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1147 -- If a null exclusion is imposed on the result type, then
1148 -- create a null-excluding itype (an access subtype) and use
1149 -- it as the function's Etype.
1151 if Is_Access_Type (Typ)
1152 and then Null_Exclusion_In_Return_Present (T_Def)
1154 Set_Etype (Desig_Type,
1155 Create_Null_Excluding_Itype
1157 Related_Nod => T_Def,
1158 Scope_Id => Current_Scope));
1161 if From_Limited_With (Typ) then
1163 -- AI05-151: Incomplete types are allowed in all basic
1164 -- declarations, including access to subprograms.
1166 if Ada_Version >= Ada_2012 then
1171 ("illegal use of incomplete type&",
1172 Result_Definition (T_Def), Typ);
1175 elsif Ekind (Current_Scope) = E_Package
1176 and then In_Private_Part (Current_Scope)
1178 if Ekind (Typ) = E_Incomplete_Type then
1179 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1181 elsif Is_Class_Wide_Type (Typ)
1182 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1185 (Desig_Type, Private_Dependents (Etype (Typ)));
1189 Set_Etype (Desig_Type, Typ);
1194 if not Is_Type (Etype (Desig_Type)) then
1196 ("expect type in function specification",
1197 Result_Definition (T_Def));
1201 Set_Etype (Desig_Type, Standard_Void_Type);
1204 if Present (Formals) then
1205 Push_Scope (Desig_Type);
1207 -- Some special tests here. These special tests can be removed
1208 -- if and when Itypes always have proper parent pointers to their
1211 -- Special test 1) Link defining_identifier of formals. Required by
1212 -- First_Formal to provide its functionality.
1218 F := First (Formals);
1220 while Present (F) loop
1221 if No (Parent (Defining_Identifier (F))) then
1222 Set_Parent (Defining_Identifier (F), F);
1229 Process_Formals (Formals, Parent (T_Def));
1231 -- Special test 2) End_Scope requires that the parent pointer be set
1232 -- to something reasonable, but Itypes don't have parent pointers. So
1233 -- we set it and then unset it ???
1235 Set_Parent (Desig_Type, T_Name);
1237 Set_Parent (Desig_Type, Empty);
1240 -- Check for premature usage of the type being defined
1242 Check_For_Premature_Usage (T_Def);
1244 -- The return type and/or any parameter type may be incomplete. Mark the
1245 -- subprogram_type as depending on the incomplete type, so that it can
1246 -- be updated when the full type declaration is seen. This only applies
1247 -- to incomplete types declared in some enclosing scope, not to limited
1248 -- views from other packages.
1250 -- Prior to Ada 2012, access to functions can only have in_parameters.
1252 if Present (Formals) then
1253 Formal := First_Formal (Desig_Type);
1254 while Present (Formal) loop
1255 if Ekind (Formal) /= E_In_Parameter
1256 and then Nkind (T_Def) = N_Access_Function_Definition
1257 and then Ada_Version < Ada_2012
1259 Error_Msg_N ("functions can only have IN parameters", Formal);
1262 if Ekind (Etype (Formal)) = E_Incomplete_Type
1263 and then In_Open_Scopes (Scope (Etype (Formal)))
1265 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1266 Set_Has_Delayed_Freeze (Desig_Type);
1269 Next_Formal (Formal);
1273 -- Check whether an indirect call without actuals may be possible. This
1274 -- is used when resolving calls whose result is then indexed.
1276 May_Need_Actuals (Desig_Type);
1278 -- If the return type is incomplete, this is legal as long as the type
1279 -- is declared in the current scope and will be completed in it (rather
1280 -- than being part of limited view).
1282 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1283 and then not Has_Delayed_Freeze (Desig_Type)
1284 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1286 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1287 Set_Has_Delayed_Freeze (Desig_Type);
1290 Check_Delayed_Subprogram (Desig_Type);
1292 if Protected_Present (T_Def) then
1293 Mutate_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1294 Set_Convention (Desig_Type, Convention_Protected);
1296 Mutate_Ekind (T_Name, E_Access_Subprogram_Type);
1299 Set_Can_Use_Internal_Rep (T_Name,
1300 not Always_Compatible_Rep_On_Target);
1301 Set_Etype (T_Name, T_Name);
1302 Reinit_Size_Align (T_Name);
1303 Set_Directly_Designated_Type (T_Name, Desig_Type);
1305 -- If the access_to_subprogram is not declared at the library level,
1306 -- it can only point to subprograms that are at the same or deeper
1307 -- accessibility level. The corresponding subprogram type might
1308 -- require an activation record when compiling for C.
1310 Set_Needs_Activation_Record (Desig_Type,
1311 not Is_Library_Level_Entity (T_Name));
1313 Generate_Reference_To_Formals (T_Name);
1315 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1317 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1319 Check_Restriction (No_Access_Subprograms, T_Def);
1321 Create_Extra_Formals (Desig_Type);
1322 end Access_Subprogram_Declaration;
1324 ----------------------------
1325 -- Access_Type_Declaration --
1326 ----------------------------
1328 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1330 procedure Setup_Access_Type (Desig_Typ : Entity_Id);
1331 -- After type declaration is analysed with T being an incomplete type,
1332 -- this routine will mutate the kind of T to the appropriate access type
1333 -- and set its directly designated type to Desig_Typ.
1335 -----------------------
1336 -- Setup_Access_Type --
1337 -----------------------
1339 procedure Setup_Access_Type (Desig_Typ : Entity_Id) is
1341 if All_Present (Def) or else Constant_Present (Def) then
1342 Mutate_Ekind (T, E_General_Access_Type);
1344 Mutate_Ekind (T, E_Access_Type);
1347 Set_Directly_Designated_Type (T, Desig_Typ);
1348 end Setup_Access_Type;
1352 P : constant Node_Id := Parent (Def);
1353 S : constant Node_Id := Subtype_Indication (Def);
1355 Full_Desig : Entity_Id;
1357 -- Start of processing for Access_Type_Declaration
1360 -- Check for permissible use of incomplete type
1362 if Nkind (S) /= N_Subtype_Indication then
1366 if Nkind (S) in N_Has_Entity
1367 and then Present (Entity (S))
1368 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1370 Setup_Access_Type (Desig_Typ => Entity (S));
1372 -- If the designated type is a limited view, we cannot tell if
1373 -- the full view contains tasks, and there is no way to handle
1374 -- that full view in a client. We create a master entity for the
1375 -- scope, which will be used when a client determines that one
1378 if From_Limited_With (Entity (S))
1379 and then not Is_Class_Wide_Type (Entity (S))
1381 Build_Master_Entity (T);
1382 Build_Master_Renaming (T);
1386 Setup_Access_Type (Desig_Typ => Process_Subtype (S, P, T, 'P'));
1389 -- If the access definition is of the form: ACCESS NOT NULL ..
1390 -- the subtype indication must be of an access type. Create
1391 -- a null-excluding subtype of it.
1393 if Null_Excluding_Subtype (Def) then
1394 if not Is_Access_Type (Entity (S)) then
1395 Error_Msg_N ("null exclusion must apply to access type", Def);
1399 Loc : constant Source_Ptr := Sloc (S);
1401 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1405 Make_Subtype_Declaration (Loc,
1406 Defining_Identifier => Nam,
1407 Subtype_Indication =>
1408 New_Occurrence_Of (Entity (S), Loc));
1409 Set_Null_Exclusion_Present (Decl);
1410 Insert_Before (Parent (Def), Decl);
1412 Set_Entity (S, Nam);
1418 Setup_Access_Type (Desig_Typ => Process_Subtype (S, P, T, 'P'));
1421 if not Error_Posted (T) then
1422 Full_Desig := Designated_Type (T);
1424 if Base_Type (Full_Desig) = T then
1425 Error_Msg_N ("access type cannot designate itself", S);
1427 -- In Ada 2005, the type may have a limited view through some unit in
1428 -- its own context, allowing the following circularity that cannot be
1429 -- detected earlier.
1431 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1434 ("access type cannot designate its own class-wide type", S);
1436 -- Clean up indication of tagged status to prevent cascaded errors
1438 Set_Is_Tagged_Type (T, False);
1443 -- For SPARK, check that the designated type is compatible with
1444 -- respect to volatility with the access type.
1446 if SPARK_Mode /= Off
1447 and then Comes_From_Source (T)
1449 -- ??? UNIMPLEMENTED
1450 -- In the case where the designated type is incomplete at this
1451 -- point, performing this check here is harmless but the check
1452 -- will need to be repeated when the designated type is complete.
1454 -- The preceding call to Comes_From_Source is needed because the
1455 -- FE sometimes introduces implicitly declared access types. See,
1456 -- for example, the expansion of nested_po.ads in OA28-015.
1458 Check_Volatility_Compatibility
1459 (Full_Desig, T, "designated type", "access type",
1460 Srcpos_Bearer => T);
1464 -- If the type has appeared already in a with_type clause, it is frozen
1465 -- and the pointer size is already set. Else, initialize.
1467 if not From_Limited_With (T) then
1468 Reinit_Size_Align (T);
1471 -- Note that Has_Task is always false, since the access type itself
1472 -- is not a task type. See Einfo for more description on this point.
1473 -- Exactly the same consideration applies to Has_Controlled_Component
1474 -- and to Has_Protected.
1476 Set_Has_Task (T, False);
1477 Set_Has_Protected (T, False);
1478 Set_Has_Timing_Event (T, False);
1479 Set_Has_Controlled_Component (T, False);
1481 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1482 -- problems where an incomplete view of this entity has been previously
1483 -- established by a limited with and an overlaid version of this field
1484 -- (Stored_Constraint) was initialized for the incomplete view.
1486 -- This reset is performed in most cases except where the access type
1487 -- has been created for the purposes of allocating or deallocating a
1488 -- build-in-place object. Such access types have explicitly set pools
1489 -- and finalization masters.
1491 if No (Associated_Storage_Pool (T)) then
1492 Set_Finalization_Master (T, Empty);
1495 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1498 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1499 Set_Is_Access_Constant (T, Constant_Present (Def));
1500 end Access_Type_Declaration;
1502 ----------------------------------
1503 -- Add_Interface_Tag_Components --
1504 ----------------------------------
1506 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1507 Loc : constant Source_Ptr := Sloc (N);
1511 procedure Add_Tag (Iface : Entity_Id);
1512 -- Add tag for one of the progenitor interfaces
1518 procedure Add_Tag (Iface : Entity_Id) is
1525 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1527 -- This is a reasonable place to propagate predicates
1529 if Has_Predicates (Iface) then
1530 Set_Has_Predicates (Typ);
1534 Make_Component_Definition (Loc,
1535 Aliased_Present => True,
1536 Subtype_Indication =>
1537 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1539 Tag := Make_Temporary (Loc, 'V');
1542 Make_Component_Declaration (Loc,
1543 Defining_Identifier => Tag,
1544 Component_Definition => Def);
1546 Analyze_Component_Declaration (Decl);
1548 Set_Analyzed (Decl);
1549 Mutate_Ekind (Tag, E_Component);
1551 Set_Is_Aliased (Tag);
1552 Set_Is_Independent (Tag);
1553 Set_Related_Type (Tag, Iface);
1554 Reinit_Component_Location (Tag);
1556 pragma Assert (Is_Frozen (Iface));
1558 Set_DT_Entry_Count (Tag,
1559 DT_Entry_Count (First_Entity (Iface)));
1561 if No (Last_Tag) then
1564 Insert_After (Last_Tag, Decl);
1569 -- If the ancestor has discriminants we need to give special support
1570 -- to store the offset_to_top value of the secondary dispatch tables.
1571 -- For this purpose we add a supplementary component just after the
1572 -- field that contains the tag associated with each secondary DT.
1574 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1576 Make_Component_Definition (Loc,
1577 Subtype_Indication =>
1578 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1580 Offset := Make_Temporary (Loc, 'V');
1583 Make_Component_Declaration (Loc,
1584 Defining_Identifier => Offset,
1585 Component_Definition => Def);
1587 Analyze_Component_Declaration (Decl);
1589 Set_Analyzed (Decl);
1590 Mutate_Ekind (Offset, E_Component);
1591 Set_Is_Aliased (Offset);
1592 Set_Is_Independent (Offset);
1593 Set_Related_Type (Offset, Iface);
1594 Reinit_Component_Location (Offset);
1595 Insert_After (Last_Tag, Decl);
1606 -- Start of processing for Add_Interface_Tag_Components
1609 if not RTE_Available (RE_Interface_Tag) then
1611 ("(Ada 2005) interface types not supported by this run-time!", N);
1615 if Ekind (Typ) /= E_Record_Type
1616 or else (Is_Concurrent_Record_Type (Typ)
1617 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1618 or else (not Is_Concurrent_Record_Type (Typ)
1619 and then No (Interfaces (Typ))
1620 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1625 -- Find the current last tag
1627 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1628 Ext := Record_Extension_Part (Type_Definition (N));
1630 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1631 Ext := Type_Definition (N);
1636 if not (Present (Component_List (Ext))) then
1637 Set_Null_Present (Ext, False);
1639 Set_Component_List (Ext,
1640 Make_Component_List (Loc,
1641 Component_Items => L,
1642 Null_Present => False));
1644 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1645 L := Component_Items
1647 (Record_Extension_Part
1648 (Type_Definition (N))));
1650 L := Component_Items
1652 (Type_Definition (N)));
1655 -- Find the last tag component
1658 while Present (Comp) loop
1659 if Nkind (Comp) = N_Component_Declaration
1660 and then Is_Tag (Defining_Identifier (Comp))
1669 -- At this point L references the list of components and Last_Tag
1670 -- references the current last tag (if any). Now we add the tag
1671 -- corresponding with all the interfaces that are not implemented
1674 if Present (Interfaces (Typ)) then
1675 Elmt := First_Elmt (Interfaces (Typ));
1676 while Present (Elmt) loop
1677 Add_Tag (Node (Elmt));
1681 end Add_Interface_Tag_Components;
1683 -------------------------------------
1684 -- Add_Internal_Interface_Entities --
1685 -------------------------------------
1687 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1690 Iface_Elmt : Elmt_Id;
1691 Iface_Prim : Entity_Id;
1692 Ifaces_List : Elist_Id;
1693 New_Subp : Entity_Id := Empty;
1695 Restore_Scope : Boolean := False;
1698 pragma Assert (Ada_Version >= Ada_2005
1699 and then Is_Record_Type (Tagged_Type)
1700 and then Is_Tagged_Type (Tagged_Type)
1701 and then Has_Interfaces (Tagged_Type)
1702 and then not Is_Interface (Tagged_Type));
1704 -- Ensure that the internal entities are added to the scope of the type
1706 if Scope (Tagged_Type) /= Current_Scope then
1707 Push_Scope (Scope (Tagged_Type));
1708 Restore_Scope := True;
1711 Collect_Interfaces (Tagged_Type, Ifaces_List);
1713 Iface_Elmt := First_Elmt (Ifaces_List);
1714 while Present (Iface_Elmt) loop
1715 Iface := Node (Iface_Elmt);
1717 -- Originally we excluded here from this processing interfaces that
1718 -- are parents of Tagged_Type because their primitives are located
1719 -- in the primary dispatch table (and hence no auxiliary internal
1720 -- entities are required to handle secondary dispatch tables in such
1721 -- case). However, these auxiliary entities are also required to
1722 -- handle derivations of interfaces in formals of generics (see
1723 -- Derive_Subprograms).
1725 Elmt := First_Elmt (Primitive_Operations (Iface));
1726 while Present (Elmt) loop
1727 Iface_Prim := Node (Elmt);
1729 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1731 Find_Primitive_Covering_Interface
1732 (Tagged_Type => Tagged_Type,
1733 Iface_Prim => Iface_Prim);
1735 if No (Prim) and then Serious_Errors_Detected > 0 then
1739 pragma Assert (Present (Prim));
1741 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1742 -- differs from the name of the interface primitive then it is
1743 -- a private primitive inherited from a parent type. In such
1744 -- case, given that Tagged_Type covers the interface, the
1745 -- inherited private primitive becomes visible. For such
1746 -- purpose we add a new entity that renames the inherited
1747 -- private primitive.
1749 if Chars (Prim) /= Chars (Iface_Prim) then
1750 pragma Assert (Has_Suffix (Prim, 'P'));
1752 (New_Subp => New_Subp,
1753 Parent_Subp => Iface_Prim,
1754 Derived_Type => Tagged_Type,
1755 Parent_Type => Iface);
1756 Set_Alias (New_Subp, Prim);
1757 Set_Is_Abstract_Subprogram
1758 (New_Subp, Is_Abstract_Subprogram (Prim));
1762 (New_Subp => New_Subp,
1763 Parent_Subp => Iface_Prim,
1764 Derived_Type => Tagged_Type,
1765 Parent_Type => Iface);
1770 if Is_Inherited_Operation (Prim)
1771 and then Present (Alias (Prim))
1773 Anc := Alias (Prim);
1775 Anc := Overridden_Operation (Prim);
1778 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1779 -- nonconforming preconditions in both an ancestor and
1780 -- a progenitor operation.
1782 -- If the operation is a primitive wrapper it is an explicit
1783 -- (overriding) operqtion and all is fine.
1786 and then Has_Non_Trivial_Precondition (Anc)
1787 and then Has_Non_Trivial_Precondition (Iface_Prim)
1789 if Is_Abstract_Subprogram (Prim)
1791 (Ekind (Prim) = E_Procedure
1792 and then Nkind (Parent (Prim)) =
1793 N_Procedure_Specification
1794 and then Null_Present (Parent (Prim)))
1795 or else Is_Primitive_Wrapper (Prim)
1799 -- The operation is inherited and must be overridden
1801 elsif not Comes_From_Source (Prim) then
1803 ("&inherits non-conforming preconditions and must "
1804 & "be overridden (RM 6.1.1 (10-16))",
1805 Parent (Tagged_Type), Prim);
1810 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1811 -- associated with interface types. These entities are
1812 -- only registered in the list of primitives of its
1813 -- corresponding tagged type because they are only used
1814 -- to fill the contents of the secondary dispatch tables.
1815 -- Therefore they are removed from the homonym chains.
1817 Set_Is_Hidden (New_Subp);
1818 Set_Is_Internal (New_Subp);
1819 Set_Alias (New_Subp, Prim);
1820 Set_Is_Abstract_Subprogram
1821 (New_Subp, Is_Abstract_Subprogram (Prim));
1822 Set_Interface_Alias (New_Subp, Iface_Prim);
1824 -- If the returned type is an interface then propagate it to
1825 -- the returned type. Needed by the thunk to generate the code
1826 -- which displaces "this" to reference the corresponding
1827 -- secondary dispatch table in the returned object.
1829 if Is_Interface (Etype (Iface_Prim)) then
1830 Set_Etype (New_Subp, Etype (Iface_Prim));
1833 -- Internal entities associated with interface types are only
1834 -- registered in the list of primitives of the tagged type.
1835 -- They are only used to fill the contents of the secondary
1836 -- dispatch tables. Therefore they are not needed in the
1839 Remove_Homonym (New_Subp);
1841 -- Hidden entities associated with interfaces must have set
1842 -- the Has_Delay_Freeze attribute to ensure that, in case
1843 -- of locally defined tagged types (or compiling with static
1844 -- dispatch tables generation disabled) the corresponding
1845 -- entry of the secondary dispatch table is filled when such
1846 -- an entity is frozen.
1848 Set_Has_Delayed_Freeze (New_Subp);
1855 Next_Elmt (Iface_Elmt);
1858 if Restore_Scope then
1861 end Add_Internal_Interface_Entities;
1863 -----------------------------------
1864 -- Analyze_Component_Declaration --
1865 -----------------------------------
1867 procedure Analyze_Component_Declaration (N : Node_Id) is
1868 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1869 Id : constant Entity_Id := Defining_Identifier (N);
1870 E : constant Node_Id := Expression (N);
1871 Typ : constant Node_Id :=
1872 Subtype_Indication (Component_Definition (N));
1876 function Contains_POC (Constr : Node_Id) return Boolean;
1877 -- Determines whether a constraint uses the discriminant of a record
1878 -- type thus becoming a per-object constraint (POC).
1880 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1881 -- Typ is the type of the current component, check whether this type is
1882 -- a limited type. Used to validate declaration against that of
1883 -- enclosing record.
1889 function Contains_POC (Constr : Node_Id) return Boolean is
1891 -- Prevent cascaded errors
1893 if Error_Posted (Constr) then
1897 case Nkind (Constr) is
1898 when N_Attribute_Reference =>
1899 return Attribute_Name (Constr) = Name_Access
1900 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1902 when N_Discriminant_Association =>
1903 return Denotes_Discriminant (Expression (Constr));
1905 when N_Identifier =>
1906 return Denotes_Discriminant (Constr);
1908 when N_Index_Or_Discriminant_Constraint =>
1913 IDC := First (Constraints (Constr));
1914 while Present (IDC) loop
1916 -- One per-object constraint is sufficient
1918 if Contains_POC (IDC) then
1929 return Denotes_Discriminant (Low_Bound (Constr))
1931 Denotes_Discriminant (High_Bound (Constr));
1933 when N_Range_Constraint =>
1934 return Denotes_Discriminant (Range_Expression (Constr));
1941 ----------------------
1942 -- Is_Known_Limited --
1943 ----------------------
1945 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1946 P : constant Entity_Id := Etype (Typ);
1947 R : constant Entity_Id := Root_Type (Typ);
1950 if Is_Limited_Record (Typ) then
1953 -- If the root type is limited (and not a limited interface) so is
1954 -- the current type.
1956 elsif Is_Limited_Record (R)
1957 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1961 -- Else the type may have a limited interface progenitor, but a
1962 -- limited record parent that is not an interface.
1965 and then Is_Limited_Record (P)
1966 and then not Is_Interface (P)
1973 end Is_Known_Limited;
1975 -- Start of processing for Analyze_Component_Declaration
1978 Generate_Definition (Id);
1981 if Present (Typ) then
1982 T := Find_Type_Of_Object
1983 (Subtype_Indication (Component_Definition (N)), N);
1985 -- Ada 2005 (AI-230): Access Definition case
1988 pragma Assert (Present
1989 (Access_Definition (Component_Definition (N))));
1991 T := Access_Definition
1993 N => Access_Definition (Component_Definition (N)));
1994 Set_Is_Local_Anonymous_Access (T);
1996 -- Ada 2005 (AI-254)
1998 if Present (Access_To_Subprogram_Definition
1999 (Access_Definition (Component_Definition (N))))
2000 and then Protected_Present (Access_To_Subprogram_Definition
2002 (Component_Definition (N))))
2004 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
2008 -- If the subtype is a constrained subtype of the enclosing record,
2009 -- (which must have a partial view) the back-end does not properly
2010 -- handle the recursion. Rewrite the component declaration with an
2011 -- explicit subtype indication, which is acceptable to Gigi. We can copy
2012 -- the tree directly because side effects have already been removed from
2013 -- discriminant constraints.
2015 if Ekind (T) = E_Access_Subtype
2016 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
2017 and then Comes_From_Source (T)
2018 and then Nkind (Parent (T)) = N_Subtype_Declaration
2019 and then Etype (Directly_Designated_Type (T)) = Current_Scope
2022 (Subtype_Indication (Component_Definition (N)),
2023 New_Copy_Tree (Subtype_Indication (Parent (T))));
2024 T := Find_Type_Of_Object
2025 (Subtype_Indication (Component_Definition (N)), N);
2028 -- If the component declaration includes a default expression, then we
2029 -- check that the component is not of a limited type (RM 3.7(5)),
2030 -- and do the special preanalysis of the expression (see section on
2031 -- "Handling of Default and Per-Object Expressions" in the spec of
2035 Preanalyze_Default_Expression (E, T);
2036 Check_Initialization (T, E);
2038 if Ada_Version >= Ada_2005
2039 and then Ekind (T) = E_Anonymous_Access_Type
2040 and then Etype (E) /= Any_Type
2042 -- Check RM 3.9.2(9): "if the expected type for an expression is
2043 -- an anonymous access-to-specific tagged type, then the object
2044 -- designated by the expression shall not be dynamically tagged
2045 -- unless it is a controlling operand in a call on a dispatching
2048 if Is_Tagged_Type (Directly_Designated_Type (T))
2050 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2052 Ekind (Directly_Designated_Type (Etype (E))) =
2056 ("access to specific tagged type required (RM 3.9.2(9))", E);
2059 -- (Ada 2005: AI-230): Accessibility check for anonymous
2062 if Type_Access_Level (Etype (E)) >
2063 Deepest_Type_Access_Level (T)
2066 ("expression has deeper access level than component " &
2067 "(RM 3.10.2 (12.2))", E);
2070 -- The initialization expression is a reference to an access
2071 -- discriminant. The type of the discriminant is always deeper
2072 -- than any access type.
2074 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2075 and then Is_Entity_Name (E)
2076 and then Ekind (Entity (E)) = E_In_Parameter
2077 and then Present (Discriminal_Link (Entity (E)))
2080 ("discriminant has deeper accessibility level than target",
2086 -- The parent type may be a private view with unknown discriminants,
2087 -- and thus unconstrained. Regular components must be constrained.
2089 if not Is_Definite_Subtype (T)
2090 and then Chars (Id) /= Name_uParent
2092 if Is_Class_Wide_Type (T) then
2094 ("class-wide subtype with unknown discriminants" &
2095 " in component declaration",
2096 Subtype_Indication (Component_Definition (N)));
2099 ("unconstrained subtype in component declaration",
2100 Subtype_Indication (Component_Definition (N)));
2103 -- Components cannot be abstract, except for the special case of
2104 -- the _Parent field (case of extending an abstract tagged type)
2106 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2107 Error_Msg_N ("type of a component cannot be abstract", N);
2112 if Aliased_Present (Component_Definition (N)) then
2113 Set_Is_Aliased (Id);
2115 -- AI12-001: All aliased objects are considered to be specified as
2116 -- independently addressable (RM C.6(8.1/4)).
2118 Set_Is_Independent (Id);
2121 -- The component declaration may have a per-object constraint, set
2122 -- the appropriate flag in the defining identifier of the subtype.
2124 if Present (Subtype_Indication (Component_Definition (N))) then
2126 Sindic : constant Node_Id :=
2127 Subtype_Indication (Component_Definition (N));
2129 if Nkind (Sindic) = N_Subtype_Indication
2130 and then Present (Constraint (Sindic))
2131 and then Contains_POC (Constraint (Sindic))
2133 Set_Has_Per_Object_Constraint (Id);
2138 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2139 -- out some static checks.
2141 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2142 Null_Exclusion_Static_Checks (N);
2145 -- If this component is private (or depends on a private type), flag the
2146 -- record type to indicate that some operations are not available.
2148 P := Private_Component (T);
2152 -- Check for circular definitions
2154 if P = Any_Type then
2155 Set_Etype (Id, Any_Type);
2157 -- There is a gap in the visibility of operations only if the
2158 -- component type is not defined in the scope of the record type.
2160 elsif Scope (P) = Scope (Current_Scope) then
2163 elsif Is_Limited_Type (P) then
2164 Set_Is_Limited_Composite (Current_Scope);
2167 Set_Is_Private_Composite (Current_Scope);
2172 and then Is_Limited_Type (T)
2173 and then Chars (Id) /= Name_uParent
2174 and then Is_Tagged_Type (Current_Scope)
2176 if Is_Derived_Type (Current_Scope)
2177 and then not Is_Known_Limited (Current_Scope)
2180 ("extension of nonlimited type cannot have limited components",
2183 if Is_Interface (Root_Type (Current_Scope)) then
2185 ("\limitedness is not inherited from limited interface", N);
2186 Error_Msg_N ("\add LIMITED to type indication", N);
2189 Explain_Limited_Type (T, N);
2190 Set_Etype (Id, Any_Type);
2191 Set_Is_Limited_Composite (Current_Scope, False);
2193 elsif not Is_Derived_Type (Current_Scope)
2194 and then not Is_Limited_Record (Current_Scope)
2195 and then not Is_Concurrent_Type (Current_Scope)
2198 ("nonlimited tagged type cannot have limited components", N);
2199 Explain_Limited_Type (T, N);
2200 Set_Etype (Id, Any_Type);
2201 Set_Is_Limited_Composite (Current_Scope, False);
2205 -- If the component is an unconstrained task or protected type with
2206 -- discriminants, the component and the enclosing record are limited
2207 -- and the component is constrained by its default values. Compute
2208 -- its actual subtype, else it may be allocated the maximum size by
2209 -- the backend, and possibly overflow.
2211 if Is_Concurrent_Type (T)
2212 and then not Is_Constrained (T)
2213 and then Has_Discriminants (T)
2214 and then not Has_Discriminants (Current_Scope)
2217 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2220 Set_Etype (Id, Act_T);
2222 -- Rewrite component definition to use the constrained subtype
2224 Rewrite (Component_Definition (N),
2225 Make_Component_Definition (Loc,
2226 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2230 Set_Original_Record_Component (Id, Id);
2232 if Has_Aspects (N) then
2233 Analyze_Aspect_Specifications (N, Id);
2236 Analyze_Dimension (N);
2237 end Analyze_Component_Declaration;
2239 --------------------------
2240 -- Analyze_Declarations --
2241 --------------------------
2243 procedure Analyze_Declarations (L : List_Id) is
2246 procedure Adjust_Decl;
2247 -- Adjust Decl not to include implicit label declarations, since these
2248 -- have strange Sloc values that result in elaboration check problems.
2249 -- (They have the sloc of the label as found in the source, and that
2250 -- is ahead of the current declarative part).
2252 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2253 -- Create the subprogram bodies which verify the run-time semantics of
2254 -- the pragmas listed below for each elibigle type found in declarative
2255 -- list Decls. The pragmas are:
2257 -- Default_Initial_Condition
2261 -- Context denotes the owner of the declarative list.
2263 procedure Check_Entry_Contracts;
2264 -- Perform a preanalysis of the pre- and postconditions of an entry
2265 -- declaration. This must be done before full resolution and creation
2266 -- of the parameter block, etc. to catch illegal uses within the
2267 -- contract expression. Full analysis of the expression is done when
2268 -- the contract is processed.
2270 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2271 -- Check if a nested package has entities within it that rely on library
2272 -- level private types where the full view has not been completed for
2273 -- the purposes of checking if it is acceptable to freeze an expression
2274 -- function at the point of declaration.
2276 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2277 -- Determine whether Body_Decl denotes the body of a late controlled
2278 -- primitive (either Initialize, Adjust or Finalize). If this is the
2279 -- case, add a proper spec if the body lacks one. The spec is inserted
2280 -- before Body_Decl and immediately analyzed.
2282 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2283 -- Spec_Id is the entity of a package that may define abstract states,
2284 -- and in the case of a child unit, whose ancestors may define abstract
2285 -- states. If the states have partial visible refinement, remove the
2286 -- partial visibility of each constituent at the end of the package
2287 -- spec and body declarations.
2289 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2290 -- Spec_Id is the entity of a package that may define abstract states.
2291 -- If the states have visible refinement, remove the visibility of each
2292 -- constituent at the end of the package body declaration.
2294 procedure Resolve_Aspects;
2295 -- Utility to resolve the expressions of aspects at the end of a list of
2296 -- declarations, or before a declaration that freezes previous entities,
2297 -- such as in a subprogram body.
2303 procedure Adjust_Decl is
2305 while Present (Prev (Decl))
2306 and then Nkind (Decl) = N_Implicit_Label_Declaration
2312 ----------------------------
2313 -- Build_Assertion_Bodies --
2314 ----------------------------
2316 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2317 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2318 -- Create the subprogram bodies which verify the run-time semantics
2319 -- of the pragmas listed below for type Typ. The pragmas are:
2321 -- Default_Initial_Condition
2325 -------------------------------------
2326 -- Build_Assertion_Bodies_For_Type --
2327 -------------------------------------
2329 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2331 if Nkind (Context) = N_Package_Specification then
2333 -- Preanalyze and resolve the class-wide invariants of an
2334 -- interface at the end of whichever declarative part has the
2335 -- interface type. Note that an interface may be declared in
2336 -- any non-package declarative part, but reaching the end of
2337 -- such a declarative part will always freeze the type and
2338 -- generate the invariant procedure (see Freeze_Type).
2340 if Is_Interface (Typ) then
2342 -- Interfaces are treated as the partial view of a private
2343 -- type, in order to achieve uniformity with the general
2344 -- case. As a result, an interface receives only a "partial"
2345 -- invariant procedure, which is never called.
2347 if Has_Own_Invariants (Typ) then
2348 Build_Invariant_Procedure_Body
2350 Partial_Invariant => True);
2353 elsif Decls = Visible_Declarations (Context) then
2354 -- Preanalyze and resolve the invariants of a private type
2355 -- at the end of the visible declarations to catch potential
2356 -- errors. Inherited class-wide invariants are not included
2357 -- because they have already been resolved.
2359 if Ekind (Typ) in E_Limited_Private_Type
2361 | E_Record_Type_With_Private
2362 and then Has_Own_Invariants (Typ)
2364 Build_Invariant_Procedure_Body
2366 Partial_Invariant => True);
2369 -- Preanalyze and resolve the Default_Initial_Condition
2370 -- assertion expression at the end of the declarations to
2371 -- catch any errors.
2373 if Ekind (Typ) in E_Limited_Private_Type
2375 | E_Record_Type_With_Private
2376 and then Has_Own_DIC (Typ)
2378 Build_DIC_Procedure_Body
2380 Partial_DIC => True);
2383 elsif Decls = Private_Declarations (Context) then
2385 -- Preanalyze and resolve the invariants of a private type's
2386 -- full view at the end of the private declarations to catch
2387 -- potential errors.
2389 if (not Is_Private_Type (Typ)
2390 or else Present (Underlying_Full_View (Typ)))
2391 and then Has_Private_Declaration (Typ)
2392 and then Has_Invariants (Typ)
2394 Build_Invariant_Procedure_Body (Typ);
2397 if (not Is_Private_Type (Typ)
2398 or else Present (Underlying_Full_View (Typ)))
2399 and then Has_Private_Declaration (Typ)
2400 and then Has_DIC (Typ)
2402 Build_DIC_Procedure_Body (Typ);
2406 end Build_Assertion_Bodies_For_Type;
2411 Decl_Id : Entity_Id;
2413 -- Start of processing for Build_Assertion_Bodies
2416 Decl := First (Decls);
2417 while Present (Decl) loop
2418 if Is_Declaration (Decl) then
2419 Decl_Id := Defining_Entity (Decl);
2421 if Is_Type (Decl_Id) then
2422 Build_Assertion_Bodies_For_Type (Decl_Id);
2428 end Build_Assertion_Bodies;
2430 ---------------------------
2431 -- Check_Entry_Contracts --
2432 ---------------------------
2434 procedure Check_Entry_Contracts is
2440 Ent := First_Entity (Current_Scope);
2441 while Present (Ent) loop
2443 -- This only concerns entries with pre/postconditions
2445 if Ekind (Ent) = E_Entry
2446 and then Present (Contract (Ent))
2447 and then Present (Pre_Post_Conditions (Contract (Ent)))
2449 ASN := Pre_Post_Conditions (Contract (Ent));
2451 Install_Formals (Ent);
2453 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2454 -- is performed on a copy of the pragma expression, to prevent
2455 -- modifying the original expression.
2457 while Present (ASN) loop
2458 if Nkind (ASN) = N_Pragma then
2462 (First (Pragma_Argument_Associations (ASN))));
2463 Set_Parent (Exp, ASN);
2465 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2468 ASN := Next_Pragma (ASN);
2476 end Check_Entry_Contracts;
2478 ----------------------------------
2479 -- Contains_Lib_Incomplete_Type --
2480 ----------------------------------
2482 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2486 -- Avoid looking through scopes that do not meet the precondition of
2487 -- Pkg not being within a library unit spec.
2489 if not Is_Compilation_Unit (Pkg)
2490 and then not Is_Generic_Instance (Pkg)
2491 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2493 -- Loop through all entities in the current scope to identify
2494 -- an entity that depends on a private type.
2496 Curr := First_Entity (Pkg);
2498 if Nkind (Curr) in N_Entity
2499 and then Depends_On_Private (Curr)
2504 exit when Last_Entity (Current_Scope) = Curr;
2510 end Contains_Lib_Incomplete_Type;
2512 --------------------------------------
2513 -- Handle_Late_Controlled_Primitive --
2514 --------------------------------------
2516 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2517 Body_Spec : constant Node_Id := Specification (Body_Decl);
2518 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2519 Loc : constant Source_Ptr := Sloc (Body_Id);
2520 Params : constant List_Id :=
2521 Parameter_Specifications (Body_Spec);
2523 Spec_Id : Entity_Id;
2527 -- Consider only procedure bodies whose name matches one of the three
2528 -- controlled primitives.
2530 if Nkind (Body_Spec) /= N_Procedure_Specification
2531 or else Chars (Body_Id) not in Name_Adjust
2537 -- A controlled primitive must have exactly one formal which is not
2538 -- an anonymous access type.
2540 elsif List_Length (Params) /= 1 then
2544 Typ := Parameter_Type (First (Params));
2546 if Nkind (Typ) = N_Access_Definition then
2552 -- The type of the formal must be derived from [Limited_]Controlled
2554 if not Is_Controlled (Entity (Typ)) then
2558 -- Check whether a specification exists for this body. We do not
2559 -- analyze the spec of the body in full, because it will be analyzed
2560 -- again when the body is properly analyzed, and we cannot create
2561 -- duplicate entries in the formals chain. We look for an explicit
2562 -- specification because the body may be an overriding operation and
2563 -- an inherited spec may be present.
2565 Spec_Id := Current_Entity (Body_Id);
2567 while Present (Spec_Id) loop
2568 if Ekind (Spec_Id) in E_Procedure | E_Generic_Procedure
2569 and then Scope (Spec_Id) = Current_Scope
2570 and then Present (First_Formal (Spec_Id))
2571 and then No (Next_Formal (First_Formal (Spec_Id)))
2572 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2573 and then Comes_From_Source (Spec_Id)
2578 Spec_Id := Homonym (Spec_Id);
2581 -- At this point the body is known to be a late controlled primitive.
2582 -- Generate a matching spec and insert it before the body. Note the
2583 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2584 -- tree in this case.
2586 Spec := Copy_Separate_Tree (Body_Spec);
2588 -- Ensure that the subprogram declaration does not inherit the null
2589 -- indicator from the body as we now have a proper spec/body pair.
2591 Set_Null_Present (Spec, False);
2593 -- Ensure that the freeze node is inserted after the declaration of
2594 -- the primitive since its expansion will freeze the primitive.
2596 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2598 Insert_Before_And_Analyze (Body_Decl, Decl);
2599 end Handle_Late_Controlled_Primitive;
2601 ----------------------------------------
2602 -- Remove_Partial_Visible_Refinements --
2603 ----------------------------------------
2605 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2606 State_Elmt : Elmt_Id;
2608 if Present (Abstract_States (Spec_Id)) then
2609 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2610 while Present (State_Elmt) loop
2611 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2612 Next_Elmt (State_Elmt);
2616 -- For a child unit, also hide the partial state refinement from
2617 -- ancestor packages.
2619 if Is_Child_Unit (Spec_Id) then
2620 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2622 end Remove_Partial_Visible_Refinements;
2624 --------------------------------
2625 -- Remove_Visible_Refinements --
2626 --------------------------------
2628 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2629 State_Elmt : Elmt_Id;
2631 if Present (Abstract_States (Spec_Id)) then
2632 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2633 while Present (State_Elmt) loop
2634 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2635 Next_Elmt (State_Elmt);
2638 end Remove_Visible_Refinements;
2640 ---------------------
2641 -- Resolve_Aspects --
2642 ---------------------
2644 procedure Resolve_Aspects is
2648 E := First_Entity (Current_Scope);
2649 while Present (E) loop
2650 Resolve_Aspect_Expressions (E);
2652 -- Now that the aspect expressions have been resolved, if this is
2653 -- at the end of the visible declarations, we can set the flag
2654 -- Known_To_Have_Preelab_Init properly on types declared in the
2655 -- visible part, which is needed for checking whether full types
2656 -- in the private part satisfy the Preelaborable_Initialization
2657 -- aspect of the partial view. We can't wait for the creation of
2658 -- the pragma by Analyze_Aspects_At_Freeze_Point, because the
2659 -- freeze point may occur after the end of the package declaration
2660 -- (in the case of nested packages).
2663 and then L = Visible_Declarations (Parent (L))
2664 and then Has_Aspect (E, Aspect_Preelaborable_Initialization)
2667 ASN : constant Node_Id :=
2668 Find_Aspect (E, Aspect_Preelaborable_Initialization);
2669 Expr : constant Node_Id := Expression (ASN);
2671 -- Set Known_To_Have_Preelab_Init to True if aspect has no
2672 -- expression, or if the expression is True (or was folded
2673 -- to True), or if the expression is a conjunction of one or
2674 -- more Preelaborable_Initialization attributes applied to
2675 -- formal types and wasn't folded to False. (Note that
2676 -- Is_Conjunction_Of_Formal_Preelab_Init_Attributes goes to
2677 -- Original_Node if needed, hence test for Standard_False.)
2679 if not Present (Expr)
2680 or else (Is_Entity_Name (Expr)
2681 and then Entity (Expr) = Standard_True)
2683 (Is_Conjunction_Of_Formal_Preelab_Init_Attributes (Expr)
2685 not (Is_Entity_Name (Expr)
2686 and then Entity (Expr) = Standard_False))
2688 Set_Known_To_Have_Preelab_Init (E);
2695 end Resolve_Aspects;
2699 Context : Node_Id := Empty;
2700 Ctrl_Typ : Entity_Id := Empty;
2701 Freeze_From : Entity_Id := Empty;
2702 Next_Decl : Node_Id;
2704 -- Start of processing for Analyze_Declarations
2708 while Present (Decl) loop
2710 -- Complete analysis of declaration
2713 Next_Decl := Next (Decl);
2715 if No (Freeze_From) then
2716 Freeze_From := First_Entity (Current_Scope);
2719 -- Remember if the declaration we just processed is the full type
2720 -- declaration of a controlled type (to handle late overriding of
2721 -- initialize, adjust or finalize).
2723 if Nkind (Decl) = N_Full_Type_Declaration
2724 and then Is_Controlled (Defining_Identifier (Decl))
2726 Ctrl_Typ := Defining_Identifier (Decl);
2729 -- At the end of a declarative part, freeze remaining entities
2730 -- declared in it. The end of the visible declarations of package
2731 -- specification is not the end of a declarative part if private
2732 -- declarations are present. The end of a package declaration is a
2733 -- freezing point only if it a library package. A task definition or
2734 -- protected type definition is not a freeze point either. Finally,
2735 -- we do not freeze entities in generic scopes, because there is no
2736 -- code generated for them and freeze nodes will be generated for
2739 -- The end of a package instantiation is not a freeze point, but
2740 -- for now we make it one, because the generic body is inserted
2741 -- (currently) immediately after. Generic instantiations will not
2742 -- be a freeze point once delayed freezing of bodies is implemented.
2743 -- (This is needed in any case for early instantiations ???).
2745 if No (Next_Decl) then
2746 if Nkind (Parent (L)) = N_Component_List then
2749 elsif Nkind (Parent (L)) in
2750 N_Protected_Definition | N_Task_Definition
2752 Check_Entry_Contracts;
2754 elsif Nkind (Parent (L)) /= N_Package_Specification then
2755 if Nkind (Parent (L)) = N_Package_Body then
2756 Freeze_From := First_Entity (Current_Scope);
2759 -- There may have been several freezing points previously,
2760 -- for example object declarations or subprogram bodies, but
2761 -- at the end of a declarative part we check freezing from
2762 -- the beginning, even though entities may already be frozen,
2763 -- in order to perform visibility checks on delayed aspects.
2767 -- If the current scope is a generic subprogram body. Skip the
2768 -- generic formal parameters that are not frozen here.
2770 if Is_Subprogram (Current_Scope)
2771 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2772 N_Generic_Subprogram_Declaration
2773 and then Present (First_Entity (Current_Scope))
2775 while Is_Generic_Formal (Freeze_From) loop
2776 Next_Entity (Freeze_From);
2779 Freeze_All (Freeze_From, Decl);
2780 Freeze_From := Last_Entity (Current_Scope);
2783 -- For declarations in a subprogram body there is no issue
2784 -- with name resolution in aspect specifications.
2786 Freeze_All (First_Entity (Current_Scope), Decl);
2787 Freeze_From := Last_Entity (Current_Scope);
2790 -- Current scope is a package specification
2792 elsif Scope (Current_Scope) /= Standard_Standard
2793 and then not Is_Child_Unit (Current_Scope)
2794 and then No (Generic_Parent (Parent (L)))
2796 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2797 -- resolved at the end of the immediately enclosing declaration
2798 -- list (AI05-0183-1).
2802 elsif L /= Visible_Declarations (Parent (L))
2803 or else Is_Empty_List (Private_Declarations (Parent (L)))
2807 -- End of a package declaration
2809 -- This is a freeze point because it is the end of a
2810 -- compilation unit.
2812 Freeze_All (First_Entity (Current_Scope), Decl);
2813 Freeze_From := Last_Entity (Current_Scope);
2815 -- At the end of the visible declarations the expressions in
2816 -- aspects of all entities declared so far must be resolved.
2817 -- The entities themselves might be frozen later, and the
2818 -- generated pragmas and attribute definition clauses analyzed
2819 -- in full at that point, but name resolution must take place
2821 -- In addition to being the proper semantics, this is mandatory
2822 -- within generic units, because global name capture requires
2823 -- those expressions to be analyzed, given that the generated
2824 -- pragmas do not appear in the original generic tree.
2826 elsif Serious_Errors_Detected = 0 then
2830 -- If next node is a body then freeze all types before the body.
2831 -- An exception occurs for some expander-generated bodies. If these
2832 -- are generated at places where in general language rules would not
2833 -- allow a freeze point, then we assume that the expander has
2834 -- explicitly checked that all required types are properly frozen,
2835 -- and we do not cause general freezing here. This special circuit
2836 -- is used when the encountered body is marked as having already
2839 -- In all other cases (bodies that come from source, and expander
2840 -- generated bodies that have not been analyzed yet), freeze all
2841 -- types now. Note that in the latter case, the expander must take
2842 -- care to attach the bodies at a proper place in the tree so as to
2843 -- not cause unwanted freezing at that point.
2845 -- It is also necessary to check for a case where both an expression
2846 -- function is used and the current scope depends on an incomplete
2847 -- private type from a library unit, otherwise premature freezing of
2848 -- the private type will occur.
2850 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2851 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2852 or else not Was_Expression_Function (Next_Decl))
2853 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2854 and then not Contains_Lib_Incomplete_Type
2857 -- When a controlled type is frozen, the expander generates stream
2858 -- and controlled-type support routines. If the freeze is caused
2859 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2860 -- expander will end up using the wrong version of these routines,
2861 -- as the body has not been processed yet. To remedy this, detect
2862 -- a late controlled primitive and create a proper spec for it.
2863 -- This ensures that the primitive will override its inherited
2864 -- counterpart before the freeze takes place.
2866 -- If the declaration we just processed is a body, do not attempt
2867 -- to examine Next_Decl as the late primitive idiom can only apply
2868 -- to the first encountered body.
2870 -- ??? A cleaner approach may be possible and/or this solution
2871 -- could be extended to general-purpose late primitives.
2873 if Present (Ctrl_Typ) then
2875 -- No need to continue searching for late body overriding if
2876 -- the controlled type is already frozen.
2878 if Is_Frozen (Ctrl_Typ) then
2881 elsif Nkind (Next_Decl) = N_Subprogram_Body then
2882 Handle_Late_Controlled_Primitive (Next_Decl);
2888 -- The generated body of an expression function does not freeze,
2889 -- unless it is a completion, in which case only the expression
2890 -- itself freezes. This is handled when the body itself is
2891 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2893 Freeze_All (Freeze_From, Decl);
2894 Freeze_From := Last_Entity (Current_Scope);
2900 -- Post-freezing actions
2903 Context := Parent (L);
2905 -- Certain contract annotations have forward visibility semantics and
2906 -- must be analyzed after all declarative items have been processed.
2907 -- This timing ensures that entities referenced by such contracts are
2910 -- Analyze the contract of an immediately enclosing package spec or
2911 -- body first because other contracts may depend on its information.
2913 if Nkind (Context) = N_Package_Body then
2914 Analyze_Package_Body_Contract (Defining_Entity (Context));
2916 elsif Nkind (Context) = N_Package_Specification then
2917 Analyze_Package_Contract (Defining_Entity (Context));
2920 -- Analyze the contracts of various constructs in the declarative
2923 Analyze_Contracts (L);
2925 if Nkind (Context) = N_Package_Body then
2927 -- Ensure that all abstract states and objects declared in the
2928 -- state space of a package body are utilized as constituents.
2930 Check_Unused_Body_States (Defining_Entity (Context));
2932 -- State refinements are visible up to the end of the package body
2933 -- declarations. Hide the state refinements from visibility to
2934 -- restore the original state conditions.
2936 Remove_Visible_Refinements (Corresponding_Spec (Context));
2937 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2939 elsif Nkind (Context) = N_Package_Specification then
2941 -- Partial state refinements are visible up to the end of the
2942 -- package spec declarations. Hide the partial state refinements
2943 -- from visibility to restore the original state conditions.
2945 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2948 -- Verify that all abstract states found in any package declared in
2949 -- the input declarative list have proper refinements. The check is
2950 -- performed only when the context denotes a block, entry, package,
2951 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2953 Check_State_Refinements (Context);
2955 -- Create the subprogram bodies which verify the run-time semantics
2956 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2957 -- types within the current declarative list. This ensures that all
2958 -- assertion expressions are preanalyzed and resolved at the end of
2959 -- the declarative part. Note that the resolution happens even when
2960 -- freezing does not take place.
2962 Build_Assertion_Bodies (L, Context);
2964 end Analyze_Declarations;
2966 -----------------------------------
2967 -- Analyze_Full_Type_Declaration --
2968 -----------------------------------
2970 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2971 Def : constant Node_Id := Type_Definition (N);
2972 Def_Id : constant Entity_Id := Defining_Identifier (N);
2976 Is_Remote : constant Boolean :=
2977 (Is_Remote_Types (Current_Scope)
2978 or else Is_Remote_Call_Interface (Current_Scope))
2979 and then not (In_Private_Part (Current_Scope)
2980 or else In_Package_Body (Current_Scope));
2982 procedure Check_Nonoverridable_Aspects;
2983 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2984 -- be overridden, and can only be confirmed on derivation.
2986 procedure Check_Ops_From_Incomplete_Type;
2987 -- If there is a tagged incomplete partial view of the type, traverse
2988 -- the primitives of the incomplete view and change the type of any
2989 -- controlling formals and result to indicate the full view. The
2990 -- primitives will be added to the full type's primitive operations
2991 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2992 -- is called from Process_Incomplete_Dependents).
2994 ----------------------------------
2995 -- Check_Nonoverridable_Aspects --
2996 ----------------------------------
2998 procedure Check_Nonoverridable_Aspects is
2999 function Get_Aspect_Spec
3001 Aspect_Name : Name_Id) return Node_Id;
3002 -- Check whether a list of aspect specifications includes an entry
3003 -- for a specific aspect. The list is either that of a partial or
3006 ---------------------
3007 -- Get_Aspect_Spec --
3008 ---------------------
3010 function Get_Aspect_Spec
3012 Aspect_Name : Name_Id) return Node_Id
3017 Spec := First (Specs);
3018 while Present (Spec) loop
3019 if Chars (Identifier (Spec)) = Aspect_Name then
3026 end Get_Aspect_Spec;
3030 Prev_Aspects : constant List_Id :=
3031 Aspect_Specifications (Parent (Def_Id));
3032 Par_Type : Entity_Id;
3033 Prev_Aspect : Node_Id;
3035 -- Start of processing for Check_Nonoverridable_Aspects
3038 -- Get parent type of derived type. Note that Prev is the entity in
3039 -- the partial declaration, but its contents are now those of full
3040 -- view, while Def_Id reflects the partial view.
3042 if Is_Private_Type (Def_Id) then
3043 Par_Type := Etype (Full_View (Def_Id));
3045 Par_Type := Etype (Def_Id);
3048 -- If there is an inherited Implicit_Dereference, verify that it is
3049 -- made explicit in the partial view.
3051 if Has_Discriminants (Base_Type (Par_Type))
3052 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
3053 and then Present (Discriminant_Specifications (Parent (Prev)))
3054 and then Present (Get_Reference_Discriminant (Par_Type))
3057 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
3061 (Discriminant_Specifications
3062 (Original_Node (Parent (Prev))))
3065 ("type does not inherit implicit dereference", Prev);
3068 -- If one of the views has the aspect specified, verify that it
3069 -- is consistent with that of the parent.
3072 Cur_Discr : constant Entity_Id :=
3073 Get_Reference_Discriminant (Prev);
3074 Par_Discr : constant Entity_Id :=
3075 Get_Reference_Discriminant (Par_Type);
3078 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3080 ("aspect inconsistent with that of parent", N);
3083 -- Check that specification in partial view matches the
3084 -- inherited aspect. Compare names directly because aspect
3085 -- expression may not be analyzed.
3087 if Present (Prev_Aspect)
3088 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3089 and then Chars (Expression (Prev_Aspect)) /=
3093 ("aspect inconsistent with that of parent", N);
3099 -- What about other nonoverridable aspects???
3100 end Check_Nonoverridable_Aspects;
3102 ------------------------------------
3103 -- Check_Ops_From_Incomplete_Type --
3104 ------------------------------------
3106 procedure Check_Ops_From_Incomplete_Type is
3113 and then Ekind (Prev) = E_Incomplete_Type
3114 and then Is_Tagged_Type (Prev)
3115 and then Is_Tagged_Type (T)
3116 and then Present (Primitive_Operations (Prev))
3118 Elmt := First_Elmt (Primitive_Operations (Prev));
3119 while Present (Elmt) loop
3122 Formal := First_Formal (Op);
3123 while Present (Formal) loop
3124 if Etype (Formal) = Prev then
3125 Set_Etype (Formal, T);
3128 Next_Formal (Formal);
3131 if Etype (Op) = Prev then
3138 end Check_Ops_From_Incomplete_Type;
3140 -- Start of processing for Analyze_Full_Type_Declaration
3143 Prev := Find_Type_Name (N);
3145 -- The full view, if present, now points to the current type. If there
3146 -- is an incomplete partial view, set a link to it, to simplify the
3147 -- retrieval of primitive operations of the type.
3149 -- Ada 2005 (AI-50217): If the type was previously decorated when
3150 -- imported through a LIMITED WITH clause, it appears as incomplete
3151 -- but has no full view.
3153 if Ekind (Prev) = E_Incomplete_Type
3154 and then Present (Full_View (Prev))
3156 T := Full_View (Prev);
3157 Set_Incomplete_View (N, Parent (Prev));
3162 Set_Is_Pure (T, Is_Pure (Current_Scope));
3164 -- We set the flag Is_First_Subtype here. It is needed to set the
3165 -- corresponding flag for the Implicit class-wide-type created
3166 -- during tagged types processing.
3168 Set_Is_First_Subtype (T, True);
3170 -- Only composite types other than array types are allowed to have
3175 -- For derived types, the rule will be checked once we've figured
3176 -- out the parent type.
3178 when N_Derived_Type_Definition =>
3181 -- For record types, discriminants are allowed.
3183 when N_Record_Definition =>
3187 if Present (Discriminant_Specifications (N)) then
3189 ("elementary or array type cannot have discriminants",
3191 (First (Discriminant_Specifications (N))));
3195 -- Elaborate the type definition according to kind, and generate
3196 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3197 -- already done (this happens during the reanalysis that follows a call
3198 -- to the high level optimizer).
3200 if not Analyzed (T) then
3203 -- Set the SPARK mode from the current context
3205 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3206 Set_SPARK_Pragma_Inherited (T);
3209 when N_Access_To_Subprogram_Definition =>
3210 Access_Subprogram_Declaration (T, Def);
3212 -- If this is a remote access to subprogram, we must create the
3213 -- equivalent fat pointer type, and related subprograms.
3216 Process_Remote_AST_Declaration (N);
3219 -- Validate categorization rule against access type declaration
3220 -- usually a violation in Pure unit, Shared_Passive unit.
3222 Validate_Access_Type_Declaration (T, N);
3224 -- If the type has contracts, we create the corresponding
3225 -- wrapper at once, before analyzing the aspect specifications,
3226 -- so that pre/postconditions can be handled directly on the
3227 -- generated wrapper.
3229 if Ada_Version >= Ada_2022
3230 and then Present (Aspect_Specifications (N))
3232 Build_Access_Subprogram_Wrapper (N);
3235 when N_Access_To_Object_Definition =>
3236 Access_Type_Declaration (T, Def);
3238 -- Validate categorization rule against access type declaration
3239 -- usually a violation in Pure unit, Shared_Passive unit.
3241 Validate_Access_Type_Declaration (T, N);
3243 -- If we are in a Remote_Call_Interface package and define a
3244 -- RACW, then calling stubs and specific stream attributes
3248 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3250 Add_RACW_Features (Def_Id);
3253 when N_Array_Type_Definition =>
3254 Array_Type_Declaration (T, Def);
3256 when N_Derived_Type_Definition =>
3257 Derived_Type_Declaration (T, N, T /= Def_Id);
3259 -- Inherit predicates from parent, and protect against illegal
3262 if Is_Type (T) and then Has_Predicates (T) then
3263 Set_Has_Predicates (Def_Id);
3266 -- Save the scenario for examination by the ABE Processing
3269 Record_Elaboration_Scenario (N);
3271 when N_Enumeration_Type_Definition =>
3272 Enumeration_Type_Declaration (T, Def);
3274 when N_Floating_Point_Definition =>
3275 Floating_Point_Type_Declaration (T, Def);
3277 when N_Decimal_Fixed_Point_Definition =>
3278 Decimal_Fixed_Point_Type_Declaration (T, Def);
3280 when N_Ordinary_Fixed_Point_Definition =>
3281 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3283 when N_Signed_Integer_Type_Definition =>
3284 Signed_Integer_Type_Declaration (T, Def);
3286 when N_Modular_Type_Definition =>
3287 Modular_Type_Declaration (T, Def);
3289 when N_Record_Definition =>
3290 Record_Type_Declaration (T, N, Prev);
3292 -- If declaration has a parse error, nothing to elaborate.
3298 raise Program_Error;
3302 if Etype (T) = Any_Type then
3306 -- Set the primitives list of the full type and its base type when
3307 -- needed. T may be E_Void in cases of earlier errors, and in that
3308 -- case we bypass this.
3310 if Ekind (T) /= E_Void
3311 and then not Present (Direct_Primitive_Operations (T))
3313 if Etype (T) = T then
3314 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3316 -- If Etype of T is the base type (as opposed to a parent type) and
3317 -- already has an associated list of primitive operations, then set
3318 -- T's primitive list to the base type's list. Otherwise, create a
3319 -- new empty primitives list and share the list between T and its
3320 -- base type. The lists need to be shared in common between the two.
3322 elsif Etype (T) = Base_Type (T) then
3324 if not Present (Direct_Primitive_Operations (Base_Type (T))) then
3325 Set_Direct_Primitive_Operations
3326 (Base_Type (T), New_Elmt_List);
3329 Set_Direct_Primitive_Operations
3330 (T, Direct_Primitive_Operations (Base_Type (T)));
3332 -- Case where the Etype is a parent type, so we need a new primitives
3336 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3340 -- Some common processing for all types
3342 Set_Depends_On_Private (T, Has_Private_Component (T));
3343 Check_Ops_From_Incomplete_Type;
3345 -- Both the declared entity, and its anonymous base type if one was
3346 -- created, need freeze nodes allocated.
3349 B : constant Entity_Id := Base_Type (T);
3352 -- In the case where the base type differs from the first subtype, we
3353 -- pre-allocate a freeze node, and set the proper link to the first
3354 -- subtype. Freeze_Entity will use this preallocated freeze node when
3355 -- it freezes the entity.
3357 -- This does not apply if the base type is a generic type, whose
3358 -- declaration is independent of the current derived definition.
3360 if B /= T and then not Is_Generic_Type (B) then
3361 Ensure_Freeze_Node (B);
3362 Set_First_Subtype_Link (Freeze_Node (B), T);
3365 -- A type that is imported through a limited_with clause cannot
3366 -- generate any code, and thus need not be frozen. However, an access
3367 -- type with an imported designated type needs a finalization list,
3368 -- which may be referenced in some other package that has non-limited
3369 -- visibility on the designated type. Thus we must create the
3370 -- finalization list at the point the access type is frozen, to
3371 -- prevent unsatisfied references at link time.
3373 if not From_Limited_With (T) or else Is_Access_Type (T) then
3374 Set_Has_Delayed_Freeze (T);
3378 -- Case where T is the full declaration of some private type which has
3379 -- been swapped in Defining_Identifier (N).
3381 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3382 Process_Full_View (N, T, Def_Id);
3384 -- Record the reference. The form of this is a little strange, since
3385 -- the full declaration has been swapped in. So the first parameter
3386 -- here represents the entity to which a reference is made which is
3387 -- the "real" entity, i.e. the one swapped in, and the second
3388 -- parameter provides the reference location.
3390 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3391 -- since we don't want a complaint about the full type being an
3392 -- unwanted reference to the private type
3395 B : constant Boolean := Has_Pragma_Unreferenced (T);
3397 Set_Has_Pragma_Unreferenced (T, False);
3398 Generate_Reference (T, T, 'c');
3399 Set_Has_Pragma_Unreferenced (T, B);
3402 Set_Completion_Referenced (Def_Id);
3404 -- For completion of incomplete type, process incomplete dependents
3405 -- and always mark the full type as referenced (it is the incomplete
3406 -- type that we get for any real reference).
3408 elsif Ekind (Prev) = E_Incomplete_Type then
3409 Process_Incomplete_Dependents (N, T, Prev);
3410 Generate_Reference (Prev, Def_Id, 'c');
3411 Set_Completion_Referenced (Def_Id);
3413 -- If not private type or incomplete type completion, this is a real
3414 -- definition of a new entity, so record it.
3417 Generate_Definition (Def_Id);
3420 -- Propagate any pending access types whose finalization masters need to
3421 -- be fully initialized from the partial to the full view. Guard against
3422 -- an illegal full view that remains unanalyzed.
3424 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3425 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3428 if Chars (Scope (Def_Id)) = Name_System
3429 and then Chars (Def_Id) = Name_Address
3430 and then In_Predefined_Unit (N)
3432 Set_Is_Descendant_Of_Address (Def_Id);
3433 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3434 Set_Is_Descendant_Of_Address (Prev);
3437 Set_Optimize_Alignment_Flags (Def_Id);
3438 Check_Eliminated (Def_Id);
3440 -- If the declaration is a completion and aspects are present, apply
3441 -- them to the entity for the type which is currently the partial
3442 -- view, but which is the one that will be frozen.
3444 if Has_Aspects (N) then
3446 -- In most cases the partial view is a private type, and both views
3447 -- appear in different declarative parts. In the unusual case where
3448 -- the partial view is incomplete, perform the analysis on the
3449 -- full view, to prevent freezing anomalies with the corresponding
3450 -- class-wide type, which otherwise might be frozen before the
3451 -- dispatch table is built.
3454 and then Ekind (Prev) /= E_Incomplete_Type
3456 Analyze_Aspect_Specifications (N, Prev);
3461 Analyze_Aspect_Specifications (N, Def_Id);
3465 if Is_Derived_Type (Prev)
3466 and then Def_Id /= Prev
3468 Check_Nonoverridable_Aspects;
3470 end Analyze_Full_Type_Declaration;
3472 ----------------------------------
3473 -- Analyze_Incomplete_Type_Decl --
3474 ----------------------------------
3476 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3477 F : constant Boolean := Is_Pure (Current_Scope);
3481 Generate_Definition (Defining_Identifier (N));
3483 -- Process an incomplete declaration. The identifier must not have been
3484 -- declared already in the scope. However, an incomplete declaration may
3485 -- appear in the private part of a package, for a private type that has
3486 -- already been declared.
3488 -- In this case, the discriminants (if any) must match
3490 T := Find_Type_Name (N);
3492 Mutate_Ekind (T, E_Incomplete_Type);
3494 Set_Is_First_Subtype (T);
3495 Reinit_Size_Align (T);
3497 -- Set the SPARK mode from the current context
3499 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3500 Set_SPARK_Pragma_Inherited (T);
3502 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3503 -- incomplete types.
3505 if Tagged_Present (N) then
3506 Set_Is_Tagged_Type (T, True);
3507 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3508 Make_Class_Wide_Type (T);
3509 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3512 Set_Stored_Constraint (T, No_Elist);
3514 if Present (Discriminant_Specifications (N)) then
3516 Process_Discriminants (N);
3520 -- If the type has discriminants, nontrivial subtypes may be declared
3521 -- before the full view of the type. The full views of those subtypes
3522 -- will be built after the full view of the type.
3524 Set_Private_Dependents (T, New_Elmt_List);
3526 end Analyze_Incomplete_Type_Decl;
3528 -----------------------------------
3529 -- Analyze_Interface_Declaration --
3530 -----------------------------------
3532 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3533 CW : constant Entity_Id := Class_Wide_Type (T);
3536 Set_Is_Tagged_Type (T);
3537 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3539 Set_Is_Limited_Record (T, Limited_Present (Def)
3540 or else Task_Present (Def)
3541 or else Protected_Present (Def)
3542 or else Synchronized_Present (Def));
3544 -- Type is abstract if full declaration carries keyword, or if previous
3545 -- partial view did.
3547 Set_Is_Abstract_Type (T);
3548 Set_Is_Interface (T);
3550 -- Type is a limited interface if it includes the keyword limited, task,
3551 -- protected, or synchronized.
3553 Set_Is_Limited_Interface
3554 (T, Limited_Present (Def)
3555 or else Protected_Present (Def)
3556 or else Synchronized_Present (Def)
3557 or else Task_Present (Def));
3559 Set_Interfaces (T, New_Elmt_List);
3560 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3562 -- Complete the decoration of the class-wide entity if it was already
3563 -- built (i.e. during the creation of the limited view)
3565 if Present (CW) then
3566 Set_Is_Interface (CW);
3567 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3570 -- Check runtime support for synchronized interfaces
3572 if Is_Concurrent_Interface (T)
3573 and then not RTE_Available (RE_Select_Specific_Data)
3575 Error_Msg_CRT ("synchronized interfaces", T);
3577 end Analyze_Interface_Declaration;
3579 -----------------------------
3580 -- Analyze_Itype_Reference --
3581 -----------------------------
3583 -- Nothing to do. This node is placed in the tree only for the benefit of
3584 -- back end processing, and has no effect on the semantic processing.
3586 procedure Analyze_Itype_Reference (N : Node_Id) is
3588 pragma Assert (Is_Itype (Itype (N)));
3590 end Analyze_Itype_Reference;
3592 --------------------------------
3593 -- Analyze_Number_Declaration --
3594 --------------------------------
3596 procedure Analyze_Number_Declaration (N : Node_Id) is
3597 E : constant Node_Id := Expression (N);
3598 Id : constant Entity_Id := Defining_Identifier (N);
3599 Index : Interp_Index;
3604 Generate_Definition (Id);
3607 -- This is an optimization of a common case of an integer literal
3609 if Nkind (E) = N_Integer_Literal then
3610 Set_Is_Static_Expression (E, True);
3611 Set_Etype (E, Universal_Integer);
3613 Set_Etype (Id, Universal_Integer);
3614 Mutate_Ekind (Id, E_Named_Integer);
3615 Set_Is_Frozen (Id, True);
3617 Set_Debug_Info_Needed (Id);
3621 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3623 -- Process expression, replacing error by integer zero, to avoid
3624 -- cascaded errors or aborts further along in the processing
3626 -- Replace Error by integer zero, which seems least likely to cause
3630 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3631 Set_Error_Posted (E);
3636 -- Verify that the expression is static and numeric. If
3637 -- the expression is overloaded, we apply the preference
3638 -- rule that favors root numeric types.
3640 if not Is_Overloaded (E) then
3642 if Has_Dynamic_Predicate_Aspect (T) then
3644 ("subtype has dynamic predicate, "
3645 & "not allowed in number declaration", N);
3651 Get_First_Interp (E, Index, It);
3652 while Present (It.Typ) loop
3653 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3654 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3656 if T = Any_Type then
3659 elsif Is_Universal_Numeric_Type (It.Typ) then
3660 -- Choose universal interpretation over any other
3667 Get_Next_Interp (Index, It);
3671 if Is_Integer_Type (T) then
3673 Set_Etype (Id, Universal_Integer);
3674 Mutate_Ekind (Id, E_Named_Integer);
3676 elsif Is_Real_Type (T) then
3678 -- Because the real value is converted to universal_real, this is a
3679 -- legal context for a universal fixed expression.
3681 if T = Universal_Fixed then
3683 Loc : constant Source_Ptr := Sloc (N);
3684 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3686 New_Occurrence_Of (Universal_Real, Loc),
3687 Expression => Relocate_Node (E));
3694 elsif T = Any_Fixed then
3695 Error_Msg_N ("illegal context for mixed mode operation", E);
3697 -- Expression is of the form : universal_fixed * integer. Try to
3698 -- resolve as universal_real.
3700 T := Universal_Real;
3705 Set_Etype (Id, Universal_Real);
3706 Mutate_Ekind (Id, E_Named_Real);
3709 Wrong_Type (E, Any_Numeric);
3713 Mutate_Ekind (Id, E_Constant);
3714 Set_Never_Set_In_Source (Id, True);
3715 Set_Is_True_Constant (Id, True);
3719 if Nkind (E) in N_Integer_Literal | N_Real_Literal then
3720 Set_Etype (E, Etype (Id));
3723 if not Is_OK_Static_Expression (E) then
3724 Flag_Non_Static_Expr
3725 ("non-static expression used in number declaration!", E);
3726 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3727 Set_Etype (E, Any_Type);
3730 Analyze_Dimension (N);
3731 end Analyze_Number_Declaration;
3733 --------------------------------
3734 -- Analyze_Object_Declaration --
3735 --------------------------------
3737 -- WARNING: This routine manages Ghost regions. Return statements must be
3738 -- replaced by gotos which jump to the end of the routine and restore the
3741 procedure Analyze_Object_Declaration (N : Node_Id) is
3742 Loc : constant Source_Ptr := Sloc (N);
3743 Id : constant Entity_Id := Defining_Identifier (N);
3744 Next_Decl : constant Node_Id := Next (N);
3749 E : Node_Id := Expression (N);
3750 -- E is set to Expression (N) throughout this routine. When Expression
3751 -- (N) is modified, E is changed accordingly.
3753 procedure Check_Dynamic_Object (Typ : Entity_Id);
3754 -- A library-level object with nonstatic discriminant constraints may
3755 -- require dynamic allocation. The declaration is illegal if the
3756 -- profile includes the restriction No_Implicit_Heap_Allocations.
3758 procedure Check_For_Null_Excluding_Components
3759 (Obj_Typ : Entity_Id;
3760 Obj_Decl : Node_Id);
3761 -- Verify that each null-excluding component of object declaration
3762 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3763 -- a compile-time warning if this is not the case.
3765 function Count_Tasks (T : Entity_Id) return Uint;
3766 -- This function is called when a non-generic library level object of a
3767 -- task type is declared. Its function is to count the static number of
3768 -- tasks declared within the type (it is only called if Has_Task is set
3769 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3770 -- or a variant record type is encountered, Check_Restriction is called
3771 -- indicating the count is unknown.
3773 function Delayed_Aspect_Present return Boolean;
3774 -- If the declaration has an expression that is an aggregate, and it
3775 -- has aspects that require delayed analysis, the resolution of the
3776 -- aggregate must be deferred to the freeze point of the object. This
3777 -- special processing was created for address clauses, but it must
3778 -- also apply to address aspects. This must be done before the aspect
3779 -- specifications are analyzed because we must handle the aggregate
3780 -- before the analysis of the object declaration is complete.
3782 -- Any other relevant delayed aspects on object declarations ???
3784 --------------------------
3785 -- Check_Dynamic_Object --
3786 --------------------------
3788 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3790 Obj_Type : Entity_Id;
3795 if Is_Private_Type (Obj_Type)
3796 and then Present (Full_View (Obj_Type))
3798 Obj_Type := Full_View (Obj_Type);
3801 if Known_Static_Esize (Obj_Type) then
3805 if Restriction_Active (No_Implicit_Heap_Allocations)
3806 and then Expander_Active
3807 and then Has_Discriminants (Obj_Type)
3809 Comp := First_Component (Obj_Type);
3810 while Present (Comp) loop
3811 if Known_Static_Esize (Etype (Comp))
3812 or else Size_Known_At_Compile_Time (Etype (Comp))
3816 elsif not Discriminated_Size (Comp)
3817 and then Comes_From_Source (Comp)
3820 ("component& of non-static size will violate restriction "
3821 & "No_Implicit_Heap_Allocation?", N, Comp);
3823 elsif Is_Record_Type (Etype (Comp)) then
3824 Check_Dynamic_Object (Etype (Comp));
3827 Next_Component (Comp);
3830 end Check_Dynamic_Object;
3832 -----------------------------------------
3833 -- Check_For_Null_Excluding_Components --
3834 -----------------------------------------
3836 procedure Check_For_Null_Excluding_Components
3837 (Obj_Typ : Entity_Id;
3840 procedure Check_Component
3841 (Comp_Typ : Entity_Id;
3842 Comp_Decl : Node_Id := Empty;
3843 Array_Comp : Boolean := False);
3844 -- Apply a compile-time null-exclusion check on a component denoted
3845 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3846 -- subcomponents (if any).
3848 ---------------------
3849 -- Check_Component --
3850 ---------------------
3852 procedure Check_Component
3853 (Comp_Typ : Entity_Id;
3854 Comp_Decl : Node_Id := Empty;
3855 Array_Comp : Boolean := False)
3861 -- Do not consider internally-generated components or those that
3862 -- are already initialized.
3864 if Present (Comp_Decl)
3865 and then (not Comes_From_Source (Comp_Decl)
3866 or else Present (Expression (Comp_Decl)))
3871 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3872 and then Present (Full_View (Comp_Typ))
3874 T := Full_View (Comp_Typ);
3879 -- Verify a component of a null-excluding access type
3881 if Is_Access_Type (T)
3882 and then Can_Never_Be_Null (T)
3884 if Comp_Decl = Obj_Decl then
3885 Null_Exclusion_Static_Checks
3888 Array_Comp => Array_Comp);
3891 Null_Exclusion_Static_Checks
3894 Array_Comp => Array_Comp);
3897 -- Check array components
3899 elsif Is_Array_Type (T) then
3901 -- There is no suitable component when the object is of an
3902 -- array type. However, a namable component may appear at some
3903 -- point during the recursive inspection, but not at the top
3904 -- level. At the top level just indicate array component case.
3906 if Comp_Decl = Obj_Decl then
3907 Check_Component (Component_Type (T), Array_Comp => True);
3909 Check_Component (Component_Type (T), Comp_Decl);
3912 -- Verify all components of type T
3914 -- Note: No checks are performed on types with discriminants due
3915 -- to complexities involving variants. ???
3917 elsif (Is_Concurrent_Type (T)
3918 or else Is_Incomplete_Or_Private_Type (T)
3919 or else Is_Record_Type (T))
3920 and then not Has_Discriminants (T)
3922 Comp := First_Component (T);
3923 while Present (Comp) loop
3924 Check_Component (Etype (Comp), Parent (Comp));
3926 Next_Component (Comp);
3929 end Check_Component;
3931 -- Start processing for Check_For_Null_Excluding_Components
3934 Check_Component (Obj_Typ, Obj_Decl);
3935 end Check_For_Null_Excluding_Components;
3941 function Count_Tasks (T : Entity_Id) return Uint is
3947 if Is_Task_Type (T) then
3950 elsif Is_Record_Type (T) then
3951 if Has_Discriminants (T) then
3952 Check_Restriction (Max_Tasks, N);
3957 C := First_Component (T);
3958 while Present (C) loop
3959 V := V + Count_Tasks (Etype (C));
3966 elsif Is_Array_Type (T) then
3967 X := First_Index (T);
3968 V := Count_Tasks (Component_Type (T));
3969 while Present (X) loop
3972 if not Is_OK_Static_Subtype (C) then
3973 Check_Restriction (Max_Tasks, N);
3976 V := V * (UI_Max (Uint_0,
3977 Expr_Value (Type_High_Bound (C)) -
3978 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3991 ----------------------------
3992 -- Delayed_Aspect_Present --
3993 ----------------------------
3995 function Delayed_Aspect_Present return Boolean is
4000 if Present (Aspect_Specifications (N)) then
4001 A := First (Aspect_Specifications (N));
4003 while Present (A) loop
4004 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
4006 if A_Id = Aspect_Address then
4008 -- Set flag on object entity, for later processing at
4009 -- the freeze point.
4011 Set_Has_Delayed_Aspects (Id);
4020 end Delayed_Aspect_Present;
4024 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
4025 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
4026 -- Save the Ghost-related attributes to restore on exit
4028 Prev_Entity : Entity_Id := Empty;
4029 Related_Id : Entity_Id;
4030 Full_View_Present : Boolean := False;
4032 -- Start of processing for Analyze_Object_Declaration
4035 -- There are three kinds of implicit types generated by an
4036 -- object declaration:
4038 -- 1. Those generated by the original Object Definition
4040 -- 2. Those generated by the Expression
4042 -- 3. Those used to constrain the Object Definition with the
4043 -- expression constraints when the definition is unconstrained.
4045 -- They must be generated in this order to avoid order of elaboration
4046 -- issues. Thus the first step (after entering the name) is to analyze
4047 -- the object definition.
4049 if Constant_Present (N) then
4050 Prev_Entity := Current_Entity_In_Scope (Id);
4052 if Present (Prev_Entity)
4054 -- If the homograph is an implicit subprogram, it is overridden
4055 -- by the current declaration.
4057 ((Is_Overloadable (Prev_Entity)
4058 and then Is_Inherited_Operation (Prev_Entity))
4060 -- The current object is a discriminal generated for an entry
4061 -- family index. Even though the index is a constant, in this
4062 -- particular context there is no true constant redeclaration.
4063 -- Enter_Name will handle the visibility.
4066 (Is_Discriminal (Id)
4067 and then Ekind (Discriminal_Link (Id)) =
4068 E_Entry_Index_Parameter)
4070 -- The current object is the renaming for a generic declared
4071 -- within the instance.
4074 (Ekind (Prev_Entity) = E_Package
4075 and then Nkind (Parent (Prev_Entity)) =
4076 N_Package_Renaming_Declaration
4077 and then not Comes_From_Source (Prev_Entity)
4079 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
4081 -- The entity may be a homonym of a private component of the
4082 -- enclosing protected object, for which we create a local
4083 -- renaming declaration. The declaration is legal, even if
4084 -- useless when it just captures that component.
4087 (Ekind (Scope (Current_Scope)) = E_Protected_Type
4088 and then Nkind (Parent (Prev_Entity)) =
4089 N_Object_Renaming_Declaration))
4091 Prev_Entity := Empty;
4095 if Present (Prev_Entity) then
4097 -- The object declaration is Ghost when it completes a deferred Ghost
4100 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
4102 Constant_Redeclaration (Id, N, T);
4104 Generate_Reference (Prev_Entity, Id, 'c');
4105 Set_Completion_Referenced (Id);
4107 if Error_Posted (N) then
4109 -- Type mismatch or illegal redeclaration; do not analyze
4110 -- expression to avoid cascaded errors.
4112 T := Find_Type_Of_Object (Object_Definition (N), N);
4114 Mutate_Ekind (Id, E_Variable);
4118 -- In the normal case, enter identifier at the start to catch premature
4119 -- usage in the initialization expression.
4122 Generate_Definition (Id);
4125 Mark_Coextensions (N, Object_Definition (N));
4127 T := Find_Type_Of_Object (Object_Definition (N), N);
4129 if Nkind (Object_Definition (N)) = N_Access_Definition
4131 (Access_To_Subprogram_Definition (Object_Definition (N)))
4132 and then Protected_Present
4133 (Access_To_Subprogram_Definition (Object_Definition (N)))
4135 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4138 if Error_Posted (Id) then
4140 Mutate_Ekind (Id, E_Variable);
4145 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4146 -- out some static checks.
4148 if Ada_Version >= Ada_2005 then
4150 -- In case of aggregates we must also take care of the correct
4151 -- initialization of nested aggregates bug this is done at the
4152 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4154 if Can_Never_Be_Null (T) then
4155 if Present (Expression (N))
4156 and then Nkind (Expression (N)) = N_Aggregate
4160 elsif Comes_From_Source (Id) then
4162 Save_Typ : constant Entity_Id := Etype (Id);
4164 Set_Etype (Id, T); -- Temp. decoration for static checks
4165 Null_Exclusion_Static_Checks (N);
4166 Set_Etype (Id, Save_Typ);
4170 -- We might be dealing with an object of a composite type containing
4171 -- null-excluding components without an aggregate, so we must verify
4172 -- that such components have default initialization.
4175 Check_For_Null_Excluding_Components (T, N);
4179 -- Object is marked pure if it is in a pure scope
4181 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4183 -- If deferred constant, make sure context is appropriate. We detect
4184 -- a deferred constant as a constant declaration with no expression.
4185 -- A deferred constant can appear in a package body if its completion
4186 -- is by means of an interface pragma.
4188 if Constant_Present (N) and then No (E) then
4190 -- A deferred constant may appear in the declarative part of the
4191 -- following constructs:
4195 -- extended return statements
4198 -- subprogram bodies
4201 -- When declared inside a package spec, a deferred constant must be
4202 -- completed by a full constant declaration or pragma Import. In all
4203 -- other cases, the only proper completion is pragma Import. Extended
4204 -- return statements are flagged as invalid contexts because they do
4205 -- not have a declarative part and so cannot accommodate the pragma.
4207 if Ekind (Current_Scope) = E_Return_Statement then
4209 ("invalid context for deferred constant declaration (RM 7.4)",
4212 ("\declaration requires an initialization expression",
4214 Set_Constant_Present (N, False);
4216 -- In Ada 83, deferred constant must be of private type
4218 elsif not Is_Private_Type (T) then
4219 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4221 ("(Ada 83) deferred constant must be private type", N);
4225 -- If not a deferred constant, then the object declaration freezes
4226 -- its type, unless the object is of an anonymous type and has delayed
4227 -- aspects. In that case the type is frozen when the object itself is.
4230 Check_Fully_Declared (T, N);
4232 if Has_Delayed_Aspects (Id)
4233 and then Is_Array_Type (T)
4234 and then Is_Itype (T)
4236 Set_Has_Delayed_Freeze (T);
4238 Freeze_Before (N, T);
4242 -- If the object was created by a constrained array definition, then
4243 -- set the link in both the anonymous base type and anonymous subtype
4244 -- that are built to represent the array type to point to the object.
4246 if Nkind (Object_Definition (Declaration_Node (Id))) =
4247 N_Constrained_Array_Definition
4249 Set_Related_Array_Object (T, Id);
4250 Set_Related_Array_Object (Base_Type (T), Id);
4253 -- Check for protected objects not at library level
4255 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4256 Check_Restriction (No_Local_Protected_Objects, Id);
4259 -- Check for violation of No_Local_Timing_Events
4261 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4262 Check_Restriction (No_Local_Timing_Events, Id);
4265 -- The actual subtype of the object is the nominal subtype, unless
4266 -- the nominal one is unconstrained and obtained from the expression.
4270 if Is_Library_Level_Entity (Id) then
4271 Check_Dynamic_Object (T);
4274 -- Process initialization expression if present and not in error
4276 if Present (E) and then E /= Error then
4278 -- Generate an error in case of CPP class-wide object initialization.
4279 -- Required because otherwise the expansion of the class-wide
4280 -- assignment would try to use 'size to initialize the object
4281 -- (primitive that is not available in CPP tagged types).
4283 if Is_Class_Wide_Type (Act_T)
4285 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4287 (Present (Full_View (Root_Type (Etype (Act_T))))
4289 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4292 ("predefined assignment not available for 'C'P'P tagged types",
4296 Mark_Coextensions (N, E);
4299 -- In case of errors detected in the analysis of the expression,
4300 -- decorate it with the expected type to avoid cascaded errors.
4302 if No (Etype (E)) then
4306 -- If an initialization expression is present, then we set the
4307 -- Is_True_Constant flag. It will be reset if this is a variable
4308 -- and it is indeed modified.
4310 Set_Is_True_Constant (Id, True);
4312 -- If we are analyzing a constant declaration, set its completion
4313 -- flag after analyzing and resolving the expression.
4315 if Constant_Present (N) then
4316 Set_Has_Completion (Id);
4319 -- Set type and resolve (type may be overridden later on). Note:
4320 -- Ekind (Id) must still be E_Void at this point so that incorrect
4321 -- early usage within E is properly diagnosed.
4325 -- If the expression is an aggregate we must look ahead to detect
4326 -- the possible presence of an address clause, and defer resolution
4327 -- and expansion of the aggregate to the freeze point of the entity.
4329 -- This is not always legal because the aggregate may contain other
4330 -- references that need freezing, e.g. references to other entities
4331 -- with address clauses. In any case, when compiling with -gnatI the
4332 -- presence of the address clause must be ignored.
4334 if Comes_From_Source (N)
4335 and then Expander_Active
4336 and then Nkind (E) = N_Aggregate
4338 ((Present (Following_Address_Clause (N))
4339 and then not Ignore_Rep_Clauses)
4340 or else Delayed_Aspect_Present)
4344 -- If the aggregate is limited it will be built in place, and its
4345 -- expansion is deferred until the object declaration is expanded.
4347 -- This is also required when generating C code to ensure that an
4348 -- object with an alignment or address clause can be initialized
4349 -- by means of component by component assignments.
4351 if Is_Limited_Type (T) or else Modify_Tree_For_C then
4352 Set_Expansion_Delayed (E);
4356 -- If the expression is a formal that is a "subprogram pointer"
4357 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4358 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4359 -- the corresponding check, as is done for assignments.
4361 if Is_Entity_Name (E)
4362 and then Present (Entity (E))
4363 and then Is_Formal (Entity (E))
4365 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4366 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4368 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4374 -- No further action needed if E is a call to an inlined function
4375 -- which returns an unconstrained type and it has been expanded into
4376 -- a procedure call. In that case N has been replaced by an object
4377 -- declaration without initializing expression and it has been
4378 -- analyzed (see Expand_Inlined_Call).
4380 if Back_End_Inlining
4381 and then Expander_Active
4382 and then Nkind (E) = N_Function_Call
4383 and then Nkind (Name (E)) in N_Has_Entity
4384 and then Is_Inlined (Entity (Name (E)))
4385 and then not Is_Constrained (Etype (E))
4386 and then Analyzed (N)
4387 and then No (Expression (N))
4392 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4393 -- node (which was marked already-analyzed), we need to set the type
4394 -- to something other than Any_Access in order to keep gigi happy.
4396 if Etype (E) = Any_Access then
4400 -- If the object is an access to variable, the initialization
4401 -- expression cannot be an access to constant.
4403 if Is_Access_Type (T)
4404 and then not Is_Access_Constant (T)
4405 and then Is_Access_Type (Etype (E))
4406 and then Is_Access_Constant (Etype (E))
4409 ("access to variable cannot be initialized with an "
4410 & "access-to-constant expression", E);
4413 if not Assignment_OK (N) then
4414 Check_Initialization (T, E);
4417 Check_Unset_Reference (E);
4419 -- If this is a variable, then set current value. If this is a
4420 -- declared constant of a scalar type with a static expression,
4421 -- indicate that it is always valid.
4423 if not Constant_Present (N) then
4424 if Compile_Time_Known_Value (E) then
4425 Set_Current_Value (Id, E);
4428 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4429 Set_Is_Known_Valid (Id);
4431 -- If it is a constant initialized with a valid nonstatic entity,
4432 -- the constant is known valid as well, and can inherit the subtype
4433 -- of the entity if it is a subtype of the given type. This info
4434 -- is preserved on the actual subtype of the constant.
4436 elsif Is_Scalar_Type (T)
4437 and then Is_Entity_Name (E)
4438 and then Is_Known_Valid (Entity (E))
4439 and then In_Subrange_Of (Etype (Entity (E)), T)
4441 Set_Is_Known_Valid (Id);
4442 Mutate_Ekind (Id, E_Constant);
4443 Set_Actual_Subtype (Id, Etype (Entity (E)));
4446 -- Deal with setting of null flags
4448 if Is_Access_Type (T) then
4449 if Known_Non_Null (E) then
4450 Set_Is_Known_Non_Null (Id, True);
4451 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4452 Set_Is_Known_Null (Id, True);
4456 -- Check incorrect use of dynamically tagged expressions
4458 if Is_Tagged_Type (T) then
4459 Check_Dynamically_Tagged_Expression
4465 Apply_Scalar_Range_Check (E, T);
4466 Apply_Static_Length_Check (E, T);
4468 -- A formal parameter of a specific tagged type whose related
4469 -- subprogram is subject to pragma Extensions_Visible with value
4470 -- "False" cannot be implicitly converted to a class-wide type by
4471 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4472 -- not consider internally generated expressions.
4474 if Is_Class_Wide_Type (T)
4475 and then Comes_From_Source (E)
4476 and then Is_EVF_Expression (E)
4479 ("formal parameter cannot be implicitly converted to "
4480 & "class-wide type when Extensions_Visible is False", E);
4484 -- If the No_Streams restriction is set, check that the type of the
4485 -- object is not, and does not contain, any subtype derived from
4486 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4487 -- Has_Stream just for efficiency reasons. There is no point in
4488 -- spending time on a Has_Stream check if the restriction is not set.
4490 if Restriction_Check_Required (No_Streams) then
4491 if Has_Stream (T) then
4492 Check_Restriction (No_Streams, N);
4496 -- Deal with predicate check before we start to do major rewriting. It
4497 -- is OK to initialize and then check the initialized value, since the
4498 -- object goes out of scope if we get a predicate failure. Note that we
4499 -- do this in the analyzer and not the expander because the analyzer
4500 -- does some substantial rewriting in some cases.
4502 -- We need a predicate check if the type has predicates that are not
4503 -- ignored, and if either there is an initializing expression, or for
4504 -- default initialization when we have at least one case of an explicit
4505 -- default initial value (including via a Default_Value or
4506 -- Default_Component_Value aspect, see AI12-0301) and then this is not
4507 -- an internal declaration whose initialization comes later (as for an
4508 -- aggregate expansion).
4509 -- If expression is an aggregate it may be expanded into assignments
4510 -- and the declaration itself is marked with No_Initialization, but
4511 -- the predicate still applies.
4513 if not Suppress_Assignment_Checks (N)
4514 and then (Predicate_Enabled (T) or else Has_Static_Predicate (T))
4516 (not No_Initialization (N)
4517 or else (Present (E) and then Nkind (E) = N_Aggregate))
4521 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4523 -- If the type has a static predicate and the expression is known at
4524 -- compile time, see if the expression satisfies the predicate.
4525 -- In the case of a static expression, this must be done even if
4526 -- the predicate is not enabled (as per static expression rules).
4529 Check_Expression_Against_Static_Predicate (E, T);
4532 -- Do not perform further predicate-related checks unless
4533 -- predicates are enabled for the subtype.
4535 if not Predicate_Enabled (T) then
4538 -- If the type is a null record and there is no explicit initial
4539 -- expression, no predicate check applies.
4541 elsif No (E) and then Is_Null_Record_Type (T) then
4544 -- Do not generate a predicate check if the initialization expression
4545 -- is a type conversion because the conversion has been subjected to
4546 -- the same check. This is a small optimization which avoid redundant
4549 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4553 -- The check must be inserted after the expanded aggregate
4554 -- expansion code, if any.
4557 Check : constant Node_Id :=
4558 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4561 if No (Next_Decl) then
4562 Append_To (List_Containing (N), Check);
4564 Insert_Before (Next_Decl, Check);
4570 -- Case of unconstrained type
4572 if not Is_Definite_Subtype (T) then
4574 -- Nothing to do in deferred constant case
4576 if Constant_Present (N) and then No (E) then
4579 -- Case of no initialization present
4582 if No_Initialization (N) then
4585 elsif Is_Class_Wide_Type (T) then
4587 ("initialization required in class-wide declaration", N);
4591 ("unconstrained subtype not allowed (need initialization)",
4592 Object_Definition (N));
4594 if Is_Record_Type (T) and then Has_Discriminants (T) then
4596 ("\provide initial value or explicit discriminant values",
4597 Object_Definition (N));
4600 ("\or give default discriminant values for type&",
4601 Object_Definition (N), T);
4603 elsif Is_Array_Type (T) then
4605 ("\provide initial value or explicit array bounds",
4606 Object_Definition (N));
4610 -- Case of initialization present but in error. Set initial
4611 -- expression as absent (but do not make above complaints).
4613 elsif E = Error then
4614 Set_Expression (N, Empty);
4617 -- Case of initialization present
4620 -- Unconstrained variables not allowed in Ada 83
4622 if Ada_Version = Ada_83
4623 and then not Constant_Present (N)
4624 and then Comes_From_Source (Object_Definition (N))
4627 ("(Ada 83) unconstrained variable not allowed",
4628 Object_Definition (N));
4631 -- Now we constrain the variable from the initializing expression
4633 -- If the expression is an aggregate, it has been expanded into
4634 -- individual assignments. Retrieve the actual type from the
4635 -- expanded construct.
4637 if Is_Array_Type (T)
4638 and then No_Initialization (N)
4639 and then Nkind (Original_Node (E)) = N_Aggregate
4643 -- In case of class-wide interface object declarations we delay
4644 -- the generation of the equivalent record type declarations until
4645 -- its expansion because there are cases in they are not required.
4647 elsif Is_Interface (T) then
4650 -- If the type is an unchecked union, no subtype can be built from
4651 -- the expression. Rewrite declaration as a renaming, which the
4652 -- back-end can handle properly. This is a rather unusual case,
4653 -- because most unchecked_union declarations have default values
4654 -- for discriminants and are thus not indefinite.
4656 elsif Is_Unchecked_Union (T) then
4657 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4658 Mutate_Ekind (Id, E_Constant);
4660 Mutate_Ekind (Id, E_Variable);
4663 -- If the expression is an aggregate it contains the required
4664 -- discriminant values but it has not been resolved yet, so do
4665 -- it now, and treat it as the initial expression of an object
4666 -- declaration, rather than a renaming.
4668 if Nkind (E) = N_Aggregate then
4669 Analyze_And_Resolve (E, T);
4673 Make_Object_Renaming_Declaration (Loc,
4674 Defining_Identifier => Id,
4675 Subtype_Mark => New_Occurrence_Of (T, Loc),
4678 Set_Renamed_Object (Id, E);
4679 Freeze_Before (N, T);
4685 -- Ensure that the generated subtype has a unique external name
4686 -- when the related object is public. This guarantees that the
4687 -- subtype and its bounds will not be affected by switches or
4688 -- pragmas that may offset the internal counter due to extra
4691 if Is_Public (Id) then
4694 Related_Id := Empty;
4697 -- If the object has an unconstrained array subtype with fixed
4698 -- lower bound, then sliding to that bound may be needed.
4700 if Is_Fixed_Lower_Bound_Array_Subtype (T) then
4701 Expand_Sliding_Conversion (E, T);
4704 Expand_Subtype_From_Expr
4707 Subtype_Indic => Object_Definition (N),
4709 Related_Id => Related_Id);
4711 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4714 -- Propagate attributes to full view when needed
4716 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4718 if Is_Private_Type (Act_T) and then Present (Full_View (Act_T))
4720 Full_View_Present := True;
4723 if Full_View_Present then
4724 Set_Is_Constr_Subt_For_U_Nominal (Full_View (Act_T));
4727 if Aliased_Present (N) then
4728 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4730 if Full_View_Present then
4731 Set_Is_Constr_Subt_For_UN_Aliased (Full_View (Act_T));
4735 Freeze_Before (N, Act_T);
4736 Freeze_Before (N, T);
4739 elsif Is_Array_Type (T)
4740 and then No_Initialization (N)
4741 and then (Nkind (Original_Node (E)) = N_Aggregate
4742 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4743 and then Nkind (Original_Node (Expression
4744 (Original_Node (E)))) = N_Aggregate))
4746 if not Is_Entity_Name (Object_Definition (N)) then
4748 Check_Compile_Time_Size (Act_T);
4750 if Aliased_Present (N) then
4751 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4755 -- When the given object definition and the aggregate are specified
4756 -- independently, and their lengths might differ do a length check.
4757 -- This cannot happen if the aggregate is of the form (others =>...)
4759 if not Is_Constrained (T) then
4762 elsif Nkind (E) = N_Raise_Constraint_Error then
4764 -- Aggregate is statically illegal. Place back in declaration
4766 Set_Expression (N, E);
4767 Set_No_Initialization (N, False);
4769 elsif T = Etype (E) then
4772 elsif Nkind (E) = N_Aggregate
4773 and then Present (Component_Associations (E))
4774 and then Present (Choice_List (First (Component_Associations (E))))
4776 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4782 Apply_Length_Check (E, T);
4785 -- If the type is limited unconstrained with defaulted discriminants and
4786 -- there is no expression, then the object is constrained by the
4787 -- defaults, so it is worthwhile building the corresponding subtype.
4789 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4790 and then not Is_Constrained (T)
4791 and then Has_Discriminants (T)
4794 Act_T := Build_Default_Subtype (T, N);
4796 -- Ada 2005: A limited object may be initialized by means of an
4797 -- aggregate. If the type has default discriminants it has an
4798 -- unconstrained nominal type, Its actual subtype will be obtained
4799 -- from the aggregate, and not from the default discriminants.
4804 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4806 elsif Nkind (E) = N_Function_Call
4807 and then Constant_Present (N)
4808 and then Has_Unconstrained_Elements (Etype (E))
4810 -- The back-end has problems with constants of a discriminated type
4811 -- with defaults, if the initial value is a function call. We
4812 -- generate an intermediate temporary that will receive a reference
4813 -- to the result of the call. The initialization expression then
4814 -- becomes a dereference of that temporary.
4816 Remove_Side_Effects (E);
4818 -- If this is a constant declaration of an unconstrained type and
4819 -- the initialization is an aggregate, we can use the subtype of the
4820 -- aggregate for the declared entity because it is immutable.
4822 elsif not Is_Constrained (T)
4823 and then Has_Discriminants (T)
4824 and then Constant_Present (N)
4825 and then not Has_Unchecked_Union (T)
4826 and then Nkind (E) = N_Aggregate
4831 -- Check No_Wide_Characters restriction
4833 Check_Wide_Character_Restriction (T, Object_Definition (N));
4835 -- Indicate this is not set in source. Certainly true for constants, and
4836 -- true for variables so far (will be reset for a variable if and when
4837 -- we encounter a modification in the source).
4839 Set_Never_Set_In_Source (Id);
4841 -- Now establish the proper kind and type of the object
4843 if Ekind (Id) = E_Void then
4844 Reinit_Field_To_Zero (Id, F_Next_Inlined_Subprogram);
4847 if Constant_Present (N) then
4848 Mutate_Ekind (Id, E_Constant);
4849 Set_Is_True_Constant (Id);
4852 Mutate_Ekind (Id, E_Variable);
4854 -- A variable is set as shared passive if it appears in a shared
4855 -- passive package, and is at the outer level. This is not done for
4856 -- entities generated during expansion, because those are always
4857 -- manipulated locally.
4859 if Is_Shared_Passive (Current_Scope)
4860 and then Is_Library_Level_Entity (Id)
4861 and then Comes_From_Source (Id)
4863 Set_Is_Shared_Passive (Id);
4864 Check_Shared_Var (Id, T, N);
4867 -- Set Has_Initial_Value if initializing expression present. Note
4868 -- that if there is no initializing expression, we leave the state
4869 -- of this flag unchanged (usually it will be False, but notably in
4870 -- the case of exception choice variables, it will already be true).
4873 Set_Has_Initial_Value (Id);
4877 -- Set the SPARK mode from the current context (may be overwritten later
4878 -- with explicit pragma).
4880 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4881 Set_SPARK_Pragma_Inherited (Id);
4883 -- Preserve relevant elaboration-related attributes of the context which
4884 -- are no longer available or very expensive to recompute once analysis,
4885 -- resolution, and expansion are over.
4887 Mark_Elaboration_Attributes
4892 -- Initialize alignment and size and capture alignment setting
4894 Reinit_Alignment (Id);
4896 Set_Optimize_Alignment_Flags (Id);
4898 -- Deal with aliased case
4900 if Aliased_Present (N) then
4901 Set_Is_Aliased (Id);
4903 -- AI12-001: All aliased objects are considered to be specified as
4904 -- independently addressable (RM C.6(8.1/4)).
4906 Set_Is_Independent (Id);
4908 -- If the object is aliased and the type is unconstrained with
4909 -- defaulted discriminants and there is no expression, then the
4910 -- object is constrained by the defaults, so it is worthwhile
4911 -- building the corresponding subtype.
4913 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4914 -- unconstrained, then only establish an actual subtype if the
4915 -- nominal subtype is indefinite. In definite cases the object is
4916 -- unconstrained in Ada 2005.
4919 and then Is_Record_Type (T)
4920 and then not Is_Constrained (T)
4921 and then Has_Discriminants (T)
4922 and then (Ada_Version < Ada_2005
4923 or else not Is_Definite_Subtype (T))
4925 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4929 -- Now we can set the type of the object
4931 Set_Etype (Id, Act_T);
4933 -- Non-constant object is marked to be treated as volatile if type is
4934 -- volatile and we clear the Current_Value setting that may have been
4935 -- set above. Doing so for constants isn't required and might interfere
4936 -- with possible uses of the object as a static expression in contexts
4937 -- incompatible with volatility (e.g. as a case-statement alternative).
4939 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4940 Set_Treat_As_Volatile (Id);
4941 Set_Current_Value (Id, Empty);
4944 -- Deal with controlled types
4946 if Has_Controlled_Component (Etype (Id))
4947 or else Is_Controlled (Etype (Id))
4949 if not Is_Library_Level_Entity (Id) then
4950 Check_Restriction (No_Nested_Finalization, N);
4952 Validate_Controlled_Object (Id);
4956 if Has_Task (Etype (Id)) then
4957 Check_Restriction (No_Tasking, N);
4959 -- Deal with counting max tasks
4961 -- Nothing to do if inside a generic
4963 if Inside_A_Generic then
4966 -- If library level entity, then count tasks
4968 elsif Is_Library_Level_Entity (Id) then
4969 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4971 -- If not library level entity, then indicate we don't know max
4972 -- tasks and also check task hierarchy restriction and blocking
4973 -- operation (since starting a task is definitely blocking).
4976 Check_Restriction (Max_Tasks, N);
4977 Check_Restriction (No_Task_Hierarchy, N);
4978 Check_Potentially_Blocking_Operation (N);
4981 -- A rather specialized test. If we see two tasks being declared
4982 -- of the same type in the same object declaration, and the task
4983 -- has an entry with an address clause, we know that program error
4984 -- will be raised at run time since we can't have two tasks with
4985 -- entries at the same address.
4987 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4992 E := First_Entity (Etype (Id));
4993 while Present (E) loop
4994 if Ekind (E) = E_Entry
4995 and then Present (Get_Attribute_Definition_Clause
4996 (E, Attribute_Address))
4998 Error_Msg_Warn := SPARK_Mode /= On;
5000 ("more than one task with same entry address<<", N);
5001 Error_Msg_N ("\Program_Error [<<", N);
5003 Make_Raise_Program_Error (Loc,
5004 Reason => PE_Duplicated_Entry_Address));
5014 -- Some simple constant-propagation: if the expression is a constant
5015 -- string initialized with a literal, share the literal. This avoids
5019 and then Is_Entity_Name (E)
5020 and then Ekind (Entity (E)) = E_Constant
5021 and then Base_Type (Etype (E)) = Standard_String
5024 Val : constant Node_Id := Constant_Value (Entity (E));
5026 if Present (Val) and then Nkind (Val) = N_String_Literal then
5027 Rewrite (E, New_Copy (Val));
5032 -- Another optimization: if the nominal subtype is unconstrained and
5033 -- the expression is a function call that returns an unconstrained
5034 -- type, rewrite the declaration as a renaming of the result of the
5035 -- call. The exceptions below are cases where the copy is expected,
5036 -- either by the back end (Aliased case) or by the semantics, as for
5037 -- initializing controlled types or copying tags for class-wide types.
5040 and then Nkind (E) = N_Explicit_Dereference
5041 and then Nkind (Original_Node (E)) = N_Function_Call
5042 and then not Is_Library_Level_Entity (Id)
5043 and then not Is_Constrained (Underlying_Type (T))
5044 and then not Is_Aliased (Id)
5045 and then not Is_Class_Wide_Type (T)
5046 and then not Is_Controlled (T)
5047 and then not Has_Controlled_Component (Base_Type (T))
5048 and then Expander_Active
5051 Make_Object_Renaming_Declaration (Loc,
5052 Defining_Identifier => Id,
5053 Access_Definition => Empty,
5054 Subtype_Mark => New_Occurrence_Of
5055 (Base_Type (Etype (Id)), Loc),
5058 Set_Renamed_Object (Id, E);
5060 -- Force generation of debugging information for the constant and for
5061 -- the renamed function call.
5063 Set_Debug_Info_Needed (Id);
5064 Set_Debug_Info_Needed (Entity (Prefix (E)));
5067 if Present (Prev_Entity)
5068 and then Is_Frozen (Prev_Entity)
5069 and then not Error_Posted (Id)
5071 Error_Msg_N ("full constant declaration appears too late", N);
5074 Check_Eliminated (Id);
5076 -- Deal with setting In_Private_Part flag if in private part
5078 if Ekind (Scope (Id)) = E_Package
5079 and then In_Private_Part (Scope (Id))
5081 Set_In_Private_Part (Id);
5085 -- Initialize the refined state of a variable here because this is a
5086 -- common destination for legal and illegal object declarations.
5088 if Ekind (Id) = E_Variable then
5089 Set_Encapsulating_State (Id, Empty);
5092 if Has_Aspects (N) then
5093 Analyze_Aspect_Specifications (N, Id);
5096 Analyze_Dimension (N);
5098 -- Verify whether the object declaration introduces an illegal hidden
5099 -- state within a package subject to a null abstract state.
5101 if Ekind (Id) = E_Variable then
5102 Check_No_Hidden_State (Id);
5105 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5106 end Analyze_Object_Declaration;
5108 ---------------------------
5109 -- Analyze_Others_Choice --
5110 ---------------------------
5112 -- Nothing to do for the others choice node itself, the semantic analysis
5113 -- of the others choice will occur as part of the processing of the parent
5115 procedure Analyze_Others_Choice (N : Node_Id) is
5116 pragma Warnings (Off, N);
5119 end Analyze_Others_Choice;
5121 -------------------------------------------
5122 -- Analyze_Private_Extension_Declaration --
5123 -------------------------------------------
5125 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5126 Indic : constant Node_Id := Subtype_Indication (N);
5127 T : constant Entity_Id := Defining_Identifier (N);
5129 Iface_Elmt : Elmt_Id;
5130 Parent_Base : Entity_Id;
5131 Parent_Type : Entity_Id;
5134 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5136 if Is_Non_Empty_List (Interface_List (N)) then
5142 Intf := First (Interface_List (N));
5143 while Present (Intf) loop
5144 T := Find_Type_Of_Subtype_Indic (Intf);
5146 Diagnose_Interface (Intf, T);
5152 Generate_Definition (T);
5154 -- For other than Ada 2012, just enter the name in the current scope
5156 if Ada_Version < Ada_2012 then
5159 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5160 -- case of private type that completes an incomplete type.
5167 Prev := Find_Type_Name (N);
5169 pragma Assert (Prev = T
5170 or else (Ekind (Prev) = E_Incomplete_Type
5171 and then Present (Full_View (Prev))
5172 and then Full_View (Prev) = T));
5176 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5177 Parent_Base := Base_Type (Parent_Type);
5179 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5180 Mutate_Ekind (T, Ekind (Parent_Type));
5181 Set_Etype (T, Any_Type);
5184 elsif not Is_Tagged_Type (Parent_Type) then
5186 ("parent of type extension must be a tagged type", Indic);
5189 elsif Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
5190 Error_Msg_N ("premature derivation of incomplete type", Indic);
5193 elsif Is_Concurrent_Type (Parent_Type) then
5195 ("parent type of a private extension cannot be a synchronized "
5196 & "tagged type (RM 3.9.1 (3/1))", N);
5198 Set_Etype (T, Any_Type);
5199 Mutate_Ekind (T, E_Limited_Private_Type);
5200 Set_Private_Dependents (T, New_Elmt_List);
5201 Set_Error_Posted (T);
5205 Check_Wide_Character_Restriction (Parent_Type, Indic);
5207 -- Perhaps the parent type should be changed to the class-wide type's
5208 -- specific type in this case to prevent cascading errors ???
5210 if Is_Class_Wide_Type (Parent_Type) then
5212 ("parent of type extension must not be a class-wide type", Indic);
5216 if (not Is_Package_Or_Generic_Package (Current_Scope)
5217 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5218 or else In_Private_Part (Current_Scope)
5220 Error_Msg_N ("invalid context for private extension", N);
5223 -- Set common attributes
5225 Set_Is_Pure (T, Is_Pure (Current_Scope));
5226 Set_Scope (T, Current_Scope);
5227 Mutate_Ekind (T, E_Record_Type_With_Private);
5228 Reinit_Size_Align (T);
5229 Set_Default_SSO (T);
5230 Set_No_Reordering (T, No_Component_Reordering);
5232 Set_Etype (T, Parent_Base);
5233 Propagate_Concurrent_Flags (T, Parent_Base);
5235 Set_Convention (T, Convention (Parent_Type));
5236 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5237 Set_Is_First_Subtype (T);
5238 Make_Class_Wide_Type (T);
5240 -- Set the SPARK mode from the current context
5242 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5243 Set_SPARK_Pragma_Inherited (T);
5245 if Unknown_Discriminants_Present (N) then
5246 Set_Discriminant_Constraint (T, No_Elist);
5249 Build_Derived_Record_Type (N, Parent_Type, T);
5251 -- A private extension inherits the Default_Initial_Condition pragma
5252 -- coming from any parent type within the derivation chain.
5254 if Has_DIC (Parent_Type) then
5255 Set_Has_Inherited_DIC (T);
5258 -- A private extension inherits any class-wide invariants coming from a
5259 -- parent type or an interface. Note that the invariant procedure of the
5260 -- parent type should not be inherited because the private extension may
5261 -- define invariants of its own.
5263 if Has_Inherited_Invariants (Parent_Type)
5264 or else Has_Inheritable_Invariants (Parent_Type)
5266 Set_Has_Inherited_Invariants (T);
5268 elsif Present (Interfaces (T)) then
5269 Iface_Elmt := First_Elmt (Interfaces (T));
5270 while Present (Iface_Elmt) loop
5271 Iface := Node (Iface_Elmt);
5273 if Has_Inheritable_Invariants (Iface) then
5274 Set_Has_Inherited_Invariants (T);
5278 Next_Elmt (Iface_Elmt);
5282 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5283 -- synchronized formal derived type.
5285 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5286 Set_Is_Limited_Record (T);
5288 -- Formal derived type case
5290 if Is_Generic_Type (T) then
5292 -- The parent must be a tagged limited type or a synchronized
5295 if (not Is_Tagged_Type (Parent_Type)
5296 or else not Is_Limited_Type (Parent_Type))
5298 (not Is_Interface (Parent_Type)
5299 or else not Is_Synchronized_Interface (Parent_Type))
5302 ("parent type of & must be tagged limited or synchronized",
5306 -- The progenitors (if any) must be limited or synchronized
5309 if Present (Interfaces (T)) then
5310 Iface_Elmt := First_Elmt (Interfaces (T));
5311 while Present (Iface_Elmt) loop
5312 Iface := Node (Iface_Elmt);
5314 if not Is_Limited_Interface (Iface)
5315 and then not Is_Synchronized_Interface (Iface)
5318 ("progenitor & must be limited or synchronized",
5322 Next_Elmt (Iface_Elmt);
5326 -- Regular derived extension, the parent must be a limited or
5327 -- synchronized interface.
5330 if not Is_Interface (Parent_Type)
5331 or else (not Is_Limited_Interface (Parent_Type)
5332 and then not Is_Synchronized_Interface (Parent_Type))
5335 ("parent type of & must be limited interface", N, T);
5339 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5340 -- extension with a synchronized parent must be explicitly declared
5341 -- synchronized, because the full view will be a synchronized type.
5342 -- This must be checked before the check for limited types below,
5343 -- to ensure that types declared limited are not allowed to extend
5344 -- synchronized interfaces.
5346 elsif Is_Interface (Parent_Type)
5347 and then Is_Synchronized_Interface (Parent_Type)
5348 and then not Synchronized_Present (N)
5351 ("private extension of& must be explicitly synchronized",
5354 elsif Limited_Present (N) then
5355 Set_Is_Limited_Record (T);
5357 if not Is_Limited_Type (Parent_Type)
5359 (not Is_Interface (Parent_Type)
5360 or else not Is_Limited_Interface (Parent_Type))
5362 Error_Msg_NE ("parent type& of limited extension must be limited",
5367 -- Remember that its parent type has a private extension. Used to warn
5368 -- on public primitives of the parent type defined after its private
5369 -- extensions (see Check_Dispatching_Operation).
5371 Set_Has_Private_Extension (Parent_Type);
5374 if Has_Aspects (N) then
5375 Analyze_Aspect_Specifications (N, T);
5377 end Analyze_Private_Extension_Declaration;
5379 ---------------------------------
5380 -- Analyze_Subtype_Declaration --
5381 ---------------------------------
5383 procedure Analyze_Subtype_Declaration
5385 Skip : Boolean := False)
5387 Id : constant Entity_Id := Defining_Identifier (N);
5391 Generate_Definition (Id);
5392 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5393 Reinit_Size_Align (Id);
5395 -- The following guard condition on Enter_Name is to handle cases where
5396 -- the defining identifier has already been entered into the scope but
5397 -- the declaration as a whole needs to be analyzed.
5399 -- This case in particular happens for derived enumeration types. The
5400 -- derived enumeration type is processed as an inserted enumeration type
5401 -- declaration followed by a rewritten subtype declaration. The defining
5402 -- identifier, however, is entered into the name scope very early in the
5403 -- processing of the original type declaration and therefore needs to be
5404 -- avoided here, when the created subtype declaration is analyzed. (See
5405 -- Build_Derived_Types)
5407 -- This also happens when the full view of a private type is derived
5408 -- type with constraints. In this case the entity has been introduced
5409 -- in the private declaration.
5411 -- Finally this happens in some complex cases when validity checks are
5412 -- enabled, where the same subtype declaration may be analyzed twice.
5413 -- This can happen if the subtype is created by the preanalysis of
5414 -- an attribute tht gives the range of a loop statement, and the loop
5415 -- itself appears within an if_statement that will be rewritten during
5419 or else (Present (Etype (Id))
5420 and then (Is_Private_Type (Etype (Id))
5421 or else Is_Task_Type (Etype (Id))
5422 or else Is_Rewrite_Substitution (N)))
5426 elsif Current_Entity (Id) = Id then
5433 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5435 -- Class-wide equivalent types of records with unknown discriminants
5436 -- involve the generation of an itype which serves as the private view
5437 -- of a constrained record subtype. In such cases the base type of the
5438 -- current subtype we are processing is the private itype. Use the full
5439 -- of the private itype when decorating various attributes.
5442 and then Is_Private_Type (T)
5443 and then Present (Full_View (T))
5448 -- Inherit common attributes
5450 Set_Is_Volatile (Id, Is_Volatile (T));
5451 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5452 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5453 Set_Convention (Id, Convention (T));
5455 -- If ancestor has predicates then so does the subtype, and in addition
5456 -- we must delay the freeze to properly arrange predicate inheritance.
5458 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5459 -- in which T = ID, so the above tests and assignments do nothing???
5461 if Has_Predicates (T)
5462 or else (Present (Ancestor_Subtype (T))
5463 and then Has_Predicates (Ancestor_Subtype (T)))
5465 Set_Has_Predicates (Id);
5466 Set_Has_Delayed_Freeze (Id);
5468 -- Generated subtypes inherit the predicate function from the parent
5469 -- (no aspects to examine on the generated declaration).
5471 if not Comes_From_Source (N) then
5472 Mutate_Ekind (Id, Ekind (T));
5474 if Present (Predicate_Function (Id)) then
5477 elsif Present (Predicate_Function (T)) then
5478 Set_Predicate_Function (Id, Predicate_Function (T));
5480 elsif Present (Ancestor_Subtype (T))
5481 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5483 Set_Predicate_Function (Id,
5484 Predicate_Function (Ancestor_Subtype (T)));
5489 -- In the case where there is no constraint given in the subtype
5490 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5491 -- semantic attributes must be established here.
5493 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5494 Set_Etype (Id, Base_Type (T));
5498 Mutate_Ekind (Id, E_Array_Subtype);
5499 Copy_Array_Subtype_Attributes (Id, T);
5501 when Decimal_Fixed_Point_Kind =>
5502 Mutate_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5503 Set_Digits_Value (Id, Digits_Value (T));
5504 Set_Delta_Value (Id, Delta_Value (T));
5505 Set_Scale_Value (Id, Scale_Value (T));
5506 Set_Small_Value (Id, Small_Value (T));
5507 Set_Scalar_Range (Id, Scalar_Range (T));
5508 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5509 Set_Is_Constrained (Id, Is_Constrained (T));
5510 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5511 Copy_RM_Size (To => Id, From => T);
5513 when Enumeration_Kind =>
5514 Mutate_Ekind (Id, E_Enumeration_Subtype);
5515 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5516 Set_Scalar_Range (Id, Scalar_Range (T));
5517 Set_Is_Character_Type (Id, Is_Character_Type (T));
5518 Set_Is_Constrained (Id, Is_Constrained (T));
5519 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5520 Copy_RM_Size (To => Id, From => T);
5522 when Ordinary_Fixed_Point_Kind =>
5523 Mutate_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5524 Set_Scalar_Range (Id, Scalar_Range (T));
5525 Set_Small_Value (Id, Small_Value (T));
5526 Set_Delta_Value (Id, Delta_Value (T));
5527 Set_Is_Constrained (Id, Is_Constrained (T));
5528 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5529 Copy_RM_Size (To => Id, From => T);
5532 Mutate_Ekind (Id, E_Floating_Point_Subtype);
5533 Set_Scalar_Range (Id, Scalar_Range (T));
5534 Set_Digits_Value (Id, Digits_Value (T));
5535 Set_Is_Constrained (Id, Is_Constrained (T));
5537 -- If the floating point type has dimensions, these will be
5538 -- inherited subsequently when Analyze_Dimensions is called.
5540 when Signed_Integer_Kind =>
5541 Mutate_Ekind (Id, E_Signed_Integer_Subtype);
5542 Set_Scalar_Range (Id, Scalar_Range (T));
5543 Set_Is_Constrained (Id, Is_Constrained (T));
5544 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5545 Copy_RM_Size (To => Id, From => T);
5547 when Modular_Integer_Kind =>
5548 Mutate_Ekind (Id, E_Modular_Integer_Subtype);
5549 Set_Scalar_Range (Id, Scalar_Range (T));
5550 Set_Is_Constrained (Id, Is_Constrained (T));
5551 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5552 Copy_RM_Size (To => Id, From => T);
5554 when Class_Wide_Kind =>
5555 Mutate_Ekind (Id, E_Class_Wide_Subtype);
5556 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5557 Set_Cloned_Subtype (Id, T);
5558 Set_Is_Tagged_Type (Id, True);
5559 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5560 Set_Has_Unknown_Discriminants
5562 Set_No_Tagged_Streams_Pragma
5563 (Id, No_Tagged_Streams_Pragma (T));
5565 if Ekind (T) = E_Class_Wide_Subtype then
5566 Set_Equivalent_Type (Id, Equivalent_Type (T));
5569 when E_Record_Subtype
5572 Mutate_Ekind (Id, E_Record_Subtype);
5574 -- Subtype declarations introduced for formal type parameters
5575 -- in generic instantiations should inherit the Size value of
5576 -- the type they rename.
5578 if Present (Generic_Parent_Type (N)) then
5579 Copy_RM_Size (To => Id, From => T);
5582 if Ekind (T) = E_Record_Subtype
5583 and then Present (Cloned_Subtype (T))
5585 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5587 Set_Cloned_Subtype (Id, T);
5590 Set_First_Entity (Id, First_Entity (T));
5591 Set_Last_Entity (Id, Last_Entity (T));
5592 Set_Has_Discriminants (Id, Has_Discriminants (T));
5593 Set_Is_Constrained (Id, Is_Constrained (T));
5594 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5595 Set_Has_Implicit_Dereference
5596 (Id, Has_Implicit_Dereference (T));
5597 Set_Has_Unknown_Discriminants
5598 (Id, Has_Unknown_Discriminants (T));
5600 if Has_Discriminants (T) then
5601 Set_Discriminant_Constraint
5602 (Id, Discriminant_Constraint (T));
5603 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5605 elsif Has_Unknown_Discriminants (Id) then
5606 Set_Discriminant_Constraint (Id, No_Elist);
5609 if Is_Tagged_Type (T) then
5610 Set_Is_Tagged_Type (Id, True);
5611 Set_No_Tagged_Streams_Pragma
5612 (Id, No_Tagged_Streams_Pragma (T));
5613 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5614 Set_Direct_Primitive_Operations
5615 (Id, Direct_Primitive_Operations (T));
5616 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5618 if Is_Interface (T) then
5619 Set_Is_Interface (Id);
5620 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5624 when Private_Kind =>
5625 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5626 Set_Has_Discriminants (Id, Has_Discriminants (T));
5627 Set_Is_Constrained (Id, Is_Constrained (T));
5628 Set_First_Entity (Id, First_Entity (T));
5629 Set_Last_Entity (Id, Last_Entity (T));
5630 Set_Private_Dependents (Id, New_Elmt_List);
5631 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5632 Set_Has_Implicit_Dereference
5633 (Id, Has_Implicit_Dereference (T));
5634 Set_Has_Unknown_Discriminants
5635 (Id, Has_Unknown_Discriminants (T));
5636 Set_Known_To_Have_Preelab_Init
5637 (Id, Known_To_Have_Preelab_Init (T));
5639 if Is_Tagged_Type (T) then
5640 Set_Is_Tagged_Type (Id);
5641 Set_No_Tagged_Streams_Pragma (Id,
5642 No_Tagged_Streams_Pragma (T));
5643 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5644 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5645 Set_Direct_Primitive_Operations (Id,
5646 Direct_Primitive_Operations (T));
5649 -- In general the attributes of the subtype of a private type
5650 -- are the attributes of the partial view of parent. However,
5651 -- the full view may be a discriminated type, and the subtype
5652 -- must share the discriminant constraint to generate correct
5653 -- calls to initialization procedures.
5655 if Has_Discriminants (T) then
5656 Set_Discriminant_Constraint
5657 (Id, Discriminant_Constraint (T));
5658 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5660 elsif Present (Full_View (T))
5661 and then Has_Discriminants (Full_View (T))
5663 Set_Discriminant_Constraint
5664 (Id, Discriminant_Constraint (Full_View (T)));
5665 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5667 -- This would seem semantically correct, but apparently
5668 -- generates spurious errors about missing components ???
5670 -- Set_Has_Discriminants (Id);
5673 Prepare_Private_Subtype_Completion (Id, N);
5675 -- If this is the subtype of a constrained private type with
5676 -- discriminants that has got a full view and we also have
5677 -- built a completion just above, show that the completion
5678 -- is a clone of the full view to the back-end.
5680 if Has_Discriminants (T)
5681 and then not Has_Unknown_Discriminants (T)
5682 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5683 and then Present (Full_View (T))
5684 and then Present (Full_View (Id))
5686 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5690 Mutate_Ekind (Id, E_Access_Subtype);
5691 Set_Is_Constrained (Id, Is_Constrained (T));
5692 Set_Is_Access_Constant
5693 (Id, Is_Access_Constant (T));
5694 Set_Directly_Designated_Type
5695 (Id, Designated_Type (T));
5696 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5698 -- A Pure library_item must not contain the declaration of a
5699 -- named access type, except within a subprogram, generic
5700 -- subprogram, task unit, or protected unit, or if it has
5701 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5703 if Comes_From_Source (Id)
5704 and then In_Pure_Unit
5705 and then not In_Subprogram_Task_Protected_Unit
5706 and then not No_Pool_Assigned (Id)
5709 ("named access types not allowed in pure unit", N);
5712 when Concurrent_Kind =>
5713 Mutate_Ekind (Id, Subtype_Kind (Ekind (T)));
5714 Set_Corresponding_Record_Type (Id,
5715 Corresponding_Record_Type (T));
5716 Set_First_Entity (Id, First_Entity (T));
5717 Set_First_Private_Entity (Id, First_Private_Entity (T));
5718 Set_Has_Discriminants (Id, Has_Discriminants (T));
5719 Set_Is_Constrained (Id, Is_Constrained (T));
5720 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5721 Set_Last_Entity (Id, Last_Entity (T));
5723 if Is_Tagged_Type (T) then
5724 Set_No_Tagged_Streams_Pragma
5725 (Id, No_Tagged_Streams_Pragma (T));
5728 if Has_Discriminants (T) then
5729 Set_Discriminant_Constraint
5730 (Id, Discriminant_Constraint (T));
5731 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5734 when Incomplete_Kind =>
5735 if Ada_Version >= Ada_2005 then
5737 -- In Ada 2005 an incomplete type can be explicitly tagged:
5738 -- propagate indication. Note that we also have to include
5739 -- subtypes for Ada 2012 extended use of incomplete types.
5741 Mutate_Ekind (Id, E_Incomplete_Subtype);
5742 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5743 Set_Private_Dependents (Id, New_Elmt_List);
5745 if Is_Tagged_Type (Id) then
5746 Set_No_Tagged_Streams_Pragma
5747 (Id, No_Tagged_Streams_Pragma (T));
5748 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5751 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5752 -- incomplete type visible through a limited with clause.
5754 if From_Limited_With (T)
5755 and then Present (Non_Limited_View (T))
5757 Set_From_Limited_With (Id);
5758 Set_Non_Limited_View (Id, Non_Limited_View (T));
5760 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5761 -- to the private dependents of the original incomplete
5762 -- type for future transformation.
5765 Append_Elmt (Id, Private_Dependents (T));
5768 -- If the subtype name denotes an incomplete type an error
5769 -- was already reported by Process_Subtype.
5772 Set_Etype (Id, Any_Type);
5776 raise Program_Error;
5779 -- If there is no constraint in the subtype indication, the
5780 -- declared entity inherits predicates from the parent.
5782 Inherit_Predicate_Flags (Id, T);
5785 -- When prefixed calls are enabled for untagged types, the subtype
5786 -- shares the primitive operations of its base type.
5788 if Extensions_Allowed then
5789 Set_Direct_Primitive_Operations
5790 (Id, Direct_Primitive_Operations (Base_Type (T)));
5793 if Etype (Id) = Any_Type then
5797 -- Some common processing on all types
5799 Set_Size_Info (Id, T);
5800 Set_First_Rep_Item (Id, First_Rep_Item (T));
5802 -- If the parent type is a generic actual, so is the subtype. This may
5803 -- happen in a nested instance. Why Comes_From_Source test???
5805 if not Comes_From_Source (N) then
5806 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5809 -- If this is a subtype declaration for an actual in an instance,
5810 -- inherit static and dynamic predicates if any.
5812 -- If declaration has no aspect specifications, inherit predicate
5813 -- info as well. Unclear how to handle the case of both specified
5814 -- and inherited predicates ??? Other inherited aspects, such as
5815 -- invariants, should be OK, but the combination with later pragmas
5816 -- may also require special merging.
5818 if Has_Predicates (T)
5819 and then Present (Predicate_Function (T))
5821 ((In_Instance and then not Comes_From_Source (N))
5822 or else No (Aspect_Specifications (N)))
5824 -- Inherit Subprograms_For_Type from the full view, if present
5826 if Present (Full_View (T))
5827 and then Subprograms_For_Type (Full_View (T)) /= No_Elist
5829 Set_Subprograms_For_Type
5830 (Id, Subprograms_For_Type (Full_View (T)));
5832 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5835 -- If the current declaration created both a private and a full view,
5836 -- then propagate Predicate_Function to the latter as well.
5838 if Present (Full_View (Id))
5839 and then No (Predicate_Function (Full_View (Id)))
5841 Set_Subprograms_For_Type
5842 (Full_View (Id), Subprograms_For_Type (Id));
5845 if Has_Static_Predicate (T) then
5846 Set_Has_Static_Predicate (Id);
5847 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5851 -- If the base type is a scalar type, or else if there is no
5852 -- constraint, the atomic flag is inherited by the subtype.
5853 -- Ditto for the Independent aspect.
5855 if Is_Scalar_Type (Id)
5856 or else Is_Entity_Name (Subtype_Indication (N))
5858 Set_Is_Atomic (Id, Is_Atomic (T));
5859 Set_Is_Independent (Id, Is_Independent (T));
5862 -- Remaining processing depends on characteristics of base type
5866 Set_Is_Immediately_Visible (Id, True);
5867 Set_Depends_On_Private (Id, Has_Private_Component (T));
5868 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5870 if Is_Interface (T) then
5871 Set_Is_Interface (Id);
5872 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5875 if Present (Generic_Parent_Type (N))
5877 (Nkind (Parent (Generic_Parent_Type (N))) /=
5878 N_Formal_Type_Declaration
5879 or else Nkind (Formal_Type_Definition
5880 (Parent (Generic_Parent_Type (N)))) /=
5881 N_Formal_Private_Type_Definition)
5883 if Is_Tagged_Type (Id) then
5885 -- If this is a generic actual subtype for a synchronized type,
5886 -- the primitive operations are those of the corresponding record
5887 -- for which there is a separate subtype declaration.
5889 if Is_Concurrent_Type (Id) then
5891 elsif Is_Class_Wide_Type (Id) then
5892 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5894 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5897 elsif Scope (Etype (Id)) /= Standard_Standard then
5898 Derive_Subprograms (Generic_Parent_Type (N), Id);
5902 if Is_Private_Type (T) and then Present (Full_View (T)) then
5903 Conditional_Delay (Id, Full_View (T));
5905 -- The subtypes of components or subcomponents of protected types
5906 -- do not need freeze nodes, which would otherwise appear in the
5907 -- wrong scope (before the freeze node for the protected type). The
5908 -- proper subtypes are those of the subcomponents of the corresponding
5911 elsif Ekind (Scope (Id)) /= E_Protected_Type
5912 and then Present (Scope (Scope (Id))) -- error defense
5913 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5915 Conditional_Delay (Id, T);
5918 -- If we have a subtype of an incomplete type whose full type is a
5919 -- derived numeric type, we need to have a freeze node for the subtype.
5920 -- Otherwise gigi will complain while computing the (static) bounds of
5924 and then Is_Elementary_Type (Id)
5925 and then Etype (Id) /= Id
5928 Partial : constant Entity_Id :=
5929 Incomplete_Or_Partial_View (First_Subtype (Id));
5931 if Present (Partial)
5932 and then Ekind (Partial) = E_Incomplete_Type
5934 Set_Has_Delayed_Freeze (Id);
5939 -- Check that Constraint_Error is raised for a scalar subtype indication
5940 -- when the lower or upper bound of a non-null range lies outside the
5941 -- range of the type mark. Likewise for an array subtype, but check the
5942 -- compatibility for each index.
5944 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5946 Indic_Typ : constant Entity_Id :=
5947 Etype (Subtype_Mark (Subtype_Indication (N)));
5948 Subt_Index : Node_Id;
5949 Target_Index : Node_Id;
5952 if Is_Scalar_Type (Etype (Id))
5953 and then Scalar_Range (Id) /= Scalar_Range (Indic_Typ)
5955 Apply_Range_Check (Scalar_Range (Id), Indic_Typ);
5957 elsif Is_Array_Type (Etype (Id))
5958 and then Present (First_Index (Id))
5960 Subt_Index := First_Index (Id);
5961 Target_Index := First_Index (Indic_Typ);
5963 while Present (Subt_Index) loop
5964 if ((Nkind (Subt_Index) in N_Expanded_Name | N_Identifier
5965 and then Is_Scalar_Type (Entity (Subt_Index)))
5966 or else Nkind (Subt_Index) = N_Subtype_Indication)
5968 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5971 (Scalar_Range (Etype (Subt_Index)),
5972 Etype (Target_Index),
5976 Next_Index (Subt_Index);
5977 Next_Index (Target_Index);
5983 Set_Optimize_Alignment_Flags (Id);
5984 Check_Eliminated (Id);
5987 if Has_Aspects (N) then
5988 Analyze_Aspect_Specifications (N, Id);
5991 Analyze_Dimension (N);
5993 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5994 -- indications on composite types where the constraints are dynamic.
5995 -- Note that object declarations and aggregates generate implicit
5996 -- subtype declarations, which this covers. One special case is that the
5997 -- implicitly generated "=" for discriminated types includes an
5998 -- offending subtype declaration, which is harmless, so we ignore it
6001 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
6003 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
6005 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
6006 and then not (Is_Internal (Id)
6007 and then Is_TSS (Scope (Id),
6008 TSS_Composite_Equality))
6009 and then not Within_Init_Proc
6010 and then not All_Composite_Constraints_Static (Cstr)
6012 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
6016 end Analyze_Subtype_Declaration;
6018 --------------------------------
6019 -- Analyze_Subtype_Indication --
6020 --------------------------------
6022 procedure Analyze_Subtype_Indication (N : Node_Id) is
6023 T : constant Entity_Id := Subtype_Mark (N);
6024 R : constant Node_Id := Range_Expression (Constraint (N));
6031 Set_Etype (N, Etype (R));
6032 Resolve (R, Entity (T));
6034 Set_Error_Posted (R);
6035 Set_Error_Posted (T);
6037 end Analyze_Subtype_Indication;
6039 --------------------------
6040 -- Analyze_Variant_Part --
6041 --------------------------
6043 procedure Analyze_Variant_Part (N : Node_Id) is
6044 Discr_Name : Node_Id;
6045 Discr_Type : Entity_Id;
6047 procedure Process_Variant (A : Node_Id);
6048 -- Analyze declarations for a single variant
6050 package Analyze_Variant_Choices is
6051 new Generic_Analyze_Choices (Process_Variant);
6052 use Analyze_Variant_Choices;
6054 ---------------------
6055 -- Process_Variant --
6056 ---------------------
6058 procedure Process_Variant (A : Node_Id) is
6059 CL : constant Node_Id := Component_List (A);
6061 if not Null_Present (CL) then
6062 Analyze_Declarations (Component_Items (CL));
6064 if Present (Variant_Part (CL)) then
6065 Analyze (Variant_Part (CL));
6068 end Process_Variant;
6070 -- Start of processing for Analyze_Variant_Part
6073 Discr_Name := Name (N);
6074 Analyze (Discr_Name);
6076 -- If Discr_Name bad, get out (prevent cascaded errors)
6078 if Etype (Discr_Name) = Any_Type then
6082 -- Check invalid discriminant in variant part
6084 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6085 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6088 Discr_Type := Etype (Entity (Discr_Name));
6090 if not Is_Discrete_Type (Discr_Type) then
6092 ("discriminant in a variant part must be of a discrete type",
6097 -- Now analyze the choices, which also analyzes the declarations that
6098 -- are associated with each choice.
6100 Analyze_Choices (Variants (N), Discr_Type);
6102 -- Note: we used to instantiate and call Check_Choices here to check
6103 -- that the choices covered the discriminant, but it's too early to do
6104 -- that because of statically predicated subtypes, whose analysis may
6105 -- be deferred to their freeze point which may be as late as the freeze
6106 -- point of the containing record. So this call is now to be found in
6107 -- Freeze_Record_Declaration.
6109 end Analyze_Variant_Part;
6111 ----------------------------
6112 -- Array_Type_Declaration --
6113 ----------------------------
6115 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6116 Component_Def : constant Node_Id := Component_Definition (Def);
6117 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6118 P : constant Node_Id := Parent (Def);
6119 Element_Type : Entity_Id;
6120 Implicit_Base : Entity_Id;
6124 Related_Id : Entity_Id;
6125 Has_FLB_Index : Boolean := False;
6128 if Nkind (Def) = N_Constrained_Array_Definition then
6129 Index := First (Discrete_Subtype_Definitions (Def));
6131 Index := First (Subtype_Marks (Def));
6134 -- Find proper names for the implicit types which may be public. In case
6135 -- of anonymous arrays we use the name of the first object of that type
6139 Related_Id := Defining_Identifier (P);
6145 while Present (Index) loop
6148 -- Test for odd case of trying to index a type by the type itself
6150 if Is_Entity_Name (Index) and then Entity (Index) = T then
6151 Error_Msg_N ("type& cannot be indexed by itself", Index);
6152 Set_Entity (Index, Standard_Boolean);
6153 Set_Etype (Index, Standard_Boolean);
6156 -- Add a subtype declaration for each index of private array type
6157 -- declaration whose type is also private. For example:
6160 -- type Index is private;
6162 -- type Table is array (Index) of ...
6165 -- This is currently required by the expander for the internally
6166 -- generated equality subprogram of records with variant parts in
6167 -- which the type of some component is such a private type. And it
6168 -- also helps semantic analysis in peculiar cases where the array
6169 -- type is referenced from an instance but not the index directly.
6171 if Is_Package_Or_Generic_Package (Current_Scope)
6172 and then In_Private_Part (Current_Scope)
6173 and then Has_Private_Declaration (Etype (Index))
6174 and then Scope (Etype (Index)) = Current_Scope
6177 Loc : constant Source_Ptr := Sloc (Def);
6182 New_E := Make_Temporary (Loc, 'T');
6183 Set_Is_Internal (New_E);
6186 Make_Subtype_Declaration (Loc,
6187 Defining_Identifier => New_E,
6188 Subtype_Indication =>
6189 New_Occurrence_Of (Etype (Index), Loc));
6191 Insert_Before (Parent (Def), Decl);
6193 Set_Etype (Index, New_E);
6195 -- If the index is a range or a subtype indication it carries
6196 -- no entity. Example:
6199 -- type T is private;
6201 -- type T is new Natural;
6202 -- Table : array (T(1) .. T(10)) of Boolean;
6205 -- Otherwise the type of the reference is its entity.
6207 if Is_Entity_Name (Index) then
6208 Set_Entity (Index, New_E);
6213 Make_Index (Index, P, Related_Id, Nb_Index);
6215 -- In the case where we have an unconstrained array with an index
6216 -- given by a subtype_indication, this is necessarily a "fixed lower
6217 -- bound" index. We change the upper bound of that index to the upper
6218 -- bound of the index's subtype (denoted by the subtype_mark), since
6219 -- that upper bound was originally set by the parser to be the same
6220 -- as the lower bound. In truth, that upper bound corresponds to
6221 -- a box ("<>"), and could be set to Empty, but it's convenient to
6222 -- set it to the upper bound to avoid needing to add special tests
6223 -- in various places for an Empty upper bound, and in any case that
6224 -- accurately characterizes the index's range of values.
6226 if Nkind (Def) = N_Unconstrained_Array_Definition
6227 and then Nkind (Index) = N_Subtype_Indication
6230 Index_Subtype_High_Bound : constant Entity_Id :=
6231 Type_High_Bound (Entity (Subtype_Mark (Index)));
6233 Set_High_Bound (Range_Expression (Constraint (Index)),
6234 Index_Subtype_High_Bound);
6236 -- Record that the array type has one or more indexes with
6237 -- a fixed lower bound.
6239 Has_FLB_Index := True;
6241 -- Mark the index as belonging to an array type with a fixed
6244 Set_Is_Fixed_Lower_Bound_Index_Subtype (Etype (Index));
6248 -- Check error of subtype with predicate for index type
6250 Bad_Predicated_Subtype_Use
6251 ("subtype& has predicate, not allowed as index subtype",
6252 Index, Etype (Index));
6254 -- Move to next index
6257 Nb_Index := Nb_Index + 1;
6260 -- Process subtype indication if one is present
6262 if Present (Component_Typ) then
6263 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6264 Set_Etype (Component_Typ, Element_Type);
6266 -- Ada 2005 (AI-230): Access Definition case
6268 else pragma Assert (Present (Access_Definition (Component_Def)));
6270 -- Indicate that the anonymous access type is created by the
6271 -- array type declaration.
6273 Element_Type := Access_Definition
6275 N => Access_Definition (Component_Def));
6276 Set_Is_Local_Anonymous_Access (Element_Type);
6278 -- Propagate the parent. This field is needed if we have to generate
6279 -- the master_id associated with an anonymous access to task type
6280 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6282 Copy_Parent (To => Element_Type, From => T);
6284 -- Ada 2005 (AI-230): In case of components that are anonymous access
6285 -- types the level of accessibility depends on the enclosing type
6288 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6290 -- Ada 2005 (AI-254)
6293 CD : constant Node_Id :=
6294 Access_To_Subprogram_Definition
6295 (Access_Definition (Component_Def));
6297 if Present (CD) and then Protected_Present (CD) then
6299 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6304 -- Constrained array case
6307 -- We might be creating more than one itype with the same Related_Id,
6308 -- e.g. for an array object definition and its initial value. Give
6309 -- them unique suffixes, because GNATprove require distinct types to
6310 -- have different names.
6312 T := Create_Itype (E_Void, P, Related_Id, 'T', Suffix_Index => -1);
6315 if Nkind (Def) = N_Constrained_Array_Definition then
6317 if Ekind (T) in Incomplete_Or_Private_Kind then
6318 Reinit_Field_To_Zero (T, F_Stored_Constraint);
6320 pragma Assert (Ekind (T) = E_Void);
6323 -- Establish Implicit_Base as unconstrained base type
6325 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6327 Set_Etype (Implicit_Base, Implicit_Base);
6328 Set_Scope (Implicit_Base, Current_Scope);
6329 Set_Has_Delayed_Freeze (Implicit_Base);
6330 Set_Default_SSO (Implicit_Base);
6332 -- The constrained array type is a subtype of the unconstrained one
6334 Mutate_Ekind (T, E_Array_Subtype);
6335 Reinit_Size_Align (T);
6336 Set_Etype (T, Implicit_Base);
6337 Set_Scope (T, Current_Scope);
6338 Set_Is_Constrained (T);
6340 First (Discrete_Subtype_Definitions (Def)));
6341 Set_Has_Delayed_Freeze (T);
6343 -- Complete setup of implicit base type
6345 Set_Component_Size (Implicit_Base, Uint_0);
6346 Set_Component_Type (Implicit_Base, Element_Type);
6347 Set_Finalize_Storage_Only
6349 Finalize_Storage_Only (Element_Type));
6350 Set_First_Index (Implicit_Base, First_Index (T));
6351 Set_Has_Controlled_Component
6353 Has_Controlled_Component (Element_Type)
6354 or else Is_Controlled (Element_Type));
6355 Set_Packed_Array_Impl_Type
6356 (Implicit_Base, Empty);
6358 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6360 -- Unconstrained array case
6362 else pragma Assert (Nkind (Def) = N_Unconstrained_Array_Definition);
6364 if Ekind (T) in Incomplete_Or_Private_Kind then
6365 Reinit_Field_To_Zero (T, F_Stored_Constraint);
6367 pragma Assert (Ekind (T) = E_Void);
6370 Mutate_Ekind (T, E_Array_Type);
6371 Reinit_Size_Align (T);
6373 Set_Scope (T, Current_Scope);
6374 Set_Component_Size (T, Uint_0);
6375 Set_Is_Constrained (T, False);
6376 Set_Is_Fixed_Lower_Bound_Array_Subtype
6378 Set_First_Index (T, First (Subtype_Marks (Def)));
6379 Set_Has_Delayed_Freeze (T, True);
6380 Propagate_Concurrent_Flags (T, Element_Type);
6381 Set_Has_Controlled_Component (T, Has_Controlled_Component
6384 Is_Controlled (Element_Type));
6385 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6387 Set_Default_SSO (T);
6390 -- Common attributes for both cases
6392 Set_Component_Type (Base_Type (T), Element_Type);
6393 Set_Packed_Array_Impl_Type (T, Empty);
6395 if Aliased_Present (Component_Definition (Def)) then
6396 Set_Has_Aliased_Components (Etype (T));
6398 -- AI12-001: All aliased objects are considered to be specified as
6399 -- independently addressable (RM C.6(8.1/4)).
6401 Set_Has_Independent_Components (Etype (T));
6404 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6405 -- array type to ensure that objects of this type are initialized.
6407 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6408 Set_Can_Never_Be_Null (T);
6410 if Null_Exclusion_Present (Component_Definition (Def))
6412 -- No need to check itypes because in their case this check was
6413 -- done at their point of creation
6415 and then not Is_Itype (Element_Type)
6418 ("`NOT NULL` not allowed (null already excluded)",
6419 Subtype_Indication (Component_Definition (Def)));
6423 Priv := Private_Component (Element_Type);
6425 if Present (Priv) then
6427 -- Check for circular definitions
6429 if Priv = Any_Type then
6430 Set_Component_Type (Etype (T), Any_Type);
6432 -- There is a gap in the visibility of operations on the composite
6433 -- type only if the component type is defined in a different scope.
6435 elsif Scope (Priv) = Current_Scope then
6438 elsif Is_Limited_Type (Priv) then
6439 Set_Is_Limited_Composite (Etype (T));
6440 Set_Is_Limited_Composite (T);
6442 Set_Is_Private_Composite (Etype (T));
6443 Set_Is_Private_Composite (T);
6447 -- A syntax error in the declaration itself may lead to an empty index
6448 -- list, in which case do a minimal patch.
6450 if No (First_Index (T)) then
6451 Error_Msg_N ("missing index definition in array type declaration", T);
6454 Indexes : constant List_Id :=
6455 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6457 Set_Discrete_Subtype_Definitions (Def, Indexes);
6458 Set_First_Index (T, First (Indexes));
6463 -- Create a concatenation operator for the new type. Internal array
6464 -- types created for packed entities do not need such, they are
6465 -- compatible with the user-defined type.
6467 if Number_Dimensions (T) = 1
6468 and then not Is_Packed_Array_Impl_Type (T)
6470 New_Concatenation_Op (T);
6473 -- In the case of an unconstrained array the parser has already verified
6474 -- that all the indexes are unconstrained but we still need to make sure
6475 -- that the element type is constrained.
6477 if not Is_Definite_Subtype (Element_Type) then
6479 ("unconstrained element type in array declaration",
6480 Subtype_Indication (Component_Def));
6482 elsif Is_Abstract_Type (Element_Type) then
6484 ("the type of a component cannot be abstract",
6485 Subtype_Indication (Component_Def));
6488 -- There may be an invariant declared for the component type, but
6489 -- the construction of the component invariant checking procedure
6490 -- takes place during expansion.
6491 end Array_Type_Declaration;
6493 ------------------------------------------------------
6494 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6495 ------------------------------------------------------
6497 function Replace_Anonymous_Access_To_Protected_Subprogram
6498 (N : Node_Id) return Entity_Id
6500 Loc : constant Source_Ptr := Sloc (N);
6502 Curr_Scope : constant Scope_Stack_Entry :=
6503 Scope_Stack.Table (Scope_Stack.Last);
6505 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6508 -- Access definition in declaration
6511 -- Object definition or formal definition with an access definition
6514 -- Declaration of anonymous access to subprogram type
6517 -- Original specification in access to subprogram
6522 Set_Is_Internal (Anon);
6525 when N_Constrained_Array_Definition
6526 | N_Component_Declaration
6527 | N_Unconstrained_Array_Definition
6529 Comp := Component_Definition (N);
6530 Acc := Access_Definition (Comp);
6532 when N_Discriminant_Specification =>
6533 Comp := Discriminant_Type (N);
6536 when N_Parameter_Specification =>
6537 Comp := Parameter_Type (N);
6540 when N_Access_Function_Definition =>
6541 Comp := Result_Definition (N);
6544 when N_Object_Declaration =>
6545 Comp := Object_Definition (N);
6548 when N_Function_Specification =>
6549 Comp := Result_Definition (N);
6553 raise Program_Error;
6556 Spec := Access_To_Subprogram_Definition (Acc);
6559 Make_Full_Type_Declaration (Loc,
6560 Defining_Identifier => Anon,
6561 Type_Definition => Copy_Separate_Tree (Spec));
6563 Mark_Rewrite_Insertion (Decl);
6565 -- Insert the new declaration in the nearest enclosing scope. If the
6566 -- parent is a body and N is its return type, the declaration belongs
6567 -- in the enclosing scope. Likewise if N is the type of a parameter.
6571 if Nkind (N) = N_Function_Specification
6572 and then Nkind (P) = N_Subprogram_Body
6575 elsif Nkind (N) = N_Parameter_Specification
6576 and then Nkind (P) in N_Subprogram_Specification
6577 and then Nkind (Parent (P)) = N_Subprogram_Body
6579 P := Parent (Parent (P));
6582 while Present (P) and then not Has_Declarations (P) loop
6586 pragma Assert (Present (P));
6588 if Nkind (P) = N_Package_Specification then
6589 Prepend (Decl, Visible_Declarations (P));
6591 Prepend (Decl, Declarations (P));
6594 -- Replace the anonymous type with an occurrence of the new declaration.
6595 -- In all cases the rewritten node does not have the null-exclusion
6596 -- attribute because (if present) it was already inherited by the
6597 -- anonymous entity (Anon). Thus, in case of components we do not
6598 -- inherit this attribute.
6600 if Nkind (N) = N_Parameter_Specification then
6601 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6602 Set_Etype (Defining_Identifier (N), Anon);
6603 Set_Null_Exclusion_Present (N, False);
6605 elsif Nkind (N) = N_Object_Declaration then
6606 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6607 Set_Etype (Defining_Identifier (N), Anon);
6609 elsif Nkind (N) = N_Access_Function_Definition then
6610 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6612 elsif Nkind (N) = N_Function_Specification then
6613 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6614 Set_Etype (Defining_Unit_Name (N), Anon);
6618 Make_Component_Definition (Loc,
6619 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6622 Mark_Rewrite_Insertion (Comp);
6624 if Nkind (N) in N_Object_Declaration | N_Access_Function_Definition
6625 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6626 and then not Is_Type (Current_Scope))
6629 -- Declaration can be analyzed in the current scope.
6634 -- Temporarily remove the current scope (record or subprogram) from
6635 -- the stack to add the new declarations to the enclosing scope.
6636 -- The anonymous entity is an Itype with the proper attributes.
6638 Scope_Stack.Decrement_Last;
6640 Set_Is_Itype (Anon);
6641 Set_Associated_Node_For_Itype (Anon, N);
6642 Scope_Stack.Append (Curr_Scope);
6645 Mutate_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6646 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6648 end Replace_Anonymous_Access_To_Protected_Subprogram;
6650 -------------------------------------
6651 -- Build_Access_Subprogram_Wrapper --
6652 -------------------------------------
6654 procedure Build_Access_Subprogram_Wrapper (Decl : Node_Id) is
6655 Loc : constant Source_Ptr := Sloc (Decl);
6656 Id : constant Entity_Id := Defining_Identifier (Decl);
6657 Type_Def : constant Node_Id := Type_Definition (Decl);
6658 Specs : constant List_Id :=
6659 Parameter_Specifications (Type_Def);
6660 Profile : constant List_Id := New_List;
6661 Subp : constant Entity_Id := Make_Temporary (Loc, 'A');
6663 Contracts : constant List_Id := New_List;
6669 procedure Replace_Type_Name (Expr : Node_Id);
6670 -- In the expressions for contract aspects, replace occurrences of the
6671 -- access type with the name of the subprogram entity, as needed, e.g.
6672 -- for 'Result. Aspects that are not contracts, e.g. Size or Alignment)
6673 -- remain on the original access type declaration. What about expanded
6674 -- names denoting formals, whose prefix in source is the type name ???
6676 -----------------------
6677 -- Replace_Type_Name --
6678 -----------------------
6680 procedure Replace_Type_Name (Expr : Node_Id) is
6681 function Process (N : Node_Id) return Traverse_Result;
6682 function Process (N : Node_Id) return Traverse_Result is
6684 if Nkind (N) = N_Attribute_Reference
6685 and then Is_Entity_Name (Prefix (N))
6686 and then Chars (Prefix (N)) = Chars (Id)
6688 Set_Prefix (N, Make_Identifier (Sloc (N), Chars (Subp)));
6694 procedure Traverse is new Traverse_Proc (Process);
6697 end Replace_Type_Name;
6700 if Ekind (Id) in E_Access_Subprogram_Type
6701 | E_Access_Protected_Subprogram_Type
6702 | E_Anonymous_Access_Protected_Subprogram_Type
6703 | E_Anonymous_Access_Subprogram_Type
6709 ("illegal pre/postcondition on access type", Decl);
6720 Asp := First (Aspect_Specifications (Decl));
6721 while Present (Asp) loop
6722 A_Id := Get_Aspect_Id (Chars (Identifier (Asp)));
6723 if A_Id = Aspect_Pre or else A_Id = Aspect_Post then
6725 Expr := Expression (Cond);
6726 Replace_Type_Name (Expr);
6730 Append (Cond, Contracts);
6738 -- If there are no contract aspects, no need for a wrapper.
6740 if Is_Empty_List (Contracts) then
6744 Form_P := First (Specs);
6746 while Present (Form_P) loop
6747 New_P := New_Copy_Tree (Form_P);
6748 Set_Defining_Identifier (New_P,
6749 Make_Defining_Identifier
6750 (Loc, Chars (Defining_Identifier (Form_P))));
6751 Append (New_P, Profile);
6755 -- Add to parameter specifications the access parameter that is passed
6756 -- in from an indirect call.
6759 Make_Parameter_Specification (Loc,
6760 Defining_Identifier => Make_Temporary (Loc, 'P'),
6761 Parameter_Type => New_Occurrence_Of (Id, Loc)),
6764 if Nkind (Type_Def) = N_Access_Procedure_Definition then
6766 Make_Procedure_Specification (Loc,
6767 Defining_Unit_Name => Subp,
6768 Parameter_Specifications => Profile);
6771 Make_Function_Specification (Loc,
6772 Defining_Unit_Name => Subp,
6773 Parameter_Specifications => Profile,
6774 Result_Definition =>
6776 (Result_Definition (Type_Definition (Decl))));
6780 Make_Subprogram_Declaration (Loc, Specification => Spec);
6781 Set_Aspect_Specifications (New_Decl, Contracts);
6783 Insert_After (Decl, New_Decl);
6784 Set_Access_Subprogram_Wrapper (Designated_Type (Id), Subp);
6785 Build_Access_Subprogram_Wrapper_Body (Decl, New_Decl);
6786 end Build_Access_Subprogram_Wrapper;
6788 -------------------------------
6789 -- Build_Derived_Access_Type --
6790 -------------------------------
6792 procedure Build_Derived_Access_Type
6794 Parent_Type : Entity_Id;
6795 Derived_Type : Entity_Id)
6797 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6799 Desig_Type : Entity_Id;
6801 Discr_Con_Elist : Elist_Id;
6802 Discr_Con_El : Elmt_Id;
6806 -- Set the designated type so it is available in case this is an access
6807 -- to a self-referential type, e.g. a standard list type with a next
6808 -- pointer. Will be reset after subtype is built.
6810 Set_Directly_Designated_Type
6811 (Derived_Type, Designated_Type (Parent_Type));
6813 Subt := Process_Subtype (S, N);
6815 if Nkind (S) /= N_Subtype_Indication
6816 and then Subt /= Base_Type (Subt)
6818 Mutate_Ekind (Derived_Type, E_Access_Subtype);
6821 if Ekind (Derived_Type) = E_Access_Subtype then
6823 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6824 Ibase : constant Entity_Id :=
6825 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6826 Svg_Chars : constant Name_Id := Chars (Ibase);
6827 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6828 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
6831 Copy_Node (Pbase, Ibase);
6833 -- Restore Itype status after Copy_Node
6835 Set_Is_Itype (Ibase);
6836 Set_Associated_Node_For_Itype (Ibase, N);
6838 Set_Chars (Ibase, Svg_Chars);
6839 Set_Prev_Entity (Ibase, Svg_Prev_E);
6840 Set_Next_Entity (Ibase, Svg_Next_E);
6841 Set_Sloc (Ibase, Sloc (Derived_Type));
6842 Set_Scope (Ibase, Scope (Derived_Type));
6843 Set_Freeze_Node (Ibase, Empty);
6844 Set_Is_Frozen (Ibase, False);
6845 Set_Comes_From_Source (Ibase, False);
6846 Set_Is_First_Subtype (Ibase, False);
6848 Set_Etype (Ibase, Pbase);
6849 Set_Etype (Derived_Type, Ibase);
6853 Set_Directly_Designated_Type
6854 (Derived_Type, Designated_Type (Subt));
6856 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6857 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6858 Set_Size_Info (Derived_Type, Parent_Type);
6859 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
6860 Set_Depends_On_Private (Derived_Type,
6861 Has_Private_Component (Derived_Type));
6862 Conditional_Delay (Derived_Type, Subt);
6864 if Is_Access_Subprogram_Type (Derived_Type)
6865 and then Is_Base_Type (Derived_Type)
6867 Set_Can_Use_Internal_Rep
6868 (Derived_Type, Can_Use_Internal_Rep (Parent_Type));
6871 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6872 -- that it is not redundant.
6874 if Null_Exclusion_Present (Type_Definition (N)) then
6875 Set_Can_Never_Be_Null (Derived_Type);
6877 elsif Can_Never_Be_Null (Parent_Type) then
6878 Set_Can_Never_Be_Null (Derived_Type);
6881 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6882 -- the root type for this information.
6884 -- Apply range checks to discriminants for derived record case
6885 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6887 Desig_Type := Designated_Type (Derived_Type);
6889 if Is_Composite_Type (Desig_Type)
6890 and then (not Is_Array_Type (Desig_Type))
6891 and then Has_Discriminants (Desig_Type)
6892 and then Base_Type (Desig_Type) /= Desig_Type
6894 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6895 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6897 Discr := First_Discriminant (Base_Type (Desig_Type));
6898 while Present (Discr_Con_El) loop
6899 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6900 Next_Elmt (Discr_Con_El);
6901 Next_Discriminant (Discr);
6904 end Build_Derived_Access_Type;
6906 ------------------------------
6907 -- Build_Derived_Array_Type --
6908 ------------------------------
6910 procedure Build_Derived_Array_Type
6912 Parent_Type : Entity_Id;
6913 Derived_Type : Entity_Id)
6915 Loc : constant Source_Ptr := Sloc (N);
6916 Tdef : constant Node_Id := Type_Definition (N);
6917 Indic : constant Node_Id := Subtype_Indication (Tdef);
6918 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6919 Implicit_Base : Entity_Id := Empty;
6920 New_Indic : Node_Id;
6922 procedure Make_Implicit_Base;
6923 -- If the parent subtype is constrained, the derived type is a subtype
6924 -- of an implicit base type derived from the parent base.
6926 ------------------------
6927 -- Make_Implicit_Base --
6928 ------------------------
6930 procedure Make_Implicit_Base is
6933 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6935 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
6936 Set_Etype (Implicit_Base, Parent_Base);
6938 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6939 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6941 Set_Has_Delayed_Freeze (Implicit_Base, True);
6942 end Make_Implicit_Base;
6944 -- Start of processing for Build_Derived_Array_Type
6947 if not Is_Constrained (Parent_Type) then
6948 if Nkind (Indic) /= N_Subtype_Indication then
6949 Mutate_Ekind (Derived_Type, E_Array_Type);
6951 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6952 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6954 Set_Has_Delayed_Freeze (Derived_Type, True);
6958 Set_Etype (Derived_Type, Implicit_Base);
6961 Make_Subtype_Declaration (Loc,
6962 Defining_Identifier => Derived_Type,
6963 Subtype_Indication =>
6964 Make_Subtype_Indication (Loc,
6965 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6966 Constraint => Constraint (Indic)));
6968 Rewrite (N, New_Indic);
6973 if Nkind (Indic) /= N_Subtype_Indication then
6976 Mutate_Ekind (Derived_Type, Ekind (Parent_Type));
6977 Set_Etype (Derived_Type, Implicit_Base);
6978 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6981 Error_Msg_N ("illegal constraint on constrained type", Indic);
6985 -- If parent type is not a derived type itself, and is declared in
6986 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6987 -- the new type's concatenation operator since Derive_Subprograms
6988 -- will not inherit the parent's operator. If the parent type is
6989 -- unconstrained, the operator is of the unconstrained base type.
6991 if Number_Dimensions (Parent_Type) = 1
6992 and then not Is_Limited_Type (Parent_Type)
6993 and then not Is_Derived_Type (Parent_Type)
6994 and then not Is_Package_Or_Generic_Package
6995 (Scope (Base_Type (Parent_Type)))
6997 if not Is_Constrained (Parent_Type)
6998 and then Is_Constrained (Derived_Type)
7000 New_Concatenation_Op (Implicit_Base);
7002 New_Concatenation_Op (Derived_Type);
7005 end Build_Derived_Array_Type;
7007 -----------------------------------
7008 -- Build_Derived_Concurrent_Type --
7009 -----------------------------------
7011 procedure Build_Derived_Concurrent_Type
7013 Parent_Type : Entity_Id;
7014 Derived_Type : Entity_Id)
7016 Loc : constant Source_Ptr := Sloc (N);
7017 Def : constant Node_Id := Type_Definition (N);
7018 Indic : constant Node_Id := Subtype_Indication (Def);
7020 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
7021 Corr_Decl : Node_Id;
7022 Corr_Decl_Needed : Boolean;
7023 -- If the derived type has fewer discriminants than its parent, the
7024 -- corresponding record is also a derived type, in order to account for
7025 -- the bound discriminants. We create a full type declaration for it in
7028 Constraint_Present : constant Boolean :=
7029 Nkind (Indic) = N_Subtype_Indication;
7031 D_Constraint : Node_Id;
7032 New_Constraint : Elist_Id := No_Elist;
7033 Old_Disc : Entity_Id;
7034 New_Disc : Entity_Id;
7038 Set_Stored_Constraint (Derived_Type, No_Elist);
7039 Corr_Decl_Needed := False;
7042 if Present (Discriminant_Specifications (N))
7043 and then Constraint_Present
7045 Old_Disc := First_Discriminant (Parent_Type);
7046 New_Disc := First (Discriminant_Specifications (N));
7047 while Present (New_Disc) and then Present (Old_Disc) loop
7048 Next_Discriminant (Old_Disc);
7053 if Present (Old_Disc) and then Expander_Active then
7055 -- The new type has fewer discriminants, so we need to create a new
7056 -- corresponding record, which is derived from the corresponding
7057 -- record of the parent, and has a stored constraint that captures
7058 -- the values of the discriminant constraints. The corresponding
7059 -- record is needed only if expander is active and code generation is
7062 -- The type declaration for the derived corresponding record has the
7063 -- same discriminant part and constraints as the current declaration.
7064 -- Copy the unanalyzed tree to build declaration.
7066 Corr_Decl_Needed := True;
7067 New_N := Copy_Separate_Tree (N);
7070 Make_Full_Type_Declaration (Loc,
7071 Defining_Identifier => Corr_Record,
7072 Discriminant_Specifications =>
7073 Discriminant_Specifications (New_N),
7075 Make_Derived_Type_Definition (Loc,
7076 Subtype_Indication =>
7077 Make_Subtype_Indication (Loc,
7080 (Corresponding_Record_Type (Parent_Type), Loc),
7083 (Subtype_Indication (Type_Definition (New_N))))));
7086 -- Copy Storage_Size and Relative_Deadline variables if task case
7088 if Is_Task_Type (Parent_Type) then
7089 Set_Storage_Size_Variable (Derived_Type,
7090 Storage_Size_Variable (Parent_Type));
7091 Set_Relative_Deadline_Variable (Derived_Type,
7092 Relative_Deadline_Variable (Parent_Type));
7095 if Present (Discriminant_Specifications (N)) then
7096 Push_Scope (Derived_Type);
7097 Check_Or_Process_Discriminants (N, Derived_Type);
7099 if Constraint_Present then
7101 Expand_To_Stored_Constraint
7103 Build_Discriminant_Constraints
7104 (Parent_Type, Indic, True));
7109 elsif Constraint_Present then
7111 -- Build an unconstrained derived type and rewrite the derived type
7112 -- as a subtype of this new base type.
7115 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7116 New_Base : Entity_Id;
7118 New_Indic : Node_Id;
7122 Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
7125 Make_Full_Type_Declaration (Loc,
7126 Defining_Identifier => New_Base,
7128 Make_Derived_Type_Definition (Loc,
7129 Abstract_Present => Abstract_Present (Def),
7130 Limited_Present => Limited_Present (Def),
7131 Subtype_Indication =>
7132 New_Occurrence_Of (Parent_Base, Loc)));
7134 Mark_Rewrite_Insertion (New_Decl);
7135 Insert_Before (N, New_Decl);
7139 Make_Subtype_Indication (Loc,
7140 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
7141 Constraint => Relocate_Node (Constraint (Indic)));
7144 Make_Subtype_Declaration (Loc,
7145 Defining_Identifier => Derived_Type,
7146 Subtype_Indication => New_Indic));
7153 -- By default, operations and private data are inherited from parent.
7154 -- However, in the presence of bound discriminants, a new corresponding
7155 -- record will be created, see below.
7157 Set_Has_Discriminants
7158 (Derived_Type, Has_Discriminants (Parent_Type));
7159 Set_Corresponding_Record_Type
7160 (Derived_Type, Corresponding_Record_Type (Parent_Type));
7162 -- Is_Constrained is set according the parent subtype, but is set to
7163 -- False if the derived type is declared with new discriminants.
7167 (Is_Constrained (Parent_Type) or else Constraint_Present)
7168 and then not Present (Discriminant_Specifications (N)));
7170 if Constraint_Present then
7171 if not Has_Discriminants (Parent_Type) then
7172 Error_Msg_N ("untagged parent must have discriminants", N);
7174 elsif Present (Discriminant_Specifications (N)) then
7176 -- Verify that new discriminants are used to constrain old ones
7178 D_Constraint := First (Constraints (Constraint (Indic)));
7180 Old_Disc := First_Discriminant (Parent_Type);
7182 while Present (D_Constraint) loop
7183 if Nkind (D_Constraint) /= N_Discriminant_Association then
7185 -- Positional constraint. If it is a reference to a new
7186 -- discriminant, it constrains the corresponding old one.
7188 if Nkind (D_Constraint) = N_Identifier then
7189 New_Disc := First_Discriminant (Derived_Type);
7190 while Present (New_Disc) loop
7191 exit when Chars (New_Disc) = Chars (D_Constraint);
7192 Next_Discriminant (New_Disc);
7195 if Present (New_Disc) then
7196 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
7200 Next_Discriminant (Old_Disc);
7202 -- if this is a named constraint, search by name for the old
7203 -- discriminants constrained by the new one.
7205 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7207 -- Find new discriminant with that name
7209 New_Disc := First_Discriminant (Derived_Type);
7210 while Present (New_Disc) loop
7212 Chars (New_Disc) = Chars (Expression (D_Constraint));
7213 Next_Discriminant (New_Disc);
7216 if Present (New_Disc) then
7218 -- Verify that new discriminant renames some discriminant
7219 -- of the parent type, and associate the new discriminant
7220 -- with one or more old ones that it renames.
7226 Selector := First (Selector_Names (D_Constraint));
7227 while Present (Selector) loop
7228 Old_Disc := First_Discriminant (Parent_Type);
7229 while Present (Old_Disc) loop
7230 exit when Chars (Old_Disc) = Chars (Selector);
7231 Next_Discriminant (Old_Disc);
7234 if Present (Old_Disc) then
7235 Set_Corresponding_Discriminant
7236 (New_Disc, Old_Disc);
7245 Next (D_Constraint);
7248 New_Disc := First_Discriminant (Derived_Type);
7249 while Present (New_Disc) loop
7250 if No (Corresponding_Discriminant (New_Disc)) then
7252 ("new discriminant& must constrain old one", N, New_Disc);
7254 -- If a new discriminant is used in the constraint, then its
7255 -- subtype must be statically compatible with the subtype of
7256 -- the parent discriminant (RM 3.7(15)).
7259 Check_Constraining_Discriminant
7260 (New_Disc, Corresponding_Discriminant (New_Disc));
7263 Next_Discriminant (New_Disc);
7267 elsif Present (Discriminant_Specifications (N)) then
7269 ("missing discriminant constraint in untagged derivation", N);
7272 -- The entity chain of the derived type includes the new discriminants
7273 -- but shares operations with the parent.
7275 if Present (Discriminant_Specifications (N)) then
7276 Old_Disc := First_Discriminant (Parent_Type);
7277 while Present (Old_Disc) loop
7278 if No (Next_Entity (Old_Disc))
7279 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7282 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7286 Next_Discriminant (Old_Disc);
7290 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7291 if Has_Discriminants (Parent_Type) then
7292 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7293 Set_Discriminant_Constraint (
7294 Derived_Type, Discriminant_Constraint (Parent_Type));
7298 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7300 Set_Has_Completion (Derived_Type);
7302 if Corr_Decl_Needed then
7303 Set_Stored_Constraint (Derived_Type, New_Constraint);
7304 Insert_After (N, Corr_Decl);
7305 Analyze (Corr_Decl);
7306 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7308 end Build_Derived_Concurrent_Type;
7310 ------------------------------------
7311 -- Build_Derived_Enumeration_Type --
7312 ------------------------------------
7314 procedure Build_Derived_Enumeration_Type
7316 Parent_Type : Entity_Id;
7317 Derived_Type : Entity_Id)
7319 function Bound_Belongs_To_Type (B : Node_Id) return Boolean;
7320 -- When the type declaration includes a constraint, we generate
7321 -- a subtype declaration of an anonymous base type, with the constraint
7322 -- given in the original type declaration. Conceptually, the bounds
7323 -- are converted to the new base type, and this conversion freezes
7324 -- (prematurely) that base type, when the bounds are simply literals.
7325 -- As a result, a representation clause for the derived type is then
7326 -- rejected or ignored. This procedure recognizes the simple case of
7327 -- literal bounds, which allows us to indicate that the conversions
7328 -- are not freeze points, and the subsequent representation clause
7330 -- A similar approach might be used to resolve the long-standing
7331 -- problem of premature freezing of derived numeric types ???
7333 function Bound_Belongs_To_Type (B : Node_Id) return Boolean is
7335 return Nkind (B) = N_Type_Conversion
7336 and then Is_Entity_Name (Expression (B))
7337 and then Ekind (Entity (Expression (B))) = E_Enumeration_Literal;
7338 end Bound_Belongs_To_Type;
7340 Loc : constant Source_Ptr := Sloc (N);
7341 Def : constant Node_Id := Type_Definition (N);
7342 Indic : constant Node_Id := Subtype_Indication (Def);
7343 Implicit_Base : Entity_Id;
7344 Literal : Entity_Id;
7345 New_Lit : Entity_Id;
7346 Literals_List : List_Id;
7347 Type_Decl : Node_Id;
7349 Rang_Expr : Node_Id;
7352 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7353 -- not have explicit literals lists we need to process types derived
7354 -- from them specially. This is handled by Derived_Standard_Character.
7355 -- If the parent type is a generic type, there are no literals either,
7356 -- and we construct the same skeletal representation as for the generic
7359 if Is_Standard_Character_Type (Parent_Type) then
7360 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7362 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7368 if Nkind (Indic) /= N_Subtype_Indication then
7370 Make_Attribute_Reference (Loc,
7371 Attribute_Name => Name_First,
7372 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7373 Set_Etype (Lo, Derived_Type);
7376 Make_Attribute_Reference (Loc,
7377 Attribute_Name => Name_Last,
7378 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7379 Set_Etype (Hi, Derived_Type);
7381 Set_Scalar_Range (Derived_Type,
7387 -- Analyze subtype indication and verify compatibility
7388 -- with parent type.
7390 if Base_Type (Process_Subtype (Indic, N)) /=
7391 Base_Type (Parent_Type)
7394 ("illegal constraint for formal discrete type", N);
7400 -- If a constraint is present, analyze the bounds to catch
7401 -- premature usage of the derived literals.
7403 if Nkind (Indic) = N_Subtype_Indication
7404 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7406 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7407 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7410 -- Introduce an implicit base type for the derived type even if there
7411 -- is no constraint attached to it, since this seems closer to the
7412 -- Ada semantics. Build a full type declaration tree for the derived
7413 -- type using the implicit base type as the defining identifier. Then
7414 -- build a subtype declaration tree which applies the constraint (if
7415 -- any) have it replace the derived type declaration.
7417 Literal := First_Literal (Parent_Type);
7418 Literals_List := New_List;
7419 while Present (Literal)
7420 and then Ekind (Literal) = E_Enumeration_Literal
7422 -- Literals of the derived type have the same representation as
7423 -- those of the parent type, but this representation can be
7424 -- overridden by an explicit representation clause. Indicate
7425 -- that there is no explicit representation given yet. These
7426 -- derived literals are implicit operations of the new type,
7427 -- and can be overridden by explicit ones.
7429 if Nkind (Literal) = N_Defining_Character_Literal then
7431 Make_Defining_Character_Literal (Loc, Chars (Literal));
7433 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7436 Mutate_Ekind (New_Lit, E_Enumeration_Literal);
7437 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7438 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7439 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7440 Set_Alias (New_Lit, Literal);
7441 Set_Is_Known_Valid (New_Lit, True);
7443 Append (New_Lit, Literals_List);
7444 Next_Literal (Literal);
7448 Make_Defining_Identifier (Sloc (Derived_Type),
7449 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7451 -- Indicate the proper nature of the derived type. This must be done
7452 -- before analysis of the literals, to recognize cases when a literal
7453 -- may be hidden by a previous explicit function definition (cf.
7456 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
7457 Set_Etype (Derived_Type, Implicit_Base);
7460 Make_Full_Type_Declaration (Loc,
7461 Defining_Identifier => Implicit_Base,
7462 Discriminant_Specifications => No_List,
7464 Make_Enumeration_Type_Definition (Loc, Literals_List));
7466 Mark_Rewrite_Insertion (Type_Decl);
7467 Insert_Before (N, Type_Decl);
7468 Analyze (Type_Decl);
7470 -- The anonymous base now has a full declaration, but this base
7471 -- is not a first subtype.
7473 Set_Is_First_Subtype (Implicit_Base, False);
7475 -- After the implicit base is analyzed its Etype needs to be changed
7476 -- to reflect the fact that it is derived from the parent type which
7477 -- was ignored during analysis. We also set the size at this point.
7479 Set_Etype (Implicit_Base, Parent_Type);
7481 Set_Size_Info (Implicit_Base, Parent_Type);
7482 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7483 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7485 -- Copy other flags from parent type
7487 Set_Has_Non_Standard_Rep
7488 (Implicit_Base, Has_Non_Standard_Rep
7490 Set_Has_Pragma_Ordered
7491 (Implicit_Base, Has_Pragma_Ordered
7493 Set_Has_Delayed_Freeze (Implicit_Base);
7495 -- Process the subtype indication including a validation check on the
7496 -- constraint, if any. If a constraint is given, its bounds must be
7497 -- implicitly converted to the new type.
7499 if Nkind (Indic) = N_Subtype_Indication then
7501 R : constant Node_Id :=
7502 Range_Expression (Constraint (Indic));
7505 if Nkind (R) = N_Range then
7506 Hi := Build_Scalar_Bound
7507 (High_Bound (R), Parent_Type, Implicit_Base);
7508 Lo := Build_Scalar_Bound
7509 (Low_Bound (R), Parent_Type, Implicit_Base);
7512 -- Constraint is a Range attribute. Replace with explicit
7513 -- mention of the bounds of the prefix, which must be a
7516 Analyze (Prefix (R));
7518 Convert_To (Implicit_Base,
7519 Make_Attribute_Reference (Loc,
7520 Attribute_Name => Name_Last,
7522 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7525 Convert_To (Implicit_Base,
7526 Make_Attribute_Reference (Loc,
7527 Attribute_Name => Name_First,
7529 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7536 (Type_High_Bound (Parent_Type),
7537 Parent_Type, Implicit_Base);
7540 (Type_Low_Bound (Parent_Type),
7541 Parent_Type, Implicit_Base);
7549 -- If we constructed a default range for the case where no range
7550 -- was given, then the expressions in the range must not freeze
7551 -- since they do not correspond to expressions in the source.
7552 -- However, if the type inherits predicates the expressions will
7553 -- be elaborated earlier and must freeze.
7555 if (Nkind (Indic) /= N_Subtype_Indication
7557 (Bound_Belongs_To_Type (Lo) and then Bound_Belongs_To_Type (Hi)))
7558 and then not Has_Predicates (Derived_Type)
7560 Set_Must_Not_Freeze (Lo);
7561 Set_Must_Not_Freeze (Hi);
7562 Set_Must_Not_Freeze (Rang_Expr);
7566 Make_Subtype_Declaration (Loc,
7567 Defining_Identifier => Derived_Type,
7568 Subtype_Indication =>
7569 Make_Subtype_Indication (Loc,
7570 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7572 Make_Range_Constraint (Loc,
7573 Range_Expression => Rang_Expr))));
7577 -- Propagate the aspects from the original type declaration to the
7578 -- declaration of the implicit base.
7580 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7582 -- Apply a range check. Since this range expression doesn't have an
7583 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7586 if Nkind (Indic) = N_Subtype_Indication then
7588 (Range_Expression (Constraint (Indic)), Parent_Type,
7589 Source_Typ => Entity (Subtype_Mark (Indic)));
7592 end Build_Derived_Enumeration_Type;
7594 --------------------------------
7595 -- Build_Derived_Numeric_Type --
7596 --------------------------------
7598 procedure Build_Derived_Numeric_Type
7600 Parent_Type : Entity_Id;
7601 Derived_Type : Entity_Id)
7603 Loc : constant Source_Ptr := Sloc (N);
7604 Tdef : constant Node_Id := Type_Definition (N);
7605 Indic : constant Node_Id := Subtype_Indication (Tdef);
7606 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7607 No_Constraint : constant Boolean := Nkind (Indic) /=
7608 N_Subtype_Indication;
7609 Implicit_Base : Entity_Id;
7615 -- Process the subtype indication including a validation check on
7616 -- the constraint if any.
7618 Discard_Node (Process_Subtype (Indic, N));
7620 -- Introduce an implicit base type for the derived type even if there
7621 -- is no constraint attached to it, since this seems closer to the Ada
7625 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7627 Set_Etype (Implicit_Base, Parent_Base);
7628 Mutate_Ekind (Implicit_Base, Ekind (Parent_Base));
7629 Set_Size_Info (Implicit_Base, Parent_Base);
7630 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7631 Set_Parent (Implicit_Base, Parent (Derived_Type));
7632 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7633 Set_Is_Volatile (Implicit_Base, Is_Volatile (Parent_Base));
7635 -- Set RM Size for discrete type or decimal fixed-point type
7636 -- Ordinary fixed-point is excluded, why???
7638 if Is_Discrete_Type (Parent_Base)
7639 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7641 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7644 Set_Has_Delayed_Freeze (Implicit_Base);
7646 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7647 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7649 Set_Scalar_Range (Implicit_Base,
7654 if Has_Infinities (Parent_Base) then
7655 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7658 -- The Derived_Type, which is the entity of the declaration, is a
7659 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7660 -- absence of an explicit constraint.
7662 Set_Etype (Derived_Type, Implicit_Base);
7664 -- If we did not have a constraint, then the Ekind is set from the
7665 -- parent type (otherwise Process_Subtype has set the bounds)
7667 if No_Constraint then
7668 Mutate_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7671 -- If we did not have a range constraint, then set the range from the
7672 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7674 if No_Constraint or else not Has_Range_Constraint (Indic) then
7675 Set_Scalar_Range (Derived_Type,
7677 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7678 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7679 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7681 if Has_Infinities (Parent_Type) then
7682 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7685 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7688 Set_Is_Descendant_Of_Address (Derived_Type,
7689 Is_Descendant_Of_Address (Parent_Type));
7690 Set_Is_Descendant_Of_Address (Implicit_Base,
7691 Is_Descendant_Of_Address (Parent_Type));
7693 -- Set remaining type-specific fields, depending on numeric type
7695 if Is_Modular_Integer_Type (Parent_Type) then
7696 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7698 Set_Non_Binary_Modulus
7699 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7702 (Implicit_Base, Is_Known_Valid (Parent_Base));
7704 elsif Is_Floating_Point_Type (Parent_Type) then
7706 -- Digits of base type is always copied from the digits value of
7707 -- the parent base type, but the digits of the derived type will
7708 -- already have been set if there was a constraint present.
7710 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7711 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7713 if No_Constraint then
7714 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7717 elsif Is_Fixed_Point_Type (Parent_Type) then
7719 -- Small of base type and derived type are always copied from the
7720 -- parent base type, since smalls never change. The delta of the
7721 -- base type is also copied from the parent base type. However the
7722 -- delta of the derived type will have been set already if a
7723 -- constraint was present.
7725 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7726 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7727 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7729 if No_Constraint then
7730 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7733 -- The scale and machine radix in the decimal case are always
7734 -- copied from the parent base type.
7736 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7737 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7738 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7740 Set_Machine_Radix_10
7741 (Derived_Type, Machine_Radix_10 (Parent_Base));
7742 Set_Machine_Radix_10
7743 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7745 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7747 if No_Constraint then
7748 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7751 -- the analysis of the subtype_indication sets the
7752 -- digits value of the derived type.
7759 if Is_Integer_Type (Parent_Type) then
7760 Set_Has_Shift_Operator
7761 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7764 -- The type of the bounds is that of the parent type, and they
7765 -- must be converted to the derived type.
7767 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7769 -- The implicit_base should be frozen when the derived type is frozen,
7770 -- but note that it is used in the conversions of the bounds. For fixed
7771 -- types we delay the determination of the bounds until the proper
7772 -- freezing point. For other numeric types this is rejected by GCC, for
7773 -- reasons that are currently unclear (???), so we choose to freeze the
7774 -- implicit base now. In the case of integers and floating point types
7775 -- this is harmless because subsequent representation clauses cannot
7776 -- affect anything, but it is still baffling that we cannot use the
7777 -- same mechanism for all derived numeric types.
7779 -- There is a further complication: actually some representation
7780 -- clauses can affect the implicit base type. For example, attribute
7781 -- definition clauses for stream-oriented attributes need to set the
7782 -- corresponding TSS entries on the base type, and this normally
7783 -- cannot be done after the base type is frozen, so the circuitry in
7784 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7785 -- and not use Set_TSS in this case.
7787 -- There are also consequences for the case of delayed representation
7788 -- aspects for some cases. For example, a Size aspect is delayed and
7789 -- should not be evaluated to the freeze point. This early freezing
7790 -- means that the size attribute evaluation happens too early???
7792 if Is_Fixed_Point_Type (Parent_Type) then
7793 Conditional_Delay (Implicit_Base, Parent_Type);
7795 Freeze_Before (N, Implicit_Base);
7797 end Build_Derived_Numeric_Type;
7799 --------------------------------
7800 -- Build_Derived_Private_Type --
7801 --------------------------------
7803 procedure Build_Derived_Private_Type
7805 Parent_Type : Entity_Id;
7806 Derived_Type : Entity_Id;
7807 Is_Completion : Boolean;
7808 Derive_Subps : Boolean := True)
7810 Loc : constant Source_Ptr := Sloc (N);
7811 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7812 Par_Scope : constant Entity_Id := Scope (Par_Base);
7813 Full_N : constant Node_Id := New_Copy_Tree (N);
7814 Full_Der : Entity_Id := New_Copy (Derived_Type);
7817 function Available_Full_View (Typ : Entity_Id) return Entity_Id;
7818 -- Return the Full_View or Underlying_Full_View of Typ, whichever is
7819 -- present (they cannot be both present for the same type), or Empty.
7821 procedure Build_Full_Derivation;
7822 -- Build full derivation, i.e. derive from the full view
7824 procedure Copy_And_Build;
7825 -- Copy derived type declaration, replace parent with its full view,
7826 -- and build derivation
7828 -------------------------
7829 -- Available_Full_View --
7830 -------------------------
7832 function Available_Full_View (Typ : Entity_Id) return Entity_Id is
7834 if Present (Full_View (Typ)) then
7835 return Full_View (Typ);
7837 elsif Present (Underlying_Full_View (Typ)) then
7839 -- We should be called on a type with an underlying full view
7840 -- only by means of the recursive call made in Copy_And_Build
7841 -- through the first call to Build_Derived_Type, or else if
7842 -- the parent scope is being analyzed because we are deriving
7845 pragma Assert (Is_Completion or else In_Private_Part (Par_Scope));
7847 return Underlying_Full_View (Typ);
7852 end Available_Full_View;
7854 ---------------------------
7855 -- Build_Full_Derivation --
7856 ---------------------------
7858 procedure Build_Full_Derivation is
7860 -- If parent scope is not open, install the declarations
7862 if not In_Open_Scopes (Par_Scope) then
7863 Install_Private_Declarations (Par_Scope);
7864 Install_Visible_Declarations (Par_Scope);
7866 Uninstall_Declarations (Par_Scope);
7868 -- If parent scope is open and in another unit, and parent has a
7869 -- completion, then the derivation is taking place in the visible
7870 -- part of a child unit. In that case retrieve the full view of
7871 -- the parent momentarily.
7873 elsif not In_Same_Source_Unit (N, Parent_Type)
7874 and then Present (Full_View (Parent_Type))
7876 Full_P := Full_View (Parent_Type);
7877 Exchange_Declarations (Parent_Type);
7879 Exchange_Declarations (Full_P);
7881 -- Otherwise it is a local derivation
7886 end Build_Full_Derivation;
7888 --------------------
7889 -- Copy_And_Build --
7890 --------------------
7892 procedure Copy_And_Build is
7893 Full_Parent : Entity_Id := Parent_Type;
7896 -- If the parent is itself derived from another private type,
7897 -- installing the private declarations has not affected its
7898 -- privacy status, so use its own full view explicitly.
7900 if Is_Private_Type (Full_Parent)
7901 and then Present (Full_View (Full_Parent))
7903 Full_Parent := Full_View (Full_Parent);
7906 -- If the full view is itself derived from another private type
7907 -- and has got an underlying full view, and this is done for a
7908 -- completion, i.e. to build the underlying full view of the type,
7909 -- then use this underlying full view. We cannot do that if this
7910 -- is not a completion, i.e. to build the full view of the type,
7911 -- because this would break the privacy of the parent type, except
7912 -- if the parent scope is being analyzed because we are deriving a
7915 if Is_Private_Type (Full_Parent)
7916 and then Present (Underlying_Full_View (Full_Parent))
7917 and then (Is_Completion or else In_Private_Part (Par_Scope))
7919 Full_Parent := Underlying_Full_View (Full_Parent);
7922 -- For private, record, concurrent, access and almost all enumeration
7923 -- types, the derivation from the full view requires a fully-fledged
7924 -- declaration. In the other cases, just use an itype.
7926 if Is_Private_Type (Full_Parent)
7927 or else Is_Record_Type (Full_Parent)
7928 or else Is_Concurrent_Type (Full_Parent)
7929 or else Is_Access_Type (Full_Parent)
7931 (Is_Enumeration_Type (Full_Parent)
7932 and then not Is_Standard_Character_Type (Full_Parent)
7933 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7935 -- Copy and adjust declaration to provide a completion for what
7936 -- is originally a private declaration. Indicate that full view
7937 -- is internally generated.
7939 Set_Comes_From_Source (Full_N, False);
7940 Set_Comes_From_Source (Full_Der, False);
7941 Set_Parent (Full_Der, Full_N);
7942 Set_Defining_Identifier (Full_N, Full_Der);
7944 -- If there are no constraints, adjust the subtype mark
7946 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7947 N_Subtype_Indication
7949 Set_Subtype_Indication
7950 (Type_Definition (Full_N),
7951 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7954 Insert_After (N, Full_N);
7956 -- Build full view of derived type from full view of parent which
7957 -- is now installed. Subprograms have been derived on the partial
7958 -- view, the completion does not derive them anew.
7960 if Is_Record_Type (Full_Parent) then
7962 -- If parent type is tagged, the completion inherits the proper
7963 -- primitive operations.
7965 if Is_Tagged_Type (Parent_Type) then
7966 Build_Derived_Record_Type
7967 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7969 Build_Derived_Record_Type
7970 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7974 -- If the parent type is private, this is not a completion and
7975 -- we build the full derivation recursively as a completion.
7978 (Full_N, Full_Parent, Full_Der,
7979 Is_Completion => Is_Private_Type (Full_Parent),
7980 Derive_Subps => False);
7983 -- The full declaration has been introduced into the tree and
7984 -- processed in the step above. It should not be analyzed again
7985 -- (when encountered later in the current list of declarations)
7986 -- to prevent spurious name conflicts. The full entity remains
7989 Set_Analyzed (Full_N);
7993 Make_Defining_Identifier (Sloc (Derived_Type),
7994 Chars => Chars (Derived_Type));
7995 Set_Is_Itype (Full_Der);
7996 Set_Associated_Node_For_Itype (Full_Der, N);
7997 Set_Parent (Full_Der, N);
7999 (N, Full_Parent, Full_Der,
8000 Is_Completion => False, Derive_Subps => False);
8003 Set_Has_Private_Declaration (Full_Der);
8004 Set_Has_Private_Declaration (Derived_Type);
8006 Set_Scope (Full_Der, Scope (Derived_Type));
8007 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
8008 Set_Has_Size_Clause (Full_Der, False);
8009 Set_Has_Alignment_Clause (Full_Der, False);
8010 Set_Has_Delayed_Freeze (Full_Der);
8011 Set_Is_Frozen (Full_Der, False);
8012 Set_Freeze_Node (Full_Der, Empty);
8013 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
8014 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
8016 -- The convention on the base type may be set in the private part
8017 -- and not propagated to the subtype until later, so we obtain the
8018 -- convention from the base type of the parent.
8020 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
8023 -- Start of processing for Build_Derived_Private_Type
8026 if Is_Tagged_Type (Parent_Type) then
8027 Full_P := Full_View (Parent_Type);
8029 -- A type extension of a type with unknown discriminants is an
8030 -- indefinite type that the back-end cannot handle directly.
8031 -- We treat it as a private type, and build a completion that is
8032 -- derived from the full view of the parent, and hopefully has
8033 -- known discriminants.
8035 -- If the full view of the parent type has an underlying record view,
8036 -- use it to generate the underlying record view of this derived type
8037 -- (required for chains of derivations with unknown discriminants).
8039 -- Minor optimization: we avoid the generation of useless underlying
8040 -- record view entities if the private type declaration has unknown
8041 -- discriminants but its corresponding full view has no
8044 if Has_Unknown_Discriminants (Parent_Type)
8045 and then Present (Full_P)
8046 and then (Has_Discriminants (Full_P)
8047 or else Present (Underlying_Record_View (Full_P)))
8048 and then not In_Open_Scopes (Par_Scope)
8049 and then Expander_Active
8052 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
8053 New_Ext : constant Node_Id :=
8055 (Record_Extension_Part (Type_Definition (N)));
8059 Build_Derived_Record_Type
8060 (N, Parent_Type, Derived_Type, Derive_Subps);
8062 -- Build anonymous completion, as a derivation from the full
8063 -- view of the parent. This is not a completion in the usual
8064 -- sense, because the current type is not private.
8067 Make_Full_Type_Declaration (Loc,
8068 Defining_Identifier => Full_Der,
8070 Make_Derived_Type_Definition (Loc,
8071 Subtype_Indication =>
8073 (Subtype_Indication (Type_Definition (N))),
8074 Record_Extension_Part => New_Ext));
8076 -- If the parent type has an underlying record view, use it
8077 -- here to build the new underlying record view.
8079 if Present (Underlying_Record_View (Full_P)) then
8081 (Nkind (Subtype_Indication (Type_Definition (Decl)))
8083 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
8084 Underlying_Record_View (Full_P));
8087 Install_Private_Declarations (Par_Scope);
8088 Install_Visible_Declarations (Par_Scope);
8089 Insert_Before (N, Decl);
8091 -- Mark entity as an underlying record view before analysis,
8092 -- to avoid generating the list of its primitive operations
8093 -- (which is not really required for this entity) and thus
8094 -- prevent spurious errors associated with missing overriding
8095 -- of abstract primitives (overridden only for Derived_Type).
8097 Mutate_Ekind (Full_Der, E_Record_Type);
8098 Set_Is_Underlying_Record_View (Full_Der);
8099 Set_Default_SSO (Full_Der);
8100 Set_No_Reordering (Full_Der, No_Component_Reordering);
8104 pragma Assert (Has_Discriminants (Full_Der)
8105 and then not Has_Unknown_Discriminants (Full_Der));
8107 Uninstall_Declarations (Par_Scope);
8109 -- Freeze the underlying record view, to prevent generation of
8110 -- useless dispatching information, which is simply shared with
8111 -- the real derived type.
8113 Set_Is_Frozen (Full_Der);
8115 -- If the derived type has access discriminants, create
8116 -- references to their anonymous types now, to prevent
8117 -- back-end problems when their first use is in generated
8118 -- bodies of primitives.
8124 E := First_Entity (Full_Der);
8126 while Present (E) loop
8127 if Ekind (E) = E_Discriminant
8128 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
8130 Build_Itype_Reference (Etype (E), Decl);
8137 -- Set up links between real entity and underlying record view
8139 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
8140 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
8143 -- If discriminants are known, build derived record
8146 Build_Derived_Record_Type
8147 (N, Parent_Type, Derived_Type, Derive_Subps);
8152 elsif Has_Discriminants (Parent_Type) then
8154 -- Build partial view of derived type from partial view of parent.
8155 -- This must be done before building the full derivation because the
8156 -- second derivation will modify the discriminants of the first and
8157 -- the discriminants are chained with the rest of the components in
8158 -- the full derivation.
8160 Build_Derived_Record_Type
8161 (N, Parent_Type, Derived_Type, Derive_Subps);
8163 -- Build the full derivation if this is not the anonymous derived
8164 -- base type created by Build_Derived_Record_Type in the constrained
8165 -- case (see point 5. of its head comment) since we build it for the
8168 if Present (Available_Full_View (Parent_Type))
8169 and then not Is_Itype (Derived_Type)
8172 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
8174 Last_Discr : Entity_Id;
8177 -- If this is not a completion, construct the implicit full
8178 -- view by deriving from the full view of the parent type.
8179 -- But if this is a completion, the derived private type
8180 -- being built is a full view and the full derivation can
8181 -- only be its underlying full view.
8183 Build_Full_Derivation;
8185 if not Is_Completion then
8186 Set_Full_View (Derived_Type, Full_Der);
8188 Set_Underlying_Full_View (Derived_Type, Full_Der);
8189 Set_Is_Underlying_Full_View (Full_Der);
8192 if not Is_Base_Type (Derived_Type) then
8193 Set_Full_View (Der_Base, Base_Type (Full_Der));
8196 -- Copy the discriminant list from full view to the partial
8197 -- view (base type and its subtype). Gigi requires that the
8198 -- partial and full views have the same discriminants.
8200 -- Note that since the partial view points to discriminants
8201 -- in the full view, their scope will be that of the full
8202 -- view. This might cause some front end problems and need
8205 Discr := First_Discriminant (Base_Type (Full_Der));
8206 Set_First_Entity (Der_Base, Discr);
8209 Last_Discr := Discr;
8210 Next_Discriminant (Discr);
8211 exit when No (Discr);
8214 Set_Last_Entity (Der_Base, Last_Discr);
8215 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
8216 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
8220 elsif Present (Available_Full_View (Parent_Type))
8221 and then Has_Discriminants (Available_Full_View (Parent_Type))
8223 if Has_Unknown_Discriminants (Parent_Type)
8224 and then Nkind (Subtype_Indication (Type_Definition (N))) =
8225 N_Subtype_Indication
8228 ("cannot constrain type with unknown discriminants",
8229 Subtype_Indication (Type_Definition (N)));
8233 -- If this is not a completion, construct the implicit full view by
8234 -- deriving from the full view of the parent type. But if this is a
8235 -- completion, the derived private type being built is a full view
8236 -- and the full derivation can only be its underlying full view.
8238 Build_Full_Derivation;
8240 if not Is_Completion then
8241 Set_Full_View (Derived_Type, Full_Der);
8243 Set_Underlying_Full_View (Derived_Type, Full_Der);
8244 Set_Is_Underlying_Full_View (Full_Der);
8247 -- In any case, the primitive operations are inherited from the
8248 -- parent type, not from the internal full view.
8250 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8252 if Derive_Subps then
8253 Derive_Subprograms (Parent_Type, Derived_Type);
8256 Set_Stored_Constraint (Derived_Type, No_Elist);
8258 (Derived_Type, Is_Constrained (Available_Full_View (Parent_Type)));
8261 -- Untagged type, No discriminants on either view
8263 if Nkind (Subtype_Indication (Type_Definition (N))) =
8264 N_Subtype_Indication
8267 ("illegal constraint on type without discriminants", N);
8270 if Present (Discriminant_Specifications (N))
8271 and then Present (Available_Full_View (Parent_Type))
8272 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8274 Error_Msg_N ("cannot add discriminants to untagged type", N);
8277 Set_Stored_Constraint (Derived_Type, No_Elist);
8278 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8280 Set_Is_Controlled_Active
8281 (Derived_Type, Is_Controlled_Active (Parent_Type));
8283 Set_Disable_Controlled
8284 (Derived_Type, Disable_Controlled (Parent_Type));
8286 Set_Has_Controlled_Component
8287 (Derived_Type, Has_Controlled_Component (Parent_Type));
8289 -- Direct controlled types do not inherit Finalize_Storage_Only flag
8291 if not Is_Controlled (Parent_Type) then
8292 Set_Finalize_Storage_Only
8293 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8296 -- If this is not a completion, construct the implicit full view by
8297 -- deriving from the full view of the parent type. But if this is a
8298 -- completion, the derived private type being built is a full view
8299 -- and the full derivation can only be its underlying full view.
8301 -- ??? If the parent type is untagged private and its completion is
8302 -- tagged, this mechanism will not work because we cannot derive from
8303 -- the tagged full view unless we have an extension.
8305 if Present (Available_Full_View (Parent_Type))
8306 and then not Is_Tagged_Type (Available_Full_View (Parent_Type))
8307 and then not Error_Posted (N)
8309 Build_Full_Derivation;
8311 if not Is_Completion then
8312 Set_Full_View (Derived_Type, Full_Der);
8314 Set_Underlying_Full_View (Derived_Type, Full_Der);
8315 Set_Is_Underlying_Full_View (Full_Der);
8320 Set_Has_Unknown_Discriminants (Derived_Type,
8321 Has_Unknown_Discriminants (Parent_Type));
8323 if Is_Private_Type (Derived_Type) then
8324 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8327 -- If the parent base type is in scope, add the derived type to its
8328 -- list of private dependents, because its full view may become
8329 -- visible subsequently (in a nested private part, a body, or in a
8330 -- further child unit).
8332 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8333 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8335 -- Check for unusual case where a type completed by a private
8336 -- derivation occurs within a package nested in a child unit, and
8337 -- the parent is declared in an ancestor.
8339 if Is_Child_Unit (Scope (Current_Scope))
8340 and then Is_Completion
8341 and then In_Private_Part (Current_Scope)
8342 and then Scope (Parent_Type) /= Current_Scope
8344 -- Note that if the parent has a completion in the private part,
8345 -- (which is itself a derivation from some other private type)
8346 -- it is that completion that is visible, there is no full view
8347 -- available, and no special processing is needed.
8349 and then Present (Full_View (Parent_Type))
8351 -- In this case, the full view of the parent type will become
8352 -- visible in the body of the enclosing child, and only then will
8353 -- the current type be possibly non-private. Build an underlying
8354 -- full view that will be installed when the enclosing child body
8357 if Present (Underlying_Full_View (Derived_Type)) then
8358 Full_Der := Underlying_Full_View (Derived_Type);
8360 Build_Full_Derivation;
8361 Set_Underlying_Full_View (Derived_Type, Full_Der);
8362 Set_Is_Underlying_Full_View (Full_Der);
8365 -- The full view will be used to swap entities on entry/exit to
8366 -- the body, and must appear in the entity list for the package.
8368 Append_Entity (Full_Der, Scope (Derived_Type));
8371 end Build_Derived_Private_Type;
8373 -------------------------------
8374 -- Build_Derived_Record_Type --
8375 -------------------------------
8379 -- Ideally we would like to use the same model of type derivation for
8380 -- tagged and untagged record types. Unfortunately this is not quite
8381 -- possible because the semantics of representation clauses is different
8382 -- for tagged and untagged records under inheritance. Consider the
8385 -- type R (...) is [tagged] record ... end record;
8386 -- type T (...) is new R (...) [with ...];
8388 -- The representation clauses for T can specify a completely different
8389 -- record layout from R's. Hence the same component can be placed in two
8390 -- very different positions in objects of type T and R. If R and T are
8391 -- tagged types, representation clauses for T can only specify the layout
8392 -- of non inherited components, thus components that are common in R and T
8393 -- have the same position in objects of type R and T.
8395 -- This has two implications. The first is that the entire tree for R's
8396 -- declaration needs to be copied for T in the untagged case, so that T
8397 -- can be viewed as a record type of its own with its own representation
8398 -- clauses. The second implication is the way we handle discriminants.
8399 -- Specifically, in the untagged case we need a way to communicate to Gigi
8400 -- what are the real discriminants in the record, while for the semantics
8401 -- we need to consider those introduced by the user to rename the
8402 -- discriminants in the parent type. This is handled by introducing the
8403 -- notion of stored discriminants. See below for more.
8405 -- Fortunately the way regular components are inherited can be handled in
8406 -- the same way in tagged and untagged types.
8408 -- To complicate things a bit more the private view of a private extension
8409 -- cannot be handled in the same way as the full view (for one thing the
8410 -- semantic rules are somewhat different). We will explain what differs
8413 -- 2. DISCRIMINANTS UNDER INHERITANCE
8415 -- The semantic rules governing the discriminants of derived types are
8418 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8419 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8421 -- If parent type has discriminants, then the discriminants that are
8422 -- declared in the derived type are [3.4 (11)]:
8424 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8427 -- o Otherwise, each discriminant of the parent type (implicitly declared
8428 -- in the same order with the same specifications). In this case, the
8429 -- discriminants are said to be "inherited", or if unknown in the parent
8430 -- are also unknown in the derived type.
8432 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8434 -- o The parent subtype must be constrained;
8436 -- o If the parent type is not a tagged type, then each discriminant of
8437 -- the derived type must be used in the constraint defining a parent
8438 -- subtype. [Implementation note: This ensures that the new discriminant
8439 -- can share storage with an existing discriminant.]
8441 -- For the derived type each discriminant of the parent type is either
8442 -- inherited, constrained to equal some new discriminant of the derived
8443 -- type, or constrained to the value of an expression.
8445 -- When inherited or constrained to equal some new discriminant, the
8446 -- parent discriminant and the discriminant of the derived type are said
8449 -- If a discriminant of the parent type is constrained to a specific value
8450 -- in the derived type definition, then the discriminant is said to be
8451 -- "specified" by that derived type definition.
8453 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8455 -- We have spoken about stored discriminants in point 1 (introduction)
8456 -- above. There are two sorts of stored discriminants: implicit and
8457 -- explicit. As long as the derived type inherits the same discriminants as
8458 -- the root record type, stored discriminants are the same as regular
8459 -- discriminants, and are said to be implicit. However, if any discriminant
8460 -- in the root type was renamed in the derived type, then the derived
8461 -- type will contain explicit stored discriminants. Explicit stored
8462 -- discriminants are discriminants in addition to the semantically visible
8463 -- discriminants defined for the derived type. Stored discriminants are
8464 -- used by Gigi to figure out what are the physical discriminants in
8465 -- objects of the derived type (see precise definition in einfo.ads).
8466 -- As an example, consider the following:
8468 -- type R (D1, D2, D3 : Int) is record ... end record;
8469 -- type T1 is new R;
8470 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8471 -- type T3 is new T2;
8472 -- type T4 (Y : Int) is new T3 (Y, 99);
8474 -- The following table summarizes the discriminants and stored
8475 -- discriminants in R and T1 through T4:
8477 -- Type Discrim Stored Discrim Comment
8478 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8479 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8480 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8481 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8482 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8484 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8485 -- find the corresponding discriminant in the parent type, while
8486 -- Original_Record_Component (abbreviated ORC below) the actual physical
8487 -- component that is renamed. Finally the field Is_Completely_Hidden
8488 -- (abbreviated ICH below) is set for all explicit stored discriminants
8489 -- (see einfo.ads for more info). For the above example this gives:
8491 -- Discrim CD ORC ICH
8492 -- ^^^^^^^ ^^ ^^^ ^^^
8493 -- D1 in R empty itself no
8494 -- D2 in R empty itself no
8495 -- D3 in R empty itself no
8497 -- D1 in T1 D1 in R itself no
8498 -- D2 in T1 D2 in R itself no
8499 -- D3 in T1 D3 in R itself no
8501 -- X1 in T2 D3 in T1 D3 in T2 no
8502 -- X2 in T2 D1 in T1 D1 in T2 no
8503 -- D1 in T2 empty itself yes
8504 -- D2 in T2 empty itself yes
8505 -- D3 in T2 empty itself yes
8507 -- X1 in T3 X1 in T2 D3 in T3 no
8508 -- X2 in T3 X2 in T2 D1 in T3 no
8509 -- D1 in T3 empty itself yes
8510 -- D2 in T3 empty itself yes
8511 -- D3 in T3 empty itself yes
8513 -- Y in T4 X1 in T3 D3 in T4 no
8514 -- D1 in T4 empty itself yes
8515 -- D2 in T4 empty itself yes
8516 -- D3 in T4 empty itself yes
8518 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8520 -- Type derivation for tagged types is fairly straightforward. If no
8521 -- discriminants are specified by the derived type, these are inherited
8522 -- from the parent. No explicit stored discriminants are ever necessary.
8523 -- The only manipulation that is done to the tree is that of adding a
8524 -- _parent field with parent type and constrained to the same constraint
8525 -- specified for the parent in the derived type definition. For instance:
8527 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8528 -- type T1 is new R with null record;
8529 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8531 -- are changed into:
8533 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8534 -- _parent : R (D1, D2, D3);
8537 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8538 -- _parent : T1 (X2, 88, X1);
8541 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8542 -- ORC and ICH fields are:
8544 -- Discrim CD ORC ICH
8545 -- ^^^^^^^ ^^ ^^^ ^^^
8546 -- D1 in R empty itself no
8547 -- D2 in R empty itself no
8548 -- D3 in R empty itself no
8550 -- D1 in T1 D1 in R D1 in R no
8551 -- D2 in T1 D2 in R D2 in R no
8552 -- D3 in T1 D3 in R D3 in R no
8554 -- X1 in T2 D3 in T1 D3 in R no
8555 -- X2 in T2 D1 in T1 D1 in R no
8557 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8559 -- Regardless of whether we dealing with a tagged or untagged type
8560 -- we will transform all derived type declarations of the form
8562 -- type T is new R (...) [with ...];
8564 -- subtype S is R (...);
8565 -- type T is new S [with ...];
8567 -- type BT is new R [with ...];
8568 -- subtype T is BT (...);
8570 -- That is, the base derived type is constrained only if it has no
8571 -- discriminants. The reason for doing this is that GNAT's semantic model
8572 -- assumes that a base type with discriminants is unconstrained.
8574 -- Note that, strictly speaking, the above transformation is not always
8575 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8577 -- procedure B34011A is
8578 -- type REC (D : integer := 0) is record
8583 -- type T6 is new Rec;
8584 -- function F return T6;
8589 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8592 -- The definition of Q6.U is illegal. However transforming Q6.U into
8594 -- type BaseU is new T6;
8595 -- subtype U is BaseU (Q6.F.I)
8597 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8598 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8599 -- the transformation described above.
8601 -- There is another instance where the above transformation is incorrect.
8605 -- type Base (D : Integer) is tagged null record;
8606 -- procedure P (X : Base);
8608 -- type Der is new Base (2) with null record;
8609 -- procedure P (X : Der);
8612 -- Then the above transformation turns this into
8614 -- type Der_Base is new Base with null record;
8615 -- -- procedure P (X : Base) is implicitly inherited here
8616 -- -- as procedure P (X : Der_Base).
8618 -- subtype Der is Der_Base (2);
8619 -- procedure P (X : Der);
8620 -- -- The overriding of P (X : Der_Base) is illegal since we
8621 -- -- have a parameter conformance problem.
8623 -- To get around this problem, after having semantically processed Der_Base
8624 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8625 -- Discriminant_Constraint from Der so that when parameter conformance is
8626 -- checked when P is overridden, no semantic errors are flagged.
8628 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8630 -- Regardless of whether we are dealing with a tagged or untagged type
8631 -- we will transform all derived type declarations of the form
8633 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8634 -- type T is new R [with ...];
8636 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8638 -- The reason for such transformation is that it allows us to implement a
8639 -- very clean form of component inheritance as explained below.
8641 -- Note that this transformation is not achieved by direct tree rewriting
8642 -- and manipulation, but rather by redoing the semantic actions that the
8643 -- above transformation will entail. This is done directly in routine
8644 -- Inherit_Components.
8646 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8648 -- In both tagged and untagged derived types, regular non discriminant
8649 -- components are inherited in the derived type from the parent type. In
8650 -- the absence of discriminants component, inheritance is straightforward
8651 -- as components can simply be copied from the parent.
8653 -- If the parent has discriminants, inheriting components constrained with
8654 -- these discriminants requires caution. Consider the following example:
8656 -- type R (D1, D2 : Positive) is [tagged] record
8657 -- S : String (D1 .. D2);
8660 -- type T1 is new R [with null record];
8661 -- type T2 (X : positive) is new R (1, X) [with null record];
8663 -- As explained in 6. above, T1 is rewritten as
8664 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8665 -- which makes the treatment for T1 and T2 identical.
8667 -- What we want when inheriting S, is that references to D1 and D2 in R are
8668 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8669 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8670 -- with either discriminant references in the derived type or expressions.
8671 -- This replacement is achieved as follows: before inheriting R's
8672 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8673 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8674 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8675 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8676 -- by String (1 .. X).
8678 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8680 -- We explain here the rules governing private type extensions relevant to
8681 -- type derivation. These rules are explained on the following example:
8683 -- type D [(...)] is new A [(...)] with private; <-- partial view
8684 -- type D [(...)] is new P [(...)] with null record; <-- full view
8686 -- Type A is called the ancestor subtype of the private extension.
8687 -- Type P is the parent type of the full view of the private extension. It
8688 -- must be A or a type derived from A.
8690 -- The rules concerning the discriminants of private type extensions are
8693 -- o If a private extension inherits known discriminants from the ancestor
8694 -- subtype, then the full view must also inherit its discriminants from
8695 -- the ancestor subtype and the parent subtype of the full view must be
8696 -- constrained if and only if the ancestor subtype is constrained.
8698 -- o If a partial view has unknown discriminants, then the full view may
8699 -- define a definite or an indefinite subtype, with or without
8702 -- o If a partial view has neither known nor unknown discriminants, then
8703 -- the full view must define a definite subtype.
8705 -- o If the ancestor subtype of a private extension has constrained
8706 -- discriminants, then the parent subtype of the full view must impose a
8707 -- statically matching constraint on those discriminants.
8709 -- This means that only the following forms of private extensions are
8712 -- type D is new A with private; <-- partial view
8713 -- type D is new P with null record; <-- full view
8715 -- If A has no discriminants than P has no discriminants, otherwise P must
8716 -- inherit A's discriminants.
8718 -- type D is new A (...) with private; <-- partial view
8719 -- type D is new P (:::) with null record; <-- full view
8721 -- P must inherit A's discriminants and (...) and (:::) must statically
8724 -- subtype A is R (...);
8725 -- type D is new A with private; <-- partial view
8726 -- type D is new P with null record; <-- full view
8728 -- P must have inherited R's discriminants and must be derived from A or
8729 -- any of its subtypes.
8731 -- type D (..) is new A with private; <-- partial view
8732 -- type D (..) is new P [(:::)] with null record; <-- full view
8734 -- No specific constraints on P's discriminants or constraint (:::).
8735 -- Note that A can be unconstrained, but the parent subtype P must either
8736 -- be constrained or (:::) must be present.
8738 -- type D (..) is new A [(...)] with private; <-- partial view
8739 -- type D (..) is new P [(:::)] with null record; <-- full view
8741 -- P's constraints on A's discriminants must statically match those
8742 -- imposed by (...).
8744 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8746 -- The full view of a private extension is handled exactly as described
8747 -- above. The model chose for the private view of a private extension is
8748 -- the same for what concerns discriminants (i.e. they receive the same
8749 -- treatment as in the tagged case). However, the private view of the
8750 -- private extension always inherits the components of the parent base,
8751 -- without replacing any discriminant reference. Strictly speaking this is
8752 -- incorrect. However, Gigi never uses this view to generate code so this
8753 -- is a purely semantic issue. In theory, a set of transformations similar
8754 -- to those given in 5. and 6. above could be applied to private views of
8755 -- private extensions to have the same model of component inheritance as
8756 -- for non private extensions. However, this is not done because it would
8757 -- further complicate private type processing. Semantically speaking, this
8758 -- leaves us in an uncomfortable situation. As an example consider:
8761 -- type R (D : integer) is tagged record
8762 -- S : String (1 .. D);
8764 -- procedure P (X : R);
8765 -- type T is new R (1) with private;
8767 -- type T is new R (1) with null record;
8770 -- This is transformed into:
8773 -- type R (D : integer) is tagged record
8774 -- S : String (1 .. D);
8776 -- procedure P (X : R);
8777 -- type T is new R (1) with private;
8779 -- type BaseT is new R with null record;
8780 -- subtype T is BaseT (1);
8783 -- (strictly speaking the above is incorrect Ada)
8785 -- From the semantic standpoint the private view of private extension T
8786 -- should be flagged as constrained since one can clearly have
8790 -- in a unit withing Pack. However, when deriving subprograms for the
8791 -- private view of private extension T, T must be seen as unconstrained
8792 -- since T has discriminants (this is a constraint of the current
8793 -- subprogram derivation model). Thus, when processing the private view of
8794 -- a private extension such as T, we first mark T as unconstrained, we
8795 -- process it, we perform program derivation and just before returning from
8796 -- Build_Derived_Record_Type we mark T as constrained.
8798 -- ??? Are there are other uncomfortable cases that we will have to
8801 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8803 -- Types that are derived from a visible record type and have a private
8804 -- extension present other peculiarities. They behave mostly like private
8805 -- types, but if they have primitive operations defined, these will not
8806 -- have the proper signatures for further inheritance, because other
8807 -- primitive operations will use the implicit base that we define for
8808 -- private derivations below. This affect subprogram inheritance (see
8809 -- Derive_Subprograms for details). We also derive the implicit base from
8810 -- the base type of the full view, so that the implicit base is a record
8811 -- type and not another private type, This avoids infinite loops.
8813 procedure Build_Derived_Record_Type
8815 Parent_Type : Entity_Id;
8816 Derived_Type : Entity_Id;
8817 Derive_Subps : Boolean := True)
8819 Discriminant_Specs : constant Boolean :=
8820 Present (Discriminant_Specifications (N));
8821 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8822 Loc : constant Source_Ptr := Sloc (N);
8823 Private_Extension : constant Boolean :=
8824 Nkind (N) = N_Private_Extension_Declaration;
8825 Assoc_List : Elist_Id;
8826 Constraint_Present : Boolean;
8828 Discrim : Entity_Id;
8830 Inherit_Discrims : Boolean := False;
8831 Last_Discrim : Entity_Id;
8832 New_Base : Entity_Id;
8834 New_Discrs : Elist_Id;
8835 New_Indic : Node_Id;
8836 Parent_Base : Entity_Id;
8837 Save_Etype : Entity_Id;
8838 Save_Discr_Constr : Elist_Id;
8839 Save_Next_Entity : Entity_Id;
8842 Discs : Elist_Id := New_Elmt_List;
8843 -- An empty Discs list means that there were no constraints in the
8844 -- subtype indication or that there was an error processing it.
8846 procedure Check_Generic_Ancestors;
8847 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8848 -- cannot be declared at a deeper level than its parent type is
8849 -- removed. The check on derivation within a generic body is also
8850 -- relaxed, but there's a restriction that a derived tagged type
8851 -- cannot be declared in a generic body if it's derived directly
8852 -- or indirectly from a formal type of that generic. This applies
8853 -- to progenitors as well.
8855 -----------------------------
8856 -- Check_Generic_Ancestors --
8857 -----------------------------
8859 procedure Check_Generic_Ancestors is
8860 Ancestor_Type : Entity_Id;
8861 Intf_List : List_Id;
8862 Intf_Name : Node_Id;
8864 procedure Check_Ancestor;
8865 -- For parent and progenitors.
8867 --------------------
8868 -- Check_Ancestor --
8869 --------------------
8871 procedure Check_Ancestor is
8873 -- If the derived type does have a formal type as an ancestor
8874 -- then it's an error if the derived type is declared within
8875 -- the body of the generic unit that declares the formal type
8876 -- in its generic formal part. It's sufficient to check whether
8877 -- the ancestor type is declared inside the same generic body
8878 -- as the derived type (such as within a nested generic spec),
8879 -- in which case the derivation is legal. If the formal type is
8880 -- declared outside of that generic body, then it's certain
8881 -- that the derived type is declared within the generic body
8882 -- of the generic unit declaring the formal type.
8884 if Is_Generic_Type (Ancestor_Type)
8885 and then Enclosing_Generic_Body (Ancestor_Type) /=
8886 Enclosing_Generic_Body (Derived_Type)
8889 ("ancestor type& is formal type of enclosing"
8890 & " generic unit (RM 3.9.1 (4/2))",
8891 Indic, Ancestor_Type);
8896 if Nkind (N) = N_Private_Extension_Declaration then
8897 Intf_List := Interface_List (N);
8899 Intf_List := Interface_List (Type_Definition (N));
8902 if Present (Enclosing_Generic_Body (Derived_Type)) then
8903 Ancestor_Type := Parent_Type;
8905 while not Is_Generic_Type (Ancestor_Type)
8906 and then Etype (Ancestor_Type) /= Ancestor_Type
8908 Ancestor_Type := Etype (Ancestor_Type);
8913 if Present (Intf_List) then
8914 Intf_Name := First (Intf_List);
8915 while Present (Intf_Name) loop
8916 Ancestor_Type := Entity (Intf_Name);
8922 end Check_Generic_Ancestors;
8924 -- Start of processing for Build_Derived_Record_Type
8927 if Ekind (Parent_Type) = E_Record_Type_With_Private
8928 and then Present (Full_View (Parent_Type))
8929 and then Has_Discriminants (Parent_Type)
8931 Parent_Base := Base_Type (Full_View (Parent_Type));
8933 Parent_Base := Base_Type (Parent_Type);
8936 -- If the parent type is declared as a subtype of another private
8937 -- type with inherited discriminants, its generated base type is
8938 -- itself a record subtype. To further inherit the constraint we
8939 -- need to use its own base to have an unconstrained type on which
8940 -- to apply the inherited constraint.
8942 if Ekind (Parent_Base) = E_Record_Subtype then
8943 Parent_Base := Base_Type (Parent_Base);
8946 -- AI05-0115: if this is a derivation from a private type in some
8947 -- other scope that may lead to invisible components for the derived
8948 -- type, mark it accordingly.
8950 if Is_Private_Type (Parent_Type) then
8951 if Scope (Parent_Base) = Scope (Derived_Type) then
8954 elsif In_Open_Scopes (Scope (Parent_Base))
8955 and then In_Private_Part (Scope (Parent_Base))
8960 Set_Has_Private_Ancestor (Derived_Type);
8964 Set_Has_Private_Ancestor
8965 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8968 -- Before we start the previously documented transformations, here is
8969 -- little fix for size and alignment of tagged types. Normally when we
8970 -- derive type D from type P, we copy the size and alignment of P as the
8971 -- default for D, and in the absence of explicit representation clauses
8972 -- for D, the size and alignment are indeed the same as the parent.
8974 -- But this is wrong for tagged types, since fields may be added, and
8975 -- the default size may need to be larger, and the default alignment may
8976 -- need to be larger.
8978 -- We therefore reset the size and alignment fields in the tagged case.
8979 -- Note that the size and alignment will in any case be at least as
8980 -- large as the parent type (since the derived type has a copy of the
8981 -- parent type in the _parent field)
8983 -- The type is also marked as being tagged here, which is needed when
8984 -- processing components with a self-referential anonymous access type
8985 -- in the call to Check_Anonymous_Access_Components below. Note that
8986 -- this flag is also set later on for completeness.
8989 Set_Is_Tagged_Type (Derived_Type);
8990 Reinit_Size_Align (Derived_Type);
8993 -- STEP 0a: figure out what kind of derived type declaration we have
8995 if Private_Extension then
8997 Mutate_Ekind (Derived_Type, E_Record_Type_With_Private);
8998 Set_Default_SSO (Derived_Type);
8999 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9002 Type_Def := Type_Definition (N);
9004 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
9005 -- Parent_Base can be a private type or private extension. However,
9006 -- for tagged types with an extension the newly added fields are
9007 -- visible and hence the Derived_Type is always an E_Record_Type.
9008 -- (except that the parent may have its own private fields).
9009 -- For untagged types we preserve the Ekind of the Parent_Base.
9011 if Present (Record_Extension_Part (Type_Def)) then
9012 Mutate_Ekind (Derived_Type, E_Record_Type);
9013 Set_Default_SSO (Derived_Type);
9014 Set_No_Reordering (Derived_Type, No_Component_Reordering);
9016 -- Create internal access types for components with anonymous
9019 if Ada_Version >= Ada_2005 then
9020 Check_Anonymous_Access_Components
9021 (N, Derived_Type, Derived_Type,
9022 Component_List (Record_Extension_Part (Type_Def)));
9026 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
9030 -- Indic can either be an N_Identifier if the subtype indication
9031 -- contains no constraint or an N_Subtype_Indication if the subtype
9032 -- indication has a constraint. In either case it can include an
9035 Indic := Subtype_Indication (Type_Def);
9036 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
9038 -- Check that the type has visible discriminants. The type may be
9039 -- a private type with unknown discriminants whose full view has
9040 -- discriminants which are invisible.
9042 if Constraint_Present then
9043 if not Has_Discriminants (Parent_Base)
9045 (Has_Unknown_Discriminants (Parent_Base)
9046 and then Is_Private_Type (Parent_Base))
9049 ("invalid constraint: type has no discriminant",
9050 Constraint (Indic));
9052 Constraint_Present := False;
9053 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9055 elsif Is_Constrained (Parent_Type) then
9057 ("invalid constraint: parent type is already constrained",
9058 Constraint (Indic));
9060 Constraint_Present := False;
9061 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
9065 -- STEP 0b: If needed, apply transformation given in point 5. above
9067 if not Private_Extension
9068 and then Has_Discriminants (Parent_Type)
9069 and then not Discriminant_Specs
9070 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
9072 -- First, we must analyze the constraint (see comment in point 5.)
9073 -- The constraint may come from the subtype indication of the full
9076 if Constraint_Present then
9077 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
9079 -- If there is no explicit constraint, there might be one that is
9080 -- inherited from a constrained parent type. In that case verify that
9081 -- it conforms to the constraint in the partial view. In perverse
9082 -- cases the parent subtypes of the partial and full view can have
9083 -- different constraints.
9085 elsif Present (Stored_Constraint (Parent_Type)) then
9086 New_Discrs := Stored_Constraint (Parent_Type);
9089 New_Discrs := No_Elist;
9092 if Has_Discriminants (Derived_Type)
9093 and then Has_Private_Declaration (Derived_Type)
9094 and then Present (Discriminant_Constraint (Derived_Type))
9095 and then Present (New_Discrs)
9097 -- Verify that constraints of the full view statically match
9098 -- those given in the partial view.
9104 C1 := First_Elmt (New_Discrs);
9105 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
9106 while Present (C1) and then Present (C2) loop
9107 if Fully_Conformant_Expressions (Node (C1), Node (C2))
9109 (Is_OK_Static_Expression (Node (C1))
9110 and then Is_OK_Static_Expression (Node (C2))
9112 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
9117 if Constraint_Present then
9119 ("constraint not conformant to previous declaration",
9123 ("constraint of full view is incompatible "
9124 & "with partial view", N);
9134 -- Insert and analyze the declaration for the unconstrained base type
9136 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
9139 Make_Full_Type_Declaration (Loc,
9140 Defining_Identifier => New_Base,
9142 Make_Derived_Type_Definition (Loc,
9143 Abstract_Present => Abstract_Present (Type_Def),
9144 Limited_Present => Limited_Present (Type_Def),
9145 Subtype_Indication =>
9146 New_Occurrence_Of (Parent_Base, Loc),
9147 Record_Extension_Part =>
9148 Relocate_Node (Record_Extension_Part (Type_Def)),
9149 Interface_List => Interface_List (Type_Def)));
9151 Set_Parent (New_Decl, Parent (N));
9152 Mark_Rewrite_Insertion (New_Decl);
9153 Insert_Before (N, New_Decl);
9155 -- In the extension case, make sure ancestor is frozen appropriately
9156 -- (see also non-discriminated case below).
9158 if Present (Record_Extension_Part (Type_Def))
9159 or else Is_Interface (Parent_Base)
9161 Freeze_Before (New_Decl, Parent_Type);
9164 -- Note that this call passes False for the Derive_Subps parameter
9165 -- because subprogram derivation is deferred until after creating
9166 -- the subtype (see below).
9169 (New_Decl, Parent_Base, New_Base,
9170 Is_Completion => False, Derive_Subps => False);
9172 -- ??? This needs re-examination to determine whether the
9173 -- above call can simply be replaced by a call to Analyze.
9175 Set_Analyzed (New_Decl);
9177 -- Insert and analyze the declaration for the constrained subtype
9179 if Constraint_Present then
9181 Make_Subtype_Indication (Loc,
9182 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9183 Constraint => Relocate_Node (Constraint (Indic)));
9187 Constr_List : constant List_Id := New_List;
9192 C := First_Elmt (Discriminant_Constraint (Parent_Type));
9193 while Present (C) loop
9196 -- It is safe here to call New_Copy_Tree since we called
9197 -- Force_Evaluation on each constraint previously
9198 -- in Build_Discriminant_Constraints.
9200 Append (New_Copy_Tree (Expr), To => Constr_List);
9206 Make_Subtype_Indication (Loc,
9207 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
9209 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
9214 Make_Subtype_Declaration (Loc,
9215 Defining_Identifier => Derived_Type,
9216 Subtype_Indication => New_Indic));
9220 -- Derivation of subprograms must be delayed until the full subtype
9221 -- has been established, to ensure proper overriding of subprograms
9222 -- inherited by full types. If the derivations occurred as part of
9223 -- the call to Build_Derived_Type above, then the check for type
9224 -- conformance would fail because earlier primitive subprograms
9225 -- could still refer to the full type prior the change to the new
9226 -- subtype and hence would not match the new base type created here.
9227 -- Subprograms are not derived, however, when Derive_Subps is False
9228 -- (since otherwise there could be redundant derivations).
9230 if Derive_Subps then
9231 Derive_Subprograms (Parent_Type, Derived_Type);
9234 -- For tagged types the Discriminant_Constraint of the new base itype
9235 -- is inherited from the first subtype so that no subtype conformance
9236 -- problem arise when the first subtype overrides primitive
9237 -- operations inherited by the implicit base type.
9240 Set_Discriminant_Constraint
9241 (New_Base, Discriminant_Constraint (Derived_Type));
9247 -- If we get here Derived_Type will have no discriminants or it will be
9248 -- a discriminated unconstrained base type.
9250 -- STEP 1a: perform preliminary actions/checks for derived tagged types
9254 -- The parent type is frozen for non-private extensions (RM 13.14(7))
9255 -- The declaration of a specific descendant of an interface type
9256 -- freezes the interface type (RM 13.14).
9258 if not Private_Extension or else Is_Interface (Parent_Base) then
9259 Freeze_Before (N, Parent_Type);
9262 if Ada_Version >= Ada_2005 then
9263 Check_Generic_Ancestors;
9265 elsif Type_Access_Level (Derived_Type) /=
9266 Type_Access_Level (Parent_Type)
9267 and then not Is_Generic_Type (Derived_Type)
9269 if Is_Controlled (Parent_Type) then
9271 ("controlled type must be declared at the library level",
9275 ("type extension at deeper accessibility level than parent",
9281 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
9284 and then GB /= Enclosing_Generic_Body (Parent_Base)
9287 ("parent type of& must not be outside generic body"
9289 Indic, Derived_Type);
9295 -- Ada 2005 (AI-251)
9297 if Ada_Version >= Ada_2005 and then Is_Tagged then
9299 -- "The declaration of a specific descendant of an interface type
9300 -- freezes the interface type" (RM 13.14).
9305 if Is_Non_Empty_List (Interface_List (Type_Def)) then
9306 Iface := First (Interface_List (Type_Def));
9307 while Present (Iface) loop
9308 Freeze_Before (N, Etype (Iface));
9315 -- STEP 1b : preliminary cleanup of the full view of private types
9317 -- If the type is already marked as having discriminants, then it's the
9318 -- completion of a private type or private extension and we need to
9319 -- retain the discriminants from the partial view if the current
9320 -- declaration has Discriminant_Specifications so that we can verify
9321 -- conformance. However, we must remove any existing components that
9322 -- were inherited from the parent (and attached in Copy_And_Swap)
9323 -- because the full type inherits all appropriate components anyway, and
9324 -- we do not want the partial view's components interfering.
9326 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9327 Discrim := First_Discriminant (Derived_Type);
9329 Last_Discrim := Discrim;
9330 Next_Discriminant (Discrim);
9331 exit when No (Discrim);
9334 Set_Last_Entity (Derived_Type, Last_Discrim);
9336 -- In all other cases wipe out the list of inherited components (even
9337 -- inherited discriminants), it will be properly rebuilt here.
9340 Set_First_Entity (Derived_Type, Empty);
9341 Set_Last_Entity (Derived_Type, Empty);
9344 -- STEP 1c: Initialize some flags for the Derived_Type
9346 -- The following flags must be initialized here so that
9347 -- Process_Discriminants can check that discriminants of tagged types do
9348 -- not have a default initial value and that access discriminants are
9349 -- only specified for limited records. For completeness, these flags are
9350 -- also initialized along with all the other flags below.
9352 -- AI-419: Limitedness is not inherited from an interface parent, so to
9353 -- be limited in that case the type must be explicitly declared as
9354 -- limited. However, task and protected interfaces are always limited.
9356 if Limited_Present (Type_Def) then
9357 Set_Is_Limited_Record (Derived_Type);
9359 elsif Is_Limited_Record (Parent_Type)
9360 or else (Present (Full_View (Parent_Type))
9361 and then Is_Limited_Record (Full_View (Parent_Type)))
9363 if not Is_Interface (Parent_Type)
9364 or else Is_Concurrent_Interface (Parent_Type)
9366 Set_Is_Limited_Record (Derived_Type);
9370 -- STEP 2a: process discriminants of derived type if any
9372 Push_Scope (Derived_Type);
9374 if Discriminant_Specs then
9375 Set_Has_Unknown_Discriminants (Derived_Type, False);
9377 -- The following call initializes fields Has_Discriminants and
9378 -- Discriminant_Constraint, unless we are processing the completion
9379 -- of a private type declaration.
9381 Check_Or_Process_Discriminants (N, Derived_Type);
9383 -- For untagged types, the constraint on the Parent_Type must be
9384 -- present and is used to rename the discriminants.
9386 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9387 Error_Msg_N ("untagged parent must have discriminants", Indic);
9389 elsif not Is_Tagged and then not Constraint_Present then
9391 ("discriminant constraint needed for derived untagged records",
9394 -- Otherwise the parent subtype must be constrained unless we have a
9395 -- private extension.
9397 elsif not Constraint_Present
9398 and then not Private_Extension
9399 and then not Is_Constrained (Parent_Type)
9402 ("unconstrained type not allowed in this context", Indic);
9404 elsif Constraint_Present then
9405 -- The following call sets the field Corresponding_Discriminant
9406 -- for the discriminants in the Derived_Type.
9408 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9410 -- For untagged types all new discriminants must rename
9411 -- discriminants in the parent. For private extensions new
9412 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9414 Discrim := First_Discriminant (Derived_Type);
9415 while Present (Discrim) loop
9417 and then No (Corresponding_Discriminant (Discrim))
9420 ("new discriminants must constrain old ones", Discrim);
9422 elsif Private_Extension
9423 and then Present (Corresponding_Discriminant (Discrim))
9426 ("only static constraints allowed for parent"
9427 & " discriminants in the partial view", Indic);
9431 -- If a new discriminant is used in the constraint, then its
9432 -- subtype must be statically compatible with the subtype of
9433 -- the parent discriminant (RM 3.7(15)).
9435 if Present (Corresponding_Discriminant (Discrim)) then
9436 Check_Constraining_Discriminant
9437 (Discrim, Corresponding_Discriminant (Discrim));
9440 Next_Discriminant (Discrim);
9443 -- Check whether the constraints of the full view statically
9444 -- match those imposed by the parent subtype [7.3(13)].
9446 if Present (Stored_Constraint (Derived_Type)) then
9451 C1 := First_Elmt (Discs);
9452 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9453 while Present (C1) and then Present (C2) loop
9455 Fully_Conformant_Expressions (Node (C1), Node (C2))
9458 ("not conformant with previous declaration",
9469 -- STEP 2b: No new discriminants, inherit discriminants if any
9472 if Private_Extension then
9473 Set_Has_Unknown_Discriminants
9475 Has_Unknown_Discriminants (Parent_Type)
9476 or else Unknown_Discriminants_Present (N));
9478 -- The partial view of the parent may have unknown discriminants,
9479 -- but if the full view has discriminants and the parent type is
9480 -- in scope they must be inherited.
9482 elsif Has_Unknown_Discriminants (Parent_Type)
9484 (not Has_Discriminants (Parent_Type)
9485 or else not In_Open_Scopes (Scope (Parent_Base)))
9487 Set_Has_Unknown_Discriminants (Derived_Type);
9490 if not Has_Unknown_Discriminants (Derived_Type)
9491 and then not Has_Unknown_Discriminants (Parent_Base)
9492 and then Has_Discriminants (Parent_Type)
9494 Inherit_Discrims := True;
9495 Set_Has_Discriminants
9496 (Derived_Type, True);
9497 Set_Discriminant_Constraint
9498 (Derived_Type, Discriminant_Constraint (Parent_Base));
9501 -- The following test is true for private types (remember
9502 -- transformation 5. is not applied to those) and in an error
9505 if Constraint_Present then
9506 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9509 -- For now mark a new derived type as constrained only if it has no
9510 -- discriminants. At the end of Build_Derived_Record_Type we properly
9511 -- set this flag in the case of private extensions. See comments in
9512 -- point 9. just before body of Build_Derived_Record_Type.
9516 not (Inherit_Discrims
9517 or else Has_Unknown_Discriminants (Derived_Type)));
9520 -- STEP 3: initialize fields of derived type
9522 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9523 Set_Stored_Constraint (Derived_Type, No_Elist);
9525 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9526 -- but cannot be interfaces
9528 if not Private_Extension
9529 and then Ekind (Derived_Type) /= E_Private_Type
9530 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9532 if Interface_Present (Type_Def) then
9533 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9536 Set_Interfaces (Derived_Type, No_Elist);
9539 -- Fields inherited from the Parent_Type
9541 Set_Has_Specified_Layout
9542 (Derived_Type, Has_Specified_Layout (Parent_Type));
9543 Set_Is_Limited_Composite
9544 (Derived_Type, Is_Limited_Composite (Parent_Type));
9545 Set_Is_Private_Composite
9546 (Derived_Type, Is_Private_Composite (Parent_Type));
9548 if Is_Tagged_Type (Parent_Type) then
9549 Set_No_Tagged_Streams_Pragma
9550 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9553 -- Fields inherited from the Parent_Base
9555 Set_Has_Controlled_Component
9556 (Derived_Type, Has_Controlled_Component (Parent_Base));
9557 Set_Has_Non_Standard_Rep
9558 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9559 Set_Has_Primitive_Operations
9560 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9562 -- Set fields for private derived types
9564 if Is_Private_Type (Derived_Type) then
9565 Set_Depends_On_Private (Derived_Type, True);
9566 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9569 -- Inherit fields for non-private types. If this is the completion of a
9570 -- derivation from a private type, the parent itself is private and the
9571 -- attributes come from its full view, which must be present.
9573 if Is_Record_Type (Derived_Type) then
9575 Parent_Full : Entity_Id;
9578 if Is_Private_Type (Parent_Base)
9579 and then not Is_Record_Type (Parent_Base)
9581 Parent_Full := Full_View (Parent_Base);
9583 Parent_Full := Parent_Base;
9586 Set_Component_Alignment
9587 (Derived_Type, Component_Alignment (Parent_Full));
9589 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9590 Set_Has_Complex_Representation
9591 (Derived_Type, Has_Complex_Representation (Parent_Full));
9593 -- For untagged types, inherit the layout by default to avoid
9594 -- costly changes of representation for type conversions.
9596 if not Is_Tagged then
9597 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9598 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9603 -- When prefixed-call syntax is allowed for untagged types, initialize
9604 -- the list of primitive operations to an empty list.
9606 if Extensions_Allowed and then not Is_Tagged then
9607 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9610 -- Set fields for tagged types
9613 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9615 -- All tagged types defined in Ada.Finalization are controlled
9617 if Chars (Scope (Derived_Type)) = Name_Finalization
9618 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9619 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9621 Set_Is_Controlled_Active (Derived_Type);
9623 Set_Is_Controlled_Active
9624 (Derived_Type, Is_Controlled_Active (Parent_Base));
9627 -- Minor optimization: there is no need to generate the class-wide
9628 -- entity associated with an underlying record view.
9630 if not Is_Underlying_Record_View (Derived_Type) then
9631 Make_Class_Wide_Type (Derived_Type);
9634 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9636 if Has_Discriminants (Derived_Type)
9637 and then Constraint_Present
9639 Set_Stored_Constraint
9640 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9643 if Ada_Version >= Ada_2005 then
9645 Ifaces_List : Elist_Id;
9648 -- Checks rules 3.9.4 (13/2 and 14/2)
9650 if Comes_From_Source (Derived_Type)
9651 and then not Is_Private_Type (Derived_Type)
9652 and then Is_Interface (Parent_Type)
9653 and then not Is_Interface (Derived_Type)
9655 if Is_Task_Interface (Parent_Type) then
9657 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9660 elsif Is_Protected_Interface (Parent_Type) then
9662 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9667 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9669 Check_Interfaces (N, Type_Def);
9671 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9672 -- not already in the parents.
9676 Ifaces_List => Ifaces_List,
9677 Exclude_Parents => True);
9679 Set_Interfaces (Derived_Type, Ifaces_List);
9681 -- If the derived type is the anonymous type created for
9682 -- a declaration whose parent has a constraint, propagate
9683 -- the interface list to the source type. This must be done
9684 -- prior to the completion of the analysis of the source type
9685 -- because the components in the extension may contain current
9686 -- instances whose legality depends on some ancestor.
9688 if Is_Itype (Derived_Type) then
9690 Def : constant Node_Id :=
9691 Associated_Node_For_Itype (Derived_Type);
9694 and then Nkind (Def) = N_Full_Type_Declaration
9697 (Defining_Identifier (Def), Ifaces_List);
9702 -- A type extension is automatically Ghost when one of its
9703 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9704 -- also inherited when the parent type is Ghost, but this is
9705 -- done in Build_Derived_Type as the mechanism also handles
9706 -- untagged derivations.
9708 if Implements_Ghost_Interface (Derived_Type) then
9709 Set_Is_Ghost_Entity (Derived_Type);
9715 -- STEP 4: Inherit components from the parent base and constrain them.
9716 -- Apply the second transformation described in point 6. above.
9718 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9719 or else not Has_Discriminants (Parent_Type)
9720 or else not Is_Constrained (Parent_Type)
9724 Constrs := Discriminant_Constraint (Parent_Type);
9729 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9731 -- STEP 5a: Copy the parent record declaration for untagged types
9733 Set_Has_Implicit_Dereference
9734 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9736 if not Is_Tagged then
9738 -- Discriminant_Constraint (Derived_Type) has been properly
9739 -- constructed. Save it and temporarily set it to Empty because we
9740 -- do not want the call to New_Copy_Tree below to mess this list.
9742 if Has_Discriminants (Derived_Type) then
9743 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9744 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9746 Save_Discr_Constr := No_Elist;
9749 -- Save the Etype field of Derived_Type. It is correctly set now,
9750 -- but the call to New_Copy tree may remap it to point to itself,
9751 -- which is not what we want. Ditto for the Next_Entity field.
9753 Save_Etype := Etype (Derived_Type);
9754 Save_Next_Entity := Next_Entity (Derived_Type);
9756 -- Assoc_List maps all stored discriminants in the Parent_Base to
9757 -- stored discriminants in the Derived_Type. It is fundamental that
9758 -- no types or itypes with discriminants other than the stored
9759 -- discriminants appear in the entities declared inside
9760 -- Derived_Type, since the back end cannot deal with it.
9764 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9765 Copy_Dimensions_Of_Components (Derived_Type);
9767 -- Restore the fields saved prior to the New_Copy_Tree call
9768 -- and compute the stored constraint.
9770 Set_Etype (Derived_Type, Save_Etype);
9771 Link_Entities (Derived_Type, Save_Next_Entity);
9773 if Has_Discriminants (Derived_Type) then
9774 Set_Discriminant_Constraint
9775 (Derived_Type, Save_Discr_Constr);
9776 Set_Stored_Constraint
9777 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9779 Replace_Discriminants (Derived_Type, New_Decl);
9782 -- Insert the new derived type declaration
9784 Rewrite (N, New_Decl);
9786 -- STEP 5b: Complete the processing for record extensions in generics
9788 -- There is no completion for record extensions declared in the
9789 -- parameter part of a generic, so we need to complete processing for
9790 -- these generic record extensions here. The Record_Type_Definition call
9791 -- will change the Ekind of the components from E_Void to E_Component.
9793 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9794 Record_Type_Definition (Empty, Derived_Type);
9796 -- STEP 5c: Process the record extension for non private tagged types
9798 elsif not Private_Extension then
9799 Expand_Record_Extension (Derived_Type, Type_Def);
9801 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9802 -- implemented interfaces if we are in expansion mode
9805 and then Has_Interfaces (Derived_Type)
9807 Add_Interface_Tag_Components (N, Derived_Type);
9810 -- Analyze the record extension
9812 Record_Type_Definition
9813 (Record_Extension_Part (Type_Def), Derived_Type);
9818 -- Nothing else to do if there is an error in the derivation.
9819 -- An unusual case: the full view may be derived from a type in an
9820 -- instance, when the partial view was used illegally as an actual
9821 -- in that instance, leading to a circular definition.
9823 if Etype (Derived_Type) = Any_Type
9824 or else Etype (Parent_Type) = Derived_Type
9829 -- Set delayed freeze and then derive subprograms, we need to do
9830 -- this in this order so that derived subprograms inherit the
9831 -- derived freeze if necessary.
9833 Set_Has_Delayed_Freeze (Derived_Type);
9835 if Derive_Subps then
9836 Derive_Subprograms (Parent_Type, Derived_Type);
9839 -- If we have a private extension which defines a constrained derived
9840 -- type mark as constrained here after we have derived subprograms. See
9841 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9843 if Private_Extension and then Inherit_Discrims then
9844 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9845 Set_Is_Constrained (Derived_Type, True);
9846 Set_Discriminant_Constraint (Derived_Type, Discs);
9848 elsif Is_Constrained (Parent_Type) then
9850 (Derived_Type, True);
9851 Set_Discriminant_Constraint
9852 (Derived_Type, Discriminant_Constraint (Parent_Type));
9856 -- Update the class-wide type, which shares the now-completed entity
9857 -- list with its specific type. In case of underlying record views,
9858 -- we do not generate the corresponding class wide entity.
9861 and then not Is_Underlying_Record_View (Derived_Type)
9864 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9866 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9869 Check_Function_Writable_Actuals (N);
9870 end Build_Derived_Record_Type;
9872 ------------------------
9873 -- Build_Derived_Type --
9874 ------------------------
9876 procedure Build_Derived_Type
9878 Parent_Type : Entity_Id;
9879 Derived_Type : Entity_Id;
9880 Is_Completion : Boolean;
9881 Derive_Subps : Boolean := True)
9883 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9886 -- Set common attributes
9888 if Ekind (Derived_Type) in Incomplete_Or_Private_Kind
9889 and then Ekind (Parent_Base) in Modular_Integer_Kind | Array_Kind
9891 Reinit_Field_To_Zero (Derived_Type, F_Stored_Constraint);
9894 Set_Scope (Derived_Type, Current_Scope);
9895 Set_Etype (Derived_Type, Parent_Base);
9896 Mutate_Ekind (Derived_Type, Ekind (Parent_Base));
9897 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9899 Set_Size_Info (Derived_Type, Parent_Type);
9900 Copy_RM_Size (To => Derived_Type, From => Parent_Type);
9902 Set_Is_Controlled_Active
9903 (Derived_Type, Is_Controlled_Active (Parent_Type));
9905 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9906 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9907 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9909 if Is_Tagged_Type (Derived_Type) then
9910 Set_No_Tagged_Streams_Pragma
9911 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9914 -- If the parent has primitive routines and may have not-seen-yet aspect
9915 -- specifications (e.g., a Pack pragma), then set the derived type link
9916 -- in order to later diagnose "early derivation" issues. If in different
9917 -- compilation units, then "early derivation" cannot be an issue (and we
9918 -- don't like interunit references that go in the opposite direction of
9919 -- semantic dependencies).
9921 if Has_Primitive_Operations (Parent_Type)
9922 and then Enclosing_Comp_Unit_Node (Parent_Type) =
9923 Enclosing_Comp_Unit_Node (Derived_Type)
9925 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9928 -- If the parent type is a private subtype, the convention on the base
9929 -- type may be set in the private part, and not propagated to the
9930 -- subtype until later, so we obtain the convention from the base type.
9932 Set_Convention (Derived_Type, Convention (Parent_Base));
9934 if Is_Tagged_Type (Derived_Type)
9935 and then Present (Class_Wide_Type (Derived_Type))
9937 Set_Convention (Class_Wide_Type (Derived_Type),
9938 Convention (Class_Wide_Type (Parent_Base)));
9941 -- Set SSO default for record or array type
9943 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9944 and then Is_Base_Type (Derived_Type)
9946 Set_Default_SSO (Derived_Type);
9949 -- A derived type inherits the Default_Initial_Condition pragma coming
9950 -- from any parent type within the derivation chain.
9952 if Has_DIC (Parent_Type) then
9953 Set_Has_Inherited_DIC (Derived_Type);
9956 -- A derived type inherits any class-wide invariants coming from a
9957 -- parent type or an interface. Note that the invariant procedure of
9958 -- the parent type should not be inherited because the derived type may
9959 -- define invariants of its own.
9961 if not Is_Interface (Derived_Type) then
9962 if Has_Inherited_Invariants (Parent_Type)
9963 or else Has_Inheritable_Invariants (Parent_Type)
9965 Set_Has_Inherited_Invariants (Derived_Type);
9967 elsif Is_Concurrent_Type (Derived_Type)
9968 or else Is_Tagged_Type (Derived_Type)
9973 Iface_Elmt : Elmt_Id;
9978 Ifaces_List => Ifaces,
9979 Exclude_Parents => True);
9981 if Present (Ifaces) then
9982 Iface_Elmt := First_Elmt (Ifaces);
9983 while Present (Iface_Elmt) loop
9984 Iface := Node (Iface_Elmt);
9986 if Has_Inheritable_Invariants (Iface) then
9987 Set_Has_Inherited_Invariants (Derived_Type);
9991 Next_Elmt (Iface_Elmt);
9998 -- We similarly inherit predicates. Note that for scalar derived types
9999 -- the predicate is inherited from the first subtype, and not from its
10000 -- (anonymous) base type.
10002 if Has_Predicates (Parent_Type)
10003 or else Has_Predicates (First_Subtype (Parent_Type))
10005 Set_Has_Predicates (Derived_Type);
10008 -- The derived type inherits representation clauses from the parent
10009 -- type, and from any interfaces.
10011 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
10014 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
10016 while Present (Iface) loop
10017 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
10022 -- If the parent type has delayed rep aspects, then mark the derived
10023 -- type as possibly inheriting a delayed rep aspect.
10025 if Has_Delayed_Rep_Aspects (Parent_Type) then
10026 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
10029 -- A derived type becomes Ghost when its parent type is also Ghost
10030 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
10031 -- directly inherited because the Ghost policy in effect may differ.
10033 if Is_Ghost_Entity (Parent_Type) then
10034 Set_Is_Ghost_Entity (Derived_Type);
10037 -- Type dependent processing
10039 case Ekind (Parent_Type) is
10040 when Numeric_Kind =>
10041 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
10044 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
10046 when Class_Wide_Kind
10050 Build_Derived_Record_Type
10051 (N, Parent_Type, Derived_Type, Derive_Subps);
10054 when Enumeration_Kind =>
10055 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
10057 when Access_Kind =>
10058 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
10060 when Incomplete_Or_Private_Kind =>
10061 Build_Derived_Private_Type
10062 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
10064 -- For discriminated types, the derivation includes deriving
10065 -- primitive operations. For others it is done below.
10067 if Is_Tagged_Type (Parent_Type)
10068 or else Has_Discriminants (Parent_Type)
10069 or else (Present (Full_View (Parent_Type))
10070 and then Has_Discriminants (Full_View (Parent_Type)))
10075 when Concurrent_Kind =>
10076 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
10079 raise Program_Error;
10082 -- Nothing more to do if some error occurred
10084 if Etype (Derived_Type) = Any_Type then
10088 -- If not already set, initialize the derived type's list of primitive
10089 -- operations to an empty element list.
10091 if not Present (Direct_Primitive_Operations (Derived_Type)) then
10092 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
10094 -- If Etype of the derived type is the base type (as opposed to
10095 -- a parent type) and doesn't have an associated list of primitive
10096 -- operations, then set the base type's primitive list to the
10097 -- derived type's list. The lists need to be shared in common
10098 -- between the two.
10100 if Etype (Derived_Type) = Base_Type (Derived_Type)
10102 not Present (Direct_Primitive_Operations (Etype (Derived_Type)))
10104 Set_Direct_Primitive_Operations
10105 (Etype (Derived_Type),
10106 Direct_Primitive_Operations (Derived_Type));
10110 -- Set delayed freeze and then derive subprograms, we need to do this
10111 -- in this order so that derived subprograms inherit the derived freeze
10114 Set_Has_Delayed_Freeze (Derived_Type);
10116 if Derive_Subps then
10117 Derive_Subprograms (Parent_Type, Derived_Type);
10120 Set_Has_Primitive_Operations
10121 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
10122 end Build_Derived_Type;
10124 -----------------------
10125 -- Build_Discriminal --
10126 -----------------------
10128 procedure Build_Discriminal (Discrim : Entity_Id) is
10129 D_Minal : Entity_Id;
10130 CR_Disc : Entity_Id;
10133 -- A discriminal has the same name as the discriminant
10135 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10137 Mutate_Ekind (D_Minal, E_In_Parameter);
10138 Set_Mechanism (D_Minal, Default_Mechanism);
10139 Set_Etype (D_Minal, Etype (Discrim));
10140 Set_Scope (D_Minal, Current_Scope);
10141 Set_Parent (D_Minal, Parent (Discrim));
10143 Set_Discriminal (Discrim, D_Minal);
10144 Set_Discriminal_Link (D_Minal, Discrim);
10146 -- For task types, build at once the discriminants of the corresponding
10147 -- record, which are needed if discriminants are used in entry defaults
10148 -- and in family bounds.
10150 if Is_Concurrent_Type (Current_Scope)
10152 Is_Limited_Type (Current_Scope)
10154 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
10156 Mutate_Ekind (CR_Disc, E_In_Parameter);
10157 Set_Mechanism (CR_Disc, Default_Mechanism);
10158 Set_Etype (CR_Disc, Etype (Discrim));
10159 Set_Scope (CR_Disc, Current_Scope);
10160 Set_Discriminal_Link (CR_Disc, Discrim);
10161 Set_CR_Discriminant (Discrim, CR_Disc);
10163 end Build_Discriminal;
10165 ------------------------------------
10166 -- Build_Discriminant_Constraints --
10167 ------------------------------------
10169 function Build_Discriminant_Constraints
10172 Derived_Def : Boolean := False) return Elist_Id
10174 C : constant Node_Id := Constraint (Def);
10175 Nb_Discr : constant Nat := Number_Discriminants (T);
10177 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
10178 -- Saves the expression corresponding to a given discriminant in T
10180 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
10181 -- Return the Position number within array Discr_Expr of a discriminant
10182 -- D within the discriminant list of the discriminated type T.
10184 procedure Process_Discriminant_Expression
10187 -- If this is a discriminant constraint on a partial view, do not
10188 -- generate an overflow check on the discriminant expression. The check
10189 -- will be generated when constraining the full view. Otherwise the
10190 -- backend creates duplicate symbols for the temporaries corresponding
10191 -- to the expressions to be checked, causing spurious assembler errors.
10197 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
10201 Disc := First_Discriminant (T);
10202 for J in Discr_Expr'Range loop
10207 Next_Discriminant (Disc);
10210 -- Note: Since this function is called on discriminants that are
10211 -- known to belong to the discriminated type, falling through the
10212 -- loop with no match signals an internal compiler error.
10214 raise Program_Error;
10217 -------------------------------------
10218 -- Process_Discriminant_Expression --
10219 -------------------------------------
10221 procedure Process_Discriminant_Expression
10225 BDT : constant Entity_Id := Base_Type (Etype (D));
10228 -- If this is a discriminant constraint on a partial view, do
10229 -- not generate an overflow on the discriminant expression. The
10230 -- check will be generated when constraining the full view.
10232 if Is_Private_Type (T)
10233 and then Present (Full_View (T))
10235 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
10237 Analyze_And_Resolve (Expr, BDT);
10239 end Process_Discriminant_Expression;
10241 -- Declarations local to Build_Discriminant_Constraints
10245 Elist : constant Elist_Id := New_Elmt_List;
10253 Discrim_Present : Boolean := False;
10255 -- Start of processing for Build_Discriminant_Constraints
10258 -- The following loop will process positional associations only.
10259 -- For a positional association, the (single) discriminant is
10260 -- implicitly specified by position, in textual order (RM 3.7.2).
10262 Discr := First_Discriminant (T);
10263 Constr := First (Constraints (C));
10264 for D in Discr_Expr'Range loop
10265 exit when Nkind (Constr) = N_Discriminant_Association;
10267 if No (Constr) then
10268 Error_Msg_N ("too few discriminants given in constraint", C);
10269 return New_Elmt_List;
10271 elsif Nkind (Constr) = N_Range
10272 or else (Nkind (Constr) = N_Attribute_Reference
10273 and then Attribute_Name (Constr) = Name_Range)
10276 ("a range is not a valid discriminant constraint", Constr);
10277 Discr_Expr (D) := Error;
10279 elsif Nkind (Constr) = N_Subtype_Indication then
10281 ("a subtype indication is not a valid discriminant constraint",
10283 Discr_Expr (D) := Error;
10286 Process_Discriminant_Expression (Constr, Discr);
10287 Discr_Expr (D) := Constr;
10290 Next_Discriminant (Discr);
10294 if No (Discr) and then Present (Constr) then
10295 Error_Msg_N ("too many discriminants given in constraint", Constr);
10296 return New_Elmt_List;
10299 -- Named associations can be given in any order, but if both positional
10300 -- and named associations are used in the same discriminant constraint,
10301 -- then positional associations must occur first, at their normal
10302 -- position. Hence once a named association is used, the rest of the
10303 -- discriminant constraint must use only named associations.
10305 while Present (Constr) loop
10307 -- Positional association forbidden after a named association
10309 if Nkind (Constr) /= N_Discriminant_Association then
10310 Error_Msg_N ("positional association follows named one", Constr);
10311 return New_Elmt_List;
10313 -- Otherwise it is a named association
10316 -- E records the type of the discriminants in the named
10317 -- association. All the discriminants specified in the same name
10318 -- association must have the same type.
10322 -- Search the list of discriminants in T to see if the simple name
10323 -- given in the constraint matches any of them.
10325 Id := First (Selector_Names (Constr));
10326 while Present (Id) loop
10329 -- If Original_Discriminant is present, we are processing a
10330 -- generic instantiation and this is an instance node. We need
10331 -- to find the name of the corresponding discriminant in the
10332 -- actual record type T and not the name of the discriminant in
10333 -- the generic formal. Example:
10336 -- type G (D : int) is private;
10338 -- subtype W is G (D => 1);
10340 -- type Rec (X : int) is record ... end record;
10341 -- package Q is new P (G => Rec);
10343 -- At the point of the instantiation, formal type G is Rec
10344 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10345 -- which really looks like "subtype W is Rec (D => 1);" at
10346 -- the point of instantiation, we want to find the discriminant
10347 -- that corresponds to D in Rec, i.e. X.
10349 if Present (Original_Discriminant (Id))
10350 and then In_Instance
10352 Discr := Find_Corresponding_Discriminant (Id, T);
10356 Discr := First_Discriminant (T);
10357 while Present (Discr) loop
10358 if Chars (Discr) = Chars (Id) then
10363 Next_Discriminant (Discr);
10367 Error_Msg_N ("& does not match any discriminant", Id);
10368 return New_Elmt_List;
10370 -- If the parent type is a generic formal, preserve the
10371 -- name of the discriminant for subsequent instances.
10372 -- see comment at the beginning of this if statement.
10374 elsif Is_Generic_Type (Root_Type (T)) then
10375 Set_Original_Discriminant (Id, Discr);
10379 Position := Pos_Of_Discr (T, Discr);
10381 if Present (Discr_Expr (Position)) then
10382 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10385 -- Each discriminant specified in the same named association
10386 -- must be associated with a separate copy of the
10387 -- corresponding expression.
10389 if Present (Next (Id)) then
10390 Expr := New_Copy_Tree (Expression (Constr));
10391 Set_Parent (Expr, Parent (Expression (Constr)));
10393 Expr := Expression (Constr);
10396 Discr_Expr (Position) := Expr;
10397 Process_Discriminant_Expression (Expr, Discr);
10400 -- A discriminant association with more than one discriminant
10401 -- name is only allowed if the named discriminants are all of
10402 -- the same type (RM 3.7.1(8)).
10405 E := Base_Type (Etype (Discr));
10407 elsif Base_Type (Etype (Discr)) /= E then
10409 ("all discriminants in an association " &
10410 "must have the same type", Id);
10420 -- A discriminant constraint must provide exactly one value for each
10421 -- discriminant of the type (RM 3.7.1(8)).
10423 for J in Discr_Expr'Range loop
10424 if No (Discr_Expr (J)) then
10425 Error_Msg_N ("too few discriminants given in constraint", C);
10426 return New_Elmt_List;
10430 -- Determine if there are discriminant expressions in the constraint
10432 for J in Discr_Expr'Range loop
10433 if Denotes_Discriminant
10434 (Discr_Expr (J), Check_Concurrent => True)
10436 Discrim_Present := True;
10441 -- Build an element list consisting of the expressions given in the
10442 -- discriminant constraint and apply the appropriate checks. The list
10443 -- is constructed after resolving any named discriminant associations
10444 -- and therefore the expressions appear in the textual order of the
10447 Discr := First_Discriminant (T);
10448 for J in Discr_Expr'Range loop
10449 if Discr_Expr (J) /= Error then
10450 Append_Elmt (Discr_Expr (J), Elist);
10452 -- If any of the discriminant constraints is given by a
10453 -- discriminant and we are in a derived type declaration we
10454 -- have a discriminant renaming. Establish link between new
10455 -- and old discriminant. The new discriminant has an implicit
10456 -- dereference if the old one does.
10458 if Denotes_Discriminant (Discr_Expr (J)) then
10459 if Derived_Def then
10461 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10464 Set_Corresponding_Discriminant (New_Discr, Discr);
10465 Set_Has_Implicit_Dereference (New_Discr,
10466 Has_Implicit_Dereference (Discr));
10470 -- Force the evaluation of non-discriminant expressions.
10471 -- If we have found a discriminant in the constraint 3.4(26)
10472 -- and 3.8(18) demand that no range checks are performed are
10473 -- after evaluation. If the constraint is for a component
10474 -- definition that has a per-object constraint, expressions are
10475 -- evaluated but not checked either. In all other cases perform
10479 if Discrim_Present then
10482 elsif Parent_Kind (Parent (Def)) = N_Component_Declaration
10483 and then Has_Per_Object_Constraint
10484 (Defining_Identifier (Parent (Parent (Def))))
10488 elsif Is_Access_Type (Etype (Discr)) then
10489 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10492 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10495 -- If the value of the discriminant may be visible in
10496 -- another unit or child unit, create an external name
10497 -- for it. We use the name of the object or component
10498 -- that carries the discriminated subtype. The code
10499 -- below may generate external symbols for the discriminant
10500 -- expression when not strictly needed, which is harmless.
10503 and then Comes_From_Source (Def)
10504 and then not Is_Subprogram (Current_Scope)
10507 Id : Entity_Id := Empty;
10509 if Nkind (Parent (Def)) = N_Object_Declaration then
10510 Id := Defining_Identifier (Parent (Def));
10512 elsif Nkind (Parent (Def)) = N_Component_Definition
10514 Nkind (Parent (Parent (Def)))
10515 = N_Component_Declaration
10517 Id := Defining_Identifier (Parent (Parent (Def)));
10520 if Present (Id) then
10524 Discr_Number => J);
10526 Force_Evaluation (Discr_Expr (J));
10530 Force_Evaluation (Discr_Expr (J));
10534 -- Check that the designated type of an access discriminant's
10535 -- expression is not a class-wide type unless the discriminant's
10536 -- designated type is also class-wide.
10538 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10539 and then not Is_Class_Wide_Type
10540 (Designated_Type (Etype (Discr)))
10541 and then Etype (Discr_Expr (J)) /= Any_Type
10542 and then Is_Class_Wide_Type
10543 (Designated_Type (Etype (Discr_Expr (J))))
10545 Wrong_Type (Discr_Expr (J), Etype (Discr));
10547 elsif Is_Access_Type (Etype (Discr))
10548 and then not Is_Access_Constant (Etype (Discr))
10549 and then Is_Access_Type (Etype (Discr_Expr (J)))
10550 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10553 ("constraint for discriminant& must be access to variable",
10558 Next_Discriminant (Discr);
10562 end Build_Discriminant_Constraints;
10564 ---------------------------------
10565 -- Build_Discriminated_Subtype --
10566 ---------------------------------
10568 procedure Build_Discriminated_Subtype
10570 Def_Id : Entity_Id;
10572 Related_Nod : Node_Id;
10573 For_Access : Boolean := False)
10575 Has_Discrs : constant Boolean := Has_Discriminants (T);
10576 Constrained : constant Boolean :=
10578 and then not Is_Empty_Elmt_List (Elist)
10579 and then not Is_Class_Wide_Type (T))
10580 or else Is_Constrained (T);
10583 if Ekind (T) = E_Record_Type then
10584 Mutate_Ekind (Def_Id, E_Record_Subtype);
10586 -- Inherit preelaboration flag from base, for types for which it
10587 -- may have been set: records, private types, protected types.
10589 Set_Known_To_Have_Preelab_Init
10590 (Def_Id, Known_To_Have_Preelab_Init (T));
10592 elsif Ekind (T) = E_Task_Type then
10593 Mutate_Ekind (Def_Id, E_Task_Subtype);
10595 elsif Ekind (T) = E_Protected_Type then
10596 Mutate_Ekind (Def_Id, E_Protected_Subtype);
10597 Set_Known_To_Have_Preelab_Init
10598 (Def_Id, Known_To_Have_Preelab_Init (T));
10600 elsif Is_Private_Type (T) then
10601 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10602 Set_Known_To_Have_Preelab_Init
10603 (Def_Id, Known_To_Have_Preelab_Init (T));
10605 -- Private subtypes may have private dependents
10607 Set_Private_Dependents (Def_Id, New_Elmt_List);
10609 elsif Is_Class_Wide_Type (T) then
10610 Mutate_Ekind (Def_Id, E_Class_Wide_Subtype);
10613 -- Incomplete type. Attach subtype to list of dependents, to be
10614 -- completed with full view of parent type, unless is it the
10615 -- designated subtype of a record component within an init_proc.
10616 -- This last case arises for a component of an access type whose
10617 -- designated type is incomplete (e.g. a Taft Amendment type).
10618 -- The designated subtype is within an inner scope, and needs no
10619 -- elaboration, because only the access type is needed in the
10620 -- initialization procedure.
10622 if Ekind (T) = E_Incomplete_Type then
10623 Mutate_Ekind (Def_Id, E_Incomplete_Subtype);
10625 Mutate_Ekind (Def_Id, Ekind (T));
10628 if For_Access and then Within_Init_Proc then
10631 Append_Elmt (Def_Id, Private_Dependents (T));
10635 Set_Etype (Def_Id, T);
10636 Reinit_Size_Align (Def_Id);
10637 Set_Has_Discriminants (Def_Id, Has_Discrs);
10638 Set_Is_Constrained (Def_Id, Constrained);
10640 Set_First_Entity (Def_Id, First_Entity (T));
10641 Set_Last_Entity (Def_Id, Last_Entity (T));
10642 Set_Has_Implicit_Dereference
10643 (Def_Id, Has_Implicit_Dereference (T));
10644 Set_Has_Pragma_Unreferenced_Objects
10645 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10647 -- If the subtype is the completion of a private declaration, there may
10648 -- have been representation clauses for the partial view, and they must
10649 -- be preserved. Build_Derived_Type chains the inherited clauses with
10650 -- the ones appearing on the extension. If this comes from a subtype
10651 -- declaration, all clauses are inherited.
10653 if No (First_Rep_Item (Def_Id)) then
10654 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10657 if Is_Tagged_Type (T) then
10658 Set_Is_Tagged_Type (Def_Id);
10659 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10660 Make_Class_Wide_Type (Def_Id);
10663 Set_Stored_Constraint (Def_Id, No_Elist);
10666 Set_Discriminant_Constraint (Def_Id, Elist);
10667 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10670 if Is_Tagged_Type (T) then
10672 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10673 -- concurrent record type (which has the list of primitive
10676 if Ada_Version >= Ada_2005
10677 and then Is_Concurrent_Type (T)
10679 Set_Corresponding_Record_Type (Def_Id,
10680 Corresponding_Record_Type (T));
10682 Set_Direct_Primitive_Operations (Def_Id,
10683 Direct_Primitive_Operations (T));
10686 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10689 -- Subtypes introduced by component declarations do not need to be
10690 -- marked as delayed, and do not get freeze nodes, because the semantics
10691 -- verifies that the parents of the subtypes are frozen before the
10692 -- enclosing record is frozen.
10694 if not Is_Type (Scope (Def_Id)) then
10695 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10697 if Is_Private_Type (T)
10698 and then Present (Full_View (T))
10700 Conditional_Delay (Def_Id, Full_View (T));
10702 Conditional_Delay (Def_Id, T);
10706 if Is_Record_Type (T) then
10707 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10710 and then not Is_Empty_Elmt_List (Elist)
10711 and then not For_Access
10713 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10715 elsif not Is_Private_Type (T) then
10716 Set_Cloned_Subtype (Def_Id, T);
10719 end Build_Discriminated_Subtype;
10721 ---------------------------
10722 -- Build_Itype_Reference --
10723 ---------------------------
10725 procedure Build_Itype_Reference
10729 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10732 -- Itype references are only created for use by the back-end
10734 if Inside_A_Generic then
10737 Set_Itype (IR, Ityp);
10739 -- If Nod is a library unit entity, then Insert_After won't work,
10740 -- because Nod is not a member of any list. Therefore, we use
10741 -- Add_Global_Declaration in this case. This can happen if we have a
10742 -- build-in-place library function, child unit or not.
10744 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10745 or else (Nkind (Nod) in
10746 N_Defining_Program_Unit_Name | N_Subprogram_Declaration
10747 and then Is_Compilation_Unit (Defining_Entity (Nod)))
10749 Add_Global_Declaration (IR);
10751 Insert_After (Nod, IR);
10754 end Build_Itype_Reference;
10756 ------------------------
10757 -- Build_Scalar_Bound --
10758 ------------------------
10760 function Build_Scalar_Bound
10763 Der_T : Entity_Id) return Node_Id
10765 New_Bound : Entity_Id;
10768 -- Note: not clear why this is needed, how can the original bound
10769 -- be unanalyzed at this point? and if it is, what business do we
10770 -- have messing around with it? and why is the base type of the
10771 -- parent type the right type for the resolution. It probably is
10772 -- not. It is OK for the new bound we are creating, but not for
10773 -- the old one??? Still if it never happens, no problem.
10775 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10777 if Nkind (Bound) in N_Integer_Literal | N_Real_Literal then
10778 New_Bound := New_Copy (Bound);
10779 Set_Etype (New_Bound, Der_T);
10780 Set_Analyzed (New_Bound);
10782 elsif Is_Entity_Name (Bound) then
10783 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10785 -- The following is almost certainly wrong. What business do we have
10786 -- relocating a node (Bound) that is presumably still attached to
10787 -- the tree elsewhere???
10790 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10793 Set_Etype (New_Bound, Der_T);
10795 end Build_Scalar_Bound;
10797 -------------------------------
10798 -- Check_Abstract_Overriding --
10799 -------------------------------
10801 procedure Check_Abstract_Overriding (T : Entity_Id) is
10802 Alias_Subp : Entity_Id;
10804 Op_List : Elist_Id;
10806 Type_Def : Node_Id;
10808 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10809 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10810 -- which has pragma Implemented already set. Check whether Subp's entity
10811 -- kind conforms to the implementation kind of the overridden routine.
10813 procedure Check_Pragma_Implemented
10815 Iface_Subp : Entity_Id);
10816 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10817 -- Iface_Subp and both entities have pragma Implemented already set on
10818 -- them. Check whether the two implementation kinds are conforming.
10820 procedure Inherit_Pragma_Implemented
10822 Iface_Subp : Entity_Id);
10823 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10824 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10825 -- Propagate the implementation kind of Iface_Subp to Subp.
10827 ------------------------------
10828 -- Check_Pragma_Implemented --
10829 ------------------------------
10831 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10832 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10833 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10834 Subp_Alias : constant Entity_Id := Alias (Subp);
10835 Contr_Typ : Entity_Id;
10836 Impl_Subp : Entity_Id;
10839 -- Subp must have an alias since it is a hidden entity used to link
10840 -- an interface subprogram to its overriding counterpart.
10842 pragma Assert (Present (Subp_Alias));
10844 -- Handle aliases to synchronized wrappers
10846 Impl_Subp := Subp_Alias;
10848 if Is_Primitive_Wrapper (Impl_Subp) then
10849 Impl_Subp := Wrapped_Entity (Impl_Subp);
10852 -- Extract the type of the controlling formal
10854 Contr_Typ := Etype (First_Formal (Subp_Alias));
10856 if Is_Concurrent_Record_Type (Contr_Typ) then
10857 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10860 -- An interface subprogram whose implementation kind is By_Entry must
10861 -- be implemented by an entry.
10863 if Impl_Kind = Name_By_Entry
10864 and then Ekind (Impl_Subp) /= E_Entry
10866 Error_Msg_Node_2 := Iface_Alias;
10868 ("type & must implement abstract subprogram & with an entry",
10869 Subp_Alias, Contr_Typ);
10871 elsif Impl_Kind = Name_By_Protected_Procedure then
10873 -- An interface subprogram whose implementation kind is By_
10874 -- Protected_Procedure cannot be implemented by a primitive
10875 -- procedure of a task type.
10877 if Ekind (Contr_Typ) /= E_Protected_Type then
10878 Error_Msg_Node_2 := Contr_Typ;
10880 ("interface subprogram & cannot be implemented by a "
10881 & "primitive procedure of task type &",
10882 Subp_Alias, Iface_Alias);
10884 -- An interface subprogram whose implementation kind is By_
10885 -- Protected_Procedure must be implemented by a procedure.
10887 elsif Ekind (Impl_Subp) /= E_Procedure then
10888 Error_Msg_Node_2 := Iface_Alias;
10890 ("type & must implement abstract subprogram & with a "
10891 & "procedure", Subp_Alias, Contr_Typ);
10893 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10894 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10896 Error_Msg_Name_1 := Impl_Kind;
10898 ("overriding operation& must have synchronization%",
10902 -- If primitive has Optional synchronization, overriding operation
10903 -- must match if it has an explicit synchronization.
10905 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10906 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10908 Error_Msg_Name_1 := Impl_Kind;
10910 ("overriding operation& must have synchronization%", Subp_Alias);
10912 end Check_Pragma_Implemented;
10914 ------------------------------
10915 -- Check_Pragma_Implemented --
10916 ------------------------------
10918 procedure Check_Pragma_Implemented
10920 Iface_Subp : Entity_Id)
10922 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10923 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10926 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10927 -- and overriding subprogram are different. In general this is an
10928 -- error except when the implementation kind of the overridden
10929 -- subprograms is By_Any or Optional.
10931 if Iface_Kind /= Subp_Kind
10932 and then Iface_Kind /= Name_By_Any
10933 and then Iface_Kind /= Name_Optional
10935 if Iface_Kind = Name_By_Entry then
10937 ("incompatible implementation kind, overridden subprogram " &
10938 "is marked By_Entry", Subp);
10941 ("incompatible implementation kind, overridden subprogram " &
10942 "is marked By_Protected_Procedure", Subp);
10945 end Check_Pragma_Implemented;
10947 --------------------------------
10948 -- Inherit_Pragma_Implemented --
10949 --------------------------------
10951 procedure Inherit_Pragma_Implemented
10953 Iface_Subp : Entity_Id)
10955 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10956 Loc : constant Source_Ptr := Sloc (Subp);
10957 Impl_Prag : Node_Id;
10960 -- Since the implementation kind is stored as a representation item
10961 -- rather than a flag, create a pragma node.
10965 Chars => Name_Implemented,
10966 Pragma_Argument_Associations => New_List (
10967 Make_Pragma_Argument_Association (Loc,
10968 Expression => New_Occurrence_Of (Subp, Loc)),
10970 Make_Pragma_Argument_Association (Loc,
10971 Expression => Make_Identifier (Loc, Iface_Kind))));
10973 -- The pragma doesn't need to be analyzed because it is internally
10974 -- built. It is safe to directly register it as a rep item since we
10975 -- are only interested in the characters of the implementation kind.
10977 Record_Rep_Item (Subp, Impl_Prag);
10978 end Inherit_Pragma_Implemented;
10980 -- Start of processing for Check_Abstract_Overriding
10983 Op_List := Primitive_Operations (T);
10985 -- Loop to check primitive operations
10987 Elmt := First_Elmt (Op_List);
10988 while Present (Elmt) loop
10989 Subp := Node (Elmt);
10990 Alias_Subp := Alias (Subp);
10992 -- Inherited subprograms are identified by the fact that they do not
10993 -- come from source, and the associated source location is the
10994 -- location of the first subtype of the derived type.
10996 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10997 -- subprograms that "require overriding".
10999 -- Special exception, do not complain about failure to override the
11000 -- stream routines _Input and _Output, as well as the primitive
11001 -- operations used in dispatching selects since we always provide
11002 -- automatic overridings for these subprograms.
11004 -- The partial view of T may have been a private extension, for
11005 -- which inherited functions dispatching on result are abstract.
11006 -- If the full view is a null extension, there is no need for
11007 -- overriding in Ada 2005, but wrappers need to be built for them
11008 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
11010 if Is_Null_Extension (T)
11011 and then Has_Controlling_Result (Subp)
11012 and then Ada_Version >= Ada_2005
11013 and then Present (Alias_Subp)
11014 and then not Comes_From_Source (Subp)
11015 and then not Is_Abstract_Subprogram (Alias_Subp)
11016 and then not Is_Access_Type (Etype (Subp))
11020 -- Ada 2005 (AI-251): Internal entities of interfaces need no
11021 -- processing because this check is done with the aliased
11024 elsif Present (Interface_Alias (Subp)) then
11027 -- AI12-0042: Test for rule in 7.3.2(6.1/4), that requires overriding
11028 -- of a visible private primitive inherited from an ancestor with
11029 -- the aspect Type_Invariant'Class, unless the inherited primitive
11032 elsif not Is_Abstract_Subprogram (Subp)
11033 and then not Comes_From_Source (Subp) -- An inherited subprogram
11034 and then Requires_Overriding (Subp)
11035 and then Present (Alias_Subp)
11036 and then Has_Invariants (Etype (T))
11037 and then Present (Get_Pragma (Etype (T), Pragma_Invariant))
11038 and then Class_Present (Get_Pragma (Etype (T), Pragma_Invariant))
11039 and then Is_Private_Primitive (Alias_Subp)
11042 ("inherited private primitive & must be overridden", T, Subp);
11044 ("\because ancestor type has 'Type_'Invariant''Class " &
11045 "(RM 7.3.2(6.1))", T);
11047 elsif (Is_Abstract_Subprogram (Subp)
11048 or else Requires_Overriding (Subp)
11050 (Has_Controlling_Result (Subp)
11051 and then Present (Alias_Subp)
11052 and then not Comes_From_Source (Subp)
11053 and then Sloc (Subp) = Sloc (First_Subtype (T))))
11054 and then not Is_TSS (Subp, TSS_Stream_Input)
11055 and then not Is_TSS (Subp, TSS_Stream_Output)
11056 and then not Is_Abstract_Type (T)
11057 and then not Is_Predefined_Interface_Primitive (Subp)
11059 -- Ada 2005 (AI-251): Do not consider hidden entities associated
11060 -- with abstract interface types because the check will be done
11061 -- with the aliased entity (otherwise we generate a duplicated
11064 and then not Present (Interface_Alias (Subp))
11066 if Present (Alias_Subp) then
11068 -- Only perform the check for a derived subprogram when the
11069 -- type has an explicit record extension. This avoids incorrect
11070 -- flagging of abstract subprograms for the case of a type
11071 -- without an extension that is derived from a formal type
11072 -- with a tagged actual (can occur within a private part).
11074 -- Ada 2005 (AI-391): In the case of an inherited function with
11075 -- a controlling result of the type, the rule does not apply if
11076 -- the type is a null extension (unless the parent function
11077 -- itself is abstract, in which case the function must still be
11078 -- be overridden). The expander will generate an overriding
11079 -- wrapper function calling the parent subprogram (see
11080 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
11082 Type_Def := Type_Definition (Parent (T));
11084 if Nkind (Type_Def) = N_Derived_Type_Definition
11085 and then Present (Record_Extension_Part (Type_Def))
11087 (Ada_Version < Ada_2005
11088 or else not Is_Null_Extension (T)
11089 or else Ekind (Subp) = E_Procedure
11090 or else not Has_Controlling_Result (Subp)
11091 or else Is_Abstract_Subprogram (Alias_Subp)
11092 or else Requires_Overriding (Subp)
11093 or else Is_Access_Type (Etype (Subp)))
11095 -- Avoid reporting error in case of abstract predefined
11096 -- primitive inherited from interface type because the
11097 -- body of internally generated predefined primitives
11098 -- of tagged types are generated later by Freeze_Type
11100 if Is_Interface (Root_Type (T))
11101 and then Is_Abstract_Subprogram (Subp)
11102 and then Is_Predefined_Dispatching_Operation (Subp)
11103 and then not Comes_From_Source (Ultimate_Alias (Subp))
11107 -- A null extension is not obliged to override an inherited
11108 -- procedure subject to pragma Extensions_Visible with value
11109 -- False and at least one controlling OUT parameter
11110 -- (SPARK RM 6.1.7(6)).
11112 elsif Is_Null_Extension (T)
11113 and then Is_EVF_Procedure (Subp)
11117 -- Subprogram renamings cannot be overridden
11119 elsif Comes_From_Source (Subp)
11120 and then Present (Alias (Subp))
11124 -- Skip reporting the error on Ada 2022 only subprograms
11125 -- that require overriding if we are not in Ada 2022 mode.
11127 elsif Ada_Version < Ada_2022
11128 and then Requires_Overriding (Subp)
11129 and then Is_Ada_2022_Only (Ultimate_Alias (Subp))
11135 ("type must be declared abstract or & overridden",
11138 -- Traverse the whole chain of aliased subprograms to
11139 -- complete the error notification. This is especially
11140 -- useful for traceability of the chain of entities when
11141 -- the subprogram corresponds with an interface
11142 -- subprogram (which may be defined in another package).
11144 if Present (Alias_Subp) then
11150 while Present (Alias (E)) loop
11152 -- Avoid reporting redundant errors on entities
11153 -- inherited from interfaces
11155 if Sloc (E) /= Sloc (T) then
11156 Error_Msg_Sloc := Sloc (E);
11158 ("\& has been inherited #", T, Subp);
11164 Error_Msg_Sloc := Sloc (E);
11166 -- AI05-0068: report if there is an overriding
11167 -- non-abstract subprogram that is invisible.
11170 and then not Is_Abstract_Subprogram (E)
11173 ("\& subprogram# is not visible",
11176 -- Clarify the case where a non-null extension must
11177 -- override inherited procedure subject to pragma
11178 -- Extensions_Visible with value False and at least
11179 -- one controlling OUT param.
11181 elsif Is_EVF_Procedure (E) then
11183 ("\& # is subject to Extensions_Visible False",
11188 ("\& has been inherited from subprogram #",
11195 -- Ada 2005 (AI-345): Protected or task type implementing
11196 -- abstract interfaces.
11198 elsif Is_Concurrent_Record_Type (T)
11199 and then Present (Interfaces (T))
11201 -- There is no need to check here RM 9.4(11.9/3) since we
11202 -- are processing the corresponding record type and the
11203 -- mode of the overriding subprograms was verified by
11204 -- Check_Conformance when the corresponding concurrent
11205 -- type declaration was analyzed.
11208 ("interface subprogram & must be overridden", T, Subp);
11210 -- Examine primitive operations of synchronized type to find
11211 -- homonyms that have the wrong profile.
11217 Prim := First_Entity (Corresponding_Concurrent_Type (T));
11218 while Present (Prim) loop
11219 if Chars (Prim) = Chars (Subp) then
11221 ("profile is not type conformant with prefixed "
11222 & "view profile of inherited operation&",
11226 Next_Entity (Prim);
11232 Error_Msg_Node_2 := T;
11234 ("abstract subprogram& not allowed for type&", Subp);
11236 -- Also post unconditional warning on the type (unconditional
11237 -- so that if there are more than one of these cases, we get
11238 -- them all, and not just the first one).
11240 Error_Msg_Node_2 := Subp;
11241 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
11244 -- A subprogram subject to pragma Extensions_Visible with value
11245 -- "True" cannot override a subprogram subject to the same pragma
11246 -- with value "False" (SPARK RM 6.1.7(5)).
11248 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
11249 and then Present (Overridden_Operation (Subp))
11250 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
11251 Extensions_Visible_False
11253 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
11255 ("subprogram & with Extensions_Visible True cannot override "
11256 & "subprogram # with Extensions_Visible False", Subp);
11259 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
11261 -- Subp is an expander-generated procedure which maps an interface
11262 -- alias to a protected wrapper. The interface alias is flagged by
11263 -- pragma Implemented. Ensure that Subp is a procedure when the
11264 -- implementation kind is By_Protected_Procedure or an entry when
11267 if Ada_Version >= Ada_2012
11268 and then Is_Hidden (Subp)
11269 and then Present (Interface_Alias (Subp))
11270 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
11272 Check_Pragma_Implemented (Subp);
11275 -- Subp is an interface primitive which overrides another interface
11276 -- primitive marked with pragma Implemented.
11278 if Ada_Version >= Ada_2012
11279 and then Present (Overridden_Operation (Subp))
11280 and then Has_Rep_Pragma
11281 (Overridden_Operation (Subp), Name_Implemented)
11283 -- If the overriding routine is also marked by Implemented, check
11284 -- that the two implementation kinds are conforming.
11286 if Has_Rep_Pragma (Subp, Name_Implemented) then
11287 Check_Pragma_Implemented
11289 Iface_Subp => Overridden_Operation (Subp));
11291 -- Otherwise the overriding routine inherits the implementation
11292 -- kind from the overridden subprogram.
11295 Inherit_Pragma_Implemented
11297 Iface_Subp => Overridden_Operation (Subp));
11301 -- Ada 2005 (AI95-0414) and Ada 2022 (AI12-0269): Diagnose failure to
11302 -- match No_Return in parent, but do it unconditionally in Ada 95 too
11303 -- for procedures, since this is our pragma.
11305 if Present (Overridden_Operation (Subp))
11306 and then No_Return (Overridden_Operation (Subp))
11309 -- If the subprogram is a renaming, check that the renamed
11310 -- subprogram is No_Return.
11312 if Present (Renamed_Or_Alias (Subp)) then
11313 if not No_Return (Renamed_Or_Alias (Subp)) then
11314 Error_Msg_NE ("subprogram & must be No_Return",
11316 Renamed_Or_Alias (Subp));
11317 Error_Msg_N ("\since renaming & overrides No_Return "
11318 & "subprogram (RM 6.5.1(6/2))",
11322 -- Make sure that the subprogram itself is No_Return.
11324 elsif not No_Return (Subp) then
11325 Error_Msg_N ("overriding subprogram & must be No_Return", Subp);
11327 ("\since overridden subprogram is No_Return (RM 6.5.1(6/2))",
11332 -- If the operation is a wrapper for a synchronized primitive, it
11333 -- may be called indirectly through a dispatching select. We assume
11334 -- that it will be referenced elsewhere indirectly, and suppress
11335 -- warnings about an unused entity.
11337 if Is_Primitive_Wrapper (Subp)
11338 and then Present (Wrapped_Entity (Subp))
11340 Set_Referenced (Wrapped_Entity (Subp));
11345 end Check_Abstract_Overriding;
11347 ------------------------------------------------
11348 -- Check_Access_Discriminant_Requires_Limited --
11349 ------------------------------------------------
11351 procedure Check_Access_Discriminant_Requires_Limited
11356 -- A discriminant_specification for an access discriminant shall appear
11357 -- only in the declaration for a task or protected type, or for a type
11358 -- with the reserved word 'limited' in its definition or in one of its
11359 -- ancestors (RM 3.7(10)).
11361 -- AI-0063: The proper condition is that type must be immutably limited,
11362 -- or else be a partial view.
11364 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11365 if Is_Limited_View (Current_Scope)
11367 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11368 and then Limited_Present (Parent (Current_Scope)))
11374 ("access discriminants allowed only for limited types", Loc);
11377 end Check_Access_Discriminant_Requires_Limited;
11379 -----------------------------------
11380 -- Check_Aliased_Component_Types --
11381 -----------------------------------
11383 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11387 -- ??? Also need to check components of record extensions, but not
11388 -- components of protected types (which are always limited).
11390 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11391 -- types to be unconstrained. This is safe because it is illegal to
11392 -- create access subtypes to such types with explicit discriminant
11395 if not Is_Limited_Type (T) then
11396 if Ekind (T) = E_Record_Type then
11397 C := First_Component (T);
11398 while Present (C) loop
11400 and then Has_Discriminants (Etype (C))
11401 and then not Is_Constrained (Etype (C))
11402 and then not In_Instance_Body
11403 and then Ada_Version < Ada_2005
11406 ("aliased component must be constrained (RM 3.6(11))",
11410 Next_Component (C);
11413 elsif Ekind (T) = E_Array_Type then
11414 if Has_Aliased_Components (T)
11415 and then Has_Discriminants (Component_Type (T))
11416 and then not Is_Constrained (Component_Type (T))
11417 and then not In_Instance_Body
11418 and then Ada_Version < Ada_2005
11421 ("aliased component type must be constrained (RM 3.6(11))",
11426 end Check_Aliased_Component_Types;
11428 --------------------------------------
11429 -- Check_Anonymous_Access_Component --
11430 --------------------------------------
11432 procedure Check_Anonymous_Access_Component
11433 (Typ_Decl : Node_Id;
11436 Comp_Def : Node_Id;
11437 Access_Def : Node_Id)
11439 Loc : constant Source_Ptr := Sloc (Comp_Def);
11440 Anon_Access : Entity_Id;
11443 Type_Def : Node_Id;
11445 procedure Build_Incomplete_Type_Declaration;
11446 -- If the record type contains components that include an access to the
11447 -- current record, then create an incomplete type declaration for the
11448 -- record, to be used as the designated type of the anonymous access.
11449 -- This is done only once, and only if there is no previous partial
11450 -- view of the type.
11452 function Designates_T (Subt : Node_Id) return Boolean;
11453 -- Check whether a node designates the enclosing record type, or 'Class
11456 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11457 -- Check whether an access definition includes a reference to
11458 -- the enclosing record type. The reference can be a subtype mark
11459 -- in the access definition itself, a 'Class attribute reference, or
11460 -- recursively a reference appearing in a parameter specification
11461 -- or result definition of an access_to_subprogram definition.
11463 --------------------------------------
11464 -- Build_Incomplete_Type_Declaration --
11465 --------------------------------------
11467 procedure Build_Incomplete_Type_Declaration is
11472 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11473 -- it's "is new ... with record" or else "is tagged record ...".
11475 Typ_Def : constant Node_Id :=
11476 (if Nkind (Typ_Decl) = N_Full_Type_Declaration
11477 then Type_Definition (Typ_Decl) else Empty);
11478 Is_Tagged : constant Boolean :=
11481 ((Nkind (Typ_Def) = N_Derived_Type_Definition
11483 Present (Record_Extension_Part (Typ_Def)))
11485 (Nkind (Typ_Def) = N_Record_Definition
11486 and then Tagged_Present (Typ_Def)));
11489 -- If there is a previous partial view, no need to create a new one
11490 -- If the partial view, given by Prev, is incomplete, If Prev is
11491 -- a private declaration, full declaration is flagged accordingly.
11493 if Prev /= Typ then
11495 Make_Class_Wide_Type (Prev);
11496 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11497 Set_Etype (Class_Wide_Type (Typ), Typ);
11502 elsif Has_Private_Declaration (Typ) then
11504 -- If we refer to T'Class inside T, and T is the completion of a
11505 -- private type, then make sure the class-wide type exists.
11508 Make_Class_Wide_Type (Typ);
11513 -- If there was a previous anonymous access type, the incomplete
11514 -- type declaration will have been created already.
11516 elsif Present (Current_Entity (Typ))
11517 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11518 and then Full_View (Current_Entity (Typ)) = Typ
11521 and then Comes_From_Source (Current_Entity (Typ))
11522 and then not Is_Tagged_Type (Current_Entity (Typ))
11524 Make_Class_Wide_Type (Typ);
11526 ("incomplete view of tagged type should be declared tagged??",
11527 Parent (Current_Entity (Typ)));
11532 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11533 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11535 -- Type has already been inserted into the current scope. Remove
11536 -- it, and add incomplete declaration for type, so that subsequent
11537 -- anonymous access types can use it. The entity is unchained from
11538 -- the homonym list and from immediate visibility. After analysis,
11539 -- the entity in the incomplete declaration becomes immediately
11540 -- visible in the record declaration that follows.
11542 H := Current_Entity (Typ);
11545 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11548 and then Homonym (H) /= Typ
11550 H := Homonym (Typ);
11553 Set_Homonym (H, Homonym (Typ));
11556 Insert_Before (Typ_Decl, Decl);
11558 Set_Full_View (Inc_T, Typ);
11562 -- Create a common class-wide type for both views, and set the
11563 -- Etype of the class-wide type to the full view.
11565 Make_Class_Wide_Type (Inc_T);
11566 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11567 Set_Etype (Class_Wide_Type (Typ), Typ);
11570 end Build_Incomplete_Type_Declaration;
11576 function Designates_T (Subt : Node_Id) return Boolean is
11577 Type_Id : constant Name_Id := Chars (Typ);
11579 function Names_T (Nam : Node_Id) return Boolean;
11580 -- The record type has not been introduced in the current scope
11581 -- yet, so we must examine the name of the type itself, either
11582 -- an identifier T, or an expanded name of the form P.T, where
11583 -- P denotes the current scope.
11589 function Names_T (Nam : Node_Id) return Boolean is
11591 if Nkind (Nam) = N_Identifier then
11592 return Chars (Nam) = Type_Id;
11594 elsif Nkind (Nam) = N_Selected_Component then
11595 if Chars (Selector_Name (Nam)) = Type_Id then
11596 if Nkind (Prefix (Nam)) = N_Identifier then
11597 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11599 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11600 return Chars (Selector_Name (Prefix (Nam))) =
11601 Chars (Current_Scope);
11615 -- Start of processing for Designates_T
11618 if Nkind (Subt) = N_Identifier then
11619 return Chars (Subt) = Type_Id;
11621 -- Reference can be through an expanded name which has not been
11622 -- analyzed yet, and which designates enclosing scopes.
11624 elsif Nkind (Subt) = N_Selected_Component then
11625 if Names_T (Subt) then
11628 -- Otherwise it must denote an entity that is already visible.
11629 -- The access definition may name a subtype of the enclosing
11630 -- type, if there is a previous incomplete declaration for it.
11633 Find_Selected_Component (Subt);
11635 Is_Entity_Name (Subt)
11636 and then Scope (Entity (Subt)) = Current_Scope
11638 (Chars (Base_Type (Entity (Subt))) = Type_Id
11640 (Is_Class_Wide_Type (Entity (Subt))
11642 Chars (Etype (Base_Type (Entity (Subt)))) =
11646 -- A reference to the current type may appear as the prefix of
11647 -- a 'Class attribute.
11649 elsif Nkind (Subt) = N_Attribute_Reference
11650 and then Attribute_Name (Subt) = Name_Class
11652 return Names_T (Prefix (Subt));
11663 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11664 Param_Spec : Node_Id;
11666 Acc_Subprg : constant Node_Id :=
11667 Access_To_Subprogram_Definition (Acc_Def);
11670 if No (Acc_Subprg) then
11671 return Designates_T (Subtype_Mark (Acc_Def));
11674 -- Component is an access_to_subprogram: examine its formals,
11675 -- and result definition in the case of an access_to_function.
11677 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11678 while Present (Param_Spec) loop
11679 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11680 and then Mentions_T (Parameter_Type (Param_Spec))
11684 elsif Designates_T (Parameter_Type (Param_Spec)) then
11691 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11692 if Nkind (Result_Definition (Acc_Subprg)) =
11693 N_Access_Definition
11695 return Mentions_T (Result_Definition (Acc_Subprg));
11697 return Designates_T (Result_Definition (Acc_Subprg));
11704 -- Start of processing for Check_Anonymous_Access_Component
11707 if Present (Access_Def) and then Mentions_T (Access_Def) then
11708 Acc_Def := Access_To_Subprogram_Definition (Access_Def);
11710 Build_Incomplete_Type_Declaration;
11711 Anon_Access := Make_Temporary (Loc, 'S');
11713 -- Create a declaration for the anonymous access type: either
11714 -- an access_to_object or an access_to_subprogram.
11716 if Present (Acc_Def) then
11717 if Nkind (Acc_Def) = N_Access_Function_Definition then
11719 Make_Access_Function_Definition (Loc,
11720 Parameter_Specifications =>
11721 Parameter_Specifications (Acc_Def),
11722 Result_Definition => Result_Definition (Acc_Def));
11725 Make_Access_Procedure_Definition (Loc,
11726 Parameter_Specifications =>
11727 Parameter_Specifications (Acc_Def));
11732 Make_Access_To_Object_Definition (Loc,
11733 Subtype_Indication =>
11734 Relocate_Node (Subtype_Mark (Access_Def)));
11736 Set_Constant_Present (Type_Def, Constant_Present (Access_Def));
11737 Set_All_Present (Type_Def, All_Present (Access_Def));
11740 Set_Null_Exclusion_Present
11741 (Type_Def, Null_Exclusion_Present (Access_Def));
11744 Make_Full_Type_Declaration (Loc,
11745 Defining_Identifier => Anon_Access,
11746 Type_Definition => Type_Def);
11748 Insert_Before (Typ_Decl, Decl);
11751 -- If an access to subprogram, create the extra formals
11753 if Present (Acc_Def) then
11754 Create_Extra_Formals (Designated_Type (Anon_Access));
11757 if Nkind (Comp_Def) = N_Component_Definition then
11759 Make_Component_Definition (Loc,
11760 Subtype_Indication => New_Occurrence_Of (Anon_Access, Loc)));
11762 pragma Assert (Nkind (Comp_Def) = N_Discriminant_Specification);
11764 Make_Discriminant_Specification (Loc,
11765 Defining_Identifier => Defining_Identifier (Comp_Def),
11766 Discriminant_Type => New_Occurrence_Of (Anon_Access, Loc)));
11769 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11770 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11772 Mutate_Ekind (Anon_Access, E_Anonymous_Access_Type);
11775 Set_Is_Local_Anonymous_Access (Anon_Access);
11777 end Check_Anonymous_Access_Component;
11779 ---------------------------------------
11780 -- Check_Anonymous_Access_Components --
11781 ---------------------------------------
11783 procedure Check_Anonymous_Access_Components
11784 (Typ_Decl : Node_Id;
11787 Comp_List : Node_Id)
11791 if No (Comp_List) then
11795 Comp := First (Component_Items (Comp_List));
11796 while Present (Comp) loop
11797 if Nkind (Comp) = N_Component_Declaration then
11798 Check_Anonymous_Access_Component
11799 (Typ_Decl, Typ, Prev,
11800 Component_Definition (Comp),
11801 Access_Definition (Component_Definition (Comp)));
11807 if Present (Variant_Part (Comp_List)) then
11811 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11812 while Present (V) loop
11813 Check_Anonymous_Access_Components
11814 (Typ_Decl, Typ, Prev, Component_List (V));
11815 Next_Non_Pragma (V);
11819 end Check_Anonymous_Access_Components;
11821 ----------------------
11822 -- Check_Completion --
11823 ----------------------
11825 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11828 procedure Post_Error;
11829 -- Post error message for lack of completion for entity E
11835 procedure Post_Error is
11836 procedure Missing_Body;
11837 -- Output missing body message
11843 procedure Missing_Body is
11845 -- Spec is in same unit, so we can post on spec
11847 if In_Same_Source_Unit (Body_Id, E) then
11848 Error_Msg_N ("missing body for &", E);
11850 -- Spec is in a separate unit, so we have to post on the body
11853 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11857 -- Start of processing for Post_Error
11860 if not Comes_From_Source (E) then
11861 if Ekind (E) in E_Task_Type | E_Protected_Type then
11863 -- It may be an anonymous protected type created for a
11864 -- single variable. Post error on variable, if present.
11870 Var := First_Entity (Current_Scope);
11871 while Present (Var) loop
11872 exit when Etype (Var) = E
11873 and then Comes_From_Source (Var);
11878 if Present (Var) then
11885 -- If a generated entity has no completion, then either previous
11886 -- semantic errors have disabled the expansion phase, or else we had
11887 -- missing subunits, or else we are compiling without expansion,
11888 -- or else something is very wrong.
11890 if not Comes_From_Source (E) then
11892 (Serious_Errors_Detected > 0
11893 or else Configurable_Run_Time_Violations > 0
11894 or else Subunits_Missing
11895 or else not Expander_Active);
11898 -- Here for source entity
11901 -- Here if no body to post the error message, so we post the error
11902 -- on the declaration that has no completion. This is not really
11903 -- the right place to post it, think about this later ???
11905 if No (Body_Id) then
11906 if Is_Type (E) then
11908 ("missing full declaration for }", Parent (E), E);
11910 Error_Msg_NE ("missing body for &", Parent (E), E);
11913 -- Package body has no completion for a declaration that appears
11914 -- in the corresponding spec. Post error on the body, with a
11915 -- reference to the non-completed declaration.
11918 Error_Msg_Sloc := Sloc (E);
11920 if Is_Type (E) then
11921 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11923 elsif Is_Overloadable (E)
11924 and then Current_Entity_In_Scope (E) /= E
11926 -- It may be that the completion is mistyped and appears as
11927 -- a distinct overloading of the entity.
11930 Candidate : constant Entity_Id :=
11931 Current_Entity_In_Scope (E);
11932 Decl : constant Node_Id :=
11933 Unit_Declaration_Node (Candidate);
11936 if Is_Overloadable (Candidate)
11937 and then Ekind (Candidate) = Ekind (E)
11938 and then Nkind (Decl) = N_Subprogram_Body
11939 and then Acts_As_Spec (Decl)
11941 Check_Type_Conformant (Candidate, E);
11957 Pack_Id : constant Entity_Id := Current_Scope;
11959 -- Start of processing for Check_Completion
11962 E := First_Entity (Pack_Id);
11963 while Present (E) loop
11964 if Is_Intrinsic_Subprogram (E) then
11967 -- The following situation requires special handling: a child unit
11968 -- that appears in the context clause of the body of its parent:
11970 -- procedure Parent.Child (...);
11972 -- with Parent.Child;
11973 -- package body Parent is
11975 -- Here Parent.Child appears as a local entity, but should not be
11976 -- flagged as requiring completion, because it is a compilation
11979 -- Ignore missing completion for a subprogram that does not come from
11980 -- source (including the _Call primitive operation of RAS types,
11981 -- which has to have the flag Comes_From_Source for other purposes):
11982 -- we assume that the expander will provide the missing completion.
11983 -- In case of previous errors, other expansion actions that provide
11984 -- bodies for null procedures with not be invoked, so inhibit message
11987 -- Note that E_Operator is not in the list that follows, because
11988 -- this kind is reserved for predefined operators, that are
11989 -- intrinsic and do not need completion.
11991 elsif Ekind (E) in E_Function
11993 | E_Generic_Function
11994 | E_Generic_Procedure
11996 if Has_Completion (E) then
11999 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
12002 elsif Is_Subprogram (E)
12003 and then (not Comes_From_Source (E)
12004 or else Chars (E) = Name_uCall)
12009 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
12013 elsif Nkind (Parent (E)) = N_Procedure_Specification
12014 and then Null_Present (Parent (E))
12015 and then Serious_Errors_Detected > 0
12023 elsif Is_Entry (E) then
12024 if not Has_Completion (E)
12025 and then Ekind (Scope (E)) = E_Protected_Type
12030 elsif Is_Package_Or_Generic_Package (E) then
12031 if Unit_Requires_Body (E) then
12032 if not Has_Completion (E)
12033 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
12039 elsif not Is_Child_Unit (E) then
12040 May_Need_Implicit_Body (E);
12043 -- A formal incomplete type (Ada 2012) does not require a completion;
12044 -- other incomplete type declarations do.
12046 elsif Ekind (E) = E_Incomplete_Type then
12047 if No (Underlying_Type (E))
12048 and then not Is_Generic_Type (E)
12053 elsif Ekind (E) in E_Task_Type | E_Protected_Type then
12054 if not Has_Completion (E) then
12058 -- A single task declared in the current scope is a constant, verify
12059 -- that the body of its anonymous type is in the same scope. If the
12060 -- task is defined elsewhere, this may be a renaming declaration for
12061 -- which no completion is needed.
12063 elsif Ekind (E) = E_Constant then
12064 if Ekind (Etype (E)) = E_Task_Type
12065 and then not Has_Completion (Etype (E))
12066 and then Scope (Etype (E)) = Current_Scope
12071 elsif Ekind (E) = E_Record_Type then
12072 if Is_Tagged_Type (E) then
12073 Check_Abstract_Overriding (E);
12074 Check_Conventions (E);
12077 Check_Aliased_Component_Types (E);
12079 elsif Ekind (E) = E_Array_Type then
12080 Check_Aliased_Component_Types (E);
12086 end Check_Completion;
12088 -------------------------------------
12089 -- Check_Constraining_Discriminant --
12090 -------------------------------------
12092 procedure Check_Constraining_Discriminant (New_Disc, Old_Disc : Entity_Id)
12094 New_Type : constant Entity_Id := Etype (New_Disc);
12095 Old_Type : Entity_Id;
12098 -- If the record type contains an array constrained by the discriminant
12099 -- but with some different bound, the compiler tries to create a smaller
12100 -- range for the discriminant type (see exp_ch3.Adjust_Discriminants).
12101 -- In this case, where the discriminant type is a scalar type, the check
12102 -- must use the original discriminant type in the parent declaration.
12104 if Is_Scalar_Type (New_Type) then
12105 Old_Type := Entity (Discriminant_Type (Parent (Old_Disc)));
12107 Old_Type := Etype (Old_Disc);
12110 if not Subtypes_Statically_Compatible (New_Type, Old_Type) then
12112 ("subtype must be statically compatible with parent discriminant",
12115 if not Predicates_Compatible (New_Type, Old_Type) then
12117 ("\subtype predicate is not compatible with parent discriminant",
12121 end Check_Constraining_Discriminant;
12123 ------------------------------------
12124 -- Check_CPP_Type_Has_No_Defaults --
12125 ------------------------------------
12127 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
12128 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
12133 -- Obtain the component list
12135 if Nkind (Tdef) = N_Record_Definition then
12136 Clist := Component_List (Tdef);
12137 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
12138 Clist := Component_List (Record_Extension_Part (Tdef));
12141 -- Check all components to ensure no default expressions
12143 if Present (Clist) then
12144 Comp := First (Component_Items (Clist));
12145 while Present (Comp) loop
12146 if Present (Expression (Comp)) then
12148 ("component of imported 'C'P'P type cannot have "
12149 & "default expression", Expression (Comp));
12155 end Check_CPP_Type_Has_No_Defaults;
12157 ----------------------------
12158 -- Check_Delta_Expression --
12159 ----------------------------
12161 procedure Check_Delta_Expression (E : Node_Id) is
12163 if not (Is_Real_Type (Etype (E))) then
12164 Wrong_Type (E, Any_Real);
12166 elsif not Is_OK_Static_Expression (E) then
12167 Flag_Non_Static_Expr
12168 ("non-static expression used for delta value!", E);
12170 elsif not UR_Is_Positive (Expr_Value_R (E)) then
12171 Error_Msg_N ("delta expression must be positive", E);
12177 -- If any of above errors occurred, then replace the incorrect
12178 -- expression by the real 0.1, which should prevent further errors.
12181 Make_Real_Literal (Sloc (E), Ureal_Tenth));
12182 Analyze_And_Resolve (E, Standard_Float);
12183 end Check_Delta_Expression;
12185 -----------------------------
12186 -- Check_Digits_Expression --
12187 -----------------------------
12189 procedure Check_Digits_Expression (E : Node_Id) is
12191 if not (Is_Integer_Type (Etype (E))) then
12192 Wrong_Type (E, Any_Integer);
12194 elsif not Is_OK_Static_Expression (E) then
12195 Flag_Non_Static_Expr
12196 ("non-static expression used for digits value!", E);
12198 elsif Expr_Value (E) <= 0 then
12199 Error_Msg_N ("digits value must be greater than zero", E);
12205 -- If any of above errors occurred, then replace the incorrect
12206 -- expression by the integer 1, which should prevent further errors.
12208 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
12209 Analyze_And_Resolve (E, Standard_Integer);
12211 end Check_Digits_Expression;
12213 --------------------------
12214 -- Check_Initialization --
12215 --------------------------
12217 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
12219 -- Special processing for limited types
12221 if Is_Limited_Type (T)
12222 and then not In_Instance
12223 and then not In_Inlined_Body
12225 if not OK_For_Limited_Init (T, Exp) then
12227 -- In GNAT mode, this is just a warning, to allow it to be evilly
12228 -- turned off. Otherwise it is a real error.
12232 ("??cannot initialize entities of limited type!", Exp);
12234 elsif Ada_Version < Ada_2005 then
12236 -- The side effect removal machinery may generate illegal Ada
12237 -- code to avoid the usage of access types and 'reference in
12238 -- SPARK mode. Since this is legal code with respect to theorem
12239 -- proving, do not emit the error.
12242 and then Nkind (Exp) = N_Function_Call
12243 and then Nkind (Parent (Exp)) = N_Object_Declaration
12244 and then not Comes_From_Source
12245 (Defining_Identifier (Parent (Exp)))
12251 ("cannot initialize entities of limited type", Exp);
12252 Explain_Limited_Type (T, Exp);
12256 -- Specialize error message according to kind of illegal
12257 -- initial expression. We check the Original_Node to cover
12258 -- cases where the initialization expression of an object
12259 -- declaration generated by the compiler has been rewritten
12260 -- (such as for dispatching calls).
12262 if Nkind (Original_Node (Exp)) = N_Type_Conversion
12264 Nkind (Expression (Original_Node (Exp))) = N_Function_Call
12266 -- No error for internally-generated object declarations,
12267 -- which can come from build-in-place assignment statements.
12269 if Nkind (Parent (Exp)) = N_Object_Declaration
12270 and then not Comes_From_Source
12271 (Defining_Identifier (Parent (Exp)))
12277 ("illegal context for call to function with limited "
12283 ("initialization of limited object requires aggregate or "
12284 & "function call", Exp);
12290 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
12291 -- set unless we can be sure that no range check is required.
12293 if not Expander_Active
12294 and then Is_Scalar_Type (T)
12295 and then not Is_In_Range (Exp, T, Assume_Valid => True)
12297 Set_Do_Range_Check (Exp);
12299 end Check_Initialization;
12301 ----------------------
12302 -- Check_Interfaces --
12303 ----------------------
12305 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
12306 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
12309 Iface_Def : Node_Id;
12310 Iface_Typ : Entity_Id;
12311 Parent_Node : Node_Id;
12313 Is_Task : Boolean := False;
12314 -- Set True if parent type or any progenitor is a task interface
12316 Is_Protected : Boolean := False;
12317 -- Set True if parent type or any progenitor is a protected interface
12319 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
12320 -- Check that a progenitor is compatible with declaration. If an error
12321 -- message is output, it is posted on Error_Node.
12327 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
12328 Iface_Id : constant Entity_Id :=
12329 Defining_Identifier (Parent (Iface_Def));
12330 Type_Def : Node_Id;
12333 if Nkind (N) = N_Private_Extension_Declaration then
12336 Type_Def := Type_Definition (N);
12339 if Is_Task_Interface (Iface_Id) then
12342 elsif Is_Protected_Interface (Iface_Id) then
12343 Is_Protected := True;
12346 if Is_Synchronized_Interface (Iface_Id) then
12348 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
12349 -- extension derived from a synchronized interface must explicitly
12350 -- be declared synchronized, because the full view will be a
12351 -- synchronized type.
12353 if Nkind (N) = N_Private_Extension_Declaration then
12354 if not Synchronized_Present (N) then
12356 ("private extension of& must be explicitly synchronized",
12360 -- However, by 3.9.4(16/2), a full type that is a record extension
12361 -- is never allowed to derive from a synchronized interface (note
12362 -- that interfaces must be excluded from this check, because those
12363 -- are represented by derived type definitions in some cases).
12365 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12366 and then not Interface_Present (Type_Definition (N))
12368 Error_Msg_N ("record extension cannot derive from synchronized "
12369 & "interface", Error_Node);
12373 -- Check that the characteristics of the progenitor are compatible
12374 -- with the explicit qualifier in the declaration.
12375 -- The check only applies to qualifiers that come from source.
12376 -- Limited_Present also appears in the declaration of corresponding
12377 -- records, and the check does not apply to them.
12379 if Limited_Present (Type_Def)
12381 Is_Concurrent_Record_Type (Defining_Identifier (N))
12383 if Is_Limited_Interface (Parent_Type)
12384 and then not Is_Limited_Interface (Iface_Id)
12387 ("progenitor & must be limited interface",
12388 Error_Node, Iface_Id);
12391 (Task_Present (Iface_Def)
12392 or else Protected_Present (Iface_Def)
12393 or else Synchronized_Present (Iface_Def))
12394 and then Nkind (N) /= N_Private_Extension_Declaration
12395 and then not Error_Posted (N)
12398 ("progenitor & must be limited interface",
12399 Error_Node, Iface_Id);
12402 -- Protected interfaces can only inherit from limited, synchronized
12403 -- or protected interfaces.
12405 elsif Nkind (N) = N_Full_Type_Declaration
12406 and then Protected_Present (Type_Def)
12408 if Limited_Present (Iface_Def)
12409 or else Synchronized_Present (Iface_Def)
12410 or else Protected_Present (Iface_Def)
12414 elsif Task_Present (Iface_Def) then
12415 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12416 & "from task interface", Error_Node);
12419 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12420 & "from non-limited interface", Error_Node);
12423 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12424 -- limited and synchronized.
12426 elsif Synchronized_Present (Type_Def) then
12427 if Limited_Present (Iface_Def)
12428 or else Synchronized_Present (Iface_Def)
12432 elsif Protected_Present (Iface_Def)
12433 and then Nkind (N) /= N_Private_Extension_Declaration
12435 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12436 & "from protected interface", Error_Node);
12438 elsif Task_Present (Iface_Def)
12439 and then Nkind (N) /= N_Private_Extension_Declaration
12441 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12442 & "from task interface", Error_Node);
12444 elsif not Is_Limited_Interface (Iface_Id) then
12445 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12446 & "from non-limited interface", Error_Node);
12449 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12450 -- synchronized or task interfaces.
12452 elsif Nkind (N) = N_Full_Type_Declaration
12453 and then Task_Present (Type_Def)
12455 if Limited_Present (Iface_Def)
12456 or else Synchronized_Present (Iface_Def)
12457 or else Task_Present (Iface_Def)
12461 elsif Protected_Present (Iface_Def) then
12462 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12463 & "protected interface", Error_Node);
12466 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12467 & "non-limited interface", Error_Node);
12472 -- Start of processing for Check_Interfaces
12475 if Is_Interface (Parent_Type) then
12476 if Is_Task_Interface (Parent_Type) then
12479 elsif Is_Protected_Interface (Parent_Type) then
12480 Is_Protected := True;
12484 if Nkind (N) = N_Private_Extension_Declaration then
12486 -- Check that progenitors are compatible with declaration
12488 Iface := First (Interface_List (Def));
12489 while Present (Iface) loop
12490 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12492 Parent_Node := Parent (Base_Type (Iface_Typ));
12493 Iface_Def := Type_Definition (Parent_Node);
12495 if not Is_Interface (Iface_Typ) then
12496 Diagnose_Interface (Iface, Iface_Typ);
12498 Check_Ifaces (Iface_Def, Iface);
12504 if Is_Task and Is_Protected then
12506 ("type cannot derive from task and protected interface", N);
12512 -- Full type declaration of derived type.
12513 -- Check compatibility with parent if it is interface type
12515 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12516 and then Is_Interface (Parent_Type)
12518 Parent_Node := Parent (Parent_Type);
12520 -- More detailed checks for interface varieties
12523 (Iface_Def => Type_Definition (Parent_Node),
12524 Error_Node => Subtype_Indication (Type_Definition (N)));
12527 Iface := First (Interface_List (Def));
12528 while Present (Iface) loop
12529 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12531 Parent_Node := Parent (Base_Type (Iface_Typ));
12532 Iface_Def := Type_Definition (Parent_Node);
12534 if not Is_Interface (Iface_Typ) then
12535 Diagnose_Interface (Iface, Iface_Typ);
12538 -- "The declaration of a specific descendant of an interface
12539 -- type freezes the interface type" RM 13.14
12541 Freeze_Before (N, Iface_Typ);
12542 Check_Ifaces (Iface_Def, Error_Node => Iface);
12548 if Is_Task and Is_Protected then
12550 ("type cannot derive from task and protected interface", N);
12552 end Check_Interfaces;
12554 ------------------------------------
12555 -- Check_Or_Process_Discriminants --
12556 ------------------------------------
12558 -- If an incomplete or private type declaration was already given for the
12559 -- type, the discriminants may have already been processed if they were
12560 -- present on the incomplete declaration. In this case a full conformance
12561 -- check has been performed in Find_Type_Name, and we then recheck here
12562 -- some properties that can't be checked on the partial view alone.
12563 -- Otherwise we call Process_Discriminants.
12565 procedure Check_Or_Process_Discriminants
12568 Prev : Entity_Id := Empty)
12571 if Has_Discriminants (T) then
12573 -- Discriminants are already set on T if they were already present
12574 -- on the partial view. Make them visible to component declarations.
12578 -- Discriminant on T (full view) referencing expr on partial view
12580 Prev_D : Entity_Id;
12581 -- Entity of corresponding discriminant on partial view
12584 -- Discriminant specification for full view, expression is
12585 -- the syntactic copy on full view (which has been checked for
12586 -- conformance with partial view), only used here to post error
12590 D := First_Discriminant (T);
12591 New_D := First (Discriminant_Specifications (N));
12592 while Present (D) loop
12593 Prev_D := Current_Entity (D);
12594 Set_Current_Entity (D);
12595 Set_Is_Immediately_Visible (D);
12596 Set_Homonym (D, Prev_D);
12598 -- Handle the case where there is an untagged partial view and
12599 -- the full view is tagged: must disallow discriminants with
12600 -- defaults, unless compiling for Ada 2012, which allows a
12601 -- limited tagged type to have defaulted discriminants (see
12602 -- AI05-0214). However, suppress error here if it was already
12603 -- reported on the default expression of the partial view.
12605 if Is_Tagged_Type (T)
12606 and then Present (Expression (Parent (D)))
12607 and then (not Is_Limited_Type (Current_Scope)
12608 or else Ada_Version < Ada_2012)
12609 and then not Error_Posted (Expression (Parent (D)))
12611 if Ada_Version >= Ada_2012 then
12613 ("discriminants of nonlimited tagged type cannot have "
12615 Expression (New_D));
12618 ("discriminants of tagged type cannot have defaults",
12619 Expression (New_D));
12623 -- Ada 2005 (AI-230): Access discriminant allowed in
12624 -- non-limited record types.
12626 if Ada_Version < Ada_2005 then
12628 -- This restriction gets applied to the full type here. It
12629 -- has already been applied earlier to the partial view.
12631 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12634 Next_Discriminant (D);
12639 elsif Present (Discriminant_Specifications (N)) then
12640 Process_Discriminants (N, Prev);
12642 end Check_Or_Process_Discriminants;
12644 ----------------------
12645 -- Check_Real_Bound --
12646 ----------------------
12648 procedure Check_Real_Bound (Bound : Node_Id) is
12650 if not Is_Real_Type (Etype (Bound)) then
12652 ("bound in real type definition must be of real type", Bound);
12654 elsif not Is_OK_Static_Expression (Bound) then
12655 Flag_Non_Static_Expr
12656 ("non-static expression used for real type bound!", Bound);
12663 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12665 Resolve (Bound, Standard_Float);
12666 end Check_Real_Bound;
12668 ------------------------------
12669 -- Complete_Private_Subtype --
12670 ------------------------------
12672 procedure Complete_Private_Subtype
12675 Full_Base : Entity_Id;
12676 Related_Nod : Node_Id)
12678 Save_Next_Entity : Entity_Id;
12679 Save_Homonym : Entity_Id;
12682 -- Set semantic attributes for (implicit) private subtype completion.
12683 -- If the full type has no discriminants, then it is a copy of the
12684 -- full view of the base. Otherwise, it is a subtype of the base with
12685 -- a possible discriminant constraint. Save and restore the original
12686 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12687 -- not corrupt the entity chain.
12689 Save_Next_Entity := Next_Entity (Full);
12690 Save_Homonym := Homonym (Priv);
12692 if Is_Private_Type (Full_Base)
12693 or else Is_Record_Type (Full_Base)
12694 or else Is_Concurrent_Type (Full_Base)
12696 Copy_Node (Priv, Full);
12698 -- Note that the Etype of the full view is the same as the Etype of
12699 -- the partial view. In this fashion, the subtype has access to the
12700 -- correct view of the parent.
12702 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
12703 Set_Has_Unknown_Discriminants
12704 (Full, Has_Unknown_Discriminants (Full_Base));
12705 Set_First_Entity (Full, First_Entity (Full_Base));
12706 Set_Last_Entity (Full, Last_Entity (Full_Base));
12708 -- If the underlying base type is constrained, we know that the
12709 -- full view of the subtype is constrained as well (the converse
12710 -- is not necessarily true).
12712 if Is_Constrained (Full_Base) then
12713 Set_Is_Constrained (Full);
12717 Copy_Node (Full_Base, Full);
12719 -- The following subtlety with the Etype of the full view needs to be
12720 -- taken into account here. One could think that it must naturally be
12721 -- set to the base type of the full base:
12723 -- Set_Etype (Full, Base_Type (Full_Base));
12725 -- so that the full view becomes a subtype of the full base when the
12726 -- latter is a base type, which must for example happen when the full
12727 -- base is declared as derived type. That's also correct if the full
12728 -- base is declared as an array type, or a floating-point type, or a
12729 -- fixed-point type, or a signed integer type, as these declarations
12730 -- create an implicit base type and a first subtype so the Etype of
12731 -- the full views must be the implicit base type. But that's wrong
12732 -- if the full base is declared as an access type, or an enumeration
12733 -- type, or a modular integer type, as these declarations directly
12734 -- create a base type, i.e. with Etype pointing to itself. Moreover
12735 -- the full base being declared in the private part, i.e. when the
12736 -- views are swapped, the end result is that the Etype of the full
12737 -- base is set to its private view in this case and that we need to
12738 -- propagate this setting to the full view in order for the subtype
12739 -- to be compatible with the base type.
12741 if Is_Base_Type (Full_Base)
12742 and then (Is_Derived_Type (Full_Base)
12743 or else Ekind (Full_Base) in Array_Kind
12744 or else Ekind (Full_Base) in Fixed_Point_Kind
12745 or else Ekind (Full_Base) in Float_Kind
12746 or else Ekind (Full_Base) in Signed_Integer_Kind)
12748 Set_Etype (Full, Full_Base);
12751 Set_Chars (Full, Chars (Priv));
12752 Set_Sloc (Full, Sloc (Priv));
12753 Conditional_Delay (Full, Priv);
12756 Link_Entities (Full, Save_Next_Entity);
12757 Set_Homonym (Full, Save_Homonym);
12758 Set_Associated_Node_For_Itype (Full, Related_Nod);
12760 if Ekind (Full) in Incomplete_Or_Private_Kind then
12761 Reinit_Field_To_Zero (Full, F_Private_Dependents);
12764 -- Set common attributes for all subtypes: kind, convention, etc.
12766 Mutate_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12767 Set_Convention (Full, Convention (Full_Base));
12768 Set_Is_First_Subtype (Full, False);
12769 Set_Scope (Full, Scope (Priv));
12770 Set_Size_Info (Full, Full_Base);
12771 Copy_RM_Size (To => Full, From => Full_Base);
12772 Set_Is_Itype (Full);
12774 -- A subtype of a private-type-without-discriminants, whose full-view
12775 -- has discriminants with default expressions, is not constrained.
12777 if not Has_Discriminants (Priv) then
12778 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12780 if Has_Discriminants (Full_Base) then
12781 Set_Discriminant_Constraint
12782 (Full, Discriminant_Constraint (Full_Base));
12784 -- The partial view may have been indefinite, the full view
12787 Set_Has_Unknown_Discriminants
12788 (Full, Has_Unknown_Discriminants (Full_Base));
12792 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12793 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12795 -- Freeze the private subtype entity if its parent is delayed, and not
12796 -- already frozen. We skip this processing if the type is an anonymous
12797 -- subtype of a record component, or is the corresponding record of a
12798 -- protected type, since these are processed when the enclosing type
12799 -- is frozen. If the parent type is declared in a nested package then
12800 -- the freezing of the private and full views also happens later.
12802 if not Is_Type (Scope (Full)) then
12804 and then In_Same_Source_Unit (Full, Full_Base)
12805 and then Scope (Full_Base) /= Scope (Full)
12807 Set_Has_Delayed_Freeze (Full);
12808 Set_Has_Delayed_Freeze (Priv);
12811 Set_Has_Delayed_Freeze (Full,
12812 Has_Delayed_Freeze (Full_Base)
12813 and then not Is_Frozen (Full_Base));
12817 Set_Freeze_Node (Full, Empty);
12818 Set_Is_Frozen (Full, False);
12820 if Has_Discriminants (Full) then
12821 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12822 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12824 if Has_Unknown_Discriminants (Full) then
12825 Set_Discriminant_Constraint (Full, No_Elist);
12829 if Ekind (Full_Base) = E_Record_Type
12830 and then Has_Discriminants (Full_Base)
12831 and then Has_Discriminants (Priv) -- might not, if errors
12832 and then not Has_Unknown_Discriminants (Priv)
12833 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12835 Create_Constrained_Components
12836 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12838 -- If the full base is itself derived from private, build a congruent
12839 -- subtype of its underlying full view, for use by the back end.
12841 elsif Is_Private_Type (Full_Base)
12842 and then Present (Underlying_Full_View (Full_Base))
12845 Underlying_Full_Base : constant Entity_Id
12846 := Underlying_Full_View (Full_Base);
12847 Underlying_Full : constant Entity_Id
12848 := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
12850 Set_Is_Itype (Underlying_Full);
12851 Set_Associated_Node_For_Itype (Underlying_Full, Related_Nod);
12852 Complete_Private_Subtype
12853 (Priv, Underlying_Full, Underlying_Full_Base, Related_Nod);
12854 Set_Underlying_Full_View (Full, Underlying_Full);
12855 Set_Is_Underlying_Full_View (Underlying_Full);
12858 elsif Is_Record_Type (Full_Base) then
12860 -- Show Full is simply a renaming of Full_Base
12862 Set_Cloned_Subtype (Full, Full_Base);
12863 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
12865 -- Propagate predicates
12867 Propagate_Predicate_Attributes (Full, Full_Base);
12870 -- It is unsafe to share the bounds of a scalar type, because the Itype
12871 -- is elaborated on demand, and if a bound is nonstatic, then different
12872 -- orders of elaboration in different units will lead to different
12873 -- external symbols.
12875 if Is_Scalar_Type (Full_Base) then
12876 Set_Scalar_Range (Full,
12877 Make_Range (Sloc (Related_Nod),
12879 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12881 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12883 -- This completion inherits the bounds of the full parent, but if
12884 -- the parent is an unconstrained floating point type, so is the
12887 if Is_Floating_Point_Type (Full_Base) then
12888 Set_Includes_Infinities
12889 (Scalar_Range (Full), Has_Infinities (Full_Base));
12893 -- ??? It seems that a lot of fields are missing that should be copied
12894 -- from Full_Base to Full. Here are some that are introduced in a
12895 -- non-disruptive way but a cleanup is necessary.
12897 if Is_Tagged_Type (Full_Base) then
12898 Set_Is_Tagged_Type (Full);
12899 Set_Is_Limited_Record (Full, Is_Limited_Record (Full_Base));
12901 Set_Direct_Primitive_Operations
12902 (Full, Direct_Primitive_Operations (Full_Base));
12903 Set_No_Tagged_Streams_Pragma
12904 (Full, No_Tagged_Streams_Pragma (Full_Base));
12906 if Is_Interface (Full_Base) then
12907 Set_Is_Interface (Full);
12908 Set_Is_Limited_Interface (Full, Is_Limited_Interface (Full_Base));
12911 -- Inherit class_wide type of full_base in case the partial view was
12912 -- not tagged. Otherwise it has already been created when the private
12913 -- subtype was analyzed.
12915 if No (Class_Wide_Type (Full)) then
12916 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12919 -- If this is a subtype of a protected or task type, constrain its
12920 -- corresponding record, unless this is a subtype without constraints,
12921 -- i.e. a simple renaming as with an actual subtype in an instance.
12923 elsif Is_Concurrent_Type (Full_Base) then
12924 if Has_Discriminants (Full)
12925 and then Present (Corresponding_Record_Type (Full_Base))
12927 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12929 Set_Corresponding_Record_Type (Full,
12930 Constrain_Corresponding_Record
12931 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12934 Set_Corresponding_Record_Type (Full,
12935 Corresponding_Record_Type (Full_Base));
12939 -- Link rep item chain, and also setting of Has_Predicates from private
12940 -- subtype to full subtype, since we will need these on the full subtype
12941 -- to create the predicate function. Note that the full subtype may
12942 -- already have rep items, inherited from the full view of the base
12943 -- type, so we must be sure not to overwrite these entries.
12948 Next_Item : Node_Id;
12949 Priv_Item : Node_Id;
12952 Item := First_Rep_Item (Full);
12953 Priv_Item := First_Rep_Item (Priv);
12955 -- If no existing rep items on full type, we can just link directly
12956 -- to the list of items on the private type, if any exist.. Same if
12957 -- the rep items are only those inherited from the base
12960 or else Nkind (Item) /= N_Aspect_Specification
12961 or else Entity (Item) = Full_Base)
12962 and then Present (First_Rep_Item (Priv))
12964 Set_First_Rep_Item (Full, Priv_Item);
12966 -- Otherwise, search to the end of items currently linked to the full
12967 -- subtype and append the private items to the end. However, if Priv
12968 -- and Full already have the same list of rep items, then the append
12969 -- is not done, as that would create a circularity.
12971 -- The partial view may have a predicate and the rep item lists of
12972 -- both views agree when inherited from the same ancestor. In that
12973 -- case, simply propagate the list from one view to the other.
12974 -- A more complex analysis needed here ???
12976 elsif Present (Priv_Item)
12977 and then Item = Next_Rep_Item (Priv_Item)
12979 Set_First_Rep_Item (Full, Priv_Item);
12981 elsif Item /= Priv_Item then
12984 Next_Item := Next_Rep_Item (Item);
12985 exit when No (Next_Item);
12988 -- If the private view has aspect specifications, the full view
12989 -- inherits them. Since these aspects may already have been
12990 -- attached to the full view during derivation, do not append
12991 -- them if already present.
12993 if Item = First_Rep_Item (Priv) then
12999 -- And link the private type items at the end of the chain
13002 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
13007 -- Make sure Has_Predicates is set on full type if it is set on the
13008 -- private type. Note that it may already be set on the full type and
13009 -- if so, we don't want to unset it. Similarly, propagate information
13010 -- about delayed aspects, because the corresponding pragmas must be
13011 -- analyzed when one of the views is frozen. This last step is needed
13012 -- in particular when the full type is a scalar type for which an
13013 -- anonymous base type is constructed.
13015 -- The predicate functions are generated either at the freeze point
13016 -- of the type or at the end of the visible part, and we must avoid
13017 -- generating them twice.
13019 Propagate_Predicate_Attributes (Full, Priv);
13021 if Has_Delayed_Aspects (Priv) then
13022 Set_Has_Delayed_Aspects (Full);
13024 end Complete_Private_Subtype;
13026 ----------------------------
13027 -- Constant_Redeclaration --
13028 ----------------------------
13030 procedure Constant_Redeclaration
13035 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
13036 Obj_Def : constant Node_Id := Object_Definition (N);
13039 procedure Check_Possible_Deferred_Completion
13040 (Prev_Id : Entity_Id;
13041 Prev_Obj_Def : Node_Id;
13042 Curr_Obj_Def : Node_Id);
13043 -- Determine whether the two object definitions describe the partial
13044 -- and the full view of a constrained deferred constant. Generate
13045 -- a subtype for the full view and verify that it statically matches
13046 -- the subtype of the partial view.
13048 procedure Check_Recursive_Declaration (Typ : Entity_Id);
13049 -- If deferred constant is an access type initialized with an allocator,
13050 -- check whether there is an illegal recursion in the definition,
13051 -- through a default value of some record subcomponent. This is normally
13052 -- detected when generating init procs, but requires this additional
13053 -- mechanism when expansion is disabled.
13055 ----------------------------------------
13056 -- Check_Possible_Deferred_Completion --
13057 ----------------------------------------
13059 procedure Check_Possible_Deferred_Completion
13060 (Prev_Id : Entity_Id;
13061 Prev_Obj_Def : Node_Id;
13062 Curr_Obj_Def : Node_Id)
13065 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
13066 and then Present (Constraint (Prev_Obj_Def))
13067 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
13068 and then Present (Constraint (Curr_Obj_Def))
13071 Loc : constant Source_Ptr := Sloc (N);
13072 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
13073 Decl : constant Node_Id :=
13074 Make_Subtype_Declaration (Loc,
13075 Defining_Identifier => Def_Id,
13076 Subtype_Indication =>
13077 Relocate_Node (Curr_Obj_Def));
13080 Insert_Before_And_Analyze (N, Decl);
13081 Set_Etype (Id, Def_Id);
13083 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
13084 Error_Msg_Sloc := Sloc (Prev_Id);
13085 Error_Msg_N ("subtype does not statically match deferred "
13086 & "declaration #", N);
13090 end Check_Possible_Deferred_Completion;
13092 ---------------------------------
13093 -- Check_Recursive_Declaration --
13094 ---------------------------------
13096 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
13100 if Is_Record_Type (Typ) then
13101 Comp := First_Component (Typ);
13102 while Present (Comp) loop
13103 if Comes_From_Source (Comp) then
13104 if Present (Expression (Parent (Comp)))
13105 and then Is_Entity_Name (Expression (Parent (Comp)))
13106 and then Entity (Expression (Parent (Comp))) = Prev
13108 Error_Msg_Sloc := Sloc (Parent (Comp));
13110 ("illegal circularity with declaration for & #",
13114 elsif Is_Record_Type (Etype (Comp)) then
13115 Check_Recursive_Declaration (Etype (Comp));
13119 Next_Component (Comp);
13122 end Check_Recursive_Declaration;
13124 -- Start of processing for Constant_Redeclaration
13127 if Nkind (Parent (Prev)) = N_Object_Declaration then
13128 if Nkind (Object_Definition
13129 (Parent (Prev))) = N_Subtype_Indication
13131 -- Find type of new declaration. The constraints of the two
13132 -- views must match statically, but there is no point in
13133 -- creating an itype for the full view.
13135 if Nkind (Obj_Def) = N_Subtype_Indication then
13136 Find_Type (Subtype_Mark (Obj_Def));
13137 New_T := Entity (Subtype_Mark (Obj_Def));
13140 Find_Type (Obj_Def);
13141 New_T := Entity (Obj_Def);
13147 -- The full view may impose a constraint, even if the partial
13148 -- view does not, so construct the subtype.
13150 New_T := Find_Type_Of_Object (Obj_Def, N);
13155 -- Current declaration is illegal, diagnosed below in Enter_Name
13161 -- If previous full declaration or a renaming declaration exists, or if
13162 -- a homograph is present, let Enter_Name handle it, either with an
13163 -- error or with the removal of an overridden implicit subprogram.
13164 -- The previous one is a full declaration if it has an expression
13165 -- (which in the case of an aggregate is indicated by the Init flag).
13167 if Ekind (Prev) /= E_Constant
13168 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
13169 or else Present (Expression (Parent (Prev)))
13170 or else Has_Init_Expression (Parent (Prev))
13171 or else Present (Full_View (Prev))
13175 -- Verify that types of both declarations match, or else that both types
13176 -- are anonymous access types whose designated subtypes statically match
13177 -- (as allowed in Ada 2005 by AI-385).
13179 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
13181 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
13182 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
13183 or else Is_Access_Constant (Etype (New_T)) /=
13184 Is_Access_Constant (Etype (Prev))
13185 or else Can_Never_Be_Null (Etype (New_T)) /=
13186 Can_Never_Be_Null (Etype (Prev))
13187 or else Null_Exclusion_Present (Parent (Prev)) /=
13188 Null_Exclusion_Present (Parent (Id))
13189 or else not Subtypes_Statically_Match
13190 (Designated_Type (Etype (Prev)),
13191 Designated_Type (Etype (New_T))))
13193 Error_Msg_Sloc := Sloc (Prev);
13194 Error_Msg_N ("type does not match declaration#", N);
13195 Set_Full_View (Prev, Id);
13196 Set_Etype (Id, Any_Type);
13198 -- A deferred constant whose type is an anonymous array is always
13199 -- illegal (unless imported). A detailed error message might be
13200 -- helpful for Ada beginners.
13202 if Nkind (Object_Definition (Parent (Prev)))
13203 = N_Constrained_Array_Definition
13204 and then Nkind (Object_Definition (N))
13205 = N_Constrained_Array_Definition
13207 Error_Msg_N ("\each anonymous array is a distinct type", N);
13208 Error_Msg_N ("a deferred constant must have a named type",
13209 Object_Definition (Parent (Prev)));
13213 Null_Exclusion_Present (Parent (Prev))
13214 and then not Null_Exclusion_Present (N)
13216 Error_Msg_Sloc := Sloc (Prev);
13217 Error_Msg_N ("null-exclusion does not match declaration#", N);
13218 Set_Full_View (Prev, Id);
13219 Set_Etype (Id, Any_Type);
13221 -- If so, process the full constant declaration
13224 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
13225 -- the deferred declaration is constrained, then the subtype defined
13226 -- by the subtype_indication in the full declaration shall match it
13229 Check_Possible_Deferred_Completion
13231 Prev_Obj_Def => Object_Definition (Parent (Prev)),
13232 Curr_Obj_Def => Obj_Def);
13234 Set_Full_View (Prev, Id);
13235 Set_Is_Public (Id, Is_Public (Prev));
13236 Set_Is_Internal (Id);
13237 Append_Entity (Id, Current_Scope);
13239 -- Check ALIASED present if present before (RM 7.4(7))
13241 if Is_Aliased (Prev)
13242 and then not Aliased_Present (N)
13244 Error_Msg_Sloc := Sloc (Prev);
13245 Error_Msg_N ("ALIASED required (see declaration #)", N);
13248 -- Check that placement is in private part and that the incomplete
13249 -- declaration appeared in the visible part.
13251 if Ekind (Current_Scope) = E_Package
13252 and then not In_Private_Part (Current_Scope)
13254 Error_Msg_Sloc := Sloc (Prev);
13256 ("full constant for declaration # must be in private part", N);
13258 elsif Ekind (Current_Scope) = E_Package
13260 List_Containing (Parent (Prev)) /=
13261 Visible_Declarations (Package_Specification (Current_Scope))
13264 ("deferred constant must be declared in visible part",
13268 if Is_Access_Type (T)
13269 and then Nkind (Expression (N)) = N_Allocator
13271 Check_Recursive_Declaration (Designated_Type (T));
13274 -- A deferred constant is a visible entity. If type has invariants,
13275 -- verify that the initial value satisfies them. This is not done in
13276 -- GNATprove mode, as GNATprove handles invariant checks itself.
13278 if Has_Invariants (T)
13279 and then Present (Invariant_Procedure (T))
13280 and then not GNATprove_Mode
13283 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
13286 end Constant_Redeclaration;
13288 ----------------------
13289 -- Constrain_Access --
13290 ----------------------
13292 procedure Constrain_Access
13293 (Def_Id : in out Entity_Id;
13295 Related_Nod : Node_Id)
13297 T : constant Entity_Id := Entity (Subtype_Mark (S));
13298 Desig_Type : constant Entity_Id := Designated_Type (T);
13299 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
13300 Constraint_OK : Boolean := True;
13303 if Is_Array_Type (Desig_Type) then
13304 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
13306 elsif (Is_Record_Type (Desig_Type)
13307 or else Is_Incomplete_Or_Private_Type (Desig_Type))
13308 and then not Is_Constrained (Desig_Type)
13310 -- If this is a constrained access definition for a record
13311 -- component, we leave the type as an unconstrained access,
13312 -- and mark the component so that its actual type is built
13313 -- at a point of use (e.g., an assignment statement). This
13314 -- is handled in Sem_Util.Build_Actual_Subtype_Of_Component.
13316 if Desig_Type = Current_Scope
13317 and then No (Def_Id)
13321 (E_Void, Related_Nod, Scope_Id => Scope (Desig_Type));
13322 Mutate_Ekind (Desig_Subtype, E_Record_Subtype);
13323 Def_Id := Entity (Subtype_Mark (S));
13325 -- We indicate that the component has a per-object constraint
13326 -- for treatment at a point of use, even though the constraint
13327 -- may be independent of discriminants of the enclosing type.
13329 if Nkind (Related_Nod) = N_Component_Declaration then
13330 Set_Has_Per_Object_Constraint
13331 (Defining_Identifier (Related_Nod));
13334 -- This call added to ensure that the constraint is analyzed
13335 -- (needed for a B test). Note that we still return early from
13336 -- this procedure to avoid recursive processing.
13338 Constrain_Discriminated_Type
13339 (Desig_Subtype, S, Related_Nod, For_Access => True);
13343 -- Enforce rule that the constraint is illegal if there is an
13344 -- unconstrained view of the designated type. This means that the
13345 -- partial view (either a private type declaration or a derivation
13346 -- from a private type) has no discriminants. (Defect Report
13347 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
13349 -- Rule updated for Ada 2005: The private type is said to have
13350 -- a constrained partial view, given that objects of the type
13351 -- can be declared. Furthermore, the rule applies to all access
13352 -- types, unlike the rule concerning default discriminants (see
13355 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
13356 and then Has_Private_Declaration (Desig_Type)
13357 and then In_Open_Scopes (Scope (Desig_Type))
13358 and then Has_Discriminants (Desig_Type)
13361 Pack : constant Node_Id :=
13362 Unit_Declaration_Node (Scope (Desig_Type));
13367 if Nkind (Pack) = N_Package_Declaration then
13368 Decls := Visible_Declarations (Specification (Pack));
13369 Decl := First (Decls);
13370 while Present (Decl) loop
13371 if (Nkind (Decl) = N_Private_Type_Declaration
13372 and then Chars (Defining_Identifier (Decl)) =
13373 Chars (Desig_Type))
13376 (Nkind (Decl) = N_Full_Type_Declaration
13378 Chars (Defining_Identifier (Decl)) =
13380 and then Is_Derived_Type (Desig_Type)
13382 Has_Private_Declaration (Etype (Desig_Type)))
13384 if No (Discriminant_Specifications (Decl)) then
13386 ("cannot constrain access type if designated "
13387 & "type has constrained partial view", S);
13399 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13400 For_Access => True);
13402 elsif Is_Concurrent_Type (Desig_Type)
13403 and then not Is_Constrained (Desig_Type)
13405 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13408 Error_Msg_N ("invalid constraint on access type", S);
13410 -- We simply ignore an invalid constraint
13412 Desig_Subtype := Desig_Type;
13413 Constraint_OK := False;
13416 if No (Def_Id) then
13417 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13419 Mutate_Ekind (Def_Id, E_Access_Subtype);
13422 if Constraint_OK then
13423 Set_Etype (Def_Id, Base_Type (T));
13425 if Is_Private_Type (Desig_Type) then
13426 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13429 Set_Etype (Def_Id, Any_Type);
13432 Set_Size_Info (Def_Id, T);
13433 Set_Is_Constrained (Def_Id, Constraint_OK);
13434 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13435 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13436 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13438 Conditional_Delay (Def_Id, T);
13440 -- AI-363 : Subtypes of general access types whose designated types have
13441 -- default discriminants are disallowed. In instances, the rule has to
13442 -- be checked against the actual, of which T is the subtype. In a
13443 -- generic body, the rule is checked assuming that the actual type has
13444 -- defaulted discriminants.
13446 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13447 if Ekind (Base_Type (T)) = E_General_Access_Type
13448 and then Has_Defaulted_Discriminants (Desig_Type)
13450 if Ada_Version < Ada_2005 then
13452 ("access subtype of general access type would not " &
13453 "be allowed in Ada 2005?y?", S);
13456 ("access subtype of general access type not allowed", S);
13459 Error_Msg_N ("\discriminants have defaults", S);
13461 elsif Is_Access_Type (T)
13462 and then Is_Generic_Type (Desig_Type)
13463 and then Has_Discriminants (Desig_Type)
13464 and then In_Package_Body (Current_Scope)
13466 if Ada_Version < Ada_2005 then
13468 ("access subtype would not be allowed in generic body "
13469 & "in Ada 2005?y?", S);
13472 ("access subtype not allowed in generic body", S);
13476 ("\designated type is a discriminated formal", S);
13479 end Constrain_Access;
13481 ---------------------
13482 -- Constrain_Array --
13483 ---------------------
13485 procedure Constrain_Array
13486 (Def_Id : in out Entity_Id;
13488 Related_Nod : Node_Id;
13489 Related_Id : Entity_Id;
13490 Suffix : Character)
13492 C : constant Node_Id := Constraint (SI);
13493 Number_Of_Constraints : Nat := 0;
13496 Constraint_OK : Boolean := True;
13497 Is_FLB_Array_Subtype : Boolean := False;
13500 T := Entity (Subtype_Mark (SI));
13502 if Is_Access_Type (T) then
13503 T := Designated_Type (T);
13506 -- If an index constraint follows a subtype mark in a subtype indication
13507 -- then the type or subtype denoted by the subtype mark must not already
13508 -- impose an index constraint. The subtype mark must denote either an
13509 -- unconstrained array type or an access type whose designated type
13510 -- is such an array type... (RM 3.6.1)
13512 if Is_Constrained (T) then
13513 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13514 Constraint_OK := False;
13517 S := First (Constraints (C));
13518 while Present (S) loop
13519 Number_Of_Constraints := Number_Of_Constraints + 1;
13523 -- In either case, the index constraint must provide a discrete
13524 -- range for each index of the array type and the type of each
13525 -- discrete range must be the same as that of the corresponding
13526 -- index. (RM 3.6.1)
13528 if Number_Of_Constraints /= Number_Dimensions (T) then
13529 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13530 Constraint_OK := False;
13533 S := First (Constraints (C));
13534 Index := First_Index (T);
13537 -- Apply constraints to each index type
13539 for J in 1 .. Number_Of_Constraints loop
13540 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13542 -- If the subtype of the index has been set to indicate that
13543 -- it has a fixed lower bound, then record that the subtype's
13544 -- entity will need to be marked as being a fixed-lower-bound
13547 if S = First (Constraints (C)) then
13548 Is_FLB_Array_Subtype :=
13549 Is_Fixed_Lower_Bound_Index_Subtype (Etype (S));
13551 -- If the parent subtype (or should this be Etype of that?)
13552 -- is an FLB array subtype, we flag an error, because we
13553 -- don't currently allow subtypes of such subtypes to
13554 -- specify a fixed lower bound for any of their indexes,
13555 -- even if the index of the parent subtype is a "range <>"
13558 if Is_FLB_Array_Subtype
13559 and then Is_Fixed_Lower_Bound_Array_Subtype (T)
13562 ("index with fixed lower bound not allowed for subtype "
13563 & "of fixed-lower-bound }", S, T);
13565 Is_FLB_Array_Subtype := False;
13568 elsif Is_FLB_Array_Subtype
13569 and then not Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13572 ("constrained index not allowed for fixed-lower-bound "
13573 & "subtype of}", S, T);
13575 elsif not Is_FLB_Array_Subtype
13576 and then Is_Fixed_Lower_Bound_Index_Subtype (Etype (S))
13579 ("index with fixed lower bound not allowed for "
13580 & "constrained subtype of}", S, T);
13590 if No (Def_Id) then
13592 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13593 Set_Parent (Def_Id, Related_Nod);
13596 Mutate_Ekind (Def_Id, E_Array_Subtype);
13599 Set_Size_Info (Def_Id, (T));
13600 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13601 Set_Etype (Def_Id, Base_Type (T));
13603 if Constraint_OK then
13604 Set_First_Index (Def_Id, First (Constraints (C)));
13606 Set_First_Index (Def_Id, First_Index (T));
13609 Set_Is_Constrained (Def_Id, not Is_FLB_Array_Subtype);
13610 Set_Is_Fixed_Lower_Bound_Array_Subtype
13611 (Def_Id, Is_FLB_Array_Subtype);
13612 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13613 Set_Is_Independent (Def_Id, Is_Independent (T));
13614 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13616 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13617 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13619 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13620 -- We need to initialize the attribute because if Def_Id is previously
13621 -- analyzed through a limited_with clause, it will have the attributes
13622 -- of an incomplete type, one of which is an Elist that overlaps the
13623 -- Packed_Array_Impl_Type field.
13625 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13627 -- Build a freeze node if parent still needs one. Also make sure that
13628 -- the Depends_On_Private status is set because the subtype will need
13629 -- reprocessing at the time the base type does, and also we must set a
13630 -- conditional delay.
13632 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13633 Conditional_Delay (Def_Id, T);
13634 end Constrain_Array;
13636 ------------------------------
13637 -- Constrain_Component_Type --
13638 ------------------------------
13640 function Constrain_Component_Type
13642 Constrained_Typ : Entity_Id;
13643 Related_Node : Node_Id;
13645 Constraints : Elist_Id) return Entity_Id
13647 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13648 Compon_Type : constant Entity_Id := Etype (Comp);
13650 function Build_Constrained_Array_Type
13651 (Old_Type : Entity_Id) return Entity_Id;
13652 -- If Old_Type is an array type, one of whose indexes is constrained
13653 -- by a discriminant, build an Itype whose constraint replaces the
13654 -- discriminant with its value in the constraint.
13656 function Build_Constrained_Discriminated_Type
13657 (Old_Type : Entity_Id) return Entity_Id;
13658 -- Ditto for record components. Handle the case where the constraint
13659 -- is a conversion of the discriminant value, introduced during
13662 function Build_Constrained_Access_Type
13663 (Old_Type : Entity_Id) return Entity_Id;
13664 -- Ditto for access types. Makes use of previous two functions, to
13665 -- constrain designated type.
13667 function Is_Discriminant (Expr : Node_Id) return Boolean;
13668 -- Returns True if Expr is a discriminant
13670 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id;
13671 -- Find the value of a discriminant named by Discr_Expr in Constraints
13673 -----------------------------------
13674 -- Build_Constrained_Access_Type --
13675 -----------------------------------
13677 function Build_Constrained_Access_Type
13678 (Old_Type : Entity_Id) return Entity_Id
13680 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13682 Desig_Subtype : Entity_Id;
13686 -- If the original access type was not embedded in the enclosing
13687 -- type definition, there is no need to produce a new access
13688 -- subtype. In fact every access type with an explicit constraint
13689 -- generates an itype whose scope is the enclosing record.
13691 if not Is_Type (Scope (Old_Type)) then
13694 elsif Is_Array_Type (Desig_Type) then
13695 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13697 elsif Has_Discriminants (Desig_Type) then
13699 -- This may be an access type to an enclosing record type for
13700 -- which we are constructing the constrained components. Return
13701 -- the enclosing record subtype. This is not always correct,
13702 -- but avoids infinite recursion. ???
13704 Desig_Subtype := Any_Type;
13706 for J in reverse 0 .. Scope_Stack.Last loop
13707 Scop := Scope_Stack.Table (J).Entity;
13710 and then Base_Type (Scop) = Base_Type (Desig_Type)
13712 Desig_Subtype := Scop;
13715 exit when not Is_Type (Scop);
13718 if Desig_Subtype = Any_Type then
13720 Build_Constrained_Discriminated_Type (Desig_Type);
13727 if Desig_Subtype /= Desig_Type then
13729 -- The Related_Node better be here or else we won't be able
13730 -- to attach new itypes to a node in the tree.
13732 pragma Assert (Present (Related_Node));
13734 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13736 Set_Etype (Itype, Base_Type (Old_Type));
13737 Set_Size_Info (Itype, (Old_Type));
13738 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13739 Set_Depends_On_Private (Itype, Has_Private_Component
13741 Set_Is_Access_Constant (Itype, Is_Access_Constant
13744 -- The new itype needs freezing when it depends on a not frozen
13745 -- type and the enclosing subtype needs freezing.
13747 if Has_Delayed_Freeze (Constrained_Typ)
13748 and then not Is_Frozen (Constrained_Typ)
13750 Conditional_Delay (Itype, Base_Type (Old_Type));
13758 end Build_Constrained_Access_Type;
13760 ----------------------------------
13761 -- Build_Constrained_Array_Type --
13762 ----------------------------------
13764 function Build_Constrained_Array_Type
13765 (Old_Type : Entity_Id) return Entity_Id
13769 Old_Index : Node_Id;
13770 Range_Node : Node_Id;
13771 Constr_List : List_Id;
13773 Need_To_Create_Itype : Boolean := False;
13776 Old_Index := First_Index (Old_Type);
13777 while Present (Old_Index) loop
13778 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13780 if Is_Discriminant (Lo_Expr)
13782 Is_Discriminant (Hi_Expr)
13784 Need_To_Create_Itype := True;
13788 Next_Index (Old_Index);
13791 if Need_To_Create_Itype then
13792 Constr_List := New_List;
13794 Old_Index := First_Index (Old_Type);
13795 while Present (Old_Index) loop
13796 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13798 if Is_Discriminant (Lo_Expr) then
13799 Lo_Expr := Get_Discr_Value (Lo_Expr);
13802 if Is_Discriminant (Hi_Expr) then
13803 Hi_Expr := Get_Discr_Value (Hi_Expr);
13808 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13810 Append (Range_Node, To => Constr_List);
13812 Next_Index (Old_Index);
13815 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
13820 end Build_Constrained_Array_Type;
13822 ------------------------------------------
13823 -- Build_Constrained_Discriminated_Type --
13824 ------------------------------------------
13826 function Build_Constrained_Discriminated_Type
13827 (Old_Type : Entity_Id) return Entity_Id
13830 Constr_List : List_Id;
13831 Old_Constraint : Elmt_Id;
13833 Need_To_Create_Itype : Boolean := False;
13836 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13837 while Present (Old_Constraint) loop
13838 Expr := Node (Old_Constraint);
13840 if Is_Discriminant (Expr) then
13841 Need_To_Create_Itype := True;
13844 -- After expansion of discriminated task types, the value
13845 -- of the discriminant may be converted to a run-time type
13846 -- for restricted run-times. Propagate the value of the
13847 -- discriminant as well, so that e.g. the secondary stack
13848 -- component has a static constraint. Necessary for LLVM.
13850 elsif Nkind (Expr) = N_Type_Conversion
13851 and then Is_Discriminant (Expression (Expr))
13853 Need_To_Create_Itype := True;
13857 Next_Elmt (Old_Constraint);
13860 if Need_To_Create_Itype then
13861 Constr_List := New_List;
13863 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13864 while Present (Old_Constraint) loop
13865 Expr := Node (Old_Constraint);
13867 if Is_Discriminant (Expr) then
13868 Expr := Get_Discr_Value (Expr);
13870 elsif Nkind (Expr) = N_Type_Conversion
13871 and then Is_Discriminant (Expression (Expr))
13873 Expr := New_Copy_Tree (Expr);
13874 Set_Expression (Expr, Get_Discr_Value (Expression (Expr)));
13877 Append (New_Copy_Tree (Expr), To => Constr_List);
13879 Next_Elmt (Old_Constraint);
13882 return Build_Subtype (Related_Node, Loc, Old_Type, Constr_List);
13887 end Build_Constrained_Discriminated_Type;
13889 ---------------------
13890 -- Get_Discr_Value --
13891 ---------------------
13893 function Get_Discr_Value (Discr_Expr : Node_Id) return Node_Id is
13894 Discr_Id : constant Entity_Id := Entity (Discr_Expr);
13895 -- Entity of a discriminant that appear as a standalone expression in
13896 -- the constraint of a component.
13902 -- The discriminant may be declared for the type, in which case we
13903 -- find it by iterating over the list of discriminants. If the
13904 -- discriminant is inherited from a parent type, it appears as the
13905 -- corresponding discriminant of the current type. This will be the
13906 -- case when constraining an inherited component whose constraint is
13907 -- given by a discriminant of the parent.
13909 D := First_Discriminant (Typ);
13910 E := First_Elmt (Constraints);
13912 while Present (D) loop
13914 or else D = CR_Discriminant (Discr_Id)
13915 or else Corresponding_Discriminant (D) = Discr_Id
13920 Next_Discriminant (D);
13924 -- The Corresponding_Discriminant mechanism is incomplete, because
13925 -- the correspondence between new and old discriminants is not one
13926 -- to one: one new discriminant can constrain several old ones. In
13927 -- that case, scan sequentially the stored_constraint, the list of
13928 -- discriminants of the parents, and the constraints.
13930 -- Previous code checked for the present of the Stored_Constraint
13931 -- list for the derived type, but did not use it at all. Should it
13932 -- be present when the component is a discriminated task type?
13934 if Is_Derived_Type (Typ)
13935 and then Scope (Discr_Id) = Etype (Typ)
13937 D := First_Discriminant (Etype (Typ));
13938 E := First_Elmt (Constraints);
13939 while Present (D) loop
13940 if D = Discr_Id then
13944 Next_Discriminant (D);
13949 -- Something is wrong if we did not find the value
13951 raise Program_Error;
13952 end Get_Discr_Value;
13954 ---------------------
13955 -- Is_Discriminant --
13956 ---------------------
13958 function Is_Discriminant (Expr : Node_Id) return Boolean is
13959 Discrim_Scope : Entity_Id;
13962 if Denotes_Discriminant (Expr) then
13963 Discrim_Scope := Scope (Entity (Expr));
13965 -- Either we have a reference to one of Typ's discriminants,
13967 pragma Assert (Discrim_Scope = Typ
13969 -- or to the discriminants of the parent type, in the case
13970 -- of a derivation of a tagged type with variants.
13972 or else Discrim_Scope = Etype (Typ)
13973 or else Full_View (Discrim_Scope) = Etype (Typ)
13975 -- or same as above for the case where the discriminants
13976 -- were declared in Typ's private view.
13978 or else (Is_Private_Type (Discrim_Scope)
13979 and then Chars (Discrim_Scope) = Chars (Typ))
13981 -- or else we are deriving from the full view and the
13982 -- discriminant is declared in the private entity.
13984 or else (Is_Private_Type (Typ)
13985 and then Chars (Discrim_Scope) = Chars (Typ))
13987 -- Or we are constrained the corresponding record of a
13988 -- synchronized type that completes a private declaration.
13990 or else (Is_Concurrent_Record_Type (Typ)
13992 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13994 -- or we have a class-wide type, in which case make sure the
13995 -- discriminant found belongs to the root type.
13997 or else (Is_Class_Wide_Type (Typ)
13998 and then Etype (Typ) = Discrim_Scope));
14003 -- In all other cases we have something wrong
14006 end Is_Discriminant;
14008 -- Start of processing for Constrain_Component_Type
14011 if Nkind (Parent (Comp)) = N_Component_Declaration
14012 and then Comes_From_Source (Parent (Comp))
14013 and then Comes_From_Source
14014 (Subtype_Indication (Component_Definition (Parent (Comp))))
14017 (Subtype_Indication (Component_Definition (Parent (Comp))))
14019 return Compon_Type;
14021 elsif Is_Array_Type (Compon_Type) then
14022 return Build_Constrained_Array_Type (Compon_Type);
14024 elsif Has_Discriminants (Compon_Type) then
14025 return Build_Constrained_Discriminated_Type (Compon_Type);
14027 elsif Is_Access_Type (Compon_Type) then
14028 return Build_Constrained_Access_Type (Compon_Type);
14031 return Compon_Type;
14033 end Constrain_Component_Type;
14035 --------------------------
14036 -- Constrain_Concurrent --
14037 --------------------------
14039 -- For concurrent types, the associated record value type carries the same
14040 -- discriminants, so when we constrain a concurrent type, we must constrain
14041 -- the corresponding record type as well.
14043 procedure Constrain_Concurrent
14044 (Def_Id : in out Entity_Id;
14046 Related_Nod : Node_Id;
14047 Related_Id : Entity_Id;
14048 Suffix : Character)
14050 -- Retrieve Base_Type to ensure getting to the concurrent type in the
14051 -- case of a private subtype (needed when only doing semantic analysis).
14053 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
14057 if Is_Access_Type (T_Ent) then
14058 T_Ent := Designated_Type (T_Ent);
14061 T_Val := Corresponding_Record_Type (T_Ent);
14063 if Present (T_Val) then
14065 if No (Def_Id) then
14066 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14068 -- Elaborate itype now, as it may be used in a subsequent
14069 -- synchronized operation in another scope.
14071 if Nkind (Related_Nod) = N_Full_Type_Declaration then
14072 Build_Itype_Reference (Def_Id, Related_Nod);
14076 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14077 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
14079 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
14080 Set_Corresponding_Record_Type (Def_Id,
14081 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
14084 -- If there is no associated record, expansion is disabled and this
14085 -- is a generic context. Create a subtype in any case, so that
14086 -- semantic analysis can proceed.
14088 if No (Def_Id) then
14089 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
14092 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
14094 end Constrain_Concurrent;
14096 ------------------------------------
14097 -- Constrain_Corresponding_Record --
14098 ------------------------------------
14100 function Constrain_Corresponding_Record
14101 (Prot_Subt : Entity_Id;
14102 Corr_Rec : Entity_Id;
14103 Related_Nod : Node_Id) return Entity_Id
14105 T_Sub : constant Entity_Id :=
14107 (Ekind => E_Record_Subtype,
14108 Related_Nod => Related_Nod,
14109 Related_Id => Corr_Rec,
14111 Suffix_Index => -1);
14114 Set_Etype (T_Sub, Corr_Rec);
14115 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
14116 Set_Is_Tagged_Type (T_Sub, Is_Tagged_Type (Corr_Rec));
14117 Set_Is_Constrained (T_Sub, True);
14118 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
14119 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
14121 if Has_Discriminants (Prot_Subt) then -- False only if errors.
14122 Set_Discriminant_Constraint
14123 (T_Sub, Discriminant_Constraint (Prot_Subt));
14124 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
14125 Create_Constrained_Components
14126 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
14129 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
14131 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
14132 Conditional_Delay (T_Sub, Corr_Rec);
14135 -- This is a component subtype: it will be frozen in the context of
14136 -- the enclosing record's init_proc, so that discriminant references
14137 -- are resolved to discriminals. (Note: we used to skip freezing
14138 -- altogether in that case, which caused errors downstream for
14139 -- components of a bit packed array type).
14141 Set_Has_Delayed_Freeze (T_Sub);
14145 end Constrain_Corresponding_Record;
14147 -----------------------
14148 -- Constrain_Decimal --
14149 -----------------------
14151 procedure Constrain_Decimal (Def_Id : Entity_Id; S : Node_Id) is
14152 T : constant Entity_Id := Entity (Subtype_Mark (S));
14153 C : constant Node_Id := Constraint (S);
14154 Loc : constant Source_Ptr := Sloc (C);
14155 Range_Expr : Node_Id;
14156 Digits_Expr : Node_Id;
14161 Mutate_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
14163 if Nkind (C) = N_Range_Constraint then
14164 Range_Expr := Range_Expression (C);
14165 Digits_Val := Digits_Value (T);
14168 pragma Assert (Nkind (C) = N_Digits_Constraint);
14170 Digits_Expr := Digits_Expression (C);
14171 Analyze_And_Resolve (Digits_Expr, Any_Integer);
14173 Check_Digits_Expression (Digits_Expr);
14174 Digits_Val := Expr_Value (Digits_Expr);
14176 if Digits_Val > Digits_Value (T) then
14178 ("digits expression is incompatible with subtype", C);
14179 Digits_Val := Digits_Value (T);
14182 if Present (Range_Constraint (C)) then
14183 Range_Expr := Range_Expression (Range_Constraint (C));
14185 Range_Expr := Empty;
14189 Set_Etype (Def_Id, Base_Type (T));
14190 Set_Size_Info (Def_Id, (T));
14191 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14192 Set_Delta_Value (Def_Id, Delta_Value (T));
14193 Set_Scale_Value (Def_Id, Scale_Value (T));
14194 Set_Small_Value (Def_Id, Small_Value (T));
14195 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
14196 Set_Digits_Value (Def_Id, Digits_Val);
14198 -- Manufacture range from given digits value if no range present
14200 if No (Range_Expr) then
14201 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
14205 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
14207 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
14210 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
14211 Set_Discrete_RM_Size (Def_Id);
14213 -- Unconditionally delay the freeze, since we cannot set size
14214 -- information in all cases correctly until the freeze point.
14216 Set_Has_Delayed_Freeze (Def_Id);
14217 end Constrain_Decimal;
14219 ----------------------------------
14220 -- Constrain_Discriminated_Type --
14221 ----------------------------------
14223 procedure Constrain_Discriminated_Type
14224 (Def_Id : Entity_Id;
14226 Related_Nod : Node_Id;
14227 For_Access : Boolean := False)
14229 E : Entity_Id := Entity (Subtype_Mark (S));
14232 procedure Fixup_Bad_Constraint;
14233 -- Called after finding a bad constraint, and after having posted an
14234 -- appropriate error message. The goal is to leave type Def_Id in as
14235 -- reasonable state as possible.
14237 --------------------------
14238 -- Fixup_Bad_Constraint --
14239 --------------------------
14241 procedure Fixup_Bad_Constraint is
14243 -- Set a reasonable Ekind for the entity, including incomplete types.
14245 Mutate_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
14247 -- Set Etype to the known type, to reduce chances of cascaded errors
14249 Set_Etype (Def_Id, E);
14250 Set_Error_Posted (Def_Id);
14251 end Fixup_Bad_Constraint;
14256 Constr : Elist_Id := New_Elmt_List;
14258 -- Start of processing for Constrain_Discriminated_Type
14261 C := Constraint (S);
14263 -- A discriminant constraint is only allowed in a subtype indication,
14264 -- after a subtype mark. This subtype mark must denote either a type
14265 -- with discriminants, or an access type whose designated type is a
14266 -- type with discriminants. A discriminant constraint specifies the
14267 -- values of these discriminants (RM 3.7.2(5)).
14269 T := Base_Type (Entity (Subtype_Mark (S)));
14271 if Is_Access_Type (T) then
14272 T := Designated_Type (T);
14275 -- In an instance it may be necessary to retrieve the full view of a
14276 -- type with unknown discriminants, or a full view with defaulted
14277 -- discriminants. In other contexts the constraint is illegal.
14280 and then Is_Private_Type (T)
14281 and then Present (Full_View (T))
14283 (Has_Unknown_Discriminants (T)
14285 (not Has_Discriminants (T)
14286 and then Has_Defaulted_Discriminants (Full_View (T))))
14288 T := Full_View (T);
14289 E := Full_View (E);
14292 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
14293 -- generating an error for access-to-incomplete subtypes.
14295 if Ada_Version >= Ada_2005
14296 and then Ekind (T) = E_Incomplete_Type
14297 and then Nkind (Parent (S)) = N_Subtype_Declaration
14298 and then not Is_Itype (Def_Id)
14300 -- A little sanity check: emit an error message if the type has
14301 -- discriminants to begin with. Type T may be a regular incomplete
14302 -- type or imported via a limited with clause.
14304 if Has_Discriminants (T)
14305 or else (From_Limited_With (T)
14306 and then Present (Non_Limited_View (T))
14307 and then Nkind (Parent (Non_Limited_View (T))) =
14308 N_Full_Type_Declaration
14309 and then Present (Discriminant_Specifications
14310 (Parent (Non_Limited_View (T)))))
14313 ("(Ada 2005) incomplete subtype may not be constrained", C);
14315 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14318 Fixup_Bad_Constraint;
14321 -- Check that the type has visible discriminants. The type may be
14322 -- a private type with unknown discriminants whose full view has
14323 -- discriminants which are invisible.
14325 elsif not Has_Discriminants (T)
14327 (Has_Unknown_Discriminants (T)
14328 and then Is_Private_Type (T))
14330 Error_Msg_N ("invalid constraint: type has no discriminant", C);
14331 Fixup_Bad_Constraint;
14334 elsif Is_Constrained (E)
14335 or else (Ekind (E) = E_Class_Wide_Subtype
14336 and then Present (Discriminant_Constraint (E)))
14338 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
14339 Fixup_Bad_Constraint;
14343 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
14344 -- applies to the base type.
14346 T := Base_Type (T);
14348 Constr := Build_Discriminant_Constraints (T, S);
14350 -- If the list returned was empty we had an error in building the
14351 -- discriminant constraint. We have also already signalled an error
14352 -- in the incomplete type case
14354 if Is_Empty_Elmt_List (Constr) then
14355 Fixup_Bad_Constraint;
14359 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
14360 end Constrain_Discriminated_Type;
14362 ---------------------------
14363 -- Constrain_Enumeration --
14364 ---------------------------
14366 procedure Constrain_Enumeration (Def_Id : Entity_Id; S : Node_Id) is
14367 T : constant Entity_Id := Entity (Subtype_Mark (S));
14368 C : constant Node_Id := Constraint (S);
14371 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14373 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
14375 Set_Etype (Def_Id, Base_Type (T));
14376 Set_Size_Info (Def_Id, (T));
14377 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14378 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14380 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14382 Set_Discrete_RM_Size (Def_Id);
14383 end Constrain_Enumeration;
14385 ----------------------
14386 -- Constrain_Float --
14387 ----------------------
14389 procedure Constrain_Float (Def_Id : Entity_Id; S : Node_Id) is
14390 T : constant Entity_Id := Entity (Subtype_Mark (S));
14396 Mutate_Ekind (Def_Id, E_Floating_Point_Subtype);
14398 Set_Etype (Def_Id, Base_Type (T));
14399 Set_Size_Info (Def_Id, (T));
14400 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14402 -- Process the constraint
14404 C := Constraint (S);
14406 -- Digits constraint present
14408 if Nkind (C) = N_Digits_Constraint then
14409 Check_Restriction (No_Obsolescent_Features, C);
14411 if Warn_On_Obsolescent_Feature then
14413 ("subtype digits constraint is an " &
14414 "obsolescent feature (RM J.3(8))?j?", C);
14417 D := Digits_Expression (C);
14418 Analyze_And_Resolve (D, Any_Integer);
14419 Check_Digits_Expression (D);
14420 Set_Digits_Value (Def_Id, Expr_Value (D));
14422 -- Check that digits value is in range. Obviously we can do this
14423 -- at compile time, but it is strictly a runtime check, and of
14424 -- course there is an ACVC test that checks this.
14426 if Digits_Value (Def_Id) > Digits_Value (T) then
14427 Error_Msg_Uint_1 := Digits_Value (T);
14428 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14430 Make_Raise_Constraint_Error (Sloc (D),
14431 Reason => CE_Range_Check_Failed);
14432 Insert_Action (Declaration_Node (Def_Id), Rais);
14435 C := Range_Constraint (C);
14437 -- No digits constraint present
14440 Set_Digits_Value (Def_Id, Digits_Value (T));
14443 -- Range constraint present
14445 if Nkind (C) = N_Range_Constraint then
14446 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14448 -- No range constraint present
14451 pragma Assert (No (C));
14452 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14455 Set_Is_Constrained (Def_Id);
14456 end Constrain_Float;
14458 ---------------------
14459 -- Constrain_Index --
14460 ---------------------
14462 procedure Constrain_Index
14465 Related_Nod : Node_Id;
14466 Related_Id : Entity_Id;
14467 Suffix : Character;
14468 Suffix_Index : Pos)
14470 Def_Id : Entity_Id;
14471 R : Node_Id := Empty;
14472 T : constant Entity_Id := Etype (Index);
14473 Is_FLB_Index : Boolean := False;
14477 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14478 Set_Etype (Def_Id, Base_Type (T));
14480 if Nkind (S) = N_Range
14482 (Nkind (S) = N_Attribute_Reference
14483 and then Attribute_Name (S) = Name_Range)
14485 -- A Range attribute will be transformed into N_Range by Resolve
14487 -- If a range has an Empty upper bound, then remember that for later
14488 -- setting of the index subtype's Is_Fixed_Lower_Bound_Index_Subtype
14489 -- flag, and also set the upper bound of the range to the index
14490 -- subtype's upper bound rather than leaving it Empty. In truth,
14491 -- that upper bound corresponds to a box ("<>"), but it's convenient
14492 -- to set it to the upper bound to avoid needing to add special tests
14493 -- in various places for an Empty upper bound, and in any case it
14494 -- accurately characterizes the index's range of values.
14496 if Nkind (S) = N_Range and then not Present (High_Bound (S)) then
14497 Is_FLB_Index := True;
14498 Set_High_Bound (S, Type_High_Bound (T));
14503 Process_Range_Expr_In_Decl (R, T);
14505 if not Error_Posted (S)
14507 (Nkind (S) /= N_Range
14508 or else not Covers (T, (Etype (Low_Bound (S))))
14509 or else not Covers (T, (Etype (High_Bound (S)))))
14511 if Base_Type (T) /= Any_Type
14512 and then Etype (Low_Bound (S)) /= Any_Type
14513 and then Etype (High_Bound (S)) /= Any_Type
14515 Error_Msg_N ("range expected", S);
14519 elsif Nkind (S) = N_Subtype_Indication then
14521 -- The parser has verified that this is a discrete indication
14523 Resolve_Discrete_Subtype_Indication (S, T);
14524 Bad_Predicated_Subtype_Use
14525 ("subtype& has predicate, not allowed in index constraint",
14526 S, Entity (Subtype_Mark (S)));
14528 R := Range_Expression (Constraint (S));
14530 -- Capture values of bounds and generate temporaries for them if
14531 -- needed, since checks may cause duplication of the expressions
14532 -- which must not be reevaluated.
14534 -- The forced evaluation removes side effects from expressions, which
14535 -- should occur also in GNATprove mode. Otherwise, we end up with
14536 -- unexpected insertions of actions at places where this is not
14537 -- supposed to occur, e.g. on default parameters of a call.
14539 if Expander_Active or GNATprove_Mode then
14541 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14543 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14546 elsif Nkind (S) = N_Discriminant_Association then
14548 -- Syntactically valid in subtype indication
14550 Error_Msg_N ("invalid index constraint", S);
14551 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14554 -- Subtype_Mark case, no anonymous subtypes to construct
14559 if Is_Entity_Name (S) then
14560 if not Is_Type (Entity (S)) then
14561 Error_Msg_N ("expect subtype mark for index constraint", S);
14563 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14564 Wrong_Type (S, Base_Type (T));
14566 -- Check error of subtype with predicate in index constraint
14569 Bad_Predicated_Subtype_Use
14570 ("subtype& has predicate, not allowed in index constraint",
14577 Error_Msg_N ("invalid index constraint", S);
14578 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14583 -- Complete construction of the Itype
14585 if Is_Modular_Integer_Type (T) then
14586 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14588 elsif Is_Integer_Type (T) then
14589 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14592 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
14593 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14594 Set_First_Literal (Def_Id, First_Literal (T));
14597 Set_Size_Info (Def_Id, (T));
14598 Copy_RM_Size (To => Def_Id, From => T);
14599 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14601 -- If this is a range for a fixed-lower-bound subtype, then set the
14602 -- index itype's low bound to the FLB and the index itype's upper bound
14603 -- to the high bound of the parent array type's index subtype. Also,
14604 -- mark the itype as an FLB index subtype.
14606 if Nkind (S) = N_Range and then Is_FLB_Index then
14609 Make_Range (Sloc (S),
14610 Low_Bound => Low_Bound (S),
14611 High_Bound => Type_High_Bound (T)));
14612 Set_Is_Fixed_Lower_Bound_Index_Subtype (Def_Id);
14615 Set_Scalar_Range (Def_Id, R);
14618 Set_Etype (S, Def_Id);
14619 Set_Discrete_RM_Size (Def_Id);
14620 end Constrain_Index;
14622 -----------------------
14623 -- Constrain_Integer --
14624 -----------------------
14626 procedure Constrain_Integer (Def_Id : Entity_Id; S : Node_Id) is
14627 T : constant Entity_Id := Entity (Subtype_Mark (S));
14628 C : constant Node_Id := Constraint (S);
14631 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14633 if Is_Modular_Integer_Type (T) then
14634 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
14636 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
14639 Set_Etype (Def_Id, Base_Type (T));
14640 Set_Size_Info (Def_Id, (T));
14641 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14642 Set_Discrete_RM_Size (Def_Id);
14643 end Constrain_Integer;
14645 ------------------------------
14646 -- Constrain_Ordinary_Fixed --
14647 ------------------------------
14649 procedure Constrain_Ordinary_Fixed (Def_Id : Entity_Id; S : Node_Id) is
14650 T : constant Entity_Id := Entity (Subtype_Mark (S));
14656 Mutate_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14657 Set_Etype (Def_Id, Base_Type (T));
14658 Set_Size_Info (Def_Id, (T));
14659 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14660 Set_Small_Value (Def_Id, Small_Value (T));
14662 -- Process the constraint
14664 C := Constraint (S);
14666 -- Delta constraint present
14668 if Nkind (C) = N_Delta_Constraint then
14669 Check_Restriction (No_Obsolescent_Features, C);
14671 if Warn_On_Obsolescent_Feature then
14673 ("subtype delta constraint is an " &
14674 "obsolescent feature (RM J.3(7))?j?");
14677 D := Delta_Expression (C);
14678 Analyze_And_Resolve (D, Any_Real);
14679 Check_Delta_Expression (D);
14680 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14682 -- Check that delta value is in range. Obviously we can do this
14683 -- at compile time, but it is strictly a runtime check, and of
14684 -- course there is an ACVC test that checks this.
14686 if Delta_Value (Def_Id) < Delta_Value (T) then
14687 Error_Msg_N ("??delta value is too small", D);
14689 Make_Raise_Constraint_Error (Sloc (D),
14690 Reason => CE_Range_Check_Failed);
14691 Insert_Action (Declaration_Node (Def_Id), Rais);
14694 C := Range_Constraint (C);
14696 -- No delta constraint present
14699 Set_Delta_Value (Def_Id, Delta_Value (T));
14702 -- Range constraint present
14704 if Nkind (C) = N_Range_Constraint then
14705 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14707 -- No range constraint present
14710 pragma Assert (No (C));
14711 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14714 Set_Discrete_RM_Size (Def_Id);
14716 -- Unconditionally delay the freeze, since we cannot set size
14717 -- information in all cases correctly until the freeze point.
14719 Set_Has_Delayed_Freeze (Def_Id);
14720 end Constrain_Ordinary_Fixed;
14722 -----------------------
14723 -- Contain_Interface --
14724 -----------------------
14726 function Contain_Interface
14727 (Iface : Entity_Id;
14728 Ifaces : Elist_Id) return Boolean
14730 Iface_Elmt : Elmt_Id;
14733 if Present (Ifaces) then
14734 Iface_Elmt := First_Elmt (Ifaces);
14735 while Present (Iface_Elmt) loop
14736 if Node (Iface_Elmt) = Iface then
14740 Next_Elmt (Iface_Elmt);
14745 end Contain_Interface;
14747 ---------------------------
14748 -- Convert_Scalar_Bounds --
14749 ---------------------------
14751 procedure Convert_Scalar_Bounds
14753 Parent_Type : Entity_Id;
14754 Derived_Type : Entity_Id;
14757 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14764 -- Defend against previous errors
14766 if No (Scalar_Range (Derived_Type)) then
14767 Check_Error_Detected;
14771 Lo := Build_Scalar_Bound
14772 (Type_Low_Bound (Derived_Type),
14773 Parent_Type, Implicit_Base);
14775 Hi := Build_Scalar_Bound
14776 (Type_High_Bound (Derived_Type),
14777 Parent_Type, Implicit_Base);
14784 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14786 Set_Parent (Rng, N);
14787 Set_Scalar_Range (Derived_Type, Rng);
14789 -- Analyze the bounds
14791 Analyze_And_Resolve (Lo, Implicit_Base);
14792 Analyze_And_Resolve (Hi, Implicit_Base);
14794 -- Analyze the range itself, except that we do not analyze it if
14795 -- the bounds are real literals, and we have a fixed-point type.
14796 -- The reason for this is that we delay setting the bounds in this
14797 -- case till we know the final Small and Size values (see circuit
14798 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14800 if Is_Fixed_Point_Type (Parent_Type)
14801 and then Nkind (Lo) = N_Real_Literal
14802 and then Nkind (Hi) = N_Real_Literal
14806 -- Here we do the analysis of the range
14808 -- Note: we do this manually, since if we do a normal Analyze and
14809 -- Resolve call, there are problems with the conversions used for
14810 -- the derived type range.
14813 Set_Etype (Rng, Implicit_Base);
14814 Set_Analyzed (Rng, True);
14816 end Convert_Scalar_Bounds;
14818 -------------------
14819 -- Copy_And_Swap --
14820 -------------------
14822 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14824 -- Initialize new full declaration entity by copying the pertinent
14825 -- fields of the corresponding private declaration entity.
14827 -- We temporarily set Ekind to a value appropriate for a type to
14828 -- avoid assert failures in Einfo from checking for setting type
14829 -- attributes on something that is not a type. Ekind (Priv) is an
14830 -- appropriate choice, since it allowed the attributes to be set
14831 -- in the first place. This Ekind value will be modified later.
14833 Mutate_Ekind (Full, Ekind (Priv));
14835 -- Also set Etype temporarily to Any_Type, again, in the absence
14836 -- of errors, it will be properly reset, and if there are errors,
14837 -- then we want a value of Any_Type to remain.
14839 Set_Etype (Full, Any_Type);
14841 -- Now start copying attributes
14843 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14845 if Has_Discriminants (Full) then
14846 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14847 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14850 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14851 Set_Homonym (Full, Homonym (Priv));
14852 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14853 Set_Is_Public (Full, Is_Public (Priv));
14854 Set_Is_Pure (Full, Is_Pure (Priv));
14855 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14856 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14857 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14858 Set_Has_Pragma_Unreferenced_Objects
14859 (Full, Has_Pragma_Unreferenced_Objects
14862 Conditional_Delay (Full, Priv);
14864 if Is_Tagged_Type (Full) then
14865 Set_Direct_Primitive_Operations
14866 (Full, Direct_Primitive_Operations (Priv));
14867 Set_No_Tagged_Streams_Pragma
14868 (Full, No_Tagged_Streams_Pragma (Priv));
14870 if Is_Base_Type (Priv) then
14871 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14875 Set_Is_Volatile (Full, Is_Volatile (Priv));
14876 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14877 Set_Scope (Full, Scope (Priv));
14878 Set_Prev_Entity (Full, Prev_Entity (Priv));
14879 Set_Next_Entity (Full, Next_Entity (Priv));
14880 Set_First_Entity (Full, First_Entity (Priv));
14881 Set_Last_Entity (Full, Last_Entity (Priv));
14883 -- If access types have been recorded for later handling, keep them in
14884 -- the full view so that they get handled when the full view freeze
14885 -- node is expanded.
14887 if Present (Freeze_Node (Priv))
14888 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14890 Ensure_Freeze_Node (Full);
14891 Set_Access_Types_To_Process
14892 (Freeze_Node (Full),
14893 Access_Types_To_Process (Freeze_Node (Priv)));
14896 -- Swap the two entities. Now Private is the full type entity and Full
14897 -- is the private one. They will be swapped back at the end of the
14898 -- private part. This swapping ensures that the entity that is visible
14899 -- in the private part is the full declaration.
14901 Exchange_Entities (Priv, Full);
14902 Append_Entity (Full, Scope (Full));
14905 -------------------------------------
14906 -- Copy_Array_Base_Type_Attributes --
14907 -------------------------------------
14909 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14911 Set_Component_Alignment (T1, Component_Alignment (T2));
14912 Set_Component_Type (T1, Component_Type (T2));
14913 Set_Component_Size (T1, Component_Size (T2));
14914 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14915 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14916 Propagate_Concurrent_Flags (T1, T2);
14917 Set_Is_Packed (T1, Is_Packed (T2));
14918 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14919 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14920 Set_Has_Independent_Components (T1, Has_Independent_Components (T2));
14921 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14922 end Copy_Array_Base_Type_Attributes;
14924 -----------------------------------
14925 -- Copy_Array_Subtype_Attributes --
14926 -----------------------------------
14928 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14930 Set_Size_Info (T1, T2);
14932 Set_First_Index (T1, First_Index (T2));
14933 Set_Is_Aliased (T1, Is_Aliased (T2));
14934 Set_Is_Atomic (T1, Is_Atomic (T2));
14935 Set_Is_Independent (T1, Is_Independent (T2));
14936 Set_Is_Volatile (T1, Is_Volatile (T2));
14937 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
14938 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14939 Set_Is_Constrained (T1, Is_Constrained (T2));
14940 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14941 Inherit_Rep_Item_Chain (T1, T2);
14942 Set_Convention (T1, Convention (T2));
14943 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14944 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14945 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14946 end Copy_Array_Subtype_Attributes;
14948 -----------------------------------
14949 -- Create_Constrained_Components --
14950 -----------------------------------
14952 procedure Create_Constrained_Components
14954 Decl_Node : Node_Id;
14956 Constraints : Elist_Id)
14958 Loc : constant Source_Ptr := Sloc (Subt);
14959 Comp_List : constant Elist_Id := New_Elmt_List;
14960 Parent_Type : constant Entity_Id := Etype (Typ);
14961 Assoc_List : constant List_Id := New_List;
14963 Discr_Val : Elmt_Id;
14967 Is_Static : Boolean := True;
14968 Is_Compile_Time_Known : Boolean := True;
14970 procedure Collect_Fixed_Components (Typ : Entity_Id);
14971 -- Collect parent type components that do not appear in a variant part
14973 procedure Create_All_Components;
14974 -- Iterate over Comp_List to create the components of the subtype
14976 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14977 -- Creates a new component from Old_Compon, copying all the fields from
14978 -- it, including its Etype, inserts the new component in the Subt entity
14979 -- chain and returns the new component.
14981 function Is_Variant_Record (T : Entity_Id) return Boolean;
14982 -- If true, and discriminants are static, collect only components from
14983 -- variants selected by discriminant values.
14985 ------------------------------
14986 -- Collect_Fixed_Components --
14987 ------------------------------
14989 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14991 -- Build association list for discriminants, and find components of the
14992 -- variant part selected by the values of the discriminants.
14994 Old_C := First_Discriminant (Typ);
14995 Discr_Val := First_Elmt (Constraints);
14996 while Present (Old_C) loop
14997 Append_To (Assoc_List,
14998 Make_Component_Association (Loc,
14999 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
15000 Expression => New_Copy (Node (Discr_Val))));
15002 Next_Elmt (Discr_Val);
15003 Next_Discriminant (Old_C);
15006 -- The tag and the possible parent component are unconditionally in
15009 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
15010 Old_C := First_Component (Typ);
15011 while Present (Old_C) loop
15012 if Chars (Old_C) in Name_uTag | Name_uParent then
15013 Append_Elmt (Old_C, Comp_List);
15016 Next_Component (Old_C);
15019 end Collect_Fixed_Components;
15021 ---------------------------
15022 -- Create_All_Components --
15023 ---------------------------
15025 procedure Create_All_Components is
15029 Comp := First_Elmt (Comp_List);
15030 while Present (Comp) loop
15031 Old_C := Node (Comp);
15032 New_C := Create_Component (Old_C);
15036 Constrain_Component_Type
15037 (Old_C, Subt, Decl_Node, Typ, Constraints));
15038 Set_Is_Public (New_C, Is_Public (Subt));
15042 end Create_All_Components;
15044 ----------------------
15045 -- Create_Component --
15046 ----------------------
15048 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
15049 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
15052 if Ekind (Old_Compon) = E_Discriminant
15053 and then Is_Completely_Hidden (Old_Compon)
15055 -- This is a shadow discriminant created for a discriminant of
15056 -- the parent type, which needs to be present in the subtype.
15057 -- Give the shadow discriminant an internal name that cannot
15058 -- conflict with that of visible components.
15060 Set_Chars (New_Compon, New_Internal_Name ('C'));
15063 -- Set the parent so we have a proper link for freezing etc. This is
15064 -- not a real parent pointer, since of course our parent does not own
15065 -- up to us and reference us, we are an illegitimate child of the
15066 -- original parent.
15068 Set_Parent (New_Compon, Parent (Old_Compon));
15070 -- We do not want this node marked as Comes_From_Source, since
15071 -- otherwise it would get first class status and a separate cross-
15072 -- reference line would be generated. Illegitimate children do not
15073 -- rate such recognition.
15075 Set_Comes_From_Source (New_Compon, False);
15077 -- But it is a real entity, and a birth certificate must be properly
15078 -- registered by entering it into the entity list, and setting its
15079 -- scope to the given subtype. This turns out to be useful for the
15080 -- LLVM code generator, but that scope is not used otherwise.
15082 Enter_Name (New_Compon);
15083 Set_Scope (New_Compon, Subt);
15086 end Create_Component;
15088 -----------------------
15089 -- Is_Variant_Record --
15090 -----------------------
15092 function Is_Variant_Record (T : Entity_Id) return Boolean is
15094 return Nkind (Parent (T)) = N_Full_Type_Declaration
15095 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
15096 and then Present (Component_List (Type_Definition (Parent (T))))
15099 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
15100 end Is_Variant_Record;
15102 -- Start of processing for Create_Constrained_Components
15105 pragma Assert (Subt /= Base_Type (Subt));
15106 pragma Assert (Typ = Base_Type (Typ));
15108 Set_First_Entity (Subt, Empty);
15109 Set_Last_Entity (Subt, Empty);
15111 -- Check whether constraint is fully static, in which case we can
15112 -- optimize the list of components.
15114 Discr_Val := First_Elmt (Constraints);
15115 while Present (Discr_Val) loop
15116 if not Is_OK_Static_Expression (Node (Discr_Val)) then
15117 Is_Static := False;
15119 if not Compile_Time_Known_Value (Node (Discr_Val)) then
15120 Is_Compile_Time_Known := False;
15125 Next_Elmt (Discr_Val);
15128 Set_Has_Static_Discriminants (Subt, Is_Static);
15132 -- Inherit the discriminants of the parent type
15134 Add_Discriminants : declare
15140 Old_C := First_Discriminant (Typ);
15142 while Present (Old_C) loop
15143 Num_Disc := Num_Disc + 1;
15144 New_C := Create_Component (Old_C);
15145 Set_Is_Public (New_C, Is_Public (Subt));
15146 Next_Discriminant (Old_C);
15149 -- For an untagged derived subtype, the number of discriminants may
15150 -- be smaller than the number of inherited discriminants, because
15151 -- several of them may be renamed by a single new discriminant or
15152 -- constrained. In this case, add the hidden discriminants back into
15153 -- the subtype, because they need to be present if the optimizer of
15154 -- the GCC 4.x back-end decides to break apart assignments between
15155 -- objects using the parent view into member-wise assignments.
15159 if Is_Derived_Type (Typ)
15160 and then not Is_Tagged_Type (Typ)
15162 Old_C := First_Stored_Discriminant (Typ);
15164 while Present (Old_C) loop
15165 Num_Gird := Num_Gird + 1;
15166 Next_Stored_Discriminant (Old_C);
15170 if Num_Gird > Num_Disc then
15172 -- Find out multiple uses of new discriminants, and add hidden
15173 -- components for the extra renamed discriminants. We recognize
15174 -- multiple uses through the Corresponding_Discriminant of a
15175 -- new discriminant: if it constrains several old discriminants,
15176 -- this field points to the last one in the parent type. The
15177 -- stored discriminants of the derived type have the same name
15178 -- as those of the parent.
15182 New_Discr : Entity_Id;
15183 Old_Discr : Entity_Id;
15186 Constr := First_Elmt (Stored_Constraint (Typ));
15187 Old_Discr := First_Stored_Discriminant (Typ);
15188 while Present (Constr) loop
15189 if Is_Entity_Name (Node (Constr))
15190 and then Ekind (Entity (Node (Constr))) = E_Discriminant
15192 New_Discr := Entity (Node (Constr));
15194 if Chars (Corresponding_Discriminant (New_Discr)) /=
15197 -- The new discriminant has been used to rename a
15198 -- subsequent old discriminant. Introduce a shadow
15199 -- component for the current old discriminant.
15201 New_C := Create_Component (Old_Discr);
15202 Set_Original_Record_Component (New_C, Old_Discr);
15206 -- The constraint has eliminated the old discriminant.
15207 -- Introduce a shadow component.
15209 New_C := Create_Component (Old_Discr);
15210 Set_Original_Record_Component (New_C, Old_Discr);
15213 Next_Elmt (Constr);
15214 Next_Stored_Discriminant (Old_Discr);
15218 end Add_Discriminants;
15220 if Is_Compile_Time_Known
15221 and then Is_Variant_Record (Typ)
15223 Collect_Fixed_Components (Typ);
15226 Component_List (Type_Definition (Parent (Typ))),
15227 Governed_By => Assoc_List,
15229 Report_Errors => Errors,
15230 Allow_Compile_Time => True);
15231 pragma Assert (not Errors or else Serious_Errors_Detected > 0);
15233 Create_All_Components;
15235 -- If the subtype declaration is created for a tagged type derivation
15236 -- with constraints, we retrieve the record definition of the parent
15237 -- type to select the components of the proper variant.
15239 elsif Is_Compile_Time_Known
15240 and then Is_Tagged_Type (Typ)
15241 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
15243 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
15244 and then Is_Variant_Record (Parent_Type)
15246 Collect_Fixed_Components (Typ);
15249 Component_List (Type_Definition (Parent (Parent_Type))),
15250 Governed_By => Assoc_List,
15252 Report_Errors => Errors,
15253 Allow_Compile_Time => True);
15255 -- Note: previously there was a check at this point that no errors
15256 -- were detected. As a consequence of AI05-220 there may be an error
15257 -- if an inherited discriminant that controls a variant has a non-
15258 -- static constraint.
15260 -- If the tagged derivation has a type extension, collect all the
15261 -- new relevant components therein via Gather_Components.
15263 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
15268 (Record_Extension_Part (Type_Definition (Parent (Typ)))),
15269 Governed_By => Assoc_List,
15271 Report_Errors => Errors,
15272 Allow_Compile_Time => True,
15273 Include_Interface_Tag => True);
15276 Create_All_Components;
15279 -- If discriminants are not static, or if this is a multi-level type
15280 -- extension, we have to include all components of the parent type.
15282 Old_C := First_Component (Typ);
15283 while Present (Old_C) loop
15284 New_C := Create_Component (Old_C);
15288 Constrain_Component_Type
15289 (Old_C, Subt, Decl_Node, Typ, Constraints));
15290 Set_Is_Public (New_C, Is_Public (Subt));
15292 Next_Component (Old_C);
15297 end Create_Constrained_Components;
15299 ------------------------------------------
15300 -- Decimal_Fixed_Point_Type_Declaration --
15301 ------------------------------------------
15303 procedure Decimal_Fixed_Point_Type_Declaration
15307 Loc : constant Source_Ptr := Sloc (Def);
15308 Digs_Expr : constant Node_Id := Digits_Expression (Def);
15309 Delta_Expr : constant Node_Id := Delta_Expression (Def);
15310 Max_Digits : constant Nat :=
15311 (if System_Max_Integer_Size = 128 then 38 else 18);
15312 -- Maximum number of digits that can be represented in an integer
15314 Implicit_Base : Entity_Id;
15321 Check_Restriction (No_Fixed_Point, Def);
15323 -- Create implicit base type
15326 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
15327 Set_Etype (Implicit_Base, Implicit_Base);
15329 -- Analyze and process delta expression
15331 Analyze_And_Resolve (Delta_Expr, Universal_Real);
15333 Check_Delta_Expression (Delta_Expr);
15334 Delta_Val := Expr_Value_R (Delta_Expr);
15336 -- Check delta is power of 10, and determine scale value from it
15342 Scale_Val := Uint_0;
15345 if Val < Ureal_1 then
15346 while Val < Ureal_1 loop
15347 Val := Val * Ureal_10;
15348 Scale_Val := Scale_Val + 1;
15351 if Scale_Val > Max_Digits then
15352 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15353 Error_Msg_N ("scale exceeds maximum value of ^", Def);
15354 Scale_Val := UI_From_Int (Max_Digits);
15358 while Val > Ureal_1 loop
15359 Val := Val / Ureal_10;
15360 Scale_Val := Scale_Val - 1;
15363 if Scale_Val < -Max_Digits then
15364 Error_Msg_Uint_1 := UI_From_Int (-Max_Digits);
15365 Error_Msg_N ("scale is less than minimum value of ^", Def);
15366 Scale_Val := UI_From_Int (-Max_Digits);
15370 if Val /= Ureal_1 then
15371 Error_Msg_N ("delta expression must be a power of 10", Def);
15372 Delta_Val := Ureal_10 ** (-Scale_Val);
15376 -- Set delta, scale and small (small = delta for decimal type)
15378 Set_Delta_Value (Implicit_Base, Delta_Val);
15379 Set_Scale_Value (Implicit_Base, Scale_Val);
15380 Set_Small_Value (Implicit_Base, Delta_Val);
15382 -- Analyze and process digits expression
15384 Analyze_And_Resolve (Digs_Expr, Any_Integer);
15385 Check_Digits_Expression (Digs_Expr);
15386 Digs_Val := Expr_Value (Digs_Expr);
15388 if Digs_Val > Max_Digits then
15389 Error_Msg_Uint_1 := UI_From_Int (Max_Digits);
15390 Error_Msg_N ("digits value out of range, maximum is ^", Digs_Expr);
15391 Digs_Val := UI_From_Int (Max_Digits);
15394 Set_Digits_Value (Implicit_Base, Digs_Val);
15395 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
15397 -- Set range of base type from digits value for now. This will be
15398 -- expanded to represent the true underlying base range by Freeze.
15400 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
15402 -- Note: We leave Esize unset for now, size will be set at freeze
15403 -- time. We have to do this for ordinary fixed-point, because the size
15404 -- depends on the specified small, and we might as well do the same for
15405 -- decimal fixed-point.
15407 pragma Assert (not Known_Esize (Implicit_Base));
15409 -- If there are bounds given in the declaration use them as the
15410 -- bounds of the first named subtype.
15412 if Present (Real_Range_Specification (Def)) then
15414 RRS : constant Node_Id := Real_Range_Specification (Def);
15415 Low : constant Node_Id := Low_Bound (RRS);
15416 High : constant Node_Id := High_Bound (RRS);
15421 Analyze_And_Resolve (Low, Any_Real);
15422 Analyze_And_Resolve (High, Any_Real);
15423 Check_Real_Bound (Low);
15424 Check_Real_Bound (High);
15425 Low_Val := Expr_Value_R (Low);
15426 High_Val := Expr_Value_R (High);
15428 if Low_Val < (-Bound_Val) then
15430 ("range low bound too small for digits value", Low);
15431 Low_Val := -Bound_Val;
15434 if High_Val > Bound_Val then
15436 ("range high bound too large for digits value", High);
15437 High_Val := Bound_Val;
15440 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15443 -- If no explicit range, use range that corresponds to given
15444 -- digits value. This will end up as the final range for the
15448 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15451 -- Complete entity for first subtype. The inheritance of the rep item
15452 -- chain ensures that SPARK-related pragmas are not clobbered when the
15453 -- decimal fixed point type acts as a full view of a private type.
15455 Mutate_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15456 Set_Etype (T, Implicit_Base);
15457 Set_Size_Info (T, Implicit_Base);
15458 Inherit_Rep_Item_Chain (T, Implicit_Base);
15459 Set_Digits_Value (T, Digs_Val);
15460 Set_Delta_Value (T, Delta_Val);
15461 Set_Small_Value (T, Delta_Val);
15462 Set_Scale_Value (T, Scale_Val);
15463 Set_Is_Constrained (T);
15464 end Decimal_Fixed_Point_Type_Declaration;
15466 -----------------------------------
15467 -- Derive_Progenitor_Subprograms --
15468 -----------------------------------
15470 procedure Derive_Progenitor_Subprograms
15471 (Parent_Type : Entity_Id;
15472 Tagged_Type : Entity_Id)
15477 Iface_Alias : Entity_Id;
15478 Iface_Elmt : Elmt_Id;
15479 Iface_Subp : Entity_Id;
15480 New_Subp : Entity_Id := Empty;
15481 Prim_Elmt : Elmt_Id;
15486 pragma Assert (Ada_Version >= Ada_2005
15487 and then Is_Record_Type (Tagged_Type)
15488 and then Is_Tagged_Type (Tagged_Type)
15489 and then Has_Interfaces (Tagged_Type));
15491 -- Step 1: Transfer to the full-view primitives associated with the
15492 -- partial-view that cover interface primitives. Conceptually this
15493 -- work should be done later by Process_Full_View; done here to
15494 -- simplify its implementation at later stages. It can be safely
15495 -- done here because interfaces must be visible in the partial and
15496 -- private view (RM 7.3(7.3/2)).
15498 -- Small optimization: This work is only required if the parent may
15499 -- have entities whose Alias attribute reference an interface primitive.
15500 -- Such a situation may occur if the parent is an abstract type and the
15501 -- primitive has not been yet overridden or if the parent is a generic
15502 -- formal type covering interfaces.
15504 -- If the tagged type is not abstract, it cannot have abstract
15505 -- primitives (the only entities in the list of primitives of
15506 -- non-abstract tagged types that can reference abstract primitives
15507 -- through its Alias attribute are the internal entities that have
15508 -- attribute Interface_Alias, and these entities are generated later
15509 -- by Add_Internal_Interface_Entities).
15511 if In_Private_Part (Current_Scope)
15512 and then (Is_Abstract_Type (Parent_Type)
15514 Is_Generic_Type (Parent_Type))
15516 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15517 while Present (Elmt) loop
15518 Subp := Node (Elmt);
15520 -- At this stage it is not possible to have entities in the list
15521 -- of primitives that have attribute Interface_Alias.
15523 pragma Assert (No (Interface_Alias (Subp)));
15525 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15527 if Is_Interface (Typ) then
15528 E := Find_Primitive_Covering_Interface
15529 (Tagged_Type => Tagged_Type,
15530 Iface_Prim => Subp);
15533 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15535 Replace_Elmt (Elmt, E);
15536 Remove_Homonym (Subp);
15544 -- Step 2: Add primitives of progenitors that are not implemented by
15545 -- parents of Tagged_Type.
15547 if Present (Interfaces (Base_Type (Tagged_Type))) then
15548 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15549 while Present (Iface_Elmt) loop
15550 Iface := Node (Iface_Elmt);
15552 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15553 while Present (Prim_Elmt) loop
15554 Iface_Subp := Node (Prim_Elmt);
15555 Iface_Alias := Ultimate_Alias (Iface_Subp);
15557 -- Exclude derivation of predefined primitives except those
15558 -- that come from source, or are inherited from one that comes
15559 -- from source. Required to catch declarations of equality
15560 -- operators of interfaces. For example:
15562 -- type Iface is interface;
15563 -- function "=" (Left, Right : Iface) return Boolean;
15565 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15566 or else Comes_From_Source (Iface_Alias)
15569 Find_Primitive_Covering_Interface
15570 (Tagged_Type => Tagged_Type,
15571 Iface_Prim => Iface_Subp);
15573 -- If not found we derive a new primitive leaving its alias
15574 -- attribute referencing the interface primitive.
15578 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15580 -- Ada 2012 (AI05-0197): If the covering primitive's name
15581 -- differs from the name of the interface primitive then it
15582 -- is a private primitive inherited from a parent type. In
15583 -- such case, given that Tagged_Type covers the interface,
15584 -- the inherited private primitive becomes visible. For such
15585 -- purpose we add a new entity that renames the inherited
15586 -- private primitive.
15588 elsif Chars (E) /= Chars (Iface_Subp) then
15589 pragma Assert (Has_Suffix (E, 'P'));
15591 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15592 Set_Alias (New_Subp, E);
15593 Set_Is_Abstract_Subprogram (New_Subp,
15594 Is_Abstract_Subprogram (E));
15596 -- Propagate to the full view interface entities associated
15597 -- with the partial view.
15599 elsif In_Private_Part (Current_Scope)
15600 and then Present (Alias (E))
15601 and then Alias (E) = Iface_Subp
15603 List_Containing (Parent (E)) /=
15604 Private_Declarations
15606 (Unit_Declaration_Node (Current_Scope)))
15608 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15612 Next_Elmt (Prim_Elmt);
15615 Next_Elmt (Iface_Elmt);
15618 end Derive_Progenitor_Subprograms;
15620 -----------------------
15621 -- Derive_Subprogram --
15622 -----------------------
15624 procedure Derive_Subprogram
15625 (New_Subp : out Entity_Id;
15626 Parent_Subp : Entity_Id;
15627 Derived_Type : Entity_Id;
15628 Parent_Type : Entity_Id;
15629 Actual_Subp : Entity_Id := Empty)
15631 Formal : Entity_Id;
15632 -- Formal parameter of parent primitive operation
15634 Formal_Of_Actual : Entity_Id;
15635 -- Formal parameter of actual operation, when the derivation is to
15636 -- create a renaming for a primitive operation of an actual in an
15639 New_Formal : Entity_Id;
15640 -- Formal of inherited operation
15642 Visible_Subp : Entity_Id := Parent_Subp;
15644 function Is_Private_Overriding return Boolean;
15645 -- If Subp is a private overriding of a visible operation, the inherited
15646 -- operation derives from the overridden op (even though its body is the
15647 -- overriding one) and the inherited operation is visible now. See
15648 -- sem_disp to see the full details of the handling of the overridden
15649 -- subprogram, which is removed from the list of primitive operations of
15650 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15651 -- and used to diagnose abstract operations that need overriding in the
15654 procedure Replace_Type (Id, New_Id : Entity_Id);
15655 -- When the type is an anonymous access type, create a new access type
15656 -- designating the derived type.
15658 procedure Set_Derived_Name;
15659 -- This procedure sets the appropriate Chars name for New_Subp. This
15660 -- is normally just a copy of the parent name. An exception arises for
15661 -- type support subprograms, where the name is changed to reflect the
15662 -- name of the derived type, e.g. if type foo is derived from type bar,
15663 -- then a procedure barDA is derived with a name fooDA.
15665 ---------------------------
15666 -- Is_Private_Overriding --
15667 ---------------------------
15669 function Is_Private_Overriding return Boolean is
15673 -- If the parent is not a dispatching operation there is no
15674 -- need to investigate overridings
15676 if not Is_Dispatching_Operation (Parent_Subp) then
15680 -- The visible operation that is overridden is a homonym of the
15681 -- parent subprogram. We scan the homonym chain to find the one
15682 -- whose alias is the subprogram we are deriving.
15684 Prev := Current_Entity (Parent_Subp);
15685 while Present (Prev) loop
15686 if Ekind (Prev) = Ekind (Parent_Subp)
15687 and then Alias (Prev) = Parent_Subp
15688 and then Scope (Parent_Subp) = Scope (Prev)
15689 and then not Is_Hidden (Prev)
15691 Visible_Subp := Prev;
15695 Prev := Homonym (Prev);
15699 end Is_Private_Overriding;
15705 procedure Replace_Type (Id, New_Id : Entity_Id) is
15706 Id_Type : constant Entity_Id := Etype (Id);
15707 Acc_Type : Entity_Id;
15708 Par : constant Node_Id := Parent (Derived_Type);
15711 -- When the type is an anonymous access type, create a new access
15712 -- type designating the derived type. This itype must be elaborated
15713 -- at the point of the derivation, not on subsequent calls that may
15714 -- be out of the proper scope for Gigi, so we insert a reference to
15715 -- it after the derivation.
15717 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15719 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15722 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15723 and then Present (Full_View (Desig_Typ))
15724 and then not Is_Private_Type (Parent_Type)
15726 Desig_Typ := Full_View (Desig_Typ);
15729 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15731 -- Ada 2005 (AI-251): Handle also derivations of abstract
15732 -- interface primitives.
15734 or else (Is_Interface (Desig_Typ)
15735 and then not Is_Class_Wide_Type (Desig_Typ))
15737 Acc_Type := New_Copy (Id_Type);
15738 Set_Etype (Acc_Type, Acc_Type);
15739 Set_Scope (Acc_Type, New_Subp);
15741 -- Set size of anonymous access type. If we have an access
15742 -- to an unconstrained array, this is a fat pointer, so it
15743 -- is sizes at twice addtress size.
15745 if Is_Array_Type (Desig_Typ)
15746 and then not Is_Constrained (Desig_Typ)
15748 Init_Size (Acc_Type, 2 * System_Address_Size);
15750 -- Other cases use a thin pointer
15753 Init_Size (Acc_Type, System_Address_Size);
15756 -- Set remaining characterstics of anonymous access type
15758 Reinit_Alignment (Acc_Type);
15759 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15761 Set_Etype (New_Id, Acc_Type);
15762 Set_Scope (New_Id, New_Subp);
15764 -- Create a reference to it
15766 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15769 Set_Etype (New_Id, Id_Type);
15773 -- In Ada2012, a formal may have an incomplete type but the type
15774 -- derivation that inherits the primitive follows the full view.
15776 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15778 (Ekind (Id_Type) = E_Record_Type_With_Private
15779 and then Present (Full_View (Id_Type))
15781 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15783 (Ada_Version >= Ada_2012
15784 and then Ekind (Id_Type) = E_Incomplete_Type
15785 and then Full_View (Id_Type) = Parent_Type)
15787 -- Constraint checks on formals are generated during expansion,
15788 -- based on the signature of the original subprogram. The bounds
15789 -- of the derived type are not relevant, and thus we can use
15790 -- the base type for the formals. However, the return type may be
15791 -- used in a context that requires that the proper static bounds
15792 -- be used (a case statement, for example) and for those cases
15793 -- we must use the derived type (first subtype), not its base.
15795 -- If the derived_type_definition has no constraints, we know that
15796 -- the derived type has the same constraints as the first subtype
15797 -- of the parent, and we can also use it rather than its base,
15798 -- which can lead to more efficient code.
15800 if Etype (Id) = Parent_Type then
15801 if Is_Scalar_Type (Parent_Type)
15803 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15805 Set_Etype (New_Id, Derived_Type);
15807 elsif Nkind (Par) = N_Full_Type_Declaration
15809 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15812 (Subtype_Indication (Type_Definition (Par)))
15814 Set_Etype (New_Id, Derived_Type);
15817 Set_Etype (New_Id, Base_Type (Derived_Type));
15821 Set_Etype (New_Id, Base_Type (Derived_Type));
15825 Set_Etype (New_Id, Etype (Id));
15829 ----------------------
15830 -- Set_Derived_Name --
15831 ----------------------
15833 procedure Set_Derived_Name is
15834 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15836 if Nm = TSS_Null then
15837 Set_Chars (New_Subp, Chars (Parent_Subp));
15839 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15841 end Set_Derived_Name;
15843 -- Start of processing for Derive_Subprogram
15846 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15847 Mutate_Ekind (New_Subp, Ekind (Parent_Subp));
15849 -- Check whether the inherited subprogram is a private operation that
15850 -- should be inherited but not yet made visible. Such subprograms can
15851 -- become visible at a later point (e.g., the private part of a public
15852 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15853 -- following predicate is true, then this is not such a private
15854 -- operation and the subprogram simply inherits the name of the parent
15855 -- subprogram. Note the special check for the names of controlled
15856 -- operations, which are currently exempted from being inherited with
15857 -- a hidden name because they must be findable for generation of
15858 -- implicit run-time calls.
15860 if not Is_Hidden (Parent_Subp)
15861 or else Is_Internal (Parent_Subp)
15862 or else Is_Private_Overriding
15863 or else Is_Internal_Name (Chars (Parent_Subp))
15864 or else (Is_Controlled (Parent_Type)
15865 and then Chars (Parent_Subp) in Name_Adjust
15871 -- An inherited dispatching equality will be overridden by an internally
15872 -- generated one, or by an explicit one, so preserve its name and thus
15873 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15874 -- private operation it may become invisible if the full view has
15875 -- progenitors, and the dispatch table will be malformed.
15876 -- We check that the type is limited to handle the anomalous declaration
15877 -- of Limited_Controlled, which is derived from a non-limited type, and
15878 -- which is handled specially elsewhere as well.
15880 elsif Chars (Parent_Subp) = Name_Op_Eq
15881 and then Is_Dispatching_Operation (Parent_Subp)
15882 and then Etype (Parent_Subp) = Standard_Boolean
15883 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15885 Etype (First_Formal (Parent_Subp)) =
15886 Etype (Next_Formal (First_Formal (Parent_Subp)))
15890 -- If parent is hidden, this can be a regular derivation if the
15891 -- parent is immediately visible in a non-instantiating context,
15892 -- or if we are in the private part of an instance. This test
15893 -- should still be refined ???
15895 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15896 -- operation as a non-visible operation in cases where the parent
15897 -- subprogram might not be visible now, but was visible within the
15898 -- original generic, so it would be wrong to make the inherited
15899 -- subprogram non-visible now. (Not clear if this test is fully
15900 -- correct; are there any cases where we should declare the inherited
15901 -- operation as not visible to avoid it being overridden, e.g., when
15902 -- the parent type is a generic actual with private primitives ???)
15904 -- (they should be treated the same as other private inherited
15905 -- subprograms, but it's not clear how to do this cleanly). ???
15907 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15908 and then Is_Immediately_Visible (Parent_Subp)
15909 and then not In_Instance)
15910 or else In_Instance_Not_Visible
15914 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15915 -- overrides an interface primitive because interface primitives
15916 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15918 elsif Ada_Version >= Ada_2005
15919 and then Is_Dispatching_Operation (Parent_Subp)
15920 and then Present (Covered_Interface_Op (Parent_Subp))
15924 -- Otherwise, the type is inheriting a private operation, so enter it
15925 -- with a special name so it can't be overridden. See also below, where
15926 -- we check for this case, and if so avoid setting Requires_Overriding.
15929 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15932 Set_Parent (New_Subp, Parent (Derived_Type));
15934 if Present (Actual_Subp) then
15935 Replace_Type (Actual_Subp, New_Subp);
15937 Replace_Type (Parent_Subp, New_Subp);
15940 Conditional_Delay (New_Subp, Parent_Subp);
15942 -- If we are creating a renaming for a primitive operation of an
15943 -- actual of a generic derived type, we must examine the signature
15944 -- of the actual primitive, not that of the generic formal, which for
15945 -- example may be an interface. However the name and initial value
15946 -- of the inherited operation are those of the formal primitive.
15948 Formal := First_Formal (Parent_Subp);
15950 if Present (Actual_Subp) then
15951 Formal_Of_Actual := First_Formal (Actual_Subp);
15953 Formal_Of_Actual := Empty;
15956 while Present (Formal) loop
15957 New_Formal := New_Copy (Formal);
15959 -- Extra formals are not inherited from a limited interface parent
15960 -- since limitedness is not inherited in such case (AI-419) and this
15961 -- affects the extra formals.
15963 if Is_Limited_Interface (Parent_Type) then
15964 Set_Extra_Formal (New_Formal, Empty);
15965 Set_Extra_Accessibility (New_Formal, Empty);
15968 -- Normally we do not go copying parents, but in the case of
15969 -- formals, we need to link up to the declaration (which is the
15970 -- parameter specification), and it is fine to link up to the
15971 -- original formal's parameter specification in this case.
15973 Set_Parent (New_Formal, Parent (Formal));
15974 Append_Entity (New_Formal, New_Subp);
15976 if Present (Formal_Of_Actual) then
15977 Replace_Type (Formal_Of_Actual, New_Formal);
15978 Next_Formal (Formal_Of_Actual);
15980 Replace_Type (Formal, New_Formal);
15983 Next_Formal (Formal);
15986 -- Extra formals are shared between the parent subprogram and the
15987 -- derived subprogram (implicit in the above copy of formals), unless
15988 -- the parent type is a limited interface type; hence we must inherit
15989 -- also the reference to the first extra formal. When the parent type is
15990 -- an interface the extra formals will be added when the subprogram is
15991 -- frozen (see Freeze.Freeze_Subprogram).
15993 if not Is_Limited_Interface (Parent_Type) then
15994 Set_Extra_Formals (New_Subp, Extra_Formals (Parent_Subp));
15996 if Ekind (New_Subp) = E_Function then
15997 Set_Extra_Accessibility_Of_Result (New_Subp,
15998 Extra_Accessibility_Of_Result (Parent_Subp));
16002 -- If this derivation corresponds to a tagged generic actual, then
16003 -- primitive operations rename those of the actual. Otherwise the
16004 -- primitive operations rename those of the parent type, If the parent
16005 -- renames an intrinsic operator, so does the new subprogram. We except
16006 -- concatenation, which is always properly typed, and does not get
16007 -- expanded as other intrinsic operations.
16009 if No (Actual_Subp) then
16010 if Is_Intrinsic_Subprogram (Parent_Subp) then
16011 Set_Is_Intrinsic_Subprogram (New_Subp);
16013 if Present (Alias (Parent_Subp))
16014 and then Chars (Parent_Subp) /= Name_Op_Concat
16016 Set_Alias (New_Subp, Alias (Parent_Subp));
16018 Set_Alias (New_Subp, Parent_Subp);
16022 Set_Alias (New_Subp, Parent_Subp);
16026 Set_Alias (New_Subp, Actual_Subp);
16029 -- Derived subprograms of a tagged type must inherit the convention
16030 -- of the parent subprogram (a requirement of AI-117). Derived
16031 -- subprograms of untagged types simply get convention Ada by default.
16033 -- If the derived type is a tagged generic formal type with unknown
16034 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
16036 -- However, if the type is derived from a generic formal, the further
16037 -- inherited subprogram has the convention of the non-generic ancestor.
16038 -- Otherwise there would be no way to override the operation.
16039 -- (This is subject to forthcoming ARG discussions).
16041 if Is_Tagged_Type (Derived_Type) then
16042 if Is_Generic_Type (Derived_Type)
16043 and then Has_Unknown_Discriminants (Derived_Type)
16045 Set_Convention (New_Subp, Convention_Intrinsic);
16048 if Is_Generic_Type (Parent_Type)
16049 and then Has_Unknown_Discriminants (Parent_Type)
16051 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
16053 Set_Convention (New_Subp, Convention (Parent_Subp));
16058 -- Predefined controlled operations retain their name even if the parent
16059 -- is hidden (see above), but they are not primitive operations if the
16060 -- ancestor is not visible, for example if the parent is a private
16061 -- extension completed with a controlled extension. Note that a full
16062 -- type that is controlled can break privacy: the flag Is_Controlled is
16063 -- set on both views of the type.
16065 if Is_Controlled (Parent_Type)
16066 and then Chars (Parent_Subp) in Name_Initialize
16069 and then Is_Hidden (Parent_Subp)
16070 and then not Is_Visibly_Controlled (Parent_Type)
16072 Set_Is_Hidden (New_Subp);
16075 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
16076 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
16078 if Ekind (Parent_Subp) = E_Procedure then
16079 Set_Is_Valued_Procedure
16080 (New_Subp, Is_Valued_Procedure (Parent_Subp));
16082 Set_Has_Controlling_Result
16083 (New_Subp, Has_Controlling_Result (Parent_Subp));
16086 -- No_Return must be inherited properly. If this is overridden in the
16087 -- case of a dispatching operation, then the check is made later in
16088 -- Check_Abstract_Overriding that the overriding operation is also
16089 -- No_Return (no such check is required for the nondispatching case).
16091 Set_No_Return (New_Subp, No_Return (Parent_Subp));
16093 -- A derived function with a controlling result is abstract. If the
16094 -- Derived_Type is a nonabstract formal generic derived type, then
16095 -- inherited operations are not abstract: the required check is done at
16096 -- instantiation time. If the derivation is for a generic actual, the
16097 -- function is not abstract unless the actual is.
16099 if Is_Generic_Type (Derived_Type)
16100 and then not Is_Abstract_Type (Derived_Type)
16104 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
16105 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2). Note
16106 -- that functions with controlling access results of record extensions
16107 -- with a null extension part require overriding (AI95-00391/06).
16109 -- Ada 2022 (AI12-0042): Similarly, set those properties for
16110 -- implementing the rule of RM 7.3.2(6.1/4).
16112 -- A subprogram subject to pragma Extensions_Visible with value False
16113 -- requires overriding if the subprogram has at least one controlling
16114 -- OUT parameter (SPARK RM 6.1.7(6)).
16116 elsif Ada_Version >= Ada_2005
16117 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16118 or else (Is_Tagged_Type (Derived_Type)
16119 and then Etype (New_Subp) = Derived_Type
16120 and then not Is_Null_Extension (Derived_Type))
16121 or else (Is_Tagged_Type (Derived_Type)
16122 and then Ekind (Etype (New_Subp)) =
16123 E_Anonymous_Access_Type
16124 and then Designated_Type (Etype (New_Subp)) =
16126 or else (Comes_From_Source (Alias (New_Subp))
16127 and then Is_EVF_Procedure (Alias (New_Subp)))
16129 -- AI12-0042: Set Requires_Overriding when a type extension
16130 -- inherits a private operation that is visible at the
16131 -- point of extension (Has_Private_Ancestor is False) from
16132 -- an ancestor that has Type_Invariant'Class, and when the
16133 -- type extension is in a visible part (the latter as
16134 -- clarified by AI12-0382).
16137 (not Has_Private_Ancestor (Derived_Type)
16138 and then Has_Invariants (Parent_Type)
16140 Present (Get_Pragma (Parent_Type, Pragma_Invariant))
16143 (Get_Pragma (Parent_Type, Pragma_Invariant))
16144 and then Is_Private_Primitive (Parent_Subp)
16145 and then In_Visible_Part (Scope (Derived_Type))))
16147 and then No (Actual_Subp)
16149 if not Is_Tagged_Type (Derived_Type)
16150 or else Is_Abstract_Type (Derived_Type)
16151 or else Is_Abstract_Subprogram (Alias (New_Subp))
16153 Set_Is_Abstract_Subprogram (New_Subp);
16155 -- If the Chars of the new subprogram is different from that of the
16156 -- parent's one, it means that we entered it with a special name so
16157 -- it can't be overridden (see above). In that case we had better not
16158 -- *require* it to be overridden. This is the case where the parent
16159 -- type inherited the operation privately, so there's no danger of
16160 -- dangling dispatching.
16162 elsif Chars (New_Subp) = Chars (Alias (New_Subp)) then
16163 Set_Requires_Overriding (New_Subp);
16166 elsif Ada_Version < Ada_2005
16167 and then (Is_Abstract_Subprogram (Alias (New_Subp))
16168 or else (Is_Tagged_Type (Derived_Type)
16169 and then Etype (New_Subp) = Derived_Type
16170 and then No (Actual_Subp)))
16172 Set_Is_Abstract_Subprogram (New_Subp);
16174 -- AI05-0097 : an inherited operation that dispatches on result is
16175 -- abstract if the derived type is abstract, even if the parent type
16176 -- is concrete and the derived type is a null extension.
16178 elsif Has_Controlling_Result (Alias (New_Subp))
16179 and then Is_Abstract_Type (Etype (New_Subp))
16181 Set_Is_Abstract_Subprogram (New_Subp);
16183 -- Finally, if the parent type is abstract we must verify that all
16184 -- inherited operations are either non-abstract or overridden, or that
16185 -- the derived type itself is abstract (this check is performed at the
16186 -- end of a package declaration, in Check_Abstract_Overriding). A
16187 -- private overriding in the parent type will not be visible in the
16188 -- derivation if we are not in an inner package or in a child unit of
16189 -- the parent type, in which case the abstractness of the inherited
16190 -- operation is carried to the new subprogram.
16192 elsif Is_Abstract_Type (Parent_Type)
16193 and then not In_Open_Scopes (Scope (Parent_Type))
16194 and then Is_Private_Overriding
16195 and then Is_Abstract_Subprogram (Visible_Subp)
16197 if No (Actual_Subp) then
16198 Set_Alias (New_Subp, Visible_Subp);
16199 Set_Is_Abstract_Subprogram (New_Subp, True);
16202 -- If this is a derivation for an instance of a formal derived
16203 -- type, abstractness comes from the primitive operation of the
16204 -- actual, not from the operation inherited from the ancestor.
16206 Set_Is_Abstract_Subprogram
16207 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
16211 New_Overloaded_Entity (New_Subp, Derived_Type);
16213 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
16214 -- preconditions and the derived type is abstract, the derived operation
16215 -- is abstract as well if parent subprogram is not abstract or null.
16217 if Is_Abstract_Type (Derived_Type)
16218 and then Has_Non_Trivial_Precondition (Parent_Subp)
16219 and then Present (Interfaces (Derived_Type))
16222 -- Add useful attributes of subprogram before the freeze point,
16223 -- in case freezing is delayed or there are previous errors.
16225 Set_Is_Dispatching_Operation (New_Subp);
16228 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
16231 if Present (Iface_Prim)
16232 and then Has_Non_Trivial_Precondition (Iface_Prim)
16234 Set_Is_Abstract_Subprogram (New_Subp);
16239 -- Check for case of a derived subprogram for the instantiation of a
16240 -- formal derived tagged type, if so mark the subprogram as dispatching
16241 -- and inherit the dispatching attributes of the actual subprogram. The
16242 -- derived subprogram is effectively renaming of the actual subprogram,
16243 -- so it needs to have the same attributes as the actual.
16245 if Present (Actual_Subp)
16246 and then Is_Dispatching_Operation (Actual_Subp)
16248 Set_Is_Dispatching_Operation (New_Subp);
16250 if Present (DTC_Entity (Actual_Subp)) then
16251 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
16252 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
16256 -- Indicate that a derived subprogram does not require a body and that
16257 -- it does not require processing of default expressions.
16259 Set_Has_Completion (New_Subp);
16260 Set_Default_Expressions_Processed (New_Subp);
16262 if Ekind (New_Subp) = E_Function then
16263 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
16266 -- Ada 2022 (AI12-0279): If a Yield aspect is specified True for a
16267 -- primitive subprogram S of a type T, then the aspect is inherited
16268 -- by the corresponding primitive subprogram of each descendant of T.
16270 if Is_Tagged_Type (Derived_Type)
16271 and then Is_Dispatching_Operation (New_Subp)
16272 and then Has_Yield_Aspect (Alias (New_Subp))
16274 Set_Has_Yield_Aspect (New_Subp, Has_Yield_Aspect (Alias (New_Subp)));
16277 Set_Is_Ada_2022_Only (New_Subp, Is_Ada_2022_Only (Parent_Subp));
16278 end Derive_Subprogram;
16280 ------------------------
16281 -- Derive_Subprograms --
16282 ------------------------
16284 procedure Derive_Subprograms
16285 (Parent_Type : Entity_Id;
16286 Derived_Type : Entity_Id;
16287 Generic_Actual : Entity_Id := Empty)
16289 Op_List : constant Elist_Id :=
16290 Collect_Primitive_Operations (Parent_Type);
16292 function Check_Derived_Type return Boolean;
16293 -- Check that all the entities derived from Parent_Type are found in
16294 -- the list of primitives of Derived_Type exactly in the same order.
16296 procedure Derive_Interface_Subprogram
16297 (New_Subp : out Entity_Id;
16299 Actual_Subp : Entity_Id);
16300 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
16301 -- (which is an interface primitive). If Generic_Actual is present then
16302 -- Actual_Subp is the actual subprogram corresponding with the generic
16303 -- subprogram Subp.
16305 ------------------------
16306 -- Check_Derived_Type --
16307 ------------------------
16309 function Check_Derived_Type return Boolean is
16313 New_Subp : Entity_Id;
16318 -- Traverse list of entities in the current scope searching for
16319 -- an incomplete type whose full-view is derived type.
16321 E := First_Entity (Scope (Derived_Type));
16322 while Present (E) and then E /= Derived_Type loop
16323 if Ekind (E) = E_Incomplete_Type
16324 and then Present (Full_View (E))
16325 and then Full_View (E) = Derived_Type
16327 -- Disable this test if Derived_Type completes an incomplete
16328 -- type because in such case more primitives can be added
16329 -- later to the list of primitives of Derived_Type by routine
16330 -- Process_Incomplete_Dependents
16338 List := Collect_Primitive_Operations (Derived_Type);
16339 Elmt := First_Elmt (List);
16341 Op_Elmt := First_Elmt (Op_List);
16342 while Present (Op_Elmt) loop
16343 Subp := Node (Op_Elmt);
16344 New_Subp := Node (Elmt);
16346 -- At this early stage Derived_Type has no entities with attribute
16347 -- Interface_Alias. In addition, such primitives are always
16348 -- located at the end of the list of primitives of Parent_Type.
16349 -- Therefore, if found we can safely stop processing pending
16352 exit when Present (Interface_Alias (Subp));
16354 -- Handle hidden entities
16356 if not Is_Predefined_Dispatching_Operation (Subp)
16357 and then Is_Hidden (Subp)
16359 if Present (New_Subp)
16360 and then Primitive_Names_Match (Subp, New_Subp)
16366 if not Present (New_Subp)
16367 or else Ekind (Subp) /= Ekind (New_Subp)
16368 or else not Primitive_Names_Match (Subp, New_Subp)
16376 Next_Elmt (Op_Elmt);
16380 end Check_Derived_Type;
16382 ---------------------------------
16383 -- Derive_Interface_Subprogram --
16384 ---------------------------------
16386 procedure Derive_Interface_Subprogram
16387 (New_Subp : out Entity_Id;
16389 Actual_Subp : Entity_Id)
16391 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
16392 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
16395 pragma Assert (Is_Interface (Iface_Type));
16398 (New_Subp => New_Subp,
16399 Parent_Subp => Iface_Subp,
16400 Derived_Type => Derived_Type,
16401 Parent_Type => Iface_Type,
16402 Actual_Subp => Actual_Subp);
16404 -- Given that this new interface entity corresponds with a primitive
16405 -- of the parent that was not overridden we must leave it associated
16406 -- with its parent primitive to ensure that it will share the same
16407 -- dispatch table slot when overridden. We must set the Alias to Subp
16408 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
16409 -- (in case we inherited Subp from Iface_Type via a nonabstract
16410 -- generic formal type).
16412 if No (Actual_Subp) then
16413 Set_Alias (New_Subp, Subp);
16416 T : Entity_Id := Find_Dispatching_Type (Subp);
16418 while Etype (T) /= T loop
16419 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
16420 Set_Is_Abstract_Subprogram (New_Subp, False);
16428 -- For instantiations this is not needed since the previous call to
16429 -- Derive_Subprogram leaves the entity well decorated.
16432 pragma Assert (Alias (New_Subp) = Actual_Subp);
16435 end Derive_Interface_Subprogram;
16439 Alias_Subp : Entity_Id;
16440 Act_List : Elist_Id;
16441 Act_Elmt : Elmt_Id;
16442 Act_Subp : Entity_Id := Empty;
16444 Need_Search : Boolean := False;
16445 New_Subp : Entity_Id := Empty;
16446 Parent_Base : Entity_Id;
16449 -- Start of processing for Derive_Subprograms
16452 if Ekind (Parent_Type) = E_Record_Type_With_Private
16453 and then Has_Discriminants (Parent_Type)
16454 and then Present (Full_View (Parent_Type))
16456 Parent_Base := Full_View (Parent_Type);
16458 Parent_Base := Parent_Type;
16461 if Present (Generic_Actual) then
16462 Act_List := Collect_Primitive_Operations (Generic_Actual);
16463 Act_Elmt := First_Elmt (Act_List);
16465 Act_List := No_Elist;
16466 Act_Elmt := No_Elmt;
16469 -- Derive primitives inherited from the parent. Note that if the generic
16470 -- actual is present, this is not really a type derivation, it is a
16471 -- completion within an instance.
16473 -- Case 1: Derived_Type does not implement interfaces
16475 if not Is_Tagged_Type (Derived_Type)
16476 or else (not Has_Interfaces (Derived_Type)
16477 and then not (Present (Generic_Actual)
16478 and then Has_Interfaces (Generic_Actual)))
16480 Elmt := First_Elmt (Op_List);
16481 while Present (Elmt) loop
16482 Subp := Node (Elmt);
16484 -- Literals are derived earlier in the process of building the
16485 -- derived type, and are skipped here.
16487 if Ekind (Subp) = E_Enumeration_Literal then
16490 -- The actual is a direct descendant and the common primitive
16491 -- operations appear in the same order.
16493 -- If the generic parent type is present, the derived type is an
16494 -- instance of a formal derived type, and within the instance its
16495 -- operations are those of the actual. We derive from the formal
16496 -- type but make the inherited operations aliases of the
16497 -- corresponding operations of the actual.
16500 pragma Assert (No (Node (Act_Elmt))
16501 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
16504 (Subp, Node (Act_Elmt),
16505 Skip_Controlling_Formals => True)));
16508 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
16510 if Present (Act_Elmt) then
16511 Next_Elmt (Act_Elmt);
16518 -- Case 2: Derived_Type implements interfaces
16521 -- If the parent type has no predefined primitives we remove
16522 -- predefined primitives from the list of primitives of generic
16523 -- actual to simplify the complexity of this algorithm.
16525 if Present (Generic_Actual) then
16527 Has_Predefined_Primitives : Boolean := False;
16530 -- Check if the parent type has predefined primitives
16532 Elmt := First_Elmt (Op_List);
16533 while Present (Elmt) loop
16534 Subp := Node (Elmt);
16536 if Is_Predefined_Dispatching_Operation (Subp)
16537 and then not Comes_From_Source (Ultimate_Alias (Subp))
16539 Has_Predefined_Primitives := True;
16546 -- Remove predefined primitives of Generic_Actual. We must use
16547 -- an auxiliary list because in case of tagged types the value
16548 -- returned by Collect_Primitive_Operations is the value stored
16549 -- in its Primitive_Operations attribute (and we don't want to
16550 -- modify its current contents).
16552 if not Has_Predefined_Primitives then
16554 Aux_List : constant Elist_Id := New_Elmt_List;
16557 Elmt := First_Elmt (Act_List);
16558 while Present (Elmt) loop
16559 Subp := Node (Elmt);
16561 if not Is_Predefined_Dispatching_Operation (Subp)
16562 or else Comes_From_Source (Subp)
16564 Append_Elmt (Subp, Aux_List);
16570 Act_List := Aux_List;
16574 Act_Elmt := First_Elmt (Act_List);
16575 Act_Subp := Node (Act_Elmt);
16579 -- Stage 1: If the generic actual is not present we derive the
16580 -- primitives inherited from the parent type. If the generic parent
16581 -- type is present, the derived type is an instance of a formal
16582 -- derived type, and within the instance its operations are those of
16583 -- the actual. We derive from the formal type but make the inherited
16584 -- operations aliases of the corresponding operations of the actual.
16586 Elmt := First_Elmt (Op_List);
16587 while Present (Elmt) loop
16588 Subp := Node (Elmt);
16589 Alias_Subp := Ultimate_Alias (Subp);
16591 -- Do not derive internal entities of the parent that link
16592 -- interface primitives with their covering primitive. These
16593 -- entities will be added to this type when frozen.
16595 if Present (Interface_Alias (Subp)) then
16599 -- If the generic actual is present find the corresponding
16600 -- operation in the generic actual. If the parent type is a
16601 -- direct ancestor of the derived type then, even if it is an
16602 -- interface, the operations are inherited from the primary
16603 -- dispatch table and are in the proper order. If we detect here
16604 -- that primitives are not in the same order we traverse the list
16605 -- of primitive operations of the actual to find the one that
16606 -- implements the interface primitive.
16610 (Present (Generic_Actual)
16611 and then Present (Act_Subp)
16613 (Primitive_Names_Match (Subp, Act_Subp)
16615 Type_Conformant (Subp, Act_Subp,
16616 Skip_Controlling_Formals => True)))
16618 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16619 Use_Full_View => True));
16621 -- Remember that we need searching for all pending primitives
16623 Need_Search := True;
16625 -- Handle entities associated with interface primitives
16627 if Present (Alias_Subp)
16628 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16629 and then not Is_Predefined_Dispatching_Operation (Subp)
16631 -- Search for the primitive in the homonym chain
16634 Find_Primitive_Covering_Interface
16635 (Tagged_Type => Generic_Actual,
16636 Iface_Prim => Alias_Subp);
16638 -- Previous search may not locate primitives covering
16639 -- interfaces defined in generics units or instantiations.
16640 -- (it fails if the covering primitive has formals whose
16641 -- type is also defined in generics or instantiations).
16642 -- In such case we search in the list of primitives of the
16643 -- generic actual for the internal entity that links the
16644 -- interface primitive and the covering primitive.
16647 and then Is_Generic_Type (Parent_Type)
16649 -- This code has been designed to handle only generic
16650 -- formals that implement interfaces that are defined
16651 -- in a generic unit or instantiation. If this code is
16652 -- needed for other cases we must review it because
16653 -- (given that it relies on Original_Location to locate
16654 -- the primitive of Generic_Actual that covers the
16655 -- interface) it could leave linked through attribute
16656 -- Alias entities of unrelated instantiations).
16660 (Scope (Find_Dispatching_Type (Alias_Subp)))
16662 Instantiation_Depth
16663 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16666 Iface_Prim_Loc : constant Source_Ptr :=
16667 Original_Location (Sloc (Alias_Subp));
16674 First_Elmt (Primitive_Operations (Generic_Actual));
16676 Search : while Present (Elmt) loop
16677 Prim := Node (Elmt);
16679 if Present (Interface_Alias (Prim))
16680 and then Original_Location
16681 (Sloc (Interface_Alias (Prim))) =
16684 Act_Subp := Alias (Prim);
16693 pragma Assert (Present (Act_Subp)
16694 or else Is_Abstract_Type (Generic_Actual)
16695 or else Serious_Errors_Detected > 0);
16697 -- Handle predefined primitives plus the rest of user-defined
16701 Act_Elmt := First_Elmt (Act_List);
16702 while Present (Act_Elmt) loop
16703 Act_Subp := Node (Act_Elmt);
16705 exit when Primitive_Names_Match (Subp, Act_Subp)
16706 and then Type_Conformant
16708 Skip_Controlling_Formals => True)
16709 and then No (Interface_Alias (Act_Subp));
16711 Next_Elmt (Act_Elmt);
16714 if No (Act_Elmt) then
16720 -- Case 1: If the parent is a limited interface then it has the
16721 -- predefined primitives of synchronized interfaces. However, the
16722 -- actual type may be a non-limited type and hence it does not
16723 -- have such primitives.
16725 if Present (Generic_Actual)
16726 and then not Present (Act_Subp)
16727 and then Is_Limited_Interface (Parent_Base)
16728 and then Is_Predefined_Interface_Primitive (Subp)
16732 -- Case 2: Inherit entities associated with interfaces that were
16733 -- not covered by the parent type. We exclude here null interface
16734 -- primitives because they do not need special management.
16736 -- We also exclude interface operations that are renamings. If the
16737 -- subprogram is an explicit renaming of an interface primitive,
16738 -- it is a regular primitive operation, and the presence of its
16739 -- alias is not relevant: it has to be derived like any other
16742 elsif Present (Alias (Subp))
16743 and then Nkind (Unit_Declaration_Node (Subp)) /=
16744 N_Subprogram_Renaming_Declaration
16745 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16747 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16748 and then Null_Present (Parent (Alias_Subp)))
16750 -- If this is an abstract private type then we transfer the
16751 -- derivation of the interface primitive from the partial view
16752 -- to the full view. This is safe because all the interfaces
16753 -- must be visible in the partial view. Done to avoid adding
16754 -- a new interface derivation to the private part of the
16755 -- enclosing package; otherwise this new derivation would be
16756 -- decorated as hidden when the analysis of the enclosing
16757 -- package completes.
16759 if Is_Abstract_Type (Derived_Type)
16760 and then In_Private_Part (Current_Scope)
16761 and then Has_Private_Declaration (Derived_Type)
16764 Partial_View : Entity_Id;
16769 Partial_View := First_Entity (Current_Scope);
16771 exit when No (Partial_View)
16772 or else (Has_Private_Declaration (Partial_View)
16774 Full_View (Partial_View) = Derived_Type);
16776 Next_Entity (Partial_View);
16779 -- If the partial view was not found then the source code
16780 -- has errors and the derivation is not needed.
16782 if Present (Partial_View) then
16784 First_Elmt (Primitive_Operations (Partial_View));
16785 while Present (Elmt) loop
16786 Ent := Node (Elmt);
16788 if Present (Alias (Ent))
16789 and then Ultimate_Alias (Ent) = Alias (Subp)
16792 (Ent, Primitive_Operations (Derived_Type));
16799 -- If the interface primitive was not found in the
16800 -- partial view then this interface primitive was
16801 -- overridden. We add a derivation to activate in
16802 -- Derive_Progenitor_Subprograms the machinery to
16806 Derive_Interface_Subprogram
16807 (New_Subp => New_Subp,
16809 Actual_Subp => Act_Subp);
16814 Derive_Interface_Subprogram
16815 (New_Subp => New_Subp,
16817 Actual_Subp => Act_Subp);
16820 -- Case 3: Common derivation
16824 (New_Subp => New_Subp,
16825 Parent_Subp => Subp,
16826 Derived_Type => Derived_Type,
16827 Parent_Type => Parent_Base,
16828 Actual_Subp => Act_Subp);
16831 -- No need to update Act_Elm if we must search for the
16832 -- corresponding operation in the generic actual
16835 and then Present (Act_Elmt)
16837 Next_Elmt (Act_Elmt);
16838 Act_Subp := Node (Act_Elmt);
16845 -- Inherit additional operations from progenitors. If the derived
16846 -- type is a generic actual, there are not new primitive operations
16847 -- for the type because it has those of the actual, and therefore
16848 -- nothing needs to be done. The renamings generated above are not
16849 -- primitive operations, and their purpose is simply to make the
16850 -- proper operations visible within an instantiation.
16852 if No (Generic_Actual) then
16853 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16857 -- Final check: Direct descendants must have their primitives in the
16858 -- same order. We exclude from this test untagged types and instances
16859 -- of formal derived types. We skip this test if we have already
16860 -- reported serious errors in the sources.
16862 pragma Assert (not Is_Tagged_Type (Derived_Type)
16863 or else Present (Generic_Actual)
16864 or else Serious_Errors_Detected > 0
16865 or else Check_Derived_Type);
16866 end Derive_Subprograms;
16868 --------------------------------
16869 -- Derived_Standard_Character --
16870 --------------------------------
16872 procedure Derived_Standard_Character
16874 Parent_Type : Entity_Id;
16875 Derived_Type : Entity_Id)
16877 Loc : constant Source_Ptr := Sloc (N);
16878 Def : constant Node_Id := Type_Definition (N);
16879 Indic : constant Node_Id := Subtype_Indication (Def);
16880 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16881 Implicit_Base : constant Entity_Id :=
16883 (E_Enumeration_Type, N, Derived_Type, 'B');
16889 Discard_Node (Process_Subtype (Indic, N));
16891 Set_Etype (Implicit_Base, Parent_Base);
16892 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16893 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16895 Set_Is_Character_Type (Implicit_Base, True);
16896 Set_Has_Delayed_Freeze (Implicit_Base);
16898 -- The bounds of the implicit base are the bounds of the parent base.
16899 -- Note that their type is the parent base.
16901 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16902 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16904 Set_Scalar_Range (Implicit_Base,
16907 High_Bound => Hi));
16909 Conditional_Delay (Derived_Type, Parent_Type);
16911 Mutate_Ekind (Derived_Type, E_Enumeration_Subtype);
16912 Set_Etype (Derived_Type, Implicit_Base);
16913 Set_Size_Info (Derived_Type, Parent_Type);
16915 if not Known_RM_Size (Derived_Type) then
16916 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16919 Set_Is_Character_Type (Derived_Type, True);
16921 if Nkind (Indic) /= N_Subtype_Indication then
16923 -- If no explicit constraint, the bounds are those
16924 -- of the parent type.
16926 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16927 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16928 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16931 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16933 -- Because the implicit base is used in the conversion of the bounds, we
16934 -- have to freeze it now. This is similar to what is done for numeric
16935 -- types, and it equally suspicious, but otherwise a nonstatic bound
16936 -- will have a reference to an unfrozen type, which is rejected by Gigi
16937 -- (???). This requires specific care for definition of stream
16938 -- attributes. For details, see comments at the end of
16939 -- Build_Derived_Numeric_Type.
16941 Freeze_Before (N, Implicit_Base);
16942 end Derived_Standard_Character;
16944 ------------------------------
16945 -- Derived_Type_Declaration --
16946 ------------------------------
16948 procedure Derived_Type_Declaration
16951 Is_Completion : Boolean)
16953 Parent_Type : Entity_Id;
16955 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16956 -- Check whether the parent type is a generic formal, or derives
16957 -- directly or indirectly from one.
16959 ------------------------
16960 -- Comes_From_Generic --
16961 ------------------------
16963 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16965 if Is_Generic_Type (Typ) then
16968 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16971 elsif Is_Private_Type (Typ)
16972 and then Present (Full_View (Typ))
16973 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16977 elsif Is_Generic_Actual_Type (Typ) then
16983 end Comes_From_Generic;
16987 Def : constant Node_Id := Type_Definition (N);
16988 Iface_Def : Node_Id;
16989 Indic : constant Node_Id := Subtype_Indication (Def);
16990 Extension : constant Node_Id := Record_Extension_Part (Def);
16991 Parent_Node : Node_Id;
16994 -- Start of processing for Derived_Type_Declaration
16997 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
17000 and then Is_Tagged_Type (Parent_Type)
17003 Partial_View : constant Entity_Id :=
17004 Incomplete_Or_Partial_View (Parent_Type);
17007 -- If the partial view was not found then the parent type is not
17008 -- a private type. Otherwise check if the partial view is a tagged
17011 if Present (Partial_View)
17012 and then Is_Private_Type (Partial_View)
17013 and then not Is_Tagged_Type (Partial_View)
17016 ("cannot derive from & declared as untagged private "
17017 & "(SPARK RM 3.4(1))", N, Partial_View);
17022 -- Ada 2005 (AI-251): In case of interface derivation check that the
17023 -- parent is also an interface.
17025 if Interface_Present (Def) then
17026 if not Is_Interface (Parent_Type) then
17027 Diagnose_Interface (Indic, Parent_Type);
17030 Parent_Node := Parent (Base_Type (Parent_Type));
17031 Iface_Def := Type_Definition (Parent_Node);
17033 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
17034 -- other limited interfaces.
17036 if Limited_Present (Def) then
17037 if Limited_Present (Iface_Def) then
17040 elsif Protected_Present (Iface_Def) then
17042 ("descendant of & must be declared as a protected "
17043 & "interface", N, Parent_Type);
17045 elsif Synchronized_Present (Iface_Def) then
17047 ("descendant of & must be declared as a synchronized "
17048 & "interface", N, Parent_Type);
17050 elsif Task_Present (Iface_Def) then
17052 ("descendant of & must be declared as a task interface",
17057 ("(Ada 2005) limited interface cannot inherit from "
17058 & "non-limited interface", Indic);
17061 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
17062 -- from non-limited or limited interfaces.
17064 elsif not Protected_Present (Def)
17065 and then not Synchronized_Present (Def)
17066 and then not Task_Present (Def)
17068 if Limited_Present (Iface_Def) then
17071 elsif Protected_Present (Iface_Def) then
17073 ("descendant of & must be declared as a protected "
17074 & "interface", N, Parent_Type);
17076 elsif Synchronized_Present (Iface_Def) then
17078 ("descendant of & must be declared as a synchronized "
17079 & "interface", N, Parent_Type);
17081 elsif Task_Present (Iface_Def) then
17083 ("descendant of & must be declared as a task interface",
17092 if Is_Tagged_Type (Parent_Type)
17093 and then Is_Concurrent_Type (Parent_Type)
17094 and then not Is_Interface (Parent_Type)
17097 ("parent type of a record extension cannot be a synchronized "
17098 & "tagged type (RM 3.9.1 (3/1))", N);
17099 Set_Etype (T, Any_Type);
17103 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
17106 if Is_Tagged_Type (Parent_Type)
17107 and then Is_Non_Empty_List (Interface_List (Def))
17114 Intf := First (Interface_List (Def));
17115 while Present (Intf) loop
17116 T := Find_Type_Of_Subtype_Indic (Intf);
17118 if not Is_Interface (T) then
17119 Diagnose_Interface (Intf, T);
17121 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
17122 -- a limited type from having a nonlimited progenitor.
17124 elsif (Limited_Present (Def)
17125 or else (not Is_Interface (Parent_Type)
17126 and then Is_Limited_Type (Parent_Type)))
17127 and then not Is_Limited_Interface (T)
17130 ("progenitor interface& of limited type must be limited",
17138 -- Check consistency of any nonoverridable aspects that are
17139 -- inherited from multiple sources.
17141 Check_Inherited_Nonoverridable_Aspects
17143 Interface_List => Interface_List (Def),
17144 Parent_Type => Parent_Type);
17147 if Parent_Type = Any_Type
17148 or else Etype (Parent_Type) = Any_Type
17149 or else (Is_Class_Wide_Type (Parent_Type)
17150 and then Etype (Parent_Type) = T)
17152 -- If Parent_Type is undefined or illegal, make new type into a
17153 -- subtype of Any_Type, and set a few attributes to prevent cascaded
17154 -- errors. If this is a self-definition, emit error now.
17156 if T = Parent_Type or else T = Etype (Parent_Type) then
17157 Error_Msg_N ("type cannot be used in its own definition", Indic);
17160 Mutate_Ekind (T, Ekind (Parent_Type));
17161 Set_Etype (T, Any_Type);
17162 Set_Scalar_Range (T, Scalar_Range (Any_Type));
17164 if Is_Tagged_Type (T)
17165 and then Is_Record_Type (T)
17167 Set_Direct_Primitive_Operations (T, New_Elmt_List);
17173 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
17174 -- an interface is special because the list of interfaces in the full
17175 -- view can be given in any order. For example:
17177 -- type A is interface;
17178 -- type B is interface and A;
17179 -- type D is new B with private;
17181 -- type D is new A and B with null record; -- 1 --
17183 -- In this case we perform the following transformation of -1-:
17185 -- type D is new B and A with null record;
17187 -- If the parent of the full-view covers the parent of the partial-view
17188 -- we have two possible cases:
17190 -- 1) They have the same parent
17191 -- 2) The parent of the full-view implements some further interfaces
17193 -- In both cases we do not need to perform the transformation. In the
17194 -- first case the source program is correct and the transformation is
17195 -- not needed; in the second case the source program does not fulfill
17196 -- the no-hidden interfaces rule (AI-396) and the error will be reported
17199 -- This transformation not only simplifies the rest of the analysis of
17200 -- this type declaration but also simplifies the correct generation of
17201 -- the object layout to the expander.
17203 if In_Private_Part (Current_Scope)
17204 and then Is_Interface (Parent_Type)
17208 Partial_View : Entity_Id;
17209 Partial_View_Parent : Entity_Id;
17210 New_Iface : Node_Id;
17213 -- Look for the associated private type declaration
17215 Partial_View := Incomplete_Or_Partial_View (T);
17217 -- If the partial view was not found then the source code has
17218 -- errors and the transformation is not needed.
17220 if Present (Partial_View) then
17221 Partial_View_Parent := Etype (Partial_View);
17223 -- If the parent of the full-view covers the parent of the
17224 -- partial-view we have nothing else to do.
17226 if Interface_Present_In_Ancestor
17227 (Parent_Type, Partial_View_Parent)
17231 -- Traverse the list of interfaces of the full-view to look
17232 -- for the parent of the partial-view and perform the tree
17236 Iface := First (Interface_List (Def));
17237 while Present (Iface) loop
17238 if Etype (Iface) = Etype (Partial_View) then
17239 Rewrite (Subtype_Indication (Def),
17240 New_Copy (Subtype_Indication
17241 (Parent (Partial_View))));
17244 Make_Identifier (Sloc (N), Chars (Parent_Type));
17245 Append (New_Iface, Interface_List (Def));
17247 -- Analyze the transformed code
17249 Derived_Type_Declaration (T, N, Is_Completion);
17260 -- Only composite types other than array types are allowed to have
17263 if Present (Discriminant_Specifications (N)) then
17264 if (Is_Elementary_Type (Parent_Type)
17266 Is_Array_Type (Parent_Type))
17267 and then not Error_Posted (N)
17270 ("elementary or array type cannot have discriminants",
17271 Defining_Identifier (First (Discriminant_Specifications (N))));
17273 -- Unset Has_Discriminants flag to prevent cascaded errors, but
17274 -- only if we are not already processing a malformed syntax tree.
17276 if Is_Type (T) then
17277 Set_Has_Discriminants (T, False);
17282 -- In Ada 83, a derived type defined in a package specification cannot
17283 -- be used for further derivation until the end of its visible part.
17284 -- Note that derivation in the private part of the package is allowed.
17286 if Ada_Version = Ada_83
17287 and then Is_Derived_Type (Parent_Type)
17288 and then In_Visible_Part (Scope (Parent_Type))
17290 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
17292 ("(Ada 83) premature use of type for derivation", Indic);
17296 -- Check for early use of incomplete or private type
17298 if Ekind (Parent_Type) in E_Void | E_Incomplete_Type then
17299 Error_Msg_N ("premature derivation of incomplete type", Indic);
17302 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
17303 and then not Comes_From_Generic (Parent_Type))
17304 or else Has_Private_Component (Parent_Type)
17306 -- The ancestor type of a formal type can be incomplete, in which
17307 -- case only the operations of the partial view are available in the
17308 -- generic. Subsequent checks may be required when the full view is
17309 -- analyzed to verify that a derivation from a tagged type has an
17312 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
17315 elsif No (Underlying_Type (Parent_Type))
17316 or else Has_Private_Component (Parent_Type)
17319 ("premature derivation of derived or private type", Indic);
17321 -- Flag the type itself as being in error, this prevents some
17322 -- nasty problems with subsequent uses of the malformed type.
17324 Set_Error_Posted (T);
17326 -- Check that within the immediate scope of an untagged partial
17327 -- view it's illegal to derive from the partial view if the
17328 -- full view is tagged. (7.3(7))
17330 -- We verify that the Parent_Type is a partial view by checking
17331 -- that it is not a Full_Type_Declaration (i.e. a private type or
17332 -- private extension declaration), to distinguish a partial view
17333 -- from a derivation from a private type which also appears as
17334 -- E_Private_Type. If the parent base type is not declared in an
17335 -- enclosing scope there is no need to check.
17337 elsif Present (Full_View (Parent_Type))
17338 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
17339 and then not Is_Tagged_Type (Parent_Type)
17340 and then Is_Tagged_Type (Full_View (Parent_Type))
17341 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
17344 ("premature derivation from type with tagged full view",
17349 -- Check that form of derivation is appropriate
17351 Taggd := Is_Tagged_Type (Parent_Type);
17353 -- Set the parent type to the class-wide type's specific type in this
17354 -- case to prevent cascading errors
17356 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
17357 Error_Msg_N ("parent type must not be a class-wide type", Indic);
17358 Set_Etype (T, Etype (Parent_Type));
17362 if Present (Extension) and then not Taggd then
17364 ("type derived from untagged type cannot have extension", Indic);
17366 elsif No (Extension) and then Taggd then
17368 -- If this declaration is within a private part (or body) of a
17369 -- generic instantiation then the derivation is allowed (the parent
17370 -- type can only appear tagged in this case if it's a generic actual
17371 -- type, since it would otherwise have been rejected in the analysis
17372 -- of the generic template).
17374 if not Is_Generic_Actual_Type (Parent_Type)
17375 or else In_Visible_Part (Scope (Parent_Type))
17377 if Is_Class_Wide_Type (Parent_Type) then
17379 ("parent type must not be a class-wide type", Indic);
17381 -- Use specific type to prevent cascaded errors.
17383 Parent_Type := Etype (Parent_Type);
17387 ("type derived from tagged type must have extension", Indic);
17392 -- AI-443: Synchronized formal derived types require a private
17393 -- extension. There is no point in checking the ancestor type or
17394 -- the progenitors since the construct is wrong to begin with.
17396 if Ada_Version >= Ada_2005
17397 and then Is_Generic_Type (T)
17398 and then Present (Original_Node (N))
17401 Decl : constant Node_Id := Original_Node (N);
17404 if Nkind (Decl) = N_Formal_Type_Declaration
17405 and then Nkind (Formal_Type_Definition (Decl)) =
17406 N_Formal_Derived_Type_Definition
17407 and then Synchronized_Present (Formal_Type_Definition (Decl))
17408 and then No (Extension)
17410 -- Avoid emitting a duplicate error message
17412 and then not Error_Posted (Indic)
17415 ("synchronized derived type must have extension", N);
17420 if Null_Exclusion_Present (Def)
17421 and then not Is_Access_Type (Parent_Type)
17423 Error_Msg_N ("null exclusion can only apply to an access type", N);
17426 Check_Wide_Character_Restriction (Parent_Type, Indic);
17428 -- Avoid deriving parent primitives of underlying record views
17430 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
17431 Derive_Subps => not Is_Underlying_Record_View (T));
17433 -- AI-419: The parent type of an explicitly limited derived type must
17434 -- be a limited type or a limited interface.
17436 if Limited_Present (Def) then
17437 Set_Is_Limited_Record (T);
17439 if Is_Interface (T) then
17440 Set_Is_Limited_Interface (T);
17443 if not Is_Limited_Type (Parent_Type)
17445 (not Is_Interface (Parent_Type)
17446 or else not Is_Limited_Interface (Parent_Type))
17448 -- AI05-0096: a derivation in the private part of an instance is
17449 -- legal if the generic formal is untagged limited, and the actual
17452 if Is_Generic_Actual_Type (Parent_Type)
17453 and then In_Private_Part (Current_Scope)
17456 (Generic_Parent_Type (Parent (Parent_Type)))
17462 ("parent type& of limited type must be limited",
17467 end Derived_Type_Declaration;
17469 ------------------------
17470 -- Diagnose_Interface --
17471 ------------------------
17473 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
17475 if not Is_Interface (E) and then E /= Any_Type then
17476 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
17478 end Diagnose_Interface;
17480 ----------------------------------
17481 -- Enumeration_Type_Declaration --
17482 ----------------------------------
17484 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17491 -- Create identifier node representing lower bound
17493 B_Node := New_Node (N_Identifier, Sloc (Def));
17494 L := First (Literals (Def));
17495 Set_Chars (B_Node, Chars (L));
17496 Set_Entity (B_Node, L);
17497 Set_Etype (B_Node, T);
17498 Set_Is_Static_Expression (B_Node, True);
17500 R_Node := New_Node (N_Range, Sloc (Def));
17501 Set_Low_Bound (R_Node, B_Node);
17503 Mutate_Ekind (T, E_Enumeration_Type);
17504 Set_First_Literal (T, L);
17506 Set_Is_Constrained (T);
17510 -- Loop through literals of enumeration type setting pos and rep values
17511 -- except that if the Ekind is already set, then it means the literal
17512 -- was already constructed (case of a derived type declaration and we
17513 -- should not disturb the Pos and Rep values.
17515 while Present (L) loop
17516 if Ekind (L) /= E_Enumeration_Literal then
17517 Mutate_Ekind (L, E_Enumeration_Literal);
17518 Set_Enumeration_Pos (L, Ev);
17519 Set_Enumeration_Rep (L, Ev);
17520 Set_Is_Known_Valid (L, True);
17524 New_Overloaded_Entity (L);
17525 Generate_Definition (L);
17526 Set_Convention (L, Convention_Intrinsic);
17528 -- Case of character literal
17530 if Nkind (L) = N_Defining_Character_Literal then
17531 Set_Is_Character_Type (T, True);
17533 -- Check violation of No_Wide_Characters
17535 if Restriction_Check_Required (No_Wide_Characters) then
17536 Get_Name_String (Chars (L));
17538 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17539 Check_Restriction (No_Wide_Characters, L);
17548 -- Now create a node representing upper bound
17550 B_Node := New_Node (N_Identifier, Sloc (Def));
17551 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17552 Set_Entity (B_Node, Last (Literals (Def)));
17553 Set_Etype (B_Node, T);
17554 Set_Is_Static_Expression (B_Node, True);
17556 Set_High_Bound (R_Node, B_Node);
17558 -- Initialize various fields of the type. Some of this information
17559 -- may be overwritten later through rep.clauses.
17561 Set_Scalar_Range (T, R_Node);
17562 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17563 Set_Enum_Esize (T);
17564 Set_Enum_Pos_To_Rep (T, Empty);
17566 -- Set Discard_Names if configuration pragma set, or if there is
17567 -- a parameterless pragma in the current declarative region
17569 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17570 Set_Discard_Names (T);
17573 -- Process end label if there is one
17575 if Present (Def) then
17576 Process_End_Label (Def, 'e', T);
17578 end Enumeration_Type_Declaration;
17580 ---------------------------------
17581 -- Expand_To_Stored_Constraint --
17582 ---------------------------------
17584 function Expand_To_Stored_Constraint
17586 Constraint : Elist_Id) return Elist_Id
17588 Explicitly_Discriminated_Type : Entity_Id;
17589 Expansion : Elist_Id;
17590 Discriminant : Entity_Id;
17592 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17593 -- Find the nearest type that actually specifies discriminants
17595 ---------------------------------
17596 -- Type_With_Explicit_Discrims --
17597 ---------------------------------
17599 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17600 Typ : constant E := Base_Type (Id);
17603 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17604 if Present (Full_View (Typ)) then
17605 return Type_With_Explicit_Discrims (Full_View (Typ));
17609 if Has_Discriminants (Typ) then
17614 if Etype (Typ) = Typ then
17616 elsif Has_Discriminants (Typ) then
17619 return Type_With_Explicit_Discrims (Etype (Typ));
17622 end Type_With_Explicit_Discrims;
17624 -- Start of processing for Expand_To_Stored_Constraint
17627 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17631 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17633 if No (Explicitly_Discriminated_Type) then
17637 Expansion := New_Elmt_List;
17640 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17641 while Present (Discriminant) loop
17643 (Get_Discriminant_Value
17644 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17646 Next_Stored_Discriminant (Discriminant);
17650 end Expand_To_Stored_Constraint;
17652 ---------------------------
17653 -- Find_Hidden_Interface --
17654 ---------------------------
17656 function Find_Hidden_Interface
17658 Dest : Elist_Id) return Entity_Id
17661 Iface_Elmt : Elmt_Id;
17664 if Present (Src) and then Present (Dest) then
17665 Iface_Elmt := First_Elmt (Src);
17666 while Present (Iface_Elmt) loop
17667 Iface := Node (Iface_Elmt);
17669 if Is_Interface (Iface)
17670 and then not Contain_Interface (Iface, Dest)
17675 Next_Elmt (Iface_Elmt);
17680 end Find_Hidden_Interface;
17682 --------------------
17683 -- Find_Type_Name --
17684 --------------------
17686 function Find_Type_Name (N : Node_Id) return Entity_Id is
17687 Id : constant Entity_Id := Defining_Identifier (N);
17688 New_Id : Entity_Id;
17690 Prev_Par : Node_Id;
17692 procedure Check_Duplicate_Aspects;
17693 -- Check that aspects specified in a completion have not been specified
17694 -- already in the partial view.
17696 procedure Tag_Mismatch;
17697 -- Diagnose a tagged partial view whose full view is untagged. We post
17698 -- the message on the full view, with a reference to the previous
17699 -- partial view. The partial view can be private or incomplete, and
17700 -- these are handled in a different manner, so we determine the position
17701 -- of the error message from the respective slocs of both.
17703 -----------------------------
17704 -- Check_Duplicate_Aspects --
17705 -----------------------------
17707 procedure Check_Duplicate_Aspects is
17708 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17709 -- Return the corresponding aspect of the partial view which matches
17710 -- the aspect id of Asp. Return Empty is no such aspect exists.
17712 -----------------------------
17713 -- Get_Partial_View_Aspect --
17714 -----------------------------
17716 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17717 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17718 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17719 Prev_Asp : Node_Id;
17722 if Present (Prev_Asps) then
17723 Prev_Asp := First (Prev_Asps);
17724 while Present (Prev_Asp) loop
17725 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17734 end Get_Partial_View_Aspect;
17738 Full_Asps : constant List_Id := Aspect_Specifications (N);
17739 Full_Asp : Node_Id;
17740 Part_Asp : Node_Id;
17742 -- Start of processing for Check_Duplicate_Aspects
17745 if Present (Full_Asps) then
17746 Full_Asp := First (Full_Asps);
17747 while Present (Full_Asp) loop
17748 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17750 -- An aspect and its class-wide counterpart are two distinct
17751 -- aspects and may apply to both views of an entity.
17753 if Present (Part_Asp)
17754 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17757 ("aspect already specified in private declaration",
17764 if Has_Discriminants (Prev)
17765 and then not Has_Unknown_Discriminants (Prev)
17766 and then Get_Aspect_Id (Full_Asp) =
17767 Aspect_Implicit_Dereference
17770 ("cannot specify aspect if partial view has known "
17771 & "discriminants", Full_Asp);
17777 end Check_Duplicate_Aspects;
17783 procedure Tag_Mismatch is
17785 if Sloc (Prev) < Sloc (Id) then
17786 if Ada_Version >= Ada_2012
17787 and then Nkind (N) = N_Private_Type_Declaration
17790 ("declaration of private } must be a tagged type", Id, Prev);
17793 ("full declaration of } must be a tagged type", Id, Prev);
17797 if Ada_Version >= Ada_2012
17798 and then Nkind (N) = N_Private_Type_Declaration
17801 ("declaration of private } must be a tagged type", Prev, Id);
17804 ("full declaration of } must be a tagged type", Prev, Id);
17809 -- Start of processing for Find_Type_Name
17812 -- Find incomplete declaration, if one was given
17814 Prev := Current_Entity_In_Scope (Id);
17816 -- New type declaration
17822 -- Previous declaration exists
17825 Prev_Par := Parent (Prev);
17827 -- Error if not incomplete/private case except if previous
17828 -- declaration is implicit, etc. Enter_Name will emit error if
17831 if not Is_Incomplete_Or_Private_Type (Prev) then
17835 -- Check invalid completion of private or incomplete type
17837 elsif Nkind (N) not in N_Full_Type_Declaration
17838 | N_Task_Type_Declaration
17839 | N_Protected_Type_Declaration
17841 (Ada_Version < Ada_2012
17842 or else not Is_Incomplete_Type (Prev)
17843 or else Nkind (N) not in N_Private_Type_Declaration
17844 | N_Private_Extension_Declaration)
17846 -- Completion must be a full type declarations (RM 7.3(4))
17848 Error_Msg_Sloc := Sloc (Prev);
17849 Error_Msg_NE ("invalid completion of }", Id, Prev);
17851 -- Set scope of Id to avoid cascaded errors. Entity is never
17852 -- examined again, except when saving globals in generics.
17854 Set_Scope (Id, Current_Scope);
17857 -- If this is a repeated incomplete declaration, no further
17858 -- checks are possible.
17860 if Nkind (N) = N_Incomplete_Type_Declaration then
17864 -- Case of full declaration of incomplete type
17866 elsif Ekind (Prev) = E_Incomplete_Type
17867 and then (Ada_Version < Ada_2012
17868 or else No (Full_View (Prev))
17869 or else not Is_Private_Type (Full_View (Prev)))
17871 -- Indicate that the incomplete declaration has a matching full
17872 -- declaration. The defining occurrence of the incomplete
17873 -- declaration remains the visible one, and the procedure
17874 -- Get_Full_View dereferences it whenever the type is used.
17876 if Present (Full_View (Prev)) then
17877 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17880 Set_Full_View (Prev, Id);
17881 Append_Entity (Id, Current_Scope);
17882 Set_Is_Public (Id, Is_Public (Prev));
17883 Set_Is_Internal (Id);
17886 -- If the incomplete view is tagged, a class_wide type has been
17887 -- created already. Use it for the private type as well, in order
17888 -- to prevent multiple incompatible class-wide types that may be
17889 -- created for self-referential anonymous access components.
17891 if Is_Tagged_Type (Prev)
17892 and then Present (Class_Wide_Type (Prev))
17894 Mutate_Ekind (Id, Ekind (Prev)); -- will be reset later
17895 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17897 -- Type of the class-wide type is the current Id. Previously
17898 -- this was not done for private declarations because of order-
17899 -- of-elaboration issues in the back end, but gigi now handles
17902 Set_Etype (Class_Wide_Type (Id), Id);
17905 -- Case of full declaration of private type
17908 -- If the private type was a completion of an incomplete type then
17909 -- update Prev to reference the private type
17911 if Ada_Version >= Ada_2012
17912 and then Ekind (Prev) = E_Incomplete_Type
17913 and then Present (Full_View (Prev))
17914 and then Is_Private_Type (Full_View (Prev))
17916 Prev := Full_View (Prev);
17917 Prev_Par := Parent (Prev);
17920 if Nkind (N) = N_Full_Type_Declaration
17921 and then Nkind (Type_Definition (N)) in
17922 N_Record_Definition | N_Derived_Type_Definition
17923 and then Interface_Present (Type_Definition (N))
17926 ("completion of private type cannot be an interface", N);
17929 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17930 if Etype (Prev) /= Prev then
17932 -- Prev is a private subtype or a derived type, and needs
17935 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17938 elsif Ekind (Prev) = E_Private_Type
17939 and then Nkind (N) in N_Task_Type_Declaration
17940 | N_Protected_Type_Declaration
17943 ("completion of nonlimited type cannot be limited", N);
17945 elsif Ekind (Prev) = E_Record_Type_With_Private
17946 and then Nkind (N) in N_Task_Type_Declaration
17947 | N_Protected_Type_Declaration
17949 if not Is_Limited_Record (Prev) then
17951 ("completion of nonlimited type cannot be limited", N);
17953 elsif No (Interface_List (N)) then
17955 ("completion of tagged private type must be tagged",
17960 -- Ada 2005 (AI-251): Private extension declaration of a task
17961 -- type or a protected type. This case arises when covering
17962 -- interface types.
17964 elsif Nkind (N) in N_Task_Type_Declaration
17965 | N_Protected_Type_Declaration
17969 elsif Nkind (N) /= N_Full_Type_Declaration
17970 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17973 ("full view of private extension must be an extension", N);
17975 elsif not (Abstract_Present (Parent (Prev)))
17976 and then Abstract_Present (Type_Definition (N))
17979 ("full view of non-abstract extension cannot be abstract", N);
17982 if not In_Private_Part (Current_Scope) then
17984 ("declaration of full view must appear in private part", N);
17987 if Ada_Version >= Ada_2012 then
17988 Check_Duplicate_Aspects;
17991 Copy_And_Swap (Prev, Id);
17992 Set_Has_Private_Declaration (Prev);
17993 Set_Has_Private_Declaration (Id);
17995 -- AI12-0133: Indicate whether we have a partial view with
17996 -- unknown discriminants, in which case initialization of objects
17997 -- of the type do not receive an invariant check.
17999 Set_Partial_View_Has_Unknown_Discr
18000 (Prev, Has_Unknown_Discriminants (Id));
18002 -- Preserve aspect and iterator flags that may have been set on
18003 -- the partial view.
18005 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
18006 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
18008 -- If no error, propagate freeze_node from private to full view.
18009 -- It may have been generated for an early operational item.
18011 if Present (Freeze_Node (Id))
18012 and then Serious_Errors_Detected = 0
18013 and then No (Full_View (Id))
18015 Set_Freeze_Node (Prev, Freeze_Node (Id));
18016 Set_Freeze_Node (Id, Empty);
18017 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
18020 Set_Full_View (Id, Prev);
18024 -- Verify that full declaration conforms to partial one
18026 if Is_Incomplete_Or_Private_Type (Prev)
18027 and then Present (Discriminant_Specifications (Prev_Par))
18029 if Present (Discriminant_Specifications (N)) then
18030 if Ekind (Prev) = E_Incomplete_Type then
18031 Check_Discriminant_Conformance (N, Prev, Prev);
18033 Check_Discriminant_Conformance (N, Prev, Id);
18038 ("missing discriminants in full type declaration", N);
18040 -- To avoid cascaded errors on subsequent use, share the
18041 -- discriminants of the partial view.
18043 Set_Discriminant_Specifications (N,
18044 Discriminant_Specifications (Prev_Par));
18048 -- A prior untagged partial view can have an associated class-wide
18049 -- type due to use of the class attribute, and in this case the full
18050 -- type must also be tagged. This Ada 95 usage is deprecated in favor
18051 -- of incomplete tagged declarations, but we check for it.
18054 and then (Is_Tagged_Type (Prev)
18055 or else Present (Class_Wide_Type (Prev)))
18057 -- Ada 2012 (AI05-0162): A private type may be the completion of
18058 -- an incomplete type.
18060 if Ada_Version >= Ada_2012
18061 and then Is_Incomplete_Type (Prev)
18062 and then Nkind (N) in N_Private_Type_Declaration
18063 | N_Private_Extension_Declaration
18065 -- No need to check private extensions since they are tagged
18067 if Nkind (N) = N_Private_Type_Declaration
18068 and then not Tagged_Present (N)
18073 -- The full declaration is either a tagged type (including
18074 -- a synchronized type that implements interfaces) or a
18075 -- type extension, otherwise this is an error.
18077 elsif Nkind (N) in N_Task_Type_Declaration
18078 | N_Protected_Type_Declaration
18080 if No (Interface_List (N)) and then not Error_Posted (N) then
18084 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
18086 -- Indicate that the previous declaration (tagged incomplete
18087 -- or private declaration) requires the same on the full one.
18089 if not Tagged_Present (Type_Definition (N)) then
18091 Set_Is_Tagged_Type (Id);
18094 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
18095 if No (Record_Extension_Part (Type_Definition (N))) then
18097 ("full declaration of } must be a record extension",
18100 -- Set some attributes to produce a usable full view
18102 Set_Is_Tagged_Type (Id);
18111 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
18112 and then Present (Premature_Use (Parent (Prev)))
18114 Error_Msg_Sloc := Sloc (N);
18116 ("\full declaration #", Premature_Use (Parent (Prev)));
18121 end Find_Type_Name;
18123 -------------------------
18124 -- Find_Type_Of_Object --
18125 -------------------------
18127 function Find_Type_Of_Object
18128 (Obj_Def : Node_Id;
18129 Related_Nod : Node_Id) return Entity_Id
18131 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
18132 P : Node_Id := Parent (Obj_Def);
18137 -- If the parent is a component_definition node we climb to the
18138 -- component_declaration node
18140 if Nkind (P) = N_Component_Definition then
18144 -- Case of an anonymous array subtype
18146 if Def_Kind in N_Array_Type_Definition then
18148 Array_Type_Declaration (T, Obj_Def);
18150 -- Create an explicit subtype whenever possible
18152 elsif Nkind (P) /= N_Component_Declaration
18153 and then Def_Kind = N_Subtype_Indication
18155 -- Base name of subtype on object name, which will be unique in
18156 -- the current scope.
18158 -- If this is a duplicate declaration, return base type, to avoid
18159 -- generating duplicate anonymous types.
18161 if Error_Posted (P) then
18162 Analyze (Subtype_Mark (Obj_Def));
18163 return Entity (Subtype_Mark (Obj_Def));
18168 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
18170 T := Make_Defining_Identifier (Sloc (P), Nam);
18172 -- If In_Spec_Expression, for example within a pre/postcondition,
18173 -- provide enough information for use of the subtype without
18174 -- depending on full analysis and freezing, which will happen when
18175 -- building the correspondiing subprogram.
18177 if In_Spec_Expression then
18178 Analyze (Subtype_Mark (Obj_Def));
18181 Base_T : constant Entity_Id := Entity (Subtype_Mark (Obj_Def));
18182 Decl : constant Node_Id :=
18183 Make_Subtype_Declaration (Sloc (P),
18184 Defining_Identifier => T,
18185 Subtype_Indication => Relocate_Node (Obj_Def));
18187 Set_Etype (T, Base_T);
18188 Mutate_Ekind (T, Subtype_Kind (Ekind (Base_T)));
18189 Set_Parent (T, Obj_Def);
18191 if Ekind (T) = E_Array_Subtype then
18192 Set_First_Index (T, First_Index (Base_T));
18193 Set_Is_Constrained (T);
18195 elsif Ekind (T) = E_Record_Subtype then
18196 Set_First_Entity (T, First_Entity (Base_T));
18197 Set_Has_Discriminants (T, Has_Discriminants (Base_T));
18198 Set_Is_Constrained (T);
18201 Insert_Before (Related_Nod, Decl);
18207 -- When generating code, insert subtype declaration ahead of
18208 -- declaration that generated it.
18210 Insert_Action (Obj_Def,
18211 Make_Subtype_Declaration (Sloc (P),
18212 Defining_Identifier => T,
18213 Subtype_Indication => Relocate_Node (Obj_Def)));
18215 -- This subtype may need freezing, and this will not be done
18216 -- automatically if the object declaration is not in declarative
18217 -- part. Since this is an object declaration, the type cannot always
18218 -- be frozen here. Deferred constants do not freeze their type
18219 -- (which often enough will be private).
18221 if Nkind (P) = N_Object_Declaration
18222 and then Constant_Present (P)
18223 and then No (Expression (P))
18227 -- Here we freeze the base type of object type to catch premature use
18228 -- of discriminated private type without a full view.
18231 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
18234 -- Ada 2005 AI-406: the object definition in an object declaration
18235 -- can be an access definition.
18237 elsif Def_Kind = N_Access_Definition then
18238 T := Access_Definition (Related_Nod, Obj_Def);
18240 Set_Is_Local_Anonymous_Access
18241 (T, Ada_Version < Ada_2012
18242 or else Nkind (P) /= N_Object_Declaration
18243 or else Is_Library_Level_Entity (Defining_Identifier (P)));
18245 -- Otherwise, the object definition is just a subtype_mark
18248 T := Process_Subtype (Obj_Def, Related_Nod);
18252 end Find_Type_Of_Object;
18254 --------------------------------
18255 -- Find_Type_Of_Subtype_Indic --
18256 --------------------------------
18258 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
18262 -- Case of subtype mark with a constraint
18264 if Nkind (S) = N_Subtype_Indication then
18265 Find_Type (Subtype_Mark (S));
18266 Typ := Entity (Subtype_Mark (S));
18269 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
18272 ("incorrect constraint for this kind of type", Constraint (S));
18273 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
18276 -- Otherwise we have a subtype mark without a constraint
18278 elsif Error_Posted (S) then
18279 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
18288 end Find_Type_Of_Subtype_Indic;
18290 -------------------------------------
18291 -- Floating_Point_Type_Declaration --
18292 -------------------------------------
18294 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18295 Digs : constant Node_Id := Digits_Expression (Def);
18296 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
18298 Base_Typ : Entity_Id;
18299 Implicit_Base : Entity_Id;
18301 function Can_Derive_From (E : Entity_Id) return Boolean;
18302 -- Find if given digits value, and possibly a specified range, allows
18303 -- derivation from specified type
18305 procedure Convert_Bound (B : Node_Id);
18306 -- If specified, the bounds must be static but may be of different
18307 -- types. They must be converted into machine numbers of the base type,
18308 -- in accordance with RM 4.9(38).
18310 function Find_Base_Type return Entity_Id;
18311 -- Find a predefined base type that Def can derive from, or generate
18312 -- an error and substitute Long_Long_Float if none exists.
18314 ---------------------
18315 -- Can_Derive_From --
18316 ---------------------
18318 function Can_Derive_From (E : Entity_Id) return Boolean is
18319 Spec : constant Entity_Id := Real_Range_Specification (Def);
18322 -- Check specified "digits" constraint
18324 if Digs_Val > Digits_Value (E) then
18328 -- Check for matching range, if specified
18330 if Present (Spec) then
18331 if Expr_Value_R (Type_Low_Bound (E)) >
18332 Expr_Value_R (Low_Bound (Spec))
18337 if Expr_Value_R (Type_High_Bound (E)) <
18338 Expr_Value_R (High_Bound (Spec))
18345 end Can_Derive_From;
18347 -------------------
18348 -- Convert_Bound --
18349 --------------------
18351 procedure Convert_Bound (B : Node_Id) is
18353 -- If the bound is not a literal it can only be static if it is
18354 -- a static constant, possibly of a specified type.
18356 if Is_Entity_Name (B)
18357 and then Ekind (Entity (B)) = E_Constant
18359 Rewrite (B, Constant_Value (Entity (B)));
18362 if Nkind (B) = N_Real_Literal then
18363 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
18364 Set_Is_Machine_Number (B);
18365 Set_Etype (B, Base_Typ);
18369 --------------------
18370 -- Find_Base_Type --
18371 --------------------
18373 function Find_Base_Type return Entity_Id is
18374 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
18377 -- Iterate over the predefined types in order, returning the first
18378 -- one that Def can derive from.
18380 while Present (Choice) loop
18381 if Can_Derive_From (Node (Choice)) then
18382 return Node (Choice);
18385 Next_Elmt (Choice);
18388 -- If we can't derive from any existing type, use Long_Long_Float
18389 -- and give appropriate message explaining the problem.
18391 if Digs_Val > Max_Digs_Val then
18392 -- It might be the case that there is a type with the requested
18393 -- range, just not the combination of digits and range.
18396 ("no predefined type has requested range and precision",
18397 Real_Range_Specification (Def));
18401 ("range too large for any predefined type",
18402 Real_Range_Specification (Def));
18405 return Standard_Long_Long_Float;
18406 end Find_Base_Type;
18408 -- Start of processing for Floating_Point_Type_Declaration
18411 Check_Restriction (No_Floating_Point, Def);
18413 -- Create an implicit base type
18416 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
18418 -- Analyze and verify digits value
18420 Analyze_And_Resolve (Digs, Any_Integer);
18421 Check_Digits_Expression (Digs);
18422 Digs_Val := Expr_Value (Digs);
18424 -- Process possible range spec and find correct type to derive from
18426 Process_Real_Range_Specification (Def);
18428 -- Check that requested number of digits is not too high.
18430 if Digs_Val > Max_Digs_Val then
18432 -- The check for Max_Base_Digits may be somewhat expensive, as it
18433 -- requires reading System, so only do it when necessary.
18436 Max_Base_Digits : constant Uint :=
18439 (Parent (RTE (RE_Max_Base_Digits))));
18442 if Digs_Val > Max_Base_Digits then
18443 Error_Msg_Uint_1 := Max_Base_Digits;
18444 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
18446 elsif No (Real_Range_Specification (Def)) then
18447 Error_Msg_Uint_1 := Max_Digs_Val;
18448 Error_Msg_N ("types with more than ^ digits need range spec "
18449 & "(RM 3.5.7(6))", Digs);
18454 -- Find a suitable type to derive from or complain and use a substitute
18456 Base_Typ := Find_Base_Type;
18458 -- If there are bounds given in the declaration use them as the bounds
18459 -- of the type, otherwise use the bounds of the predefined base type
18460 -- that was chosen based on the Digits value.
18462 if Present (Real_Range_Specification (Def)) then
18463 Set_Scalar_Range (T, Real_Range_Specification (Def));
18464 Set_Is_Constrained (T);
18466 Convert_Bound (Type_Low_Bound (T));
18467 Convert_Bound (Type_High_Bound (T));
18470 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
18473 -- Complete definition of implicit base and declared first subtype. The
18474 -- inheritance of the rep item chain ensures that SPARK-related pragmas
18475 -- are not clobbered when the floating point type acts as a full view of
18478 Set_Etype (Implicit_Base, Base_Typ);
18479 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
18480 Set_Size_Info (Implicit_Base, Base_Typ);
18481 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
18482 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18483 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
18484 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
18486 Mutate_Ekind (T, E_Floating_Point_Subtype);
18487 Set_Etype (T, Implicit_Base);
18488 Set_Size_Info (T, Implicit_Base);
18489 Set_RM_Size (T, RM_Size (Implicit_Base));
18490 Inherit_Rep_Item_Chain (T, Implicit_Base);
18491 Set_Digits_Value (T, Digs_Val);
18492 end Floating_Point_Type_Declaration;
18494 ----------------------------
18495 -- Get_Discriminant_Value --
18496 ----------------------------
18498 -- This is the situation:
18500 -- There is a non-derived type
18502 -- type T0 (Dx, Dy, Dz...)
18504 -- There are zero or more levels of derivation, with each derivation
18505 -- either purely inheriting the discriminants, or defining its own.
18507 -- type Ti is new Ti-1
18509 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
18511 -- subtype Ti is ...
18513 -- The subtype issue is avoided by the use of Original_Record_Component,
18514 -- and the fact that derived subtypes also derive the constraints.
18516 -- This chain leads back from
18518 -- Typ_For_Constraint
18520 -- Typ_For_Constraint has discriminants, and the value for each
18521 -- discriminant is given by its corresponding Elmt of Constraints.
18523 -- Discriminant is some discriminant in this hierarchy
18525 -- We need to return its value
18527 -- We do this by recursively searching each level, and looking for
18528 -- Discriminant. Once we get to the bottom, we start backing up
18529 -- returning the value for it which may in turn be a discriminant
18530 -- further up, so on the backup we continue the substitution.
18532 function Get_Discriminant_Value
18533 (Discriminant : Entity_Id;
18534 Typ_For_Constraint : Entity_Id;
18535 Constraint : Elist_Id) return Node_Id
18537 function Root_Corresponding_Discriminant
18538 (Discr : Entity_Id) return Entity_Id;
18539 -- Given a discriminant, traverse the chain of inherited discriminants
18540 -- and return the topmost discriminant.
18542 function Search_Derivation_Levels
18544 Discrim_Values : Elist_Id;
18545 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18546 -- This is the routine that performs the recursive search of levels
18547 -- as described above.
18549 -------------------------------------
18550 -- Root_Corresponding_Discriminant --
18551 -------------------------------------
18553 function Root_Corresponding_Discriminant
18554 (Discr : Entity_Id) return Entity_Id
18560 while Present (Corresponding_Discriminant (D)) loop
18561 D := Corresponding_Discriminant (D);
18565 end Root_Corresponding_Discriminant;
18567 ------------------------------
18568 -- Search_Derivation_Levels --
18569 ------------------------------
18571 function Search_Derivation_Levels
18573 Discrim_Values : Elist_Id;
18574 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18578 Result : Node_Or_Entity_Id;
18579 Result_Entity : Node_Id;
18582 -- If inappropriate type, return Error, this happens only in
18583 -- cascaded error situations, and we want to avoid a blow up.
18585 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18589 -- Look deeper if possible. Use Stored_Constraints only for
18590 -- untagged types. For tagged types use the given constraint.
18591 -- This asymmetry needs explanation???
18593 if not Stored_Discrim_Values
18594 and then Present (Stored_Constraint (Ti))
18595 and then not Is_Tagged_Type (Ti)
18598 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18602 Td : Entity_Id := Etype (Ti);
18605 -- If the parent type is private, the full view may include
18606 -- renamed discriminants, and it is those stored values that
18607 -- may be needed (the partial view never has more information
18608 -- than the full view).
18610 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18611 Td := Full_View (Td);
18615 Result := Discriminant;
18618 if Present (Stored_Constraint (Ti)) then
18620 Search_Derivation_Levels
18621 (Td, Stored_Constraint (Ti), True);
18624 Search_Derivation_Levels
18625 (Td, Discrim_Values, Stored_Discrim_Values);
18631 -- Extra underlying places to search, if not found above. For
18632 -- concurrent types, the relevant discriminant appears in the
18633 -- corresponding record. For a type derived from a private type
18634 -- without discriminant, the full view inherits the discriminants
18635 -- of the full view of the parent.
18637 if Result = Discriminant then
18638 if Is_Concurrent_Type (Ti)
18639 and then Present (Corresponding_Record_Type (Ti))
18642 Search_Derivation_Levels (
18643 Corresponding_Record_Type (Ti),
18645 Stored_Discrim_Values);
18647 elsif Is_Private_Type (Ti)
18648 and then not Has_Discriminants (Ti)
18649 and then Present (Full_View (Ti))
18650 and then Etype (Full_View (Ti)) /= Ti
18653 Search_Derivation_Levels (
18656 Stored_Discrim_Values);
18660 -- If Result is not a (reference to a) discriminant, return it,
18661 -- otherwise set Result_Entity to the discriminant.
18663 if Nkind (Result) = N_Defining_Identifier then
18664 pragma Assert (Result = Discriminant);
18665 Result_Entity := Result;
18668 if not Denotes_Discriminant (Result) then
18672 Result_Entity := Entity (Result);
18675 -- See if this level of derivation actually has discriminants because
18676 -- tagged derivations can add them, hence the lower levels need not
18679 if not Has_Discriminants (Ti) then
18683 -- Scan Ti's discriminants for Result_Entity, and return its
18684 -- corresponding value, if any.
18686 Result_Entity := Original_Record_Component (Result_Entity);
18688 Assoc := First_Elmt (Discrim_Values);
18690 if Stored_Discrim_Values then
18691 Disc := First_Stored_Discriminant (Ti);
18693 Disc := First_Discriminant (Ti);
18696 while Present (Disc) loop
18698 -- If no further associations return the discriminant, value will
18699 -- be found on the second pass.
18705 if Original_Record_Component (Disc) = Result_Entity then
18706 return Node (Assoc);
18711 if Stored_Discrim_Values then
18712 Next_Stored_Discriminant (Disc);
18714 Next_Discriminant (Disc);
18718 -- Could not find it
18721 end Search_Derivation_Levels;
18725 Result : Node_Or_Entity_Id;
18727 -- Start of processing for Get_Discriminant_Value
18730 -- ??? This routine is a gigantic mess and will be deleted. For the
18731 -- time being just test for the trivial case before calling recurse.
18733 -- We are now celebrating the 20th anniversary of this comment!
18735 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18741 D := First_Discriminant (Typ_For_Constraint);
18742 E := First_Elmt (Constraint);
18743 while Present (D) loop
18744 if Chars (D) = Chars (Discriminant) then
18748 Next_Discriminant (D);
18754 Result := Search_Derivation_Levels
18755 (Typ_For_Constraint, Constraint, False);
18757 -- ??? hack to disappear when this routine is gone
18759 if Nkind (Result) = N_Defining_Identifier then
18765 D := First_Discriminant (Typ_For_Constraint);
18766 E := First_Elmt (Constraint);
18767 while Present (D) loop
18768 if Root_Corresponding_Discriminant (D) = Discriminant then
18772 Next_Discriminant (D);
18778 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18780 end Get_Discriminant_Value;
18782 --------------------------
18783 -- Has_Range_Constraint --
18784 --------------------------
18786 function Has_Range_Constraint (N : Node_Id) return Boolean is
18787 C : constant Node_Id := Constraint (N);
18790 if Nkind (C) = N_Range_Constraint then
18793 elsif Nkind (C) = N_Digits_Constraint then
18795 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18796 or else Present (Range_Constraint (C));
18798 elsif Nkind (C) = N_Delta_Constraint then
18799 return Present (Range_Constraint (C));
18804 end Has_Range_Constraint;
18806 ------------------------
18807 -- Inherit_Components --
18808 ------------------------
18810 function Inherit_Components
18812 Parent_Base : Entity_Id;
18813 Derived_Base : Entity_Id;
18814 Is_Tagged : Boolean;
18815 Inherit_Discr : Boolean;
18816 Discs : Elist_Id) return Elist_Id
18818 Assoc_List : constant Elist_Id := New_Elmt_List;
18820 procedure Inherit_Component
18821 (Old_C : Entity_Id;
18822 Plain_Discrim : Boolean := False;
18823 Stored_Discrim : Boolean := False);
18824 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18825 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18826 -- True, Old_C is a stored discriminant. If they are both false then
18827 -- Old_C is a regular component.
18829 -----------------------
18830 -- Inherit_Component --
18831 -----------------------
18833 procedure Inherit_Component
18834 (Old_C : Entity_Id;
18835 Plain_Discrim : Boolean := False;
18836 Stored_Discrim : Boolean := False)
18838 procedure Set_Anonymous_Type (Id : Entity_Id);
18839 -- Id denotes the entity of an access discriminant or anonymous
18840 -- access component. Set the type of Id to either the same type of
18841 -- Old_C or create a new one depending on whether the parent and
18842 -- the child types are in the same scope.
18844 ------------------------
18845 -- Set_Anonymous_Type --
18846 ------------------------
18848 procedure Set_Anonymous_Type (Id : Entity_Id) is
18849 Old_Typ : constant Entity_Id := Etype (Old_C);
18852 if Scope (Parent_Base) = Scope (Derived_Base) then
18853 Set_Etype (Id, Old_Typ);
18855 -- The parent and the derived type are in two different scopes.
18856 -- Reuse the type of the original discriminant / component by
18857 -- copying it in order to preserve all attributes.
18861 Typ : constant Entity_Id := New_Copy (Old_Typ);
18864 Set_Etype (Id, Typ);
18866 -- Since we do not generate component declarations for
18867 -- inherited components, associate the itype with the
18870 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18871 Set_Scope (Typ, Derived_Base);
18874 end Set_Anonymous_Type;
18876 -- Local variables and constants
18878 New_C : constant Entity_Id := New_Copy (Old_C);
18880 Corr_Discrim : Entity_Id;
18881 Discrim : Entity_Id;
18883 -- Start of processing for Inherit_Component
18886 pragma Assert (not Is_Tagged or not Stored_Discrim);
18888 Set_Parent (New_C, Parent (Old_C));
18890 -- Regular discriminants and components must be inserted in the scope
18891 -- of the Derived_Base. Do it here.
18893 if not Stored_Discrim then
18894 Enter_Name (New_C);
18897 -- For tagged types the Original_Record_Component must point to
18898 -- whatever this field was pointing to in the parent type. This has
18899 -- already been achieved by the call to New_Copy above.
18901 if not Is_Tagged then
18902 Set_Original_Record_Component (New_C, New_C);
18903 Set_Corresponding_Record_Component (New_C, Old_C);
18906 -- Set the proper type of an access discriminant
18908 if Ekind (New_C) = E_Discriminant
18909 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18911 Set_Anonymous_Type (New_C);
18914 -- If we have inherited a component then see if its Etype contains
18915 -- references to Parent_Base discriminants. In this case, replace
18916 -- these references with the constraints given in Discs. We do not
18917 -- do this for the partial view of private types because this is
18918 -- not needed (only the components of the full view will be used
18919 -- for code generation) and cause problem. We also avoid this
18920 -- transformation in some error situations.
18922 if Ekind (New_C) = E_Component then
18924 -- Set the proper type of an anonymous access component
18926 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18927 Set_Anonymous_Type (New_C);
18929 elsif (Is_Private_Type (Derived_Base)
18930 and then not Is_Generic_Type (Derived_Base))
18931 or else (Is_Empty_Elmt_List (Discs)
18932 and then not Expander_Active)
18934 Set_Etype (New_C, Etype (Old_C));
18937 -- The current component introduces a circularity of the
18940 -- limited with Pack_2;
18941 -- package Pack_1 is
18942 -- type T_1 is tagged record
18943 -- Comp : access Pack_2.T_2;
18949 -- package Pack_2 is
18950 -- type T_2 is new Pack_1.T_1 with ...;
18955 Constrain_Component_Type
18956 (Old_C, Derived_Base, N, Parent_Base, Discs));
18960 -- In derived tagged types it is illegal to reference a non
18961 -- discriminant component in the parent type. To catch this, mark
18962 -- these components with an Ekind of E_Void. This will be reset in
18963 -- Record_Type_Definition after processing the record extension of
18964 -- the derived type.
18966 -- If the declaration is a private extension, there is no further
18967 -- record extension to process, and the components retain their
18968 -- current kind, because they are visible at this point.
18970 if Is_Tagged and then Ekind (New_C) = E_Component
18971 and then Nkind (N) /= N_Private_Extension_Declaration
18973 Mutate_Ekind (New_C, E_Void);
18976 if Plain_Discrim then
18977 Set_Corresponding_Discriminant (New_C, Old_C);
18978 Build_Discriminal (New_C);
18980 -- If we are explicitly inheriting a stored discriminant it will be
18981 -- completely hidden.
18983 elsif Stored_Discrim then
18984 Set_Corresponding_Discriminant (New_C, Empty);
18985 Set_Discriminal (New_C, Empty);
18986 Set_Is_Completely_Hidden (New_C);
18988 -- Set the Original_Record_Component of each discriminant in the
18989 -- derived base to point to the corresponding stored that we just
18992 Discrim := First_Discriminant (Derived_Base);
18993 while Present (Discrim) loop
18994 Corr_Discrim := Corresponding_Discriminant (Discrim);
18996 -- Corr_Discrim could be missing in an error situation
18998 if Present (Corr_Discrim)
18999 and then Original_Record_Component (Corr_Discrim) = Old_C
19001 Set_Original_Record_Component (Discrim, New_C);
19002 Set_Corresponding_Record_Component (Discrim, Empty);
19005 Next_Discriminant (Discrim);
19008 Append_Entity (New_C, Derived_Base);
19011 if not Is_Tagged then
19012 Append_Elmt (Old_C, Assoc_List);
19013 Append_Elmt (New_C, Assoc_List);
19015 end Inherit_Component;
19017 -- Variables local to Inherit_Component
19019 Loc : constant Source_Ptr := Sloc (N);
19021 Parent_Discrim : Entity_Id;
19022 Stored_Discrim : Entity_Id;
19024 Component : Entity_Id;
19026 -- Start of processing for Inherit_Components
19029 if not Is_Tagged then
19030 Append_Elmt (Parent_Base, Assoc_List);
19031 Append_Elmt (Derived_Base, Assoc_List);
19034 -- Inherit parent discriminants if needed
19036 if Inherit_Discr then
19037 Parent_Discrim := First_Discriminant (Parent_Base);
19038 while Present (Parent_Discrim) loop
19039 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
19040 Next_Discriminant (Parent_Discrim);
19044 -- Create explicit stored discrims for untagged types when necessary
19046 if not Has_Unknown_Discriminants (Derived_Base)
19047 and then Has_Discriminants (Parent_Base)
19048 and then not Is_Tagged
19051 or else First_Discriminant (Parent_Base) /=
19052 First_Stored_Discriminant (Parent_Base))
19054 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
19055 while Present (Stored_Discrim) loop
19056 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
19057 Next_Stored_Discriminant (Stored_Discrim);
19061 -- See if we can apply the second transformation for derived types, as
19062 -- explained in point 6. in the comments above Build_Derived_Record_Type
19063 -- This is achieved by appending Derived_Base discriminants into Discs,
19064 -- which has the side effect of returning a non empty Discs list to the
19065 -- caller of Inherit_Components, which is what we want. This must be
19066 -- done for private derived types if there are explicit stored
19067 -- discriminants, to ensure that we can retrieve the values of the
19068 -- constraints provided in the ancestors.
19071 and then Is_Empty_Elmt_List (Discs)
19072 and then Present (First_Discriminant (Derived_Base))
19074 (not Is_Private_Type (Derived_Base)
19075 or else Is_Completely_Hidden
19076 (First_Stored_Discriminant (Derived_Base))
19077 or else Is_Generic_Type (Derived_Base))
19079 D := First_Discriminant (Derived_Base);
19080 while Present (D) loop
19081 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
19082 Next_Discriminant (D);
19086 -- Finally, inherit non-discriminant components unless they are not
19087 -- visible because defined or inherited from the full view of the
19088 -- parent. Don't inherit the _parent field of the parent type.
19090 Component := First_Entity (Parent_Base);
19091 while Present (Component) loop
19093 -- Ada 2005 (AI-251): Do not inherit components associated with
19094 -- secondary tags of the parent.
19096 if Ekind (Component) = E_Component
19097 and then Present (Related_Type (Component))
19101 elsif Ekind (Component) /= E_Component
19102 or else Chars (Component) = Name_uParent
19106 -- If the derived type is within the parent type's declarative
19107 -- region, then the components can still be inherited even though
19108 -- they aren't visible at this point. This can occur for cases
19109 -- such as within public child units where the components must
19110 -- become visible upon entering the child unit's private part.
19112 elsif not Is_Visible_Component (Component)
19113 and then not In_Open_Scopes (Scope (Parent_Base))
19117 elsif Ekind (Derived_Base) in E_Private_Type | E_Limited_Private_Type
19122 Inherit_Component (Component);
19125 Next_Entity (Component);
19128 -- For tagged derived types, inherited discriminants cannot be used in
19129 -- component declarations of the record extension part. To achieve this
19130 -- we mark the inherited discriminants as not visible.
19132 if Is_Tagged and then Inherit_Discr then
19133 D := First_Discriminant (Derived_Base);
19134 while Present (D) loop
19135 Set_Is_Immediately_Visible (D, False);
19136 Next_Discriminant (D);
19141 end Inherit_Components;
19143 ----------------------
19144 -- Is_EVF_Procedure --
19145 ----------------------
19147 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
19148 Formal : Entity_Id;
19151 -- Examine the formals of an Extensions_Visible False procedure looking
19152 -- for a controlling OUT parameter.
19154 if Ekind (Subp) = E_Procedure
19155 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
19157 Formal := First_Formal (Subp);
19158 while Present (Formal) loop
19159 if Ekind (Formal) = E_Out_Parameter
19160 and then Is_Controlling_Formal (Formal)
19165 Next_Formal (Formal);
19170 end Is_EVF_Procedure;
19172 --------------------------
19173 -- Is_Private_Primitive --
19174 --------------------------
19176 function Is_Private_Primitive (Prim : Entity_Id) return Boolean is
19177 Prim_Scope : constant Entity_Id := Scope (Prim);
19178 Priv_Entity : Entity_Id;
19180 if Is_Package_Or_Generic_Package (Prim_Scope) then
19181 Priv_Entity := First_Private_Entity (Prim_Scope);
19183 while Present (Priv_Entity) loop
19184 if Priv_Entity = Prim then
19188 Next_Entity (Priv_Entity);
19193 end Is_Private_Primitive;
19195 ------------------------------
19196 -- Is_Valid_Constraint_Kind --
19197 ------------------------------
19199 function Is_Valid_Constraint_Kind
19200 (T_Kind : Type_Kind;
19201 Constraint_Kind : Node_Kind) return Boolean
19205 when Enumeration_Kind
19208 return Constraint_Kind = N_Range_Constraint;
19210 when Decimal_Fixed_Point_Kind =>
19211 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19213 when Ordinary_Fixed_Point_Kind =>
19214 return Constraint_Kind in N_Delta_Constraint | N_Range_Constraint;
19217 return Constraint_Kind in N_Digits_Constraint | N_Range_Constraint;
19224 | E_Incomplete_Type
19228 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
19231 return True; -- Error will be detected later
19233 end Is_Valid_Constraint_Kind;
19235 --------------------------
19236 -- Is_Visible_Component --
19237 --------------------------
19239 function Is_Visible_Component
19241 N : Node_Id := Empty) return Boolean
19243 Original_Comp : Entity_Id := Empty;
19244 Original_Type : Entity_Id;
19245 Type_Scope : Entity_Id;
19247 function Is_Local_Type (Typ : Entity_Id) return Boolean;
19248 -- Check whether parent type of inherited component is declared locally,
19249 -- possibly within a nested package or instance. The current scope is
19250 -- the derived record itself.
19252 -------------------
19253 -- Is_Local_Type --
19254 -------------------
19256 function Is_Local_Type (Typ : Entity_Id) return Boolean is
19258 return Scope_Within (Inner => Typ, Outer => Scope (Current_Scope));
19261 -- Start of processing for Is_Visible_Component
19264 if Ekind (C) in E_Component | E_Discriminant then
19265 Original_Comp := Original_Record_Component (C);
19268 if No (Original_Comp) then
19270 -- Premature usage, or previous error
19275 Original_Type := Scope (Original_Comp);
19276 Type_Scope := Scope (Base_Type (Scope (C)));
19279 -- This test only concerns tagged types
19281 if not Is_Tagged_Type (Original_Type) then
19283 -- Check if this is a renamed discriminant (hidden either by the
19284 -- derived type or by some ancestor), unless we are analyzing code
19285 -- generated by the expander since it may reference such components
19286 -- (for example see the expansion of Deep_Adjust).
19288 if Ekind (C) = E_Discriminant and then Present (N) then
19290 not Comes_From_Source (N)
19291 or else not Is_Completely_Hidden (C);
19296 -- If it is _Parent or _Tag, there is no visibility issue
19298 elsif not Comes_From_Source (Original_Comp) then
19301 -- Discriminants are visible unless the (private) type has unknown
19302 -- discriminants. If the discriminant reference is inserted for a
19303 -- discriminant check on a full view it is also visible.
19305 elsif Ekind (Original_Comp) = E_Discriminant
19307 (not Has_Unknown_Discriminants (Original_Type)
19308 or else (Present (N)
19309 and then Nkind (N) = N_Selected_Component
19310 and then Nkind (Prefix (N)) = N_Type_Conversion
19311 and then not Comes_From_Source (Prefix (N))))
19315 -- If the component has been declared in an ancestor which is currently
19316 -- a private type, then it is not visible. The same applies if the
19317 -- component's containing type is not in an open scope and the original
19318 -- component's enclosing type is a visible full view of a private type
19319 -- (which can occur in cases where an attempt is being made to reference
19320 -- a component in a sibling package that is inherited from a visible
19321 -- component of a type in an ancestor package; the component in the
19322 -- sibling package should not be visible even though the component it
19323 -- inherited from is visible), but instance bodies are not subject to
19324 -- this second case since they have the Has_Private_View mechanism to
19325 -- ensure proper visibility. This does not apply however in the case
19326 -- where the scope of the type is a private child unit, or when the
19327 -- parent comes from a local package in which the ancestor is currently
19328 -- visible. The latter suppression of visibility is needed for cases
19329 -- that are tested in B730006.
19331 elsif Is_Private_Type (Original_Type)
19333 (not Is_Private_Descendant (Type_Scope)
19334 and then not In_Open_Scopes (Type_Scope)
19335 and then Has_Private_Declaration (Original_Type)
19336 and then not In_Instance_Body)
19338 -- If the type derives from an entity in a formal package, there
19339 -- are no additional visible components.
19341 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
19342 N_Formal_Package_Declaration
19346 -- if we are not in the private part of the current package, there
19347 -- are no additional visible components.
19349 elsif Ekind (Scope (Current_Scope)) = E_Package
19350 and then not In_Private_Part (Scope (Current_Scope))
19355 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
19356 and then In_Open_Scopes (Scope (Original_Type))
19357 and then Is_Local_Type (Type_Scope);
19360 -- There is another weird way in which a component may be invisible when
19361 -- the private and the full view are not derived from the same ancestor.
19362 -- Here is an example :
19364 -- type A1 is tagged record F1 : integer; end record;
19365 -- type A2 is new A1 with record F2 : integer; end record;
19366 -- type T is new A1 with private;
19368 -- type T is new A2 with null record;
19370 -- In this case, the full view of T inherits F1 and F2 but the private
19371 -- view inherits only F1
19375 Ancestor : Entity_Id := Scope (C);
19379 if Ancestor = Original_Type then
19382 -- The ancestor may have a partial view of the original type,
19383 -- but if the full view is in scope, as in a child body, the
19384 -- component is visible.
19386 elsif In_Private_Part (Scope (Original_Type))
19387 and then Full_View (Ancestor) = Original_Type
19391 elsif Ancestor = Etype (Ancestor) then
19393 -- No further ancestors to examine
19398 Ancestor := Etype (Ancestor);
19402 end Is_Visible_Component;
19404 --------------------------
19405 -- Make_Class_Wide_Type --
19406 --------------------------
19408 procedure Make_Class_Wide_Type (T : Entity_Id) is
19409 CW_Type : Entity_Id;
19411 Next_E : Entity_Id;
19412 Prev_E : Entity_Id;
19415 if Present (Class_Wide_Type (T)) then
19417 -- The class-wide type is a partially decorated entity created for a
19418 -- unanalyzed tagged type referenced through a limited with clause.
19419 -- When the tagged type is analyzed, its class-wide type needs to be
19420 -- redecorated. Note that we reuse the entity created by Decorate_
19421 -- Tagged_Type in order to preserve all links.
19423 if Materialize_Entity (Class_Wide_Type (T)) then
19424 CW_Type := Class_Wide_Type (T);
19425 Set_Materialize_Entity (CW_Type, False);
19427 -- The class wide type can have been defined by the partial view, in
19428 -- which case everything is already done.
19434 -- Default case, we need to create a new class-wide type
19438 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19441 -- Inherit root type characteristics
19443 CW_Name := Chars (CW_Type);
19444 Next_E := Next_Entity (CW_Type);
19445 Prev_E := Prev_Entity (CW_Type);
19446 Copy_Node (T, CW_Type);
19447 Set_Comes_From_Source (CW_Type, False);
19448 Set_Chars (CW_Type, CW_Name);
19449 Set_Parent (CW_Type, Parent (T));
19450 Set_Prev_Entity (CW_Type, Prev_E);
19451 Set_Next_Entity (CW_Type, Next_E);
19453 -- Ensure we have a new freeze node for the class-wide type. The partial
19454 -- view may have freeze action of its own, requiring a proper freeze
19455 -- node, and the same freeze node cannot be shared between the two
19458 Set_Has_Delayed_Freeze (CW_Type);
19459 Set_Freeze_Node (CW_Type, Empty);
19461 -- Customize the class-wide type: It has no prim. op., it cannot be
19462 -- abstract, its Etype points back to the specific root type, and it
19463 -- cannot have any invariants.
19465 if Ekind (CW_Type) in Incomplete_Or_Private_Kind then
19466 Reinit_Field_To_Zero (CW_Type, F_Private_Dependents);
19468 elsif Ekind (CW_Type) in Concurrent_Kind then
19469 Reinit_Field_To_Zero (CW_Type, F_First_Private_Entity);
19470 Reinit_Field_To_Zero (CW_Type, F_Scope_Depth_Value);
19472 if Ekind (CW_Type) in Task_Kind then
19473 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Checks_OK_Id);
19474 Reinit_Field_To_Zero (CW_Type, F_Is_Elaboration_Warnings_OK_Id);
19477 if Ekind (CW_Type) in E_Task_Type | E_Protected_Type then
19478 Reinit_Field_To_Zero (CW_Type, F_SPARK_Aux_Pragma_Inherited);
19482 Mutate_Ekind (CW_Type, E_Class_Wide_Type);
19483 Set_Is_Tagged_Type (CW_Type, True);
19484 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19485 Set_Is_Abstract_Type (CW_Type, False);
19486 Set_Is_Constrained (CW_Type, False);
19487 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19488 Set_Default_SSO (CW_Type);
19489 Set_Has_Inheritable_Invariants (CW_Type, False);
19490 Set_Has_Inherited_Invariants (CW_Type, False);
19491 Set_Has_Own_Invariants (CW_Type, False);
19493 if Ekind (T) = E_Class_Wide_Subtype then
19494 Set_Etype (CW_Type, Etype (Base_Type (T)));
19496 Set_Etype (CW_Type, T);
19499 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19501 -- If this is the class_wide type of a constrained subtype, it does
19502 -- not have discriminants.
19504 Set_Has_Discriminants (CW_Type,
19505 Has_Discriminants (T) and then not Is_Constrained (T));
19507 Set_Has_Unknown_Discriminants (CW_Type, True);
19508 Set_Class_Wide_Type (T, CW_Type);
19509 Set_Equivalent_Type (CW_Type, Empty);
19511 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19513 Set_Class_Wide_Type (CW_Type, CW_Type);
19514 end Make_Class_Wide_Type;
19520 procedure Make_Index
19522 Related_Nod : Node_Id;
19523 Related_Id : Entity_Id := Empty;
19524 Suffix_Index : Pos := 1)
19528 Def_Id : Entity_Id := Empty;
19529 Found : Boolean := False;
19532 -- For a discrete range used in a constrained array definition and
19533 -- defined by a range, an implicit conversion to the predefined type
19534 -- INTEGER is assumed if each bound is either a numeric literal, a named
19535 -- number, or an attribute, and the type of both bounds (prior to the
19536 -- implicit conversion) is the type universal_integer. Otherwise, both
19537 -- bounds must be of the same discrete type, other than universal
19538 -- integer; this type must be determinable independently of the
19539 -- context, but using the fact that the type must be discrete and that
19540 -- both bounds must have the same type.
19542 -- Character literals also have a universal type in the absence of
19543 -- of additional context, and are resolved to Standard_Character.
19545 if Nkind (N) = N_Range then
19547 -- The index is given by a range constraint. The bounds are known
19548 -- to be of a consistent type.
19550 if not Is_Overloaded (N) then
19553 -- For universal bounds, choose the specific predefined type
19555 if T = Universal_Integer then
19556 T := Standard_Integer;
19558 elsif T = Any_Character then
19559 Ambiguous_Character (Low_Bound (N));
19561 T := Standard_Character;
19564 -- The node may be overloaded because some user-defined operators
19565 -- are available, but if a universal interpretation exists it is
19566 -- also the selected one.
19568 elsif Universal_Interpretation (N) = Universal_Integer then
19569 T := Standard_Integer;
19575 Ind : Interp_Index;
19579 Get_First_Interp (N, Ind, It);
19580 while Present (It.Typ) loop
19581 if Is_Discrete_Type (It.Typ) then
19584 and then not Covers (It.Typ, T)
19585 and then not Covers (T, It.Typ)
19587 Error_Msg_N ("ambiguous bounds in discrete range", N);
19595 Get_Next_Interp (Ind, It);
19598 if T = Any_Type then
19599 Error_Msg_N ("discrete type required for range", N);
19600 Set_Etype (N, Any_Type);
19603 elsif T = Universal_Integer then
19604 T := Standard_Integer;
19609 if not Is_Discrete_Type (T) then
19610 Error_Msg_N ("discrete type required for range", N);
19611 Set_Etype (N, Any_Type);
19615 -- If the range bounds are "T'Low .. T'High" where T is a name of
19616 -- a discrete type, then use T as the type of the index.
19618 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19619 and then Attribute_Name (Low_Bound (N)) = Name_First
19620 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19621 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19623 and then Nkind (High_Bound (N)) = N_Attribute_Reference
19624 and then Attribute_Name (High_Bound (N)) = Name_Last
19625 and then Is_Entity_Name (Prefix (High_Bound (N)))
19626 and then Entity (Prefix (High_Bound (N))) = Def_Id
19628 Def_Id := Entity (Prefix (Low_Bound (N)));
19632 Process_Range_Expr_In_Decl (R, T);
19634 elsif Nkind (N) = N_Subtype_Indication then
19636 -- The index is given by a subtype with a range constraint
19638 T := Base_Type (Entity (Subtype_Mark (N)));
19640 if not Is_Discrete_Type (T) then
19641 Error_Msg_N ("discrete type required for range", N);
19642 Set_Etype (N, Any_Type);
19646 R := Range_Expression (Constraint (N));
19649 Process_Range_Expr_In_Decl (R, Entity (Subtype_Mark (N)));
19651 elsif Nkind (N) = N_Attribute_Reference then
19653 -- Catch beginner's error (use of attribute other than 'Range)
19655 if Attribute_Name (N) /= Name_Range then
19656 Error_Msg_N ("expect attribute ''Range", N);
19657 Set_Etype (N, Any_Type);
19661 -- If the node denotes the range of a type mark, that is also the
19662 -- resulting type, and we do not need to create an Itype for it.
19664 if Is_Entity_Name (Prefix (N))
19665 and then Comes_From_Source (N)
19666 and then Is_Discrete_Type (Entity (Prefix (N)))
19668 Def_Id := Entity (Prefix (N));
19671 Analyze_And_Resolve (N);
19675 -- If none of the above, must be a subtype. We convert this to a
19676 -- range attribute reference because in the case of declared first
19677 -- named subtypes, the types in the range reference can be different
19678 -- from the type of the entity. A range attribute normalizes the
19679 -- reference and obtains the correct types for the bounds.
19681 -- This transformation is in the nature of an expansion, is only
19682 -- done if expansion is active. In particular, it is not done on
19683 -- formal generic types, because we need to retain the name of the
19684 -- original index for instantiation purposes.
19687 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19688 Error_Msg_N ("invalid subtype mark in discrete range", N);
19689 Set_Etype (N, Any_Integer);
19693 -- The type mark may be that of an incomplete type. It is only
19694 -- now that we can get the full view, previous analysis does
19695 -- not look specifically for a type mark.
19697 Set_Entity (N, Get_Full_View (Entity (N)));
19698 Set_Etype (N, Entity (N));
19699 Def_Id := Entity (N);
19701 if not Is_Discrete_Type (Def_Id) then
19702 Error_Msg_N ("discrete type required for index", N);
19703 Set_Etype (N, Any_Type);
19708 if Expander_Active then
19710 Make_Attribute_Reference (Sloc (N),
19711 Attribute_Name => Name_Range,
19712 Prefix => Relocate_Node (N)));
19714 -- The original was a subtype mark that does not freeze. This
19715 -- means that the rewritten version must not freeze either.
19717 Set_Must_Not_Freeze (N);
19718 Set_Must_Not_Freeze (Prefix (N));
19719 Analyze_And_Resolve (N);
19723 -- If expander is inactive, type is legal, nothing else to construct
19730 if not Is_Discrete_Type (T) then
19731 Error_Msg_N ("discrete type required for range", N);
19732 Set_Etype (N, Any_Type);
19735 elsif T = Any_Type then
19736 Set_Etype (N, Any_Type);
19740 -- We will now create the appropriate Itype to describe the range, but
19741 -- first a check. If we originally had a subtype, then we just label
19742 -- the range with this subtype. Not only is there no need to construct
19743 -- a new subtype, but it is wrong to do so for two reasons:
19745 -- 1. A legality concern, if we have a subtype, it must not freeze,
19746 -- and the Itype would cause freezing incorrectly
19748 -- 2. An efficiency concern, if we created an Itype, it would not be
19749 -- recognized as the same type for the purposes of eliminating
19750 -- checks in some circumstances.
19752 -- We signal this case by setting the subtype entity in Def_Id
19754 if No (Def_Id) then
19756 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19757 Set_Etype (Def_Id, Base_Type (T));
19759 if Is_Signed_Integer_Type (T) then
19760 Mutate_Ekind (Def_Id, E_Signed_Integer_Subtype);
19762 elsif Is_Modular_Integer_Type (T) then
19763 Mutate_Ekind (Def_Id, E_Modular_Integer_Subtype);
19766 Mutate_Ekind (Def_Id, E_Enumeration_Subtype);
19767 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19768 Set_First_Literal (Def_Id, First_Literal (T));
19771 Set_Size_Info (Def_Id, (T));
19772 Set_RM_Size (Def_Id, RM_Size (T));
19773 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19775 Set_Scalar_Range (Def_Id, R);
19776 Conditional_Delay (Def_Id, T);
19778 -- In the subtype indication case inherit properties of the parent
19780 if Nkind (N) = N_Subtype_Indication then
19782 -- It is enough to inherit predicate flags and not the predicate
19783 -- functions, because predicates on an index type are illegal
19784 -- anyway and the flags are enough to detect them.
19786 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19788 -- If the immediate parent of the new subtype is nonstatic, then
19789 -- the subtype we create is nonstatic as well, even if its bounds
19792 if not Is_OK_Static_Subtype (Entity (Subtype_Mark (N))) then
19793 Set_Is_Non_Static_Subtype (Def_Id);
19798 -- Final step is to label the index with this constructed type
19800 Set_Etype (N, Def_Id);
19803 ------------------------------
19804 -- Modular_Type_Declaration --
19805 ------------------------------
19807 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19808 Mod_Expr : constant Node_Id := Expression (Def);
19811 procedure Set_Modular_Size (Bits : Int);
19812 -- Sets RM_Size to Bits, and Esize to normal word size above this
19814 ----------------------
19815 -- Set_Modular_Size --
19816 ----------------------
19818 procedure Set_Modular_Size (Bits : Int) is
19822 Set_RM_Size (T, UI_From_Int (Bits));
19824 if Bits < System_Max_Binary_Modulus_Power then
19827 while Siz < 128 loop
19828 exit when Bits <= Siz;
19832 Set_Esize (T, UI_From_Int (Siz));
19835 Set_Esize (T, UI_From_Int (System_Max_Binary_Modulus_Power));
19838 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19839 Set_Is_Known_Valid (T);
19841 end Set_Modular_Size;
19843 -- Start of processing for Modular_Type_Declaration
19846 -- If the mod expression is (exactly) 2 * literal, where literal is
19847 -- 128 or less, then almost certainly the * was meant to be **. Warn.
19849 if Warn_On_Suspicious_Modulus_Value
19850 and then Nkind (Mod_Expr) = N_Op_Multiply
19851 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19852 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19853 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19854 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_128
19857 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19860 -- Proceed with analysis of mod expression
19862 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19864 if Ekind (T) in Incomplete_Or_Private_Kind then
19865 Reinit_Field_To_Zero (T, F_Stored_Constraint);
19869 Mutate_Ekind (T, E_Modular_Integer_Type);
19870 Reinit_Alignment (T);
19871 Set_Is_Constrained (T);
19873 if not Is_OK_Static_Expression (Mod_Expr) then
19874 Flag_Non_Static_Expr
19875 ("non-static expression used for modular type bound!", Mod_Expr);
19876 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19878 M_Val := Expr_Value (Mod_Expr);
19882 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19883 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19886 if M_Val > 2 ** Standard_Long_Integer_Size then
19887 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19890 Set_Modulus (T, M_Val);
19892 -- Create bounds for the modular type based on the modulus given in
19893 -- the type declaration and then analyze and resolve those bounds.
19895 Set_Scalar_Range (T,
19896 Make_Range (Sloc (Mod_Expr),
19897 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19898 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19900 -- Properly analyze the literals for the range. We do this manually
19901 -- because we can't go calling Resolve, since we are resolving these
19902 -- bounds with the type, and this type is certainly not complete yet.
19904 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19905 Set_Etype (High_Bound (Scalar_Range (T)), T);
19906 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19907 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19909 -- Loop through powers of two to find number of bits required
19911 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19915 if M_Val = 2 ** Bits then
19916 Set_Modular_Size (Bits);
19921 elsif M_Val < 2 ** Bits then
19922 Set_Non_Binary_Modulus (T);
19924 if Bits > System_Max_Nonbinary_Modulus_Power then
19925 Error_Msg_Uint_1 :=
19926 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19928 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19929 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19933 -- In the nonbinary case, set size as per RM 13.3(55)
19935 Set_Modular_Size (Bits);
19942 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19943 -- so we just signal an error and set the maximum size.
19945 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19946 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19948 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19949 Reinit_Alignment (T);
19951 end Modular_Type_Declaration;
19953 --------------------------
19954 -- New_Concatenation_Op --
19955 --------------------------
19957 procedure New_Concatenation_Op (Typ : Entity_Id) is
19958 Loc : constant Source_Ptr := Sloc (Typ);
19961 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19962 -- Create abbreviated declaration for the formal of a predefined
19963 -- Operator 'Op' of type 'Typ'
19965 --------------------
19966 -- Make_Op_Formal --
19967 --------------------
19969 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19970 Formal : Entity_Id;
19972 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19973 Set_Etype (Formal, Typ);
19974 Set_Mechanism (Formal, Default_Mechanism);
19976 end Make_Op_Formal;
19978 -- Start of processing for New_Concatenation_Op
19981 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19983 Mutate_Ekind (Op, E_Operator);
19984 Set_Scope (Op, Current_Scope);
19985 Set_Etype (Op, Typ);
19986 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19987 Set_Is_Immediately_Visible (Op);
19988 Set_Is_Intrinsic_Subprogram (Op);
19989 Set_Has_Completion (Op);
19990 Append_Entity (Op, Current_Scope);
19992 Set_Name_Entity_Id (Name_Op_Concat, Op);
19994 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19995 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19996 end New_Concatenation_Op;
19998 -------------------------
19999 -- OK_For_Limited_Init --
20000 -------------------------
20002 -- ???Check all calls of this, and compare the conditions under which it's
20005 function OK_For_Limited_Init
20007 Exp : Node_Id) return Boolean
20010 return Is_CPP_Constructor_Call (Exp)
20011 or else (Ada_Version >= Ada_2005
20012 and then not Debug_Flag_Dot_L
20013 and then OK_For_Limited_Init_In_05 (Typ, Exp));
20014 end OK_For_Limited_Init;
20016 -------------------------------
20017 -- OK_For_Limited_Init_In_05 --
20018 -------------------------------
20020 function OK_For_Limited_Init_In_05
20022 Exp : Node_Id) return Boolean
20025 -- An object of a limited interface type can be initialized with any
20026 -- expression of a nonlimited descendant type. However this does not
20027 -- apply if this is a view conversion of some other expression. This
20028 -- is checked below.
20030 if Is_Class_Wide_Type (Typ)
20031 and then Is_Limited_Interface (Typ)
20032 and then not Is_Limited_Type (Etype (Exp))
20033 and then Nkind (Exp) /= N_Type_Conversion
20038 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
20039 -- case of limited aggregates (including extension aggregates), and
20040 -- function calls. The function call may have been given in prefixed
20041 -- notation, in which case the original node is an indexed component.
20042 -- If the function is parameterless, the original node was an explicit
20043 -- dereference. The function may also be parameterless, in which case
20044 -- the source node is just an identifier.
20046 -- A branch of a conditional expression may have been removed if the
20047 -- condition is statically known. This happens during expansion, and
20048 -- thus will not happen if previous errors were encountered. The check
20049 -- will have been performed on the chosen branch, which replaces the
20050 -- original conditional expression.
20056 case Nkind (Original_Node (Exp)) is
20058 | N_Extension_Aggregate
20064 when N_Identifier =>
20065 return Present (Entity (Original_Node (Exp)))
20066 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
20068 when N_Qualified_Expression =>
20070 OK_For_Limited_Init_In_05
20071 (Typ, Expression (Original_Node (Exp)));
20073 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
20074 -- with a function call, the expander has rewritten the call into an
20075 -- N_Type_Conversion node to force displacement of the pointer to
20076 -- reference the component containing the secondary dispatch table.
20077 -- Otherwise a type conversion is not a legal context.
20078 -- A return statement for a build-in-place function returning a
20079 -- synchronized type also introduces an unchecked conversion.
20081 when N_Type_Conversion
20082 | N_Unchecked_Type_Conversion
20084 return not Comes_From_Source (Exp)
20086 -- If the conversion has been rewritten, check Original_Node
20088 ((Original_Node (Exp) /= Exp
20090 OK_For_Limited_Init_In_05 (Typ, Original_Node (Exp)))
20092 -- Otherwise, check the expression of the compiler-generated
20093 -- conversion (which is a conversion that we want to ignore
20094 -- for purposes of the limited-initialization restrictions).
20097 (Original_Node (Exp) = Exp
20099 OK_For_Limited_Init_In_05 (Typ, Expression (Exp))));
20101 when N_Explicit_Dereference
20102 | N_Indexed_Component
20103 | N_Selected_Component
20105 return Nkind (Exp) = N_Function_Call;
20107 -- A use of 'Input is a function call, hence allowed. Normally the
20108 -- attribute will be changed to a call, but the attribute by itself
20109 -- can occur with -gnatc.
20111 when N_Attribute_Reference =>
20112 return Attribute_Name (Original_Node (Exp)) = Name_Input;
20114 -- "return raise ..." is OK
20116 when N_Raise_Expression =>
20119 -- For a case expression, all dependent expressions must be legal
20121 when N_Case_Expression =>
20126 Alt := First (Alternatives (Original_Node (Exp)));
20127 while Present (Alt) loop
20128 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
20138 -- For an if expression, all dependent expressions must be legal
20140 when N_If_Expression =>
20142 Then_Expr : constant Node_Id :=
20143 Next (First (Expressions (Original_Node (Exp))));
20144 Else_Expr : constant Node_Id := Next (Then_Expr);
20146 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
20148 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
20154 end OK_For_Limited_Init_In_05;
20156 -------------------------------------------
20157 -- Ordinary_Fixed_Point_Type_Declaration --
20158 -------------------------------------------
20160 procedure Ordinary_Fixed_Point_Type_Declaration
20164 Loc : constant Source_Ptr := Sloc (Def);
20165 Delta_Expr : constant Node_Id := Delta_Expression (Def);
20166 RRS : constant Node_Id := Real_Range_Specification (Def);
20167 Implicit_Base : Entity_Id;
20174 Check_Restriction (No_Fixed_Point, Def);
20176 -- Create implicit base type
20179 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
20180 Set_Etype (Implicit_Base, Implicit_Base);
20182 -- Analyze and process delta expression
20184 Analyze_And_Resolve (Delta_Expr, Any_Real);
20186 Check_Delta_Expression (Delta_Expr);
20187 Delta_Val := Expr_Value_R (Delta_Expr);
20189 Set_Delta_Value (Implicit_Base, Delta_Val);
20191 -- Compute default small from given delta, which is the largest power
20192 -- of two that does not exceed the given delta value.
20202 if Delta_Val < Ureal_1 then
20203 while Delta_Val < Tmp loop
20204 Tmp := Tmp / Ureal_2;
20205 Scale := Scale + 1;
20210 Tmp := Tmp * Ureal_2;
20211 exit when Tmp > Delta_Val;
20212 Scale := Scale - 1;
20216 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
20219 Set_Small_Value (Implicit_Base, Small_Val);
20221 -- If no range was given, set a dummy range
20223 if RRS <= Empty_Or_Error then
20224 Low_Val := -Small_Val;
20225 High_Val := Small_Val;
20227 -- Otherwise analyze and process given range
20231 Low : constant Node_Id := Low_Bound (RRS);
20232 High : constant Node_Id := High_Bound (RRS);
20235 Analyze_And_Resolve (Low, Any_Real);
20236 Analyze_And_Resolve (High, Any_Real);
20237 Check_Real_Bound (Low);
20238 Check_Real_Bound (High);
20240 -- Obtain and set the range
20242 Low_Val := Expr_Value_R (Low);
20243 High_Val := Expr_Value_R (High);
20245 if Low_Val > High_Val then
20246 Error_Msg_NE ("??fixed point type& has null range", Def, T);
20251 -- The range for both the implicit base and the declared first subtype
20252 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
20253 -- set a temporary range in place. Note that the bounds of the base
20254 -- type will be widened to be symmetrical and to fill the available
20255 -- bits when the type is frozen.
20257 -- We could do this with all discrete types, and probably should, but
20258 -- we absolutely have to do it for fixed-point, since the end-points
20259 -- of the range and the size are determined by the small value, which
20260 -- could be reset before the freeze point.
20262 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
20263 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
20265 -- Complete definition of first subtype. The inheritance of the rep item
20266 -- chain ensures that SPARK-related pragmas are not clobbered when the
20267 -- ordinary fixed point type acts as a full view of a private type.
20269 Mutate_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
20270 Set_Etype (T, Implicit_Base);
20271 Reinit_Size_Align (T);
20272 Inherit_Rep_Item_Chain (T, Implicit_Base);
20273 Set_Small_Value (T, Small_Val);
20274 Set_Delta_Value (T, Delta_Val);
20275 Set_Is_Constrained (T);
20276 end Ordinary_Fixed_Point_Type_Declaration;
20278 ----------------------------------
20279 -- Preanalyze_Assert_Expression --
20280 ----------------------------------
20282 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
20284 In_Assertion_Expr := In_Assertion_Expr + 1;
20285 Preanalyze_Spec_Expression (N, T);
20286 In_Assertion_Expr := In_Assertion_Expr - 1;
20287 end Preanalyze_Assert_Expression;
20289 -----------------------------------
20290 -- Preanalyze_Default_Expression --
20291 -----------------------------------
20293 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
20294 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
20295 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20298 In_Default_Expr := True;
20299 In_Spec_Expression := True;
20301 Preanalyze_With_Freezing_And_Resolve (N, T);
20303 In_Default_Expr := Save_In_Default_Expr;
20304 In_Spec_Expression := Save_In_Spec_Expression;
20305 end Preanalyze_Default_Expression;
20307 --------------------------------
20308 -- Preanalyze_Spec_Expression --
20309 --------------------------------
20311 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
20312 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
20314 In_Spec_Expression := True;
20315 Preanalyze_And_Resolve (N, T);
20316 In_Spec_Expression := Save_In_Spec_Expression;
20317 end Preanalyze_Spec_Expression;
20319 ----------------------------------------
20320 -- Prepare_Private_Subtype_Completion --
20321 ----------------------------------------
20323 procedure Prepare_Private_Subtype_Completion
20325 Related_Nod : Node_Id)
20327 Id_B : constant Entity_Id := Base_Type (Id);
20328 Full_B : constant Entity_Id := Full_View (Id_B);
20332 if Present (Full_B) then
20334 -- The Base_Type is already completed, we can complete the subtype
20335 -- now. We have to create a new entity with the same name, Thus we
20336 -- can't use Create_Itype.
20338 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
20339 Set_Is_Itype (Full);
20340 Set_Associated_Node_For_Itype (Full, Related_Nod);
20341 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
20342 Set_Full_View (Id, Full);
20345 -- The parent subtype may be private, but the base might not, in some
20346 -- nested instances. In that case, the subtype does not need to be
20347 -- exchanged. It would still be nice to make private subtypes and their
20348 -- bases consistent at all times ???
20350 if Is_Private_Type (Id_B) then
20351 Append_Elmt (Id, Private_Dependents (Id_B));
20353 end Prepare_Private_Subtype_Completion;
20355 ---------------------------
20356 -- Process_Discriminants --
20357 ---------------------------
20359 procedure Process_Discriminants
20361 Prev : Entity_Id := Empty)
20363 Elist : constant Elist_Id := New_Elmt_List;
20366 Discr_Number : Uint;
20367 Discr_Type : Entity_Id;
20368 Default_Present : Boolean := False;
20369 Default_Not_Present : Boolean := False;
20372 -- A composite type other than an array type can have discriminants.
20373 -- On entry, the current scope is the composite type.
20375 -- The discriminants are initially entered into the scope of the type
20376 -- via Enter_Name with the default Ekind of E_Void to prevent premature
20377 -- use, as explained at the end of this procedure.
20379 Discr := First (Discriminant_Specifications (N));
20380 while Present (Discr) loop
20381 Enter_Name (Defining_Identifier (Discr));
20383 -- For navigation purposes we add a reference to the discriminant
20384 -- in the entity for the type. If the current declaration is a
20385 -- completion, place references on the partial view. Otherwise the
20386 -- type is the current scope.
20388 if Present (Prev) then
20390 -- The references go on the partial view, if present. If the
20391 -- partial view has discriminants, the references have been
20392 -- generated already.
20394 if not Has_Discriminants (Prev) then
20395 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
20399 (Current_Scope, Defining_Identifier (Discr), 'd');
20402 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
20403 Check_Anonymous_Access_Component
20405 Typ => Defining_Identifier (N),
20408 Access_Def => Discriminant_Type (Discr));
20410 -- if Check_Anonymous_Access_Component replaced Discr then
20411 -- its Original_Node points to the old Discr and the access type
20412 -- for Discr_Type has already been created.
20414 if Original_Node (Discr) /= Discr then
20415 Discr_Type := Etype (Discriminant_Type (Discr));
20418 Access_Definition (Discr, Discriminant_Type (Discr));
20420 -- Ada 2005 (AI-254)
20422 if Present (Access_To_Subprogram_Definition
20423 (Discriminant_Type (Discr)))
20424 and then Protected_Present (Access_To_Subprogram_Definition
20425 (Discriminant_Type (Discr)))
20428 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
20432 Find_Type (Discriminant_Type (Discr));
20433 Discr_Type := Etype (Discriminant_Type (Discr));
20435 if Error_Posted (Discriminant_Type (Discr)) then
20436 Discr_Type := Any_Type;
20440 -- Handling of discriminants that are access types
20442 if Is_Access_Type (Discr_Type) then
20444 -- Ada 2005 (AI-230): Access discriminant allowed in non-
20445 -- limited record types
20447 if Ada_Version < Ada_2005 then
20448 Check_Access_Discriminant_Requires_Limited
20449 (Discr, Discriminant_Type (Discr));
20452 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
20454 ("(Ada 83) access discriminant not allowed", Discr);
20457 -- If not access type, must be a discrete type
20459 elsif not Is_Discrete_Type (Discr_Type) then
20461 ("discriminants must have a discrete or access type",
20462 Discriminant_Type (Discr));
20465 Set_Etype (Defining_Identifier (Discr), Discr_Type);
20467 -- If a discriminant specification includes the assignment compound
20468 -- delimiter followed by an expression, the expression is the default
20469 -- expression of the discriminant; the default expression must be of
20470 -- the type of the discriminant. (RM 3.7.1) Since this expression is
20471 -- a default expression, we do the special preanalysis, since this
20472 -- expression does not freeze (see section "Handling of Default and
20473 -- Per-Object Expressions" in spec of package Sem).
20475 if Present (Expression (Discr)) then
20476 Preanalyze_Default_Expression (Expression (Discr), Discr_Type);
20480 if Nkind (N) = N_Formal_Type_Declaration then
20482 ("discriminant defaults not allowed for formal type",
20483 Expression (Discr));
20485 -- Flag an error for a tagged type with defaulted discriminants,
20486 -- excluding limited tagged types when compiling for Ada 2012
20487 -- (see AI05-0214).
20489 elsif Is_Tagged_Type (Current_Scope)
20490 and then (not Is_Limited_Type (Current_Scope)
20491 or else Ada_Version < Ada_2012)
20492 and then Comes_From_Source (N)
20494 -- Note: see similar test in Check_Or_Process_Discriminants, to
20495 -- handle the (illegal) case of the completion of an untagged
20496 -- view with discriminants with defaults by a tagged full view.
20497 -- We skip the check if Discr does not come from source, to
20498 -- account for the case of an untagged derived type providing
20499 -- defaults for a renamed discriminant from a private untagged
20500 -- ancestor with a tagged full view (ACATS B460006).
20502 if Ada_Version >= Ada_2012 then
20504 ("discriminants of nonlimited tagged type cannot have"
20506 Expression (Discr));
20509 ("discriminants of tagged type cannot have defaults",
20510 Expression (Discr));
20514 Default_Present := True;
20515 Append_Elmt (Expression (Discr), Elist);
20517 -- Tag the defining identifiers for the discriminants with
20518 -- their corresponding default expressions from the tree.
20520 Set_Discriminant_Default_Value
20521 (Defining_Identifier (Discr), Expression (Discr));
20524 -- In gnatc or GNATprove mode, make sure set Do_Range_Check flag
20525 -- gets set unless we can be sure that no range check is required.
20527 if not Expander_Active
20530 (Expression (Discr), Discr_Type, Assume_Valid => True)
20532 Set_Do_Range_Check (Expression (Discr));
20535 -- No default discriminant value given
20538 Default_Not_Present := True;
20541 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20542 -- Discr_Type but with the null-exclusion attribute
20544 if Ada_Version >= Ada_2005 then
20546 -- Ada 2005 (AI-231): Static checks
20548 if Can_Never_Be_Null (Discr_Type) then
20549 Null_Exclusion_Static_Checks (Discr);
20551 elsif Is_Access_Type (Discr_Type)
20552 and then Null_Exclusion_Present (Discr)
20554 -- No need to check itypes because in their case this check
20555 -- was done at their point of creation
20557 and then not Is_Itype (Discr_Type)
20559 if Can_Never_Be_Null (Discr_Type) then
20561 ("`NOT NULL` not allowed (& already excludes null)",
20566 Set_Etype (Defining_Identifier (Discr),
20567 Create_Null_Excluding_Itype
20569 Related_Nod => Discr));
20571 -- Check for improper null exclusion if the type is otherwise
20572 -- legal for a discriminant.
20574 elsif Null_Exclusion_Present (Discr)
20575 and then Is_Discrete_Type (Discr_Type)
20578 ("null exclusion can only apply to an access type", Discr);
20581 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20582 -- can't have defaults. Synchronized types, or types that are
20583 -- explicitly limited are fine, but special tests apply to derived
20584 -- types in generics: in a generic body we have to assume the
20585 -- worst, and therefore defaults are not allowed if the parent is
20586 -- a generic formal private type (see ACATS B370001).
20588 if Is_Access_Type (Discr_Type) and then Default_Present then
20589 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20590 or else Is_Limited_Record (Current_Scope)
20591 or else Is_Concurrent_Type (Current_Scope)
20592 or else Is_Concurrent_Record_Type (Current_Scope)
20593 or else Ekind (Current_Scope) = E_Limited_Private_Type
20595 if not Is_Derived_Type (Current_Scope)
20596 or else not Is_Generic_Type (Etype (Current_Scope))
20597 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20598 or else Limited_Present
20599 (Type_Definition (Parent (Current_Scope)))
20605 ("access discriminants of nonlimited types cannot "
20606 & "have defaults", Expression (Discr));
20609 elsif Present (Expression (Discr)) then
20611 ("(Ada 2005) access discriminants of nonlimited types "
20612 & "cannot have defaults", Expression (Discr));
20617 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(4)).
20618 -- This check is relevant only when SPARK_Mode is on as it is not a
20619 -- standard Ada legality rule. The only way for a discriminant to be
20620 -- effectively volatile is to have an effectively volatile type, so
20621 -- we check this directly, because the Ekind of Discr might not be
20622 -- set yet (to help preventing cascaded errors on derived types).
20625 and then Is_Effectively_Volatile (Discr_Type)
20627 Error_Msg_N ("discriminant cannot be volatile", Discr);
20633 -- An element list consisting of the default expressions of the
20634 -- discriminants is constructed in the above loop and used to set
20635 -- the Discriminant_Constraint attribute for the type. If an object
20636 -- is declared of this (record or task) type without any explicit
20637 -- discriminant constraint given, this element list will form the
20638 -- actual parameters for the corresponding initialization procedure
20641 Set_Discriminant_Constraint (Current_Scope, Elist);
20642 Set_Stored_Constraint (Current_Scope, No_Elist);
20644 -- Default expressions must be provided either for all or for none
20645 -- of the discriminants of a discriminant part. (RM 3.7.1)
20647 if Default_Present and then Default_Not_Present then
20649 ("incomplete specification of defaults for discriminants", N);
20652 -- The use of the name of a discriminant is not allowed in default
20653 -- expressions of a discriminant part if the specification of the
20654 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20656 -- To detect this, the discriminant names are entered initially with an
20657 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20658 -- attempt to use a void entity (for example in an expression that is
20659 -- type-checked) produces the error message: premature usage. Now after
20660 -- completing the semantic analysis of the discriminant part, we can set
20661 -- the Ekind of all the discriminants appropriately.
20663 Discr := First (Discriminant_Specifications (N));
20664 Discr_Number := Uint_1;
20665 while Present (Discr) loop
20666 Id := Defining_Identifier (Discr);
20668 if Ekind (Id) = E_In_Parameter then
20669 Reinit_Field_To_Zero (Id, F_Discriminal_Link);
20672 Mutate_Ekind (Id, E_Discriminant);
20673 Reinit_Component_Location (Id);
20675 Set_Discriminant_Number (Id, Discr_Number);
20677 -- Make sure this is always set, even in illegal programs
20679 Set_Corresponding_Discriminant (Id, Empty);
20681 -- Initialize the Original_Record_Component to the entity itself.
20682 -- Inherit_Components will propagate the right value to
20683 -- discriminants in derived record types.
20685 Set_Original_Record_Component (Id, Id);
20687 -- Create the discriminal for the discriminant
20689 Build_Discriminal (Id);
20692 Discr_Number := Discr_Number + 1;
20695 Set_Has_Discriminants (Current_Scope);
20696 end Process_Discriminants;
20698 -----------------------
20699 -- Process_Full_View --
20700 -----------------------
20702 -- WARNING: This routine manages Ghost regions. Return statements must be
20703 -- replaced by gotos which jump to the end of the routine and restore the
20706 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20707 procedure Collect_Implemented_Interfaces
20709 Ifaces : Elist_Id);
20710 -- Ada 2005: Gather all the interfaces that Typ directly or
20711 -- inherently implements. Duplicate entries are not added to
20712 -- the list Ifaces.
20714 ------------------------------------
20715 -- Collect_Implemented_Interfaces --
20716 ------------------------------------
20718 procedure Collect_Implemented_Interfaces
20723 Iface_Elmt : Elmt_Id;
20726 -- Abstract interfaces are only associated with tagged record types
20728 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20732 -- Recursively climb to the ancestors
20734 if Etype (Typ) /= Typ
20736 -- Protect the frontend against wrong cyclic declarations like:
20738 -- type B is new A with private;
20739 -- type C is new A with private;
20741 -- type B is new C with null record;
20742 -- type C is new B with null record;
20744 and then Etype (Typ) /= Priv_T
20745 and then Etype (Typ) /= Full_T
20747 -- Keep separate the management of private type declarations
20749 if Ekind (Typ) = E_Record_Type_With_Private then
20751 -- Handle the following illegal usage:
20752 -- type Private_Type is tagged private;
20754 -- type Private_Type is new Type_Implementing_Iface;
20756 if Present (Full_View (Typ))
20757 and then Etype (Typ) /= Full_View (Typ)
20759 if Is_Interface (Etype (Typ)) then
20760 Append_Unique_Elmt (Etype (Typ), Ifaces);
20763 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20766 -- Non-private types
20769 if Is_Interface (Etype (Typ)) then
20770 Append_Unique_Elmt (Etype (Typ), Ifaces);
20773 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20777 -- Handle entities in the list of abstract interfaces
20779 if Present (Interfaces (Typ)) then
20780 Iface_Elmt := First_Elmt (Interfaces (Typ));
20781 while Present (Iface_Elmt) loop
20782 Iface := Node (Iface_Elmt);
20784 pragma Assert (Is_Interface (Iface));
20786 if not Contain_Interface (Iface, Ifaces) then
20787 Append_Elmt (Iface, Ifaces);
20788 Collect_Implemented_Interfaces (Iface, Ifaces);
20791 Next_Elmt (Iface_Elmt);
20794 end Collect_Implemented_Interfaces;
20798 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20799 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
20800 -- Save the Ghost-related attributes to restore on exit
20802 Full_Indic : Node_Id;
20803 Full_Parent : Entity_Id;
20804 Priv_Parent : Entity_Id;
20806 -- Start of processing for Process_Full_View
20809 Mark_And_Set_Ghost_Completion (N, Priv_T);
20811 -- First some sanity checks that must be done after semantic
20812 -- decoration of the full view and thus cannot be placed with other
20813 -- similar checks in Find_Type_Name
20815 if not Is_Limited_Type (Priv_T)
20816 and then (Is_Limited_Type (Full_T)
20817 or else Is_Limited_Composite (Full_T))
20819 if In_Instance then
20823 ("completion of nonlimited type cannot be limited", Full_T);
20824 Explain_Limited_Type (Full_T, Full_T);
20827 elsif Is_Abstract_Type (Full_T)
20828 and then not Is_Abstract_Type (Priv_T)
20831 ("completion of nonabstract type cannot be abstract", Full_T);
20833 elsif Is_Tagged_Type (Priv_T)
20834 and then Is_Limited_Type (Priv_T)
20835 and then not Is_Limited_Type (Full_T)
20837 -- If pragma CPP_Class was applied to the private declaration
20838 -- propagate the limitedness to the full-view
20840 if Is_CPP_Class (Priv_T) then
20841 Set_Is_Limited_Record (Full_T);
20843 -- GNAT allow its own definition of Limited_Controlled to disobey
20844 -- this rule in order in ease the implementation. This test is safe
20845 -- because Root_Controlled is defined in a child of System that
20846 -- normal programs are not supposed to use.
20848 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20849 Set_Is_Limited_Composite (Full_T);
20852 ("completion of limited tagged type must be limited", Full_T);
20855 elsif Is_Generic_Type (Priv_T) then
20856 Error_Msg_N ("generic type cannot have a completion", Full_T);
20859 -- Check that ancestor interfaces of private and full views are
20860 -- consistent. We omit this check for synchronized types because
20861 -- they are performed on the corresponding record type when frozen.
20863 if Ada_Version >= Ada_2005
20864 and then Is_Tagged_Type (Priv_T)
20865 and then Is_Tagged_Type (Full_T)
20866 and then not Is_Concurrent_Type (Full_T)
20870 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20871 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20874 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20875 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20877 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20878 -- an interface type if and only if the full type is descendant
20879 -- of the interface type (AARM 7.3 (7.3/2)).
20881 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20883 if Present (Iface) then
20885 ("interface in partial view& not implemented by full type "
20886 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20889 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20891 if Present (Iface) then
20893 ("interface & not implemented by partial view "
20894 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20899 if Is_Tagged_Type (Priv_T)
20900 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20901 and then Is_Derived_Type (Full_T)
20903 Priv_Parent := Etype (Priv_T);
20905 -- The full view of a private extension may have been transformed
20906 -- into an unconstrained derived type declaration and a subtype
20907 -- declaration (see build_derived_record_type for details).
20909 if Nkind (N) = N_Subtype_Declaration then
20910 Full_Indic := Subtype_Indication (N);
20911 Full_Parent := Etype (Base_Type (Full_T));
20913 Full_Indic := Subtype_Indication (Type_Definition (N));
20914 Full_Parent := Etype (Full_T);
20917 -- Check that the parent type of the full type is a descendant of
20918 -- the ancestor subtype given in the private extension. If either
20919 -- entity has an Etype equal to Any_Type then we had some previous
20920 -- error situation [7.3(8)].
20922 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20925 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20926 -- any order. Therefore we don't have to check that its parent must
20927 -- be a descendant of the parent of the private type declaration.
20929 elsif Is_Interface (Priv_Parent)
20930 and then Is_Interface (Full_Parent)
20934 -- Ada 2005 (AI-251): If the parent of the private type declaration
20935 -- is an interface there is no need to check that it is an ancestor
20936 -- of the associated full type declaration. The required tests for
20937 -- this case are performed by Build_Derived_Record_Type.
20939 elsif not Is_Interface (Base_Type (Priv_Parent))
20940 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20943 ("parent of full type must descend from parent of private "
20944 & "extension", Full_Indic);
20946 -- First check a formal restriction, and then proceed with checking
20947 -- Ada rules. Since the formal restriction is not a serious error, we
20948 -- don't prevent further error detection for this check, hence the
20952 -- Check the rules of 7.3(10): if the private extension inherits
20953 -- known discriminants, then the full type must also inherit those
20954 -- discriminants from the same (ancestor) type, and the parent
20955 -- subtype of the full type must be constrained if and only if
20956 -- the ancestor subtype of the private extension is constrained.
20958 if No (Discriminant_Specifications (Parent (Priv_T)))
20959 and then not Has_Unknown_Discriminants (Priv_T)
20960 and then Has_Discriminants (Base_Type (Priv_Parent))
20963 Priv_Indic : constant Node_Id :=
20964 Subtype_Indication (Parent (Priv_T));
20966 Priv_Constr : constant Boolean :=
20967 Is_Constrained (Priv_Parent)
20969 Nkind (Priv_Indic) = N_Subtype_Indication
20971 Is_Constrained (Entity (Priv_Indic));
20973 Full_Constr : constant Boolean :=
20974 Is_Constrained (Full_Parent)
20976 Nkind (Full_Indic) = N_Subtype_Indication
20978 Is_Constrained (Entity (Full_Indic));
20980 Priv_Discr : Entity_Id;
20981 Full_Discr : Entity_Id;
20984 Priv_Discr := First_Discriminant (Priv_Parent);
20985 Full_Discr := First_Discriminant (Full_Parent);
20986 while Present (Priv_Discr) and then Present (Full_Discr) loop
20987 if Original_Record_Component (Priv_Discr) =
20988 Original_Record_Component (Full_Discr)
20990 Corresponding_Discriminant (Priv_Discr) =
20991 Corresponding_Discriminant (Full_Discr)
20998 Next_Discriminant (Priv_Discr);
20999 Next_Discriminant (Full_Discr);
21002 if Present (Priv_Discr) or else Present (Full_Discr) then
21004 ("full view must inherit discriminants of the parent "
21005 & "type used in the private extension", Full_Indic);
21007 elsif Priv_Constr and then not Full_Constr then
21009 ("parent subtype of full type must be constrained",
21012 elsif Full_Constr and then not Priv_Constr then
21014 ("parent subtype of full type must be unconstrained",
21019 -- Check the rules of 7.3(12): if a partial view has neither
21020 -- known or unknown discriminants, then the full type
21021 -- declaration shall define a definite subtype.
21023 elsif not Has_Unknown_Discriminants (Priv_T)
21024 and then not Has_Discriminants (Priv_T)
21025 and then not Is_Constrained (Full_T)
21028 ("full view must define a constrained type if partial view "
21029 & "has no discriminants", Full_T);
21032 -- Do we implement the following properly???
21033 -- If the ancestor subtype of a private extension has constrained
21034 -- discriminants, then the parent subtype of the full view shall
21035 -- impose a statically matching constraint on those discriminants
21040 -- For untagged types, verify that a type without discriminants is
21041 -- not completed with an unconstrained type. A separate error message
21042 -- is produced if the full type has defaulted discriminants.
21044 if Is_Definite_Subtype (Priv_T)
21045 and then not Is_Definite_Subtype (Full_T)
21047 Error_Msg_Sloc := Sloc (Parent (Priv_T));
21049 ("full view of& not compatible with declaration#",
21052 if not Is_Tagged_Type (Full_T) then
21054 ("\one is constrained, the other unconstrained", Full_T);
21059 -- AI-419: verify that the use of "limited" is consistent
21062 Orig_Decl : constant Node_Id := Original_Node (N);
21065 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21066 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
21068 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
21070 if not Limited_Present (Parent (Priv_T))
21071 and then not Synchronized_Present (Parent (Priv_T))
21072 and then Limited_Present (Type_Definition (Orig_Decl))
21075 ("full view of non-limited extension cannot be limited", N);
21077 -- Conversely, if the partial view carries the limited keyword,
21078 -- the full view must as well, even if it may be redundant.
21080 elsif Limited_Present (Parent (Priv_T))
21081 and then not Limited_Present (Type_Definition (Orig_Decl))
21084 ("full view of limited extension must be explicitly limited",
21090 -- Ada 2005 (AI-443): A synchronized private extension must be
21091 -- completed by a task or protected type.
21093 if Ada_Version >= Ada_2005
21094 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
21095 and then Synchronized_Present (Parent (Priv_T))
21096 and then not Is_Concurrent_Type (Full_T)
21098 Error_Msg_N ("full view of synchronized extension must " &
21099 "be synchronized type", N);
21102 -- Ada 2005 AI-363: if the full view has discriminants with
21103 -- defaults, it is illegal to declare constrained access subtypes
21104 -- whose designated type is the current type. This allows objects
21105 -- of the type that are declared in the heap to be unconstrained.
21107 if not Has_Unknown_Discriminants (Priv_T)
21108 and then not Has_Discriminants (Priv_T)
21109 and then Has_Defaulted_Discriminants (Full_T)
21111 Set_Has_Constrained_Partial_View (Base_Type (Full_T));
21112 Set_Has_Constrained_Partial_View (Priv_T);
21115 -- Create a full declaration for all its subtypes recorded in
21116 -- Private_Dependents and swap them similarly to the base type. These
21117 -- are subtypes that have been define before the full declaration of
21118 -- the private type. We also swap the entry in Private_Dependents list
21119 -- so we can properly restore the private view on exit from the scope.
21122 Priv_Elmt : Elmt_Id;
21123 Priv_Scop : Entity_Id;
21128 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
21129 while Present (Priv_Elmt) loop
21130 Priv := Node (Priv_Elmt);
21131 Priv_Scop := Scope (Priv);
21133 if Ekind (Priv) in E_Private_Subtype
21134 | E_Limited_Private_Subtype
21135 | E_Record_Subtype_With_Private
21137 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
21138 Set_Is_Itype (Full);
21139 Set_Parent (Full, Parent (Priv));
21140 Set_Associated_Node_For_Itype (Full, N);
21142 -- Now we need to complete the private subtype, but since the
21143 -- base type has already been swapped, we must also swap the
21144 -- subtypes (and thus, reverse the arguments in the call to
21145 -- Complete_Private_Subtype). Also note that we may need to
21146 -- re-establish the scope of the private subtype.
21148 Copy_And_Swap (Priv, Full);
21150 if not In_Open_Scopes (Priv_Scop) then
21151 Push_Scope (Priv_Scop);
21154 -- Reset Priv_Scop to Empty to indicate no scope was pushed
21156 Priv_Scop := Empty;
21159 Complete_Private_Subtype (Full, Priv, Full_T, N);
21160 Set_Full_View (Full, Priv);
21162 if Present (Priv_Scop) then
21166 Replace_Elmt (Priv_Elmt, Full);
21169 Next_Elmt (Priv_Elmt);
21174 Disp_Typ : Entity_Id;
21175 Full_List : Elist_Id;
21177 Prim_Elmt : Elmt_Id;
21178 Priv_List : Elist_Id;
21182 L : Elist_Id) return Boolean;
21183 -- Determine whether list L contains element E
21191 L : Elist_Id) return Boolean
21193 List_Elmt : Elmt_Id;
21196 List_Elmt := First_Elmt (L);
21197 while Present (List_Elmt) loop
21198 if Node (List_Elmt) = E then
21202 Next_Elmt (List_Elmt);
21208 -- Start of processing
21211 -- If the private view was tagged, copy the new primitive operations
21212 -- from the private view to the full view.
21214 if Is_Tagged_Type (Full_T) then
21215 if Is_Tagged_Type (Priv_T) then
21216 Priv_List := Primitive_Operations (Priv_T);
21217 Prim_Elmt := First_Elmt (Priv_List);
21219 -- In the case of a concurrent type completing a private tagged
21220 -- type, primitives may have been declared in between the two
21221 -- views. These subprograms need to be wrapped the same way
21222 -- entries and protected procedures are handled because they
21223 -- cannot be directly shared by the two views.
21225 if Is_Concurrent_Type (Full_T) then
21227 Conc_Typ : constant Entity_Id :=
21228 Corresponding_Record_Type (Full_T);
21229 Curr_Nod : Node_Id := Parent (Conc_Typ);
21230 Wrap_Spec : Node_Id;
21233 while Present (Prim_Elmt) loop
21234 Prim := Node (Prim_Elmt);
21236 if Comes_From_Source (Prim)
21237 and then not Is_Abstract_Subprogram (Prim)
21240 Make_Subprogram_Declaration (Sloc (Prim),
21244 Obj_Typ => Conc_Typ,
21246 Parameter_Specifications
21249 Insert_After (Curr_Nod, Wrap_Spec);
21250 Curr_Nod := Wrap_Spec;
21252 Analyze (Wrap_Spec);
21254 -- Remove the wrapper from visibility to avoid
21255 -- spurious conflict with the wrapped entity.
21257 Set_Is_Immediately_Visible
21258 (Defining_Entity (Specification (Wrap_Spec)),
21262 Next_Elmt (Prim_Elmt);
21268 -- For non-concurrent types, transfer explicit primitives, but
21269 -- omit those inherited from the parent of the private view
21270 -- since they will be re-inherited later on.
21273 Full_List := Primitive_Operations (Full_T);
21274 while Present (Prim_Elmt) loop
21275 Prim := Node (Prim_Elmt);
21277 if Comes_From_Source (Prim)
21278 and then not Contains (Prim, Full_List)
21280 Append_Elmt (Prim, Full_List);
21283 Next_Elmt (Prim_Elmt);
21287 -- Untagged private view
21290 Full_List := Primitive_Operations (Full_T);
21292 -- In this case the partial view is untagged, so here we locate
21293 -- all of the earlier primitives that need to be treated as
21294 -- dispatching (those that appear between the two views). Note
21295 -- that these additional operations must all be new operations
21296 -- (any earlier operations that override inherited operations
21297 -- of the full view will already have been inserted in the
21298 -- primitives list, marked by Check_Operation_From_Private_View
21299 -- as dispatching. Note that implicit "/=" operators are
21300 -- excluded from being added to the primitives list since they
21301 -- shouldn't be treated as dispatching (tagged "/=" is handled
21304 Prim := Next_Entity (Full_T);
21305 while Present (Prim) and then Prim /= Priv_T loop
21306 if Ekind (Prim) in E_Procedure | E_Function then
21307 Disp_Typ := Find_Dispatching_Type (Prim);
21309 if Disp_Typ = Full_T
21310 and then (Chars (Prim) /= Name_Op_Ne
21311 or else Comes_From_Source (Prim))
21313 Check_Controlling_Formals (Full_T, Prim);
21315 if Is_Suitable_Primitive (Prim)
21316 and then not Is_Dispatching_Operation (Prim)
21318 Append_Elmt (Prim, Full_List);
21319 Set_Is_Dispatching_Operation (Prim);
21320 Set_DT_Position_Value (Prim, No_Uint);
21323 elsif Is_Dispatching_Operation (Prim)
21324 and then Disp_Typ /= Full_T
21326 -- Verify that it is not otherwise controlled by a
21327 -- formal or a return value of type T.
21329 Check_Controlling_Formals (Disp_Typ, Prim);
21333 Next_Entity (Prim);
21337 -- For the tagged case, the two views can share the same primitive
21338 -- operations list and the same class-wide type. Update attributes
21339 -- of the class-wide type which depend on the full declaration.
21341 if Is_Tagged_Type (Priv_T) then
21342 Set_Direct_Primitive_Operations (Priv_T, Full_List);
21343 Set_Class_Wide_Type
21344 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
21346 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
21349 -- For untagged types, copy the primitives across from the private
21350 -- view to the full view (when extensions are allowed), for support
21351 -- of prefixed calls (when extensions are enabled).
21353 elsif Extensions_Allowed then
21354 Priv_List := Primitive_Operations (Priv_T);
21355 Prim_Elmt := First_Elmt (Priv_List);
21357 Full_List := Primitive_Operations (Full_T);
21358 while Present (Prim_Elmt) loop
21359 Prim := Node (Prim_Elmt);
21360 Append_Elmt (Prim, Full_List);
21361 Next_Elmt (Prim_Elmt);
21366 -- Ada 2005 AI 161: Check preelaborable initialization consistency
21368 if Known_To_Have_Preelab_Init (Priv_T) then
21370 -- Case where there is a pragma Preelaborable_Initialization. We
21371 -- always allow this in predefined units, which is cheating a bit,
21372 -- but it means we don't have to struggle to meet the requirements in
21373 -- the RM for having Preelaborable Initialization. Otherwise we
21374 -- require that the type meets the RM rules. But we can't check that
21375 -- yet, because of the rule about overriding Initialize, so we simply
21376 -- set a flag that will be checked at freeze time.
21378 if not In_Predefined_Unit (Full_T) then
21379 Set_Must_Have_Preelab_Init (Full_T);
21383 -- If pragma CPP_Class was applied to the private type declaration,
21384 -- propagate it now to the full type declaration.
21386 if Is_CPP_Class (Priv_T) then
21387 Set_Is_CPP_Class (Full_T);
21388 Set_Convention (Full_T, Convention_CPP);
21390 -- Check that components of imported CPP types do not have default
21393 Check_CPP_Type_Has_No_Defaults (Full_T);
21396 -- If the private view has user specified stream attributes, then so has
21399 -- Why the test, how could these flags be already set in Full_T ???
21401 if Has_Specified_Stream_Read (Priv_T) then
21402 Set_Has_Specified_Stream_Read (Full_T);
21405 if Has_Specified_Stream_Write (Priv_T) then
21406 Set_Has_Specified_Stream_Write (Full_T);
21409 if Has_Specified_Stream_Input (Priv_T) then
21410 Set_Has_Specified_Stream_Input (Full_T);
21413 if Has_Specified_Stream_Output (Priv_T) then
21414 Set_Has_Specified_Stream_Output (Full_T);
21417 -- Propagate Default_Initial_Condition-related attributes from the
21418 -- partial view to the full view.
21420 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
21422 -- And to the underlying full view, if any
21424 if Is_Private_Type (Full_T)
21425 and then Present (Underlying_Full_View (Full_T))
21427 Propagate_DIC_Attributes
21428 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21431 -- Propagate invariant-related attributes from the partial view to the
21434 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
21436 -- And to the underlying full view, if any
21438 if Is_Private_Type (Full_T)
21439 and then Present (Underlying_Full_View (Full_T))
21441 Propagate_Invariant_Attributes
21442 (Underlying_Full_View (Full_T), From_Typ => Priv_T);
21445 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
21446 -- in the full view without advertising the inheritance in the partial
21447 -- view. This can only occur when the partial view has no parent type
21448 -- and the full view has an interface as a parent. Any other scenarios
21449 -- are illegal because implemented interfaces must match between the
21452 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
21454 Full_Par : constant Entity_Id := Etype (Full_T);
21455 Priv_Par : constant Entity_Id := Etype (Priv_T);
21458 if not Is_Interface (Priv_Par)
21459 and then Is_Interface (Full_Par)
21460 and then Has_Inheritable_Invariants (Full_Par)
21463 ("hidden inheritance of class-wide type invariants not "
21469 -- Propagate predicates to full type, and predicate function if already
21470 -- defined. It is not clear that this can actually happen? the partial
21471 -- view cannot be frozen yet, and the predicate function has not been
21472 -- built. Still it is a cheap check and seems safer to make it.
21474 Propagate_Predicate_Attributes (Full_T, Priv_T);
21476 if Is_Private_Type (Full_T)
21477 and then Present (Underlying_Full_View (Full_T))
21479 Propagate_Predicate_Attributes
21480 (Underlying_Full_View (Full_T), Priv_T);
21484 Restore_Ghost_Region (Saved_GM, Saved_IGR);
21485 end Process_Full_View;
21487 -----------------------------------
21488 -- Process_Incomplete_Dependents --
21489 -----------------------------------
21491 procedure Process_Incomplete_Dependents
21493 Full_T : Entity_Id;
21496 Inc_Elmt : Elmt_Id;
21497 Priv_Dep : Entity_Id;
21498 New_Subt : Entity_Id;
21500 Disc_Constraint : Elist_Id;
21503 if No (Private_Dependents (Inc_T)) then
21507 -- Itypes that may be generated by the completion of an incomplete
21508 -- subtype are not used by the back-end and not attached to the tree.
21509 -- They are created only for constraint-checking purposes.
21511 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
21512 while Present (Inc_Elmt) loop
21513 Priv_Dep := Node (Inc_Elmt);
21515 if Ekind (Priv_Dep) = E_Subprogram_Type then
21517 -- An Access_To_Subprogram type may have a return type or a
21518 -- parameter type that is incomplete. Replace with the full view.
21520 if Etype (Priv_Dep) = Inc_T then
21521 Set_Etype (Priv_Dep, Full_T);
21525 Formal : Entity_Id;
21528 Formal := First_Formal (Priv_Dep);
21529 while Present (Formal) loop
21530 if Etype (Formal) = Inc_T then
21531 Set_Etype (Formal, Full_T);
21534 Next_Formal (Formal);
21538 elsif Is_Overloadable (Priv_Dep) then
21540 -- If a subprogram in the incomplete dependents list is primitive
21541 -- for a tagged full type then mark it as a dispatching operation,
21542 -- check whether it overrides an inherited subprogram, and check
21543 -- restrictions on its controlling formals. Note that a protected
21544 -- operation is never dispatching: only its wrapper operation
21545 -- (which has convention Ada) is.
21547 if Is_Tagged_Type (Full_T)
21548 and then Is_Primitive (Priv_Dep)
21549 and then Convention (Priv_Dep) /= Convention_Protected
21551 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21552 Set_Is_Dispatching_Operation (Priv_Dep);
21553 Check_Controlling_Formals (Full_T, Priv_Dep);
21556 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21558 -- Can happen during processing of a body before the completion
21559 -- of a TA type. Ignore, because spec is also on dependent list.
21563 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21564 -- corresponding subtype of the full view.
21566 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21567 and then Comes_From_Source (Priv_Dep)
21569 Set_Subtype_Indication
21570 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21571 Reinit_Field_To_Zero
21572 (Priv_Dep, F_Private_Dependents,
21573 Old_Ekind => E_Incomplete_Subtype);
21574 Mutate_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21575 Set_Etype (Priv_Dep, Full_T);
21576 Set_Analyzed (Parent (Priv_Dep), False);
21578 -- Reanalyze the declaration, suppressing the call to Enter_Name
21579 -- to avoid duplicate names.
21581 Analyze_Subtype_Declaration
21582 (N => Parent (Priv_Dep),
21585 -- Dependent is a subtype
21588 -- We build a new subtype indication using the full view of the
21589 -- incomplete parent. The discriminant constraints have been
21590 -- elaborated already at the point of the subtype declaration.
21592 New_Subt := Create_Itype (E_Void, N);
21594 if Has_Discriminants (Full_T) then
21595 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21597 Disc_Constraint := No_Elist;
21600 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21601 Set_Full_View (Priv_Dep, New_Subt);
21604 Next_Elmt (Inc_Elmt);
21606 end Process_Incomplete_Dependents;
21608 --------------------------------
21609 -- Process_Range_Expr_In_Decl --
21610 --------------------------------
21612 procedure Process_Range_Expr_In_Decl
21615 Subtyp : Entity_Id := Empty;
21616 Check_List : List_Id := No_List;
21617 R_Check_Off : Boolean := False)
21620 R_Checks : Check_Result;
21621 Insert_Node : Node_Id;
21622 Def_Id : Entity_Id;
21625 Analyze_And_Resolve (R, Base_Type (T));
21627 if Nkind (R) = N_Range then
21628 Lo := Low_Bound (R);
21629 Hi := High_Bound (R);
21631 -- Validity checks on the range of a quantified expression are
21632 -- delayed until the construct is transformed into a loop.
21634 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21635 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21639 -- We need to ensure validity of the bounds here, because if we
21640 -- go ahead and do the expansion, then the expanded code will get
21641 -- analyzed with range checks suppressed and we miss the check.
21643 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21644 -- the temporaries generated by routine Remove_Side_Effects by means
21645 -- of validity checks must use the same names. When a range appears
21646 -- in the parent of a generic, the range is processed with checks
21647 -- disabled as part of the generic context and with checks enabled
21648 -- for code generation purposes. This leads to link issues as the
21649 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21650 -- template sees the temporaries generated by Remove_Side_Effects.
21653 Validity_Check_Range (R, Subtyp);
21656 -- If there were errors in the declaration, try and patch up some
21657 -- common mistakes in the bounds. The cases handled are literals
21658 -- which are Integer where the expected type is Real and vice versa.
21659 -- These corrections allow the compilation process to proceed further
21660 -- along since some basic assumptions of the format of the bounds
21663 if Etype (R) = Any_Type then
21664 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21666 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21668 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21670 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21672 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21674 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21676 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21678 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21685 -- If the bounds of the range have been mistakenly given as string
21686 -- literals (perhaps in place of character literals), then an error
21687 -- has already been reported, but we rewrite the string literal as a
21688 -- bound of the range's type to avoid blowups in later processing
21689 -- that looks at static values.
21691 if Nkind (Lo) = N_String_Literal then
21693 Make_Attribute_Reference (Sloc (Lo),
21694 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21695 Attribute_Name => Name_First));
21696 Analyze_And_Resolve (Lo);
21699 if Nkind (Hi) = N_String_Literal then
21701 Make_Attribute_Reference (Sloc (Hi),
21702 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21703 Attribute_Name => Name_First));
21704 Analyze_And_Resolve (Hi);
21707 -- If bounds aren't scalar at this point then exit, avoiding
21708 -- problems with further processing of the range in this procedure.
21710 if not Is_Scalar_Type (Etype (Lo)) then
21714 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21715 -- then range of the base type. Here we check whether the bounds
21716 -- are in the range of the subtype itself. Note that if the bounds
21717 -- represent the null range the Constraint_Error exception should
21720 -- ??? The following code should be cleaned up as follows
21722 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21723 -- is done in the call to Range_Check (R, T); below
21725 -- 2. The use of R_Check_Off should be investigated and possibly
21726 -- removed, this would clean up things a bit.
21728 if Is_Null_Range (Lo, Hi) then
21732 -- Capture values of bounds and generate temporaries for them
21733 -- if needed, before applying checks, since checks may cause
21734 -- duplication of the expression without forcing evaluation.
21736 -- The forced evaluation removes side effects from expressions,
21737 -- which should occur also in GNATprove mode. Otherwise, we end up
21738 -- with unexpected insertions of actions at places where this is
21739 -- not supposed to occur, e.g. on default parameters of a call.
21741 if Expander_Active or GNATprove_Mode then
21743 -- Call Force_Evaluation to create declarations as needed to
21744 -- deal with side effects, and also create typ_FIRST/LAST
21745 -- entities for bounds if we have a subtype name.
21747 -- Note: we do this transformation even if expansion is not
21748 -- active if we are in GNATprove_Mode since the transformation
21749 -- is in general required to ensure that the resulting tree has
21750 -- proper Ada semantics.
21753 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21755 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21758 -- We use a flag here instead of suppressing checks on the type
21759 -- because the type we check against isn't necessarily the place
21760 -- where we put the check.
21762 if not R_Check_Off then
21763 R_Checks := Get_Range_Checks (R, T);
21765 -- Look up tree to find an appropriate insertion point. We
21766 -- can't just use insert_actions because later processing
21767 -- depends on the insertion node. Prior to Ada 2012 the
21768 -- insertion point could only be a declaration or a loop, but
21769 -- quantified expressions can appear within any context in an
21770 -- expression, and the insertion point can be any statement,
21771 -- pragma, or declaration.
21773 Insert_Node := Parent (R);
21774 while Present (Insert_Node) loop
21776 Nkind (Insert_Node) in N_Declaration
21778 Nkind (Insert_Node) not in N_Component_Declaration
21779 | N_Loop_Parameter_Specification
21780 | N_Function_Specification
21781 | N_Procedure_Specification;
21783 exit when Nkind (Insert_Node) in
21784 N_Later_Decl_Item |
21785 N_Statement_Other_Than_Procedure_Call |
21786 N_Procedure_Call_Statement |
21789 Insert_Node := Parent (Insert_Node);
21792 -- Why would Type_Decl not be present??? Without this test,
21793 -- short regression tests fail.
21795 if Present (Insert_Node) then
21797 -- Case of loop statement. Verify that the range is part
21798 -- of the subtype indication of the iteration scheme.
21800 if Nkind (Insert_Node) = N_Loop_Statement then
21805 Indic := Parent (R);
21806 while Present (Indic)
21807 and then Nkind (Indic) /= N_Subtype_Indication
21809 Indic := Parent (Indic);
21812 if Present (Indic) then
21813 Def_Id := Etype (Subtype_Mark (Indic));
21815 Insert_Range_Checks
21819 Sloc (Insert_Node),
21820 Do_Before => True);
21824 -- Case of declarations. If the declaration is for a type
21825 -- and involves discriminants, the checks are premature at
21826 -- the declaration point and need to wait for the expansion
21827 -- of the initialization procedure, which will pass in the
21828 -- list to put them on; otherwise, the checks are done at
21829 -- the declaration point and there is no need to do them
21830 -- again in the initialization procedure.
21832 elsif Nkind (Insert_Node) in N_Declaration then
21833 Def_Id := Defining_Identifier (Insert_Node);
21835 if (Ekind (Def_Id) = E_Record_Type
21836 and then Depends_On_Discriminant (R))
21838 (Ekind (Def_Id) = E_Protected_Type
21839 and then Has_Discriminants (Def_Id))
21841 if Present (Check_List) then
21842 Append_Range_Checks
21844 Check_List, Def_Id, Sloc (Insert_Node));
21848 if No (Check_List) then
21849 Insert_Range_Checks
21851 Insert_Node, Def_Id, Sloc (Insert_Node));
21855 -- Case of statements. Drop the checks, as the range appears
21856 -- in the context of a quantified expression. Insertion will
21857 -- take place when expression is expanded.
21866 -- Case of other than an explicit N_Range node
21868 -- The forced evaluation removes side effects from expressions, which
21869 -- should occur also in GNATprove mode. Otherwise, we end up with
21870 -- unexpected insertions of actions at places where this is not
21871 -- supposed to occur, e.g. on default parameters of a call.
21873 elsif Expander_Active or GNATprove_Mode then
21874 Get_Index_Bounds (R, Lo, Hi);
21875 Force_Evaluation (Lo);
21876 Force_Evaluation (Hi);
21878 end Process_Range_Expr_In_Decl;
21880 --------------------------------------
21881 -- Process_Real_Range_Specification --
21882 --------------------------------------
21884 procedure Process_Real_Range_Specification (Def : Node_Id) is
21885 Spec : constant Node_Id := Real_Range_Specification (Def);
21888 Err : Boolean := False;
21890 procedure Analyze_Bound (N : Node_Id);
21891 -- Analyze and check one bound
21893 -------------------
21894 -- Analyze_Bound --
21895 -------------------
21897 procedure Analyze_Bound (N : Node_Id) is
21899 Analyze_And_Resolve (N, Any_Real);
21901 if not Is_OK_Static_Expression (N) then
21902 Flag_Non_Static_Expr
21903 ("bound in real type definition is not static!", N);
21908 -- Start of processing for Process_Real_Range_Specification
21911 if Present (Spec) then
21912 Lo := Low_Bound (Spec);
21913 Hi := High_Bound (Spec);
21914 Analyze_Bound (Lo);
21915 Analyze_Bound (Hi);
21917 -- If error, clear away junk range specification
21920 Set_Real_Range_Specification (Def, Empty);
21923 end Process_Real_Range_Specification;
21925 ---------------------
21926 -- Process_Subtype --
21927 ---------------------
21929 function Process_Subtype
21931 Related_Nod : Node_Id;
21932 Related_Id : Entity_Id := Empty;
21933 Suffix : Character := ' ') return Entity_Id
21935 procedure Check_Incomplete (T : Node_Id);
21936 -- Called to verify that an incomplete type is not used prematurely
21938 ----------------------
21939 -- Check_Incomplete --
21940 ----------------------
21942 procedure Check_Incomplete (T : Node_Id) is
21944 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21946 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21948 not (Ada_Version >= Ada_2005
21950 (Nkind (Parent (T)) = N_Subtype_Declaration
21951 or else (Nkind (Parent (T)) = N_Subtype_Indication
21952 and then Nkind (Parent (Parent (T))) =
21953 N_Subtype_Declaration)))
21955 Error_Msg_N ("invalid use of type before its full declaration", T);
21957 end Check_Incomplete;
21962 Def_Id : Entity_Id;
21963 Error_Node : Node_Id;
21964 Full_View_Id : Entity_Id;
21965 Subtype_Mark_Id : Entity_Id;
21967 May_Have_Null_Exclusion : Boolean;
21969 -- Start of processing for Process_Subtype
21972 -- Case of no constraints present
21974 if Nkind (S) /= N_Subtype_Indication then
21977 -- No way to proceed if the subtype indication is malformed. This
21978 -- will happen for example when the subtype indication in an object
21979 -- declaration is missing altogether and the expression is analyzed
21980 -- as if it were that indication.
21982 if not Is_Entity_Name (S) then
21986 Check_Incomplete (S);
21989 -- The following mirroring of assertion in Null_Exclusion_Present is
21990 -- ugly, can't we have a range, a static predicate or even a flag???
21992 May_Have_Null_Exclusion :=
21995 Nkind (P) in N_Access_Definition
21996 | N_Access_Function_Definition
21997 | N_Access_Procedure_Definition
21998 | N_Access_To_Object_Definition
22000 | N_Component_Definition
22001 | N_Derived_Type_Definition
22002 | N_Discriminant_Specification
22003 | N_Formal_Object_Declaration
22004 | N_Function_Specification
22005 | N_Object_Declaration
22006 | N_Object_Renaming_Declaration
22007 | N_Parameter_Specification
22008 | N_Subtype_Declaration;
22010 -- Ada 2005 (AI-231): Static check
22012 if Ada_Version >= Ada_2005
22013 and then May_Have_Null_Exclusion
22014 and then Null_Exclusion_Present (P)
22015 and then Nkind (P) /= N_Access_To_Object_Definition
22016 and then not Is_Access_Type (Entity (S))
22018 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
22021 -- Create an Itype that is a duplicate of Entity (S) but with the
22022 -- null-exclusion attribute.
22024 if May_Have_Null_Exclusion
22025 and then Is_Access_Type (Entity (S))
22026 and then Null_Exclusion_Present (P)
22028 -- No need to check the case of an access to object definition.
22029 -- It is correct to define double not-null pointers.
22032 -- type Not_Null_Int_Ptr is not null access Integer;
22033 -- type Acc is not null access Not_Null_Int_Ptr;
22035 and then Nkind (P) /= N_Access_To_Object_Definition
22037 if Can_Never_Be_Null (Entity (S)) then
22038 case Nkind (Related_Nod) is
22039 when N_Full_Type_Declaration =>
22040 if Nkind (Type_Definition (Related_Nod))
22041 in N_Array_Type_Definition
22045 (Component_Definition
22046 (Type_Definition (Related_Nod)));
22049 Subtype_Indication (Type_Definition (Related_Nod));
22052 when N_Subtype_Declaration =>
22053 Error_Node := Subtype_Indication (Related_Nod);
22055 when N_Object_Declaration =>
22056 Error_Node := Object_Definition (Related_Nod);
22058 when N_Component_Declaration =>
22060 Subtype_Indication (Component_Definition (Related_Nod));
22062 when N_Allocator =>
22063 Error_Node := Expression (Related_Nod);
22066 pragma Assert (False);
22067 Error_Node := Related_Nod;
22071 ("`NOT NULL` not allowed (& already excludes null)",
22077 Create_Null_Excluding_Itype
22079 Related_Nod => P));
22080 Set_Entity (S, Etype (S));
22085 -- Case of constraint present, so that we have an N_Subtype_Indication
22086 -- node (this node is created only if constraints are present).
22089 Find_Type (Subtype_Mark (S));
22091 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
22093 (Nkind (Parent (S)) = N_Subtype_Declaration
22094 and then Is_Itype (Defining_Identifier (Parent (S))))
22096 Check_Incomplete (Subtype_Mark (S));
22100 Subtype_Mark_Id := Entity (Subtype_Mark (S));
22102 -- Explicit subtype declaration case
22104 if Nkind (P) = N_Subtype_Declaration then
22105 Def_Id := Defining_Identifier (P);
22107 -- Explicit derived type definition case
22109 elsif Nkind (P) = N_Derived_Type_Definition then
22110 Def_Id := Defining_Identifier (Parent (P));
22112 -- Implicit case, the Def_Id must be created as an implicit type.
22113 -- The one exception arises in the case of concurrent types, array
22114 -- and access types, where other subsidiary implicit types may be
22115 -- created and must appear before the main implicit type. In these
22116 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
22117 -- has not yet been called to create Def_Id.
22120 if Is_Array_Type (Subtype_Mark_Id)
22121 or else Is_Concurrent_Type (Subtype_Mark_Id)
22122 or else Is_Access_Type (Subtype_Mark_Id)
22126 -- For the other cases, we create a new unattached Itype,
22127 -- and set the indication to ensure it gets attached later.
22131 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22135 -- If the kind of constraint is invalid for this kind of type,
22136 -- then give an error, and then pretend no constraint was given.
22138 if not Is_Valid_Constraint_Kind
22139 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
22142 ("incorrect constraint for this kind of type", Constraint (S));
22144 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
22146 -- Set Ekind of orphan itype, to prevent cascaded errors
22148 if Present (Def_Id) then
22149 Mutate_Ekind (Def_Id, Ekind (Any_Type));
22152 -- Make recursive call, having got rid of the bogus constraint
22154 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
22157 -- Remaining processing depends on type. Select on Base_Type kind to
22158 -- ensure getting to the concrete type kind in the case of a private
22159 -- subtype (needed when only doing semantic analysis).
22161 case Ekind (Base_Type (Subtype_Mark_Id)) is
22162 when Access_Kind =>
22164 -- If this is a constraint on a class-wide type, discard it.
22165 -- There is currently no way to express a partial discriminant
22166 -- constraint on a type with unknown discriminants. This is
22167 -- a pathology that the ACATS wisely decides not to test.
22169 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
22170 if Comes_From_Source (S) then
22172 ("constraint on class-wide type ignored??",
22176 if Nkind (P) = N_Subtype_Declaration then
22177 Set_Subtype_Indication (P,
22178 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
22181 return Subtype_Mark_Id;
22184 Constrain_Access (Def_Id, S, Related_Nod);
22187 and then Is_Itype (Designated_Type (Def_Id))
22188 and then Nkind (Related_Nod) = N_Subtype_Declaration
22189 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
22191 Build_Itype_Reference
22192 (Designated_Type (Def_Id), Related_Nod);
22196 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22198 when Decimal_Fixed_Point_Kind =>
22199 Constrain_Decimal (Def_Id, S);
22201 when Enumeration_Kind =>
22202 Constrain_Enumeration (Def_Id, S);
22204 when Ordinary_Fixed_Point_Kind =>
22205 Constrain_Ordinary_Fixed (Def_Id, S);
22208 Constrain_Float (Def_Id, S);
22210 when Integer_Kind =>
22211 Constrain_Integer (Def_Id, S);
22213 when Class_Wide_Kind
22214 | E_Incomplete_Type
22218 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22220 if Ekind (Def_Id) = E_Incomplete_Type then
22221 Set_Private_Dependents (Def_Id, New_Elmt_List);
22224 when Private_Kind =>
22226 -- A private type with unknown discriminants may be completed
22227 -- by an unconstrained array type.
22229 if Has_Unknown_Discriminants (Subtype_Mark_Id)
22230 and then Present (Full_View (Subtype_Mark_Id))
22231 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
22233 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
22235 -- ... but more commonly is completed by a discriminated record
22239 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
22242 -- The base type may be private but Def_Id may be a full view
22245 if Is_Private_Type (Def_Id) then
22246 Set_Private_Dependents (Def_Id, New_Elmt_List);
22249 -- In case of an invalid constraint prevent further processing
22250 -- since the type constructed is missing expected fields.
22252 if Etype (Def_Id) = Any_Type then
22256 -- If the full view is that of a task with discriminants,
22257 -- we must constrain both the concurrent type and its
22258 -- corresponding record type. Otherwise we will just propagate
22259 -- the constraint to the full view, if available.
22261 if Present (Full_View (Subtype_Mark_Id))
22262 and then Has_Discriminants (Subtype_Mark_Id)
22263 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
22266 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
22268 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
22269 Constrain_Concurrent (Full_View_Id, S,
22270 Related_Nod, Related_Id, Suffix);
22271 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
22272 Set_Full_View (Def_Id, Full_View_Id);
22274 -- Introduce an explicit reference to the private subtype,
22275 -- to prevent scope anomalies in gigi if first use appears
22276 -- in a nested context, e.g. a later function body.
22277 -- Should this be generated in other contexts than a full
22278 -- type declaration?
22280 if Is_Itype (Def_Id)
22282 Nkind (Parent (P)) = N_Full_Type_Declaration
22284 Build_Itype_Reference (Def_Id, Parent (P));
22288 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
22291 when Concurrent_Kind =>
22292 Constrain_Concurrent (Def_Id, S,
22293 Related_Nod, Related_Id, Suffix);
22296 Error_Msg_N ("invalid subtype mark in subtype indication", S);
22299 -- Size, Alignment, Representation aspects and Convention are always
22300 -- inherited from the base type.
22302 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
22303 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
22304 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
22306 -- The anonymous subtype created for the subtype indication
22307 -- inherits the predicates of the parent.
22309 if Has_Predicates (Subtype_Mark_Id) then
22310 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
22312 -- Indicate where the predicate function may be found
22314 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
22315 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
22321 end Process_Subtype;
22323 -----------------------------
22324 -- Record_Type_Declaration --
22325 -----------------------------
22327 procedure Record_Type_Declaration
22332 Def : constant Node_Id := Type_Definition (N);
22333 Is_Tagged : Boolean;
22334 Tag_Comp : Entity_Id;
22337 -- These flags must be initialized before calling Process_Discriminants
22338 -- because this routine makes use of them.
22340 Mutate_Ekind (T, E_Record_Type);
22342 Reinit_Size_Align (T);
22343 Set_Interfaces (T, No_Elist);
22344 Set_Stored_Constraint (T, No_Elist);
22345 Set_Default_SSO (T);
22346 Set_No_Reordering (T, No_Component_Reordering);
22350 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
22351 -- The flag Is_Tagged_Type might have already been set by
22352 -- Find_Type_Name if it detected an error for declaration T. This
22353 -- arises in the case of private tagged types where the full view
22354 -- omits the word tagged.
22357 Tagged_Present (Def)
22358 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
22360 Set_Is_Limited_Record (T, Limited_Present (Def));
22363 Set_Is_Tagged_Type (T, True);
22364 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
22367 -- Type is abstract if full declaration carries keyword, or if
22368 -- previous partial view did.
22370 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
22371 or else Abstract_Present (Def));
22375 Analyze_Interface_Declaration (T, Def);
22377 if Present (Discriminant_Specifications (N)) then
22379 ("interface types cannot have discriminants",
22380 Defining_Identifier
22381 (First (Discriminant_Specifications (N))));
22385 -- First pass: if there are self-referential access components,
22386 -- create the required anonymous access type declarations, and if
22387 -- need be an incomplete type declaration for T itself.
22389 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
22391 if Ada_Version >= Ada_2005
22392 and then Present (Interface_List (Def))
22394 Check_Interfaces (N, Def);
22397 Ifaces_List : Elist_Id;
22400 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
22401 -- already in the parents.
22405 Ifaces_List => Ifaces_List,
22406 Exclude_Parents => True);
22408 Set_Interfaces (T, Ifaces_List);
22412 -- Records constitute a scope for the component declarations within.
22413 -- The scope is created prior to the processing of these declarations.
22414 -- Discriminants are processed first, so that they are visible when
22415 -- processing the other components. The Ekind of the record type itself
22416 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
22418 -- Enter record scope
22422 -- If an incomplete or private type declaration was already given for
22423 -- the type, then this scope already exists, and the discriminants have
22424 -- been declared within. We must verify that the full declaration
22425 -- matches the incomplete one.
22427 Check_Or_Process_Discriminants (N, T, Prev);
22429 Set_Is_Constrained (T, not Has_Discriminants (T));
22430 Set_Has_Delayed_Freeze (T, True);
22432 -- For tagged types add a manually analyzed component corresponding
22433 -- to the component _tag, the corresponding piece of tree will be
22434 -- expanded as part of the freezing actions if it is not a CPP_Class.
22438 -- Do not add the tag unless we are in expansion mode
22440 if Expander_Active then
22441 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
22442 Enter_Name (Tag_Comp);
22444 Mutate_Ekind (Tag_Comp, E_Component);
22445 Set_Is_Tag (Tag_Comp);
22446 Set_Is_Aliased (Tag_Comp);
22447 Set_Is_Independent (Tag_Comp);
22448 Set_Etype (Tag_Comp, RTE (RE_Tag));
22449 Set_DT_Entry_Count (Tag_Comp, No_Uint);
22450 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
22451 Reinit_Component_Location (Tag_Comp);
22453 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
22454 -- implemented interfaces.
22456 if Has_Interfaces (T) then
22457 Add_Interface_Tag_Components (N, T);
22461 Make_Class_Wide_Type (T);
22462 Set_Direct_Primitive_Operations (T, New_Elmt_List);
22465 -- We must suppress range checks when processing record components in
22466 -- the presence of discriminants, since we don't want spurious checks to
22467 -- be generated during their analysis, but Suppress_Range_Checks flags
22468 -- must be reset the after processing the record definition.
22470 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
22471 -- couldn't we just use the normal range check suppression method here.
22472 -- That would seem cleaner ???
22474 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
22475 Set_Kill_Range_Checks (T, True);
22476 Record_Type_Definition (Def, Prev);
22477 Set_Kill_Range_Checks (T, False);
22479 Record_Type_Definition (Def, Prev);
22482 -- Exit from record scope
22486 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
22487 -- the implemented interfaces and associate them an aliased entity.
22490 and then not Is_Empty_List (Interface_List (Def))
22492 Derive_Progenitor_Subprograms (T, T);
22495 Check_Function_Writable_Actuals (N);
22496 end Record_Type_Declaration;
22498 ----------------------------
22499 -- Record_Type_Definition --
22500 ----------------------------
22502 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
22503 Component : Entity_Id;
22504 Ctrl_Components : Boolean := False;
22505 Final_Storage_Only : Boolean;
22509 if Ekind (Prev_T) = E_Incomplete_Type then
22510 T := Full_View (Prev_T);
22515 Final_Storage_Only := not Is_Controlled (T);
22517 -- Ada 2005: Check whether an explicit "limited" is present in a derived
22518 -- type declaration.
22520 if Parent_Kind (Def) = N_Derived_Type_Definition
22521 and then Limited_Present (Parent (Def))
22523 Set_Is_Limited_Record (T);
22526 -- If the component list of a record type is defined by the reserved
22527 -- word null and there is no discriminant part, then the record type has
22528 -- no components and all records of the type are null records (RM 3.7)
22529 -- This procedure is also called to process the extension part of a
22530 -- record extension, in which case the current scope may have inherited
22534 and then Present (Component_List (Def))
22535 and then not Null_Present (Component_List (Def))
22537 Analyze_Declarations (Component_Items (Component_List (Def)));
22539 if Present (Variant_Part (Component_List (Def))) then
22540 Analyze (Variant_Part (Component_List (Def)));
22544 -- After completing the semantic analysis of the record definition,
22545 -- record components, both new and inherited, are accessible. Set their
22546 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22547 -- whose Ekind may be void.
22549 Component := First_Entity (Current_Scope);
22550 while Present (Component) loop
22551 if Ekind (Component) = E_Void
22552 and then not Is_Itype (Component)
22554 Mutate_Ekind (Component, E_Component);
22555 Reinit_Component_Location (Component);
22558 Propagate_Concurrent_Flags (T, Etype (Component));
22560 if Ekind (Component) /= E_Component then
22563 -- Do not set Has_Controlled_Component on a class-wide equivalent
22564 -- type. See Make_CW_Equivalent_Type.
22566 elsif not Is_Class_Wide_Equivalent_Type (T)
22567 and then (Has_Controlled_Component (Etype (Component))
22568 or else (Chars (Component) /= Name_uParent
22569 and then Is_Controlled (Etype (Component))))
22571 Set_Has_Controlled_Component (T, True);
22572 Final_Storage_Only :=
22574 and then Finalize_Storage_Only (Etype (Component));
22575 Ctrl_Components := True;
22578 Next_Entity (Component);
22581 -- A Type is Finalize_Storage_Only only if all its controlled components
22584 if Ctrl_Components then
22585 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22588 -- Place reference to end record on the proper entity, which may
22589 -- be a partial view.
22591 if Present (Def) then
22592 Process_End_Label (Def, 'e', Prev_T);
22594 end Record_Type_Definition;
22596 ---------------------------
22597 -- Replace_Discriminants --
22598 ---------------------------
22600 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id) is
22601 function Process (N : Node_Id) return Traverse_Result;
22607 function Process (N : Node_Id) return Traverse_Result is
22611 if Nkind (N) = N_Discriminant_Specification then
22612 Comp := First_Discriminant (Typ);
22613 while Present (Comp) loop
22614 if Original_Record_Component (Comp) = Defining_Identifier (N)
22615 or else Chars (Comp) = Chars (Defining_Identifier (N))
22617 Set_Defining_Identifier (N, Comp);
22621 Next_Discriminant (Comp);
22624 elsif Nkind (N) = N_Variant_Part then
22625 Comp := First_Discriminant (Typ);
22626 while Present (Comp) loop
22627 if Original_Record_Component (Comp) = Entity (Name (N))
22628 or else Chars (Comp) = Chars (Name (N))
22630 -- Make sure to preserve the type coming from the parent on
22631 -- the Name, even if the subtype of the discriminant can be
22632 -- constrained, so that discrete choices inherited from the
22633 -- parent in the variant part are not flagged as violating
22634 -- the constraints of the subtype.
22637 Typ : constant Entity_Id := Etype (Name (N));
22639 Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
22640 Set_Etype (Name (N), Typ);
22645 Next_Discriminant (Comp);
22652 procedure Replace is new Traverse_Proc (Process);
22654 -- Start of processing for Replace_Discriminants
22658 end Replace_Discriminants;
22660 -------------------------------
22661 -- Set_Completion_Referenced --
22662 -------------------------------
22664 procedure Set_Completion_Referenced (E : Entity_Id) is
22666 -- If in main unit, mark entity that is a completion as referenced,
22667 -- warnings go on the partial view when needed.
22669 if In_Extended_Main_Source_Unit (E) then
22670 Set_Referenced (E);
22672 end Set_Completion_Referenced;
22674 ---------------------
22675 -- Set_Default_SSO --
22676 ---------------------
22678 procedure Set_Default_SSO (T : Entity_Id) is
22680 case Opt.Default_SSO is
22684 Set_SSO_Set_Low_By_Default (T, True);
22686 Set_SSO_Set_High_By_Default (T, True);
22688 raise Program_Error;
22690 end Set_Default_SSO;
22692 ---------------------
22693 -- Set_Fixed_Range --
22694 ---------------------
22696 -- The range for fixed-point types is complicated by the fact that we
22697 -- do not know the exact end points at the time of the declaration. This
22698 -- is true for three reasons:
22700 -- A size clause may affect the fudging of the end-points.
22701 -- A small clause may affect the values of the end-points.
22702 -- We try to include the end-points if it does not affect the size.
22704 -- This means that the actual end-points must be established at the
22705 -- point when the type is frozen. Meanwhile, we first narrow the range
22706 -- as permitted (so that it will fit if necessary in a small specified
22707 -- size), and then build a range subtree with these narrowed bounds.
22708 -- Set_Fixed_Range constructs the range from real literal values, and
22709 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22711 -- The parent of this range is set to point to the entity so that it is
22712 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22713 -- other scalar types, which are just pointers to the range in the
22714 -- original tree, this would otherwise be an orphan).
22716 -- The tree is left unanalyzed. When the type is frozen, the processing
22717 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22718 -- analyzed, and uses this as an indication that it should complete
22719 -- work on the range (it will know the final small and size values).
22721 procedure Set_Fixed_Range
22727 S : constant Node_Id :=
22729 Low_Bound => Make_Real_Literal (Loc, Lo),
22730 High_Bound => Make_Real_Literal (Loc, Hi));
22732 Set_Scalar_Range (E, S);
22735 -- Before the freeze point, the bounds of a fixed point are universal
22736 -- and carry the corresponding type.
22738 Set_Etype (Low_Bound (S), Universal_Real);
22739 Set_Etype (High_Bound (S), Universal_Real);
22740 end Set_Fixed_Range;
22742 ----------------------------------
22743 -- Set_Scalar_Range_For_Subtype --
22744 ----------------------------------
22746 procedure Set_Scalar_Range_For_Subtype
22747 (Def_Id : Entity_Id;
22751 Kind : constant Entity_Kind := Ekind (Def_Id);
22754 -- Defend against previous error
22756 if Nkind (R) = N_Error then
22760 Set_Scalar_Range (Def_Id, R);
22762 -- We need to link the range into the tree before resolving it so
22763 -- that types that are referenced, including importantly the subtype
22764 -- itself, are properly frozen (Freeze_Expression requires that the
22765 -- expression be properly linked into the tree). Of course if it is
22766 -- already linked in, then we do not disturb the current link.
22768 if No (Parent (R)) then
22769 Set_Parent (R, Def_Id);
22772 -- Reset the kind of the subtype during analysis of the range, to
22773 -- catch possible premature use in the bounds themselves.
22775 Mutate_Ekind (Def_Id, E_Void);
22776 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22777 Mutate_Ekind (Def_Id, Kind);
22778 end Set_Scalar_Range_For_Subtype;
22780 --------------------------------------------------------
22781 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22782 --------------------------------------------------------
22784 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22788 -- Make sure set if encountered during Expand_To_Stored_Constraint
22790 Set_Stored_Constraint (E, No_Elist);
22792 -- Give it the right value
22794 if Is_Constrained (E) and then Has_Discriminants (E) then
22795 Set_Stored_Constraint (E,
22796 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22798 end Set_Stored_Constraint_From_Discriminant_Constraint;
22800 -------------------------------------
22801 -- Signed_Integer_Type_Declaration --
22802 -------------------------------------
22804 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22805 Implicit_Base : Entity_Id;
22806 Base_Typ : Entity_Id;
22809 Errs : Boolean := False;
22813 function Can_Derive_From (E : Entity_Id) return Boolean;
22814 -- Determine whether given bounds allow derivation from specified type
22816 procedure Check_Bound (Expr : Node_Id);
22817 -- Check bound to make sure it is integral and static. If not, post
22818 -- appropriate error message and set Errs flag
22820 ---------------------
22821 -- Can_Derive_From --
22822 ---------------------
22824 -- Note we check both bounds against both end values, to deal with
22825 -- strange types like ones with a range of 0 .. -12341234.
22827 function Can_Derive_From (E : Entity_Id) return Boolean is
22828 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22829 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22831 return Lo <= Lo_Val and then Lo_Val <= Hi
22833 Lo <= Hi_Val and then Hi_Val <= Hi;
22834 end Can_Derive_From;
22840 procedure Check_Bound (Expr : Node_Id) is
22842 -- If a range constraint is used as an integer type definition, each
22843 -- bound of the range must be defined by a static expression of some
22844 -- integer type, but the two bounds need not have the same integer
22845 -- type (Negative bounds are allowed.) (RM 3.5.4)
22847 if not Is_Integer_Type (Etype (Expr)) then
22849 ("integer type definition bounds must be of integer type", Expr);
22852 elsif not Is_OK_Static_Expression (Expr) then
22853 Flag_Non_Static_Expr
22854 ("non-static expression used for integer type bound!", Expr);
22857 -- Otherwise the bounds are folded into literals
22859 elsif Is_Entity_Name (Expr) then
22860 Fold_Uint (Expr, Expr_Value (Expr), True);
22864 -- Start of processing for Signed_Integer_Type_Declaration
22867 -- Create an anonymous base type
22870 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22872 -- Analyze and check the bounds, they can be of any integer type
22874 Lo := Low_Bound (Def);
22875 Hi := High_Bound (Def);
22877 -- Arbitrarily use Integer as the type if either bound had an error
22879 if Hi = Error or else Lo = Error then
22880 Base_Typ := Any_Integer;
22881 Set_Error_Posted (T, True);
22884 -- Here both bounds are OK expressions
22887 Analyze_And_Resolve (Lo, Any_Integer);
22888 Analyze_And_Resolve (Hi, Any_Integer);
22894 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
22895 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
22898 -- Find type to derive from
22900 Lo_Val := Expr_Value (Lo);
22901 Hi_Val := Expr_Value (Hi);
22903 if Can_Derive_From (Standard_Short_Short_Integer) then
22904 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22906 elsif Can_Derive_From (Standard_Short_Integer) then
22907 Base_Typ := Base_Type (Standard_Short_Integer);
22909 elsif Can_Derive_From (Standard_Integer) then
22910 Base_Typ := Base_Type (Standard_Integer);
22912 elsif Can_Derive_From (Standard_Long_Integer) then
22913 Base_Typ := Base_Type (Standard_Long_Integer);
22915 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22916 Check_Restriction (No_Long_Long_Integers, Def);
22917 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22919 elsif Can_Derive_From (Standard_Long_Long_Long_Integer) then
22920 Check_Restriction (No_Long_Long_Integers, Def);
22921 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
22924 Base_Typ := Base_Type (Standard_Long_Long_Long_Integer);
22925 Error_Msg_N ("integer type definition bounds out of range", Def);
22926 Hi := Type_High_Bound (Standard_Long_Long_Long_Integer);
22927 Lo := Type_Low_Bound (Standard_Long_Long_Long_Integer);
22931 -- Set the type of the bounds to the implicit base: we cannot set it to
22932 -- the new type, because this would be a forward reference for the code
22933 -- generator and, if the original type is user-defined, this could even
22934 -- lead to spurious semantic errors. Furthermore we do not set it to be
22935 -- universal, because this could make it much larger than needed here.
22938 Set_Etype (Lo, Implicit_Base);
22939 Set_Etype (Hi, Implicit_Base);
22942 -- Complete both implicit base and declared first subtype entities. The
22943 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22944 -- are not clobbered when the signed integer type acts as a full view of
22947 Set_Etype (Implicit_Base, Base_Typ);
22948 Set_Size_Info (Implicit_Base, Base_Typ);
22949 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22950 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22951 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22953 Mutate_Ekind (T, E_Signed_Integer_Subtype);
22954 Set_Etype (T, Implicit_Base);
22955 Set_Size_Info (T, Implicit_Base);
22956 Inherit_Rep_Item_Chain (T, Implicit_Base);
22957 Set_Scalar_Range (T, Def);
22958 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22959 Set_Is_Constrained (T);
22960 end Signed_Integer_Type_Declaration;