1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, 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 Debug; use Debug;
30 with Elists; use Elists;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch10; use Sem_Ch10;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Dim; use Sem_Dim;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Prag; use Sem_Prag;
69 with Sem_Res; use Sem_Res;
70 with Sem_Smem; use Sem_Smem;
71 with Sem_Type; use Sem_Type;
72 with Sem_Util; use Sem_Util;
73 with Sem_Warn; use Sem_Warn;
74 with Stand; use Stand;
75 with Sinfo; use Sinfo;
76 with Sinput; use Sinput;
77 with Snames; use Snames;
78 with Targparm; use Targparm;
79 with Tbuild; use Tbuild;
80 with Ttypes; use Ttypes;
81 with Uintp; use Uintp;
82 with Urealp; use Urealp;
84 package body Sem_Ch3 is
86 -----------------------
87 -- Local Subprograms --
88 -----------------------
90 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
91 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
92 -- abstract interface types implemented by a record type or a derived
95 procedure Analyze_Object_Contract (Obj_Id : Entity_Id);
96 -- Analyze all delayed pragmas chained on the contract of object Obj_Id as
97 -- if they appeared at the end of the declarative region. The pragmas to be
105 procedure Build_Derived_Type
107 Parent_Type : Entity_Id;
108 Derived_Type : Entity_Id;
109 Is_Completion : Boolean;
110 Derive_Subps : Boolean := True);
111 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
112 -- the N_Full_Type_Declaration node containing the derived type definition.
113 -- Parent_Type is the entity for the parent type in the derived type
114 -- definition and Derived_Type the actual derived type. Is_Completion must
115 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
116 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
117 -- completion of a private type declaration. If Is_Completion is set to
118 -- True, N is the completion of a private type declaration and Derived_Type
119 -- is different from the defining identifier inside N (i.e. Derived_Type /=
120 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
121 -- subprograms should be derived. The only case where this parameter is
122 -- False is when Build_Derived_Type is recursively called to process an
123 -- implicit derived full type for a type derived from a private type (in
124 -- that case the subprograms must only be derived for the private view of
127 -- ??? These flags need a bit of re-examination and re-documentation:
128 -- ??? are they both necessary (both seem related to the recursion)?
130 procedure Build_Derived_Access_Type
132 Parent_Type : Entity_Id;
133 Derived_Type : Entity_Id);
134 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
135 -- create an implicit base if the parent type is constrained or if the
136 -- subtype indication has a constraint.
138 procedure Build_Derived_Array_Type
140 Parent_Type : Entity_Id;
141 Derived_Type : Entity_Id);
142 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
143 -- create an implicit base if the parent type is constrained or if the
144 -- subtype indication has a constraint.
146 procedure Build_Derived_Concurrent_Type
148 Parent_Type : Entity_Id;
149 Derived_Type : Entity_Id);
150 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
151 -- protected type, inherit entries and protected subprograms, check
152 -- legality of discriminant constraints if any.
154 procedure Build_Derived_Enumeration_Type
156 Parent_Type : Entity_Id;
157 Derived_Type : Entity_Id);
158 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
159 -- type, we must create a new list of literals. Types derived from
160 -- Character and [Wide_]Wide_Character are special-cased.
162 procedure Build_Derived_Numeric_Type
164 Parent_Type : Entity_Id;
165 Derived_Type : Entity_Id);
166 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
167 -- an anonymous base type, and propagate constraint to subtype if needed.
169 procedure Build_Derived_Private_Type
171 Parent_Type : Entity_Id;
172 Derived_Type : Entity_Id;
173 Is_Completion : Boolean;
174 Derive_Subps : Boolean := True);
175 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
176 -- because the parent may or may not have a completion, and the derivation
177 -- may itself be a completion.
179 procedure Build_Derived_Record_Type
181 Parent_Type : Entity_Id;
182 Derived_Type : Entity_Id;
183 Derive_Subps : Boolean := True);
184 -- Subsidiary procedure used for tagged and untagged record types
185 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
186 -- All parameters are as in Build_Derived_Type except that N, in
187 -- addition to being an N_Full_Type_Declaration node, can also be an
188 -- N_Private_Extension_Declaration node. See the definition of this routine
189 -- for much more info. Derive_Subps indicates whether subprograms should be
190 -- derived from the parent type. The only case where Derive_Subps is False
191 -- is for an implicit derived full type for a type derived from a private
192 -- type (see Build_Derived_Type).
194 procedure Build_Discriminal (Discrim : Entity_Id);
195 -- Create the discriminal corresponding to discriminant Discrim, that is
196 -- the parameter corresponding to Discrim to be used in initialization
197 -- procedures for the type where Discrim is a discriminant. Discriminals
198 -- are not used during semantic analysis, and are not fully defined
199 -- entities until expansion. Thus they are not given a scope until
200 -- initialization procedures are built.
202 function Build_Discriminant_Constraints
205 Derived_Def : Boolean := False) return Elist_Id;
206 -- Validate discriminant constraints and return the list of the constraints
207 -- in order of discriminant declarations, where T is the discriminated
208 -- unconstrained type. Def is the N_Subtype_Indication node where the
209 -- discriminants constraints for T are specified. Derived_Def is True
210 -- when building the discriminant constraints in a derived type definition
211 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
212 -- type and Def is the constraint "(xxx)" on T and this routine sets the
213 -- Corresponding_Discriminant field of the discriminants in the derived
214 -- type D to point to the corresponding discriminants in the parent type T.
216 procedure Build_Discriminated_Subtype
220 Related_Nod : Node_Id;
221 For_Access : Boolean := False);
222 -- Subsidiary procedure to Constrain_Discriminated_Type and to
223 -- Process_Incomplete_Dependents. Given
225 -- T (a possibly discriminated base type)
226 -- Def_Id (a very partially built subtype for T),
228 -- the call completes Def_Id to be the appropriate E_*_Subtype.
230 -- The Elist is the list of discriminant constraints if any (it is set
231 -- to No_Elist if T is not a discriminated type, and to an empty list if
232 -- T has discriminants but there are no discriminant constraints). The
233 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
234 -- The For_Access says whether or not this subtype is really constraining
235 -- an access type. That is its sole purpose is the designated type of an
236 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
237 -- is built to avoid freezing T when the access subtype is frozen.
239 function Build_Scalar_Bound
242 Der_T : Entity_Id) return Node_Id;
243 -- The bounds of a derived scalar type are conversions of the bounds of
244 -- the parent type. Optimize the representation if the bounds are literals.
245 -- Needs a more complete spec--what are the parameters exactly, and what
246 -- exactly is the returned value, and how is Bound affected???
248 procedure Build_Underlying_Full_View
252 -- If the completion of a private type is itself derived from a private
253 -- type, or if the full view of a private subtype is itself private, the
254 -- back-end has no way to compute the actual size of this type. We build
255 -- an internal subtype declaration of the proper parent type to convey
256 -- this information. This extra mechanism is needed because a full
257 -- view cannot itself have a full view (it would get clobbered during
260 procedure Check_Access_Discriminant_Requires_Limited
263 -- Check the restriction that the type to which an access discriminant
264 -- belongs must be a concurrent type or a descendant of a type with
265 -- the reserved word 'limited' in its declaration.
267 procedure Check_Anonymous_Access_Components
271 Comp_List : Node_Id);
272 -- Ada 2005 AI-382: an access component in a record definition can refer to
273 -- the enclosing record, in which case it denotes the type itself, and not
274 -- the current instance of the type. We create an anonymous access type for
275 -- the component, and flag it as an access to a component, so accessibility
276 -- checks are properly performed on it. The declaration of the access type
277 -- is placed ahead of that of the record to prevent order-of-elaboration
278 -- circularity issues in Gigi. We create an incomplete type for the record
279 -- declaration, which is the designated type of the anonymous access.
281 procedure Check_Delta_Expression (E : Node_Id);
282 -- Check that the expression represented by E is suitable for use as a
283 -- delta expression, i.e. it is of real type and is static.
285 procedure Check_Digits_Expression (E : Node_Id);
286 -- Check that the expression represented by E is suitable for use as a
287 -- digits expression, i.e. it is of integer type, positive and static.
289 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
290 -- Validate the initialization of an object declaration. T is the required
291 -- type, and Exp is the initialization expression.
293 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
294 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
296 procedure Check_Or_Process_Discriminants
299 Prev : Entity_Id := Empty);
300 -- If N is the full declaration of the completion T of an incomplete or
301 -- private type, check its discriminants (which are already known to be
302 -- conformant with those of the partial view, see Find_Type_Name),
303 -- otherwise process them. Prev is the entity of the partial declaration,
306 procedure Check_Real_Bound (Bound : Node_Id);
307 -- Check given bound for being of real type and static. If not, post an
308 -- appropriate message, and rewrite the bound with the real literal zero.
310 procedure Constant_Redeclaration
314 -- Various checks on legality of full declaration of deferred constant.
315 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
316 -- node. The caller has not yet set any attributes of this entity.
318 function Contain_Interface
320 Ifaces : Elist_Id) return Boolean;
321 -- Ada 2005: Determine whether Iface is present in the list Ifaces
323 procedure Convert_Scalar_Bounds
325 Parent_Type : Entity_Id;
326 Derived_Type : Entity_Id;
328 -- For derived scalar types, convert the bounds in the type definition to
329 -- the derived type, and complete their analysis. Given a constraint of the
330 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
331 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
332 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
333 -- subtype are conversions of those bounds to the derived_type, so that
334 -- their typing is consistent.
336 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
337 -- Copies attributes from array base type T2 to array base type T1. Copies
338 -- only attributes that apply to base types, but not subtypes.
340 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
341 -- Copies attributes from array subtype T2 to array subtype T1. Copies
342 -- attributes that apply to both subtypes and base types.
344 procedure Create_Constrained_Components
348 Constraints : Elist_Id);
349 -- Build the list of entities for a constrained discriminated record
350 -- subtype. If a component depends on a discriminant, replace its subtype
351 -- using the discriminant values in the discriminant constraint. Subt
352 -- is the defining identifier for the subtype whose list of constrained
353 -- entities we will create. Decl_Node is the type declaration node where
354 -- we will attach all the itypes created. Typ is the base discriminated
355 -- type for the subtype Subt. Constraints is the list of discriminant
356 -- constraints for Typ.
358 function Constrain_Component_Type
360 Constrained_Typ : Entity_Id;
361 Related_Node : Node_Id;
363 Constraints : Elist_Id) return Entity_Id;
364 -- Given a discriminated base type Typ, a list of discriminant constraints,
365 -- Constraints, for Typ and a component Comp of Typ, create and return the
366 -- type corresponding to Etype (Comp) where all discriminant references
367 -- are replaced with the corresponding constraint. If Etype (Comp) contains
368 -- no discriminant references then it is returned as-is. Constrained_Typ
369 -- is the final constrained subtype to which the constrained component
370 -- belongs. Related_Node is the node where we attach all created itypes.
372 procedure Constrain_Access
373 (Def_Id : in out Entity_Id;
375 Related_Nod : Node_Id);
376 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
377 -- an anonymous type created for a subtype indication. In that case it is
378 -- created in the procedure and attached to Related_Nod.
380 procedure Constrain_Array
381 (Def_Id : in out Entity_Id;
383 Related_Nod : Node_Id;
384 Related_Id : Entity_Id;
386 -- Apply a list of index constraints to an unconstrained array type. The
387 -- first parameter is the entity for the resulting subtype. A value of
388 -- Empty for Def_Id indicates that an implicit type must be created, but
389 -- creation is delayed (and must be done by this procedure) because other
390 -- subsidiary implicit types must be created first (which is why Def_Id
391 -- is an in/out parameter). The second parameter is a subtype indication
392 -- node for the constrained array to be created (e.g. something of the
393 -- form string (1 .. 10)). Related_Nod gives the place where this type
394 -- has to be inserted in the tree. The Related_Id and Suffix parameters
395 -- are used to build the associated Implicit type name.
397 procedure Constrain_Concurrent
398 (Def_Id : in out Entity_Id;
400 Related_Nod : Node_Id;
401 Related_Id : Entity_Id;
403 -- Apply list of discriminant constraints to an unconstrained concurrent
406 -- SI is the N_Subtype_Indication node containing the constraint and
407 -- the unconstrained type to constrain.
409 -- Def_Id is the entity for the resulting constrained subtype. A value
410 -- of Empty for Def_Id indicates that an implicit type must be created,
411 -- but creation is delayed (and must be done by this procedure) because
412 -- other subsidiary implicit types must be created first (which is why
413 -- Def_Id is an in/out parameter).
415 -- Related_Nod gives the place where this type has to be inserted
418 -- The last two arguments are used to create its external name if needed.
420 function Constrain_Corresponding_Record
421 (Prot_Subt : Entity_Id;
422 Corr_Rec : Entity_Id;
423 Related_Nod : Node_Id) return Entity_Id;
424 -- When constraining a protected type or task type with discriminants,
425 -- constrain the corresponding record with the same discriminant values.
427 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
428 -- Constrain a decimal fixed point type with a digits constraint and/or a
429 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
431 procedure Constrain_Discriminated_Type
434 Related_Nod : Node_Id;
435 For_Access : Boolean := False);
436 -- Process discriminant constraints of composite type. Verify that values
437 -- have been provided for all discriminants, that the original type is
438 -- unconstrained, and that the types of the supplied expressions match
439 -- the discriminant types. The first three parameters are like in routine
440 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
443 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
444 -- Constrain an enumeration type with a range constraint. This is identical
445 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
447 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
448 -- Constrain a floating point type with either a digits constraint
449 -- and/or a range constraint, building a E_Floating_Point_Subtype.
451 procedure Constrain_Index
454 Related_Nod : Node_Id;
455 Related_Id : Entity_Id;
458 -- Process an index constraint S in a constrained array declaration. The
459 -- constraint can be a subtype name, or a range with or without an explicit
460 -- subtype mark. The index is the corresponding index of the unconstrained
461 -- array. The Related_Id and Suffix parameters are used to build the
462 -- associated Implicit type name.
464 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
465 -- Build subtype of a signed or modular integer type
467 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
468 -- Constrain an ordinary fixed point type with a range constraint, and
469 -- build an E_Ordinary_Fixed_Point_Subtype entity.
471 procedure Copy_And_Swap (Priv, Full : Entity_Id);
472 -- Copy the Priv entity into the entity of its full declaration then swap
473 -- the two entities in such a manner that the former private type is now
474 -- seen as a full type.
476 procedure Decimal_Fixed_Point_Type_Declaration
479 -- Create a new decimal fixed point type, and apply the constraint to
480 -- obtain a subtype of this new type.
482 procedure Complete_Private_Subtype
485 Full_Base : Entity_Id;
486 Related_Nod : Node_Id);
487 -- Complete the implicit full view of a private subtype by setting the
488 -- appropriate semantic fields. If the full view of the parent is a record
489 -- type, build constrained components of subtype.
491 procedure Derive_Progenitor_Subprograms
492 (Parent_Type : Entity_Id;
493 Tagged_Type : Entity_Id);
494 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
495 -- operations of progenitors of Tagged_Type, and replace the subsidiary
496 -- subtypes with Tagged_Type, to build the specs of the inherited interface
497 -- primitives. The derived primitives are aliased to those of the
498 -- interface. This routine takes care also of transferring to the full view
499 -- subprograms associated with the partial view of Tagged_Type that cover
500 -- interface primitives.
502 procedure Derived_Standard_Character
504 Parent_Type : Entity_Id;
505 Derived_Type : Entity_Id);
506 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
507 -- derivations from types Standard.Character and Standard.Wide_Character.
509 procedure Derived_Type_Declaration
512 Is_Completion : Boolean);
513 -- Process a derived type declaration. Build_Derived_Type is invoked
514 -- to process the actual derived type definition. Parameters N and
515 -- Is_Completion have the same meaning as in Build_Derived_Type.
516 -- T is the N_Defining_Identifier for the entity defined in the
517 -- N_Full_Type_Declaration node N, that is T is the derived type.
519 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
520 -- Insert each literal in symbol table, as an overloadable identifier. Each
521 -- enumeration type is mapped into a sequence of integers, and each literal
522 -- is defined as a constant with integer value. If any of the literals are
523 -- character literals, the type is a character type, which means that
524 -- strings are legal aggregates for arrays of components of the type.
526 function Expand_To_Stored_Constraint
528 Constraint : Elist_Id) return Elist_Id;
529 -- Given a constraint (i.e. a list of expressions) on the discriminants of
530 -- Typ, expand it into a constraint on the stored discriminants and return
531 -- the new list of expressions constraining the stored discriminants.
533 function Find_Type_Of_Object
535 Related_Nod : Node_Id) return Entity_Id;
536 -- Get type entity for object referenced by Obj_Def, attaching the implicit
537 -- types generated to Related_Nod.
539 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
540 -- Create a new float and apply the constraint to obtain subtype of it
542 function Has_Range_Constraint (N : Node_Id) return Boolean;
543 -- Given an N_Subtype_Indication node N, return True if a range constraint
544 -- is present, either directly, or as part of a digits or delta constraint.
545 -- In addition, a digits constraint in the decimal case returns True, since
546 -- it establishes a default range if no explicit range is present.
548 function Inherit_Components
550 Parent_Base : Entity_Id;
551 Derived_Base : Entity_Id;
553 Inherit_Discr : Boolean;
554 Discs : Elist_Id) return Elist_Id;
555 -- Called from Build_Derived_Record_Type to inherit the components of
556 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
557 -- For more information on derived types and component inheritance please
558 -- consult the comment above the body of Build_Derived_Record_Type.
560 -- N is the original derived type declaration
562 -- Is_Tagged is set if we are dealing with tagged types
564 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
565 -- Parent_Base, otherwise no discriminants are inherited.
567 -- Discs gives the list of constraints that apply to Parent_Base in the
568 -- derived type declaration. If Discs is set to No_Elist, then we have
569 -- the following situation:
571 -- type Parent (D1..Dn : ..) is [tagged] record ...;
572 -- type Derived is new Parent [with ...];
574 -- which gets treated as
576 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
578 -- For untagged types the returned value is an association list. The list
579 -- starts from the association (Parent_Base => Derived_Base), and then it
580 -- contains a sequence of the associations of the form
582 -- (Old_Component => New_Component),
584 -- where Old_Component is the Entity_Id of a component in Parent_Base and
585 -- New_Component is the Entity_Id of the corresponding component in
586 -- Derived_Base. For untagged records, this association list is needed when
587 -- copying the record declaration for the derived base. In the tagged case
588 -- the value returned is irrelevant.
590 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
591 -- Propagate static and dynamic predicate flags from a parent to the
592 -- subtype in a subtype declaration with and without constraints.
594 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
595 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
596 -- Determine whether subprogram Subp is a procedure subject to pragma
597 -- Extensions_Visible with value False and has at least one controlling
598 -- parameter of mode OUT.
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 Prepare_Private_Subtype_Completion
621 Related_Nod : Node_Id);
622 -- Id is a subtype of some private type. Creates the full declaration
623 -- associated with Id whenever possible, i.e. when the full declaration
624 -- of the base type is already known. Records each subtype into
625 -- Private_Dependents of the base type.
627 procedure Process_Incomplete_Dependents
631 -- Process all entities that depend on an incomplete type. There include
632 -- subtypes, subprogram types that mention the incomplete type in their
633 -- profiles, and subprogram with access parameters that designate the
636 -- Inc_T is the defining identifier of an incomplete type declaration, its
637 -- Ekind is E_Incomplete_Type.
639 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
641 -- Full_T is N's defining identifier.
643 -- Subtypes of incomplete types with discriminants are completed when the
644 -- parent type is. This is simpler than private subtypes, because they can
645 -- only appear in the same scope, and there is no need to exchange views.
646 -- Similarly, access_to_subprogram types may have a parameter or a return
647 -- type that is an incomplete type, and that must be replaced with the
650 -- If the full type is tagged, subprogram with access parameters that
651 -- designated the incomplete may be primitive operations of the full type,
652 -- and have to be processed accordingly.
654 procedure Process_Real_Range_Specification (Def : Node_Id);
655 -- Given the type definition for a real type, this procedure processes and
656 -- checks the real range specification of this type definition if one is
657 -- present. If errors are found, error messages are posted, and the
658 -- Real_Range_Specification of Def is reset to Empty.
660 procedure Propagate_Default_Init_Cond_Attributes
661 (From_Typ : Entity_Id;
663 Parent_To_Derivation : Boolean := False;
664 Private_To_Full_View : Boolean := False);
665 -- Subsidiary to routines Build_Derived_Type and Process_Full_View. Inherit
666 -- all attributes related to pragma Default_Initial_Condition from From_Typ
667 -- to To_Typ. Flag Parent_To_Derivation should be set when the context is
668 -- the creation of a derived type. Flag Private_To_Full_View should be set
669 -- when processing both views of a private type.
671 procedure Record_Type_Declaration
675 -- Process a record type declaration (for both untagged and tagged
676 -- records). Parameters T and N are exactly like in procedure
677 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
678 -- for this routine. If this is the completion of an incomplete type
679 -- declaration, Prev is the entity of the incomplete declaration, used for
680 -- cross-referencing. Otherwise Prev = T.
682 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
683 -- This routine is used to process the actual record type definition (both
684 -- for untagged and tagged records). Def is a record type definition node.
685 -- This procedure analyzes the components in this record type definition.
686 -- Prev_T is the entity for the enclosing record type. It is provided so
687 -- that its Has_Task flag can be set if any of the component have Has_Task
688 -- set. If the declaration is the completion of an incomplete type
689 -- declaration, Prev_T is the original incomplete type, whose full view is
692 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
693 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
694 -- build a copy of the declaration tree of the parent, and we create
695 -- independently the list of components for the derived type. Semantic
696 -- information uses the component entities, but record representation
697 -- clauses are validated on the declaration tree. This procedure replaces
698 -- discriminants and components in the declaration with those that have
699 -- been created by Inherit_Components.
701 procedure Set_Fixed_Range
706 -- Build a range node with the given bounds and set it as the Scalar_Range
707 -- of the given fixed-point type entity. Loc is the source location used
708 -- for the constructed range. See body for further details.
710 procedure Set_Scalar_Range_For_Subtype
714 -- This routine is used to set the scalar range field for a subtype given
715 -- Def_Id, the entity for the subtype, and R, the range expression for the
716 -- scalar range. Subt provides the parent subtype to be used to analyze,
717 -- resolve, and check the given range.
719 procedure Set_Default_SSO (T : Entity_Id);
720 -- T is the entity for an array or record being declared. This procedure
721 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
722 -- to the setting of Opt.Default_SSO.
724 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
725 -- Create a new signed integer entity, and apply the constraint to obtain
726 -- the required first named subtype of this type.
728 procedure Set_Stored_Constraint_From_Discriminant_Constraint
730 -- E is some record type. This routine computes E's Stored_Constraint
731 -- from its Discriminant_Constraint.
733 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
734 -- Check that an entity in a list of progenitors is an interface,
735 -- emit error otherwise.
737 -----------------------
738 -- Access_Definition --
739 -----------------------
741 function Access_Definition
742 (Related_Nod : Node_Id;
743 N : Node_Id) return Entity_Id
745 Anon_Type : Entity_Id;
746 Anon_Scope : Entity_Id;
747 Desig_Type : Entity_Id;
748 Enclosing_Prot_Type : Entity_Id := Empty;
751 Check_SPARK_05_Restriction ("access type is not allowed", N);
753 if Is_Entry (Current_Scope)
754 and then Is_Task_Type (Etype (Scope (Current_Scope)))
756 Error_Msg_N ("task entries cannot have access parameters", N);
760 -- Ada 2005: For an object declaration the corresponding anonymous
761 -- type is declared in the current scope.
763 -- If the access definition is the return type of another access to
764 -- function, scope is the current one, because it is the one of the
765 -- current type declaration, except for the pathological case below.
767 if Nkind_In (Related_Nod, N_Object_Declaration,
768 N_Access_Function_Definition)
770 Anon_Scope := Current_Scope;
772 -- A pathological case: function returning access functions that
773 -- return access functions, etc. Each anonymous access type created
774 -- is in the enclosing scope of the outermost function.
781 while Nkind_In (Par, N_Access_Function_Definition,
787 if Nkind (Par) = N_Function_Specification then
788 Anon_Scope := Scope (Defining_Entity (Par));
792 -- For the anonymous function result case, retrieve the scope of the
793 -- function specification's associated entity rather than using the
794 -- current scope. The current scope will be the function itself if the
795 -- formal part is currently being analyzed, but will be the parent scope
796 -- in the case of a parameterless function, and we always want to use
797 -- the function's parent scope. Finally, if the function is a child
798 -- unit, we must traverse the tree to retrieve the proper entity.
800 elsif Nkind (Related_Nod) = N_Function_Specification
801 and then Nkind (Parent (N)) /= N_Parameter_Specification
803 -- If the current scope is a protected type, the anonymous access
804 -- is associated with one of the protected operations, and must
805 -- be available in the scope that encloses the protected declaration.
806 -- Otherwise the type is in the scope enclosing the subprogram.
808 -- If the function has formals, The return type of a subprogram
809 -- declaration is analyzed in the scope of the subprogram (see
810 -- Process_Formals) and thus the protected type, if present, is
811 -- the scope of the current function scope.
813 if Ekind (Current_Scope) = E_Protected_Type then
814 Enclosing_Prot_Type := Current_Scope;
816 elsif Ekind (Current_Scope) = E_Function
817 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
819 Enclosing_Prot_Type := Scope (Current_Scope);
822 if Present (Enclosing_Prot_Type) then
823 Anon_Scope := Scope (Enclosing_Prot_Type);
826 Anon_Scope := Scope (Defining_Entity (Related_Nod));
829 -- For an access type definition, if the current scope is a child
830 -- unit it is the scope of the type.
832 elsif Is_Compilation_Unit (Current_Scope) then
833 Anon_Scope := Current_Scope;
835 -- For access formals, access components, and access discriminants, the
836 -- scope is that of the enclosing declaration,
839 Anon_Scope := Scope (Current_Scope);
844 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
847 and then Ada_Version >= Ada_2005
849 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
852 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
853 -- the corresponding semantic routine
855 if Present (Access_To_Subprogram_Definition (N)) then
857 -- Compiler runtime units are compiled in Ada 2005 mode when building
858 -- the runtime library but must also be compilable in Ada 95 mode
859 -- (when bootstrapping the compiler).
861 Check_Compiler_Unit ("anonymous access to subprogram", N);
863 Access_Subprogram_Declaration
864 (T_Name => Anon_Type,
865 T_Def => Access_To_Subprogram_Definition (N));
867 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
869 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
871 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
874 Set_Can_Use_Internal_Rep
875 (Anon_Type, not Always_Compatible_Rep_On_Target);
877 -- If the anonymous access is associated with a protected operation,
878 -- create a reference to it after the enclosing protected definition
879 -- because the itype will be used in the subsequent bodies.
881 -- If the anonymous access itself is protected, a full type
882 -- declaratiton will be created for it, so that the equivalent
883 -- record type can be constructed. For further details, see
884 -- Replace_Anonymous_Access_To_Protected-Subprogram.
886 if Ekind (Current_Scope) = E_Protected_Type
887 and then not Protected_Present (Access_To_Subprogram_Definition (N))
889 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
895 Find_Type (Subtype_Mark (N));
896 Desig_Type := Entity (Subtype_Mark (N));
898 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
899 Set_Etype (Anon_Type, Anon_Type);
901 -- Make sure the anonymous access type has size and alignment fields
902 -- set, as required by gigi. This is necessary in the case of the
903 -- Task_Body_Procedure.
905 if not Has_Private_Component (Desig_Type) then
906 Layout_Type (Anon_Type);
909 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
910 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
911 -- the null value is allowed. In Ada 95 the null value is never allowed.
913 if Ada_Version >= Ada_2005 then
914 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
916 Set_Can_Never_Be_Null (Anon_Type, True);
919 -- The anonymous access type is as public as the discriminated type or
920 -- subprogram that defines it. It is imported (for back-end purposes)
921 -- if the designated type is.
923 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
925 -- Ada 2005 (AI-231): Propagate the access-constant attribute
927 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
929 -- The context is either a subprogram declaration, object declaration,
930 -- or an access discriminant, in a private or a full type declaration.
931 -- In the case of a subprogram, if the designated type is incomplete,
932 -- the operation will be a primitive operation of the full type, to be
933 -- updated subsequently. If the type is imported through a limited_with
934 -- clause, the subprogram is not a primitive operation of the type
935 -- (which is declared elsewhere in some other scope).
937 if Ekind (Desig_Type) = E_Incomplete_Type
938 and then not From_Limited_With (Desig_Type)
939 and then Is_Overloadable (Current_Scope)
941 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
942 Set_Has_Delayed_Freeze (Current_Scope);
945 -- Ada 2005: If the designated type is an interface that may contain
946 -- tasks, create a Master entity for the declaration. This must be done
947 -- before expansion of the full declaration, because the declaration may
948 -- include an expression that is an allocator, whose expansion needs the
949 -- proper Master for the created tasks.
951 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
953 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
955 Build_Class_Wide_Master (Anon_Type);
957 -- Similarly, if the type is an anonymous access that designates
958 -- tasks, create a master entity for it in the current context.
960 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
962 Build_Master_Entity (Defining_Identifier (Related_Nod));
963 Build_Master_Renaming (Anon_Type);
967 -- For a private component of a protected type, it is imperative that
968 -- the back-end elaborate the type immediately after the protected
969 -- declaration, because this type will be used in the declarations
970 -- created for the component within each protected body, so we must
971 -- create an itype reference for it now.
973 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
974 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
976 -- Similarly, if the access definition is the return result of a
977 -- function, create an itype reference for it because it will be used
978 -- within the function body. For a regular function that is not a
979 -- compilation unit, insert reference after the declaration. For a
980 -- protected operation, insert it after the enclosing protected type
981 -- declaration. In either case, do not create a reference for a type
982 -- obtained through a limited_with clause, because this would introduce
983 -- semantic dependencies.
985 -- Similarly, do not create a reference if the designated type is a
986 -- generic formal, because no use of it will reach the backend.
988 elsif Nkind (Related_Nod) = N_Function_Specification
989 and then not From_Limited_With (Desig_Type)
990 and then not Is_Generic_Type (Desig_Type)
992 if Present (Enclosing_Prot_Type) then
993 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
995 elsif Is_List_Member (Parent (Related_Nod))
996 and then Nkind (Parent (N)) /= N_Parameter_Specification
998 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
1001 -- Finally, create an itype reference for an object declaration of an
1002 -- anonymous access type. This is strictly necessary only for deferred
1003 -- constants, but in any case will avoid out-of-scope problems in the
1006 elsif Nkind (Related_Nod) = N_Object_Declaration then
1007 Build_Itype_Reference (Anon_Type, Related_Nod);
1011 end Access_Definition;
1013 -----------------------------------
1014 -- Access_Subprogram_Declaration --
1015 -----------------------------------
1017 procedure Access_Subprogram_Declaration
1018 (T_Name : Entity_Id;
1021 procedure Check_For_Premature_Usage (Def : Node_Id);
1022 -- Check that type T_Name is not used, directly or recursively, as a
1023 -- parameter or a return type in Def. Def is either a subtype, an
1024 -- access_definition, or an access_to_subprogram_definition.
1026 -------------------------------
1027 -- Check_For_Premature_Usage --
1028 -------------------------------
1030 procedure Check_For_Premature_Usage (Def : Node_Id) is
1034 -- Check for a subtype mark
1036 if Nkind (Def) in N_Has_Etype then
1037 if Etype (Def) = T_Name then
1039 ("type& cannot be used before end of its declaration", Def);
1042 -- If this is not a subtype, then this is an access_definition
1044 elsif Nkind (Def) = N_Access_Definition then
1045 if Present (Access_To_Subprogram_Definition (Def)) then
1046 Check_For_Premature_Usage
1047 (Access_To_Subprogram_Definition (Def));
1049 Check_For_Premature_Usage (Subtype_Mark (Def));
1052 -- The only cases left are N_Access_Function_Definition and
1053 -- N_Access_Procedure_Definition.
1056 if Present (Parameter_Specifications (Def)) then
1057 Param := First (Parameter_Specifications (Def));
1058 while Present (Param) loop
1059 Check_For_Premature_Usage (Parameter_Type (Param));
1060 Param := Next (Param);
1064 if Nkind (Def) = N_Access_Function_Definition then
1065 Check_For_Premature_Usage (Result_Definition (Def));
1068 end Check_For_Premature_Usage;
1072 Formals : constant List_Id := Parameter_Specifications (T_Def);
1075 Desig_Type : constant Entity_Id :=
1076 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1078 -- Start of processing for Access_Subprogram_Declaration
1081 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1083 -- Associate the Itype node with the inner full-type declaration or
1084 -- subprogram spec or entry body. This is required to handle nested
1085 -- anonymous declarations. For example:
1088 -- (X : access procedure
1089 -- (Y : access procedure
1092 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1093 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1094 N_Private_Type_Declaration,
1095 N_Private_Extension_Declaration,
1096 N_Procedure_Specification,
1097 N_Function_Specification,
1101 Nkind_In (D_Ityp, N_Object_Declaration,
1102 N_Object_Renaming_Declaration,
1103 N_Formal_Object_Declaration,
1104 N_Formal_Type_Declaration,
1105 N_Task_Type_Declaration,
1106 N_Protected_Type_Declaration))
1108 D_Ityp := Parent (D_Ityp);
1109 pragma Assert (D_Ityp /= Empty);
1112 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1114 if Nkind_In (D_Ityp, N_Procedure_Specification,
1115 N_Function_Specification)
1117 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1119 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1120 N_Object_Declaration,
1121 N_Object_Renaming_Declaration,
1122 N_Formal_Type_Declaration)
1124 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1127 if Nkind (T_Def) = N_Access_Function_Definition then
1128 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1130 Acc : constant Node_Id := Result_Definition (T_Def);
1133 if Present (Access_To_Subprogram_Definition (Acc))
1135 Protected_Present (Access_To_Subprogram_Definition (Acc))
1139 Replace_Anonymous_Access_To_Protected_Subprogram
1145 Access_Definition (T_Def, Result_Definition (T_Def)));
1150 Analyze (Result_Definition (T_Def));
1153 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1156 -- If a null exclusion is imposed on the result type, then
1157 -- create a null-excluding itype (an access subtype) and use
1158 -- it as the function's Etype.
1160 if Is_Access_Type (Typ)
1161 and then Null_Exclusion_In_Return_Present (T_Def)
1163 Set_Etype (Desig_Type,
1164 Create_Null_Excluding_Itype
1166 Related_Nod => T_Def,
1167 Scope_Id => Current_Scope));
1170 if From_Limited_With (Typ) then
1172 -- AI05-151: Incomplete types are allowed in all basic
1173 -- declarations, including access to subprograms.
1175 if Ada_Version >= Ada_2012 then
1180 ("illegal use of incomplete type&",
1181 Result_Definition (T_Def), Typ);
1184 elsif Ekind (Current_Scope) = E_Package
1185 and then In_Private_Part (Current_Scope)
1187 if Ekind (Typ) = E_Incomplete_Type then
1188 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1190 elsif Is_Class_Wide_Type (Typ)
1191 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1194 (Desig_Type, Private_Dependents (Etype (Typ)));
1198 Set_Etype (Desig_Type, Typ);
1203 if not (Is_Type (Etype (Desig_Type))) then
1205 ("expect type in function specification",
1206 Result_Definition (T_Def));
1210 Set_Etype (Desig_Type, Standard_Void_Type);
1213 if Present (Formals) then
1214 Push_Scope (Desig_Type);
1216 -- Some special tests here. These special tests can be removed
1217 -- if and when Itypes always have proper parent pointers to their
1220 -- Special test 1) Link defining_identifier of formals. Required by
1221 -- First_Formal to provide its functionality.
1227 F := First (Formals);
1229 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1230 -- when it is part of an unconstrained type and subtype expansion
1231 -- is disabled. To avoid back-end problems with shared profiles,
1232 -- use previous subprogram type as the designated type, and then
1233 -- remove scope added above.
1235 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1237 Set_Etype (T_Name, T_Name);
1238 Init_Size_Align (T_Name);
1239 Set_Directly_Designated_Type (T_Name,
1240 Scope (Defining_Identifier (F)));
1245 while Present (F) loop
1246 if No (Parent (Defining_Identifier (F))) then
1247 Set_Parent (Defining_Identifier (F), F);
1254 Process_Formals (Formals, Parent (T_Def));
1256 -- Special test 2) End_Scope requires that the parent pointer be set
1257 -- to something reasonable, but Itypes don't have parent pointers. So
1258 -- we set it and then unset it ???
1260 Set_Parent (Desig_Type, T_Name);
1262 Set_Parent (Desig_Type, Empty);
1265 -- Check for premature usage of the type being defined
1267 Check_For_Premature_Usage (T_Def);
1269 -- The return type and/or any parameter type may be incomplete. Mark the
1270 -- subprogram_type as depending on the incomplete type, so that it can
1271 -- be updated when the full type declaration is seen. This only applies
1272 -- to incomplete types declared in some enclosing scope, not to limited
1273 -- views from other packages.
1275 -- Prior to Ada 2012, access to functions can only have in_parameters.
1277 if Present (Formals) then
1278 Formal := First_Formal (Desig_Type);
1279 while Present (Formal) loop
1280 if Ekind (Formal) /= E_In_Parameter
1281 and then Nkind (T_Def) = N_Access_Function_Definition
1282 and then Ada_Version < Ada_2012
1284 Error_Msg_N ("functions can only have IN parameters", Formal);
1287 if Ekind (Etype (Formal)) = E_Incomplete_Type
1288 and then In_Open_Scopes (Scope (Etype (Formal)))
1290 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1291 Set_Has_Delayed_Freeze (Desig_Type);
1294 Next_Formal (Formal);
1298 -- Check whether an indirect call without actuals may be possible. This
1299 -- is used when resolving calls whose result is then indexed.
1301 May_Need_Actuals (Desig_Type);
1303 -- If the return type is incomplete, this is legal as long as the type
1304 -- is declared in the current scope and will be completed in it (rather
1305 -- than being part of limited view).
1307 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1308 and then not Has_Delayed_Freeze (Desig_Type)
1309 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1311 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1312 Set_Has_Delayed_Freeze (Desig_Type);
1315 Check_Delayed_Subprogram (Desig_Type);
1317 if Protected_Present (T_Def) then
1318 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1319 Set_Convention (Desig_Type, Convention_Protected);
1321 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1324 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1326 Set_Etype (T_Name, T_Name);
1327 Init_Size_Align (T_Name);
1328 Set_Directly_Designated_Type (T_Name, Desig_Type);
1330 Generate_Reference_To_Formals (T_Name);
1332 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1334 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1336 Check_Restriction (No_Access_Subprograms, T_Def);
1337 end Access_Subprogram_Declaration;
1339 ----------------------------
1340 -- Access_Type_Declaration --
1341 ----------------------------
1343 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1344 P : constant Node_Id := Parent (Def);
1345 S : constant Node_Id := Subtype_Indication (Def);
1347 Full_Desig : Entity_Id;
1350 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1352 -- Check for permissible use of incomplete type
1354 if Nkind (S) /= N_Subtype_Indication then
1357 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1358 Set_Directly_Designated_Type (T, Entity (S));
1360 -- If the designated type is a limited view, we cannot tell if
1361 -- the full view contains tasks, and there is no way to handle
1362 -- that full view in a client. We create a master entity for the
1363 -- scope, which will be used when a client determines that one
1366 if From_Limited_With (Entity (S))
1367 and then not Is_Class_Wide_Type (Entity (S))
1369 Set_Ekind (T, E_Access_Type);
1370 Build_Master_Entity (T);
1371 Build_Master_Renaming (T);
1375 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1378 -- If the access definition is of the form: ACCESS NOT NULL ..
1379 -- the subtype indication must be of an access type. Create
1380 -- a null-excluding subtype of it.
1382 if Null_Excluding_Subtype (Def) then
1383 if not Is_Access_Type (Entity (S)) then
1384 Error_Msg_N ("null exclusion must apply to access type", Def);
1388 Loc : constant Source_Ptr := Sloc (S);
1390 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1394 Make_Subtype_Declaration (Loc,
1395 Defining_Identifier => Nam,
1396 Subtype_Indication =>
1397 New_Occurrence_Of (Entity (S), Loc));
1398 Set_Null_Exclusion_Present (Decl);
1399 Insert_Before (Parent (Def), Decl);
1401 Set_Entity (S, Nam);
1407 Set_Directly_Designated_Type (T,
1408 Process_Subtype (S, P, T, 'P'));
1411 if All_Present (Def) or Constant_Present (Def) then
1412 Set_Ekind (T, E_General_Access_Type);
1414 Set_Ekind (T, E_Access_Type);
1417 Full_Desig := Designated_Type (T);
1419 if Base_Type (Full_Desig) = T then
1420 Error_Msg_N ("access type cannot designate itself", S);
1422 -- In Ada 2005, the type may have a limited view through some unit in
1423 -- its own context, allowing the following circularity that cannot be
1424 -- detected earlier.
1426 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1429 ("access type cannot designate its own classwide type", S);
1431 -- Clean up indication of tagged status to prevent cascaded errors
1433 Set_Is_Tagged_Type (T, False);
1438 -- If the type has appeared already in a with_type clause, it is frozen
1439 -- and the pointer size is already set. Else, initialize.
1441 if not From_Limited_With (T) then
1442 Init_Size_Align (T);
1445 -- Note that Has_Task is always false, since the access type itself
1446 -- is not a task type. See Einfo for more description on this point.
1447 -- Exactly the same consideration applies to Has_Controlled_Component
1448 -- and to Has_Protected.
1450 Set_Has_Task (T, False);
1451 Set_Has_Controlled_Component (T, False);
1452 Set_Has_Protected (T, False);
1454 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1455 -- problems where an incomplete view of this entity has been previously
1456 -- established by a limited with and an overlaid version of this field
1457 -- (Stored_Constraint) was initialized for the incomplete view.
1459 -- This reset is performed in most cases except where the access type
1460 -- has been created for the purposes of allocating or deallocating a
1461 -- build-in-place object. Such access types have explicitly set pools
1462 -- and finalization masters.
1464 if No (Associated_Storage_Pool (T)) then
1465 Set_Finalization_Master (T, Empty);
1468 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1471 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1472 Set_Is_Access_Constant (T, Constant_Present (Def));
1473 end Access_Type_Declaration;
1475 ----------------------------------
1476 -- Add_Interface_Tag_Components --
1477 ----------------------------------
1479 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1480 Loc : constant Source_Ptr := Sloc (N);
1484 procedure Add_Tag (Iface : Entity_Id);
1485 -- Add tag for one of the progenitor interfaces
1491 procedure Add_Tag (Iface : Entity_Id) is
1498 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1500 -- This is a reasonable place to propagate predicates
1502 if Has_Predicates (Iface) then
1503 Set_Has_Predicates (Typ);
1507 Make_Component_Definition (Loc,
1508 Aliased_Present => True,
1509 Subtype_Indication =>
1510 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1512 Tag := Make_Temporary (Loc, 'V');
1515 Make_Component_Declaration (Loc,
1516 Defining_Identifier => Tag,
1517 Component_Definition => Def);
1519 Analyze_Component_Declaration (Decl);
1521 Set_Analyzed (Decl);
1522 Set_Ekind (Tag, E_Component);
1524 Set_Is_Aliased (Tag);
1525 Set_Related_Type (Tag, Iface);
1526 Init_Component_Location (Tag);
1528 pragma Assert (Is_Frozen (Iface));
1530 Set_DT_Entry_Count (Tag,
1531 DT_Entry_Count (First_Entity (Iface)));
1533 if No (Last_Tag) then
1536 Insert_After (Last_Tag, Decl);
1541 -- If the ancestor has discriminants we need to give special support
1542 -- to store the offset_to_top value of the secondary dispatch tables.
1543 -- For this purpose we add a supplementary component just after the
1544 -- field that contains the tag associated with each secondary DT.
1546 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1548 Make_Component_Definition (Loc,
1549 Subtype_Indication =>
1550 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1552 Offset := Make_Temporary (Loc, 'V');
1555 Make_Component_Declaration (Loc,
1556 Defining_Identifier => Offset,
1557 Component_Definition => Def);
1559 Analyze_Component_Declaration (Decl);
1561 Set_Analyzed (Decl);
1562 Set_Ekind (Offset, E_Component);
1563 Set_Is_Aliased (Offset);
1564 Set_Related_Type (Offset, Iface);
1565 Init_Component_Location (Offset);
1566 Insert_After (Last_Tag, Decl);
1577 -- Start of processing for Add_Interface_Tag_Components
1580 if not RTE_Available (RE_Interface_Tag) then
1582 ("(Ada 2005) interface types not supported by this run-time!",
1587 if Ekind (Typ) /= E_Record_Type
1588 or else (Is_Concurrent_Record_Type (Typ)
1589 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1590 or else (not Is_Concurrent_Record_Type (Typ)
1591 and then No (Interfaces (Typ))
1592 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1597 -- Find the current last tag
1599 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1600 Ext := Record_Extension_Part (Type_Definition (N));
1602 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1603 Ext := Type_Definition (N);
1608 if not (Present (Component_List (Ext))) then
1609 Set_Null_Present (Ext, False);
1611 Set_Component_List (Ext,
1612 Make_Component_List (Loc,
1613 Component_Items => L,
1614 Null_Present => False));
1616 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1617 L := Component_Items
1619 (Record_Extension_Part
1620 (Type_Definition (N))));
1622 L := Component_Items
1624 (Type_Definition (N)));
1627 -- Find the last tag component
1630 while Present (Comp) loop
1631 if Nkind (Comp) = N_Component_Declaration
1632 and then Is_Tag (Defining_Identifier (Comp))
1641 -- At this point L references the list of components and Last_Tag
1642 -- references the current last tag (if any). Now we add the tag
1643 -- corresponding with all the interfaces that are not implemented
1646 if Present (Interfaces (Typ)) then
1647 Elmt := First_Elmt (Interfaces (Typ));
1648 while Present (Elmt) loop
1649 Add_Tag (Node (Elmt));
1653 end Add_Interface_Tag_Components;
1655 -------------------------------------
1656 -- Add_Internal_Interface_Entities --
1657 -------------------------------------
1659 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1662 Iface_Elmt : Elmt_Id;
1663 Iface_Prim : Entity_Id;
1664 Ifaces_List : Elist_Id;
1665 New_Subp : Entity_Id := Empty;
1667 Restore_Scope : Boolean := False;
1670 pragma Assert (Ada_Version >= Ada_2005
1671 and then Is_Record_Type (Tagged_Type)
1672 and then Is_Tagged_Type (Tagged_Type)
1673 and then Has_Interfaces (Tagged_Type)
1674 and then not Is_Interface (Tagged_Type));
1676 -- Ensure that the internal entities are added to the scope of the type
1678 if Scope (Tagged_Type) /= Current_Scope then
1679 Push_Scope (Scope (Tagged_Type));
1680 Restore_Scope := True;
1683 Collect_Interfaces (Tagged_Type, Ifaces_List);
1685 Iface_Elmt := First_Elmt (Ifaces_List);
1686 while Present (Iface_Elmt) loop
1687 Iface := Node (Iface_Elmt);
1689 -- Originally we excluded here from this processing interfaces that
1690 -- are parents of Tagged_Type because their primitives are located
1691 -- in the primary dispatch table (and hence no auxiliary internal
1692 -- entities are required to handle secondary dispatch tables in such
1693 -- case). However, these auxiliary entities are also required to
1694 -- handle derivations of interfaces in formals of generics (see
1695 -- Derive_Subprograms).
1697 Elmt := First_Elmt (Primitive_Operations (Iface));
1698 while Present (Elmt) loop
1699 Iface_Prim := Node (Elmt);
1701 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1703 Find_Primitive_Covering_Interface
1704 (Tagged_Type => Tagged_Type,
1705 Iface_Prim => Iface_Prim);
1707 if No (Prim) and then Serious_Errors_Detected > 0 then
1711 pragma Assert (Present (Prim));
1713 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1714 -- differs from the name of the interface primitive then it is
1715 -- a private primitive inherited from a parent type. In such
1716 -- case, given that Tagged_Type covers the interface, the
1717 -- inherited private primitive becomes visible. For such
1718 -- purpose we add a new entity that renames the inherited
1719 -- private primitive.
1721 if Chars (Prim) /= Chars (Iface_Prim) then
1722 pragma Assert (Has_Suffix (Prim, 'P'));
1724 (New_Subp => New_Subp,
1725 Parent_Subp => Iface_Prim,
1726 Derived_Type => Tagged_Type,
1727 Parent_Type => Iface);
1728 Set_Alias (New_Subp, Prim);
1729 Set_Is_Abstract_Subprogram
1730 (New_Subp, Is_Abstract_Subprogram (Prim));
1734 (New_Subp => New_Subp,
1735 Parent_Subp => Iface_Prim,
1736 Derived_Type => Tagged_Type,
1737 Parent_Type => Iface);
1739 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1740 -- associated with interface types. These entities are
1741 -- only registered in the list of primitives of its
1742 -- corresponding tagged type because they are only used
1743 -- to fill the contents of the secondary dispatch tables.
1744 -- Therefore they are removed from the homonym chains.
1746 Set_Is_Hidden (New_Subp);
1747 Set_Is_Internal (New_Subp);
1748 Set_Alias (New_Subp, Prim);
1749 Set_Is_Abstract_Subprogram
1750 (New_Subp, Is_Abstract_Subprogram (Prim));
1751 Set_Interface_Alias (New_Subp, Iface_Prim);
1753 -- If the returned type is an interface then propagate it to
1754 -- the returned type. Needed by the thunk to generate the code
1755 -- which displaces "this" to reference the corresponding
1756 -- secondary dispatch table in the returned object.
1758 if Is_Interface (Etype (Iface_Prim)) then
1759 Set_Etype (New_Subp, Etype (Iface_Prim));
1762 -- Internal entities associated with interface types are
1763 -- only registered in the list of primitives of the tagged
1764 -- type. They are only used to fill the contents of the
1765 -- secondary dispatch tables. Therefore they are not needed
1766 -- in the homonym chains.
1768 Remove_Homonym (New_Subp);
1770 -- Hidden entities associated with interfaces must have set
1771 -- the Has_Delay_Freeze attribute to ensure that, in case of
1772 -- locally defined tagged types (or compiling with static
1773 -- dispatch tables generation disabled) the corresponding
1774 -- entry of the secondary dispatch table is filled when
1775 -- such an entity is frozen.
1777 Set_Has_Delayed_Freeze (New_Subp);
1784 Next_Elmt (Iface_Elmt);
1787 if Restore_Scope then
1790 end Add_Internal_Interface_Entities;
1792 -----------------------------------
1793 -- Analyze_Component_Declaration --
1794 -----------------------------------
1796 procedure Analyze_Component_Declaration (N : Node_Id) is
1797 Loc : constant Source_Ptr := Sloc (N);
1798 Id : constant Entity_Id := Defining_Identifier (N);
1799 E : constant Node_Id := Expression (N);
1800 Typ : constant Node_Id :=
1801 Subtype_Indication (Component_Definition (N));
1805 function Contains_POC (Constr : Node_Id) return Boolean;
1806 -- Determines whether a constraint uses the discriminant of a record
1807 -- type thus becoming a per-object constraint (POC).
1809 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1810 -- Typ is the type of the current component, check whether this type is
1811 -- a limited type. Used to validate declaration against that of
1812 -- enclosing record.
1818 function Contains_POC (Constr : Node_Id) return Boolean is
1820 -- Prevent cascaded errors
1822 if Error_Posted (Constr) then
1826 case Nkind (Constr) is
1827 when N_Attribute_Reference =>
1828 return Attribute_Name (Constr) = Name_Access
1829 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1831 when N_Discriminant_Association =>
1832 return Denotes_Discriminant (Expression (Constr));
1834 when N_Identifier =>
1835 return Denotes_Discriminant (Constr);
1837 when N_Index_Or_Discriminant_Constraint =>
1842 IDC := First (Constraints (Constr));
1843 while Present (IDC) loop
1845 -- One per-object constraint is sufficient
1847 if Contains_POC (IDC) then
1858 return Denotes_Discriminant (Low_Bound (Constr))
1860 Denotes_Discriminant (High_Bound (Constr));
1862 when N_Range_Constraint =>
1863 return Denotes_Discriminant (Range_Expression (Constr));
1871 ----------------------
1872 -- Is_Known_Limited --
1873 ----------------------
1875 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1876 P : constant Entity_Id := Etype (Typ);
1877 R : constant Entity_Id := Root_Type (Typ);
1880 if Is_Limited_Record (Typ) then
1883 -- If the root type is limited (and not a limited interface)
1884 -- so is the current type
1886 elsif Is_Limited_Record (R)
1887 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1891 -- Else the type may have a limited interface progenitor, but a
1892 -- limited record parent.
1894 elsif R /= P and then Is_Limited_Record (P) then
1900 end Is_Known_Limited;
1902 -- Start of processing for Analyze_Component_Declaration
1905 Generate_Definition (Id);
1908 if Present (Typ) then
1909 T := Find_Type_Of_Object
1910 (Subtype_Indication (Component_Definition (N)), N);
1912 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1913 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1916 -- Ada 2005 (AI-230): Access Definition case
1919 pragma Assert (Present
1920 (Access_Definition (Component_Definition (N))));
1922 T := Access_Definition
1924 N => Access_Definition (Component_Definition (N)));
1925 Set_Is_Local_Anonymous_Access (T);
1927 -- Ada 2005 (AI-254)
1929 if Present (Access_To_Subprogram_Definition
1930 (Access_Definition (Component_Definition (N))))
1931 and then Protected_Present (Access_To_Subprogram_Definition
1933 (Component_Definition (N))))
1935 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1939 -- If the subtype is a constrained subtype of the enclosing record,
1940 -- (which must have a partial view) the back-end does not properly
1941 -- handle the recursion. Rewrite the component declaration with an
1942 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1943 -- the tree directly because side effects have already been removed from
1944 -- discriminant constraints.
1946 if Ekind (T) = E_Access_Subtype
1947 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1948 and then Comes_From_Source (T)
1949 and then Nkind (Parent (T)) = N_Subtype_Declaration
1950 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1953 (Subtype_Indication (Component_Definition (N)),
1954 New_Copy_Tree (Subtype_Indication (Parent (T))));
1955 T := Find_Type_Of_Object
1956 (Subtype_Indication (Component_Definition (N)), N);
1959 -- If the component declaration includes a default expression, then we
1960 -- check that the component is not of a limited type (RM 3.7(5)),
1961 -- and do the special preanalysis of the expression (see section on
1962 -- "Handling of Default and Per-Object Expressions" in the spec of
1966 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1967 Preanalyze_Default_Expression (E, T);
1968 Check_Initialization (T, E);
1970 if Ada_Version >= Ada_2005
1971 and then Ekind (T) = E_Anonymous_Access_Type
1972 and then Etype (E) /= Any_Type
1974 -- Check RM 3.9.2(9): "if the expected type for an expression is
1975 -- an anonymous access-to-specific tagged type, then the object
1976 -- designated by the expression shall not be dynamically tagged
1977 -- unless it is a controlling operand in a call on a dispatching
1980 if Is_Tagged_Type (Directly_Designated_Type (T))
1982 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1984 Ekind (Directly_Designated_Type (Etype (E))) =
1988 ("access to specific tagged type required (RM 3.9.2(9))", E);
1991 -- (Ada 2005: AI-230): Accessibility check for anonymous
1994 if Type_Access_Level (Etype (E)) >
1995 Deepest_Type_Access_Level (T)
1998 ("expression has deeper access level than component " &
1999 "(RM 3.10.2 (12.2))", E);
2002 -- The initialization expression is a reference to an access
2003 -- discriminant. The type of the discriminant is always deeper
2004 -- than any access type.
2006 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2007 and then Is_Entity_Name (E)
2008 and then Ekind (Entity (E)) = E_In_Parameter
2009 and then Present (Discriminal_Link (Entity (E)))
2012 ("discriminant has deeper accessibility level than target",
2018 -- The parent type may be a private view with unknown discriminants,
2019 -- and thus unconstrained. Regular components must be constrained.
2021 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
2022 if Is_Class_Wide_Type (T) then
2024 ("class-wide subtype with unknown discriminants" &
2025 " in component declaration",
2026 Subtype_Indication (Component_Definition (N)));
2029 ("unconstrained subtype in component declaration",
2030 Subtype_Indication (Component_Definition (N)));
2033 -- Components cannot be abstract, except for the special case of
2034 -- the _Parent field (case of extending an abstract tagged type)
2036 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2037 Error_Msg_N ("type of a component cannot be abstract", N);
2041 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2043 -- The component declaration may have a per-object constraint, set
2044 -- the appropriate flag in the defining identifier of the subtype.
2046 if Present (Subtype_Indication (Component_Definition (N))) then
2048 Sindic : constant Node_Id :=
2049 Subtype_Indication (Component_Definition (N));
2051 if Nkind (Sindic) = N_Subtype_Indication
2052 and then Present (Constraint (Sindic))
2053 and then Contains_POC (Constraint (Sindic))
2055 Set_Has_Per_Object_Constraint (Id);
2060 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2061 -- out some static checks.
2063 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2064 Null_Exclusion_Static_Checks (N);
2067 -- If this component is private (or depends on a private type), flag the
2068 -- record type to indicate that some operations are not available.
2070 P := Private_Component (T);
2074 -- Check for circular definitions
2076 if P = Any_Type then
2077 Set_Etype (Id, Any_Type);
2079 -- There is a gap in the visibility of operations only if the
2080 -- component type is not defined in the scope of the record type.
2082 elsif Scope (P) = Scope (Current_Scope) then
2085 elsif Is_Limited_Type (P) then
2086 Set_Is_Limited_Composite (Current_Scope);
2089 Set_Is_Private_Composite (Current_Scope);
2094 and then Is_Limited_Type (T)
2095 and then Chars (Id) /= Name_uParent
2096 and then Is_Tagged_Type (Current_Scope)
2098 if Is_Derived_Type (Current_Scope)
2099 and then not Is_Known_Limited (Current_Scope)
2102 ("extension of nonlimited type cannot have limited components",
2105 if Is_Interface (Root_Type (Current_Scope)) then
2107 ("\limitedness is not inherited from limited interface", N);
2108 Error_Msg_N ("\add LIMITED to type indication", N);
2111 Explain_Limited_Type (T, N);
2112 Set_Etype (Id, Any_Type);
2113 Set_Is_Limited_Composite (Current_Scope, False);
2115 elsif not Is_Derived_Type (Current_Scope)
2116 and then not Is_Limited_Record (Current_Scope)
2117 and then not Is_Concurrent_Type (Current_Scope)
2120 ("nonlimited tagged type cannot have limited components", N);
2121 Explain_Limited_Type (T, N);
2122 Set_Etype (Id, Any_Type);
2123 Set_Is_Limited_Composite (Current_Scope, False);
2127 -- If the component is an unconstrained task or protected type with
2128 -- discriminants, the component and the enclosing record are limited
2129 -- and the component is constrained by its default values. Compute
2130 -- its actual subtype, else it may be allocated the maximum size by
2131 -- the backend, and possibly overflow.
2133 if Is_Concurrent_Type (T)
2134 and then not Is_Constrained (T)
2135 and then Has_Discriminants (T)
2136 and then not Has_Discriminants (Current_Scope)
2139 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2141 Set_Etype (Id, Act_T);
2142 Set_Component_Definition (N,
2143 Make_Component_Definition (Loc,
2144 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2148 Set_Original_Record_Component (Id, Id);
2150 if Has_Aspects (N) then
2151 Analyze_Aspect_Specifications (N, Id);
2154 Analyze_Dimension (N);
2155 end Analyze_Component_Declaration;
2157 --------------------------
2158 -- Analyze_Declarations --
2159 --------------------------
2161 procedure Analyze_Declarations (L : List_Id) is
2164 procedure Adjust_Decl;
2165 -- Adjust Decl not to include implicit label declarations, since these
2166 -- have strange Sloc values that result in elaboration check problems.
2167 -- (They have the sloc of the label as found in the source, and that
2168 -- is ahead of the current declarative part).
2170 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2171 -- Determine whether Body_Decl denotes the body of a late controlled
2172 -- primitive (either Initialize, Adjust or Finalize). If this is the
2173 -- case, add a proper spec if the body lacks one. The spec is inserted
2174 -- before Body_Decl and immedately analyzed.
2176 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2177 -- Spec_Id is the entity of a package that may define abstract states.
2178 -- If the states have visible refinement, remove the visibility of each
2179 -- constituent at the end of the package body declarations.
2185 procedure Adjust_Decl is
2187 while Present (Prev (Decl))
2188 and then Nkind (Decl) = N_Implicit_Label_Declaration
2194 --------------------------------------
2195 -- Handle_Late_Controlled_Primitive --
2196 --------------------------------------
2198 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2199 Body_Spec : constant Node_Id := Specification (Body_Decl);
2200 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2201 Loc : constant Source_Ptr := Sloc (Body_Id);
2202 Params : constant List_Id :=
2203 Parameter_Specifications (Body_Spec);
2205 Spec_Id : Entity_Id;
2209 -- Consider only procedure bodies whose name matches one of the three
2210 -- controlled primitives.
2212 if Nkind (Body_Spec) /= N_Procedure_Specification
2213 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2219 -- A controlled primitive must have exactly one formal which is not
2220 -- an anonymous access type.
2222 elsif List_Length (Params) /= 1 then
2226 Typ := Parameter_Type (First (Params));
2228 if Nkind (Typ) = N_Access_Definition then
2234 -- The type of the formal must be derived from [Limited_]Controlled
2236 if not Is_Controlled (Entity (Typ)) then
2240 -- Check whether a specification exists for this body. We do not
2241 -- analyze the spec of the body in full, because it will be analyzed
2242 -- again when the body is properly analyzed, and we cannot create
2243 -- duplicate entries in the formals chain. We look for an explicit
2244 -- specification because the body may be an overriding operation and
2245 -- an inherited spec may be present.
2247 Spec_Id := Current_Entity (Body_Id);
2249 while Present (Spec_Id) loop
2250 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2251 and then Scope (Spec_Id) = Current_Scope
2252 and then Present (First_Formal (Spec_Id))
2253 and then No (Next_Formal (First_Formal (Spec_Id)))
2254 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2255 and then Comes_From_Source (Spec_Id)
2260 Spec_Id := Homonym (Spec_Id);
2263 -- At this point the body is known to be a late controlled primitive.
2264 -- Generate a matching spec and insert it before the body. Note the
2265 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2266 -- tree in this case.
2268 Spec := Copy_Separate_Tree (Body_Spec);
2270 -- Ensure that the subprogram declaration does not inherit the null
2271 -- indicator from the body as we now have a proper spec/body pair.
2273 Set_Null_Present (Spec, False);
2275 Insert_Before_And_Analyze (Body_Decl,
2276 Make_Subprogram_Declaration (Loc, Specification => Spec));
2277 end Handle_Late_Controlled_Primitive;
2279 --------------------------------
2280 -- Remove_Visible_Refinements --
2281 --------------------------------
2283 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2284 State_Elmt : Elmt_Id;
2286 if Present (Abstract_States (Spec_Id)) then
2287 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2288 while Present (State_Elmt) loop
2289 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2290 Next_Elmt (State_Elmt);
2293 end Remove_Visible_Refinements;
2298 Freeze_From : Entity_Id := Empty;
2299 Next_Decl : Node_Id;
2300 Spec_Id : Entity_Id;
2302 Body_Seen : Boolean := False;
2303 -- Flag set when the first body [stub] is encountered
2305 In_Package_Body : Boolean := False;
2306 -- Flag set when the current declaration list belongs to a package body
2308 -- Start of processing for Analyze_Declarations
2311 if Restriction_Check_Required (SPARK_05) then
2312 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2316 while Present (Decl) loop
2318 -- Package spec cannot contain a package declaration in SPARK
2320 if Nkind (Decl) = N_Package_Declaration
2321 and then Nkind (Parent (L)) = N_Package_Specification
2323 Check_SPARK_05_Restriction
2324 ("package specification cannot contain a package declaration",
2328 -- Complete analysis of declaration
2331 Next_Decl := Next (Decl);
2333 if No (Freeze_From) then
2334 Freeze_From := First_Entity (Current_Scope);
2337 -- At the end of a declarative part, freeze remaining entities
2338 -- declared in it. The end of the visible declarations of package
2339 -- specification is not the end of a declarative part if private
2340 -- declarations are present. The end of a package declaration is a
2341 -- freezing point only if it a library package. A task definition or
2342 -- protected type definition is not a freeze point either. Finally,
2343 -- we do not freeze entities in generic scopes, because there is no
2344 -- code generated for them and freeze nodes will be generated for
2347 -- The end of a package instantiation is not a freeze point, but
2348 -- for now we make it one, because the generic body is inserted
2349 -- (currently) immediately after. Generic instantiations will not
2350 -- be a freeze point once delayed freezing of bodies is implemented.
2351 -- (This is needed in any case for early instantiations ???).
2353 if No (Next_Decl) then
2354 if Nkind_In (Parent (L), N_Component_List,
2356 N_Protected_Definition)
2360 elsif Nkind (Parent (L)) /= N_Package_Specification then
2361 if Nkind (Parent (L)) = N_Package_Body then
2362 Freeze_From := First_Entity (Current_Scope);
2365 -- There may have been several freezing points previously,
2366 -- for example object declarations or subprogram bodies, but
2367 -- at the end of a declarative part we check freezing from
2368 -- the beginning, even though entities may already be frozen,
2369 -- in order to perform visibility checks on delayed aspects.
2372 Freeze_All (First_Entity (Current_Scope), Decl);
2373 Freeze_From := Last_Entity (Current_Scope);
2375 elsif Scope (Current_Scope) /= Standard_Standard
2376 and then not Is_Child_Unit (Current_Scope)
2377 and then No (Generic_Parent (Parent (L)))
2381 elsif L /= Visible_Declarations (Parent (L))
2382 or else No (Private_Declarations (Parent (L)))
2383 or else Is_Empty_List (Private_Declarations (Parent (L)))
2386 Freeze_All (First_Entity (Current_Scope), Decl);
2387 Freeze_From := Last_Entity (Current_Scope);
2390 -- If next node is a body then freeze all types before the body.
2391 -- An exception occurs for some expander-generated bodies. If these
2392 -- are generated at places where in general language rules would not
2393 -- allow a freeze point, then we assume that the expander has
2394 -- explicitly checked that all required types are properly frozen,
2395 -- and we do not cause general freezing here. This special circuit
2396 -- is used when the encountered body is marked as having already
2399 -- In all other cases (bodies that come from source, and expander
2400 -- generated bodies that have not been analyzed yet), freeze all
2401 -- types now. Note that in the latter case, the expander must take
2402 -- care to attach the bodies at a proper place in the tree so as to
2403 -- not cause unwanted freezing at that point.
2405 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2407 -- When a controlled type is frozen, the expander generates stream
2408 -- and controlled type support routines. If the freeze is caused
2409 -- by the stand alone body of Initialize, Adjust and Finalize, the
2410 -- expander will end up using the wrong version of these routines
2411 -- as the body has not been processed yet. To remedy this, detect
2412 -- a late controlled primitive and create a proper spec for it.
2413 -- This ensures that the primitive will override its inherited
2414 -- counterpart before the freeze takes place.
2416 -- If the declaration we just processed is a body, do not attempt
2417 -- to examine Next_Decl as the late primitive idiom can only apply
2418 -- to the first encountered body.
2420 -- The spec of the late primitive is not generated in ASIS mode to
2421 -- ensure a consistent list of primitives that indicates the true
2422 -- semantic structure of the program (which is not relevant when
2423 -- generating executable code.
2425 -- ??? a cleaner approach may be possible and/or this solution
2426 -- could be extended to general-purpose late primitives, TBD.
2428 if not ASIS_Mode and then not Body_Seen and then not Is_Body (Decl)
2432 if Nkind (Next_Decl) = N_Subprogram_Body then
2433 Handle_Late_Controlled_Primitive (Next_Decl);
2438 Freeze_All (Freeze_From, Decl);
2439 Freeze_From := Last_Entity (Current_Scope);
2445 -- Analyze the contracts of packages and their bodies
2448 Context := Parent (L);
2450 if Nkind (Context) = N_Package_Specification then
2452 -- When a package has private declarations, its contract must be
2453 -- analyzed at the end of the said declarations. This way both the
2454 -- analysis and freeze actions are properly synchronized in case
2455 -- of private type use within the contract.
2457 if L = Private_Declarations (Context) then
2458 Analyze_Package_Contract (Defining_Entity (Context));
2460 -- Build the bodies of the default initial condition procedures
2461 -- for all types subject to pragma Default_Initial_Condition.
2462 -- From a purely Ada stand point, this is a freezing activity,
2463 -- however freezing is not available under GNATprove_Mode. To
2464 -- accomodate both scenarios, the bodies are build at the end
2465 -- of private declaration analysis.
2467 Build_Default_Init_Cond_Procedure_Bodies (L);
2469 -- Otherwise the contract is analyzed at the end of the visible
2472 elsif L = Visible_Declarations (Context)
2473 and then No (Private_Declarations (Context))
2475 Analyze_Package_Contract (Defining_Entity (Context));
2478 elsif Nkind (Context) = N_Package_Body then
2479 In_Package_Body := True;
2480 Spec_Id := Corresponding_Spec (Context);
2482 Analyze_Package_Body_Contract (Defining_Entity (Context));
2486 -- Analyze the contracts of subprogram declarations, subprogram bodies
2487 -- and variables now due to the delayed visibility requirements of their
2491 while Present (Decl) loop
2492 if Nkind (Decl) = N_Object_Declaration then
2493 Analyze_Object_Contract (Defining_Entity (Decl));
2495 elsif Nkind_In (Decl, N_Abstract_Subprogram_Declaration,
2496 N_Generic_Subprogram_Declaration,
2497 N_Subprogram_Declaration)
2499 Analyze_Subprogram_Contract (Defining_Entity (Decl));
2501 elsif Nkind (Decl) = N_Subprogram_Body then
2502 Analyze_Subprogram_Body_Contract (Defining_Entity (Decl));
2504 elsif Nkind (Decl) = N_Subprogram_Body_Stub then
2505 Analyze_Subprogram_Body_Stub_Contract (Defining_Entity (Decl));
2511 -- State refinements are visible upto the end the of the package body
2512 -- declarations. Hide the refinements from visibility to restore the
2513 -- original state conditions.
2515 if In_Package_Body then
2516 Remove_Visible_Refinements (Spec_Id);
2518 end Analyze_Declarations;
2520 -----------------------------------
2521 -- Analyze_Full_Type_Declaration --
2522 -----------------------------------
2524 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2525 Def : constant Node_Id := Type_Definition (N);
2526 Def_Id : constant Entity_Id := Defining_Identifier (N);
2530 Is_Remote : constant Boolean :=
2531 (Is_Remote_Types (Current_Scope)
2532 or else Is_Remote_Call_Interface (Current_Scope))
2533 and then not (In_Private_Part (Current_Scope)
2534 or else In_Package_Body (Current_Scope));
2536 procedure Check_Ops_From_Incomplete_Type;
2537 -- If there is a tagged incomplete partial view of the type, traverse
2538 -- the primitives of the incomplete view and change the type of any
2539 -- controlling formals and result to indicate the full view. The
2540 -- primitives will be added to the full type's primitive operations
2541 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2542 -- is called from Process_Incomplete_Dependents).
2544 ------------------------------------
2545 -- Check_Ops_From_Incomplete_Type --
2546 ------------------------------------
2548 procedure Check_Ops_From_Incomplete_Type is
2555 and then Ekind (Prev) = E_Incomplete_Type
2556 and then Is_Tagged_Type (Prev)
2557 and then Is_Tagged_Type (T)
2559 Elmt := First_Elmt (Primitive_Operations (Prev));
2560 while Present (Elmt) loop
2563 Formal := First_Formal (Op);
2564 while Present (Formal) loop
2565 if Etype (Formal) = Prev then
2566 Set_Etype (Formal, T);
2569 Next_Formal (Formal);
2572 if Etype (Op) = Prev then
2579 end Check_Ops_From_Incomplete_Type;
2581 -- Start of processing for Analyze_Full_Type_Declaration
2584 Prev := Find_Type_Name (N);
2586 -- The type declaration may be subject to pragma Ghost with policy
2587 -- Ignore. Set the mode now to ensure that any nodes generated during
2588 -- analysis and expansion are properly flagged as ignored Ghost.
2590 Set_Ghost_Mode (N, Prev);
2592 -- The full view, if present, now points to the current type. If there
2593 -- is an incomplete partial view, set a link to it, to simplify the
2594 -- retrieval of primitive operations of the type.
2596 -- Ada 2005 (AI-50217): If the type was previously decorated when
2597 -- imported through a LIMITED WITH clause, it appears as incomplete
2598 -- but has no full view.
2600 if Ekind (Prev) = E_Incomplete_Type
2601 and then Present (Full_View (Prev))
2603 T := Full_View (Prev);
2604 Set_Incomplete_View (N, Parent (Prev));
2609 Set_Is_Pure (T, Is_Pure (Current_Scope));
2611 -- We set the flag Is_First_Subtype here. It is needed to set the
2612 -- corresponding flag for the Implicit class-wide-type created
2613 -- during tagged types processing.
2615 Set_Is_First_Subtype (T, True);
2617 -- Only composite types other than array types are allowed to have
2622 -- For derived types, the rule will be checked once we've figured
2623 -- out the parent type.
2625 when N_Derived_Type_Definition =>
2628 -- For record types, discriminants are allowed, unless we are in
2631 when N_Record_Definition =>
2632 if Present (Discriminant_Specifications (N)) then
2633 Check_SPARK_05_Restriction
2634 ("discriminant type is not allowed",
2636 (First (Discriminant_Specifications (N))));
2640 if Present (Discriminant_Specifications (N)) then
2642 ("elementary or array type cannot have discriminants",
2644 (First (Discriminant_Specifications (N))));
2648 -- Elaborate the type definition according to kind, and generate
2649 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2650 -- already done (this happens during the reanalysis that follows a call
2651 -- to the high level optimizer).
2653 if not Analyzed (T) then
2657 when N_Access_To_Subprogram_Definition =>
2658 Access_Subprogram_Declaration (T, Def);
2660 -- If this is a remote access to subprogram, we must create the
2661 -- equivalent fat pointer type, and related subprograms.
2664 Process_Remote_AST_Declaration (N);
2667 -- Validate categorization rule against access type declaration
2668 -- usually a violation in Pure unit, Shared_Passive unit.
2670 Validate_Access_Type_Declaration (T, N);
2672 when N_Access_To_Object_Definition =>
2673 Access_Type_Declaration (T, Def);
2675 -- Validate categorization rule against access type declaration
2676 -- usually a violation in Pure unit, Shared_Passive unit.
2678 Validate_Access_Type_Declaration (T, N);
2680 -- If we are in a Remote_Call_Interface package and define a
2681 -- RACW, then calling stubs and specific stream attributes
2685 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2687 Add_RACW_Features (Def_Id);
2690 when N_Array_Type_Definition =>
2691 Array_Type_Declaration (T, Def);
2693 when N_Derived_Type_Definition =>
2694 Derived_Type_Declaration (T, N, T /= Def_Id);
2696 when N_Enumeration_Type_Definition =>
2697 Enumeration_Type_Declaration (T, Def);
2699 when N_Floating_Point_Definition =>
2700 Floating_Point_Type_Declaration (T, Def);
2702 when N_Decimal_Fixed_Point_Definition =>
2703 Decimal_Fixed_Point_Type_Declaration (T, Def);
2705 when N_Ordinary_Fixed_Point_Definition =>
2706 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2708 when N_Signed_Integer_Type_Definition =>
2709 Signed_Integer_Type_Declaration (T, Def);
2711 when N_Modular_Type_Definition =>
2712 Modular_Type_Declaration (T, Def);
2714 when N_Record_Definition =>
2715 Record_Type_Declaration (T, N, Prev);
2717 -- If declaration has a parse error, nothing to elaborate.
2723 raise Program_Error;
2728 if Etype (T) = Any_Type then
2732 -- Controlled type is not allowed in SPARK
2734 if Is_Visibly_Controlled (T) then
2735 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
2738 -- A type declared within a Ghost region is automatically Ghost
2739 -- (SPARK RM 6.9(2)).
2741 if Comes_From_Source (T) and then Ghost_Mode > None then
2742 Set_Is_Ghost_Entity (T);
2745 -- Some common processing for all types
2747 Set_Depends_On_Private (T, Has_Private_Component (T));
2748 Check_Ops_From_Incomplete_Type;
2750 -- Both the declared entity, and its anonymous base type if one was
2751 -- created, need freeze nodes allocated.
2754 B : constant Entity_Id := Base_Type (T);
2757 -- In the case where the base type differs from the first subtype, we
2758 -- pre-allocate a freeze node, and set the proper link to the first
2759 -- subtype. Freeze_Entity will use this preallocated freeze node when
2760 -- it freezes the entity.
2762 -- This does not apply if the base type is a generic type, whose
2763 -- declaration is independent of the current derived definition.
2765 if B /= T and then not Is_Generic_Type (B) then
2766 Ensure_Freeze_Node (B);
2767 Set_First_Subtype_Link (Freeze_Node (B), T);
2770 -- A type that is imported through a limited_with clause cannot
2771 -- generate any code, and thus need not be frozen. However, an access
2772 -- type with an imported designated type needs a finalization list,
2773 -- which may be referenced in some other package that has non-limited
2774 -- visibility on the designated type. Thus we must create the
2775 -- finalization list at the point the access type is frozen, to
2776 -- prevent unsatisfied references at link time.
2778 if not From_Limited_With (T) or else Is_Access_Type (T) then
2779 Set_Has_Delayed_Freeze (T);
2783 -- Case where T is the full declaration of some private type which has
2784 -- been swapped in Defining_Identifier (N).
2786 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2787 Process_Full_View (N, T, Def_Id);
2789 -- Record the reference. The form of this is a little strange, since
2790 -- the full declaration has been swapped in. So the first parameter
2791 -- here represents the entity to which a reference is made which is
2792 -- the "real" entity, i.e. the one swapped in, and the second
2793 -- parameter provides the reference location.
2795 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2796 -- since we don't want a complaint about the full type being an
2797 -- unwanted reference to the private type
2800 B : constant Boolean := Has_Pragma_Unreferenced (T);
2802 Set_Has_Pragma_Unreferenced (T, False);
2803 Generate_Reference (T, T, 'c');
2804 Set_Has_Pragma_Unreferenced (T, B);
2807 Set_Completion_Referenced (Def_Id);
2809 -- For completion of incomplete type, process incomplete dependents
2810 -- and always mark the full type as referenced (it is the incomplete
2811 -- type that we get for any real reference).
2813 elsif Ekind (Prev) = E_Incomplete_Type then
2814 Process_Incomplete_Dependents (N, T, Prev);
2815 Generate_Reference (Prev, Def_Id, 'c');
2816 Set_Completion_Referenced (Def_Id);
2818 -- If not private type or incomplete type completion, this is a real
2819 -- definition of a new entity, so record it.
2822 Generate_Definition (Def_Id);
2825 -- Propagate any pending access types whose finalization masters need to
2826 -- be fully initialized from the partial to the full view. Guard against
2827 -- an illegal full view that remains unanalyzed.
2829 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
2830 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
2833 if Chars (Scope (Def_Id)) = Name_System
2834 and then Chars (Def_Id) = Name_Address
2835 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2837 Set_Is_Descendent_Of_Address (Def_Id);
2838 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2839 Set_Is_Descendent_Of_Address (Prev);
2842 Set_Optimize_Alignment_Flags (Def_Id);
2843 Check_Eliminated (Def_Id);
2845 -- If the declaration is a completion and aspects are present, apply
2846 -- them to the entity for the type which is currently the partial
2847 -- view, but which is the one that will be frozen.
2849 if Has_Aspects (N) then
2851 -- In most cases the partial view is a private type, and both views
2852 -- appear in different declarative parts. In the unusual case where
2853 -- the partial view is incomplete, perform the analysis on the
2854 -- full view, to prevent freezing anomalies with the corresponding
2855 -- class-wide type, which otherwise might be frozen before the
2856 -- dispatch table is built.
2859 and then Ekind (Prev) /= E_Incomplete_Type
2861 Analyze_Aspect_Specifications (N, Prev);
2866 Analyze_Aspect_Specifications (N, Def_Id);
2869 end Analyze_Full_Type_Declaration;
2871 ----------------------------------
2872 -- Analyze_Incomplete_Type_Decl --
2873 ----------------------------------
2875 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2876 F : constant Boolean := Is_Pure (Current_Scope);
2880 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
2882 Generate_Definition (Defining_Identifier (N));
2884 -- Process an incomplete declaration. The identifier must not have been
2885 -- declared already in the scope. However, an incomplete declaration may
2886 -- appear in the private part of a package, for a private type that has
2887 -- already been declared.
2889 -- In this case, the discriminants (if any) must match
2891 T := Find_Type_Name (N);
2893 Set_Ekind (T, E_Incomplete_Type);
2894 Init_Size_Align (T);
2895 Set_Is_First_Subtype (T, True);
2898 -- An incomplete type declared within a Ghost region is automatically
2899 -- Ghost (SPARK RM 6.9(2)).
2901 if Ghost_Mode > None then
2902 Set_Is_Ghost_Entity (T);
2905 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2906 -- incomplete types.
2908 if Tagged_Present (N) then
2909 Set_Is_Tagged_Type (T, True);
2910 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2911 Make_Class_Wide_Type (T);
2912 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2917 Set_Stored_Constraint (T, No_Elist);
2919 if Present (Discriminant_Specifications (N)) then
2920 Process_Discriminants (N);
2925 -- If the type has discriminants, non-trivial subtypes may be
2926 -- declared before the full view of the type. The full views of those
2927 -- subtypes will be built after the full view of the type.
2929 Set_Private_Dependents (T, New_Elmt_List);
2931 end Analyze_Incomplete_Type_Decl;
2933 -----------------------------------
2934 -- Analyze_Interface_Declaration --
2935 -----------------------------------
2937 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2938 CW : constant Entity_Id := Class_Wide_Type (T);
2941 Set_Is_Tagged_Type (T);
2942 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2944 Set_Is_Limited_Record (T, Limited_Present (Def)
2945 or else Task_Present (Def)
2946 or else Protected_Present (Def)
2947 or else Synchronized_Present (Def));
2949 -- Type is abstract if full declaration carries keyword, or if previous
2950 -- partial view did.
2952 Set_Is_Abstract_Type (T);
2953 Set_Is_Interface (T);
2955 -- Type is a limited interface if it includes the keyword limited, task,
2956 -- protected, or synchronized.
2958 Set_Is_Limited_Interface
2959 (T, Limited_Present (Def)
2960 or else Protected_Present (Def)
2961 or else Synchronized_Present (Def)
2962 or else Task_Present (Def));
2964 Set_Interfaces (T, New_Elmt_List);
2965 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2967 -- Complete the decoration of the class-wide entity if it was already
2968 -- built (i.e. during the creation of the limited view)
2970 if Present (CW) then
2971 Set_Is_Interface (CW);
2972 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
2975 -- Check runtime support for synchronized interfaces
2977 if VM_Target = No_VM
2978 and then (Is_Task_Interface (T)
2979 or else Is_Protected_Interface (T)
2980 or else Is_Synchronized_Interface (T))
2981 and then not RTE_Available (RE_Select_Specific_Data)
2983 Error_Msg_CRT ("synchronized interfaces", T);
2985 end Analyze_Interface_Declaration;
2987 -----------------------------
2988 -- Analyze_Itype_Reference --
2989 -----------------------------
2991 -- Nothing to do. This node is placed in the tree only for the benefit of
2992 -- back end processing, and has no effect on the semantic processing.
2994 procedure Analyze_Itype_Reference (N : Node_Id) is
2996 pragma Assert (Is_Itype (Itype (N)));
2998 end Analyze_Itype_Reference;
3000 --------------------------------
3001 -- Analyze_Number_Declaration --
3002 --------------------------------
3004 procedure Analyze_Number_Declaration (N : Node_Id) is
3005 Id : constant Entity_Id := Defining_Identifier (N);
3006 E : constant Node_Id := Expression (N);
3008 Index : Interp_Index;
3012 -- The number declaration may be subject to pragma Ghost with policy
3013 -- Ignore. Set the mode now to ensure that any nodes generated during
3014 -- analysis and expansion are properly flagged as ignored Ghost.
3018 Generate_Definition (Id);
3021 -- A number declared within a Ghost region is automatically Ghost
3022 -- (SPARK RM 6.9(2)).
3024 if Ghost_Mode > None then
3025 Set_Is_Ghost_Entity (Id);
3028 -- This is an optimization of a common case of an integer literal
3030 if Nkind (E) = N_Integer_Literal then
3031 Set_Is_Static_Expression (E, True);
3032 Set_Etype (E, Universal_Integer);
3034 Set_Etype (Id, Universal_Integer);
3035 Set_Ekind (Id, E_Named_Integer);
3036 Set_Is_Frozen (Id, True);
3040 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3042 -- Process expression, replacing error by integer zero, to avoid
3043 -- cascaded errors or aborts further along in the processing
3045 -- Replace Error by integer zero, which seems least likely to cause
3049 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3050 Set_Error_Posted (E);
3055 -- Verify that the expression is static and numeric. If
3056 -- the expression is overloaded, we apply the preference
3057 -- rule that favors root numeric types.
3059 if not Is_Overloaded (E) then
3061 if Has_Dynamic_Predicate_Aspect (T) then
3063 ("subtype has dynamic predicate, "
3064 & "not allowed in number declaration", N);
3070 Get_First_Interp (E, Index, It);
3071 while Present (It.Typ) loop
3072 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3073 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3075 if T = Any_Type then
3078 elsif It.Typ = Universal_Real
3080 It.Typ = Universal_Integer
3082 -- Choose universal interpretation over any other
3089 Get_Next_Interp (Index, It);
3093 if Is_Integer_Type (T) then
3095 Set_Etype (Id, Universal_Integer);
3096 Set_Ekind (Id, E_Named_Integer);
3098 elsif Is_Real_Type (T) then
3100 -- Because the real value is converted to universal_real, this is a
3101 -- legal context for a universal fixed expression.
3103 if T = Universal_Fixed then
3105 Loc : constant Source_Ptr := Sloc (N);
3106 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3108 New_Occurrence_Of (Universal_Real, Loc),
3109 Expression => Relocate_Node (E));
3116 elsif T = Any_Fixed then
3117 Error_Msg_N ("illegal context for mixed mode operation", E);
3119 -- Expression is of the form : universal_fixed * integer. Try to
3120 -- resolve as universal_real.
3122 T := Universal_Real;
3127 Set_Etype (Id, Universal_Real);
3128 Set_Ekind (Id, E_Named_Real);
3131 Wrong_Type (E, Any_Numeric);
3135 Set_Ekind (Id, E_Constant);
3136 Set_Never_Set_In_Source (Id, True);
3137 Set_Is_True_Constant (Id, True);
3141 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3142 Set_Etype (E, Etype (Id));
3145 if not Is_OK_Static_Expression (E) then
3146 Flag_Non_Static_Expr
3147 ("non-static expression used in number declaration!", E);
3148 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3149 Set_Etype (E, Any_Type);
3151 end Analyze_Number_Declaration;
3153 -----------------------------
3154 -- Analyze_Object_Contract --
3155 -----------------------------
3157 procedure Analyze_Object_Contract (Obj_Id : Entity_Id) is
3158 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
3159 AR_Val : Boolean := False;
3160 AW_Val : Boolean := False;
3161 ER_Val : Boolean := False;
3162 EW_Val : Boolean := False;
3164 Seen : Boolean := False;
3167 -- The loop parameter in an element iterator over a formal container
3168 -- is declared with an object declaration but no contracts apply.
3170 if Ekind (Obj_Id) = E_Loop_Parameter then
3174 if Ekind (Obj_Id) = E_Constant then
3176 -- A constant cannot be effectively volatile. This check is only
3177 -- relevant with SPARK_Mode on as it is not a standard Ada legality
3178 -- rule. Do not flag internally-generated constants that map generic
3179 -- formals to actuals in instantiations (SPARK RM 7.1.3(6)).
3182 and then Is_Effectively_Volatile (Obj_Id)
3183 and then No (Corresponding_Generic_Association (Parent (Obj_Id)))
3185 -- Don't give this for internally generated entities (such as the
3186 -- FIRST and LAST temporaries generated for bounds).
3188 and then Comes_From_Source (Obj_Id)
3190 Error_Msg_N ("constant cannot be volatile", Obj_Id);
3193 else pragma Assert (Ekind (Obj_Id) = E_Variable);
3195 -- The following checks are only relevant when SPARK_Mode is on as
3196 -- they are not standard Ada legality rules. Internally generated
3197 -- temporaries are ignored.
3199 if SPARK_Mode = On and then Comes_From_Source (Obj_Id) then
3200 if Is_Effectively_Volatile (Obj_Id) then
3202 -- The declaration of an effectively volatile object must
3203 -- appear at the library level (SPARK RM 7.1.3(7), C.6(6)).
3205 if not Is_Library_Level_Entity (Obj_Id) then
3207 ("volatile variable & must be declared at library level",
3210 -- An object of a discriminated type cannot be effectively
3211 -- volatile (SPARK RM C.6(4)).
3213 elsif Has_Discriminants (Obj_Typ) then
3215 ("discriminated object & cannot be volatile", Obj_Id);
3217 -- An object of a tagged type cannot be effectively volatile
3218 -- (SPARK RM C.6(5)).
3220 elsif Is_Tagged_Type (Obj_Typ) then
3221 Error_Msg_N ("tagged object & cannot be volatile", Obj_Id);
3224 -- The object is not effectively volatile
3227 -- A non-effectively volatile object cannot have effectively
3228 -- volatile components (SPARK RM 7.1.3(7)).
3230 if not Is_Effectively_Volatile (Obj_Id)
3231 and then Has_Volatile_Component (Obj_Typ)
3234 ("non-volatile object & cannot have volatile components",
3240 if Is_Ghost_Entity (Obj_Id) then
3242 -- A Ghost object cannot be effectively volatile (SPARK RM 6.9(8))
3244 if Is_Effectively_Volatile (Obj_Id) then
3245 Error_Msg_N ("ghost variable & cannot be volatile", Obj_Id);
3247 -- A Ghost object cannot be imported or exported (SPARK RM 6.9(8))
3249 elsif Is_Imported (Obj_Id) then
3250 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3252 elsif Is_Exported (Obj_Id) then
3253 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3257 -- Analyze all external properties
3259 Prag := Get_Pragma (Obj_Id, Pragma_Async_Readers);
3261 if Present (Prag) then
3262 Analyze_External_Property_In_Decl_Part (Prag, AR_Val);
3266 Prag := Get_Pragma (Obj_Id, Pragma_Async_Writers);
3268 if Present (Prag) then
3269 Analyze_External_Property_In_Decl_Part (Prag, AW_Val);
3273 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Reads);
3275 if Present (Prag) then
3276 Analyze_External_Property_In_Decl_Part (Prag, ER_Val);
3280 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Writes);
3282 if Present (Prag) then
3283 Analyze_External_Property_In_Decl_Part (Prag, EW_Val);
3287 -- Verify the mutual interaction of the various external properties
3290 Check_External_Properties (Obj_Id, AR_Val, AW_Val, ER_Val, EW_Val);
3293 -- Check whether the lack of indicator Part_Of agrees with the
3294 -- placement of the variable with respect to the state space.
3296 Prag := Get_Pragma (Obj_Id, Pragma_Part_Of);
3299 Check_Missing_Part_Of (Obj_Id);
3303 -- A ghost object cannot be imported or exported (SPARK RM 6.9(8))
3305 if Is_Ghost_Entity (Obj_Id) then
3306 if Is_Exported (Obj_Id) then
3307 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3309 elsif Is_Imported (Obj_Id) then
3310 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3313 end Analyze_Object_Contract;
3315 --------------------------------
3316 -- Analyze_Object_Declaration --
3317 --------------------------------
3319 procedure Analyze_Object_Declaration (N : Node_Id) is
3320 Loc : constant Source_Ptr := Sloc (N);
3321 Id : constant Entity_Id := Defining_Identifier (N);
3325 E : Node_Id := Expression (N);
3326 -- E is set to Expression (N) throughout this routine. When
3327 -- Expression (N) is modified, E is changed accordingly.
3329 Prev_Entity : Entity_Id := Empty;
3331 function Count_Tasks (T : Entity_Id) return Uint;
3332 -- This function is called when a non-generic library level object of a
3333 -- task type is declared. Its function is to count the static number of
3334 -- tasks declared within the type (it is only called if Has_Tasks is set
3335 -- for T). As a side effect, if an array of tasks with non-static bounds
3336 -- or a variant record type is encountered, Check_Restrictions is called
3337 -- indicating the count is unknown.
3339 function Delayed_Aspect_Present return Boolean;
3340 -- If the declaration has an expression that is an aggregate, and it
3341 -- has aspects that require delayed analysis, the resolution of the
3342 -- aggregate must be deferred to the freeze point of the objet. This
3343 -- special processing was created for address clauses, but it must
3344 -- also apply to Alignment.
3345 -- This must be done before the aspect specifications are analyzed
3346 -- because we must handle the aggregate before the analysis of the
3347 -- object declaration is complete.
3349 -- any other relevant delayed aspects on object declarations ???
3355 function Count_Tasks (T : Entity_Id) return Uint is
3361 if Is_Task_Type (T) then
3364 elsif Is_Record_Type (T) then
3365 if Has_Discriminants (T) then
3366 Check_Restriction (Max_Tasks, N);
3371 C := First_Component (T);
3372 while Present (C) loop
3373 V := V + Count_Tasks (Etype (C));
3380 elsif Is_Array_Type (T) then
3381 X := First_Index (T);
3382 V := Count_Tasks (Component_Type (T));
3383 while Present (X) loop
3386 if not Is_OK_Static_Subtype (C) then
3387 Check_Restriction (Max_Tasks, N);
3390 V := V * (UI_Max (Uint_0,
3391 Expr_Value (Type_High_Bound (C)) -
3392 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3405 ----------------------------
3406 -- Delayed_Aspect_Present --
3407 ----------------------------
3409 function Delayed_Aspect_Present return Boolean is
3414 if Present (Aspect_Specifications (N)) then
3415 A := First (Aspect_Specifications (N));
3416 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3417 while Present (A) loop
3419 A_Id = Aspect_Alignment or else A_Id = Aspect_Address
3429 end Delayed_Aspect_Present;
3431 -- Start of processing for Analyze_Object_Declaration
3434 -- There are three kinds of implicit types generated by an
3435 -- object declaration:
3437 -- 1. Those generated by the original Object Definition
3439 -- 2. Those generated by the Expression
3441 -- 3. Those used to constrain the Object Definition with the
3442 -- expression constraints when the definition is unconstrained.
3444 -- They must be generated in this order to avoid order of elaboration
3445 -- issues. Thus the first step (after entering the name) is to analyze
3446 -- the object definition.
3448 if Constant_Present (N) then
3449 Prev_Entity := Current_Entity_In_Scope (Id);
3451 if Present (Prev_Entity)
3453 -- If the homograph is an implicit subprogram, it is overridden
3454 -- by the current declaration.
3456 ((Is_Overloadable (Prev_Entity)
3457 and then Is_Inherited_Operation (Prev_Entity))
3459 -- The current object is a discriminal generated for an entry
3460 -- family index. Even though the index is a constant, in this
3461 -- particular context there is no true constant redeclaration.
3462 -- Enter_Name will handle the visibility.
3465 (Is_Discriminal (Id)
3466 and then Ekind (Discriminal_Link (Id)) =
3467 E_Entry_Index_Parameter)
3469 -- The current object is the renaming for a generic declared
3470 -- within the instance.
3473 (Ekind (Prev_Entity) = E_Package
3474 and then Nkind (Parent (Prev_Entity)) =
3475 N_Package_Renaming_Declaration
3476 and then not Comes_From_Source (Prev_Entity)
3478 Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
3480 Prev_Entity := Empty;
3484 -- The object declaration may be subject to pragma Ghost with policy
3485 -- Ignore. Set the mode now to ensure that any nodes generated during
3486 -- analysis and expansion are properly flagged as ignored Ghost.
3488 Set_Ghost_Mode (N, Prev_Entity);
3490 if Present (Prev_Entity) then
3491 Constant_Redeclaration (Id, N, T);
3493 Generate_Reference (Prev_Entity, Id, 'c');
3494 Set_Completion_Referenced (Id);
3496 if Error_Posted (N) then
3498 -- Type mismatch or illegal redeclaration, Do not analyze
3499 -- expression to avoid cascaded errors.
3501 T := Find_Type_Of_Object (Object_Definition (N), N);
3503 Set_Ekind (Id, E_Variable);
3507 -- In the normal case, enter identifier at the start to catch premature
3508 -- usage in the initialization expression.
3511 Generate_Definition (Id);
3514 Mark_Coextensions (N, Object_Definition (N));
3516 T := Find_Type_Of_Object (Object_Definition (N), N);
3518 if Nkind (Object_Definition (N)) = N_Access_Definition
3520 (Access_To_Subprogram_Definition (Object_Definition (N)))
3521 and then Protected_Present
3522 (Access_To_Subprogram_Definition (Object_Definition (N)))
3524 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3527 if Error_Posted (Id) then
3529 Set_Ekind (Id, E_Variable);
3534 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3535 -- out some static checks
3537 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3539 -- In case of aggregates we must also take care of the correct
3540 -- initialization of nested aggregates bug this is done at the
3541 -- point of the analysis of the aggregate (see sem_aggr.adb).
3543 if Present (Expression (N))
3544 and then Nkind (Expression (N)) = N_Aggregate
3550 Save_Typ : constant Entity_Id := Etype (Id);
3552 Set_Etype (Id, T); -- Temp. decoration for static checks
3553 Null_Exclusion_Static_Checks (N);
3554 Set_Etype (Id, Save_Typ);
3559 -- Object is marked pure if it is in a pure scope
3561 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3563 -- If deferred constant, make sure context is appropriate. We detect
3564 -- a deferred constant as a constant declaration with no expression.
3565 -- A deferred constant can appear in a package body if its completion
3566 -- is by means of an interface pragma.
3568 if Constant_Present (N) and then No (E) then
3570 -- A deferred constant may appear in the declarative part of the
3571 -- following constructs:
3575 -- extended return statements
3578 -- subprogram bodies
3581 -- When declared inside a package spec, a deferred constant must be
3582 -- completed by a full constant declaration or pragma Import. In all
3583 -- other cases, the only proper completion is pragma Import. Extended
3584 -- return statements are flagged as invalid contexts because they do
3585 -- not have a declarative part and so cannot accommodate the pragma.
3587 if Ekind (Current_Scope) = E_Return_Statement then
3589 ("invalid context for deferred constant declaration (RM 7.4)",
3592 ("\declaration requires an initialization expression",
3594 Set_Constant_Present (N, False);
3596 -- In Ada 83, deferred constant must be of private type
3598 elsif not Is_Private_Type (T) then
3599 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3601 ("(Ada 83) deferred constant must be private type", N);
3605 -- If not a deferred constant, then the object declaration freezes
3606 -- its type, unless the object is of an anonymous type and has delayed
3607 -- aspects. In that case the type is frozen when the object itself is.
3610 Check_Fully_Declared (T, N);
3612 if Has_Delayed_Aspects (Id)
3613 and then Is_Array_Type (T)
3614 and then Is_Itype (T)
3616 Set_Has_Delayed_Freeze (T);
3618 Freeze_Before (N, T);
3622 -- If the object was created by a constrained array definition, then
3623 -- set the link in both the anonymous base type and anonymous subtype
3624 -- that are built to represent the array type to point to the object.
3626 if Nkind (Object_Definition (Declaration_Node (Id))) =
3627 N_Constrained_Array_Definition
3629 Set_Related_Array_Object (T, Id);
3630 Set_Related_Array_Object (Base_Type (T), Id);
3633 -- Special checks for protected objects not at library level
3635 if Is_Protected_Type (T)
3636 and then not Is_Library_Level_Entity (Id)
3638 Check_Restriction (No_Local_Protected_Objects, Id);
3640 -- Protected objects with interrupt handlers must be at library level
3642 -- Ada 2005: This test is not needed (and the corresponding clause
3643 -- in the RM is removed) because accessibility checks are sufficient
3644 -- to make handlers not at the library level illegal.
3646 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3647 -- applies to the '95 version of the language as well.
3649 if Has_Interrupt_Handler (T) and then Ada_Version < Ada_95 then
3651 ("interrupt object can only be declared at library level", Id);
3655 -- The actual subtype of the object is the nominal subtype, unless
3656 -- the nominal one is unconstrained and obtained from the expression.
3660 -- These checks should be performed before the initialization expression
3661 -- is considered, so that the Object_Definition node is still the same
3662 -- as in source code.
3664 -- In SPARK, the nominal subtype is always given by a subtype mark
3665 -- and must not be unconstrained. (The only exception to this is the
3666 -- acceptance of declarations of constants of type String.)
3668 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3670 Check_SPARK_05_Restriction
3671 ("subtype mark required", Object_Definition (N));
3673 elsif Is_Array_Type (T)
3674 and then not Is_Constrained (T)
3675 and then T /= Standard_String
3677 Check_SPARK_05_Restriction
3678 ("subtype mark of constrained type expected",
3679 Object_Definition (N));
3682 -- There are no aliased objects in SPARK
3684 if Aliased_Present (N) then
3685 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3688 -- Process initialization expression if present and not in error
3690 if Present (E) and then E /= Error then
3692 -- Generate an error in case of CPP class-wide object initialization.
3693 -- Required because otherwise the expansion of the class-wide
3694 -- assignment would try to use 'size to initialize the object
3695 -- (primitive that is not available in CPP tagged types).
3697 if Is_Class_Wide_Type (Act_T)
3699 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3701 (Present (Full_View (Root_Type (Etype (Act_T))))
3703 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3706 ("predefined assignment not available for 'C'P'P tagged types",
3710 Mark_Coextensions (N, E);
3713 -- In case of errors detected in the analysis of the expression,
3714 -- decorate it with the expected type to avoid cascaded errors
3716 if No (Etype (E)) then
3720 -- If an initialization expression is present, then we set the
3721 -- Is_True_Constant flag. It will be reset if this is a variable
3722 -- and it is indeed modified.
3724 Set_Is_True_Constant (Id, True);
3726 -- If we are analyzing a constant declaration, set its completion
3727 -- flag after analyzing and resolving the expression.
3729 if Constant_Present (N) then
3730 Set_Has_Completion (Id);
3733 -- Set type and resolve (type may be overridden later on). Note:
3734 -- Ekind (Id) must still be E_Void at this point so that incorrect
3735 -- early usage within E is properly diagnosed.
3739 -- If the expression is an aggregate we must look ahead to detect
3740 -- the possible presence of an address clause, and defer resolution
3741 -- and expansion of the aggregate to the freeze point of the entity.
3743 if Comes_From_Source (N)
3744 and then Expander_Active
3745 and then Nkind (E) = N_Aggregate
3746 and then (Present (Following_Address_Clause (N))
3747 or else Delayed_Aspect_Present)
3755 -- No further action needed if E is a call to an inlined function
3756 -- which returns an unconstrained type and it has been expanded into
3757 -- a procedure call. In that case N has been replaced by an object
3758 -- declaration without initializing expression and it has been
3759 -- analyzed (see Expand_Inlined_Call).
3761 if Back_End_Inlining
3762 and then Expander_Active
3763 and then Nkind (E) = N_Function_Call
3764 and then Nkind (Name (E)) in N_Has_Entity
3765 and then Is_Inlined (Entity (Name (E)))
3766 and then not Is_Constrained (Etype (E))
3767 and then Analyzed (N)
3768 and then No (Expression (N))
3773 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3774 -- node (which was marked already-analyzed), we need to set the type
3775 -- to something other than Any_Access in order to keep gigi happy.
3777 if Etype (E) = Any_Access then
3781 -- If the object is an access to variable, the initialization
3782 -- expression cannot be an access to constant.
3784 if Is_Access_Type (T)
3785 and then not Is_Access_Constant (T)
3786 and then Is_Access_Type (Etype (E))
3787 and then Is_Access_Constant (Etype (E))
3790 ("access to variable cannot be initialized with an "
3791 & "access-to-constant expression", E);
3794 if not Assignment_OK (N) then
3795 Check_Initialization (T, E);
3798 Check_Unset_Reference (E);
3800 -- If this is a variable, then set current value. If this is a
3801 -- declared constant of a scalar type with a static expression,
3802 -- indicate that it is always valid.
3804 if not Constant_Present (N) then
3805 if Compile_Time_Known_Value (E) then
3806 Set_Current_Value (Id, E);
3809 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
3810 Set_Is_Known_Valid (Id);
3813 -- Deal with setting of null flags
3815 if Is_Access_Type (T) then
3816 if Known_Non_Null (E) then
3817 Set_Is_Known_Non_Null (Id, True);
3818 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
3819 Set_Is_Known_Null (Id, True);
3823 -- Check incorrect use of dynamically tagged expressions
3825 if Is_Tagged_Type (T) then
3826 Check_Dynamically_Tagged_Expression
3832 Apply_Scalar_Range_Check (E, T);
3833 Apply_Static_Length_Check (E, T);
3835 if Nkind (Original_Node (N)) = N_Object_Declaration
3836 and then Comes_From_Source (Original_Node (N))
3838 -- Only call test if needed
3840 and then Restriction_Check_Required (SPARK_05)
3841 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
3843 Check_SPARK_05_Restriction
3844 ("initialization expression is not appropriate", E);
3847 -- A formal parameter of a specific tagged type whose related
3848 -- subprogram is subject to pragma Extensions_Visible with value
3849 -- "False" cannot be implicitly converted to a class-wide type by
3850 -- means of an initialization expression (SPARK RM 6.1.7(3)).
3852 if Is_Class_Wide_Type (T) and then Is_EVF_Expression (E) then
3854 ("formal parameter with Extensions_Visible False cannot be "
3855 & "implicitly converted to class-wide type", E);
3859 -- If the No_Streams restriction is set, check that the type of the
3860 -- object is not, and does not contain, any subtype derived from
3861 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3862 -- Has_Stream just for efficiency reasons. There is no point in
3863 -- spending time on a Has_Stream check if the restriction is not set.
3865 if Restriction_Check_Required (No_Streams) then
3866 if Has_Stream (T) then
3867 Check_Restriction (No_Streams, N);
3871 -- Deal with predicate check before we start to do major rewriting. It
3872 -- is OK to initialize and then check the initialized value, since the
3873 -- object goes out of scope if we get a predicate failure. Note that we
3874 -- do this in the analyzer and not the expander because the analyzer
3875 -- does some substantial rewriting in some cases.
3877 -- We need a predicate check if the type has predicates, and if either
3878 -- there is an initializing expression, or for default initialization
3879 -- when we have at least one case of an explicit default initial value
3880 -- and then this is not an internal declaration whose initialization
3881 -- comes later (as for an aggregate expansion).
3883 if not Suppress_Assignment_Checks (N)
3884 and then Present (Predicate_Function (T))
3885 and then not No_Initialization (N)
3889 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3891 -- If the type has a static predicate and the expression is known at
3892 -- compile time, see if the expression satisfies the predicate.
3895 Check_Expression_Against_Static_Predicate (E, T);
3899 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3902 -- Case of unconstrained type
3904 if Is_Indefinite_Subtype (T) then
3906 -- In SPARK, a declaration of unconstrained type is allowed
3907 -- only for constants of type string.
3909 if Is_String_Type (T) and then not Constant_Present (N) then
3910 Check_SPARK_05_Restriction
3911 ("declaration of object of unconstrained type not allowed", N);
3914 -- Nothing to do in deferred constant case
3916 if Constant_Present (N) and then No (E) then
3919 -- Case of no initialization present
3922 if No_Initialization (N) then
3925 elsif Is_Class_Wide_Type (T) then
3927 ("initialization required in class-wide declaration ", N);
3931 ("unconstrained subtype not allowed (need initialization)",
3932 Object_Definition (N));
3934 if Is_Record_Type (T) and then Has_Discriminants (T) then
3936 ("\provide initial value or explicit discriminant values",
3937 Object_Definition (N));
3940 ("\or give default discriminant values for type&",
3941 Object_Definition (N), T);
3943 elsif Is_Array_Type (T) then
3945 ("\provide initial value or explicit array bounds",
3946 Object_Definition (N));
3950 -- Case of initialization present but in error. Set initial
3951 -- expression as absent (but do not make above complaints)
3953 elsif E = Error then
3954 Set_Expression (N, Empty);
3957 -- Case of initialization present
3960 -- Check restrictions in Ada 83
3962 if not Constant_Present (N) then
3964 -- Unconstrained variables not allowed in Ada 83 mode
3966 if Ada_Version = Ada_83
3967 and then Comes_From_Source (Object_Definition (N))
3970 ("(Ada 83) unconstrained variable not allowed",
3971 Object_Definition (N));
3975 -- Now we constrain the variable from the initializing expression
3977 -- If the expression is an aggregate, it has been expanded into
3978 -- individual assignments. Retrieve the actual type from the
3979 -- expanded construct.
3981 if Is_Array_Type (T)
3982 and then No_Initialization (N)
3983 and then Nkind (Original_Node (E)) = N_Aggregate
3987 -- In case of class-wide interface object declarations we delay
3988 -- the generation of the equivalent record type declarations until
3989 -- its expansion because there are cases in they are not required.
3991 elsif Is_Interface (T) then
3994 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
3995 -- we should prevent the generation of another Itype with the
3996 -- same name as the one already generated, or we end up with
3997 -- two identical types in GNATprove.
3999 elsif GNATprove_Mode then
4002 -- If the type is an unchecked union, no subtype can be built from
4003 -- the expression. Rewrite declaration as a renaming, which the
4004 -- back-end can handle properly. This is a rather unusual case,
4005 -- because most unchecked_union declarations have default values
4006 -- for discriminants and are thus not indefinite.
4008 elsif Is_Unchecked_Union (T) then
4009 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4010 Set_Ekind (Id, E_Constant);
4012 Set_Ekind (Id, E_Variable);
4015 -- An object declared within a Ghost region is automatically
4016 -- Ghost (SPARK RM 6.9(2)).
4018 if Comes_From_Source (Id) and then Ghost_Mode > None then
4019 Set_Is_Ghost_Entity (Id);
4021 -- The Ghost policy in effect at the point of declaration
4022 -- and at the point of completion must match
4023 -- (SPARK RM 6.9(15)).
4025 if Present (Prev_Entity)
4026 and then Is_Ghost_Entity (Prev_Entity)
4028 Check_Ghost_Completion (Prev_Entity, Id);
4033 Make_Object_Renaming_Declaration (Loc,
4034 Defining_Identifier => Id,
4035 Subtype_Mark => New_Occurrence_Of (T, Loc),
4038 Set_Renamed_Object (Id, E);
4039 Freeze_Before (N, T);
4044 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
4045 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4048 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4050 if Aliased_Present (N) then
4051 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4054 Freeze_Before (N, Act_T);
4055 Freeze_Before (N, T);
4058 elsif Is_Array_Type (T)
4059 and then No_Initialization (N)
4060 and then Nkind (Original_Node (E)) = N_Aggregate
4062 if not Is_Entity_Name (Object_Definition (N)) then
4064 Check_Compile_Time_Size (Act_T);
4066 if Aliased_Present (N) then
4067 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4071 -- When the given object definition and the aggregate are specified
4072 -- independently, and their lengths might differ do a length check.
4073 -- This cannot happen if the aggregate is of the form (others =>...)
4075 if not Is_Constrained (T) then
4078 elsif Nkind (E) = N_Raise_Constraint_Error then
4080 -- Aggregate is statically illegal. Place back in declaration
4082 Set_Expression (N, E);
4083 Set_No_Initialization (N, False);
4085 elsif T = Etype (E) then
4088 elsif Nkind (E) = N_Aggregate
4089 and then Present (Component_Associations (E))
4090 and then Present (Choices (First (Component_Associations (E))))
4091 and then Nkind (First
4092 (Choices (First (Component_Associations (E))))) = N_Others_Choice
4097 Apply_Length_Check (E, T);
4100 -- If the type is limited unconstrained with defaulted discriminants and
4101 -- there is no expression, then the object is constrained by the
4102 -- defaults, so it is worthwhile building the corresponding subtype.
4104 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4105 and then not Is_Constrained (T)
4106 and then Has_Discriminants (T)
4109 Act_T := Build_Default_Subtype (T, N);
4111 -- Ada 2005: A limited object may be initialized by means of an
4112 -- aggregate. If the type has default discriminants it has an
4113 -- unconstrained nominal type, Its actual subtype will be obtained
4114 -- from the aggregate, and not from the default discriminants.
4119 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4121 elsif Nkind (E) = N_Function_Call
4122 and then Constant_Present (N)
4123 and then Has_Unconstrained_Elements (Etype (E))
4125 -- The back-end has problems with constants of a discriminated type
4126 -- with defaults, if the initial value is a function call. We
4127 -- generate an intermediate temporary that will receive a reference
4128 -- to the result of the call. The initialization expression then
4129 -- becomes a dereference of that temporary.
4131 Remove_Side_Effects (E);
4133 -- If this is a constant declaration of an unconstrained type and
4134 -- the initialization is an aggregate, we can use the subtype of the
4135 -- aggregate for the declared entity because it is immutable.
4137 elsif not Is_Constrained (T)
4138 and then Has_Discriminants (T)
4139 and then Constant_Present (N)
4140 and then not Has_Unchecked_Union (T)
4141 and then Nkind (E) = N_Aggregate
4146 -- Check No_Wide_Characters restriction
4148 Check_Wide_Character_Restriction (T, Object_Definition (N));
4150 -- Indicate this is not set in source. Certainly true for constants, and
4151 -- true for variables so far (will be reset for a variable if and when
4152 -- we encounter a modification in the source).
4154 Set_Never_Set_In_Source (Id);
4156 -- Now establish the proper kind and type of the object
4158 if Constant_Present (N) then
4159 Set_Ekind (Id, E_Constant);
4160 Set_Is_True_Constant (Id);
4163 Set_Ekind (Id, E_Variable);
4165 -- A variable is set as shared passive if it appears in a shared
4166 -- passive package, and is at the outer level. This is not done for
4167 -- entities generated during expansion, because those are always
4168 -- manipulated locally.
4170 if Is_Shared_Passive (Current_Scope)
4171 and then Is_Library_Level_Entity (Id)
4172 and then Comes_From_Source (Id)
4174 Set_Is_Shared_Passive (Id);
4175 Check_Shared_Var (Id, T, N);
4178 -- Set Has_Initial_Value if initializing expression present. Note
4179 -- that if there is no initializing expression, we leave the state
4180 -- of this flag unchanged (usually it will be False, but notably in
4181 -- the case of exception choice variables, it will already be true).
4184 Set_Has_Initial_Value (Id);
4188 -- Initialize alignment and size and capture alignment setting
4190 Init_Alignment (Id);
4192 Set_Optimize_Alignment_Flags (Id);
4194 -- An object declared within a Ghost region is automatically Ghost
4195 -- (SPARK RM 6.9(2)).
4197 if Comes_From_Source (Id)
4198 and then (Ghost_Mode > None
4199 or else (Present (Prev_Entity)
4200 and then Is_Ghost_Entity (Prev_Entity)))
4202 Set_Is_Ghost_Entity (Id);
4204 -- The Ghost policy in effect at the point of declaration and at the
4205 -- point of completion must match (SPARK RM 6.9(16)).
4207 if Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity) then
4208 Check_Ghost_Completion (Prev_Entity, Id);
4212 -- Deal with aliased case
4214 if Aliased_Present (N) then
4215 Set_Is_Aliased (Id);
4217 -- If the object is aliased and the type is unconstrained with
4218 -- defaulted discriminants and there is no expression, then the
4219 -- object is constrained by the defaults, so it is worthwhile
4220 -- building the corresponding subtype.
4222 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4223 -- unconstrained, then only establish an actual subtype if the
4224 -- nominal subtype is indefinite. In definite cases the object is
4225 -- unconstrained in Ada 2005.
4228 and then Is_Record_Type (T)
4229 and then not Is_Constrained (T)
4230 and then Has_Discriminants (T)
4231 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
4233 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4237 -- Now we can set the type of the object
4239 Set_Etype (Id, Act_T);
4241 -- Non-constant object is marked to be treated as volatile if type is
4242 -- volatile and we clear the Current_Value setting that may have been
4243 -- set above. Doing so for constants isn't required and might interfere
4244 -- with possible uses of the object as a static expression in contexts
4245 -- incompatible with volatility (e.g. as a case-statement alternative).
4247 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4248 Set_Treat_As_Volatile (Id);
4249 Set_Current_Value (Id, Empty);
4252 -- Deal with controlled types
4254 if Has_Controlled_Component (Etype (Id))
4255 or else Is_Controlled (Etype (Id))
4257 if not Is_Library_Level_Entity (Id) then
4258 Check_Restriction (No_Nested_Finalization, N);
4260 Validate_Controlled_Object (Id);
4264 if Has_Task (Etype (Id)) then
4265 Check_Restriction (No_Tasking, N);
4267 -- Deal with counting max tasks
4269 -- Nothing to do if inside a generic
4271 if Inside_A_Generic then
4274 -- If library level entity, then count tasks
4276 elsif Is_Library_Level_Entity (Id) then
4277 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4279 -- If not library level entity, then indicate we don't know max
4280 -- tasks and also check task hierarchy restriction and blocking
4281 -- operation (since starting a task is definitely blocking).
4284 Check_Restriction (Max_Tasks, N);
4285 Check_Restriction (No_Task_Hierarchy, N);
4286 Check_Potentially_Blocking_Operation (N);
4289 -- A rather specialized test. If we see two tasks being declared
4290 -- of the same type in the same object declaration, and the task
4291 -- has an entry with an address clause, we know that program error
4292 -- will be raised at run time since we can't have two tasks with
4293 -- entries at the same address.
4295 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4300 E := First_Entity (Etype (Id));
4301 while Present (E) loop
4302 if Ekind (E) = E_Entry
4303 and then Present (Get_Attribute_Definition_Clause
4304 (E, Attribute_Address))
4306 Error_Msg_Warn := SPARK_Mode /= On;
4308 ("more than one task with same entry address<<", N);
4309 Error_Msg_N ("\Program_Error [<<", N);
4311 Make_Raise_Program_Error (Loc,
4312 Reason => PE_Duplicated_Entry_Address));
4322 -- Some simple constant-propagation: if the expression is a constant
4323 -- string initialized with a literal, share the literal. This avoids
4327 and then Is_Entity_Name (E)
4328 and then Ekind (Entity (E)) = E_Constant
4329 and then Base_Type (Etype (E)) = Standard_String
4332 Val : constant Node_Id := Constant_Value (Entity (E));
4334 if Present (Val) and then Nkind (Val) = N_String_Literal then
4335 Rewrite (E, New_Copy (Val));
4340 -- Another optimization: if the nominal subtype is unconstrained and
4341 -- the expression is a function call that returns an unconstrained
4342 -- type, rewrite the declaration as a renaming of the result of the
4343 -- call. The exceptions below are cases where the copy is expected,
4344 -- either by the back end (Aliased case) or by the semantics, as for
4345 -- initializing controlled types or copying tags for classwide types.
4348 and then Nkind (E) = N_Explicit_Dereference
4349 and then Nkind (Original_Node (E)) = N_Function_Call
4350 and then not Is_Library_Level_Entity (Id)
4351 and then not Is_Constrained (Underlying_Type (T))
4352 and then not Is_Aliased (Id)
4353 and then not Is_Class_Wide_Type (T)
4354 and then not Is_Controlled (T)
4355 and then not Has_Controlled_Component (Base_Type (T))
4356 and then Expander_Active
4359 Make_Object_Renaming_Declaration (Loc,
4360 Defining_Identifier => Id,
4361 Access_Definition => Empty,
4362 Subtype_Mark => New_Occurrence_Of
4363 (Base_Type (Etype (Id)), Loc),
4366 Set_Renamed_Object (Id, E);
4368 -- Force generation of debugging information for the constant and for
4369 -- the renamed function call.
4371 Set_Debug_Info_Needed (Id);
4372 Set_Debug_Info_Needed (Entity (Prefix (E)));
4375 if Present (Prev_Entity)
4376 and then Is_Frozen (Prev_Entity)
4377 and then not Error_Posted (Id)
4379 Error_Msg_N ("full constant declaration appears too late", N);
4382 Check_Eliminated (Id);
4384 -- Deal with setting In_Private_Part flag if in private part
4386 if Ekind (Scope (Id)) = E_Package and then In_Private_Part (Scope (Id))
4388 Set_In_Private_Part (Id);
4391 -- Check for violation of No_Local_Timing_Events
4393 if Restriction_Check_Required (No_Local_Timing_Events)
4394 and then not Is_Library_Level_Entity (Id)
4395 and then Is_RTE (Etype (Id), RE_Timing_Event)
4397 Check_Restriction (No_Local_Timing_Events, N);
4401 -- Initialize the refined state of a variable here because this is a
4402 -- common destination for legal and illegal object declarations.
4404 if Ekind (Id) = E_Variable then
4405 Set_Encapsulating_State (Id, Empty);
4408 if Has_Aspects (N) then
4409 Analyze_Aspect_Specifications (N, Id);
4412 Analyze_Dimension (N);
4414 -- Verify whether the object declaration introduces an illegal hidden
4415 -- state within a package subject to a null abstract state.
4417 if Ekind (Id) = E_Variable then
4418 Check_No_Hidden_State (Id);
4420 end Analyze_Object_Declaration;
4422 ---------------------------
4423 -- Analyze_Others_Choice --
4424 ---------------------------
4426 -- Nothing to do for the others choice node itself, the semantic analysis
4427 -- of the others choice will occur as part of the processing of the parent
4429 procedure Analyze_Others_Choice (N : Node_Id) is
4430 pragma Warnings (Off, N);
4433 end Analyze_Others_Choice;
4435 -------------------------------------------
4436 -- Analyze_Private_Extension_Declaration --
4437 -------------------------------------------
4439 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4440 T : constant Entity_Id := Defining_Identifier (N);
4441 Indic : constant Node_Id := Subtype_Indication (N);
4442 Parent_Type : Entity_Id;
4443 Parent_Base : Entity_Id;
4446 -- The private extension declaration may be subject to pragma Ghost with
4447 -- policy Ignore. Set the mode now to ensure that any nodes generated
4448 -- during analysis and expansion are properly flagged as ignored Ghost.
4452 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4454 if Is_Non_Empty_List (Interface_List (N)) then
4460 Intf := First (Interface_List (N));
4461 while Present (Intf) loop
4462 T := Find_Type_Of_Subtype_Indic (Intf);
4464 Diagnose_Interface (Intf, T);
4470 Generate_Definition (T);
4472 -- For other than Ada 2012, just enter the name in the current scope
4474 if Ada_Version < Ada_2012 then
4477 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4478 -- case of private type that completes an incomplete type.
4485 Prev := Find_Type_Name (N);
4487 pragma Assert (Prev = T
4488 or else (Ekind (Prev) = E_Incomplete_Type
4489 and then Present (Full_View (Prev))
4490 and then Full_View (Prev) = T));
4494 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4495 Parent_Base := Base_Type (Parent_Type);
4497 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4498 Set_Ekind (T, Ekind (Parent_Type));
4499 Set_Etype (T, Any_Type);
4502 elsif not Is_Tagged_Type (Parent_Type) then
4504 ("parent of type extension must be a tagged type ", Indic);
4507 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4508 Error_Msg_N ("premature derivation of incomplete type", Indic);
4511 elsif Is_Concurrent_Type (Parent_Type) then
4513 ("parent type of a private extension cannot be "
4514 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
4516 Set_Etype (T, Any_Type);
4517 Set_Ekind (T, E_Limited_Private_Type);
4518 Set_Private_Dependents (T, New_Elmt_List);
4519 Set_Error_Posted (T);
4523 -- Perhaps the parent type should be changed to the class-wide type's
4524 -- specific type in this case to prevent cascading errors ???
4526 if Is_Class_Wide_Type (Parent_Type) then
4528 ("parent of type extension must not be a class-wide type", Indic);
4532 if (not Is_Package_Or_Generic_Package (Current_Scope)
4533 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4534 or else In_Private_Part (Current_Scope)
4537 Error_Msg_N ("invalid context for private extension", N);
4540 -- Set common attributes
4542 Set_Is_Pure (T, Is_Pure (Current_Scope));
4543 Set_Scope (T, Current_Scope);
4544 Set_Ekind (T, E_Record_Type_With_Private);
4545 Init_Size_Align (T);
4546 Set_Default_SSO (T);
4548 Set_Etype (T, Parent_Base);
4549 Set_Has_Task (T, Has_Task (Parent_Base));
4550 Set_Has_Protected (T, Has_Task (Parent_Base));
4552 Set_Convention (T, Convention (Parent_Type));
4553 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4554 Set_Is_First_Subtype (T);
4555 Make_Class_Wide_Type (T);
4557 if Unknown_Discriminants_Present (N) then
4558 Set_Discriminant_Constraint (T, No_Elist);
4561 Build_Derived_Record_Type (N, Parent_Type, T);
4563 -- Propagate inherited invariant information. The new type has
4564 -- invariants, if the parent type has inheritable invariants,
4565 -- and these invariants can in turn be inherited.
4567 if Has_Inheritable_Invariants (Parent_Type) then
4568 Set_Has_Inheritable_Invariants (T);
4569 Set_Has_Invariants (T);
4572 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4573 -- synchronized formal derived type.
4575 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4576 Set_Is_Limited_Record (T);
4578 -- Formal derived type case
4580 if Is_Generic_Type (T) then
4582 -- The parent must be a tagged limited type or a synchronized
4585 if (not Is_Tagged_Type (Parent_Type)
4586 or else not Is_Limited_Type (Parent_Type))
4588 (not Is_Interface (Parent_Type)
4589 or else not Is_Synchronized_Interface (Parent_Type))
4591 Error_Msg_NE ("parent type of & must be tagged limited " &
4592 "or synchronized", N, T);
4595 -- The progenitors (if any) must be limited or synchronized
4598 if Present (Interfaces (T)) then
4601 Iface_Elmt : Elmt_Id;
4604 Iface_Elmt := First_Elmt (Interfaces (T));
4605 while Present (Iface_Elmt) loop
4606 Iface := Node (Iface_Elmt);
4608 if not Is_Limited_Interface (Iface)
4609 and then not Is_Synchronized_Interface (Iface)
4611 Error_Msg_NE ("progenitor & must be limited " &
4612 "or synchronized", N, Iface);
4615 Next_Elmt (Iface_Elmt);
4620 -- Regular derived extension, the parent must be a limited or
4621 -- synchronized interface.
4624 if not Is_Interface (Parent_Type)
4625 or else (not Is_Limited_Interface (Parent_Type)
4626 and then not Is_Synchronized_Interface (Parent_Type))
4629 ("parent type of & must be limited interface", N, T);
4633 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4634 -- extension with a synchronized parent must be explicitly declared
4635 -- synchronized, because the full view will be a synchronized type.
4636 -- This must be checked before the check for limited types below,
4637 -- to ensure that types declared limited are not allowed to extend
4638 -- synchronized interfaces.
4640 elsif Is_Interface (Parent_Type)
4641 and then Is_Synchronized_Interface (Parent_Type)
4642 and then not Synchronized_Present (N)
4645 ("private extension of& must be explicitly synchronized",
4648 elsif Limited_Present (N) then
4649 Set_Is_Limited_Record (T);
4651 if not Is_Limited_Type (Parent_Type)
4653 (not Is_Interface (Parent_Type)
4654 or else not Is_Limited_Interface (Parent_Type))
4656 Error_Msg_NE ("parent type& of limited extension must be limited",
4662 if Has_Aspects (N) then
4663 Analyze_Aspect_Specifications (N, T);
4665 end Analyze_Private_Extension_Declaration;
4667 ---------------------------------
4668 -- Analyze_Subtype_Declaration --
4669 ---------------------------------
4671 procedure Analyze_Subtype_Declaration
4673 Skip : Boolean := False)
4675 Id : constant Entity_Id := Defining_Identifier (N);
4677 R_Checks : Check_Result;
4680 -- The subtype declaration may be subject to pragma Ghost with policy
4681 -- Ignore. Set the mode now to ensure that any nodes generated during
4682 -- analysis and expansion are properly flagged as ignored Ghost.
4686 Generate_Definition (Id);
4687 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4688 Init_Size_Align (Id);
4690 -- The following guard condition on Enter_Name is to handle cases where
4691 -- the defining identifier has already been entered into the scope but
4692 -- the declaration as a whole needs to be analyzed.
4694 -- This case in particular happens for derived enumeration types. The
4695 -- derived enumeration type is processed as an inserted enumeration type
4696 -- declaration followed by a rewritten subtype declaration. The defining
4697 -- identifier, however, is entered into the name scope very early in the
4698 -- processing of the original type declaration and therefore needs to be
4699 -- avoided here, when the created subtype declaration is analyzed. (See
4700 -- Build_Derived_Types)
4702 -- This also happens when the full view of a private type is derived
4703 -- type with constraints. In this case the entity has been introduced
4704 -- in the private declaration.
4706 -- Finally this happens in some complex cases when validity checks are
4707 -- enabled, where the same subtype declaration may be analyzed twice.
4708 -- This can happen if the subtype is created by the pre-analysis of
4709 -- an attribute tht gives the range of a loop statement, and the loop
4710 -- itself appears within an if_statement that will be rewritten during
4714 or else (Present (Etype (Id))
4715 and then (Is_Private_Type (Etype (Id))
4716 or else Is_Task_Type (Etype (Id))
4717 or else Is_Rewrite_Substitution (N)))
4721 elsif Current_Entity (Id) = Id then
4728 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4730 -- Class-wide equivalent types of records with unknown discriminants
4731 -- involve the generation of an itype which serves as the private view
4732 -- of a constrained record subtype. In such cases the base type of the
4733 -- current subtype we are processing is the private itype. Use the full
4734 -- of the private itype when decorating various attributes.
4737 and then Is_Private_Type (T)
4738 and then Present (Full_View (T))
4743 -- Inherit common attributes
4745 Set_Is_Volatile (Id, Is_Volatile (T));
4746 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4747 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4748 Set_Convention (Id, Convention (T));
4750 -- If ancestor has predicates then so does the subtype, and in addition
4751 -- we must delay the freeze to properly arrange predicate inheritance.
4753 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4754 -- in which T = ID, so the above tests and assignments do nothing???
4756 if Has_Predicates (T)
4757 or else (Present (Ancestor_Subtype (T))
4758 and then Has_Predicates (Ancestor_Subtype (T)))
4760 Set_Has_Predicates (Id);
4761 Set_Has_Delayed_Freeze (Id);
4764 -- Subtype of Boolean cannot have a constraint in SPARK
4766 if Is_Boolean_Type (T)
4767 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4769 Check_SPARK_05_Restriction
4770 ("subtype of Boolean cannot have constraint", N);
4773 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4775 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4781 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4782 One_Cstr := First (Constraints (Cstr));
4783 while Present (One_Cstr) loop
4785 -- Index or discriminant constraint in SPARK must be a
4789 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4791 Check_SPARK_05_Restriction
4792 ("subtype mark required", One_Cstr);
4794 -- String subtype must have a lower bound of 1 in SPARK.
4795 -- Note that we do not need to test for the non-static case
4796 -- here, since that was already taken care of in
4797 -- Process_Range_Expr_In_Decl.
4799 elsif Base_Type (T) = Standard_String then
4800 Get_Index_Bounds (One_Cstr, Low, High);
4802 if Is_OK_Static_Expression (Low)
4803 and then Expr_Value (Low) /= 1
4805 Check_SPARK_05_Restriction
4806 ("String subtype must have lower bound of 1", N);
4816 -- In the case where there is no constraint given in the subtype
4817 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4818 -- semantic attributes must be established here.
4820 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4821 Set_Etype (Id, Base_Type (T));
4823 -- Subtype of unconstrained array without constraint is not allowed
4826 if Is_Array_Type (T) and then not Is_Constrained (T) then
4827 Check_SPARK_05_Restriction
4828 ("subtype of unconstrained array must have constraint", N);
4833 Set_Ekind (Id, E_Array_Subtype);
4834 Copy_Array_Subtype_Attributes (Id, T);
4836 when Decimal_Fixed_Point_Kind =>
4837 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4838 Set_Digits_Value (Id, Digits_Value (T));
4839 Set_Delta_Value (Id, Delta_Value (T));
4840 Set_Scale_Value (Id, Scale_Value (T));
4841 Set_Small_Value (Id, Small_Value (T));
4842 Set_Scalar_Range (Id, Scalar_Range (T));
4843 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4844 Set_Is_Constrained (Id, Is_Constrained (T));
4845 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4846 Set_RM_Size (Id, RM_Size (T));
4848 when Enumeration_Kind =>
4849 Set_Ekind (Id, E_Enumeration_Subtype);
4850 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4851 Set_Scalar_Range (Id, Scalar_Range (T));
4852 Set_Is_Character_Type (Id, Is_Character_Type (T));
4853 Set_Is_Constrained (Id, Is_Constrained (T));
4854 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4855 Set_RM_Size (Id, RM_Size (T));
4856 Inherit_Predicate_Flags (Id, T);
4858 when Ordinary_Fixed_Point_Kind =>
4859 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4860 Set_Scalar_Range (Id, Scalar_Range (T));
4861 Set_Small_Value (Id, Small_Value (T));
4862 Set_Delta_Value (Id, Delta_Value (T));
4863 Set_Is_Constrained (Id, Is_Constrained (T));
4864 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4865 Set_RM_Size (Id, RM_Size (T));
4868 Set_Ekind (Id, E_Floating_Point_Subtype);
4869 Set_Scalar_Range (Id, Scalar_Range (T));
4870 Set_Digits_Value (Id, Digits_Value (T));
4871 Set_Is_Constrained (Id, Is_Constrained (T));
4873 when Signed_Integer_Kind =>
4874 Set_Ekind (Id, E_Signed_Integer_Subtype);
4875 Set_Scalar_Range (Id, Scalar_Range (T));
4876 Set_Is_Constrained (Id, Is_Constrained (T));
4877 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4878 Set_RM_Size (Id, RM_Size (T));
4879 Inherit_Predicate_Flags (Id, T);
4881 when Modular_Integer_Kind =>
4882 Set_Ekind (Id, E_Modular_Integer_Subtype);
4883 Set_Scalar_Range (Id, Scalar_Range (T));
4884 Set_Is_Constrained (Id, Is_Constrained (T));
4885 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4886 Set_RM_Size (Id, RM_Size (T));
4887 Inherit_Predicate_Flags (Id, T);
4889 when Class_Wide_Kind =>
4890 Set_Ekind (Id, E_Class_Wide_Subtype);
4891 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4892 Set_Cloned_Subtype (Id, T);
4893 Set_Is_Tagged_Type (Id, True);
4894 Set_Has_Unknown_Discriminants
4896 Set_No_Tagged_Streams_Pragma
4897 (Id, No_Tagged_Streams_Pragma (T));
4899 if Ekind (T) = E_Class_Wide_Subtype then
4900 Set_Equivalent_Type (Id, Equivalent_Type (T));
4903 when E_Record_Type | E_Record_Subtype =>
4904 Set_Ekind (Id, E_Record_Subtype);
4906 if Ekind (T) = E_Record_Subtype
4907 and then Present (Cloned_Subtype (T))
4909 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4911 Set_Cloned_Subtype (Id, T);
4914 Set_First_Entity (Id, First_Entity (T));
4915 Set_Last_Entity (Id, Last_Entity (T));
4916 Set_Has_Discriminants (Id, Has_Discriminants (T));
4917 Set_Is_Constrained (Id, Is_Constrained (T));
4918 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4919 Set_Has_Implicit_Dereference
4920 (Id, Has_Implicit_Dereference (T));
4921 Set_Has_Unknown_Discriminants
4922 (Id, Has_Unknown_Discriminants (T));
4924 if Has_Discriminants (T) then
4925 Set_Discriminant_Constraint
4926 (Id, Discriminant_Constraint (T));
4927 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4929 elsif Has_Unknown_Discriminants (Id) then
4930 Set_Discriminant_Constraint (Id, No_Elist);
4933 if Is_Tagged_Type (T) then
4934 Set_Is_Tagged_Type (Id, True);
4935 Set_No_Tagged_Streams_Pragma
4936 (Id, No_Tagged_Streams_Pragma (T));
4937 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4938 Set_Direct_Primitive_Operations
4939 (Id, Direct_Primitive_Operations (T));
4940 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4942 if Is_Interface (T) then
4943 Set_Is_Interface (Id);
4944 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4948 when Private_Kind =>
4949 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4950 Set_Has_Discriminants (Id, Has_Discriminants (T));
4951 Set_Is_Constrained (Id, Is_Constrained (T));
4952 Set_First_Entity (Id, First_Entity (T));
4953 Set_Last_Entity (Id, Last_Entity (T));
4954 Set_Private_Dependents (Id, New_Elmt_List);
4955 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4956 Set_Has_Implicit_Dereference
4957 (Id, Has_Implicit_Dereference (T));
4958 Set_Has_Unknown_Discriminants
4959 (Id, Has_Unknown_Discriminants (T));
4960 Set_Known_To_Have_Preelab_Init
4961 (Id, Known_To_Have_Preelab_Init (T));
4963 if Is_Tagged_Type (T) then
4964 Set_Is_Tagged_Type (Id);
4965 Set_No_Tagged_Streams_Pragma (Id,
4966 No_Tagged_Streams_Pragma (T));
4967 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4968 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4969 Set_Direct_Primitive_Operations (Id,
4970 Direct_Primitive_Operations (T));
4973 -- In general the attributes of the subtype of a private type
4974 -- are the attributes of the partial view of parent. However,
4975 -- the full view may be a discriminated type, and the subtype
4976 -- must share the discriminant constraint to generate correct
4977 -- calls to initialization procedures.
4979 if Has_Discriminants (T) then
4980 Set_Discriminant_Constraint
4981 (Id, Discriminant_Constraint (T));
4982 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4984 elsif Present (Full_View (T))
4985 and then Has_Discriminants (Full_View (T))
4987 Set_Discriminant_Constraint
4988 (Id, Discriminant_Constraint (Full_View (T)));
4989 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4991 -- This would seem semantically correct, but apparently
4992 -- generates spurious errors about missing components ???
4994 -- Set_Has_Discriminants (Id);
4997 Prepare_Private_Subtype_Completion (Id, N);
4999 -- If this is the subtype of a constrained private type with
5000 -- discriminants that has got a full view and we also have
5001 -- built a completion just above, show that the completion
5002 -- is a clone of the full view to the back-end.
5004 if Has_Discriminants (T)
5005 and then not Has_Unknown_Discriminants (T)
5006 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5007 and then Present (Full_View (T))
5008 and then Present (Full_View (Id))
5010 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5014 Set_Ekind (Id, E_Access_Subtype);
5015 Set_Is_Constrained (Id, Is_Constrained (T));
5016 Set_Is_Access_Constant
5017 (Id, Is_Access_Constant (T));
5018 Set_Directly_Designated_Type
5019 (Id, Designated_Type (T));
5020 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5022 -- A Pure library_item must not contain the declaration of a
5023 -- named access type, except within a subprogram, generic
5024 -- subprogram, task unit, or protected unit, or if it has
5025 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5027 if Comes_From_Source (Id)
5028 and then In_Pure_Unit
5029 and then not In_Subprogram_Task_Protected_Unit
5030 and then not No_Pool_Assigned (Id)
5033 ("named access types not allowed in pure unit", N);
5036 when Concurrent_Kind =>
5037 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5038 Set_Corresponding_Record_Type (Id,
5039 Corresponding_Record_Type (T));
5040 Set_First_Entity (Id, First_Entity (T));
5041 Set_First_Private_Entity (Id, First_Private_Entity (T));
5042 Set_Has_Discriminants (Id, Has_Discriminants (T));
5043 Set_Is_Constrained (Id, Is_Constrained (T));
5044 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5045 Set_Last_Entity (Id, Last_Entity (T));
5047 if Is_Tagged_Type (T) then
5048 Set_No_Tagged_Streams_Pragma
5049 (Id, No_Tagged_Streams_Pragma (T));
5052 if Has_Discriminants (T) then
5053 Set_Discriminant_Constraint
5054 (Id, Discriminant_Constraint (T));
5055 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5058 when Incomplete_Kind =>
5059 if Ada_Version >= Ada_2005 then
5061 -- In Ada 2005 an incomplete type can be explicitly tagged:
5062 -- propagate indication. Note that we also have to include
5063 -- subtypes for Ada 2012 extended use of incomplete types.
5065 Set_Ekind (Id, E_Incomplete_Subtype);
5066 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5067 Set_Private_Dependents (Id, New_Elmt_List);
5069 if Is_Tagged_Type (Id) then
5070 Set_No_Tagged_Streams_Pragma
5071 (Id, No_Tagged_Streams_Pragma (T));
5072 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5075 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5076 -- incomplete type visible through a limited with clause.
5078 if From_Limited_With (T)
5079 and then Present (Non_Limited_View (T))
5081 Set_From_Limited_With (Id);
5082 Set_Non_Limited_View (Id, Non_Limited_View (T));
5084 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5085 -- to the private dependents of the original incomplete
5086 -- type for future transformation.
5089 Append_Elmt (Id, Private_Dependents (T));
5092 -- If the subtype name denotes an incomplete type an error
5093 -- was already reported by Process_Subtype.
5096 Set_Etype (Id, Any_Type);
5100 raise Program_Error;
5104 if Etype (Id) = Any_Type then
5108 -- Some common processing on all types
5110 Set_Size_Info (Id, T);
5111 Set_First_Rep_Item (Id, First_Rep_Item (T));
5113 -- If the parent type is a generic actual, so is the subtype. This may
5114 -- happen in a nested instance. Why Comes_From_Source test???
5116 if not Comes_From_Source (N) then
5117 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5122 Set_Is_Immediately_Visible (Id, True);
5123 Set_Depends_On_Private (Id, Has_Private_Component (T));
5124 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
5126 if Is_Interface (T) then
5127 Set_Is_Interface (Id);
5130 if Present (Generic_Parent_Type (N))
5132 (Nkind (Parent (Generic_Parent_Type (N))) /=
5133 N_Formal_Type_Declaration
5134 or else Nkind (Formal_Type_Definition
5135 (Parent (Generic_Parent_Type (N)))) /=
5136 N_Formal_Private_Type_Definition)
5138 if Is_Tagged_Type (Id) then
5140 -- If this is a generic actual subtype for a synchronized type,
5141 -- the primitive operations are those of the corresponding record
5142 -- for which there is a separate subtype declaration.
5144 if Is_Concurrent_Type (Id) then
5146 elsif Is_Class_Wide_Type (Id) then
5147 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5149 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5152 elsif Scope (Etype (Id)) /= Standard_Standard then
5153 Derive_Subprograms (Generic_Parent_Type (N), Id);
5157 if Is_Private_Type (T) and then Present (Full_View (T)) then
5158 Conditional_Delay (Id, Full_View (T));
5160 -- The subtypes of components or subcomponents of protected types
5161 -- do not need freeze nodes, which would otherwise appear in the
5162 -- wrong scope (before the freeze node for the protected type). The
5163 -- proper subtypes are those of the subcomponents of the corresponding
5166 elsif Ekind (Scope (Id)) /= E_Protected_Type
5167 and then Present (Scope (Scope (Id))) -- error defense
5168 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5170 Conditional_Delay (Id, T);
5173 -- Check that Constraint_Error is raised for a scalar subtype indication
5174 -- when the lower or upper bound of a non-null range lies outside the
5175 -- range of the type mark.
5177 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5178 if Is_Scalar_Type (Etype (Id))
5179 and then Scalar_Range (Id) /=
5180 Scalar_Range (Etype (Subtype_Mark
5181 (Subtype_Indication (N))))
5185 Etype (Subtype_Mark (Subtype_Indication (N))));
5187 -- In the array case, check compatibility for each index
5189 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5191 -- This really should be a subprogram that finds the indications
5195 Subt_Index : Node_Id := First_Index (Id);
5196 Target_Index : Node_Id :=
5198 (Subtype_Mark (Subtype_Indication (N))));
5199 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5202 while Present (Subt_Index) loop
5203 if ((Nkind (Subt_Index) = N_Identifier
5204 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5205 or else Nkind (Subt_Index) = N_Subtype_Indication)
5207 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5210 Target_Typ : constant Entity_Id :=
5211 Etype (Target_Index);
5215 (Scalar_Range (Etype (Subt_Index)),
5218 Defining_Identifier (N));
5220 -- Reset Has_Dynamic_Range_Check on the subtype to
5221 -- prevent elision of the index check due to a dynamic
5222 -- check generated for a preceding index (needed since
5223 -- Insert_Range_Checks tries to avoid generating
5224 -- redundant checks on a given declaration).
5226 Set_Has_Dynamic_Range_Check (N, False);
5232 Sloc (Defining_Identifier (N)));
5234 -- Record whether this index involved a dynamic check
5237 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5241 Next_Index (Subt_Index);
5242 Next_Index (Target_Index);
5245 -- Finally, mark whether the subtype involves dynamic checks
5247 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5252 -- A type invariant applies to any subtype in its scope, in particular
5253 -- to a generic actual.
5255 if Has_Invariants (T) and then In_Open_Scopes (Scope (T)) then
5256 Set_Has_Invariants (Id);
5257 Set_Invariant_Procedure (Id, Invariant_Procedure (T));
5260 -- Make sure that generic actual types are properly frozen. The subtype
5261 -- is marked as a generic actual type when the enclosing instance is
5262 -- analyzed, so here we identify the subtype from the tree structure.
5265 and then Is_Generic_Actual_Type (Id)
5266 and then In_Instance
5267 and then not Comes_From_Source (N)
5268 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
5269 and then Is_Frozen (T)
5271 Freeze_Before (N, Id);
5274 Set_Optimize_Alignment_Flags (Id);
5275 Check_Eliminated (Id);
5278 if Has_Aspects (N) then
5279 Analyze_Aspect_Specifications (N, Id);
5282 Analyze_Dimension (N);
5283 end Analyze_Subtype_Declaration;
5285 --------------------------------
5286 -- Analyze_Subtype_Indication --
5287 --------------------------------
5289 procedure Analyze_Subtype_Indication (N : Node_Id) is
5290 T : constant Entity_Id := Subtype_Mark (N);
5291 R : constant Node_Id := Range_Expression (Constraint (N));
5298 Set_Etype (N, Etype (R));
5299 Resolve (R, Entity (T));
5301 Set_Error_Posted (R);
5302 Set_Error_Posted (T);
5304 end Analyze_Subtype_Indication;
5306 --------------------------
5307 -- Analyze_Variant_Part --
5308 --------------------------
5310 procedure Analyze_Variant_Part (N : Node_Id) is
5311 Discr_Name : Node_Id;
5312 Discr_Type : Entity_Id;
5314 procedure Process_Variant (A : Node_Id);
5315 -- Analyze declarations for a single variant
5317 package Analyze_Variant_Choices is
5318 new Generic_Analyze_Choices (Process_Variant);
5319 use Analyze_Variant_Choices;
5321 ---------------------
5322 -- Process_Variant --
5323 ---------------------
5325 procedure Process_Variant (A : Node_Id) is
5326 CL : constant Node_Id := Component_List (A);
5328 if not Null_Present (CL) then
5329 Analyze_Declarations (Component_Items (CL));
5331 if Present (Variant_Part (CL)) then
5332 Analyze (Variant_Part (CL));
5335 end Process_Variant;
5337 -- Start of processing for Analyze_Variant_Part
5340 Discr_Name := Name (N);
5341 Analyze (Discr_Name);
5343 -- If Discr_Name bad, get out (prevent cascaded errors)
5345 if Etype (Discr_Name) = Any_Type then
5349 -- Check invalid discriminant in variant part
5351 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5352 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5355 Discr_Type := Etype (Entity (Discr_Name));
5357 if not Is_Discrete_Type (Discr_Type) then
5359 ("discriminant in a variant part must be of a discrete type",
5364 -- Now analyze the choices, which also analyzes the declarations that
5365 -- are associated with each choice.
5367 Analyze_Choices (Variants (N), Discr_Type);
5369 -- Note: we used to instantiate and call Check_Choices here to check
5370 -- that the choices covered the discriminant, but it's too early to do
5371 -- that because of statically predicated subtypes, whose analysis may
5372 -- be deferred to their freeze point which may be as late as the freeze
5373 -- point of the containing record. So this call is now to be found in
5374 -- Freeze_Record_Declaration.
5376 end Analyze_Variant_Part;
5378 ----------------------------
5379 -- Array_Type_Declaration --
5380 ----------------------------
5382 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5383 Component_Def : constant Node_Id := Component_Definition (Def);
5384 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5385 Element_Type : Entity_Id;
5386 Implicit_Base : Entity_Id;
5388 Related_Id : Entity_Id := Empty;
5390 P : constant Node_Id := Parent (Def);
5394 if Nkind (Def) = N_Constrained_Array_Definition then
5395 Index := First (Discrete_Subtype_Definitions (Def));
5397 Index := First (Subtype_Marks (Def));
5400 -- Find proper names for the implicit types which may be public. In case
5401 -- of anonymous arrays we use the name of the first object of that type
5405 Related_Id := Defining_Identifier (P);
5411 while Present (Index) loop
5414 -- Test for odd case of trying to index a type by the type itself
5416 if Is_Entity_Name (Index) and then Entity (Index) = T then
5417 Error_Msg_N ("type& cannot be indexed by itself", Index);
5418 Set_Entity (Index, Standard_Boolean);
5419 Set_Etype (Index, Standard_Boolean);
5422 -- Check SPARK restriction requiring a subtype mark
5424 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5425 Check_SPARK_05_Restriction ("subtype mark required", Index);
5428 -- Add a subtype declaration for each index of private array type
5429 -- declaration whose etype is also private. For example:
5432 -- type Index is private;
5434 -- type Table is array (Index) of ...
5437 -- This is currently required by the expander for the internally
5438 -- generated equality subprogram of records with variant parts in
5439 -- which the etype of some component is such private type.
5441 if Ekind (Current_Scope) = E_Package
5442 and then In_Private_Part (Current_Scope)
5443 and then Has_Private_Declaration (Etype (Index))
5446 Loc : constant Source_Ptr := Sloc (Def);
5451 New_E := Make_Temporary (Loc, 'T');
5452 Set_Is_Internal (New_E);
5455 Make_Subtype_Declaration (Loc,
5456 Defining_Identifier => New_E,
5457 Subtype_Indication =>
5458 New_Occurrence_Of (Etype (Index), Loc));
5460 Insert_Before (Parent (Def), Decl);
5462 Set_Etype (Index, New_E);
5464 -- If the index is a range the Entity attribute is not
5465 -- available. Example:
5468 -- type T is private;
5470 -- type T is new Natural;
5471 -- Table : array (T(1) .. T(10)) of Boolean;
5474 if Nkind (Index) /= N_Range then
5475 Set_Entity (Index, New_E);
5480 Make_Index (Index, P, Related_Id, Nb_Index);
5482 -- Check error of subtype with predicate for index type
5484 Bad_Predicated_Subtype_Use
5485 ("subtype& has predicate, not allowed as index subtype",
5486 Index, Etype (Index));
5488 -- Move to next index
5491 Nb_Index := Nb_Index + 1;
5494 -- Process subtype indication if one is present
5496 if Present (Component_Typ) then
5497 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5499 Set_Etype (Component_Typ, Element_Type);
5501 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5502 Check_SPARK_05_Restriction
5503 ("subtype mark required", Component_Typ);
5506 -- Ada 2005 (AI-230): Access Definition case
5508 else pragma Assert (Present (Access_Definition (Component_Def)));
5510 -- Indicate that the anonymous access type is created by the
5511 -- array type declaration.
5513 Element_Type := Access_Definition
5515 N => Access_Definition (Component_Def));
5516 Set_Is_Local_Anonymous_Access (Element_Type);
5518 -- Propagate the parent. This field is needed if we have to generate
5519 -- the master_id associated with an anonymous access to task type
5520 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5522 Set_Parent (Element_Type, Parent (T));
5524 -- Ada 2005 (AI-230): In case of components that are anonymous access
5525 -- types the level of accessibility depends on the enclosing type
5528 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5530 -- Ada 2005 (AI-254)
5533 CD : constant Node_Id :=
5534 Access_To_Subprogram_Definition
5535 (Access_Definition (Component_Def));
5537 if Present (CD) and then Protected_Present (CD) then
5539 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5544 -- Constrained array case
5547 T := Create_Itype (E_Void, P, Related_Id, 'T');
5550 if Nkind (Def) = N_Constrained_Array_Definition then
5552 -- Establish Implicit_Base as unconstrained base type
5554 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5556 Set_Etype (Implicit_Base, Implicit_Base);
5557 Set_Scope (Implicit_Base, Current_Scope);
5558 Set_Has_Delayed_Freeze (Implicit_Base);
5559 Set_Default_SSO (Implicit_Base);
5561 -- The constrained array type is a subtype of the unconstrained one
5563 Set_Ekind (T, E_Array_Subtype);
5564 Init_Size_Align (T);
5565 Set_Etype (T, Implicit_Base);
5566 Set_Scope (T, Current_Scope);
5567 Set_Is_Constrained (T);
5569 First (Discrete_Subtype_Definitions (Def)));
5570 Set_Has_Delayed_Freeze (T);
5572 -- Complete setup of implicit base type
5574 Set_First_Index (Implicit_Base, First_Index (T));
5575 Set_Component_Type (Implicit_Base, Element_Type);
5576 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
5577 Set_Has_Protected (Implicit_Base, Has_Protected (Element_Type));
5578 Set_Component_Size (Implicit_Base, Uint_0);
5579 Set_Packed_Array_Impl_Type (Implicit_Base, Empty);
5580 Set_Has_Controlled_Component (Implicit_Base,
5581 Has_Controlled_Component (Element_Type)
5582 or else Is_Controlled (Element_Type));
5583 Set_Finalize_Storage_Only (Implicit_Base,
5584 Finalize_Storage_Only (Element_Type));
5586 -- Inherit the "ghostness" from the constrained array type
5588 if Is_Ghost_Entity (T) or else Ghost_Mode > None then
5589 Set_Is_Ghost_Entity (Implicit_Base);
5592 -- Unconstrained array case
5595 Set_Ekind (T, E_Array_Type);
5596 Init_Size_Align (T);
5598 Set_Scope (T, Current_Scope);
5599 Set_Component_Size (T, Uint_0);
5600 Set_Is_Constrained (T, False);
5601 Set_First_Index (T, First (Subtype_Marks (Def)));
5602 Set_Has_Delayed_Freeze (T, True);
5603 Set_Has_Task (T, Has_Task (Element_Type));
5604 Set_Has_Protected (T, Has_Protected (Element_Type));
5605 Set_Has_Controlled_Component (T, Has_Controlled_Component
5608 Is_Controlled (Element_Type));
5609 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5611 Set_Default_SSO (T);
5614 -- Common attributes for both cases
5616 Set_Component_Type (Base_Type (T), Element_Type);
5617 Set_Packed_Array_Impl_Type (T, Empty);
5619 if Aliased_Present (Component_Definition (Def)) then
5620 Check_SPARK_05_Restriction
5621 ("aliased is not allowed", Component_Definition (Def));
5622 Set_Has_Aliased_Components (Etype (T));
5625 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5626 -- array type to ensure that objects of this type are initialized.
5628 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5629 Set_Can_Never_Be_Null (T);
5631 if Null_Exclusion_Present (Component_Definition (Def))
5633 -- No need to check itypes because in their case this check was
5634 -- done at their point of creation
5636 and then not Is_Itype (Element_Type)
5639 ("`NOT NULL` not allowed (null already excluded)",
5640 Subtype_Indication (Component_Definition (Def)));
5644 Priv := Private_Component (Element_Type);
5646 if Present (Priv) then
5648 -- Check for circular definitions
5650 if Priv = Any_Type then
5651 Set_Component_Type (Etype (T), Any_Type);
5653 -- There is a gap in the visibility of operations on the composite
5654 -- type only if the component type is defined in a different scope.
5656 elsif Scope (Priv) = Current_Scope then
5659 elsif Is_Limited_Type (Priv) then
5660 Set_Is_Limited_Composite (Etype (T));
5661 Set_Is_Limited_Composite (T);
5663 Set_Is_Private_Composite (Etype (T));
5664 Set_Is_Private_Composite (T);
5668 -- A syntax error in the declaration itself may lead to an empty index
5669 -- list, in which case do a minimal patch.
5671 if No (First_Index (T)) then
5672 Error_Msg_N ("missing index definition in array type declaration", T);
5675 Indexes : constant List_Id :=
5676 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5678 Set_Discrete_Subtype_Definitions (Def, Indexes);
5679 Set_First_Index (T, First (Indexes));
5684 -- Create a concatenation operator for the new type. Internal array
5685 -- types created for packed entities do not need such, they are
5686 -- compatible with the user-defined type.
5688 if Number_Dimensions (T) = 1
5689 and then not Is_Packed_Array_Impl_Type (T)
5691 New_Concatenation_Op (T);
5694 -- In the case of an unconstrained array the parser has already verified
5695 -- that all the indexes are unconstrained but we still need to make sure
5696 -- that the element type is constrained.
5698 if Is_Indefinite_Subtype (Element_Type) then
5700 ("unconstrained element type in array declaration",
5701 Subtype_Indication (Component_Def));
5703 elsif Is_Abstract_Type (Element_Type) then
5705 ("the type of a component cannot be abstract",
5706 Subtype_Indication (Component_Def));
5709 -- There may be an invariant declared for the component type, but
5710 -- the construction of the component invariant checking procedure
5711 -- takes place during expansion.
5712 end Array_Type_Declaration;
5714 ------------------------------------------------------
5715 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5716 ------------------------------------------------------
5718 function Replace_Anonymous_Access_To_Protected_Subprogram
5719 (N : Node_Id) return Entity_Id
5721 Loc : constant Source_Ptr := Sloc (N);
5723 Curr_Scope : constant Scope_Stack_Entry :=
5724 Scope_Stack.Table (Scope_Stack.Last);
5726 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5729 -- Access definition in declaration
5732 -- Object definition or formal definition with an access definition
5735 -- Declaration of anonymous access to subprogram type
5738 -- Original specification in access to subprogram
5743 Set_Is_Internal (Anon);
5746 when N_Component_Declaration |
5747 N_Unconstrained_Array_Definition |
5748 N_Constrained_Array_Definition =>
5749 Comp := Component_Definition (N);
5750 Acc := Access_Definition (Comp);
5752 when N_Discriminant_Specification =>
5753 Comp := Discriminant_Type (N);
5756 when N_Parameter_Specification =>
5757 Comp := Parameter_Type (N);
5760 when N_Access_Function_Definition =>
5761 Comp := Result_Definition (N);
5764 when N_Object_Declaration =>
5765 Comp := Object_Definition (N);
5768 when N_Function_Specification =>
5769 Comp := Result_Definition (N);
5773 raise Program_Error;
5776 Spec := Access_To_Subprogram_Definition (Acc);
5779 Make_Full_Type_Declaration (Loc,
5780 Defining_Identifier => Anon,
5781 Type_Definition => Copy_Separate_Tree (Spec));
5783 Mark_Rewrite_Insertion (Decl);
5785 -- In ASIS mode, analyze the profile on the original node, because
5786 -- the separate copy does not provide enough links to recover the
5787 -- original tree. Analysis is limited to type annotations, within
5788 -- a temporary scope that serves as an anonymous subprogram to collect
5789 -- otherwise useless temporaries and itypes.
5793 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5796 if Nkind (Spec) = N_Access_Function_Definition then
5797 Set_Ekind (Typ, E_Function);
5799 Set_Ekind (Typ, E_Procedure);
5802 Set_Parent (Typ, N);
5803 Set_Scope (Typ, Current_Scope);
5806 Process_Formals (Parameter_Specifications (Spec), Spec);
5808 if Nkind (Spec) = N_Access_Function_Definition then
5810 Def : constant Node_Id := Result_Definition (Spec);
5813 -- The result might itself be an anonymous access type, so
5816 if Nkind (Def) = N_Access_Definition then
5817 if Present (Access_To_Subprogram_Definition (Def)) then
5820 Replace_Anonymous_Access_To_Protected_Subprogram
5823 Find_Type (Subtype_Mark (Def));
5836 -- Insert the new declaration in the nearest enclosing scope. If the
5837 -- node is a body and N is its return type, the declaration belongs in
5838 -- the enclosing scope.
5842 if Nkind (P) = N_Subprogram_Body
5843 and then Nkind (N) = N_Function_Specification
5848 while Present (P) and then not Has_Declarations (P) loop
5852 pragma Assert (Present (P));
5854 if Nkind (P) = N_Package_Specification then
5855 Prepend (Decl, Visible_Declarations (P));
5857 Prepend (Decl, Declarations (P));
5860 -- Replace the anonymous type with an occurrence of the new declaration.
5861 -- In all cases the rewritten node does not have the null-exclusion
5862 -- attribute because (if present) it was already inherited by the
5863 -- anonymous entity (Anon). Thus, in case of components we do not
5864 -- inherit this attribute.
5866 if Nkind (N) = N_Parameter_Specification then
5867 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5868 Set_Etype (Defining_Identifier (N), Anon);
5869 Set_Null_Exclusion_Present (N, False);
5871 elsif Nkind (N) = N_Object_Declaration then
5872 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5873 Set_Etype (Defining_Identifier (N), Anon);
5875 elsif Nkind (N) = N_Access_Function_Definition then
5876 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5878 elsif Nkind (N) = N_Function_Specification then
5879 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5880 Set_Etype (Defining_Unit_Name (N), Anon);
5884 Make_Component_Definition (Loc,
5885 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5888 Mark_Rewrite_Insertion (Comp);
5890 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
5894 -- Temporarily remove the current scope (record or subprogram) from
5895 -- the stack to add the new declarations to the enclosing scope.
5897 Scope_Stack.Decrement_Last;
5899 Set_Is_Itype (Anon);
5900 Scope_Stack.Append (Curr_Scope);
5903 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5904 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5906 end Replace_Anonymous_Access_To_Protected_Subprogram;
5908 -------------------------------
5909 -- Build_Derived_Access_Type --
5910 -------------------------------
5912 procedure Build_Derived_Access_Type
5914 Parent_Type : Entity_Id;
5915 Derived_Type : Entity_Id)
5917 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5919 Desig_Type : Entity_Id;
5921 Discr_Con_Elist : Elist_Id;
5922 Discr_Con_El : Elmt_Id;
5926 -- Set the designated type so it is available in case this is an access
5927 -- to a self-referential type, e.g. a standard list type with a next
5928 -- pointer. Will be reset after subtype is built.
5930 Set_Directly_Designated_Type
5931 (Derived_Type, Designated_Type (Parent_Type));
5933 Subt := Process_Subtype (S, N);
5935 if Nkind (S) /= N_Subtype_Indication
5936 and then Subt /= Base_Type (Subt)
5938 Set_Ekind (Derived_Type, E_Access_Subtype);
5941 if Ekind (Derived_Type) = E_Access_Subtype then
5943 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5944 Ibase : constant Entity_Id :=
5945 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5946 Svg_Chars : constant Name_Id := Chars (Ibase);
5947 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5950 Copy_Node (Pbase, Ibase);
5952 Set_Chars (Ibase, Svg_Chars);
5953 Set_Next_Entity (Ibase, Svg_Next_E);
5954 Set_Sloc (Ibase, Sloc (Derived_Type));
5955 Set_Scope (Ibase, Scope (Derived_Type));
5956 Set_Freeze_Node (Ibase, Empty);
5957 Set_Is_Frozen (Ibase, False);
5958 Set_Comes_From_Source (Ibase, False);
5959 Set_Is_First_Subtype (Ibase, False);
5961 Set_Etype (Ibase, Pbase);
5962 Set_Etype (Derived_Type, Ibase);
5966 Set_Directly_Designated_Type
5967 (Derived_Type, Designated_Type (Subt));
5969 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
5970 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
5971 Set_Size_Info (Derived_Type, Parent_Type);
5972 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
5973 Set_Depends_On_Private (Derived_Type,
5974 Has_Private_Component (Derived_Type));
5975 Conditional_Delay (Derived_Type, Subt);
5977 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
5978 -- that it is not redundant.
5980 if Null_Exclusion_Present (Type_Definition (N)) then
5981 Set_Can_Never_Be_Null (Derived_Type);
5983 -- What is with the "AND THEN FALSE" here ???
5985 if Can_Never_Be_Null (Parent_Type)
5989 ("`NOT NULL` not allowed (& already excludes null)",
5993 elsif Can_Never_Be_Null (Parent_Type) then
5994 Set_Can_Never_Be_Null (Derived_Type);
5997 -- Note: we do not copy the Storage_Size_Variable, since we always go to
5998 -- the root type for this information.
6000 -- Apply range checks to discriminants for derived record case
6001 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6003 Desig_Type := Designated_Type (Derived_Type);
6004 if Is_Composite_Type (Desig_Type)
6005 and then (not Is_Array_Type (Desig_Type))
6006 and then Has_Discriminants (Desig_Type)
6007 and then Base_Type (Desig_Type) /= Desig_Type
6009 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6010 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6012 Discr := First_Discriminant (Base_Type (Desig_Type));
6013 while Present (Discr_Con_El) loop
6014 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6015 Next_Elmt (Discr_Con_El);
6016 Next_Discriminant (Discr);
6019 end Build_Derived_Access_Type;
6021 ------------------------------
6022 -- Build_Derived_Array_Type --
6023 ------------------------------
6025 procedure Build_Derived_Array_Type
6027 Parent_Type : Entity_Id;
6028 Derived_Type : Entity_Id)
6030 Loc : constant Source_Ptr := Sloc (N);
6031 Tdef : constant Node_Id := Type_Definition (N);
6032 Indic : constant Node_Id := Subtype_Indication (Tdef);
6033 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6034 Implicit_Base : Entity_Id;
6035 New_Indic : Node_Id;
6037 procedure Make_Implicit_Base;
6038 -- If the parent subtype is constrained, the derived type is a subtype
6039 -- of an implicit base type derived from the parent base.
6041 ------------------------
6042 -- Make_Implicit_Base --
6043 ------------------------
6045 procedure Make_Implicit_Base is
6048 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6050 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6051 Set_Etype (Implicit_Base, Parent_Base);
6053 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6054 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6056 Set_Has_Delayed_Freeze (Implicit_Base, True);
6058 -- Inherit the "ghostness" from the parent base type
6060 if Is_Ghost_Entity (Parent_Base) or else Ghost_Mode > None then
6061 Set_Is_Ghost_Entity (Implicit_Base);
6063 end Make_Implicit_Base;
6065 -- Start of processing for Build_Derived_Array_Type
6068 if not Is_Constrained (Parent_Type) then
6069 if Nkind (Indic) /= N_Subtype_Indication then
6070 Set_Ekind (Derived_Type, E_Array_Type);
6072 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6073 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6075 Set_Has_Delayed_Freeze (Derived_Type, True);
6079 Set_Etype (Derived_Type, Implicit_Base);
6082 Make_Subtype_Declaration (Loc,
6083 Defining_Identifier => Derived_Type,
6084 Subtype_Indication =>
6085 Make_Subtype_Indication (Loc,
6086 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6087 Constraint => Constraint (Indic)));
6089 Rewrite (N, New_Indic);
6094 if Nkind (Indic) /= N_Subtype_Indication then
6097 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6098 Set_Etype (Derived_Type, Implicit_Base);
6099 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6102 Error_Msg_N ("illegal constraint on constrained type", Indic);
6106 -- If parent type is not a derived type itself, and is declared in
6107 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6108 -- the new type's concatenation operator since Derive_Subprograms
6109 -- will not inherit the parent's operator. If the parent type is
6110 -- unconstrained, the operator is of the unconstrained base type.
6112 if Number_Dimensions (Parent_Type) = 1
6113 and then not Is_Limited_Type (Parent_Type)
6114 and then not Is_Derived_Type (Parent_Type)
6115 and then not Is_Package_Or_Generic_Package
6116 (Scope (Base_Type (Parent_Type)))
6118 if not Is_Constrained (Parent_Type)
6119 and then Is_Constrained (Derived_Type)
6121 New_Concatenation_Op (Implicit_Base);
6123 New_Concatenation_Op (Derived_Type);
6126 end Build_Derived_Array_Type;
6128 -----------------------------------
6129 -- Build_Derived_Concurrent_Type --
6130 -----------------------------------
6132 procedure Build_Derived_Concurrent_Type
6134 Parent_Type : Entity_Id;
6135 Derived_Type : Entity_Id)
6137 Loc : constant Source_Ptr := Sloc (N);
6139 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6140 Corr_Decl : Node_Id;
6141 Corr_Decl_Needed : Boolean;
6142 -- If the derived type has fewer discriminants than its parent, the
6143 -- corresponding record is also a derived type, in order to account for
6144 -- the bound discriminants. We create a full type declaration for it in
6147 Constraint_Present : constant Boolean :=
6148 Nkind (Subtype_Indication (Type_Definition (N))) =
6149 N_Subtype_Indication;
6151 D_Constraint : Node_Id;
6152 New_Constraint : Elist_Id;
6153 Old_Disc : Entity_Id;
6154 New_Disc : Entity_Id;
6158 Set_Stored_Constraint (Derived_Type, No_Elist);
6159 Corr_Decl_Needed := False;
6162 if Present (Discriminant_Specifications (N))
6163 and then Constraint_Present
6165 Old_Disc := First_Discriminant (Parent_Type);
6166 New_Disc := First (Discriminant_Specifications (N));
6167 while Present (New_Disc) and then Present (Old_Disc) loop
6168 Next_Discriminant (Old_Disc);
6173 if Present (Old_Disc) and then Expander_Active then
6175 -- The new type has fewer discriminants, so we need to create a new
6176 -- corresponding record, which is derived from the corresponding
6177 -- record of the parent, and has a stored constraint that captures
6178 -- the values of the discriminant constraints. The corresponding
6179 -- record is needed only if expander is active and code generation is
6182 -- The type declaration for the derived corresponding record has the
6183 -- same discriminant part and constraints as the current declaration.
6184 -- Copy the unanalyzed tree to build declaration.
6186 Corr_Decl_Needed := True;
6187 New_N := Copy_Separate_Tree (N);
6190 Make_Full_Type_Declaration (Loc,
6191 Defining_Identifier => Corr_Record,
6192 Discriminant_Specifications =>
6193 Discriminant_Specifications (New_N),
6195 Make_Derived_Type_Definition (Loc,
6196 Subtype_Indication =>
6197 Make_Subtype_Indication (Loc,
6200 (Corresponding_Record_Type (Parent_Type), Loc),
6203 (Subtype_Indication (Type_Definition (New_N))))));
6206 -- Copy Storage_Size and Relative_Deadline variables if task case
6208 if Is_Task_Type (Parent_Type) then
6209 Set_Storage_Size_Variable (Derived_Type,
6210 Storage_Size_Variable (Parent_Type));
6211 Set_Relative_Deadline_Variable (Derived_Type,
6212 Relative_Deadline_Variable (Parent_Type));
6215 if Present (Discriminant_Specifications (N)) then
6216 Push_Scope (Derived_Type);
6217 Check_Or_Process_Discriminants (N, Derived_Type);
6219 if Constraint_Present then
6221 Expand_To_Stored_Constraint
6223 Build_Discriminant_Constraints
6225 Subtype_Indication (Type_Definition (N)), True));
6230 elsif Constraint_Present then
6232 -- Build constrained subtype, copying the constraint, and derive
6233 -- from it to create a derived constrained type.
6236 Loc : constant Source_Ptr := Sloc (N);
6237 Anon : constant Entity_Id :=
6238 Make_Defining_Identifier (Loc,
6239 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6244 Make_Subtype_Declaration (Loc,
6245 Defining_Identifier => Anon,
6246 Subtype_Indication =>
6247 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6248 Insert_Before (N, Decl);
6251 Rewrite (Subtype_Indication (Type_Definition (N)),
6252 New_Occurrence_Of (Anon, Loc));
6253 Set_Analyzed (Derived_Type, False);
6259 -- By default, operations and private data are inherited from parent.
6260 -- However, in the presence of bound discriminants, a new corresponding
6261 -- record will be created, see below.
6263 Set_Has_Discriminants
6264 (Derived_Type, Has_Discriminants (Parent_Type));
6265 Set_Corresponding_Record_Type
6266 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6268 -- Is_Constrained is set according the parent subtype, but is set to
6269 -- False if the derived type is declared with new discriminants.
6273 (Is_Constrained (Parent_Type) or else Constraint_Present)
6274 and then not Present (Discriminant_Specifications (N)));
6276 if Constraint_Present then
6277 if not Has_Discriminants (Parent_Type) then
6278 Error_Msg_N ("untagged parent must have discriminants", N);
6280 elsif Present (Discriminant_Specifications (N)) then
6282 -- Verify that new discriminants are used to constrain old ones
6287 (Constraint (Subtype_Indication (Type_Definition (N)))));
6289 Old_Disc := First_Discriminant (Parent_Type);
6291 while Present (D_Constraint) loop
6292 if Nkind (D_Constraint) /= N_Discriminant_Association then
6294 -- Positional constraint. If it is a reference to a new
6295 -- discriminant, it constrains the corresponding old one.
6297 if Nkind (D_Constraint) = N_Identifier then
6298 New_Disc := First_Discriminant (Derived_Type);
6299 while Present (New_Disc) loop
6300 exit when Chars (New_Disc) = Chars (D_Constraint);
6301 Next_Discriminant (New_Disc);
6304 if Present (New_Disc) then
6305 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6309 Next_Discriminant (Old_Disc);
6311 -- if this is a named constraint, search by name for the old
6312 -- discriminants constrained by the new one.
6314 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6316 -- Find new discriminant with that name
6318 New_Disc := First_Discriminant (Derived_Type);
6319 while Present (New_Disc) loop
6321 Chars (New_Disc) = Chars (Expression (D_Constraint));
6322 Next_Discriminant (New_Disc);
6325 if Present (New_Disc) then
6327 -- Verify that new discriminant renames some discriminant
6328 -- of the parent type, and associate the new discriminant
6329 -- with one or more old ones that it renames.
6335 Selector := First (Selector_Names (D_Constraint));
6336 while Present (Selector) loop
6337 Old_Disc := First_Discriminant (Parent_Type);
6338 while Present (Old_Disc) loop
6339 exit when Chars (Old_Disc) = Chars (Selector);
6340 Next_Discriminant (Old_Disc);
6343 if Present (Old_Disc) then
6344 Set_Corresponding_Discriminant
6345 (New_Disc, Old_Disc);
6354 Next (D_Constraint);
6357 New_Disc := First_Discriminant (Derived_Type);
6358 while Present (New_Disc) loop
6359 if No (Corresponding_Discriminant (New_Disc)) then
6361 ("new discriminant& must constrain old one", N, New_Disc);
6364 Subtypes_Statically_Compatible
6366 Etype (Corresponding_Discriminant (New_Disc)))
6369 ("& not statically compatible with parent discriminant",
6373 Next_Discriminant (New_Disc);
6377 elsif Present (Discriminant_Specifications (N)) then
6379 ("missing discriminant constraint in untagged derivation", N);
6382 -- The entity chain of the derived type includes the new discriminants
6383 -- but shares operations with the parent.
6385 if Present (Discriminant_Specifications (N)) then
6386 Old_Disc := First_Discriminant (Parent_Type);
6387 while Present (Old_Disc) loop
6388 if No (Next_Entity (Old_Disc))
6389 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6392 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6396 Next_Discriminant (Old_Disc);
6400 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6401 if Has_Discriminants (Parent_Type) then
6402 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6403 Set_Discriminant_Constraint (
6404 Derived_Type, Discriminant_Constraint (Parent_Type));
6408 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6410 Set_Has_Completion (Derived_Type);
6412 if Corr_Decl_Needed then
6413 Set_Stored_Constraint (Derived_Type, New_Constraint);
6414 Insert_After (N, Corr_Decl);
6415 Analyze (Corr_Decl);
6416 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6418 end Build_Derived_Concurrent_Type;
6420 ------------------------------------
6421 -- Build_Derived_Enumeration_Type --
6422 ------------------------------------
6424 procedure Build_Derived_Enumeration_Type
6426 Parent_Type : Entity_Id;
6427 Derived_Type : Entity_Id)
6429 Loc : constant Source_Ptr := Sloc (N);
6430 Def : constant Node_Id := Type_Definition (N);
6431 Indic : constant Node_Id := Subtype_Indication (Def);
6432 Implicit_Base : Entity_Id;
6433 Literal : Entity_Id;
6434 New_Lit : Entity_Id;
6435 Literals_List : List_Id;
6436 Type_Decl : Node_Id;
6438 Rang_Expr : Node_Id;
6441 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6442 -- not have explicit literals lists we need to process types derived
6443 -- from them specially. This is handled by Derived_Standard_Character.
6444 -- If the parent type is a generic type, there are no literals either,
6445 -- and we construct the same skeletal representation as for the generic
6448 if Is_Standard_Character_Type (Parent_Type) then
6449 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6451 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6457 if Nkind (Indic) /= N_Subtype_Indication then
6459 Make_Attribute_Reference (Loc,
6460 Attribute_Name => Name_First,
6461 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6462 Set_Etype (Lo, Derived_Type);
6465 Make_Attribute_Reference (Loc,
6466 Attribute_Name => Name_Last,
6467 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6468 Set_Etype (Hi, Derived_Type);
6470 Set_Scalar_Range (Derived_Type,
6476 -- Analyze subtype indication and verify compatibility
6477 -- with parent type.
6479 if Base_Type (Process_Subtype (Indic, N)) /=
6480 Base_Type (Parent_Type)
6483 ("illegal constraint for formal discrete type", N);
6489 -- If a constraint is present, analyze the bounds to catch
6490 -- premature usage of the derived literals.
6492 if Nkind (Indic) = N_Subtype_Indication
6493 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6495 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6496 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6499 -- Introduce an implicit base type for the derived type even if there
6500 -- is no constraint attached to it, since this seems closer to the
6501 -- Ada semantics. Build a full type declaration tree for the derived
6502 -- type using the implicit base type as the defining identifier. The
6503 -- build a subtype declaration tree which applies the constraint (if
6504 -- any) have it replace the derived type declaration.
6506 Literal := First_Literal (Parent_Type);
6507 Literals_List := New_List;
6508 while Present (Literal)
6509 and then Ekind (Literal) = E_Enumeration_Literal
6511 -- Literals of the derived type have the same representation as
6512 -- those of the parent type, but this representation can be
6513 -- overridden by an explicit representation clause. Indicate
6514 -- that there is no explicit representation given yet. These
6515 -- derived literals are implicit operations of the new type,
6516 -- and can be overridden by explicit ones.
6518 if Nkind (Literal) = N_Defining_Character_Literal then
6520 Make_Defining_Character_Literal (Loc, Chars (Literal));
6522 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6525 Set_Ekind (New_Lit, E_Enumeration_Literal);
6526 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6527 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6528 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6529 Set_Alias (New_Lit, Literal);
6530 Set_Is_Known_Valid (New_Lit, True);
6532 Append (New_Lit, Literals_List);
6533 Next_Literal (Literal);
6537 Make_Defining_Identifier (Sloc (Derived_Type),
6538 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6540 -- Indicate the proper nature of the derived type. This must be done
6541 -- before analysis of the literals, to recognize cases when a literal
6542 -- may be hidden by a previous explicit function definition (cf.
6545 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6546 Set_Etype (Derived_Type, Implicit_Base);
6549 Make_Full_Type_Declaration (Loc,
6550 Defining_Identifier => Implicit_Base,
6551 Discriminant_Specifications => No_List,
6553 Make_Enumeration_Type_Definition (Loc, Literals_List));
6555 Mark_Rewrite_Insertion (Type_Decl);
6556 Insert_Before (N, Type_Decl);
6557 Analyze (Type_Decl);
6559 -- After the implicit base is analyzed its Etype needs to be changed
6560 -- to reflect the fact that it is derived from the parent type which
6561 -- was ignored during analysis. We also set the size at this point.
6563 Set_Etype (Implicit_Base, Parent_Type);
6565 Set_Size_Info (Implicit_Base, Parent_Type);
6566 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6567 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6569 -- Copy other flags from parent type
6571 Set_Has_Non_Standard_Rep
6572 (Implicit_Base, Has_Non_Standard_Rep
6574 Set_Has_Pragma_Ordered
6575 (Implicit_Base, Has_Pragma_Ordered
6577 Set_Has_Delayed_Freeze (Implicit_Base);
6579 -- Process the subtype indication including a validation check on the
6580 -- constraint, if any. If a constraint is given, its bounds must be
6581 -- implicitly converted to the new type.
6583 if Nkind (Indic) = N_Subtype_Indication then
6585 R : constant Node_Id :=
6586 Range_Expression (Constraint (Indic));
6589 if Nkind (R) = N_Range then
6590 Hi := Build_Scalar_Bound
6591 (High_Bound (R), Parent_Type, Implicit_Base);
6592 Lo := Build_Scalar_Bound
6593 (Low_Bound (R), Parent_Type, Implicit_Base);
6596 -- Constraint is a Range attribute. Replace with explicit
6597 -- mention of the bounds of the prefix, which must be a
6600 Analyze (Prefix (R));
6602 Convert_To (Implicit_Base,
6603 Make_Attribute_Reference (Loc,
6604 Attribute_Name => Name_Last,
6606 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6609 Convert_To (Implicit_Base,
6610 Make_Attribute_Reference (Loc,
6611 Attribute_Name => Name_First,
6613 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6620 (Type_High_Bound (Parent_Type),
6621 Parent_Type, Implicit_Base);
6624 (Type_Low_Bound (Parent_Type),
6625 Parent_Type, Implicit_Base);
6633 -- If we constructed a default range for the case where no range
6634 -- was given, then the expressions in the range must not freeze
6635 -- since they do not correspond to expressions in the source.
6637 if Nkind (Indic) /= N_Subtype_Indication then
6638 Set_Must_Not_Freeze (Lo);
6639 Set_Must_Not_Freeze (Hi);
6640 Set_Must_Not_Freeze (Rang_Expr);
6644 Make_Subtype_Declaration (Loc,
6645 Defining_Identifier => Derived_Type,
6646 Subtype_Indication =>
6647 Make_Subtype_Indication (Loc,
6648 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6650 Make_Range_Constraint (Loc,
6651 Range_Expression => Rang_Expr))));
6655 -- Propagate the aspects from the original type declaration to the
6656 -- declaration of the implicit base.
6658 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6660 -- Apply a range check. Since this range expression doesn't have an
6661 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6664 if Nkind (Indic) = N_Subtype_Indication then
6666 (Range_Expression (Constraint (Indic)), Parent_Type,
6667 Source_Typ => Entity (Subtype_Mark (Indic)));
6670 end Build_Derived_Enumeration_Type;
6672 --------------------------------
6673 -- Build_Derived_Numeric_Type --
6674 --------------------------------
6676 procedure Build_Derived_Numeric_Type
6678 Parent_Type : Entity_Id;
6679 Derived_Type : Entity_Id)
6681 Loc : constant Source_Ptr := Sloc (N);
6682 Tdef : constant Node_Id := Type_Definition (N);
6683 Indic : constant Node_Id := Subtype_Indication (Tdef);
6684 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6685 No_Constraint : constant Boolean := Nkind (Indic) /=
6686 N_Subtype_Indication;
6687 Implicit_Base : Entity_Id;
6693 -- Process the subtype indication including a validation check on
6694 -- the constraint if any.
6696 Discard_Node (Process_Subtype (Indic, N));
6698 -- Introduce an implicit base type for the derived type even if there
6699 -- is no constraint attached to it, since this seems closer to the Ada
6703 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6705 Set_Etype (Implicit_Base, Parent_Base);
6706 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6707 Set_Size_Info (Implicit_Base, Parent_Base);
6708 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6709 Set_Parent (Implicit_Base, Parent (Derived_Type));
6710 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
6712 -- Set RM Size for discrete type or decimal fixed-point type
6713 -- Ordinary fixed-point is excluded, why???
6715 if Is_Discrete_Type (Parent_Base)
6716 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
6718 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6721 Set_Has_Delayed_Freeze (Implicit_Base);
6723 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6724 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6726 Set_Scalar_Range (Implicit_Base,
6731 if Has_Infinities (Parent_Base) then
6732 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6735 -- The Derived_Type, which is the entity of the declaration, is a
6736 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6737 -- absence of an explicit constraint.
6739 Set_Etype (Derived_Type, Implicit_Base);
6741 -- If we did not have a constraint, then the Ekind is set from the
6742 -- parent type (otherwise Process_Subtype has set the bounds)
6744 if No_Constraint then
6745 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6748 -- If we did not have a range constraint, then set the range from the
6749 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6751 if No_Constraint or else not Has_Range_Constraint (Indic) then
6752 Set_Scalar_Range (Derived_Type,
6754 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6755 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6756 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6758 if Has_Infinities (Parent_Type) then
6759 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6762 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6765 Set_Is_Descendent_Of_Address (Derived_Type,
6766 Is_Descendent_Of_Address (Parent_Type));
6767 Set_Is_Descendent_Of_Address (Implicit_Base,
6768 Is_Descendent_Of_Address (Parent_Type));
6770 -- Set remaining type-specific fields, depending on numeric type
6772 if Is_Modular_Integer_Type (Parent_Type) then
6773 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6775 Set_Non_Binary_Modulus
6776 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6779 (Implicit_Base, Is_Known_Valid (Parent_Base));
6781 elsif Is_Floating_Point_Type (Parent_Type) then
6783 -- Digits of base type is always copied from the digits value of
6784 -- the parent base type, but the digits of the derived type will
6785 -- already have been set if there was a constraint present.
6787 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6788 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6790 if No_Constraint then
6791 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6794 elsif Is_Fixed_Point_Type (Parent_Type) then
6796 -- Small of base type and derived type are always copied from the
6797 -- parent base type, since smalls never change. The delta of the
6798 -- base type is also copied from the parent base type. However the
6799 -- delta of the derived type will have been set already if a
6800 -- constraint was present.
6802 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6803 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6804 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6806 if No_Constraint then
6807 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6810 -- The scale and machine radix in the decimal case are always
6811 -- copied from the parent base type.
6813 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6814 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6815 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6817 Set_Machine_Radix_10
6818 (Derived_Type, Machine_Radix_10 (Parent_Base));
6819 Set_Machine_Radix_10
6820 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6822 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6824 if No_Constraint then
6825 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6828 -- the analysis of the subtype_indication sets the
6829 -- digits value of the derived type.
6836 if Is_Integer_Type (Parent_Type) then
6837 Set_Has_Shift_Operator
6838 (Implicit_Base, Has_Shift_Operator (Parent_Type));
6841 -- The type of the bounds is that of the parent type, and they
6842 -- must be converted to the derived type.
6844 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6846 -- The implicit_base should be frozen when the derived type is frozen,
6847 -- but note that it is used in the conversions of the bounds. For fixed
6848 -- types we delay the determination of the bounds until the proper
6849 -- freezing point. For other numeric types this is rejected by GCC, for
6850 -- reasons that are currently unclear (???), so we choose to freeze the
6851 -- implicit base now. In the case of integers and floating point types
6852 -- this is harmless because subsequent representation clauses cannot
6853 -- affect anything, but it is still baffling that we cannot use the
6854 -- same mechanism for all derived numeric types.
6856 -- There is a further complication: actually some representation
6857 -- clauses can affect the implicit base type. For example, attribute
6858 -- definition clauses for stream-oriented attributes need to set the
6859 -- corresponding TSS entries on the base type, and this normally
6860 -- cannot be done after the base type is frozen, so the circuitry in
6861 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
6862 -- and not use Set_TSS in this case.
6864 -- There are also consequences for the case of delayed representation
6865 -- aspects for some cases. For example, a Size aspect is delayed and
6866 -- should not be evaluated to the freeze point. This early freezing
6867 -- means that the size attribute evaluation happens too early???
6869 if Is_Fixed_Point_Type (Parent_Type) then
6870 Conditional_Delay (Implicit_Base, Parent_Type);
6872 Freeze_Before (N, Implicit_Base);
6874 end Build_Derived_Numeric_Type;
6876 --------------------------------
6877 -- Build_Derived_Private_Type --
6878 --------------------------------
6880 procedure Build_Derived_Private_Type
6882 Parent_Type : Entity_Id;
6883 Derived_Type : Entity_Id;
6884 Is_Completion : Boolean;
6885 Derive_Subps : Boolean := True)
6887 Loc : constant Source_Ptr := Sloc (N);
6888 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
6889 Par_Scope : constant Entity_Id := Scope (Par_Base);
6890 Full_N : constant Node_Id := New_Copy_Tree (N);
6891 Full_Der : Entity_Id := New_Copy (Derived_Type);
6894 procedure Build_Full_Derivation;
6895 -- Build full derivation, i.e. derive from the full view
6897 procedure Copy_And_Build;
6898 -- Copy derived type declaration, replace parent with its full view,
6899 -- and build derivation
6901 ---------------------------
6902 -- Build_Full_Derivation --
6903 ---------------------------
6905 procedure Build_Full_Derivation is
6907 -- If parent scope is not open, install the declarations
6909 if not In_Open_Scopes (Par_Scope) then
6910 Install_Private_Declarations (Par_Scope);
6911 Install_Visible_Declarations (Par_Scope);
6913 Uninstall_Declarations (Par_Scope);
6915 -- If parent scope is open and in another unit, and parent has a
6916 -- completion, then the derivation is taking place in the visible
6917 -- part of a child unit. In that case retrieve the full view of
6918 -- the parent momentarily.
6920 elsif not In_Same_Source_Unit (N, Parent_Type) then
6921 Full_P := Full_View (Parent_Type);
6922 Exchange_Declarations (Parent_Type);
6924 Exchange_Declarations (Full_P);
6926 -- Otherwise it is a local derivation
6931 end Build_Full_Derivation;
6933 --------------------
6934 -- Copy_And_Build --
6935 --------------------
6937 procedure Copy_And_Build is
6938 Full_Parent : Entity_Id := Parent_Type;
6941 -- If the parent is itself derived from another private type,
6942 -- installing the private declarations has not affected its
6943 -- privacy status, so use its own full view explicitly.
6945 if Is_Private_Type (Full_Parent)
6946 and then Present (Full_View (Full_Parent))
6948 Full_Parent := Full_View (Full_Parent);
6951 -- And its underlying full view if necessary
6953 if Is_Private_Type (Full_Parent)
6954 and then Present (Underlying_Full_View (Full_Parent))
6956 Full_Parent := Underlying_Full_View (Full_Parent);
6959 -- For record, access and most enumeration types, derivation from
6960 -- the full view requires a fully-fledged declaration. In the other
6961 -- cases, just use an itype.
6963 if Ekind (Full_Parent) in Record_Kind
6964 or else Ekind (Full_Parent) in Access_Kind
6966 (Ekind (Full_Parent) in Enumeration_Kind
6967 and then not Is_Standard_Character_Type (Full_Parent)
6968 and then not Is_Generic_Type (Root_Type (Full_Parent)))
6970 -- Copy and adjust declaration to provide a completion for what
6971 -- is originally a private declaration. Indicate that full view
6972 -- is internally generated.
6974 Set_Comes_From_Source (Full_N, False);
6975 Set_Comes_From_Source (Full_Der, False);
6976 Set_Parent (Full_Der, Full_N);
6977 Set_Defining_Identifier (Full_N, Full_Der);
6979 -- If there are no constraints, adjust the subtype mark
6981 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
6982 N_Subtype_Indication
6984 Set_Subtype_Indication
6985 (Type_Definition (Full_N),
6986 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
6989 Insert_After (N, Full_N);
6991 -- Build full view of derived type from full view of parent which
6992 -- is now installed. Subprograms have been derived on the partial
6993 -- view, the completion does not derive them anew.
6995 if Ekind (Full_Parent) in Record_Kind then
6997 -- If parent type is tagged, the completion inherits the proper
6998 -- primitive operations.
7000 if Is_Tagged_Type (Parent_Type) then
7001 Build_Derived_Record_Type
7002 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7004 Build_Derived_Record_Type
7005 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7010 (Full_N, Full_Parent, Full_Der,
7011 Is_Completion => False, Derive_Subps => False);
7014 -- The full declaration has been introduced into the tree and
7015 -- processed in the step above. It should not be analyzed again
7016 -- (when encountered later in the current list of declarations)
7017 -- to prevent spurious name conflicts. The full entity remains
7020 Set_Analyzed (Full_N);
7024 Make_Defining_Identifier (Sloc (Derived_Type),
7025 Chars => Chars (Derived_Type));
7026 Set_Is_Itype (Full_Der);
7027 Set_Associated_Node_For_Itype (Full_Der, N);
7028 Set_Parent (Full_Der, N);
7030 (N, Full_Parent, Full_Der,
7031 Is_Completion => False, Derive_Subps => False);
7034 Set_Has_Private_Declaration (Full_Der);
7035 Set_Has_Private_Declaration (Derived_Type);
7037 Set_Scope (Full_Der, Scope (Derived_Type));
7038 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7039 Set_Has_Size_Clause (Full_Der, False);
7040 Set_Has_Alignment_Clause (Full_Der, False);
7041 Set_Has_Delayed_Freeze (Full_Der);
7042 Set_Is_Frozen (Full_Der, False);
7043 Set_Freeze_Node (Full_Der, Empty);
7044 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7045 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7047 -- The convention on the base type may be set in the private part
7048 -- and not propagated to the subtype until later, so we obtain the
7049 -- convention from the base type of the parent.
7051 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7054 -- Start of processing for Build_Derived_Private_Type
7057 if Is_Tagged_Type (Parent_Type) then
7058 Full_P := Full_View (Parent_Type);
7060 -- A type extension of a type with unknown discriminants is an
7061 -- indefinite type that the back-end cannot handle directly.
7062 -- We treat it as a private type, and build a completion that is
7063 -- derived from the full view of the parent, and hopefully has
7064 -- known discriminants.
7066 -- If the full view of the parent type has an underlying record view,
7067 -- use it to generate the underlying record view of this derived type
7068 -- (required for chains of derivations with unknown discriminants).
7070 -- Minor optimization: we avoid the generation of useless underlying
7071 -- record view entities if the private type declaration has unknown
7072 -- discriminants but its corresponding full view has no
7075 if Has_Unknown_Discriminants (Parent_Type)
7076 and then Present (Full_P)
7077 and then (Has_Discriminants (Full_P)
7078 or else Present (Underlying_Record_View (Full_P)))
7079 and then not In_Open_Scopes (Par_Scope)
7080 and then Expander_Active
7083 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7084 New_Ext : constant Node_Id :=
7086 (Record_Extension_Part (Type_Definition (N)));
7090 Build_Derived_Record_Type
7091 (N, Parent_Type, Derived_Type, Derive_Subps);
7093 -- Build anonymous completion, as a derivation from the full
7094 -- view of the parent. This is not a completion in the usual
7095 -- sense, because the current type is not private.
7098 Make_Full_Type_Declaration (Loc,
7099 Defining_Identifier => Full_Der,
7101 Make_Derived_Type_Definition (Loc,
7102 Subtype_Indication =>
7104 (Subtype_Indication (Type_Definition (N))),
7105 Record_Extension_Part => New_Ext));
7107 -- If the parent type has an underlying record view, use it
7108 -- here to build the new underlying record view.
7110 if Present (Underlying_Record_View (Full_P)) then
7112 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7114 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7115 Underlying_Record_View (Full_P));
7118 Install_Private_Declarations (Par_Scope);
7119 Install_Visible_Declarations (Par_Scope);
7120 Insert_Before (N, Decl);
7122 -- Mark entity as an underlying record view before analysis,
7123 -- to avoid generating the list of its primitive operations
7124 -- (which is not really required for this entity) and thus
7125 -- prevent spurious errors associated with missing overriding
7126 -- of abstract primitives (overridden only for Derived_Type).
7128 Set_Ekind (Full_Der, E_Record_Type);
7129 Set_Is_Underlying_Record_View (Full_Der);
7130 Set_Default_SSO (Full_Der);
7134 pragma Assert (Has_Discriminants (Full_Der)
7135 and then not Has_Unknown_Discriminants (Full_Der));
7137 Uninstall_Declarations (Par_Scope);
7139 -- Freeze the underlying record view, to prevent generation of
7140 -- useless dispatching information, which is simply shared with
7141 -- the real derived type.
7143 Set_Is_Frozen (Full_Der);
7145 -- If the derived type has access discriminants, create
7146 -- references to their anonymous types now, to prevent
7147 -- back-end problems when their first use is in generated
7148 -- bodies of primitives.
7154 E := First_Entity (Full_Der);
7156 while Present (E) loop
7157 if Ekind (E) = E_Discriminant
7158 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7160 Build_Itype_Reference (Etype (E), Decl);
7167 -- Set up links between real entity and underlying record view
7169 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7170 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7173 -- If discriminants are known, build derived record
7176 Build_Derived_Record_Type
7177 (N, Parent_Type, Derived_Type, Derive_Subps);
7182 elsif Has_Discriminants (Parent_Type) then
7184 -- Build partial view of derived type from partial view of parent.
7185 -- This must be done before building the full derivation because the
7186 -- second derivation will modify the discriminants of the first and
7187 -- the discriminants are chained with the rest of the components in
7188 -- the full derivation.
7190 Build_Derived_Record_Type
7191 (N, Parent_Type, Derived_Type, Derive_Subps);
7193 -- Build the full derivation if this is not the anonymous derived
7194 -- base type created by Build_Derived_Record_Type in the constrained
7195 -- case (see point 5. of its head comment) since we build it for the
7196 -- derived subtype. And skip it for protected types altogether, as
7197 -- gigi does not use these types directly.
7199 if Present (Full_View (Parent_Type))
7200 and then not Is_Itype (Derived_Type)
7201 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7204 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7206 Last_Discr : Entity_Id;
7209 -- If this is not a completion, construct the implicit full
7210 -- view by deriving from the full view of the parent type.
7211 -- But if this is a completion, the derived private type
7212 -- being built is a full view and the full derivation can
7213 -- only be its underlying full view.
7215 Build_Full_Derivation;
7217 if not Is_Completion then
7218 Set_Full_View (Derived_Type, Full_Der);
7220 Set_Underlying_Full_View (Derived_Type, Full_Der);
7223 if not Is_Base_Type (Derived_Type) then
7224 Set_Full_View (Der_Base, Base_Type (Full_Der));
7227 -- Copy the discriminant list from full view to the partial
7228 -- view (base type and its subtype). Gigi requires that the
7229 -- partial and full views have the same discriminants.
7231 -- Note that since the partial view points to discriminants
7232 -- in the full view, their scope will be that of the full
7233 -- view. This might cause some front end problems and need
7236 Discr := First_Discriminant (Base_Type (Full_Der));
7237 Set_First_Entity (Der_Base, Discr);
7240 Last_Discr := Discr;
7241 Next_Discriminant (Discr);
7242 exit when No (Discr);
7245 Set_Last_Entity (Der_Base, Last_Discr);
7246 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7247 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7249 Set_Stored_Constraint
7250 (Full_Der, Stored_Constraint (Derived_Type));
7254 elsif Present (Full_View (Parent_Type))
7255 and then Has_Discriminants (Full_View (Parent_Type))
7257 if Has_Unknown_Discriminants (Parent_Type)
7258 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7259 N_Subtype_Indication
7262 ("cannot constrain type with unknown discriminants",
7263 Subtype_Indication (Type_Definition (N)));
7267 -- If this is not a completion, construct the implicit full view by
7268 -- deriving from the full view of the parent type. But if this is a
7269 -- completion, the derived private type being built is a full view
7270 -- and the full derivation can only be its underlying full view.
7272 Build_Full_Derivation;
7274 if not Is_Completion then
7275 Set_Full_View (Derived_Type, Full_Der);
7277 Set_Underlying_Full_View (Derived_Type, Full_Der);
7280 -- In any case, the primitive operations are inherited from the
7281 -- parent type, not from the internal full view.
7283 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7285 if Derive_Subps then
7286 Derive_Subprograms (Parent_Type, Derived_Type);
7289 Set_Stored_Constraint (Derived_Type, No_Elist);
7291 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7294 -- Untagged type, No discriminants on either view
7296 if Nkind (Subtype_Indication (Type_Definition (N))) =
7297 N_Subtype_Indication
7300 ("illegal constraint on type without discriminants", N);
7303 if Present (Discriminant_Specifications (N))
7304 and then Present (Full_View (Parent_Type))
7305 and then not Is_Tagged_Type (Full_View (Parent_Type))
7307 Error_Msg_N ("cannot add discriminants to untagged type", N);
7310 Set_Stored_Constraint (Derived_Type, No_Elist);
7311 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7312 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7313 Set_Has_Controlled_Component
7314 (Derived_Type, Has_Controlled_Component
7317 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7319 if not Is_Controlled (Parent_Type) then
7320 Set_Finalize_Storage_Only
7321 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7324 -- If this is not a completion, construct the implicit full view by
7325 -- deriving from the full view of the parent type.
7327 -- ??? If the parent is untagged private and its completion is
7328 -- tagged, this mechanism will not work because we cannot derive from
7329 -- the tagged full view unless we have an extension.
7331 if Present (Full_View (Parent_Type))
7332 and then not Is_Tagged_Type (Full_View (Parent_Type))
7333 and then not Is_Completion
7335 Build_Full_Derivation;
7336 Set_Full_View (Derived_Type, Full_Der);
7340 Set_Has_Unknown_Discriminants (Derived_Type,
7341 Has_Unknown_Discriminants (Parent_Type));
7343 if Is_Private_Type (Derived_Type) then
7344 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7347 -- If the parent base type is in scope, add the derived type to its
7348 -- list of private dependents, because its full view may become
7349 -- visible subsequently (in a nested private part, a body, or in a
7350 -- further child unit).
7352 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7353 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7355 -- Check for unusual case where a type completed by a private
7356 -- derivation occurs within a package nested in a child unit, and
7357 -- the parent is declared in an ancestor.
7359 if Is_Child_Unit (Scope (Current_Scope))
7360 and then Is_Completion
7361 and then In_Private_Part (Current_Scope)
7362 and then Scope (Parent_Type) /= Current_Scope
7364 -- Note that if the parent has a completion in the private part,
7365 -- (which is itself a derivation from some other private type)
7366 -- it is that completion that is visible, there is no full view
7367 -- available, and no special processing is needed.
7369 and then Present (Full_View (Parent_Type))
7371 -- In this case, the full view of the parent type will become
7372 -- visible in the body of the enclosing child, and only then will
7373 -- the current type be possibly non-private. Build an underlying
7374 -- full view that will be installed when the enclosing child body
7377 if Present (Underlying_Full_View (Derived_Type)) then
7378 Full_Der := Underlying_Full_View (Derived_Type);
7380 Build_Full_Derivation;
7381 Set_Underlying_Full_View (Derived_Type, Full_Der);
7384 -- The full view will be used to swap entities on entry/exit to
7385 -- the body, and must appear in the entity list for the package.
7387 Append_Entity (Full_Der, Scope (Derived_Type));
7390 end Build_Derived_Private_Type;
7392 -------------------------------
7393 -- Build_Derived_Record_Type --
7394 -------------------------------
7398 -- Ideally we would like to use the same model of type derivation for
7399 -- tagged and untagged record types. Unfortunately this is not quite
7400 -- possible because the semantics of representation clauses is different
7401 -- for tagged and untagged records under inheritance. Consider the
7404 -- type R (...) is [tagged] record ... end record;
7405 -- type T (...) is new R (...) [with ...];
7407 -- The representation clauses for T can specify a completely different
7408 -- record layout from R's. Hence the same component can be placed in two
7409 -- very different positions in objects of type T and R. If R and T are
7410 -- tagged types, representation clauses for T can only specify the layout
7411 -- of non inherited components, thus components that are common in R and T
7412 -- have the same position in objects of type R and T.
7414 -- This has two implications. The first is that the entire tree for R's
7415 -- declaration needs to be copied for T in the untagged case, so that T
7416 -- can be viewed as a record type of its own with its own representation
7417 -- clauses. The second implication is the way we handle discriminants.
7418 -- Specifically, in the untagged case we need a way to communicate to Gigi
7419 -- what are the real discriminants in the record, while for the semantics
7420 -- we need to consider those introduced by the user to rename the
7421 -- discriminants in the parent type. This is handled by introducing the
7422 -- notion of stored discriminants. See below for more.
7424 -- Fortunately the way regular components are inherited can be handled in
7425 -- the same way in tagged and untagged types.
7427 -- To complicate things a bit more the private view of a private extension
7428 -- cannot be handled in the same way as the full view (for one thing the
7429 -- semantic rules are somewhat different). We will explain what differs
7432 -- 2. DISCRIMINANTS UNDER INHERITANCE
7434 -- The semantic rules governing the discriminants of derived types are
7437 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7438 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7440 -- If parent type has discriminants, then the discriminants that are
7441 -- declared in the derived type are [3.4 (11)]:
7443 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7446 -- o Otherwise, each discriminant of the parent type (implicitly declared
7447 -- in the same order with the same specifications). In this case, the
7448 -- discriminants are said to be "inherited", or if unknown in the parent
7449 -- are also unknown in the derived type.
7451 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7453 -- o The parent subtype must be constrained;
7455 -- o If the parent type is not a tagged type, then each discriminant of
7456 -- the derived type must be used in the constraint defining a parent
7457 -- subtype. [Implementation note: This ensures that the new discriminant
7458 -- can share storage with an existing discriminant.]
7460 -- For the derived type each discriminant of the parent type is either
7461 -- inherited, constrained to equal some new discriminant of the derived
7462 -- type, or constrained to the value of an expression.
7464 -- When inherited or constrained to equal some new discriminant, the
7465 -- parent discriminant and the discriminant of the derived type are said
7468 -- If a discriminant of the parent type is constrained to a specific value
7469 -- in the derived type definition, then the discriminant is said to be
7470 -- "specified" by that derived type definition.
7472 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7474 -- We have spoken about stored discriminants in point 1 (introduction)
7475 -- above. There are two sort of stored discriminants: implicit and
7476 -- explicit. As long as the derived type inherits the same discriminants as
7477 -- the root record type, stored discriminants are the same as regular
7478 -- discriminants, and are said to be implicit. However, if any discriminant
7479 -- in the root type was renamed in the derived type, then the derived
7480 -- type will contain explicit stored discriminants. Explicit stored
7481 -- discriminants are discriminants in addition to the semantically visible
7482 -- discriminants defined for the derived type. Stored discriminants are
7483 -- used by Gigi to figure out what are the physical discriminants in
7484 -- objects of the derived type (see precise definition in einfo.ads).
7485 -- As an example, consider the following:
7487 -- type R (D1, D2, D3 : Int) is record ... end record;
7488 -- type T1 is new R;
7489 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7490 -- type T3 is new T2;
7491 -- type T4 (Y : Int) is new T3 (Y, 99);
7493 -- The following table summarizes the discriminants and stored
7494 -- discriminants in R and T1 through T4.
7496 -- Type Discrim Stored Discrim Comment
7497 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7498 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7499 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7500 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7501 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7503 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7504 -- find the corresponding discriminant in the parent type, while
7505 -- Original_Record_Component (abbreviated ORC below), the actual physical
7506 -- component that is renamed. Finally the field Is_Completely_Hidden
7507 -- (abbreviated ICH below) is set for all explicit stored discriminants
7508 -- (see einfo.ads for more info). For the above example this gives:
7510 -- Discrim CD ORC ICH
7511 -- ^^^^^^^ ^^ ^^^ ^^^
7512 -- D1 in R empty itself no
7513 -- D2 in R empty itself no
7514 -- D3 in R empty itself no
7516 -- D1 in T1 D1 in R itself no
7517 -- D2 in T1 D2 in R itself no
7518 -- D3 in T1 D3 in R itself no
7520 -- X1 in T2 D3 in T1 D3 in T2 no
7521 -- X2 in T2 D1 in T1 D1 in T2 no
7522 -- D1 in T2 empty itself yes
7523 -- D2 in T2 empty itself yes
7524 -- D3 in T2 empty itself yes
7526 -- X1 in T3 X1 in T2 D3 in T3 no
7527 -- X2 in T3 X2 in T2 D1 in T3 no
7528 -- D1 in T3 empty itself yes
7529 -- D2 in T3 empty itself yes
7530 -- D3 in T3 empty itself yes
7532 -- Y in T4 X1 in T3 D3 in T3 no
7533 -- D1 in T3 empty itself yes
7534 -- D2 in T3 empty itself yes
7535 -- D3 in T3 empty itself yes
7537 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7539 -- Type derivation for tagged types is fairly straightforward. If no
7540 -- discriminants are specified by the derived type, these are inherited
7541 -- from the parent. No explicit stored discriminants are ever necessary.
7542 -- The only manipulation that is done to the tree is that of adding a
7543 -- _parent field with parent type and constrained to the same constraint
7544 -- specified for the parent in the derived type definition. For instance:
7546 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7547 -- type T1 is new R with null record;
7548 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7550 -- are changed into:
7552 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7553 -- _parent : R (D1, D2, D3);
7556 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7557 -- _parent : T1 (X2, 88, X1);
7560 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7561 -- ORC and ICH fields are:
7563 -- Discrim CD ORC ICH
7564 -- ^^^^^^^ ^^ ^^^ ^^^
7565 -- D1 in R empty itself no
7566 -- D2 in R empty itself no
7567 -- D3 in R empty itself no
7569 -- D1 in T1 D1 in R D1 in R no
7570 -- D2 in T1 D2 in R D2 in R no
7571 -- D3 in T1 D3 in R D3 in R no
7573 -- X1 in T2 D3 in T1 D3 in R no
7574 -- X2 in T2 D1 in T1 D1 in R no
7576 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7578 -- Regardless of whether we dealing with a tagged or untagged type
7579 -- we will transform all derived type declarations of the form
7581 -- type T is new R (...) [with ...];
7583 -- subtype S is R (...);
7584 -- type T is new S [with ...];
7586 -- type BT is new R [with ...];
7587 -- subtype T is BT (...);
7589 -- That is, the base derived type is constrained only if it has no
7590 -- discriminants. The reason for doing this is that GNAT's semantic model
7591 -- assumes that a base type with discriminants is unconstrained.
7593 -- Note that, strictly speaking, the above transformation is not always
7594 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7596 -- procedure B34011A is
7597 -- type REC (D : integer := 0) is record
7602 -- type T6 is new Rec;
7603 -- function F return T6;
7608 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7611 -- The definition of Q6.U is illegal. However transforming Q6.U into
7613 -- type BaseU is new T6;
7614 -- subtype U is BaseU (Q6.F.I)
7616 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7617 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7618 -- the transformation described above.
7620 -- There is another instance where the above transformation is incorrect.
7624 -- type Base (D : Integer) is tagged null record;
7625 -- procedure P (X : Base);
7627 -- type Der is new Base (2) with null record;
7628 -- procedure P (X : Der);
7631 -- Then the above transformation turns this into
7633 -- type Der_Base is new Base with null record;
7634 -- -- procedure P (X : Base) is implicitly inherited here
7635 -- -- as procedure P (X : Der_Base).
7637 -- subtype Der is Der_Base (2);
7638 -- procedure P (X : Der);
7639 -- -- The overriding of P (X : Der_Base) is illegal since we
7640 -- -- have a parameter conformance problem.
7642 -- To get around this problem, after having semantically processed Der_Base
7643 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7644 -- Discriminant_Constraint from Der so that when parameter conformance is
7645 -- checked when P is overridden, no semantic errors are flagged.
7647 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7649 -- Regardless of whether we are dealing with a tagged or untagged type
7650 -- we will transform all derived type declarations of the form
7652 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7653 -- type T is new R [with ...];
7655 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7657 -- The reason for such transformation is that it allows us to implement a
7658 -- very clean form of component inheritance as explained below.
7660 -- Note that this transformation is not achieved by direct tree rewriting
7661 -- and manipulation, but rather by redoing the semantic actions that the
7662 -- above transformation will entail. This is done directly in routine
7663 -- Inherit_Components.
7665 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7667 -- In both tagged and untagged derived types, regular non discriminant
7668 -- components are inherited in the derived type from the parent type. In
7669 -- the absence of discriminants component, inheritance is straightforward
7670 -- as components can simply be copied from the parent.
7672 -- If the parent has discriminants, inheriting components constrained with
7673 -- these discriminants requires caution. Consider the following example:
7675 -- type R (D1, D2 : Positive) is [tagged] record
7676 -- S : String (D1 .. D2);
7679 -- type T1 is new R [with null record];
7680 -- type T2 (X : positive) is new R (1, X) [with null record];
7682 -- As explained in 6. above, T1 is rewritten as
7683 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7684 -- which makes the treatment for T1 and T2 identical.
7686 -- What we want when inheriting S, is that references to D1 and D2 in R are
7687 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7688 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7689 -- with either discriminant references in the derived type or expressions.
7690 -- This replacement is achieved as follows: before inheriting R's
7691 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7692 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7693 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7694 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7695 -- by String (1 .. X).
7697 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7699 -- We explain here the rules governing private type extensions relevant to
7700 -- type derivation. These rules are explained on the following example:
7702 -- type D [(...)] is new A [(...)] with private; <-- partial view
7703 -- type D [(...)] is new P [(...)] with null record; <-- full view
7705 -- Type A is called the ancestor subtype of the private extension.
7706 -- Type P is the parent type of the full view of the private extension. It
7707 -- must be A or a type derived from A.
7709 -- The rules concerning the discriminants of private type extensions are
7712 -- o If a private extension inherits known discriminants from the ancestor
7713 -- subtype, then the full view must also inherit its discriminants from
7714 -- the ancestor subtype and the parent subtype of the full view must be
7715 -- constrained if and only if the ancestor subtype is constrained.
7717 -- o If a partial view has unknown discriminants, then the full view may
7718 -- define a definite or an indefinite subtype, with or without
7721 -- o If a partial view has neither known nor unknown discriminants, then
7722 -- the full view must define a definite subtype.
7724 -- o If the ancestor subtype of a private extension has constrained
7725 -- discriminants, then the parent subtype of the full view must impose a
7726 -- statically matching constraint on those discriminants.
7728 -- This means that only the following forms of private extensions are
7731 -- type D is new A with private; <-- partial view
7732 -- type D is new P with null record; <-- full view
7734 -- If A has no discriminants than P has no discriminants, otherwise P must
7735 -- inherit A's discriminants.
7737 -- type D is new A (...) with private; <-- partial view
7738 -- type D is new P (:::) with null record; <-- full view
7740 -- P must inherit A's discriminants and (...) and (:::) must statically
7743 -- subtype A is R (...);
7744 -- type D is new A with private; <-- partial view
7745 -- type D is new P with null record; <-- full view
7747 -- P must have inherited R's discriminants and must be derived from A or
7748 -- any of its subtypes.
7750 -- type D (..) is new A with private; <-- partial view
7751 -- type D (..) is new P [(:::)] with null record; <-- full view
7753 -- No specific constraints on P's discriminants or constraint (:::).
7754 -- Note that A can be unconstrained, but the parent subtype P must either
7755 -- be constrained or (:::) must be present.
7757 -- type D (..) is new A [(...)] with private; <-- partial view
7758 -- type D (..) is new P [(:::)] with null record; <-- full view
7760 -- P's constraints on A's discriminants must statically match those
7761 -- imposed by (...).
7763 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
7765 -- The full view of a private extension is handled exactly as described
7766 -- above. The model chose for the private view of a private extension is
7767 -- the same for what concerns discriminants (i.e. they receive the same
7768 -- treatment as in the tagged case). However, the private view of the
7769 -- private extension always inherits the components of the parent base,
7770 -- without replacing any discriminant reference. Strictly speaking this is
7771 -- incorrect. However, Gigi never uses this view to generate code so this
7772 -- is a purely semantic issue. In theory, a set of transformations similar
7773 -- to those given in 5. and 6. above could be applied to private views of
7774 -- private extensions to have the same model of component inheritance as
7775 -- for non private extensions. However, this is not done because it would
7776 -- further complicate private type processing. Semantically speaking, this
7777 -- leaves us in an uncomfortable situation. As an example consider:
7780 -- type R (D : integer) is tagged record
7781 -- S : String (1 .. D);
7783 -- procedure P (X : R);
7784 -- type T is new R (1) with private;
7786 -- type T is new R (1) with null record;
7789 -- This is transformed into:
7792 -- type R (D : integer) is tagged record
7793 -- S : String (1 .. D);
7795 -- procedure P (X : R);
7796 -- type T is new R (1) with private;
7798 -- type BaseT is new R with null record;
7799 -- subtype T is BaseT (1);
7802 -- (strictly speaking the above is incorrect Ada)
7804 -- From the semantic standpoint the private view of private extension T
7805 -- should be flagged as constrained since one can clearly have
7809 -- in a unit withing Pack. However, when deriving subprograms for the
7810 -- private view of private extension T, T must be seen as unconstrained
7811 -- since T has discriminants (this is a constraint of the current
7812 -- subprogram derivation model). Thus, when processing the private view of
7813 -- a private extension such as T, we first mark T as unconstrained, we
7814 -- process it, we perform program derivation and just before returning from
7815 -- Build_Derived_Record_Type we mark T as constrained.
7817 -- ??? Are there are other uncomfortable cases that we will have to
7820 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7822 -- Types that are derived from a visible record type and have a private
7823 -- extension present other peculiarities. They behave mostly like private
7824 -- types, but if they have primitive operations defined, these will not
7825 -- have the proper signatures for further inheritance, because other
7826 -- primitive operations will use the implicit base that we define for
7827 -- private derivations below. This affect subprogram inheritance (see
7828 -- Derive_Subprograms for details). We also derive the implicit base from
7829 -- the base type of the full view, so that the implicit base is a record
7830 -- type and not another private type, This avoids infinite loops.
7832 procedure Build_Derived_Record_Type
7834 Parent_Type : Entity_Id;
7835 Derived_Type : Entity_Id;
7836 Derive_Subps : Boolean := True)
7838 Discriminant_Specs : constant Boolean :=
7839 Present (Discriminant_Specifications (N));
7840 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7841 Loc : constant Source_Ptr := Sloc (N);
7842 Private_Extension : constant Boolean :=
7843 Nkind (N) = N_Private_Extension_Declaration;
7844 Assoc_List : Elist_Id;
7845 Constraint_Present : Boolean;
7847 Discrim : Entity_Id;
7849 Inherit_Discrims : Boolean := False;
7850 Last_Discrim : Entity_Id;
7851 New_Base : Entity_Id;
7853 New_Discrs : Elist_Id;
7854 New_Indic : Node_Id;
7855 Parent_Base : Entity_Id;
7856 Save_Etype : Entity_Id;
7857 Save_Discr_Constr : Elist_Id;
7858 Save_Next_Entity : Entity_Id;
7861 Discs : Elist_Id := New_Elmt_List;
7862 -- An empty Discs list means that there were no constraints in the
7863 -- subtype indication or that there was an error processing it.
7866 if Ekind (Parent_Type) = E_Record_Type_With_Private
7867 and then Present (Full_View (Parent_Type))
7868 and then Has_Discriminants (Parent_Type)
7870 Parent_Base := Base_Type (Full_View (Parent_Type));
7872 Parent_Base := Base_Type (Parent_Type);
7875 -- AI05-0115 : if this is a derivation from a private type in some
7876 -- other scope that may lead to invisible components for the derived
7877 -- type, mark it accordingly.
7879 if Is_Private_Type (Parent_Type) then
7880 if Scope (Parent_Type) = Scope (Derived_Type) then
7883 elsif In_Open_Scopes (Scope (Parent_Type))
7884 and then In_Private_Part (Scope (Parent_Type))
7889 Set_Has_Private_Ancestor (Derived_Type);
7893 Set_Has_Private_Ancestor
7894 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7897 -- Before we start the previously documented transformations, here is
7898 -- little fix for size and alignment of tagged types. Normally when we
7899 -- derive type D from type P, we copy the size and alignment of P as the
7900 -- default for D, and in the absence of explicit representation clauses
7901 -- for D, the size and alignment are indeed the same as the parent.
7903 -- But this is wrong for tagged types, since fields may be added, and
7904 -- the default size may need to be larger, and the default alignment may
7905 -- need to be larger.
7907 -- We therefore reset the size and alignment fields in the tagged case.
7908 -- Note that the size and alignment will in any case be at least as
7909 -- large as the parent type (since the derived type has a copy of the
7910 -- parent type in the _parent field)
7912 -- The type is also marked as being tagged here, which is needed when
7913 -- processing components with a self-referential anonymous access type
7914 -- in the call to Check_Anonymous_Access_Components below. Note that
7915 -- this flag is also set later on for completeness.
7918 Set_Is_Tagged_Type (Derived_Type);
7919 Init_Size_Align (Derived_Type);
7922 -- STEP 0a: figure out what kind of derived type declaration we have
7924 if Private_Extension then
7926 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7927 Set_Default_SSO (Derived_Type);
7930 Type_Def := Type_Definition (N);
7932 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7933 -- Parent_Base can be a private type or private extension. However,
7934 -- for tagged types with an extension the newly added fields are
7935 -- visible and hence the Derived_Type is always an E_Record_Type.
7936 -- (except that the parent may have its own private fields).
7937 -- For untagged types we preserve the Ekind of the Parent_Base.
7939 if Present (Record_Extension_Part (Type_Def)) then
7940 Set_Ekind (Derived_Type, E_Record_Type);
7941 Set_Default_SSO (Derived_Type);
7943 -- Create internal access types for components with anonymous
7946 if Ada_Version >= Ada_2005 then
7947 Check_Anonymous_Access_Components
7948 (N, Derived_Type, Derived_Type,
7949 Component_List (Record_Extension_Part (Type_Def)));
7953 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7957 -- Indic can either be an N_Identifier if the subtype indication
7958 -- contains no constraint or an N_Subtype_Indication if the subtype
7959 -- indication has a constraint.
7961 Indic := Subtype_Indication (Type_Def);
7962 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
7964 -- Check that the type has visible discriminants. The type may be
7965 -- a private type with unknown discriminants whose full view has
7966 -- discriminants which are invisible.
7968 if Constraint_Present then
7969 if not Has_Discriminants (Parent_Base)
7971 (Has_Unknown_Discriminants (Parent_Base)
7972 and then Is_Private_Type (Parent_Base))
7975 ("invalid constraint: type has no discriminant",
7976 Constraint (Indic));
7978 Constraint_Present := False;
7979 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7981 elsif Is_Constrained (Parent_Type) then
7983 ("invalid constraint: parent type is already constrained",
7984 Constraint (Indic));
7986 Constraint_Present := False;
7987 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
7991 -- STEP 0b: If needed, apply transformation given in point 5. above
7993 if not Private_Extension
7994 and then Has_Discriminants (Parent_Type)
7995 and then not Discriminant_Specs
7996 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
7998 -- First, we must analyze the constraint (see comment in point 5.)
7999 -- The constraint may come from the subtype indication of the full
8002 if Constraint_Present then
8003 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8005 -- If there is no explicit constraint, there might be one that is
8006 -- inherited from a constrained parent type. In that case verify that
8007 -- it conforms to the constraint in the partial view. In perverse
8008 -- cases the parent subtypes of the partial and full view can have
8009 -- different constraints.
8011 elsif Present (Stored_Constraint (Parent_Type)) then
8012 New_Discrs := Stored_Constraint (Parent_Type);
8015 New_Discrs := No_Elist;
8018 if Has_Discriminants (Derived_Type)
8019 and then Has_Private_Declaration (Derived_Type)
8020 and then Present (Discriminant_Constraint (Derived_Type))
8021 and then Present (New_Discrs)
8023 -- Verify that constraints of the full view statically match
8024 -- those given in the partial view.
8030 C1 := First_Elmt (New_Discrs);
8031 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8032 while Present (C1) and then Present (C2) loop
8033 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8035 (Is_OK_Static_Expression (Node (C1))
8036 and then Is_OK_Static_Expression (Node (C2))
8038 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8043 if Constraint_Present then
8045 ("constraint not conformant to previous declaration",
8049 ("constraint of full view is incompatible "
8050 & "with partial view", N);
8060 -- Insert and analyze the declaration for the unconstrained base type
8062 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8065 Make_Full_Type_Declaration (Loc,
8066 Defining_Identifier => New_Base,
8068 Make_Derived_Type_Definition (Loc,
8069 Abstract_Present => Abstract_Present (Type_Def),
8070 Limited_Present => Limited_Present (Type_Def),
8071 Subtype_Indication =>
8072 New_Occurrence_Of (Parent_Base, Loc),
8073 Record_Extension_Part =>
8074 Relocate_Node (Record_Extension_Part (Type_Def)),
8075 Interface_List => Interface_List (Type_Def)));
8077 Set_Parent (New_Decl, Parent (N));
8078 Mark_Rewrite_Insertion (New_Decl);
8079 Insert_Before (N, New_Decl);
8081 -- In the extension case, make sure ancestor is frozen appropriately
8082 -- (see also non-discriminated case below).
8084 if Present (Record_Extension_Part (Type_Def))
8085 or else Is_Interface (Parent_Base)
8087 Freeze_Before (New_Decl, Parent_Type);
8090 -- Note that this call passes False for the Derive_Subps parameter
8091 -- because subprogram derivation is deferred until after creating
8092 -- the subtype (see below).
8095 (New_Decl, Parent_Base, New_Base,
8096 Is_Completion => False, Derive_Subps => False);
8098 -- ??? This needs re-examination to determine whether the
8099 -- above call can simply be replaced by a call to Analyze.
8101 Set_Analyzed (New_Decl);
8103 -- Insert and analyze the declaration for the constrained subtype
8105 if Constraint_Present then
8107 Make_Subtype_Indication (Loc,
8108 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8109 Constraint => Relocate_Node (Constraint (Indic)));
8113 Constr_List : constant List_Id := New_List;
8118 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8119 while Present (C) loop
8122 -- It is safe here to call New_Copy_Tree since we called
8123 -- Force_Evaluation on each constraint previously
8124 -- in Build_Discriminant_Constraints.
8126 Append (New_Copy_Tree (Expr), To => Constr_List);
8132 Make_Subtype_Indication (Loc,
8133 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8135 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8140 Make_Subtype_Declaration (Loc,
8141 Defining_Identifier => Derived_Type,
8142 Subtype_Indication => New_Indic));
8146 -- Derivation of subprograms must be delayed until the full subtype
8147 -- has been established, to ensure proper overriding of subprograms
8148 -- inherited by full types. If the derivations occurred as part of
8149 -- the call to Build_Derived_Type above, then the check for type
8150 -- conformance would fail because earlier primitive subprograms
8151 -- could still refer to the full type prior the change to the new
8152 -- subtype and hence would not match the new base type created here.
8153 -- Subprograms are not derived, however, when Derive_Subps is False
8154 -- (since otherwise there could be redundant derivations).
8156 if Derive_Subps then
8157 Derive_Subprograms (Parent_Type, Derived_Type);
8160 -- For tagged types the Discriminant_Constraint of the new base itype
8161 -- is inherited from the first subtype so that no subtype conformance
8162 -- problem arise when the first subtype overrides primitive
8163 -- operations inherited by the implicit base type.
8166 Set_Discriminant_Constraint
8167 (New_Base, Discriminant_Constraint (Derived_Type));
8173 -- If we get here Derived_Type will have no discriminants or it will be
8174 -- a discriminated unconstrained base type.
8176 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8180 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8181 -- The declaration of a specific descendant of an interface type
8182 -- freezes the interface type (RM 13.14).
8184 if not Private_Extension or else Is_Interface (Parent_Base) then
8185 Freeze_Before (N, Parent_Type);
8188 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8189 -- cannot be declared at a deeper level than its parent type is
8190 -- removed. The check on derivation within a generic body is also
8191 -- relaxed, but there's a restriction that a derived tagged type
8192 -- cannot be declared in a generic body if it's derived directly
8193 -- or indirectly from a formal type of that generic.
8195 if Ada_Version >= Ada_2005 then
8196 if Present (Enclosing_Generic_Body (Derived_Type)) then
8198 Ancestor_Type : Entity_Id;
8201 -- Check to see if any ancestor of the derived type is a
8204 Ancestor_Type := Parent_Type;
8205 while not Is_Generic_Type (Ancestor_Type)
8206 and then Etype (Ancestor_Type) /= Ancestor_Type
8208 Ancestor_Type := Etype (Ancestor_Type);
8211 -- If the derived type does have a formal type as an
8212 -- ancestor, then it's an error if the derived type is
8213 -- declared within the body of the generic unit that
8214 -- declares the formal type in its generic formal part. It's
8215 -- sufficient to check whether the ancestor type is declared
8216 -- inside the same generic body as the derived type (such as
8217 -- within a nested generic spec), in which case the
8218 -- derivation is legal. If the formal type is declared
8219 -- outside of that generic body, then it's guaranteed that
8220 -- the derived type is declared within the generic body of
8221 -- the generic unit declaring the formal type.
8223 if Is_Generic_Type (Ancestor_Type)
8224 and then Enclosing_Generic_Body (Ancestor_Type) /=
8225 Enclosing_Generic_Body (Derived_Type)
8228 ("parent type of& must not be descendant of formal type"
8229 & " of an enclosing generic body",
8230 Indic, Derived_Type);
8235 elsif Type_Access_Level (Derived_Type) /=
8236 Type_Access_Level (Parent_Type)
8237 and then not Is_Generic_Type (Derived_Type)
8239 if Is_Controlled (Parent_Type) then
8241 ("controlled type must be declared at the library level",
8245 ("type extension at deeper accessibility level than parent",
8251 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8254 and then GB /= Enclosing_Generic_Body (Parent_Base)
8257 ("parent type of& must not be outside generic body"
8259 Indic, Derived_Type);
8265 -- Ada 2005 (AI-251)
8267 if Ada_Version >= Ada_2005 and then Is_Tagged then
8269 -- "The declaration of a specific descendant of an interface type
8270 -- freezes the interface type" (RM 13.14).
8275 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8276 Iface := First (Interface_List (Type_Def));
8277 while Present (Iface) loop
8278 Freeze_Before (N, Etype (Iface));
8285 -- STEP 1b : preliminary cleanup of the full view of private types
8287 -- If the type is already marked as having discriminants, then it's the
8288 -- completion of a private type or private extension and we need to
8289 -- retain the discriminants from the partial view if the current
8290 -- declaration has Discriminant_Specifications so that we can verify
8291 -- conformance. However, we must remove any existing components that
8292 -- were inherited from the parent (and attached in Copy_And_Swap)
8293 -- because the full type inherits all appropriate components anyway, and
8294 -- we do not want the partial view's components interfering.
8296 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8297 Discrim := First_Discriminant (Derived_Type);
8299 Last_Discrim := Discrim;
8300 Next_Discriminant (Discrim);
8301 exit when No (Discrim);
8304 Set_Last_Entity (Derived_Type, Last_Discrim);
8306 -- In all other cases wipe out the list of inherited components (even
8307 -- inherited discriminants), it will be properly rebuilt here.
8310 Set_First_Entity (Derived_Type, Empty);
8311 Set_Last_Entity (Derived_Type, Empty);
8314 -- STEP 1c: Initialize some flags for the Derived_Type
8316 -- The following flags must be initialized here so that
8317 -- Process_Discriminants can check that discriminants of tagged types do
8318 -- not have a default initial value and that access discriminants are
8319 -- only specified for limited records. For completeness, these flags are
8320 -- also initialized along with all the other flags below.
8322 -- AI-419: Limitedness is not inherited from an interface parent, so to
8323 -- be limited in that case the type must be explicitly declared as
8324 -- limited. However, task and protected interfaces are always limited.
8326 if Limited_Present (Type_Def) then
8327 Set_Is_Limited_Record (Derived_Type);
8329 elsif Is_Limited_Record (Parent_Type)
8330 or else (Present (Full_View (Parent_Type))
8331 and then Is_Limited_Record (Full_View (Parent_Type)))
8333 if not Is_Interface (Parent_Type)
8334 or else Is_Synchronized_Interface (Parent_Type)
8335 or else Is_Protected_Interface (Parent_Type)
8336 or else Is_Task_Interface (Parent_Type)
8338 Set_Is_Limited_Record (Derived_Type);
8342 -- STEP 2a: process discriminants of derived type if any
8344 Push_Scope (Derived_Type);
8346 if Discriminant_Specs then
8347 Set_Has_Unknown_Discriminants (Derived_Type, False);
8349 -- The following call initializes fields Has_Discriminants and
8350 -- Discriminant_Constraint, unless we are processing the completion
8351 -- of a private type declaration.
8353 Check_Or_Process_Discriminants (N, Derived_Type);
8355 -- For untagged types, the constraint on the Parent_Type must be
8356 -- present and is used to rename the discriminants.
8358 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8359 Error_Msg_N ("untagged parent must have discriminants", Indic);
8361 elsif not Is_Tagged and then not Constraint_Present then
8363 ("discriminant constraint needed for derived untagged records",
8366 -- Otherwise the parent subtype must be constrained unless we have a
8367 -- private extension.
8369 elsif not Constraint_Present
8370 and then not Private_Extension
8371 and then not Is_Constrained (Parent_Type)
8374 ("unconstrained type not allowed in this context", Indic);
8376 elsif Constraint_Present then
8377 -- The following call sets the field Corresponding_Discriminant
8378 -- for the discriminants in the Derived_Type.
8380 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8382 -- For untagged types all new discriminants must rename
8383 -- discriminants in the parent. For private extensions new
8384 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8386 Discrim := First_Discriminant (Derived_Type);
8387 while Present (Discrim) loop
8389 and then No (Corresponding_Discriminant (Discrim))
8392 ("new discriminants must constrain old ones", Discrim);
8394 elsif Private_Extension
8395 and then Present (Corresponding_Discriminant (Discrim))
8398 ("only static constraints allowed for parent"
8399 & " discriminants in the partial view", Indic);
8403 -- If a new discriminant is used in the constraint, then its
8404 -- subtype must be statically compatible with the parent
8405 -- discriminant's subtype (3.7(15)).
8407 -- However, if the record contains an array constrained by
8408 -- the discriminant but with some different bound, the compiler
8409 -- attemps to create a smaller range for the discriminant type.
8410 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8411 -- the discriminant type is a scalar type, the check must use
8412 -- the original discriminant type in the parent declaration.
8415 Corr_Disc : constant Entity_Id :=
8416 Corresponding_Discriminant (Discrim);
8417 Disc_Type : constant Entity_Id := Etype (Discrim);
8418 Corr_Type : Entity_Id;
8421 if Present (Corr_Disc) then
8422 if Is_Scalar_Type (Disc_Type) then
8424 Entity (Discriminant_Type (Parent (Corr_Disc)));
8426 Corr_Type := Etype (Corr_Disc);
8430 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8433 ("subtype must be compatible "
8434 & "with parent discriminant",
8440 Next_Discriminant (Discrim);
8443 -- Check whether the constraints of the full view statically
8444 -- match those imposed by the parent subtype [7.3(13)].
8446 if Present (Stored_Constraint (Derived_Type)) then
8451 C1 := First_Elmt (Discs);
8452 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8453 while Present (C1) and then Present (C2) loop
8455 Fully_Conformant_Expressions (Node (C1), Node (C2))
8458 ("not conformant with previous declaration",
8469 -- STEP 2b: No new discriminants, inherit discriminants if any
8472 if Private_Extension then
8473 Set_Has_Unknown_Discriminants
8475 Has_Unknown_Discriminants (Parent_Type)
8476 or else Unknown_Discriminants_Present (N));
8478 -- The partial view of the parent may have unknown discriminants,
8479 -- but if the full view has discriminants and the parent type is
8480 -- in scope they must be inherited.
8482 elsif Has_Unknown_Discriminants (Parent_Type)
8484 (not Has_Discriminants (Parent_Type)
8485 or else not In_Open_Scopes (Scope (Parent_Type)))
8487 Set_Has_Unknown_Discriminants (Derived_Type);
8490 if not Has_Unknown_Discriminants (Derived_Type)
8491 and then not Has_Unknown_Discriminants (Parent_Base)
8492 and then Has_Discriminants (Parent_Type)
8494 Inherit_Discrims := True;
8495 Set_Has_Discriminants
8496 (Derived_Type, True);
8497 Set_Discriminant_Constraint
8498 (Derived_Type, Discriminant_Constraint (Parent_Base));
8501 -- The following test is true for private types (remember
8502 -- transformation 5. is not applied to those) and in an error
8505 if Constraint_Present then
8506 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8509 -- For now mark a new derived type as constrained only if it has no
8510 -- discriminants. At the end of Build_Derived_Record_Type we properly
8511 -- set this flag in the case of private extensions. See comments in
8512 -- point 9. just before body of Build_Derived_Record_Type.
8516 not (Inherit_Discrims
8517 or else Has_Unknown_Discriminants (Derived_Type)));
8520 -- STEP 3: initialize fields of derived type
8522 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8523 Set_Stored_Constraint (Derived_Type, No_Elist);
8525 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8526 -- but cannot be interfaces
8528 if not Private_Extension
8529 and then Ekind (Derived_Type) /= E_Private_Type
8530 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8532 if Interface_Present (Type_Def) then
8533 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8536 Set_Interfaces (Derived_Type, No_Elist);
8539 -- Fields inherited from the Parent_Type
8541 Set_Has_Specified_Layout
8542 (Derived_Type, Has_Specified_Layout (Parent_Type));
8543 Set_Is_Limited_Composite
8544 (Derived_Type, Is_Limited_Composite (Parent_Type));
8545 Set_Is_Private_Composite
8546 (Derived_Type, Is_Private_Composite (Parent_Type));
8548 if Is_Tagged_Type (Parent_Type) then
8549 Set_No_Tagged_Streams_Pragma
8550 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8553 -- Fields inherited from the Parent_Base
8555 Set_Has_Controlled_Component
8556 (Derived_Type, Has_Controlled_Component (Parent_Base));
8557 Set_Has_Non_Standard_Rep
8558 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8559 Set_Has_Primitive_Operations
8560 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8562 -- Fields inherited from the Parent_Base in the non-private case
8564 if Ekind (Derived_Type) = E_Record_Type then
8565 Set_Has_Complex_Representation
8566 (Derived_Type, Has_Complex_Representation (Parent_Base));
8569 -- Fields inherited from the Parent_Base for record types
8571 if Is_Record_Type (Derived_Type) then
8573 Parent_Full : Entity_Id;
8576 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8577 -- Parent_Base can be a private type or private extension. Go
8578 -- to the full view here to get the E_Record_Type specific flags.
8580 if Present (Full_View (Parent_Base)) then
8581 Parent_Full := Full_View (Parent_Base);
8583 Parent_Full := Parent_Base;
8586 Set_OK_To_Reorder_Components
8587 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8591 -- Set fields for private derived types
8593 if Is_Private_Type (Derived_Type) then
8594 Set_Depends_On_Private (Derived_Type, True);
8595 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8597 -- Inherit fields from non private record types. If this is the
8598 -- completion of a derivation from a private type, the parent itself
8599 -- is private, and the attributes come from its full view, which must
8603 if Is_Private_Type (Parent_Base)
8604 and then not Is_Record_Type (Parent_Base)
8606 Set_Component_Alignment
8607 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8609 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8611 Set_Component_Alignment
8612 (Derived_Type, Component_Alignment (Parent_Base));
8614 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8618 -- Set fields for tagged types
8621 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8623 -- All tagged types defined in Ada.Finalization are controlled
8625 if Chars (Scope (Derived_Type)) = Name_Finalization
8626 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8627 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8629 Set_Is_Controlled (Derived_Type);
8631 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8634 -- Minor optimization: there is no need to generate the class-wide
8635 -- entity associated with an underlying record view.
8637 if not Is_Underlying_Record_View (Derived_Type) then
8638 Make_Class_Wide_Type (Derived_Type);
8641 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8643 if Has_Discriminants (Derived_Type)
8644 and then Constraint_Present
8646 Set_Stored_Constraint
8647 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8650 if Ada_Version >= Ada_2005 then
8652 Ifaces_List : Elist_Id;
8655 -- Checks rules 3.9.4 (13/2 and 14/2)
8657 if Comes_From_Source (Derived_Type)
8658 and then not Is_Private_Type (Derived_Type)
8659 and then Is_Interface (Parent_Type)
8660 and then not Is_Interface (Derived_Type)
8662 if Is_Task_Interface (Parent_Type) then
8664 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8667 elsif Is_Protected_Interface (Parent_Type) then
8669 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8674 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8676 Check_Interfaces (N, Type_Def);
8678 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8679 -- not already in the parents.
8683 Ifaces_List => Ifaces_List,
8684 Exclude_Parents => True);
8686 Set_Interfaces (Derived_Type, Ifaces_List);
8688 -- If the derived type is the anonymous type created for
8689 -- a declaration whose parent has a constraint, propagate
8690 -- the interface list to the source type. This must be done
8691 -- prior to the completion of the analysis of the source type
8692 -- because the components in the extension may contain current
8693 -- instances whose legality depends on some ancestor.
8695 if Is_Itype (Derived_Type) then
8697 Def : constant Node_Id :=
8698 Associated_Node_For_Itype (Derived_Type);
8701 and then Nkind (Def) = N_Full_Type_Declaration
8704 (Defining_Identifier (Def), Ifaces_List);
8709 -- Propagate inherited invariant information of parents
8712 if Ada_Version >= Ada_2012
8713 and then not Is_Interface (Derived_Type)
8715 if Has_Inheritable_Invariants (Parent_Type) then
8716 Set_Has_Invariants (Derived_Type);
8717 Set_Has_Inheritable_Invariants (Derived_Type);
8719 elsif not Is_Empty_Elmt_List (Ifaces_List) then
8724 AI := First_Elmt (Ifaces_List);
8725 while Present (AI) loop
8726 if Has_Inheritable_Invariants (Node (AI)) then
8727 Set_Has_Invariants (Derived_Type);
8728 Set_Has_Inheritable_Invariants (Derived_Type);
8739 -- A type extension is automatically Ghost when one of its
8740 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
8741 -- also inherited when the parent type is Ghost, but this is
8742 -- done in Build_Derived_Type as the mechanism also handles
8743 -- untagged derivations.
8745 if Implements_Ghost_Interface (Derived_Type) then
8746 Set_Is_Ghost_Entity (Derived_Type);
8752 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8753 Set_Has_Non_Standard_Rep
8754 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8757 -- STEP 4: Inherit components from the parent base and constrain them.
8758 -- Apply the second transformation described in point 6. above.
8760 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8761 or else not Has_Discriminants (Parent_Type)
8762 or else not Is_Constrained (Parent_Type)
8766 Constrs := Discriminant_Constraint (Parent_Type);
8771 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8773 -- STEP 5a: Copy the parent record declaration for untagged types
8775 if not Is_Tagged then
8777 -- Discriminant_Constraint (Derived_Type) has been properly
8778 -- constructed. Save it and temporarily set it to Empty because we
8779 -- do not want the call to New_Copy_Tree below to mess this list.
8781 if Has_Discriminants (Derived_Type) then
8782 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8783 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8785 Save_Discr_Constr := No_Elist;
8788 -- Save the Etype field of Derived_Type. It is correctly set now,
8789 -- but the call to New_Copy tree may remap it to point to itself,
8790 -- which is not what we want. Ditto for the Next_Entity field.
8792 Save_Etype := Etype (Derived_Type);
8793 Save_Next_Entity := Next_Entity (Derived_Type);
8795 -- Assoc_List maps all stored discriminants in the Parent_Base to
8796 -- stored discriminants in the Derived_Type. It is fundamental that
8797 -- no types or itypes with discriminants other than the stored
8798 -- discriminants appear in the entities declared inside
8799 -- Derived_Type, since the back end cannot deal with it.
8803 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8805 -- Restore the fields saved prior to the New_Copy_Tree call
8806 -- and compute the stored constraint.
8808 Set_Etype (Derived_Type, Save_Etype);
8809 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8811 if Has_Discriminants (Derived_Type) then
8812 Set_Discriminant_Constraint
8813 (Derived_Type, Save_Discr_Constr);
8814 Set_Stored_Constraint
8815 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
8816 Replace_Components (Derived_Type, New_Decl);
8817 Set_Has_Implicit_Dereference
8818 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
8821 -- Insert the new derived type declaration
8823 Rewrite (N, New_Decl);
8825 -- STEP 5b: Complete the processing for record extensions in generics
8827 -- There is no completion for record extensions declared in the
8828 -- parameter part of a generic, so we need to complete processing for
8829 -- these generic record extensions here. The Record_Type_Definition call
8830 -- will change the Ekind of the components from E_Void to E_Component.
8832 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8833 Record_Type_Definition (Empty, Derived_Type);
8835 -- STEP 5c: Process the record extension for non private tagged types
8837 elsif not Private_Extension then
8838 Expand_Record_Extension (Derived_Type, Type_Def);
8840 -- Note : previously in ASIS mode we set the Parent_Subtype of the
8841 -- derived type to propagate some semantic information. This led
8842 -- to other ASIS failures and has been removed.
8844 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8845 -- implemented interfaces if we are in expansion mode
8848 and then Has_Interfaces (Derived_Type)
8850 Add_Interface_Tag_Components (N, Derived_Type);
8853 -- Analyze the record extension
8855 Record_Type_Definition
8856 (Record_Extension_Part (Type_Def), Derived_Type);
8861 -- Nothing else to do if there is an error in the derivation.
8862 -- An unusual case: the full view may be derived from a type in an
8863 -- instance, when the partial view was used illegally as an actual
8864 -- in that instance, leading to a circular definition.
8866 if Etype (Derived_Type) = Any_Type
8867 or else Etype (Parent_Type) = Derived_Type
8872 -- Set delayed freeze and then derive subprograms, we need to do
8873 -- this in this order so that derived subprograms inherit the
8874 -- derived freeze if necessary.
8876 Set_Has_Delayed_Freeze (Derived_Type);
8878 if Derive_Subps then
8879 Derive_Subprograms (Parent_Type, Derived_Type);
8882 -- If we have a private extension which defines a constrained derived
8883 -- type mark as constrained here after we have derived subprograms. See
8884 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8886 if Private_Extension and then Inherit_Discrims then
8887 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8888 Set_Is_Constrained (Derived_Type, True);
8889 Set_Discriminant_Constraint (Derived_Type, Discs);
8891 elsif Is_Constrained (Parent_Type) then
8893 (Derived_Type, True);
8894 Set_Discriminant_Constraint
8895 (Derived_Type, Discriminant_Constraint (Parent_Type));
8899 -- Update the class-wide type, which shares the now-completed entity
8900 -- list with its specific type. In case of underlying record views,
8901 -- we do not generate the corresponding class wide entity.
8904 and then not Is_Underlying_Record_View (Derived_Type)
8907 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8909 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8912 Check_Function_Writable_Actuals (N);
8913 end Build_Derived_Record_Type;
8915 ------------------------
8916 -- Build_Derived_Type --
8917 ------------------------
8919 procedure Build_Derived_Type
8921 Parent_Type : Entity_Id;
8922 Derived_Type : Entity_Id;
8923 Is_Completion : Boolean;
8924 Derive_Subps : Boolean := True)
8926 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8929 -- Set common attributes
8931 Set_Scope (Derived_Type, Current_Scope);
8933 Set_Etype (Derived_Type, Parent_Base);
8934 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8935 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8936 Set_Has_Protected (Derived_Type, Has_Protected (Parent_Base));
8938 Set_Size_Info (Derived_Type, Parent_Type);
8939 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8940 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8941 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8942 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
8944 if Is_Tagged_Type (Derived_Type) then
8945 Set_No_Tagged_Streams_Pragma
8946 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8949 -- If the parent has primitive routines, set the derived type link
8951 if Has_Primitive_Operations (Parent_Type) then
8952 Set_Derived_Type_Link (Parent_Base, Derived_Type);
8955 -- If the parent type is a private subtype, the convention on the base
8956 -- type may be set in the private part, and not propagated to the
8957 -- subtype until later, so we obtain the convention from the base type.
8959 Set_Convention (Derived_Type, Convention (Parent_Base));
8961 -- Set SSO default for record or array type
8963 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
8964 and then Is_Base_Type (Derived_Type)
8966 Set_Default_SSO (Derived_Type);
8969 -- Propagate invariant information. The new type has invariants if
8970 -- they are inherited from the parent type, and these invariants can
8971 -- be further inherited, so both flags are set.
8973 -- We similarly inherit predicates
8975 if Has_Predicates (Parent_Type) then
8976 Set_Has_Predicates (Derived_Type);
8979 -- The derived type inherits the representation clauses of the parent
8981 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
8983 -- Propagate the attributes related to pragma Default_Initial_Condition
8984 -- from the parent type to the private extension. A derived type always
8985 -- inherits the default initial condition flag from the parent type. If
8986 -- the derived type carries its own Default_Initial_Condition pragma,
8987 -- the flag is later reset in Analyze_Pragma. Note that both flags are
8988 -- mutually exclusive.
8990 Propagate_Default_Init_Cond_Attributes
8991 (From_Typ => Parent_Type,
8992 To_Typ => Derived_Type,
8993 Parent_To_Derivation => True);
8995 -- If the parent type has delayed rep aspects, then mark the derived
8996 -- type as possibly inheriting a delayed rep aspect.
8998 if Has_Delayed_Rep_Aspects (Parent_Type) then
8999 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9002 -- Propagate the attributes related to pragma Ghost from the parent type
9003 -- to the derived type or type extension (SPARK RM 6.9(9)).
9005 if Is_Ghost_Entity (Parent_Type) then
9006 Set_Is_Ghost_Entity (Derived_Type);
9009 -- Type dependent processing
9011 case Ekind (Parent_Type) is
9012 when Numeric_Kind =>
9013 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9016 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9020 | Class_Wide_Kind =>
9021 Build_Derived_Record_Type
9022 (N, Parent_Type, Derived_Type, Derive_Subps);
9025 when Enumeration_Kind =>
9026 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9029 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9031 when Incomplete_Or_Private_Kind =>
9032 Build_Derived_Private_Type
9033 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9035 -- For discriminated types, the derivation includes deriving
9036 -- primitive operations. For others it is done below.
9038 if Is_Tagged_Type (Parent_Type)
9039 or else Has_Discriminants (Parent_Type)
9040 or else (Present (Full_View (Parent_Type))
9041 and then Has_Discriminants (Full_View (Parent_Type)))
9046 when Concurrent_Kind =>
9047 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9050 raise Program_Error;
9053 -- Nothing more to do if some error occurred
9055 if Etype (Derived_Type) = Any_Type then
9059 -- Set delayed freeze and then derive subprograms, we need to do this
9060 -- in this order so that derived subprograms inherit the derived freeze
9063 Set_Has_Delayed_Freeze (Derived_Type);
9065 if Derive_Subps then
9066 Derive_Subprograms (Parent_Type, Derived_Type);
9069 Set_Has_Primitive_Operations
9070 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9071 end Build_Derived_Type;
9073 -----------------------
9074 -- Build_Discriminal --
9075 -----------------------
9077 procedure Build_Discriminal (Discrim : Entity_Id) is
9078 D_Minal : Entity_Id;
9079 CR_Disc : Entity_Id;
9082 -- A discriminal has the same name as the discriminant
9084 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9086 Set_Ekind (D_Minal, E_In_Parameter);
9087 Set_Mechanism (D_Minal, Default_Mechanism);
9088 Set_Etype (D_Minal, Etype (Discrim));
9089 Set_Scope (D_Minal, Current_Scope);
9091 Set_Discriminal (Discrim, D_Minal);
9092 Set_Discriminal_Link (D_Minal, Discrim);
9094 -- For task types, build at once the discriminants of the corresponding
9095 -- record, which are needed if discriminants are used in entry defaults
9096 -- and in family bounds.
9098 if Is_Concurrent_Type (Current_Scope)
9100 Is_Limited_Type (Current_Scope)
9102 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9104 Set_Ekind (CR_Disc, E_In_Parameter);
9105 Set_Mechanism (CR_Disc, Default_Mechanism);
9106 Set_Etype (CR_Disc, Etype (Discrim));
9107 Set_Scope (CR_Disc, Current_Scope);
9108 Set_Discriminal_Link (CR_Disc, Discrim);
9109 Set_CR_Discriminant (Discrim, CR_Disc);
9111 end Build_Discriminal;
9113 ------------------------------------
9114 -- Build_Discriminant_Constraints --
9115 ------------------------------------
9117 function Build_Discriminant_Constraints
9120 Derived_Def : Boolean := False) return Elist_Id
9122 C : constant Node_Id := Constraint (Def);
9123 Nb_Discr : constant Nat := Number_Discriminants (T);
9125 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9126 -- Saves the expression corresponding to a given discriminant in T
9128 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9129 -- Return the Position number within array Discr_Expr of a discriminant
9130 -- D within the discriminant list of the discriminated type T.
9132 procedure Process_Discriminant_Expression
9135 -- If this is a discriminant constraint on a partial view, do not
9136 -- generate an overflow check on the discriminant expression. The check
9137 -- will be generated when constraining the full view. Otherwise the
9138 -- backend creates duplicate symbols for the temporaries corresponding
9139 -- to the expressions to be checked, causing spurious assembler errors.
9145 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9149 Disc := First_Discriminant (T);
9150 for J in Discr_Expr'Range loop
9155 Next_Discriminant (Disc);
9158 -- Note: Since this function is called on discriminants that are
9159 -- known to belong to the discriminated type, falling through the
9160 -- loop with no match signals an internal compiler error.
9162 raise Program_Error;
9165 -------------------------------------
9166 -- Process_Discriminant_Expression --
9167 -------------------------------------
9169 procedure Process_Discriminant_Expression
9173 BDT : constant Entity_Id := Base_Type (Etype (D));
9176 -- If this is a discriminant constraint on a partial view, do
9177 -- not generate an overflow on the discriminant expression. The
9178 -- check will be generated when constraining the full view.
9180 if Is_Private_Type (T)
9181 and then Present (Full_View (T))
9183 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9185 Analyze_And_Resolve (Expr, BDT);
9187 end Process_Discriminant_Expression;
9189 -- Declarations local to Build_Discriminant_Constraints
9193 Elist : constant Elist_Id := New_Elmt_List;
9201 Discrim_Present : Boolean := False;
9203 -- Start of processing for Build_Discriminant_Constraints
9206 -- The following loop will process positional associations only.
9207 -- For a positional association, the (single) discriminant is
9208 -- implicitly specified by position, in textual order (RM 3.7.2).
9210 Discr := First_Discriminant (T);
9211 Constr := First (Constraints (C));
9212 for D in Discr_Expr'Range loop
9213 exit when Nkind (Constr) = N_Discriminant_Association;
9216 Error_Msg_N ("too few discriminants given in constraint", C);
9217 return New_Elmt_List;
9219 elsif Nkind (Constr) = N_Range
9220 or else (Nkind (Constr) = N_Attribute_Reference
9221 and then Attribute_Name (Constr) = Name_Range)
9224 ("a range is not a valid discriminant constraint", Constr);
9225 Discr_Expr (D) := Error;
9228 Process_Discriminant_Expression (Constr, Discr);
9229 Discr_Expr (D) := Constr;
9232 Next_Discriminant (Discr);
9236 if No (Discr) and then Present (Constr) then
9237 Error_Msg_N ("too many discriminants given in constraint", Constr);
9238 return New_Elmt_List;
9241 -- Named associations can be given in any order, but if both positional
9242 -- and named associations are used in the same discriminant constraint,
9243 -- then positional associations must occur first, at their normal
9244 -- position. Hence once a named association is used, the rest of the
9245 -- discriminant constraint must use only named associations.
9247 while Present (Constr) loop
9249 -- Positional association forbidden after a named association
9251 if Nkind (Constr) /= N_Discriminant_Association then
9252 Error_Msg_N ("positional association follows named one", Constr);
9253 return New_Elmt_List;
9255 -- Otherwise it is a named association
9258 -- E records the type of the discriminants in the named
9259 -- association. All the discriminants specified in the same name
9260 -- association must have the same type.
9264 -- Search the list of discriminants in T to see if the simple name
9265 -- given in the constraint matches any of them.
9267 Id := First (Selector_Names (Constr));
9268 while Present (Id) loop
9271 -- If Original_Discriminant is present, we are processing a
9272 -- generic instantiation and this is an instance node. We need
9273 -- to find the name of the corresponding discriminant in the
9274 -- actual record type T and not the name of the discriminant in
9275 -- the generic formal. Example:
9278 -- type G (D : int) is private;
9280 -- subtype W is G (D => 1);
9282 -- type Rec (X : int) is record ... end record;
9283 -- package Q is new P (G => Rec);
9285 -- At the point of the instantiation, formal type G is Rec
9286 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9287 -- which really looks like "subtype W is Rec (D => 1);" at
9288 -- the point of instantiation, we want to find the discriminant
9289 -- that corresponds to D in Rec, i.e. X.
9291 if Present (Original_Discriminant (Id))
9292 and then In_Instance
9294 Discr := Find_Corresponding_Discriminant (Id, T);
9298 Discr := First_Discriminant (T);
9299 while Present (Discr) loop
9300 if Chars (Discr) = Chars (Id) then
9305 Next_Discriminant (Discr);
9309 Error_Msg_N ("& does not match any discriminant", Id);
9310 return New_Elmt_List;
9312 -- If the parent type is a generic formal, preserve the
9313 -- name of the discriminant for subsequent instances.
9314 -- see comment at the beginning of this if statement.
9316 elsif Is_Generic_Type (Root_Type (T)) then
9317 Set_Original_Discriminant (Id, Discr);
9321 Position := Pos_Of_Discr (T, Discr);
9323 if Present (Discr_Expr (Position)) then
9324 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9327 -- Each discriminant specified in the same named association
9328 -- must be associated with a separate copy of the
9329 -- corresponding expression.
9331 if Present (Next (Id)) then
9332 Expr := New_Copy_Tree (Expression (Constr));
9333 Set_Parent (Expr, Parent (Expression (Constr)));
9335 Expr := Expression (Constr);
9338 Discr_Expr (Position) := Expr;
9339 Process_Discriminant_Expression (Expr, Discr);
9342 -- A discriminant association with more than one discriminant
9343 -- name is only allowed if the named discriminants are all of
9344 -- the same type (RM 3.7.1(8)).
9347 E := Base_Type (Etype (Discr));
9349 elsif Base_Type (Etype (Discr)) /= E then
9351 ("all discriminants in an association " &
9352 "must have the same type", Id);
9362 -- A discriminant constraint must provide exactly one value for each
9363 -- discriminant of the type (RM 3.7.1(8)).
9365 for J in Discr_Expr'Range loop
9366 if No (Discr_Expr (J)) then
9367 Error_Msg_N ("too few discriminants given in constraint", C);
9368 return New_Elmt_List;
9372 -- Determine if there are discriminant expressions in the constraint
9374 for J in Discr_Expr'Range loop
9375 if Denotes_Discriminant
9376 (Discr_Expr (J), Check_Concurrent => True)
9378 Discrim_Present := True;
9382 -- Build an element list consisting of the expressions given in the
9383 -- discriminant constraint and apply the appropriate checks. The list
9384 -- is constructed after resolving any named discriminant associations
9385 -- and therefore the expressions appear in the textual order of the
9388 Discr := First_Discriminant (T);
9389 for J in Discr_Expr'Range loop
9390 if Discr_Expr (J) /= Error then
9391 Append_Elmt (Discr_Expr (J), Elist);
9393 -- If any of the discriminant constraints is given by a
9394 -- discriminant and we are in a derived type declaration we
9395 -- have a discriminant renaming. Establish link between new
9396 -- and old discriminant.
9398 if Denotes_Discriminant (Discr_Expr (J)) then
9400 Set_Corresponding_Discriminant
9401 (Entity (Discr_Expr (J)), Discr);
9404 -- Force the evaluation of non-discriminant expressions.
9405 -- If we have found a discriminant in the constraint 3.4(26)
9406 -- and 3.8(18) demand that no range checks are performed are
9407 -- after evaluation. If the constraint is for a component
9408 -- definition that has a per-object constraint, expressions are
9409 -- evaluated but not checked either. In all other cases perform
9413 if Discrim_Present then
9416 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9418 Has_Per_Object_Constraint
9419 (Defining_Identifier (Parent (Parent (Def))))
9423 elsif Is_Access_Type (Etype (Discr)) then
9424 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9427 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9430 Force_Evaluation (Discr_Expr (J));
9433 -- Check that the designated type of an access discriminant's
9434 -- expression is not a class-wide type unless the discriminant's
9435 -- designated type is also class-wide.
9437 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9438 and then not Is_Class_Wide_Type
9439 (Designated_Type (Etype (Discr)))
9440 and then Etype (Discr_Expr (J)) /= Any_Type
9441 and then Is_Class_Wide_Type
9442 (Designated_Type (Etype (Discr_Expr (J))))
9444 Wrong_Type (Discr_Expr (J), Etype (Discr));
9446 elsif Is_Access_Type (Etype (Discr))
9447 and then not Is_Access_Constant (Etype (Discr))
9448 and then Is_Access_Type (Etype (Discr_Expr (J)))
9449 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9452 ("constraint for discriminant& must be access to variable",
9457 Next_Discriminant (Discr);
9461 end Build_Discriminant_Constraints;
9463 ---------------------------------
9464 -- Build_Discriminated_Subtype --
9465 ---------------------------------
9467 procedure Build_Discriminated_Subtype
9471 Related_Nod : Node_Id;
9472 For_Access : Boolean := False)
9474 Has_Discrs : constant Boolean := Has_Discriminants (T);
9475 Constrained : constant Boolean :=
9477 and then not Is_Empty_Elmt_List (Elist)
9478 and then not Is_Class_Wide_Type (T))
9479 or else Is_Constrained (T);
9482 if Ekind (T) = E_Record_Type then
9484 Set_Ekind (Def_Id, E_Private_Subtype);
9485 Set_Is_For_Access_Subtype (Def_Id, True);
9487 Set_Ekind (Def_Id, E_Record_Subtype);
9490 -- Inherit preelaboration flag from base, for types for which it
9491 -- may have been set: records, private types, protected types.
9493 Set_Known_To_Have_Preelab_Init
9494 (Def_Id, Known_To_Have_Preelab_Init (T));
9496 elsif Ekind (T) = E_Task_Type then
9497 Set_Ekind (Def_Id, E_Task_Subtype);
9499 elsif Ekind (T) = E_Protected_Type then
9500 Set_Ekind (Def_Id, E_Protected_Subtype);
9501 Set_Known_To_Have_Preelab_Init
9502 (Def_Id, Known_To_Have_Preelab_Init (T));
9504 elsif Is_Private_Type (T) then
9505 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9506 Set_Known_To_Have_Preelab_Init
9507 (Def_Id, Known_To_Have_Preelab_Init (T));
9509 -- Private subtypes may have private dependents
9511 Set_Private_Dependents (Def_Id, New_Elmt_List);
9513 elsif Is_Class_Wide_Type (T) then
9514 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9517 -- Incomplete type. Attach subtype to list of dependents, to be
9518 -- completed with full view of parent type, unless is it the
9519 -- designated subtype of a record component within an init_proc.
9520 -- This last case arises for a component of an access type whose
9521 -- designated type is incomplete (e.g. a Taft Amendment type).
9522 -- The designated subtype is within an inner scope, and needs no
9523 -- elaboration, because only the access type is needed in the
9524 -- initialization procedure.
9526 Set_Ekind (Def_Id, Ekind (T));
9528 if For_Access and then Within_Init_Proc then
9531 Append_Elmt (Def_Id, Private_Dependents (T));
9535 Set_Etype (Def_Id, T);
9536 Init_Size_Align (Def_Id);
9537 Set_Has_Discriminants (Def_Id, Has_Discrs);
9538 Set_Is_Constrained (Def_Id, Constrained);
9540 Set_First_Entity (Def_Id, First_Entity (T));
9541 Set_Last_Entity (Def_Id, Last_Entity (T));
9542 Set_Has_Implicit_Dereference
9543 (Def_Id, Has_Implicit_Dereference (T));
9545 -- If the subtype is the completion of a private declaration, there may
9546 -- have been representation clauses for the partial view, and they must
9547 -- be preserved. Build_Derived_Type chains the inherited clauses with
9548 -- the ones appearing on the extension. If this comes from a subtype
9549 -- declaration, all clauses are inherited.
9551 if No (First_Rep_Item (Def_Id)) then
9552 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9555 if Is_Tagged_Type (T) then
9556 Set_Is_Tagged_Type (Def_Id);
9557 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9558 Make_Class_Wide_Type (Def_Id);
9561 Set_Stored_Constraint (Def_Id, No_Elist);
9564 Set_Discriminant_Constraint (Def_Id, Elist);
9565 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9568 if Is_Tagged_Type (T) then
9570 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9571 -- concurrent record type (which has the list of primitive
9574 if Ada_Version >= Ada_2005
9575 and then Is_Concurrent_Type (T)
9577 Set_Corresponding_Record_Type (Def_Id,
9578 Corresponding_Record_Type (T));
9580 Set_Direct_Primitive_Operations (Def_Id,
9581 Direct_Primitive_Operations (T));
9584 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9587 -- Subtypes introduced by component declarations do not need to be
9588 -- marked as delayed, and do not get freeze nodes, because the semantics
9589 -- verifies that the parents of the subtypes are frozen before the
9590 -- enclosing record is frozen.
9592 if not Is_Type (Scope (Def_Id)) then
9593 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9595 if Is_Private_Type (T)
9596 and then Present (Full_View (T))
9598 Conditional_Delay (Def_Id, Full_View (T));
9600 Conditional_Delay (Def_Id, T);
9604 if Is_Record_Type (T) then
9605 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9608 and then not Is_Empty_Elmt_List (Elist)
9609 and then not For_Access
9611 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9612 elsif not For_Access then
9613 Set_Cloned_Subtype (Def_Id, T);
9616 end Build_Discriminated_Subtype;
9618 ---------------------------
9619 -- Build_Itype_Reference --
9620 ---------------------------
9622 procedure Build_Itype_Reference
9626 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9629 -- Itype references are only created for use by the back-end
9631 if Inside_A_Generic then
9634 Set_Itype (IR, Ityp);
9635 Insert_After (Nod, IR);
9637 end Build_Itype_Reference;
9639 ------------------------
9640 -- Build_Scalar_Bound --
9641 ------------------------
9643 function Build_Scalar_Bound
9646 Der_T : Entity_Id) return Node_Id
9648 New_Bound : Entity_Id;
9651 -- Note: not clear why this is needed, how can the original bound
9652 -- be unanalyzed at this point? and if it is, what business do we
9653 -- have messing around with it? and why is the base type of the
9654 -- parent type the right type for the resolution. It probably is
9655 -- not. It is OK for the new bound we are creating, but not for
9656 -- the old one??? Still if it never happens, no problem.
9658 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9660 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9661 New_Bound := New_Copy (Bound);
9662 Set_Etype (New_Bound, Der_T);
9663 Set_Analyzed (New_Bound);
9665 elsif Is_Entity_Name (Bound) then
9666 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9668 -- The following is almost certainly wrong. What business do we have
9669 -- relocating a node (Bound) that is presumably still attached to
9670 -- the tree elsewhere???
9673 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9676 Set_Etype (New_Bound, Der_T);
9678 end Build_Scalar_Bound;
9680 --------------------------------
9681 -- Build_Underlying_Full_View --
9682 --------------------------------
9684 procedure Build_Underlying_Full_View
9689 Loc : constant Source_Ptr := Sloc (N);
9690 Subt : constant Entity_Id :=
9691 Make_Defining_Identifier
9692 (Loc, New_External_Name (Chars (Typ), 'S'));
9699 procedure Set_Discriminant_Name (Id : Node_Id);
9700 -- If the derived type has discriminants, they may rename discriminants
9701 -- of the parent. When building the full view of the parent, we need to
9702 -- recover the names of the original discriminants if the constraint is
9703 -- given by named associations.
9705 ---------------------------
9706 -- Set_Discriminant_Name --
9707 ---------------------------
9709 procedure Set_Discriminant_Name (Id : Node_Id) is
9713 Set_Original_Discriminant (Id, Empty);
9715 if Has_Discriminants (Typ) then
9716 Disc := First_Discriminant (Typ);
9717 while Present (Disc) loop
9718 if Chars (Disc) = Chars (Id)
9719 and then Present (Corresponding_Discriminant (Disc))
9721 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9723 Next_Discriminant (Disc);
9726 end Set_Discriminant_Name;
9728 -- Start of processing for Build_Underlying_Full_View
9731 if Nkind (N) = N_Full_Type_Declaration then
9732 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9734 elsif Nkind (N) = N_Subtype_Declaration then
9735 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
9737 elsif Nkind (N) = N_Component_Declaration then
9740 (Constraint (Subtype_Indication (Component_Definition (N))));
9743 raise Program_Error;
9746 C := First (Constraints (Constr));
9747 while Present (C) loop
9748 if Nkind (C) = N_Discriminant_Association then
9749 Id := First (Selector_Names (C));
9750 while Present (Id) loop
9751 Set_Discriminant_Name (Id);
9760 Make_Subtype_Declaration (Loc,
9761 Defining_Identifier => Subt,
9762 Subtype_Indication =>
9763 Make_Subtype_Indication (Loc,
9764 Subtype_Mark => New_Occurrence_Of (Par, Loc),
9765 Constraint => New_Copy_Tree (Constr)));
9767 -- If this is a component subtype for an outer itype, it is not
9768 -- a list member, so simply set the parent link for analysis: if
9769 -- the enclosing type does not need to be in a declarative list,
9770 -- neither do the components.
9772 if Is_List_Member (N)
9773 and then Nkind (N) /= N_Component_Declaration
9775 Insert_Before (N, Indic);
9777 Set_Parent (Indic, Parent (N));
9781 Set_Underlying_Full_View (Typ, Full_View (Subt));
9782 end Build_Underlying_Full_View;
9784 -------------------------------
9785 -- Check_Abstract_Overriding --
9786 -------------------------------
9788 procedure Check_Abstract_Overriding (T : Entity_Id) is
9789 Alias_Subp : Entity_Id;
9795 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9796 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9797 -- which has pragma Implemented already set. Check whether Subp's entity
9798 -- kind conforms to the implementation kind of the overridden routine.
9800 procedure Check_Pragma_Implemented
9802 Iface_Subp : Entity_Id);
9803 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9804 -- Iface_Subp and both entities have pragma Implemented already set on
9805 -- them. Check whether the two implementation kinds are conforming.
9807 procedure Inherit_Pragma_Implemented
9809 Iface_Subp : Entity_Id);
9810 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9811 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9812 -- Propagate the implementation kind of Iface_Subp to Subp.
9814 ------------------------------
9815 -- Check_Pragma_Implemented --
9816 ------------------------------
9818 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9819 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9820 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9821 Subp_Alias : constant Entity_Id := Alias (Subp);
9822 Contr_Typ : Entity_Id;
9823 Impl_Subp : Entity_Id;
9826 -- Subp must have an alias since it is a hidden entity used to link
9827 -- an interface subprogram to its overriding counterpart.
9829 pragma Assert (Present (Subp_Alias));
9831 -- Handle aliases to synchronized wrappers
9833 Impl_Subp := Subp_Alias;
9835 if Is_Primitive_Wrapper (Impl_Subp) then
9836 Impl_Subp := Wrapped_Entity (Impl_Subp);
9839 -- Extract the type of the controlling formal
9841 Contr_Typ := Etype (First_Formal (Subp_Alias));
9843 if Is_Concurrent_Record_Type (Contr_Typ) then
9844 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9847 -- An interface subprogram whose implementation kind is By_Entry must
9848 -- be implemented by an entry.
9850 if Impl_Kind = Name_By_Entry
9851 and then Ekind (Impl_Subp) /= E_Entry
9853 Error_Msg_Node_2 := Iface_Alias;
9855 ("type & must implement abstract subprogram & with an entry",
9856 Subp_Alias, Contr_Typ);
9858 elsif Impl_Kind = Name_By_Protected_Procedure then
9860 -- An interface subprogram whose implementation kind is By_
9861 -- Protected_Procedure cannot be implemented by a primitive
9862 -- procedure of a task type.
9864 if Ekind (Contr_Typ) /= E_Protected_Type then
9865 Error_Msg_Node_2 := Contr_Typ;
9867 ("interface subprogram & cannot be implemented by a " &
9868 "primitive procedure of task type &", Subp_Alias,
9871 -- An interface subprogram whose implementation kind is By_
9872 -- Protected_Procedure must be implemented by a procedure.
9874 elsif Ekind (Impl_Subp) /= E_Procedure then
9875 Error_Msg_Node_2 := Iface_Alias;
9877 ("type & must implement abstract subprogram & with a " &
9878 "procedure", Subp_Alias, Contr_Typ);
9880 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9881 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9883 Error_Msg_Name_1 := Impl_Kind;
9885 ("overriding operation& must have synchronization%",
9889 -- If primitive has Optional synchronization, overriding operation
9890 -- must match if it has an explicit synchronization..
9892 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9893 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9895 Error_Msg_Name_1 := Impl_Kind;
9897 ("overriding operation& must have syncrhonization%",
9900 end Check_Pragma_Implemented;
9902 ------------------------------
9903 -- Check_Pragma_Implemented --
9904 ------------------------------
9906 procedure Check_Pragma_Implemented
9908 Iface_Subp : Entity_Id)
9910 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9911 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
9914 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
9915 -- and overriding subprogram are different. In general this is an
9916 -- error except when the implementation kind of the overridden
9917 -- subprograms is By_Any or Optional.
9919 if Iface_Kind /= Subp_Kind
9920 and then Iface_Kind /= Name_By_Any
9921 and then Iface_Kind /= Name_Optional
9923 if Iface_Kind = Name_By_Entry then
9925 ("incompatible implementation kind, overridden subprogram " &
9926 "is marked By_Entry", Subp);
9929 ("incompatible implementation kind, overridden subprogram " &
9930 "is marked By_Protected_Procedure", Subp);
9933 end Check_Pragma_Implemented;
9935 --------------------------------
9936 -- Inherit_Pragma_Implemented --
9937 --------------------------------
9939 procedure Inherit_Pragma_Implemented
9941 Iface_Subp : Entity_Id)
9943 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9944 Loc : constant Source_Ptr := Sloc (Subp);
9945 Impl_Prag : Node_Id;
9948 -- Since the implementation kind is stored as a representation item
9949 -- rather than a flag, create a pragma node.
9953 Chars => Name_Implemented,
9954 Pragma_Argument_Associations => New_List (
9955 Make_Pragma_Argument_Association (Loc,
9956 Expression => New_Occurrence_Of (Subp, Loc)),
9958 Make_Pragma_Argument_Association (Loc,
9959 Expression => Make_Identifier (Loc, Iface_Kind))));
9961 -- The pragma doesn't need to be analyzed because it is internally
9962 -- built. It is safe to directly register it as a rep item since we
9963 -- are only interested in the characters of the implementation kind.
9965 Record_Rep_Item (Subp, Impl_Prag);
9966 end Inherit_Pragma_Implemented;
9968 -- Start of processing for Check_Abstract_Overriding
9971 Op_List := Primitive_Operations (T);
9973 -- Loop to check primitive operations
9975 Elmt := First_Elmt (Op_List);
9976 while Present (Elmt) loop
9977 Subp := Node (Elmt);
9978 Alias_Subp := Alias (Subp);
9980 -- Inherited subprograms are identified by the fact that they do not
9981 -- come from source, and the associated source location is the
9982 -- location of the first subtype of the derived type.
9984 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
9985 -- subprograms that "require overriding".
9987 -- Special exception, do not complain about failure to override the
9988 -- stream routines _Input and _Output, as well as the primitive
9989 -- operations used in dispatching selects since we always provide
9990 -- automatic overridings for these subprograms.
9992 -- Also ignore this rule for convention CIL since .NET libraries
9993 -- do bizarre things with interfaces???
9995 -- The partial view of T may have been a private extension, for
9996 -- which inherited functions dispatching on result are abstract.
9997 -- If the full view is a null extension, there is no need for
9998 -- overriding in Ada 2005, but wrappers need to be built for them
9999 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10001 if Is_Null_Extension (T)
10002 and then Has_Controlling_Result (Subp)
10003 and then Ada_Version >= Ada_2005
10004 and then Present (Alias_Subp)
10005 and then not Comes_From_Source (Subp)
10006 and then not Is_Abstract_Subprogram (Alias_Subp)
10007 and then not Is_Access_Type (Etype (Subp))
10011 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10012 -- processing because this check is done with the aliased
10015 elsif Present (Interface_Alias (Subp)) then
10018 elsif (Is_Abstract_Subprogram (Subp)
10019 or else Requires_Overriding (Subp)
10021 (Has_Controlling_Result (Subp)
10022 and then Present (Alias_Subp)
10023 and then not Comes_From_Source (Subp)
10024 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10025 and then not Is_TSS (Subp, TSS_Stream_Input)
10026 and then not Is_TSS (Subp, TSS_Stream_Output)
10027 and then not Is_Abstract_Type (T)
10028 and then Convention (T) /= Convention_CIL
10029 and then not Is_Predefined_Interface_Primitive (Subp)
10031 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10032 -- with abstract interface types because the check will be done
10033 -- with the aliased entity (otherwise we generate a duplicated
10036 and then not Present (Interface_Alias (Subp))
10038 if Present (Alias_Subp) then
10040 -- Only perform the check for a derived subprogram when the
10041 -- type has an explicit record extension. This avoids incorrect
10042 -- flagging of abstract subprograms for the case of a type
10043 -- without an extension that is derived from a formal type
10044 -- with a tagged actual (can occur within a private part).
10046 -- Ada 2005 (AI-391): In the case of an inherited function with
10047 -- a controlling result of the type, the rule does not apply if
10048 -- the type is a null extension (unless the parent function
10049 -- itself is abstract, in which case the function must still be
10050 -- be overridden). The expander will generate an overriding
10051 -- wrapper function calling the parent subprogram (see
10052 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10054 Type_Def := Type_Definition (Parent (T));
10056 if Nkind (Type_Def) = N_Derived_Type_Definition
10057 and then Present (Record_Extension_Part (Type_Def))
10059 (Ada_Version < Ada_2005
10060 or else not Is_Null_Extension (T)
10061 or else Ekind (Subp) = E_Procedure
10062 or else not Has_Controlling_Result (Subp)
10063 or else Is_Abstract_Subprogram (Alias_Subp)
10064 or else Requires_Overriding (Subp)
10065 or else Is_Access_Type (Etype (Subp)))
10067 -- Avoid reporting error in case of abstract predefined
10068 -- primitive inherited from interface type because the
10069 -- body of internally generated predefined primitives
10070 -- of tagged types are generated later by Freeze_Type
10072 if Is_Interface (Root_Type (T))
10073 and then Is_Abstract_Subprogram (Subp)
10074 and then Is_Predefined_Dispatching_Operation (Subp)
10075 and then not Comes_From_Source (Ultimate_Alias (Subp))
10079 -- A null extension is not obliged to override an inherited
10080 -- procedure subject to pragma Extensions_Visible with value
10081 -- False and at least one controlling OUT parameter
10082 -- (SPARK RM 6.1.7(6)).
10084 elsif Is_Null_Extension (T)
10085 and then Is_EVF_Procedure (Subp)
10091 ("type must be declared abstract or & overridden",
10094 -- Traverse the whole chain of aliased subprograms to
10095 -- complete the error notification. This is especially
10096 -- useful for traceability of the chain of entities when
10097 -- the subprogram corresponds with an interface
10098 -- subprogram (which may be defined in another package).
10100 if Present (Alias_Subp) then
10106 while Present (Alias (E)) loop
10108 -- Avoid reporting redundant errors on entities
10109 -- inherited from interfaces
10111 if Sloc (E) /= Sloc (T) then
10112 Error_Msg_Sloc := Sloc (E);
10114 ("\& has been inherited #", T, Subp);
10120 Error_Msg_Sloc := Sloc (E);
10122 -- AI05-0068: report if there is an overriding
10123 -- non-abstract subprogram that is invisible.
10126 and then not Is_Abstract_Subprogram (E)
10129 ("\& subprogram# is not visible",
10132 -- Clarify the case where a non-null extension must
10133 -- override inherited procedure subject to pragma
10134 -- Extensions_Visible with value False and at least
10135 -- one controlling OUT param.
10137 elsif Is_EVF_Procedure (E) then
10139 ("\& # is subject to Extensions_Visible False",
10144 ("\& has been inherited from subprogram #",
10151 -- Ada 2005 (AI-345): Protected or task type implementing
10152 -- abstract interfaces.
10154 elsif Is_Concurrent_Record_Type (T)
10155 and then Present (Interfaces (T))
10157 -- There is no need to check here RM 9.4(11.9/3) since we
10158 -- are processing the corresponding record type and the
10159 -- mode of the overriding subprograms was verified by
10160 -- Check_Conformance when the corresponding concurrent
10161 -- type declaration was analyzed.
10164 ("interface subprogram & must be overridden", T, Subp);
10166 -- Examine primitive operations of synchronized type to find
10167 -- homonyms that have the wrong profile.
10173 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10174 while Present (Prim) loop
10175 if Chars (Prim) = Chars (Subp) then
10177 ("profile is not type conformant with prefixed "
10178 & "view profile of inherited operation&",
10182 Next_Entity (Prim);
10188 Error_Msg_Node_2 := T;
10190 ("abstract subprogram& not allowed for type&", Subp);
10192 -- Also post unconditional warning on the type (unconditional
10193 -- so that if there are more than one of these cases, we get
10194 -- them all, and not just the first one).
10196 Error_Msg_Node_2 := Subp;
10197 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10200 -- A subprogram subject to pragma Extensions_Visible with value
10201 -- "True" cannot override a subprogram subject to the same pragma
10202 -- with value "False" (SPARK RM 6.1.7(5)).
10204 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10205 and then Present (Overridden_Operation (Subp))
10206 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10207 Extensions_Visible_False
10209 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10211 ("subprogram & with Extensions_Visible True cannot override "
10212 & "subprogram # with Extensions_Visible False", Subp);
10215 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10217 -- Subp is an expander-generated procedure which maps an interface
10218 -- alias to a protected wrapper. The interface alias is flagged by
10219 -- pragma Implemented. Ensure that Subp is a procedure when the
10220 -- implementation kind is By_Protected_Procedure or an entry when
10223 if Ada_Version >= Ada_2012
10224 and then Is_Hidden (Subp)
10225 and then Present (Interface_Alias (Subp))
10226 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10228 Check_Pragma_Implemented (Subp);
10231 -- Subp is an interface primitive which overrides another interface
10232 -- primitive marked with pragma Implemented.
10234 if Ada_Version >= Ada_2012
10235 and then Present (Overridden_Operation (Subp))
10236 and then Has_Rep_Pragma
10237 (Overridden_Operation (Subp), Name_Implemented)
10239 -- If the overriding routine is also marked by Implemented, check
10240 -- that the two implementation kinds are conforming.
10242 if Has_Rep_Pragma (Subp, Name_Implemented) then
10243 Check_Pragma_Implemented
10245 Iface_Subp => Overridden_Operation (Subp));
10247 -- Otherwise the overriding routine inherits the implementation
10248 -- kind from the overridden subprogram.
10251 Inherit_Pragma_Implemented
10253 Iface_Subp => Overridden_Operation (Subp));
10257 -- If the operation is a wrapper for a synchronized primitive, it
10258 -- may be called indirectly through a dispatching select. We assume
10259 -- that it will be referenced elsewhere indirectly, and suppress
10260 -- warnings about an unused entity.
10262 if Is_Primitive_Wrapper (Subp)
10263 and then Present (Wrapped_Entity (Subp))
10265 Set_Referenced (Wrapped_Entity (Subp));
10270 end Check_Abstract_Overriding;
10272 ------------------------------------------------
10273 -- Check_Access_Discriminant_Requires_Limited --
10274 ------------------------------------------------
10276 procedure Check_Access_Discriminant_Requires_Limited
10281 -- A discriminant_specification for an access discriminant shall appear
10282 -- only in the declaration for a task or protected type, or for a type
10283 -- with the reserved word 'limited' in its definition or in one of its
10284 -- ancestors (RM 3.7(10)).
10286 -- AI-0063: The proper condition is that type must be immutably limited,
10287 -- or else be a partial view.
10289 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10290 if Is_Limited_View (Current_Scope)
10292 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10293 and then Limited_Present (Parent (Current_Scope)))
10299 ("access discriminants allowed only for limited types", Loc);
10302 end Check_Access_Discriminant_Requires_Limited;
10304 -----------------------------------
10305 -- Check_Aliased_Component_Types --
10306 -----------------------------------
10308 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10312 -- ??? Also need to check components of record extensions, but not
10313 -- components of protected types (which are always limited).
10315 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10316 -- types to be unconstrained. This is safe because it is illegal to
10317 -- create access subtypes to such types with explicit discriminant
10320 if not Is_Limited_Type (T) then
10321 if Ekind (T) = E_Record_Type then
10322 C := First_Component (T);
10323 while Present (C) loop
10325 and then Has_Discriminants (Etype (C))
10326 and then not Is_Constrained (Etype (C))
10327 and then not In_Instance_Body
10328 and then Ada_Version < Ada_2005
10331 ("aliased component must be constrained (RM 3.6(11))",
10335 Next_Component (C);
10338 elsif Ekind (T) = E_Array_Type then
10339 if Has_Aliased_Components (T)
10340 and then Has_Discriminants (Component_Type (T))
10341 and then not Is_Constrained (Component_Type (T))
10342 and then not In_Instance_Body
10343 and then Ada_Version < Ada_2005
10346 ("aliased component type must be constrained (RM 3.6(11))",
10351 end Check_Aliased_Component_Types;
10353 ---------------------------------------
10354 -- Check_Anonymous_Access_Components --
10355 ---------------------------------------
10357 procedure Check_Anonymous_Access_Components
10358 (Typ_Decl : Node_Id;
10361 Comp_List : Node_Id)
10363 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10364 Anon_Access : Entity_Id;
10367 Comp_Def : Node_Id;
10369 Type_Def : Node_Id;
10371 procedure Build_Incomplete_Type_Declaration;
10372 -- If the record type contains components that include an access to the
10373 -- current record, then create an incomplete type declaration for the
10374 -- record, to be used as the designated type of the anonymous access.
10375 -- This is done only once, and only if there is no previous partial
10376 -- view of the type.
10378 function Designates_T (Subt : Node_Id) return Boolean;
10379 -- Check whether a node designates the enclosing record type, or 'Class
10382 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10383 -- Check whether an access definition includes a reference to
10384 -- the enclosing record type. The reference can be a subtype mark
10385 -- in the access definition itself, a 'Class attribute reference, or
10386 -- recursively a reference appearing in a parameter specification
10387 -- or result definition of an access_to_subprogram definition.
10389 --------------------------------------
10390 -- Build_Incomplete_Type_Declaration --
10391 --------------------------------------
10393 procedure Build_Incomplete_Type_Declaration is
10398 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10399 -- it's "is new ... with record" or else "is tagged record ...".
10401 Is_Tagged : constant Boolean :=
10402 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10404 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10406 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10407 and then Tagged_Present (Type_Definition (Typ_Decl)));
10410 -- If there is a previous partial view, no need to create a new one
10411 -- If the partial view, given by Prev, is incomplete, If Prev is
10412 -- a private declaration, full declaration is flagged accordingly.
10414 if Prev /= Typ then
10416 Make_Class_Wide_Type (Prev);
10417 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10418 Set_Etype (Class_Wide_Type (Typ), Typ);
10423 elsif Has_Private_Declaration (Typ) then
10425 -- If we refer to T'Class inside T, and T is the completion of a
10426 -- private type, then make sure the class-wide type exists.
10429 Make_Class_Wide_Type (Typ);
10434 -- If there was a previous anonymous access type, the incomplete
10435 -- type declaration will have been created already.
10437 elsif Present (Current_Entity (Typ))
10438 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10439 and then Full_View (Current_Entity (Typ)) = Typ
10442 and then Comes_From_Source (Current_Entity (Typ))
10443 and then not Is_Tagged_Type (Current_Entity (Typ))
10445 Make_Class_Wide_Type (Typ);
10447 ("incomplete view of tagged type should be declared tagged??",
10448 Parent (Current_Entity (Typ)));
10453 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10454 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10456 -- Type has already been inserted into the current scope. Remove
10457 -- it, and add incomplete declaration for type, so that subsequent
10458 -- anonymous access types can use it. The entity is unchained from
10459 -- the homonym list and from immediate visibility. After analysis,
10460 -- the entity in the incomplete declaration becomes immediately
10461 -- visible in the record declaration that follows.
10463 H := Current_Entity (Typ);
10466 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10469 and then Homonym (H) /= Typ
10471 H := Homonym (Typ);
10474 Set_Homonym (H, Homonym (Typ));
10477 Insert_Before (Typ_Decl, Decl);
10479 Set_Full_View (Inc_T, Typ);
10483 -- Create a common class-wide type for both views, and set the
10484 -- Etype of the class-wide type to the full view.
10486 Make_Class_Wide_Type (Inc_T);
10487 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10488 Set_Etype (Class_Wide_Type (Typ), Typ);
10491 end Build_Incomplete_Type_Declaration;
10497 function Designates_T (Subt : Node_Id) return Boolean is
10498 Type_Id : constant Name_Id := Chars (Typ);
10500 function Names_T (Nam : Node_Id) return Boolean;
10501 -- The record type has not been introduced in the current scope
10502 -- yet, so we must examine the name of the type itself, either
10503 -- an identifier T, or an expanded name of the form P.T, where
10504 -- P denotes the current scope.
10510 function Names_T (Nam : Node_Id) return Boolean is
10512 if Nkind (Nam) = N_Identifier then
10513 return Chars (Nam) = Type_Id;
10515 elsif Nkind (Nam) = N_Selected_Component then
10516 if Chars (Selector_Name (Nam)) = Type_Id then
10517 if Nkind (Prefix (Nam)) = N_Identifier then
10518 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10520 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10521 return Chars (Selector_Name (Prefix (Nam))) =
10522 Chars (Current_Scope);
10536 -- Start of processing for Designates_T
10539 if Nkind (Subt) = N_Identifier then
10540 return Chars (Subt) = Type_Id;
10542 -- Reference can be through an expanded name which has not been
10543 -- analyzed yet, and which designates enclosing scopes.
10545 elsif Nkind (Subt) = N_Selected_Component then
10546 if Names_T (Subt) then
10549 -- Otherwise it must denote an entity that is already visible.
10550 -- The access definition may name a subtype of the enclosing
10551 -- type, if there is a previous incomplete declaration for it.
10554 Find_Selected_Component (Subt);
10556 Is_Entity_Name (Subt)
10557 and then Scope (Entity (Subt)) = Current_Scope
10559 (Chars (Base_Type (Entity (Subt))) = Type_Id
10561 (Is_Class_Wide_Type (Entity (Subt))
10563 Chars (Etype (Base_Type (Entity (Subt)))) =
10567 -- A reference to the current type may appear as the prefix of
10568 -- a 'Class attribute.
10570 elsif Nkind (Subt) = N_Attribute_Reference
10571 and then Attribute_Name (Subt) = Name_Class
10573 return Names_T (Prefix (Subt));
10584 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10585 Param_Spec : Node_Id;
10587 Acc_Subprg : constant Node_Id :=
10588 Access_To_Subprogram_Definition (Acc_Def);
10591 if No (Acc_Subprg) then
10592 return Designates_T (Subtype_Mark (Acc_Def));
10595 -- Component is an access_to_subprogram: examine its formals,
10596 -- and result definition in the case of an access_to_function.
10598 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10599 while Present (Param_Spec) loop
10600 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10601 and then Mentions_T (Parameter_Type (Param_Spec))
10605 elsif Designates_T (Parameter_Type (Param_Spec)) then
10612 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10613 if Nkind (Result_Definition (Acc_Subprg)) =
10614 N_Access_Definition
10616 return Mentions_T (Result_Definition (Acc_Subprg));
10618 return Designates_T (Result_Definition (Acc_Subprg));
10625 -- Start of processing for Check_Anonymous_Access_Components
10628 if No (Comp_List) then
10632 Comp := First (Component_Items (Comp_List));
10633 while Present (Comp) loop
10634 if Nkind (Comp) = N_Component_Declaration
10636 (Access_Definition (Component_Definition (Comp)))
10638 Mentions_T (Access_Definition (Component_Definition (Comp)))
10640 Comp_Def := Component_Definition (Comp);
10642 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10644 Build_Incomplete_Type_Declaration;
10645 Anon_Access := Make_Temporary (Loc, 'S');
10647 -- Create a declaration for the anonymous access type: either
10648 -- an access_to_object or an access_to_subprogram.
10650 if Present (Acc_Def) then
10651 if Nkind (Acc_Def) = N_Access_Function_Definition then
10653 Make_Access_Function_Definition (Loc,
10654 Parameter_Specifications =>
10655 Parameter_Specifications (Acc_Def),
10656 Result_Definition => Result_Definition (Acc_Def));
10659 Make_Access_Procedure_Definition (Loc,
10660 Parameter_Specifications =>
10661 Parameter_Specifications (Acc_Def));
10666 Make_Access_To_Object_Definition (Loc,
10667 Subtype_Indication =>
10669 (Subtype_Mark (Access_Definition (Comp_Def))));
10671 Set_Constant_Present
10672 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10674 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10677 Set_Null_Exclusion_Present
10679 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10682 Make_Full_Type_Declaration (Loc,
10683 Defining_Identifier => Anon_Access,
10684 Type_Definition => Type_Def);
10686 Insert_Before (Typ_Decl, Decl);
10689 -- If an access to subprogram, create the extra formals
10691 if Present (Acc_Def) then
10692 Create_Extra_Formals (Designated_Type (Anon_Access));
10694 -- If an access to object, preserve entity of designated type,
10695 -- for ASIS use, before rewriting the component definition.
10702 Desig := Entity (Subtype_Indication (Type_Def));
10704 -- If the access definition is to the current record,
10705 -- the visible entity at this point is an incomplete
10706 -- type. Retrieve the full view to simplify ASIS queries
10708 if Ekind (Desig) = E_Incomplete_Type then
10709 Desig := Full_View (Desig);
10713 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
10718 Make_Component_Definition (Loc,
10719 Subtype_Indication =>
10720 New_Occurrence_Of (Anon_Access, Loc)));
10722 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
10723 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
10725 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
10728 Set_Is_Local_Anonymous_Access (Anon_Access);
10734 if Present (Variant_Part (Comp_List)) then
10738 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
10739 while Present (V) loop
10740 Check_Anonymous_Access_Components
10741 (Typ_Decl, Typ, Prev, Component_List (V));
10742 Next_Non_Pragma (V);
10746 end Check_Anonymous_Access_Components;
10748 ----------------------
10749 -- Check_Completion --
10750 ----------------------
10752 procedure Check_Completion (Body_Id : Node_Id := Empty) is
10755 procedure Post_Error;
10756 -- Post error message for lack of completion for entity E
10762 procedure Post_Error is
10764 procedure Missing_Body;
10765 -- Output missing body message
10771 procedure Missing_Body is
10773 -- Spec is in same unit, so we can post on spec
10775 if In_Same_Source_Unit (Body_Id, E) then
10776 Error_Msg_N ("missing body for &", E);
10778 -- Spec is in a separate unit, so we have to post on the body
10781 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
10785 -- Start of processing for Post_Error
10788 if not Comes_From_Source (E) then
10790 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
10792 -- It may be an anonymous protected type created for a
10793 -- single variable. Post error on variable, if present.
10799 Var := First_Entity (Current_Scope);
10800 while Present (Var) loop
10801 exit when Etype (Var) = E
10802 and then Comes_From_Source (Var);
10807 if Present (Var) then
10814 -- If a generated entity has no completion, then either previous
10815 -- semantic errors have disabled the expansion phase, or else we had
10816 -- missing subunits, or else we are compiling without expansion,
10817 -- or else something is very wrong.
10819 if not Comes_From_Source (E) then
10821 (Serious_Errors_Detected > 0
10822 or else Configurable_Run_Time_Violations > 0
10823 or else Subunits_Missing
10824 or else not Expander_Active);
10827 -- Here for source entity
10830 -- Here if no body to post the error message, so we post the error
10831 -- on the declaration that has no completion. This is not really
10832 -- the right place to post it, think about this later ???
10834 if No (Body_Id) then
10835 if Is_Type (E) then
10837 ("missing full declaration for }", Parent (E), E);
10839 Error_Msg_NE ("missing body for &", Parent (E), E);
10842 -- Package body has no completion for a declaration that appears
10843 -- in the corresponding spec. Post error on the body, with a
10844 -- reference to the non-completed declaration.
10847 Error_Msg_Sloc := Sloc (E);
10849 if Is_Type (E) then
10850 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
10852 elsif Is_Overloadable (E)
10853 and then Current_Entity_In_Scope (E) /= E
10855 -- It may be that the completion is mistyped and appears as
10856 -- a distinct overloading of the entity.
10859 Candidate : constant Entity_Id :=
10860 Current_Entity_In_Scope (E);
10861 Decl : constant Node_Id :=
10862 Unit_Declaration_Node (Candidate);
10865 if Is_Overloadable (Candidate)
10866 and then Ekind (Candidate) = Ekind (E)
10867 and then Nkind (Decl) = N_Subprogram_Body
10868 and then Acts_As_Spec (Decl)
10870 Check_Type_Conformant (Candidate, E);
10886 Pack_Id : constant Entity_Id := Current_Scope;
10888 -- Start of processing for Check_Completion
10891 E := First_Entity (Pack_Id);
10892 while Present (E) loop
10893 if Is_Intrinsic_Subprogram (E) then
10896 -- A Ghost entity declared in a non-Ghost package does not force the
10897 -- need for a body (SPARK RM 6.9(11)).
10899 elsif not Is_Ghost_Entity (Pack_Id) and then Is_Ghost_Entity (E) then
10902 -- The following situation requires special handling: a child unit
10903 -- that appears in the context clause of the body of its parent:
10905 -- procedure Parent.Child (...);
10907 -- with Parent.Child;
10908 -- package body Parent is
10910 -- Here Parent.Child appears as a local entity, but should not be
10911 -- flagged as requiring completion, because it is a compilation
10914 -- Ignore missing completion for a subprogram that does not come from
10915 -- source (including the _Call primitive operation of RAS types,
10916 -- which has to have the flag Comes_From_Source for other purposes):
10917 -- we assume that the expander will provide the missing completion.
10918 -- In case of previous errors, other expansion actions that provide
10919 -- bodies for null procedures with not be invoked, so inhibit message
10922 -- Note that E_Operator is not in the list that follows, because
10923 -- this kind is reserved for predefined operators, that are
10924 -- intrinsic and do not need completion.
10926 elsif Ekind_In (E, E_Function,
10928 E_Generic_Function,
10929 E_Generic_Procedure)
10931 if Has_Completion (E) then
10934 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
10937 elsif Is_Subprogram (E)
10938 and then (not Comes_From_Source (E)
10939 or else Chars (E) = Name_uCall)
10944 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
10948 elsif Nkind (Parent (E)) = N_Procedure_Specification
10949 and then Null_Present (Parent (E))
10950 and then Serious_Errors_Detected > 0
10958 elsif Is_Entry (E) then
10959 if not Has_Completion (E) and then
10960 (Ekind (Scope (E)) = E_Protected_Object
10961 or else Ekind (Scope (E)) = E_Protected_Type)
10966 elsif Is_Package_Or_Generic_Package (E) then
10967 if Unit_Requires_Body (E) then
10968 if not Has_Completion (E)
10969 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
10975 elsif not Is_Child_Unit (E) then
10976 May_Need_Implicit_Body (E);
10979 -- A formal incomplete type (Ada 2012) does not require a completion;
10980 -- other incomplete type declarations do.
10982 elsif Ekind (E) = E_Incomplete_Type
10983 and then No (Underlying_Type (E))
10984 and then not Is_Generic_Type (E)
10988 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
10989 and then not Has_Completion (E)
10993 -- A single task declared in the current scope is a constant, verify
10994 -- that the body of its anonymous type is in the same scope. If the
10995 -- task is defined elsewhere, this may be a renaming declaration for
10996 -- which no completion is needed.
10998 elsif Ekind (E) = E_Constant
10999 and then Ekind (Etype (E)) = E_Task_Type
11000 and then not Has_Completion (Etype (E))
11001 and then Scope (Etype (E)) = Current_Scope
11005 elsif Ekind (E) = E_Protected_Object
11006 and then not Has_Completion (Etype (E))
11010 elsif Ekind (E) = E_Record_Type then
11011 if Is_Tagged_Type (E) then
11012 Check_Abstract_Overriding (E);
11013 Check_Conventions (E);
11016 Check_Aliased_Component_Types (E);
11018 elsif Ekind (E) = E_Array_Type then
11019 Check_Aliased_Component_Types (E);
11025 end Check_Completion;
11027 ------------------------------------
11028 -- Check_CPP_Type_Has_No_Defaults --
11029 ------------------------------------
11031 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11032 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11037 -- Obtain the component list
11039 if Nkind (Tdef) = N_Record_Definition then
11040 Clist := Component_List (Tdef);
11041 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11042 Clist := Component_List (Record_Extension_Part (Tdef));
11045 -- Check all components to ensure no default expressions
11047 if Present (Clist) then
11048 Comp := First (Component_Items (Clist));
11049 while Present (Comp) loop
11050 if Present (Expression (Comp)) then
11052 ("component of imported 'C'P'P type cannot have "
11053 & "default expression", Expression (Comp));
11059 end Check_CPP_Type_Has_No_Defaults;
11061 ----------------------------
11062 -- Check_Delta_Expression --
11063 ----------------------------
11065 procedure Check_Delta_Expression (E : Node_Id) is
11067 if not (Is_Real_Type (Etype (E))) then
11068 Wrong_Type (E, Any_Real);
11070 elsif not Is_OK_Static_Expression (E) then
11071 Flag_Non_Static_Expr
11072 ("non-static expression used for delta value!", E);
11074 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11075 Error_Msg_N ("delta expression must be positive", E);
11081 -- If any of above errors occurred, then replace the incorrect
11082 -- expression by the real 0.1, which should prevent further errors.
11085 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11086 Analyze_And_Resolve (E, Standard_Float);
11087 end Check_Delta_Expression;
11089 -----------------------------
11090 -- Check_Digits_Expression --
11091 -----------------------------
11093 procedure Check_Digits_Expression (E : Node_Id) is
11095 if not (Is_Integer_Type (Etype (E))) then
11096 Wrong_Type (E, Any_Integer);
11098 elsif not Is_OK_Static_Expression (E) then
11099 Flag_Non_Static_Expr
11100 ("non-static expression used for digits value!", E);
11102 elsif Expr_Value (E) <= 0 then
11103 Error_Msg_N ("digits value must be greater than zero", E);
11109 -- If any of above errors occurred, then replace the incorrect
11110 -- expression by the integer 1, which should prevent further errors.
11112 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11113 Analyze_And_Resolve (E, Standard_Integer);
11115 end Check_Digits_Expression;
11117 --------------------------
11118 -- Check_Initialization --
11119 --------------------------
11121 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11123 -- Special processing for limited types
11125 if Is_Limited_Type (T)
11126 and then not In_Instance
11127 and then not In_Inlined_Body
11129 if not OK_For_Limited_Init (T, Exp) then
11131 -- In GNAT mode, this is just a warning, to allow it to be evilly
11132 -- turned off. Otherwise it is a real error.
11136 ("??cannot initialize entities of limited type!", Exp);
11138 elsif Ada_Version < Ada_2005 then
11140 -- The side effect removal machinery may generate illegal Ada
11141 -- code to avoid the usage of access types and 'reference in
11142 -- SPARK mode. Since this is legal code with respect to theorem
11143 -- proving, do not emit the error.
11146 and then Nkind (Exp) = N_Function_Call
11147 and then Nkind (Parent (Exp)) = N_Object_Declaration
11148 and then not Comes_From_Source
11149 (Defining_Identifier (Parent (Exp)))
11155 ("cannot initialize entities of limited type", Exp);
11156 Explain_Limited_Type (T, Exp);
11160 -- Specialize error message according to kind of illegal
11161 -- initial expression.
11163 if Nkind (Exp) = N_Type_Conversion
11164 and then Nkind (Expression (Exp)) = N_Function_Call
11167 ("illegal context for call"
11168 & " to function with limited result", Exp);
11172 ("initialization of limited object requires aggregate "
11173 & "or function call", Exp);
11179 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11180 -- set unless we can be sure that no range check is required.
11182 if (GNATprove_Mode or not Expander_Active)
11183 and then Is_Scalar_Type (T)
11184 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11186 Set_Do_Range_Check (Exp);
11188 end Check_Initialization;
11190 ----------------------
11191 -- Check_Interfaces --
11192 ----------------------
11194 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11195 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11198 Iface_Def : Node_Id;
11199 Iface_Typ : Entity_Id;
11200 Parent_Node : Node_Id;
11202 Is_Task : Boolean := False;
11203 -- Set True if parent type or any progenitor is a task interface
11205 Is_Protected : Boolean := False;
11206 -- Set True if parent type or any progenitor is a protected interface
11208 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11209 -- Check that a progenitor is compatible with declaration. If an error
11210 -- message is output, it is posted on Error_Node.
11216 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11217 Iface_Id : constant Entity_Id :=
11218 Defining_Identifier (Parent (Iface_Def));
11219 Type_Def : Node_Id;
11222 if Nkind (N) = N_Private_Extension_Declaration then
11225 Type_Def := Type_Definition (N);
11228 if Is_Task_Interface (Iface_Id) then
11231 elsif Is_Protected_Interface (Iface_Id) then
11232 Is_Protected := True;
11235 if Is_Synchronized_Interface (Iface_Id) then
11237 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11238 -- extension derived from a synchronized interface must explicitly
11239 -- be declared synchronized, because the full view will be a
11240 -- synchronized type.
11242 if Nkind (N) = N_Private_Extension_Declaration then
11243 if not Synchronized_Present (N) then
11245 ("private extension of& must be explicitly synchronized",
11249 -- However, by 3.9.4(16/2), a full type that is a record extension
11250 -- is never allowed to derive from a synchronized interface (note
11251 -- that interfaces must be excluded from this check, because those
11252 -- are represented by derived type definitions in some cases).
11254 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11255 and then not Interface_Present (Type_Definition (N))
11257 Error_Msg_N ("record extension cannot derive from synchronized "
11258 & "interface", Error_Node);
11262 -- Check that the characteristics of the progenitor are compatible
11263 -- with the explicit qualifier in the declaration.
11264 -- The check only applies to qualifiers that come from source.
11265 -- Limited_Present also appears in the declaration of corresponding
11266 -- records, and the check does not apply to them.
11268 if Limited_Present (Type_Def)
11270 Is_Concurrent_Record_Type (Defining_Identifier (N))
11272 if Is_Limited_Interface (Parent_Type)
11273 and then not Is_Limited_Interface (Iface_Id)
11276 ("progenitor & must be limited interface",
11277 Error_Node, Iface_Id);
11280 (Task_Present (Iface_Def)
11281 or else Protected_Present (Iface_Def)
11282 or else Synchronized_Present (Iface_Def))
11283 and then Nkind (N) /= N_Private_Extension_Declaration
11284 and then not Error_Posted (N)
11287 ("progenitor & must be limited interface",
11288 Error_Node, Iface_Id);
11291 -- Protected interfaces can only inherit from limited, synchronized
11292 -- or protected interfaces.
11294 elsif Nkind (N) = N_Full_Type_Declaration
11295 and then Protected_Present (Type_Def)
11297 if Limited_Present (Iface_Def)
11298 or else Synchronized_Present (Iface_Def)
11299 or else Protected_Present (Iface_Def)
11303 elsif Task_Present (Iface_Def) then
11304 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11305 & "from task interface", Error_Node);
11308 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11309 & "from non-limited interface", Error_Node);
11312 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11313 -- limited and synchronized.
11315 elsif Synchronized_Present (Type_Def) then
11316 if Limited_Present (Iface_Def)
11317 or else Synchronized_Present (Iface_Def)
11321 elsif Protected_Present (Iface_Def)
11322 and then Nkind (N) /= N_Private_Extension_Declaration
11324 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11325 & "from protected interface", Error_Node);
11327 elsif Task_Present (Iface_Def)
11328 and then Nkind (N) /= N_Private_Extension_Declaration
11330 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11331 & "from task interface", Error_Node);
11333 elsif not Is_Limited_Interface (Iface_Id) then
11334 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11335 & "from non-limited interface", Error_Node);
11338 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11339 -- synchronized or task interfaces.
11341 elsif Nkind (N) = N_Full_Type_Declaration
11342 and then Task_Present (Type_Def)
11344 if Limited_Present (Iface_Def)
11345 or else Synchronized_Present (Iface_Def)
11346 or else Task_Present (Iface_Def)
11350 elsif Protected_Present (Iface_Def) then
11351 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11352 & "protected interface", Error_Node);
11355 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11356 & "non-limited interface", Error_Node);
11361 -- Start of processing for Check_Interfaces
11364 if Is_Interface (Parent_Type) then
11365 if Is_Task_Interface (Parent_Type) then
11368 elsif Is_Protected_Interface (Parent_Type) then
11369 Is_Protected := True;
11373 if Nkind (N) = N_Private_Extension_Declaration then
11375 -- Check that progenitors are compatible with declaration
11377 Iface := First (Interface_List (Def));
11378 while Present (Iface) loop
11379 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11381 Parent_Node := Parent (Base_Type (Iface_Typ));
11382 Iface_Def := Type_Definition (Parent_Node);
11384 if not Is_Interface (Iface_Typ) then
11385 Diagnose_Interface (Iface, Iface_Typ);
11387 Check_Ifaces (Iface_Def, Iface);
11393 if Is_Task and Is_Protected then
11395 ("type cannot derive from task and protected interface", N);
11401 -- Full type declaration of derived type.
11402 -- Check compatibility with parent if it is interface type
11404 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11405 and then Is_Interface (Parent_Type)
11407 Parent_Node := Parent (Parent_Type);
11409 -- More detailed checks for interface varieties
11412 (Iface_Def => Type_Definition (Parent_Node),
11413 Error_Node => Subtype_Indication (Type_Definition (N)));
11416 Iface := First (Interface_List (Def));
11417 while Present (Iface) loop
11418 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11420 Parent_Node := Parent (Base_Type (Iface_Typ));
11421 Iface_Def := Type_Definition (Parent_Node);
11423 if not Is_Interface (Iface_Typ) then
11424 Diagnose_Interface (Iface, Iface_Typ);
11427 -- "The declaration of a specific descendant of an interface
11428 -- type freezes the interface type" RM 13.14
11430 Freeze_Before (N, Iface_Typ);
11431 Check_Ifaces (Iface_Def, Error_Node => Iface);
11437 if Is_Task and Is_Protected then
11439 ("type cannot derive from task and protected interface", N);
11441 end Check_Interfaces;
11443 ------------------------------------
11444 -- Check_Or_Process_Discriminants --
11445 ------------------------------------
11447 -- If an incomplete or private type declaration was already given for the
11448 -- type, the discriminants may have already been processed if they were
11449 -- present on the incomplete declaration. In this case a full conformance
11450 -- check has been performed in Find_Type_Name, and we then recheck here
11451 -- some properties that can't be checked on the partial view alone.
11452 -- Otherwise we call Process_Discriminants.
11454 procedure Check_Or_Process_Discriminants
11457 Prev : Entity_Id := Empty)
11460 if Has_Discriminants (T) then
11462 -- Discriminants are already set on T if they were already present
11463 -- on the partial view. Make them visible to component declarations.
11467 -- Discriminant on T (full view) referencing expr on partial view
11469 Prev_D : Entity_Id;
11470 -- Entity of corresponding discriminant on partial view
11473 -- Discriminant specification for full view, expression is
11474 -- the syntactic copy on full view (which has been checked for
11475 -- conformance with partial view), only used here to post error
11479 D := First_Discriminant (T);
11480 New_D := First (Discriminant_Specifications (N));
11481 while Present (D) loop
11482 Prev_D := Current_Entity (D);
11483 Set_Current_Entity (D);
11484 Set_Is_Immediately_Visible (D);
11485 Set_Homonym (D, Prev_D);
11487 -- Handle the case where there is an untagged partial view and
11488 -- the full view is tagged: must disallow discriminants with
11489 -- defaults, unless compiling for Ada 2012, which allows a
11490 -- limited tagged type to have defaulted discriminants (see
11491 -- AI05-0214). However, suppress error here if it was already
11492 -- reported on the default expression of the partial view.
11494 if Is_Tagged_Type (T)
11495 and then Present (Expression (Parent (D)))
11496 and then (not Is_Limited_Type (Current_Scope)
11497 or else Ada_Version < Ada_2012)
11498 and then not Error_Posted (Expression (Parent (D)))
11500 if Ada_Version >= Ada_2012 then
11502 ("discriminants of nonlimited tagged type cannot have "
11504 Expression (New_D));
11507 ("discriminants of tagged type cannot have defaults",
11508 Expression (New_D));
11512 -- Ada 2005 (AI-230): Access discriminant allowed in
11513 -- non-limited record types.
11515 if Ada_Version < Ada_2005 then
11517 -- This restriction gets applied to the full type here. It
11518 -- has already been applied earlier to the partial view.
11520 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11523 Next_Discriminant (D);
11528 elsif Present (Discriminant_Specifications (N)) then
11529 Process_Discriminants (N, Prev);
11531 end Check_Or_Process_Discriminants;
11533 ----------------------
11534 -- Check_Real_Bound --
11535 ----------------------
11537 procedure Check_Real_Bound (Bound : Node_Id) is
11539 if not Is_Real_Type (Etype (Bound)) then
11541 ("bound in real type definition must be of real type", Bound);
11543 elsif not Is_OK_Static_Expression (Bound) then
11544 Flag_Non_Static_Expr
11545 ("non-static expression used for real type bound!", Bound);
11552 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11554 Resolve (Bound, Standard_Float);
11555 end Check_Real_Bound;
11557 ------------------------------
11558 -- Complete_Private_Subtype --
11559 ------------------------------
11561 procedure Complete_Private_Subtype
11564 Full_Base : Entity_Id;
11565 Related_Nod : Node_Id)
11567 Save_Next_Entity : Entity_Id;
11568 Save_Homonym : Entity_Id;
11571 -- Set semantic attributes for (implicit) private subtype completion.
11572 -- If the full type has no discriminants, then it is a copy of the
11573 -- full view of the base. Otherwise, it is a subtype of the base with
11574 -- a possible discriminant constraint. Save and restore the original
11575 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11576 -- not corrupt the entity chain.
11578 -- Note that the type of the full view is the same entity as the type
11579 -- of the partial view. In this fashion, the subtype has access to the
11580 -- correct view of the parent.
11582 Save_Next_Entity := Next_Entity (Full);
11583 Save_Homonym := Homonym (Priv);
11585 case Ekind (Full_Base) is
11586 when E_Record_Type |
11592 Copy_Node (Priv, Full);
11594 Set_Has_Discriminants
11595 (Full, Has_Discriminants (Full_Base));
11596 Set_Has_Unknown_Discriminants
11597 (Full, Has_Unknown_Discriminants (Full_Base));
11598 Set_First_Entity (Full, First_Entity (Full_Base));
11599 Set_Last_Entity (Full, Last_Entity (Full_Base));
11601 -- If the underlying base type is constrained, we know that the
11602 -- full view of the subtype is constrained as well (the converse
11603 -- is not necessarily true).
11605 if Is_Constrained (Full_Base) then
11606 Set_Is_Constrained (Full);
11610 Copy_Node (Full_Base, Full);
11612 Set_Chars (Full, Chars (Priv));
11613 Conditional_Delay (Full, Priv);
11614 Set_Sloc (Full, Sloc (Priv));
11617 Set_Next_Entity (Full, Save_Next_Entity);
11618 Set_Homonym (Full, Save_Homonym);
11619 Set_Associated_Node_For_Itype (Full, Related_Nod);
11621 -- Set common attributes for all subtypes: kind, convention, etc.
11623 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11624 Set_Convention (Full, Convention (Full_Base));
11626 -- The Etype of the full view is inconsistent. Gigi needs to see the
11627 -- structural full view, which is what the current scheme gives: the
11628 -- Etype of the full view is the etype of the full base. However, if the
11629 -- full base is a derived type, the full view then looks like a subtype
11630 -- of the parent, not a subtype of the full base. If instead we write:
11632 -- Set_Etype (Full, Full_Base);
11634 -- then we get inconsistencies in the front-end (confusion between
11635 -- views). Several outstanding bugs are related to this ???
11637 Set_Is_First_Subtype (Full, False);
11638 Set_Scope (Full, Scope (Priv));
11639 Set_Size_Info (Full, Full_Base);
11640 Set_RM_Size (Full, RM_Size (Full_Base));
11641 Set_Is_Itype (Full);
11643 -- A subtype of a private-type-without-discriminants, whose full-view
11644 -- has discriminants with default expressions, is not constrained.
11646 if not Has_Discriminants (Priv) then
11647 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11649 if Has_Discriminants (Full_Base) then
11650 Set_Discriminant_Constraint
11651 (Full, Discriminant_Constraint (Full_Base));
11653 -- The partial view may have been indefinite, the full view
11656 Set_Has_Unknown_Discriminants
11657 (Full, Has_Unknown_Discriminants (Full_Base));
11661 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11662 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11664 -- Freeze the private subtype entity if its parent is delayed, and not
11665 -- already frozen. We skip this processing if the type is an anonymous
11666 -- subtype of a record component, or is the corresponding record of a
11667 -- protected type, since these are processed when the enclosing type
11670 if not Is_Type (Scope (Full)) then
11671 Set_Has_Delayed_Freeze (Full,
11672 Has_Delayed_Freeze (Full_Base)
11673 and then (not Is_Frozen (Full_Base)));
11676 Set_Freeze_Node (Full, Empty);
11677 Set_Is_Frozen (Full, False);
11678 Set_Full_View (Priv, Full);
11680 if Has_Discriminants (Full) then
11681 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
11682 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
11684 if Has_Unknown_Discriminants (Full) then
11685 Set_Discriminant_Constraint (Full, No_Elist);
11689 if Ekind (Full_Base) = E_Record_Type
11690 and then Has_Discriminants (Full_Base)
11691 and then Has_Discriminants (Priv) -- might not, if errors
11692 and then not Has_Unknown_Discriminants (Priv)
11693 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
11695 Create_Constrained_Components
11696 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
11698 -- If the full base is itself derived from private, build a congruent
11699 -- subtype of its underlying type, for use by the back end. For a
11700 -- constrained record component, the declaration cannot be placed on
11701 -- the component list, but it must nevertheless be built an analyzed, to
11702 -- supply enough information for Gigi to compute the size of component.
11704 elsif Ekind (Full_Base) in Private_Kind
11705 and then Is_Derived_Type (Full_Base)
11706 and then Has_Discriminants (Full_Base)
11707 and then (Ekind (Current_Scope) /= E_Record_Subtype)
11709 if not Is_Itype (Priv)
11711 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
11713 Build_Underlying_Full_View
11714 (Parent (Priv), Full, Etype (Full_Base));
11716 elsif Nkind (Related_Nod) = N_Component_Declaration then
11717 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
11720 elsif Is_Record_Type (Full_Base) then
11722 -- Show Full is simply a renaming of Full_Base
11724 Set_Cloned_Subtype (Full, Full_Base);
11727 -- It is unsafe to share the bounds of a scalar type, because the Itype
11728 -- is elaborated on demand, and if a bound is non-static then different
11729 -- orders of elaboration in different units will lead to different
11730 -- external symbols.
11732 if Is_Scalar_Type (Full_Base) then
11733 Set_Scalar_Range (Full,
11734 Make_Range (Sloc (Related_Nod),
11736 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
11738 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
11740 -- This completion inherits the bounds of the full parent, but if
11741 -- the parent is an unconstrained floating point type, so is the
11744 if Is_Floating_Point_Type (Full_Base) then
11745 Set_Includes_Infinities
11746 (Scalar_Range (Full), Has_Infinities (Full_Base));
11750 -- ??? It seems that a lot of fields are missing that should be copied
11751 -- from Full_Base to Full. Here are some that are introduced in a
11752 -- non-disruptive way but a cleanup is necessary.
11754 if Is_Tagged_Type (Full_Base) then
11755 Set_Is_Tagged_Type (Full);
11756 Set_Direct_Primitive_Operations
11757 (Full, Direct_Primitive_Operations (Full_Base));
11758 Set_No_Tagged_Streams_Pragma
11759 (Full, No_Tagged_Streams_Pragma (Full_Base));
11761 -- Inherit class_wide type of full_base in case the partial view was
11762 -- not tagged. Otherwise it has already been created when the private
11763 -- subtype was analyzed.
11765 if No (Class_Wide_Type (Full)) then
11766 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
11769 -- If this is a subtype of a protected or task type, constrain its
11770 -- corresponding record, unless this is a subtype without constraints,
11771 -- i.e. a simple renaming as with an actual subtype in an instance.
11773 elsif Is_Concurrent_Type (Full_Base) then
11774 if Has_Discriminants (Full)
11775 and then Present (Corresponding_Record_Type (Full_Base))
11777 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
11779 Set_Corresponding_Record_Type (Full,
11780 Constrain_Corresponding_Record
11781 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
11784 Set_Corresponding_Record_Type (Full,
11785 Corresponding_Record_Type (Full_Base));
11789 -- Link rep item chain, and also setting of Has_Predicates from private
11790 -- subtype to full subtype, since we will need these on the full subtype
11791 -- to create the predicate function. Note that the full subtype may
11792 -- already have rep items, inherited from the full view of the base
11793 -- type, so we must be sure not to overwrite these entries.
11798 Next_Item : Node_Id;
11801 Item := First_Rep_Item (Full);
11803 -- If no existing rep items on full type, we can just link directly
11804 -- to the list of items on the private type, if any exist.. Same if
11805 -- the rep items are only those inherited from the base
11808 or else Nkind (Item) /= N_Aspect_Specification
11809 or else Entity (Item) = Full_Base)
11810 and then Present (First_Rep_Item (Priv))
11812 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11814 -- Otherwise, search to the end of items currently linked to the full
11815 -- subtype and append the private items to the end. However, if Priv
11816 -- and Full already have the same list of rep items, then the append
11817 -- is not done, as that would create a circularity.
11819 elsif Item /= First_Rep_Item (Priv) then
11822 Next_Item := Next_Rep_Item (Item);
11823 exit when No (Next_Item);
11826 -- If the private view has aspect specifications, the full view
11827 -- inherits them. Since these aspects may already have been
11828 -- attached to the full view during derivation, do not append
11829 -- them if already present.
11831 if Item = First_Rep_Item (Priv) then
11837 -- And link the private type items at the end of the chain
11840 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
11845 -- Make sure Has_Predicates is set on full type if it is set on the
11846 -- private type. Note that it may already be set on the full type and
11847 -- if so, we don't want to unset it. Similarly, propagate information
11848 -- about delayed aspects, because the corresponding pragmas must be
11849 -- analyzed when one of the views is frozen. This last step is needed
11850 -- in particular when the full type is a scalar type for which an
11851 -- anonymous base type is constructed.
11853 if Has_Predicates (Priv) then
11854 Set_Has_Predicates (Full);
11857 if Has_Delayed_Aspects (Priv) then
11858 Set_Has_Delayed_Aspects (Full);
11860 end Complete_Private_Subtype;
11862 ----------------------------
11863 -- Constant_Redeclaration --
11864 ----------------------------
11866 procedure Constant_Redeclaration
11871 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
11872 Obj_Def : constant Node_Id := Object_Definition (N);
11875 procedure Check_Possible_Deferred_Completion
11876 (Prev_Id : Entity_Id;
11877 Prev_Obj_Def : Node_Id;
11878 Curr_Obj_Def : Node_Id);
11879 -- Determine whether the two object definitions describe the partial
11880 -- and the full view of a constrained deferred constant. Generate
11881 -- a subtype for the full view and verify that it statically matches
11882 -- the subtype of the partial view.
11884 procedure Check_Recursive_Declaration (Typ : Entity_Id);
11885 -- If deferred constant is an access type initialized with an allocator,
11886 -- check whether there is an illegal recursion in the definition,
11887 -- through a default value of some record subcomponent. This is normally
11888 -- detected when generating init procs, but requires this additional
11889 -- mechanism when expansion is disabled.
11891 ----------------------------------------
11892 -- Check_Possible_Deferred_Completion --
11893 ----------------------------------------
11895 procedure Check_Possible_Deferred_Completion
11896 (Prev_Id : Entity_Id;
11897 Prev_Obj_Def : Node_Id;
11898 Curr_Obj_Def : Node_Id)
11901 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
11902 and then Present (Constraint (Prev_Obj_Def))
11903 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
11904 and then Present (Constraint (Curr_Obj_Def))
11907 Loc : constant Source_Ptr := Sloc (N);
11908 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
11909 Decl : constant Node_Id :=
11910 Make_Subtype_Declaration (Loc,
11911 Defining_Identifier => Def_Id,
11912 Subtype_Indication =>
11913 Relocate_Node (Curr_Obj_Def));
11916 Insert_Before_And_Analyze (N, Decl);
11917 Set_Etype (Id, Def_Id);
11919 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
11920 Error_Msg_Sloc := Sloc (Prev_Id);
11921 Error_Msg_N ("subtype does not statically match deferred "
11922 & "declaration #", N);
11926 end Check_Possible_Deferred_Completion;
11928 ---------------------------------
11929 -- Check_Recursive_Declaration --
11930 ---------------------------------
11932 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
11936 if Is_Record_Type (Typ) then
11937 Comp := First_Component (Typ);
11938 while Present (Comp) loop
11939 if Comes_From_Source (Comp) then
11940 if Present (Expression (Parent (Comp)))
11941 and then Is_Entity_Name (Expression (Parent (Comp)))
11942 and then Entity (Expression (Parent (Comp))) = Prev
11944 Error_Msg_Sloc := Sloc (Parent (Comp));
11946 ("illegal circularity with declaration for & #",
11950 elsif Is_Record_Type (Etype (Comp)) then
11951 Check_Recursive_Declaration (Etype (Comp));
11955 Next_Component (Comp);
11958 end Check_Recursive_Declaration;
11960 -- Start of processing for Constant_Redeclaration
11963 if Nkind (Parent (Prev)) = N_Object_Declaration then
11964 if Nkind (Object_Definition
11965 (Parent (Prev))) = N_Subtype_Indication
11967 -- Find type of new declaration. The constraints of the two
11968 -- views must match statically, but there is no point in
11969 -- creating an itype for the full view.
11971 if Nkind (Obj_Def) = N_Subtype_Indication then
11972 Find_Type (Subtype_Mark (Obj_Def));
11973 New_T := Entity (Subtype_Mark (Obj_Def));
11976 Find_Type (Obj_Def);
11977 New_T := Entity (Obj_Def);
11983 -- The full view may impose a constraint, even if the partial
11984 -- view does not, so construct the subtype.
11986 New_T := Find_Type_Of_Object (Obj_Def, N);
11991 -- Current declaration is illegal, diagnosed below in Enter_Name
11997 -- If previous full declaration or a renaming declaration exists, or if
11998 -- a homograph is present, let Enter_Name handle it, either with an
11999 -- error or with the removal of an overridden implicit subprogram.
12000 -- The previous one is a full declaration if it has an expression
12001 -- (which in the case of an aggregate is indicated by the Init flag).
12003 if Ekind (Prev) /= E_Constant
12004 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12005 or else Present (Expression (Parent (Prev)))
12006 or else Has_Init_Expression (Parent (Prev))
12007 or else Present (Full_View (Prev))
12011 -- Verify that types of both declarations match, or else that both types
12012 -- are anonymous access types whose designated subtypes statically match
12013 -- (as allowed in Ada 2005 by AI-385).
12015 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12017 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12018 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12019 or else Is_Access_Constant (Etype (New_T)) /=
12020 Is_Access_Constant (Etype (Prev))
12021 or else Can_Never_Be_Null (Etype (New_T)) /=
12022 Can_Never_Be_Null (Etype (Prev))
12023 or else Null_Exclusion_Present (Parent (Prev)) /=
12024 Null_Exclusion_Present (Parent (Id))
12025 or else not Subtypes_Statically_Match
12026 (Designated_Type (Etype (Prev)),
12027 Designated_Type (Etype (New_T))))
12029 Error_Msg_Sloc := Sloc (Prev);
12030 Error_Msg_N ("type does not match declaration#", N);
12031 Set_Full_View (Prev, Id);
12032 Set_Etype (Id, Any_Type);
12034 -- A deferred constant whose type is an anonymous array is always
12035 -- illegal (unless imported). A detailed error message might be
12036 -- helpful for Ada beginners.
12038 if Nkind (Object_Definition (Parent (Prev)))
12039 = N_Constrained_Array_Definition
12040 and then Nkind (Object_Definition (N))
12041 = N_Constrained_Array_Definition
12043 Error_Msg_N ("\each anonymous array is a distinct type", N);
12044 Error_Msg_N ("a deferred constant must have a named type",
12045 Object_Definition (Parent (Prev)));
12049 Null_Exclusion_Present (Parent (Prev))
12050 and then not Null_Exclusion_Present (N)
12052 Error_Msg_Sloc := Sloc (Prev);
12053 Error_Msg_N ("null-exclusion does not match declaration#", N);
12054 Set_Full_View (Prev, Id);
12055 Set_Etype (Id, Any_Type);
12057 -- If so, process the full constant declaration
12060 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12061 -- the deferred declaration is constrained, then the subtype defined
12062 -- by the subtype_indication in the full declaration shall match it
12065 Check_Possible_Deferred_Completion
12067 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12068 Curr_Obj_Def => Obj_Def);
12070 Set_Full_View (Prev, Id);
12071 Set_Is_Public (Id, Is_Public (Prev));
12072 Set_Is_Internal (Id);
12073 Append_Entity (Id, Current_Scope);
12075 -- Check ALIASED present if present before (RM 7.4(7))
12077 if Is_Aliased (Prev)
12078 and then not Aliased_Present (N)
12080 Error_Msg_Sloc := Sloc (Prev);
12081 Error_Msg_N ("ALIASED required (see declaration #)", N);
12084 -- Check that placement is in private part and that the incomplete
12085 -- declaration appeared in the visible part.
12087 if Ekind (Current_Scope) = E_Package
12088 and then not In_Private_Part (Current_Scope)
12090 Error_Msg_Sloc := Sloc (Prev);
12092 ("full constant for declaration # must be in private part", N);
12094 elsif Ekind (Current_Scope) = E_Package
12096 List_Containing (Parent (Prev)) /=
12097 Visible_Declarations (Package_Specification (Current_Scope))
12100 ("deferred constant must be declared in visible part",
12104 if Is_Access_Type (T)
12105 and then Nkind (Expression (N)) = N_Allocator
12107 Check_Recursive_Declaration (Designated_Type (T));
12110 -- A deferred constant is a visible entity. If type has invariants,
12111 -- verify that the initial value satisfies them.
12113 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12115 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12118 end Constant_Redeclaration;
12120 ----------------------
12121 -- Constrain_Access --
12122 ----------------------
12124 procedure Constrain_Access
12125 (Def_Id : in out Entity_Id;
12127 Related_Nod : Node_Id)
12129 T : constant Entity_Id := Entity (Subtype_Mark (S));
12130 Desig_Type : constant Entity_Id := Designated_Type (T);
12131 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12132 Constraint_OK : Boolean := True;
12135 if Is_Array_Type (Desig_Type) then
12136 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12138 elsif (Is_Record_Type (Desig_Type)
12139 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12140 and then not Is_Constrained (Desig_Type)
12142 -- ??? The following code is a temporary bypass to ignore a
12143 -- discriminant constraint on access type if it is constraining
12144 -- the current record. Avoid creating the implicit subtype of the
12145 -- record we are currently compiling since right now, we cannot
12146 -- handle these. For now, just return the access type itself.
12148 if Desig_Type = Current_Scope
12149 and then No (Def_Id)
12151 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12152 Def_Id := Entity (Subtype_Mark (S));
12154 -- This call added to ensure that the constraint is analyzed
12155 -- (needed for a B test). Note that we still return early from
12156 -- this procedure to avoid recursive processing. ???
12158 Constrain_Discriminated_Type
12159 (Desig_Subtype, S, Related_Nod, For_Access => True);
12163 -- Enforce rule that the constraint is illegal if there is an
12164 -- unconstrained view of the designated type. This means that the
12165 -- partial view (either a private type declaration or a derivation
12166 -- from a private type) has no discriminants. (Defect Report
12167 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12169 -- Rule updated for Ada 2005: The private type is said to have
12170 -- a constrained partial view, given that objects of the type
12171 -- can be declared. Furthermore, the rule applies to all access
12172 -- types, unlike the rule concerning default discriminants (see
12175 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12176 and then Has_Private_Declaration (Desig_Type)
12177 and then In_Open_Scopes (Scope (Desig_Type))
12178 and then Has_Discriminants (Desig_Type)
12181 Pack : constant Node_Id :=
12182 Unit_Declaration_Node (Scope (Desig_Type));
12187 if Nkind (Pack) = N_Package_Declaration then
12188 Decls := Visible_Declarations (Specification (Pack));
12189 Decl := First (Decls);
12190 while Present (Decl) loop
12191 if (Nkind (Decl) = N_Private_Type_Declaration
12192 and then Chars (Defining_Identifier (Decl)) =
12193 Chars (Desig_Type))
12196 (Nkind (Decl) = N_Full_Type_Declaration
12198 Chars (Defining_Identifier (Decl)) =
12200 and then Is_Derived_Type (Desig_Type)
12202 Has_Private_Declaration (Etype (Desig_Type)))
12204 if No (Discriminant_Specifications (Decl)) then
12206 ("cannot constrain access type if designated "
12207 & "type has constrained partial view", S);
12219 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12220 For_Access => True);
12222 elsif Is_Concurrent_Type (Desig_Type)
12223 and then not Is_Constrained (Desig_Type)
12225 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12228 Error_Msg_N ("invalid constraint on access type", S);
12230 -- We simply ignore an invalid constraint
12232 Desig_Subtype := Desig_Type;
12233 Constraint_OK := False;
12236 if No (Def_Id) then
12237 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12239 Set_Ekind (Def_Id, E_Access_Subtype);
12242 if Constraint_OK then
12243 Set_Etype (Def_Id, Base_Type (T));
12245 if Is_Private_Type (Desig_Type) then
12246 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12249 Set_Etype (Def_Id, Any_Type);
12252 Set_Size_Info (Def_Id, T);
12253 Set_Is_Constrained (Def_Id, Constraint_OK);
12254 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12255 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12256 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12258 Conditional_Delay (Def_Id, T);
12260 -- AI-363 : Subtypes of general access types whose designated types have
12261 -- default discriminants are disallowed. In instances, the rule has to
12262 -- be checked against the actual, of which T is the subtype. In a
12263 -- generic body, the rule is checked assuming that the actual type has
12264 -- defaulted discriminants.
12266 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12267 if Ekind (Base_Type (T)) = E_General_Access_Type
12268 and then Has_Defaulted_Discriminants (Desig_Type)
12270 if Ada_Version < Ada_2005 then
12272 ("access subtype of general access type would not " &
12273 "be allowed in Ada 2005?y?", S);
12276 ("access subtype of general access type not allowed", S);
12279 Error_Msg_N ("\discriminants have defaults", S);
12281 elsif Is_Access_Type (T)
12282 and then Is_Generic_Type (Desig_Type)
12283 and then Has_Discriminants (Desig_Type)
12284 and then In_Package_Body (Current_Scope)
12286 if Ada_Version < Ada_2005 then
12288 ("access subtype would not be allowed in generic body "
12289 & "in Ada 2005?y?", S);
12292 ("access subtype not allowed in generic body", S);
12296 ("\designated type is a discriminated formal", S);
12299 end Constrain_Access;
12301 ---------------------
12302 -- Constrain_Array --
12303 ---------------------
12305 procedure Constrain_Array
12306 (Def_Id : in out Entity_Id;
12308 Related_Nod : Node_Id;
12309 Related_Id : Entity_Id;
12310 Suffix : Character)
12312 C : constant Node_Id := Constraint (SI);
12313 Number_Of_Constraints : Nat := 0;
12316 Constraint_OK : Boolean := True;
12319 T := Entity (Subtype_Mark (SI));
12321 if Is_Access_Type (T) then
12322 T := Designated_Type (T);
12325 -- If an index constraint follows a subtype mark in a subtype indication
12326 -- then the type or subtype denoted by the subtype mark must not already
12327 -- impose an index constraint. The subtype mark must denote either an
12328 -- unconstrained array type or an access type whose designated type
12329 -- is such an array type... (RM 3.6.1)
12331 if Is_Constrained (T) then
12332 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12333 Constraint_OK := False;
12336 S := First (Constraints (C));
12337 while Present (S) loop
12338 Number_Of_Constraints := Number_Of_Constraints + 1;
12342 -- In either case, the index constraint must provide a discrete
12343 -- range for each index of the array type and the type of each
12344 -- discrete range must be the same as that of the corresponding
12345 -- index. (RM 3.6.1)
12347 if Number_Of_Constraints /= Number_Dimensions (T) then
12348 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12349 Constraint_OK := False;
12352 S := First (Constraints (C));
12353 Index := First_Index (T);
12356 -- Apply constraints to each index type
12358 for J in 1 .. Number_Of_Constraints loop
12359 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12367 if No (Def_Id) then
12369 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12370 Set_Parent (Def_Id, Related_Nod);
12373 Set_Ekind (Def_Id, E_Array_Subtype);
12376 Set_Size_Info (Def_Id, (T));
12377 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12378 Set_Etype (Def_Id, Base_Type (T));
12380 if Constraint_OK then
12381 Set_First_Index (Def_Id, First (Constraints (C)));
12383 Set_First_Index (Def_Id, First_Index (T));
12386 Set_Is_Constrained (Def_Id, True);
12387 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12388 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12390 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12391 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12393 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12394 -- We need to initialize the attribute because if Def_Id is previously
12395 -- analyzed through a limited_with clause, it will have the attributes
12396 -- of an incomplete type, one of which is an Elist that overlaps the
12397 -- Packed_Array_Impl_Type field.
12399 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12401 -- Build a freeze node if parent still needs one. Also make sure that
12402 -- the Depends_On_Private status is set because the subtype will need
12403 -- reprocessing at the time the base type does, and also we must set a
12404 -- conditional delay.
12406 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12407 Conditional_Delay (Def_Id, T);
12408 end Constrain_Array;
12410 ------------------------------
12411 -- Constrain_Component_Type --
12412 ------------------------------
12414 function Constrain_Component_Type
12416 Constrained_Typ : Entity_Id;
12417 Related_Node : Node_Id;
12419 Constraints : Elist_Id) return Entity_Id
12421 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12422 Compon_Type : constant Entity_Id := Etype (Comp);
12424 function Build_Constrained_Array_Type
12425 (Old_Type : Entity_Id) return Entity_Id;
12426 -- If Old_Type is an array type, one of whose indexes is constrained
12427 -- by a discriminant, build an Itype whose constraint replaces the
12428 -- discriminant with its value in the constraint.
12430 function Build_Constrained_Discriminated_Type
12431 (Old_Type : Entity_Id) return Entity_Id;
12432 -- Ditto for record components
12434 function Build_Constrained_Access_Type
12435 (Old_Type : Entity_Id) return Entity_Id;
12436 -- Ditto for access types. Makes use of previous two functions, to
12437 -- constrain designated type.
12439 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12440 -- T is an array or discriminated type, C is a list of constraints
12441 -- that apply to T. This routine builds the constrained subtype.
12443 function Is_Discriminant (Expr : Node_Id) return Boolean;
12444 -- Returns True if Expr is a discriminant
12446 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12447 -- Find the value of discriminant Discrim in Constraint
12449 -----------------------------------
12450 -- Build_Constrained_Access_Type --
12451 -----------------------------------
12453 function Build_Constrained_Access_Type
12454 (Old_Type : Entity_Id) return Entity_Id
12456 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12458 Desig_Subtype : Entity_Id;
12462 -- if the original access type was not embedded in the enclosing
12463 -- type definition, there is no need to produce a new access
12464 -- subtype. In fact every access type with an explicit constraint
12465 -- generates an itype whose scope is the enclosing record.
12467 if not Is_Type (Scope (Old_Type)) then
12470 elsif Is_Array_Type (Desig_Type) then
12471 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12473 elsif Has_Discriminants (Desig_Type) then
12475 -- This may be an access type to an enclosing record type for
12476 -- which we are constructing the constrained components. Return
12477 -- the enclosing record subtype. This is not always correct,
12478 -- but avoids infinite recursion. ???
12480 Desig_Subtype := Any_Type;
12482 for J in reverse 0 .. Scope_Stack.Last loop
12483 Scop := Scope_Stack.Table (J).Entity;
12486 and then Base_Type (Scop) = Base_Type (Desig_Type)
12488 Desig_Subtype := Scop;
12491 exit when not Is_Type (Scop);
12494 if Desig_Subtype = Any_Type then
12496 Build_Constrained_Discriminated_Type (Desig_Type);
12503 if Desig_Subtype /= Desig_Type then
12505 -- The Related_Node better be here or else we won't be able
12506 -- to attach new itypes to a node in the tree.
12508 pragma Assert (Present (Related_Node));
12510 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12512 Set_Etype (Itype, Base_Type (Old_Type));
12513 Set_Size_Info (Itype, (Old_Type));
12514 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12515 Set_Depends_On_Private (Itype, Has_Private_Component
12517 Set_Is_Access_Constant (Itype, Is_Access_Constant
12520 -- The new itype needs freezing when it depends on a not frozen
12521 -- type and the enclosing subtype needs freezing.
12523 if Has_Delayed_Freeze (Constrained_Typ)
12524 and then not Is_Frozen (Constrained_Typ)
12526 Conditional_Delay (Itype, Base_Type (Old_Type));
12534 end Build_Constrained_Access_Type;
12536 ----------------------------------
12537 -- Build_Constrained_Array_Type --
12538 ----------------------------------
12540 function Build_Constrained_Array_Type
12541 (Old_Type : Entity_Id) return Entity_Id
12545 Old_Index : Node_Id;
12546 Range_Node : Node_Id;
12547 Constr_List : List_Id;
12549 Need_To_Create_Itype : Boolean := False;
12552 Old_Index := First_Index (Old_Type);
12553 while Present (Old_Index) loop
12554 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12556 if Is_Discriminant (Lo_Expr)
12558 Is_Discriminant (Hi_Expr)
12560 Need_To_Create_Itype := True;
12563 Next_Index (Old_Index);
12566 if Need_To_Create_Itype then
12567 Constr_List := New_List;
12569 Old_Index := First_Index (Old_Type);
12570 while Present (Old_Index) loop
12571 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12573 if Is_Discriminant (Lo_Expr) then
12574 Lo_Expr := Get_Discr_Value (Lo_Expr);
12577 if Is_Discriminant (Hi_Expr) then
12578 Hi_Expr := Get_Discr_Value (Hi_Expr);
12583 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12585 Append (Range_Node, To => Constr_List);
12587 Next_Index (Old_Index);
12590 return Build_Subtype (Old_Type, Constr_List);
12595 end Build_Constrained_Array_Type;
12597 ------------------------------------------
12598 -- Build_Constrained_Discriminated_Type --
12599 ------------------------------------------
12601 function Build_Constrained_Discriminated_Type
12602 (Old_Type : Entity_Id) return Entity_Id
12605 Constr_List : List_Id;
12606 Old_Constraint : Elmt_Id;
12608 Need_To_Create_Itype : Boolean := False;
12611 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12612 while Present (Old_Constraint) loop
12613 Expr := Node (Old_Constraint);
12615 if Is_Discriminant (Expr) then
12616 Need_To_Create_Itype := True;
12619 Next_Elmt (Old_Constraint);
12622 if Need_To_Create_Itype then
12623 Constr_List := New_List;
12625 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12626 while Present (Old_Constraint) loop
12627 Expr := Node (Old_Constraint);
12629 if Is_Discriminant (Expr) then
12630 Expr := Get_Discr_Value (Expr);
12633 Append (New_Copy_Tree (Expr), To => Constr_List);
12635 Next_Elmt (Old_Constraint);
12638 return Build_Subtype (Old_Type, Constr_List);
12643 end Build_Constrained_Discriminated_Type;
12645 -------------------
12646 -- Build_Subtype --
12647 -------------------
12649 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12651 Subtyp_Decl : Node_Id;
12652 Def_Id : Entity_Id;
12653 Btyp : Entity_Id := Base_Type (T);
12656 -- The Related_Node better be here or else we won't be able to
12657 -- attach new itypes to a node in the tree.
12659 pragma Assert (Present (Related_Node));
12661 -- If the view of the component's type is incomplete or private
12662 -- with unknown discriminants, then the constraint must be applied
12663 -- to the full type.
12665 if Has_Unknown_Discriminants (Btyp)
12666 and then Present (Underlying_Type (Btyp))
12668 Btyp := Underlying_Type (Btyp);
12672 Make_Subtype_Indication (Loc,
12673 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
12674 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
12676 Def_Id := Create_Itype (Ekind (T), Related_Node);
12679 Make_Subtype_Declaration (Loc,
12680 Defining_Identifier => Def_Id,
12681 Subtype_Indication => Indic);
12683 Set_Parent (Subtyp_Decl, Parent (Related_Node));
12685 -- Itypes must be analyzed with checks off (see package Itypes)
12687 Analyze (Subtyp_Decl, Suppress => All_Checks);
12692 ---------------------
12693 -- Get_Discr_Value --
12694 ---------------------
12696 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
12701 -- The discriminant may be declared for the type, in which case we
12702 -- find it by iterating over the list of discriminants. If the
12703 -- discriminant is inherited from a parent type, it appears as the
12704 -- corresponding discriminant of the current type. This will be the
12705 -- case when constraining an inherited component whose constraint is
12706 -- given by a discriminant of the parent.
12708 D := First_Discriminant (Typ);
12709 E := First_Elmt (Constraints);
12711 while Present (D) loop
12712 if D = Entity (Discrim)
12713 or else D = CR_Discriminant (Entity (Discrim))
12714 or else Corresponding_Discriminant (D) = Entity (Discrim)
12719 Next_Discriminant (D);
12723 -- The Corresponding_Discriminant mechanism is incomplete, because
12724 -- the correspondence between new and old discriminants is not one
12725 -- to one: one new discriminant can constrain several old ones. In
12726 -- that case, scan sequentially the stored_constraint, the list of
12727 -- discriminants of the parents, and the constraints.
12729 -- Previous code checked for the present of the Stored_Constraint
12730 -- list for the derived type, but did not use it at all. Should it
12731 -- be present when the component is a discriminated task type?
12733 if Is_Derived_Type (Typ)
12734 and then Scope (Entity (Discrim)) = Etype (Typ)
12736 D := First_Discriminant (Etype (Typ));
12737 E := First_Elmt (Constraints);
12738 while Present (D) loop
12739 if D = Entity (Discrim) then
12743 Next_Discriminant (D);
12748 -- Something is wrong if we did not find the value
12750 raise Program_Error;
12751 end Get_Discr_Value;
12753 ---------------------
12754 -- Is_Discriminant --
12755 ---------------------
12757 function Is_Discriminant (Expr : Node_Id) return Boolean is
12758 Discrim_Scope : Entity_Id;
12761 if Denotes_Discriminant (Expr) then
12762 Discrim_Scope := Scope (Entity (Expr));
12764 -- Either we have a reference to one of Typ's discriminants,
12766 pragma Assert (Discrim_Scope = Typ
12768 -- or to the discriminants of the parent type, in the case
12769 -- of a derivation of a tagged type with variants.
12771 or else Discrim_Scope = Etype (Typ)
12772 or else Full_View (Discrim_Scope) = Etype (Typ)
12774 -- or same as above for the case where the discriminants
12775 -- were declared in Typ's private view.
12777 or else (Is_Private_Type (Discrim_Scope)
12778 and then Chars (Discrim_Scope) = Chars (Typ))
12780 -- or else we are deriving from the full view and the
12781 -- discriminant is declared in the private entity.
12783 or else (Is_Private_Type (Typ)
12784 and then Chars (Discrim_Scope) = Chars (Typ))
12786 -- Or we are constrained the corresponding record of a
12787 -- synchronized type that completes a private declaration.
12789 or else (Is_Concurrent_Record_Type (Typ)
12791 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
12793 -- or we have a class-wide type, in which case make sure the
12794 -- discriminant found belongs to the root type.
12796 or else (Is_Class_Wide_Type (Typ)
12797 and then Etype (Typ) = Discrim_Scope));
12802 -- In all other cases we have something wrong
12805 end Is_Discriminant;
12807 -- Start of processing for Constrain_Component_Type
12810 if Nkind (Parent (Comp)) = N_Component_Declaration
12811 and then Comes_From_Source (Parent (Comp))
12812 and then Comes_From_Source
12813 (Subtype_Indication (Component_Definition (Parent (Comp))))
12816 (Subtype_Indication (Component_Definition (Parent (Comp))))
12818 return Compon_Type;
12820 elsif Is_Array_Type (Compon_Type) then
12821 return Build_Constrained_Array_Type (Compon_Type);
12823 elsif Has_Discriminants (Compon_Type) then
12824 return Build_Constrained_Discriminated_Type (Compon_Type);
12826 elsif Is_Access_Type (Compon_Type) then
12827 return Build_Constrained_Access_Type (Compon_Type);
12830 return Compon_Type;
12832 end Constrain_Component_Type;
12834 --------------------------
12835 -- Constrain_Concurrent --
12836 --------------------------
12838 -- For concurrent types, the associated record value type carries the same
12839 -- discriminants, so when we constrain a concurrent type, we must constrain
12840 -- the corresponding record type as well.
12842 procedure Constrain_Concurrent
12843 (Def_Id : in out Entity_Id;
12845 Related_Nod : Node_Id;
12846 Related_Id : Entity_Id;
12847 Suffix : Character)
12849 -- Retrieve Base_Type to ensure getting to the concurrent type in the
12850 -- case of a private subtype (needed when only doing semantic analysis).
12852 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
12856 if Is_Access_Type (T_Ent) then
12857 T_Ent := Designated_Type (T_Ent);
12860 T_Val := Corresponding_Record_Type (T_Ent);
12862 if Present (T_Val) then
12864 if No (Def_Id) then
12865 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12868 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12870 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12871 Set_Corresponding_Record_Type (Def_Id,
12872 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
12875 -- If there is no associated record, expansion is disabled and this
12876 -- is a generic context. Create a subtype in any case, so that
12877 -- semantic analysis can proceed.
12879 if No (Def_Id) then
12880 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12883 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12885 end Constrain_Concurrent;
12887 ------------------------------------
12888 -- Constrain_Corresponding_Record --
12889 ------------------------------------
12891 function Constrain_Corresponding_Record
12892 (Prot_Subt : Entity_Id;
12893 Corr_Rec : Entity_Id;
12894 Related_Nod : Node_Id) return Entity_Id
12896 T_Sub : constant Entity_Id :=
12897 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
12900 Set_Etype (T_Sub, Corr_Rec);
12901 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
12902 Set_Is_Constrained (T_Sub, True);
12903 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
12904 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
12906 if Has_Discriminants (Prot_Subt) then -- False only if errors.
12907 Set_Discriminant_Constraint
12908 (T_Sub, Discriminant_Constraint (Prot_Subt));
12909 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
12910 Create_Constrained_Components
12911 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
12914 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
12916 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
12917 Conditional_Delay (T_Sub, Corr_Rec);
12920 -- This is a component subtype: it will be frozen in the context of
12921 -- the enclosing record's init_proc, so that discriminant references
12922 -- are resolved to discriminals. (Note: we used to skip freezing
12923 -- altogether in that case, which caused errors downstream for
12924 -- components of a bit packed array type).
12926 Set_Has_Delayed_Freeze (T_Sub);
12930 end Constrain_Corresponding_Record;
12932 -----------------------
12933 -- Constrain_Decimal --
12934 -----------------------
12936 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
12937 T : constant Entity_Id := Entity (Subtype_Mark (S));
12938 C : constant Node_Id := Constraint (S);
12939 Loc : constant Source_Ptr := Sloc (C);
12940 Range_Expr : Node_Id;
12941 Digits_Expr : Node_Id;
12946 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
12948 if Nkind (C) = N_Range_Constraint then
12949 Range_Expr := Range_Expression (C);
12950 Digits_Val := Digits_Value (T);
12953 pragma Assert (Nkind (C) = N_Digits_Constraint);
12955 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
12957 Digits_Expr := Digits_Expression (C);
12958 Analyze_And_Resolve (Digits_Expr, Any_Integer);
12960 Check_Digits_Expression (Digits_Expr);
12961 Digits_Val := Expr_Value (Digits_Expr);
12963 if Digits_Val > Digits_Value (T) then
12965 ("digits expression is incompatible with subtype", C);
12966 Digits_Val := Digits_Value (T);
12969 if Present (Range_Constraint (C)) then
12970 Range_Expr := Range_Expression (Range_Constraint (C));
12972 Range_Expr := Empty;
12976 Set_Etype (Def_Id, Base_Type (T));
12977 Set_Size_Info (Def_Id, (T));
12978 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12979 Set_Delta_Value (Def_Id, Delta_Value (T));
12980 Set_Scale_Value (Def_Id, Scale_Value (T));
12981 Set_Small_Value (Def_Id, Small_Value (T));
12982 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
12983 Set_Digits_Value (Def_Id, Digits_Val);
12985 -- Manufacture range from given digits value if no range present
12987 if No (Range_Expr) then
12988 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
12992 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
12994 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
12997 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
12998 Set_Discrete_RM_Size (Def_Id);
13000 -- Unconditionally delay the freeze, since we cannot set size
13001 -- information in all cases correctly until the freeze point.
13003 Set_Has_Delayed_Freeze (Def_Id);
13004 end Constrain_Decimal;
13006 ----------------------------------
13007 -- Constrain_Discriminated_Type --
13008 ----------------------------------
13010 procedure Constrain_Discriminated_Type
13011 (Def_Id : Entity_Id;
13013 Related_Nod : Node_Id;
13014 For_Access : Boolean := False)
13016 E : constant Entity_Id := Entity (Subtype_Mark (S));
13019 Elist : Elist_Id := New_Elmt_List;
13021 procedure Fixup_Bad_Constraint;
13022 -- This is called after finding a bad constraint, and after having
13023 -- posted an appropriate error message. The mission is to leave the
13024 -- entity T in as reasonable state as possible.
13026 --------------------------
13027 -- Fixup_Bad_Constraint --
13028 --------------------------
13030 procedure Fixup_Bad_Constraint is
13032 -- Set a reasonable Ekind for the entity. For an incomplete type,
13033 -- we can't do much, but for other types, we can set the proper
13034 -- corresponding subtype kind.
13036 if Ekind (T) = E_Incomplete_Type then
13037 Set_Ekind (Def_Id, Ekind (T));
13039 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13042 -- Set Etype to the known type, to reduce chances of cascaded errors
13044 Set_Etype (Def_Id, E);
13045 Set_Error_Posted (Def_Id);
13046 end Fixup_Bad_Constraint;
13048 -- Start of processing for Constrain_Discriminated_Type
13051 C := Constraint (S);
13053 -- A discriminant constraint is only allowed in a subtype indication,
13054 -- after a subtype mark. This subtype mark must denote either a type
13055 -- with discriminants, or an access type whose designated type is a
13056 -- type with discriminants. A discriminant constraint specifies the
13057 -- values of these discriminants (RM 3.7.2(5)).
13059 T := Base_Type (Entity (Subtype_Mark (S)));
13061 if Is_Access_Type (T) then
13062 T := Designated_Type (T);
13065 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
13066 -- Avoid generating an error for access-to-incomplete subtypes.
13068 if Ada_Version >= Ada_2005
13069 and then Ekind (T) = E_Incomplete_Type
13070 and then Nkind (Parent (S)) = N_Subtype_Declaration
13071 and then not Is_Itype (Def_Id)
13073 -- A little sanity check, emit an error message if the type
13074 -- has discriminants to begin with. Type T may be a regular
13075 -- incomplete type or imported via a limited with clause.
13077 if Has_Discriminants (T)
13078 or else (From_Limited_With (T)
13079 and then Present (Non_Limited_View (T))
13080 and then Nkind (Parent (Non_Limited_View (T))) =
13081 N_Full_Type_Declaration
13082 and then Present (Discriminant_Specifications
13083 (Parent (Non_Limited_View (T)))))
13086 ("(Ada 2005) incomplete subtype may not be constrained", C);
13088 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13091 Fixup_Bad_Constraint;
13094 -- Check that the type has visible discriminants. The type may be
13095 -- a private type with unknown discriminants whose full view has
13096 -- discriminants which are invisible.
13098 elsif not Has_Discriminants (T)
13100 (Has_Unknown_Discriminants (T)
13101 and then Is_Private_Type (T))
13103 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13104 Fixup_Bad_Constraint;
13107 elsif Is_Constrained (E)
13108 or else (Ekind (E) = E_Class_Wide_Subtype
13109 and then Present (Discriminant_Constraint (E)))
13111 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13112 Fixup_Bad_Constraint;
13116 -- T may be an unconstrained subtype (e.g. a generic actual).
13117 -- Constraint applies to the base type.
13119 T := Base_Type (T);
13121 Elist := Build_Discriminant_Constraints (T, S);
13123 -- If the list returned was empty we had an error in building the
13124 -- discriminant constraint. We have also already signalled an error
13125 -- in the incomplete type case
13127 if Is_Empty_Elmt_List (Elist) then
13128 Fixup_Bad_Constraint;
13132 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
13133 end Constrain_Discriminated_Type;
13135 ---------------------------
13136 -- Constrain_Enumeration --
13137 ---------------------------
13139 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13140 T : constant Entity_Id := Entity (Subtype_Mark (S));
13141 C : constant Node_Id := Constraint (S);
13144 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13146 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13148 Set_Etype (Def_Id, Base_Type (T));
13149 Set_Size_Info (Def_Id, (T));
13150 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13151 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13153 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13155 Set_Discrete_RM_Size (Def_Id);
13156 end Constrain_Enumeration;
13158 ----------------------
13159 -- Constrain_Float --
13160 ----------------------
13162 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13163 T : constant Entity_Id := Entity (Subtype_Mark (S));
13169 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13171 Set_Etype (Def_Id, Base_Type (T));
13172 Set_Size_Info (Def_Id, (T));
13173 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13175 -- Process the constraint
13177 C := Constraint (S);
13179 -- Digits constraint present
13181 if Nkind (C) = N_Digits_Constraint then
13183 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13184 Check_Restriction (No_Obsolescent_Features, C);
13186 if Warn_On_Obsolescent_Feature then
13188 ("subtype digits constraint is an " &
13189 "obsolescent feature (RM J.3(8))?j?", C);
13192 D := Digits_Expression (C);
13193 Analyze_And_Resolve (D, Any_Integer);
13194 Check_Digits_Expression (D);
13195 Set_Digits_Value (Def_Id, Expr_Value (D));
13197 -- Check that digits value is in range. Obviously we can do this
13198 -- at compile time, but it is strictly a runtime check, and of
13199 -- course there is an ACVC test that checks this.
13201 if Digits_Value (Def_Id) > Digits_Value (T) then
13202 Error_Msg_Uint_1 := Digits_Value (T);
13203 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13205 Make_Raise_Constraint_Error (Sloc (D),
13206 Reason => CE_Range_Check_Failed);
13207 Insert_Action (Declaration_Node (Def_Id), Rais);
13210 C := Range_Constraint (C);
13212 -- No digits constraint present
13215 Set_Digits_Value (Def_Id, Digits_Value (T));
13218 -- Range constraint present
13220 if Nkind (C) = N_Range_Constraint then
13221 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13223 -- No range constraint present
13226 pragma Assert (No (C));
13227 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13230 Set_Is_Constrained (Def_Id);
13231 end Constrain_Float;
13233 ---------------------
13234 -- Constrain_Index --
13235 ---------------------
13237 procedure Constrain_Index
13240 Related_Nod : Node_Id;
13241 Related_Id : Entity_Id;
13242 Suffix : Character;
13243 Suffix_Index : Nat)
13245 Def_Id : Entity_Id;
13246 R : Node_Id := Empty;
13247 T : constant Entity_Id := Etype (Index);
13251 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13252 Set_Etype (Def_Id, Base_Type (T));
13254 if Nkind (S) = N_Range
13256 (Nkind (S) = N_Attribute_Reference
13257 and then Attribute_Name (S) = Name_Range)
13259 -- A Range attribute will be transformed into N_Range by Resolve
13265 Process_Range_Expr_In_Decl (R, T);
13267 if not Error_Posted (S)
13269 (Nkind (S) /= N_Range
13270 or else not Covers (T, (Etype (Low_Bound (S))))
13271 or else not Covers (T, (Etype (High_Bound (S)))))
13273 if Base_Type (T) /= Any_Type
13274 and then Etype (Low_Bound (S)) /= Any_Type
13275 and then Etype (High_Bound (S)) /= Any_Type
13277 Error_Msg_N ("range expected", S);
13281 elsif Nkind (S) = N_Subtype_Indication then
13283 -- The parser has verified that this is a discrete indication
13285 Resolve_Discrete_Subtype_Indication (S, T);
13286 Bad_Predicated_Subtype_Use
13287 ("subtype& has predicate, not allowed in index constraint",
13288 S, Entity (Subtype_Mark (S)));
13290 R := Range_Expression (Constraint (S));
13292 -- Capture values of bounds and generate temporaries for them if
13293 -- needed, since checks may cause duplication of the expressions
13294 -- which must not be reevaluated.
13296 -- The forced evaluation removes side effects from expressions, which
13297 -- should occur also in GNATprove mode. Otherwise, we end up with
13298 -- unexpected insertions of actions at places where this is not
13299 -- supposed to occur, e.g. on default parameters of a call.
13301 if Expander_Active or GNATprove_Mode then
13303 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13305 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13308 elsif Nkind (S) = N_Discriminant_Association then
13310 -- Syntactically valid in subtype indication
13312 Error_Msg_N ("invalid index constraint", S);
13313 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13316 -- Subtype_Mark case, no anonymous subtypes to construct
13321 if Is_Entity_Name (S) then
13322 if not Is_Type (Entity (S)) then
13323 Error_Msg_N ("expect subtype mark for index constraint", S);
13325 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13326 Wrong_Type (S, Base_Type (T));
13328 -- Check error of subtype with predicate in index constraint
13331 Bad_Predicated_Subtype_Use
13332 ("subtype& has predicate, not allowed in index constraint",
13339 Error_Msg_N ("invalid index constraint", S);
13340 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13345 -- Complete construction of the Itype
13347 if Is_Modular_Integer_Type (T) then
13348 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13350 elsif Is_Integer_Type (T) then
13351 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13354 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13355 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13356 Set_First_Literal (Def_Id, First_Literal (T));
13359 Set_Size_Info (Def_Id, (T));
13360 Set_RM_Size (Def_Id, RM_Size (T));
13361 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13363 Set_Scalar_Range (Def_Id, R);
13365 Set_Etype (S, Def_Id);
13366 Set_Discrete_RM_Size (Def_Id);
13367 end Constrain_Index;
13369 -----------------------
13370 -- Constrain_Integer --
13371 -----------------------
13373 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13374 T : constant Entity_Id := Entity (Subtype_Mark (S));
13375 C : constant Node_Id := Constraint (S);
13378 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13380 if Is_Modular_Integer_Type (T) then
13381 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13383 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13386 Set_Etype (Def_Id, Base_Type (T));
13387 Set_Size_Info (Def_Id, (T));
13388 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13389 Set_Discrete_RM_Size (Def_Id);
13390 end Constrain_Integer;
13392 ------------------------------
13393 -- Constrain_Ordinary_Fixed --
13394 ------------------------------
13396 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13397 T : constant Entity_Id := Entity (Subtype_Mark (S));
13403 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13404 Set_Etype (Def_Id, Base_Type (T));
13405 Set_Size_Info (Def_Id, (T));
13406 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13407 Set_Small_Value (Def_Id, Small_Value (T));
13409 -- Process the constraint
13411 C := Constraint (S);
13413 -- Delta constraint present
13415 if Nkind (C) = N_Delta_Constraint then
13417 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13418 Check_Restriction (No_Obsolescent_Features, C);
13420 if Warn_On_Obsolescent_Feature then
13422 ("subtype delta constraint is an " &
13423 "obsolescent feature (RM J.3(7))?j?");
13426 D := Delta_Expression (C);
13427 Analyze_And_Resolve (D, Any_Real);
13428 Check_Delta_Expression (D);
13429 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13431 -- Check that delta value is in range. Obviously we can do this
13432 -- at compile time, but it is strictly a runtime check, and of
13433 -- course there is an ACVC test that checks this.
13435 if Delta_Value (Def_Id) < Delta_Value (T) then
13436 Error_Msg_N ("??delta value is too small", D);
13438 Make_Raise_Constraint_Error (Sloc (D),
13439 Reason => CE_Range_Check_Failed);
13440 Insert_Action (Declaration_Node (Def_Id), Rais);
13443 C := Range_Constraint (C);
13445 -- No delta constraint present
13448 Set_Delta_Value (Def_Id, Delta_Value (T));
13451 -- Range constraint present
13453 if Nkind (C) = N_Range_Constraint then
13454 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13456 -- No range constraint present
13459 pragma Assert (No (C));
13460 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13463 Set_Discrete_RM_Size (Def_Id);
13465 -- Unconditionally delay the freeze, since we cannot set size
13466 -- information in all cases correctly until the freeze point.
13468 Set_Has_Delayed_Freeze (Def_Id);
13469 end Constrain_Ordinary_Fixed;
13471 -----------------------
13472 -- Contain_Interface --
13473 -----------------------
13475 function Contain_Interface
13476 (Iface : Entity_Id;
13477 Ifaces : Elist_Id) return Boolean
13479 Iface_Elmt : Elmt_Id;
13482 if Present (Ifaces) then
13483 Iface_Elmt := First_Elmt (Ifaces);
13484 while Present (Iface_Elmt) loop
13485 if Node (Iface_Elmt) = Iface then
13489 Next_Elmt (Iface_Elmt);
13494 end Contain_Interface;
13496 ---------------------------
13497 -- Convert_Scalar_Bounds --
13498 ---------------------------
13500 procedure Convert_Scalar_Bounds
13502 Parent_Type : Entity_Id;
13503 Derived_Type : Entity_Id;
13506 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13513 -- Defend against previous errors
13515 if No (Scalar_Range (Derived_Type)) then
13516 Check_Error_Detected;
13520 Lo := Build_Scalar_Bound
13521 (Type_Low_Bound (Derived_Type),
13522 Parent_Type, Implicit_Base);
13524 Hi := Build_Scalar_Bound
13525 (Type_High_Bound (Derived_Type),
13526 Parent_Type, Implicit_Base);
13533 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13535 Set_Parent (Rng, N);
13536 Set_Scalar_Range (Derived_Type, Rng);
13538 -- Analyze the bounds
13540 Analyze_And_Resolve (Lo, Implicit_Base);
13541 Analyze_And_Resolve (Hi, Implicit_Base);
13543 -- Analyze the range itself, except that we do not analyze it if
13544 -- the bounds are real literals, and we have a fixed-point type.
13545 -- The reason for this is that we delay setting the bounds in this
13546 -- case till we know the final Small and Size values (see circuit
13547 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13549 if Is_Fixed_Point_Type (Parent_Type)
13550 and then Nkind (Lo) = N_Real_Literal
13551 and then Nkind (Hi) = N_Real_Literal
13555 -- Here we do the analysis of the range
13557 -- Note: we do this manually, since if we do a normal Analyze and
13558 -- Resolve call, there are problems with the conversions used for
13559 -- the derived type range.
13562 Set_Etype (Rng, Implicit_Base);
13563 Set_Analyzed (Rng, True);
13565 end Convert_Scalar_Bounds;
13567 -------------------
13568 -- Copy_And_Swap --
13569 -------------------
13571 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13573 -- Initialize new full declaration entity by copying the pertinent
13574 -- fields of the corresponding private declaration entity.
13576 -- We temporarily set Ekind to a value appropriate for a type to
13577 -- avoid assert failures in Einfo from checking for setting type
13578 -- attributes on something that is not a type. Ekind (Priv) is an
13579 -- appropriate choice, since it allowed the attributes to be set
13580 -- in the first place. This Ekind value will be modified later.
13582 Set_Ekind (Full, Ekind (Priv));
13584 -- Also set Etype temporarily to Any_Type, again, in the absence
13585 -- of errors, it will be properly reset, and if there are errors,
13586 -- then we want a value of Any_Type to remain.
13588 Set_Etype (Full, Any_Type);
13590 -- Now start copying attributes
13592 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13594 if Has_Discriminants (Full) then
13595 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13596 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13599 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13600 Set_Homonym (Full, Homonym (Priv));
13601 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13602 Set_Is_Public (Full, Is_Public (Priv));
13603 Set_Is_Pure (Full, Is_Pure (Priv));
13604 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13605 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13606 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13607 Set_Has_Pragma_Unreferenced_Objects
13608 (Full, Has_Pragma_Unreferenced_Objects
13611 Conditional_Delay (Full, Priv);
13613 if Is_Tagged_Type (Full) then
13614 Set_Direct_Primitive_Operations
13615 (Full, Direct_Primitive_Operations (Priv));
13616 Set_No_Tagged_Streams_Pragma
13617 (Full, No_Tagged_Streams_Pragma (Priv));
13619 if Is_Base_Type (Priv) then
13620 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13624 Set_Is_Volatile (Full, Is_Volatile (Priv));
13625 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13626 Set_Scope (Full, Scope (Priv));
13627 Set_Next_Entity (Full, Next_Entity (Priv));
13628 Set_First_Entity (Full, First_Entity (Priv));
13629 Set_Last_Entity (Full, Last_Entity (Priv));
13631 -- If access types have been recorded for later handling, keep them in
13632 -- the full view so that they get handled when the full view freeze
13633 -- node is expanded.
13635 if Present (Freeze_Node (Priv))
13636 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
13638 Ensure_Freeze_Node (Full);
13639 Set_Access_Types_To_Process
13640 (Freeze_Node (Full),
13641 Access_Types_To_Process (Freeze_Node (Priv)));
13644 -- Swap the two entities. Now Private is the full type entity and Full
13645 -- is the private one. They will be swapped back at the end of the
13646 -- private part. This swapping ensures that the entity that is visible
13647 -- in the private part is the full declaration.
13649 Exchange_Entities (Priv, Full);
13650 Append_Entity (Full, Scope (Full));
13653 -------------------------------------
13654 -- Copy_Array_Base_Type_Attributes --
13655 -------------------------------------
13657 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
13659 Set_Component_Alignment (T1, Component_Alignment (T2));
13660 Set_Component_Type (T1, Component_Type (T2));
13661 Set_Component_Size (T1, Component_Size (T2));
13662 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
13663 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
13664 Set_Has_Protected (T1, Has_Protected (T2));
13665 Set_Has_Task (T1, Has_Task (T2));
13666 Set_Is_Packed (T1, Is_Packed (T2));
13667 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
13668 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
13669 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
13670 end Copy_Array_Base_Type_Attributes;
13672 -----------------------------------
13673 -- Copy_Array_Subtype_Attributes --
13674 -----------------------------------
13676 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
13678 Set_Size_Info (T1, T2);
13680 Set_First_Index (T1, First_Index (T2));
13681 Set_Is_Aliased (T1, Is_Aliased (T2));
13682 Set_Is_Volatile (T1, Is_Volatile (T2));
13683 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
13684 Set_Is_Constrained (T1, Is_Constrained (T2));
13685 Set_Depends_On_Private (T1, Has_Private_Component (T2));
13686 Inherit_Rep_Item_Chain (T1, T2);
13687 Set_Convention (T1, Convention (T2));
13688 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
13689 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
13690 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
13691 end Copy_Array_Subtype_Attributes;
13693 -----------------------------------
13694 -- Create_Constrained_Components --
13695 -----------------------------------
13697 procedure Create_Constrained_Components
13699 Decl_Node : Node_Id;
13701 Constraints : Elist_Id)
13703 Loc : constant Source_Ptr := Sloc (Subt);
13704 Comp_List : constant Elist_Id := New_Elmt_List;
13705 Parent_Type : constant Entity_Id := Etype (Typ);
13706 Assoc_List : constant List_Id := New_List;
13707 Discr_Val : Elmt_Id;
13711 Is_Static : Boolean := True;
13713 procedure Collect_Fixed_Components (Typ : Entity_Id);
13714 -- Collect parent type components that do not appear in a variant part
13716 procedure Create_All_Components;
13717 -- Iterate over Comp_List to create the components of the subtype
13719 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
13720 -- Creates a new component from Old_Compon, copying all the fields from
13721 -- it, including its Etype, inserts the new component in the Subt entity
13722 -- chain and returns the new component.
13724 function Is_Variant_Record (T : Entity_Id) return Boolean;
13725 -- If true, and discriminants are static, collect only components from
13726 -- variants selected by discriminant values.
13728 ------------------------------
13729 -- Collect_Fixed_Components --
13730 ------------------------------
13732 procedure Collect_Fixed_Components (Typ : Entity_Id) is
13734 -- Build association list for discriminants, and find components of the
13735 -- variant part selected by the values of the discriminants.
13737 Old_C := First_Discriminant (Typ);
13738 Discr_Val := First_Elmt (Constraints);
13739 while Present (Old_C) loop
13740 Append_To (Assoc_List,
13741 Make_Component_Association (Loc,
13742 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
13743 Expression => New_Copy (Node (Discr_Val))));
13745 Next_Elmt (Discr_Val);
13746 Next_Discriminant (Old_C);
13749 -- The tag and the possible parent component are unconditionally in
13752 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
13753 Old_C := First_Component (Typ);
13754 while Present (Old_C) loop
13755 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
13756 Append_Elmt (Old_C, Comp_List);
13759 Next_Component (Old_C);
13762 end Collect_Fixed_Components;
13764 ---------------------------
13765 -- Create_All_Components --
13766 ---------------------------
13768 procedure Create_All_Components is
13772 Comp := First_Elmt (Comp_List);
13773 while Present (Comp) loop
13774 Old_C := Node (Comp);
13775 New_C := Create_Component (Old_C);
13779 Constrain_Component_Type
13780 (Old_C, Subt, Decl_Node, Typ, Constraints));
13781 Set_Is_Public (New_C, Is_Public (Subt));
13785 end Create_All_Components;
13787 ----------------------
13788 -- Create_Component --
13789 ----------------------
13791 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
13792 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
13795 if Ekind (Old_Compon) = E_Discriminant
13796 and then Is_Completely_Hidden (Old_Compon)
13798 -- This is a shadow discriminant created for a discriminant of
13799 -- the parent type, which needs to be present in the subtype.
13800 -- Give the shadow discriminant an internal name that cannot
13801 -- conflict with that of visible components.
13803 Set_Chars (New_Compon, New_Internal_Name ('C'));
13806 -- Set the parent so we have a proper link for freezing etc. This is
13807 -- not a real parent pointer, since of course our parent does not own
13808 -- up to us and reference us, we are an illegitimate child of the
13809 -- original parent.
13811 Set_Parent (New_Compon, Parent (Old_Compon));
13813 -- If the old component's Esize was already determined and is a
13814 -- static value, then the new component simply inherits it. Otherwise
13815 -- the old component's size may require run-time determination, but
13816 -- the new component's size still might be statically determinable
13817 -- (if, for example it has a static constraint). In that case we want
13818 -- Layout_Type to recompute the component's size, so we reset its
13819 -- size and positional fields.
13821 if Frontend_Layout_On_Target
13822 and then not Known_Static_Esize (Old_Compon)
13824 Set_Esize (New_Compon, Uint_0);
13825 Init_Normalized_First_Bit (New_Compon);
13826 Init_Normalized_Position (New_Compon);
13827 Init_Normalized_Position_Max (New_Compon);
13830 -- We do not want this node marked as Comes_From_Source, since
13831 -- otherwise it would get first class status and a separate cross-
13832 -- reference line would be generated. Illegitimate children do not
13833 -- rate such recognition.
13835 Set_Comes_From_Source (New_Compon, False);
13837 -- But it is a real entity, and a birth certificate must be properly
13838 -- registered by entering it into the entity list.
13840 Enter_Name (New_Compon);
13843 end Create_Component;
13845 -----------------------
13846 -- Is_Variant_Record --
13847 -----------------------
13849 function Is_Variant_Record (T : Entity_Id) return Boolean is
13851 return Nkind (Parent (T)) = N_Full_Type_Declaration
13852 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
13853 and then Present (Component_List (Type_Definition (Parent (T))))
13856 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
13857 end Is_Variant_Record;
13859 -- Start of processing for Create_Constrained_Components
13862 pragma Assert (Subt /= Base_Type (Subt));
13863 pragma Assert (Typ = Base_Type (Typ));
13865 Set_First_Entity (Subt, Empty);
13866 Set_Last_Entity (Subt, Empty);
13868 -- Check whether constraint is fully static, in which case we can
13869 -- optimize the list of components.
13871 Discr_Val := First_Elmt (Constraints);
13872 while Present (Discr_Val) loop
13873 if not Is_OK_Static_Expression (Node (Discr_Val)) then
13874 Is_Static := False;
13878 Next_Elmt (Discr_Val);
13881 Set_Has_Static_Discriminants (Subt, Is_Static);
13885 -- Inherit the discriminants of the parent type
13887 Add_Discriminants : declare
13893 Old_C := First_Discriminant (Typ);
13895 while Present (Old_C) loop
13896 Num_Disc := Num_Disc + 1;
13897 New_C := Create_Component (Old_C);
13898 Set_Is_Public (New_C, Is_Public (Subt));
13899 Next_Discriminant (Old_C);
13902 -- For an untagged derived subtype, the number of discriminants may
13903 -- be smaller than the number of inherited discriminants, because
13904 -- several of them may be renamed by a single new discriminant or
13905 -- constrained. In this case, add the hidden discriminants back into
13906 -- the subtype, because they need to be present if the optimizer of
13907 -- the GCC 4.x back-end decides to break apart assignments between
13908 -- objects using the parent view into member-wise assignments.
13912 if Is_Derived_Type (Typ)
13913 and then not Is_Tagged_Type (Typ)
13915 Old_C := First_Stored_Discriminant (Typ);
13917 while Present (Old_C) loop
13918 Num_Gird := Num_Gird + 1;
13919 Next_Stored_Discriminant (Old_C);
13923 if Num_Gird > Num_Disc then
13925 -- Find out multiple uses of new discriminants, and add hidden
13926 -- components for the extra renamed discriminants. We recognize
13927 -- multiple uses through the Corresponding_Discriminant of a
13928 -- new discriminant: if it constrains several old discriminants,
13929 -- this field points to the last one in the parent type. The
13930 -- stored discriminants of the derived type have the same name
13931 -- as those of the parent.
13935 New_Discr : Entity_Id;
13936 Old_Discr : Entity_Id;
13939 Constr := First_Elmt (Stored_Constraint (Typ));
13940 Old_Discr := First_Stored_Discriminant (Typ);
13941 while Present (Constr) loop
13942 if Is_Entity_Name (Node (Constr))
13943 and then Ekind (Entity (Node (Constr))) = E_Discriminant
13945 New_Discr := Entity (Node (Constr));
13947 if Chars (Corresponding_Discriminant (New_Discr)) /=
13950 -- The new discriminant has been used to rename a
13951 -- subsequent old discriminant. Introduce a shadow
13952 -- component for the current old discriminant.
13954 New_C := Create_Component (Old_Discr);
13955 Set_Original_Record_Component (New_C, Old_Discr);
13959 -- The constraint has eliminated the old discriminant.
13960 -- Introduce a shadow component.
13962 New_C := Create_Component (Old_Discr);
13963 Set_Original_Record_Component (New_C, Old_Discr);
13966 Next_Elmt (Constr);
13967 Next_Stored_Discriminant (Old_Discr);
13971 end Add_Discriminants;
13974 and then Is_Variant_Record (Typ)
13976 Collect_Fixed_Components (Typ);
13978 Gather_Components (
13980 Component_List (Type_Definition (Parent (Typ))),
13981 Governed_By => Assoc_List,
13983 Report_Errors => Errors);
13984 pragma Assert (not Errors);
13986 Create_All_Components;
13988 -- If the subtype declaration is created for a tagged type derivation
13989 -- with constraints, we retrieve the record definition of the parent
13990 -- type to select the components of the proper variant.
13993 and then Is_Tagged_Type (Typ)
13994 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
13996 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
13997 and then Is_Variant_Record (Parent_Type)
13999 Collect_Fixed_Components (Typ);
14003 Component_List (Type_Definition (Parent (Parent_Type))),
14004 Governed_By => Assoc_List,
14006 Report_Errors => Errors);
14008 -- Note: previously there was a check at this point that no errors
14009 -- were detected. As a consequence of AI05-220 there may be an error
14010 -- if an inherited discriminant that controls a variant has a non-
14011 -- static constraint.
14013 -- If the tagged derivation has a type extension, collect all the
14014 -- new components therein.
14016 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14018 Old_C := First_Component (Typ);
14019 while Present (Old_C) loop
14020 if Original_Record_Component (Old_C) = Old_C
14021 and then Chars (Old_C) /= Name_uTag
14022 and then Chars (Old_C) /= Name_uParent
14024 Append_Elmt (Old_C, Comp_List);
14027 Next_Component (Old_C);
14031 Create_All_Components;
14034 -- If discriminants are not static, or if this is a multi-level type
14035 -- extension, we have to include all components of the parent type.
14037 Old_C := First_Component (Typ);
14038 while Present (Old_C) loop
14039 New_C := Create_Component (Old_C);
14043 Constrain_Component_Type
14044 (Old_C, Subt, Decl_Node, Typ, Constraints));
14045 Set_Is_Public (New_C, Is_Public (Subt));
14047 Next_Component (Old_C);
14052 end Create_Constrained_Components;
14054 ------------------------------------------
14055 -- Decimal_Fixed_Point_Type_Declaration --
14056 ------------------------------------------
14058 procedure Decimal_Fixed_Point_Type_Declaration
14062 Loc : constant Source_Ptr := Sloc (Def);
14063 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14064 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14065 Implicit_Base : Entity_Id;
14072 Check_SPARK_05_Restriction
14073 ("decimal fixed point type is not allowed", Def);
14074 Check_Restriction (No_Fixed_Point, Def);
14076 -- Create implicit base type
14079 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14080 Set_Etype (Implicit_Base, Implicit_Base);
14082 -- Analyze and process delta expression
14084 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14086 Check_Delta_Expression (Delta_Expr);
14087 Delta_Val := Expr_Value_R (Delta_Expr);
14089 -- Check delta is power of 10, and determine scale value from it
14095 Scale_Val := Uint_0;
14098 if Val < Ureal_1 then
14099 while Val < Ureal_1 loop
14100 Val := Val * Ureal_10;
14101 Scale_Val := Scale_Val + 1;
14104 if Scale_Val > 18 then
14105 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14106 Scale_Val := UI_From_Int (+18);
14110 while Val > Ureal_1 loop
14111 Val := Val / Ureal_10;
14112 Scale_Val := Scale_Val - 1;
14115 if Scale_Val < -18 then
14116 Error_Msg_N ("scale is less than minimum value of -18", Def);
14117 Scale_Val := UI_From_Int (-18);
14121 if Val /= Ureal_1 then
14122 Error_Msg_N ("delta expression must be a power of 10", Def);
14123 Delta_Val := Ureal_10 ** (-Scale_Val);
14127 -- Set delta, scale and small (small = delta for decimal type)
14129 Set_Delta_Value (Implicit_Base, Delta_Val);
14130 Set_Scale_Value (Implicit_Base, Scale_Val);
14131 Set_Small_Value (Implicit_Base, Delta_Val);
14133 -- Analyze and process digits expression
14135 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14136 Check_Digits_Expression (Digs_Expr);
14137 Digs_Val := Expr_Value (Digs_Expr);
14139 if Digs_Val > 18 then
14140 Digs_Val := UI_From_Int (+18);
14141 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14144 Set_Digits_Value (Implicit_Base, Digs_Val);
14145 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14147 -- Set range of base type from digits value for now. This will be
14148 -- expanded to represent the true underlying base range by Freeze.
14150 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14152 -- Note: We leave size as zero for now, size will be set at freeze
14153 -- time. We have to do this for ordinary fixed-point, because the size
14154 -- depends on the specified small, and we might as well do the same for
14155 -- decimal fixed-point.
14157 pragma Assert (Esize (Implicit_Base) = Uint_0);
14159 -- If there are bounds given in the declaration use them as the
14160 -- bounds of the first named subtype.
14162 if Present (Real_Range_Specification (Def)) then
14164 RRS : constant Node_Id := Real_Range_Specification (Def);
14165 Low : constant Node_Id := Low_Bound (RRS);
14166 High : constant Node_Id := High_Bound (RRS);
14171 Analyze_And_Resolve (Low, Any_Real);
14172 Analyze_And_Resolve (High, Any_Real);
14173 Check_Real_Bound (Low);
14174 Check_Real_Bound (High);
14175 Low_Val := Expr_Value_R (Low);
14176 High_Val := Expr_Value_R (High);
14178 if Low_Val < (-Bound_Val) then
14180 ("range low bound too small for digits value", Low);
14181 Low_Val := -Bound_Val;
14184 if High_Val > Bound_Val then
14186 ("range high bound too large for digits value", High);
14187 High_Val := Bound_Val;
14190 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14193 -- If no explicit range, use range that corresponds to given
14194 -- digits value. This will end up as the final range for the
14198 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14201 -- Complete entity for first subtype. The inheritance of the rep item
14202 -- chain ensures that SPARK-related pragmas are not clobbered when the
14203 -- decimal fixed point type acts as a full view of a private type.
14205 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14206 Set_Etype (T, Implicit_Base);
14207 Set_Size_Info (T, Implicit_Base);
14208 Inherit_Rep_Item_Chain (T, Implicit_Base);
14209 Set_Digits_Value (T, Digs_Val);
14210 Set_Delta_Value (T, Delta_Val);
14211 Set_Small_Value (T, Delta_Val);
14212 Set_Scale_Value (T, Scale_Val);
14213 Set_Is_Constrained (T);
14214 end Decimal_Fixed_Point_Type_Declaration;
14216 -----------------------------------
14217 -- Derive_Progenitor_Subprograms --
14218 -----------------------------------
14220 procedure Derive_Progenitor_Subprograms
14221 (Parent_Type : Entity_Id;
14222 Tagged_Type : Entity_Id)
14227 Iface_Elmt : Elmt_Id;
14228 Iface_Subp : Entity_Id;
14229 New_Subp : Entity_Id := Empty;
14230 Prim_Elmt : Elmt_Id;
14235 pragma Assert (Ada_Version >= Ada_2005
14236 and then Is_Record_Type (Tagged_Type)
14237 and then Is_Tagged_Type (Tagged_Type)
14238 and then Has_Interfaces (Tagged_Type));
14240 -- Step 1: Transfer to the full-view primitives associated with the
14241 -- partial-view that cover interface primitives. Conceptually this
14242 -- work should be done later by Process_Full_View; done here to
14243 -- simplify its implementation at later stages. It can be safely
14244 -- done here because interfaces must be visible in the partial and
14245 -- private view (RM 7.3(7.3/2)).
14247 -- Small optimization: This work is only required if the parent may
14248 -- have entities whose Alias attribute reference an interface primitive.
14249 -- Such a situation may occur if the parent is an abstract type and the
14250 -- primitive has not been yet overridden or if the parent is a generic
14251 -- formal type covering interfaces.
14253 -- If the tagged type is not abstract, it cannot have abstract
14254 -- primitives (the only entities in the list of primitives of
14255 -- non-abstract tagged types that can reference abstract primitives
14256 -- through its Alias attribute are the internal entities that have
14257 -- attribute Interface_Alias, and these entities are generated later
14258 -- by Add_Internal_Interface_Entities).
14260 if In_Private_Part (Current_Scope)
14261 and then (Is_Abstract_Type (Parent_Type)
14263 Is_Generic_Type (Parent_Type))
14265 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14266 while Present (Elmt) loop
14267 Subp := Node (Elmt);
14269 -- At this stage it is not possible to have entities in the list
14270 -- of primitives that have attribute Interface_Alias.
14272 pragma Assert (No (Interface_Alias (Subp)));
14274 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14276 if Is_Interface (Typ) then
14277 E := Find_Primitive_Covering_Interface
14278 (Tagged_Type => Tagged_Type,
14279 Iface_Prim => Subp);
14282 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14284 Replace_Elmt (Elmt, E);
14285 Remove_Homonym (Subp);
14293 -- Step 2: Add primitives of progenitors that are not implemented by
14294 -- parents of Tagged_Type.
14296 if Present (Interfaces (Base_Type (Tagged_Type))) then
14297 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14298 while Present (Iface_Elmt) loop
14299 Iface := Node (Iface_Elmt);
14301 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14302 while Present (Prim_Elmt) loop
14303 Iface_Subp := Node (Prim_Elmt);
14305 -- Exclude derivation of predefined primitives except those
14306 -- that come from source, or are inherited from one that comes
14307 -- from source. Required to catch declarations of equality
14308 -- operators of interfaces. For example:
14310 -- type Iface is interface;
14311 -- function "=" (Left, Right : Iface) return Boolean;
14313 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14314 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14316 E := Find_Primitive_Covering_Interface
14317 (Tagged_Type => Tagged_Type,
14318 Iface_Prim => Iface_Subp);
14320 -- If not found we derive a new primitive leaving its alias
14321 -- attribute referencing the interface primitive.
14325 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14327 -- Ada 2012 (AI05-0197): If the covering primitive's name
14328 -- differs from the name of the interface primitive then it
14329 -- is a private primitive inherited from a parent type. In
14330 -- such case, given that Tagged_Type covers the interface,
14331 -- the inherited private primitive becomes visible. For such
14332 -- purpose we add a new entity that renames the inherited
14333 -- private primitive.
14335 elsif Chars (E) /= Chars (Iface_Subp) then
14336 pragma Assert (Has_Suffix (E, 'P'));
14338 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14339 Set_Alias (New_Subp, E);
14340 Set_Is_Abstract_Subprogram (New_Subp,
14341 Is_Abstract_Subprogram (E));
14343 -- Propagate to the full view interface entities associated
14344 -- with the partial view.
14346 elsif In_Private_Part (Current_Scope)
14347 and then Present (Alias (E))
14348 and then Alias (E) = Iface_Subp
14350 List_Containing (Parent (E)) /=
14351 Private_Declarations
14353 (Unit_Declaration_Node (Current_Scope)))
14355 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14359 Next_Elmt (Prim_Elmt);
14362 Next_Elmt (Iface_Elmt);
14365 end Derive_Progenitor_Subprograms;
14367 -----------------------
14368 -- Derive_Subprogram --
14369 -----------------------
14371 procedure Derive_Subprogram
14372 (New_Subp : in out Entity_Id;
14373 Parent_Subp : Entity_Id;
14374 Derived_Type : Entity_Id;
14375 Parent_Type : Entity_Id;
14376 Actual_Subp : Entity_Id := Empty)
14378 Formal : Entity_Id;
14379 -- Formal parameter of parent primitive operation
14381 Formal_Of_Actual : Entity_Id;
14382 -- Formal parameter of actual operation, when the derivation is to
14383 -- create a renaming for a primitive operation of an actual in an
14386 New_Formal : Entity_Id;
14387 -- Formal of inherited operation
14389 Visible_Subp : Entity_Id := Parent_Subp;
14391 function Is_Private_Overriding return Boolean;
14392 -- If Subp is a private overriding of a visible operation, the inherited
14393 -- operation derives from the overridden op (even though its body is the
14394 -- overriding one) and the inherited operation is visible now. See
14395 -- sem_disp to see the full details of the handling of the overridden
14396 -- subprogram, which is removed from the list of primitive operations of
14397 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14398 -- and used to diagnose abstract operations that need overriding in the
14401 procedure Replace_Type (Id, New_Id : Entity_Id);
14402 -- When the type is an anonymous access type, create a new access type
14403 -- designating the derived type.
14405 procedure Set_Derived_Name;
14406 -- This procedure sets the appropriate Chars name for New_Subp. This
14407 -- is normally just a copy of the parent name. An exception arises for
14408 -- type support subprograms, where the name is changed to reflect the
14409 -- name of the derived type, e.g. if type foo is derived from type bar,
14410 -- then a procedure barDA is derived with a name fooDA.
14412 ---------------------------
14413 -- Is_Private_Overriding --
14414 ---------------------------
14416 function Is_Private_Overriding return Boolean is
14420 -- If the parent is not a dispatching operation there is no
14421 -- need to investigate overridings
14423 if not Is_Dispatching_Operation (Parent_Subp) then
14427 -- The visible operation that is overridden is a homonym of the
14428 -- parent subprogram. We scan the homonym chain to find the one
14429 -- whose alias is the subprogram we are deriving.
14431 Prev := Current_Entity (Parent_Subp);
14432 while Present (Prev) loop
14433 if Ekind (Prev) = Ekind (Parent_Subp)
14434 and then Alias (Prev) = Parent_Subp
14435 and then Scope (Parent_Subp) = Scope (Prev)
14436 and then not Is_Hidden (Prev)
14438 Visible_Subp := Prev;
14442 Prev := Homonym (Prev);
14446 end Is_Private_Overriding;
14452 procedure Replace_Type (Id, New_Id : Entity_Id) is
14453 Id_Type : constant Entity_Id := Etype (Id);
14454 Acc_Type : Entity_Id;
14455 Par : constant Node_Id := Parent (Derived_Type);
14458 -- When the type is an anonymous access type, create a new access
14459 -- type designating the derived type. This itype must be elaborated
14460 -- at the point of the derivation, not on subsequent calls that may
14461 -- be out of the proper scope for Gigi, so we insert a reference to
14462 -- it after the derivation.
14464 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14466 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14469 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14470 and then Present (Full_View (Desig_Typ))
14471 and then not Is_Private_Type (Parent_Type)
14473 Desig_Typ := Full_View (Desig_Typ);
14476 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14478 -- Ada 2005 (AI-251): Handle also derivations of abstract
14479 -- interface primitives.
14481 or else (Is_Interface (Desig_Typ)
14482 and then not Is_Class_Wide_Type (Desig_Typ))
14484 Acc_Type := New_Copy (Id_Type);
14485 Set_Etype (Acc_Type, Acc_Type);
14486 Set_Scope (Acc_Type, New_Subp);
14488 -- Set size of anonymous access type. If we have an access
14489 -- to an unconstrained array, this is a fat pointer, so it
14490 -- is sizes at twice addtress size.
14492 if Is_Array_Type (Desig_Typ)
14493 and then not Is_Constrained (Desig_Typ)
14495 Init_Size (Acc_Type, 2 * System_Address_Size);
14497 -- Other cases use a thin pointer
14500 Init_Size (Acc_Type, System_Address_Size);
14503 -- Set remaining characterstics of anonymous access type
14505 Init_Alignment (Acc_Type);
14506 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14508 Set_Etype (New_Id, Acc_Type);
14509 Set_Scope (New_Id, New_Subp);
14511 -- Create a reference to it
14513 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14516 Set_Etype (New_Id, Id_Type);
14520 -- In Ada2012, a formal may have an incomplete type but the type
14521 -- derivation that inherits the primitive follows the full view.
14523 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14525 (Ekind (Id_Type) = E_Record_Type_With_Private
14526 and then Present (Full_View (Id_Type))
14528 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14530 (Ada_Version >= Ada_2012
14531 and then Ekind (Id_Type) = E_Incomplete_Type
14532 and then Full_View (Id_Type) = Parent_Type)
14534 -- Constraint checks on formals are generated during expansion,
14535 -- based on the signature of the original subprogram. The bounds
14536 -- of the derived type are not relevant, and thus we can use
14537 -- the base type for the formals. However, the return type may be
14538 -- used in a context that requires that the proper static bounds
14539 -- be used (a case statement, for example) and for those cases
14540 -- we must use the derived type (first subtype), not its base.
14542 -- If the derived_type_definition has no constraints, we know that
14543 -- the derived type has the same constraints as the first subtype
14544 -- of the parent, and we can also use it rather than its base,
14545 -- which can lead to more efficient code.
14547 if Etype (Id) = Parent_Type then
14548 if Is_Scalar_Type (Parent_Type)
14550 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14552 Set_Etype (New_Id, Derived_Type);
14554 elsif Nkind (Par) = N_Full_Type_Declaration
14556 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14559 (Subtype_Indication (Type_Definition (Par)))
14561 Set_Etype (New_Id, Derived_Type);
14564 Set_Etype (New_Id, Base_Type (Derived_Type));
14568 Set_Etype (New_Id, Base_Type (Derived_Type));
14572 Set_Etype (New_Id, Etype (Id));
14576 ----------------------
14577 -- Set_Derived_Name --
14578 ----------------------
14580 procedure Set_Derived_Name is
14581 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14583 if Nm = TSS_Null then
14584 Set_Chars (New_Subp, Chars (Parent_Subp));
14586 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14588 end Set_Derived_Name;
14590 -- Start of processing for Derive_Subprogram
14593 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14594 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14596 -- Check whether the inherited subprogram is a private operation that
14597 -- should be inherited but not yet made visible. Such subprograms can
14598 -- become visible at a later point (e.g., the private part of a public
14599 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14600 -- following predicate is true, then this is not such a private
14601 -- operation and the subprogram simply inherits the name of the parent
14602 -- subprogram. Note the special check for the names of controlled
14603 -- operations, which are currently exempted from being inherited with
14604 -- a hidden name because they must be findable for generation of
14605 -- implicit run-time calls.
14607 if not Is_Hidden (Parent_Subp)
14608 or else Is_Internal (Parent_Subp)
14609 or else Is_Private_Overriding
14610 or else Is_Internal_Name (Chars (Parent_Subp))
14611 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
14617 -- An inherited dispatching equality will be overridden by an internally
14618 -- generated one, or by an explicit one, so preserve its name and thus
14619 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14620 -- private operation it may become invisible if the full view has
14621 -- progenitors, and the dispatch table will be malformed.
14622 -- We check that the type is limited to handle the anomalous declaration
14623 -- of Limited_Controlled, which is derived from a non-limited type, and
14624 -- which is handled specially elsewhere as well.
14626 elsif Chars (Parent_Subp) = Name_Op_Eq
14627 and then Is_Dispatching_Operation (Parent_Subp)
14628 and then Etype (Parent_Subp) = Standard_Boolean
14629 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
14631 Etype (First_Formal (Parent_Subp)) =
14632 Etype (Next_Formal (First_Formal (Parent_Subp)))
14636 -- If parent is hidden, this can be a regular derivation if the
14637 -- parent is immediately visible in a non-instantiating context,
14638 -- or if we are in the private part of an instance. This test
14639 -- should still be refined ???
14641 -- The test for In_Instance_Not_Visible avoids inheriting the derived
14642 -- operation as a non-visible operation in cases where the parent
14643 -- subprogram might not be visible now, but was visible within the
14644 -- original generic, so it would be wrong to make the inherited
14645 -- subprogram non-visible now. (Not clear if this test is fully
14646 -- correct; are there any cases where we should declare the inherited
14647 -- operation as not visible to avoid it being overridden, e.g., when
14648 -- the parent type is a generic actual with private primitives ???)
14650 -- (they should be treated the same as other private inherited
14651 -- subprograms, but it's not clear how to do this cleanly). ???
14653 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14654 and then Is_Immediately_Visible (Parent_Subp)
14655 and then not In_Instance)
14656 or else In_Instance_Not_Visible
14660 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
14661 -- overrides an interface primitive because interface primitives
14662 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
14664 elsif Ada_Version >= Ada_2005
14665 and then Is_Dispatching_Operation (Parent_Subp)
14666 and then Covers_Some_Interface (Parent_Subp)
14670 -- Otherwise, the type is inheriting a private operation, so enter
14671 -- it with a special name so it can't be overridden.
14674 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
14677 Set_Parent (New_Subp, Parent (Derived_Type));
14679 if Present (Actual_Subp) then
14680 Replace_Type (Actual_Subp, New_Subp);
14682 Replace_Type (Parent_Subp, New_Subp);
14685 Conditional_Delay (New_Subp, Parent_Subp);
14687 -- If we are creating a renaming for a primitive operation of an
14688 -- actual of a generic derived type, we must examine the signature
14689 -- of the actual primitive, not that of the generic formal, which for
14690 -- example may be an interface. However the name and initial value
14691 -- of the inherited operation are those of the formal primitive.
14693 Formal := First_Formal (Parent_Subp);
14695 if Present (Actual_Subp) then
14696 Formal_Of_Actual := First_Formal (Actual_Subp);
14698 Formal_Of_Actual := Empty;
14701 while Present (Formal) loop
14702 New_Formal := New_Copy (Formal);
14704 -- Normally we do not go copying parents, but in the case of
14705 -- formals, we need to link up to the declaration (which is the
14706 -- parameter specification), and it is fine to link up to the
14707 -- original formal's parameter specification in this case.
14709 Set_Parent (New_Formal, Parent (Formal));
14710 Append_Entity (New_Formal, New_Subp);
14712 if Present (Formal_Of_Actual) then
14713 Replace_Type (Formal_Of_Actual, New_Formal);
14714 Next_Formal (Formal_Of_Actual);
14716 Replace_Type (Formal, New_Formal);
14719 Next_Formal (Formal);
14722 -- If this derivation corresponds to a tagged generic actual, then
14723 -- primitive operations rename those of the actual. Otherwise the
14724 -- primitive operations rename those of the parent type, If the parent
14725 -- renames an intrinsic operator, so does the new subprogram. We except
14726 -- concatenation, which is always properly typed, and does not get
14727 -- expanded as other intrinsic operations.
14729 if No (Actual_Subp) then
14730 if Is_Intrinsic_Subprogram (Parent_Subp) then
14731 Set_Is_Intrinsic_Subprogram (New_Subp);
14733 if Present (Alias (Parent_Subp))
14734 and then Chars (Parent_Subp) /= Name_Op_Concat
14736 Set_Alias (New_Subp, Alias (Parent_Subp));
14738 Set_Alias (New_Subp, Parent_Subp);
14742 Set_Alias (New_Subp, Parent_Subp);
14746 Set_Alias (New_Subp, Actual_Subp);
14749 -- Inherit the "ghostness" from the parent subprogram
14751 if Is_Ghost_Entity (Alias (New_Subp)) then
14752 Set_Is_Ghost_Entity (New_Subp);
14755 -- Derived subprograms of a tagged type must inherit the convention
14756 -- of the parent subprogram (a requirement of AI-117). Derived
14757 -- subprograms of untagged types simply get convention Ada by default.
14759 -- If the derived type is a tagged generic formal type with unknown
14760 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
14762 -- However, if the type is derived from a generic formal, the further
14763 -- inherited subprogram has the convention of the non-generic ancestor.
14764 -- Otherwise there would be no way to override the operation.
14765 -- (This is subject to forthcoming ARG discussions).
14767 if Is_Tagged_Type (Derived_Type) then
14768 if Is_Generic_Type (Derived_Type)
14769 and then Has_Unknown_Discriminants (Derived_Type)
14771 Set_Convention (New_Subp, Convention_Intrinsic);
14774 if Is_Generic_Type (Parent_Type)
14775 and then Has_Unknown_Discriminants (Parent_Type)
14777 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
14779 Set_Convention (New_Subp, Convention (Parent_Subp));
14784 -- Predefined controlled operations retain their name even if the parent
14785 -- is hidden (see above), but they are not primitive operations if the
14786 -- ancestor is not visible, for example if the parent is a private
14787 -- extension completed with a controlled extension. Note that a full
14788 -- type that is controlled can break privacy: the flag Is_Controlled is
14789 -- set on both views of the type.
14791 if Is_Controlled (Parent_Type)
14792 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
14795 and then Is_Hidden (Parent_Subp)
14796 and then not Is_Visibly_Controlled (Parent_Type)
14798 Set_Is_Hidden (New_Subp);
14801 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
14802 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
14804 if Ekind (Parent_Subp) = E_Procedure then
14805 Set_Is_Valued_Procedure
14806 (New_Subp, Is_Valued_Procedure (Parent_Subp));
14808 Set_Has_Controlling_Result
14809 (New_Subp, Has_Controlling_Result (Parent_Subp));
14812 -- No_Return must be inherited properly. If this is overridden in the
14813 -- case of a dispatching operation, then a check is made in Sem_Disp
14814 -- that the overriding operation is also No_Return (no such check is
14815 -- required for the case of non-dispatching operation.
14817 Set_No_Return (New_Subp, No_Return (Parent_Subp));
14819 -- A derived function with a controlling result is abstract. If the
14820 -- Derived_Type is a nonabstract formal generic derived type, then
14821 -- inherited operations are not abstract: the required check is done at
14822 -- instantiation time. If the derivation is for a generic actual, the
14823 -- function is not abstract unless the actual is.
14825 if Is_Generic_Type (Derived_Type)
14826 and then not Is_Abstract_Type (Derived_Type)
14830 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
14831 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
14833 -- A subprogram subject to pragma Extensions_Visible with value False
14834 -- requires overriding if the subprogram has at least one controlling
14835 -- OUT parameter (SPARK RM 6.1.7(6)).
14837 elsif Ada_Version >= Ada_2005
14838 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14839 or else (Is_Tagged_Type (Derived_Type)
14840 and then Etype (New_Subp) = Derived_Type
14841 and then not Is_Null_Extension (Derived_Type))
14842 or else (Is_Tagged_Type (Derived_Type)
14843 and then Ekind (Etype (New_Subp)) =
14844 E_Anonymous_Access_Type
14845 and then Designated_Type (Etype (New_Subp)) =
14847 and then not Is_Null_Extension (Derived_Type))
14848 or else (Comes_From_Source (Alias (New_Subp))
14849 and then Is_EVF_Procedure (Alias (New_Subp))))
14850 and then No (Actual_Subp)
14852 if not Is_Tagged_Type (Derived_Type)
14853 or else Is_Abstract_Type (Derived_Type)
14854 or else Is_Abstract_Subprogram (Alias (New_Subp))
14856 Set_Is_Abstract_Subprogram (New_Subp);
14858 Set_Requires_Overriding (New_Subp);
14861 elsif Ada_Version < Ada_2005
14862 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14863 or else (Is_Tagged_Type (Derived_Type)
14864 and then Etype (New_Subp) = Derived_Type
14865 and then No (Actual_Subp)))
14867 Set_Is_Abstract_Subprogram (New_Subp);
14869 -- AI05-0097 : an inherited operation that dispatches on result is
14870 -- abstract if the derived type is abstract, even if the parent type
14871 -- is concrete and the derived type is a null extension.
14873 elsif Has_Controlling_Result (Alias (New_Subp))
14874 and then Is_Abstract_Type (Etype (New_Subp))
14876 Set_Is_Abstract_Subprogram (New_Subp);
14878 -- Finally, if the parent type is abstract we must verify that all
14879 -- inherited operations are either non-abstract or overridden, or that
14880 -- the derived type itself is abstract (this check is performed at the
14881 -- end of a package declaration, in Check_Abstract_Overriding). A
14882 -- private overriding in the parent type will not be visible in the
14883 -- derivation if we are not in an inner package or in a child unit of
14884 -- the parent type, in which case the abstractness of the inherited
14885 -- operation is carried to the new subprogram.
14887 elsif Is_Abstract_Type (Parent_Type)
14888 and then not In_Open_Scopes (Scope (Parent_Type))
14889 and then Is_Private_Overriding
14890 and then Is_Abstract_Subprogram (Visible_Subp)
14892 if No (Actual_Subp) then
14893 Set_Alias (New_Subp, Visible_Subp);
14894 Set_Is_Abstract_Subprogram (New_Subp, True);
14897 -- If this is a derivation for an instance of a formal derived
14898 -- type, abstractness comes from the primitive operation of the
14899 -- actual, not from the operation inherited from the ancestor.
14901 Set_Is_Abstract_Subprogram
14902 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
14906 New_Overloaded_Entity (New_Subp, Derived_Type);
14908 -- Check for case of a derived subprogram for the instantiation of a
14909 -- formal derived tagged type, if so mark the subprogram as dispatching
14910 -- and inherit the dispatching attributes of the actual subprogram. The
14911 -- derived subprogram is effectively renaming of the actual subprogram,
14912 -- so it needs to have the same attributes as the actual.
14914 if Present (Actual_Subp)
14915 and then Is_Dispatching_Operation (Actual_Subp)
14917 Set_Is_Dispatching_Operation (New_Subp);
14919 if Present (DTC_Entity (Actual_Subp)) then
14920 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
14921 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
14925 -- Indicate that a derived subprogram does not require a body and that
14926 -- it does not require processing of default expressions.
14928 Set_Has_Completion (New_Subp);
14929 Set_Default_Expressions_Processed (New_Subp);
14931 if Ekind (New_Subp) = E_Function then
14932 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
14934 end Derive_Subprogram;
14936 ------------------------
14937 -- Derive_Subprograms --
14938 ------------------------
14940 procedure Derive_Subprograms
14941 (Parent_Type : Entity_Id;
14942 Derived_Type : Entity_Id;
14943 Generic_Actual : Entity_Id := Empty)
14945 Op_List : constant Elist_Id :=
14946 Collect_Primitive_Operations (Parent_Type);
14948 function Check_Derived_Type return Boolean;
14949 -- Check that all the entities derived from Parent_Type are found in
14950 -- the list of primitives of Derived_Type exactly in the same order.
14952 procedure Derive_Interface_Subprogram
14953 (New_Subp : in out Entity_Id;
14955 Actual_Subp : Entity_Id);
14956 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
14957 -- (which is an interface primitive). If Generic_Actual is present then
14958 -- Actual_Subp is the actual subprogram corresponding with the generic
14959 -- subprogram Subp.
14961 function Check_Derived_Type return Boolean is
14965 New_Subp : Entity_Id;
14970 -- Traverse list of entities in the current scope searching for
14971 -- an incomplete type whose full-view is derived type
14973 E := First_Entity (Scope (Derived_Type));
14974 while Present (E) and then E /= Derived_Type loop
14975 if Ekind (E) = E_Incomplete_Type
14976 and then Present (Full_View (E))
14977 and then Full_View (E) = Derived_Type
14979 -- Disable this test if Derived_Type completes an incomplete
14980 -- type because in such case more primitives can be added
14981 -- later to the list of primitives of Derived_Type by routine
14982 -- Process_Incomplete_Dependents
14987 E := Next_Entity (E);
14990 List := Collect_Primitive_Operations (Derived_Type);
14991 Elmt := First_Elmt (List);
14993 Op_Elmt := First_Elmt (Op_List);
14994 while Present (Op_Elmt) loop
14995 Subp := Node (Op_Elmt);
14996 New_Subp := Node (Elmt);
14998 -- At this early stage Derived_Type has no entities with attribute
14999 -- Interface_Alias. In addition, such primitives are always
15000 -- located at the end of the list of primitives of Parent_Type.
15001 -- Therefore, if found we can safely stop processing pending
15004 exit when Present (Interface_Alias (Subp));
15006 -- Handle hidden entities
15008 if not Is_Predefined_Dispatching_Operation (Subp)
15009 and then Is_Hidden (Subp)
15011 if Present (New_Subp)
15012 and then Primitive_Names_Match (Subp, New_Subp)
15018 if not Present (New_Subp)
15019 or else Ekind (Subp) /= Ekind (New_Subp)
15020 or else not Primitive_Names_Match (Subp, New_Subp)
15028 Next_Elmt (Op_Elmt);
15032 end Check_Derived_Type;
15034 ---------------------------------
15035 -- Derive_Interface_Subprogram --
15036 ---------------------------------
15038 procedure Derive_Interface_Subprogram
15039 (New_Subp : in out Entity_Id;
15041 Actual_Subp : Entity_Id)
15043 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15044 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15047 pragma Assert (Is_Interface (Iface_Type));
15050 (New_Subp => New_Subp,
15051 Parent_Subp => Iface_Subp,
15052 Derived_Type => Derived_Type,
15053 Parent_Type => Iface_Type,
15054 Actual_Subp => Actual_Subp);
15056 -- Given that this new interface entity corresponds with a primitive
15057 -- of the parent that was not overridden we must leave it associated
15058 -- with its parent primitive to ensure that it will share the same
15059 -- dispatch table slot when overridden.
15061 if No (Actual_Subp) then
15062 Set_Alias (New_Subp, Subp);
15064 -- For instantiations this is not needed since the previous call to
15065 -- Derive_Subprogram leaves the entity well decorated.
15068 pragma Assert (Alias (New_Subp) = Actual_Subp);
15071 end Derive_Interface_Subprogram;
15075 Alias_Subp : Entity_Id;
15076 Act_List : Elist_Id;
15077 Act_Elmt : Elmt_Id;
15078 Act_Subp : Entity_Id := Empty;
15080 Need_Search : Boolean := False;
15081 New_Subp : Entity_Id := Empty;
15082 Parent_Base : Entity_Id;
15085 -- Start of processing for Derive_Subprograms
15088 if Ekind (Parent_Type) = E_Record_Type_With_Private
15089 and then Has_Discriminants (Parent_Type)
15090 and then Present (Full_View (Parent_Type))
15092 Parent_Base := Full_View (Parent_Type);
15094 Parent_Base := Parent_Type;
15097 if Present (Generic_Actual) then
15098 Act_List := Collect_Primitive_Operations (Generic_Actual);
15099 Act_Elmt := First_Elmt (Act_List);
15101 Act_List := No_Elist;
15102 Act_Elmt := No_Elmt;
15105 -- Derive primitives inherited from the parent. Note that if the generic
15106 -- actual is present, this is not really a type derivation, it is a
15107 -- completion within an instance.
15109 -- Case 1: Derived_Type does not implement interfaces
15111 if not Is_Tagged_Type (Derived_Type)
15112 or else (not Has_Interfaces (Derived_Type)
15113 and then not (Present (Generic_Actual)
15114 and then Has_Interfaces (Generic_Actual)))
15116 Elmt := First_Elmt (Op_List);
15117 while Present (Elmt) loop
15118 Subp := Node (Elmt);
15120 -- Literals are derived earlier in the process of building the
15121 -- derived type, and are skipped here.
15123 if Ekind (Subp) = E_Enumeration_Literal then
15126 -- The actual is a direct descendant and the common primitive
15127 -- operations appear in the same order.
15129 -- If the generic parent type is present, the derived type is an
15130 -- instance of a formal derived type, and within the instance its
15131 -- operations are those of the actual. We derive from the formal
15132 -- type but make the inherited operations aliases of the
15133 -- corresponding operations of the actual.
15136 pragma Assert (No (Node (Act_Elmt))
15137 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15140 (Subp, Node (Act_Elmt),
15141 Skip_Controlling_Formals => True)));
15144 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15146 if Present (Act_Elmt) then
15147 Next_Elmt (Act_Elmt);
15154 -- Case 2: Derived_Type implements interfaces
15157 -- If the parent type has no predefined primitives we remove
15158 -- predefined primitives from the list of primitives of generic
15159 -- actual to simplify the complexity of this algorithm.
15161 if Present (Generic_Actual) then
15163 Has_Predefined_Primitives : Boolean := False;
15166 -- Check if the parent type has predefined primitives
15168 Elmt := First_Elmt (Op_List);
15169 while Present (Elmt) loop
15170 Subp := Node (Elmt);
15172 if Is_Predefined_Dispatching_Operation (Subp)
15173 and then not Comes_From_Source (Ultimate_Alias (Subp))
15175 Has_Predefined_Primitives := True;
15182 -- Remove predefined primitives of Generic_Actual. We must use
15183 -- an auxiliary list because in case of tagged types the value
15184 -- returned by Collect_Primitive_Operations is the value stored
15185 -- in its Primitive_Operations attribute (and we don't want to
15186 -- modify its current contents).
15188 if not Has_Predefined_Primitives then
15190 Aux_List : constant Elist_Id := New_Elmt_List;
15193 Elmt := First_Elmt (Act_List);
15194 while Present (Elmt) loop
15195 Subp := Node (Elmt);
15197 if not Is_Predefined_Dispatching_Operation (Subp)
15198 or else Comes_From_Source (Subp)
15200 Append_Elmt (Subp, Aux_List);
15206 Act_List := Aux_List;
15210 Act_Elmt := First_Elmt (Act_List);
15211 Act_Subp := Node (Act_Elmt);
15215 -- Stage 1: If the generic actual is not present we derive the
15216 -- primitives inherited from the parent type. If the generic parent
15217 -- type is present, the derived type is an instance of a formal
15218 -- derived type, and within the instance its operations are those of
15219 -- the actual. We derive from the formal type but make the inherited
15220 -- operations aliases of the corresponding operations of the actual.
15222 Elmt := First_Elmt (Op_List);
15223 while Present (Elmt) loop
15224 Subp := Node (Elmt);
15225 Alias_Subp := Ultimate_Alias (Subp);
15227 -- Do not derive internal entities of the parent that link
15228 -- interface primitives with their covering primitive. These
15229 -- entities will be added to this type when frozen.
15231 if Present (Interface_Alias (Subp)) then
15235 -- If the generic actual is present find the corresponding
15236 -- operation in the generic actual. If the parent type is a
15237 -- direct ancestor of the derived type then, even if it is an
15238 -- interface, the operations are inherited from the primary
15239 -- dispatch table and are in the proper order. If we detect here
15240 -- that primitives are not in the same order we traverse the list
15241 -- of primitive operations of the actual to find the one that
15242 -- implements the interface primitive.
15246 (Present (Generic_Actual)
15247 and then Present (Act_Subp)
15249 (Primitive_Names_Match (Subp, Act_Subp)
15251 Type_Conformant (Subp, Act_Subp,
15252 Skip_Controlling_Formals => True)))
15254 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15255 Use_Full_View => True));
15257 -- Remember that we need searching for all pending primitives
15259 Need_Search := True;
15261 -- Handle entities associated with interface primitives
15263 if Present (Alias_Subp)
15264 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15265 and then not Is_Predefined_Dispatching_Operation (Subp)
15267 -- Search for the primitive in the homonym chain
15270 Find_Primitive_Covering_Interface
15271 (Tagged_Type => Generic_Actual,
15272 Iface_Prim => Alias_Subp);
15274 -- Previous search may not locate primitives covering
15275 -- interfaces defined in generics units or instantiations.
15276 -- (it fails if the covering primitive has formals whose
15277 -- type is also defined in generics or instantiations).
15278 -- In such case we search in the list of primitives of the
15279 -- generic actual for the internal entity that links the
15280 -- interface primitive and the covering primitive.
15283 and then Is_Generic_Type (Parent_Type)
15285 -- This code has been designed to handle only generic
15286 -- formals that implement interfaces that are defined
15287 -- in a generic unit or instantiation. If this code is
15288 -- needed for other cases we must review it because
15289 -- (given that it relies on Original_Location to locate
15290 -- the primitive of Generic_Actual that covers the
15291 -- interface) it could leave linked through attribute
15292 -- Alias entities of unrelated instantiations).
15296 (Scope (Find_Dispatching_Type (Alias_Subp)))
15298 Instantiation_Depth
15299 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15302 Iface_Prim_Loc : constant Source_Ptr :=
15303 Original_Location (Sloc (Alias_Subp));
15310 First_Elmt (Primitive_Operations (Generic_Actual));
15312 Search : while Present (Elmt) loop
15313 Prim := Node (Elmt);
15315 if Present (Interface_Alias (Prim))
15316 and then Original_Location
15317 (Sloc (Interface_Alias (Prim))) =
15320 Act_Subp := Alias (Prim);
15329 pragma Assert (Present (Act_Subp)
15330 or else Is_Abstract_Type (Generic_Actual)
15331 or else Serious_Errors_Detected > 0);
15333 -- Handle predefined primitives plus the rest of user-defined
15337 Act_Elmt := First_Elmt (Act_List);
15338 while Present (Act_Elmt) loop
15339 Act_Subp := Node (Act_Elmt);
15341 exit when Primitive_Names_Match (Subp, Act_Subp)
15342 and then Type_Conformant
15344 Skip_Controlling_Formals => True)
15345 and then No (Interface_Alias (Act_Subp));
15347 Next_Elmt (Act_Elmt);
15350 if No (Act_Elmt) then
15356 -- Case 1: If the parent is a limited interface then it has the
15357 -- predefined primitives of synchronized interfaces. However, the
15358 -- actual type may be a non-limited type and hence it does not
15359 -- have such primitives.
15361 if Present (Generic_Actual)
15362 and then not Present (Act_Subp)
15363 and then Is_Limited_Interface (Parent_Base)
15364 and then Is_Predefined_Interface_Primitive (Subp)
15368 -- Case 2: Inherit entities associated with interfaces that were
15369 -- not covered by the parent type. We exclude here null interface
15370 -- primitives because they do not need special management.
15372 -- We also exclude interface operations that are renamings. If the
15373 -- subprogram is an explicit renaming of an interface primitive,
15374 -- it is a regular primitive operation, and the presence of its
15375 -- alias is not relevant: it has to be derived like any other
15378 elsif Present (Alias (Subp))
15379 and then Nkind (Unit_Declaration_Node (Subp)) /=
15380 N_Subprogram_Renaming_Declaration
15381 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15383 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15384 and then Null_Present (Parent (Alias_Subp)))
15386 -- If this is an abstract private type then we transfer the
15387 -- derivation of the interface primitive from the partial view
15388 -- to the full view. This is safe because all the interfaces
15389 -- must be visible in the partial view. Done to avoid adding
15390 -- a new interface derivation to the private part of the
15391 -- enclosing package; otherwise this new derivation would be
15392 -- decorated as hidden when the analysis of the enclosing
15393 -- package completes.
15395 if Is_Abstract_Type (Derived_Type)
15396 and then In_Private_Part (Current_Scope)
15397 and then Has_Private_Declaration (Derived_Type)
15400 Partial_View : Entity_Id;
15405 Partial_View := First_Entity (Current_Scope);
15407 exit when No (Partial_View)
15408 or else (Has_Private_Declaration (Partial_View)
15410 Full_View (Partial_View) = Derived_Type);
15412 Next_Entity (Partial_View);
15415 -- If the partial view was not found then the source code
15416 -- has errors and the derivation is not needed.
15418 if Present (Partial_View) then
15420 First_Elmt (Primitive_Operations (Partial_View));
15421 while Present (Elmt) loop
15422 Ent := Node (Elmt);
15424 if Present (Alias (Ent))
15425 and then Ultimate_Alias (Ent) = Alias (Subp)
15428 (Ent, Primitive_Operations (Derived_Type));
15435 -- If the interface primitive was not found in the
15436 -- partial view then this interface primitive was
15437 -- overridden. We add a derivation to activate in
15438 -- Derive_Progenitor_Subprograms the machinery to
15442 Derive_Interface_Subprogram
15443 (New_Subp => New_Subp,
15445 Actual_Subp => Act_Subp);
15450 Derive_Interface_Subprogram
15451 (New_Subp => New_Subp,
15453 Actual_Subp => Act_Subp);
15456 -- Case 3: Common derivation
15460 (New_Subp => New_Subp,
15461 Parent_Subp => Subp,
15462 Derived_Type => Derived_Type,
15463 Parent_Type => Parent_Base,
15464 Actual_Subp => Act_Subp);
15467 -- No need to update Act_Elm if we must search for the
15468 -- corresponding operation in the generic actual
15471 and then Present (Act_Elmt)
15473 Next_Elmt (Act_Elmt);
15474 Act_Subp := Node (Act_Elmt);
15481 -- Inherit additional operations from progenitors. If the derived
15482 -- type is a generic actual, there are not new primitive operations
15483 -- for the type because it has those of the actual, and therefore
15484 -- nothing needs to be done. The renamings generated above are not
15485 -- primitive operations, and their purpose is simply to make the
15486 -- proper operations visible within an instantiation.
15488 if No (Generic_Actual) then
15489 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15493 -- Final check: Direct descendants must have their primitives in the
15494 -- same order. We exclude from this test untagged types and instances
15495 -- of formal derived types. We skip this test if we have already
15496 -- reported serious errors in the sources.
15498 pragma Assert (not Is_Tagged_Type (Derived_Type)
15499 or else Present (Generic_Actual)
15500 or else Serious_Errors_Detected > 0
15501 or else Check_Derived_Type);
15502 end Derive_Subprograms;
15504 --------------------------------
15505 -- Derived_Standard_Character --
15506 --------------------------------
15508 procedure Derived_Standard_Character
15510 Parent_Type : Entity_Id;
15511 Derived_Type : Entity_Id)
15513 Loc : constant Source_Ptr := Sloc (N);
15514 Def : constant Node_Id := Type_Definition (N);
15515 Indic : constant Node_Id := Subtype_Indication (Def);
15516 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15517 Implicit_Base : constant Entity_Id :=
15519 (E_Enumeration_Type, N, Derived_Type, 'B');
15525 Discard_Node (Process_Subtype (Indic, N));
15527 Set_Etype (Implicit_Base, Parent_Base);
15528 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15529 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15531 Set_Is_Character_Type (Implicit_Base, True);
15532 Set_Has_Delayed_Freeze (Implicit_Base);
15534 -- The bounds of the implicit base are the bounds of the parent base.
15535 -- Note that their type is the parent base.
15537 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15538 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15540 Set_Scalar_Range (Implicit_Base,
15543 High_Bound => Hi));
15545 Conditional_Delay (Derived_Type, Parent_Type);
15547 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15548 Set_Etype (Derived_Type, Implicit_Base);
15549 Set_Size_Info (Derived_Type, Parent_Type);
15551 if Unknown_RM_Size (Derived_Type) then
15552 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15555 Set_Is_Character_Type (Derived_Type, True);
15557 if Nkind (Indic) /= N_Subtype_Indication then
15559 -- If no explicit constraint, the bounds are those
15560 -- of the parent type.
15562 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15563 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15564 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15567 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15569 -- Because the implicit base is used in the conversion of the bounds, we
15570 -- have to freeze it now. This is similar to what is done for numeric
15571 -- types, and it equally suspicious, but otherwise a non-static bound
15572 -- will have a reference to an unfrozen type, which is rejected by Gigi
15573 -- (???). This requires specific care for definition of stream
15574 -- attributes. For details, see comments at the end of
15575 -- Build_Derived_Numeric_Type.
15577 Freeze_Before (N, Implicit_Base);
15578 end Derived_Standard_Character;
15580 ------------------------------
15581 -- Derived_Type_Declaration --
15582 ------------------------------
15584 procedure Derived_Type_Declaration
15587 Is_Completion : Boolean)
15589 Parent_Type : Entity_Id;
15591 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15592 -- Check whether the parent type is a generic formal, or derives
15593 -- directly or indirectly from one.
15595 ------------------------
15596 -- Comes_From_Generic --
15597 ------------------------
15599 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15601 if Is_Generic_Type (Typ) then
15604 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15607 elsif Is_Private_Type (Typ)
15608 and then Present (Full_View (Typ))
15609 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15613 elsif Is_Generic_Actual_Type (Typ) then
15619 end Comes_From_Generic;
15623 Def : constant Node_Id := Type_Definition (N);
15624 Iface_Def : Node_Id;
15625 Indic : constant Node_Id := Subtype_Indication (Def);
15626 Extension : constant Node_Id := Record_Extension_Part (Def);
15627 Parent_Node : Node_Id;
15630 -- Start of processing for Derived_Type_Declaration
15633 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
15635 -- Ada 2005 (AI-251): In case of interface derivation check that the
15636 -- parent is also an interface.
15638 if Interface_Present (Def) then
15639 Check_SPARK_05_Restriction ("interface is not allowed", Def);
15641 if not Is_Interface (Parent_Type) then
15642 Diagnose_Interface (Indic, Parent_Type);
15645 Parent_Node := Parent (Base_Type (Parent_Type));
15646 Iface_Def := Type_Definition (Parent_Node);
15648 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
15649 -- other limited interfaces.
15651 if Limited_Present (Def) then
15652 if Limited_Present (Iface_Def) then
15655 elsif Protected_Present (Iface_Def) then
15657 ("descendant of& must be declared"
15658 & " as a protected interface",
15661 elsif Synchronized_Present (Iface_Def) then
15663 ("descendant of& must be declared"
15664 & " as a synchronized interface",
15667 elsif Task_Present (Iface_Def) then
15669 ("descendant of& must be declared as a task interface",
15674 ("(Ada 2005) limited interface cannot "
15675 & "inherit from non-limited interface", Indic);
15678 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
15679 -- from non-limited or limited interfaces.
15681 elsif not Protected_Present (Def)
15682 and then not Synchronized_Present (Def)
15683 and then not Task_Present (Def)
15685 if Limited_Present (Iface_Def) then
15688 elsif Protected_Present (Iface_Def) then
15690 ("descendant of& must be declared"
15691 & " as a protected interface",
15694 elsif Synchronized_Present (Iface_Def) then
15696 ("descendant of& must be declared"
15697 & " as a synchronized interface",
15700 elsif Task_Present (Iface_Def) then
15702 ("descendant of& must be declared as a task interface",
15711 if Is_Tagged_Type (Parent_Type)
15712 and then Is_Concurrent_Type (Parent_Type)
15713 and then not Is_Interface (Parent_Type)
15716 ("parent type of a record extension cannot be "
15717 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
15718 Set_Etype (T, Any_Type);
15722 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
15725 if Is_Tagged_Type (Parent_Type)
15726 and then Is_Non_Empty_List (Interface_List (Def))
15733 Intf := First (Interface_List (Def));
15734 while Present (Intf) loop
15735 T := Find_Type_Of_Subtype_Indic (Intf);
15737 if not Is_Interface (T) then
15738 Diagnose_Interface (Intf, T);
15740 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
15741 -- a limited type from having a nonlimited progenitor.
15743 elsif (Limited_Present (Def)
15744 or else (not Is_Interface (Parent_Type)
15745 and then Is_Limited_Type (Parent_Type)))
15746 and then not Is_Limited_Interface (T)
15749 ("progenitor interface& of limited type must be limited",
15758 if Parent_Type = Any_Type
15759 or else Etype (Parent_Type) = Any_Type
15760 or else (Is_Class_Wide_Type (Parent_Type)
15761 and then Etype (Parent_Type) = T)
15763 -- If Parent_Type is undefined or illegal, make new type into a
15764 -- subtype of Any_Type, and set a few attributes to prevent cascaded
15765 -- errors. If this is a self-definition, emit error now.
15767 if T = Parent_Type or else T = Etype (Parent_Type) then
15768 Error_Msg_N ("type cannot be used in its own definition", Indic);
15771 Set_Ekind (T, Ekind (Parent_Type));
15772 Set_Etype (T, Any_Type);
15773 Set_Scalar_Range (T, Scalar_Range (Any_Type));
15775 if Is_Tagged_Type (T)
15776 and then Is_Record_Type (T)
15778 Set_Direct_Primitive_Operations (T, New_Elmt_List);
15784 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
15785 -- an interface is special because the list of interfaces in the full
15786 -- view can be given in any order. For example:
15788 -- type A is interface;
15789 -- type B is interface and A;
15790 -- type D is new B with private;
15792 -- type D is new A and B with null record; -- 1 --
15794 -- In this case we perform the following transformation of -1-:
15796 -- type D is new B and A with null record;
15798 -- If the parent of the full-view covers the parent of the partial-view
15799 -- we have two possible cases:
15801 -- 1) They have the same parent
15802 -- 2) The parent of the full-view implements some further interfaces
15804 -- In both cases we do not need to perform the transformation. In the
15805 -- first case the source program is correct and the transformation is
15806 -- not needed; in the second case the source program does not fulfill
15807 -- the no-hidden interfaces rule (AI-396) and the error will be reported
15810 -- This transformation not only simplifies the rest of the analysis of
15811 -- this type declaration but also simplifies the correct generation of
15812 -- the object layout to the expander.
15814 if In_Private_Part (Current_Scope)
15815 and then Is_Interface (Parent_Type)
15819 Partial_View : Entity_Id;
15820 Partial_View_Parent : Entity_Id;
15821 New_Iface : Node_Id;
15824 -- Look for the associated private type declaration
15826 Partial_View := First_Entity (Current_Scope);
15828 exit when No (Partial_View)
15829 or else (Has_Private_Declaration (Partial_View)
15830 and then Full_View (Partial_View) = T);
15832 Next_Entity (Partial_View);
15835 -- If the partial view was not found then the source code has
15836 -- errors and the transformation is not needed.
15838 if Present (Partial_View) then
15839 Partial_View_Parent := Etype (Partial_View);
15841 -- If the parent of the full-view covers the parent of the
15842 -- partial-view we have nothing else to do.
15844 if Interface_Present_In_Ancestor
15845 (Parent_Type, Partial_View_Parent)
15849 -- Traverse the list of interfaces of the full-view to look
15850 -- for the parent of the partial-view and perform the tree
15854 Iface := First (Interface_List (Def));
15855 while Present (Iface) loop
15856 if Etype (Iface) = Etype (Partial_View) then
15857 Rewrite (Subtype_Indication (Def),
15858 New_Copy (Subtype_Indication
15859 (Parent (Partial_View))));
15862 Make_Identifier (Sloc (N), Chars (Parent_Type));
15863 Append (New_Iface, Interface_List (Def));
15865 -- Analyze the transformed code
15867 Derived_Type_Declaration (T, N, Is_Completion);
15878 -- Only composite types other than array types are allowed to have
15881 if Present (Discriminant_Specifications (N)) then
15882 if (Is_Elementary_Type (Parent_Type)
15884 Is_Array_Type (Parent_Type))
15885 and then not Error_Posted (N)
15888 ("elementary or array type cannot have discriminants",
15889 Defining_Identifier (First (Discriminant_Specifications (N))));
15890 Set_Has_Discriminants (T, False);
15892 -- The type is allowed to have discriminants
15895 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
15899 -- In Ada 83, a derived type defined in a package specification cannot
15900 -- be used for further derivation until the end of its visible part.
15901 -- Note that derivation in the private part of the package is allowed.
15903 if Ada_Version = Ada_83
15904 and then Is_Derived_Type (Parent_Type)
15905 and then In_Visible_Part (Scope (Parent_Type))
15907 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
15909 ("(Ada 83): premature use of type for derivation", Indic);
15913 -- Check for early use of incomplete or private type
15915 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
15916 Error_Msg_N ("premature derivation of incomplete type", Indic);
15919 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
15920 and then not Comes_From_Generic (Parent_Type))
15921 or else Has_Private_Component (Parent_Type)
15923 -- The ancestor type of a formal type can be incomplete, in which
15924 -- case only the operations of the partial view are available in the
15925 -- generic. Subsequent checks may be required when the full view is
15926 -- analyzed to verify that a derivation from a tagged type has an
15929 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
15932 elsif No (Underlying_Type (Parent_Type))
15933 or else Has_Private_Component (Parent_Type)
15936 ("premature derivation of derived or private type", Indic);
15938 -- Flag the type itself as being in error, this prevents some
15939 -- nasty problems with subsequent uses of the malformed type.
15941 Set_Error_Posted (T);
15943 -- Check that within the immediate scope of an untagged partial
15944 -- view it's illegal to derive from the partial view if the
15945 -- full view is tagged. (7.3(7))
15947 -- We verify that the Parent_Type is a partial view by checking
15948 -- that it is not a Full_Type_Declaration (i.e. a private type or
15949 -- private extension declaration), to distinguish a partial view
15950 -- from a derivation from a private type which also appears as
15951 -- E_Private_Type. If the parent base type is not declared in an
15952 -- enclosing scope there is no need to check.
15954 elsif Present (Full_View (Parent_Type))
15955 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
15956 and then not Is_Tagged_Type (Parent_Type)
15957 and then Is_Tagged_Type (Full_View (Parent_Type))
15958 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15961 ("premature derivation from type with tagged full view",
15966 -- Check that form of derivation is appropriate
15968 Taggd := Is_Tagged_Type (Parent_Type);
15970 -- Set the parent type to the class-wide type's specific type in this
15971 -- case to prevent cascading errors
15973 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
15974 Error_Msg_N ("parent type must not be a class-wide type", Indic);
15975 Set_Etype (T, Etype (Parent_Type));
15979 if Present (Extension) and then not Taggd then
15981 ("type derived from untagged type cannot have extension", Indic);
15983 elsif No (Extension) and then Taggd then
15985 -- If this declaration is within a private part (or body) of a
15986 -- generic instantiation then the derivation is allowed (the parent
15987 -- type can only appear tagged in this case if it's a generic actual
15988 -- type, since it would otherwise have been rejected in the analysis
15989 -- of the generic template).
15991 if not Is_Generic_Actual_Type (Parent_Type)
15992 or else In_Visible_Part (Scope (Parent_Type))
15994 if Is_Class_Wide_Type (Parent_Type) then
15996 ("parent type must not be a class-wide type", Indic);
15998 -- Use specific type to prevent cascaded errors.
16000 Parent_Type := Etype (Parent_Type);
16004 ("type derived from tagged type must have extension", Indic);
16009 -- AI-443: Synchronized formal derived types require a private
16010 -- extension. There is no point in checking the ancestor type or
16011 -- the progenitors since the construct is wrong to begin with.
16013 if Ada_Version >= Ada_2005
16014 and then Is_Generic_Type (T)
16015 and then Present (Original_Node (N))
16018 Decl : constant Node_Id := Original_Node (N);
16021 if Nkind (Decl) = N_Formal_Type_Declaration
16022 and then Nkind (Formal_Type_Definition (Decl)) =
16023 N_Formal_Derived_Type_Definition
16024 and then Synchronized_Present (Formal_Type_Definition (Decl))
16025 and then No (Extension)
16027 -- Avoid emitting a duplicate error message
16029 and then not Error_Posted (Indic)
16032 ("synchronized derived type must have extension", N);
16037 if Null_Exclusion_Present (Def)
16038 and then not Is_Access_Type (Parent_Type)
16040 Error_Msg_N ("null exclusion can only apply to an access type", N);
16043 -- Avoid deriving parent primitives of underlying record views
16045 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16046 Derive_Subps => not Is_Underlying_Record_View (T));
16048 -- AI-419: The parent type of an explicitly limited derived type must
16049 -- be a limited type or a limited interface.
16051 if Limited_Present (Def) then
16052 Set_Is_Limited_Record (T);
16054 if Is_Interface (T) then
16055 Set_Is_Limited_Interface (T);
16058 if not Is_Limited_Type (Parent_Type)
16060 (not Is_Interface (Parent_Type)
16061 or else not Is_Limited_Interface (Parent_Type))
16063 -- AI05-0096: a derivation in the private part of an instance is
16064 -- legal if the generic formal is untagged limited, and the actual
16067 if Is_Generic_Actual_Type (Parent_Type)
16068 and then In_Private_Part (Current_Scope)
16071 (Generic_Parent_Type (Parent (Parent_Type)))
16077 ("parent type& of limited type must be limited",
16083 -- In SPARK, there are no derived type definitions other than type
16084 -- extensions of tagged record types.
16086 if No (Extension) then
16087 Check_SPARK_05_Restriction
16088 ("derived type is not allowed", Original_Node (N));
16090 end Derived_Type_Declaration;
16092 ------------------------
16093 -- Diagnose_Interface --
16094 ------------------------
16096 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16098 if not Is_Interface (E) and then E /= Any_Type then
16099 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16101 end Diagnose_Interface;
16103 ----------------------------------
16104 -- Enumeration_Type_Declaration --
16105 ----------------------------------
16107 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16114 -- Create identifier node representing lower bound
16116 B_Node := New_Node (N_Identifier, Sloc (Def));
16117 L := First (Literals (Def));
16118 Set_Chars (B_Node, Chars (L));
16119 Set_Entity (B_Node, L);
16120 Set_Etype (B_Node, T);
16121 Set_Is_Static_Expression (B_Node, True);
16123 R_Node := New_Node (N_Range, Sloc (Def));
16124 Set_Low_Bound (R_Node, B_Node);
16126 Set_Ekind (T, E_Enumeration_Type);
16127 Set_First_Literal (T, L);
16129 Set_Is_Constrained (T);
16133 -- Loop through literals of enumeration type setting pos and rep values
16134 -- except that if the Ekind is already set, then it means the literal
16135 -- was already constructed (case of a derived type declaration and we
16136 -- should not disturb the Pos and Rep values.
16138 while Present (L) loop
16139 if Ekind (L) /= E_Enumeration_Literal then
16140 Set_Ekind (L, E_Enumeration_Literal);
16141 Set_Enumeration_Pos (L, Ev);
16142 Set_Enumeration_Rep (L, Ev);
16143 Set_Is_Known_Valid (L, True);
16147 New_Overloaded_Entity (L);
16148 Generate_Definition (L);
16149 Set_Convention (L, Convention_Intrinsic);
16151 -- Case of character literal
16153 if Nkind (L) = N_Defining_Character_Literal then
16154 Set_Is_Character_Type (T, True);
16156 -- Check violation of No_Wide_Characters
16158 if Restriction_Check_Required (No_Wide_Characters) then
16159 Get_Name_String (Chars (L));
16161 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16162 Check_Restriction (No_Wide_Characters, L);
16171 -- Now create a node representing upper bound
16173 B_Node := New_Node (N_Identifier, Sloc (Def));
16174 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16175 Set_Entity (B_Node, Last (Literals (Def)));
16176 Set_Etype (B_Node, T);
16177 Set_Is_Static_Expression (B_Node, True);
16179 Set_High_Bound (R_Node, B_Node);
16181 -- Initialize various fields of the type. Some of this information
16182 -- may be overwritten later through rep.clauses.
16184 Set_Scalar_Range (T, R_Node);
16185 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16186 Set_Enum_Esize (T);
16187 Set_Enum_Pos_To_Rep (T, Empty);
16189 -- Set Discard_Names if configuration pragma set, or if there is
16190 -- a parameterless pragma in the current declarative region
16192 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16193 Set_Discard_Names (T);
16196 -- Process end label if there is one
16198 if Present (Def) then
16199 Process_End_Label (Def, 'e', T);
16201 end Enumeration_Type_Declaration;
16203 ---------------------------------
16204 -- Expand_To_Stored_Constraint --
16205 ---------------------------------
16207 function Expand_To_Stored_Constraint
16209 Constraint : Elist_Id) return Elist_Id
16211 Explicitly_Discriminated_Type : Entity_Id;
16212 Expansion : Elist_Id;
16213 Discriminant : Entity_Id;
16215 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16216 -- Find the nearest type that actually specifies discriminants
16218 ---------------------------------
16219 -- Type_With_Explicit_Discrims --
16220 ---------------------------------
16222 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16223 Typ : constant E := Base_Type (Id);
16226 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16227 if Present (Full_View (Typ)) then
16228 return Type_With_Explicit_Discrims (Full_View (Typ));
16232 if Has_Discriminants (Typ) then
16237 if Etype (Typ) = Typ then
16239 elsif Has_Discriminants (Typ) then
16242 return Type_With_Explicit_Discrims (Etype (Typ));
16245 end Type_With_Explicit_Discrims;
16247 -- Start of processing for Expand_To_Stored_Constraint
16250 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16254 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16256 if No (Explicitly_Discriminated_Type) then
16260 Expansion := New_Elmt_List;
16263 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16264 while Present (Discriminant) loop
16266 (Get_Discriminant_Value
16267 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16269 Next_Stored_Discriminant (Discriminant);
16273 end Expand_To_Stored_Constraint;
16275 ---------------------------
16276 -- Find_Hidden_Interface --
16277 ---------------------------
16279 function Find_Hidden_Interface
16281 Dest : Elist_Id) return Entity_Id
16284 Iface_Elmt : Elmt_Id;
16287 if Present (Src) and then Present (Dest) then
16288 Iface_Elmt := First_Elmt (Src);
16289 while Present (Iface_Elmt) loop
16290 Iface := Node (Iface_Elmt);
16292 if Is_Interface (Iface)
16293 and then not Contain_Interface (Iface, Dest)
16298 Next_Elmt (Iface_Elmt);
16303 end Find_Hidden_Interface;
16305 --------------------
16306 -- Find_Type_Name --
16307 --------------------
16309 function Find_Type_Name (N : Node_Id) return Entity_Id is
16310 Id : constant Entity_Id := Defining_Identifier (N);
16312 New_Id : Entity_Id;
16313 Prev_Par : Node_Id;
16315 procedure Check_Duplicate_Aspects;
16316 -- Check that aspects specified in a completion have not been specified
16317 -- already in the partial view. Type_Invariant and others can be
16318 -- specified on either view but never on both.
16320 procedure Tag_Mismatch;
16321 -- Diagnose a tagged partial view whose full view is untagged.
16322 -- We post the message on the full view, with a reference to
16323 -- the previous partial view. The partial view can be private
16324 -- or incomplete, and these are handled in a different manner,
16325 -- so we determine the position of the error message from the
16326 -- respective slocs of both.
16328 -----------------------------
16329 -- Check_Duplicate_Aspects --
16330 -----------------------------
16331 procedure Check_Duplicate_Aspects is
16332 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
16333 Full_Aspects : constant List_Id := Aspect_Specifications (N);
16334 F_Spec, P_Spec : Node_Id;
16337 if Present (Prev_Aspects) and then Present (Full_Aspects) then
16338 F_Spec := First (Full_Aspects);
16339 while Present (F_Spec) loop
16340 P_Spec := First (Prev_Aspects);
16341 while Present (P_Spec) loop
16342 if Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
16345 ("aspect already specified in private declaration",
16357 end Check_Duplicate_Aspects;
16363 procedure Tag_Mismatch is
16365 if Sloc (Prev) < Sloc (Id) then
16366 if Ada_Version >= Ada_2012
16367 and then Nkind (N) = N_Private_Type_Declaration
16370 ("declaration of private } must be a tagged type ", Id, Prev);
16373 ("full declaration of } must be a tagged type ", Id, Prev);
16377 if Ada_Version >= Ada_2012
16378 and then Nkind (N) = N_Private_Type_Declaration
16381 ("declaration of private } must be a tagged type ", Prev, Id);
16384 ("full declaration of } must be a tagged type ", Prev, Id);
16389 -- Start of processing for Find_Type_Name
16392 -- Find incomplete declaration, if one was given
16394 Prev := Current_Entity_In_Scope (Id);
16396 -- New type declaration
16402 -- Previous declaration exists
16405 Prev_Par := Parent (Prev);
16407 -- Error if not incomplete/private case except if previous
16408 -- declaration is implicit, etc. Enter_Name will emit error if
16411 if not Is_Incomplete_Or_Private_Type (Prev) then
16415 -- Check invalid completion of private or incomplete type
16417 elsif not Nkind_In (N, N_Full_Type_Declaration,
16418 N_Task_Type_Declaration,
16419 N_Protected_Type_Declaration)
16421 (Ada_Version < Ada_2012
16422 or else not Is_Incomplete_Type (Prev)
16423 or else not Nkind_In (N, N_Private_Type_Declaration,
16424 N_Private_Extension_Declaration))
16426 -- Completion must be a full type declarations (RM 7.3(4))
16428 Error_Msg_Sloc := Sloc (Prev);
16429 Error_Msg_NE ("invalid completion of }", Id, Prev);
16431 -- Set scope of Id to avoid cascaded errors. Entity is never
16432 -- examined again, except when saving globals in generics.
16434 Set_Scope (Id, Current_Scope);
16437 -- If this is a repeated incomplete declaration, no further
16438 -- checks are possible.
16440 if Nkind (N) = N_Incomplete_Type_Declaration then
16444 -- Case of full declaration of incomplete type
16446 elsif Ekind (Prev) = E_Incomplete_Type
16447 and then (Ada_Version < Ada_2012
16448 or else No (Full_View (Prev))
16449 or else not Is_Private_Type (Full_View (Prev)))
16451 -- Indicate that the incomplete declaration has a matching full
16452 -- declaration. The defining occurrence of the incomplete
16453 -- declaration remains the visible one, and the procedure
16454 -- Get_Full_View dereferences it whenever the type is used.
16456 if Present (Full_View (Prev)) then
16457 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16460 Set_Full_View (Prev, Id);
16461 Append_Entity (Id, Current_Scope);
16462 Set_Is_Public (Id, Is_Public (Prev));
16463 Set_Is_Internal (Id);
16466 -- If the incomplete view is tagged, a class_wide type has been
16467 -- created already. Use it for the private type as well, in order
16468 -- to prevent multiple incompatible class-wide types that may be
16469 -- created for self-referential anonymous access components.
16471 if Is_Tagged_Type (Prev)
16472 and then Present (Class_Wide_Type (Prev))
16474 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16475 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16477 -- The type of the classwide type is the current Id. Previously
16478 -- this was not done for private declarations because of order-
16479 -- of elaboration issues in the back-end, but gigi now handles
16482 Set_Etype (Class_Wide_Type (Id), Id);
16485 -- Case of full declaration of private type
16488 -- If the private type was a completion of an incomplete type then
16489 -- update Prev to reference the private type
16491 if Ada_Version >= Ada_2012
16492 and then Ekind (Prev) = E_Incomplete_Type
16493 and then Present (Full_View (Prev))
16494 and then Is_Private_Type (Full_View (Prev))
16496 Prev := Full_View (Prev);
16497 Prev_Par := Parent (Prev);
16500 if Nkind (N) = N_Full_Type_Declaration
16502 (Type_Definition (N), N_Record_Definition,
16503 N_Derived_Type_Definition)
16504 and then Interface_Present (Type_Definition (N))
16507 ("completion of private type cannot be an interface", N);
16510 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16511 if Etype (Prev) /= Prev then
16513 -- Prev is a private subtype or a derived type, and needs
16516 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16519 elsif Ekind (Prev) = E_Private_Type
16520 and then Nkind_In (N, N_Task_Type_Declaration,
16521 N_Protected_Type_Declaration)
16524 ("completion of nonlimited type cannot be limited", N);
16526 elsif Ekind (Prev) = E_Record_Type_With_Private
16527 and then Nkind_In (N, N_Task_Type_Declaration,
16528 N_Protected_Type_Declaration)
16530 if not Is_Limited_Record (Prev) then
16532 ("completion of nonlimited type cannot be limited", N);
16534 elsif No (Interface_List (N)) then
16536 ("completion of tagged private type must be tagged",
16541 -- Ada 2005 (AI-251): Private extension declaration of a task
16542 -- type or a protected type. This case arises when covering
16543 -- interface types.
16545 elsif Nkind_In (N, N_Task_Type_Declaration,
16546 N_Protected_Type_Declaration)
16550 elsif Nkind (N) /= N_Full_Type_Declaration
16551 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16554 ("full view of private extension must be an extension", N);
16556 elsif not (Abstract_Present (Parent (Prev)))
16557 and then Abstract_Present (Type_Definition (N))
16560 ("full view of non-abstract extension cannot be abstract", N);
16563 if not In_Private_Part (Current_Scope) then
16565 ("declaration of full view must appear in private part", N);
16568 if Ada_Version >= Ada_2012 then
16569 Check_Duplicate_Aspects;
16572 Copy_And_Swap (Prev, Id);
16573 Set_Has_Private_Declaration (Prev);
16574 Set_Has_Private_Declaration (Id);
16576 -- AI12-0133: Indicate whether we have a partial view with
16577 -- unknown discriminants, in which case initialization of objects
16578 -- of the type do not receive an invariant check.
16580 Set_Partial_View_Has_Unknown_Discr
16581 (Prev, Has_Unknown_Discriminants (Id));
16583 -- Preserve aspect and iterator flags that may have been set on
16584 -- the partial view.
16586 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
16587 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
16589 -- If no error, propagate freeze_node from private to full view.
16590 -- It may have been generated for an early operational item.
16592 if Present (Freeze_Node (Id))
16593 and then Serious_Errors_Detected = 0
16594 and then No (Full_View (Id))
16596 Set_Freeze_Node (Prev, Freeze_Node (Id));
16597 Set_Freeze_Node (Id, Empty);
16598 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
16601 Set_Full_View (Id, Prev);
16605 -- Verify that full declaration conforms to partial one
16607 if Is_Incomplete_Or_Private_Type (Prev)
16608 and then Present (Discriminant_Specifications (Prev_Par))
16610 if Present (Discriminant_Specifications (N)) then
16611 if Ekind (Prev) = E_Incomplete_Type then
16612 Check_Discriminant_Conformance (N, Prev, Prev);
16614 Check_Discriminant_Conformance (N, Prev, Id);
16619 ("missing discriminants in full type declaration", N);
16621 -- To avoid cascaded errors on subsequent use, share the
16622 -- discriminants of the partial view.
16624 Set_Discriminant_Specifications (N,
16625 Discriminant_Specifications (Prev_Par));
16629 -- A prior untagged partial view can have an associated class-wide
16630 -- type due to use of the class attribute, and in this case the full
16631 -- type must also be tagged. This Ada 95 usage is deprecated in favor
16632 -- of incomplete tagged declarations, but we check for it.
16635 and then (Is_Tagged_Type (Prev)
16636 or else Present (Class_Wide_Type (Prev)))
16638 -- Ada 2012 (AI05-0162): A private type may be the completion of
16639 -- an incomplete type.
16641 if Ada_Version >= Ada_2012
16642 and then Is_Incomplete_Type (Prev)
16643 and then Nkind_In (N, N_Private_Type_Declaration,
16644 N_Private_Extension_Declaration)
16646 -- No need to check private extensions since they are tagged
16648 if Nkind (N) = N_Private_Type_Declaration
16649 and then not Tagged_Present (N)
16654 -- The full declaration is either a tagged type (including
16655 -- a synchronized type that implements interfaces) or a
16656 -- type extension, otherwise this is an error.
16658 elsif Nkind_In (N, N_Task_Type_Declaration,
16659 N_Protected_Type_Declaration)
16661 if No (Interface_List (N)) and then not Error_Posted (N) then
16665 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
16667 -- Indicate that the previous declaration (tagged incomplete
16668 -- or private declaration) requires the same on the full one.
16670 if not Tagged_Present (Type_Definition (N)) then
16672 Set_Is_Tagged_Type (Id);
16675 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
16676 if No (Record_Extension_Part (Type_Definition (N))) then
16678 ("full declaration of } must be a record extension",
16681 -- Set some attributes to produce a usable full view
16683 Set_Is_Tagged_Type (Id);
16692 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
16693 and then Present (Premature_Use (Parent (Prev)))
16695 Error_Msg_Sloc := Sloc (N);
16697 ("\full declaration #", Premature_Use (Parent (Prev)));
16702 end Find_Type_Name;
16704 -------------------------
16705 -- Find_Type_Of_Object --
16706 -------------------------
16708 function Find_Type_Of_Object
16709 (Obj_Def : Node_Id;
16710 Related_Nod : Node_Id) return Entity_Id
16712 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
16713 P : Node_Id := Parent (Obj_Def);
16718 -- If the parent is a component_definition node we climb to the
16719 -- component_declaration node
16721 if Nkind (P) = N_Component_Definition then
16725 -- Case of an anonymous array subtype
16727 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
16728 N_Unconstrained_Array_Definition)
16731 Array_Type_Declaration (T, Obj_Def);
16733 -- Create an explicit subtype whenever possible
16735 elsif Nkind (P) /= N_Component_Declaration
16736 and then Def_Kind = N_Subtype_Indication
16738 -- Base name of subtype on object name, which will be unique in
16739 -- the current scope.
16741 -- If this is a duplicate declaration, return base type, to avoid
16742 -- generating duplicate anonymous types.
16744 if Error_Posted (P) then
16745 Analyze (Subtype_Mark (Obj_Def));
16746 return Entity (Subtype_Mark (Obj_Def));
16751 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
16753 T := Make_Defining_Identifier (Sloc (P), Nam);
16755 Insert_Action (Obj_Def,
16756 Make_Subtype_Declaration (Sloc (P),
16757 Defining_Identifier => T,
16758 Subtype_Indication => Relocate_Node (Obj_Def)));
16760 -- This subtype may need freezing, and this will not be done
16761 -- automatically if the object declaration is not in declarative
16762 -- part. Since this is an object declaration, the type cannot always
16763 -- be frozen here. Deferred constants do not freeze their type
16764 -- (which often enough will be private).
16766 if Nkind (P) = N_Object_Declaration
16767 and then Constant_Present (P)
16768 and then No (Expression (P))
16772 -- Here we freeze the base type of object type to catch premature use
16773 -- of discriminated private type without a full view.
16776 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
16779 -- Ada 2005 AI-406: the object definition in an object declaration
16780 -- can be an access definition.
16782 elsif Def_Kind = N_Access_Definition then
16783 T := Access_Definition (Related_Nod, Obj_Def);
16785 Set_Is_Local_Anonymous_Access
16787 V => (Ada_Version < Ada_2012)
16788 or else (Nkind (P) /= N_Object_Declaration)
16789 or else Is_Library_Level_Entity (Defining_Identifier (P)));
16791 -- Otherwise, the object definition is just a subtype_mark
16794 T := Process_Subtype (Obj_Def, Related_Nod);
16796 -- If expansion is disabled an object definition that is an aggregate
16797 -- will not get expanded and may lead to scoping problems in the back
16798 -- end, if the object is referenced in an inner scope. In that case
16799 -- create an itype reference for the object definition now. This
16800 -- may be redundant in some cases, but harmless.
16803 and then Nkind (Related_Nod) = N_Object_Declaration
16806 Build_Itype_Reference (T, Related_Nod);
16811 end Find_Type_Of_Object;
16813 --------------------------------
16814 -- Find_Type_Of_Subtype_Indic --
16815 --------------------------------
16817 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
16821 -- Case of subtype mark with a constraint
16823 if Nkind (S) = N_Subtype_Indication then
16824 Find_Type (Subtype_Mark (S));
16825 Typ := Entity (Subtype_Mark (S));
16828 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
16831 ("incorrect constraint for this kind of type", Constraint (S));
16832 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16835 -- Otherwise we have a subtype mark without a constraint
16837 elsif Error_Posted (S) then
16838 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
16846 -- Check No_Wide_Characters restriction
16848 Check_Wide_Character_Restriction (Typ, S);
16851 end Find_Type_Of_Subtype_Indic;
16853 -------------------------------------
16854 -- Floating_Point_Type_Declaration --
16855 -------------------------------------
16857 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16858 Digs : constant Node_Id := Digits_Expression (Def);
16859 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
16861 Base_Typ : Entity_Id;
16862 Implicit_Base : Entity_Id;
16865 function Can_Derive_From (E : Entity_Id) return Boolean;
16866 -- Find if given digits value, and possibly a specified range, allows
16867 -- derivation from specified type
16869 function Find_Base_Type return Entity_Id;
16870 -- Find a predefined base type that Def can derive from, or generate
16871 -- an error and substitute Long_Long_Float if none exists.
16873 ---------------------
16874 -- Can_Derive_From --
16875 ---------------------
16877 function Can_Derive_From (E : Entity_Id) return Boolean is
16878 Spec : constant Entity_Id := Real_Range_Specification (Def);
16881 -- Check specified "digits" constraint
16883 if Digs_Val > Digits_Value (E) then
16887 -- Check for matching range, if specified
16889 if Present (Spec) then
16890 if Expr_Value_R (Type_Low_Bound (E)) >
16891 Expr_Value_R (Low_Bound (Spec))
16896 if Expr_Value_R (Type_High_Bound (E)) <
16897 Expr_Value_R (High_Bound (Spec))
16904 end Can_Derive_From;
16906 --------------------
16907 -- Find_Base_Type --
16908 --------------------
16910 function Find_Base_Type return Entity_Id is
16911 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
16914 -- Iterate over the predefined types in order, returning the first
16915 -- one that Def can derive from.
16917 while Present (Choice) loop
16918 if Can_Derive_From (Node (Choice)) then
16919 return Node (Choice);
16922 Next_Elmt (Choice);
16925 -- If we can't derive from any existing type, use Long_Long_Float
16926 -- and give appropriate message explaining the problem.
16928 if Digs_Val > Max_Digs_Val then
16929 -- It might be the case that there is a type with the requested
16930 -- range, just not the combination of digits and range.
16933 ("no predefined type has requested range and precision",
16934 Real_Range_Specification (Def));
16938 ("range too large for any predefined type",
16939 Real_Range_Specification (Def));
16942 return Standard_Long_Long_Float;
16943 end Find_Base_Type;
16945 -- Start of processing for Floating_Point_Type_Declaration
16948 Check_Restriction (No_Floating_Point, Def);
16950 -- Create an implicit base type
16953 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
16955 -- Analyze and verify digits value
16957 Analyze_And_Resolve (Digs, Any_Integer);
16958 Check_Digits_Expression (Digs);
16959 Digs_Val := Expr_Value (Digs);
16961 -- Process possible range spec and find correct type to derive from
16963 Process_Real_Range_Specification (Def);
16965 -- Check that requested number of digits is not too high.
16967 if Digs_Val > Max_Digs_Val then
16969 -- The check for Max_Base_Digits may be somewhat expensive, as it
16970 -- requires reading System, so only do it when necessary.
16973 Max_Base_Digits : constant Uint :=
16976 (Parent (RTE (RE_Max_Base_Digits))));
16979 if Digs_Val > Max_Base_Digits then
16980 Error_Msg_Uint_1 := Max_Base_Digits;
16981 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
16983 elsif No (Real_Range_Specification (Def)) then
16984 Error_Msg_Uint_1 := Max_Digs_Val;
16985 Error_Msg_N ("types with more than ^ digits need range spec "
16986 & "(RM 3.5.7(6))", Digs);
16991 -- Find a suitable type to derive from or complain and use a substitute
16993 Base_Typ := Find_Base_Type;
16995 -- If there are bounds given in the declaration use them as the bounds
16996 -- of the type, otherwise use the bounds of the predefined base type
16997 -- that was chosen based on the Digits value.
16999 if Present (Real_Range_Specification (Def)) then
17000 Set_Scalar_Range (T, Real_Range_Specification (Def));
17001 Set_Is_Constrained (T);
17003 -- The bounds of this range must be converted to machine numbers
17004 -- in accordance with RM 4.9(38).
17006 Bound := Type_Low_Bound (T);
17008 if Nkind (Bound) = N_Real_Literal then
17010 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17011 Set_Is_Machine_Number (Bound);
17014 Bound := Type_High_Bound (T);
17016 if Nkind (Bound) = N_Real_Literal then
17018 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17019 Set_Is_Machine_Number (Bound);
17023 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17026 -- Complete definition of implicit base and declared first subtype. The
17027 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17028 -- are not clobbered when the floating point type acts as a full view of
17031 Set_Etype (Implicit_Base, Base_Typ);
17032 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17033 Set_Size_Info (Implicit_Base, Base_Typ);
17034 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17035 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17036 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17037 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17039 Set_Ekind (T, E_Floating_Point_Subtype);
17040 Set_Etype (T, Implicit_Base);
17041 Set_Size_Info (T, Implicit_Base);
17042 Set_RM_Size (T, RM_Size (Implicit_Base));
17043 Inherit_Rep_Item_Chain (T, Implicit_Base);
17044 Set_Digits_Value (T, Digs_Val);
17045 end Floating_Point_Type_Declaration;
17047 ----------------------------
17048 -- Get_Discriminant_Value --
17049 ----------------------------
17051 -- This is the situation:
17053 -- There is a non-derived type
17055 -- type T0 (Dx, Dy, Dz...)
17057 -- There are zero or more levels of derivation, with each derivation
17058 -- either purely inheriting the discriminants, or defining its own.
17060 -- type Ti is new Ti-1
17062 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17064 -- subtype Ti is ...
17066 -- The subtype issue is avoided by the use of Original_Record_Component,
17067 -- and the fact that derived subtypes also derive the constraints.
17069 -- This chain leads back from
17071 -- Typ_For_Constraint
17073 -- Typ_For_Constraint has discriminants, and the value for each
17074 -- discriminant is given by its corresponding Elmt of Constraints.
17076 -- Discriminant is some discriminant in this hierarchy
17078 -- We need to return its value
17080 -- We do this by recursively searching each level, and looking for
17081 -- Discriminant. Once we get to the bottom, we start backing up
17082 -- returning the value for it which may in turn be a discriminant
17083 -- further up, so on the backup we continue the substitution.
17085 function Get_Discriminant_Value
17086 (Discriminant : Entity_Id;
17087 Typ_For_Constraint : Entity_Id;
17088 Constraint : Elist_Id) return Node_Id
17090 function Root_Corresponding_Discriminant
17091 (Discr : Entity_Id) return Entity_Id;
17092 -- Given a discriminant, traverse the chain of inherited discriminants
17093 -- and return the topmost discriminant.
17095 function Search_Derivation_Levels
17097 Discrim_Values : Elist_Id;
17098 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17099 -- This is the routine that performs the recursive search of levels
17100 -- as described above.
17102 -------------------------------------
17103 -- Root_Corresponding_Discriminant --
17104 -------------------------------------
17106 function Root_Corresponding_Discriminant
17107 (Discr : Entity_Id) return Entity_Id
17113 while Present (Corresponding_Discriminant (D)) loop
17114 D := Corresponding_Discriminant (D);
17118 end Root_Corresponding_Discriminant;
17120 ------------------------------
17121 -- Search_Derivation_Levels --
17122 ------------------------------
17124 function Search_Derivation_Levels
17126 Discrim_Values : Elist_Id;
17127 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17131 Result : Node_Or_Entity_Id;
17132 Result_Entity : Node_Id;
17135 -- If inappropriate type, return Error, this happens only in
17136 -- cascaded error situations, and we want to avoid a blow up.
17138 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17142 -- Look deeper if possible. Use Stored_Constraints only for
17143 -- untagged types. For tagged types use the given constraint.
17144 -- This asymmetry needs explanation???
17146 if not Stored_Discrim_Values
17147 and then Present (Stored_Constraint (Ti))
17148 and then not Is_Tagged_Type (Ti)
17151 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17154 Td : constant Entity_Id := Etype (Ti);
17158 Result := Discriminant;
17161 if Present (Stored_Constraint (Ti)) then
17163 Search_Derivation_Levels
17164 (Td, Stored_Constraint (Ti), True);
17167 Search_Derivation_Levels
17168 (Td, Discrim_Values, Stored_Discrim_Values);
17174 -- Extra underlying places to search, if not found above. For
17175 -- concurrent types, the relevant discriminant appears in the
17176 -- corresponding record. For a type derived from a private type
17177 -- without discriminant, the full view inherits the discriminants
17178 -- of the full view of the parent.
17180 if Result = Discriminant then
17181 if Is_Concurrent_Type (Ti)
17182 and then Present (Corresponding_Record_Type (Ti))
17185 Search_Derivation_Levels (
17186 Corresponding_Record_Type (Ti),
17188 Stored_Discrim_Values);
17190 elsif Is_Private_Type (Ti)
17191 and then not Has_Discriminants (Ti)
17192 and then Present (Full_View (Ti))
17193 and then Etype (Full_View (Ti)) /= Ti
17196 Search_Derivation_Levels (
17199 Stored_Discrim_Values);
17203 -- If Result is not a (reference to a) discriminant, return it,
17204 -- otherwise set Result_Entity to the discriminant.
17206 if Nkind (Result) = N_Defining_Identifier then
17207 pragma Assert (Result = Discriminant);
17208 Result_Entity := Result;
17211 if not Denotes_Discriminant (Result) then
17215 Result_Entity := Entity (Result);
17218 -- See if this level of derivation actually has discriminants because
17219 -- tagged derivations can add them, hence the lower levels need not
17222 if not Has_Discriminants (Ti) then
17226 -- Scan Ti's discriminants for Result_Entity, and return its
17227 -- corresponding value, if any.
17229 Result_Entity := Original_Record_Component (Result_Entity);
17231 Assoc := First_Elmt (Discrim_Values);
17233 if Stored_Discrim_Values then
17234 Disc := First_Stored_Discriminant (Ti);
17236 Disc := First_Discriminant (Ti);
17239 while Present (Disc) loop
17240 pragma Assert (Present (Assoc));
17242 if Original_Record_Component (Disc) = Result_Entity then
17243 return Node (Assoc);
17248 if Stored_Discrim_Values then
17249 Next_Stored_Discriminant (Disc);
17251 Next_Discriminant (Disc);
17255 -- Could not find it
17258 end Search_Derivation_Levels;
17262 Result : Node_Or_Entity_Id;
17264 -- Start of processing for Get_Discriminant_Value
17267 -- ??? This routine is a gigantic mess and will be deleted. For the
17268 -- time being just test for the trivial case before calling recurse.
17270 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17276 D := First_Discriminant (Typ_For_Constraint);
17277 E := First_Elmt (Constraint);
17278 while Present (D) loop
17279 if Chars (D) = Chars (Discriminant) then
17283 Next_Discriminant (D);
17289 Result := Search_Derivation_Levels
17290 (Typ_For_Constraint, Constraint, False);
17292 -- ??? hack to disappear when this routine is gone
17294 if Nkind (Result) = N_Defining_Identifier then
17300 D := First_Discriminant (Typ_For_Constraint);
17301 E := First_Elmt (Constraint);
17302 while Present (D) loop
17303 if Root_Corresponding_Discriminant (D) = Discriminant then
17307 Next_Discriminant (D);
17313 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17315 end Get_Discriminant_Value;
17317 --------------------------
17318 -- Has_Range_Constraint --
17319 --------------------------
17321 function Has_Range_Constraint (N : Node_Id) return Boolean is
17322 C : constant Node_Id := Constraint (N);
17325 if Nkind (C) = N_Range_Constraint then
17328 elsif Nkind (C) = N_Digits_Constraint then
17330 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17331 or else Present (Range_Constraint (C));
17333 elsif Nkind (C) = N_Delta_Constraint then
17334 return Present (Range_Constraint (C));
17339 end Has_Range_Constraint;
17341 ------------------------
17342 -- Inherit_Components --
17343 ------------------------
17345 function Inherit_Components
17347 Parent_Base : Entity_Id;
17348 Derived_Base : Entity_Id;
17349 Is_Tagged : Boolean;
17350 Inherit_Discr : Boolean;
17351 Discs : Elist_Id) return Elist_Id
17353 Assoc_List : constant Elist_Id := New_Elmt_List;
17355 procedure Inherit_Component
17356 (Old_C : Entity_Id;
17357 Plain_Discrim : Boolean := False;
17358 Stored_Discrim : Boolean := False);
17359 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17360 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17361 -- True, Old_C is a stored discriminant. If they are both false then
17362 -- Old_C is a regular component.
17364 -----------------------
17365 -- Inherit_Component --
17366 -----------------------
17368 procedure Inherit_Component
17369 (Old_C : Entity_Id;
17370 Plain_Discrim : Boolean := False;
17371 Stored_Discrim : Boolean := False)
17373 procedure Set_Anonymous_Type (Id : Entity_Id);
17374 -- Id denotes the entity of an access discriminant or anonymous
17375 -- access component. Set the type of Id to either the same type of
17376 -- Old_C or create a new one depending on whether the parent and
17377 -- the child types are in the same scope.
17379 ------------------------
17380 -- Set_Anonymous_Type --
17381 ------------------------
17383 procedure Set_Anonymous_Type (Id : Entity_Id) is
17384 Old_Typ : constant Entity_Id := Etype (Old_C);
17387 if Scope (Parent_Base) = Scope (Derived_Base) then
17388 Set_Etype (Id, Old_Typ);
17390 -- The parent and the derived type are in two different scopes.
17391 -- Reuse the type of the original discriminant / component by
17392 -- copying it in order to preserve all attributes.
17396 Typ : constant Entity_Id := New_Copy (Old_Typ);
17399 Set_Etype (Id, Typ);
17401 -- Since we do not generate component declarations for
17402 -- inherited components, associate the itype with the
17405 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17406 Set_Scope (Typ, Derived_Base);
17409 end Set_Anonymous_Type;
17411 -- Local variables and constants
17413 New_C : constant Entity_Id := New_Copy (Old_C);
17415 Corr_Discrim : Entity_Id;
17416 Discrim : Entity_Id;
17418 -- Start of processing for Inherit_Component
17421 pragma Assert (not Is_Tagged or not Stored_Discrim);
17423 Set_Parent (New_C, Parent (Old_C));
17425 -- Regular discriminants and components must be inserted in the scope
17426 -- of the Derived_Base. Do it here.
17428 if not Stored_Discrim then
17429 Enter_Name (New_C);
17432 -- For tagged types the Original_Record_Component must point to
17433 -- whatever this field was pointing to in the parent type. This has
17434 -- already been achieved by the call to New_Copy above.
17436 if not Is_Tagged then
17437 Set_Original_Record_Component (New_C, New_C);
17440 -- Set the proper type of an access discriminant
17442 if Ekind (New_C) = E_Discriminant
17443 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17445 Set_Anonymous_Type (New_C);
17448 -- If we have inherited a component then see if its Etype contains
17449 -- references to Parent_Base discriminants. In this case, replace
17450 -- these references with the constraints given in Discs. We do not
17451 -- do this for the partial view of private types because this is
17452 -- not needed (only the components of the full view will be used
17453 -- for code generation) and cause problem. We also avoid this
17454 -- transformation in some error situations.
17456 if Ekind (New_C) = E_Component then
17458 -- Set the proper type of an anonymous access component
17460 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17461 Set_Anonymous_Type (New_C);
17463 elsif (Is_Private_Type (Derived_Base)
17464 and then not Is_Generic_Type (Derived_Base))
17465 or else (Is_Empty_Elmt_List (Discs)
17466 and then not Expander_Active)
17468 Set_Etype (New_C, Etype (Old_C));
17471 -- The current component introduces a circularity of the
17474 -- limited with Pack_2;
17475 -- package Pack_1 is
17476 -- type T_1 is tagged record
17477 -- Comp : access Pack_2.T_2;
17483 -- package Pack_2 is
17484 -- type T_2 is new Pack_1.T_1 with ...;
17489 Constrain_Component_Type
17490 (Old_C, Derived_Base, N, Parent_Base, Discs));
17494 -- In derived tagged types it is illegal to reference a non
17495 -- discriminant component in the parent type. To catch this, mark
17496 -- these components with an Ekind of E_Void. This will be reset in
17497 -- Record_Type_Definition after processing the record extension of
17498 -- the derived type.
17500 -- If the declaration is a private extension, there is no further
17501 -- record extension to process, and the components retain their
17502 -- current kind, because they are visible at this point.
17504 if Is_Tagged and then Ekind (New_C) = E_Component
17505 and then Nkind (N) /= N_Private_Extension_Declaration
17507 Set_Ekind (New_C, E_Void);
17510 if Plain_Discrim then
17511 Set_Corresponding_Discriminant (New_C, Old_C);
17512 Build_Discriminal (New_C);
17514 -- If we are explicitly inheriting a stored discriminant it will be
17515 -- completely hidden.
17517 elsif Stored_Discrim then
17518 Set_Corresponding_Discriminant (New_C, Empty);
17519 Set_Discriminal (New_C, Empty);
17520 Set_Is_Completely_Hidden (New_C);
17522 -- Set the Original_Record_Component of each discriminant in the
17523 -- derived base to point to the corresponding stored that we just
17526 Discrim := First_Discriminant (Derived_Base);
17527 while Present (Discrim) loop
17528 Corr_Discrim := Corresponding_Discriminant (Discrim);
17530 -- Corr_Discrim could be missing in an error situation
17532 if Present (Corr_Discrim)
17533 and then Original_Record_Component (Corr_Discrim) = Old_C
17535 Set_Original_Record_Component (Discrim, New_C);
17538 Next_Discriminant (Discrim);
17541 Append_Entity (New_C, Derived_Base);
17544 if not Is_Tagged then
17545 Append_Elmt (Old_C, Assoc_List);
17546 Append_Elmt (New_C, Assoc_List);
17548 end Inherit_Component;
17550 -- Variables local to Inherit_Component
17552 Loc : constant Source_Ptr := Sloc (N);
17554 Parent_Discrim : Entity_Id;
17555 Stored_Discrim : Entity_Id;
17557 Component : Entity_Id;
17559 -- Start of processing for Inherit_Components
17562 if not Is_Tagged then
17563 Append_Elmt (Parent_Base, Assoc_List);
17564 Append_Elmt (Derived_Base, Assoc_List);
17567 -- Inherit parent discriminants if needed
17569 if Inherit_Discr then
17570 Parent_Discrim := First_Discriminant (Parent_Base);
17571 while Present (Parent_Discrim) loop
17572 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17573 Next_Discriminant (Parent_Discrim);
17577 -- Create explicit stored discrims for untagged types when necessary
17579 if not Has_Unknown_Discriminants (Derived_Base)
17580 and then Has_Discriminants (Parent_Base)
17581 and then not Is_Tagged
17584 or else First_Discriminant (Parent_Base) /=
17585 First_Stored_Discriminant (Parent_Base))
17587 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
17588 while Present (Stored_Discrim) loop
17589 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
17590 Next_Stored_Discriminant (Stored_Discrim);
17594 -- See if we can apply the second transformation for derived types, as
17595 -- explained in point 6. in the comments above Build_Derived_Record_Type
17596 -- This is achieved by appending Derived_Base discriminants into Discs,
17597 -- which has the side effect of returning a non empty Discs list to the
17598 -- caller of Inherit_Components, which is what we want. This must be
17599 -- done for private derived types if there are explicit stored
17600 -- discriminants, to ensure that we can retrieve the values of the
17601 -- constraints provided in the ancestors.
17604 and then Is_Empty_Elmt_List (Discs)
17605 and then Present (First_Discriminant (Derived_Base))
17607 (not Is_Private_Type (Derived_Base)
17608 or else Is_Completely_Hidden
17609 (First_Stored_Discriminant (Derived_Base))
17610 or else Is_Generic_Type (Derived_Base))
17612 D := First_Discriminant (Derived_Base);
17613 while Present (D) loop
17614 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
17615 Next_Discriminant (D);
17619 -- Finally, inherit non-discriminant components unless they are not
17620 -- visible because defined or inherited from the full view of the
17621 -- parent. Don't inherit the _parent field of the parent type.
17623 Component := First_Entity (Parent_Base);
17624 while Present (Component) loop
17626 -- Ada 2005 (AI-251): Do not inherit components associated with
17627 -- secondary tags of the parent.
17629 if Ekind (Component) = E_Component
17630 and then Present (Related_Type (Component))
17634 elsif Ekind (Component) /= E_Component
17635 or else Chars (Component) = Name_uParent
17639 -- If the derived type is within the parent type's declarative
17640 -- region, then the components can still be inherited even though
17641 -- they aren't visible at this point. This can occur for cases
17642 -- such as within public child units where the components must
17643 -- become visible upon entering the child unit's private part.
17645 elsif not Is_Visible_Component (Component)
17646 and then not In_Open_Scopes (Scope (Parent_Base))
17650 elsif Ekind_In (Derived_Base, E_Private_Type,
17651 E_Limited_Private_Type)
17656 Inherit_Component (Component);
17659 Next_Entity (Component);
17662 -- For tagged derived types, inherited discriminants cannot be used in
17663 -- component declarations of the record extension part. To achieve this
17664 -- we mark the inherited discriminants as not visible.
17666 if Is_Tagged and then Inherit_Discr then
17667 D := First_Discriminant (Derived_Base);
17668 while Present (D) loop
17669 Set_Is_Immediately_Visible (D, False);
17670 Next_Discriminant (D);
17675 end Inherit_Components;
17677 -----------------------------
17678 -- Inherit_Predicate_Flags --
17679 -----------------------------
17681 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
17683 Set_Has_Predicates (Subt, Has_Predicates (Par));
17684 Set_Has_Static_Predicate_Aspect
17685 (Subt, Has_Static_Predicate_Aspect (Par));
17686 Set_Has_Dynamic_Predicate_Aspect
17687 (Subt, Has_Dynamic_Predicate_Aspect (Par));
17688 end Inherit_Predicate_Flags;
17690 ----------------------
17691 -- Is_EVF_Procedure --
17692 ----------------------
17694 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
17695 Formal : Entity_Id;
17698 -- Examine the formals of an Extensions_Visible False procedure looking
17699 -- for a controlling OUT parameter.
17701 if Ekind (Subp) = E_Procedure
17702 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
17704 Formal := First_Formal (Subp);
17705 while Present (Formal) loop
17706 if Ekind (Formal) = E_Out_Parameter
17707 and then Is_Controlling_Formal (Formal)
17712 Next_Formal (Formal);
17717 end Is_EVF_Procedure;
17719 -----------------------
17720 -- Is_Null_Extension --
17721 -----------------------
17723 function Is_Null_Extension (T : Entity_Id) return Boolean is
17724 Type_Decl : constant Node_Id := Parent (Base_Type (T));
17725 Comp_List : Node_Id;
17729 if Nkind (Type_Decl) /= N_Full_Type_Declaration
17730 or else not Is_Tagged_Type (T)
17731 or else Nkind (Type_Definition (Type_Decl)) /=
17732 N_Derived_Type_Definition
17733 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
17739 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
17741 if Present (Discriminant_Specifications (Type_Decl)) then
17744 elsif Present (Comp_List)
17745 and then Is_Non_Empty_List (Component_Items (Comp_List))
17747 Comp := First (Component_Items (Comp_List));
17749 -- Only user-defined components are relevant. The component list
17750 -- may also contain a parent component and internal components
17751 -- corresponding to secondary tags, but these do not determine
17752 -- whether this is a null extension.
17754 while Present (Comp) loop
17755 if Comes_From_Source (Comp) then
17767 end Is_Null_Extension;
17769 ------------------------------
17770 -- Is_Valid_Constraint_Kind --
17771 ------------------------------
17773 function Is_Valid_Constraint_Kind
17774 (T_Kind : Type_Kind;
17775 Constraint_Kind : Node_Kind) return Boolean
17779 when Enumeration_Kind |
17781 return Constraint_Kind = N_Range_Constraint;
17783 when Decimal_Fixed_Point_Kind =>
17784 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17785 N_Range_Constraint);
17787 when Ordinary_Fixed_Point_Kind =>
17788 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
17789 N_Range_Constraint);
17792 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17793 N_Range_Constraint);
17800 E_Incomplete_Type |
17803 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
17806 return True; -- Error will be detected later
17808 end Is_Valid_Constraint_Kind;
17810 --------------------------
17811 -- Is_Visible_Component --
17812 --------------------------
17814 function Is_Visible_Component
17816 N : Node_Id := Empty) return Boolean
17818 Original_Comp : Entity_Id := Empty;
17819 Original_Scope : Entity_Id;
17820 Type_Scope : Entity_Id;
17822 function Is_Local_Type (Typ : Entity_Id) return Boolean;
17823 -- Check whether parent type of inherited component is declared locally,
17824 -- possibly within a nested package or instance. The current scope is
17825 -- the derived record itself.
17827 -------------------
17828 -- Is_Local_Type --
17829 -------------------
17831 function Is_Local_Type (Typ : Entity_Id) return Boolean is
17835 Scop := Scope (Typ);
17836 while Present (Scop)
17837 and then Scop /= Standard_Standard
17839 if Scop = Scope (Current_Scope) then
17843 Scop := Scope (Scop);
17849 -- Start of processing for Is_Visible_Component
17852 if Ekind_In (C, E_Component, E_Discriminant) then
17853 Original_Comp := Original_Record_Component (C);
17856 if No (Original_Comp) then
17858 -- Premature usage, or previous error
17863 Original_Scope := Scope (Original_Comp);
17864 Type_Scope := Scope (Base_Type (Scope (C)));
17867 -- This test only concerns tagged types
17869 if not Is_Tagged_Type (Original_Scope) then
17872 -- If it is _Parent or _Tag, there is no visibility issue
17874 elsif not Comes_From_Source (Original_Comp) then
17877 -- Discriminants are visible unless the (private) type has unknown
17878 -- discriminants. If the discriminant reference is inserted for a
17879 -- discriminant check on a full view it is also visible.
17881 elsif Ekind (Original_Comp) = E_Discriminant
17883 (not Has_Unknown_Discriminants (Original_Scope)
17884 or else (Present (N)
17885 and then Nkind (N) = N_Selected_Component
17886 and then Nkind (Prefix (N)) = N_Type_Conversion
17887 and then not Comes_From_Source (Prefix (N))))
17891 -- In the body of an instantiation, no need to check for the visibility
17894 elsif In_Instance_Body then
17897 -- If the component has been declared in an ancestor which is currently
17898 -- a private type, then it is not visible. The same applies if the
17899 -- component's containing type is not in an open scope and the original
17900 -- component's enclosing type is a visible full view of a private type
17901 -- (which can occur in cases where an attempt is being made to reference
17902 -- a component in a sibling package that is inherited from a visible
17903 -- component of a type in an ancestor package; the component in the
17904 -- sibling package should not be visible even though the component it
17905 -- inherited from is visible). This does not apply however in the case
17906 -- where the scope of the type is a private child unit, or when the
17907 -- parent comes from a local package in which the ancestor is currently
17908 -- visible. The latter suppression of visibility is needed for cases
17909 -- that are tested in B730006.
17911 elsif Is_Private_Type (Original_Scope)
17913 (not Is_Private_Descendant (Type_Scope)
17914 and then not In_Open_Scopes (Type_Scope)
17915 and then Has_Private_Declaration (Original_Scope))
17917 -- If the type derives from an entity in a formal package, there
17918 -- are no additional visible components.
17920 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
17921 N_Formal_Package_Declaration
17925 -- if we are not in the private part of the current package, there
17926 -- are no additional visible components.
17928 elsif Ekind (Scope (Current_Scope)) = E_Package
17929 and then not In_Private_Part (Scope (Current_Scope))
17934 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
17935 and then In_Open_Scopes (Scope (Original_Scope))
17936 and then Is_Local_Type (Type_Scope);
17939 -- There is another weird way in which a component may be invisible when
17940 -- the private and the full view are not derived from the same ancestor.
17941 -- Here is an example :
17943 -- type A1 is tagged record F1 : integer; end record;
17944 -- type A2 is new A1 with record F2 : integer; end record;
17945 -- type T is new A1 with private;
17947 -- type T is new A2 with null record;
17949 -- In this case, the full view of T inherits F1 and F2 but the private
17950 -- view inherits only F1
17954 Ancestor : Entity_Id := Scope (C);
17958 if Ancestor = Original_Scope then
17960 elsif Ancestor = Etype (Ancestor) then
17964 Ancestor := Etype (Ancestor);
17968 end Is_Visible_Component;
17970 --------------------------
17971 -- Make_Class_Wide_Type --
17972 --------------------------
17974 procedure Make_Class_Wide_Type (T : Entity_Id) is
17975 CW_Type : Entity_Id;
17977 Next_E : Entity_Id;
17980 if Present (Class_Wide_Type (T)) then
17982 -- The class-wide type is a partially decorated entity created for a
17983 -- unanalyzed tagged type referenced through a limited with clause.
17984 -- When the tagged type is analyzed, its class-wide type needs to be
17985 -- redecorated. Note that we reuse the entity created by Decorate_
17986 -- Tagged_Type in order to preserve all links.
17988 if Materialize_Entity (Class_Wide_Type (T)) then
17989 CW_Type := Class_Wide_Type (T);
17990 Set_Materialize_Entity (CW_Type, False);
17992 -- The class wide type can have been defined by the partial view, in
17993 -- which case everything is already done.
17999 -- Default case, we need to create a new class-wide type
18003 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18006 -- Inherit root type characteristics
18008 CW_Name := Chars (CW_Type);
18009 Next_E := Next_Entity (CW_Type);
18010 Copy_Node (T, CW_Type);
18011 Set_Comes_From_Source (CW_Type, False);
18012 Set_Chars (CW_Type, CW_Name);
18013 Set_Parent (CW_Type, Parent (T));
18014 Set_Next_Entity (CW_Type, Next_E);
18016 -- Ensure we have a new freeze node for the class-wide type. The partial
18017 -- view may have freeze action of its own, requiring a proper freeze
18018 -- node, and the same freeze node cannot be shared between the two
18021 Set_Has_Delayed_Freeze (CW_Type);
18022 Set_Freeze_Node (CW_Type, Empty);
18024 -- Customize the class-wide type: It has no prim. op., it cannot be
18025 -- abstract and its Etype points back to the specific root type.
18027 Set_Ekind (CW_Type, E_Class_Wide_Type);
18028 Set_Is_Tagged_Type (CW_Type, True);
18029 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18030 Set_Is_Abstract_Type (CW_Type, False);
18031 Set_Is_Constrained (CW_Type, False);
18032 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18033 Set_Default_SSO (CW_Type);
18035 if Ekind (T) = E_Class_Wide_Subtype then
18036 Set_Etype (CW_Type, Etype (Base_Type (T)));
18038 Set_Etype (CW_Type, T);
18041 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18043 -- If this is the class_wide type of a constrained subtype, it does
18044 -- not have discriminants.
18046 Set_Has_Discriminants (CW_Type,
18047 Has_Discriminants (T) and then not Is_Constrained (T));
18049 Set_Has_Unknown_Discriminants (CW_Type, True);
18050 Set_Class_Wide_Type (T, CW_Type);
18051 Set_Equivalent_Type (CW_Type, Empty);
18053 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18055 Set_Class_Wide_Type (CW_Type, CW_Type);
18056 end Make_Class_Wide_Type;
18062 procedure Make_Index
18064 Related_Nod : Node_Id;
18065 Related_Id : Entity_Id := Empty;
18066 Suffix_Index : Nat := 1;
18067 In_Iter_Schm : Boolean := False)
18071 Def_Id : Entity_Id := Empty;
18072 Found : Boolean := False;
18075 -- For a discrete range used in a constrained array definition and
18076 -- defined by a range, an implicit conversion to the predefined type
18077 -- INTEGER is assumed if each bound is either a numeric literal, a named
18078 -- number, or an attribute, and the type of both bounds (prior to the
18079 -- implicit conversion) is the type universal_integer. Otherwise, both
18080 -- bounds must be of the same discrete type, other than universal
18081 -- integer; this type must be determinable independently of the
18082 -- context, but using the fact that the type must be discrete and that
18083 -- both bounds must have the same type.
18085 -- Character literals also have a universal type in the absence of
18086 -- of additional context, and are resolved to Standard_Character.
18088 if Nkind (N) = N_Range then
18090 -- The index is given by a range constraint. The bounds are known
18091 -- to be of a consistent type.
18093 if not Is_Overloaded (N) then
18096 -- For universal bounds, choose the specific predefined type
18098 if T = Universal_Integer then
18099 T := Standard_Integer;
18101 elsif T = Any_Character then
18102 Ambiguous_Character (Low_Bound (N));
18104 T := Standard_Character;
18107 -- The node may be overloaded because some user-defined operators
18108 -- are available, but if a universal interpretation exists it is
18109 -- also the selected one.
18111 elsif Universal_Interpretation (N) = Universal_Integer then
18112 T := Standard_Integer;
18118 Ind : Interp_Index;
18122 Get_First_Interp (N, Ind, It);
18123 while Present (It.Typ) loop
18124 if Is_Discrete_Type (It.Typ) then
18127 and then not Covers (It.Typ, T)
18128 and then not Covers (T, It.Typ)
18130 Error_Msg_N ("ambiguous bounds in discrete range", N);
18138 Get_Next_Interp (Ind, It);
18141 if T = Any_Type then
18142 Error_Msg_N ("discrete type required for range", N);
18143 Set_Etype (N, Any_Type);
18146 elsif T = Universal_Integer then
18147 T := Standard_Integer;
18152 if not Is_Discrete_Type (T) then
18153 Error_Msg_N ("discrete type required for range", N);
18154 Set_Etype (N, Any_Type);
18158 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18159 and then Attribute_Name (Low_Bound (N)) = Name_First
18160 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18161 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18162 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18164 -- The type of the index will be the type of the prefix, as long
18165 -- as the upper bound is 'Last of the same type.
18167 Def_Id := Entity (Prefix (Low_Bound (N)));
18169 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18170 or else Attribute_Name (High_Bound (N)) /= Name_Last
18171 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18172 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18179 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18181 elsif Nkind (N) = N_Subtype_Indication then
18183 -- The index is given by a subtype with a range constraint
18185 T := Base_Type (Entity (Subtype_Mark (N)));
18187 if not Is_Discrete_Type (T) then
18188 Error_Msg_N ("discrete type required for range", N);
18189 Set_Etype (N, Any_Type);
18193 R := Range_Expression (Constraint (N));
18196 Process_Range_Expr_In_Decl
18197 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18199 elsif Nkind (N) = N_Attribute_Reference then
18201 -- Catch beginner's error (use of attribute other than 'Range)
18203 if Attribute_Name (N) /= Name_Range then
18204 Error_Msg_N ("expect attribute ''Range", N);
18205 Set_Etype (N, Any_Type);
18209 -- If the node denotes the range of a type mark, that is also the
18210 -- resulting type, and we do not need to create an Itype for it.
18212 if Is_Entity_Name (Prefix (N))
18213 and then Comes_From_Source (N)
18214 and then Is_Type (Entity (Prefix (N)))
18215 and then Is_Discrete_Type (Entity (Prefix (N)))
18217 Def_Id := Entity (Prefix (N));
18220 Analyze_And_Resolve (N);
18224 -- If none of the above, must be a subtype. We convert this to a
18225 -- range attribute reference because in the case of declared first
18226 -- named subtypes, the types in the range reference can be different
18227 -- from the type of the entity. A range attribute normalizes the
18228 -- reference and obtains the correct types for the bounds.
18230 -- This transformation is in the nature of an expansion, is only
18231 -- done if expansion is active. In particular, it is not done on
18232 -- formal generic types, because we need to retain the name of the
18233 -- original index for instantiation purposes.
18236 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18237 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18238 Set_Etype (N, Any_Integer);
18242 -- The type mark may be that of an incomplete type. It is only
18243 -- now that we can get the full view, previous analysis does
18244 -- not look specifically for a type mark.
18246 Set_Entity (N, Get_Full_View (Entity (N)));
18247 Set_Etype (N, Entity (N));
18248 Def_Id := Entity (N);
18250 if not Is_Discrete_Type (Def_Id) then
18251 Error_Msg_N ("discrete type required for index", N);
18252 Set_Etype (N, Any_Type);
18257 if Expander_Active then
18259 Make_Attribute_Reference (Sloc (N),
18260 Attribute_Name => Name_Range,
18261 Prefix => Relocate_Node (N)));
18263 -- The original was a subtype mark that does not freeze. This
18264 -- means that the rewritten version must not freeze either.
18266 Set_Must_Not_Freeze (N);
18267 Set_Must_Not_Freeze (Prefix (N));
18268 Analyze_And_Resolve (N);
18272 -- If expander is inactive, type is legal, nothing else to construct
18279 if not Is_Discrete_Type (T) then
18280 Error_Msg_N ("discrete type required for range", N);
18281 Set_Etype (N, Any_Type);
18284 elsif T = Any_Type then
18285 Set_Etype (N, Any_Type);
18289 -- We will now create the appropriate Itype to describe the range, but
18290 -- first a check. If we originally had a subtype, then we just label
18291 -- the range with this subtype. Not only is there no need to construct
18292 -- a new subtype, but it is wrong to do so for two reasons:
18294 -- 1. A legality concern, if we have a subtype, it must not freeze,
18295 -- and the Itype would cause freezing incorrectly
18297 -- 2. An efficiency concern, if we created an Itype, it would not be
18298 -- recognized as the same type for the purposes of eliminating
18299 -- checks in some circumstances.
18301 -- We signal this case by setting the subtype entity in Def_Id
18303 if No (Def_Id) then
18305 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18306 Set_Etype (Def_Id, Base_Type (T));
18308 if Is_Signed_Integer_Type (T) then
18309 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18311 elsif Is_Modular_Integer_Type (T) then
18312 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18315 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18316 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18317 Set_First_Literal (Def_Id, First_Literal (T));
18320 Set_Size_Info (Def_Id, (T));
18321 Set_RM_Size (Def_Id, RM_Size (T));
18322 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18324 Set_Scalar_Range (Def_Id, R);
18325 Conditional_Delay (Def_Id, T);
18327 if Nkind (N) = N_Subtype_Indication then
18328 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18331 -- In the subtype indication case, if the immediate parent of the
18332 -- new subtype is non-static, then the subtype we create is non-
18333 -- static, even if its bounds are static.
18335 if Nkind (N) = N_Subtype_Indication
18336 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18338 Set_Is_Non_Static_Subtype (Def_Id);
18342 -- Final step is to label the index with this constructed type
18344 Set_Etype (N, Def_Id);
18347 ------------------------------
18348 -- Modular_Type_Declaration --
18349 ------------------------------
18351 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18352 Mod_Expr : constant Node_Id := Expression (Def);
18355 procedure Set_Modular_Size (Bits : Int);
18356 -- Sets RM_Size to Bits, and Esize to normal word size above this
18358 ----------------------
18359 -- Set_Modular_Size --
18360 ----------------------
18362 procedure Set_Modular_Size (Bits : Int) is
18364 Set_RM_Size (T, UI_From_Int (Bits));
18369 elsif Bits <= 16 then
18370 Init_Esize (T, 16);
18372 elsif Bits <= 32 then
18373 Init_Esize (T, 32);
18376 Init_Esize (T, System_Max_Binary_Modulus_Power);
18379 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18380 Set_Is_Known_Valid (T);
18382 end Set_Modular_Size;
18384 -- Start of processing for Modular_Type_Declaration
18387 -- If the mod expression is (exactly) 2 * literal, where literal is
18388 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18390 if Warn_On_Suspicious_Modulus_Value
18391 and then Nkind (Mod_Expr) = N_Op_Multiply
18392 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18393 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18394 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18395 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18398 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18401 -- Proceed with analysis of mod expression
18403 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18405 Set_Ekind (T, E_Modular_Integer_Type);
18406 Init_Alignment (T);
18407 Set_Is_Constrained (T);
18409 if not Is_OK_Static_Expression (Mod_Expr) then
18410 Flag_Non_Static_Expr
18411 ("non-static expression used for modular type bound!", Mod_Expr);
18412 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18414 M_Val := Expr_Value (Mod_Expr);
18418 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18419 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18422 if M_Val > 2 ** Standard_Long_Integer_Size then
18423 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18426 Set_Modulus (T, M_Val);
18428 -- Create bounds for the modular type based on the modulus given in
18429 -- the type declaration and then analyze and resolve those bounds.
18431 Set_Scalar_Range (T,
18432 Make_Range (Sloc (Mod_Expr),
18433 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18434 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18436 -- Properly analyze the literals for the range. We do this manually
18437 -- because we can't go calling Resolve, since we are resolving these
18438 -- bounds with the type, and this type is certainly not complete yet.
18440 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18441 Set_Etype (High_Bound (Scalar_Range (T)), T);
18442 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18443 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18445 -- Loop through powers of two to find number of bits required
18447 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18451 if M_Val = 2 ** Bits then
18452 Set_Modular_Size (Bits);
18457 elsif M_Val < 2 ** Bits then
18458 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18459 Set_Non_Binary_Modulus (T);
18461 if Bits > System_Max_Nonbinary_Modulus_Power then
18462 Error_Msg_Uint_1 :=
18463 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18465 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18466 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18470 -- In the non-binary case, set size as per RM 13.3(55)
18472 Set_Modular_Size (Bits);
18479 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18480 -- so we just signal an error and set the maximum size.
18482 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18483 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18485 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18486 Init_Alignment (T);
18488 end Modular_Type_Declaration;
18490 --------------------------
18491 -- New_Concatenation_Op --
18492 --------------------------
18494 procedure New_Concatenation_Op (Typ : Entity_Id) is
18495 Loc : constant Source_Ptr := Sloc (Typ);
18498 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18499 -- Create abbreviated declaration for the formal of a predefined
18500 -- Operator 'Op' of type 'Typ'
18502 --------------------
18503 -- Make_Op_Formal --
18504 --------------------
18506 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18507 Formal : Entity_Id;
18509 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18510 Set_Etype (Formal, Typ);
18511 Set_Mechanism (Formal, Default_Mechanism);
18513 end Make_Op_Formal;
18515 -- Start of processing for New_Concatenation_Op
18518 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18520 Set_Ekind (Op, E_Operator);
18521 Set_Scope (Op, Current_Scope);
18522 Set_Etype (Op, Typ);
18523 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18524 Set_Is_Immediately_Visible (Op);
18525 Set_Is_Intrinsic_Subprogram (Op);
18526 Set_Has_Completion (Op);
18527 Append_Entity (Op, Current_Scope);
18529 Set_Name_Entity_Id (Name_Op_Concat, Op);
18531 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18532 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18533 end New_Concatenation_Op;
18535 -------------------------
18536 -- OK_For_Limited_Init --
18537 -------------------------
18539 -- ???Check all calls of this, and compare the conditions under which it's
18542 function OK_For_Limited_Init
18544 Exp : Node_Id) return Boolean
18547 return Is_CPP_Constructor_Call (Exp)
18548 or else (Ada_Version >= Ada_2005
18549 and then not Debug_Flag_Dot_L
18550 and then OK_For_Limited_Init_In_05 (Typ, Exp));
18551 end OK_For_Limited_Init;
18553 -------------------------------
18554 -- OK_For_Limited_Init_In_05 --
18555 -------------------------------
18557 function OK_For_Limited_Init_In_05
18559 Exp : Node_Id) return Boolean
18562 -- An object of a limited interface type can be initialized with any
18563 -- expression of a nonlimited descendant type.
18565 if Is_Class_Wide_Type (Typ)
18566 and then Is_Limited_Interface (Typ)
18567 and then not Is_Limited_Type (Etype (Exp))
18572 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
18573 -- case of limited aggregates (including extension aggregates), and
18574 -- function calls. The function call may have been given in prefixed
18575 -- notation, in which case the original node is an indexed component.
18576 -- If the function is parameterless, the original node was an explicit
18577 -- dereference. The function may also be parameterless, in which case
18578 -- the source node is just an identifier.
18580 case Nkind (Original_Node (Exp)) is
18581 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
18584 when N_Identifier =>
18585 return Present (Entity (Original_Node (Exp)))
18586 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
18588 when N_Qualified_Expression =>
18590 OK_For_Limited_Init_In_05
18591 (Typ, Expression (Original_Node (Exp)));
18593 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
18594 -- with a function call, the expander has rewritten the call into an
18595 -- N_Type_Conversion node to force displacement of the pointer to
18596 -- reference the component containing the secondary dispatch table.
18597 -- Otherwise a type conversion is not a legal context.
18598 -- A return statement for a build-in-place function returning a
18599 -- synchronized type also introduces an unchecked conversion.
18601 when N_Type_Conversion |
18602 N_Unchecked_Type_Conversion =>
18603 return not Comes_From_Source (Exp)
18605 OK_For_Limited_Init_In_05
18606 (Typ, Expression (Original_Node (Exp)));
18608 when N_Indexed_Component |
18609 N_Selected_Component |
18610 N_Explicit_Dereference =>
18611 return Nkind (Exp) = N_Function_Call;
18613 -- A use of 'Input is a function call, hence allowed. Normally the
18614 -- attribute will be changed to a call, but the attribute by itself
18615 -- can occur with -gnatc.
18617 when N_Attribute_Reference =>
18618 return Attribute_Name (Original_Node (Exp)) = Name_Input;
18620 -- For a case expression, all dependent expressions must be legal
18622 when N_Case_Expression =>
18627 Alt := First (Alternatives (Original_Node (Exp)));
18628 while Present (Alt) loop
18629 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
18639 -- For an if expression, all dependent expressions must be legal
18641 when N_If_Expression =>
18643 Then_Expr : constant Node_Id :=
18644 Next (First (Expressions (Original_Node (Exp))));
18645 Else_Expr : constant Node_Id := Next (Then_Expr);
18647 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
18649 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
18655 end OK_For_Limited_Init_In_05;
18657 -------------------------------------------
18658 -- Ordinary_Fixed_Point_Type_Declaration --
18659 -------------------------------------------
18661 procedure Ordinary_Fixed_Point_Type_Declaration
18665 Loc : constant Source_Ptr := Sloc (Def);
18666 Delta_Expr : constant Node_Id := Delta_Expression (Def);
18667 RRS : constant Node_Id := Real_Range_Specification (Def);
18668 Implicit_Base : Entity_Id;
18675 Check_Restriction (No_Fixed_Point, Def);
18677 -- Create implicit base type
18680 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
18681 Set_Etype (Implicit_Base, Implicit_Base);
18683 -- Analyze and process delta expression
18685 Analyze_And_Resolve (Delta_Expr, Any_Real);
18687 Check_Delta_Expression (Delta_Expr);
18688 Delta_Val := Expr_Value_R (Delta_Expr);
18690 Set_Delta_Value (Implicit_Base, Delta_Val);
18692 -- Compute default small from given delta, which is the largest power
18693 -- of two that does not exceed the given delta value.
18703 if Delta_Val < Ureal_1 then
18704 while Delta_Val < Tmp loop
18705 Tmp := Tmp / Ureal_2;
18706 Scale := Scale + 1;
18711 Tmp := Tmp * Ureal_2;
18712 exit when Tmp > Delta_Val;
18713 Scale := Scale - 1;
18717 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
18720 Set_Small_Value (Implicit_Base, Small_Val);
18722 -- If no range was given, set a dummy range
18724 if RRS <= Empty_Or_Error then
18725 Low_Val := -Small_Val;
18726 High_Val := Small_Val;
18728 -- Otherwise analyze and process given range
18732 Low : constant Node_Id := Low_Bound (RRS);
18733 High : constant Node_Id := High_Bound (RRS);
18736 Analyze_And_Resolve (Low, Any_Real);
18737 Analyze_And_Resolve (High, Any_Real);
18738 Check_Real_Bound (Low);
18739 Check_Real_Bound (High);
18741 -- Obtain and set the range
18743 Low_Val := Expr_Value_R (Low);
18744 High_Val := Expr_Value_R (High);
18746 if Low_Val > High_Val then
18747 Error_Msg_NE ("??fixed point type& has null range", Def, T);
18752 -- The range for both the implicit base and the declared first subtype
18753 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
18754 -- set a temporary range in place. Note that the bounds of the base
18755 -- type will be widened to be symmetrical and to fill the available
18756 -- bits when the type is frozen.
18758 -- We could do this with all discrete types, and probably should, but
18759 -- we absolutely have to do it for fixed-point, since the end-points
18760 -- of the range and the size are determined by the small value, which
18761 -- could be reset before the freeze point.
18763 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
18764 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
18766 -- Complete definition of first subtype. The inheritance of the rep item
18767 -- chain ensures that SPARK-related pragmas are not clobbered when the
18768 -- ordinary fixed point type acts as a full view of a private type.
18770 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
18771 Set_Etype (T, Implicit_Base);
18772 Init_Size_Align (T);
18773 Inherit_Rep_Item_Chain (T, Implicit_Base);
18774 Set_Small_Value (T, Small_Val);
18775 Set_Delta_Value (T, Delta_Val);
18776 Set_Is_Constrained (T);
18777 end Ordinary_Fixed_Point_Type_Declaration;
18779 ----------------------------------
18780 -- Preanalyze_Assert_Expression --
18781 ----------------------------------
18783 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
18785 In_Assertion_Expr := In_Assertion_Expr + 1;
18786 Preanalyze_Spec_Expression (N, T);
18787 In_Assertion_Expr := In_Assertion_Expr - 1;
18788 end Preanalyze_Assert_Expression;
18790 -----------------------------------
18791 -- Preanalyze_Default_Expression --
18792 -----------------------------------
18794 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
18795 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
18797 In_Default_Expr := True;
18798 Preanalyze_Spec_Expression (N, T);
18799 In_Default_Expr := Save_In_Default_Expr;
18800 end Preanalyze_Default_Expression;
18802 --------------------------------
18803 -- Preanalyze_Spec_Expression --
18804 --------------------------------
18806 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18807 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18809 In_Spec_Expression := True;
18810 Preanalyze_And_Resolve (N, T);
18811 In_Spec_Expression := Save_In_Spec_Expression;
18812 end Preanalyze_Spec_Expression;
18814 ----------------------------------------
18815 -- Prepare_Private_Subtype_Completion --
18816 ----------------------------------------
18818 procedure Prepare_Private_Subtype_Completion
18820 Related_Nod : Node_Id)
18822 Id_B : constant Entity_Id := Base_Type (Id);
18823 Full_B : Entity_Id := Full_View (Id_B);
18827 if Present (Full_B) then
18829 -- Get to the underlying full view if necessary
18831 if Is_Private_Type (Full_B)
18832 and then Present (Underlying_Full_View (Full_B))
18834 Full_B := Underlying_Full_View (Full_B);
18837 -- The Base_Type is already completed, we can complete the subtype
18838 -- now. We have to create a new entity with the same name, Thus we
18839 -- can't use Create_Itype.
18841 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
18842 Set_Is_Itype (Full);
18843 Set_Associated_Node_For_Itype (Full, Related_Nod);
18844 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
18847 -- The parent subtype may be private, but the base might not, in some
18848 -- nested instances. In that case, the subtype does not need to be
18849 -- exchanged. It would still be nice to make private subtypes and their
18850 -- bases consistent at all times ???
18852 if Is_Private_Type (Id_B) then
18853 Append_Elmt (Id, Private_Dependents (Id_B));
18855 end Prepare_Private_Subtype_Completion;
18857 ---------------------------
18858 -- Process_Discriminants --
18859 ---------------------------
18861 procedure Process_Discriminants
18863 Prev : Entity_Id := Empty)
18865 Elist : constant Elist_Id := New_Elmt_List;
18868 Discr_Number : Uint;
18869 Discr_Type : Entity_Id;
18870 Default_Present : Boolean := False;
18871 Default_Not_Present : Boolean := False;
18874 -- A composite type other than an array type can have discriminants.
18875 -- On entry, the current scope is the composite type.
18877 -- The discriminants are initially entered into the scope of the type
18878 -- via Enter_Name with the default Ekind of E_Void to prevent premature
18879 -- use, as explained at the end of this procedure.
18881 Discr := First (Discriminant_Specifications (N));
18882 while Present (Discr) loop
18883 Enter_Name (Defining_Identifier (Discr));
18885 -- For navigation purposes we add a reference to the discriminant
18886 -- in the entity for the type. If the current declaration is a
18887 -- completion, place references on the partial view. Otherwise the
18888 -- type is the current scope.
18890 if Present (Prev) then
18892 -- The references go on the partial view, if present. If the
18893 -- partial view has discriminants, the references have been
18894 -- generated already.
18896 if not Has_Discriminants (Prev) then
18897 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
18901 (Current_Scope, Defining_Identifier (Discr), 'd');
18904 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
18905 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
18907 -- Ada 2005 (AI-254)
18909 if Present (Access_To_Subprogram_Definition
18910 (Discriminant_Type (Discr)))
18911 and then Protected_Present (Access_To_Subprogram_Definition
18912 (Discriminant_Type (Discr)))
18915 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
18919 Find_Type (Discriminant_Type (Discr));
18920 Discr_Type := Etype (Discriminant_Type (Discr));
18922 if Error_Posted (Discriminant_Type (Discr)) then
18923 Discr_Type := Any_Type;
18927 -- Handling of discriminants that are access types
18929 if Is_Access_Type (Discr_Type) then
18931 -- Ada 2005 (AI-230): Access discriminant allowed in non-
18932 -- limited record types
18934 if Ada_Version < Ada_2005 then
18935 Check_Access_Discriminant_Requires_Limited
18936 (Discr, Discriminant_Type (Discr));
18939 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
18941 ("(Ada 83) access discriminant not allowed", Discr);
18944 -- If not access type, must be a discrete type
18946 elsif not Is_Discrete_Type (Discr_Type) then
18948 ("discriminants must have a discrete or access type",
18949 Discriminant_Type (Discr));
18952 Set_Etype (Defining_Identifier (Discr), Discr_Type);
18954 -- If a discriminant specification includes the assignment compound
18955 -- delimiter followed by an expression, the expression is the default
18956 -- expression of the discriminant; the default expression must be of
18957 -- the type of the discriminant. (RM 3.7.1) Since this expression is
18958 -- a default expression, we do the special preanalysis, since this
18959 -- expression does not freeze (see section "Handling of Default and
18960 -- Per-Object Expressions" in spec of package Sem).
18962 if Present (Expression (Discr)) then
18963 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
18967 if Nkind (N) = N_Formal_Type_Declaration then
18969 ("discriminant defaults not allowed for formal type",
18970 Expression (Discr));
18972 -- Flag an error for a tagged type with defaulted discriminants,
18973 -- excluding limited tagged types when compiling for Ada 2012
18974 -- (see AI05-0214).
18976 elsif Is_Tagged_Type (Current_Scope)
18977 and then (not Is_Limited_Type (Current_Scope)
18978 or else Ada_Version < Ada_2012)
18979 and then Comes_From_Source (N)
18981 -- Note: see similar test in Check_Or_Process_Discriminants, to
18982 -- handle the (illegal) case of the completion of an untagged
18983 -- view with discriminants with defaults by a tagged full view.
18984 -- We skip the check if Discr does not come from source, to
18985 -- account for the case of an untagged derived type providing
18986 -- defaults for a renamed discriminant from a private untagged
18987 -- ancestor with a tagged full view (ACATS B460006).
18989 if Ada_Version >= Ada_2012 then
18991 ("discriminants of nonlimited tagged type cannot have"
18993 Expression (Discr));
18996 ("discriminants of tagged type cannot have defaults",
18997 Expression (Discr));
19001 Default_Present := True;
19002 Append_Elmt (Expression (Discr), Elist);
19004 -- Tag the defining identifiers for the discriminants with
19005 -- their corresponding default expressions from the tree.
19007 Set_Discriminant_Default_Value
19008 (Defining_Identifier (Discr), Expression (Discr));
19011 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19012 -- gets set unless we can be sure that no range check is required.
19014 if (GNATprove_Mode or not Expander_Active)
19017 (Expression (Discr), Discr_Type, Assume_Valid => True)
19019 Set_Do_Range_Check (Expression (Discr));
19022 -- No default discriminant value given
19025 Default_Not_Present := True;
19028 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19029 -- Discr_Type but with the null-exclusion attribute
19031 if Ada_Version >= Ada_2005 then
19033 -- Ada 2005 (AI-231): Static checks
19035 if Can_Never_Be_Null (Discr_Type) then
19036 Null_Exclusion_Static_Checks (Discr);
19038 elsif Is_Access_Type (Discr_Type)
19039 and then Null_Exclusion_Present (Discr)
19041 -- No need to check itypes because in their case this check
19042 -- was done at their point of creation
19044 and then not Is_Itype (Discr_Type)
19046 if Can_Never_Be_Null (Discr_Type) then
19048 ("`NOT NULL` not allowed (& already excludes null)",
19053 Set_Etype (Defining_Identifier (Discr),
19054 Create_Null_Excluding_Itype
19056 Related_Nod => Discr));
19058 -- Check for improper null exclusion if the type is otherwise
19059 -- legal for a discriminant.
19061 elsif Null_Exclusion_Present (Discr)
19062 and then Is_Discrete_Type (Discr_Type)
19065 ("null exclusion can only apply to an access type", Discr);
19068 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19069 -- can't have defaults. Synchronized types, or types that are
19070 -- explicitly limited are fine, but special tests apply to derived
19071 -- types in generics: in a generic body we have to assume the
19072 -- worst, and therefore defaults are not allowed if the parent is
19073 -- a generic formal private type (see ACATS B370001).
19075 if Is_Access_Type (Discr_Type) and then Default_Present then
19076 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19077 or else Is_Limited_Record (Current_Scope)
19078 or else Is_Concurrent_Type (Current_Scope)
19079 or else Is_Concurrent_Record_Type (Current_Scope)
19080 or else Ekind (Current_Scope) = E_Limited_Private_Type
19082 if not Is_Derived_Type (Current_Scope)
19083 or else not Is_Generic_Type (Etype (Current_Scope))
19084 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19085 or else Limited_Present
19086 (Type_Definition (Parent (Current_Scope)))
19092 ("access discriminants of nonlimited types cannot "
19093 & "have defaults", Expression (Discr));
19096 elsif Present (Expression (Discr)) then
19098 ("(Ada 2005) access discriminants of nonlimited types "
19099 & "cannot have defaults", Expression (Discr));
19104 -- A discriminant cannot be effectively volatile. This check is only
19105 -- relevant when SPARK_Mode is on as it is not standard Ada legality
19106 -- rule (SPARK RM 7.1.3(6)).
19109 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19111 Error_Msg_N ("discriminant cannot be volatile", Discr);
19117 -- An element list consisting of the default expressions of the
19118 -- discriminants is constructed in the above loop and used to set
19119 -- the Discriminant_Constraint attribute for the type. If an object
19120 -- is declared of this (record or task) type without any explicit
19121 -- discriminant constraint given, this element list will form the
19122 -- actual parameters for the corresponding initialization procedure
19125 Set_Discriminant_Constraint (Current_Scope, Elist);
19126 Set_Stored_Constraint (Current_Scope, No_Elist);
19128 -- Default expressions must be provided either for all or for none
19129 -- of the discriminants of a discriminant part. (RM 3.7.1)
19131 if Default_Present and then Default_Not_Present then
19133 ("incomplete specification of defaults for discriminants", N);
19136 -- The use of the name of a discriminant is not allowed in default
19137 -- expressions of a discriminant part if the specification of the
19138 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19140 -- To detect this, the discriminant names are entered initially with an
19141 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19142 -- attempt to use a void entity (for example in an expression that is
19143 -- type-checked) produces the error message: premature usage. Now after
19144 -- completing the semantic analysis of the discriminant part, we can set
19145 -- the Ekind of all the discriminants appropriately.
19147 Discr := First (Discriminant_Specifications (N));
19148 Discr_Number := Uint_1;
19149 while Present (Discr) loop
19150 Id := Defining_Identifier (Discr);
19151 Set_Ekind (Id, E_Discriminant);
19152 Init_Component_Location (Id);
19154 Set_Discriminant_Number (Id, Discr_Number);
19156 -- Make sure this is always set, even in illegal programs
19158 Set_Corresponding_Discriminant (Id, Empty);
19160 -- Initialize the Original_Record_Component to the entity itself.
19161 -- Inherit_Components will propagate the right value to
19162 -- discriminants in derived record types.
19164 Set_Original_Record_Component (Id, Id);
19166 -- Create the discriminal for the discriminant
19168 Build_Discriminal (Id);
19171 Discr_Number := Discr_Number + 1;
19174 Set_Has_Discriminants (Current_Scope);
19175 end Process_Discriminants;
19177 -----------------------
19178 -- Process_Full_View --
19179 -----------------------
19181 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19182 procedure Collect_Implemented_Interfaces
19184 Ifaces : Elist_Id);
19185 -- Ada 2005: Gather all the interfaces that Typ directly or
19186 -- inherently implements. Duplicate entries are not added to
19187 -- the list Ifaces.
19189 ------------------------------------
19190 -- Collect_Implemented_Interfaces --
19191 ------------------------------------
19193 procedure Collect_Implemented_Interfaces
19198 Iface_Elmt : Elmt_Id;
19201 -- Abstract interfaces are only associated with tagged record types
19203 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19207 -- Recursively climb to the ancestors
19209 if Etype (Typ) /= Typ
19211 -- Protect the frontend against wrong cyclic declarations like:
19213 -- type B is new A with private;
19214 -- type C is new A with private;
19216 -- type B is new C with null record;
19217 -- type C is new B with null record;
19219 and then Etype (Typ) /= Priv_T
19220 and then Etype (Typ) /= Full_T
19222 -- Keep separate the management of private type declarations
19224 if Ekind (Typ) = E_Record_Type_With_Private then
19226 -- Handle the following illegal usage:
19227 -- type Private_Type is tagged private;
19229 -- type Private_Type is new Type_Implementing_Iface;
19231 if Present (Full_View (Typ))
19232 and then Etype (Typ) /= Full_View (Typ)
19234 if Is_Interface (Etype (Typ)) then
19235 Append_Unique_Elmt (Etype (Typ), Ifaces);
19238 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19241 -- Non-private types
19244 if Is_Interface (Etype (Typ)) then
19245 Append_Unique_Elmt (Etype (Typ), Ifaces);
19248 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19252 -- Handle entities in the list of abstract interfaces
19254 if Present (Interfaces (Typ)) then
19255 Iface_Elmt := First_Elmt (Interfaces (Typ));
19256 while Present (Iface_Elmt) loop
19257 Iface := Node (Iface_Elmt);
19259 pragma Assert (Is_Interface (Iface));
19261 if not Contain_Interface (Iface, Ifaces) then
19262 Append_Elmt (Iface, Ifaces);
19263 Collect_Implemented_Interfaces (Iface, Ifaces);
19266 Next_Elmt (Iface_Elmt);
19269 end Collect_Implemented_Interfaces;
19273 Full_Indic : Node_Id;
19274 Full_Parent : Entity_Id;
19275 Priv_Parent : Entity_Id;
19277 -- Start of processing for Process_Full_View
19280 -- First some sanity checks that must be done after semantic
19281 -- decoration of the full view and thus cannot be placed with other
19282 -- similar checks in Find_Type_Name
19284 if not Is_Limited_Type (Priv_T)
19285 and then (Is_Limited_Type (Full_T)
19286 or else Is_Limited_Composite (Full_T))
19288 if In_Instance then
19292 ("completion of nonlimited type cannot be limited", Full_T);
19293 Explain_Limited_Type (Full_T, Full_T);
19296 elsif Is_Abstract_Type (Full_T)
19297 and then not Is_Abstract_Type (Priv_T)
19300 ("completion of nonabstract type cannot be abstract", Full_T);
19302 elsif Is_Tagged_Type (Priv_T)
19303 and then Is_Limited_Type (Priv_T)
19304 and then not Is_Limited_Type (Full_T)
19306 -- If pragma CPP_Class was applied to the private declaration
19307 -- propagate the limitedness to the full-view
19309 if Is_CPP_Class (Priv_T) then
19310 Set_Is_Limited_Record (Full_T);
19312 -- GNAT allow its own definition of Limited_Controlled to disobey
19313 -- this rule in order in ease the implementation. This test is safe
19314 -- because Root_Controlled is defined in a child of System that
19315 -- normal programs are not supposed to use.
19317 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19318 Set_Is_Limited_Composite (Full_T);
19321 ("completion of limited tagged type must be limited", Full_T);
19324 elsif Is_Generic_Type (Priv_T) then
19325 Error_Msg_N ("generic type cannot have a completion", Full_T);
19328 -- Check that ancestor interfaces of private and full views are
19329 -- consistent. We omit this check for synchronized types because
19330 -- they are performed on the corresponding record type when frozen.
19332 if Ada_Version >= Ada_2005
19333 and then Is_Tagged_Type (Priv_T)
19334 and then Is_Tagged_Type (Full_T)
19335 and then not Is_Concurrent_Type (Full_T)
19339 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19340 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19343 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19344 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19346 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19347 -- an interface type if and only if the full type is descendant
19348 -- of the interface type (AARM 7.3 (7.3/2)).
19350 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19352 if Present (Iface) then
19354 ("interface in partial view& not implemented by full type "
19355 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19358 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19360 if Present (Iface) then
19362 ("interface & not implemented by partial view "
19363 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19368 if Is_Tagged_Type (Priv_T)
19369 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19370 and then Is_Derived_Type (Full_T)
19372 Priv_Parent := Etype (Priv_T);
19374 -- The full view of a private extension may have been transformed
19375 -- into an unconstrained derived type declaration and a subtype
19376 -- declaration (see build_derived_record_type for details).
19378 if Nkind (N) = N_Subtype_Declaration then
19379 Full_Indic := Subtype_Indication (N);
19380 Full_Parent := Etype (Base_Type (Full_T));
19382 Full_Indic := Subtype_Indication (Type_Definition (N));
19383 Full_Parent := Etype (Full_T);
19386 -- Check that the parent type of the full type is a descendant of
19387 -- the ancestor subtype given in the private extension. If either
19388 -- entity has an Etype equal to Any_Type then we had some previous
19389 -- error situation [7.3(8)].
19391 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19394 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19395 -- any order. Therefore we don't have to check that its parent must
19396 -- be a descendant of the parent of the private type declaration.
19398 elsif Is_Interface (Priv_Parent)
19399 and then Is_Interface (Full_Parent)
19403 -- Ada 2005 (AI-251): If the parent of the private type declaration
19404 -- is an interface there is no need to check that it is an ancestor
19405 -- of the associated full type declaration. The required tests for
19406 -- this case are performed by Build_Derived_Record_Type.
19408 elsif not Is_Interface (Base_Type (Priv_Parent))
19409 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19412 ("parent of full type must descend from parent"
19413 & " of private extension", Full_Indic);
19415 -- First check a formal restriction, and then proceed with checking
19416 -- Ada rules. Since the formal restriction is not a serious error, we
19417 -- don't prevent further error detection for this check, hence the
19421 -- In formal mode, when completing a private extension the type
19422 -- named in the private part must be exactly the same as that
19423 -- named in the visible part.
19425 if Priv_Parent /= Full_Parent then
19426 Error_Msg_Name_1 := Chars (Priv_Parent);
19427 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19430 -- Check the rules of 7.3(10): if the private extension inherits
19431 -- known discriminants, then the full type must also inherit those
19432 -- discriminants from the same (ancestor) type, and the parent
19433 -- subtype of the full type must be constrained if and only if
19434 -- the ancestor subtype of the private extension is constrained.
19436 if No (Discriminant_Specifications (Parent (Priv_T)))
19437 and then not Has_Unknown_Discriminants (Priv_T)
19438 and then Has_Discriminants (Base_Type (Priv_Parent))
19441 Priv_Indic : constant Node_Id :=
19442 Subtype_Indication (Parent (Priv_T));
19444 Priv_Constr : constant Boolean :=
19445 Is_Constrained (Priv_Parent)
19447 Nkind (Priv_Indic) = N_Subtype_Indication
19449 Is_Constrained (Entity (Priv_Indic));
19451 Full_Constr : constant Boolean :=
19452 Is_Constrained (Full_Parent)
19454 Nkind (Full_Indic) = N_Subtype_Indication
19456 Is_Constrained (Entity (Full_Indic));
19458 Priv_Discr : Entity_Id;
19459 Full_Discr : Entity_Id;
19462 Priv_Discr := First_Discriminant (Priv_Parent);
19463 Full_Discr := First_Discriminant (Full_Parent);
19464 while Present (Priv_Discr) and then Present (Full_Discr) loop
19465 if Original_Record_Component (Priv_Discr) =
19466 Original_Record_Component (Full_Discr)
19468 Corresponding_Discriminant (Priv_Discr) =
19469 Corresponding_Discriminant (Full_Discr)
19476 Next_Discriminant (Priv_Discr);
19477 Next_Discriminant (Full_Discr);
19480 if Present (Priv_Discr) or else Present (Full_Discr) then
19482 ("full view must inherit discriminants of the parent"
19483 & " type used in the private extension", Full_Indic);
19485 elsif Priv_Constr and then not Full_Constr then
19487 ("parent subtype of full type must be constrained",
19490 elsif Full_Constr and then not Priv_Constr then
19492 ("parent subtype of full type must be unconstrained",
19497 -- Check the rules of 7.3(12): if a partial view has neither
19498 -- known or unknown discriminants, then the full type
19499 -- declaration shall define a definite subtype.
19501 elsif not Has_Unknown_Discriminants (Priv_T)
19502 and then not Has_Discriminants (Priv_T)
19503 and then not Is_Constrained (Full_T)
19506 ("full view must define a constrained type if partial view"
19507 & " has no discriminants", Full_T);
19510 -- ??????? Do we implement the following properly ?????
19511 -- If the ancestor subtype of a private extension has constrained
19512 -- discriminants, then the parent subtype of the full view shall
19513 -- impose a statically matching constraint on those discriminants
19518 -- For untagged types, verify that a type without discriminants is
19519 -- not completed with an unconstrained type. A separate error message
19520 -- is produced if the full type has defaulted discriminants.
19522 if not Is_Indefinite_Subtype (Priv_T)
19523 and then Is_Indefinite_Subtype (Full_T)
19525 Error_Msg_Sloc := Sloc (Parent (Priv_T));
19527 ("full view of& not compatible with declaration#",
19530 if not Is_Tagged_Type (Full_T) then
19532 ("\one is constrained, the other unconstrained", Full_T);
19537 -- AI-419: verify that the use of "limited" is consistent
19540 Orig_Decl : constant Node_Id := Original_Node (N);
19543 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19544 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
19546 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
19548 if not Limited_Present (Parent (Priv_T))
19549 and then not Synchronized_Present (Parent (Priv_T))
19550 and then Limited_Present (Type_Definition (Orig_Decl))
19553 ("full view of non-limited extension cannot be limited", N);
19555 -- Conversely, if the partial view carries the limited keyword,
19556 -- the full view must as well, even if it may be redundant.
19558 elsif Limited_Present (Parent (Priv_T))
19559 and then not Limited_Present (Type_Definition (Orig_Decl))
19562 ("full view of limited extension must be explicitly limited",
19568 -- Ada 2005 (AI-443): A synchronized private extension must be
19569 -- completed by a task or protected type.
19571 if Ada_Version >= Ada_2005
19572 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19573 and then Synchronized_Present (Parent (Priv_T))
19574 and then not Is_Concurrent_Type (Full_T)
19576 Error_Msg_N ("full view of synchronized extension must " &
19577 "be synchronized type", N);
19580 -- Ada 2005 AI-363: if the full view has discriminants with
19581 -- defaults, it is illegal to declare constrained access subtypes
19582 -- whose designated type is the current type. This allows objects
19583 -- of the type that are declared in the heap to be unconstrained.
19585 if not Has_Unknown_Discriminants (Priv_T)
19586 and then not Has_Discriminants (Priv_T)
19587 and then Has_Discriminants (Full_T)
19589 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
19591 Set_Has_Constrained_Partial_View (Full_T);
19592 Set_Has_Constrained_Partial_View (Priv_T);
19595 -- Create a full declaration for all its subtypes recorded in
19596 -- Private_Dependents and swap them similarly to the base type. These
19597 -- are subtypes that have been define before the full declaration of
19598 -- the private type. We also swap the entry in Private_Dependents list
19599 -- so we can properly restore the private view on exit from the scope.
19602 Priv_Elmt : Elmt_Id;
19603 Priv_Scop : Entity_Id;
19608 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
19609 while Present (Priv_Elmt) loop
19610 Priv := Node (Priv_Elmt);
19611 Priv_Scop := Scope (Priv);
19613 if Ekind_In (Priv, E_Private_Subtype,
19614 E_Limited_Private_Subtype,
19615 E_Record_Subtype_With_Private)
19617 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
19618 Set_Is_Itype (Full);
19619 Set_Parent (Full, Parent (Priv));
19620 Set_Associated_Node_For_Itype (Full, N);
19622 -- Now we need to complete the private subtype, but since the
19623 -- base type has already been swapped, we must also swap the
19624 -- subtypes (and thus, reverse the arguments in the call to
19625 -- Complete_Private_Subtype). Also note that we may need to
19626 -- re-establish the scope of the private subtype.
19628 Copy_And_Swap (Priv, Full);
19630 if not In_Open_Scopes (Priv_Scop) then
19631 Push_Scope (Priv_Scop);
19634 -- Reset Priv_Scop to Empty to indicate no scope was pushed
19636 Priv_Scop := Empty;
19639 Complete_Private_Subtype (Full, Priv, Full_T, N);
19641 if Present (Priv_Scop) then
19645 Replace_Elmt (Priv_Elmt, Full);
19648 Next_Elmt (Priv_Elmt);
19652 -- If the private view was tagged, copy the new primitive operations
19653 -- from the private view to the full view.
19655 if Is_Tagged_Type (Full_T) then
19657 Disp_Typ : Entity_Id;
19658 Full_List : Elist_Id;
19660 Prim_Elmt : Elmt_Id;
19661 Priv_List : Elist_Id;
19665 L : Elist_Id) return Boolean;
19666 -- Determine whether list L contains element E
19674 L : Elist_Id) return Boolean
19676 List_Elmt : Elmt_Id;
19679 List_Elmt := First_Elmt (L);
19680 while Present (List_Elmt) loop
19681 if Node (List_Elmt) = E then
19685 Next_Elmt (List_Elmt);
19691 -- Start of processing
19694 if Is_Tagged_Type (Priv_T) then
19695 Priv_List := Primitive_Operations (Priv_T);
19696 Prim_Elmt := First_Elmt (Priv_List);
19698 -- In the case of a concurrent type completing a private tagged
19699 -- type, primitives may have been declared in between the two
19700 -- views. These subprograms need to be wrapped the same way
19701 -- entries and protected procedures are handled because they
19702 -- cannot be directly shared by the two views.
19704 if Is_Concurrent_Type (Full_T) then
19706 Conc_Typ : constant Entity_Id :=
19707 Corresponding_Record_Type (Full_T);
19708 Curr_Nod : Node_Id := Parent (Conc_Typ);
19709 Wrap_Spec : Node_Id;
19712 while Present (Prim_Elmt) loop
19713 Prim := Node (Prim_Elmt);
19715 if Comes_From_Source (Prim)
19716 and then not Is_Abstract_Subprogram (Prim)
19719 Make_Subprogram_Declaration (Sloc (Prim),
19723 Obj_Typ => Conc_Typ,
19725 Parameter_Specifications (
19728 Insert_After (Curr_Nod, Wrap_Spec);
19729 Curr_Nod := Wrap_Spec;
19731 Analyze (Wrap_Spec);
19734 Next_Elmt (Prim_Elmt);
19740 -- For non-concurrent types, transfer explicit primitives, but
19741 -- omit those inherited from the parent of the private view
19742 -- since they will be re-inherited later on.
19745 Full_List := Primitive_Operations (Full_T);
19747 while Present (Prim_Elmt) loop
19748 Prim := Node (Prim_Elmt);
19750 if Comes_From_Source (Prim)
19751 and then not Contains (Prim, Full_List)
19753 Append_Elmt (Prim, Full_List);
19756 Next_Elmt (Prim_Elmt);
19760 -- Untagged private view
19763 Full_List := Primitive_Operations (Full_T);
19765 -- In this case the partial view is untagged, so here we locate
19766 -- all of the earlier primitives that need to be treated as
19767 -- dispatching (those that appear between the two views). Note
19768 -- that these additional operations must all be new operations
19769 -- (any earlier operations that override inherited operations
19770 -- of the full view will already have been inserted in the
19771 -- primitives list, marked by Check_Operation_From_Private_View
19772 -- as dispatching. Note that implicit "/=" operators are
19773 -- excluded from being added to the primitives list since they
19774 -- shouldn't be treated as dispatching (tagged "/=" is handled
19777 Prim := Next_Entity (Full_T);
19778 while Present (Prim) and then Prim /= Priv_T loop
19779 if Ekind_In (Prim, E_Procedure, E_Function) then
19780 Disp_Typ := Find_Dispatching_Type (Prim);
19782 if Disp_Typ = Full_T
19783 and then (Chars (Prim) /= Name_Op_Ne
19784 or else Comes_From_Source (Prim))
19786 Check_Controlling_Formals (Full_T, Prim);
19788 if not Is_Dispatching_Operation (Prim) then
19789 Append_Elmt (Prim, Full_List);
19790 Set_Is_Dispatching_Operation (Prim, True);
19791 Set_DT_Position_Value (Prim, No_Uint);
19794 elsif Is_Dispatching_Operation (Prim)
19795 and then Disp_Typ /= Full_T
19798 -- Verify that it is not otherwise controlled by a
19799 -- formal or a return value of type T.
19801 Check_Controlling_Formals (Disp_Typ, Prim);
19805 Next_Entity (Prim);
19809 -- For the tagged case, the two views can share the same primitive
19810 -- operations list and the same class-wide type. Update attributes
19811 -- of the class-wide type which depend on the full declaration.
19813 if Is_Tagged_Type (Priv_T) then
19814 Set_Direct_Primitive_Operations (Priv_T, Full_List);
19815 Set_Class_Wide_Type
19816 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
19818 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
19820 (Class_Wide_Type (Priv_T), Has_Protected (Full_T));
19825 -- Ada 2005 AI 161: Check preelaborable initialization consistency
19827 if Known_To_Have_Preelab_Init (Priv_T) then
19829 -- Case where there is a pragma Preelaborable_Initialization. We
19830 -- always allow this in predefined units, which is cheating a bit,
19831 -- but it means we don't have to struggle to meet the requirements in
19832 -- the RM for having Preelaborable Initialization. Otherwise we
19833 -- require that the type meets the RM rules. But we can't check that
19834 -- yet, because of the rule about overriding Initialize, so we simply
19835 -- set a flag that will be checked at freeze time.
19837 if not In_Predefined_Unit (Full_T) then
19838 Set_Must_Have_Preelab_Init (Full_T);
19842 -- If pragma CPP_Class was applied to the private type declaration,
19843 -- propagate it now to the full type declaration.
19845 if Is_CPP_Class (Priv_T) then
19846 Set_Is_CPP_Class (Full_T);
19847 Set_Convention (Full_T, Convention_CPP);
19849 -- Check that components of imported CPP types do not have default
19852 Check_CPP_Type_Has_No_Defaults (Full_T);
19855 -- If the private view has user specified stream attributes, then so has
19858 -- Why the test, how could these flags be already set in Full_T ???
19860 if Has_Specified_Stream_Read (Priv_T) then
19861 Set_Has_Specified_Stream_Read (Full_T);
19864 if Has_Specified_Stream_Write (Priv_T) then
19865 Set_Has_Specified_Stream_Write (Full_T);
19868 if Has_Specified_Stream_Input (Priv_T) then
19869 Set_Has_Specified_Stream_Input (Full_T);
19872 if Has_Specified_Stream_Output (Priv_T) then
19873 Set_Has_Specified_Stream_Output (Full_T);
19876 -- Propagate the attributes related to pragma Default_Initial_Condition
19877 -- from the private to the full view. Note that both flags are mutually
19880 if Has_Default_Init_Cond (Priv_T)
19881 or else Has_Inherited_Default_Init_Cond (Priv_T)
19883 Propagate_Default_Init_Cond_Attributes
19884 (From_Typ => Priv_T,
19886 Private_To_Full_View => True);
19888 -- In the case where the full view is derived from another private type,
19889 -- the attributes related to pragma Default_Initial_Condition must be
19890 -- propagated from the full to the private view to maintain consistency
19894 -- type Parent_Typ is private
19895 -- with Default_Initial_Condition ...;
19897 -- type Parent_Typ is ...;
19900 -- with Pack; use Pack;
19901 -- package Pack_2 is
19902 -- type Deriv_Typ is private; -- must inherit
19904 -- type Deriv_Typ is new Parent_Typ; -- must inherit
19907 elsif Has_Default_Init_Cond (Full_T)
19908 or else Has_Inherited_Default_Init_Cond (Full_T)
19910 Propagate_Default_Init_Cond_Attributes
19911 (From_Typ => Full_T,
19913 Private_To_Full_View => True);
19916 -- Propagate the attributes related to pragma Ghost from the private to
19919 if Is_Ghost_Entity (Priv_T) then
19920 Set_Is_Ghost_Entity (Full_T);
19922 -- The Ghost policy in effect at the point of declaration and at the
19923 -- point of completion must match (SPARK RM 6.9(15)).
19925 Check_Ghost_Completion (Priv_T, Full_T);
19927 -- In the case where the private view of a tagged type lacks a parent
19928 -- type and is subject to pragma Ghost, ensure that the parent type
19929 -- specified by the full view is also Ghost (SPARK RM 6.9(9)).
19931 if Is_Derived_Type (Full_T) then
19932 Check_Ghost_Derivation (Full_T);
19936 -- Propagate invariants to full type
19938 if Has_Invariants (Priv_T) then
19939 Set_Has_Invariants (Full_T);
19940 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
19943 if Has_Inheritable_Invariants (Priv_T) then
19944 Set_Has_Inheritable_Invariants (Full_T);
19947 -- Check hidden inheritance of class-wide type invariants
19949 if Ada_Version >= Ada_2012
19950 and then not Has_Inheritable_Invariants (Full_T)
19951 and then In_Private_Part (Current_Scope)
19952 and then Has_Interfaces (Full_T)
19959 Collect_Interfaces (Full_T, Ifaces, Exclude_Parents => True);
19961 AI := First_Elmt (Ifaces);
19962 while Present (AI) loop
19963 if Has_Inheritable_Invariants (Node (AI)) then
19965 ("hidden inheritance of class-wide type invariants " &
19975 -- Propagate predicates to full type, and predicate function if already
19976 -- defined. It is not clear that this can actually happen? the partial
19977 -- view cannot be frozen yet, and the predicate function has not been
19978 -- built. Still it is a cheap check and seems safer to make it.
19980 if Has_Predicates (Priv_T) then
19981 if Present (Predicate_Function (Priv_T)) then
19982 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
19985 Set_Has_Predicates (Full_T);
19987 end Process_Full_View;
19989 -----------------------------------
19990 -- Process_Incomplete_Dependents --
19991 -----------------------------------
19993 procedure Process_Incomplete_Dependents
19995 Full_T : Entity_Id;
19998 Inc_Elmt : Elmt_Id;
19999 Priv_Dep : Entity_Id;
20000 New_Subt : Entity_Id;
20002 Disc_Constraint : Elist_Id;
20005 if No (Private_Dependents (Inc_T)) then
20009 -- Itypes that may be generated by the completion of an incomplete
20010 -- subtype are not used by the back-end and not attached to the tree.
20011 -- They are created only for constraint-checking purposes.
20013 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20014 while Present (Inc_Elmt) loop
20015 Priv_Dep := Node (Inc_Elmt);
20017 if Ekind (Priv_Dep) = E_Subprogram_Type then
20019 -- An Access_To_Subprogram type may have a return type or a
20020 -- parameter type that is incomplete. Replace with the full view.
20022 if Etype (Priv_Dep) = Inc_T then
20023 Set_Etype (Priv_Dep, Full_T);
20027 Formal : Entity_Id;
20030 Formal := First_Formal (Priv_Dep);
20031 while Present (Formal) loop
20032 if Etype (Formal) = Inc_T then
20033 Set_Etype (Formal, Full_T);
20036 Next_Formal (Formal);
20040 elsif Is_Overloadable (Priv_Dep) then
20042 -- If a subprogram in the incomplete dependents list is primitive
20043 -- for a tagged full type then mark it as a dispatching operation,
20044 -- check whether it overrides an inherited subprogram, and check
20045 -- restrictions on its controlling formals. Note that a protected
20046 -- operation is never dispatching: only its wrapper operation
20047 -- (which has convention Ada) is.
20049 if Is_Tagged_Type (Full_T)
20050 and then Is_Primitive (Priv_Dep)
20051 and then Convention (Priv_Dep) /= Convention_Protected
20053 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20054 Set_Is_Dispatching_Operation (Priv_Dep);
20055 Check_Controlling_Formals (Full_T, Priv_Dep);
20058 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20060 -- Can happen during processing of a body before the completion
20061 -- of a TA type. Ignore, because spec is also on dependent list.
20065 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20066 -- corresponding subtype of the full view.
20068 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20069 Set_Subtype_Indication
20070 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20071 Set_Etype (Priv_Dep, Full_T);
20072 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20073 Set_Analyzed (Parent (Priv_Dep), False);
20075 -- Reanalyze the declaration, suppressing the call to
20076 -- Enter_Name to avoid duplicate names.
20078 Analyze_Subtype_Declaration
20079 (N => Parent (Priv_Dep),
20082 -- Dependent is a subtype
20085 -- We build a new subtype indication using the full view of the
20086 -- incomplete parent. The discriminant constraints have been
20087 -- elaborated already at the point of the subtype declaration.
20089 New_Subt := Create_Itype (E_Void, N);
20091 if Has_Discriminants (Full_T) then
20092 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20094 Disc_Constraint := No_Elist;
20097 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20098 Set_Full_View (Priv_Dep, New_Subt);
20101 Next_Elmt (Inc_Elmt);
20103 end Process_Incomplete_Dependents;
20105 --------------------------------
20106 -- Process_Range_Expr_In_Decl --
20107 --------------------------------
20109 procedure Process_Range_Expr_In_Decl
20112 Subtyp : Entity_Id := Empty;
20113 Check_List : List_Id := Empty_List;
20114 R_Check_Off : Boolean := False;
20115 In_Iter_Schm : Boolean := False)
20118 R_Checks : Check_Result;
20119 Insert_Node : Node_Id;
20120 Def_Id : Entity_Id;
20123 Analyze_And_Resolve (R, Base_Type (T));
20125 if Nkind (R) = N_Range then
20127 -- In SPARK, all ranges should be static, with the exception of the
20128 -- discrete type definition of a loop parameter specification.
20130 if not In_Iter_Schm
20131 and then not Is_OK_Static_Range (R)
20133 Check_SPARK_05_Restriction ("range should be static", R);
20136 Lo := Low_Bound (R);
20137 Hi := High_Bound (R);
20139 -- Validity checks on the range of a quantified expression are
20140 -- delayed until the construct is transformed into a loop.
20142 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20143 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20147 -- We need to ensure validity of the bounds here, because if we
20148 -- go ahead and do the expansion, then the expanded code will get
20149 -- analyzed with range checks suppressed and we miss the check.
20151 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20152 -- the temporaries generated by routine Remove_Side_Effects by means
20153 -- of validity checks must use the same names. When a range appears
20154 -- in the parent of a generic, the range is processed with checks
20155 -- disabled as part of the generic context and with checks enabled
20156 -- for code generation purposes. This leads to link issues as the
20157 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20158 -- template sees the temporaries generated by Remove_Side_Effects.
20161 Validity_Check_Range (R, Subtyp);
20164 -- If there were errors in the declaration, try and patch up some
20165 -- common mistakes in the bounds. The cases handled are literals
20166 -- which are Integer where the expected type is Real and vice versa.
20167 -- These corrections allow the compilation process to proceed further
20168 -- along since some basic assumptions of the format of the bounds
20171 if Etype (R) = Any_Type then
20172 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20174 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20176 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20178 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20180 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20182 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20184 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20186 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20193 -- If the bounds of the range have been mistakenly given as string
20194 -- literals (perhaps in place of character literals), then an error
20195 -- has already been reported, but we rewrite the string literal as a
20196 -- bound of the range's type to avoid blowups in later processing
20197 -- that looks at static values.
20199 if Nkind (Lo) = N_String_Literal then
20201 Make_Attribute_Reference (Sloc (Lo),
20202 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20203 Attribute_Name => Name_First));
20204 Analyze_And_Resolve (Lo);
20207 if Nkind (Hi) = N_String_Literal then
20209 Make_Attribute_Reference (Sloc (Hi),
20210 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20211 Attribute_Name => Name_First));
20212 Analyze_And_Resolve (Hi);
20215 -- If bounds aren't scalar at this point then exit, avoiding
20216 -- problems with further processing of the range in this procedure.
20218 if not Is_Scalar_Type (Etype (Lo)) then
20222 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20223 -- then range of the base type. Here we check whether the bounds
20224 -- are in the range of the subtype itself. Note that if the bounds
20225 -- represent the null range the Constraint_Error exception should
20228 -- ??? The following code should be cleaned up as follows
20230 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20231 -- is done in the call to Range_Check (R, T); below
20233 -- 2. The use of R_Check_Off should be investigated and possibly
20234 -- removed, this would clean up things a bit.
20236 if Is_Null_Range (Lo, Hi) then
20240 -- Capture values of bounds and generate temporaries for them
20241 -- if needed, before applying checks, since checks may cause
20242 -- duplication of the expression without forcing evaluation.
20244 -- The forced evaluation removes side effects from expressions,
20245 -- which should occur also in GNATprove mode. Otherwise, we end up
20246 -- with unexpected insertions of actions at places where this is
20247 -- not supposed to occur, e.g. on default parameters of a call.
20249 if Expander_Active or GNATprove_Mode then
20251 -- Call Force_Evaluation to create declarations as needed to
20252 -- deal with side effects, and also create typ_FIRST/LAST
20253 -- entities for bounds if we have a subtype name.
20255 -- Note: we do this transformation even if expansion is not
20256 -- active if we are in GNATprove_Mode since the transformation
20257 -- is in general required to ensure that the resulting tree has
20258 -- proper Ada semantics.
20261 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20263 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20266 -- We use a flag here instead of suppressing checks on the type
20267 -- because the type we check against isn't necessarily the place
20268 -- where we put the check.
20270 if not R_Check_Off then
20271 R_Checks := Get_Range_Checks (R, T);
20273 -- Look up tree to find an appropriate insertion point. We
20274 -- can't just use insert_actions because later processing
20275 -- depends on the insertion node. Prior to Ada 2012 the
20276 -- insertion point could only be a declaration or a loop, but
20277 -- quantified expressions can appear within any context in an
20278 -- expression, and the insertion point can be any statement,
20279 -- pragma, or declaration.
20281 Insert_Node := Parent (R);
20282 while Present (Insert_Node) loop
20284 Nkind (Insert_Node) in N_Declaration
20287 (Insert_Node, N_Component_Declaration,
20288 N_Loop_Parameter_Specification,
20289 N_Function_Specification,
20290 N_Procedure_Specification);
20292 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20293 or else Nkind (Insert_Node) in
20294 N_Statement_Other_Than_Procedure_Call
20295 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20298 Insert_Node := Parent (Insert_Node);
20301 -- Why would Type_Decl not be present??? Without this test,
20302 -- short regression tests fail.
20304 if Present (Insert_Node) then
20306 -- Case of loop statement. Verify that the range is part
20307 -- of the subtype indication of the iteration scheme.
20309 if Nkind (Insert_Node) = N_Loop_Statement then
20314 Indic := Parent (R);
20315 while Present (Indic)
20316 and then Nkind (Indic) /= N_Subtype_Indication
20318 Indic := Parent (Indic);
20321 if Present (Indic) then
20322 Def_Id := Etype (Subtype_Mark (Indic));
20324 Insert_Range_Checks
20328 Sloc (Insert_Node),
20330 Do_Before => True);
20334 -- Insertion before a declaration. If the declaration
20335 -- includes discriminants, the list of applicable checks
20336 -- is given by the caller.
20338 elsif Nkind (Insert_Node) in N_Declaration then
20339 Def_Id := Defining_Identifier (Insert_Node);
20341 if (Ekind (Def_Id) = E_Record_Type
20342 and then Depends_On_Discriminant (R))
20344 (Ekind (Def_Id) = E_Protected_Type
20345 and then Has_Discriminants (Def_Id))
20347 Append_Range_Checks
20349 Check_List, Def_Id, Sloc (Insert_Node), R);
20352 Insert_Range_Checks
20354 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20358 -- Insertion before a statement. Range appears in the
20359 -- context of a quantified expression. Insertion will
20360 -- take place when expression is expanded.
20369 -- Case of other than an explicit N_Range node
20371 -- The forced evaluation removes side effects from expressions, which
20372 -- should occur also in GNATprove mode. Otherwise, we end up with
20373 -- unexpected insertions of actions at places where this is not
20374 -- supposed to occur, e.g. on default parameters of a call.
20376 elsif Expander_Active or GNATprove_Mode then
20377 Get_Index_Bounds (R, Lo, Hi);
20378 Force_Evaluation (Lo);
20379 Force_Evaluation (Hi);
20381 end Process_Range_Expr_In_Decl;
20383 --------------------------------------
20384 -- Process_Real_Range_Specification --
20385 --------------------------------------
20387 procedure Process_Real_Range_Specification (Def : Node_Id) is
20388 Spec : constant Node_Id := Real_Range_Specification (Def);
20391 Err : Boolean := False;
20393 procedure Analyze_Bound (N : Node_Id);
20394 -- Analyze and check one bound
20396 -------------------
20397 -- Analyze_Bound --
20398 -------------------
20400 procedure Analyze_Bound (N : Node_Id) is
20402 Analyze_And_Resolve (N, Any_Real);
20404 if not Is_OK_Static_Expression (N) then
20405 Flag_Non_Static_Expr
20406 ("bound in real type definition is not static!", N);
20411 -- Start of processing for Process_Real_Range_Specification
20414 if Present (Spec) then
20415 Lo := Low_Bound (Spec);
20416 Hi := High_Bound (Spec);
20417 Analyze_Bound (Lo);
20418 Analyze_Bound (Hi);
20420 -- If error, clear away junk range specification
20423 Set_Real_Range_Specification (Def, Empty);
20426 end Process_Real_Range_Specification;
20428 ---------------------
20429 -- Process_Subtype --
20430 ---------------------
20432 function Process_Subtype
20434 Related_Nod : Node_Id;
20435 Related_Id : Entity_Id := Empty;
20436 Suffix : Character := ' ') return Entity_Id
20439 Def_Id : Entity_Id;
20440 Error_Node : Node_Id;
20441 Full_View_Id : Entity_Id;
20442 Subtype_Mark_Id : Entity_Id;
20444 May_Have_Null_Exclusion : Boolean;
20446 procedure Check_Incomplete (T : Entity_Id);
20447 -- Called to verify that an incomplete type is not used prematurely
20449 ----------------------
20450 -- Check_Incomplete --
20451 ----------------------
20453 procedure Check_Incomplete (T : Entity_Id) is
20455 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20457 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20459 not (Ada_Version >= Ada_2005
20461 (Nkind (Parent (T)) = N_Subtype_Declaration
20462 or else (Nkind (Parent (T)) = N_Subtype_Indication
20463 and then Nkind (Parent (Parent (T))) =
20464 N_Subtype_Declaration)))
20466 Error_Msg_N ("invalid use of type before its full declaration", T);
20468 end Check_Incomplete;
20470 -- Start of processing for Process_Subtype
20473 -- Case of no constraints present
20475 if Nkind (S) /= N_Subtype_Indication then
20477 Check_Incomplete (S);
20480 -- Ada 2005 (AI-231): Static check
20482 if Ada_Version >= Ada_2005
20483 and then Present (P)
20484 and then Null_Exclusion_Present (P)
20485 and then Nkind (P) /= N_Access_To_Object_Definition
20486 and then not Is_Access_Type (Entity (S))
20488 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20491 -- The following is ugly, can't we have a range or even a flag???
20493 May_Have_Null_Exclusion :=
20494 Nkind_In (P, N_Access_Definition,
20495 N_Access_Function_Definition,
20496 N_Access_Procedure_Definition,
20497 N_Access_To_Object_Definition,
20499 N_Component_Definition)
20501 Nkind_In (P, N_Derived_Type_Definition,
20502 N_Discriminant_Specification,
20503 N_Formal_Object_Declaration,
20504 N_Object_Declaration,
20505 N_Object_Renaming_Declaration,
20506 N_Parameter_Specification,
20507 N_Subtype_Declaration);
20509 -- Create an Itype that is a duplicate of Entity (S) but with the
20510 -- null-exclusion attribute.
20512 if May_Have_Null_Exclusion
20513 and then Is_Access_Type (Entity (S))
20514 and then Null_Exclusion_Present (P)
20516 -- No need to check the case of an access to object definition.
20517 -- It is correct to define double not-null pointers.
20520 -- type Not_Null_Int_Ptr is not null access Integer;
20521 -- type Acc is not null access Not_Null_Int_Ptr;
20523 and then Nkind (P) /= N_Access_To_Object_Definition
20525 if Can_Never_Be_Null (Entity (S)) then
20526 case Nkind (Related_Nod) is
20527 when N_Full_Type_Declaration =>
20528 if Nkind (Type_Definition (Related_Nod))
20529 in N_Array_Type_Definition
20533 (Component_Definition
20534 (Type_Definition (Related_Nod)));
20537 Subtype_Indication (Type_Definition (Related_Nod));
20540 when N_Subtype_Declaration =>
20541 Error_Node := Subtype_Indication (Related_Nod);
20543 when N_Object_Declaration =>
20544 Error_Node := Object_Definition (Related_Nod);
20546 when N_Component_Declaration =>
20548 Subtype_Indication (Component_Definition (Related_Nod));
20550 when N_Allocator =>
20551 Error_Node := Expression (Related_Nod);
20554 pragma Assert (False);
20555 Error_Node := Related_Nod;
20559 ("`NOT NULL` not allowed (& already excludes null)",
20565 Create_Null_Excluding_Itype
20567 Related_Nod => P));
20568 Set_Entity (S, Etype (S));
20573 -- Case of constraint present, so that we have an N_Subtype_Indication
20574 -- node (this node is created only if constraints are present).
20577 Find_Type (Subtype_Mark (S));
20579 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
20581 (Nkind (Parent (S)) = N_Subtype_Declaration
20582 and then Is_Itype (Defining_Identifier (Parent (S))))
20584 Check_Incomplete (Subtype_Mark (S));
20588 Subtype_Mark_Id := Entity (Subtype_Mark (S));
20590 -- Explicit subtype declaration case
20592 if Nkind (P) = N_Subtype_Declaration then
20593 Def_Id := Defining_Identifier (P);
20595 -- Explicit derived type definition case
20597 elsif Nkind (P) = N_Derived_Type_Definition then
20598 Def_Id := Defining_Identifier (Parent (P));
20600 -- Implicit case, the Def_Id must be created as an implicit type.
20601 -- The one exception arises in the case of concurrent types, array
20602 -- and access types, where other subsidiary implicit types may be
20603 -- created and must appear before the main implicit type. In these
20604 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
20605 -- has not yet been called to create Def_Id.
20608 if Is_Array_Type (Subtype_Mark_Id)
20609 or else Is_Concurrent_Type (Subtype_Mark_Id)
20610 or else Is_Access_Type (Subtype_Mark_Id)
20614 -- For the other cases, we create a new unattached Itype,
20615 -- and set the indication to ensure it gets attached later.
20619 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20623 -- If the kind of constraint is invalid for this kind of type,
20624 -- then give an error, and then pretend no constraint was given.
20626 if not Is_Valid_Constraint_Kind
20627 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
20630 ("incorrect constraint for this kind of type", Constraint (S));
20632 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
20634 -- Set Ekind of orphan itype, to prevent cascaded errors
20636 if Present (Def_Id) then
20637 Set_Ekind (Def_Id, Ekind (Any_Type));
20640 -- Make recursive call, having got rid of the bogus constraint
20642 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
20645 -- Remaining processing depends on type. Select on Base_Type kind to
20646 -- ensure getting to the concrete type kind in the case of a private
20647 -- subtype (needed when only doing semantic analysis).
20649 case Ekind (Base_Type (Subtype_Mark_Id)) is
20650 when Access_Kind =>
20652 -- If this is a constraint on a class-wide type, discard it.
20653 -- There is currently no way to express a partial discriminant
20654 -- constraint on a type with unknown discriminants. This is
20655 -- a pathology that the ACATS wisely decides not to test.
20657 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
20658 if Comes_From_Source (S) then
20660 ("constraint on class-wide type ignored??",
20664 if Nkind (P) = N_Subtype_Declaration then
20665 Set_Subtype_Indication (P,
20666 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
20669 return Subtype_Mark_Id;
20672 Constrain_Access (Def_Id, S, Related_Nod);
20675 and then Is_Itype (Designated_Type (Def_Id))
20676 and then Nkind (Related_Nod) = N_Subtype_Declaration
20677 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
20679 Build_Itype_Reference
20680 (Designated_Type (Def_Id), Related_Nod);
20684 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
20686 when Decimal_Fixed_Point_Kind =>
20687 Constrain_Decimal (Def_Id, S);
20689 when Enumeration_Kind =>
20690 Constrain_Enumeration (Def_Id, S);
20691 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20693 when Ordinary_Fixed_Point_Kind =>
20694 Constrain_Ordinary_Fixed (Def_Id, S);
20697 Constrain_Float (Def_Id, S);
20699 when Integer_Kind =>
20700 Constrain_Integer (Def_Id, S);
20701 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20703 when E_Record_Type |
20706 E_Incomplete_Type =>
20707 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20709 if Ekind (Def_Id) = E_Incomplete_Type then
20710 Set_Private_Dependents (Def_Id, New_Elmt_List);
20713 when Private_Kind =>
20714 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20715 Set_Private_Dependents (Def_Id, New_Elmt_List);
20717 -- In case of an invalid constraint prevent further processing
20718 -- since the type constructed is missing expected fields.
20720 if Etype (Def_Id) = Any_Type then
20724 -- If the full view is that of a task with discriminants,
20725 -- we must constrain both the concurrent type and its
20726 -- corresponding record type. Otherwise we will just propagate
20727 -- the constraint to the full view, if available.
20729 if Present (Full_View (Subtype_Mark_Id))
20730 and then Has_Discriminants (Subtype_Mark_Id)
20731 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
20734 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20736 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
20737 Constrain_Concurrent (Full_View_Id, S,
20738 Related_Nod, Related_Id, Suffix);
20739 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
20740 Set_Full_View (Def_Id, Full_View_Id);
20742 -- Introduce an explicit reference to the private subtype,
20743 -- to prevent scope anomalies in gigi if first use appears
20744 -- in a nested context, e.g. a later function body.
20745 -- Should this be generated in other contexts than a full
20746 -- type declaration?
20748 if Is_Itype (Def_Id)
20750 Nkind (Parent (P)) = N_Full_Type_Declaration
20752 Build_Itype_Reference (Def_Id, Parent (P));
20756 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
20759 when Concurrent_Kind =>
20760 Constrain_Concurrent (Def_Id, S,
20761 Related_Nod, Related_Id, Suffix);
20764 Error_Msg_N ("invalid subtype mark in subtype indication", S);
20767 -- Size and Convention are always inherited from the base type
20769 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
20770 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
20774 end Process_Subtype;
20776 --------------------------------------------
20777 -- Propagate_Default_Init_Cond_Attributes --
20778 --------------------------------------------
20780 procedure Propagate_Default_Init_Cond_Attributes
20781 (From_Typ : Entity_Id;
20782 To_Typ : Entity_Id;
20783 Parent_To_Derivation : Boolean := False;
20784 Private_To_Full_View : Boolean := False)
20786 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id);
20787 -- Remove the default initial procedure (if any) from the rep chain of
20790 ----------------------------------------
20791 -- Remove_Default_Init_Cond_Procedure --
20792 ----------------------------------------
20794 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id) is
20795 Found : Boolean := False;
20801 Subp := Subprograms_For_Type (Typ);
20802 while Present (Subp) loop
20803 if Is_Default_Init_Cond_Procedure (Subp) then
20809 Subp := Subprograms_For_Type (Subp);
20813 Set_Subprograms_For_Type (Prev, Subprograms_For_Type (Subp));
20814 Set_Subprograms_For_Type (Subp, Empty);
20816 end Remove_Default_Init_Cond_Procedure;
20820 Inherit_Procedure : Boolean := False;
20822 -- Start of processing for Propagate_Default_Init_Cond_Attributes
20825 if Has_Default_Init_Cond (From_Typ) then
20827 -- A derived type inherits the attributes from its parent type
20829 if Parent_To_Derivation then
20830 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20832 -- A full view shares the attributes with its private view
20835 Set_Has_Default_Init_Cond (To_Typ);
20838 Inherit_Procedure := True;
20840 -- Due to the order of expansion, a derived private type is processed
20841 -- by two routines which both attempt to set the attributes related
20842 -- to pragma Default_Initial_Condition - Build_Derived_Type and then
20843 -- Process_Full_View.
20846 -- type Parent_Typ is private
20847 -- with Default_Initial_Condition ...;
20849 -- type Parent_Typ is ...;
20852 -- with Pack; use Pack;
20853 -- package Pack_2 is
20854 -- type Deriv_Typ is private
20855 -- with Default_Initial_Condition ...;
20857 -- type Deriv_Typ is new Parent_Typ;
20860 -- When Build_Derived_Type operates, it sets the attributes on the
20861 -- full view without taking into account that the private view may
20862 -- define its own default initial condition procedure. This becomes
20863 -- apparent in Process_Full_View which must undo some of the work by
20864 -- Build_Derived_Type and propagate the attributes from the private
20865 -- to the full view.
20867 if Private_To_Full_View then
20868 Set_Has_Inherited_Default_Init_Cond (To_Typ, False);
20869 Remove_Default_Init_Cond_Procedure (To_Typ);
20872 -- A type must inherit the default initial condition procedure from a
20873 -- parent type when the parent itself is inheriting the procedure or
20874 -- when it is defining one. This circuitry is also used when dealing
20875 -- with the private / full view of a type.
20877 elsif Has_Inherited_Default_Init_Cond (From_Typ)
20878 or (Parent_To_Derivation
20879 and Present (Get_Pragma
20880 (From_Typ, Pragma_Default_Initial_Condition)))
20882 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20883 Inherit_Procedure := True;
20886 if Inherit_Procedure
20887 and then No (Default_Init_Cond_Procedure (To_Typ))
20889 Set_Default_Init_Cond_Procedure
20890 (To_Typ, Default_Init_Cond_Procedure (From_Typ));
20892 end Propagate_Default_Init_Cond_Attributes;
20894 -----------------------------
20895 -- Record_Type_Declaration --
20896 -----------------------------
20898 procedure Record_Type_Declaration
20903 Def : constant Node_Id := Type_Definition (N);
20904 Is_Tagged : Boolean;
20905 Tag_Comp : Entity_Id;
20908 -- These flags must be initialized before calling Process_Discriminants
20909 -- because this routine makes use of them.
20911 Set_Ekind (T, E_Record_Type);
20913 Init_Size_Align (T);
20914 Set_Interfaces (T, No_Elist);
20915 Set_Stored_Constraint (T, No_Elist);
20916 Set_Default_SSO (T);
20920 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
20921 if Limited_Present (Def) then
20922 Check_SPARK_05_Restriction ("limited is not allowed", N);
20925 if Abstract_Present (Def) then
20926 Check_SPARK_05_Restriction ("abstract is not allowed", N);
20929 -- The flag Is_Tagged_Type might have already been set by
20930 -- Find_Type_Name if it detected an error for declaration T. This
20931 -- arises in the case of private tagged types where the full view
20932 -- omits the word tagged.
20935 Tagged_Present (Def)
20936 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
20938 Set_Is_Limited_Record (T, Limited_Present (Def));
20941 Set_Is_Tagged_Type (T, True);
20942 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
20945 -- Type is abstract if full declaration carries keyword, or if
20946 -- previous partial view did.
20948 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
20949 or else Abstract_Present (Def));
20952 Check_SPARK_05_Restriction ("interface is not allowed", N);
20955 Analyze_Interface_Declaration (T, Def);
20957 if Present (Discriminant_Specifications (N)) then
20959 ("interface types cannot have discriminants",
20960 Defining_Identifier
20961 (First (Discriminant_Specifications (N))));
20965 -- First pass: if there are self-referential access components,
20966 -- create the required anonymous access type declarations, and if
20967 -- need be an incomplete type declaration for T itself.
20969 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
20971 if Ada_Version >= Ada_2005
20972 and then Present (Interface_List (Def))
20974 Check_Interfaces (N, Def);
20977 Ifaces_List : Elist_Id;
20980 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
20981 -- already in the parents.
20985 Ifaces_List => Ifaces_List,
20986 Exclude_Parents => True);
20988 Set_Interfaces (T, Ifaces_List);
20992 -- Records constitute a scope for the component declarations within.
20993 -- The scope is created prior to the processing of these declarations.
20994 -- Discriminants are processed first, so that they are visible when
20995 -- processing the other components. The Ekind of the record type itself
20996 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
20998 -- Enter record scope
21002 -- If an incomplete or private type declaration was already given for
21003 -- the type, then this scope already exists, and the discriminants have
21004 -- been declared within. We must verify that the full declaration
21005 -- matches the incomplete one.
21007 Check_Or_Process_Discriminants (N, T, Prev);
21009 Set_Is_Constrained (T, not Has_Discriminants (T));
21010 Set_Has_Delayed_Freeze (T, True);
21012 -- For tagged types add a manually analyzed component corresponding
21013 -- to the component _tag, the corresponding piece of tree will be
21014 -- expanded as part of the freezing actions if it is not a CPP_Class.
21018 -- Do not add the tag unless we are in expansion mode
21020 if Expander_Active then
21021 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21022 Enter_Name (Tag_Comp);
21024 Set_Ekind (Tag_Comp, E_Component);
21025 Set_Is_Tag (Tag_Comp);
21026 Set_Is_Aliased (Tag_Comp);
21027 Set_Etype (Tag_Comp, RTE (RE_Tag));
21028 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21029 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21030 Init_Component_Location (Tag_Comp);
21032 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21033 -- implemented interfaces.
21035 if Has_Interfaces (T) then
21036 Add_Interface_Tag_Components (N, T);
21040 Make_Class_Wide_Type (T);
21041 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21044 -- We must suppress range checks when processing record components in
21045 -- the presence of discriminants, since we don't want spurious checks to
21046 -- be generated during their analysis, but Suppress_Range_Checks flags
21047 -- must be reset the after processing the record definition.
21049 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21050 -- couldn't we just use the normal range check suppression method here.
21051 -- That would seem cleaner ???
21053 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21054 Set_Kill_Range_Checks (T, True);
21055 Record_Type_Definition (Def, Prev);
21056 Set_Kill_Range_Checks (T, False);
21058 Record_Type_Definition (Def, Prev);
21061 -- Exit from record scope
21065 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21066 -- the implemented interfaces and associate them an aliased entity.
21069 and then not Is_Empty_List (Interface_List (Def))
21071 Derive_Progenitor_Subprograms (T, T);
21074 Check_Function_Writable_Actuals (N);
21075 end Record_Type_Declaration;
21077 ----------------------------
21078 -- Record_Type_Definition --
21079 ----------------------------
21081 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21082 Component : Entity_Id;
21083 Ctrl_Components : Boolean := False;
21084 Final_Storage_Only : Boolean;
21088 if Ekind (Prev_T) = E_Incomplete_Type then
21089 T := Full_View (Prev_T);
21094 -- In SPARK, tagged types and type extensions may only be declared in
21095 -- the specification of library unit packages.
21097 if Present (Def) and then Is_Tagged_Type (T) then
21103 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21104 Typ := Parent (Def);
21107 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21108 Typ := Parent (Parent (Def));
21111 Ctxt := Parent (Typ);
21113 if Nkind (Ctxt) = N_Package_Body
21114 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21116 Check_SPARK_05_Restriction
21117 ("type should be defined in package specification", Typ);
21119 elsif Nkind (Ctxt) /= N_Package_Specification
21120 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21122 Check_SPARK_05_Restriction
21123 ("type should be defined in library unit package", Typ);
21128 Final_Storage_Only := not Is_Controlled (T);
21130 -- Ada 2005: Check whether an explicit Limited is present in a derived
21131 -- type declaration.
21133 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21134 and then Limited_Present (Parent (Def))
21136 Set_Is_Limited_Record (T);
21139 -- If the component list of a record type is defined by the reserved
21140 -- word null and there is no discriminant part, then the record type has
21141 -- no components and all records of the type are null records (RM 3.7)
21142 -- This procedure is also called to process the extension part of a
21143 -- record extension, in which case the current scope may have inherited
21147 or else No (Component_List (Def))
21148 or else Null_Present (Component_List (Def))
21150 if not Is_Tagged_Type (T) then
21151 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21155 Analyze_Declarations (Component_Items (Component_List (Def)));
21157 if Present (Variant_Part (Component_List (Def))) then
21158 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21159 Analyze (Variant_Part (Component_List (Def)));
21163 -- After completing the semantic analysis of the record definition,
21164 -- record components, both new and inherited, are accessible. Set their
21165 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21166 -- whose Ekind may be void.
21168 Component := First_Entity (Current_Scope);
21169 while Present (Component) loop
21170 if Ekind (Component) = E_Void
21171 and then not Is_Itype (Component)
21173 Set_Ekind (Component, E_Component);
21174 Init_Component_Location (Component);
21177 if Has_Task (Etype (Component)) then
21181 if Has_Protected (Etype (Component)) then
21182 Set_Has_Protected (T);
21185 if Ekind (Component) /= E_Component then
21188 -- Do not set Has_Controlled_Component on a class-wide equivalent
21189 -- type. See Make_CW_Equivalent_Type.
21191 elsif not Is_Class_Wide_Equivalent_Type (T)
21192 and then (Has_Controlled_Component (Etype (Component))
21193 or else (Chars (Component) /= Name_uParent
21194 and then Is_Controlled (Etype (Component))))
21196 Set_Has_Controlled_Component (T, True);
21197 Final_Storage_Only :=
21199 and then Finalize_Storage_Only (Etype (Component));
21200 Ctrl_Components := True;
21203 Next_Entity (Component);
21206 -- A Type is Finalize_Storage_Only only if all its controlled components
21209 if Ctrl_Components then
21210 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21213 -- Place reference to end record on the proper entity, which may
21214 -- be a partial view.
21216 if Present (Def) then
21217 Process_End_Label (Def, 'e', Prev_T);
21219 end Record_Type_Definition;
21221 ------------------------
21222 -- Replace_Components --
21223 ------------------------
21225 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21226 function Process (N : Node_Id) return Traverse_Result;
21232 function Process (N : Node_Id) return Traverse_Result is
21236 if Nkind (N) = N_Discriminant_Specification then
21237 Comp := First_Discriminant (Typ);
21238 while Present (Comp) loop
21239 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21240 Set_Defining_Identifier (N, Comp);
21244 Next_Discriminant (Comp);
21247 elsif Nkind (N) = N_Component_Declaration then
21248 Comp := First_Component (Typ);
21249 while Present (Comp) loop
21250 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21251 Set_Defining_Identifier (N, Comp);
21255 Next_Component (Comp);
21262 procedure Replace is new Traverse_Proc (Process);
21264 -- Start of processing for Replace_Components
21268 end Replace_Components;
21270 -------------------------------
21271 -- Set_Completion_Referenced --
21272 -------------------------------
21274 procedure Set_Completion_Referenced (E : Entity_Id) is
21276 -- If in main unit, mark entity that is a completion as referenced,
21277 -- warnings go on the partial view when needed.
21279 if In_Extended_Main_Source_Unit (E) then
21280 Set_Referenced (E);
21282 end Set_Completion_Referenced;
21284 ---------------------
21285 -- Set_Default_SSO --
21286 ---------------------
21288 procedure Set_Default_SSO (T : Entity_Id) is
21290 case Opt.Default_SSO is
21294 Set_SSO_Set_Low_By_Default (T, True);
21296 Set_SSO_Set_High_By_Default (T, True);
21298 raise Program_Error;
21300 end Set_Default_SSO;
21302 ---------------------
21303 -- Set_Fixed_Range --
21304 ---------------------
21306 -- The range for fixed-point types is complicated by the fact that we
21307 -- do not know the exact end points at the time of the declaration. This
21308 -- is true for three reasons:
21310 -- A size clause may affect the fudging of the end-points.
21311 -- A small clause may affect the values of the end-points.
21312 -- We try to include the end-points if it does not affect the size.
21314 -- This means that the actual end-points must be established at the
21315 -- point when the type is frozen. Meanwhile, we first narrow the range
21316 -- as permitted (so that it will fit if necessary in a small specified
21317 -- size), and then build a range subtree with these narrowed bounds.
21318 -- Set_Fixed_Range constructs the range from real literal values, and
21319 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21321 -- The parent of this range is set to point to the entity so that it is
21322 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21323 -- other scalar types, which are just pointers to the range in the
21324 -- original tree, this would otherwise be an orphan).
21326 -- The tree is left unanalyzed. When the type is frozen, the processing
21327 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21328 -- analyzed, and uses this as an indication that it should complete
21329 -- work on the range (it will know the final small and size values).
21331 procedure Set_Fixed_Range
21337 S : constant Node_Id :=
21339 Low_Bound => Make_Real_Literal (Loc, Lo),
21340 High_Bound => Make_Real_Literal (Loc, Hi));
21342 Set_Scalar_Range (E, S);
21345 -- Before the freeze point, the bounds of a fixed point are universal
21346 -- and carry the corresponding type.
21348 Set_Etype (Low_Bound (S), Universal_Real);
21349 Set_Etype (High_Bound (S), Universal_Real);
21350 end Set_Fixed_Range;
21352 ----------------------------------
21353 -- Set_Scalar_Range_For_Subtype --
21354 ----------------------------------
21356 procedure Set_Scalar_Range_For_Subtype
21357 (Def_Id : Entity_Id;
21361 Kind : constant Entity_Kind := Ekind (Def_Id);
21364 -- Defend against previous error
21366 if Nkind (R) = N_Error then
21370 Set_Scalar_Range (Def_Id, R);
21372 -- We need to link the range into the tree before resolving it so
21373 -- that types that are referenced, including importantly the subtype
21374 -- itself, are properly frozen (Freeze_Expression requires that the
21375 -- expression be properly linked into the tree). Of course if it is
21376 -- already linked in, then we do not disturb the current link.
21378 if No (Parent (R)) then
21379 Set_Parent (R, Def_Id);
21382 -- Reset the kind of the subtype during analysis of the range, to
21383 -- catch possible premature use in the bounds themselves.
21385 Set_Ekind (Def_Id, E_Void);
21386 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21387 Set_Ekind (Def_Id, Kind);
21388 end Set_Scalar_Range_For_Subtype;
21390 --------------------------------------------------------
21391 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21392 --------------------------------------------------------
21394 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21398 -- Make sure set if encountered during Expand_To_Stored_Constraint
21400 Set_Stored_Constraint (E, No_Elist);
21402 -- Give it the right value
21404 if Is_Constrained (E) and then Has_Discriminants (E) then
21405 Set_Stored_Constraint (E,
21406 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21408 end Set_Stored_Constraint_From_Discriminant_Constraint;
21410 -------------------------------------
21411 -- Signed_Integer_Type_Declaration --
21412 -------------------------------------
21414 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21415 Implicit_Base : Entity_Id;
21416 Base_Typ : Entity_Id;
21419 Errs : Boolean := False;
21423 function Can_Derive_From (E : Entity_Id) return Boolean;
21424 -- Determine whether given bounds allow derivation from specified type
21426 procedure Check_Bound (Expr : Node_Id);
21427 -- Check bound to make sure it is integral and static. If not, post
21428 -- appropriate error message and set Errs flag
21430 ---------------------
21431 -- Can_Derive_From --
21432 ---------------------
21434 -- Note we check both bounds against both end values, to deal with
21435 -- strange types like ones with a range of 0 .. -12341234.
21437 function Can_Derive_From (E : Entity_Id) return Boolean is
21438 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21439 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21441 return Lo <= Lo_Val and then Lo_Val <= Hi
21443 Lo <= Hi_Val and then Hi_Val <= Hi;
21444 end Can_Derive_From;
21450 procedure Check_Bound (Expr : Node_Id) is
21452 -- If a range constraint is used as an integer type definition, each
21453 -- bound of the range must be defined by a static expression of some
21454 -- integer type, but the two bounds need not have the same integer
21455 -- type (Negative bounds are allowed.) (RM 3.5.4)
21457 if not Is_Integer_Type (Etype (Expr)) then
21459 ("integer type definition bounds must be of integer type", Expr);
21462 elsif not Is_OK_Static_Expression (Expr) then
21463 Flag_Non_Static_Expr
21464 ("non-static expression used for integer type bound!", Expr);
21467 -- The bounds are folded into literals, and we set their type to be
21468 -- universal, to avoid typing difficulties: we cannot set the type
21469 -- of the literal to the new type, because this would be a forward
21470 -- reference for the back end, and if the original type is user-
21471 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21474 if Is_Entity_Name (Expr) then
21475 Fold_Uint (Expr, Expr_Value (Expr), True);
21478 Set_Etype (Expr, Universal_Integer);
21482 -- Start of processing for Signed_Integer_Type_Declaration
21485 -- Create an anonymous base type
21488 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21490 -- Analyze and check the bounds, they can be of any integer type
21492 Lo := Low_Bound (Def);
21493 Hi := High_Bound (Def);
21495 -- Arbitrarily use Integer as the type if either bound had an error
21497 if Hi = Error or else Lo = Error then
21498 Base_Typ := Any_Integer;
21499 Set_Error_Posted (T, True);
21501 -- Here both bounds are OK expressions
21504 Analyze_And_Resolve (Lo, Any_Integer);
21505 Analyze_And_Resolve (Hi, Any_Integer);
21511 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21512 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21515 -- Find type to derive from
21517 Lo_Val := Expr_Value (Lo);
21518 Hi_Val := Expr_Value (Hi);
21520 if Can_Derive_From (Standard_Short_Short_Integer) then
21521 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21523 elsif Can_Derive_From (Standard_Short_Integer) then
21524 Base_Typ := Base_Type (Standard_Short_Integer);
21526 elsif Can_Derive_From (Standard_Integer) then
21527 Base_Typ := Base_Type (Standard_Integer);
21529 elsif Can_Derive_From (Standard_Long_Integer) then
21530 Base_Typ := Base_Type (Standard_Long_Integer);
21532 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21533 Check_Restriction (No_Long_Long_Integers, Def);
21534 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21537 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21538 Error_Msg_N ("integer type definition bounds out of range", Def);
21539 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21540 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21544 -- Complete both implicit base and declared first subtype entities. The
21545 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21546 -- are not clobbered when the signed integer type acts as a full view of
21549 Set_Etype (Implicit_Base, Base_Typ);
21550 Set_Size_Info (Implicit_Base, Base_Typ);
21551 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21552 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21553 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21555 Set_Ekind (T, E_Signed_Integer_Subtype);
21556 Set_Etype (T, Implicit_Base);
21557 Set_Size_Info (T, Implicit_Base);
21558 Inherit_Rep_Item_Chain (T, Implicit_Base);
21559 Set_Scalar_Range (T, Def);
21560 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21561 Set_Is_Constrained (T);
21562 end Signed_Integer_Type_Declaration;