1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2018, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Einfo; use Einfo;
33 with Errout; use Errout;
34 with Eval_Fat; use Eval_Fat;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dist; use Exp_Dist;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Elab; use Sem_Elab;
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,
1303 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 -- If the access_to_subprogram is not declared at the library level,
1309 -- it can only point to subprograms that are at the same or deeper
1310 -- accessibility level. All such might require an activation record
1311 -- when compiling for C.
1313 Set_Needs_Activation_Record (T_Name,
1314 not Is_Library_Level_Entity (T_Name));
1315 Set_Needs_Activation_Record (Desig_Type,
1316 not Is_Library_Level_Entity (T_Name));
1318 Generate_Reference_To_Formals (T_Name);
1320 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1322 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1324 Check_Restriction (No_Access_Subprograms, T_Def);
1325 end Access_Subprogram_Declaration;
1327 ----------------------------
1328 -- Access_Type_Declaration --
1329 ----------------------------
1331 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1332 P : constant Node_Id := Parent (Def);
1333 S : constant Node_Id := Subtype_Indication (Def);
1335 Full_Desig : Entity_Id;
1338 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1340 -- Check for permissible use of incomplete type
1342 if Nkind (S) /= N_Subtype_Indication then
1345 if Present (Entity (S))
1346 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1348 Set_Directly_Designated_Type (T, Entity (S));
1350 -- If the designated type is a limited view, we cannot tell if
1351 -- the full view contains tasks, and there is no way to handle
1352 -- that full view in a client. We create a master entity for the
1353 -- scope, which will be used when a client determines that one
1356 if From_Limited_With (Entity (S))
1357 and then not Is_Class_Wide_Type (Entity (S))
1359 Set_Ekind (T, E_Access_Type);
1360 Build_Master_Entity (T);
1361 Build_Master_Renaming (T);
1365 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1368 -- If the access definition is of the form: ACCESS NOT NULL ..
1369 -- the subtype indication must be of an access type. Create
1370 -- a null-excluding subtype of it.
1372 if Null_Excluding_Subtype (Def) then
1373 if not Is_Access_Type (Entity (S)) then
1374 Error_Msg_N ("null exclusion must apply to access type", Def);
1378 Loc : constant Source_Ptr := Sloc (S);
1380 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1384 Make_Subtype_Declaration (Loc,
1385 Defining_Identifier => Nam,
1386 Subtype_Indication =>
1387 New_Occurrence_Of (Entity (S), Loc));
1388 Set_Null_Exclusion_Present (Decl);
1389 Insert_Before (Parent (Def), Decl);
1391 Set_Entity (S, Nam);
1397 Set_Directly_Designated_Type (T,
1398 Process_Subtype (S, P, T, 'P'));
1401 if All_Present (Def) or Constant_Present (Def) then
1402 Set_Ekind (T, E_General_Access_Type);
1404 Set_Ekind (T, E_Access_Type);
1407 Full_Desig := Designated_Type (T);
1409 if Base_Type (Full_Desig) = T then
1410 Error_Msg_N ("access type cannot designate itself", S);
1412 -- In Ada 2005, the type may have a limited view through some unit in
1413 -- its own context, allowing the following circularity that cannot be
1414 -- detected earlier.
1416 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1419 ("access type cannot designate its own class-wide type", S);
1421 -- Clean up indication of tagged status to prevent cascaded errors
1423 Set_Is_Tagged_Type (T, False);
1428 -- If the type has appeared already in a with_type clause, it is frozen
1429 -- and the pointer size is already set. Else, initialize.
1431 if not From_Limited_With (T) then
1432 Init_Size_Align (T);
1435 -- Note that Has_Task is always false, since the access type itself
1436 -- is not a task type. See Einfo for more description on this point.
1437 -- Exactly the same consideration applies to Has_Controlled_Component
1438 -- and to Has_Protected.
1440 Set_Has_Task (T, False);
1441 Set_Has_Protected (T, False);
1442 Set_Has_Timing_Event (T, False);
1443 Set_Has_Controlled_Component (T, False);
1445 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1446 -- problems where an incomplete view of this entity has been previously
1447 -- established by a limited with and an overlaid version of this field
1448 -- (Stored_Constraint) was initialized for the incomplete view.
1450 -- This reset is performed in most cases except where the access type
1451 -- has been created for the purposes of allocating or deallocating a
1452 -- build-in-place object. Such access types have explicitly set pools
1453 -- and finalization masters.
1455 if No (Associated_Storage_Pool (T)) then
1456 Set_Finalization_Master (T, Empty);
1459 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1462 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1463 Set_Is_Access_Constant (T, Constant_Present (Def));
1464 end Access_Type_Declaration;
1466 ----------------------------------
1467 -- Add_Interface_Tag_Components --
1468 ----------------------------------
1470 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1471 Loc : constant Source_Ptr := Sloc (N);
1475 procedure Add_Tag (Iface : Entity_Id);
1476 -- Add tag for one of the progenitor interfaces
1482 procedure Add_Tag (Iface : Entity_Id) is
1489 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1491 -- This is a reasonable place to propagate predicates
1493 if Has_Predicates (Iface) then
1494 Set_Has_Predicates (Typ);
1498 Make_Component_Definition (Loc,
1499 Aliased_Present => True,
1500 Subtype_Indication =>
1501 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1503 Tag := Make_Temporary (Loc, 'V');
1506 Make_Component_Declaration (Loc,
1507 Defining_Identifier => Tag,
1508 Component_Definition => Def);
1510 Analyze_Component_Declaration (Decl);
1512 Set_Analyzed (Decl);
1513 Set_Ekind (Tag, E_Component);
1515 Set_Is_Aliased (Tag);
1516 Set_Related_Type (Tag, Iface);
1517 Init_Component_Location (Tag);
1519 pragma Assert (Is_Frozen (Iface));
1521 Set_DT_Entry_Count (Tag,
1522 DT_Entry_Count (First_Entity (Iface)));
1524 if No (Last_Tag) then
1527 Insert_After (Last_Tag, Decl);
1532 -- If the ancestor has discriminants we need to give special support
1533 -- to store the offset_to_top value of the secondary dispatch tables.
1534 -- For this purpose we add a supplementary component just after the
1535 -- field that contains the tag associated with each secondary DT.
1537 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1539 Make_Component_Definition (Loc,
1540 Subtype_Indication =>
1541 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1543 Offset := Make_Temporary (Loc, 'V');
1546 Make_Component_Declaration (Loc,
1547 Defining_Identifier => Offset,
1548 Component_Definition => Def);
1550 Analyze_Component_Declaration (Decl);
1552 Set_Analyzed (Decl);
1553 Set_Ekind (Offset, E_Component);
1554 Set_Is_Aliased (Offset);
1555 Set_Related_Type (Offset, Iface);
1556 Init_Component_Location (Offset);
1557 Insert_After (Last_Tag, Decl);
1568 -- Start of processing for Add_Interface_Tag_Components
1571 if not RTE_Available (RE_Interface_Tag) then
1573 ("(Ada 2005) interface types not supported by this run-time!",
1578 if Ekind (Typ) /= E_Record_Type
1579 or else (Is_Concurrent_Record_Type (Typ)
1580 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1581 or else (not Is_Concurrent_Record_Type (Typ)
1582 and then No (Interfaces (Typ))
1583 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1588 -- Find the current last tag
1590 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1591 Ext := Record_Extension_Part (Type_Definition (N));
1593 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1594 Ext := Type_Definition (N);
1599 if not (Present (Component_List (Ext))) then
1600 Set_Null_Present (Ext, False);
1602 Set_Component_List (Ext,
1603 Make_Component_List (Loc,
1604 Component_Items => L,
1605 Null_Present => False));
1607 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1608 L := Component_Items
1610 (Record_Extension_Part
1611 (Type_Definition (N))));
1613 L := Component_Items
1615 (Type_Definition (N)));
1618 -- Find the last tag component
1621 while Present (Comp) loop
1622 if Nkind (Comp) = N_Component_Declaration
1623 and then Is_Tag (Defining_Identifier (Comp))
1632 -- At this point L references the list of components and Last_Tag
1633 -- references the current last tag (if any). Now we add the tag
1634 -- corresponding with all the interfaces that are not implemented
1637 if Present (Interfaces (Typ)) then
1638 Elmt := First_Elmt (Interfaces (Typ));
1639 while Present (Elmt) loop
1640 Add_Tag (Node (Elmt));
1644 end Add_Interface_Tag_Components;
1646 -------------------------------------
1647 -- Add_Internal_Interface_Entities --
1648 -------------------------------------
1650 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1653 Iface_Elmt : Elmt_Id;
1654 Iface_Prim : Entity_Id;
1655 Ifaces_List : Elist_Id;
1656 New_Subp : Entity_Id := Empty;
1658 Restore_Scope : Boolean := False;
1661 pragma Assert (Ada_Version >= Ada_2005
1662 and then Is_Record_Type (Tagged_Type)
1663 and then Is_Tagged_Type (Tagged_Type)
1664 and then Has_Interfaces (Tagged_Type)
1665 and then not Is_Interface (Tagged_Type));
1667 -- Ensure that the internal entities are added to the scope of the type
1669 if Scope (Tagged_Type) /= Current_Scope then
1670 Push_Scope (Scope (Tagged_Type));
1671 Restore_Scope := True;
1674 Collect_Interfaces (Tagged_Type, Ifaces_List);
1676 Iface_Elmt := First_Elmt (Ifaces_List);
1677 while Present (Iface_Elmt) loop
1678 Iface := Node (Iface_Elmt);
1680 -- Originally we excluded here from this processing interfaces that
1681 -- are parents of Tagged_Type because their primitives are located
1682 -- in the primary dispatch table (and hence no auxiliary internal
1683 -- entities are required to handle secondary dispatch tables in such
1684 -- case). However, these auxiliary entities are also required to
1685 -- handle derivations of interfaces in formals of generics (see
1686 -- Derive_Subprograms).
1688 Elmt := First_Elmt (Primitive_Operations (Iface));
1689 while Present (Elmt) loop
1690 Iface_Prim := Node (Elmt);
1692 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1694 Find_Primitive_Covering_Interface
1695 (Tagged_Type => Tagged_Type,
1696 Iface_Prim => Iface_Prim);
1698 if No (Prim) and then Serious_Errors_Detected > 0 then
1702 pragma Assert (Present (Prim));
1704 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1705 -- differs from the name of the interface primitive then it is
1706 -- a private primitive inherited from a parent type. In such
1707 -- case, given that Tagged_Type covers the interface, the
1708 -- inherited private primitive becomes visible. For such
1709 -- purpose we add a new entity that renames the inherited
1710 -- private primitive.
1712 if Chars (Prim) /= Chars (Iface_Prim) then
1713 pragma Assert (Has_Suffix (Prim, 'P'));
1715 (New_Subp => New_Subp,
1716 Parent_Subp => Iface_Prim,
1717 Derived_Type => Tagged_Type,
1718 Parent_Type => Iface);
1719 Set_Alias (New_Subp, Prim);
1720 Set_Is_Abstract_Subprogram
1721 (New_Subp, Is_Abstract_Subprogram (Prim));
1725 (New_Subp => New_Subp,
1726 Parent_Subp => Iface_Prim,
1727 Derived_Type => Tagged_Type,
1728 Parent_Type => Iface);
1733 if Is_Inherited_Operation (Prim)
1734 and then Present (Alias (Prim))
1736 Anc := Alias (Prim);
1738 Anc := Overridden_Operation (Prim);
1741 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1742 -- nonconforming preconditions in both an ancestor and
1743 -- a progenitor operation.
1745 -- If the operation is a primitive wrapper it is an explicit
1746 -- (overriding) operqtion and all is fine.
1749 and then Has_Non_Trivial_Precondition (Anc)
1750 and then Has_Non_Trivial_Precondition (Iface_Prim)
1752 if Is_Abstract_Subprogram (Prim)
1754 (Ekind (Prim) = E_Procedure
1755 and then Nkind (Parent (Prim)) =
1756 N_Procedure_Specification
1757 and then Null_Present (Parent (Prim)))
1758 or else Is_Primitive_Wrapper (Prim)
1762 -- The operation is inherited and must be overridden
1764 elsif not Comes_From_Source (Prim) then
1766 ("&inherits non-conforming preconditions and must "
1767 & "be overridden (RM 6.1.1 (10-16)",
1768 Parent (Tagged_Type), Prim);
1773 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1774 -- associated with interface types. These entities are
1775 -- only registered in the list of primitives of its
1776 -- corresponding tagged type because they are only used
1777 -- to fill the contents of the secondary dispatch tables.
1778 -- Therefore they are removed from the homonym chains.
1780 Set_Is_Hidden (New_Subp);
1781 Set_Is_Internal (New_Subp);
1782 Set_Alias (New_Subp, Prim);
1783 Set_Is_Abstract_Subprogram
1784 (New_Subp, Is_Abstract_Subprogram (Prim));
1785 Set_Interface_Alias (New_Subp, Iface_Prim);
1787 -- If the returned type is an interface then propagate it to
1788 -- the returned type. Needed by the thunk to generate the code
1789 -- which displaces "this" to reference the corresponding
1790 -- secondary dispatch table in the returned object.
1792 if Is_Interface (Etype (Iface_Prim)) then
1793 Set_Etype (New_Subp, Etype (Iface_Prim));
1796 -- Internal entities associated with interface types are only
1797 -- registered in the list of primitives of the tagged type.
1798 -- They are only used to fill the contents of the secondary
1799 -- dispatch tables. Therefore they are not needed in the
1802 Remove_Homonym (New_Subp);
1804 -- Hidden entities associated with interfaces must have set
1805 -- the Has_Delay_Freeze attribute to ensure that, in case
1806 -- of locally defined tagged types (or compiling with static
1807 -- dispatch tables generation disabled) the corresponding
1808 -- entry of the secondary dispatch table is filled when such
1809 -- an entity is frozen. This is an expansion activity that must
1810 -- be suppressed for ASIS because it leads to gigi elaboration
1811 -- issues in annotate mode.
1813 if not ASIS_Mode then
1814 Set_Has_Delayed_Freeze (New_Subp);
1822 Next_Elmt (Iface_Elmt);
1825 if Restore_Scope then
1828 end Add_Internal_Interface_Entities;
1830 -----------------------------------
1831 -- Analyze_Component_Declaration --
1832 -----------------------------------
1834 procedure Analyze_Component_Declaration (N : Node_Id) is
1835 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1836 Id : constant Entity_Id := Defining_Identifier (N);
1837 E : constant Node_Id := Expression (N);
1838 Typ : constant Node_Id :=
1839 Subtype_Indication (Component_Definition (N));
1843 function Contains_POC (Constr : Node_Id) return Boolean;
1844 -- Determines whether a constraint uses the discriminant of a record
1845 -- type thus becoming a per-object constraint (POC).
1847 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1848 -- Typ is the type of the current component, check whether this type is
1849 -- a limited type. Used to validate declaration against that of
1850 -- enclosing record.
1856 function Contains_POC (Constr : Node_Id) return Boolean is
1858 -- Prevent cascaded errors
1860 if Error_Posted (Constr) then
1864 case Nkind (Constr) is
1865 when N_Attribute_Reference =>
1866 return Attribute_Name (Constr) = Name_Access
1867 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1869 when N_Discriminant_Association =>
1870 return Denotes_Discriminant (Expression (Constr));
1872 when N_Identifier =>
1873 return Denotes_Discriminant (Constr);
1875 when N_Index_Or_Discriminant_Constraint =>
1880 IDC := First (Constraints (Constr));
1881 while Present (IDC) loop
1883 -- One per-object constraint is sufficient
1885 if Contains_POC (IDC) then
1896 return Denotes_Discriminant (Low_Bound (Constr))
1898 Denotes_Discriminant (High_Bound (Constr));
1900 when N_Range_Constraint =>
1901 return Denotes_Discriminant (Range_Expression (Constr));
1908 ----------------------
1909 -- Is_Known_Limited --
1910 ----------------------
1912 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1913 P : constant Entity_Id := Etype (Typ);
1914 R : constant Entity_Id := Root_Type (Typ);
1917 if Is_Limited_Record (Typ) then
1920 -- If the root type is limited (and not a limited interface)
1921 -- so is the current type
1923 elsif Is_Limited_Record (R)
1924 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1928 -- Else the type may have a limited interface progenitor, but a
1929 -- limited record parent.
1931 elsif R /= P and then Is_Limited_Record (P) then
1937 end Is_Known_Limited;
1939 -- Start of processing for Analyze_Component_Declaration
1942 Generate_Definition (Id);
1945 if Present (Typ) then
1946 T := Find_Type_Of_Object
1947 (Subtype_Indication (Component_Definition (N)), N);
1949 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1950 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1953 -- Ada 2005 (AI-230): Access Definition case
1956 pragma Assert (Present
1957 (Access_Definition (Component_Definition (N))));
1959 T := Access_Definition
1961 N => Access_Definition (Component_Definition (N)));
1962 Set_Is_Local_Anonymous_Access (T);
1964 -- Ada 2005 (AI-254)
1966 if Present (Access_To_Subprogram_Definition
1967 (Access_Definition (Component_Definition (N))))
1968 and then Protected_Present (Access_To_Subprogram_Definition
1970 (Component_Definition (N))))
1972 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1976 -- If the subtype is a constrained subtype of the enclosing record,
1977 -- (which must have a partial view) the back-end does not properly
1978 -- handle the recursion. Rewrite the component declaration with an
1979 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1980 -- the tree directly because side effects have already been removed from
1981 -- discriminant constraints.
1983 if Ekind (T) = E_Access_Subtype
1984 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1985 and then Comes_From_Source (T)
1986 and then Nkind (Parent (T)) = N_Subtype_Declaration
1987 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1990 (Subtype_Indication (Component_Definition (N)),
1991 New_Copy_Tree (Subtype_Indication (Parent (T))));
1992 T := Find_Type_Of_Object
1993 (Subtype_Indication (Component_Definition (N)), N);
1996 -- If the component declaration includes a default expression, then we
1997 -- check that the component is not of a limited type (RM 3.7(5)),
1998 -- and do the special preanalysis of the expression (see section on
1999 -- "Handling of Default and Per-Object Expressions" in the spec of
2003 Check_SPARK_05_Restriction ("default expression is not allowed", E);
2004 Preanalyze_Default_Expression (E, T);
2005 Check_Initialization (T, E);
2007 if Ada_Version >= Ada_2005
2008 and then Ekind (T) = E_Anonymous_Access_Type
2009 and then Etype (E) /= Any_Type
2011 -- Check RM 3.9.2(9): "if the expected type for an expression is
2012 -- an anonymous access-to-specific tagged type, then the object
2013 -- designated by the expression shall not be dynamically tagged
2014 -- unless it is a controlling operand in a call on a dispatching
2017 if Is_Tagged_Type (Directly_Designated_Type (T))
2019 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2021 Ekind (Directly_Designated_Type (Etype (E))) =
2025 ("access to specific tagged type required (RM 3.9.2(9))", E);
2028 -- (Ada 2005: AI-230): Accessibility check for anonymous
2031 if Type_Access_Level (Etype (E)) >
2032 Deepest_Type_Access_Level (T)
2035 ("expression has deeper access level than component " &
2036 "(RM 3.10.2 (12.2))", E);
2039 -- The initialization expression is a reference to an access
2040 -- discriminant. The type of the discriminant is always deeper
2041 -- than any access type.
2043 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2044 and then Is_Entity_Name (E)
2045 and then Ekind (Entity (E)) = E_In_Parameter
2046 and then Present (Discriminal_Link (Entity (E)))
2049 ("discriminant has deeper accessibility level than target",
2055 -- The parent type may be a private view with unknown discriminants,
2056 -- and thus unconstrained. Regular components must be constrained.
2058 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2059 if Is_Class_Wide_Type (T) then
2061 ("class-wide subtype with unknown discriminants" &
2062 " in component declaration",
2063 Subtype_Indication (Component_Definition (N)));
2066 ("unconstrained subtype in component declaration",
2067 Subtype_Indication (Component_Definition (N)));
2070 -- Components cannot be abstract, except for the special case of
2071 -- the _Parent field (case of extending an abstract tagged type)
2073 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2074 Error_Msg_N ("type of a component cannot be abstract", N);
2078 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2080 -- The component declaration may have a per-object constraint, set
2081 -- the appropriate flag in the defining identifier of the subtype.
2083 if Present (Subtype_Indication (Component_Definition (N))) then
2085 Sindic : constant Node_Id :=
2086 Subtype_Indication (Component_Definition (N));
2088 if Nkind (Sindic) = N_Subtype_Indication
2089 and then Present (Constraint (Sindic))
2090 and then Contains_POC (Constraint (Sindic))
2092 Set_Has_Per_Object_Constraint (Id);
2097 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2098 -- out some static checks.
2100 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2101 Null_Exclusion_Static_Checks (N);
2104 -- If this component is private (or depends on a private type), flag the
2105 -- record type to indicate that some operations are not available.
2107 P := Private_Component (T);
2111 -- Check for circular definitions
2113 if P = Any_Type then
2114 Set_Etype (Id, Any_Type);
2116 -- There is a gap in the visibility of operations only if the
2117 -- component type is not defined in the scope of the record type.
2119 elsif Scope (P) = Scope (Current_Scope) then
2122 elsif Is_Limited_Type (P) then
2123 Set_Is_Limited_Composite (Current_Scope);
2126 Set_Is_Private_Composite (Current_Scope);
2131 and then Is_Limited_Type (T)
2132 and then Chars (Id) /= Name_uParent
2133 and then Is_Tagged_Type (Current_Scope)
2135 if Is_Derived_Type (Current_Scope)
2136 and then not Is_Known_Limited (Current_Scope)
2139 ("extension of nonlimited type cannot have limited components",
2142 if Is_Interface (Root_Type (Current_Scope)) then
2144 ("\limitedness is not inherited from limited interface", N);
2145 Error_Msg_N ("\add LIMITED to type indication", N);
2148 Explain_Limited_Type (T, N);
2149 Set_Etype (Id, Any_Type);
2150 Set_Is_Limited_Composite (Current_Scope, False);
2152 elsif not Is_Derived_Type (Current_Scope)
2153 and then not Is_Limited_Record (Current_Scope)
2154 and then not Is_Concurrent_Type (Current_Scope)
2157 ("nonlimited tagged type cannot have limited components", N);
2158 Explain_Limited_Type (T, N);
2159 Set_Etype (Id, Any_Type);
2160 Set_Is_Limited_Composite (Current_Scope, False);
2164 -- If the component is an unconstrained task or protected type with
2165 -- discriminants, the component and the enclosing record are limited
2166 -- and the component is constrained by its default values. Compute
2167 -- its actual subtype, else it may be allocated the maximum size by
2168 -- the backend, and possibly overflow.
2170 if Is_Concurrent_Type (T)
2171 and then not Is_Constrained (T)
2172 and then Has_Discriminants (T)
2173 and then not Has_Discriminants (Current_Scope)
2176 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2179 Set_Etype (Id, Act_T);
2181 -- Rewrite component definition to use the constrained subtype
2183 Rewrite (Component_Definition (N),
2184 Make_Component_Definition (Loc,
2185 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2189 Set_Original_Record_Component (Id, Id);
2191 if Has_Aspects (N) then
2192 Analyze_Aspect_Specifications (N, Id);
2195 Analyze_Dimension (N);
2196 end Analyze_Component_Declaration;
2198 --------------------------
2199 -- Analyze_Declarations --
2200 --------------------------
2202 procedure Analyze_Declarations (L : List_Id) is
2205 procedure Adjust_Decl;
2206 -- Adjust Decl not to include implicit label declarations, since these
2207 -- have strange Sloc values that result in elaboration check problems.
2208 -- (They have the sloc of the label as found in the source, and that
2209 -- is ahead of the current declarative part).
2211 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2212 -- Create the subprogram bodies which verify the run-time semantics of
2213 -- the pragmas listed below for each elibigle type found in declarative
2214 -- list Decls. The pragmas are:
2216 -- Default_Initial_Condition
2220 -- Context denotes the owner of the declarative list.
2222 procedure Check_Entry_Contracts;
2223 -- Perform a pre-analysis of the pre- and postconditions of an entry
2224 -- declaration. This must be done before full resolution and creation
2225 -- of the parameter block, etc. to catch illegal uses within the
2226 -- contract expression. Full analysis of the expression is done when
2227 -- the contract is processed.
2229 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2230 -- Check if a nested package has entities within it that rely on library
2231 -- level private types where the full view has not been completed for
2232 -- the purposes of checking if it is acceptable to freeze an expression
2233 -- function at the point of declaration.
2235 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2236 -- Determine whether Body_Decl denotes the body of a late controlled
2237 -- primitive (either Initialize, Adjust or Finalize). If this is the
2238 -- case, add a proper spec if the body lacks one. The spec is inserted
2239 -- before Body_Decl and immediately analyzed.
2241 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2242 -- Spec_Id is the entity of a package that may define abstract states,
2243 -- and in the case of a child unit, whose ancestors may define abstract
2244 -- states. If the states have partial visible refinement, remove the
2245 -- partial visibility of each constituent at the end of the package
2246 -- spec and body declarations.
2248 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2249 -- Spec_Id is the entity of a package that may define abstract states.
2250 -- If the states have visible refinement, remove the visibility of each
2251 -- constituent at the end of the package body declaration.
2253 procedure Resolve_Aspects;
2254 -- Utility to resolve the expressions of aspects at the end of a list of
2255 -- declarations, or before a declaration that freezes previous entities,
2256 -- such as in a subprogram body.
2262 procedure Adjust_Decl is
2264 while Present (Prev (Decl))
2265 and then Nkind (Decl) = N_Implicit_Label_Declaration
2271 ----------------------------
2272 -- Build_Assertion_Bodies --
2273 ----------------------------
2275 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2276 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2277 -- Create the subprogram bodies which verify the run-time semantics
2278 -- of the pragmas listed below for type Typ. The pragmas are:
2280 -- Default_Initial_Condition
2284 -------------------------------------
2285 -- Build_Assertion_Bodies_For_Type --
2286 -------------------------------------
2288 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2290 -- Preanalyze and resolve the Default_Initial_Condition assertion
2291 -- expression at the end of the declarations to catch any errors.
2293 if Has_DIC (Typ) then
2294 Build_DIC_Procedure_Body (Typ);
2297 if Nkind (Context) = N_Package_Specification then
2299 -- Preanalyze and resolve the class-wide invariants of an
2300 -- interface at the end of whichever declarative part has the
2301 -- interface type. Note that an interface may be declared in
2302 -- any non-package declarative part, but reaching the end of
2303 -- such a declarative part will always freeze the type and
2304 -- generate the invariant procedure (see Freeze_Type).
2306 if Is_Interface (Typ) then
2308 -- Interfaces are treated as the partial view of a private
2309 -- type, in order to achieve uniformity with the general
2310 -- case. As a result, an interface receives only a "partial"
2311 -- invariant procedure, which is never called.
2313 if Has_Own_Invariants (Typ) then
2314 Build_Invariant_Procedure_Body
2316 Partial_Invariant => True);
2319 -- Preanalyze and resolve the invariants of a private type
2320 -- at the end of the visible declarations to catch potential
2321 -- errors. Inherited class-wide invariants are not included
2322 -- because they have already been resolved.
2324 elsif Decls = Visible_Declarations (Context)
2325 and then Ekind_In (Typ, E_Limited_Private_Type,
2327 E_Record_Type_With_Private)
2328 and then Has_Own_Invariants (Typ)
2330 Build_Invariant_Procedure_Body
2332 Partial_Invariant => True);
2334 -- Preanalyze and resolve the invariants of a private type's
2335 -- full view at the end of the private declarations to catch
2336 -- potential errors.
2338 elsif Decls = Private_Declarations (Context)
2339 and then not Is_Private_Type (Typ)
2340 and then Has_Private_Declaration (Typ)
2341 and then Has_Invariants (Typ)
2343 Build_Invariant_Procedure_Body (Typ);
2346 end Build_Assertion_Bodies_For_Type;
2351 Decl_Id : Entity_Id;
2353 -- Start of processing for Build_Assertion_Bodies
2356 Decl := First (Decls);
2357 while Present (Decl) loop
2358 if Is_Declaration (Decl) then
2359 Decl_Id := Defining_Entity (Decl);
2361 if Is_Type (Decl_Id) then
2362 Build_Assertion_Bodies_For_Type (Decl_Id);
2368 end Build_Assertion_Bodies;
2370 ---------------------------
2371 -- Check_Entry_Contracts --
2372 ---------------------------
2374 procedure Check_Entry_Contracts is
2380 Ent := First_Entity (Current_Scope);
2381 while Present (Ent) loop
2383 -- This only concerns entries with pre/postconditions
2385 if Ekind (Ent) = E_Entry
2386 and then Present (Contract (Ent))
2387 and then Present (Pre_Post_Conditions (Contract (Ent)))
2389 ASN := Pre_Post_Conditions (Contract (Ent));
2391 Install_Formals (Ent);
2393 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2394 -- is performed on a copy of the pragma expression, to prevent
2395 -- modifying the original expression.
2397 while Present (ASN) loop
2398 if Nkind (ASN) = N_Pragma then
2402 (First (Pragma_Argument_Associations (ASN))));
2403 Set_Parent (Exp, ASN);
2405 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2408 ASN := Next_Pragma (ASN);
2416 end Check_Entry_Contracts;
2418 ----------------------------------
2419 -- Contains_Lib_Incomplete_Type --
2420 ----------------------------------
2422 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2426 -- Avoid looking through scopes that do not meet the precondition of
2427 -- Pkg not being within a library unit spec.
2429 if not Is_Compilation_Unit (Pkg)
2430 and then not Is_Generic_Instance (Pkg)
2431 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2433 -- Loop through all entities in the current scope to identify
2434 -- an entity that depends on a private type.
2436 Curr := First_Entity (Pkg);
2438 if Nkind (Curr) in N_Entity
2439 and then Depends_On_Private (Curr)
2444 exit when Last_Entity (Current_Scope) = Curr;
2445 Curr := Next_Entity (Curr);
2450 end Contains_Lib_Incomplete_Type;
2452 --------------------------------------
2453 -- Handle_Late_Controlled_Primitive --
2454 --------------------------------------
2456 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2457 Body_Spec : constant Node_Id := Specification (Body_Decl);
2458 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2459 Loc : constant Source_Ptr := Sloc (Body_Id);
2460 Params : constant List_Id :=
2461 Parameter_Specifications (Body_Spec);
2463 Spec_Id : Entity_Id;
2467 -- Consider only procedure bodies whose name matches one of the three
2468 -- controlled primitives.
2470 if Nkind (Body_Spec) /= N_Procedure_Specification
2471 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2477 -- A controlled primitive must have exactly one formal which is not
2478 -- an anonymous access type.
2480 elsif List_Length (Params) /= 1 then
2484 Typ := Parameter_Type (First (Params));
2486 if Nkind (Typ) = N_Access_Definition then
2492 -- The type of the formal must be derived from [Limited_]Controlled
2494 if not Is_Controlled (Entity (Typ)) then
2498 -- Check whether a specification exists for this body. We do not
2499 -- analyze the spec of the body in full, because it will be analyzed
2500 -- again when the body is properly analyzed, and we cannot create
2501 -- duplicate entries in the formals chain. We look for an explicit
2502 -- specification because the body may be an overriding operation and
2503 -- an inherited spec may be present.
2505 Spec_Id := Current_Entity (Body_Id);
2507 while Present (Spec_Id) loop
2508 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2509 and then Scope (Spec_Id) = Current_Scope
2510 and then Present (First_Formal (Spec_Id))
2511 and then No (Next_Formal (First_Formal (Spec_Id)))
2512 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2513 and then Comes_From_Source (Spec_Id)
2518 Spec_Id := Homonym (Spec_Id);
2521 -- At this point the body is known to be a late controlled primitive.
2522 -- Generate a matching spec and insert it before the body. Note the
2523 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2524 -- tree in this case.
2526 Spec := Copy_Separate_Tree (Body_Spec);
2528 -- Ensure that the subprogram declaration does not inherit the null
2529 -- indicator from the body as we now have a proper spec/body pair.
2531 Set_Null_Present (Spec, False);
2533 -- Ensure that the freeze node is inserted after the declaration of
2534 -- the primitive since its expansion will freeze the primitive.
2536 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2538 Insert_Before_And_Analyze (Body_Decl, Decl);
2539 end Handle_Late_Controlled_Primitive;
2541 ----------------------------------------
2542 -- Remove_Partial_Visible_Refinements --
2543 ----------------------------------------
2545 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2546 State_Elmt : Elmt_Id;
2548 if Present (Abstract_States (Spec_Id)) then
2549 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2550 while Present (State_Elmt) loop
2551 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2552 Next_Elmt (State_Elmt);
2556 -- For a child unit, also hide the partial state refinement from
2557 -- ancestor packages.
2559 if Is_Child_Unit (Spec_Id) then
2560 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2562 end Remove_Partial_Visible_Refinements;
2564 --------------------------------
2565 -- Remove_Visible_Refinements --
2566 --------------------------------
2568 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2569 State_Elmt : Elmt_Id;
2571 if Present (Abstract_States (Spec_Id)) then
2572 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2573 while Present (State_Elmt) loop
2574 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2575 Next_Elmt (State_Elmt);
2578 end Remove_Visible_Refinements;
2580 ---------------------
2581 -- Resolve_Aspects --
2582 ---------------------
2584 procedure Resolve_Aspects is
2588 E := First_Entity (Current_Scope);
2589 while Present (E) loop
2590 Resolve_Aspect_Expressions (E);
2593 end Resolve_Aspects;
2597 Context : Node_Id := Empty;
2598 Freeze_From : Entity_Id := Empty;
2599 Next_Decl : Node_Id;
2601 Body_Seen : Boolean := False;
2602 -- Flag set when the first body [stub] is encountered
2604 -- Start of processing for Analyze_Declarations
2607 if Restriction_Check_Required (SPARK_05) then
2608 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2612 while Present (Decl) loop
2614 -- Package spec cannot contain a package declaration in SPARK
2616 if Nkind (Decl) = N_Package_Declaration
2617 and then Nkind (Parent (L)) = N_Package_Specification
2619 Check_SPARK_05_Restriction
2620 ("package specification cannot contain a package declaration",
2624 -- Complete analysis of declaration
2627 Next_Decl := Next (Decl);
2629 if No (Freeze_From) then
2630 Freeze_From := First_Entity (Current_Scope);
2633 -- At the end of a declarative part, freeze remaining entities
2634 -- declared in it. The end of the visible declarations of package
2635 -- specification is not the end of a declarative part if private
2636 -- declarations are present. The end of a package declaration is a
2637 -- freezing point only if it a library package. A task definition or
2638 -- protected type definition is not a freeze point either. Finally,
2639 -- we do not freeze entities in generic scopes, because there is no
2640 -- code generated for them and freeze nodes will be generated for
2643 -- The end of a package instantiation is not a freeze point, but
2644 -- for now we make it one, because the generic body is inserted
2645 -- (currently) immediately after. Generic instantiations will not
2646 -- be a freeze point once delayed freezing of bodies is implemented.
2647 -- (This is needed in any case for early instantiations ???).
2649 if No (Next_Decl) then
2650 if Nkind (Parent (L)) = N_Component_List then
2653 elsif Nkind_In (Parent (L), N_Protected_Definition,
2656 Check_Entry_Contracts;
2658 elsif Nkind (Parent (L)) /= N_Package_Specification then
2659 if Nkind (Parent (L)) = N_Package_Body then
2660 Freeze_From := First_Entity (Current_Scope);
2663 -- There may have been several freezing points previously,
2664 -- for example object declarations or subprogram bodies, but
2665 -- at the end of a declarative part we check freezing from
2666 -- the beginning, even though entities may already be frozen,
2667 -- in order to perform visibility checks on delayed aspects.
2671 -- If the current scope is a generic subprogram body. Skip the
2672 -- generic formal parameters that are not frozen here.
2674 if Is_Subprogram (Current_Scope)
2675 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2676 N_Generic_Subprogram_Declaration
2677 and then Present (First_Entity (Current_Scope))
2679 while Is_Generic_Formal (Freeze_From) loop
2680 Freeze_From := Next_Entity (Freeze_From);
2683 Freeze_All (Freeze_From, Decl);
2684 Freeze_From := Last_Entity (Current_Scope);
2687 -- For declarations in a subprogram body there is no issue
2688 -- with name resolution in aspect specifications, but in
2689 -- ASIS mode we need to preanalyze aspect specifications
2690 -- that may otherwise only be analyzed during expansion
2691 -- (e.g. during generation of a related subprogram).
2697 Freeze_All (First_Entity (Current_Scope), Decl);
2698 Freeze_From := Last_Entity (Current_Scope);
2701 -- Current scope is a package specification
2703 elsif Scope (Current_Scope) /= Standard_Standard
2704 and then not Is_Child_Unit (Current_Scope)
2705 and then No (Generic_Parent (Parent (L)))
2707 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2708 -- resolved at the end of the immediately enclosing declaration
2709 -- list (AI05-0183-1).
2713 elsif L /= Visible_Declarations (Parent (L))
2714 or else No (Private_Declarations (Parent (L)))
2715 or else Is_Empty_List (Private_Declarations (Parent (L)))
2719 -- End of a package declaration
2721 -- In compilation mode the expansion of freeze node takes care
2722 -- of resolving expressions of all aspects in the list. In ASIS
2723 -- mode this must be done explicitly.
2726 and then Scope (Current_Scope) = Standard_Standard
2731 -- This is a freeze point because it is the end of a
2732 -- compilation unit.
2734 Freeze_All (First_Entity (Current_Scope), Decl);
2735 Freeze_From := Last_Entity (Current_Scope);
2737 -- At the end of the visible declarations the expressions in
2738 -- aspects of all entities declared so far must be resolved.
2739 -- The entities themselves might be frozen later, and the
2740 -- generated pragmas and attribute definition clauses analyzed
2741 -- in full at that point, but name resolution must take place
2743 -- In addition to being the proper semantics, this is mandatory
2744 -- within generic units, because global name capture requires
2745 -- those expressions to be analyzed, given that the generated
2746 -- pragmas do not appear in the original generic tree.
2748 elsif Serious_Errors_Detected = 0 then
2752 -- If next node is a body then freeze all types before the body.
2753 -- An exception occurs for some expander-generated bodies. If these
2754 -- are generated at places where in general language rules would not
2755 -- allow a freeze point, then we assume that the expander has
2756 -- explicitly checked that all required types are properly frozen,
2757 -- and we do not cause general freezing here. This special circuit
2758 -- is used when the encountered body is marked as having already
2761 -- In all other cases (bodies that come from source, and expander
2762 -- generated bodies that have not been analyzed yet), freeze all
2763 -- types now. Note that in the latter case, the expander must take
2764 -- care to attach the bodies at a proper place in the tree so as to
2765 -- not cause unwanted freezing at that point.
2767 -- It is also necessary to check for a case where both an expression
2768 -- function is used and the current scope depends on an incomplete
2769 -- private type from a library unit, otherwise premature freezing of
2770 -- the private type will occur.
2772 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2773 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2774 or else not Was_Expression_Function (Next_Decl))
2775 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2776 and then not Contains_Lib_Incomplete_Type
2779 -- When a controlled type is frozen, the expander generates stream
2780 -- and controlled-type support routines. If the freeze is caused
2781 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2782 -- expander will end up using the wrong version of these routines,
2783 -- as the body has not been processed yet. To remedy this, detect
2784 -- a late controlled primitive and create a proper spec for it.
2785 -- This ensures that the primitive will override its inherited
2786 -- counterpart before the freeze takes place.
2788 -- If the declaration we just processed is a body, do not attempt
2789 -- to examine Next_Decl as the late primitive idiom can only apply
2790 -- to the first encountered body.
2792 -- The spec of the late primitive is not generated in ASIS mode to
2793 -- ensure a consistent list of primitives that indicates the true
2794 -- semantic structure of the program (which is not relevant when
2795 -- generating executable code).
2797 -- ??? A cleaner approach may be possible and/or this solution
2798 -- could be extended to general-purpose late primitives, TBD.
2801 and then not Body_Seen
2802 and then not Is_Body (Decl)
2806 if Nkind (Next_Decl) = N_Subprogram_Body then
2807 Handle_Late_Controlled_Primitive (Next_Decl);
2811 -- In ASIS mode, if the next declaration is a body, complete
2812 -- the analysis of declarations so far.
2819 -- The generated body of an expression function does not freeze,
2820 -- unless it is a completion, in which case only the expression
2821 -- itself freezes. This is handled when the body itself is
2822 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2824 Freeze_All (Freeze_From, Decl);
2825 Freeze_From := Last_Entity (Current_Scope);
2831 -- Post-freezing actions
2834 Context := Parent (L);
2836 -- Certain contract annocations have forward visibility semantics and
2837 -- must be analyzed after all declarative items have been processed.
2838 -- This timing ensures that entities referenced by such contracts are
2841 -- Analyze the contract of an immediately enclosing package spec or
2842 -- body first because other contracts may depend on its information.
2844 if Nkind (Context) = N_Package_Body then
2845 Analyze_Package_Body_Contract (Defining_Entity (Context));
2847 elsif Nkind (Context) = N_Package_Specification then
2848 Analyze_Package_Contract (Defining_Entity (Context));
2851 -- Analyze the contracts of various constructs in the declarative
2854 Analyze_Contracts (L);
2856 if Nkind (Context) = N_Package_Body then
2858 -- Ensure that all abstract states and objects declared in the
2859 -- state space of a package body are utilized as constituents.
2861 Check_Unused_Body_States (Defining_Entity (Context));
2863 -- State refinements are visible up to the end of the package body
2864 -- declarations. Hide the state refinements from visibility to
2865 -- restore the original state conditions.
2867 Remove_Visible_Refinements (Corresponding_Spec (Context));
2868 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2870 elsif Nkind (Context) = N_Package_Specification then
2872 -- Partial state refinements are visible up to the end of the
2873 -- package spec declarations. Hide the partial state refinements
2874 -- from visibility to restore the original state conditions.
2876 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2879 -- Verify that all abstract states found in any package declared in
2880 -- the input declarative list have proper refinements. The check is
2881 -- performed only when the context denotes a block, entry, package,
2882 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2884 Check_State_Refinements (Context);
2886 -- Create the subprogram bodies which verify the run-time semantics
2887 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2888 -- types within the current declarative list. This ensures that all
2889 -- assertion expressions are preanalyzed and resolved at the end of
2890 -- the declarative part. Note that the resolution happens even when
2891 -- freezing does not take place.
2893 Build_Assertion_Bodies (L, Context);
2895 end Analyze_Declarations;
2897 -----------------------------------
2898 -- Analyze_Full_Type_Declaration --
2899 -----------------------------------
2901 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2902 Def : constant Node_Id := Type_Definition (N);
2903 Def_Id : constant Entity_Id := Defining_Identifier (N);
2907 Is_Remote : constant Boolean :=
2908 (Is_Remote_Types (Current_Scope)
2909 or else Is_Remote_Call_Interface (Current_Scope))
2910 and then not (In_Private_Part (Current_Scope)
2911 or else In_Package_Body (Current_Scope));
2913 procedure Check_Nonoverridable_Aspects;
2914 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2915 -- be overridden, and can only be confirmed on derivation.
2917 procedure Check_Ops_From_Incomplete_Type;
2918 -- If there is a tagged incomplete partial view of the type, traverse
2919 -- the primitives of the incomplete view and change the type of any
2920 -- controlling formals and result to indicate the full view. The
2921 -- primitives will be added to the full type's primitive operations
2922 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2923 -- is called from Process_Incomplete_Dependents).
2925 ----------------------------------
2926 -- Check_Nonoverridable_Aspects --
2927 ----------------------------------
2929 procedure Check_Nonoverridable_Aspects is
2930 function Get_Aspect_Spec
2932 Aspect_Name : Name_Id) return Node_Id;
2933 -- Check whether a list of aspect specifications includes an entry
2934 -- for a specific aspect. The list is either that of a partial or
2937 ---------------------
2938 -- Get_Aspect_Spec --
2939 ---------------------
2941 function Get_Aspect_Spec
2943 Aspect_Name : Name_Id) return Node_Id
2948 Spec := First (Specs);
2949 while Present (Spec) loop
2950 if Chars (Identifier (Spec)) = Aspect_Name then
2957 end Get_Aspect_Spec;
2961 Prev_Aspects : constant List_Id :=
2962 Aspect_Specifications (Parent (Def_Id));
2963 Par_Type : Entity_Id;
2964 Prev_Aspect : Node_Id;
2966 -- Start of processing for Check_Nonoverridable_Aspects
2969 -- Get parent type of derived type. Note that Prev is the entity in
2970 -- the partial declaration, but its contents are now those of full
2971 -- view, while Def_Id reflects the partial view.
2973 if Is_Private_Type (Def_Id) then
2974 Par_Type := Etype (Full_View (Def_Id));
2976 Par_Type := Etype (Def_Id);
2979 -- If there is an inherited Implicit_Dereference, verify that it is
2980 -- made explicit in the partial view.
2982 if Has_Discriminants (Base_Type (Par_Type))
2983 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2984 and then Present (Discriminant_Specifications (Parent (Prev)))
2985 and then Present (Get_Reference_Discriminant (Par_Type))
2988 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2992 (Discriminant_Specifications
2993 (Original_Node (Parent (Prev))))
2996 ("type does not inherit implicit dereference", Prev);
2999 -- If one of the views has the aspect specified, verify that it
3000 -- is consistent with that of the parent.
3003 Par_Discr : constant Entity_Id :=
3004 Get_Reference_Discriminant (Par_Type);
3005 Cur_Discr : constant Entity_Id :=
3006 Get_Reference_Discriminant (Prev);
3009 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3010 Error_Msg_N ("aspect incosistent with that of parent", N);
3013 -- Check that specification in partial view matches the
3014 -- inherited aspect. Compare names directly because aspect
3015 -- expression may not be analyzed.
3017 if Present (Prev_Aspect)
3018 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3019 and then Chars (Expression (Prev_Aspect)) /=
3023 ("aspect incosistent with that of parent", N);
3029 -- TBD : other nonoverridable aspects.
3030 end Check_Nonoverridable_Aspects;
3032 ------------------------------------
3033 -- Check_Ops_From_Incomplete_Type --
3034 ------------------------------------
3036 procedure Check_Ops_From_Incomplete_Type is
3043 and then Ekind (Prev) = E_Incomplete_Type
3044 and then Is_Tagged_Type (Prev)
3045 and then Is_Tagged_Type (T)
3047 Elmt := First_Elmt (Primitive_Operations (Prev));
3048 while Present (Elmt) loop
3051 Formal := First_Formal (Op);
3052 while Present (Formal) loop
3053 if Etype (Formal) = Prev then
3054 Set_Etype (Formal, T);
3057 Next_Formal (Formal);
3060 if Etype (Op) = Prev then
3067 end Check_Ops_From_Incomplete_Type;
3069 -- Start of processing for Analyze_Full_Type_Declaration
3072 Prev := Find_Type_Name (N);
3074 -- The full view, if present, now points to the current type. If there
3075 -- is an incomplete partial view, set a link to it, to simplify the
3076 -- retrieval of primitive operations of the type.
3078 -- Ada 2005 (AI-50217): If the type was previously decorated when
3079 -- imported through a LIMITED WITH clause, it appears as incomplete
3080 -- but has no full view.
3082 if Ekind (Prev) = E_Incomplete_Type
3083 and then Present (Full_View (Prev))
3085 T := Full_View (Prev);
3086 Set_Incomplete_View (N, Parent (Prev));
3091 Set_Is_Pure (T, Is_Pure (Current_Scope));
3093 -- We set the flag Is_First_Subtype here. It is needed to set the
3094 -- corresponding flag for the Implicit class-wide-type created
3095 -- during tagged types processing.
3097 Set_Is_First_Subtype (T, True);
3099 -- Only composite types other than array types are allowed to have
3104 -- For derived types, the rule will be checked once we've figured
3105 -- out the parent type.
3107 when N_Derived_Type_Definition =>
3110 -- For record types, discriminants are allowed, unless we are in
3113 when N_Record_Definition =>
3114 if Present (Discriminant_Specifications (N)) then
3115 Check_SPARK_05_Restriction
3116 ("discriminant type is not allowed",
3118 (First (Discriminant_Specifications (N))));
3122 if Present (Discriminant_Specifications (N)) then
3124 ("elementary or array type cannot have discriminants",
3126 (First (Discriminant_Specifications (N))));
3130 -- Elaborate the type definition according to kind, and generate
3131 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3132 -- already done (this happens during the reanalysis that follows a call
3133 -- to the high level optimizer).
3135 if not Analyzed (T) then
3138 -- Set the SPARK mode from the current context
3140 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3141 Set_SPARK_Pragma_Inherited (T);
3144 when N_Access_To_Subprogram_Definition =>
3145 Access_Subprogram_Declaration (T, Def);
3147 -- If this is a remote access to subprogram, we must create the
3148 -- equivalent fat pointer type, and related subprograms.
3151 Process_Remote_AST_Declaration (N);
3154 -- Validate categorization rule against access type declaration
3155 -- usually a violation in Pure unit, Shared_Passive unit.
3157 Validate_Access_Type_Declaration (T, N);
3159 when N_Access_To_Object_Definition =>
3160 Access_Type_Declaration (T, Def);
3162 -- Validate categorization rule against access type declaration
3163 -- usually a violation in Pure unit, Shared_Passive unit.
3165 Validate_Access_Type_Declaration (T, N);
3167 -- If we are in a Remote_Call_Interface package and define a
3168 -- RACW, then calling stubs and specific stream attributes
3172 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3174 Add_RACW_Features (Def_Id);
3177 when N_Array_Type_Definition =>
3178 Array_Type_Declaration (T, Def);
3180 when N_Derived_Type_Definition =>
3181 Derived_Type_Declaration (T, N, T /= Def_Id);
3183 -- Inherit predicates from parent, and protect against illegal
3186 if Is_Type (T) and then Has_Predicates (T) then
3187 Set_Has_Predicates (Def_Id);
3190 -- Save the scenario for examination by the ABE Processing
3193 Record_Elaboration_Scenario (N);
3195 when N_Enumeration_Type_Definition =>
3196 Enumeration_Type_Declaration (T, Def);
3198 when N_Floating_Point_Definition =>
3199 Floating_Point_Type_Declaration (T, Def);
3201 when N_Decimal_Fixed_Point_Definition =>
3202 Decimal_Fixed_Point_Type_Declaration (T, Def);
3204 when N_Ordinary_Fixed_Point_Definition =>
3205 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3207 when N_Signed_Integer_Type_Definition =>
3208 Signed_Integer_Type_Declaration (T, Def);
3210 when N_Modular_Type_Definition =>
3211 Modular_Type_Declaration (T, Def);
3213 when N_Record_Definition =>
3214 Record_Type_Declaration (T, N, Prev);
3216 -- If declaration has a parse error, nothing to elaborate.
3222 raise Program_Error;
3226 if Etype (T) = Any_Type then
3230 -- Controlled type is not allowed in SPARK
3232 if Is_Visibly_Controlled (T) then
3233 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3236 -- Some common processing for all types
3238 Set_Depends_On_Private (T, Has_Private_Component (T));
3239 Check_Ops_From_Incomplete_Type;
3241 -- Both the declared entity, and its anonymous base type if one was
3242 -- created, need freeze nodes allocated.
3245 B : constant Entity_Id := Base_Type (T);
3248 -- In the case where the base type differs from the first subtype, we
3249 -- pre-allocate a freeze node, and set the proper link to the first
3250 -- subtype. Freeze_Entity will use this preallocated freeze node when
3251 -- it freezes the entity.
3253 -- This does not apply if the base type is a generic type, whose
3254 -- declaration is independent of the current derived definition.
3256 if B /= T and then not Is_Generic_Type (B) then
3257 Ensure_Freeze_Node (B);
3258 Set_First_Subtype_Link (Freeze_Node (B), T);
3261 -- A type that is imported through a limited_with clause cannot
3262 -- generate any code, and thus need not be frozen. However, an access
3263 -- type with an imported designated type needs a finalization list,
3264 -- which may be referenced in some other package that has non-limited
3265 -- visibility on the designated type. Thus we must create the
3266 -- finalization list at the point the access type is frozen, to
3267 -- prevent unsatisfied references at link time.
3269 if not From_Limited_With (T) or else Is_Access_Type (T) then
3270 Set_Has_Delayed_Freeze (T);
3274 -- Case where T is the full declaration of some private type which has
3275 -- been swapped in Defining_Identifier (N).
3277 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3278 Process_Full_View (N, T, Def_Id);
3280 -- Record the reference. The form of this is a little strange, since
3281 -- the full declaration has been swapped in. So the first parameter
3282 -- here represents the entity to which a reference is made which is
3283 -- the "real" entity, i.e. the one swapped in, and the second
3284 -- parameter provides the reference location.
3286 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3287 -- since we don't want a complaint about the full type being an
3288 -- unwanted reference to the private type
3291 B : constant Boolean := Has_Pragma_Unreferenced (T);
3293 Set_Has_Pragma_Unreferenced (T, False);
3294 Generate_Reference (T, T, 'c');
3295 Set_Has_Pragma_Unreferenced (T, B);
3298 Set_Completion_Referenced (Def_Id);
3300 -- For completion of incomplete type, process incomplete dependents
3301 -- and always mark the full type as referenced (it is the incomplete
3302 -- type that we get for any real reference).
3304 elsif Ekind (Prev) = E_Incomplete_Type then
3305 Process_Incomplete_Dependents (N, T, Prev);
3306 Generate_Reference (Prev, Def_Id, 'c');
3307 Set_Completion_Referenced (Def_Id);
3309 -- If not private type or incomplete type completion, this is a real
3310 -- definition of a new entity, so record it.
3313 Generate_Definition (Def_Id);
3316 -- Propagate any pending access types whose finalization masters need to
3317 -- be fully initialized from the partial to the full view. Guard against
3318 -- an illegal full view that remains unanalyzed.
3320 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3321 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3324 if Chars (Scope (Def_Id)) = Name_System
3325 and then Chars (Def_Id) = Name_Address
3326 and then In_Predefined_Unit (N)
3328 Set_Is_Descendant_Of_Address (Def_Id);
3329 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3330 Set_Is_Descendant_Of_Address (Prev);
3333 Set_Optimize_Alignment_Flags (Def_Id);
3334 Check_Eliminated (Def_Id);
3336 -- If the declaration is a completion and aspects are present, apply
3337 -- them to the entity for the type which is currently the partial
3338 -- view, but which is the one that will be frozen.
3340 if Has_Aspects (N) then
3342 -- In most cases the partial view is a private type, and both views
3343 -- appear in different declarative parts. In the unusual case where
3344 -- the partial view is incomplete, perform the analysis on the
3345 -- full view, to prevent freezing anomalies with the corresponding
3346 -- class-wide type, which otherwise might be frozen before the
3347 -- dispatch table is built.
3350 and then Ekind (Prev) /= E_Incomplete_Type
3352 Analyze_Aspect_Specifications (N, Prev);
3357 Analyze_Aspect_Specifications (N, Def_Id);
3361 if Is_Derived_Type (Prev)
3362 and then Def_Id /= Prev
3364 Check_Nonoverridable_Aspects;
3366 end Analyze_Full_Type_Declaration;
3368 ----------------------------------
3369 -- Analyze_Incomplete_Type_Decl --
3370 ----------------------------------
3372 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3373 F : constant Boolean := Is_Pure (Current_Scope);
3377 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3379 Generate_Definition (Defining_Identifier (N));
3381 -- Process an incomplete declaration. The identifier must not have been
3382 -- declared already in the scope. However, an incomplete declaration may
3383 -- appear in the private part of a package, for a private type that has
3384 -- already been declared.
3386 -- In this case, the discriminants (if any) must match
3388 T := Find_Type_Name (N);
3390 Set_Ekind (T, E_Incomplete_Type);
3392 Set_Is_First_Subtype (T);
3393 Init_Size_Align (T);
3395 -- Set the SPARK mode from the current context
3397 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3398 Set_SPARK_Pragma_Inherited (T);
3400 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3401 -- incomplete types.
3403 if Tagged_Present (N) then
3404 Set_Is_Tagged_Type (T, True);
3405 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3406 Make_Class_Wide_Type (T);
3407 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3410 Set_Stored_Constraint (T, No_Elist);
3412 if Present (Discriminant_Specifications (N)) then
3414 Process_Discriminants (N);
3418 -- If the type has discriminants, nontrivial subtypes may be declared
3419 -- before the full view of the type. The full views of those subtypes
3420 -- will be built after the full view of the type.
3422 Set_Private_Dependents (T, New_Elmt_List);
3424 end Analyze_Incomplete_Type_Decl;
3426 -----------------------------------
3427 -- Analyze_Interface_Declaration --
3428 -----------------------------------
3430 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3431 CW : constant Entity_Id := Class_Wide_Type (T);
3434 Set_Is_Tagged_Type (T);
3435 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3437 Set_Is_Limited_Record (T, Limited_Present (Def)
3438 or else Task_Present (Def)
3439 or else Protected_Present (Def)
3440 or else Synchronized_Present (Def));
3442 -- Type is abstract if full declaration carries keyword, or if previous
3443 -- partial view did.
3445 Set_Is_Abstract_Type (T);
3446 Set_Is_Interface (T);
3448 -- Type is a limited interface if it includes the keyword limited, task,
3449 -- protected, or synchronized.
3451 Set_Is_Limited_Interface
3452 (T, Limited_Present (Def)
3453 or else Protected_Present (Def)
3454 or else Synchronized_Present (Def)
3455 or else Task_Present (Def));
3457 Set_Interfaces (T, New_Elmt_List);
3458 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3460 -- Complete the decoration of the class-wide entity if it was already
3461 -- built (i.e. during the creation of the limited view)
3463 if Present (CW) then
3464 Set_Is_Interface (CW);
3465 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3468 -- Check runtime support for synchronized interfaces
3470 if (Is_Task_Interface (T)
3471 or else Is_Protected_Interface (T)
3472 or else Is_Synchronized_Interface (T))
3473 and then not RTE_Available (RE_Select_Specific_Data)
3475 Error_Msg_CRT ("synchronized interfaces", T);
3477 end Analyze_Interface_Declaration;
3479 -----------------------------
3480 -- Analyze_Itype_Reference --
3481 -----------------------------
3483 -- Nothing to do. This node is placed in the tree only for the benefit of
3484 -- back end processing, and has no effect on the semantic processing.
3486 procedure Analyze_Itype_Reference (N : Node_Id) is
3488 pragma Assert (Is_Itype (Itype (N)));
3490 end Analyze_Itype_Reference;
3492 --------------------------------
3493 -- Analyze_Number_Declaration --
3494 --------------------------------
3496 procedure Analyze_Number_Declaration (N : Node_Id) is
3497 E : constant Node_Id := Expression (N);
3498 Id : constant Entity_Id := Defining_Identifier (N);
3499 Index : Interp_Index;
3504 Generate_Definition (Id);
3507 -- This is an optimization of a common case of an integer literal
3509 if Nkind (E) = N_Integer_Literal then
3510 Set_Is_Static_Expression (E, True);
3511 Set_Etype (E, Universal_Integer);
3513 Set_Etype (Id, Universal_Integer);
3514 Set_Ekind (Id, E_Named_Integer);
3515 Set_Is_Frozen (Id, True);
3519 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3521 -- Process expression, replacing error by integer zero, to avoid
3522 -- cascaded errors or aborts further along in the processing
3524 -- Replace Error by integer zero, which seems least likely to cause
3528 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3529 Set_Error_Posted (E);
3534 -- Verify that the expression is static and numeric. If
3535 -- the expression is overloaded, we apply the preference
3536 -- rule that favors root numeric types.
3538 if not Is_Overloaded (E) then
3540 if Has_Dynamic_Predicate_Aspect (T) then
3542 ("subtype has dynamic predicate, "
3543 & "not allowed in number declaration", N);
3549 Get_First_Interp (E, Index, It);
3550 while Present (It.Typ) loop
3551 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3552 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3554 if T = Any_Type then
3557 elsif It.Typ = Universal_Real
3559 It.Typ = Universal_Integer
3561 -- Choose universal interpretation over any other
3568 Get_Next_Interp (Index, It);
3572 if Is_Integer_Type (T) then
3574 Set_Etype (Id, Universal_Integer);
3575 Set_Ekind (Id, E_Named_Integer);
3577 elsif Is_Real_Type (T) then
3579 -- Because the real value is converted to universal_real, this is a
3580 -- legal context for a universal fixed expression.
3582 if T = Universal_Fixed then
3584 Loc : constant Source_Ptr := Sloc (N);
3585 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3587 New_Occurrence_Of (Universal_Real, Loc),
3588 Expression => Relocate_Node (E));
3595 elsif T = Any_Fixed then
3596 Error_Msg_N ("illegal context for mixed mode operation", E);
3598 -- Expression is of the form : universal_fixed * integer. Try to
3599 -- resolve as universal_real.
3601 T := Universal_Real;
3606 Set_Etype (Id, Universal_Real);
3607 Set_Ekind (Id, E_Named_Real);
3610 Wrong_Type (E, Any_Numeric);
3614 Set_Ekind (Id, E_Constant);
3615 Set_Never_Set_In_Source (Id, True);
3616 Set_Is_True_Constant (Id, True);
3620 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3621 Set_Etype (E, Etype (Id));
3624 if not Is_OK_Static_Expression (E) then
3625 Flag_Non_Static_Expr
3626 ("non-static expression used in number declaration!", E);
3627 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3628 Set_Etype (E, Any_Type);
3631 Analyze_Dimension (N);
3632 end Analyze_Number_Declaration;
3634 --------------------------------
3635 -- Analyze_Object_Declaration --
3636 --------------------------------
3638 -- WARNING: This routine manages Ghost regions. Return statements must be
3639 -- replaced by gotos which jump to the end of the routine and restore the
3642 procedure Analyze_Object_Declaration (N : Node_Id) is
3643 Loc : constant Source_Ptr := Sloc (N);
3644 Id : constant Entity_Id := Defining_Identifier (N);
3648 E : Node_Id := Expression (N);
3649 -- E is set to Expression (N) throughout this routine. When Expression
3650 -- (N) is modified, E is changed accordingly.
3652 Prev_Entity : Entity_Id := Empty;
3654 procedure Check_Dynamic_Object (Typ : Entity_Id);
3655 -- A library-level object with non-static discriminant constraints may
3656 -- require dynamic allocation. The declaration is illegal if the
3657 -- profile includes the restriction No_Implicit_Heap_Allocations.
3659 procedure Check_For_Null_Excluding_Components
3660 (Obj_Typ : Entity_Id;
3661 Obj_Decl : Node_Id);
3662 -- Verify that each null-excluding component of object declaration
3663 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3664 -- a compile-time warning if this is not the case.
3666 function Count_Tasks (T : Entity_Id) return Uint;
3667 -- This function is called when a non-generic library level object of a
3668 -- task type is declared. Its function is to count the static number of
3669 -- tasks declared within the type (it is only called if Has_Task is set
3670 -- for T). As a side effect, if an array of tasks with non-static bounds
3671 -- or a variant record type is encountered, Check_Restriction is called
3672 -- indicating the count is unknown.
3674 function Delayed_Aspect_Present return Boolean;
3675 -- If the declaration has an expression that is an aggregate, and it
3676 -- has aspects that require delayed analysis, the resolution of the
3677 -- aggregate must be deferred to the freeze point of the object. This
3678 -- special processing was created for address clauses, but it must
3679 -- also apply to Alignment. This must be done before the aspect
3680 -- specifications are analyzed because we must handle the aggregate
3681 -- before the analysis of the object declaration is complete.
3683 -- Any other relevant delayed aspects on object declarations ???
3685 --------------------------
3686 -- Check_Dynamic_Object --
3687 --------------------------
3689 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3691 Obj_Type : Entity_Id;
3696 if Is_Private_Type (Obj_Type)
3697 and then Present (Full_View (Obj_Type))
3699 Obj_Type := Full_View (Obj_Type);
3702 if Known_Static_Esize (Obj_Type) then
3706 if Restriction_Active (No_Implicit_Heap_Allocations)
3707 and then Expander_Active
3708 and then Has_Discriminants (Obj_Type)
3710 Comp := First_Component (Obj_Type);
3711 while Present (Comp) loop
3712 if Known_Static_Esize (Etype (Comp))
3713 or else Size_Known_At_Compile_Time (Etype (Comp))
3717 elsif not Discriminated_Size (Comp)
3718 and then Comes_From_Source (Comp)
3721 ("component& of non-static size will violate restriction "
3722 & "No_Implicit_Heap_Allocation?", N, Comp);
3724 elsif Is_Record_Type (Etype (Comp)) then
3725 Check_Dynamic_Object (Etype (Comp));
3728 Next_Component (Comp);
3731 end Check_Dynamic_Object;
3733 -----------------------------------------
3734 -- Check_For_Null_Excluding_Components --
3735 -----------------------------------------
3737 procedure Check_For_Null_Excluding_Components
3738 (Obj_Typ : Entity_Id;
3741 procedure Check_Component
3742 (Comp_Typ : Entity_Id;
3743 Comp_Decl : Node_Id := Empty;
3744 Array_Comp : Boolean := False);
3745 -- Apply a compile-time null-exclusion check on a component denoted
3746 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3747 -- subcomponents (if any).
3749 ---------------------
3750 -- Check_Component --
3751 ---------------------
3753 procedure Check_Component
3754 (Comp_Typ : Entity_Id;
3755 Comp_Decl : Node_Id := Empty;
3756 Array_Comp : Boolean := False)
3762 -- Do not consider internally-generated components or those that
3763 -- are already initialized.
3765 if Present (Comp_Decl)
3766 and then (not Comes_From_Source (Comp_Decl)
3767 or else Present (Expression (Comp_Decl)))
3772 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3773 and then Present (Full_View (Comp_Typ))
3775 T := Full_View (Comp_Typ);
3780 -- Verify a component of a null-excluding access type
3782 if Is_Access_Type (T)
3783 and then Can_Never_Be_Null (T)
3785 if Comp_Decl = Obj_Decl then
3786 Null_Exclusion_Static_Checks
3789 Array_Comp => Array_Comp);
3792 Null_Exclusion_Static_Checks
3795 Array_Comp => Array_Comp);
3798 -- Check array components
3800 elsif Is_Array_Type (T) then
3802 -- There is no suitable component when the object is of an
3803 -- array type. However, a namable component may appear at some
3804 -- point during the recursive inspection, but not at the top
3805 -- level. At the top level just indicate array component case.
3807 if Comp_Decl = Obj_Decl then
3808 Check_Component (Component_Type (T), Array_Comp => True);
3810 Check_Component (Component_Type (T), Comp_Decl);
3813 -- Verify all components of type T
3815 -- Note: No checks are performed on types with discriminants due
3816 -- to complexities involving variants. ???
3818 elsif (Is_Concurrent_Type (T)
3819 or else Is_Incomplete_Or_Private_Type (T)
3820 or else Is_Record_Type (T))
3821 and then not Has_Discriminants (T)
3823 Comp := First_Component (T);
3824 while Present (Comp) loop
3825 Check_Component (Etype (Comp), Parent (Comp));
3827 Comp := Next_Component (Comp);
3830 end Check_Component;
3832 -- Start processing for Check_For_Null_Excluding_Components
3835 Check_Component (Obj_Typ, Obj_Decl);
3836 end Check_For_Null_Excluding_Components;
3842 function Count_Tasks (T : Entity_Id) return Uint is
3848 if Is_Task_Type (T) then
3851 elsif Is_Record_Type (T) then
3852 if Has_Discriminants (T) then
3853 Check_Restriction (Max_Tasks, N);
3858 C := First_Component (T);
3859 while Present (C) loop
3860 V := V + Count_Tasks (Etype (C));
3867 elsif Is_Array_Type (T) then
3868 X := First_Index (T);
3869 V := Count_Tasks (Component_Type (T));
3870 while Present (X) loop
3873 if not Is_OK_Static_Subtype (C) then
3874 Check_Restriction (Max_Tasks, N);
3877 V := V * (UI_Max (Uint_0,
3878 Expr_Value (Type_High_Bound (C)) -
3879 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3892 ----------------------------
3893 -- Delayed_Aspect_Present --
3894 ----------------------------
3896 function Delayed_Aspect_Present return Boolean is
3901 if Present (Aspect_Specifications (N)) then
3902 A := First (Aspect_Specifications (N));
3903 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3904 while Present (A) loop
3905 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3914 end Delayed_Aspect_Present;
3918 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3919 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3920 -- Save the Ghost-related attributes to restore on exit
3922 Related_Id : Entity_Id;
3924 -- Start of processing for Analyze_Object_Declaration
3927 -- There are three kinds of implicit types generated by an
3928 -- object declaration:
3930 -- 1. Those generated by the original Object Definition
3932 -- 2. Those generated by the Expression
3934 -- 3. Those used to constrain the Object Definition with the
3935 -- expression constraints when the definition is unconstrained.
3937 -- They must be generated in this order to avoid order of elaboration
3938 -- issues. Thus the first step (after entering the name) is to analyze
3939 -- the object definition.
3941 if Constant_Present (N) then
3942 Prev_Entity := Current_Entity_In_Scope (Id);
3944 if Present (Prev_Entity)
3946 -- If the homograph is an implicit subprogram, it is overridden
3947 -- by the current declaration.
3949 ((Is_Overloadable (Prev_Entity)
3950 and then Is_Inherited_Operation (Prev_Entity))
3952 -- The current object is a discriminal generated for an entry
3953 -- family index. Even though the index is a constant, in this
3954 -- particular context there is no true constant redeclaration.
3955 -- Enter_Name will handle the visibility.
3958 (Is_Discriminal (Id)
3959 and then Ekind (Discriminal_Link (Id)) =
3960 E_Entry_Index_Parameter)
3962 -- The current object is the renaming for a generic declared
3963 -- within the instance.
3966 (Ekind (Prev_Entity) = E_Package
3967 and then Nkind (Parent (Prev_Entity)) =
3968 N_Package_Renaming_Declaration
3969 and then not Comes_From_Source (Prev_Entity)
3971 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3973 -- The entity may be a homonym of a private component of the
3974 -- enclosing protected object, for which we create a local
3975 -- renaming declaration. The declaration is legal, even if
3976 -- useless when it just captures that component.
3979 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3980 and then Nkind (Parent (Prev_Entity)) =
3981 N_Object_Renaming_Declaration))
3983 Prev_Entity := Empty;
3987 if Present (Prev_Entity) then
3989 -- The object declaration is Ghost when it completes a deferred Ghost
3992 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
3994 Constant_Redeclaration (Id, N, T);
3996 Generate_Reference (Prev_Entity, Id, 'c');
3997 Set_Completion_Referenced (Id);
3999 if Error_Posted (N) then
4001 -- Type mismatch or illegal redeclaration; do not analyze
4002 -- expression to avoid cascaded errors.
4004 T := Find_Type_Of_Object (Object_Definition (N), N);
4006 Set_Ekind (Id, E_Variable);
4010 -- In the normal case, enter identifier at the start to catch premature
4011 -- usage in the initialization expression.
4014 Generate_Definition (Id);
4017 Mark_Coextensions (N, Object_Definition (N));
4019 T := Find_Type_Of_Object (Object_Definition (N), N);
4021 if Nkind (Object_Definition (N)) = N_Access_Definition
4023 (Access_To_Subprogram_Definition (Object_Definition (N)))
4024 and then Protected_Present
4025 (Access_To_Subprogram_Definition (Object_Definition (N)))
4027 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4030 if Error_Posted (Id) then
4032 Set_Ekind (Id, E_Variable);
4037 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4038 -- out some static checks.
4040 if Ada_Version >= Ada_2005 then
4042 -- In case of aggregates we must also take care of the correct
4043 -- initialization of nested aggregates bug this is done at the
4044 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4046 if Can_Never_Be_Null (T) then
4047 if Present (Expression (N))
4048 and then Nkind (Expression (N)) = N_Aggregate
4054 Save_Typ : constant Entity_Id := Etype (Id);
4056 Set_Etype (Id, T); -- Temp. decoration for static checks
4057 Null_Exclusion_Static_Checks (N);
4058 Set_Etype (Id, Save_Typ);
4062 -- We might be dealing with an object of a composite type containing
4063 -- null-excluding components without an aggregate, so we must verify
4064 -- that such components have default initialization.
4067 Check_For_Null_Excluding_Components (T, N);
4071 -- Object is marked pure if it is in a pure scope
4073 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4075 -- If deferred constant, make sure context is appropriate. We detect
4076 -- a deferred constant as a constant declaration with no expression.
4077 -- A deferred constant can appear in a package body if its completion
4078 -- is by means of an interface pragma.
4080 if Constant_Present (N) and then No (E) then
4082 -- A deferred constant may appear in the declarative part of the
4083 -- following constructs:
4087 -- extended return statements
4090 -- subprogram bodies
4093 -- When declared inside a package spec, a deferred constant must be
4094 -- completed by a full constant declaration or pragma Import. In all
4095 -- other cases, the only proper completion is pragma Import. Extended
4096 -- return statements are flagged as invalid contexts because they do
4097 -- not have a declarative part and so cannot accommodate the pragma.
4099 if Ekind (Current_Scope) = E_Return_Statement then
4101 ("invalid context for deferred constant declaration (RM 7.4)",
4104 ("\declaration requires an initialization expression",
4106 Set_Constant_Present (N, False);
4108 -- In Ada 83, deferred constant must be of private type
4110 elsif not Is_Private_Type (T) then
4111 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4113 ("(Ada 83) deferred constant must be private type", N);
4117 -- If not a deferred constant, then the object declaration freezes
4118 -- its type, unless the object is of an anonymous type and has delayed
4119 -- aspects. In that case the type is frozen when the object itself is.
4122 Check_Fully_Declared (T, N);
4124 if Has_Delayed_Aspects (Id)
4125 and then Is_Array_Type (T)
4126 and then Is_Itype (T)
4128 Set_Has_Delayed_Freeze (T);
4130 Freeze_Before (N, T);
4134 -- If the object was created by a constrained array definition, then
4135 -- set the link in both the anonymous base type and anonymous subtype
4136 -- that are built to represent the array type to point to the object.
4138 if Nkind (Object_Definition (Declaration_Node (Id))) =
4139 N_Constrained_Array_Definition
4141 Set_Related_Array_Object (T, Id);
4142 Set_Related_Array_Object (Base_Type (T), Id);
4145 -- Special checks for protected objects not at library level
4147 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4148 Check_Restriction (No_Local_Protected_Objects, Id);
4150 -- Protected objects with interrupt handlers must be at library level
4152 -- Ada 2005: This test is not needed (and the corresponding clause
4153 -- in the RM is removed) because accessibility checks are sufficient
4154 -- to make handlers not at the library level illegal.
4156 -- AI05-0303: The AI is in fact a binding interpretation, and thus
4157 -- applies to the '95 version of the language as well.
4159 if Is_Protected_Type (T)
4160 and then Has_Interrupt_Handler (T)
4161 and then Ada_Version < Ada_95
4164 ("interrupt object can only be declared at library level", Id);
4168 -- Check for violation of No_Local_Timing_Events
4170 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4171 Check_Restriction (No_Local_Timing_Events, Id);
4174 -- The actual subtype of the object is the nominal subtype, unless
4175 -- the nominal one is unconstrained and obtained from the expression.
4179 -- These checks should be performed before the initialization expression
4180 -- is considered, so that the Object_Definition node is still the same
4181 -- as in source code.
4183 -- In SPARK, the nominal subtype is always given by a subtype mark
4184 -- and must not be unconstrained. (The only exception to this is the
4185 -- acceptance of declarations of constants of type String.)
4187 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
4189 Check_SPARK_05_Restriction
4190 ("subtype mark required", Object_Definition (N));
4192 elsif Is_Array_Type (T)
4193 and then not Is_Constrained (T)
4194 and then T /= Standard_String
4196 Check_SPARK_05_Restriction
4197 ("subtype mark of constrained type expected",
4198 Object_Definition (N));
4201 if Is_Library_Level_Entity (Id) then
4202 Check_Dynamic_Object (T);
4205 -- There are no aliased objects in SPARK
4207 if Aliased_Present (N) then
4208 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
4211 -- Process initialization expression if present and not in error
4213 if Present (E) and then E /= Error then
4215 -- Generate an error in case of CPP class-wide object initialization.
4216 -- Required because otherwise the expansion of the class-wide
4217 -- assignment would try to use 'size to initialize the object
4218 -- (primitive that is not available in CPP tagged types).
4220 if Is_Class_Wide_Type (Act_T)
4222 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4224 (Present (Full_View (Root_Type (Etype (Act_T))))
4226 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4229 ("predefined assignment not available for 'C'P'P tagged types",
4233 Mark_Coextensions (N, E);
4236 -- In case of errors detected in the analysis of the expression,
4237 -- decorate it with the expected type to avoid cascaded errors
4239 if No (Etype (E)) then
4243 -- If an initialization expression is present, then we set the
4244 -- Is_True_Constant flag. It will be reset if this is a variable
4245 -- and it is indeed modified.
4247 Set_Is_True_Constant (Id, True);
4249 -- If we are analyzing a constant declaration, set its completion
4250 -- flag after analyzing and resolving the expression.
4252 if Constant_Present (N) then
4253 Set_Has_Completion (Id);
4256 -- Set type and resolve (type may be overridden later on). Note:
4257 -- Ekind (Id) must still be E_Void at this point so that incorrect
4258 -- early usage within E is properly diagnosed.
4262 -- If the expression is an aggregate we must look ahead to detect
4263 -- the possible presence of an address clause, and defer resolution
4264 -- and expansion of the aggregate to the freeze point of the entity.
4266 -- This is not always legal because the aggregate may contain other
4267 -- references that need freezing, e.g. references to other entities
4268 -- with address clauses. In any case, when compiling with -gnatI the
4269 -- presence of the address clause must be ignored.
4271 if Comes_From_Source (N)
4272 and then Expander_Active
4273 and then Nkind (E) = N_Aggregate
4275 ((Present (Following_Address_Clause (N))
4276 and then not Ignore_Rep_Clauses)
4277 or else Delayed_Aspect_Present)
4283 -- If the expression is a formal that is a "subprogram pointer"
4284 -- this is illegal in accessibility terms. Add an explicit
4285 -- conversion to force the corresponding check, as is done for
4288 if Comes_From_Source (N)
4289 and then Is_Entity_Name (E)
4290 and then Present (Entity (E))
4291 and then Is_Formal (Entity (E))
4293 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4294 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4296 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4302 -- No further action needed if E is a call to an inlined function
4303 -- which returns an unconstrained type and it has been expanded into
4304 -- a procedure call. In that case N has been replaced by an object
4305 -- declaration without initializing expression and it has been
4306 -- analyzed (see Expand_Inlined_Call).
4308 if Back_End_Inlining
4309 and then Expander_Active
4310 and then Nkind (E) = N_Function_Call
4311 and then Nkind (Name (E)) in N_Has_Entity
4312 and then Is_Inlined (Entity (Name (E)))
4313 and then not Is_Constrained (Etype (E))
4314 and then Analyzed (N)
4315 and then No (Expression (N))
4320 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4321 -- node (which was marked already-analyzed), we need to set the type
4322 -- to something other than Any_Access in order to keep gigi happy.
4324 if Etype (E) = Any_Access then
4328 -- If the object is an access to variable, the initialization
4329 -- expression cannot be an access to constant.
4331 if Is_Access_Type (T)
4332 and then not Is_Access_Constant (T)
4333 and then Is_Access_Type (Etype (E))
4334 and then Is_Access_Constant (Etype (E))
4337 ("access to variable cannot be initialized with an "
4338 & "access-to-constant expression", E);
4341 if not Assignment_OK (N) then
4342 Check_Initialization (T, E);
4345 Check_Unset_Reference (E);
4347 -- If this is a variable, then set current value. If this is a
4348 -- declared constant of a scalar type with a static expression,
4349 -- indicate that it is always valid.
4351 if not Constant_Present (N) then
4352 if Compile_Time_Known_Value (E) then
4353 Set_Current_Value (Id, E);
4356 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4357 Set_Is_Known_Valid (Id);
4360 -- Deal with setting of null flags
4362 if Is_Access_Type (T) then
4363 if Known_Non_Null (E) then
4364 Set_Is_Known_Non_Null (Id, True);
4365 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4366 Set_Is_Known_Null (Id, True);
4370 -- Check incorrect use of dynamically tagged expressions
4372 if Is_Tagged_Type (T) then
4373 Check_Dynamically_Tagged_Expression
4379 Apply_Scalar_Range_Check (E, T);
4380 Apply_Static_Length_Check (E, T);
4382 if Nkind (Original_Node (N)) = N_Object_Declaration
4383 and then Comes_From_Source (Original_Node (N))
4385 -- Only call test if needed
4387 and then Restriction_Check_Required (SPARK_05)
4388 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4390 Check_SPARK_05_Restriction
4391 ("initialization expression is not appropriate", E);
4394 -- A formal parameter of a specific tagged type whose related
4395 -- subprogram is subject to pragma Extensions_Visible with value
4396 -- "False" cannot be implicitly converted to a class-wide type by
4397 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4398 -- not consider internally generated expressions.
4400 if Is_Class_Wide_Type (T)
4401 and then Comes_From_Source (E)
4402 and then Is_EVF_Expression (E)
4405 ("formal parameter cannot be implicitly converted to "
4406 & "class-wide type when Extensions_Visible is False", E);
4410 -- If the No_Streams restriction is set, check that the type of the
4411 -- object is not, and does not contain, any subtype derived from
4412 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4413 -- Has_Stream just for efficiency reasons. There is no point in
4414 -- spending time on a Has_Stream check if the restriction is not set.
4416 if Restriction_Check_Required (No_Streams) then
4417 if Has_Stream (T) then
4418 Check_Restriction (No_Streams, N);
4422 -- Deal with predicate check before we start to do major rewriting. It
4423 -- is OK to initialize and then check the initialized value, since the
4424 -- object goes out of scope if we get a predicate failure. Note that we
4425 -- do this in the analyzer and not the expander because the analyzer
4426 -- does some substantial rewriting in some cases.
4428 -- We need a predicate check if the type has predicates that are not
4429 -- ignored, and if either there is an initializing expression, or for
4430 -- default initialization when we have at least one case of an explicit
4431 -- default initial value and then this is not an internal declaration
4432 -- whose initialization comes later (as for an aggregate expansion).
4434 if not Suppress_Assignment_Checks (N)
4435 and then Present (Predicate_Function (T))
4436 and then not Predicates_Ignored (T)
4437 and then not No_Initialization (N)
4441 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4443 -- If the type has a static predicate and the expression is known at
4444 -- compile time, see if the expression satisfies the predicate.
4447 Check_Expression_Against_Static_Predicate (E, T);
4450 -- If the type is a null record and there is no explicit initial
4451 -- expression, no predicate check applies.
4453 if No (E) and then Is_Null_Record_Type (T) then
4456 -- Do not generate a predicate check if the initialization expression
4457 -- is a type conversion because the conversion has been subjected to
4458 -- the same check. This is a small optimization which avoid redundant
4461 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4466 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
4470 -- Case of unconstrained type
4472 if not Is_Definite_Subtype (T) then
4474 -- In SPARK, a declaration of unconstrained type is allowed
4475 -- only for constants of type string.
4477 if Is_String_Type (T) and then not Constant_Present (N) then
4478 Check_SPARK_05_Restriction
4479 ("declaration of object of unconstrained type not allowed", N);
4482 -- Nothing to do in deferred constant case
4484 if Constant_Present (N) and then No (E) then
4487 -- Case of no initialization present
4490 if No_Initialization (N) then
4493 elsif Is_Class_Wide_Type (T) then
4495 ("initialization required in class-wide declaration ", N);
4499 ("unconstrained subtype not allowed (need initialization)",
4500 Object_Definition (N));
4502 if Is_Record_Type (T) and then Has_Discriminants (T) then
4504 ("\provide initial value or explicit discriminant values",
4505 Object_Definition (N));
4508 ("\or give default discriminant values for type&",
4509 Object_Definition (N), T);
4511 elsif Is_Array_Type (T) then
4513 ("\provide initial value or explicit array bounds",
4514 Object_Definition (N));
4518 -- Case of initialization present but in error. Set initial
4519 -- expression as absent (but do not make above complaints)
4521 elsif E = Error then
4522 Set_Expression (N, Empty);
4525 -- Case of initialization present
4528 -- Check restrictions in Ada 83
4530 if not Constant_Present (N) then
4532 -- Unconstrained variables not allowed in Ada 83 mode
4534 if Ada_Version = Ada_83
4535 and then Comes_From_Source (Object_Definition (N))
4538 ("(Ada 83) unconstrained variable not allowed",
4539 Object_Definition (N));
4543 -- Now we constrain the variable from the initializing expression
4545 -- If the expression is an aggregate, it has been expanded into
4546 -- individual assignments. Retrieve the actual type from the
4547 -- expanded construct.
4549 if Is_Array_Type (T)
4550 and then No_Initialization (N)
4551 and then Nkind (Original_Node (E)) = N_Aggregate
4555 -- In case of class-wide interface object declarations we delay
4556 -- the generation of the equivalent record type declarations until
4557 -- its expansion because there are cases in they are not required.
4559 elsif Is_Interface (T) then
4562 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4563 -- we should prevent the generation of another Itype with the
4564 -- same name as the one already generated, or we end up with
4565 -- two identical types in GNATprove.
4567 elsif GNATprove_Mode then
4570 -- If the type is an unchecked union, no subtype can be built from
4571 -- the expression. Rewrite declaration as a renaming, which the
4572 -- back-end can handle properly. This is a rather unusual case,
4573 -- because most unchecked_union declarations have default values
4574 -- for discriminants and are thus not indefinite.
4576 elsif Is_Unchecked_Union (T) then
4577 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4578 Set_Ekind (Id, E_Constant);
4580 Set_Ekind (Id, E_Variable);
4584 Make_Object_Renaming_Declaration (Loc,
4585 Defining_Identifier => Id,
4586 Subtype_Mark => New_Occurrence_Of (T, Loc),
4589 Set_Renamed_Object (Id, E);
4590 Freeze_Before (N, T);
4595 -- Ensure that the generated subtype has a unique external name
4596 -- when the related object is public. This guarantees that the
4597 -- subtype and its bounds will not be affected by switches or
4598 -- pragmas that may offset the internal counter due to extra
4601 if Is_Public (Id) then
4604 Related_Id := Empty;
4607 Expand_Subtype_From_Expr
4610 Subtype_Indic => Object_Definition (N),
4612 Related_Id => Related_Id);
4614 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4617 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4619 if Aliased_Present (N) then
4620 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4623 Freeze_Before (N, Act_T);
4624 Freeze_Before (N, T);
4627 elsif Is_Array_Type (T)
4628 and then No_Initialization (N)
4629 and then (Nkind (Original_Node (E)) = N_Aggregate
4630 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4631 and then Nkind (Original_Node (Expression
4632 (Original_Node (E)))) = N_Aggregate))
4634 if not Is_Entity_Name (Object_Definition (N)) then
4636 Check_Compile_Time_Size (Act_T);
4638 if Aliased_Present (N) then
4639 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4643 -- When the given object definition and the aggregate are specified
4644 -- independently, and their lengths might differ do a length check.
4645 -- This cannot happen if the aggregate is of the form (others =>...)
4647 if not Is_Constrained (T) then
4650 elsif Nkind (E) = N_Raise_Constraint_Error then
4652 -- Aggregate is statically illegal. Place back in declaration
4654 Set_Expression (N, E);
4655 Set_No_Initialization (N, False);
4657 elsif T = Etype (E) then
4660 elsif Nkind (E) = N_Aggregate
4661 and then Present (Component_Associations (E))
4662 and then Present (Choice_List (First (Component_Associations (E))))
4664 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4670 Apply_Length_Check (E, T);
4673 -- If the type is limited unconstrained with defaulted discriminants and
4674 -- there is no expression, then the object is constrained by the
4675 -- defaults, so it is worthwhile building the corresponding subtype.
4677 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4678 and then not Is_Constrained (T)
4679 and then Has_Discriminants (T)
4682 Act_T := Build_Default_Subtype (T, N);
4684 -- Ada 2005: A limited object may be initialized by means of an
4685 -- aggregate. If the type has default discriminants it has an
4686 -- unconstrained nominal type, Its actual subtype will be obtained
4687 -- from the aggregate, and not from the default discriminants.
4692 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4694 elsif Nkind (E) = N_Function_Call
4695 and then Constant_Present (N)
4696 and then Has_Unconstrained_Elements (Etype (E))
4698 -- The back-end has problems with constants of a discriminated type
4699 -- with defaults, if the initial value is a function call. We
4700 -- generate an intermediate temporary that will receive a reference
4701 -- to the result of the call. The initialization expression then
4702 -- becomes a dereference of that temporary.
4704 Remove_Side_Effects (E);
4706 -- If this is a constant declaration of an unconstrained type and
4707 -- the initialization is an aggregate, we can use the subtype of the
4708 -- aggregate for the declared entity because it is immutable.
4710 elsif not Is_Constrained (T)
4711 and then Has_Discriminants (T)
4712 and then Constant_Present (N)
4713 and then not Has_Unchecked_Union (T)
4714 and then Nkind (E) = N_Aggregate
4719 -- Check No_Wide_Characters restriction
4721 Check_Wide_Character_Restriction (T, Object_Definition (N));
4723 -- Indicate this is not set in source. Certainly true for constants, and
4724 -- true for variables so far (will be reset for a variable if and when
4725 -- we encounter a modification in the source).
4727 Set_Never_Set_In_Source (Id);
4729 -- Now establish the proper kind and type of the object
4731 if Constant_Present (N) then
4732 Set_Ekind (Id, E_Constant);
4733 Set_Is_True_Constant (Id);
4736 Set_Ekind (Id, E_Variable);
4738 -- A variable is set as shared passive if it appears in a shared
4739 -- passive package, and is at the outer level. This is not done for
4740 -- entities generated during expansion, because those are always
4741 -- manipulated locally.
4743 if Is_Shared_Passive (Current_Scope)
4744 and then Is_Library_Level_Entity (Id)
4745 and then Comes_From_Source (Id)
4747 Set_Is_Shared_Passive (Id);
4748 Check_Shared_Var (Id, T, N);
4751 -- Set Has_Initial_Value if initializing expression present. Note
4752 -- that if there is no initializing expression, we leave the state
4753 -- of this flag unchanged (usually it will be False, but notably in
4754 -- the case of exception choice variables, it will already be true).
4757 Set_Has_Initial_Value (Id);
4761 -- Set the SPARK mode from the current context (may be overwritten later
4762 -- with explicit pragma).
4764 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4765 Set_SPARK_Pragma_Inherited (Id);
4767 -- Preserve relevant elaboration-related attributes of the context which
4768 -- are no longer available or very expensive to recompute once analysis,
4769 -- resolution, and expansion are over.
4771 Mark_Elaboration_Attributes
4776 -- Initialize alignment and size and capture alignment setting
4778 Init_Alignment (Id);
4780 Set_Optimize_Alignment_Flags (Id);
4782 -- Deal with aliased case
4784 if Aliased_Present (N) then
4785 Set_Is_Aliased (Id);
4787 -- If the object is aliased and the type is unconstrained with
4788 -- defaulted discriminants and there is no expression, then the
4789 -- object is constrained by the defaults, so it is worthwhile
4790 -- building the corresponding subtype.
4792 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4793 -- unconstrained, then only establish an actual subtype if the
4794 -- nominal subtype is indefinite. In definite cases the object is
4795 -- unconstrained in Ada 2005.
4798 and then Is_Record_Type (T)
4799 and then not Is_Constrained (T)
4800 and then Has_Discriminants (T)
4801 and then (Ada_Version < Ada_2005
4802 or else not Is_Definite_Subtype (T))
4804 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4808 -- Now we can set the type of the object
4810 Set_Etype (Id, Act_T);
4812 -- Non-constant object is marked to be treated as volatile if type is
4813 -- volatile and we clear the Current_Value setting that may have been
4814 -- set above. Doing so for constants isn't required and might interfere
4815 -- with possible uses of the object as a static expression in contexts
4816 -- incompatible with volatility (e.g. as a case-statement alternative).
4818 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4819 Set_Treat_As_Volatile (Id);
4820 Set_Current_Value (Id, Empty);
4823 -- Deal with controlled types
4825 if Has_Controlled_Component (Etype (Id))
4826 or else Is_Controlled (Etype (Id))
4828 if not Is_Library_Level_Entity (Id) then
4829 Check_Restriction (No_Nested_Finalization, N);
4831 Validate_Controlled_Object (Id);
4835 if Has_Task (Etype (Id)) then
4836 Check_Restriction (No_Tasking, N);
4838 -- Deal with counting max tasks
4840 -- Nothing to do if inside a generic
4842 if Inside_A_Generic then
4845 -- If library level entity, then count tasks
4847 elsif Is_Library_Level_Entity (Id) then
4848 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4850 -- If not library level entity, then indicate we don't know max
4851 -- tasks and also check task hierarchy restriction and blocking
4852 -- operation (since starting a task is definitely blocking).
4855 Check_Restriction (Max_Tasks, N);
4856 Check_Restriction (No_Task_Hierarchy, N);
4857 Check_Potentially_Blocking_Operation (N);
4860 -- A rather specialized test. If we see two tasks being declared
4861 -- of the same type in the same object declaration, and the task
4862 -- has an entry with an address clause, we know that program error
4863 -- will be raised at run time since we can't have two tasks with
4864 -- entries at the same address.
4866 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4871 E := First_Entity (Etype (Id));
4872 while Present (E) loop
4873 if Ekind (E) = E_Entry
4874 and then Present (Get_Attribute_Definition_Clause
4875 (E, Attribute_Address))
4877 Error_Msg_Warn := SPARK_Mode /= On;
4879 ("more than one task with same entry address<<", N);
4880 Error_Msg_N ("\Program_Error [<<", N);
4882 Make_Raise_Program_Error (Loc,
4883 Reason => PE_Duplicated_Entry_Address));
4893 -- Some simple constant-propagation: if the expression is a constant
4894 -- string initialized with a literal, share the literal. This avoids
4898 and then Is_Entity_Name (E)
4899 and then Ekind (Entity (E)) = E_Constant
4900 and then Base_Type (Etype (E)) = Standard_String
4903 Val : constant Node_Id := Constant_Value (Entity (E));
4905 if Present (Val) and then Nkind (Val) = N_String_Literal then
4906 Rewrite (E, New_Copy (Val));
4911 -- Another optimization: if the nominal subtype is unconstrained and
4912 -- the expression is a function call that returns an unconstrained
4913 -- type, rewrite the declaration as a renaming of the result of the
4914 -- call. The exceptions below are cases where the copy is expected,
4915 -- either by the back end (Aliased case) or by the semantics, as for
4916 -- initializing controlled types or copying tags for class-wide types.
4919 and then Nkind (E) = N_Explicit_Dereference
4920 and then Nkind (Original_Node (E)) = N_Function_Call
4921 and then not Is_Library_Level_Entity (Id)
4922 and then not Is_Constrained (Underlying_Type (T))
4923 and then not Is_Aliased (Id)
4924 and then not Is_Class_Wide_Type (T)
4925 and then not Is_Controlled (T)
4926 and then not Has_Controlled_Component (Base_Type (T))
4927 and then Expander_Active
4930 Make_Object_Renaming_Declaration (Loc,
4931 Defining_Identifier => Id,
4932 Access_Definition => Empty,
4933 Subtype_Mark => New_Occurrence_Of
4934 (Base_Type (Etype (Id)), Loc),
4937 Set_Renamed_Object (Id, E);
4939 -- Force generation of debugging information for the constant and for
4940 -- the renamed function call.
4942 Set_Debug_Info_Needed (Id);
4943 Set_Debug_Info_Needed (Entity (Prefix (E)));
4946 if Present (Prev_Entity)
4947 and then Is_Frozen (Prev_Entity)
4948 and then not Error_Posted (Id)
4950 Error_Msg_N ("full constant declaration appears too late", N);
4953 Check_Eliminated (Id);
4955 -- Deal with setting In_Private_Part flag if in private part
4957 if Ekind (Scope (Id)) = E_Package
4958 and then In_Private_Part (Scope (Id))
4960 Set_In_Private_Part (Id);
4964 -- Initialize the refined state of a variable here because this is a
4965 -- common destination for legal and illegal object declarations.
4967 if Ekind (Id) = E_Variable then
4968 Set_Encapsulating_State (Id, Empty);
4971 if Has_Aspects (N) then
4972 Analyze_Aspect_Specifications (N, Id);
4975 Analyze_Dimension (N);
4977 -- Verify whether the object declaration introduces an illegal hidden
4978 -- state within a package subject to a null abstract state.
4980 if Ekind (Id) = E_Variable then
4981 Check_No_Hidden_State (Id);
4984 Restore_Ghost_Region (Saved_GM, Saved_IGR);
4985 end Analyze_Object_Declaration;
4987 ---------------------------
4988 -- Analyze_Others_Choice --
4989 ---------------------------
4991 -- Nothing to do for the others choice node itself, the semantic analysis
4992 -- of the others choice will occur as part of the processing of the parent
4994 procedure Analyze_Others_Choice (N : Node_Id) is
4995 pragma Warnings (Off, N);
4998 end Analyze_Others_Choice;
5000 -------------------------------------------
5001 -- Analyze_Private_Extension_Declaration --
5002 -------------------------------------------
5004 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5005 Indic : constant Node_Id := Subtype_Indication (N);
5006 T : constant Entity_Id := Defining_Identifier (N);
5008 Iface_Elmt : Elmt_Id;
5009 Parent_Base : Entity_Id;
5010 Parent_Type : Entity_Id;
5013 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5015 if Is_Non_Empty_List (Interface_List (N)) then
5021 Intf := First (Interface_List (N));
5022 while Present (Intf) loop
5023 T := Find_Type_Of_Subtype_Indic (Intf);
5025 Diagnose_Interface (Intf, T);
5031 Generate_Definition (T);
5033 -- For other than Ada 2012, just enter the name in the current scope
5035 if Ada_Version < Ada_2012 then
5038 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5039 -- case of private type that completes an incomplete type.
5046 Prev := Find_Type_Name (N);
5048 pragma Assert (Prev = T
5049 or else (Ekind (Prev) = E_Incomplete_Type
5050 and then Present (Full_View (Prev))
5051 and then Full_View (Prev) = T));
5055 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5056 Parent_Base := Base_Type (Parent_Type);
5058 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5059 Set_Ekind (T, Ekind (Parent_Type));
5060 Set_Etype (T, Any_Type);
5063 elsif not Is_Tagged_Type (Parent_Type) then
5065 ("parent of type extension must be a tagged type ", Indic);
5068 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
5069 Error_Msg_N ("premature derivation of incomplete type", Indic);
5072 elsif Is_Concurrent_Type (Parent_Type) then
5074 ("parent type of a private extension cannot be a synchronized "
5075 & "tagged type (RM 3.9.1 (3/1))", N);
5077 Set_Etype (T, Any_Type);
5078 Set_Ekind (T, E_Limited_Private_Type);
5079 Set_Private_Dependents (T, New_Elmt_List);
5080 Set_Error_Posted (T);
5084 -- Perhaps the parent type should be changed to the class-wide type's
5085 -- specific type in this case to prevent cascading errors ???
5087 if Is_Class_Wide_Type (Parent_Type) then
5089 ("parent of type extension must not be a class-wide type", Indic);
5093 if (not Is_Package_Or_Generic_Package (Current_Scope)
5094 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5095 or else In_Private_Part (Current_Scope)
5097 Error_Msg_N ("invalid context for private extension", N);
5100 -- Set common attributes
5102 Set_Is_Pure (T, Is_Pure (Current_Scope));
5103 Set_Scope (T, Current_Scope);
5104 Set_Ekind (T, E_Record_Type_With_Private);
5105 Init_Size_Align (T);
5106 Set_Default_SSO (T);
5107 Set_No_Reordering (T, No_Component_Reordering);
5109 Set_Etype (T, Parent_Base);
5110 Propagate_Concurrent_Flags (T, Parent_Base);
5112 Set_Convention (T, Convention (Parent_Type));
5113 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5114 Set_Is_First_Subtype (T);
5115 Make_Class_Wide_Type (T);
5117 -- Set the SPARK mode from the current context
5119 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5120 Set_SPARK_Pragma_Inherited (T);
5122 if Unknown_Discriminants_Present (N) then
5123 Set_Discriminant_Constraint (T, No_Elist);
5126 Build_Derived_Record_Type (N, Parent_Type, T);
5128 -- A private extension inherits the Default_Initial_Condition pragma
5129 -- coming from any parent type within the derivation chain.
5131 if Has_DIC (Parent_Type) then
5132 Set_Has_Inherited_DIC (T);
5135 -- A private extension inherits any class-wide invariants coming from a
5136 -- parent type or an interface. Note that the invariant procedure of the
5137 -- parent type should not be inherited because the private extension may
5138 -- define invariants of its own.
5140 if Has_Inherited_Invariants (Parent_Type)
5141 or else Has_Inheritable_Invariants (Parent_Type)
5143 Set_Has_Inherited_Invariants (T);
5145 elsif Present (Interfaces (T)) then
5146 Iface_Elmt := First_Elmt (Interfaces (T));
5147 while Present (Iface_Elmt) loop
5148 Iface := Node (Iface_Elmt);
5150 if Has_Inheritable_Invariants (Iface) then
5151 Set_Has_Inherited_Invariants (T);
5155 Next_Elmt (Iface_Elmt);
5159 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5160 -- synchronized formal derived type.
5162 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5163 Set_Is_Limited_Record (T);
5165 -- Formal derived type case
5167 if Is_Generic_Type (T) then
5169 -- The parent must be a tagged limited type or a synchronized
5172 if (not Is_Tagged_Type (Parent_Type)
5173 or else not Is_Limited_Type (Parent_Type))
5175 (not Is_Interface (Parent_Type)
5176 or else not Is_Synchronized_Interface (Parent_Type))
5179 ("parent type of & must be tagged limited or synchronized",
5183 -- The progenitors (if any) must be limited or synchronized
5186 if Present (Interfaces (T)) then
5187 Iface_Elmt := First_Elmt (Interfaces (T));
5188 while Present (Iface_Elmt) loop
5189 Iface := Node (Iface_Elmt);
5191 if not Is_Limited_Interface (Iface)
5192 and then not Is_Synchronized_Interface (Iface)
5195 ("progenitor & must be limited or synchronized",
5199 Next_Elmt (Iface_Elmt);
5203 -- Regular derived extension, the parent must be a limited or
5204 -- synchronized interface.
5207 if not Is_Interface (Parent_Type)
5208 or else (not Is_Limited_Interface (Parent_Type)
5209 and then not Is_Synchronized_Interface (Parent_Type))
5212 ("parent type of & must be limited interface", N, T);
5216 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5217 -- extension with a synchronized parent must be explicitly declared
5218 -- synchronized, because the full view will be a synchronized type.
5219 -- This must be checked before the check for limited types below,
5220 -- to ensure that types declared limited are not allowed to extend
5221 -- synchronized interfaces.
5223 elsif Is_Interface (Parent_Type)
5224 and then Is_Synchronized_Interface (Parent_Type)
5225 and then not Synchronized_Present (N)
5228 ("private extension of& must be explicitly synchronized",
5231 elsif Limited_Present (N) then
5232 Set_Is_Limited_Record (T);
5234 if not Is_Limited_Type (Parent_Type)
5236 (not Is_Interface (Parent_Type)
5237 or else not Is_Limited_Interface (Parent_Type))
5239 Error_Msg_NE ("parent type& of limited extension must be limited",
5244 -- Remember that its parent type has a private extension. Used to warn
5245 -- on public primitives of the parent type defined after its private
5246 -- extensions (see Check_Dispatching_Operation).
5248 Set_Has_Private_Extension (Parent_Type);
5251 if Has_Aspects (N) then
5252 Analyze_Aspect_Specifications (N, T);
5254 end Analyze_Private_Extension_Declaration;
5256 ---------------------------------
5257 -- Analyze_Subtype_Declaration --
5258 ---------------------------------
5260 procedure Analyze_Subtype_Declaration
5262 Skip : Boolean := False)
5264 Id : constant Entity_Id := Defining_Identifier (N);
5265 R_Checks : Check_Result;
5269 Generate_Definition (Id);
5270 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5271 Init_Size_Align (Id);
5273 -- The following guard condition on Enter_Name is to handle cases where
5274 -- the defining identifier has already been entered into the scope but
5275 -- the declaration as a whole needs to be analyzed.
5277 -- This case in particular happens for derived enumeration types. The
5278 -- derived enumeration type is processed as an inserted enumeration type
5279 -- declaration followed by a rewritten subtype declaration. The defining
5280 -- identifier, however, is entered into the name scope very early in the
5281 -- processing of the original type declaration and therefore needs to be
5282 -- avoided here, when the created subtype declaration is analyzed. (See
5283 -- Build_Derived_Types)
5285 -- This also happens when the full view of a private type is derived
5286 -- type with constraints. In this case the entity has been introduced
5287 -- in the private declaration.
5289 -- Finally this happens in some complex cases when validity checks are
5290 -- enabled, where the same subtype declaration may be analyzed twice.
5291 -- This can happen if the subtype is created by the pre-analysis of
5292 -- an attribute tht gives the range of a loop statement, and the loop
5293 -- itself appears within an if_statement that will be rewritten during
5297 or else (Present (Etype (Id))
5298 and then (Is_Private_Type (Etype (Id))
5299 or else Is_Task_Type (Etype (Id))
5300 or else Is_Rewrite_Substitution (N)))
5304 elsif Current_Entity (Id) = Id then
5311 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5313 -- Class-wide equivalent types of records with unknown discriminants
5314 -- involve the generation of an itype which serves as the private view
5315 -- of a constrained record subtype. In such cases the base type of the
5316 -- current subtype we are processing is the private itype. Use the full
5317 -- of the private itype when decorating various attributes.
5320 and then Is_Private_Type (T)
5321 and then Present (Full_View (T))
5326 -- Inherit common attributes
5328 Set_Is_Volatile (Id, Is_Volatile (T));
5329 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5330 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5331 Set_Convention (Id, Convention (T));
5333 -- If ancestor has predicates then so does the subtype, and in addition
5334 -- we must delay the freeze to properly arrange predicate inheritance.
5336 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5337 -- in which T = ID, so the above tests and assignments do nothing???
5339 if Has_Predicates (T)
5340 or else (Present (Ancestor_Subtype (T))
5341 and then Has_Predicates (Ancestor_Subtype (T)))
5343 Set_Has_Predicates (Id);
5344 Set_Has_Delayed_Freeze (Id);
5346 -- Generated subtypes inherit the predicate function from the parent
5347 -- (no aspects to examine on the generated declaration).
5349 if not Comes_From_Source (N) then
5350 Set_Ekind (Id, Ekind (T));
5352 if Present (Predicate_Function (Id)) then
5355 elsif Present (Predicate_Function (T)) then
5356 Set_Predicate_Function (Id, Predicate_Function (T));
5358 elsif Present (Ancestor_Subtype (T))
5359 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5361 Set_Predicate_Function (Id,
5362 Predicate_Function (Ancestor_Subtype (T)));
5367 -- Subtype of Boolean cannot have a constraint in SPARK
5369 if Is_Boolean_Type (T)
5370 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5372 Check_SPARK_05_Restriction
5373 ("subtype of Boolean cannot have constraint", N);
5376 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5378 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5384 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5385 One_Cstr := First (Constraints (Cstr));
5386 while Present (One_Cstr) loop
5388 -- Index or discriminant constraint in SPARK must be a
5392 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5394 Check_SPARK_05_Restriction
5395 ("subtype mark required", One_Cstr);
5397 -- String subtype must have a lower bound of 1 in SPARK.
5398 -- Note that we do not need to test for the non-static case
5399 -- here, since that was already taken care of in
5400 -- Process_Range_Expr_In_Decl.
5402 elsif Base_Type (T) = Standard_String then
5403 Get_Index_Bounds (One_Cstr, Low, High);
5405 if Is_OK_Static_Expression (Low)
5406 and then Expr_Value (Low) /= 1
5408 Check_SPARK_05_Restriction
5409 ("String subtype must have lower bound of 1", N);
5419 -- In the case where there is no constraint given in the subtype
5420 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5421 -- semantic attributes must be established here.
5423 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5424 Set_Etype (Id, Base_Type (T));
5426 -- Subtype of unconstrained array without constraint is not allowed
5429 if Is_Array_Type (T) and then not Is_Constrained (T) then
5430 Check_SPARK_05_Restriction
5431 ("subtype of unconstrained array must have constraint", N);
5436 Set_Ekind (Id, E_Array_Subtype);
5437 Copy_Array_Subtype_Attributes (Id, T);
5439 when Decimal_Fixed_Point_Kind =>
5440 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5441 Set_Digits_Value (Id, Digits_Value (T));
5442 Set_Delta_Value (Id, Delta_Value (T));
5443 Set_Scale_Value (Id, Scale_Value (T));
5444 Set_Small_Value (Id, Small_Value (T));
5445 Set_Scalar_Range (Id, Scalar_Range (T));
5446 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5447 Set_Is_Constrained (Id, Is_Constrained (T));
5448 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5449 Set_RM_Size (Id, RM_Size (T));
5451 when Enumeration_Kind =>
5452 Set_Ekind (Id, E_Enumeration_Subtype);
5453 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5454 Set_Scalar_Range (Id, Scalar_Range (T));
5455 Set_Is_Character_Type (Id, Is_Character_Type (T));
5456 Set_Is_Constrained (Id, Is_Constrained (T));
5457 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5458 Set_RM_Size (Id, RM_Size (T));
5460 when Ordinary_Fixed_Point_Kind =>
5461 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5462 Set_Scalar_Range (Id, Scalar_Range (T));
5463 Set_Small_Value (Id, Small_Value (T));
5464 Set_Delta_Value (Id, Delta_Value (T));
5465 Set_Is_Constrained (Id, Is_Constrained (T));
5466 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5467 Set_RM_Size (Id, RM_Size (T));
5470 Set_Ekind (Id, E_Floating_Point_Subtype);
5471 Set_Scalar_Range (Id, Scalar_Range (T));
5472 Set_Digits_Value (Id, Digits_Value (T));
5473 Set_Is_Constrained (Id, Is_Constrained (T));
5475 -- If the floating point type has dimensions, these will be
5476 -- inherited subsequently when Analyze_Dimensions is called.
5478 when Signed_Integer_Kind =>
5479 Set_Ekind (Id, E_Signed_Integer_Subtype);
5480 Set_Scalar_Range (Id, Scalar_Range (T));
5481 Set_Is_Constrained (Id, Is_Constrained (T));
5482 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5483 Set_RM_Size (Id, RM_Size (T));
5485 when Modular_Integer_Kind =>
5486 Set_Ekind (Id, E_Modular_Integer_Subtype);
5487 Set_Scalar_Range (Id, Scalar_Range (T));
5488 Set_Is_Constrained (Id, Is_Constrained (T));
5489 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5490 Set_RM_Size (Id, RM_Size (T));
5492 when Class_Wide_Kind =>
5493 Set_Ekind (Id, E_Class_Wide_Subtype);
5494 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5495 Set_Cloned_Subtype (Id, T);
5496 Set_Is_Tagged_Type (Id, True);
5497 Set_Has_Unknown_Discriminants
5499 Set_No_Tagged_Streams_Pragma
5500 (Id, No_Tagged_Streams_Pragma (T));
5502 if Ekind (T) = E_Class_Wide_Subtype then
5503 Set_Equivalent_Type (Id, Equivalent_Type (T));
5506 when E_Record_Subtype
5509 Set_Ekind (Id, E_Record_Subtype);
5511 if Ekind (T) = E_Record_Subtype
5512 and then Present (Cloned_Subtype (T))
5514 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5516 Set_Cloned_Subtype (Id, T);
5519 Set_First_Entity (Id, First_Entity (T));
5520 Set_Last_Entity (Id, Last_Entity (T));
5521 Set_Has_Discriminants (Id, Has_Discriminants (T));
5522 Set_Is_Constrained (Id, Is_Constrained (T));
5523 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5524 Set_Has_Implicit_Dereference
5525 (Id, Has_Implicit_Dereference (T));
5526 Set_Has_Unknown_Discriminants
5527 (Id, Has_Unknown_Discriminants (T));
5529 if Has_Discriminants (T) then
5530 Set_Discriminant_Constraint
5531 (Id, Discriminant_Constraint (T));
5532 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5534 elsif Has_Unknown_Discriminants (Id) then
5535 Set_Discriminant_Constraint (Id, No_Elist);
5538 if Is_Tagged_Type (T) then
5539 Set_Is_Tagged_Type (Id, True);
5540 Set_No_Tagged_Streams_Pragma
5541 (Id, No_Tagged_Streams_Pragma (T));
5542 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5543 Set_Direct_Primitive_Operations
5544 (Id, Direct_Primitive_Operations (T));
5545 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5547 if Is_Interface (T) then
5548 Set_Is_Interface (Id);
5549 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5553 when Private_Kind =>
5554 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5555 Set_Has_Discriminants (Id, Has_Discriminants (T));
5556 Set_Is_Constrained (Id, Is_Constrained (T));
5557 Set_First_Entity (Id, First_Entity (T));
5558 Set_Last_Entity (Id, Last_Entity (T));
5559 Set_Private_Dependents (Id, New_Elmt_List);
5560 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5561 Set_Has_Implicit_Dereference
5562 (Id, Has_Implicit_Dereference (T));
5563 Set_Has_Unknown_Discriminants
5564 (Id, Has_Unknown_Discriminants (T));
5565 Set_Known_To_Have_Preelab_Init
5566 (Id, Known_To_Have_Preelab_Init (T));
5568 if Is_Tagged_Type (T) then
5569 Set_Is_Tagged_Type (Id);
5570 Set_No_Tagged_Streams_Pragma (Id,
5571 No_Tagged_Streams_Pragma (T));
5572 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5573 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5574 Set_Direct_Primitive_Operations (Id,
5575 Direct_Primitive_Operations (T));
5578 -- In general the attributes of the subtype of a private type
5579 -- are the attributes of the partial view of parent. However,
5580 -- the full view may be a discriminated type, and the subtype
5581 -- must share the discriminant constraint to generate correct
5582 -- calls to initialization procedures.
5584 if Has_Discriminants (T) then
5585 Set_Discriminant_Constraint
5586 (Id, Discriminant_Constraint (T));
5587 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5589 elsif Present (Full_View (T))
5590 and then Has_Discriminants (Full_View (T))
5592 Set_Discriminant_Constraint
5593 (Id, Discriminant_Constraint (Full_View (T)));
5594 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5596 -- This would seem semantically correct, but apparently
5597 -- generates spurious errors about missing components ???
5599 -- Set_Has_Discriminants (Id);
5602 Prepare_Private_Subtype_Completion (Id, N);
5604 -- If this is the subtype of a constrained private type with
5605 -- discriminants that has got a full view and we also have
5606 -- built a completion just above, show that the completion
5607 -- is a clone of the full view to the back-end.
5609 if Has_Discriminants (T)
5610 and then not Has_Unknown_Discriminants (T)
5611 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5612 and then Present (Full_View (T))
5613 and then Present (Full_View (Id))
5615 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5619 Set_Ekind (Id, E_Access_Subtype);
5620 Set_Is_Constrained (Id, Is_Constrained (T));
5621 Set_Is_Access_Constant
5622 (Id, Is_Access_Constant (T));
5623 Set_Directly_Designated_Type
5624 (Id, Designated_Type (T));
5625 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5627 -- A Pure library_item must not contain the declaration of a
5628 -- named access type, except within a subprogram, generic
5629 -- subprogram, task unit, or protected unit, or if it has
5630 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5632 if Comes_From_Source (Id)
5633 and then In_Pure_Unit
5634 and then not In_Subprogram_Task_Protected_Unit
5635 and then not No_Pool_Assigned (Id)
5638 ("named access types not allowed in pure unit", N);
5641 when Concurrent_Kind =>
5642 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5643 Set_Corresponding_Record_Type (Id,
5644 Corresponding_Record_Type (T));
5645 Set_First_Entity (Id, First_Entity (T));
5646 Set_First_Private_Entity (Id, First_Private_Entity (T));
5647 Set_Has_Discriminants (Id, Has_Discriminants (T));
5648 Set_Is_Constrained (Id, Is_Constrained (T));
5649 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5650 Set_Last_Entity (Id, Last_Entity (T));
5652 if Is_Tagged_Type (T) then
5653 Set_No_Tagged_Streams_Pragma
5654 (Id, No_Tagged_Streams_Pragma (T));
5657 if Has_Discriminants (T) then
5658 Set_Discriminant_Constraint
5659 (Id, Discriminant_Constraint (T));
5660 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5663 when Incomplete_Kind =>
5664 if Ada_Version >= Ada_2005 then
5666 -- In Ada 2005 an incomplete type can be explicitly tagged:
5667 -- propagate indication. Note that we also have to include
5668 -- subtypes for Ada 2012 extended use of incomplete types.
5670 Set_Ekind (Id, E_Incomplete_Subtype);
5671 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5672 Set_Private_Dependents (Id, New_Elmt_List);
5674 if Is_Tagged_Type (Id) then
5675 Set_No_Tagged_Streams_Pragma
5676 (Id, No_Tagged_Streams_Pragma (T));
5677 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5680 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5681 -- incomplete type visible through a limited with clause.
5683 if From_Limited_With (T)
5684 and then Present (Non_Limited_View (T))
5686 Set_From_Limited_With (Id);
5687 Set_Non_Limited_View (Id, Non_Limited_View (T));
5689 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5690 -- to the private dependents of the original incomplete
5691 -- type for future transformation.
5694 Append_Elmt (Id, Private_Dependents (T));
5697 -- If the subtype name denotes an incomplete type an error
5698 -- was already reported by Process_Subtype.
5701 Set_Etype (Id, Any_Type);
5705 raise Program_Error;
5708 -- If there is no constraint in the subtype indication, the
5709 -- declared entity inherits predicates from the parent.
5711 Inherit_Predicate_Flags (Id, T);
5714 if Etype (Id) = Any_Type then
5718 -- Some common processing on all types
5720 Set_Size_Info (Id, T);
5721 Set_First_Rep_Item (Id, First_Rep_Item (T));
5723 -- If the parent type is a generic actual, so is the subtype. This may
5724 -- happen in a nested instance. Why Comes_From_Source test???
5726 if not Comes_From_Source (N) then
5727 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5730 -- If this is a subtype declaration for an actual in an instance,
5731 -- inherit static and dynamic predicates if any.
5733 -- If declaration has no aspect specifications, inherit predicate
5734 -- info as well. Unclear how to handle the case of both specified
5735 -- and inherited predicates ??? Other inherited aspects, such as
5736 -- invariants, should be OK, but the combination with later pragmas
5737 -- may also require special merging.
5739 if Has_Predicates (T)
5740 and then Present (Predicate_Function (T))
5742 ((In_Instance and then not Comes_From_Source (N))
5743 or else No (Aspect_Specifications (N)))
5745 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5747 if Has_Static_Predicate (T) then
5748 Set_Has_Static_Predicate (Id);
5749 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5753 -- Remaining processing depends on characteristics of base type
5757 Set_Is_Immediately_Visible (Id, True);
5758 Set_Depends_On_Private (Id, Has_Private_Component (T));
5759 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5761 if Is_Interface (T) then
5762 Set_Is_Interface (Id);
5765 if Present (Generic_Parent_Type (N))
5767 (Nkind (Parent (Generic_Parent_Type (N))) /=
5768 N_Formal_Type_Declaration
5769 or else Nkind (Formal_Type_Definition
5770 (Parent (Generic_Parent_Type (N)))) /=
5771 N_Formal_Private_Type_Definition)
5773 if Is_Tagged_Type (Id) then
5775 -- If this is a generic actual subtype for a synchronized type,
5776 -- the primitive operations are those of the corresponding record
5777 -- for which there is a separate subtype declaration.
5779 if Is_Concurrent_Type (Id) then
5781 elsif Is_Class_Wide_Type (Id) then
5782 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5784 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5787 elsif Scope (Etype (Id)) /= Standard_Standard then
5788 Derive_Subprograms (Generic_Parent_Type (N), Id);
5792 if Is_Private_Type (T) and then Present (Full_View (T)) then
5793 Conditional_Delay (Id, Full_View (T));
5795 -- The subtypes of components or subcomponents of protected types
5796 -- do not need freeze nodes, which would otherwise appear in the
5797 -- wrong scope (before the freeze node for the protected type). The
5798 -- proper subtypes are those of the subcomponents of the corresponding
5801 elsif Ekind (Scope (Id)) /= E_Protected_Type
5802 and then Present (Scope (Scope (Id))) -- error defense
5803 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5805 Conditional_Delay (Id, T);
5808 -- If we have a subtype of an incomplete type whose full type is a
5809 -- derived numeric type, we need to have a freeze node for the subtype.
5810 -- Otherwise gigi will complain while computing the (static) bounds of
5814 and then Is_Elementary_Type (Id)
5815 and then Etype (Id) /= Id
5818 Partial : constant Entity_Id :=
5819 Incomplete_Or_Partial_View (First_Subtype (Id));
5821 if Present (Partial)
5822 and then Ekind (Partial) = E_Incomplete_Type
5824 Set_Has_Delayed_Freeze (Id);
5829 -- Check that Constraint_Error is raised for a scalar subtype indication
5830 -- when the lower or upper bound of a non-null range lies outside the
5831 -- range of the type mark.
5833 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5834 if Is_Scalar_Type (Etype (Id))
5835 and then Scalar_Range (Id) /=
5837 (Etype (Subtype_Mark (Subtype_Indication (N))))
5841 Etype (Subtype_Mark (Subtype_Indication (N))));
5843 -- In the array case, check compatibility for each index
5845 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5847 -- This really should be a subprogram that finds the indications
5851 Subt_Index : Node_Id := First_Index (Id);
5852 Target_Index : Node_Id :=
5854 (Subtype_Mark (Subtype_Indication (N))));
5855 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5858 while Present (Subt_Index) loop
5859 if ((Nkind (Subt_Index) = N_Identifier
5860 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5861 or else Nkind (Subt_Index) = N_Subtype_Indication)
5863 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5866 Target_Typ : constant Entity_Id :=
5867 Etype (Target_Index);
5871 (Scalar_Range (Etype (Subt_Index)),
5874 Defining_Identifier (N));
5876 -- Reset Has_Dynamic_Range_Check on the subtype to
5877 -- prevent elision of the index check due to a dynamic
5878 -- check generated for a preceding index (needed since
5879 -- Insert_Range_Checks tries to avoid generating
5880 -- redundant checks on a given declaration).
5882 Set_Has_Dynamic_Range_Check (N, False);
5888 Sloc (Defining_Identifier (N)));
5890 -- Record whether this index involved a dynamic check
5893 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5897 Next_Index (Subt_Index);
5898 Next_Index (Target_Index);
5901 -- Finally, mark whether the subtype involves dynamic checks
5903 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5908 Set_Optimize_Alignment_Flags (Id);
5909 Check_Eliminated (Id);
5912 if Has_Aspects (N) then
5913 Analyze_Aspect_Specifications (N, Id);
5916 Analyze_Dimension (N);
5918 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5919 -- indications on composite types where the constraints are dynamic.
5920 -- Note that object declarations and aggregates generate implicit
5921 -- subtype declarations, which this covers. One special case is that the
5922 -- implicitly generated "=" for discriminated types includes an
5923 -- offending subtype declaration, which is harmless, so we ignore it
5926 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5928 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5930 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5931 and then not (Is_Internal (Id)
5932 and then Is_TSS (Scope (Id),
5933 TSS_Composite_Equality))
5934 and then not Within_Init_Proc
5935 and then not All_Composite_Constraints_Static (Cstr)
5937 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5941 end Analyze_Subtype_Declaration;
5943 --------------------------------
5944 -- Analyze_Subtype_Indication --
5945 --------------------------------
5947 procedure Analyze_Subtype_Indication (N : Node_Id) is
5948 T : constant Entity_Id := Subtype_Mark (N);
5949 R : constant Node_Id := Range_Expression (Constraint (N));
5956 Set_Etype (N, Etype (R));
5957 Resolve (R, Entity (T));
5959 Set_Error_Posted (R);
5960 Set_Error_Posted (T);
5962 end Analyze_Subtype_Indication;
5964 --------------------------
5965 -- Analyze_Variant_Part --
5966 --------------------------
5968 procedure Analyze_Variant_Part (N : Node_Id) is
5969 Discr_Name : Node_Id;
5970 Discr_Type : Entity_Id;
5972 procedure Process_Variant (A : Node_Id);
5973 -- Analyze declarations for a single variant
5975 package Analyze_Variant_Choices is
5976 new Generic_Analyze_Choices (Process_Variant);
5977 use Analyze_Variant_Choices;
5979 ---------------------
5980 -- Process_Variant --
5981 ---------------------
5983 procedure Process_Variant (A : Node_Id) is
5984 CL : constant Node_Id := Component_List (A);
5986 if not Null_Present (CL) then
5987 Analyze_Declarations (Component_Items (CL));
5989 if Present (Variant_Part (CL)) then
5990 Analyze (Variant_Part (CL));
5993 end Process_Variant;
5995 -- Start of processing for Analyze_Variant_Part
5998 Discr_Name := Name (N);
5999 Analyze (Discr_Name);
6001 -- If Discr_Name bad, get out (prevent cascaded errors)
6003 if Etype (Discr_Name) = Any_Type then
6007 -- Check invalid discriminant in variant part
6009 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6010 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6013 Discr_Type := Etype (Entity (Discr_Name));
6015 if not Is_Discrete_Type (Discr_Type) then
6017 ("discriminant in a variant part must be of a discrete type",
6022 -- Now analyze the choices, which also analyzes the declarations that
6023 -- are associated with each choice.
6025 Analyze_Choices (Variants (N), Discr_Type);
6027 -- Note: we used to instantiate and call Check_Choices here to check
6028 -- that the choices covered the discriminant, but it's too early to do
6029 -- that because of statically predicated subtypes, whose analysis may
6030 -- be deferred to their freeze point which may be as late as the freeze
6031 -- point of the containing record. So this call is now to be found in
6032 -- Freeze_Record_Declaration.
6034 end Analyze_Variant_Part;
6036 ----------------------------
6037 -- Array_Type_Declaration --
6038 ----------------------------
6040 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6041 Component_Def : constant Node_Id := Component_Definition (Def);
6042 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6043 P : constant Node_Id := Parent (Def);
6044 Element_Type : Entity_Id;
6045 Implicit_Base : Entity_Id;
6049 Related_Id : Entity_Id := Empty;
6052 if Nkind (Def) = N_Constrained_Array_Definition then
6053 Index := First (Discrete_Subtype_Definitions (Def));
6055 Index := First (Subtype_Marks (Def));
6058 -- Find proper names for the implicit types which may be public. In case
6059 -- of anonymous arrays we use the name of the first object of that type
6063 Related_Id := Defining_Identifier (P);
6069 while Present (Index) loop
6072 -- Test for odd case of trying to index a type by the type itself
6074 if Is_Entity_Name (Index) and then Entity (Index) = T then
6075 Error_Msg_N ("type& cannot be indexed by itself", Index);
6076 Set_Entity (Index, Standard_Boolean);
6077 Set_Etype (Index, Standard_Boolean);
6080 -- Check SPARK restriction requiring a subtype mark
6082 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
6083 Check_SPARK_05_Restriction ("subtype mark required", Index);
6086 -- Add a subtype declaration for each index of private array type
6087 -- declaration whose etype is also private. For example:
6090 -- type Index is private;
6092 -- type Table is array (Index) of ...
6095 -- This is currently required by the expander for the internally
6096 -- generated equality subprogram of records with variant parts in
6097 -- which the etype of some component is such private type.
6099 if Ekind (Current_Scope) = E_Package
6100 and then In_Private_Part (Current_Scope)
6101 and then Has_Private_Declaration (Etype (Index))
6104 Loc : constant Source_Ptr := Sloc (Def);
6109 New_E := Make_Temporary (Loc, 'T');
6110 Set_Is_Internal (New_E);
6113 Make_Subtype_Declaration (Loc,
6114 Defining_Identifier => New_E,
6115 Subtype_Indication =>
6116 New_Occurrence_Of (Etype (Index), Loc));
6118 Insert_Before (Parent (Def), Decl);
6120 Set_Etype (Index, New_E);
6122 -- If the index is a range or a subtype indication it carries
6123 -- no entity. Example:
6126 -- type T is private;
6128 -- type T is new Natural;
6129 -- Table : array (T(1) .. T(10)) of Boolean;
6132 -- Otherwise the type of the reference is its entity.
6134 if Is_Entity_Name (Index) then
6135 Set_Entity (Index, New_E);
6140 Make_Index (Index, P, Related_Id, Nb_Index);
6142 -- Check error of subtype with predicate for index type
6144 Bad_Predicated_Subtype_Use
6145 ("subtype& has predicate, not allowed as index subtype",
6146 Index, Etype (Index));
6148 -- Move to next index
6151 Nb_Index := Nb_Index + 1;
6154 -- Process subtype indication if one is present
6156 if Present (Component_Typ) then
6157 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6159 Set_Etype (Component_Typ, Element_Type);
6161 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
6162 Check_SPARK_05_Restriction
6163 ("subtype mark required", Component_Typ);
6166 -- Ada 2005 (AI-230): Access Definition case
6168 else pragma Assert (Present (Access_Definition (Component_Def)));
6170 -- Indicate that the anonymous access type is created by the
6171 -- array type declaration.
6173 Element_Type := Access_Definition
6175 N => Access_Definition (Component_Def));
6176 Set_Is_Local_Anonymous_Access (Element_Type);
6178 -- Propagate the parent. This field is needed if we have to generate
6179 -- the master_id associated with an anonymous access to task type
6180 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6182 Set_Parent (Element_Type, Parent (T));
6184 -- Ada 2005 (AI-230): In case of components that are anonymous access
6185 -- types the level of accessibility depends on the enclosing type
6188 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6190 -- Ada 2005 (AI-254)
6193 CD : constant Node_Id :=
6194 Access_To_Subprogram_Definition
6195 (Access_Definition (Component_Def));
6197 if Present (CD) and then Protected_Present (CD) then
6199 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6204 -- Constrained array case
6207 T := Create_Itype (E_Void, P, Related_Id, 'T');
6210 if Nkind (Def) = N_Constrained_Array_Definition then
6212 -- Establish Implicit_Base as unconstrained base type
6214 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6216 Set_Etype (Implicit_Base, Implicit_Base);
6217 Set_Scope (Implicit_Base, Current_Scope);
6218 Set_Has_Delayed_Freeze (Implicit_Base);
6219 Set_Default_SSO (Implicit_Base);
6221 -- The constrained array type is a subtype of the unconstrained one
6223 Set_Ekind (T, E_Array_Subtype);
6224 Init_Size_Align (T);
6225 Set_Etype (T, Implicit_Base);
6226 Set_Scope (T, Current_Scope);
6227 Set_Is_Constrained (T);
6229 First (Discrete_Subtype_Definitions (Def)));
6230 Set_Has_Delayed_Freeze (T);
6232 -- Complete setup of implicit base type
6234 Set_Component_Size (Implicit_Base, Uint_0);
6235 Set_Component_Type (Implicit_Base, Element_Type);
6236 Set_Finalize_Storage_Only
6238 Finalize_Storage_Only (Element_Type));
6239 Set_First_Index (Implicit_Base, First_Index (T));
6240 Set_Has_Controlled_Component
6242 Has_Controlled_Component (Element_Type)
6243 or else Is_Controlled (Element_Type));
6244 Set_Packed_Array_Impl_Type
6245 (Implicit_Base, Empty);
6247 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6249 -- Unconstrained array case
6252 Set_Ekind (T, E_Array_Type);
6253 Init_Size_Align (T);
6255 Set_Scope (T, Current_Scope);
6256 Set_Component_Size (T, Uint_0);
6257 Set_Is_Constrained (T, False);
6258 Set_First_Index (T, First (Subtype_Marks (Def)));
6259 Set_Has_Delayed_Freeze (T, True);
6260 Propagate_Concurrent_Flags (T, Element_Type);
6261 Set_Has_Controlled_Component (T, Has_Controlled_Component
6264 Is_Controlled (Element_Type));
6265 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6267 Set_Default_SSO (T);
6270 -- Common attributes for both cases
6272 Set_Component_Type (Base_Type (T), Element_Type);
6273 Set_Packed_Array_Impl_Type (T, Empty);
6275 if Aliased_Present (Component_Definition (Def)) then
6276 Check_SPARK_05_Restriction
6277 ("aliased is not allowed", Component_Definition (Def));
6278 Set_Has_Aliased_Components (Etype (T));
6281 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6282 -- array type to ensure that objects of this type are initialized.
6284 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6285 Set_Can_Never_Be_Null (T);
6287 if Null_Exclusion_Present (Component_Definition (Def))
6289 -- No need to check itypes because in their case this check was
6290 -- done at their point of creation
6292 and then not Is_Itype (Element_Type)
6295 ("`NOT NULL` not allowed (null already excluded)",
6296 Subtype_Indication (Component_Definition (Def)));
6300 Priv := Private_Component (Element_Type);
6302 if Present (Priv) then
6304 -- Check for circular definitions
6306 if Priv = Any_Type then
6307 Set_Component_Type (Etype (T), Any_Type);
6309 -- There is a gap in the visibility of operations on the composite
6310 -- type only if the component type is defined in a different scope.
6312 elsif Scope (Priv) = Current_Scope then
6315 elsif Is_Limited_Type (Priv) then
6316 Set_Is_Limited_Composite (Etype (T));
6317 Set_Is_Limited_Composite (T);
6319 Set_Is_Private_Composite (Etype (T));
6320 Set_Is_Private_Composite (T);
6324 -- A syntax error in the declaration itself may lead to an empty index
6325 -- list, in which case do a minimal patch.
6327 if No (First_Index (T)) then
6328 Error_Msg_N ("missing index definition in array type declaration", T);
6331 Indexes : constant List_Id :=
6332 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6334 Set_Discrete_Subtype_Definitions (Def, Indexes);
6335 Set_First_Index (T, First (Indexes));
6340 -- Create a concatenation operator for the new type. Internal array
6341 -- types created for packed entities do not need such, they are
6342 -- compatible with the user-defined type.
6344 if Number_Dimensions (T) = 1
6345 and then not Is_Packed_Array_Impl_Type (T)
6347 New_Concatenation_Op (T);
6350 -- In the case of an unconstrained array the parser has already verified
6351 -- that all the indexes are unconstrained but we still need to make sure
6352 -- that the element type is constrained.
6354 if not Is_Definite_Subtype (Element_Type) then
6356 ("unconstrained element type in array declaration",
6357 Subtype_Indication (Component_Def));
6359 elsif Is_Abstract_Type (Element_Type) then
6361 ("the type of a component cannot be abstract",
6362 Subtype_Indication (Component_Def));
6365 -- There may be an invariant declared for the component type, but
6366 -- the construction of the component invariant checking procedure
6367 -- takes place during expansion.
6368 end Array_Type_Declaration;
6370 ------------------------------------------------------
6371 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6372 ------------------------------------------------------
6374 function Replace_Anonymous_Access_To_Protected_Subprogram
6375 (N : Node_Id) return Entity_Id
6377 Loc : constant Source_Ptr := Sloc (N);
6379 Curr_Scope : constant Scope_Stack_Entry :=
6380 Scope_Stack.Table (Scope_Stack.Last);
6382 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6385 -- Access definition in declaration
6388 -- Object definition or formal definition with an access definition
6391 -- Declaration of anonymous access to subprogram type
6394 -- Original specification in access to subprogram
6399 Set_Is_Internal (Anon);
6402 when N_Constrained_Array_Definition
6403 | N_Component_Declaration
6404 | N_Unconstrained_Array_Definition
6406 Comp := Component_Definition (N);
6407 Acc := Access_Definition (Comp);
6409 when N_Discriminant_Specification =>
6410 Comp := Discriminant_Type (N);
6413 when N_Parameter_Specification =>
6414 Comp := Parameter_Type (N);
6417 when N_Access_Function_Definition =>
6418 Comp := Result_Definition (N);
6421 when N_Object_Declaration =>
6422 Comp := Object_Definition (N);
6425 when N_Function_Specification =>
6426 Comp := Result_Definition (N);
6430 raise Program_Error;
6433 Spec := Access_To_Subprogram_Definition (Acc);
6436 Make_Full_Type_Declaration (Loc,
6437 Defining_Identifier => Anon,
6438 Type_Definition => Copy_Separate_Tree (Spec));
6440 Mark_Rewrite_Insertion (Decl);
6442 -- In ASIS mode, analyze the profile on the original node, because
6443 -- the separate copy does not provide enough links to recover the
6444 -- original tree. Analysis is limited to type annotations, within
6445 -- a temporary scope that serves as an anonymous subprogram to collect
6446 -- otherwise useless temporaries and itypes.
6450 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
6453 if Nkind (Spec) = N_Access_Function_Definition then
6454 Set_Ekind (Typ, E_Function);
6456 Set_Ekind (Typ, E_Procedure);
6459 Set_Parent (Typ, N);
6460 Set_Scope (Typ, Current_Scope);
6463 -- Nothing to do if procedure is parameterless
6465 if Present (Parameter_Specifications (Spec)) then
6466 Process_Formals (Parameter_Specifications (Spec), Spec);
6469 if Nkind (Spec) = N_Access_Function_Definition then
6471 Def : constant Node_Id := Result_Definition (Spec);
6474 -- The result might itself be an anonymous access type, so
6477 if Nkind (Def) = N_Access_Definition then
6478 if Present (Access_To_Subprogram_Definition (Def)) then
6481 Replace_Anonymous_Access_To_Protected_Subprogram
6484 Find_Type (Subtype_Mark (Def));
6497 -- Insert the new declaration in the nearest enclosing scope. If the
6498 -- parent is a body and N is its return type, the declaration belongs
6499 -- in the enclosing scope. Likewise if N is the type of a parameter.
6503 if Nkind (N) = N_Function_Specification
6504 and then Nkind (P) = N_Subprogram_Body
6507 elsif Nkind (N) = N_Parameter_Specification
6508 and then Nkind (P) in N_Subprogram_Specification
6509 and then Nkind (Parent (P)) = N_Subprogram_Body
6511 P := Parent (Parent (P));
6514 while Present (P) and then not Has_Declarations (P) loop
6518 pragma Assert (Present (P));
6520 if Nkind (P) = N_Package_Specification then
6521 Prepend (Decl, Visible_Declarations (P));
6523 Prepend (Decl, Declarations (P));
6526 -- Replace the anonymous type with an occurrence of the new declaration.
6527 -- In all cases the rewritten node does not have the null-exclusion
6528 -- attribute because (if present) it was already inherited by the
6529 -- anonymous entity (Anon). Thus, in case of components we do not
6530 -- inherit this attribute.
6532 if Nkind (N) = N_Parameter_Specification then
6533 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6534 Set_Etype (Defining_Identifier (N), Anon);
6535 Set_Null_Exclusion_Present (N, False);
6537 elsif Nkind (N) = N_Object_Declaration then
6538 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6539 Set_Etype (Defining_Identifier (N), Anon);
6541 elsif Nkind (N) = N_Access_Function_Definition then
6542 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6544 elsif Nkind (N) = N_Function_Specification then
6545 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6546 Set_Etype (Defining_Unit_Name (N), Anon);
6550 Make_Component_Definition (Loc,
6551 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6554 Mark_Rewrite_Insertion (Comp);
6556 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6557 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6558 and then not Is_Type (Current_Scope))
6561 -- Declaration can be analyzed in the current scope.
6566 -- Temporarily remove the current scope (record or subprogram) from
6567 -- the stack to add the new declarations to the enclosing scope.
6568 -- The anonymous entity is an Itype with the proper attributes.
6570 Scope_Stack.Decrement_Last;
6572 Set_Is_Itype (Anon);
6573 Set_Associated_Node_For_Itype (Anon, N);
6574 Scope_Stack.Append (Curr_Scope);
6577 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6578 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6580 end Replace_Anonymous_Access_To_Protected_Subprogram;
6582 -------------------------------
6583 -- Build_Derived_Access_Type --
6584 -------------------------------
6586 procedure Build_Derived_Access_Type
6588 Parent_Type : Entity_Id;
6589 Derived_Type : Entity_Id)
6591 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6593 Desig_Type : Entity_Id;
6595 Discr_Con_Elist : Elist_Id;
6596 Discr_Con_El : Elmt_Id;
6600 -- Set the designated type so it is available in case this is an access
6601 -- to a self-referential type, e.g. a standard list type with a next
6602 -- pointer. Will be reset after subtype is built.
6604 Set_Directly_Designated_Type
6605 (Derived_Type, Designated_Type (Parent_Type));
6607 Subt := Process_Subtype (S, N);
6609 if Nkind (S) /= N_Subtype_Indication
6610 and then Subt /= Base_Type (Subt)
6612 Set_Ekind (Derived_Type, E_Access_Subtype);
6615 if Ekind (Derived_Type) = E_Access_Subtype then
6617 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6618 Ibase : constant Entity_Id :=
6619 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6620 Svg_Chars : constant Name_Id := Chars (Ibase);
6621 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6622 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
6625 Copy_Node (Pbase, Ibase);
6627 -- Restore Itype status after Copy_Node
6629 Set_Is_Itype (Ibase);
6630 Set_Associated_Node_For_Itype (Ibase, N);
6632 Set_Chars (Ibase, Svg_Chars);
6633 Set_Prev_Entity (Ibase, Svg_Prev_E);
6634 Set_Next_Entity (Ibase, Svg_Next_E);
6635 Set_Sloc (Ibase, Sloc (Derived_Type));
6636 Set_Scope (Ibase, Scope (Derived_Type));
6637 Set_Freeze_Node (Ibase, Empty);
6638 Set_Is_Frozen (Ibase, False);
6639 Set_Comes_From_Source (Ibase, False);
6640 Set_Is_First_Subtype (Ibase, False);
6642 Set_Etype (Ibase, Pbase);
6643 Set_Etype (Derived_Type, Ibase);
6647 Set_Directly_Designated_Type
6648 (Derived_Type, Designated_Type (Subt));
6650 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6651 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6652 Set_Size_Info (Derived_Type, Parent_Type);
6653 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6654 Set_Depends_On_Private (Derived_Type,
6655 Has_Private_Component (Derived_Type));
6656 Conditional_Delay (Derived_Type, Subt);
6658 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6659 -- that it is not redundant.
6661 if Null_Exclusion_Present (Type_Definition (N)) then
6662 Set_Can_Never_Be_Null (Derived_Type);
6664 elsif Can_Never_Be_Null (Parent_Type) then
6665 Set_Can_Never_Be_Null (Derived_Type);
6668 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6669 -- the root type for this information.
6671 -- Apply range checks to discriminants for derived record case
6672 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6674 Desig_Type := Designated_Type (Derived_Type);
6676 if Is_Composite_Type (Desig_Type)
6677 and then (not Is_Array_Type (Desig_Type))
6678 and then Has_Discriminants (Desig_Type)
6679 and then Base_Type (Desig_Type) /= Desig_Type
6681 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6682 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6684 Discr := First_Discriminant (Base_Type (Desig_Type));
6685 while Present (Discr_Con_El) loop
6686 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6687 Next_Elmt (Discr_Con_El);
6688 Next_Discriminant (Discr);
6691 end Build_Derived_Access_Type;
6693 ------------------------------
6694 -- Build_Derived_Array_Type --
6695 ------------------------------
6697 procedure Build_Derived_Array_Type
6699 Parent_Type : Entity_Id;
6700 Derived_Type : Entity_Id)
6702 Loc : constant Source_Ptr := Sloc (N);
6703 Tdef : constant Node_Id := Type_Definition (N);
6704 Indic : constant Node_Id := Subtype_Indication (Tdef);
6705 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6706 Implicit_Base : Entity_Id := Empty;
6707 New_Indic : Node_Id;
6709 procedure Make_Implicit_Base;
6710 -- If the parent subtype is constrained, the derived type is a subtype
6711 -- of an implicit base type derived from the parent base.
6713 ------------------------
6714 -- Make_Implicit_Base --
6715 ------------------------
6717 procedure Make_Implicit_Base is
6720 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6722 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6723 Set_Etype (Implicit_Base, Parent_Base);
6725 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6726 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6728 Set_Has_Delayed_Freeze (Implicit_Base, True);
6729 end Make_Implicit_Base;
6731 -- Start of processing for Build_Derived_Array_Type
6734 if not Is_Constrained (Parent_Type) then
6735 if Nkind (Indic) /= N_Subtype_Indication then
6736 Set_Ekind (Derived_Type, E_Array_Type);
6738 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6739 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6741 Set_Has_Delayed_Freeze (Derived_Type, True);
6745 Set_Etype (Derived_Type, Implicit_Base);
6748 Make_Subtype_Declaration (Loc,
6749 Defining_Identifier => Derived_Type,
6750 Subtype_Indication =>
6751 Make_Subtype_Indication (Loc,
6752 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6753 Constraint => Constraint (Indic)));
6755 Rewrite (N, New_Indic);
6760 if Nkind (Indic) /= N_Subtype_Indication then
6763 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6764 Set_Etype (Derived_Type, Implicit_Base);
6765 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6768 Error_Msg_N ("illegal constraint on constrained type", Indic);
6772 -- If parent type is not a derived type itself, and is declared in
6773 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6774 -- the new type's concatenation operator since Derive_Subprograms
6775 -- will not inherit the parent's operator. If the parent type is
6776 -- unconstrained, the operator is of the unconstrained base type.
6778 if Number_Dimensions (Parent_Type) = 1
6779 and then not Is_Limited_Type (Parent_Type)
6780 and then not Is_Derived_Type (Parent_Type)
6781 and then not Is_Package_Or_Generic_Package
6782 (Scope (Base_Type (Parent_Type)))
6784 if not Is_Constrained (Parent_Type)
6785 and then Is_Constrained (Derived_Type)
6787 New_Concatenation_Op (Implicit_Base);
6789 New_Concatenation_Op (Derived_Type);
6792 end Build_Derived_Array_Type;
6794 -----------------------------------
6795 -- Build_Derived_Concurrent_Type --
6796 -----------------------------------
6798 procedure Build_Derived_Concurrent_Type
6800 Parent_Type : Entity_Id;
6801 Derived_Type : Entity_Id)
6803 Loc : constant Source_Ptr := Sloc (N);
6805 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6806 Corr_Decl : Node_Id;
6807 Corr_Decl_Needed : Boolean;
6808 -- If the derived type has fewer discriminants than its parent, the
6809 -- corresponding record is also a derived type, in order to account for
6810 -- the bound discriminants. We create a full type declaration for it in
6813 Constraint_Present : constant Boolean :=
6814 Nkind (Subtype_Indication (Type_Definition (N))) =
6815 N_Subtype_Indication;
6817 D_Constraint : Node_Id;
6818 New_Constraint : Elist_Id := No_Elist;
6819 Old_Disc : Entity_Id;
6820 New_Disc : Entity_Id;
6824 Set_Stored_Constraint (Derived_Type, No_Elist);
6825 Corr_Decl_Needed := False;
6828 if Present (Discriminant_Specifications (N))
6829 and then Constraint_Present
6831 Old_Disc := First_Discriminant (Parent_Type);
6832 New_Disc := First (Discriminant_Specifications (N));
6833 while Present (New_Disc) and then Present (Old_Disc) loop
6834 Next_Discriminant (Old_Disc);
6839 if Present (Old_Disc) and then Expander_Active then
6841 -- The new type has fewer discriminants, so we need to create a new
6842 -- corresponding record, which is derived from the corresponding
6843 -- record of the parent, and has a stored constraint that captures
6844 -- the values of the discriminant constraints. The corresponding
6845 -- record is needed only if expander is active and code generation is
6848 -- The type declaration for the derived corresponding record has the
6849 -- same discriminant part and constraints as the current declaration.
6850 -- Copy the unanalyzed tree to build declaration.
6852 Corr_Decl_Needed := True;
6853 New_N := Copy_Separate_Tree (N);
6856 Make_Full_Type_Declaration (Loc,
6857 Defining_Identifier => Corr_Record,
6858 Discriminant_Specifications =>
6859 Discriminant_Specifications (New_N),
6861 Make_Derived_Type_Definition (Loc,
6862 Subtype_Indication =>
6863 Make_Subtype_Indication (Loc,
6866 (Corresponding_Record_Type (Parent_Type), Loc),
6869 (Subtype_Indication (Type_Definition (New_N))))));
6872 -- Copy Storage_Size and Relative_Deadline variables if task case
6874 if Is_Task_Type (Parent_Type) then
6875 Set_Storage_Size_Variable (Derived_Type,
6876 Storage_Size_Variable (Parent_Type));
6877 Set_Relative_Deadline_Variable (Derived_Type,
6878 Relative_Deadline_Variable (Parent_Type));
6881 if Present (Discriminant_Specifications (N)) then
6882 Push_Scope (Derived_Type);
6883 Check_Or_Process_Discriminants (N, Derived_Type);
6885 if Constraint_Present then
6887 Expand_To_Stored_Constraint
6889 Build_Discriminant_Constraints
6891 Subtype_Indication (Type_Definition (N)), True));
6896 elsif Constraint_Present then
6898 -- Build constrained subtype, copying the constraint, and derive
6899 -- from it to create a derived constrained type.
6902 Loc : constant Source_Ptr := Sloc (N);
6903 Anon : constant Entity_Id :=
6904 Make_Defining_Identifier (Loc,
6905 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6910 Make_Subtype_Declaration (Loc,
6911 Defining_Identifier => Anon,
6912 Subtype_Indication =>
6913 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6914 Insert_Before (N, Decl);
6917 Rewrite (Subtype_Indication (Type_Definition (N)),
6918 New_Occurrence_Of (Anon, Loc));
6919 Set_Analyzed (Derived_Type, False);
6925 -- By default, operations and private data are inherited from parent.
6926 -- However, in the presence of bound discriminants, a new corresponding
6927 -- record will be created, see below.
6929 Set_Has_Discriminants
6930 (Derived_Type, Has_Discriminants (Parent_Type));
6931 Set_Corresponding_Record_Type
6932 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6934 -- Is_Constrained is set according the parent subtype, but is set to
6935 -- False if the derived type is declared with new discriminants.
6939 (Is_Constrained (Parent_Type) or else Constraint_Present)
6940 and then not Present (Discriminant_Specifications (N)));
6942 if Constraint_Present then
6943 if not Has_Discriminants (Parent_Type) then
6944 Error_Msg_N ("untagged parent must have discriminants", N);
6946 elsif Present (Discriminant_Specifications (N)) then
6948 -- Verify that new discriminants are used to constrain old ones
6953 (Constraint (Subtype_Indication (Type_Definition (N)))));
6955 Old_Disc := First_Discriminant (Parent_Type);
6957 while Present (D_Constraint) loop
6958 if Nkind (D_Constraint) /= N_Discriminant_Association then
6960 -- Positional constraint. If it is a reference to a new
6961 -- discriminant, it constrains the corresponding old one.
6963 if Nkind (D_Constraint) = N_Identifier then
6964 New_Disc := First_Discriminant (Derived_Type);
6965 while Present (New_Disc) loop
6966 exit when Chars (New_Disc) = Chars (D_Constraint);
6967 Next_Discriminant (New_Disc);
6970 if Present (New_Disc) then
6971 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6975 Next_Discriminant (Old_Disc);
6977 -- if this is a named constraint, search by name for the old
6978 -- discriminants constrained by the new one.
6980 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6982 -- Find new discriminant with that name
6984 New_Disc := First_Discriminant (Derived_Type);
6985 while Present (New_Disc) loop
6987 Chars (New_Disc) = Chars (Expression (D_Constraint));
6988 Next_Discriminant (New_Disc);
6991 if Present (New_Disc) then
6993 -- Verify that new discriminant renames some discriminant
6994 -- of the parent type, and associate the new discriminant
6995 -- with one or more old ones that it renames.
7001 Selector := First (Selector_Names (D_Constraint));
7002 while Present (Selector) loop
7003 Old_Disc := First_Discriminant (Parent_Type);
7004 while Present (Old_Disc) loop
7005 exit when Chars (Old_Disc) = Chars (Selector);
7006 Next_Discriminant (Old_Disc);
7009 if Present (Old_Disc) then
7010 Set_Corresponding_Discriminant
7011 (New_Disc, Old_Disc);
7020 Next (D_Constraint);
7023 New_Disc := First_Discriminant (Derived_Type);
7024 while Present (New_Disc) loop
7025 if No (Corresponding_Discriminant (New_Disc)) then
7027 ("new discriminant& must constrain old one", N, New_Disc);
7030 Subtypes_Statically_Compatible
7032 Etype (Corresponding_Discriminant (New_Disc)))
7035 ("& not statically compatible with parent discriminant",
7039 Next_Discriminant (New_Disc);
7043 elsif Present (Discriminant_Specifications (N)) then
7045 ("missing discriminant constraint in untagged derivation", N);
7048 -- The entity chain of the derived type includes the new discriminants
7049 -- but shares operations with the parent.
7051 if Present (Discriminant_Specifications (N)) then
7052 Old_Disc := First_Discriminant (Parent_Type);
7053 while Present (Old_Disc) loop
7054 if No (Next_Entity (Old_Disc))
7055 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7058 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7062 Next_Discriminant (Old_Disc);
7066 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7067 if Has_Discriminants (Parent_Type) then
7068 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7069 Set_Discriminant_Constraint (
7070 Derived_Type, Discriminant_Constraint (Parent_Type));
7074 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7076 Set_Has_Completion (Derived_Type);
7078 if Corr_Decl_Needed then
7079 Set_Stored_Constraint (Derived_Type, New_Constraint);
7080 Insert_After (N, Corr_Decl);
7081 Analyze (Corr_Decl);
7082 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7084 end Build_Derived_Concurrent_Type;
7086 ------------------------------------
7087 -- Build_Derived_Enumeration_Type --
7088 ------------------------------------
7090 procedure Build_Derived_Enumeration_Type
7092 Parent_Type : Entity_Id;
7093 Derived_Type : Entity_Id)
7095 Loc : constant Source_Ptr := Sloc (N);
7096 Def : constant Node_Id := Type_Definition (N);
7097 Indic : constant Node_Id := Subtype_Indication (Def);
7098 Implicit_Base : Entity_Id;
7099 Literal : Entity_Id;
7100 New_Lit : Entity_Id;
7101 Literals_List : List_Id;
7102 Type_Decl : Node_Id;
7104 Rang_Expr : Node_Id;
7107 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7108 -- not have explicit literals lists we need to process types derived
7109 -- from them specially. This is handled by Derived_Standard_Character.
7110 -- If the parent type is a generic type, there are no literals either,
7111 -- and we construct the same skeletal representation as for the generic
7114 if Is_Standard_Character_Type (Parent_Type) then
7115 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7117 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7123 if Nkind (Indic) /= N_Subtype_Indication then
7125 Make_Attribute_Reference (Loc,
7126 Attribute_Name => Name_First,
7127 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7128 Set_Etype (Lo, Derived_Type);
7131 Make_Attribute_Reference (Loc,
7132 Attribute_Name => Name_Last,
7133 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7134 Set_Etype (Hi, Derived_Type);
7136 Set_Scalar_Range (Derived_Type,
7142 -- Analyze subtype indication and verify compatibility
7143 -- with parent type.
7145 if Base_Type (Process_Subtype (Indic, N)) /=
7146 Base_Type (Parent_Type)
7149 ("illegal constraint for formal discrete type", N);
7155 -- If a constraint is present, analyze the bounds to catch
7156 -- premature usage of the derived literals.
7158 if Nkind (Indic) = N_Subtype_Indication
7159 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7161 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7162 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7165 -- Introduce an implicit base type for the derived type even if there
7166 -- is no constraint attached to it, since this seems closer to the
7167 -- Ada semantics. Build a full type declaration tree for the derived
7168 -- type using the implicit base type as the defining identifier. The
7169 -- build a subtype declaration tree which applies the constraint (if
7170 -- any) have it replace the derived type declaration.
7172 Literal := First_Literal (Parent_Type);
7173 Literals_List := New_List;
7174 while Present (Literal)
7175 and then Ekind (Literal) = E_Enumeration_Literal
7177 -- Literals of the derived type have the same representation as
7178 -- those of the parent type, but this representation can be
7179 -- overridden by an explicit representation clause. Indicate
7180 -- that there is no explicit representation given yet. These
7181 -- derived literals are implicit operations of the new type,
7182 -- and can be overridden by explicit ones.
7184 if Nkind (Literal) = N_Defining_Character_Literal then
7186 Make_Defining_Character_Literal (Loc, Chars (Literal));
7188 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7191 Set_Ekind (New_Lit, E_Enumeration_Literal);
7192 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7193 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7194 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7195 Set_Alias (New_Lit, Literal);
7196 Set_Is_Known_Valid (New_Lit, True);
7198 Append (New_Lit, Literals_List);
7199 Next_Literal (Literal);
7203 Make_Defining_Identifier (Sloc (Derived_Type),
7204 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7206 -- Indicate the proper nature of the derived type. This must be done
7207 -- before analysis of the literals, to recognize cases when a literal
7208 -- may be hidden by a previous explicit function definition (cf.
7211 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
7212 Set_Etype (Derived_Type, Implicit_Base);
7215 Make_Full_Type_Declaration (Loc,
7216 Defining_Identifier => Implicit_Base,
7217 Discriminant_Specifications => No_List,
7219 Make_Enumeration_Type_Definition (Loc, Literals_List));
7221 Mark_Rewrite_Insertion (Type_Decl);
7222 Insert_Before (N, Type_Decl);
7223 Analyze (Type_Decl);
7225 -- The anonymous base now has a full declaration, but this base
7226 -- is not a first subtype.
7228 Set_Is_First_Subtype (Implicit_Base, False);
7230 -- After the implicit base is analyzed its Etype needs to be changed
7231 -- to reflect the fact that it is derived from the parent type which
7232 -- was ignored during analysis. We also set the size at this point.
7234 Set_Etype (Implicit_Base, Parent_Type);
7236 Set_Size_Info (Implicit_Base, Parent_Type);
7237 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7238 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7240 -- Copy other flags from parent type
7242 Set_Has_Non_Standard_Rep
7243 (Implicit_Base, Has_Non_Standard_Rep
7245 Set_Has_Pragma_Ordered
7246 (Implicit_Base, Has_Pragma_Ordered
7248 Set_Has_Delayed_Freeze (Implicit_Base);
7250 -- Process the subtype indication including a validation check on the
7251 -- constraint, if any. If a constraint is given, its bounds must be
7252 -- implicitly converted to the new type.
7254 if Nkind (Indic) = N_Subtype_Indication then
7256 R : constant Node_Id :=
7257 Range_Expression (Constraint (Indic));
7260 if Nkind (R) = N_Range then
7261 Hi := Build_Scalar_Bound
7262 (High_Bound (R), Parent_Type, Implicit_Base);
7263 Lo := Build_Scalar_Bound
7264 (Low_Bound (R), Parent_Type, Implicit_Base);
7267 -- Constraint is a Range attribute. Replace with explicit
7268 -- mention of the bounds of the prefix, which must be a
7271 Analyze (Prefix (R));
7273 Convert_To (Implicit_Base,
7274 Make_Attribute_Reference (Loc,
7275 Attribute_Name => Name_Last,
7277 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7280 Convert_To (Implicit_Base,
7281 Make_Attribute_Reference (Loc,
7282 Attribute_Name => Name_First,
7284 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7291 (Type_High_Bound (Parent_Type),
7292 Parent_Type, Implicit_Base);
7295 (Type_Low_Bound (Parent_Type),
7296 Parent_Type, Implicit_Base);
7304 -- If we constructed a default range for the case where no range
7305 -- was given, then the expressions in the range must not freeze
7306 -- since they do not correspond to expressions in the source.
7307 -- However, if the type inherits predicates the expressions will
7308 -- be elaborated earlier and must freeze.
7310 if Nkind (Indic) /= N_Subtype_Indication
7311 and then not Has_Predicates (Derived_Type)
7313 Set_Must_Not_Freeze (Lo);
7314 Set_Must_Not_Freeze (Hi);
7315 Set_Must_Not_Freeze (Rang_Expr);
7319 Make_Subtype_Declaration (Loc,
7320 Defining_Identifier => Derived_Type,
7321 Subtype_Indication =>
7322 Make_Subtype_Indication (Loc,
7323 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7325 Make_Range_Constraint (Loc,
7326 Range_Expression => Rang_Expr))));
7330 -- Propagate the aspects from the original type declaration to the
7331 -- declaration of the implicit base.
7333 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7335 -- Apply a range check. Since this range expression doesn't have an
7336 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7339 if Nkind (Indic) = N_Subtype_Indication then
7341 (Range_Expression (Constraint (Indic)), Parent_Type,
7342 Source_Typ => Entity (Subtype_Mark (Indic)));
7345 end Build_Derived_Enumeration_Type;
7347 --------------------------------
7348 -- Build_Derived_Numeric_Type --
7349 --------------------------------
7351 procedure Build_Derived_Numeric_Type
7353 Parent_Type : Entity_Id;
7354 Derived_Type : Entity_Id)
7356 Loc : constant Source_Ptr := Sloc (N);
7357 Tdef : constant Node_Id := Type_Definition (N);
7358 Indic : constant Node_Id := Subtype_Indication (Tdef);
7359 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7360 No_Constraint : constant Boolean := Nkind (Indic) /=
7361 N_Subtype_Indication;
7362 Implicit_Base : Entity_Id;
7368 -- Process the subtype indication including a validation check on
7369 -- the constraint if any.
7371 Discard_Node (Process_Subtype (Indic, N));
7373 -- Introduce an implicit base type for the derived type even if there
7374 -- is no constraint attached to it, since this seems closer to the Ada
7378 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7380 Set_Etype (Implicit_Base, Parent_Base);
7381 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7382 Set_Size_Info (Implicit_Base, Parent_Base);
7383 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7384 Set_Parent (Implicit_Base, Parent (Derived_Type));
7385 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7387 -- Set RM Size for discrete type or decimal fixed-point type
7388 -- Ordinary fixed-point is excluded, why???
7390 if Is_Discrete_Type (Parent_Base)
7391 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7393 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7396 Set_Has_Delayed_Freeze (Implicit_Base);
7398 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7399 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7401 Set_Scalar_Range (Implicit_Base,
7406 if Has_Infinities (Parent_Base) then
7407 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7410 -- The Derived_Type, which is the entity of the declaration, is a
7411 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7412 -- absence of an explicit constraint.
7414 Set_Etype (Derived_Type, Implicit_Base);
7416 -- If we did not have a constraint, then the Ekind is set from the
7417 -- parent type (otherwise Process_Subtype has set the bounds)
7419 if No_Constraint then
7420 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7423 -- If we did not have a range constraint, then set the range from the
7424 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7426 if No_Constraint or else not Has_Range_Constraint (Indic) then
7427 Set_Scalar_Range (Derived_Type,
7429 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7430 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7431 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7433 if Has_Infinities (Parent_Type) then
7434 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7437 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7440 Set_Is_Descendant_Of_Address (Derived_Type,
7441 Is_Descendant_Of_Address (Parent_Type));
7442 Set_Is_Descendant_Of_Address (Implicit_Base,
7443 Is_Descendant_Of_Address (Parent_Type));
7445 -- Set remaining type-specific fields, depending on numeric type
7447 if Is_Modular_Integer_Type (Parent_Type) then
7448 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7450 Set_Non_Binary_Modulus
7451 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7454 (Implicit_Base, Is_Known_Valid (Parent_Base));
7456 elsif Is_Floating_Point_Type (Parent_Type) then
7458 -- Digits of base type is always copied from the digits value of
7459 -- the parent base type, but the digits of the derived type will
7460 -- already have been set if there was a constraint present.
7462 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7463 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7465 if No_Constraint then
7466 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7469 elsif Is_Fixed_Point_Type (Parent_Type) then
7471 -- Small of base type and derived type are always copied from the
7472 -- parent base type, since smalls never change. The delta of the
7473 -- base type is also copied from the parent base type. However the
7474 -- delta of the derived type will have been set already if a
7475 -- constraint was present.
7477 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7478 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7479 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7481 if No_Constraint then
7482 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7485 -- The scale and machine radix in the decimal case are always
7486 -- copied from the parent base type.
7488 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7489 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7490 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7492 Set_Machine_Radix_10
7493 (Derived_Type, Machine_Radix_10 (Parent_Base));
7494 Set_Machine_Radix_10
7495 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7497 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7499 if No_Constraint then
7500 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7503 -- the analysis of the subtype_indication sets the
7504 -- digits value of the derived type.
7511 if Is_Integer_Type (Parent_Type) then
7512 Set_Has_Shift_Operator
7513 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7516 -- The type of the bounds is that of the parent type, and they
7517 -- must be converted to the derived type.
7519 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7521 -- The implicit_base should be frozen when the derived type is frozen,
7522 -- but note that it is used in the conversions of the bounds. For fixed
7523 -- types we delay the determination of the bounds until the proper
7524 -- freezing point. For other numeric types this is rejected by GCC, for
7525 -- reasons that are currently unclear (???), so we choose to freeze the
7526 -- implicit base now. In the case of integers and floating point types
7527 -- this is harmless because subsequent representation clauses cannot
7528 -- affect anything, but it is still baffling that we cannot use the
7529 -- same mechanism for all derived numeric types.
7531 -- There is a further complication: actually some representation
7532 -- clauses can affect the implicit base type. For example, attribute
7533 -- definition clauses for stream-oriented attributes need to set the
7534 -- corresponding TSS entries on the base type, and this normally
7535 -- cannot be done after the base type is frozen, so the circuitry in
7536 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7537 -- and not use Set_TSS in this case.
7539 -- There are also consequences for the case of delayed representation
7540 -- aspects for some cases. For example, a Size aspect is delayed and
7541 -- should not be evaluated to the freeze point. This early freezing
7542 -- means that the size attribute evaluation happens too early???
7544 if Is_Fixed_Point_Type (Parent_Type) then
7545 Conditional_Delay (Implicit_Base, Parent_Type);
7547 Freeze_Before (N, Implicit_Base);
7549 end Build_Derived_Numeric_Type;
7551 --------------------------------
7552 -- Build_Derived_Private_Type --
7553 --------------------------------
7555 procedure Build_Derived_Private_Type
7557 Parent_Type : Entity_Id;
7558 Derived_Type : Entity_Id;
7559 Is_Completion : Boolean;
7560 Derive_Subps : Boolean := True)
7562 Loc : constant Source_Ptr := Sloc (N);
7563 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7564 Par_Scope : constant Entity_Id := Scope (Par_Base);
7565 Full_N : constant Node_Id := New_Copy_Tree (N);
7566 Full_Der : Entity_Id := New_Copy (Derived_Type);
7569 procedure Build_Full_Derivation;
7570 -- Build full derivation, i.e. derive from the full view
7572 procedure Copy_And_Build;
7573 -- Copy derived type declaration, replace parent with its full view,
7574 -- and build derivation
7576 ---------------------------
7577 -- Build_Full_Derivation --
7578 ---------------------------
7580 procedure Build_Full_Derivation is
7582 -- If parent scope is not open, install the declarations
7584 if not In_Open_Scopes (Par_Scope) then
7585 Install_Private_Declarations (Par_Scope);
7586 Install_Visible_Declarations (Par_Scope);
7588 Uninstall_Declarations (Par_Scope);
7590 -- If parent scope is open and in another unit, and parent has a
7591 -- completion, then the derivation is taking place in the visible
7592 -- part of a child unit. In that case retrieve the full view of
7593 -- the parent momentarily.
7595 elsif not In_Same_Source_Unit (N, Parent_Type) then
7596 Full_P := Full_View (Parent_Type);
7597 Exchange_Declarations (Parent_Type);
7599 Exchange_Declarations (Full_P);
7601 -- Otherwise it is a local derivation
7606 end Build_Full_Derivation;
7608 --------------------
7609 -- Copy_And_Build --
7610 --------------------
7612 procedure Copy_And_Build is
7613 Full_Parent : Entity_Id := Parent_Type;
7616 -- If the parent is itself derived from another private type,
7617 -- installing the private declarations has not affected its
7618 -- privacy status, so use its own full view explicitly.
7620 if Is_Private_Type (Full_Parent)
7621 and then Present (Full_View (Full_Parent))
7623 Full_Parent := Full_View (Full_Parent);
7626 -- And its underlying full view if necessary
7628 if Is_Private_Type (Full_Parent)
7629 and then Present (Underlying_Full_View (Full_Parent))
7631 Full_Parent := Underlying_Full_View (Full_Parent);
7634 -- For record, access and most enumeration types, derivation from
7635 -- the full view requires a fully-fledged declaration. In the other
7636 -- cases, just use an itype.
7638 if Ekind (Full_Parent) in Record_Kind
7639 or else Ekind (Full_Parent) in Access_Kind
7641 (Ekind (Full_Parent) in Enumeration_Kind
7642 and then not Is_Standard_Character_Type (Full_Parent)
7643 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7645 -- Copy and adjust declaration to provide a completion for what
7646 -- is originally a private declaration. Indicate that full view
7647 -- is internally generated.
7649 Set_Comes_From_Source (Full_N, False);
7650 Set_Comes_From_Source (Full_Der, False);
7651 Set_Parent (Full_Der, Full_N);
7652 Set_Defining_Identifier (Full_N, Full_Der);
7654 -- If there are no constraints, adjust the subtype mark
7656 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7657 N_Subtype_Indication
7659 Set_Subtype_Indication
7660 (Type_Definition (Full_N),
7661 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7664 Insert_After (N, Full_N);
7666 -- Build full view of derived type from full view of parent which
7667 -- is now installed. Subprograms have been derived on the partial
7668 -- view, the completion does not derive them anew.
7670 if Ekind (Full_Parent) in Record_Kind then
7672 -- If parent type is tagged, the completion inherits the proper
7673 -- primitive operations.
7675 if Is_Tagged_Type (Parent_Type) then
7676 Build_Derived_Record_Type
7677 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7679 Build_Derived_Record_Type
7680 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7685 (Full_N, Full_Parent, Full_Der,
7686 Is_Completion => False, Derive_Subps => False);
7689 -- The full declaration has been introduced into the tree and
7690 -- processed in the step above. It should not be analyzed again
7691 -- (when encountered later in the current list of declarations)
7692 -- to prevent spurious name conflicts. The full entity remains
7695 Set_Analyzed (Full_N);
7699 Make_Defining_Identifier (Sloc (Derived_Type),
7700 Chars => Chars (Derived_Type));
7701 Set_Is_Itype (Full_Der);
7702 Set_Associated_Node_For_Itype (Full_Der, N);
7703 Set_Parent (Full_Der, N);
7705 (N, Full_Parent, Full_Der,
7706 Is_Completion => False, Derive_Subps => False);
7709 Set_Has_Private_Declaration (Full_Der);
7710 Set_Has_Private_Declaration (Derived_Type);
7712 Set_Scope (Full_Der, Scope (Derived_Type));
7713 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7714 Set_Has_Size_Clause (Full_Der, False);
7715 Set_Has_Alignment_Clause (Full_Der, False);
7716 Set_Has_Delayed_Freeze (Full_Der);
7717 Set_Is_Frozen (Full_Der, False);
7718 Set_Freeze_Node (Full_Der, Empty);
7719 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7720 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7722 -- The convention on the base type may be set in the private part
7723 -- and not propagated to the subtype until later, so we obtain the
7724 -- convention from the base type of the parent.
7726 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7729 -- Start of processing for Build_Derived_Private_Type
7732 if Is_Tagged_Type (Parent_Type) then
7733 Full_P := Full_View (Parent_Type);
7735 -- A type extension of a type with unknown discriminants is an
7736 -- indefinite type that the back-end cannot handle directly.
7737 -- We treat it as a private type, and build a completion that is
7738 -- derived from the full view of the parent, and hopefully has
7739 -- known discriminants.
7741 -- If the full view of the parent type has an underlying record view,
7742 -- use it to generate the underlying record view of this derived type
7743 -- (required for chains of derivations with unknown discriminants).
7745 -- Minor optimization: we avoid the generation of useless underlying
7746 -- record view entities if the private type declaration has unknown
7747 -- discriminants but its corresponding full view has no
7750 if Has_Unknown_Discriminants (Parent_Type)
7751 and then Present (Full_P)
7752 and then (Has_Discriminants (Full_P)
7753 or else Present (Underlying_Record_View (Full_P)))
7754 and then not In_Open_Scopes (Par_Scope)
7755 and then Expander_Active
7758 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7759 New_Ext : constant Node_Id :=
7761 (Record_Extension_Part (Type_Definition (N)));
7765 Build_Derived_Record_Type
7766 (N, Parent_Type, Derived_Type, Derive_Subps);
7768 -- Build anonymous completion, as a derivation from the full
7769 -- view of the parent. This is not a completion in the usual
7770 -- sense, because the current type is not private.
7773 Make_Full_Type_Declaration (Loc,
7774 Defining_Identifier => Full_Der,
7776 Make_Derived_Type_Definition (Loc,
7777 Subtype_Indication =>
7779 (Subtype_Indication (Type_Definition (N))),
7780 Record_Extension_Part => New_Ext));
7782 -- If the parent type has an underlying record view, use it
7783 -- here to build the new underlying record view.
7785 if Present (Underlying_Record_View (Full_P)) then
7787 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7789 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7790 Underlying_Record_View (Full_P));
7793 Install_Private_Declarations (Par_Scope);
7794 Install_Visible_Declarations (Par_Scope);
7795 Insert_Before (N, Decl);
7797 -- Mark entity as an underlying record view before analysis,
7798 -- to avoid generating the list of its primitive operations
7799 -- (which is not really required for this entity) and thus
7800 -- prevent spurious errors associated with missing overriding
7801 -- of abstract primitives (overridden only for Derived_Type).
7803 Set_Ekind (Full_Der, E_Record_Type);
7804 Set_Is_Underlying_Record_View (Full_Der);
7805 Set_Default_SSO (Full_Der);
7806 Set_No_Reordering (Full_Der, No_Component_Reordering);
7810 pragma Assert (Has_Discriminants (Full_Der)
7811 and then not Has_Unknown_Discriminants (Full_Der));
7813 Uninstall_Declarations (Par_Scope);
7815 -- Freeze the underlying record view, to prevent generation of
7816 -- useless dispatching information, which is simply shared with
7817 -- the real derived type.
7819 Set_Is_Frozen (Full_Der);
7821 -- If the derived type has access discriminants, create
7822 -- references to their anonymous types now, to prevent
7823 -- back-end problems when their first use is in generated
7824 -- bodies of primitives.
7830 E := First_Entity (Full_Der);
7832 while Present (E) loop
7833 if Ekind (E) = E_Discriminant
7834 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7836 Build_Itype_Reference (Etype (E), Decl);
7843 -- Set up links between real entity and underlying record view
7845 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7846 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7849 -- If discriminants are known, build derived record
7852 Build_Derived_Record_Type
7853 (N, Parent_Type, Derived_Type, Derive_Subps);
7858 elsif Has_Discriminants (Parent_Type) then
7860 -- Build partial view of derived type from partial view of parent.
7861 -- This must be done before building the full derivation because the
7862 -- second derivation will modify the discriminants of the first and
7863 -- the discriminants are chained with the rest of the components in
7864 -- the full derivation.
7866 Build_Derived_Record_Type
7867 (N, Parent_Type, Derived_Type, Derive_Subps);
7869 -- Build the full derivation if this is not the anonymous derived
7870 -- base type created by Build_Derived_Record_Type in the constrained
7871 -- case (see point 5. of its head comment) since we build it for the
7872 -- derived subtype. And skip it for synchronized types altogether, as
7873 -- gigi does not use these types directly.
7875 if Present (Full_View (Parent_Type))
7876 and then not Is_Itype (Derived_Type)
7877 and then not Is_Concurrent_Type (Full_View (Parent_Type))
7880 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7882 Last_Discr : Entity_Id;
7885 -- If this is not a completion, construct the implicit full
7886 -- view by deriving from the full view of the parent type.
7887 -- But if this is a completion, the derived private type
7888 -- being built is a full view and the full derivation can
7889 -- only be its underlying full view.
7891 Build_Full_Derivation;
7893 if not Is_Completion then
7894 Set_Full_View (Derived_Type, Full_Der);
7896 Set_Underlying_Full_View (Derived_Type, Full_Der);
7897 Set_Is_Underlying_Full_View (Full_Der);
7900 if not Is_Base_Type (Derived_Type) then
7901 Set_Full_View (Der_Base, Base_Type (Full_Der));
7904 -- Copy the discriminant list from full view to the partial
7905 -- view (base type and its subtype). Gigi requires that the
7906 -- partial and full views have the same discriminants.
7908 -- Note that since the partial view points to discriminants
7909 -- in the full view, their scope will be that of the full
7910 -- view. This might cause some front end problems and need
7913 Discr := First_Discriminant (Base_Type (Full_Der));
7914 Set_First_Entity (Der_Base, Discr);
7917 Last_Discr := Discr;
7918 Next_Discriminant (Discr);
7919 exit when No (Discr);
7922 Set_Last_Entity (Der_Base, Last_Discr);
7923 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7924 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7928 elsif Present (Full_View (Parent_Type))
7929 and then Has_Discriminants (Full_View (Parent_Type))
7931 if Has_Unknown_Discriminants (Parent_Type)
7932 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7933 N_Subtype_Indication
7936 ("cannot constrain type with unknown discriminants",
7937 Subtype_Indication (Type_Definition (N)));
7941 -- If this is not a completion, construct the implicit full view by
7942 -- deriving from the full view of the parent type. But if this is a
7943 -- completion, the derived private type being built is a full view
7944 -- and the full derivation can only be its underlying full view.
7946 Build_Full_Derivation;
7948 if not Is_Completion then
7949 Set_Full_View (Derived_Type, Full_Der);
7951 Set_Underlying_Full_View (Derived_Type, Full_Der);
7952 Set_Is_Underlying_Full_View (Full_Der);
7955 -- In any case, the primitive operations are inherited from the
7956 -- parent type, not from the internal full view.
7958 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7960 if Derive_Subps then
7961 Derive_Subprograms (Parent_Type, Derived_Type);
7964 Set_Stored_Constraint (Derived_Type, No_Elist);
7966 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7969 -- Untagged type, No discriminants on either view
7971 if Nkind (Subtype_Indication (Type_Definition (N))) =
7972 N_Subtype_Indication
7975 ("illegal constraint on type without discriminants", N);
7978 if Present (Discriminant_Specifications (N))
7979 and then Present (Full_View (Parent_Type))
7980 and then not Is_Tagged_Type (Full_View (Parent_Type))
7982 Error_Msg_N ("cannot add discriminants to untagged type", N);
7985 Set_Stored_Constraint (Derived_Type, No_Elist);
7986 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7988 Set_Is_Controlled_Active
7989 (Derived_Type, Is_Controlled_Active (Parent_Type));
7991 Set_Disable_Controlled
7992 (Derived_Type, Disable_Controlled (Parent_Type));
7994 Set_Has_Controlled_Component
7995 (Derived_Type, Has_Controlled_Component (Parent_Type));
7997 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7999 if not Is_Controlled (Parent_Type) then
8000 Set_Finalize_Storage_Only
8001 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8004 -- If this is not a completion, construct the implicit full view by
8005 -- deriving from the full view of the parent type.
8007 -- ??? If the parent is untagged private and its completion is
8008 -- tagged, this mechanism will not work because we cannot derive from
8009 -- the tagged full view unless we have an extension.
8011 if Present (Full_View (Parent_Type))
8012 and then not Is_Tagged_Type (Full_View (Parent_Type))
8013 and then not Is_Completion
8015 Build_Full_Derivation;
8016 Set_Full_View (Derived_Type, Full_Der);
8020 Set_Has_Unknown_Discriminants (Derived_Type,
8021 Has_Unknown_Discriminants (Parent_Type));
8023 if Is_Private_Type (Derived_Type) then
8024 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8027 -- If the parent base type is in scope, add the derived type to its
8028 -- list of private dependents, because its full view may become
8029 -- visible subsequently (in a nested private part, a body, or in a
8030 -- further child unit).
8032 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8033 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8035 -- Check for unusual case where a type completed by a private
8036 -- derivation occurs within a package nested in a child unit, and
8037 -- the parent is declared in an ancestor.
8039 if Is_Child_Unit (Scope (Current_Scope))
8040 and then Is_Completion
8041 and then In_Private_Part (Current_Scope)
8042 and then Scope (Parent_Type) /= Current_Scope
8044 -- Note that if the parent has a completion in the private part,
8045 -- (which is itself a derivation from some other private type)
8046 -- it is that completion that is visible, there is no full view
8047 -- available, and no special processing is needed.
8049 and then Present (Full_View (Parent_Type))
8051 -- In this case, the full view of the parent type will become
8052 -- visible in the body of the enclosing child, and only then will
8053 -- the current type be possibly non-private. Build an underlying
8054 -- full view that will be installed when the enclosing child body
8057 if Present (Underlying_Full_View (Derived_Type)) then
8058 Full_Der := Underlying_Full_View (Derived_Type);
8060 Build_Full_Derivation;
8061 Set_Underlying_Full_View (Derived_Type, Full_Der);
8062 Set_Is_Underlying_Full_View (Full_Der);
8065 -- The full view will be used to swap entities on entry/exit to
8066 -- the body, and must appear in the entity list for the package.
8068 Append_Entity (Full_Der, Scope (Derived_Type));
8071 end Build_Derived_Private_Type;
8073 -------------------------------
8074 -- Build_Derived_Record_Type --
8075 -------------------------------
8079 -- Ideally we would like to use the same model of type derivation for
8080 -- tagged and untagged record types. Unfortunately this is not quite
8081 -- possible because the semantics of representation clauses is different
8082 -- for tagged and untagged records under inheritance. Consider the
8085 -- type R (...) is [tagged] record ... end record;
8086 -- type T (...) is new R (...) [with ...];
8088 -- The representation clauses for T can specify a completely different
8089 -- record layout from R's. Hence the same component can be placed in two
8090 -- very different positions in objects of type T and R. If R and T are
8091 -- tagged types, representation clauses for T can only specify the layout
8092 -- of non inherited components, thus components that are common in R and T
8093 -- have the same position in objects of type R and T.
8095 -- This has two implications. The first is that the entire tree for R's
8096 -- declaration needs to be copied for T in the untagged case, so that T
8097 -- can be viewed as a record type of its own with its own representation
8098 -- clauses. The second implication is the way we handle discriminants.
8099 -- Specifically, in the untagged case we need a way to communicate to Gigi
8100 -- what are the real discriminants in the record, while for the semantics
8101 -- we need to consider those introduced by the user to rename the
8102 -- discriminants in the parent type. This is handled by introducing the
8103 -- notion of stored discriminants. See below for more.
8105 -- Fortunately the way regular components are inherited can be handled in
8106 -- the same way in tagged and untagged types.
8108 -- To complicate things a bit more the private view of a private extension
8109 -- cannot be handled in the same way as the full view (for one thing the
8110 -- semantic rules are somewhat different). We will explain what differs
8113 -- 2. DISCRIMINANTS UNDER INHERITANCE
8115 -- The semantic rules governing the discriminants of derived types are
8118 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8119 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8121 -- If parent type has discriminants, then the discriminants that are
8122 -- declared in the derived type are [3.4 (11)]:
8124 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8127 -- o Otherwise, each discriminant of the parent type (implicitly declared
8128 -- in the same order with the same specifications). In this case, the
8129 -- discriminants are said to be "inherited", or if unknown in the parent
8130 -- are also unknown in the derived type.
8132 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8134 -- o The parent subtype must be constrained;
8136 -- o If the parent type is not a tagged type, then each discriminant of
8137 -- the derived type must be used in the constraint defining a parent
8138 -- subtype. [Implementation note: This ensures that the new discriminant
8139 -- can share storage with an existing discriminant.]
8141 -- For the derived type each discriminant of the parent type is either
8142 -- inherited, constrained to equal some new discriminant of the derived
8143 -- type, or constrained to the value of an expression.
8145 -- When inherited or constrained to equal some new discriminant, the
8146 -- parent discriminant and the discriminant of the derived type are said
8149 -- If a discriminant of the parent type is constrained to a specific value
8150 -- in the derived type definition, then the discriminant is said to be
8151 -- "specified" by that derived type definition.
8153 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8155 -- We have spoken about stored discriminants in point 1 (introduction)
8156 -- above. There are two sorts of stored discriminants: implicit and
8157 -- explicit. As long as the derived type inherits the same discriminants as
8158 -- the root record type, stored discriminants are the same as regular
8159 -- discriminants, and are said to be implicit. However, if any discriminant
8160 -- in the root type was renamed in the derived type, then the derived
8161 -- type will contain explicit stored discriminants. Explicit stored
8162 -- discriminants are discriminants in addition to the semantically visible
8163 -- discriminants defined for the derived type. Stored discriminants are
8164 -- used by Gigi to figure out what are the physical discriminants in
8165 -- objects of the derived type (see precise definition in einfo.ads).
8166 -- As an example, consider the following:
8168 -- type R (D1, D2, D3 : Int) is record ... end record;
8169 -- type T1 is new R;
8170 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8171 -- type T3 is new T2;
8172 -- type T4 (Y : Int) is new T3 (Y, 99);
8174 -- The following table summarizes the discriminants and stored
8175 -- discriminants in R and T1 through T4:
8177 -- Type Discrim Stored Discrim Comment
8178 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8179 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8180 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8181 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8182 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8184 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8185 -- find the corresponding discriminant in the parent type, while
8186 -- Original_Record_Component (abbreviated ORC below) the actual physical
8187 -- component that is renamed. Finally the field Is_Completely_Hidden
8188 -- (abbreviated ICH below) is set for all explicit stored discriminants
8189 -- (see einfo.ads for more info). For the above example this gives:
8191 -- Discrim CD ORC ICH
8192 -- ^^^^^^^ ^^ ^^^ ^^^
8193 -- D1 in R empty itself no
8194 -- D2 in R empty itself no
8195 -- D3 in R empty itself no
8197 -- D1 in T1 D1 in R itself no
8198 -- D2 in T1 D2 in R itself no
8199 -- D3 in T1 D3 in R itself no
8201 -- X1 in T2 D3 in T1 D3 in T2 no
8202 -- X2 in T2 D1 in T1 D1 in T2 no
8203 -- D1 in T2 empty itself yes
8204 -- D2 in T2 empty itself yes
8205 -- D3 in T2 empty itself yes
8207 -- X1 in T3 X1 in T2 D3 in T3 no
8208 -- X2 in T3 X2 in T2 D1 in T3 no
8209 -- D1 in T3 empty itself yes
8210 -- D2 in T3 empty itself yes
8211 -- D3 in T3 empty itself yes
8213 -- Y in T4 X1 in T3 D3 in T4 no
8214 -- D1 in T4 empty itself yes
8215 -- D2 in T4 empty itself yes
8216 -- D3 in T4 empty itself yes
8218 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8220 -- Type derivation for tagged types is fairly straightforward. If no
8221 -- discriminants are specified by the derived type, these are inherited
8222 -- from the parent. No explicit stored discriminants are ever necessary.
8223 -- The only manipulation that is done to the tree is that of adding a
8224 -- _parent field with parent type and constrained to the same constraint
8225 -- specified for the parent in the derived type definition. For instance:
8227 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8228 -- type T1 is new R with null record;
8229 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8231 -- are changed into:
8233 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8234 -- _parent : R (D1, D2, D3);
8237 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8238 -- _parent : T1 (X2, 88, X1);
8241 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8242 -- ORC and ICH fields are:
8244 -- Discrim CD ORC ICH
8245 -- ^^^^^^^ ^^ ^^^ ^^^
8246 -- D1 in R empty itself no
8247 -- D2 in R empty itself no
8248 -- D3 in R empty itself no
8250 -- D1 in T1 D1 in R D1 in R no
8251 -- D2 in T1 D2 in R D2 in R no
8252 -- D3 in T1 D3 in R D3 in R no
8254 -- X1 in T2 D3 in T1 D3 in R no
8255 -- X2 in T2 D1 in T1 D1 in R no
8257 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8259 -- Regardless of whether we dealing with a tagged or untagged type
8260 -- we will transform all derived type declarations of the form
8262 -- type T is new R (...) [with ...];
8264 -- subtype S is R (...);
8265 -- type T is new S [with ...];
8267 -- type BT is new R [with ...];
8268 -- subtype T is BT (...);
8270 -- That is, the base derived type is constrained only if it has no
8271 -- discriminants. The reason for doing this is that GNAT's semantic model
8272 -- assumes that a base type with discriminants is unconstrained.
8274 -- Note that, strictly speaking, the above transformation is not always
8275 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8277 -- procedure B34011A is
8278 -- type REC (D : integer := 0) is record
8283 -- type T6 is new Rec;
8284 -- function F return T6;
8289 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8292 -- The definition of Q6.U is illegal. However transforming Q6.U into
8294 -- type BaseU is new T6;
8295 -- subtype U is BaseU (Q6.F.I)
8297 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8298 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8299 -- the transformation described above.
8301 -- There is another instance where the above transformation is incorrect.
8305 -- type Base (D : Integer) is tagged null record;
8306 -- procedure P (X : Base);
8308 -- type Der is new Base (2) with null record;
8309 -- procedure P (X : Der);
8312 -- Then the above transformation turns this into
8314 -- type Der_Base is new Base with null record;
8315 -- -- procedure P (X : Base) is implicitly inherited here
8316 -- -- as procedure P (X : Der_Base).
8318 -- subtype Der is Der_Base (2);
8319 -- procedure P (X : Der);
8320 -- -- The overriding of P (X : Der_Base) is illegal since we
8321 -- -- have a parameter conformance problem.
8323 -- To get around this problem, after having semantically processed Der_Base
8324 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8325 -- Discriminant_Constraint from Der so that when parameter conformance is
8326 -- checked when P is overridden, no semantic errors are flagged.
8328 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8330 -- Regardless of whether we are dealing with a tagged or untagged type
8331 -- we will transform all derived type declarations of the form
8333 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8334 -- type T is new R [with ...];
8336 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8338 -- The reason for such transformation is that it allows us to implement a
8339 -- very clean form of component inheritance as explained below.
8341 -- Note that this transformation is not achieved by direct tree rewriting
8342 -- and manipulation, but rather by redoing the semantic actions that the
8343 -- above transformation will entail. This is done directly in routine
8344 -- Inherit_Components.
8346 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8348 -- In both tagged and untagged derived types, regular non discriminant
8349 -- components are inherited in the derived type from the parent type. In
8350 -- the absence of discriminants component, inheritance is straightforward
8351 -- as components can simply be copied from the parent.
8353 -- If the parent has discriminants, inheriting components constrained with
8354 -- these discriminants requires caution. Consider the following example:
8356 -- type R (D1, D2 : Positive) is [tagged] record
8357 -- S : String (D1 .. D2);
8360 -- type T1 is new R [with null record];
8361 -- type T2 (X : positive) is new R (1, X) [with null record];
8363 -- As explained in 6. above, T1 is rewritten as
8364 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8365 -- which makes the treatment for T1 and T2 identical.
8367 -- What we want when inheriting S, is that references to D1 and D2 in R are
8368 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8369 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8370 -- with either discriminant references in the derived type or expressions.
8371 -- This replacement is achieved as follows: before inheriting R's
8372 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8373 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8374 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8375 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8376 -- by String (1 .. X).
8378 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8380 -- We explain here the rules governing private type extensions relevant to
8381 -- type derivation. These rules are explained on the following example:
8383 -- type D [(...)] is new A [(...)] with private; <-- partial view
8384 -- type D [(...)] is new P [(...)] with null record; <-- full view
8386 -- Type A is called the ancestor subtype of the private extension.
8387 -- Type P is the parent type of the full view of the private extension. It
8388 -- must be A or a type derived from A.
8390 -- The rules concerning the discriminants of private type extensions are
8393 -- o If a private extension inherits known discriminants from the ancestor
8394 -- subtype, then the full view must also inherit its discriminants from
8395 -- the ancestor subtype and the parent subtype of the full view must be
8396 -- constrained if and only if the ancestor subtype is constrained.
8398 -- o If a partial view has unknown discriminants, then the full view may
8399 -- define a definite or an indefinite subtype, with or without
8402 -- o If a partial view has neither known nor unknown discriminants, then
8403 -- the full view must define a definite subtype.
8405 -- o If the ancestor subtype of a private extension has constrained
8406 -- discriminants, then the parent subtype of the full view must impose a
8407 -- statically matching constraint on those discriminants.
8409 -- This means that only the following forms of private extensions are
8412 -- type D is new A with private; <-- partial view
8413 -- type D is new P with null record; <-- full view
8415 -- If A has no discriminants than P has no discriminants, otherwise P must
8416 -- inherit A's discriminants.
8418 -- type D is new A (...) with private; <-- partial view
8419 -- type D is new P (:::) with null record; <-- full view
8421 -- P must inherit A's discriminants and (...) and (:::) must statically
8424 -- subtype A is R (...);
8425 -- type D is new A with private; <-- partial view
8426 -- type D is new P with null record; <-- full view
8428 -- P must have inherited R's discriminants and must be derived from A or
8429 -- any of its subtypes.
8431 -- type D (..) is new A with private; <-- partial view
8432 -- type D (..) is new P [(:::)] with null record; <-- full view
8434 -- No specific constraints on P's discriminants or constraint (:::).
8435 -- Note that A can be unconstrained, but the parent subtype P must either
8436 -- be constrained or (:::) must be present.
8438 -- type D (..) is new A [(...)] with private; <-- partial view
8439 -- type D (..) is new P [(:::)] with null record; <-- full view
8441 -- P's constraints on A's discriminants must statically match those
8442 -- imposed by (...).
8444 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8446 -- The full view of a private extension is handled exactly as described
8447 -- above. The model chose for the private view of a private extension is
8448 -- the same for what concerns discriminants (i.e. they receive the same
8449 -- treatment as in the tagged case). However, the private view of the
8450 -- private extension always inherits the components of the parent base,
8451 -- without replacing any discriminant reference. Strictly speaking this is
8452 -- incorrect. However, Gigi never uses this view to generate code so this
8453 -- is a purely semantic issue. In theory, a set of transformations similar
8454 -- to those given in 5. and 6. above could be applied to private views of
8455 -- private extensions to have the same model of component inheritance as
8456 -- for non private extensions. However, this is not done because it would
8457 -- further complicate private type processing. Semantically speaking, this
8458 -- leaves us in an uncomfortable situation. As an example consider:
8461 -- type R (D : integer) is tagged record
8462 -- S : String (1 .. D);
8464 -- procedure P (X : R);
8465 -- type T is new R (1) with private;
8467 -- type T is new R (1) with null record;
8470 -- This is transformed into:
8473 -- type R (D : integer) is tagged record
8474 -- S : String (1 .. D);
8476 -- procedure P (X : R);
8477 -- type T is new R (1) with private;
8479 -- type BaseT is new R with null record;
8480 -- subtype T is BaseT (1);
8483 -- (strictly speaking the above is incorrect Ada)
8485 -- From the semantic standpoint the private view of private extension T
8486 -- should be flagged as constrained since one can clearly have
8490 -- in a unit withing Pack. However, when deriving subprograms for the
8491 -- private view of private extension T, T must be seen as unconstrained
8492 -- since T has discriminants (this is a constraint of the current
8493 -- subprogram derivation model). Thus, when processing the private view of
8494 -- a private extension such as T, we first mark T as unconstrained, we
8495 -- process it, we perform program derivation and just before returning from
8496 -- Build_Derived_Record_Type we mark T as constrained.
8498 -- ??? Are there are other uncomfortable cases that we will have to
8501 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8503 -- Types that are derived from a visible record type and have a private
8504 -- extension present other peculiarities. They behave mostly like private
8505 -- types, but if they have primitive operations defined, these will not
8506 -- have the proper signatures for further inheritance, because other
8507 -- primitive operations will use the implicit base that we define for
8508 -- private derivations below. This affect subprogram inheritance (see
8509 -- Derive_Subprograms for details). We also derive the implicit base from
8510 -- the base type of the full view, so that the implicit base is a record
8511 -- type and not another private type, This avoids infinite loops.
8513 procedure Build_Derived_Record_Type
8515 Parent_Type : Entity_Id;
8516 Derived_Type : Entity_Id;
8517 Derive_Subps : Boolean := True)
8519 Discriminant_Specs : constant Boolean :=
8520 Present (Discriminant_Specifications (N));
8521 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8522 Loc : constant Source_Ptr := Sloc (N);
8523 Private_Extension : constant Boolean :=
8524 Nkind (N) = N_Private_Extension_Declaration;
8525 Assoc_List : Elist_Id;
8526 Constraint_Present : Boolean;
8528 Discrim : Entity_Id;
8530 Inherit_Discrims : Boolean := False;
8531 Last_Discrim : Entity_Id;
8532 New_Base : Entity_Id;
8534 New_Discrs : Elist_Id;
8535 New_Indic : Node_Id;
8536 Parent_Base : Entity_Id;
8537 Save_Etype : Entity_Id;
8538 Save_Discr_Constr : Elist_Id;
8539 Save_Next_Entity : Entity_Id;
8542 Discs : Elist_Id := New_Elmt_List;
8543 -- An empty Discs list means that there were no constraints in the
8544 -- subtype indication or that there was an error processing it.
8547 if Ekind (Parent_Type) = E_Record_Type_With_Private
8548 and then Present (Full_View (Parent_Type))
8549 and then Has_Discriminants (Parent_Type)
8551 Parent_Base := Base_Type (Full_View (Parent_Type));
8553 Parent_Base := Base_Type (Parent_Type);
8556 -- AI05-0115: if this is a derivation from a private type in some
8557 -- other scope that may lead to invisible components for the derived
8558 -- type, mark it accordingly.
8560 if Is_Private_Type (Parent_Type) then
8561 if Scope (Parent_Base) = Scope (Derived_Type) then
8564 elsif In_Open_Scopes (Scope (Parent_Base))
8565 and then In_Private_Part (Scope (Parent_Base))
8570 Set_Has_Private_Ancestor (Derived_Type);
8574 Set_Has_Private_Ancestor
8575 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8578 -- Before we start the previously documented transformations, here is
8579 -- little fix for size and alignment of tagged types. Normally when we
8580 -- derive type D from type P, we copy the size and alignment of P as the
8581 -- default for D, and in the absence of explicit representation clauses
8582 -- for D, the size and alignment are indeed the same as the parent.
8584 -- But this is wrong for tagged types, since fields may be added, and
8585 -- the default size may need to be larger, and the default alignment may
8586 -- need to be larger.
8588 -- We therefore reset the size and alignment fields in the tagged case.
8589 -- Note that the size and alignment will in any case be at least as
8590 -- large as the parent type (since the derived type has a copy of the
8591 -- parent type in the _parent field)
8593 -- The type is also marked as being tagged here, which is needed when
8594 -- processing components with a self-referential anonymous access type
8595 -- in the call to Check_Anonymous_Access_Components below. Note that
8596 -- this flag is also set later on for completeness.
8599 Set_Is_Tagged_Type (Derived_Type);
8600 Init_Size_Align (Derived_Type);
8603 -- STEP 0a: figure out what kind of derived type declaration we have
8605 if Private_Extension then
8607 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8608 Set_Default_SSO (Derived_Type);
8609 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8612 Type_Def := Type_Definition (N);
8614 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8615 -- Parent_Base can be a private type or private extension. However,
8616 -- for tagged types with an extension the newly added fields are
8617 -- visible and hence the Derived_Type is always an E_Record_Type.
8618 -- (except that the parent may have its own private fields).
8619 -- For untagged types we preserve the Ekind of the Parent_Base.
8621 if Present (Record_Extension_Part (Type_Def)) then
8622 Set_Ekind (Derived_Type, E_Record_Type);
8623 Set_Default_SSO (Derived_Type);
8624 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8626 -- Create internal access types for components with anonymous
8629 if Ada_Version >= Ada_2005 then
8630 Check_Anonymous_Access_Components
8631 (N, Derived_Type, Derived_Type,
8632 Component_List (Record_Extension_Part (Type_Def)));
8636 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8640 -- Indic can either be an N_Identifier if the subtype indication
8641 -- contains no constraint or an N_Subtype_Indication if the subtype
8642 -- indication has a constraint.
8644 Indic := Subtype_Indication (Type_Def);
8645 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8647 -- Check that the type has visible discriminants. The type may be
8648 -- a private type with unknown discriminants whose full view has
8649 -- discriminants which are invisible.
8651 if Constraint_Present then
8652 if not Has_Discriminants (Parent_Base)
8654 (Has_Unknown_Discriminants (Parent_Base)
8655 and then Is_Private_Type (Parent_Base))
8658 ("invalid constraint: type has no discriminant",
8659 Constraint (Indic));
8661 Constraint_Present := False;
8662 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8664 elsif Is_Constrained (Parent_Type) then
8666 ("invalid constraint: parent type is already constrained",
8667 Constraint (Indic));
8669 Constraint_Present := False;
8670 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8674 -- STEP 0b: If needed, apply transformation given in point 5. above
8676 if not Private_Extension
8677 and then Has_Discriminants (Parent_Type)
8678 and then not Discriminant_Specs
8679 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8681 -- First, we must analyze the constraint (see comment in point 5.)
8682 -- The constraint may come from the subtype indication of the full
8685 if Constraint_Present then
8686 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8688 -- If there is no explicit constraint, there might be one that is
8689 -- inherited from a constrained parent type. In that case verify that
8690 -- it conforms to the constraint in the partial view. In perverse
8691 -- cases the parent subtypes of the partial and full view can have
8692 -- different constraints.
8694 elsif Present (Stored_Constraint (Parent_Type)) then
8695 New_Discrs := Stored_Constraint (Parent_Type);
8698 New_Discrs := No_Elist;
8701 if Has_Discriminants (Derived_Type)
8702 and then Has_Private_Declaration (Derived_Type)
8703 and then Present (Discriminant_Constraint (Derived_Type))
8704 and then Present (New_Discrs)
8706 -- Verify that constraints of the full view statically match
8707 -- those given in the partial view.
8713 C1 := First_Elmt (New_Discrs);
8714 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8715 while Present (C1) and then Present (C2) loop
8716 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8718 (Is_OK_Static_Expression (Node (C1))
8719 and then Is_OK_Static_Expression (Node (C2))
8721 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8726 if Constraint_Present then
8728 ("constraint not conformant to previous declaration",
8732 ("constraint of full view is incompatible "
8733 & "with partial view", N);
8743 -- Insert and analyze the declaration for the unconstrained base type
8745 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8748 Make_Full_Type_Declaration (Loc,
8749 Defining_Identifier => New_Base,
8751 Make_Derived_Type_Definition (Loc,
8752 Abstract_Present => Abstract_Present (Type_Def),
8753 Limited_Present => Limited_Present (Type_Def),
8754 Subtype_Indication =>
8755 New_Occurrence_Of (Parent_Base, Loc),
8756 Record_Extension_Part =>
8757 Relocate_Node (Record_Extension_Part (Type_Def)),
8758 Interface_List => Interface_List (Type_Def)));
8760 Set_Parent (New_Decl, Parent (N));
8761 Mark_Rewrite_Insertion (New_Decl);
8762 Insert_Before (N, New_Decl);
8764 -- In the extension case, make sure ancestor is frozen appropriately
8765 -- (see also non-discriminated case below).
8767 if Present (Record_Extension_Part (Type_Def))
8768 or else Is_Interface (Parent_Base)
8770 Freeze_Before (New_Decl, Parent_Type);
8773 -- Note that this call passes False for the Derive_Subps parameter
8774 -- because subprogram derivation is deferred until after creating
8775 -- the subtype (see below).
8778 (New_Decl, Parent_Base, New_Base,
8779 Is_Completion => False, Derive_Subps => False);
8781 -- ??? This needs re-examination to determine whether the
8782 -- above call can simply be replaced by a call to Analyze.
8784 Set_Analyzed (New_Decl);
8786 -- Insert and analyze the declaration for the constrained subtype
8788 if Constraint_Present then
8790 Make_Subtype_Indication (Loc,
8791 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8792 Constraint => Relocate_Node (Constraint (Indic)));
8796 Constr_List : constant List_Id := New_List;
8801 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8802 while Present (C) loop
8805 -- It is safe here to call New_Copy_Tree since we called
8806 -- Force_Evaluation on each constraint previously
8807 -- in Build_Discriminant_Constraints.
8809 Append (New_Copy_Tree (Expr), To => Constr_List);
8815 Make_Subtype_Indication (Loc,
8816 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8818 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8823 Make_Subtype_Declaration (Loc,
8824 Defining_Identifier => Derived_Type,
8825 Subtype_Indication => New_Indic));
8829 -- Derivation of subprograms must be delayed until the full subtype
8830 -- has been established, to ensure proper overriding of subprograms
8831 -- inherited by full types. If the derivations occurred as part of
8832 -- the call to Build_Derived_Type above, then the check for type
8833 -- conformance would fail because earlier primitive subprograms
8834 -- could still refer to the full type prior the change to the new
8835 -- subtype and hence would not match the new base type created here.
8836 -- Subprograms are not derived, however, when Derive_Subps is False
8837 -- (since otherwise there could be redundant derivations).
8839 if Derive_Subps then
8840 Derive_Subprograms (Parent_Type, Derived_Type);
8843 -- For tagged types the Discriminant_Constraint of the new base itype
8844 -- is inherited from the first subtype so that no subtype conformance
8845 -- problem arise when the first subtype overrides primitive
8846 -- operations inherited by the implicit base type.
8849 Set_Discriminant_Constraint
8850 (New_Base, Discriminant_Constraint (Derived_Type));
8856 -- If we get here Derived_Type will have no discriminants or it will be
8857 -- a discriminated unconstrained base type.
8859 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8863 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8864 -- The declaration of a specific descendant of an interface type
8865 -- freezes the interface type (RM 13.14).
8867 if not Private_Extension or else Is_Interface (Parent_Base) then
8868 Freeze_Before (N, Parent_Type);
8871 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8872 -- cannot be declared at a deeper level than its parent type is
8873 -- removed. The check on derivation within a generic body is also
8874 -- relaxed, but there's a restriction that a derived tagged type
8875 -- cannot be declared in a generic body if it's derived directly
8876 -- or indirectly from a formal type of that generic.
8878 if Ada_Version >= Ada_2005 then
8879 if Present (Enclosing_Generic_Body (Derived_Type)) then
8881 Ancestor_Type : Entity_Id;
8884 -- Check to see if any ancestor of the derived type is a
8887 Ancestor_Type := Parent_Type;
8888 while not Is_Generic_Type (Ancestor_Type)
8889 and then Etype (Ancestor_Type) /= Ancestor_Type
8891 Ancestor_Type := Etype (Ancestor_Type);
8894 -- If the derived type does have a formal type as an
8895 -- ancestor, then it's an error if the derived type is
8896 -- declared within the body of the generic unit that
8897 -- declares the formal type in its generic formal part. It's
8898 -- sufficient to check whether the ancestor type is declared
8899 -- inside the same generic body as the derived type (such as
8900 -- within a nested generic spec), in which case the
8901 -- derivation is legal. If the formal type is declared
8902 -- outside of that generic body, then it's guaranteed that
8903 -- the derived type is declared within the generic body of
8904 -- the generic unit declaring the formal type.
8906 if Is_Generic_Type (Ancestor_Type)
8907 and then Enclosing_Generic_Body (Ancestor_Type) /=
8908 Enclosing_Generic_Body (Derived_Type)
8911 ("parent type of& must not be descendant of formal type"
8912 & " of an enclosing generic body",
8913 Indic, Derived_Type);
8918 elsif Type_Access_Level (Derived_Type) /=
8919 Type_Access_Level (Parent_Type)
8920 and then not Is_Generic_Type (Derived_Type)
8922 if Is_Controlled (Parent_Type) then
8924 ("controlled type must be declared at the library level",
8928 ("type extension at deeper accessibility level than parent",
8934 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8937 and then GB /= Enclosing_Generic_Body (Parent_Base)
8940 ("parent type of& must not be outside generic body"
8942 Indic, Derived_Type);
8948 -- Ada 2005 (AI-251)
8950 if Ada_Version >= Ada_2005 and then Is_Tagged then
8952 -- "The declaration of a specific descendant of an interface type
8953 -- freezes the interface type" (RM 13.14).
8958 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8959 Iface := First (Interface_List (Type_Def));
8960 while Present (Iface) loop
8961 Freeze_Before (N, Etype (Iface));
8968 -- STEP 1b : preliminary cleanup of the full view of private types
8970 -- If the type is already marked as having discriminants, then it's the
8971 -- completion of a private type or private extension and we need to
8972 -- retain the discriminants from the partial view if the current
8973 -- declaration has Discriminant_Specifications so that we can verify
8974 -- conformance. However, we must remove any existing components that
8975 -- were inherited from the parent (and attached in Copy_And_Swap)
8976 -- because the full type inherits all appropriate components anyway, and
8977 -- we do not want the partial view's components interfering.
8979 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8980 Discrim := First_Discriminant (Derived_Type);
8982 Last_Discrim := Discrim;
8983 Next_Discriminant (Discrim);
8984 exit when No (Discrim);
8987 Set_Last_Entity (Derived_Type, Last_Discrim);
8989 -- In all other cases wipe out the list of inherited components (even
8990 -- inherited discriminants), it will be properly rebuilt here.
8993 Set_First_Entity (Derived_Type, Empty);
8994 Set_Last_Entity (Derived_Type, Empty);
8997 -- STEP 1c: Initialize some flags for the Derived_Type
8999 -- The following flags must be initialized here so that
9000 -- Process_Discriminants can check that discriminants of tagged types do
9001 -- not have a default initial value and that access discriminants are
9002 -- only specified for limited records. For completeness, these flags are
9003 -- also initialized along with all the other flags below.
9005 -- AI-419: Limitedness is not inherited from an interface parent, so to
9006 -- be limited in that case the type must be explicitly declared as
9007 -- limited. However, task and protected interfaces are always limited.
9009 if Limited_Present (Type_Def) then
9010 Set_Is_Limited_Record (Derived_Type);
9012 elsif Is_Limited_Record (Parent_Type)
9013 or else (Present (Full_View (Parent_Type))
9014 and then Is_Limited_Record (Full_View (Parent_Type)))
9016 if not Is_Interface (Parent_Type)
9017 or else Is_Synchronized_Interface (Parent_Type)
9018 or else Is_Protected_Interface (Parent_Type)
9019 or else Is_Task_Interface (Parent_Type)
9021 Set_Is_Limited_Record (Derived_Type);
9025 -- STEP 2a: process discriminants of derived type if any
9027 Push_Scope (Derived_Type);
9029 if Discriminant_Specs then
9030 Set_Has_Unknown_Discriminants (Derived_Type, False);
9032 -- The following call initializes fields Has_Discriminants and
9033 -- Discriminant_Constraint, unless we are processing the completion
9034 -- of a private type declaration.
9036 Check_Or_Process_Discriminants (N, Derived_Type);
9038 -- For untagged types, the constraint on the Parent_Type must be
9039 -- present and is used to rename the discriminants.
9041 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9042 Error_Msg_N ("untagged parent must have discriminants", Indic);
9044 elsif not Is_Tagged and then not Constraint_Present then
9046 ("discriminant constraint needed for derived untagged records",
9049 -- Otherwise the parent subtype must be constrained unless we have a
9050 -- private extension.
9052 elsif not Constraint_Present
9053 and then not Private_Extension
9054 and then not Is_Constrained (Parent_Type)
9057 ("unconstrained type not allowed in this context", Indic);
9059 elsif Constraint_Present then
9060 -- The following call sets the field Corresponding_Discriminant
9061 -- for the discriminants in the Derived_Type.
9063 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9065 -- For untagged types all new discriminants must rename
9066 -- discriminants in the parent. For private extensions new
9067 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9069 Discrim := First_Discriminant (Derived_Type);
9070 while Present (Discrim) loop
9072 and then No (Corresponding_Discriminant (Discrim))
9075 ("new discriminants must constrain old ones", Discrim);
9077 elsif Private_Extension
9078 and then Present (Corresponding_Discriminant (Discrim))
9081 ("only static constraints allowed for parent"
9082 & " discriminants in the partial view", Indic);
9086 -- If a new discriminant is used in the constraint, then its
9087 -- subtype must be statically compatible with the parent
9088 -- discriminant's subtype (3.7(15)).
9090 -- However, if the record contains an array constrained by
9091 -- the discriminant but with some different bound, the compiler
9092 -- tries to create a smaller range for the discriminant type.
9093 -- (See exp_ch3.Adjust_Discriminants). In this case, where
9094 -- the discriminant type is a scalar type, the check must use
9095 -- the original discriminant type in the parent declaration.
9098 Corr_Disc : constant Entity_Id :=
9099 Corresponding_Discriminant (Discrim);
9100 Disc_Type : constant Entity_Id := Etype (Discrim);
9101 Corr_Type : Entity_Id;
9104 if Present (Corr_Disc) then
9105 if Is_Scalar_Type (Disc_Type) then
9107 Entity (Discriminant_Type (Parent (Corr_Disc)));
9109 Corr_Type := Etype (Corr_Disc);
9113 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
9116 ("subtype must be compatible "
9117 & "with parent discriminant",
9123 Next_Discriminant (Discrim);
9126 -- Check whether the constraints of the full view statically
9127 -- match those imposed by the parent subtype [7.3(13)].
9129 if Present (Stored_Constraint (Derived_Type)) then
9134 C1 := First_Elmt (Discs);
9135 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9136 while Present (C1) and then Present (C2) loop
9138 Fully_Conformant_Expressions (Node (C1), Node (C2))
9141 ("not conformant with previous declaration",
9152 -- STEP 2b: No new discriminants, inherit discriminants if any
9155 if Private_Extension then
9156 Set_Has_Unknown_Discriminants
9158 Has_Unknown_Discriminants (Parent_Type)
9159 or else Unknown_Discriminants_Present (N));
9161 -- The partial view of the parent may have unknown discriminants,
9162 -- but if the full view has discriminants and the parent type is
9163 -- in scope they must be inherited.
9165 elsif Has_Unknown_Discriminants (Parent_Type)
9167 (not Has_Discriminants (Parent_Type)
9168 or else not In_Open_Scopes (Scope (Parent_Base)))
9170 Set_Has_Unknown_Discriminants (Derived_Type);
9173 if not Has_Unknown_Discriminants (Derived_Type)
9174 and then not Has_Unknown_Discriminants (Parent_Base)
9175 and then Has_Discriminants (Parent_Type)
9177 Inherit_Discrims := True;
9178 Set_Has_Discriminants
9179 (Derived_Type, True);
9180 Set_Discriminant_Constraint
9181 (Derived_Type, Discriminant_Constraint (Parent_Base));
9184 -- The following test is true for private types (remember
9185 -- transformation 5. is not applied to those) and in an error
9188 if Constraint_Present then
9189 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9192 -- For now mark a new derived type as constrained only if it has no
9193 -- discriminants. At the end of Build_Derived_Record_Type we properly
9194 -- set this flag in the case of private extensions. See comments in
9195 -- point 9. just before body of Build_Derived_Record_Type.
9199 not (Inherit_Discrims
9200 or else Has_Unknown_Discriminants (Derived_Type)));
9203 -- STEP 3: initialize fields of derived type
9205 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9206 Set_Stored_Constraint (Derived_Type, No_Elist);
9208 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9209 -- but cannot be interfaces
9211 if not Private_Extension
9212 and then Ekind (Derived_Type) /= E_Private_Type
9213 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9215 if Interface_Present (Type_Def) then
9216 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9219 Set_Interfaces (Derived_Type, No_Elist);
9222 -- Fields inherited from the Parent_Type
9224 Set_Has_Specified_Layout
9225 (Derived_Type, Has_Specified_Layout (Parent_Type));
9226 Set_Is_Limited_Composite
9227 (Derived_Type, Is_Limited_Composite (Parent_Type));
9228 Set_Is_Private_Composite
9229 (Derived_Type, Is_Private_Composite (Parent_Type));
9231 if Is_Tagged_Type (Parent_Type) then
9232 Set_No_Tagged_Streams_Pragma
9233 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9236 -- Fields inherited from the Parent_Base
9238 Set_Has_Controlled_Component
9239 (Derived_Type, Has_Controlled_Component (Parent_Base));
9240 Set_Has_Non_Standard_Rep
9241 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9242 Set_Has_Primitive_Operations
9243 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9245 -- Set fields for private derived types
9247 if Is_Private_Type (Derived_Type) then
9248 Set_Depends_On_Private (Derived_Type, True);
9249 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9252 -- Inherit fields for non-private types. If this is the completion of a
9253 -- derivation from a private type, the parent itself is private and the
9254 -- attributes come from its full view, which must be present.
9256 if Is_Record_Type (Derived_Type) then
9258 Parent_Full : Entity_Id;
9261 if Is_Private_Type (Parent_Base)
9262 and then not Is_Record_Type (Parent_Base)
9264 Parent_Full := Full_View (Parent_Base);
9266 Parent_Full := Parent_Base;
9269 Set_Component_Alignment
9270 (Derived_Type, Component_Alignment (Parent_Full));
9272 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9273 Set_Has_Complex_Representation
9274 (Derived_Type, Has_Complex_Representation (Parent_Full));
9276 -- For untagged types, inherit the layout by default to avoid
9277 -- costly changes of representation for type conversions.
9279 if not Is_Tagged then
9280 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9281 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9286 -- Set fields for tagged types
9289 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9291 -- All tagged types defined in Ada.Finalization are controlled
9293 if Chars (Scope (Derived_Type)) = Name_Finalization
9294 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9295 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9297 Set_Is_Controlled_Active (Derived_Type);
9299 Set_Is_Controlled_Active
9300 (Derived_Type, Is_Controlled_Active (Parent_Base));
9303 -- Minor optimization: there is no need to generate the class-wide
9304 -- entity associated with an underlying record view.
9306 if not Is_Underlying_Record_View (Derived_Type) then
9307 Make_Class_Wide_Type (Derived_Type);
9310 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9312 if Has_Discriminants (Derived_Type)
9313 and then Constraint_Present
9315 Set_Stored_Constraint
9316 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9319 if Ada_Version >= Ada_2005 then
9321 Ifaces_List : Elist_Id;
9324 -- Checks rules 3.9.4 (13/2 and 14/2)
9326 if Comes_From_Source (Derived_Type)
9327 and then not Is_Private_Type (Derived_Type)
9328 and then Is_Interface (Parent_Type)
9329 and then not Is_Interface (Derived_Type)
9331 if Is_Task_Interface (Parent_Type) then
9333 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9336 elsif Is_Protected_Interface (Parent_Type) then
9338 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9343 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9345 Check_Interfaces (N, Type_Def);
9347 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9348 -- not already in the parents.
9352 Ifaces_List => Ifaces_List,
9353 Exclude_Parents => True);
9355 Set_Interfaces (Derived_Type, Ifaces_List);
9357 -- If the derived type is the anonymous type created for
9358 -- a declaration whose parent has a constraint, propagate
9359 -- the interface list to the source type. This must be done
9360 -- prior to the completion of the analysis of the source type
9361 -- because the components in the extension may contain current
9362 -- instances whose legality depends on some ancestor.
9364 if Is_Itype (Derived_Type) then
9366 Def : constant Node_Id :=
9367 Associated_Node_For_Itype (Derived_Type);
9370 and then Nkind (Def) = N_Full_Type_Declaration
9373 (Defining_Identifier (Def), Ifaces_List);
9378 -- A type extension is automatically Ghost when one of its
9379 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9380 -- also inherited when the parent type is Ghost, but this is
9381 -- done in Build_Derived_Type as the mechanism also handles
9382 -- untagged derivations.
9384 if Implements_Ghost_Interface (Derived_Type) then
9385 Set_Is_Ghost_Entity (Derived_Type);
9391 -- STEP 4: Inherit components from the parent base and constrain them.
9392 -- Apply the second transformation described in point 6. above.
9394 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9395 or else not Has_Discriminants (Parent_Type)
9396 or else not Is_Constrained (Parent_Type)
9400 Constrs := Discriminant_Constraint (Parent_Type);
9405 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9407 -- STEP 5a: Copy the parent record declaration for untagged types
9409 Set_Has_Implicit_Dereference
9410 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9412 if not Is_Tagged then
9414 -- Discriminant_Constraint (Derived_Type) has been properly
9415 -- constructed. Save it and temporarily set it to Empty because we
9416 -- do not want the call to New_Copy_Tree below to mess this list.
9418 if Has_Discriminants (Derived_Type) then
9419 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9420 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9422 Save_Discr_Constr := No_Elist;
9425 -- Save the Etype field of Derived_Type. It is correctly set now,
9426 -- but the call to New_Copy tree may remap it to point to itself,
9427 -- which is not what we want. Ditto for the Next_Entity field.
9429 Save_Etype := Etype (Derived_Type);
9430 Save_Next_Entity := Next_Entity (Derived_Type);
9432 -- Assoc_List maps all stored discriminants in the Parent_Base to
9433 -- stored discriminants in the Derived_Type. It is fundamental that
9434 -- no types or itypes with discriminants other than the stored
9435 -- discriminants appear in the entities declared inside
9436 -- Derived_Type, since the back end cannot deal with it.
9440 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9441 Copy_Dimensions_Of_Components (Derived_Type);
9443 -- Restore the fields saved prior to the New_Copy_Tree call
9444 -- and compute the stored constraint.
9446 Set_Etype (Derived_Type, Save_Etype);
9447 Link_Entities (Derived_Type, Save_Next_Entity);
9449 if Has_Discriminants (Derived_Type) then
9450 Set_Discriminant_Constraint
9451 (Derived_Type, Save_Discr_Constr);
9452 Set_Stored_Constraint
9453 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9454 Replace_Components (Derived_Type, New_Decl);
9457 -- Insert the new derived type declaration
9459 Rewrite (N, New_Decl);
9461 -- STEP 5b: Complete the processing for record extensions in generics
9463 -- There is no completion for record extensions declared in the
9464 -- parameter part of a generic, so we need to complete processing for
9465 -- these generic record extensions here. The Record_Type_Definition call
9466 -- will change the Ekind of the components from E_Void to E_Component.
9468 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9469 Record_Type_Definition (Empty, Derived_Type);
9471 -- STEP 5c: Process the record extension for non private tagged types
9473 elsif not Private_Extension then
9474 Expand_Record_Extension (Derived_Type, Type_Def);
9476 -- Note : previously in ASIS mode we set the Parent_Subtype of the
9477 -- derived type to propagate some semantic information. This led
9478 -- to other ASIS failures and has been removed.
9480 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9481 -- implemented interfaces if we are in expansion mode
9484 and then Has_Interfaces (Derived_Type)
9486 Add_Interface_Tag_Components (N, Derived_Type);
9489 -- Analyze the record extension
9491 Record_Type_Definition
9492 (Record_Extension_Part (Type_Def), Derived_Type);
9497 -- Nothing else to do if there is an error in the derivation.
9498 -- An unusual case: the full view may be derived from a type in an
9499 -- instance, when the partial view was used illegally as an actual
9500 -- in that instance, leading to a circular definition.
9502 if Etype (Derived_Type) = Any_Type
9503 or else Etype (Parent_Type) = Derived_Type
9508 -- Set delayed freeze and then derive subprograms, we need to do
9509 -- this in this order so that derived subprograms inherit the
9510 -- derived freeze if necessary.
9512 Set_Has_Delayed_Freeze (Derived_Type);
9514 if Derive_Subps then
9515 Derive_Subprograms (Parent_Type, Derived_Type);
9518 -- If we have a private extension which defines a constrained derived
9519 -- type mark as constrained here after we have derived subprograms. See
9520 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9522 if Private_Extension and then Inherit_Discrims then
9523 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9524 Set_Is_Constrained (Derived_Type, True);
9525 Set_Discriminant_Constraint (Derived_Type, Discs);
9527 elsif Is_Constrained (Parent_Type) then
9529 (Derived_Type, True);
9530 Set_Discriminant_Constraint
9531 (Derived_Type, Discriminant_Constraint (Parent_Type));
9535 -- Update the class-wide type, which shares the now-completed entity
9536 -- list with its specific type. In case of underlying record views,
9537 -- we do not generate the corresponding class wide entity.
9540 and then not Is_Underlying_Record_View (Derived_Type)
9543 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9545 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9548 Check_Function_Writable_Actuals (N);
9549 end Build_Derived_Record_Type;
9551 ------------------------
9552 -- Build_Derived_Type --
9553 ------------------------
9555 procedure Build_Derived_Type
9557 Parent_Type : Entity_Id;
9558 Derived_Type : Entity_Id;
9559 Is_Completion : Boolean;
9560 Derive_Subps : Boolean := True)
9562 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9565 -- Set common attributes
9567 Set_Scope (Derived_Type, Current_Scope);
9568 Set_Etype (Derived_Type, Parent_Base);
9569 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9570 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9572 Set_Size_Info (Derived_Type, Parent_Type);
9573 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9575 Set_Is_Controlled_Active
9576 (Derived_Type, Is_Controlled_Active (Parent_Type));
9578 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9579 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9580 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9582 if Is_Tagged_Type (Derived_Type) then
9583 Set_No_Tagged_Streams_Pragma
9584 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9587 -- If the parent has primitive routines, set the derived type link
9589 if Has_Primitive_Operations (Parent_Type) then
9590 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9593 -- If the parent type is a private subtype, the convention on the base
9594 -- type may be set in the private part, and not propagated to the
9595 -- subtype until later, so we obtain the convention from the base type.
9597 Set_Convention (Derived_Type, Convention (Parent_Base));
9599 -- Set SSO default for record or array type
9601 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9602 and then Is_Base_Type (Derived_Type)
9604 Set_Default_SSO (Derived_Type);
9607 -- A derived type inherits the Default_Initial_Condition pragma coming
9608 -- from any parent type within the derivation chain.
9610 if Has_DIC (Parent_Type) then
9611 Set_Has_Inherited_DIC (Derived_Type);
9614 -- A derived type inherits any class-wide invariants coming from a
9615 -- parent type or an interface. Note that the invariant procedure of
9616 -- the parent type should not be inherited because the derived type may
9617 -- define invariants of its own.
9619 if not Is_Interface (Derived_Type) then
9620 if Has_Inherited_Invariants (Parent_Type)
9621 or else Has_Inheritable_Invariants (Parent_Type)
9623 Set_Has_Inherited_Invariants (Derived_Type);
9625 elsif Is_Concurrent_Type (Derived_Type)
9626 or else Is_Tagged_Type (Derived_Type)
9631 Iface_Elmt : Elmt_Id;
9636 Ifaces_List => Ifaces,
9637 Exclude_Parents => True);
9639 if Present (Ifaces) then
9640 Iface_Elmt := First_Elmt (Ifaces);
9641 while Present (Iface_Elmt) loop
9642 Iface := Node (Iface_Elmt);
9644 if Has_Inheritable_Invariants (Iface) then
9645 Set_Has_Inherited_Invariants (Derived_Type);
9649 Next_Elmt (Iface_Elmt);
9656 -- We similarly inherit predicates. Note that for scalar derived types
9657 -- the predicate is inherited from the first subtype, and not from its
9658 -- (anonymous) base type.
9660 if Has_Predicates (Parent_Type)
9661 or else Has_Predicates (First_Subtype (Parent_Type))
9663 Set_Has_Predicates (Derived_Type);
9666 -- The derived type inherits representation clauses from the parent
9667 -- type, and from any interfaces.
9669 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9672 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
9674 while Present (Iface) loop
9675 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
9680 -- If the parent type has delayed rep aspects, then mark the derived
9681 -- type as possibly inheriting a delayed rep aspect.
9683 if Has_Delayed_Rep_Aspects (Parent_Type) then
9684 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9687 -- A derived type becomes Ghost when its parent type is also Ghost
9688 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9689 -- directly inherited because the Ghost policy in effect may differ.
9691 if Is_Ghost_Entity (Parent_Type) then
9692 Set_Is_Ghost_Entity (Derived_Type);
9695 -- Type dependent processing
9697 case Ekind (Parent_Type) is
9698 when Numeric_Kind =>
9699 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9702 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9704 when Class_Wide_Kind
9708 Build_Derived_Record_Type
9709 (N, Parent_Type, Derived_Type, Derive_Subps);
9712 when Enumeration_Kind =>
9713 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9716 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9718 when Incomplete_Or_Private_Kind =>
9719 Build_Derived_Private_Type
9720 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9722 -- For discriminated types, the derivation includes deriving
9723 -- primitive operations. For others it is done below.
9725 if Is_Tagged_Type (Parent_Type)
9726 or else Has_Discriminants (Parent_Type)
9727 or else (Present (Full_View (Parent_Type))
9728 and then Has_Discriminants (Full_View (Parent_Type)))
9733 when Concurrent_Kind =>
9734 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9737 raise Program_Error;
9740 -- Nothing more to do if some error occurred
9742 if Etype (Derived_Type) = Any_Type then
9746 -- Set delayed freeze and then derive subprograms, we need to do this
9747 -- in this order so that derived subprograms inherit the derived freeze
9750 Set_Has_Delayed_Freeze (Derived_Type);
9752 if Derive_Subps then
9753 Derive_Subprograms (Parent_Type, Derived_Type);
9756 Set_Has_Primitive_Operations
9757 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9758 end Build_Derived_Type;
9760 -----------------------
9761 -- Build_Discriminal --
9762 -----------------------
9764 procedure Build_Discriminal (Discrim : Entity_Id) is
9765 D_Minal : Entity_Id;
9766 CR_Disc : Entity_Id;
9769 -- A discriminal has the same name as the discriminant
9771 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9773 Set_Ekind (D_Minal, E_In_Parameter);
9774 Set_Mechanism (D_Minal, Default_Mechanism);
9775 Set_Etype (D_Minal, Etype (Discrim));
9776 Set_Scope (D_Minal, Current_Scope);
9777 Set_Parent (D_Minal, Parent (Discrim));
9779 Set_Discriminal (Discrim, D_Minal);
9780 Set_Discriminal_Link (D_Minal, Discrim);
9782 -- For task types, build at once the discriminants of the corresponding
9783 -- record, which are needed if discriminants are used in entry defaults
9784 -- and in family bounds.
9786 if Is_Concurrent_Type (Current_Scope)
9788 Is_Limited_Type (Current_Scope)
9790 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9792 Set_Ekind (CR_Disc, E_In_Parameter);
9793 Set_Mechanism (CR_Disc, Default_Mechanism);
9794 Set_Etype (CR_Disc, Etype (Discrim));
9795 Set_Scope (CR_Disc, Current_Scope);
9796 Set_Discriminal_Link (CR_Disc, Discrim);
9797 Set_CR_Discriminant (Discrim, CR_Disc);
9799 end Build_Discriminal;
9801 ------------------------------------
9802 -- Build_Discriminant_Constraints --
9803 ------------------------------------
9805 function Build_Discriminant_Constraints
9808 Derived_Def : Boolean := False) return Elist_Id
9810 C : constant Node_Id := Constraint (Def);
9811 Nb_Discr : constant Nat := Number_Discriminants (T);
9813 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9814 -- Saves the expression corresponding to a given discriminant in T
9816 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9817 -- Return the Position number within array Discr_Expr of a discriminant
9818 -- D within the discriminant list of the discriminated type T.
9820 procedure Process_Discriminant_Expression
9823 -- If this is a discriminant constraint on a partial view, do not
9824 -- generate an overflow check on the discriminant expression. The check
9825 -- will be generated when constraining the full view. Otherwise the
9826 -- backend creates duplicate symbols for the temporaries corresponding
9827 -- to the expressions to be checked, causing spurious assembler errors.
9833 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9837 Disc := First_Discriminant (T);
9838 for J in Discr_Expr'Range loop
9843 Next_Discriminant (Disc);
9846 -- Note: Since this function is called on discriminants that are
9847 -- known to belong to the discriminated type, falling through the
9848 -- loop with no match signals an internal compiler error.
9850 raise Program_Error;
9853 -------------------------------------
9854 -- Process_Discriminant_Expression --
9855 -------------------------------------
9857 procedure Process_Discriminant_Expression
9861 BDT : constant Entity_Id := Base_Type (Etype (D));
9864 -- If this is a discriminant constraint on a partial view, do
9865 -- not generate an overflow on the discriminant expression. The
9866 -- check will be generated when constraining the full view.
9868 if Is_Private_Type (T)
9869 and then Present (Full_View (T))
9871 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9873 Analyze_And_Resolve (Expr, BDT);
9875 end Process_Discriminant_Expression;
9877 -- Declarations local to Build_Discriminant_Constraints
9881 Elist : constant Elist_Id := New_Elmt_List;
9889 Discrim_Present : Boolean := False;
9891 -- Start of processing for Build_Discriminant_Constraints
9894 -- The following loop will process positional associations only.
9895 -- For a positional association, the (single) discriminant is
9896 -- implicitly specified by position, in textual order (RM 3.7.2).
9898 Discr := First_Discriminant (T);
9899 Constr := First (Constraints (C));
9900 for D in Discr_Expr'Range loop
9901 exit when Nkind (Constr) = N_Discriminant_Association;
9904 Error_Msg_N ("too few discriminants given in constraint", C);
9905 return New_Elmt_List;
9907 elsif Nkind (Constr) = N_Range
9908 or else (Nkind (Constr) = N_Attribute_Reference
9909 and then Attribute_Name (Constr) = Name_Range)
9912 ("a range is not a valid discriminant constraint", Constr);
9913 Discr_Expr (D) := Error;
9915 elsif Nkind (Constr) = N_Subtype_Indication then
9917 ("a subtype indication is not a valid discriminant constraint",
9919 Discr_Expr (D) := Error;
9922 Process_Discriminant_Expression (Constr, Discr);
9923 Discr_Expr (D) := Constr;
9926 Next_Discriminant (Discr);
9930 if No (Discr) and then Present (Constr) then
9931 Error_Msg_N ("too many discriminants given in constraint", Constr);
9932 return New_Elmt_List;
9935 -- Named associations can be given in any order, but if both positional
9936 -- and named associations are used in the same discriminant constraint,
9937 -- then positional associations must occur first, at their normal
9938 -- position. Hence once a named association is used, the rest of the
9939 -- discriminant constraint must use only named associations.
9941 while Present (Constr) loop
9943 -- Positional association forbidden after a named association
9945 if Nkind (Constr) /= N_Discriminant_Association then
9946 Error_Msg_N ("positional association follows named one", Constr);
9947 return New_Elmt_List;
9949 -- Otherwise it is a named association
9952 -- E records the type of the discriminants in the named
9953 -- association. All the discriminants specified in the same name
9954 -- association must have the same type.
9958 -- Search the list of discriminants in T to see if the simple name
9959 -- given in the constraint matches any of them.
9961 Id := First (Selector_Names (Constr));
9962 while Present (Id) loop
9965 -- If Original_Discriminant is present, we are processing a
9966 -- generic instantiation and this is an instance node. We need
9967 -- to find the name of the corresponding discriminant in the
9968 -- actual record type T and not the name of the discriminant in
9969 -- the generic formal. Example:
9972 -- type G (D : int) is private;
9974 -- subtype W is G (D => 1);
9976 -- type Rec (X : int) is record ... end record;
9977 -- package Q is new P (G => Rec);
9979 -- At the point of the instantiation, formal type G is Rec
9980 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9981 -- which really looks like "subtype W is Rec (D => 1);" at
9982 -- the point of instantiation, we want to find the discriminant
9983 -- that corresponds to D in Rec, i.e. X.
9985 if Present (Original_Discriminant (Id))
9986 and then In_Instance
9988 Discr := Find_Corresponding_Discriminant (Id, T);
9992 Discr := First_Discriminant (T);
9993 while Present (Discr) loop
9994 if Chars (Discr) = Chars (Id) then
9999 Next_Discriminant (Discr);
10003 Error_Msg_N ("& does not match any discriminant", Id);
10004 return New_Elmt_List;
10006 -- If the parent type is a generic formal, preserve the
10007 -- name of the discriminant for subsequent instances.
10008 -- see comment at the beginning of this if statement.
10010 elsif Is_Generic_Type (Root_Type (T)) then
10011 Set_Original_Discriminant (Id, Discr);
10015 Position := Pos_Of_Discr (T, Discr);
10017 if Present (Discr_Expr (Position)) then
10018 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10021 -- Each discriminant specified in the same named association
10022 -- must be associated with a separate copy of the
10023 -- corresponding expression.
10025 if Present (Next (Id)) then
10026 Expr := New_Copy_Tree (Expression (Constr));
10027 Set_Parent (Expr, Parent (Expression (Constr)));
10029 Expr := Expression (Constr);
10032 Discr_Expr (Position) := Expr;
10033 Process_Discriminant_Expression (Expr, Discr);
10036 -- A discriminant association with more than one discriminant
10037 -- name is only allowed if the named discriminants are all of
10038 -- the same type (RM 3.7.1(8)).
10041 E := Base_Type (Etype (Discr));
10043 elsif Base_Type (Etype (Discr)) /= E then
10045 ("all discriminants in an association " &
10046 "must have the same type", Id);
10056 -- A discriminant constraint must provide exactly one value for each
10057 -- discriminant of the type (RM 3.7.1(8)).
10059 for J in Discr_Expr'Range loop
10060 if No (Discr_Expr (J)) then
10061 Error_Msg_N ("too few discriminants given in constraint", C);
10062 return New_Elmt_List;
10066 -- Determine if there are discriminant expressions in the constraint
10068 for J in Discr_Expr'Range loop
10069 if Denotes_Discriminant
10070 (Discr_Expr (J), Check_Concurrent => True)
10072 Discrim_Present := True;
10076 -- Build an element list consisting of the expressions given in the
10077 -- discriminant constraint and apply the appropriate checks. The list
10078 -- is constructed after resolving any named discriminant associations
10079 -- and therefore the expressions appear in the textual order of the
10082 Discr := First_Discriminant (T);
10083 for J in Discr_Expr'Range loop
10084 if Discr_Expr (J) /= Error then
10085 Append_Elmt (Discr_Expr (J), Elist);
10087 -- If any of the discriminant constraints is given by a
10088 -- discriminant and we are in a derived type declaration we
10089 -- have a discriminant renaming. Establish link between new
10090 -- and old discriminant. The new discriminant has an implicit
10091 -- dereference if the old one does.
10093 if Denotes_Discriminant (Discr_Expr (J)) then
10094 if Derived_Def then
10096 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10099 Set_Corresponding_Discriminant (New_Discr, Discr);
10100 Set_Has_Implicit_Dereference (New_Discr,
10101 Has_Implicit_Dereference (Discr));
10105 -- Force the evaluation of non-discriminant expressions.
10106 -- If we have found a discriminant in the constraint 3.4(26)
10107 -- and 3.8(18) demand that no range checks are performed are
10108 -- after evaluation. If the constraint is for a component
10109 -- definition that has a per-object constraint, expressions are
10110 -- evaluated but not checked either. In all other cases perform
10114 if Discrim_Present then
10117 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
10118 and then Has_Per_Object_Constraint
10119 (Defining_Identifier (Parent (Parent (Def))))
10123 elsif Is_Access_Type (Etype (Discr)) then
10124 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10127 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10130 Force_Evaluation (Discr_Expr (J));
10133 -- Check that the designated type of an access discriminant's
10134 -- expression is not a class-wide type unless the discriminant's
10135 -- designated type is also class-wide.
10137 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10138 and then not Is_Class_Wide_Type
10139 (Designated_Type (Etype (Discr)))
10140 and then Etype (Discr_Expr (J)) /= Any_Type
10141 and then Is_Class_Wide_Type
10142 (Designated_Type (Etype (Discr_Expr (J))))
10144 Wrong_Type (Discr_Expr (J), Etype (Discr));
10146 elsif Is_Access_Type (Etype (Discr))
10147 and then not Is_Access_Constant (Etype (Discr))
10148 and then Is_Access_Type (Etype (Discr_Expr (J)))
10149 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10152 ("constraint for discriminant& must be access to variable",
10157 Next_Discriminant (Discr);
10161 end Build_Discriminant_Constraints;
10163 ---------------------------------
10164 -- Build_Discriminated_Subtype --
10165 ---------------------------------
10167 procedure Build_Discriminated_Subtype
10169 Def_Id : Entity_Id;
10171 Related_Nod : Node_Id;
10172 For_Access : Boolean := False)
10174 Has_Discrs : constant Boolean := Has_Discriminants (T);
10175 Constrained : constant Boolean :=
10177 and then not Is_Empty_Elmt_List (Elist)
10178 and then not Is_Class_Wide_Type (T))
10179 or else Is_Constrained (T);
10182 if Ekind (T) = E_Record_Type then
10184 Set_Ekind (Def_Id, E_Private_Subtype);
10185 Set_Is_For_Access_Subtype (Def_Id, True);
10187 Set_Ekind (Def_Id, E_Record_Subtype);
10190 -- Inherit preelaboration flag from base, for types for which it
10191 -- may have been set: records, private types, protected types.
10193 Set_Known_To_Have_Preelab_Init
10194 (Def_Id, Known_To_Have_Preelab_Init (T));
10196 elsif Ekind (T) = E_Task_Type then
10197 Set_Ekind (Def_Id, E_Task_Subtype);
10199 elsif Ekind (T) = E_Protected_Type then
10200 Set_Ekind (Def_Id, E_Protected_Subtype);
10201 Set_Known_To_Have_Preelab_Init
10202 (Def_Id, Known_To_Have_Preelab_Init (T));
10204 elsif Is_Private_Type (T) then
10205 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10206 Set_Known_To_Have_Preelab_Init
10207 (Def_Id, Known_To_Have_Preelab_Init (T));
10209 -- Private subtypes may have private dependents
10211 Set_Private_Dependents (Def_Id, New_Elmt_List);
10213 elsif Is_Class_Wide_Type (T) then
10214 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
10217 -- Incomplete type. Attach subtype to list of dependents, to be
10218 -- completed with full view of parent type, unless is it the
10219 -- designated subtype of a record component within an init_proc.
10220 -- This last case arises for a component of an access type whose
10221 -- designated type is incomplete (e.g. a Taft Amendment type).
10222 -- The designated subtype is within an inner scope, and needs no
10223 -- elaboration, because only the access type is needed in the
10224 -- initialization procedure.
10226 if Ekind (T) = E_Incomplete_Type then
10227 Set_Ekind (Def_Id, E_Incomplete_Subtype);
10229 Set_Ekind (Def_Id, Ekind (T));
10232 if For_Access and then Within_Init_Proc then
10235 Append_Elmt (Def_Id, Private_Dependents (T));
10239 Set_Etype (Def_Id, T);
10240 Init_Size_Align (Def_Id);
10241 Set_Has_Discriminants (Def_Id, Has_Discrs);
10242 Set_Is_Constrained (Def_Id, Constrained);
10244 Set_First_Entity (Def_Id, First_Entity (T));
10245 Set_Last_Entity (Def_Id, Last_Entity (T));
10246 Set_Has_Implicit_Dereference
10247 (Def_Id, Has_Implicit_Dereference (T));
10248 Set_Has_Pragma_Unreferenced_Objects
10249 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10251 -- If the subtype is the completion of a private declaration, there may
10252 -- have been representation clauses for the partial view, and they must
10253 -- be preserved. Build_Derived_Type chains the inherited clauses with
10254 -- the ones appearing on the extension. If this comes from a subtype
10255 -- declaration, all clauses are inherited.
10257 if No (First_Rep_Item (Def_Id)) then
10258 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10261 if Is_Tagged_Type (T) then
10262 Set_Is_Tagged_Type (Def_Id);
10263 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10264 Make_Class_Wide_Type (Def_Id);
10267 Set_Stored_Constraint (Def_Id, No_Elist);
10270 Set_Discriminant_Constraint (Def_Id, Elist);
10271 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10274 if Is_Tagged_Type (T) then
10276 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10277 -- concurrent record type (which has the list of primitive
10280 if Ada_Version >= Ada_2005
10281 and then Is_Concurrent_Type (T)
10283 Set_Corresponding_Record_Type (Def_Id,
10284 Corresponding_Record_Type (T));
10286 Set_Direct_Primitive_Operations (Def_Id,
10287 Direct_Primitive_Operations (T));
10290 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10293 -- Subtypes introduced by component declarations do not need to be
10294 -- marked as delayed, and do not get freeze nodes, because the semantics
10295 -- verifies that the parents of the subtypes are frozen before the
10296 -- enclosing record is frozen.
10298 if not Is_Type (Scope (Def_Id)) then
10299 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10301 if Is_Private_Type (T)
10302 and then Present (Full_View (T))
10304 Conditional_Delay (Def_Id, Full_View (T));
10306 Conditional_Delay (Def_Id, T);
10310 if Is_Record_Type (T) then
10311 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10314 and then not Is_Empty_Elmt_List (Elist)
10315 and then not For_Access
10317 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10319 elsif not For_Access then
10320 Set_Cloned_Subtype (Def_Id, T);
10323 end Build_Discriminated_Subtype;
10325 ---------------------------
10326 -- Build_Itype_Reference --
10327 ---------------------------
10329 procedure Build_Itype_Reference
10333 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10336 -- Itype references are only created for use by the back-end
10338 if Inside_A_Generic then
10341 Set_Itype (IR, Ityp);
10343 -- If Nod is a library unit entity, then Insert_After won't work,
10344 -- because Nod is not a member of any list. Therefore, we use
10345 -- Add_Global_Declaration in this case. This can happen if we have a
10346 -- build-in-place library function.
10348 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10350 (Nkind (Nod) = N_Defining_Program_Unit_Name
10351 and then Is_Compilation_Unit (Defining_Identifier (Nod)))
10353 Add_Global_Declaration (IR);
10355 Insert_After (Nod, IR);
10358 end Build_Itype_Reference;
10360 ------------------------
10361 -- Build_Scalar_Bound --
10362 ------------------------
10364 function Build_Scalar_Bound
10367 Der_T : Entity_Id) return Node_Id
10369 New_Bound : Entity_Id;
10372 -- Note: not clear why this is needed, how can the original bound
10373 -- be unanalyzed at this point? and if it is, what business do we
10374 -- have messing around with it? and why is the base type of the
10375 -- parent type the right type for the resolution. It probably is
10376 -- not. It is OK for the new bound we are creating, but not for
10377 -- the old one??? Still if it never happens, no problem.
10379 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10381 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10382 New_Bound := New_Copy (Bound);
10383 Set_Etype (New_Bound, Der_T);
10384 Set_Analyzed (New_Bound);
10386 elsif Is_Entity_Name (Bound) then
10387 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10389 -- The following is almost certainly wrong. What business do we have
10390 -- relocating a node (Bound) that is presumably still attached to
10391 -- the tree elsewhere???
10394 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10397 Set_Etype (New_Bound, Der_T);
10399 end Build_Scalar_Bound;
10401 --------------------------------
10402 -- Build_Underlying_Full_View --
10403 --------------------------------
10405 procedure Build_Underlying_Full_View
10410 Loc : constant Source_Ptr := Sloc (N);
10411 Subt : constant Entity_Id :=
10412 Make_Defining_Identifier
10413 (Loc, New_External_Name (Chars (Typ), 'S'));
10420 procedure Set_Discriminant_Name (Id : Node_Id);
10421 -- If the derived type has discriminants, they may rename discriminants
10422 -- of the parent. When building the full view of the parent, we need to
10423 -- recover the names of the original discriminants if the constraint is
10424 -- given by named associations.
10426 ---------------------------
10427 -- Set_Discriminant_Name --
10428 ---------------------------
10430 procedure Set_Discriminant_Name (Id : Node_Id) is
10434 Set_Original_Discriminant (Id, Empty);
10436 if Has_Discriminants (Typ) then
10437 Disc := First_Discriminant (Typ);
10438 while Present (Disc) loop
10439 if Chars (Disc) = Chars (Id)
10440 and then Present (Corresponding_Discriminant (Disc))
10442 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
10444 Next_Discriminant (Disc);
10447 end Set_Discriminant_Name;
10449 -- Start of processing for Build_Underlying_Full_View
10452 if Nkind (N) = N_Full_Type_Declaration then
10453 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
10455 elsif Nkind (N) = N_Subtype_Declaration then
10456 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
10458 elsif Nkind (N) = N_Component_Declaration then
10461 (Constraint (Subtype_Indication (Component_Definition (N))));
10464 raise Program_Error;
10467 C := First (Constraints (Constr));
10468 while Present (C) loop
10469 if Nkind (C) = N_Discriminant_Association then
10470 Id := First (Selector_Names (C));
10471 while Present (Id) loop
10472 Set_Discriminant_Name (Id);
10481 Make_Subtype_Declaration (Loc,
10482 Defining_Identifier => Subt,
10483 Subtype_Indication =>
10484 Make_Subtype_Indication (Loc,
10485 Subtype_Mark => New_Occurrence_Of (Par, Loc),
10486 Constraint => New_Copy_Tree (Constr)));
10488 -- If this is a component subtype for an outer itype, it is not
10489 -- a list member, so simply set the parent link for analysis: if
10490 -- the enclosing type does not need to be in a declarative list,
10491 -- neither do the components.
10493 if Is_List_Member (N)
10494 and then Nkind (N) /= N_Component_Declaration
10496 Insert_Before (N, Indic);
10498 Set_Parent (Indic, Parent (N));
10502 Set_Underlying_Full_View (Typ, Full_View (Subt));
10503 Set_Is_Underlying_Full_View (Full_View (Subt));
10504 end Build_Underlying_Full_View;
10506 -------------------------------
10507 -- Check_Abstract_Overriding --
10508 -------------------------------
10510 procedure Check_Abstract_Overriding (T : Entity_Id) is
10511 Alias_Subp : Entity_Id;
10513 Op_List : Elist_Id;
10515 Type_Def : Node_Id;
10517 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10518 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10519 -- which has pragma Implemented already set. Check whether Subp's entity
10520 -- kind conforms to the implementation kind of the overridden routine.
10522 procedure Check_Pragma_Implemented
10524 Iface_Subp : Entity_Id);
10525 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10526 -- Iface_Subp and both entities have pragma Implemented already set on
10527 -- them. Check whether the two implementation kinds are conforming.
10529 procedure Inherit_Pragma_Implemented
10531 Iface_Subp : Entity_Id);
10532 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10533 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10534 -- Propagate the implementation kind of Iface_Subp to Subp.
10536 ------------------------------
10537 -- Check_Pragma_Implemented --
10538 ------------------------------
10540 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10541 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10542 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10543 Subp_Alias : constant Entity_Id := Alias (Subp);
10544 Contr_Typ : Entity_Id;
10545 Impl_Subp : Entity_Id;
10548 -- Subp must have an alias since it is a hidden entity used to link
10549 -- an interface subprogram to its overriding counterpart.
10551 pragma Assert (Present (Subp_Alias));
10553 -- Handle aliases to synchronized wrappers
10555 Impl_Subp := Subp_Alias;
10557 if Is_Primitive_Wrapper (Impl_Subp) then
10558 Impl_Subp := Wrapped_Entity (Impl_Subp);
10561 -- Extract the type of the controlling formal
10563 Contr_Typ := Etype (First_Formal (Subp_Alias));
10565 if Is_Concurrent_Record_Type (Contr_Typ) then
10566 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10569 -- An interface subprogram whose implementation kind is By_Entry must
10570 -- be implemented by an entry.
10572 if Impl_Kind = Name_By_Entry
10573 and then Ekind (Impl_Subp) /= E_Entry
10575 Error_Msg_Node_2 := Iface_Alias;
10577 ("type & must implement abstract subprogram & with an entry",
10578 Subp_Alias, Contr_Typ);
10580 elsif Impl_Kind = Name_By_Protected_Procedure then
10582 -- An interface subprogram whose implementation kind is By_
10583 -- Protected_Procedure cannot be implemented by a primitive
10584 -- procedure of a task type.
10586 if Ekind (Contr_Typ) /= E_Protected_Type then
10587 Error_Msg_Node_2 := Contr_Typ;
10589 ("interface subprogram & cannot be implemented by a " &
10590 "primitive procedure of task type &", Subp_Alias,
10593 -- An interface subprogram whose implementation kind is By_
10594 -- Protected_Procedure must be implemented by a procedure.
10596 elsif Ekind (Impl_Subp) /= E_Procedure then
10597 Error_Msg_Node_2 := Iface_Alias;
10599 ("type & must implement abstract subprogram & with a " &
10600 "procedure", Subp_Alias, Contr_Typ);
10602 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10603 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10605 Error_Msg_Name_1 := Impl_Kind;
10607 ("overriding operation& must have synchronization%",
10611 -- If primitive has Optional synchronization, overriding operation
10612 -- must match if it has an explicit synchronization..
10614 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10615 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10617 Error_Msg_Name_1 := Impl_Kind;
10619 ("overriding operation& must have syncrhonization%",
10622 end Check_Pragma_Implemented;
10624 ------------------------------
10625 -- Check_Pragma_Implemented --
10626 ------------------------------
10628 procedure Check_Pragma_Implemented
10630 Iface_Subp : Entity_Id)
10632 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10633 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10636 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10637 -- and overriding subprogram are different. In general this is an
10638 -- error except when the implementation kind of the overridden
10639 -- subprograms is By_Any or Optional.
10641 if Iface_Kind /= Subp_Kind
10642 and then Iface_Kind /= Name_By_Any
10643 and then Iface_Kind /= Name_Optional
10645 if Iface_Kind = Name_By_Entry then
10647 ("incompatible implementation kind, overridden subprogram " &
10648 "is marked By_Entry", Subp);
10651 ("incompatible implementation kind, overridden subprogram " &
10652 "is marked By_Protected_Procedure", Subp);
10655 end Check_Pragma_Implemented;
10657 --------------------------------
10658 -- Inherit_Pragma_Implemented --
10659 --------------------------------
10661 procedure Inherit_Pragma_Implemented
10663 Iface_Subp : Entity_Id)
10665 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10666 Loc : constant Source_Ptr := Sloc (Subp);
10667 Impl_Prag : Node_Id;
10670 -- Since the implementation kind is stored as a representation item
10671 -- rather than a flag, create a pragma node.
10675 Chars => Name_Implemented,
10676 Pragma_Argument_Associations => New_List (
10677 Make_Pragma_Argument_Association (Loc,
10678 Expression => New_Occurrence_Of (Subp, Loc)),
10680 Make_Pragma_Argument_Association (Loc,
10681 Expression => Make_Identifier (Loc, Iface_Kind))));
10683 -- The pragma doesn't need to be analyzed because it is internally
10684 -- built. It is safe to directly register it as a rep item since we
10685 -- are only interested in the characters of the implementation kind.
10687 Record_Rep_Item (Subp, Impl_Prag);
10688 end Inherit_Pragma_Implemented;
10690 -- Start of processing for Check_Abstract_Overriding
10693 Op_List := Primitive_Operations (T);
10695 -- Loop to check primitive operations
10697 Elmt := First_Elmt (Op_List);
10698 while Present (Elmt) loop
10699 Subp := Node (Elmt);
10700 Alias_Subp := Alias (Subp);
10702 -- Inherited subprograms are identified by the fact that they do not
10703 -- come from source, and the associated source location is the
10704 -- location of the first subtype of the derived type.
10706 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10707 -- subprograms that "require overriding".
10709 -- Special exception, do not complain about failure to override the
10710 -- stream routines _Input and _Output, as well as the primitive
10711 -- operations used in dispatching selects since we always provide
10712 -- automatic overridings for these subprograms.
10714 -- The partial view of T may have been a private extension, for
10715 -- which inherited functions dispatching on result are abstract.
10716 -- If the full view is a null extension, there is no need for
10717 -- overriding in Ada 2005, but wrappers need to be built for them
10718 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10720 if Is_Null_Extension (T)
10721 and then Has_Controlling_Result (Subp)
10722 and then Ada_Version >= Ada_2005
10723 and then Present (Alias_Subp)
10724 and then not Comes_From_Source (Subp)
10725 and then not Is_Abstract_Subprogram (Alias_Subp)
10726 and then not Is_Access_Type (Etype (Subp))
10730 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10731 -- processing because this check is done with the aliased
10734 elsif Present (Interface_Alias (Subp)) then
10737 elsif (Is_Abstract_Subprogram (Subp)
10738 or else Requires_Overriding (Subp)
10740 (Has_Controlling_Result (Subp)
10741 and then Present (Alias_Subp)
10742 and then not Comes_From_Source (Subp)
10743 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10744 and then not Is_TSS (Subp, TSS_Stream_Input)
10745 and then not Is_TSS (Subp, TSS_Stream_Output)
10746 and then not Is_Abstract_Type (T)
10747 and then not Is_Predefined_Interface_Primitive (Subp)
10749 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10750 -- with abstract interface types because the check will be done
10751 -- with the aliased entity (otherwise we generate a duplicated
10754 and then not Present (Interface_Alias (Subp))
10756 if Present (Alias_Subp) then
10758 -- Only perform the check for a derived subprogram when the
10759 -- type has an explicit record extension. This avoids incorrect
10760 -- flagging of abstract subprograms for the case of a type
10761 -- without an extension that is derived from a formal type
10762 -- with a tagged actual (can occur within a private part).
10764 -- Ada 2005 (AI-391): In the case of an inherited function with
10765 -- a controlling result of the type, the rule does not apply if
10766 -- the type is a null extension (unless the parent function
10767 -- itself is abstract, in which case the function must still be
10768 -- be overridden). The expander will generate an overriding
10769 -- wrapper function calling the parent subprogram (see
10770 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10772 Type_Def := Type_Definition (Parent (T));
10774 if Nkind (Type_Def) = N_Derived_Type_Definition
10775 and then Present (Record_Extension_Part (Type_Def))
10777 (Ada_Version < Ada_2005
10778 or else not Is_Null_Extension (T)
10779 or else Ekind (Subp) = E_Procedure
10780 or else not Has_Controlling_Result (Subp)
10781 or else Is_Abstract_Subprogram (Alias_Subp)
10782 or else Requires_Overriding (Subp)
10783 or else Is_Access_Type (Etype (Subp)))
10785 -- Avoid reporting error in case of abstract predefined
10786 -- primitive inherited from interface type because the
10787 -- body of internally generated predefined primitives
10788 -- of tagged types are generated later by Freeze_Type
10790 if Is_Interface (Root_Type (T))
10791 and then Is_Abstract_Subprogram (Subp)
10792 and then Is_Predefined_Dispatching_Operation (Subp)
10793 and then not Comes_From_Source (Ultimate_Alias (Subp))
10797 -- A null extension is not obliged to override an inherited
10798 -- procedure subject to pragma Extensions_Visible with value
10799 -- False and at least one controlling OUT parameter
10800 -- (SPARK RM 6.1.7(6)).
10802 elsif Is_Null_Extension (T)
10803 and then Is_EVF_Procedure (Subp)
10809 ("type must be declared abstract or & overridden",
10812 -- Traverse the whole chain of aliased subprograms to
10813 -- complete the error notification. This is especially
10814 -- useful for traceability of the chain of entities when
10815 -- the subprogram corresponds with an interface
10816 -- subprogram (which may be defined in another package).
10818 if Present (Alias_Subp) then
10824 while Present (Alias (E)) loop
10826 -- Avoid reporting redundant errors on entities
10827 -- inherited from interfaces
10829 if Sloc (E) /= Sloc (T) then
10830 Error_Msg_Sloc := Sloc (E);
10832 ("\& has been inherited #", T, Subp);
10838 Error_Msg_Sloc := Sloc (E);
10840 -- AI05-0068: report if there is an overriding
10841 -- non-abstract subprogram that is invisible.
10844 and then not Is_Abstract_Subprogram (E)
10847 ("\& subprogram# is not visible",
10850 -- Clarify the case where a non-null extension must
10851 -- override inherited procedure subject to pragma
10852 -- Extensions_Visible with value False and at least
10853 -- one controlling OUT param.
10855 elsif Is_EVF_Procedure (E) then
10857 ("\& # is subject to Extensions_Visible False",
10862 ("\& has been inherited from subprogram #",
10869 -- Ada 2005 (AI-345): Protected or task type implementing
10870 -- abstract interfaces.
10872 elsif Is_Concurrent_Record_Type (T)
10873 and then Present (Interfaces (T))
10875 -- There is no need to check here RM 9.4(11.9/3) since we
10876 -- are processing the corresponding record type and the
10877 -- mode of the overriding subprograms was verified by
10878 -- Check_Conformance when the corresponding concurrent
10879 -- type declaration was analyzed.
10882 ("interface subprogram & must be overridden", T, Subp);
10884 -- Examine primitive operations of synchronized type to find
10885 -- homonyms that have the wrong profile.
10891 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10892 while Present (Prim) loop
10893 if Chars (Prim) = Chars (Subp) then
10895 ("profile is not type conformant with prefixed "
10896 & "view profile of inherited operation&",
10900 Next_Entity (Prim);
10906 Error_Msg_Node_2 := T;
10908 ("abstract subprogram& not allowed for type&", Subp);
10910 -- Also post unconditional warning on the type (unconditional
10911 -- so that if there are more than one of these cases, we get
10912 -- them all, and not just the first one).
10914 Error_Msg_Node_2 := Subp;
10915 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10918 -- A subprogram subject to pragma Extensions_Visible with value
10919 -- "True" cannot override a subprogram subject to the same pragma
10920 -- with value "False" (SPARK RM 6.1.7(5)).
10922 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10923 and then Present (Overridden_Operation (Subp))
10924 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10925 Extensions_Visible_False
10927 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10929 ("subprogram & with Extensions_Visible True cannot override "
10930 & "subprogram # with Extensions_Visible False", Subp);
10933 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10935 -- Subp is an expander-generated procedure which maps an interface
10936 -- alias to a protected wrapper. The interface alias is flagged by
10937 -- pragma Implemented. Ensure that Subp is a procedure when the
10938 -- implementation kind is By_Protected_Procedure or an entry when
10941 if Ada_Version >= Ada_2012
10942 and then Is_Hidden (Subp)
10943 and then Present (Interface_Alias (Subp))
10944 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10946 Check_Pragma_Implemented (Subp);
10949 -- Subp is an interface primitive which overrides another interface
10950 -- primitive marked with pragma Implemented.
10952 if Ada_Version >= Ada_2012
10953 and then Present (Overridden_Operation (Subp))
10954 and then Has_Rep_Pragma
10955 (Overridden_Operation (Subp), Name_Implemented)
10957 -- If the overriding routine is also marked by Implemented, check
10958 -- that the two implementation kinds are conforming.
10960 if Has_Rep_Pragma (Subp, Name_Implemented) then
10961 Check_Pragma_Implemented
10963 Iface_Subp => Overridden_Operation (Subp));
10965 -- Otherwise the overriding routine inherits the implementation
10966 -- kind from the overridden subprogram.
10969 Inherit_Pragma_Implemented
10971 Iface_Subp => Overridden_Operation (Subp));
10975 -- If the operation is a wrapper for a synchronized primitive, it
10976 -- may be called indirectly through a dispatching select. We assume
10977 -- that it will be referenced elsewhere indirectly, and suppress
10978 -- warnings about an unused entity.
10980 if Is_Primitive_Wrapper (Subp)
10981 and then Present (Wrapped_Entity (Subp))
10983 Set_Referenced (Wrapped_Entity (Subp));
10988 end Check_Abstract_Overriding;
10990 ------------------------------------------------
10991 -- Check_Access_Discriminant_Requires_Limited --
10992 ------------------------------------------------
10994 procedure Check_Access_Discriminant_Requires_Limited
10999 -- A discriminant_specification for an access discriminant shall appear
11000 -- only in the declaration for a task or protected type, or for a type
11001 -- with the reserved word 'limited' in its definition or in one of its
11002 -- ancestors (RM 3.7(10)).
11004 -- AI-0063: The proper condition is that type must be immutably limited,
11005 -- or else be a partial view.
11007 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11008 if Is_Limited_View (Current_Scope)
11010 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11011 and then Limited_Present (Parent (Current_Scope)))
11017 ("access discriminants allowed only for limited types", Loc);
11020 end Check_Access_Discriminant_Requires_Limited;
11022 -----------------------------------
11023 -- Check_Aliased_Component_Types --
11024 -----------------------------------
11026 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11030 -- ??? Also need to check components of record extensions, but not
11031 -- components of protected types (which are always limited).
11033 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11034 -- types to be unconstrained. This is safe because it is illegal to
11035 -- create access subtypes to such types with explicit discriminant
11038 if not Is_Limited_Type (T) then
11039 if Ekind (T) = E_Record_Type then
11040 C := First_Component (T);
11041 while Present (C) loop
11043 and then Has_Discriminants (Etype (C))
11044 and then not Is_Constrained (Etype (C))
11045 and then not In_Instance_Body
11046 and then Ada_Version < Ada_2005
11049 ("aliased component must be constrained (RM 3.6(11))",
11053 Next_Component (C);
11056 elsif Ekind (T) = E_Array_Type then
11057 if Has_Aliased_Components (T)
11058 and then Has_Discriminants (Component_Type (T))
11059 and then not Is_Constrained (Component_Type (T))
11060 and then not In_Instance_Body
11061 and then Ada_Version < Ada_2005
11064 ("aliased component type must be constrained (RM 3.6(11))",
11069 end Check_Aliased_Component_Types;
11071 ---------------------------------------
11072 -- Check_Anonymous_Access_Components --
11073 ---------------------------------------
11075 procedure Check_Anonymous_Access_Components
11076 (Typ_Decl : Node_Id;
11079 Comp_List : Node_Id)
11081 Loc : constant Source_Ptr := Sloc (Typ_Decl);
11082 Anon_Access : Entity_Id;
11085 Comp_Def : Node_Id;
11087 Type_Def : Node_Id;
11089 procedure Build_Incomplete_Type_Declaration;
11090 -- If the record type contains components that include an access to the
11091 -- current record, then create an incomplete type declaration for the
11092 -- record, to be used as the designated type of the anonymous access.
11093 -- This is done only once, and only if there is no previous partial
11094 -- view of the type.
11096 function Designates_T (Subt : Node_Id) return Boolean;
11097 -- Check whether a node designates the enclosing record type, or 'Class
11100 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11101 -- Check whether an access definition includes a reference to
11102 -- the enclosing record type. The reference can be a subtype mark
11103 -- in the access definition itself, a 'Class attribute reference, or
11104 -- recursively a reference appearing in a parameter specification
11105 -- or result definition of an access_to_subprogram definition.
11107 --------------------------------------
11108 -- Build_Incomplete_Type_Declaration --
11109 --------------------------------------
11111 procedure Build_Incomplete_Type_Declaration is
11116 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11117 -- it's "is new ... with record" or else "is tagged record ...".
11119 Is_Tagged : constant Boolean :=
11120 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
11122 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
11124 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
11125 and then Tagged_Present (Type_Definition (Typ_Decl)));
11128 -- If there is a previous partial view, no need to create a new one
11129 -- If the partial view, given by Prev, is incomplete, If Prev is
11130 -- a private declaration, full declaration is flagged accordingly.
11132 if Prev /= Typ then
11134 Make_Class_Wide_Type (Prev);
11135 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11136 Set_Etype (Class_Wide_Type (Typ), Typ);
11141 elsif Has_Private_Declaration (Typ) then
11143 -- If we refer to T'Class inside T, and T is the completion of a
11144 -- private type, then make sure the class-wide type exists.
11147 Make_Class_Wide_Type (Typ);
11152 -- If there was a previous anonymous access type, the incomplete
11153 -- type declaration will have been created already.
11155 elsif Present (Current_Entity (Typ))
11156 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11157 and then Full_View (Current_Entity (Typ)) = Typ
11160 and then Comes_From_Source (Current_Entity (Typ))
11161 and then not Is_Tagged_Type (Current_Entity (Typ))
11163 Make_Class_Wide_Type (Typ);
11165 ("incomplete view of tagged type should be declared tagged??",
11166 Parent (Current_Entity (Typ)));
11171 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11172 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11174 -- Type has already been inserted into the current scope. Remove
11175 -- it, and add incomplete declaration for type, so that subsequent
11176 -- anonymous access types can use it. The entity is unchained from
11177 -- the homonym list and from immediate visibility. After analysis,
11178 -- the entity in the incomplete declaration becomes immediately
11179 -- visible in the record declaration that follows.
11181 H := Current_Entity (Typ);
11184 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11187 and then Homonym (H) /= Typ
11189 H := Homonym (Typ);
11192 Set_Homonym (H, Homonym (Typ));
11195 Insert_Before (Typ_Decl, Decl);
11197 Set_Full_View (Inc_T, Typ);
11201 -- Create a common class-wide type for both views, and set the
11202 -- Etype of the class-wide type to the full view.
11204 Make_Class_Wide_Type (Inc_T);
11205 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11206 Set_Etype (Class_Wide_Type (Typ), Typ);
11209 end Build_Incomplete_Type_Declaration;
11215 function Designates_T (Subt : Node_Id) return Boolean is
11216 Type_Id : constant Name_Id := Chars (Typ);
11218 function Names_T (Nam : Node_Id) return Boolean;
11219 -- The record type has not been introduced in the current scope
11220 -- yet, so we must examine the name of the type itself, either
11221 -- an identifier T, or an expanded name of the form P.T, where
11222 -- P denotes the current scope.
11228 function Names_T (Nam : Node_Id) return Boolean is
11230 if Nkind (Nam) = N_Identifier then
11231 return Chars (Nam) = Type_Id;
11233 elsif Nkind (Nam) = N_Selected_Component then
11234 if Chars (Selector_Name (Nam)) = Type_Id then
11235 if Nkind (Prefix (Nam)) = N_Identifier then
11236 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11238 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11239 return Chars (Selector_Name (Prefix (Nam))) =
11240 Chars (Current_Scope);
11254 -- Start of processing for Designates_T
11257 if Nkind (Subt) = N_Identifier then
11258 return Chars (Subt) = Type_Id;
11260 -- Reference can be through an expanded name which has not been
11261 -- analyzed yet, and which designates enclosing scopes.
11263 elsif Nkind (Subt) = N_Selected_Component then
11264 if Names_T (Subt) then
11267 -- Otherwise it must denote an entity that is already visible.
11268 -- The access definition may name a subtype of the enclosing
11269 -- type, if there is a previous incomplete declaration for it.
11272 Find_Selected_Component (Subt);
11274 Is_Entity_Name (Subt)
11275 and then Scope (Entity (Subt)) = Current_Scope
11277 (Chars (Base_Type (Entity (Subt))) = Type_Id
11279 (Is_Class_Wide_Type (Entity (Subt))
11281 Chars (Etype (Base_Type (Entity (Subt)))) =
11285 -- A reference to the current type may appear as the prefix of
11286 -- a 'Class attribute.
11288 elsif Nkind (Subt) = N_Attribute_Reference
11289 and then Attribute_Name (Subt) = Name_Class
11291 return Names_T (Prefix (Subt));
11302 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11303 Param_Spec : Node_Id;
11305 Acc_Subprg : constant Node_Id :=
11306 Access_To_Subprogram_Definition (Acc_Def);
11309 if No (Acc_Subprg) then
11310 return Designates_T (Subtype_Mark (Acc_Def));
11313 -- Component is an access_to_subprogram: examine its formals,
11314 -- and result definition in the case of an access_to_function.
11316 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11317 while Present (Param_Spec) loop
11318 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11319 and then Mentions_T (Parameter_Type (Param_Spec))
11323 elsif Designates_T (Parameter_Type (Param_Spec)) then
11330 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11331 if Nkind (Result_Definition (Acc_Subprg)) =
11332 N_Access_Definition
11334 return Mentions_T (Result_Definition (Acc_Subprg));
11336 return Designates_T (Result_Definition (Acc_Subprg));
11343 -- Start of processing for Check_Anonymous_Access_Components
11346 if No (Comp_List) then
11350 Comp := First (Component_Items (Comp_List));
11351 while Present (Comp) loop
11352 if Nkind (Comp) = N_Component_Declaration
11354 (Access_Definition (Component_Definition (Comp)))
11356 Mentions_T (Access_Definition (Component_Definition (Comp)))
11358 Comp_Def := Component_Definition (Comp);
11360 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11362 Build_Incomplete_Type_Declaration;
11363 Anon_Access := Make_Temporary (Loc, 'S');
11365 -- Create a declaration for the anonymous access type: either
11366 -- an access_to_object or an access_to_subprogram.
11368 if Present (Acc_Def) then
11369 if Nkind (Acc_Def) = N_Access_Function_Definition then
11371 Make_Access_Function_Definition (Loc,
11372 Parameter_Specifications =>
11373 Parameter_Specifications (Acc_Def),
11374 Result_Definition => Result_Definition (Acc_Def));
11377 Make_Access_Procedure_Definition (Loc,
11378 Parameter_Specifications =>
11379 Parameter_Specifications (Acc_Def));
11384 Make_Access_To_Object_Definition (Loc,
11385 Subtype_Indication =>
11387 (Subtype_Mark (Access_Definition (Comp_Def))));
11389 Set_Constant_Present
11390 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11392 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11395 Set_Null_Exclusion_Present
11397 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11400 Make_Full_Type_Declaration (Loc,
11401 Defining_Identifier => Anon_Access,
11402 Type_Definition => Type_Def);
11404 Insert_Before (Typ_Decl, Decl);
11407 -- If an access to subprogram, create the extra formals
11409 if Present (Acc_Def) then
11410 Create_Extra_Formals (Designated_Type (Anon_Access));
11412 -- If an access to object, preserve entity of designated type,
11413 -- for ASIS use, before rewriting the component definition.
11420 Desig := Entity (Subtype_Indication (Type_Def));
11422 -- If the access definition is to the current record,
11423 -- the visible entity at this point is an incomplete
11424 -- type. Retrieve the full view to simplify ASIS queries
11426 if Ekind (Desig) = E_Incomplete_Type then
11427 Desig := Full_View (Desig);
11431 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11436 Make_Component_Definition (Loc,
11437 Subtype_Indication =>
11438 New_Occurrence_Of (Anon_Access, Loc)));
11440 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11441 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11443 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11446 Set_Is_Local_Anonymous_Access (Anon_Access);
11452 if Present (Variant_Part (Comp_List)) then
11456 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11457 while Present (V) loop
11458 Check_Anonymous_Access_Components
11459 (Typ_Decl, Typ, Prev, Component_List (V));
11460 Next_Non_Pragma (V);
11464 end Check_Anonymous_Access_Components;
11466 ----------------------
11467 -- Check_Completion --
11468 ----------------------
11470 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11473 procedure Post_Error;
11474 -- Post error message for lack of completion for entity E
11480 procedure Post_Error is
11481 procedure Missing_Body;
11482 -- Output missing body message
11488 procedure Missing_Body is
11490 -- Spec is in same unit, so we can post on spec
11492 if In_Same_Source_Unit (Body_Id, E) then
11493 Error_Msg_N ("missing body for &", E);
11495 -- Spec is in a separate unit, so we have to post on the body
11498 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11502 -- Start of processing for Post_Error
11505 if not Comes_From_Source (E) then
11506 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11508 -- It may be an anonymous protected type created for a
11509 -- single variable. Post error on variable, if present.
11515 Var := First_Entity (Current_Scope);
11516 while Present (Var) loop
11517 exit when Etype (Var) = E
11518 and then Comes_From_Source (Var);
11523 if Present (Var) then
11530 -- If a generated entity has no completion, then either previous
11531 -- semantic errors have disabled the expansion phase, or else we had
11532 -- missing subunits, or else we are compiling without expansion,
11533 -- or else something is very wrong.
11535 if not Comes_From_Source (E) then
11537 (Serious_Errors_Detected > 0
11538 or else Configurable_Run_Time_Violations > 0
11539 or else Subunits_Missing
11540 or else not Expander_Active);
11543 -- Here for source entity
11546 -- Here if no body to post the error message, so we post the error
11547 -- on the declaration that has no completion. This is not really
11548 -- the right place to post it, think about this later ???
11550 if No (Body_Id) then
11551 if Is_Type (E) then
11553 ("missing full declaration for }", Parent (E), E);
11555 Error_Msg_NE ("missing body for &", Parent (E), E);
11558 -- Package body has no completion for a declaration that appears
11559 -- in the corresponding spec. Post error on the body, with a
11560 -- reference to the non-completed declaration.
11563 Error_Msg_Sloc := Sloc (E);
11565 if Is_Type (E) then
11566 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11568 elsif Is_Overloadable (E)
11569 and then Current_Entity_In_Scope (E) /= E
11571 -- It may be that the completion is mistyped and appears as
11572 -- a distinct overloading of the entity.
11575 Candidate : constant Entity_Id :=
11576 Current_Entity_In_Scope (E);
11577 Decl : constant Node_Id :=
11578 Unit_Declaration_Node (Candidate);
11581 if Is_Overloadable (Candidate)
11582 and then Ekind (Candidate) = Ekind (E)
11583 and then Nkind (Decl) = N_Subprogram_Body
11584 and then Acts_As_Spec (Decl)
11586 Check_Type_Conformant (Candidate, E);
11602 Pack_Id : constant Entity_Id := Current_Scope;
11604 -- Start of processing for Check_Completion
11607 E := First_Entity (Pack_Id);
11608 while Present (E) loop
11609 if Is_Intrinsic_Subprogram (E) then
11612 -- The following situation requires special handling: a child unit
11613 -- that appears in the context clause of the body of its parent:
11615 -- procedure Parent.Child (...);
11617 -- with Parent.Child;
11618 -- package body Parent is
11620 -- Here Parent.Child appears as a local entity, but should not be
11621 -- flagged as requiring completion, because it is a compilation
11624 -- Ignore missing completion for a subprogram that does not come from
11625 -- source (including the _Call primitive operation of RAS types,
11626 -- which has to have the flag Comes_From_Source for other purposes):
11627 -- we assume that the expander will provide the missing completion.
11628 -- In case of previous errors, other expansion actions that provide
11629 -- bodies for null procedures with not be invoked, so inhibit message
11632 -- Note that E_Operator is not in the list that follows, because
11633 -- this kind is reserved for predefined operators, that are
11634 -- intrinsic and do not need completion.
11636 elsif Ekind_In (E, E_Function,
11638 E_Generic_Function,
11639 E_Generic_Procedure)
11641 if Has_Completion (E) then
11644 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11647 elsif Is_Subprogram (E)
11648 and then (not Comes_From_Source (E)
11649 or else Chars (E) = Name_uCall)
11654 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11658 elsif Nkind (Parent (E)) = N_Procedure_Specification
11659 and then Null_Present (Parent (E))
11660 and then Serious_Errors_Detected > 0
11668 elsif Is_Entry (E) then
11669 if not Has_Completion (E) and then
11670 (Ekind (Scope (E)) = E_Protected_Object
11671 or else Ekind (Scope (E)) = E_Protected_Type)
11676 elsif Is_Package_Or_Generic_Package (E) then
11677 if Unit_Requires_Body (E) then
11678 if not Has_Completion (E)
11679 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11685 elsif not Is_Child_Unit (E) then
11686 May_Need_Implicit_Body (E);
11689 -- A formal incomplete type (Ada 2012) does not require a completion;
11690 -- other incomplete type declarations do.
11692 elsif Ekind (E) = E_Incomplete_Type
11693 and then No (Underlying_Type (E))
11694 and then not Is_Generic_Type (E)
11698 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11699 and then not Has_Completion (E)
11703 -- A single task declared in the current scope is a constant, verify
11704 -- that the body of its anonymous type is in the same scope. If the
11705 -- task is defined elsewhere, this may be a renaming declaration for
11706 -- which no completion is needed.
11708 elsif Ekind (E) = E_Constant
11709 and then Ekind (Etype (E)) = E_Task_Type
11710 and then not Has_Completion (Etype (E))
11711 and then Scope (Etype (E)) = Current_Scope
11715 elsif Ekind (E) = E_Protected_Object
11716 and then not Has_Completion (Etype (E))
11720 elsif Ekind (E) = E_Record_Type then
11721 if Is_Tagged_Type (E) then
11722 Check_Abstract_Overriding (E);
11723 Check_Conventions (E);
11726 Check_Aliased_Component_Types (E);
11728 elsif Ekind (E) = E_Array_Type then
11729 Check_Aliased_Component_Types (E);
11735 end Check_Completion;
11737 ------------------------------------
11738 -- Check_CPP_Type_Has_No_Defaults --
11739 ------------------------------------
11741 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11742 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11747 -- Obtain the component list
11749 if Nkind (Tdef) = N_Record_Definition then
11750 Clist := Component_List (Tdef);
11751 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11752 Clist := Component_List (Record_Extension_Part (Tdef));
11755 -- Check all components to ensure no default expressions
11757 if Present (Clist) then
11758 Comp := First (Component_Items (Clist));
11759 while Present (Comp) loop
11760 if Present (Expression (Comp)) then
11762 ("component of imported 'C'P'P type cannot have "
11763 & "default expression", Expression (Comp));
11769 end Check_CPP_Type_Has_No_Defaults;
11771 ----------------------------
11772 -- Check_Delta_Expression --
11773 ----------------------------
11775 procedure Check_Delta_Expression (E : Node_Id) is
11777 if not (Is_Real_Type (Etype (E))) then
11778 Wrong_Type (E, Any_Real);
11780 elsif not Is_OK_Static_Expression (E) then
11781 Flag_Non_Static_Expr
11782 ("non-static expression used for delta value!", E);
11784 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11785 Error_Msg_N ("delta expression must be positive", E);
11791 -- If any of above errors occurred, then replace the incorrect
11792 -- expression by the real 0.1, which should prevent further errors.
11795 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11796 Analyze_And_Resolve (E, Standard_Float);
11797 end Check_Delta_Expression;
11799 -----------------------------
11800 -- Check_Digits_Expression --
11801 -----------------------------
11803 procedure Check_Digits_Expression (E : Node_Id) is
11805 if not (Is_Integer_Type (Etype (E))) then
11806 Wrong_Type (E, Any_Integer);
11808 elsif not Is_OK_Static_Expression (E) then
11809 Flag_Non_Static_Expr
11810 ("non-static expression used for digits value!", E);
11812 elsif Expr_Value (E) <= 0 then
11813 Error_Msg_N ("digits value must be greater than zero", E);
11819 -- If any of above errors occurred, then replace the incorrect
11820 -- expression by the integer 1, which should prevent further errors.
11822 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11823 Analyze_And_Resolve (E, Standard_Integer);
11825 end Check_Digits_Expression;
11827 --------------------------
11828 -- Check_Initialization --
11829 --------------------------
11831 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11833 -- Special processing for limited types
11835 if Is_Limited_Type (T)
11836 and then not In_Instance
11837 and then not In_Inlined_Body
11839 if not OK_For_Limited_Init (T, Exp) then
11841 -- In GNAT mode, this is just a warning, to allow it to be evilly
11842 -- turned off. Otherwise it is a real error.
11846 ("??cannot initialize entities of limited type!", Exp);
11848 elsif Ada_Version < Ada_2005 then
11850 -- The side effect removal machinery may generate illegal Ada
11851 -- code to avoid the usage of access types and 'reference in
11852 -- SPARK mode. Since this is legal code with respect to theorem
11853 -- proving, do not emit the error.
11856 and then Nkind (Exp) = N_Function_Call
11857 and then Nkind (Parent (Exp)) = N_Object_Declaration
11858 and then not Comes_From_Source
11859 (Defining_Identifier (Parent (Exp)))
11865 ("cannot initialize entities of limited type", Exp);
11866 Explain_Limited_Type (T, Exp);
11870 -- Specialize error message according to kind of illegal
11871 -- initial expression.
11873 if Nkind (Exp) = N_Type_Conversion
11874 and then Nkind (Expression (Exp)) = N_Function_Call
11876 -- No error for internally-generated object declarations,
11877 -- which can come from build-in-place assignment statements.
11879 if Nkind (Parent (Exp)) = N_Object_Declaration
11880 and then not Comes_From_Source
11881 (Defining_Identifier (Parent (Exp)))
11887 ("illegal context for call to function with limited "
11893 ("initialization of limited object requires aggregate or "
11894 & "function call", Exp);
11900 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11901 -- set unless we can be sure that no range check is required.
11903 if (GNATprove_Mode or not Expander_Active)
11904 and then Is_Scalar_Type (T)
11905 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11907 Set_Do_Range_Check (Exp);
11909 end Check_Initialization;
11911 ----------------------
11912 -- Check_Interfaces --
11913 ----------------------
11915 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11916 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11919 Iface_Def : Node_Id;
11920 Iface_Typ : Entity_Id;
11921 Parent_Node : Node_Id;
11923 Is_Task : Boolean := False;
11924 -- Set True if parent type or any progenitor is a task interface
11926 Is_Protected : Boolean := False;
11927 -- Set True if parent type or any progenitor is a protected interface
11929 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11930 -- Check that a progenitor is compatible with declaration. If an error
11931 -- message is output, it is posted on Error_Node.
11937 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11938 Iface_Id : constant Entity_Id :=
11939 Defining_Identifier (Parent (Iface_Def));
11940 Type_Def : Node_Id;
11943 if Nkind (N) = N_Private_Extension_Declaration then
11946 Type_Def := Type_Definition (N);
11949 if Is_Task_Interface (Iface_Id) then
11952 elsif Is_Protected_Interface (Iface_Id) then
11953 Is_Protected := True;
11956 if Is_Synchronized_Interface (Iface_Id) then
11958 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11959 -- extension derived from a synchronized interface must explicitly
11960 -- be declared synchronized, because the full view will be a
11961 -- synchronized type.
11963 if Nkind (N) = N_Private_Extension_Declaration then
11964 if not Synchronized_Present (N) then
11966 ("private extension of& must be explicitly synchronized",
11970 -- However, by 3.9.4(16/2), a full type that is a record extension
11971 -- is never allowed to derive from a synchronized interface (note
11972 -- that interfaces must be excluded from this check, because those
11973 -- are represented by derived type definitions in some cases).
11975 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11976 and then not Interface_Present (Type_Definition (N))
11978 Error_Msg_N ("record extension cannot derive from synchronized "
11979 & "interface", Error_Node);
11983 -- Check that the characteristics of the progenitor are compatible
11984 -- with the explicit qualifier in the declaration.
11985 -- The check only applies to qualifiers that come from source.
11986 -- Limited_Present also appears in the declaration of corresponding
11987 -- records, and the check does not apply to them.
11989 if Limited_Present (Type_Def)
11991 Is_Concurrent_Record_Type (Defining_Identifier (N))
11993 if Is_Limited_Interface (Parent_Type)
11994 and then not Is_Limited_Interface (Iface_Id)
11997 ("progenitor & must be limited interface",
11998 Error_Node, Iface_Id);
12001 (Task_Present (Iface_Def)
12002 or else Protected_Present (Iface_Def)
12003 or else Synchronized_Present (Iface_Def))
12004 and then Nkind (N) /= N_Private_Extension_Declaration
12005 and then not Error_Posted (N)
12008 ("progenitor & must be limited interface",
12009 Error_Node, Iface_Id);
12012 -- Protected interfaces can only inherit from limited, synchronized
12013 -- or protected interfaces.
12015 elsif Nkind (N) = N_Full_Type_Declaration
12016 and then Protected_Present (Type_Def)
12018 if Limited_Present (Iface_Def)
12019 or else Synchronized_Present (Iface_Def)
12020 or else Protected_Present (Iface_Def)
12024 elsif Task_Present (Iface_Def) then
12025 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12026 & "from task interface", Error_Node);
12029 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12030 & "from non-limited interface", Error_Node);
12033 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12034 -- limited and synchronized.
12036 elsif Synchronized_Present (Type_Def) then
12037 if Limited_Present (Iface_Def)
12038 or else Synchronized_Present (Iface_Def)
12042 elsif Protected_Present (Iface_Def)
12043 and then Nkind (N) /= N_Private_Extension_Declaration
12045 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12046 & "from protected interface", Error_Node);
12048 elsif Task_Present (Iface_Def)
12049 and then Nkind (N) /= N_Private_Extension_Declaration
12051 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12052 & "from task interface", Error_Node);
12054 elsif not Is_Limited_Interface (Iface_Id) then
12055 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12056 & "from non-limited interface", Error_Node);
12059 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12060 -- synchronized or task interfaces.
12062 elsif Nkind (N) = N_Full_Type_Declaration
12063 and then Task_Present (Type_Def)
12065 if Limited_Present (Iface_Def)
12066 or else Synchronized_Present (Iface_Def)
12067 or else Task_Present (Iface_Def)
12071 elsif Protected_Present (Iface_Def) then
12072 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12073 & "protected interface", Error_Node);
12076 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12077 & "non-limited interface", Error_Node);
12082 -- Start of processing for Check_Interfaces
12085 if Is_Interface (Parent_Type) then
12086 if Is_Task_Interface (Parent_Type) then
12089 elsif Is_Protected_Interface (Parent_Type) then
12090 Is_Protected := True;
12094 if Nkind (N) = N_Private_Extension_Declaration then
12096 -- Check that progenitors are compatible with declaration
12098 Iface := First (Interface_List (Def));
12099 while Present (Iface) loop
12100 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12102 Parent_Node := Parent (Base_Type (Iface_Typ));
12103 Iface_Def := Type_Definition (Parent_Node);
12105 if not Is_Interface (Iface_Typ) then
12106 Diagnose_Interface (Iface, Iface_Typ);
12108 Check_Ifaces (Iface_Def, Iface);
12114 if Is_Task and Is_Protected then
12116 ("type cannot derive from task and protected interface", N);
12122 -- Full type declaration of derived type.
12123 -- Check compatibility with parent if it is interface type
12125 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12126 and then Is_Interface (Parent_Type)
12128 Parent_Node := Parent (Parent_Type);
12130 -- More detailed checks for interface varieties
12133 (Iface_Def => Type_Definition (Parent_Node),
12134 Error_Node => Subtype_Indication (Type_Definition (N)));
12137 Iface := First (Interface_List (Def));
12138 while Present (Iface) loop
12139 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12141 Parent_Node := Parent (Base_Type (Iface_Typ));
12142 Iface_Def := Type_Definition (Parent_Node);
12144 if not Is_Interface (Iface_Typ) then
12145 Diagnose_Interface (Iface, Iface_Typ);
12148 -- "The declaration of a specific descendant of an interface
12149 -- type freezes the interface type" RM 13.14
12151 Freeze_Before (N, Iface_Typ);
12152 Check_Ifaces (Iface_Def, Error_Node => Iface);
12158 if Is_Task and Is_Protected then
12160 ("type cannot derive from task and protected interface", N);
12162 end Check_Interfaces;
12164 ------------------------------------
12165 -- Check_Or_Process_Discriminants --
12166 ------------------------------------
12168 -- If an incomplete or private type declaration was already given for the
12169 -- type, the discriminants may have already been processed if they were
12170 -- present on the incomplete declaration. In this case a full conformance
12171 -- check has been performed in Find_Type_Name, and we then recheck here
12172 -- some properties that can't be checked on the partial view alone.
12173 -- Otherwise we call Process_Discriminants.
12175 procedure Check_Or_Process_Discriminants
12178 Prev : Entity_Id := Empty)
12181 if Has_Discriminants (T) then
12183 -- Discriminants are already set on T if they were already present
12184 -- on the partial view. Make them visible to component declarations.
12188 -- Discriminant on T (full view) referencing expr on partial view
12190 Prev_D : Entity_Id;
12191 -- Entity of corresponding discriminant on partial view
12194 -- Discriminant specification for full view, expression is
12195 -- the syntactic copy on full view (which has been checked for
12196 -- conformance with partial view), only used here to post error
12200 D := First_Discriminant (T);
12201 New_D := First (Discriminant_Specifications (N));
12202 while Present (D) loop
12203 Prev_D := Current_Entity (D);
12204 Set_Current_Entity (D);
12205 Set_Is_Immediately_Visible (D);
12206 Set_Homonym (D, Prev_D);
12208 -- Handle the case where there is an untagged partial view and
12209 -- the full view is tagged: must disallow discriminants with
12210 -- defaults, unless compiling for Ada 2012, which allows a
12211 -- limited tagged type to have defaulted discriminants (see
12212 -- AI05-0214). However, suppress error here if it was already
12213 -- reported on the default expression of the partial view.
12215 if Is_Tagged_Type (T)
12216 and then Present (Expression (Parent (D)))
12217 and then (not Is_Limited_Type (Current_Scope)
12218 or else Ada_Version < Ada_2012)
12219 and then not Error_Posted (Expression (Parent (D)))
12221 if Ada_Version >= Ada_2012 then
12223 ("discriminants of nonlimited tagged type cannot have "
12225 Expression (New_D));
12228 ("discriminants of tagged type cannot have defaults",
12229 Expression (New_D));
12233 -- Ada 2005 (AI-230): Access discriminant allowed in
12234 -- non-limited record types.
12236 if Ada_Version < Ada_2005 then
12238 -- This restriction gets applied to the full type here. It
12239 -- has already been applied earlier to the partial view.
12241 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12244 Next_Discriminant (D);
12249 elsif Present (Discriminant_Specifications (N)) then
12250 Process_Discriminants (N, Prev);
12252 end Check_Or_Process_Discriminants;
12254 ----------------------
12255 -- Check_Real_Bound --
12256 ----------------------
12258 procedure Check_Real_Bound (Bound : Node_Id) is
12260 if not Is_Real_Type (Etype (Bound)) then
12262 ("bound in real type definition must be of real type", Bound);
12264 elsif not Is_OK_Static_Expression (Bound) then
12265 Flag_Non_Static_Expr
12266 ("non-static expression used for real type bound!", Bound);
12273 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12275 Resolve (Bound, Standard_Float);
12276 end Check_Real_Bound;
12278 ------------------------------
12279 -- Complete_Private_Subtype --
12280 ------------------------------
12282 procedure Complete_Private_Subtype
12285 Full_Base : Entity_Id;
12286 Related_Nod : Node_Id)
12288 Save_Next_Entity : Entity_Id;
12289 Save_Homonym : Entity_Id;
12292 -- Set semantic attributes for (implicit) private subtype completion.
12293 -- If the full type has no discriminants, then it is a copy of the
12294 -- full view of the base. Otherwise, it is a subtype of the base with
12295 -- a possible discriminant constraint. Save and restore the original
12296 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12297 -- not corrupt the entity chain.
12299 -- Note that the type of the full view is the same entity as the type
12300 -- of the partial view. In this fashion, the subtype has access to the
12301 -- correct view of the parent.
12303 Save_Next_Entity := Next_Entity (Full);
12304 Save_Homonym := Homonym (Priv);
12306 case Ekind (Full_Base) is
12307 when Class_Wide_Kind
12314 Copy_Node (Priv, Full);
12316 Set_Has_Discriminants
12317 (Full, Has_Discriminants (Full_Base));
12318 Set_Has_Unknown_Discriminants
12319 (Full, Has_Unknown_Discriminants (Full_Base));
12320 Set_First_Entity (Full, First_Entity (Full_Base));
12321 Set_Last_Entity (Full, Last_Entity (Full_Base));
12323 -- If the underlying base type is constrained, we know that the
12324 -- full view of the subtype is constrained as well (the converse
12325 -- is not necessarily true).
12327 if Is_Constrained (Full_Base) then
12328 Set_Is_Constrained (Full);
12332 Copy_Node (Full_Base, Full);
12334 Set_Chars (Full, Chars (Priv));
12335 Conditional_Delay (Full, Priv);
12336 Set_Sloc (Full, Sloc (Priv));
12339 Link_Entities (Full, Save_Next_Entity);
12340 Set_Homonym (Full, Save_Homonym);
12341 Set_Associated_Node_For_Itype (Full, Related_Nod);
12343 -- Set common attributes for all subtypes: kind, convention, etc.
12345 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12346 Set_Convention (Full, Convention (Full_Base));
12348 -- The Etype of the full view is inconsistent. Gigi needs to see the
12349 -- structural full view, which is what the current scheme gives: the
12350 -- Etype of the full view is the etype of the full base. However, if the
12351 -- full base is a derived type, the full view then looks like a subtype
12352 -- of the parent, not a subtype of the full base. If instead we write:
12354 -- Set_Etype (Full, Full_Base);
12356 -- then we get inconsistencies in the front-end (confusion between
12357 -- views). Several outstanding bugs are related to this ???
12359 Set_Is_First_Subtype (Full, False);
12360 Set_Scope (Full, Scope (Priv));
12361 Set_Size_Info (Full, Full_Base);
12362 Set_RM_Size (Full, RM_Size (Full_Base));
12363 Set_Is_Itype (Full);
12365 -- For the unusual case of a type with unknown discriminants whose
12366 -- completion is an array, use the proper full base.
12368 if Is_Array_Type (Full_Base)
12369 and then Has_Unknown_Discriminants (Priv)
12371 Set_Etype (Full, Full_Base);
12374 -- A subtype of a private-type-without-discriminants, whose full-view
12375 -- has discriminants with default expressions, is not constrained.
12377 if not Has_Discriminants (Priv) then
12378 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12380 if Has_Discriminants (Full_Base) then
12381 Set_Discriminant_Constraint
12382 (Full, Discriminant_Constraint (Full_Base));
12384 -- The partial view may have been indefinite, the full view
12387 Set_Has_Unknown_Discriminants
12388 (Full, Has_Unknown_Discriminants (Full_Base));
12392 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12393 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12395 -- Freeze the private subtype entity if its parent is delayed, and not
12396 -- already frozen. We skip this processing if the type is an anonymous
12397 -- subtype of a record component, or is the corresponding record of a
12398 -- protected type, since these are processed when the enclosing type
12399 -- is frozen. If the parent type is declared in a nested package then
12400 -- the freezing of the private and full views also happens later.
12402 if not Is_Type (Scope (Full)) then
12404 and then In_Same_Source_Unit (Full, Full_Base)
12405 and then Scope (Full_Base) /= Scope (Full)
12407 Set_Has_Delayed_Freeze (Full);
12408 Set_Has_Delayed_Freeze (Priv);
12411 Set_Has_Delayed_Freeze (Full,
12412 Has_Delayed_Freeze (Full_Base)
12413 and then not Is_Frozen (Full_Base));
12417 Set_Freeze_Node (Full, Empty);
12418 Set_Is_Frozen (Full, False);
12419 Set_Full_View (Priv, Full);
12421 if Has_Discriminants (Full) then
12422 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12423 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12425 if Has_Unknown_Discriminants (Full) then
12426 Set_Discriminant_Constraint (Full, No_Elist);
12430 if Ekind (Full_Base) = E_Record_Type
12431 and then Has_Discriminants (Full_Base)
12432 and then Has_Discriminants (Priv) -- might not, if errors
12433 and then not Has_Unknown_Discriminants (Priv)
12434 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12436 Create_Constrained_Components
12437 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12439 -- If the full base is itself derived from private, build a congruent
12440 -- subtype of its underlying type, for use by the back end. For a
12441 -- constrained record component, the declaration cannot be placed on
12442 -- the component list, but it must nevertheless be built an analyzed, to
12443 -- supply enough information for Gigi to compute the size of component.
12445 elsif Ekind (Full_Base) in Private_Kind
12446 and then Is_Derived_Type (Full_Base)
12447 and then Has_Discriminants (Full_Base)
12448 and then (Ekind (Current_Scope) /= E_Record_Subtype)
12450 if not Is_Itype (Priv)
12452 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
12454 Build_Underlying_Full_View
12455 (Parent (Priv), Full, Etype (Full_Base));
12457 elsif Nkind (Related_Nod) = N_Component_Declaration then
12458 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
12461 elsif Is_Record_Type (Full_Base) then
12463 -- Show Full is simply a renaming of Full_Base
12465 Set_Cloned_Subtype (Full, Full_Base);
12468 -- It is unsafe to share the bounds of a scalar type, because the Itype
12469 -- is elaborated on demand, and if a bound is non-static then different
12470 -- orders of elaboration in different units will lead to different
12471 -- external symbols.
12473 if Is_Scalar_Type (Full_Base) then
12474 Set_Scalar_Range (Full,
12475 Make_Range (Sloc (Related_Nod),
12477 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12479 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12481 -- This completion inherits the bounds of the full parent, but if
12482 -- the parent is an unconstrained floating point type, so is the
12485 if Is_Floating_Point_Type (Full_Base) then
12486 Set_Includes_Infinities
12487 (Scalar_Range (Full), Has_Infinities (Full_Base));
12491 -- ??? It seems that a lot of fields are missing that should be copied
12492 -- from Full_Base to Full. Here are some that are introduced in a
12493 -- non-disruptive way but a cleanup is necessary.
12495 if Is_Tagged_Type (Full_Base) then
12496 Set_Is_Tagged_Type (Full);
12497 Set_Direct_Primitive_Operations
12498 (Full, Direct_Primitive_Operations (Full_Base));
12499 Set_No_Tagged_Streams_Pragma
12500 (Full, No_Tagged_Streams_Pragma (Full_Base));
12502 -- Inherit class_wide type of full_base in case the partial view was
12503 -- not tagged. Otherwise it has already been created when the private
12504 -- subtype was analyzed.
12506 if No (Class_Wide_Type (Full)) then
12507 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12510 -- If this is a subtype of a protected or task type, constrain its
12511 -- corresponding record, unless this is a subtype without constraints,
12512 -- i.e. a simple renaming as with an actual subtype in an instance.
12514 elsif Is_Concurrent_Type (Full_Base) then
12515 if Has_Discriminants (Full)
12516 and then Present (Corresponding_Record_Type (Full_Base))
12518 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12520 Set_Corresponding_Record_Type (Full,
12521 Constrain_Corresponding_Record
12522 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12525 Set_Corresponding_Record_Type (Full,
12526 Corresponding_Record_Type (Full_Base));
12530 -- Link rep item chain, and also setting of Has_Predicates from private
12531 -- subtype to full subtype, since we will need these on the full subtype
12532 -- to create the predicate function. Note that the full subtype may
12533 -- already have rep items, inherited from the full view of the base
12534 -- type, so we must be sure not to overwrite these entries.
12539 Next_Item : Node_Id;
12540 Priv_Item : Node_Id;
12543 Item := First_Rep_Item (Full);
12544 Priv_Item := First_Rep_Item (Priv);
12546 -- If no existing rep items on full type, we can just link directly
12547 -- to the list of items on the private type, if any exist.. Same if
12548 -- the rep items are only those inherited from the base
12551 or else Nkind (Item) /= N_Aspect_Specification
12552 or else Entity (Item) = Full_Base)
12553 and then Present (First_Rep_Item (Priv))
12555 Set_First_Rep_Item (Full, Priv_Item);
12557 -- Otherwise, search to the end of items currently linked to the full
12558 -- subtype and append the private items to the end. However, if Priv
12559 -- and Full already have the same list of rep items, then the append
12560 -- is not done, as that would create a circularity.
12562 -- The partial view may have a predicate and the rep item lists of
12563 -- both views agree when inherited from the same ancestor. In that
12564 -- case, simply propagate the list from one view to the other.
12565 -- A more complex analysis needed here ???
12567 elsif Present (Priv_Item)
12568 and then Item = Next_Rep_Item (Priv_Item)
12570 Set_First_Rep_Item (Full, Priv_Item);
12572 elsif Item /= Priv_Item then
12575 Next_Item := Next_Rep_Item (Item);
12576 exit when No (Next_Item);
12579 -- If the private view has aspect specifications, the full view
12580 -- inherits them. Since these aspects may already have been
12581 -- attached to the full view during derivation, do not append
12582 -- them if already present.
12584 if Item = First_Rep_Item (Priv) then
12590 -- And link the private type items at the end of the chain
12593 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12598 -- Make sure Has_Predicates is set on full type if it is set on the
12599 -- private type. Note that it may already be set on the full type and
12600 -- if so, we don't want to unset it. Similarly, propagate information
12601 -- about delayed aspects, because the corresponding pragmas must be
12602 -- analyzed when one of the views is frozen. This last step is needed
12603 -- in particular when the full type is a scalar type for which an
12604 -- anonymous base type is constructed.
12606 -- The predicate functions are generated either at the freeze point
12607 -- of the type or at the end of the visible part, and we must avoid
12608 -- generating them twice.
12610 if Has_Predicates (Priv) then
12611 Set_Has_Predicates (Full);
12613 if Present (Predicate_Function (Priv))
12614 and then No (Predicate_Function (Full))
12616 Set_Predicate_Function (Full, Predicate_Function (Priv));
12620 if Has_Delayed_Aspects (Priv) then
12621 Set_Has_Delayed_Aspects (Full);
12623 end Complete_Private_Subtype;
12625 ----------------------------
12626 -- Constant_Redeclaration --
12627 ----------------------------
12629 procedure Constant_Redeclaration
12634 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12635 Obj_Def : constant Node_Id := Object_Definition (N);
12638 procedure Check_Possible_Deferred_Completion
12639 (Prev_Id : Entity_Id;
12640 Prev_Obj_Def : Node_Id;
12641 Curr_Obj_Def : Node_Id);
12642 -- Determine whether the two object definitions describe the partial
12643 -- and the full view of a constrained deferred constant. Generate
12644 -- a subtype for the full view and verify that it statically matches
12645 -- the subtype of the partial view.
12647 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12648 -- If deferred constant is an access type initialized with an allocator,
12649 -- check whether there is an illegal recursion in the definition,
12650 -- through a default value of some record subcomponent. This is normally
12651 -- detected when generating init procs, but requires this additional
12652 -- mechanism when expansion is disabled.
12654 ----------------------------------------
12655 -- Check_Possible_Deferred_Completion --
12656 ----------------------------------------
12658 procedure Check_Possible_Deferred_Completion
12659 (Prev_Id : Entity_Id;
12660 Prev_Obj_Def : Node_Id;
12661 Curr_Obj_Def : Node_Id)
12664 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12665 and then Present (Constraint (Prev_Obj_Def))
12666 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12667 and then Present (Constraint (Curr_Obj_Def))
12670 Loc : constant Source_Ptr := Sloc (N);
12671 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12672 Decl : constant Node_Id :=
12673 Make_Subtype_Declaration (Loc,
12674 Defining_Identifier => Def_Id,
12675 Subtype_Indication =>
12676 Relocate_Node (Curr_Obj_Def));
12679 Insert_Before_And_Analyze (N, Decl);
12680 Set_Etype (Id, Def_Id);
12682 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12683 Error_Msg_Sloc := Sloc (Prev_Id);
12684 Error_Msg_N ("subtype does not statically match deferred "
12685 & "declaration #", N);
12689 end Check_Possible_Deferred_Completion;
12691 ---------------------------------
12692 -- Check_Recursive_Declaration --
12693 ---------------------------------
12695 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12699 if Is_Record_Type (Typ) then
12700 Comp := First_Component (Typ);
12701 while Present (Comp) loop
12702 if Comes_From_Source (Comp) then
12703 if Present (Expression (Parent (Comp)))
12704 and then Is_Entity_Name (Expression (Parent (Comp)))
12705 and then Entity (Expression (Parent (Comp))) = Prev
12707 Error_Msg_Sloc := Sloc (Parent (Comp));
12709 ("illegal circularity with declaration for & #",
12713 elsif Is_Record_Type (Etype (Comp)) then
12714 Check_Recursive_Declaration (Etype (Comp));
12718 Next_Component (Comp);
12721 end Check_Recursive_Declaration;
12723 -- Start of processing for Constant_Redeclaration
12726 if Nkind (Parent (Prev)) = N_Object_Declaration then
12727 if Nkind (Object_Definition
12728 (Parent (Prev))) = N_Subtype_Indication
12730 -- Find type of new declaration. The constraints of the two
12731 -- views must match statically, but there is no point in
12732 -- creating an itype for the full view.
12734 if Nkind (Obj_Def) = N_Subtype_Indication then
12735 Find_Type (Subtype_Mark (Obj_Def));
12736 New_T := Entity (Subtype_Mark (Obj_Def));
12739 Find_Type (Obj_Def);
12740 New_T := Entity (Obj_Def);
12746 -- The full view may impose a constraint, even if the partial
12747 -- view does not, so construct the subtype.
12749 New_T := Find_Type_Of_Object (Obj_Def, N);
12754 -- Current declaration is illegal, diagnosed below in Enter_Name
12760 -- If previous full declaration or a renaming declaration exists, or if
12761 -- a homograph is present, let Enter_Name handle it, either with an
12762 -- error or with the removal of an overridden implicit subprogram.
12763 -- The previous one is a full declaration if it has an expression
12764 -- (which in the case of an aggregate is indicated by the Init flag).
12766 if Ekind (Prev) /= E_Constant
12767 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12768 or else Present (Expression (Parent (Prev)))
12769 or else Has_Init_Expression (Parent (Prev))
12770 or else Present (Full_View (Prev))
12774 -- Verify that types of both declarations match, or else that both types
12775 -- are anonymous access types whose designated subtypes statically match
12776 -- (as allowed in Ada 2005 by AI-385).
12778 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12780 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12781 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12782 or else Is_Access_Constant (Etype (New_T)) /=
12783 Is_Access_Constant (Etype (Prev))
12784 or else Can_Never_Be_Null (Etype (New_T)) /=
12785 Can_Never_Be_Null (Etype (Prev))
12786 or else Null_Exclusion_Present (Parent (Prev)) /=
12787 Null_Exclusion_Present (Parent (Id))
12788 or else not Subtypes_Statically_Match
12789 (Designated_Type (Etype (Prev)),
12790 Designated_Type (Etype (New_T))))
12792 Error_Msg_Sloc := Sloc (Prev);
12793 Error_Msg_N ("type does not match declaration#", N);
12794 Set_Full_View (Prev, Id);
12795 Set_Etype (Id, Any_Type);
12797 -- A deferred constant whose type is an anonymous array is always
12798 -- illegal (unless imported). A detailed error message might be
12799 -- helpful for Ada beginners.
12801 if Nkind (Object_Definition (Parent (Prev)))
12802 = N_Constrained_Array_Definition
12803 and then Nkind (Object_Definition (N))
12804 = N_Constrained_Array_Definition
12806 Error_Msg_N ("\each anonymous array is a distinct type", N);
12807 Error_Msg_N ("a deferred constant must have a named type",
12808 Object_Definition (Parent (Prev)));
12812 Null_Exclusion_Present (Parent (Prev))
12813 and then not Null_Exclusion_Present (N)
12815 Error_Msg_Sloc := Sloc (Prev);
12816 Error_Msg_N ("null-exclusion does not match declaration#", N);
12817 Set_Full_View (Prev, Id);
12818 Set_Etype (Id, Any_Type);
12820 -- If so, process the full constant declaration
12823 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12824 -- the deferred declaration is constrained, then the subtype defined
12825 -- by the subtype_indication in the full declaration shall match it
12828 Check_Possible_Deferred_Completion
12830 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12831 Curr_Obj_Def => Obj_Def);
12833 Set_Full_View (Prev, Id);
12834 Set_Is_Public (Id, Is_Public (Prev));
12835 Set_Is_Internal (Id);
12836 Append_Entity (Id, Current_Scope);
12838 -- Check ALIASED present if present before (RM 7.4(7))
12840 if Is_Aliased (Prev)
12841 and then not Aliased_Present (N)
12843 Error_Msg_Sloc := Sloc (Prev);
12844 Error_Msg_N ("ALIASED required (see declaration #)", N);
12847 -- Check that placement is in private part and that the incomplete
12848 -- declaration appeared in the visible part.
12850 if Ekind (Current_Scope) = E_Package
12851 and then not In_Private_Part (Current_Scope)
12853 Error_Msg_Sloc := Sloc (Prev);
12855 ("full constant for declaration # must be in private part", N);
12857 elsif Ekind (Current_Scope) = E_Package
12859 List_Containing (Parent (Prev)) /=
12860 Visible_Declarations (Package_Specification (Current_Scope))
12863 ("deferred constant must be declared in visible part",
12867 if Is_Access_Type (T)
12868 and then Nkind (Expression (N)) = N_Allocator
12870 Check_Recursive_Declaration (Designated_Type (T));
12873 -- A deferred constant is a visible entity. If type has invariants,
12874 -- verify that the initial value satisfies them. This is not done in
12875 -- GNATprove mode, as GNATprove handles invariant checks itself.
12877 if Has_Invariants (T)
12878 and then Present (Invariant_Procedure (T))
12879 and then not GNATprove_Mode
12882 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12885 end Constant_Redeclaration;
12887 ----------------------
12888 -- Constrain_Access --
12889 ----------------------
12891 procedure Constrain_Access
12892 (Def_Id : in out Entity_Id;
12894 Related_Nod : Node_Id)
12896 T : constant Entity_Id := Entity (Subtype_Mark (S));
12897 Desig_Type : constant Entity_Id := Designated_Type (T);
12898 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12899 Constraint_OK : Boolean := True;
12902 if Is_Array_Type (Desig_Type) then
12903 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12905 elsif (Is_Record_Type (Desig_Type)
12906 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12907 and then not Is_Constrained (Desig_Type)
12909 -- ??? The following code is a temporary bypass to ignore a
12910 -- discriminant constraint on access type if it is constraining
12911 -- the current record. Avoid creating the implicit subtype of the
12912 -- record we are currently compiling since right now, we cannot
12913 -- handle these. For now, just return the access type itself.
12915 if Desig_Type = Current_Scope
12916 and then No (Def_Id)
12918 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12919 Def_Id := Entity (Subtype_Mark (S));
12921 -- This call added to ensure that the constraint is analyzed
12922 -- (needed for a B test). Note that we still return early from
12923 -- this procedure to avoid recursive processing. ???
12925 Constrain_Discriminated_Type
12926 (Desig_Subtype, S, Related_Nod, For_Access => True);
12930 -- Enforce rule that the constraint is illegal if there is an
12931 -- unconstrained view of the designated type. This means that the
12932 -- partial view (either a private type declaration or a derivation
12933 -- from a private type) has no discriminants. (Defect Report
12934 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12936 -- Rule updated for Ada 2005: The private type is said to have
12937 -- a constrained partial view, given that objects of the type
12938 -- can be declared. Furthermore, the rule applies to all access
12939 -- types, unlike the rule concerning default discriminants (see
12942 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12943 and then Has_Private_Declaration (Desig_Type)
12944 and then In_Open_Scopes (Scope (Desig_Type))
12945 and then Has_Discriminants (Desig_Type)
12948 Pack : constant Node_Id :=
12949 Unit_Declaration_Node (Scope (Desig_Type));
12954 if Nkind (Pack) = N_Package_Declaration then
12955 Decls := Visible_Declarations (Specification (Pack));
12956 Decl := First (Decls);
12957 while Present (Decl) loop
12958 if (Nkind (Decl) = N_Private_Type_Declaration
12959 and then Chars (Defining_Identifier (Decl)) =
12960 Chars (Desig_Type))
12963 (Nkind (Decl) = N_Full_Type_Declaration
12965 Chars (Defining_Identifier (Decl)) =
12967 and then Is_Derived_Type (Desig_Type)
12969 Has_Private_Declaration (Etype (Desig_Type)))
12971 if No (Discriminant_Specifications (Decl)) then
12973 ("cannot constrain access type if designated "
12974 & "type has constrained partial view", S);
12986 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12987 For_Access => True);
12989 elsif Is_Concurrent_Type (Desig_Type)
12990 and then not Is_Constrained (Desig_Type)
12992 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12995 Error_Msg_N ("invalid constraint on access type", S);
12997 -- We simply ignore an invalid constraint
12999 Desig_Subtype := Desig_Type;
13000 Constraint_OK := False;
13003 if No (Def_Id) then
13004 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13006 Set_Ekind (Def_Id, E_Access_Subtype);
13009 if Constraint_OK then
13010 Set_Etype (Def_Id, Base_Type (T));
13012 if Is_Private_Type (Desig_Type) then
13013 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13016 Set_Etype (Def_Id, Any_Type);
13019 Set_Size_Info (Def_Id, T);
13020 Set_Is_Constrained (Def_Id, Constraint_OK);
13021 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13022 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13023 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13025 Conditional_Delay (Def_Id, T);
13027 -- AI-363 : Subtypes of general access types whose designated types have
13028 -- default discriminants are disallowed. In instances, the rule has to
13029 -- be checked against the actual, of which T is the subtype. In a
13030 -- generic body, the rule is checked assuming that the actual type has
13031 -- defaulted discriminants.
13033 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13034 if Ekind (Base_Type (T)) = E_General_Access_Type
13035 and then Has_Defaulted_Discriminants (Desig_Type)
13037 if Ada_Version < Ada_2005 then
13039 ("access subtype of general access type would not " &
13040 "be allowed in Ada 2005?y?", S);
13043 ("access subtype of general access type not allowed", S);
13046 Error_Msg_N ("\discriminants have defaults", S);
13048 elsif Is_Access_Type (T)
13049 and then Is_Generic_Type (Desig_Type)
13050 and then Has_Discriminants (Desig_Type)
13051 and then In_Package_Body (Current_Scope)
13053 if Ada_Version < Ada_2005 then
13055 ("access subtype would not be allowed in generic body "
13056 & "in Ada 2005?y?", S);
13059 ("access subtype not allowed in generic body", S);
13063 ("\designated type is a discriminated formal", S);
13066 end Constrain_Access;
13068 ---------------------
13069 -- Constrain_Array --
13070 ---------------------
13072 procedure Constrain_Array
13073 (Def_Id : in out Entity_Id;
13075 Related_Nod : Node_Id;
13076 Related_Id : Entity_Id;
13077 Suffix : Character)
13079 C : constant Node_Id := Constraint (SI);
13080 Number_Of_Constraints : Nat := 0;
13083 Constraint_OK : Boolean := True;
13086 T := Entity (Subtype_Mark (SI));
13088 if Is_Access_Type (T) then
13089 T := Designated_Type (T);
13092 -- If an index constraint follows a subtype mark in a subtype indication
13093 -- then the type or subtype denoted by the subtype mark must not already
13094 -- impose an index constraint. The subtype mark must denote either an
13095 -- unconstrained array type or an access type whose designated type
13096 -- is such an array type... (RM 3.6.1)
13098 if Is_Constrained (T) then
13099 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13100 Constraint_OK := False;
13103 S := First (Constraints (C));
13104 while Present (S) loop
13105 Number_Of_Constraints := Number_Of_Constraints + 1;
13109 -- In either case, the index constraint must provide a discrete
13110 -- range for each index of the array type and the type of each
13111 -- discrete range must be the same as that of the corresponding
13112 -- index. (RM 3.6.1)
13114 if Number_Of_Constraints /= Number_Dimensions (T) then
13115 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13116 Constraint_OK := False;
13119 S := First (Constraints (C));
13120 Index := First_Index (T);
13123 -- Apply constraints to each index type
13125 for J in 1 .. Number_Of_Constraints loop
13126 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13134 if No (Def_Id) then
13136 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13137 Set_Parent (Def_Id, Related_Nod);
13140 Set_Ekind (Def_Id, E_Array_Subtype);
13143 Set_Size_Info (Def_Id, (T));
13144 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13145 Set_Etype (Def_Id, Base_Type (T));
13147 if Constraint_OK then
13148 Set_First_Index (Def_Id, First (Constraints (C)));
13150 Set_First_Index (Def_Id, First_Index (T));
13153 Set_Is_Constrained (Def_Id, True);
13154 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13155 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13157 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13158 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13160 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13161 -- We need to initialize the attribute because if Def_Id is previously
13162 -- analyzed through a limited_with clause, it will have the attributes
13163 -- of an incomplete type, one of which is an Elist that overlaps the
13164 -- Packed_Array_Impl_Type field.
13166 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13168 -- Build a freeze node if parent still needs one. Also make sure that
13169 -- the Depends_On_Private status is set because the subtype will need
13170 -- reprocessing at the time the base type does, and also we must set a
13171 -- conditional delay.
13173 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13174 Conditional_Delay (Def_Id, T);
13175 end Constrain_Array;
13177 ------------------------------
13178 -- Constrain_Component_Type --
13179 ------------------------------
13181 function Constrain_Component_Type
13183 Constrained_Typ : Entity_Id;
13184 Related_Node : Node_Id;
13186 Constraints : Elist_Id) return Entity_Id
13188 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13189 Compon_Type : constant Entity_Id := Etype (Comp);
13191 function Build_Constrained_Array_Type
13192 (Old_Type : Entity_Id) return Entity_Id;
13193 -- If Old_Type is an array type, one of whose indexes is constrained
13194 -- by a discriminant, build an Itype whose constraint replaces the
13195 -- discriminant with its value in the constraint.
13197 function Build_Constrained_Discriminated_Type
13198 (Old_Type : Entity_Id) return Entity_Id;
13199 -- Ditto for record components
13201 function Build_Constrained_Access_Type
13202 (Old_Type : Entity_Id) return Entity_Id;
13203 -- Ditto for access types. Makes use of previous two functions, to
13204 -- constrain designated type.
13206 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
13207 -- T is an array or discriminated type, C is a list of constraints
13208 -- that apply to T. This routine builds the constrained subtype.
13210 function Is_Discriminant (Expr : Node_Id) return Boolean;
13211 -- Returns True if Expr is a discriminant
13213 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
13214 -- Find the value of discriminant Discrim in Constraint
13216 -----------------------------------
13217 -- Build_Constrained_Access_Type --
13218 -----------------------------------
13220 function Build_Constrained_Access_Type
13221 (Old_Type : Entity_Id) return Entity_Id
13223 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13225 Desig_Subtype : Entity_Id;
13229 -- if the original access type was not embedded in the enclosing
13230 -- type definition, there is no need to produce a new access
13231 -- subtype. In fact every access type with an explicit constraint
13232 -- generates an itype whose scope is the enclosing record.
13234 if not Is_Type (Scope (Old_Type)) then
13237 elsif Is_Array_Type (Desig_Type) then
13238 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13240 elsif Has_Discriminants (Desig_Type) then
13242 -- This may be an access type to an enclosing record type for
13243 -- which we are constructing the constrained components. Return
13244 -- the enclosing record subtype. This is not always correct,
13245 -- but avoids infinite recursion. ???
13247 Desig_Subtype := Any_Type;
13249 for J in reverse 0 .. Scope_Stack.Last loop
13250 Scop := Scope_Stack.Table (J).Entity;
13253 and then Base_Type (Scop) = Base_Type (Desig_Type)
13255 Desig_Subtype := Scop;
13258 exit when not Is_Type (Scop);
13261 if Desig_Subtype = Any_Type then
13263 Build_Constrained_Discriminated_Type (Desig_Type);
13270 if Desig_Subtype /= Desig_Type then
13272 -- The Related_Node better be here or else we won't be able
13273 -- to attach new itypes to a node in the tree.
13275 pragma Assert (Present (Related_Node));
13277 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13279 Set_Etype (Itype, Base_Type (Old_Type));
13280 Set_Size_Info (Itype, (Old_Type));
13281 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13282 Set_Depends_On_Private (Itype, Has_Private_Component
13284 Set_Is_Access_Constant (Itype, Is_Access_Constant
13287 -- The new itype needs freezing when it depends on a not frozen
13288 -- type and the enclosing subtype needs freezing.
13290 if Has_Delayed_Freeze (Constrained_Typ)
13291 and then not Is_Frozen (Constrained_Typ)
13293 Conditional_Delay (Itype, Base_Type (Old_Type));
13301 end Build_Constrained_Access_Type;
13303 ----------------------------------
13304 -- Build_Constrained_Array_Type --
13305 ----------------------------------
13307 function Build_Constrained_Array_Type
13308 (Old_Type : Entity_Id) return Entity_Id
13312 Old_Index : Node_Id;
13313 Range_Node : Node_Id;
13314 Constr_List : List_Id;
13316 Need_To_Create_Itype : Boolean := False;
13319 Old_Index := First_Index (Old_Type);
13320 while Present (Old_Index) loop
13321 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13323 if Is_Discriminant (Lo_Expr)
13325 Is_Discriminant (Hi_Expr)
13327 Need_To_Create_Itype := True;
13330 Next_Index (Old_Index);
13333 if Need_To_Create_Itype then
13334 Constr_List := New_List;
13336 Old_Index := First_Index (Old_Type);
13337 while Present (Old_Index) loop
13338 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13340 if Is_Discriminant (Lo_Expr) then
13341 Lo_Expr := Get_Discr_Value (Lo_Expr);
13344 if Is_Discriminant (Hi_Expr) then
13345 Hi_Expr := Get_Discr_Value (Hi_Expr);
13350 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13352 Append (Range_Node, To => Constr_List);
13354 Next_Index (Old_Index);
13357 return Build_Subtype (Old_Type, Constr_List);
13362 end Build_Constrained_Array_Type;
13364 ------------------------------------------
13365 -- Build_Constrained_Discriminated_Type --
13366 ------------------------------------------
13368 function Build_Constrained_Discriminated_Type
13369 (Old_Type : Entity_Id) return Entity_Id
13372 Constr_List : List_Id;
13373 Old_Constraint : Elmt_Id;
13375 Need_To_Create_Itype : Boolean := False;
13378 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13379 while Present (Old_Constraint) loop
13380 Expr := Node (Old_Constraint);
13382 if Is_Discriminant (Expr) then
13383 Need_To_Create_Itype := True;
13386 Next_Elmt (Old_Constraint);
13389 if Need_To_Create_Itype then
13390 Constr_List := New_List;
13392 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13393 while Present (Old_Constraint) loop
13394 Expr := Node (Old_Constraint);
13396 if Is_Discriminant (Expr) then
13397 Expr := Get_Discr_Value (Expr);
13400 Append (New_Copy_Tree (Expr), To => Constr_List);
13402 Next_Elmt (Old_Constraint);
13405 return Build_Subtype (Old_Type, Constr_List);
13410 end Build_Constrained_Discriminated_Type;
13412 -------------------
13413 -- Build_Subtype --
13414 -------------------
13416 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
13418 Subtyp_Decl : Node_Id;
13419 Def_Id : Entity_Id;
13420 Btyp : Entity_Id := Base_Type (T);
13423 -- The Related_Node better be here or else we won't be able to
13424 -- attach new itypes to a node in the tree.
13426 pragma Assert (Present (Related_Node));
13428 -- If the view of the component's type is incomplete or private
13429 -- with unknown discriminants, then the constraint must be applied
13430 -- to the full type.
13432 if Has_Unknown_Discriminants (Btyp)
13433 and then Present (Underlying_Type (Btyp))
13435 Btyp := Underlying_Type (Btyp);
13439 Make_Subtype_Indication (Loc,
13440 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13441 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13443 Def_Id := Create_Itype (Ekind (T), Related_Node);
13446 Make_Subtype_Declaration (Loc,
13447 Defining_Identifier => Def_Id,
13448 Subtype_Indication => Indic);
13450 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13452 -- Itypes must be analyzed with checks off (see package Itypes)
13454 Analyze (Subtyp_Decl, Suppress => All_Checks);
13456 if Is_Itype (Def_Id) and then Has_Predicates (T) then
13457 Inherit_Predicate_Flags (Def_Id, T);
13459 -- Indicate where the predicate function may be found
13461 if Is_Itype (T) then
13462 if Present (Predicate_Function (Def_Id)) then
13465 elsif Present (Predicate_Function (T)) then
13466 Set_Predicate_Function (Def_Id, Predicate_Function (T));
13469 Set_Predicated_Parent (Def_Id, Predicated_Parent (T));
13472 elsif No (Predicate_Function (Def_Id)) then
13473 Set_Predicated_Parent (Def_Id, T);
13480 ---------------------
13481 -- Get_Discr_Value --
13482 ---------------------
13484 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13489 -- The discriminant may be declared for the type, in which case we
13490 -- find it by iterating over the list of discriminants. If the
13491 -- discriminant is inherited from a parent type, it appears as the
13492 -- corresponding discriminant of the current type. This will be the
13493 -- case when constraining an inherited component whose constraint is
13494 -- given by a discriminant of the parent.
13496 D := First_Discriminant (Typ);
13497 E := First_Elmt (Constraints);
13499 while Present (D) loop
13500 if D = Entity (Discrim)
13501 or else D = CR_Discriminant (Entity (Discrim))
13502 or else Corresponding_Discriminant (D) = Entity (Discrim)
13507 Next_Discriminant (D);
13511 -- The Corresponding_Discriminant mechanism is incomplete, because
13512 -- the correspondence between new and old discriminants is not one
13513 -- to one: one new discriminant can constrain several old ones. In
13514 -- that case, scan sequentially the stored_constraint, the list of
13515 -- discriminants of the parents, and the constraints.
13517 -- Previous code checked for the present of the Stored_Constraint
13518 -- list for the derived type, but did not use it at all. Should it
13519 -- be present when the component is a discriminated task type?
13521 if Is_Derived_Type (Typ)
13522 and then Scope (Entity (Discrim)) = Etype (Typ)
13524 D := First_Discriminant (Etype (Typ));
13525 E := First_Elmt (Constraints);
13526 while Present (D) loop
13527 if D = Entity (Discrim) then
13531 Next_Discriminant (D);
13536 -- Something is wrong if we did not find the value
13538 raise Program_Error;
13539 end Get_Discr_Value;
13541 ---------------------
13542 -- Is_Discriminant --
13543 ---------------------
13545 function Is_Discriminant (Expr : Node_Id) return Boolean is
13546 Discrim_Scope : Entity_Id;
13549 if Denotes_Discriminant (Expr) then
13550 Discrim_Scope := Scope (Entity (Expr));
13552 -- Either we have a reference to one of Typ's discriminants,
13554 pragma Assert (Discrim_Scope = Typ
13556 -- or to the discriminants of the parent type, in the case
13557 -- of a derivation of a tagged type with variants.
13559 or else Discrim_Scope = Etype (Typ)
13560 or else Full_View (Discrim_Scope) = Etype (Typ)
13562 -- or same as above for the case where the discriminants
13563 -- were declared in Typ's private view.
13565 or else (Is_Private_Type (Discrim_Scope)
13566 and then Chars (Discrim_Scope) = Chars (Typ))
13568 -- or else we are deriving from the full view and the
13569 -- discriminant is declared in the private entity.
13571 or else (Is_Private_Type (Typ)
13572 and then Chars (Discrim_Scope) = Chars (Typ))
13574 -- Or we are constrained the corresponding record of a
13575 -- synchronized type that completes a private declaration.
13577 or else (Is_Concurrent_Record_Type (Typ)
13579 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13581 -- or we have a class-wide type, in which case make sure the
13582 -- discriminant found belongs to the root type.
13584 or else (Is_Class_Wide_Type (Typ)
13585 and then Etype (Typ) = Discrim_Scope));
13590 -- In all other cases we have something wrong
13593 end Is_Discriminant;
13595 -- Start of processing for Constrain_Component_Type
13598 if Nkind (Parent (Comp)) = N_Component_Declaration
13599 and then Comes_From_Source (Parent (Comp))
13600 and then Comes_From_Source
13601 (Subtype_Indication (Component_Definition (Parent (Comp))))
13604 (Subtype_Indication (Component_Definition (Parent (Comp))))
13606 return Compon_Type;
13608 elsif Is_Array_Type (Compon_Type) then
13609 return Build_Constrained_Array_Type (Compon_Type);
13611 elsif Has_Discriminants (Compon_Type) then
13612 return Build_Constrained_Discriminated_Type (Compon_Type);
13614 elsif Is_Access_Type (Compon_Type) then
13615 return Build_Constrained_Access_Type (Compon_Type);
13618 return Compon_Type;
13620 end Constrain_Component_Type;
13622 --------------------------
13623 -- Constrain_Concurrent --
13624 --------------------------
13626 -- For concurrent types, the associated record value type carries the same
13627 -- discriminants, so when we constrain a concurrent type, we must constrain
13628 -- the corresponding record type as well.
13630 procedure Constrain_Concurrent
13631 (Def_Id : in out Entity_Id;
13633 Related_Nod : Node_Id;
13634 Related_Id : Entity_Id;
13635 Suffix : Character)
13637 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13638 -- case of a private subtype (needed when only doing semantic analysis).
13640 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13644 if Is_Access_Type (T_Ent) then
13645 T_Ent := Designated_Type (T_Ent);
13648 T_Val := Corresponding_Record_Type (T_Ent);
13650 if Present (T_Val) then
13652 if No (Def_Id) then
13653 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13655 -- Elaborate itype now, as it may be used in a subsequent
13656 -- synchronized operation in another scope.
13658 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13659 Build_Itype_Reference (Def_Id, Related_Nod);
13663 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13664 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
13666 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13667 Set_Corresponding_Record_Type (Def_Id,
13668 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13671 -- If there is no associated record, expansion is disabled and this
13672 -- is a generic context. Create a subtype in any case, so that
13673 -- semantic analysis can proceed.
13675 if No (Def_Id) then
13676 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13679 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13681 end Constrain_Concurrent;
13683 ------------------------------------
13684 -- Constrain_Corresponding_Record --
13685 ------------------------------------
13687 function Constrain_Corresponding_Record
13688 (Prot_Subt : Entity_Id;
13689 Corr_Rec : Entity_Id;
13690 Related_Nod : Node_Id) return Entity_Id
13692 T_Sub : constant Entity_Id :=
13693 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
13696 Set_Etype (T_Sub, Corr_Rec);
13697 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13698 Set_Is_Constrained (T_Sub, True);
13699 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13700 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13702 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13703 Set_Discriminant_Constraint
13704 (T_Sub, Discriminant_Constraint (Prot_Subt));
13705 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13706 Create_Constrained_Components
13707 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13710 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13712 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13713 Conditional_Delay (T_Sub, Corr_Rec);
13716 -- This is a component subtype: it will be frozen in the context of
13717 -- the enclosing record's init_proc, so that discriminant references
13718 -- are resolved to discriminals. (Note: we used to skip freezing
13719 -- altogether in that case, which caused errors downstream for
13720 -- components of a bit packed array type).
13722 Set_Has_Delayed_Freeze (T_Sub);
13726 end Constrain_Corresponding_Record;
13728 -----------------------
13729 -- Constrain_Decimal --
13730 -----------------------
13732 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13733 T : constant Entity_Id := Entity (Subtype_Mark (S));
13734 C : constant Node_Id := Constraint (S);
13735 Loc : constant Source_Ptr := Sloc (C);
13736 Range_Expr : Node_Id;
13737 Digits_Expr : Node_Id;
13742 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13744 if Nkind (C) = N_Range_Constraint then
13745 Range_Expr := Range_Expression (C);
13746 Digits_Val := Digits_Value (T);
13749 pragma Assert (Nkind (C) = N_Digits_Constraint);
13751 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13753 Digits_Expr := Digits_Expression (C);
13754 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13756 Check_Digits_Expression (Digits_Expr);
13757 Digits_Val := Expr_Value (Digits_Expr);
13759 if Digits_Val > Digits_Value (T) then
13761 ("digits expression is incompatible with subtype", C);
13762 Digits_Val := Digits_Value (T);
13765 if Present (Range_Constraint (C)) then
13766 Range_Expr := Range_Expression (Range_Constraint (C));
13768 Range_Expr := Empty;
13772 Set_Etype (Def_Id, Base_Type (T));
13773 Set_Size_Info (Def_Id, (T));
13774 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13775 Set_Delta_Value (Def_Id, Delta_Value (T));
13776 Set_Scale_Value (Def_Id, Scale_Value (T));
13777 Set_Small_Value (Def_Id, Small_Value (T));
13778 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13779 Set_Digits_Value (Def_Id, Digits_Val);
13781 -- Manufacture range from given digits value if no range present
13783 if No (Range_Expr) then
13784 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13788 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13790 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13793 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13794 Set_Discrete_RM_Size (Def_Id);
13796 -- Unconditionally delay the freeze, since we cannot set size
13797 -- information in all cases correctly until the freeze point.
13799 Set_Has_Delayed_Freeze (Def_Id);
13800 end Constrain_Decimal;
13802 ----------------------------------
13803 -- Constrain_Discriminated_Type --
13804 ----------------------------------
13806 procedure Constrain_Discriminated_Type
13807 (Def_Id : Entity_Id;
13809 Related_Nod : Node_Id;
13810 For_Access : Boolean := False)
13812 E : Entity_Id := Entity (Subtype_Mark (S));
13815 procedure Fixup_Bad_Constraint;
13816 -- Called after finding a bad constraint, and after having posted an
13817 -- appropriate error message. The goal is to leave type Def_Id in as
13818 -- reasonable state as possible.
13820 --------------------------
13821 -- Fixup_Bad_Constraint --
13822 --------------------------
13824 procedure Fixup_Bad_Constraint is
13826 -- Set a reasonable Ekind for the entity, including incomplete types.
13828 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13830 -- Set Etype to the known type, to reduce chances of cascaded errors
13832 Set_Etype (Def_Id, E);
13833 Set_Error_Posted (Def_Id);
13834 end Fixup_Bad_Constraint;
13839 Constr : Elist_Id := New_Elmt_List;
13841 -- Start of processing for Constrain_Discriminated_Type
13844 C := Constraint (S);
13846 -- A discriminant constraint is only allowed in a subtype indication,
13847 -- after a subtype mark. This subtype mark must denote either a type
13848 -- with discriminants, or an access type whose designated type is a
13849 -- type with discriminants. A discriminant constraint specifies the
13850 -- values of these discriminants (RM 3.7.2(5)).
13852 T := Base_Type (Entity (Subtype_Mark (S)));
13854 if Is_Access_Type (T) then
13855 T := Designated_Type (T);
13858 -- In an instance it may be necessary to retrieve the full view of a
13859 -- type with unknown discriminants, or a full view with defaulted
13860 -- discriminants. In other contexts the constraint is illegal.
13863 and then Is_Private_Type (T)
13864 and then Present (Full_View (T))
13866 (Has_Unknown_Discriminants (T)
13868 (not Has_Discriminants (T)
13869 and then Has_Discriminants (Full_View (T))
13870 and then Present (Discriminant_Default_Value
13871 (First_Discriminant (Full_View (T))))))
13873 T := Full_View (T);
13874 E := Full_View (E);
13877 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13878 -- generating an error for access-to-incomplete subtypes.
13880 if Ada_Version >= Ada_2005
13881 and then Ekind (T) = E_Incomplete_Type
13882 and then Nkind (Parent (S)) = N_Subtype_Declaration
13883 and then not Is_Itype (Def_Id)
13885 -- A little sanity check: emit an error message if the type has
13886 -- discriminants to begin with. Type T may be a regular incomplete
13887 -- type or imported via a limited with clause.
13889 if Has_Discriminants (T)
13890 or else (From_Limited_With (T)
13891 and then Present (Non_Limited_View (T))
13892 and then Nkind (Parent (Non_Limited_View (T))) =
13893 N_Full_Type_Declaration
13894 and then Present (Discriminant_Specifications
13895 (Parent (Non_Limited_View (T)))))
13898 ("(Ada 2005) incomplete subtype may not be constrained", C);
13900 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13903 Fixup_Bad_Constraint;
13906 -- Check that the type has visible discriminants. The type may be
13907 -- a private type with unknown discriminants whose full view has
13908 -- discriminants which are invisible.
13910 elsif not Has_Discriminants (T)
13912 (Has_Unknown_Discriminants (T)
13913 and then Is_Private_Type (T))
13915 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13916 Fixup_Bad_Constraint;
13919 elsif Is_Constrained (E)
13920 or else (Ekind (E) = E_Class_Wide_Subtype
13921 and then Present (Discriminant_Constraint (E)))
13923 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13924 Fixup_Bad_Constraint;
13928 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13929 -- applies to the base type.
13931 T := Base_Type (T);
13933 Constr := Build_Discriminant_Constraints (T, S);
13935 -- If the list returned was empty we had an error in building the
13936 -- discriminant constraint. We have also already signalled an error
13937 -- in the incomplete type case
13939 if Is_Empty_Elmt_List (Constr) then
13940 Fixup_Bad_Constraint;
13944 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13945 end Constrain_Discriminated_Type;
13947 ---------------------------
13948 -- Constrain_Enumeration --
13949 ---------------------------
13951 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13952 T : constant Entity_Id := Entity (Subtype_Mark (S));
13953 C : constant Node_Id := Constraint (S);
13956 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13958 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13960 Set_Etype (Def_Id, Base_Type (T));
13961 Set_Size_Info (Def_Id, (T));
13962 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13963 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13965 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13967 Set_Discrete_RM_Size (Def_Id);
13968 end Constrain_Enumeration;
13970 ----------------------
13971 -- Constrain_Float --
13972 ----------------------
13974 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13975 T : constant Entity_Id := Entity (Subtype_Mark (S));
13981 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13983 Set_Etype (Def_Id, Base_Type (T));
13984 Set_Size_Info (Def_Id, (T));
13985 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13987 -- Process the constraint
13989 C := Constraint (S);
13991 -- Digits constraint present
13993 if Nkind (C) = N_Digits_Constraint then
13995 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13996 Check_Restriction (No_Obsolescent_Features, C);
13998 if Warn_On_Obsolescent_Feature then
14000 ("subtype digits constraint is an " &
14001 "obsolescent feature (RM J.3(8))?j?", C);
14004 D := Digits_Expression (C);
14005 Analyze_And_Resolve (D, Any_Integer);
14006 Check_Digits_Expression (D);
14007 Set_Digits_Value (Def_Id, Expr_Value (D));
14009 -- Check that digits value is in range. Obviously we can do this
14010 -- at compile time, but it is strictly a runtime check, and of
14011 -- course there is an ACVC test that checks this.
14013 if Digits_Value (Def_Id) > Digits_Value (T) then
14014 Error_Msg_Uint_1 := Digits_Value (T);
14015 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14017 Make_Raise_Constraint_Error (Sloc (D),
14018 Reason => CE_Range_Check_Failed);
14019 Insert_Action (Declaration_Node (Def_Id), Rais);
14022 C := Range_Constraint (C);
14024 -- No digits constraint present
14027 Set_Digits_Value (Def_Id, Digits_Value (T));
14030 -- Range constraint present
14032 if Nkind (C) = N_Range_Constraint then
14033 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14035 -- No range constraint present
14038 pragma Assert (No (C));
14039 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14042 Set_Is_Constrained (Def_Id);
14043 end Constrain_Float;
14045 ---------------------
14046 -- Constrain_Index --
14047 ---------------------
14049 procedure Constrain_Index
14052 Related_Nod : Node_Id;
14053 Related_Id : Entity_Id;
14054 Suffix : Character;
14055 Suffix_Index : Nat)
14057 Def_Id : Entity_Id;
14058 R : Node_Id := Empty;
14059 T : constant Entity_Id := Etype (Index);
14063 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14064 Set_Etype (Def_Id, Base_Type (T));
14066 if Nkind (S) = N_Range
14068 (Nkind (S) = N_Attribute_Reference
14069 and then Attribute_Name (S) = Name_Range)
14071 -- A Range attribute will be transformed into N_Range by Resolve
14077 Process_Range_Expr_In_Decl (R, T);
14079 if not Error_Posted (S)
14081 (Nkind (S) /= N_Range
14082 or else not Covers (T, (Etype (Low_Bound (S))))
14083 or else not Covers (T, (Etype (High_Bound (S)))))
14085 if Base_Type (T) /= Any_Type
14086 and then Etype (Low_Bound (S)) /= Any_Type
14087 and then Etype (High_Bound (S)) /= Any_Type
14089 Error_Msg_N ("range expected", S);
14093 elsif Nkind (S) = N_Subtype_Indication then
14095 -- The parser has verified that this is a discrete indication
14097 Resolve_Discrete_Subtype_Indication (S, T);
14098 Bad_Predicated_Subtype_Use
14099 ("subtype& has predicate, not allowed in index constraint",
14100 S, Entity (Subtype_Mark (S)));
14102 R := Range_Expression (Constraint (S));
14104 -- Capture values of bounds and generate temporaries for them if
14105 -- needed, since checks may cause duplication of the expressions
14106 -- which must not be reevaluated.
14108 -- The forced evaluation removes side effects from expressions, which
14109 -- should occur also in GNATprove mode. Otherwise, we end up with
14110 -- unexpected insertions of actions at places where this is not
14111 -- supposed to occur, e.g. on default parameters of a call.
14113 if Expander_Active or GNATprove_Mode then
14115 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14117 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14120 elsif Nkind (S) = N_Discriminant_Association then
14122 -- Syntactically valid in subtype indication
14124 Error_Msg_N ("invalid index constraint", S);
14125 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14128 -- Subtype_Mark case, no anonymous subtypes to construct
14133 if Is_Entity_Name (S) then
14134 if not Is_Type (Entity (S)) then
14135 Error_Msg_N ("expect subtype mark for index constraint", S);
14137 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14138 Wrong_Type (S, Base_Type (T));
14140 -- Check error of subtype with predicate in index constraint
14143 Bad_Predicated_Subtype_Use
14144 ("subtype& has predicate, not allowed in index constraint",
14151 Error_Msg_N ("invalid index constraint", S);
14152 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14157 -- Complete construction of the Itype
14159 if Is_Modular_Integer_Type (T) then
14160 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14162 elsif Is_Integer_Type (T) then
14163 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14166 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14167 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14168 Set_First_Literal (Def_Id, First_Literal (T));
14171 Set_Size_Info (Def_Id, (T));
14172 Set_RM_Size (Def_Id, RM_Size (T));
14173 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14175 Set_Scalar_Range (Def_Id, R);
14177 Set_Etype (S, Def_Id);
14178 Set_Discrete_RM_Size (Def_Id);
14179 end Constrain_Index;
14181 -----------------------
14182 -- Constrain_Integer --
14183 -----------------------
14185 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
14186 T : constant Entity_Id := Entity (Subtype_Mark (S));
14187 C : constant Node_Id := Constraint (S);
14190 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14192 if Is_Modular_Integer_Type (T) then
14193 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14195 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14198 Set_Etype (Def_Id, Base_Type (T));
14199 Set_Size_Info (Def_Id, (T));
14200 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14201 Set_Discrete_RM_Size (Def_Id);
14202 end Constrain_Integer;
14204 ------------------------------
14205 -- Constrain_Ordinary_Fixed --
14206 ------------------------------
14208 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
14209 T : constant Entity_Id := Entity (Subtype_Mark (S));
14215 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14216 Set_Etype (Def_Id, Base_Type (T));
14217 Set_Size_Info (Def_Id, (T));
14218 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14219 Set_Small_Value (Def_Id, Small_Value (T));
14221 -- Process the constraint
14223 C := Constraint (S);
14225 -- Delta constraint present
14227 if Nkind (C) = N_Delta_Constraint then
14229 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
14230 Check_Restriction (No_Obsolescent_Features, C);
14232 if Warn_On_Obsolescent_Feature then
14234 ("subtype delta constraint is an " &
14235 "obsolescent feature (RM J.3(7))?j?");
14238 D := Delta_Expression (C);
14239 Analyze_And_Resolve (D, Any_Real);
14240 Check_Delta_Expression (D);
14241 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14243 -- Check that delta value is in range. Obviously we can do this
14244 -- at compile time, but it is strictly a runtime check, and of
14245 -- course there is an ACVC test that checks this.
14247 if Delta_Value (Def_Id) < Delta_Value (T) then
14248 Error_Msg_N ("??delta value is too small", D);
14250 Make_Raise_Constraint_Error (Sloc (D),
14251 Reason => CE_Range_Check_Failed);
14252 Insert_Action (Declaration_Node (Def_Id), Rais);
14255 C := Range_Constraint (C);
14257 -- No delta constraint present
14260 Set_Delta_Value (Def_Id, Delta_Value (T));
14263 -- Range constraint present
14265 if Nkind (C) = N_Range_Constraint then
14266 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14268 -- No range constraint present
14271 pragma Assert (No (C));
14272 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14275 Set_Discrete_RM_Size (Def_Id);
14277 -- Unconditionally delay the freeze, since we cannot set size
14278 -- information in all cases correctly until the freeze point.
14280 Set_Has_Delayed_Freeze (Def_Id);
14281 end Constrain_Ordinary_Fixed;
14283 -----------------------
14284 -- Contain_Interface --
14285 -----------------------
14287 function Contain_Interface
14288 (Iface : Entity_Id;
14289 Ifaces : Elist_Id) return Boolean
14291 Iface_Elmt : Elmt_Id;
14294 if Present (Ifaces) then
14295 Iface_Elmt := First_Elmt (Ifaces);
14296 while Present (Iface_Elmt) loop
14297 if Node (Iface_Elmt) = Iface then
14301 Next_Elmt (Iface_Elmt);
14306 end Contain_Interface;
14308 ---------------------------
14309 -- Convert_Scalar_Bounds --
14310 ---------------------------
14312 procedure Convert_Scalar_Bounds
14314 Parent_Type : Entity_Id;
14315 Derived_Type : Entity_Id;
14318 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14325 -- Defend against previous errors
14327 if No (Scalar_Range (Derived_Type)) then
14328 Check_Error_Detected;
14332 Lo := Build_Scalar_Bound
14333 (Type_Low_Bound (Derived_Type),
14334 Parent_Type, Implicit_Base);
14336 Hi := Build_Scalar_Bound
14337 (Type_High_Bound (Derived_Type),
14338 Parent_Type, Implicit_Base);
14345 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14347 Set_Parent (Rng, N);
14348 Set_Scalar_Range (Derived_Type, Rng);
14350 -- Analyze the bounds
14352 Analyze_And_Resolve (Lo, Implicit_Base);
14353 Analyze_And_Resolve (Hi, Implicit_Base);
14355 -- Analyze the range itself, except that we do not analyze it if
14356 -- the bounds are real literals, and we have a fixed-point type.
14357 -- The reason for this is that we delay setting the bounds in this
14358 -- case till we know the final Small and Size values (see circuit
14359 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14361 if Is_Fixed_Point_Type (Parent_Type)
14362 and then Nkind (Lo) = N_Real_Literal
14363 and then Nkind (Hi) = N_Real_Literal
14367 -- Here we do the analysis of the range
14369 -- Note: we do this manually, since if we do a normal Analyze and
14370 -- Resolve call, there are problems with the conversions used for
14371 -- the derived type range.
14374 Set_Etype (Rng, Implicit_Base);
14375 Set_Analyzed (Rng, True);
14377 end Convert_Scalar_Bounds;
14379 -------------------
14380 -- Copy_And_Swap --
14381 -------------------
14383 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14385 -- Initialize new full declaration entity by copying the pertinent
14386 -- fields of the corresponding private declaration entity.
14388 -- We temporarily set Ekind to a value appropriate for a type to
14389 -- avoid assert failures in Einfo from checking for setting type
14390 -- attributes on something that is not a type. Ekind (Priv) is an
14391 -- appropriate choice, since it allowed the attributes to be set
14392 -- in the first place. This Ekind value will be modified later.
14394 Set_Ekind (Full, Ekind (Priv));
14396 -- Also set Etype temporarily to Any_Type, again, in the absence
14397 -- of errors, it will be properly reset, and if there are errors,
14398 -- then we want a value of Any_Type to remain.
14400 Set_Etype (Full, Any_Type);
14402 -- Now start copying attributes
14404 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14406 if Has_Discriminants (Full) then
14407 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14408 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14411 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14412 Set_Homonym (Full, Homonym (Priv));
14413 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14414 Set_Is_Public (Full, Is_Public (Priv));
14415 Set_Is_Pure (Full, Is_Pure (Priv));
14416 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14417 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14418 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14419 Set_Has_Pragma_Unreferenced_Objects
14420 (Full, Has_Pragma_Unreferenced_Objects
14423 Conditional_Delay (Full, Priv);
14425 if Is_Tagged_Type (Full) then
14426 Set_Direct_Primitive_Operations
14427 (Full, Direct_Primitive_Operations (Priv));
14428 Set_No_Tagged_Streams_Pragma
14429 (Full, No_Tagged_Streams_Pragma (Priv));
14431 if Is_Base_Type (Priv) then
14432 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14436 Set_Is_Volatile (Full, Is_Volatile (Priv));
14437 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14438 Set_Scope (Full, Scope (Priv));
14439 Set_Prev_Entity (Full, Prev_Entity (Priv));
14440 Set_Next_Entity (Full, Next_Entity (Priv));
14441 Set_First_Entity (Full, First_Entity (Priv));
14442 Set_Last_Entity (Full, Last_Entity (Priv));
14444 -- If access types have been recorded for later handling, keep them in
14445 -- the full view so that they get handled when the full view freeze
14446 -- node is expanded.
14448 if Present (Freeze_Node (Priv))
14449 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14451 Ensure_Freeze_Node (Full);
14452 Set_Access_Types_To_Process
14453 (Freeze_Node (Full),
14454 Access_Types_To_Process (Freeze_Node (Priv)));
14457 -- Swap the two entities. Now Private is the full type entity and Full
14458 -- is the private one. They will be swapped back at the end of the
14459 -- private part. This swapping ensures that the entity that is visible
14460 -- in the private part is the full declaration.
14462 Exchange_Entities (Priv, Full);
14463 Append_Entity (Full, Scope (Full));
14466 -------------------------------------
14467 -- Copy_Array_Base_Type_Attributes --
14468 -------------------------------------
14470 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14472 Set_Component_Alignment (T1, Component_Alignment (T2));
14473 Set_Component_Type (T1, Component_Type (T2));
14474 Set_Component_Size (T1, Component_Size (T2));
14475 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14476 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14477 Propagate_Concurrent_Flags (T1, T2);
14478 Set_Is_Packed (T1, Is_Packed (T2));
14479 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14480 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14481 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14482 end Copy_Array_Base_Type_Attributes;
14484 -----------------------------------
14485 -- Copy_Array_Subtype_Attributes --
14486 -----------------------------------
14488 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14490 Set_Size_Info (T1, T2);
14492 Set_First_Index (T1, First_Index (T2));
14493 Set_Is_Aliased (T1, Is_Aliased (T2));
14494 Set_Is_Volatile (T1, Is_Volatile (T2));
14495 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14496 Set_Is_Constrained (T1, Is_Constrained (T2));
14497 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14498 Inherit_Rep_Item_Chain (T1, T2);
14499 Set_Convention (T1, Convention (T2));
14500 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14501 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14502 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14503 end Copy_Array_Subtype_Attributes;
14505 -----------------------------------
14506 -- Create_Constrained_Components --
14507 -----------------------------------
14509 procedure Create_Constrained_Components
14511 Decl_Node : Node_Id;
14513 Constraints : Elist_Id)
14515 Loc : constant Source_Ptr := Sloc (Subt);
14516 Comp_List : constant Elist_Id := New_Elmt_List;
14517 Parent_Type : constant Entity_Id := Etype (Typ);
14518 Assoc_List : constant List_Id := New_List;
14519 Discr_Val : Elmt_Id;
14523 Is_Static : Boolean := True;
14525 procedure Collect_Fixed_Components (Typ : Entity_Id);
14526 -- Collect parent type components that do not appear in a variant part
14528 procedure Create_All_Components;
14529 -- Iterate over Comp_List to create the components of the subtype
14531 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14532 -- Creates a new component from Old_Compon, copying all the fields from
14533 -- it, including its Etype, inserts the new component in the Subt entity
14534 -- chain and returns the new component.
14536 function Is_Variant_Record (T : Entity_Id) return Boolean;
14537 -- If true, and discriminants are static, collect only components from
14538 -- variants selected by discriminant values.
14540 ------------------------------
14541 -- Collect_Fixed_Components --
14542 ------------------------------
14544 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14546 -- Build association list for discriminants, and find components of the
14547 -- variant part selected by the values of the discriminants.
14549 Old_C := First_Discriminant (Typ);
14550 Discr_Val := First_Elmt (Constraints);
14551 while Present (Old_C) loop
14552 Append_To (Assoc_List,
14553 Make_Component_Association (Loc,
14554 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14555 Expression => New_Copy (Node (Discr_Val))));
14557 Next_Elmt (Discr_Val);
14558 Next_Discriminant (Old_C);
14561 -- The tag and the possible parent component are unconditionally in
14564 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14565 Old_C := First_Component (Typ);
14566 while Present (Old_C) loop
14567 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14568 Append_Elmt (Old_C, Comp_List);
14571 Next_Component (Old_C);
14574 end Collect_Fixed_Components;
14576 ---------------------------
14577 -- Create_All_Components --
14578 ---------------------------
14580 procedure Create_All_Components is
14584 Comp := First_Elmt (Comp_List);
14585 while Present (Comp) loop
14586 Old_C := Node (Comp);
14587 New_C := Create_Component (Old_C);
14591 Constrain_Component_Type
14592 (Old_C, Subt, Decl_Node, Typ, Constraints));
14593 Set_Is_Public (New_C, Is_Public (Subt));
14597 end Create_All_Components;
14599 ----------------------
14600 -- Create_Component --
14601 ----------------------
14603 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14604 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14607 if Ekind (Old_Compon) = E_Discriminant
14608 and then Is_Completely_Hidden (Old_Compon)
14610 -- This is a shadow discriminant created for a discriminant of
14611 -- the parent type, which needs to be present in the subtype.
14612 -- Give the shadow discriminant an internal name that cannot
14613 -- conflict with that of visible components.
14615 Set_Chars (New_Compon, New_Internal_Name ('C'));
14618 -- Set the parent so we have a proper link for freezing etc. This is
14619 -- not a real parent pointer, since of course our parent does not own
14620 -- up to us and reference us, we are an illegitimate child of the
14621 -- original parent.
14623 Set_Parent (New_Compon, Parent (Old_Compon));
14625 -- We do not want this node marked as Comes_From_Source, since
14626 -- otherwise it would get first class status and a separate cross-
14627 -- reference line would be generated. Illegitimate children do not
14628 -- rate such recognition.
14630 Set_Comes_From_Source (New_Compon, False);
14632 -- But it is a real entity, and a birth certificate must be properly
14633 -- registered by entering it into the entity list, and setting its
14634 -- scope to the given subtype. This turns out to be useful for the
14635 -- LLVM code generator, but that scope is not used otherwise.
14637 Enter_Name (New_Compon);
14638 Set_Scope (New_Compon, Subt);
14641 end Create_Component;
14643 -----------------------
14644 -- Is_Variant_Record --
14645 -----------------------
14647 function Is_Variant_Record (T : Entity_Id) return Boolean is
14649 return Nkind (Parent (T)) = N_Full_Type_Declaration
14650 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14651 and then Present (Component_List (Type_Definition (Parent (T))))
14654 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14655 end Is_Variant_Record;
14657 -- Start of processing for Create_Constrained_Components
14660 pragma Assert (Subt /= Base_Type (Subt));
14661 pragma Assert (Typ = Base_Type (Typ));
14663 Set_First_Entity (Subt, Empty);
14664 Set_Last_Entity (Subt, Empty);
14666 -- Check whether constraint is fully static, in which case we can
14667 -- optimize the list of components.
14669 Discr_Val := First_Elmt (Constraints);
14670 while Present (Discr_Val) loop
14671 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14672 Is_Static := False;
14676 Next_Elmt (Discr_Val);
14679 Set_Has_Static_Discriminants (Subt, Is_Static);
14683 -- Inherit the discriminants of the parent type
14685 Add_Discriminants : declare
14691 Old_C := First_Discriminant (Typ);
14693 while Present (Old_C) loop
14694 Num_Disc := Num_Disc + 1;
14695 New_C := Create_Component (Old_C);
14696 Set_Is_Public (New_C, Is_Public (Subt));
14697 Next_Discriminant (Old_C);
14700 -- For an untagged derived subtype, the number of discriminants may
14701 -- be smaller than the number of inherited discriminants, because
14702 -- several of them may be renamed by a single new discriminant or
14703 -- constrained. In this case, add the hidden discriminants back into
14704 -- the subtype, because they need to be present if the optimizer of
14705 -- the GCC 4.x back-end decides to break apart assignments between
14706 -- objects using the parent view into member-wise assignments.
14710 if Is_Derived_Type (Typ)
14711 and then not Is_Tagged_Type (Typ)
14713 Old_C := First_Stored_Discriminant (Typ);
14715 while Present (Old_C) loop
14716 Num_Gird := Num_Gird + 1;
14717 Next_Stored_Discriminant (Old_C);
14721 if Num_Gird > Num_Disc then
14723 -- Find out multiple uses of new discriminants, and add hidden
14724 -- components for the extra renamed discriminants. We recognize
14725 -- multiple uses through the Corresponding_Discriminant of a
14726 -- new discriminant: if it constrains several old discriminants,
14727 -- this field points to the last one in the parent type. The
14728 -- stored discriminants of the derived type have the same name
14729 -- as those of the parent.
14733 New_Discr : Entity_Id;
14734 Old_Discr : Entity_Id;
14737 Constr := First_Elmt (Stored_Constraint (Typ));
14738 Old_Discr := First_Stored_Discriminant (Typ);
14739 while Present (Constr) loop
14740 if Is_Entity_Name (Node (Constr))
14741 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14743 New_Discr := Entity (Node (Constr));
14745 if Chars (Corresponding_Discriminant (New_Discr)) /=
14748 -- The new discriminant has been used to rename a
14749 -- subsequent old discriminant. Introduce a shadow
14750 -- component for the current old discriminant.
14752 New_C := Create_Component (Old_Discr);
14753 Set_Original_Record_Component (New_C, Old_Discr);
14757 -- The constraint has eliminated the old discriminant.
14758 -- Introduce a shadow component.
14760 New_C := Create_Component (Old_Discr);
14761 Set_Original_Record_Component (New_C, Old_Discr);
14764 Next_Elmt (Constr);
14765 Next_Stored_Discriminant (Old_Discr);
14769 end Add_Discriminants;
14772 and then Is_Variant_Record (Typ)
14774 Collect_Fixed_Components (Typ);
14776 Gather_Components (
14778 Component_List (Type_Definition (Parent (Typ))),
14779 Governed_By => Assoc_List,
14781 Report_Errors => Errors);
14782 pragma Assert (not Errors
14783 or else Serious_Errors_Detected > 0);
14785 Create_All_Components;
14787 -- If the subtype declaration is created for a tagged type derivation
14788 -- with constraints, we retrieve the record definition of the parent
14789 -- type to select the components of the proper variant.
14792 and then Is_Tagged_Type (Typ)
14793 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14795 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14796 and then Is_Variant_Record (Parent_Type)
14798 Collect_Fixed_Components (Typ);
14802 Component_List (Type_Definition (Parent (Parent_Type))),
14803 Governed_By => Assoc_List,
14805 Report_Errors => Errors);
14807 -- Note: previously there was a check at this point that no errors
14808 -- were detected. As a consequence of AI05-220 there may be an error
14809 -- if an inherited discriminant that controls a variant has a non-
14810 -- static constraint.
14812 -- If the tagged derivation has a type extension, collect all the
14813 -- new components therein.
14815 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14817 Old_C := First_Component (Typ);
14818 while Present (Old_C) loop
14819 if Original_Record_Component (Old_C) = Old_C
14820 and then Chars (Old_C) /= Name_uTag
14821 and then Chars (Old_C) /= Name_uParent
14823 Append_Elmt (Old_C, Comp_List);
14826 Next_Component (Old_C);
14830 Create_All_Components;
14833 -- If discriminants are not static, or if this is a multi-level type
14834 -- extension, we have to include all components of the parent type.
14836 Old_C := First_Component (Typ);
14837 while Present (Old_C) loop
14838 New_C := Create_Component (Old_C);
14842 Constrain_Component_Type
14843 (Old_C, Subt, Decl_Node, Typ, Constraints));
14844 Set_Is_Public (New_C, Is_Public (Subt));
14846 Next_Component (Old_C);
14851 end Create_Constrained_Components;
14853 ------------------------------------------
14854 -- Decimal_Fixed_Point_Type_Declaration --
14855 ------------------------------------------
14857 procedure Decimal_Fixed_Point_Type_Declaration
14861 Loc : constant Source_Ptr := Sloc (Def);
14862 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14863 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14864 Implicit_Base : Entity_Id;
14871 Check_SPARK_05_Restriction
14872 ("decimal fixed point type is not allowed", Def);
14873 Check_Restriction (No_Fixed_Point, Def);
14875 -- Create implicit base type
14878 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14879 Set_Etype (Implicit_Base, Implicit_Base);
14881 -- Analyze and process delta expression
14883 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14885 Check_Delta_Expression (Delta_Expr);
14886 Delta_Val := Expr_Value_R (Delta_Expr);
14888 -- Check delta is power of 10, and determine scale value from it
14894 Scale_Val := Uint_0;
14897 if Val < Ureal_1 then
14898 while Val < Ureal_1 loop
14899 Val := Val * Ureal_10;
14900 Scale_Val := Scale_Val + 1;
14903 if Scale_Val > 18 then
14904 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14905 Scale_Val := UI_From_Int (+18);
14909 while Val > Ureal_1 loop
14910 Val := Val / Ureal_10;
14911 Scale_Val := Scale_Val - 1;
14914 if Scale_Val < -18 then
14915 Error_Msg_N ("scale is less than minimum value of -18", Def);
14916 Scale_Val := UI_From_Int (-18);
14920 if Val /= Ureal_1 then
14921 Error_Msg_N ("delta expression must be a power of 10", Def);
14922 Delta_Val := Ureal_10 ** (-Scale_Val);
14926 -- Set delta, scale and small (small = delta for decimal type)
14928 Set_Delta_Value (Implicit_Base, Delta_Val);
14929 Set_Scale_Value (Implicit_Base, Scale_Val);
14930 Set_Small_Value (Implicit_Base, Delta_Val);
14932 -- Analyze and process digits expression
14934 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14935 Check_Digits_Expression (Digs_Expr);
14936 Digs_Val := Expr_Value (Digs_Expr);
14938 if Digs_Val > 18 then
14939 Digs_Val := UI_From_Int (+18);
14940 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14943 Set_Digits_Value (Implicit_Base, Digs_Val);
14944 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14946 -- Set range of base type from digits value for now. This will be
14947 -- expanded to represent the true underlying base range by Freeze.
14949 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14951 -- Note: We leave size as zero for now, size will be set at freeze
14952 -- time. We have to do this for ordinary fixed-point, because the size
14953 -- depends on the specified small, and we might as well do the same for
14954 -- decimal fixed-point.
14956 pragma Assert (Esize (Implicit_Base) = Uint_0);
14958 -- If there are bounds given in the declaration use them as the
14959 -- bounds of the first named subtype.
14961 if Present (Real_Range_Specification (Def)) then
14963 RRS : constant Node_Id := Real_Range_Specification (Def);
14964 Low : constant Node_Id := Low_Bound (RRS);
14965 High : constant Node_Id := High_Bound (RRS);
14970 Analyze_And_Resolve (Low, Any_Real);
14971 Analyze_And_Resolve (High, Any_Real);
14972 Check_Real_Bound (Low);
14973 Check_Real_Bound (High);
14974 Low_Val := Expr_Value_R (Low);
14975 High_Val := Expr_Value_R (High);
14977 if Low_Val < (-Bound_Val) then
14979 ("range low bound too small for digits value", Low);
14980 Low_Val := -Bound_Val;
14983 if High_Val > Bound_Val then
14985 ("range high bound too large for digits value", High);
14986 High_Val := Bound_Val;
14989 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14992 -- If no explicit range, use range that corresponds to given
14993 -- digits value. This will end up as the final range for the
14997 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15000 -- Complete entity for first subtype. The inheritance of the rep item
15001 -- chain ensures that SPARK-related pragmas are not clobbered when the
15002 -- decimal fixed point type acts as a full view of a private type.
15004 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15005 Set_Etype (T, Implicit_Base);
15006 Set_Size_Info (T, Implicit_Base);
15007 Inherit_Rep_Item_Chain (T, Implicit_Base);
15008 Set_Digits_Value (T, Digs_Val);
15009 Set_Delta_Value (T, Delta_Val);
15010 Set_Small_Value (T, Delta_Val);
15011 Set_Scale_Value (T, Scale_Val);
15012 Set_Is_Constrained (T);
15013 end Decimal_Fixed_Point_Type_Declaration;
15015 -----------------------------------
15016 -- Derive_Progenitor_Subprograms --
15017 -----------------------------------
15019 procedure Derive_Progenitor_Subprograms
15020 (Parent_Type : Entity_Id;
15021 Tagged_Type : Entity_Id)
15026 Iface_Alias : Entity_Id;
15027 Iface_Elmt : Elmt_Id;
15028 Iface_Subp : Entity_Id;
15029 New_Subp : Entity_Id := Empty;
15030 Prim_Elmt : Elmt_Id;
15035 pragma Assert (Ada_Version >= Ada_2005
15036 and then Is_Record_Type (Tagged_Type)
15037 and then Is_Tagged_Type (Tagged_Type)
15038 and then Has_Interfaces (Tagged_Type));
15040 -- Step 1: Transfer to the full-view primitives associated with the
15041 -- partial-view that cover interface primitives. Conceptually this
15042 -- work should be done later by Process_Full_View; done here to
15043 -- simplify its implementation at later stages. It can be safely
15044 -- done here because interfaces must be visible in the partial and
15045 -- private view (RM 7.3(7.3/2)).
15047 -- Small optimization: This work is only required if the parent may
15048 -- have entities whose Alias attribute reference an interface primitive.
15049 -- Such a situation may occur if the parent is an abstract type and the
15050 -- primitive has not been yet overridden or if the parent is a generic
15051 -- formal type covering interfaces.
15053 -- If the tagged type is not abstract, it cannot have abstract
15054 -- primitives (the only entities in the list of primitives of
15055 -- non-abstract tagged types that can reference abstract primitives
15056 -- through its Alias attribute are the internal entities that have
15057 -- attribute Interface_Alias, and these entities are generated later
15058 -- by Add_Internal_Interface_Entities).
15060 if In_Private_Part (Current_Scope)
15061 and then (Is_Abstract_Type (Parent_Type)
15063 Is_Generic_Type (Parent_Type))
15065 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15066 while Present (Elmt) loop
15067 Subp := Node (Elmt);
15069 -- At this stage it is not possible to have entities in the list
15070 -- of primitives that have attribute Interface_Alias.
15072 pragma Assert (No (Interface_Alias (Subp)));
15074 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15076 if Is_Interface (Typ) then
15077 E := Find_Primitive_Covering_Interface
15078 (Tagged_Type => Tagged_Type,
15079 Iface_Prim => Subp);
15082 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15084 Replace_Elmt (Elmt, E);
15085 Remove_Homonym (Subp);
15093 -- Step 2: Add primitives of progenitors that are not implemented by
15094 -- parents of Tagged_Type.
15096 if Present (Interfaces (Base_Type (Tagged_Type))) then
15097 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15098 while Present (Iface_Elmt) loop
15099 Iface := Node (Iface_Elmt);
15101 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15102 while Present (Prim_Elmt) loop
15103 Iface_Subp := Node (Prim_Elmt);
15104 Iface_Alias := Ultimate_Alias (Iface_Subp);
15106 -- Exclude derivation of predefined primitives except those
15107 -- that come from source, or are inherited from one that comes
15108 -- from source. Required to catch declarations of equality
15109 -- operators of interfaces. For example:
15111 -- type Iface is interface;
15112 -- function "=" (Left, Right : Iface) return Boolean;
15114 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15115 or else Comes_From_Source (Iface_Alias)
15118 Find_Primitive_Covering_Interface
15119 (Tagged_Type => Tagged_Type,
15120 Iface_Prim => Iface_Subp);
15122 -- If not found we derive a new primitive leaving its alias
15123 -- attribute referencing the interface primitive.
15127 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15129 -- Ada 2012 (AI05-0197): If the covering primitive's name
15130 -- differs from the name of the interface primitive then it
15131 -- is a private primitive inherited from a parent type. In
15132 -- such case, given that Tagged_Type covers the interface,
15133 -- the inherited private primitive becomes visible. For such
15134 -- purpose we add a new entity that renames the inherited
15135 -- private primitive.
15137 elsif Chars (E) /= Chars (Iface_Subp) then
15138 pragma Assert (Has_Suffix (E, 'P'));
15140 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15141 Set_Alias (New_Subp, E);
15142 Set_Is_Abstract_Subprogram (New_Subp,
15143 Is_Abstract_Subprogram (E));
15145 -- Propagate to the full view interface entities associated
15146 -- with the partial view.
15148 elsif In_Private_Part (Current_Scope)
15149 and then Present (Alias (E))
15150 and then Alias (E) = Iface_Subp
15152 List_Containing (Parent (E)) /=
15153 Private_Declarations
15155 (Unit_Declaration_Node (Current_Scope)))
15157 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15161 Next_Elmt (Prim_Elmt);
15164 Next_Elmt (Iface_Elmt);
15167 end Derive_Progenitor_Subprograms;
15169 -----------------------
15170 -- Derive_Subprogram --
15171 -----------------------
15173 procedure Derive_Subprogram
15174 (New_Subp : out Entity_Id;
15175 Parent_Subp : Entity_Id;
15176 Derived_Type : Entity_Id;
15177 Parent_Type : Entity_Id;
15178 Actual_Subp : Entity_Id := Empty)
15180 Formal : Entity_Id;
15181 -- Formal parameter of parent primitive operation
15183 Formal_Of_Actual : Entity_Id;
15184 -- Formal parameter of actual operation, when the derivation is to
15185 -- create a renaming for a primitive operation of an actual in an
15188 New_Formal : Entity_Id;
15189 -- Formal of inherited operation
15191 Visible_Subp : Entity_Id := Parent_Subp;
15193 function Is_Private_Overriding return Boolean;
15194 -- If Subp is a private overriding of a visible operation, the inherited
15195 -- operation derives from the overridden op (even though its body is the
15196 -- overriding one) and the inherited operation is visible now. See
15197 -- sem_disp to see the full details of the handling of the overridden
15198 -- subprogram, which is removed from the list of primitive operations of
15199 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15200 -- and used to diagnose abstract operations that need overriding in the
15203 procedure Replace_Type (Id, New_Id : Entity_Id);
15204 -- When the type is an anonymous access type, create a new access type
15205 -- designating the derived type.
15207 procedure Set_Derived_Name;
15208 -- This procedure sets the appropriate Chars name for New_Subp. This
15209 -- is normally just a copy of the parent name. An exception arises for
15210 -- type support subprograms, where the name is changed to reflect the
15211 -- name of the derived type, e.g. if type foo is derived from type bar,
15212 -- then a procedure barDA is derived with a name fooDA.
15214 ---------------------------
15215 -- Is_Private_Overriding --
15216 ---------------------------
15218 function Is_Private_Overriding return Boolean is
15222 -- If the parent is not a dispatching operation there is no
15223 -- need to investigate overridings
15225 if not Is_Dispatching_Operation (Parent_Subp) then
15229 -- The visible operation that is overridden is a homonym of the
15230 -- parent subprogram. We scan the homonym chain to find the one
15231 -- whose alias is the subprogram we are deriving.
15233 Prev := Current_Entity (Parent_Subp);
15234 while Present (Prev) loop
15235 if Ekind (Prev) = Ekind (Parent_Subp)
15236 and then Alias (Prev) = Parent_Subp
15237 and then Scope (Parent_Subp) = Scope (Prev)
15238 and then not Is_Hidden (Prev)
15240 Visible_Subp := Prev;
15244 Prev := Homonym (Prev);
15248 end Is_Private_Overriding;
15254 procedure Replace_Type (Id, New_Id : Entity_Id) is
15255 Id_Type : constant Entity_Id := Etype (Id);
15256 Acc_Type : Entity_Id;
15257 Par : constant Node_Id := Parent (Derived_Type);
15260 -- When the type is an anonymous access type, create a new access
15261 -- type designating the derived type. This itype must be elaborated
15262 -- at the point of the derivation, not on subsequent calls that may
15263 -- be out of the proper scope for Gigi, so we insert a reference to
15264 -- it after the derivation.
15266 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15268 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15271 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15272 and then Present (Full_View (Desig_Typ))
15273 and then not Is_Private_Type (Parent_Type)
15275 Desig_Typ := Full_View (Desig_Typ);
15278 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15280 -- Ada 2005 (AI-251): Handle also derivations of abstract
15281 -- interface primitives.
15283 or else (Is_Interface (Desig_Typ)
15284 and then not Is_Class_Wide_Type (Desig_Typ))
15286 Acc_Type := New_Copy (Id_Type);
15287 Set_Etype (Acc_Type, Acc_Type);
15288 Set_Scope (Acc_Type, New_Subp);
15290 -- Set size of anonymous access type. If we have an access
15291 -- to an unconstrained array, this is a fat pointer, so it
15292 -- is sizes at twice addtress size.
15294 if Is_Array_Type (Desig_Typ)
15295 and then not Is_Constrained (Desig_Typ)
15297 Init_Size (Acc_Type, 2 * System_Address_Size);
15299 -- Other cases use a thin pointer
15302 Init_Size (Acc_Type, System_Address_Size);
15305 -- Set remaining characterstics of anonymous access type
15307 Init_Alignment (Acc_Type);
15308 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15310 Set_Etype (New_Id, Acc_Type);
15311 Set_Scope (New_Id, New_Subp);
15313 -- Create a reference to it
15315 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15318 Set_Etype (New_Id, Id_Type);
15322 -- In Ada2012, a formal may have an incomplete type but the type
15323 -- derivation that inherits the primitive follows the full view.
15325 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15327 (Ekind (Id_Type) = E_Record_Type_With_Private
15328 and then Present (Full_View (Id_Type))
15330 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15332 (Ada_Version >= Ada_2012
15333 and then Ekind (Id_Type) = E_Incomplete_Type
15334 and then Full_View (Id_Type) = Parent_Type)
15336 -- Constraint checks on formals are generated during expansion,
15337 -- based on the signature of the original subprogram. The bounds
15338 -- of the derived type are not relevant, and thus we can use
15339 -- the base type for the formals. However, the return type may be
15340 -- used in a context that requires that the proper static bounds
15341 -- be used (a case statement, for example) and for those cases
15342 -- we must use the derived type (first subtype), not its base.
15344 -- If the derived_type_definition has no constraints, we know that
15345 -- the derived type has the same constraints as the first subtype
15346 -- of the parent, and we can also use it rather than its base,
15347 -- which can lead to more efficient code.
15349 if Etype (Id) = Parent_Type then
15350 if Is_Scalar_Type (Parent_Type)
15352 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15354 Set_Etype (New_Id, Derived_Type);
15356 elsif Nkind (Par) = N_Full_Type_Declaration
15358 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15361 (Subtype_Indication (Type_Definition (Par)))
15363 Set_Etype (New_Id, Derived_Type);
15366 Set_Etype (New_Id, Base_Type (Derived_Type));
15370 Set_Etype (New_Id, Base_Type (Derived_Type));
15374 Set_Etype (New_Id, Etype (Id));
15378 ----------------------
15379 -- Set_Derived_Name --
15380 ----------------------
15382 procedure Set_Derived_Name is
15383 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15385 if Nm = TSS_Null then
15386 Set_Chars (New_Subp, Chars (Parent_Subp));
15388 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15390 end Set_Derived_Name;
15392 -- Start of processing for Derive_Subprogram
15395 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15396 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15398 -- Check whether the inherited subprogram is a private operation that
15399 -- should be inherited but not yet made visible. Such subprograms can
15400 -- become visible at a later point (e.g., the private part of a public
15401 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15402 -- following predicate is true, then this is not such a private
15403 -- operation and the subprogram simply inherits the name of the parent
15404 -- subprogram. Note the special check for the names of controlled
15405 -- operations, which are currently exempted from being inherited with
15406 -- a hidden name because they must be findable for generation of
15407 -- implicit run-time calls.
15409 if not Is_Hidden (Parent_Subp)
15410 or else Is_Internal (Parent_Subp)
15411 or else Is_Private_Overriding
15412 or else Is_Internal_Name (Chars (Parent_Subp))
15413 or else (Is_Controlled (Parent_Type)
15414 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15420 -- An inherited dispatching equality will be overridden by an internally
15421 -- generated one, or by an explicit one, so preserve its name and thus
15422 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15423 -- private operation it may become invisible if the full view has
15424 -- progenitors, and the dispatch table will be malformed.
15425 -- We check that the type is limited to handle the anomalous declaration
15426 -- of Limited_Controlled, which is derived from a non-limited type, and
15427 -- which is handled specially elsewhere as well.
15429 elsif Chars (Parent_Subp) = Name_Op_Eq
15430 and then Is_Dispatching_Operation (Parent_Subp)
15431 and then Etype (Parent_Subp) = Standard_Boolean
15432 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15434 Etype (First_Formal (Parent_Subp)) =
15435 Etype (Next_Formal (First_Formal (Parent_Subp)))
15439 -- If parent is hidden, this can be a regular derivation if the
15440 -- parent is immediately visible in a non-instantiating context,
15441 -- or if we are in the private part of an instance. This test
15442 -- should still be refined ???
15444 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15445 -- operation as a non-visible operation in cases where the parent
15446 -- subprogram might not be visible now, but was visible within the
15447 -- original generic, so it would be wrong to make the inherited
15448 -- subprogram non-visible now. (Not clear if this test is fully
15449 -- correct; are there any cases where we should declare the inherited
15450 -- operation as not visible to avoid it being overridden, e.g., when
15451 -- the parent type is a generic actual with private primitives ???)
15453 -- (they should be treated the same as other private inherited
15454 -- subprograms, but it's not clear how to do this cleanly). ???
15456 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15457 and then Is_Immediately_Visible (Parent_Subp)
15458 and then not In_Instance)
15459 or else In_Instance_Not_Visible
15463 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15464 -- overrides an interface primitive because interface primitives
15465 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15467 elsif Ada_Version >= Ada_2005
15468 and then Is_Dispatching_Operation (Parent_Subp)
15469 and then Present (Covered_Interface_Op (Parent_Subp))
15473 -- Otherwise, the type is inheriting a private operation, so enter it
15474 -- with a special name so it can't be overridden.
15477 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15480 Set_Parent (New_Subp, Parent (Derived_Type));
15482 if Present (Actual_Subp) then
15483 Replace_Type (Actual_Subp, New_Subp);
15485 Replace_Type (Parent_Subp, New_Subp);
15488 Conditional_Delay (New_Subp, Parent_Subp);
15490 -- If we are creating a renaming for a primitive operation of an
15491 -- actual of a generic derived type, we must examine the signature
15492 -- of the actual primitive, not that of the generic formal, which for
15493 -- example may be an interface. However the name and initial value
15494 -- of the inherited operation are those of the formal primitive.
15496 Formal := First_Formal (Parent_Subp);
15498 if Present (Actual_Subp) then
15499 Formal_Of_Actual := First_Formal (Actual_Subp);
15501 Formal_Of_Actual := Empty;
15504 while Present (Formal) loop
15505 New_Formal := New_Copy (Formal);
15507 -- Normally we do not go copying parents, but in the case of
15508 -- formals, we need to link up to the declaration (which is the
15509 -- parameter specification), and it is fine to link up to the
15510 -- original formal's parameter specification in this case.
15512 Set_Parent (New_Formal, Parent (Formal));
15513 Append_Entity (New_Formal, New_Subp);
15515 if Present (Formal_Of_Actual) then
15516 Replace_Type (Formal_Of_Actual, New_Formal);
15517 Next_Formal (Formal_Of_Actual);
15519 Replace_Type (Formal, New_Formal);
15522 Next_Formal (Formal);
15525 -- If this derivation corresponds to a tagged generic actual, then
15526 -- primitive operations rename those of the actual. Otherwise the
15527 -- primitive operations rename those of the parent type, If the parent
15528 -- renames an intrinsic operator, so does the new subprogram. We except
15529 -- concatenation, which is always properly typed, and does not get
15530 -- expanded as other intrinsic operations.
15532 if No (Actual_Subp) then
15533 if Is_Intrinsic_Subprogram (Parent_Subp) then
15534 Set_Is_Intrinsic_Subprogram (New_Subp);
15536 if Present (Alias (Parent_Subp))
15537 and then Chars (Parent_Subp) /= Name_Op_Concat
15539 Set_Alias (New_Subp, Alias (Parent_Subp));
15541 Set_Alias (New_Subp, Parent_Subp);
15545 Set_Alias (New_Subp, Parent_Subp);
15549 Set_Alias (New_Subp, Actual_Subp);
15552 -- Derived subprograms of a tagged type must inherit the convention
15553 -- of the parent subprogram (a requirement of AI-117). Derived
15554 -- subprograms of untagged types simply get convention Ada by default.
15556 -- If the derived type is a tagged generic formal type with unknown
15557 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15559 -- However, if the type is derived from a generic formal, the further
15560 -- inherited subprogram has the convention of the non-generic ancestor.
15561 -- Otherwise there would be no way to override the operation.
15562 -- (This is subject to forthcoming ARG discussions).
15564 if Is_Tagged_Type (Derived_Type) then
15565 if Is_Generic_Type (Derived_Type)
15566 and then Has_Unknown_Discriminants (Derived_Type)
15568 Set_Convention (New_Subp, Convention_Intrinsic);
15571 if Is_Generic_Type (Parent_Type)
15572 and then Has_Unknown_Discriminants (Parent_Type)
15574 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15576 Set_Convention (New_Subp, Convention (Parent_Subp));
15581 -- Predefined controlled operations retain their name even if the parent
15582 -- is hidden (see above), but they are not primitive operations if the
15583 -- ancestor is not visible, for example if the parent is a private
15584 -- extension completed with a controlled extension. Note that a full
15585 -- type that is controlled can break privacy: the flag Is_Controlled is
15586 -- set on both views of the type.
15588 if Is_Controlled (Parent_Type)
15589 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15592 and then Is_Hidden (Parent_Subp)
15593 and then not Is_Visibly_Controlled (Parent_Type)
15595 Set_Is_Hidden (New_Subp);
15598 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15599 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15601 if Ekind (Parent_Subp) = E_Procedure then
15602 Set_Is_Valued_Procedure
15603 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15605 Set_Has_Controlling_Result
15606 (New_Subp, Has_Controlling_Result (Parent_Subp));
15609 -- No_Return must be inherited properly. If this is overridden in the
15610 -- case of a dispatching operation, then a check is made in Sem_Disp
15611 -- that the overriding operation is also No_Return (no such check is
15612 -- required for the case of non-dispatching operation.
15614 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15616 -- A derived function with a controlling result is abstract. If the
15617 -- Derived_Type is a nonabstract formal generic derived type, then
15618 -- inherited operations are not abstract: the required check is done at
15619 -- instantiation time. If the derivation is for a generic actual, the
15620 -- function is not abstract unless the actual is.
15622 if Is_Generic_Type (Derived_Type)
15623 and then not Is_Abstract_Type (Derived_Type)
15627 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15628 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15630 -- A subprogram subject to pragma Extensions_Visible with value False
15631 -- requires overriding if the subprogram has at least one controlling
15632 -- OUT parameter (SPARK RM 6.1.7(6)).
15634 elsif Ada_Version >= Ada_2005
15635 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15636 or else (Is_Tagged_Type (Derived_Type)
15637 and then Etype (New_Subp) = Derived_Type
15638 and then not Is_Null_Extension (Derived_Type))
15639 or else (Is_Tagged_Type (Derived_Type)
15640 and then Ekind (Etype (New_Subp)) =
15641 E_Anonymous_Access_Type
15642 and then Designated_Type (Etype (New_Subp)) =
15644 and then not Is_Null_Extension (Derived_Type))
15645 or else (Comes_From_Source (Alias (New_Subp))
15646 and then Is_EVF_Procedure (Alias (New_Subp))))
15647 and then No (Actual_Subp)
15649 if not Is_Tagged_Type (Derived_Type)
15650 or else Is_Abstract_Type (Derived_Type)
15651 or else Is_Abstract_Subprogram (Alias (New_Subp))
15653 Set_Is_Abstract_Subprogram (New_Subp);
15655 Set_Requires_Overriding (New_Subp);
15658 elsif Ada_Version < Ada_2005
15659 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15660 or else (Is_Tagged_Type (Derived_Type)
15661 and then Etype (New_Subp) = Derived_Type
15662 and then No (Actual_Subp)))
15664 Set_Is_Abstract_Subprogram (New_Subp);
15666 -- AI05-0097 : an inherited operation that dispatches on result is
15667 -- abstract if the derived type is abstract, even if the parent type
15668 -- is concrete and the derived type is a null extension.
15670 elsif Has_Controlling_Result (Alias (New_Subp))
15671 and then Is_Abstract_Type (Etype (New_Subp))
15673 Set_Is_Abstract_Subprogram (New_Subp);
15675 -- Finally, if the parent type is abstract we must verify that all
15676 -- inherited operations are either non-abstract or overridden, or that
15677 -- the derived type itself is abstract (this check is performed at the
15678 -- end of a package declaration, in Check_Abstract_Overriding). A
15679 -- private overriding in the parent type will not be visible in the
15680 -- derivation if we are not in an inner package or in a child unit of
15681 -- the parent type, in which case the abstractness of the inherited
15682 -- operation is carried to the new subprogram.
15684 elsif Is_Abstract_Type (Parent_Type)
15685 and then not In_Open_Scopes (Scope (Parent_Type))
15686 and then Is_Private_Overriding
15687 and then Is_Abstract_Subprogram (Visible_Subp)
15689 if No (Actual_Subp) then
15690 Set_Alias (New_Subp, Visible_Subp);
15691 Set_Is_Abstract_Subprogram (New_Subp, True);
15694 -- If this is a derivation for an instance of a formal derived
15695 -- type, abstractness comes from the primitive operation of the
15696 -- actual, not from the operation inherited from the ancestor.
15698 Set_Is_Abstract_Subprogram
15699 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15703 New_Overloaded_Entity (New_Subp, Derived_Type);
15705 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15706 -- preconditions and the derived type is abstract, the derived operation
15707 -- is abstract as well if parent subprogram is not abstract or null.
15709 if Is_Abstract_Type (Derived_Type)
15710 and then Has_Non_Trivial_Precondition (Parent_Subp)
15711 and then Present (Interfaces (Derived_Type))
15714 -- Add useful attributes of subprogram before the freeze point,
15715 -- in case freezing is delayed or there are previous errors.
15717 Set_Is_Dispatching_Operation (New_Subp);
15720 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15723 if Present (Iface_Prim)
15724 and then Has_Non_Trivial_Precondition (Iface_Prim)
15726 Set_Is_Abstract_Subprogram (New_Subp);
15731 -- Check for case of a derived subprogram for the instantiation of a
15732 -- formal derived tagged type, if so mark the subprogram as dispatching
15733 -- and inherit the dispatching attributes of the actual subprogram. The
15734 -- derived subprogram is effectively renaming of the actual subprogram,
15735 -- so it needs to have the same attributes as the actual.
15737 if Present (Actual_Subp)
15738 and then Is_Dispatching_Operation (Actual_Subp)
15740 Set_Is_Dispatching_Operation (New_Subp);
15742 if Present (DTC_Entity (Actual_Subp)) then
15743 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15744 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15748 -- Indicate that a derived subprogram does not require a body and that
15749 -- it does not require processing of default expressions.
15751 Set_Has_Completion (New_Subp);
15752 Set_Default_Expressions_Processed (New_Subp);
15754 if Ekind (New_Subp) = E_Function then
15755 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15757 end Derive_Subprogram;
15759 ------------------------
15760 -- Derive_Subprograms --
15761 ------------------------
15763 procedure Derive_Subprograms
15764 (Parent_Type : Entity_Id;
15765 Derived_Type : Entity_Id;
15766 Generic_Actual : Entity_Id := Empty)
15768 Op_List : constant Elist_Id :=
15769 Collect_Primitive_Operations (Parent_Type);
15771 function Check_Derived_Type return Boolean;
15772 -- Check that all the entities derived from Parent_Type are found in
15773 -- the list of primitives of Derived_Type exactly in the same order.
15775 procedure Derive_Interface_Subprogram
15776 (New_Subp : out Entity_Id;
15778 Actual_Subp : Entity_Id);
15779 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15780 -- (which is an interface primitive). If Generic_Actual is present then
15781 -- Actual_Subp is the actual subprogram corresponding with the generic
15782 -- subprogram Subp.
15784 ------------------------
15785 -- Check_Derived_Type --
15786 ------------------------
15788 function Check_Derived_Type return Boolean is
15792 New_Subp : Entity_Id;
15797 -- Traverse list of entities in the current scope searching for
15798 -- an incomplete type whose full-view is derived type.
15800 E := First_Entity (Scope (Derived_Type));
15801 while Present (E) and then E /= Derived_Type loop
15802 if Ekind (E) = E_Incomplete_Type
15803 and then Present (Full_View (E))
15804 and then Full_View (E) = Derived_Type
15806 -- Disable this test if Derived_Type completes an incomplete
15807 -- type because in such case more primitives can be added
15808 -- later to the list of primitives of Derived_Type by routine
15809 -- Process_Incomplete_Dependents
15814 E := Next_Entity (E);
15817 List := Collect_Primitive_Operations (Derived_Type);
15818 Elmt := First_Elmt (List);
15820 Op_Elmt := First_Elmt (Op_List);
15821 while Present (Op_Elmt) loop
15822 Subp := Node (Op_Elmt);
15823 New_Subp := Node (Elmt);
15825 -- At this early stage Derived_Type has no entities with attribute
15826 -- Interface_Alias. In addition, such primitives are always
15827 -- located at the end of the list of primitives of Parent_Type.
15828 -- Therefore, if found we can safely stop processing pending
15831 exit when Present (Interface_Alias (Subp));
15833 -- Handle hidden entities
15835 if not Is_Predefined_Dispatching_Operation (Subp)
15836 and then Is_Hidden (Subp)
15838 if Present (New_Subp)
15839 and then Primitive_Names_Match (Subp, New_Subp)
15845 if not Present (New_Subp)
15846 or else Ekind (Subp) /= Ekind (New_Subp)
15847 or else not Primitive_Names_Match (Subp, New_Subp)
15855 Next_Elmt (Op_Elmt);
15859 end Check_Derived_Type;
15861 ---------------------------------
15862 -- Derive_Interface_Subprogram --
15863 ---------------------------------
15865 procedure Derive_Interface_Subprogram
15866 (New_Subp : out Entity_Id;
15868 Actual_Subp : Entity_Id)
15870 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15871 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15874 pragma Assert (Is_Interface (Iface_Type));
15877 (New_Subp => New_Subp,
15878 Parent_Subp => Iface_Subp,
15879 Derived_Type => Derived_Type,
15880 Parent_Type => Iface_Type,
15881 Actual_Subp => Actual_Subp);
15883 -- Given that this new interface entity corresponds with a primitive
15884 -- of the parent that was not overridden we must leave it associated
15885 -- with its parent primitive to ensure that it will share the same
15886 -- dispatch table slot when overridden. We must set the Alias to Subp
15887 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15888 -- (in case we inherited Subp from Iface_Type via a nonabstract
15889 -- generic formal type).
15891 if No (Actual_Subp) then
15892 Set_Alias (New_Subp, Subp);
15895 T : Entity_Id := Find_Dispatching_Type (Subp);
15897 while Etype (T) /= T loop
15898 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15899 Set_Is_Abstract_Subprogram (New_Subp, False);
15907 -- For instantiations this is not needed since the previous call to
15908 -- Derive_Subprogram leaves the entity well decorated.
15911 pragma Assert (Alias (New_Subp) = Actual_Subp);
15914 end Derive_Interface_Subprogram;
15918 Alias_Subp : Entity_Id;
15919 Act_List : Elist_Id;
15920 Act_Elmt : Elmt_Id;
15921 Act_Subp : Entity_Id := Empty;
15923 Need_Search : Boolean := False;
15924 New_Subp : Entity_Id := Empty;
15925 Parent_Base : Entity_Id;
15928 -- Start of processing for Derive_Subprograms
15931 if Ekind (Parent_Type) = E_Record_Type_With_Private
15932 and then Has_Discriminants (Parent_Type)
15933 and then Present (Full_View (Parent_Type))
15935 Parent_Base := Full_View (Parent_Type);
15937 Parent_Base := Parent_Type;
15940 if Present (Generic_Actual) then
15941 Act_List := Collect_Primitive_Operations (Generic_Actual);
15942 Act_Elmt := First_Elmt (Act_List);
15944 Act_List := No_Elist;
15945 Act_Elmt := No_Elmt;
15948 -- Derive primitives inherited from the parent. Note that if the generic
15949 -- actual is present, this is not really a type derivation, it is a
15950 -- completion within an instance.
15952 -- Case 1: Derived_Type does not implement interfaces
15954 if not Is_Tagged_Type (Derived_Type)
15955 or else (not Has_Interfaces (Derived_Type)
15956 and then not (Present (Generic_Actual)
15957 and then Has_Interfaces (Generic_Actual)))
15959 Elmt := First_Elmt (Op_List);
15960 while Present (Elmt) loop
15961 Subp := Node (Elmt);
15963 -- Literals are derived earlier in the process of building the
15964 -- derived type, and are skipped here.
15966 if Ekind (Subp) = E_Enumeration_Literal then
15969 -- The actual is a direct descendant and the common primitive
15970 -- operations appear in the same order.
15972 -- If the generic parent type is present, the derived type is an
15973 -- instance of a formal derived type, and within the instance its
15974 -- operations are those of the actual. We derive from the formal
15975 -- type but make the inherited operations aliases of the
15976 -- corresponding operations of the actual.
15979 pragma Assert (No (Node (Act_Elmt))
15980 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15983 (Subp, Node (Act_Elmt),
15984 Skip_Controlling_Formals => True)));
15987 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15989 if Present (Act_Elmt) then
15990 Next_Elmt (Act_Elmt);
15997 -- Case 2: Derived_Type implements interfaces
16000 -- If the parent type has no predefined primitives we remove
16001 -- predefined primitives from the list of primitives of generic
16002 -- actual to simplify the complexity of this algorithm.
16004 if Present (Generic_Actual) then
16006 Has_Predefined_Primitives : Boolean := False;
16009 -- Check if the parent type has predefined primitives
16011 Elmt := First_Elmt (Op_List);
16012 while Present (Elmt) loop
16013 Subp := Node (Elmt);
16015 if Is_Predefined_Dispatching_Operation (Subp)
16016 and then not Comes_From_Source (Ultimate_Alias (Subp))
16018 Has_Predefined_Primitives := True;
16025 -- Remove predefined primitives of Generic_Actual. We must use
16026 -- an auxiliary list because in case of tagged types the value
16027 -- returned by Collect_Primitive_Operations is the value stored
16028 -- in its Primitive_Operations attribute (and we don't want to
16029 -- modify its current contents).
16031 if not Has_Predefined_Primitives then
16033 Aux_List : constant Elist_Id := New_Elmt_List;
16036 Elmt := First_Elmt (Act_List);
16037 while Present (Elmt) loop
16038 Subp := Node (Elmt);
16040 if not Is_Predefined_Dispatching_Operation (Subp)
16041 or else Comes_From_Source (Subp)
16043 Append_Elmt (Subp, Aux_List);
16049 Act_List := Aux_List;
16053 Act_Elmt := First_Elmt (Act_List);
16054 Act_Subp := Node (Act_Elmt);
16058 -- Stage 1: If the generic actual is not present we derive the
16059 -- primitives inherited from the parent type. If the generic parent
16060 -- type is present, the derived type is an instance of a formal
16061 -- derived type, and within the instance its operations are those of
16062 -- the actual. We derive from the formal type but make the inherited
16063 -- operations aliases of the corresponding operations of the actual.
16065 Elmt := First_Elmt (Op_List);
16066 while Present (Elmt) loop
16067 Subp := Node (Elmt);
16068 Alias_Subp := Ultimate_Alias (Subp);
16070 -- Do not derive internal entities of the parent that link
16071 -- interface primitives with their covering primitive. These
16072 -- entities will be added to this type when frozen.
16074 if Present (Interface_Alias (Subp)) then
16078 -- If the generic actual is present find the corresponding
16079 -- operation in the generic actual. If the parent type is a
16080 -- direct ancestor of the derived type then, even if it is an
16081 -- interface, the operations are inherited from the primary
16082 -- dispatch table and are in the proper order. If we detect here
16083 -- that primitives are not in the same order we traverse the list
16084 -- of primitive operations of the actual to find the one that
16085 -- implements the interface primitive.
16089 (Present (Generic_Actual)
16090 and then Present (Act_Subp)
16092 (Primitive_Names_Match (Subp, Act_Subp)
16094 Type_Conformant (Subp, Act_Subp,
16095 Skip_Controlling_Formals => True)))
16097 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16098 Use_Full_View => True));
16100 -- Remember that we need searching for all pending primitives
16102 Need_Search := True;
16104 -- Handle entities associated with interface primitives
16106 if Present (Alias_Subp)
16107 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16108 and then not Is_Predefined_Dispatching_Operation (Subp)
16110 -- Search for the primitive in the homonym chain
16113 Find_Primitive_Covering_Interface
16114 (Tagged_Type => Generic_Actual,
16115 Iface_Prim => Alias_Subp);
16117 -- Previous search may not locate primitives covering
16118 -- interfaces defined in generics units or instantiations.
16119 -- (it fails if the covering primitive has formals whose
16120 -- type is also defined in generics or instantiations).
16121 -- In such case we search in the list of primitives of the
16122 -- generic actual for the internal entity that links the
16123 -- interface primitive and the covering primitive.
16126 and then Is_Generic_Type (Parent_Type)
16128 -- This code has been designed to handle only generic
16129 -- formals that implement interfaces that are defined
16130 -- in a generic unit or instantiation. If this code is
16131 -- needed for other cases we must review it because
16132 -- (given that it relies on Original_Location to locate
16133 -- the primitive of Generic_Actual that covers the
16134 -- interface) it could leave linked through attribute
16135 -- Alias entities of unrelated instantiations).
16139 (Scope (Find_Dispatching_Type (Alias_Subp)))
16141 Instantiation_Depth
16142 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16145 Iface_Prim_Loc : constant Source_Ptr :=
16146 Original_Location (Sloc (Alias_Subp));
16153 First_Elmt (Primitive_Operations (Generic_Actual));
16155 Search : while Present (Elmt) loop
16156 Prim := Node (Elmt);
16158 if Present (Interface_Alias (Prim))
16159 and then Original_Location
16160 (Sloc (Interface_Alias (Prim))) =
16163 Act_Subp := Alias (Prim);
16172 pragma Assert (Present (Act_Subp)
16173 or else Is_Abstract_Type (Generic_Actual)
16174 or else Serious_Errors_Detected > 0);
16176 -- Handle predefined primitives plus the rest of user-defined
16180 Act_Elmt := First_Elmt (Act_List);
16181 while Present (Act_Elmt) loop
16182 Act_Subp := Node (Act_Elmt);
16184 exit when Primitive_Names_Match (Subp, Act_Subp)
16185 and then Type_Conformant
16187 Skip_Controlling_Formals => True)
16188 and then No (Interface_Alias (Act_Subp));
16190 Next_Elmt (Act_Elmt);
16193 if No (Act_Elmt) then
16199 -- Case 1: If the parent is a limited interface then it has the
16200 -- predefined primitives of synchronized interfaces. However, the
16201 -- actual type may be a non-limited type and hence it does not
16202 -- have such primitives.
16204 if Present (Generic_Actual)
16205 and then not Present (Act_Subp)
16206 and then Is_Limited_Interface (Parent_Base)
16207 and then Is_Predefined_Interface_Primitive (Subp)
16211 -- Case 2: Inherit entities associated with interfaces that were
16212 -- not covered by the parent type. We exclude here null interface
16213 -- primitives because they do not need special management.
16215 -- We also exclude interface operations that are renamings. If the
16216 -- subprogram is an explicit renaming of an interface primitive,
16217 -- it is a regular primitive operation, and the presence of its
16218 -- alias is not relevant: it has to be derived like any other
16221 elsif Present (Alias (Subp))
16222 and then Nkind (Unit_Declaration_Node (Subp)) /=
16223 N_Subprogram_Renaming_Declaration
16224 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16226 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16227 and then Null_Present (Parent (Alias_Subp)))
16229 -- If this is an abstract private type then we transfer the
16230 -- derivation of the interface primitive from the partial view
16231 -- to the full view. This is safe because all the interfaces
16232 -- must be visible in the partial view. Done to avoid adding
16233 -- a new interface derivation to the private part of the
16234 -- enclosing package; otherwise this new derivation would be
16235 -- decorated as hidden when the analysis of the enclosing
16236 -- package completes.
16238 if Is_Abstract_Type (Derived_Type)
16239 and then In_Private_Part (Current_Scope)
16240 and then Has_Private_Declaration (Derived_Type)
16243 Partial_View : Entity_Id;
16248 Partial_View := First_Entity (Current_Scope);
16250 exit when No (Partial_View)
16251 or else (Has_Private_Declaration (Partial_View)
16253 Full_View (Partial_View) = Derived_Type);
16255 Next_Entity (Partial_View);
16258 -- If the partial view was not found then the source code
16259 -- has errors and the derivation is not needed.
16261 if Present (Partial_View) then
16263 First_Elmt (Primitive_Operations (Partial_View));
16264 while Present (Elmt) loop
16265 Ent := Node (Elmt);
16267 if Present (Alias (Ent))
16268 and then Ultimate_Alias (Ent) = Alias (Subp)
16271 (Ent, Primitive_Operations (Derived_Type));
16278 -- If the interface primitive was not found in the
16279 -- partial view then this interface primitive was
16280 -- overridden. We add a derivation to activate in
16281 -- Derive_Progenitor_Subprograms the machinery to
16285 Derive_Interface_Subprogram
16286 (New_Subp => New_Subp,
16288 Actual_Subp => Act_Subp);
16293 Derive_Interface_Subprogram
16294 (New_Subp => New_Subp,
16296 Actual_Subp => Act_Subp);
16299 -- Case 3: Common derivation
16303 (New_Subp => New_Subp,
16304 Parent_Subp => Subp,
16305 Derived_Type => Derived_Type,
16306 Parent_Type => Parent_Base,
16307 Actual_Subp => Act_Subp);
16310 -- No need to update Act_Elm if we must search for the
16311 -- corresponding operation in the generic actual
16314 and then Present (Act_Elmt)
16316 Next_Elmt (Act_Elmt);
16317 Act_Subp := Node (Act_Elmt);
16324 -- Inherit additional operations from progenitors. If the derived
16325 -- type is a generic actual, there are not new primitive operations
16326 -- for the type because it has those of the actual, and therefore
16327 -- nothing needs to be done. The renamings generated above are not
16328 -- primitive operations, and their purpose is simply to make the
16329 -- proper operations visible within an instantiation.
16331 if No (Generic_Actual) then
16332 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16336 -- Final check: Direct descendants must have their primitives in the
16337 -- same order. We exclude from this test untagged types and instances
16338 -- of formal derived types. We skip this test if we have already
16339 -- reported serious errors in the sources.
16341 pragma Assert (not Is_Tagged_Type (Derived_Type)
16342 or else Present (Generic_Actual)
16343 or else Serious_Errors_Detected > 0
16344 or else Check_Derived_Type);
16345 end Derive_Subprograms;
16347 --------------------------------
16348 -- Derived_Standard_Character --
16349 --------------------------------
16351 procedure Derived_Standard_Character
16353 Parent_Type : Entity_Id;
16354 Derived_Type : Entity_Id)
16356 Loc : constant Source_Ptr := Sloc (N);
16357 Def : constant Node_Id := Type_Definition (N);
16358 Indic : constant Node_Id := Subtype_Indication (Def);
16359 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16360 Implicit_Base : constant Entity_Id :=
16362 (E_Enumeration_Type, N, Derived_Type, 'B');
16368 Discard_Node (Process_Subtype (Indic, N));
16370 Set_Etype (Implicit_Base, Parent_Base);
16371 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16372 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16374 Set_Is_Character_Type (Implicit_Base, True);
16375 Set_Has_Delayed_Freeze (Implicit_Base);
16377 -- The bounds of the implicit base are the bounds of the parent base.
16378 -- Note that their type is the parent base.
16380 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16381 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16383 Set_Scalar_Range (Implicit_Base,
16386 High_Bound => Hi));
16388 Conditional_Delay (Derived_Type, Parent_Type);
16390 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16391 Set_Etype (Derived_Type, Implicit_Base);
16392 Set_Size_Info (Derived_Type, Parent_Type);
16394 if Unknown_RM_Size (Derived_Type) then
16395 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16398 Set_Is_Character_Type (Derived_Type, True);
16400 if Nkind (Indic) /= N_Subtype_Indication then
16402 -- If no explicit constraint, the bounds are those
16403 -- of the parent type.
16405 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16406 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16407 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16410 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16412 -- Because the implicit base is used in the conversion of the bounds, we
16413 -- have to freeze it now. This is similar to what is done for numeric
16414 -- types, and it equally suspicious, but otherwise a non-static bound
16415 -- will have a reference to an unfrozen type, which is rejected by Gigi
16416 -- (???). This requires specific care for definition of stream
16417 -- attributes. For details, see comments at the end of
16418 -- Build_Derived_Numeric_Type.
16420 Freeze_Before (N, Implicit_Base);
16421 end Derived_Standard_Character;
16423 ------------------------------
16424 -- Derived_Type_Declaration --
16425 ------------------------------
16427 procedure Derived_Type_Declaration
16430 Is_Completion : Boolean)
16432 Parent_Type : Entity_Id;
16434 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16435 -- Check whether the parent type is a generic formal, or derives
16436 -- directly or indirectly from one.
16438 ------------------------
16439 -- Comes_From_Generic --
16440 ------------------------
16442 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16444 if Is_Generic_Type (Typ) then
16447 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16450 elsif Is_Private_Type (Typ)
16451 and then Present (Full_View (Typ))
16452 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16456 elsif Is_Generic_Actual_Type (Typ) then
16462 end Comes_From_Generic;
16466 Def : constant Node_Id := Type_Definition (N);
16467 Iface_Def : Node_Id;
16468 Indic : constant Node_Id := Subtype_Indication (Def);
16469 Extension : constant Node_Id := Record_Extension_Part (Def);
16470 Parent_Node : Node_Id;
16473 -- Start of processing for Derived_Type_Declaration
16476 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16479 and then Is_Tagged_Type (Parent_Type)
16482 Partial_View : constant Entity_Id :=
16483 Incomplete_Or_Partial_View (Parent_Type);
16486 -- If the partial view was not found then the parent type is not
16487 -- a private type. Otherwise check if the partial view is a tagged
16490 if Present (Partial_View)
16491 and then Is_Private_Type (Partial_View)
16492 and then not Is_Tagged_Type (Partial_View)
16495 ("cannot derive from & declared as untagged private "
16496 & "(SPARK RM 3.4(1))", N, Partial_View);
16501 -- Ada 2005 (AI-251): In case of interface derivation check that the
16502 -- parent is also an interface.
16504 if Interface_Present (Def) then
16505 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16507 if not Is_Interface (Parent_Type) then
16508 Diagnose_Interface (Indic, Parent_Type);
16511 Parent_Node := Parent (Base_Type (Parent_Type));
16512 Iface_Def := Type_Definition (Parent_Node);
16514 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16515 -- other limited interfaces.
16517 if Limited_Present (Def) then
16518 if Limited_Present (Iface_Def) then
16521 elsif Protected_Present (Iface_Def) then
16523 ("descendant of & must be declared as a protected "
16524 & "interface", N, Parent_Type);
16526 elsif Synchronized_Present (Iface_Def) then
16528 ("descendant of & must be declared as a synchronized "
16529 & "interface", N, Parent_Type);
16531 elsif Task_Present (Iface_Def) then
16533 ("descendant of & must be declared as a task interface",
16538 ("(Ada 2005) limited interface cannot inherit from "
16539 & "non-limited interface", Indic);
16542 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16543 -- from non-limited or limited interfaces.
16545 elsif not Protected_Present (Def)
16546 and then not Synchronized_Present (Def)
16547 and then not Task_Present (Def)
16549 if Limited_Present (Iface_Def) then
16552 elsif Protected_Present (Iface_Def) then
16554 ("descendant of & must be declared as a protected "
16555 & "interface", N, Parent_Type);
16557 elsif Synchronized_Present (Iface_Def) then
16559 ("descendant of & must be declared as a synchronized "
16560 & "interface", N, Parent_Type);
16562 elsif Task_Present (Iface_Def) then
16564 ("descendant of & must be declared as a task interface",
16573 if Is_Tagged_Type (Parent_Type)
16574 and then Is_Concurrent_Type (Parent_Type)
16575 and then not Is_Interface (Parent_Type)
16578 ("parent type of a record extension cannot be a synchronized "
16579 & "tagged type (RM 3.9.1 (3/1))", N);
16580 Set_Etype (T, Any_Type);
16584 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16587 if Is_Tagged_Type (Parent_Type)
16588 and then Is_Non_Empty_List (Interface_List (Def))
16595 Intf := First (Interface_List (Def));
16596 while Present (Intf) loop
16597 T := Find_Type_Of_Subtype_Indic (Intf);
16599 if not Is_Interface (T) then
16600 Diagnose_Interface (Intf, T);
16602 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16603 -- a limited type from having a nonlimited progenitor.
16605 elsif (Limited_Present (Def)
16606 or else (not Is_Interface (Parent_Type)
16607 and then Is_Limited_Type (Parent_Type)))
16608 and then not Is_Limited_Interface (T)
16611 ("progenitor interface& of limited type must be limited",
16620 if Parent_Type = Any_Type
16621 or else Etype (Parent_Type) = Any_Type
16622 or else (Is_Class_Wide_Type (Parent_Type)
16623 and then Etype (Parent_Type) = T)
16625 -- If Parent_Type is undefined or illegal, make new type into a
16626 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16627 -- errors. If this is a self-definition, emit error now.
16629 if T = Parent_Type or else T = Etype (Parent_Type) then
16630 Error_Msg_N ("type cannot be used in its own definition", Indic);
16633 Set_Ekind (T, Ekind (Parent_Type));
16634 Set_Etype (T, Any_Type);
16635 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16637 if Is_Tagged_Type (T)
16638 and then Is_Record_Type (T)
16640 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16646 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16647 -- an interface is special because the list of interfaces in the full
16648 -- view can be given in any order. For example:
16650 -- type A is interface;
16651 -- type B is interface and A;
16652 -- type D is new B with private;
16654 -- type D is new A and B with null record; -- 1 --
16656 -- In this case we perform the following transformation of -1-:
16658 -- type D is new B and A with null record;
16660 -- If the parent of the full-view covers the parent of the partial-view
16661 -- we have two possible cases:
16663 -- 1) They have the same parent
16664 -- 2) The parent of the full-view implements some further interfaces
16666 -- In both cases we do not need to perform the transformation. In the
16667 -- first case the source program is correct and the transformation is
16668 -- not needed; in the second case the source program does not fulfill
16669 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16672 -- This transformation not only simplifies the rest of the analysis of
16673 -- this type declaration but also simplifies the correct generation of
16674 -- the object layout to the expander.
16676 if In_Private_Part (Current_Scope)
16677 and then Is_Interface (Parent_Type)
16681 Partial_View : Entity_Id;
16682 Partial_View_Parent : Entity_Id;
16683 New_Iface : Node_Id;
16686 -- Look for the associated private type declaration
16688 Partial_View := Incomplete_Or_Partial_View (T);
16690 -- If the partial view was not found then the source code has
16691 -- errors and the transformation is not needed.
16693 if Present (Partial_View) then
16694 Partial_View_Parent := Etype (Partial_View);
16696 -- If the parent of the full-view covers the parent of the
16697 -- partial-view we have nothing else to do.
16699 if Interface_Present_In_Ancestor
16700 (Parent_Type, Partial_View_Parent)
16704 -- Traverse the list of interfaces of the full-view to look
16705 -- for the parent of the partial-view and perform the tree
16709 Iface := First (Interface_List (Def));
16710 while Present (Iface) loop
16711 if Etype (Iface) = Etype (Partial_View) then
16712 Rewrite (Subtype_Indication (Def),
16713 New_Copy (Subtype_Indication
16714 (Parent (Partial_View))));
16717 Make_Identifier (Sloc (N), Chars (Parent_Type));
16718 Append (New_Iface, Interface_List (Def));
16720 -- Analyze the transformed code
16722 Derived_Type_Declaration (T, N, Is_Completion);
16733 -- Only composite types other than array types are allowed to have
16736 if Present (Discriminant_Specifications (N)) then
16737 if (Is_Elementary_Type (Parent_Type)
16739 Is_Array_Type (Parent_Type))
16740 and then not Error_Posted (N)
16743 ("elementary or array type cannot have discriminants",
16744 Defining_Identifier (First (Discriminant_Specifications (N))));
16746 -- Unset Has_Discriminants flag to prevent cascaded errors, but
16747 -- only if we are not already processing a malformed syntax tree.
16749 if Is_Type (T) then
16750 Set_Has_Discriminants (T, False);
16753 -- The type is allowed to have discriminants
16756 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16760 -- In Ada 83, a derived type defined in a package specification cannot
16761 -- be used for further derivation until the end of its visible part.
16762 -- Note that derivation in the private part of the package is allowed.
16764 if Ada_Version = Ada_83
16765 and then Is_Derived_Type (Parent_Type)
16766 and then In_Visible_Part (Scope (Parent_Type))
16768 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16770 ("(Ada 83): premature use of type for derivation", Indic);
16774 -- Check for early use of incomplete or private type
16776 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16777 Error_Msg_N ("premature derivation of incomplete type", Indic);
16780 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16781 and then not Comes_From_Generic (Parent_Type))
16782 or else Has_Private_Component (Parent_Type)
16784 -- The ancestor type of a formal type can be incomplete, in which
16785 -- case only the operations of the partial view are available in the
16786 -- generic. Subsequent checks may be required when the full view is
16787 -- analyzed to verify that a derivation from a tagged type has an
16790 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16793 elsif No (Underlying_Type (Parent_Type))
16794 or else Has_Private_Component (Parent_Type)
16797 ("premature derivation of derived or private type", Indic);
16799 -- Flag the type itself as being in error, this prevents some
16800 -- nasty problems with subsequent uses of the malformed type.
16802 Set_Error_Posted (T);
16804 -- Check that within the immediate scope of an untagged partial
16805 -- view it's illegal to derive from the partial view if the
16806 -- full view is tagged. (7.3(7))
16808 -- We verify that the Parent_Type is a partial view by checking
16809 -- that it is not a Full_Type_Declaration (i.e. a private type or
16810 -- private extension declaration), to distinguish a partial view
16811 -- from a derivation from a private type which also appears as
16812 -- E_Private_Type. If the parent base type is not declared in an
16813 -- enclosing scope there is no need to check.
16815 elsif Present (Full_View (Parent_Type))
16816 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16817 and then not Is_Tagged_Type (Parent_Type)
16818 and then Is_Tagged_Type (Full_View (Parent_Type))
16819 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16822 ("premature derivation from type with tagged full view",
16827 -- Check that form of derivation is appropriate
16829 Taggd := Is_Tagged_Type (Parent_Type);
16831 -- Set the parent type to the class-wide type's specific type in this
16832 -- case to prevent cascading errors
16834 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16835 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16836 Set_Etype (T, Etype (Parent_Type));
16840 if Present (Extension) and then not Taggd then
16842 ("type derived from untagged type cannot have extension", Indic);
16844 elsif No (Extension) and then Taggd then
16846 -- If this declaration is within a private part (or body) of a
16847 -- generic instantiation then the derivation is allowed (the parent
16848 -- type can only appear tagged in this case if it's a generic actual
16849 -- type, since it would otherwise have been rejected in the analysis
16850 -- of the generic template).
16852 if not Is_Generic_Actual_Type (Parent_Type)
16853 or else In_Visible_Part (Scope (Parent_Type))
16855 if Is_Class_Wide_Type (Parent_Type) then
16857 ("parent type must not be a class-wide type", Indic);
16859 -- Use specific type to prevent cascaded errors.
16861 Parent_Type := Etype (Parent_Type);
16865 ("type derived from tagged type must have extension", Indic);
16870 -- AI-443: Synchronized formal derived types require a private
16871 -- extension. There is no point in checking the ancestor type or
16872 -- the progenitors since the construct is wrong to begin with.
16874 if Ada_Version >= Ada_2005
16875 and then Is_Generic_Type (T)
16876 and then Present (Original_Node (N))
16879 Decl : constant Node_Id := Original_Node (N);
16882 if Nkind (Decl) = N_Formal_Type_Declaration
16883 and then Nkind (Formal_Type_Definition (Decl)) =
16884 N_Formal_Derived_Type_Definition
16885 and then Synchronized_Present (Formal_Type_Definition (Decl))
16886 and then No (Extension)
16888 -- Avoid emitting a duplicate error message
16890 and then not Error_Posted (Indic)
16893 ("synchronized derived type must have extension", N);
16898 if Null_Exclusion_Present (Def)
16899 and then not Is_Access_Type (Parent_Type)
16901 Error_Msg_N ("null exclusion can only apply to an access type", N);
16904 -- Avoid deriving parent primitives of underlying record views
16906 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16907 Derive_Subps => not Is_Underlying_Record_View (T));
16909 -- AI-419: The parent type of an explicitly limited derived type must
16910 -- be a limited type or a limited interface.
16912 if Limited_Present (Def) then
16913 Set_Is_Limited_Record (T);
16915 if Is_Interface (T) then
16916 Set_Is_Limited_Interface (T);
16919 if not Is_Limited_Type (Parent_Type)
16921 (not Is_Interface (Parent_Type)
16922 or else not Is_Limited_Interface (Parent_Type))
16924 -- AI05-0096: a derivation in the private part of an instance is
16925 -- legal if the generic formal is untagged limited, and the actual
16928 if Is_Generic_Actual_Type (Parent_Type)
16929 and then In_Private_Part (Current_Scope)
16932 (Generic_Parent_Type (Parent (Parent_Type)))
16938 ("parent type& of limited type must be limited",
16944 -- In SPARK, there are no derived type definitions other than type
16945 -- extensions of tagged record types.
16947 if No (Extension) then
16948 Check_SPARK_05_Restriction
16949 ("derived type is not allowed", Original_Node (N));
16951 end Derived_Type_Declaration;
16953 ------------------------
16954 -- Diagnose_Interface --
16955 ------------------------
16957 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16959 if not Is_Interface (E) and then E /= Any_Type then
16960 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16962 end Diagnose_Interface;
16964 ----------------------------------
16965 -- Enumeration_Type_Declaration --
16966 ----------------------------------
16968 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16975 -- Create identifier node representing lower bound
16977 B_Node := New_Node (N_Identifier, Sloc (Def));
16978 L := First (Literals (Def));
16979 Set_Chars (B_Node, Chars (L));
16980 Set_Entity (B_Node, L);
16981 Set_Etype (B_Node, T);
16982 Set_Is_Static_Expression (B_Node, True);
16984 R_Node := New_Node (N_Range, Sloc (Def));
16985 Set_Low_Bound (R_Node, B_Node);
16987 Set_Ekind (T, E_Enumeration_Type);
16988 Set_First_Literal (T, L);
16990 Set_Is_Constrained (T);
16994 -- Loop through literals of enumeration type setting pos and rep values
16995 -- except that if the Ekind is already set, then it means the literal
16996 -- was already constructed (case of a derived type declaration and we
16997 -- should not disturb the Pos and Rep values.
16999 while Present (L) loop
17000 if Ekind (L) /= E_Enumeration_Literal then
17001 Set_Ekind (L, E_Enumeration_Literal);
17002 Set_Enumeration_Pos (L, Ev);
17003 Set_Enumeration_Rep (L, Ev);
17004 Set_Is_Known_Valid (L, True);
17008 New_Overloaded_Entity (L);
17009 Generate_Definition (L);
17010 Set_Convention (L, Convention_Intrinsic);
17012 -- Case of character literal
17014 if Nkind (L) = N_Defining_Character_Literal then
17015 Set_Is_Character_Type (T, True);
17017 -- Check violation of No_Wide_Characters
17019 if Restriction_Check_Required (No_Wide_Characters) then
17020 Get_Name_String (Chars (L));
17022 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17023 Check_Restriction (No_Wide_Characters, L);
17032 -- Now create a node representing upper bound
17034 B_Node := New_Node (N_Identifier, Sloc (Def));
17035 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17036 Set_Entity (B_Node, Last (Literals (Def)));
17037 Set_Etype (B_Node, T);
17038 Set_Is_Static_Expression (B_Node, True);
17040 Set_High_Bound (R_Node, B_Node);
17042 -- Initialize various fields of the type. Some of this information
17043 -- may be overwritten later through rep.clauses.
17045 Set_Scalar_Range (T, R_Node);
17046 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17047 Set_Enum_Esize (T);
17048 Set_Enum_Pos_To_Rep (T, Empty);
17050 -- Set Discard_Names if configuration pragma set, or if there is
17051 -- a parameterless pragma in the current declarative region
17053 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17054 Set_Discard_Names (T);
17057 -- Process end label if there is one
17059 if Present (Def) then
17060 Process_End_Label (Def, 'e', T);
17062 end Enumeration_Type_Declaration;
17064 ---------------------------------
17065 -- Expand_To_Stored_Constraint --
17066 ---------------------------------
17068 function Expand_To_Stored_Constraint
17070 Constraint : Elist_Id) return Elist_Id
17072 Explicitly_Discriminated_Type : Entity_Id;
17073 Expansion : Elist_Id;
17074 Discriminant : Entity_Id;
17076 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17077 -- Find the nearest type that actually specifies discriminants
17079 ---------------------------------
17080 -- Type_With_Explicit_Discrims --
17081 ---------------------------------
17083 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17084 Typ : constant E := Base_Type (Id);
17087 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17088 if Present (Full_View (Typ)) then
17089 return Type_With_Explicit_Discrims (Full_View (Typ));
17093 if Has_Discriminants (Typ) then
17098 if Etype (Typ) = Typ then
17100 elsif Has_Discriminants (Typ) then
17103 return Type_With_Explicit_Discrims (Etype (Typ));
17106 end Type_With_Explicit_Discrims;
17108 -- Start of processing for Expand_To_Stored_Constraint
17111 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17115 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17117 if No (Explicitly_Discriminated_Type) then
17121 Expansion := New_Elmt_List;
17124 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17125 while Present (Discriminant) loop
17127 (Get_Discriminant_Value
17128 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17130 Next_Stored_Discriminant (Discriminant);
17134 end Expand_To_Stored_Constraint;
17136 ---------------------------
17137 -- Find_Hidden_Interface --
17138 ---------------------------
17140 function Find_Hidden_Interface
17142 Dest : Elist_Id) return Entity_Id
17145 Iface_Elmt : Elmt_Id;
17148 if Present (Src) and then Present (Dest) then
17149 Iface_Elmt := First_Elmt (Src);
17150 while Present (Iface_Elmt) loop
17151 Iface := Node (Iface_Elmt);
17153 if Is_Interface (Iface)
17154 and then not Contain_Interface (Iface, Dest)
17159 Next_Elmt (Iface_Elmt);
17164 end Find_Hidden_Interface;
17166 --------------------
17167 -- Find_Type_Name --
17168 --------------------
17170 function Find_Type_Name (N : Node_Id) return Entity_Id is
17171 Id : constant Entity_Id := Defining_Identifier (N);
17172 New_Id : Entity_Id;
17174 Prev_Par : Node_Id;
17176 procedure Check_Duplicate_Aspects;
17177 -- Check that aspects specified in a completion have not been specified
17178 -- already in the partial view.
17180 procedure Tag_Mismatch;
17181 -- Diagnose a tagged partial view whose full view is untagged. We post
17182 -- the message on the full view, with a reference to the previous
17183 -- partial view. The partial view can be private or incomplete, and
17184 -- these are handled in a different manner, so we determine the position
17185 -- of the error message from the respective slocs of both.
17187 -----------------------------
17188 -- Check_Duplicate_Aspects --
17189 -----------------------------
17191 procedure Check_Duplicate_Aspects is
17192 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17193 -- Return the corresponding aspect of the partial view which matches
17194 -- the aspect id of Asp. Return Empty is no such aspect exists.
17196 -----------------------------
17197 -- Get_Partial_View_Aspect --
17198 -----------------------------
17200 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17201 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17202 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17203 Prev_Asp : Node_Id;
17206 if Present (Prev_Asps) then
17207 Prev_Asp := First (Prev_Asps);
17208 while Present (Prev_Asp) loop
17209 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17218 end Get_Partial_View_Aspect;
17222 Full_Asps : constant List_Id := Aspect_Specifications (N);
17223 Full_Asp : Node_Id;
17224 Part_Asp : Node_Id;
17226 -- Start of processing for Check_Duplicate_Aspects
17229 if Present (Full_Asps) then
17230 Full_Asp := First (Full_Asps);
17231 while Present (Full_Asp) loop
17232 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17234 -- An aspect and its class-wide counterpart are two distinct
17235 -- aspects and may apply to both views of an entity.
17237 if Present (Part_Asp)
17238 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17241 ("aspect already specified in private declaration",
17248 if Has_Discriminants (Prev)
17249 and then not Has_Unknown_Discriminants (Prev)
17250 and then Get_Aspect_Id (Full_Asp) =
17251 Aspect_Implicit_Dereference
17254 ("cannot specify aspect if partial view has known "
17255 & "discriminants", Full_Asp);
17261 end Check_Duplicate_Aspects;
17267 procedure Tag_Mismatch is
17269 if Sloc (Prev) < Sloc (Id) then
17270 if Ada_Version >= Ada_2012
17271 and then Nkind (N) = N_Private_Type_Declaration
17274 ("declaration of private } must be a tagged type ", Id, Prev);
17277 ("full declaration of } must be a tagged type ", Id, Prev);
17281 if Ada_Version >= Ada_2012
17282 and then Nkind (N) = N_Private_Type_Declaration
17285 ("declaration of private } must be a tagged type ", Prev, Id);
17288 ("full declaration of } must be a tagged type ", Prev, Id);
17293 -- Start of processing for Find_Type_Name
17296 -- Find incomplete declaration, if one was given
17298 Prev := Current_Entity_In_Scope (Id);
17300 -- New type declaration
17306 -- Previous declaration exists
17309 Prev_Par := Parent (Prev);
17311 -- Error if not incomplete/private case except if previous
17312 -- declaration is implicit, etc. Enter_Name will emit error if
17315 if not Is_Incomplete_Or_Private_Type (Prev) then
17319 -- Check invalid completion of private or incomplete type
17321 elsif not Nkind_In (N, N_Full_Type_Declaration,
17322 N_Task_Type_Declaration,
17323 N_Protected_Type_Declaration)
17325 (Ada_Version < Ada_2012
17326 or else not Is_Incomplete_Type (Prev)
17327 or else not Nkind_In (N, N_Private_Type_Declaration,
17328 N_Private_Extension_Declaration))
17330 -- Completion must be a full type declarations (RM 7.3(4))
17332 Error_Msg_Sloc := Sloc (Prev);
17333 Error_Msg_NE ("invalid completion of }", Id, Prev);
17335 -- Set scope of Id to avoid cascaded errors. Entity is never
17336 -- examined again, except when saving globals in generics.
17338 Set_Scope (Id, Current_Scope);
17341 -- If this is a repeated incomplete declaration, no further
17342 -- checks are possible.
17344 if Nkind (N) = N_Incomplete_Type_Declaration then
17348 -- Case of full declaration of incomplete type
17350 elsif Ekind (Prev) = E_Incomplete_Type
17351 and then (Ada_Version < Ada_2012
17352 or else No (Full_View (Prev))
17353 or else not Is_Private_Type (Full_View (Prev)))
17355 -- Indicate that the incomplete declaration has a matching full
17356 -- declaration. The defining occurrence of the incomplete
17357 -- declaration remains the visible one, and the procedure
17358 -- Get_Full_View dereferences it whenever the type is used.
17360 if Present (Full_View (Prev)) then
17361 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17364 Set_Full_View (Prev, Id);
17365 Append_Entity (Id, Current_Scope);
17366 Set_Is_Public (Id, Is_Public (Prev));
17367 Set_Is_Internal (Id);
17370 -- If the incomplete view is tagged, a class_wide type has been
17371 -- created already. Use it for the private type as well, in order
17372 -- to prevent multiple incompatible class-wide types that may be
17373 -- created for self-referential anonymous access components.
17375 if Is_Tagged_Type (Prev)
17376 and then Present (Class_Wide_Type (Prev))
17378 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17379 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17381 -- Type of the class-wide type is the current Id. Previously
17382 -- this was not done for private declarations because of order-
17383 -- of-elaboration issues in the back end, but gigi now handles
17386 Set_Etype (Class_Wide_Type (Id), Id);
17389 -- Case of full declaration of private type
17392 -- If the private type was a completion of an incomplete type then
17393 -- update Prev to reference the private type
17395 if Ada_Version >= Ada_2012
17396 and then Ekind (Prev) = E_Incomplete_Type
17397 and then Present (Full_View (Prev))
17398 and then Is_Private_Type (Full_View (Prev))
17400 Prev := Full_View (Prev);
17401 Prev_Par := Parent (Prev);
17404 if Nkind (N) = N_Full_Type_Declaration
17406 (Type_Definition (N), N_Record_Definition,
17407 N_Derived_Type_Definition)
17408 and then Interface_Present (Type_Definition (N))
17411 ("completion of private type cannot be an interface", N);
17414 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17415 if Etype (Prev) /= Prev then
17417 -- Prev is a private subtype or a derived type, and needs
17420 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17423 elsif Ekind (Prev) = E_Private_Type
17424 and then Nkind_In (N, N_Task_Type_Declaration,
17425 N_Protected_Type_Declaration)
17428 ("completion of nonlimited type cannot be limited", N);
17430 elsif Ekind (Prev) = E_Record_Type_With_Private
17431 and then Nkind_In (N, N_Task_Type_Declaration,
17432 N_Protected_Type_Declaration)
17434 if not Is_Limited_Record (Prev) then
17436 ("completion of nonlimited type cannot be limited", N);
17438 elsif No (Interface_List (N)) then
17440 ("completion of tagged private type must be tagged",
17445 -- Ada 2005 (AI-251): Private extension declaration of a task
17446 -- type or a protected type. This case arises when covering
17447 -- interface types.
17449 elsif Nkind_In (N, N_Task_Type_Declaration,
17450 N_Protected_Type_Declaration)
17454 elsif Nkind (N) /= N_Full_Type_Declaration
17455 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17458 ("full view of private extension must be an extension", N);
17460 elsif not (Abstract_Present (Parent (Prev)))
17461 and then Abstract_Present (Type_Definition (N))
17464 ("full view of non-abstract extension cannot be abstract", N);
17467 if not In_Private_Part (Current_Scope) then
17469 ("declaration of full view must appear in private part", N);
17472 if Ada_Version >= Ada_2012 then
17473 Check_Duplicate_Aspects;
17476 Copy_And_Swap (Prev, Id);
17477 Set_Has_Private_Declaration (Prev);
17478 Set_Has_Private_Declaration (Id);
17480 -- AI12-0133: Indicate whether we have a partial view with
17481 -- unknown discriminants, in which case initialization of objects
17482 -- of the type do not receive an invariant check.
17484 Set_Partial_View_Has_Unknown_Discr
17485 (Prev, Has_Unknown_Discriminants (Id));
17487 -- Preserve aspect and iterator flags that may have been set on
17488 -- the partial view.
17490 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17491 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17493 -- If no error, propagate freeze_node from private to full view.
17494 -- It may have been generated for an early operational item.
17496 if Present (Freeze_Node (Id))
17497 and then Serious_Errors_Detected = 0
17498 and then No (Full_View (Id))
17500 Set_Freeze_Node (Prev, Freeze_Node (Id));
17501 Set_Freeze_Node (Id, Empty);
17502 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17505 Set_Full_View (Id, Prev);
17509 -- Verify that full declaration conforms to partial one
17511 if Is_Incomplete_Or_Private_Type (Prev)
17512 and then Present (Discriminant_Specifications (Prev_Par))
17514 if Present (Discriminant_Specifications (N)) then
17515 if Ekind (Prev) = E_Incomplete_Type then
17516 Check_Discriminant_Conformance (N, Prev, Prev);
17518 Check_Discriminant_Conformance (N, Prev, Id);
17523 ("missing discriminants in full type declaration", N);
17525 -- To avoid cascaded errors on subsequent use, share the
17526 -- discriminants of the partial view.
17528 Set_Discriminant_Specifications (N,
17529 Discriminant_Specifications (Prev_Par));
17533 -- A prior untagged partial view can have an associated class-wide
17534 -- type due to use of the class attribute, and in this case the full
17535 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17536 -- of incomplete tagged declarations, but we check for it.
17539 and then (Is_Tagged_Type (Prev)
17540 or else Present (Class_Wide_Type (Prev)))
17542 -- Ada 2012 (AI05-0162): A private type may be the completion of
17543 -- an incomplete type.
17545 if Ada_Version >= Ada_2012
17546 and then Is_Incomplete_Type (Prev)
17547 and then Nkind_In (N, N_Private_Type_Declaration,
17548 N_Private_Extension_Declaration)
17550 -- No need to check private extensions since they are tagged
17552 if Nkind (N) = N_Private_Type_Declaration
17553 and then not Tagged_Present (N)
17558 -- The full declaration is either a tagged type (including
17559 -- a synchronized type that implements interfaces) or a
17560 -- type extension, otherwise this is an error.
17562 elsif Nkind_In (N, N_Task_Type_Declaration,
17563 N_Protected_Type_Declaration)
17565 if No (Interface_List (N)) and then not Error_Posted (N) then
17569 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17571 -- Indicate that the previous declaration (tagged incomplete
17572 -- or private declaration) requires the same on the full one.
17574 if not Tagged_Present (Type_Definition (N)) then
17576 Set_Is_Tagged_Type (Id);
17579 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17580 if No (Record_Extension_Part (Type_Definition (N))) then
17582 ("full declaration of } must be a record extension",
17585 -- Set some attributes to produce a usable full view
17587 Set_Is_Tagged_Type (Id);
17596 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17597 and then Present (Premature_Use (Parent (Prev)))
17599 Error_Msg_Sloc := Sloc (N);
17601 ("\full declaration #", Premature_Use (Parent (Prev)));
17606 end Find_Type_Name;
17608 -------------------------
17609 -- Find_Type_Of_Object --
17610 -------------------------
17612 function Find_Type_Of_Object
17613 (Obj_Def : Node_Id;
17614 Related_Nod : Node_Id) return Entity_Id
17616 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17617 P : Node_Id := Parent (Obj_Def);
17622 -- If the parent is a component_definition node we climb to the
17623 -- component_declaration node
17625 if Nkind (P) = N_Component_Definition then
17629 -- Case of an anonymous array subtype
17631 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17632 N_Unconstrained_Array_Definition)
17635 Array_Type_Declaration (T, Obj_Def);
17637 -- Create an explicit subtype whenever possible
17639 elsif Nkind (P) /= N_Component_Declaration
17640 and then Def_Kind = N_Subtype_Indication
17642 -- Base name of subtype on object name, which will be unique in
17643 -- the current scope.
17645 -- If this is a duplicate declaration, return base type, to avoid
17646 -- generating duplicate anonymous types.
17648 if Error_Posted (P) then
17649 Analyze (Subtype_Mark (Obj_Def));
17650 return Entity (Subtype_Mark (Obj_Def));
17655 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17657 T := Make_Defining_Identifier (Sloc (P), Nam);
17659 Insert_Action (Obj_Def,
17660 Make_Subtype_Declaration (Sloc (P),
17661 Defining_Identifier => T,
17662 Subtype_Indication => Relocate_Node (Obj_Def)));
17664 -- This subtype may need freezing, and this will not be done
17665 -- automatically if the object declaration is not in declarative
17666 -- part. Since this is an object declaration, the type cannot always
17667 -- be frozen here. Deferred constants do not freeze their type
17668 -- (which often enough will be private).
17670 if Nkind (P) = N_Object_Declaration
17671 and then Constant_Present (P)
17672 and then No (Expression (P))
17676 -- Here we freeze the base type of object type to catch premature use
17677 -- of discriminated private type without a full view.
17680 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17683 -- Ada 2005 AI-406: the object definition in an object declaration
17684 -- can be an access definition.
17686 elsif Def_Kind = N_Access_Definition then
17687 T := Access_Definition (Related_Nod, Obj_Def);
17689 Set_Is_Local_Anonymous_Access
17691 V => (Ada_Version < Ada_2012)
17692 or else (Nkind (P) /= N_Object_Declaration)
17693 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17695 -- Otherwise, the object definition is just a subtype_mark
17698 T := Process_Subtype (Obj_Def, Related_Nod);
17700 -- If expansion is disabled an object definition that is an aggregate
17701 -- will not get expanded and may lead to scoping problems in the back
17702 -- end, if the object is referenced in an inner scope. In that case
17703 -- create an itype reference for the object definition now. This
17704 -- may be redundant in some cases, but harmless.
17707 and then Nkind (Related_Nod) = N_Object_Declaration
17710 Build_Itype_Reference (T, Related_Nod);
17715 end Find_Type_Of_Object;
17717 --------------------------------
17718 -- Find_Type_Of_Subtype_Indic --
17719 --------------------------------
17721 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17725 -- Case of subtype mark with a constraint
17727 if Nkind (S) = N_Subtype_Indication then
17728 Find_Type (Subtype_Mark (S));
17729 Typ := Entity (Subtype_Mark (S));
17732 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17735 ("incorrect constraint for this kind of type", Constraint (S));
17736 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17739 -- Otherwise we have a subtype mark without a constraint
17741 elsif Error_Posted (S) then
17742 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17750 -- Check No_Wide_Characters restriction
17752 Check_Wide_Character_Restriction (Typ, S);
17755 end Find_Type_Of_Subtype_Indic;
17757 -------------------------------------
17758 -- Floating_Point_Type_Declaration --
17759 -------------------------------------
17761 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17762 Digs : constant Node_Id := Digits_Expression (Def);
17763 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17765 Base_Typ : Entity_Id;
17766 Implicit_Base : Entity_Id;
17769 function Can_Derive_From (E : Entity_Id) return Boolean;
17770 -- Find if given digits value, and possibly a specified range, allows
17771 -- derivation from specified type
17773 function Find_Base_Type return Entity_Id;
17774 -- Find a predefined base type that Def can derive from, or generate
17775 -- an error and substitute Long_Long_Float if none exists.
17777 ---------------------
17778 -- Can_Derive_From --
17779 ---------------------
17781 function Can_Derive_From (E : Entity_Id) return Boolean is
17782 Spec : constant Entity_Id := Real_Range_Specification (Def);
17785 -- Check specified "digits" constraint
17787 if Digs_Val > Digits_Value (E) then
17791 -- Check for matching range, if specified
17793 if Present (Spec) then
17794 if Expr_Value_R (Type_Low_Bound (E)) >
17795 Expr_Value_R (Low_Bound (Spec))
17800 if Expr_Value_R (Type_High_Bound (E)) <
17801 Expr_Value_R (High_Bound (Spec))
17808 end Can_Derive_From;
17810 --------------------
17811 -- Find_Base_Type --
17812 --------------------
17814 function Find_Base_Type return Entity_Id is
17815 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17818 -- Iterate over the predefined types in order, returning the first
17819 -- one that Def can derive from.
17821 while Present (Choice) loop
17822 if Can_Derive_From (Node (Choice)) then
17823 return Node (Choice);
17826 Next_Elmt (Choice);
17829 -- If we can't derive from any existing type, use Long_Long_Float
17830 -- and give appropriate message explaining the problem.
17832 if Digs_Val > Max_Digs_Val then
17833 -- It might be the case that there is a type with the requested
17834 -- range, just not the combination of digits and range.
17837 ("no predefined type has requested range and precision",
17838 Real_Range_Specification (Def));
17842 ("range too large for any predefined type",
17843 Real_Range_Specification (Def));
17846 return Standard_Long_Long_Float;
17847 end Find_Base_Type;
17849 -- Start of processing for Floating_Point_Type_Declaration
17852 Check_Restriction (No_Floating_Point, Def);
17854 -- Create an implicit base type
17857 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17859 -- Analyze and verify digits value
17861 Analyze_And_Resolve (Digs, Any_Integer);
17862 Check_Digits_Expression (Digs);
17863 Digs_Val := Expr_Value (Digs);
17865 -- Process possible range spec and find correct type to derive from
17867 Process_Real_Range_Specification (Def);
17869 -- Check that requested number of digits is not too high.
17871 if Digs_Val > Max_Digs_Val then
17873 -- The check for Max_Base_Digits may be somewhat expensive, as it
17874 -- requires reading System, so only do it when necessary.
17877 Max_Base_Digits : constant Uint :=
17880 (Parent (RTE (RE_Max_Base_Digits))));
17883 if Digs_Val > Max_Base_Digits then
17884 Error_Msg_Uint_1 := Max_Base_Digits;
17885 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17887 elsif No (Real_Range_Specification (Def)) then
17888 Error_Msg_Uint_1 := Max_Digs_Val;
17889 Error_Msg_N ("types with more than ^ digits need range spec "
17890 & "(RM 3.5.7(6))", Digs);
17895 -- Find a suitable type to derive from or complain and use a substitute
17897 Base_Typ := Find_Base_Type;
17899 -- If there are bounds given in the declaration use them as the bounds
17900 -- of the type, otherwise use the bounds of the predefined base type
17901 -- that was chosen based on the Digits value.
17903 if Present (Real_Range_Specification (Def)) then
17904 Set_Scalar_Range (T, Real_Range_Specification (Def));
17905 Set_Is_Constrained (T);
17907 -- The bounds of this range must be converted to machine numbers
17908 -- in accordance with RM 4.9(38).
17910 Bound := Type_Low_Bound (T);
17912 if Nkind (Bound) = N_Real_Literal then
17914 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17915 Set_Is_Machine_Number (Bound);
17918 Bound := Type_High_Bound (T);
17920 if Nkind (Bound) = N_Real_Literal then
17922 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17923 Set_Is_Machine_Number (Bound);
17927 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17930 -- Complete definition of implicit base and declared first subtype. The
17931 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17932 -- are not clobbered when the floating point type acts as a full view of
17935 Set_Etype (Implicit_Base, Base_Typ);
17936 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17937 Set_Size_Info (Implicit_Base, Base_Typ);
17938 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17939 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17940 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17941 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17943 Set_Ekind (T, E_Floating_Point_Subtype);
17944 Set_Etype (T, Implicit_Base);
17945 Set_Size_Info (T, Implicit_Base);
17946 Set_RM_Size (T, RM_Size (Implicit_Base));
17947 Inherit_Rep_Item_Chain (T, Implicit_Base);
17948 Set_Digits_Value (T, Digs_Val);
17949 end Floating_Point_Type_Declaration;
17951 ----------------------------
17952 -- Get_Discriminant_Value --
17953 ----------------------------
17955 -- This is the situation:
17957 -- There is a non-derived type
17959 -- type T0 (Dx, Dy, Dz...)
17961 -- There are zero or more levels of derivation, with each derivation
17962 -- either purely inheriting the discriminants, or defining its own.
17964 -- type Ti is new Ti-1
17966 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17968 -- subtype Ti is ...
17970 -- The subtype issue is avoided by the use of Original_Record_Component,
17971 -- and the fact that derived subtypes also derive the constraints.
17973 -- This chain leads back from
17975 -- Typ_For_Constraint
17977 -- Typ_For_Constraint has discriminants, and the value for each
17978 -- discriminant is given by its corresponding Elmt of Constraints.
17980 -- Discriminant is some discriminant in this hierarchy
17982 -- We need to return its value
17984 -- We do this by recursively searching each level, and looking for
17985 -- Discriminant. Once we get to the bottom, we start backing up
17986 -- returning the value for it which may in turn be a discriminant
17987 -- further up, so on the backup we continue the substitution.
17989 function Get_Discriminant_Value
17990 (Discriminant : Entity_Id;
17991 Typ_For_Constraint : Entity_Id;
17992 Constraint : Elist_Id) return Node_Id
17994 function Root_Corresponding_Discriminant
17995 (Discr : Entity_Id) return Entity_Id;
17996 -- Given a discriminant, traverse the chain of inherited discriminants
17997 -- and return the topmost discriminant.
17999 function Search_Derivation_Levels
18001 Discrim_Values : Elist_Id;
18002 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18003 -- This is the routine that performs the recursive search of levels
18004 -- as described above.
18006 -------------------------------------
18007 -- Root_Corresponding_Discriminant --
18008 -------------------------------------
18010 function Root_Corresponding_Discriminant
18011 (Discr : Entity_Id) return Entity_Id
18017 while Present (Corresponding_Discriminant (D)) loop
18018 D := Corresponding_Discriminant (D);
18022 end Root_Corresponding_Discriminant;
18024 ------------------------------
18025 -- Search_Derivation_Levels --
18026 ------------------------------
18028 function Search_Derivation_Levels
18030 Discrim_Values : Elist_Id;
18031 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18035 Result : Node_Or_Entity_Id;
18036 Result_Entity : Node_Id;
18039 -- If inappropriate type, return Error, this happens only in
18040 -- cascaded error situations, and we want to avoid a blow up.
18042 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18046 -- Look deeper if possible. Use Stored_Constraints only for
18047 -- untagged types. For tagged types use the given constraint.
18048 -- This asymmetry needs explanation???
18050 if not Stored_Discrim_Values
18051 and then Present (Stored_Constraint (Ti))
18052 and then not Is_Tagged_Type (Ti)
18055 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18059 Td : Entity_Id := Etype (Ti);
18062 -- If the parent type is private, the full view may include
18063 -- renamed discriminants, and it is those stored values that
18064 -- may be needed (the partial view never has more information
18065 -- than the full view).
18067 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18068 Td := Full_View (Td);
18072 Result := Discriminant;
18075 if Present (Stored_Constraint (Ti)) then
18077 Search_Derivation_Levels
18078 (Td, Stored_Constraint (Ti), True);
18081 Search_Derivation_Levels
18082 (Td, Discrim_Values, Stored_Discrim_Values);
18088 -- Extra underlying places to search, if not found above. For
18089 -- concurrent types, the relevant discriminant appears in the
18090 -- corresponding record. For a type derived from a private type
18091 -- without discriminant, the full view inherits the discriminants
18092 -- of the full view of the parent.
18094 if Result = Discriminant then
18095 if Is_Concurrent_Type (Ti)
18096 and then Present (Corresponding_Record_Type (Ti))
18099 Search_Derivation_Levels (
18100 Corresponding_Record_Type (Ti),
18102 Stored_Discrim_Values);
18104 elsif Is_Private_Type (Ti)
18105 and then not Has_Discriminants (Ti)
18106 and then Present (Full_View (Ti))
18107 and then Etype (Full_View (Ti)) /= Ti
18110 Search_Derivation_Levels (
18113 Stored_Discrim_Values);
18117 -- If Result is not a (reference to a) discriminant, return it,
18118 -- otherwise set Result_Entity to the discriminant.
18120 if Nkind (Result) = N_Defining_Identifier then
18121 pragma Assert (Result = Discriminant);
18122 Result_Entity := Result;
18125 if not Denotes_Discriminant (Result) then
18129 Result_Entity := Entity (Result);
18132 -- See if this level of derivation actually has discriminants because
18133 -- tagged derivations can add them, hence the lower levels need not
18136 if not Has_Discriminants (Ti) then
18140 -- Scan Ti's discriminants for Result_Entity, and return its
18141 -- corresponding value, if any.
18143 Result_Entity := Original_Record_Component (Result_Entity);
18145 Assoc := First_Elmt (Discrim_Values);
18147 if Stored_Discrim_Values then
18148 Disc := First_Stored_Discriminant (Ti);
18150 Disc := First_Discriminant (Ti);
18153 while Present (Disc) loop
18155 -- If no further associations return the discriminant, value will
18156 -- be found on the second pass.
18162 if Original_Record_Component (Disc) = Result_Entity then
18163 return Node (Assoc);
18168 if Stored_Discrim_Values then
18169 Next_Stored_Discriminant (Disc);
18171 Next_Discriminant (Disc);
18175 -- Could not find it
18178 end Search_Derivation_Levels;
18182 Result : Node_Or_Entity_Id;
18184 -- Start of processing for Get_Discriminant_Value
18187 -- ??? This routine is a gigantic mess and will be deleted. For the
18188 -- time being just test for the trivial case before calling recurse.
18190 -- We are now celebrating the 20th anniversary of this comment!
18192 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18198 D := First_Discriminant (Typ_For_Constraint);
18199 E := First_Elmt (Constraint);
18200 while Present (D) loop
18201 if Chars (D) = Chars (Discriminant) then
18205 Next_Discriminant (D);
18211 Result := Search_Derivation_Levels
18212 (Typ_For_Constraint, Constraint, False);
18214 -- ??? hack to disappear when this routine is gone
18216 if Nkind (Result) = N_Defining_Identifier then
18222 D := First_Discriminant (Typ_For_Constraint);
18223 E := First_Elmt (Constraint);
18224 while Present (D) loop
18225 if Root_Corresponding_Discriminant (D) = Discriminant then
18229 Next_Discriminant (D);
18235 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18237 end Get_Discriminant_Value;
18239 --------------------------
18240 -- Has_Range_Constraint --
18241 --------------------------
18243 function Has_Range_Constraint (N : Node_Id) return Boolean is
18244 C : constant Node_Id := Constraint (N);
18247 if Nkind (C) = N_Range_Constraint then
18250 elsif Nkind (C) = N_Digits_Constraint then
18252 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18253 or else Present (Range_Constraint (C));
18255 elsif Nkind (C) = N_Delta_Constraint then
18256 return Present (Range_Constraint (C));
18261 end Has_Range_Constraint;
18263 ------------------------
18264 -- Inherit_Components --
18265 ------------------------
18267 function Inherit_Components
18269 Parent_Base : Entity_Id;
18270 Derived_Base : Entity_Id;
18271 Is_Tagged : Boolean;
18272 Inherit_Discr : Boolean;
18273 Discs : Elist_Id) return Elist_Id
18275 Assoc_List : constant Elist_Id := New_Elmt_List;
18277 procedure Inherit_Component
18278 (Old_C : Entity_Id;
18279 Plain_Discrim : Boolean := False;
18280 Stored_Discrim : Boolean := False);
18281 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18282 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18283 -- True, Old_C is a stored discriminant. If they are both false then
18284 -- Old_C is a regular component.
18286 -----------------------
18287 -- Inherit_Component --
18288 -----------------------
18290 procedure Inherit_Component
18291 (Old_C : Entity_Id;
18292 Plain_Discrim : Boolean := False;
18293 Stored_Discrim : Boolean := False)
18295 procedure Set_Anonymous_Type (Id : Entity_Id);
18296 -- Id denotes the entity of an access discriminant or anonymous
18297 -- access component. Set the type of Id to either the same type of
18298 -- Old_C or create a new one depending on whether the parent and
18299 -- the child types are in the same scope.
18301 ------------------------
18302 -- Set_Anonymous_Type --
18303 ------------------------
18305 procedure Set_Anonymous_Type (Id : Entity_Id) is
18306 Old_Typ : constant Entity_Id := Etype (Old_C);
18309 if Scope (Parent_Base) = Scope (Derived_Base) then
18310 Set_Etype (Id, Old_Typ);
18312 -- The parent and the derived type are in two different scopes.
18313 -- Reuse the type of the original discriminant / component by
18314 -- copying it in order to preserve all attributes.
18318 Typ : constant Entity_Id := New_Copy (Old_Typ);
18321 Set_Etype (Id, Typ);
18323 -- Since we do not generate component declarations for
18324 -- inherited components, associate the itype with the
18327 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18328 Set_Scope (Typ, Derived_Base);
18331 end Set_Anonymous_Type;
18333 -- Local variables and constants
18335 New_C : constant Entity_Id := New_Copy (Old_C);
18337 Corr_Discrim : Entity_Id;
18338 Discrim : Entity_Id;
18340 -- Start of processing for Inherit_Component
18343 pragma Assert (not Is_Tagged or not Stored_Discrim);
18345 Set_Parent (New_C, Parent (Old_C));
18347 -- Regular discriminants and components must be inserted in the scope
18348 -- of the Derived_Base. Do it here.
18350 if not Stored_Discrim then
18351 Enter_Name (New_C);
18354 -- For tagged types the Original_Record_Component must point to
18355 -- whatever this field was pointing to in the parent type. This has
18356 -- already been achieved by the call to New_Copy above.
18358 if not Is_Tagged then
18359 Set_Original_Record_Component (New_C, New_C);
18360 Set_Corresponding_Record_Component (New_C, Old_C);
18363 -- Set the proper type of an access discriminant
18365 if Ekind (New_C) = E_Discriminant
18366 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18368 Set_Anonymous_Type (New_C);
18371 -- If we have inherited a component then see if its Etype contains
18372 -- references to Parent_Base discriminants. In this case, replace
18373 -- these references with the constraints given in Discs. We do not
18374 -- do this for the partial view of private types because this is
18375 -- not needed (only the components of the full view will be used
18376 -- for code generation) and cause problem. We also avoid this
18377 -- transformation in some error situations.
18379 if Ekind (New_C) = E_Component then
18381 -- Set the proper type of an anonymous access component
18383 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18384 Set_Anonymous_Type (New_C);
18386 elsif (Is_Private_Type (Derived_Base)
18387 and then not Is_Generic_Type (Derived_Base))
18388 or else (Is_Empty_Elmt_List (Discs)
18389 and then not Expander_Active)
18391 Set_Etype (New_C, Etype (Old_C));
18394 -- The current component introduces a circularity of the
18397 -- limited with Pack_2;
18398 -- package Pack_1 is
18399 -- type T_1 is tagged record
18400 -- Comp : access Pack_2.T_2;
18406 -- package Pack_2 is
18407 -- type T_2 is new Pack_1.T_1 with ...;
18412 Constrain_Component_Type
18413 (Old_C, Derived_Base, N, Parent_Base, Discs));
18417 -- In derived tagged types it is illegal to reference a non
18418 -- discriminant component in the parent type. To catch this, mark
18419 -- these components with an Ekind of E_Void. This will be reset in
18420 -- Record_Type_Definition after processing the record extension of
18421 -- the derived type.
18423 -- If the declaration is a private extension, there is no further
18424 -- record extension to process, and the components retain their
18425 -- current kind, because they are visible at this point.
18427 if Is_Tagged and then Ekind (New_C) = E_Component
18428 and then Nkind (N) /= N_Private_Extension_Declaration
18430 Set_Ekind (New_C, E_Void);
18433 if Plain_Discrim then
18434 Set_Corresponding_Discriminant (New_C, Old_C);
18435 Build_Discriminal (New_C);
18437 -- If we are explicitly inheriting a stored discriminant it will be
18438 -- completely hidden.
18440 elsif Stored_Discrim then
18441 Set_Corresponding_Discriminant (New_C, Empty);
18442 Set_Discriminal (New_C, Empty);
18443 Set_Is_Completely_Hidden (New_C);
18445 -- Set the Original_Record_Component of each discriminant in the
18446 -- derived base to point to the corresponding stored that we just
18449 Discrim := First_Discriminant (Derived_Base);
18450 while Present (Discrim) loop
18451 Corr_Discrim := Corresponding_Discriminant (Discrim);
18453 -- Corr_Discrim could be missing in an error situation
18455 if Present (Corr_Discrim)
18456 and then Original_Record_Component (Corr_Discrim) = Old_C
18458 Set_Original_Record_Component (Discrim, New_C);
18459 Set_Corresponding_Record_Component (Discrim, Empty);
18462 Next_Discriminant (Discrim);
18465 Append_Entity (New_C, Derived_Base);
18468 if not Is_Tagged then
18469 Append_Elmt (Old_C, Assoc_List);
18470 Append_Elmt (New_C, Assoc_List);
18472 end Inherit_Component;
18474 -- Variables local to Inherit_Component
18476 Loc : constant Source_Ptr := Sloc (N);
18478 Parent_Discrim : Entity_Id;
18479 Stored_Discrim : Entity_Id;
18481 Component : Entity_Id;
18483 -- Start of processing for Inherit_Components
18486 if not Is_Tagged then
18487 Append_Elmt (Parent_Base, Assoc_List);
18488 Append_Elmt (Derived_Base, Assoc_List);
18491 -- Inherit parent discriminants if needed
18493 if Inherit_Discr then
18494 Parent_Discrim := First_Discriminant (Parent_Base);
18495 while Present (Parent_Discrim) loop
18496 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18497 Next_Discriminant (Parent_Discrim);
18501 -- Create explicit stored discrims for untagged types when necessary
18503 if not Has_Unknown_Discriminants (Derived_Base)
18504 and then Has_Discriminants (Parent_Base)
18505 and then not Is_Tagged
18508 or else First_Discriminant (Parent_Base) /=
18509 First_Stored_Discriminant (Parent_Base))
18511 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18512 while Present (Stored_Discrim) loop
18513 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18514 Next_Stored_Discriminant (Stored_Discrim);
18518 -- See if we can apply the second transformation for derived types, as
18519 -- explained in point 6. in the comments above Build_Derived_Record_Type
18520 -- This is achieved by appending Derived_Base discriminants into Discs,
18521 -- which has the side effect of returning a non empty Discs list to the
18522 -- caller of Inherit_Components, which is what we want. This must be
18523 -- done for private derived types if there are explicit stored
18524 -- discriminants, to ensure that we can retrieve the values of the
18525 -- constraints provided in the ancestors.
18528 and then Is_Empty_Elmt_List (Discs)
18529 and then Present (First_Discriminant (Derived_Base))
18531 (not Is_Private_Type (Derived_Base)
18532 or else Is_Completely_Hidden
18533 (First_Stored_Discriminant (Derived_Base))
18534 or else Is_Generic_Type (Derived_Base))
18536 D := First_Discriminant (Derived_Base);
18537 while Present (D) loop
18538 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18539 Next_Discriminant (D);
18543 -- Finally, inherit non-discriminant components unless they are not
18544 -- visible because defined or inherited from the full view of the
18545 -- parent. Don't inherit the _parent field of the parent type.
18547 Component := First_Entity (Parent_Base);
18548 while Present (Component) loop
18550 -- Ada 2005 (AI-251): Do not inherit components associated with
18551 -- secondary tags of the parent.
18553 if Ekind (Component) = E_Component
18554 and then Present (Related_Type (Component))
18558 elsif Ekind (Component) /= E_Component
18559 or else Chars (Component) = Name_uParent
18563 -- If the derived type is within the parent type's declarative
18564 -- region, then the components can still be inherited even though
18565 -- they aren't visible at this point. This can occur for cases
18566 -- such as within public child units where the components must
18567 -- become visible upon entering the child unit's private part.
18569 elsif not Is_Visible_Component (Component)
18570 and then not In_Open_Scopes (Scope (Parent_Base))
18574 elsif Ekind_In (Derived_Base, E_Private_Type,
18575 E_Limited_Private_Type)
18580 Inherit_Component (Component);
18583 Next_Entity (Component);
18586 -- For tagged derived types, inherited discriminants cannot be used in
18587 -- component declarations of the record extension part. To achieve this
18588 -- we mark the inherited discriminants as not visible.
18590 if Is_Tagged and then Inherit_Discr then
18591 D := First_Discriminant (Derived_Base);
18592 while Present (D) loop
18593 Set_Is_Immediately_Visible (D, False);
18594 Next_Discriminant (D);
18599 end Inherit_Components;
18601 -----------------------------
18602 -- Inherit_Predicate_Flags --
18603 -----------------------------
18605 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18607 if Present (Predicate_Function (Subt)) then
18611 Set_Has_Predicates (Subt, Has_Predicates (Par));
18612 Set_Has_Static_Predicate_Aspect
18613 (Subt, Has_Static_Predicate_Aspect (Par));
18614 Set_Has_Dynamic_Predicate_Aspect
18615 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18617 -- A named subtype does not inherit the predicate function of its
18618 -- parent but an itype declared for a loop index needs the discrete
18619 -- predicate information of its parent to execute the loop properly.
18621 if Is_Itype (Subt) and then Present (Predicate_Function (Par)) then
18622 Set_Subprograms_For_Type (Subt, Subprograms_For_Type (Par));
18624 if Has_Static_Predicate (Par) then
18625 Set_Static_Discrete_Predicate
18626 (Subt, Static_Discrete_Predicate (Par));
18629 end Inherit_Predicate_Flags;
18631 ----------------------
18632 -- Is_EVF_Procedure --
18633 ----------------------
18635 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18636 Formal : Entity_Id;
18639 -- Examine the formals of an Extensions_Visible False procedure looking
18640 -- for a controlling OUT parameter.
18642 if Ekind (Subp) = E_Procedure
18643 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18645 Formal := First_Formal (Subp);
18646 while Present (Formal) loop
18647 if Ekind (Formal) = E_Out_Parameter
18648 and then Is_Controlling_Formal (Formal)
18653 Next_Formal (Formal);
18658 end Is_EVF_Procedure;
18660 -----------------------
18661 -- Is_Null_Extension --
18662 -----------------------
18664 function Is_Null_Extension (T : Entity_Id) return Boolean is
18665 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18666 Comp_List : Node_Id;
18670 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18671 or else not Is_Tagged_Type (T)
18672 or else Nkind (Type_Definition (Type_Decl)) /=
18673 N_Derived_Type_Definition
18674 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18680 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18682 if Present (Discriminant_Specifications (Type_Decl)) then
18685 elsif Present (Comp_List)
18686 and then Is_Non_Empty_List (Component_Items (Comp_List))
18688 Comp := First (Component_Items (Comp_List));
18690 -- Only user-defined components are relevant. The component list
18691 -- may also contain a parent component and internal components
18692 -- corresponding to secondary tags, but these do not determine
18693 -- whether this is a null extension.
18695 while Present (Comp) loop
18696 if Comes_From_Source (Comp) then
18708 end Is_Null_Extension;
18710 ------------------------------
18711 -- Is_Valid_Constraint_Kind --
18712 ------------------------------
18714 function Is_Valid_Constraint_Kind
18715 (T_Kind : Type_Kind;
18716 Constraint_Kind : Node_Kind) return Boolean
18720 when Enumeration_Kind
18723 return Constraint_Kind = N_Range_Constraint;
18725 when Decimal_Fixed_Point_Kind =>
18726 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18727 N_Range_Constraint);
18729 when Ordinary_Fixed_Point_Kind =>
18730 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18731 N_Range_Constraint);
18734 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18735 N_Range_Constraint);
18742 | E_Incomplete_Type
18746 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18749 return True; -- Error will be detected later
18751 end Is_Valid_Constraint_Kind;
18753 --------------------------
18754 -- Is_Visible_Component --
18755 --------------------------
18757 function Is_Visible_Component
18759 N : Node_Id := Empty) return Boolean
18761 Original_Comp : Entity_Id := Empty;
18762 Original_Type : Entity_Id;
18763 Type_Scope : Entity_Id;
18765 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18766 -- Check whether parent type of inherited component is declared locally,
18767 -- possibly within a nested package or instance. The current scope is
18768 -- the derived record itself.
18770 -------------------
18771 -- Is_Local_Type --
18772 -------------------
18774 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18778 Scop := Scope (Typ);
18779 while Present (Scop)
18780 and then Scop /= Standard_Standard
18782 if Scop = Scope (Current_Scope) then
18786 Scop := Scope (Scop);
18792 -- Start of processing for Is_Visible_Component
18795 if Ekind_In (C, E_Component, E_Discriminant) then
18796 Original_Comp := Original_Record_Component (C);
18799 if No (Original_Comp) then
18801 -- Premature usage, or previous error
18806 Original_Type := Scope (Original_Comp);
18807 Type_Scope := Scope (Base_Type (Scope (C)));
18810 -- This test only concerns tagged types
18812 if not Is_Tagged_Type (Original_Type) then
18814 -- Check if this is a renamed discriminant (hidden either by the
18815 -- derived type or by some ancestor), unless we are analyzing code
18816 -- generated by the expander since it may reference such components
18817 -- (for example see the expansion of Deep_Adjust).
18819 if Ekind (C) = E_Discriminant and then Present (N) then
18821 not Comes_From_Source (N)
18822 or else not Is_Completely_Hidden (C);
18827 -- If it is _Parent or _Tag, there is no visibility issue
18829 elsif not Comes_From_Source (Original_Comp) then
18832 -- Discriminants are visible unless the (private) type has unknown
18833 -- discriminants. If the discriminant reference is inserted for a
18834 -- discriminant check on a full view it is also visible.
18836 elsif Ekind (Original_Comp) = E_Discriminant
18838 (not Has_Unknown_Discriminants (Original_Type)
18839 or else (Present (N)
18840 and then Nkind (N) = N_Selected_Component
18841 and then Nkind (Prefix (N)) = N_Type_Conversion
18842 and then not Comes_From_Source (Prefix (N))))
18846 -- In the body of an instantiation, check the visibility of a component
18847 -- in case it has a homograph that is a primitive operation of a private
18848 -- type which was not visible in the generic unit.
18850 -- Should Is_Prefixed_Call be propagated from template to instance???
18852 elsif In_Instance_Body then
18853 if not Is_Tagged_Type (Original_Type)
18854 or else not Is_Private_Type (Original_Type)
18860 Subp_Elmt : Elmt_Id;
18863 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18864 while Present (Subp_Elmt) loop
18866 -- The component is hidden by a primitive operation
18868 if Chars (Node (Subp_Elmt)) = Chars (C) then
18872 Next_Elmt (Subp_Elmt);
18879 -- If the component has been declared in an ancestor which is currently
18880 -- a private type, then it is not visible. The same applies if the
18881 -- component's containing type is not in an open scope and the original
18882 -- component's enclosing type is a visible full view of a private type
18883 -- (which can occur in cases where an attempt is being made to reference
18884 -- a component in a sibling package that is inherited from a visible
18885 -- component of a type in an ancestor package; the component in the
18886 -- sibling package should not be visible even though the component it
18887 -- inherited from is visible). This does not apply however in the case
18888 -- where the scope of the type is a private child unit, or when the
18889 -- parent comes from a local package in which the ancestor is currently
18890 -- visible. The latter suppression of visibility is needed for cases
18891 -- that are tested in B730006.
18893 elsif Is_Private_Type (Original_Type)
18895 (not Is_Private_Descendant (Type_Scope)
18896 and then not In_Open_Scopes (Type_Scope)
18897 and then Has_Private_Declaration (Original_Type))
18899 -- If the type derives from an entity in a formal package, there
18900 -- are no additional visible components.
18902 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18903 N_Formal_Package_Declaration
18907 -- if we are not in the private part of the current package, there
18908 -- are no additional visible components.
18910 elsif Ekind (Scope (Current_Scope)) = E_Package
18911 and then not In_Private_Part (Scope (Current_Scope))
18916 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18917 and then In_Open_Scopes (Scope (Original_Type))
18918 and then Is_Local_Type (Type_Scope);
18921 -- There is another weird way in which a component may be invisible when
18922 -- the private and the full view are not derived from the same ancestor.
18923 -- Here is an example :
18925 -- type A1 is tagged record F1 : integer; end record;
18926 -- type A2 is new A1 with record F2 : integer; end record;
18927 -- type T is new A1 with private;
18929 -- type T is new A2 with null record;
18931 -- In this case, the full view of T inherits F1 and F2 but the private
18932 -- view inherits only F1
18936 Ancestor : Entity_Id := Scope (C);
18940 if Ancestor = Original_Type then
18943 -- The ancestor may have a partial view of the original type,
18944 -- but if the full view is in scope, as in a child body, the
18945 -- component is visible.
18947 elsif In_Private_Part (Scope (Original_Type))
18948 and then Full_View (Ancestor) = Original_Type
18952 elsif Ancestor = Etype (Ancestor) then
18954 -- No further ancestors to examine
18959 Ancestor := Etype (Ancestor);
18963 end Is_Visible_Component;
18965 --------------------------
18966 -- Make_Class_Wide_Type --
18967 --------------------------
18969 procedure Make_Class_Wide_Type (T : Entity_Id) is
18970 CW_Type : Entity_Id;
18972 Next_E : Entity_Id;
18973 Prev_E : Entity_Id;
18976 if Present (Class_Wide_Type (T)) then
18978 -- The class-wide type is a partially decorated entity created for a
18979 -- unanalyzed tagged type referenced through a limited with clause.
18980 -- When the tagged type is analyzed, its class-wide type needs to be
18981 -- redecorated. Note that we reuse the entity created by Decorate_
18982 -- Tagged_Type in order to preserve all links.
18984 if Materialize_Entity (Class_Wide_Type (T)) then
18985 CW_Type := Class_Wide_Type (T);
18986 Set_Materialize_Entity (CW_Type, False);
18988 -- The class wide type can have been defined by the partial view, in
18989 -- which case everything is already done.
18995 -- Default case, we need to create a new class-wide type
18999 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19002 -- Inherit root type characteristics
19004 CW_Name := Chars (CW_Type);
19005 Next_E := Next_Entity (CW_Type);
19006 Prev_E := Prev_Entity (CW_Type);
19007 Copy_Node (T, CW_Type);
19008 Set_Comes_From_Source (CW_Type, False);
19009 Set_Chars (CW_Type, CW_Name);
19010 Set_Parent (CW_Type, Parent (T));
19011 Set_Prev_Entity (CW_Type, Prev_E);
19012 Set_Next_Entity (CW_Type, Next_E);
19014 -- Ensure we have a new freeze node for the class-wide type. The partial
19015 -- view may have freeze action of its own, requiring a proper freeze
19016 -- node, and the same freeze node cannot be shared between the two
19019 Set_Has_Delayed_Freeze (CW_Type);
19020 Set_Freeze_Node (CW_Type, Empty);
19022 -- Customize the class-wide type: It has no prim. op., it cannot be
19023 -- abstract, its Etype points back to the specific root type, and it
19024 -- cannot have any invariants.
19026 Set_Ekind (CW_Type, E_Class_Wide_Type);
19027 Set_Is_Tagged_Type (CW_Type, True);
19028 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19029 Set_Is_Abstract_Type (CW_Type, False);
19030 Set_Is_Constrained (CW_Type, False);
19031 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19032 Set_Default_SSO (CW_Type);
19033 Set_Has_Inheritable_Invariants (CW_Type, False);
19034 Set_Has_Inherited_Invariants (CW_Type, False);
19035 Set_Has_Own_Invariants (CW_Type, False);
19037 if Ekind (T) = E_Class_Wide_Subtype then
19038 Set_Etype (CW_Type, Etype (Base_Type (T)));
19040 Set_Etype (CW_Type, T);
19043 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19045 -- If this is the class_wide type of a constrained subtype, it does
19046 -- not have discriminants.
19048 Set_Has_Discriminants (CW_Type,
19049 Has_Discriminants (T) and then not Is_Constrained (T));
19051 Set_Has_Unknown_Discriminants (CW_Type, True);
19052 Set_Class_Wide_Type (T, CW_Type);
19053 Set_Equivalent_Type (CW_Type, Empty);
19055 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19057 Set_Class_Wide_Type (CW_Type, CW_Type);
19058 end Make_Class_Wide_Type;
19064 procedure Make_Index
19066 Related_Nod : Node_Id;
19067 Related_Id : Entity_Id := Empty;
19068 Suffix_Index : Nat := 1;
19069 In_Iter_Schm : Boolean := False)
19073 Def_Id : Entity_Id := Empty;
19074 Found : Boolean := False;
19077 -- For a discrete range used in a constrained array definition and
19078 -- defined by a range, an implicit conversion to the predefined type
19079 -- INTEGER is assumed if each bound is either a numeric literal, a named
19080 -- number, or an attribute, and the type of both bounds (prior to the
19081 -- implicit conversion) is the type universal_integer. Otherwise, both
19082 -- bounds must be of the same discrete type, other than universal
19083 -- integer; this type must be determinable independently of the
19084 -- context, but using the fact that the type must be discrete and that
19085 -- both bounds must have the same type.
19087 -- Character literals also have a universal type in the absence of
19088 -- of additional context, and are resolved to Standard_Character.
19090 if Nkind (N) = N_Range then
19092 -- The index is given by a range constraint. The bounds are known
19093 -- to be of a consistent type.
19095 if not Is_Overloaded (N) then
19098 -- For universal bounds, choose the specific predefined type
19100 if T = Universal_Integer then
19101 T := Standard_Integer;
19103 elsif T = Any_Character then
19104 Ambiguous_Character (Low_Bound (N));
19106 T := Standard_Character;
19109 -- The node may be overloaded because some user-defined operators
19110 -- are available, but if a universal interpretation exists it is
19111 -- also the selected one.
19113 elsif Universal_Interpretation (N) = Universal_Integer then
19114 T := Standard_Integer;
19120 Ind : Interp_Index;
19124 Get_First_Interp (N, Ind, It);
19125 while Present (It.Typ) loop
19126 if Is_Discrete_Type (It.Typ) then
19129 and then not Covers (It.Typ, T)
19130 and then not Covers (T, It.Typ)
19132 Error_Msg_N ("ambiguous bounds in discrete range", N);
19140 Get_Next_Interp (Ind, It);
19143 if T = Any_Type then
19144 Error_Msg_N ("discrete type required for range", N);
19145 Set_Etype (N, Any_Type);
19148 elsif T = Universal_Integer then
19149 T := Standard_Integer;
19154 if not Is_Discrete_Type (T) then
19155 Error_Msg_N ("discrete type required for range", N);
19156 Set_Etype (N, Any_Type);
19160 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19161 and then Attribute_Name (Low_Bound (N)) = Name_First
19162 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19163 and then Is_Type (Entity (Prefix (Low_Bound (N))))
19164 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19166 -- The type of the index will be the type of the prefix, as long
19167 -- as the upper bound is 'Last of the same type.
19169 Def_Id := Entity (Prefix (Low_Bound (N)));
19171 if Nkind (High_Bound (N)) /= N_Attribute_Reference
19172 or else Attribute_Name (High_Bound (N)) /= Name_Last
19173 or else not Is_Entity_Name (Prefix (High_Bound (N)))
19174 or else Entity (Prefix (High_Bound (N))) /= Def_Id
19181 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
19183 elsif Nkind (N) = N_Subtype_Indication then
19185 -- The index is given by a subtype with a range constraint
19187 T := Base_Type (Entity (Subtype_Mark (N)));
19189 if not Is_Discrete_Type (T) then
19190 Error_Msg_N ("discrete type required for range", N);
19191 Set_Etype (N, Any_Type);
19195 R := Range_Expression (Constraint (N));
19198 Process_Range_Expr_In_Decl
19199 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
19201 elsif Nkind (N) = N_Attribute_Reference then
19203 -- Catch beginner's error (use of attribute other than 'Range)
19205 if Attribute_Name (N) /= Name_Range then
19206 Error_Msg_N ("expect attribute ''Range", N);
19207 Set_Etype (N, Any_Type);
19211 -- If the node denotes the range of a type mark, that is also the
19212 -- resulting type, and we do not need to create an Itype for it.
19214 if Is_Entity_Name (Prefix (N))
19215 and then Comes_From_Source (N)
19216 and then Is_Type (Entity (Prefix (N)))
19217 and then Is_Discrete_Type (Entity (Prefix (N)))
19219 Def_Id := Entity (Prefix (N));
19222 Analyze_And_Resolve (N);
19226 -- If none of the above, must be a subtype. We convert this to a
19227 -- range attribute reference because in the case of declared first
19228 -- named subtypes, the types in the range reference can be different
19229 -- from the type of the entity. A range attribute normalizes the
19230 -- reference and obtains the correct types for the bounds.
19232 -- This transformation is in the nature of an expansion, is only
19233 -- done if expansion is active. In particular, it is not done on
19234 -- formal generic types, because we need to retain the name of the
19235 -- original index for instantiation purposes.
19238 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19239 Error_Msg_N ("invalid subtype mark in discrete range ", N);
19240 Set_Etype (N, Any_Integer);
19244 -- The type mark may be that of an incomplete type. It is only
19245 -- now that we can get the full view, previous analysis does
19246 -- not look specifically for a type mark.
19248 Set_Entity (N, Get_Full_View (Entity (N)));
19249 Set_Etype (N, Entity (N));
19250 Def_Id := Entity (N);
19252 if not Is_Discrete_Type (Def_Id) then
19253 Error_Msg_N ("discrete type required for index", N);
19254 Set_Etype (N, Any_Type);
19259 if Expander_Active then
19261 Make_Attribute_Reference (Sloc (N),
19262 Attribute_Name => Name_Range,
19263 Prefix => Relocate_Node (N)));
19265 -- The original was a subtype mark that does not freeze. This
19266 -- means that the rewritten version must not freeze either.
19268 Set_Must_Not_Freeze (N);
19269 Set_Must_Not_Freeze (Prefix (N));
19270 Analyze_And_Resolve (N);
19274 -- If expander is inactive, type is legal, nothing else to construct
19281 if not Is_Discrete_Type (T) then
19282 Error_Msg_N ("discrete type required for range", N);
19283 Set_Etype (N, Any_Type);
19286 elsif T = Any_Type then
19287 Set_Etype (N, Any_Type);
19291 -- We will now create the appropriate Itype to describe the range, but
19292 -- first a check. If we originally had a subtype, then we just label
19293 -- the range with this subtype. Not only is there no need to construct
19294 -- a new subtype, but it is wrong to do so for two reasons:
19296 -- 1. A legality concern, if we have a subtype, it must not freeze,
19297 -- and the Itype would cause freezing incorrectly
19299 -- 2. An efficiency concern, if we created an Itype, it would not be
19300 -- recognized as the same type for the purposes of eliminating
19301 -- checks in some circumstances.
19303 -- We signal this case by setting the subtype entity in Def_Id
19305 if No (Def_Id) then
19307 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19308 Set_Etype (Def_Id, Base_Type (T));
19310 if Is_Signed_Integer_Type (T) then
19311 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19313 elsif Is_Modular_Integer_Type (T) then
19314 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19317 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19318 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19319 Set_First_Literal (Def_Id, First_Literal (T));
19322 Set_Size_Info (Def_Id, (T));
19323 Set_RM_Size (Def_Id, RM_Size (T));
19324 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19326 Set_Scalar_Range (Def_Id, R);
19327 Conditional_Delay (Def_Id, T);
19329 if Nkind (N) = N_Subtype_Indication then
19330 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19333 -- In the subtype indication case, if the immediate parent of the
19334 -- new subtype is non-static, then the subtype we create is non-
19335 -- static, even if its bounds are static.
19337 if Nkind (N) = N_Subtype_Indication
19338 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19340 Set_Is_Non_Static_Subtype (Def_Id);
19344 -- Final step is to label the index with this constructed type
19346 Set_Etype (N, Def_Id);
19349 ------------------------------
19350 -- Modular_Type_Declaration --
19351 ------------------------------
19353 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19354 Mod_Expr : constant Node_Id := Expression (Def);
19357 procedure Set_Modular_Size (Bits : Int);
19358 -- Sets RM_Size to Bits, and Esize to normal word size above this
19360 ----------------------
19361 -- Set_Modular_Size --
19362 ----------------------
19364 procedure Set_Modular_Size (Bits : Int) is
19366 Set_RM_Size (T, UI_From_Int (Bits));
19371 elsif Bits <= 16 then
19372 Init_Esize (T, 16);
19374 elsif Bits <= 32 then
19375 Init_Esize (T, 32);
19378 Init_Esize (T, System_Max_Binary_Modulus_Power);
19381 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19382 Set_Is_Known_Valid (T);
19384 end Set_Modular_Size;
19386 -- Start of processing for Modular_Type_Declaration
19389 -- If the mod expression is (exactly) 2 * literal, where literal is
19390 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19392 if Warn_On_Suspicious_Modulus_Value
19393 and then Nkind (Mod_Expr) = N_Op_Multiply
19394 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19395 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19396 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19397 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19400 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19403 -- Proceed with analysis of mod expression
19405 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19407 Set_Ekind (T, E_Modular_Integer_Type);
19408 Init_Alignment (T);
19409 Set_Is_Constrained (T);
19411 if not Is_OK_Static_Expression (Mod_Expr) then
19412 Flag_Non_Static_Expr
19413 ("non-static expression used for modular type bound!", Mod_Expr);
19414 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19416 M_Val := Expr_Value (Mod_Expr);
19420 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19421 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19424 if M_Val > 2 ** Standard_Long_Integer_Size then
19425 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19428 Set_Modulus (T, M_Val);
19430 -- Create bounds for the modular type based on the modulus given in
19431 -- the type declaration and then analyze and resolve those bounds.
19433 Set_Scalar_Range (T,
19434 Make_Range (Sloc (Mod_Expr),
19435 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19436 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19438 -- Properly analyze the literals for the range. We do this manually
19439 -- because we can't go calling Resolve, since we are resolving these
19440 -- bounds with the type, and this type is certainly not complete yet.
19442 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19443 Set_Etype (High_Bound (Scalar_Range (T)), T);
19444 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19445 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19447 -- Loop through powers of two to find number of bits required
19449 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19453 if M_Val = 2 ** Bits then
19454 Set_Modular_Size (Bits);
19459 elsif M_Val < 2 ** Bits then
19460 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
19461 Set_Non_Binary_Modulus (T);
19463 if Bits > System_Max_Nonbinary_Modulus_Power then
19464 Error_Msg_Uint_1 :=
19465 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19467 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19468 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19472 -- In the nonbinary case, set size as per RM 13.3(55)
19474 Set_Modular_Size (Bits);
19481 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19482 -- so we just signal an error and set the maximum size.
19484 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19485 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19487 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19488 Init_Alignment (T);
19490 end Modular_Type_Declaration;
19492 --------------------------
19493 -- New_Concatenation_Op --
19494 --------------------------
19496 procedure New_Concatenation_Op (Typ : Entity_Id) is
19497 Loc : constant Source_Ptr := Sloc (Typ);
19500 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19501 -- Create abbreviated declaration for the formal of a predefined
19502 -- Operator 'Op' of type 'Typ'
19504 --------------------
19505 -- Make_Op_Formal --
19506 --------------------
19508 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19509 Formal : Entity_Id;
19511 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19512 Set_Etype (Formal, Typ);
19513 Set_Mechanism (Formal, Default_Mechanism);
19515 end Make_Op_Formal;
19517 -- Start of processing for New_Concatenation_Op
19520 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19522 Set_Ekind (Op, E_Operator);
19523 Set_Scope (Op, Current_Scope);
19524 Set_Etype (Op, Typ);
19525 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19526 Set_Is_Immediately_Visible (Op);
19527 Set_Is_Intrinsic_Subprogram (Op);
19528 Set_Has_Completion (Op);
19529 Append_Entity (Op, Current_Scope);
19531 Set_Name_Entity_Id (Name_Op_Concat, Op);
19533 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19534 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19535 end New_Concatenation_Op;
19537 -------------------------
19538 -- OK_For_Limited_Init --
19539 -------------------------
19541 -- ???Check all calls of this, and compare the conditions under which it's
19544 function OK_For_Limited_Init
19546 Exp : Node_Id) return Boolean
19549 return Is_CPP_Constructor_Call (Exp)
19550 or else (Ada_Version >= Ada_2005
19551 and then not Debug_Flag_Dot_L
19552 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19553 end OK_For_Limited_Init;
19555 -------------------------------
19556 -- OK_For_Limited_Init_In_05 --
19557 -------------------------------
19559 function OK_For_Limited_Init_In_05
19561 Exp : Node_Id) return Boolean
19564 -- An object of a limited interface type can be initialized with any
19565 -- expression of a nonlimited descendant type. However this does not
19566 -- apply if this is a view conversion of some other expression. This
19567 -- is checked below.
19569 if Is_Class_Wide_Type (Typ)
19570 and then Is_Limited_Interface (Typ)
19571 and then not Is_Limited_Type (Etype (Exp))
19572 and then Nkind (Exp) /= N_Type_Conversion
19577 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19578 -- case of limited aggregates (including extension aggregates), and
19579 -- function calls. The function call may have been given in prefixed
19580 -- notation, in which case the original node is an indexed component.
19581 -- If the function is parameterless, the original node was an explicit
19582 -- dereference. The function may also be parameterless, in which case
19583 -- the source node is just an identifier.
19585 -- A branch of a conditional expression may have been removed if the
19586 -- condition is statically known. This happens during expansion, and
19587 -- thus will not happen if previous errors were encountered. The check
19588 -- will have been performed on the chosen branch, which replaces the
19589 -- original conditional expression.
19595 case Nkind (Original_Node (Exp)) is
19597 | N_Extension_Aggregate
19603 when N_Identifier =>
19604 return Present (Entity (Original_Node (Exp)))
19605 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19607 when N_Qualified_Expression =>
19609 OK_For_Limited_Init_In_05
19610 (Typ, Expression (Original_Node (Exp)));
19612 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19613 -- with a function call, the expander has rewritten the call into an
19614 -- N_Type_Conversion node to force displacement of the pointer to
19615 -- reference the component containing the secondary dispatch table.
19616 -- Otherwise a type conversion is not a legal context.
19617 -- A return statement for a build-in-place function returning a
19618 -- synchronized type also introduces an unchecked conversion.
19620 when N_Type_Conversion
19621 | N_Unchecked_Type_Conversion
19623 return not Comes_From_Source (Exp)
19625 OK_For_Limited_Init_In_05
19626 (Typ, Expression (Original_Node (Exp)));
19628 when N_Explicit_Dereference
19629 | N_Indexed_Component
19630 | N_Selected_Component
19632 return Nkind (Exp) = N_Function_Call;
19634 -- A use of 'Input is a function call, hence allowed. Normally the
19635 -- attribute will be changed to a call, but the attribute by itself
19636 -- can occur with -gnatc.
19638 when N_Attribute_Reference =>
19639 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19641 -- "return raise ..." is OK
19643 when N_Raise_Expression =>
19646 -- For a case expression, all dependent expressions must be legal
19648 when N_Case_Expression =>
19653 Alt := First (Alternatives (Original_Node (Exp)));
19654 while Present (Alt) loop
19655 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19665 -- For an if expression, all dependent expressions must be legal
19667 when N_If_Expression =>
19669 Then_Expr : constant Node_Id :=
19670 Next (First (Expressions (Original_Node (Exp))));
19671 Else_Expr : constant Node_Id := Next (Then_Expr);
19673 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19675 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19681 end OK_For_Limited_Init_In_05;
19683 -------------------------------------------
19684 -- Ordinary_Fixed_Point_Type_Declaration --
19685 -------------------------------------------
19687 procedure Ordinary_Fixed_Point_Type_Declaration
19691 Loc : constant Source_Ptr := Sloc (Def);
19692 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19693 RRS : constant Node_Id := Real_Range_Specification (Def);
19694 Implicit_Base : Entity_Id;
19701 Check_Restriction (No_Fixed_Point, Def);
19703 -- Create implicit base type
19706 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19707 Set_Etype (Implicit_Base, Implicit_Base);
19709 -- Analyze and process delta expression
19711 Analyze_And_Resolve (Delta_Expr, Any_Real);
19713 Check_Delta_Expression (Delta_Expr);
19714 Delta_Val := Expr_Value_R (Delta_Expr);
19716 Set_Delta_Value (Implicit_Base, Delta_Val);
19718 -- Compute default small from given delta, which is the largest power
19719 -- of two that does not exceed the given delta value.
19729 if Delta_Val < Ureal_1 then
19730 while Delta_Val < Tmp loop
19731 Tmp := Tmp / Ureal_2;
19732 Scale := Scale + 1;
19737 Tmp := Tmp * Ureal_2;
19738 exit when Tmp > Delta_Val;
19739 Scale := Scale - 1;
19743 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19746 Set_Small_Value (Implicit_Base, Small_Val);
19748 -- If no range was given, set a dummy range
19750 if RRS <= Empty_Or_Error then
19751 Low_Val := -Small_Val;
19752 High_Val := Small_Val;
19754 -- Otherwise analyze and process given range
19758 Low : constant Node_Id := Low_Bound (RRS);
19759 High : constant Node_Id := High_Bound (RRS);
19762 Analyze_And_Resolve (Low, Any_Real);
19763 Analyze_And_Resolve (High, Any_Real);
19764 Check_Real_Bound (Low);
19765 Check_Real_Bound (High);
19767 -- Obtain and set the range
19769 Low_Val := Expr_Value_R (Low);
19770 High_Val := Expr_Value_R (High);
19772 if Low_Val > High_Val then
19773 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19778 -- The range for both the implicit base and the declared first subtype
19779 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19780 -- set a temporary range in place. Note that the bounds of the base
19781 -- type will be widened to be symmetrical and to fill the available
19782 -- bits when the type is frozen.
19784 -- We could do this with all discrete types, and probably should, but
19785 -- we absolutely have to do it for fixed-point, since the end-points
19786 -- of the range and the size are determined by the small value, which
19787 -- could be reset before the freeze point.
19789 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19790 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19792 -- Complete definition of first subtype. The inheritance of the rep item
19793 -- chain ensures that SPARK-related pragmas are not clobbered when the
19794 -- ordinary fixed point type acts as a full view of a private type.
19796 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19797 Set_Etype (T, Implicit_Base);
19798 Init_Size_Align (T);
19799 Inherit_Rep_Item_Chain (T, Implicit_Base);
19800 Set_Small_Value (T, Small_Val);
19801 Set_Delta_Value (T, Delta_Val);
19802 Set_Is_Constrained (T);
19803 end Ordinary_Fixed_Point_Type_Declaration;
19805 ----------------------------------
19806 -- Preanalyze_Assert_Expression --
19807 ----------------------------------
19809 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19811 In_Assertion_Expr := In_Assertion_Expr + 1;
19812 Preanalyze_Spec_Expression (N, T);
19813 In_Assertion_Expr := In_Assertion_Expr - 1;
19814 end Preanalyze_Assert_Expression;
19816 -----------------------------------
19817 -- Preanalyze_Default_Expression --
19818 -----------------------------------
19820 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19821 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19823 In_Default_Expr := True;
19824 Preanalyze_Spec_Expression (N, T);
19825 In_Default_Expr := Save_In_Default_Expr;
19826 end Preanalyze_Default_Expression;
19828 --------------------------------
19829 -- Preanalyze_Spec_Expression --
19830 --------------------------------
19832 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19833 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19835 In_Spec_Expression := True;
19836 Preanalyze_And_Resolve (N, T);
19837 In_Spec_Expression := Save_In_Spec_Expression;
19838 end Preanalyze_Spec_Expression;
19840 ----------------------------------------
19841 -- Prepare_Private_Subtype_Completion --
19842 ----------------------------------------
19844 procedure Prepare_Private_Subtype_Completion
19846 Related_Nod : Node_Id)
19848 Id_B : constant Entity_Id := Base_Type (Id);
19849 Full_B : Entity_Id := Full_View (Id_B);
19853 if Present (Full_B) then
19855 -- Get to the underlying full view if necessary
19857 if Is_Private_Type (Full_B)
19858 and then Present (Underlying_Full_View (Full_B))
19860 Full_B := Underlying_Full_View (Full_B);
19863 -- The Base_Type is already completed, we can complete the subtype
19864 -- now. We have to create a new entity with the same name, Thus we
19865 -- can't use Create_Itype.
19867 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19868 Set_Is_Itype (Full);
19869 Set_Associated_Node_For_Itype (Full, Related_Nod);
19870 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19873 -- The parent subtype may be private, but the base might not, in some
19874 -- nested instances. In that case, the subtype does not need to be
19875 -- exchanged. It would still be nice to make private subtypes and their
19876 -- bases consistent at all times ???
19878 if Is_Private_Type (Id_B) then
19879 Append_Elmt (Id, Private_Dependents (Id_B));
19881 end Prepare_Private_Subtype_Completion;
19883 ---------------------------
19884 -- Process_Discriminants --
19885 ---------------------------
19887 procedure Process_Discriminants
19889 Prev : Entity_Id := Empty)
19891 Elist : constant Elist_Id := New_Elmt_List;
19894 Discr_Number : Uint;
19895 Discr_Type : Entity_Id;
19896 Default_Present : Boolean := False;
19897 Default_Not_Present : Boolean := False;
19900 -- A composite type other than an array type can have discriminants.
19901 -- On entry, the current scope is the composite type.
19903 -- The discriminants are initially entered into the scope of the type
19904 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19905 -- use, as explained at the end of this procedure.
19907 Discr := First (Discriminant_Specifications (N));
19908 while Present (Discr) loop
19909 Enter_Name (Defining_Identifier (Discr));
19911 -- For navigation purposes we add a reference to the discriminant
19912 -- in the entity for the type. If the current declaration is a
19913 -- completion, place references on the partial view. Otherwise the
19914 -- type is the current scope.
19916 if Present (Prev) then
19918 -- The references go on the partial view, if present. If the
19919 -- partial view has discriminants, the references have been
19920 -- generated already.
19922 if not Has_Discriminants (Prev) then
19923 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19927 (Current_Scope, Defining_Identifier (Discr), 'd');
19930 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19931 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19933 -- Ada 2005 (AI-254)
19935 if Present (Access_To_Subprogram_Definition
19936 (Discriminant_Type (Discr)))
19937 and then Protected_Present (Access_To_Subprogram_Definition
19938 (Discriminant_Type (Discr)))
19941 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19945 Find_Type (Discriminant_Type (Discr));
19946 Discr_Type := Etype (Discriminant_Type (Discr));
19948 if Error_Posted (Discriminant_Type (Discr)) then
19949 Discr_Type := Any_Type;
19953 -- Handling of discriminants that are access types
19955 if Is_Access_Type (Discr_Type) then
19957 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19958 -- limited record types
19960 if Ada_Version < Ada_2005 then
19961 Check_Access_Discriminant_Requires_Limited
19962 (Discr, Discriminant_Type (Discr));
19965 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19967 ("(Ada 83) access discriminant not allowed", Discr);
19970 -- If not access type, must be a discrete type
19972 elsif not Is_Discrete_Type (Discr_Type) then
19974 ("discriminants must have a discrete or access type",
19975 Discriminant_Type (Discr));
19978 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19980 -- If a discriminant specification includes the assignment compound
19981 -- delimiter followed by an expression, the expression is the default
19982 -- expression of the discriminant; the default expression must be of
19983 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19984 -- a default expression, we do the special preanalysis, since this
19985 -- expression does not freeze (see section "Handling of Default and
19986 -- Per-Object Expressions" in spec of package Sem).
19988 if Present (Expression (Discr)) then
19989 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19993 if Nkind (N) = N_Formal_Type_Declaration then
19995 ("discriminant defaults not allowed for formal type",
19996 Expression (Discr));
19998 -- Flag an error for a tagged type with defaulted discriminants,
19999 -- excluding limited tagged types when compiling for Ada 2012
20000 -- (see AI05-0214).
20002 elsif Is_Tagged_Type (Current_Scope)
20003 and then (not Is_Limited_Type (Current_Scope)
20004 or else Ada_Version < Ada_2012)
20005 and then Comes_From_Source (N)
20007 -- Note: see similar test in Check_Or_Process_Discriminants, to
20008 -- handle the (illegal) case of the completion of an untagged
20009 -- view with discriminants with defaults by a tagged full view.
20010 -- We skip the check if Discr does not come from source, to
20011 -- account for the case of an untagged derived type providing
20012 -- defaults for a renamed discriminant from a private untagged
20013 -- ancestor with a tagged full view (ACATS B460006).
20015 if Ada_Version >= Ada_2012 then
20017 ("discriminants of nonlimited tagged type cannot have"
20019 Expression (Discr));
20022 ("discriminants of tagged type cannot have defaults",
20023 Expression (Discr));
20027 Default_Present := True;
20028 Append_Elmt (Expression (Discr), Elist);
20030 -- Tag the defining identifiers for the discriminants with
20031 -- their corresponding default expressions from the tree.
20033 Set_Discriminant_Default_Value
20034 (Defining_Identifier (Discr), Expression (Discr));
20037 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
20038 -- gets set unless we can be sure that no range check is required.
20040 if (GNATprove_Mode or not Expander_Active)
20043 (Expression (Discr), Discr_Type, Assume_Valid => True)
20045 Set_Do_Range_Check (Expression (Discr));
20048 -- No default discriminant value given
20051 Default_Not_Present := True;
20054 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20055 -- Discr_Type but with the null-exclusion attribute
20057 if Ada_Version >= Ada_2005 then
20059 -- Ada 2005 (AI-231): Static checks
20061 if Can_Never_Be_Null (Discr_Type) then
20062 Null_Exclusion_Static_Checks (Discr);
20064 elsif Is_Access_Type (Discr_Type)
20065 and then Null_Exclusion_Present (Discr)
20067 -- No need to check itypes because in their case this check
20068 -- was done at their point of creation
20070 and then not Is_Itype (Discr_Type)
20072 if Can_Never_Be_Null (Discr_Type) then
20074 ("`NOT NULL` not allowed (& already excludes null)",
20079 Set_Etype (Defining_Identifier (Discr),
20080 Create_Null_Excluding_Itype
20082 Related_Nod => Discr));
20084 -- Check for improper null exclusion if the type is otherwise
20085 -- legal for a discriminant.
20087 elsif Null_Exclusion_Present (Discr)
20088 and then Is_Discrete_Type (Discr_Type)
20091 ("null exclusion can only apply to an access type", Discr);
20094 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20095 -- can't have defaults. Synchronized types, or types that are
20096 -- explicitly limited are fine, but special tests apply to derived
20097 -- types in generics: in a generic body we have to assume the
20098 -- worst, and therefore defaults are not allowed if the parent is
20099 -- a generic formal private type (see ACATS B370001).
20101 if Is_Access_Type (Discr_Type) and then Default_Present then
20102 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20103 or else Is_Limited_Record (Current_Scope)
20104 or else Is_Concurrent_Type (Current_Scope)
20105 or else Is_Concurrent_Record_Type (Current_Scope)
20106 or else Ekind (Current_Scope) = E_Limited_Private_Type
20108 if not Is_Derived_Type (Current_Scope)
20109 or else not Is_Generic_Type (Etype (Current_Scope))
20110 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20111 or else Limited_Present
20112 (Type_Definition (Parent (Current_Scope)))
20118 ("access discriminants of nonlimited types cannot "
20119 & "have defaults", Expression (Discr));
20122 elsif Present (Expression (Discr)) then
20124 ("(Ada 2005) access discriminants of nonlimited types "
20125 & "cannot have defaults", Expression (Discr));
20130 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(6)).
20131 -- This check is relevant only when SPARK_Mode is on as it is not a
20132 -- standard Ada legality rule.
20135 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
20137 Error_Msg_N ("discriminant cannot be volatile", Discr);
20143 -- An element list consisting of the default expressions of the
20144 -- discriminants is constructed in the above loop and used to set
20145 -- the Discriminant_Constraint attribute for the type. If an object
20146 -- is declared of this (record or task) type without any explicit
20147 -- discriminant constraint given, this element list will form the
20148 -- actual parameters for the corresponding initialization procedure
20151 Set_Discriminant_Constraint (Current_Scope, Elist);
20152 Set_Stored_Constraint (Current_Scope, No_Elist);
20154 -- Default expressions must be provided either for all or for none
20155 -- of the discriminants of a discriminant part. (RM 3.7.1)
20157 if Default_Present and then Default_Not_Present then
20159 ("incomplete specification of defaults for discriminants", N);
20162 -- The use of the name of a discriminant is not allowed in default
20163 -- expressions of a discriminant part if the specification of the
20164 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20166 -- To detect this, the discriminant names are entered initially with an
20167 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20168 -- attempt to use a void entity (for example in an expression that is
20169 -- type-checked) produces the error message: premature usage. Now after
20170 -- completing the semantic analysis of the discriminant part, we can set
20171 -- the Ekind of all the discriminants appropriately.
20173 Discr := First (Discriminant_Specifications (N));
20174 Discr_Number := Uint_1;
20175 while Present (Discr) loop
20176 Id := Defining_Identifier (Discr);
20177 Set_Ekind (Id, E_Discriminant);
20178 Init_Component_Location (Id);
20180 Set_Discriminant_Number (Id, Discr_Number);
20182 -- Make sure this is always set, even in illegal programs
20184 Set_Corresponding_Discriminant (Id, Empty);
20186 -- Initialize the Original_Record_Component to the entity itself.
20187 -- Inherit_Components will propagate the right value to
20188 -- discriminants in derived record types.
20190 Set_Original_Record_Component (Id, Id);
20192 -- Create the discriminal for the discriminant
20194 Build_Discriminal (Id);
20197 Discr_Number := Discr_Number + 1;
20200 Set_Has_Discriminants (Current_Scope);
20201 end Process_Discriminants;
20203 -----------------------
20204 -- Process_Full_View --
20205 -----------------------
20207 -- WARNING: This routine manages Ghost regions. Return statements must be
20208 -- replaced by gotos which jump to the end of the routine and restore the
20211 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20212 procedure Collect_Implemented_Interfaces
20214 Ifaces : Elist_Id);
20215 -- Ada 2005: Gather all the interfaces that Typ directly or
20216 -- inherently implements. Duplicate entries are not added to
20217 -- the list Ifaces.
20219 ------------------------------------
20220 -- Collect_Implemented_Interfaces --
20221 ------------------------------------
20223 procedure Collect_Implemented_Interfaces
20228 Iface_Elmt : Elmt_Id;
20231 -- Abstract interfaces are only associated with tagged record types
20233 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20237 -- Recursively climb to the ancestors
20239 if Etype (Typ) /= Typ
20241 -- Protect the frontend against wrong cyclic declarations like:
20243 -- type B is new A with private;
20244 -- type C is new A with private;
20246 -- type B is new C with null record;
20247 -- type C is new B with null record;
20249 and then Etype (Typ) /= Priv_T
20250 and then Etype (Typ) /= Full_T
20252 -- Keep separate the management of private type declarations
20254 if Ekind (Typ) = E_Record_Type_With_Private then
20256 -- Handle the following illegal usage:
20257 -- type Private_Type is tagged private;
20259 -- type Private_Type is new Type_Implementing_Iface;
20261 if Present (Full_View (Typ))
20262 and then Etype (Typ) /= Full_View (Typ)
20264 if Is_Interface (Etype (Typ)) then
20265 Append_Unique_Elmt (Etype (Typ), Ifaces);
20268 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20271 -- Non-private types
20274 if Is_Interface (Etype (Typ)) then
20275 Append_Unique_Elmt (Etype (Typ), Ifaces);
20278 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20282 -- Handle entities in the list of abstract interfaces
20284 if Present (Interfaces (Typ)) then
20285 Iface_Elmt := First_Elmt (Interfaces (Typ));
20286 while Present (Iface_Elmt) loop
20287 Iface := Node (Iface_Elmt);
20289 pragma Assert (Is_Interface (Iface));
20291 if not Contain_Interface (Iface, Ifaces) then
20292 Append_Elmt (Iface, Ifaces);
20293 Collect_Implemented_Interfaces (Iface, Ifaces);
20296 Next_Elmt (Iface_Elmt);
20299 end Collect_Implemented_Interfaces;
20303 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20304 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
20305 -- Save the Ghost-related attributes to restore on exit
20307 Full_Indic : Node_Id;
20308 Full_Parent : Entity_Id;
20309 Priv_Parent : Entity_Id;
20311 -- Start of processing for Process_Full_View
20314 Mark_And_Set_Ghost_Completion (N, Priv_T);
20316 -- First some sanity checks that must be done after semantic
20317 -- decoration of the full view and thus cannot be placed with other
20318 -- similar checks in Find_Type_Name
20320 if not Is_Limited_Type (Priv_T)
20321 and then (Is_Limited_Type (Full_T)
20322 or else Is_Limited_Composite (Full_T))
20324 if In_Instance then
20328 ("completion of nonlimited type cannot be limited", Full_T);
20329 Explain_Limited_Type (Full_T, Full_T);
20332 elsif Is_Abstract_Type (Full_T)
20333 and then not Is_Abstract_Type (Priv_T)
20336 ("completion of nonabstract type cannot be abstract", Full_T);
20338 elsif Is_Tagged_Type (Priv_T)
20339 and then Is_Limited_Type (Priv_T)
20340 and then not Is_Limited_Type (Full_T)
20342 -- If pragma CPP_Class was applied to the private declaration
20343 -- propagate the limitedness to the full-view
20345 if Is_CPP_Class (Priv_T) then
20346 Set_Is_Limited_Record (Full_T);
20348 -- GNAT allow its own definition of Limited_Controlled to disobey
20349 -- this rule in order in ease the implementation. This test is safe
20350 -- because Root_Controlled is defined in a child of System that
20351 -- normal programs are not supposed to use.
20353 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20354 Set_Is_Limited_Composite (Full_T);
20357 ("completion of limited tagged type must be limited", Full_T);
20360 elsif Is_Generic_Type (Priv_T) then
20361 Error_Msg_N ("generic type cannot have a completion", Full_T);
20364 -- Check that ancestor interfaces of private and full views are
20365 -- consistent. We omit this check for synchronized types because
20366 -- they are performed on the corresponding record type when frozen.
20368 if Ada_Version >= Ada_2005
20369 and then Is_Tagged_Type (Priv_T)
20370 and then Is_Tagged_Type (Full_T)
20371 and then not Is_Concurrent_Type (Full_T)
20375 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20376 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20379 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20380 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20382 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20383 -- an interface type if and only if the full type is descendant
20384 -- of the interface type (AARM 7.3 (7.3/2)).
20386 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20388 if Present (Iface) then
20390 ("interface in partial view& not implemented by full type "
20391 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20394 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20396 if Present (Iface) then
20398 ("interface & not implemented by partial view "
20399 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20404 if Is_Tagged_Type (Priv_T)
20405 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20406 and then Is_Derived_Type (Full_T)
20408 Priv_Parent := Etype (Priv_T);
20410 -- The full view of a private extension may have been transformed
20411 -- into an unconstrained derived type declaration and a subtype
20412 -- declaration (see build_derived_record_type for details).
20414 if Nkind (N) = N_Subtype_Declaration then
20415 Full_Indic := Subtype_Indication (N);
20416 Full_Parent := Etype (Base_Type (Full_T));
20418 Full_Indic := Subtype_Indication (Type_Definition (N));
20419 Full_Parent := Etype (Full_T);
20422 -- Check that the parent type of the full type is a descendant of
20423 -- the ancestor subtype given in the private extension. If either
20424 -- entity has an Etype equal to Any_Type then we had some previous
20425 -- error situation [7.3(8)].
20427 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20430 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20431 -- any order. Therefore we don't have to check that its parent must
20432 -- be a descendant of the parent of the private type declaration.
20434 elsif Is_Interface (Priv_Parent)
20435 and then Is_Interface (Full_Parent)
20439 -- Ada 2005 (AI-251): If the parent of the private type declaration
20440 -- is an interface there is no need to check that it is an ancestor
20441 -- of the associated full type declaration. The required tests for
20442 -- this case are performed by Build_Derived_Record_Type.
20444 elsif not Is_Interface (Base_Type (Priv_Parent))
20445 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20448 ("parent of full type must descend from parent of private "
20449 & "extension", Full_Indic);
20451 -- First check a formal restriction, and then proceed with checking
20452 -- Ada rules. Since the formal restriction is not a serious error, we
20453 -- don't prevent further error detection for this check, hence the
20457 -- In formal mode, when completing a private extension the type
20458 -- named in the private part must be exactly the same as that
20459 -- named in the visible part.
20461 if Priv_Parent /= Full_Parent then
20462 Error_Msg_Name_1 := Chars (Priv_Parent);
20463 Check_SPARK_05_Restriction ("% expected", Full_Indic);
20466 -- Check the rules of 7.3(10): if the private extension inherits
20467 -- known discriminants, then the full type must also inherit those
20468 -- discriminants from the same (ancestor) type, and the parent
20469 -- subtype of the full type must be constrained if and only if
20470 -- the ancestor subtype of the private extension is constrained.
20472 if No (Discriminant_Specifications (Parent (Priv_T)))
20473 and then not Has_Unknown_Discriminants (Priv_T)
20474 and then Has_Discriminants (Base_Type (Priv_Parent))
20477 Priv_Indic : constant Node_Id :=
20478 Subtype_Indication (Parent (Priv_T));
20480 Priv_Constr : constant Boolean :=
20481 Is_Constrained (Priv_Parent)
20483 Nkind (Priv_Indic) = N_Subtype_Indication
20485 Is_Constrained (Entity (Priv_Indic));
20487 Full_Constr : constant Boolean :=
20488 Is_Constrained (Full_Parent)
20490 Nkind (Full_Indic) = N_Subtype_Indication
20492 Is_Constrained (Entity (Full_Indic));
20494 Priv_Discr : Entity_Id;
20495 Full_Discr : Entity_Id;
20498 Priv_Discr := First_Discriminant (Priv_Parent);
20499 Full_Discr := First_Discriminant (Full_Parent);
20500 while Present (Priv_Discr) and then Present (Full_Discr) loop
20501 if Original_Record_Component (Priv_Discr) =
20502 Original_Record_Component (Full_Discr)
20504 Corresponding_Discriminant (Priv_Discr) =
20505 Corresponding_Discriminant (Full_Discr)
20512 Next_Discriminant (Priv_Discr);
20513 Next_Discriminant (Full_Discr);
20516 if Present (Priv_Discr) or else Present (Full_Discr) then
20518 ("full view must inherit discriminants of the parent "
20519 & "type used in the private extension", Full_Indic);
20521 elsif Priv_Constr and then not Full_Constr then
20523 ("parent subtype of full type must be constrained",
20526 elsif Full_Constr and then not Priv_Constr then
20528 ("parent subtype of full type must be unconstrained",
20533 -- Check the rules of 7.3(12): if a partial view has neither
20534 -- known or unknown discriminants, then the full type
20535 -- declaration shall define a definite subtype.
20537 elsif not Has_Unknown_Discriminants (Priv_T)
20538 and then not Has_Discriminants (Priv_T)
20539 and then not Is_Constrained (Full_T)
20542 ("full view must define a constrained type if partial view "
20543 & "has no discriminants", Full_T);
20546 -- ??????? Do we implement the following properly ?????
20547 -- If the ancestor subtype of a private extension has constrained
20548 -- discriminants, then the parent subtype of the full view shall
20549 -- impose a statically matching constraint on those discriminants
20554 -- For untagged types, verify that a type without discriminants is
20555 -- not completed with an unconstrained type. A separate error message
20556 -- is produced if the full type has defaulted discriminants.
20558 if Is_Definite_Subtype (Priv_T)
20559 and then not Is_Definite_Subtype (Full_T)
20561 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20563 ("full view of& not compatible with declaration#",
20566 if not Is_Tagged_Type (Full_T) then
20568 ("\one is constrained, the other unconstrained", Full_T);
20573 -- AI-419: verify that the use of "limited" is consistent
20576 Orig_Decl : constant Node_Id := Original_Node (N);
20579 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20580 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20582 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20584 if not Limited_Present (Parent (Priv_T))
20585 and then not Synchronized_Present (Parent (Priv_T))
20586 and then Limited_Present (Type_Definition (Orig_Decl))
20589 ("full view of non-limited extension cannot be limited", N);
20591 -- Conversely, if the partial view carries the limited keyword,
20592 -- the full view must as well, even if it may be redundant.
20594 elsif Limited_Present (Parent (Priv_T))
20595 and then not Limited_Present (Type_Definition (Orig_Decl))
20598 ("full view of limited extension must be explicitly limited",
20604 -- Ada 2005 (AI-443): A synchronized private extension must be
20605 -- completed by a task or protected type.
20607 if Ada_Version >= Ada_2005
20608 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20609 and then Synchronized_Present (Parent (Priv_T))
20610 and then not Is_Concurrent_Type (Full_T)
20612 Error_Msg_N ("full view of synchronized extension must " &
20613 "be synchronized type", N);
20616 -- Ada 2005 AI-363: if the full view has discriminants with
20617 -- defaults, it is illegal to declare constrained access subtypes
20618 -- whose designated type is the current type. This allows objects
20619 -- of the type that are declared in the heap to be unconstrained.
20621 if not Has_Unknown_Discriminants (Priv_T)
20622 and then not Has_Discriminants (Priv_T)
20623 and then Has_Discriminants (Full_T)
20625 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20627 Set_Has_Constrained_Partial_View (Full_T);
20628 Set_Has_Constrained_Partial_View (Priv_T);
20631 -- Create a full declaration for all its subtypes recorded in
20632 -- Private_Dependents and swap them similarly to the base type. These
20633 -- are subtypes that have been define before the full declaration of
20634 -- the private type. We also swap the entry in Private_Dependents list
20635 -- so we can properly restore the private view on exit from the scope.
20638 Priv_Elmt : Elmt_Id;
20639 Priv_Scop : Entity_Id;
20644 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20645 while Present (Priv_Elmt) loop
20646 Priv := Node (Priv_Elmt);
20647 Priv_Scop := Scope (Priv);
20649 if Ekind_In (Priv, E_Private_Subtype,
20650 E_Limited_Private_Subtype,
20651 E_Record_Subtype_With_Private)
20653 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20654 Set_Is_Itype (Full);
20655 Set_Parent (Full, Parent (Priv));
20656 Set_Associated_Node_For_Itype (Full, N);
20658 -- Now we need to complete the private subtype, but since the
20659 -- base type has already been swapped, we must also swap the
20660 -- subtypes (and thus, reverse the arguments in the call to
20661 -- Complete_Private_Subtype). Also note that we may need to
20662 -- re-establish the scope of the private subtype.
20664 Copy_And_Swap (Priv, Full);
20666 if not In_Open_Scopes (Priv_Scop) then
20667 Push_Scope (Priv_Scop);
20670 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20672 Priv_Scop := Empty;
20675 Complete_Private_Subtype (Full, Priv, Full_T, N);
20677 if Present (Priv_Scop) then
20681 Replace_Elmt (Priv_Elmt, Full);
20684 Next_Elmt (Priv_Elmt);
20688 -- If the private view was tagged, copy the new primitive operations
20689 -- from the private view to the full view.
20691 if Is_Tagged_Type (Full_T) then
20693 Disp_Typ : Entity_Id;
20694 Full_List : Elist_Id;
20696 Prim_Elmt : Elmt_Id;
20697 Priv_List : Elist_Id;
20701 L : Elist_Id) return Boolean;
20702 -- Determine whether list L contains element E
20710 L : Elist_Id) return Boolean
20712 List_Elmt : Elmt_Id;
20715 List_Elmt := First_Elmt (L);
20716 while Present (List_Elmt) loop
20717 if Node (List_Elmt) = E then
20721 Next_Elmt (List_Elmt);
20727 -- Start of processing
20730 if Is_Tagged_Type (Priv_T) then
20731 Priv_List := Primitive_Operations (Priv_T);
20732 Prim_Elmt := First_Elmt (Priv_List);
20734 -- In the case of a concurrent type completing a private tagged
20735 -- type, primitives may have been declared in between the two
20736 -- views. These subprograms need to be wrapped the same way
20737 -- entries and protected procedures are handled because they
20738 -- cannot be directly shared by the two views.
20740 if Is_Concurrent_Type (Full_T) then
20742 Conc_Typ : constant Entity_Id :=
20743 Corresponding_Record_Type (Full_T);
20744 Curr_Nod : Node_Id := Parent (Conc_Typ);
20745 Wrap_Spec : Node_Id;
20748 while Present (Prim_Elmt) loop
20749 Prim := Node (Prim_Elmt);
20751 if Comes_From_Source (Prim)
20752 and then not Is_Abstract_Subprogram (Prim)
20755 Make_Subprogram_Declaration (Sloc (Prim),
20759 Obj_Typ => Conc_Typ,
20761 Parameter_Specifications
20764 Insert_After (Curr_Nod, Wrap_Spec);
20765 Curr_Nod := Wrap_Spec;
20767 Analyze (Wrap_Spec);
20769 -- Remove the wrapper from visibility to avoid
20770 -- spurious conflict with the wrapped entity.
20772 Set_Is_Immediately_Visible
20773 (Defining_Entity (Specification (Wrap_Spec)),
20777 Next_Elmt (Prim_Elmt);
20783 -- For non-concurrent types, transfer explicit primitives, but
20784 -- omit those inherited from the parent of the private view
20785 -- since they will be re-inherited later on.
20788 Full_List := Primitive_Operations (Full_T);
20789 while Present (Prim_Elmt) loop
20790 Prim := Node (Prim_Elmt);
20792 if Comes_From_Source (Prim)
20793 and then not Contains (Prim, Full_List)
20795 Append_Elmt (Prim, Full_List);
20798 Next_Elmt (Prim_Elmt);
20802 -- Untagged private view
20805 Full_List := Primitive_Operations (Full_T);
20807 -- In this case the partial view is untagged, so here we locate
20808 -- all of the earlier primitives that need to be treated as
20809 -- dispatching (those that appear between the two views). Note
20810 -- that these additional operations must all be new operations
20811 -- (any earlier operations that override inherited operations
20812 -- of the full view will already have been inserted in the
20813 -- primitives list, marked by Check_Operation_From_Private_View
20814 -- as dispatching. Note that implicit "/=" operators are
20815 -- excluded from being added to the primitives list since they
20816 -- shouldn't be treated as dispatching (tagged "/=" is handled
20819 Prim := Next_Entity (Full_T);
20820 while Present (Prim) and then Prim /= Priv_T loop
20821 if Ekind_In (Prim, E_Procedure, E_Function) then
20822 Disp_Typ := Find_Dispatching_Type (Prim);
20824 if Disp_Typ = Full_T
20825 and then (Chars (Prim) /= Name_Op_Ne
20826 or else Comes_From_Source (Prim))
20828 Check_Controlling_Formals (Full_T, Prim);
20830 if Is_Suitable_Primitive (Prim)
20831 and then not Is_Dispatching_Operation (Prim)
20833 Append_Elmt (Prim, Full_List);
20834 Set_Is_Dispatching_Operation (Prim);
20835 Set_DT_Position_Value (Prim, No_Uint);
20838 elsif Is_Dispatching_Operation (Prim)
20839 and then Disp_Typ /= Full_T
20841 -- Verify that it is not otherwise controlled by a
20842 -- formal or a return value of type T.
20844 Check_Controlling_Formals (Disp_Typ, Prim);
20848 Next_Entity (Prim);
20852 -- For the tagged case, the two views can share the same primitive
20853 -- operations list and the same class-wide type. Update attributes
20854 -- of the class-wide type which depend on the full declaration.
20856 if Is_Tagged_Type (Priv_T) then
20857 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20858 Set_Class_Wide_Type
20859 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20861 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20866 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20868 if Known_To_Have_Preelab_Init (Priv_T) then
20870 -- Case where there is a pragma Preelaborable_Initialization. We
20871 -- always allow this in predefined units, which is cheating a bit,
20872 -- but it means we don't have to struggle to meet the requirements in
20873 -- the RM for having Preelaborable Initialization. Otherwise we
20874 -- require that the type meets the RM rules. But we can't check that
20875 -- yet, because of the rule about overriding Initialize, so we simply
20876 -- set a flag that will be checked at freeze time.
20878 if not In_Predefined_Unit (Full_T) then
20879 Set_Must_Have_Preelab_Init (Full_T);
20883 -- If pragma CPP_Class was applied to the private type declaration,
20884 -- propagate it now to the full type declaration.
20886 if Is_CPP_Class (Priv_T) then
20887 Set_Is_CPP_Class (Full_T);
20888 Set_Convention (Full_T, Convention_CPP);
20890 -- Check that components of imported CPP types do not have default
20893 Check_CPP_Type_Has_No_Defaults (Full_T);
20896 -- If the private view has user specified stream attributes, then so has
20899 -- Why the test, how could these flags be already set in Full_T ???
20901 if Has_Specified_Stream_Read (Priv_T) then
20902 Set_Has_Specified_Stream_Read (Full_T);
20905 if Has_Specified_Stream_Write (Priv_T) then
20906 Set_Has_Specified_Stream_Write (Full_T);
20909 if Has_Specified_Stream_Input (Priv_T) then
20910 Set_Has_Specified_Stream_Input (Full_T);
20913 if Has_Specified_Stream_Output (Priv_T) then
20914 Set_Has_Specified_Stream_Output (Full_T);
20917 -- Propagate Default_Initial_Condition-related attributes from the
20918 -- partial view to the full view and its base type.
20920 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20921 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20923 -- Propagate invariant-related attributes from the partial view to the
20924 -- full view and its base type.
20926 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20927 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20929 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20930 -- in the full view without advertising the inheritance in the partial
20931 -- view. This can only occur when the partial view has no parent type
20932 -- and the full view has an interface as a parent. Any other scenarios
20933 -- are illegal because implemented interfaces must match between the
20936 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20938 Full_Par : constant Entity_Id := Etype (Full_T);
20939 Priv_Par : constant Entity_Id := Etype (Priv_T);
20942 if not Is_Interface (Priv_Par)
20943 and then Is_Interface (Full_Par)
20944 and then Has_Inheritable_Invariants (Full_Par)
20947 ("hidden inheritance of class-wide type invariants not "
20953 -- Propagate predicates to full type, and predicate function if already
20954 -- defined. It is not clear that this can actually happen? the partial
20955 -- view cannot be frozen yet, and the predicate function has not been
20956 -- built. Still it is a cheap check and seems safer to make it.
20958 if Has_Predicates (Priv_T) then
20959 Set_Has_Predicates (Full_T);
20961 if Present (Predicate_Function (Priv_T)) then
20962 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20967 Restore_Ghost_Region (Saved_GM, Saved_IGR);
20968 end Process_Full_View;
20970 -----------------------------------
20971 -- Process_Incomplete_Dependents --
20972 -----------------------------------
20974 procedure Process_Incomplete_Dependents
20976 Full_T : Entity_Id;
20979 Inc_Elmt : Elmt_Id;
20980 Priv_Dep : Entity_Id;
20981 New_Subt : Entity_Id;
20983 Disc_Constraint : Elist_Id;
20986 if No (Private_Dependents (Inc_T)) then
20990 -- Itypes that may be generated by the completion of an incomplete
20991 -- subtype are not used by the back-end and not attached to the tree.
20992 -- They are created only for constraint-checking purposes.
20994 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20995 while Present (Inc_Elmt) loop
20996 Priv_Dep := Node (Inc_Elmt);
20998 if Ekind (Priv_Dep) = E_Subprogram_Type then
21000 -- An Access_To_Subprogram type may have a return type or a
21001 -- parameter type that is incomplete. Replace with the full view.
21003 if Etype (Priv_Dep) = Inc_T then
21004 Set_Etype (Priv_Dep, Full_T);
21008 Formal : Entity_Id;
21011 Formal := First_Formal (Priv_Dep);
21012 while Present (Formal) loop
21013 if Etype (Formal) = Inc_T then
21014 Set_Etype (Formal, Full_T);
21017 Next_Formal (Formal);
21021 elsif Is_Overloadable (Priv_Dep) then
21023 -- If a subprogram in the incomplete dependents list is primitive
21024 -- for a tagged full type then mark it as a dispatching operation,
21025 -- check whether it overrides an inherited subprogram, and check
21026 -- restrictions on its controlling formals. Note that a protected
21027 -- operation is never dispatching: only its wrapper operation
21028 -- (which has convention Ada) is.
21030 if Is_Tagged_Type (Full_T)
21031 and then Is_Primitive (Priv_Dep)
21032 and then Convention (Priv_Dep) /= Convention_Protected
21034 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21035 Set_Is_Dispatching_Operation (Priv_Dep);
21036 Check_Controlling_Formals (Full_T, Priv_Dep);
21039 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21041 -- Can happen during processing of a body before the completion
21042 -- of a TA type. Ignore, because spec is also on dependent list.
21046 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21047 -- corresponding subtype of the full view.
21049 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21050 and then Comes_From_Source (Priv_Dep)
21052 Set_Subtype_Indication
21053 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21054 Set_Etype (Priv_Dep, Full_T);
21055 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21056 Set_Analyzed (Parent (Priv_Dep), False);
21058 -- Reanalyze the declaration, suppressing the call to Enter_Name
21059 -- to avoid duplicate names.
21061 Analyze_Subtype_Declaration
21062 (N => Parent (Priv_Dep),
21065 -- Dependent is a subtype
21068 -- We build a new subtype indication using the full view of the
21069 -- incomplete parent. The discriminant constraints have been
21070 -- elaborated already at the point of the subtype declaration.
21072 New_Subt := Create_Itype (E_Void, N);
21074 if Has_Discriminants (Full_T) then
21075 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21077 Disc_Constraint := No_Elist;
21080 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21081 Set_Full_View (Priv_Dep, New_Subt);
21084 Next_Elmt (Inc_Elmt);
21086 end Process_Incomplete_Dependents;
21088 --------------------------------
21089 -- Process_Range_Expr_In_Decl --
21090 --------------------------------
21092 procedure Process_Range_Expr_In_Decl
21095 Subtyp : Entity_Id := Empty;
21096 Check_List : List_Id := Empty_List;
21097 R_Check_Off : Boolean := False;
21098 In_Iter_Schm : Boolean := False)
21101 R_Checks : Check_Result;
21102 Insert_Node : Node_Id;
21103 Def_Id : Entity_Id;
21106 Analyze_And_Resolve (R, Base_Type (T));
21108 if Nkind (R) = N_Range then
21110 -- In SPARK, all ranges should be static, with the exception of the
21111 -- discrete type definition of a loop parameter specification.
21113 if not In_Iter_Schm
21114 and then not Is_OK_Static_Range (R)
21116 Check_SPARK_05_Restriction ("range should be static", R);
21119 Lo := Low_Bound (R);
21120 Hi := High_Bound (R);
21122 -- Validity checks on the range of a quantified expression are
21123 -- delayed until the construct is transformed into a loop.
21125 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21126 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21130 -- We need to ensure validity of the bounds here, because if we
21131 -- go ahead and do the expansion, then the expanded code will get
21132 -- analyzed with range checks suppressed and we miss the check.
21134 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21135 -- the temporaries generated by routine Remove_Side_Effects by means
21136 -- of validity checks must use the same names. When a range appears
21137 -- in the parent of a generic, the range is processed with checks
21138 -- disabled as part of the generic context and with checks enabled
21139 -- for code generation purposes. This leads to link issues as the
21140 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21141 -- template sees the temporaries generated by Remove_Side_Effects.
21144 Validity_Check_Range (R, Subtyp);
21147 -- If there were errors in the declaration, try and patch up some
21148 -- common mistakes in the bounds. The cases handled are literals
21149 -- which are Integer where the expected type is Real and vice versa.
21150 -- These corrections allow the compilation process to proceed further
21151 -- along since some basic assumptions of the format of the bounds
21154 if Etype (R) = Any_Type then
21155 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21157 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21159 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21161 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21163 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21165 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21167 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21169 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21176 -- If the bounds of the range have been mistakenly given as string
21177 -- literals (perhaps in place of character literals), then an error
21178 -- has already been reported, but we rewrite the string literal as a
21179 -- bound of the range's type to avoid blowups in later processing
21180 -- that looks at static values.
21182 if Nkind (Lo) = N_String_Literal then
21184 Make_Attribute_Reference (Sloc (Lo),
21185 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21186 Attribute_Name => Name_First));
21187 Analyze_And_Resolve (Lo);
21190 if Nkind (Hi) = N_String_Literal then
21192 Make_Attribute_Reference (Sloc (Hi),
21193 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21194 Attribute_Name => Name_First));
21195 Analyze_And_Resolve (Hi);
21198 -- If bounds aren't scalar at this point then exit, avoiding
21199 -- problems with further processing of the range in this procedure.
21201 if not Is_Scalar_Type (Etype (Lo)) then
21205 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21206 -- then range of the base type. Here we check whether the bounds
21207 -- are in the range of the subtype itself. Note that if the bounds
21208 -- represent the null range the Constraint_Error exception should
21211 -- ??? The following code should be cleaned up as follows
21213 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21214 -- is done in the call to Range_Check (R, T); below
21216 -- 2. The use of R_Check_Off should be investigated and possibly
21217 -- removed, this would clean up things a bit.
21219 if Is_Null_Range (Lo, Hi) then
21223 -- Capture values of bounds and generate temporaries for them
21224 -- if needed, before applying checks, since checks may cause
21225 -- duplication of the expression without forcing evaluation.
21227 -- The forced evaluation removes side effects from expressions,
21228 -- which should occur also in GNATprove mode. Otherwise, we end up
21229 -- with unexpected insertions of actions at places where this is
21230 -- not supposed to occur, e.g. on default parameters of a call.
21232 if Expander_Active or GNATprove_Mode then
21234 -- Call Force_Evaluation to create declarations as needed to
21235 -- deal with side effects, and also create typ_FIRST/LAST
21236 -- entities for bounds if we have a subtype name.
21238 -- Note: we do this transformation even if expansion is not
21239 -- active if we are in GNATprove_Mode since the transformation
21240 -- is in general required to ensure that the resulting tree has
21241 -- proper Ada semantics.
21244 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21246 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21249 -- We use a flag here instead of suppressing checks on the type
21250 -- because the type we check against isn't necessarily the place
21251 -- where we put the check.
21253 if not R_Check_Off then
21254 R_Checks := Get_Range_Checks (R, T);
21256 -- Look up tree to find an appropriate insertion point. We
21257 -- can't just use insert_actions because later processing
21258 -- depends on the insertion node. Prior to Ada 2012 the
21259 -- insertion point could only be a declaration or a loop, but
21260 -- quantified expressions can appear within any context in an
21261 -- expression, and the insertion point can be any statement,
21262 -- pragma, or declaration.
21264 Insert_Node := Parent (R);
21265 while Present (Insert_Node) loop
21267 Nkind (Insert_Node) in N_Declaration
21270 (Insert_Node, N_Component_Declaration,
21271 N_Loop_Parameter_Specification,
21272 N_Function_Specification,
21273 N_Procedure_Specification);
21275 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21276 or else Nkind (Insert_Node) in
21277 N_Statement_Other_Than_Procedure_Call
21278 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21281 Insert_Node := Parent (Insert_Node);
21284 -- Why would Type_Decl not be present??? Without this test,
21285 -- short regression tests fail.
21287 if Present (Insert_Node) then
21289 -- Case of loop statement. Verify that the range is part
21290 -- of the subtype indication of the iteration scheme.
21292 if Nkind (Insert_Node) = N_Loop_Statement then
21297 Indic := Parent (R);
21298 while Present (Indic)
21299 and then Nkind (Indic) /= N_Subtype_Indication
21301 Indic := Parent (Indic);
21304 if Present (Indic) then
21305 Def_Id := Etype (Subtype_Mark (Indic));
21307 Insert_Range_Checks
21311 Sloc (Insert_Node),
21313 Do_Before => True);
21317 -- Insertion before a declaration. If the declaration
21318 -- includes discriminants, the list of applicable checks
21319 -- is given by the caller.
21321 elsif Nkind (Insert_Node) in N_Declaration then
21322 Def_Id := Defining_Identifier (Insert_Node);
21324 if (Ekind (Def_Id) = E_Record_Type
21325 and then Depends_On_Discriminant (R))
21327 (Ekind (Def_Id) = E_Protected_Type
21328 and then Has_Discriminants (Def_Id))
21330 Append_Range_Checks
21332 Check_List, Def_Id, Sloc (Insert_Node), R);
21335 Insert_Range_Checks
21337 Insert_Node, Def_Id, Sloc (Insert_Node), R);
21341 -- Insertion before a statement. Range appears in the
21342 -- context of a quantified expression. Insertion will
21343 -- take place when expression is expanded.
21352 -- Case of other than an explicit N_Range node
21354 -- The forced evaluation removes side effects from expressions, which
21355 -- should occur also in GNATprove mode. Otherwise, we end up with
21356 -- unexpected insertions of actions at places where this is not
21357 -- supposed to occur, e.g. on default parameters of a call.
21359 elsif Expander_Active or GNATprove_Mode then
21360 Get_Index_Bounds (R, Lo, Hi);
21361 Force_Evaluation (Lo);
21362 Force_Evaluation (Hi);
21364 end Process_Range_Expr_In_Decl;
21366 --------------------------------------
21367 -- Process_Real_Range_Specification --
21368 --------------------------------------
21370 procedure Process_Real_Range_Specification (Def : Node_Id) is
21371 Spec : constant Node_Id := Real_Range_Specification (Def);
21374 Err : Boolean := False;
21376 procedure Analyze_Bound (N : Node_Id);
21377 -- Analyze and check one bound
21379 -------------------
21380 -- Analyze_Bound --
21381 -------------------
21383 procedure Analyze_Bound (N : Node_Id) is
21385 Analyze_And_Resolve (N, Any_Real);
21387 if not Is_OK_Static_Expression (N) then
21388 Flag_Non_Static_Expr
21389 ("bound in real type definition is not static!", N);
21394 -- Start of processing for Process_Real_Range_Specification
21397 if Present (Spec) then
21398 Lo := Low_Bound (Spec);
21399 Hi := High_Bound (Spec);
21400 Analyze_Bound (Lo);
21401 Analyze_Bound (Hi);
21403 -- If error, clear away junk range specification
21406 Set_Real_Range_Specification (Def, Empty);
21409 end Process_Real_Range_Specification;
21411 ---------------------
21412 -- Process_Subtype --
21413 ---------------------
21415 function Process_Subtype
21417 Related_Nod : Node_Id;
21418 Related_Id : Entity_Id := Empty;
21419 Suffix : Character := ' ') return Entity_Id
21422 Def_Id : Entity_Id;
21423 Error_Node : Node_Id;
21424 Full_View_Id : Entity_Id;
21425 Subtype_Mark_Id : Entity_Id;
21427 May_Have_Null_Exclusion : Boolean;
21429 procedure Check_Incomplete (T : Node_Id);
21430 -- Called to verify that an incomplete type is not used prematurely
21432 ----------------------
21433 -- Check_Incomplete --
21434 ----------------------
21436 procedure Check_Incomplete (T : Node_Id) is
21438 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21440 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21442 not (Ada_Version >= Ada_2005
21444 (Nkind (Parent (T)) = N_Subtype_Declaration
21445 or else (Nkind (Parent (T)) = N_Subtype_Indication
21446 and then Nkind (Parent (Parent (T))) =
21447 N_Subtype_Declaration)))
21449 Error_Msg_N ("invalid use of type before its full declaration", T);
21451 end Check_Incomplete;
21453 -- Start of processing for Process_Subtype
21456 -- Case of no constraints present
21458 if Nkind (S) /= N_Subtype_Indication then
21461 -- No way to proceed if the subtype indication is malformed. This
21462 -- will happen for example when the subtype indication in an object
21463 -- declaration is missing altogether and the expression is analyzed
21464 -- as if it were that indication.
21466 if not Is_Entity_Name (S) then
21470 Check_Incomplete (S);
21473 -- Ada 2005 (AI-231): Static check
21475 if Ada_Version >= Ada_2005
21476 and then Present (P)
21477 and then Null_Exclusion_Present (P)
21478 and then Nkind (P) /= N_Access_To_Object_Definition
21479 and then not Is_Access_Type (Entity (S))
21481 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21484 -- The following is ugly, can't we have a range or even a flag???
21486 May_Have_Null_Exclusion :=
21487 Nkind_In (P, N_Access_Definition,
21488 N_Access_Function_Definition,
21489 N_Access_Procedure_Definition,
21490 N_Access_To_Object_Definition,
21492 N_Component_Definition)
21494 Nkind_In (P, N_Derived_Type_Definition,
21495 N_Discriminant_Specification,
21496 N_Formal_Object_Declaration,
21497 N_Object_Declaration,
21498 N_Object_Renaming_Declaration,
21499 N_Parameter_Specification,
21500 N_Subtype_Declaration);
21502 -- Create an Itype that is a duplicate of Entity (S) but with the
21503 -- null-exclusion attribute.
21505 if May_Have_Null_Exclusion
21506 and then Is_Access_Type (Entity (S))
21507 and then Null_Exclusion_Present (P)
21509 -- No need to check the case of an access to object definition.
21510 -- It is correct to define double not-null pointers.
21513 -- type Not_Null_Int_Ptr is not null access Integer;
21514 -- type Acc is not null access Not_Null_Int_Ptr;
21516 and then Nkind (P) /= N_Access_To_Object_Definition
21518 if Can_Never_Be_Null (Entity (S)) then
21519 case Nkind (Related_Nod) is
21520 when N_Full_Type_Declaration =>
21521 if Nkind (Type_Definition (Related_Nod))
21522 in N_Array_Type_Definition
21526 (Component_Definition
21527 (Type_Definition (Related_Nod)));
21530 Subtype_Indication (Type_Definition (Related_Nod));
21533 when N_Subtype_Declaration =>
21534 Error_Node := Subtype_Indication (Related_Nod);
21536 when N_Object_Declaration =>
21537 Error_Node := Object_Definition (Related_Nod);
21539 when N_Component_Declaration =>
21541 Subtype_Indication (Component_Definition (Related_Nod));
21543 when N_Allocator =>
21544 Error_Node := Expression (Related_Nod);
21547 pragma Assert (False);
21548 Error_Node := Related_Nod;
21552 ("`NOT NULL` not allowed (& already excludes null)",
21558 Create_Null_Excluding_Itype
21560 Related_Nod => P));
21561 Set_Entity (S, Etype (S));
21566 -- Case of constraint present, so that we have an N_Subtype_Indication
21567 -- node (this node is created only if constraints are present).
21570 Find_Type (Subtype_Mark (S));
21572 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21574 (Nkind (Parent (S)) = N_Subtype_Declaration
21575 and then Is_Itype (Defining_Identifier (Parent (S))))
21577 Check_Incomplete (Subtype_Mark (S));
21581 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21583 -- Explicit subtype declaration case
21585 if Nkind (P) = N_Subtype_Declaration then
21586 Def_Id := Defining_Identifier (P);
21588 -- Explicit derived type definition case
21590 elsif Nkind (P) = N_Derived_Type_Definition then
21591 Def_Id := Defining_Identifier (Parent (P));
21593 -- Implicit case, the Def_Id must be created as an implicit type.
21594 -- The one exception arises in the case of concurrent types, array
21595 -- and access types, where other subsidiary implicit types may be
21596 -- created and must appear before the main implicit type. In these
21597 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21598 -- has not yet been called to create Def_Id.
21601 if Is_Array_Type (Subtype_Mark_Id)
21602 or else Is_Concurrent_Type (Subtype_Mark_Id)
21603 or else Is_Access_Type (Subtype_Mark_Id)
21607 -- For the other cases, we create a new unattached Itype,
21608 -- and set the indication to ensure it gets attached later.
21612 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21616 -- If the kind of constraint is invalid for this kind of type,
21617 -- then give an error, and then pretend no constraint was given.
21619 if not Is_Valid_Constraint_Kind
21620 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21623 ("incorrect constraint for this kind of type", Constraint (S));
21625 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21627 -- Set Ekind of orphan itype, to prevent cascaded errors
21629 if Present (Def_Id) then
21630 Set_Ekind (Def_Id, Ekind (Any_Type));
21633 -- Make recursive call, having got rid of the bogus constraint
21635 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21638 -- Remaining processing depends on type. Select on Base_Type kind to
21639 -- ensure getting to the concrete type kind in the case of a private
21640 -- subtype (needed when only doing semantic analysis).
21642 case Ekind (Base_Type (Subtype_Mark_Id)) is
21643 when Access_Kind =>
21645 -- If this is a constraint on a class-wide type, discard it.
21646 -- There is currently no way to express a partial discriminant
21647 -- constraint on a type with unknown discriminants. This is
21648 -- a pathology that the ACATS wisely decides not to test.
21650 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21651 if Comes_From_Source (S) then
21653 ("constraint on class-wide type ignored??",
21657 if Nkind (P) = N_Subtype_Declaration then
21658 Set_Subtype_Indication (P,
21659 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21662 return Subtype_Mark_Id;
21665 Constrain_Access (Def_Id, S, Related_Nod);
21668 and then Is_Itype (Designated_Type (Def_Id))
21669 and then Nkind (Related_Nod) = N_Subtype_Declaration
21670 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21672 Build_Itype_Reference
21673 (Designated_Type (Def_Id), Related_Nod);
21677 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21679 when Decimal_Fixed_Point_Kind =>
21680 Constrain_Decimal (Def_Id, S);
21682 when Enumeration_Kind =>
21683 Constrain_Enumeration (Def_Id, S);
21685 when Ordinary_Fixed_Point_Kind =>
21686 Constrain_Ordinary_Fixed (Def_Id, S);
21689 Constrain_Float (Def_Id, S);
21691 when Integer_Kind =>
21692 Constrain_Integer (Def_Id, S);
21694 when Class_Wide_Kind
21695 | E_Incomplete_Type
21699 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21701 if Ekind (Def_Id) = E_Incomplete_Type then
21702 Set_Private_Dependents (Def_Id, New_Elmt_List);
21705 when Private_Kind =>
21707 -- A private type with unknown discriminants may be completed
21708 -- by an unconstrained array type.
21710 if Has_Unknown_Discriminants (Subtype_Mark_Id)
21711 and then Present (Full_View (Subtype_Mark_Id))
21712 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
21714 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21716 -- ... but more commonly is completed by a discriminated record
21720 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21723 -- The base type may be private but Def_Id may be a full view
21726 if Is_Private_Type (Def_Id) then
21727 Set_Private_Dependents (Def_Id, New_Elmt_List);
21730 -- In case of an invalid constraint prevent further processing
21731 -- since the type constructed is missing expected fields.
21733 if Etype (Def_Id) = Any_Type then
21737 -- If the full view is that of a task with discriminants,
21738 -- we must constrain both the concurrent type and its
21739 -- corresponding record type. Otherwise we will just propagate
21740 -- the constraint to the full view, if available.
21742 if Present (Full_View (Subtype_Mark_Id))
21743 and then Has_Discriminants (Subtype_Mark_Id)
21744 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21747 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21749 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21750 Constrain_Concurrent (Full_View_Id, S,
21751 Related_Nod, Related_Id, Suffix);
21752 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21753 Set_Full_View (Def_Id, Full_View_Id);
21755 -- Introduce an explicit reference to the private subtype,
21756 -- to prevent scope anomalies in gigi if first use appears
21757 -- in a nested context, e.g. a later function body.
21758 -- Should this be generated in other contexts than a full
21759 -- type declaration?
21761 if Is_Itype (Def_Id)
21763 Nkind (Parent (P)) = N_Full_Type_Declaration
21765 Build_Itype_Reference (Def_Id, Parent (P));
21769 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21772 when Concurrent_Kind =>
21773 Constrain_Concurrent (Def_Id, S,
21774 Related_Nod, Related_Id, Suffix);
21777 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21780 -- Size, Alignment, Representation aspects and Convention are always
21781 -- inherited from the base type.
21783 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21784 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
21785 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21787 -- The anonymous subtype created for the subtype indication
21788 -- inherits the predicates of the parent.
21790 if Has_Predicates (Subtype_Mark_Id) then
21791 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21793 -- Indicate where the predicate function may be found
21795 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
21796 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
21802 end Process_Subtype;
21804 -----------------------------
21805 -- Record_Type_Declaration --
21806 -----------------------------
21808 procedure Record_Type_Declaration
21813 Def : constant Node_Id := Type_Definition (N);
21814 Is_Tagged : Boolean;
21815 Tag_Comp : Entity_Id;
21818 -- These flags must be initialized before calling Process_Discriminants
21819 -- because this routine makes use of them.
21821 Set_Ekind (T, E_Record_Type);
21823 Init_Size_Align (T);
21824 Set_Interfaces (T, No_Elist);
21825 Set_Stored_Constraint (T, No_Elist);
21826 Set_Default_SSO (T);
21827 Set_No_Reordering (T, No_Component_Reordering);
21831 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21832 if Limited_Present (Def) then
21833 Check_SPARK_05_Restriction ("limited is not allowed", N);
21836 if Abstract_Present (Def) then
21837 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21840 -- The flag Is_Tagged_Type might have already been set by
21841 -- Find_Type_Name if it detected an error for declaration T. This
21842 -- arises in the case of private tagged types where the full view
21843 -- omits the word tagged.
21846 Tagged_Present (Def)
21847 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21849 Set_Is_Limited_Record (T, Limited_Present (Def));
21852 Set_Is_Tagged_Type (T, True);
21853 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21856 -- Type is abstract if full declaration carries keyword, or if
21857 -- previous partial view did.
21859 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21860 or else Abstract_Present (Def));
21863 Check_SPARK_05_Restriction ("interface is not allowed", N);
21866 Analyze_Interface_Declaration (T, Def);
21868 if Present (Discriminant_Specifications (N)) then
21870 ("interface types cannot have discriminants",
21871 Defining_Identifier
21872 (First (Discriminant_Specifications (N))));
21876 -- First pass: if there are self-referential access components,
21877 -- create the required anonymous access type declarations, and if
21878 -- need be an incomplete type declaration for T itself.
21880 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21882 if Ada_Version >= Ada_2005
21883 and then Present (Interface_List (Def))
21885 Check_Interfaces (N, Def);
21888 Ifaces_List : Elist_Id;
21891 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21892 -- already in the parents.
21896 Ifaces_List => Ifaces_List,
21897 Exclude_Parents => True);
21899 Set_Interfaces (T, Ifaces_List);
21903 -- Records constitute a scope for the component declarations within.
21904 -- The scope is created prior to the processing of these declarations.
21905 -- Discriminants are processed first, so that they are visible when
21906 -- processing the other components. The Ekind of the record type itself
21907 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21909 -- Enter record scope
21913 -- If an incomplete or private type declaration was already given for
21914 -- the type, then this scope already exists, and the discriminants have
21915 -- been declared within. We must verify that the full declaration
21916 -- matches the incomplete one.
21918 Check_Or_Process_Discriminants (N, T, Prev);
21920 Set_Is_Constrained (T, not Has_Discriminants (T));
21921 Set_Has_Delayed_Freeze (T, True);
21923 -- For tagged types add a manually analyzed component corresponding
21924 -- to the component _tag, the corresponding piece of tree will be
21925 -- expanded as part of the freezing actions if it is not a CPP_Class.
21929 -- Do not add the tag unless we are in expansion mode
21931 if Expander_Active then
21932 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21933 Enter_Name (Tag_Comp);
21935 Set_Ekind (Tag_Comp, E_Component);
21936 Set_Is_Tag (Tag_Comp);
21937 Set_Is_Aliased (Tag_Comp);
21938 Set_Etype (Tag_Comp, RTE (RE_Tag));
21939 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21940 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21941 Init_Component_Location (Tag_Comp);
21943 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21944 -- implemented interfaces.
21946 if Has_Interfaces (T) then
21947 Add_Interface_Tag_Components (N, T);
21951 Make_Class_Wide_Type (T);
21952 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21955 -- We must suppress range checks when processing record components in
21956 -- the presence of discriminants, since we don't want spurious checks to
21957 -- be generated during their analysis, but Suppress_Range_Checks flags
21958 -- must be reset the after processing the record definition.
21960 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21961 -- couldn't we just use the normal range check suppression method here.
21962 -- That would seem cleaner ???
21964 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21965 Set_Kill_Range_Checks (T, True);
21966 Record_Type_Definition (Def, Prev);
21967 Set_Kill_Range_Checks (T, False);
21969 Record_Type_Definition (Def, Prev);
21972 -- Exit from record scope
21976 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21977 -- the implemented interfaces and associate them an aliased entity.
21980 and then not Is_Empty_List (Interface_List (Def))
21982 Derive_Progenitor_Subprograms (T, T);
21985 Check_Function_Writable_Actuals (N);
21986 end Record_Type_Declaration;
21988 ----------------------------
21989 -- Record_Type_Definition --
21990 ----------------------------
21992 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21993 Component : Entity_Id;
21994 Ctrl_Components : Boolean := False;
21995 Final_Storage_Only : Boolean;
21999 if Ekind (Prev_T) = E_Incomplete_Type then
22000 T := Full_View (Prev_T);
22005 -- In SPARK, tagged types and type extensions may only be declared in
22006 -- the specification of library unit packages.
22008 if Present (Def) and then Is_Tagged_Type (T) then
22014 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
22015 Typ := Parent (Def);
22018 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
22019 Typ := Parent (Parent (Def));
22022 Ctxt := Parent (Typ);
22024 if Nkind (Ctxt) = N_Package_Body
22025 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
22027 Check_SPARK_05_Restriction
22028 ("type should be defined in package specification", Typ);
22030 elsif Nkind (Ctxt) /= N_Package_Specification
22031 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
22033 Check_SPARK_05_Restriction
22034 ("type should be defined in library unit package", Typ);
22039 Final_Storage_Only := not Is_Controlled (T);
22041 -- Ada 2005: Check whether an explicit Limited is present in a derived
22042 -- type declaration.
22044 if Nkind (Parent (Def)) = N_Derived_Type_Definition
22045 and then Limited_Present (Parent (Def))
22047 Set_Is_Limited_Record (T);
22050 -- If the component list of a record type is defined by the reserved
22051 -- word null and there is no discriminant part, then the record type has
22052 -- no components and all records of the type are null records (RM 3.7)
22053 -- This procedure is also called to process the extension part of a
22054 -- record extension, in which case the current scope may have inherited
22058 or else No (Component_List (Def))
22059 or else Null_Present (Component_List (Def))
22061 if not Is_Tagged_Type (T) then
22062 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
22066 Analyze_Declarations (Component_Items (Component_List (Def)));
22068 if Present (Variant_Part (Component_List (Def))) then
22069 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
22070 Analyze (Variant_Part (Component_List (Def)));
22074 -- After completing the semantic analysis of the record definition,
22075 -- record components, both new and inherited, are accessible. Set their
22076 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22077 -- whose Ekind may be void.
22079 Component := First_Entity (Current_Scope);
22080 while Present (Component) loop
22081 if Ekind (Component) = E_Void
22082 and then not Is_Itype (Component)
22084 Set_Ekind (Component, E_Component);
22085 Init_Component_Location (Component);
22088 Propagate_Concurrent_Flags (T, Etype (Component));
22090 if Ekind (Component) /= E_Component then
22093 -- Do not set Has_Controlled_Component on a class-wide equivalent
22094 -- type. See Make_CW_Equivalent_Type.
22096 elsif not Is_Class_Wide_Equivalent_Type (T)
22097 and then (Has_Controlled_Component (Etype (Component))
22098 or else (Chars (Component) /= Name_uParent
22099 and then Is_Controlled (Etype (Component))))
22101 Set_Has_Controlled_Component (T, True);
22102 Final_Storage_Only :=
22104 and then Finalize_Storage_Only (Etype (Component));
22105 Ctrl_Components := True;
22108 Next_Entity (Component);
22111 -- A Type is Finalize_Storage_Only only if all its controlled components
22114 if Ctrl_Components then
22115 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22118 -- Place reference to end record on the proper entity, which may
22119 -- be a partial view.
22121 if Present (Def) then
22122 Process_End_Label (Def, 'e', Prev_T);
22124 end Record_Type_Definition;
22126 ------------------------
22127 -- Replace_Components --
22128 ------------------------
22130 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
22131 function Process (N : Node_Id) return Traverse_Result;
22137 function Process (N : Node_Id) return Traverse_Result is
22141 if Nkind (N) = N_Discriminant_Specification then
22142 Comp := First_Discriminant (Typ);
22143 while Present (Comp) loop
22144 if Chars (Comp) = Chars (Defining_Identifier (N)) then
22145 Set_Defining_Identifier (N, Comp);
22149 Next_Discriminant (Comp);
22152 elsif Nkind (N) = N_Variant_Part then
22153 Comp := First_Discriminant (Typ);
22154 while Present (Comp) loop
22155 if Chars (Comp) = Chars (Name (N)) then
22156 Set_Entity (Name (N), Comp);
22160 Next_Discriminant (Comp);
22163 elsif Nkind (N) = N_Component_Declaration then
22164 Comp := First_Component (Typ);
22165 while Present (Comp) loop
22166 if Chars (Comp) = Chars (Defining_Identifier (N)) then
22167 Set_Defining_Identifier (N, Comp);
22171 Next_Component (Comp);
22178 procedure Replace is new Traverse_Proc (Process);
22180 -- Start of processing for Replace_Components
22184 end Replace_Components;
22186 -------------------------------
22187 -- Set_Completion_Referenced --
22188 -------------------------------
22190 procedure Set_Completion_Referenced (E : Entity_Id) is
22192 -- If in main unit, mark entity that is a completion as referenced,
22193 -- warnings go on the partial view when needed.
22195 if In_Extended_Main_Source_Unit (E) then
22196 Set_Referenced (E);
22198 end Set_Completion_Referenced;
22200 ---------------------
22201 -- Set_Default_SSO --
22202 ---------------------
22204 procedure Set_Default_SSO (T : Entity_Id) is
22206 case Opt.Default_SSO is
22210 Set_SSO_Set_Low_By_Default (T, True);
22212 Set_SSO_Set_High_By_Default (T, True);
22214 raise Program_Error;
22216 end Set_Default_SSO;
22218 ---------------------
22219 -- Set_Fixed_Range --
22220 ---------------------
22222 -- The range for fixed-point types is complicated by the fact that we
22223 -- do not know the exact end points at the time of the declaration. This
22224 -- is true for three reasons:
22226 -- A size clause may affect the fudging of the end-points.
22227 -- A small clause may affect the values of the end-points.
22228 -- We try to include the end-points if it does not affect the size.
22230 -- This means that the actual end-points must be established at the
22231 -- point when the type is frozen. Meanwhile, we first narrow the range
22232 -- as permitted (so that it will fit if necessary in a small specified
22233 -- size), and then build a range subtree with these narrowed bounds.
22234 -- Set_Fixed_Range constructs the range from real literal values, and
22235 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22237 -- The parent of this range is set to point to the entity so that it is
22238 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22239 -- other scalar types, which are just pointers to the range in the
22240 -- original tree, this would otherwise be an orphan).
22242 -- The tree is left unanalyzed. When the type is frozen, the processing
22243 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22244 -- analyzed, and uses this as an indication that it should complete
22245 -- work on the range (it will know the final small and size values).
22247 procedure Set_Fixed_Range
22253 S : constant Node_Id :=
22255 Low_Bound => Make_Real_Literal (Loc, Lo),
22256 High_Bound => Make_Real_Literal (Loc, Hi));
22258 Set_Scalar_Range (E, S);
22261 -- Before the freeze point, the bounds of a fixed point are universal
22262 -- and carry the corresponding type.
22264 Set_Etype (Low_Bound (S), Universal_Real);
22265 Set_Etype (High_Bound (S), Universal_Real);
22266 end Set_Fixed_Range;
22268 ----------------------------------
22269 -- Set_Scalar_Range_For_Subtype --
22270 ----------------------------------
22272 procedure Set_Scalar_Range_For_Subtype
22273 (Def_Id : Entity_Id;
22277 Kind : constant Entity_Kind := Ekind (Def_Id);
22280 -- Defend against previous error
22282 if Nkind (R) = N_Error then
22286 Set_Scalar_Range (Def_Id, R);
22288 -- We need to link the range into the tree before resolving it so
22289 -- that types that are referenced, including importantly the subtype
22290 -- itself, are properly frozen (Freeze_Expression requires that the
22291 -- expression be properly linked into the tree). Of course if it is
22292 -- already linked in, then we do not disturb the current link.
22294 if No (Parent (R)) then
22295 Set_Parent (R, Def_Id);
22298 -- Reset the kind of the subtype during analysis of the range, to
22299 -- catch possible premature use in the bounds themselves.
22301 Set_Ekind (Def_Id, E_Void);
22302 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22303 Set_Ekind (Def_Id, Kind);
22304 end Set_Scalar_Range_For_Subtype;
22306 --------------------------------------------------------
22307 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22308 --------------------------------------------------------
22310 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22314 -- Make sure set if encountered during Expand_To_Stored_Constraint
22316 Set_Stored_Constraint (E, No_Elist);
22318 -- Give it the right value
22320 if Is_Constrained (E) and then Has_Discriminants (E) then
22321 Set_Stored_Constraint (E,
22322 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22324 end Set_Stored_Constraint_From_Discriminant_Constraint;
22326 -------------------------------------
22327 -- Signed_Integer_Type_Declaration --
22328 -------------------------------------
22330 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22331 Implicit_Base : Entity_Id;
22332 Base_Typ : Entity_Id;
22335 Errs : Boolean := False;
22339 function Can_Derive_From (E : Entity_Id) return Boolean;
22340 -- Determine whether given bounds allow derivation from specified type
22342 procedure Check_Bound (Expr : Node_Id);
22343 -- Check bound to make sure it is integral and static. If not, post
22344 -- appropriate error message and set Errs flag
22346 ---------------------
22347 -- Can_Derive_From --
22348 ---------------------
22350 -- Note we check both bounds against both end values, to deal with
22351 -- strange types like ones with a range of 0 .. -12341234.
22353 function Can_Derive_From (E : Entity_Id) return Boolean is
22354 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22355 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22357 return Lo <= Lo_Val and then Lo_Val <= Hi
22359 Lo <= Hi_Val and then Hi_Val <= Hi;
22360 end Can_Derive_From;
22366 procedure Check_Bound (Expr : Node_Id) is
22368 -- If a range constraint is used as an integer type definition, each
22369 -- bound of the range must be defined by a static expression of some
22370 -- integer type, but the two bounds need not have the same integer
22371 -- type (Negative bounds are allowed.) (RM 3.5.4)
22373 if not Is_Integer_Type (Etype (Expr)) then
22375 ("integer type definition bounds must be of integer type", Expr);
22378 elsif not Is_OK_Static_Expression (Expr) then
22379 Flag_Non_Static_Expr
22380 ("non-static expression used for integer type bound!", Expr);
22383 -- The bounds are folded into literals, and we set their type to be
22384 -- universal, to avoid typing difficulties: we cannot set the type
22385 -- of the literal to the new type, because this would be a forward
22386 -- reference for the back end, and if the original type is user-
22387 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
22390 if Is_Entity_Name (Expr) then
22391 Fold_Uint (Expr, Expr_Value (Expr), True);
22394 Set_Etype (Expr, Universal_Integer);
22398 -- Start of processing for Signed_Integer_Type_Declaration
22401 -- Create an anonymous base type
22404 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22406 -- Analyze and check the bounds, they can be of any integer type
22408 Lo := Low_Bound (Def);
22409 Hi := High_Bound (Def);
22411 -- Arbitrarily use Integer as the type if either bound had an error
22413 if Hi = Error or else Lo = Error then
22414 Base_Typ := Any_Integer;
22415 Set_Error_Posted (T, True);
22417 -- Here both bounds are OK expressions
22420 Analyze_And_Resolve (Lo, Any_Integer);
22421 Analyze_And_Resolve (Hi, Any_Integer);
22427 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22428 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22431 -- Find type to derive from
22433 Lo_Val := Expr_Value (Lo);
22434 Hi_Val := Expr_Value (Hi);
22436 if Can_Derive_From (Standard_Short_Short_Integer) then
22437 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22439 elsif Can_Derive_From (Standard_Short_Integer) then
22440 Base_Typ := Base_Type (Standard_Short_Integer);
22442 elsif Can_Derive_From (Standard_Integer) then
22443 Base_Typ := Base_Type (Standard_Integer);
22445 elsif Can_Derive_From (Standard_Long_Integer) then
22446 Base_Typ := Base_Type (Standard_Long_Integer);
22448 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22449 Check_Restriction (No_Long_Long_Integers, Def);
22450 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22453 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22454 Error_Msg_N ("integer type definition bounds out of range", Def);
22455 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22456 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22460 -- Complete both implicit base and declared first subtype entities. The
22461 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22462 -- are not clobbered when the signed integer type acts as a full view of
22465 Set_Etype (Implicit_Base, Base_Typ);
22466 Set_Size_Info (Implicit_Base, Base_Typ);
22467 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22468 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22469 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22471 Set_Ekind (T, E_Signed_Integer_Subtype);
22472 Set_Etype (T, Implicit_Base);
22473 Set_Size_Info (T, Implicit_Base);
22474 Inherit_Rep_Item_Chain (T, Implicit_Base);
22475 Set_Scalar_Range (T, Def);
22476 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22477 Set_Is_Constrained (T);
22478 end Signed_Integer_Type_Declaration;