1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2017, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Einfo; use Einfo;
33 with Errout; use Errout;
34 with Eval_Fat; use Eval_Fat;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dist; use Exp_Dist;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Fname; use Fname;
42 with Freeze; use Freeze;
43 with Ghost; use Ghost;
44 with Itypes; use Itypes;
45 with Layout; use Layout;
47 with Lib.Xref; use Lib.Xref;
48 with Namet; use Namet;
49 with Nmake; use Nmake;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Case; use Sem_Case;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch7; use Sem_Ch7;
60 with Sem_Ch8; use Sem_Ch8;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Dim; use Sem_Dim;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Res; use Sem_Res;
69 with Sem_Smem; use Sem_Smem;
70 with Sem_Type; use Sem_Type;
71 with Sem_Util; use Sem_Util;
72 with Sem_Warn; use Sem_Warn;
73 with Stand; use Stand;
74 with Sinfo; use Sinfo;
75 with Sinput; use Sinput;
76 with Snames; use Snames;
77 with Targparm; use Targparm;
78 with Tbuild; use Tbuild;
79 with Ttypes; use Ttypes;
80 with Uintp; use Uintp;
81 with Urealp; use Urealp;
83 package body Sem_Ch3 is
85 -----------------------
86 -- Local Subprograms --
87 -----------------------
89 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
90 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
91 -- abstract interface types implemented by a record type or a derived
94 procedure Build_Derived_Type
96 Parent_Type : Entity_Id;
97 Derived_Type : Entity_Id;
98 Is_Completion : Boolean;
99 Derive_Subps : Boolean := True);
100 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
101 -- the N_Full_Type_Declaration node containing the derived type definition.
102 -- Parent_Type is the entity for the parent type in the derived type
103 -- definition and Derived_Type the actual derived type. Is_Completion must
104 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
105 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
106 -- completion of a private type declaration. If Is_Completion is set to
107 -- True, N is the completion of a private type declaration and Derived_Type
108 -- is different from the defining identifier inside N (i.e. Derived_Type /=
109 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
110 -- subprograms should be derived. The only case where this parameter is
111 -- False is when Build_Derived_Type is recursively called to process an
112 -- implicit derived full type for a type derived from a private type (in
113 -- that case the subprograms must only be derived for the private view of
116 -- ??? These flags need a bit of re-examination and re-documentation:
117 -- ??? are they both necessary (both seem related to the recursion)?
119 procedure Build_Derived_Access_Type
121 Parent_Type : Entity_Id;
122 Derived_Type : Entity_Id);
123 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
124 -- create an implicit base if the parent type is constrained or if the
125 -- subtype indication has a constraint.
127 procedure Build_Derived_Array_Type
129 Parent_Type : Entity_Id;
130 Derived_Type : Entity_Id);
131 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
132 -- create an implicit base if the parent type is constrained or if the
133 -- subtype indication has a constraint.
135 procedure Build_Derived_Concurrent_Type
137 Parent_Type : Entity_Id;
138 Derived_Type : Entity_Id);
139 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
140 -- protected type, inherit entries and protected subprograms, check
141 -- legality of discriminant constraints if any.
143 procedure Build_Derived_Enumeration_Type
145 Parent_Type : Entity_Id;
146 Derived_Type : Entity_Id);
147 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
148 -- type, we must create a new list of literals. Types derived from
149 -- Character and [Wide_]Wide_Character are special-cased.
151 procedure Build_Derived_Numeric_Type
153 Parent_Type : Entity_Id;
154 Derived_Type : Entity_Id);
155 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
156 -- an anonymous base type, and propagate constraint to subtype if needed.
158 procedure Build_Derived_Private_Type
160 Parent_Type : Entity_Id;
161 Derived_Type : Entity_Id;
162 Is_Completion : Boolean;
163 Derive_Subps : Boolean := True);
164 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
165 -- because the parent may or may not have a completion, and the derivation
166 -- may itself be a completion.
168 procedure Build_Derived_Record_Type
170 Parent_Type : Entity_Id;
171 Derived_Type : Entity_Id;
172 Derive_Subps : Boolean := True);
173 -- Subsidiary procedure used for tagged and untagged record types
174 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
175 -- All parameters are as in Build_Derived_Type except that N, in
176 -- addition to being an N_Full_Type_Declaration node, can also be an
177 -- N_Private_Extension_Declaration node. See the definition of this routine
178 -- for much more info. Derive_Subps indicates whether subprograms should be
179 -- derived from the parent type. The only case where Derive_Subps is False
180 -- is for an implicit derived full type for a type derived from a private
181 -- type (see Build_Derived_Type).
183 procedure Build_Discriminal (Discrim : Entity_Id);
184 -- Create the discriminal corresponding to discriminant Discrim, that is
185 -- the parameter corresponding to Discrim to be used in initialization
186 -- procedures for the type where Discrim is a discriminant. Discriminals
187 -- are not used during semantic analysis, and are not fully defined
188 -- entities until expansion. Thus they are not given a scope until
189 -- initialization procedures are built.
191 function Build_Discriminant_Constraints
194 Derived_Def : Boolean := False) return Elist_Id;
195 -- Validate discriminant constraints and return the list of the constraints
196 -- in order of discriminant declarations, where T is the discriminated
197 -- unconstrained type. Def is the N_Subtype_Indication node where the
198 -- discriminants constraints for T are specified. Derived_Def is True
199 -- when building the discriminant constraints in a derived type definition
200 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
201 -- type and Def is the constraint "(xxx)" on T and this routine sets the
202 -- Corresponding_Discriminant field of the discriminants in the derived
203 -- type D to point to the corresponding discriminants in the parent type T.
205 procedure Build_Discriminated_Subtype
209 Related_Nod : Node_Id;
210 For_Access : Boolean := False);
211 -- Subsidiary procedure to Constrain_Discriminated_Type and to
212 -- Process_Incomplete_Dependents. Given
214 -- T (a possibly discriminated base type)
215 -- Def_Id (a very partially built subtype for T),
217 -- the call completes Def_Id to be the appropriate E_*_Subtype.
219 -- The Elist is the list of discriminant constraints if any (it is set
220 -- to No_Elist if T is not a discriminated type, and to an empty list if
221 -- T has discriminants but there are no discriminant constraints). The
222 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
223 -- The For_Access says whether or not this subtype is really constraining
224 -- an access type. That is its sole purpose is the designated type of an
225 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
226 -- is built to avoid freezing T when the access subtype is frozen.
228 function Build_Scalar_Bound
231 Der_T : Entity_Id) return Node_Id;
232 -- The bounds of a derived scalar type are conversions of the bounds of
233 -- the parent type. Optimize the representation if the bounds are literals.
234 -- Needs a more complete spec--what are the parameters exactly, and what
235 -- exactly is the returned value, and how is Bound affected???
237 procedure Build_Underlying_Full_View
241 -- If the completion of a private type is itself derived from a private
242 -- type, or if the full view of a private subtype is itself private, the
243 -- back-end has no way to compute the actual size of this type. We build
244 -- an internal subtype declaration of the proper parent type to convey
245 -- this information. This extra mechanism is needed because a full
246 -- view cannot itself have a full view (it would get clobbered during
249 procedure Check_Access_Discriminant_Requires_Limited
252 -- Check the restriction that the type to which an access discriminant
253 -- belongs must be a concurrent type or a descendant of a type with
254 -- the reserved word 'limited' in its declaration.
256 procedure Check_Anonymous_Access_Components
260 Comp_List : Node_Id);
261 -- Ada 2005 AI-382: an access component in a record definition can refer to
262 -- the enclosing record, in which case it denotes the type itself, and not
263 -- the current instance of the type. We create an anonymous access type for
264 -- the component, and flag it as an access to a component, so accessibility
265 -- checks are properly performed on it. The declaration of the access type
266 -- is placed ahead of that of the record to prevent order-of-elaboration
267 -- circularity issues in Gigi. We create an incomplete type for the record
268 -- declaration, which is the designated type of the anonymous access.
270 procedure Check_Delta_Expression (E : Node_Id);
271 -- Check that the expression represented by E is suitable for use as a
272 -- delta expression, i.e. it is of real type and is static.
274 procedure Check_Digits_Expression (E : Node_Id);
275 -- Check that the expression represented by E is suitable for use as a
276 -- digits expression, i.e. it is of integer type, positive and static.
278 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
279 -- Validate the initialization of an object declaration. T is the required
280 -- type, and Exp is the initialization expression.
282 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
283 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
285 procedure Check_Or_Process_Discriminants
288 Prev : Entity_Id := Empty);
289 -- If N is the full declaration of the completion T of an incomplete or
290 -- private type, check its discriminants (which are already known to be
291 -- conformant with those of the partial view, see Find_Type_Name),
292 -- otherwise process them. Prev is the entity of the partial declaration,
295 procedure Check_Real_Bound (Bound : Node_Id);
296 -- Check given bound for being of real type and static. If not, post an
297 -- appropriate message, and rewrite the bound with the real literal zero.
299 procedure Constant_Redeclaration
303 -- Various checks on legality of full declaration of deferred constant.
304 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
305 -- node. The caller has not yet set any attributes of this entity.
307 function Contain_Interface
309 Ifaces : Elist_Id) return Boolean;
310 -- Ada 2005: Determine whether Iface is present in the list Ifaces
312 procedure Convert_Scalar_Bounds
314 Parent_Type : Entity_Id;
315 Derived_Type : Entity_Id;
317 -- For derived scalar types, convert the bounds in the type definition to
318 -- the derived type, and complete their analysis. Given a constraint of the
319 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
320 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
321 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
322 -- subtype are conversions of those bounds to the derived_type, so that
323 -- their typing is consistent.
325 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
326 -- Copies attributes from array base type T2 to array base type T1. Copies
327 -- only attributes that apply to base types, but not subtypes.
329 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
330 -- Copies attributes from array subtype T2 to array subtype T1. Copies
331 -- attributes that apply to both subtypes and base types.
333 procedure Create_Constrained_Components
337 Constraints : Elist_Id);
338 -- Build the list of entities for a constrained discriminated record
339 -- subtype. If a component depends on a discriminant, replace its subtype
340 -- using the discriminant values in the discriminant constraint. Subt
341 -- is the defining identifier for the subtype whose list of constrained
342 -- entities we will create. Decl_Node is the type declaration node where
343 -- we will attach all the itypes created. Typ is the base discriminated
344 -- type for the subtype Subt. Constraints is the list of discriminant
345 -- constraints for Typ.
347 function Constrain_Component_Type
349 Constrained_Typ : Entity_Id;
350 Related_Node : Node_Id;
352 Constraints : Elist_Id) return Entity_Id;
353 -- Given a discriminated base type Typ, a list of discriminant constraints,
354 -- Constraints, for Typ and a component Comp of Typ, create and return the
355 -- type corresponding to Etype (Comp) where all discriminant references
356 -- are replaced with the corresponding constraint. If Etype (Comp) contains
357 -- no discriminant references then it is returned as-is. Constrained_Typ
358 -- is the final constrained subtype to which the constrained component
359 -- belongs. Related_Node is the node where we attach all created itypes.
361 procedure Constrain_Access
362 (Def_Id : in out Entity_Id;
364 Related_Nod : Node_Id);
365 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
366 -- an anonymous type created for a subtype indication. In that case it is
367 -- created in the procedure and attached to Related_Nod.
369 procedure Constrain_Array
370 (Def_Id : in out Entity_Id;
372 Related_Nod : Node_Id;
373 Related_Id : Entity_Id;
375 -- Apply a list of index constraints to an unconstrained array type. The
376 -- first parameter is the entity for the resulting subtype. A value of
377 -- Empty for Def_Id indicates that an implicit type must be created, but
378 -- creation is delayed (and must be done by this procedure) because other
379 -- subsidiary implicit types must be created first (which is why Def_Id
380 -- is an in/out parameter). The second parameter is a subtype indication
381 -- node for the constrained array to be created (e.g. something of the
382 -- form string (1 .. 10)). Related_Nod gives the place where this type
383 -- has to be inserted in the tree. The Related_Id and Suffix parameters
384 -- are used to build the associated Implicit type name.
386 procedure Constrain_Concurrent
387 (Def_Id : in out Entity_Id;
389 Related_Nod : Node_Id;
390 Related_Id : Entity_Id;
392 -- Apply list of discriminant constraints to an unconstrained concurrent
395 -- SI is the N_Subtype_Indication node containing the constraint and
396 -- the unconstrained type to constrain.
398 -- Def_Id is the entity for the resulting constrained subtype. A value
399 -- of Empty for Def_Id indicates that an implicit type must be created,
400 -- but creation is delayed (and must be done by this procedure) because
401 -- other subsidiary implicit types must be created first (which is why
402 -- Def_Id is an in/out parameter).
404 -- Related_Nod gives the place where this type has to be inserted
407 -- The last two arguments are used to create its external name if needed.
409 function Constrain_Corresponding_Record
410 (Prot_Subt : Entity_Id;
411 Corr_Rec : Entity_Id;
412 Related_Nod : Node_Id) return Entity_Id;
413 -- When constraining a protected type or task type with discriminants,
414 -- constrain the corresponding record with the same discriminant values.
416 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
417 -- Constrain a decimal fixed point type with a digits constraint and/or a
418 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
420 procedure Constrain_Discriminated_Type
423 Related_Nod : Node_Id;
424 For_Access : Boolean := False);
425 -- Process discriminant constraints of composite type. Verify that values
426 -- have been provided for all discriminants, that the original type is
427 -- unconstrained, and that the types of the supplied expressions match
428 -- the discriminant types. The first three parameters are like in routine
429 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
432 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
433 -- Constrain an enumeration type with a range constraint. This is identical
434 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
436 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
437 -- Constrain a floating point type with either a digits constraint
438 -- and/or a range constraint, building a E_Floating_Point_Subtype.
440 procedure Constrain_Index
443 Related_Nod : Node_Id;
444 Related_Id : Entity_Id;
447 -- Process an index constraint S in a constrained array declaration. The
448 -- constraint can be a subtype name, or a range with or without an explicit
449 -- subtype mark. The index is the corresponding index of the unconstrained
450 -- array. The Related_Id and Suffix parameters are used to build the
451 -- associated Implicit type name.
453 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
454 -- Build subtype of a signed or modular integer type
456 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
457 -- Constrain an ordinary fixed point type with a range constraint, and
458 -- build an E_Ordinary_Fixed_Point_Subtype entity.
460 procedure Copy_And_Swap (Priv, Full : Entity_Id);
461 -- Copy the Priv entity into the entity of its full declaration then swap
462 -- the two entities in such a manner that the former private type is now
463 -- seen as a full type.
465 procedure Decimal_Fixed_Point_Type_Declaration
468 -- Create a new decimal fixed point type, and apply the constraint to
469 -- obtain a subtype of this new type.
471 procedure Complete_Private_Subtype
474 Full_Base : Entity_Id;
475 Related_Nod : Node_Id);
476 -- Complete the implicit full view of a private subtype by setting the
477 -- appropriate semantic fields. If the full view of the parent is a record
478 -- type, build constrained components of subtype.
480 procedure Derive_Progenitor_Subprograms
481 (Parent_Type : Entity_Id;
482 Tagged_Type : Entity_Id);
483 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
484 -- operations of progenitors of Tagged_Type, and replace the subsidiary
485 -- subtypes with Tagged_Type, to build the specs of the inherited interface
486 -- primitives. The derived primitives are aliased to those of the
487 -- interface. This routine takes care also of transferring to the full view
488 -- subprograms associated with the partial view of Tagged_Type that cover
489 -- interface primitives.
491 procedure Derived_Standard_Character
493 Parent_Type : Entity_Id;
494 Derived_Type : Entity_Id);
495 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
496 -- derivations from types Standard.Character and Standard.Wide_Character.
498 procedure Derived_Type_Declaration
501 Is_Completion : Boolean);
502 -- Process a derived type declaration. Build_Derived_Type is invoked
503 -- to process the actual derived type definition. Parameters N and
504 -- Is_Completion have the same meaning as in Build_Derived_Type.
505 -- T is the N_Defining_Identifier for the entity defined in the
506 -- N_Full_Type_Declaration node N, that is T is the derived type.
508 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
509 -- Insert each literal in symbol table, as an overloadable identifier. Each
510 -- enumeration type is mapped into a sequence of integers, and each literal
511 -- is defined as a constant with integer value. If any of the literals are
512 -- character literals, the type is a character type, which means that
513 -- strings are legal aggregates for arrays of components of the type.
515 function Expand_To_Stored_Constraint
517 Constraint : Elist_Id) return Elist_Id;
518 -- Given a constraint (i.e. a list of expressions) on the discriminants of
519 -- Typ, expand it into a constraint on the stored discriminants and return
520 -- the new list of expressions constraining the stored discriminants.
522 function Find_Type_Of_Object
524 Related_Nod : Node_Id) return Entity_Id;
525 -- Get type entity for object referenced by Obj_Def, attaching the implicit
526 -- types generated to Related_Nod.
528 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
529 -- Create a new float and apply the constraint to obtain subtype of it
531 function Has_Range_Constraint (N : Node_Id) return Boolean;
532 -- Given an N_Subtype_Indication node N, return True if a range constraint
533 -- is present, either directly, or as part of a digits or delta constraint.
534 -- In addition, a digits constraint in the decimal case returns True, since
535 -- it establishes a default range if no explicit range is present.
537 function Inherit_Components
539 Parent_Base : Entity_Id;
540 Derived_Base : Entity_Id;
542 Inherit_Discr : Boolean;
543 Discs : Elist_Id) return Elist_Id;
544 -- Called from Build_Derived_Record_Type to inherit the components of
545 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
546 -- For more information on derived types and component inheritance please
547 -- consult the comment above the body of Build_Derived_Record_Type.
549 -- N is the original derived type declaration
551 -- Is_Tagged is set if we are dealing with tagged types
553 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
554 -- Parent_Base, otherwise no discriminants are inherited.
556 -- Discs gives the list of constraints that apply to Parent_Base in the
557 -- derived type declaration. If Discs is set to No_Elist, then we have
558 -- the following situation:
560 -- type Parent (D1..Dn : ..) is [tagged] record ...;
561 -- type Derived is new Parent [with ...];
563 -- which gets treated as
565 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
567 -- For untagged types the returned value is an association list. The list
568 -- starts from the association (Parent_Base => Derived_Base), and then it
569 -- contains a sequence of the associations of the form
571 -- (Old_Component => New_Component),
573 -- where Old_Component is the Entity_Id of a component in Parent_Base and
574 -- New_Component is the Entity_Id of the corresponding component in
575 -- Derived_Base. For untagged records, this association list is needed when
576 -- copying the record declaration for the derived base. In the tagged case
577 -- the value returned is irrelevant.
579 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
580 -- Propagate static and dynamic predicate flags from a parent to the
581 -- subtype in a subtype declaration with and without constraints.
583 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
584 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
585 -- Determine whether subprogram Subp is a procedure subject to pragma
586 -- Extensions_Visible with value False and has at least one controlling
587 -- parameter of mode OUT.
589 function Is_Valid_Constraint_Kind
591 Constraint_Kind : Node_Kind) return Boolean;
592 -- Returns True if it is legal to apply the given kind of constraint to the
593 -- given kind of type (index constraint to an array type, for example).
595 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
596 -- Create new modular type. Verify that modulus is in bounds
598 procedure New_Concatenation_Op (Typ : Entity_Id);
599 -- Create an abbreviated declaration for an operator in order to
600 -- materialize concatenation on array types.
602 procedure Ordinary_Fixed_Point_Type_Declaration
605 -- Create a new ordinary fixed point type, and apply the constraint to
606 -- obtain subtype of it.
608 procedure Prepare_Private_Subtype_Completion
610 Related_Nod : Node_Id);
611 -- Id is a subtype of some private type. Creates the full declaration
612 -- associated with Id whenever possible, i.e. when the full declaration
613 -- of the base type is already known. Records each subtype into
614 -- Private_Dependents of the base type.
616 procedure Process_Incomplete_Dependents
620 -- Process all entities that depend on an incomplete type. There include
621 -- subtypes, subprogram types that mention the incomplete type in their
622 -- profiles, and subprogram with access parameters that designate the
625 -- Inc_T is the defining identifier of an incomplete type declaration, its
626 -- Ekind is E_Incomplete_Type.
628 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
630 -- Full_T is N's defining identifier.
632 -- Subtypes of incomplete types with discriminants are completed when the
633 -- parent type is. This is simpler than private subtypes, because they can
634 -- only appear in the same scope, and there is no need to exchange views.
635 -- Similarly, access_to_subprogram types may have a parameter or a return
636 -- type that is an incomplete type, and that must be replaced with the
639 -- If the full type is tagged, subprogram with access parameters that
640 -- designated the incomplete may be primitive operations of the full type,
641 -- and have to be processed accordingly.
643 procedure Process_Real_Range_Specification (Def : Node_Id);
644 -- Given the type definition for a real type, this procedure processes and
645 -- checks the real range specification of this type definition if one is
646 -- present. If errors are found, error messages are posted, and the
647 -- Real_Range_Specification of Def is reset to Empty.
649 procedure Record_Type_Declaration
653 -- Process a record type declaration (for both untagged and tagged
654 -- records). Parameters T and N are exactly like in procedure
655 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
656 -- for this routine. If this is the completion of an incomplete type
657 -- declaration, Prev is the entity of the incomplete declaration, used for
658 -- cross-referencing. Otherwise Prev = T.
660 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
661 -- This routine is used to process the actual record type definition (both
662 -- for untagged and tagged records). Def is a record type definition node.
663 -- This procedure analyzes the components in this record type definition.
664 -- Prev_T is the entity for the enclosing record type. It is provided so
665 -- that its Has_Task flag can be set if any of the component have Has_Task
666 -- set. If the declaration is the completion of an incomplete type
667 -- declaration, Prev_T is the original incomplete type, whose full view is
670 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
671 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
672 -- build a copy of the declaration tree of the parent, and we create
673 -- independently the list of components for the derived type. Semantic
674 -- information uses the component entities, but record representation
675 -- clauses are validated on the declaration tree. This procedure replaces
676 -- discriminants and components in the declaration with those that have
677 -- been created by Inherit_Components.
679 procedure Set_Fixed_Range
684 -- Build a range node with the given bounds and set it as the Scalar_Range
685 -- of the given fixed-point type entity. Loc is the source location used
686 -- for the constructed range. See body for further details.
688 procedure Set_Scalar_Range_For_Subtype
692 -- This routine is used to set the scalar range field for a subtype given
693 -- Def_Id, the entity for the subtype, and R, the range expression for the
694 -- scalar range. Subt provides the parent subtype to be used to analyze,
695 -- resolve, and check the given range.
697 procedure Set_Default_SSO (T : Entity_Id);
698 -- T is the entity for an array or record being declared. This procedure
699 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
700 -- to the setting of Opt.Default_SSO.
702 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
703 -- Create a new signed integer entity, and apply the constraint to obtain
704 -- the required first named subtype of this type.
706 procedure Set_Stored_Constraint_From_Discriminant_Constraint
708 -- E is some record type. This routine computes E's Stored_Constraint
709 -- from its Discriminant_Constraint.
711 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
712 -- Check that an entity in a list of progenitors is an interface,
713 -- emit error otherwise.
715 -----------------------
716 -- Access_Definition --
717 -----------------------
719 function Access_Definition
720 (Related_Nod : Node_Id;
721 N : Node_Id) return Entity_Id
723 Anon_Type : Entity_Id;
724 Anon_Scope : Entity_Id;
725 Desig_Type : Entity_Id;
726 Enclosing_Prot_Type : Entity_Id := Empty;
729 Check_SPARK_05_Restriction ("access type is not allowed", N);
731 if Is_Entry (Current_Scope)
732 and then Is_Task_Type (Etype (Scope (Current_Scope)))
734 Error_Msg_N ("task entries cannot have access parameters", N);
738 -- Ada 2005: For an object declaration the corresponding anonymous
739 -- type is declared in the current scope.
741 -- If the access definition is the return type of another access to
742 -- function, scope is the current one, because it is the one of the
743 -- current type declaration, except for the pathological case below.
745 if Nkind_In (Related_Nod, N_Object_Declaration,
746 N_Access_Function_Definition)
748 Anon_Scope := Current_Scope;
750 -- A pathological case: function returning access functions that
751 -- return access functions, etc. Each anonymous access type created
752 -- is in the enclosing scope of the outermost function.
759 while Nkind_In (Par, N_Access_Function_Definition,
765 if Nkind (Par) = N_Function_Specification then
766 Anon_Scope := Scope (Defining_Entity (Par));
770 -- For the anonymous function result case, retrieve the scope of the
771 -- function specification's associated entity rather than using the
772 -- current scope. The current scope will be the function itself if the
773 -- formal part is currently being analyzed, but will be the parent scope
774 -- in the case of a parameterless function, and we always want to use
775 -- the function's parent scope. Finally, if the function is a child
776 -- unit, we must traverse the tree to retrieve the proper entity.
778 elsif Nkind (Related_Nod) = N_Function_Specification
779 and then Nkind (Parent (N)) /= N_Parameter_Specification
781 -- If the current scope is a protected type, the anonymous access
782 -- is associated with one of the protected operations, and must
783 -- be available in the scope that encloses the protected declaration.
784 -- Otherwise the type is in the scope enclosing the subprogram.
786 -- If the function has formals, The return type of a subprogram
787 -- declaration is analyzed in the scope of the subprogram (see
788 -- Process_Formals) and thus the protected type, if present, is
789 -- the scope of the current function scope.
791 if Ekind (Current_Scope) = E_Protected_Type then
792 Enclosing_Prot_Type := Current_Scope;
794 elsif Ekind (Current_Scope) = E_Function
795 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
797 Enclosing_Prot_Type := Scope (Current_Scope);
800 if Present (Enclosing_Prot_Type) then
801 Anon_Scope := Scope (Enclosing_Prot_Type);
804 Anon_Scope := Scope (Defining_Entity (Related_Nod));
807 -- For an access type definition, if the current scope is a child
808 -- unit it is the scope of the type.
810 elsif Is_Compilation_Unit (Current_Scope) then
811 Anon_Scope := Current_Scope;
813 -- For access formals, access components, and access discriminants, the
814 -- scope is that of the enclosing declaration,
817 Anon_Scope := Scope (Current_Scope);
822 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
825 and then Ada_Version >= Ada_2005
827 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
830 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
831 -- the corresponding semantic routine
833 if Present (Access_To_Subprogram_Definition (N)) then
835 -- Compiler runtime units are compiled in Ada 2005 mode when building
836 -- the runtime library but must also be compilable in Ada 95 mode
837 -- (when bootstrapping the compiler).
839 Check_Compiler_Unit ("anonymous access to subprogram", N);
841 Access_Subprogram_Declaration
842 (T_Name => Anon_Type,
843 T_Def => Access_To_Subprogram_Definition (N));
845 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
847 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
849 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
852 Set_Can_Use_Internal_Rep
853 (Anon_Type, not Always_Compatible_Rep_On_Target);
855 -- If the anonymous access is associated with a protected operation,
856 -- create a reference to it after the enclosing protected definition
857 -- because the itype will be used in the subsequent bodies.
859 -- If the anonymous access itself is protected, a full type
860 -- declaratiton will be created for it, so that the equivalent
861 -- record type can be constructed. For further details, see
862 -- Replace_Anonymous_Access_To_Protected-Subprogram.
864 if Ekind (Current_Scope) = E_Protected_Type
865 and then not Protected_Present (Access_To_Subprogram_Definition (N))
867 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
873 Find_Type (Subtype_Mark (N));
874 Desig_Type := Entity (Subtype_Mark (N));
876 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
877 Set_Etype (Anon_Type, Anon_Type);
879 -- Make sure the anonymous access type has size and alignment fields
880 -- set, as required by gigi. This is necessary in the case of the
881 -- Task_Body_Procedure.
883 if not Has_Private_Component (Desig_Type) then
884 Layout_Type (Anon_Type);
887 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
888 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
889 -- the null value is allowed. In Ada 95 the null value is never allowed.
891 if Ada_Version >= Ada_2005 then
892 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
894 Set_Can_Never_Be_Null (Anon_Type, True);
897 -- The anonymous access type is as public as the discriminated type or
898 -- subprogram that defines it. It is imported (for back-end purposes)
899 -- if the designated type is.
901 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
903 -- Ada 2005 (AI-231): Propagate the access-constant attribute
905 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
907 -- The context is either a subprogram declaration, object declaration,
908 -- or an access discriminant, in a private or a full type declaration.
909 -- In the case of a subprogram, if the designated type is incomplete,
910 -- the operation will be a primitive operation of the full type, to be
911 -- updated subsequently. If the type is imported through a limited_with
912 -- clause, the subprogram is not a primitive operation of the type
913 -- (which is declared elsewhere in some other scope).
915 if Ekind (Desig_Type) = E_Incomplete_Type
916 and then not From_Limited_With (Desig_Type)
917 and then Is_Overloadable (Current_Scope)
919 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
920 Set_Has_Delayed_Freeze (Current_Scope);
923 -- Ada 2005: If the designated type is an interface that may contain
924 -- tasks, create a Master entity for the declaration. This must be done
925 -- before expansion of the full declaration, because the declaration may
926 -- include an expression that is an allocator, whose expansion needs the
927 -- proper Master for the created tasks.
929 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
931 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
933 Build_Class_Wide_Master (Anon_Type);
935 -- Similarly, if the type is an anonymous access that designates
936 -- tasks, create a master entity for it in the current context.
938 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
940 Build_Master_Entity (Defining_Identifier (Related_Nod));
941 Build_Master_Renaming (Anon_Type);
945 -- For a private component of a protected type, it is imperative that
946 -- the back-end elaborate the type immediately after the protected
947 -- declaration, because this type will be used in the declarations
948 -- created for the component within each protected body, so we must
949 -- create an itype reference for it now.
951 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
952 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
954 -- Similarly, if the access definition is the return result of a
955 -- function, create an itype reference for it because it will be used
956 -- within the function body. For a regular function that is not a
957 -- compilation unit, insert reference after the declaration. For a
958 -- protected operation, insert it after the enclosing protected type
959 -- declaration. In either case, do not create a reference for a type
960 -- obtained through a limited_with clause, because this would introduce
961 -- semantic dependencies.
963 -- Similarly, do not create a reference if the designated type is a
964 -- generic formal, because no use of it will reach the backend.
966 elsif Nkind (Related_Nod) = N_Function_Specification
967 and then not From_Limited_With (Desig_Type)
968 and then not Is_Generic_Type (Desig_Type)
970 if Present (Enclosing_Prot_Type) then
971 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
973 elsif Is_List_Member (Parent (Related_Nod))
974 and then Nkind (Parent (N)) /= N_Parameter_Specification
976 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
979 -- Finally, create an itype reference for an object declaration of an
980 -- anonymous access type. This is strictly necessary only for deferred
981 -- constants, but in any case will avoid out-of-scope problems in the
984 elsif Nkind (Related_Nod) = N_Object_Declaration then
985 Build_Itype_Reference (Anon_Type, Related_Nod);
989 end Access_Definition;
991 -----------------------------------
992 -- Access_Subprogram_Declaration --
993 -----------------------------------
995 procedure Access_Subprogram_Declaration
999 procedure Check_For_Premature_Usage (Def : Node_Id);
1000 -- Check that type T_Name is not used, directly or recursively, as a
1001 -- parameter or a return type in Def. Def is either a subtype, an
1002 -- access_definition, or an access_to_subprogram_definition.
1004 -------------------------------
1005 -- Check_For_Premature_Usage --
1006 -------------------------------
1008 procedure Check_For_Premature_Usage (Def : Node_Id) is
1012 -- Check for a subtype mark
1014 if Nkind (Def) in N_Has_Etype then
1015 if Etype (Def) = T_Name then
1017 ("type& cannot be used before end of its declaration", Def);
1020 -- If this is not a subtype, then this is an access_definition
1022 elsif Nkind (Def) = N_Access_Definition then
1023 if Present (Access_To_Subprogram_Definition (Def)) then
1024 Check_For_Premature_Usage
1025 (Access_To_Subprogram_Definition (Def));
1027 Check_For_Premature_Usage (Subtype_Mark (Def));
1030 -- The only cases left are N_Access_Function_Definition and
1031 -- N_Access_Procedure_Definition.
1034 if Present (Parameter_Specifications (Def)) then
1035 Param := First (Parameter_Specifications (Def));
1036 while Present (Param) loop
1037 Check_For_Premature_Usage (Parameter_Type (Param));
1038 Param := Next (Param);
1042 if Nkind (Def) = N_Access_Function_Definition then
1043 Check_For_Premature_Usage (Result_Definition (Def));
1046 end Check_For_Premature_Usage;
1050 Formals : constant List_Id := Parameter_Specifications (T_Def);
1053 Desig_Type : constant Entity_Id :=
1054 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1056 -- Start of processing for Access_Subprogram_Declaration
1059 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1061 -- Associate the Itype node with the inner full-type declaration or
1062 -- subprogram spec or entry body. This is required to handle nested
1063 -- anonymous declarations. For example:
1066 -- (X : access procedure
1067 -- (Y : access procedure
1070 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1071 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1072 N_Private_Type_Declaration,
1073 N_Private_Extension_Declaration,
1074 N_Procedure_Specification,
1075 N_Function_Specification,
1079 Nkind_In (D_Ityp, N_Object_Declaration,
1080 N_Object_Renaming_Declaration,
1081 N_Formal_Object_Declaration,
1082 N_Formal_Type_Declaration,
1083 N_Task_Type_Declaration,
1084 N_Protected_Type_Declaration))
1086 D_Ityp := Parent (D_Ityp);
1087 pragma Assert (D_Ityp /= Empty);
1090 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1092 if Nkind_In (D_Ityp, N_Procedure_Specification,
1093 N_Function_Specification)
1095 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1097 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1098 N_Object_Declaration,
1099 N_Object_Renaming_Declaration,
1100 N_Formal_Type_Declaration)
1102 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1105 if Nkind (T_Def) = N_Access_Function_Definition then
1106 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1108 Acc : constant Node_Id := Result_Definition (T_Def);
1111 if Present (Access_To_Subprogram_Definition (Acc))
1113 Protected_Present (Access_To_Subprogram_Definition (Acc))
1117 Replace_Anonymous_Access_To_Protected_Subprogram
1123 Access_Definition (T_Def, Result_Definition (T_Def)));
1128 Analyze (Result_Definition (T_Def));
1131 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1134 -- If a null exclusion is imposed on the result type, then
1135 -- create a null-excluding itype (an access subtype) and use
1136 -- it as the function's Etype.
1138 if Is_Access_Type (Typ)
1139 and then Null_Exclusion_In_Return_Present (T_Def)
1141 Set_Etype (Desig_Type,
1142 Create_Null_Excluding_Itype
1144 Related_Nod => T_Def,
1145 Scope_Id => Current_Scope));
1148 if From_Limited_With (Typ) then
1150 -- AI05-151: Incomplete types are allowed in all basic
1151 -- declarations, including access to subprograms.
1153 if Ada_Version >= Ada_2012 then
1158 ("illegal use of incomplete type&",
1159 Result_Definition (T_Def), Typ);
1162 elsif Ekind (Current_Scope) = E_Package
1163 and then In_Private_Part (Current_Scope)
1165 if Ekind (Typ) = E_Incomplete_Type then
1166 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1168 elsif Is_Class_Wide_Type (Typ)
1169 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1172 (Desig_Type, Private_Dependents (Etype (Typ)));
1176 Set_Etype (Desig_Type, Typ);
1181 if not (Is_Type (Etype (Desig_Type))) then
1183 ("expect type in function specification",
1184 Result_Definition (T_Def));
1188 Set_Etype (Desig_Type, Standard_Void_Type);
1191 if Present (Formals) then
1192 Push_Scope (Desig_Type);
1194 -- Some special tests here. These special tests can be removed
1195 -- if and when Itypes always have proper parent pointers to their
1198 -- Special test 1) Link defining_identifier of formals. Required by
1199 -- First_Formal to provide its functionality.
1205 F := First (Formals);
1207 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1208 -- when it is part of an unconstrained type and subtype expansion
1209 -- is disabled. To avoid back-end problems with shared profiles,
1210 -- use previous subprogram type as the designated type, and then
1211 -- remove scope added above.
1213 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1215 Set_Etype (T_Name, T_Name);
1216 Init_Size_Align (T_Name);
1217 Set_Directly_Designated_Type (T_Name,
1218 Scope (Defining_Identifier (F)));
1223 while Present (F) loop
1224 if No (Parent (Defining_Identifier (F))) then
1225 Set_Parent (Defining_Identifier (F), F);
1232 Process_Formals (Formals, Parent (T_Def));
1234 -- Special test 2) End_Scope requires that the parent pointer be set
1235 -- to something reasonable, but Itypes don't have parent pointers. So
1236 -- we set it and then unset it ???
1238 Set_Parent (Desig_Type, T_Name);
1240 Set_Parent (Desig_Type, Empty);
1243 -- Check for premature usage of the type being defined
1245 Check_For_Premature_Usage (T_Def);
1247 -- The return type and/or any parameter type may be incomplete. Mark the
1248 -- subprogram_type as depending on the incomplete type, so that it can
1249 -- be updated when the full type declaration is seen. This only applies
1250 -- to incomplete types declared in some enclosing scope, not to limited
1251 -- views from other packages.
1253 -- Prior to Ada 2012, access to functions can only have in_parameters.
1255 if Present (Formals) then
1256 Formal := First_Formal (Desig_Type);
1257 while Present (Formal) loop
1258 if Ekind (Formal) /= E_In_Parameter
1259 and then Nkind (T_Def) = N_Access_Function_Definition
1260 and then Ada_Version < Ada_2012
1262 Error_Msg_N ("functions can only have IN parameters", Formal);
1265 if Ekind (Etype (Formal)) = E_Incomplete_Type
1266 and then In_Open_Scopes (Scope (Etype (Formal)))
1268 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1269 Set_Has_Delayed_Freeze (Desig_Type);
1272 Next_Formal (Formal);
1276 -- Check whether an indirect call without actuals may be possible. This
1277 -- is used when resolving calls whose result is then indexed.
1279 May_Need_Actuals (Desig_Type);
1281 -- If the return type is incomplete, this is legal as long as the type
1282 -- is declared in the current scope and will be completed in it (rather
1283 -- than being part of limited view).
1285 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1286 and then not Has_Delayed_Freeze (Desig_Type)
1287 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1289 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1290 Set_Has_Delayed_Freeze (Desig_Type);
1293 Check_Delayed_Subprogram (Desig_Type);
1295 if Protected_Present (T_Def) then
1296 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1297 Set_Convention (Desig_Type, Convention_Protected);
1299 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1302 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1304 Set_Etype (T_Name, T_Name);
1305 Init_Size_Align (T_Name);
1306 Set_Directly_Designated_Type (T_Name, Desig_Type);
1308 Generate_Reference_To_Formals (T_Name);
1310 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1312 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1314 Check_Restriction (No_Access_Subprograms, T_Def);
1315 end Access_Subprogram_Declaration;
1317 ----------------------------
1318 -- Access_Type_Declaration --
1319 ----------------------------
1321 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1322 P : constant Node_Id := Parent (Def);
1323 S : constant Node_Id := Subtype_Indication (Def);
1325 Full_Desig : Entity_Id;
1328 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1330 -- Check for permissible use of incomplete type
1332 if Nkind (S) /= N_Subtype_Indication then
1335 if Present (Entity (S))
1336 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1338 Set_Directly_Designated_Type (T, Entity (S));
1340 -- If the designated type is a limited view, we cannot tell if
1341 -- the full view contains tasks, and there is no way to handle
1342 -- that full view in a client. We create a master entity for the
1343 -- scope, which will be used when a client determines that one
1346 if From_Limited_With (Entity (S))
1347 and then not Is_Class_Wide_Type (Entity (S))
1349 Set_Ekind (T, E_Access_Type);
1350 Build_Master_Entity (T);
1351 Build_Master_Renaming (T);
1355 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1358 -- If the access definition is of the form: ACCESS NOT NULL ..
1359 -- the subtype indication must be of an access type. Create
1360 -- a null-excluding subtype of it.
1362 if Null_Excluding_Subtype (Def) then
1363 if not Is_Access_Type (Entity (S)) then
1364 Error_Msg_N ("null exclusion must apply to access type", Def);
1368 Loc : constant Source_Ptr := Sloc (S);
1370 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1374 Make_Subtype_Declaration (Loc,
1375 Defining_Identifier => Nam,
1376 Subtype_Indication =>
1377 New_Occurrence_Of (Entity (S), Loc));
1378 Set_Null_Exclusion_Present (Decl);
1379 Insert_Before (Parent (Def), Decl);
1381 Set_Entity (S, Nam);
1387 Set_Directly_Designated_Type (T,
1388 Process_Subtype (S, P, T, 'P'));
1391 if All_Present (Def) or Constant_Present (Def) then
1392 Set_Ekind (T, E_General_Access_Type);
1394 Set_Ekind (T, E_Access_Type);
1397 Full_Desig := Designated_Type (T);
1399 if Base_Type (Full_Desig) = T then
1400 Error_Msg_N ("access type cannot designate itself", S);
1402 -- In Ada 2005, the type may have a limited view through some unit in
1403 -- its own context, allowing the following circularity that cannot be
1404 -- detected earlier.
1406 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1409 ("access type cannot designate its own class-wide type", S);
1411 -- Clean up indication of tagged status to prevent cascaded errors
1413 Set_Is_Tagged_Type (T, False);
1418 -- If the type has appeared already in a with_type clause, it is frozen
1419 -- and the pointer size is already set. Else, initialize.
1421 if not From_Limited_With (T) then
1422 Init_Size_Align (T);
1425 -- Note that Has_Task is always false, since the access type itself
1426 -- is not a task type. See Einfo for more description on this point.
1427 -- Exactly the same consideration applies to Has_Controlled_Component
1428 -- and to Has_Protected.
1430 Set_Has_Task (T, False);
1431 Set_Has_Protected (T, False);
1432 Set_Has_Timing_Event (T, False);
1433 Set_Has_Controlled_Component (T, False);
1435 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1436 -- problems where an incomplete view of this entity has been previously
1437 -- established by a limited with and an overlaid version of this field
1438 -- (Stored_Constraint) was initialized for the incomplete view.
1440 -- This reset is performed in most cases except where the access type
1441 -- has been created for the purposes of allocating or deallocating a
1442 -- build-in-place object. Such access types have explicitly set pools
1443 -- and finalization masters.
1445 if No (Associated_Storage_Pool (T)) then
1446 Set_Finalization_Master (T, Empty);
1449 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1452 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1453 Set_Is_Access_Constant (T, Constant_Present (Def));
1454 end Access_Type_Declaration;
1456 ----------------------------------
1457 -- Add_Interface_Tag_Components --
1458 ----------------------------------
1460 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1461 Loc : constant Source_Ptr := Sloc (N);
1465 procedure Add_Tag (Iface : Entity_Id);
1466 -- Add tag for one of the progenitor interfaces
1472 procedure Add_Tag (Iface : Entity_Id) is
1479 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1481 -- This is a reasonable place to propagate predicates
1483 if Has_Predicates (Iface) then
1484 Set_Has_Predicates (Typ);
1488 Make_Component_Definition (Loc,
1489 Aliased_Present => True,
1490 Subtype_Indication =>
1491 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1493 Tag := Make_Temporary (Loc, 'V');
1496 Make_Component_Declaration (Loc,
1497 Defining_Identifier => Tag,
1498 Component_Definition => Def);
1500 Analyze_Component_Declaration (Decl);
1502 Set_Analyzed (Decl);
1503 Set_Ekind (Tag, E_Component);
1505 Set_Is_Aliased (Tag);
1506 Set_Related_Type (Tag, Iface);
1507 Init_Component_Location (Tag);
1509 pragma Assert (Is_Frozen (Iface));
1511 Set_DT_Entry_Count (Tag,
1512 DT_Entry_Count (First_Entity (Iface)));
1514 if No (Last_Tag) then
1517 Insert_After (Last_Tag, Decl);
1522 -- If the ancestor has discriminants we need to give special support
1523 -- to store the offset_to_top value of the secondary dispatch tables.
1524 -- For this purpose we add a supplementary component just after the
1525 -- field that contains the tag associated with each secondary DT.
1527 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1529 Make_Component_Definition (Loc,
1530 Subtype_Indication =>
1531 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1533 Offset := Make_Temporary (Loc, 'V');
1536 Make_Component_Declaration (Loc,
1537 Defining_Identifier => Offset,
1538 Component_Definition => Def);
1540 Analyze_Component_Declaration (Decl);
1542 Set_Analyzed (Decl);
1543 Set_Ekind (Offset, E_Component);
1544 Set_Is_Aliased (Offset);
1545 Set_Related_Type (Offset, Iface);
1546 Init_Component_Location (Offset);
1547 Insert_After (Last_Tag, Decl);
1558 -- Start of processing for Add_Interface_Tag_Components
1561 if not RTE_Available (RE_Interface_Tag) then
1563 ("(Ada 2005) interface types not supported by this run-time!",
1568 if Ekind (Typ) /= E_Record_Type
1569 or else (Is_Concurrent_Record_Type (Typ)
1570 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1571 or else (not Is_Concurrent_Record_Type (Typ)
1572 and then No (Interfaces (Typ))
1573 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1578 -- Find the current last tag
1580 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1581 Ext := Record_Extension_Part (Type_Definition (N));
1583 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1584 Ext := Type_Definition (N);
1589 if not (Present (Component_List (Ext))) then
1590 Set_Null_Present (Ext, False);
1592 Set_Component_List (Ext,
1593 Make_Component_List (Loc,
1594 Component_Items => L,
1595 Null_Present => False));
1597 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1598 L := Component_Items
1600 (Record_Extension_Part
1601 (Type_Definition (N))));
1603 L := Component_Items
1605 (Type_Definition (N)));
1608 -- Find the last tag component
1611 while Present (Comp) loop
1612 if Nkind (Comp) = N_Component_Declaration
1613 and then Is_Tag (Defining_Identifier (Comp))
1622 -- At this point L references the list of components and Last_Tag
1623 -- references the current last tag (if any). Now we add the tag
1624 -- corresponding with all the interfaces that are not implemented
1627 if Present (Interfaces (Typ)) then
1628 Elmt := First_Elmt (Interfaces (Typ));
1629 while Present (Elmt) loop
1630 Add_Tag (Node (Elmt));
1634 end Add_Interface_Tag_Components;
1636 -------------------------------------
1637 -- Add_Internal_Interface_Entities --
1638 -------------------------------------
1640 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1643 Iface_Elmt : Elmt_Id;
1644 Iface_Prim : Entity_Id;
1645 Ifaces_List : Elist_Id;
1646 New_Subp : Entity_Id := Empty;
1648 Restore_Scope : Boolean := False;
1651 pragma Assert (Ada_Version >= Ada_2005
1652 and then Is_Record_Type (Tagged_Type)
1653 and then Is_Tagged_Type (Tagged_Type)
1654 and then Has_Interfaces (Tagged_Type)
1655 and then not Is_Interface (Tagged_Type));
1657 -- Ensure that the internal entities are added to the scope of the type
1659 if Scope (Tagged_Type) /= Current_Scope then
1660 Push_Scope (Scope (Tagged_Type));
1661 Restore_Scope := True;
1664 Collect_Interfaces (Tagged_Type, Ifaces_List);
1666 Iface_Elmt := First_Elmt (Ifaces_List);
1667 while Present (Iface_Elmt) loop
1668 Iface := Node (Iface_Elmt);
1670 -- Originally we excluded here from this processing interfaces that
1671 -- are parents of Tagged_Type because their primitives are located
1672 -- in the primary dispatch table (and hence no auxiliary internal
1673 -- entities are required to handle secondary dispatch tables in such
1674 -- case). However, these auxiliary entities are also required to
1675 -- handle derivations of interfaces in formals of generics (see
1676 -- Derive_Subprograms).
1678 Elmt := First_Elmt (Primitive_Operations (Iface));
1679 while Present (Elmt) loop
1680 Iface_Prim := Node (Elmt);
1682 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1684 Find_Primitive_Covering_Interface
1685 (Tagged_Type => Tagged_Type,
1686 Iface_Prim => Iface_Prim);
1688 if No (Prim) and then Serious_Errors_Detected > 0 then
1692 pragma Assert (Present (Prim));
1694 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1695 -- differs from the name of the interface primitive then it is
1696 -- a private primitive inherited from a parent type. In such
1697 -- case, given that Tagged_Type covers the interface, the
1698 -- inherited private primitive becomes visible. For such
1699 -- purpose we add a new entity that renames the inherited
1700 -- private primitive.
1702 if Chars (Prim) /= Chars (Iface_Prim) then
1703 pragma Assert (Has_Suffix (Prim, 'P'));
1705 (New_Subp => New_Subp,
1706 Parent_Subp => Iface_Prim,
1707 Derived_Type => Tagged_Type,
1708 Parent_Type => Iface);
1709 Set_Alias (New_Subp, Prim);
1710 Set_Is_Abstract_Subprogram
1711 (New_Subp, Is_Abstract_Subprogram (Prim));
1715 (New_Subp => New_Subp,
1716 Parent_Subp => Iface_Prim,
1717 Derived_Type => Tagged_Type,
1718 Parent_Type => Iface);
1723 if Is_Inherited_Operation (Prim)
1724 and then Present (Alias (Prim))
1726 Anc := Alias (Prim);
1728 Anc := Overridden_Operation (Prim);
1731 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1732 -- nonconforming preconditions in both an ancestor and
1733 -- a progenitor operation.
1736 and then Has_Non_Trivial_Precondition (Anc)
1737 and then Has_Non_Trivial_Precondition (Iface_Prim)
1739 if Is_Abstract_Subprogram (Prim)
1741 (Ekind (Prim) = E_Procedure
1742 and then Nkind (Parent (Prim)) =
1743 N_Procedure_Specification
1744 and then Null_Present (Parent (Prim)))
1748 -- The inherited operation must be overridden
1750 elsif not Comes_From_Source (Prim) then
1752 ("&inherits non-conforming preconditions and must "
1753 & "be overridden (RM 6.1.1 (10-16)",
1754 Parent (Tagged_Type), Prim);
1759 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1760 -- associated with interface types. These entities are
1761 -- only registered in the list of primitives of its
1762 -- corresponding tagged type because they are only used
1763 -- to fill the contents of the secondary dispatch tables.
1764 -- Therefore they are removed from the homonym chains.
1766 Set_Is_Hidden (New_Subp);
1767 Set_Is_Internal (New_Subp);
1768 Set_Alias (New_Subp, Prim);
1769 Set_Is_Abstract_Subprogram
1770 (New_Subp, Is_Abstract_Subprogram (Prim));
1771 Set_Interface_Alias (New_Subp, Iface_Prim);
1773 -- If the returned type is an interface then propagate it to
1774 -- the returned type. Needed by the thunk to generate the code
1775 -- which displaces "this" to reference the corresponding
1776 -- secondary dispatch table in the returned object.
1778 if Is_Interface (Etype (Iface_Prim)) then
1779 Set_Etype (New_Subp, Etype (Iface_Prim));
1782 -- Internal entities associated with interface types are only
1783 -- registered in the list of primitives of the tagged type.
1784 -- They are only used to fill the contents of the secondary
1785 -- dispatch tables. Therefore they are not needed in the
1788 Remove_Homonym (New_Subp);
1790 -- Hidden entities associated with interfaces must have set
1791 -- the Has_Delay_Freeze attribute to ensure that, in case
1792 -- of locally defined tagged types (or compiling with static
1793 -- dispatch tables generation disabled) the corresponding
1794 -- entry of the secondary dispatch table is filled when such
1795 -- an entity is frozen. This is an expansion activity that must
1796 -- be suppressed for ASIS because it leads to gigi elaboration
1797 -- issues in annotate mode.
1799 if not ASIS_Mode then
1800 Set_Has_Delayed_Freeze (New_Subp);
1808 Next_Elmt (Iface_Elmt);
1811 if Restore_Scope then
1814 end Add_Internal_Interface_Entities;
1816 -----------------------------------
1817 -- Analyze_Component_Declaration --
1818 -----------------------------------
1820 procedure Analyze_Component_Declaration (N : Node_Id) is
1821 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1822 Id : constant Entity_Id := Defining_Identifier (N);
1823 E : constant Node_Id := Expression (N);
1824 Typ : constant Node_Id :=
1825 Subtype_Indication (Component_Definition (N));
1829 function Contains_POC (Constr : Node_Id) return Boolean;
1830 -- Determines whether a constraint uses the discriminant of a record
1831 -- type thus becoming a per-object constraint (POC).
1833 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1834 -- Typ is the type of the current component, check whether this type is
1835 -- a limited type. Used to validate declaration against that of
1836 -- enclosing record.
1842 function Contains_POC (Constr : Node_Id) return Boolean is
1844 -- Prevent cascaded errors
1846 if Error_Posted (Constr) then
1850 case Nkind (Constr) is
1851 when N_Attribute_Reference =>
1852 return Attribute_Name (Constr) = Name_Access
1853 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1855 when N_Discriminant_Association =>
1856 return Denotes_Discriminant (Expression (Constr));
1858 when N_Identifier =>
1859 return Denotes_Discriminant (Constr);
1861 when N_Index_Or_Discriminant_Constraint =>
1866 IDC := First (Constraints (Constr));
1867 while Present (IDC) loop
1869 -- One per-object constraint is sufficient
1871 if Contains_POC (IDC) then
1882 return Denotes_Discriminant (Low_Bound (Constr))
1884 Denotes_Discriminant (High_Bound (Constr));
1886 when N_Range_Constraint =>
1887 return Denotes_Discriminant (Range_Expression (Constr));
1894 ----------------------
1895 -- Is_Known_Limited --
1896 ----------------------
1898 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1899 P : constant Entity_Id := Etype (Typ);
1900 R : constant Entity_Id := Root_Type (Typ);
1903 if Is_Limited_Record (Typ) then
1906 -- If the root type is limited (and not a limited interface)
1907 -- so is the current type
1909 elsif Is_Limited_Record (R)
1910 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1914 -- Else the type may have a limited interface progenitor, but a
1915 -- limited record parent.
1917 elsif R /= P and then Is_Limited_Record (P) then
1923 end Is_Known_Limited;
1925 -- Start of processing for Analyze_Component_Declaration
1928 Generate_Definition (Id);
1931 if Present (Typ) then
1932 T := Find_Type_Of_Object
1933 (Subtype_Indication (Component_Definition (N)), N);
1935 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1936 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1939 -- Ada 2005 (AI-230): Access Definition case
1942 pragma Assert (Present
1943 (Access_Definition (Component_Definition (N))));
1945 T := Access_Definition
1947 N => Access_Definition (Component_Definition (N)));
1948 Set_Is_Local_Anonymous_Access (T);
1950 -- Ada 2005 (AI-254)
1952 if Present (Access_To_Subprogram_Definition
1953 (Access_Definition (Component_Definition (N))))
1954 and then Protected_Present (Access_To_Subprogram_Definition
1956 (Component_Definition (N))))
1958 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1962 -- If the subtype is a constrained subtype of the enclosing record,
1963 -- (which must have a partial view) the back-end does not properly
1964 -- handle the recursion. Rewrite the component declaration with an
1965 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1966 -- the tree directly because side effects have already been removed from
1967 -- discriminant constraints.
1969 if Ekind (T) = E_Access_Subtype
1970 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1971 and then Comes_From_Source (T)
1972 and then Nkind (Parent (T)) = N_Subtype_Declaration
1973 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1976 (Subtype_Indication (Component_Definition (N)),
1977 New_Copy_Tree (Subtype_Indication (Parent (T))));
1978 T := Find_Type_Of_Object
1979 (Subtype_Indication (Component_Definition (N)), N);
1982 -- If the component declaration includes a default expression, then we
1983 -- check that the component is not of a limited type (RM 3.7(5)),
1984 -- and do the special preanalysis of the expression (see section on
1985 -- "Handling of Default and Per-Object Expressions" in the spec of
1989 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1990 Preanalyze_Default_Expression (E, T);
1991 Check_Initialization (T, E);
1993 if Ada_Version >= Ada_2005
1994 and then Ekind (T) = E_Anonymous_Access_Type
1995 and then Etype (E) /= Any_Type
1997 -- Check RM 3.9.2(9): "if the expected type for an expression is
1998 -- an anonymous access-to-specific tagged type, then the object
1999 -- designated by the expression shall not be dynamically tagged
2000 -- unless it is a controlling operand in a call on a dispatching
2003 if Is_Tagged_Type (Directly_Designated_Type (T))
2005 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2007 Ekind (Directly_Designated_Type (Etype (E))) =
2011 ("access to specific tagged type required (RM 3.9.2(9))", E);
2014 -- (Ada 2005: AI-230): Accessibility check for anonymous
2017 if Type_Access_Level (Etype (E)) >
2018 Deepest_Type_Access_Level (T)
2021 ("expression has deeper access level than component " &
2022 "(RM 3.10.2 (12.2))", E);
2025 -- The initialization expression is a reference to an access
2026 -- discriminant. The type of the discriminant is always deeper
2027 -- than any access type.
2029 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2030 and then Is_Entity_Name (E)
2031 and then Ekind (Entity (E)) = E_In_Parameter
2032 and then Present (Discriminal_Link (Entity (E)))
2035 ("discriminant has deeper accessibility level than target",
2041 -- The parent type may be a private view with unknown discriminants,
2042 -- and thus unconstrained. Regular components must be constrained.
2044 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2045 if Is_Class_Wide_Type (T) then
2047 ("class-wide subtype with unknown discriminants" &
2048 " in component declaration",
2049 Subtype_Indication (Component_Definition (N)));
2052 ("unconstrained subtype in component declaration",
2053 Subtype_Indication (Component_Definition (N)));
2056 -- Components cannot be abstract, except for the special case of
2057 -- the _Parent field (case of extending an abstract tagged type)
2059 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2060 Error_Msg_N ("type of a component cannot be abstract", N);
2064 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2066 -- The component declaration may have a per-object constraint, set
2067 -- the appropriate flag in the defining identifier of the subtype.
2069 if Present (Subtype_Indication (Component_Definition (N))) then
2071 Sindic : constant Node_Id :=
2072 Subtype_Indication (Component_Definition (N));
2074 if Nkind (Sindic) = N_Subtype_Indication
2075 and then Present (Constraint (Sindic))
2076 and then Contains_POC (Constraint (Sindic))
2078 Set_Has_Per_Object_Constraint (Id);
2083 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2084 -- out some static checks.
2086 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2087 Null_Exclusion_Static_Checks (N);
2090 -- If this component is private (or depends on a private type), flag the
2091 -- record type to indicate that some operations are not available.
2093 P := Private_Component (T);
2097 -- Check for circular definitions
2099 if P = Any_Type then
2100 Set_Etype (Id, Any_Type);
2102 -- There is a gap in the visibility of operations only if the
2103 -- component type is not defined in the scope of the record type.
2105 elsif Scope (P) = Scope (Current_Scope) then
2108 elsif Is_Limited_Type (P) then
2109 Set_Is_Limited_Composite (Current_Scope);
2112 Set_Is_Private_Composite (Current_Scope);
2117 and then Is_Limited_Type (T)
2118 and then Chars (Id) /= Name_uParent
2119 and then Is_Tagged_Type (Current_Scope)
2121 if Is_Derived_Type (Current_Scope)
2122 and then not Is_Known_Limited (Current_Scope)
2125 ("extension of nonlimited type cannot have limited components",
2128 if Is_Interface (Root_Type (Current_Scope)) then
2130 ("\limitedness is not inherited from limited interface", N);
2131 Error_Msg_N ("\add LIMITED to type indication", N);
2134 Explain_Limited_Type (T, N);
2135 Set_Etype (Id, Any_Type);
2136 Set_Is_Limited_Composite (Current_Scope, False);
2138 elsif not Is_Derived_Type (Current_Scope)
2139 and then not Is_Limited_Record (Current_Scope)
2140 and then not Is_Concurrent_Type (Current_Scope)
2143 ("nonlimited tagged type cannot have limited components", N);
2144 Explain_Limited_Type (T, N);
2145 Set_Etype (Id, Any_Type);
2146 Set_Is_Limited_Composite (Current_Scope, False);
2150 -- If the component is an unconstrained task or protected type with
2151 -- discriminants, the component and the enclosing record are limited
2152 -- and the component is constrained by its default values. Compute
2153 -- its actual subtype, else it may be allocated the maximum size by
2154 -- the backend, and possibly overflow.
2156 if Is_Concurrent_Type (T)
2157 and then not Is_Constrained (T)
2158 and then Has_Discriminants (T)
2159 and then not Has_Discriminants (Current_Scope)
2162 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2165 Set_Etype (Id, Act_T);
2167 -- Rewrite component definition to use the constrained subtype
2169 Rewrite (Component_Definition (N),
2170 Make_Component_Definition (Loc,
2171 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2175 Set_Original_Record_Component (Id, Id);
2177 if Has_Aspects (N) then
2178 Analyze_Aspect_Specifications (N, Id);
2181 Analyze_Dimension (N);
2182 end Analyze_Component_Declaration;
2184 --------------------------
2185 -- Analyze_Declarations --
2186 --------------------------
2188 procedure Analyze_Declarations (L : List_Id) is
2191 procedure Adjust_Decl;
2192 -- Adjust Decl not to include implicit label declarations, since these
2193 -- have strange Sloc values that result in elaboration check problems.
2194 -- (They have the sloc of the label as found in the source, and that
2195 -- is ahead of the current declarative part).
2197 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2198 -- Create the subprogram bodies which verify the run-time semantics of
2199 -- the pragmas listed below for each elibigle type found in declarative
2200 -- list Decls. The pragmas are:
2202 -- Default_Initial_Condition
2206 -- Context denotes the owner of the declarative list.
2208 procedure Check_Entry_Contracts;
2209 -- Perform a pre-analysis of the pre- and postconditions of an entry
2210 -- declaration. This must be done before full resolution and creation
2211 -- of the parameter block, etc. to catch illegal uses within the
2212 -- contract expression. Full analysis of the expression is done when
2213 -- the contract is processed.
2215 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2216 -- Determine whether Body_Decl denotes the body of a late controlled
2217 -- primitive (either Initialize, Adjust or Finalize). If this is the
2218 -- case, add a proper spec if the body lacks one. The spec is inserted
2219 -- before Body_Decl and immediately analyzed.
2221 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2222 -- Spec_Id is the entity of a package that may define abstract states,
2223 -- and in the case of a child unit, whose ancestors may define abstract
2224 -- states. If the states have partial visible refinement, remove the
2225 -- partial visibility of each constituent at the end of the package
2226 -- spec and body declarations.
2228 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2229 -- Spec_Id is the entity of a package that may define abstract states.
2230 -- If the states have visible refinement, remove the visibility of each
2231 -- constituent at the end of the package body declaration.
2233 procedure Resolve_Aspects;
2234 -- Utility to resolve the expressions of aspects at the end of a list of
2237 function Uses_Unseen_Lib_Unit_Priv (Pkg : Entity_Id) return Boolean;
2238 -- Check if an inner package has entities within it that rely on library
2239 -- level private types where the full view has not been seen.
2245 procedure Adjust_Decl is
2247 while Present (Prev (Decl))
2248 and then Nkind (Decl) = N_Implicit_Label_Declaration
2254 ----------------------------
2255 -- Build_Assertion_Bodies --
2256 ----------------------------
2258 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2259 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2260 -- Create the subprogram bodies which verify the run-time semantics
2261 -- of the pragmas listed below for type Typ. The pragmas are:
2263 -- Default_Initial_Condition
2267 -------------------------------------
2268 -- Build_Assertion_Bodies_For_Type --
2269 -------------------------------------
2271 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2273 -- Preanalyze and resolve the Default_Initial_Condition assertion
2274 -- expression at the end of the declarations to catch any errors.
2276 if Has_DIC (Typ) then
2277 Build_DIC_Procedure_Body (Typ);
2280 if Nkind (Context) = N_Package_Specification then
2282 -- Preanalyze and resolve the class-wide invariants of an
2283 -- interface at the end of whichever declarative part has the
2284 -- interface type. Note that an interface may be declared in
2285 -- any non-package declarative part, but reaching the end of
2286 -- such a declarative part will always freeze the type and
2287 -- generate the invariant procedure (see Freeze_Type).
2289 if Is_Interface (Typ) then
2291 -- Interfaces are treated as the partial view of a private
2292 -- type, in order to achieve uniformity with the general
2293 -- case. As a result, an interface receives only a "partial"
2294 -- invariant procedure, which is never called.
2296 if Has_Own_Invariants (Typ) then
2297 Build_Invariant_Procedure_Body
2299 Partial_Invariant => True);
2302 -- Preanalyze and resolve the invariants of a private type
2303 -- at the end of the visible declarations to catch potential
2304 -- errors. Inherited class-wide invariants are not included
2305 -- because they have already been resolved.
2307 elsif Decls = Visible_Declarations (Context)
2308 and then Ekind_In (Typ, E_Limited_Private_Type,
2310 E_Record_Type_With_Private)
2311 and then Has_Own_Invariants (Typ)
2313 Build_Invariant_Procedure_Body
2315 Partial_Invariant => True);
2317 -- Preanalyze and resolve the invariants of a private type's
2318 -- full view at the end of the private declarations to catch
2319 -- potential errors.
2321 elsif Decls = Private_Declarations (Context)
2322 and then not Is_Private_Type (Typ)
2323 and then Has_Private_Declaration (Typ)
2324 and then Has_Invariants (Typ)
2326 Build_Invariant_Procedure_Body (Typ);
2329 end Build_Assertion_Bodies_For_Type;
2334 Decl_Id : Entity_Id;
2336 -- Start of processing for Build_Assertion_Bodies
2339 Decl := First (Decls);
2340 while Present (Decl) loop
2341 if Is_Declaration (Decl) then
2342 Decl_Id := Defining_Entity (Decl);
2344 if Is_Type (Decl_Id) then
2345 Build_Assertion_Bodies_For_Type (Decl_Id);
2351 end Build_Assertion_Bodies;
2353 ---------------------------
2354 -- Check_Entry_Contracts --
2355 ---------------------------
2357 procedure Check_Entry_Contracts is
2363 Ent := First_Entity (Current_Scope);
2364 while Present (Ent) loop
2366 -- This only concerns entries with pre/postconditions
2368 if Ekind (Ent) = E_Entry
2369 and then Present (Contract (Ent))
2370 and then Present (Pre_Post_Conditions (Contract (Ent)))
2372 ASN := Pre_Post_Conditions (Contract (Ent));
2374 Install_Formals (Ent);
2376 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2377 -- is performed on a copy of the pragma expression, to prevent
2378 -- modifying the original expression.
2380 while Present (ASN) loop
2381 if Nkind (ASN) = N_Pragma then
2385 (First (Pragma_Argument_Associations (ASN))));
2386 Set_Parent (Exp, ASN);
2388 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2391 ASN := Next_Pragma (ASN);
2399 end Check_Entry_Contracts;
2401 --------------------------------------
2402 -- Handle_Late_Controlled_Primitive --
2403 --------------------------------------
2405 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2406 Body_Spec : constant Node_Id := Specification (Body_Decl);
2407 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2408 Loc : constant Source_Ptr := Sloc (Body_Id);
2409 Params : constant List_Id :=
2410 Parameter_Specifications (Body_Spec);
2412 Spec_Id : Entity_Id;
2416 -- Consider only procedure bodies whose name matches one of the three
2417 -- controlled primitives.
2419 if Nkind (Body_Spec) /= N_Procedure_Specification
2420 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2426 -- A controlled primitive must have exactly one formal which is not
2427 -- an anonymous access type.
2429 elsif List_Length (Params) /= 1 then
2433 Typ := Parameter_Type (First (Params));
2435 if Nkind (Typ) = N_Access_Definition then
2441 -- The type of the formal must be derived from [Limited_]Controlled
2443 if not Is_Controlled (Entity (Typ)) then
2447 -- Check whether a specification exists for this body. We do not
2448 -- analyze the spec of the body in full, because it will be analyzed
2449 -- again when the body is properly analyzed, and we cannot create
2450 -- duplicate entries in the formals chain. We look for an explicit
2451 -- specification because the body may be an overriding operation and
2452 -- an inherited spec may be present.
2454 Spec_Id := Current_Entity (Body_Id);
2456 while Present (Spec_Id) loop
2457 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2458 and then Scope (Spec_Id) = Current_Scope
2459 and then Present (First_Formal (Spec_Id))
2460 and then No (Next_Formal (First_Formal (Spec_Id)))
2461 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2462 and then Comes_From_Source (Spec_Id)
2467 Spec_Id := Homonym (Spec_Id);
2470 -- At this point the body is known to be a late controlled primitive.
2471 -- Generate a matching spec and insert it before the body. Note the
2472 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2473 -- tree in this case.
2475 Spec := Copy_Separate_Tree (Body_Spec);
2477 -- Ensure that the subprogram declaration does not inherit the null
2478 -- indicator from the body as we now have a proper spec/body pair.
2480 Set_Null_Present (Spec, False);
2482 -- Ensure that the freeze node is inserted after the declaration of
2483 -- the primitive since its expansion will freeze the primitive.
2485 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2487 Insert_Before_And_Analyze (Body_Decl, Decl);
2488 end Handle_Late_Controlled_Primitive;
2490 ----------------------------------------
2491 -- Remove_Partial_Visible_Refinements --
2492 ----------------------------------------
2494 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2495 State_Elmt : Elmt_Id;
2497 if Present (Abstract_States (Spec_Id)) then
2498 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2499 while Present (State_Elmt) loop
2500 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2501 Next_Elmt (State_Elmt);
2505 -- For a child unit, also hide the partial state refinement from
2506 -- ancestor packages.
2508 if Is_Child_Unit (Spec_Id) then
2509 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2511 end Remove_Partial_Visible_Refinements;
2513 --------------------------------
2514 -- Remove_Visible_Refinements --
2515 --------------------------------
2517 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2518 State_Elmt : Elmt_Id;
2520 if Present (Abstract_States (Spec_Id)) then
2521 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2522 while Present (State_Elmt) loop
2523 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2524 Next_Elmt (State_Elmt);
2527 end Remove_Visible_Refinements;
2529 ---------------------
2530 -- Resolve_Aspects --
2531 ---------------------
2533 procedure Resolve_Aspects is
2537 E := First_Entity (Current_Scope);
2538 while Present (E) loop
2539 Resolve_Aspect_Expressions (E);
2542 end Resolve_Aspects;
2544 -------------------------------
2545 -- Uses_Unseen_Lib_Unit_Priv --
2546 -------------------------------
2548 function Uses_Unseen_Lib_Unit_Priv (Pkg : Entity_Id) return Boolean is
2552 -- Avoid looking through scopes that do not meet the precondition of
2553 -- Pkg not being within a library unit spec.
2555 if not Is_Compilation_Unit (Pkg)
2556 and then not Is_Generic_Instance (Pkg)
2557 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2559 -- Loop through all entities in the current scope to identify
2560 -- an entity that depends on a private type.
2562 Curr := First_Entity (Pkg);
2564 if Nkind (Curr) in N_Entity
2565 and then Depends_On_Private (Curr)
2570 exit when Last_Entity (Current_Scope) = Curr;
2571 Curr := Next_Entity (Curr);
2576 end Uses_Unseen_Lib_Unit_Priv;
2580 Context : Node_Id := Empty;
2581 Freeze_From : Entity_Id := Empty;
2582 Next_Decl : Node_Id;
2584 Body_Seen : Boolean := False;
2585 -- Flag set when the first body [stub] is encountered
2587 -- Start of processing for Analyze_Declarations
2590 if Restriction_Check_Required (SPARK_05) then
2591 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2595 while Present (Decl) loop
2597 -- Package spec cannot contain a package declaration in SPARK
2599 if Nkind (Decl) = N_Package_Declaration
2600 and then Nkind (Parent (L)) = N_Package_Specification
2602 Check_SPARK_05_Restriction
2603 ("package specification cannot contain a package declaration",
2607 -- Complete analysis of declaration
2610 Next_Decl := Next (Decl);
2612 if No (Freeze_From) then
2613 Freeze_From := First_Entity (Current_Scope);
2616 -- At the end of a declarative part, freeze remaining entities
2617 -- declared in it. The end of the visible declarations of package
2618 -- specification is not the end of a declarative part if private
2619 -- declarations are present. The end of a package declaration is a
2620 -- freezing point only if it a library package. A task definition or
2621 -- protected type definition is not a freeze point either. Finally,
2622 -- we do not freeze entities in generic scopes, because there is no
2623 -- code generated for them and freeze nodes will be generated for
2626 -- The end of a package instantiation is not a freeze point, but
2627 -- for now we make it one, because the generic body is inserted
2628 -- (currently) immediately after. Generic instantiations will not
2629 -- be a freeze point once delayed freezing of bodies is implemented.
2630 -- (This is needed in any case for early instantiations ???).
2632 if No (Next_Decl) then
2633 if Nkind (Parent (L)) = N_Component_List then
2636 elsif Nkind_In (Parent (L), N_Protected_Definition,
2639 Check_Entry_Contracts;
2641 elsif Nkind (Parent (L)) /= N_Package_Specification then
2642 if Nkind (Parent (L)) = N_Package_Body then
2643 Freeze_From := First_Entity (Current_Scope);
2646 -- There may have been several freezing points previously,
2647 -- for example object declarations or subprogram bodies, but
2648 -- at the end of a declarative part we check freezing from
2649 -- the beginning, even though entities may already be frozen,
2650 -- in order to perform visibility checks on delayed aspects.
2653 Freeze_All (First_Entity (Current_Scope), Decl);
2654 Freeze_From := Last_Entity (Current_Scope);
2656 -- Current scope is a package specification
2658 elsif Scope (Current_Scope) /= Standard_Standard
2659 and then not Is_Child_Unit (Current_Scope)
2660 and then No (Generic_Parent (Parent (L)))
2662 -- This is needed in all cases to catch visibility errors in
2663 -- aspect expressions, but several large user tests are now
2664 -- rejected. Pending notification we restrict this call to
2671 elsif L /= Visible_Declarations (Parent (L))
2672 or else No (Private_Declarations (Parent (L)))
2673 or else Is_Empty_List (Private_Declarations (Parent (L)))
2677 -- End of a package declaration
2679 -- In compilation mode the expansion of freeze node takes care
2680 -- of resolving expressions of all aspects in the list. In ASIS
2681 -- mode this must be done explicitly.
2684 and then Scope (Current_Scope) = Standard_Standard
2689 -- This is a freeze point because it is the end of a
2690 -- compilation unit.
2692 Freeze_All (First_Entity (Current_Scope), Decl);
2693 Freeze_From := Last_Entity (Current_Scope);
2695 -- At the end of the visible declarations the expressions in
2696 -- aspects of all entities declared so far must be resolved.
2697 -- The entities themselves might be frozen later, and the
2698 -- generated pragmas and attribute definition clauses analyzed
2699 -- in full at that point, but name resolution must take place
2701 -- In addition to being the proper semantics, this is mandatory
2702 -- within generic units, because global name capture requires
2703 -- those expressions to be analyzed, given that the generated
2704 -- pragmas do not appear in the original generic tree.
2706 elsif Serious_Errors_Detected = 0 then
2710 -- If next node is a body then freeze all types before the body.
2711 -- An exception occurs for some expander-generated bodies. If these
2712 -- are generated at places where in general language rules would not
2713 -- allow a freeze point, then we assume that the expander has
2714 -- explicitly checked that all required types are properly frozen,
2715 -- and we do not cause general freezing here. This special circuit
2716 -- is used when the encountered body is marked as having already
2719 -- In all other cases (bodies that come from source, and expander
2720 -- generated bodies that have not been analyzed yet), freeze all
2721 -- types now. Note that in the latter case, the expander must take
2722 -- care to attach the bodies at a proper place in the tree so as to
2723 -- not cause unwanted freezing at that point.
2725 -- It is also necessary to check for a case where both an expression
2726 -- function is used and the current scope depends on an unseen
2727 -- private type from a library unit, otherwise premature freezing of
2728 -- the private type will occur.
2730 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2731 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2732 or else not Was_Expression_Function (Next_Decl))
2733 or else not Uses_Unseen_Lib_Unit_Priv (Current_Scope))
2735 -- When a controlled type is frozen, the expander generates stream
2736 -- and controlled-type support routines. If the freeze is caused
2737 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2738 -- expander will end up using the wrong version of these routines,
2739 -- as the body has not been processed yet. To remedy this, detect
2740 -- a late controlled primitive and create a proper spec for it.
2741 -- This ensures that the primitive will override its inherited
2742 -- counterpart before the freeze takes place.
2744 -- If the declaration we just processed is a body, do not attempt
2745 -- to examine Next_Decl as the late primitive idiom can only apply
2746 -- to the first encountered body.
2748 -- The spec of the late primitive is not generated in ASIS mode to
2749 -- ensure a consistent list of primitives that indicates the true
2750 -- semantic structure of the program (which is not relevant when
2751 -- generating executable code).
2753 -- ??? A cleaner approach may be possible and/or this solution
2754 -- could be extended to general-purpose late primitives, TBD.
2757 and then not Body_Seen
2758 and then not Is_Body (Decl)
2762 if Nkind (Next_Decl) = N_Subprogram_Body then
2763 Handle_Late_Controlled_Primitive (Next_Decl);
2769 -- The generated body of an expression function does not freeze,
2770 -- unless it is a completion, in which case only the expression
2771 -- itself freezes. This is handled when the body itself is
2772 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2774 Freeze_All (Freeze_From, Decl);
2775 Freeze_From := Last_Entity (Current_Scope);
2781 -- Post-freezing actions
2784 Context := Parent (L);
2786 -- Analyze the contracts of packages and their bodies
2788 if Nkind (Context) = N_Package_Specification then
2790 -- When a package has private declarations, its contract must be
2791 -- analyzed at the end of the said declarations. This way both the
2792 -- analysis and freeze actions are properly synchronized in case
2793 -- of private type use within the contract.
2795 if L = Private_Declarations (Context) then
2796 Analyze_Package_Contract (Defining_Entity (Context));
2798 -- Otherwise the contract is analyzed at the end of the visible
2801 elsif L = Visible_Declarations (Context)
2802 and then No (Private_Declarations (Context))
2804 Analyze_Package_Contract (Defining_Entity (Context));
2807 elsif Nkind (Context) = N_Package_Body then
2808 Analyze_Package_Body_Contract (Defining_Entity (Context));
2811 -- Analyze the contracts of various constructs now due to the delayed
2812 -- visibility needs of their aspects and pragmas.
2814 Analyze_Contracts (L);
2816 if Nkind (Context) = N_Package_Body then
2818 -- Ensure that all abstract states and objects declared in the
2819 -- state space of a package body are utilized as constituents.
2821 Check_Unused_Body_States (Defining_Entity (Context));
2823 -- State refinements are visible up to the end of the package body
2824 -- declarations. Hide the state refinements from visibility to
2825 -- restore the original state conditions.
2827 Remove_Visible_Refinements (Corresponding_Spec (Context));
2828 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2830 elsif Nkind (Context) = N_Package_Declaration then
2832 -- Partial state refinements are visible up to the end of the
2833 -- package spec declarations. Hide the partial state refinements
2834 -- from visibility to restore the original state conditions.
2836 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2839 -- Verify that all abstract states found in any package declared in
2840 -- the input declarative list have proper refinements. The check is
2841 -- performed only when the context denotes a block, entry, package,
2842 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2844 Check_State_Refinements (Context);
2846 -- Create the subprogram bodies which verify the run-time semantics
2847 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2848 -- types within the current declarative list. This ensures that all
2849 -- assertion expressions are preanalyzed and resolved at the end of
2850 -- the declarative part. Note that the resolution happens even when
2851 -- freezing does not take place.
2853 Build_Assertion_Bodies (L, Context);
2855 end Analyze_Declarations;
2857 -----------------------------------
2858 -- Analyze_Full_Type_Declaration --
2859 -----------------------------------
2861 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2862 Def : constant Node_Id := Type_Definition (N);
2863 Def_Id : constant Entity_Id := Defining_Identifier (N);
2867 Is_Remote : constant Boolean :=
2868 (Is_Remote_Types (Current_Scope)
2869 or else Is_Remote_Call_Interface (Current_Scope))
2870 and then not (In_Private_Part (Current_Scope)
2871 or else In_Package_Body (Current_Scope));
2873 procedure Check_Nonoverridable_Aspects;
2874 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2875 -- be overridden, and can only be confirmed on derivation.
2877 procedure Check_Ops_From_Incomplete_Type;
2878 -- If there is a tagged incomplete partial view of the type, traverse
2879 -- the primitives of the incomplete view and change the type of any
2880 -- controlling formals and result to indicate the full view. The
2881 -- primitives will be added to the full type's primitive operations
2882 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2883 -- is called from Process_Incomplete_Dependents).
2885 ----------------------------------
2886 -- Check_Nonoverridable_Aspects --
2887 ----------------------------------
2889 procedure Check_Nonoverridable_Aspects is
2890 function Get_Aspect_Spec
2892 Aspect_Name : Name_Id) return Node_Id;
2893 -- Check whether a list of aspect specifications includes an entry
2894 -- for a specific aspect. The list is either that of a partial or
2897 ---------------------
2898 -- Get_Aspect_Spec --
2899 ---------------------
2901 function Get_Aspect_Spec
2903 Aspect_Name : Name_Id) return Node_Id
2908 Spec := First (Specs);
2909 while Present (Spec) loop
2910 if Chars (Identifier (Spec)) = Aspect_Name then
2917 end Get_Aspect_Spec;
2921 Prev_Aspects : constant List_Id :=
2922 Aspect_Specifications (Parent (Def_Id));
2923 Par_Type : Entity_Id;
2924 Prev_Aspect : Node_Id;
2926 -- Start of processing for Check_Nonoverridable_Aspects
2929 -- Get parent type of derived type. Note that Prev is the entity in
2930 -- the partial declaration, but its contents are now those of full
2931 -- view, while Def_Id reflects the partial view.
2933 if Is_Private_Type (Def_Id) then
2934 Par_Type := Etype (Full_View (Def_Id));
2936 Par_Type := Etype (Def_Id);
2939 -- If there is an inherited Implicit_Dereference, verify that it is
2940 -- made explicit in the partial view.
2942 if Has_Discriminants (Base_Type (Par_Type))
2943 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2944 and then Present (Discriminant_Specifications (Parent (Prev)))
2945 and then Present (Get_Reference_Discriminant (Par_Type))
2948 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2952 (Discriminant_Specifications
2953 (Original_Node (Parent (Prev))))
2956 ("type does not inherit implicit dereference", Prev);
2959 -- If one of the views has the aspect specified, verify that it
2960 -- is consistent with that of the parent.
2963 Par_Discr : constant Entity_Id :=
2964 Get_Reference_Discriminant (Par_Type);
2965 Cur_Discr : constant Entity_Id :=
2966 Get_Reference_Discriminant (Prev);
2969 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
2970 Error_Msg_N ("aspect incosistent with that of parent", N);
2973 -- Check that specification in partial view matches the
2974 -- inherited aspect. Compare names directly because aspect
2975 -- expression may not be analyzed.
2977 if Present (Prev_Aspect)
2978 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
2979 and then Chars (Expression (Prev_Aspect)) /=
2983 ("aspect incosistent with that of parent", N);
2989 -- TBD : other nonoverridable aspects.
2990 end Check_Nonoverridable_Aspects;
2992 ------------------------------------
2993 -- Check_Ops_From_Incomplete_Type --
2994 ------------------------------------
2996 procedure Check_Ops_From_Incomplete_Type is
3003 and then Ekind (Prev) = E_Incomplete_Type
3004 and then Is_Tagged_Type (Prev)
3005 and then Is_Tagged_Type (T)
3007 Elmt := First_Elmt (Primitive_Operations (Prev));
3008 while Present (Elmt) loop
3011 Formal := First_Formal (Op);
3012 while Present (Formal) loop
3013 if Etype (Formal) = Prev then
3014 Set_Etype (Formal, T);
3017 Next_Formal (Formal);
3020 if Etype (Op) = Prev then
3027 end Check_Ops_From_Incomplete_Type;
3029 -- Start of processing for Analyze_Full_Type_Declaration
3032 Prev := Find_Type_Name (N);
3034 -- The full view, if present, now points to the current type. If there
3035 -- is an incomplete partial view, set a link to it, to simplify the
3036 -- retrieval of primitive operations of the type.
3038 -- Ada 2005 (AI-50217): If the type was previously decorated when
3039 -- imported through a LIMITED WITH clause, it appears as incomplete
3040 -- but has no full view.
3042 if Ekind (Prev) = E_Incomplete_Type
3043 and then Present (Full_View (Prev))
3045 T := Full_View (Prev);
3046 Set_Incomplete_View (N, Parent (Prev));
3051 Set_Is_Pure (T, Is_Pure (Current_Scope));
3053 -- We set the flag Is_First_Subtype here. It is needed to set the
3054 -- corresponding flag for the Implicit class-wide-type created
3055 -- during tagged types processing.
3057 Set_Is_First_Subtype (T, True);
3059 -- Only composite types other than array types are allowed to have
3064 -- For derived types, the rule will be checked once we've figured
3065 -- out the parent type.
3067 when N_Derived_Type_Definition =>
3070 -- For record types, discriminants are allowed, unless we are in
3073 when N_Record_Definition =>
3074 if Present (Discriminant_Specifications (N)) then
3075 Check_SPARK_05_Restriction
3076 ("discriminant type is not allowed",
3078 (First (Discriminant_Specifications (N))));
3082 if Present (Discriminant_Specifications (N)) then
3084 ("elementary or array type cannot have discriminants",
3086 (First (Discriminant_Specifications (N))));
3090 -- Elaborate the type definition according to kind, and generate
3091 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3092 -- already done (this happens during the reanalysis that follows a call
3093 -- to the high level optimizer).
3095 if not Analyzed (T) then
3099 when N_Access_To_Subprogram_Definition =>
3100 Access_Subprogram_Declaration (T, Def);
3102 -- If this is a remote access to subprogram, we must create the
3103 -- equivalent fat pointer type, and related subprograms.
3106 Process_Remote_AST_Declaration (N);
3109 -- Validate categorization rule against access type declaration
3110 -- usually a violation in Pure unit, Shared_Passive unit.
3112 Validate_Access_Type_Declaration (T, N);
3114 when N_Access_To_Object_Definition =>
3115 Access_Type_Declaration (T, Def);
3117 -- Validate categorization rule against access type declaration
3118 -- usually a violation in Pure unit, Shared_Passive unit.
3120 Validate_Access_Type_Declaration (T, N);
3122 -- If we are in a Remote_Call_Interface package and define a
3123 -- RACW, then calling stubs and specific stream attributes
3127 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3129 Add_RACW_Features (Def_Id);
3132 when N_Array_Type_Definition =>
3133 Array_Type_Declaration (T, Def);
3135 when N_Derived_Type_Definition =>
3136 Derived_Type_Declaration (T, N, T /= Def_Id);
3138 when N_Enumeration_Type_Definition =>
3139 Enumeration_Type_Declaration (T, Def);
3141 when N_Floating_Point_Definition =>
3142 Floating_Point_Type_Declaration (T, Def);
3144 when N_Decimal_Fixed_Point_Definition =>
3145 Decimal_Fixed_Point_Type_Declaration (T, Def);
3147 when N_Ordinary_Fixed_Point_Definition =>
3148 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3150 when N_Signed_Integer_Type_Definition =>
3151 Signed_Integer_Type_Declaration (T, Def);
3153 when N_Modular_Type_Definition =>
3154 Modular_Type_Declaration (T, Def);
3156 when N_Record_Definition =>
3157 Record_Type_Declaration (T, N, Prev);
3159 -- If declaration has a parse error, nothing to elaborate.
3165 raise Program_Error;
3169 if Etype (T) = Any_Type then
3173 -- Controlled type is not allowed in SPARK
3175 if Is_Visibly_Controlled (T) then
3176 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3179 -- Some common processing for all types
3181 Set_Depends_On_Private (T, Has_Private_Component (T));
3182 Check_Ops_From_Incomplete_Type;
3184 -- Both the declared entity, and its anonymous base type if one was
3185 -- created, need freeze nodes allocated.
3188 B : constant Entity_Id := Base_Type (T);
3191 -- In the case where the base type differs from the first subtype, we
3192 -- pre-allocate a freeze node, and set the proper link to the first
3193 -- subtype. Freeze_Entity will use this preallocated freeze node when
3194 -- it freezes the entity.
3196 -- This does not apply if the base type is a generic type, whose
3197 -- declaration is independent of the current derived definition.
3199 if B /= T and then not Is_Generic_Type (B) then
3200 Ensure_Freeze_Node (B);
3201 Set_First_Subtype_Link (Freeze_Node (B), T);
3204 -- A type that is imported through a limited_with clause cannot
3205 -- generate any code, and thus need not be frozen. However, an access
3206 -- type with an imported designated type needs a finalization list,
3207 -- which may be referenced in some other package that has non-limited
3208 -- visibility on the designated type. Thus we must create the
3209 -- finalization list at the point the access type is frozen, to
3210 -- prevent unsatisfied references at link time.
3212 if not From_Limited_With (T) or else Is_Access_Type (T) then
3213 Set_Has_Delayed_Freeze (T);
3217 -- Case where T is the full declaration of some private type which has
3218 -- been swapped in Defining_Identifier (N).
3220 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3221 Process_Full_View (N, T, Def_Id);
3223 -- Record the reference. The form of this is a little strange, since
3224 -- the full declaration has been swapped in. So the first parameter
3225 -- here represents the entity to which a reference is made which is
3226 -- the "real" entity, i.e. the one swapped in, and the second
3227 -- parameter provides the reference location.
3229 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3230 -- since we don't want a complaint about the full type being an
3231 -- unwanted reference to the private type
3234 B : constant Boolean := Has_Pragma_Unreferenced (T);
3236 Set_Has_Pragma_Unreferenced (T, False);
3237 Generate_Reference (T, T, 'c');
3238 Set_Has_Pragma_Unreferenced (T, B);
3241 Set_Completion_Referenced (Def_Id);
3243 -- For completion of incomplete type, process incomplete dependents
3244 -- and always mark the full type as referenced (it is the incomplete
3245 -- type that we get for any real reference).
3247 elsif Ekind (Prev) = E_Incomplete_Type then
3248 Process_Incomplete_Dependents (N, T, Prev);
3249 Generate_Reference (Prev, Def_Id, 'c');
3250 Set_Completion_Referenced (Def_Id);
3252 -- If not private type or incomplete type completion, this is a real
3253 -- definition of a new entity, so record it.
3256 Generate_Definition (Def_Id);
3259 -- Propagate any pending access types whose finalization masters need to
3260 -- be fully initialized from the partial to the full view. Guard against
3261 -- an illegal full view that remains unanalyzed.
3263 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3264 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3267 if Chars (Scope (Def_Id)) = Name_System
3268 and then Chars (Def_Id) = Name_Address
3269 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
3271 Set_Is_Descendant_Of_Address (Def_Id);
3272 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3273 Set_Is_Descendant_Of_Address (Prev);
3276 Set_Optimize_Alignment_Flags (Def_Id);
3277 Check_Eliminated (Def_Id);
3279 -- If the declaration is a completion and aspects are present, apply
3280 -- them to the entity for the type which is currently the partial
3281 -- view, but which is the one that will be frozen.
3283 if Has_Aspects (N) then
3285 -- In most cases the partial view is a private type, and both views
3286 -- appear in different declarative parts. In the unusual case where
3287 -- the partial view is incomplete, perform the analysis on the
3288 -- full view, to prevent freezing anomalies with the corresponding
3289 -- class-wide type, which otherwise might be frozen before the
3290 -- dispatch table is built.
3293 and then Ekind (Prev) /= E_Incomplete_Type
3295 Analyze_Aspect_Specifications (N, Prev);
3300 Analyze_Aspect_Specifications (N, Def_Id);
3304 if Is_Derived_Type (Prev)
3305 and then Def_Id /= Prev
3307 Check_Nonoverridable_Aspects;
3309 end Analyze_Full_Type_Declaration;
3311 ----------------------------------
3312 -- Analyze_Incomplete_Type_Decl --
3313 ----------------------------------
3315 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3316 F : constant Boolean := Is_Pure (Current_Scope);
3320 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3322 Generate_Definition (Defining_Identifier (N));
3324 -- Process an incomplete declaration. The identifier must not have been
3325 -- declared already in the scope. However, an incomplete declaration may
3326 -- appear in the private part of a package, for a private type that has
3327 -- already been declared.
3329 -- In this case, the discriminants (if any) must match
3331 T := Find_Type_Name (N);
3333 Set_Ekind (T, E_Incomplete_Type);
3334 Init_Size_Align (T);
3335 Set_Is_First_Subtype (T, True);
3338 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3339 -- incomplete types.
3341 if Tagged_Present (N) then
3342 Set_Is_Tagged_Type (T, True);
3343 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3344 Make_Class_Wide_Type (T);
3345 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3348 Set_Stored_Constraint (T, No_Elist);
3350 if Present (Discriminant_Specifications (N)) then
3352 Process_Discriminants (N);
3356 -- If the type has discriminants, nontrivial subtypes may be declared
3357 -- before the full view of the type. The full views of those subtypes
3358 -- will be built after the full view of the type.
3360 Set_Private_Dependents (T, New_Elmt_List);
3362 end Analyze_Incomplete_Type_Decl;
3364 -----------------------------------
3365 -- Analyze_Interface_Declaration --
3366 -----------------------------------
3368 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3369 CW : constant Entity_Id := Class_Wide_Type (T);
3372 Set_Is_Tagged_Type (T);
3373 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3375 Set_Is_Limited_Record (T, Limited_Present (Def)
3376 or else Task_Present (Def)
3377 or else Protected_Present (Def)
3378 or else Synchronized_Present (Def));
3380 -- Type is abstract if full declaration carries keyword, or if previous
3381 -- partial view did.
3383 Set_Is_Abstract_Type (T);
3384 Set_Is_Interface (T);
3386 -- Type is a limited interface if it includes the keyword limited, task,
3387 -- protected, or synchronized.
3389 Set_Is_Limited_Interface
3390 (T, Limited_Present (Def)
3391 or else Protected_Present (Def)
3392 or else Synchronized_Present (Def)
3393 or else Task_Present (Def));
3395 Set_Interfaces (T, New_Elmt_List);
3396 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3398 -- Complete the decoration of the class-wide entity if it was already
3399 -- built (i.e. during the creation of the limited view)
3401 if Present (CW) then
3402 Set_Is_Interface (CW);
3403 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3406 -- Check runtime support for synchronized interfaces
3408 if (Is_Task_Interface (T)
3409 or else Is_Protected_Interface (T)
3410 or else Is_Synchronized_Interface (T))
3411 and then not RTE_Available (RE_Select_Specific_Data)
3413 Error_Msg_CRT ("synchronized interfaces", T);
3415 end Analyze_Interface_Declaration;
3417 -----------------------------
3418 -- Analyze_Itype_Reference --
3419 -----------------------------
3421 -- Nothing to do. This node is placed in the tree only for the benefit of
3422 -- back end processing, and has no effect on the semantic processing.
3424 procedure Analyze_Itype_Reference (N : Node_Id) is
3426 pragma Assert (Is_Itype (Itype (N)));
3428 end Analyze_Itype_Reference;
3430 --------------------------------
3431 -- Analyze_Number_Declaration --
3432 --------------------------------
3434 procedure Analyze_Number_Declaration (N : Node_Id) is
3435 E : constant Node_Id := Expression (N);
3436 Id : constant Entity_Id := Defining_Identifier (N);
3437 Index : Interp_Index;
3442 Generate_Definition (Id);
3445 -- This is an optimization of a common case of an integer literal
3447 if Nkind (E) = N_Integer_Literal then
3448 Set_Is_Static_Expression (E, True);
3449 Set_Etype (E, Universal_Integer);
3451 Set_Etype (Id, Universal_Integer);
3452 Set_Ekind (Id, E_Named_Integer);
3453 Set_Is_Frozen (Id, True);
3457 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3459 -- Process expression, replacing error by integer zero, to avoid
3460 -- cascaded errors or aborts further along in the processing
3462 -- Replace Error by integer zero, which seems least likely to cause
3466 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3467 Set_Error_Posted (E);
3472 -- Verify that the expression is static and numeric. If
3473 -- the expression is overloaded, we apply the preference
3474 -- rule that favors root numeric types.
3476 if not Is_Overloaded (E) then
3478 if Has_Dynamic_Predicate_Aspect (T) then
3480 ("subtype has dynamic predicate, "
3481 & "not allowed in number declaration", N);
3487 Get_First_Interp (E, Index, It);
3488 while Present (It.Typ) loop
3489 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3490 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3492 if T = Any_Type then
3495 elsif It.Typ = Universal_Real
3497 It.Typ = Universal_Integer
3499 -- Choose universal interpretation over any other
3506 Get_Next_Interp (Index, It);
3510 if Is_Integer_Type (T) then
3512 Set_Etype (Id, Universal_Integer);
3513 Set_Ekind (Id, E_Named_Integer);
3515 elsif Is_Real_Type (T) then
3517 -- Because the real value is converted to universal_real, this is a
3518 -- legal context for a universal fixed expression.
3520 if T = Universal_Fixed then
3522 Loc : constant Source_Ptr := Sloc (N);
3523 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3525 New_Occurrence_Of (Universal_Real, Loc),
3526 Expression => Relocate_Node (E));
3533 elsif T = Any_Fixed then
3534 Error_Msg_N ("illegal context for mixed mode operation", E);
3536 -- Expression is of the form : universal_fixed * integer. Try to
3537 -- resolve as universal_real.
3539 T := Universal_Real;
3544 Set_Etype (Id, Universal_Real);
3545 Set_Ekind (Id, E_Named_Real);
3548 Wrong_Type (E, Any_Numeric);
3552 Set_Ekind (Id, E_Constant);
3553 Set_Never_Set_In_Source (Id, True);
3554 Set_Is_True_Constant (Id, True);
3558 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3559 Set_Etype (E, Etype (Id));
3562 if not Is_OK_Static_Expression (E) then
3563 Flag_Non_Static_Expr
3564 ("non-static expression used in number declaration!", E);
3565 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3566 Set_Etype (E, Any_Type);
3569 Analyze_Dimension (N);
3570 end Analyze_Number_Declaration;
3572 --------------------------------
3573 -- Analyze_Object_Declaration --
3574 --------------------------------
3576 -- WARNING: This routine manages Ghost regions. Return statements must be
3577 -- replaced by gotos which jump to the end of the routine and restore the
3580 procedure Analyze_Object_Declaration (N : Node_Id) is
3581 Loc : constant Source_Ptr := Sloc (N);
3582 Id : constant Entity_Id := Defining_Identifier (N);
3586 E : Node_Id := Expression (N);
3587 -- E is set to Expression (N) throughout this routine. When
3588 -- Expression (N) is modified, E is changed accordingly.
3590 Prev_Entity : Entity_Id := Empty;
3592 function Count_Tasks (T : Entity_Id) return Uint;
3593 -- This function is called when a non-generic library level object of a
3594 -- task type is declared. Its function is to count the static number of
3595 -- tasks declared within the type (it is only called if Has_Task is set
3596 -- for T). As a side effect, if an array of tasks with non-static bounds
3597 -- or a variant record type is encountered, Check_Restriction is called
3598 -- indicating the count is unknown.
3600 function Delayed_Aspect_Present return Boolean;
3601 -- If the declaration has an expression that is an aggregate, and it
3602 -- has aspects that require delayed analysis, the resolution of the
3603 -- aggregate must be deferred to the freeze point of the objet. This
3604 -- special processing was created for address clauses, but it must
3605 -- also apply to Alignment. This must be done before the aspect
3606 -- specifications are analyzed because we must handle the aggregate
3607 -- before the analysis of the object declaration is complete.
3609 -- Any other relevant delayed aspects on object declarations ???
3615 function Count_Tasks (T : Entity_Id) return Uint is
3621 if Is_Task_Type (T) then
3624 elsif Is_Record_Type (T) then
3625 if Has_Discriminants (T) then
3626 Check_Restriction (Max_Tasks, N);
3631 C := First_Component (T);
3632 while Present (C) loop
3633 V := V + Count_Tasks (Etype (C));
3640 elsif Is_Array_Type (T) then
3641 X := First_Index (T);
3642 V := Count_Tasks (Component_Type (T));
3643 while Present (X) loop
3646 if not Is_OK_Static_Subtype (C) then
3647 Check_Restriction (Max_Tasks, N);
3650 V := V * (UI_Max (Uint_0,
3651 Expr_Value (Type_High_Bound (C)) -
3652 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3665 ----------------------------
3666 -- Delayed_Aspect_Present --
3667 ----------------------------
3669 function Delayed_Aspect_Present return Boolean is
3674 if Present (Aspect_Specifications (N)) then
3675 A := First (Aspect_Specifications (N));
3676 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3677 while Present (A) loop
3678 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3687 end Delayed_Aspect_Present;
3691 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3692 -- Save the Ghost mode to restore on exit
3694 Related_Id : Entity_Id;
3696 -- Start of processing for Analyze_Object_Declaration
3699 -- There are three kinds of implicit types generated by an
3700 -- object declaration:
3702 -- 1. Those generated by the original Object Definition
3704 -- 2. Those generated by the Expression
3706 -- 3. Those used to constrain the Object Definition with the
3707 -- expression constraints when the definition is unconstrained.
3709 -- They must be generated in this order to avoid order of elaboration
3710 -- issues. Thus the first step (after entering the name) is to analyze
3711 -- the object definition.
3713 if Constant_Present (N) then
3714 Prev_Entity := Current_Entity_In_Scope (Id);
3716 if Present (Prev_Entity)
3718 -- If the homograph is an implicit subprogram, it is overridden
3719 -- by the current declaration.
3721 ((Is_Overloadable (Prev_Entity)
3722 and then Is_Inherited_Operation (Prev_Entity))
3724 -- The current object is a discriminal generated for an entry
3725 -- family index. Even though the index is a constant, in this
3726 -- particular context there is no true constant redeclaration.
3727 -- Enter_Name will handle the visibility.
3730 (Is_Discriminal (Id)
3731 and then Ekind (Discriminal_Link (Id)) =
3732 E_Entry_Index_Parameter)
3734 -- The current object is the renaming for a generic declared
3735 -- within the instance.
3738 (Ekind (Prev_Entity) = E_Package
3739 and then Nkind (Parent (Prev_Entity)) =
3740 N_Package_Renaming_Declaration
3741 and then not Comes_From_Source (Prev_Entity)
3743 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3745 -- The entity may be a homonym of a private component of the
3746 -- enclosing protected object, for which we create a local
3747 -- renaming declaration. The declaration is legal, even if
3748 -- useless when it just captures that component.
3751 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3752 and then Nkind (Parent (Prev_Entity)) =
3753 N_Object_Renaming_Declaration))
3755 Prev_Entity := Empty;
3759 if Present (Prev_Entity) then
3761 -- The object declaration is Ghost when it completes a deferred Ghost
3764 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
3766 Constant_Redeclaration (Id, N, T);
3768 Generate_Reference (Prev_Entity, Id, 'c');
3769 Set_Completion_Referenced (Id);
3771 if Error_Posted (N) then
3773 -- Type mismatch or illegal redeclaration; do not analyze
3774 -- expression to avoid cascaded errors.
3776 T := Find_Type_Of_Object (Object_Definition (N), N);
3778 Set_Ekind (Id, E_Variable);
3782 -- In the normal case, enter identifier at the start to catch premature
3783 -- usage in the initialization expression.
3786 Generate_Definition (Id);
3789 Mark_Coextensions (N, Object_Definition (N));
3791 T := Find_Type_Of_Object (Object_Definition (N), N);
3793 if Nkind (Object_Definition (N)) = N_Access_Definition
3795 (Access_To_Subprogram_Definition (Object_Definition (N)))
3796 and then Protected_Present
3797 (Access_To_Subprogram_Definition (Object_Definition (N)))
3799 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3802 if Error_Posted (Id) then
3804 Set_Ekind (Id, E_Variable);
3809 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3810 -- out some static checks.
3812 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3814 -- In case of aggregates we must also take care of the correct
3815 -- initialization of nested aggregates bug this is done at the
3816 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
3818 if Present (Expression (N))
3819 and then Nkind (Expression (N)) = N_Aggregate
3825 Save_Typ : constant Entity_Id := Etype (Id);
3827 Set_Etype (Id, T); -- Temp. decoration for static checks
3828 Null_Exclusion_Static_Checks (N);
3829 Set_Etype (Id, Save_Typ);
3834 -- Object is marked pure if it is in a pure scope
3836 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3838 -- If deferred constant, make sure context is appropriate. We detect
3839 -- a deferred constant as a constant declaration with no expression.
3840 -- A deferred constant can appear in a package body if its completion
3841 -- is by means of an interface pragma.
3843 if Constant_Present (N) and then No (E) then
3845 -- A deferred constant may appear in the declarative part of the
3846 -- following constructs:
3850 -- extended return statements
3853 -- subprogram bodies
3856 -- When declared inside a package spec, a deferred constant must be
3857 -- completed by a full constant declaration or pragma Import. In all
3858 -- other cases, the only proper completion is pragma Import. Extended
3859 -- return statements are flagged as invalid contexts because they do
3860 -- not have a declarative part and so cannot accommodate the pragma.
3862 if Ekind (Current_Scope) = E_Return_Statement then
3864 ("invalid context for deferred constant declaration (RM 7.4)",
3867 ("\declaration requires an initialization expression",
3869 Set_Constant_Present (N, False);
3871 -- In Ada 83, deferred constant must be of private type
3873 elsif not Is_Private_Type (T) then
3874 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3876 ("(Ada 83) deferred constant must be private type", N);
3880 -- If not a deferred constant, then the object declaration freezes
3881 -- its type, unless the object is of an anonymous type and has delayed
3882 -- aspects. In that case the type is frozen when the object itself is.
3885 Check_Fully_Declared (T, N);
3887 if Has_Delayed_Aspects (Id)
3888 and then Is_Array_Type (T)
3889 and then Is_Itype (T)
3891 Set_Has_Delayed_Freeze (T);
3893 Freeze_Before (N, T);
3897 -- If the object was created by a constrained array definition, then
3898 -- set the link in both the anonymous base type and anonymous subtype
3899 -- that are built to represent the array type to point to the object.
3901 if Nkind (Object_Definition (Declaration_Node (Id))) =
3902 N_Constrained_Array_Definition
3904 Set_Related_Array_Object (T, Id);
3905 Set_Related_Array_Object (Base_Type (T), Id);
3908 -- Special checks for protected objects not at library level
3910 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
3911 Check_Restriction (No_Local_Protected_Objects, Id);
3913 -- Protected objects with interrupt handlers must be at library level
3915 -- Ada 2005: This test is not needed (and the corresponding clause
3916 -- in the RM is removed) because accessibility checks are sufficient
3917 -- to make handlers not at the library level illegal.
3919 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3920 -- applies to the '95 version of the language as well.
3922 if Is_Protected_Type (T)
3923 and then Has_Interrupt_Handler (T)
3924 and then Ada_Version < Ada_95
3927 ("interrupt object can only be declared at library level", Id);
3931 -- Check for violation of No_Local_Timing_Events
3933 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
3934 Check_Restriction (No_Local_Timing_Events, Id);
3937 -- The actual subtype of the object is the nominal subtype, unless
3938 -- the nominal one is unconstrained and obtained from the expression.
3942 -- These checks should be performed before the initialization expression
3943 -- is considered, so that the Object_Definition node is still the same
3944 -- as in source code.
3946 -- In SPARK, the nominal subtype is always given by a subtype mark
3947 -- and must not be unconstrained. (The only exception to this is the
3948 -- acceptance of declarations of constants of type String.)
3950 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3952 Check_SPARK_05_Restriction
3953 ("subtype mark required", Object_Definition (N));
3955 elsif Is_Array_Type (T)
3956 and then not Is_Constrained (T)
3957 and then T /= Standard_String
3959 Check_SPARK_05_Restriction
3960 ("subtype mark of constrained type expected",
3961 Object_Definition (N));
3964 -- There are no aliased objects in SPARK
3966 if Aliased_Present (N) then
3967 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3970 -- Process initialization expression if present and not in error
3972 if Present (E) and then E /= Error then
3974 -- Generate an error in case of CPP class-wide object initialization.
3975 -- Required because otherwise the expansion of the class-wide
3976 -- assignment would try to use 'size to initialize the object
3977 -- (primitive that is not available in CPP tagged types).
3979 if Is_Class_Wide_Type (Act_T)
3981 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3983 (Present (Full_View (Root_Type (Etype (Act_T))))
3985 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3988 ("predefined assignment not available for 'C'P'P tagged types",
3992 Mark_Coextensions (N, E);
3995 -- In case of errors detected in the analysis of the expression,
3996 -- decorate it with the expected type to avoid cascaded errors
3998 if No (Etype (E)) then
4002 -- If an initialization expression is present, then we set the
4003 -- Is_True_Constant flag. It will be reset if this is a variable
4004 -- and it is indeed modified.
4006 Set_Is_True_Constant (Id, True);
4008 -- If we are analyzing a constant declaration, set its completion
4009 -- flag after analyzing and resolving the expression.
4011 if Constant_Present (N) then
4012 Set_Has_Completion (Id);
4015 -- Set type and resolve (type may be overridden later on). Note:
4016 -- Ekind (Id) must still be E_Void at this point so that incorrect
4017 -- early usage within E is properly diagnosed.
4021 -- If the expression is an aggregate we must look ahead to detect
4022 -- the possible presence of an address clause, and defer resolution
4023 -- and expansion of the aggregate to the freeze point of the entity.
4025 -- This is not always legal because the aggregate may contain other
4026 -- references that need freezing, e.g. references to other entities
4027 -- with address clauses. In any case, when compiling with -gnatI the
4028 -- presence of the address clause must be ignored.
4030 if Comes_From_Source (N)
4031 and then Expander_Active
4032 and then Nkind (E) = N_Aggregate
4034 ((Present (Following_Address_Clause (N))
4035 and then not Ignore_Rep_Clauses)
4036 or else Delayed_Aspect_Present)
4044 -- No further action needed if E is a call to an inlined function
4045 -- which returns an unconstrained type and it has been expanded into
4046 -- a procedure call. In that case N has been replaced by an object
4047 -- declaration without initializing expression and it has been
4048 -- analyzed (see Expand_Inlined_Call).
4050 if Back_End_Inlining
4051 and then Expander_Active
4052 and then Nkind (E) = N_Function_Call
4053 and then Nkind (Name (E)) in N_Has_Entity
4054 and then Is_Inlined (Entity (Name (E)))
4055 and then not Is_Constrained (Etype (E))
4056 and then Analyzed (N)
4057 and then No (Expression (N))
4062 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4063 -- node (which was marked already-analyzed), we need to set the type
4064 -- to something other than Any_Access in order to keep gigi happy.
4066 if Etype (E) = Any_Access then
4070 -- If the object is an access to variable, the initialization
4071 -- expression cannot be an access to constant.
4073 if Is_Access_Type (T)
4074 and then not Is_Access_Constant (T)
4075 and then Is_Access_Type (Etype (E))
4076 and then Is_Access_Constant (Etype (E))
4079 ("access to variable cannot be initialized with an "
4080 & "access-to-constant expression", E);
4083 if not Assignment_OK (N) then
4084 Check_Initialization (T, E);
4087 Check_Unset_Reference (E);
4089 -- If this is a variable, then set current value. If this is a
4090 -- declared constant of a scalar type with a static expression,
4091 -- indicate that it is always valid.
4093 if not Constant_Present (N) then
4094 if Compile_Time_Known_Value (E) then
4095 Set_Current_Value (Id, E);
4098 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4099 Set_Is_Known_Valid (Id);
4102 -- Deal with setting of null flags
4104 if Is_Access_Type (T) then
4105 if Known_Non_Null (E) then
4106 Set_Is_Known_Non_Null (Id, True);
4107 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4108 Set_Is_Known_Null (Id, True);
4112 -- Check incorrect use of dynamically tagged expressions
4114 if Is_Tagged_Type (T) then
4115 Check_Dynamically_Tagged_Expression
4121 Apply_Scalar_Range_Check (E, T);
4122 Apply_Static_Length_Check (E, T);
4124 if Nkind (Original_Node (N)) = N_Object_Declaration
4125 and then Comes_From_Source (Original_Node (N))
4127 -- Only call test if needed
4129 and then Restriction_Check_Required (SPARK_05)
4130 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4132 Check_SPARK_05_Restriction
4133 ("initialization expression is not appropriate", E);
4136 -- A formal parameter of a specific tagged type whose related
4137 -- subprogram is subject to pragma Extensions_Visible with value
4138 -- "False" cannot be implicitly converted to a class-wide type by
4139 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4140 -- not consider internally generated expressions.
4142 if Is_Class_Wide_Type (T)
4143 and then Comes_From_Source (E)
4144 and then Is_EVF_Expression (E)
4147 ("formal parameter cannot be implicitly converted to "
4148 & "class-wide type when Extensions_Visible is False", E);
4152 -- If the No_Streams restriction is set, check that the type of the
4153 -- object is not, and does not contain, any subtype derived from
4154 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4155 -- Has_Stream just for efficiency reasons. There is no point in
4156 -- spending time on a Has_Stream check if the restriction is not set.
4158 if Restriction_Check_Required (No_Streams) then
4159 if Has_Stream (T) then
4160 Check_Restriction (No_Streams, N);
4164 -- Deal with predicate check before we start to do major rewriting. It
4165 -- is OK to initialize and then check the initialized value, since the
4166 -- object goes out of scope if we get a predicate failure. Note that we
4167 -- do this in the analyzer and not the expander because the analyzer
4168 -- does some substantial rewriting in some cases.
4170 -- We need a predicate check if the type has predicates that are not
4171 -- ignored, and if either there is an initializing expression, or for
4172 -- default initialization when we have at least one case of an explicit
4173 -- default initial value and then this is not an internal declaration
4174 -- whose initialization comes later (as for an aggregate expansion).
4176 if not Suppress_Assignment_Checks (N)
4177 and then Present (Predicate_Function (T))
4178 and then not Predicates_Ignored (T)
4179 and then not No_Initialization (N)
4183 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4185 -- If the type has a static predicate and the expression is known at
4186 -- compile time, see if the expression satisfies the predicate.
4189 Check_Expression_Against_Static_Predicate (E, T);
4192 -- If the type is a null record and there is no explicit initial
4193 -- expression, no predicate check applies.
4195 if No (E) and then Is_Null_Record_Type (T) then
4198 -- Do not generate a predicate check if the initialization expression
4199 -- is a type conversion because the conversion has been subjected to
4200 -- the same check. This is a small optimization which avoid redundant
4203 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4208 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
4212 -- Case of unconstrained type
4214 if not Is_Definite_Subtype (T) then
4216 -- In SPARK, a declaration of unconstrained type is allowed
4217 -- only for constants of type string.
4219 if Is_String_Type (T) and then not Constant_Present (N) then
4220 Check_SPARK_05_Restriction
4221 ("declaration of object of unconstrained type not allowed", N);
4224 -- Nothing to do in deferred constant case
4226 if Constant_Present (N) and then No (E) then
4229 -- Case of no initialization present
4232 if No_Initialization (N) then
4235 elsif Is_Class_Wide_Type (T) then
4237 ("initialization required in class-wide declaration ", N);
4241 ("unconstrained subtype not allowed (need initialization)",
4242 Object_Definition (N));
4244 if Is_Record_Type (T) and then Has_Discriminants (T) then
4246 ("\provide initial value or explicit discriminant values",
4247 Object_Definition (N));
4250 ("\or give default discriminant values for type&",
4251 Object_Definition (N), T);
4253 elsif Is_Array_Type (T) then
4255 ("\provide initial value or explicit array bounds",
4256 Object_Definition (N));
4260 -- Case of initialization present but in error. Set initial
4261 -- expression as absent (but do not make above complaints)
4263 elsif E = Error then
4264 Set_Expression (N, Empty);
4267 -- Case of initialization present
4270 -- Check restrictions in Ada 83
4272 if not Constant_Present (N) then
4274 -- Unconstrained variables not allowed in Ada 83 mode
4276 if Ada_Version = Ada_83
4277 and then Comes_From_Source (Object_Definition (N))
4280 ("(Ada 83) unconstrained variable not allowed",
4281 Object_Definition (N));
4285 -- Now we constrain the variable from the initializing expression
4287 -- If the expression is an aggregate, it has been expanded into
4288 -- individual assignments. Retrieve the actual type from the
4289 -- expanded construct.
4291 if Is_Array_Type (T)
4292 and then No_Initialization (N)
4293 and then Nkind (Original_Node (E)) = N_Aggregate
4297 -- In case of class-wide interface object declarations we delay
4298 -- the generation of the equivalent record type declarations until
4299 -- its expansion because there are cases in they are not required.
4301 elsif Is_Interface (T) then
4304 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4305 -- we should prevent the generation of another Itype with the
4306 -- same name as the one already generated, or we end up with
4307 -- two identical types in GNATprove.
4309 elsif GNATprove_Mode then
4312 -- If the type is an unchecked union, no subtype can be built from
4313 -- the expression. Rewrite declaration as a renaming, which the
4314 -- back-end can handle properly. This is a rather unusual case,
4315 -- because most unchecked_union declarations have default values
4316 -- for discriminants and are thus not indefinite.
4318 elsif Is_Unchecked_Union (T) then
4319 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4320 Set_Ekind (Id, E_Constant);
4322 Set_Ekind (Id, E_Variable);
4326 Make_Object_Renaming_Declaration (Loc,
4327 Defining_Identifier => Id,
4328 Subtype_Mark => New_Occurrence_Of (T, Loc),
4331 Set_Renamed_Object (Id, E);
4332 Freeze_Before (N, T);
4337 -- Ensure that the generated subtype has a unique external name
4338 -- when the related object is public. This guarantees that the
4339 -- subtype and its bounds will not be affected by switches or
4340 -- pragmas that may offset the internal counter due to extra
4343 if Is_Public (Id) then
4346 Related_Id := Empty;
4349 Expand_Subtype_From_Expr
4352 Subtype_Indic => Object_Definition (N),
4354 Related_Id => Related_Id);
4356 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4359 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4361 if Aliased_Present (N) then
4362 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4365 Freeze_Before (N, Act_T);
4366 Freeze_Before (N, T);
4369 elsif Is_Array_Type (T)
4370 and then No_Initialization (N)
4371 and then (Nkind (Original_Node (E)) = N_Aggregate
4372 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4373 and then Nkind (Original_Node (Expression
4374 (Original_Node (E)))) = N_Aggregate))
4376 if not Is_Entity_Name (Object_Definition (N)) then
4378 Check_Compile_Time_Size (Act_T);
4380 if Aliased_Present (N) then
4381 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4385 -- When the given object definition and the aggregate are specified
4386 -- independently, and their lengths might differ do a length check.
4387 -- This cannot happen if the aggregate is of the form (others =>...)
4389 if not Is_Constrained (T) then
4392 elsif Nkind (E) = N_Raise_Constraint_Error then
4394 -- Aggregate is statically illegal. Place back in declaration
4396 Set_Expression (N, E);
4397 Set_No_Initialization (N, False);
4399 elsif T = Etype (E) then
4402 elsif Nkind (E) = N_Aggregate
4403 and then Present (Component_Associations (E))
4404 and then Present (Choice_List (First (Component_Associations (E))))
4406 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4412 Apply_Length_Check (E, T);
4415 -- If the type is limited unconstrained with defaulted discriminants and
4416 -- there is no expression, then the object is constrained by the
4417 -- defaults, so it is worthwhile building the corresponding subtype.
4419 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4420 and then not Is_Constrained (T)
4421 and then Has_Discriminants (T)
4424 Act_T := Build_Default_Subtype (T, N);
4426 -- Ada 2005: A limited object may be initialized by means of an
4427 -- aggregate. If the type has default discriminants it has an
4428 -- unconstrained nominal type, Its actual subtype will be obtained
4429 -- from the aggregate, and not from the default discriminants.
4434 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4436 elsif Nkind (E) = N_Function_Call
4437 and then Constant_Present (N)
4438 and then Has_Unconstrained_Elements (Etype (E))
4440 -- The back-end has problems with constants of a discriminated type
4441 -- with defaults, if the initial value is a function call. We
4442 -- generate an intermediate temporary that will receive a reference
4443 -- to the result of the call. The initialization expression then
4444 -- becomes a dereference of that temporary.
4446 Remove_Side_Effects (E);
4448 -- If this is a constant declaration of an unconstrained type and
4449 -- the initialization is an aggregate, we can use the subtype of the
4450 -- aggregate for the declared entity because it is immutable.
4452 elsif not Is_Constrained (T)
4453 and then Has_Discriminants (T)
4454 and then Constant_Present (N)
4455 and then not Has_Unchecked_Union (T)
4456 and then Nkind (E) = N_Aggregate
4461 -- Check No_Wide_Characters restriction
4463 Check_Wide_Character_Restriction (T, Object_Definition (N));
4465 -- Indicate this is not set in source. Certainly true for constants, and
4466 -- true for variables so far (will be reset for a variable if and when
4467 -- we encounter a modification in the source).
4469 Set_Never_Set_In_Source (Id);
4471 -- Now establish the proper kind and type of the object
4473 if Constant_Present (N) then
4474 Set_Ekind (Id, E_Constant);
4475 Set_Is_True_Constant (Id);
4478 Set_Ekind (Id, E_Variable);
4480 -- A variable is set as shared passive if it appears in a shared
4481 -- passive package, and is at the outer level. This is not done for
4482 -- entities generated during expansion, because those are always
4483 -- manipulated locally.
4485 if Is_Shared_Passive (Current_Scope)
4486 and then Is_Library_Level_Entity (Id)
4487 and then Comes_From_Source (Id)
4489 Set_Is_Shared_Passive (Id);
4490 Check_Shared_Var (Id, T, N);
4493 -- Set Has_Initial_Value if initializing expression present. Note
4494 -- that if there is no initializing expression, we leave the state
4495 -- of this flag unchanged (usually it will be False, but notably in
4496 -- the case of exception choice variables, it will already be true).
4499 Set_Has_Initial_Value (Id);
4503 -- Initialize alignment and size and capture alignment setting
4505 Init_Alignment (Id);
4507 Set_Optimize_Alignment_Flags (Id);
4509 -- Deal with aliased case
4511 if Aliased_Present (N) then
4512 Set_Is_Aliased (Id);
4514 -- If the object is aliased and the type is unconstrained with
4515 -- defaulted discriminants and there is no expression, then the
4516 -- object is constrained by the defaults, so it is worthwhile
4517 -- building the corresponding subtype.
4519 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4520 -- unconstrained, then only establish an actual subtype if the
4521 -- nominal subtype is indefinite. In definite cases the object is
4522 -- unconstrained in Ada 2005.
4525 and then Is_Record_Type (T)
4526 and then not Is_Constrained (T)
4527 and then Has_Discriminants (T)
4528 and then (Ada_Version < Ada_2005
4529 or else not Is_Definite_Subtype (T))
4531 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4535 -- Now we can set the type of the object
4537 Set_Etype (Id, Act_T);
4539 -- Non-constant object is marked to be treated as volatile if type is
4540 -- volatile and we clear the Current_Value setting that may have been
4541 -- set above. Doing so for constants isn't required and might interfere
4542 -- with possible uses of the object as a static expression in contexts
4543 -- incompatible with volatility (e.g. as a case-statement alternative).
4545 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4546 Set_Treat_As_Volatile (Id);
4547 Set_Current_Value (Id, Empty);
4550 -- Deal with controlled types
4552 if Has_Controlled_Component (Etype (Id))
4553 or else Is_Controlled (Etype (Id))
4555 if not Is_Library_Level_Entity (Id) then
4556 Check_Restriction (No_Nested_Finalization, N);
4558 Validate_Controlled_Object (Id);
4562 if Has_Task (Etype (Id)) then
4563 Check_Restriction (No_Tasking, N);
4565 -- Deal with counting max tasks
4567 -- Nothing to do if inside a generic
4569 if Inside_A_Generic then
4572 -- If library level entity, then count tasks
4574 elsif Is_Library_Level_Entity (Id) then
4575 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4577 -- If not library level entity, then indicate we don't know max
4578 -- tasks and also check task hierarchy restriction and blocking
4579 -- operation (since starting a task is definitely blocking).
4582 Check_Restriction (Max_Tasks, N);
4583 Check_Restriction (No_Task_Hierarchy, N);
4584 Check_Potentially_Blocking_Operation (N);
4587 -- A rather specialized test. If we see two tasks being declared
4588 -- of the same type in the same object declaration, and the task
4589 -- has an entry with an address clause, we know that program error
4590 -- will be raised at run time since we can't have two tasks with
4591 -- entries at the same address.
4593 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4598 E := First_Entity (Etype (Id));
4599 while Present (E) loop
4600 if Ekind (E) = E_Entry
4601 and then Present (Get_Attribute_Definition_Clause
4602 (E, Attribute_Address))
4604 Error_Msg_Warn := SPARK_Mode /= On;
4606 ("more than one task with same entry address<<", N);
4607 Error_Msg_N ("\Program_Error [<<", N);
4609 Make_Raise_Program_Error (Loc,
4610 Reason => PE_Duplicated_Entry_Address));
4620 -- Some simple constant-propagation: if the expression is a constant
4621 -- string initialized with a literal, share the literal. This avoids
4625 and then Is_Entity_Name (E)
4626 and then Ekind (Entity (E)) = E_Constant
4627 and then Base_Type (Etype (E)) = Standard_String
4630 Val : constant Node_Id := Constant_Value (Entity (E));
4632 if Present (Val) and then Nkind (Val) = N_String_Literal then
4633 Rewrite (E, New_Copy (Val));
4638 -- Another optimization: if the nominal subtype is unconstrained and
4639 -- the expression is a function call that returns an unconstrained
4640 -- type, rewrite the declaration as a renaming of the result of the
4641 -- call. The exceptions below are cases where the copy is expected,
4642 -- either by the back end (Aliased case) or by the semantics, as for
4643 -- initializing controlled types or copying tags for class-wide types.
4646 and then Nkind (E) = N_Explicit_Dereference
4647 and then Nkind (Original_Node (E)) = N_Function_Call
4648 and then not Is_Library_Level_Entity (Id)
4649 and then not Is_Constrained (Underlying_Type (T))
4650 and then not Is_Aliased (Id)
4651 and then not Is_Class_Wide_Type (T)
4652 and then not Is_Controlled_Active (T)
4653 and then not Has_Controlled_Component (Base_Type (T))
4654 and then Expander_Active
4657 Make_Object_Renaming_Declaration (Loc,
4658 Defining_Identifier => Id,
4659 Access_Definition => Empty,
4660 Subtype_Mark => New_Occurrence_Of
4661 (Base_Type (Etype (Id)), Loc),
4664 Set_Renamed_Object (Id, E);
4666 -- Force generation of debugging information for the constant and for
4667 -- the renamed function call.
4669 Set_Debug_Info_Needed (Id);
4670 Set_Debug_Info_Needed (Entity (Prefix (E)));
4673 if Present (Prev_Entity)
4674 and then Is_Frozen (Prev_Entity)
4675 and then not Error_Posted (Id)
4677 Error_Msg_N ("full constant declaration appears too late", N);
4680 Check_Eliminated (Id);
4682 -- Deal with setting In_Private_Part flag if in private part
4684 if Ekind (Scope (Id)) = E_Package
4685 and then In_Private_Part (Scope (Id))
4687 Set_In_Private_Part (Id);
4691 -- Initialize the refined state of a variable here because this is a
4692 -- common destination for legal and illegal object declarations.
4694 if Ekind (Id) = E_Variable then
4695 Set_Encapsulating_State (Id, Empty);
4698 if Has_Aspects (N) then
4699 Analyze_Aspect_Specifications (N, Id);
4702 Analyze_Dimension (N);
4704 -- Verify whether the object declaration introduces an illegal hidden
4705 -- state within a package subject to a null abstract state.
4707 if Ekind (Id) = E_Variable then
4708 Check_No_Hidden_State (Id);
4711 Restore_Ghost_Mode (Saved_GM);
4712 end Analyze_Object_Declaration;
4714 ---------------------------
4715 -- Analyze_Others_Choice --
4716 ---------------------------
4718 -- Nothing to do for the others choice node itself, the semantic analysis
4719 -- of the others choice will occur as part of the processing of the parent
4721 procedure Analyze_Others_Choice (N : Node_Id) is
4722 pragma Warnings (Off, N);
4725 end Analyze_Others_Choice;
4727 -------------------------------------------
4728 -- Analyze_Private_Extension_Declaration --
4729 -------------------------------------------
4731 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4732 Indic : constant Node_Id := Subtype_Indication (N);
4733 T : constant Entity_Id := Defining_Identifier (N);
4735 Iface_Elmt : Elmt_Id;
4736 Parent_Base : Entity_Id;
4737 Parent_Type : Entity_Id;
4740 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4742 if Is_Non_Empty_List (Interface_List (N)) then
4748 Intf := First (Interface_List (N));
4749 while Present (Intf) loop
4750 T := Find_Type_Of_Subtype_Indic (Intf);
4752 Diagnose_Interface (Intf, T);
4758 Generate_Definition (T);
4760 -- For other than Ada 2012, just enter the name in the current scope
4762 if Ada_Version < Ada_2012 then
4765 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4766 -- case of private type that completes an incomplete type.
4773 Prev := Find_Type_Name (N);
4775 pragma Assert (Prev = T
4776 or else (Ekind (Prev) = E_Incomplete_Type
4777 and then Present (Full_View (Prev))
4778 and then Full_View (Prev) = T));
4782 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4783 Parent_Base := Base_Type (Parent_Type);
4785 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4786 Set_Ekind (T, Ekind (Parent_Type));
4787 Set_Etype (T, Any_Type);
4790 elsif not Is_Tagged_Type (Parent_Type) then
4792 ("parent of type extension must be a tagged type ", Indic);
4795 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4796 Error_Msg_N ("premature derivation of incomplete type", Indic);
4799 elsif Is_Concurrent_Type (Parent_Type) then
4801 ("parent type of a private extension cannot be a synchronized "
4802 & "tagged type (RM 3.9.1 (3/1))", N);
4804 Set_Etype (T, Any_Type);
4805 Set_Ekind (T, E_Limited_Private_Type);
4806 Set_Private_Dependents (T, New_Elmt_List);
4807 Set_Error_Posted (T);
4811 -- Perhaps the parent type should be changed to the class-wide type's
4812 -- specific type in this case to prevent cascading errors ???
4814 if Is_Class_Wide_Type (Parent_Type) then
4816 ("parent of type extension must not be a class-wide type", Indic);
4820 if (not Is_Package_Or_Generic_Package (Current_Scope)
4821 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4822 or else In_Private_Part (Current_Scope)
4824 Error_Msg_N ("invalid context for private extension", N);
4827 -- Set common attributes
4829 Set_Is_Pure (T, Is_Pure (Current_Scope));
4830 Set_Scope (T, Current_Scope);
4831 Set_Ekind (T, E_Record_Type_With_Private);
4832 Init_Size_Align (T);
4833 Set_Default_SSO (T);
4835 Set_Etype (T, Parent_Base);
4836 Propagate_Concurrent_Flags (T, Parent_Base);
4838 Set_Convention (T, Convention (Parent_Type));
4839 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4840 Set_Is_First_Subtype (T);
4841 Make_Class_Wide_Type (T);
4843 if Unknown_Discriminants_Present (N) then
4844 Set_Discriminant_Constraint (T, No_Elist);
4847 Build_Derived_Record_Type (N, Parent_Type, T);
4849 -- A private extension inherits the Default_Initial_Condition pragma
4850 -- coming from any parent type within the derivation chain.
4852 if Has_DIC (Parent_Type) then
4853 Set_Has_Inherited_DIC (T);
4856 -- A private extension inherits any class-wide invariants coming from a
4857 -- parent type or an interface. Note that the invariant procedure of the
4858 -- parent type should not be inherited because the private extension may
4859 -- define invariants of its own.
4861 if Has_Inherited_Invariants (Parent_Type)
4862 or else Has_Inheritable_Invariants (Parent_Type)
4864 Set_Has_Inherited_Invariants (T);
4866 elsif Present (Interfaces (T)) then
4867 Iface_Elmt := First_Elmt (Interfaces (T));
4868 while Present (Iface_Elmt) loop
4869 Iface := Node (Iface_Elmt);
4871 if Has_Inheritable_Invariants (Iface) then
4872 Set_Has_Inherited_Invariants (T);
4876 Next_Elmt (Iface_Elmt);
4880 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4881 -- synchronized formal derived type.
4883 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4884 Set_Is_Limited_Record (T);
4886 -- Formal derived type case
4888 if Is_Generic_Type (T) then
4890 -- The parent must be a tagged limited type or a synchronized
4893 if (not Is_Tagged_Type (Parent_Type)
4894 or else not Is_Limited_Type (Parent_Type))
4896 (not Is_Interface (Parent_Type)
4897 or else not Is_Synchronized_Interface (Parent_Type))
4900 ("parent type of & must be tagged limited or synchronized",
4904 -- The progenitors (if any) must be limited or synchronized
4907 if Present (Interfaces (T)) then
4908 Iface_Elmt := First_Elmt (Interfaces (T));
4909 while Present (Iface_Elmt) loop
4910 Iface := Node (Iface_Elmt);
4912 if not Is_Limited_Interface (Iface)
4913 and then not Is_Synchronized_Interface (Iface)
4916 ("progenitor & must be limited or synchronized",
4920 Next_Elmt (Iface_Elmt);
4924 -- Regular derived extension, the parent must be a limited or
4925 -- synchronized interface.
4928 if not Is_Interface (Parent_Type)
4929 or else (not Is_Limited_Interface (Parent_Type)
4930 and then not Is_Synchronized_Interface (Parent_Type))
4933 ("parent type of & must be limited interface", N, T);
4937 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4938 -- extension with a synchronized parent must be explicitly declared
4939 -- synchronized, because the full view will be a synchronized type.
4940 -- This must be checked before the check for limited types below,
4941 -- to ensure that types declared limited are not allowed to extend
4942 -- synchronized interfaces.
4944 elsif Is_Interface (Parent_Type)
4945 and then Is_Synchronized_Interface (Parent_Type)
4946 and then not Synchronized_Present (N)
4949 ("private extension of& must be explicitly synchronized",
4952 elsif Limited_Present (N) then
4953 Set_Is_Limited_Record (T);
4955 if not Is_Limited_Type (Parent_Type)
4957 (not Is_Interface (Parent_Type)
4958 or else not Is_Limited_Interface (Parent_Type))
4960 Error_Msg_NE ("parent type& of limited extension must be limited",
4965 -- Remember that its parent type has a private extension. Used to warn
4966 -- on public primitives of the parent type defined after its private
4967 -- extensions (see Check_Dispatching_Operation).
4969 Set_Has_Private_Extension (Parent_Type);
4972 if Has_Aspects (N) then
4973 Analyze_Aspect_Specifications (N, T);
4975 end Analyze_Private_Extension_Declaration;
4977 ---------------------------------
4978 -- Analyze_Subtype_Declaration --
4979 ---------------------------------
4981 procedure Analyze_Subtype_Declaration
4983 Skip : Boolean := False)
4985 Id : constant Entity_Id := Defining_Identifier (N);
4986 R_Checks : Check_Result;
4990 Generate_Definition (Id);
4991 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4992 Init_Size_Align (Id);
4994 -- The following guard condition on Enter_Name is to handle cases where
4995 -- the defining identifier has already been entered into the scope but
4996 -- the declaration as a whole needs to be analyzed.
4998 -- This case in particular happens for derived enumeration types. The
4999 -- derived enumeration type is processed as an inserted enumeration type
5000 -- declaration followed by a rewritten subtype declaration. The defining
5001 -- identifier, however, is entered into the name scope very early in the
5002 -- processing of the original type declaration and therefore needs to be
5003 -- avoided here, when the created subtype declaration is analyzed. (See
5004 -- Build_Derived_Types)
5006 -- This also happens when the full view of a private type is derived
5007 -- type with constraints. In this case the entity has been introduced
5008 -- in the private declaration.
5010 -- Finally this happens in some complex cases when validity checks are
5011 -- enabled, where the same subtype declaration may be analyzed twice.
5012 -- This can happen if the subtype is created by the pre-analysis of
5013 -- an attribute tht gives the range of a loop statement, and the loop
5014 -- itself appears within an if_statement that will be rewritten during
5018 or else (Present (Etype (Id))
5019 and then (Is_Private_Type (Etype (Id))
5020 or else Is_Task_Type (Etype (Id))
5021 or else Is_Rewrite_Substitution (N)))
5025 elsif Current_Entity (Id) = Id then
5032 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5034 -- Class-wide equivalent types of records with unknown discriminants
5035 -- involve the generation of an itype which serves as the private view
5036 -- of a constrained record subtype. In such cases the base type of the
5037 -- current subtype we are processing is the private itype. Use the full
5038 -- of the private itype when decorating various attributes.
5041 and then Is_Private_Type (T)
5042 and then Present (Full_View (T))
5047 -- Inherit common attributes
5049 Set_Is_Volatile (Id, Is_Volatile (T));
5050 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5051 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5052 Set_Convention (Id, Convention (T));
5054 -- If ancestor has predicates then so does the subtype, and in addition
5055 -- we must delay the freeze to properly arrange predicate inheritance.
5057 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5058 -- in which T = ID, so the above tests and assignments do nothing???
5060 if Has_Predicates (T)
5061 or else (Present (Ancestor_Subtype (T))
5062 and then Has_Predicates (Ancestor_Subtype (T)))
5064 Set_Has_Predicates (Id);
5065 Set_Has_Delayed_Freeze (Id);
5067 -- Generated subtypes inherit the predicate function from the parent
5068 -- (no aspects to examine on the generated declaration).
5070 if not Comes_From_Source (N) then
5071 Set_Ekind (Id, Ekind (T));
5073 if Present (Predicate_Function (T)) then
5074 Set_Predicate_Function (Id, Predicate_Function (T));
5076 elsif Present (Ancestor_Subtype (T))
5077 and then Has_Predicates (Ancestor_Subtype (T))
5078 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5080 Set_Predicate_Function (Id,
5081 Predicate_Function (Ancestor_Subtype (T)));
5086 -- Subtype of Boolean cannot have a constraint in SPARK
5088 if Is_Boolean_Type (T)
5089 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5091 Check_SPARK_05_Restriction
5092 ("subtype of Boolean cannot have constraint", N);
5095 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5097 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5103 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5104 One_Cstr := First (Constraints (Cstr));
5105 while Present (One_Cstr) loop
5107 -- Index or discriminant constraint in SPARK must be a
5111 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5113 Check_SPARK_05_Restriction
5114 ("subtype mark required", One_Cstr);
5116 -- String subtype must have a lower bound of 1 in SPARK.
5117 -- Note that we do not need to test for the non-static case
5118 -- here, since that was already taken care of in
5119 -- Process_Range_Expr_In_Decl.
5121 elsif Base_Type (T) = Standard_String then
5122 Get_Index_Bounds (One_Cstr, Low, High);
5124 if Is_OK_Static_Expression (Low)
5125 and then Expr_Value (Low) /= 1
5127 Check_SPARK_05_Restriction
5128 ("String subtype must have lower bound of 1", N);
5138 -- In the case where there is no constraint given in the subtype
5139 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5140 -- semantic attributes must be established here.
5142 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5143 Set_Etype (Id, Base_Type (T));
5145 -- Subtype of unconstrained array without constraint is not allowed
5148 if Is_Array_Type (T) and then not Is_Constrained (T) then
5149 Check_SPARK_05_Restriction
5150 ("subtype of unconstrained array must have constraint", N);
5155 Set_Ekind (Id, E_Array_Subtype);
5156 Copy_Array_Subtype_Attributes (Id, T);
5158 when Decimal_Fixed_Point_Kind =>
5159 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5160 Set_Digits_Value (Id, Digits_Value (T));
5161 Set_Delta_Value (Id, Delta_Value (T));
5162 Set_Scale_Value (Id, Scale_Value (T));
5163 Set_Small_Value (Id, Small_Value (T));
5164 Set_Scalar_Range (Id, Scalar_Range (T));
5165 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5166 Set_Is_Constrained (Id, Is_Constrained (T));
5167 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5168 Set_RM_Size (Id, RM_Size (T));
5170 when Enumeration_Kind =>
5171 Set_Ekind (Id, E_Enumeration_Subtype);
5172 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5173 Set_Scalar_Range (Id, Scalar_Range (T));
5174 Set_Is_Character_Type (Id, Is_Character_Type (T));
5175 Set_Is_Constrained (Id, Is_Constrained (T));
5176 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5177 Set_RM_Size (Id, RM_Size (T));
5178 Inherit_Predicate_Flags (Id, T);
5180 when Ordinary_Fixed_Point_Kind =>
5181 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5182 Set_Scalar_Range (Id, Scalar_Range (T));
5183 Set_Small_Value (Id, Small_Value (T));
5184 Set_Delta_Value (Id, Delta_Value (T));
5185 Set_Is_Constrained (Id, Is_Constrained (T));
5186 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5187 Set_RM_Size (Id, RM_Size (T));
5190 Set_Ekind (Id, E_Floating_Point_Subtype);
5191 Set_Scalar_Range (Id, Scalar_Range (T));
5192 Set_Digits_Value (Id, Digits_Value (T));
5193 Set_Is_Constrained (Id, Is_Constrained (T));
5195 -- If the floating point type has dimensions, these will be
5196 -- inherited subsequently when Analyze_Dimensions is called.
5198 when Signed_Integer_Kind =>
5199 Set_Ekind (Id, E_Signed_Integer_Subtype);
5200 Set_Scalar_Range (Id, Scalar_Range (T));
5201 Set_Is_Constrained (Id, Is_Constrained (T));
5202 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5203 Set_RM_Size (Id, RM_Size (T));
5204 Inherit_Predicate_Flags (Id, T);
5206 when Modular_Integer_Kind =>
5207 Set_Ekind (Id, E_Modular_Integer_Subtype);
5208 Set_Scalar_Range (Id, Scalar_Range (T));
5209 Set_Is_Constrained (Id, Is_Constrained (T));
5210 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5211 Set_RM_Size (Id, RM_Size (T));
5212 Inherit_Predicate_Flags (Id, T);
5214 when Class_Wide_Kind =>
5215 Set_Ekind (Id, E_Class_Wide_Subtype);
5216 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5217 Set_Cloned_Subtype (Id, T);
5218 Set_Is_Tagged_Type (Id, True);
5219 Set_Has_Unknown_Discriminants
5221 Set_No_Tagged_Streams_Pragma
5222 (Id, No_Tagged_Streams_Pragma (T));
5224 if Ekind (T) = E_Class_Wide_Subtype then
5225 Set_Equivalent_Type (Id, Equivalent_Type (T));
5228 when E_Record_Subtype
5231 Set_Ekind (Id, E_Record_Subtype);
5233 if Ekind (T) = E_Record_Subtype
5234 and then Present (Cloned_Subtype (T))
5236 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5238 Set_Cloned_Subtype (Id, T);
5241 Set_First_Entity (Id, First_Entity (T));
5242 Set_Last_Entity (Id, Last_Entity (T));
5243 Set_Has_Discriminants (Id, Has_Discriminants (T));
5244 Set_Is_Constrained (Id, Is_Constrained (T));
5245 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5246 Set_Has_Implicit_Dereference
5247 (Id, Has_Implicit_Dereference (T));
5248 Set_Has_Unknown_Discriminants
5249 (Id, Has_Unknown_Discriminants (T));
5251 if Has_Discriminants (T) then
5252 Set_Discriminant_Constraint
5253 (Id, Discriminant_Constraint (T));
5254 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5256 elsif Has_Unknown_Discriminants (Id) then
5257 Set_Discriminant_Constraint (Id, No_Elist);
5260 if Is_Tagged_Type (T) then
5261 Set_Is_Tagged_Type (Id, True);
5262 Set_No_Tagged_Streams_Pragma
5263 (Id, No_Tagged_Streams_Pragma (T));
5264 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5265 Set_Direct_Primitive_Operations
5266 (Id, Direct_Primitive_Operations (T));
5267 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5269 if Is_Interface (T) then
5270 Set_Is_Interface (Id);
5271 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5275 when Private_Kind =>
5276 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5277 Set_Has_Discriminants (Id, Has_Discriminants (T));
5278 Set_Is_Constrained (Id, Is_Constrained (T));
5279 Set_First_Entity (Id, First_Entity (T));
5280 Set_Last_Entity (Id, Last_Entity (T));
5281 Set_Private_Dependents (Id, New_Elmt_List);
5282 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5283 Set_Has_Implicit_Dereference
5284 (Id, Has_Implicit_Dereference (T));
5285 Set_Has_Unknown_Discriminants
5286 (Id, Has_Unknown_Discriminants (T));
5287 Set_Known_To_Have_Preelab_Init
5288 (Id, Known_To_Have_Preelab_Init (T));
5290 if Is_Tagged_Type (T) then
5291 Set_Is_Tagged_Type (Id);
5292 Set_No_Tagged_Streams_Pragma (Id,
5293 No_Tagged_Streams_Pragma (T));
5294 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5295 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5296 Set_Direct_Primitive_Operations (Id,
5297 Direct_Primitive_Operations (T));
5300 -- In general the attributes of the subtype of a private type
5301 -- are the attributes of the partial view of parent. However,
5302 -- the full view may be a discriminated type, and the subtype
5303 -- must share the discriminant constraint to generate correct
5304 -- calls to initialization procedures.
5306 if Has_Discriminants (T) then
5307 Set_Discriminant_Constraint
5308 (Id, Discriminant_Constraint (T));
5309 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5311 elsif Present (Full_View (T))
5312 and then Has_Discriminants (Full_View (T))
5314 Set_Discriminant_Constraint
5315 (Id, Discriminant_Constraint (Full_View (T)));
5316 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5318 -- This would seem semantically correct, but apparently
5319 -- generates spurious errors about missing components ???
5321 -- Set_Has_Discriminants (Id);
5324 Prepare_Private_Subtype_Completion (Id, N);
5326 -- If this is the subtype of a constrained private type with
5327 -- discriminants that has got a full view and we also have
5328 -- built a completion just above, show that the completion
5329 -- is a clone of the full view to the back-end.
5331 if Has_Discriminants (T)
5332 and then not Has_Unknown_Discriminants (T)
5333 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5334 and then Present (Full_View (T))
5335 and then Present (Full_View (Id))
5337 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5341 Set_Ekind (Id, E_Access_Subtype);
5342 Set_Is_Constrained (Id, Is_Constrained (T));
5343 Set_Is_Access_Constant
5344 (Id, Is_Access_Constant (T));
5345 Set_Directly_Designated_Type
5346 (Id, Designated_Type (T));
5347 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5349 -- A Pure library_item must not contain the declaration of a
5350 -- named access type, except within a subprogram, generic
5351 -- subprogram, task unit, or protected unit, or if it has
5352 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5354 if Comes_From_Source (Id)
5355 and then In_Pure_Unit
5356 and then not In_Subprogram_Task_Protected_Unit
5357 and then not No_Pool_Assigned (Id)
5360 ("named access types not allowed in pure unit", N);
5363 when Concurrent_Kind =>
5364 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5365 Set_Corresponding_Record_Type (Id,
5366 Corresponding_Record_Type (T));
5367 Set_First_Entity (Id, First_Entity (T));
5368 Set_First_Private_Entity (Id, First_Private_Entity (T));
5369 Set_Has_Discriminants (Id, Has_Discriminants (T));
5370 Set_Is_Constrained (Id, Is_Constrained (T));
5371 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5372 Set_Last_Entity (Id, Last_Entity (T));
5374 if Is_Tagged_Type (T) then
5375 Set_No_Tagged_Streams_Pragma
5376 (Id, No_Tagged_Streams_Pragma (T));
5379 if Has_Discriminants (T) then
5380 Set_Discriminant_Constraint
5381 (Id, Discriminant_Constraint (T));
5382 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5385 when Incomplete_Kind =>
5386 if Ada_Version >= Ada_2005 then
5388 -- In Ada 2005 an incomplete type can be explicitly tagged:
5389 -- propagate indication. Note that we also have to include
5390 -- subtypes for Ada 2012 extended use of incomplete types.
5392 Set_Ekind (Id, E_Incomplete_Subtype);
5393 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5394 Set_Private_Dependents (Id, New_Elmt_List);
5396 if Is_Tagged_Type (Id) then
5397 Set_No_Tagged_Streams_Pragma
5398 (Id, No_Tagged_Streams_Pragma (T));
5399 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5402 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5403 -- incomplete type visible through a limited with clause.
5405 if From_Limited_With (T)
5406 and then Present (Non_Limited_View (T))
5408 Set_From_Limited_With (Id);
5409 Set_Non_Limited_View (Id, Non_Limited_View (T));
5411 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5412 -- to the private dependents of the original incomplete
5413 -- type for future transformation.
5416 Append_Elmt (Id, Private_Dependents (T));
5419 -- If the subtype name denotes an incomplete type an error
5420 -- was already reported by Process_Subtype.
5423 Set_Etype (Id, Any_Type);
5427 raise Program_Error;
5431 if Etype (Id) = Any_Type then
5435 -- Some common processing on all types
5437 Set_Size_Info (Id, T);
5438 Set_First_Rep_Item (Id, First_Rep_Item (T));
5440 -- If the parent type is a generic actual, so is the subtype. This may
5441 -- happen in a nested instance. Why Comes_From_Source test???
5443 if not Comes_From_Source (N) then
5444 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5447 -- If this is a subtype declaration for an actual in an instance,
5448 -- inherit static and dynamic predicates if any.
5450 -- If declaration has no aspect specifications, inherit predicate
5451 -- info as well. Unclear how to handle the case of both specified
5452 -- and inherited predicates ??? Other inherited aspects, such as
5453 -- invariants, should be OK, but the combination with later pragmas
5454 -- may also require special merging.
5456 if Has_Predicates (T)
5457 and then Present (Predicate_Function (T))
5459 ((In_Instance and then not Comes_From_Source (N))
5460 or else No (Aspect_Specifications (N)))
5462 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5464 if Has_Static_Predicate (T) then
5465 Set_Has_Static_Predicate (Id);
5466 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5470 -- Remaining processing depends on characteristics of base type
5474 Set_Is_Immediately_Visible (Id, True);
5475 Set_Depends_On_Private (Id, Has_Private_Component (T));
5476 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5478 if Is_Interface (T) then
5479 Set_Is_Interface (Id);
5482 if Present (Generic_Parent_Type (N))
5484 (Nkind (Parent (Generic_Parent_Type (N))) /=
5485 N_Formal_Type_Declaration
5486 or else Nkind (Formal_Type_Definition
5487 (Parent (Generic_Parent_Type (N)))) /=
5488 N_Formal_Private_Type_Definition)
5490 if Is_Tagged_Type (Id) then
5492 -- If this is a generic actual subtype for a synchronized type,
5493 -- the primitive operations are those of the corresponding record
5494 -- for which there is a separate subtype declaration.
5496 if Is_Concurrent_Type (Id) then
5498 elsif Is_Class_Wide_Type (Id) then
5499 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5501 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5504 elsif Scope (Etype (Id)) /= Standard_Standard then
5505 Derive_Subprograms (Generic_Parent_Type (N), Id);
5509 if Is_Private_Type (T) and then Present (Full_View (T)) then
5510 Conditional_Delay (Id, Full_View (T));
5512 -- The subtypes of components or subcomponents of protected types
5513 -- do not need freeze nodes, which would otherwise appear in the
5514 -- wrong scope (before the freeze node for the protected type). The
5515 -- proper subtypes are those of the subcomponents of the corresponding
5518 elsif Ekind (Scope (Id)) /= E_Protected_Type
5519 and then Present (Scope (Scope (Id))) -- error defense
5520 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5522 Conditional_Delay (Id, T);
5525 -- Check that Constraint_Error is raised for a scalar subtype indication
5526 -- when the lower or upper bound of a non-null range lies outside the
5527 -- range of the type mark.
5529 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5530 if Is_Scalar_Type (Etype (Id))
5531 and then Scalar_Range (Id) /=
5533 (Etype (Subtype_Mark (Subtype_Indication (N))))
5537 Etype (Subtype_Mark (Subtype_Indication (N))));
5539 -- In the array case, check compatibility for each index
5541 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5543 -- This really should be a subprogram that finds the indications
5547 Subt_Index : Node_Id := First_Index (Id);
5548 Target_Index : Node_Id :=
5550 (Subtype_Mark (Subtype_Indication (N))));
5551 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5554 while Present (Subt_Index) loop
5555 if ((Nkind (Subt_Index) = N_Identifier
5556 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5557 or else Nkind (Subt_Index) = N_Subtype_Indication)
5559 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5562 Target_Typ : constant Entity_Id :=
5563 Etype (Target_Index);
5567 (Scalar_Range (Etype (Subt_Index)),
5570 Defining_Identifier (N));
5572 -- Reset Has_Dynamic_Range_Check on the subtype to
5573 -- prevent elision of the index check due to a dynamic
5574 -- check generated for a preceding index (needed since
5575 -- Insert_Range_Checks tries to avoid generating
5576 -- redundant checks on a given declaration).
5578 Set_Has_Dynamic_Range_Check (N, False);
5584 Sloc (Defining_Identifier (N)));
5586 -- Record whether this index involved a dynamic check
5589 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5593 Next_Index (Subt_Index);
5594 Next_Index (Target_Index);
5597 -- Finally, mark whether the subtype involves dynamic checks
5599 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5604 Set_Optimize_Alignment_Flags (Id);
5605 Check_Eliminated (Id);
5608 if Has_Aspects (N) then
5609 Analyze_Aspect_Specifications (N, Id);
5612 Analyze_Dimension (N);
5614 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5615 -- indications on composite types where the constraints are dynamic.
5616 -- Note that object declarations and aggregates generate implicit
5617 -- subtype declarations, which this covers. One special case is that the
5618 -- implicitly generated "=" for discriminated types includes an
5619 -- offending subtype declaration, which is harmless, so we ignore it
5622 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5624 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5626 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5627 and then not (Is_Internal (Id)
5628 and then Is_TSS (Scope (Id),
5629 TSS_Composite_Equality))
5630 and then not Within_Init_Proc
5631 and then not All_Composite_Constraints_Static (Cstr)
5633 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5637 end Analyze_Subtype_Declaration;
5639 --------------------------------
5640 -- Analyze_Subtype_Indication --
5641 --------------------------------
5643 procedure Analyze_Subtype_Indication (N : Node_Id) is
5644 T : constant Entity_Id := Subtype_Mark (N);
5645 R : constant Node_Id := Range_Expression (Constraint (N));
5652 Set_Etype (N, Etype (R));
5653 Resolve (R, Entity (T));
5655 Set_Error_Posted (R);
5656 Set_Error_Posted (T);
5658 end Analyze_Subtype_Indication;
5660 --------------------------
5661 -- Analyze_Variant_Part --
5662 --------------------------
5664 procedure Analyze_Variant_Part (N : Node_Id) is
5665 Discr_Name : Node_Id;
5666 Discr_Type : Entity_Id;
5668 procedure Process_Variant (A : Node_Id);
5669 -- Analyze declarations for a single variant
5671 package Analyze_Variant_Choices is
5672 new Generic_Analyze_Choices (Process_Variant);
5673 use Analyze_Variant_Choices;
5675 ---------------------
5676 -- Process_Variant --
5677 ---------------------
5679 procedure Process_Variant (A : Node_Id) is
5680 CL : constant Node_Id := Component_List (A);
5682 if not Null_Present (CL) then
5683 Analyze_Declarations (Component_Items (CL));
5685 if Present (Variant_Part (CL)) then
5686 Analyze (Variant_Part (CL));
5689 end Process_Variant;
5691 -- Start of processing for Analyze_Variant_Part
5694 Discr_Name := Name (N);
5695 Analyze (Discr_Name);
5697 -- If Discr_Name bad, get out (prevent cascaded errors)
5699 if Etype (Discr_Name) = Any_Type then
5703 -- Check invalid discriminant in variant part
5705 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5706 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5709 Discr_Type := Etype (Entity (Discr_Name));
5711 if not Is_Discrete_Type (Discr_Type) then
5713 ("discriminant in a variant part must be of a discrete type",
5718 -- Now analyze the choices, which also analyzes the declarations that
5719 -- are associated with each choice.
5721 Analyze_Choices (Variants (N), Discr_Type);
5723 -- Note: we used to instantiate and call Check_Choices here to check
5724 -- that the choices covered the discriminant, but it's too early to do
5725 -- that because of statically predicated subtypes, whose analysis may
5726 -- be deferred to their freeze point which may be as late as the freeze
5727 -- point of the containing record. So this call is now to be found in
5728 -- Freeze_Record_Declaration.
5730 end Analyze_Variant_Part;
5732 ----------------------------
5733 -- Array_Type_Declaration --
5734 ----------------------------
5736 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5737 Component_Def : constant Node_Id := Component_Definition (Def);
5738 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5739 P : constant Node_Id := Parent (Def);
5740 Element_Type : Entity_Id;
5741 Implicit_Base : Entity_Id;
5745 Related_Id : Entity_Id := Empty;
5748 if Nkind (Def) = N_Constrained_Array_Definition then
5749 Index := First (Discrete_Subtype_Definitions (Def));
5751 Index := First (Subtype_Marks (Def));
5754 -- Find proper names for the implicit types which may be public. In case
5755 -- of anonymous arrays we use the name of the first object of that type
5759 Related_Id := Defining_Identifier (P);
5765 while Present (Index) loop
5768 -- Test for odd case of trying to index a type by the type itself
5770 if Is_Entity_Name (Index) and then Entity (Index) = T then
5771 Error_Msg_N ("type& cannot be indexed by itself", Index);
5772 Set_Entity (Index, Standard_Boolean);
5773 Set_Etype (Index, Standard_Boolean);
5776 -- Check SPARK restriction requiring a subtype mark
5778 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5779 Check_SPARK_05_Restriction ("subtype mark required", Index);
5782 -- Add a subtype declaration for each index of private array type
5783 -- declaration whose etype is also private. For example:
5786 -- type Index is private;
5788 -- type Table is array (Index) of ...
5791 -- This is currently required by the expander for the internally
5792 -- generated equality subprogram of records with variant parts in
5793 -- which the etype of some component is such private type.
5795 if Ekind (Current_Scope) = E_Package
5796 and then In_Private_Part (Current_Scope)
5797 and then Has_Private_Declaration (Etype (Index))
5800 Loc : constant Source_Ptr := Sloc (Def);
5805 New_E := Make_Temporary (Loc, 'T');
5806 Set_Is_Internal (New_E);
5809 Make_Subtype_Declaration (Loc,
5810 Defining_Identifier => New_E,
5811 Subtype_Indication =>
5812 New_Occurrence_Of (Etype (Index), Loc));
5814 Insert_Before (Parent (Def), Decl);
5816 Set_Etype (Index, New_E);
5818 -- If the index is a range the Entity attribute is not
5819 -- available. Example:
5822 -- type T is private;
5824 -- type T is new Natural;
5825 -- Table : array (T(1) .. T(10)) of Boolean;
5828 if Nkind (Index) /= N_Range then
5829 Set_Entity (Index, New_E);
5834 Make_Index (Index, P, Related_Id, Nb_Index);
5836 -- Check error of subtype with predicate for index type
5838 Bad_Predicated_Subtype_Use
5839 ("subtype& has predicate, not allowed as index subtype",
5840 Index, Etype (Index));
5842 -- Move to next index
5845 Nb_Index := Nb_Index + 1;
5848 -- Process subtype indication if one is present
5850 if Present (Component_Typ) then
5851 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5853 Set_Etype (Component_Typ, Element_Type);
5855 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5856 Check_SPARK_05_Restriction
5857 ("subtype mark required", Component_Typ);
5860 -- Ada 2005 (AI-230): Access Definition case
5862 else pragma Assert (Present (Access_Definition (Component_Def)));
5864 -- Indicate that the anonymous access type is created by the
5865 -- array type declaration.
5867 Element_Type := Access_Definition
5869 N => Access_Definition (Component_Def));
5870 Set_Is_Local_Anonymous_Access (Element_Type);
5872 -- Propagate the parent. This field is needed if we have to generate
5873 -- the master_id associated with an anonymous access to task type
5874 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5876 Set_Parent (Element_Type, Parent (T));
5878 -- Ada 2005 (AI-230): In case of components that are anonymous access
5879 -- types the level of accessibility depends on the enclosing type
5882 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5884 -- Ada 2005 (AI-254)
5887 CD : constant Node_Id :=
5888 Access_To_Subprogram_Definition
5889 (Access_Definition (Component_Def));
5891 if Present (CD) and then Protected_Present (CD) then
5893 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5898 -- Constrained array case
5901 T := Create_Itype (E_Void, P, Related_Id, 'T');
5904 if Nkind (Def) = N_Constrained_Array_Definition then
5906 -- Establish Implicit_Base as unconstrained base type
5908 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5910 Set_Etype (Implicit_Base, Implicit_Base);
5911 Set_Scope (Implicit_Base, Current_Scope);
5912 Set_Has_Delayed_Freeze (Implicit_Base);
5913 Set_Default_SSO (Implicit_Base);
5915 -- The constrained array type is a subtype of the unconstrained one
5917 Set_Ekind (T, E_Array_Subtype);
5918 Init_Size_Align (T);
5919 Set_Etype (T, Implicit_Base);
5920 Set_Scope (T, Current_Scope);
5921 Set_Is_Constrained (T);
5923 First (Discrete_Subtype_Definitions (Def)));
5924 Set_Has_Delayed_Freeze (T);
5926 -- Complete setup of implicit base type
5928 Set_Component_Size (Implicit_Base, Uint_0);
5929 Set_Component_Type (Implicit_Base, Element_Type);
5930 Set_Finalize_Storage_Only
5932 Finalize_Storage_Only (Element_Type));
5933 Set_First_Index (Implicit_Base, First_Index (T));
5934 Set_Has_Controlled_Component
5936 Has_Controlled_Component (Element_Type)
5937 or else Is_Controlled_Active (Element_Type));
5938 Set_Packed_Array_Impl_Type
5939 (Implicit_Base, Empty);
5941 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
5943 -- Unconstrained array case
5946 Set_Ekind (T, E_Array_Type);
5947 Init_Size_Align (T);
5949 Set_Scope (T, Current_Scope);
5950 Set_Component_Size (T, Uint_0);
5951 Set_Is_Constrained (T, False);
5952 Set_First_Index (T, First (Subtype_Marks (Def)));
5953 Set_Has_Delayed_Freeze (T, True);
5954 Propagate_Concurrent_Flags (T, Element_Type);
5955 Set_Has_Controlled_Component (T, Has_Controlled_Component
5958 Is_Controlled_Active (Element_Type));
5959 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5961 Set_Default_SSO (T);
5964 -- Common attributes for both cases
5966 Set_Component_Type (Base_Type (T), Element_Type);
5967 Set_Packed_Array_Impl_Type (T, Empty);
5969 if Aliased_Present (Component_Definition (Def)) then
5970 Check_SPARK_05_Restriction
5971 ("aliased is not allowed", Component_Definition (Def));
5972 Set_Has_Aliased_Components (Etype (T));
5975 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5976 -- array type to ensure that objects of this type are initialized.
5978 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5979 Set_Can_Never_Be_Null (T);
5981 if Null_Exclusion_Present (Component_Definition (Def))
5983 -- No need to check itypes because in their case this check was
5984 -- done at their point of creation
5986 and then not Is_Itype (Element_Type)
5989 ("`NOT NULL` not allowed (null already excluded)",
5990 Subtype_Indication (Component_Definition (Def)));
5994 Priv := Private_Component (Element_Type);
5996 if Present (Priv) then
5998 -- Check for circular definitions
6000 if Priv = Any_Type then
6001 Set_Component_Type (Etype (T), Any_Type);
6003 -- There is a gap in the visibility of operations on the composite
6004 -- type only if the component type is defined in a different scope.
6006 elsif Scope (Priv) = Current_Scope then
6009 elsif Is_Limited_Type (Priv) then
6010 Set_Is_Limited_Composite (Etype (T));
6011 Set_Is_Limited_Composite (T);
6013 Set_Is_Private_Composite (Etype (T));
6014 Set_Is_Private_Composite (T);
6018 -- A syntax error in the declaration itself may lead to an empty index
6019 -- list, in which case do a minimal patch.
6021 if No (First_Index (T)) then
6022 Error_Msg_N ("missing index definition in array type declaration", T);
6025 Indexes : constant List_Id :=
6026 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6028 Set_Discrete_Subtype_Definitions (Def, Indexes);
6029 Set_First_Index (T, First (Indexes));
6034 -- Create a concatenation operator for the new type. Internal array
6035 -- types created for packed entities do not need such, they are
6036 -- compatible with the user-defined type.
6038 if Number_Dimensions (T) = 1
6039 and then not Is_Packed_Array_Impl_Type (T)
6041 New_Concatenation_Op (T);
6044 -- In the case of an unconstrained array the parser has already verified
6045 -- that all the indexes are unconstrained but we still need to make sure
6046 -- that the element type is constrained.
6048 if not Is_Definite_Subtype (Element_Type) then
6050 ("unconstrained element type in array declaration",
6051 Subtype_Indication (Component_Def));
6053 elsif Is_Abstract_Type (Element_Type) then
6055 ("the type of a component cannot be abstract",
6056 Subtype_Indication (Component_Def));
6059 -- There may be an invariant declared for the component type, but
6060 -- the construction of the component invariant checking procedure
6061 -- takes place during expansion.
6062 end Array_Type_Declaration;
6064 ------------------------------------------------------
6065 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6066 ------------------------------------------------------
6068 function Replace_Anonymous_Access_To_Protected_Subprogram
6069 (N : Node_Id) return Entity_Id
6071 Loc : constant Source_Ptr := Sloc (N);
6073 Curr_Scope : constant Scope_Stack_Entry :=
6074 Scope_Stack.Table (Scope_Stack.Last);
6076 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6079 -- Access definition in declaration
6082 -- Object definition or formal definition with an access definition
6085 -- Declaration of anonymous access to subprogram type
6088 -- Original specification in access to subprogram
6093 Set_Is_Internal (Anon);
6096 when N_Constrained_Array_Definition
6097 | N_Component_Declaration
6098 | N_Unconstrained_Array_Definition
6100 Comp := Component_Definition (N);
6101 Acc := Access_Definition (Comp);
6103 when N_Discriminant_Specification =>
6104 Comp := Discriminant_Type (N);
6107 when N_Parameter_Specification =>
6108 Comp := Parameter_Type (N);
6111 when N_Access_Function_Definition =>
6112 Comp := Result_Definition (N);
6115 when N_Object_Declaration =>
6116 Comp := Object_Definition (N);
6119 when N_Function_Specification =>
6120 Comp := Result_Definition (N);
6124 raise Program_Error;
6127 Spec := Access_To_Subprogram_Definition (Acc);
6130 Make_Full_Type_Declaration (Loc,
6131 Defining_Identifier => Anon,
6132 Type_Definition => Copy_Separate_Tree (Spec));
6134 Mark_Rewrite_Insertion (Decl);
6136 -- In ASIS mode, analyze the profile on the original node, because
6137 -- the separate copy does not provide enough links to recover the
6138 -- original tree. Analysis is limited to type annotations, within
6139 -- a temporary scope that serves as an anonymous subprogram to collect
6140 -- otherwise useless temporaries and itypes.
6144 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
6147 if Nkind (Spec) = N_Access_Function_Definition then
6148 Set_Ekind (Typ, E_Function);
6150 Set_Ekind (Typ, E_Procedure);
6153 Set_Parent (Typ, N);
6154 Set_Scope (Typ, Current_Scope);
6157 -- Nothing to do if procedure is parameterless
6159 if Present (Parameter_Specifications (Spec)) then
6160 Process_Formals (Parameter_Specifications (Spec), Spec);
6163 if Nkind (Spec) = N_Access_Function_Definition then
6165 Def : constant Node_Id := Result_Definition (Spec);
6168 -- The result might itself be an anonymous access type, so
6171 if Nkind (Def) = N_Access_Definition then
6172 if Present (Access_To_Subprogram_Definition (Def)) then
6175 Replace_Anonymous_Access_To_Protected_Subprogram
6178 Find_Type (Subtype_Mark (Def));
6191 -- Insert the new declaration in the nearest enclosing scope. If the
6192 -- parent is a body and N is its return type, the declaration belongs
6193 -- in the enclosing scope. Likewise if N is the type of a parameter.
6197 if Nkind (N) = N_Function_Specification
6198 and then Nkind (P) = N_Subprogram_Body
6201 elsif Nkind (N) = N_Parameter_Specification
6202 and then Nkind (P) in N_Subprogram_Specification
6203 and then Nkind (Parent (P)) = N_Subprogram_Body
6205 P := Parent (Parent (P));
6208 while Present (P) and then not Has_Declarations (P) loop
6212 pragma Assert (Present (P));
6214 if Nkind (P) = N_Package_Specification then
6215 Prepend (Decl, Visible_Declarations (P));
6217 Prepend (Decl, Declarations (P));
6220 -- Replace the anonymous type with an occurrence of the new declaration.
6221 -- In all cases the rewritten node does not have the null-exclusion
6222 -- attribute because (if present) it was already inherited by the
6223 -- anonymous entity (Anon). Thus, in case of components we do not
6224 -- inherit this attribute.
6226 if Nkind (N) = N_Parameter_Specification then
6227 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6228 Set_Etype (Defining_Identifier (N), Anon);
6229 Set_Null_Exclusion_Present (N, False);
6231 elsif Nkind (N) = N_Object_Declaration then
6232 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6233 Set_Etype (Defining_Identifier (N), Anon);
6235 elsif Nkind (N) = N_Access_Function_Definition then
6236 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6238 elsif Nkind (N) = N_Function_Specification then
6239 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6240 Set_Etype (Defining_Unit_Name (N), Anon);
6244 Make_Component_Definition (Loc,
6245 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6248 Mark_Rewrite_Insertion (Comp);
6250 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6251 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6252 and then not Is_Type (Current_Scope))
6255 -- Declaration can be analyzed in the current scope.
6260 -- Temporarily remove the current scope (record or subprogram) from
6261 -- the stack to add the new declarations to the enclosing scope.
6262 -- The anonymous entity is an Itype with the proper attributes.
6264 Scope_Stack.Decrement_Last;
6266 Set_Is_Itype (Anon);
6267 Set_Associated_Node_For_Itype (Anon, N);
6268 Scope_Stack.Append (Curr_Scope);
6271 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6272 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6274 end Replace_Anonymous_Access_To_Protected_Subprogram;
6276 -------------------------------
6277 -- Build_Derived_Access_Type --
6278 -------------------------------
6280 procedure Build_Derived_Access_Type
6282 Parent_Type : Entity_Id;
6283 Derived_Type : Entity_Id)
6285 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6287 Desig_Type : Entity_Id;
6289 Discr_Con_Elist : Elist_Id;
6290 Discr_Con_El : Elmt_Id;
6294 -- Set the designated type so it is available in case this is an access
6295 -- to a self-referential type, e.g. a standard list type with a next
6296 -- pointer. Will be reset after subtype is built.
6298 Set_Directly_Designated_Type
6299 (Derived_Type, Designated_Type (Parent_Type));
6301 Subt := Process_Subtype (S, N);
6303 if Nkind (S) /= N_Subtype_Indication
6304 and then Subt /= Base_Type (Subt)
6306 Set_Ekind (Derived_Type, E_Access_Subtype);
6309 if Ekind (Derived_Type) = E_Access_Subtype then
6311 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6312 Ibase : constant Entity_Id :=
6313 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6314 Svg_Chars : constant Name_Id := Chars (Ibase);
6315 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6318 Copy_Node (Pbase, Ibase);
6320 -- Restore Itype status after Copy_Node
6322 Set_Is_Itype (Ibase);
6323 Set_Associated_Node_For_Itype (Ibase, N);
6325 Set_Chars (Ibase, Svg_Chars);
6326 Set_Next_Entity (Ibase, Svg_Next_E);
6327 Set_Sloc (Ibase, Sloc (Derived_Type));
6328 Set_Scope (Ibase, Scope (Derived_Type));
6329 Set_Freeze_Node (Ibase, Empty);
6330 Set_Is_Frozen (Ibase, False);
6331 Set_Comes_From_Source (Ibase, False);
6332 Set_Is_First_Subtype (Ibase, False);
6334 Set_Etype (Ibase, Pbase);
6335 Set_Etype (Derived_Type, Ibase);
6339 Set_Directly_Designated_Type
6340 (Derived_Type, Designated_Type (Subt));
6342 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6343 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6344 Set_Size_Info (Derived_Type, Parent_Type);
6345 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6346 Set_Depends_On_Private (Derived_Type,
6347 Has_Private_Component (Derived_Type));
6348 Conditional_Delay (Derived_Type, Subt);
6350 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6351 -- that it is not redundant.
6353 if Null_Exclusion_Present (Type_Definition (N)) then
6354 Set_Can_Never_Be_Null (Derived_Type);
6356 elsif Can_Never_Be_Null (Parent_Type) then
6357 Set_Can_Never_Be_Null (Derived_Type);
6360 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6361 -- the root type for this information.
6363 -- Apply range checks to discriminants for derived record case
6364 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6366 Desig_Type := Designated_Type (Derived_Type);
6368 if Is_Composite_Type (Desig_Type)
6369 and then (not Is_Array_Type (Desig_Type))
6370 and then Has_Discriminants (Desig_Type)
6371 and then Base_Type (Desig_Type) /= Desig_Type
6373 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6374 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6376 Discr := First_Discriminant (Base_Type (Desig_Type));
6377 while Present (Discr_Con_El) loop
6378 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6379 Next_Elmt (Discr_Con_El);
6380 Next_Discriminant (Discr);
6383 end Build_Derived_Access_Type;
6385 ------------------------------
6386 -- Build_Derived_Array_Type --
6387 ------------------------------
6389 procedure Build_Derived_Array_Type
6391 Parent_Type : Entity_Id;
6392 Derived_Type : Entity_Id)
6394 Loc : constant Source_Ptr := Sloc (N);
6395 Tdef : constant Node_Id := Type_Definition (N);
6396 Indic : constant Node_Id := Subtype_Indication (Tdef);
6397 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6398 Implicit_Base : Entity_Id;
6399 New_Indic : Node_Id;
6401 procedure Make_Implicit_Base;
6402 -- If the parent subtype is constrained, the derived type is a subtype
6403 -- of an implicit base type derived from the parent base.
6405 ------------------------
6406 -- Make_Implicit_Base --
6407 ------------------------
6409 procedure Make_Implicit_Base is
6412 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6414 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6415 Set_Etype (Implicit_Base, Parent_Base);
6417 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6418 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6420 Set_Has_Delayed_Freeze (Implicit_Base, True);
6421 end Make_Implicit_Base;
6423 -- Start of processing for Build_Derived_Array_Type
6426 if not Is_Constrained (Parent_Type) then
6427 if Nkind (Indic) /= N_Subtype_Indication then
6428 Set_Ekind (Derived_Type, E_Array_Type);
6430 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6431 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6433 Set_Has_Delayed_Freeze (Derived_Type, True);
6437 Set_Etype (Derived_Type, Implicit_Base);
6440 Make_Subtype_Declaration (Loc,
6441 Defining_Identifier => Derived_Type,
6442 Subtype_Indication =>
6443 Make_Subtype_Indication (Loc,
6444 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6445 Constraint => Constraint (Indic)));
6447 Rewrite (N, New_Indic);
6452 if Nkind (Indic) /= N_Subtype_Indication then
6455 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6456 Set_Etype (Derived_Type, Implicit_Base);
6457 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6460 Error_Msg_N ("illegal constraint on constrained type", Indic);
6464 -- If parent type is not a derived type itself, and is declared in
6465 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6466 -- the new type's concatenation operator since Derive_Subprograms
6467 -- will not inherit the parent's operator. If the parent type is
6468 -- unconstrained, the operator is of the unconstrained base type.
6470 if Number_Dimensions (Parent_Type) = 1
6471 and then not Is_Limited_Type (Parent_Type)
6472 and then not Is_Derived_Type (Parent_Type)
6473 and then not Is_Package_Or_Generic_Package
6474 (Scope (Base_Type (Parent_Type)))
6476 if not Is_Constrained (Parent_Type)
6477 and then Is_Constrained (Derived_Type)
6479 New_Concatenation_Op (Implicit_Base);
6481 New_Concatenation_Op (Derived_Type);
6484 end Build_Derived_Array_Type;
6486 -----------------------------------
6487 -- Build_Derived_Concurrent_Type --
6488 -----------------------------------
6490 procedure Build_Derived_Concurrent_Type
6492 Parent_Type : Entity_Id;
6493 Derived_Type : Entity_Id)
6495 Loc : constant Source_Ptr := Sloc (N);
6497 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6498 Corr_Decl : Node_Id;
6499 Corr_Decl_Needed : Boolean;
6500 -- If the derived type has fewer discriminants than its parent, the
6501 -- corresponding record is also a derived type, in order to account for
6502 -- the bound discriminants. We create a full type declaration for it in
6505 Constraint_Present : constant Boolean :=
6506 Nkind (Subtype_Indication (Type_Definition (N))) =
6507 N_Subtype_Indication;
6509 D_Constraint : Node_Id;
6510 New_Constraint : Elist_Id;
6511 Old_Disc : Entity_Id;
6512 New_Disc : Entity_Id;
6516 Set_Stored_Constraint (Derived_Type, No_Elist);
6517 Corr_Decl_Needed := False;
6520 if Present (Discriminant_Specifications (N))
6521 and then Constraint_Present
6523 Old_Disc := First_Discriminant (Parent_Type);
6524 New_Disc := First (Discriminant_Specifications (N));
6525 while Present (New_Disc) and then Present (Old_Disc) loop
6526 Next_Discriminant (Old_Disc);
6531 if Present (Old_Disc) and then Expander_Active then
6533 -- The new type has fewer discriminants, so we need to create a new
6534 -- corresponding record, which is derived from the corresponding
6535 -- record of the parent, and has a stored constraint that captures
6536 -- the values of the discriminant constraints. The corresponding
6537 -- record is needed only if expander is active and code generation is
6540 -- The type declaration for the derived corresponding record has the
6541 -- same discriminant part and constraints as the current declaration.
6542 -- Copy the unanalyzed tree to build declaration.
6544 Corr_Decl_Needed := True;
6545 New_N := Copy_Separate_Tree (N);
6548 Make_Full_Type_Declaration (Loc,
6549 Defining_Identifier => Corr_Record,
6550 Discriminant_Specifications =>
6551 Discriminant_Specifications (New_N),
6553 Make_Derived_Type_Definition (Loc,
6554 Subtype_Indication =>
6555 Make_Subtype_Indication (Loc,
6558 (Corresponding_Record_Type (Parent_Type), Loc),
6561 (Subtype_Indication (Type_Definition (New_N))))));
6564 -- Copy Storage_Size and Relative_Deadline variables if task case
6566 if Is_Task_Type (Parent_Type) then
6567 Set_Storage_Size_Variable (Derived_Type,
6568 Storage_Size_Variable (Parent_Type));
6569 Set_Relative_Deadline_Variable (Derived_Type,
6570 Relative_Deadline_Variable (Parent_Type));
6573 if Present (Discriminant_Specifications (N)) then
6574 Push_Scope (Derived_Type);
6575 Check_Or_Process_Discriminants (N, Derived_Type);
6577 if Constraint_Present then
6579 Expand_To_Stored_Constraint
6581 Build_Discriminant_Constraints
6583 Subtype_Indication (Type_Definition (N)), True));
6588 elsif Constraint_Present then
6590 -- Build constrained subtype, copying the constraint, and derive
6591 -- from it to create a derived constrained type.
6594 Loc : constant Source_Ptr := Sloc (N);
6595 Anon : constant Entity_Id :=
6596 Make_Defining_Identifier (Loc,
6597 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6602 Make_Subtype_Declaration (Loc,
6603 Defining_Identifier => Anon,
6604 Subtype_Indication =>
6605 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6606 Insert_Before (N, Decl);
6609 Rewrite (Subtype_Indication (Type_Definition (N)),
6610 New_Occurrence_Of (Anon, Loc));
6611 Set_Analyzed (Derived_Type, False);
6617 -- By default, operations and private data are inherited from parent.
6618 -- However, in the presence of bound discriminants, a new corresponding
6619 -- record will be created, see below.
6621 Set_Has_Discriminants
6622 (Derived_Type, Has_Discriminants (Parent_Type));
6623 Set_Corresponding_Record_Type
6624 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6626 -- Is_Constrained is set according the parent subtype, but is set to
6627 -- False if the derived type is declared with new discriminants.
6631 (Is_Constrained (Parent_Type) or else Constraint_Present)
6632 and then not Present (Discriminant_Specifications (N)));
6634 if Constraint_Present then
6635 if not Has_Discriminants (Parent_Type) then
6636 Error_Msg_N ("untagged parent must have discriminants", N);
6638 elsif Present (Discriminant_Specifications (N)) then
6640 -- Verify that new discriminants are used to constrain old ones
6645 (Constraint (Subtype_Indication (Type_Definition (N)))));
6647 Old_Disc := First_Discriminant (Parent_Type);
6649 while Present (D_Constraint) loop
6650 if Nkind (D_Constraint) /= N_Discriminant_Association then
6652 -- Positional constraint. If it is a reference to a new
6653 -- discriminant, it constrains the corresponding old one.
6655 if Nkind (D_Constraint) = N_Identifier then
6656 New_Disc := First_Discriminant (Derived_Type);
6657 while Present (New_Disc) loop
6658 exit when Chars (New_Disc) = Chars (D_Constraint);
6659 Next_Discriminant (New_Disc);
6662 if Present (New_Disc) then
6663 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6667 Next_Discriminant (Old_Disc);
6669 -- if this is a named constraint, search by name for the old
6670 -- discriminants constrained by the new one.
6672 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6674 -- Find new discriminant with that name
6676 New_Disc := First_Discriminant (Derived_Type);
6677 while Present (New_Disc) loop
6679 Chars (New_Disc) = Chars (Expression (D_Constraint));
6680 Next_Discriminant (New_Disc);
6683 if Present (New_Disc) then
6685 -- Verify that new discriminant renames some discriminant
6686 -- of the parent type, and associate the new discriminant
6687 -- with one or more old ones that it renames.
6693 Selector := First (Selector_Names (D_Constraint));
6694 while Present (Selector) loop
6695 Old_Disc := First_Discriminant (Parent_Type);
6696 while Present (Old_Disc) loop
6697 exit when Chars (Old_Disc) = Chars (Selector);
6698 Next_Discriminant (Old_Disc);
6701 if Present (Old_Disc) then
6702 Set_Corresponding_Discriminant
6703 (New_Disc, Old_Disc);
6712 Next (D_Constraint);
6715 New_Disc := First_Discriminant (Derived_Type);
6716 while Present (New_Disc) loop
6717 if No (Corresponding_Discriminant (New_Disc)) then
6719 ("new discriminant& must constrain old one", N, New_Disc);
6722 Subtypes_Statically_Compatible
6724 Etype (Corresponding_Discriminant (New_Disc)))
6727 ("& not statically compatible with parent discriminant",
6731 Next_Discriminant (New_Disc);
6735 elsif Present (Discriminant_Specifications (N)) then
6737 ("missing discriminant constraint in untagged derivation", N);
6740 -- The entity chain of the derived type includes the new discriminants
6741 -- but shares operations with the parent.
6743 if Present (Discriminant_Specifications (N)) then
6744 Old_Disc := First_Discriminant (Parent_Type);
6745 while Present (Old_Disc) loop
6746 if No (Next_Entity (Old_Disc))
6747 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6750 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6754 Next_Discriminant (Old_Disc);
6758 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6759 if Has_Discriminants (Parent_Type) then
6760 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6761 Set_Discriminant_Constraint (
6762 Derived_Type, Discriminant_Constraint (Parent_Type));
6766 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6768 Set_Has_Completion (Derived_Type);
6770 if Corr_Decl_Needed then
6771 Set_Stored_Constraint (Derived_Type, New_Constraint);
6772 Insert_After (N, Corr_Decl);
6773 Analyze (Corr_Decl);
6774 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6776 end Build_Derived_Concurrent_Type;
6778 ------------------------------------
6779 -- Build_Derived_Enumeration_Type --
6780 ------------------------------------
6782 procedure Build_Derived_Enumeration_Type
6784 Parent_Type : Entity_Id;
6785 Derived_Type : Entity_Id)
6787 Loc : constant Source_Ptr := Sloc (N);
6788 Def : constant Node_Id := Type_Definition (N);
6789 Indic : constant Node_Id := Subtype_Indication (Def);
6790 Implicit_Base : Entity_Id;
6791 Literal : Entity_Id;
6792 New_Lit : Entity_Id;
6793 Literals_List : List_Id;
6794 Type_Decl : Node_Id;
6796 Rang_Expr : Node_Id;
6799 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6800 -- not have explicit literals lists we need to process types derived
6801 -- from them specially. This is handled by Derived_Standard_Character.
6802 -- If the parent type is a generic type, there are no literals either,
6803 -- and we construct the same skeletal representation as for the generic
6806 if Is_Standard_Character_Type (Parent_Type) then
6807 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6809 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6815 if Nkind (Indic) /= N_Subtype_Indication then
6817 Make_Attribute_Reference (Loc,
6818 Attribute_Name => Name_First,
6819 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6820 Set_Etype (Lo, Derived_Type);
6823 Make_Attribute_Reference (Loc,
6824 Attribute_Name => Name_Last,
6825 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6826 Set_Etype (Hi, Derived_Type);
6828 Set_Scalar_Range (Derived_Type,
6834 -- Analyze subtype indication and verify compatibility
6835 -- with parent type.
6837 if Base_Type (Process_Subtype (Indic, N)) /=
6838 Base_Type (Parent_Type)
6841 ("illegal constraint for formal discrete type", N);
6847 -- If a constraint is present, analyze the bounds to catch
6848 -- premature usage of the derived literals.
6850 if Nkind (Indic) = N_Subtype_Indication
6851 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6853 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6854 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6857 -- Introduce an implicit base type for the derived type even if there
6858 -- is no constraint attached to it, since this seems closer to the
6859 -- Ada semantics. Build a full type declaration tree for the derived
6860 -- type using the implicit base type as the defining identifier. The
6861 -- build a subtype declaration tree which applies the constraint (if
6862 -- any) have it replace the derived type declaration.
6864 Literal := First_Literal (Parent_Type);
6865 Literals_List := New_List;
6866 while Present (Literal)
6867 and then Ekind (Literal) = E_Enumeration_Literal
6869 -- Literals of the derived type have the same representation as
6870 -- those of the parent type, but this representation can be
6871 -- overridden by an explicit representation clause. Indicate
6872 -- that there is no explicit representation given yet. These
6873 -- derived literals are implicit operations of the new type,
6874 -- and can be overridden by explicit ones.
6876 if Nkind (Literal) = N_Defining_Character_Literal then
6878 Make_Defining_Character_Literal (Loc, Chars (Literal));
6880 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6883 Set_Ekind (New_Lit, E_Enumeration_Literal);
6884 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6885 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6886 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6887 Set_Alias (New_Lit, Literal);
6888 Set_Is_Known_Valid (New_Lit, True);
6890 Append (New_Lit, Literals_List);
6891 Next_Literal (Literal);
6895 Make_Defining_Identifier (Sloc (Derived_Type),
6896 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6898 -- Indicate the proper nature of the derived type. This must be done
6899 -- before analysis of the literals, to recognize cases when a literal
6900 -- may be hidden by a previous explicit function definition (cf.
6903 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6904 Set_Etype (Derived_Type, Implicit_Base);
6907 Make_Full_Type_Declaration (Loc,
6908 Defining_Identifier => Implicit_Base,
6909 Discriminant_Specifications => No_List,
6911 Make_Enumeration_Type_Definition (Loc, Literals_List));
6913 Mark_Rewrite_Insertion (Type_Decl);
6914 Insert_Before (N, Type_Decl);
6915 Analyze (Type_Decl);
6917 -- The anonymous base now has a full declaration, but this base
6918 -- is not a first subtype.
6920 Set_Is_First_Subtype (Implicit_Base, False);
6922 -- After the implicit base is analyzed its Etype needs to be changed
6923 -- to reflect the fact that it is derived from the parent type which
6924 -- was ignored during analysis. We also set the size at this point.
6926 Set_Etype (Implicit_Base, Parent_Type);
6928 Set_Size_Info (Implicit_Base, Parent_Type);
6929 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6930 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6932 -- Copy other flags from parent type
6934 Set_Has_Non_Standard_Rep
6935 (Implicit_Base, Has_Non_Standard_Rep
6937 Set_Has_Pragma_Ordered
6938 (Implicit_Base, Has_Pragma_Ordered
6940 Set_Has_Delayed_Freeze (Implicit_Base);
6942 -- Process the subtype indication including a validation check on the
6943 -- constraint, if any. If a constraint is given, its bounds must be
6944 -- implicitly converted to the new type.
6946 if Nkind (Indic) = N_Subtype_Indication then
6948 R : constant Node_Id :=
6949 Range_Expression (Constraint (Indic));
6952 if Nkind (R) = N_Range then
6953 Hi := Build_Scalar_Bound
6954 (High_Bound (R), Parent_Type, Implicit_Base);
6955 Lo := Build_Scalar_Bound
6956 (Low_Bound (R), Parent_Type, Implicit_Base);
6959 -- Constraint is a Range attribute. Replace with explicit
6960 -- mention of the bounds of the prefix, which must be a
6963 Analyze (Prefix (R));
6965 Convert_To (Implicit_Base,
6966 Make_Attribute_Reference (Loc,
6967 Attribute_Name => Name_Last,
6969 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6972 Convert_To (Implicit_Base,
6973 Make_Attribute_Reference (Loc,
6974 Attribute_Name => Name_First,
6976 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6983 (Type_High_Bound (Parent_Type),
6984 Parent_Type, Implicit_Base);
6987 (Type_Low_Bound (Parent_Type),
6988 Parent_Type, Implicit_Base);
6996 -- If we constructed a default range for the case where no range
6997 -- was given, then the expressions in the range must not freeze
6998 -- since they do not correspond to expressions in the source.
6999 -- However, if the type inherits predicates the expressions will
7000 -- be elaborated earlier and must freeze.
7002 if Nkind (Indic) /= N_Subtype_Indication
7003 and then not Has_Predicates (Derived_Type)
7005 Set_Must_Not_Freeze (Lo);
7006 Set_Must_Not_Freeze (Hi);
7007 Set_Must_Not_Freeze (Rang_Expr);
7011 Make_Subtype_Declaration (Loc,
7012 Defining_Identifier => Derived_Type,
7013 Subtype_Indication =>
7014 Make_Subtype_Indication (Loc,
7015 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7017 Make_Range_Constraint (Loc,
7018 Range_Expression => Rang_Expr))));
7022 -- Propagate the aspects from the original type declaration to the
7023 -- declaration of the implicit base.
7025 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7027 -- Apply a range check. Since this range expression doesn't have an
7028 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7031 if Nkind (Indic) = N_Subtype_Indication then
7033 (Range_Expression (Constraint (Indic)), Parent_Type,
7034 Source_Typ => Entity (Subtype_Mark (Indic)));
7037 end Build_Derived_Enumeration_Type;
7039 --------------------------------
7040 -- Build_Derived_Numeric_Type --
7041 --------------------------------
7043 procedure Build_Derived_Numeric_Type
7045 Parent_Type : Entity_Id;
7046 Derived_Type : Entity_Id)
7048 Loc : constant Source_Ptr := Sloc (N);
7049 Tdef : constant Node_Id := Type_Definition (N);
7050 Indic : constant Node_Id := Subtype_Indication (Tdef);
7051 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7052 No_Constraint : constant Boolean := Nkind (Indic) /=
7053 N_Subtype_Indication;
7054 Implicit_Base : Entity_Id;
7060 -- Process the subtype indication including a validation check on
7061 -- the constraint if any.
7063 Discard_Node (Process_Subtype (Indic, N));
7065 -- Introduce an implicit base type for the derived type even if there
7066 -- is no constraint attached to it, since this seems closer to the Ada
7070 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7072 Set_Etype (Implicit_Base, Parent_Base);
7073 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7074 Set_Size_Info (Implicit_Base, Parent_Base);
7075 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7076 Set_Parent (Implicit_Base, Parent (Derived_Type));
7077 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7079 -- Set RM Size for discrete type or decimal fixed-point type
7080 -- Ordinary fixed-point is excluded, why???
7082 if Is_Discrete_Type (Parent_Base)
7083 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7085 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7088 Set_Has_Delayed_Freeze (Implicit_Base);
7090 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7091 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7093 Set_Scalar_Range (Implicit_Base,
7098 if Has_Infinities (Parent_Base) then
7099 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7102 -- The Derived_Type, which is the entity of the declaration, is a
7103 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7104 -- absence of an explicit constraint.
7106 Set_Etype (Derived_Type, Implicit_Base);
7108 -- If we did not have a constraint, then the Ekind is set from the
7109 -- parent type (otherwise Process_Subtype has set the bounds)
7111 if No_Constraint then
7112 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7115 -- If we did not have a range constraint, then set the range from the
7116 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7118 if No_Constraint or else not Has_Range_Constraint (Indic) then
7119 Set_Scalar_Range (Derived_Type,
7121 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7122 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7123 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7125 if Has_Infinities (Parent_Type) then
7126 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7129 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7132 Set_Is_Descendant_Of_Address (Derived_Type,
7133 Is_Descendant_Of_Address (Parent_Type));
7134 Set_Is_Descendant_Of_Address (Implicit_Base,
7135 Is_Descendant_Of_Address (Parent_Type));
7137 -- Set remaining type-specific fields, depending on numeric type
7139 if Is_Modular_Integer_Type (Parent_Type) then
7140 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7142 Set_Non_Binary_Modulus
7143 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7146 (Implicit_Base, Is_Known_Valid (Parent_Base));
7148 elsif Is_Floating_Point_Type (Parent_Type) then
7150 -- Digits of base type is always copied from the digits value of
7151 -- the parent base type, but the digits of the derived type will
7152 -- already have been set if there was a constraint present.
7154 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7155 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7157 if No_Constraint then
7158 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7161 elsif Is_Fixed_Point_Type (Parent_Type) then
7163 -- Small of base type and derived type are always copied from the
7164 -- parent base type, since smalls never change. The delta of the
7165 -- base type is also copied from the parent base type. However the
7166 -- delta of the derived type will have been set already if a
7167 -- constraint was present.
7169 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7170 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7171 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7173 if No_Constraint then
7174 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7177 -- The scale and machine radix in the decimal case are always
7178 -- copied from the parent base type.
7180 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7181 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7182 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7184 Set_Machine_Radix_10
7185 (Derived_Type, Machine_Radix_10 (Parent_Base));
7186 Set_Machine_Radix_10
7187 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7189 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7191 if No_Constraint then
7192 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7195 -- the analysis of the subtype_indication sets the
7196 -- digits value of the derived type.
7203 if Is_Integer_Type (Parent_Type) then
7204 Set_Has_Shift_Operator
7205 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7208 -- The type of the bounds is that of the parent type, and they
7209 -- must be converted to the derived type.
7211 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7213 -- The implicit_base should be frozen when the derived type is frozen,
7214 -- but note that it is used in the conversions of the bounds. For fixed
7215 -- types we delay the determination of the bounds until the proper
7216 -- freezing point. For other numeric types this is rejected by GCC, for
7217 -- reasons that are currently unclear (???), so we choose to freeze the
7218 -- implicit base now. In the case of integers and floating point types
7219 -- this is harmless because subsequent representation clauses cannot
7220 -- affect anything, but it is still baffling that we cannot use the
7221 -- same mechanism for all derived numeric types.
7223 -- There is a further complication: actually some representation
7224 -- clauses can affect the implicit base type. For example, attribute
7225 -- definition clauses for stream-oriented attributes need to set the
7226 -- corresponding TSS entries on the base type, and this normally
7227 -- cannot be done after the base type is frozen, so the circuitry in
7228 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7229 -- and not use Set_TSS in this case.
7231 -- There are also consequences for the case of delayed representation
7232 -- aspects for some cases. For example, a Size aspect is delayed and
7233 -- should not be evaluated to the freeze point. This early freezing
7234 -- means that the size attribute evaluation happens too early???
7236 if Is_Fixed_Point_Type (Parent_Type) then
7237 Conditional_Delay (Implicit_Base, Parent_Type);
7239 Freeze_Before (N, Implicit_Base);
7241 end Build_Derived_Numeric_Type;
7243 --------------------------------
7244 -- Build_Derived_Private_Type --
7245 --------------------------------
7247 procedure Build_Derived_Private_Type
7249 Parent_Type : Entity_Id;
7250 Derived_Type : Entity_Id;
7251 Is_Completion : Boolean;
7252 Derive_Subps : Boolean := True)
7254 Loc : constant Source_Ptr := Sloc (N);
7255 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7256 Par_Scope : constant Entity_Id := Scope (Par_Base);
7257 Full_N : constant Node_Id := New_Copy_Tree (N);
7258 Full_Der : Entity_Id := New_Copy (Derived_Type);
7261 procedure Build_Full_Derivation;
7262 -- Build full derivation, i.e. derive from the full view
7264 procedure Copy_And_Build;
7265 -- Copy derived type declaration, replace parent with its full view,
7266 -- and build derivation
7268 ---------------------------
7269 -- Build_Full_Derivation --
7270 ---------------------------
7272 procedure Build_Full_Derivation is
7274 -- If parent scope is not open, install the declarations
7276 if not In_Open_Scopes (Par_Scope) then
7277 Install_Private_Declarations (Par_Scope);
7278 Install_Visible_Declarations (Par_Scope);
7280 Uninstall_Declarations (Par_Scope);
7282 -- If parent scope is open and in another unit, and parent has a
7283 -- completion, then the derivation is taking place in the visible
7284 -- part of a child unit. In that case retrieve the full view of
7285 -- the parent momentarily.
7287 elsif not In_Same_Source_Unit (N, Parent_Type) then
7288 Full_P := Full_View (Parent_Type);
7289 Exchange_Declarations (Parent_Type);
7291 Exchange_Declarations (Full_P);
7293 -- Otherwise it is a local derivation
7298 end Build_Full_Derivation;
7300 --------------------
7301 -- Copy_And_Build --
7302 --------------------
7304 procedure Copy_And_Build is
7305 Full_Parent : Entity_Id := Parent_Type;
7308 -- If the parent is itself derived from another private type,
7309 -- installing the private declarations has not affected its
7310 -- privacy status, so use its own full view explicitly.
7312 if Is_Private_Type (Full_Parent)
7313 and then Present (Full_View (Full_Parent))
7315 Full_Parent := Full_View (Full_Parent);
7318 -- And its underlying full view if necessary
7320 if Is_Private_Type (Full_Parent)
7321 and then Present (Underlying_Full_View (Full_Parent))
7323 Full_Parent := Underlying_Full_View (Full_Parent);
7326 -- For record, access and most enumeration types, derivation from
7327 -- the full view requires a fully-fledged declaration. In the other
7328 -- cases, just use an itype.
7330 if Ekind (Full_Parent) in Record_Kind
7331 or else Ekind (Full_Parent) in Access_Kind
7333 (Ekind (Full_Parent) in Enumeration_Kind
7334 and then not Is_Standard_Character_Type (Full_Parent)
7335 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7337 -- Copy and adjust declaration to provide a completion for what
7338 -- is originally a private declaration. Indicate that full view
7339 -- is internally generated.
7341 Set_Comes_From_Source (Full_N, False);
7342 Set_Comes_From_Source (Full_Der, False);
7343 Set_Parent (Full_Der, Full_N);
7344 Set_Defining_Identifier (Full_N, Full_Der);
7346 -- If there are no constraints, adjust the subtype mark
7348 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7349 N_Subtype_Indication
7351 Set_Subtype_Indication
7352 (Type_Definition (Full_N),
7353 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7356 Insert_After (N, Full_N);
7358 -- Build full view of derived type from full view of parent which
7359 -- is now installed. Subprograms have been derived on the partial
7360 -- view, the completion does not derive them anew.
7362 if Ekind (Full_Parent) in Record_Kind then
7364 -- If parent type is tagged, the completion inherits the proper
7365 -- primitive operations.
7367 if Is_Tagged_Type (Parent_Type) then
7368 Build_Derived_Record_Type
7369 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7371 Build_Derived_Record_Type
7372 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7377 (Full_N, Full_Parent, Full_Der,
7378 Is_Completion => False, Derive_Subps => False);
7381 -- The full declaration has been introduced into the tree and
7382 -- processed in the step above. It should not be analyzed again
7383 -- (when encountered later in the current list of declarations)
7384 -- to prevent spurious name conflicts. The full entity remains
7387 Set_Analyzed (Full_N);
7391 Make_Defining_Identifier (Sloc (Derived_Type),
7392 Chars => Chars (Derived_Type));
7393 Set_Is_Itype (Full_Der);
7394 Set_Associated_Node_For_Itype (Full_Der, N);
7395 Set_Parent (Full_Der, N);
7397 (N, Full_Parent, Full_Der,
7398 Is_Completion => False, Derive_Subps => False);
7401 Set_Has_Private_Declaration (Full_Der);
7402 Set_Has_Private_Declaration (Derived_Type);
7404 Set_Scope (Full_Der, Scope (Derived_Type));
7405 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7406 Set_Has_Size_Clause (Full_Der, False);
7407 Set_Has_Alignment_Clause (Full_Der, False);
7408 Set_Has_Delayed_Freeze (Full_Der);
7409 Set_Is_Frozen (Full_Der, False);
7410 Set_Freeze_Node (Full_Der, Empty);
7411 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7412 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7414 -- The convention on the base type may be set in the private part
7415 -- and not propagated to the subtype until later, so we obtain the
7416 -- convention from the base type of the parent.
7418 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7421 -- Start of processing for Build_Derived_Private_Type
7424 if Is_Tagged_Type (Parent_Type) then
7425 Full_P := Full_View (Parent_Type);
7427 -- A type extension of a type with unknown discriminants is an
7428 -- indefinite type that the back-end cannot handle directly.
7429 -- We treat it as a private type, and build a completion that is
7430 -- derived from the full view of the parent, and hopefully has
7431 -- known discriminants.
7433 -- If the full view of the parent type has an underlying record view,
7434 -- use it to generate the underlying record view of this derived type
7435 -- (required for chains of derivations with unknown discriminants).
7437 -- Minor optimization: we avoid the generation of useless underlying
7438 -- record view entities if the private type declaration has unknown
7439 -- discriminants but its corresponding full view has no
7442 if Has_Unknown_Discriminants (Parent_Type)
7443 and then Present (Full_P)
7444 and then (Has_Discriminants (Full_P)
7445 or else Present (Underlying_Record_View (Full_P)))
7446 and then not In_Open_Scopes (Par_Scope)
7447 and then Expander_Active
7450 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7451 New_Ext : constant Node_Id :=
7453 (Record_Extension_Part (Type_Definition (N)));
7457 Build_Derived_Record_Type
7458 (N, Parent_Type, Derived_Type, Derive_Subps);
7460 -- Build anonymous completion, as a derivation from the full
7461 -- view of the parent. This is not a completion in the usual
7462 -- sense, because the current type is not private.
7465 Make_Full_Type_Declaration (Loc,
7466 Defining_Identifier => Full_Der,
7468 Make_Derived_Type_Definition (Loc,
7469 Subtype_Indication =>
7471 (Subtype_Indication (Type_Definition (N))),
7472 Record_Extension_Part => New_Ext));
7474 -- If the parent type has an underlying record view, use it
7475 -- here to build the new underlying record view.
7477 if Present (Underlying_Record_View (Full_P)) then
7479 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7481 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7482 Underlying_Record_View (Full_P));
7485 Install_Private_Declarations (Par_Scope);
7486 Install_Visible_Declarations (Par_Scope);
7487 Insert_Before (N, Decl);
7489 -- Mark entity as an underlying record view before analysis,
7490 -- to avoid generating the list of its primitive operations
7491 -- (which is not really required for this entity) and thus
7492 -- prevent spurious errors associated with missing overriding
7493 -- of abstract primitives (overridden only for Derived_Type).
7495 Set_Ekind (Full_Der, E_Record_Type);
7496 Set_Is_Underlying_Record_View (Full_Der);
7497 Set_Default_SSO (Full_Der);
7501 pragma Assert (Has_Discriminants (Full_Der)
7502 and then not Has_Unknown_Discriminants (Full_Der));
7504 Uninstall_Declarations (Par_Scope);
7506 -- Freeze the underlying record view, to prevent generation of
7507 -- useless dispatching information, which is simply shared with
7508 -- the real derived type.
7510 Set_Is_Frozen (Full_Der);
7512 -- If the derived type has access discriminants, create
7513 -- references to their anonymous types now, to prevent
7514 -- back-end problems when their first use is in generated
7515 -- bodies of primitives.
7521 E := First_Entity (Full_Der);
7523 while Present (E) loop
7524 if Ekind (E) = E_Discriminant
7525 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7527 Build_Itype_Reference (Etype (E), Decl);
7534 -- Set up links between real entity and underlying record view
7536 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7537 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7540 -- If discriminants are known, build derived record
7543 Build_Derived_Record_Type
7544 (N, Parent_Type, Derived_Type, Derive_Subps);
7549 elsif Has_Discriminants (Parent_Type) then
7551 -- Build partial view of derived type from partial view of parent.
7552 -- This must be done before building the full derivation because the
7553 -- second derivation will modify the discriminants of the first and
7554 -- the discriminants are chained with the rest of the components in
7555 -- the full derivation.
7557 Build_Derived_Record_Type
7558 (N, Parent_Type, Derived_Type, Derive_Subps);
7560 -- Build the full derivation if this is not the anonymous derived
7561 -- base type created by Build_Derived_Record_Type in the constrained
7562 -- case (see point 5. of its head comment) since we build it for the
7563 -- derived subtype. And skip it for protected types altogether, as
7564 -- gigi does not use these types directly.
7566 if Present (Full_View (Parent_Type))
7567 and then not Is_Itype (Derived_Type)
7568 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7571 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7573 Last_Discr : Entity_Id;
7576 -- If this is not a completion, construct the implicit full
7577 -- view by deriving from the full view of the parent type.
7578 -- But if this is a completion, the derived private type
7579 -- being built is a full view and the full derivation can
7580 -- only be its underlying full view.
7582 Build_Full_Derivation;
7584 if not Is_Completion then
7585 Set_Full_View (Derived_Type, Full_Der);
7587 Set_Underlying_Full_View (Derived_Type, Full_Der);
7588 Set_Is_Underlying_Full_View (Full_Der);
7591 if not Is_Base_Type (Derived_Type) then
7592 Set_Full_View (Der_Base, Base_Type (Full_Der));
7595 -- Copy the discriminant list from full view to the partial
7596 -- view (base type and its subtype). Gigi requires that the
7597 -- partial and full views have the same discriminants.
7599 -- Note that since the partial view points to discriminants
7600 -- in the full view, their scope will be that of the full
7601 -- view. This might cause some front end problems and need
7604 Discr := First_Discriminant (Base_Type (Full_Der));
7605 Set_First_Entity (Der_Base, Discr);
7608 Last_Discr := Discr;
7609 Next_Discriminant (Discr);
7610 exit when No (Discr);
7613 Set_Last_Entity (Der_Base, Last_Discr);
7614 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7615 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7617 Set_Stored_Constraint
7618 (Full_Der, Stored_Constraint (Derived_Type));
7622 elsif Present (Full_View (Parent_Type))
7623 and then Has_Discriminants (Full_View (Parent_Type))
7625 if Has_Unknown_Discriminants (Parent_Type)
7626 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7627 N_Subtype_Indication
7630 ("cannot constrain type with unknown discriminants",
7631 Subtype_Indication (Type_Definition (N)));
7635 -- If this is not a completion, construct the implicit full view by
7636 -- deriving from the full view of the parent type. But if this is a
7637 -- completion, the derived private type being built is a full view
7638 -- and the full derivation can only be its underlying full view.
7640 Build_Full_Derivation;
7642 if not Is_Completion then
7643 Set_Full_View (Derived_Type, Full_Der);
7645 Set_Underlying_Full_View (Derived_Type, Full_Der);
7646 Set_Is_Underlying_Full_View (Full_Der);
7649 -- In any case, the primitive operations are inherited from the
7650 -- parent type, not from the internal full view.
7652 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7654 if Derive_Subps then
7655 Derive_Subprograms (Parent_Type, Derived_Type);
7658 Set_Stored_Constraint (Derived_Type, No_Elist);
7660 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7663 -- Untagged type, No discriminants on either view
7665 if Nkind (Subtype_Indication (Type_Definition (N))) =
7666 N_Subtype_Indication
7669 ("illegal constraint on type without discriminants", N);
7672 if Present (Discriminant_Specifications (N))
7673 and then Present (Full_View (Parent_Type))
7674 and then not Is_Tagged_Type (Full_View (Parent_Type))
7676 Error_Msg_N ("cannot add discriminants to untagged type", N);
7679 Set_Stored_Constraint (Derived_Type, No_Elist);
7680 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7681 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7682 Set_Disable_Controlled (Derived_Type, Disable_Controlled
7684 Set_Has_Controlled_Component
7685 (Derived_Type, Has_Controlled_Component
7688 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7690 if not Is_Controlled_Active (Parent_Type) then
7691 Set_Finalize_Storage_Only
7692 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7695 -- If this is not a completion, construct the implicit full view by
7696 -- deriving from the full view of the parent type.
7698 -- ??? If the parent is untagged private and its completion is
7699 -- tagged, this mechanism will not work because we cannot derive from
7700 -- the tagged full view unless we have an extension.
7702 if Present (Full_View (Parent_Type))
7703 and then not Is_Tagged_Type (Full_View (Parent_Type))
7704 and then not Is_Completion
7706 Build_Full_Derivation;
7707 Set_Full_View (Derived_Type, Full_Der);
7711 Set_Has_Unknown_Discriminants (Derived_Type,
7712 Has_Unknown_Discriminants (Parent_Type));
7714 if Is_Private_Type (Derived_Type) then
7715 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7718 -- If the parent base type is in scope, add the derived type to its
7719 -- list of private dependents, because its full view may become
7720 -- visible subsequently (in a nested private part, a body, or in a
7721 -- further child unit).
7723 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7724 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7726 -- Check for unusual case where a type completed by a private
7727 -- derivation occurs within a package nested in a child unit, and
7728 -- the parent is declared in an ancestor.
7730 if Is_Child_Unit (Scope (Current_Scope))
7731 and then Is_Completion
7732 and then In_Private_Part (Current_Scope)
7733 and then Scope (Parent_Type) /= Current_Scope
7735 -- Note that if the parent has a completion in the private part,
7736 -- (which is itself a derivation from some other private type)
7737 -- it is that completion that is visible, there is no full view
7738 -- available, and no special processing is needed.
7740 and then Present (Full_View (Parent_Type))
7742 -- In this case, the full view of the parent type will become
7743 -- visible in the body of the enclosing child, and only then will
7744 -- the current type be possibly non-private. Build an underlying
7745 -- full view that will be installed when the enclosing child body
7748 if Present (Underlying_Full_View (Derived_Type)) then
7749 Full_Der := Underlying_Full_View (Derived_Type);
7751 Build_Full_Derivation;
7752 Set_Underlying_Full_View (Derived_Type, Full_Der);
7753 Set_Is_Underlying_Full_View (Full_Der);
7756 -- The full view will be used to swap entities on entry/exit to
7757 -- the body, and must appear in the entity list for the package.
7759 Append_Entity (Full_Der, Scope (Derived_Type));
7762 end Build_Derived_Private_Type;
7764 -------------------------------
7765 -- Build_Derived_Record_Type --
7766 -------------------------------
7770 -- Ideally we would like to use the same model of type derivation for
7771 -- tagged and untagged record types. Unfortunately this is not quite
7772 -- possible because the semantics of representation clauses is different
7773 -- for tagged and untagged records under inheritance. Consider the
7776 -- type R (...) is [tagged] record ... end record;
7777 -- type T (...) is new R (...) [with ...];
7779 -- The representation clauses for T can specify a completely different
7780 -- record layout from R's. Hence the same component can be placed in two
7781 -- very different positions in objects of type T and R. If R and T are
7782 -- tagged types, representation clauses for T can only specify the layout
7783 -- of non inherited components, thus components that are common in R and T
7784 -- have the same position in objects of type R and T.
7786 -- This has two implications. The first is that the entire tree for R's
7787 -- declaration needs to be copied for T in the untagged case, so that T
7788 -- can be viewed as a record type of its own with its own representation
7789 -- clauses. The second implication is the way we handle discriminants.
7790 -- Specifically, in the untagged case we need a way to communicate to Gigi
7791 -- what are the real discriminants in the record, while for the semantics
7792 -- we need to consider those introduced by the user to rename the
7793 -- discriminants in the parent type. This is handled by introducing the
7794 -- notion of stored discriminants. See below for more.
7796 -- Fortunately the way regular components are inherited can be handled in
7797 -- the same way in tagged and untagged types.
7799 -- To complicate things a bit more the private view of a private extension
7800 -- cannot be handled in the same way as the full view (for one thing the
7801 -- semantic rules are somewhat different). We will explain what differs
7804 -- 2. DISCRIMINANTS UNDER INHERITANCE
7806 -- The semantic rules governing the discriminants of derived types are
7809 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7810 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7812 -- If parent type has discriminants, then the discriminants that are
7813 -- declared in the derived type are [3.4 (11)]:
7815 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7818 -- o Otherwise, each discriminant of the parent type (implicitly declared
7819 -- in the same order with the same specifications). In this case, the
7820 -- discriminants are said to be "inherited", or if unknown in the parent
7821 -- are also unknown in the derived type.
7823 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7825 -- o The parent subtype must be constrained;
7827 -- o If the parent type is not a tagged type, then each discriminant of
7828 -- the derived type must be used in the constraint defining a parent
7829 -- subtype. [Implementation note: This ensures that the new discriminant
7830 -- can share storage with an existing discriminant.]
7832 -- For the derived type each discriminant of the parent type is either
7833 -- inherited, constrained to equal some new discriminant of the derived
7834 -- type, or constrained to the value of an expression.
7836 -- When inherited or constrained to equal some new discriminant, the
7837 -- parent discriminant and the discriminant of the derived type are said
7840 -- If a discriminant of the parent type is constrained to a specific value
7841 -- in the derived type definition, then the discriminant is said to be
7842 -- "specified" by that derived type definition.
7844 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7846 -- We have spoken about stored discriminants in point 1 (introduction)
7847 -- above. There are two sort of stored discriminants: implicit and
7848 -- explicit. As long as the derived type inherits the same discriminants as
7849 -- the root record type, stored discriminants are the same as regular
7850 -- discriminants, and are said to be implicit. However, if any discriminant
7851 -- in the root type was renamed in the derived type, then the derived
7852 -- type will contain explicit stored discriminants. Explicit stored
7853 -- discriminants are discriminants in addition to the semantically visible
7854 -- discriminants defined for the derived type. Stored discriminants are
7855 -- used by Gigi to figure out what are the physical discriminants in
7856 -- objects of the derived type (see precise definition in einfo.ads).
7857 -- As an example, consider the following:
7859 -- type R (D1, D2, D3 : Int) is record ... end record;
7860 -- type T1 is new R;
7861 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7862 -- type T3 is new T2;
7863 -- type T4 (Y : Int) is new T3 (Y, 99);
7865 -- The following table summarizes the discriminants and stored
7866 -- discriminants in R and T1 through T4.
7868 -- Type Discrim Stored Discrim Comment
7869 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7870 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7871 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7872 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7873 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7875 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7876 -- find the corresponding discriminant in the parent type, while
7877 -- Original_Record_Component (abbreviated ORC below), the actual physical
7878 -- component that is renamed. Finally the field Is_Completely_Hidden
7879 -- (abbreviated ICH below) is set for all explicit stored discriminants
7880 -- (see einfo.ads for more info). For the above example this gives:
7882 -- Discrim CD ORC ICH
7883 -- ^^^^^^^ ^^ ^^^ ^^^
7884 -- D1 in R empty itself no
7885 -- D2 in R empty itself no
7886 -- D3 in R empty itself no
7888 -- D1 in T1 D1 in R itself no
7889 -- D2 in T1 D2 in R itself no
7890 -- D3 in T1 D3 in R itself no
7892 -- X1 in T2 D3 in T1 D3 in T2 no
7893 -- X2 in T2 D1 in T1 D1 in T2 no
7894 -- D1 in T2 empty itself yes
7895 -- D2 in T2 empty itself yes
7896 -- D3 in T2 empty itself yes
7898 -- X1 in T3 X1 in T2 D3 in T3 no
7899 -- X2 in T3 X2 in T2 D1 in T3 no
7900 -- D1 in T3 empty itself yes
7901 -- D2 in T3 empty itself yes
7902 -- D3 in T3 empty itself yes
7904 -- Y in T4 X1 in T3 D3 in T3 no
7905 -- D1 in T3 empty itself yes
7906 -- D2 in T3 empty itself yes
7907 -- D3 in T3 empty itself yes
7909 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7911 -- Type derivation for tagged types is fairly straightforward. If no
7912 -- discriminants are specified by the derived type, these are inherited
7913 -- from the parent. No explicit stored discriminants are ever necessary.
7914 -- The only manipulation that is done to the tree is that of adding a
7915 -- _parent field with parent type and constrained to the same constraint
7916 -- specified for the parent in the derived type definition. For instance:
7918 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7919 -- type T1 is new R with null record;
7920 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7922 -- are changed into:
7924 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7925 -- _parent : R (D1, D2, D3);
7928 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7929 -- _parent : T1 (X2, 88, X1);
7932 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7933 -- ORC and ICH fields are:
7935 -- Discrim CD ORC ICH
7936 -- ^^^^^^^ ^^ ^^^ ^^^
7937 -- D1 in R empty itself no
7938 -- D2 in R empty itself no
7939 -- D3 in R empty itself no
7941 -- D1 in T1 D1 in R D1 in R no
7942 -- D2 in T1 D2 in R D2 in R no
7943 -- D3 in T1 D3 in R D3 in R no
7945 -- X1 in T2 D3 in T1 D3 in R no
7946 -- X2 in T2 D1 in T1 D1 in R no
7948 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7950 -- Regardless of whether we dealing with a tagged or untagged type
7951 -- we will transform all derived type declarations of the form
7953 -- type T is new R (...) [with ...];
7955 -- subtype S is R (...);
7956 -- type T is new S [with ...];
7958 -- type BT is new R [with ...];
7959 -- subtype T is BT (...);
7961 -- That is, the base derived type is constrained only if it has no
7962 -- discriminants. The reason for doing this is that GNAT's semantic model
7963 -- assumes that a base type with discriminants is unconstrained.
7965 -- Note that, strictly speaking, the above transformation is not always
7966 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7968 -- procedure B34011A is
7969 -- type REC (D : integer := 0) is record
7974 -- type T6 is new Rec;
7975 -- function F return T6;
7980 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7983 -- The definition of Q6.U is illegal. However transforming Q6.U into
7985 -- type BaseU is new T6;
7986 -- subtype U is BaseU (Q6.F.I)
7988 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7989 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7990 -- the transformation described above.
7992 -- There is another instance where the above transformation is incorrect.
7996 -- type Base (D : Integer) is tagged null record;
7997 -- procedure P (X : Base);
7999 -- type Der is new Base (2) with null record;
8000 -- procedure P (X : Der);
8003 -- Then the above transformation turns this into
8005 -- type Der_Base is new Base with null record;
8006 -- -- procedure P (X : Base) is implicitly inherited here
8007 -- -- as procedure P (X : Der_Base).
8009 -- subtype Der is Der_Base (2);
8010 -- procedure P (X : Der);
8011 -- -- The overriding of P (X : Der_Base) is illegal since we
8012 -- -- have a parameter conformance problem.
8014 -- To get around this problem, after having semantically processed Der_Base
8015 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8016 -- Discriminant_Constraint from Der so that when parameter conformance is
8017 -- checked when P is overridden, no semantic errors are flagged.
8019 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8021 -- Regardless of whether we are dealing with a tagged or untagged type
8022 -- we will transform all derived type declarations of the form
8024 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8025 -- type T is new R [with ...];
8027 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8029 -- The reason for such transformation is that it allows us to implement a
8030 -- very clean form of component inheritance as explained below.
8032 -- Note that this transformation is not achieved by direct tree rewriting
8033 -- and manipulation, but rather by redoing the semantic actions that the
8034 -- above transformation will entail. This is done directly in routine
8035 -- Inherit_Components.
8037 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8039 -- In both tagged and untagged derived types, regular non discriminant
8040 -- components are inherited in the derived type from the parent type. In
8041 -- the absence of discriminants component, inheritance is straightforward
8042 -- as components can simply be copied from the parent.
8044 -- If the parent has discriminants, inheriting components constrained with
8045 -- these discriminants requires caution. Consider the following example:
8047 -- type R (D1, D2 : Positive) is [tagged] record
8048 -- S : String (D1 .. D2);
8051 -- type T1 is new R [with null record];
8052 -- type T2 (X : positive) is new R (1, X) [with null record];
8054 -- As explained in 6. above, T1 is rewritten as
8055 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8056 -- which makes the treatment for T1 and T2 identical.
8058 -- What we want when inheriting S, is that references to D1 and D2 in R are
8059 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8060 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8061 -- with either discriminant references in the derived type or expressions.
8062 -- This replacement is achieved as follows: before inheriting R's
8063 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8064 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8065 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8066 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8067 -- by String (1 .. X).
8069 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8071 -- We explain here the rules governing private type extensions relevant to
8072 -- type derivation. These rules are explained on the following example:
8074 -- type D [(...)] is new A [(...)] with private; <-- partial view
8075 -- type D [(...)] is new P [(...)] with null record; <-- full view
8077 -- Type A is called the ancestor subtype of the private extension.
8078 -- Type P is the parent type of the full view of the private extension. It
8079 -- must be A or a type derived from A.
8081 -- The rules concerning the discriminants of private type extensions are
8084 -- o If a private extension inherits known discriminants from the ancestor
8085 -- subtype, then the full view must also inherit its discriminants from
8086 -- the ancestor subtype and the parent subtype of the full view must be
8087 -- constrained if and only if the ancestor subtype is constrained.
8089 -- o If a partial view has unknown discriminants, then the full view may
8090 -- define a definite or an indefinite subtype, with or without
8093 -- o If a partial view has neither known nor unknown discriminants, then
8094 -- the full view must define a definite subtype.
8096 -- o If the ancestor subtype of a private extension has constrained
8097 -- discriminants, then the parent subtype of the full view must impose a
8098 -- statically matching constraint on those discriminants.
8100 -- This means that only the following forms of private extensions are
8103 -- type D is new A with private; <-- partial view
8104 -- type D is new P with null record; <-- full view
8106 -- If A has no discriminants than P has no discriminants, otherwise P must
8107 -- inherit A's discriminants.
8109 -- type D is new A (...) with private; <-- partial view
8110 -- type D is new P (:::) with null record; <-- full view
8112 -- P must inherit A's discriminants and (...) and (:::) must statically
8115 -- subtype A is R (...);
8116 -- type D is new A with private; <-- partial view
8117 -- type D is new P with null record; <-- full view
8119 -- P must have inherited R's discriminants and must be derived from A or
8120 -- any of its subtypes.
8122 -- type D (..) is new A with private; <-- partial view
8123 -- type D (..) is new P [(:::)] with null record; <-- full view
8125 -- No specific constraints on P's discriminants or constraint (:::).
8126 -- Note that A can be unconstrained, but the parent subtype P must either
8127 -- be constrained or (:::) must be present.
8129 -- type D (..) is new A [(...)] with private; <-- partial view
8130 -- type D (..) is new P [(:::)] with null record; <-- full view
8132 -- P's constraints on A's discriminants must statically match those
8133 -- imposed by (...).
8135 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8137 -- The full view of a private extension is handled exactly as described
8138 -- above. The model chose for the private view of a private extension is
8139 -- the same for what concerns discriminants (i.e. they receive the same
8140 -- treatment as in the tagged case). However, the private view of the
8141 -- private extension always inherits the components of the parent base,
8142 -- without replacing any discriminant reference. Strictly speaking this is
8143 -- incorrect. However, Gigi never uses this view to generate code so this
8144 -- is a purely semantic issue. In theory, a set of transformations similar
8145 -- to those given in 5. and 6. above could be applied to private views of
8146 -- private extensions to have the same model of component inheritance as
8147 -- for non private extensions. However, this is not done because it would
8148 -- further complicate private type processing. Semantically speaking, this
8149 -- leaves us in an uncomfortable situation. As an example consider:
8152 -- type R (D : integer) is tagged record
8153 -- S : String (1 .. D);
8155 -- procedure P (X : R);
8156 -- type T is new R (1) with private;
8158 -- type T is new R (1) with null record;
8161 -- This is transformed into:
8164 -- type R (D : integer) is tagged record
8165 -- S : String (1 .. D);
8167 -- procedure P (X : R);
8168 -- type T is new R (1) with private;
8170 -- type BaseT is new R with null record;
8171 -- subtype T is BaseT (1);
8174 -- (strictly speaking the above is incorrect Ada)
8176 -- From the semantic standpoint the private view of private extension T
8177 -- should be flagged as constrained since one can clearly have
8181 -- in a unit withing Pack. However, when deriving subprograms for the
8182 -- private view of private extension T, T must be seen as unconstrained
8183 -- since T has discriminants (this is a constraint of the current
8184 -- subprogram derivation model). Thus, when processing the private view of
8185 -- a private extension such as T, we first mark T as unconstrained, we
8186 -- process it, we perform program derivation and just before returning from
8187 -- Build_Derived_Record_Type we mark T as constrained.
8189 -- ??? Are there are other uncomfortable cases that we will have to
8192 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8194 -- Types that are derived from a visible record type and have a private
8195 -- extension present other peculiarities. They behave mostly like private
8196 -- types, but if they have primitive operations defined, these will not
8197 -- have the proper signatures for further inheritance, because other
8198 -- primitive operations will use the implicit base that we define for
8199 -- private derivations below. This affect subprogram inheritance (see
8200 -- Derive_Subprograms for details). We also derive the implicit base from
8201 -- the base type of the full view, so that the implicit base is a record
8202 -- type and not another private type, This avoids infinite loops.
8204 procedure Build_Derived_Record_Type
8206 Parent_Type : Entity_Id;
8207 Derived_Type : Entity_Id;
8208 Derive_Subps : Boolean := True)
8210 Discriminant_Specs : constant Boolean :=
8211 Present (Discriminant_Specifications (N));
8212 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8213 Loc : constant Source_Ptr := Sloc (N);
8214 Private_Extension : constant Boolean :=
8215 Nkind (N) = N_Private_Extension_Declaration;
8216 Assoc_List : Elist_Id;
8217 Constraint_Present : Boolean;
8219 Discrim : Entity_Id;
8221 Inherit_Discrims : Boolean := False;
8222 Last_Discrim : Entity_Id;
8223 New_Base : Entity_Id;
8225 New_Discrs : Elist_Id;
8226 New_Indic : Node_Id;
8227 Parent_Base : Entity_Id;
8228 Save_Etype : Entity_Id;
8229 Save_Discr_Constr : Elist_Id;
8230 Save_Next_Entity : Entity_Id;
8233 Discs : Elist_Id := New_Elmt_List;
8234 -- An empty Discs list means that there were no constraints in the
8235 -- subtype indication or that there was an error processing it.
8238 if Ekind (Parent_Type) = E_Record_Type_With_Private
8239 and then Present (Full_View (Parent_Type))
8240 and then Has_Discriminants (Parent_Type)
8242 Parent_Base := Base_Type (Full_View (Parent_Type));
8244 Parent_Base := Base_Type (Parent_Type);
8247 -- AI05-0115 : if this is a derivation from a private type in some
8248 -- other scope that may lead to invisible components for the derived
8249 -- type, mark it accordingly.
8251 if Is_Private_Type (Parent_Type) then
8252 if Scope (Parent_Type) = Scope (Derived_Type) then
8255 elsif In_Open_Scopes (Scope (Parent_Type))
8256 and then In_Private_Part (Scope (Parent_Type))
8261 Set_Has_Private_Ancestor (Derived_Type);
8265 Set_Has_Private_Ancestor
8266 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8269 -- Before we start the previously documented transformations, here is
8270 -- little fix for size and alignment of tagged types. Normally when we
8271 -- derive type D from type P, we copy the size and alignment of P as the
8272 -- default for D, and in the absence of explicit representation clauses
8273 -- for D, the size and alignment are indeed the same as the parent.
8275 -- But this is wrong for tagged types, since fields may be added, and
8276 -- the default size may need to be larger, and the default alignment may
8277 -- need to be larger.
8279 -- We therefore reset the size and alignment fields in the tagged case.
8280 -- Note that the size and alignment will in any case be at least as
8281 -- large as the parent type (since the derived type has a copy of the
8282 -- parent type in the _parent field)
8284 -- The type is also marked as being tagged here, which is needed when
8285 -- processing components with a self-referential anonymous access type
8286 -- in the call to Check_Anonymous_Access_Components below. Note that
8287 -- this flag is also set later on for completeness.
8290 Set_Is_Tagged_Type (Derived_Type);
8291 Init_Size_Align (Derived_Type);
8294 -- STEP 0a: figure out what kind of derived type declaration we have
8296 if Private_Extension then
8298 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8299 Set_Default_SSO (Derived_Type);
8302 Type_Def := Type_Definition (N);
8304 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8305 -- Parent_Base can be a private type or private extension. However,
8306 -- for tagged types with an extension the newly added fields are
8307 -- visible and hence the Derived_Type is always an E_Record_Type.
8308 -- (except that the parent may have its own private fields).
8309 -- For untagged types we preserve the Ekind of the Parent_Base.
8311 if Present (Record_Extension_Part (Type_Def)) then
8312 Set_Ekind (Derived_Type, E_Record_Type);
8313 Set_Default_SSO (Derived_Type);
8315 -- Create internal access types for components with anonymous
8318 if Ada_Version >= Ada_2005 then
8319 Check_Anonymous_Access_Components
8320 (N, Derived_Type, Derived_Type,
8321 Component_List (Record_Extension_Part (Type_Def)));
8325 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8329 -- Indic can either be an N_Identifier if the subtype indication
8330 -- contains no constraint or an N_Subtype_Indication if the subtype
8331 -- indication has a constraint.
8333 Indic := Subtype_Indication (Type_Def);
8334 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8336 -- Check that the type has visible discriminants. The type may be
8337 -- a private type with unknown discriminants whose full view has
8338 -- discriminants which are invisible.
8340 if Constraint_Present then
8341 if not Has_Discriminants (Parent_Base)
8343 (Has_Unknown_Discriminants (Parent_Base)
8344 and then Is_Private_Type (Parent_Base))
8347 ("invalid constraint: type has no discriminant",
8348 Constraint (Indic));
8350 Constraint_Present := False;
8351 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8353 elsif Is_Constrained (Parent_Type) then
8355 ("invalid constraint: parent type is already constrained",
8356 Constraint (Indic));
8358 Constraint_Present := False;
8359 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8363 -- STEP 0b: If needed, apply transformation given in point 5. above
8365 if not Private_Extension
8366 and then Has_Discriminants (Parent_Type)
8367 and then not Discriminant_Specs
8368 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8370 -- First, we must analyze the constraint (see comment in point 5.)
8371 -- The constraint may come from the subtype indication of the full
8374 if Constraint_Present then
8375 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8377 -- If there is no explicit constraint, there might be one that is
8378 -- inherited from a constrained parent type. In that case verify that
8379 -- it conforms to the constraint in the partial view. In perverse
8380 -- cases the parent subtypes of the partial and full view can have
8381 -- different constraints.
8383 elsif Present (Stored_Constraint (Parent_Type)) then
8384 New_Discrs := Stored_Constraint (Parent_Type);
8387 New_Discrs := No_Elist;
8390 if Has_Discriminants (Derived_Type)
8391 and then Has_Private_Declaration (Derived_Type)
8392 and then Present (Discriminant_Constraint (Derived_Type))
8393 and then Present (New_Discrs)
8395 -- Verify that constraints of the full view statically match
8396 -- those given in the partial view.
8402 C1 := First_Elmt (New_Discrs);
8403 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8404 while Present (C1) and then Present (C2) loop
8405 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8407 (Is_OK_Static_Expression (Node (C1))
8408 and then Is_OK_Static_Expression (Node (C2))
8410 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8415 if Constraint_Present then
8417 ("constraint not conformant to previous declaration",
8421 ("constraint of full view is incompatible "
8422 & "with partial view", N);
8432 -- Insert and analyze the declaration for the unconstrained base type
8434 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8437 Make_Full_Type_Declaration (Loc,
8438 Defining_Identifier => New_Base,
8440 Make_Derived_Type_Definition (Loc,
8441 Abstract_Present => Abstract_Present (Type_Def),
8442 Limited_Present => Limited_Present (Type_Def),
8443 Subtype_Indication =>
8444 New_Occurrence_Of (Parent_Base, Loc),
8445 Record_Extension_Part =>
8446 Relocate_Node (Record_Extension_Part (Type_Def)),
8447 Interface_List => Interface_List (Type_Def)));
8449 Set_Parent (New_Decl, Parent (N));
8450 Mark_Rewrite_Insertion (New_Decl);
8451 Insert_Before (N, New_Decl);
8453 -- In the extension case, make sure ancestor is frozen appropriately
8454 -- (see also non-discriminated case below).
8456 if Present (Record_Extension_Part (Type_Def))
8457 or else Is_Interface (Parent_Base)
8459 Freeze_Before (New_Decl, Parent_Type);
8462 -- Note that this call passes False for the Derive_Subps parameter
8463 -- because subprogram derivation is deferred until after creating
8464 -- the subtype (see below).
8467 (New_Decl, Parent_Base, New_Base,
8468 Is_Completion => False, Derive_Subps => False);
8470 -- ??? This needs re-examination to determine whether the
8471 -- above call can simply be replaced by a call to Analyze.
8473 Set_Analyzed (New_Decl);
8475 -- Insert and analyze the declaration for the constrained subtype
8477 if Constraint_Present then
8479 Make_Subtype_Indication (Loc,
8480 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8481 Constraint => Relocate_Node (Constraint (Indic)));
8485 Constr_List : constant List_Id := New_List;
8490 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8491 while Present (C) loop
8494 -- It is safe here to call New_Copy_Tree since we called
8495 -- Force_Evaluation on each constraint previously
8496 -- in Build_Discriminant_Constraints.
8498 Append (New_Copy_Tree (Expr), To => Constr_List);
8504 Make_Subtype_Indication (Loc,
8505 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8507 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8512 Make_Subtype_Declaration (Loc,
8513 Defining_Identifier => Derived_Type,
8514 Subtype_Indication => New_Indic));
8518 -- Derivation of subprograms must be delayed until the full subtype
8519 -- has been established, to ensure proper overriding of subprograms
8520 -- inherited by full types. If the derivations occurred as part of
8521 -- the call to Build_Derived_Type above, then the check for type
8522 -- conformance would fail because earlier primitive subprograms
8523 -- could still refer to the full type prior the change to the new
8524 -- subtype and hence would not match the new base type created here.
8525 -- Subprograms are not derived, however, when Derive_Subps is False
8526 -- (since otherwise there could be redundant derivations).
8528 if Derive_Subps then
8529 Derive_Subprograms (Parent_Type, Derived_Type);
8532 -- For tagged types the Discriminant_Constraint of the new base itype
8533 -- is inherited from the first subtype so that no subtype conformance
8534 -- problem arise when the first subtype overrides primitive
8535 -- operations inherited by the implicit base type.
8538 Set_Discriminant_Constraint
8539 (New_Base, Discriminant_Constraint (Derived_Type));
8545 -- If we get here Derived_Type will have no discriminants or it will be
8546 -- a discriminated unconstrained base type.
8548 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8552 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8553 -- The declaration of a specific descendant of an interface type
8554 -- freezes the interface type (RM 13.14).
8556 if not Private_Extension or else Is_Interface (Parent_Base) then
8557 Freeze_Before (N, Parent_Type);
8560 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8561 -- cannot be declared at a deeper level than its parent type is
8562 -- removed. The check on derivation within a generic body is also
8563 -- relaxed, but there's a restriction that a derived tagged type
8564 -- cannot be declared in a generic body if it's derived directly
8565 -- or indirectly from a formal type of that generic.
8567 if Ada_Version >= Ada_2005 then
8568 if Present (Enclosing_Generic_Body (Derived_Type)) then
8570 Ancestor_Type : Entity_Id;
8573 -- Check to see if any ancestor of the derived type is a
8576 Ancestor_Type := Parent_Type;
8577 while not Is_Generic_Type (Ancestor_Type)
8578 and then Etype (Ancestor_Type) /= Ancestor_Type
8580 Ancestor_Type := Etype (Ancestor_Type);
8583 -- If the derived type does have a formal type as an
8584 -- ancestor, then it's an error if the derived type is
8585 -- declared within the body of the generic unit that
8586 -- declares the formal type in its generic formal part. It's
8587 -- sufficient to check whether the ancestor type is declared
8588 -- inside the same generic body as the derived type (such as
8589 -- within a nested generic spec), in which case the
8590 -- derivation is legal. If the formal type is declared
8591 -- outside of that generic body, then it's guaranteed that
8592 -- the derived type is declared within the generic body of
8593 -- the generic unit declaring the formal type.
8595 if Is_Generic_Type (Ancestor_Type)
8596 and then Enclosing_Generic_Body (Ancestor_Type) /=
8597 Enclosing_Generic_Body (Derived_Type)
8600 ("parent type of& must not be descendant of formal type"
8601 & " of an enclosing generic body",
8602 Indic, Derived_Type);
8607 elsif Type_Access_Level (Derived_Type) /=
8608 Type_Access_Level (Parent_Type)
8609 and then not Is_Generic_Type (Derived_Type)
8611 if Is_Controlled (Parent_Type) then
8613 ("controlled type must be declared at the library level",
8617 ("type extension at deeper accessibility level than parent",
8623 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8626 and then GB /= Enclosing_Generic_Body (Parent_Base)
8629 ("parent type of& must not be outside generic body"
8631 Indic, Derived_Type);
8637 -- Ada 2005 (AI-251)
8639 if Ada_Version >= Ada_2005 and then Is_Tagged then
8641 -- "The declaration of a specific descendant of an interface type
8642 -- freezes the interface type" (RM 13.14).
8647 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8648 Iface := First (Interface_List (Type_Def));
8649 while Present (Iface) loop
8650 Freeze_Before (N, Etype (Iface));
8657 -- STEP 1b : preliminary cleanup of the full view of private types
8659 -- If the type is already marked as having discriminants, then it's the
8660 -- completion of a private type or private extension and we need to
8661 -- retain the discriminants from the partial view if the current
8662 -- declaration has Discriminant_Specifications so that we can verify
8663 -- conformance. However, we must remove any existing components that
8664 -- were inherited from the parent (and attached in Copy_And_Swap)
8665 -- because the full type inherits all appropriate components anyway, and
8666 -- we do not want the partial view's components interfering.
8668 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8669 Discrim := First_Discriminant (Derived_Type);
8671 Last_Discrim := Discrim;
8672 Next_Discriminant (Discrim);
8673 exit when No (Discrim);
8676 Set_Last_Entity (Derived_Type, Last_Discrim);
8678 -- In all other cases wipe out the list of inherited components (even
8679 -- inherited discriminants), it will be properly rebuilt here.
8682 Set_First_Entity (Derived_Type, Empty);
8683 Set_Last_Entity (Derived_Type, Empty);
8686 -- STEP 1c: Initialize some flags for the Derived_Type
8688 -- The following flags must be initialized here so that
8689 -- Process_Discriminants can check that discriminants of tagged types do
8690 -- not have a default initial value and that access discriminants are
8691 -- only specified for limited records. For completeness, these flags are
8692 -- also initialized along with all the other flags below.
8694 -- AI-419: Limitedness is not inherited from an interface parent, so to
8695 -- be limited in that case the type must be explicitly declared as
8696 -- limited. However, task and protected interfaces are always limited.
8698 if Limited_Present (Type_Def) then
8699 Set_Is_Limited_Record (Derived_Type);
8701 elsif Is_Limited_Record (Parent_Type)
8702 or else (Present (Full_View (Parent_Type))
8703 and then Is_Limited_Record (Full_View (Parent_Type)))
8705 if not Is_Interface (Parent_Type)
8706 or else Is_Synchronized_Interface (Parent_Type)
8707 or else Is_Protected_Interface (Parent_Type)
8708 or else Is_Task_Interface (Parent_Type)
8710 Set_Is_Limited_Record (Derived_Type);
8714 -- STEP 2a: process discriminants of derived type if any
8716 Push_Scope (Derived_Type);
8718 if Discriminant_Specs then
8719 Set_Has_Unknown_Discriminants (Derived_Type, False);
8721 -- The following call initializes fields Has_Discriminants and
8722 -- Discriminant_Constraint, unless we are processing the completion
8723 -- of a private type declaration.
8725 Check_Or_Process_Discriminants (N, Derived_Type);
8727 -- For untagged types, the constraint on the Parent_Type must be
8728 -- present and is used to rename the discriminants.
8730 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8731 Error_Msg_N ("untagged parent must have discriminants", Indic);
8733 elsif not Is_Tagged and then not Constraint_Present then
8735 ("discriminant constraint needed for derived untagged records",
8738 -- Otherwise the parent subtype must be constrained unless we have a
8739 -- private extension.
8741 elsif not Constraint_Present
8742 and then not Private_Extension
8743 and then not Is_Constrained (Parent_Type)
8746 ("unconstrained type not allowed in this context", Indic);
8748 elsif Constraint_Present then
8749 -- The following call sets the field Corresponding_Discriminant
8750 -- for the discriminants in the Derived_Type.
8752 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8754 -- For untagged types all new discriminants must rename
8755 -- discriminants in the parent. For private extensions new
8756 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8758 Discrim := First_Discriminant (Derived_Type);
8759 while Present (Discrim) loop
8761 and then No (Corresponding_Discriminant (Discrim))
8764 ("new discriminants must constrain old ones", Discrim);
8766 elsif Private_Extension
8767 and then Present (Corresponding_Discriminant (Discrim))
8770 ("only static constraints allowed for parent"
8771 & " discriminants in the partial view", Indic);
8775 -- If a new discriminant is used in the constraint, then its
8776 -- subtype must be statically compatible with the parent
8777 -- discriminant's subtype (3.7(15)).
8779 -- However, if the record contains an array constrained by
8780 -- the discriminant but with some different bound, the compiler
8781 -- tries to create a smaller range for the discriminant type.
8782 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8783 -- the discriminant type is a scalar type, the check must use
8784 -- the original discriminant type in the parent declaration.
8787 Corr_Disc : constant Entity_Id :=
8788 Corresponding_Discriminant (Discrim);
8789 Disc_Type : constant Entity_Id := Etype (Discrim);
8790 Corr_Type : Entity_Id;
8793 if Present (Corr_Disc) then
8794 if Is_Scalar_Type (Disc_Type) then
8796 Entity (Discriminant_Type (Parent (Corr_Disc)));
8798 Corr_Type := Etype (Corr_Disc);
8802 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8805 ("subtype must be compatible "
8806 & "with parent discriminant",
8812 Next_Discriminant (Discrim);
8815 -- Check whether the constraints of the full view statically
8816 -- match those imposed by the parent subtype [7.3(13)].
8818 if Present (Stored_Constraint (Derived_Type)) then
8823 C1 := First_Elmt (Discs);
8824 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8825 while Present (C1) and then Present (C2) loop
8827 Fully_Conformant_Expressions (Node (C1), Node (C2))
8830 ("not conformant with previous declaration",
8841 -- STEP 2b: No new discriminants, inherit discriminants if any
8844 if Private_Extension then
8845 Set_Has_Unknown_Discriminants
8847 Has_Unknown_Discriminants (Parent_Type)
8848 or else Unknown_Discriminants_Present (N));
8850 -- The partial view of the parent may have unknown discriminants,
8851 -- but if the full view has discriminants and the parent type is
8852 -- in scope they must be inherited.
8854 elsif Has_Unknown_Discriminants (Parent_Type)
8856 (not Has_Discriminants (Parent_Type)
8857 or else not In_Open_Scopes (Scope (Parent_Type)))
8859 Set_Has_Unknown_Discriminants (Derived_Type);
8862 if not Has_Unknown_Discriminants (Derived_Type)
8863 and then not Has_Unknown_Discriminants (Parent_Base)
8864 and then Has_Discriminants (Parent_Type)
8866 Inherit_Discrims := True;
8867 Set_Has_Discriminants
8868 (Derived_Type, True);
8869 Set_Discriminant_Constraint
8870 (Derived_Type, Discriminant_Constraint (Parent_Base));
8873 -- The following test is true for private types (remember
8874 -- transformation 5. is not applied to those) and in an error
8877 if Constraint_Present then
8878 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8881 -- For now mark a new derived type as constrained only if it has no
8882 -- discriminants. At the end of Build_Derived_Record_Type we properly
8883 -- set this flag in the case of private extensions. See comments in
8884 -- point 9. just before body of Build_Derived_Record_Type.
8888 not (Inherit_Discrims
8889 or else Has_Unknown_Discriminants (Derived_Type)));
8892 -- STEP 3: initialize fields of derived type
8894 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8895 Set_Stored_Constraint (Derived_Type, No_Elist);
8897 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8898 -- but cannot be interfaces
8900 if not Private_Extension
8901 and then Ekind (Derived_Type) /= E_Private_Type
8902 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8904 if Interface_Present (Type_Def) then
8905 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8908 Set_Interfaces (Derived_Type, No_Elist);
8911 -- Fields inherited from the Parent_Type
8913 Set_Has_Specified_Layout
8914 (Derived_Type, Has_Specified_Layout (Parent_Type));
8915 Set_Is_Limited_Composite
8916 (Derived_Type, Is_Limited_Composite (Parent_Type));
8917 Set_Is_Private_Composite
8918 (Derived_Type, Is_Private_Composite (Parent_Type));
8920 if Is_Tagged_Type (Parent_Type) then
8921 Set_No_Tagged_Streams_Pragma
8922 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8925 -- Fields inherited from the Parent_Base
8927 Set_Has_Controlled_Component
8928 (Derived_Type, Has_Controlled_Component (Parent_Base));
8929 Set_Has_Non_Standard_Rep
8930 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8931 Set_Has_Primitive_Operations
8932 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8934 -- Fields inherited from the Parent_Base in the non-private case
8936 if Ekind (Derived_Type) = E_Record_Type then
8937 Set_Has_Complex_Representation
8938 (Derived_Type, Has_Complex_Representation (Parent_Base));
8941 -- Fields inherited from the Parent_Base for record types
8943 if Is_Record_Type (Derived_Type) then
8945 Parent_Full : Entity_Id;
8948 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8949 -- Parent_Base can be a private type or private extension. Go
8950 -- to the full view here to get the E_Record_Type specific flags.
8952 if Present (Full_View (Parent_Base)) then
8953 Parent_Full := Full_View (Parent_Base);
8955 Parent_Full := Parent_Base;
8958 Set_OK_To_Reorder_Components
8959 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8963 -- Set fields for private derived types
8965 if Is_Private_Type (Derived_Type) then
8966 Set_Depends_On_Private (Derived_Type, True);
8967 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8969 -- Inherit fields from non private record types. If this is the
8970 -- completion of a derivation from a private type, the parent itself
8971 -- is private, and the attributes come from its full view, which must
8975 if Is_Private_Type (Parent_Base)
8976 and then not Is_Record_Type (Parent_Base)
8978 Set_Component_Alignment
8979 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8981 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8983 Set_Component_Alignment
8984 (Derived_Type, Component_Alignment (Parent_Base));
8986 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8990 -- Set fields for tagged types
8993 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8995 -- All tagged types defined in Ada.Finalization are controlled
8997 if Chars (Scope (Derived_Type)) = Name_Finalization
8998 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8999 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9001 Set_Is_Controlled (Derived_Type);
9003 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
9006 -- Minor optimization: there is no need to generate the class-wide
9007 -- entity associated with an underlying record view.
9009 if not Is_Underlying_Record_View (Derived_Type) then
9010 Make_Class_Wide_Type (Derived_Type);
9013 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9015 if Has_Discriminants (Derived_Type)
9016 and then Constraint_Present
9018 Set_Stored_Constraint
9019 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9022 if Ada_Version >= Ada_2005 then
9024 Ifaces_List : Elist_Id;
9027 -- Checks rules 3.9.4 (13/2 and 14/2)
9029 if Comes_From_Source (Derived_Type)
9030 and then not Is_Private_Type (Derived_Type)
9031 and then Is_Interface (Parent_Type)
9032 and then not Is_Interface (Derived_Type)
9034 if Is_Task_Interface (Parent_Type) then
9036 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9039 elsif Is_Protected_Interface (Parent_Type) then
9041 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9046 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9048 Check_Interfaces (N, Type_Def);
9050 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9051 -- not already in the parents.
9055 Ifaces_List => Ifaces_List,
9056 Exclude_Parents => True);
9058 Set_Interfaces (Derived_Type, Ifaces_List);
9060 -- If the derived type is the anonymous type created for
9061 -- a declaration whose parent has a constraint, propagate
9062 -- the interface list to the source type. This must be done
9063 -- prior to the completion of the analysis of the source type
9064 -- because the components in the extension may contain current
9065 -- instances whose legality depends on some ancestor.
9067 if Is_Itype (Derived_Type) then
9069 Def : constant Node_Id :=
9070 Associated_Node_For_Itype (Derived_Type);
9073 and then Nkind (Def) = N_Full_Type_Declaration
9076 (Defining_Identifier (Def), Ifaces_List);
9081 -- A type extension is automatically Ghost when one of its
9082 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9083 -- also inherited when the parent type is Ghost, but this is
9084 -- done in Build_Derived_Type as the mechanism also handles
9085 -- untagged derivations.
9087 if Implements_Ghost_Interface (Derived_Type) then
9088 Set_Is_Ghost_Entity (Derived_Type);
9094 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
9095 Set_Has_Non_Standard_Rep
9096 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9099 -- STEP 4: Inherit components from the parent base and constrain them.
9100 -- Apply the second transformation described in point 6. above.
9102 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9103 or else not Has_Discriminants (Parent_Type)
9104 or else not Is_Constrained (Parent_Type)
9108 Constrs := Discriminant_Constraint (Parent_Type);
9113 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9115 -- STEP 5a: Copy the parent record declaration for untagged types
9117 Set_Has_Implicit_Dereference
9118 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9120 if not Is_Tagged then
9122 -- Discriminant_Constraint (Derived_Type) has been properly
9123 -- constructed. Save it and temporarily set it to Empty because we
9124 -- do not want the call to New_Copy_Tree below to mess this list.
9126 if Has_Discriminants (Derived_Type) then
9127 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9128 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9130 Save_Discr_Constr := No_Elist;
9133 -- Save the Etype field of Derived_Type. It is correctly set now,
9134 -- but the call to New_Copy tree may remap it to point to itself,
9135 -- which is not what we want. Ditto for the Next_Entity field.
9137 Save_Etype := Etype (Derived_Type);
9138 Save_Next_Entity := Next_Entity (Derived_Type);
9140 -- Assoc_List maps all stored discriminants in the Parent_Base to
9141 -- stored discriminants in the Derived_Type. It is fundamental that
9142 -- no types or itypes with discriminants other than the stored
9143 -- discriminants appear in the entities declared inside
9144 -- Derived_Type, since the back end cannot deal with it.
9148 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9150 -- Restore the fields saved prior to the New_Copy_Tree call
9151 -- and compute the stored constraint.
9153 Set_Etype (Derived_Type, Save_Etype);
9154 Set_Next_Entity (Derived_Type, Save_Next_Entity);
9156 if Has_Discriminants (Derived_Type) then
9157 Set_Discriminant_Constraint
9158 (Derived_Type, Save_Discr_Constr);
9159 Set_Stored_Constraint
9160 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9161 Replace_Components (Derived_Type, New_Decl);
9164 -- Insert the new derived type declaration
9166 Rewrite (N, New_Decl);
9168 -- STEP 5b: Complete the processing for record extensions in generics
9170 -- There is no completion for record extensions declared in the
9171 -- parameter part of a generic, so we need to complete processing for
9172 -- these generic record extensions here. The Record_Type_Definition call
9173 -- will change the Ekind of the components from E_Void to E_Component.
9175 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9176 Record_Type_Definition (Empty, Derived_Type);
9178 -- STEP 5c: Process the record extension for non private tagged types
9180 elsif not Private_Extension then
9181 Expand_Record_Extension (Derived_Type, Type_Def);
9183 -- Note : previously in ASIS mode we set the Parent_Subtype of the
9184 -- derived type to propagate some semantic information. This led
9185 -- to other ASIS failures and has been removed.
9187 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9188 -- implemented interfaces if we are in expansion mode
9191 and then Has_Interfaces (Derived_Type)
9193 Add_Interface_Tag_Components (N, Derived_Type);
9196 -- Analyze the record extension
9198 Record_Type_Definition
9199 (Record_Extension_Part (Type_Def), Derived_Type);
9204 -- Nothing else to do if there is an error in the derivation.
9205 -- An unusual case: the full view may be derived from a type in an
9206 -- instance, when the partial view was used illegally as an actual
9207 -- in that instance, leading to a circular definition.
9209 if Etype (Derived_Type) = Any_Type
9210 or else Etype (Parent_Type) = Derived_Type
9215 -- Set delayed freeze and then derive subprograms, we need to do
9216 -- this in this order so that derived subprograms inherit the
9217 -- derived freeze if necessary.
9219 Set_Has_Delayed_Freeze (Derived_Type);
9221 if Derive_Subps then
9222 Derive_Subprograms (Parent_Type, Derived_Type);
9225 -- If we have a private extension which defines a constrained derived
9226 -- type mark as constrained here after we have derived subprograms. See
9227 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9229 if Private_Extension and then Inherit_Discrims then
9230 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9231 Set_Is_Constrained (Derived_Type, True);
9232 Set_Discriminant_Constraint (Derived_Type, Discs);
9234 elsif Is_Constrained (Parent_Type) then
9236 (Derived_Type, True);
9237 Set_Discriminant_Constraint
9238 (Derived_Type, Discriminant_Constraint (Parent_Type));
9242 -- Update the class-wide type, which shares the now-completed entity
9243 -- list with its specific type. In case of underlying record views,
9244 -- we do not generate the corresponding class wide entity.
9247 and then not Is_Underlying_Record_View (Derived_Type)
9250 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9252 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9255 Check_Function_Writable_Actuals (N);
9256 end Build_Derived_Record_Type;
9258 ------------------------
9259 -- Build_Derived_Type --
9260 ------------------------
9262 procedure Build_Derived_Type
9264 Parent_Type : Entity_Id;
9265 Derived_Type : Entity_Id;
9266 Is_Completion : Boolean;
9267 Derive_Subps : Boolean := True)
9269 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9272 -- Set common attributes
9274 Set_Scope (Derived_Type, Current_Scope);
9276 Set_Etype (Derived_Type, Parent_Base);
9277 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9278 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9280 Set_Size_Info (Derived_Type, Parent_Type);
9281 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9282 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
9283 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9285 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9286 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9288 if Is_Tagged_Type (Derived_Type) then
9289 Set_No_Tagged_Streams_Pragma
9290 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9293 -- If the parent has primitive routines, set the derived type link
9295 if Has_Primitive_Operations (Parent_Type) then
9296 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9299 -- If the parent type is a private subtype, the convention on the base
9300 -- type may be set in the private part, and not propagated to the
9301 -- subtype until later, so we obtain the convention from the base type.
9303 Set_Convention (Derived_Type, Convention (Parent_Base));
9305 -- Set SSO default for record or array type
9307 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9308 and then Is_Base_Type (Derived_Type)
9310 Set_Default_SSO (Derived_Type);
9313 -- A derived type inherits the Default_Initial_Condition pragma coming
9314 -- from any parent type within the derivation chain.
9316 if Has_DIC (Parent_Type) then
9317 Set_Has_Inherited_DIC (Derived_Type);
9320 -- A derived type inherits any class-wide invariants coming from a
9321 -- parent type or an interface. Note that the invariant procedure of
9322 -- the parent type should not be inherited because the derived type may
9323 -- define invariants of its own.
9325 if not Is_Interface (Derived_Type) then
9326 if Has_Inherited_Invariants (Parent_Type)
9327 or else Has_Inheritable_Invariants (Parent_Type)
9329 Set_Has_Inherited_Invariants (Derived_Type);
9331 elsif Is_Concurrent_Type (Derived_Type)
9332 or else Is_Tagged_Type (Derived_Type)
9337 Iface_Elmt : Elmt_Id;
9342 Ifaces_List => Ifaces,
9343 Exclude_Parents => True);
9345 if Present (Ifaces) then
9346 Iface_Elmt := First_Elmt (Ifaces);
9347 while Present (Iface_Elmt) loop
9348 Iface := Node (Iface_Elmt);
9350 if Has_Inheritable_Invariants (Iface) then
9351 Set_Has_Inherited_Invariants (Derived_Type);
9355 Next_Elmt (Iface_Elmt);
9362 -- We similarly inherit predicates. Note that for scalar derived types
9363 -- the predicate is inherited from the first subtype, and not from its
9364 -- (anonymous) base type.
9366 if Has_Predicates (Parent_Type)
9367 or else Has_Predicates (First_Subtype (Parent_Type))
9369 Set_Has_Predicates (Derived_Type);
9372 -- The derived type inherits the representation clauses of the parent
9374 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9376 -- If the parent type has delayed rep aspects, then mark the derived
9377 -- type as possibly inheriting a delayed rep aspect.
9379 if Has_Delayed_Rep_Aspects (Parent_Type) then
9380 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9383 -- A derived type becomes Ghost when its parent type is also Ghost
9384 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9385 -- directly inherited because the Ghost policy in effect may differ.
9387 if Is_Ghost_Entity (Parent_Type) then
9388 Set_Is_Ghost_Entity (Derived_Type);
9391 -- Type dependent processing
9393 case Ekind (Parent_Type) is
9394 when Numeric_Kind =>
9395 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9398 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9400 when Class_Wide_Kind
9404 Build_Derived_Record_Type
9405 (N, Parent_Type, Derived_Type, Derive_Subps);
9408 when Enumeration_Kind =>
9409 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9412 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9414 when Incomplete_Or_Private_Kind =>
9415 Build_Derived_Private_Type
9416 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9418 -- For discriminated types, the derivation includes deriving
9419 -- primitive operations. For others it is done below.
9421 if Is_Tagged_Type (Parent_Type)
9422 or else Has_Discriminants (Parent_Type)
9423 or else (Present (Full_View (Parent_Type))
9424 and then Has_Discriminants (Full_View (Parent_Type)))
9429 when Concurrent_Kind =>
9430 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9433 raise Program_Error;
9436 -- Nothing more to do if some error occurred
9438 if Etype (Derived_Type) = Any_Type then
9442 -- Set delayed freeze and then derive subprograms, we need to do this
9443 -- in this order so that derived subprograms inherit the derived freeze
9446 Set_Has_Delayed_Freeze (Derived_Type);
9448 if Derive_Subps then
9449 Derive_Subprograms (Parent_Type, Derived_Type);
9452 Set_Has_Primitive_Operations
9453 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9454 end Build_Derived_Type;
9456 -----------------------
9457 -- Build_Discriminal --
9458 -----------------------
9460 procedure Build_Discriminal (Discrim : Entity_Id) is
9461 D_Minal : Entity_Id;
9462 CR_Disc : Entity_Id;
9465 -- A discriminal has the same name as the discriminant
9467 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9469 Set_Ekind (D_Minal, E_In_Parameter);
9470 Set_Mechanism (D_Minal, Default_Mechanism);
9471 Set_Etype (D_Minal, Etype (Discrim));
9472 Set_Scope (D_Minal, Current_Scope);
9473 Set_Parent (D_Minal, Parent (Discrim));
9475 Set_Discriminal (Discrim, D_Minal);
9476 Set_Discriminal_Link (D_Minal, Discrim);
9478 -- For task types, build at once the discriminants of the corresponding
9479 -- record, which are needed if discriminants are used in entry defaults
9480 -- and in family bounds.
9482 if Is_Concurrent_Type (Current_Scope)
9484 Is_Limited_Type (Current_Scope)
9486 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9488 Set_Ekind (CR_Disc, E_In_Parameter);
9489 Set_Mechanism (CR_Disc, Default_Mechanism);
9490 Set_Etype (CR_Disc, Etype (Discrim));
9491 Set_Scope (CR_Disc, Current_Scope);
9492 Set_Discriminal_Link (CR_Disc, Discrim);
9493 Set_CR_Discriminant (Discrim, CR_Disc);
9495 end Build_Discriminal;
9497 ------------------------------------
9498 -- Build_Discriminant_Constraints --
9499 ------------------------------------
9501 function Build_Discriminant_Constraints
9504 Derived_Def : Boolean := False) return Elist_Id
9506 C : constant Node_Id := Constraint (Def);
9507 Nb_Discr : constant Nat := Number_Discriminants (T);
9509 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9510 -- Saves the expression corresponding to a given discriminant in T
9512 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9513 -- Return the Position number within array Discr_Expr of a discriminant
9514 -- D within the discriminant list of the discriminated type T.
9516 procedure Process_Discriminant_Expression
9519 -- If this is a discriminant constraint on a partial view, do not
9520 -- generate an overflow check on the discriminant expression. The check
9521 -- will be generated when constraining the full view. Otherwise the
9522 -- backend creates duplicate symbols for the temporaries corresponding
9523 -- to the expressions to be checked, causing spurious assembler errors.
9529 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9533 Disc := First_Discriminant (T);
9534 for J in Discr_Expr'Range loop
9539 Next_Discriminant (Disc);
9542 -- Note: Since this function is called on discriminants that are
9543 -- known to belong to the discriminated type, falling through the
9544 -- loop with no match signals an internal compiler error.
9546 raise Program_Error;
9549 -------------------------------------
9550 -- Process_Discriminant_Expression --
9551 -------------------------------------
9553 procedure Process_Discriminant_Expression
9557 BDT : constant Entity_Id := Base_Type (Etype (D));
9560 -- If this is a discriminant constraint on a partial view, do
9561 -- not generate an overflow on the discriminant expression. The
9562 -- check will be generated when constraining the full view.
9564 if Is_Private_Type (T)
9565 and then Present (Full_View (T))
9567 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9569 Analyze_And_Resolve (Expr, BDT);
9571 end Process_Discriminant_Expression;
9573 -- Declarations local to Build_Discriminant_Constraints
9577 Elist : constant Elist_Id := New_Elmt_List;
9585 Discrim_Present : Boolean := False;
9587 -- Start of processing for Build_Discriminant_Constraints
9590 -- The following loop will process positional associations only.
9591 -- For a positional association, the (single) discriminant is
9592 -- implicitly specified by position, in textual order (RM 3.7.2).
9594 Discr := First_Discriminant (T);
9595 Constr := First (Constraints (C));
9596 for D in Discr_Expr'Range loop
9597 exit when Nkind (Constr) = N_Discriminant_Association;
9600 Error_Msg_N ("too few discriminants given in constraint", C);
9601 return New_Elmt_List;
9603 elsif Nkind (Constr) = N_Range
9604 or else (Nkind (Constr) = N_Attribute_Reference
9605 and then Attribute_Name (Constr) = Name_Range)
9608 ("a range is not a valid discriminant constraint", Constr);
9609 Discr_Expr (D) := Error;
9612 Process_Discriminant_Expression (Constr, Discr);
9613 Discr_Expr (D) := Constr;
9616 Next_Discriminant (Discr);
9620 if No (Discr) and then Present (Constr) then
9621 Error_Msg_N ("too many discriminants given in constraint", Constr);
9622 return New_Elmt_List;
9625 -- Named associations can be given in any order, but if both positional
9626 -- and named associations are used in the same discriminant constraint,
9627 -- then positional associations must occur first, at their normal
9628 -- position. Hence once a named association is used, the rest of the
9629 -- discriminant constraint must use only named associations.
9631 while Present (Constr) loop
9633 -- Positional association forbidden after a named association
9635 if Nkind (Constr) /= N_Discriminant_Association then
9636 Error_Msg_N ("positional association follows named one", Constr);
9637 return New_Elmt_List;
9639 -- Otherwise it is a named association
9642 -- E records the type of the discriminants in the named
9643 -- association. All the discriminants specified in the same name
9644 -- association must have the same type.
9648 -- Search the list of discriminants in T to see if the simple name
9649 -- given in the constraint matches any of them.
9651 Id := First (Selector_Names (Constr));
9652 while Present (Id) loop
9655 -- If Original_Discriminant is present, we are processing a
9656 -- generic instantiation and this is an instance node. We need
9657 -- to find the name of the corresponding discriminant in the
9658 -- actual record type T and not the name of the discriminant in
9659 -- the generic formal. Example:
9662 -- type G (D : int) is private;
9664 -- subtype W is G (D => 1);
9666 -- type Rec (X : int) is record ... end record;
9667 -- package Q is new P (G => Rec);
9669 -- At the point of the instantiation, formal type G is Rec
9670 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9671 -- which really looks like "subtype W is Rec (D => 1);" at
9672 -- the point of instantiation, we want to find the discriminant
9673 -- that corresponds to D in Rec, i.e. X.
9675 if Present (Original_Discriminant (Id))
9676 and then In_Instance
9678 Discr := Find_Corresponding_Discriminant (Id, T);
9682 Discr := First_Discriminant (T);
9683 while Present (Discr) loop
9684 if Chars (Discr) = Chars (Id) then
9689 Next_Discriminant (Discr);
9693 Error_Msg_N ("& does not match any discriminant", Id);
9694 return New_Elmt_List;
9696 -- If the parent type is a generic formal, preserve the
9697 -- name of the discriminant for subsequent instances.
9698 -- see comment at the beginning of this if statement.
9700 elsif Is_Generic_Type (Root_Type (T)) then
9701 Set_Original_Discriminant (Id, Discr);
9705 Position := Pos_Of_Discr (T, Discr);
9707 if Present (Discr_Expr (Position)) then
9708 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9711 -- Each discriminant specified in the same named association
9712 -- must be associated with a separate copy of the
9713 -- corresponding expression.
9715 if Present (Next (Id)) then
9716 Expr := New_Copy_Tree (Expression (Constr));
9717 Set_Parent (Expr, Parent (Expression (Constr)));
9719 Expr := Expression (Constr);
9722 Discr_Expr (Position) := Expr;
9723 Process_Discriminant_Expression (Expr, Discr);
9726 -- A discriminant association with more than one discriminant
9727 -- name is only allowed if the named discriminants are all of
9728 -- the same type (RM 3.7.1(8)).
9731 E := Base_Type (Etype (Discr));
9733 elsif Base_Type (Etype (Discr)) /= E then
9735 ("all discriminants in an association " &
9736 "must have the same type", Id);
9746 -- A discriminant constraint must provide exactly one value for each
9747 -- discriminant of the type (RM 3.7.1(8)).
9749 for J in Discr_Expr'Range loop
9750 if No (Discr_Expr (J)) then
9751 Error_Msg_N ("too few discriminants given in constraint", C);
9752 return New_Elmt_List;
9756 -- Determine if there are discriminant expressions in the constraint
9758 for J in Discr_Expr'Range loop
9759 if Denotes_Discriminant
9760 (Discr_Expr (J), Check_Concurrent => True)
9762 Discrim_Present := True;
9766 -- Build an element list consisting of the expressions given in the
9767 -- discriminant constraint and apply the appropriate checks. The list
9768 -- is constructed after resolving any named discriminant associations
9769 -- and therefore the expressions appear in the textual order of the
9772 Discr := First_Discriminant (T);
9773 for J in Discr_Expr'Range loop
9774 if Discr_Expr (J) /= Error then
9775 Append_Elmt (Discr_Expr (J), Elist);
9777 -- If any of the discriminant constraints is given by a
9778 -- discriminant and we are in a derived type declaration we
9779 -- have a discriminant renaming. Establish link between new
9780 -- and old discriminant. The new discriminant has an implicit
9781 -- dereference if the old one does.
9783 if Denotes_Discriminant (Discr_Expr (J)) then
9786 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
9789 Set_Corresponding_Discriminant (New_Discr, Discr);
9790 Set_Has_Implicit_Dereference (New_Discr,
9791 Has_Implicit_Dereference (Discr));
9795 -- Force the evaluation of non-discriminant expressions.
9796 -- If we have found a discriminant in the constraint 3.4(26)
9797 -- and 3.8(18) demand that no range checks are performed are
9798 -- after evaluation. If the constraint is for a component
9799 -- definition that has a per-object constraint, expressions are
9800 -- evaluated but not checked either. In all other cases perform
9804 if Discrim_Present then
9807 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9808 and then Has_Per_Object_Constraint
9809 (Defining_Identifier (Parent (Parent (Def))))
9813 elsif Is_Access_Type (Etype (Discr)) then
9814 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9817 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9820 Force_Evaluation (Discr_Expr (J));
9823 -- Check that the designated type of an access discriminant's
9824 -- expression is not a class-wide type unless the discriminant's
9825 -- designated type is also class-wide.
9827 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9828 and then not Is_Class_Wide_Type
9829 (Designated_Type (Etype (Discr)))
9830 and then Etype (Discr_Expr (J)) /= Any_Type
9831 and then Is_Class_Wide_Type
9832 (Designated_Type (Etype (Discr_Expr (J))))
9834 Wrong_Type (Discr_Expr (J), Etype (Discr));
9836 elsif Is_Access_Type (Etype (Discr))
9837 and then not Is_Access_Constant (Etype (Discr))
9838 and then Is_Access_Type (Etype (Discr_Expr (J)))
9839 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9842 ("constraint for discriminant& must be access to variable",
9847 Next_Discriminant (Discr);
9851 end Build_Discriminant_Constraints;
9853 ---------------------------------
9854 -- Build_Discriminated_Subtype --
9855 ---------------------------------
9857 procedure Build_Discriminated_Subtype
9861 Related_Nod : Node_Id;
9862 For_Access : Boolean := False)
9864 Has_Discrs : constant Boolean := Has_Discriminants (T);
9865 Constrained : constant Boolean :=
9867 and then not Is_Empty_Elmt_List (Elist)
9868 and then not Is_Class_Wide_Type (T))
9869 or else Is_Constrained (T);
9872 if Ekind (T) = E_Record_Type then
9874 Set_Ekind (Def_Id, E_Private_Subtype);
9875 Set_Is_For_Access_Subtype (Def_Id, True);
9877 Set_Ekind (Def_Id, E_Record_Subtype);
9880 -- Inherit preelaboration flag from base, for types for which it
9881 -- may have been set: records, private types, protected types.
9883 Set_Known_To_Have_Preelab_Init
9884 (Def_Id, Known_To_Have_Preelab_Init (T));
9886 elsif Ekind (T) = E_Task_Type then
9887 Set_Ekind (Def_Id, E_Task_Subtype);
9889 elsif Ekind (T) = E_Protected_Type then
9890 Set_Ekind (Def_Id, E_Protected_Subtype);
9891 Set_Known_To_Have_Preelab_Init
9892 (Def_Id, Known_To_Have_Preelab_Init (T));
9894 elsif Is_Private_Type (T) then
9895 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9896 Set_Known_To_Have_Preelab_Init
9897 (Def_Id, Known_To_Have_Preelab_Init (T));
9899 -- Private subtypes may have private dependents
9901 Set_Private_Dependents (Def_Id, New_Elmt_List);
9903 elsif Is_Class_Wide_Type (T) then
9904 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9907 -- Incomplete type. Attach subtype to list of dependents, to be
9908 -- completed with full view of parent type, unless is it the
9909 -- designated subtype of a record component within an init_proc.
9910 -- This last case arises for a component of an access type whose
9911 -- designated type is incomplete (e.g. a Taft Amendment type).
9912 -- The designated subtype is within an inner scope, and needs no
9913 -- elaboration, because only the access type is needed in the
9914 -- initialization procedure.
9916 Set_Ekind (Def_Id, Ekind (T));
9918 if For_Access and then Within_Init_Proc then
9921 Append_Elmt (Def_Id, Private_Dependents (T));
9925 Set_Etype (Def_Id, T);
9926 Init_Size_Align (Def_Id);
9927 Set_Has_Discriminants (Def_Id, Has_Discrs);
9928 Set_Is_Constrained (Def_Id, Constrained);
9930 Set_First_Entity (Def_Id, First_Entity (T));
9931 Set_Last_Entity (Def_Id, Last_Entity (T));
9932 Set_Has_Implicit_Dereference
9933 (Def_Id, Has_Implicit_Dereference (T));
9934 Set_Has_Pragma_Unreferenced_Objects
9935 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
9937 -- If the subtype is the completion of a private declaration, there may
9938 -- have been representation clauses for the partial view, and they must
9939 -- be preserved. Build_Derived_Type chains the inherited clauses with
9940 -- the ones appearing on the extension. If this comes from a subtype
9941 -- declaration, all clauses are inherited.
9943 if No (First_Rep_Item (Def_Id)) then
9944 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9947 if Is_Tagged_Type (T) then
9948 Set_Is_Tagged_Type (Def_Id);
9949 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9950 Make_Class_Wide_Type (Def_Id);
9953 Set_Stored_Constraint (Def_Id, No_Elist);
9956 Set_Discriminant_Constraint (Def_Id, Elist);
9957 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9960 if Is_Tagged_Type (T) then
9962 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9963 -- concurrent record type (which has the list of primitive
9966 if Ada_Version >= Ada_2005
9967 and then Is_Concurrent_Type (T)
9969 Set_Corresponding_Record_Type (Def_Id,
9970 Corresponding_Record_Type (T));
9972 Set_Direct_Primitive_Operations (Def_Id,
9973 Direct_Primitive_Operations (T));
9976 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9979 -- Subtypes introduced by component declarations do not need to be
9980 -- marked as delayed, and do not get freeze nodes, because the semantics
9981 -- verifies that the parents of the subtypes are frozen before the
9982 -- enclosing record is frozen.
9984 if not Is_Type (Scope (Def_Id)) then
9985 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9987 if Is_Private_Type (T)
9988 and then Present (Full_View (T))
9990 Conditional_Delay (Def_Id, Full_View (T));
9992 Conditional_Delay (Def_Id, T);
9996 if Is_Record_Type (T) then
9997 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10000 and then not Is_Empty_Elmt_List (Elist)
10001 and then not For_Access
10003 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10004 elsif not For_Access then
10005 Set_Cloned_Subtype (Def_Id, T);
10008 end Build_Discriminated_Subtype;
10010 ---------------------------
10011 -- Build_Itype_Reference --
10012 ---------------------------
10014 procedure Build_Itype_Reference
10018 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10021 -- Itype references are only created for use by the back-end
10023 if Inside_A_Generic then
10026 Set_Itype (IR, Ityp);
10027 Insert_After (Nod, IR);
10029 end Build_Itype_Reference;
10031 ------------------------
10032 -- Build_Scalar_Bound --
10033 ------------------------
10035 function Build_Scalar_Bound
10038 Der_T : Entity_Id) return Node_Id
10040 New_Bound : Entity_Id;
10043 -- Note: not clear why this is needed, how can the original bound
10044 -- be unanalyzed at this point? and if it is, what business do we
10045 -- have messing around with it? and why is the base type of the
10046 -- parent type the right type for the resolution. It probably is
10047 -- not. It is OK for the new bound we are creating, but not for
10048 -- the old one??? Still if it never happens, no problem.
10050 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10052 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10053 New_Bound := New_Copy (Bound);
10054 Set_Etype (New_Bound, Der_T);
10055 Set_Analyzed (New_Bound);
10057 elsif Is_Entity_Name (Bound) then
10058 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10060 -- The following is almost certainly wrong. What business do we have
10061 -- relocating a node (Bound) that is presumably still attached to
10062 -- the tree elsewhere???
10065 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10068 Set_Etype (New_Bound, Der_T);
10070 end Build_Scalar_Bound;
10072 --------------------------------
10073 -- Build_Underlying_Full_View --
10074 --------------------------------
10076 procedure Build_Underlying_Full_View
10081 Loc : constant Source_Ptr := Sloc (N);
10082 Subt : constant Entity_Id :=
10083 Make_Defining_Identifier
10084 (Loc, New_External_Name (Chars (Typ), 'S'));
10091 procedure Set_Discriminant_Name (Id : Node_Id);
10092 -- If the derived type has discriminants, they may rename discriminants
10093 -- of the parent. When building the full view of the parent, we need to
10094 -- recover the names of the original discriminants if the constraint is
10095 -- given by named associations.
10097 ---------------------------
10098 -- Set_Discriminant_Name --
10099 ---------------------------
10101 procedure Set_Discriminant_Name (Id : Node_Id) is
10105 Set_Original_Discriminant (Id, Empty);
10107 if Has_Discriminants (Typ) then
10108 Disc := First_Discriminant (Typ);
10109 while Present (Disc) loop
10110 if Chars (Disc) = Chars (Id)
10111 and then Present (Corresponding_Discriminant (Disc))
10113 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
10115 Next_Discriminant (Disc);
10118 end Set_Discriminant_Name;
10120 -- Start of processing for Build_Underlying_Full_View
10123 if Nkind (N) = N_Full_Type_Declaration then
10124 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
10126 elsif Nkind (N) = N_Subtype_Declaration then
10127 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
10129 elsif Nkind (N) = N_Component_Declaration then
10132 (Constraint (Subtype_Indication (Component_Definition (N))));
10135 raise Program_Error;
10138 C := First (Constraints (Constr));
10139 while Present (C) loop
10140 if Nkind (C) = N_Discriminant_Association then
10141 Id := First (Selector_Names (C));
10142 while Present (Id) loop
10143 Set_Discriminant_Name (Id);
10152 Make_Subtype_Declaration (Loc,
10153 Defining_Identifier => Subt,
10154 Subtype_Indication =>
10155 Make_Subtype_Indication (Loc,
10156 Subtype_Mark => New_Occurrence_Of (Par, Loc),
10157 Constraint => New_Copy_Tree (Constr)));
10159 -- If this is a component subtype for an outer itype, it is not
10160 -- a list member, so simply set the parent link for analysis: if
10161 -- the enclosing type does not need to be in a declarative list,
10162 -- neither do the components.
10164 if Is_List_Member (N)
10165 and then Nkind (N) /= N_Component_Declaration
10167 Insert_Before (N, Indic);
10169 Set_Parent (Indic, Parent (N));
10173 Set_Underlying_Full_View (Typ, Full_View (Subt));
10174 Set_Is_Underlying_Full_View (Full_View (Subt));
10175 end Build_Underlying_Full_View;
10177 -------------------------------
10178 -- Check_Abstract_Overriding --
10179 -------------------------------
10181 procedure Check_Abstract_Overriding (T : Entity_Id) is
10182 Alias_Subp : Entity_Id;
10184 Op_List : Elist_Id;
10186 Type_Def : Node_Id;
10188 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10189 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10190 -- which has pragma Implemented already set. Check whether Subp's entity
10191 -- kind conforms to the implementation kind of the overridden routine.
10193 procedure Check_Pragma_Implemented
10195 Iface_Subp : Entity_Id);
10196 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10197 -- Iface_Subp and both entities have pragma Implemented already set on
10198 -- them. Check whether the two implementation kinds are conforming.
10200 procedure Inherit_Pragma_Implemented
10202 Iface_Subp : Entity_Id);
10203 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10204 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10205 -- Propagate the implementation kind of Iface_Subp to Subp.
10207 ------------------------------
10208 -- Check_Pragma_Implemented --
10209 ------------------------------
10211 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10212 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10213 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10214 Subp_Alias : constant Entity_Id := Alias (Subp);
10215 Contr_Typ : Entity_Id;
10216 Impl_Subp : Entity_Id;
10219 -- Subp must have an alias since it is a hidden entity used to link
10220 -- an interface subprogram to its overriding counterpart.
10222 pragma Assert (Present (Subp_Alias));
10224 -- Handle aliases to synchronized wrappers
10226 Impl_Subp := Subp_Alias;
10228 if Is_Primitive_Wrapper (Impl_Subp) then
10229 Impl_Subp := Wrapped_Entity (Impl_Subp);
10232 -- Extract the type of the controlling formal
10234 Contr_Typ := Etype (First_Formal (Subp_Alias));
10236 if Is_Concurrent_Record_Type (Contr_Typ) then
10237 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10240 -- An interface subprogram whose implementation kind is By_Entry must
10241 -- be implemented by an entry.
10243 if Impl_Kind = Name_By_Entry
10244 and then Ekind (Impl_Subp) /= E_Entry
10246 Error_Msg_Node_2 := Iface_Alias;
10248 ("type & must implement abstract subprogram & with an entry",
10249 Subp_Alias, Contr_Typ);
10251 elsif Impl_Kind = Name_By_Protected_Procedure then
10253 -- An interface subprogram whose implementation kind is By_
10254 -- Protected_Procedure cannot be implemented by a primitive
10255 -- procedure of a task type.
10257 if Ekind (Contr_Typ) /= E_Protected_Type then
10258 Error_Msg_Node_2 := Contr_Typ;
10260 ("interface subprogram & cannot be implemented by a " &
10261 "primitive procedure of task type &", Subp_Alias,
10264 -- An interface subprogram whose implementation kind is By_
10265 -- Protected_Procedure must be implemented by a procedure.
10267 elsif Ekind (Impl_Subp) /= E_Procedure then
10268 Error_Msg_Node_2 := Iface_Alias;
10270 ("type & must implement abstract subprogram & with a " &
10271 "procedure", Subp_Alias, Contr_Typ);
10273 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10274 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10276 Error_Msg_Name_1 := Impl_Kind;
10278 ("overriding operation& must have synchronization%",
10282 -- If primitive has Optional synchronization, overriding operation
10283 -- must match if it has an explicit synchronization..
10285 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10286 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10288 Error_Msg_Name_1 := Impl_Kind;
10290 ("overriding operation& must have syncrhonization%",
10293 end Check_Pragma_Implemented;
10295 ------------------------------
10296 -- Check_Pragma_Implemented --
10297 ------------------------------
10299 procedure Check_Pragma_Implemented
10301 Iface_Subp : Entity_Id)
10303 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10304 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10307 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10308 -- and overriding subprogram are different. In general this is an
10309 -- error except when the implementation kind of the overridden
10310 -- subprograms is By_Any or Optional.
10312 if Iface_Kind /= Subp_Kind
10313 and then Iface_Kind /= Name_By_Any
10314 and then Iface_Kind /= Name_Optional
10316 if Iface_Kind = Name_By_Entry then
10318 ("incompatible implementation kind, overridden subprogram " &
10319 "is marked By_Entry", Subp);
10322 ("incompatible implementation kind, overridden subprogram " &
10323 "is marked By_Protected_Procedure", Subp);
10326 end Check_Pragma_Implemented;
10328 --------------------------------
10329 -- Inherit_Pragma_Implemented --
10330 --------------------------------
10332 procedure Inherit_Pragma_Implemented
10334 Iface_Subp : Entity_Id)
10336 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10337 Loc : constant Source_Ptr := Sloc (Subp);
10338 Impl_Prag : Node_Id;
10341 -- Since the implementation kind is stored as a representation item
10342 -- rather than a flag, create a pragma node.
10346 Chars => Name_Implemented,
10347 Pragma_Argument_Associations => New_List (
10348 Make_Pragma_Argument_Association (Loc,
10349 Expression => New_Occurrence_Of (Subp, Loc)),
10351 Make_Pragma_Argument_Association (Loc,
10352 Expression => Make_Identifier (Loc, Iface_Kind))));
10354 -- The pragma doesn't need to be analyzed because it is internally
10355 -- built. It is safe to directly register it as a rep item since we
10356 -- are only interested in the characters of the implementation kind.
10358 Record_Rep_Item (Subp, Impl_Prag);
10359 end Inherit_Pragma_Implemented;
10361 -- Start of processing for Check_Abstract_Overriding
10364 Op_List := Primitive_Operations (T);
10366 -- Loop to check primitive operations
10368 Elmt := First_Elmt (Op_List);
10369 while Present (Elmt) loop
10370 Subp := Node (Elmt);
10371 Alias_Subp := Alias (Subp);
10373 -- Inherited subprograms are identified by the fact that they do not
10374 -- come from source, and the associated source location is the
10375 -- location of the first subtype of the derived type.
10377 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10378 -- subprograms that "require overriding".
10380 -- Special exception, do not complain about failure to override the
10381 -- stream routines _Input and _Output, as well as the primitive
10382 -- operations used in dispatching selects since we always provide
10383 -- automatic overridings for these subprograms.
10385 -- The partial view of T may have been a private extension, for
10386 -- which inherited functions dispatching on result are abstract.
10387 -- If the full view is a null extension, there is no need for
10388 -- overriding in Ada 2005, but wrappers need to be built for them
10389 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10391 if Is_Null_Extension (T)
10392 and then Has_Controlling_Result (Subp)
10393 and then Ada_Version >= Ada_2005
10394 and then Present (Alias_Subp)
10395 and then not Comes_From_Source (Subp)
10396 and then not Is_Abstract_Subprogram (Alias_Subp)
10397 and then not Is_Access_Type (Etype (Subp))
10401 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10402 -- processing because this check is done with the aliased
10405 elsif Present (Interface_Alias (Subp)) then
10408 elsif (Is_Abstract_Subprogram (Subp)
10409 or else Requires_Overriding (Subp)
10411 (Has_Controlling_Result (Subp)
10412 and then Present (Alias_Subp)
10413 and then not Comes_From_Source (Subp)
10414 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10415 and then not Is_TSS (Subp, TSS_Stream_Input)
10416 and then not Is_TSS (Subp, TSS_Stream_Output)
10417 and then not Is_Abstract_Type (T)
10418 and then not Is_Predefined_Interface_Primitive (Subp)
10420 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10421 -- with abstract interface types because the check will be done
10422 -- with the aliased entity (otherwise we generate a duplicated
10425 and then not Present (Interface_Alias (Subp))
10427 if Present (Alias_Subp) then
10429 -- Only perform the check for a derived subprogram when the
10430 -- type has an explicit record extension. This avoids incorrect
10431 -- flagging of abstract subprograms for the case of a type
10432 -- without an extension that is derived from a formal type
10433 -- with a tagged actual (can occur within a private part).
10435 -- Ada 2005 (AI-391): In the case of an inherited function with
10436 -- a controlling result of the type, the rule does not apply if
10437 -- the type is a null extension (unless the parent function
10438 -- itself is abstract, in which case the function must still be
10439 -- be overridden). The expander will generate an overriding
10440 -- wrapper function calling the parent subprogram (see
10441 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10443 Type_Def := Type_Definition (Parent (T));
10445 if Nkind (Type_Def) = N_Derived_Type_Definition
10446 and then Present (Record_Extension_Part (Type_Def))
10448 (Ada_Version < Ada_2005
10449 or else not Is_Null_Extension (T)
10450 or else Ekind (Subp) = E_Procedure
10451 or else not Has_Controlling_Result (Subp)
10452 or else Is_Abstract_Subprogram (Alias_Subp)
10453 or else Requires_Overriding (Subp)
10454 or else Is_Access_Type (Etype (Subp)))
10456 -- Avoid reporting error in case of abstract predefined
10457 -- primitive inherited from interface type because the
10458 -- body of internally generated predefined primitives
10459 -- of tagged types are generated later by Freeze_Type
10461 if Is_Interface (Root_Type (T))
10462 and then Is_Abstract_Subprogram (Subp)
10463 and then Is_Predefined_Dispatching_Operation (Subp)
10464 and then not Comes_From_Source (Ultimate_Alias (Subp))
10468 -- A null extension is not obliged to override an inherited
10469 -- procedure subject to pragma Extensions_Visible with value
10470 -- False and at least one controlling OUT parameter
10471 -- (SPARK RM 6.1.7(6)).
10473 elsif Is_Null_Extension (T)
10474 and then Is_EVF_Procedure (Subp)
10480 ("type must be declared abstract or & overridden",
10483 -- Traverse the whole chain of aliased subprograms to
10484 -- complete the error notification. This is especially
10485 -- useful for traceability of the chain of entities when
10486 -- the subprogram corresponds with an interface
10487 -- subprogram (which may be defined in another package).
10489 if Present (Alias_Subp) then
10495 while Present (Alias (E)) loop
10497 -- Avoid reporting redundant errors on entities
10498 -- inherited from interfaces
10500 if Sloc (E) /= Sloc (T) then
10501 Error_Msg_Sloc := Sloc (E);
10503 ("\& has been inherited #", T, Subp);
10509 Error_Msg_Sloc := Sloc (E);
10511 -- AI05-0068: report if there is an overriding
10512 -- non-abstract subprogram that is invisible.
10515 and then not Is_Abstract_Subprogram (E)
10518 ("\& subprogram# is not visible",
10521 -- Clarify the case where a non-null extension must
10522 -- override inherited procedure subject to pragma
10523 -- Extensions_Visible with value False and at least
10524 -- one controlling OUT param.
10526 elsif Is_EVF_Procedure (E) then
10528 ("\& # is subject to Extensions_Visible False",
10533 ("\& has been inherited from subprogram #",
10540 -- Ada 2005 (AI-345): Protected or task type implementing
10541 -- abstract interfaces.
10543 elsif Is_Concurrent_Record_Type (T)
10544 and then Present (Interfaces (T))
10546 -- There is no need to check here RM 9.4(11.9/3) since we
10547 -- are processing the corresponding record type and the
10548 -- mode of the overriding subprograms was verified by
10549 -- Check_Conformance when the corresponding concurrent
10550 -- type declaration was analyzed.
10553 ("interface subprogram & must be overridden", T, Subp);
10555 -- Examine primitive operations of synchronized type to find
10556 -- homonyms that have the wrong profile.
10562 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10563 while Present (Prim) loop
10564 if Chars (Prim) = Chars (Subp) then
10566 ("profile is not type conformant with prefixed "
10567 & "view profile of inherited operation&",
10571 Next_Entity (Prim);
10577 Error_Msg_Node_2 := T;
10579 ("abstract subprogram& not allowed for type&", Subp);
10581 -- Also post unconditional warning on the type (unconditional
10582 -- so that if there are more than one of these cases, we get
10583 -- them all, and not just the first one).
10585 Error_Msg_Node_2 := Subp;
10586 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10589 -- A subprogram subject to pragma Extensions_Visible with value
10590 -- "True" cannot override a subprogram subject to the same pragma
10591 -- with value "False" (SPARK RM 6.1.7(5)).
10593 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10594 and then Present (Overridden_Operation (Subp))
10595 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10596 Extensions_Visible_False
10598 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10600 ("subprogram & with Extensions_Visible True cannot override "
10601 & "subprogram # with Extensions_Visible False", Subp);
10604 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10606 -- Subp is an expander-generated procedure which maps an interface
10607 -- alias to a protected wrapper. The interface alias is flagged by
10608 -- pragma Implemented. Ensure that Subp is a procedure when the
10609 -- implementation kind is By_Protected_Procedure or an entry when
10612 if Ada_Version >= Ada_2012
10613 and then Is_Hidden (Subp)
10614 and then Present (Interface_Alias (Subp))
10615 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10617 Check_Pragma_Implemented (Subp);
10620 -- Subp is an interface primitive which overrides another interface
10621 -- primitive marked with pragma Implemented.
10623 if Ada_Version >= Ada_2012
10624 and then Present (Overridden_Operation (Subp))
10625 and then Has_Rep_Pragma
10626 (Overridden_Operation (Subp), Name_Implemented)
10628 -- If the overriding routine is also marked by Implemented, check
10629 -- that the two implementation kinds are conforming.
10631 if Has_Rep_Pragma (Subp, Name_Implemented) then
10632 Check_Pragma_Implemented
10634 Iface_Subp => Overridden_Operation (Subp));
10636 -- Otherwise the overriding routine inherits the implementation
10637 -- kind from the overridden subprogram.
10640 Inherit_Pragma_Implemented
10642 Iface_Subp => Overridden_Operation (Subp));
10646 -- If the operation is a wrapper for a synchronized primitive, it
10647 -- may be called indirectly through a dispatching select. We assume
10648 -- that it will be referenced elsewhere indirectly, and suppress
10649 -- warnings about an unused entity.
10651 if Is_Primitive_Wrapper (Subp)
10652 and then Present (Wrapped_Entity (Subp))
10654 Set_Referenced (Wrapped_Entity (Subp));
10659 end Check_Abstract_Overriding;
10661 ------------------------------------------------
10662 -- Check_Access_Discriminant_Requires_Limited --
10663 ------------------------------------------------
10665 procedure Check_Access_Discriminant_Requires_Limited
10670 -- A discriminant_specification for an access discriminant shall appear
10671 -- only in the declaration for a task or protected type, or for a type
10672 -- with the reserved word 'limited' in its definition or in one of its
10673 -- ancestors (RM 3.7(10)).
10675 -- AI-0063: The proper condition is that type must be immutably limited,
10676 -- or else be a partial view.
10678 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10679 if Is_Limited_View (Current_Scope)
10681 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10682 and then Limited_Present (Parent (Current_Scope)))
10688 ("access discriminants allowed only for limited types", Loc);
10691 end Check_Access_Discriminant_Requires_Limited;
10693 -----------------------------------
10694 -- Check_Aliased_Component_Types --
10695 -----------------------------------
10697 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10701 -- ??? Also need to check components of record extensions, but not
10702 -- components of protected types (which are always limited).
10704 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10705 -- types to be unconstrained. This is safe because it is illegal to
10706 -- create access subtypes to such types with explicit discriminant
10709 if not Is_Limited_Type (T) then
10710 if Ekind (T) = E_Record_Type then
10711 C := First_Component (T);
10712 while Present (C) loop
10714 and then Has_Discriminants (Etype (C))
10715 and then not Is_Constrained (Etype (C))
10716 and then not In_Instance_Body
10717 and then Ada_Version < Ada_2005
10720 ("aliased component must be constrained (RM 3.6(11))",
10724 Next_Component (C);
10727 elsif Ekind (T) = E_Array_Type then
10728 if Has_Aliased_Components (T)
10729 and then Has_Discriminants (Component_Type (T))
10730 and then not Is_Constrained (Component_Type (T))
10731 and then not In_Instance_Body
10732 and then Ada_Version < Ada_2005
10735 ("aliased component type must be constrained (RM 3.6(11))",
10740 end Check_Aliased_Component_Types;
10742 ---------------------------------------
10743 -- Check_Anonymous_Access_Components --
10744 ---------------------------------------
10746 procedure Check_Anonymous_Access_Components
10747 (Typ_Decl : Node_Id;
10750 Comp_List : Node_Id)
10752 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10753 Anon_Access : Entity_Id;
10756 Comp_Def : Node_Id;
10758 Type_Def : Node_Id;
10760 procedure Build_Incomplete_Type_Declaration;
10761 -- If the record type contains components that include an access to the
10762 -- current record, then create an incomplete type declaration for the
10763 -- record, to be used as the designated type of the anonymous access.
10764 -- This is done only once, and only if there is no previous partial
10765 -- view of the type.
10767 function Designates_T (Subt : Node_Id) return Boolean;
10768 -- Check whether a node designates the enclosing record type, or 'Class
10771 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10772 -- Check whether an access definition includes a reference to
10773 -- the enclosing record type. The reference can be a subtype mark
10774 -- in the access definition itself, a 'Class attribute reference, or
10775 -- recursively a reference appearing in a parameter specification
10776 -- or result definition of an access_to_subprogram definition.
10778 --------------------------------------
10779 -- Build_Incomplete_Type_Declaration --
10780 --------------------------------------
10782 procedure Build_Incomplete_Type_Declaration is
10787 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10788 -- it's "is new ... with record" or else "is tagged record ...".
10790 Is_Tagged : constant Boolean :=
10791 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10793 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10795 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10796 and then Tagged_Present (Type_Definition (Typ_Decl)));
10799 -- If there is a previous partial view, no need to create a new one
10800 -- If the partial view, given by Prev, is incomplete, If Prev is
10801 -- a private declaration, full declaration is flagged accordingly.
10803 if Prev /= Typ then
10805 Make_Class_Wide_Type (Prev);
10806 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10807 Set_Etype (Class_Wide_Type (Typ), Typ);
10812 elsif Has_Private_Declaration (Typ) then
10814 -- If we refer to T'Class inside T, and T is the completion of a
10815 -- private type, then make sure the class-wide type exists.
10818 Make_Class_Wide_Type (Typ);
10823 -- If there was a previous anonymous access type, the incomplete
10824 -- type declaration will have been created already.
10826 elsif Present (Current_Entity (Typ))
10827 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10828 and then Full_View (Current_Entity (Typ)) = Typ
10831 and then Comes_From_Source (Current_Entity (Typ))
10832 and then not Is_Tagged_Type (Current_Entity (Typ))
10834 Make_Class_Wide_Type (Typ);
10836 ("incomplete view of tagged type should be declared tagged??",
10837 Parent (Current_Entity (Typ)));
10842 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10843 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10845 -- Type has already been inserted into the current scope. Remove
10846 -- it, and add incomplete declaration for type, so that subsequent
10847 -- anonymous access types can use it. The entity is unchained from
10848 -- the homonym list and from immediate visibility. After analysis,
10849 -- the entity in the incomplete declaration becomes immediately
10850 -- visible in the record declaration that follows.
10852 H := Current_Entity (Typ);
10855 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10858 and then Homonym (H) /= Typ
10860 H := Homonym (Typ);
10863 Set_Homonym (H, Homonym (Typ));
10866 Insert_Before (Typ_Decl, Decl);
10868 Set_Full_View (Inc_T, Typ);
10872 -- Create a common class-wide type for both views, and set the
10873 -- Etype of the class-wide type to the full view.
10875 Make_Class_Wide_Type (Inc_T);
10876 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10877 Set_Etype (Class_Wide_Type (Typ), Typ);
10880 end Build_Incomplete_Type_Declaration;
10886 function Designates_T (Subt : Node_Id) return Boolean is
10887 Type_Id : constant Name_Id := Chars (Typ);
10889 function Names_T (Nam : Node_Id) return Boolean;
10890 -- The record type has not been introduced in the current scope
10891 -- yet, so we must examine the name of the type itself, either
10892 -- an identifier T, or an expanded name of the form P.T, where
10893 -- P denotes the current scope.
10899 function Names_T (Nam : Node_Id) return Boolean is
10901 if Nkind (Nam) = N_Identifier then
10902 return Chars (Nam) = Type_Id;
10904 elsif Nkind (Nam) = N_Selected_Component then
10905 if Chars (Selector_Name (Nam)) = Type_Id then
10906 if Nkind (Prefix (Nam)) = N_Identifier then
10907 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10909 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10910 return Chars (Selector_Name (Prefix (Nam))) =
10911 Chars (Current_Scope);
10925 -- Start of processing for Designates_T
10928 if Nkind (Subt) = N_Identifier then
10929 return Chars (Subt) = Type_Id;
10931 -- Reference can be through an expanded name which has not been
10932 -- analyzed yet, and which designates enclosing scopes.
10934 elsif Nkind (Subt) = N_Selected_Component then
10935 if Names_T (Subt) then
10938 -- Otherwise it must denote an entity that is already visible.
10939 -- The access definition may name a subtype of the enclosing
10940 -- type, if there is a previous incomplete declaration for it.
10943 Find_Selected_Component (Subt);
10945 Is_Entity_Name (Subt)
10946 and then Scope (Entity (Subt)) = Current_Scope
10948 (Chars (Base_Type (Entity (Subt))) = Type_Id
10950 (Is_Class_Wide_Type (Entity (Subt))
10952 Chars (Etype (Base_Type (Entity (Subt)))) =
10956 -- A reference to the current type may appear as the prefix of
10957 -- a 'Class attribute.
10959 elsif Nkind (Subt) = N_Attribute_Reference
10960 and then Attribute_Name (Subt) = Name_Class
10962 return Names_T (Prefix (Subt));
10973 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10974 Param_Spec : Node_Id;
10976 Acc_Subprg : constant Node_Id :=
10977 Access_To_Subprogram_Definition (Acc_Def);
10980 if No (Acc_Subprg) then
10981 return Designates_T (Subtype_Mark (Acc_Def));
10984 -- Component is an access_to_subprogram: examine its formals,
10985 -- and result definition in the case of an access_to_function.
10987 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10988 while Present (Param_Spec) loop
10989 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10990 and then Mentions_T (Parameter_Type (Param_Spec))
10994 elsif Designates_T (Parameter_Type (Param_Spec)) then
11001 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11002 if Nkind (Result_Definition (Acc_Subprg)) =
11003 N_Access_Definition
11005 return Mentions_T (Result_Definition (Acc_Subprg));
11007 return Designates_T (Result_Definition (Acc_Subprg));
11014 -- Start of processing for Check_Anonymous_Access_Components
11017 if No (Comp_List) then
11021 Comp := First (Component_Items (Comp_List));
11022 while Present (Comp) loop
11023 if Nkind (Comp) = N_Component_Declaration
11025 (Access_Definition (Component_Definition (Comp)))
11027 Mentions_T (Access_Definition (Component_Definition (Comp)))
11029 Comp_Def := Component_Definition (Comp);
11031 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11033 Build_Incomplete_Type_Declaration;
11034 Anon_Access := Make_Temporary (Loc, 'S');
11036 -- Create a declaration for the anonymous access type: either
11037 -- an access_to_object or an access_to_subprogram.
11039 if Present (Acc_Def) then
11040 if Nkind (Acc_Def) = N_Access_Function_Definition then
11042 Make_Access_Function_Definition (Loc,
11043 Parameter_Specifications =>
11044 Parameter_Specifications (Acc_Def),
11045 Result_Definition => Result_Definition (Acc_Def));
11048 Make_Access_Procedure_Definition (Loc,
11049 Parameter_Specifications =>
11050 Parameter_Specifications (Acc_Def));
11055 Make_Access_To_Object_Definition (Loc,
11056 Subtype_Indication =>
11058 (Subtype_Mark (Access_Definition (Comp_Def))));
11060 Set_Constant_Present
11061 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11063 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11066 Set_Null_Exclusion_Present
11068 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11071 Make_Full_Type_Declaration (Loc,
11072 Defining_Identifier => Anon_Access,
11073 Type_Definition => Type_Def);
11075 Insert_Before (Typ_Decl, Decl);
11078 -- If an access to subprogram, create the extra formals
11080 if Present (Acc_Def) then
11081 Create_Extra_Formals (Designated_Type (Anon_Access));
11083 -- If an access to object, preserve entity of designated type,
11084 -- for ASIS use, before rewriting the component definition.
11091 Desig := Entity (Subtype_Indication (Type_Def));
11093 -- If the access definition is to the current record,
11094 -- the visible entity at this point is an incomplete
11095 -- type. Retrieve the full view to simplify ASIS queries
11097 if Ekind (Desig) = E_Incomplete_Type then
11098 Desig := Full_View (Desig);
11102 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11107 Make_Component_Definition (Loc,
11108 Subtype_Indication =>
11109 New_Occurrence_Of (Anon_Access, Loc)));
11111 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11112 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11114 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11117 Set_Is_Local_Anonymous_Access (Anon_Access);
11123 if Present (Variant_Part (Comp_List)) then
11127 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11128 while Present (V) loop
11129 Check_Anonymous_Access_Components
11130 (Typ_Decl, Typ, Prev, Component_List (V));
11131 Next_Non_Pragma (V);
11135 end Check_Anonymous_Access_Components;
11137 ----------------------
11138 -- Check_Completion --
11139 ----------------------
11141 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11144 procedure Post_Error;
11145 -- Post error message for lack of completion for entity E
11151 procedure Post_Error is
11152 procedure Missing_Body;
11153 -- Output missing body message
11159 procedure Missing_Body is
11161 -- Spec is in same unit, so we can post on spec
11163 if In_Same_Source_Unit (Body_Id, E) then
11164 Error_Msg_N ("missing body for &", E);
11166 -- Spec is in a separate unit, so we have to post on the body
11169 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11173 -- Start of processing for Post_Error
11176 if not Comes_From_Source (E) then
11177 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11179 -- It may be an anonymous protected type created for a
11180 -- single variable. Post error on variable, if present.
11186 Var := First_Entity (Current_Scope);
11187 while Present (Var) loop
11188 exit when Etype (Var) = E
11189 and then Comes_From_Source (Var);
11194 if Present (Var) then
11201 -- If a generated entity has no completion, then either previous
11202 -- semantic errors have disabled the expansion phase, or else we had
11203 -- missing subunits, or else we are compiling without expansion,
11204 -- or else something is very wrong.
11206 if not Comes_From_Source (E) then
11208 (Serious_Errors_Detected > 0
11209 or else Configurable_Run_Time_Violations > 0
11210 or else Subunits_Missing
11211 or else not Expander_Active);
11214 -- Here for source entity
11217 -- Here if no body to post the error message, so we post the error
11218 -- on the declaration that has no completion. This is not really
11219 -- the right place to post it, think about this later ???
11221 if No (Body_Id) then
11222 if Is_Type (E) then
11224 ("missing full declaration for }", Parent (E), E);
11226 Error_Msg_NE ("missing body for &", Parent (E), E);
11229 -- Package body has no completion for a declaration that appears
11230 -- in the corresponding spec. Post error on the body, with a
11231 -- reference to the non-completed declaration.
11234 Error_Msg_Sloc := Sloc (E);
11236 if Is_Type (E) then
11237 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11239 elsif Is_Overloadable (E)
11240 and then Current_Entity_In_Scope (E) /= E
11242 -- It may be that the completion is mistyped and appears as
11243 -- a distinct overloading of the entity.
11246 Candidate : constant Entity_Id :=
11247 Current_Entity_In_Scope (E);
11248 Decl : constant Node_Id :=
11249 Unit_Declaration_Node (Candidate);
11252 if Is_Overloadable (Candidate)
11253 and then Ekind (Candidate) = Ekind (E)
11254 and then Nkind (Decl) = N_Subprogram_Body
11255 and then Acts_As_Spec (Decl)
11257 Check_Type_Conformant (Candidate, E);
11273 Pack_Id : constant Entity_Id := Current_Scope;
11275 -- Start of processing for Check_Completion
11278 E := First_Entity (Pack_Id);
11279 while Present (E) loop
11280 if Is_Intrinsic_Subprogram (E) then
11283 -- The following situation requires special handling: a child unit
11284 -- that appears in the context clause of the body of its parent:
11286 -- procedure Parent.Child (...);
11288 -- with Parent.Child;
11289 -- package body Parent is
11291 -- Here Parent.Child appears as a local entity, but should not be
11292 -- flagged as requiring completion, because it is a compilation
11295 -- Ignore missing completion for a subprogram that does not come from
11296 -- source (including the _Call primitive operation of RAS types,
11297 -- which has to have the flag Comes_From_Source for other purposes):
11298 -- we assume that the expander will provide the missing completion.
11299 -- In case of previous errors, other expansion actions that provide
11300 -- bodies for null procedures with not be invoked, so inhibit message
11303 -- Note that E_Operator is not in the list that follows, because
11304 -- this kind is reserved for predefined operators, that are
11305 -- intrinsic and do not need completion.
11307 elsif Ekind_In (E, E_Function,
11309 E_Generic_Function,
11310 E_Generic_Procedure)
11312 if Has_Completion (E) then
11315 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11318 elsif Is_Subprogram (E)
11319 and then (not Comes_From_Source (E)
11320 or else Chars (E) = Name_uCall)
11325 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11329 elsif Nkind (Parent (E)) = N_Procedure_Specification
11330 and then Null_Present (Parent (E))
11331 and then Serious_Errors_Detected > 0
11339 elsif Is_Entry (E) then
11340 if not Has_Completion (E) and then
11341 (Ekind (Scope (E)) = E_Protected_Object
11342 or else Ekind (Scope (E)) = E_Protected_Type)
11347 elsif Is_Package_Or_Generic_Package (E) then
11348 if Unit_Requires_Body (E) then
11349 if not Has_Completion (E)
11350 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11356 elsif not Is_Child_Unit (E) then
11357 May_Need_Implicit_Body (E);
11360 -- A formal incomplete type (Ada 2012) does not require a completion;
11361 -- other incomplete type declarations do.
11363 elsif Ekind (E) = E_Incomplete_Type
11364 and then No (Underlying_Type (E))
11365 and then not Is_Generic_Type (E)
11369 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11370 and then not Has_Completion (E)
11374 -- A single task declared in the current scope is a constant, verify
11375 -- that the body of its anonymous type is in the same scope. If the
11376 -- task is defined elsewhere, this may be a renaming declaration for
11377 -- which no completion is needed.
11379 elsif Ekind (E) = E_Constant
11380 and then Ekind (Etype (E)) = E_Task_Type
11381 and then not Has_Completion (Etype (E))
11382 and then Scope (Etype (E)) = Current_Scope
11386 elsif Ekind (E) = E_Protected_Object
11387 and then not Has_Completion (Etype (E))
11391 elsif Ekind (E) = E_Record_Type then
11392 if Is_Tagged_Type (E) then
11393 Check_Abstract_Overriding (E);
11394 Check_Conventions (E);
11397 Check_Aliased_Component_Types (E);
11399 elsif Ekind (E) = E_Array_Type then
11400 Check_Aliased_Component_Types (E);
11406 end Check_Completion;
11408 ------------------------------------
11409 -- Check_CPP_Type_Has_No_Defaults --
11410 ------------------------------------
11412 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11413 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11418 -- Obtain the component list
11420 if Nkind (Tdef) = N_Record_Definition then
11421 Clist := Component_List (Tdef);
11422 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11423 Clist := Component_List (Record_Extension_Part (Tdef));
11426 -- Check all components to ensure no default expressions
11428 if Present (Clist) then
11429 Comp := First (Component_Items (Clist));
11430 while Present (Comp) loop
11431 if Present (Expression (Comp)) then
11433 ("component of imported 'C'P'P type cannot have "
11434 & "default expression", Expression (Comp));
11440 end Check_CPP_Type_Has_No_Defaults;
11442 ----------------------------
11443 -- Check_Delta_Expression --
11444 ----------------------------
11446 procedure Check_Delta_Expression (E : Node_Id) is
11448 if not (Is_Real_Type (Etype (E))) then
11449 Wrong_Type (E, Any_Real);
11451 elsif not Is_OK_Static_Expression (E) then
11452 Flag_Non_Static_Expr
11453 ("non-static expression used for delta value!", E);
11455 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11456 Error_Msg_N ("delta expression must be positive", E);
11462 -- If any of above errors occurred, then replace the incorrect
11463 -- expression by the real 0.1, which should prevent further errors.
11466 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11467 Analyze_And_Resolve (E, Standard_Float);
11468 end Check_Delta_Expression;
11470 -----------------------------
11471 -- Check_Digits_Expression --
11472 -----------------------------
11474 procedure Check_Digits_Expression (E : Node_Id) is
11476 if not (Is_Integer_Type (Etype (E))) then
11477 Wrong_Type (E, Any_Integer);
11479 elsif not Is_OK_Static_Expression (E) then
11480 Flag_Non_Static_Expr
11481 ("non-static expression used for digits value!", E);
11483 elsif Expr_Value (E) <= 0 then
11484 Error_Msg_N ("digits value must be greater than zero", E);
11490 -- If any of above errors occurred, then replace the incorrect
11491 -- expression by the integer 1, which should prevent further errors.
11493 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11494 Analyze_And_Resolve (E, Standard_Integer);
11496 end Check_Digits_Expression;
11498 --------------------------
11499 -- Check_Initialization --
11500 --------------------------
11502 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11504 -- Special processing for limited types
11506 if Is_Limited_Type (T)
11507 and then not In_Instance
11508 and then not In_Inlined_Body
11510 if not OK_For_Limited_Init (T, Exp) then
11512 -- In GNAT mode, this is just a warning, to allow it to be evilly
11513 -- turned off. Otherwise it is a real error.
11517 ("??cannot initialize entities of limited type!", Exp);
11519 elsif Ada_Version < Ada_2005 then
11521 -- The side effect removal machinery may generate illegal Ada
11522 -- code to avoid the usage of access types and 'reference in
11523 -- SPARK mode. Since this is legal code with respect to theorem
11524 -- proving, do not emit the error.
11527 and then Nkind (Exp) = N_Function_Call
11528 and then Nkind (Parent (Exp)) = N_Object_Declaration
11529 and then not Comes_From_Source
11530 (Defining_Identifier (Parent (Exp)))
11536 ("cannot initialize entities of limited type", Exp);
11537 Explain_Limited_Type (T, Exp);
11541 -- Specialize error message according to kind of illegal
11542 -- initial expression.
11544 if Nkind (Exp) = N_Type_Conversion
11545 and then Nkind (Expression (Exp)) = N_Function_Call
11548 ("illegal context for call"
11549 & " to function with limited result", Exp);
11553 ("initialization of limited object requires aggregate "
11554 & "or function call", Exp);
11560 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11561 -- set unless we can be sure that no range check is required.
11563 if (GNATprove_Mode or not Expander_Active)
11564 and then Is_Scalar_Type (T)
11565 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11567 Set_Do_Range_Check (Exp);
11569 end Check_Initialization;
11571 ----------------------
11572 -- Check_Interfaces --
11573 ----------------------
11575 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11576 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11579 Iface_Def : Node_Id;
11580 Iface_Typ : Entity_Id;
11581 Parent_Node : Node_Id;
11583 Is_Task : Boolean := False;
11584 -- Set True if parent type or any progenitor is a task interface
11586 Is_Protected : Boolean := False;
11587 -- Set True if parent type or any progenitor is a protected interface
11589 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11590 -- Check that a progenitor is compatible with declaration. If an error
11591 -- message is output, it is posted on Error_Node.
11597 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11598 Iface_Id : constant Entity_Id :=
11599 Defining_Identifier (Parent (Iface_Def));
11600 Type_Def : Node_Id;
11603 if Nkind (N) = N_Private_Extension_Declaration then
11606 Type_Def := Type_Definition (N);
11609 if Is_Task_Interface (Iface_Id) then
11612 elsif Is_Protected_Interface (Iface_Id) then
11613 Is_Protected := True;
11616 if Is_Synchronized_Interface (Iface_Id) then
11618 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11619 -- extension derived from a synchronized interface must explicitly
11620 -- be declared synchronized, because the full view will be a
11621 -- synchronized type.
11623 if Nkind (N) = N_Private_Extension_Declaration then
11624 if not Synchronized_Present (N) then
11626 ("private extension of& must be explicitly synchronized",
11630 -- However, by 3.9.4(16/2), a full type that is a record extension
11631 -- is never allowed to derive from a synchronized interface (note
11632 -- that interfaces must be excluded from this check, because those
11633 -- are represented by derived type definitions in some cases).
11635 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11636 and then not Interface_Present (Type_Definition (N))
11638 Error_Msg_N ("record extension cannot derive from synchronized "
11639 & "interface", Error_Node);
11643 -- Check that the characteristics of the progenitor are compatible
11644 -- with the explicit qualifier in the declaration.
11645 -- The check only applies to qualifiers that come from source.
11646 -- Limited_Present also appears in the declaration of corresponding
11647 -- records, and the check does not apply to them.
11649 if Limited_Present (Type_Def)
11651 Is_Concurrent_Record_Type (Defining_Identifier (N))
11653 if Is_Limited_Interface (Parent_Type)
11654 and then not Is_Limited_Interface (Iface_Id)
11657 ("progenitor & must be limited interface",
11658 Error_Node, Iface_Id);
11661 (Task_Present (Iface_Def)
11662 or else Protected_Present (Iface_Def)
11663 or else Synchronized_Present (Iface_Def))
11664 and then Nkind (N) /= N_Private_Extension_Declaration
11665 and then not Error_Posted (N)
11668 ("progenitor & must be limited interface",
11669 Error_Node, Iface_Id);
11672 -- Protected interfaces can only inherit from limited, synchronized
11673 -- or protected interfaces.
11675 elsif Nkind (N) = N_Full_Type_Declaration
11676 and then Protected_Present (Type_Def)
11678 if Limited_Present (Iface_Def)
11679 or else Synchronized_Present (Iface_Def)
11680 or else Protected_Present (Iface_Def)
11684 elsif Task_Present (Iface_Def) then
11685 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11686 & "from task interface", Error_Node);
11689 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11690 & "from non-limited interface", Error_Node);
11693 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11694 -- limited and synchronized.
11696 elsif Synchronized_Present (Type_Def) then
11697 if Limited_Present (Iface_Def)
11698 or else Synchronized_Present (Iface_Def)
11702 elsif Protected_Present (Iface_Def)
11703 and then Nkind (N) /= N_Private_Extension_Declaration
11705 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11706 & "from protected interface", Error_Node);
11708 elsif Task_Present (Iface_Def)
11709 and then Nkind (N) /= N_Private_Extension_Declaration
11711 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11712 & "from task interface", Error_Node);
11714 elsif not Is_Limited_Interface (Iface_Id) then
11715 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11716 & "from non-limited interface", Error_Node);
11719 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11720 -- synchronized or task interfaces.
11722 elsif Nkind (N) = N_Full_Type_Declaration
11723 and then Task_Present (Type_Def)
11725 if Limited_Present (Iface_Def)
11726 or else Synchronized_Present (Iface_Def)
11727 or else Task_Present (Iface_Def)
11731 elsif Protected_Present (Iface_Def) then
11732 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11733 & "protected interface", Error_Node);
11736 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11737 & "non-limited interface", Error_Node);
11742 -- Start of processing for Check_Interfaces
11745 if Is_Interface (Parent_Type) then
11746 if Is_Task_Interface (Parent_Type) then
11749 elsif Is_Protected_Interface (Parent_Type) then
11750 Is_Protected := True;
11754 if Nkind (N) = N_Private_Extension_Declaration then
11756 -- Check that progenitors are compatible with declaration
11758 Iface := First (Interface_List (Def));
11759 while Present (Iface) loop
11760 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11762 Parent_Node := Parent (Base_Type (Iface_Typ));
11763 Iface_Def := Type_Definition (Parent_Node);
11765 if not Is_Interface (Iface_Typ) then
11766 Diagnose_Interface (Iface, Iface_Typ);
11768 Check_Ifaces (Iface_Def, Iface);
11774 if Is_Task and Is_Protected then
11776 ("type cannot derive from task and protected interface", N);
11782 -- Full type declaration of derived type.
11783 -- Check compatibility with parent if it is interface type
11785 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11786 and then Is_Interface (Parent_Type)
11788 Parent_Node := Parent (Parent_Type);
11790 -- More detailed checks for interface varieties
11793 (Iface_Def => Type_Definition (Parent_Node),
11794 Error_Node => Subtype_Indication (Type_Definition (N)));
11797 Iface := First (Interface_List (Def));
11798 while Present (Iface) loop
11799 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11801 Parent_Node := Parent (Base_Type (Iface_Typ));
11802 Iface_Def := Type_Definition (Parent_Node);
11804 if not Is_Interface (Iface_Typ) then
11805 Diagnose_Interface (Iface, Iface_Typ);
11808 -- "The declaration of a specific descendant of an interface
11809 -- type freezes the interface type" RM 13.14
11811 Freeze_Before (N, Iface_Typ);
11812 Check_Ifaces (Iface_Def, Error_Node => Iface);
11818 if Is_Task and Is_Protected then
11820 ("type cannot derive from task and protected interface", N);
11822 end Check_Interfaces;
11824 ------------------------------------
11825 -- Check_Or_Process_Discriminants --
11826 ------------------------------------
11828 -- If an incomplete or private type declaration was already given for the
11829 -- type, the discriminants may have already been processed if they were
11830 -- present on the incomplete declaration. In this case a full conformance
11831 -- check has been performed in Find_Type_Name, and we then recheck here
11832 -- some properties that can't be checked on the partial view alone.
11833 -- Otherwise we call Process_Discriminants.
11835 procedure Check_Or_Process_Discriminants
11838 Prev : Entity_Id := Empty)
11841 if Has_Discriminants (T) then
11843 -- Discriminants are already set on T if they were already present
11844 -- on the partial view. Make them visible to component declarations.
11848 -- Discriminant on T (full view) referencing expr on partial view
11850 Prev_D : Entity_Id;
11851 -- Entity of corresponding discriminant on partial view
11854 -- Discriminant specification for full view, expression is
11855 -- the syntactic copy on full view (which has been checked for
11856 -- conformance with partial view), only used here to post error
11860 D := First_Discriminant (T);
11861 New_D := First (Discriminant_Specifications (N));
11862 while Present (D) loop
11863 Prev_D := Current_Entity (D);
11864 Set_Current_Entity (D);
11865 Set_Is_Immediately_Visible (D);
11866 Set_Homonym (D, Prev_D);
11868 -- Handle the case where there is an untagged partial view and
11869 -- the full view is tagged: must disallow discriminants with
11870 -- defaults, unless compiling for Ada 2012, which allows a
11871 -- limited tagged type to have defaulted discriminants (see
11872 -- AI05-0214). However, suppress error here if it was already
11873 -- reported on the default expression of the partial view.
11875 if Is_Tagged_Type (T)
11876 and then Present (Expression (Parent (D)))
11877 and then (not Is_Limited_Type (Current_Scope)
11878 or else Ada_Version < Ada_2012)
11879 and then not Error_Posted (Expression (Parent (D)))
11881 if Ada_Version >= Ada_2012 then
11883 ("discriminants of nonlimited tagged type cannot have "
11885 Expression (New_D));
11888 ("discriminants of tagged type cannot have defaults",
11889 Expression (New_D));
11893 -- Ada 2005 (AI-230): Access discriminant allowed in
11894 -- non-limited record types.
11896 if Ada_Version < Ada_2005 then
11898 -- This restriction gets applied to the full type here. It
11899 -- has already been applied earlier to the partial view.
11901 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11904 Next_Discriminant (D);
11909 elsif Present (Discriminant_Specifications (N)) then
11910 Process_Discriminants (N, Prev);
11912 end Check_Or_Process_Discriminants;
11914 ----------------------
11915 -- Check_Real_Bound --
11916 ----------------------
11918 procedure Check_Real_Bound (Bound : Node_Id) is
11920 if not Is_Real_Type (Etype (Bound)) then
11922 ("bound in real type definition must be of real type", Bound);
11924 elsif not Is_OK_Static_Expression (Bound) then
11925 Flag_Non_Static_Expr
11926 ("non-static expression used for real type bound!", Bound);
11933 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11935 Resolve (Bound, Standard_Float);
11936 end Check_Real_Bound;
11938 ------------------------------
11939 -- Complete_Private_Subtype --
11940 ------------------------------
11942 procedure Complete_Private_Subtype
11945 Full_Base : Entity_Id;
11946 Related_Nod : Node_Id)
11948 Save_Next_Entity : Entity_Id;
11949 Save_Homonym : Entity_Id;
11952 -- Set semantic attributes for (implicit) private subtype completion.
11953 -- If the full type has no discriminants, then it is a copy of the
11954 -- full view of the base. Otherwise, it is a subtype of the base with
11955 -- a possible discriminant constraint. Save and restore the original
11956 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11957 -- not corrupt the entity chain.
11959 -- Note that the type of the full view is the same entity as the type
11960 -- of the partial view. In this fashion, the subtype has access to the
11961 -- correct view of the parent.
11963 Save_Next_Entity := Next_Entity (Full);
11964 Save_Homonym := Homonym (Priv);
11966 case Ekind (Full_Base) is
11967 when Class_Wide_Kind
11974 Copy_Node (Priv, Full);
11976 Set_Has_Discriminants
11977 (Full, Has_Discriminants (Full_Base));
11978 Set_Has_Unknown_Discriminants
11979 (Full, Has_Unknown_Discriminants (Full_Base));
11980 Set_First_Entity (Full, First_Entity (Full_Base));
11981 Set_Last_Entity (Full, Last_Entity (Full_Base));
11983 -- If the underlying base type is constrained, we know that the
11984 -- full view of the subtype is constrained as well (the converse
11985 -- is not necessarily true).
11987 if Is_Constrained (Full_Base) then
11988 Set_Is_Constrained (Full);
11992 Copy_Node (Full_Base, Full);
11994 Set_Chars (Full, Chars (Priv));
11995 Conditional_Delay (Full, Priv);
11996 Set_Sloc (Full, Sloc (Priv));
11999 Set_Next_Entity (Full, Save_Next_Entity);
12000 Set_Homonym (Full, Save_Homonym);
12001 Set_Associated_Node_For_Itype (Full, Related_Nod);
12003 -- Set common attributes for all subtypes: kind, convention, etc.
12005 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12006 Set_Convention (Full, Convention (Full_Base));
12008 -- The Etype of the full view is inconsistent. Gigi needs to see the
12009 -- structural full view, which is what the current scheme gives: the
12010 -- Etype of the full view is the etype of the full base. However, if the
12011 -- full base is a derived type, the full view then looks like a subtype
12012 -- of the parent, not a subtype of the full base. If instead we write:
12014 -- Set_Etype (Full, Full_Base);
12016 -- then we get inconsistencies in the front-end (confusion between
12017 -- views). Several outstanding bugs are related to this ???
12019 Set_Is_First_Subtype (Full, False);
12020 Set_Scope (Full, Scope (Priv));
12021 Set_Size_Info (Full, Full_Base);
12022 Set_RM_Size (Full, RM_Size (Full_Base));
12023 Set_Is_Itype (Full);
12025 -- A subtype of a private-type-without-discriminants, whose full-view
12026 -- has discriminants with default expressions, is not constrained.
12028 if not Has_Discriminants (Priv) then
12029 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12031 if Has_Discriminants (Full_Base) then
12032 Set_Discriminant_Constraint
12033 (Full, Discriminant_Constraint (Full_Base));
12035 -- The partial view may have been indefinite, the full view
12038 Set_Has_Unknown_Discriminants
12039 (Full, Has_Unknown_Discriminants (Full_Base));
12043 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12044 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12046 -- Freeze the private subtype entity if its parent is delayed, and not
12047 -- already frozen. We skip this processing if the type is an anonymous
12048 -- subtype of a record component, or is the corresponding record of a
12049 -- protected type, since these are processed when the enclosing type
12050 -- is frozen. If the parent type is declared in a nested package then
12051 -- the freezing of the private and full views also happens later.
12053 if not Is_Type (Scope (Full)) then
12055 and then In_Same_Source_Unit (Full, Full_Base)
12056 and then Scope (Full_Base) /= Scope (Full)
12058 Set_Has_Delayed_Freeze (Full);
12059 Set_Has_Delayed_Freeze (Priv);
12062 Set_Has_Delayed_Freeze (Full,
12063 Has_Delayed_Freeze (Full_Base)
12064 and then not Is_Frozen (Full_Base));
12068 Set_Freeze_Node (Full, Empty);
12069 Set_Is_Frozen (Full, False);
12070 Set_Full_View (Priv, Full);
12072 if Has_Discriminants (Full) then
12073 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12074 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12076 if Has_Unknown_Discriminants (Full) then
12077 Set_Discriminant_Constraint (Full, No_Elist);
12081 if Ekind (Full_Base) = E_Record_Type
12082 and then Has_Discriminants (Full_Base)
12083 and then Has_Discriminants (Priv) -- might not, if errors
12084 and then not Has_Unknown_Discriminants (Priv)
12085 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12087 Create_Constrained_Components
12088 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12090 -- If the full base is itself derived from private, build a congruent
12091 -- subtype of its underlying type, for use by the back end. For a
12092 -- constrained record component, the declaration cannot be placed on
12093 -- the component list, but it must nevertheless be built an analyzed, to
12094 -- supply enough information for Gigi to compute the size of component.
12096 elsif Ekind (Full_Base) in Private_Kind
12097 and then Is_Derived_Type (Full_Base)
12098 and then Has_Discriminants (Full_Base)
12099 and then (Ekind (Current_Scope) /= E_Record_Subtype)
12101 if not Is_Itype (Priv)
12103 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
12105 Build_Underlying_Full_View
12106 (Parent (Priv), Full, Etype (Full_Base));
12108 elsif Nkind (Related_Nod) = N_Component_Declaration then
12109 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
12112 elsif Is_Record_Type (Full_Base) then
12114 -- Show Full is simply a renaming of Full_Base
12116 Set_Cloned_Subtype (Full, Full_Base);
12119 -- It is unsafe to share the bounds of a scalar type, because the Itype
12120 -- is elaborated on demand, and if a bound is non-static then different
12121 -- orders of elaboration in different units will lead to different
12122 -- external symbols.
12124 if Is_Scalar_Type (Full_Base) then
12125 Set_Scalar_Range (Full,
12126 Make_Range (Sloc (Related_Nod),
12128 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12130 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12132 -- This completion inherits the bounds of the full parent, but if
12133 -- the parent is an unconstrained floating point type, so is the
12136 if Is_Floating_Point_Type (Full_Base) then
12137 Set_Includes_Infinities
12138 (Scalar_Range (Full), Has_Infinities (Full_Base));
12142 -- ??? It seems that a lot of fields are missing that should be copied
12143 -- from Full_Base to Full. Here are some that are introduced in a
12144 -- non-disruptive way but a cleanup is necessary.
12146 if Is_Tagged_Type (Full_Base) then
12147 Set_Is_Tagged_Type (Full);
12148 Set_Direct_Primitive_Operations
12149 (Full, Direct_Primitive_Operations (Full_Base));
12150 Set_No_Tagged_Streams_Pragma
12151 (Full, No_Tagged_Streams_Pragma (Full_Base));
12153 -- Inherit class_wide type of full_base in case the partial view was
12154 -- not tagged. Otherwise it has already been created when the private
12155 -- subtype was analyzed.
12157 if No (Class_Wide_Type (Full)) then
12158 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12161 -- If this is a subtype of a protected or task type, constrain its
12162 -- corresponding record, unless this is a subtype without constraints,
12163 -- i.e. a simple renaming as with an actual subtype in an instance.
12165 elsif Is_Concurrent_Type (Full_Base) then
12166 if Has_Discriminants (Full)
12167 and then Present (Corresponding_Record_Type (Full_Base))
12169 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12171 Set_Corresponding_Record_Type (Full,
12172 Constrain_Corresponding_Record
12173 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12176 Set_Corresponding_Record_Type (Full,
12177 Corresponding_Record_Type (Full_Base));
12181 -- Link rep item chain, and also setting of Has_Predicates from private
12182 -- subtype to full subtype, since we will need these on the full subtype
12183 -- to create the predicate function. Note that the full subtype may
12184 -- already have rep items, inherited from the full view of the base
12185 -- type, so we must be sure not to overwrite these entries.
12190 Next_Item : Node_Id;
12191 Priv_Item : Node_Id;
12194 Item := First_Rep_Item (Full);
12195 Priv_Item := First_Rep_Item (Priv);
12197 -- If no existing rep items on full type, we can just link directly
12198 -- to the list of items on the private type, if any exist.. Same if
12199 -- the rep items are only those inherited from the base
12202 or else Nkind (Item) /= N_Aspect_Specification
12203 or else Entity (Item) = Full_Base)
12204 and then Present (First_Rep_Item (Priv))
12206 Set_First_Rep_Item (Full, Priv_Item);
12208 -- Otherwise, search to the end of items currently linked to the full
12209 -- subtype and append the private items to the end. However, if Priv
12210 -- and Full already have the same list of rep items, then the append
12211 -- is not done, as that would create a circularity.
12213 -- The partial view may have a predicate and the rep item lists of
12214 -- both views agree when inherited from the same ancestor. In that
12215 -- case, simply propagate the list from one view to the other.
12216 -- A more complex analysis needed here ???
12218 elsif Present (Priv_Item)
12219 and then Item = Next_Rep_Item (Priv_Item)
12221 Set_First_Rep_Item (Full, Priv_Item);
12223 elsif Item /= Priv_Item then
12226 Next_Item := Next_Rep_Item (Item);
12227 exit when No (Next_Item);
12230 -- If the private view has aspect specifications, the full view
12231 -- inherits them. Since these aspects may already have been
12232 -- attached to the full view during derivation, do not append
12233 -- them if already present.
12235 if Item = First_Rep_Item (Priv) then
12241 -- And link the private type items at the end of the chain
12244 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12249 -- Make sure Has_Predicates is set on full type if it is set on the
12250 -- private type. Note that it may already be set on the full type and
12251 -- if so, we don't want to unset it. Similarly, propagate information
12252 -- about delayed aspects, because the corresponding pragmas must be
12253 -- analyzed when one of the views is frozen. This last step is needed
12254 -- in particular when the full type is a scalar type for which an
12255 -- anonymous base type is constructed.
12257 -- The predicate functions are generated either at the freeze point
12258 -- of the type or at the end of the visible part, and we must avoid
12259 -- generating them twice.
12261 if Has_Predicates (Priv) then
12262 Set_Has_Predicates (Full);
12264 if Present (Predicate_Function (Priv))
12265 and then No (Predicate_Function (Full))
12267 Set_Predicate_Function (Full, Predicate_Function (Priv));
12271 if Has_Delayed_Aspects (Priv) then
12272 Set_Has_Delayed_Aspects (Full);
12274 end Complete_Private_Subtype;
12276 ----------------------------
12277 -- Constant_Redeclaration --
12278 ----------------------------
12280 procedure Constant_Redeclaration
12285 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12286 Obj_Def : constant Node_Id := Object_Definition (N);
12289 procedure Check_Possible_Deferred_Completion
12290 (Prev_Id : Entity_Id;
12291 Prev_Obj_Def : Node_Id;
12292 Curr_Obj_Def : Node_Id);
12293 -- Determine whether the two object definitions describe the partial
12294 -- and the full view of a constrained deferred constant. Generate
12295 -- a subtype for the full view and verify that it statically matches
12296 -- the subtype of the partial view.
12298 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12299 -- If deferred constant is an access type initialized with an allocator,
12300 -- check whether there is an illegal recursion in the definition,
12301 -- through a default value of some record subcomponent. This is normally
12302 -- detected when generating init procs, but requires this additional
12303 -- mechanism when expansion is disabled.
12305 ----------------------------------------
12306 -- Check_Possible_Deferred_Completion --
12307 ----------------------------------------
12309 procedure Check_Possible_Deferred_Completion
12310 (Prev_Id : Entity_Id;
12311 Prev_Obj_Def : Node_Id;
12312 Curr_Obj_Def : Node_Id)
12315 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12316 and then Present (Constraint (Prev_Obj_Def))
12317 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12318 and then Present (Constraint (Curr_Obj_Def))
12321 Loc : constant Source_Ptr := Sloc (N);
12322 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12323 Decl : constant Node_Id :=
12324 Make_Subtype_Declaration (Loc,
12325 Defining_Identifier => Def_Id,
12326 Subtype_Indication =>
12327 Relocate_Node (Curr_Obj_Def));
12330 Insert_Before_And_Analyze (N, Decl);
12331 Set_Etype (Id, Def_Id);
12333 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12334 Error_Msg_Sloc := Sloc (Prev_Id);
12335 Error_Msg_N ("subtype does not statically match deferred "
12336 & "declaration #", N);
12340 end Check_Possible_Deferred_Completion;
12342 ---------------------------------
12343 -- Check_Recursive_Declaration --
12344 ---------------------------------
12346 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12350 if Is_Record_Type (Typ) then
12351 Comp := First_Component (Typ);
12352 while Present (Comp) loop
12353 if Comes_From_Source (Comp) then
12354 if Present (Expression (Parent (Comp)))
12355 and then Is_Entity_Name (Expression (Parent (Comp)))
12356 and then Entity (Expression (Parent (Comp))) = Prev
12358 Error_Msg_Sloc := Sloc (Parent (Comp));
12360 ("illegal circularity with declaration for & #",
12364 elsif Is_Record_Type (Etype (Comp)) then
12365 Check_Recursive_Declaration (Etype (Comp));
12369 Next_Component (Comp);
12372 end Check_Recursive_Declaration;
12374 -- Start of processing for Constant_Redeclaration
12377 if Nkind (Parent (Prev)) = N_Object_Declaration then
12378 if Nkind (Object_Definition
12379 (Parent (Prev))) = N_Subtype_Indication
12381 -- Find type of new declaration. The constraints of the two
12382 -- views must match statically, but there is no point in
12383 -- creating an itype for the full view.
12385 if Nkind (Obj_Def) = N_Subtype_Indication then
12386 Find_Type (Subtype_Mark (Obj_Def));
12387 New_T := Entity (Subtype_Mark (Obj_Def));
12390 Find_Type (Obj_Def);
12391 New_T := Entity (Obj_Def);
12397 -- The full view may impose a constraint, even if the partial
12398 -- view does not, so construct the subtype.
12400 New_T := Find_Type_Of_Object (Obj_Def, N);
12405 -- Current declaration is illegal, diagnosed below in Enter_Name
12411 -- If previous full declaration or a renaming declaration exists, or if
12412 -- a homograph is present, let Enter_Name handle it, either with an
12413 -- error or with the removal of an overridden implicit subprogram.
12414 -- The previous one is a full declaration if it has an expression
12415 -- (which in the case of an aggregate is indicated by the Init flag).
12417 if Ekind (Prev) /= E_Constant
12418 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12419 or else Present (Expression (Parent (Prev)))
12420 or else Has_Init_Expression (Parent (Prev))
12421 or else Present (Full_View (Prev))
12425 -- Verify that types of both declarations match, or else that both types
12426 -- are anonymous access types whose designated subtypes statically match
12427 -- (as allowed in Ada 2005 by AI-385).
12429 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12431 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12432 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12433 or else Is_Access_Constant (Etype (New_T)) /=
12434 Is_Access_Constant (Etype (Prev))
12435 or else Can_Never_Be_Null (Etype (New_T)) /=
12436 Can_Never_Be_Null (Etype (Prev))
12437 or else Null_Exclusion_Present (Parent (Prev)) /=
12438 Null_Exclusion_Present (Parent (Id))
12439 or else not Subtypes_Statically_Match
12440 (Designated_Type (Etype (Prev)),
12441 Designated_Type (Etype (New_T))))
12443 Error_Msg_Sloc := Sloc (Prev);
12444 Error_Msg_N ("type does not match declaration#", N);
12445 Set_Full_View (Prev, Id);
12446 Set_Etype (Id, Any_Type);
12448 -- A deferred constant whose type is an anonymous array is always
12449 -- illegal (unless imported). A detailed error message might be
12450 -- helpful for Ada beginners.
12452 if Nkind (Object_Definition (Parent (Prev)))
12453 = N_Constrained_Array_Definition
12454 and then Nkind (Object_Definition (N))
12455 = N_Constrained_Array_Definition
12457 Error_Msg_N ("\each anonymous array is a distinct type", N);
12458 Error_Msg_N ("a deferred constant must have a named type",
12459 Object_Definition (Parent (Prev)));
12463 Null_Exclusion_Present (Parent (Prev))
12464 and then not Null_Exclusion_Present (N)
12466 Error_Msg_Sloc := Sloc (Prev);
12467 Error_Msg_N ("null-exclusion does not match declaration#", N);
12468 Set_Full_View (Prev, Id);
12469 Set_Etype (Id, Any_Type);
12471 -- If so, process the full constant declaration
12474 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12475 -- the deferred declaration is constrained, then the subtype defined
12476 -- by the subtype_indication in the full declaration shall match it
12479 Check_Possible_Deferred_Completion
12481 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12482 Curr_Obj_Def => Obj_Def);
12484 Set_Full_View (Prev, Id);
12485 Set_Is_Public (Id, Is_Public (Prev));
12486 Set_Is_Internal (Id);
12487 Append_Entity (Id, Current_Scope);
12489 -- Check ALIASED present if present before (RM 7.4(7))
12491 if Is_Aliased (Prev)
12492 and then not Aliased_Present (N)
12494 Error_Msg_Sloc := Sloc (Prev);
12495 Error_Msg_N ("ALIASED required (see declaration #)", N);
12498 -- Check that placement is in private part and that the incomplete
12499 -- declaration appeared in the visible part.
12501 if Ekind (Current_Scope) = E_Package
12502 and then not In_Private_Part (Current_Scope)
12504 Error_Msg_Sloc := Sloc (Prev);
12506 ("full constant for declaration # must be in private part", N);
12508 elsif Ekind (Current_Scope) = E_Package
12510 List_Containing (Parent (Prev)) /=
12511 Visible_Declarations (Package_Specification (Current_Scope))
12514 ("deferred constant must be declared in visible part",
12518 if Is_Access_Type (T)
12519 and then Nkind (Expression (N)) = N_Allocator
12521 Check_Recursive_Declaration (Designated_Type (T));
12524 -- A deferred constant is a visible entity. If type has invariants,
12525 -- verify that the initial value satisfies them.
12527 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12529 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12532 end Constant_Redeclaration;
12534 ----------------------
12535 -- Constrain_Access --
12536 ----------------------
12538 procedure Constrain_Access
12539 (Def_Id : in out Entity_Id;
12541 Related_Nod : Node_Id)
12543 T : constant Entity_Id := Entity (Subtype_Mark (S));
12544 Desig_Type : constant Entity_Id := Designated_Type (T);
12545 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12546 Constraint_OK : Boolean := True;
12549 if Is_Array_Type (Desig_Type) then
12550 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12552 elsif (Is_Record_Type (Desig_Type)
12553 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12554 and then not Is_Constrained (Desig_Type)
12556 -- ??? The following code is a temporary bypass to ignore a
12557 -- discriminant constraint on access type if it is constraining
12558 -- the current record. Avoid creating the implicit subtype of the
12559 -- record we are currently compiling since right now, we cannot
12560 -- handle these. For now, just return the access type itself.
12562 if Desig_Type = Current_Scope
12563 and then No (Def_Id)
12565 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12566 Def_Id := Entity (Subtype_Mark (S));
12568 -- This call added to ensure that the constraint is analyzed
12569 -- (needed for a B test). Note that we still return early from
12570 -- this procedure to avoid recursive processing. ???
12572 Constrain_Discriminated_Type
12573 (Desig_Subtype, S, Related_Nod, For_Access => True);
12577 -- Enforce rule that the constraint is illegal if there is an
12578 -- unconstrained view of the designated type. This means that the
12579 -- partial view (either a private type declaration or a derivation
12580 -- from a private type) has no discriminants. (Defect Report
12581 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12583 -- Rule updated for Ada 2005: The private type is said to have
12584 -- a constrained partial view, given that objects of the type
12585 -- can be declared. Furthermore, the rule applies to all access
12586 -- types, unlike the rule concerning default discriminants (see
12589 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12590 and then Has_Private_Declaration (Desig_Type)
12591 and then In_Open_Scopes (Scope (Desig_Type))
12592 and then Has_Discriminants (Desig_Type)
12595 Pack : constant Node_Id :=
12596 Unit_Declaration_Node (Scope (Desig_Type));
12601 if Nkind (Pack) = N_Package_Declaration then
12602 Decls := Visible_Declarations (Specification (Pack));
12603 Decl := First (Decls);
12604 while Present (Decl) loop
12605 if (Nkind (Decl) = N_Private_Type_Declaration
12606 and then Chars (Defining_Identifier (Decl)) =
12607 Chars (Desig_Type))
12610 (Nkind (Decl) = N_Full_Type_Declaration
12612 Chars (Defining_Identifier (Decl)) =
12614 and then Is_Derived_Type (Desig_Type)
12616 Has_Private_Declaration (Etype (Desig_Type)))
12618 if No (Discriminant_Specifications (Decl)) then
12620 ("cannot constrain access type if designated "
12621 & "type has constrained partial view", S);
12633 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12634 For_Access => True);
12636 elsif Is_Concurrent_Type (Desig_Type)
12637 and then not Is_Constrained (Desig_Type)
12639 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12642 Error_Msg_N ("invalid constraint on access type", S);
12644 -- We simply ignore an invalid constraint
12646 Desig_Subtype := Desig_Type;
12647 Constraint_OK := False;
12650 if No (Def_Id) then
12651 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12653 Set_Ekind (Def_Id, E_Access_Subtype);
12656 if Constraint_OK then
12657 Set_Etype (Def_Id, Base_Type (T));
12659 if Is_Private_Type (Desig_Type) then
12660 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12663 Set_Etype (Def_Id, Any_Type);
12666 Set_Size_Info (Def_Id, T);
12667 Set_Is_Constrained (Def_Id, Constraint_OK);
12668 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12669 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12670 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12672 Conditional_Delay (Def_Id, T);
12674 -- AI-363 : Subtypes of general access types whose designated types have
12675 -- default discriminants are disallowed. In instances, the rule has to
12676 -- be checked against the actual, of which T is the subtype. In a
12677 -- generic body, the rule is checked assuming that the actual type has
12678 -- defaulted discriminants.
12680 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12681 if Ekind (Base_Type (T)) = E_General_Access_Type
12682 and then Has_Defaulted_Discriminants (Desig_Type)
12684 if Ada_Version < Ada_2005 then
12686 ("access subtype of general access type would not " &
12687 "be allowed in Ada 2005?y?", S);
12690 ("access subtype of general access type not allowed", S);
12693 Error_Msg_N ("\discriminants have defaults", S);
12695 elsif Is_Access_Type (T)
12696 and then Is_Generic_Type (Desig_Type)
12697 and then Has_Discriminants (Desig_Type)
12698 and then In_Package_Body (Current_Scope)
12700 if Ada_Version < Ada_2005 then
12702 ("access subtype would not be allowed in generic body "
12703 & "in Ada 2005?y?", S);
12706 ("access subtype not allowed in generic body", S);
12710 ("\designated type is a discriminated formal", S);
12713 end Constrain_Access;
12715 ---------------------
12716 -- Constrain_Array --
12717 ---------------------
12719 procedure Constrain_Array
12720 (Def_Id : in out Entity_Id;
12722 Related_Nod : Node_Id;
12723 Related_Id : Entity_Id;
12724 Suffix : Character)
12726 C : constant Node_Id := Constraint (SI);
12727 Number_Of_Constraints : Nat := 0;
12730 Constraint_OK : Boolean := True;
12733 T := Entity (Subtype_Mark (SI));
12735 if Is_Access_Type (T) then
12736 T := Designated_Type (T);
12739 -- If an index constraint follows a subtype mark in a subtype indication
12740 -- then the type or subtype denoted by the subtype mark must not already
12741 -- impose an index constraint. The subtype mark must denote either an
12742 -- unconstrained array type or an access type whose designated type
12743 -- is such an array type... (RM 3.6.1)
12745 if Is_Constrained (T) then
12746 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12747 Constraint_OK := False;
12750 S := First (Constraints (C));
12751 while Present (S) loop
12752 Number_Of_Constraints := Number_Of_Constraints + 1;
12756 -- In either case, the index constraint must provide a discrete
12757 -- range for each index of the array type and the type of each
12758 -- discrete range must be the same as that of the corresponding
12759 -- index. (RM 3.6.1)
12761 if Number_Of_Constraints /= Number_Dimensions (T) then
12762 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12763 Constraint_OK := False;
12766 S := First (Constraints (C));
12767 Index := First_Index (T);
12770 -- Apply constraints to each index type
12772 for J in 1 .. Number_Of_Constraints loop
12773 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12781 if No (Def_Id) then
12783 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12784 Set_Parent (Def_Id, Related_Nod);
12787 Set_Ekind (Def_Id, E_Array_Subtype);
12790 Set_Size_Info (Def_Id, (T));
12791 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12792 Set_Etype (Def_Id, Base_Type (T));
12794 if Constraint_OK then
12795 Set_First_Index (Def_Id, First (Constraints (C)));
12797 Set_First_Index (Def_Id, First_Index (T));
12800 Set_Is_Constrained (Def_Id, True);
12801 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12802 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12804 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12805 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12807 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12808 -- We need to initialize the attribute because if Def_Id is previously
12809 -- analyzed through a limited_with clause, it will have the attributes
12810 -- of an incomplete type, one of which is an Elist that overlaps the
12811 -- Packed_Array_Impl_Type field.
12813 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12815 -- Build a freeze node if parent still needs one. Also make sure that
12816 -- the Depends_On_Private status is set because the subtype will need
12817 -- reprocessing at the time the base type does, and also we must set a
12818 -- conditional delay.
12820 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12821 Conditional_Delay (Def_Id, T);
12822 end Constrain_Array;
12824 ------------------------------
12825 -- Constrain_Component_Type --
12826 ------------------------------
12828 function Constrain_Component_Type
12830 Constrained_Typ : Entity_Id;
12831 Related_Node : Node_Id;
12833 Constraints : Elist_Id) return Entity_Id
12835 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12836 Compon_Type : constant Entity_Id := Etype (Comp);
12838 function Build_Constrained_Array_Type
12839 (Old_Type : Entity_Id) return Entity_Id;
12840 -- If Old_Type is an array type, one of whose indexes is constrained
12841 -- by a discriminant, build an Itype whose constraint replaces the
12842 -- discriminant with its value in the constraint.
12844 function Build_Constrained_Discriminated_Type
12845 (Old_Type : Entity_Id) return Entity_Id;
12846 -- Ditto for record components
12848 function Build_Constrained_Access_Type
12849 (Old_Type : Entity_Id) return Entity_Id;
12850 -- Ditto for access types. Makes use of previous two functions, to
12851 -- constrain designated type.
12853 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12854 -- T is an array or discriminated type, C is a list of constraints
12855 -- that apply to T. This routine builds the constrained subtype.
12857 function Is_Discriminant (Expr : Node_Id) return Boolean;
12858 -- Returns True if Expr is a discriminant
12860 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12861 -- Find the value of discriminant Discrim in Constraint
12863 -----------------------------------
12864 -- Build_Constrained_Access_Type --
12865 -----------------------------------
12867 function Build_Constrained_Access_Type
12868 (Old_Type : Entity_Id) return Entity_Id
12870 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12872 Desig_Subtype : Entity_Id;
12876 -- if the original access type was not embedded in the enclosing
12877 -- type definition, there is no need to produce a new access
12878 -- subtype. In fact every access type with an explicit constraint
12879 -- generates an itype whose scope is the enclosing record.
12881 if not Is_Type (Scope (Old_Type)) then
12884 elsif Is_Array_Type (Desig_Type) then
12885 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12887 elsif Has_Discriminants (Desig_Type) then
12889 -- This may be an access type to an enclosing record type for
12890 -- which we are constructing the constrained components. Return
12891 -- the enclosing record subtype. This is not always correct,
12892 -- but avoids infinite recursion. ???
12894 Desig_Subtype := Any_Type;
12896 for J in reverse 0 .. Scope_Stack.Last loop
12897 Scop := Scope_Stack.Table (J).Entity;
12900 and then Base_Type (Scop) = Base_Type (Desig_Type)
12902 Desig_Subtype := Scop;
12905 exit when not Is_Type (Scop);
12908 if Desig_Subtype = Any_Type then
12910 Build_Constrained_Discriminated_Type (Desig_Type);
12917 if Desig_Subtype /= Desig_Type then
12919 -- The Related_Node better be here or else we won't be able
12920 -- to attach new itypes to a node in the tree.
12922 pragma Assert (Present (Related_Node));
12924 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12926 Set_Etype (Itype, Base_Type (Old_Type));
12927 Set_Size_Info (Itype, (Old_Type));
12928 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12929 Set_Depends_On_Private (Itype, Has_Private_Component
12931 Set_Is_Access_Constant (Itype, Is_Access_Constant
12934 -- The new itype needs freezing when it depends on a not frozen
12935 -- type and the enclosing subtype needs freezing.
12937 if Has_Delayed_Freeze (Constrained_Typ)
12938 and then not Is_Frozen (Constrained_Typ)
12940 Conditional_Delay (Itype, Base_Type (Old_Type));
12948 end Build_Constrained_Access_Type;
12950 ----------------------------------
12951 -- Build_Constrained_Array_Type --
12952 ----------------------------------
12954 function Build_Constrained_Array_Type
12955 (Old_Type : Entity_Id) return Entity_Id
12959 Old_Index : Node_Id;
12960 Range_Node : Node_Id;
12961 Constr_List : List_Id;
12963 Need_To_Create_Itype : Boolean := False;
12966 Old_Index := First_Index (Old_Type);
12967 while Present (Old_Index) loop
12968 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12970 if Is_Discriminant (Lo_Expr)
12972 Is_Discriminant (Hi_Expr)
12974 Need_To_Create_Itype := True;
12977 Next_Index (Old_Index);
12980 if Need_To_Create_Itype then
12981 Constr_List := New_List;
12983 Old_Index := First_Index (Old_Type);
12984 while Present (Old_Index) loop
12985 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12987 if Is_Discriminant (Lo_Expr) then
12988 Lo_Expr := Get_Discr_Value (Lo_Expr);
12991 if Is_Discriminant (Hi_Expr) then
12992 Hi_Expr := Get_Discr_Value (Hi_Expr);
12997 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12999 Append (Range_Node, To => Constr_List);
13001 Next_Index (Old_Index);
13004 return Build_Subtype (Old_Type, Constr_List);
13009 end Build_Constrained_Array_Type;
13011 ------------------------------------------
13012 -- Build_Constrained_Discriminated_Type --
13013 ------------------------------------------
13015 function Build_Constrained_Discriminated_Type
13016 (Old_Type : Entity_Id) return Entity_Id
13019 Constr_List : List_Id;
13020 Old_Constraint : Elmt_Id;
13022 Need_To_Create_Itype : Boolean := False;
13025 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13026 while Present (Old_Constraint) loop
13027 Expr := Node (Old_Constraint);
13029 if Is_Discriminant (Expr) then
13030 Need_To_Create_Itype := True;
13033 Next_Elmt (Old_Constraint);
13036 if Need_To_Create_Itype then
13037 Constr_List := New_List;
13039 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13040 while Present (Old_Constraint) loop
13041 Expr := Node (Old_Constraint);
13043 if Is_Discriminant (Expr) then
13044 Expr := Get_Discr_Value (Expr);
13047 Append (New_Copy_Tree (Expr), To => Constr_List);
13049 Next_Elmt (Old_Constraint);
13052 return Build_Subtype (Old_Type, Constr_List);
13057 end Build_Constrained_Discriminated_Type;
13059 -------------------
13060 -- Build_Subtype --
13061 -------------------
13063 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
13065 Subtyp_Decl : Node_Id;
13066 Def_Id : Entity_Id;
13067 Btyp : Entity_Id := Base_Type (T);
13070 -- The Related_Node better be here or else we won't be able to
13071 -- attach new itypes to a node in the tree.
13073 pragma Assert (Present (Related_Node));
13075 -- If the view of the component's type is incomplete or private
13076 -- with unknown discriminants, then the constraint must be applied
13077 -- to the full type.
13079 if Has_Unknown_Discriminants (Btyp)
13080 and then Present (Underlying_Type (Btyp))
13082 Btyp := Underlying_Type (Btyp);
13086 Make_Subtype_Indication (Loc,
13087 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13088 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13090 Def_Id := Create_Itype (Ekind (T), Related_Node);
13093 Make_Subtype_Declaration (Loc,
13094 Defining_Identifier => Def_Id,
13095 Subtype_Indication => Indic);
13097 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13099 -- Itypes must be analyzed with checks off (see package Itypes)
13101 Analyze (Subtyp_Decl, Suppress => All_Checks);
13106 ---------------------
13107 -- Get_Discr_Value --
13108 ---------------------
13110 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13115 -- The discriminant may be declared for the type, in which case we
13116 -- find it by iterating over the list of discriminants. If the
13117 -- discriminant is inherited from a parent type, it appears as the
13118 -- corresponding discriminant of the current type. This will be the
13119 -- case when constraining an inherited component whose constraint is
13120 -- given by a discriminant of the parent.
13122 D := First_Discriminant (Typ);
13123 E := First_Elmt (Constraints);
13125 while Present (D) loop
13126 if D = Entity (Discrim)
13127 or else D = CR_Discriminant (Entity (Discrim))
13128 or else Corresponding_Discriminant (D) = Entity (Discrim)
13133 Next_Discriminant (D);
13137 -- The Corresponding_Discriminant mechanism is incomplete, because
13138 -- the correspondence between new and old discriminants is not one
13139 -- to one: one new discriminant can constrain several old ones. In
13140 -- that case, scan sequentially the stored_constraint, the list of
13141 -- discriminants of the parents, and the constraints.
13143 -- Previous code checked for the present of the Stored_Constraint
13144 -- list for the derived type, but did not use it at all. Should it
13145 -- be present when the component is a discriminated task type?
13147 if Is_Derived_Type (Typ)
13148 and then Scope (Entity (Discrim)) = Etype (Typ)
13150 D := First_Discriminant (Etype (Typ));
13151 E := First_Elmt (Constraints);
13152 while Present (D) loop
13153 if D = Entity (Discrim) then
13157 Next_Discriminant (D);
13162 -- Something is wrong if we did not find the value
13164 raise Program_Error;
13165 end Get_Discr_Value;
13167 ---------------------
13168 -- Is_Discriminant --
13169 ---------------------
13171 function Is_Discriminant (Expr : Node_Id) return Boolean is
13172 Discrim_Scope : Entity_Id;
13175 if Denotes_Discriminant (Expr) then
13176 Discrim_Scope := Scope (Entity (Expr));
13178 -- Either we have a reference to one of Typ's discriminants,
13180 pragma Assert (Discrim_Scope = Typ
13182 -- or to the discriminants of the parent type, in the case
13183 -- of a derivation of a tagged type with variants.
13185 or else Discrim_Scope = Etype (Typ)
13186 or else Full_View (Discrim_Scope) = Etype (Typ)
13188 -- or same as above for the case where the discriminants
13189 -- were declared in Typ's private view.
13191 or else (Is_Private_Type (Discrim_Scope)
13192 and then Chars (Discrim_Scope) = Chars (Typ))
13194 -- or else we are deriving from the full view and the
13195 -- discriminant is declared in the private entity.
13197 or else (Is_Private_Type (Typ)
13198 and then Chars (Discrim_Scope) = Chars (Typ))
13200 -- Or we are constrained the corresponding record of a
13201 -- synchronized type that completes a private declaration.
13203 or else (Is_Concurrent_Record_Type (Typ)
13205 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13207 -- or we have a class-wide type, in which case make sure the
13208 -- discriminant found belongs to the root type.
13210 or else (Is_Class_Wide_Type (Typ)
13211 and then Etype (Typ) = Discrim_Scope));
13216 -- In all other cases we have something wrong
13219 end Is_Discriminant;
13221 -- Start of processing for Constrain_Component_Type
13224 if Nkind (Parent (Comp)) = N_Component_Declaration
13225 and then Comes_From_Source (Parent (Comp))
13226 and then Comes_From_Source
13227 (Subtype_Indication (Component_Definition (Parent (Comp))))
13230 (Subtype_Indication (Component_Definition (Parent (Comp))))
13232 return Compon_Type;
13234 elsif Is_Array_Type (Compon_Type) then
13235 return Build_Constrained_Array_Type (Compon_Type);
13237 elsif Has_Discriminants (Compon_Type) then
13238 return Build_Constrained_Discriminated_Type (Compon_Type);
13240 elsif Is_Access_Type (Compon_Type) then
13241 return Build_Constrained_Access_Type (Compon_Type);
13244 return Compon_Type;
13246 end Constrain_Component_Type;
13248 --------------------------
13249 -- Constrain_Concurrent --
13250 --------------------------
13252 -- For concurrent types, the associated record value type carries the same
13253 -- discriminants, so when we constrain a concurrent type, we must constrain
13254 -- the corresponding record type as well.
13256 procedure Constrain_Concurrent
13257 (Def_Id : in out Entity_Id;
13259 Related_Nod : Node_Id;
13260 Related_Id : Entity_Id;
13261 Suffix : Character)
13263 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13264 -- case of a private subtype (needed when only doing semantic analysis).
13266 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13270 if Is_Access_Type (T_Ent) then
13271 T_Ent := Designated_Type (T_Ent);
13274 T_Val := Corresponding_Record_Type (T_Ent);
13276 if Present (T_Val) then
13278 if No (Def_Id) then
13279 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13281 -- Elaborate itype now, as it may be used in a subsequent
13282 -- synchronized operation in another scope.
13284 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13285 Build_Itype_Reference (Def_Id, Related_Nod);
13289 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13291 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13292 Set_Corresponding_Record_Type (Def_Id,
13293 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13296 -- If there is no associated record, expansion is disabled and this
13297 -- is a generic context. Create a subtype in any case, so that
13298 -- semantic analysis can proceed.
13300 if No (Def_Id) then
13301 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13304 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13306 end Constrain_Concurrent;
13308 ------------------------------------
13309 -- Constrain_Corresponding_Record --
13310 ------------------------------------
13312 function Constrain_Corresponding_Record
13313 (Prot_Subt : Entity_Id;
13314 Corr_Rec : Entity_Id;
13315 Related_Nod : Node_Id) return Entity_Id
13317 T_Sub : constant Entity_Id :=
13318 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
13321 Set_Etype (T_Sub, Corr_Rec);
13322 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13323 Set_Is_Constrained (T_Sub, True);
13324 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13325 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13327 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13328 Set_Discriminant_Constraint
13329 (T_Sub, Discriminant_Constraint (Prot_Subt));
13330 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13331 Create_Constrained_Components
13332 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13335 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13337 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13338 Conditional_Delay (T_Sub, Corr_Rec);
13341 -- This is a component subtype: it will be frozen in the context of
13342 -- the enclosing record's init_proc, so that discriminant references
13343 -- are resolved to discriminals. (Note: we used to skip freezing
13344 -- altogether in that case, which caused errors downstream for
13345 -- components of a bit packed array type).
13347 Set_Has_Delayed_Freeze (T_Sub);
13351 end Constrain_Corresponding_Record;
13353 -----------------------
13354 -- Constrain_Decimal --
13355 -----------------------
13357 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13358 T : constant Entity_Id := Entity (Subtype_Mark (S));
13359 C : constant Node_Id := Constraint (S);
13360 Loc : constant Source_Ptr := Sloc (C);
13361 Range_Expr : Node_Id;
13362 Digits_Expr : Node_Id;
13367 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13369 if Nkind (C) = N_Range_Constraint then
13370 Range_Expr := Range_Expression (C);
13371 Digits_Val := Digits_Value (T);
13374 pragma Assert (Nkind (C) = N_Digits_Constraint);
13376 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13378 Digits_Expr := Digits_Expression (C);
13379 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13381 Check_Digits_Expression (Digits_Expr);
13382 Digits_Val := Expr_Value (Digits_Expr);
13384 if Digits_Val > Digits_Value (T) then
13386 ("digits expression is incompatible with subtype", C);
13387 Digits_Val := Digits_Value (T);
13390 if Present (Range_Constraint (C)) then
13391 Range_Expr := Range_Expression (Range_Constraint (C));
13393 Range_Expr := Empty;
13397 Set_Etype (Def_Id, Base_Type (T));
13398 Set_Size_Info (Def_Id, (T));
13399 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13400 Set_Delta_Value (Def_Id, Delta_Value (T));
13401 Set_Scale_Value (Def_Id, Scale_Value (T));
13402 Set_Small_Value (Def_Id, Small_Value (T));
13403 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13404 Set_Digits_Value (Def_Id, Digits_Val);
13406 -- Manufacture range from given digits value if no range present
13408 if No (Range_Expr) then
13409 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13413 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13415 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13418 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13419 Set_Discrete_RM_Size (Def_Id);
13421 -- Unconditionally delay the freeze, since we cannot set size
13422 -- information in all cases correctly until the freeze point.
13424 Set_Has_Delayed_Freeze (Def_Id);
13425 end Constrain_Decimal;
13427 ----------------------------------
13428 -- Constrain_Discriminated_Type --
13429 ----------------------------------
13431 procedure Constrain_Discriminated_Type
13432 (Def_Id : Entity_Id;
13434 Related_Nod : Node_Id;
13435 For_Access : Boolean := False)
13437 E : Entity_Id := Entity (Subtype_Mark (S));
13440 procedure Fixup_Bad_Constraint;
13441 -- Called after finding a bad constraint, and after having posted an
13442 -- appropriate error message. The goal is to leave type Def_Id in as
13443 -- reasonable state as possible.
13445 --------------------------
13446 -- Fixup_Bad_Constraint --
13447 --------------------------
13449 procedure Fixup_Bad_Constraint is
13451 -- Set a reasonable Ekind for the entity. For an incomplete type,
13452 -- we can't do much, but for other types, we can set the proper
13453 -- corresponding subtype kind.
13455 if Ekind (T) = E_Incomplete_Type then
13456 Set_Ekind (Def_Id, Ekind (T));
13458 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13461 -- Set Etype to the known type, to reduce chances of cascaded errors
13463 Set_Etype (Def_Id, E);
13464 Set_Error_Posted (Def_Id);
13465 end Fixup_Bad_Constraint;
13470 Constr : Elist_Id := New_Elmt_List;
13472 -- Start of processing for Constrain_Discriminated_Type
13475 C := Constraint (S);
13477 -- A discriminant constraint is only allowed in a subtype indication,
13478 -- after a subtype mark. This subtype mark must denote either a type
13479 -- with discriminants, or an access type whose designated type is a
13480 -- type with discriminants. A discriminant constraint specifies the
13481 -- values of these discriminants (RM 3.7.2(5)).
13483 T := Base_Type (Entity (Subtype_Mark (S)));
13485 if Is_Access_Type (T) then
13486 T := Designated_Type (T);
13489 -- In an instance it may be necessary to retrieve the full view of a
13490 -- type with unknown discriminants, or a full view with defaulted
13491 -- discriminants. In other contexts the constraint is illegal.
13494 and then Is_Private_Type (T)
13495 and then Present (Full_View (T))
13497 (Has_Unknown_Discriminants (T)
13499 (not Has_Discriminants (T)
13500 and then Has_Discriminants (Full_View (T))
13501 and then Present (Discriminant_Default_Value
13502 (First_Discriminant (Full_View (T))))))
13504 T := Full_View (T);
13505 E := Full_View (E);
13508 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13509 -- generating an error for access-to-incomplete subtypes.
13511 if Ada_Version >= Ada_2005
13512 and then Ekind (T) = E_Incomplete_Type
13513 and then Nkind (Parent (S)) = N_Subtype_Declaration
13514 and then not Is_Itype (Def_Id)
13516 -- A little sanity check: emit an error message if the type has
13517 -- discriminants to begin with. Type T may be a regular incomplete
13518 -- type or imported via a limited with clause.
13520 if Has_Discriminants (T)
13521 or else (From_Limited_With (T)
13522 and then Present (Non_Limited_View (T))
13523 and then Nkind (Parent (Non_Limited_View (T))) =
13524 N_Full_Type_Declaration
13525 and then Present (Discriminant_Specifications
13526 (Parent (Non_Limited_View (T)))))
13529 ("(Ada 2005) incomplete subtype may not be constrained", C);
13531 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13534 Fixup_Bad_Constraint;
13537 -- Check that the type has visible discriminants. The type may be
13538 -- a private type with unknown discriminants whose full view has
13539 -- discriminants which are invisible.
13541 elsif not Has_Discriminants (T)
13543 (Has_Unknown_Discriminants (T)
13544 and then Is_Private_Type (T))
13546 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13547 Fixup_Bad_Constraint;
13550 elsif Is_Constrained (E)
13551 or else (Ekind (E) = E_Class_Wide_Subtype
13552 and then Present (Discriminant_Constraint (E)))
13554 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13555 Fixup_Bad_Constraint;
13559 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13560 -- applies to the base type.
13562 T := Base_Type (T);
13564 Constr := Build_Discriminant_Constraints (T, S);
13566 -- If the list returned was empty we had an error in building the
13567 -- discriminant constraint. We have also already signalled an error
13568 -- in the incomplete type case
13570 if Is_Empty_Elmt_List (Constr) then
13571 Fixup_Bad_Constraint;
13575 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13576 end Constrain_Discriminated_Type;
13578 ---------------------------
13579 -- Constrain_Enumeration --
13580 ---------------------------
13582 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13583 T : constant Entity_Id := Entity (Subtype_Mark (S));
13584 C : constant Node_Id := Constraint (S);
13587 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13589 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13591 Set_Etype (Def_Id, Base_Type (T));
13592 Set_Size_Info (Def_Id, (T));
13593 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13594 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13596 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13598 Set_Discrete_RM_Size (Def_Id);
13599 end Constrain_Enumeration;
13601 ----------------------
13602 -- Constrain_Float --
13603 ----------------------
13605 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13606 T : constant Entity_Id := Entity (Subtype_Mark (S));
13612 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13614 Set_Etype (Def_Id, Base_Type (T));
13615 Set_Size_Info (Def_Id, (T));
13616 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13618 -- Process the constraint
13620 C := Constraint (S);
13622 -- Digits constraint present
13624 if Nkind (C) = N_Digits_Constraint then
13626 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13627 Check_Restriction (No_Obsolescent_Features, C);
13629 if Warn_On_Obsolescent_Feature then
13631 ("subtype digits constraint is an " &
13632 "obsolescent feature (RM J.3(8))?j?", C);
13635 D := Digits_Expression (C);
13636 Analyze_And_Resolve (D, Any_Integer);
13637 Check_Digits_Expression (D);
13638 Set_Digits_Value (Def_Id, Expr_Value (D));
13640 -- Check that digits value is in range. Obviously we can do this
13641 -- at compile time, but it is strictly a runtime check, and of
13642 -- course there is an ACVC test that checks this.
13644 if Digits_Value (Def_Id) > Digits_Value (T) then
13645 Error_Msg_Uint_1 := Digits_Value (T);
13646 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13648 Make_Raise_Constraint_Error (Sloc (D),
13649 Reason => CE_Range_Check_Failed);
13650 Insert_Action (Declaration_Node (Def_Id), Rais);
13653 C := Range_Constraint (C);
13655 -- No digits constraint present
13658 Set_Digits_Value (Def_Id, Digits_Value (T));
13661 -- Range constraint present
13663 if Nkind (C) = N_Range_Constraint then
13664 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13666 -- No range constraint present
13669 pragma Assert (No (C));
13670 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13673 Set_Is_Constrained (Def_Id);
13674 end Constrain_Float;
13676 ---------------------
13677 -- Constrain_Index --
13678 ---------------------
13680 procedure Constrain_Index
13683 Related_Nod : Node_Id;
13684 Related_Id : Entity_Id;
13685 Suffix : Character;
13686 Suffix_Index : Nat)
13688 Def_Id : Entity_Id;
13689 R : Node_Id := Empty;
13690 T : constant Entity_Id := Etype (Index);
13694 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13695 Set_Etype (Def_Id, Base_Type (T));
13697 if Nkind (S) = N_Range
13699 (Nkind (S) = N_Attribute_Reference
13700 and then Attribute_Name (S) = Name_Range)
13702 -- A Range attribute will be transformed into N_Range by Resolve
13708 Process_Range_Expr_In_Decl (R, T);
13710 if not Error_Posted (S)
13712 (Nkind (S) /= N_Range
13713 or else not Covers (T, (Etype (Low_Bound (S))))
13714 or else not Covers (T, (Etype (High_Bound (S)))))
13716 if Base_Type (T) /= Any_Type
13717 and then Etype (Low_Bound (S)) /= Any_Type
13718 and then Etype (High_Bound (S)) /= Any_Type
13720 Error_Msg_N ("range expected", S);
13724 elsif Nkind (S) = N_Subtype_Indication then
13726 -- The parser has verified that this is a discrete indication
13728 Resolve_Discrete_Subtype_Indication (S, T);
13729 Bad_Predicated_Subtype_Use
13730 ("subtype& has predicate, not allowed in index constraint",
13731 S, Entity (Subtype_Mark (S)));
13733 R := Range_Expression (Constraint (S));
13735 -- Capture values of bounds and generate temporaries for them if
13736 -- needed, since checks may cause duplication of the expressions
13737 -- which must not be reevaluated.
13739 -- The forced evaluation removes side effects from expressions, which
13740 -- should occur also in GNATprove mode. Otherwise, we end up with
13741 -- unexpected insertions of actions at places where this is not
13742 -- supposed to occur, e.g. on default parameters of a call.
13744 if Expander_Active or GNATprove_Mode then
13746 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13748 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13751 elsif Nkind (S) = N_Discriminant_Association then
13753 -- Syntactically valid in subtype indication
13755 Error_Msg_N ("invalid index constraint", S);
13756 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13759 -- Subtype_Mark case, no anonymous subtypes to construct
13764 if Is_Entity_Name (S) then
13765 if not Is_Type (Entity (S)) then
13766 Error_Msg_N ("expect subtype mark for index constraint", S);
13768 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13769 Wrong_Type (S, Base_Type (T));
13771 -- Check error of subtype with predicate in index constraint
13774 Bad_Predicated_Subtype_Use
13775 ("subtype& has predicate, not allowed in index constraint",
13782 Error_Msg_N ("invalid index constraint", S);
13783 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13788 -- Complete construction of the Itype
13790 if Is_Modular_Integer_Type (T) then
13791 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13793 elsif Is_Integer_Type (T) then
13794 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13797 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13798 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13799 Set_First_Literal (Def_Id, First_Literal (T));
13802 Set_Size_Info (Def_Id, (T));
13803 Set_RM_Size (Def_Id, RM_Size (T));
13804 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13806 Set_Scalar_Range (Def_Id, R);
13808 Set_Etype (S, Def_Id);
13809 Set_Discrete_RM_Size (Def_Id);
13810 end Constrain_Index;
13812 -----------------------
13813 -- Constrain_Integer --
13814 -----------------------
13816 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13817 T : constant Entity_Id := Entity (Subtype_Mark (S));
13818 C : constant Node_Id := Constraint (S);
13821 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13823 if Is_Modular_Integer_Type (T) then
13824 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13826 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13829 Set_Etype (Def_Id, Base_Type (T));
13830 Set_Size_Info (Def_Id, (T));
13831 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13832 Set_Discrete_RM_Size (Def_Id);
13833 end Constrain_Integer;
13835 ------------------------------
13836 -- Constrain_Ordinary_Fixed --
13837 ------------------------------
13839 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13840 T : constant Entity_Id := Entity (Subtype_Mark (S));
13846 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13847 Set_Etype (Def_Id, Base_Type (T));
13848 Set_Size_Info (Def_Id, (T));
13849 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13850 Set_Small_Value (Def_Id, Small_Value (T));
13852 -- Process the constraint
13854 C := Constraint (S);
13856 -- Delta constraint present
13858 if Nkind (C) = N_Delta_Constraint then
13860 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13861 Check_Restriction (No_Obsolescent_Features, C);
13863 if Warn_On_Obsolescent_Feature then
13865 ("subtype delta constraint is an " &
13866 "obsolescent feature (RM J.3(7))?j?");
13869 D := Delta_Expression (C);
13870 Analyze_And_Resolve (D, Any_Real);
13871 Check_Delta_Expression (D);
13872 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13874 -- Check that delta value is in range. Obviously we can do this
13875 -- at compile time, but it is strictly a runtime check, and of
13876 -- course there is an ACVC test that checks this.
13878 if Delta_Value (Def_Id) < Delta_Value (T) then
13879 Error_Msg_N ("??delta value is too small", D);
13881 Make_Raise_Constraint_Error (Sloc (D),
13882 Reason => CE_Range_Check_Failed);
13883 Insert_Action (Declaration_Node (Def_Id), Rais);
13886 C := Range_Constraint (C);
13888 -- No delta constraint present
13891 Set_Delta_Value (Def_Id, Delta_Value (T));
13894 -- Range constraint present
13896 if Nkind (C) = N_Range_Constraint then
13897 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13899 -- No range constraint present
13902 pragma Assert (No (C));
13903 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13906 Set_Discrete_RM_Size (Def_Id);
13908 -- Unconditionally delay the freeze, since we cannot set size
13909 -- information in all cases correctly until the freeze point.
13911 Set_Has_Delayed_Freeze (Def_Id);
13912 end Constrain_Ordinary_Fixed;
13914 -----------------------
13915 -- Contain_Interface --
13916 -----------------------
13918 function Contain_Interface
13919 (Iface : Entity_Id;
13920 Ifaces : Elist_Id) return Boolean
13922 Iface_Elmt : Elmt_Id;
13925 if Present (Ifaces) then
13926 Iface_Elmt := First_Elmt (Ifaces);
13927 while Present (Iface_Elmt) loop
13928 if Node (Iface_Elmt) = Iface then
13932 Next_Elmt (Iface_Elmt);
13937 end Contain_Interface;
13939 ---------------------------
13940 -- Convert_Scalar_Bounds --
13941 ---------------------------
13943 procedure Convert_Scalar_Bounds
13945 Parent_Type : Entity_Id;
13946 Derived_Type : Entity_Id;
13949 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13956 -- Defend against previous errors
13958 if No (Scalar_Range (Derived_Type)) then
13959 Check_Error_Detected;
13963 Lo := Build_Scalar_Bound
13964 (Type_Low_Bound (Derived_Type),
13965 Parent_Type, Implicit_Base);
13967 Hi := Build_Scalar_Bound
13968 (Type_High_Bound (Derived_Type),
13969 Parent_Type, Implicit_Base);
13976 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13978 Set_Parent (Rng, N);
13979 Set_Scalar_Range (Derived_Type, Rng);
13981 -- Analyze the bounds
13983 Analyze_And_Resolve (Lo, Implicit_Base);
13984 Analyze_And_Resolve (Hi, Implicit_Base);
13986 -- Analyze the range itself, except that we do not analyze it if
13987 -- the bounds are real literals, and we have a fixed-point type.
13988 -- The reason for this is that we delay setting the bounds in this
13989 -- case till we know the final Small and Size values (see circuit
13990 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13992 if Is_Fixed_Point_Type (Parent_Type)
13993 and then Nkind (Lo) = N_Real_Literal
13994 and then Nkind (Hi) = N_Real_Literal
13998 -- Here we do the analysis of the range
14000 -- Note: we do this manually, since if we do a normal Analyze and
14001 -- Resolve call, there are problems with the conversions used for
14002 -- the derived type range.
14005 Set_Etype (Rng, Implicit_Base);
14006 Set_Analyzed (Rng, True);
14008 end Convert_Scalar_Bounds;
14010 -------------------
14011 -- Copy_And_Swap --
14012 -------------------
14014 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14016 -- Initialize new full declaration entity by copying the pertinent
14017 -- fields of the corresponding private declaration entity.
14019 -- We temporarily set Ekind to a value appropriate for a type to
14020 -- avoid assert failures in Einfo from checking for setting type
14021 -- attributes on something that is not a type. Ekind (Priv) is an
14022 -- appropriate choice, since it allowed the attributes to be set
14023 -- in the first place. This Ekind value will be modified later.
14025 Set_Ekind (Full, Ekind (Priv));
14027 -- Also set Etype temporarily to Any_Type, again, in the absence
14028 -- of errors, it will be properly reset, and if there are errors,
14029 -- then we want a value of Any_Type to remain.
14031 Set_Etype (Full, Any_Type);
14033 -- Now start copying attributes
14035 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14037 if Has_Discriminants (Full) then
14038 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14039 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14042 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14043 Set_Homonym (Full, Homonym (Priv));
14044 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14045 Set_Is_Public (Full, Is_Public (Priv));
14046 Set_Is_Pure (Full, Is_Pure (Priv));
14047 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14048 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14049 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14050 Set_Has_Pragma_Unreferenced_Objects
14051 (Full, Has_Pragma_Unreferenced_Objects
14054 Conditional_Delay (Full, Priv);
14056 if Is_Tagged_Type (Full) then
14057 Set_Direct_Primitive_Operations
14058 (Full, Direct_Primitive_Operations (Priv));
14059 Set_No_Tagged_Streams_Pragma
14060 (Full, No_Tagged_Streams_Pragma (Priv));
14062 if Is_Base_Type (Priv) then
14063 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14067 Set_Is_Volatile (Full, Is_Volatile (Priv));
14068 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14069 Set_Scope (Full, Scope (Priv));
14070 Set_Next_Entity (Full, Next_Entity (Priv));
14071 Set_First_Entity (Full, First_Entity (Priv));
14072 Set_Last_Entity (Full, Last_Entity (Priv));
14074 -- If access types have been recorded for later handling, keep them in
14075 -- the full view so that they get handled when the full view freeze
14076 -- node is expanded.
14078 if Present (Freeze_Node (Priv))
14079 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14081 Ensure_Freeze_Node (Full);
14082 Set_Access_Types_To_Process
14083 (Freeze_Node (Full),
14084 Access_Types_To_Process (Freeze_Node (Priv)));
14087 -- Swap the two entities. Now Private is the full type entity and Full
14088 -- is the private one. They will be swapped back at the end of the
14089 -- private part. This swapping ensures that the entity that is visible
14090 -- in the private part is the full declaration.
14092 Exchange_Entities (Priv, Full);
14093 Append_Entity (Full, Scope (Full));
14096 -------------------------------------
14097 -- Copy_Array_Base_Type_Attributes --
14098 -------------------------------------
14100 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14102 Set_Component_Alignment (T1, Component_Alignment (T2));
14103 Set_Component_Type (T1, Component_Type (T2));
14104 Set_Component_Size (T1, Component_Size (T2));
14105 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14106 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14107 Propagate_Concurrent_Flags (T1, T2);
14108 Set_Is_Packed (T1, Is_Packed (T2));
14109 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14110 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14111 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14112 end Copy_Array_Base_Type_Attributes;
14114 -----------------------------------
14115 -- Copy_Array_Subtype_Attributes --
14116 -----------------------------------
14118 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14120 Set_Size_Info (T1, T2);
14122 Set_First_Index (T1, First_Index (T2));
14123 Set_Is_Aliased (T1, Is_Aliased (T2));
14124 Set_Is_Volatile (T1, Is_Volatile (T2));
14125 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14126 Set_Is_Constrained (T1, Is_Constrained (T2));
14127 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14128 Inherit_Rep_Item_Chain (T1, T2);
14129 Set_Convention (T1, Convention (T2));
14130 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14131 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14132 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14133 end Copy_Array_Subtype_Attributes;
14135 -----------------------------------
14136 -- Create_Constrained_Components --
14137 -----------------------------------
14139 procedure Create_Constrained_Components
14141 Decl_Node : Node_Id;
14143 Constraints : Elist_Id)
14145 Loc : constant Source_Ptr := Sloc (Subt);
14146 Comp_List : constant Elist_Id := New_Elmt_List;
14147 Parent_Type : constant Entity_Id := Etype (Typ);
14148 Assoc_List : constant List_Id := New_List;
14149 Discr_Val : Elmt_Id;
14153 Is_Static : Boolean := True;
14155 procedure Collect_Fixed_Components (Typ : Entity_Id);
14156 -- Collect parent type components that do not appear in a variant part
14158 procedure Create_All_Components;
14159 -- Iterate over Comp_List to create the components of the subtype
14161 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14162 -- Creates a new component from Old_Compon, copying all the fields from
14163 -- it, including its Etype, inserts the new component in the Subt entity
14164 -- chain and returns the new component.
14166 function Is_Variant_Record (T : Entity_Id) return Boolean;
14167 -- If true, and discriminants are static, collect only components from
14168 -- variants selected by discriminant values.
14170 ------------------------------
14171 -- Collect_Fixed_Components --
14172 ------------------------------
14174 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14176 -- Build association list for discriminants, and find components of the
14177 -- variant part selected by the values of the discriminants.
14179 Old_C := First_Discriminant (Typ);
14180 Discr_Val := First_Elmt (Constraints);
14181 while Present (Old_C) loop
14182 Append_To (Assoc_List,
14183 Make_Component_Association (Loc,
14184 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14185 Expression => New_Copy (Node (Discr_Val))));
14187 Next_Elmt (Discr_Val);
14188 Next_Discriminant (Old_C);
14191 -- The tag and the possible parent component are unconditionally in
14194 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14195 Old_C := First_Component (Typ);
14196 while Present (Old_C) loop
14197 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14198 Append_Elmt (Old_C, Comp_List);
14201 Next_Component (Old_C);
14204 end Collect_Fixed_Components;
14206 ---------------------------
14207 -- Create_All_Components --
14208 ---------------------------
14210 procedure Create_All_Components is
14214 Comp := First_Elmt (Comp_List);
14215 while Present (Comp) loop
14216 Old_C := Node (Comp);
14217 New_C := Create_Component (Old_C);
14221 Constrain_Component_Type
14222 (Old_C, Subt, Decl_Node, Typ, Constraints));
14223 Set_Is_Public (New_C, Is_Public (Subt));
14227 end Create_All_Components;
14229 ----------------------
14230 -- Create_Component --
14231 ----------------------
14233 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14234 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14237 if Ekind (Old_Compon) = E_Discriminant
14238 and then Is_Completely_Hidden (Old_Compon)
14240 -- This is a shadow discriminant created for a discriminant of
14241 -- the parent type, which needs to be present in the subtype.
14242 -- Give the shadow discriminant an internal name that cannot
14243 -- conflict with that of visible components.
14245 Set_Chars (New_Compon, New_Internal_Name ('C'));
14248 -- Set the parent so we have a proper link for freezing etc. This is
14249 -- not a real parent pointer, since of course our parent does not own
14250 -- up to us and reference us, we are an illegitimate child of the
14251 -- original parent.
14253 Set_Parent (New_Compon, Parent (Old_Compon));
14255 -- If the old component's Esize was already determined and is a
14256 -- static value, then the new component simply inherits it. Otherwise
14257 -- the old component's size may require run-time determination, but
14258 -- the new component's size still might be statically determinable
14259 -- (if, for example it has a static constraint). In that case we want
14260 -- Layout_Type to recompute the component's size, so we reset its
14261 -- size and positional fields.
14263 if Frontend_Layout_On_Target
14264 and then not Known_Static_Esize (Old_Compon)
14266 Set_Esize (New_Compon, Uint_0);
14267 Init_Normalized_First_Bit (New_Compon);
14268 Init_Normalized_Position (New_Compon);
14269 Init_Normalized_Position_Max (New_Compon);
14272 -- We do not want this node marked as Comes_From_Source, since
14273 -- otherwise it would get first class status and a separate cross-
14274 -- reference line would be generated. Illegitimate children do not
14275 -- rate such recognition.
14277 Set_Comes_From_Source (New_Compon, False);
14279 -- But it is a real entity, and a birth certificate must be properly
14280 -- registered by entering it into the entity list.
14282 Enter_Name (New_Compon);
14285 end Create_Component;
14287 -----------------------
14288 -- Is_Variant_Record --
14289 -----------------------
14291 function Is_Variant_Record (T : Entity_Id) return Boolean is
14293 return Nkind (Parent (T)) = N_Full_Type_Declaration
14294 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14295 and then Present (Component_List (Type_Definition (Parent (T))))
14298 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14299 end Is_Variant_Record;
14301 -- Start of processing for Create_Constrained_Components
14304 pragma Assert (Subt /= Base_Type (Subt));
14305 pragma Assert (Typ = Base_Type (Typ));
14307 Set_First_Entity (Subt, Empty);
14308 Set_Last_Entity (Subt, Empty);
14310 -- Check whether constraint is fully static, in which case we can
14311 -- optimize the list of components.
14313 Discr_Val := First_Elmt (Constraints);
14314 while Present (Discr_Val) loop
14315 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14316 Is_Static := False;
14320 Next_Elmt (Discr_Val);
14323 Set_Has_Static_Discriminants (Subt, Is_Static);
14327 -- Inherit the discriminants of the parent type
14329 Add_Discriminants : declare
14335 Old_C := First_Discriminant (Typ);
14337 while Present (Old_C) loop
14338 Num_Disc := Num_Disc + 1;
14339 New_C := Create_Component (Old_C);
14340 Set_Is_Public (New_C, Is_Public (Subt));
14341 Next_Discriminant (Old_C);
14344 -- For an untagged derived subtype, the number of discriminants may
14345 -- be smaller than the number of inherited discriminants, because
14346 -- several of them may be renamed by a single new discriminant or
14347 -- constrained. In this case, add the hidden discriminants back into
14348 -- the subtype, because they need to be present if the optimizer of
14349 -- the GCC 4.x back-end decides to break apart assignments between
14350 -- objects using the parent view into member-wise assignments.
14354 if Is_Derived_Type (Typ)
14355 and then not Is_Tagged_Type (Typ)
14357 Old_C := First_Stored_Discriminant (Typ);
14359 while Present (Old_C) loop
14360 Num_Gird := Num_Gird + 1;
14361 Next_Stored_Discriminant (Old_C);
14365 if Num_Gird > Num_Disc then
14367 -- Find out multiple uses of new discriminants, and add hidden
14368 -- components for the extra renamed discriminants. We recognize
14369 -- multiple uses through the Corresponding_Discriminant of a
14370 -- new discriminant: if it constrains several old discriminants,
14371 -- this field points to the last one in the parent type. The
14372 -- stored discriminants of the derived type have the same name
14373 -- as those of the parent.
14377 New_Discr : Entity_Id;
14378 Old_Discr : Entity_Id;
14381 Constr := First_Elmt (Stored_Constraint (Typ));
14382 Old_Discr := First_Stored_Discriminant (Typ);
14383 while Present (Constr) loop
14384 if Is_Entity_Name (Node (Constr))
14385 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14387 New_Discr := Entity (Node (Constr));
14389 if Chars (Corresponding_Discriminant (New_Discr)) /=
14392 -- The new discriminant has been used to rename a
14393 -- subsequent old discriminant. Introduce a shadow
14394 -- component for the current old discriminant.
14396 New_C := Create_Component (Old_Discr);
14397 Set_Original_Record_Component (New_C, Old_Discr);
14401 -- The constraint has eliminated the old discriminant.
14402 -- Introduce a shadow component.
14404 New_C := Create_Component (Old_Discr);
14405 Set_Original_Record_Component (New_C, Old_Discr);
14408 Next_Elmt (Constr);
14409 Next_Stored_Discriminant (Old_Discr);
14413 end Add_Discriminants;
14416 and then Is_Variant_Record (Typ)
14418 Collect_Fixed_Components (Typ);
14420 Gather_Components (
14422 Component_List (Type_Definition (Parent (Typ))),
14423 Governed_By => Assoc_List,
14425 Report_Errors => Errors);
14426 pragma Assert (not Errors
14427 or else Serious_Errors_Detected > 0);
14429 Create_All_Components;
14431 -- If the subtype declaration is created for a tagged type derivation
14432 -- with constraints, we retrieve the record definition of the parent
14433 -- type to select the components of the proper variant.
14436 and then Is_Tagged_Type (Typ)
14437 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14439 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14440 and then Is_Variant_Record (Parent_Type)
14442 Collect_Fixed_Components (Typ);
14446 Component_List (Type_Definition (Parent (Parent_Type))),
14447 Governed_By => Assoc_List,
14449 Report_Errors => Errors);
14451 -- Note: previously there was a check at this point that no errors
14452 -- were detected. As a consequence of AI05-220 there may be an error
14453 -- if an inherited discriminant that controls a variant has a non-
14454 -- static constraint.
14456 -- If the tagged derivation has a type extension, collect all the
14457 -- new components therein.
14459 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14461 Old_C := First_Component (Typ);
14462 while Present (Old_C) loop
14463 if Original_Record_Component (Old_C) = Old_C
14464 and then Chars (Old_C) /= Name_uTag
14465 and then Chars (Old_C) /= Name_uParent
14467 Append_Elmt (Old_C, Comp_List);
14470 Next_Component (Old_C);
14474 Create_All_Components;
14477 -- If discriminants are not static, or if this is a multi-level type
14478 -- extension, we have to include all components of the parent type.
14480 Old_C := First_Component (Typ);
14481 while Present (Old_C) loop
14482 New_C := Create_Component (Old_C);
14486 Constrain_Component_Type
14487 (Old_C, Subt, Decl_Node, Typ, Constraints));
14488 Set_Is_Public (New_C, Is_Public (Subt));
14490 Next_Component (Old_C);
14495 end Create_Constrained_Components;
14497 ------------------------------------------
14498 -- Decimal_Fixed_Point_Type_Declaration --
14499 ------------------------------------------
14501 procedure Decimal_Fixed_Point_Type_Declaration
14505 Loc : constant Source_Ptr := Sloc (Def);
14506 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14507 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14508 Implicit_Base : Entity_Id;
14515 Check_SPARK_05_Restriction
14516 ("decimal fixed point type is not allowed", Def);
14517 Check_Restriction (No_Fixed_Point, Def);
14519 -- Create implicit base type
14522 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14523 Set_Etype (Implicit_Base, Implicit_Base);
14525 -- Analyze and process delta expression
14527 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14529 Check_Delta_Expression (Delta_Expr);
14530 Delta_Val := Expr_Value_R (Delta_Expr);
14532 -- Check delta is power of 10, and determine scale value from it
14538 Scale_Val := Uint_0;
14541 if Val < Ureal_1 then
14542 while Val < Ureal_1 loop
14543 Val := Val * Ureal_10;
14544 Scale_Val := Scale_Val + 1;
14547 if Scale_Val > 18 then
14548 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14549 Scale_Val := UI_From_Int (+18);
14553 while Val > Ureal_1 loop
14554 Val := Val / Ureal_10;
14555 Scale_Val := Scale_Val - 1;
14558 if Scale_Val < -18 then
14559 Error_Msg_N ("scale is less than minimum value of -18", Def);
14560 Scale_Val := UI_From_Int (-18);
14564 if Val /= Ureal_1 then
14565 Error_Msg_N ("delta expression must be a power of 10", Def);
14566 Delta_Val := Ureal_10 ** (-Scale_Val);
14570 -- Set delta, scale and small (small = delta for decimal type)
14572 Set_Delta_Value (Implicit_Base, Delta_Val);
14573 Set_Scale_Value (Implicit_Base, Scale_Val);
14574 Set_Small_Value (Implicit_Base, Delta_Val);
14576 -- Analyze and process digits expression
14578 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14579 Check_Digits_Expression (Digs_Expr);
14580 Digs_Val := Expr_Value (Digs_Expr);
14582 if Digs_Val > 18 then
14583 Digs_Val := UI_From_Int (+18);
14584 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14587 Set_Digits_Value (Implicit_Base, Digs_Val);
14588 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14590 -- Set range of base type from digits value for now. This will be
14591 -- expanded to represent the true underlying base range by Freeze.
14593 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14595 -- Note: We leave size as zero for now, size will be set at freeze
14596 -- time. We have to do this for ordinary fixed-point, because the size
14597 -- depends on the specified small, and we might as well do the same for
14598 -- decimal fixed-point.
14600 pragma Assert (Esize (Implicit_Base) = Uint_0);
14602 -- If there are bounds given in the declaration use them as the
14603 -- bounds of the first named subtype.
14605 if Present (Real_Range_Specification (Def)) then
14607 RRS : constant Node_Id := Real_Range_Specification (Def);
14608 Low : constant Node_Id := Low_Bound (RRS);
14609 High : constant Node_Id := High_Bound (RRS);
14614 Analyze_And_Resolve (Low, Any_Real);
14615 Analyze_And_Resolve (High, Any_Real);
14616 Check_Real_Bound (Low);
14617 Check_Real_Bound (High);
14618 Low_Val := Expr_Value_R (Low);
14619 High_Val := Expr_Value_R (High);
14621 if Low_Val < (-Bound_Val) then
14623 ("range low bound too small for digits value", Low);
14624 Low_Val := -Bound_Val;
14627 if High_Val > Bound_Val then
14629 ("range high bound too large for digits value", High);
14630 High_Val := Bound_Val;
14633 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14636 -- If no explicit range, use range that corresponds to given
14637 -- digits value. This will end up as the final range for the
14641 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14644 -- Complete entity for first subtype. The inheritance of the rep item
14645 -- chain ensures that SPARK-related pragmas are not clobbered when the
14646 -- decimal fixed point type acts as a full view of a private type.
14648 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14649 Set_Etype (T, Implicit_Base);
14650 Set_Size_Info (T, Implicit_Base);
14651 Inherit_Rep_Item_Chain (T, Implicit_Base);
14652 Set_Digits_Value (T, Digs_Val);
14653 Set_Delta_Value (T, Delta_Val);
14654 Set_Small_Value (T, Delta_Val);
14655 Set_Scale_Value (T, Scale_Val);
14656 Set_Is_Constrained (T);
14657 end Decimal_Fixed_Point_Type_Declaration;
14659 -----------------------------------
14660 -- Derive_Progenitor_Subprograms --
14661 -----------------------------------
14663 procedure Derive_Progenitor_Subprograms
14664 (Parent_Type : Entity_Id;
14665 Tagged_Type : Entity_Id)
14670 Iface_Elmt : Elmt_Id;
14671 Iface_Subp : Entity_Id;
14672 New_Subp : Entity_Id := Empty;
14673 Prim_Elmt : Elmt_Id;
14678 pragma Assert (Ada_Version >= Ada_2005
14679 and then Is_Record_Type (Tagged_Type)
14680 and then Is_Tagged_Type (Tagged_Type)
14681 and then Has_Interfaces (Tagged_Type));
14683 -- Step 1: Transfer to the full-view primitives associated with the
14684 -- partial-view that cover interface primitives. Conceptually this
14685 -- work should be done later by Process_Full_View; done here to
14686 -- simplify its implementation at later stages. It can be safely
14687 -- done here because interfaces must be visible in the partial and
14688 -- private view (RM 7.3(7.3/2)).
14690 -- Small optimization: This work is only required if the parent may
14691 -- have entities whose Alias attribute reference an interface primitive.
14692 -- Such a situation may occur if the parent is an abstract type and the
14693 -- primitive has not been yet overridden or if the parent is a generic
14694 -- formal type covering interfaces.
14696 -- If the tagged type is not abstract, it cannot have abstract
14697 -- primitives (the only entities in the list of primitives of
14698 -- non-abstract tagged types that can reference abstract primitives
14699 -- through its Alias attribute are the internal entities that have
14700 -- attribute Interface_Alias, and these entities are generated later
14701 -- by Add_Internal_Interface_Entities).
14703 if In_Private_Part (Current_Scope)
14704 and then (Is_Abstract_Type (Parent_Type)
14706 Is_Generic_Type (Parent_Type))
14708 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14709 while Present (Elmt) loop
14710 Subp := Node (Elmt);
14712 -- At this stage it is not possible to have entities in the list
14713 -- of primitives that have attribute Interface_Alias.
14715 pragma Assert (No (Interface_Alias (Subp)));
14717 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14719 if Is_Interface (Typ) then
14720 E := Find_Primitive_Covering_Interface
14721 (Tagged_Type => Tagged_Type,
14722 Iface_Prim => Subp);
14725 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14727 Replace_Elmt (Elmt, E);
14728 Remove_Homonym (Subp);
14736 -- Step 2: Add primitives of progenitors that are not implemented by
14737 -- parents of Tagged_Type.
14739 if Present (Interfaces (Base_Type (Tagged_Type))) then
14740 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14741 while Present (Iface_Elmt) loop
14742 Iface := Node (Iface_Elmt);
14744 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14745 while Present (Prim_Elmt) loop
14746 Iface_Subp := Node (Prim_Elmt);
14748 -- Exclude derivation of predefined primitives except those
14749 -- that come from source, or are inherited from one that comes
14750 -- from source. Required to catch declarations of equality
14751 -- operators of interfaces. For example:
14753 -- type Iface is interface;
14754 -- function "=" (Left, Right : Iface) return Boolean;
14756 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14757 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14759 E := Find_Primitive_Covering_Interface
14760 (Tagged_Type => Tagged_Type,
14761 Iface_Prim => Iface_Subp);
14763 -- If not found we derive a new primitive leaving its alias
14764 -- attribute referencing the interface primitive.
14768 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14770 -- Ada 2012 (AI05-0197): If the covering primitive's name
14771 -- differs from the name of the interface primitive then it
14772 -- is a private primitive inherited from a parent type. In
14773 -- such case, given that Tagged_Type covers the interface,
14774 -- the inherited private primitive becomes visible. For such
14775 -- purpose we add a new entity that renames the inherited
14776 -- private primitive.
14778 elsif Chars (E) /= Chars (Iface_Subp) then
14779 pragma Assert (Has_Suffix (E, 'P'));
14781 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14782 Set_Alias (New_Subp, E);
14783 Set_Is_Abstract_Subprogram (New_Subp,
14784 Is_Abstract_Subprogram (E));
14786 -- Propagate to the full view interface entities associated
14787 -- with the partial view.
14789 elsif In_Private_Part (Current_Scope)
14790 and then Present (Alias (E))
14791 and then Alias (E) = Iface_Subp
14793 List_Containing (Parent (E)) /=
14794 Private_Declarations
14796 (Unit_Declaration_Node (Current_Scope)))
14798 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14802 Next_Elmt (Prim_Elmt);
14805 Next_Elmt (Iface_Elmt);
14808 end Derive_Progenitor_Subprograms;
14810 -----------------------
14811 -- Derive_Subprogram --
14812 -----------------------
14814 procedure Derive_Subprogram
14815 (New_Subp : out Entity_Id;
14816 Parent_Subp : Entity_Id;
14817 Derived_Type : Entity_Id;
14818 Parent_Type : Entity_Id;
14819 Actual_Subp : Entity_Id := Empty)
14821 Formal : Entity_Id;
14822 -- Formal parameter of parent primitive operation
14824 Formal_Of_Actual : Entity_Id;
14825 -- Formal parameter of actual operation, when the derivation is to
14826 -- create a renaming for a primitive operation of an actual in an
14829 New_Formal : Entity_Id;
14830 -- Formal of inherited operation
14832 Visible_Subp : Entity_Id := Parent_Subp;
14834 function Is_Private_Overriding return Boolean;
14835 -- If Subp is a private overriding of a visible operation, the inherited
14836 -- operation derives from the overridden op (even though its body is the
14837 -- overriding one) and the inherited operation is visible now. See
14838 -- sem_disp to see the full details of the handling of the overridden
14839 -- subprogram, which is removed from the list of primitive operations of
14840 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14841 -- and used to diagnose abstract operations that need overriding in the
14844 procedure Replace_Type (Id, New_Id : Entity_Id);
14845 -- When the type is an anonymous access type, create a new access type
14846 -- designating the derived type.
14848 procedure Set_Derived_Name;
14849 -- This procedure sets the appropriate Chars name for New_Subp. This
14850 -- is normally just a copy of the parent name. An exception arises for
14851 -- type support subprograms, where the name is changed to reflect the
14852 -- name of the derived type, e.g. if type foo is derived from type bar,
14853 -- then a procedure barDA is derived with a name fooDA.
14855 ---------------------------
14856 -- Is_Private_Overriding --
14857 ---------------------------
14859 function Is_Private_Overriding return Boolean is
14863 -- If the parent is not a dispatching operation there is no
14864 -- need to investigate overridings
14866 if not Is_Dispatching_Operation (Parent_Subp) then
14870 -- The visible operation that is overridden is a homonym of the
14871 -- parent subprogram. We scan the homonym chain to find the one
14872 -- whose alias is the subprogram we are deriving.
14874 Prev := Current_Entity (Parent_Subp);
14875 while Present (Prev) loop
14876 if Ekind (Prev) = Ekind (Parent_Subp)
14877 and then Alias (Prev) = Parent_Subp
14878 and then Scope (Parent_Subp) = Scope (Prev)
14879 and then not Is_Hidden (Prev)
14881 Visible_Subp := Prev;
14885 Prev := Homonym (Prev);
14889 end Is_Private_Overriding;
14895 procedure Replace_Type (Id, New_Id : Entity_Id) is
14896 Id_Type : constant Entity_Id := Etype (Id);
14897 Acc_Type : Entity_Id;
14898 Par : constant Node_Id := Parent (Derived_Type);
14901 -- When the type is an anonymous access type, create a new access
14902 -- type designating the derived type. This itype must be elaborated
14903 -- at the point of the derivation, not on subsequent calls that may
14904 -- be out of the proper scope for Gigi, so we insert a reference to
14905 -- it after the derivation.
14907 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14909 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14912 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14913 and then Present (Full_View (Desig_Typ))
14914 and then not Is_Private_Type (Parent_Type)
14916 Desig_Typ := Full_View (Desig_Typ);
14919 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14921 -- Ada 2005 (AI-251): Handle also derivations of abstract
14922 -- interface primitives.
14924 or else (Is_Interface (Desig_Typ)
14925 and then not Is_Class_Wide_Type (Desig_Typ))
14927 Acc_Type := New_Copy (Id_Type);
14928 Set_Etype (Acc_Type, Acc_Type);
14929 Set_Scope (Acc_Type, New_Subp);
14931 -- Set size of anonymous access type. If we have an access
14932 -- to an unconstrained array, this is a fat pointer, so it
14933 -- is sizes at twice addtress size.
14935 if Is_Array_Type (Desig_Typ)
14936 and then not Is_Constrained (Desig_Typ)
14938 Init_Size (Acc_Type, 2 * System_Address_Size);
14940 -- Other cases use a thin pointer
14943 Init_Size (Acc_Type, System_Address_Size);
14946 -- Set remaining characterstics of anonymous access type
14948 Init_Alignment (Acc_Type);
14949 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14951 Set_Etype (New_Id, Acc_Type);
14952 Set_Scope (New_Id, New_Subp);
14954 -- Create a reference to it
14956 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14959 Set_Etype (New_Id, Id_Type);
14963 -- In Ada2012, a formal may have an incomplete type but the type
14964 -- derivation that inherits the primitive follows the full view.
14966 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14968 (Ekind (Id_Type) = E_Record_Type_With_Private
14969 and then Present (Full_View (Id_Type))
14971 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14973 (Ada_Version >= Ada_2012
14974 and then Ekind (Id_Type) = E_Incomplete_Type
14975 and then Full_View (Id_Type) = Parent_Type)
14977 -- Constraint checks on formals are generated during expansion,
14978 -- based on the signature of the original subprogram. The bounds
14979 -- of the derived type are not relevant, and thus we can use
14980 -- the base type for the formals. However, the return type may be
14981 -- used in a context that requires that the proper static bounds
14982 -- be used (a case statement, for example) and for those cases
14983 -- we must use the derived type (first subtype), not its base.
14985 -- If the derived_type_definition has no constraints, we know that
14986 -- the derived type has the same constraints as the first subtype
14987 -- of the parent, and we can also use it rather than its base,
14988 -- which can lead to more efficient code.
14990 if Etype (Id) = Parent_Type then
14991 if Is_Scalar_Type (Parent_Type)
14993 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14995 Set_Etype (New_Id, Derived_Type);
14997 elsif Nkind (Par) = N_Full_Type_Declaration
14999 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15002 (Subtype_Indication (Type_Definition (Par)))
15004 Set_Etype (New_Id, Derived_Type);
15007 Set_Etype (New_Id, Base_Type (Derived_Type));
15011 Set_Etype (New_Id, Base_Type (Derived_Type));
15015 Set_Etype (New_Id, Etype (Id));
15019 ----------------------
15020 -- Set_Derived_Name --
15021 ----------------------
15023 procedure Set_Derived_Name is
15024 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15026 if Nm = TSS_Null then
15027 Set_Chars (New_Subp, Chars (Parent_Subp));
15029 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15031 end Set_Derived_Name;
15033 -- Start of processing for Derive_Subprogram
15036 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15037 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15039 -- Check whether the inherited subprogram is a private operation that
15040 -- should be inherited but not yet made visible. Such subprograms can
15041 -- become visible at a later point (e.g., the private part of a public
15042 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15043 -- following predicate is true, then this is not such a private
15044 -- operation and the subprogram simply inherits the name of the parent
15045 -- subprogram. Note the special check for the names of controlled
15046 -- operations, which are currently exempted from being inherited with
15047 -- a hidden name because they must be findable for generation of
15048 -- implicit run-time calls.
15050 if not Is_Hidden (Parent_Subp)
15051 or else Is_Internal (Parent_Subp)
15052 or else Is_Private_Overriding
15053 or else Is_Internal_Name (Chars (Parent_Subp))
15054 or else (Is_Controlled (Parent_Type)
15055 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15061 -- An inherited dispatching equality will be overridden by an internally
15062 -- generated one, or by an explicit one, so preserve its name and thus
15063 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15064 -- private operation it may become invisible if the full view has
15065 -- progenitors, and the dispatch table will be malformed.
15066 -- We check that the type is limited to handle the anomalous declaration
15067 -- of Limited_Controlled, which is derived from a non-limited type, and
15068 -- which is handled specially elsewhere as well.
15070 elsif Chars (Parent_Subp) = Name_Op_Eq
15071 and then Is_Dispatching_Operation (Parent_Subp)
15072 and then Etype (Parent_Subp) = Standard_Boolean
15073 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15075 Etype (First_Formal (Parent_Subp)) =
15076 Etype (Next_Formal (First_Formal (Parent_Subp)))
15080 -- If parent is hidden, this can be a regular derivation if the
15081 -- parent is immediately visible in a non-instantiating context,
15082 -- or if we are in the private part of an instance. This test
15083 -- should still be refined ???
15085 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15086 -- operation as a non-visible operation in cases where the parent
15087 -- subprogram might not be visible now, but was visible within the
15088 -- original generic, so it would be wrong to make the inherited
15089 -- subprogram non-visible now. (Not clear if this test is fully
15090 -- correct; are there any cases where we should declare the inherited
15091 -- operation as not visible to avoid it being overridden, e.g., when
15092 -- the parent type is a generic actual with private primitives ???)
15094 -- (they should be treated the same as other private inherited
15095 -- subprograms, but it's not clear how to do this cleanly). ???
15097 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15098 and then Is_Immediately_Visible (Parent_Subp)
15099 and then not In_Instance)
15100 or else In_Instance_Not_Visible
15104 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15105 -- overrides an interface primitive because interface primitives
15106 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15108 elsif Ada_Version >= Ada_2005
15109 and then Is_Dispatching_Operation (Parent_Subp)
15110 and then Present (Covered_Interface_Op (Parent_Subp))
15114 -- Otherwise, the type is inheriting a private operation, so enter it
15115 -- with a special name so it can't be overridden.
15118 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15121 Set_Parent (New_Subp, Parent (Derived_Type));
15123 if Present (Actual_Subp) then
15124 Replace_Type (Actual_Subp, New_Subp);
15126 Replace_Type (Parent_Subp, New_Subp);
15129 Conditional_Delay (New_Subp, Parent_Subp);
15131 -- If we are creating a renaming for a primitive operation of an
15132 -- actual of a generic derived type, we must examine the signature
15133 -- of the actual primitive, not that of the generic formal, which for
15134 -- example may be an interface. However the name and initial value
15135 -- of the inherited operation are those of the formal primitive.
15137 Formal := First_Formal (Parent_Subp);
15139 if Present (Actual_Subp) then
15140 Formal_Of_Actual := First_Formal (Actual_Subp);
15142 Formal_Of_Actual := Empty;
15145 while Present (Formal) loop
15146 New_Formal := New_Copy (Formal);
15148 -- Normally we do not go copying parents, but in the case of
15149 -- formals, we need to link up to the declaration (which is the
15150 -- parameter specification), and it is fine to link up to the
15151 -- original formal's parameter specification in this case.
15153 Set_Parent (New_Formal, Parent (Formal));
15154 Append_Entity (New_Formal, New_Subp);
15156 if Present (Formal_Of_Actual) then
15157 Replace_Type (Formal_Of_Actual, New_Formal);
15158 Next_Formal (Formal_Of_Actual);
15160 Replace_Type (Formal, New_Formal);
15163 Next_Formal (Formal);
15166 -- If this derivation corresponds to a tagged generic actual, then
15167 -- primitive operations rename those of the actual. Otherwise the
15168 -- primitive operations rename those of the parent type, If the parent
15169 -- renames an intrinsic operator, so does the new subprogram. We except
15170 -- concatenation, which is always properly typed, and does not get
15171 -- expanded as other intrinsic operations.
15173 if No (Actual_Subp) then
15174 if Is_Intrinsic_Subprogram (Parent_Subp) then
15175 Set_Is_Intrinsic_Subprogram (New_Subp);
15177 if Present (Alias (Parent_Subp))
15178 and then Chars (Parent_Subp) /= Name_Op_Concat
15180 Set_Alias (New_Subp, Alias (Parent_Subp));
15182 Set_Alias (New_Subp, Parent_Subp);
15186 Set_Alias (New_Subp, Parent_Subp);
15190 Set_Alias (New_Subp, Actual_Subp);
15193 -- Derived subprograms of a tagged type must inherit the convention
15194 -- of the parent subprogram (a requirement of AI-117). Derived
15195 -- subprograms of untagged types simply get convention Ada by default.
15197 -- If the derived type is a tagged generic formal type with unknown
15198 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15200 -- However, if the type is derived from a generic formal, the further
15201 -- inherited subprogram has the convention of the non-generic ancestor.
15202 -- Otherwise there would be no way to override the operation.
15203 -- (This is subject to forthcoming ARG discussions).
15205 if Is_Tagged_Type (Derived_Type) then
15206 if Is_Generic_Type (Derived_Type)
15207 and then Has_Unknown_Discriminants (Derived_Type)
15209 Set_Convention (New_Subp, Convention_Intrinsic);
15212 if Is_Generic_Type (Parent_Type)
15213 and then Has_Unknown_Discriminants (Parent_Type)
15215 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15217 Set_Convention (New_Subp, Convention (Parent_Subp));
15222 -- Predefined controlled operations retain their name even if the parent
15223 -- is hidden (see above), but they are not primitive operations if the
15224 -- ancestor is not visible, for example if the parent is a private
15225 -- extension completed with a controlled extension. Note that a full
15226 -- type that is controlled can break privacy: the flag Is_Controlled is
15227 -- set on both views of the type.
15229 if Is_Controlled (Parent_Type)
15230 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15233 and then Is_Hidden (Parent_Subp)
15234 and then not Is_Visibly_Controlled (Parent_Type)
15236 Set_Is_Hidden (New_Subp);
15239 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15240 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15242 if Ekind (Parent_Subp) = E_Procedure then
15243 Set_Is_Valued_Procedure
15244 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15246 Set_Has_Controlling_Result
15247 (New_Subp, Has_Controlling_Result (Parent_Subp));
15250 -- No_Return must be inherited properly. If this is overridden in the
15251 -- case of a dispatching operation, then a check is made in Sem_Disp
15252 -- that the overriding operation is also No_Return (no such check is
15253 -- required for the case of non-dispatching operation.
15255 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15257 -- A derived function with a controlling result is abstract. If the
15258 -- Derived_Type is a nonabstract formal generic derived type, then
15259 -- inherited operations are not abstract: the required check is done at
15260 -- instantiation time. If the derivation is for a generic actual, the
15261 -- function is not abstract unless the actual is.
15263 if Is_Generic_Type (Derived_Type)
15264 and then not Is_Abstract_Type (Derived_Type)
15268 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15269 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15271 -- A subprogram subject to pragma Extensions_Visible with value False
15272 -- requires overriding if the subprogram has at least one controlling
15273 -- OUT parameter (SPARK RM 6.1.7(6)).
15275 elsif Ada_Version >= Ada_2005
15276 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15277 or else (Is_Tagged_Type (Derived_Type)
15278 and then Etype (New_Subp) = Derived_Type
15279 and then not Is_Null_Extension (Derived_Type))
15280 or else (Is_Tagged_Type (Derived_Type)
15281 and then Ekind (Etype (New_Subp)) =
15282 E_Anonymous_Access_Type
15283 and then Designated_Type (Etype (New_Subp)) =
15285 and then not Is_Null_Extension (Derived_Type))
15286 or else (Comes_From_Source (Alias (New_Subp))
15287 and then Is_EVF_Procedure (Alias (New_Subp))))
15288 and then No (Actual_Subp)
15290 if not Is_Tagged_Type (Derived_Type)
15291 or else Is_Abstract_Type (Derived_Type)
15292 or else Is_Abstract_Subprogram (Alias (New_Subp))
15294 Set_Is_Abstract_Subprogram (New_Subp);
15296 Set_Requires_Overriding (New_Subp);
15299 elsif Ada_Version < Ada_2005
15300 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15301 or else (Is_Tagged_Type (Derived_Type)
15302 and then Etype (New_Subp) = Derived_Type
15303 and then No (Actual_Subp)))
15305 Set_Is_Abstract_Subprogram (New_Subp);
15307 -- AI05-0097 : an inherited operation that dispatches on result is
15308 -- abstract if the derived type is abstract, even if the parent type
15309 -- is concrete and the derived type is a null extension.
15311 elsif Has_Controlling_Result (Alias (New_Subp))
15312 and then Is_Abstract_Type (Etype (New_Subp))
15314 Set_Is_Abstract_Subprogram (New_Subp);
15316 -- Finally, if the parent type is abstract we must verify that all
15317 -- inherited operations are either non-abstract or overridden, or that
15318 -- the derived type itself is abstract (this check is performed at the
15319 -- end of a package declaration, in Check_Abstract_Overriding). A
15320 -- private overriding in the parent type will not be visible in the
15321 -- derivation if we are not in an inner package or in a child unit of
15322 -- the parent type, in which case the abstractness of the inherited
15323 -- operation is carried to the new subprogram.
15325 elsif Is_Abstract_Type (Parent_Type)
15326 and then not In_Open_Scopes (Scope (Parent_Type))
15327 and then Is_Private_Overriding
15328 and then Is_Abstract_Subprogram (Visible_Subp)
15330 if No (Actual_Subp) then
15331 Set_Alias (New_Subp, Visible_Subp);
15332 Set_Is_Abstract_Subprogram (New_Subp, True);
15335 -- If this is a derivation for an instance of a formal derived
15336 -- type, abstractness comes from the primitive operation of the
15337 -- actual, not from the operation inherited from the ancestor.
15339 Set_Is_Abstract_Subprogram
15340 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15344 New_Overloaded_Entity (New_Subp, Derived_Type);
15346 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15347 -- preconditions and the derived type is abstract, the derived operation
15348 -- is abstract as well if parent subprogram is not abstract or null.
15350 if Is_Abstract_Type (Derived_Type)
15351 and then Has_Non_Trivial_Precondition (Parent_Subp)
15352 and then Present (Interfaces (Derived_Type))
15354 Set_Is_Dispatching_Operation (New_Subp);
15357 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15360 if Present (Iface_Prim)
15361 and then Has_Non_Trivial_Precondition (Iface_Prim)
15363 Set_Is_Abstract_Subprogram (New_Subp);
15368 -- Check for case of a derived subprogram for the instantiation of a
15369 -- formal derived tagged type, if so mark the subprogram as dispatching
15370 -- and inherit the dispatching attributes of the actual subprogram. The
15371 -- derived subprogram is effectively renaming of the actual subprogram,
15372 -- so it needs to have the same attributes as the actual.
15374 if Present (Actual_Subp)
15375 and then Is_Dispatching_Operation (Actual_Subp)
15377 Set_Is_Dispatching_Operation (New_Subp);
15379 if Present (DTC_Entity (Actual_Subp)) then
15380 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15381 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15385 -- Indicate that a derived subprogram does not require a body and that
15386 -- it does not require processing of default expressions.
15388 Set_Has_Completion (New_Subp);
15389 Set_Default_Expressions_Processed (New_Subp);
15391 if Ekind (New_Subp) = E_Function then
15392 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15394 end Derive_Subprogram;
15396 ------------------------
15397 -- Derive_Subprograms --
15398 ------------------------
15400 procedure Derive_Subprograms
15401 (Parent_Type : Entity_Id;
15402 Derived_Type : Entity_Id;
15403 Generic_Actual : Entity_Id := Empty)
15405 Op_List : constant Elist_Id :=
15406 Collect_Primitive_Operations (Parent_Type);
15408 function Check_Derived_Type return Boolean;
15409 -- Check that all the entities derived from Parent_Type are found in
15410 -- the list of primitives of Derived_Type exactly in the same order.
15412 procedure Derive_Interface_Subprogram
15413 (New_Subp : out Entity_Id;
15415 Actual_Subp : Entity_Id);
15416 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15417 -- (which is an interface primitive). If Generic_Actual is present then
15418 -- Actual_Subp is the actual subprogram corresponding with the generic
15419 -- subprogram Subp.
15421 ------------------------
15422 -- Check_Derived_Type --
15423 ------------------------
15425 function Check_Derived_Type return Boolean is
15429 New_Subp : Entity_Id;
15434 -- Traverse list of entities in the current scope searching for
15435 -- an incomplete type whose full-view is derived type.
15437 E := First_Entity (Scope (Derived_Type));
15438 while Present (E) and then E /= Derived_Type loop
15439 if Ekind (E) = E_Incomplete_Type
15440 and then Present (Full_View (E))
15441 and then Full_View (E) = Derived_Type
15443 -- Disable this test if Derived_Type completes an incomplete
15444 -- type because in such case more primitives can be added
15445 -- later to the list of primitives of Derived_Type by routine
15446 -- Process_Incomplete_Dependents
15451 E := Next_Entity (E);
15454 List := Collect_Primitive_Operations (Derived_Type);
15455 Elmt := First_Elmt (List);
15457 Op_Elmt := First_Elmt (Op_List);
15458 while Present (Op_Elmt) loop
15459 Subp := Node (Op_Elmt);
15460 New_Subp := Node (Elmt);
15462 -- At this early stage Derived_Type has no entities with attribute
15463 -- Interface_Alias. In addition, such primitives are always
15464 -- located at the end of the list of primitives of Parent_Type.
15465 -- Therefore, if found we can safely stop processing pending
15468 exit when Present (Interface_Alias (Subp));
15470 -- Handle hidden entities
15472 if not Is_Predefined_Dispatching_Operation (Subp)
15473 and then Is_Hidden (Subp)
15475 if Present (New_Subp)
15476 and then Primitive_Names_Match (Subp, New_Subp)
15482 if not Present (New_Subp)
15483 or else Ekind (Subp) /= Ekind (New_Subp)
15484 or else not Primitive_Names_Match (Subp, New_Subp)
15492 Next_Elmt (Op_Elmt);
15496 end Check_Derived_Type;
15498 ---------------------------------
15499 -- Derive_Interface_Subprogram --
15500 ---------------------------------
15502 procedure Derive_Interface_Subprogram
15503 (New_Subp : out Entity_Id;
15505 Actual_Subp : Entity_Id)
15507 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15508 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15511 pragma Assert (Is_Interface (Iface_Type));
15514 (New_Subp => New_Subp,
15515 Parent_Subp => Iface_Subp,
15516 Derived_Type => Derived_Type,
15517 Parent_Type => Iface_Type,
15518 Actual_Subp => Actual_Subp);
15520 -- Given that this new interface entity corresponds with a primitive
15521 -- of the parent that was not overridden we must leave it associated
15522 -- with its parent primitive to ensure that it will share the same
15523 -- dispatch table slot when overridden. We must set the Alias to Subp
15524 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15525 -- (in case we inherited Subp from Iface_Type via a nonabstract
15526 -- generic formal type).
15528 if No (Actual_Subp) then
15529 Set_Alias (New_Subp, Subp);
15532 T : Entity_Id := Find_Dispatching_Type (Subp);
15534 while Etype (T) /= T loop
15535 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15536 Set_Is_Abstract_Subprogram (New_Subp, False);
15544 -- For instantiations this is not needed since the previous call to
15545 -- Derive_Subprogram leaves the entity well decorated.
15548 pragma Assert (Alias (New_Subp) = Actual_Subp);
15551 end Derive_Interface_Subprogram;
15555 Alias_Subp : Entity_Id;
15556 Act_List : Elist_Id;
15557 Act_Elmt : Elmt_Id;
15558 Act_Subp : Entity_Id := Empty;
15560 Need_Search : Boolean := False;
15561 New_Subp : Entity_Id := Empty;
15562 Parent_Base : Entity_Id;
15565 -- Start of processing for Derive_Subprograms
15568 if Ekind (Parent_Type) = E_Record_Type_With_Private
15569 and then Has_Discriminants (Parent_Type)
15570 and then Present (Full_View (Parent_Type))
15572 Parent_Base := Full_View (Parent_Type);
15574 Parent_Base := Parent_Type;
15577 if Present (Generic_Actual) then
15578 Act_List := Collect_Primitive_Operations (Generic_Actual);
15579 Act_Elmt := First_Elmt (Act_List);
15581 Act_List := No_Elist;
15582 Act_Elmt := No_Elmt;
15585 -- Derive primitives inherited from the parent. Note that if the generic
15586 -- actual is present, this is not really a type derivation, it is a
15587 -- completion within an instance.
15589 -- Case 1: Derived_Type does not implement interfaces
15591 if not Is_Tagged_Type (Derived_Type)
15592 or else (not Has_Interfaces (Derived_Type)
15593 and then not (Present (Generic_Actual)
15594 and then Has_Interfaces (Generic_Actual)))
15596 Elmt := First_Elmt (Op_List);
15597 while Present (Elmt) loop
15598 Subp := Node (Elmt);
15600 -- Literals are derived earlier in the process of building the
15601 -- derived type, and are skipped here.
15603 if Ekind (Subp) = E_Enumeration_Literal then
15606 -- The actual is a direct descendant and the common primitive
15607 -- operations appear in the same order.
15609 -- If the generic parent type is present, the derived type is an
15610 -- instance of a formal derived type, and within the instance its
15611 -- operations are those of the actual. We derive from the formal
15612 -- type but make the inherited operations aliases of the
15613 -- corresponding operations of the actual.
15616 pragma Assert (No (Node (Act_Elmt))
15617 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15620 (Subp, Node (Act_Elmt),
15621 Skip_Controlling_Formals => True)));
15624 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15626 if Present (Act_Elmt) then
15627 Next_Elmt (Act_Elmt);
15634 -- Case 2: Derived_Type implements interfaces
15637 -- If the parent type has no predefined primitives we remove
15638 -- predefined primitives from the list of primitives of generic
15639 -- actual to simplify the complexity of this algorithm.
15641 if Present (Generic_Actual) then
15643 Has_Predefined_Primitives : Boolean := False;
15646 -- Check if the parent type has predefined primitives
15648 Elmt := First_Elmt (Op_List);
15649 while Present (Elmt) loop
15650 Subp := Node (Elmt);
15652 if Is_Predefined_Dispatching_Operation (Subp)
15653 and then not Comes_From_Source (Ultimate_Alias (Subp))
15655 Has_Predefined_Primitives := True;
15662 -- Remove predefined primitives of Generic_Actual. We must use
15663 -- an auxiliary list because in case of tagged types the value
15664 -- returned by Collect_Primitive_Operations is the value stored
15665 -- in its Primitive_Operations attribute (and we don't want to
15666 -- modify its current contents).
15668 if not Has_Predefined_Primitives then
15670 Aux_List : constant Elist_Id := New_Elmt_List;
15673 Elmt := First_Elmt (Act_List);
15674 while Present (Elmt) loop
15675 Subp := Node (Elmt);
15677 if not Is_Predefined_Dispatching_Operation (Subp)
15678 or else Comes_From_Source (Subp)
15680 Append_Elmt (Subp, Aux_List);
15686 Act_List := Aux_List;
15690 Act_Elmt := First_Elmt (Act_List);
15691 Act_Subp := Node (Act_Elmt);
15695 -- Stage 1: If the generic actual is not present we derive the
15696 -- primitives inherited from the parent type. If the generic parent
15697 -- type is present, the derived type is an instance of a formal
15698 -- derived type, and within the instance its operations are those of
15699 -- the actual. We derive from the formal type but make the inherited
15700 -- operations aliases of the corresponding operations of the actual.
15702 Elmt := First_Elmt (Op_List);
15703 while Present (Elmt) loop
15704 Subp := Node (Elmt);
15705 Alias_Subp := Ultimate_Alias (Subp);
15707 -- Do not derive internal entities of the parent that link
15708 -- interface primitives with their covering primitive. These
15709 -- entities will be added to this type when frozen.
15711 if Present (Interface_Alias (Subp)) then
15715 -- If the generic actual is present find the corresponding
15716 -- operation in the generic actual. If the parent type is a
15717 -- direct ancestor of the derived type then, even if it is an
15718 -- interface, the operations are inherited from the primary
15719 -- dispatch table and are in the proper order. If we detect here
15720 -- that primitives are not in the same order we traverse the list
15721 -- of primitive operations of the actual to find the one that
15722 -- implements the interface primitive.
15726 (Present (Generic_Actual)
15727 and then Present (Act_Subp)
15729 (Primitive_Names_Match (Subp, Act_Subp)
15731 Type_Conformant (Subp, Act_Subp,
15732 Skip_Controlling_Formals => True)))
15734 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15735 Use_Full_View => True));
15737 -- Remember that we need searching for all pending primitives
15739 Need_Search := True;
15741 -- Handle entities associated with interface primitives
15743 if Present (Alias_Subp)
15744 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15745 and then not Is_Predefined_Dispatching_Operation (Subp)
15747 -- Search for the primitive in the homonym chain
15750 Find_Primitive_Covering_Interface
15751 (Tagged_Type => Generic_Actual,
15752 Iface_Prim => Alias_Subp);
15754 -- Previous search may not locate primitives covering
15755 -- interfaces defined in generics units or instantiations.
15756 -- (it fails if the covering primitive has formals whose
15757 -- type is also defined in generics or instantiations).
15758 -- In such case we search in the list of primitives of the
15759 -- generic actual for the internal entity that links the
15760 -- interface primitive and the covering primitive.
15763 and then Is_Generic_Type (Parent_Type)
15765 -- This code has been designed to handle only generic
15766 -- formals that implement interfaces that are defined
15767 -- in a generic unit or instantiation. If this code is
15768 -- needed for other cases we must review it because
15769 -- (given that it relies on Original_Location to locate
15770 -- the primitive of Generic_Actual that covers the
15771 -- interface) it could leave linked through attribute
15772 -- Alias entities of unrelated instantiations).
15776 (Scope (Find_Dispatching_Type (Alias_Subp)))
15778 Instantiation_Depth
15779 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15782 Iface_Prim_Loc : constant Source_Ptr :=
15783 Original_Location (Sloc (Alias_Subp));
15790 First_Elmt (Primitive_Operations (Generic_Actual));
15792 Search : while Present (Elmt) loop
15793 Prim := Node (Elmt);
15795 if Present (Interface_Alias (Prim))
15796 and then Original_Location
15797 (Sloc (Interface_Alias (Prim))) =
15800 Act_Subp := Alias (Prim);
15809 pragma Assert (Present (Act_Subp)
15810 or else Is_Abstract_Type (Generic_Actual)
15811 or else Serious_Errors_Detected > 0);
15813 -- Handle predefined primitives plus the rest of user-defined
15817 Act_Elmt := First_Elmt (Act_List);
15818 while Present (Act_Elmt) loop
15819 Act_Subp := Node (Act_Elmt);
15821 exit when Primitive_Names_Match (Subp, Act_Subp)
15822 and then Type_Conformant
15824 Skip_Controlling_Formals => True)
15825 and then No (Interface_Alias (Act_Subp));
15827 Next_Elmt (Act_Elmt);
15830 if No (Act_Elmt) then
15836 -- Case 1: If the parent is a limited interface then it has the
15837 -- predefined primitives of synchronized interfaces. However, the
15838 -- actual type may be a non-limited type and hence it does not
15839 -- have such primitives.
15841 if Present (Generic_Actual)
15842 and then not Present (Act_Subp)
15843 and then Is_Limited_Interface (Parent_Base)
15844 and then Is_Predefined_Interface_Primitive (Subp)
15848 -- Case 2: Inherit entities associated with interfaces that were
15849 -- not covered by the parent type. We exclude here null interface
15850 -- primitives because they do not need special management.
15852 -- We also exclude interface operations that are renamings. If the
15853 -- subprogram is an explicit renaming of an interface primitive,
15854 -- it is a regular primitive operation, and the presence of its
15855 -- alias is not relevant: it has to be derived like any other
15858 elsif Present (Alias (Subp))
15859 and then Nkind (Unit_Declaration_Node (Subp)) /=
15860 N_Subprogram_Renaming_Declaration
15861 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15863 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15864 and then Null_Present (Parent (Alias_Subp)))
15866 -- If this is an abstract private type then we transfer the
15867 -- derivation of the interface primitive from the partial view
15868 -- to the full view. This is safe because all the interfaces
15869 -- must be visible in the partial view. Done to avoid adding
15870 -- a new interface derivation to the private part of the
15871 -- enclosing package; otherwise this new derivation would be
15872 -- decorated as hidden when the analysis of the enclosing
15873 -- package completes.
15875 if Is_Abstract_Type (Derived_Type)
15876 and then In_Private_Part (Current_Scope)
15877 and then Has_Private_Declaration (Derived_Type)
15880 Partial_View : Entity_Id;
15885 Partial_View := First_Entity (Current_Scope);
15887 exit when No (Partial_View)
15888 or else (Has_Private_Declaration (Partial_View)
15890 Full_View (Partial_View) = Derived_Type);
15892 Next_Entity (Partial_View);
15895 -- If the partial view was not found then the source code
15896 -- has errors and the derivation is not needed.
15898 if Present (Partial_View) then
15900 First_Elmt (Primitive_Operations (Partial_View));
15901 while Present (Elmt) loop
15902 Ent := Node (Elmt);
15904 if Present (Alias (Ent))
15905 and then Ultimate_Alias (Ent) = Alias (Subp)
15908 (Ent, Primitive_Operations (Derived_Type));
15915 -- If the interface primitive was not found in the
15916 -- partial view then this interface primitive was
15917 -- overridden. We add a derivation to activate in
15918 -- Derive_Progenitor_Subprograms the machinery to
15922 Derive_Interface_Subprogram
15923 (New_Subp => New_Subp,
15925 Actual_Subp => Act_Subp);
15930 Derive_Interface_Subprogram
15931 (New_Subp => New_Subp,
15933 Actual_Subp => Act_Subp);
15936 -- Case 3: Common derivation
15940 (New_Subp => New_Subp,
15941 Parent_Subp => Subp,
15942 Derived_Type => Derived_Type,
15943 Parent_Type => Parent_Base,
15944 Actual_Subp => Act_Subp);
15947 -- No need to update Act_Elm if we must search for the
15948 -- corresponding operation in the generic actual
15951 and then Present (Act_Elmt)
15953 Next_Elmt (Act_Elmt);
15954 Act_Subp := Node (Act_Elmt);
15961 -- Inherit additional operations from progenitors. If the derived
15962 -- type is a generic actual, there are not new primitive operations
15963 -- for the type because it has those of the actual, and therefore
15964 -- nothing needs to be done. The renamings generated above are not
15965 -- primitive operations, and their purpose is simply to make the
15966 -- proper operations visible within an instantiation.
15968 if No (Generic_Actual) then
15969 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15973 -- Final check: Direct descendants must have their primitives in the
15974 -- same order. We exclude from this test untagged types and instances
15975 -- of formal derived types. We skip this test if we have already
15976 -- reported serious errors in the sources.
15978 pragma Assert (not Is_Tagged_Type (Derived_Type)
15979 or else Present (Generic_Actual)
15980 or else Serious_Errors_Detected > 0
15981 or else Check_Derived_Type);
15982 end Derive_Subprograms;
15984 --------------------------------
15985 -- Derived_Standard_Character --
15986 --------------------------------
15988 procedure Derived_Standard_Character
15990 Parent_Type : Entity_Id;
15991 Derived_Type : Entity_Id)
15993 Loc : constant Source_Ptr := Sloc (N);
15994 Def : constant Node_Id := Type_Definition (N);
15995 Indic : constant Node_Id := Subtype_Indication (Def);
15996 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15997 Implicit_Base : constant Entity_Id :=
15999 (E_Enumeration_Type, N, Derived_Type, 'B');
16005 Discard_Node (Process_Subtype (Indic, N));
16007 Set_Etype (Implicit_Base, Parent_Base);
16008 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16009 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16011 Set_Is_Character_Type (Implicit_Base, True);
16012 Set_Has_Delayed_Freeze (Implicit_Base);
16014 -- The bounds of the implicit base are the bounds of the parent base.
16015 -- Note that their type is the parent base.
16017 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16018 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16020 Set_Scalar_Range (Implicit_Base,
16023 High_Bound => Hi));
16025 Conditional_Delay (Derived_Type, Parent_Type);
16027 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16028 Set_Etype (Derived_Type, Implicit_Base);
16029 Set_Size_Info (Derived_Type, Parent_Type);
16031 if Unknown_RM_Size (Derived_Type) then
16032 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16035 Set_Is_Character_Type (Derived_Type, True);
16037 if Nkind (Indic) /= N_Subtype_Indication then
16039 -- If no explicit constraint, the bounds are those
16040 -- of the parent type.
16042 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16043 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16044 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16047 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16049 -- Because the implicit base is used in the conversion of the bounds, we
16050 -- have to freeze it now. This is similar to what is done for numeric
16051 -- types, and it equally suspicious, but otherwise a non-static bound
16052 -- will have a reference to an unfrozen type, which is rejected by Gigi
16053 -- (???). This requires specific care for definition of stream
16054 -- attributes. For details, see comments at the end of
16055 -- Build_Derived_Numeric_Type.
16057 Freeze_Before (N, Implicit_Base);
16058 end Derived_Standard_Character;
16060 ------------------------------
16061 -- Derived_Type_Declaration --
16062 ------------------------------
16064 procedure Derived_Type_Declaration
16067 Is_Completion : Boolean)
16069 Parent_Type : Entity_Id;
16071 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16072 -- Check whether the parent type is a generic formal, or derives
16073 -- directly or indirectly from one.
16075 ------------------------
16076 -- Comes_From_Generic --
16077 ------------------------
16079 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16081 if Is_Generic_Type (Typ) then
16084 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16087 elsif Is_Private_Type (Typ)
16088 and then Present (Full_View (Typ))
16089 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16093 elsif Is_Generic_Actual_Type (Typ) then
16099 end Comes_From_Generic;
16103 Def : constant Node_Id := Type_Definition (N);
16104 Iface_Def : Node_Id;
16105 Indic : constant Node_Id := Subtype_Indication (Def);
16106 Extension : constant Node_Id := Record_Extension_Part (Def);
16107 Parent_Node : Node_Id;
16110 -- Start of processing for Derived_Type_Declaration
16113 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16115 -- Ada 2005 (AI-251): In case of interface derivation check that the
16116 -- parent is also an interface.
16118 if Interface_Present (Def) then
16119 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16121 if not Is_Interface (Parent_Type) then
16122 Diagnose_Interface (Indic, Parent_Type);
16125 Parent_Node := Parent (Base_Type (Parent_Type));
16126 Iface_Def := Type_Definition (Parent_Node);
16128 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16129 -- other limited interfaces.
16131 if Limited_Present (Def) then
16132 if Limited_Present (Iface_Def) then
16135 elsif Protected_Present (Iface_Def) then
16137 ("descendant of & must be declared as a protected "
16138 & "interface", N, Parent_Type);
16140 elsif Synchronized_Present (Iface_Def) then
16142 ("descendant of & must be declared as a synchronized "
16143 & "interface", N, Parent_Type);
16145 elsif Task_Present (Iface_Def) then
16147 ("descendant of & must be declared as a task interface",
16152 ("(Ada 2005) limited interface cannot inherit from "
16153 & "non-limited interface", Indic);
16156 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16157 -- from non-limited or limited interfaces.
16159 elsif not Protected_Present (Def)
16160 and then not Synchronized_Present (Def)
16161 and then not Task_Present (Def)
16163 if Limited_Present (Iface_Def) then
16166 elsif Protected_Present (Iface_Def) then
16168 ("descendant of & must be declared as a protected "
16169 & "interface", N, Parent_Type);
16171 elsif Synchronized_Present (Iface_Def) then
16173 ("descendant of & must be declared as a synchronized "
16174 & "interface", N, Parent_Type);
16176 elsif Task_Present (Iface_Def) then
16178 ("descendant of & must be declared as a task interface",
16187 if Is_Tagged_Type (Parent_Type)
16188 and then Is_Concurrent_Type (Parent_Type)
16189 and then not Is_Interface (Parent_Type)
16192 ("parent type of a record extension cannot be a synchronized "
16193 & "tagged type (RM 3.9.1 (3/1))", N);
16194 Set_Etype (T, Any_Type);
16198 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16201 if Is_Tagged_Type (Parent_Type)
16202 and then Is_Non_Empty_List (Interface_List (Def))
16209 Intf := First (Interface_List (Def));
16210 while Present (Intf) loop
16211 T := Find_Type_Of_Subtype_Indic (Intf);
16213 if not Is_Interface (T) then
16214 Diagnose_Interface (Intf, T);
16216 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16217 -- a limited type from having a nonlimited progenitor.
16219 elsif (Limited_Present (Def)
16220 or else (not Is_Interface (Parent_Type)
16221 and then Is_Limited_Type (Parent_Type)))
16222 and then not Is_Limited_Interface (T)
16225 ("progenitor interface& of limited type must be limited",
16234 if Parent_Type = Any_Type
16235 or else Etype (Parent_Type) = Any_Type
16236 or else (Is_Class_Wide_Type (Parent_Type)
16237 and then Etype (Parent_Type) = T)
16239 -- If Parent_Type is undefined or illegal, make new type into a
16240 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16241 -- errors. If this is a self-definition, emit error now.
16243 if T = Parent_Type or else T = Etype (Parent_Type) then
16244 Error_Msg_N ("type cannot be used in its own definition", Indic);
16247 Set_Ekind (T, Ekind (Parent_Type));
16248 Set_Etype (T, Any_Type);
16249 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16251 if Is_Tagged_Type (T)
16252 and then Is_Record_Type (T)
16254 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16260 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16261 -- an interface is special because the list of interfaces in the full
16262 -- view can be given in any order. For example:
16264 -- type A is interface;
16265 -- type B is interface and A;
16266 -- type D is new B with private;
16268 -- type D is new A and B with null record; -- 1 --
16270 -- In this case we perform the following transformation of -1-:
16272 -- type D is new B and A with null record;
16274 -- If the parent of the full-view covers the parent of the partial-view
16275 -- we have two possible cases:
16277 -- 1) They have the same parent
16278 -- 2) The parent of the full-view implements some further interfaces
16280 -- In both cases we do not need to perform the transformation. In the
16281 -- first case the source program is correct and the transformation is
16282 -- not needed; in the second case the source program does not fulfill
16283 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16286 -- This transformation not only simplifies the rest of the analysis of
16287 -- this type declaration but also simplifies the correct generation of
16288 -- the object layout to the expander.
16290 if In_Private_Part (Current_Scope)
16291 and then Is_Interface (Parent_Type)
16295 Partial_View : Entity_Id;
16296 Partial_View_Parent : Entity_Id;
16297 New_Iface : Node_Id;
16300 -- Look for the associated private type declaration
16302 Partial_View := First_Entity (Current_Scope);
16304 exit when No (Partial_View)
16305 or else (Has_Private_Declaration (Partial_View)
16306 and then Full_View (Partial_View) = T);
16308 Next_Entity (Partial_View);
16311 -- If the partial view was not found then the source code has
16312 -- errors and the transformation is not needed.
16314 if Present (Partial_View) then
16315 Partial_View_Parent := Etype (Partial_View);
16317 -- If the parent of the full-view covers the parent of the
16318 -- partial-view we have nothing else to do.
16320 if Interface_Present_In_Ancestor
16321 (Parent_Type, Partial_View_Parent)
16325 -- Traverse the list of interfaces of the full-view to look
16326 -- for the parent of the partial-view and perform the tree
16330 Iface := First (Interface_List (Def));
16331 while Present (Iface) loop
16332 if Etype (Iface) = Etype (Partial_View) then
16333 Rewrite (Subtype_Indication (Def),
16334 New_Copy (Subtype_Indication
16335 (Parent (Partial_View))));
16338 Make_Identifier (Sloc (N), Chars (Parent_Type));
16339 Append (New_Iface, Interface_List (Def));
16341 -- Analyze the transformed code
16343 Derived_Type_Declaration (T, N, Is_Completion);
16354 -- Only composite types other than array types are allowed to have
16357 if Present (Discriminant_Specifications (N)) then
16358 if (Is_Elementary_Type (Parent_Type)
16360 Is_Array_Type (Parent_Type))
16361 and then not Error_Posted (N)
16364 ("elementary or array type cannot have discriminants",
16365 Defining_Identifier (First (Discriminant_Specifications (N))));
16366 Set_Has_Discriminants (T, False);
16368 -- The type is allowed to have discriminants
16371 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16375 -- In Ada 83, a derived type defined in a package specification cannot
16376 -- be used for further derivation until the end of its visible part.
16377 -- Note that derivation in the private part of the package is allowed.
16379 if Ada_Version = Ada_83
16380 and then Is_Derived_Type (Parent_Type)
16381 and then In_Visible_Part (Scope (Parent_Type))
16383 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16385 ("(Ada 83): premature use of type for derivation", Indic);
16389 -- Check for early use of incomplete or private type
16391 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16392 Error_Msg_N ("premature derivation of incomplete type", Indic);
16395 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16396 and then not Comes_From_Generic (Parent_Type))
16397 or else Has_Private_Component (Parent_Type)
16399 -- The ancestor type of a formal type can be incomplete, in which
16400 -- case only the operations of the partial view are available in the
16401 -- generic. Subsequent checks may be required when the full view is
16402 -- analyzed to verify that a derivation from a tagged type has an
16405 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16408 elsif No (Underlying_Type (Parent_Type))
16409 or else Has_Private_Component (Parent_Type)
16412 ("premature derivation of derived or private type", Indic);
16414 -- Flag the type itself as being in error, this prevents some
16415 -- nasty problems with subsequent uses of the malformed type.
16417 Set_Error_Posted (T);
16419 -- Check that within the immediate scope of an untagged partial
16420 -- view it's illegal to derive from the partial view if the
16421 -- full view is tagged. (7.3(7))
16423 -- We verify that the Parent_Type is a partial view by checking
16424 -- that it is not a Full_Type_Declaration (i.e. a private type or
16425 -- private extension declaration), to distinguish a partial view
16426 -- from a derivation from a private type which also appears as
16427 -- E_Private_Type. If the parent base type is not declared in an
16428 -- enclosing scope there is no need to check.
16430 elsif Present (Full_View (Parent_Type))
16431 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16432 and then not Is_Tagged_Type (Parent_Type)
16433 and then Is_Tagged_Type (Full_View (Parent_Type))
16434 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16437 ("premature derivation from type with tagged full view",
16442 -- Check that form of derivation is appropriate
16444 Taggd := Is_Tagged_Type (Parent_Type);
16446 -- Set the parent type to the class-wide type's specific type in this
16447 -- case to prevent cascading errors
16449 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16450 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16451 Set_Etype (T, Etype (Parent_Type));
16455 if Present (Extension) and then not Taggd then
16457 ("type derived from untagged type cannot have extension", Indic);
16459 elsif No (Extension) and then Taggd then
16461 -- If this declaration is within a private part (or body) of a
16462 -- generic instantiation then the derivation is allowed (the parent
16463 -- type can only appear tagged in this case if it's a generic actual
16464 -- type, since it would otherwise have been rejected in the analysis
16465 -- of the generic template).
16467 if not Is_Generic_Actual_Type (Parent_Type)
16468 or else In_Visible_Part (Scope (Parent_Type))
16470 if Is_Class_Wide_Type (Parent_Type) then
16472 ("parent type must not be a class-wide type", Indic);
16474 -- Use specific type to prevent cascaded errors.
16476 Parent_Type := Etype (Parent_Type);
16480 ("type derived from tagged type must have extension", Indic);
16485 -- AI-443: Synchronized formal derived types require a private
16486 -- extension. There is no point in checking the ancestor type or
16487 -- the progenitors since the construct is wrong to begin with.
16489 if Ada_Version >= Ada_2005
16490 and then Is_Generic_Type (T)
16491 and then Present (Original_Node (N))
16494 Decl : constant Node_Id := Original_Node (N);
16497 if Nkind (Decl) = N_Formal_Type_Declaration
16498 and then Nkind (Formal_Type_Definition (Decl)) =
16499 N_Formal_Derived_Type_Definition
16500 and then Synchronized_Present (Formal_Type_Definition (Decl))
16501 and then No (Extension)
16503 -- Avoid emitting a duplicate error message
16505 and then not Error_Posted (Indic)
16508 ("synchronized derived type must have extension", N);
16513 if Null_Exclusion_Present (Def)
16514 and then not Is_Access_Type (Parent_Type)
16516 Error_Msg_N ("null exclusion can only apply to an access type", N);
16519 -- Avoid deriving parent primitives of underlying record views
16521 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16522 Derive_Subps => not Is_Underlying_Record_View (T));
16524 -- AI-419: The parent type of an explicitly limited derived type must
16525 -- be a limited type or a limited interface.
16527 if Limited_Present (Def) then
16528 Set_Is_Limited_Record (T);
16530 if Is_Interface (T) then
16531 Set_Is_Limited_Interface (T);
16534 if not Is_Limited_Type (Parent_Type)
16536 (not Is_Interface (Parent_Type)
16537 or else not Is_Limited_Interface (Parent_Type))
16539 -- AI05-0096: a derivation in the private part of an instance is
16540 -- legal if the generic formal is untagged limited, and the actual
16543 if Is_Generic_Actual_Type (Parent_Type)
16544 and then In_Private_Part (Current_Scope)
16547 (Generic_Parent_Type (Parent (Parent_Type)))
16553 ("parent type& of limited type must be limited",
16559 -- In SPARK, there are no derived type definitions other than type
16560 -- extensions of tagged record types.
16562 if No (Extension) then
16563 Check_SPARK_05_Restriction
16564 ("derived type is not allowed", Original_Node (N));
16566 end Derived_Type_Declaration;
16568 ------------------------
16569 -- Diagnose_Interface --
16570 ------------------------
16572 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16574 if not Is_Interface (E) and then E /= Any_Type then
16575 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16577 end Diagnose_Interface;
16579 ----------------------------------
16580 -- Enumeration_Type_Declaration --
16581 ----------------------------------
16583 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16590 -- Create identifier node representing lower bound
16592 B_Node := New_Node (N_Identifier, Sloc (Def));
16593 L := First (Literals (Def));
16594 Set_Chars (B_Node, Chars (L));
16595 Set_Entity (B_Node, L);
16596 Set_Etype (B_Node, T);
16597 Set_Is_Static_Expression (B_Node, True);
16599 R_Node := New_Node (N_Range, Sloc (Def));
16600 Set_Low_Bound (R_Node, B_Node);
16602 Set_Ekind (T, E_Enumeration_Type);
16603 Set_First_Literal (T, L);
16605 Set_Is_Constrained (T);
16609 -- Loop through literals of enumeration type setting pos and rep values
16610 -- except that if the Ekind is already set, then it means the literal
16611 -- was already constructed (case of a derived type declaration and we
16612 -- should not disturb the Pos and Rep values.
16614 while Present (L) loop
16615 if Ekind (L) /= E_Enumeration_Literal then
16616 Set_Ekind (L, E_Enumeration_Literal);
16617 Set_Enumeration_Pos (L, Ev);
16618 Set_Enumeration_Rep (L, Ev);
16619 Set_Is_Known_Valid (L, True);
16623 New_Overloaded_Entity (L);
16624 Generate_Definition (L);
16625 Set_Convention (L, Convention_Intrinsic);
16627 -- Case of character literal
16629 if Nkind (L) = N_Defining_Character_Literal then
16630 Set_Is_Character_Type (T, True);
16632 -- Check violation of No_Wide_Characters
16634 if Restriction_Check_Required (No_Wide_Characters) then
16635 Get_Name_String (Chars (L));
16637 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16638 Check_Restriction (No_Wide_Characters, L);
16647 -- Now create a node representing upper bound
16649 B_Node := New_Node (N_Identifier, Sloc (Def));
16650 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16651 Set_Entity (B_Node, Last (Literals (Def)));
16652 Set_Etype (B_Node, T);
16653 Set_Is_Static_Expression (B_Node, True);
16655 Set_High_Bound (R_Node, B_Node);
16657 -- Initialize various fields of the type. Some of this information
16658 -- may be overwritten later through rep.clauses.
16660 Set_Scalar_Range (T, R_Node);
16661 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16662 Set_Enum_Esize (T);
16663 Set_Enum_Pos_To_Rep (T, Empty);
16665 -- Set Discard_Names if configuration pragma set, or if there is
16666 -- a parameterless pragma in the current declarative region
16668 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16669 Set_Discard_Names (T);
16672 -- Process end label if there is one
16674 if Present (Def) then
16675 Process_End_Label (Def, 'e', T);
16677 end Enumeration_Type_Declaration;
16679 ---------------------------------
16680 -- Expand_To_Stored_Constraint --
16681 ---------------------------------
16683 function Expand_To_Stored_Constraint
16685 Constraint : Elist_Id) return Elist_Id
16687 Explicitly_Discriminated_Type : Entity_Id;
16688 Expansion : Elist_Id;
16689 Discriminant : Entity_Id;
16691 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16692 -- Find the nearest type that actually specifies discriminants
16694 ---------------------------------
16695 -- Type_With_Explicit_Discrims --
16696 ---------------------------------
16698 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16699 Typ : constant E := Base_Type (Id);
16702 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16703 if Present (Full_View (Typ)) then
16704 return Type_With_Explicit_Discrims (Full_View (Typ));
16708 if Has_Discriminants (Typ) then
16713 if Etype (Typ) = Typ then
16715 elsif Has_Discriminants (Typ) then
16718 return Type_With_Explicit_Discrims (Etype (Typ));
16721 end Type_With_Explicit_Discrims;
16723 -- Start of processing for Expand_To_Stored_Constraint
16726 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16730 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16732 if No (Explicitly_Discriminated_Type) then
16736 Expansion := New_Elmt_List;
16739 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16740 while Present (Discriminant) loop
16742 (Get_Discriminant_Value
16743 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16745 Next_Stored_Discriminant (Discriminant);
16749 end Expand_To_Stored_Constraint;
16751 ---------------------------
16752 -- Find_Hidden_Interface --
16753 ---------------------------
16755 function Find_Hidden_Interface
16757 Dest : Elist_Id) return Entity_Id
16760 Iface_Elmt : Elmt_Id;
16763 if Present (Src) and then Present (Dest) then
16764 Iface_Elmt := First_Elmt (Src);
16765 while Present (Iface_Elmt) loop
16766 Iface := Node (Iface_Elmt);
16768 if Is_Interface (Iface)
16769 and then not Contain_Interface (Iface, Dest)
16774 Next_Elmt (Iface_Elmt);
16779 end Find_Hidden_Interface;
16781 --------------------
16782 -- Find_Type_Name --
16783 --------------------
16785 function Find_Type_Name (N : Node_Id) return Entity_Id is
16786 Id : constant Entity_Id := Defining_Identifier (N);
16787 New_Id : Entity_Id;
16789 Prev_Par : Node_Id;
16791 procedure Check_Duplicate_Aspects;
16792 -- Check that aspects specified in a completion have not been specified
16793 -- already in the partial view.
16795 procedure Tag_Mismatch;
16796 -- Diagnose a tagged partial view whose full view is untagged. We post
16797 -- the message on the full view, with a reference to the previous
16798 -- partial view. The partial view can be private or incomplete, and
16799 -- these are handled in a different manner, so we determine the position
16800 -- of the error message from the respective slocs of both.
16802 -----------------------------
16803 -- Check_Duplicate_Aspects --
16804 -----------------------------
16806 procedure Check_Duplicate_Aspects is
16807 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
16808 -- Return the corresponding aspect of the partial view which matches
16809 -- the aspect id of Asp. Return Empty is no such aspect exists.
16811 -----------------------------
16812 -- Get_Partial_View_Aspect --
16813 -----------------------------
16815 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
16816 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
16817 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
16818 Prev_Asp : Node_Id;
16821 if Present (Prev_Asps) then
16822 Prev_Asp := First (Prev_Asps);
16823 while Present (Prev_Asp) loop
16824 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
16833 end Get_Partial_View_Aspect;
16837 Full_Asps : constant List_Id := Aspect_Specifications (N);
16838 Full_Asp : Node_Id;
16839 Part_Asp : Node_Id;
16841 -- Start of processing for Check_Duplicate_Aspects
16844 if Present (Full_Asps) then
16845 Full_Asp := First (Full_Asps);
16846 while Present (Full_Asp) loop
16847 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
16849 -- An aspect and its class-wide counterpart are two distinct
16850 -- aspects and may apply to both views of an entity.
16852 if Present (Part_Asp)
16853 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
16856 ("aspect already specified in private declaration",
16863 if Has_Discriminants (Prev)
16864 and then not Has_Unknown_Discriminants (Prev)
16865 and then Get_Aspect_Id (Full_Asp) =
16866 Aspect_Implicit_Dereference
16869 ("cannot specify aspect if partial view has known "
16870 & "discriminants", Full_Asp);
16876 end Check_Duplicate_Aspects;
16882 procedure Tag_Mismatch is
16884 if Sloc (Prev) < Sloc (Id) then
16885 if Ada_Version >= Ada_2012
16886 and then Nkind (N) = N_Private_Type_Declaration
16889 ("declaration of private } must be a tagged type ", Id, Prev);
16892 ("full declaration of } must be a tagged type ", Id, Prev);
16896 if Ada_Version >= Ada_2012
16897 and then Nkind (N) = N_Private_Type_Declaration
16900 ("declaration of private } must be a tagged type ", Prev, Id);
16903 ("full declaration of } must be a tagged type ", Prev, Id);
16908 -- Start of processing for Find_Type_Name
16911 -- Find incomplete declaration, if one was given
16913 Prev := Current_Entity_In_Scope (Id);
16915 -- New type declaration
16921 -- Previous declaration exists
16924 Prev_Par := Parent (Prev);
16926 -- Error if not incomplete/private case except if previous
16927 -- declaration is implicit, etc. Enter_Name will emit error if
16930 if not Is_Incomplete_Or_Private_Type (Prev) then
16934 -- Check invalid completion of private or incomplete type
16936 elsif not Nkind_In (N, N_Full_Type_Declaration,
16937 N_Task_Type_Declaration,
16938 N_Protected_Type_Declaration)
16940 (Ada_Version < Ada_2012
16941 or else not Is_Incomplete_Type (Prev)
16942 or else not Nkind_In (N, N_Private_Type_Declaration,
16943 N_Private_Extension_Declaration))
16945 -- Completion must be a full type declarations (RM 7.3(4))
16947 Error_Msg_Sloc := Sloc (Prev);
16948 Error_Msg_NE ("invalid completion of }", Id, Prev);
16950 -- Set scope of Id to avoid cascaded errors. Entity is never
16951 -- examined again, except when saving globals in generics.
16953 Set_Scope (Id, Current_Scope);
16956 -- If this is a repeated incomplete declaration, no further
16957 -- checks are possible.
16959 if Nkind (N) = N_Incomplete_Type_Declaration then
16963 -- Case of full declaration of incomplete type
16965 elsif Ekind (Prev) = E_Incomplete_Type
16966 and then (Ada_Version < Ada_2012
16967 or else No (Full_View (Prev))
16968 or else not Is_Private_Type (Full_View (Prev)))
16970 -- Indicate that the incomplete declaration has a matching full
16971 -- declaration. The defining occurrence of the incomplete
16972 -- declaration remains the visible one, and the procedure
16973 -- Get_Full_View dereferences it whenever the type is used.
16975 if Present (Full_View (Prev)) then
16976 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16979 Set_Full_View (Prev, Id);
16980 Append_Entity (Id, Current_Scope);
16981 Set_Is_Public (Id, Is_Public (Prev));
16982 Set_Is_Internal (Id);
16985 -- If the incomplete view is tagged, a class_wide type has been
16986 -- created already. Use it for the private type as well, in order
16987 -- to prevent multiple incompatible class-wide types that may be
16988 -- created for self-referential anonymous access components.
16990 if Is_Tagged_Type (Prev)
16991 and then Present (Class_Wide_Type (Prev))
16993 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16994 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16996 -- Type of the class-wide type is the current Id. Previously
16997 -- this was not done for private declarations because of order-
16998 -- of-elaboration issues in the back end, but gigi now handles
17001 Set_Etype (Class_Wide_Type (Id), Id);
17004 -- Case of full declaration of private type
17007 -- If the private type was a completion of an incomplete type then
17008 -- update Prev to reference the private type
17010 if Ada_Version >= Ada_2012
17011 and then Ekind (Prev) = E_Incomplete_Type
17012 and then Present (Full_View (Prev))
17013 and then Is_Private_Type (Full_View (Prev))
17015 Prev := Full_View (Prev);
17016 Prev_Par := Parent (Prev);
17019 if Nkind (N) = N_Full_Type_Declaration
17021 (Type_Definition (N), N_Record_Definition,
17022 N_Derived_Type_Definition)
17023 and then Interface_Present (Type_Definition (N))
17026 ("completion of private type cannot be an interface", N);
17029 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17030 if Etype (Prev) /= Prev then
17032 -- Prev is a private subtype or a derived type, and needs
17035 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17038 elsif Ekind (Prev) = E_Private_Type
17039 and then Nkind_In (N, N_Task_Type_Declaration,
17040 N_Protected_Type_Declaration)
17043 ("completion of nonlimited type cannot be limited", N);
17045 elsif Ekind (Prev) = E_Record_Type_With_Private
17046 and then Nkind_In (N, N_Task_Type_Declaration,
17047 N_Protected_Type_Declaration)
17049 if not Is_Limited_Record (Prev) then
17051 ("completion of nonlimited type cannot be limited", N);
17053 elsif No (Interface_List (N)) then
17055 ("completion of tagged private type must be tagged",
17060 -- Ada 2005 (AI-251): Private extension declaration of a task
17061 -- type or a protected type. This case arises when covering
17062 -- interface types.
17064 elsif Nkind_In (N, N_Task_Type_Declaration,
17065 N_Protected_Type_Declaration)
17069 elsif Nkind (N) /= N_Full_Type_Declaration
17070 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17073 ("full view of private extension must be an extension", N);
17075 elsif not (Abstract_Present (Parent (Prev)))
17076 and then Abstract_Present (Type_Definition (N))
17079 ("full view of non-abstract extension cannot be abstract", N);
17082 if not In_Private_Part (Current_Scope) then
17084 ("declaration of full view must appear in private part", N);
17087 if Ada_Version >= Ada_2012 then
17088 Check_Duplicate_Aspects;
17091 Copy_And_Swap (Prev, Id);
17092 Set_Has_Private_Declaration (Prev);
17093 Set_Has_Private_Declaration (Id);
17095 -- AI12-0133: Indicate whether we have a partial view with
17096 -- unknown discriminants, in which case initialization of objects
17097 -- of the type do not receive an invariant check.
17099 Set_Partial_View_Has_Unknown_Discr
17100 (Prev, Has_Unknown_Discriminants (Id));
17102 -- Preserve aspect and iterator flags that may have been set on
17103 -- the partial view.
17105 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17106 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17108 -- If no error, propagate freeze_node from private to full view.
17109 -- It may have been generated for an early operational item.
17111 if Present (Freeze_Node (Id))
17112 and then Serious_Errors_Detected = 0
17113 and then No (Full_View (Id))
17115 Set_Freeze_Node (Prev, Freeze_Node (Id));
17116 Set_Freeze_Node (Id, Empty);
17117 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17120 Set_Full_View (Id, Prev);
17124 -- Verify that full declaration conforms to partial one
17126 if Is_Incomplete_Or_Private_Type (Prev)
17127 and then Present (Discriminant_Specifications (Prev_Par))
17129 if Present (Discriminant_Specifications (N)) then
17130 if Ekind (Prev) = E_Incomplete_Type then
17131 Check_Discriminant_Conformance (N, Prev, Prev);
17133 Check_Discriminant_Conformance (N, Prev, Id);
17138 ("missing discriminants in full type declaration", N);
17140 -- To avoid cascaded errors on subsequent use, share the
17141 -- discriminants of the partial view.
17143 Set_Discriminant_Specifications (N,
17144 Discriminant_Specifications (Prev_Par));
17148 -- A prior untagged partial view can have an associated class-wide
17149 -- type due to use of the class attribute, and in this case the full
17150 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17151 -- of incomplete tagged declarations, but we check for it.
17154 and then (Is_Tagged_Type (Prev)
17155 or else Present (Class_Wide_Type (Prev)))
17157 -- Ada 2012 (AI05-0162): A private type may be the completion of
17158 -- an incomplete type.
17160 if Ada_Version >= Ada_2012
17161 and then Is_Incomplete_Type (Prev)
17162 and then Nkind_In (N, N_Private_Type_Declaration,
17163 N_Private_Extension_Declaration)
17165 -- No need to check private extensions since they are tagged
17167 if Nkind (N) = N_Private_Type_Declaration
17168 and then not Tagged_Present (N)
17173 -- The full declaration is either a tagged type (including
17174 -- a synchronized type that implements interfaces) or a
17175 -- type extension, otherwise this is an error.
17177 elsif Nkind_In (N, N_Task_Type_Declaration,
17178 N_Protected_Type_Declaration)
17180 if No (Interface_List (N)) and then not Error_Posted (N) then
17184 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17186 -- Indicate that the previous declaration (tagged incomplete
17187 -- or private declaration) requires the same on the full one.
17189 if not Tagged_Present (Type_Definition (N)) then
17191 Set_Is_Tagged_Type (Id);
17194 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17195 if No (Record_Extension_Part (Type_Definition (N))) then
17197 ("full declaration of } must be a record extension",
17200 -- Set some attributes to produce a usable full view
17202 Set_Is_Tagged_Type (Id);
17211 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17212 and then Present (Premature_Use (Parent (Prev)))
17214 Error_Msg_Sloc := Sloc (N);
17216 ("\full declaration #", Premature_Use (Parent (Prev)));
17221 end Find_Type_Name;
17223 -------------------------
17224 -- Find_Type_Of_Object --
17225 -------------------------
17227 function Find_Type_Of_Object
17228 (Obj_Def : Node_Id;
17229 Related_Nod : Node_Id) return Entity_Id
17231 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17232 P : Node_Id := Parent (Obj_Def);
17237 -- If the parent is a component_definition node we climb to the
17238 -- component_declaration node
17240 if Nkind (P) = N_Component_Definition then
17244 -- Case of an anonymous array subtype
17246 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17247 N_Unconstrained_Array_Definition)
17250 Array_Type_Declaration (T, Obj_Def);
17252 -- Create an explicit subtype whenever possible
17254 elsif Nkind (P) /= N_Component_Declaration
17255 and then Def_Kind = N_Subtype_Indication
17257 -- Base name of subtype on object name, which will be unique in
17258 -- the current scope.
17260 -- If this is a duplicate declaration, return base type, to avoid
17261 -- generating duplicate anonymous types.
17263 if Error_Posted (P) then
17264 Analyze (Subtype_Mark (Obj_Def));
17265 return Entity (Subtype_Mark (Obj_Def));
17270 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17272 T := Make_Defining_Identifier (Sloc (P), Nam);
17274 Insert_Action (Obj_Def,
17275 Make_Subtype_Declaration (Sloc (P),
17276 Defining_Identifier => T,
17277 Subtype_Indication => Relocate_Node (Obj_Def)));
17279 -- This subtype may need freezing, and this will not be done
17280 -- automatically if the object declaration is not in declarative
17281 -- part. Since this is an object declaration, the type cannot always
17282 -- be frozen here. Deferred constants do not freeze their type
17283 -- (which often enough will be private).
17285 if Nkind (P) = N_Object_Declaration
17286 and then Constant_Present (P)
17287 and then No (Expression (P))
17291 -- Here we freeze the base type of object type to catch premature use
17292 -- of discriminated private type without a full view.
17295 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17298 -- Ada 2005 AI-406: the object definition in an object declaration
17299 -- can be an access definition.
17301 elsif Def_Kind = N_Access_Definition then
17302 T := Access_Definition (Related_Nod, Obj_Def);
17304 Set_Is_Local_Anonymous_Access
17306 V => (Ada_Version < Ada_2012)
17307 or else (Nkind (P) /= N_Object_Declaration)
17308 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17310 -- Otherwise, the object definition is just a subtype_mark
17313 T := Process_Subtype (Obj_Def, Related_Nod);
17315 -- If expansion is disabled an object definition that is an aggregate
17316 -- will not get expanded and may lead to scoping problems in the back
17317 -- end, if the object is referenced in an inner scope. In that case
17318 -- create an itype reference for the object definition now. This
17319 -- may be redundant in some cases, but harmless.
17322 and then Nkind (Related_Nod) = N_Object_Declaration
17325 Build_Itype_Reference (T, Related_Nod);
17330 end Find_Type_Of_Object;
17332 --------------------------------
17333 -- Find_Type_Of_Subtype_Indic --
17334 --------------------------------
17336 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17340 -- Case of subtype mark with a constraint
17342 if Nkind (S) = N_Subtype_Indication then
17343 Find_Type (Subtype_Mark (S));
17344 Typ := Entity (Subtype_Mark (S));
17347 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17350 ("incorrect constraint for this kind of type", Constraint (S));
17351 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17354 -- Otherwise we have a subtype mark without a constraint
17356 elsif Error_Posted (S) then
17357 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17365 -- Check No_Wide_Characters restriction
17367 Check_Wide_Character_Restriction (Typ, S);
17370 end Find_Type_Of_Subtype_Indic;
17372 -------------------------------------
17373 -- Floating_Point_Type_Declaration --
17374 -------------------------------------
17376 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17377 Digs : constant Node_Id := Digits_Expression (Def);
17378 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17380 Base_Typ : Entity_Id;
17381 Implicit_Base : Entity_Id;
17384 function Can_Derive_From (E : Entity_Id) return Boolean;
17385 -- Find if given digits value, and possibly a specified range, allows
17386 -- derivation from specified type
17388 function Find_Base_Type return Entity_Id;
17389 -- Find a predefined base type that Def can derive from, or generate
17390 -- an error and substitute Long_Long_Float if none exists.
17392 ---------------------
17393 -- Can_Derive_From --
17394 ---------------------
17396 function Can_Derive_From (E : Entity_Id) return Boolean is
17397 Spec : constant Entity_Id := Real_Range_Specification (Def);
17400 -- Check specified "digits" constraint
17402 if Digs_Val > Digits_Value (E) then
17406 -- Check for matching range, if specified
17408 if Present (Spec) then
17409 if Expr_Value_R (Type_Low_Bound (E)) >
17410 Expr_Value_R (Low_Bound (Spec))
17415 if Expr_Value_R (Type_High_Bound (E)) <
17416 Expr_Value_R (High_Bound (Spec))
17423 end Can_Derive_From;
17425 --------------------
17426 -- Find_Base_Type --
17427 --------------------
17429 function Find_Base_Type return Entity_Id is
17430 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17433 -- Iterate over the predefined types in order, returning the first
17434 -- one that Def can derive from.
17436 while Present (Choice) loop
17437 if Can_Derive_From (Node (Choice)) then
17438 return Node (Choice);
17441 Next_Elmt (Choice);
17444 -- If we can't derive from any existing type, use Long_Long_Float
17445 -- and give appropriate message explaining the problem.
17447 if Digs_Val > Max_Digs_Val then
17448 -- It might be the case that there is a type with the requested
17449 -- range, just not the combination of digits and range.
17452 ("no predefined type has requested range and precision",
17453 Real_Range_Specification (Def));
17457 ("range too large for any predefined type",
17458 Real_Range_Specification (Def));
17461 return Standard_Long_Long_Float;
17462 end Find_Base_Type;
17464 -- Start of processing for Floating_Point_Type_Declaration
17467 Check_Restriction (No_Floating_Point, Def);
17469 -- Create an implicit base type
17472 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17474 -- Analyze and verify digits value
17476 Analyze_And_Resolve (Digs, Any_Integer);
17477 Check_Digits_Expression (Digs);
17478 Digs_Val := Expr_Value (Digs);
17480 -- Process possible range spec and find correct type to derive from
17482 Process_Real_Range_Specification (Def);
17484 -- Check that requested number of digits is not too high.
17486 if Digs_Val > Max_Digs_Val then
17488 -- The check for Max_Base_Digits may be somewhat expensive, as it
17489 -- requires reading System, so only do it when necessary.
17492 Max_Base_Digits : constant Uint :=
17495 (Parent (RTE (RE_Max_Base_Digits))));
17498 if Digs_Val > Max_Base_Digits then
17499 Error_Msg_Uint_1 := Max_Base_Digits;
17500 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17502 elsif No (Real_Range_Specification (Def)) then
17503 Error_Msg_Uint_1 := Max_Digs_Val;
17504 Error_Msg_N ("types with more than ^ digits need range spec "
17505 & "(RM 3.5.7(6))", Digs);
17510 -- Find a suitable type to derive from or complain and use a substitute
17512 Base_Typ := Find_Base_Type;
17514 -- If there are bounds given in the declaration use them as the bounds
17515 -- of the type, otherwise use the bounds of the predefined base type
17516 -- that was chosen based on the Digits value.
17518 if Present (Real_Range_Specification (Def)) then
17519 Set_Scalar_Range (T, Real_Range_Specification (Def));
17520 Set_Is_Constrained (T);
17522 -- The bounds of this range must be converted to machine numbers
17523 -- in accordance with RM 4.9(38).
17525 Bound := Type_Low_Bound (T);
17527 if Nkind (Bound) = N_Real_Literal then
17529 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17530 Set_Is_Machine_Number (Bound);
17533 Bound := Type_High_Bound (T);
17535 if Nkind (Bound) = N_Real_Literal then
17537 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17538 Set_Is_Machine_Number (Bound);
17542 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17545 -- Complete definition of implicit base and declared first subtype. The
17546 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17547 -- are not clobbered when the floating point type acts as a full view of
17550 Set_Etype (Implicit_Base, Base_Typ);
17551 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17552 Set_Size_Info (Implicit_Base, Base_Typ);
17553 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17554 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17555 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17556 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17558 Set_Ekind (T, E_Floating_Point_Subtype);
17559 Set_Etype (T, Implicit_Base);
17560 Set_Size_Info (T, Implicit_Base);
17561 Set_RM_Size (T, RM_Size (Implicit_Base));
17562 Inherit_Rep_Item_Chain (T, Implicit_Base);
17563 Set_Digits_Value (T, Digs_Val);
17564 end Floating_Point_Type_Declaration;
17566 ----------------------------
17567 -- Get_Discriminant_Value --
17568 ----------------------------
17570 -- This is the situation:
17572 -- There is a non-derived type
17574 -- type T0 (Dx, Dy, Dz...)
17576 -- There are zero or more levels of derivation, with each derivation
17577 -- either purely inheriting the discriminants, or defining its own.
17579 -- type Ti is new Ti-1
17581 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17583 -- subtype Ti is ...
17585 -- The subtype issue is avoided by the use of Original_Record_Component,
17586 -- and the fact that derived subtypes also derive the constraints.
17588 -- This chain leads back from
17590 -- Typ_For_Constraint
17592 -- Typ_For_Constraint has discriminants, and the value for each
17593 -- discriminant is given by its corresponding Elmt of Constraints.
17595 -- Discriminant is some discriminant in this hierarchy
17597 -- We need to return its value
17599 -- We do this by recursively searching each level, and looking for
17600 -- Discriminant. Once we get to the bottom, we start backing up
17601 -- returning the value for it which may in turn be a discriminant
17602 -- further up, so on the backup we continue the substitution.
17604 function Get_Discriminant_Value
17605 (Discriminant : Entity_Id;
17606 Typ_For_Constraint : Entity_Id;
17607 Constraint : Elist_Id) return Node_Id
17609 function Root_Corresponding_Discriminant
17610 (Discr : Entity_Id) return Entity_Id;
17611 -- Given a discriminant, traverse the chain of inherited discriminants
17612 -- and return the topmost discriminant.
17614 function Search_Derivation_Levels
17616 Discrim_Values : Elist_Id;
17617 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17618 -- This is the routine that performs the recursive search of levels
17619 -- as described above.
17621 -------------------------------------
17622 -- Root_Corresponding_Discriminant --
17623 -------------------------------------
17625 function Root_Corresponding_Discriminant
17626 (Discr : Entity_Id) return Entity_Id
17632 while Present (Corresponding_Discriminant (D)) loop
17633 D := Corresponding_Discriminant (D);
17637 end Root_Corresponding_Discriminant;
17639 ------------------------------
17640 -- Search_Derivation_Levels --
17641 ------------------------------
17643 function Search_Derivation_Levels
17645 Discrim_Values : Elist_Id;
17646 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17650 Result : Node_Or_Entity_Id;
17651 Result_Entity : Node_Id;
17654 -- If inappropriate type, return Error, this happens only in
17655 -- cascaded error situations, and we want to avoid a blow up.
17657 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17661 -- Look deeper if possible. Use Stored_Constraints only for
17662 -- untagged types. For tagged types use the given constraint.
17663 -- This asymmetry needs explanation???
17665 if not Stored_Discrim_Values
17666 and then Present (Stored_Constraint (Ti))
17667 and then not Is_Tagged_Type (Ti)
17670 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17673 Td : constant Entity_Id := Etype (Ti);
17677 Result := Discriminant;
17680 if Present (Stored_Constraint (Ti)) then
17682 Search_Derivation_Levels
17683 (Td, Stored_Constraint (Ti), True);
17686 Search_Derivation_Levels
17687 (Td, Discrim_Values, Stored_Discrim_Values);
17693 -- Extra underlying places to search, if not found above. For
17694 -- concurrent types, the relevant discriminant appears in the
17695 -- corresponding record. For a type derived from a private type
17696 -- without discriminant, the full view inherits the discriminants
17697 -- of the full view of the parent.
17699 if Result = Discriminant then
17700 if Is_Concurrent_Type (Ti)
17701 and then Present (Corresponding_Record_Type (Ti))
17704 Search_Derivation_Levels (
17705 Corresponding_Record_Type (Ti),
17707 Stored_Discrim_Values);
17709 elsif Is_Private_Type (Ti)
17710 and then not Has_Discriminants (Ti)
17711 and then Present (Full_View (Ti))
17712 and then Etype (Full_View (Ti)) /= Ti
17715 Search_Derivation_Levels (
17718 Stored_Discrim_Values);
17722 -- If Result is not a (reference to a) discriminant, return it,
17723 -- otherwise set Result_Entity to the discriminant.
17725 if Nkind (Result) = N_Defining_Identifier then
17726 pragma Assert (Result = Discriminant);
17727 Result_Entity := Result;
17730 if not Denotes_Discriminant (Result) then
17734 Result_Entity := Entity (Result);
17737 -- See if this level of derivation actually has discriminants because
17738 -- tagged derivations can add them, hence the lower levels need not
17741 if not Has_Discriminants (Ti) then
17745 -- Scan Ti's discriminants for Result_Entity, and return its
17746 -- corresponding value, if any.
17748 Result_Entity := Original_Record_Component (Result_Entity);
17750 Assoc := First_Elmt (Discrim_Values);
17752 if Stored_Discrim_Values then
17753 Disc := First_Stored_Discriminant (Ti);
17755 Disc := First_Discriminant (Ti);
17758 while Present (Disc) loop
17760 -- If no further associations return the discriminant, value will
17761 -- be found on the second pass.
17767 if Original_Record_Component (Disc) = Result_Entity then
17768 return Node (Assoc);
17773 if Stored_Discrim_Values then
17774 Next_Stored_Discriminant (Disc);
17776 Next_Discriminant (Disc);
17780 -- Could not find it
17783 end Search_Derivation_Levels;
17787 Result : Node_Or_Entity_Id;
17789 -- Start of processing for Get_Discriminant_Value
17792 -- ??? This routine is a gigantic mess and will be deleted. For the
17793 -- time being just test for the trivial case before calling recurse.
17795 -- We are now celebrating the 20th anniversary of this comment!
17797 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17803 D := First_Discriminant (Typ_For_Constraint);
17804 E := First_Elmt (Constraint);
17805 while Present (D) loop
17806 if Chars (D) = Chars (Discriminant) then
17810 Next_Discriminant (D);
17816 Result := Search_Derivation_Levels
17817 (Typ_For_Constraint, Constraint, False);
17819 -- ??? hack to disappear when this routine is gone
17821 if Nkind (Result) = N_Defining_Identifier then
17827 D := First_Discriminant (Typ_For_Constraint);
17828 E := First_Elmt (Constraint);
17829 while Present (D) loop
17830 if Root_Corresponding_Discriminant (D) = Discriminant then
17834 Next_Discriminant (D);
17840 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17842 end Get_Discriminant_Value;
17844 --------------------------
17845 -- Has_Range_Constraint --
17846 --------------------------
17848 function Has_Range_Constraint (N : Node_Id) return Boolean is
17849 C : constant Node_Id := Constraint (N);
17852 if Nkind (C) = N_Range_Constraint then
17855 elsif Nkind (C) = N_Digits_Constraint then
17857 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17858 or else Present (Range_Constraint (C));
17860 elsif Nkind (C) = N_Delta_Constraint then
17861 return Present (Range_Constraint (C));
17866 end Has_Range_Constraint;
17868 ------------------------
17869 -- Inherit_Components --
17870 ------------------------
17872 function Inherit_Components
17874 Parent_Base : Entity_Id;
17875 Derived_Base : Entity_Id;
17876 Is_Tagged : Boolean;
17877 Inherit_Discr : Boolean;
17878 Discs : Elist_Id) return Elist_Id
17880 Assoc_List : constant Elist_Id := New_Elmt_List;
17882 procedure Inherit_Component
17883 (Old_C : Entity_Id;
17884 Plain_Discrim : Boolean := False;
17885 Stored_Discrim : Boolean := False);
17886 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17887 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17888 -- True, Old_C is a stored discriminant. If they are both false then
17889 -- Old_C is a regular component.
17891 -----------------------
17892 -- Inherit_Component --
17893 -----------------------
17895 procedure Inherit_Component
17896 (Old_C : Entity_Id;
17897 Plain_Discrim : Boolean := False;
17898 Stored_Discrim : Boolean := False)
17900 procedure Set_Anonymous_Type (Id : Entity_Id);
17901 -- Id denotes the entity of an access discriminant or anonymous
17902 -- access component. Set the type of Id to either the same type of
17903 -- Old_C or create a new one depending on whether the parent and
17904 -- the child types are in the same scope.
17906 ------------------------
17907 -- Set_Anonymous_Type --
17908 ------------------------
17910 procedure Set_Anonymous_Type (Id : Entity_Id) is
17911 Old_Typ : constant Entity_Id := Etype (Old_C);
17914 if Scope (Parent_Base) = Scope (Derived_Base) then
17915 Set_Etype (Id, Old_Typ);
17917 -- The parent and the derived type are in two different scopes.
17918 -- Reuse the type of the original discriminant / component by
17919 -- copying it in order to preserve all attributes.
17923 Typ : constant Entity_Id := New_Copy (Old_Typ);
17926 Set_Etype (Id, Typ);
17928 -- Since we do not generate component declarations for
17929 -- inherited components, associate the itype with the
17932 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17933 Set_Scope (Typ, Derived_Base);
17936 end Set_Anonymous_Type;
17938 -- Local variables and constants
17940 New_C : constant Entity_Id := New_Copy (Old_C);
17942 Corr_Discrim : Entity_Id;
17943 Discrim : Entity_Id;
17945 -- Start of processing for Inherit_Component
17948 pragma Assert (not Is_Tagged or not Stored_Discrim);
17950 Set_Parent (New_C, Parent (Old_C));
17952 -- Regular discriminants and components must be inserted in the scope
17953 -- of the Derived_Base. Do it here.
17955 if not Stored_Discrim then
17956 Enter_Name (New_C);
17959 -- For tagged types the Original_Record_Component must point to
17960 -- whatever this field was pointing to in the parent type. This has
17961 -- already been achieved by the call to New_Copy above.
17963 if not Is_Tagged then
17964 Set_Original_Record_Component (New_C, New_C);
17967 -- Set the proper type of an access discriminant
17969 if Ekind (New_C) = E_Discriminant
17970 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17972 Set_Anonymous_Type (New_C);
17975 -- If we have inherited a component then see if its Etype contains
17976 -- references to Parent_Base discriminants. In this case, replace
17977 -- these references with the constraints given in Discs. We do not
17978 -- do this for the partial view of private types because this is
17979 -- not needed (only the components of the full view will be used
17980 -- for code generation) and cause problem. We also avoid this
17981 -- transformation in some error situations.
17983 if Ekind (New_C) = E_Component then
17985 -- Set the proper type of an anonymous access component
17987 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17988 Set_Anonymous_Type (New_C);
17990 elsif (Is_Private_Type (Derived_Base)
17991 and then not Is_Generic_Type (Derived_Base))
17992 or else (Is_Empty_Elmt_List (Discs)
17993 and then not Expander_Active)
17995 Set_Etype (New_C, Etype (Old_C));
17998 -- The current component introduces a circularity of the
18001 -- limited with Pack_2;
18002 -- package Pack_1 is
18003 -- type T_1 is tagged record
18004 -- Comp : access Pack_2.T_2;
18010 -- package Pack_2 is
18011 -- type T_2 is new Pack_1.T_1 with ...;
18016 Constrain_Component_Type
18017 (Old_C, Derived_Base, N, Parent_Base, Discs));
18021 -- In derived tagged types it is illegal to reference a non
18022 -- discriminant component in the parent type. To catch this, mark
18023 -- these components with an Ekind of E_Void. This will be reset in
18024 -- Record_Type_Definition after processing the record extension of
18025 -- the derived type.
18027 -- If the declaration is a private extension, there is no further
18028 -- record extension to process, and the components retain their
18029 -- current kind, because they are visible at this point.
18031 if Is_Tagged and then Ekind (New_C) = E_Component
18032 and then Nkind (N) /= N_Private_Extension_Declaration
18034 Set_Ekind (New_C, E_Void);
18037 if Plain_Discrim then
18038 Set_Corresponding_Discriminant (New_C, Old_C);
18039 Build_Discriminal (New_C);
18041 -- If we are explicitly inheriting a stored discriminant it will be
18042 -- completely hidden.
18044 elsif Stored_Discrim then
18045 Set_Corresponding_Discriminant (New_C, Empty);
18046 Set_Discriminal (New_C, Empty);
18047 Set_Is_Completely_Hidden (New_C);
18049 -- Set the Original_Record_Component of each discriminant in the
18050 -- derived base to point to the corresponding stored that we just
18053 Discrim := First_Discriminant (Derived_Base);
18054 while Present (Discrim) loop
18055 Corr_Discrim := Corresponding_Discriminant (Discrim);
18057 -- Corr_Discrim could be missing in an error situation
18059 if Present (Corr_Discrim)
18060 and then Original_Record_Component (Corr_Discrim) = Old_C
18062 Set_Original_Record_Component (Discrim, New_C);
18065 Next_Discriminant (Discrim);
18068 Append_Entity (New_C, Derived_Base);
18071 if not Is_Tagged then
18072 Append_Elmt (Old_C, Assoc_List);
18073 Append_Elmt (New_C, Assoc_List);
18075 end Inherit_Component;
18077 -- Variables local to Inherit_Component
18079 Loc : constant Source_Ptr := Sloc (N);
18081 Parent_Discrim : Entity_Id;
18082 Stored_Discrim : Entity_Id;
18084 Component : Entity_Id;
18086 -- Start of processing for Inherit_Components
18089 if not Is_Tagged then
18090 Append_Elmt (Parent_Base, Assoc_List);
18091 Append_Elmt (Derived_Base, Assoc_List);
18094 -- Inherit parent discriminants if needed
18096 if Inherit_Discr then
18097 Parent_Discrim := First_Discriminant (Parent_Base);
18098 while Present (Parent_Discrim) loop
18099 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18100 Next_Discriminant (Parent_Discrim);
18104 -- Create explicit stored discrims for untagged types when necessary
18106 if not Has_Unknown_Discriminants (Derived_Base)
18107 and then Has_Discriminants (Parent_Base)
18108 and then not Is_Tagged
18111 or else First_Discriminant (Parent_Base) /=
18112 First_Stored_Discriminant (Parent_Base))
18114 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18115 while Present (Stored_Discrim) loop
18116 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18117 Next_Stored_Discriminant (Stored_Discrim);
18121 -- See if we can apply the second transformation for derived types, as
18122 -- explained in point 6. in the comments above Build_Derived_Record_Type
18123 -- This is achieved by appending Derived_Base discriminants into Discs,
18124 -- which has the side effect of returning a non empty Discs list to the
18125 -- caller of Inherit_Components, which is what we want. This must be
18126 -- done for private derived types if there are explicit stored
18127 -- discriminants, to ensure that we can retrieve the values of the
18128 -- constraints provided in the ancestors.
18131 and then Is_Empty_Elmt_List (Discs)
18132 and then Present (First_Discriminant (Derived_Base))
18134 (not Is_Private_Type (Derived_Base)
18135 or else Is_Completely_Hidden
18136 (First_Stored_Discriminant (Derived_Base))
18137 or else Is_Generic_Type (Derived_Base))
18139 D := First_Discriminant (Derived_Base);
18140 while Present (D) loop
18141 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18142 Next_Discriminant (D);
18146 -- Finally, inherit non-discriminant components unless they are not
18147 -- visible because defined or inherited from the full view of the
18148 -- parent. Don't inherit the _parent field of the parent type.
18150 Component := First_Entity (Parent_Base);
18151 while Present (Component) loop
18153 -- Ada 2005 (AI-251): Do not inherit components associated with
18154 -- secondary tags of the parent.
18156 if Ekind (Component) = E_Component
18157 and then Present (Related_Type (Component))
18161 elsif Ekind (Component) /= E_Component
18162 or else Chars (Component) = Name_uParent
18166 -- If the derived type is within the parent type's declarative
18167 -- region, then the components can still be inherited even though
18168 -- they aren't visible at this point. This can occur for cases
18169 -- such as within public child units where the components must
18170 -- become visible upon entering the child unit's private part.
18172 elsif not Is_Visible_Component (Component)
18173 and then not In_Open_Scopes (Scope (Parent_Base))
18177 elsif Ekind_In (Derived_Base, E_Private_Type,
18178 E_Limited_Private_Type)
18183 Inherit_Component (Component);
18186 Next_Entity (Component);
18189 -- For tagged derived types, inherited discriminants cannot be used in
18190 -- component declarations of the record extension part. To achieve this
18191 -- we mark the inherited discriminants as not visible.
18193 if Is_Tagged and then Inherit_Discr then
18194 D := First_Discriminant (Derived_Base);
18195 while Present (D) loop
18196 Set_Is_Immediately_Visible (D, False);
18197 Next_Discriminant (D);
18202 end Inherit_Components;
18204 -----------------------------
18205 -- Inherit_Predicate_Flags --
18206 -----------------------------
18208 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18210 Set_Has_Predicates (Subt, Has_Predicates (Par));
18211 Set_Has_Static_Predicate_Aspect
18212 (Subt, Has_Static_Predicate_Aspect (Par));
18213 Set_Has_Dynamic_Predicate_Aspect
18214 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18215 end Inherit_Predicate_Flags;
18217 ----------------------
18218 -- Is_EVF_Procedure --
18219 ----------------------
18221 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18222 Formal : Entity_Id;
18225 -- Examine the formals of an Extensions_Visible False procedure looking
18226 -- for a controlling OUT parameter.
18228 if Ekind (Subp) = E_Procedure
18229 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18231 Formal := First_Formal (Subp);
18232 while Present (Formal) loop
18233 if Ekind (Formal) = E_Out_Parameter
18234 and then Is_Controlling_Formal (Formal)
18239 Next_Formal (Formal);
18244 end Is_EVF_Procedure;
18246 -----------------------
18247 -- Is_Null_Extension --
18248 -----------------------
18250 function Is_Null_Extension (T : Entity_Id) return Boolean is
18251 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18252 Comp_List : Node_Id;
18256 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18257 or else not Is_Tagged_Type (T)
18258 or else Nkind (Type_Definition (Type_Decl)) /=
18259 N_Derived_Type_Definition
18260 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18266 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18268 if Present (Discriminant_Specifications (Type_Decl)) then
18271 elsif Present (Comp_List)
18272 and then Is_Non_Empty_List (Component_Items (Comp_List))
18274 Comp := First (Component_Items (Comp_List));
18276 -- Only user-defined components are relevant. The component list
18277 -- may also contain a parent component and internal components
18278 -- corresponding to secondary tags, but these do not determine
18279 -- whether this is a null extension.
18281 while Present (Comp) loop
18282 if Comes_From_Source (Comp) then
18294 end Is_Null_Extension;
18296 ------------------------------
18297 -- Is_Valid_Constraint_Kind --
18298 ------------------------------
18300 function Is_Valid_Constraint_Kind
18301 (T_Kind : Type_Kind;
18302 Constraint_Kind : Node_Kind) return Boolean
18306 when Enumeration_Kind
18309 return Constraint_Kind = N_Range_Constraint;
18311 when Decimal_Fixed_Point_Kind =>
18312 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18313 N_Range_Constraint);
18315 when Ordinary_Fixed_Point_Kind =>
18316 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18317 N_Range_Constraint);
18320 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18321 N_Range_Constraint);
18328 | E_Incomplete_Type
18332 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18335 return True; -- Error will be detected later
18337 end Is_Valid_Constraint_Kind;
18339 --------------------------
18340 -- Is_Visible_Component --
18341 --------------------------
18343 function Is_Visible_Component
18345 N : Node_Id := Empty) return Boolean
18347 Original_Comp : Entity_Id := Empty;
18348 Original_Type : Entity_Id;
18349 Type_Scope : Entity_Id;
18351 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18352 -- Check whether parent type of inherited component is declared locally,
18353 -- possibly within a nested package or instance. The current scope is
18354 -- the derived record itself.
18356 -------------------
18357 -- Is_Local_Type --
18358 -------------------
18360 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18364 Scop := Scope (Typ);
18365 while Present (Scop)
18366 and then Scop /= Standard_Standard
18368 if Scop = Scope (Current_Scope) then
18372 Scop := Scope (Scop);
18378 -- Start of processing for Is_Visible_Component
18381 if Ekind_In (C, E_Component, E_Discriminant) then
18382 Original_Comp := Original_Record_Component (C);
18385 if No (Original_Comp) then
18387 -- Premature usage, or previous error
18392 Original_Type := Scope (Original_Comp);
18393 Type_Scope := Scope (Base_Type (Scope (C)));
18396 -- This test only concerns tagged types
18398 if not Is_Tagged_Type (Original_Type) then
18401 -- If it is _Parent or _Tag, there is no visibility issue
18403 elsif not Comes_From_Source (Original_Comp) then
18406 -- Discriminants are visible unless the (private) type has unknown
18407 -- discriminants. If the discriminant reference is inserted for a
18408 -- discriminant check on a full view it is also visible.
18410 elsif Ekind (Original_Comp) = E_Discriminant
18412 (not Has_Unknown_Discriminants (Original_Type)
18413 or else (Present (N)
18414 and then Nkind (N) = N_Selected_Component
18415 and then Nkind (Prefix (N)) = N_Type_Conversion
18416 and then not Comes_From_Source (Prefix (N))))
18420 -- In the body of an instantiation, check the visibility of a component
18421 -- in case it has a homograph that is a primitive operation of a private
18422 -- type which was not visible in the generic unit.
18424 -- Should Is_Prefixed_Call be propagated from template to instance???
18426 elsif In_Instance_Body then
18427 if not Is_Tagged_Type (Original_Type)
18428 or else not Is_Private_Type (Original_Type)
18434 Subp_Elmt : Elmt_Id;
18437 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18438 while Present (Subp_Elmt) loop
18440 -- The component is hidden by a primitive operation
18442 if Chars (Node (Subp_Elmt)) = Chars (C) then
18446 Next_Elmt (Subp_Elmt);
18453 -- If the component has been declared in an ancestor which is currently
18454 -- a private type, then it is not visible. The same applies if the
18455 -- component's containing type is not in an open scope and the original
18456 -- component's enclosing type is a visible full view of a private type
18457 -- (which can occur in cases where an attempt is being made to reference
18458 -- a component in a sibling package that is inherited from a visible
18459 -- component of a type in an ancestor package; the component in the
18460 -- sibling package should not be visible even though the component it
18461 -- inherited from is visible). This does not apply however in the case
18462 -- where the scope of the type is a private child unit, or when the
18463 -- parent comes from a local package in which the ancestor is currently
18464 -- visible. The latter suppression of visibility is needed for cases
18465 -- that are tested in B730006.
18467 elsif Is_Private_Type (Original_Type)
18469 (not Is_Private_Descendant (Type_Scope)
18470 and then not In_Open_Scopes (Type_Scope)
18471 and then Has_Private_Declaration (Original_Type))
18473 -- If the type derives from an entity in a formal package, there
18474 -- are no additional visible components.
18476 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18477 N_Formal_Package_Declaration
18481 -- if we are not in the private part of the current package, there
18482 -- are no additional visible components.
18484 elsif Ekind (Scope (Current_Scope)) = E_Package
18485 and then not In_Private_Part (Scope (Current_Scope))
18490 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18491 and then In_Open_Scopes (Scope (Original_Type))
18492 and then Is_Local_Type (Type_Scope);
18495 -- There is another weird way in which a component may be invisible when
18496 -- the private and the full view are not derived from the same ancestor.
18497 -- Here is an example :
18499 -- type A1 is tagged record F1 : integer; end record;
18500 -- type A2 is new A1 with record F2 : integer; end record;
18501 -- type T is new A1 with private;
18503 -- type T is new A2 with null record;
18505 -- In this case, the full view of T inherits F1 and F2 but the private
18506 -- view inherits only F1
18510 Ancestor : Entity_Id := Scope (C);
18514 if Ancestor = Original_Type then
18517 -- The ancestor may have a partial view of the original type,
18518 -- but if the full view is in scope, as in a child body, the
18519 -- component is visible.
18521 elsif In_Private_Part (Scope (Original_Type))
18522 and then Full_View (Ancestor) = Original_Type
18526 elsif Ancestor = Etype (Ancestor) then
18528 -- No further ancestors to examine
18533 Ancestor := Etype (Ancestor);
18537 end Is_Visible_Component;
18539 --------------------------
18540 -- Make_Class_Wide_Type --
18541 --------------------------
18543 procedure Make_Class_Wide_Type (T : Entity_Id) is
18544 CW_Type : Entity_Id;
18546 Next_E : Entity_Id;
18549 if Present (Class_Wide_Type (T)) then
18551 -- The class-wide type is a partially decorated entity created for a
18552 -- unanalyzed tagged type referenced through a limited with clause.
18553 -- When the tagged type is analyzed, its class-wide type needs to be
18554 -- redecorated. Note that we reuse the entity created by Decorate_
18555 -- Tagged_Type in order to preserve all links.
18557 if Materialize_Entity (Class_Wide_Type (T)) then
18558 CW_Type := Class_Wide_Type (T);
18559 Set_Materialize_Entity (CW_Type, False);
18561 -- The class wide type can have been defined by the partial view, in
18562 -- which case everything is already done.
18568 -- Default case, we need to create a new class-wide type
18572 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18575 -- Inherit root type characteristics
18577 CW_Name := Chars (CW_Type);
18578 Next_E := Next_Entity (CW_Type);
18579 Copy_Node (T, CW_Type);
18580 Set_Comes_From_Source (CW_Type, False);
18581 Set_Chars (CW_Type, CW_Name);
18582 Set_Parent (CW_Type, Parent (T));
18583 Set_Next_Entity (CW_Type, Next_E);
18585 -- Ensure we have a new freeze node for the class-wide type. The partial
18586 -- view may have freeze action of its own, requiring a proper freeze
18587 -- node, and the same freeze node cannot be shared between the two
18590 Set_Has_Delayed_Freeze (CW_Type);
18591 Set_Freeze_Node (CW_Type, Empty);
18593 -- Customize the class-wide type: It has no prim. op., it cannot be
18594 -- abstract, its Etype points back to the specific root type, and it
18595 -- cannot have any invariants.
18597 Set_Ekind (CW_Type, E_Class_Wide_Type);
18598 Set_Is_Tagged_Type (CW_Type, True);
18599 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18600 Set_Is_Abstract_Type (CW_Type, False);
18601 Set_Is_Constrained (CW_Type, False);
18602 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18603 Set_Default_SSO (CW_Type);
18604 Set_Has_Inheritable_Invariants (CW_Type, False);
18605 Set_Has_Inherited_Invariants (CW_Type, False);
18606 Set_Has_Own_Invariants (CW_Type, False);
18608 if Ekind (T) = E_Class_Wide_Subtype then
18609 Set_Etype (CW_Type, Etype (Base_Type (T)));
18611 Set_Etype (CW_Type, T);
18614 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18616 -- If this is the class_wide type of a constrained subtype, it does
18617 -- not have discriminants.
18619 Set_Has_Discriminants (CW_Type,
18620 Has_Discriminants (T) and then not Is_Constrained (T));
18622 Set_Has_Unknown_Discriminants (CW_Type, True);
18623 Set_Class_Wide_Type (T, CW_Type);
18624 Set_Equivalent_Type (CW_Type, Empty);
18626 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18628 Set_Class_Wide_Type (CW_Type, CW_Type);
18629 end Make_Class_Wide_Type;
18635 procedure Make_Index
18637 Related_Nod : Node_Id;
18638 Related_Id : Entity_Id := Empty;
18639 Suffix_Index : Nat := 1;
18640 In_Iter_Schm : Boolean := False)
18644 Def_Id : Entity_Id := Empty;
18645 Found : Boolean := False;
18648 -- For a discrete range used in a constrained array definition and
18649 -- defined by a range, an implicit conversion to the predefined type
18650 -- INTEGER is assumed if each bound is either a numeric literal, a named
18651 -- number, or an attribute, and the type of both bounds (prior to the
18652 -- implicit conversion) is the type universal_integer. Otherwise, both
18653 -- bounds must be of the same discrete type, other than universal
18654 -- integer; this type must be determinable independently of the
18655 -- context, but using the fact that the type must be discrete and that
18656 -- both bounds must have the same type.
18658 -- Character literals also have a universal type in the absence of
18659 -- of additional context, and are resolved to Standard_Character.
18661 if Nkind (N) = N_Range then
18663 -- The index is given by a range constraint. The bounds are known
18664 -- to be of a consistent type.
18666 if not Is_Overloaded (N) then
18669 -- For universal bounds, choose the specific predefined type
18671 if T = Universal_Integer then
18672 T := Standard_Integer;
18674 elsif T = Any_Character then
18675 Ambiguous_Character (Low_Bound (N));
18677 T := Standard_Character;
18680 -- The node may be overloaded because some user-defined operators
18681 -- are available, but if a universal interpretation exists it is
18682 -- also the selected one.
18684 elsif Universal_Interpretation (N) = Universal_Integer then
18685 T := Standard_Integer;
18691 Ind : Interp_Index;
18695 Get_First_Interp (N, Ind, It);
18696 while Present (It.Typ) loop
18697 if Is_Discrete_Type (It.Typ) then
18700 and then not Covers (It.Typ, T)
18701 and then not Covers (T, It.Typ)
18703 Error_Msg_N ("ambiguous bounds in discrete range", N);
18711 Get_Next_Interp (Ind, It);
18714 if T = Any_Type then
18715 Error_Msg_N ("discrete type required for range", N);
18716 Set_Etype (N, Any_Type);
18719 elsif T = Universal_Integer then
18720 T := Standard_Integer;
18725 if not Is_Discrete_Type (T) then
18726 Error_Msg_N ("discrete type required for range", N);
18727 Set_Etype (N, Any_Type);
18731 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18732 and then Attribute_Name (Low_Bound (N)) = Name_First
18733 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18734 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18735 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18737 -- The type of the index will be the type of the prefix, as long
18738 -- as the upper bound is 'Last of the same type.
18740 Def_Id := Entity (Prefix (Low_Bound (N)));
18742 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18743 or else Attribute_Name (High_Bound (N)) /= Name_Last
18744 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18745 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18752 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18754 elsif Nkind (N) = N_Subtype_Indication then
18756 -- The index is given by a subtype with a range constraint
18758 T := Base_Type (Entity (Subtype_Mark (N)));
18760 if not Is_Discrete_Type (T) then
18761 Error_Msg_N ("discrete type required for range", N);
18762 Set_Etype (N, Any_Type);
18766 R := Range_Expression (Constraint (N));
18769 Process_Range_Expr_In_Decl
18770 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18772 elsif Nkind (N) = N_Attribute_Reference then
18774 -- Catch beginner's error (use of attribute other than 'Range)
18776 if Attribute_Name (N) /= Name_Range then
18777 Error_Msg_N ("expect attribute ''Range", N);
18778 Set_Etype (N, Any_Type);
18782 -- If the node denotes the range of a type mark, that is also the
18783 -- resulting type, and we do not need to create an Itype for it.
18785 if Is_Entity_Name (Prefix (N))
18786 and then Comes_From_Source (N)
18787 and then Is_Type (Entity (Prefix (N)))
18788 and then Is_Discrete_Type (Entity (Prefix (N)))
18790 Def_Id := Entity (Prefix (N));
18793 Analyze_And_Resolve (N);
18797 -- If none of the above, must be a subtype. We convert this to a
18798 -- range attribute reference because in the case of declared first
18799 -- named subtypes, the types in the range reference can be different
18800 -- from the type of the entity. A range attribute normalizes the
18801 -- reference and obtains the correct types for the bounds.
18803 -- This transformation is in the nature of an expansion, is only
18804 -- done if expansion is active. In particular, it is not done on
18805 -- formal generic types, because we need to retain the name of the
18806 -- original index for instantiation purposes.
18809 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18810 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18811 Set_Etype (N, Any_Integer);
18815 -- The type mark may be that of an incomplete type. It is only
18816 -- now that we can get the full view, previous analysis does
18817 -- not look specifically for a type mark.
18819 Set_Entity (N, Get_Full_View (Entity (N)));
18820 Set_Etype (N, Entity (N));
18821 Def_Id := Entity (N);
18823 if not Is_Discrete_Type (Def_Id) then
18824 Error_Msg_N ("discrete type required for index", N);
18825 Set_Etype (N, Any_Type);
18830 if Expander_Active then
18832 Make_Attribute_Reference (Sloc (N),
18833 Attribute_Name => Name_Range,
18834 Prefix => Relocate_Node (N)));
18836 -- The original was a subtype mark that does not freeze. This
18837 -- means that the rewritten version must not freeze either.
18839 Set_Must_Not_Freeze (N);
18840 Set_Must_Not_Freeze (Prefix (N));
18841 Analyze_And_Resolve (N);
18845 -- If expander is inactive, type is legal, nothing else to construct
18852 if not Is_Discrete_Type (T) then
18853 Error_Msg_N ("discrete type required for range", N);
18854 Set_Etype (N, Any_Type);
18857 elsif T = Any_Type then
18858 Set_Etype (N, Any_Type);
18862 -- We will now create the appropriate Itype to describe the range, but
18863 -- first a check. If we originally had a subtype, then we just label
18864 -- the range with this subtype. Not only is there no need to construct
18865 -- a new subtype, but it is wrong to do so for two reasons:
18867 -- 1. A legality concern, if we have a subtype, it must not freeze,
18868 -- and the Itype would cause freezing incorrectly
18870 -- 2. An efficiency concern, if we created an Itype, it would not be
18871 -- recognized as the same type for the purposes of eliminating
18872 -- checks in some circumstances.
18874 -- We signal this case by setting the subtype entity in Def_Id
18876 if No (Def_Id) then
18878 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18879 Set_Etype (Def_Id, Base_Type (T));
18881 if Is_Signed_Integer_Type (T) then
18882 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18884 elsif Is_Modular_Integer_Type (T) then
18885 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18888 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18889 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18890 Set_First_Literal (Def_Id, First_Literal (T));
18893 Set_Size_Info (Def_Id, (T));
18894 Set_RM_Size (Def_Id, RM_Size (T));
18895 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18897 Set_Scalar_Range (Def_Id, R);
18898 Conditional_Delay (Def_Id, T);
18900 if Nkind (N) = N_Subtype_Indication then
18901 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18904 -- In the subtype indication case, if the immediate parent of the
18905 -- new subtype is non-static, then the subtype we create is non-
18906 -- static, even if its bounds are static.
18908 if Nkind (N) = N_Subtype_Indication
18909 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18911 Set_Is_Non_Static_Subtype (Def_Id);
18915 -- Final step is to label the index with this constructed type
18917 Set_Etype (N, Def_Id);
18920 ------------------------------
18921 -- Modular_Type_Declaration --
18922 ------------------------------
18924 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18925 Mod_Expr : constant Node_Id := Expression (Def);
18928 procedure Set_Modular_Size (Bits : Int);
18929 -- Sets RM_Size to Bits, and Esize to normal word size above this
18931 ----------------------
18932 -- Set_Modular_Size --
18933 ----------------------
18935 procedure Set_Modular_Size (Bits : Int) is
18937 Set_RM_Size (T, UI_From_Int (Bits));
18942 elsif Bits <= 16 then
18943 Init_Esize (T, 16);
18945 elsif Bits <= 32 then
18946 Init_Esize (T, 32);
18949 Init_Esize (T, System_Max_Binary_Modulus_Power);
18952 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18953 Set_Is_Known_Valid (T);
18955 end Set_Modular_Size;
18957 -- Start of processing for Modular_Type_Declaration
18960 -- If the mod expression is (exactly) 2 * literal, where literal is
18961 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18963 if Warn_On_Suspicious_Modulus_Value
18964 and then Nkind (Mod_Expr) = N_Op_Multiply
18965 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18966 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18967 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18968 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18971 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18974 -- Proceed with analysis of mod expression
18976 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18978 Set_Ekind (T, E_Modular_Integer_Type);
18979 Init_Alignment (T);
18980 Set_Is_Constrained (T);
18982 if not Is_OK_Static_Expression (Mod_Expr) then
18983 Flag_Non_Static_Expr
18984 ("non-static expression used for modular type bound!", Mod_Expr);
18985 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18987 M_Val := Expr_Value (Mod_Expr);
18991 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18992 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18995 if M_Val > 2 ** Standard_Long_Integer_Size then
18996 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18999 Set_Modulus (T, M_Val);
19001 -- Create bounds for the modular type based on the modulus given in
19002 -- the type declaration and then analyze and resolve those bounds.
19004 Set_Scalar_Range (T,
19005 Make_Range (Sloc (Mod_Expr),
19006 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19007 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19009 -- Properly analyze the literals for the range. We do this manually
19010 -- because we can't go calling Resolve, since we are resolving these
19011 -- bounds with the type, and this type is certainly not complete yet.
19013 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19014 Set_Etype (High_Bound (Scalar_Range (T)), T);
19015 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19016 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19018 -- Loop through powers of two to find number of bits required
19020 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19024 if M_Val = 2 ** Bits then
19025 Set_Modular_Size (Bits);
19030 elsif M_Val < 2 ** Bits then
19031 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
19032 Set_Non_Binary_Modulus (T);
19034 if Bits > System_Max_Nonbinary_Modulus_Power then
19035 Error_Msg_Uint_1 :=
19036 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19038 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19039 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19043 -- In the nonbinary case, set size as per RM 13.3(55)
19045 Set_Modular_Size (Bits);
19052 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19053 -- so we just signal an error and set the maximum size.
19055 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19056 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19058 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19059 Init_Alignment (T);
19061 end Modular_Type_Declaration;
19063 --------------------------
19064 -- New_Concatenation_Op --
19065 --------------------------
19067 procedure New_Concatenation_Op (Typ : Entity_Id) is
19068 Loc : constant Source_Ptr := Sloc (Typ);
19071 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19072 -- Create abbreviated declaration for the formal of a predefined
19073 -- Operator 'Op' of type 'Typ'
19075 --------------------
19076 -- Make_Op_Formal --
19077 --------------------
19079 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19080 Formal : Entity_Id;
19082 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19083 Set_Etype (Formal, Typ);
19084 Set_Mechanism (Formal, Default_Mechanism);
19086 end Make_Op_Formal;
19088 -- Start of processing for New_Concatenation_Op
19091 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19093 Set_Ekind (Op, E_Operator);
19094 Set_Scope (Op, Current_Scope);
19095 Set_Etype (Op, Typ);
19096 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19097 Set_Is_Immediately_Visible (Op);
19098 Set_Is_Intrinsic_Subprogram (Op);
19099 Set_Has_Completion (Op);
19100 Append_Entity (Op, Current_Scope);
19102 Set_Name_Entity_Id (Name_Op_Concat, Op);
19104 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19105 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19106 end New_Concatenation_Op;
19108 -------------------------
19109 -- OK_For_Limited_Init --
19110 -------------------------
19112 -- ???Check all calls of this, and compare the conditions under which it's
19115 function OK_For_Limited_Init
19117 Exp : Node_Id) return Boolean
19120 return Is_CPP_Constructor_Call (Exp)
19121 or else (Ada_Version >= Ada_2005
19122 and then not Debug_Flag_Dot_L
19123 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19124 end OK_For_Limited_Init;
19126 -------------------------------
19127 -- OK_For_Limited_Init_In_05 --
19128 -------------------------------
19130 function OK_For_Limited_Init_In_05
19132 Exp : Node_Id) return Boolean
19135 -- An object of a limited interface type can be initialized with any
19136 -- expression of a nonlimited descendant type. However this does not
19137 -- apply if this is a view conversion of some other expression. This
19138 -- is checked below.
19140 if Is_Class_Wide_Type (Typ)
19141 and then Is_Limited_Interface (Typ)
19142 and then not Is_Limited_Type (Etype (Exp))
19143 and then Nkind (Exp) /= N_Type_Conversion
19148 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19149 -- case of limited aggregates (including extension aggregates), and
19150 -- function calls. The function call may have been given in prefixed
19151 -- notation, in which case the original node is an indexed component.
19152 -- If the function is parameterless, the original node was an explicit
19153 -- dereference. The function may also be parameterless, in which case
19154 -- the source node is just an identifier.
19156 -- A branch of a conditional expression may have been removed if the
19157 -- condition is statically known. This happens during expansion, and
19158 -- thus will not happen if previous errors were encountered. The check
19159 -- will have been performed on the chosen branch, which replaces the
19160 -- original conditional expression.
19166 case Nkind (Original_Node (Exp)) is
19168 | N_Extension_Aggregate
19174 when N_Identifier =>
19175 return Present (Entity (Original_Node (Exp)))
19176 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19178 when N_Qualified_Expression =>
19180 OK_For_Limited_Init_In_05
19181 (Typ, Expression (Original_Node (Exp)));
19183 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19184 -- with a function call, the expander has rewritten the call into an
19185 -- N_Type_Conversion node to force displacement of the pointer to
19186 -- reference the component containing the secondary dispatch table.
19187 -- Otherwise a type conversion is not a legal context.
19188 -- A return statement for a build-in-place function returning a
19189 -- synchronized type also introduces an unchecked conversion.
19191 when N_Type_Conversion
19192 | N_Unchecked_Type_Conversion
19194 return not Comes_From_Source (Exp)
19196 OK_For_Limited_Init_In_05
19197 (Typ, Expression (Original_Node (Exp)));
19199 when N_Explicit_Dereference
19200 | N_Indexed_Component
19201 | N_Selected_Component
19203 return Nkind (Exp) = N_Function_Call;
19205 -- A use of 'Input is a function call, hence allowed. Normally the
19206 -- attribute will be changed to a call, but the attribute by itself
19207 -- can occur with -gnatc.
19209 when N_Attribute_Reference =>
19210 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19212 -- For a case expression, all dependent expressions must be legal
19214 when N_Case_Expression =>
19219 Alt := First (Alternatives (Original_Node (Exp)));
19220 while Present (Alt) loop
19221 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19231 -- For an if expression, all dependent expressions must be legal
19233 when N_If_Expression =>
19235 Then_Expr : constant Node_Id :=
19236 Next (First (Expressions (Original_Node (Exp))));
19237 Else_Expr : constant Node_Id := Next (Then_Expr);
19239 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19241 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19247 end OK_For_Limited_Init_In_05;
19249 -------------------------------------------
19250 -- Ordinary_Fixed_Point_Type_Declaration --
19251 -------------------------------------------
19253 procedure Ordinary_Fixed_Point_Type_Declaration
19257 Loc : constant Source_Ptr := Sloc (Def);
19258 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19259 RRS : constant Node_Id := Real_Range_Specification (Def);
19260 Implicit_Base : Entity_Id;
19267 Check_Restriction (No_Fixed_Point, Def);
19269 -- Create implicit base type
19272 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19273 Set_Etype (Implicit_Base, Implicit_Base);
19275 -- Analyze and process delta expression
19277 Analyze_And_Resolve (Delta_Expr, Any_Real);
19279 Check_Delta_Expression (Delta_Expr);
19280 Delta_Val := Expr_Value_R (Delta_Expr);
19282 Set_Delta_Value (Implicit_Base, Delta_Val);
19284 -- Compute default small from given delta, which is the largest power
19285 -- of two that does not exceed the given delta value.
19295 if Delta_Val < Ureal_1 then
19296 while Delta_Val < Tmp loop
19297 Tmp := Tmp / Ureal_2;
19298 Scale := Scale + 1;
19303 Tmp := Tmp * Ureal_2;
19304 exit when Tmp > Delta_Val;
19305 Scale := Scale - 1;
19309 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19312 Set_Small_Value (Implicit_Base, Small_Val);
19314 -- If no range was given, set a dummy range
19316 if RRS <= Empty_Or_Error then
19317 Low_Val := -Small_Val;
19318 High_Val := Small_Val;
19320 -- Otherwise analyze and process given range
19324 Low : constant Node_Id := Low_Bound (RRS);
19325 High : constant Node_Id := High_Bound (RRS);
19328 Analyze_And_Resolve (Low, Any_Real);
19329 Analyze_And_Resolve (High, Any_Real);
19330 Check_Real_Bound (Low);
19331 Check_Real_Bound (High);
19333 -- Obtain and set the range
19335 Low_Val := Expr_Value_R (Low);
19336 High_Val := Expr_Value_R (High);
19338 if Low_Val > High_Val then
19339 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19344 -- The range for both the implicit base and the declared first subtype
19345 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19346 -- set a temporary range in place. Note that the bounds of the base
19347 -- type will be widened to be symmetrical and to fill the available
19348 -- bits when the type is frozen.
19350 -- We could do this with all discrete types, and probably should, but
19351 -- we absolutely have to do it for fixed-point, since the end-points
19352 -- of the range and the size are determined by the small value, which
19353 -- could be reset before the freeze point.
19355 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19356 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19358 -- Complete definition of first subtype. The inheritance of the rep item
19359 -- chain ensures that SPARK-related pragmas are not clobbered when the
19360 -- ordinary fixed point type acts as a full view of a private type.
19362 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19363 Set_Etype (T, Implicit_Base);
19364 Init_Size_Align (T);
19365 Inherit_Rep_Item_Chain (T, Implicit_Base);
19366 Set_Small_Value (T, Small_Val);
19367 Set_Delta_Value (T, Delta_Val);
19368 Set_Is_Constrained (T);
19369 end Ordinary_Fixed_Point_Type_Declaration;
19371 ----------------------------------
19372 -- Preanalyze_Assert_Expression --
19373 ----------------------------------
19375 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19377 In_Assertion_Expr := In_Assertion_Expr + 1;
19378 Preanalyze_Spec_Expression (N, T);
19379 In_Assertion_Expr := In_Assertion_Expr - 1;
19380 end Preanalyze_Assert_Expression;
19382 -----------------------------------
19383 -- Preanalyze_Default_Expression --
19384 -----------------------------------
19386 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19387 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19389 In_Default_Expr := True;
19390 Preanalyze_Spec_Expression (N, T);
19391 In_Default_Expr := Save_In_Default_Expr;
19392 end Preanalyze_Default_Expression;
19394 --------------------------------
19395 -- Preanalyze_Spec_Expression --
19396 --------------------------------
19398 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19399 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19401 In_Spec_Expression := True;
19402 Preanalyze_And_Resolve (N, T);
19403 In_Spec_Expression := Save_In_Spec_Expression;
19404 end Preanalyze_Spec_Expression;
19406 ----------------------------------------
19407 -- Prepare_Private_Subtype_Completion --
19408 ----------------------------------------
19410 procedure Prepare_Private_Subtype_Completion
19412 Related_Nod : Node_Id)
19414 Id_B : constant Entity_Id := Base_Type (Id);
19415 Full_B : Entity_Id := Full_View (Id_B);
19419 if Present (Full_B) then
19421 -- Get to the underlying full view if necessary
19423 if Is_Private_Type (Full_B)
19424 and then Present (Underlying_Full_View (Full_B))
19426 Full_B := Underlying_Full_View (Full_B);
19429 -- The Base_Type is already completed, we can complete the subtype
19430 -- now. We have to create a new entity with the same name, Thus we
19431 -- can't use Create_Itype.
19433 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19434 Set_Is_Itype (Full);
19435 Set_Associated_Node_For_Itype (Full, Related_Nod);
19436 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19439 -- The parent subtype may be private, but the base might not, in some
19440 -- nested instances. In that case, the subtype does not need to be
19441 -- exchanged. It would still be nice to make private subtypes and their
19442 -- bases consistent at all times ???
19444 if Is_Private_Type (Id_B) then
19445 Append_Elmt (Id, Private_Dependents (Id_B));
19447 end Prepare_Private_Subtype_Completion;
19449 ---------------------------
19450 -- Process_Discriminants --
19451 ---------------------------
19453 procedure Process_Discriminants
19455 Prev : Entity_Id := Empty)
19457 Elist : constant Elist_Id := New_Elmt_List;
19460 Discr_Number : Uint;
19461 Discr_Type : Entity_Id;
19462 Default_Present : Boolean := False;
19463 Default_Not_Present : Boolean := False;
19466 -- A composite type other than an array type can have discriminants.
19467 -- On entry, the current scope is the composite type.
19469 -- The discriminants are initially entered into the scope of the type
19470 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19471 -- use, as explained at the end of this procedure.
19473 Discr := First (Discriminant_Specifications (N));
19474 while Present (Discr) loop
19475 Enter_Name (Defining_Identifier (Discr));
19477 -- For navigation purposes we add a reference to the discriminant
19478 -- in the entity for the type. If the current declaration is a
19479 -- completion, place references on the partial view. Otherwise the
19480 -- type is the current scope.
19482 if Present (Prev) then
19484 -- The references go on the partial view, if present. If the
19485 -- partial view has discriminants, the references have been
19486 -- generated already.
19488 if not Has_Discriminants (Prev) then
19489 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19493 (Current_Scope, Defining_Identifier (Discr), 'd');
19496 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19497 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19499 -- Ada 2005 (AI-254)
19501 if Present (Access_To_Subprogram_Definition
19502 (Discriminant_Type (Discr)))
19503 and then Protected_Present (Access_To_Subprogram_Definition
19504 (Discriminant_Type (Discr)))
19507 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19511 Find_Type (Discriminant_Type (Discr));
19512 Discr_Type := Etype (Discriminant_Type (Discr));
19514 if Error_Posted (Discriminant_Type (Discr)) then
19515 Discr_Type := Any_Type;
19519 -- Handling of discriminants that are access types
19521 if Is_Access_Type (Discr_Type) then
19523 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19524 -- limited record types
19526 if Ada_Version < Ada_2005 then
19527 Check_Access_Discriminant_Requires_Limited
19528 (Discr, Discriminant_Type (Discr));
19531 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19533 ("(Ada 83) access discriminant not allowed", Discr);
19536 -- If not access type, must be a discrete type
19538 elsif not Is_Discrete_Type (Discr_Type) then
19540 ("discriminants must have a discrete or access type",
19541 Discriminant_Type (Discr));
19544 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19546 -- If a discriminant specification includes the assignment compound
19547 -- delimiter followed by an expression, the expression is the default
19548 -- expression of the discriminant; the default expression must be of
19549 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19550 -- a default expression, we do the special preanalysis, since this
19551 -- expression does not freeze (see section "Handling of Default and
19552 -- Per-Object Expressions" in spec of package Sem).
19554 if Present (Expression (Discr)) then
19555 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19559 if Nkind (N) = N_Formal_Type_Declaration then
19561 ("discriminant defaults not allowed for formal type",
19562 Expression (Discr));
19564 -- Flag an error for a tagged type with defaulted discriminants,
19565 -- excluding limited tagged types when compiling for Ada 2012
19566 -- (see AI05-0214).
19568 elsif Is_Tagged_Type (Current_Scope)
19569 and then (not Is_Limited_Type (Current_Scope)
19570 or else Ada_Version < Ada_2012)
19571 and then Comes_From_Source (N)
19573 -- Note: see similar test in Check_Or_Process_Discriminants, to
19574 -- handle the (illegal) case of the completion of an untagged
19575 -- view with discriminants with defaults by a tagged full view.
19576 -- We skip the check if Discr does not come from source, to
19577 -- account for the case of an untagged derived type providing
19578 -- defaults for a renamed discriminant from a private untagged
19579 -- ancestor with a tagged full view (ACATS B460006).
19581 if Ada_Version >= Ada_2012 then
19583 ("discriminants of nonlimited tagged type cannot have"
19585 Expression (Discr));
19588 ("discriminants of tagged type cannot have defaults",
19589 Expression (Discr));
19593 Default_Present := True;
19594 Append_Elmt (Expression (Discr), Elist);
19596 -- Tag the defining identifiers for the discriminants with
19597 -- their corresponding default expressions from the tree.
19599 Set_Discriminant_Default_Value
19600 (Defining_Identifier (Discr), Expression (Discr));
19603 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19604 -- gets set unless we can be sure that no range check is required.
19606 if (GNATprove_Mode or not Expander_Active)
19609 (Expression (Discr), Discr_Type, Assume_Valid => True)
19611 Set_Do_Range_Check (Expression (Discr));
19614 -- No default discriminant value given
19617 Default_Not_Present := True;
19620 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19621 -- Discr_Type but with the null-exclusion attribute
19623 if Ada_Version >= Ada_2005 then
19625 -- Ada 2005 (AI-231): Static checks
19627 if Can_Never_Be_Null (Discr_Type) then
19628 Null_Exclusion_Static_Checks (Discr);
19630 elsif Is_Access_Type (Discr_Type)
19631 and then Null_Exclusion_Present (Discr)
19633 -- No need to check itypes because in their case this check
19634 -- was done at their point of creation
19636 and then not Is_Itype (Discr_Type)
19638 if Can_Never_Be_Null (Discr_Type) then
19640 ("`NOT NULL` not allowed (& already excludes null)",
19645 Set_Etype (Defining_Identifier (Discr),
19646 Create_Null_Excluding_Itype
19648 Related_Nod => Discr));
19650 -- Check for improper null exclusion if the type is otherwise
19651 -- legal for a discriminant.
19653 elsif Null_Exclusion_Present (Discr)
19654 and then Is_Discrete_Type (Discr_Type)
19657 ("null exclusion can only apply to an access type", Discr);
19660 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19661 -- can't have defaults. Synchronized types, or types that are
19662 -- explicitly limited are fine, but special tests apply to derived
19663 -- types in generics: in a generic body we have to assume the
19664 -- worst, and therefore defaults are not allowed if the parent is
19665 -- a generic formal private type (see ACATS B370001).
19667 if Is_Access_Type (Discr_Type) and then Default_Present then
19668 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19669 or else Is_Limited_Record (Current_Scope)
19670 or else Is_Concurrent_Type (Current_Scope)
19671 or else Is_Concurrent_Record_Type (Current_Scope)
19672 or else Ekind (Current_Scope) = E_Limited_Private_Type
19674 if not Is_Derived_Type (Current_Scope)
19675 or else not Is_Generic_Type (Etype (Current_Scope))
19676 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19677 or else Limited_Present
19678 (Type_Definition (Parent (Current_Scope)))
19684 ("access discriminants of nonlimited types cannot "
19685 & "have defaults", Expression (Discr));
19688 elsif Present (Expression (Discr)) then
19690 ("(Ada 2005) access discriminants of nonlimited types "
19691 & "cannot have defaults", Expression (Discr));
19696 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(6)).
19697 -- This check is relevant only when SPARK_Mode is on as it is not a
19698 -- standard Ada legality rule.
19701 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19703 Error_Msg_N ("discriminant cannot be volatile", Discr);
19709 -- An element list consisting of the default expressions of the
19710 -- discriminants is constructed in the above loop and used to set
19711 -- the Discriminant_Constraint attribute for the type. If an object
19712 -- is declared of this (record or task) type without any explicit
19713 -- discriminant constraint given, this element list will form the
19714 -- actual parameters for the corresponding initialization procedure
19717 Set_Discriminant_Constraint (Current_Scope, Elist);
19718 Set_Stored_Constraint (Current_Scope, No_Elist);
19720 -- Default expressions must be provided either for all or for none
19721 -- of the discriminants of a discriminant part. (RM 3.7.1)
19723 if Default_Present and then Default_Not_Present then
19725 ("incomplete specification of defaults for discriminants", N);
19728 -- The use of the name of a discriminant is not allowed in default
19729 -- expressions of a discriminant part if the specification of the
19730 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19732 -- To detect this, the discriminant names are entered initially with an
19733 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19734 -- attempt to use a void entity (for example in an expression that is
19735 -- type-checked) produces the error message: premature usage. Now after
19736 -- completing the semantic analysis of the discriminant part, we can set
19737 -- the Ekind of all the discriminants appropriately.
19739 Discr := First (Discriminant_Specifications (N));
19740 Discr_Number := Uint_1;
19741 while Present (Discr) loop
19742 Id := Defining_Identifier (Discr);
19743 Set_Ekind (Id, E_Discriminant);
19744 Init_Component_Location (Id);
19746 Set_Discriminant_Number (Id, Discr_Number);
19748 -- Make sure this is always set, even in illegal programs
19750 Set_Corresponding_Discriminant (Id, Empty);
19752 -- Initialize the Original_Record_Component to the entity itself.
19753 -- Inherit_Components will propagate the right value to
19754 -- discriminants in derived record types.
19756 Set_Original_Record_Component (Id, Id);
19758 -- Create the discriminal for the discriminant
19760 Build_Discriminal (Id);
19763 Discr_Number := Discr_Number + 1;
19766 Set_Has_Discriminants (Current_Scope);
19767 end Process_Discriminants;
19769 -----------------------
19770 -- Process_Full_View --
19771 -----------------------
19773 -- WARNING: This routine manages Ghost regions. Return statements must be
19774 -- replaced by gotos which jump to the end of the routine and restore the
19777 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19778 procedure Collect_Implemented_Interfaces
19780 Ifaces : Elist_Id);
19781 -- Ada 2005: Gather all the interfaces that Typ directly or
19782 -- inherently implements. Duplicate entries are not added to
19783 -- the list Ifaces.
19785 ------------------------------------
19786 -- Collect_Implemented_Interfaces --
19787 ------------------------------------
19789 procedure Collect_Implemented_Interfaces
19794 Iface_Elmt : Elmt_Id;
19797 -- Abstract interfaces are only associated with tagged record types
19799 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19803 -- Recursively climb to the ancestors
19805 if Etype (Typ) /= Typ
19807 -- Protect the frontend against wrong cyclic declarations like:
19809 -- type B is new A with private;
19810 -- type C is new A with private;
19812 -- type B is new C with null record;
19813 -- type C is new B with null record;
19815 and then Etype (Typ) /= Priv_T
19816 and then Etype (Typ) /= Full_T
19818 -- Keep separate the management of private type declarations
19820 if Ekind (Typ) = E_Record_Type_With_Private then
19822 -- Handle the following illegal usage:
19823 -- type Private_Type is tagged private;
19825 -- type Private_Type is new Type_Implementing_Iface;
19827 if Present (Full_View (Typ))
19828 and then Etype (Typ) /= Full_View (Typ)
19830 if Is_Interface (Etype (Typ)) then
19831 Append_Unique_Elmt (Etype (Typ), Ifaces);
19834 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19837 -- Non-private types
19840 if Is_Interface (Etype (Typ)) then
19841 Append_Unique_Elmt (Etype (Typ), Ifaces);
19844 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19848 -- Handle entities in the list of abstract interfaces
19850 if Present (Interfaces (Typ)) then
19851 Iface_Elmt := First_Elmt (Interfaces (Typ));
19852 while Present (Iface_Elmt) loop
19853 Iface := Node (Iface_Elmt);
19855 pragma Assert (Is_Interface (Iface));
19857 if not Contain_Interface (Iface, Ifaces) then
19858 Append_Elmt (Iface, Ifaces);
19859 Collect_Implemented_Interfaces (Iface, Ifaces);
19862 Next_Elmt (Iface_Elmt);
19865 end Collect_Implemented_Interfaces;
19869 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
19871 Full_Indic : Node_Id;
19872 Full_Parent : Entity_Id;
19873 Priv_Parent : Entity_Id;
19875 -- Start of processing for Process_Full_View
19878 Mark_And_Set_Ghost_Completion (N, Priv_T);
19880 -- First some sanity checks that must be done after semantic
19881 -- decoration of the full view and thus cannot be placed with other
19882 -- similar checks in Find_Type_Name
19884 if not Is_Limited_Type (Priv_T)
19885 and then (Is_Limited_Type (Full_T)
19886 or else Is_Limited_Composite (Full_T))
19888 if In_Instance then
19892 ("completion of nonlimited type cannot be limited", Full_T);
19893 Explain_Limited_Type (Full_T, Full_T);
19896 elsif Is_Abstract_Type (Full_T)
19897 and then not Is_Abstract_Type (Priv_T)
19900 ("completion of nonabstract type cannot be abstract", Full_T);
19902 elsif Is_Tagged_Type (Priv_T)
19903 and then Is_Limited_Type (Priv_T)
19904 and then not Is_Limited_Type (Full_T)
19906 -- If pragma CPP_Class was applied to the private declaration
19907 -- propagate the limitedness to the full-view
19909 if Is_CPP_Class (Priv_T) then
19910 Set_Is_Limited_Record (Full_T);
19912 -- GNAT allow its own definition of Limited_Controlled to disobey
19913 -- this rule in order in ease the implementation. This test is safe
19914 -- because Root_Controlled is defined in a child of System that
19915 -- normal programs are not supposed to use.
19917 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19918 Set_Is_Limited_Composite (Full_T);
19921 ("completion of limited tagged type must be limited", Full_T);
19924 elsif Is_Generic_Type (Priv_T) then
19925 Error_Msg_N ("generic type cannot have a completion", Full_T);
19928 -- Check that ancestor interfaces of private and full views are
19929 -- consistent. We omit this check for synchronized types because
19930 -- they are performed on the corresponding record type when frozen.
19932 if Ada_Version >= Ada_2005
19933 and then Is_Tagged_Type (Priv_T)
19934 and then Is_Tagged_Type (Full_T)
19935 and then not Is_Concurrent_Type (Full_T)
19939 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19940 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19943 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19944 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19946 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19947 -- an interface type if and only if the full type is descendant
19948 -- of the interface type (AARM 7.3 (7.3/2)).
19950 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19952 if Present (Iface) then
19954 ("interface in partial view& not implemented by full type "
19955 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19958 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19960 if Present (Iface) then
19962 ("interface & not implemented by partial view "
19963 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19968 if Is_Tagged_Type (Priv_T)
19969 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19970 and then Is_Derived_Type (Full_T)
19972 Priv_Parent := Etype (Priv_T);
19974 -- The full view of a private extension may have been transformed
19975 -- into an unconstrained derived type declaration and a subtype
19976 -- declaration (see build_derived_record_type for details).
19978 if Nkind (N) = N_Subtype_Declaration then
19979 Full_Indic := Subtype_Indication (N);
19980 Full_Parent := Etype (Base_Type (Full_T));
19982 Full_Indic := Subtype_Indication (Type_Definition (N));
19983 Full_Parent := Etype (Full_T);
19986 -- Check that the parent type of the full type is a descendant of
19987 -- the ancestor subtype given in the private extension. If either
19988 -- entity has an Etype equal to Any_Type then we had some previous
19989 -- error situation [7.3(8)].
19991 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19994 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19995 -- any order. Therefore we don't have to check that its parent must
19996 -- be a descendant of the parent of the private type declaration.
19998 elsif Is_Interface (Priv_Parent)
19999 and then Is_Interface (Full_Parent)
20003 -- Ada 2005 (AI-251): If the parent of the private type declaration
20004 -- is an interface there is no need to check that it is an ancestor
20005 -- of the associated full type declaration. The required tests for
20006 -- this case are performed by Build_Derived_Record_Type.
20008 elsif not Is_Interface (Base_Type (Priv_Parent))
20009 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20012 ("parent of full type must descend from parent of private "
20013 & "extension", Full_Indic);
20015 -- First check a formal restriction, and then proceed with checking
20016 -- Ada rules. Since the formal restriction is not a serious error, we
20017 -- don't prevent further error detection for this check, hence the
20021 -- In formal mode, when completing a private extension the type
20022 -- named in the private part must be exactly the same as that
20023 -- named in the visible part.
20025 if Priv_Parent /= Full_Parent then
20026 Error_Msg_Name_1 := Chars (Priv_Parent);
20027 Check_SPARK_05_Restriction ("% expected", Full_Indic);
20030 -- Check the rules of 7.3(10): if the private extension inherits
20031 -- known discriminants, then the full type must also inherit those
20032 -- discriminants from the same (ancestor) type, and the parent
20033 -- subtype of the full type must be constrained if and only if
20034 -- the ancestor subtype of the private extension is constrained.
20036 if No (Discriminant_Specifications (Parent (Priv_T)))
20037 and then not Has_Unknown_Discriminants (Priv_T)
20038 and then Has_Discriminants (Base_Type (Priv_Parent))
20041 Priv_Indic : constant Node_Id :=
20042 Subtype_Indication (Parent (Priv_T));
20044 Priv_Constr : constant Boolean :=
20045 Is_Constrained (Priv_Parent)
20047 Nkind (Priv_Indic) = N_Subtype_Indication
20049 Is_Constrained (Entity (Priv_Indic));
20051 Full_Constr : constant Boolean :=
20052 Is_Constrained (Full_Parent)
20054 Nkind (Full_Indic) = N_Subtype_Indication
20056 Is_Constrained (Entity (Full_Indic));
20058 Priv_Discr : Entity_Id;
20059 Full_Discr : Entity_Id;
20062 Priv_Discr := First_Discriminant (Priv_Parent);
20063 Full_Discr := First_Discriminant (Full_Parent);
20064 while Present (Priv_Discr) and then Present (Full_Discr) loop
20065 if Original_Record_Component (Priv_Discr) =
20066 Original_Record_Component (Full_Discr)
20068 Corresponding_Discriminant (Priv_Discr) =
20069 Corresponding_Discriminant (Full_Discr)
20076 Next_Discriminant (Priv_Discr);
20077 Next_Discriminant (Full_Discr);
20080 if Present (Priv_Discr) or else Present (Full_Discr) then
20082 ("full view must inherit discriminants of the parent "
20083 & "type used in the private extension", Full_Indic);
20085 elsif Priv_Constr and then not Full_Constr then
20087 ("parent subtype of full type must be constrained",
20090 elsif Full_Constr and then not Priv_Constr then
20092 ("parent subtype of full type must be unconstrained",
20097 -- Check the rules of 7.3(12): if a partial view has neither
20098 -- known or unknown discriminants, then the full type
20099 -- declaration shall define a definite subtype.
20101 elsif not Has_Unknown_Discriminants (Priv_T)
20102 and then not Has_Discriminants (Priv_T)
20103 and then not Is_Constrained (Full_T)
20106 ("full view must define a constrained type if partial view "
20107 & "has no discriminants", Full_T);
20110 -- ??????? Do we implement the following properly ?????
20111 -- If the ancestor subtype of a private extension has constrained
20112 -- discriminants, then the parent subtype of the full view shall
20113 -- impose a statically matching constraint on those discriminants
20118 -- For untagged types, verify that a type without discriminants is
20119 -- not completed with an unconstrained type. A separate error message
20120 -- is produced if the full type has defaulted discriminants.
20122 if Is_Definite_Subtype (Priv_T)
20123 and then not Is_Definite_Subtype (Full_T)
20125 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20127 ("full view of& not compatible with declaration#",
20130 if not Is_Tagged_Type (Full_T) then
20132 ("\one is constrained, the other unconstrained", Full_T);
20137 -- AI-419: verify that the use of "limited" is consistent
20140 Orig_Decl : constant Node_Id := Original_Node (N);
20143 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20144 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20146 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20148 if not Limited_Present (Parent (Priv_T))
20149 and then not Synchronized_Present (Parent (Priv_T))
20150 and then Limited_Present (Type_Definition (Orig_Decl))
20153 ("full view of non-limited extension cannot be limited", N);
20155 -- Conversely, if the partial view carries the limited keyword,
20156 -- the full view must as well, even if it may be redundant.
20158 elsif Limited_Present (Parent (Priv_T))
20159 and then not Limited_Present (Type_Definition (Orig_Decl))
20162 ("full view of limited extension must be explicitly limited",
20168 -- Ada 2005 (AI-443): A synchronized private extension must be
20169 -- completed by a task or protected type.
20171 if Ada_Version >= Ada_2005
20172 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20173 and then Synchronized_Present (Parent (Priv_T))
20174 and then not Is_Concurrent_Type (Full_T)
20176 Error_Msg_N ("full view of synchronized extension must " &
20177 "be synchronized type", N);
20180 -- Ada 2005 AI-363: if the full view has discriminants with
20181 -- defaults, it is illegal to declare constrained access subtypes
20182 -- whose designated type is the current type. This allows objects
20183 -- of the type that are declared in the heap to be unconstrained.
20185 if not Has_Unknown_Discriminants (Priv_T)
20186 and then not Has_Discriminants (Priv_T)
20187 and then Has_Discriminants (Full_T)
20189 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20191 Set_Has_Constrained_Partial_View (Full_T);
20192 Set_Has_Constrained_Partial_View (Priv_T);
20195 -- Create a full declaration for all its subtypes recorded in
20196 -- Private_Dependents and swap them similarly to the base type. These
20197 -- are subtypes that have been define before the full declaration of
20198 -- the private type. We also swap the entry in Private_Dependents list
20199 -- so we can properly restore the private view on exit from the scope.
20202 Priv_Elmt : Elmt_Id;
20203 Priv_Scop : Entity_Id;
20208 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20209 while Present (Priv_Elmt) loop
20210 Priv := Node (Priv_Elmt);
20211 Priv_Scop := Scope (Priv);
20213 if Ekind_In (Priv, E_Private_Subtype,
20214 E_Limited_Private_Subtype,
20215 E_Record_Subtype_With_Private)
20217 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20218 Set_Is_Itype (Full);
20219 Set_Parent (Full, Parent (Priv));
20220 Set_Associated_Node_For_Itype (Full, N);
20222 -- Now we need to complete the private subtype, but since the
20223 -- base type has already been swapped, we must also swap the
20224 -- subtypes (and thus, reverse the arguments in the call to
20225 -- Complete_Private_Subtype). Also note that we may need to
20226 -- re-establish the scope of the private subtype.
20228 Copy_And_Swap (Priv, Full);
20230 if not In_Open_Scopes (Priv_Scop) then
20231 Push_Scope (Priv_Scop);
20234 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20236 Priv_Scop := Empty;
20239 Complete_Private_Subtype (Full, Priv, Full_T, N);
20241 if Present (Priv_Scop) then
20245 Replace_Elmt (Priv_Elmt, Full);
20248 Next_Elmt (Priv_Elmt);
20252 -- If the private view was tagged, copy the new primitive operations
20253 -- from the private view to the full view.
20255 if Is_Tagged_Type (Full_T) then
20257 Disp_Typ : Entity_Id;
20258 Full_List : Elist_Id;
20260 Prim_Elmt : Elmt_Id;
20261 Priv_List : Elist_Id;
20265 L : Elist_Id) return Boolean;
20266 -- Determine whether list L contains element E
20274 L : Elist_Id) return Boolean
20276 List_Elmt : Elmt_Id;
20279 List_Elmt := First_Elmt (L);
20280 while Present (List_Elmt) loop
20281 if Node (List_Elmt) = E then
20285 Next_Elmt (List_Elmt);
20291 -- Start of processing
20294 if Is_Tagged_Type (Priv_T) then
20295 Priv_List := Primitive_Operations (Priv_T);
20296 Prim_Elmt := First_Elmt (Priv_List);
20298 -- In the case of a concurrent type completing a private tagged
20299 -- type, primitives may have been declared in between the two
20300 -- views. These subprograms need to be wrapped the same way
20301 -- entries and protected procedures are handled because they
20302 -- cannot be directly shared by the two views.
20304 if Is_Concurrent_Type (Full_T) then
20306 Conc_Typ : constant Entity_Id :=
20307 Corresponding_Record_Type (Full_T);
20308 Curr_Nod : Node_Id := Parent (Conc_Typ);
20309 Wrap_Spec : Node_Id;
20312 while Present (Prim_Elmt) loop
20313 Prim := Node (Prim_Elmt);
20315 if Comes_From_Source (Prim)
20316 and then not Is_Abstract_Subprogram (Prim)
20319 Make_Subprogram_Declaration (Sloc (Prim),
20323 Obj_Typ => Conc_Typ,
20325 Parameter_Specifications
20328 Insert_After (Curr_Nod, Wrap_Spec);
20329 Curr_Nod := Wrap_Spec;
20331 Analyze (Wrap_Spec);
20333 -- Remove the wrapper from visibility to avoid
20334 -- spurious conflict with the wrapped entity.
20336 Set_Is_Immediately_Visible
20337 (Defining_Entity (Specification (Wrap_Spec)),
20341 Next_Elmt (Prim_Elmt);
20347 -- For non-concurrent types, transfer explicit primitives, but
20348 -- omit those inherited from the parent of the private view
20349 -- since they will be re-inherited later on.
20352 Full_List := Primitive_Operations (Full_T);
20354 while Present (Prim_Elmt) loop
20355 Prim := Node (Prim_Elmt);
20357 if Comes_From_Source (Prim)
20358 and then not Contains (Prim, Full_List)
20360 Append_Elmt (Prim, Full_List);
20363 Next_Elmt (Prim_Elmt);
20367 -- Untagged private view
20370 Full_List := Primitive_Operations (Full_T);
20372 -- In this case the partial view is untagged, so here we locate
20373 -- all of the earlier primitives that need to be treated as
20374 -- dispatching (those that appear between the two views). Note
20375 -- that these additional operations must all be new operations
20376 -- (any earlier operations that override inherited operations
20377 -- of the full view will already have been inserted in the
20378 -- primitives list, marked by Check_Operation_From_Private_View
20379 -- as dispatching. Note that implicit "/=" operators are
20380 -- excluded from being added to the primitives list since they
20381 -- shouldn't be treated as dispatching (tagged "/=" is handled
20384 Prim := Next_Entity (Full_T);
20385 while Present (Prim) and then Prim /= Priv_T loop
20386 if Ekind_In (Prim, E_Procedure, E_Function) then
20387 Disp_Typ := Find_Dispatching_Type (Prim);
20389 if Disp_Typ = Full_T
20390 and then (Chars (Prim) /= Name_Op_Ne
20391 or else Comes_From_Source (Prim))
20393 Check_Controlling_Formals (Full_T, Prim);
20395 if not Is_Dispatching_Operation (Prim) then
20396 Append_Elmt (Prim, Full_List);
20397 Set_Is_Dispatching_Operation (Prim, True);
20398 Set_DT_Position_Value (Prim, No_Uint);
20401 elsif Is_Dispatching_Operation (Prim)
20402 and then Disp_Typ /= Full_T
20405 -- Verify that it is not otherwise controlled by a
20406 -- formal or a return value of type T.
20408 Check_Controlling_Formals (Disp_Typ, Prim);
20412 Next_Entity (Prim);
20416 -- For the tagged case, the two views can share the same primitive
20417 -- operations list and the same class-wide type. Update attributes
20418 -- of the class-wide type which depend on the full declaration.
20420 if Is_Tagged_Type (Priv_T) then
20421 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20422 Set_Class_Wide_Type
20423 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20425 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20430 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20432 if Known_To_Have_Preelab_Init (Priv_T) then
20434 -- Case where there is a pragma Preelaborable_Initialization. We
20435 -- always allow this in predefined units, which is cheating a bit,
20436 -- but it means we don't have to struggle to meet the requirements in
20437 -- the RM for having Preelaborable Initialization. Otherwise we
20438 -- require that the type meets the RM rules. But we can't check that
20439 -- yet, because of the rule about overriding Initialize, so we simply
20440 -- set a flag that will be checked at freeze time.
20442 if not In_Predefined_Unit (Full_T) then
20443 Set_Must_Have_Preelab_Init (Full_T);
20447 -- If pragma CPP_Class was applied to the private type declaration,
20448 -- propagate it now to the full type declaration.
20450 if Is_CPP_Class (Priv_T) then
20451 Set_Is_CPP_Class (Full_T);
20452 Set_Convention (Full_T, Convention_CPP);
20454 -- Check that components of imported CPP types do not have default
20457 Check_CPP_Type_Has_No_Defaults (Full_T);
20460 -- If the private view has user specified stream attributes, then so has
20463 -- Why the test, how could these flags be already set in Full_T ???
20465 if Has_Specified_Stream_Read (Priv_T) then
20466 Set_Has_Specified_Stream_Read (Full_T);
20469 if Has_Specified_Stream_Write (Priv_T) then
20470 Set_Has_Specified_Stream_Write (Full_T);
20473 if Has_Specified_Stream_Input (Priv_T) then
20474 Set_Has_Specified_Stream_Input (Full_T);
20477 if Has_Specified_Stream_Output (Priv_T) then
20478 Set_Has_Specified_Stream_Output (Full_T);
20481 -- Propagate Default_Initial_Condition-related attributes from the
20482 -- partial view to the full view and its base type.
20484 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20485 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20487 -- Propagate invariant-related attributes from the partial view to the
20488 -- full view and its base type.
20490 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20491 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20493 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20494 -- in the full view without advertising the inheritance in the partial
20495 -- view. This can only occur when the partial view has no parent type
20496 -- and the full view has an interface as a parent. Any other scenarios
20497 -- are illegal because implemented interfaces must match between the
20500 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20502 Full_Par : constant Entity_Id := Etype (Full_T);
20503 Priv_Par : constant Entity_Id := Etype (Priv_T);
20506 if not Is_Interface (Priv_Par)
20507 and then Is_Interface (Full_Par)
20508 and then Has_Inheritable_Invariants (Full_Par)
20511 ("hidden inheritance of class-wide type invariants not "
20517 -- Propagate predicates to full type, and predicate function if already
20518 -- defined. It is not clear that this can actually happen? the partial
20519 -- view cannot be frozen yet, and the predicate function has not been
20520 -- built. Still it is a cheap check and seems safer to make it.
20522 if Has_Predicates (Priv_T) then
20523 Set_Has_Predicates (Full_T);
20525 if Present (Predicate_Function (Priv_T)) then
20526 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20531 Restore_Ghost_Mode (Saved_GM);
20532 end Process_Full_View;
20534 -----------------------------------
20535 -- Process_Incomplete_Dependents --
20536 -----------------------------------
20538 procedure Process_Incomplete_Dependents
20540 Full_T : Entity_Id;
20543 Inc_Elmt : Elmt_Id;
20544 Priv_Dep : Entity_Id;
20545 New_Subt : Entity_Id;
20547 Disc_Constraint : Elist_Id;
20550 if No (Private_Dependents (Inc_T)) then
20554 -- Itypes that may be generated by the completion of an incomplete
20555 -- subtype are not used by the back-end and not attached to the tree.
20556 -- They are created only for constraint-checking purposes.
20558 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20559 while Present (Inc_Elmt) loop
20560 Priv_Dep := Node (Inc_Elmt);
20562 if Ekind (Priv_Dep) = E_Subprogram_Type then
20564 -- An Access_To_Subprogram type may have a return type or a
20565 -- parameter type that is incomplete. Replace with the full view.
20567 if Etype (Priv_Dep) = Inc_T then
20568 Set_Etype (Priv_Dep, Full_T);
20572 Formal : Entity_Id;
20575 Formal := First_Formal (Priv_Dep);
20576 while Present (Formal) loop
20577 if Etype (Formal) = Inc_T then
20578 Set_Etype (Formal, Full_T);
20581 Next_Formal (Formal);
20585 elsif Is_Overloadable (Priv_Dep) then
20587 -- If a subprogram in the incomplete dependents list is primitive
20588 -- for a tagged full type then mark it as a dispatching operation,
20589 -- check whether it overrides an inherited subprogram, and check
20590 -- restrictions on its controlling formals. Note that a protected
20591 -- operation is never dispatching: only its wrapper operation
20592 -- (which has convention Ada) is.
20594 if Is_Tagged_Type (Full_T)
20595 and then Is_Primitive (Priv_Dep)
20596 and then Convention (Priv_Dep) /= Convention_Protected
20598 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20599 Set_Is_Dispatching_Operation (Priv_Dep);
20600 Check_Controlling_Formals (Full_T, Priv_Dep);
20603 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20605 -- Can happen during processing of a body before the completion
20606 -- of a TA type. Ignore, because spec is also on dependent list.
20610 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20611 -- corresponding subtype of the full view.
20613 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20614 Set_Subtype_Indication
20615 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20616 Set_Etype (Priv_Dep, Full_T);
20617 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20618 Set_Analyzed (Parent (Priv_Dep), False);
20620 -- Reanalyze the declaration, suppressing the call to
20621 -- Enter_Name to avoid duplicate names.
20623 Analyze_Subtype_Declaration
20624 (N => Parent (Priv_Dep),
20627 -- Dependent is a subtype
20630 -- We build a new subtype indication using the full view of the
20631 -- incomplete parent. The discriminant constraints have been
20632 -- elaborated already at the point of the subtype declaration.
20634 New_Subt := Create_Itype (E_Void, N);
20636 if Has_Discriminants (Full_T) then
20637 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20639 Disc_Constraint := No_Elist;
20642 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20643 Set_Full_View (Priv_Dep, New_Subt);
20646 Next_Elmt (Inc_Elmt);
20648 end Process_Incomplete_Dependents;
20650 --------------------------------
20651 -- Process_Range_Expr_In_Decl --
20652 --------------------------------
20654 procedure Process_Range_Expr_In_Decl
20657 Subtyp : Entity_Id := Empty;
20658 Check_List : List_Id := Empty_List;
20659 R_Check_Off : Boolean := False;
20660 In_Iter_Schm : Boolean := False)
20663 R_Checks : Check_Result;
20664 Insert_Node : Node_Id;
20665 Def_Id : Entity_Id;
20668 Analyze_And_Resolve (R, Base_Type (T));
20670 if Nkind (R) = N_Range then
20672 -- In SPARK, all ranges should be static, with the exception of the
20673 -- discrete type definition of a loop parameter specification.
20675 if not In_Iter_Schm
20676 and then not Is_OK_Static_Range (R)
20678 Check_SPARK_05_Restriction ("range should be static", R);
20681 Lo := Low_Bound (R);
20682 Hi := High_Bound (R);
20684 -- Validity checks on the range of a quantified expression are
20685 -- delayed until the construct is transformed into a loop.
20687 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20688 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20692 -- We need to ensure validity of the bounds here, because if we
20693 -- go ahead and do the expansion, then the expanded code will get
20694 -- analyzed with range checks suppressed and we miss the check.
20696 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20697 -- the temporaries generated by routine Remove_Side_Effects by means
20698 -- of validity checks must use the same names. When a range appears
20699 -- in the parent of a generic, the range is processed with checks
20700 -- disabled as part of the generic context and with checks enabled
20701 -- for code generation purposes. This leads to link issues as the
20702 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20703 -- template sees the temporaries generated by Remove_Side_Effects.
20706 Validity_Check_Range (R, Subtyp);
20709 -- If there were errors in the declaration, try and patch up some
20710 -- common mistakes in the bounds. The cases handled are literals
20711 -- which are Integer where the expected type is Real and vice versa.
20712 -- These corrections allow the compilation process to proceed further
20713 -- along since some basic assumptions of the format of the bounds
20716 if Etype (R) = Any_Type then
20717 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20719 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20721 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20723 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20725 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20727 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20729 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20731 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20738 -- If the bounds of the range have been mistakenly given as string
20739 -- literals (perhaps in place of character literals), then an error
20740 -- has already been reported, but we rewrite the string literal as a
20741 -- bound of the range's type to avoid blowups in later processing
20742 -- that looks at static values.
20744 if Nkind (Lo) = N_String_Literal then
20746 Make_Attribute_Reference (Sloc (Lo),
20747 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20748 Attribute_Name => Name_First));
20749 Analyze_And_Resolve (Lo);
20752 if Nkind (Hi) = N_String_Literal then
20754 Make_Attribute_Reference (Sloc (Hi),
20755 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20756 Attribute_Name => Name_First));
20757 Analyze_And_Resolve (Hi);
20760 -- If bounds aren't scalar at this point then exit, avoiding
20761 -- problems with further processing of the range in this procedure.
20763 if not Is_Scalar_Type (Etype (Lo)) then
20767 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20768 -- then range of the base type. Here we check whether the bounds
20769 -- are in the range of the subtype itself. Note that if the bounds
20770 -- represent the null range the Constraint_Error exception should
20773 -- ??? The following code should be cleaned up as follows
20775 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20776 -- is done in the call to Range_Check (R, T); below
20778 -- 2. The use of R_Check_Off should be investigated and possibly
20779 -- removed, this would clean up things a bit.
20781 if Is_Null_Range (Lo, Hi) then
20785 -- Capture values of bounds and generate temporaries for them
20786 -- if needed, before applying checks, since checks may cause
20787 -- duplication of the expression without forcing evaluation.
20789 -- The forced evaluation removes side effects from expressions,
20790 -- which should occur also in GNATprove mode. Otherwise, we end up
20791 -- with unexpected insertions of actions at places where this is
20792 -- not supposed to occur, e.g. on default parameters of a call.
20794 if Expander_Active or GNATprove_Mode then
20796 -- Call Force_Evaluation to create declarations as needed to
20797 -- deal with side effects, and also create typ_FIRST/LAST
20798 -- entities for bounds if we have a subtype name.
20800 -- Note: we do this transformation even if expansion is not
20801 -- active if we are in GNATprove_Mode since the transformation
20802 -- is in general required to ensure that the resulting tree has
20803 -- proper Ada semantics.
20806 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20808 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20811 -- We use a flag here instead of suppressing checks on the type
20812 -- because the type we check against isn't necessarily the place
20813 -- where we put the check.
20815 if not R_Check_Off then
20816 R_Checks := Get_Range_Checks (R, T);
20818 -- Look up tree to find an appropriate insertion point. We
20819 -- can't just use insert_actions because later processing
20820 -- depends on the insertion node. Prior to Ada 2012 the
20821 -- insertion point could only be a declaration or a loop, but
20822 -- quantified expressions can appear within any context in an
20823 -- expression, and the insertion point can be any statement,
20824 -- pragma, or declaration.
20826 Insert_Node := Parent (R);
20827 while Present (Insert_Node) loop
20829 Nkind (Insert_Node) in N_Declaration
20832 (Insert_Node, N_Component_Declaration,
20833 N_Loop_Parameter_Specification,
20834 N_Function_Specification,
20835 N_Procedure_Specification);
20837 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20838 or else Nkind (Insert_Node) in
20839 N_Statement_Other_Than_Procedure_Call
20840 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20843 Insert_Node := Parent (Insert_Node);
20846 -- Why would Type_Decl not be present??? Without this test,
20847 -- short regression tests fail.
20849 if Present (Insert_Node) then
20851 -- Case of loop statement. Verify that the range is part
20852 -- of the subtype indication of the iteration scheme.
20854 if Nkind (Insert_Node) = N_Loop_Statement then
20859 Indic := Parent (R);
20860 while Present (Indic)
20861 and then Nkind (Indic) /= N_Subtype_Indication
20863 Indic := Parent (Indic);
20866 if Present (Indic) then
20867 Def_Id := Etype (Subtype_Mark (Indic));
20869 Insert_Range_Checks
20873 Sloc (Insert_Node),
20875 Do_Before => True);
20879 -- Insertion before a declaration. If the declaration
20880 -- includes discriminants, the list of applicable checks
20881 -- is given by the caller.
20883 elsif Nkind (Insert_Node) in N_Declaration then
20884 Def_Id := Defining_Identifier (Insert_Node);
20886 if (Ekind (Def_Id) = E_Record_Type
20887 and then Depends_On_Discriminant (R))
20889 (Ekind (Def_Id) = E_Protected_Type
20890 and then Has_Discriminants (Def_Id))
20892 Append_Range_Checks
20894 Check_List, Def_Id, Sloc (Insert_Node), R);
20897 Insert_Range_Checks
20899 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20903 -- Insertion before a statement. Range appears in the
20904 -- context of a quantified expression. Insertion will
20905 -- take place when expression is expanded.
20914 -- Case of other than an explicit N_Range node
20916 -- The forced evaluation removes side effects from expressions, which
20917 -- should occur also in GNATprove mode. Otherwise, we end up with
20918 -- unexpected insertions of actions at places where this is not
20919 -- supposed to occur, e.g. on default parameters of a call.
20921 elsif Expander_Active or GNATprove_Mode then
20922 Get_Index_Bounds (R, Lo, Hi);
20923 Force_Evaluation (Lo);
20924 Force_Evaluation (Hi);
20926 end Process_Range_Expr_In_Decl;
20928 --------------------------------------
20929 -- Process_Real_Range_Specification --
20930 --------------------------------------
20932 procedure Process_Real_Range_Specification (Def : Node_Id) is
20933 Spec : constant Node_Id := Real_Range_Specification (Def);
20936 Err : Boolean := False;
20938 procedure Analyze_Bound (N : Node_Id);
20939 -- Analyze and check one bound
20941 -------------------
20942 -- Analyze_Bound --
20943 -------------------
20945 procedure Analyze_Bound (N : Node_Id) is
20947 Analyze_And_Resolve (N, Any_Real);
20949 if not Is_OK_Static_Expression (N) then
20950 Flag_Non_Static_Expr
20951 ("bound in real type definition is not static!", N);
20956 -- Start of processing for Process_Real_Range_Specification
20959 if Present (Spec) then
20960 Lo := Low_Bound (Spec);
20961 Hi := High_Bound (Spec);
20962 Analyze_Bound (Lo);
20963 Analyze_Bound (Hi);
20965 -- If error, clear away junk range specification
20968 Set_Real_Range_Specification (Def, Empty);
20971 end Process_Real_Range_Specification;
20973 ---------------------
20974 -- Process_Subtype --
20975 ---------------------
20977 function Process_Subtype
20979 Related_Nod : Node_Id;
20980 Related_Id : Entity_Id := Empty;
20981 Suffix : Character := ' ') return Entity_Id
20984 Def_Id : Entity_Id;
20985 Error_Node : Node_Id;
20986 Full_View_Id : Entity_Id;
20987 Subtype_Mark_Id : Entity_Id;
20989 May_Have_Null_Exclusion : Boolean;
20991 procedure Check_Incomplete (T : Node_Id);
20992 -- Called to verify that an incomplete type is not used prematurely
20994 ----------------------
20995 -- Check_Incomplete --
20996 ----------------------
20998 procedure Check_Incomplete (T : Node_Id) is
21000 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21002 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21004 not (Ada_Version >= Ada_2005
21006 (Nkind (Parent (T)) = N_Subtype_Declaration
21007 or else (Nkind (Parent (T)) = N_Subtype_Indication
21008 and then Nkind (Parent (Parent (T))) =
21009 N_Subtype_Declaration)))
21011 Error_Msg_N ("invalid use of type before its full declaration", T);
21013 end Check_Incomplete;
21015 -- Start of processing for Process_Subtype
21018 -- Case of no constraints present
21020 if Nkind (S) /= N_Subtype_Indication then
21022 Check_Incomplete (S);
21025 -- Ada 2005 (AI-231): Static check
21027 if Ada_Version >= Ada_2005
21028 and then Present (P)
21029 and then Null_Exclusion_Present (P)
21030 and then Nkind (P) /= N_Access_To_Object_Definition
21031 and then not Is_Access_Type (Entity (S))
21033 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21036 -- The following is ugly, can't we have a range or even a flag???
21038 May_Have_Null_Exclusion :=
21039 Nkind_In (P, N_Access_Definition,
21040 N_Access_Function_Definition,
21041 N_Access_Procedure_Definition,
21042 N_Access_To_Object_Definition,
21044 N_Component_Definition)
21046 Nkind_In (P, N_Derived_Type_Definition,
21047 N_Discriminant_Specification,
21048 N_Formal_Object_Declaration,
21049 N_Object_Declaration,
21050 N_Object_Renaming_Declaration,
21051 N_Parameter_Specification,
21052 N_Subtype_Declaration);
21054 -- Create an Itype that is a duplicate of Entity (S) but with the
21055 -- null-exclusion attribute.
21057 if May_Have_Null_Exclusion
21058 and then Is_Access_Type (Entity (S))
21059 and then Null_Exclusion_Present (P)
21061 -- No need to check the case of an access to object definition.
21062 -- It is correct to define double not-null pointers.
21065 -- type Not_Null_Int_Ptr is not null access Integer;
21066 -- type Acc is not null access Not_Null_Int_Ptr;
21068 and then Nkind (P) /= N_Access_To_Object_Definition
21070 if Can_Never_Be_Null (Entity (S)) then
21071 case Nkind (Related_Nod) is
21072 when N_Full_Type_Declaration =>
21073 if Nkind (Type_Definition (Related_Nod))
21074 in N_Array_Type_Definition
21078 (Component_Definition
21079 (Type_Definition (Related_Nod)));
21082 Subtype_Indication (Type_Definition (Related_Nod));
21085 when N_Subtype_Declaration =>
21086 Error_Node := Subtype_Indication (Related_Nod);
21088 when N_Object_Declaration =>
21089 Error_Node := Object_Definition (Related_Nod);
21091 when N_Component_Declaration =>
21093 Subtype_Indication (Component_Definition (Related_Nod));
21095 when N_Allocator =>
21096 Error_Node := Expression (Related_Nod);
21099 pragma Assert (False);
21100 Error_Node := Related_Nod;
21104 ("`NOT NULL` not allowed (& already excludes null)",
21110 Create_Null_Excluding_Itype
21112 Related_Nod => P));
21113 Set_Entity (S, Etype (S));
21118 -- Case of constraint present, so that we have an N_Subtype_Indication
21119 -- node (this node is created only if constraints are present).
21122 Find_Type (Subtype_Mark (S));
21124 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21126 (Nkind (Parent (S)) = N_Subtype_Declaration
21127 and then Is_Itype (Defining_Identifier (Parent (S))))
21129 Check_Incomplete (Subtype_Mark (S));
21133 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21135 -- Explicit subtype declaration case
21137 if Nkind (P) = N_Subtype_Declaration then
21138 Def_Id := Defining_Identifier (P);
21140 -- Explicit derived type definition case
21142 elsif Nkind (P) = N_Derived_Type_Definition then
21143 Def_Id := Defining_Identifier (Parent (P));
21145 -- Implicit case, the Def_Id must be created as an implicit type.
21146 -- The one exception arises in the case of concurrent types, array
21147 -- and access types, where other subsidiary implicit types may be
21148 -- created and must appear before the main implicit type. In these
21149 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21150 -- has not yet been called to create Def_Id.
21153 if Is_Array_Type (Subtype_Mark_Id)
21154 or else Is_Concurrent_Type (Subtype_Mark_Id)
21155 or else Is_Access_Type (Subtype_Mark_Id)
21159 -- For the other cases, we create a new unattached Itype,
21160 -- and set the indication to ensure it gets attached later.
21164 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21168 -- If the kind of constraint is invalid for this kind of type,
21169 -- then give an error, and then pretend no constraint was given.
21171 if not Is_Valid_Constraint_Kind
21172 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21175 ("incorrect constraint for this kind of type", Constraint (S));
21177 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21179 -- Set Ekind of orphan itype, to prevent cascaded errors
21181 if Present (Def_Id) then
21182 Set_Ekind (Def_Id, Ekind (Any_Type));
21185 -- Make recursive call, having got rid of the bogus constraint
21187 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21190 -- Remaining processing depends on type. Select on Base_Type kind to
21191 -- ensure getting to the concrete type kind in the case of a private
21192 -- subtype (needed when only doing semantic analysis).
21194 case Ekind (Base_Type (Subtype_Mark_Id)) is
21195 when Access_Kind =>
21197 -- If this is a constraint on a class-wide type, discard it.
21198 -- There is currently no way to express a partial discriminant
21199 -- constraint on a type with unknown discriminants. This is
21200 -- a pathology that the ACATS wisely decides not to test.
21202 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21203 if Comes_From_Source (S) then
21205 ("constraint on class-wide type ignored??",
21209 if Nkind (P) = N_Subtype_Declaration then
21210 Set_Subtype_Indication (P,
21211 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21214 return Subtype_Mark_Id;
21217 Constrain_Access (Def_Id, S, Related_Nod);
21220 and then Is_Itype (Designated_Type (Def_Id))
21221 and then Nkind (Related_Nod) = N_Subtype_Declaration
21222 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21224 Build_Itype_Reference
21225 (Designated_Type (Def_Id), Related_Nod);
21229 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21231 when Decimal_Fixed_Point_Kind =>
21232 Constrain_Decimal (Def_Id, S);
21234 when Enumeration_Kind =>
21235 Constrain_Enumeration (Def_Id, S);
21236 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21238 when Ordinary_Fixed_Point_Kind =>
21239 Constrain_Ordinary_Fixed (Def_Id, S);
21242 Constrain_Float (Def_Id, S);
21244 when Integer_Kind =>
21245 Constrain_Integer (Def_Id, S);
21246 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21248 when Class_Wide_Kind
21249 | E_Incomplete_Type
21253 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21255 if Ekind (Def_Id) = E_Incomplete_Type then
21256 Set_Private_Dependents (Def_Id, New_Elmt_List);
21259 when Private_Kind =>
21260 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21262 -- The base type may be private but Def_Id may be a full view
21265 if Is_Private_Type (Def_Id) then
21266 Set_Private_Dependents (Def_Id, New_Elmt_List);
21269 -- In case of an invalid constraint prevent further processing
21270 -- since the type constructed is missing expected fields.
21272 if Etype (Def_Id) = Any_Type then
21276 -- If the full view is that of a task with discriminants,
21277 -- we must constrain both the concurrent type and its
21278 -- corresponding record type. Otherwise we will just propagate
21279 -- the constraint to the full view, if available.
21281 if Present (Full_View (Subtype_Mark_Id))
21282 and then Has_Discriminants (Subtype_Mark_Id)
21283 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21286 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21288 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21289 Constrain_Concurrent (Full_View_Id, S,
21290 Related_Nod, Related_Id, Suffix);
21291 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21292 Set_Full_View (Def_Id, Full_View_Id);
21294 -- Introduce an explicit reference to the private subtype,
21295 -- to prevent scope anomalies in gigi if first use appears
21296 -- in a nested context, e.g. a later function body.
21297 -- Should this be generated in other contexts than a full
21298 -- type declaration?
21300 if Is_Itype (Def_Id)
21302 Nkind (Parent (P)) = N_Full_Type_Declaration
21304 Build_Itype_Reference (Def_Id, Parent (P));
21308 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21311 when Concurrent_Kind =>
21312 Constrain_Concurrent (Def_Id, S,
21313 Related_Nod, Related_Id, Suffix);
21316 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21319 -- Size and Convention are always inherited from the base type
21321 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21322 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21326 end Process_Subtype;
21328 -----------------------------
21329 -- Record_Type_Declaration --
21330 -----------------------------
21332 procedure Record_Type_Declaration
21337 Def : constant Node_Id := Type_Definition (N);
21338 Is_Tagged : Boolean;
21339 Tag_Comp : Entity_Id;
21342 -- These flags must be initialized before calling Process_Discriminants
21343 -- because this routine makes use of them.
21345 Set_Ekind (T, E_Record_Type);
21347 Init_Size_Align (T);
21348 Set_Interfaces (T, No_Elist);
21349 Set_Stored_Constraint (T, No_Elist);
21350 Set_Default_SSO (T);
21354 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21355 if Limited_Present (Def) then
21356 Check_SPARK_05_Restriction ("limited is not allowed", N);
21359 if Abstract_Present (Def) then
21360 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21363 -- The flag Is_Tagged_Type might have already been set by
21364 -- Find_Type_Name if it detected an error for declaration T. This
21365 -- arises in the case of private tagged types where the full view
21366 -- omits the word tagged.
21369 Tagged_Present (Def)
21370 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21372 Set_Is_Limited_Record (T, Limited_Present (Def));
21375 Set_Is_Tagged_Type (T, True);
21376 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21379 -- Type is abstract if full declaration carries keyword, or if
21380 -- previous partial view did.
21382 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21383 or else Abstract_Present (Def));
21386 Check_SPARK_05_Restriction ("interface is not allowed", N);
21389 Analyze_Interface_Declaration (T, Def);
21391 if Present (Discriminant_Specifications (N)) then
21393 ("interface types cannot have discriminants",
21394 Defining_Identifier
21395 (First (Discriminant_Specifications (N))));
21399 -- First pass: if there are self-referential access components,
21400 -- create the required anonymous access type declarations, and if
21401 -- need be an incomplete type declaration for T itself.
21403 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21405 if Ada_Version >= Ada_2005
21406 and then Present (Interface_List (Def))
21408 Check_Interfaces (N, Def);
21411 Ifaces_List : Elist_Id;
21414 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21415 -- already in the parents.
21419 Ifaces_List => Ifaces_List,
21420 Exclude_Parents => True);
21422 Set_Interfaces (T, Ifaces_List);
21426 -- Records constitute a scope for the component declarations within.
21427 -- The scope is created prior to the processing of these declarations.
21428 -- Discriminants are processed first, so that they are visible when
21429 -- processing the other components. The Ekind of the record type itself
21430 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21432 -- Enter record scope
21436 -- If an incomplete or private type declaration was already given for
21437 -- the type, then this scope already exists, and the discriminants have
21438 -- been declared within. We must verify that the full declaration
21439 -- matches the incomplete one.
21441 Check_Or_Process_Discriminants (N, T, Prev);
21443 Set_Is_Constrained (T, not Has_Discriminants (T));
21444 Set_Has_Delayed_Freeze (T, True);
21446 -- For tagged types add a manually analyzed component corresponding
21447 -- to the component _tag, the corresponding piece of tree will be
21448 -- expanded as part of the freezing actions if it is not a CPP_Class.
21452 -- Do not add the tag unless we are in expansion mode
21454 if Expander_Active then
21455 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21456 Enter_Name (Tag_Comp);
21458 Set_Ekind (Tag_Comp, E_Component);
21459 Set_Is_Tag (Tag_Comp);
21460 Set_Is_Aliased (Tag_Comp);
21461 Set_Etype (Tag_Comp, RTE (RE_Tag));
21462 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21463 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21464 Init_Component_Location (Tag_Comp);
21466 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21467 -- implemented interfaces.
21469 if Has_Interfaces (T) then
21470 Add_Interface_Tag_Components (N, T);
21474 Make_Class_Wide_Type (T);
21475 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21478 -- We must suppress range checks when processing record components in
21479 -- the presence of discriminants, since we don't want spurious checks to
21480 -- be generated during their analysis, but Suppress_Range_Checks flags
21481 -- must be reset the after processing the record definition.
21483 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21484 -- couldn't we just use the normal range check suppression method here.
21485 -- That would seem cleaner ???
21487 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21488 Set_Kill_Range_Checks (T, True);
21489 Record_Type_Definition (Def, Prev);
21490 Set_Kill_Range_Checks (T, False);
21492 Record_Type_Definition (Def, Prev);
21495 -- Exit from record scope
21499 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21500 -- the implemented interfaces and associate them an aliased entity.
21503 and then not Is_Empty_List (Interface_List (Def))
21505 Derive_Progenitor_Subprograms (T, T);
21508 Check_Function_Writable_Actuals (N);
21509 end Record_Type_Declaration;
21511 ----------------------------
21512 -- Record_Type_Definition --
21513 ----------------------------
21515 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21516 Component : Entity_Id;
21517 Ctrl_Components : Boolean := False;
21518 Final_Storage_Only : Boolean;
21522 if Ekind (Prev_T) = E_Incomplete_Type then
21523 T := Full_View (Prev_T);
21528 -- In SPARK, tagged types and type extensions may only be declared in
21529 -- the specification of library unit packages.
21531 if Present (Def) and then Is_Tagged_Type (T) then
21537 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21538 Typ := Parent (Def);
21541 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21542 Typ := Parent (Parent (Def));
21545 Ctxt := Parent (Typ);
21547 if Nkind (Ctxt) = N_Package_Body
21548 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21550 Check_SPARK_05_Restriction
21551 ("type should be defined in package specification", Typ);
21553 elsif Nkind (Ctxt) /= N_Package_Specification
21554 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21556 Check_SPARK_05_Restriction
21557 ("type should be defined in library unit package", Typ);
21562 Final_Storage_Only := not Is_Controlled_Active (T);
21564 -- Ada 2005: Check whether an explicit Limited is present in a derived
21565 -- type declaration.
21567 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21568 and then Limited_Present (Parent (Def))
21570 Set_Is_Limited_Record (T);
21573 -- If the component list of a record type is defined by the reserved
21574 -- word null and there is no discriminant part, then the record type has
21575 -- no components and all records of the type are null records (RM 3.7)
21576 -- This procedure is also called to process the extension part of a
21577 -- record extension, in which case the current scope may have inherited
21581 or else No (Component_List (Def))
21582 or else Null_Present (Component_List (Def))
21584 if not Is_Tagged_Type (T) then
21585 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21589 Analyze_Declarations (Component_Items (Component_List (Def)));
21591 if Present (Variant_Part (Component_List (Def))) then
21592 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21593 Analyze (Variant_Part (Component_List (Def)));
21597 -- After completing the semantic analysis of the record definition,
21598 -- record components, both new and inherited, are accessible. Set their
21599 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21600 -- whose Ekind may be void.
21602 Component := First_Entity (Current_Scope);
21603 while Present (Component) loop
21604 if Ekind (Component) = E_Void
21605 and then not Is_Itype (Component)
21607 Set_Ekind (Component, E_Component);
21608 Init_Component_Location (Component);
21611 Propagate_Concurrent_Flags (T, Etype (Component));
21613 if Ekind (Component) /= E_Component then
21616 -- Do not set Has_Controlled_Component on a class-wide equivalent
21617 -- type. See Make_CW_Equivalent_Type.
21619 elsif not Is_Class_Wide_Equivalent_Type (T)
21620 and then (Has_Controlled_Component (Etype (Component))
21621 or else (Chars (Component) /= Name_uParent
21622 and then Is_Controlled_Active
21623 (Etype (Component))))
21625 Set_Has_Controlled_Component (T, True);
21626 Final_Storage_Only :=
21628 and then Finalize_Storage_Only (Etype (Component));
21629 Ctrl_Components := True;
21632 Next_Entity (Component);
21635 -- A Type is Finalize_Storage_Only only if all its controlled components
21638 if Ctrl_Components then
21639 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21642 -- Place reference to end record on the proper entity, which may
21643 -- be a partial view.
21645 if Present (Def) then
21646 Process_End_Label (Def, 'e', Prev_T);
21648 end Record_Type_Definition;
21650 ------------------------
21651 -- Replace_Components --
21652 ------------------------
21654 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21655 function Process (N : Node_Id) return Traverse_Result;
21661 function Process (N : Node_Id) return Traverse_Result is
21665 if Nkind (N) = N_Discriminant_Specification then
21666 Comp := First_Discriminant (Typ);
21667 while Present (Comp) loop
21668 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21669 Set_Defining_Identifier (N, Comp);
21673 Next_Discriminant (Comp);
21676 elsif Nkind (N) = N_Component_Declaration then
21677 Comp := First_Component (Typ);
21678 while Present (Comp) loop
21679 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21680 Set_Defining_Identifier (N, Comp);
21684 Next_Component (Comp);
21691 procedure Replace is new Traverse_Proc (Process);
21693 -- Start of processing for Replace_Components
21697 end Replace_Components;
21699 -------------------------------
21700 -- Set_Completion_Referenced --
21701 -------------------------------
21703 procedure Set_Completion_Referenced (E : Entity_Id) is
21705 -- If in main unit, mark entity that is a completion as referenced,
21706 -- warnings go on the partial view when needed.
21708 if In_Extended_Main_Source_Unit (E) then
21709 Set_Referenced (E);
21711 end Set_Completion_Referenced;
21713 ---------------------
21714 -- Set_Default_SSO --
21715 ---------------------
21717 procedure Set_Default_SSO (T : Entity_Id) is
21719 case Opt.Default_SSO is
21723 Set_SSO_Set_Low_By_Default (T, True);
21725 Set_SSO_Set_High_By_Default (T, True);
21727 raise Program_Error;
21729 end Set_Default_SSO;
21731 ---------------------
21732 -- Set_Fixed_Range --
21733 ---------------------
21735 -- The range for fixed-point types is complicated by the fact that we
21736 -- do not know the exact end points at the time of the declaration. This
21737 -- is true for three reasons:
21739 -- A size clause may affect the fudging of the end-points.
21740 -- A small clause may affect the values of the end-points.
21741 -- We try to include the end-points if it does not affect the size.
21743 -- This means that the actual end-points must be established at the
21744 -- point when the type is frozen. Meanwhile, we first narrow the range
21745 -- as permitted (so that it will fit if necessary in a small specified
21746 -- size), and then build a range subtree with these narrowed bounds.
21747 -- Set_Fixed_Range constructs the range from real literal values, and
21748 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21750 -- The parent of this range is set to point to the entity so that it is
21751 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21752 -- other scalar types, which are just pointers to the range in the
21753 -- original tree, this would otherwise be an orphan).
21755 -- The tree is left unanalyzed. When the type is frozen, the processing
21756 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21757 -- analyzed, and uses this as an indication that it should complete
21758 -- work on the range (it will know the final small and size values).
21760 procedure Set_Fixed_Range
21766 S : constant Node_Id :=
21768 Low_Bound => Make_Real_Literal (Loc, Lo),
21769 High_Bound => Make_Real_Literal (Loc, Hi));
21771 Set_Scalar_Range (E, S);
21774 -- Before the freeze point, the bounds of a fixed point are universal
21775 -- and carry the corresponding type.
21777 Set_Etype (Low_Bound (S), Universal_Real);
21778 Set_Etype (High_Bound (S), Universal_Real);
21779 end Set_Fixed_Range;
21781 ----------------------------------
21782 -- Set_Scalar_Range_For_Subtype --
21783 ----------------------------------
21785 procedure Set_Scalar_Range_For_Subtype
21786 (Def_Id : Entity_Id;
21790 Kind : constant Entity_Kind := Ekind (Def_Id);
21793 -- Defend against previous error
21795 if Nkind (R) = N_Error then
21799 Set_Scalar_Range (Def_Id, R);
21801 -- We need to link the range into the tree before resolving it so
21802 -- that types that are referenced, including importantly the subtype
21803 -- itself, are properly frozen (Freeze_Expression requires that the
21804 -- expression be properly linked into the tree). Of course if it is
21805 -- already linked in, then we do not disturb the current link.
21807 if No (Parent (R)) then
21808 Set_Parent (R, Def_Id);
21811 -- Reset the kind of the subtype during analysis of the range, to
21812 -- catch possible premature use in the bounds themselves.
21814 Set_Ekind (Def_Id, E_Void);
21815 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21816 Set_Ekind (Def_Id, Kind);
21817 end Set_Scalar_Range_For_Subtype;
21819 --------------------------------------------------------
21820 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21821 --------------------------------------------------------
21823 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21827 -- Make sure set if encountered during Expand_To_Stored_Constraint
21829 Set_Stored_Constraint (E, No_Elist);
21831 -- Give it the right value
21833 if Is_Constrained (E) and then Has_Discriminants (E) then
21834 Set_Stored_Constraint (E,
21835 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21837 end Set_Stored_Constraint_From_Discriminant_Constraint;
21839 -------------------------------------
21840 -- Signed_Integer_Type_Declaration --
21841 -------------------------------------
21843 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21844 Implicit_Base : Entity_Id;
21845 Base_Typ : Entity_Id;
21848 Errs : Boolean := False;
21852 function Can_Derive_From (E : Entity_Id) return Boolean;
21853 -- Determine whether given bounds allow derivation from specified type
21855 procedure Check_Bound (Expr : Node_Id);
21856 -- Check bound to make sure it is integral and static. If not, post
21857 -- appropriate error message and set Errs flag
21859 ---------------------
21860 -- Can_Derive_From --
21861 ---------------------
21863 -- Note we check both bounds against both end values, to deal with
21864 -- strange types like ones with a range of 0 .. -12341234.
21866 function Can_Derive_From (E : Entity_Id) return Boolean is
21867 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21868 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21870 return Lo <= Lo_Val and then Lo_Val <= Hi
21872 Lo <= Hi_Val and then Hi_Val <= Hi;
21873 end Can_Derive_From;
21879 procedure Check_Bound (Expr : Node_Id) is
21881 -- If a range constraint is used as an integer type definition, each
21882 -- bound of the range must be defined by a static expression of some
21883 -- integer type, but the two bounds need not have the same integer
21884 -- type (Negative bounds are allowed.) (RM 3.5.4)
21886 if not Is_Integer_Type (Etype (Expr)) then
21888 ("integer type definition bounds must be of integer type", Expr);
21891 elsif not Is_OK_Static_Expression (Expr) then
21892 Flag_Non_Static_Expr
21893 ("non-static expression used for integer type bound!", Expr);
21896 -- The bounds are folded into literals, and we set their type to be
21897 -- universal, to avoid typing difficulties: we cannot set the type
21898 -- of the literal to the new type, because this would be a forward
21899 -- reference for the back end, and if the original type is user-
21900 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21903 if Is_Entity_Name (Expr) then
21904 Fold_Uint (Expr, Expr_Value (Expr), True);
21907 Set_Etype (Expr, Universal_Integer);
21911 -- Start of processing for Signed_Integer_Type_Declaration
21914 -- Create an anonymous base type
21917 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21919 -- Analyze and check the bounds, they can be of any integer type
21921 Lo := Low_Bound (Def);
21922 Hi := High_Bound (Def);
21924 -- Arbitrarily use Integer as the type if either bound had an error
21926 if Hi = Error or else Lo = Error then
21927 Base_Typ := Any_Integer;
21928 Set_Error_Posted (T, True);
21930 -- Here both bounds are OK expressions
21933 Analyze_And_Resolve (Lo, Any_Integer);
21934 Analyze_And_Resolve (Hi, Any_Integer);
21940 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21941 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21944 -- Find type to derive from
21946 Lo_Val := Expr_Value (Lo);
21947 Hi_Val := Expr_Value (Hi);
21949 if Can_Derive_From (Standard_Short_Short_Integer) then
21950 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21952 elsif Can_Derive_From (Standard_Short_Integer) then
21953 Base_Typ := Base_Type (Standard_Short_Integer);
21955 elsif Can_Derive_From (Standard_Integer) then
21956 Base_Typ := Base_Type (Standard_Integer);
21958 elsif Can_Derive_From (Standard_Long_Integer) then
21959 Base_Typ := Base_Type (Standard_Long_Integer);
21961 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21962 Check_Restriction (No_Long_Long_Integers, Def);
21963 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21966 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21967 Error_Msg_N ("integer type definition bounds out of range", Def);
21968 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21969 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21973 -- Complete both implicit base and declared first subtype entities. The
21974 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21975 -- are not clobbered when the signed integer type acts as a full view of
21978 Set_Etype (Implicit_Base, Base_Typ);
21979 Set_Size_Info (Implicit_Base, Base_Typ);
21980 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21981 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21982 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21984 Set_Ekind (T, E_Signed_Integer_Subtype);
21985 Set_Etype (T, Implicit_Base);
21986 Set_Size_Info (T, Implicit_Base);
21987 Inherit_Rep_Item_Chain (T, Implicit_Base);
21988 Set_Scalar_Range (T, Def);
21989 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21990 Set_Is_Constrained (T);
21991 end Signed_Integer_Type_Declaration;