1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2016, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Einfo; use Einfo;
33 with Errout; use Errout;
34 with Eval_Fat; use Eval_Fat;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dist; use Exp_Dist;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Fname; use Fname;
42 with Freeze; use Freeze;
43 with Ghost; use Ghost;
44 with Itypes; use Itypes;
45 with Layout; use Layout;
47 with Lib.Xref; use Lib.Xref;
48 with Namet; use Namet;
49 with Nmake; use Nmake;
51 with Restrict; use Restrict;
52 with Rident; use Rident;
53 with Rtsfind; use Rtsfind;
55 with Sem_Aux; use Sem_Aux;
56 with Sem_Case; use Sem_Case;
57 with Sem_Cat; use Sem_Cat;
58 with Sem_Ch6; use Sem_Ch6;
59 with Sem_Ch7; use Sem_Ch7;
60 with Sem_Ch8; use Sem_Ch8;
61 with Sem_Ch13; use Sem_Ch13;
62 with Sem_Dim; use Sem_Dim;
63 with Sem_Disp; use Sem_Disp;
64 with Sem_Dist; use Sem_Dist;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Res; use Sem_Res;
69 with Sem_Smem; use Sem_Smem;
70 with Sem_Type; use Sem_Type;
71 with Sem_Util; use Sem_Util;
72 with Sem_Warn; use Sem_Warn;
73 with Stand; use Stand;
74 with Sinfo; use Sinfo;
75 with Sinput; use Sinput;
76 with Snames; use Snames;
77 with Targparm; use Targparm;
78 with Tbuild; use Tbuild;
79 with Ttypes; use Ttypes;
80 with Uintp; use Uintp;
81 with Urealp; use Urealp;
83 package body Sem_Ch3 is
85 -----------------------
86 -- Local Subprograms --
87 -----------------------
89 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
90 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
91 -- abstract interface types implemented by a record type or a derived
94 procedure Build_Derived_Type
96 Parent_Type : Entity_Id;
97 Derived_Type : Entity_Id;
98 Is_Completion : Boolean;
99 Derive_Subps : Boolean := True);
100 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
101 -- the N_Full_Type_Declaration node containing the derived type definition.
102 -- Parent_Type is the entity for the parent type in the derived type
103 -- definition and Derived_Type the actual derived type. Is_Completion must
104 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
105 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
106 -- completion of a private type declaration. If Is_Completion is set to
107 -- True, N is the completion of a private type declaration and Derived_Type
108 -- is different from the defining identifier inside N (i.e. Derived_Type /=
109 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
110 -- subprograms should be derived. The only case where this parameter is
111 -- False is when Build_Derived_Type is recursively called to process an
112 -- implicit derived full type for a type derived from a private type (in
113 -- that case the subprograms must only be derived for the private view of
116 -- ??? These flags need a bit of re-examination and re-documentation:
117 -- ??? are they both necessary (both seem related to the recursion)?
119 procedure Build_Derived_Access_Type
121 Parent_Type : Entity_Id;
122 Derived_Type : Entity_Id);
123 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
124 -- create an implicit base if the parent type is constrained or if the
125 -- subtype indication has a constraint.
127 procedure Build_Derived_Array_Type
129 Parent_Type : Entity_Id;
130 Derived_Type : Entity_Id);
131 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
132 -- create an implicit base if the parent type is constrained or if the
133 -- subtype indication has a constraint.
135 procedure Build_Derived_Concurrent_Type
137 Parent_Type : Entity_Id;
138 Derived_Type : Entity_Id);
139 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
140 -- protected type, inherit entries and protected subprograms, check
141 -- legality of discriminant constraints if any.
143 procedure Build_Derived_Enumeration_Type
145 Parent_Type : Entity_Id;
146 Derived_Type : Entity_Id);
147 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
148 -- type, we must create a new list of literals. Types derived from
149 -- Character and [Wide_]Wide_Character are special-cased.
151 procedure Build_Derived_Numeric_Type
153 Parent_Type : Entity_Id;
154 Derived_Type : Entity_Id);
155 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
156 -- an anonymous base type, and propagate constraint to subtype if needed.
158 procedure Build_Derived_Private_Type
160 Parent_Type : Entity_Id;
161 Derived_Type : Entity_Id;
162 Is_Completion : Boolean;
163 Derive_Subps : Boolean := True);
164 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
165 -- because the parent may or may not have a completion, and the derivation
166 -- may itself be a completion.
168 procedure Build_Derived_Record_Type
170 Parent_Type : Entity_Id;
171 Derived_Type : Entity_Id;
172 Derive_Subps : Boolean := True);
173 -- Subsidiary procedure used for tagged and untagged record types
174 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
175 -- All parameters are as in Build_Derived_Type except that N, in
176 -- addition to being an N_Full_Type_Declaration node, can also be an
177 -- N_Private_Extension_Declaration node. See the definition of this routine
178 -- for much more info. Derive_Subps indicates whether subprograms should be
179 -- derived from the parent type. The only case where Derive_Subps is False
180 -- is for an implicit derived full type for a type derived from a private
181 -- type (see Build_Derived_Type).
183 procedure Build_Discriminal (Discrim : Entity_Id);
184 -- Create the discriminal corresponding to discriminant Discrim, that is
185 -- the parameter corresponding to Discrim to be used in initialization
186 -- procedures for the type where Discrim is a discriminant. Discriminals
187 -- are not used during semantic analysis, and are not fully defined
188 -- entities until expansion. Thus they are not given a scope until
189 -- initialization procedures are built.
191 function Build_Discriminant_Constraints
194 Derived_Def : Boolean := False) return Elist_Id;
195 -- Validate discriminant constraints and return the list of the constraints
196 -- in order of discriminant declarations, where T is the discriminated
197 -- unconstrained type. Def is the N_Subtype_Indication node where the
198 -- discriminants constraints for T are specified. Derived_Def is True
199 -- when building the discriminant constraints in a derived type definition
200 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
201 -- type and Def is the constraint "(xxx)" on T and this routine sets the
202 -- Corresponding_Discriminant field of the discriminants in the derived
203 -- type D to point to the corresponding discriminants in the parent type T.
205 procedure Build_Discriminated_Subtype
209 Related_Nod : Node_Id;
210 For_Access : Boolean := False);
211 -- Subsidiary procedure to Constrain_Discriminated_Type and to
212 -- Process_Incomplete_Dependents. Given
214 -- T (a possibly discriminated base type)
215 -- Def_Id (a very partially built subtype for T),
217 -- the call completes Def_Id to be the appropriate E_*_Subtype.
219 -- The Elist is the list of discriminant constraints if any (it is set
220 -- to No_Elist if T is not a discriminated type, and to an empty list if
221 -- T has discriminants but there are no discriminant constraints). The
222 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
223 -- The For_Access says whether or not this subtype is really constraining
224 -- an access type. That is its sole purpose is the designated type of an
225 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
226 -- is built to avoid freezing T when the access subtype is frozen.
228 function Build_Scalar_Bound
231 Der_T : Entity_Id) return Node_Id;
232 -- The bounds of a derived scalar type are conversions of the bounds of
233 -- the parent type. Optimize the representation if the bounds are literals.
234 -- Needs a more complete spec--what are the parameters exactly, and what
235 -- exactly is the returned value, and how is Bound affected???
237 procedure Build_Underlying_Full_View
241 -- If the completion of a private type is itself derived from a private
242 -- type, or if the full view of a private subtype is itself private, the
243 -- back-end has no way to compute the actual size of this type. We build
244 -- an internal subtype declaration of the proper parent type to convey
245 -- this information. This extra mechanism is needed because a full
246 -- view cannot itself have a full view (it would get clobbered during
249 procedure Check_Access_Discriminant_Requires_Limited
252 -- Check the restriction that the type to which an access discriminant
253 -- belongs must be a concurrent type or a descendant of a type with
254 -- the reserved word 'limited' in its declaration.
256 procedure Check_Anonymous_Access_Components
260 Comp_List : Node_Id);
261 -- Ada 2005 AI-382: an access component in a record definition can refer to
262 -- the enclosing record, in which case it denotes the type itself, and not
263 -- the current instance of the type. We create an anonymous access type for
264 -- the component, and flag it as an access to a component, so accessibility
265 -- checks are properly performed on it. The declaration of the access type
266 -- is placed ahead of that of the record to prevent order-of-elaboration
267 -- circularity issues in Gigi. We create an incomplete type for the record
268 -- declaration, which is the designated type of the anonymous access.
270 procedure Check_Delta_Expression (E : Node_Id);
271 -- Check that the expression represented by E is suitable for use as a
272 -- delta expression, i.e. it is of real type and is static.
274 procedure Check_Digits_Expression (E : Node_Id);
275 -- Check that the expression represented by E is suitable for use as a
276 -- digits expression, i.e. it is of integer type, positive and static.
278 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
279 -- Validate the initialization of an object declaration. T is the required
280 -- type, and Exp is the initialization expression.
282 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
283 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
285 procedure Check_Or_Process_Discriminants
288 Prev : Entity_Id := Empty);
289 -- If N is the full declaration of the completion T of an incomplete or
290 -- private type, check its discriminants (which are already known to be
291 -- conformant with those of the partial view, see Find_Type_Name),
292 -- otherwise process them. Prev is the entity of the partial declaration,
295 procedure Check_Real_Bound (Bound : Node_Id);
296 -- Check given bound for being of real type and static. If not, post an
297 -- appropriate message, and rewrite the bound with the real literal zero.
299 procedure Constant_Redeclaration
303 -- Various checks on legality of full declaration of deferred constant.
304 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
305 -- node. The caller has not yet set any attributes of this entity.
307 function Contain_Interface
309 Ifaces : Elist_Id) return Boolean;
310 -- Ada 2005: Determine whether Iface is present in the list Ifaces
312 procedure Convert_Scalar_Bounds
314 Parent_Type : Entity_Id;
315 Derived_Type : Entity_Id;
317 -- For derived scalar types, convert the bounds in the type definition to
318 -- the derived type, and complete their analysis. Given a constraint of the
319 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
320 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
321 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
322 -- subtype are conversions of those bounds to the derived_type, so that
323 -- their typing is consistent.
325 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
326 -- Copies attributes from array base type T2 to array base type T1. Copies
327 -- only attributes that apply to base types, but not subtypes.
329 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
330 -- Copies attributes from array subtype T2 to array subtype T1. Copies
331 -- attributes that apply to both subtypes and base types.
333 procedure Create_Constrained_Components
337 Constraints : Elist_Id);
338 -- Build the list of entities for a constrained discriminated record
339 -- subtype. If a component depends on a discriminant, replace its subtype
340 -- using the discriminant values in the discriminant constraint. Subt
341 -- is the defining identifier for the subtype whose list of constrained
342 -- entities we will create. Decl_Node is the type declaration node where
343 -- we will attach all the itypes created. Typ is the base discriminated
344 -- type for the subtype Subt. Constraints is the list of discriminant
345 -- constraints for Typ.
347 function Constrain_Component_Type
349 Constrained_Typ : Entity_Id;
350 Related_Node : Node_Id;
352 Constraints : Elist_Id) return Entity_Id;
353 -- Given a discriminated base type Typ, a list of discriminant constraints,
354 -- Constraints, for Typ and a component Comp of Typ, create and return the
355 -- type corresponding to Etype (Comp) where all discriminant references
356 -- are replaced with the corresponding constraint. If Etype (Comp) contains
357 -- no discriminant references then it is returned as-is. Constrained_Typ
358 -- is the final constrained subtype to which the constrained component
359 -- belongs. Related_Node is the node where we attach all created itypes.
361 procedure Constrain_Access
362 (Def_Id : in out Entity_Id;
364 Related_Nod : Node_Id);
365 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
366 -- an anonymous type created for a subtype indication. In that case it is
367 -- created in the procedure and attached to Related_Nod.
369 procedure Constrain_Array
370 (Def_Id : in out Entity_Id;
372 Related_Nod : Node_Id;
373 Related_Id : Entity_Id;
375 -- Apply a list of index constraints to an unconstrained array type. The
376 -- first parameter is the entity for the resulting subtype. A value of
377 -- Empty for Def_Id indicates that an implicit type must be created, but
378 -- creation is delayed (and must be done by this procedure) because other
379 -- subsidiary implicit types must be created first (which is why Def_Id
380 -- is an in/out parameter). The second parameter is a subtype indication
381 -- node for the constrained array to be created (e.g. something of the
382 -- form string (1 .. 10)). Related_Nod gives the place where this type
383 -- has to be inserted in the tree. The Related_Id and Suffix parameters
384 -- are used to build the associated Implicit type name.
386 procedure Constrain_Concurrent
387 (Def_Id : in out Entity_Id;
389 Related_Nod : Node_Id;
390 Related_Id : Entity_Id;
392 -- Apply list of discriminant constraints to an unconstrained concurrent
395 -- SI is the N_Subtype_Indication node containing the constraint and
396 -- the unconstrained type to constrain.
398 -- Def_Id is the entity for the resulting constrained subtype. A value
399 -- of Empty for Def_Id indicates that an implicit type must be created,
400 -- but creation is delayed (and must be done by this procedure) because
401 -- other subsidiary implicit types must be created first (which is why
402 -- Def_Id is an in/out parameter).
404 -- Related_Nod gives the place where this type has to be inserted
407 -- The last two arguments are used to create its external name if needed.
409 function Constrain_Corresponding_Record
410 (Prot_Subt : Entity_Id;
411 Corr_Rec : Entity_Id;
412 Related_Nod : Node_Id) return Entity_Id;
413 -- When constraining a protected type or task type with discriminants,
414 -- constrain the corresponding record with the same discriminant values.
416 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
417 -- Constrain a decimal fixed point type with a digits constraint and/or a
418 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
420 procedure Constrain_Discriminated_Type
423 Related_Nod : Node_Id;
424 For_Access : Boolean := False);
425 -- Process discriminant constraints of composite type. Verify that values
426 -- have been provided for all discriminants, that the original type is
427 -- unconstrained, and that the types of the supplied expressions match
428 -- the discriminant types. The first three parameters are like in routine
429 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
432 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
433 -- Constrain an enumeration type with a range constraint. This is identical
434 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
436 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
437 -- Constrain a floating point type with either a digits constraint
438 -- and/or a range constraint, building a E_Floating_Point_Subtype.
440 procedure Constrain_Index
443 Related_Nod : Node_Id;
444 Related_Id : Entity_Id;
447 -- Process an index constraint S in a constrained array declaration. The
448 -- constraint can be a subtype name, or a range with or without an explicit
449 -- subtype mark. The index is the corresponding index of the unconstrained
450 -- array. The Related_Id and Suffix parameters are used to build the
451 -- associated Implicit type name.
453 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
454 -- Build subtype of a signed or modular integer type
456 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
457 -- Constrain an ordinary fixed point type with a range constraint, and
458 -- build an E_Ordinary_Fixed_Point_Subtype entity.
460 procedure Copy_And_Swap (Priv, Full : Entity_Id);
461 -- Copy the Priv entity into the entity of its full declaration then swap
462 -- the two entities in such a manner that the former private type is now
463 -- seen as a full type.
465 procedure Decimal_Fixed_Point_Type_Declaration
468 -- Create a new decimal fixed point type, and apply the constraint to
469 -- obtain a subtype of this new type.
471 procedure Complete_Private_Subtype
474 Full_Base : Entity_Id;
475 Related_Nod : Node_Id);
476 -- Complete the implicit full view of a private subtype by setting the
477 -- appropriate semantic fields. If the full view of the parent is a record
478 -- type, build constrained components of subtype.
480 procedure Derive_Progenitor_Subprograms
481 (Parent_Type : Entity_Id;
482 Tagged_Type : Entity_Id);
483 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
484 -- operations of progenitors of Tagged_Type, and replace the subsidiary
485 -- subtypes with Tagged_Type, to build the specs of the inherited interface
486 -- primitives. The derived primitives are aliased to those of the
487 -- interface. This routine takes care also of transferring to the full view
488 -- subprograms associated with the partial view of Tagged_Type that cover
489 -- interface primitives.
491 procedure Derived_Standard_Character
493 Parent_Type : Entity_Id;
494 Derived_Type : Entity_Id);
495 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
496 -- derivations from types Standard.Character and Standard.Wide_Character.
498 procedure Derived_Type_Declaration
501 Is_Completion : Boolean);
502 -- Process a derived type declaration. Build_Derived_Type is invoked
503 -- to process the actual derived type definition. Parameters N and
504 -- Is_Completion have the same meaning as in Build_Derived_Type.
505 -- T is the N_Defining_Identifier for the entity defined in the
506 -- N_Full_Type_Declaration node N, that is T is the derived type.
508 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
509 -- Insert each literal in symbol table, as an overloadable identifier. Each
510 -- enumeration type is mapped into a sequence of integers, and each literal
511 -- is defined as a constant with integer value. If any of the literals are
512 -- character literals, the type is a character type, which means that
513 -- strings are legal aggregates for arrays of components of the type.
515 function Expand_To_Stored_Constraint
517 Constraint : Elist_Id) return Elist_Id;
518 -- Given a constraint (i.e. a list of expressions) on the discriminants of
519 -- Typ, expand it into a constraint on the stored discriminants and return
520 -- the new list of expressions constraining the stored discriminants.
522 function Find_Type_Of_Object
524 Related_Nod : Node_Id) return Entity_Id;
525 -- Get type entity for object referenced by Obj_Def, attaching the implicit
526 -- types generated to Related_Nod.
528 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
529 -- Create a new float and apply the constraint to obtain subtype of it
531 function Has_Range_Constraint (N : Node_Id) return Boolean;
532 -- Given an N_Subtype_Indication node N, return True if a range constraint
533 -- is present, either directly, or as part of a digits or delta constraint.
534 -- In addition, a digits constraint in the decimal case returns True, since
535 -- it establishes a default range if no explicit range is present.
537 function Inherit_Components
539 Parent_Base : Entity_Id;
540 Derived_Base : Entity_Id;
542 Inherit_Discr : Boolean;
543 Discs : Elist_Id) return Elist_Id;
544 -- Called from Build_Derived_Record_Type to inherit the components of
545 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
546 -- For more information on derived types and component inheritance please
547 -- consult the comment above the body of Build_Derived_Record_Type.
549 -- N is the original derived type declaration
551 -- Is_Tagged is set if we are dealing with tagged types
553 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
554 -- Parent_Base, otherwise no discriminants are inherited.
556 -- Discs gives the list of constraints that apply to Parent_Base in the
557 -- derived type declaration. If Discs is set to No_Elist, then we have
558 -- the following situation:
560 -- type Parent (D1..Dn : ..) is [tagged] record ...;
561 -- type Derived is new Parent [with ...];
563 -- which gets treated as
565 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
567 -- For untagged types the returned value is an association list. The list
568 -- starts from the association (Parent_Base => Derived_Base), and then it
569 -- contains a sequence of the associations of the form
571 -- (Old_Component => New_Component),
573 -- where Old_Component is the Entity_Id of a component in Parent_Base and
574 -- New_Component is the Entity_Id of the corresponding component in
575 -- Derived_Base. For untagged records, this association list is needed when
576 -- copying the record declaration for the derived base. In the tagged case
577 -- the value returned is irrelevant.
579 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
580 -- Propagate static and dynamic predicate flags from a parent to the
581 -- subtype in a subtype declaration with and without constraints.
583 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
584 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
585 -- Determine whether subprogram Subp is a procedure subject to pragma
586 -- Extensions_Visible with value False and has at least one controlling
587 -- parameter of mode OUT.
589 function Is_Valid_Constraint_Kind
591 Constraint_Kind : Node_Kind) return Boolean;
592 -- Returns True if it is legal to apply the given kind of constraint to the
593 -- given kind of type (index constraint to an array type, for example).
595 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
596 -- Create new modular type. Verify that modulus is in bounds
598 procedure New_Concatenation_Op (Typ : Entity_Id);
599 -- Create an abbreviated declaration for an operator in order to
600 -- materialize concatenation on array types.
602 procedure Ordinary_Fixed_Point_Type_Declaration
605 -- Create a new ordinary fixed point type, and apply the constraint to
606 -- obtain subtype of it.
608 procedure Prepare_Private_Subtype_Completion
610 Related_Nod : Node_Id);
611 -- Id is a subtype of some private type. Creates the full declaration
612 -- associated with Id whenever possible, i.e. when the full declaration
613 -- of the base type is already known. Records each subtype into
614 -- Private_Dependents of the base type.
616 procedure Process_Incomplete_Dependents
620 -- Process all entities that depend on an incomplete type. There include
621 -- subtypes, subprogram types that mention the incomplete type in their
622 -- profiles, and subprogram with access parameters that designate the
625 -- Inc_T is the defining identifier of an incomplete type declaration, its
626 -- Ekind is E_Incomplete_Type.
628 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
630 -- Full_T is N's defining identifier.
632 -- Subtypes of incomplete types with discriminants are completed when the
633 -- parent type is. This is simpler than private subtypes, because they can
634 -- only appear in the same scope, and there is no need to exchange views.
635 -- Similarly, access_to_subprogram types may have a parameter or a return
636 -- type that is an incomplete type, and that must be replaced with the
639 -- If the full type is tagged, subprogram with access parameters that
640 -- designated the incomplete may be primitive operations of the full type,
641 -- and have to be processed accordingly.
643 procedure Process_Real_Range_Specification (Def : Node_Id);
644 -- Given the type definition for a real type, this procedure processes and
645 -- checks the real range specification of this type definition if one is
646 -- present. If errors are found, error messages are posted, and the
647 -- Real_Range_Specification of Def is reset to Empty.
649 procedure Record_Type_Declaration
653 -- Process a record type declaration (for both untagged and tagged
654 -- records). Parameters T and N are exactly like in procedure
655 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
656 -- for this routine. If this is the completion of an incomplete type
657 -- declaration, Prev is the entity of the incomplete declaration, used for
658 -- cross-referencing. Otherwise Prev = T.
660 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
661 -- This routine is used to process the actual record type definition (both
662 -- for untagged and tagged records). Def is a record type definition node.
663 -- This procedure analyzes the components in this record type definition.
664 -- Prev_T is the entity for the enclosing record type. It is provided so
665 -- that its Has_Task flag can be set if any of the component have Has_Task
666 -- set. If the declaration is the completion of an incomplete type
667 -- declaration, Prev_T is the original incomplete type, whose full view is
670 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
671 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
672 -- build a copy of the declaration tree of the parent, and we create
673 -- independently the list of components for the derived type. Semantic
674 -- information uses the component entities, but record representation
675 -- clauses are validated on the declaration tree. This procedure replaces
676 -- discriminants and components in the declaration with those that have
677 -- been created by Inherit_Components.
679 procedure Set_Fixed_Range
684 -- Build a range node with the given bounds and set it as the Scalar_Range
685 -- of the given fixed-point type entity. Loc is the source location used
686 -- for the constructed range. See body for further details.
688 procedure Set_Scalar_Range_For_Subtype
692 -- This routine is used to set the scalar range field for a subtype given
693 -- Def_Id, the entity for the subtype, and R, the range expression for the
694 -- scalar range. Subt provides the parent subtype to be used to analyze,
695 -- resolve, and check the given range.
697 procedure Set_Default_SSO (T : Entity_Id);
698 -- T is the entity for an array or record being declared. This procedure
699 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
700 -- to the setting of Opt.Default_SSO.
702 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
703 -- Create a new signed integer entity, and apply the constraint to obtain
704 -- the required first named subtype of this type.
706 procedure Set_Stored_Constraint_From_Discriminant_Constraint
708 -- E is some record type. This routine computes E's Stored_Constraint
709 -- from its Discriminant_Constraint.
711 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
712 -- Check that an entity in a list of progenitors is an interface,
713 -- emit error otherwise.
715 -----------------------
716 -- Access_Definition --
717 -----------------------
719 function Access_Definition
720 (Related_Nod : Node_Id;
721 N : Node_Id) return Entity_Id
723 Anon_Type : Entity_Id;
724 Anon_Scope : Entity_Id;
725 Desig_Type : Entity_Id;
726 Enclosing_Prot_Type : Entity_Id := Empty;
729 Check_SPARK_05_Restriction ("access type is not allowed", N);
731 if Is_Entry (Current_Scope)
732 and then Is_Task_Type (Etype (Scope (Current_Scope)))
734 Error_Msg_N ("task entries cannot have access parameters", N);
738 -- Ada 2005: For an object declaration the corresponding anonymous
739 -- type is declared in the current scope.
741 -- If the access definition is the return type of another access to
742 -- function, scope is the current one, because it is the one of the
743 -- current type declaration, except for the pathological case below.
745 if Nkind_In (Related_Nod, N_Object_Declaration,
746 N_Access_Function_Definition)
748 Anon_Scope := Current_Scope;
750 -- A pathological case: function returning access functions that
751 -- return access functions, etc. Each anonymous access type created
752 -- is in the enclosing scope of the outermost function.
759 while Nkind_In (Par, N_Access_Function_Definition,
765 if Nkind (Par) = N_Function_Specification then
766 Anon_Scope := Scope (Defining_Entity (Par));
770 -- For the anonymous function result case, retrieve the scope of the
771 -- function specification's associated entity rather than using the
772 -- current scope. The current scope will be the function itself if the
773 -- formal part is currently being analyzed, but will be the parent scope
774 -- in the case of a parameterless function, and we always want to use
775 -- the function's parent scope. Finally, if the function is a child
776 -- unit, we must traverse the tree to retrieve the proper entity.
778 elsif Nkind (Related_Nod) = N_Function_Specification
779 and then Nkind (Parent (N)) /= N_Parameter_Specification
781 -- If the current scope is a protected type, the anonymous access
782 -- is associated with one of the protected operations, and must
783 -- be available in the scope that encloses the protected declaration.
784 -- Otherwise the type is in the scope enclosing the subprogram.
786 -- If the function has formals, The return type of a subprogram
787 -- declaration is analyzed in the scope of the subprogram (see
788 -- Process_Formals) and thus the protected type, if present, is
789 -- the scope of the current function scope.
791 if Ekind (Current_Scope) = E_Protected_Type then
792 Enclosing_Prot_Type := Current_Scope;
794 elsif Ekind (Current_Scope) = E_Function
795 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
797 Enclosing_Prot_Type := Scope (Current_Scope);
800 if Present (Enclosing_Prot_Type) then
801 Anon_Scope := Scope (Enclosing_Prot_Type);
804 Anon_Scope := Scope (Defining_Entity (Related_Nod));
807 -- For an access type definition, if the current scope is a child
808 -- unit it is the scope of the type.
810 elsif Is_Compilation_Unit (Current_Scope) then
811 Anon_Scope := Current_Scope;
813 -- For access formals, access components, and access discriminants, the
814 -- scope is that of the enclosing declaration,
817 Anon_Scope := Scope (Current_Scope);
822 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
825 and then Ada_Version >= Ada_2005
827 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
830 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
831 -- the corresponding semantic routine
833 if Present (Access_To_Subprogram_Definition (N)) then
835 -- Compiler runtime units are compiled in Ada 2005 mode when building
836 -- the runtime library but must also be compilable in Ada 95 mode
837 -- (when bootstrapping the compiler).
839 Check_Compiler_Unit ("anonymous access to subprogram", N);
841 Access_Subprogram_Declaration
842 (T_Name => Anon_Type,
843 T_Def => Access_To_Subprogram_Definition (N));
845 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
847 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
849 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
852 Set_Can_Use_Internal_Rep
853 (Anon_Type, not Always_Compatible_Rep_On_Target);
855 -- If the anonymous access is associated with a protected operation,
856 -- create a reference to it after the enclosing protected definition
857 -- because the itype will be used in the subsequent bodies.
859 -- If the anonymous access itself is protected, a full type
860 -- declaratiton will be created for it, so that the equivalent
861 -- record type can be constructed. For further details, see
862 -- Replace_Anonymous_Access_To_Protected-Subprogram.
864 if Ekind (Current_Scope) = E_Protected_Type
865 and then not Protected_Present (Access_To_Subprogram_Definition (N))
867 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
873 Find_Type (Subtype_Mark (N));
874 Desig_Type := Entity (Subtype_Mark (N));
876 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
877 Set_Etype (Anon_Type, Anon_Type);
879 -- Make sure the anonymous access type has size and alignment fields
880 -- set, as required by gigi. This is necessary in the case of the
881 -- Task_Body_Procedure.
883 if not Has_Private_Component (Desig_Type) then
884 Layout_Type (Anon_Type);
887 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
888 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
889 -- the null value is allowed. In Ada 95 the null value is never allowed.
891 if Ada_Version >= Ada_2005 then
892 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
894 Set_Can_Never_Be_Null (Anon_Type, True);
897 -- The anonymous access type is as public as the discriminated type or
898 -- subprogram that defines it. It is imported (for back-end purposes)
899 -- if the designated type is.
901 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
903 -- Ada 2005 (AI-231): Propagate the access-constant attribute
905 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
907 -- The context is either a subprogram declaration, object declaration,
908 -- or an access discriminant, in a private or a full type declaration.
909 -- In the case of a subprogram, if the designated type is incomplete,
910 -- the operation will be a primitive operation of the full type, to be
911 -- updated subsequently. If the type is imported through a limited_with
912 -- clause, the subprogram is not a primitive operation of the type
913 -- (which is declared elsewhere in some other scope).
915 if Ekind (Desig_Type) = E_Incomplete_Type
916 and then not From_Limited_With (Desig_Type)
917 and then Is_Overloadable (Current_Scope)
919 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
920 Set_Has_Delayed_Freeze (Current_Scope);
923 -- Ada 2005: If the designated type is an interface that may contain
924 -- tasks, create a Master entity for the declaration. This must be done
925 -- before expansion of the full declaration, because the declaration may
926 -- include an expression that is an allocator, whose expansion needs the
927 -- proper Master for the created tasks.
929 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
931 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
933 Build_Class_Wide_Master (Anon_Type);
935 -- Similarly, if the type is an anonymous access that designates
936 -- tasks, create a master entity for it in the current context.
938 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
940 Build_Master_Entity (Defining_Identifier (Related_Nod));
941 Build_Master_Renaming (Anon_Type);
945 -- For a private component of a protected type, it is imperative that
946 -- the back-end elaborate the type immediately after the protected
947 -- declaration, because this type will be used in the declarations
948 -- created for the component within each protected body, so we must
949 -- create an itype reference for it now.
951 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
952 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
954 -- Similarly, if the access definition is the return result of a
955 -- function, create an itype reference for it because it will be used
956 -- within the function body. For a regular function that is not a
957 -- compilation unit, insert reference after the declaration. For a
958 -- protected operation, insert it after the enclosing protected type
959 -- declaration. In either case, do not create a reference for a type
960 -- obtained through a limited_with clause, because this would introduce
961 -- semantic dependencies.
963 -- Similarly, do not create a reference if the designated type is a
964 -- generic formal, because no use of it will reach the backend.
966 elsif Nkind (Related_Nod) = N_Function_Specification
967 and then not From_Limited_With (Desig_Type)
968 and then not Is_Generic_Type (Desig_Type)
970 if Present (Enclosing_Prot_Type) then
971 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
973 elsif Is_List_Member (Parent (Related_Nod))
974 and then Nkind (Parent (N)) /= N_Parameter_Specification
976 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
979 -- Finally, create an itype reference for an object declaration of an
980 -- anonymous access type. This is strictly necessary only for deferred
981 -- constants, but in any case will avoid out-of-scope problems in the
984 elsif Nkind (Related_Nod) = N_Object_Declaration then
985 Build_Itype_Reference (Anon_Type, Related_Nod);
989 end Access_Definition;
991 -----------------------------------
992 -- Access_Subprogram_Declaration --
993 -----------------------------------
995 procedure Access_Subprogram_Declaration
999 procedure Check_For_Premature_Usage (Def : Node_Id);
1000 -- Check that type T_Name is not used, directly or recursively, as a
1001 -- parameter or a return type in Def. Def is either a subtype, an
1002 -- access_definition, or an access_to_subprogram_definition.
1004 -------------------------------
1005 -- Check_For_Premature_Usage --
1006 -------------------------------
1008 procedure Check_For_Premature_Usage (Def : Node_Id) is
1012 -- Check for a subtype mark
1014 if Nkind (Def) in N_Has_Etype then
1015 if Etype (Def) = T_Name then
1017 ("type& cannot be used before end of its declaration", Def);
1020 -- If this is not a subtype, then this is an access_definition
1022 elsif Nkind (Def) = N_Access_Definition then
1023 if Present (Access_To_Subprogram_Definition (Def)) then
1024 Check_For_Premature_Usage
1025 (Access_To_Subprogram_Definition (Def));
1027 Check_For_Premature_Usage (Subtype_Mark (Def));
1030 -- The only cases left are N_Access_Function_Definition and
1031 -- N_Access_Procedure_Definition.
1034 if Present (Parameter_Specifications (Def)) then
1035 Param := First (Parameter_Specifications (Def));
1036 while Present (Param) loop
1037 Check_For_Premature_Usage (Parameter_Type (Param));
1038 Param := Next (Param);
1042 if Nkind (Def) = N_Access_Function_Definition then
1043 Check_For_Premature_Usage (Result_Definition (Def));
1046 end Check_For_Premature_Usage;
1050 Formals : constant List_Id := Parameter_Specifications (T_Def);
1053 Desig_Type : constant Entity_Id :=
1054 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1056 -- Start of processing for Access_Subprogram_Declaration
1059 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1061 -- Associate the Itype node with the inner full-type declaration or
1062 -- subprogram spec or entry body. This is required to handle nested
1063 -- anonymous declarations. For example:
1066 -- (X : access procedure
1067 -- (Y : access procedure
1070 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1071 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1072 N_Private_Type_Declaration,
1073 N_Private_Extension_Declaration,
1074 N_Procedure_Specification,
1075 N_Function_Specification,
1079 Nkind_In (D_Ityp, N_Object_Declaration,
1080 N_Object_Renaming_Declaration,
1081 N_Formal_Object_Declaration,
1082 N_Formal_Type_Declaration,
1083 N_Task_Type_Declaration,
1084 N_Protected_Type_Declaration))
1086 D_Ityp := Parent (D_Ityp);
1087 pragma Assert (D_Ityp /= Empty);
1090 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1092 if Nkind_In (D_Ityp, N_Procedure_Specification,
1093 N_Function_Specification)
1095 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1097 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1098 N_Object_Declaration,
1099 N_Object_Renaming_Declaration,
1100 N_Formal_Type_Declaration)
1102 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1105 if Nkind (T_Def) = N_Access_Function_Definition then
1106 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1108 Acc : constant Node_Id := Result_Definition (T_Def);
1111 if Present (Access_To_Subprogram_Definition (Acc))
1113 Protected_Present (Access_To_Subprogram_Definition (Acc))
1117 Replace_Anonymous_Access_To_Protected_Subprogram
1123 Access_Definition (T_Def, Result_Definition (T_Def)));
1128 Analyze (Result_Definition (T_Def));
1131 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1134 -- If a null exclusion is imposed on the result type, then
1135 -- create a null-excluding itype (an access subtype) and use
1136 -- it as the function's Etype.
1138 if Is_Access_Type (Typ)
1139 and then Null_Exclusion_In_Return_Present (T_Def)
1141 Set_Etype (Desig_Type,
1142 Create_Null_Excluding_Itype
1144 Related_Nod => T_Def,
1145 Scope_Id => Current_Scope));
1148 if From_Limited_With (Typ) then
1150 -- AI05-151: Incomplete types are allowed in all basic
1151 -- declarations, including access to subprograms.
1153 if Ada_Version >= Ada_2012 then
1158 ("illegal use of incomplete type&",
1159 Result_Definition (T_Def), Typ);
1162 elsif Ekind (Current_Scope) = E_Package
1163 and then In_Private_Part (Current_Scope)
1165 if Ekind (Typ) = E_Incomplete_Type then
1166 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1168 elsif Is_Class_Wide_Type (Typ)
1169 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1172 (Desig_Type, Private_Dependents (Etype (Typ)));
1176 Set_Etype (Desig_Type, Typ);
1181 if not (Is_Type (Etype (Desig_Type))) then
1183 ("expect type in function specification",
1184 Result_Definition (T_Def));
1188 Set_Etype (Desig_Type, Standard_Void_Type);
1191 if Present (Formals) then
1192 Push_Scope (Desig_Type);
1194 -- Some special tests here. These special tests can be removed
1195 -- if and when Itypes always have proper parent pointers to their
1198 -- Special test 1) Link defining_identifier of formals. Required by
1199 -- First_Formal to provide its functionality.
1205 F := First (Formals);
1207 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1208 -- when it is part of an unconstrained type and subtype expansion
1209 -- is disabled. To avoid back-end problems with shared profiles,
1210 -- use previous subprogram type as the designated type, and then
1211 -- remove scope added above.
1213 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1215 Set_Etype (T_Name, T_Name);
1216 Init_Size_Align (T_Name);
1217 Set_Directly_Designated_Type (T_Name,
1218 Scope (Defining_Identifier (F)));
1223 while Present (F) loop
1224 if No (Parent (Defining_Identifier (F))) then
1225 Set_Parent (Defining_Identifier (F), F);
1232 Process_Formals (Formals, Parent (T_Def));
1234 -- Special test 2) End_Scope requires that the parent pointer be set
1235 -- to something reasonable, but Itypes don't have parent pointers. So
1236 -- we set it and then unset it ???
1238 Set_Parent (Desig_Type, T_Name);
1240 Set_Parent (Desig_Type, Empty);
1243 -- Check for premature usage of the type being defined
1245 Check_For_Premature_Usage (T_Def);
1247 -- The return type and/or any parameter type may be incomplete. Mark the
1248 -- subprogram_type as depending on the incomplete type, so that it can
1249 -- be updated when the full type declaration is seen. This only applies
1250 -- to incomplete types declared in some enclosing scope, not to limited
1251 -- views from other packages.
1253 -- Prior to Ada 2012, access to functions can only have in_parameters.
1255 if Present (Formals) then
1256 Formal := First_Formal (Desig_Type);
1257 while Present (Formal) loop
1258 if Ekind (Formal) /= E_In_Parameter
1259 and then Nkind (T_Def) = N_Access_Function_Definition
1260 and then Ada_Version < Ada_2012
1262 Error_Msg_N ("functions can only have IN parameters", Formal);
1265 if Ekind (Etype (Formal)) = E_Incomplete_Type
1266 and then In_Open_Scopes (Scope (Etype (Formal)))
1268 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1269 Set_Has_Delayed_Freeze (Desig_Type);
1272 Next_Formal (Formal);
1276 -- Check whether an indirect call without actuals may be possible. This
1277 -- is used when resolving calls whose result is then indexed.
1279 May_Need_Actuals (Desig_Type);
1281 -- If the return type is incomplete, this is legal as long as the type
1282 -- is declared in the current scope and will be completed in it (rather
1283 -- than being part of limited view).
1285 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1286 and then not Has_Delayed_Freeze (Desig_Type)
1287 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1289 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1290 Set_Has_Delayed_Freeze (Desig_Type);
1293 Check_Delayed_Subprogram (Desig_Type);
1295 if Protected_Present (T_Def) then
1296 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1297 Set_Convention (Desig_Type, Convention_Protected);
1299 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1302 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1304 Set_Etype (T_Name, T_Name);
1305 Init_Size_Align (T_Name);
1306 Set_Directly_Designated_Type (T_Name, Desig_Type);
1308 Generate_Reference_To_Formals (T_Name);
1310 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1312 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1314 Check_Restriction (No_Access_Subprograms, T_Def);
1315 end Access_Subprogram_Declaration;
1317 ----------------------------
1318 -- Access_Type_Declaration --
1319 ----------------------------
1321 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1322 P : constant Node_Id := Parent (Def);
1323 S : constant Node_Id := Subtype_Indication (Def);
1325 Full_Desig : Entity_Id;
1328 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1330 -- Check for permissible use of incomplete type
1332 if Nkind (S) /= N_Subtype_Indication then
1335 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1336 Set_Directly_Designated_Type (T, Entity (S));
1338 -- If the designated type is a limited view, we cannot tell if
1339 -- the full view contains tasks, and there is no way to handle
1340 -- that full view in a client. We create a master entity for the
1341 -- scope, which will be used when a client determines that one
1344 if From_Limited_With (Entity (S))
1345 and then not Is_Class_Wide_Type (Entity (S))
1347 Set_Ekind (T, E_Access_Type);
1348 Build_Master_Entity (T);
1349 Build_Master_Renaming (T);
1353 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1356 -- If the access definition is of the form: ACCESS NOT NULL ..
1357 -- the subtype indication must be of an access type. Create
1358 -- a null-excluding subtype of it.
1360 if Null_Excluding_Subtype (Def) then
1361 if not Is_Access_Type (Entity (S)) then
1362 Error_Msg_N ("null exclusion must apply to access type", Def);
1366 Loc : constant Source_Ptr := Sloc (S);
1368 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1372 Make_Subtype_Declaration (Loc,
1373 Defining_Identifier => Nam,
1374 Subtype_Indication =>
1375 New_Occurrence_Of (Entity (S), Loc));
1376 Set_Null_Exclusion_Present (Decl);
1377 Insert_Before (Parent (Def), Decl);
1379 Set_Entity (S, Nam);
1385 Set_Directly_Designated_Type (T,
1386 Process_Subtype (S, P, T, 'P'));
1389 if All_Present (Def) or Constant_Present (Def) then
1390 Set_Ekind (T, E_General_Access_Type);
1392 Set_Ekind (T, E_Access_Type);
1395 Full_Desig := Designated_Type (T);
1397 if Base_Type (Full_Desig) = T then
1398 Error_Msg_N ("access type cannot designate itself", S);
1400 -- In Ada 2005, the type may have a limited view through some unit in
1401 -- its own context, allowing the following circularity that cannot be
1402 -- detected earlier.
1404 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1407 ("access type cannot designate its own class-wide type", S);
1409 -- Clean up indication of tagged status to prevent cascaded errors
1411 Set_Is_Tagged_Type (T, False);
1416 -- If the type has appeared already in a with_type clause, it is frozen
1417 -- and the pointer size is already set. Else, initialize.
1419 if not From_Limited_With (T) then
1420 Init_Size_Align (T);
1423 -- Note that Has_Task is always false, since the access type itself
1424 -- is not a task type. See Einfo for more description on this point.
1425 -- Exactly the same consideration applies to Has_Controlled_Component
1426 -- and to Has_Protected.
1428 Set_Has_Task (T, False);
1429 Set_Has_Protected (T, False);
1430 Set_Has_Timing_Event (T, False);
1431 Set_Has_Controlled_Component (T, False);
1433 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1434 -- problems where an incomplete view of this entity has been previously
1435 -- established by a limited with and an overlaid version of this field
1436 -- (Stored_Constraint) was initialized for the incomplete view.
1438 -- This reset is performed in most cases except where the access type
1439 -- has been created for the purposes of allocating or deallocating a
1440 -- build-in-place object. Such access types have explicitly set pools
1441 -- and finalization masters.
1443 if No (Associated_Storage_Pool (T)) then
1444 Set_Finalization_Master (T, Empty);
1447 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1450 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1451 Set_Is_Access_Constant (T, Constant_Present (Def));
1452 end Access_Type_Declaration;
1454 ----------------------------------
1455 -- Add_Interface_Tag_Components --
1456 ----------------------------------
1458 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1459 Loc : constant Source_Ptr := Sloc (N);
1463 procedure Add_Tag (Iface : Entity_Id);
1464 -- Add tag for one of the progenitor interfaces
1470 procedure Add_Tag (Iface : Entity_Id) is
1477 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1479 -- This is a reasonable place to propagate predicates
1481 if Has_Predicates (Iface) then
1482 Set_Has_Predicates (Typ);
1486 Make_Component_Definition (Loc,
1487 Aliased_Present => True,
1488 Subtype_Indication =>
1489 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1491 Tag := Make_Temporary (Loc, 'V');
1494 Make_Component_Declaration (Loc,
1495 Defining_Identifier => Tag,
1496 Component_Definition => Def);
1498 Analyze_Component_Declaration (Decl);
1500 Set_Analyzed (Decl);
1501 Set_Ekind (Tag, E_Component);
1503 Set_Is_Aliased (Tag);
1504 Set_Related_Type (Tag, Iface);
1505 Init_Component_Location (Tag);
1507 pragma Assert (Is_Frozen (Iface));
1509 Set_DT_Entry_Count (Tag,
1510 DT_Entry_Count (First_Entity (Iface)));
1512 if No (Last_Tag) then
1515 Insert_After (Last_Tag, Decl);
1520 -- If the ancestor has discriminants we need to give special support
1521 -- to store the offset_to_top value of the secondary dispatch tables.
1522 -- For this purpose we add a supplementary component just after the
1523 -- field that contains the tag associated with each secondary DT.
1525 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1527 Make_Component_Definition (Loc,
1528 Subtype_Indication =>
1529 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1531 Offset := Make_Temporary (Loc, 'V');
1534 Make_Component_Declaration (Loc,
1535 Defining_Identifier => Offset,
1536 Component_Definition => Def);
1538 Analyze_Component_Declaration (Decl);
1540 Set_Analyzed (Decl);
1541 Set_Ekind (Offset, E_Component);
1542 Set_Is_Aliased (Offset);
1543 Set_Related_Type (Offset, Iface);
1544 Init_Component_Location (Offset);
1545 Insert_After (Last_Tag, Decl);
1556 -- Start of processing for Add_Interface_Tag_Components
1559 if not RTE_Available (RE_Interface_Tag) then
1561 ("(Ada 2005) interface types not supported by this run-time!",
1566 if Ekind (Typ) /= E_Record_Type
1567 or else (Is_Concurrent_Record_Type (Typ)
1568 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1569 or else (not Is_Concurrent_Record_Type (Typ)
1570 and then No (Interfaces (Typ))
1571 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1576 -- Find the current last tag
1578 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1579 Ext := Record_Extension_Part (Type_Definition (N));
1581 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1582 Ext := Type_Definition (N);
1587 if not (Present (Component_List (Ext))) then
1588 Set_Null_Present (Ext, False);
1590 Set_Component_List (Ext,
1591 Make_Component_List (Loc,
1592 Component_Items => L,
1593 Null_Present => False));
1595 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1596 L := Component_Items
1598 (Record_Extension_Part
1599 (Type_Definition (N))));
1601 L := Component_Items
1603 (Type_Definition (N)));
1606 -- Find the last tag component
1609 while Present (Comp) loop
1610 if Nkind (Comp) = N_Component_Declaration
1611 and then Is_Tag (Defining_Identifier (Comp))
1620 -- At this point L references the list of components and Last_Tag
1621 -- references the current last tag (if any). Now we add the tag
1622 -- corresponding with all the interfaces that are not implemented
1625 if Present (Interfaces (Typ)) then
1626 Elmt := First_Elmt (Interfaces (Typ));
1627 while Present (Elmt) loop
1628 Add_Tag (Node (Elmt));
1632 end Add_Interface_Tag_Components;
1634 -------------------------------------
1635 -- Add_Internal_Interface_Entities --
1636 -------------------------------------
1638 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1641 Iface_Elmt : Elmt_Id;
1642 Iface_Prim : Entity_Id;
1643 Ifaces_List : Elist_Id;
1644 New_Subp : Entity_Id := Empty;
1646 Restore_Scope : Boolean := False;
1649 pragma Assert (Ada_Version >= Ada_2005
1650 and then Is_Record_Type (Tagged_Type)
1651 and then Is_Tagged_Type (Tagged_Type)
1652 and then Has_Interfaces (Tagged_Type)
1653 and then not Is_Interface (Tagged_Type));
1655 -- Ensure that the internal entities are added to the scope of the type
1657 if Scope (Tagged_Type) /= Current_Scope then
1658 Push_Scope (Scope (Tagged_Type));
1659 Restore_Scope := True;
1662 Collect_Interfaces (Tagged_Type, Ifaces_List);
1664 Iface_Elmt := First_Elmt (Ifaces_List);
1665 while Present (Iface_Elmt) loop
1666 Iface := Node (Iface_Elmt);
1668 -- Originally we excluded here from this processing interfaces that
1669 -- are parents of Tagged_Type because their primitives are located
1670 -- in the primary dispatch table (and hence no auxiliary internal
1671 -- entities are required to handle secondary dispatch tables in such
1672 -- case). However, these auxiliary entities are also required to
1673 -- handle derivations of interfaces in formals of generics (see
1674 -- Derive_Subprograms).
1676 Elmt := First_Elmt (Primitive_Operations (Iface));
1677 while Present (Elmt) loop
1678 Iface_Prim := Node (Elmt);
1680 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1682 Find_Primitive_Covering_Interface
1683 (Tagged_Type => Tagged_Type,
1684 Iface_Prim => Iface_Prim);
1686 if No (Prim) and then Serious_Errors_Detected > 0 then
1690 pragma Assert (Present (Prim));
1692 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1693 -- differs from the name of the interface primitive then it is
1694 -- a private primitive inherited from a parent type. In such
1695 -- case, given that Tagged_Type covers the interface, the
1696 -- inherited private primitive becomes visible. For such
1697 -- purpose we add a new entity that renames the inherited
1698 -- private primitive.
1700 if Chars (Prim) /= Chars (Iface_Prim) then
1701 pragma Assert (Has_Suffix (Prim, 'P'));
1703 (New_Subp => New_Subp,
1704 Parent_Subp => Iface_Prim,
1705 Derived_Type => Tagged_Type,
1706 Parent_Type => Iface);
1707 Set_Alias (New_Subp, Prim);
1708 Set_Is_Abstract_Subprogram
1709 (New_Subp, Is_Abstract_Subprogram (Prim));
1713 (New_Subp => New_Subp,
1714 Parent_Subp => Iface_Prim,
1715 Derived_Type => Tagged_Type,
1716 Parent_Type => Iface);
1718 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1719 -- associated with interface types. These entities are
1720 -- only registered in the list of primitives of its
1721 -- corresponding tagged type because they are only used
1722 -- to fill the contents of the secondary dispatch tables.
1723 -- Therefore they are removed from the homonym chains.
1725 Set_Is_Hidden (New_Subp);
1726 Set_Is_Internal (New_Subp);
1727 Set_Alias (New_Subp, Prim);
1728 Set_Is_Abstract_Subprogram
1729 (New_Subp, Is_Abstract_Subprogram (Prim));
1730 Set_Interface_Alias (New_Subp, Iface_Prim);
1732 -- If the returned type is an interface then propagate it to
1733 -- the returned type. Needed by the thunk to generate the code
1734 -- which displaces "this" to reference the corresponding
1735 -- secondary dispatch table in the returned object.
1737 if Is_Interface (Etype (Iface_Prim)) then
1738 Set_Etype (New_Subp, Etype (Iface_Prim));
1741 -- Internal entities associated with interface types are only
1742 -- registered in the list of primitives of the tagged type.
1743 -- They are only used to fill the contents of the secondary
1744 -- dispatch tables. Therefore they are not needed in the
1747 Remove_Homonym (New_Subp);
1749 -- Hidden entities associated with interfaces must have set
1750 -- the Has_Delay_Freeze attribute to ensure that, in case
1751 -- of locally defined tagged types (or compiling with static
1752 -- dispatch tables generation disabled) the corresponding
1753 -- entry of the secondary dispatch table is filled when such
1754 -- an entity is frozen. This is an expansion activity that must
1755 -- be suppressed for ASIS because it leads to gigi elaboration
1756 -- issues in annotate mode.
1758 if not ASIS_Mode then
1759 Set_Has_Delayed_Freeze (New_Subp);
1767 Next_Elmt (Iface_Elmt);
1770 if Restore_Scope then
1773 end Add_Internal_Interface_Entities;
1775 -----------------------------------
1776 -- Analyze_Component_Declaration --
1777 -----------------------------------
1779 procedure Analyze_Component_Declaration (N : Node_Id) is
1780 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1781 Id : constant Entity_Id := Defining_Identifier (N);
1782 E : constant Node_Id := Expression (N);
1783 Typ : constant Node_Id :=
1784 Subtype_Indication (Component_Definition (N));
1788 function Contains_POC (Constr : Node_Id) return Boolean;
1789 -- Determines whether a constraint uses the discriminant of a record
1790 -- type thus becoming a per-object constraint (POC).
1792 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1793 -- Typ is the type of the current component, check whether this type is
1794 -- a limited type. Used to validate declaration against that of
1795 -- enclosing record.
1801 function Contains_POC (Constr : Node_Id) return Boolean is
1803 -- Prevent cascaded errors
1805 if Error_Posted (Constr) then
1809 case Nkind (Constr) is
1810 when N_Attribute_Reference =>
1811 return Attribute_Name (Constr) = Name_Access
1812 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1814 when N_Discriminant_Association =>
1815 return Denotes_Discriminant (Expression (Constr));
1817 when N_Identifier =>
1818 return Denotes_Discriminant (Constr);
1820 when N_Index_Or_Discriminant_Constraint =>
1825 IDC := First (Constraints (Constr));
1826 while Present (IDC) loop
1828 -- One per-object constraint is sufficient
1830 if Contains_POC (IDC) then
1841 return Denotes_Discriminant (Low_Bound (Constr))
1843 Denotes_Discriminant (High_Bound (Constr));
1845 when N_Range_Constraint =>
1846 return Denotes_Discriminant (Range_Expression (Constr));
1854 ----------------------
1855 -- Is_Known_Limited --
1856 ----------------------
1858 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1859 P : constant Entity_Id := Etype (Typ);
1860 R : constant Entity_Id := Root_Type (Typ);
1863 if Is_Limited_Record (Typ) then
1866 -- If the root type is limited (and not a limited interface)
1867 -- so is the current type
1869 elsif Is_Limited_Record (R)
1870 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1874 -- Else the type may have a limited interface progenitor, but a
1875 -- limited record parent.
1877 elsif R /= P and then Is_Limited_Record (P) then
1883 end Is_Known_Limited;
1885 -- Start of processing for Analyze_Component_Declaration
1888 Generate_Definition (Id);
1891 if Present (Typ) then
1892 T := Find_Type_Of_Object
1893 (Subtype_Indication (Component_Definition (N)), N);
1895 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1896 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1899 -- Ada 2005 (AI-230): Access Definition case
1902 pragma Assert (Present
1903 (Access_Definition (Component_Definition (N))));
1905 T := Access_Definition
1907 N => Access_Definition (Component_Definition (N)));
1908 Set_Is_Local_Anonymous_Access (T);
1910 -- Ada 2005 (AI-254)
1912 if Present (Access_To_Subprogram_Definition
1913 (Access_Definition (Component_Definition (N))))
1914 and then Protected_Present (Access_To_Subprogram_Definition
1916 (Component_Definition (N))))
1918 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1922 -- If the subtype is a constrained subtype of the enclosing record,
1923 -- (which must have a partial view) the back-end does not properly
1924 -- handle the recursion. Rewrite the component declaration with an
1925 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1926 -- the tree directly because side effects have already been removed from
1927 -- discriminant constraints.
1929 if Ekind (T) = E_Access_Subtype
1930 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1931 and then Comes_From_Source (T)
1932 and then Nkind (Parent (T)) = N_Subtype_Declaration
1933 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1936 (Subtype_Indication (Component_Definition (N)),
1937 New_Copy_Tree (Subtype_Indication (Parent (T))));
1938 T := Find_Type_Of_Object
1939 (Subtype_Indication (Component_Definition (N)), N);
1942 -- If the component declaration includes a default expression, then we
1943 -- check that the component is not of a limited type (RM 3.7(5)),
1944 -- and do the special preanalysis of the expression (see section on
1945 -- "Handling of Default and Per-Object Expressions" in the spec of
1949 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1950 Preanalyze_Default_Expression (E, T);
1951 Check_Initialization (T, E);
1953 if Ada_Version >= Ada_2005
1954 and then Ekind (T) = E_Anonymous_Access_Type
1955 and then Etype (E) /= Any_Type
1957 -- Check RM 3.9.2(9): "if the expected type for an expression is
1958 -- an anonymous access-to-specific tagged type, then the object
1959 -- designated by the expression shall not be dynamically tagged
1960 -- unless it is a controlling operand in a call on a dispatching
1963 if Is_Tagged_Type (Directly_Designated_Type (T))
1965 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1967 Ekind (Directly_Designated_Type (Etype (E))) =
1971 ("access to specific tagged type required (RM 3.9.2(9))", E);
1974 -- (Ada 2005: AI-230): Accessibility check for anonymous
1977 if Type_Access_Level (Etype (E)) >
1978 Deepest_Type_Access_Level (T)
1981 ("expression has deeper access level than component " &
1982 "(RM 3.10.2 (12.2))", E);
1985 -- The initialization expression is a reference to an access
1986 -- discriminant. The type of the discriminant is always deeper
1987 -- than any access type.
1989 if Ekind (Etype (E)) = E_Anonymous_Access_Type
1990 and then Is_Entity_Name (E)
1991 and then Ekind (Entity (E)) = E_In_Parameter
1992 and then Present (Discriminal_Link (Entity (E)))
1995 ("discriminant has deeper accessibility level than target",
2001 -- The parent type may be a private view with unknown discriminants,
2002 -- and thus unconstrained. Regular components must be constrained.
2004 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2005 if Is_Class_Wide_Type (T) then
2007 ("class-wide subtype with unknown discriminants" &
2008 " in component declaration",
2009 Subtype_Indication (Component_Definition (N)));
2012 ("unconstrained subtype in component declaration",
2013 Subtype_Indication (Component_Definition (N)));
2016 -- Components cannot be abstract, except for the special case of
2017 -- the _Parent field (case of extending an abstract tagged type)
2019 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2020 Error_Msg_N ("type of a component cannot be abstract", N);
2024 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2026 -- The component declaration may have a per-object constraint, set
2027 -- the appropriate flag in the defining identifier of the subtype.
2029 if Present (Subtype_Indication (Component_Definition (N))) then
2031 Sindic : constant Node_Id :=
2032 Subtype_Indication (Component_Definition (N));
2034 if Nkind (Sindic) = N_Subtype_Indication
2035 and then Present (Constraint (Sindic))
2036 and then Contains_POC (Constraint (Sindic))
2038 Set_Has_Per_Object_Constraint (Id);
2043 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2044 -- out some static checks.
2046 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2047 Null_Exclusion_Static_Checks (N);
2050 -- If this component is private (or depends on a private type), flag the
2051 -- record type to indicate that some operations are not available.
2053 P := Private_Component (T);
2057 -- Check for circular definitions
2059 if P = Any_Type then
2060 Set_Etype (Id, Any_Type);
2062 -- There is a gap in the visibility of operations only if the
2063 -- component type is not defined in the scope of the record type.
2065 elsif Scope (P) = Scope (Current_Scope) then
2068 elsif Is_Limited_Type (P) then
2069 Set_Is_Limited_Composite (Current_Scope);
2072 Set_Is_Private_Composite (Current_Scope);
2077 and then Is_Limited_Type (T)
2078 and then Chars (Id) /= Name_uParent
2079 and then Is_Tagged_Type (Current_Scope)
2081 if Is_Derived_Type (Current_Scope)
2082 and then not Is_Known_Limited (Current_Scope)
2085 ("extension of nonlimited type cannot have limited components",
2088 if Is_Interface (Root_Type (Current_Scope)) then
2090 ("\limitedness is not inherited from limited interface", N);
2091 Error_Msg_N ("\add LIMITED to type indication", N);
2094 Explain_Limited_Type (T, N);
2095 Set_Etype (Id, Any_Type);
2096 Set_Is_Limited_Composite (Current_Scope, False);
2098 elsif not Is_Derived_Type (Current_Scope)
2099 and then not Is_Limited_Record (Current_Scope)
2100 and then not Is_Concurrent_Type (Current_Scope)
2103 ("nonlimited tagged type cannot have limited components", N);
2104 Explain_Limited_Type (T, N);
2105 Set_Etype (Id, Any_Type);
2106 Set_Is_Limited_Composite (Current_Scope, False);
2110 -- If the component is an unconstrained task or protected type with
2111 -- discriminants, the component and the enclosing record are limited
2112 -- and the component is constrained by its default values. Compute
2113 -- its actual subtype, else it may be allocated the maximum size by
2114 -- the backend, and possibly overflow.
2116 if Is_Concurrent_Type (T)
2117 and then not Is_Constrained (T)
2118 and then Has_Discriminants (T)
2119 and then not Has_Discriminants (Current_Scope)
2122 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2125 Set_Etype (Id, Act_T);
2127 -- Rewrite component definition to use the constrained subtype
2129 Rewrite (Component_Definition (N),
2130 Make_Component_Definition (Loc,
2131 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2135 Set_Original_Record_Component (Id, Id);
2137 if Has_Aspects (N) then
2138 Analyze_Aspect_Specifications (N, Id);
2141 Analyze_Dimension (N);
2142 end Analyze_Component_Declaration;
2144 --------------------------
2145 -- Analyze_Declarations --
2146 --------------------------
2148 procedure Analyze_Declarations (L : List_Id) is
2151 procedure Adjust_Decl;
2152 -- Adjust Decl not to include implicit label declarations, since these
2153 -- have strange Sloc values that result in elaboration check problems.
2154 -- (They have the sloc of the label as found in the source, and that
2155 -- is ahead of the current declarative part).
2157 procedure Check_Entry_Contracts;
2158 -- Perform a pre-analysis of the pre- and postconditions of an entry
2159 -- declaration. This must be done before full resolution and creation
2160 -- of the parameter block, etc. to catch illegal uses within the
2161 -- contract expression. Full analysis of the expression is done when
2162 -- the contract is processed.
2164 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2165 -- Determine whether Body_Decl denotes the body of a late controlled
2166 -- primitive (either Initialize, Adjust or Finalize). If this is the
2167 -- case, add a proper spec if the body lacks one. The spec is inserted
2168 -- before Body_Decl and immediately analyzed.
2170 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2171 -- Spec_Id is the entity of a package that may define abstract states,
2172 -- and in the case of a child unit, whose ancestors may define abstract
2173 -- states. If the states have partial visible refinement, remove the
2174 -- partial visibility of each constituent at the end of the package
2175 -- spec and body declarations.
2177 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2178 -- Spec_Id is the entity of a package that may define abstract states.
2179 -- If the states have visible refinement, remove the visibility of each
2180 -- constituent at the end of the package body declaration.
2186 procedure Adjust_Decl is
2188 while Present (Prev (Decl))
2189 and then Nkind (Decl) = N_Implicit_Label_Declaration
2195 ---------------------------
2196 -- Check_Entry_Contracts --
2197 ---------------------------
2199 procedure Check_Entry_Contracts is
2205 Ent := First_Entity (Current_Scope);
2206 while Present (Ent) loop
2208 -- This only concerns entries with pre/postconditions
2210 if Ekind (Ent) = E_Entry
2211 and then Present (Contract (Ent))
2212 and then Present (Pre_Post_Conditions (Contract (Ent)))
2214 ASN := Pre_Post_Conditions (Contract (Ent));
2216 Install_Formals (Ent);
2218 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2219 -- is performed on a copy of the pragma expression, to prevent
2220 -- modifying the original expression.
2222 while Present (ASN) loop
2223 if Nkind (ASN) = N_Pragma then
2227 (First (Pragma_Argument_Associations (ASN))));
2228 Set_Parent (Exp, ASN);
2230 -- ??? why not Preanalyze_Assert_Expression
2235 ASN := Next_Pragma (ASN);
2243 end Check_Entry_Contracts;
2245 --------------------------------------
2246 -- Handle_Late_Controlled_Primitive --
2247 --------------------------------------
2249 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2250 Body_Spec : constant Node_Id := Specification (Body_Decl);
2251 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2252 Loc : constant Source_Ptr := Sloc (Body_Id);
2253 Params : constant List_Id :=
2254 Parameter_Specifications (Body_Spec);
2256 Spec_Id : Entity_Id;
2260 -- Consider only procedure bodies whose name matches one of the three
2261 -- controlled primitives.
2263 if Nkind (Body_Spec) /= N_Procedure_Specification
2264 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2270 -- A controlled primitive must have exactly one formal which is not
2271 -- an anonymous access type.
2273 elsif List_Length (Params) /= 1 then
2277 Typ := Parameter_Type (First (Params));
2279 if Nkind (Typ) = N_Access_Definition then
2285 -- The type of the formal must be derived from [Limited_]Controlled
2287 if not Is_Controlled (Entity (Typ)) then
2291 -- Check whether a specification exists for this body. We do not
2292 -- analyze the spec of the body in full, because it will be analyzed
2293 -- again when the body is properly analyzed, and we cannot create
2294 -- duplicate entries in the formals chain. We look for an explicit
2295 -- specification because the body may be an overriding operation and
2296 -- an inherited spec may be present.
2298 Spec_Id := Current_Entity (Body_Id);
2300 while Present (Spec_Id) loop
2301 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2302 and then Scope (Spec_Id) = Current_Scope
2303 and then Present (First_Formal (Spec_Id))
2304 and then No (Next_Formal (First_Formal (Spec_Id)))
2305 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2306 and then Comes_From_Source (Spec_Id)
2311 Spec_Id := Homonym (Spec_Id);
2314 -- At this point the body is known to be a late controlled primitive.
2315 -- Generate a matching spec and insert it before the body. Note the
2316 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2317 -- tree in this case.
2319 Spec := Copy_Separate_Tree (Body_Spec);
2321 -- Ensure that the subprogram declaration does not inherit the null
2322 -- indicator from the body as we now have a proper spec/body pair.
2324 Set_Null_Present (Spec, False);
2326 -- Ensure that the freeze node is inserted after the declaration of
2327 -- the primitive since its expansion will freeze the primitive.
2329 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2331 Insert_Before_And_Analyze (Body_Decl, Decl);
2332 end Handle_Late_Controlled_Primitive;
2334 ----------------------------------------
2335 -- Remove_Partial_Visible_Refinements --
2336 ----------------------------------------
2338 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2339 State_Elmt : Elmt_Id;
2341 if Present (Abstract_States (Spec_Id)) then
2342 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2343 while Present (State_Elmt) loop
2344 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2345 Next_Elmt (State_Elmt);
2349 -- For a child unit, also hide the partial state refinement from
2350 -- ancestor packages.
2352 if Is_Child_Unit (Spec_Id) then
2353 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2355 end Remove_Partial_Visible_Refinements;
2357 --------------------------------
2358 -- Remove_Visible_Refinements --
2359 --------------------------------
2361 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2362 State_Elmt : Elmt_Id;
2364 if Present (Abstract_States (Spec_Id)) then
2365 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2366 while Present (State_Elmt) loop
2367 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2368 Next_Elmt (State_Elmt);
2371 end Remove_Visible_Refinements;
2375 Context : Node_Id := Empty;
2376 Freeze_From : Entity_Id := Empty;
2377 Next_Decl : Node_Id;
2379 Body_Seen : Boolean := False;
2380 -- Flag set when the first body [stub] is encountered
2382 -- Start of processing for Analyze_Declarations
2385 if Restriction_Check_Required (SPARK_05) then
2386 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2390 while Present (Decl) loop
2392 -- Package spec cannot contain a package declaration in SPARK
2394 if Nkind (Decl) = N_Package_Declaration
2395 and then Nkind (Parent (L)) = N_Package_Specification
2397 Check_SPARK_05_Restriction
2398 ("package specification cannot contain a package declaration",
2402 -- Complete analysis of declaration
2405 Next_Decl := Next (Decl);
2407 if No (Freeze_From) then
2408 Freeze_From := First_Entity (Current_Scope);
2411 -- At the end of a declarative part, freeze remaining entities
2412 -- declared in it. The end of the visible declarations of package
2413 -- specification is not the end of a declarative part if private
2414 -- declarations are present. The end of a package declaration is a
2415 -- freezing point only if it a library package. A task definition or
2416 -- protected type definition is not a freeze point either. Finally,
2417 -- we do not freeze entities in generic scopes, because there is no
2418 -- code generated for them and freeze nodes will be generated for
2421 -- The end of a package instantiation is not a freeze point, but
2422 -- for now we make it one, because the generic body is inserted
2423 -- (currently) immediately after. Generic instantiations will not
2424 -- be a freeze point once delayed freezing of bodies is implemented.
2425 -- (This is needed in any case for early instantiations ???).
2427 if No (Next_Decl) then
2428 if Nkind (Parent (L)) = N_Component_List then
2431 elsif Nkind_In (Parent (L), N_Protected_Definition,
2434 Check_Entry_Contracts;
2436 elsif Nkind (Parent (L)) /= N_Package_Specification then
2437 if Nkind (Parent (L)) = N_Package_Body then
2438 Freeze_From := First_Entity (Current_Scope);
2441 -- There may have been several freezing points previously,
2442 -- for example object declarations or subprogram bodies, but
2443 -- at the end of a declarative part we check freezing from
2444 -- the beginning, even though entities may already be frozen,
2445 -- in order to perform visibility checks on delayed aspects.
2448 Freeze_All (First_Entity (Current_Scope), Decl);
2449 Freeze_From := Last_Entity (Current_Scope);
2451 elsif Scope (Current_Scope) /= Standard_Standard
2452 and then not Is_Child_Unit (Current_Scope)
2453 and then No (Generic_Parent (Parent (L)))
2457 elsif L /= Visible_Declarations (Parent (L))
2458 or else No (Private_Declarations (Parent (L)))
2459 or else Is_Empty_List (Private_Declarations (Parent (L)))
2462 Freeze_All (First_Entity (Current_Scope), Decl);
2463 Freeze_From := Last_Entity (Current_Scope);
2465 -- At the end of the visible declarations the expressions in
2466 -- aspects of all entities declared so far must be resolved.
2467 -- The entities themselves might be frozen later, and the
2468 -- generated pragmas and attribute definition clauses analyzed
2469 -- in full at that point, but name resolution must take place
2471 -- In addition to being the proper semantics, this is mandatory
2472 -- within generic units, because global name capture requires
2473 -- those expressions to be analyzed, given that the generated
2474 -- pragmas do not appear in the original generic tree.
2476 elsif Serious_Errors_Detected = 0 then
2481 E := First_Entity (Current_Scope);
2482 while Present (E) loop
2483 Resolve_Aspect_Expressions (E);
2489 -- If next node is a body then freeze all types before the body.
2490 -- An exception occurs for some expander-generated bodies. If these
2491 -- are generated at places where in general language rules would not
2492 -- allow a freeze point, then we assume that the expander has
2493 -- explicitly checked that all required types are properly frozen,
2494 -- and we do not cause general freezing here. This special circuit
2495 -- is used when the encountered body is marked as having already
2498 -- In all other cases (bodies that come from source, and expander
2499 -- generated bodies that have not been analyzed yet), freeze all
2500 -- types now. Note that in the latter case, the expander must take
2501 -- care to attach the bodies at a proper place in the tree so as to
2502 -- not cause unwanted freezing at that point.
2504 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2506 -- When a controlled type is frozen, the expander generates stream
2507 -- and controlled type support routines. If the freeze is caused
2508 -- by the stand alone body of Initialize, Adjust and Finalize, the
2509 -- expander will end up using the wrong version of these routines
2510 -- as the body has not been processed yet. To remedy this, detect
2511 -- a late controlled primitive and create a proper spec for it.
2512 -- This ensures that the primitive will override its inherited
2513 -- counterpart before the freeze takes place.
2515 -- If the declaration we just processed is a body, do not attempt
2516 -- to examine Next_Decl as the late primitive idiom can only apply
2517 -- to the first encountered body.
2519 -- The spec of the late primitive is not generated in ASIS mode to
2520 -- ensure a consistent list of primitives that indicates the true
2521 -- semantic structure of the program (which is not relevant when
2522 -- generating executable code.
2524 -- ??? a cleaner approach may be possible and/or this solution
2525 -- could be extended to general-purpose late primitives, TBD.
2527 if not ASIS_Mode and then not Body_Seen and then not Is_Body (Decl)
2531 if Nkind (Next_Decl) = N_Subprogram_Body then
2532 Handle_Late_Controlled_Primitive (Next_Decl);
2537 Freeze_All (Freeze_From, Decl);
2538 Freeze_From := Last_Entity (Current_Scope);
2544 -- Analyze the contracts of packages and their bodies
2547 Context := Parent (L);
2549 if Nkind (Context) = N_Package_Specification then
2551 -- When a package has private declarations, its contract must be
2552 -- analyzed at the end of the said declarations. This way both the
2553 -- analysis and freeze actions are properly synchronized in case
2554 -- of private type use within the contract.
2556 if L = Private_Declarations (Context) then
2557 Analyze_Package_Contract (Defining_Entity (Context));
2559 -- Otherwise the contract is analyzed at the end of the visible
2562 elsif L = Visible_Declarations (Context)
2563 and then No (Private_Declarations (Context))
2565 Analyze_Package_Contract (Defining_Entity (Context));
2568 elsif Nkind (Context) = N_Package_Body then
2569 Analyze_Package_Body_Contract (Defining_Entity (Context));
2572 -- Analyze the contracts of various constructs now due to the delayed
2573 -- visibility needs of their aspects and pragmas.
2575 Analyze_Contracts (L);
2577 if Nkind (Context) = N_Package_Body then
2579 -- Ensure that all abstract states and objects declared in the
2580 -- state space of a package body are utilized as constituents.
2582 Check_Unused_Body_States (Defining_Entity (Context));
2584 -- State refinements are visible up to the end of the package body
2585 -- declarations. Hide the state refinements from visibility to
2586 -- restore the original state conditions.
2588 Remove_Visible_Refinements (Corresponding_Spec (Context));
2589 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2591 elsif Nkind (Context) = N_Package_Declaration then
2593 -- Partial state refinements are visible up to the end of the
2594 -- package spec declarations. Hide the partial state refinements
2595 -- from visibility to restore the original state conditions.
2597 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2600 -- Verify that all abstract states found in any package declared in
2601 -- the input declarative list have proper refinements. The check is
2602 -- performed only when the context denotes a block, entry, package,
2603 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2605 Check_State_Refinements (Context);
2607 end Analyze_Declarations;
2609 -----------------------------------
2610 -- Analyze_Full_Type_Declaration --
2611 -----------------------------------
2613 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2614 Def : constant Node_Id := Type_Definition (N);
2615 Def_Id : constant Entity_Id := Defining_Identifier (N);
2619 Is_Remote : constant Boolean :=
2620 (Is_Remote_Types (Current_Scope)
2621 or else Is_Remote_Call_Interface (Current_Scope))
2622 and then not (In_Private_Part (Current_Scope)
2623 or else In_Package_Body (Current_Scope));
2625 procedure Check_Nonoverridable_Aspects;
2626 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2627 -- be overridden, and can only be confirmed on derivation.
2629 procedure Check_Ops_From_Incomplete_Type;
2630 -- If there is a tagged incomplete partial view of the type, traverse
2631 -- the primitives of the incomplete view and change the type of any
2632 -- controlling formals and result to indicate the full view. The
2633 -- primitives will be added to the full type's primitive operations
2634 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2635 -- is called from Process_Incomplete_Dependents).
2637 ----------------------------------
2638 -- Check_Nonoverridable_Aspects --
2639 ----------------------------------
2641 procedure Check_Nonoverridable_Aspects is
2642 Prev_Aspects : constant List_Id :=
2643 Aspect_Specifications (Parent (Def_Id));
2644 Par_Type : Entity_Id;
2646 function Has_Aspect_Spec
2648 Aspect_Name : Name_Id) return Boolean;
2649 -- Check whether a list of aspect specifications includes an entry
2650 -- for a specific aspect. The list is either that of a partial or
2653 ---------------------
2654 -- Has_Aspect_Spec --
2655 ---------------------
2657 function Has_Aspect_Spec
2659 Aspect_Name : Name_Id) return Boolean
2663 Spec := First (Specs);
2664 while Present (Spec) loop
2665 if Chars (Identifier (Spec)) = Aspect_Name then
2671 end Has_Aspect_Spec;
2673 -- Start of processing for Check_Nonoverridable_Aspects
2677 -- Get parent type of derived type. Note that Prev is the entity
2678 -- in the partial declaration, but its contents are now those of
2679 -- full view, while Def_Id reflects the partial view.
2681 if Is_Private_Type (Def_Id) then
2682 Par_Type := Etype (Full_View (Def_Id));
2684 Par_Type := Etype (Def_Id);
2687 -- If there is an inherited Implicit_Dereference, verify that it is
2688 -- made explicit in the partial view.
2690 if Has_Discriminants (Base_Type (Par_Type))
2691 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2692 and then Present (Discriminant_Specifications (Parent (Prev)))
2693 and then Present (Get_Reference_Discriminant (Par_Type))
2696 not Has_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference)
2699 ("type does not inherit implicit dereference", Prev);
2702 -- If one of the views has the aspect specified, verify that it
2703 -- is consistent with that of the parent.
2706 Par_Discr : constant Entity_Id :=
2707 Get_Reference_Discriminant (Par_Type);
2708 Cur_Discr : constant Entity_Id :=
2709 Get_Reference_Discriminant (Prev);
2711 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
2712 Error_Msg_N ("aspect incosistent with that of parent", N);
2718 -- TBD : other nonoverridable aspects.
2719 end Check_Nonoverridable_Aspects;
2721 ------------------------------------
2722 -- Check_Ops_From_Incomplete_Type --
2723 ------------------------------------
2725 procedure Check_Ops_From_Incomplete_Type is
2732 and then Ekind (Prev) = E_Incomplete_Type
2733 and then Is_Tagged_Type (Prev)
2734 and then Is_Tagged_Type (T)
2736 Elmt := First_Elmt (Primitive_Operations (Prev));
2737 while Present (Elmt) loop
2740 Formal := First_Formal (Op);
2741 while Present (Formal) loop
2742 if Etype (Formal) = Prev then
2743 Set_Etype (Formal, T);
2746 Next_Formal (Formal);
2749 if Etype (Op) = Prev then
2756 end Check_Ops_From_Incomplete_Type;
2758 -- Start of processing for Analyze_Full_Type_Declaration
2761 Prev := Find_Type_Name (N);
2763 -- The full view, if present, now points to the current type. If there
2764 -- is an incomplete partial view, set a link to it, to simplify the
2765 -- retrieval of primitive operations of the type.
2767 -- Ada 2005 (AI-50217): If the type was previously decorated when
2768 -- imported through a LIMITED WITH clause, it appears as incomplete
2769 -- but has no full view.
2771 if Ekind (Prev) = E_Incomplete_Type
2772 and then Present (Full_View (Prev))
2774 T := Full_View (Prev);
2775 Set_Incomplete_View (N, Parent (Prev));
2780 Set_Is_Pure (T, Is_Pure (Current_Scope));
2782 -- We set the flag Is_First_Subtype here. It is needed to set the
2783 -- corresponding flag for the Implicit class-wide-type created
2784 -- during tagged types processing.
2786 Set_Is_First_Subtype (T, True);
2788 -- Only composite types other than array types are allowed to have
2793 -- For derived types, the rule will be checked once we've figured
2794 -- out the parent type.
2796 when N_Derived_Type_Definition =>
2799 -- For record types, discriminants are allowed, unless we are in
2802 when N_Record_Definition =>
2803 if Present (Discriminant_Specifications (N)) then
2804 Check_SPARK_05_Restriction
2805 ("discriminant type is not allowed",
2807 (First (Discriminant_Specifications (N))));
2811 if Present (Discriminant_Specifications (N)) then
2813 ("elementary or array type cannot have discriminants",
2815 (First (Discriminant_Specifications (N))));
2819 -- Elaborate the type definition according to kind, and generate
2820 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2821 -- already done (this happens during the reanalysis that follows a call
2822 -- to the high level optimizer).
2824 if not Analyzed (T) then
2827 -- A type declared within a Ghost region is automatically Ghost
2828 -- (SPARK RM 6.9(2)).
2830 if Ghost_Mode > None then
2831 Set_Is_Ghost_Entity (T);
2835 when N_Access_To_Subprogram_Definition =>
2836 Access_Subprogram_Declaration (T, Def);
2838 -- If this is a remote access to subprogram, we must create the
2839 -- equivalent fat pointer type, and related subprograms.
2842 Process_Remote_AST_Declaration (N);
2845 -- Validate categorization rule against access type declaration
2846 -- usually a violation in Pure unit, Shared_Passive unit.
2848 Validate_Access_Type_Declaration (T, N);
2850 when N_Access_To_Object_Definition =>
2851 Access_Type_Declaration (T, Def);
2853 -- Validate categorization rule against access type declaration
2854 -- usually a violation in Pure unit, Shared_Passive unit.
2856 Validate_Access_Type_Declaration (T, N);
2858 -- If we are in a Remote_Call_Interface package and define a
2859 -- RACW, then calling stubs and specific stream attributes
2863 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2865 Add_RACW_Features (Def_Id);
2868 when N_Array_Type_Definition =>
2869 Array_Type_Declaration (T, Def);
2871 when N_Derived_Type_Definition =>
2872 Derived_Type_Declaration (T, N, T /= Def_Id);
2874 when N_Enumeration_Type_Definition =>
2875 Enumeration_Type_Declaration (T, Def);
2877 when N_Floating_Point_Definition =>
2878 Floating_Point_Type_Declaration (T, Def);
2880 when N_Decimal_Fixed_Point_Definition =>
2881 Decimal_Fixed_Point_Type_Declaration (T, Def);
2883 when N_Ordinary_Fixed_Point_Definition =>
2884 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2886 when N_Signed_Integer_Type_Definition =>
2887 Signed_Integer_Type_Declaration (T, Def);
2889 when N_Modular_Type_Definition =>
2890 Modular_Type_Declaration (T, Def);
2892 when N_Record_Definition =>
2893 Record_Type_Declaration (T, N, Prev);
2895 -- If declaration has a parse error, nothing to elaborate.
2901 raise Program_Error;
2906 if Etype (T) = Any_Type then
2910 -- Controlled type is not allowed in SPARK
2912 if Is_Visibly_Controlled (T) then
2913 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
2916 -- Some common processing for all types
2918 Set_Depends_On_Private (T, Has_Private_Component (T));
2919 Check_Ops_From_Incomplete_Type;
2921 -- Both the declared entity, and its anonymous base type if one was
2922 -- created, need freeze nodes allocated.
2925 B : constant Entity_Id := Base_Type (T);
2928 -- In the case where the base type differs from the first subtype, we
2929 -- pre-allocate a freeze node, and set the proper link to the first
2930 -- subtype. Freeze_Entity will use this preallocated freeze node when
2931 -- it freezes the entity.
2933 -- This does not apply if the base type is a generic type, whose
2934 -- declaration is independent of the current derived definition.
2936 if B /= T and then not Is_Generic_Type (B) then
2937 Ensure_Freeze_Node (B);
2938 Set_First_Subtype_Link (Freeze_Node (B), T);
2941 -- A type that is imported through a limited_with clause cannot
2942 -- generate any code, and thus need not be frozen. However, an access
2943 -- type with an imported designated type needs a finalization list,
2944 -- which may be referenced in some other package that has non-limited
2945 -- visibility on the designated type. Thus we must create the
2946 -- finalization list at the point the access type is frozen, to
2947 -- prevent unsatisfied references at link time.
2949 if not From_Limited_With (T) or else Is_Access_Type (T) then
2950 Set_Has_Delayed_Freeze (T);
2954 -- Case where T is the full declaration of some private type which has
2955 -- been swapped in Defining_Identifier (N).
2957 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2958 Process_Full_View (N, T, Def_Id);
2960 -- Record the reference. The form of this is a little strange, since
2961 -- the full declaration has been swapped in. So the first parameter
2962 -- here represents the entity to which a reference is made which is
2963 -- the "real" entity, i.e. the one swapped in, and the second
2964 -- parameter provides the reference location.
2966 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2967 -- since we don't want a complaint about the full type being an
2968 -- unwanted reference to the private type
2971 B : constant Boolean := Has_Pragma_Unreferenced (T);
2973 Set_Has_Pragma_Unreferenced (T, False);
2974 Generate_Reference (T, T, 'c');
2975 Set_Has_Pragma_Unreferenced (T, B);
2978 Set_Completion_Referenced (Def_Id);
2980 -- For completion of incomplete type, process incomplete dependents
2981 -- and always mark the full type as referenced (it is the incomplete
2982 -- type that we get for any real reference).
2984 elsif Ekind (Prev) = E_Incomplete_Type then
2985 Process_Incomplete_Dependents (N, T, Prev);
2986 Generate_Reference (Prev, Def_Id, 'c');
2987 Set_Completion_Referenced (Def_Id);
2989 -- If not private type or incomplete type completion, this is a real
2990 -- definition of a new entity, so record it.
2993 Generate_Definition (Def_Id);
2996 -- Propagate any pending access types whose finalization masters need to
2997 -- be fully initialized from the partial to the full view. Guard against
2998 -- an illegal full view that remains unanalyzed.
3000 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3001 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3004 if Chars (Scope (Def_Id)) = Name_System
3005 and then Chars (Def_Id) = Name_Address
3006 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
3008 Set_Is_Descendant_Of_Address (Def_Id);
3009 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3010 Set_Is_Descendant_Of_Address (Prev);
3013 Set_Optimize_Alignment_Flags (Def_Id);
3014 Check_Eliminated (Def_Id);
3016 -- If the declaration is a completion and aspects are present, apply
3017 -- them to the entity for the type which is currently the partial
3018 -- view, but which is the one that will be frozen.
3020 if Has_Aspects (N) then
3022 -- In most cases the partial view is a private type, and both views
3023 -- appear in different declarative parts. In the unusual case where
3024 -- the partial view is incomplete, perform the analysis on the
3025 -- full view, to prevent freezing anomalies with the corresponding
3026 -- class-wide type, which otherwise might be frozen before the
3027 -- dispatch table is built.
3030 and then Ekind (Prev) /= E_Incomplete_Type
3032 Analyze_Aspect_Specifications (N, Prev);
3037 Analyze_Aspect_Specifications (N, Def_Id);
3041 if Is_Derived_Type (Prev)
3042 and then Def_Id /= Prev
3044 Check_Nonoverridable_Aspects;
3046 end Analyze_Full_Type_Declaration;
3048 ----------------------------------
3049 -- Analyze_Incomplete_Type_Decl --
3050 ----------------------------------
3052 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3053 F : constant Boolean := Is_Pure (Current_Scope);
3057 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3059 Generate_Definition (Defining_Identifier (N));
3061 -- Process an incomplete declaration. The identifier must not have been
3062 -- declared already in the scope. However, an incomplete declaration may
3063 -- appear in the private part of a package, for a private type that has
3064 -- already been declared.
3066 -- In this case, the discriminants (if any) must match
3068 T := Find_Type_Name (N);
3070 Set_Ekind (T, E_Incomplete_Type);
3071 Init_Size_Align (T);
3072 Set_Is_First_Subtype (T, True);
3075 -- An incomplete type declared within a Ghost region is automatically
3076 -- Ghost (SPARK RM 6.9(2)).
3078 if Ghost_Mode > None then
3079 Set_Is_Ghost_Entity (T);
3082 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3083 -- incomplete types.
3085 if Tagged_Present (N) then
3086 Set_Is_Tagged_Type (T, True);
3087 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3088 Make_Class_Wide_Type (T);
3089 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3092 Set_Stored_Constraint (T, No_Elist);
3094 if Present (Discriminant_Specifications (N)) then
3096 Process_Discriminants (N);
3100 -- If the type has discriminants, nontrivial subtypes may be declared
3101 -- before the full view of the type. The full views of those subtypes
3102 -- will be built after the full view of the type.
3104 Set_Private_Dependents (T, New_Elmt_List);
3106 end Analyze_Incomplete_Type_Decl;
3108 -----------------------------------
3109 -- Analyze_Interface_Declaration --
3110 -----------------------------------
3112 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3113 CW : constant Entity_Id := Class_Wide_Type (T);
3116 Set_Is_Tagged_Type (T);
3117 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3119 Set_Is_Limited_Record (T, Limited_Present (Def)
3120 or else Task_Present (Def)
3121 or else Protected_Present (Def)
3122 or else Synchronized_Present (Def));
3124 -- Type is abstract if full declaration carries keyword, or if previous
3125 -- partial view did.
3127 Set_Is_Abstract_Type (T);
3128 Set_Is_Interface (T);
3130 -- Type is a limited interface if it includes the keyword limited, task,
3131 -- protected, or synchronized.
3133 Set_Is_Limited_Interface
3134 (T, Limited_Present (Def)
3135 or else Protected_Present (Def)
3136 or else Synchronized_Present (Def)
3137 or else Task_Present (Def));
3139 Set_Interfaces (T, New_Elmt_List);
3140 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3142 -- Complete the decoration of the class-wide entity if it was already
3143 -- built (i.e. during the creation of the limited view)
3145 if Present (CW) then
3146 Set_Is_Interface (CW);
3147 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3150 -- Check runtime support for synchronized interfaces
3152 if (Is_Task_Interface (T)
3153 or else Is_Protected_Interface (T)
3154 or else Is_Synchronized_Interface (T))
3155 and then not RTE_Available (RE_Select_Specific_Data)
3157 Error_Msg_CRT ("synchronized interfaces", T);
3159 end Analyze_Interface_Declaration;
3161 -----------------------------
3162 -- Analyze_Itype_Reference --
3163 -----------------------------
3165 -- Nothing to do. This node is placed in the tree only for the benefit of
3166 -- back end processing, and has no effect on the semantic processing.
3168 procedure Analyze_Itype_Reference (N : Node_Id) is
3170 pragma Assert (Is_Itype (Itype (N)));
3172 end Analyze_Itype_Reference;
3174 --------------------------------
3175 -- Analyze_Number_Declaration --
3176 --------------------------------
3178 procedure Analyze_Number_Declaration (N : Node_Id) is
3179 E : constant Node_Id := Expression (N);
3180 Id : constant Entity_Id := Defining_Identifier (N);
3181 Index : Interp_Index;
3186 Generate_Definition (Id);
3189 -- A number declared within a Ghost region is automatically Ghost
3190 -- (SPARK RM 6.9(2)).
3192 if Ghost_Mode > None then
3193 Set_Is_Ghost_Entity (Id);
3196 -- This is an optimization of a common case of an integer literal
3198 if Nkind (E) = N_Integer_Literal then
3199 Set_Is_Static_Expression (E, True);
3200 Set_Etype (E, Universal_Integer);
3202 Set_Etype (Id, Universal_Integer);
3203 Set_Ekind (Id, E_Named_Integer);
3204 Set_Is_Frozen (Id, True);
3208 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3210 -- Process expression, replacing error by integer zero, to avoid
3211 -- cascaded errors or aborts further along in the processing
3213 -- Replace Error by integer zero, which seems least likely to cause
3217 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3218 Set_Error_Posted (E);
3223 -- Verify that the expression is static and numeric. If
3224 -- the expression is overloaded, we apply the preference
3225 -- rule that favors root numeric types.
3227 if not Is_Overloaded (E) then
3229 if Has_Dynamic_Predicate_Aspect (T) then
3231 ("subtype has dynamic predicate, "
3232 & "not allowed in number declaration", N);
3238 Get_First_Interp (E, Index, It);
3239 while Present (It.Typ) loop
3240 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3241 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3243 if T = Any_Type then
3246 elsif It.Typ = Universal_Real
3248 It.Typ = Universal_Integer
3250 -- Choose universal interpretation over any other
3257 Get_Next_Interp (Index, It);
3261 if Is_Integer_Type (T) then
3263 Set_Etype (Id, Universal_Integer);
3264 Set_Ekind (Id, E_Named_Integer);
3266 elsif Is_Real_Type (T) then
3268 -- Because the real value is converted to universal_real, this is a
3269 -- legal context for a universal fixed expression.
3271 if T = Universal_Fixed then
3273 Loc : constant Source_Ptr := Sloc (N);
3274 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3276 New_Occurrence_Of (Universal_Real, Loc),
3277 Expression => Relocate_Node (E));
3284 elsif T = Any_Fixed then
3285 Error_Msg_N ("illegal context for mixed mode operation", E);
3287 -- Expression is of the form : universal_fixed * integer. Try to
3288 -- resolve as universal_real.
3290 T := Universal_Real;
3295 Set_Etype (Id, Universal_Real);
3296 Set_Ekind (Id, E_Named_Real);
3299 Wrong_Type (E, Any_Numeric);
3303 Set_Ekind (Id, E_Constant);
3304 Set_Never_Set_In_Source (Id, True);
3305 Set_Is_True_Constant (Id, True);
3309 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3310 Set_Etype (E, Etype (Id));
3313 if not Is_OK_Static_Expression (E) then
3314 Flag_Non_Static_Expr
3315 ("non-static expression used in number declaration!", E);
3316 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3317 Set_Etype (E, Any_Type);
3320 Analyze_Dimension (N);
3321 end Analyze_Number_Declaration;
3323 --------------------------------
3324 -- Analyze_Object_Declaration --
3325 --------------------------------
3327 procedure Analyze_Object_Declaration (N : Node_Id) is
3328 Loc : constant Source_Ptr := Sloc (N);
3329 Id : constant Entity_Id := Defining_Identifier (N);
3333 E : Node_Id := Expression (N);
3334 -- E is set to Expression (N) throughout this routine. When
3335 -- Expression (N) is modified, E is changed accordingly.
3337 Prev_Entity : Entity_Id := Empty;
3339 function Count_Tasks (T : Entity_Id) return Uint;
3340 -- This function is called when a non-generic library level object of a
3341 -- task type is declared. Its function is to count the static number of
3342 -- tasks declared within the type (it is only called if Has_Task is set
3343 -- for T). As a side effect, if an array of tasks with non-static bounds
3344 -- or a variant record type is encountered, Check_Restriction is called
3345 -- indicating the count is unknown.
3347 function Delayed_Aspect_Present return Boolean;
3348 -- If the declaration has an expression that is an aggregate, and it
3349 -- has aspects that require delayed analysis, the resolution of the
3350 -- aggregate must be deferred to the freeze point of the objet. This
3351 -- special processing was created for address clauses, but it must
3352 -- also apply to Alignment. This must be done before the aspect
3353 -- specifications are analyzed because we must handle the aggregate
3354 -- before the analysis of the object declaration is complete.
3356 -- Any other relevant delayed aspects on object declarations ???
3362 function Count_Tasks (T : Entity_Id) return Uint is
3368 if Is_Task_Type (T) then
3371 elsif Is_Record_Type (T) then
3372 if Has_Discriminants (T) then
3373 Check_Restriction (Max_Tasks, N);
3378 C := First_Component (T);
3379 while Present (C) loop
3380 V := V + Count_Tasks (Etype (C));
3387 elsif Is_Array_Type (T) then
3388 X := First_Index (T);
3389 V := Count_Tasks (Component_Type (T));
3390 while Present (X) loop
3393 if not Is_OK_Static_Subtype (C) then
3394 Check_Restriction (Max_Tasks, N);
3397 V := V * (UI_Max (Uint_0,
3398 Expr_Value (Type_High_Bound (C)) -
3399 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3412 ----------------------------
3413 -- Delayed_Aspect_Present --
3414 ----------------------------
3416 function Delayed_Aspect_Present return Boolean is
3421 if Present (Aspect_Specifications (N)) then
3422 A := First (Aspect_Specifications (N));
3423 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3424 while Present (A) loop
3425 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3434 end Delayed_Aspect_Present;
3438 Save_Ghost_Mode : constant Ghost_Mode_Type := Ghost_Mode;
3439 Related_Id : Entity_Id;
3441 -- Start of processing for Analyze_Object_Declaration
3444 -- There are three kinds of implicit types generated by an
3445 -- object declaration:
3447 -- 1. Those generated by the original Object Definition
3449 -- 2. Those generated by the Expression
3451 -- 3. Those used to constrain the Object Definition with the
3452 -- expression constraints when the definition is unconstrained.
3454 -- They must be generated in this order to avoid order of elaboration
3455 -- issues. Thus the first step (after entering the name) is to analyze
3456 -- the object definition.
3458 if Constant_Present (N) then
3459 Prev_Entity := Current_Entity_In_Scope (Id);
3461 if Present (Prev_Entity)
3463 -- If the homograph is an implicit subprogram, it is overridden
3464 -- by the current declaration.
3466 ((Is_Overloadable (Prev_Entity)
3467 and then Is_Inherited_Operation (Prev_Entity))
3469 -- The current object is a discriminal generated for an entry
3470 -- family index. Even though the index is a constant, in this
3471 -- particular context there is no true constant redeclaration.
3472 -- Enter_Name will handle the visibility.
3475 (Is_Discriminal (Id)
3476 and then Ekind (Discriminal_Link (Id)) =
3477 E_Entry_Index_Parameter)
3479 -- The current object is the renaming for a generic declared
3480 -- within the instance.
3483 (Ekind (Prev_Entity) = E_Package
3484 and then Nkind (Parent (Prev_Entity)) =
3485 N_Package_Renaming_Declaration
3486 and then not Comes_From_Source (Prev_Entity)
3488 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3490 -- The entity may be a homonym of a private component of the
3491 -- enclosing protected object, for which we create a local
3492 -- renaming declaration. The declaration is legal, even if
3493 -- useless when it just captures that component.
3496 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3497 and then Nkind (Parent (Prev_Entity)) =
3498 N_Object_Renaming_Declaration))
3500 Prev_Entity := Empty;
3504 -- The object declaration is Ghost when it is subject to pragma Ghost or
3505 -- completes a deferred Ghost constant. Set the mode now to ensure that
3506 -- any nodes generated during analysis and expansion are properly marked
3509 Set_Ghost_Mode (N, Prev_Entity);
3511 if Present (Prev_Entity) then
3512 Constant_Redeclaration (Id, N, T);
3514 Generate_Reference (Prev_Entity, Id, 'c');
3515 Set_Completion_Referenced (Id);
3517 if Error_Posted (N) then
3519 -- Type mismatch or illegal redeclaration; do not analyze
3520 -- expression to avoid cascaded errors.
3522 T := Find_Type_Of_Object (Object_Definition (N), N);
3524 Set_Ekind (Id, E_Variable);
3528 -- In the normal case, enter identifier at the start to catch premature
3529 -- usage in the initialization expression.
3532 Generate_Definition (Id);
3535 Mark_Coextensions (N, Object_Definition (N));
3537 T := Find_Type_Of_Object (Object_Definition (N), N);
3539 if Nkind (Object_Definition (N)) = N_Access_Definition
3541 (Access_To_Subprogram_Definition (Object_Definition (N)))
3542 and then Protected_Present
3543 (Access_To_Subprogram_Definition (Object_Definition (N)))
3545 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3548 if Error_Posted (Id) then
3550 Set_Ekind (Id, E_Variable);
3555 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3556 -- out some static checks.
3558 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3560 -- In case of aggregates we must also take care of the correct
3561 -- initialization of nested aggregates bug this is done at the
3562 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
3564 if Present (Expression (N))
3565 and then Nkind (Expression (N)) = N_Aggregate
3571 Save_Typ : constant Entity_Id := Etype (Id);
3573 Set_Etype (Id, T); -- Temp. decoration for static checks
3574 Null_Exclusion_Static_Checks (N);
3575 Set_Etype (Id, Save_Typ);
3580 -- Object is marked pure if it is in a pure scope
3582 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3584 -- If deferred constant, make sure context is appropriate. We detect
3585 -- a deferred constant as a constant declaration with no expression.
3586 -- A deferred constant can appear in a package body if its completion
3587 -- is by means of an interface pragma.
3589 if Constant_Present (N) and then No (E) then
3591 -- A deferred constant may appear in the declarative part of the
3592 -- following constructs:
3596 -- extended return statements
3599 -- subprogram bodies
3602 -- When declared inside a package spec, a deferred constant must be
3603 -- completed by a full constant declaration or pragma Import. In all
3604 -- other cases, the only proper completion is pragma Import. Extended
3605 -- return statements are flagged as invalid contexts because they do
3606 -- not have a declarative part and so cannot accommodate the pragma.
3608 if Ekind (Current_Scope) = E_Return_Statement then
3610 ("invalid context for deferred constant declaration (RM 7.4)",
3613 ("\declaration requires an initialization expression",
3615 Set_Constant_Present (N, False);
3617 -- In Ada 83, deferred constant must be of private type
3619 elsif not Is_Private_Type (T) then
3620 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3622 ("(Ada 83) deferred constant must be private type", N);
3626 -- If not a deferred constant, then the object declaration freezes
3627 -- its type, unless the object is of an anonymous type and has delayed
3628 -- aspects. In that case the type is frozen when the object itself is.
3631 Check_Fully_Declared (T, N);
3633 if Has_Delayed_Aspects (Id)
3634 and then Is_Array_Type (T)
3635 and then Is_Itype (T)
3637 Set_Has_Delayed_Freeze (T);
3639 Freeze_Before (N, T);
3643 -- If the object was created by a constrained array definition, then
3644 -- set the link in both the anonymous base type and anonymous subtype
3645 -- that are built to represent the array type to point to the object.
3647 if Nkind (Object_Definition (Declaration_Node (Id))) =
3648 N_Constrained_Array_Definition
3650 Set_Related_Array_Object (T, Id);
3651 Set_Related_Array_Object (Base_Type (T), Id);
3654 -- Special checks for protected objects not at library level
3656 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
3657 Check_Restriction (No_Local_Protected_Objects, Id);
3659 -- Protected objects with interrupt handlers must be at library level
3661 -- Ada 2005: This test is not needed (and the corresponding clause
3662 -- in the RM is removed) because accessibility checks are sufficient
3663 -- to make handlers not at the library level illegal.
3665 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3666 -- applies to the '95 version of the language as well.
3668 if Is_Protected_Type (T)
3669 and then Has_Interrupt_Handler (T)
3670 and then Ada_Version < Ada_95
3673 ("interrupt object can only be declared at library level", Id);
3677 -- Check for violation of No_Local_Timing_Events
3679 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
3680 Check_Restriction (No_Local_Timing_Events, Id);
3683 -- The actual subtype of the object is the nominal subtype, unless
3684 -- the nominal one is unconstrained and obtained from the expression.
3688 -- These checks should be performed before the initialization expression
3689 -- is considered, so that the Object_Definition node is still the same
3690 -- as in source code.
3692 -- In SPARK, the nominal subtype is always given by a subtype mark
3693 -- and must not be unconstrained. (The only exception to this is the
3694 -- acceptance of declarations of constants of type String.)
3696 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3698 Check_SPARK_05_Restriction
3699 ("subtype mark required", Object_Definition (N));
3701 elsif Is_Array_Type (T)
3702 and then not Is_Constrained (T)
3703 and then T /= Standard_String
3705 Check_SPARK_05_Restriction
3706 ("subtype mark of constrained type expected",
3707 Object_Definition (N));
3710 -- There are no aliased objects in SPARK
3712 if Aliased_Present (N) then
3713 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3716 -- Process initialization expression if present and not in error
3718 if Present (E) and then E /= Error then
3720 -- Generate an error in case of CPP class-wide object initialization.
3721 -- Required because otherwise the expansion of the class-wide
3722 -- assignment would try to use 'size to initialize the object
3723 -- (primitive that is not available in CPP tagged types).
3725 if Is_Class_Wide_Type (Act_T)
3727 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3729 (Present (Full_View (Root_Type (Etype (Act_T))))
3731 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3734 ("predefined assignment not available for 'C'P'P tagged types",
3738 Mark_Coextensions (N, E);
3741 -- In case of errors detected in the analysis of the expression,
3742 -- decorate it with the expected type to avoid cascaded errors
3744 if No (Etype (E)) then
3748 -- If an initialization expression is present, then we set the
3749 -- Is_True_Constant flag. It will be reset if this is a variable
3750 -- and it is indeed modified.
3752 Set_Is_True_Constant (Id, True);
3754 -- If we are analyzing a constant declaration, set its completion
3755 -- flag after analyzing and resolving the expression.
3757 if Constant_Present (N) then
3758 Set_Has_Completion (Id);
3761 -- Set type and resolve (type may be overridden later on). Note:
3762 -- Ekind (Id) must still be E_Void at this point so that incorrect
3763 -- early usage within E is properly diagnosed.
3767 -- If the expression is an aggregate we must look ahead to detect
3768 -- the possible presence of an address clause, and defer resolution
3769 -- and expansion of the aggregate to the freeze point of the entity.
3771 -- This is not always legal because the aggregate may contain other
3772 -- references that need freezing, e.g. references to other entities
3773 -- with address clauses. In any case, when compiling with -gnatI the
3774 -- presence of the address clause must be ignored.
3776 if Comes_From_Source (N)
3777 and then Expander_Active
3778 and then Nkind (E) = N_Aggregate
3780 ((Present (Following_Address_Clause (N))
3781 and then not Ignore_Rep_Clauses)
3782 or else Delayed_Aspect_Present)
3790 -- No further action needed if E is a call to an inlined function
3791 -- which returns an unconstrained type and it has been expanded into
3792 -- a procedure call. In that case N has been replaced by an object
3793 -- declaration without initializing expression and it has been
3794 -- analyzed (see Expand_Inlined_Call).
3796 if Back_End_Inlining
3797 and then Expander_Active
3798 and then Nkind (E) = N_Function_Call
3799 and then Nkind (Name (E)) in N_Has_Entity
3800 and then Is_Inlined (Entity (Name (E)))
3801 and then not Is_Constrained (Etype (E))
3802 and then Analyzed (N)
3803 and then No (Expression (N))
3805 Ghost_Mode := Save_Ghost_Mode;
3809 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3810 -- node (which was marked already-analyzed), we need to set the type
3811 -- to something other than Any_Access in order to keep gigi happy.
3813 if Etype (E) = Any_Access then
3817 -- If the object is an access to variable, the initialization
3818 -- expression cannot be an access to constant.
3820 if Is_Access_Type (T)
3821 and then not Is_Access_Constant (T)
3822 and then Is_Access_Type (Etype (E))
3823 and then Is_Access_Constant (Etype (E))
3826 ("access to variable cannot be initialized with an "
3827 & "access-to-constant expression", E);
3830 if not Assignment_OK (N) then
3831 Check_Initialization (T, E);
3834 Check_Unset_Reference (E);
3836 -- If this is a variable, then set current value. If this is a
3837 -- declared constant of a scalar type with a static expression,
3838 -- indicate that it is always valid.
3840 if not Constant_Present (N) then
3841 if Compile_Time_Known_Value (E) then
3842 Set_Current_Value (Id, E);
3845 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
3846 Set_Is_Known_Valid (Id);
3849 -- Deal with setting of null flags
3851 if Is_Access_Type (T) then
3852 if Known_Non_Null (E) then
3853 Set_Is_Known_Non_Null (Id, True);
3854 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
3855 Set_Is_Known_Null (Id, True);
3859 -- Check incorrect use of dynamically tagged expressions
3861 if Is_Tagged_Type (T) then
3862 Check_Dynamically_Tagged_Expression
3868 Apply_Scalar_Range_Check (E, T);
3869 Apply_Static_Length_Check (E, T);
3871 if Nkind (Original_Node (N)) = N_Object_Declaration
3872 and then Comes_From_Source (Original_Node (N))
3874 -- Only call test if needed
3876 and then Restriction_Check_Required (SPARK_05)
3877 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
3879 Check_SPARK_05_Restriction
3880 ("initialization expression is not appropriate", E);
3883 -- A formal parameter of a specific tagged type whose related
3884 -- subprogram is subject to pragma Extensions_Visible with value
3885 -- "False" cannot be implicitly converted to a class-wide type by
3886 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
3887 -- not consider internally generated expressions.
3889 if Is_Class_Wide_Type (T)
3890 and then Comes_From_Source (E)
3891 and then Is_EVF_Expression (E)
3894 ("formal parameter cannot be implicitly converted to "
3895 & "class-wide type when Extensions_Visible is False", E);
3899 -- If the No_Streams restriction is set, check that the type of the
3900 -- object is not, and does not contain, any subtype derived from
3901 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3902 -- Has_Stream just for efficiency reasons. There is no point in
3903 -- spending time on a Has_Stream check if the restriction is not set.
3905 if Restriction_Check_Required (No_Streams) then
3906 if Has_Stream (T) then
3907 Check_Restriction (No_Streams, N);
3911 -- Deal with predicate check before we start to do major rewriting. It
3912 -- is OK to initialize and then check the initialized value, since the
3913 -- object goes out of scope if we get a predicate failure. Note that we
3914 -- do this in the analyzer and not the expander because the analyzer
3915 -- does some substantial rewriting in some cases.
3917 -- We need a predicate check if the type has predicates that are not
3918 -- ignored, and if either there is an initializing expression, or for
3919 -- default initialization when we have at least one case of an explicit
3920 -- default initial value and then this is not an internal declaration
3921 -- whose initialization comes later (as for an aggregate expansion).
3923 if not Suppress_Assignment_Checks (N)
3924 and then Present (Predicate_Function (T))
3925 and then not Predicates_Ignored (T)
3926 and then not No_Initialization (N)
3930 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3932 -- If the type has a static predicate and the expression is known at
3933 -- compile time, see if the expression satisfies the predicate.
3936 Check_Expression_Against_Static_Predicate (E, T);
3939 -- If the type is a null record and there is no explicit initial
3940 -- expression, no predicate check applies.
3942 if No (E) and then Is_Null_Record_Type (T) then
3947 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3951 -- Case of unconstrained type
3953 if not Is_Definite_Subtype (T) then
3955 -- In SPARK, a declaration of unconstrained type is allowed
3956 -- only for constants of type string.
3958 if Is_String_Type (T) and then not Constant_Present (N) then
3959 Check_SPARK_05_Restriction
3960 ("declaration of object of unconstrained type not allowed", N);
3963 -- Nothing to do in deferred constant case
3965 if Constant_Present (N) and then No (E) then
3968 -- Case of no initialization present
3971 if No_Initialization (N) then
3974 elsif Is_Class_Wide_Type (T) then
3976 ("initialization required in class-wide declaration ", N);
3980 ("unconstrained subtype not allowed (need initialization)",
3981 Object_Definition (N));
3983 if Is_Record_Type (T) and then Has_Discriminants (T) then
3985 ("\provide initial value or explicit discriminant values",
3986 Object_Definition (N));
3989 ("\or give default discriminant values for type&",
3990 Object_Definition (N), T);
3992 elsif Is_Array_Type (T) then
3994 ("\provide initial value or explicit array bounds",
3995 Object_Definition (N));
3999 -- Case of initialization present but in error. Set initial
4000 -- expression as absent (but do not make above complaints)
4002 elsif E = Error then
4003 Set_Expression (N, Empty);
4006 -- Case of initialization present
4009 -- Check restrictions in Ada 83
4011 if not Constant_Present (N) then
4013 -- Unconstrained variables not allowed in Ada 83 mode
4015 if Ada_Version = Ada_83
4016 and then Comes_From_Source (Object_Definition (N))
4019 ("(Ada 83) unconstrained variable not allowed",
4020 Object_Definition (N));
4024 -- Now we constrain the variable from the initializing expression
4026 -- If the expression is an aggregate, it has been expanded into
4027 -- individual assignments. Retrieve the actual type from the
4028 -- expanded construct.
4030 if Is_Array_Type (T)
4031 and then No_Initialization (N)
4032 and then Nkind (Original_Node (E)) = N_Aggregate
4036 -- In case of class-wide interface object declarations we delay
4037 -- the generation of the equivalent record type declarations until
4038 -- its expansion because there are cases in they are not required.
4040 elsif Is_Interface (T) then
4043 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4044 -- we should prevent the generation of another Itype with the
4045 -- same name as the one already generated, or we end up with
4046 -- two identical types in GNATprove.
4048 elsif GNATprove_Mode then
4051 -- If the type is an unchecked union, no subtype can be built from
4052 -- the expression. Rewrite declaration as a renaming, which the
4053 -- back-end can handle properly. This is a rather unusual case,
4054 -- because most unchecked_union declarations have default values
4055 -- for discriminants and are thus not indefinite.
4057 elsif Is_Unchecked_Union (T) then
4058 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4059 Set_Ekind (Id, E_Constant);
4061 Set_Ekind (Id, E_Variable);
4064 -- An object declared within a Ghost region is automatically
4065 -- Ghost (SPARK RM 6.9(2)).
4067 if Ghost_Mode > None then
4068 Set_Is_Ghost_Entity (Id);
4070 -- The Ghost policy in effect at the point of declaration
4071 -- and at the point of completion must match
4072 -- (SPARK RM 6.9(14)).
4074 if Present (Prev_Entity)
4075 and then Is_Ghost_Entity (Prev_Entity)
4077 Check_Ghost_Completion (Prev_Entity, Id);
4082 Make_Object_Renaming_Declaration (Loc,
4083 Defining_Identifier => Id,
4084 Subtype_Mark => New_Occurrence_Of (T, Loc),
4087 Set_Renamed_Object (Id, E);
4088 Freeze_Before (N, T);
4091 Ghost_Mode := Save_Ghost_Mode;
4095 -- Ensure that the generated subtype has a unique external name
4096 -- when the related object is public. This guarantees that the
4097 -- subtype and its bounds will not be affected by switches or
4098 -- pragmas that may offset the internal counter due to extra
4101 if Is_Public (Id) then
4104 Related_Id := Empty;
4107 Expand_Subtype_From_Expr
4110 Subtype_Indic => Object_Definition (N),
4112 Related_Id => Related_Id);
4114 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4117 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4119 if Aliased_Present (N) then
4120 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4123 Freeze_Before (N, Act_T);
4124 Freeze_Before (N, T);
4127 elsif Is_Array_Type (T)
4128 and then No_Initialization (N)
4129 and then (Nkind (Original_Node (E)) = N_Aggregate
4130 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4131 and then Nkind (Original_Node (Expression
4132 (Original_Node (E)))) = N_Aggregate))
4134 if not Is_Entity_Name (Object_Definition (N)) then
4136 Check_Compile_Time_Size (Act_T);
4138 if Aliased_Present (N) then
4139 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4143 -- When the given object definition and the aggregate are specified
4144 -- independently, and their lengths might differ do a length check.
4145 -- This cannot happen if the aggregate is of the form (others =>...)
4147 if not Is_Constrained (T) then
4150 elsif Nkind (E) = N_Raise_Constraint_Error then
4152 -- Aggregate is statically illegal. Place back in declaration
4154 Set_Expression (N, E);
4155 Set_No_Initialization (N, False);
4157 elsif T = Etype (E) then
4160 elsif Nkind (E) = N_Aggregate
4161 and then Present (Component_Associations (E))
4162 and then Present (Choices (First (Component_Associations (E))))
4163 and then Nkind (First
4164 (Choices (First (Component_Associations (E))))) = N_Others_Choice
4169 Apply_Length_Check (E, T);
4172 -- If the type is limited unconstrained with defaulted discriminants and
4173 -- there is no expression, then the object is constrained by the
4174 -- defaults, so it is worthwhile building the corresponding subtype.
4176 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4177 and then not Is_Constrained (T)
4178 and then Has_Discriminants (T)
4181 Act_T := Build_Default_Subtype (T, N);
4183 -- Ada 2005: A limited object may be initialized by means of an
4184 -- aggregate. If the type has default discriminants it has an
4185 -- unconstrained nominal type, Its actual subtype will be obtained
4186 -- from the aggregate, and not from the default discriminants.
4191 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4193 elsif Nkind (E) = N_Function_Call
4194 and then Constant_Present (N)
4195 and then Has_Unconstrained_Elements (Etype (E))
4197 -- The back-end has problems with constants of a discriminated type
4198 -- with defaults, if the initial value is a function call. We
4199 -- generate an intermediate temporary that will receive a reference
4200 -- to the result of the call. The initialization expression then
4201 -- becomes a dereference of that temporary.
4203 Remove_Side_Effects (E);
4205 -- If this is a constant declaration of an unconstrained type and
4206 -- the initialization is an aggregate, we can use the subtype of the
4207 -- aggregate for the declared entity because it is immutable.
4209 elsif not Is_Constrained (T)
4210 and then Has_Discriminants (T)
4211 and then Constant_Present (N)
4212 and then not Has_Unchecked_Union (T)
4213 and then Nkind (E) = N_Aggregate
4218 -- Check No_Wide_Characters restriction
4220 Check_Wide_Character_Restriction (T, Object_Definition (N));
4222 -- Indicate this is not set in source. Certainly true for constants, and
4223 -- true for variables so far (will be reset for a variable if and when
4224 -- we encounter a modification in the source).
4226 Set_Never_Set_In_Source (Id);
4228 -- Now establish the proper kind and type of the object
4230 if Constant_Present (N) then
4231 Set_Ekind (Id, E_Constant);
4232 Set_Is_True_Constant (Id);
4235 Set_Ekind (Id, E_Variable);
4237 -- A variable is set as shared passive if it appears in a shared
4238 -- passive package, and is at the outer level. This is not done for
4239 -- entities generated during expansion, because those are always
4240 -- manipulated locally.
4242 if Is_Shared_Passive (Current_Scope)
4243 and then Is_Library_Level_Entity (Id)
4244 and then Comes_From_Source (Id)
4246 Set_Is_Shared_Passive (Id);
4247 Check_Shared_Var (Id, T, N);
4250 -- Set Has_Initial_Value if initializing expression present. Note
4251 -- that if there is no initializing expression, we leave the state
4252 -- of this flag unchanged (usually it will be False, but notably in
4253 -- the case of exception choice variables, it will already be true).
4256 Set_Has_Initial_Value (Id);
4260 -- Initialize alignment and size and capture alignment setting
4262 Init_Alignment (Id);
4264 Set_Optimize_Alignment_Flags (Id);
4266 -- An object declared within a Ghost region is automatically Ghost
4267 -- (SPARK RM 6.9(2)).
4269 if Ghost_Mode > None
4270 or else (Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity))
4272 Set_Is_Ghost_Entity (Id);
4274 -- The Ghost policy in effect at the point of declaration and at the
4275 -- point of completion must match (SPARK RM 6.9(14)).
4277 if Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity) then
4278 Check_Ghost_Completion (Prev_Entity, Id);
4282 -- Deal with aliased case
4284 if Aliased_Present (N) then
4285 Set_Is_Aliased (Id);
4287 -- If the object is aliased and the type is unconstrained with
4288 -- defaulted discriminants and there is no expression, then the
4289 -- object is constrained by the defaults, so it is worthwhile
4290 -- building the corresponding subtype.
4292 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4293 -- unconstrained, then only establish an actual subtype if the
4294 -- nominal subtype is indefinite. In definite cases the object is
4295 -- unconstrained in Ada 2005.
4298 and then Is_Record_Type (T)
4299 and then not Is_Constrained (T)
4300 and then Has_Discriminants (T)
4301 and then (Ada_Version < Ada_2005
4302 or else not Is_Definite_Subtype (T))
4304 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4308 -- Now we can set the type of the object
4310 Set_Etype (Id, Act_T);
4312 -- Non-constant object is marked to be treated as volatile if type is
4313 -- volatile and we clear the Current_Value setting that may have been
4314 -- set above. Doing so for constants isn't required and might interfere
4315 -- with possible uses of the object as a static expression in contexts
4316 -- incompatible with volatility (e.g. as a case-statement alternative).
4318 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4319 Set_Treat_As_Volatile (Id);
4320 Set_Current_Value (Id, Empty);
4323 -- Deal with controlled types
4325 if Has_Controlled_Component (Etype (Id))
4326 or else Is_Controlled (Etype (Id))
4328 if not Is_Library_Level_Entity (Id) then
4329 Check_Restriction (No_Nested_Finalization, N);
4331 Validate_Controlled_Object (Id);
4335 if Has_Task (Etype (Id)) then
4336 Check_Restriction (No_Tasking, N);
4338 -- Deal with counting max tasks
4340 -- Nothing to do if inside a generic
4342 if Inside_A_Generic then
4345 -- If library level entity, then count tasks
4347 elsif Is_Library_Level_Entity (Id) then
4348 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4350 -- If not library level entity, then indicate we don't know max
4351 -- tasks and also check task hierarchy restriction and blocking
4352 -- operation (since starting a task is definitely blocking).
4355 Check_Restriction (Max_Tasks, N);
4356 Check_Restriction (No_Task_Hierarchy, N);
4357 Check_Potentially_Blocking_Operation (N);
4360 -- A rather specialized test. If we see two tasks being declared
4361 -- of the same type in the same object declaration, and the task
4362 -- has an entry with an address clause, we know that program error
4363 -- will be raised at run time since we can't have two tasks with
4364 -- entries at the same address.
4366 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4371 E := First_Entity (Etype (Id));
4372 while Present (E) loop
4373 if Ekind (E) = E_Entry
4374 and then Present (Get_Attribute_Definition_Clause
4375 (E, Attribute_Address))
4377 Error_Msg_Warn := SPARK_Mode /= On;
4379 ("more than one task with same entry address<<", N);
4380 Error_Msg_N ("\Program_Error [<<", N);
4382 Make_Raise_Program_Error (Loc,
4383 Reason => PE_Duplicated_Entry_Address));
4393 -- Some simple constant-propagation: if the expression is a constant
4394 -- string initialized with a literal, share the literal. This avoids
4398 and then Is_Entity_Name (E)
4399 and then Ekind (Entity (E)) = E_Constant
4400 and then Base_Type (Etype (E)) = Standard_String
4403 Val : constant Node_Id := Constant_Value (Entity (E));
4405 if Present (Val) and then Nkind (Val) = N_String_Literal then
4406 Rewrite (E, New_Copy (Val));
4411 -- Another optimization: if the nominal subtype is unconstrained and
4412 -- the expression is a function call that returns an unconstrained
4413 -- type, rewrite the declaration as a renaming of the result of the
4414 -- call. The exceptions below are cases where the copy is expected,
4415 -- either by the back end (Aliased case) or by the semantics, as for
4416 -- initializing controlled types or copying tags for class-wide types.
4419 and then Nkind (E) = N_Explicit_Dereference
4420 and then Nkind (Original_Node (E)) = N_Function_Call
4421 and then not Is_Library_Level_Entity (Id)
4422 and then not Is_Constrained (Underlying_Type (T))
4423 and then not Is_Aliased (Id)
4424 and then not Is_Class_Wide_Type (T)
4425 and then not Is_Controlled_Active (T)
4426 and then not Has_Controlled_Component (Base_Type (T))
4427 and then Expander_Active
4430 Make_Object_Renaming_Declaration (Loc,
4431 Defining_Identifier => Id,
4432 Access_Definition => Empty,
4433 Subtype_Mark => New_Occurrence_Of
4434 (Base_Type (Etype (Id)), Loc),
4437 Set_Renamed_Object (Id, E);
4439 -- Force generation of debugging information for the constant and for
4440 -- the renamed function call.
4442 Set_Debug_Info_Needed (Id);
4443 Set_Debug_Info_Needed (Entity (Prefix (E)));
4446 if Present (Prev_Entity)
4447 and then Is_Frozen (Prev_Entity)
4448 and then not Error_Posted (Id)
4450 Error_Msg_N ("full constant declaration appears too late", N);
4453 Check_Eliminated (Id);
4455 -- Deal with setting In_Private_Part flag if in private part
4457 if Ekind (Scope (Id)) = E_Package
4458 and then In_Private_Part (Scope (Id))
4460 Set_In_Private_Part (Id);
4464 -- Initialize the refined state of a variable here because this is a
4465 -- common destination for legal and illegal object declarations.
4467 if Ekind (Id) = E_Variable then
4468 Set_Encapsulating_State (Id, Empty);
4471 if Has_Aspects (N) then
4472 Analyze_Aspect_Specifications (N, Id);
4475 Analyze_Dimension (N);
4477 -- Verify whether the object declaration introduces an illegal hidden
4478 -- state within a package subject to a null abstract state.
4480 if Ekind (Id) = E_Variable then
4481 Check_No_Hidden_State (Id);
4484 Ghost_Mode := Save_Ghost_Mode;
4485 end Analyze_Object_Declaration;
4487 ---------------------------
4488 -- Analyze_Others_Choice --
4489 ---------------------------
4491 -- Nothing to do for the others choice node itself, the semantic analysis
4492 -- of the others choice will occur as part of the processing of the parent
4494 procedure Analyze_Others_Choice (N : Node_Id) is
4495 pragma Warnings (Off, N);
4498 end Analyze_Others_Choice;
4500 -------------------------------------------
4501 -- Analyze_Private_Extension_Declaration --
4502 -------------------------------------------
4504 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4505 Indic : constant Node_Id := Subtype_Indication (N);
4506 T : constant Entity_Id := Defining_Identifier (N);
4508 Iface_Elmt : Elmt_Id;
4509 Parent_Base : Entity_Id;
4510 Parent_Type : Entity_Id;
4513 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4515 if Is_Non_Empty_List (Interface_List (N)) then
4521 Intf := First (Interface_List (N));
4522 while Present (Intf) loop
4523 T := Find_Type_Of_Subtype_Indic (Intf);
4525 Diagnose_Interface (Intf, T);
4531 Generate_Definition (T);
4533 -- For other than Ada 2012, just enter the name in the current scope
4535 if Ada_Version < Ada_2012 then
4538 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4539 -- case of private type that completes an incomplete type.
4546 Prev := Find_Type_Name (N);
4548 pragma Assert (Prev = T
4549 or else (Ekind (Prev) = E_Incomplete_Type
4550 and then Present (Full_View (Prev))
4551 and then Full_View (Prev) = T));
4555 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4556 Parent_Base := Base_Type (Parent_Type);
4558 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4559 Set_Ekind (T, Ekind (Parent_Type));
4560 Set_Etype (T, Any_Type);
4563 elsif not Is_Tagged_Type (Parent_Type) then
4565 ("parent of type extension must be a tagged type ", Indic);
4568 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4569 Error_Msg_N ("premature derivation of incomplete type", Indic);
4572 elsif Is_Concurrent_Type (Parent_Type) then
4574 ("parent type of a private extension cannot be a synchronized "
4575 & "tagged type (RM 3.9.1 (3/1))", N);
4577 Set_Etype (T, Any_Type);
4578 Set_Ekind (T, E_Limited_Private_Type);
4579 Set_Private_Dependents (T, New_Elmt_List);
4580 Set_Error_Posted (T);
4584 -- Perhaps the parent type should be changed to the class-wide type's
4585 -- specific type in this case to prevent cascading errors ???
4587 if Is_Class_Wide_Type (Parent_Type) then
4589 ("parent of type extension must not be a class-wide type", Indic);
4593 if (not Is_Package_Or_Generic_Package (Current_Scope)
4594 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4595 or else In_Private_Part (Current_Scope)
4597 Error_Msg_N ("invalid context for private extension", N);
4600 -- Set common attributes
4602 Set_Is_Pure (T, Is_Pure (Current_Scope));
4603 Set_Scope (T, Current_Scope);
4604 Set_Ekind (T, E_Record_Type_With_Private);
4605 Init_Size_Align (T);
4606 Set_Default_SSO (T);
4608 Set_Etype (T, Parent_Base);
4609 Propagate_Concurrent_Flags (T, Parent_Base);
4611 Set_Convention (T, Convention (Parent_Type));
4612 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4613 Set_Is_First_Subtype (T);
4614 Make_Class_Wide_Type (T);
4616 if Unknown_Discriminants_Present (N) then
4617 Set_Discriminant_Constraint (T, No_Elist);
4620 Build_Derived_Record_Type (N, Parent_Type, T);
4622 -- A private extension inherits the Default_Initial_Condition pragma
4623 -- coming from any parent type within the derivation chain.
4625 if Has_DIC (Parent_Type) then
4626 Set_Has_Inherited_DIC (T);
4629 -- A private extension inherits any class-wide invariants coming from a
4630 -- parent type or an interface. Note that the invariant procedure of the
4631 -- parent type should not be inherited because the private extension may
4632 -- define invariants of its own.
4634 if Has_Inherited_Invariants (Parent_Type)
4635 or else Has_Inheritable_Invariants (Parent_Type)
4637 Set_Has_Inherited_Invariants (T);
4639 elsif Present (Interfaces (T)) then
4640 Iface_Elmt := First_Elmt (Interfaces (T));
4641 while Present (Iface_Elmt) loop
4642 Iface := Node (Iface_Elmt);
4644 if Has_Inheritable_Invariants (Iface) then
4645 Set_Has_Inherited_Invariants (T);
4649 Next_Elmt (Iface_Elmt);
4653 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4654 -- synchronized formal derived type.
4656 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4657 Set_Is_Limited_Record (T);
4659 -- Formal derived type case
4661 if Is_Generic_Type (T) then
4663 -- The parent must be a tagged limited type or a synchronized
4666 if (not Is_Tagged_Type (Parent_Type)
4667 or else not Is_Limited_Type (Parent_Type))
4669 (not Is_Interface (Parent_Type)
4670 or else not Is_Synchronized_Interface (Parent_Type))
4673 ("parent type of & must be tagged limited or synchronized",
4677 -- The progenitors (if any) must be limited or synchronized
4680 if Present (Interfaces (T)) then
4681 Iface_Elmt := First_Elmt (Interfaces (T));
4682 while Present (Iface_Elmt) loop
4683 Iface := Node (Iface_Elmt);
4685 if not Is_Limited_Interface (Iface)
4686 and then not Is_Synchronized_Interface (Iface)
4689 ("progenitor & must be limited or synchronized",
4693 Next_Elmt (Iface_Elmt);
4697 -- Regular derived extension, the parent must be a limited or
4698 -- synchronized interface.
4701 if not Is_Interface (Parent_Type)
4702 or else (not Is_Limited_Interface (Parent_Type)
4703 and then not Is_Synchronized_Interface (Parent_Type))
4706 ("parent type of & must be limited interface", N, T);
4710 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4711 -- extension with a synchronized parent must be explicitly declared
4712 -- synchronized, because the full view will be a synchronized type.
4713 -- This must be checked before the check for limited types below,
4714 -- to ensure that types declared limited are not allowed to extend
4715 -- synchronized interfaces.
4717 elsif Is_Interface (Parent_Type)
4718 and then Is_Synchronized_Interface (Parent_Type)
4719 and then not Synchronized_Present (N)
4722 ("private extension of& must be explicitly synchronized",
4725 elsif Limited_Present (N) then
4726 Set_Is_Limited_Record (T);
4728 if not Is_Limited_Type (Parent_Type)
4730 (not Is_Interface (Parent_Type)
4731 or else not Is_Limited_Interface (Parent_Type))
4733 Error_Msg_NE ("parent type& of limited extension must be limited",
4739 if Has_Aspects (N) then
4740 Analyze_Aspect_Specifications (N, T);
4742 end Analyze_Private_Extension_Declaration;
4744 ---------------------------------
4745 -- Analyze_Subtype_Declaration --
4746 ---------------------------------
4748 procedure Analyze_Subtype_Declaration
4750 Skip : Boolean := False)
4752 Id : constant Entity_Id := Defining_Identifier (N);
4753 R_Checks : Check_Result;
4757 Generate_Definition (Id);
4758 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4759 Init_Size_Align (Id);
4761 -- The following guard condition on Enter_Name is to handle cases where
4762 -- the defining identifier has already been entered into the scope but
4763 -- the declaration as a whole needs to be analyzed.
4765 -- This case in particular happens for derived enumeration types. The
4766 -- derived enumeration type is processed as an inserted enumeration type
4767 -- declaration followed by a rewritten subtype declaration. The defining
4768 -- identifier, however, is entered into the name scope very early in the
4769 -- processing of the original type declaration and therefore needs to be
4770 -- avoided here, when the created subtype declaration is analyzed. (See
4771 -- Build_Derived_Types)
4773 -- This also happens when the full view of a private type is derived
4774 -- type with constraints. In this case the entity has been introduced
4775 -- in the private declaration.
4777 -- Finally this happens in some complex cases when validity checks are
4778 -- enabled, where the same subtype declaration may be analyzed twice.
4779 -- This can happen if the subtype is created by the pre-analysis of
4780 -- an attribute tht gives the range of a loop statement, and the loop
4781 -- itself appears within an if_statement that will be rewritten during
4785 or else (Present (Etype (Id))
4786 and then (Is_Private_Type (Etype (Id))
4787 or else Is_Task_Type (Etype (Id))
4788 or else Is_Rewrite_Substitution (N)))
4792 elsif Current_Entity (Id) = Id then
4799 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4801 -- Class-wide equivalent types of records with unknown discriminants
4802 -- involve the generation of an itype which serves as the private view
4803 -- of a constrained record subtype. In such cases the base type of the
4804 -- current subtype we are processing is the private itype. Use the full
4805 -- of the private itype when decorating various attributes.
4808 and then Is_Private_Type (T)
4809 and then Present (Full_View (T))
4814 -- Inherit common attributes
4816 Set_Is_Volatile (Id, Is_Volatile (T));
4817 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4818 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4819 Set_Convention (Id, Convention (T));
4821 -- If ancestor has predicates then so does the subtype, and in addition
4822 -- we must delay the freeze to properly arrange predicate inheritance.
4824 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4825 -- in which T = ID, so the above tests and assignments do nothing???
4827 if Has_Predicates (T)
4828 or else (Present (Ancestor_Subtype (T))
4829 and then Has_Predicates (Ancestor_Subtype (T)))
4831 Set_Has_Predicates (Id);
4832 Set_Has_Delayed_Freeze (Id);
4834 -- Generated subtypes inherit the predicate function from the parent
4835 -- (no aspects to examine on the generated declaration).
4837 if not Comes_From_Source (N) then
4838 Set_Ekind (Id, Ekind (T));
4840 if Present (Predicate_Function (T)) then
4841 Set_Predicate_Function (Id, Predicate_Function (T));
4843 elsif Present (Ancestor_Subtype (T))
4844 and then Has_Predicates (Ancestor_Subtype (T))
4845 and then Present (Predicate_Function (Ancestor_Subtype (T)))
4847 Set_Predicate_Function (Id,
4848 Predicate_Function (Ancestor_Subtype (T)));
4853 -- Subtype of Boolean cannot have a constraint in SPARK
4855 if Is_Boolean_Type (T)
4856 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4858 Check_SPARK_05_Restriction
4859 ("subtype of Boolean cannot have constraint", N);
4862 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4864 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4870 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4871 One_Cstr := First (Constraints (Cstr));
4872 while Present (One_Cstr) loop
4874 -- Index or discriminant constraint in SPARK must be a
4878 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4880 Check_SPARK_05_Restriction
4881 ("subtype mark required", One_Cstr);
4883 -- String subtype must have a lower bound of 1 in SPARK.
4884 -- Note that we do not need to test for the non-static case
4885 -- here, since that was already taken care of in
4886 -- Process_Range_Expr_In_Decl.
4888 elsif Base_Type (T) = Standard_String then
4889 Get_Index_Bounds (One_Cstr, Low, High);
4891 if Is_OK_Static_Expression (Low)
4892 and then Expr_Value (Low) /= 1
4894 Check_SPARK_05_Restriction
4895 ("String subtype must have lower bound of 1", N);
4905 -- In the case where there is no constraint given in the subtype
4906 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4907 -- semantic attributes must be established here.
4909 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4910 Set_Etype (Id, Base_Type (T));
4912 -- Subtype of unconstrained array without constraint is not allowed
4915 if Is_Array_Type (T) and then not Is_Constrained (T) then
4916 Check_SPARK_05_Restriction
4917 ("subtype of unconstrained array must have constraint", N);
4922 Set_Ekind (Id, E_Array_Subtype);
4923 Copy_Array_Subtype_Attributes (Id, T);
4925 when Decimal_Fixed_Point_Kind =>
4926 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4927 Set_Digits_Value (Id, Digits_Value (T));
4928 Set_Delta_Value (Id, Delta_Value (T));
4929 Set_Scale_Value (Id, Scale_Value (T));
4930 Set_Small_Value (Id, Small_Value (T));
4931 Set_Scalar_Range (Id, Scalar_Range (T));
4932 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4933 Set_Is_Constrained (Id, Is_Constrained (T));
4934 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4935 Set_RM_Size (Id, RM_Size (T));
4937 when Enumeration_Kind =>
4938 Set_Ekind (Id, E_Enumeration_Subtype);
4939 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4940 Set_Scalar_Range (Id, Scalar_Range (T));
4941 Set_Is_Character_Type (Id, Is_Character_Type (T));
4942 Set_Is_Constrained (Id, Is_Constrained (T));
4943 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4944 Set_RM_Size (Id, RM_Size (T));
4945 Inherit_Predicate_Flags (Id, T);
4947 when Ordinary_Fixed_Point_Kind =>
4948 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4949 Set_Scalar_Range (Id, Scalar_Range (T));
4950 Set_Small_Value (Id, Small_Value (T));
4951 Set_Delta_Value (Id, Delta_Value (T));
4952 Set_Is_Constrained (Id, Is_Constrained (T));
4953 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4954 Set_RM_Size (Id, RM_Size (T));
4957 Set_Ekind (Id, E_Floating_Point_Subtype);
4958 Set_Scalar_Range (Id, Scalar_Range (T));
4959 Set_Digits_Value (Id, Digits_Value (T));
4960 Set_Is_Constrained (Id, Is_Constrained (T));
4962 -- If the floating point type has dimensions, these will be
4963 -- inherited subsequently when Analyze_Dimensions is called.
4965 when Signed_Integer_Kind =>
4966 Set_Ekind (Id, E_Signed_Integer_Subtype);
4967 Set_Scalar_Range (Id, Scalar_Range (T));
4968 Set_Is_Constrained (Id, Is_Constrained (T));
4969 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4970 Set_RM_Size (Id, RM_Size (T));
4971 Inherit_Predicate_Flags (Id, T);
4973 when Modular_Integer_Kind =>
4974 Set_Ekind (Id, E_Modular_Integer_Subtype);
4975 Set_Scalar_Range (Id, Scalar_Range (T));
4976 Set_Is_Constrained (Id, Is_Constrained (T));
4977 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4978 Set_RM_Size (Id, RM_Size (T));
4979 Inherit_Predicate_Flags (Id, T);
4981 when Class_Wide_Kind =>
4982 Set_Ekind (Id, E_Class_Wide_Subtype);
4983 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4984 Set_Cloned_Subtype (Id, T);
4985 Set_Is_Tagged_Type (Id, True);
4986 Set_Has_Unknown_Discriminants
4988 Set_No_Tagged_Streams_Pragma
4989 (Id, No_Tagged_Streams_Pragma (T));
4991 if Ekind (T) = E_Class_Wide_Subtype then
4992 Set_Equivalent_Type (Id, Equivalent_Type (T));
4995 when E_Record_Type | E_Record_Subtype =>
4996 Set_Ekind (Id, E_Record_Subtype);
4998 if Ekind (T) = E_Record_Subtype
4999 and then Present (Cloned_Subtype (T))
5001 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5003 Set_Cloned_Subtype (Id, T);
5006 Set_First_Entity (Id, First_Entity (T));
5007 Set_Last_Entity (Id, Last_Entity (T));
5008 Set_Has_Discriminants (Id, Has_Discriminants (T));
5009 Set_Is_Constrained (Id, Is_Constrained (T));
5010 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5011 Set_Has_Implicit_Dereference
5012 (Id, Has_Implicit_Dereference (T));
5013 Set_Has_Unknown_Discriminants
5014 (Id, Has_Unknown_Discriminants (T));
5016 if Has_Discriminants (T) then
5017 Set_Discriminant_Constraint
5018 (Id, Discriminant_Constraint (T));
5019 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5021 elsif Has_Unknown_Discriminants (Id) then
5022 Set_Discriminant_Constraint (Id, No_Elist);
5025 if Is_Tagged_Type (T) then
5026 Set_Is_Tagged_Type (Id, True);
5027 Set_No_Tagged_Streams_Pragma
5028 (Id, No_Tagged_Streams_Pragma (T));
5029 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5030 Set_Direct_Primitive_Operations
5031 (Id, Direct_Primitive_Operations (T));
5032 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5034 if Is_Interface (T) then
5035 Set_Is_Interface (Id);
5036 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5040 when Private_Kind =>
5041 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5042 Set_Has_Discriminants (Id, Has_Discriminants (T));
5043 Set_Is_Constrained (Id, Is_Constrained (T));
5044 Set_First_Entity (Id, First_Entity (T));
5045 Set_Last_Entity (Id, Last_Entity (T));
5046 Set_Private_Dependents (Id, New_Elmt_List);
5047 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5048 Set_Has_Implicit_Dereference
5049 (Id, Has_Implicit_Dereference (T));
5050 Set_Has_Unknown_Discriminants
5051 (Id, Has_Unknown_Discriminants (T));
5052 Set_Known_To_Have_Preelab_Init
5053 (Id, Known_To_Have_Preelab_Init (T));
5055 if Is_Tagged_Type (T) then
5056 Set_Is_Tagged_Type (Id);
5057 Set_No_Tagged_Streams_Pragma (Id,
5058 No_Tagged_Streams_Pragma (T));
5059 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5060 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5061 Set_Direct_Primitive_Operations (Id,
5062 Direct_Primitive_Operations (T));
5065 -- In general the attributes of the subtype of a private type
5066 -- are the attributes of the partial view of parent. However,
5067 -- the full view may be a discriminated type, and the subtype
5068 -- must share the discriminant constraint to generate correct
5069 -- calls to initialization procedures.
5071 if Has_Discriminants (T) then
5072 Set_Discriminant_Constraint
5073 (Id, Discriminant_Constraint (T));
5074 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5076 elsif Present (Full_View (T))
5077 and then Has_Discriminants (Full_View (T))
5079 Set_Discriminant_Constraint
5080 (Id, Discriminant_Constraint (Full_View (T)));
5081 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5083 -- This would seem semantically correct, but apparently
5084 -- generates spurious errors about missing components ???
5086 -- Set_Has_Discriminants (Id);
5089 Prepare_Private_Subtype_Completion (Id, N);
5091 -- If this is the subtype of a constrained private type with
5092 -- discriminants that has got a full view and we also have
5093 -- built a completion just above, show that the completion
5094 -- is a clone of the full view to the back-end.
5096 if Has_Discriminants (T)
5097 and then not Has_Unknown_Discriminants (T)
5098 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5099 and then Present (Full_View (T))
5100 and then Present (Full_View (Id))
5102 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5106 Set_Ekind (Id, E_Access_Subtype);
5107 Set_Is_Constrained (Id, Is_Constrained (T));
5108 Set_Is_Access_Constant
5109 (Id, Is_Access_Constant (T));
5110 Set_Directly_Designated_Type
5111 (Id, Designated_Type (T));
5112 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5114 -- A Pure library_item must not contain the declaration of a
5115 -- named access type, except within a subprogram, generic
5116 -- subprogram, task unit, or protected unit, or if it has
5117 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5119 if Comes_From_Source (Id)
5120 and then In_Pure_Unit
5121 and then not In_Subprogram_Task_Protected_Unit
5122 and then not No_Pool_Assigned (Id)
5125 ("named access types not allowed in pure unit", N);
5128 when Concurrent_Kind =>
5129 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5130 Set_Corresponding_Record_Type (Id,
5131 Corresponding_Record_Type (T));
5132 Set_First_Entity (Id, First_Entity (T));
5133 Set_First_Private_Entity (Id, First_Private_Entity (T));
5134 Set_Has_Discriminants (Id, Has_Discriminants (T));
5135 Set_Is_Constrained (Id, Is_Constrained (T));
5136 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5137 Set_Last_Entity (Id, Last_Entity (T));
5139 if Is_Tagged_Type (T) then
5140 Set_No_Tagged_Streams_Pragma
5141 (Id, No_Tagged_Streams_Pragma (T));
5144 if Has_Discriminants (T) then
5145 Set_Discriminant_Constraint
5146 (Id, Discriminant_Constraint (T));
5147 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5150 when Incomplete_Kind =>
5151 if Ada_Version >= Ada_2005 then
5153 -- In Ada 2005 an incomplete type can be explicitly tagged:
5154 -- propagate indication. Note that we also have to include
5155 -- subtypes for Ada 2012 extended use of incomplete types.
5157 Set_Ekind (Id, E_Incomplete_Subtype);
5158 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5159 Set_Private_Dependents (Id, New_Elmt_List);
5161 if Is_Tagged_Type (Id) then
5162 Set_No_Tagged_Streams_Pragma
5163 (Id, No_Tagged_Streams_Pragma (T));
5164 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5167 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5168 -- incomplete type visible through a limited with clause.
5170 if From_Limited_With (T)
5171 and then Present (Non_Limited_View (T))
5173 Set_From_Limited_With (Id);
5174 Set_Non_Limited_View (Id, Non_Limited_View (T));
5176 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5177 -- to the private dependents of the original incomplete
5178 -- type for future transformation.
5181 Append_Elmt (Id, Private_Dependents (T));
5184 -- If the subtype name denotes an incomplete type an error
5185 -- was already reported by Process_Subtype.
5188 Set_Etype (Id, Any_Type);
5192 raise Program_Error;
5196 if Etype (Id) = Any_Type then
5200 -- Some common processing on all types
5202 Set_Size_Info (Id, T);
5203 Set_First_Rep_Item (Id, First_Rep_Item (T));
5205 -- If the parent type is a generic actual, so is the subtype. This may
5206 -- happen in a nested instance. Why Comes_From_Source test???
5208 if not Comes_From_Source (N) then
5209 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5212 -- If this is a subtype declaration for an actual in an instance,
5213 -- inherit static and dynamic predicates if any.
5215 -- If declaration has no aspect specifications, inherit predicate
5216 -- info as well. Unclear how to handle the case of both specified
5217 -- and inherited predicates ??? Other inherited aspects, such as
5218 -- invariants, should be OK, but the combination with later pragmas
5219 -- may also require special merging.
5221 if Has_Predicates (T)
5222 and then Present (Predicate_Function (T))
5224 ((In_Instance and then not Comes_From_Source (N))
5225 or else No (Aspect_Specifications (N)))
5227 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5229 if Has_Static_Predicate (T) then
5230 Set_Has_Static_Predicate (Id);
5231 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5235 -- Remaining processing depends on characteristics of base type
5239 Set_Is_Immediately_Visible (Id, True);
5240 Set_Depends_On_Private (Id, Has_Private_Component (T));
5241 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5243 if Is_Interface (T) then
5244 Set_Is_Interface (Id);
5247 if Present (Generic_Parent_Type (N))
5249 (Nkind (Parent (Generic_Parent_Type (N))) /=
5250 N_Formal_Type_Declaration
5251 or else Nkind (Formal_Type_Definition
5252 (Parent (Generic_Parent_Type (N)))) /=
5253 N_Formal_Private_Type_Definition)
5255 if Is_Tagged_Type (Id) then
5257 -- If this is a generic actual subtype for a synchronized type,
5258 -- the primitive operations are those of the corresponding record
5259 -- for which there is a separate subtype declaration.
5261 if Is_Concurrent_Type (Id) then
5263 elsif Is_Class_Wide_Type (Id) then
5264 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5266 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5269 elsif Scope (Etype (Id)) /= Standard_Standard then
5270 Derive_Subprograms (Generic_Parent_Type (N), Id);
5274 if Is_Private_Type (T) and then Present (Full_View (T)) then
5275 Conditional_Delay (Id, Full_View (T));
5277 -- The subtypes of components or subcomponents of protected types
5278 -- do not need freeze nodes, which would otherwise appear in the
5279 -- wrong scope (before the freeze node for the protected type). The
5280 -- proper subtypes are those of the subcomponents of the corresponding
5283 elsif Ekind (Scope (Id)) /= E_Protected_Type
5284 and then Present (Scope (Scope (Id))) -- error defense
5285 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5287 Conditional_Delay (Id, T);
5290 -- Check that Constraint_Error is raised for a scalar subtype indication
5291 -- when the lower or upper bound of a non-null range lies outside the
5292 -- range of the type mark.
5294 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5295 if Is_Scalar_Type (Etype (Id))
5296 and then Scalar_Range (Id) /=
5298 (Etype (Subtype_Mark (Subtype_Indication (N))))
5302 Etype (Subtype_Mark (Subtype_Indication (N))));
5304 -- In the array case, check compatibility for each index
5306 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5308 -- This really should be a subprogram that finds the indications
5312 Subt_Index : Node_Id := First_Index (Id);
5313 Target_Index : Node_Id :=
5315 (Subtype_Mark (Subtype_Indication (N))));
5316 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5319 while Present (Subt_Index) loop
5320 if ((Nkind (Subt_Index) = N_Identifier
5321 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5322 or else Nkind (Subt_Index) = N_Subtype_Indication)
5324 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5327 Target_Typ : constant Entity_Id :=
5328 Etype (Target_Index);
5332 (Scalar_Range (Etype (Subt_Index)),
5335 Defining_Identifier (N));
5337 -- Reset Has_Dynamic_Range_Check on the subtype to
5338 -- prevent elision of the index check due to a dynamic
5339 -- check generated for a preceding index (needed since
5340 -- Insert_Range_Checks tries to avoid generating
5341 -- redundant checks on a given declaration).
5343 Set_Has_Dynamic_Range_Check (N, False);
5349 Sloc (Defining_Identifier (N)));
5351 -- Record whether this index involved a dynamic check
5354 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5358 Next_Index (Subt_Index);
5359 Next_Index (Target_Index);
5362 -- Finally, mark whether the subtype involves dynamic checks
5364 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5369 Set_Optimize_Alignment_Flags (Id);
5370 Check_Eliminated (Id);
5373 if Has_Aspects (N) then
5374 Analyze_Aspect_Specifications (N, Id);
5377 Analyze_Dimension (N);
5379 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5380 -- indications on composite types where the constraints are dynamic.
5381 -- Note that object declarations and aggregates generate implicit
5382 -- subtype declarations, which this covers. One special case is that the
5383 -- implicitly generated "=" for discriminated types includes an
5384 -- offending subtype declaration, which is harmless, so we ignore it
5387 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5389 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5391 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5392 and then not (Is_Internal (Id)
5393 and then Is_TSS (Scope (Id),
5394 TSS_Composite_Equality))
5395 and then not Within_Init_Proc
5396 and then not All_Composite_Constraints_Static (Cstr)
5398 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5402 end Analyze_Subtype_Declaration;
5404 --------------------------------
5405 -- Analyze_Subtype_Indication --
5406 --------------------------------
5408 procedure Analyze_Subtype_Indication (N : Node_Id) is
5409 T : constant Entity_Id := Subtype_Mark (N);
5410 R : constant Node_Id := Range_Expression (Constraint (N));
5417 Set_Etype (N, Etype (R));
5418 Resolve (R, Entity (T));
5420 Set_Error_Posted (R);
5421 Set_Error_Posted (T);
5423 end Analyze_Subtype_Indication;
5425 --------------------------
5426 -- Analyze_Variant_Part --
5427 --------------------------
5429 procedure Analyze_Variant_Part (N : Node_Id) is
5430 Discr_Name : Node_Id;
5431 Discr_Type : Entity_Id;
5433 procedure Process_Variant (A : Node_Id);
5434 -- Analyze declarations for a single variant
5436 package Analyze_Variant_Choices is
5437 new Generic_Analyze_Choices (Process_Variant);
5438 use Analyze_Variant_Choices;
5440 ---------------------
5441 -- Process_Variant --
5442 ---------------------
5444 procedure Process_Variant (A : Node_Id) is
5445 CL : constant Node_Id := Component_List (A);
5447 if not Null_Present (CL) then
5448 Analyze_Declarations (Component_Items (CL));
5450 if Present (Variant_Part (CL)) then
5451 Analyze (Variant_Part (CL));
5454 end Process_Variant;
5456 -- Start of processing for Analyze_Variant_Part
5459 Discr_Name := Name (N);
5460 Analyze (Discr_Name);
5462 -- If Discr_Name bad, get out (prevent cascaded errors)
5464 if Etype (Discr_Name) = Any_Type then
5468 -- Check invalid discriminant in variant part
5470 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5471 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5474 Discr_Type := Etype (Entity (Discr_Name));
5476 if not Is_Discrete_Type (Discr_Type) then
5478 ("discriminant in a variant part must be of a discrete type",
5483 -- Now analyze the choices, which also analyzes the declarations that
5484 -- are associated with each choice.
5486 Analyze_Choices (Variants (N), Discr_Type);
5488 -- Note: we used to instantiate and call Check_Choices here to check
5489 -- that the choices covered the discriminant, but it's too early to do
5490 -- that because of statically predicated subtypes, whose analysis may
5491 -- be deferred to their freeze point which may be as late as the freeze
5492 -- point of the containing record. So this call is now to be found in
5493 -- Freeze_Record_Declaration.
5495 end Analyze_Variant_Part;
5497 ----------------------------
5498 -- Array_Type_Declaration --
5499 ----------------------------
5501 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5502 Component_Def : constant Node_Id := Component_Definition (Def);
5503 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5504 Element_Type : Entity_Id;
5505 Implicit_Base : Entity_Id;
5507 Related_Id : Entity_Id := Empty;
5509 P : constant Node_Id := Parent (Def);
5513 if Nkind (Def) = N_Constrained_Array_Definition then
5514 Index := First (Discrete_Subtype_Definitions (Def));
5516 Index := First (Subtype_Marks (Def));
5519 -- Find proper names for the implicit types which may be public. In case
5520 -- of anonymous arrays we use the name of the first object of that type
5524 Related_Id := Defining_Identifier (P);
5530 while Present (Index) loop
5533 -- Test for odd case of trying to index a type by the type itself
5535 if Is_Entity_Name (Index) and then Entity (Index) = T then
5536 Error_Msg_N ("type& cannot be indexed by itself", Index);
5537 Set_Entity (Index, Standard_Boolean);
5538 Set_Etype (Index, Standard_Boolean);
5541 -- Check SPARK restriction requiring a subtype mark
5543 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5544 Check_SPARK_05_Restriction ("subtype mark required", Index);
5547 -- Add a subtype declaration for each index of private array type
5548 -- declaration whose etype is also private. For example:
5551 -- type Index is private;
5553 -- type Table is array (Index) of ...
5556 -- This is currently required by the expander for the internally
5557 -- generated equality subprogram of records with variant parts in
5558 -- which the etype of some component is such private type.
5560 if Ekind (Current_Scope) = E_Package
5561 and then In_Private_Part (Current_Scope)
5562 and then Has_Private_Declaration (Etype (Index))
5565 Loc : constant Source_Ptr := Sloc (Def);
5570 New_E := Make_Temporary (Loc, 'T');
5571 Set_Is_Internal (New_E);
5574 Make_Subtype_Declaration (Loc,
5575 Defining_Identifier => New_E,
5576 Subtype_Indication =>
5577 New_Occurrence_Of (Etype (Index), Loc));
5579 Insert_Before (Parent (Def), Decl);
5581 Set_Etype (Index, New_E);
5583 -- If the index is a range the Entity attribute is not
5584 -- available. Example:
5587 -- type T is private;
5589 -- type T is new Natural;
5590 -- Table : array (T(1) .. T(10)) of Boolean;
5593 if Nkind (Index) /= N_Range then
5594 Set_Entity (Index, New_E);
5599 Make_Index (Index, P, Related_Id, Nb_Index);
5601 -- Check error of subtype with predicate for index type
5603 Bad_Predicated_Subtype_Use
5604 ("subtype& has predicate, not allowed as index subtype",
5605 Index, Etype (Index));
5607 -- Move to next index
5610 Nb_Index := Nb_Index + 1;
5613 -- Process subtype indication if one is present
5615 if Present (Component_Typ) then
5616 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5618 Set_Etype (Component_Typ, Element_Type);
5620 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5621 Check_SPARK_05_Restriction
5622 ("subtype mark required", Component_Typ);
5625 -- Ada 2005 (AI-230): Access Definition case
5627 else pragma Assert (Present (Access_Definition (Component_Def)));
5629 -- Indicate that the anonymous access type is created by the
5630 -- array type declaration.
5632 Element_Type := Access_Definition
5634 N => Access_Definition (Component_Def));
5635 Set_Is_Local_Anonymous_Access (Element_Type);
5637 -- Propagate the parent. This field is needed if we have to generate
5638 -- the master_id associated with an anonymous access to task type
5639 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5641 Set_Parent (Element_Type, Parent (T));
5643 -- Ada 2005 (AI-230): In case of components that are anonymous access
5644 -- types the level of accessibility depends on the enclosing type
5647 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5649 -- Ada 2005 (AI-254)
5652 CD : constant Node_Id :=
5653 Access_To_Subprogram_Definition
5654 (Access_Definition (Component_Def));
5656 if Present (CD) and then Protected_Present (CD) then
5658 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5663 -- Constrained array case
5666 T := Create_Itype (E_Void, P, Related_Id, 'T');
5669 if Nkind (Def) = N_Constrained_Array_Definition then
5671 -- Establish Implicit_Base as unconstrained base type
5673 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5675 Set_Etype (Implicit_Base, Implicit_Base);
5676 Set_Scope (Implicit_Base, Current_Scope);
5677 Set_Has_Delayed_Freeze (Implicit_Base);
5678 Set_Default_SSO (Implicit_Base);
5680 -- The constrained array type is a subtype of the unconstrained one
5682 Set_Ekind (T, E_Array_Subtype);
5683 Init_Size_Align (T);
5684 Set_Etype (T, Implicit_Base);
5685 Set_Scope (T, Current_Scope);
5686 Set_Is_Constrained (T);
5688 First (Discrete_Subtype_Definitions (Def)));
5689 Set_Has_Delayed_Freeze (T);
5691 -- Complete setup of implicit base type
5693 Set_Component_Size (Implicit_Base, Uint_0);
5694 Set_Component_Type (Implicit_Base, Element_Type);
5695 Set_Finalize_Storage_Only
5697 Finalize_Storage_Only (Element_Type));
5698 Set_First_Index (Implicit_Base, First_Index (T));
5699 Set_Has_Controlled_Component
5701 Has_Controlled_Component (Element_Type)
5702 or else Is_Controlled_Active (Element_Type));
5703 Set_Packed_Array_Impl_Type
5704 (Implicit_Base, Empty);
5706 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
5708 -- Inherit the "ghostness" from the constrained array type
5710 if Ghost_Mode > None or else Is_Ghost_Entity (T) then
5711 Set_Is_Ghost_Entity (Implicit_Base);
5714 -- Unconstrained array case
5717 Set_Ekind (T, E_Array_Type);
5718 Init_Size_Align (T);
5720 Set_Scope (T, Current_Scope);
5721 Set_Component_Size (T, Uint_0);
5722 Set_Is_Constrained (T, False);
5723 Set_First_Index (T, First (Subtype_Marks (Def)));
5724 Set_Has_Delayed_Freeze (T, True);
5725 Propagate_Concurrent_Flags (T, Element_Type);
5726 Set_Has_Controlled_Component (T, Has_Controlled_Component
5729 Is_Controlled_Active (Element_Type));
5730 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5732 Set_Default_SSO (T);
5735 -- Common attributes for both cases
5737 Set_Component_Type (Base_Type (T), Element_Type);
5738 Set_Packed_Array_Impl_Type (T, Empty);
5740 if Aliased_Present (Component_Definition (Def)) then
5741 Check_SPARK_05_Restriction
5742 ("aliased is not allowed", Component_Definition (Def));
5743 Set_Has_Aliased_Components (Etype (T));
5746 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5747 -- array type to ensure that objects of this type are initialized.
5749 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5750 Set_Can_Never_Be_Null (T);
5752 if Null_Exclusion_Present (Component_Definition (Def))
5754 -- No need to check itypes because in their case this check was
5755 -- done at their point of creation
5757 and then not Is_Itype (Element_Type)
5760 ("`NOT NULL` not allowed (null already excluded)",
5761 Subtype_Indication (Component_Definition (Def)));
5765 Priv := Private_Component (Element_Type);
5767 if Present (Priv) then
5769 -- Check for circular definitions
5771 if Priv = Any_Type then
5772 Set_Component_Type (Etype (T), Any_Type);
5774 -- There is a gap in the visibility of operations on the composite
5775 -- type only if the component type is defined in a different scope.
5777 elsif Scope (Priv) = Current_Scope then
5780 elsif Is_Limited_Type (Priv) then
5781 Set_Is_Limited_Composite (Etype (T));
5782 Set_Is_Limited_Composite (T);
5784 Set_Is_Private_Composite (Etype (T));
5785 Set_Is_Private_Composite (T);
5789 -- A syntax error in the declaration itself may lead to an empty index
5790 -- list, in which case do a minimal patch.
5792 if No (First_Index (T)) then
5793 Error_Msg_N ("missing index definition in array type declaration", T);
5796 Indexes : constant List_Id :=
5797 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5799 Set_Discrete_Subtype_Definitions (Def, Indexes);
5800 Set_First_Index (T, First (Indexes));
5805 -- Create a concatenation operator for the new type. Internal array
5806 -- types created for packed entities do not need such, they are
5807 -- compatible with the user-defined type.
5809 if Number_Dimensions (T) = 1
5810 and then not Is_Packed_Array_Impl_Type (T)
5812 New_Concatenation_Op (T);
5815 -- In the case of an unconstrained array the parser has already verified
5816 -- that all the indexes are unconstrained but we still need to make sure
5817 -- that the element type is constrained.
5819 if not Is_Definite_Subtype (Element_Type) then
5821 ("unconstrained element type in array declaration",
5822 Subtype_Indication (Component_Def));
5824 elsif Is_Abstract_Type (Element_Type) then
5826 ("the type of a component cannot be abstract",
5827 Subtype_Indication (Component_Def));
5830 -- There may be an invariant declared for the component type, but
5831 -- the construction of the component invariant checking procedure
5832 -- takes place during expansion.
5833 end Array_Type_Declaration;
5835 ------------------------------------------------------
5836 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5837 ------------------------------------------------------
5839 function Replace_Anonymous_Access_To_Protected_Subprogram
5840 (N : Node_Id) return Entity_Id
5842 Loc : constant Source_Ptr := Sloc (N);
5844 Curr_Scope : constant Scope_Stack_Entry :=
5845 Scope_Stack.Table (Scope_Stack.Last);
5847 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5850 -- Access definition in declaration
5853 -- Object definition or formal definition with an access definition
5856 -- Declaration of anonymous access to subprogram type
5859 -- Original specification in access to subprogram
5864 Set_Is_Internal (Anon);
5867 when N_Component_Declaration |
5868 N_Unconstrained_Array_Definition |
5869 N_Constrained_Array_Definition =>
5870 Comp := Component_Definition (N);
5871 Acc := Access_Definition (Comp);
5873 when N_Discriminant_Specification =>
5874 Comp := Discriminant_Type (N);
5877 when N_Parameter_Specification =>
5878 Comp := Parameter_Type (N);
5881 when N_Access_Function_Definition =>
5882 Comp := Result_Definition (N);
5885 when N_Object_Declaration =>
5886 Comp := Object_Definition (N);
5889 when N_Function_Specification =>
5890 Comp := Result_Definition (N);
5894 raise Program_Error;
5897 Spec := Access_To_Subprogram_Definition (Acc);
5900 Make_Full_Type_Declaration (Loc,
5901 Defining_Identifier => Anon,
5902 Type_Definition => Copy_Separate_Tree (Spec));
5904 Mark_Rewrite_Insertion (Decl);
5906 -- In ASIS mode, analyze the profile on the original node, because
5907 -- the separate copy does not provide enough links to recover the
5908 -- original tree. Analysis is limited to type annotations, within
5909 -- a temporary scope that serves as an anonymous subprogram to collect
5910 -- otherwise useless temporaries and itypes.
5914 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5917 if Nkind (Spec) = N_Access_Function_Definition then
5918 Set_Ekind (Typ, E_Function);
5920 Set_Ekind (Typ, E_Procedure);
5923 Set_Parent (Typ, N);
5924 Set_Scope (Typ, Current_Scope);
5927 -- Nothing to do if procedure is parameterless
5929 if Present (Parameter_Specifications (Spec)) then
5930 Process_Formals (Parameter_Specifications (Spec), Spec);
5933 if Nkind (Spec) = N_Access_Function_Definition then
5935 Def : constant Node_Id := Result_Definition (Spec);
5938 -- The result might itself be an anonymous access type, so
5941 if Nkind (Def) = N_Access_Definition then
5942 if Present (Access_To_Subprogram_Definition (Def)) then
5945 Replace_Anonymous_Access_To_Protected_Subprogram
5948 Find_Type (Subtype_Mark (Def));
5961 -- Insert the new declaration in the nearest enclosing scope. If the
5962 -- parent is a body and N is its return type, the declaration belongs
5963 -- in the enclosing scope. Likewise if N is the type of a parameter.
5967 if Nkind (N) = N_Function_Specification
5968 and then Nkind (P) = N_Subprogram_Body
5971 elsif Nkind (N) = N_Parameter_Specification
5972 and then Nkind (P) in N_Subprogram_Specification
5973 and then Nkind (Parent (P)) = N_Subprogram_Body
5975 P := Parent (Parent (P));
5978 while Present (P) and then not Has_Declarations (P) loop
5982 pragma Assert (Present (P));
5984 if Nkind (P) = N_Package_Specification then
5985 Prepend (Decl, Visible_Declarations (P));
5987 Prepend (Decl, Declarations (P));
5990 -- Replace the anonymous type with an occurrence of the new declaration.
5991 -- In all cases the rewritten node does not have the null-exclusion
5992 -- attribute because (if present) it was already inherited by the
5993 -- anonymous entity (Anon). Thus, in case of components we do not
5994 -- inherit this attribute.
5996 if Nkind (N) = N_Parameter_Specification then
5997 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5998 Set_Etype (Defining_Identifier (N), Anon);
5999 Set_Null_Exclusion_Present (N, False);
6001 elsif Nkind (N) = N_Object_Declaration then
6002 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6003 Set_Etype (Defining_Identifier (N), Anon);
6005 elsif Nkind (N) = N_Access_Function_Definition then
6006 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6008 elsif Nkind (N) = N_Function_Specification then
6009 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6010 Set_Etype (Defining_Unit_Name (N), Anon);
6014 Make_Component_Definition (Loc,
6015 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6018 Mark_Rewrite_Insertion (Comp);
6020 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6021 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6022 and then not Is_Type (Current_Scope))
6025 -- Declaration can be analyzed in the current scope.
6030 -- Temporarily remove the current scope (record or subprogram) from
6031 -- the stack to add the new declarations to the enclosing scope.
6032 -- The anonymous entity is an Itype with the proper attributes.
6034 Scope_Stack.Decrement_Last;
6036 Set_Is_Itype (Anon);
6037 Set_Associated_Node_For_Itype (Anon, N);
6038 Scope_Stack.Append (Curr_Scope);
6041 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6042 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6044 end Replace_Anonymous_Access_To_Protected_Subprogram;
6046 -------------------------------
6047 -- Build_Derived_Access_Type --
6048 -------------------------------
6050 procedure Build_Derived_Access_Type
6052 Parent_Type : Entity_Id;
6053 Derived_Type : Entity_Id)
6055 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6057 Desig_Type : Entity_Id;
6059 Discr_Con_Elist : Elist_Id;
6060 Discr_Con_El : Elmt_Id;
6064 -- Set the designated type so it is available in case this is an access
6065 -- to a self-referential type, e.g. a standard list type with a next
6066 -- pointer. Will be reset after subtype is built.
6068 Set_Directly_Designated_Type
6069 (Derived_Type, Designated_Type (Parent_Type));
6071 Subt := Process_Subtype (S, N);
6073 if Nkind (S) /= N_Subtype_Indication
6074 and then Subt /= Base_Type (Subt)
6076 Set_Ekind (Derived_Type, E_Access_Subtype);
6079 if Ekind (Derived_Type) = E_Access_Subtype then
6081 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6082 Ibase : constant Entity_Id :=
6083 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6084 Svg_Chars : constant Name_Id := Chars (Ibase);
6085 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6088 Copy_Node (Pbase, Ibase);
6090 -- Restore Itype status after Copy_Node
6092 Set_Is_Itype (Ibase);
6093 Set_Associated_Node_For_Itype (Ibase, N);
6095 Set_Chars (Ibase, Svg_Chars);
6096 Set_Next_Entity (Ibase, Svg_Next_E);
6097 Set_Sloc (Ibase, Sloc (Derived_Type));
6098 Set_Scope (Ibase, Scope (Derived_Type));
6099 Set_Freeze_Node (Ibase, Empty);
6100 Set_Is_Frozen (Ibase, False);
6101 Set_Comes_From_Source (Ibase, False);
6102 Set_Is_First_Subtype (Ibase, False);
6104 Set_Etype (Ibase, Pbase);
6105 Set_Etype (Derived_Type, Ibase);
6109 Set_Directly_Designated_Type
6110 (Derived_Type, Designated_Type (Subt));
6112 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6113 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6114 Set_Size_Info (Derived_Type, Parent_Type);
6115 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6116 Set_Depends_On_Private (Derived_Type,
6117 Has_Private_Component (Derived_Type));
6118 Conditional_Delay (Derived_Type, Subt);
6120 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6121 -- that it is not redundant.
6123 if Null_Exclusion_Present (Type_Definition (N)) then
6124 Set_Can_Never_Be_Null (Derived_Type);
6126 elsif Can_Never_Be_Null (Parent_Type) then
6127 Set_Can_Never_Be_Null (Derived_Type);
6130 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6131 -- the root type for this information.
6133 -- Apply range checks to discriminants for derived record case
6134 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6136 Desig_Type := Designated_Type (Derived_Type);
6138 if Is_Composite_Type (Desig_Type)
6139 and then (not Is_Array_Type (Desig_Type))
6140 and then Has_Discriminants (Desig_Type)
6141 and then Base_Type (Desig_Type) /= Desig_Type
6143 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6144 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6146 Discr := First_Discriminant (Base_Type (Desig_Type));
6147 while Present (Discr_Con_El) loop
6148 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6149 Next_Elmt (Discr_Con_El);
6150 Next_Discriminant (Discr);
6153 end Build_Derived_Access_Type;
6155 ------------------------------
6156 -- Build_Derived_Array_Type --
6157 ------------------------------
6159 procedure Build_Derived_Array_Type
6161 Parent_Type : Entity_Id;
6162 Derived_Type : Entity_Id)
6164 Loc : constant Source_Ptr := Sloc (N);
6165 Tdef : constant Node_Id := Type_Definition (N);
6166 Indic : constant Node_Id := Subtype_Indication (Tdef);
6167 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6168 Implicit_Base : Entity_Id;
6169 New_Indic : Node_Id;
6171 procedure Make_Implicit_Base;
6172 -- If the parent subtype is constrained, the derived type is a subtype
6173 -- of an implicit base type derived from the parent base.
6175 ------------------------
6176 -- Make_Implicit_Base --
6177 ------------------------
6179 procedure Make_Implicit_Base is
6182 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6184 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6185 Set_Etype (Implicit_Base, Parent_Base);
6187 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6188 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6190 Set_Has_Delayed_Freeze (Implicit_Base, True);
6192 -- Inherit the "ghostness" from the parent base type
6194 if Ghost_Mode > None or else Is_Ghost_Entity (Parent_Base) then
6195 Set_Is_Ghost_Entity (Implicit_Base);
6197 end Make_Implicit_Base;
6199 -- Start of processing for Build_Derived_Array_Type
6202 if not Is_Constrained (Parent_Type) then
6203 if Nkind (Indic) /= N_Subtype_Indication then
6204 Set_Ekind (Derived_Type, E_Array_Type);
6206 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6207 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6209 Set_Has_Delayed_Freeze (Derived_Type, True);
6213 Set_Etype (Derived_Type, Implicit_Base);
6216 Make_Subtype_Declaration (Loc,
6217 Defining_Identifier => Derived_Type,
6218 Subtype_Indication =>
6219 Make_Subtype_Indication (Loc,
6220 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6221 Constraint => Constraint (Indic)));
6223 Rewrite (N, New_Indic);
6228 if Nkind (Indic) /= N_Subtype_Indication then
6231 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6232 Set_Etype (Derived_Type, Implicit_Base);
6233 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6236 Error_Msg_N ("illegal constraint on constrained type", Indic);
6240 -- If parent type is not a derived type itself, and is declared in
6241 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6242 -- the new type's concatenation operator since Derive_Subprograms
6243 -- will not inherit the parent's operator. If the parent type is
6244 -- unconstrained, the operator is of the unconstrained base type.
6246 if Number_Dimensions (Parent_Type) = 1
6247 and then not Is_Limited_Type (Parent_Type)
6248 and then not Is_Derived_Type (Parent_Type)
6249 and then not Is_Package_Or_Generic_Package
6250 (Scope (Base_Type (Parent_Type)))
6252 if not Is_Constrained (Parent_Type)
6253 and then Is_Constrained (Derived_Type)
6255 New_Concatenation_Op (Implicit_Base);
6257 New_Concatenation_Op (Derived_Type);
6260 end Build_Derived_Array_Type;
6262 -----------------------------------
6263 -- Build_Derived_Concurrent_Type --
6264 -----------------------------------
6266 procedure Build_Derived_Concurrent_Type
6268 Parent_Type : Entity_Id;
6269 Derived_Type : Entity_Id)
6271 Loc : constant Source_Ptr := Sloc (N);
6273 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6274 Corr_Decl : Node_Id;
6275 Corr_Decl_Needed : Boolean;
6276 -- If the derived type has fewer discriminants than its parent, the
6277 -- corresponding record is also a derived type, in order to account for
6278 -- the bound discriminants. We create a full type declaration for it in
6281 Constraint_Present : constant Boolean :=
6282 Nkind (Subtype_Indication (Type_Definition (N))) =
6283 N_Subtype_Indication;
6285 D_Constraint : Node_Id;
6286 New_Constraint : Elist_Id;
6287 Old_Disc : Entity_Id;
6288 New_Disc : Entity_Id;
6292 Set_Stored_Constraint (Derived_Type, No_Elist);
6293 Corr_Decl_Needed := False;
6296 if Present (Discriminant_Specifications (N))
6297 and then Constraint_Present
6299 Old_Disc := First_Discriminant (Parent_Type);
6300 New_Disc := First (Discriminant_Specifications (N));
6301 while Present (New_Disc) and then Present (Old_Disc) loop
6302 Next_Discriminant (Old_Disc);
6307 if Present (Old_Disc) and then Expander_Active then
6309 -- The new type has fewer discriminants, so we need to create a new
6310 -- corresponding record, which is derived from the corresponding
6311 -- record of the parent, and has a stored constraint that captures
6312 -- the values of the discriminant constraints. The corresponding
6313 -- record is needed only if expander is active and code generation is
6316 -- The type declaration for the derived corresponding record has the
6317 -- same discriminant part and constraints as the current declaration.
6318 -- Copy the unanalyzed tree to build declaration.
6320 Corr_Decl_Needed := True;
6321 New_N := Copy_Separate_Tree (N);
6324 Make_Full_Type_Declaration (Loc,
6325 Defining_Identifier => Corr_Record,
6326 Discriminant_Specifications =>
6327 Discriminant_Specifications (New_N),
6329 Make_Derived_Type_Definition (Loc,
6330 Subtype_Indication =>
6331 Make_Subtype_Indication (Loc,
6334 (Corresponding_Record_Type (Parent_Type), Loc),
6337 (Subtype_Indication (Type_Definition (New_N))))));
6340 -- Copy Storage_Size and Relative_Deadline variables if task case
6342 if Is_Task_Type (Parent_Type) then
6343 Set_Storage_Size_Variable (Derived_Type,
6344 Storage_Size_Variable (Parent_Type));
6345 Set_Relative_Deadline_Variable (Derived_Type,
6346 Relative_Deadline_Variable (Parent_Type));
6349 if Present (Discriminant_Specifications (N)) then
6350 Push_Scope (Derived_Type);
6351 Check_Or_Process_Discriminants (N, Derived_Type);
6353 if Constraint_Present then
6355 Expand_To_Stored_Constraint
6357 Build_Discriminant_Constraints
6359 Subtype_Indication (Type_Definition (N)), True));
6364 elsif Constraint_Present then
6366 -- Build constrained subtype, copying the constraint, and derive
6367 -- from it to create a derived constrained type.
6370 Loc : constant Source_Ptr := Sloc (N);
6371 Anon : constant Entity_Id :=
6372 Make_Defining_Identifier (Loc,
6373 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6378 Make_Subtype_Declaration (Loc,
6379 Defining_Identifier => Anon,
6380 Subtype_Indication =>
6381 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6382 Insert_Before (N, Decl);
6385 Rewrite (Subtype_Indication (Type_Definition (N)),
6386 New_Occurrence_Of (Anon, Loc));
6387 Set_Analyzed (Derived_Type, False);
6393 -- By default, operations and private data are inherited from parent.
6394 -- However, in the presence of bound discriminants, a new corresponding
6395 -- record will be created, see below.
6397 Set_Has_Discriminants
6398 (Derived_Type, Has_Discriminants (Parent_Type));
6399 Set_Corresponding_Record_Type
6400 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6402 -- Is_Constrained is set according the parent subtype, but is set to
6403 -- False if the derived type is declared with new discriminants.
6407 (Is_Constrained (Parent_Type) or else Constraint_Present)
6408 and then not Present (Discriminant_Specifications (N)));
6410 if Constraint_Present then
6411 if not Has_Discriminants (Parent_Type) then
6412 Error_Msg_N ("untagged parent must have discriminants", N);
6414 elsif Present (Discriminant_Specifications (N)) then
6416 -- Verify that new discriminants are used to constrain old ones
6421 (Constraint (Subtype_Indication (Type_Definition (N)))));
6423 Old_Disc := First_Discriminant (Parent_Type);
6425 while Present (D_Constraint) loop
6426 if Nkind (D_Constraint) /= N_Discriminant_Association then
6428 -- Positional constraint. If it is a reference to a new
6429 -- discriminant, it constrains the corresponding old one.
6431 if Nkind (D_Constraint) = N_Identifier then
6432 New_Disc := First_Discriminant (Derived_Type);
6433 while Present (New_Disc) loop
6434 exit when Chars (New_Disc) = Chars (D_Constraint);
6435 Next_Discriminant (New_Disc);
6438 if Present (New_Disc) then
6439 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6443 Next_Discriminant (Old_Disc);
6445 -- if this is a named constraint, search by name for the old
6446 -- discriminants constrained by the new one.
6448 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6450 -- Find new discriminant with that name
6452 New_Disc := First_Discriminant (Derived_Type);
6453 while Present (New_Disc) loop
6455 Chars (New_Disc) = Chars (Expression (D_Constraint));
6456 Next_Discriminant (New_Disc);
6459 if Present (New_Disc) then
6461 -- Verify that new discriminant renames some discriminant
6462 -- of the parent type, and associate the new discriminant
6463 -- with one or more old ones that it renames.
6469 Selector := First (Selector_Names (D_Constraint));
6470 while Present (Selector) loop
6471 Old_Disc := First_Discriminant (Parent_Type);
6472 while Present (Old_Disc) loop
6473 exit when Chars (Old_Disc) = Chars (Selector);
6474 Next_Discriminant (Old_Disc);
6477 if Present (Old_Disc) then
6478 Set_Corresponding_Discriminant
6479 (New_Disc, Old_Disc);
6488 Next (D_Constraint);
6491 New_Disc := First_Discriminant (Derived_Type);
6492 while Present (New_Disc) loop
6493 if No (Corresponding_Discriminant (New_Disc)) then
6495 ("new discriminant& must constrain old one", N, New_Disc);
6498 Subtypes_Statically_Compatible
6500 Etype (Corresponding_Discriminant (New_Disc)))
6503 ("& not statically compatible with parent discriminant",
6507 Next_Discriminant (New_Disc);
6511 elsif Present (Discriminant_Specifications (N)) then
6513 ("missing discriminant constraint in untagged derivation", N);
6516 -- The entity chain of the derived type includes the new discriminants
6517 -- but shares operations with the parent.
6519 if Present (Discriminant_Specifications (N)) then
6520 Old_Disc := First_Discriminant (Parent_Type);
6521 while Present (Old_Disc) loop
6522 if No (Next_Entity (Old_Disc))
6523 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6526 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6530 Next_Discriminant (Old_Disc);
6534 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6535 if Has_Discriminants (Parent_Type) then
6536 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6537 Set_Discriminant_Constraint (
6538 Derived_Type, Discriminant_Constraint (Parent_Type));
6542 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6544 Set_Has_Completion (Derived_Type);
6546 if Corr_Decl_Needed then
6547 Set_Stored_Constraint (Derived_Type, New_Constraint);
6548 Insert_After (N, Corr_Decl);
6549 Analyze (Corr_Decl);
6550 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6552 end Build_Derived_Concurrent_Type;
6554 ------------------------------------
6555 -- Build_Derived_Enumeration_Type --
6556 ------------------------------------
6558 procedure Build_Derived_Enumeration_Type
6560 Parent_Type : Entity_Id;
6561 Derived_Type : Entity_Id)
6563 Loc : constant Source_Ptr := Sloc (N);
6564 Def : constant Node_Id := Type_Definition (N);
6565 Indic : constant Node_Id := Subtype_Indication (Def);
6566 Implicit_Base : Entity_Id;
6567 Literal : Entity_Id;
6568 New_Lit : Entity_Id;
6569 Literals_List : List_Id;
6570 Type_Decl : Node_Id;
6572 Rang_Expr : Node_Id;
6575 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6576 -- not have explicit literals lists we need to process types derived
6577 -- from them specially. This is handled by Derived_Standard_Character.
6578 -- If the parent type is a generic type, there are no literals either,
6579 -- and we construct the same skeletal representation as for the generic
6582 if Is_Standard_Character_Type (Parent_Type) then
6583 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6585 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6591 if Nkind (Indic) /= N_Subtype_Indication then
6593 Make_Attribute_Reference (Loc,
6594 Attribute_Name => Name_First,
6595 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6596 Set_Etype (Lo, Derived_Type);
6599 Make_Attribute_Reference (Loc,
6600 Attribute_Name => Name_Last,
6601 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6602 Set_Etype (Hi, Derived_Type);
6604 Set_Scalar_Range (Derived_Type,
6610 -- Analyze subtype indication and verify compatibility
6611 -- with parent type.
6613 if Base_Type (Process_Subtype (Indic, N)) /=
6614 Base_Type (Parent_Type)
6617 ("illegal constraint for formal discrete type", N);
6623 -- If a constraint is present, analyze the bounds to catch
6624 -- premature usage of the derived literals.
6626 if Nkind (Indic) = N_Subtype_Indication
6627 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6629 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6630 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6633 -- Introduce an implicit base type for the derived type even if there
6634 -- is no constraint attached to it, since this seems closer to the
6635 -- Ada semantics. Build a full type declaration tree for the derived
6636 -- type using the implicit base type as the defining identifier. The
6637 -- build a subtype declaration tree which applies the constraint (if
6638 -- any) have it replace the derived type declaration.
6640 Literal := First_Literal (Parent_Type);
6641 Literals_List := New_List;
6642 while Present (Literal)
6643 and then Ekind (Literal) = E_Enumeration_Literal
6645 -- Literals of the derived type have the same representation as
6646 -- those of the parent type, but this representation can be
6647 -- overridden by an explicit representation clause. Indicate
6648 -- that there is no explicit representation given yet. These
6649 -- derived literals are implicit operations of the new type,
6650 -- and can be overridden by explicit ones.
6652 if Nkind (Literal) = N_Defining_Character_Literal then
6654 Make_Defining_Character_Literal (Loc, Chars (Literal));
6656 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6659 Set_Ekind (New_Lit, E_Enumeration_Literal);
6660 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6661 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6662 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6663 Set_Alias (New_Lit, Literal);
6664 Set_Is_Known_Valid (New_Lit, True);
6666 Append (New_Lit, Literals_List);
6667 Next_Literal (Literal);
6671 Make_Defining_Identifier (Sloc (Derived_Type),
6672 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6674 -- Indicate the proper nature of the derived type. This must be done
6675 -- before analysis of the literals, to recognize cases when a literal
6676 -- may be hidden by a previous explicit function definition (cf.
6679 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6680 Set_Etype (Derived_Type, Implicit_Base);
6683 Make_Full_Type_Declaration (Loc,
6684 Defining_Identifier => Implicit_Base,
6685 Discriminant_Specifications => No_List,
6687 Make_Enumeration_Type_Definition (Loc, Literals_List));
6689 Mark_Rewrite_Insertion (Type_Decl);
6690 Insert_Before (N, Type_Decl);
6691 Analyze (Type_Decl);
6693 -- The anonymous base now has a full declaration, but this base
6694 -- is not a first subtype.
6696 Set_Is_First_Subtype (Implicit_Base, False);
6698 -- After the implicit base is analyzed its Etype needs to be changed
6699 -- to reflect the fact that it is derived from the parent type which
6700 -- was ignored during analysis. We also set the size at this point.
6702 Set_Etype (Implicit_Base, Parent_Type);
6704 Set_Size_Info (Implicit_Base, Parent_Type);
6705 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6706 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6708 -- Copy other flags from parent type
6710 Set_Has_Non_Standard_Rep
6711 (Implicit_Base, Has_Non_Standard_Rep
6713 Set_Has_Pragma_Ordered
6714 (Implicit_Base, Has_Pragma_Ordered
6716 Set_Has_Delayed_Freeze (Implicit_Base);
6718 -- Process the subtype indication including a validation check on the
6719 -- constraint, if any. If a constraint is given, its bounds must be
6720 -- implicitly converted to the new type.
6722 if Nkind (Indic) = N_Subtype_Indication then
6724 R : constant Node_Id :=
6725 Range_Expression (Constraint (Indic));
6728 if Nkind (R) = N_Range then
6729 Hi := Build_Scalar_Bound
6730 (High_Bound (R), Parent_Type, Implicit_Base);
6731 Lo := Build_Scalar_Bound
6732 (Low_Bound (R), Parent_Type, Implicit_Base);
6735 -- Constraint is a Range attribute. Replace with explicit
6736 -- mention of the bounds of the prefix, which must be a
6739 Analyze (Prefix (R));
6741 Convert_To (Implicit_Base,
6742 Make_Attribute_Reference (Loc,
6743 Attribute_Name => Name_Last,
6745 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6748 Convert_To (Implicit_Base,
6749 Make_Attribute_Reference (Loc,
6750 Attribute_Name => Name_First,
6752 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6759 (Type_High_Bound (Parent_Type),
6760 Parent_Type, Implicit_Base);
6763 (Type_Low_Bound (Parent_Type),
6764 Parent_Type, Implicit_Base);
6772 -- If we constructed a default range for the case where no range
6773 -- was given, then the expressions in the range must not freeze
6774 -- since they do not correspond to expressions in the source.
6776 if Nkind (Indic) /= N_Subtype_Indication then
6777 Set_Must_Not_Freeze (Lo);
6778 Set_Must_Not_Freeze (Hi);
6779 Set_Must_Not_Freeze (Rang_Expr);
6783 Make_Subtype_Declaration (Loc,
6784 Defining_Identifier => Derived_Type,
6785 Subtype_Indication =>
6786 Make_Subtype_Indication (Loc,
6787 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6789 Make_Range_Constraint (Loc,
6790 Range_Expression => Rang_Expr))));
6794 -- Propagate the aspects from the original type declaration to the
6795 -- declaration of the implicit base.
6797 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6799 -- Apply a range check. Since this range expression doesn't have an
6800 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6803 if Nkind (Indic) = N_Subtype_Indication then
6805 (Range_Expression (Constraint (Indic)), Parent_Type,
6806 Source_Typ => Entity (Subtype_Mark (Indic)));
6809 end Build_Derived_Enumeration_Type;
6811 --------------------------------
6812 -- Build_Derived_Numeric_Type --
6813 --------------------------------
6815 procedure Build_Derived_Numeric_Type
6817 Parent_Type : Entity_Id;
6818 Derived_Type : Entity_Id)
6820 Loc : constant Source_Ptr := Sloc (N);
6821 Tdef : constant Node_Id := Type_Definition (N);
6822 Indic : constant Node_Id := Subtype_Indication (Tdef);
6823 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6824 No_Constraint : constant Boolean := Nkind (Indic) /=
6825 N_Subtype_Indication;
6826 Implicit_Base : Entity_Id;
6832 -- Process the subtype indication including a validation check on
6833 -- the constraint if any.
6835 Discard_Node (Process_Subtype (Indic, N));
6837 -- Introduce an implicit base type for the derived type even if there
6838 -- is no constraint attached to it, since this seems closer to the Ada
6842 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6844 Set_Etype (Implicit_Base, Parent_Base);
6845 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6846 Set_Size_Info (Implicit_Base, Parent_Base);
6847 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6848 Set_Parent (Implicit_Base, Parent (Derived_Type));
6849 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
6851 -- Set RM Size for discrete type or decimal fixed-point type
6852 -- Ordinary fixed-point is excluded, why???
6854 if Is_Discrete_Type (Parent_Base)
6855 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
6857 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6860 Set_Has_Delayed_Freeze (Implicit_Base);
6862 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6863 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6865 Set_Scalar_Range (Implicit_Base,
6870 if Has_Infinities (Parent_Base) then
6871 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6874 -- The Derived_Type, which is the entity of the declaration, is a
6875 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6876 -- absence of an explicit constraint.
6878 Set_Etype (Derived_Type, Implicit_Base);
6880 -- If we did not have a constraint, then the Ekind is set from the
6881 -- parent type (otherwise Process_Subtype has set the bounds)
6883 if No_Constraint then
6884 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6887 -- If we did not have a range constraint, then set the range from the
6888 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6890 if No_Constraint or else not Has_Range_Constraint (Indic) then
6891 Set_Scalar_Range (Derived_Type,
6893 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6894 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6895 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6897 if Has_Infinities (Parent_Type) then
6898 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6901 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6904 Set_Is_Descendant_Of_Address (Derived_Type,
6905 Is_Descendant_Of_Address (Parent_Type));
6906 Set_Is_Descendant_Of_Address (Implicit_Base,
6907 Is_Descendant_Of_Address (Parent_Type));
6909 -- Set remaining type-specific fields, depending on numeric type
6911 if Is_Modular_Integer_Type (Parent_Type) then
6912 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6914 Set_Non_Binary_Modulus
6915 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6918 (Implicit_Base, Is_Known_Valid (Parent_Base));
6920 elsif Is_Floating_Point_Type (Parent_Type) then
6922 -- Digits of base type is always copied from the digits value of
6923 -- the parent base type, but the digits of the derived type will
6924 -- already have been set if there was a constraint present.
6926 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6927 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6929 if No_Constraint then
6930 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6933 elsif Is_Fixed_Point_Type (Parent_Type) then
6935 -- Small of base type and derived type are always copied from the
6936 -- parent base type, since smalls never change. The delta of the
6937 -- base type is also copied from the parent base type. However the
6938 -- delta of the derived type will have been set already if a
6939 -- constraint was present.
6941 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6942 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6943 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6945 if No_Constraint then
6946 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6949 -- The scale and machine radix in the decimal case are always
6950 -- copied from the parent base type.
6952 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6953 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6954 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6956 Set_Machine_Radix_10
6957 (Derived_Type, Machine_Radix_10 (Parent_Base));
6958 Set_Machine_Radix_10
6959 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6961 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6963 if No_Constraint then
6964 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6967 -- the analysis of the subtype_indication sets the
6968 -- digits value of the derived type.
6975 if Is_Integer_Type (Parent_Type) then
6976 Set_Has_Shift_Operator
6977 (Implicit_Base, Has_Shift_Operator (Parent_Type));
6980 -- The type of the bounds is that of the parent type, and they
6981 -- must be converted to the derived type.
6983 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6985 -- The implicit_base should be frozen when the derived type is frozen,
6986 -- but note that it is used in the conversions of the bounds. For fixed
6987 -- types we delay the determination of the bounds until the proper
6988 -- freezing point. For other numeric types this is rejected by GCC, for
6989 -- reasons that are currently unclear (???), so we choose to freeze the
6990 -- implicit base now. In the case of integers and floating point types
6991 -- this is harmless because subsequent representation clauses cannot
6992 -- affect anything, but it is still baffling that we cannot use the
6993 -- same mechanism for all derived numeric types.
6995 -- There is a further complication: actually some representation
6996 -- clauses can affect the implicit base type. For example, attribute
6997 -- definition clauses for stream-oriented attributes need to set the
6998 -- corresponding TSS entries on the base type, and this normally
6999 -- cannot be done after the base type is frozen, so the circuitry in
7000 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7001 -- and not use Set_TSS in this case.
7003 -- There are also consequences for the case of delayed representation
7004 -- aspects for some cases. For example, a Size aspect is delayed and
7005 -- should not be evaluated to the freeze point. This early freezing
7006 -- means that the size attribute evaluation happens too early???
7008 if Is_Fixed_Point_Type (Parent_Type) then
7009 Conditional_Delay (Implicit_Base, Parent_Type);
7011 Freeze_Before (N, Implicit_Base);
7013 end Build_Derived_Numeric_Type;
7015 --------------------------------
7016 -- Build_Derived_Private_Type --
7017 --------------------------------
7019 procedure Build_Derived_Private_Type
7021 Parent_Type : Entity_Id;
7022 Derived_Type : Entity_Id;
7023 Is_Completion : Boolean;
7024 Derive_Subps : Boolean := True)
7026 Loc : constant Source_Ptr := Sloc (N);
7027 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7028 Par_Scope : constant Entity_Id := Scope (Par_Base);
7029 Full_N : constant Node_Id := New_Copy_Tree (N);
7030 Full_Der : Entity_Id := New_Copy (Derived_Type);
7033 procedure Build_Full_Derivation;
7034 -- Build full derivation, i.e. derive from the full view
7036 procedure Copy_And_Build;
7037 -- Copy derived type declaration, replace parent with its full view,
7038 -- and build derivation
7040 ---------------------------
7041 -- Build_Full_Derivation --
7042 ---------------------------
7044 procedure Build_Full_Derivation is
7046 -- If parent scope is not open, install the declarations
7048 if not In_Open_Scopes (Par_Scope) then
7049 Install_Private_Declarations (Par_Scope);
7050 Install_Visible_Declarations (Par_Scope);
7052 Uninstall_Declarations (Par_Scope);
7054 -- If parent scope is open and in another unit, and parent has a
7055 -- completion, then the derivation is taking place in the visible
7056 -- part of a child unit. In that case retrieve the full view of
7057 -- the parent momentarily.
7059 elsif not In_Same_Source_Unit (N, Parent_Type) then
7060 Full_P := Full_View (Parent_Type);
7061 Exchange_Declarations (Parent_Type);
7063 Exchange_Declarations (Full_P);
7065 -- Otherwise it is a local derivation
7070 end Build_Full_Derivation;
7072 --------------------
7073 -- Copy_And_Build --
7074 --------------------
7076 procedure Copy_And_Build is
7077 Full_Parent : Entity_Id := Parent_Type;
7080 -- If the parent is itself derived from another private type,
7081 -- installing the private declarations has not affected its
7082 -- privacy status, so use its own full view explicitly.
7084 if Is_Private_Type (Full_Parent)
7085 and then Present (Full_View (Full_Parent))
7087 Full_Parent := Full_View (Full_Parent);
7090 -- And its underlying full view if necessary
7092 if Is_Private_Type (Full_Parent)
7093 and then Present (Underlying_Full_View (Full_Parent))
7095 Full_Parent := Underlying_Full_View (Full_Parent);
7098 -- For record, access and most enumeration types, derivation from
7099 -- the full view requires a fully-fledged declaration. In the other
7100 -- cases, just use an itype.
7102 if Ekind (Full_Parent) in Record_Kind
7103 or else Ekind (Full_Parent) in Access_Kind
7105 (Ekind (Full_Parent) in Enumeration_Kind
7106 and then not Is_Standard_Character_Type (Full_Parent)
7107 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7109 -- Copy and adjust declaration to provide a completion for what
7110 -- is originally a private declaration. Indicate that full view
7111 -- is internally generated.
7113 Set_Comes_From_Source (Full_N, False);
7114 Set_Comes_From_Source (Full_Der, False);
7115 Set_Parent (Full_Der, Full_N);
7116 Set_Defining_Identifier (Full_N, Full_Der);
7118 -- If there are no constraints, adjust the subtype mark
7120 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7121 N_Subtype_Indication
7123 Set_Subtype_Indication
7124 (Type_Definition (Full_N),
7125 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7128 Insert_After (N, Full_N);
7130 -- Build full view of derived type from full view of parent which
7131 -- is now installed. Subprograms have been derived on the partial
7132 -- view, the completion does not derive them anew.
7134 if Ekind (Full_Parent) in Record_Kind then
7136 -- If parent type is tagged, the completion inherits the proper
7137 -- primitive operations.
7139 if Is_Tagged_Type (Parent_Type) then
7140 Build_Derived_Record_Type
7141 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7143 Build_Derived_Record_Type
7144 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7149 (Full_N, Full_Parent, Full_Der,
7150 Is_Completion => False, Derive_Subps => False);
7153 -- The full declaration has been introduced into the tree and
7154 -- processed in the step above. It should not be analyzed again
7155 -- (when encountered later in the current list of declarations)
7156 -- to prevent spurious name conflicts. The full entity remains
7159 Set_Analyzed (Full_N);
7163 Make_Defining_Identifier (Sloc (Derived_Type),
7164 Chars => Chars (Derived_Type));
7165 Set_Is_Itype (Full_Der);
7166 Set_Associated_Node_For_Itype (Full_Der, N);
7167 Set_Parent (Full_Der, N);
7169 (N, Full_Parent, Full_Der,
7170 Is_Completion => False, Derive_Subps => False);
7173 Set_Has_Private_Declaration (Full_Der);
7174 Set_Has_Private_Declaration (Derived_Type);
7176 Set_Scope (Full_Der, Scope (Derived_Type));
7177 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7178 Set_Has_Size_Clause (Full_Der, False);
7179 Set_Has_Alignment_Clause (Full_Der, False);
7180 Set_Has_Delayed_Freeze (Full_Der);
7181 Set_Is_Frozen (Full_Der, False);
7182 Set_Freeze_Node (Full_Der, Empty);
7183 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7184 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7186 -- The convention on the base type may be set in the private part
7187 -- and not propagated to the subtype until later, so we obtain the
7188 -- convention from the base type of the parent.
7190 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7193 -- Start of processing for Build_Derived_Private_Type
7196 if Is_Tagged_Type (Parent_Type) then
7197 Full_P := Full_View (Parent_Type);
7199 -- A type extension of a type with unknown discriminants is an
7200 -- indefinite type that the back-end cannot handle directly.
7201 -- We treat it as a private type, and build a completion that is
7202 -- derived from the full view of the parent, and hopefully has
7203 -- known discriminants.
7205 -- If the full view of the parent type has an underlying record view,
7206 -- use it to generate the underlying record view of this derived type
7207 -- (required for chains of derivations with unknown discriminants).
7209 -- Minor optimization: we avoid the generation of useless underlying
7210 -- record view entities if the private type declaration has unknown
7211 -- discriminants but its corresponding full view has no
7214 if Has_Unknown_Discriminants (Parent_Type)
7215 and then Present (Full_P)
7216 and then (Has_Discriminants (Full_P)
7217 or else Present (Underlying_Record_View (Full_P)))
7218 and then not In_Open_Scopes (Par_Scope)
7219 and then Expander_Active
7222 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7223 New_Ext : constant Node_Id :=
7225 (Record_Extension_Part (Type_Definition (N)));
7229 Build_Derived_Record_Type
7230 (N, Parent_Type, Derived_Type, Derive_Subps);
7232 -- Build anonymous completion, as a derivation from the full
7233 -- view of the parent. This is not a completion in the usual
7234 -- sense, because the current type is not private.
7237 Make_Full_Type_Declaration (Loc,
7238 Defining_Identifier => Full_Der,
7240 Make_Derived_Type_Definition (Loc,
7241 Subtype_Indication =>
7243 (Subtype_Indication (Type_Definition (N))),
7244 Record_Extension_Part => New_Ext));
7246 -- If the parent type has an underlying record view, use it
7247 -- here to build the new underlying record view.
7249 if Present (Underlying_Record_View (Full_P)) then
7251 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7253 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7254 Underlying_Record_View (Full_P));
7257 Install_Private_Declarations (Par_Scope);
7258 Install_Visible_Declarations (Par_Scope);
7259 Insert_Before (N, Decl);
7261 -- Mark entity as an underlying record view before analysis,
7262 -- to avoid generating the list of its primitive operations
7263 -- (which is not really required for this entity) and thus
7264 -- prevent spurious errors associated with missing overriding
7265 -- of abstract primitives (overridden only for Derived_Type).
7267 Set_Ekind (Full_Der, E_Record_Type);
7268 Set_Is_Underlying_Record_View (Full_Der);
7269 Set_Default_SSO (Full_Der);
7273 pragma Assert (Has_Discriminants (Full_Der)
7274 and then not Has_Unknown_Discriminants (Full_Der));
7276 Uninstall_Declarations (Par_Scope);
7278 -- Freeze the underlying record view, to prevent generation of
7279 -- useless dispatching information, which is simply shared with
7280 -- the real derived type.
7282 Set_Is_Frozen (Full_Der);
7284 -- If the derived type has access discriminants, create
7285 -- references to their anonymous types now, to prevent
7286 -- back-end problems when their first use is in generated
7287 -- bodies of primitives.
7293 E := First_Entity (Full_Der);
7295 while Present (E) loop
7296 if Ekind (E) = E_Discriminant
7297 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7299 Build_Itype_Reference (Etype (E), Decl);
7306 -- Set up links between real entity and underlying record view
7308 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7309 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7312 -- If discriminants are known, build derived record
7315 Build_Derived_Record_Type
7316 (N, Parent_Type, Derived_Type, Derive_Subps);
7321 elsif Has_Discriminants (Parent_Type) then
7323 -- Build partial view of derived type from partial view of parent.
7324 -- This must be done before building the full derivation because the
7325 -- second derivation will modify the discriminants of the first and
7326 -- the discriminants are chained with the rest of the components in
7327 -- the full derivation.
7329 Build_Derived_Record_Type
7330 (N, Parent_Type, Derived_Type, Derive_Subps);
7332 -- Build the full derivation if this is not the anonymous derived
7333 -- base type created by Build_Derived_Record_Type in the constrained
7334 -- case (see point 5. of its head comment) since we build it for the
7335 -- derived subtype. And skip it for protected types altogether, as
7336 -- gigi does not use these types directly.
7338 if Present (Full_View (Parent_Type))
7339 and then not Is_Itype (Derived_Type)
7340 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7343 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7345 Last_Discr : Entity_Id;
7348 -- If this is not a completion, construct the implicit full
7349 -- view by deriving from the full view of the parent type.
7350 -- But if this is a completion, the derived private type
7351 -- being built is a full view and the full derivation can
7352 -- only be its underlying full view.
7354 Build_Full_Derivation;
7356 if not Is_Completion then
7357 Set_Full_View (Derived_Type, Full_Der);
7359 Set_Underlying_Full_View (Derived_Type, Full_Der);
7362 if not Is_Base_Type (Derived_Type) then
7363 Set_Full_View (Der_Base, Base_Type (Full_Der));
7366 -- Copy the discriminant list from full view to the partial
7367 -- view (base type and its subtype). Gigi requires that the
7368 -- partial and full views have the same discriminants.
7370 -- Note that since the partial view points to discriminants
7371 -- in the full view, their scope will be that of the full
7372 -- view. This might cause some front end problems and need
7375 Discr := First_Discriminant (Base_Type (Full_Der));
7376 Set_First_Entity (Der_Base, Discr);
7379 Last_Discr := Discr;
7380 Next_Discriminant (Discr);
7381 exit when No (Discr);
7384 Set_Last_Entity (Der_Base, Last_Discr);
7385 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7386 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7388 Set_Stored_Constraint
7389 (Full_Der, Stored_Constraint (Derived_Type));
7393 elsif Present (Full_View (Parent_Type))
7394 and then Has_Discriminants (Full_View (Parent_Type))
7396 if Has_Unknown_Discriminants (Parent_Type)
7397 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7398 N_Subtype_Indication
7401 ("cannot constrain type with unknown discriminants",
7402 Subtype_Indication (Type_Definition (N)));
7406 -- If this is not a completion, construct the implicit full view by
7407 -- deriving from the full view of the parent type. But if this is a
7408 -- completion, the derived private type being built is a full view
7409 -- and the full derivation can only be its underlying full view.
7411 Build_Full_Derivation;
7413 if not Is_Completion then
7414 Set_Full_View (Derived_Type, Full_Der);
7416 Set_Underlying_Full_View (Derived_Type, Full_Der);
7419 -- In any case, the primitive operations are inherited from the
7420 -- parent type, not from the internal full view.
7422 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7424 if Derive_Subps then
7425 Derive_Subprograms (Parent_Type, Derived_Type);
7428 Set_Stored_Constraint (Derived_Type, No_Elist);
7430 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7433 -- Untagged type, No discriminants on either view
7435 if Nkind (Subtype_Indication (Type_Definition (N))) =
7436 N_Subtype_Indication
7439 ("illegal constraint on type without discriminants", N);
7442 if Present (Discriminant_Specifications (N))
7443 and then Present (Full_View (Parent_Type))
7444 and then not Is_Tagged_Type (Full_View (Parent_Type))
7446 Error_Msg_N ("cannot add discriminants to untagged type", N);
7449 Set_Stored_Constraint (Derived_Type, No_Elist);
7450 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7451 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7452 Set_Disable_Controlled (Derived_Type, Disable_Controlled
7454 Set_Has_Controlled_Component
7455 (Derived_Type, Has_Controlled_Component
7458 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7460 if not Is_Controlled_Active (Parent_Type) then
7461 Set_Finalize_Storage_Only
7462 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7465 -- If this is not a completion, construct the implicit full view by
7466 -- deriving from the full view of the parent type.
7468 -- ??? If the parent is untagged private and its completion is
7469 -- tagged, this mechanism will not work because we cannot derive from
7470 -- the tagged full view unless we have an extension.
7472 if Present (Full_View (Parent_Type))
7473 and then not Is_Tagged_Type (Full_View (Parent_Type))
7474 and then not Is_Completion
7476 Build_Full_Derivation;
7477 Set_Full_View (Derived_Type, Full_Der);
7481 Set_Has_Unknown_Discriminants (Derived_Type,
7482 Has_Unknown_Discriminants (Parent_Type));
7484 if Is_Private_Type (Derived_Type) then
7485 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7488 -- If the parent base type is in scope, add the derived type to its
7489 -- list of private dependents, because its full view may become
7490 -- visible subsequently (in a nested private part, a body, or in a
7491 -- further child unit).
7493 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7494 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7496 -- Check for unusual case where a type completed by a private
7497 -- derivation occurs within a package nested in a child unit, and
7498 -- the parent is declared in an ancestor.
7500 if Is_Child_Unit (Scope (Current_Scope))
7501 and then Is_Completion
7502 and then In_Private_Part (Current_Scope)
7503 and then Scope (Parent_Type) /= Current_Scope
7505 -- Note that if the parent has a completion in the private part,
7506 -- (which is itself a derivation from some other private type)
7507 -- it is that completion that is visible, there is no full view
7508 -- available, and no special processing is needed.
7510 and then Present (Full_View (Parent_Type))
7512 -- In this case, the full view of the parent type will become
7513 -- visible in the body of the enclosing child, and only then will
7514 -- the current type be possibly non-private. Build an underlying
7515 -- full view that will be installed when the enclosing child body
7518 if Present (Underlying_Full_View (Derived_Type)) then
7519 Full_Der := Underlying_Full_View (Derived_Type);
7521 Build_Full_Derivation;
7522 Set_Underlying_Full_View (Derived_Type, Full_Der);
7525 -- The full view will be used to swap entities on entry/exit to
7526 -- the body, and must appear in the entity list for the package.
7528 Append_Entity (Full_Der, Scope (Derived_Type));
7531 end Build_Derived_Private_Type;
7533 -------------------------------
7534 -- Build_Derived_Record_Type --
7535 -------------------------------
7539 -- Ideally we would like to use the same model of type derivation for
7540 -- tagged and untagged record types. Unfortunately this is not quite
7541 -- possible because the semantics of representation clauses is different
7542 -- for tagged and untagged records under inheritance. Consider the
7545 -- type R (...) is [tagged] record ... end record;
7546 -- type T (...) is new R (...) [with ...];
7548 -- The representation clauses for T can specify a completely different
7549 -- record layout from R's. Hence the same component can be placed in two
7550 -- very different positions in objects of type T and R. If R and T are
7551 -- tagged types, representation clauses for T can only specify the layout
7552 -- of non inherited components, thus components that are common in R and T
7553 -- have the same position in objects of type R and T.
7555 -- This has two implications. The first is that the entire tree for R's
7556 -- declaration needs to be copied for T in the untagged case, so that T
7557 -- can be viewed as a record type of its own with its own representation
7558 -- clauses. The second implication is the way we handle discriminants.
7559 -- Specifically, in the untagged case we need a way to communicate to Gigi
7560 -- what are the real discriminants in the record, while for the semantics
7561 -- we need to consider those introduced by the user to rename the
7562 -- discriminants in the parent type. This is handled by introducing the
7563 -- notion of stored discriminants. See below for more.
7565 -- Fortunately the way regular components are inherited can be handled in
7566 -- the same way in tagged and untagged types.
7568 -- To complicate things a bit more the private view of a private extension
7569 -- cannot be handled in the same way as the full view (for one thing the
7570 -- semantic rules are somewhat different). We will explain what differs
7573 -- 2. DISCRIMINANTS UNDER INHERITANCE
7575 -- The semantic rules governing the discriminants of derived types are
7578 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7579 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7581 -- If parent type has discriminants, then the discriminants that are
7582 -- declared in the derived type are [3.4 (11)]:
7584 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7587 -- o Otherwise, each discriminant of the parent type (implicitly declared
7588 -- in the same order with the same specifications). In this case, the
7589 -- discriminants are said to be "inherited", or if unknown in the parent
7590 -- are also unknown in the derived type.
7592 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7594 -- o The parent subtype must be constrained;
7596 -- o If the parent type is not a tagged type, then each discriminant of
7597 -- the derived type must be used in the constraint defining a parent
7598 -- subtype. [Implementation note: This ensures that the new discriminant
7599 -- can share storage with an existing discriminant.]
7601 -- For the derived type each discriminant of the parent type is either
7602 -- inherited, constrained to equal some new discriminant of the derived
7603 -- type, or constrained to the value of an expression.
7605 -- When inherited or constrained to equal some new discriminant, the
7606 -- parent discriminant and the discriminant of the derived type are said
7609 -- If a discriminant of the parent type is constrained to a specific value
7610 -- in the derived type definition, then the discriminant is said to be
7611 -- "specified" by that derived type definition.
7613 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7615 -- We have spoken about stored discriminants in point 1 (introduction)
7616 -- above. There are two sort of stored discriminants: implicit and
7617 -- explicit. As long as the derived type inherits the same discriminants as
7618 -- the root record type, stored discriminants are the same as regular
7619 -- discriminants, and are said to be implicit. However, if any discriminant
7620 -- in the root type was renamed in the derived type, then the derived
7621 -- type will contain explicit stored discriminants. Explicit stored
7622 -- discriminants are discriminants in addition to the semantically visible
7623 -- discriminants defined for the derived type. Stored discriminants are
7624 -- used by Gigi to figure out what are the physical discriminants in
7625 -- objects of the derived type (see precise definition in einfo.ads).
7626 -- As an example, consider the following:
7628 -- type R (D1, D2, D3 : Int) is record ... end record;
7629 -- type T1 is new R;
7630 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7631 -- type T3 is new T2;
7632 -- type T4 (Y : Int) is new T3 (Y, 99);
7634 -- The following table summarizes the discriminants and stored
7635 -- discriminants in R and T1 through T4.
7637 -- Type Discrim Stored Discrim Comment
7638 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7639 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7640 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7641 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7642 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7644 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7645 -- find the corresponding discriminant in the parent type, while
7646 -- Original_Record_Component (abbreviated ORC below), the actual physical
7647 -- component that is renamed. Finally the field Is_Completely_Hidden
7648 -- (abbreviated ICH below) is set for all explicit stored discriminants
7649 -- (see einfo.ads for more info). For the above example this gives:
7651 -- Discrim CD ORC ICH
7652 -- ^^^^^^^ ^^ ^^^ ^^^
7653 -- D1 in R empty itself no
7654 -- D2 in R empty itself no
7655 -- D3 in R empty itself no
7657 -- D1 in T1 D1 in R itself no
7658 -- D2 in T1 D2 in R itself no
7659 -- D3 in T1 D3 in R itself no
7661 -- X1 in T2 D3 in T1 D3 in T2 no
7662 -- X2 in T2 D1 in T1 D1 in T2 no
7663 -- D1 in T2 empty itself yes
7664 -- D2 in T2 empty itself yes
7665 -- D3 in T2 empty itself yes
7667 -- X1 in T3 X1 in T2 D3 in T3 no
7668 -- X2 in T3 X2 in T2 D1 in T3 no
7669 -- D1 in T3 empty itself yes
7670 -- D2 in T3 empty itself yes
7671 -- D3 in T3 empty itself yes
7673 -- Y in T4 X1 in T3 D3 in T3 no
7674 -- D1 in T3 empty itself yes
7675 -- D2 in T3 empty itself yes
7676 -- D3 in T3 empty itself yes
7678 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7680 -- Type derivation for tagged types is fairly straightforward. If no
7681 -- discriminants are specified by the derived type, these are inherited
7682 -- from the parent. No explicit stored discriminants are ever necessary.
7683 -- The only manipulation that is done to the tree is that of adding a
7684 -- _parent field with parent type and constrained to the same constraint
7685 -- specified for the parent in the derived type definition. For instance:
7687 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7688 -- type T1 is new R with null record;
7689 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7691 -- are changed into:
7693 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7694 -- _parent : R (D1, D2, D3);
7697 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7698 -- _parent : T1 (X2, 88, X1);
7701 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7702 -- ORC and ICH fields are:
7704 -- Discrim CD ORC ICH
7705 -- ^^^^^^^ ^^ ^^^ ^^^
7706 -- D1 in R empty itself no
7707 -- D2 in R empty itself no
7708 -- D3 in R empty itself no
7710 -- D1 in T1 D1 in R D1 in R no
7711 -- D2 in T1 D2 in R D2 in R no
7712 -- D3 in T1 D3 in R D3 in R no
7714 -- X1 in T2 D3 in T1 D3 in R no
7715 -- X2 in T2 D1 in T1 D1 in R no
7717 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7719 -- Regardless of whether we dealing with a tagged or untagged type
7720 -- we will transform all derived type declarations of the form
7722 -- type T is new R (...) [with ...];
7724 -- subtype S is R (...);
7725 -- type T is new S [with ...];
7727 -- type BT is new R [with ...];
7728 -- subtype T is BT (...);
7730 -- That is, the base derived type is constrained only if it has no
7731 -- discriminants. The reason for doing this is that GNAT's semantic model
7732 -- assumes that a base type with discriminants is unconstrained.
7734 -- Note that, strictly speaking, the above transformation is not always
7735 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7737 -- procedure B34011A is
7738 -- type REC (D : integer := 0) is record
7743 -- type T6 is new Rec;
7744 -- function F return T6;
7749 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7752 -- The definition of Q6.U is illegal. However transforming Q6.U into
7754 -- type BaseU is new T6;
7755 -- subtype U is BaseU (Q6.F.I)
7757 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7758 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7759 -- the transformation described above.
7761 -- There is another instance where the above transformation is incorrect.
7765 -- type Base (D : Integer) is tagged null record;
7766 -- procedure P (X : Base);
7768 -- type Der is new Base (2) with null record;
7769 -- procedure P (X : Der);
7772 -- Then the above transformation turns this into
7774 -- type Der_Base is new Base with null record;
7775 -- -- procedure P (X : Base) is implicitly inherited here
7776 -- -- as procedure P (X : Der_Base).
7778 -- subtype Der is Der_Base (2);
7779 -- procedure P (X : Der);
7780 -- -- The overriding of P (X : Der_Base) is illegal since we
7781 -- -- have a parameter conformance problem.
7783 -- To get around this problem, after having semantically processed Der_Base
7784 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7785 -- Discriminant_Constraint from Der so that when parameter conformance is
7786 -- checked when P is overridden, no semantic errors are flagged.
7788 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7790 -- Regardless of whether we are dealing with a tagged or untagged type
7791 -- we will transform all derived type declarations of the form
7793 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7794 -- type T is new R [with ...];
7796 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7798 -- The reason for such transformation is that it allows us to implement a
7799 -- very clean form of component inheritance as explained below.
7801 -- Note that this transformation is not achieved by direct tree rewriting
7802 -- and manipulation, but rather by redoing the semantic actions that the
7803 -- above transformation will entail. This is done directly in routine
7804 -- Inherit_Components.
7806 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7808 -- In both tagged and untagged derived types, regular non discriminant
7809 -- components are inherited in the derived type from the parent type. In
7810 -- the absence of discriminants component, inheritance is straightforward
7811 -- as components can simply be copied from the parent.
7813 -- If the parent has discriminants, inheriting components constrained with
7814 -- these discriminants requires caution. Consider the following example:
7816 -- type R (D1, D2 : Positive) is [tagged] record
7817 -- S : String (D1 .. D2);
7820 -- type T1 is new R [with null record];
7821 -- type T2 (X : positive) is new R (1, X) [with null record];
7823 -- As explained in 6. above, T1 is rewritten as
7824 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7825 -- which makes the treatment for T1 and T2 identical.
7827 -- What we want when inheriting S, is that references to D1 and D2 in R are
7828 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7829 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7830 -- with either discriminant references in the derived type or expressions.
7831 -- This replacement is achieved as follows: before inheriting R's
7832 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7833 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7834 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7835 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7836 -- by String (1 .. X).
7838 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7840 -- We explain here the rules governing private type extensions relevant to
7841 -- type derivation. These rules are explained on the following example:
7843 -- type D [(...)] is new A [(...)] with private; <-- partial view
7844 -- type D [(...)] is new P [(...)] with null record; <-- full view
7846 -- Type A is called the ancestor subtype of the private extension.
7847 -- Type P is the parent type of the full view of the private extension. It
7848 -- must be A or a type derived from A.
7850 -- The rules concerning the discriminants of private type extensions are
7853 -- o If a private extension inherits known discriminants from the ancestor
7854 -- subtype, then the full view must also inherit its discriminants from
7855 -- the ancestor subtype and the parent subtype of the full view must be
7856 -- constrained if and only if the ancestor subtype is constrained.
7858 -- o If a partial view has unknown discriminants, then the full view may
7859 -- define a definite or an indefinite subtype, with or without
7862 -- o If a partial view has neither known nor unknown discriminants, then
7863 -- the full view must define a definite subtype.
7865 -- o If the ancestor subtype of a private extension has constrained
7866 -- discriminants, then the parent subtype of the full view must impose a
7867 -- statically matching constraint on those discriminants.
7869 -- This means that only the following forms of private extensions are
7872 -- type D is new A with private; <-- partial view
7873 -- type D is new P with null record; <-- full view
7875 -- If A has no discriminants than P has no discriminants, otherwise P must
7876 -- inherit A's discriminants.
7878 -- type D is new A (...) with private; <-- partial view
7879 -- type D is new P (:::) with null record; <-- full view
7881 -- P must inherit A's discriminants and (...) and (:::) must statically
7884 -- subtype A is R (...);
7885 -- type D is new A with private; <-- partial view
7886 -- type D is new P with null record; <-- full view
7888 -- P must have inherited R's discriminants and must be derived from A or
7889 -- any of its subtypes.
7891 -- type D (..) is new A with private; <-- partial view
7892 -- type D (..) is new P [(:::)] with null record; <-- full view
7894 -- No specific constraints on P's discriminants or constraint (:::).
7895 -- Note that A can be unconstrained, but the parent subtype P must either
7896 -- be constrained or (:::) must be present.
7898 -- type D (..) is new A [(...)] with private; <-- partial view
7899 -- type D (..) is new P [(:::)] with null record; <-- full view
7901 -- P's constraints on A's discriminants must statically match those
7902 -- imposed by (...).
7904 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
7906 -- The full view of a private extension is handled exactly as described
7907 -- above. The model chose for the private view of a private extension is
7908 -- the same for what concerns discriminants (i.e. they receive the same
7909 -- treatment as in the tagged case). However, the private view of the
7910 -- private extension always inherits the components of the parent base,
7911 -- without replacing any discriminant reference. Strictly speaking this is
7912 -- incorrect. However, Gigi never uses this view to generate code so this
7913 -- is a purely semantic issue. In theory, a set of transformations similar
7914 -- to those given in 5. and 6. above could be applied to private views of
7915 -- private extensions to have the same model of component inheritance as
7916 -- for non private extensions. However, this is not done because it would
7917 -- further complicate private type processing. Semantically speaking, this
7918 -- leaves us in an uncomfortable situation. As an example consider:
7921 -- type R (D : integer) is tagged record
7922 -- S : String (1 .. D);
7924 -- procedure P (X : R);
7925 -- type T is new R (1) with private;
7927 -- type T is new R (1) with null record;
7930 -- This is transformed into:
7933 -- type R (D : integer) is tagged record
7934 -- S : String (1 .. D);
7936 -- procedure P (X : R);
7937 -- type T is new R (1) with private;
7939 -- type BaseT is new R with null record;
7940 -- subtype T is BaseT (1);
7943 -- (strictly speaking the above is incorrect Ada)
7945 -- From the semantic standpoint the private view of private extension T
7946 -- should be flagged as constrained since one can clearly have
7950 -- in a unit withing Pack. However, when deriving subprograms for the
7951 -- private view of private extension T, T must be seen as unconstrained
7952 -- since T has discriminants (this is a constraint of the current
7953 -- subprogram derivation model). Thus, when processing the private view of
7954 -- a private extension such as T, we first mark T as unconstrained, we
7955 -- process it, we perform program derivation and just before returning from
7956 -- Build_Derived_Record_Type we mark T as constrained.
7958 -- ??? Are there are other uncomfortable cases that we will have to
7961 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7963 -- Types that are derived from a visible record type and have a private
7964 -- extension present other peculiarities. They behave mostly like private
7965 -- types, but if they have primitive operations defined, these will not
7966 -- have the proper signatures for further inheritance, because other
7967 -- primitive operations will use the implicit base that we define for
7968 -- private derivations below. This affect subprogram inheritance (see
7969 -- Derive_Subprograms for details). We also derive the implicit base from
7970 -- the base type of the full view, so that the implicit base is a record
7971 -- type and not another private type, This avoids infinite loops.
7973 procedure Build_Derived_Record_Type
7975 Parent_Type : Entity_Id;
7976 Derived_Type : Entity_Id;
7977 Derive_Subps : Boolean := True)
7979 Discriminant_Specs : constant Boolean :=
7980 Present (Discriminant_Specifications (N));
7981 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7982 Loc : constant Source_Ptr := Sloc (N);
7983 Private_Extension : constant Boolean :=
7984 Nkind (N) = N_Private_Extension_Declaration;
7985 Assoc_List : Elist_Id;
7986 Constraint_Present : Boolean;
7988 Discrim : Entity_Id;
7990 Inherit_Discrims : Boolean := False;
7991 Last_Discrim : Entity_Id;
7992 New_Base : Entity_Id;
7994 New_Discrs : Elist_Id;
7995 New_Indic : Node_Id;
7996 Parent_Base : Entity_Id;
7997 Save_Etype : Entity_Id;
7998 Save_Discr_Constr : Elist_Id;
7999 Save_Next_Entity : Entity_Id;
8002 Discs : Elist_Id := New_Elmt_List;
8003 -- An empty Discs list means that there were no constraints in the
8004 -- subtype indication or that there was an error processing it.
8007 if Ekind (Parent_Type) = E_Record_Type_With_Private
8008 and then Present (Full_View (Parent_Type))
8009 and then Has_Discriminants (Parent_Type)
8011 Parent_Base := Base_Type (Full_View (Parent_Type));
8013 Parent_Base := Base_Type (Parent_Type);
8016 -- AI05-0115 : if this is a derivation from a private type in some
8017 -- other scope that may lead to invisible components for the derived
8018 -- type, mark it accordingly.
8020 if Is_Private_Type (Parent_Type) then
8021 if Scope (Parent_Type) = Scope (Derived_Type) then
8024 elsif In_Open_Scopes (Scope (Parent_Type))
8025 and then In_Private_Part (Scope (Parent_Type))
8030 Set_Has_Private_Ancestor (Derived_Type);
8034 Set_Has_Private_Ancestor
8035 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8038 -- Before we start the previously documented transformations, here is
8039 -- little fix for size and alignment of tagged types. Normally when we
8040 -- derive type D from type P, we copy the size and alignment of P as the
8041 -- default for D, and in the absence of explicit representation clauses
8042 -- for D, the size and alignment are indeed the same as the parent.
8044 -- But this is wrong for tagged types, since fields may be added, and
8045 -- the default size may need to be larger, and the default alignment may
8046 -- need to be larger.
8048 -- We therefore reset the size and alignment fields in the tagged case.
8049 -- Note that the size and alignment will in any case be at least as
8050 -- large as the parent type (since the derived type has a copy of the
8051 -- parent type in the _parent field)
8053 -- The type is also marked as being tagged here, which is needed when
8054 -- processing components with a self-referential anonymous access type
8055 -- in the call to Check_Anonymous_Access_Components below. Note that
8056 -- this flag is also set later on for completeness.
8059 Set_Is_Tagged_Type (Derived_Type);
8060 Init_Size_Align (Derived_Type);
8063 -- STEP 0a: figure out what kind of derived type declaration we have
8065 if Private_Extension then
8067 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8068 Set_Default_SSO (Derived_Type);
8071 Type_Def := Type_Definition (N);
8073 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8074 -- Parent_Base can be a private type or private extension. However,
8075 -- for tagged types with an extension the newly added fields are
8076 -- visible and hence the Derived_Type is always an E_Record_Type.
8077 -- (except that the parent may have its own private fields).
8078 -- For untagged types we preserve the Ekind of the Parent_Base.
8080 if Present (Record_Extension_Part (Type_Def)) then
8081 Set_Ekind (Derived_Type, E_Record_Type);
8082 Set_Default_SSO (Derived_Type);
8084 -- Create internal access types for components with anonymous
8087 if Ada_Version >= Ada_2005 then
8088 Check_Anonymous_Access_Components
8089 (N, Derived_Type, Derived_Type,
8090 Component_List (Record_Extension_Part (Type_Def)));
8094 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8098 -- Indic can either be an N_Identifier if the subtype indication
8099 -- contains no constraint or an N_Subtype_Indication if the subtype
8100 -- indication has a constraint.
8102 Indic := Subtype_Indication (Type_Def);
8103 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8105 -- Check that the type has visible discriminants. The type may be
8106 -- a private type with unknown discriminants whose full view has
8107 -- discriminants which are invisible.
8109 if Constraint_Present then
8110 if not Has_Discriminants (Parent_Base)
8112 (Has_Unknown_Discriminants (Parent_Base)
8113 and then Is_Private_Type (Parent_Base))
8116 ("invalid constraint: type has no discriminant",
8117 Constraint (Indic));
8119 Constraint_Present := False;
8120 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8122 elsif Is_Constrained (Parent_Type) then
8124 ("invalid constraint: parent type is already constrained",
8125 Constraint (Indic));
8127 Constraint_Present := False;
8128 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8132 -- STEP 0b: If needed, apply transformation given in point 5. above
8134 if not Private_Extension
8135 and then Has_Discriminants (Parent_Type)
8136 and then not Discriminant_Specs
8137 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8139 -- First, we must analyze the constraint (see comment in point 5.)
8140 -- The constraint may come from the subtype indication of the full
8143 if Constraint_Present then
8144 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8146 -- If there is no explicit constraint, there might be one that is
8147 -- inherited from a constrained parent type. In that case verify that
8148 -- it conforms to the constraint in the partial view. In perverse
8149 -- cases the parent subtypes of the partial and full view can have
8150 -- different constraints.
8152 elsif Present (Stored_Constraint (Parent_Type)) then
8153 New_Discrs := Stored_Constraint (Parent_Type);
8156 New_Discrs := No_Elist;
8159 if Has_Discriminants (Derived_Type)
8160 and then Has_Private_Declaration (Derived_Type)
8161 and then Present (Discriminant_Constraint (Derived_Type))
8162 and then Present (New_Discrs)
8164 -- Verify that constraints of the full view statically match
8165 -- those given in the partial view.
8171 C1 := First_Elmt (New_Discrs);
8172 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8173 while Present (C1) and then Present (C2) loop
8174 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8176 (Is_OK_Static_Expression (Node (C1))
8177 and then Is_OK_Static_Expression (Node (C2))
8179 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8184 if Constraint_Present then
8186 ("constraint not conformant to previous declaration",
8190 ("constraint of full view is incompatible "
8191 & "with partial view", N);
8201 -- Insert and analyze the declaration for the unconstrained base type
8203 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8206 Make_Full_Type_Declaration (Loc,
8207 Defining_Identifier => New_Base,
8209 Make_Derived_Type_Definition (Loc,
8210 Abstract_Present => Abstract_Present (Type_Def),
8211 Limited_Present => Limited_Present (Type_Def),
8212 Subtype_Indication =>
8213 New_Occurrence_Of (Parent_Base, Loc),
8214 Record_Extension_Part =>
8215 Relocate_Node (Record_Extension_Part (Type_Def)),
8216 Interface_List => Interface_List (Type_Def)));
8218 Set_Parent (New_Decl, Parent (N));
8219 Mark_Rewrite_Insertion (New_Decl);
8220 Insert_Before (N, New_Decl);
8222 -- In the extension case, make sure ancestor is frozen appropriately
8223 -- (see also non-discriminated case below).
8225 if Present (Record_Extension_Part (Type_Def))
8226 or else Is_Interface (Parent_Base)
8228 Freeze_Before (New_Decl, Parent_Type);
8231 -- Note that this call passes False for the Derive_Subps parameter
8232 -- because subprogram derivation is deferred until after creating
8233 -- the subtype (see below).
8236 (New_Decl, Parent_Base, New_Base,
8237 Is_Completion => False, Derive_Subps => False);
8239 -- ??? This needs re-examination to determine whether the
8240 -- above call can simply be replaced by a call to Analyze.
8242 Set_Analyzed (New_Decl);
8244 -- Insert and analyze the declaration for the constrained subtype
8246 if Constraint_Present then
8248 Make_Subtype_Indication (Loc,
8249 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8250 Constraint => Relocate_Node (Constraint (Indic)));
8254 Constr_List : constant List_Id := New_List;
8259 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8260 while Present (C) loop
8263 -- It is safe here to call New_Copy_Tree since we called
8264 -- Force_Evaluation on each constraint previously
8265 -- in Build_Discriminant_Constraints.
8267 Append (New_Copy_Tree (Expr), To => Constr_List);
8273 Make_Subtype_Indication (Loc,
8274 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8276 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8281 Make_Subtype_Declaration (Loc,
8282 Defining_Identifier => Derived_Type,
8283 Subtype_Indication => New_Indic));
8287 -- Derivation of subprograms must be delayed until the full subtype
8288 -- has been established, to ensure proper overriding of subprograms
8289 -- inherited by full types. If the derivations occurred as part of
8290 -- the call to Build_Derived_Type above, then the check for type
8291 -- conformance would fail because earlier primitive subprograms
8292 -- could still refer to the full type prior the change to the new
8293 -- subtype and hence would not match the new base type created here.
8294 -- Subprograms are not derived, however, when Derive_Subps is False
8295 -- (since otherwise there could be redundant derivations).
8297 if Derive_Subps then
8298 Derive_Subprograms (Parent_Type, Derived_Type);
8301 -- For tagged types the Discriminant_Constraint of the new base itype
8302 -- is inherited from the first subtype so that no subtype conformance
8303 -- problem arise when the first subtype overrides primitive
8304 -- operations inherited by the implicit base type.
8307 Set_Discriminant_Constraint
8308 (New_Base, Discriminant_Constraint (Derived_Type));
8314 -- If we get here Derived_Type will have no discriminants or it will be
8315 -- a discriminated unconstrained base type.
8317 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8321 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8322 -- The declaration of a specific descendant of an interface type
8323 -- freezes the interface type (RM 13.14).
8325 if not Private_Extension or else Is_Interface (Parent_Base) then
8326 Freeze_Before (N, Parent_Type);
8329 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8330 -- cannot be declared at a deeper level than its parent type is
8331 -- removed. The check on derivation within a generic body is also
8332 -- relaxed, but there's a restriction that a derived tagged type
8333 -- cannot be declared in a generic body if it's derived directly
8334 -- or indirectly from a formal type of that generic.
8336 if Ada_Version >= Ada_2005 then
8337 if Present (Enclosing_Generic_Body (Derived_Type)) then
8339 Ancestor_Type : Entity_Id;
8342 -- Check to see if any ancestor of the derived type is a
8345 Ancestor_Type := Parent_Type;
8346 while not Is_Generic_Type (Ancestor_Type)
8347 and then Etype (Ancestor_Type) /= Ancestor_Type
8349 Ancestor_Type := Etype (Ancestor_Type);
8352 -- If the derived type does have a formal type as an
8353 -- ancestor, then it's an error if the derived type is
8354 -- declared within the body of the generic unit that
8355 -- declares the formal type in its generic formal part. It's
8356 -- sufficient to check whether the ancestor type is declared
8357 -- inside the same generic body as the derived type (such as
8358 -- within a nested generic spec), in which case the
8359 -- derivation is legal. If the formal type is declared
8360 -- outside of that generic body, then it's guaranteed that
8361 -- the derived type is declared within the generic body of
8362 -- the generic unit declaring the formal type.
8364 if Is_Generic_Type (Ancestor_Type)
8365 and then Enclosing_Generic_Body (Ancestor_Type) /=
8366 Enclosing_Generic_Body (Derived_Type)
8369 ("parent type of& must not be descendant of formal type"
8370 & " of an enclosing generic body",
8371 Indic, Derived_Type);
8376 elsif Type_Access_Level (Derived_Type) /=
8377 Type_Access_Level (Parent_Type)
8378 and then not Is_Generic_Type (Derived_Type)
8380 if Is_Controlled (Parent_Type) then
8382 ("controlled type must be declared at the library level",
8386 ("type extension at deeper accessibility level than parent",
8392 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8395 and then GB /= Enclosing_Generic_Body (Parent_Base)
8398 ("parent type of& must not be outside generic body"
8400 Indic, Derived_Type);
8406 -- Ada 2005 (AI-251)
8408 if Ada_Version >= Ada_2005 and then Is_Tagged then
8410 -- "The declaration of a specific descendant of an interface type
8411 -- freezes the interface type" (RM 13.14).
8416 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8417 Iface := First (Interface_List (Type_Def));
8418 while Present (Iface) loop
8419 Freeze_Before (N, Etype (Iface));
8426 -- STEP 1b : preliminary cleanup of the full view of private types
8428 -- If the type is already marked as having discriminants, then it's the
8429 -- completion of a private type or private extension and we need to
8430 -- retain the discriminants from the partial view if the current
8431 -- declaration has Discriminant_Specifications so that we can verify
8432 -- conformance. However, we must remove any existing components that
8433 -- were inherited from the parent (and attached in Copy_And_Swap)
8434 -- because the full type inherits all appropriate components anyway, and
8435 -- we do not want the partial view's components interfering.
8437 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8438 Discrim := First_Discriminant (Derived_Type);
8440 Last_Discrim := Discrim;
8441 Next_Discriminant (Discrim);
8442 exit when No (Discrim);
8445 Set_Last_Entity (Derived_Type, Last_Discrim);
8447 -- In all other cases wipe out the list of inherited components (even
8448 -- inherited discriminants), it will be properly rebuilt here.
8451 Set_First_Entity (Derived_Type, Empty);
8452 Set_Last_Entity (Derived_Type, Empty);
8455 -- STEP 1c: Initialize some flags for the Derived_Type
8457 -- The following flags must be initialized here so that
8458 -- Process_Discriminants can check that discriminants of tagged types do
8459 -- not have a default initial value and that access discriminants are
8460 -- only specified for limited records. For completeness, these flags are
8461 -- also initialized along with all the other flags below.
8463 -- AI-419: Limitedness is not inherited from an interface parent, so to
8464 -- be limited in that case the type must be explicitly declared as
8465 -- limited. However, task and protected interfaces are always limited.
8467 if Limited_Present (Type_Def) then
8468 Set_Is_Limited_Record (Derived_Type);
8470 elsif Is_Limited_Record (Parent_Type)
8471 or else (Present (Full_View (Parent_Type))
8472 and then Is_Limited_Record (Full_View (Parent_Type)))
8474 if not Is_Interface (Parent_Type)
8475 or else Is_Synchronized_Interface (Parent_Type)
8476 or else Is_Protected_Interface (Parent_Type)
8477 or else Is_Task_Interface (Parent_Type)
8479 Set_Is_Limited_Record (Derived_Type);
8483 -- STEP 2a: process discriminants of derived type if any
8485 Push_Scope (Derived_Type);
8487 if Discriminant_Specs then
8488 Set_Has_Unknown_Discriminants (Derived_Type, False);
8490 -- The following call initializes fields Has_Discriminants and
8491 -- Discriminant_Constraint, unless we are processing the completion
8492 -- of a private type declaration.
8494 Check_Or_Process_Discriminants (N, Derived_Type);
8496 -- For untagged types, the constraint on the Parent_Type must be
8497 -- present and is used to rename the discriminants.
8499 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8500 Error_Msg_N ("untagged parent must have discriminants", Indic);
8502 elsif not Is_Tagged and then not Constraint_Present then
8504 ("discriminant constraint needed for derived untagged records",
8507 -- Otherwise the parent subtype must be constrained unless we have a
8508 -- private extension.
8510 elsif not Constraint_Present
8511 and then not Private_Extension
8512 and then not Is_Constrained (Parent_Type)
8515 ("unconstrained type not allowed in this context", Indic);
8517 elsif Constraint_Present then
8518 -- The following call sets the field Corresponding_Discriminant
8519 -- for the discriminants in the Derived_Type.
8521 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8523 -- For untagged types all new discriminants must rename
8524 -- discriminants in the parent. For private extensions new
8525 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8527 Discrim := First_Discriminant (Derived_Type);
8528 while Present (Discrim) loop
8530 and then No (Corresponding_Discriminant (Discrim))
8533 ("new discriminants must constrain old ones", Discrim);
8535 elsif Private_Extension
8536 and then Present (Corresponding_Discriminant (Discrim))
8539 ("only static constraints allowed for parent"
8540 & " discriminants in the partial view", Indic);
8544 -- If a new discriminant is used in the constraint, then its
8545 -- subtype must be statically compatible with the parent
8546 -- discriminant's subtype (3.7(15)).
8548 -- However, if the record contains an array constrained by
8549 -- the discriminant but with some different bound, the compiler
8550 -- attemps to create a smaller range for the discriminant type.
8551 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8552 -- the discriminant type is a scalar type, the check must use
8553 -- the original discriminant type in the parent declaration.
8556 Corr_Disc : constant Entity_Id :=
8557 Corresponding_Discriminant (Discrim);
8558 Disc_Type : constant Entity_Id := Etype (Discrim);
8559 Corr_Type : Entity_Id;
8562 if Present (Corr_Disc) then
8563 if Is_Scalar_Type (Disc_Type) then
8565 Entity (Discriminant_Type (Parent (Corr_Disc)));
8567 Corr_Type := Etype (Corr_Disc);
8571 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8574 ("subtype must be compatible "
8575 & "with parent discriminant",
8581 Next_Discriminant (Discrim);
8584 -- Check whether the constraints of the full view statically
8585 -- match those imposed by the parent subtype [7.3(13)].
8587 if Present (Stored_Constraint (Derived_Type)) then
8592 C1 := First_Elmt (Discs);
8593 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8594 while Present (C1) and then Present (C2) loop
8596 Fully_Conformant_Expressions (Node (C1), Node (C2))
8599 ("not conformant with previous declaration",
8610 -- STEP 2b: No new discriminants, inherit discriminants if any
8613 if Private_Extension then
8614 Set_Has_Unknown_Discriminants
8616 Has_Unknown_Discriminants (Parent_Type)
8617 or else Unknown_Discriminants_Present (N));
8619 -- The partial view of the parent may have unknown discriminants,
8620 -- but if the full view has discriminants and the parent type is
8621 -- in scope they must be inherited.
8623 elsif Has_Unknown_Discriminants (Parent_Type)
8625 (not Has_Discriminants (Parent_Type)
8626 or else not In_Open_Scopes (Scope (Parent_Type)))
8628 Set_Has_Unknown_Discriminants (Derived_Type);
8631 if not Has_Unknown_Discriminants (Derived_Type)
8632 and then not Has_Unknown_Discriminants (Parent_Base)
8633 and then Has_Discriminants (Parent_Type)
8635 Inherit_Discrims := True;
8636 Set_Has_Discriminants
8637 (Derived_Type, True);
8638 Set_Discriminant_Constraint
8639 (Derived_Type, Discriminant_Constraint (Parent_Base));
8642 -- The following test is true for private types (remember
8643 -- transformation 5. is not applied to those) and in an error
8646 if Constraint_Present then
8647 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8650 -- For now mark a new derived type as constrained only if it has no
8651 -- discriminants. At the end of Build_Derived_Record_Type we properly
8652 -- set this flag in the case of private extensions. See comments in
8653 -- point 9. just before body of Build_Derived_Record_Type.
8657 not (Inherit_Discrims
8658 or else Has_Unknown_Discriminants (Derived_Type)));
8661 -- STEP 3: initialize fields of derived type
8663 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8664 Set_Stored_Constraint (Derived_Type, No_Elist);
8666 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8667 -- but cannot be interfaces
8669 if not Private_Extension
8670 and then Ekind (Derived_Type) /= E_Private_Type
8671 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8673 if Interface_Present (Type_Def) then
8674 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8677 Set_Interfaces (Derived_Type, No_Elist);
8680 -- Fields inherited from the Parent_Type
8682 Set_Has_Specified_Layout
8683 (Derived_Type, Has_Specified_Layout (Parent_Type));
8684 Set_Is_Limited_Composite
8685 (Derived_Type, Is_Limited_Composite (Parent_Type));
8686 Set_Is_Private_Composite
8687 (Derived_Type, Is_Private_Composite (Parent_Type));
8689 if Is_Tagged_Type (Parent_Type) then
8690 Set_No_Tagged_Streams_Pragma
8691 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8694 -- Fields inherited from the Parent_Base
8696 Set_Has_Controlled_Component
8697 (Derived_Type, Has_Controlled_Component (Parent_Base));
8698 Set_Has_Non_Standard_Rep
8699 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8700 Set_Has_Primitive_Operations
8701 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8703 -- Fields inherited from the Parent_Base in the non-private case
8705 if Ekind (Derived_Type) = E_Record_Type then
8706 Set_Has_Complex_Representation
8707 (Derived_Type, Has_Complex_Representation (Parent_Base));
8710 -- Fields inherited from the Parent_Base for record types
8712 if Is_Record_Type (Derived_Type) then
8714 Parent_Full : Entity_Id;
8717 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8718 -- Parent_Base can be a private type or private extension. Go
8719 -- to the full view here to get the E_Record_Type specific flags.
8721 if Present (Full_View (Parent_Base)) then
8722 Parent_Full := Full_View (Parent_Base);
8724 Parent_Full := Parent_Base;
8727 Set_OK_To_Reorder_Components
8728 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8732 -- Set fields for private derived types
8734 if Is_Private_Type (Derived_Type) then
8735 Set_Depends_On_Private (Derived_Type, True);
8736 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8738 -- Inherit fields from non private record types. If this is the
8739 -- completion of a derivation from a private type, the parent itself
8740 -- is private, and the attributes come from its full view, which must
8744 if Is_Private_Type (Parent_Base)
8745 and then not Is_Record_Type (Parent_Base)
8747 Set_Component_Alignment
8748 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8750 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8752 Set_Component_Alignment
8753 (Derived_Type, Component_Alignment (Parent_Base));
8755 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8759 -- Set fields for tagged types
8762 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8764 -- All tagged types defined in Ada.Finalization are controlled
8766 if Chars (Scope (Derived_Type)) = Name_Finalization
8767 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8768 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8770 Set_Is_Controlled (Derived_Type);
8772 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8775 -- Minor optimization: there is no need to generate the class-wide
8776 -- entity associated with an underlying record view.
8778 if not Is_Underlying_Record_View (Derived_Type) then
8779 Make_Class_Wide_Type (Derived_Type);
8782 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8784 if Has_Discriminants (Derived_Type)
8785 and then Constraint_Present
8787 Set_Stored_Constraint
8788 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8791 if Ada_Version >= Ada_2005 then
8793 Ifaces_List : Elist_Id;
8796 -- Checks rules 3.9.4 (13/2 and 14/2)
8798 if Comes_From_Source (Derived_Type)
8799 and then not Is_Private_Type (Derived_Type)
8800 and then Is_Interface (Parent_Type)
8801 and then not Is_Interface (Derived_Type)
8803 if Is_Task_Interface (Parent_Type) then
8805 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8808 elsif Is_Protected_Interface (Parent_Type) then
8810 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8815 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8817 Check_Interfaces (N, Type_Def);
8819 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8820 -- not already in the parents.
8824 Ifaces_List => Ifaces_List,
8825 Exclude_Parents => True);
8827 Set_Interfaces (Derived_Type, Ifaces_List);
8829 -- If the derived type is the anonymous type created for
8830 -- a declaration whose parent has a constraint, propagate
8831 -- the interface list to the source type. This must be done
8832 -- prior to the completion of the analysis of the source type
8833 -- because the components in the extension may contain current
8834 -- instances whose legality depends on some ancestor.
8836 if Is_Itype (Derived_Type) then
8838 Def : constant Node_Id :=
8839 Associated_Node_For_Itype (Derived_Type);
8842 and then Nkind (Def) = N_Full_Type_Declaration
8845 (Defining_Identifier (Def), Ifaces_List);
8850 -- A type extension is automatically Ghost when one of its
8851 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
8852 -- also inherited when the parent type is Ghost, but this is
8853 -- done in Build_Derived_Type as the mechanism also handles
8854 -- untagged derivations.
8856 if Implements_Ghost_Interface (Derived_Type) then
8857 Set_Is_Ghost_Entity (Derived_Type);
8863 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8864 Set_Has_Non_Standard_Rep
8865 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8868 -- STEP 4: Inherit components from the parent base and constrain them.
8869 -- Apply the second transformation described in point 6. above.
8871 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8872 or else not Has_Discriminants (Parent_Type)
8873 or else not Is_Constrained (Parent_Type)
8877 Constrs := Discriminant_Constraint (Parent_Type);
8882 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8884 -- STEP 5a: Copy the parent record declaration for untagged types
8886 if not Is_Tagged then
8888 -- Discriminant_Constraint (Derived_Type) has been properly
8889 -- constructed. Save it and temporarily set it to Empty because we
8890 -- do not want the call to New_Copy_Tree below to mess this list.
8892 if Has_Discriminants (Derived_Type) then
8893 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8894 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8896 Save_Discr_Constr := No_Elist;
8899 -- Save the Etype field of Derived_Type. It is correctly set now,
8900 -- but the call to New_Copy tree may remap it to point to itself,
8901 -- which is not what we want. Ditto for the Next_Entity field.
8903 Save_Etype := Etype (Derived_Type);
8904 Save_Next_Entity := Next_Entity (Derived_Type);
8906 -- Assoc_List maps all stored discriminants in the Parent_Base to
8907 -- stored discriminants in the Derived_Type. It is fundamental that
8908 -- no types or itypes with discriminants other than the stored
8909 -- discriminants appear in the entities declared inside
8910 -- Derived_Type, since the back end cannot deal with it.
8914 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8916 -- Restore the fields saved prior to the New_Copy_Tree call
8917 -- and compute the stored constraint.
8919 Set_Etype (Derived_Type, Save_Etype);
8920 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8922 if Has_Discriminants (Derived_Type) then
8923 Set_Discriminant_Constraint
8924 (Derived_Type, Save_Discr_Constr);
8925 Set_Stored_Constraint
8926 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
8927 Replace_Components (Derived_Type, New_Decl);
8928 Set_Has_Implicit_Dereference
8929 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
8932 -- Insert the new derived type declaration
8934 Rewrite (N, New_Decl);
8936 -- STEP 5b: Complete the processing for record extensions in generics
8938 -- There is no completion for record extensions declared in the
8939 -- parameter part of a generic, so we need to complete processing for
8940 -- these generic record extensions here. The Record_Type_Definition call
8941 -- will change the Ekind of the components from E_Void to E_Component.
8943 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8944 Record_Type_Definition (Empty, Derived_Type);
8946 -- STEP 5c: Process the record extension for non private tagged types
8948 elsif not Private_Extension then
8949 Expand_Record_Extension (Derived_Type, Type_Def);
8951 -- Note : previously in ASIS mode we set the Parent_Subtype of the
8952 -- derived type to propagate some semantic information. This led
8953 -- to other ASIS failures and has been removed.
8955 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8956 -- implemented interfaces if we are in expansion mode
8959 and then Has_Interfaces (Derived_Type)
8961 Add_Interface_Tag_Components (N, Derived_Type);
8964 -- Analyze the record extension
8966 Record_Type_Definition
8967 (Record_Extension_Part (Type_Def), Derived_Type);
8972 -- Nothing else to do if there is an error in the derivation.
8973 -- An unusual case: the full view may be derived from a type in an
8974 -- instance, when the partial view was used illegally as an actual
8975 -- in that instance, leading to a circular definition.
8977 if Etype (Derived_Type) = Any_Type
8978 or else Etype (Parent_Type) = Derived_Type
8983 -- Set delayed freeze and then derive subprograms, we need to do
8984 -- this in this order so that derived subprograms inherit the
8985 -- derived freeze if necessary.
8987 Set_Has_Delayed_Freeze (Derived_Type);
8989 if Derive_Subps then
8990 Derive_Subprograms (Parent_Type, Derived_Type);
8993 -- If we have a private extension which defines a constrained derived
8994 -- type mark as constrained here after we have derived subprograms. See
8995 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8997 if Private_Extension and then Inherit_Discrims then
8998 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8999 Set_Is_Constrained (Derived_Type, True);
9000 Set_Discriminant_Constraint (Derived_Type, Discs);
9002 elsif Is_Constrained (Parent_Type) then
9004 (Derived_Type, True);
9005 Set_Discriminant_Constraint
9006 (Derived_Type, Discriminant_Constraint (Parent_Type));
9010 -- Update the class-wide type, which shares the now-completed entity
9011 -- list with its specific type. In case of underlying record views,
9012 -- we do not generate the corresponding class wide entity.
9015 and then not Is_Underlying_Record_View (Derived_Type)
9018 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9020 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9023 Check_Function_Writable_Actuals (N);
9024 end Build_Derived_Record_Type;
9026 ------------------------
9027 -- Build_Derived_Type --
9028 ------------------------
9030 procedure Build_Derived_Type
9032 Parent_Type : Entity_Id;
9033 Derived_Type : Entity_Id;
9034 Is_Completion : Boolean;
9035 Derive_Subps : Boolean := True)
9037 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9040 -- Set common attributes
9042 Set_Scope (Derived_Type, Current_Scope);
9044 Set_Etype (Derived_Type, Parent_Base);
9045 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9046 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9048 Set_Size_Info (Derived_Type, Parent_Type);
9049 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9050 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
9051 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9053 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9054 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9056 if Is_Tagged_Type (Derived_Type) then
9057 Set_No_Tagged_Streams_Pragma
9058 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9061 -- If the parent has primitive routines, set the derived type link
9063 if Has_Primitive_Operations (Parent_Type) then
9064 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9067 -- If the parent type is a private subtype, the convention on the base
9068 -- type may be set in the private part, and not propagated to the
9069 -- subtype until later, so we obtain the convention from the base type.
9071 Set_Convention (Derived_Type, Convention (Parent_Base));
9073 -- Set SSO default for record or array type
9075 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9076 and then Is_Base_Type (Derived_Type)
9078 Set_Default_SSO (Derived_Type);
9081 -- A derived type inherits the Default_Initial_Condition pragma coming
9082 -- from any parent type within the derivation chain.
9084 if Has_DIC (Parent_Type) then
9085 Set_Has_Inherited_DIC (Derived_Type);
9088 -- A derived type inherits any class-wide invariants coming from a
9089 -- parent type or an interface. Note that the invariant procedure of
9090 -- the parent type should not be inherited because the derived type may
9091 -- define invariants of its own.
9093 if not Is_Interface (Derived_Type) then
9094 if Has_Inherited_Invariants (Parent_Type)
9095 or else Has_Inheritable_Invariants (Parent_Type)
9097 Set_Has_Inherited_Invariants (Derived_Type);
9099 elsif Is_Concurrent_Type (Derived_Type)
9100 or else Is_Tagged_Type (Derived_Type)
9105 Iface_Elmt : Elmt_Id;
9110 Ifaces_List => Ifaces,
9111 Exclude_Parents => True);
9113 if Present (Ifaces) then
9114 Iface_Elmt := First_Elmt (Ifaces);
9115 while Present (Iface_Elmt) loop
9116 Iface := Node (Iface_Elmt);
9118 if Has_Inheritable_Invariants (Iface) then
9119 Set_Has_Inherited_Invariants (Derived_Type);
9123 Next_Elmt (Iface_Elmt);
9130 -- We similarly inherit predicates
9132 if Has_Predicates (Parent_Type) then
9133 Set_Has_Predicates (Derived_Type);
9136 -- The derived type inherits the representation clauses of the parent
9138 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9140 -- If the parent type has delayed rep aspects, then mark the derived
9141 -- type as possibly inheriting a delayed rep aspect.
9143 if Has_Delayed_Rep_Aspects (Parent_Type) then
9144 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9147 -- Propagate the attributes related to pragma Ghost from the parent type
9148 -- to the derived type or type extension (SPARK RM 6.9(9)).
9150 if Is_Ghost_Entity (Parent_Type) then
9151 Set_Is_Ghost_Entity (Derived_Type);
9154 -- Type dependent processing
9156 case Ekind (Parent_Type) is
9157 when Numeric_Kind =>
9158 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9161 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9165 | Class_Wide_Kind =>
9166 Build_Derived_Record_Type
9167 (N, Parent_Type, Derived_Type, Derive_Subps);
9170 when Enumeration_Kind =>
9171 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9174 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9176 when Incomplete_Or_Private_Kind =>
9177 Build_Derived_Private_Type
9178 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9180 -- For discriminated types, the derivation includes deriving
9181 -- primitive operations. For others it is done below.
9183 if Is_Tagged_Type (Parent_Type)
9184 or else Has_Discriminants (Parent_Type)
9185 or else (Present (Full_View (Parent_Type))
9186 and then Has_Discriminants (Full_View (Parent_Type)))
9191 when Concurrent_Kind =>
9192 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9195 raise Program_Error;
9198 -- Nothing more to do if some error occurred
9200 if Etype (Derived_Type) = Any_Type then
9204 -- Set delayed freeze and then derive subprograms, we need to do this
9205 -- in this order so that derived subprograms inherit the derived freeze
9208 Set_Has_Delayed_Freeze (Derived_Type);
9210 if Derive_Subps then
9211 Derive_Subprograms (Parent_Type, Derived_Type);
9214 Set_Has_Primitive_Operations
9215 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9216 end Build_Derived_Type;
9218 -----------------------
9219 -- Build_Discriminal --
9220 -----------------------
9222 procedure Build_Discriminal (Discrim : Entity_Id) is
9223 D_Minal : Entity_Id;
9224 CR_Disc : Entity_Id;
9227 -- A discriminal has the same name as the discriminant
9229 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9231 Set_Ekind (D_Minal, E_In_Parameter);
9232 Set_Mechanism (D_Minal, Default_Mechanism);
9233 Set_Etype (D_Minal, Etype (Discrim));
9234 Set_Scope (D_Minal, Current_Scope);
9235 Set_Parent (D_Minal, Parent (Discrim));
9237 Set_Discriminal (Discrim, D_Minal);
9238 Set_Discriminal_Link (D_Minal, Discrim);
9240 -- For task types, build at once the discriminants of the corresponding
9241 -- record, which are needed if discriminants are used in entry defaults
9242 -- and in family bounds.
9244 if Is_Concurrent_Type (Current_Scope)
9246 Is_Limited_Type (Current_Scope)
9248 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9250 Set_Ekind (CR_Disc, E_In_Parameter);
9251 Set_Mechanism (CR_Disc, Default_Mechanism);
9252 Set_Etype (CR_Disc, Etype (Discrim));
9253 Set_Scope (CR_Disc, Current_Scope);
9254 Set_Discriminal_Link (CR_Disc, Discrim);
9255 Set_CR_Discriminant (Discrim, CR_Disc);
9257 end Build_Discriminal;
9259 ------------------------------------
9260 -- Build_Discriminant_Constraints --
9261 ------------------------------------
9263 function Build_Discriminant_Constraints
9266 Derived_Def : Boolean := False) return Elist_Id
9268 C : constant Node_Id := Constraint (Def);
9269 Nb_Discr : constant Nat := Number_Discriminants (T);
9271 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9272 -- Saves the expression corresponding to a given discriminant in T
9274 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9275 -- Return the Position number within array Discr_Expr of a discriminant
9276 -- D within the discriminant list of the discriminated type T.
9278 procedure Process_Discriminant_Expression
9281 -- If this is a discriminant constraint on a partial view, do not
9282 -- generate an overflow check on the discriminant expression. The check
9283 -- will be generated when constraining the full view. Otherwise the
9284 -- backend creates duplicate symbols for the temporaries corresponding
9285 -- to the expressions to be checked, causing spurious assembler errors.
9291 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9295 Disc := First_Discriminant (T);
9296 for J in Discr_Expr'Range loop
9301 Next_Discriminant (Disc);
9304 -- Note: Since this function is called on discriminants that are
9305 -- known to belong to the discriminated type, falling through the
9306 -- loop with no match signals an internal compiler error.
9308 raise Program_Error;
9311 -------------------------------------
9312 -- Process_Discriminant_Expression --
9313 -------------------------------------
9315 procedure Process_Discriminant_Expression
9319 BDT : constant Entity_Id := Base_Type (Etype (D));
9322 -- If this is a discriminant constraint on a partial view, do
9323 -- not generate an overflow on the discriminant expression. The
9324 -- check will be generated when constraining the full view.
9326 if Is_Private_Type (T)
9327 and then Present (Full_View (T))
9329 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9331 Analyze_And_Resolve (Expr, BDT);
9333 end Process_Discriminant_Expression;
9335 -- Declarations local to Build_Discriminant_Constraints
9339 Elist : constant Elist_Id := New_Elmt_List;
9347 Discrim_Present : Boolean := False;
9349 -- Start of processing for Build_Discriminant_Constraints
9352 -- The following loop will process positional associations only.
9353 -- For a positional association, the (single) discriminant is
9354 -- implicitly specified by position, in textual order (RM 3.7.2).
9356 Discr := First_Discriminant (T);
9357 Constr := First (Constraints (C));
9358 for D in Discr_Expr'Range loop
9359 exit when Nkind (Constr) = N_Discriminant_Association;
9362 Error_Msg_N ("too few discriminants given in constraint", C);
9363 return New_Elmt_List;
9365 elsif Nkind (Constr) = N_Range
9366 or else (Nkind (Constr) = N_Attribute_Reference
9367 and then Attribute_Name (Constr) = Name_Range)
9370 ("a range is not a valid discriminant constraint", Constr);
9371 Discr_Expr (D) := Error;
9374 Process_Discriminant_Expression (Constr, Discr);
9375 Discr_Expr (D) := Constr;
9378 Next_Discriminant (Discr);
9382 if No (Discr) and then Present (Constr) then
9383 Error_Msg_N ("too many discriminants given in constraint", Constr);
9384 return New_Elmt_List;
9387 -- Named associations can be given in any order, but if both positional
9388 -- and named associations are used in the same discriminant constraint,
9389 -- then positional associations must occur first, at their normal
9390 -- position. Hence once a named association is used, the rest of the
9391 -- discriminant constraint must use only named associations.
9393 while Present (Constr) loop
9395 -- Positional association forbidden after a named association
9397 if Nkind (Constr) /= N_Discriminant_Association then
9398 Error_Msg_N ("positional association follows named one", Constr);
9399 return New_Elmt_List;
9401 -- Otherwise it is a named association
9404 -- E records the type of the discriminants in the named
9405 -- association. All the discriminants specified in the same name
9406 -- association must have the same type.
9410 -- Search the list of discriminants in T to see if the simple name
9411 -- given in the constraint matches any of them.
9413 Id := First (Selector_Names (Constr));
9414 while Present (Id) loop
9417 -- If Original_Discriminant is present, we are processing a
9418 -- generic instantiation and this is an instance node. We need
9419 -- to find the name of the corresponding discriminant in the
9420 -- actual record type T and not the name of the discriminant in
9421 -- the generic formal. Example:
9424 -- type G (D : int) is private;
9426 -- subtype W is G (D => 1);
9428 -- type Rec (X : int) is record ... end record;
9429 -- package Q is new P (G => Rec);
9431 -- At the point of the instantiation, formal type G is Rec
9432 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9433 -- which really looks like "subtype W is Rec (D => 1);" at
9434 -- the point of instantiation, we want to find the discriminant
9435 -- that corresponds to D in Rec, i.e. X.
9437 if Present (Original_Discriminant (Id))
9438 and then In_Instance
9440 Discr := Find_Corresponding_Discriminant (Id, T);
9444 Discr := First_Discriminant (T);
9445 while Present (Discr) loop
9446 if Chars (Discr) = Chars (Id) then
9451 Next_Discriminant (Discr);
9455 Error_Msg_N ("& does not match any discriminant", Id);
9456 return New_Elmt_List;
9458 -- If the parent type is a generic formal, preserve the
9459 -- name of the discriminant for subsequent instances.
9460 -- see comment at the beginning of this if statement.
9462 elsif Is_Generic_Type (Root_Type (T)) then
9463 Set_Original_Discriminant (Id, Discr);
9467 Position := Pos_Of_Discr (T, Discr);
9469 if Present (Discr_Expr (Position)) then
9470 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9473 -- Each discriminant specified in the same named association
9474 -- must be associated with a separate copy of the
9475 -- corresponding expression.
9477 if Present (Next (Id)) then
9478 Expr := New_Copy_Tree (Expression (Constr));
9479 Set_Parent (Expr, Parent (Expression (Constr)));
9481 Expr := Expression (Constr);
9484 Discr_Expr (Position) := Expr;
9485 Process_Discriminant_Expression (Expr, Discr);
9488 -- A discriminant association with more than one discriminant
9489 -- name is only allowed if the named discriminants are all of
9490 -- the same type (RM 3.7.1(8)).
9493 E := Base_Type (Etype (Discr));
9495 elsif Base_Type (Etype (Discr)) /= E then
9497 ("all discriminants in an association " &
9498 "must have the same type", Id);
9508 -- A discriminant constraint must provide exactly one value for each
9509 -- discriminant of the type (RM 3.7.1(8)).
9511 for J in Discr_Expr'Range loop
9512 if No (Discr_Expr (J)) then
9513 Error_Msg_N ("too few discriminants given in constraint", C);
9514 return New_Elmt_List;
9518 -- Determine if there are discriminant expressions in the constraint
9520 for J in Discr_Expr'Range loop
9521 if Denotes_Discriminant
9522 (Discr_Expr (J), Check_Concurrent => True)
9524 Discrim_Present := True;
9528 -- Build an element list consisting of the expressions given in the
9529 -- discriminant constraint and apply the appropriate checks. The list
9530 -- is constructed after resolving any named discriminant associations
9531 -- and therefore the expressions appear in the textual order of the
9534 Discr := First_Discriminant (T);
9535 for J in Discr_Expr'Range loop
9536 if Discr_Expr (J) /= Error then
9537 Append_Elmt (Discr_Expr (J), Elist);
9539 -- If any of the discriminant constraints is given by a
9540 -- discriminant and we are in a derived type declaration we
9541 -- have a discriminant renaming. Establish link between new
9542 -- and old discriminant.
9544 if Denotes_Discriminant (Discr_Expr (J)) then
9546 Set_Corresponding_Discriminant
9547 (Entity (Discr_Expr (J)), Discr);
9550 -- Force the evaluation of non-discriminant expressions.
9551 -- If we have found a discriminant in the constraint 3.4(26)
9552 -- and 3.8(18) demand that no range checks are performed are
9553 -- after evaluation. If the constraint is for a component
9554 -- definition that has a per-object constraint, expressions are
9555 -- evaluated but not checked either. In all other cases perform
9559 if Discrim_Present then
9562 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9564 Has_Per_Object_Constraint
9565 (Defining_Identifier (Parent (Parent (Def))))
9569 elsif Is_Access_Type (Etype (Discr)) then
9570 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9573 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9576 Force_Evaluation (Discr_Expr (J));
9579 -- Check that the designated type of an access discriminant's
9580 -- expression is not a class-wide type unless the discriminant's
9581 -- designated type is also class-wide.
9583 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9584 and then not Is_Class_Wide_Type
9585 (Designated_Type (Etype (Discr)))
9586 and then Etype (Discr_Expr (J)) /= Any_Type
9587 and then Is_Class_Wide_Type
9588 (Designated_Type (Etype (Discr_Expr (J))))
9590 Wrong_Type (Discr_Expr (J), Etype (Discr));
9592 elsif Is_Access_Type (Etype (Discr))
9593 and then not Is_Access_Constant (Etype (Discr))
9594 and then Is_Access_Type (Etype (Discr_Expr (J)))
9595 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9598 ("constraint for discriminant& must be access to variable",
9603 Next_Discriminant (Discr);
9607 end Build_Discriminant_Constraints;
9609 ---------------------------------
9610 -- Build_Discriminated_Subtype --
9611 ---------------------------------
9613 procedure Build_Discriminated_Subtype
9617 Related_Nod : Node_Id;
9618 For_Access : Boolean := False)
9620 Has_Discrs : constant Boolean := Has_Discriminants (T);
9621 Constrained : constant Boolean :=
9623 and then not Is_Empty_Elmt_List (Elist)
9624 and then not Is_Class_Wide_Type (T))
9625 or else Is_Constrained (T);
9628 if Ekind (T) = E_Record_Type then
9630 Set_Ekind (Def_Id, E_Private_Subtype);
9631 Set_Is_For_Access_Subtype (Def_Id, True);
9633 Set_Ekind (Def_Id, E_Record_Subtype);
9636 -- Inherit preelaboration flag from base, for types for which it
9637 -- may have been set: records, private types, protected types.
9639 Set_Known_To_Have_Preelab_Init
9640 (Def_Id, Known_To_Have_Preelab_Init (T));
9642 elsif Ekind (T) = E_Task_Type then
9643 Set_Ekind (Def_Id, E_Task_Subtype);
9645 elsif Ekind (T) = E_Protected_Type then
9646 Set_Ekind (Def_Id, E_Protected_Subtype);
9647 Set_Known_To_Have_Preelab_Init
9648 (Def_Id, Known_To_Have_Preelab_Init (T));
9650 elsif Is_Private_Type (T) then
9651 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9652 Set_Known_To_Have_Preelab_Init
9653 (Def_Id, Known_To_Have_Preelab_Init (T));
9655 -- Private subtypes may have private dependents
9657 Set_Private_Dependents (Def_Id, New_Elmt_List);
9659 elsif Is_Class_Wide_Type (T) then
9660 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9663 -- Incomplete type. Attach subtype to list of dependents, to be
9664 -- completed with full view of parent type, unless is it the
9665 -- designated subtype of a record component within an init_proc.
9666 -- This last case arises for a component of an access type whose
9667 -- designated type is incomplete (e.g. a Taft Amendment type).
9668 -- The designated subtype is within an inner scope, and needs no
9669 -- elaboration, because only the access type is needed in the
9670 -- initialization procedure.
9672 Set_Ekind (Def_Id, Ekind (T));
9674 if For_Access and then Within_Init_Proc then
9677 Append_Elmt (Def_Id, Private_Dependents (T));
9681 Set_Etype (Def_Id, T);
9682 Init_Size_Align (Def_Id);
9683 Set_Has_Discriminants (Def_Id, Has_Discrs);
9684 Set_Is_Constrained (Def_Id, Constrained);
9686 Set_First_Entity (Def_Id, First_Entity (T));
9687 Set_Last_Entity (Def_Id, Last_Entity (T));
9688 Set_Has_Implicit_Dereference
9689 (Def_Id, Has_Implicit_Dereference (T));
9691 -- If the subtype is the completion of a private declaration, there may
9692 -- have been representation clauses for the partial view, and they must
9693 -- be preserved. Build_Derived_Type chains the inherited clauses with
9694 -- the ones appearing on the extension. If this comes from a subtype
9695 -- declaration, all clauses are inherited.
9697 if No (First_Rep_Item (Def_Id)) then
9698 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9701 if Is_Tagged_Type (T) then
9702 Set_Is_Tagged_Type (Def_Id);
9703 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9704 Make_Class_Wide_Type (Def_Id);
9707 Set_Stored_Constraint (Def_Id, No_Elist);
9710 Set_Discriminant_Constraint (Def_Id, Elist);
9711 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9714 if Is_Tagged_Type (T) then
9716 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9717 -- concurrent record type (which has the list of primitive
9720 if Ada_Version >= Ada_2005
9721 and then Is_Concurrent_Type (T)
9723 Set_Corresponding_Record_Type (Def_Id,
9724 Corresponding_Record_Type (T));
9726 Set_Direct_Primitive_Operations (Def_Id,
9727 Direct_Primitive_Operations (T));
9730 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9733 -- Subtypes introduced by component declarations do not need to be
9734 -- marked as delayed, and do not get freeze nodes, because the semantics
9735 -- verifies that the parents of the subtypes are frozen before the
9736 -- enclosing record is frozen.
9738 if not Is_Type (Scope (Def_Id)) then
9739 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9741 if Is_Private_Type (T)
9742 and then Present (Full_View (T))
9744 Conditional_Delay (Def_Id, Full_View (T));
9746 Conditional_Delay (Def_Id, T);
9750 if Is_Record_Type (T) then
9751 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9754 and then not Is_Empty_Elmt_List (Elist)
9755 and then not For_Access
9757 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9758 elsif not For_Access then
9759 Set_Cloned_Subtype (Def_Id, T);
9762 end Build_Discriminated_Subtype;
9764 ---------------------------
9765 -- Build_Itype_Reference --
9766 ---------------------------
9768 procedure Build_Itype_Reference
9772 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9775 -- Itype references are only created for use by the back-end
9777 if Inside_A_Generic then
9780 Set_Itype (IR, Ityp);
9781 Insert_After (Nod, IR);
9783 end Build_Itype_Reference;
9785 ------------------------
9786 -- Build_Scalar_Bound --
9787 ------------------------
9789 function Build_Scalar_Bound
9792 Der_T : Entity_Id) return Node_Id
9794 New_Bound : Entity_Id;
9797 -- Note: not clear why this is needed, how can the original bound
9798 -- be unanalyzed at this point? and if it is, what business do we
9799 -- have messing around with it? and why is the base type of the
9800 -- parent type the right type for the resolution. It probably is
9801 -- not. It is OK for the new bound we are creating, but not for
9802 -- the old one??? Still if it never happens, no problem.
9804 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9806 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9807 New_Bound := New_Copy (Bound);
9808 Set_Etype (New_Bound, Der_T);
9809 Set_Analyzed (New_Bound);
9811 elsif Is_Entity_Name (Bound) then
9812 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9814 -- The following is almost certainly wrong. What business do we have
9815 -- relocating a node (Bound) that is presumably still attached to
9816 -- the tree elsewhere???
9819 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9822 Set_Etype (New_Bound, Der_T);
9824 end Build_Scalar_Bound;
9826 --------------------------------
9827 -- Build_Underlying_Full_View --
9828 --------------------------------
9830 procedure Build_Underlying_Full_View
9835 Loc : constant Source_Ptr := Sloc (N);
9836 Subt : constant Entity_Id :=
9837 Make_Defining_Identifier
9838 (Loc, New_External_Name (Chars (Typ), 'S'));
9845 procedure Set_Discriminant_Name (Id : Node_Id);
9846 -- If the derived type has discriminants, they may rename discriminants
9847 -- of the parent. When building the full view of the parent, we need to
9848 -- recover the names of the original discriminants if the constraint is
9849 -- given by named associations.
9851 ---------------------------
9852 -- Set_Discriminant_Name --
9853 ---------------------------
9855 procedure Set_Discriminant_Name (Id : Node_Id) is
9859 Set_Original_Discriminant (Id, Empty);
9861 if Has_Discriminants (Typ) then
9862 Disc := First_Discriminant (Typ);
9863 while Present (Disc) loop
9864 if Chars (Disc) = Chars (Id)
9865 and then Present (Corresponding_Discriminant (Disc))
9867 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9869 Next_Discriminant (Disc);
9872 end Set_Discriminant_Name;
9874 -- Start of processing for Build_Underlying_Full_View
9877 if Nkind (N) = N_Full_Type_Declaration then
9878 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9880 elsif Nkind (N) = N_Subtype_Declaration then
9881 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
9883 elsif Nkind (N) = N_Component_Declaration then
9886 (Constraint (Subtype_Indication (Component_Definition (N))));
9889 raise Program_Error;
9892 C := First (Constraints (Constr));
9893 while Present (C) loop
9894 if Nkind (C) = N_Discriminant_Association then
9895 Id := First (Selector_Names (C));
9896 while Present (Id) loop
9897 Set_Discriminant_Name (Id);
9906 Make_Subtype_Declaration (Loc,
9907 Defining_Identifier => Subt,
9908 Subtype_Indication =>
9909 Make_Subtype_Indication (Loc,
9910 Subtype_Mark => New_Occurrence_Of (Par, Loc),
9911 Constraint => New_Copy_Tree (Constr)));
9913 -- If this is a component subtype for an outer itype, it is not
9914 -- a list member, so simply set the parent link for analysis: if
9915 -- the enclosing type does not need to be in a declarative list,
9916 -- neither do the components.
9918 if Is_List_Member (N)
9919 and then Nkind (N) /= N_Component_Declaration
9921 Insert_Before (N, Indic);
9923 Set_Parent (Indic, Parent (N));
9927 Set_Underlying_Full_View (Typ, Full_View (Subt));
9928 end Build_Underlying_Full_View;
9930 -------------------------------
9931 -- Check_Abstract_Overriding --
9932 -------------------------------
9934 procedure Check_Abstract_Overriding (T : Entity_Id) is
9935 Alias_Subp : Entity_Id;
9941 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9942 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9943 -- which has pragma Implemented already set. Check whether Subp's entity
9944 -- kind conforms to the implementation kind of the overridden routine.
9946 procedure Check_Pragma_Implemented
9948 Iface_Subp : Entity_Id);
9949 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9950 -- Iface_Subp and both entities have pragma Implemented already set on
9951 -- them. Check whether the two implementation kinds are conforming.
9953 procedure Inherit_Pragma_Implemented
9955 Iface_Subp : Entity_Id);
9956 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9957 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9958 -- Propagate the implementation kind of Iface_Subp to Subp.
9960 ------------------------------
9961 -- Check_Pragma_Implemented --
9962 ------------------------------
9964 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9965 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9966 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9967 Subp_Alias : constant Entity_Id := Alias (Subp);
9968 Contr_Typ : Entity_Id;
9969 Impl_Subp : Entity_Id;
9972 -- Subp must have an alias since it is a hidden entity used to link
9973 -- an interface subprogram to its overriding counterpart.
9975 pragma Assert (Present (Subp_Alias));
9977 -- Handle aliases to synchronized wrappers
9979 Impl_Subp := Subp_Alias;
9981 if Is_Primitive_Wrapper (Impl_Subp) then
9982 Impl_Subp := Wrapped_Entity (Impl_Subp);
9985 -- Extract the type of the controlling formal
9987 Contr_Typ := Etype (First_Formal (Subp_Alias));
9989 if Is_Concurrent_Record_Type (Contr_Typ) then
9990 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9993 -- An interface subprogram whose implementation kind is By_Entry must
9994 -- be implemented by an entry.
9996 if Impl_Kind = Name_By_Entry
9997 and then Ekind (Impl_Subp) /= E_Entry
9999 Error_Msg_Node_2 := Iface_Alias;
10001 ("type & must implement abstract subprogram & with an entry",
10002 Subp_Alias, Contr_Typ);
10004 elsif Impl_Kind = Name_By_Protected_Procedure then
10006 -- An interface subprogram whose implementation kind is By_
10007 -- Protected_Procedure cannot be implemented by a primitive
10008 -- procedure of a task type.
10010 if Ekind (Contr_Typ) /= E_Protected_Type then
10011 Error_Msg_Node_2 := Contr_Typ;
10013 ("interface subprogram & cannot be implemented by a " &
10014 "primitive procedure of task type &", Subp_Alias,
10017 -- An interface subprogram whose implementation kind is By_
10018 -- Protected_Procedure must be implemented by a procedure.
10020 elsif Ekind (Impl_Subp) /= E_Procedure then
10021 Error_Msg_Node_2 := Iface_Alias;
10023 ("type & must implement abstract subprogram & with a " &
10024 "procedure", Subp_Alias, Contr_Typ);
10026 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10027 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10029 Error_Msg_Name_1 := Impl_Kind;
10031 ("overriding operation& must have synchronization%",
10035 -- If primitive has Optional synchronization, overriding operation
10036 -- must match if it has an explicit synchronization..
10038 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10039 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10041 Error_Msg_Name_1 := Impl_Kind;
10043 ("overriding operation& must have syncrhonization%",
10046 end Check_Pragma_Implemented;
10048 ------------------------------
10049 -- Check_Pragma_Implemented --
10050 ------------------------------
10052 procedure Check_Pragma_Implemented
10054 Iface_Subp : Entity_Id)
10056 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10057 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10060 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10061 -- and overriding subprogram are different. In general this is an
10062 -- error except when the implementation kind of the overridden
10063 -- subprograms is By_Any or Optional.
10065 if Iface_Kind /= Subp_Kind
10066 and then Iface_Kind /= Name_By_Any
10067 and then Iface_Kind /= Name_Optional
10069 if Iface_Kind = Name_By_Entry then
10071 ("incompatible implementation kind, overridden subprogram " &
10072 "is marked By_Entry", Subp);
10075 ("incompatible implementation kind, overridden subprogram " &
10076 "is marked By_Protected_Procedure", Subp);
10079 end Check_Pragma_Implemented;
10081 --------------------------------
10082 -- Inherit_Pragma_Implemented --
10083 --------------------------------
10085 procedure Inherit_Pragma_Implemented
10087 Iface_Subp : Entity_Id)
10089 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10090 Loc : constant Source_Ptr := Sloc (Subp);
10091 Impl_Prag : Node_Id;
10094 -- Since the implementation kind is stored as a representation item
10095 -- rather than a flag, create a pragma node.
10099 Chars => Name_Implemented,
10100 Pragma_Argument_Associations => New_List (
10101 Make_Pragma_Argument_Association (Loc,
10102 Expression => New_Occurrence_Of (Subp, Loc)),
10104 Make_Pragma_Argument_Association (Loc,
10105 Expression => Make_Identifier (Loc, Iface_Kind))));
10107 -- The pragma doesn't need to be analyzed because it is internally
10108 -- built. It is safe to directly register it as a rep item since we
10109 -- are only interested in the characters of the implementation kind.
10111 Record_Rep_Item (Subp, Impl_Prag);
10112 end Inherit_Pragma_Implemented;
10114 -- Start of processing for Check_Abstract_Overriding
10117 Op_List := Primitive_Operations (T);
10119 -- Loop to check primitive operations
10121 Elmt := First_Elmt (Op_List);
10122 while Present (Elmt) loop
10123 Subp := Node (Elmt);
10124 Alias_Subp := Alias (Subp);
10126 -- Inherited subprograms are identified by the fact that they do not
10127 -- come from source, and the associated source location is the
10128 -- location of the first subtype of the derived type.
10130 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10131 -- subprograms that "require overriding".
10133 -- Special exception, do not complain about failure to override the
10134 -- stream routines _Input and _Output, as well as the primitive
10135 -- operations used in dispatching selects since we always provide
10136 -- automatic overridings for these subprograms.
10138 -- The partial view of T may have been a private extension, for
10139 -- which inherited functions dispatching on result are abstract.
10140 -- If the full view is a null extension, there is no need for
10141 -- overriding in Ada 2005, but wrappers need to be built for them
10142 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10144 if Is_Null_Extension (T)
10145 and then Has_Controlling_Result (Subp)
10146 and then Ada_Version >= Ada_2005
10147 and then Present (Alias_Subp)
10148 and then not Comes_From_Source (Subp)
10149 and then not Is_Abstract_Subprogram (Alias_Subp)
10150 and then not Is_Access_Type (Etype (Subp))
10154 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10155 -- processing because this check is done with the aliased
10158 elsif Present (Interface_Alias (Subp)) then
10161 elsif (Is_Abstract_Subprogram (Subp)
10162 or else Requires_Overriding (Subp)
10164 (Has_Controlling_Result (Subp)
10165 and then Present (Alias_Subp)
10166 and then not Comes_From_Source (Subp)
10167 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10168 and then not Is_TSS (Subp, TSS_Stream_Input)
10169 and then not Is_TSS (Subp, TSS_Stream_Output)
10170 and then not Is_Abstract_Type (T)
10171 and then not Is_Predefined_Interface_Primitive (Subp)
10173 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10174 -- with abstract interface types because the check will be done
10175 -- with the aliased entity (otherwise we generate a duplicated
10178 and then not Present (Interface_Alias (Subp))
10180 if Present (Alias_Subp) then
10182 -- Only perform the check for a derived subprogram when the
10183 -- type has an explicit record extension. This avoids incorrect
10184 -- flagging of abstract subprograms for the case of a type
10185 -- without an extension that is derived from a formal type
10186 -- with a tagged actual (can occur within a private part).
10188 -- Ada 2005 (AI-391): In the case of an inherited function with
10189 -- a controlling result of the type, the rule does not apply if
10190 -- the type is a null extension (unless the parent function
10191 -- itself is abstract, in which case the function must still be
10192 -- be overridden). The expander will generate an overriding
10193 -- wrapper function calling the parent subprogram (see
10194 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10196 Type_Def := Type_Definition (Parent (T));
10198 if Nkind (Type_Def) = N_Derived_Type_Definition
10199 and then Present (Record_Extension_Part (Type_Def))
10201 (Ada_Version < Ada_2005
10202 or else not Is_Null_Extension (T)
10203 or else Ekind (Subp) = E_Procedure
10204 or else not Has_Controlling_Result (Subp)
10205 or else Is_Abstract_Subprogram (Alias_Subp)
10206 or else Requires_Overriding (Subp)
10207 or else Is_Access_Type (Etype (Subp)))
10209 -- Avoid reporting error in case of abstract predefined
10210 -- primitive inherited from interface type because the
10211 -- body of internally generated predefined primitives
10212 -- of tagged types are generated later by Freeze_Type
10214 if Is_Interface (Root_Type (T))
10215 and then Is_Abstract_Subprogram (Subp)
10216 and then Is_Predefined_Dispatching_Operation (Subp)
10217 and then not Comes_From_Source (Ultimate_Alias (Subp))
10221 -- A null extension is not obliged to override an inherited
10222 -- procedure subject to pragma Extensions_Visible with value
10223 -- False and at least one controlling OUT parameter
10224 -- (SPARK RM 6.1.7(6)).
10226 elsif Is_Null_Extension (T)
10227 and then Is_EVF_Procedure (Subp)
10233 ("type must be declared abstract or & overridden",
10236 -- Traverse the whole chain of aliased subprograms to
10237 -- complete the error notification. This is especially
10238 -- useful for traceability of the chain of entities when
10239 -- the subprogram corresponds with an interface
10240 -- subprogram (which may be defined in another package).
10242 if Present (Alias_Subp) then
10248 while Present (Alias (E)) loop
10250 -- Avoid reporting redundant errors on entities
10251 -- inherited from interfaces
10253 if Sloc (E) /= Sloc (T) then
10254 Error_Msg_Sloc := Sloc (E);
10256 ("\& has been inherited #", T, Subp);
10262 Error_Msg_Sloc := Sloc (E);
10264 -- AI05-0068: report if there is an overriding
10265 -- non-abstract subprogram that is invisible.
10268 and then not Is_Abstract_Subprogram (E)
10271 ("\& subprogram# is not visible",
10274 -- Clarify the case where a non-null extension must
10275 -- override inherited procedure subject to pragma
10276 -- Extensions_Visible with value False and at least
10277 -- one controlling OUT param.
10279 elsif Is_EVF_Procedure (E) then
10281 ("\& # is subject to Extensions_Visible False",
10286 ("\& has been inherited from subprogram #",
10293 -- Ada 2005 (AI-345): Protected or task type implementing
10294 -- abstract interfaces.
10296 elsif Is_Concurrent_Record_Type (T)
10297 and then Present (Interfaces (T))
10299 -- There is no need to check here RM 9.4(11.9/3) since we
10300 -- are processing the corresponding record type and the
10301 -- mode of the overriding subprograms was verified by
10302 -- Check_Conformance when the corresponding concurrent
10303 -- type declaration was analyzed.
10306 ("interface subprogram & must be overridden", T, Subp);
10308 -- Examine primitive operations of synchronized type to find
10309 -- homonyms that have the wrong profile.
10315 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10316 while Present (Prim) loop
10317 if Chars (Prim) = Chars (Subp) then
10319 ("profile is not type conformant with prefixed "
10320 & "view profile of inherited operation&",
10324 Next_Entity (Prim);
10330 Error_Msg_Node_2 := T;
10332 ("abstract subprogram& not allowed for type&", Subp);
10334 -- Also post unconditional warning on the type (unconditional
10335 -- so that if there are more than one of these cases, we get
10336 -- them all, and not just the first one).
10338 Error_Msg_Node_2 := Subp;
10339 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10342 -- A subprogram subject to pragma Extensions_Visible with value
10343 -- "True" cannot override a subprogram subject to the same pragma
10344 -- with value "False" (SPARK RM 6.1.7(5)).
10346 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10347 and then Present (Overridden_Operation (Subp))
10348 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10349 Extensions_Visible_False
10351 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10353 ("subprogram & with Extensions_Visible True cannot override "
10354 & "subprogram # with Extensions_Visible False", Subp);
10357 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10359 -- Subp is an expander-generated procedure which maps an interface
10360 -- alias to a protected wrapper. The interface alias is flagged by
10361 -- pragma Implemented. Ensure that Subp is a procedure when the
10362 -- implementation kind is By_Protected_Procedure or an entry when
10365 if Ada_Version >= Ada_2012
10366 and then Is_Hidden (Subp)
10367 and then Present (Interface_Alias (Subp))
10368 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10370 Check_Pragma_Implemented (Subp);
10373 -- Subp is an interface primitive which overrides another interface
10374 -- primitive marked with pragma Implemented.
10376 if Ada_Version >= Ada_2012
10377 and then Present (Overridden_Operation (Subp))
10378 and then Has_Rep_Pragma
10379 (Overridden_Operation (Subp), Name_Implemented)
10381 -- If the overriding routine is also marked by Implemented, check
10382 -- that the two implementation kinds are conforming.
10384 if Has_Rep_Pragma (Subp, Name_Implemented) then
10385 Check_Pragma_Implemented
10387 Iface_Subp => Overridden_Operation (Subp));
10389 -- Otherwise the overriding routine inherits the implementation
10390 -- kind from the overridden subprogram.
10393 Inherit_Pragma_Implemented
10395 Iface_Subp => Overridden_Operation (Subp));
10399 -- If the operation is a wrapper for a synchronized primitive, it
10400 -- may be called indirectly through a dispatching select. We assume
10401 -- that it will be referenced elsewhere indirectly, and suppress
10402 -- warnings about an unused entity.
10404 if Is_Primitive_Wrapper (Subp)
10405 and then Present (Wrapped_Entity (Subp))
10407 Set_Referenced (Wrapped_Entity (Subp));
10412 end Check_Abstract_Overriding;
10414 ------------------------------------------------
10415 -- Check_Access_Discriminant_Requires_Limited --
10416 ------------------------------------------------
10418 procedure Check_Access_Discriminant_Requires_Limited
10423 -- A discriminant_specification for an access discriminant shall appear
10424 -- only in the declaration for a task or protected type, or for a type
10425 -- with the reserved word 'limited' in its definition or in one of its
10426 -- ancestors (RM 3.7(10)).
10428 -- AI-0063: The proper condition is that type must be immutably limited,
10429 -- or else be a partial view.
10431 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10432 if Is_Limited_View (Current_Scope)
10434 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10435 and then Limited_Present (Parent (Current_Scope)))
10441 ("access discriminants allowed only for limited types", Loc);
10444 end Check_Access_Discriminant_Requires_Limited;
10446 -----------------------------------
10447 -- Check_Aliased_Component_Types --
10448 -----------------------------------
10450 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10454 -- ??? Also need to check components of record extensions, but not
10455 -- components of protected types (which are always limited).
10457 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10458 -- types to be unconstrained. This is safe because it is illegal to
10459 -- create access subtypes to such types with explicit discriminant
10462 if not Is_Limited_Type (T) then
10463 if Ekind (T) = E_Record_Type then
10464 C := First_Component (T);
10465 while Present (C) loop
10467 and then Has_Discriminants (Etype (C))
10468 and then not Is_Constrained (Etype (C))
10469 and then not In_Instance_Body
10470 and then Ada_Version < Ada_2005
10473 ("aliased component must be constrained (RM 3.6(11))",
10477 Next_Component (C);
10480 elsif Ekind (T) = E_Array_Type then
10481 if Has_Aliased_Components (T)
10482 and then Has_Discriminants (Component_Type (T))
10483 and then not Is_Constrained (Component_Type (T))
10484 and then not In_Instance_Body
10485 and then Ada_Version < Ada_2005
10488 ("aliased component type must be constrained (RM 3.6(11))",
10493 end Check_Aliased_Component_Types;
10495 ---------------------------------------
10496 -- Check_Anonymous_Access_Components --
10497 ---------------------------------------
10499 procedure Check_Anonymous_Access_Components
10500 (Typ_Decl : Node_Id;
10503 Comp_List : Node_Id)
10505 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10506 Anon_Access : Entity_Id;
10509 Comp_Def : Node_Id;
10511 Type_Def : Node_Id;
10513 procedure Build_Incomplete_Type_Declaration;
10514 -- If the record type contains components that include an access to the
10515 -- current record, then create an incomplete type declaration for the
10516 -- record, to be used as the designated type of the anonymous access.
10517 -- This is done only once, and only if there is no previous partial
10518 -- view of the type.
10520 function Designates_T (Subt : Node_Id) return Boolean;
10521 -- Check whether a node designates the enclosing record type, or 'Class
10524 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10525 -- Check whether an access definition includes a reference to
10526 -- the enclosing record type. The reference can be a subtype mark
10527 -- in the access definition itself, a 'Class attribute reference, or
10528 -- recursively a reference appearing in a parameter specification
10529 -- or result definition of an access_to_subprogram definition.
10531 --------------------------------------
10532 -- Build_Incomplete_Type_Declaration --
10533 --------------------------------------
10535 procedure Build_Incomplete_Type_Declaration is
10540 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10541 -- it's "is new ... with record" or else "is tagged record ...".
10543 Is_Tagged : constant Boolean :=
10544 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10546 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10548 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10549 and then Tagged_Present (Type_Definition (Typ_Decl)));
10552 -- If there is a previous partial view, no need to create a new one
10553 -- If the partial view, given by Prev, is incomplete, If Prev is
10554 -- a private declaration, full declaration is flagged accordingly.
10556 if Prev /= Typ then
10558 Make_Class_Wide_Type (Prev);
10559 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10560 Set_Etype (Class_Wide_Type (Typ), Typ);
10565 elsif Has_Private_Declaration (Typ) then
10567 -- If we refer to T'Class inside T, and T is the completion of a
10568 -- private type, then make sure the class-wide type exists.
10571 Make_Class_Wide_Type (Typ);
10576 -- If there was a previous anonymous access type, the incomplete
10577 -- type declaration will have been created already.
10579 elsif Present (Current_Entity (Typ))
10580 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10581 and then Full_View (Current_Entity (Typ)) = Typ
10584 and then Comes_From_Source (Current_Entity (Typ))
10585 and then not Is_Tagged_Type (Current_Entity (Typ))
10587 Make_Class_Wide_Type (Typ);
10589 ("incomplete view of tagged type should be declared tagged??",
10590 Parent (Current_Entity (Typ)));
10595 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10596 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10598 -- Type has already been inserted into the current scope. Remove
10599 -- it, and add incomplete declaration for type, so that subsequent
10600 -- anonymous access types can use it. The entity is unchained from
10601 -- the homonym list and from immediate visibility. After analysis,
10602 -- the entity in the incomplete declaration becomes immediately
10603 -- visible in the record declaration that follows.
10605 H := Current_Entity (Typ);
10608 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10611 and then Homonym (H) /= Typ
10613 H := Homonym (Typ);
10616 Set_Homonym (H, Homonym (Typ));
10619 Insert_Before (Typ_Decl, Decl);
10621 Set_Full_View (Inc_T, Typ);
10625 -- Create a common class-wide type for both views, and set the
10626 -- Etype of the class-wide type to the full view.
10628 Make_Class_Wide_Type (Inc_T);
10629 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10630 Set_Etype (Class_Wide_Type (Typ), Typ);
10633 end Build_Incomplete_Type_Declaration;
10639 function Designates_T (Subt : Node_Id) return Boolean is
10640 Type_Id : constant Name_Id := Chars (Typ);
10642 function Names_T (Nam : Node_Id) return Boolean;
10643 -- The record type has not been introduced in the current scope
10644 -- yet, so we must examine the name of the type itself, either
10645 -- an identifier T, or an expanded name of the form P.T, where
10646 -- P denotes the current scope.
10652 function Names_T (Nam : Node_Id) return Boolean is
10654 if Nkind (Nam) = N_Identifier then
10655 return Chars (Nam) = Type_Id;
10657 elsif Nkind (Nam) = N_Selected_Component then
10658 if Chars (Selector_Name (Nam)) = Type_Id then
10659 if Nkind (Prefix (Nam)) = N_Identifier then
10660 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10662 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10663 return Chars (Selector_Name (Prefix (Nam))) =
10664 Chars (Current_Scope);
10678 -- Start of processing for Designates_T
10681 if Nkind (Subt) = N_Identifier then
10682 return Chars (Subt) = Type_Id;
10684 -- Reference can be through an expanded name which has not been
10685 -- analyzed yet, and which designates enclosing scopes.
10687 elsif Nkind (Subt) = N_Selected_Component then
10688 if Names_T (Subt) then
10691 -- Otherwise it must denote an entity that is already visible.
10692 -- The access definition may name a subtype of the enclosing
10693 -- type, if there is a previous incomplete declaration for it.
10696 Find_Selected_Component (Subt);
10698 Is_Entity_Name (Subt)
10699 and then Scope (Entity (Subt)) = Current_Scope
10701 (Chars (Base_Type (Entity (Subt))) = Type_Id
10703 (Is_Class_Wide_Type (Entity (Subt))
10705 Chars (Etype (Base_Type (Entity (Subt)))) =
10709 -- A reference to the current type may appear as the prefix of
10710 -- a 'Class attribute.
10712 elsif Nkind (Subt) = N_Attribute_Reference
10713 and then Attribute_Name (Subt) = Name_Class
10715 return Names_T (Prefix (Subt));
10726 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10727 Param_Spec : Node_Id;
10729 Acc_Subprg : constant Node_Id :=
10730 Access_To_Subprogram_Definition (Acc_Def);
10733 if No (Acc_Subprg) then
10734 return Designates_T (Subtype_Mark (Acc_Def));
10737 -- Component is an access_to_subprogram: examine its formals,
10738 -- and result definition in the case of an access_to_function.
10740 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10741 while Present (Param_Spec) loop
10742 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10743 and then Mentions_T (Parameter_Type (Param_Spec))
10747 elsif Designates_T (Parameter_Type (Param_Spec)) then
10754 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10755 if Nkind (Result_Definition (Acc_Subprg)) =
10756 N_Access_Definition
10758 return Mentions_T (Result_Definition (Acc_Subprg));
10760 return Designates_T (Result_Definition (Acc_Subprg));
10767 -- Start of processing for Check_Anonymous_Access_Components
10770 if No (Comp_List) then
10774 Comp := First (Component_Items (Comp_List));
10775 while Present (Comp) loop
10776 if Nkind (Comp) = N_Component_Declaration
10778 (Access_Definition (Component_Definition (Comp)))
10780 Mentions_T (Access_Definition (Component_Definition (Comp)))
10782 Comp_Def := Component_Definition (Comp);
10784 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10786 Build_Incomplete_Type_Declaration;
10787 Anon_Access := Make_Temporary (Loc, 'S');
10789 -- Create a declaration for the anonymous access type: either
10790 -- an access_to_object or an access_to_subprogram.
10792 if Present (Acc_Def) then
10793 if Nkind (Acc_Def) = N_Access_Function_Definition then
10795 Make_Access_Function_Definition (Loc,
10796 Parameter_Specifications =>
10797 Parameter_Specifications (Acc_Def),
10798 Result_Definition => Result_Definition (Acc_Def));
10801 Make_Access_Procedure_Definition (Loc,
10802 Parameter_Specifications =>
10803 Parameter_Specifications (Acc_Def));
10808 Make_Access_To_Object_Definition (Loc,
10809 Subtype_Indication =>
10811 (Subtype_Mark (Access_Definition (Comp_Def))));
10813 Set_Constant_Present
10814 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10816 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10819 Set_Null_Exclusion_Present
10821 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10824 Make_Full_Type_Declaration (Loc,
10825 Defining_Identifier => Anon_Access,
10826 Type_Definition => Type_Def);
10828 Insert_Before (Typ_Decl, Decl);
10831 -- If an access to subprogram, create the extra formals
10833 if Present (Acc_Def) then
10834 Create_Extra_Formals (Designated_Type (Anon_Access));
10836 -- If an access to object, preserve entity of designated type,
10837 -- for ASIS use, before rewriting the component definition.
10844 Desig := Entity (Subtype_Indication (Type_Def));
10846 -- If the access definition is to the current record,
10847 -- the visible entity at this point is an incomplete
10848 -- type. Retrieve the full view to simplify ASIS queries
10850 if Ekind (Desig) = E_Incomplete_Type then
10851 Desig := Full_View (Desig);
10855 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
10860 Make_Component_Definition (Loc,
10861 Subtype_Indication =>
10862 New_Occurrence_Of (Anon_Access, Loc)));
10864 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
10865 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
10867 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
10870 Set_Is_Local_Anonymous_Access (Anon_Access);
10876 if Present (Variant_Part (Comp_List)) then
10880 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
10881 while Present (V) loop
10882 Check_Anonymous_Access_Components
10883 (Typ_Decl, Typ, Prev, Component_List (V));
10884 Next_Non_Pragma (V);
10888 end Check_Anonymous_Access_Components;
10890 ----------------------
10891 -- Check_Completion --
10892 ----------------------
10894 procedure Check_Completion (Body_Id : Node_Id := Empty) is
10897 procedure Post_Error;
10898 -- Post error message for lack of completion for entity E
10904 procedure Post_Error is
10905 procedure Missing_Body;
10906 -- Output missing body message
10912 procedure Missing_Body is
10914 -- Spec is in same unit, so we can post on spec
10916 if In_Same_Source_Unit (Body_Id, E) then
10917 Error_Msg_N ("missing body for &", E);
10919 -- Spec is in a separate unit, so we have to post on the body
10922 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
10926 -- Start of processing for Post_Error
10929 if not Comes_From_Source (E) then
10930 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
10932 -- It may be an anonymous protected type created for a
10933 -- single variable. Post error on variable, if present.
10939 Var := First_Entity (Current_Scope);
10940 while Present (Var) loop
10941 exit when Etype (Var) = E
10942 and then Comes_From_Source (Var);
10947 if Present (Var) then
10954 -- If a generated entity has no completion, then either previous
10955 -- semantic errors have disabled the expansion phase, or else we had
10956 -- missing subunits, or else we are compiling without expansion,
10957 -- or else something is very wrong.
10959 if not Comes_From_Source (E) then
10961 (Serious_Errors_Detected > 0
10962 or else Configurable_Run_Time_Violations > 0
10963 or else Subunits_Missing
10964 or else not Expander_Active);
10967 -- Here for source entity
10970 -- Here if no body to post the error message, so we post the error
10971 -- on the declaration that has no completion. This is not really
10972 -- the right place to post it, think about this later ???
10974 if No (Body_Id) then
10975 if Is_Type (E) then
10977 ("missing full declaration for }", Parent (E), E);
10979 Error_Msg_NE ("missing body for &", Parent (E), E);
10982 -- Package body has no completion for a declaration that appears
10983 -- in the corresponding spec. Post error on the body, with a
10984 -- reference to the non-completed declaration.
10987 Error_Msg_Sloc := Sloc (E);
10989 if Is_Type (E) then
10990 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
10992 elsif Is_Overloadable (E)
10993 and then Current_Entity_In_Scope (E) /= E
10995 -- It may be that the completion is mistyped and appears as
10996 -- a distinct overloading of the entity.
10999 Candidate : constant Entity_Id :=
11000 Current_Entity_In_Scope (E);
11001 Decl : constant Node_Id :=
11002 Unit_Declaration_Node (Candidate);
11005 if Is_Overloadable (Candidate)
11006 and then Ekind (Candidate) = Ekind (E)
11007 and then Nkind (Decl) = N_Subprogram_Body
11008 and then Acts_As_Spec (Decl)
11010 Check_Type_Conformant (Candidate, E);
11026 Pack_Id : constant Entity_Id := Current_Scope;
11028 -- Start of processing for Check_Completion
11031 E := First_Entity (Pack_Id);
11032 while Present (E) loop
11033 if Is_Intrinsic_Subprogram (E) then
11036 -- The following situation requires special handling: a child unit
11037 -- that appears in the context clause of the body of its parent:
11039 -- procedure Parent.Child (...);
11041 -- with Parent.Child;
11042 -- package body Parent is
11044 -- Here Parent.Child appears as a local entity, but should not be
11045 -- flagged as requiring completion, because it is a compilation
11048 -- Ignore missing completion for a subprogram that does not come from
11049 -- source (including the _Call primitive operation of RAS types,
11050 -- which has to have the flag Comes_From_Source for other purposes):
11051 -- we assume that the expander will provide the missing completion.
11052 -- In case of previous errors, other expansion actions that provide
11053 -- bodies for null procedures with not be invoked, so inhibit message
11056 -- Note that E_Operator is not in the list that follows, because
11057 -- this kind is reserved for predefined operators, that are
11058 -- intrinsic and do not need completion.
11060 elsif Ekind_In (E, E_Function,
11062 E_Generic_Function,
11063 E_Generic_Procedure)
11065 if Has_Completion (E) then
11068 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11071 elsif Is_Subprogram (E)
11072 and then (not Comes_From_Source (E)
11073 or else Chars (E) = Name_uCall)
11078 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11082 elsif Nkind (Parent (E)) = N_Procedure_Specification
11083 and then Null_Present (Parent (E))
11084 and then Serious_Errors_Detected > 0
11092 elsif Is_Entry (E) then
11093 if not Has_Completion (E) and then
11094 (Ekind (Scope (E)) = E_Protected_Object
11095 or else Ekind (Scope (E)) = E_Protected_Type)
11100 elsif Is_Package_Or_Generic_Package (E) then
11101 if Unit_Requires_Body (E) then
11102 if not Has_Completion (E)
11103 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11109 elsif not Is_Child_Unit (E) then
11110 May_Need_Implicit_Body (E);
11113 -- A formal incomplete type (Ada 2012) does not require a completion;
11114 -- other incomplete type declarations do.
11116 elsif Ekind (E) = E_Incomplete_Type
11117 and then No (Underlying_Type (E))
11118 and then not Is_Generic_Type (E)
11122 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11123 and then not Has_Completion (E)
11127 -- A single task declared in the current scope is a constant, verify
11128 -- that the body of its anonymous type is in the same scope. If the
11129 -- task is defined elsewhere, this may be a renaming declaration for
11130 -- which no completion is needed.
11132 elsif Ekind (E) = E_Constant
11133 and then Ekind (Etype (E)) = E_Task_Type
11134 and then not Has_Completion (Etype (E))
11135 and then Scope (Etype (E)) = Current_Scope
11139 elsif Ekind (E) = E_Protected_Object
11140 and then not Has_Completion (Etype (E))
11144 elsif Ekind (E) = E_Record_Type then
11145 if Is_Tagged_Type (E) then
11146 Check_Abstract_Overriding (E);
11147 Check_Conventions (E);
11150 Check_Aliased_Component_Types (E);
11152 elsif Ekind (E) = E_Array_Type then
11153 Check_Aliased_Component_Types (E);
11159 end Check_Completion;
11161 ------------------------------------
11162 -- Check_CPP_Type_Has_No_Defaults --
11163 ------------------------------------
11165 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11166 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11171 -- Obtain the component list
11173 if Nkind (Tdef) = N_Record_Definition then
11174 Clist := Component_List (Tdef);
11175 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11176 Clist := Component_List (Record_Extension_Part (Tdef));
11179 -- Check all components to ensure no default expressions
11181 if Present (Clist) then
11182 Comp := First (Component_Items (Clist));
11183 while Present (Comp) loop
11184 if Present (Expression (Comp)) then
11186 ("component of imported 'C'P'P type cannot have "
11187 & "default expression", Expression (Comp));
11193 end Check_CPP_Type_Has_No_Defaults;
11195 ----------------------------
11196 -- Check_Delta_Expression --
11197 ----------------------------
11199 procedure Check_Delta_Expression (E : Node_Id) is
11201 if not (Is_Real_Type (Etype (E))) then
11202 Wrong_Type (E, Any_Real);
11204 elsif not Is_OK_Static_Expression (E) then
11205 Flag_Non_Static_Expr
11206 ("non-static expression used for delta value!", E);
11208 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11209 Error_Msg_N ("delta expression must be positive", E);
11215 -- If any of above errors occurred, then replace the incorrect
11216 -- expression by the real 0.1, which should prevent further errors.
11219 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11220 Analyze_And_Resolve (E, Standard_Float);
11221 end Check_Delta_Expression;
11223 -----------------------------
11224 -- Check_Digits_Expression --
11225 -----------------------------
11227 procedure Check_Digits_Expression (E : Node_Id) is
11229 if not (Is_Integer_Type (Etype (E))) then
11230 Wrong_Type (E, Any_Integer);
11232 elsif not Is_OK_Static_Expression (E) then
11233 Flag_Non_Static_Expr
11234 ("non-static expression used for digits value!", E);
11236 elsif Expr_Value (E) <= 0 then
11237 Error_Msg_N ("digits value must be greater than zero", E);
11243 -- If any of above errors occurred, then replace the incorrect
11244 -- expression by the integer 1, which should prevent further errors.
11246 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11247 Analyze_And_Resolve (E, Standard_Integer);
11249 end Check_Digits_Expression;
11251 --------------------------
11252 -- Check_Initialization --
11253 --------------------------
11255 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11257 -- Special processing for limited types
11259 if Is_Limited_Type (T)
11260 and then not In_Instance
11261 and then not In_Inlined_Body
11263 if not OK_For_Limited_Init (T, Exp) then
11265 -- In GNAT mode, this is just a warning, to allow it to be evilly
11266 -- turned off. Otherwise it is a real error.
11270 ("??cannot initialize entities of limited type!", Exp);
11272 elsif Ada_Version < Ada_2005 then
11274 -- The side effect removal machinery may generate illegal Ada
11275 -- code to avoid the usage of access types and 'reference in
11276 -- SPARK mode. Since this is legal code with respect to theorem
11277 -- proving, do not emit the error.
11280 and then Nkind (Exp) = N_Function_Call
11281 and then Nkind (Parent (Exp)) = N_Object_Declaration
11282 and then not Comes_From_Source
11283 (Defining_Identifier (Parent (Exp)))
11289 ("cannot initialize entities of limited type", Exp);
11290 Explain_Limited_Type (T, Exp);
11294 -- Specialize error message according to kind of illegal
11295 -- initial expression.
11297 if Nkind (Exp) = N_Type_Conversion
11298 and then Nkind (Expression (Exp)) = N_Function_Call
11301 ("illegal context for call"
11302 & " to function with limited result", Exp);
11306 ("initialization of limited object requires aggregate "
11307 & "or function call", Exp);
11313 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11314 -- set unless we can be sure that no range check is required.
11316 if (GNATprove_Mode or not Expander_Active)
11317 and then Is_Scalar_Type (T)
11318 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11320 Set_Do_Range_Check (Exp);
11322 end Check_Initialization;
11324 ----------------------
11325 -- Check_Interfaces --
11326 ----------------------
11328 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11329 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11332 Iface_Def : Node_Id;
11333 Iface_Typ : Entity_Id;
11334 Parent_Node : Node_Id;
11336 Is_Task : Boolean := False;
11337 -- Set True if parent type or any progenitor is a task interface
11339 Is_Protected : Boolean := False;
11340 -- Set True if parent type or any progenitor is a protected interface
11342 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11343 -- Check that a progenitor is compatible with declaration. If an error
11344 -- message is output, it is posted on Error_Node.
11350 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11351 Iface_Id : constant Entity_Id :=
11352 Defining_Identifier (Parent (Iface_Def));
11353 Type_Def : Node_Id;
11356 if Nkind (N) = N_Private_Extension_Declaration then
11359 Type_Def := Type_Definition (N);
11362 if Is_Task_Interface (Iface_Id) then
11365 elsif Is_Protected_Interface (Iface_Id) then
11366 Is_Protected := True;
11369 if Is_Synchronized_Interface (Iface_Id) then
11371 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11372 -- extension derived from a synchronized interface must explicitly
11373 -- be declared synchronized, because the full view will be a
11374 -- synchronized type.
11376 if Nkind (N) = N_Private_Extension_Declaration then
11377 if not Synchronized_Present (N) then
11379 ("private extension of& must be explicitly synchronized",
11383 -- However, by 3.9.4(16/2), a full type that is a record extension
11384 -- is never allowed to derive from a synchronized interface (note
11385 -- that interfaces must be excluded from this check, because those
11386 -- are represented by derived type definitions in some cases).
11388 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11389 and then not Interface_Present (Type_Definition (N))
11391 Error_Msg_N ("record extension cannot derive from synchronized "
11392 & "interface", Error_Node);
11396 -- Check that the characteristics of the progenitor are compatible
11397 -- with the explicit qualifier in the declaration.
11398 -- The check only applies to qualifiers that come from source.
11399 -- Limited_Present also appears in the declaration of corresponding
11400 -- records, and the check does not apply to them.
11402 if Limited_Present (Type_Def)
11404 Is_Concurrent_Record_Type (Defining_Identifier (N))
11406 if Is_Limited_Interface (Parent_Type)
11407 and then not Is_Limited_Interface (Iface_Id)
11410 ("progenitor & must be limited interface",
11411 Error_Node, Iface_Id);
11414 (Task_Present (Iface_Def)
11415 or else Protected_Present (Iface_Def)
11416 or else Synchronized_Present (Iface_Def))
11417 and then Nkind (N) /= N_Private_Extension_Declaration
11418 and then not Error_Posted (N)
11421 ("progenitor & must be limited interface",
11422 Error_Node, Iface_Id);
11425 -- Protected interfaces can only inherit from limited, synchronized
11426 -- or protected interfaces.
11428 elsif Nkind (N) = N_Full_Type_Declaration
11429 and then Protected_Present (Type_Def)
11431 if Limited_Present (Iface_Def)
11432 or else Synchronized_Present (Iface_Def)
11433 or else Protected_Present (Iface_Def)
11437 elsif Task_Present (Iface_Def) then
11438 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11439 & "from task interface", Error_Node);
11442 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11443 & "from non-limited interface", Error_Node);
11446 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11447 -- limited and synchronized.
11449 elsif Synchronized_Present (Type_Def) then
11450 if Limited_Present (Iface_Def)
11451 or else Synchronized_Present (Iface_Def)
11455 elsif Protected_Present (Iface_Def)
11456 and then Nkind (N) /= N_Private_Extension_Declaration
11458 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11459 & "from protected interface", Error_Node);
11461 elsif Task_Present (Iface_Def)
11462 and then Nkind (N) /= N_Private_Extension_Declaration
11464 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11465 & "from task interface", Error_Node);
11467 elsif not Is_Limited_Interface (Iface_Id) then
11468 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11469 & "from non-limited interface", Error_Node);
11472 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11473 -- synchronized or task interfaces.
11475 elsif Nkind (N) = N_Full_Type_Declaration
11476 and then Task_Present (Type_Def)
11478 if Limited_Present (Iface_Def)
11479 or else Synchronized_Present (Iface_Def)
11480 or else Task_Present (Iface_Def)
11484 elsif Protected_Present (Iface_Def) then
11485 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11486 & "protected interface", Error_Node);
11489 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11490 & "non-limited interface", Error_Node);
11495 -- Start of processing for Check_Interfaces
11498 if Is_Interface (Parent_Type) then
11499 if Is_Task_Interface (Parent_Type) then
11502 elsif Is_Protected_Interface (Parent_Type) then
11503 Is_Protected := True;
11507 if Nkind (N) = N_Private_Extension_Declaration then
11509 -- Check that progenitors are compatible with declaration
11511 Iface := First (Interface_List (Def));
11512 while Present (Iface) loop
11513 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11515 Parent_Node := Parent (Base_Type (Iface_Typ));
11516 Iface_Def := Type_Definition (Parent_Node);
11518 if not Is_Interface (Iface_Typ) then
11519 Diagnose_Interface (Iface, Iface_Typ);
11521 Check_Ifaces (Iface_Def, Iface);
11527 if Is_Task and Is_Protected then
11529 ("type cannot derive from task and protected interface", N);
11535 -- Full type declaration of derived type.
11536 -- Check compatibility with parent if it is interface type
11538 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11539 and then Is_Interface (Parent_Type)
11541 Parent_Node := Parent (Parent_Type);
11543 -- More detailed checks for interface varieties
11546 (Iface_Def => Type_Definition (Parent_Node),
11547 Error_Node => Subtype_Indication (Type_Definition (N)));
11550 Iface := First (Interface_List (Def));
11551 while Present (Iface) loop
11552 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11554 Parent_Node := Parent (Base_Type (Iface_Typ));
11555 Iface_Def := Type_Definition (Parent_Node);
11557 if not Is_Interface (Iface_Typ) then
11558 Diagnose_Interface (Iface, Iface_Typ);
11561 -- "The declaration of a specific descendant of an interface
11562 -- type freezes the interface type" RM 13.14
11564 Freeze_Before (N, Iface_Typ);
11565 Check_Ifaces (Iface_Def, Error_Node => Iface);
11571 if Is_Task and Is_Protected then
11573 ("type cannot derive from task and protected interface", N);
11575 end Check_Interfaces;
11577 ------------------------------------
11578 -- Check_Or_Process_Discriminants --
11579 ------------------------------------
11581 -- If an incomplete or private type declaration was already given for the
11582 -- type, the discriminants may have already been processed if they were
11583 -- present on the incomplete declaration. In this case a full conformance
11584 -- check has been performed in Find_Type_Name, and we then recheck here
11585 -- some properties that can't be checked on the partial view alone.
11586 -- Otherwise we call Process_Discriminants.
11588 procedure Check_Or_Process_Discriminants
11591 Prev : Entity_Id := Empty)
11594 if Has_Discriminants (T) then
11596 -- Discriminants are already set on T if they were already present
11597 -- on the partial view. Make them visible to component declarations.
11601 -- Discriminant on T (full view) referencing expr on partial view
11603 Prev_D : Entity_Id;
11604 -- Entity of corresponding discriminant on partial view
11607 -- Discriminant specification for full view, expression is
11608 -- the syntactic copy on full view (which has been checked for
11609 -- conformance with partial view), only used here to post error
11613 D := First_Discriminant (T);
11614 New_D := First (Discriminant_Specifications (N));
11615 while Present (D) loop
11616 Prev_D := Current_Entity (D);
11617 Set_Current_Entity (D);
11618 Set_Is_Immediately_Visible (D);
11619 Set_Homonym (D, Prev_D);
11621 -- Handle the case where there is an untagged partial view and
11622 -- the full view is tagged: must disallow discriminants with
11623 -- defaults, unless compiling for Ada 2012, which allows a
11624 -- limited tagged type to have defaulted discriminants (see
11625 -- AI05-0214). However, suppress error here if it was already
11626 -- reported on the default expression of the partial view.
11628 if Is_Tagged_Type (T)
11629 and then Present (Expression (Parent (D)))
11630 and then (not Is_Limited_Type (Current_Scope)
11631 or else Ada_Version < Ada_2012)
11632 and then not Error_Posted (Expression (Parent (D)))
11634 if Ada_Version >= Ada_2012 then
11636 ("discriminants of nonlimited tagged type cannot have "
11638 Expression (New_D));
11641 ("discriminants of tagged type cannot have defaults",
11642 Expression (New_D));
11646 -- Ada 2005 (AI-230): Access discriminant allowed in
11647 -- non-limited record types.
11649 if Ada_Version < Ada_2005 then
11651 -- This restriction gets applied to the full type here. It
11652 -- has already been applied earlier to the partial view.
11654 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11657 Next_Discriminant (D);
11662 elsif Present (Discriminant_Specifications (N)) then
11663 Process_Discriminants (N, Prev);
11665 end Check_Or_Process_Discriminants;
11667 ----------------------
11668 -- Check_Real_Bound --
11669 ----------------------
11671 procedure Check_Real_Bound (Bound : Node_Id) is
11673 if not Is_Real_Type (Etype (Bound)) then
11675 ("bound in real type definition must be of real type", Bound);
11677 elsif not Is_OK_Static_Expression (Bound) then
11678 Flag_Non_Static_Expr
11679 ("non-static expression used for real type bound!", Bound);
11686 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11688 Resolve (Bound, Standard_Float);
11689 end Check_Real_Bound;
11691 ------------------------------
11692 -- Complete_Private_Subtype --
11693 ------------------------------
11695 procedure Complete_Private_Subtype
11698 Full_Base : Entity_Id;
11699 Related_Nod : Node_Id)
11701 Save_Next_Entity : Entity_Id;
11702 Save_Homonym : Entity_Id;
11705 -- Set semantic attributes for (implicit) private subtype completion.
11706 -- If the full type has no discriminants, then it is a copy of the
11707 -- full view of the base. Otherwise, it is a subtype of the base with
11708 -- a possible discriminant constraint. Save and restore the original
11709 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11710 -- not corrupt the entity chain.
11712 -- Note that the type of the full view is the same entity as the type
11713 -- of the partial view. In this fashion, the subtype has access to the
11714 -- correct view of the parent.
11716 Save_Next_Entity := Next_Entity (Full);
11717 Save_Homonym := Homonym (Priv);
11719 case Ekind (Full_Base) is
11720 when E_Record_Type |
11726 Copy_Node (Priv, Full);
11728 Set_Has_Discriminants
11729 (Full, Has_Discriminants (Full_Base));
11730 Set_Has_Unknown_Discriminants
11731 (Full, Has_Unknown_Discriminants (Full_Base));
11732 Set_First_Entity (Full, First_Entity (Full_Base));
11733 Set_Last_Entity (Full, Last_Entity (Full_Base));
11735 -- If the underlying base type is constrained, we know that the
11736 -- full view of the subtype is constrained as well (the converse
11737 -- is not necessarily true).
11739 if Is_Constrained (Full_Base) then
11740 Set_Is_Constrained (Full);
11744 Copy_Node (Full_Base, Full);
11746 Set_Chars (Full, Chars (Priv));
11747 Conditional_Delay (Full, Priv);
11748 Set_Sloc (Full, Sloc (Priv));
11751 Set_Next_Entity (Full, Save_Next_Entity);
11752 Set_Homonym (Full, Save_Homonym);
11753 Set_Associated_Node_For_Itype (Full, Related_Nod);
11755 -- Set common attributes for all subtypes: kind, convention, etc.
11757 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11758 Set_Convention (Full, Convention (Full_Base));
11760 -- The Etype of the full view is inconsistent. Gigi needs to see the
11761 -- structural full view, which is what the current scheme gives: the
11762 -- Etype of the full view is the etype of the full base. However, if the
11763 -- full base is a derived type, the full view then looks like a subtype
11764 -- of the parent, not a subtype of the full base. If instead we write:
11766 -- Set_Etype (Full, Full_Base);
11768 -- then we get inconsistencies in the front-end (confusion between
11769 -- views). Several outstanding bugs are related to this ???
11771 Set_Is_First_Subtype (Full, False);
11772 Set_Scope (Full, Scope (Priv));
11773 Set_Size_Info (Full, Full_Base);
11774 Set_RM_Size (Full, RM_Size (Full_Base));
11775 Set_Is_Itype (Full);
11777 -- A subtype of a private-type-without-discriminants, whose full-view
11778 -- has discriminants with default expressions, is not constrained.
11780 if not Has_Discriminants (Priv) then
11781 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11783 if Has_Discriminants (Full_Base) then
11784 Set_Discriminant_Constraint
11785 (Full, Discriminant_Constraint (Full_Base));
11787 -- The partial view may have been indefinite, the full view
11790 Set_Has_Unknown_Discriminants
11791 (Full, Has_Unknown_Discriminants (Full_Base));
11795 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11796 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11798 -- Freeze the private subtype entity if its parent is delayed, and not
11799 -- already frozen. We skip this processing if the type is an anonymous
11800 -- subtype of a record component, or is the corresponding record of a
11801 -- protected type, since these are processed when the enclosing type
11804 if not Is_Type (Scope (Full)) then
11805 Set_Has_Delayed_Freeze (Full,
11806 Has_Delayed_Freeze (Full_Base)
11807 and then (not Is_Frozen (Full_Base)));
11810 Set_Freeze_Node (Full, Empty);
11811 Set_Is_Frozen (Full, False);
11812 Set_Full_View (Priv, Full);
11814 if Has_Discriminants (Full) then
11815 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
11816 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
11818 if Has_Unknown_Discriminants (Full) then
11819 Set_Discriminant_Constraint (Full, No_Elist);
11823 if Ekind (Full_Base) = E_Record_Type
11824 and then Has_Discriminants (Full_Base)
11825 and then Has_Discriminants (Priv) -- might not, if errors
11826 and then not Has_Unknown_Discriminants (Priv)
11827 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
11829 Create_Constrained_Components
11830 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
11832 -- If the full base is itself derived from private, build a congruent
11833 -- subtype of its underlying type, for use by the back end. For a
11834 -- constrained record component, the declaration cannot be placed on
11835 -- the component list, but it must nevertheless be built an analyzed, to
11836 -- supply enough information for Gigi to compute the size of component.
11838 elsif Ekind (Full_Base) in Private_Kind
11839 and then Is_Derived_Type (Full_Base)
11840 and then Has_Discriminants (Full_Base)
11841 and then (Ekind (Current_Scope) /= E_Record_Subtype)
11843 if not Is_Itype (Priv)
11845 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
11847 Build_Underlying_Full_View
11848 (Parent (Priv), Full, Etype (Full_Base));
11850 elsif Nkind (Related_Nod) = N_Component_Declaration then
11851 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
11854 elsif Is_Record_Type (Full_Base) then
11856 -- Show Full is simply a renaming of Full_Base
11858 Set_Cloned_Subtype (Full, Full_Base);
11861 -- It is unsafe to share the bounds of a scalar type, because the Itype
11862 -- is elaborated on demand, and if a bound is non-static then different
11863 -- orders of elaboration in different units will lead to different
11864 -- external symbols.
11866 if Is_Scalar_Type (Full_Base) then
11867 Set_Scalar_Range (Full,
11868 Make_Range (Sloc (Related_Nod),
11870 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
11872 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
11874 -- This completion inherits the bounds of the full parent, but if
11875 -- the parent is an unconstrained floating point type, so is the
11878 if Is_Floating_Point_Type (Full_Base) then
11879 Set_Includes_Infinities
11880 (Scalar_Range (Full), Has_Infinities (Full_Base));
11884 -- ??? It seems that a lot of fields are missing that should be copied
11885 -- from Full_Base to Full. Here are some that are introduced in a
11886 -- non-disruptive way but a cleanup is necessary.
11888 if Is_Tagged_Type (Full_Base) then
11889 Set_Is_Tagged_Type (Full);
11890 Set_Direct_Primitive_Operations
11891 (Full, Direct_Primitive_Operations (Full_Base));
11892 Set_No_Tagged_Streams_Pragma
11893 (Full, No_Tagged_Streams_Pragma (Full_Base));
11895 -- Inherit class_wide type of full_base in case the partial view was
11896 -- not tagged. Otherwise it has already been created when the private
11897 -- subtype was analyzed.
11899 if No (Class_Wide_Type (Full)) then
11900 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
11903 -- If this is a subtype of a protected or task type, constrain its
11904 -- corresponding record, unless this is a subtype without constraints,
11905 -- i.e. a simple renaming as with an actual subtype in an instance.
11907 elsif Is_Concurrent_Type (Full_Base) then
11908 if Has_Discriminants (Full)
11909 and then Present (Corresponding_Record_Type (Full_Base))
11911 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
11913 Set_Corresponding_Record_Type (Full,
11914 Constrain_Corresponding_Record
11915 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
11918 Set_Corresponding_Record_Type (Full,
11919 Corresponding_Record_Type (Full_Base));
11923 -- Link rep item chain, and also setting of Has_Predicates from private
11924 -- subtype to full subtype, since we will need these on the full subtype
11925 -- to create the predicate function. Note that the full subtype may
11926 -- already have rep items, inherited from the full view of the base
11927 -- type, so we must be sure not to overwrite these entries.
11932 Next_Item : Node_Id;
11933 Priv_Item : Node_Id;
11936 Item := First_Rep_Item (Full);
11937 Priv_Item := First_Rep_Item (Priv);
11939 -- If no existing rep items on full type, we can just link directly
11940 -- to the list of items on the private type, if any exist.. Same if
11941 -- the rep items are only those inherited from the base
11944 or else Nkind (Item) /= N_Aspect_Specification
11945 or else Entity (Item) = Full_Base)
11946 and then Present (First_Rep_Item (Priv))
11948 Set_First_Rep_Item (Full, Priv_Item);
11950 -- Otherwise, search to the end of items currently linked to the full
11951 -- subtype and append the private items to the end. However, if Priv
11952 -- and Full already have the same list of rep items, then the append
11953 -- is not done, as that would create a circularity.
11955 -- The partial view may have a predicate and the rep item lists of
11956 -- both views agree when inherited from the same ancestor. In that
11957 -- case, simply propagate the list from one view to the other.
11958 -- A more complex analysis needed here ???
11960 elsif Present (Priv_Item)
11961 and then Item = Next_Rep_Item (Priv_Item)
11963 Set_First_Rep_Item (Full, Priv_Item);
11965 elsif Item /= Priv_Item then
11968 Next_Item := Next_Rep_Item (Item);
11969 exit when No (Next_Item);
11972 -- If the private view has aspect specifications, the full view
11973 -- inherits them. Since these aspects may already have been
11974 -- attached to the full view during derivation, do not append
11975 -- them if already present.
11977 if Item = First_Rep_Item (Priv) then
11983 -- And link the private type items at the end of the chain
11986 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
11991 -- Make sure Has_Predicates is set on full type if it is set on the
11992 -- private type. Note that it may already be set on the full type and
11993 -- if so, we don't want to unset it. Similarly, propagate information
11994 -- about delayed aspects, because the corresponding pragmas must be
11995 -- analyzed when one of the views is frozen. This last step is needed
11996 -- in particular when the full type is a scalar type for which an
11997 -- anonymous base type is constructed.
11999 -- The predicate functions are generated either at the freeze point
12000 -- of the type or at the end of the visible part, and we must avoid
12001 -- generating them twice.
12003 if Has_Predicates (Priv) then
12004 Set_Has_Predicates (Full);
12006 if Present (Predicate_Function (Priv))
12007 and then No (Predicate_Function (Full))
12009 Set_Predicate_Function (Full, Predicate_Function (Priv));
12013 if Has_Delayed_Aspects (Priv) then
12014 Set_Has_Delayed_Aspects (Full);
12016 end Complete_Private_Subtype;
12018 ----------------------------
12019 -- Constant_Redeclaration --
12020 ----------------------------
12022 procedure Constant_Redeclaration
12027 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12028 Obj_Def : constant Node_Id := Object_Definition (N);
12031 procedure Check_Possible_Deferred_Completion
12032 (Prev_Id : Entity_Id;
12033 Prev_Obj_Def : Node_Id;
12034 Curr_Obj_Def : Node_Id);
12035 -- Determine whether the two object definitions describe the partial
12036 -- and the full view of a constrained deferred constant. Generate
12037 -- a subtype for the full view and verify that it statically matches
12038 -- the subtype of the partial view.
12040 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12041 -- If deferred constant is an access type initialized with an allocator,
12042 -- check whether there is an illegal recursion in the definition,
12043 -- through a default value of some record subcomponent. This is normally
12044 -- detected when generating init procs, but requires this additional
12045 -- mechanism when expansion is disabled.
12047 ----------------------------------------
12048 -- Check_Possible_Deferred_Completion --
12049 ----------------------------------------
12051 procedure Check_Possible_Deferred_Completion
12052 (Prev_Id : Entity_Id;
12053 Prev_Obj_Def : Node_Id;
12054 Curr_Obj_Def : Node_Id)
12057 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12058 and then Present (Constraint (Prev_Obj_Def))
12059 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12060 and then Present (Constraint (Curr_Obj_Def))
12063 Loc : constant Source_Ptr := Sloc (N);
12064 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12065 Decl : constant Node_Id :=
12066 Make_Subtype_Declaration (Loc,
12067 Defining_Identifier => Def_Id,
12068 Subtype_Indication =>
12069 Relocate_Node (Curr_Obj_Def));
12072 Insert_Before_And_Analyze (N, Decl);
12073 Set_Etype (Id, Def_Id);
12075 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12076 Error_Msg_Sloc := Sloc (Prev_Id);
12077 Error_Msg_N ("subtype does not statically match deferred "
12078 & "declaration #", N);
12082 end Check_Possible_Deferred_Completion;
12084 ---------------------------------
12085 -- Check_Recursive_Declaration --
12086 ---------------------------------
12088 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12092 if Is_Record_Type (Typ) then
12093 Comp := First_Component (Typ);
12094 while Present (Comp) loop
12095 if Comes_From_Source (Comp) then
12096 if Present (Expression (Parent (Comp)))
12097 and then Is_Entity_Name (Expression (Parent (Comp)))
12098 and then Entity (Expression (Parent (Comp))) = Prev
12100 Error_Msg_Sloc := Sloc (Parent (Comp));
12102 ("illegal circularity with declaration for & #",
12106 elsif Is_Record_Type (Etype (Comp)) then
12107 Check_Recursive_Declaration (Etype (Comp));
12111 Next_Component (Comp);
12114 end Check_Recursive_Declaration;
12116 -- Start of processing for Constant_Redeclaration
12119 if Nkind (Parent (Prev)) = N_Object_Declaration then
12120 if Nkind (Object_Definition
12121 (Parent (Prev))) = N_Subtype_Indication
12123 -- Find type of new declaration. The constraints of the two
12124 -- views must match statically, but there is no point in
12125 -- creating an itype for the full view.
12127 if Nkind (Obj_Def) = N_Subtype_Indication then
12128 Find_Type (Subtype_Mark (Obj_Def));
12129 New_T := Entity (Subtype_Mark (Obj_Def));
12132 Find_Type (Obj_Def);
12133 New_T := Entity (Obj_Def);
12139 -- The full view may impose a constraint, even if the partial
12140 -- view does not, so construct the subtype.
12142 New_T := Find_Type_Of_Object (Obj_Def, N);
12147 -- Current declaration is illegal, diagnosed below in Enter_Name
12153 -- If previous full declaration or a renaming declaration exists, or if
12154 -- a homograph is present, let Enter_Name handle it, either with an
12155 -- error or with the removal of an overridden implicit subprogram.
12156 -- The previous one is a full declaration if it has an expression
12157 -- (which in the case of an aggregate is indicated by the Init flag).
12159 if Ekind (Prev) /= E_Constant
12160 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12161 or else Present (Expression (Parent (Prev)))
12162 or else Has_Init_Expression (Parent (Prev))
12163 or else Present (Full_View (Prev))
12167 -- Verify that types of both declarations match, or else that both types
12168 -- are anonymous access types whose designated subtypes statically match
12169 -- (as allowed in Ada 2005 by AI-385).
12171 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12173 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12174 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12175 or else Is_Access_Constant (Etype (New_T)) /=
12176 Is_Access_Constant (Etype (Prev))
12177 or else Can_Never_Be_Null (Etype (New_T)) /=
12178 Can_Never_Be_Null (Etype (Prev))
12179 or else Null_Exclusion_Present (Parent (Prev)) /=
12180 Null_Exclusion_Present (Parent (Id))
12181 or else not Subtypes_Statically_Match
12182 (Designated_Type (Etype (Prev)),
12183 Designated_Type (Etype (New_T))))
12185 Error_Msg_Sloc := Sloc (Prev);
12186 Error_Msg_N ("type does not match declaration#", N);
12187 Set_Full_View (Prev, Id);
12188 Set_Etype (Id, Any_Type);
12190 -- A deferred constant whose type is an anonymous array is always
12191 -- illegal (unless imported). A detailed error message might be
12192 -- helpful for Ada beginners.
12194 if Nkind (Object_Definition (Parent (Prev)))
12195 = N_Constrained_Array_Definition
12196 and then Nkind (Object_Definition (N))
12197 = N_Constrained_Array_Definition
12199 Error_Msg_N ("\each anonymous array is a distinct type", N);
12200 Error_Msg_N ("a deferred constant must have a named type",
12201 Object_Definition (Parent (Prev)));
12205 Null_Exclusion_Present (Parent (Prev))
12206 and then not Null_Exclusion_Present (N)
12208 Error_Msg_Sloc := Sloc (Prev);
12209 Error_Msg_N ("null-exclusion does not match declaration#", N);
12210 Set_Full_View (Prev, Id);
12211 Set_Etype (Id, Any_Type);
12213 -- If so, process the full constant declaration
12216 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12217 -- the deferred declaration is constrained, then the subtype defined
12218 -- by the subtype_indication in the full declaration shall match it
12221 Check_Possible_Deferred_Completion
12223 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12224 Curr_Obj_Def => Obj_Def);
12226 Set_Full_View (Prev, Id);
12227 Set_Is_Public (Id, Is_Public (Prev));
12228 Set_Is_Internal (Id);
12229 Append_Entity (Id, Current_Scope);
12231 -- Check ALIASED present if present before (RM 7.4(7))
12233 if Is_Aliased (Prev)
12234 and then not Aliased_Present (N)
12236 Error_Msg_Sloc := Sloc (Prev);
12237 Error_Msg_N ("ALIASED required (see declaration #)", N);
12240 -- Check that placement is in private part and that the incomplete
12241 -- declaration appeared in the visible part.
12243 if Ekind (Current_Scope) = E_Package
12244 and then not In_Private_Part (Current_Scope)
12246 Error_Msg_Sloc := Sloc (Prev);
12248 ("full constant for declaration # must be in private part", N);
12250 elsif Ekind (Current_Scope) = E_Package
12252 List_Containing (Parent (Prev)) /=
12253 Visible_Declarations (Package_Specification (Current_Scope))
12256 ("deferred constant must be declared in visible part",
12260 if Is_Access_Type (T)
12261 and then Nkind (Expression (N)) = N_Allocator
12263 Check_Recursive_Declaration (Designated_Type (T));
12266 -- A deferred constant is a visible entity. If type has invariants,
12267 -- verify that the initial value satisfies them.
12269 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12271 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12274 end Constant_Redeclaration;
12276 ----------------------
12277 -- Constrain_Access --
12278 ----------------------
12280 procedure Constrain_Access
12281 (Def_Id : in out Entity_Id;
12283 Related_Nod : Node_Id)
12285 T : constant Entity_Id := Entity (Subtype_Mark (S));
12286 Desig_Type : constant Entity_Id := Designated_Type (T);
12287 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12288 Constraint_OK : Boolean := True;
12291 if Is_Array_Type (Desig_Type) then
12292 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12294 elsif (Is_Record_Type (Desig_Type)
12295 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12296 and then not Is_Constrained (Desig_Type)
12298 -- ??? The following code is a temporary bypass to ignore a
12299 -- discriminant constraint on access type if it is constraining
12300 -- the current record. Avoid creating the implicit subtype of the
12301 -- record we are currently compiling since right now, we cannot
12302 -- handle these. For now, just return the access type itself.
12304 if Desig_Type = Current_Scope
12305 and then No (Def_Id)
12307 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12308 Def_Id := Entity (Subtype_Mark (S));
12310 -- This call added to ensure that the constraint is analyzed
12311 -- (needed for a B test). Note that we still return early from
12312 -- this procedure to avoid recursive processing. ???
12314 Constrain_Discriminated_Type
12315 (Desig_Subtype, S, Related_Nod, For_Access => True);
12319 -- Enforce rule that the constraint is illegal if there is an
12320 -- unconstrained view of the designated type. This means that the
12321 -- partial view (either a private type declaration or a derivation
12322 -- from a private type) has no discriminants. (Defect Report
12323 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12325 -- Rule updated for Ada 2005: The private type is said to have
12326 -- a constrained partial view, given that objects of the type
12327 -- can be declared. Furthermore, the rule applies to all access
12328 -- types, unlike the rule concerning default discriminants (see
12331 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12332 and then Has_Private_Declaration (Desig_Type)
12333 and then In_Open_Scopes (Scope (Desig_Type))
12334 and then Has_Discriminants (Desig_Type)
12337 Pack : constant Node_Id :=
12338 Unit_Declaration_Node (Scope (Desig_Type));
12343 if Nkind (Pack) = N_Package_Declaration then
12344 Decls := Visible_Declarations (Specification (Pack));
12345 Decl := First (Decls);
12346 while Present (Decl) loop
12347 if (Nkind (Decl) = N_Private_Type_Declaration
12348 and then Chars (Defining_Identifier (Decl)) =
12349 Chars (Desig_Type))
12352 (Nkind (Decl) = N_Full_Type_Declaration
12354 Chars (Defining_Identifier (Decl)) =
12356 and then Is_Derived_Type (Desig_Type)
12358 Has_Private_Declaration (Etype (Desig_Type)))
12360 if No (Discriminant_Specifications (Decl)) then
12362 ("cannot constrain access type if designated "
12363 & "type has constrained partial view", S);
12375 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12376 For_Access => True);
12378 elsif Is_Concurrent_Type (Desig_Type)
12379 and then not Is_Constrained (Desig_Type)
12381 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12384 Error_Msg_N ("invalid constraint on access type", S);
12386 -- We simply ignore an invalid constraint
12388 Desig_Subtype := Desig_Type;
12389 Constraint_OK := False;
12392 if No (Def_Id) then
12393 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12395 Set_Ekind (Def_Id, E_Access_Subtype);
12398 if Constraint_OK then
12399 Set_Etype (Def_Id, Base_Type (T));
12401 if Is_Private_Type (Desig_Type) then
12402 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12405 Set_Etype (Def_Id, Any_Type);
12408 Set_Size_Info (Def_Id, T);
12409 Set_Is_Constrained (Def_Id, Constraint_OK);
12410 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12411 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12412 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12414 Conditional_Delay (Def_Id, T);
12416 -- AI-363 : Subtypes of general access types whose designated types have
12417 -- default discriminants are disallowed. In instances, the rule has to
12418 -- be checked against the actual, of which T is the subtype. In a
12419 -- generic body, the rule is checked assuming that the actual type has
12420 -- defaulted discriminants.
12422 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12423 if Ekind (Base_Type (T)) = E_General_Access_Type
12424 and then Has_Defaulted_Discriminants (Desig_Type)
12426 if Ada_Version < Ada_2005 then
12428 ("access subtype of general access type would not " &
12429 "be allowed in Ada 2005?y?", S);
12432 ("access subtype of general access type not allowed", S);
12435 Error_Msg_N ("\discriminants have defaults", S);
12437 elsif Is_Access_Type (T)
12438 and then Is_Generic_Type (Desig_Type)
12439 and then Has_Discriminants (Desig_Type)
12440 and then In_Package_Body (Current_Scope)
12442 if Ada_Version < Ada_2005 then
12444 ("access subtype would not be allowed in generic body "
12445 & "in Ada 2005?y?", S);
12448 ("access subtype not allowed in generic body", S);
12452 ("\designated type is a discriminated formal", S);
12455 end Constrain_Access;
12457 ---------------------
12458 -- Constrain_Array --
12459 ---------------------
12461 procedure Constrain_Array
12462 (Def_Id : in out Entity_Id;
12464 Related_Nod : Node_Id;
12465 Related_Id : Entity_Id;
12466 Suffix : Character)
12468 C : constant Node_Id := Constraint (SI);
12469 Number_Of_Constraints : Nat := 0;
12472 Constraint_OK : Boolean := True;
12475 T := Entity (Subtype_Mark (SI));
12477 if Is_Access_Type (T) then
12478 T := Designated_Type (T);
12481 -- If an index constraint follows a subtype mark in a subtype indication
12482 -- then the type or subtype denoted by the subtype mark must not already
12483 -- impose an index constraint. The subtype mark must denote either an
12484 -- unconstrained array type or an access type whose designated type
12485 -- is such an array type... (RM 3.6.1)
12487 if Is_Constrained (T) then
12488 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12489 Constraint_OK := False;
12492 S := First (Constraints (C));
12493 while Present (S) loop
12494 Number_Of_Constraints := Number_Of_Constraints + 1;
12498 -- In either case, the index constraint must provide a discrete
12499 -- range for each index of the array type and the type of each
12500 -- discrete range must be the same as that of the corresponding
12501 -- index. (RM 3.6.1)
12503 if Number_Of_Constraints /= Number_Dimensions (T) then
12504 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12505 Constraint_OK := False;
12508 S := First (Constraints (C));
12509 Index := First_Index (T);
12512 -- Apply constraints to each index type
12514 for J in 1 .. Number_Of_Constraints loop
12515 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12523 if No (Def_Id) then
12525 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12526 Set_Parent (Def_Id, Related_Nod);
12529 Set_Ekind (Def_Id, E_Array_Subtype);
12532 Set_Size_Info (Def_Id, (T));
12533 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12534 Set_Etype (Def_Id, Base_Type (T));
12536 if Constraint_OK then
12537 Set_First_Index (Def_Id, First (Constraints (C)));
12539 Set_First_Index (Def_Id, First_Index (T));
12542 Set_Is_Constrained (Def_Id, True);
12543 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12544 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12546 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12547 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12549 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12550 -- We need to initialize the attribute because if Def_Id is previously
12551 -- analyzed through a limited_with clause, it will have the attributes
12552 -- of an incomplete type, one of which is an Elist that overlaps the
12553 -- Packed_Array_Impl_Type field.
12555 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12557 -- Build a freeze node if parent still needs one. Also make sure that
12558 -- the Depends_On_Private status is set because the subtype will need
12559 -- reprocessing at the time the base type does, and also we must set a
12560 -- conditional delay.
12562 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12563 Conditional_Delay (Def_Id, T);
12564 end Constrain_Array;
12566 ------------------------------
12567 -- Constrain_Component_Type --
12568 ------------------------------
12570 function Constrain_Component_Type
12572 Constrained_Typ : Entity_Id;
12573 Related_Node : Node_Id;
12575 Constraints : Elist_Id) return Entity_Id
12577 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12578 Compon_Type : constant Entity_Id := Etype (Comp);
12580 function Build_Constrained_Array_Type
12581 (Old_Type : Entity_Id) return Entity_Id;
12582 -- If Old_Type is an array type, one of whose indexes is constrained
12583 -- by a discriminant, build an Itype whose constraint replaces the
12584 -- discriminant with its value in the constraint.
12586 function Build_Constrained_Discriminated_Type
12587 (Old_Type : Entity_Id) return Entity_Id;
12588 -- Ditto for record components
12590 function Build_Constrained_Access_Type
12591 (Old_Type : Entity_Id) return Entity_Id;
12592 -- Ditto for access types. Makes use of previous two functions, to
12593 -- constrain designated type.
12595 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12596 -- T is an array or discriminated type, C is a list of constraints
12597 -- that apply to T. This routine builds the constrained subtype.
12599 function Is_Discriminant (Expr : Node_Id) return Boolean;
12600 -- Returns True if Expr is a discriminant
12602 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12603 -- Find the value of discriminant Discrim in Constraint
12605 -----------------------------------
12606 -- Build_Constrained_Access_Type --
12607 -----------------------------------
12609 function Build_Constrained_Access_Type
12610 (Old_Type : Entity_Id) return Entity_Id
12612 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12614 Desig_Subtype : Entity_Id;
12618 -- if the original access type was not embedded in the enclosing
12619 -- type definition, there is no need to produce a new access
12620 -- subtype. In fact every access type with an explicit constraint
12621 -- generates an itype whose scope is the enclosing record.
12623 if not Is_Type (Scope (Old_Type)) then
12626 elsif Is_Array_Type (Desig_Type) then
12627 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12629 elsif Has_Discriminants (Desig_Type) then
12631 -- This may be an access type to an enclosing record type for
12632 -- which we are constructing the constrained components. Return
12633 -- the enclosing record subtype. This is not always correct,
12634 -- but avoids infinite recursion. ???
12636 Desig_Subtype := Any_Type;
12638 for J in reverse 0 .. Scope_Stack.Last loop
12639 Scop := Scope_Stack.Table (J).Entity;
12642 and then Base_Type (Scop) = Base_Type (Desig_Type)
12644 Desig_Subtype := Scop;
12647 exit when not Is_Type (Scop);
12650 if Desig_Subtype = Any_Type then
12652 Build_Constrained_Discriminated_Type (Desig_Type);
12659 if Desig_Subtype /= Desig_Type then
12661 -- The Related_Node better be here or else we won't be able
12662 -- to attach new itypes to a node in the tree.
12664 pragma Assert (Present (Related_Node));
12666 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12668 Set_Etype (Itype, Base_Type (Old_Type));
12669 Set_Size_Info (Itype, (Old_Type));
12670 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12671 Set_Depends_On_Private (Itype, Has_Private_Component
12673 Set_Is_Access_Constant (Itype, Is_Access_Constant
12676 -- The new itype needs freezing when it depends on a not frozen
12677 -- type and the enclosing subtype needs freezing.
12679 if Has_Delayed_Freeze (Constrained_Typ)
12680 and then not Is_Frozen (Constrained_Typ)
12682 Conditional_Delay (Itype, Base_Type (Old_Type));
12690 end Build_Constrained_Access_Type;
12692 ----------------------------------
12693 -- Build_Constrained_Array_Type --
12694 ----------------------------------
12696 function Build_Constrained_Array_Type
12697 (Old_Type : Entity_Id) return Entity_Id
12701 Old_Index : Node_Id;
12702 Range_Node : Node_Id;
12703 Constr_List : List_Id;
12705 Need_To_Create_Itype : Boolean := False;
12708 Old_Index := First_Index (Old_Type);
12709 while Present (Old_Index) loop
12710 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12712 if Is_Discriminant (Lo_Expr)
12714 Is_Discriminant (Hi_Expr)
12716 Need_To_Create_Itype := True;
12719 Next_Index (Old_Index);
12722 if Need_To_Create_Itype then
12723 Constr_List := New_List;
12725 Old_Index := First_Index (Old_Type);
12726 while Present (Old_Index) loop
12727 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12729 if Is_Discriminant (Lo_Expr) then
12730 Lo_Expr := Get_Discr_Value (Lo_Expr);
12733 if Is_Discriminant (Hi_Expr) then
12734 Hi_Expr := Get_Discr_Value (Hi_Expr);
12739 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12741 Append (Range_Node, To => Constr_List);
12743 Next_Index (Old_Index);
12746 return Build_Subtype (Old_Type, Constr_List);
12751 end Build_Constrained_Array_Type;
12753 ------------------------------------------
12754 -- Build_Constrained_Discriminated_Type --
12755 ------------------------------------------
12757 function Build_Constrained_Discriminated_Type
12758 (Old_Type : Entity_Id) return Entity_Id
12761 Constr_List : List_Id;
12762 Old_Constraint : Elmt_Id;
12764 Need_To_Create_Itype : Boolean := False;
12767 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12768 while Present (Old_Constraint) loop
12769 Expr := Node (Old_Constraint);
12771 if Is_Discriminant (Expr) then
12772 Need_To_Create_Itype := True;
12775 Next_Elmt (Old_Constraint);
12778 if Need_To_Create_Itype then
12779 Constr_List := New_List;
12781 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12782 while Present (Old_Constraint) loop
12783 Expr := Node (Old_Constraint);
12785 if Is_Discriminant (Expr) then
12786 Expr := Get_Discr_Value (Expr);
12789 Append (New_Copy_Tree (Expr), To => Constr_List);
12791 Next_Elmt (Old_Constraint);
12794 return Build_Subtype (Old_Type, Constr_List);
12799 end Build_Constrained_Discriminated_Type;
12801 -------------------
12802 -- Build_Subtype --
12803 -------------------
12805 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12807 Subtyp_Decl : Node_Id;
12808 Def_Id : Entity_Id;
12809 Btyp : Entity_Id := Base_Type (T);
12812 -- The Related_Node better be here or else we won't be able to
12813 -- attach new itypes to a node in the tree.
12815 pragma Assert (Present (Related_Node));
12817 -- If the view of the component's type is incomplete or private
12818 -- with unknown discriminants, then the constraint must be applied
12819 -- to the full type.
12821 if Has_Unknown_Discriminants (Btyp)
12822 and then Present (Underlying_Type (Btyp))
12824 Btyp := Underlying_Type (Btyp);
12828 Make_Subtype_Indication (Loc,
12829 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
12830 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
12832 Def_Id := Create_Itype (Ekind (T), Related_Node);
12835 Make_Subtype_Declaration (Loc,
12836 Defining_Identifier => Def_Id,
12837 Subtype_Indication => Indic);
12839 Set_Parent (Subtyp_Decl, Parent (Related_Node));
12841 -- Itypes must be analyzed with checks off (see package Itypes)
12843 Analyze (Subtyp_Decl, Suppress => All_Checks);
12848 ---------------------
12849 -- Get_Discr_Value --
12850 ---------------------
12852 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
12857 -- The discriminant may be declared for the type, in which case we
12858 -- find it by iterating over the list of discriminants. If the
12859 -- discriminant is inherited from a parent type, it appears as the
12860 -- corresponding discriminant of the current type. This will be the
12861 -- case when constraining an inherited component whose constraint is
12862 -- given by a discriminant of the parent.
12864 D := First_Discriminant (Typ);
12865 E := First_Elmt (Constraints);
12867 while Present (D) loop
12868 if D = Entity (Discrim)
12869 or else D = CR_Discriminant (Entity (Discrim))
12870 or else Corresponding_Discriminant (D) = Entity (Discrim)
12875 Next_Discriminant (D);
12879 -- The Corresponding_Discriminant mechanism is incomplete, because
12880 -- the correspondence between new and old discriminants is not one
12881 -- to one: one new discriminant can constrain several old ones. In
12882 -- that case, scan sequentially the stored_constraint, the list of
12883 -- discriminants of the parents, and the constraints.
12885 -- Previous code checked for the present of the Stored_Constraint
12886 -- list for the derived type, but did not use it at all. Should it
12887 -- be present when the component is a discriminated task type?
12889 if Is_Derived_Type (Typ)
12890 and then Scope (Entity (Discrim)) = Etype (Typ)
12892 D := First_Discriminant (Etype (Typ));
12893 E := First_Elmt (Constraints);
12894 while Present (D) loop
12895 if D = Entity (Discrim) then
12899 Next_Discriminant (D);
12904 -- Something is wrong if we did not find the value
12906 raise Program_Error;
12907 end Get_Discr_Value;
12909 ---------------------
12910 -- Is_Discriminant --
12911 ---------------------
12913 function Is_Discriminant (Expr : Node_Id) return Boolean is
12914 Discrim_Scope : Entity_Id;
12917 if Denotes_Discriminant (Expr) then
12918 Discrim_Scope := Scope (Entity (Expr));
12920 -- Either we have a reference to one of Typ's discriminants,
12922 pragma Assert (Discrim_Scope = Typ
12924 -- or to the discriminants of the parent type, in the case
12925 -- of a derivation of a tagged type with variants.
12927 or else Discrim_Scope = Etype (Typ)
12928 or else Full_View (Discrim_Scope) = Etype (Typ)
12930 -- or same as above for the case where the discriminants
12931 -- were declared in Typ's private view.
12933 or else (Is_Private_Type (Discrim_Scope)
12934 and then Chars (Discrim_Scope) = Chars (Typ))
12936 -- or else we are deriving from the full view and the
12937 -- discriminant is declared in the private entity.
12939 or else (Is_Private_Type (Typ)
12940 and then Chars (Discrim_Scope) = Chars (Typ))
12942 -- Or we are constrained the corresponding record of a
12943 -- synchronized type that completes a private declaration.
12945 or else (Is_Concurrent_Record_Type (Typ)
12947 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
12949 -- or we have a class-wide type, in which case make sure the
12950 -- discriminant found belongs to the root type.
12952 or else (Is_Class_Wide_Type (Typ)
12953 and then Etype (Typ) = Discrim_Scope));
12958 -- In all other cases we have something wrong
12961 end Is_Discriminant;
12963 -- Start of processing for Constrain_Component_Type
12966 if Nkind (Parent (Comp)) = N_Component_Declaration
12967 and then Comes_From_Source (Parent (Comp))
12968 and then Comes_From_Source
12969 (Subtype_Indication (Component_Definition (Parent (Comp))))
12972 (Subtype_Indication (Component_Definition (Parent (Comp))))
12974 return Compon_Type;
12976 elsif Is_Array_Type (Compon_Type) then
12977 return Build_Constrained_Array_Type (Compon_Type);
12979 elsif Has_Discriminants (Compon_Type) then
12980 return Build_Constrained_Discriminated_Type (Compon_Type);
12982 elsif Is_Access_Type (Compon_Type) then
12983 return Build_Constrained_Access_Type (Compon_Type);
12986 return Compon_Type;
12988 end Constrain_Component_Type;
12990 --------------------------
12991 -- Constrain_Concurrent --
12992 --------------------------
12994 -- For concurrent types, the associated record value type carries the same
12995 -- discriminants, so when we constrain a concurrent type, we must constrain
12996 -- the corresponding record type as well.
12998 procedure Constrain_Concurrent
12999 (Def_Id : in out Entity_Id;
13001 Related_Nod : Node_Id;
13002 Related_Id : Entity_Id;
13003 Suffix : Character)
13005 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13006 -- case of a private subtype (needed when only doing semantic analysis).
13008 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13012 if Is_Access_Type (T_Ent) then
13013 T_Ent := Designated_Type (T_Ent);
13016 T_Val := Corresponding_Record_Type (T_Ent);
13018 if Present (T_Val) then
13020 if No (Def_Id) then
13021 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13023 -- Elaborate itype now, as it may be used in a subsequent
13024 -- synchronized operation in another scope.
13026 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13027 Build_Itype_Reference (Def_Id, Related_Nod);
13031 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13033 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13034 Set_Corresponding_Record_Type (Def_Id,
13035 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13038 -- If there is no associated record, expansion is disabled and this
13039 -- is a generic context. Create a subtype in any case, so that
13040 -- semantic analysis can proceed.
13042 if No (Def_Id) then
13043 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13046 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13048 end Constrain_Concurrent;
13050 ------------------------------------
13051 -- Constrain_Corresponding_Record --
13052 ------------------------------------
13054 function Constrain_Corresponding_Record
13055 (Prot_Subt : Entity_Id;
13056 Corr_Rec : Entity_Id;
13057 Related_Nod : Node_Id) return Entity_Id
13059 T_Sub : constant Entity_Id :=
13060 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
13063 Set_Etype (T_Sub, Corr_Rec);
13064 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13065 Set_Is_Constrained (T_Sub, True);
13066 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13067 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13069 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13070 Set_Discriminant_Constraint
13071 (T_Sub, Discriminant_Constraint (Prot_Subt));
13072 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13073 Create_Constrained_Components
13074 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13077 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13079 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13080 Conditional_Delay (T_Sub, Corr_Rec);
13083 -- This is a component subtype: it will be frozen in the context of
13084 -- the enclosing record's init_proc, so that discriminant references
13085 -- are resolved to discriminals. (Note: we used to skip freezing
13086 -- altogether in that case, which caused errors downstream for
13087 -- components of a bit packed array type).
13089 Set_Has_Delayed_Freeze (T_Sub);
13093 end Constrain_Corresponding_Record;
13095 -----------------------
13096 -- Constrain_Decimal --
13097 -----------------------
13099 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13100 T : constant Entity_Id := Entity (Subtype_Mark (S));
13101 C : constant Node_Id := Constraint (S);
13102 Loc : constant Source_Ptr := Sloc (C);
13103 Range_Expr : Node_Id;
13104 Digits_Expr : Node_Id;
13109 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13111 if Nkind (C) = N_Range_Constraint then
13112 Range_Expr := Range_Expression (C);
13113 Digits_Val := Digits_Value (T);
13116 pragma Assert (Nkind (C) = N_Digits_Constraint);
13118 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13120 Digits_Expr := Digits_Expression (C);
13121 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13123 Check_Digits_Expression (Digits_Expr);
13124 Digits_Val := Expr_Value (Digits_Expr);
13126 if Digits_Val > Digits_Value (T) then
13128 ("digits expression is incompatible with subtype", C);
13129 Digits_Val := Digits_Value (T);
13132 if Present (Range_Constraint (C)) then
13133 Range_Expr := Range_Expression (Range_Constraint (C));
13135 Range_Expr := Empty;
13139 Set_Etype (Def_Id, Base_Type (T));
13140 Set_Size_Info (Def_Id, (T));
13141 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13142 Set_Delta_Value (Def_Id, Delta_Value (T));
13143 Set_Scale_Value (Def_Id, Scale_Value (T));
13144 Set_Small_Value (Def_Id, Small_Value (T));
13145 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13146 Set_Digits_Value (Def_Id, Digits_Val);
13148 -- Manufacture range from given digits value if no range present
13150 if No (Range_Expr) then
13151 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13155 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13157 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13160 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13161 Set_Discrete_RM_Size (Def_Id);
13163 -- Unconditionally delay the freeze, since we cannot set size
13164 -- information in all cases correctly until the freeze point.
13166 Set_Has_Delayed_Freeze (Def_Id);
13167 end Constrain_Decimal;
13169 ----------------------------------
13170 -- Constrain_Discriminated_Type --
13171 ----------------------------------
13173 procedure Constrain_Discriminated_Type
13174 (Def_Id : Entity_Id;
13176 Related_Nod : Node_Id;
13177 For_Access : Boolean := False)
13179 E : Entity_Id := Entity (Subtype_Mark (S));
13182 procedure Fixup_Bad_Constraint;
13183 -- Called after finding a bad constraint, and after having posted an
13184 -- appropriate error message. The goal is to leave type Def_Id in as
13185 -- reasonable state as possible.
13187 --------------------------
13188 -- Fixup_Bad_Constraint --
13189 --------------------------
13191 procedure Fixup_Bad_Constraint is
13193 -- Set a reasonable Ekind for the entity. For an incomplete type,
13194 -- we can't do much, but for other types, we can set the proper
13195 -- corresponding subtype kind.
13197 if Ekind (T) = E_Incomplete_Type then
13198 Set_Ekind (Def_Id, Ekind (T));
13200 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13203 -- Set Etype to the known type, to reduce chances of cascaded errors
13205 Set_Etype (Def_Id, E);
13206 Set_Error_Posted (Def_Id);
13207 end Fixup_Bad_Constraint;
13212 Constr : Elist_Id := New_Elmt_List;
13214 -- Start of processing for Constrain_Discriminated_Type
13217 C := Constraint (S);
13219 -- A discriminant constraint is only allowed in a subtype indication,
13220 -- after a subtype mark. This subtype mark must denote either a type
13221 -- with discriminants, or an access type whose designated type is a
13222 -- type with discriminants. A discriminant constraint specifies the
13223 -- values of these discriminants (RM 3.7.2(5)).
13225 T := Base_Type (Entity (Subtype_Mark (S)));
13227 if Is_Access_Type (T) then
13228 T := Designated_Type (T);
13231 -- In an instance it may be necessary to retrieve the full view of a
13232 -- type with unknown discriminants, or a full view with defaulted
13233 -- discriminants. In other contexts the constraint is illegal.
13236 and then Is_Private_Type (T)
13237 and then Present (Full_View (T))
13239 (Has_Unknown_Discriminants (T)
13241 (not Has_Discriminants (T)
13242 and then Has_Discriminants (Full_View (T))
13243 and then Present (Discriminant_Default_Value
13244 (First_Discriminant (Full_View (T))))))
13246 T := Full_View (T);
13247 E := Full_View (E);
13250 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13251 -- generating an error for access-to-incomplete subtypes.
13253 if Ada_Version >= Ada_2005
13254 and then Ekind (T) = E_Incomplete_Type
13255 and then Nkind (Parent (S)) = N_Subtype_Declaration
13256 and then not Is_Itype (Def_Id)
13258 -- A little sanity check: emit an error message if the type has
13259 -- discriminants to begin with. Type T may be a regular incomplete
13260 -- type or imported via a limited with clause.
13262 if Has_Discriminants (T)
13263 or else (From_Limited_With (T)
13264 and then Present (Non_Limited_View (T))
13265 and then Nkind (Parent (Non_Limited_View (T))) =
13266 N_Full_Type_Declaration
13267 and then Present (Discriminant_Specifications
13268 (Parent (Non_Limited_View (T)))))
13271 ("(Ada 2005) incomplete subtype may not be constrained", C);
13273 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13276 Fixup_Bad_Constraint;
13279 -- Check that the type has visible discriminants. The type may be
13280 -- a private type with unknown discriminants whose full view has
13281 -- discriminants which are invisible.
13283 elsif not Has_Discriminants (T)
13285 (Has_Unknown_Discriminants (T)
13286 and then Is_Private_Type (T))
13288 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13289 Fixup_Bad_Constraint;
13292 elsif Is_Constrained (E)
13293 or else (Ekind (E) = E_Class_Wide_Subtype
13294 and then Present (Discriminant_Constraint (E)))
13296 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13297 Fixup_Bad_Constraint;
13301 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13302 -- applies to the base type.
13304 T := Base_Type (T);
13306 Constr := Build_Discriminant_Constraints (T, S);
13308 -- If the list returned was empty we had an error in building the
13309 -- discriminant constraint. We have also already signalled an error
13310 -- in the incomplete type case
13312 if Is_Empty_Elmt_List (Constr) then
13313 Fixup_Bad_Constraint;
13317 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13318 end Constrain_Discriminated_Type;
13320 ---------------------------
13321 -- Constrain_Enumeration --
13322 ---------------------------
13324 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13325 T : constant Entity_Id := Entity (Subtype_Mark (S));
13326 C : constant Node_Id := Constraint (S);
13329 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13331 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13333 Set_Etype (Def_Id, Base_Type (T));
13334 Set_Size_Info (Def_Id, (T));
13335 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13336 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13338 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13340 Set_Discrete_RM_Size (Def_Id);
13341 end Constrain_Enumeration;
13343 ----------------------
13344 -- Constrain_Float --
13345 ----------------------
13347 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13348 T : constant Entity_Id := Entity (Subtype_Mark (S));
13354 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13356 Set_Etype (Def_Id, Base_Type (T));
13357 Set_Size_Info (Def_Id, (T));
13358 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13360 -- Process the constraint
13362 C := Constraint (S);
13364 -- Digits constraint present
13366 if Nkind (C) = N_Digits_Constraint then
13368 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13369 Check_Restriction (No_Obsolescent_Features, C);
13371 if Warn_On_Obsolescent_Feature then
13373 ("subtype digits constraint is an " &
13374 "obsolescent feature (RM J.3(8))?j?", C);
13377 D := Digits_Expression (C);
13378 Analyze_And_Resolve (D, Any_Integer);
13379 Check_Digits_Expression (D);
13380 Set_Digits_Value (Def_Id, Expr_Value (D));
13382 -- Check that digits value is in range. Obviously we can do this
13383 -- at compile time, but it is strictly a runtime check, and of
13384 -- course there is an ACVC test that checks this.
13386 if Digits_Value (Def_Id) > Digits_Value (T) then
13387 Error_Msg_Uint_1 := Digits_Value (T);
13388 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13390 Make_Raise_Constraint_Error (Sloc (D),
13391 Reason => CE_Range_Check_Failed);
13392 Insert_Action (Declaration_Node (Def_Id), Rais);
13395 C := Range_Constraint (C);
13397 -- No digits constraint present
13400 Set_Digits_Value (Def_Id, Digits_Value (T));
13403 -- Range constraint present
13405 if Nkind (C) = N_Range_Constraint then
13406 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13408 -- No range constraint present
13411 pragma Assert (No (C));
13412 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13415 Set_Is_Constrained (Def_Id);
13416 end Constrain_Float;
13418 ---------------------
13419 -- Constrain_Index --
13420 ---------------------
13422 procedure Constrain_Index
13425 Related_Nod : Node_Id;
13426 Related_Id : Entity_Id;
13427 Suffix : Character;
13428 Suffix_Index : Nat)
13430 Def_Id : Entity_Id;
13431 R : Node_Id := Empty;
13432 T : constant Entity_Id := Etype (Index);
13436 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13437 Set_Etype (Def_Id, Base_Type (T));
13439 if Nkind (S) = N_Range
13441 (Nkind (S) = N_Attribute_Reference
13442 and then Attribute_Name (S) = Name_Range)
13444 -- A Range attribute will be transformed into N_Range by Resolve
13450 Process_Range_Expr_In_Decl (R, T);
13452 if not Error_Posted (S)
13454 (Nkind (S) /= N_Range
13455 or else not Covers (T, (Etype (Low_Bound (S))))
13456 or else not Covers (T, (Etype (High_Bound (S)))))
13458 if Base_Type (T) /= Any_Type
13459 and then Etype (Low_Bound (S)) /= Any_Type
13460 and then Etype (High_Bound (S)) /= Any_Type
13462 Error_Msg_N ("range expected", S);
13466 elsif Nkind (S) = N_Subtype_Indication then
13468 -- The parser has verified that this is a discrete indication
13470 Resolve_Discrete_Subtype_Indication (S, T);
13471 Bad_Predicated_Subtype_Use
13472 ("subtype& has predicate, not allowed in index constraint",
13473 S, Entity (Subtype_Mark (S)));
13475 R := Range_Expression (Constraint (S));
13477 -- Capture values of bounds and generate temporaries for them if
13478 -- needed, since checks may cause duplication of the expressions
13479 -- which must not be reevaluated.
13481 -- The forced evaluation removes side effects from expressions, which
13482 -- should occur also in GNATprove mode. Otherwise, we end up with
13483 -- unexpected insertions of actions at places where this is not
13484 -- supposed to occur, e.g. on default parameters of a call.
13486 if Expander_Active or GNATprove_Mode then
13488 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13490 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13493 elsif Nkind (S) = N_Discriminant_Association then
13495 -- Syntactically valid in subtype indication
13497 Error_Msg_N ("invalid index constraint", S);
13498 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13501 -- Subtype_Mark case, no anonymous subtypes to construct
13506 if Is_Entity_Name (S) then
13507 if not Is_Type (Entity (S)) then
13508 Error_Msg_N ("expect subtype mark for index constraint", S);
13510 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13511 Wrong_Type (S, Base_Type (T));
13513 -- Check error of subtype with predicate in index constraint
13516 Bad_Predicated_Subtype_Use
13517 ("subtype& has predicate, not allowed in index constraint",
13524 Error_Msg_N ("invalid index constraint", S);
13525 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13530 -- Complete construction of the Itype
13532 if Is_Modular_Integer_Type (T) then
13533 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13535 elsif Is_Integer_Type (T) then
13536 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13539 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13540 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13541 Set_First_Literal (Def_Id, First_Literal (T));
13544 Set_Size_Info (Def_Id, (T));
13545 Set_RM_Size (Def_Id, RM_Size (T));
13546 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13548 Set_Scalar_Range (Def_Id, R);
13550 Set_Etype (S, Def_Id);
13551 Set_Discrete_RM_Size (Def_Id);
13552 end Constrain_Index;
13554 -----------------------
13555 -- Constrain_Integer --
13556 -----------------------
13558 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13559 T : constant Entity_Id := Entity (Subtype_Mark (S));
13560 C : constant Node_Id := Constraint (S);
13563 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13565 if Is_Modular_Integer_Type (T) then
13566 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13568 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13571 Set_Etype (Def_Id, Base_Type (T));
13572 Set_Size_Info (Def_Id, (T));
13573 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13574 Set_Discrete_RM_Size (Def_Id);
13575 end Constrain_Integer;
13577 ------------------------------
13578 -- Constrain_Ordinary_Fixed --
13579 ------------------------------
13581 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13582 T : constant Entity_Id := Entity (Subtype_Mark (S));
13588 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13589 Set_Etype (Def_Id, Base_Type (T));
13590 Set_Size_Info (Def_Id, (T));
13591 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13592 Set_Small_Value (Def_Id, Small_Value (T));
13594 -- Process the constraint
13596 C := Constraint (S);
13598 -- Delta constraint present
13600 if Nkind (C) = N_Delta_Constraint then
13602 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13603 Check_Restriction (No_Obsolescent_Features, C);
13605 if Warn_On_Obsolescent_Feature then
13607 ("subtype delta constraint is an " &
13608 "obsolescent feature (RM J.3(7))?j?");
13611 D := Delta_Expression (C);
13612 Analyze_And_Resolve (D, Any_Real);
13613 Check_Delta_Expression (D);
13614 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13616 -- Check that delta value is in range. Obviously we can do this
13617 -- at compile time, but it is strictly a runtime check, and of
13618 -- course there is an ACVC test that checks this.
13620 if Delta_Value (Def_Id) < Delta_Value (T) then
13621 Error_Msg_N ("??delta value is too small", D);
13623 Make_Raise_Constraint_Error (Sloc (D),
13624 Reason => CE_Range_Check_Failed);
13625 Insert_Action (Declaration_Node (Def_Id), Rais);
13628 C := Range_Constraint (C);
13630 -- No delta constraint present
13633 Set_Delta_Value (Def_Id, Delta_Value (T));
13636 -- Range constraint present
13638 if Nkind (C) = N_Range_Constraint then
13639 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13641 -- No range constraint present
13644 pragma Assert (No (C));
13645 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13648 Set_Discrete_RM_Size (Def_Id);
13650 -- Unconditionally delay the freeze, since we cannot set size
13651 -- information in all cases correctly until the freeze point.
13653 Set_Has_Delayed_Freeze (Def_Id);
13654 end Constrain_Ordinary_Fixed;
13656 -----------------------
13657 -- Contain_Interface --
13658 -----------------------
13660 function Contain_Interface
13661 (Iface : Entity_Id;
13662 Ifaces : Elist_Id) return Boolean
13664 Iface_Elmt : Elmt_Id;
13667 if Present (Ifaces) then
13668 Iface_Elmt := First_Elmt (Ifaces);
13669 while Present (Iface_Elmt) loop
13670 if Node (Iface_Elmt) = Iface then
13674 Next_Elmt (Iface_Elmt);
13679 end Contain_Interface;
13681 ---------------------------
13682 -- Convert_Scalar_Bounds --
13683 ---------------------------
13685 procedure Convert_Scalar_Bounds
13687 Parent_Type : Entity_Id;
13688 Derived_Type : Entity_Id;
13691 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13698 -- Defend against previous errors
13700 if No (Scalar_Range (Derived_Type)) then
13701 Check_Error_Detected;
13705 Lo := Build_Scalar_Bound
13706 (Type_Low_Bound (Derived_Type),
13707 Parent_Type, Implicit_Base);
13709 Hi := Build_Scalar_Bound
13710 (Type_High_Bound (Derived_Type),
13711 Parent_Type, Implicit_Base);
13718 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13720 Set_Parent (Rng, N);
13721 Set_Scalar_Range (Derived_Type, Rng);
13723 -- Analyze the bounds
13725 Analyze_And_Resolve (Lo, Implicit_Base);
13726 Analyze_And_Resolve (Hi, Implicit_Base);
13728 -- Analyze the range itself, except that we do not analyze it if
13729 -- the bounds are real literals, and we have a fixed-point type.
13730 -- The reason for this is that we delay setting the bounds in this
13731 -- case till we know the final Small and Size values (see circuit
13732 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13734 if Is_Fixed_Point_Type (Parent_Type)
13735 and then Nkind (Lo) = N_Real_Literal
13736 and then Nkind (Hi) = N_Real_Literal
13740 -- Here we do the analysis of the range
13742 -- Note: we do this manually, since if we do a normal Analyze and
13743 -- Resolve call, there are problems with the conversions used for
13744 -- the derived type range.
13747 Set_Etype (Rng, Implicit_Base);
13748 Set_Analyzed (Rng, True);
13750 end Convert_Scalar_Bounds;
13752 -------------------
13753 -- Copy_And_Swap --
13754 -------------------
13756 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13758 -- Initialize new full declaration entity by copying the pertinent
13759 -- fields of the corresponding private declaration entity.
13761 -- We temporarily set Ekind to a value appropriate for a type to
13762 -- avoid assert failures in Einfo from checking for setting type
13763 -- attributes on something that is not a type. Ekind (Priv) is an
13764 -- appropriate choice, since it allowed the attributes to be set
13765 -- in the first place. This Ekind value will be modified later.
13767 Set_Ekind (Full, Ekind (Priv));
13769 -- Also set Etype temporarily to Any_Type, again, in the absence
13770 -- of errors, it will be properly reset, and if there are errors,
13771 -- then we want a value of Any_Type to remain.
13773 Set_Etype (Full, Any_Type);
13775 -- Now start copying attributes
13777 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13779 if Has_Discriminants (Full) then
13780 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13781 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13784 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13785 Set_Homonym (Full, Homonym (Priv));
13786 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13787 Set_Is_Public (Full, Is_Public (Priv));
13788 Set_Is_Pure (Full, Is_Pure (Priv));
13789 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13790 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13791 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13792 Set_Has_Pragma_Unreferenced_Objects
13793 (Full, Has_Pragma_Unreferenced_Objects
13796 Conditional_Delay (Full, Priv);
13798 if Is_Tagged_Type (Full) then
13799 Set_Direct_Primitive_Operations
13800 (Full, Direct_Primitive_Operations (Priv));
13801 Set_No_Tagged_Streams_Pragma
13802 (Full, No_Tagged_Streams_Pragma (Priv));
13804 if Is_Base_Type (Priv) then
13805 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13809 Set_Is_Volatile (Full, Is_Volatile (Priv));
13810 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13811 Set_Scope (Full, Scope (Priv));
13812 Set_Next_Entity (Full, Next_Entity (Priv));
13813 Set_First_Entity (Full, First_Entity (Priv));
13814 Set_Last_Entity (Full, Last_Entity (Priv));
13816 -- If access types have been recorded for later handling, keep them in
13817 -- the full view so that they get handled when the full view freeze
13818 -- node is expanded.
13820 if Present (Freeze_Node (Priv))
13821 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
13823 Ensure_Freeze_Node (Full);
13824 Set_Access_Types_To_Process
13825 (Freeze_Node (Full),
13826 Access_Types_To_Process (Freeze_Node (Priv)));
13829 -- Swap the two entities. Now Private is the full type entity and Full
13830 -- is the private one. They will be swapped back at the end of the
13831 -- private part. This swapping ensures that the entity that is visible
13832 -- in the private part is the full declaration.
13834 Exchange_Entities (Priv, Full);
13835 Append_Entity (Full, Scope (Full));
13838 -------------------------------------
13839 -- Copy_Array_Base_Type_Attributes --
13840 -------------------------------------
13842 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
13844 Set_Component_Alignment (T1, Component_Alignment (T2));
13845 Set_Component_Type (T1, Component_Type (T2));
13846 Set_Component_Size (T1, Component_Size (T2));
13847 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
13848 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
13849 Propagate_Concurrent_Flags (T1, T2);
13850 Set_Is_Packed (T1, Is_Packed (T2));
13851 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
13852 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
13853 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
13854 end Copy_Array_Base_Type_Attributes;
13856 -----------------------------------
13857 -- Copy_Array_Subtype_Attributes --
13858 -----------------------------------
13860 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
13862 Set_Size_Info (T1, T2);
13864 Set_First_Index (T1, First_Index (T2));
13865 Set_Is_Aliased (T1, Is_Aliased (T2));
13866 Set_Is_Volatile (T1, Is_Volatile (T2));
13867 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
13868 Set_Is_Constrained (T1, Is_Constrained (T2));
13869 Set_Depends_On_Private (T1, Has_Private_Component (T2));
13870 Inherit_Rep_Item_Chain (T1, T2);
13871 Set_Convention (T1, Convention (T2));
13872 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
13873 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
13874 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
13875 end Copy_Array_Subtype_Attributes;
13877 -----------------------------------
13878 -- Create_Constrained_Components --
13879 -----------------------------------
13881 procedure Create_Constrained_Components
13883 Decl_Node : Node_Id;
13885 Constraints : Elist_Id)
13887 Loc : constant Source_Ptr := Sloc (Subt);
13888 Comp_List : constant Elist_Id := New_Elmt_List;
13889 Parent_Type : constant Entity_Id := Etype (Typ);
13890 Assoc_List : constant List_Id := New_List;
13891 Discr_Val : Elmt_Id;
13895 Is_Static : Boolean := True;
13897 procedure Collect_Fixed_Components (Typ : Entity_Id);
13898 -- Collect parent type components that do not appear in a variant part
13900 procedure Create_All_Components;
13901 -- Iterate over Comp_List to create the components of the subtype
13903 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
13904 -- Creates a new component from Old_Compon, copying all the fields from
13905 -- it, including its Etype, inserts the new component in the Subt entity
13906 -- chain and returns the new component.
13908 function Is_Variant_Record (T : Entity_Id) return Boolean;
13909 -- If true, and discriminants are static, collect only components from
13910 -- variants selected by discriminant values.
13912 ------------------------------
13913 -- Collect_Fixed_Components --
13914 ------------------------------
13916 procedure Collect_Fixed_Components (Typ : Entity_Id) is
13918 -- Build association list for discriminants, and find components of the
13919 -- variant part selected by the values of the discriminants.
13921 Old_C := First_Discriminant (Typ);
13922 Discr_Val := First_Elmt (Constraints);
13923 while Present (Old_C) loop
13924 Append_To (Assoc_List,
13925 Make_Component_Association (Loc,
13926 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
13927 Expression => New_Copy (Node (Discr_Val))));
13929 Next_Elmt (Discr_Val);
13930 Next_Discriminant (Old_C);
13933 -- The tag and the possible parent component are unconditionally in
13936 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
13937 Old_C := First_Component (Typ);
13938 while Present (Old_C) loop
13939 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
13940 Append_Elmt (Old_C, Comp_List);
13943 Next_Component (Old_C);
13946 end Collect_Fixed_Components;
13948 ---------------------------
13949 -- Create_All_Components --
13950 ---------------------------
13952 procedure Create_All_Components is
13956 Comp := First_Elmt (Comp_List);
13957 while Present (Comp) loop
13958 Old_C := Node (Comp);
13959 New_C := Create_Component (Old_C);
13963 Constrain_Component_Type
13964 (Old_C, Subt, Decl_Node, Typ, Constraints));
13965 Set_Is_Public (New_C, Is_Public (Subt));
13969 end Create_All_Components;
13971 ----------------------
13972 -- Create_Component --
13973 ----------------------
13975 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
13976 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
13979 if Ekind (Old_Compon) = E_Discriminant
13980 and then Is_Completely_Hidden (Old_Compon)
13982 -- This is a shadow discriminant created for a discriminant of
13983 -- the parent type, which needs to be present in the subtype.
13984 -- Give the shadow discriminant an internal name that cannot
13985 -- conflict with that of visible components.
13987 Set_Chars (New_Compon, New_Internal_Name ('C'));
13990 -- Set the parent so we have a proper link for freezing etc. This is
13991 -- not a real parent pointer, since of course our parent does not own
13992 -- up to us and reference us, we are an illegitimate child of the
13993 -- original parent.
13995 Set_Parent (New_Compon, Parent (Old_Compon));
13997 -- If the old component's Esize was already determined and is a
13998 -- static value, then the new component simply inherits it. Otherwise
13999 -- the old component's size may require run-time determination, but
14000 -- the new component's size still might be statically determinable
14001 -- (if, for example it has a static constraint). In that case we want
14002 -- Layout_Type to recompute the component's size, so we reset its
14003 -- size and positional fields.
14005 if Frontend_Layout_On_Target
14006 and then not Known_Static_Esize (Old_Compon)
14008 Set_Esize (New_Compon, Uint_0);
14009 Init_Normalized_First_Bit (New_Compon);
14010 Init_Normalized_Position (New_Compon);
14011 Init_Normalized_Position_Max (New_Compon);
14014 -- We do not want this node marked as Comes_From_Source, since
14015 -- otherwise it would get first class status and a separate cross-
14016 -- reference line would be generated. Illegitimate children do not
14017 -- rate such recognition.
14019 Set_Comes_From_Source (New_Compon, False);
14021 -- But it is a real entity, and a birth certificate must be properly
14022 -- registered by entering it into the entity list.
14024 Enter_Name (New_Compon);
14027 end Create_Component;
14029 -----------------------
14030 -- Is_Variant_Record --
14031 -----------------------
14033 function Is_Variant_Record (T : Entity_Id) return Boolean is
14035 return Nkind (Parent (T)) = N_Full_Type_Declaration
14036 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14037 and then Present (Component_List (Type_Definition (Parent (T))))
14040 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14041 end Is_Variant_Record;
14043 -- Start of processing for Create_Constrained_Components
14046 pragma Assert (Subt /= Base_Type (Subt));
14047 pragma Assert (Typ = Base_Type (Typ));
14049 Set_First_Entity (Subt, Empty);
14050 Set_Last_Entity (Subt, Empty);
14052 -- Check whether constraint is fully static, in which case we can
14053 -- optimize the list of components.
14055 Discr_Val := First_Elmt (Constraints);
14056 while Present (Discr_Val) loop
14057 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14058 Is_Static := False;
14062 Next_Elmt (Discr_Val);
14065 Set_Has_Static_Discriminants (Subt, Is_Static);
14069 -- Inherit the discriminants of the parent type
14071 Add_Discriminants : declare
14077 Old_C := First_Discriminant (Typ);
14079 while Present (Old_C) loop
14080 Num_Disc := Num_Disc + 1;
14081 New_C := Create_Component (Old_C);
14082 Set_Is_Public (New_C, Is_Public (Subt));
14083 Next_Discriminant (Old_C);
14086 -- For an untagged derived subtype, the number of discriminants may
14087 -- be smaller than the number of inherited discriminants, because
14088 -- several of them may be renamed by a single new discriminant or
14089 -- constrained. In this case, add the hidden discriminants back into
14090 -- the subtype, because they need to be present if the optimizer of
14091 -- the GCC 4.x back-end decides to break apart assignments between
14092 -- objects using the parent view into member-wise assignments.
14096 if Is_Derived_Type (Typ)
14097 and then not Is_Tagged_Type (Typ)
14099 Old_C := First_Stored_Discriminant (Typ);
14101 while Present (Old_C) loop
14102 Num_Gird := Num_Gird + 1;
14103 Next_Stored_Discriminant (Old_C);
14107 if Num_Gird > Num_Disc then
14109 -- Find out multiple uses of new discriminants, and add hidden
14110 -- components for the extra renamed discriminants. We recognize
14111 -- multiple uses through the Corresponding_Discriminant of a
14112 -- new discriminant: if it constrains several old discriminants,
14113 -- this field points to the last one in the parent type. The
14114 -- stored discriminants of the derived type have the same name
14115 -- as those of the parent.
14119 New_Discr : Entity_Id;
14120 Old_Discr : Entity_Id;
14123 Constr := First_Elmt (Stored_Constraint (Typ));
14124 Old_Discr := First_Stored_Discriminant (Typ);
14125 while Present (Constr) loop
14126 if Is_Entity_Name (Node (Constr))
14127 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14129 New_Discr := Entity (Node (Constr));
14131 if Chars (Corresponding_Discriminant (New_Discr)) /=
14134 -- The new discriminant has been used to rename a
14135 -- subsequent old discriminant. Introduce a shadow
14136 -- component for the current old discriminant.
14138 New_C := Create_Component (Old_Discr);
14139 Set_Original_Record_Component (New_C, Old_Discr);
14143 -- The constraint has eliminated the old discriminant.
14144 -- Introduce a shadow component.
14146 New_C := Create_Component (Old_Discr);
14147 Set_Original_Record_Component (New_C, Old_Discr);
14150 Next_Elmt (Constr);
14151 Next_Stored_Discriminant (Old_Discr);
14155 end Add_Discriminants;
14158 and then Is_Variant_Record (Typ)
14160 Collect_Fixed_Components (Typ);
14162 Gather_Components (
14164 Component_List (Type_Definition (Parent (Typ))),
14165 Governed_By => Assoc_List,
14167 Report_Errors => Errors);
14168 pragma Assert (not Errors
14169 or else Serious_Errors_Detected > 0);
14171 Create_All_Components;
14173 -- If the subtype declaration is created for a tagged type derivation
14174 -- with constraints, we retrieve the record definition of the parent
14175 -- type to select the components of the proper variant.
14178 and then Is_Tagged_Type (Typ)
14179 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14181 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14182 and then Is_Variant_Record (Parent_Type)
14184 Collect_Fixed_Components (Typ);
14188 Component_List (Type_Definition (Parent (Parent_Type))),
14189 Governed_By => Assoc_List,
14191 Report_Errors => Errors);
14193 -- Note: previously there was a check at this point that no errors
14194 -- were detected. As a consequence of AI05-220 there may be an error
14195 -- if an inherited discriminant that controls a variant has a non-
14196 -- static constraint.
14198 -- If the tagged derivation has a type extension, collect all the
14199 -- new components therein.
14201 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14203 Old_C := First_Component (Typ);
14204 while Present (Old_C) loop
14205 if Original_Record_Component (Old_C) = Old_C
14206 and then Chars (Old_C) /= Name_uTag
14207 and then Chars (Old_C) /= Name_uParent
14209 Append_Elmt (Old_C, Comp_List);
14212 Next_Component (Old_C);
14216 Create_All_Components;
14219 -- If discriminants are not static, or if this is a multi-level type
14220 -- extension, we have to include all components of the parent type.
14222 Old_C := First_Component (Typ);
14223 while Present (Old_C) loop
14224 New_C := Create_Component (Old_C);
14228 Constrain_Component_Type
14229 (Old_C, Subt, Decl_Node, Typ, Constraints));
14230 Set_Is_Public (New_C, Is_Public (Subt));
14232 Next_Component (Old_C);
14237 end Create_Constrained_Components;
14239 ------------------------------------------
14240 -- Decimal_Fixed_Point_Type_Declaration --
14241 ------------------------------------------
14243 procedure Decimal_Fixed_Point_Type_Declaration
14247 Loc : constant Source_Ptr := Sloc (Def);
14248 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14249 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14250 Implicit_Base : Entity_Id;
14257 Check_SPARK_05_Restriction
14258 ("decimal fixed point type is not allowed", Def);
14259 Check_Restriction (No_Fixed_Point, Def);
14261 -- Create implicit base type
14264 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14265 Set_Etype (Implicit_Base, Implicit_Base);
14267 -- Analyze and process delta expression
14269 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14271 Check_Delta_Expression (Delta_Expr);
14272 Delta_Val := Expr_Value_R (Delta_Expr);
14274 -- Check delta is power of 10, and determine scale value from it
14280 Scale_Val := Uint_0;
14283 if Val < Ureal_1 then
14284 while Val < Ureal_1 loop
14285 Val := Val * Ureal_10;
14286 Scale_Val := Scale_Val + 1;
14289 if Scale_Val > 18 then
14290 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14291 Scale_Val := UI_From_Int (+18);
14295 while Val > Ureal_1 loop
14296 Val := Val / Ureal_10;
14297 Scale_Val := Scale_Val - 1;
14300 if Scale_Val < -18 then
14301 Error_Msg_N ("scale is less than minimum value of -18", Def);
14302 Scale_Val := UI_From_Int (-18);
14306 if Val /= Ureal_1 then
14307 Error_Msg_N ("delta expression must be a power of 10", Def);
14308 Delta_Val := Ureal_10 ** (-Scale_Val);
14312 -- Set delta, scale and small (small = delta for decimal type)
14314 Set_Delta_Value (Implicit_Base, Delta_Val);
14315 Set_Scale_Value (Implicit_Base, Scale_Val);
14316 Set_Small_Value (Implicit_Base, Delta_Val);
14318 -- Analyze and process digits expression
14320 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14321 Check_Digits_Expression (Digs_Expr);
14322 Digs_Val := Expr_Value (Digs_Expr);
14324 if Digs_Val > 18 then
14325 Digs_Val := UI_From_Int (+18);
14326 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14329 Set_Digits_Value (Implicit_Base, Digs_Val);
14330 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14332 -- Set range of base type from digits value for now. This will be
14333 -- expanded to represent the true underlying base range by Freeze.
14335 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14337 -- Note: We leave size as zero for now, size will be set at freeze
14338 -- time. We have to do this for ordinary fixed-point, because the size
14339 -- depends on the specified small, and we might as well do the same for
14340 -- decimal fixed-point.
14342 pragma Assert (Esize (Implicit_Base) = Uint_0);
14344 -- If there are bounds given in the declaration use them as the
14345 -- bounds of the first named subtype.
14347 if Present (Real_Range_Specification (Def)) then
14349 RRS : constant Node_Id := Real_Range_Specification (Def);
14350 Low : constant Node_Id := Low_Bound (RRS);
14351 High : constant Node_Id := High_Bound (RRS);
14356 Analyze_And_Resolve (Low, Any_Real);
14357 Analyze_And_Resolve (High, Any_Real);
14358 Check_Real_Bound (Low);
14359 Check_Real_Bound (High);
14360 Low_Val := Expr_Value_R (Low);
14361 High_Val := Expr_Value_R (High);
14363 if Low_Val < (-Bound_Val) then
14365 ("range low bound too small for digits value", Low);
14366 Low_Val := -Bound_Val;
14369 if High_Val > Bound_Val then
14371 ("range high bound too large for digits value", High);
14372 High_Val := Bound_Val;
14375 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14378 -- If no explicit range, use range that corresponds to given
14379 -- digits value. This will end up as the final range for the
14383 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14386 -- Complete entity for first subtype. The inheritance of the rep item
14387 -- chain ensures that SPARK-related pragmas are not clobbered when the
14388 -- decimal fixed point type acts as a full view of a private type.
14390 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14391 Set_Etype (T, Implicit_Base);
14392 Set_Size_Info (T, Implicit_Base);
14393 Inherit_Rep_Item_Chain (T, Implicit_Base);
14394 Set_Digits_Value (T, Digs_Val);
14395 Set_Delta_Value (T, Delta_Val);
14396 Set_Small_Value (T, Delta_Val);
14397 Set_Scale_Value (T, Scale_Val);
14398 Set_Is_Constrained (T);
14399 end Decimal_Fixed_Point_Type_Declaration;
14401 -----------------------------------
14402 -- Derive_Progenitor_Subprograms --
14403 -----------------------------------
14405 procedure Derive_Progenitor_Subprograms
14406 (Parent_Type : Entity_Id;
14407 Tagged_Type : Entity_Id)
14412 Iface_Elmt : Elmt_Id;
14413 Iface_Subp : Entity_Id;
14414 New_Subp : Entity_Id := Empty;
14415 Prim_Elmt : Elmt_Id;
14420 pragma Assert (Ada_Version >= Ada_2005
14421 and then Is_Record_Type (Tagged_Type)
14422 and then Is_Tagged_Type (Tagged_Type)
14423 and then Has_Interfaces (Tagged_Type));
14425 -- Step 1: Transfer to the full-view primitives associated with the
14426 -- partial-view that cover interface primitives. Conceptually this
14427 -- work should be done later by Process_Full_View; done here to
14428 -- simplify its implementation at later stages. It can be safely
14429 -- done here because interfaces must be visible in the partial and
14430 -- private view (RM 7.3(7.3/2)).
14432 -- Small optimization: This work is only required if the parent may
14433 -- have entities whose Alias attribute reference an interface primitive.
14434 -- Such a situation may occur if the parent is an abstract type and the
14435 -- primitive has not been yet overridden or if the parent is a generic
14436 -- formal type covering interfaces.
14438 -- If the tagged type is not abstract, it cannot have abstract
14439 -- primitives (the only entities in the list of primitives of
14440 -- non-abstract tagged types that can reference abstract primitives
14441 -- through its Alias attribute are the internal entities that have
14442 -- attribute Interface_Alias, and these entities are generated later
14443 -- by Add_Internal_Interface_Entities).
14445 if In_Private_Part (Current_Scope)
14446 and then (Is_Abstract_Type (Parent_Type)
14448 Is_Generic_Type (Parent_Type))
14450 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14451 while Present (Elmt) loop
14452 Subp := Node (Elmt);
14454 -- At this stage it is not possible to have entities in the list
14455 -- of primitives that have attribute Interface_Alias.
14457 pragma Assert (No (Interface_Alias (Subp)));
14459 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14461 if Is_Interface (Typ) then
14462 E := Find_Primitive_Covering_Interface
14463 (Tagged_Type => Tagged_Type,
14464 Iface_Prim => Subp);
14467 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14469 Replace_Elmt (Elmt, E);
14470 Remove_Homonym (Subp);
14478 -- Step 2: Add primitives of progenitors that are not implemented by
14479 -- parents of Tagged_Type.
14481 if Present (Interfaces (Base_Type (Tagged_Type))) then
14482 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14483 while Present (Iface_Elmt) loop
14484 Iface := Node (Iface_Elmt);
14486 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14487 while Present (Prim_Elmt) loop
14488 Iface_Subp := Node (Prim_Elmt);
14490 -- Exclude derivation of predefined primitives except those
14491 -- that come from source, or are inherited from one that comes
14492 -- from source. Required to catch declarations of equality
14493 -- operators of interfaces. For example:
14495 -- type Iface is interface;
14496 -- function "=" (Left, Right : Iface) return Boolean;
14498 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14499 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14501 E := Find_Primitive_Covering_Interface
14502 (Tagged_Type => Tagged_Type,
14503 Iface_Prim => Iface_Subp);
14505 -- If not found we derive a new primitive leaving its alias
14506 -- attribute referencing the interface primitive.
14510 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14512 -- Ada 2012 (AI05-0197): If the covering primitive's name
14513 -- differs from the name of the interface primitive then it
14514 -- is a private primitive inherited from a parent type. In
14515 -- such case, given that Tagged_Type covers the interface,
14516 -- the inherited private primitive becomes visible. For such
14517 -- purpose we add a new entity that renames the inherited
14518 -- private primitive.
14520 elsif Chars (E) /= Chars (Iface_Subp) then
14521 pragma Assert (Has_Suffix (E, 'P'));
14523 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14524 Set_Alias (New_Subp, E);
14525 Set_Is_Abstract_Subprogram (New_Subp,
14526 Is_Abstract_Subprogram (E));
14528 -- Propagate to the full view interface entities associated
14529 -- with the partial view.
14531 elsif In_Private_Part (Current_Scope)
14532 and then Present (Alias (E))
14533 and then Alias (E) = Iface_Subp
14535 List_Containing (Parent (E)) /=
14536 Private_Declarations
14538 (Unit_Declaration_Node (Current_Scope)))
14540 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14544 Next_Elmt (Prim_Elmt);
14547 Next_Elmt (Iface_Elmt);
14550 end Derive_Progenitor_Subprograms;
14552 -----------------------
14553 -- Derive_Subprogram --
14554 -----------------------
14556 procedure Derive_Subprogram
14557 (New_Subp : out Entity_Id;
14558 Parent_Subp : Entity_Id;
14559 Derived_Type : Entity_Id;
14560 Parent_Type : Entity_Id;
14561 Actual_Subp : Entity_Id := Empty)
14563 Formal : Entity_Id;
14564 -- Formal parameter of parent primitive operation
14566 Formal_Of_Actual : Entity_Id;
14567 -- Formal parameter of actual operation, when the derivation is to
14568 -- create a renaming for a primitive operation of an actual in an
14571 New_Formal : Entity_Id;
14572 -- Formal of inherited operation
14574 Visible_Subp : Entity_Id := Parent_Subp;
14576 function Is_Private_Overriding return Boolean;
14577 -- If Subp is a private overriding of a visible operation, the inherited
14578 -- operation derives from the overridden op (even though its body is the
14579 -- overriding one) and the inherited operation is visible now. See
14580 -- sem_disp to see the full details of the handling of the overridden
14581 -- subprogram, which is removed from the list of primitive operations of
14582 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14583 -- and used to diagnose abstract operations that need overriding in the
14586 procedure Replace_Type (Id, New_Id : Entity_Id);
14587 -- When the type is an anonymous access type, create a new access type
14588 -- designating the derived type.
14590 procedure Set_Derived_Name;
14591 -- This procedure sets the appropriate Chars name for New_Subp. This
14592 -- is normally just a copy of the parent name. An exception arises for
14593 -- type support subprograms, where the name is changed to reflect the
14594 -- name of the derived type, e.g. if type foo is derived from type bar,
14595 -- then a procedure barDA is derived with a name fooDA.
14597 ---------------------------
14598 -- Is_Private_Overriding --
14599 ---------------------------
14601 function Is_Private_Overriding return Boolean is
14605 -- If the parent is not a dispatching operation there is no
14606 -- need to investigate overridings
14608 if not Is_Dispatching_Operation (Parent_Subp) then
14612 -- The visible operation that is overridden is a homonym of the
14613 -- parent subprogram. We scan the homonym chain to find the one
14614 -- whose alias is the subprogram we are deriving.
14616 Prev := Current_Entity (Parent_Subp);
14617 while Present (Prev) loop
14618 if Ekind (Prev) = Ekind (Parent_Subp)
14619 and then Alias (Prev) = Parent_Subp
14620 and then Scope (Parent_Subp) = Scope (Prev)
14621 and then not Is_Hidden (Prev)
14623 Visible_Subp := Prev;
14627 Prev := Homonym (Prev);
14631 end Is_Private_Overriding;
14637 procedure Replace_Type (Id, New_Id : Entity_Id) is
14638 Id_Type : constant Entity_Id := Etype (Id);
14639 Acc_Type : Entity_Id;
14640 Par : constant Node_Id := Parent (Derived_Type);
14643 -- When the type is an anonymous access type, create a new access
14644 -- type designating the derived type. This itype must be elaborated
14645 -- at the point of the derivation, not on subsequent calls that may
14646 -- be out of the proper scope for Gigi, so we insert a reference to
14647 -- it after the derivation.
14649 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14651 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14654 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14655 and then Present (Full_View (Desig_Typ))
14656 and then not Is_Private_Type (Parent_Type)
14658 Desig_Typ := Full_View (Desig_Typ);
14661 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14663 -- Ada 2005 (AI-251): Handle also derivations of abstract
14664 -- interface primitives.
14666 or else (Is_Interface (Desig_Typ)
14667 and then not Is_Class_Wide_Type (Desig_Typ))
14669 Acc_Type := New_Copy (Id_Type);
14670 Set_Etype (Acc_Type, Acc_Type);
14671 Set_Scope (Acc_Type, New_Subp);
14673 -- Set size of anonymous access type. If we have an access
14674 -- to an unconstrained array, this is a fat pointer, so it
14675 -- is sizes at twice addtress size.
14677 if Is_Array_Type (Desig_Typ)
14678 and then not Is_Constrained (Desig_Typ)
14680 Init_Size (Acc_Type, 2 * System_Address_Size);
14682 -- Other cases use a thin pointer
14685 Init_Size (Acc_Type, System_Address_Size);
14688 -- Set remaining characterstics of anonymous access type
14690 Init_Alignment (Acc_Type);
14691 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14693 Set_Etype (New_Id, Acc_Type);
14694 Set_Scope (New_Id, New_Subp);
14696 -- Create a reference to it
14698 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14701 Set_Etype (New_Id, Id_Type);
14705 -- In Ada2012, a formal may have an incomplete type but the type
14706 -- derivation that inherits the primitive follows the full view.
14708 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14710 (Ekind (Id_Type) = E_Record_Type_With_Private
14711 and then Present (Full_View (Id_Type))
14713 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14715 (Ada_Version >= Ada_2012
14716 and then Ekind (Id_Type) = E_Incomplete_Type
14717 and then Full_View (Id_Type) = Parent_Type)
14719 -- Constraint checks on formals are generated during expansion,
14720 -- based on the signature of the original subprogram. The bounds
14721 -- of the derived type are not relevant, and thus we can use
14722 -- the base type for the formals. However, the return type may be
14723 -- used in a context that requires that the proper static bounds
14724 -- be used (a case statement, for example) and for those cases
14725 -- we must use the derived type (first subtype), not its base.
14727 -- If the derived_type_definition has no constraints, we know that
14728 -- the derived type has the same constraints as the first subtype
14729 -- of the parent, and we can also use it rather than its base,
14730 -- which can lead to more efficient code.
14732 if Etype (Id) = Parent_Type then
14733 if Is_Scalar_Type (Parent_Type)
14735 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14737 Set_Etype (New_Id, Derived_Type);
14739 elsif Nkind (Par) = N_Full_Type_Declaration
14741 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14744 (Subtype_Indication (Type_Definition (Par)))
14746 Set_Etype (New_Id, Derived_Type);
14749 Set_Etype (New_Id, Base_Type (Derived_Type));
14753 Set_Etype (New_Id, Base_Type (Derived_Type));
14757 Set_Etype (New_Id, Etype (Id));
14761 ----------------------
14762 -- Set_Derived_Name --
14763 ----------------------
14765 procedure Set_Derived_Name is
14766 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14768 if Nm = TSS_Null then
14769 Set_Chars (New_Subp, Chars (Parent_Subp));
14771 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14773 end Set_Derived_Name;
14775 -- Start of processing for Derive_Subprogram
14778 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14779 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14781 -- Check whether the inherited subprogram is a private operation that
14782 -- should be inherited but not yet made visible. Such subprograms can
14783 -- become visible at a later point (e.g., the private part of a public
14784 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14785 -- following predicate is true, then this is not such a private
14786 -- operation and the subprogram simply inherits the name of the parent
14787 -- subprogram. Note the special check for the names of controlled
14788 -- operations, which are currently exempted from being inherited with
14789 -- a hidden name because they must be findable for generation of
14790 -- implicit run-time calls.
14792 if not Is_Hidden (Parent_Subp)
14793 or else Is_Internal (Parent_Subp)
14794 or else Is_Private_Overriding
14795 or else Is_Internal_Name (Chars (Parent_Subp))
14796 or else (Is_Controlled (Parent_Type)
14797 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
14803 -- An inherited dispatching equality will be overridden by an internally
14804 -- generated one, or by an explicit one, so preserve its name and thus
14805 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14806 -- private operation it may become invisible if the full view has
14807 -- progenitors, and the dispatch table will be malformed.
14808 -- We check that the type is limited to handle the anomalous declaration
14809 -- of Limited_Controlled, which is derived from a non-limited type, and
14810 -- which is handled specially elsewhere as well.
14812 elsif Chars (Parent_Subp) = Name_Op_Eq
14813 and then Is_Dispatching_Operation (Parent_Subp)
14814 and then Etype (Parent_Subp) = Standard_Boolean
14815 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
14817 Etype (First_Formal (Parent_Subp)) =
14818 Etype (Next_Formal (First_Formal (Parent_Subp)))
14822 -- If parent is hidden, this can be a regular derivation if the
14823 -- parent is immediately visible in a non-instantiating context,
14824 -- or if we are in the private part of an instance. This test
14825 -- should still be refined ???
14827 -- The test for In_Instance_Not_Visible avoids inheriting the derived
14828 -- operation as a non-visible operation in cases where the parent
14829 -- subprogram might not be visible now, but was visible within the
14830 -- original generic, so it would be wrong to make the inherited
14831 -- subprogram non-visible now. (Not clear if this test is fully
14832 -- correct; are there any cases where we should declare the inherited
14833 -- operation as not visible to avoid it being overridden, e.g., when
14834 -- the parent type is a generic actual with private primitives ???)
14836 -- (they should be treated the same as other private inherited
14837 -- subprograms, but it's not clear how to do this cleanly). ???
14839 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14840 and then Is_Immediately_Visible (Parent_Subp)
14841 and then not In_Instance)
14842 or else In_Instance_Not_Visible
14846 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
14847 -- overrides an interface primitive because interface primitives
14848 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
14850 elsif Ada_Version >= Ada_2005
14851 and then Is_Dispatching_Operation (Parent_Subp)
14852 and then Covers_Some_Interface (Parent_Subp)
14856 -- Otherwise, the type is inheriting a private operation, so enter it
14857 -- with a special name so it can't be overridden.
14860 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
14863 Set_Parent (New_Subp, Parent (Derived_Type));
14865 if Present (Actual_Subp) then
14866 Replace_Type (Actual_Subp, New_Subp);
14868 Replace_Type (Parent_Subp, New_Subp);
14871 Conditional_Delay (New_Subp, Parent_Subp);
14873 -- If we are creating a renaming for a primitive operation of an
14874 -- actual of a generic derived type, we must examine the signature
14875 -- of the actual primitive, not that of the generic formal, which for
14876 -- example may be an interface. However the name and initial value
14877 -- of the inherited operation are those of the formal primitive.
14879 Formal := First_Formal (Parent_Subp);
14881 if Present (Actual_Subp) then
14882 Formal_Of_Actual := First_Formal (Actual_Subp);
14884 Formal_Of_Actual := Empty;
14887 while Present (Formal) loop
14888 New_Formal := New_Copy (Formal);
14890 -- Normally we do not go copying parents, but in the case of
14891 -- formals, we need to link up to the declaration (which is the
14892 -- parameter specification), and it is fine to link up to the
14893 -- original formal's parameter specification in this case.
14895 Set_Parent (New_Formal, Parent (Formal));
14896 Append_Entity (New_Formal, New_Subp);
14898 if Present (Formal_Of_Actual) then
14899 Replace_Type (Formal_Of_Actual, New_Formal);
14900 Next_Formal (Formal_Of_Actual);
14902 Replace_Type (Formal, New_Formal);
14905 Next_Formal (Formal);
14908 -- If this derivation corresponds to a tagged generic actual, then
14909 -- primitive operations rename those of the actual. Otherwise the
14910 -- primitive operations rename those of the parent type, If the parent
14911 -- renames an intrinsic operator, so does the new subprogram. We except
14912 -- concatenation, which is always properly typed, and does not get
14913 -- expanded as other intrinsic operations.
14915 if No (Actual_Subp) then
14916 if Is_Intrinsic_Subprogram (Parent_Subp) then
14917 Set_Is_Intrinsic_Subprogram (New_Subp);
14919 if Present (Alias (Parent_Subp))
14920 and then Chars (Parent_Subp) /= Name_Op_Concat
14922 Set_Alias (New_Subp, Alias (Parent_Subp));
14924 Set_Alias (New_Subp, Parent_Subp);
14928 Set_Alias (New_Subp, Parent_Subp);
14932 Set_Alias (New_Subp, Actual_Subp);
14935 -- Inherit the "ghostness" from the parent subprogram
14937 if Is_Ghost_Entity (Alias (New_Subp)) then
14938 Set_Is_Ghost_Entity (New_Subp);
14941 -- Derived subprograms of a tagged type must inherit the convention
14942 -- of the parent subprogram (a requirement of AI-117). Derived
14943 -- subprograms of untagged types simply get convention Ada by default.
14945 -- If the derived type is a tagged generic formal type with unknown
14946 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
14948 -- However, if the type is derived from a generic formal, the further
14949 -- inherited subprogram has the convention of the non-generic ancestor.
14950 -- Otherwise there would be no way to override the operation.
14951 -- (This is subject to forthcoming ARG discussions).
14953 if Is_Tagged_Type (Derived_Type) then
14954 if Is_Generic_Type (Derived_Type)
14955 and then Has_Unknown_Discriminants (Derived_Type)
14957 Set_Convention (New_Subp, Convention_Intrinsic);
14960 if Is_Generic_Type (Parent_Type)
14961 and then Has_Unknown_Discriminants (Parent_Type)
14963 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
14965 Set_Convention (New_Subp, Convention (Parent_Subp));
14970 -- Predefined controlled operations retain their name even if the parent
14971 -- is hidden (see above), but they are not primitive operations if the
14972 -- ancestor is not visible, for example if the parent is a private
14973 -- extension completed with a controlled extension. Note that a full
14974 -- type that is controlled can break privacy: the flag Is_Controlled is
14975 -- set on both views of the type.
14977 if Is_Controlled (Parent_Type)
14978 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
14981 and then Is_Hidden (Parent_Subp)
14982 and then not Is_Visibly_Controlled (Parent_Type)
14984 Set_Is_Hidden (New_Subp);
14987 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
14988 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
14990 if Ekind (Parent_Subp) = E_Procedure then
14991 Set_Is_Valued_Procedure
14992 (New_Subp, Is_Valued_Procedure (Parent_Subp));
14994 Set_Has_Controlling_Result
14995 (New_Subp, Has_Controlling_Result (Parent_Subp));
14998 -- No_Return must be inherited properly. If this is overridden in the
14999 -- case of a dispatching operation, then a check is made in Sem_Disp
15000 -- that the overriding operation is also No_Return (no such check is
15001 -- required for the case of non-dispatching operation.
15003 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15005 -- A derived function with a controlling result is abstract. If the
15006 -- Derived_Type is a nonabstract formal generic derived type, then
15007 -- inherited operations are not abstract: the required check is done at
15008 -- instantiation time. If the derivation is for a generic actual, the
15009 -- function is not abstract unless the actual is.
15011 if Is_Generic_Type (Derived_Type)
15012 and then not Is_Abstract_Type (Derived_Type)
15016 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15017 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15019 -- A subprogram subject to pragma Extensions_Visible with value False
15020 -- requires overriding if the subprogram has at least one controlling
15021 -- OUT parameter (SPARK RM 6.1.7(6)).
15023 elsif Ada_Version >= Ada_2005
15024 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15025 or else (Is_Tagged_Type (Derived_Type)
15026 and then Etype (New_Subp) = Derived_Type
15027 and then not Is_Null_Extension (Derived_Type))
15028 or else (Is_Tagged_Type (Derived_Type)
15029 and then Ekind (Etype (New_Subp)) =
15030 E_Anonymous_Access_Type
15031 and then Designated_Type (Etype (New_Subp)) =
15033 and then not Is_Null_Extension (Derived_Type))
15034 or else (Comes_From_Source (Alias (New_Subp))
15035 and then Is_EVF_Procedure (Alias (New_Subp))))
15036 and then No (Actual_Subp)
15038 if not Is_Tagged_Type (Derived_Type)
15039 or else Is_Abstract_Type (Derived_Type)
15040 or else Is_Abstract_Subprogram (Alias (New_Subp))
15042 Set_Is_Abstract_Subprogram (New_Subp);
15044 Set_Requires_Overriding (New_Subp);
15047 elsif Ada_Version < Ada_2005
15048 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15049 or else (Is_Tagged_Type (Derived_Type)
15050 and then Etype (New_Subp) = Derived_Type
15051 and then No (Actual_Subp)))
15053 Set_Is_Abstract_Subprogram (New_Subp);
15055 -- AI05-0097 : an inherited operation that dispatches on result is
15056 -- abstract if the derived type is abstract, even if the parent type
15057 -- is concrete and the derived type is a null extension.
15059 elsif Has_Controlling_Result (Alias (New_Subp))
15060 and then Is_Abstract_Type (Etype (New_Subp))
15062 Set_Is_Abstract_Subprogram (New_Subp);
15064 -- Finally, if the parent type is abstract we must verify that all
15065 -- inherited operations are either non-abstract or overridden, or that
15066 -- the derived type itself is abstract (this check is performed at the
15067 -- end of a package declaration, in Check_Abstract_Overriding). A
15068 -- private overriding in the parent type will not be visible in the
15069 -- derivation if we are not in an inner package or in a child unit of
15070 -- the parent type, in which case the abstractness of the inherited
15071 -- operation is carried to the new subprogram.
15073 elsif Is_Abstract_Type (Parent_Type)
15074 and then not In_Open_Scopes (Scope (Parent_Type))
15075 and then Is_Private_Overriding
15076 and then Is_Abstract_Subprogram (Visible_Subp)
15078 if No (Actual_Subp) then
15079 Set_Alias (New_Subp, Visible_Subp);
15080 Set_Is_Abstract_Subprogram (New_Subp, True);
15083 -- If this is a derivation for an instance of a formal derived
15084 -- type, abstractness comes from the primitive operation of the
15085 -- actual, not from the operation inherited from the ancestor.
15087 Set_Is_Abstract_Subprogram
15088 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15092 New_Overloaded_Entity (New_Subp, Derived_Type);
15094 -- Check for case of a derived subprogram for the instantiation of a
15095 -- formal derived tagged type, if so mark the subprogram as dispatching
15096 -- and inherit the dispatching attributes of the actual subprogram. The
15097 -- derived subprogram is effectively renaming of the actual subprogram,
15098 -- so it needs to have the same attributes as the actual.
15100 if Present (Actual_Subp)
15101 and then Is_Dispatching_Operation (Actual_Subp)
15103 Set_Is_Dispatching_Operation (New_Subp);
15105 if Present (DTC_Entity (Actual_Subp)) then
15106 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15107 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15111 -- Indicate that a derived subprogram does not require a body and that
15112 -- it does not require processing of default expressions.
15114 Set_Has_Completion (New_Subp);
15115 Set_Default_Expressions_Processed (New_Subp);
15117 if Ekind (New_Subp) = E_Function then
15118 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15120 end Derive_Subprogram;
15122 ------------------------
15123 -- Derive_Subprograms --
15124 ------------------------
15126 procedure Derive_Subprograms
15127 (Parent_Type : Entity_Id;
15128 Derived_Type : Entity_Id;
15129 Generic_Actual : Entity_Id := Empty)
15131 Op_List : constant Elist_Id :=
15132 Collect_Primitive_Operations (Parent_Type);
15134 function Check_Derived_Type return Boolean;
15135 -- Check that all the entities derived from Parent_Type are found in
15136 -- the list of primitives of Derived_Type exactly in the same order.
15138 procedure Derive_Interface_Subprogram
15139 (New_Subp : out Entity_Id;
15141 Actual_Subp : Entity_Id);
15142 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15143 -- (which is an interface primitive). If Generic_Actual is present then
15144 -- Actual_Subp is the actual subprogram corresponding with the generic
15145 -- subprogram Subp.
15147 ------------------------
15148 -- Check_Derived_Type --
15149 ------------------------
15151 function Check_Derived_Type return Boolean is
15155 New_Subp : Entity_Id;
15160 -- Traverse list of entities in the current scope searching for
15161 -- an incomplete type whose full-view is derived type.
15163 E := First_Entity (Scope (Derived_Type));
15164 while Present (E) and then E /= Derived_Type loop
15165 if Ekind (E) = E_Incomplete_Type
15166 and then Present (Full_View (E))
15167 and then Full_View (E) = Derived_Type
15169 -- Disable this test if Derived_Type completes an incomplete
15170 -- type because in such case more primitives can be added
15171 -- later to the list of primitives of Derived_Type by routine
15172 -- Process_Incomplete_Dependents
15177 E := Next_Entity (E);
15180 List := Collect_Primitive_Operations (Derived_Type);
15181 Elmt := First_Elmt (List);
15183 Op_Elmt := First_Elmt (Op_List);
15184 while Present (Op_Elmt) loop
15185 Subp := Node (Op_Elmt);
15186 New_Subp := Node (Elmt);
15188 -- At this early stage Derived_Type has no entities with attribute
15189 -- Interface_Alias. In addition, such primitives are always
15190 -- located at the end of the list of primitives of Parent_Type.
15191 -- Therefore, if found we can safely stop processing pending
15194 exit when Present (Interface_Alias (Subp));
15196 -- Handle hidden entities
15198 if not Is_Predefined_Dispatching_Operation (Subp)
15199 and then Is_Hidden (Subp)
15201 if Present (New_Subp)
15202 and then Primitive_Names_Match (Subp, New_Subp)
15208 if not Present (New_Subp)
15209 or else Ekind (Subp) /= Ekind (New_Subp)
15210 or else not Primitive_Names_Match (Subp, New_Subp)
15218 Next_Elmt (Op_Elmt);
15222 end Check_Derived_Type;
15224 ---------------------------------
15225 -- Derive_Interface_Subprogram --
15226 ---------------------------------
15228 procedure Derive_Interface_Subprogram
15229 (New_Subp : out Entity_Id;
15231 Actual_Subp : Entity_Id)
15233 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15234 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15237 pragma Assert (Is_Interface (Iface_Type));
15240 (New_Subp => New_Subp,
15241 Parent_Subp => Iface_Subp,
15242 Derived_Type => Derived_Type,
15243 Parent_Type => Iface_Type,
15244 Actual_Subp => Actual_Subp);
15246 -- Given that this new interface entity corresponds with a primitive
15247 -- of the parent that was not overridden we must leave it associated
15248 -- with its parent primitive to ensure that it will share the same
15249 -- dispatch table slot when overridden. We must set the Alias to Subp
15250 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15251 -- (in case we inherited Subp from Iface_Type via a nonabstract
15252 -- generic formal type).
15254 if No (Actual_Subp) then
15255 Set_Alias (New_Subp, Subp);
15258 T : Entity_Id := Find_Dispatching_Type (Subp);
15260 while Etype (T) /= T loop
15261 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15262 Set_Is_Abstract_Subprogram (New_Subp, False);
15270 -- For instantiations this is not needed since the previous call to
15271 -- Derive_Subprogram leaves the entity well decorated.
15274 pragma Assert (Alias (New_Subp) = Actual_Subp);
15277 end Derive_Interface_Subprogram;
15281 Alias_Subp : Entity_Id;
15282 Act_List : Elist_Id;
15283 Act_Elmt : Elmt_Id;
15284 Act_Subp : Entity_Id := Empty;
15286 Need_Search : Boolean := False;
15287 New_Subp : Entity_Id := Empty;
15288 Parent_Base : Entity_Id;
15291 -- Start of processing for Derive_Subprograms
15294 if Ekind (Parent_Type) = E_Record_Type_With_Private
15295 and then Has_Discriminants (Parent_Type)
15296 and then Present (Full_View (Parent_Type))
15298 Parent_Base := Full_View (Parent_Type);
15300 Parent_Base := Parent_Type;
15303 if Present (Generic_Actual) then
15304 Act_List := Collect_Primitive_Operations (Generic_Actual);
15305 Act_Elmt := First_Elmt (Act_List);
15307 Act_List := No_Elist;
15308 Act_Elmt := No_Elmt;
15311 -- Derive primitives inherited from the parent. Note that if the generic
15312 -- actual is present, this is not really a type derivation, it is a
15313 -- completion within an instance.
15315 -- Case 1: Derived_Type does not implement interfaces
15317 if not Is_Tagged_Type (Derived_Type)
15318 or else (not Has_Interfaces (Derived_Type)
15319 and then not (Present (Generic_Actual)
15320 and then Has_Interfaces (Generic_Actual)))
15322 Elmt := First_Elmt (Op_List);
15323 while Present (Elmt) loop
15324 Subp := Node (Elmt);
15326 -- Literals are derived earlier in the process of building the
15327 -- derived type, and are skipped here.
15329 if Ekind (Subp) = E_Enumeration_Literal then
15332 -- The actual is a direct descendant and the common primitive
15333 -- operations appear in the same order.
15335 -- If the generic parent type is present, the derived type is an
15336 -- instance of a formal derived type, and within the instance its
15337 -- operations are those of the actual. We derive from the formal
15338 -- type but make the inherited operations aliases of the
15339 -- corresponding operations of the actual.
15342 pragma Assert (No (Node (Act_Elmt))
15343 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15346 (Subp, Node (Act_Elmt),
15347 Skip_Controlling_Formals => True)));
15350 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15352 if Present (Act_Elmt) then
15353 Next_Elmt (Act_Elmt);
15360 -- Case 2: Derived_Type implements interfaces
15363 -- If the parent type has no predefined primitives we remove
15364 -- predefined primitives from the list of primitives of generic
15365 -- actual to simplify the complexity of this algorithm.
15367 if Present (Generic_Actual) then
15369 Has_Predefined_Primitives : Boolean := False;
15372 -- Check if the parent type has predefined primitives
15374 Elmt := First_Elmt (Op_List);
15375 while Present (Elmt) loop
15376 Subp := Node (Elmt);
15378 if Is_Predefined_Dispatching_Operation (Subp)
15379 and then not Comes_From_Source (Ultimate_Alias (Subp))
15381 Has_Predefined_Primitives := True;
15388 -- Remove predefined primitives of Generic_Actual. We must use
15389 -- an auxiliary list because in case of tagged types the value
15390 -- returned by Collect_Primitive_Operations is the value stored
15391 -- in its Primitive_Operations attribute (and we don't want to
15392 -- modify its current contents).
15394 if not Has_Predefined_Primitives then
15396 Aux_List : constant Elist_Id := New_Elmt_List;
15399 Elmt := First_Elmt (Act_List);
15400 while Present (Elmt) loop
15401 Subp := Node (Elmt);
15403 if not Is_Predefined_Dispatching_Operation (Subp)
15404 or else Comes_From_Source (Subp)
15406 Append_Elmt (Subp, Aux_List);
15412 Act_List := Aux_List;
15416 Act_Elmt := First_Elmt (Act_List);
15417 Act_Subp := Node (Act_Elmt);
15421 -- Stage 1: If the generic actual is not present we derive the
15422 -- primitives inherited from the parent type. If the generic parent
15423 -- type is present, the derived type is an instance of a formal
15424 -- derived type, and within the instance its operations are those of
15425 -- the actual. We derive from the formal type but make the inherited
15426 -- operations aliases of the corresponding operations of the actual.
15428 Elmt := First_Elmt (Op_List);
15429 while Present (Elmt) loop
15430 Subp := Node (Elmt);
15431 Alias_Subp := Ultimate_Alias (Subp);
15433 -- Do not derive internal entities of the parent that link
15434 -- interface primitives with their covering primitive. These
15435 -- entities will be added to this type when frozen.
15437 if Present (Interface_Alias (Subp)) then
15441 -- If the generic actual is present find the corresponding
15442 -- operation in the generic actual. If the parent type is a
15443 -- direct ancestor of the derived type then, even if it is an
15444 -- interface, the operations are inherited from the primary
15445 -- dispatch table and are in the proper order. If we detect here
15446 -- that primitives are not in the same order we traverse the list
15447 -- of primitive operations of the actual to find the one that
15448 -- implements the interface primitive.
15452 (Present (Generic_Actual)
15453 and then Present (Act_Subp)
15455 (Primitive_Names_Match (Subp, Act_Subp)
15457 Type_Conformant (Subp, Act_Subp,
15458 Skip_Controlling_Formals => True)))
15460 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15461 Use_Full_View => True));
15463 -- Remember that we need searching for all pending primitives
15465 Need_Search := True;
15467 -- Handle entities associated with interface primitives
15469 if Present (Alias_Subp)
15470 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15471 and then not Is_Predefined_Dispatching_Operation (Subp)
15473 -- Search for the primitive in the homonym chain
15476 Find_Primitive_Covering_Interface
15477 (Tagged_Type => Generic_Actual,
15478 Iface_Prim => Alias_Subp);
15480 -- Previous search may not locate primitives covering
15481 -- interfaces defined in generics units or instantiations.
15482 -- (it fails if the covering primitive has formals whose
15483 -- type is also defined in generics or instantiations).
15484 -- In such case we search in the list of primitives of the
15485 -- generic actual for the internal entity that links the
15486 -- interface primitive and the covering primitive.
15489 and then Is_Generic_Type (Parent_Type)
15491 -- This code has been designed to handle only generic
15492 -- formals that implement interfaces that are defined
15493 -- in a generic unit or instantiation. If this code is
15494 -- needed for other cases we must review it because
15495 -- (given that it relies on Original_Location to locate
15496 -- the primitive of Generic_Actual that covers the
15497 -- interface) it could leave linked through attribute
15498 -- Alias entities of unrelated instantiations).
15502 (Scope (Find_Dispatching_Type (Alias_Subp)))
15504 Instantiation_Depth
15505 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15508 Iface_Prim_Loc : constant Source_Ptr :=
15509 Original_Location (Sloc (Alias_Subp));
15516 First_Elmt (Primitive_Operations (Generic_Actual));
15518 Search : while Present (Elmt) loop
15519 Prim := Node (Elmt);
15521 if Present (Interface_Alias (Prim))
15522 and then Original_Location
15523 (Sloc (Interface_Alias (Prim))) =
15526 Act_Subp := Alias (Prim);
15535 pragma Assert (Present (Act_Subp)
15536 or else Is_Abstract_Type (Generic_Actual)
15537 or else Serious_Errors_Detected > 0);
15539 -- Handle predefined primitives plus the rest of user-defined
15543 Act_Elmt := First_Elmt (Act_List);
15544 while Present (Act_Elmt) loop
15545 Act_Subp := Node (Act_Elmt);
15547 exit when Primitive_Names_Match (Subp, Act_Subp)
15548 and then Type_Conformant
15550 Skip_Controlling_Formals => True)
15551 and then No (Interface_Alias (Act_Subp));
15553 Next_Elmt (Act_Elmt);
15556 if No (Act_Elmt) then
15562 -- Case 1: If the parent is a limited interface then it has the
15563 -- predefined primitives of synchronized interfaces. However, the
15564 -- actual type may be a non-limited type and hence it does not
15565 -- have such primitives.
15567 if Present (Generic_Actual)
15568 and then not Present (Act_Subp)
15569 and then Is_Limited_Interface (Parent_Base)
15570 and then Is_Predefined_Interface_Primitive (Subp)
15574 -- Case 2: Inherit entities associated with interfaces that were
15575 -- not covered by the parent type. We exclude here null interface
15576 -- primitives because they do not need special management.
15578 -- We also exclude interface operations that are renamings. If the
15579 -- subprogram is an explicit renaming of an interface primitive,
15580 -- it is a regular primitive operation, and the presence of its
15581 -- alias is not relevant: it has to be derived like any other
15584 elsif Present (Alias (Subp))
15585 and then Nkind (Unit_Declaration_Node (Subp)) /=
15586 N_Subprogram_Renaming_Declaration
15587 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15589 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15590 and then Null_Present (Parent (Alias_Subp)))
15592 -- If this is an abstract private type then we transfer the
15593 -- derivation of the interface primitive from the partial view
15594 -- to the full view. This is safe because all the interfaces
15595 -- must be visible in the partial view. Done to avoid adding
15596 -- a new interface derivation to the private part of the
15597 -- enclosing package; otherwise this new derivation would be
15598 -- decorated as hidden when the analysis of the enclosing
15599 -- package completes.
15601 if Is_Abstract_Type (Derived_Type)
15602 and then In_Private_Part (Current_Scope)
15603 and then Has_Private_Declaration (Derived_Type)
15606 Partial_View : Entity_Id;
15611 Partial_View := First_Entity (Current_Scope);
15613 exit when No (Partial_View)
15614 or else (Has_Private_Declaration (Partial_View)
15616 Full_View (Partial_View) = Derived_Type);
15618 Next_Entity (Partial_View);
15621 -- If the partial view was not found then the source code
15622 -- has errors and the derivation is not needed.
15624 if Present (Partial_View) then
15626 First_Elmt (Primitive_Operations (Partial_View));
15627 while Present (Elmt) loop
15628 Ent := Node (Elmt);
15630 if Present (Alias (Ent))
15631 and then Ultimate_Alias (Ent) = Alias (Subp)
15634 (Ent, Primitive_Operations (Derived_Type));
15641 -- If the interface primitive was not found in the
15642 -- partial view then this interface primitive was
15643 -- overridden. We add a derivation to activate in
15644 -- Derive_Progenitor_Subprograms the machinery to
15648 Derive_Interface_Subprogram
15649 (New_Subp => New_Subp,
15651 Actual_Subp => Act_Subp);
15656 Derive_Interface_Subprogram
15657 (New_Subp => New_Subp,
15659 Actual_Subp => Act_Subp);
15662 -- Case 3: Common derivation
15666 (New_Subp => New_Subp,
15667 Parent_Subp => Subp,
15668 Derived_Type => Derived_Type,
15669 Parent_Type => Parent_Base,
15670 Actual_Subp => Act_Subp);
15673 -- No need to update Act_Elm if we must search for the
15674 -- corresponding operation in the generic actual
15677 and then Present (Act_Elmt)
15679 Next_Elmt (Act_Elmt);
15680 Act_Subp := Node (Act_Elmt);
15687 -- Inherit additional operations from progenitors. If the derived
15688 -- type is a generic actual, there are not new primitive operations
15689 -- for the type because it has those of the actual, and therefore
15690 -- nothing needs to be done. The renamings generated above are not
15691 -- primitive operations, and their purpose is simply to make the
15692 -- proper operations visible within an instantiation.
15694 if No (Generic_Actual) then
15695 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15699 -- Final check: Direct descendants must have their primitives in the
15700 -- same order. We exclude from this test untagged types and instances
15701 -- of formal derived types. We skip this test if we have already
15702 -- reported serious errors in the sources.
15704 pragma Assert (not Is_Tagged_Type (Derived_Type)
15705 or else Present (Generic_Actual)
15706 or else Serious_Errors_Detected > 0
15707 or else Check_Derived_Type);
15708 end Derive_Subprograms;
15710 --------------------------------
15711 -- Derived_Standard_Character --
15712 --------------------------------
15714 procedure Derived_Standard_Character
15716 Parent_Type : Entity_Id;
15717 Derived_Type : Entity_Id)
15719 Loc : constant Source_Ptr := Sloc (N);
15720 Def : constant Node_Id := Type_Definition (N);
15721 Indic : constant Node_Id := Subtype_Indication (Def);
15722 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15723 Implicit_Base : constant Entity_Id :=
15725 (E_Enumeration_Type, N, Derived_Type, 'B');
15731 Discard_Node (Process_Subtype (Indic, N));
15733 Set_Etype (Implicit_Base, Parent_Base);
15734 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15735 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15737 Set_Is_Character_Type (Implicit_Base, True);
15738 Set_Has_Delayed_Freeze (Implicit_Base);
15740 -- The bounds of the implicit base are the bounds of the parent base.
15741 -- Note that their type is the parent base.
15743 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15744 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15746 Set_Scalar_Range (Implicit_Base,
15749 High_Bound => Hi));
15751 Conditional_Delay (Derived_Type, Parent_Type);
15753 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15754 Set_Etype (Derived_Type, Implicit_Base);
15755 Set_Size_Info (Derived_Type, Parent_Type);
15757 if Unknown_RM_Size (Derived_Type) then
15758 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15761 Set_Is_Character_Type (Derived_Type, True);
15763 if Nkind (Indic) /= N_Subtype_Indication then
15765 -- If no explicit constraint, the bounds are those
15766 -- of the parent type.
15768 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15769 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15770 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15773 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15775 -- Because the implicit base is used in the conversion of the bounds, we
15776 -- have to freeze it now. This is similar to what is done for numeric
15777 -- types, and it equally suspicious, but otherwise a non-static bound
15778 -- will have a reference to an unfrozen type, which is rejected by Gigi
15779 -- (???). This requires specific care for definition of stream
15780 -- attributes. For details, see comments at the end of
15781 -- Build_Derived_Numeric_Type.
15783 Freeze_Before (N, Implicit_Base);
15784 end Derived_Standard_Character;
15786 ------------------------------
15787 -- Derived_Type_Declaration --
15788 ------------------------------
15790 procedure Derived_Type_Declaration
15793 Is_Completion : Boolean)
15795 Parent_Type : Entity_Id;
15797 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15798 -- Check whether the parent type is a generic formal, or derives
15799 -- directly or indirectly from one.
15801 ------------------------
15802 -- Comes_From_Generic --
15803 ------------------------
15805 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15807 if Is_Generic_Type (Typ) then
15810 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15813 elsif Is_Private_Type (Typ)
15814 and then Present (Full_View (Typ))
15815 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15819 elsif Is_Generic_Actual_Type (Typ) then
15825 end Comes_From_Generic;
15829 Def : constant Node_Id := Type_Definition (N);
15830 Iface_Def : Node_Id;
15831 Indic : constant Node_Id := Subtype_Indication (Def);
15832 Extension : constant Node_Id := Record_Extension_Part (Def);
15833 Parent_Node : Node_Id;
15836 -- Start of processing for Derived_Type_Declaration
15839 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
15841 -- Ada 2005 (AI-251): In case of interface derivation check that the
15842 -- parent is also an interface.
15844 if Interface_Present (Def) then
15845 Check_SPARK_05_Restriction ("interface is not allowed", Def);
15847 if not Is_Interface (Parent_Type) then
15848 Diagnose_Interface (Indic, Parent_Type);
15851 Parent_Node := Parent (Base_Type (Parent_Type));
15852 Iface_Def := Type_Definition (Parent_Node);
15854 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
15855 -- other limited interfaces.
15857 if Limited_Present (Def) then
15858 if Limited_Present (Iface_Def) then
15861 elsif Protected_Present (Iface_Def) then
15863 ("descendant of & must be declared as a protected "
15864 & "interface", N, Parent_Type);
15866 elsif Synchronized_Present (Iface_Def) then
15868 ("descendant of & must be declared as a synchronized "
15869 & "interface", N, Parent_Type);
15871 elsif Task_Present (Iface_Def) then
15873 ("descendant of & must be declared as a task interface",
15878 ("(Ada 2005) limited interface cannot inherit from "
15879 & "non-limited interface", Indic);
15882 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
15883 -- from non-limited or limited interfaces.
15885 elsif not Protected_Present (Def)
15886 and then not Synchronized_Present (Def)
15887 and then not Task_Present (Def)
15889 if Limited_Present (Iface_Def) then
15892 elsif Protected_Present (Iface_Def) then
15894 ("descendant of & must be declared as a protected "
15895 & "interface", N, Parent_Type);
15897 elsif Synchronized_Present (Iface_Def) then
15899 ("descendant of & must be declared as a synchronized "
15900 & "interface", N, Parent_Type);
15902 elsif Task_Present (Iface_Def) then
15904 ("descendant of & must be declared as a task interface",
15913 if Is_Tagged_Type (Parent_Type)
15914 and then Is_Concurrent_Type (Parent_Type)
15915 and then not Is_Interface (Parent_Type)
15918 ("parent type of a record extension cannot be a synchronized "
15919 & "tagged type (RM 3.9.1 (3/1))", N);
15920 Set_Etype (T, Any_Type);
15924 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
15927 if Is_Tagged_Type (Parent_Type)
15928 and then Is_Non_Empty_List (Interface_List (Def))
15935 Intf := First (Interface_List (Def));
15936 while Present (Intf) loop
15937 T := Find_Type_Of_Subtype_Indic (Intf);
15939 if not Is_Interface (T) then
15940 Diagnose_Interface (Intf, T);
15942 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
15943 -- a limited type from having a nonlimited progenitor.
15945 elsif (Limited_Present (Def)
15946 or else (not Is_Interface (Parent_Type)
15947 and then Is_Limited_Type (Parent_Type)))
15948 and then not Is_Limited_Interface (T)
15951 ("progenitor interface& of limited type must be limited",
15960 if Parent_Type = Any_Type
15961 or else Etype (Parent_Type) = Any_Type
15962 or else (Is_Class_Wide_Type (Parent_Type)
15963 and then Etype (Parent_Type) = T)
15965 -- If Parent_Type is undefined or illegal, make new type into a
15966 -- subtype of Any_Type, and set a few attributes to prevent cascaded
15967 -- errors. If this is a self-definition, emit error now.
15969 if T = Parent_Type or else T = Etype (Parent_Type) then
15970 Error_Msg_N ("type cannot be used in its own definition", Indic);
15973 Set_Ekind (T, Ekind (Parent_Type));
15974 Set_Etype (T, Any_Type);
15975 Set_Scalar_Range (T, Scalar_Range (Any_Type));
15977 if Is_Tagged_Type (T)
15978 and then Is_Record_Type (T)
15980 Set_Direct_Primitive_Operations (T, New_Elmt_List);
15986 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
15987 -- an interface is special because the list of interfaces in the full
15988 -- view can be given in any order. For example:
15990 -- type A is interface;
15991 -- type B is interface and A;
15992 -- type D is new B with private;
15994 -- type D is new A and B with null record; -- 1 --
15996 -- In this case we perform the following transformation of -1-:
15998 -- type D is new B and A with null record;
16000 -- If the parent of the full-view covers the parent of the partial-view
16001 -- we have two possible cases:
16003 -- 1) They have the same parent
16004 -- 2) The parent of the full-view implements some further interfaces
16006 -- In both cases we do not need to perform the transformation. In the
16007 -- first case the source program is correct and the transformation is
16008 -- not needed; in the second case the source program does not fulfill
16009 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16012 -- This transformation not only simplifies the rest of the analysis of
16013 -- this type declaration but also simplifies the correct generation of
16014 -- the object layout to the expander.
16016 if In_Private_Part (Current_Scope)
16017 and then Is_Interface (Parent_Type)
16021 Partial_View : Entity_Id;
16022 Partial_View_Parent : Entity_Id;
16023 New_Iface : Node_Id;
16026 -- Look for the associated private type declaration
16028 Partial_View := First_Entity (Current_Scope);
16030 exit when No (Partial_View)
16031 or else (Has_Private_Declaration (Partial_View)
16032 and then Full_View (Partial_View) = T);
16034 Next_Entity (Partial_View);
16037 -- If the partial view was not found then the source code has
16038 -- errors and the transformation is not needed.
16040 if Present (Partial_View) then
16041 Partial_View_Parent := Etype (Partial_View);
16043 -- If the parent of the full-view covers the parent of the
16044 -- partial-view we have nothing else to do.
16046 if Interface_Present_In_Ancestor
16047 (Parent_Type, Partial_View_Parent)
16051 -- Traverse the list of interfaces of the full-view to look
16052 -- for the parent of the partial-view and perform the tree
16056 Iface := First (Interface_List (Def));
16057 while Present (Iface) loop
16058 if Etype (Iface) = Etype (Partial_View) then
16059 Rewrite (Subtype_Indication (Def),
16060 New_Copy (Subtype_Indication
16061 (Parent (Partial_View))));
16064 Make_Identifier (Sloc (N), Chars (Parent_Type));
16065 Append (New_Iface, Interface_List (Def));
16067 -- Analyze the transformed code
16069 Derived_Type_Declaration (T, N, Is_Completion);
16080 -- Only composite types other than array types are allowed to have
16083 if Present (Discriminant_Specifications (N)) then
16084 if (Is_Elementary_Type (Parent_Type)
16086 Is_Array_Type (Parent_Type))
16087 and then not Error_Posted (N)
16090 ("elementary or array type cannot have discriminants",
16091 Defining_Identifier (First (Discriminant_Specifications (N))));
16092 Set_Has_Discriminants (T, False);
16094 -- The type is allowed to have discriminants
16097 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16101 -- In Ada 83, a derived type defined in a package specification cannot
16102 -- be used for further derivation until the end of its visible part.
16103 -- Note that derivation in the private part of the package is allowed.
16105 if Ada_Version = Ada_83
16106 and then Is_Derived_Type (Parent_Type)
16107 and then In_Visible_Part (Scope (Parent_Type))
16109 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16111 ("(Ada 83): premature use of type for derivation", Indic);
16115 -- Check for early use of incomplete or private type
16117 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16118 Error_Msg_N ("premature derivation of incomplete type", Indic);
16121 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16122 and then not Comes_From_Generic (Parent_Type))
16123 or else Has_Private_Component (Parent_Type)
16125 -- The ancestor type of a formal type can be incomplete, in which
16126 -- case only the operations of the partial view are available in the
16127 -- generic. Subsequent checks may be required when the full view is
16128 -- analyzed to verify that a derivation from a tagged type has an
16131 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16134 elsif No (Underlying_Type (Parent_Type))
16135 or else Has_Private_Component (Parent_Type)
16138 ("premature derivation of derived or private type", Indic);
16140 -- Flag the type itself as being in error, this prevents some
16141 -- nasty problems with subsequent uses of the malformed type.
16143 Set_Error_Posted (T);
16145 -- Check that within the immediate scope of an untagged partial
16146 -- view it's illegal to derive from the partial view if the
16147 -- full view is tagged. (7.3(7))
16149 -- We verify that the Parent_Type is a partial view by checking
16150 -- that it is not a Full_Type_Declaration (i.e. a private type or
16151 -- private extension declaration), to distinguish a partial view
16152 -- from a derivation from a private type which also appears as
16153 -- E_Private_Type. If the parent base type is not declared in an
16154 -- enclosing scope there is no need to check.
16156 elsif Present (Full_View (Parent_Type))
16157 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16158 and then not Is_Tagged_Type (Parent_Type)
16159 and then Is_Tagged_Type (Full_View (Parent_Type))
16160 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16163 ("premature derivation from type with tagged full view",
16168 -- Check that form of derivation is appropriate
16170 Taggd := Is_Tagged_Type (Parent_Type);
16172 -- Set the parent type to the class-wide type's specific type in this
16173 -- case to prevent cascading errors
16175 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16176 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16177 Set_Etype (T, Etype (Parent_Type));
16181 if Present (Extension) and then not Taggd then
16183 ("type derived from untagged type cannot have extension", Indic);
16185 elsif No (Extension) and then Taggd then
16187 -- If this declaration is within a private part (or body) of a
16188 -- generic instantiation then the derivation is allowed (the parent
16189 -- type can only appear tagged in this case if it's a generic actual
16190 -- type, since it would otherwise have been rejected in the analysis
16191 -- of the generic template).
16193 if not Is_Generic_Actual_Type (Parent_Type)
16194 or else In_Visible_Part (Scope (Parent_Type))
16196 if Is_Class_Wide_Type (Parent_Type) then
16198 ("parent type must not be a class-wide type", Indic);
16200 -- Use specific type to prevent cascaded errors.
16202 Parent_Type := Etype (Parent_Type);
16206 ("type derived from tagged type must have extension", Indic);
16211 -- AI-443: Synchronized formal derived types require a private
16212 -- extension. There is no point in checking the ancestor type or
16213 -- the progenitors since the construct is wrong to begin with.
16215 if Ada_Version >= Ada_2005
16216 and then Is_Generic_Type (T)
16217 and then Present (Original_Node (N))
16220 Decl : constant Node_Id := Original_Node (N);
16223 if Nkind (Decl) = N_Formal_Type_Declaration
16224 and then Nkind (Formal_Type_Definition (Decl)) =
16225 N_Formal_Derived_Type_Definition
16226 and then Synchronized_Present (Formal_Type_Definition (Decl))
16227 and then No (Extension)
16229 -- Avoid emitting a duplicate error message
16231 and then not Error_Posted (Indic)
16234 ("synchronized derived type must have extension", N);
16239 if Null_Exclusion_Present (Def)
16240 and then not Is_Access_Type (Parent_Type)
16242 Error_Msg_N ("null exclusion can only apply to an access type", N);
16245 -- Avoid deriving parent primitives of underlying record views
16247 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16248 Derive_Subps => not Is_Underlying_Record_View (T));
16250 -- AI-419: The parent type of an explicitly limited derived type must
16251 -- be a limited type or a limited interface.
16253 if Limited_Present (Def) then
16254 Set_Is_Limited_Record (T);
16256 if Is_Interface (T) then
16257 Set_Is_Limited_Interface (T);
16260 if not Is_Limited_Type (Parent_Type)
16262 (not Is_Interface (Parent_Type)
16263 or else not Is_Limited_Interface (Parent_Type))
16265 -- AI05-0096: a derivation in the private part of an instance is
16266 -- legal if the generic formal is untagged limited, and the actual
16269 if Is_Generic_Actual_Type (Parent_Type)
16270 and then In_Private_Part (Current_Scope)
16273 (Generic_Parent_Type (Parent (Parent_Type)))
16279 ("parent type& of limited type must be limited",
16285 -- In SPARK, there are no derived type definitions other than type
16286 -- extensions of tagged record types.
16288 if No (Extension) then
16289 Check_SPARK_05_Restriction
16290 ("derived type is not allowed", Original_Node (N));
16292 end Derived_Type_Declaration;
16294 ------------------------
16295 -- Diagnose_Interface --
16296 ------------------------
16298 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16300 if not Is_Interface (E) and then E /= Any_Type then
16301 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16303 end Diagnose_Interface;
16305 ----------------------------------
16306 -- Enumeration_Type_Declaration --
16307 ----------------------------------
16309 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16316 -- Create identifier node representing lower bound
16318 B_Node := New_Node (N_Identifier, Sloc (Def));
16319 L := First (Literals (Def));
16320 Set_Chars (B_Node, Chars (L));
16321 Set_Entity (B_Node, L);
16322 Set_Etype (B_Node, T);
16323 Set_Is_Static_Expression (B_Node, True);
16325 R_Node := New_Node (N_Range, Sloc (Def));
16326 Set_Low_Bound (R_Node, B_Node);
16328 Set_Ekind (T, E_Enumeration_Type);
16329 Set_First_Literal (T, L);
16331 Set_Is_Constrained (T);
16335 -- Loop through literals of enumeration type setting pos and rep values
16336 -- except that if the Ekind is already set, then it means the literal
16337 -- was already constructed (case of a derived type declaration and we
16338 -- should not disturb the Pos and Rep values.
16340 while Present (L) loop
16341 if Ekind (L) /= E_Enumeration_Literal then
16342 Set_Ekind (L, E_Enumeration_Literal);
16343 Set_Enumeration_Pos (L, Ev);
16344 Set_Enumeration_Rep (L, Ev);
16345 Set_Is_Known_Valid (L, True);
16349 New_Overloaded_Entity (L);
16350 Generate_Definition (L);
16351 Set_Convention (L, Convention_Intrinsic);
16353 -- Case of character literal
16355 if Nkind (L) = N_Defining_Character_Literal then
16356 Set_Is_Character_Type (T, True);
16358 -- Check violation of No_Wide_Characters
16360 if Restriction_Check_Required (No_Wide_Characters) then
16361 Get_Name_String (Chars (L));
16363 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16364 Check_Restriction (No_Wide_Characters, L);
16373 -- Now create a node representing upper bound
16375 B_Node := New_Node (N_Identifier, Sloc (Def));
16376 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16377 Set_Entity (B_Node, Last (Literals (Def)));
16378 Set_Etype (B_Node, T);
16379 Set_Is_Static_Expression (B_Node, True);
16381 Set_High_Bound (R_Node, B_Node);
16383 -- Initialize various fields of the type. Some of this information
16384 -- may be overwritten later through rep.clauses.
16386 Set_Scalar_Range (T, R_Node);
16387 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16388 Set_Enum_Esize (T);
16389 Set_Enum_Pos_To_Rep (T, Empty);
16391 -- Set Discard_Names if configuration pragma set, or if there is
16392 -- a parameterless pragma in the current declarative region
16394 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16395 Set_Discard_Names (T);
16398 -- Process end label if there is one
16400 if Present (Def) then
16401 Process_End_Label (Def, 'e', T);
16403 end Enumeration_Type_Declaration;
16405 ---------------------------------
16406 -- Expand_To_Stored_Constraint --
16407 ---------------------------------
16409 function Expand_To_Stored_Constraint
16411 Constraint : Elist_Id) return Elist_Id
16413 Explicitly_Discriminated_Type : Entity_Id;
16414 Expansion : Elist_Id;
16415 Discriminant : Entity_Id;
16417 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16418 -- Find the nearest type that actually specifies discriminants
16420 ---------------------------------
16421 -- Type_With_Explicit_Discrims --
16422 ---------------------------------
16424 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16425 Typ : constant E := Base_Type (Id);
16428 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16429 if Present (Full_View (Typ)) then
16430 return Type_With_Explicit_Discrims (Full_View (Typ));
16434 if Has_Discriminants (Typ) then
16439 if Etype (Typ) = Typ then
16441 elsif Has_Discriminants (Typ) then
16444 return Type_With_Explicit_Discrims (Etype (Typ));
16447 end Type_With_Explicit_Discrims;
16449 -- Start of processing for Expand_To_Stored_Constraint
16452 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16456 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16458 if No (Explicitly_Discriminated_Type) then
16462 Expansion := New_Elmt_List;
16465 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16466 while Present (Discriminant) loop
16468 (Get_Discriminant_Value
16469 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16471 Next_Stored_Discriminant (Discriminant);
16475 end Expand_To_Stored_Constraint;
16477 ---------------------------
16478 -- Find_Hidden_Interface --
16479 ---------------------------
16481 function Find_Hidden_Interface
16483 Dest : Elist_Id) return Entity_Id
16486 Iface_Elmt : Elmt_Id;
16489 if Present (Src) and then Present (Dest) then
16490 Iface_Elmt := First_Elmt (Src);
16491 while Present (Iface_Elmt) loop
16492 Iface := Node (Iface_Elmt);
16494 if Is_Interface (Iface)
16495 and then not Contain_Interface (Iface, Dest)
16500 Next_Elmt (Iface_Elmt);
16505 end Find_Hidden_Interface;
16507 --------------------
16508 -- Find_Type_Name --
16509 --------------------
16511 function Find_Type_Name (N : Node_Id) return Entity_Id is
16512 Id : constant Entity_Id := Defining_Identifier (N);
16513 New_Id : Entity_Id;
16515 Prev_Par : Node_Id;
16517 procedure Check_Duplicate_Aspects;
16518 -- Check that aspects specified in a completion have not been specified
16519 -- already in the partial view.
16521 procedure Tag_Mismatch;
16522 -- Diagnose a tagged partial view whose full view is untagged. We post
16523 -- the message on the full view, with a reference to the previous
16524 -- partial view. The partial view can be private or incomplete, and
16525 -- these are handled in a different manner, so we determine the position
16526 -- of the error message from the respective slocs of both.
16528 -----------------------------
16529 -- Check_Duplicate_Aspects --
16530 -----------------------------
16532 procedure Check_Duplicate_Aspects is
16533 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
16534 -- Return the corresponding aspect of the partial view which matches
16535 -- the aspect id of Asp. Return Empty is no such aspect exists.
16537 -----------------------------
16538 -- Get_Partial_View_Aspect --
16539 -----------------------------
16541 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
16542 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
16543 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
16544 Prev_Asp : Node_Id;
16547 if Present (Prev_Asps) then
16548 Prev_Asp := First (Prev_Asps);
16549 while Present (Prev_Asp) loop
16550 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
16559 end Get_Partial_View_Aspect;
16563 Full_Asps : constant List_Id := Aspect_Specifications (N);
16564 Full_Asp : Node_Id;
16565 Part_Asp : Node_Id;
16567 -- Start of processing for Check_Duplicate_Aspects
16570 if Present (Full_Asps) then
16571 Full_Asp := First (Full_Asps);
16572 while Present (Full_Asp) loop
16573 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
16575 -- An aspect and its class-wide counterpart are two distinct
16576 -- aspects and may apply to both views of an entity.
16578 if Present (Part_Asp)
16579 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
16582 ("aspect already specified in private declaration",
16589 if Has_Discriminants (Prev)
16590 and then not Has_Unknown_Discriminants (Prev)
16591 and then Get_Aspect_Id (Full_Asp) =
16592 Aspect_Implicit_Dereference
16595 ("cannot specify aspect if partial view has known "
16596 & "discriminants", Full_Asp);
16602 end Check_Duplicate_Aspects;
16608 procedure Tag_Mismatch is
16610 if Sloc (Prev) < Sloc (Id) then
16611 if Ada_Version >= Ada_2012
16612 and then Nkind (N) = N_Private_Type_Declaration
16615 ("declaration of private } must be a tagged type ", Id, Prev);
16618 ("full declaration of } must be a tagged type ", Id, Prev);
16622 if Ada_Version >= Ada_2012
16623 and then Nkind (N) = N_Private_Type_Declaration
16626 ("declaration of private } must be a tagged type ", Prev, Id);
16629 ("full declaration of } must be a tagged type ", Prev, Id);
16634 -- Start of processing for Find_Type_Name
16637 -- Find incomplete declaration, if one was given
16639 Prev := Current_Entity_In_Scope (Id);
16641 -- New type declaration
16647 -- Previous declaration exists
16650 Prev_Par := Parent (Prev);
16652 -- Error if not incomplete/private case except if previous
16653 -- declaration is implicit, etc. Enter_Name will emit error if
16656 if not Is_Incomplete_Or_Private_Type (Prev) then
16660 -- Check invalid completion of private or incomplete type
16662 elsif not Nkind_In (N, N_Full_Type_Declaration,
16663 N_Task_Type_Declaration,
16664 N_Protected_Type_Declaration)
16666 (Ada_Version < Ada_2012
16667 or else not Is_Incomplete_Type (Prev)
16668 or else not Nkind_In (N, N_Private_Type_Declaration,
16669 N_Private_Extension_Declaration))
16671 -- Completion must be a full type declarations (RM 7.3(4))
16673 Error_Msg_Sloc := Sloc (Prev);
16674 Error_Msg_NE ("invalid completion of }", Id, Prev);
16676 -- Set scope of Id to avoid cascaded errors. Entity is never
16677 -- examined again, except when saving globals in generics.
16679 Set_Scope (Id, Current_Scope);
16682 -- If this is a repeated incomplete declaration, no further
16683 -- checks are possible.
16685 if Nkind (N) = N_Incomplete_Type_Declaration then
16689 -- Case of full declaration of incomplete type
16691 elsif Ekind (Prev) = E_Incomplete_Type
16692 and then (Ada_Version < Ada_2012
16693 or else No (Full_View (Prev))
16694 or else not Is_Private_Type (Full_View (Prev)))
16696 -- Indicate that the incomplete declaration has a matching full
16697 -- declaration. The defining occurrence of the incomplete
16698 -- declaration remains the visible one, and the procedure
16699 -- Get_Full_View dereferences it whenever the type is used.
16701 if Present (Full_View (Prev)) then
16702 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16705 Set_Full_View (Prev, Id);
16706 Append_Entity (Id, Current_Scope);
16707 Set_Is_Public (Id, Is_Public (Prev));
16708 Set_Is_Internal (Id);
16711 -- If the incomplete view is tagged, a class_wide type has been
16712 -- created already. Use it for the private type as well, in order
16713 -- to prevent multiple incompatible class-wide types that may be
16714 -- created for self-referential anonymous access components.
16716 if Is_Tagged_Type (Prev)
16717 and then Present (Class_Wide_Type (Prev))
16719 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16720 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16722 -- Type of the class-wide type is the current Id. Previously
16723 -- this was not done for private declarations because of order-
16724 -- of-elaboration issues in the back end, but gigi now handles
16727 Set_Etype (Class_Wide_Type (Id), Id);
16730 -- Case of full declaration of private type
16733 -- If the private type was a completion of an incomplete type then
16734 -- update Prev to reference the private type
16736 if Ada_Version >= Ada_2012
16737 and then Ekind (Prev) = E_Incomplete_Type
16738 and then Present (Full_View (Prev))
16739 and then Is_Private_Type (Full_View (Prev))
16741 Prev := Full_View (Prev);
16742 Prev_Par := Parent (Prev);
16745 if Nkind (N) = N_Full_Type_Declaration
16747 (Type_Definition (N), N_Record_Definition,
16748 N_Derived_Type_Definition)
16749 and then Interface_Present (Type_Definition (N))
16752 ("completion of private type cannot be an interface", N);
16755 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16756 if Etype (Prev) /= Prev then
16758 -- Prev is a private subtype or a derived type, and needs
16761 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16764 elsif Ekind (Prev) = E_Private_Type
16765 and then Nkind_In (N, N_Task_Type_Declaration,
16766 N_Protected_Type_Declaration)
16769 ("completion of nonlimited type cannot be limited", N);
16771 elsif Ekind (Prev) = E_Record_Type_With_Private
16772 and then Nkind_In (N, N_Task_Type_Declaration,
16773 N_Protected_Type_Declaration)
16775 if not Is_Limited_Record (Prev) then
16777 ("completion of nonlimited type cannot be limited", N);
16779 elsif No (Interface_List (N)) then
16781 ("completion of tagged private type must be tagged",
16786 -- Ada 2005 (AI-251): Private extension declaration of a task
16787 -- type or a protected type. This case arises when covering
16788 -- interface types.
16790 elsif Nkind_In (N, N_Task_Type_Declaration,
16791 N_Protected_Type_Declaration)
16795 elsif Nkind (N) /= N_Full_Type_Declaration
16796 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16799 ("full view of private extension must be an extension", N);
16801 elsif not (Abstract_Present (Parent (Prev)))
16802 and then Abstract_Present (Type_Definition (N))
16805 ("full view of non-abstract extension cannot be abstract", N);
16808 if not In_Private_Part (Current_Scope) then
16810 ("declaration of full view must appear in private part", N);
16813 if Ada_Version >= Ada_2012 then
16814 Check_Duplicate_Aspects;
16817 Copy_And_Swap (Prev, Id);
16818 Set_Has_Private_Declaration (Prev);
16819 Set_Has_Private_Declaration (Id);
16821 -- AI12-0133: Indicate whether we have a partial view with
16822 -- unknown discriminants, in which case initialization of objects
16823 -- of the type do not receive an invariant check.
16825 Set_Partial_View_Has_Unknown_Discr
16826 (Prev, Has_Unknown_Discriminants (Id));
16828 -- Preserve aspect and iterator flags that may have been set on
16829 -- the partial view.
16831 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
16832 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
16834 -- If no error, propagate freeze_node from private to full view.
16835 -- It may have been generated for an early operational item.
16837 if Present (Freeze_Node (Id))
16838 and then Serious_Errors_Detected = 0
16839 and then No (Full_View (Id))
16841 Set_Freeze_Node (Prev, Freeze_Node (Id));
16842 Set_Freeze_Node (Id, Empty);
16843 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
16846 Set_Full_View (Id, Prev);
16850 -- Verify that full declaration conforms to partial one
16852 if Is_Incomplete_Or_Private_Type (Prev)
16853 and then Present (Discriminant_Specifications (Prev_Par))
16855 if Present (Discriminant_Specifications (N)) then
16856 if Ekind (Prev) = E_Incomplete_Type then
16857 Check_Discriminant_Conformance (N, Prev, Prev);
16859 Check_Discriminant_Conformance (N, Prev, Id);
16864 ("missing discriminants in full type declaration", N);
16866 -- To avoid cascaded errors on subsequent use, share the
16867 -- discriminants of the partial view.
16869 Set_Discriminant_Specifications (N,
16870 Discriminant_Specifications (Prev_Par));
16874 -- A prior untagged partial view can have an associated class-wide
16875 -- type due to use of the class attribute, and in this case the full
16876 -- type must also be tagged. This Ada 95 usage is deprecated in favor
16877 -- of incomplete tagged declarations, but we check for it.
16880 and then (Is_Tagged_Type (Prev)
16881 or else Present (Class_Wide_Type (Prev)))
16883 -- Ada 2012 (AI05-0162): A private type may be the completion of
16884 -- an incomplete type.
16886 if Ada_Version >= Ada_2012
16887 and then Is_Incomplete_Type (Prev)
16888 and then Nkind_In (N, N_Private_Type_Declaration,
16889 N_Private_Extension_Declaration)
16891 -- No need to check private extensions since they are tagged
16893 if Nkind (N) = N_Private_Type_Declaration
16894 and then not Tagged_Present (N)
16899 -- The full declaration is either a tagged type (including
16900 -- a synchronized type that implements interfaces) or a
16901 -- type extension, otherwise this is an error.
16903 elsif Nkind_In (N, N_Task_Type_Declaration,
16904 N_Protected_Type_Declaration)
16906 if No (Interface_List (N)) and then not Error_Posted (N) then
16910 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
16912 -- Indicate that the previous declaration (tagged incomplete
16913 -- or private declaration) requires the same on the full one.
16915 if not Tagged_Present (Type_Definition (N)) then
16917 Set_Is_Tagged_Type (Id);
16920 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
16921 if No (Record_Extension_Part (Type_Definition (N))) then
16923 ("full declaration of } must be a record extension",
16926 -- Set some attributes to produce a usable full view
16928 Set_Is_Tagged_Type (Id);
16937 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
16938 and then Present (Premature_Use (Parent (Prev)))
16940 Error_Msg_Sloc := Sloc (N);
16942 ("\full declaration #", Premature_Use (Parent (Prev)));
16947 end Find_Type_Name;
16949 -------------------------
16950 -- Find_Type_Of_Object --
16951 -------------------------
16953 function Find_Type_Of_Object
16954 (Obj_Def : Node_Id;
16955 Related_Nod : Node_Id) return Entity_Id
16957 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
16958 P : Node_Id := Parent (Obj_Def);
16963 -- If the parent is a component_definition node we climb to the
16964 -- component_declaration node
16966 if Nkind (P) = N_Component_Definition then
16970 -- Case of an anonymous array subtype
16972 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
16973 N_Unconstrained_Array_Definition)
16976 Array_Type_Declaration (T, Obj_Def);
16978 -- Create an explicit subtype whenever possible
16980 elsif Nkind (P) /= N_Component_Declaration
16981 and then Def_Kind = N_Subtype_Indication
16983 -- Base name of subtype on object name, which will be unique in
16984 -- the current scope.
16986 -- If this is a duplicate declaration, return base type, to avoid
16987 -- generating duplicate anonymous types.
16989 if Error_Posted (P) then
16990 Analyze (Subtype_Mark (Obj_Def));
16991 return Entity (Subtype_Mark (Obj_Def));
16996 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
16998 T := Make_Defining_Identifier (Sloc (P), Nam);
17000 Insert_Action (Obj_Def,
17001 Make_Subtype_Declaration (Sloc (P),
17002 Defining_Identifier => T,
17003 Subtype_Indication => Relocate_Node (Obj_Def)));
17005 -- This subtype may need freezing, and this will not be done
17006 -- automatically if the object declaration is not in declarative
17007 -- part. Since this is an object declaration, the type cannot always
17008 -- be frozen here. Deferred constants do not freeze their type
17009 -- (which often enough will be private).
17011 if Nkind (P) = N_Object_Declaration
17012 and then Constant_Present (P)
17013 and then No (Expression (P))
17017 -- Here we freeze the base type of object type to catch premature use
17018 -- of discriminated private type without a full view.
17021 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17024 -- Ada 2005 AI-406: the object definition in an object declaration
17025 -- can be an access definition.
17027 elsif Def_Kind = N_Access_Definition then
17028 T := Access_Definition (Related_Nod, Obj_Def);
17030 Set_Is_Local_Anonymous_Access
17032 V => (Ada_Version < Ada_2012)
17033 or else (Nkind (P) /= N_Object_Declaration)
17034 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17036 -- Otherwise, the object definition is just a subtype_mark
17039 T := Process_Subtype (Obj_Def, Related_Nod);
17041 -- If expansion is disabled an object definition that is an aggregate
17042 -- will not get expanded and may lead to scoping problems in the back
17043 -- end, if the object is referenced in an inner scope. In that case
17044 -- create an itype reference for the object definition now. This
17045 -- may be redundant in some cases, but harmless.
17048 and then Nkind (Related_Nod) = N_Object_Declaration
17051 Build_Itype_Reference (T, Related_Nod);
17056 end Find_Type_Of_Object;
17058 --------------------------------
17059 -- Find_Type_Of_Subtype_Indic --
17060 --------------------------------
17062 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17066 -- Case of subtype mark with a constraint
17068 if Nkind (S) = N_Subtype_Indication then
17069 Find_Type (Subtype_Mark (S));
17070 Typ := Entity (Subtype_Mark (S));
17073 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17076 ("incorrect constraint for this kind of type", Constraint (S));
17077 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17080 -- Otherwise we have a subtype mark without a constraint
17082 elsif Error_Posted (S) then
17083 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17091 -- Check No_Wide_Characters restriction
17093 Check_Wide_Character_Restriction (Typ, S);
17096 end Find_Type_Of_Subtype_Indic;
17098 -------------------------------------
17099 -- Floating_Point_Type_Declaration --
17100 -------------------------------------
17102 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17103 Digs : constant Node_Id := Digits_Expression (Def);
17104 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17106 Base_Typ : Entity_Id;
17107 Implicit_Base : Entity_Id;
17110 function Can_Derive_From (E : Entity_Id) return Boolean;
17111 -- Find if given digits value, and possibly a specified range, allows
17112 -- derivation from specified type
17114 function Find_Base_Type return Entity_Id;
17115 -- Find a predefined base type that Def can derive from, or generate
17116 -- an error and substitute Long_Long_Float if none exists.
17118 ---------------------
17119 -- Can_Derive_From --
17120 ---------------------
17122 function Can_Derive_From (E : Entity_Id) return Boolean is
17123 Spec : constant Entity_Id := Real_Range_Specification (Def);
17126 -- Check specified "digits" constraint
17128 if Digs_Val > Digits_Value (E) then
17132 -- Check for matching range, if specified
17134 if Present (Spec) then
17135 if Expr_Value_R (Type_Low_Bound (E)) >
17136 Expr_Value_R (Low_Bound (Spec))
17141 if Expr_Value_R (Type_High_Bound (E)) <
17142 Expr_Value_R (High_Bound (Spec))
17149 end Can_Derive_From;
17151 --------------------
17152 -- Find_Base_Type --
17153 --------------------
17155 function Find_Base_Type return Entity_Id is
17156 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17159 -- Iterate over the predefined types in order, returning the first
17160 -- one that Def can derive from.
17162 while Present (Choice) loop
17163 if Can_Derive_From (Node (Choice)) then
17164 return Node (Choice);
17167 Next_Elmt (Choice);
17170 -- If we can't derive from any existing type, use Long_Long_Float
17171 -- and give appropriate message explaining the problem.
17173 if Digs_Val > Max_Digs_Val then
17174 -- It might be the case that there is a type with the requested
17175 -- range, just not the combination of digits and range.
17178 ("no predefined type has requested range and precision",
17179 Real_Range_Specification (Def));
17183 ("range too large for any predefined type",
17184 Real_Range_Specification (Def));
17187 return Standard_Long_Long_Float;
17188 end Find_Base_Type;
17190 -- Start of processing for Floating_Point_Type_Declaration
17193 Check_Restriction (No_Floating_Point, Def);
17195 -- Create an implicit base type
17198 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17200 -- Analyze and verify digits value
17202 Analyze_And_Resolve (Digs, Any_Integer);
17203 Check_Digits_Expression (Digs);
17204 Digs_Val := Expr_Value (Digs);
17206 -- Process possible range spec and find correct type to derive from
17208 Process_Real_Range_Specification (Def);
17210 -- Check that requested number of digits is not too high.
17212 if Digs_Val > Max_Digs_Val then
17214 -- The check for Max_Base_Digits may be somewhat expensive, as it
17215 -- requires reading System, so only do it when necessary.
17218 Max_Base_Digits : constant Uint :=
17221 (Parent (RTE (RE_Max_Base_Digits))));
17224 if Digs_Val > Max_Base_Digits then
17225 Error_Msg_Uint_1 := Max_Base_Digits;
17226 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17228 elsif No (Real_Range_Specification (Def)) then
17229 Error_Msg_Uint_1 := Max_Digs_Val;
17230 Error_Msg_N ("types with more than ^ digits need range spec "
17231 & "(RM 3.5.7(6))", Digs);
17236 -- Find a suitable type to derive from or complain and use a substitute
17238 Base_Typ := Find_Base_Type;
17240 -- If there are bounds given in the declaration use them as the bounds
17241 -- of the type, otherwise use the bounds of the predefined base type
17242 -- that was chosen based on the Digits value.
17244 if Present (Real_Range_Specification (Def)) then
17245 Set_Scalar_Range (T, Real_Range_Specification (Def));
17246 Set_Is_Constrained (T);
17248 -- The bounds of this range must be converted to machine numbers
17249 -- in accordance with RM 4.9(38).
17251 Bound := Type_Low_Bound (T);
17253 if Nkind (Bound) = N_Real_Literal then
17255 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17256 Set_Is_Machine_Number (Bound);
17259 Bound := Type_High_Bound (T);
17261 if Nkind (Bound) = N_Real_Literal then
17263 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17264 Set_Is_Machine_Number (Bound);
17268 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17271 -- Complete definition of implicit base and declared first subtype. The
17272 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17273 -- are not clobbered when the floating point type acts as a full view of
17276 Set_Etype (Implicit_Base, Base_Typ);
17277 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17278 Set_Size_Info (Implicit_Base, Base_Typ);
17279 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17280 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17281 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17282 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17284 Set_Ekind (T, E_Floating_Point_Subtype);
17285 Set_Etype (T, Implicit_Base);
17286 Set_Size_Info (T, Implicit_Base);
17287 Set_RM_Size (T, RM_Size (Implicit_Base));
17288 Inherit_Rep_Item_Chain (T, Implicit_Base);
17289 Set_Digits_Value (T, Digs_Val);
17290 end Floating_Point_Type_Declaration;
17292 ----------------------------
17293 -- Get_Discriminant_Value --
17294 ----------------------------
17296 -- This is the situation:
17298 -- There is a non-derived type
17300 -- type T0 (Dx, Dy, Dz...)
17302 -- There are zero or more levels of derivation, with each derivation
17303 -- either purely inheriting the discriminants, or defining its own.
17305 -- type Ti is new Ti-1
17307 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17309 -- subtype Ti is ...
17311 -- The subtype issue is avoided by the use of Original_Record_Component,
17312 -- and the fact that derived subtypes also derive the constraints.
17314 -- This chain leads back from
17316 -- Typ_For_Constraint
17318 -- Typ_For_Constraint has discriminants, and the value for each
17319 -- discriminant is given by its corresponding Elmt of Constraints.
17321 -- Discriminant is some discriminant in this hierarchy
17323 -- We need to return its value
17325 -- We do this by recursively searching each level, and looking for
17326 -- Discriminant. Once we get to the bottom, we start backing up
17327 -- returning the value for it which may in turn be a discriminant
17328 -- further up, so on the backup we continue the substitution.
17330 function Get_Discriminant_Value
17331 (Discriminant : Entity_Id;
17332 Typ_For_Constraint : Entity_Id;
17333 Constraint : Elist_Id) return Node_Id
17335 function Root_Corresponding_Discriminant
17336 (Discr : Entity_Id) return Entity_Id;
17337 -- Given a discriminant, traverse the chain of inherited discriminants
17338 -- and return the topmost discriminant.
17340 function Search_Derivation_Levels
17342 Discrim_Values : Elist_Id;
17343 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17344 -- This is the routine that performs the recursive search of levels
17345 -- as described above.
17347 -------------------------------------
17348 -- Root_Corresponding_Discriminant --
17349 -------------------------------------
17351 function Root_Corresponding_Discriminant
17352 (Discr : Entity_Id) return Entity_Id
17358 while Present (Corresponding_Discriminant (D)) loop
17359 D := Corresponding_Discriminant (D);
17363 end Root_Corresponding_Discriminant;
17365 ------------------------------
17366 -- Search_Derivation_Levels --
17367 ------------------------------
17369 function Search_Derivation_Levels
17371 Discrim_Values : Elist_Id;
17372 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17376 Result : Node_Or_Entity_Id;
17377 Result_Entity : Node_Id;
17380 -- If inappropriate type, return Error, this happens only in
17381 -- cascaded error situations, and we want to avoid a blow up.
17383 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17387 -- Look deeper if possible. Use Stored_Constraints only for
17388 -- untagged types. For tagged types use the given constraint.
17389 -- This asymmetry needs explanation???
17391 if not Stored_Discrim_Values
17392 and then Present (Stored_Constraint (Ti))
17393 and then not Is_Tagged_Type (Ti)
17396 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17399 Td : constant Entity_Id := Etype (Ti);
17403 Result := Discriminant;
17406 if Present (Stored_Constraint (Ti)) then
17408 Search_Derivation_Levels
17409 (Td, Stored_Constraint (Ti), True);
17412 Search_Derivation_Levels
17413 (Td, Discrim_Values, Stored_Discrim_Values);
17419 -- Extra underlying places to search, if not found above. For
17420 -- concurrent types, the relevant discriminant appears in the
17421 -- corresponding record. For a type derived from a private type
17422 -- without discriminant, the full view inherits the discriminants
17423 -- of the full view of the parent.
17425 if Result = Discriminant then
17426 if Is_Concurrent_Type (Ti)
17427 and then Present (Corresponding_Record_Type (Ti))
17430 Search_Derivation_Levels (
17431 Corresponding_Record_Type (Ti),
17433 Stored_Discrim_Values);
17435 elsif Is_Private_Type (Ti)
17436 and then not Has_Discriminants (Ti)
17437 and then Present (Full_View (Ti))
17438 and then Etype (Full_View (Ti)) /= Ti
17441 Search_Derivation_Levels (
17444 Stored_Discrim_Values);
17448 -- If Result is not a (reference to a) discriminant, return it,
17449 -- otherwise set Result_Entity to the discriminant.
17451 if Nkind (Result) = N_Defining_Identifier then
17452 pragma Assert (Result = Discriminant);
17453 Result_Entity := Result;
17456 if not Denotes_Discriminant (Result) then
17460 Result_Entity := Entity (Result);
17463 -- See if this level of derivation actually has discriminants because
17464 -- tagged derivations can add them, hence the lower levels need not
17467 if not Has_Discriminants (Ti) then
17471 -- Scan Ti's discriminants for Result_Entity, and return its
17472 -- corresponding value, if any.
17474 Result_Entity := Original_Record_Component (Result_Entity);
17476 Assoc := First_Elmt (Discrim_Values);
17478 if Stored_Discrim_Values then
17479 Disc := First_Stored_Discriminant (Ti);
17481 Disc := First_Discriminant (Ti);
17484 while Present (Disc) loop
17485 pragma Assert (Present (Assoc));
17487 if Original_Record_Component (Disc) = Result_Entity then
17488 return Node (Assoc);
17493 if Stored_Discrim_Values then
17494 Next_Stored_Discriminant (Disc);
17496 Next_Discriminant (Disc);
17500 -- Could not find it
17503 end Search_Derivation_Levels;
17507 Result : Node_Or_Entity_Id;
17509 -- Start of processing for Get_Discriminant_Value
17512 -- ??? This routine is a gigantic mess and will be deleted. For the
17513 -- time being just test for the trivial case before calling recurse.
17515 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17521 D := First_Discriminant (Typ_For_Constraint);
17522 E := First_Elmt (Constraint);
17523 while Present (D) loop
17524 if Chars (D) = Chars (Discriminant) then
17528 Next_Discriminant (D);
17534 Result := Search_Derivation_Levels
17535 (Typ_For_Constraint, Constraint, False);
17537 -- ??? hack to disappear when this routine is gone
17539 if Nkind (Result) = N_Defining_Identifier then
17545 D := First_Discriminant (Typ_For_Constraint);
17546 E := First_Elmt (Constraint);
17547 while Present (D) loop
17548 if Root_Corresponding_Discriminant (D) = Discriminant then
17552 Next_Discriminant (D);
17558 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17560 end Get_Discriminant_Value;
17562 --------------------------
17563 -- Has_Range_Constraint --
17564 --------------------------
17566 function Has_Range_Constraint (N : Node_Id) return Boolean is
17567 C : constant Node_Id := Constraint (N);
17570 if Nkind (C) = N_Range_Constraint then
17573 elsif Nkind (C) = N_Digits_Constraint then
17575 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17576 or else Present (Range_Constraint (C));
17578 elsif Nkind (C) = N_Delta_Constraint then
17579 return Present (Range_Constraint (C));
17584 end Has_Range_Constraint;
17586 ------------------------
17587 -- Inherit_Components --
17588 ------------------------
17590 function Inherit_Components
17592 Parent_Base : Entity_Id;
17593 Derived_Base : Entity_Id;
17594 Is_Tagged : Boolean;
17595 Inherit_Discr : Boolean;
17596 Discs : Elist_Id) return Elist_Id
17598 Assoc_List : constant Elist_Id := New_Elmt_List;
17600 procedure Inherit_Component
17601 (Old_C : Entity_Id;
17602 Plain_Discrim : Boolean := False;
17603 Stored_Discrim : Boolean := False);
17604 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17605 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17606 -- True, Old_C is a stored discriminant. If they are both false then
17607 -- Old_C is a regular component.
17609 -----------------------
17610 -- Inherit_Component --
17611 -----------------------
17613 procedure Inherit_Component
17614 (Old_C : Entity_Id;
17615 Plain_Discrim : Boolean := False;
17616 Stored_Discrim : Boolean := False)
17618 procedure Set_Anonymous_Type (Id : Entity_Id);
17619 -- Id denotes the entity of an access discriminant or anonymous
17620 -- access component. Set the type of Id to either the same type of
17621 -- Old_C or create a new one depending on whether the parent and
17622 -- the child types are in the same scope.
17624 ------------------------
17625 -- Set_Anonymous_Type --
17626 ------------------------
17628 procedure Set_Anonymous_Type (Id : Entity_Id) is
17629 Old_Typ : constant Entity_Id := Etype (Old_C);
17632 if Scope (Parent_Base) = Scope (Derived_Base) then
17633 Set_Etype (Id, Old_Typ);
17635 -- The parent and the derived type are in two different scopes.
17636 -- Reuse the type of the original discriminant / component by
17637 -- copying it in order to preserve all attributes.
17641 Typ : constant Entity_Id := New_Copy (Old_Typ);
17644 Set_Etype (Id, Typ);
17646 -- Since we do not generate component declarations for
17647 -- inherited components, associate the itype with the
17650 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17651 Set_Scope (Typ, Derived_Base);
17654 end Set_Anonymous_Type;
17656 -- Local variables and constants
17658 New_C : constant Entity_Id := New_Copy (Old_C);
17660 Corr_Discrim : Entity_Id;
17661 Discrim : Entity_Id;
17663 -- Start of processing for Inherit_Component
17666 pragma Assert (not Is_Tagged or not Stored_Discrim);
17668 Set_Parent (New_C, Parent (Old_C));
17670 -- Regular discriminants and components must be inserted in the scope
17671 -- of the Derived_Base. Do it here.
17673 if not Stored_Discrim then
17674 Enter_Name (New_C);
17677 -- For tagged types the Original_Record_Component must point to
17678 -- whatever this field was pointing to in the parent type. This has
17679 -- already been achieved by the call to New_Copy above.
17681 if not Is_Tagged then
17682 Set_Original_Record_Component (New_C, New_C);
17685 -- Set the proper type of an access discriminant
17687 if Ekind (New_C) = E_Discriminant
17688 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17690 Set_Anonymous_Type (New_C);
17693 -- If we have inherited a component then see if its Etype contains
17694 -- references to Parent_Base discriminants. In this case, replace
17695 -- these references with the constraints given in Discs. We do not
17696 -- do this for the partial view of private types because this is
17697 -- not needed (only the components of the full view will be used
17698 -- for code generation) and cause problem. We also avoid this
17699 -- transformation in some error situations.
17701 if Ekind (New_C) = E_Component then
17703 -- Set the proper type of an anonymous access component
17705 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17706 Set_Anonymous_Type (New_C);
17708 elsif (Is_Private_Type (Derived_Base)
17709 and then not Is_Generic_Type (Derived_Base))
17710 or else (Is_Empty_Elmt_List (Discs)
17711 and then not Expander_Active)
17713 Set_Etype (New_C, Etype (Old_C));
17716 -- The current component introduces a circularity of the
17719 -- limited with Pack_2;
17720 -- package Pack_1 is
17721 -- type T_1 is tagged record
17722 -- Comp : access Pack_2.T_2;
17728 -- package Pack_2 is
17729 -- type T_2 is new Pack_1.T_1 with ...;
17734 Constrain_Component_Type
17735 (Old_C, Derived_Base, N, Parent_Base, Discs));
17739 -- In derived tagged types it is illegal to reference a non
17740 -- discriminant component in the parent type. To catch this, mark
17741 -- these components with an Ekind of E_Void. This will be reset in
17742 -- Record_Type_Definition after processing the record extension of
17743 -- the derived type.
17745 -- If the declaration is a private extension, there is no further
17746 -- record extension to process, and the components retain their
17747 -- current kind, because they are visible at this point.
17749 if Is_Tagged and then Ekind (New_C) = E_Component
17750 and then Nkind (N) /= N_Private_Extension_Declaration
17752 Set_Ekind (New_C, E_Void);
17755 if Plain_Discrim then
17756 Set_Corresponding_Discriminant (New_C, Old_C);
17757 Build_Discriminal (New_C);
17759 -- If we are explicitly inheriting a stored discriminant it will be
17760 -- completely hidden.
17762 elsif Stored_Discrim then
17763 Set_Corresponding_Discriminant (New_C, Empty);
17764 Set_Discriminal (New_C, Empty);
17765 Set_Is_Completely_Hidden (New_C);
17767 -- Set the Original_Record_Component of each discriminant in the
17768 -- derived base to point to the corresponding stored that we just
17771 Discrim := First_Discriminant (Derived_Base);
17772 while Present (Discrim) loop
17773 Corr_Discrim := Corresponding_Discriminant (Discrim);
17775 -- Corr_Discrim could be missing in an error situation
17777 if Present (Corr_Discrim)
17778 and then Original_Record_Component (Corr_Discrim) = Old_C
17780 Set_Original_Record_Component (Discrim, New_C);
17783 Next_Discriminant (Discrim);
17786 Append_Entity (New_C, Derived_Base);
17789 if not Is_Tagged then
17790 Append_Elmt (Old_C, Assoc_List);
17791 Append_Elmt (New_C, Assoc_List);
17793 end Inherit_Component;
17795 -- Variables local to Inherit_Component
17797 Loc : constant Source_Ptr := Sloc (N);
17799 Parent_Discrim : Entity_Id;
17800 Stored_Discrim : Entity_Id;
17802 Component : Entity_Id;
17804 -- Start of processing for Inherit_Components
17807 if not Is_Tagged then
17808 Append_Elmt (Parent_Base, Assoc_List);
17809 Append_Elmt (Derived_Base, Assoc_List);
17812 -- Inherit parent discriminants if needed
17814 if Inherit_Discr then
17815 Parent_Discrim := First_Discriminant (Parent_Base);
17816 while Present (Parent_Discrim) loop
17817 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17818 Next_Discriminant (Parent_Discrim);
17822 -- Create explicit stored discrims for untagged types when necessary
17824 if not Has_Unknown_Discriminants (Derived_Base)
17825 and then Has_Discriminants (Parent_Base)
17826 and then not Is_Tagged
17829 or else First_Discriminant (Parent_Base) /=
17830 First_Stored_Discriminant (Parent_Base))
17832 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
17833 while Present (Stored_Discrim) loop
17834 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
17835 Next_Stored_Discriminant (Stored_Discrim);
17839 -- See if we can apply the second transformation for derived types, as
17840 -- explained in point 6. in the comments above Build_Derived_Record_Type
17841 -- This is achieved by appending Derived_Base discriminants into Discs,
17842 -- which has the side effect of returning a non empty Discs list to the
17843 -- caller of Inherit_Components, which is what we want. This must be
17844 -- done for private derived types if there are explicit stored
17845 -- discriminants, to ensure that we can retrieve the values of the
17846 -- constraints provided in the ancestors.
17849 and then Is_Empty_Elmt_List (Discs)
17850 and then Present (First_Discriminant (Derived_Base))
17852 (not Is_Private_Type (Derived_Base)
17853 or else Is_Completely_Hidden
17854 (First_Stored_Discriminant (Derived_Base))
17855 or else Is_Generic_Type (Derived_Base))
17857 D := First_Discriminant (Derived_Base);
17858 while Present (D) loop
17859 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
17860 Next_Discriminant (D);
17864 -- Finally, inherit non-discriminant components unless they are not
17865 -- visible because defined or inherited from the full view of the
17866 -- parent. Don't inherit the _parent field of the parent type.
17868 Component := First_Entity (Parent_Base);
17869 while Present (Component) loop
17871 -- Ada 2005 (AI-251): Do not inherit components associated with
17872 -- secondary tags of the parent.
17874 if Ekind (Component) = E_Component
17875 and then Present (Related_Type (Component))
17879 elsif Ekind (Component) /= E_Component
17880 or else Chars (Component) = Name_uParent
17884 -- If the derived type is within the parent type's declarative
17885 -- region, then the components can still be inherited even though
17886 -- they aren't visible at this point. This can occur for cases
17887 -- such as within public child units where the components must
17888 -- become visible upon entering the child unit's private part.
17890 elsif not Is_Visible_Component (Component)
17891 and then not In_Open_Scopes (Scope (Parent_Base))
17895 elsif Ekind_In (Derived_Base, E_Private_Type,
17896 E_Limited_Private_Type)
17901 Inherit_Component (Component);
17904 Next_Entity (Component);
17907 -- For tagged derived types, inherited discriminants cannot be used in
17908 -- component declarations of the record extension part. To achieve this
17909 -- we mark the inherited discriminants as not visible.
17911 if Is_Tagged and then Inherit_Discr then
17912 D := First_Discriminant (Derived_Base);
17913 while Present (D) loop
17914 Set_Is_Immediately_Visible (D, False);
17915 Next_Discriminant (D);
17920 end Inherit_Components;
17922 -----------------------------
17923 -- Inherit_Predicate_Flags --
17924 -----------------------------
17926 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
17928 Set_Has_Predicates (Subt, Has_Predicates (Par));
17929 Set_Has_Static_Predicate_Aspect
17930 (Subt, Has_Static_Predicate_Aspect (Par));
17931 Set_Has_Dynamic_Predicate_Aspect
17932 (Subt, Has_Dynamic_Predicate_Aspect (Par));
17933 end Inherit_Predicate_Flags;
17935 ----------------------
17936 -- Is_EVF_Procedure --
17937 ----------------------
17939 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
17940 Formal : Entity_Id;
17943 -- Examine the formals of an Extensions_Visible False procedure looking
17944 -- for a controlling OUT parameter.
17946 if Ekind (Subp) = E_Procedure
17947 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
17949 Formal := First_Formal (Subp);
17950 while Present (Formal) loop
17951 if Ekind (Formal) = E_Out_Parameter
17952 and then Is_Controlling_Formal (Formal)
17957 Next_Formal (Formal);
17962 end Is_EVF_Procedure;
17964 -----------------------
17965 -- Is_Null_Extension --
17966 -----------------------
17968 function Is_Null_Extension (T : Entity_Id) return Boolean is
17969 Type_Decl : constant Node_Id := Parent (Base_Type (T));
17970 Comp_List : Node_Id;
17974 if Nkind (Type_Decl) /= N_Full_Type_Declaration
17975 or else not Is_Tagged_Type (T)
17976 or else Nkind (Type_Definition (Type_Decl)) /=
17977 N_Derived_Type_Definition
17978 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
17984 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
17986 if Present (Discriminant_Specifications (Type_Decl)) then
17989 elsif Present (Comp_List)
17990 and then Is_Non_Empty_List (Component_Items (Comp_List))
17992 Comp := First (Component_Items (Comp_List));
17994 -- Only user-defined components are relevant. The component list
17995 -- may also contain a parent component and internal components
17996 -- corresponding to secondary tags, but these do not determine
17997 -- whether this is a null extension.
17999 while Present (Comp) loop
18000 if Comes_From_Source (Comp) then
18012 end Is_Null_Extension;
18014 ------------------------------
18015 -- Is_Valid_Constraint_Kind --
18016 ------------------------------
18018 function Is_Valid_Constraint_Kind
18019 (T_Kind : Type_Kind;
18020 Constraint_Kind : Node_Kind) return Boolean
18024 when Enumeration_Kind |
18026 return Constraint_Kind = N_Range_Constraint;
18028 when Decimal_Fixed_Point_Kind =>
18029 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18030 N_Range_Constraint);
18032 when Ordinary_Fixed_Point_Kind =>
18033 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18034 N_Range_Constraint);
18037 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18038 N_Range_Constraint);
18045 E_Incomplete_Type |
18048 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18051 return True; -- Error will be detected later
18053 end Is_Valid_Constraint_Kind;
18055 --------------------------
18056 -- Is_Visible_Component --
18057 --------------------------
18059 function Is_Visible_Component
18061 N : Node_Id := Empty) return Boolean
18063 Original_Comp : Entity_Id := Empty;
18064 Original_Type : Entity_Id;
18065 Type_Scope : Entity_Id;
18067 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18068 -- Check whether parent type of inherited component is declared locally,
18069 -- possibly within a nested package or instance. The current scope is
18070 -- the derived record itself.
18072 -------------------
18073 -- Is_Local_Type --
18074 -------------------
18076 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18080 Scop := Scope (Typ);
18081 while Present (Scop)
18082 and then Scop /= Standard_Standard
18084 if Scop = Scope (Current_Scope) then
18088 Scop := Scope (Scop);
18094 -- Start of processing for Is_Visible_Component
18097 if Ekind_In (C, E_Component, E_Discriminant) then
18098 Original_Comp := Original_Record_Component (C);
18101 if No (Original_Comp) then
18103 -- Premature usage, or previous error
18108 Original_Type := Scope (Original_Comp);
18109 Type_Scope := Scope (Base_Type (Scope (C)));
18112 -- This test only concerns tagged types
18114 if not Is_Tagged_Type (Original_Type) then
18117 -- If it is _Parent or _Tag, there is no visibility issue
18119 elsif not Comes_From_Source (Original_Comp) then
18122 -- Discriminants are visible unless the (private) type has unknown
18123 -- discriminants. If the discriminant reference is inserted for a
18124 -- discriminant check on a full view it is also visible.
18126 elsif Ekind (Original_Comp) = E_Discriminant
18128 (not Has_Unknown_Discriminants (Original_Type)
18129 or else (Present (N)
18130 and then Nkind (N) = N_Selected_Component
18131 and then Nkind (Prefix (N)) = N_Type_Conversion
18132 and then not Comes_From_Source (Prefix (N))))
18136 -- In the body of an instantiation, check the visibility of a component
18137 -- in case it has a homograph that is a primitive operation of a private
18138 -- type which was not visible in the generic unit.
18140 -- Should Is_Prefixed_Call be propagated from template to instance???
18142 elsif In_Instance_Body then
18143 if not Is_Tagged_Type (Original_Type)
18144 or else not Is_Private_Type (Original_Type)
18150 Subp_Elmt : Elmt_Id;
18153 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18154 while Present (Subp_Elmt) loop
18156 -- The component is hidden by a primitive operation
18158 if Chars (Node (Subp_Elmt)) = Chars (C) then
18162 Next_Elmt (Subp_Elmt);
18169 -- If the component has been declared in an ancestor which is currently
18170 -- a private type, then it is not visible. The same applies if the
18171 -- component's containing type is not in an open scope and the original
18172 -- component's enclosing type is a visible full view of a private type
18173 -- (which can occur in cases where an attempt is being made to reference
18174 -- a component in a sibling package that is inherited from a visible
18175 -- component of a type in an ancestor package; the component in the
18176 -- sibling package should not be visible even though the component it
18177 -- inherited from is visible). This does not apply however in the case
18178 -- where the scope of the type is a private child unit, or when the
18179 -- parent comes from a local package in which the ancestor is currently
18180 -- visible. The latter suppression of visibility is needed for cases
18181 -- that are tested in B730006.
18183 elsif Is_Private_Type (Original_Type)
18185 (not Is_Private_Descendant (Type_Scope)
18186 and then not In_Open_Scopes (Type_Scope)
18187 and then Has_Private_Declaration (Original_Type))
18189 -- If the type derives from an entity in a formal package, there
18190 -- are no additional visible components.
18192 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18193 N_Formal_Package_Declaration
18197 -- if we are not in the private part of the current package, there
18198 -- are no additional visible components.
18200 elsif Ekind (Scope (Current_Scope)) = E_Package
18201 and then not In_Private_Part (Scope (Current_Scope))
18206 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18207 and then In_Open_Scopes (Scope (Original_Type))
18208 and then Is_Local_Type (Type_Scope);
18211 -- There is another weird way in which a component may be invisible when
18212 -- the private and the full view are not derived from the same ancestor.
18213 -- Here is an example :
18215 -- type A1 is tagged record F1 : integer; end record;
18216 -- type A2 is new A1 with record F2 : integer; end record;
18217 -- type T is new A1 with private;
18219 -- type T is new A2 with null record;
18221 -- In this case, the full view of T inherits F1 and F2 but the private
18222 -- view inherits only F1
18226 Ancestor : Entity_Id := Scope (C);
18230 if Ancestor = Original_Type then
18233 -- The ancestor may have a partial view of the original type,
18234 -- but if the full view is in scope, as in a child body, the
18235 -- component is visible.
18237 elsif In_Private_Part (Scope (Original_Type))
18238 and then Full_View (Ancestor) = Original_Type
18242 elsif Ancestor = Etype (Ancestor) then
18244 -- No further ancestors to examine
18249 Ancestor := Etype (Ancestor);
18253 end Is_Visible_Component;
18255 --------------------------
18256 -- Make_Class_Wide_Type --
18257 --------------------------
18259 procedure Make_Class_Wide_Type (T : Entity_Id) is
18260 CW_Type : Entity_Id;
18262 Next_E : Entity_Id;
18265 if Present (Class_Wide_Type (T)) then
18267 -- The class-wide type is a partially decorated entity created for a
18268 -- unanalyzed tagged type referenced through a limited with clause.
18269 -- When the tagged type is analyzed, its class-wide type needs to be
18270 -- redecorated. Note that we reuse the entity created by Decorate_
18271 -- Tagged_Type in order to preserve all links.
18273 if Materialize_Entity (Class_Wide_Type (T)) then
18274 CW_Type := Class_Wide_Type (T);
18275 Set_Materialize_Entity (CW_Type, False);
18277 -- The class wide type can have been defined by the partial view, in
18278 -- which case everything is already done.
18284 -- Default case, we need to create a new class-wide type
18288 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18291 -- Inherit root type characteristics
18293 CW_Name := Chars (CW_Type);
18294 Next_E := Next_Entity (CW_Type);
18295 Copy_Node (T, CW_Type);
18296 Set_Comes_From_Source (CW_Type, False);
18297 Set_Chars (CW_Type, CW_Name);
18298 Set_Parent (CW_Type, Parent (T));
18299 Set_Next_Entity (CW_Type, Next_E);
18301 -- Ensure we have a new freeze node for the class-wide type. The partial
18302 -- view may have freeze action of its own, requiring a proper freeze
18303 -- node, and the same freeze node cannot be shared between the two
18306 Set_Has_Delayed_Freeze (CW_Type);
18307 Set_Freeze_Node (CW_Type, Empty);
18309 -- Customize the class-wide type: It has no prim. op., it cannot be
18310 -- abstract, its Etype points back to the specific root type, and it
18311 -- cannot have any invariants.
18313 Set_Ekind (CW_Type, E_Class_Wide_Type);
18314 Set_Is_Tagged_Type (CW_Type, True);
18315 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18316 Set_Is_Abstract_Type (CW_Type, False);
18317 Set_Is_Constrained (CW_Type, False);
18318 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18319 Set_Default_SSO (CW_Type);
18320 Set_Has_Inheritable_Invariants (CW_Type, False);
18321 Set_Has_Inherited_Invariants (CW_Type, False);
18322 Set_Has_Own_Invariants (CW_Type, False);
18324 if Ekind (T) = E_Class_Wide_Subtype then
18325 Set_Etype (CW_Type, Etype (Base_Type (T)));
18327 Set_Etype (CW_Type, T);
18330 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18332 -- If this is the class_wide type of a constrained subtype, it does
18333 -- not have discriminants.
18335 Set_Has_Discriminants (CW_Type,
18336 Has_Discriminants (T) and then not Is_Constrained (T));
18338 Set_Has_Unknown_Discriminants (CW_Type, True);
18339 Set_Class_Wide_Type (T, CW_Type);
18340 Set_Equivalent_Type (CW_Type, Empty);
18342 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18344 Set_Class_Wide_Type (CW_Type, CW_Type);
18346 -- Inherit the "ghostness" from the root tagged type
18348 if Ghost_Mode > None or else Is_Ghost_Entity (T) then
18349 Set_Is_Ghost_Entity (CW_Type);
18351 end Make_Class_Wide_Type;
18357 procedure Make_Index
18359 Related_Nod : Node_Id;
18360 Related_Id : Entity_Id := Empty;
18361 Suffix_Index : Nat := 1;
18362 In_Iter_Schm : Boolean := False)
18366 Def_Id : Entity_Id := Empty;
18367 Found : Boolean := False;
18370 -- For a discrete range used in a constrained array definition and
18371 -- defined by a range, an implicit conversion to the predefined type
18372 -- INTEGER is assumed if each bound is either a numeric literal, a named
18373 -- number, or an attribute, and the type of both bounds (prior to the
18374 -- implicit conversion) is the type universal_integer. Otherwise, both
18375 -- bounds must be of the same discrete type, other than universal
18376 -- integer; this type must be determinable independently of the
18377 -- context, but using the fact that the type must be discrete and that
18378 -- both bounds must have the same type.
18380 -- Character literals also have a universal type in the absence of
18381 -- of additional context, and are resolved to Standard_Character.
18383 if Nkind (N) = N_Range then
18385 -- The index is given by a range constraint. The bounds are known
18386 -- to be of a consistent type.
18388 if not Is_Overloaded (N) then
18391 -- For universal bounds, choose the specific predefined type
18393 if T = Universal_Integer then
18394 T := Standard_Integer;
18396 elsif T = Any_Character then
18397 Ambiguous_Character (Low_Bound (N));
18399 T := Standard_Character;
18402 -- The node may be overloaded because some user-defined operators
18403 -- are available, but if a universal interpretation exists it is
18404 -- also the selected one.
18406 elsif Universal_Interpretation (N) = Universal_Integer then
18407 T := Standard_Integer;
18413 Ind : Interp_Index;
18417 Get_First_Interp (N, Ind, It);
18418 while Present (It.Typ) loop
18419 if Is_Discrete_Type (It.Typ) then
18422 and then not Covers (It.Typ, T)
18423 and then not Covers (T, It.Typ)
18425 Error_Msg_N ("ambiguous bounds in discrete range", N);
18433 Get_Next_Interp (Ind, It);
18436 if T = Any_Type then
18437 Error_Msg_N ("discrete type required for range", N);
18438 Set_Etype (N, Any_Type);
18441 elsif T = Universal_Integer then
18442 T := Standard_Integer;
18447 if not Is_Discrete_Type (T) then
18448 Error_Msg_N ("discrete type required for range", N);
18449 Set_Etype (N, Any_Type);
18453 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18454 and then Attribute_Name (Low_Bound (N)) = Name_First
18455 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18456 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18457 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18459 -- The type of the index will be the type of the prefix, as long
18460 -- as the upper bound is 'Last of the same type.
18462 Def_Id := Entity (Prefix (Low_Bound (N)));
18464 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18465 or else Attribute_Name (High_Bound (N)) /= Name_Last
18466 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18467 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18474 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18476 elsif Nkind (N) = N_Subtype_Indication then
18478 -- The index is given by a subtype with a range constraint
18480 T := Base_Type (Entity (Subtype_Mark (N)));
18482 if not Is_Discrete_Type (T) then
18483 Error_Msg_N ("discrete type required for range", N);
18484 Set_Etype (N, Any_Type);
18488 R := Range_Expression (Constraint (N));
18491 Process_Range_Expr_In_Decl
18492 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18494 elsif Nkind (N) = N_Attribute_Reference then
18496 -- Catch beginner's error (use of attribute other than 'Range)
18498 if Attribute_Name (N) /= Name_Range then
18499 Error_Msg_N ("expect attribute ''Range", N);
18500 Set_Etype (N, Any_Type);
18504 -- If the node denotes the range of a type mark, that is also the
18505 -- resulting type, and we do not need to create an Itype for it.
18507 if Is_Entity_Name (Prefix (N))
18508 and then Comes_From_Source (N)
18509 and then Is_Type (Entity (Prefix (N)))
18510 and then Is_Discrete_Type (Entity (Prefix (N)))
18512 Def_Id := Entity (Prefix (N));
18515 Analyze_And_Resolve (N);
18519 -- If none of the above, must be a subtype. We convert this to a
18520 -- range attribute reference because in the case of declared first
18521 -- named subtypes, the types in the range reference can be different
18522 -- from the type of the entity. A range attribute normalizes the
18523 -- reference and obtains the correct types for the bounds.
18525 -- This transformation is in the nature of an expansion, is only
18526 -- done if expansion is active. In particular, it is not done on
18527 -- formal generic types, because we need to retain the name of the
18528 -- original index for instantiation purposes.
18531 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18532 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18533 Set_Etype (N, Any_Integer);
18537 -- The type mark may be that of an incomplete type. It is only
18538 -- now that we can get the full view, previous analysis does
18539 -- not look specifically for a type mark.
18541 Set_Entity (N, Get_Full_View (Entity (N)));
18542 Set_Etype (N, Entity (N));
18543 Def_Id := Entity (N);
18545 if not Is_Discrete_Type (Def_Id) then
18546 Error_Msg_N ("discrete type required for index", N);
18547 Set_Etype (N, Any_Type);
18552 if Expander_Active then
18554 Make_Attribute_Reference (Sloc (N),
18555 Attribute_Name => Name_Range,
18556 Prefix => Relocate_Node (N)));
18558 -- The original was a subtype mark that does not freeze. This
18559 -- means that the rewritten version must not freeze either.
18561 Set_Must_Not_Freeze (N);
18562 Set_Must_Not_Freeze (Prefix (N));
18563 Analyze_And_Resolve (N);
18567 -- If expander is inactive, type is legal, nothing else to construct
18574 if not Is_Discrete_Type (T) then
18575 Error_Msg_N ("discrete type required for range", N);
18576 Set_Etype (N, Any_Type);
18579 elsif T = Any_Type then
18580 Set_Etype (N, Any_Type);
18584 -- We will now create the appropriate Itype to describe the range, but
18585 -- first a check. If we originally had a subtype, then we just label
18586 -- the range with this subtype. Not only is there no need to construct
18587 -- a new subtype, but it is wrong to do so for two reasons:
18589 -- 1. A legality concern, if we have a subtype, it must not freeze,
18590 -- and the Itype would cause freezing incorrectly
18592 -- 2. An efficiency concern, if we created an Itype, it would not be
18593 -- recognized as the same type for the purposes of eliminating
18594 -- checks in some circumstances.
18596 -- We signal this case by setting the subtype entity in Def_Id
18598 if No (Def_Id) then
18600 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18601 Set_Etype (Def_Id, Base_Type (T));
18603 if Is_Signed_Integer_Type (T) then
18604 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18606 elsif Is_Modular_Integer_Type (T) then
18607 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18610 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18611 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18612 Set_First_Literal (Def_Id, First_Literal (T));
18615 Set_Size_Info (Def_Id, (T));
18616 Set_RM_Size (Def_Id, RM_Size (T));
18617 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18619 Set_Scalar_Range (Def_Id, R);
18620 Conditional_Delay (Def_Id, T);
18622 if Nkind (N) = N_Subtype_Indication then
18623 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18626 -- In the subtype indication case, if the immediate parent of the
18627 -- new subtype is non-static, then the subtype we create is non-
18628 -- static, even if its bounds are static.
18630 if Nkind (N) = N_Subtype_Indication
18631 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18633 Set_Is_Non_Static_Subtype (Def_Id);
18637 -- Final step is to label the index with this constructed type
18639 Set_Etype (N, Def_Id);
18642 ------------------------------
18643 -- Modular_Type_Declaration --
18644 ------------------------------
18646 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18647 Mod_Expr : constant Node_Id := Expression (Def);
18650 procedure Set_Modular_Size (Bits : Int);
18651 -- Sets RM_Size to Bits, and Esize to normal word size above this
18653 ----------------------
18654 -- Set_Modular_Size --
18655 ----------------------
18657 procedure Set_Modular_Size (Bits : Int) is
18659 Set_RM_Size (T, UI_From_Int (Bits));
18664 elsif Bits <= 16 then
18665 Init_Esize (T, 16);
18667 elsif Bits <= 32 then
18668 Init_Esize (T, 32);
18671 Init_Esize (T, System_Max_Binary_Modulus_Power);
18674 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18675 Set_Is_Known_Valid (T);
18677 end Set_Modular_Size;
18679 -- Start of processing for Modular_Type_Declaration
18682 -- If the mod expression is (exactly) 2 * literal, where literal is
18683 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18685 if Warn_On_Suspicious_Modulus_Value
18686 and then Nkind (Mod_Expr) = N_Op_Multiply
18687 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18688 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18689 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18690 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18693 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18696 -- Proceed with analysis of mod expression
18698 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18700 Set_Ekind (T, E_Modular_Integer_Type);
18701 Init_Alignment (T);
18702 Set_Is_Constrained (T);
18704 if not Is_OK_Static_Expression (Mod_Expr) then
18705 Flag_Non_Static_Expr
18706 ("non-static expression used for modular type bound!", Mod_Expr);
18707 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18709 M_Val := Expr_Value (Mod_Expr);
18713 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18714 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18717 if M_Val > 2 ** Standard_Long_Integer_Size then
18718 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18721 Set_Modulus (T, M_Val);
18723 -- Create bounds for the modular type based on the modulus given in
18724 -- the type declaration and then analyze and resolve those bounds.
18726 Set_Scalar_Range (T,
18727 Make_Range (Sloc (Mod_Expr),
18728 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18729 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18731 -- Properly analyze the literals for the range. We do this manually
18732 -- because we can't go calling Resolve, since we are resolving these
18733 -- bounds with the type, and this type is certainly not complete yet.
18735 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18736 Set_Etype (High_Bound (Scalar_Range (T)), T);
18737 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18738 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18740 -- Loop through powers of two to find number of bits required
18742 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18746 if M_Val = 2 ** Bits then
18747 Set_Modular_Size (Bits);
18752 elsif M_Val < 2 ** Bits then
18753 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18754 Set_Non_Binary_Modulus (T);
18756 if Bits > System_Max_Nonbinary_Modulus_Power then
18757 Error_Msg_Uint_1 :=
18758 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18760 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18761 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18765 -- In the nonbinary case, set size as per RM 13.3(55)
18767 Set_Modular_Size (Bits);
18774 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18775 -- so we just signal an error and set the maximum size.
18777 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18778 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18780 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18781 Init_Alignment (T);
18783 end Modular_Type_Declaration;
18785 --------------------------
18786 -- New_Concatenation_Op --
18787 --------------------------
18789 procedure New_Concatenation_Op (Typ : Entity_Id) is
18790 Loc : constant Source_Ptr := Sloc (Typ);
18793 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18794 -- Create abbreviated declaration for the formal of a predefined
18795 -- Operator 'Op' of type 'Typ'
18797 --------------------
18798 -- Make_Op_Formal --
18799 --------------------
18801 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18802 Formal : Entity_Id;
18804 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18805 Set_Etype (Formal, Typ);
18806 Set_Mechanism (Formal, Default_Mechanism);
18808 end Make_Op_Formal;
18810 -- Start of processing for New_Concatenation_Op
18813 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18815 Set_Ekind (Op, E_Operator);
18816 Set_Scope (Op, Current_Scope);
18817 Set_Etype (Op, Typ);
18818 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18819 Set_Is_Immediately_Visible (Op);
18820 Set_Is_Intrinsic_Subprogram (Op);
18821 Set_Has_Completion (Op);
18822 Append_Entity (Op, Current_Scope);
18824 Set_Name_Entity_Id (Name_Op_Concat, Op);
18826 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18827 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18828 end New_Concatenation_Op;
18830 -------------------------
18831 -- OK_For_Limited_Init --
18832 -------------------------
18834 -- ???Check all calls of this, and compare the conditions under which it's
18837 function OK_For_Limited_Init
18839 Exp : Node_Id) return Boolean
18842 return Is_CPP_Constructor_Call (Exp)
18843 or else (Ada_Version >= Ada_2005
18844 and then not Debug_Flag_Dot_L
18845 and then OK_For_Limited_Init_In_05 (Typ, Exp));
18846 end OK_For_Limited_Init;
18848 -------------------------------
18849 -- OK_For_Limited_Init_In_05 --
18850 -------------------------------
18852 function OK_For_Limited_Init_In_05
18854 Exp : Node_Id) return Boolean
18857 -- An object of a limited interface type can be initialized with any
18858 -- expression of a nonlimited descendant type. However this does not
18859 -- apply if this is a view conversion of some other expression. This
18860 -- is checked below.
18862 if Is_Class_Wide_Type (Typ)
18863 and then Is_Limited_Interface (Typ)
18864 and then not Is_Limited_Type (Etype (Exp))
18865 and then Nkind (Exp) /= N_Type_Conversion
18870 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
18871 -- case of limited aggregates (including extension aggregates), and
18872 -- function calls. The function call may have been given in prefixed
18873 -- notation, in which case the original node is an indexed component.
18874 -- If the function is parameterless, the original node was an explicit
18875 -- dereference. The function may also be parameterless, in which case
18876 -- the source node is just an identifier.
18878 -- A branch of a conditional expression may have been removed if the
18879 -- condition is statically known. This happens during expansion, and
18880 -- thus will not happen if previous errors were encountered. The check
18881 -- will have been performed on the chosen branch, which replaces the
18882 -- original conditional expression.
18888 case Nkind (Original_Node (Exp)) is
18889 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
18892 when N_Identifier =>
18893 return Present (Entity (Original_Node (Exp)))
18894 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
18896 when N_Qualified_Expression =>
18898 OK_For_Limited_Init_In_05
18899 (Typ, Expression (Original_Node (Exp)));
18901 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
18902 -- with a function call, the expander has rewritten the call into an
18903 -- N_Type_Conversion node to force displacement of the pointer to
18904 -- reference the component containing the secondary dispatch table.
18905 -- Otherwise a type conversion is not a legal context.
18906 -- A return statement for a build-in-place function returning a
18907 -- synchronized type also introduces an unchecked conversion.
18909 when N_Type_Conversion |
18910 N_Unchecked_Type_Conversion =>
18911 return not Comes_From_Source (Exp)
18913 OK_For_Limited_Init_In_05
18914 (Typ, Expression (Original_Node (Exp)));
18916 when N_Indexed_Component |
18917 N_Selected_Component |
18918 N_Explicit_Dereference =>
18919 return Nkind (Exp) = N_Function_Call;
18921 -- A use of 'Input is a function call, hence allowed. Normally the
18922 -- attribute will be changed to a call, but the attribute by itself
18923 -- can occur with -gnatc.
18925 when N_Attribute_Reference =>
18926 return Attribute_Name (Original_Node (Exp)) = Name_Input;
18928 -- For a case expression, all dependent expressions must be legal
18930 when N_Case_Expression =>
18935 Alt := First (Alternatives (Original_Node (Exp)));
18936 while Present (Alt) loop
18937 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
18947 -- For an if expression, all dependent expressions must be legal
18949 when N_If_Expression =>
18951 Then_Expr : constant Node_Id :=
18952 Next (First (Expressions (Original_Node (Exp))));
18953 Else_Expr : constant Node_Id := Next (Then_Expr);
18955 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
18957 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
18963 end OK_For_Limited_Init_In_05;
18965 -------------------------------------------
18966 -- Ordinary_Fixed_Point_Type_Declaration --
18967 -------------------------------------------
18969 procedure Ordinary_Fixed_Point_Type_Declaration
18973 Loc : constant Source_Ptr := Sloc (Def);
18974 Delta_Expr : constant Node_Id := Delta_Expression (Def);
18975 RRS : constant Node_Id := Real_Range_Specification (Def);
18976 Implicit_Base : Entity_Id;
18983 Check_Restriction (No_Fixed_Point, Def);
18985 -- Create implicit base type
18988 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
18989 Set_Etype (Implicit_Base, Implicit_Base);
18991 -- Analyze and process delta expression
18993 Analyze_And_Resolve (Delta_Expr, Any_Real);
18995 Check_Delta_Expression (Delta_Expr);
18996 Delta_Val := Expr_Value_R (Delta_Expr);
18998 Set_Delta_Value (Implicit_Base, Delta_Val);
19000 -- Compute default small from given delta, which is the largest power
19001 -- of two that does not exceed the given delta value.
19011 if Delta_Val < Ureal_1 then
19012 while Delta_Val < Tmp loop
19013 Tmp := Tmp / Ureal_2;
19014 Scale := Scale + 1;
19019 Tmp := Tmp * Ureal_2;
19020 exit when Tmp > Delta_Val;
19021 Scale := Scale - 1;
19025 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19028 Set_Small_Value (Implicit_Base, Small_Val);
19030 -- If no range was given, set a dummy range
19032 if RRS <= Empty_Or_Error then
19033 Low_Val := -Small_Val;
19034 High_Val := Small_Val;
19036 -- Otherwise analyze and process given range
19040 Low : constant Node_Id := Low_Bound (RRS);
19041 High : constant Node_Id := High_Bound (RRS);
19044 Analyze_And_Resolve (Low, Any_Real);
19045 Analyze_And_Resolve (High, Any_Real);
19046 Check_Real_Bound (Low);
19047 Check_Real_Bound (High);
19049 -- Obtain and set the range
19051 Low_Val := Expr_Value_R (Low);
19052 High_Val := Expr_Value_R (High);
19054 if Low_Val > High_Val then
19055 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19060 -- The range for both the implicit base and the declared first subtype
19061 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19062 -- set a temporary range in place. Note that the bounds of the base
19063 -- type will be widened to be symmetrical and to fill the available
19064 -- bits when the type is frozen.
19066 -- We could do this with all discrete types, and probably should, but
19067 -- we absolutely have to do it for fixed-point, since the end-points
19068 -- of the range and the size are determined by the small value, which
19069 -- could be reset before the freeze point.
19071 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19072 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19074 -- Complete definition of first subtype. The inheritance of the rep item
19075 -- chain ensures that SPARK-related pragmas are not clobbered when the
19076 -- ordinary fixed point type acts as a full view of a private type.
19078 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19079 Set_Etype (T, Implicit_Base);
19080 Init_Size_Align (T);
19081 Inherit_Rep_Item_Chain (T, Implicit_Base);
19082 Set_Small_Value (T, Small_Val);
19083 Set_Delta_Value (T, Delta_Val);
19084 Set_Is_Constrained (T);
19085 end Ordinary_Fixed_Point_Type_Declaration;
19087 ----------------------------------
19088 -- Preanalyze_Assert_Expression --
19089 ----------------------------------
19091 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19093 In_Assertion_Expr := In_Assertion_Expr + 1;
19094 Preanalyze_Spec_Expression (N, T);
19095 In_Assertion_Expr := In_Assertion_Expr - 1;
19096 end Preanalyze_Assert_Expression;
19098 -----------------------------------
19099 -- Preanalyze_Default_Expression --
19100 -----------------------------------
19102 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19103 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19105 In_Default_Expr := True;
19106 Preanalyze_Spec_Expression (N, T);
19107 In_Default_Expr := Save_In_Default_Expr;
19108 end Preanalyze_Default_Expression;
19110 --------------------------------
19111 -- Preanalyze_Spec_Expression --
19112 --------------------------------
19114 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19115 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19117 In_Spec_Expression := True;
19118 Preanalyze_And_Resolve (N, T);
19119 In_Spec_Expression := Save_In_Spec_Expression;
19120 end Preanalyze_Spec_Expression;
19122 ----------------------------------------
19123 -- Prepare_Private_Subtype_Completion --
19124 ----------------------------------------
19126 procedure Prepare_Private_Subtype_Completion
19128 Related_Nod : Node_Id)
19130 Id_B : constant Entity_Id := Base_Type (Id);
19131 Full_B : Entity_Id := Full_View (Id_B);
19135 if Present (Full_B) then
19137 -- Get to the underlying full view if necessary
19139 if Is_Private_Type (Full_B)
19140 and then Present (Underlying_Full_View (Full_B))
19142 Full_B := Underlying_Full_View (Full_B);
19145 -- The Base_Type is already completed, we can complete the subtype
19146 -- now. We have to create a new entity with the same name, Thus we
19147 -- can't use Create_Itype.
19149 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19150 Set_Is_Itype (Full);
19151 Set_Associated_Node_For_Itype (Full, Related_Nod);
19152 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19155 -- The parent subtype may be private, but the base might not, in some
19156 -- nested instances. In that case, the subtype does not need to be
19157 -- exchanged. It would still be nice to make private subtypes and their
19158 -- bases consistent at all times ???
19160 if Is_Private_Type (Id_B) then
19161 Append_Elmt (Id, Private_Dependents (Id_B));
19163 end Prepare_Private_Subtype_Completion;
19165 ---------------------------
19166 -- Process_Discriminants --
19167 ---------------------------
19169 procedure Process_Discriminants
19171 Prev : Entity_Id := Empty)
19173 Elist : constant Elist_Id := New_Elmt_List;
19176 Discr_Number : Uint;
19177 Discr_Type : Entity_Id;
19178 Default_Present : Boolean := False;
19179 Default_Not_Present : Boolean := False;
19182 -- A composite type other than an array type can have discriminants.
19183 -- On entry, the current scope is the composite type.
19185 -- The discriminants are initially entered into the scope of the type
19186 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19187 -- use, as explained at the end of this procedure.
19189 Discr := First (Discriminant_Specifications (N));
19190 while Present (Discr) loop
19191 Enter_Name (Defining_Identifier (Discr));
19193 -- For navigation purposes we add a reference to the discriminant
19194 -- in the entity for the type. If the current declaration is a
19195 -- completion, place references on the partial view. Otherwise the
19196 -- type is the current scope.
19198 if Present (Prev) then
19200 -- The references go on the partial view, if present. If the
19201 -- partial view has discriminants, the references have been
19202 -- generated already.
19204 if not Has_Discriminants (Prev) then
19205 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19209 (Current_Scope, Defining_Identifier (Discr), 'd');
19212 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19213 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19215 -- Ada 2005 (AI-254)
19217 if Present (Access_To_Subprogram_Definition
19218 (Discriminant_Type (Discr)))
19219 and then Protected_Present (Access_To_Subprogram_Definition
19220 (Discriminant_Type (Discr)))
19223 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
19227 Find_Type (Discriminant_Type (Discr));
19228 Discr_Type := Etype (Discriminant_Type (Discr));
19230 if Error_Posted (Discriminant_Type (Discr)) then
19231 Discr_Type := Any_Type;
19235 -- Handling of discriminants that are access types
19237 if Is_Access_Type (Discr_Type) then
19239 -- Ada 2005 (AI-230): Access discriminant allowed in non-
19240 -- limited record types
19242 if Ada_Version < Ada_2005 then
19243 Check_Access_Discriminant_Requires_Limited
19244 (Discr, Discriminant_Type (Discr));
19247 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
19249 ("(Ada 83) access discriminant not allowed", Discr);
19252 -- If not access type, must be a discrete type
19254 elsif not Is_Discrete_Type (Discr_Type) then
19256 ("discriminants must have a discrete or access type",
19257 Discriminant_Type (Discr));
19260 Set_Etype (Defining_Identifier (Discr), Discr_Type);
19262 -- If a discriminant specification includes the assignment compound
19263 -- delimiter followed by an expression, the expression is the default
19264 -- expression of the discriminant; the default expression must be of
19265 -- the type of the discriminant. (RM 3.7.1) Since this expression is
19266 -- a default expression, we do the special preanalysis, since this
19267 -- expression does not freeze (see section "Handling of Default and
19268 -- Per-Object Expressions" in spec of package Sem).
19270 if Present (Expression (Discr)) then
19271 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19275 if Nkind (N) = N_Formal_Type_Declaration then
19277 ("discriminant defaults not allowed for formal type",
19278 Expression (Discr));
19280 -- Flag an error for a tagged type with defaulted discriminants,
19281 -- excluding limited tagged types when compiling for Ada 2012
19282 -- (see AI05-0214).
19284 elsif Is_Tagged_Type (Current_Scope)
19285 and then (not Is_Limited_Type (Current_Scope)
19286 or else Ada_Version < Ada_2012)
19287 and then Comes_From_Source (N)
19289 -- Note: see similar test in Check_Or_Process_Discriminants, to
19290 -- handle the (illegal) case of the completion of an untagged
19291 -- view with discriminants with defaults by a tagged full view.
19292 -- We skip the check if Discr does not come from source, to
19293 -- account for the case of an untagged derived type providing
19294 -- defaults for a renamed discriminant from a private untagged
19295 -- ancestor with a tagged full view (ACATS B460006).
19297 if Ada_Version >= Ada_2012 then
19299 ("discriminants of nonlimited tagged type cannot have"
19301 Expression (Discr));
19304 ("discriminants of tagged type cannot have defaults",
19305 Expression (Discr));
19309 Default_Present := True;
19310 Append_Elmt (Expression (Discr), Elist);
19312 -- Tag the defining identifiers for the discriminants with
19313 -- their corresponding default expressions from the tree.
19315 Set_Discriminant_Default_Value
19316 (Defining_Identifier (Discr), Expression (Discr));
19319 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19320 -- gets set unless we can be sure that no range check is required.
19322 if (GNATprove_Mode or not Expander_Active)
19325 (Expression (Discr), Discr_Type, Assume_Valid => True)
19327 Set_Do_Range_Check (Expression (Discr));
19330 -- No default discriminant value given
19333 Default_Not_Present := True;
19336 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19337 -- Discr_Type but with the null-exclusion attribute
19339 if Ada_Version >= Ada_2005 then
19341 -- Ada 2005 (AI-231): Static checks
19343 if Can_Never_Be_Null (Discr_Type) then
19344 Null_Exclusion_Static_Checks (Discr);
19346 elsif Is_Access_Type (Discr_Type)
19347 and then Null_Exclusion_Present (Discr)
19349 -- No need to check itypes because in their case this check
19350 -- was done at their point of creation
19352 and then not Is_Itype (Discr_Type)
19354 if Can_Never_Be_Null (Discr_Type) then
19356 ("`NOT NULL` not allowed (& already excludes null)",
19361 Set_Etype (Defining_Identifier (Discr),
19362 Create_Null_Excluding_Itype
19364 Related_Nod => Discr));
19366 -- Check for improper null exclusion if the type is otherwise
19367 -- legal for a discriminant.
19369 elsif Null_Exclusion_Present (Discr)
19370 and then Is_Discrete_Type (Discr_Type)
19373 ("null exclusion can only apply to an access type", Discr);
19376 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19377 -- can't have defaults. Synchronized types, or types that are
19378 -- explicitly limited are fine, but special tests apply to derived
19379 -- types in generics: in a generic body we have to assume the
19380 -- worst, and therefore defaults are not allowed if the parent is
19381 -- a generic formal private type (see ACATS B370001).
19383 if Is_Access_Type (Discr_Type) and then Default_Present then
19384 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19385 or else Is_Limited_Record (Current_Scope)
19386 or else Is_Concurrent_Type (Current_Scope)
19387 or else Is_Concurrent_Record_Type (Current_Scope)
19388 or else Ekind (Current_Scope) = E_Limited_Private_Type
19390 if not Is_Derived_Type (Current_Scope)
19391 or else not Is_Generic_Type (Etype (Current_Scope))
19392 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19393 or else Limited_Present
19394 (Type_Definition (Parent (Current_Scope)))
19400 ("access discriminants of nonlimited types cannot "
19401 & "have defaults", Expression (Discr));
19404 elsif Present (Expression (Discr)) then
19406 ("(Ada 2005) access discriminants of nonlimited types "
19407 & "cannot have defaults", Expression (Discr));
19412 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(6)).
19413 -- This check is relevant only when SPARK_Mode is on as it is not a
19414 -- standard Ada legality rule.
19417 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19419 Error_Msg_N ("discriminant cannot be volatile", Discr);
19425 -- An element list consisting of the default expressions of the
19426 -- discriminants is constructed in the above loop and used to set
19427 -- the Discriminant_Constraint attribute for the type. If an object
19428 -- is declared of this (record or task) type without any explicit
19429 -- discriminant constraint given, this element list will form the
19430 -- actual parameters for the corresponding initialization procedure
19433 Set_Discriminant_Constraint (Current_Scope, Elist);
19434 Set_Stored_Constraint (Current_Scope, No_Elist);
19436 -- Default expressions must be provided either for all or for none
19437 -- of the discriminants of a discriminant part. (RM 3.7.1)
19439 if Default_Present and then Default_Not_Present then
19441 ("incomplete specification of defaults for discriminants", N);
19444 -- The use of the name of a discriminant is not allowed in default
19445 -- expressions of a discriminant part if the specification of the
19446 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19448 -- To detect this, the discriminant names are entered initially with an
19449 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19450 -- attempt to use a void entity (for example in an expression that is
19451 -- type-checked) produces the error message: premature usage. Now after
19452 -- completing the semantic analysis of the discriminant part, we can set
19453 -- the Ekind of all the discriminants appropriately.
19455 Discr := First (Discriminant_Specifications (N));
19456 Discr_Number := Uint_1;
19457 while Present (Discr) loop
19458 Id := Defining_Identifier (Discr);
19459 Set_Ekind (Id, E_Discriminant);
19460 Init_Component_Location (Id);
19462 Set_Discriminant_Number (Id, Discr_Number);
19464 -- Make sure this is always set, even in illegal programs
19466 Set_Corresponding_Discriminant (Id, Empty);
19468 -- Initialize the Original_Record_Component to the entity itself.
19469 -- Inherit_Components will propagate the right value to
19470 -- discriminants in derived record types.
19472 Set_Original_Record_Component (Id, Id);
19474 -- Create the discriminal for the discriminant
19476 Build_Discriminal (Id);
19479 Discr_Number := Discr_Number + 1;
19482 Set_Has_Discriminants (Current_Scope);
19483 end Process_Discriminants;
19485 -----------------------
19486 -- Process_Full_View --
19487 -----------------------
19489 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19490 procedure Collect_Implemented_Interfaces
19492 Ifaces : Elist_Id);
19493 -- Ada 2005: Gather all the interfaces that Typ directly or
19494 -- inherently implements. Duplicate entries are not added to
19495 -- the list Ifaces.
19497 ------------------------------------
19498 -- Collect_Implemented_Interfaces --
19499 ------------------------------------
19501 procedure Collect_Implemented_Interfaces
19506 Iface_Elmt : Elmt_Id;
19509 -- Abstract interfaces are only associated with tagged record types
19511 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19515 -- Recursively climb to the ancestors
19517 if Etype (Typ) /= Typ
19519 -- Protect the frontend against wrong cyclic declarations like:
19521 -- type B is new A with private;
19522 -- type C is new A with private;
19524 -- type B is new C with null record;
19525 -- type C is new B with null record;
19527 and then Etype (Typ) /= Priv_T
19528 and then Etype (Typ) /= Full_T
19530 -- Keep separate the management of private type declarations
19532 if Ekind (Typ) = E_Record_Type_With_Private then
19534 -- Handle the following illegal usage:
19535 -- type Private_Type is tagged private;
19537 -- type Private_Type is new Type_Implementing_Iface;
19539 if Present (Full_View (Typ))
19540 and then Etype (Typ) /= Full_View (Typ)
19542 if Is_Interface (Etype (Typ)) then
19543 Append_Unique_Elmt (Etype (Typ), Ifaces);
19546 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19549 -- Non-private types
19552 if Is_Interface (Etype (Typ)) then
19553 Append_Unique_Elmt (Etype (Typ), Ifaces);
19556 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19560 -- Handle entities in the list of abstract interfaces
19562 if Present (Interfaces (Typ)) then
19563 Iface_Elmt := First_Elmt (Interfaces (Typ));
19564 while Present (Iface_Elmt) loop
19565 Iface := Node (Iface_Elmt);
19567 pragma Assert (Is_Interface (Iface));
19569 if not Contain_Interface (Iface, Ifaces) then
19570 Append_Elmt (Iface, Ifaces);
19571 Collect_Implemented_Interfaces (Iface, Ifaces);
19574 Next_Elmt (Iface_Elmt);
19577 end Collect_Implemented_Interfaces;
19581 Full_Indic : Node_Id;
19582 Full_Parent : Entity_Id;
19583 Priv_Parent : Entity_Id;
19585 -- Start of processing for Process_Full_View
19588 -- First some sanity checks that must be done after semantic
19589 -- decoration of the full view and thus cannot be placed with other
19590 -- similar checks in Find_Type_Name
19592 if not Is_Limited_Type (Priv_T)
19593 and then (Is_Limited_Type (Full_T)
19594 or else Is_Limited_Composite (Full_T))
19596 if In_Instance then
19600 ("completion of nonlimited type cannot be limited", Full_T);
19601 Explain_Limited_Type (Full_T, Full_T);
19604 elsif Is_Abstract_Type (Full_T)
19605 and then not Is_Abstract_Type (Priv_T)
19608 ("completion of nonabstract type cannot be abstract", Full_T);
19610 elsif Is_Tagged_Type (Priv_T)
19611 and then Is_Limited_Type (Priv_T)
19612 and then not Is_Limited_Type (Full_T)
19614 -- If pragma CPP_Class was applied to the private declaration
19615 -- propagate the limitedness to the full-view
19617 if Is_CPP_Class (Priv_T) then
19618 Set_Is_Limited_Record (Full_T);
19620 -- GNAT allow its own definition of Limited_Controlled to disobey
19621 -- this rule in order in ease the implementation. This test is safe
19622 -- because Root_Controlled is defined in a child of System that
19623 -- normal programs are not supposed to use.
19625 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19626 Set_Is_Limited_Composite (Full_T);
19629 ("completion of limited tagged type must be limited", Full_T);
19632 elsif Is_Generic_Type (Priv_T) then
19633 Error_Msg_N ("generic type cannot have a completion", Full_T);
19636 -- Check that ancestor interfaces of private and full views are
19637 -- consistent. We omit this check for synchronized types because
19638 -- they are performed on the corresponding record type when frozen.
19640 if Ada_Version >= Ada_2005
19641 and then Is_Tagged_Type (Priv_T)
19642 and then Is_Tagged_Type (Full_T)
19643 and then not Is_Concurrent_Type (Full_T)
19647 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19648 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19651 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19652 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19654 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19655 -- an interface type if and only if the full type is descendant
19656 -- of the interface type (AARM 7.3 (7.3/2)).
19658 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19660 if Present (Iface) then
19662 ("interface in partial view& not implemented by full type "
19663 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19666 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19668 if Present (Iface) then
19670 ("interface & not implemented by partial view "
19671 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19676 if Is_Tagged_Type (Priv_T)
19677 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19678 and then Is_Derived_Type (Full_T)
19680 Priv_Parent := Etype (Priv_T);
19682 -- The full view of a private extension may have been transformed
19683 -- into an unconstrained derived type declaration and a subtype
19684 -- declaration (see build_derived_record_type for details).
19686 if Nkind (N) = N_Subtype_Declaration then
19687 Full_Indic := Subtype_Indication (N);
19688 Full_Parent := Etype (Base_Type (Full_T));
19690 Full_Indic := Subtype_Indication (Type_Definition (N));
19691 Full_Parent := Etype (Full_T);
19694 -- Check that the parent type of the full type is a descendant of
19695 -- the ancestor subtype given in the private extension. If either
19696 -- entity has an Etype equal to Any_Type then we had some previous
19697 -- error situation [7.3(8)].
19699 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19702 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19703 -- any order. Therefore we don't have to check that its parent must
19704 -- be a descendant of the parent of the private type declaration.
19706 elsif Is_Interface (Priv_Parent)
19707 and then Is_Interface (Full_Parent)
19711 -- Ada 2005 (AI-251): If the parent of the private type declaration
19712 -- is an interface there is no need to check that it is an ancestor
19713 -- of the associated full type declaration. The required tests for
19714 -- this case are performed by Build_Derived_Record_Type.
19716 elsif not Is_Interface (Base_Type (Priv_Parent))
19717 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19720 ("parent of full type must descend from parent of private "
19721 & "extension", Full_Indic);
19723 -- First check a formal restriction, and then proceed with checking
19724 -- Ada rules. Since the formal restriction is not a serious error, we
19725 -- don't prevent further error detection for this check, hence the
19729 -- In formal mode, when completing a private extension the type
19730 -- named in the private part must be exactly the same as that
19731 -- named in the visible part.
19733 if Priv_Parent /= Full_Parent then
19734 Error_Msg_Name_1 := Chars (Priv_Parent);
19735 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19738 -- Check the rules of 7.3(10): if the private extension inherits
19739 -- known discriminants, then the full type must also inherit those
19740 -- discriminants from the same (ancestor) type, and the parent
19741 -- subtype of the full type must be constrained if and only if
19742 -- the ancestor subtype of the private extension is constrained.
19744 if No (Discriminant_Specifications (Parent (Priv_T)))
19745 and then not Has_Unknown_Discriminants (Priv_T)
19746 and then Has_Discriminants (Base_Type (Priv_Parent))
19749 Priv_Indic : constant Node_Id :=
19750 Subtype_Indication (Parent (Priv_T));
19752 Priv_Constr : constant Boolean :=
19753 Is_Constrained (Priv_Parent)
19755 Nkind (Priv_Indic) = N_Subtype_Indication
19757 Is_Constrained (Entity (Priv_Indic));
19759 Full_Constr : constant Boolean :=
19760 Is_Constrained (Full_Parent)
19762 Nkind (Full_Indic) = N_Subtype_Indication
19764 Is_Constrained (Entity (Full_Indic));
19766 Priv_Discr : Entity_Id;
19767 Full_Discr : Entity_Id;
19770 Priv_Discr := First_Discriminant (Priv_Parent);
19771 Full_Discr := First_Discriminant (Full_Parent);
19772 while Present (Priv_Discr) and then Present (Full_Discr) loop
19773 if Original_Record_Component (Priv_Discr) =
19774 Original_Record_Component (Full_Discr)
19776 Corresponding_Discriminant (Priv_Discr) =
19777 Corresponding_Discriminant (Full_Discr)
19784 Next_Discriminant (Priv_Discr);
19785 Next_Discriminant (Full_Discr);
19788 if Present (Priv_Discr) or else Present (Full_Discr) then
19790 ("full view must inherit discriminants of the parent "
19791 & "type used in the private extension", Full_Indic);
19793 elsif Priv_Constr and then not Full_Constr then
19795 ("parent subtype of full type must be constrained",
19798 elsif Full_Constr and then not Priv_Constr then
19800 ("parent subtype of full type must be unconstrained",
19805 -- Check the rules of 7.3(12): if a partial view has neither
19806 -- known or unknown discriminants, then the full type
19807 -- declaration shall define a definite subtype.
19809 elsif not Has_Unknown_Discriminants (Priv_T)
19810 and then not Has_Discriminants (Priv_T)
19811 and then not Is_Constrained (Full_T)
19814 ("full view must define a constrained type if partial view "
19815 & "has no discriminants", Full_T);
19818 -- ??????? Do we implement the following properly ?????
19819 -- If the ancestor subtype of a private extension has constrained
19820 -- discriminants, then the parent subtype of the full view shall
19821 -- impose a statically matching constraint on those discriminants
19826 -- For untagged types, verify that a type without discriminants is
19827 -- not completed with an unconstrained type. A separate error message
19828 -- is produced if the full type has defaulted discriminants.
19830 if Is_Definite_Subtype (Priv_T)
19831 and then not Is_Definite_Subtype (Full_T)
19833 Error_Msg_Sloc := Sloc (Parent (Priv_T));
19835 ("full view of& not compatible with declaration#",
19838 if not Is_Tagged_Type (Full_T) then
19840 ("\one is constrained, the other unconstrained", Full_T);
19845 -- AI-419: verify that the use of "limited" is consistent
19848 Orig_Decl : constant Node_Id := Original_Node (N);
19851 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19852 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
19854 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
19856 if not Limited_Present (Parent (Priv_T))
19857 and then not Synchronized_Present (Parent (Priv_T))
19858 and then Limited_Present (Type_Definition (Orig_Decl))
19861 ("full view of non-limited extension cannot be limited", N);
19863 -- Conversely, if the partial view carries the limited keyword,
19864 -- the full view must as well, even if it may be redundant.
19866 elsif Limited_Present (Parent (Priv_T))
19867 and then not Limited_Present (Type_Definition (Orig_Decl))
19870 ("full view of limited extension must be explicitly limited",
19876 -- Ada 2005 (AI-443): A synchronized private extension must be
19877 -- completed by a task or protected type.
19879 if Ada_Version >= Ada_2005
19880 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19881 and then Synchronized_Present (Parent (Priv_T))
19882 and then not Is_Concurrent_Type (Full_T)
19884 Error_Msg_N ("full view of synchronized extension must " &
19885 "be synchronized type", N);
19888 -- Ada 2005 AI-363: if the full view has discriminants with
19889 -- defaults, it is illegal to declare constrained access subtypes
19890 -- whose designated type is the current type. This allows objects
19891 -- of the type that are declared in the heap to be unconstrained.
19893 if not Has_Unknown_Discriminants (Priv_T)
19894 and then not Has_Discriminants (Priv_T)
19895 and then Has_Discriminants (Full_T)
19897 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
19899 Set_Has_Constrained_Partial_View (Full_T);
19900 Set_Has_Constrained_Partial_View (Priv_T);
19903 -- Create a full declaration for all its subtypes recorded in
19904 -- Private_Dependents and swap them similarly to the base type. These
19905 -- are subtypes that have been define before the full declaration of
19906 -- the private type. We also swap the entry in Private_Dependents list
19907 -- so we can properly restore the private view on exit from the scope.
19910 Priv_Elmt : Elmt_Id;
19911 Priv_Scop : Entity_Id;
19916 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
19917 while Present (Priv_Elmt) loop
19918 Priv := Node (Priv_Elmt);
19919 Priv_Scop := Scope (Priv);
19921 if Ekind_In (Priv, E_Private_Subtype,
19922 E_Limited_Private_Subtype,
19923 E_Record_Subtype_With_Private)
19925 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
19926 Set_Is_Itype (Full);
19927 Set_Parent (Full, Parent (Priv));
19928 Set_Associated_Node_For_Itype (Full, N);
19930 -- Now we need to complete the private subtype, but since the
19931 -- base type has already been swapped, we must also swap the
19932 -- subtypes (and thus, reverse the arguments in the call to
19933 -- Complete_Private_Subtype). Also note that we may need to
19934 -- re-establish the scope of the private subtype.
19936 Copy_And_Swap (Priv, Full);
19938 if not In_Open_Scopes (Priv_Scop) then
19939 Push_Scope (Priv_Scop);
19942 -- Reset Priv_Scop to Empty to indicate no scope was pushed
19944 Priv_Scop := Empty;
19947 Complete_Private_Subtype (Full, Priv, Full_T, N);
19949 if Present (Priv_Scop) then
19953 Replace_Elmt (Priv_Elmt, Full);
19956 Next_Elmt (Priv_Elmt);
19960 -- If the private view was tagged, copy the new primitive operations
19961 -- from the private view to the full view.
19963 if Is_Tagged_Type (Full_T) then
19965 Disp_Typ : Entity_Id;
19966 Full_List : Elist_Id;
19968 Prim_Elmt : Elmt_Id;
19969 Priv_List : Elist_Id;
19973 L : Elist_Id) return Boolean;
19974 -- Determine whether list L contains element E
19982 L : Elist_Id) return Boolean
19984 List_Elmt : Elmt_Id;
19987 List_Elmt := First_Elmt (L);
19988 while Present (List_Elmt) loop
19989 if Node (List_Elmt) = E then
19993 Next_Elmt (List_Elmt);
19999 -- Start of processing
20002 if Is_Tagged_Type (Priv_T) then
20003 Priv_List := Primitive_Operations (Priv_T);
20004 Prim_Elmt := First_Elmt (Priv_List);
20006 -- In the case of a concurrent type completing a private tagged
20007 -- type, primitives may have been declared in between the two
20008 -- views. These subprograms need to be wrapped the same way
20009 -- entries and protected procedures are handled because they
20010 -- cannot be directly shared by the two views.
20012 if Is_Concurrent_Type (Full_T) then
20014 Conc_Typ : constant Entity_Id :=
20015 Corresponding_Record_Type (Full_T);
20016 Curr_Nod : Node_Id := Parent (Conc_Typ);
20017 Wrap_Spec : Node_Id;
20020 while Present (Prim_Elmt) loop
20021 Prim := Node (Prim_Elmt);
20023 if Comes_From_Source (Prim)
20024 and then not Is_Abstract_Subprogram (Prim)
20027 Make_Subprogram_Declaration (Sloc (Prim),
20031 Obj_Typ => Conc_Typ,
20033 Parameter_Specifications
20036 Insert_After (Curr_Nod, Wrap_Spec);
20037 Curr_Nod := Wrap_Spec;
20039 Analyze (Wrap_Spec);
20041 -- Remove the wrapper from visibility to avoid
20042 -- spurious conflict with the wrapped entity.
20044 Set_Is_Immediately_Visible
20045 (Defining_Entity (Specification (Wrap_Spec)),
20049 Next_Elmt (Prim_Elmt);
20055 -- For non-concurrent types, transfer explicit primitives, but
20056 -- omit those inherited from the parent of the private view
20057 -- since they will be re-inherited later on.
20060 Full_List := Primitive_Operations (Full_T);
20062 while Present (Prim_Elmt) loop
20063 Prim := Node (Prim_Elmt);
20065 if Comes_From_Source (Prim)
20066 and then not Contains (Prim, Full_List)
20068 Append_Elmt (Prim, Full_List);
20071 Next_Elmt (Prim_Elmt);
20075 -- Untagged private view
20078 Full_List := Primitive_Operations (Full_T);
20080 -- In this case the partial view is untagged, so here we locate
20081 -- all of the earlier primitives that need to be treated as
20082 -- dispatching (those that appear between the two views). Note
20083 -- that these additional operations must all be new operations
20084 -- (any earlier operations that override inherited operations
20085 -- of the full view will already have been inserted in the
20086 -- primitives list, marked by Check_Operation_From_Private_View
20087 -- as dispatching. Note that implicit "/=" operators are
20088 -- excluded from being added to the primitives list since they
20089 -- shouldn't be treated as dispatching (tagged "/=" is handled
20092 Prim := Next_Entity (Full_T);
20093 while Present (Prim) and then Prim /= Priv_T loop
20094 if Ekind_In (Prim, E_Procedure, E_Function) then
20095 Disp_Typ := Find_Dispatching_Type (Prim);
20097 if Disp_Typ = Full_T
20098 and then (Chars (Prim) /= Name_Op_Ne
20099 or else Comes_From_Source (Prim))
20101 Check_Controlling_Formals (Full_T, Prim);
20103 if not Is_Dispatching_Operation (Prim) then
20104 Append_Elmt (Prim, Full_List);
20105 Set_Is_Dispatching_Operation (Prim, True);
20106 Set_DT_Position_Value (Prim, No_Uint);
20109 elsif Is_Dispatching_Operation (Prim)
20110 and then Disp_Typ /= Full_T
20113 -- Verify that it is not otherwise controlled by a
20114 -- formal or a return value of type T.
20116 Check_Controlling_Formals (Disp_Typ, Prim);
20120 Next_Entity (Prim);
20124 -- For the tagged case, the two views can share the same primitive
20125 -- operations list and the same class-wide type. Update attributes
20126 -- of the class-wide type which depend on the full declaration.
20128 if Is_Tagged_Type (Priv_T) then
20129 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20130 Set_Class_Wide_Type
20131 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20133 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20138 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20140 if Known_To_Have_Preelab_Init (Priv_T) then
20142 -- Case where there is a pragma Preelaborable_Initialization. We
20143 -- always allow this in predefined units, which is cheating a bit,
20144 -- but it means we don't have to struggle to meet the requirements in
20145 -- the RM for having Preelaborable Initialization. Otherwise we
20146 -- require that the type meets the RM rules. But we can't check that
20147 -- yet, because of the rule about overriding Initialize, so we simply
20148 -- set a flag that will be checked at freeze time.
20150 if not In_Predefined_Unit (Full_T) then
20151 Set_Must_Have_Preelab_Init (Full_T);
20155 -- If pragma CPP_Class was applied to the private type declaration,
20156 -- propagate it now to the full type declaration.
20158 if Is_CPP_Class (Priv_T) then
20159 Set_Is_CPP_Class (Full_T);
20160 Set_Convention (Full_T, Convention_CPP);
20162 -- Check that components of imported CPP types do not have default
20165 Check_CPP_Type_Has_No_Defaults (Full_T);
20168 -- If the private view has user specified stream attributes, then so has
20171 -- Why the test, how could these flags be already set in Full_T ???
20173 if Has_Specified_Stream_Read (Priv_T) then
20174 Set_Has_Specified_Stream_Read (Full_T);
20177 if Has_Specified_Stream_Write (Priv_T) then
20178 Set_Has_Specified_Stream_Write (Full_T);
20181 if Has_Specified_Stream_Input (Priv_T) then
20182 Set_Has_Specified_Stream_Input (Full_T);
20185 if Has_Specified_Stream_Output (Priv_T) then
20186 Set_Has_Specified_Stream_Output (Full_T);
20189 if Is_Ghost_Entity (Priv_T) then
20191 -- The Ghost policy in effect at the point of declaration and at the
20192 -- point of completion must match (SPARK RM 6.9(14)).
20194 Check_Ghost_Completion (Priv_T, Full_T);
20196 -- Propagate the attributes related to pragma Ghost from the private
20197 -- to the full view.
20199 Mark_Full_View_As_Ghost (Priv_T, Full_T);
20202 -- Propagate Default_Initial_Condition-related attributes from the
20203 -- partial view to the full view and its base type.
20205 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20206 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20208 -- Propagate invariant-related attributes from the partial view to the
20209 -- full view and its base type.
20211 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20212 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20214 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20215 -- in the full view without advertising the inheritance in the partial
20216 -- view. This can only occur when the partial view has no parent type
20217 -- and the full view has an interface as a parent. Any other scenarios
20218 -- are illegal because implemented interfaces must match between the
20221 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
20223 Full_Par : constant Entity_Id := Etype (Full_T);
20224 Priv_Par : constant Entity_Id := Etype (Priv_T);
20227 if not Is_Interface (Priv_Par)
20228 and then Is_Interface (Full_Par)
20229 and then Has_Inheritable_Invariants (Full_Par)
20232 ("hidden inheritance of class-wide type invariants not "
20238 -- Propagate predicates to full type, and predicate function if already
20239 -- defined. It is not clear that this can actually happen? the partial
20240 -- view cannot be frozen yet, and the predicate function has not been
20241 -- built. Still it is a cheap check and seems safer to make it.
20243 if Has_Predicates (Priv_T) then
20244 Set_Has_Predicates (Full_T);
20246 if Present (Predicate_Function (Priv_T)) then
20247 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20250 end Process_Full_View;
20252 -----------------------------------
20253 -- Process_Incomplete_Dependents --
20254 -----------------------------------
20256 procedure Process_Incomplete_Dependents
20258 Full_T : Entity_Id;
20261 Inc_Elmt : Elmt_Id;
20262 Priv_Dep : Entity_Id;
20263 New_Subt : Entity_Id;
20265 Disc_Constraint : Elist_Id;
20268 if No (Private_Dependents (Inc_T)) then
20272 -- Itypes that may be generated by the completion of an incomplete
20273 -- subtype are not used by the back-end and not attached to the tree.
20274 -- They are created only for constraint-checking purposes.
20276 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20277 while Present (Inc_Elmt) loop
20278 Priv_Dep := Node (Inc_Elmt);
20280 if Ekind (Priv_Dep) = E_Subprogram_Type then
20282 -- An Access_To_Subprogram type may have a return type or a
20283 -- parameter type that is incomplete. Replace with the full view.
20285 if Etype (Priv_Dep) = Inc_T then
20286 Set_Etype (Priv_Dep, Full_T);
20290 Formal : Entity_Id;
20293 Formal := First_Formal (Priv_Dep);
20294 while Present (Formal) loop
20295 if Etype (Formal) = Inc_T then
20296 Set_Etype (Formal, Full_T);
20299 Next_Formal (Formal);
20303 elsif Is_Overloadable (Priv_Dep) then
20305 -- If a subprogram in the incomplete dependents list is primitive
20306 -- for a tagged full type then mark it as a dispatching operation,
20307 -- check whether it overrides an inherited subprogram, and check
20308 -- restrictions on its controlling formals. Note that a protected
20309 -- operation is never dispatching: only its wrapper operation
20310 -- (which has convention Ada) is.
20312 if Is_Tagged_Type (Full_T)
20313 and then Is_Primitive (Priv_Dep)
20314 and then Convention (Priv_Dep) /= Convention_Protected
20316 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20317 Set_Is_Dispatching_Operation (Priv_Dep);
20318 Check_Controlling_Formals (Full_T, Priv_Dep);
20321 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20323 -- Can happen during processing of a body before the completion
20324 -- of a TA type. Ignore, because spec is also on dependent list.
20328 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20329 -- corresponding subtype of the full view.
20331 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20332 Set_Subtype_Indication
20333 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20334 Set_Etype (Priv_Dep, Full_T);
20335 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20336 Set_Analyzed (Parent (Priv_Dep), False);
20338 -- Reanalyze the declaration, suppressing the call to
20339 -- Enter_Name to avoid duplicate names.
20341 Analyze_Subtype_Declaration
20342 (N => Parent (Priv_Dep),
20345 -- Dependent is a subtype
20348 -- We build a new subtype indication using the full view of the
20349 -- incomplete parent. The discriminant constraints have been
20350 -- elaborated already at the point of the subtype declaration.
20352 New_Subt := Create_Itype (E_Void, N);
20354 if Has_Discriminants (Full_T) then
20355 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20357 Disc_Constraint := No_Elist;
20360 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20361 Set_Full_View (Priv_Dep, New_Subt);
20364 Next_Elmt (Inc_Elmt);
20366 end Process_Incomplete_Dependents;
20368 --------------------------------
20369 -- Process_Range_Expr_In_Decl --
20370 --------------------------------
20372 procedure Process_Range_Expr_In_Decl
20375 Subtyp : Entity_Id := Empty;
20376 Check_List : List_Id := Empty_List;
20377 R_Check_Off : Boolean := False;
20378 In_Iter_Schm : Boolean := False)
20381 R_Checks : Check_Result;
20382 Insert_Node : Node_Id;
20383 Def_Id : Entity_Id;
20386 Analyze_And_Resolve (R, Base_Type (T));
20388 if Nkind (R) = N_Range then
20390 -- In SPARK, all ranges should be static, with the exception of the
20391 -- discrete type definition of a loop parameter specification.
20393 if not In_Iter_Schm
20394 and then not Is_OK_Static_Range (R)
20396 Check_SPARK_05_Restriction ("range should be static", R);
20399 Lo := Low_Bound (R);
20400 Hi := High_Bound (R);
20402 -- Validity checks on the range of a quantified expression are
20403 -- delayed until the construct is transformed into a loop.
20405 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20406 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20410 -- We need to ensure validity of the bounds here, because if we
20411 -- go ahead and do the expansion, then the expanded code will get
20412 -- analyzed with range checks suppressed and we miss the check.
20414 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20415 -- the temporaries generated by routine Remove_Side_Effects by means
20416 -- of validity checks must use the same names. When a range appears
20417 -- in the parent of a generic, the range is processed with checks
20418 -- disabled as part of the generic context and with checks enabled
20419 -- for code generation purposes. This leads to link issues as the
20420 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20421 -- template sees the temporaries generated by Remove_Side_Effects.
20424 Validity_Check_Range (R, Subtyp);
20427 -- If there were errors in the declaration, try and patch up some
20428 -- common mistakes in the bounds. The cases handled are literals
20429 -- which are Integer where the expected type is Real and vice versa.
20430 -- These corrections allow the compilation process to proceed further
20431 -- along since some basic assumptions of the format of the bounds
20434 if Etype (R) = Any_Type then
20435 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20437 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20439 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20441 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20443 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20445 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20447 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20449 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20456 -- If the bounds of the range have been mistakenly given as string
20457 -- literals (perhaps in place of character literals), then an error
20458 -- has already been reported, but we rewrite the string literal as a
20459 -- bound of the range's type to avoid blowups in later processing
20460 -- that looks at static values.
20462 if Nkind (Lo) = N_String_Literal then
20464 Make_Attribute_Reference (Sloc (Lo),
20465 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20466 Attribute_Name => Name_First));
20467 Analyze_And_Resolve (Lo);
20470 if Nkind (Hi) = N_String_Literal then
20472 Make_Attribute_Reference (Sloc (Hi),
20473 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20474 Attribute_Name => Name_First));
20475 Analyze_And_Resolve (Hi);
20478 -- If bounds aren't scalar at this point then exit, avoiding
20479 -- problems with further processing of the range in this procedure.
20481 if not Is_Scalar_Type (Etype (Lo)) then
20485 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20486 -- then range of the base type. Here we check whether the bounds
20487 -- are in the range of the subtype itself. Note that if the bounds
20488 -- represent the null range the Constraint_Error exception should
20491 -- ??? The following code should be cleaned up as follows
20493 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20494 -- is done in the call to Range_Check (R, T); below
20496 -- 2. The use of R_Check_Off should be investigated and possibly
20497 -- removed, this would clean up things a bit.
20499 if Is_Null_Range (Lo, Hi) then
20503 -- Capture values of bounds and generate temporaries for them
20504 -- if needed, before applying checks, since checks may cause
20505 -- duplication of the expression without forcing evaluation.
20507 -- The forced evaluation removes side effects from expressions,
20508 -- which should occur also in GNATprove mode. Otherwise, we end up
20509 -- with unexpected insertions of actions at places where this is
20510 -- not supposed to occur, e.g. on default parameters of a call.
20512 if Expander_Active or GNATprove_Mode then
20514 -- Call Force_Evaluation to create declarations as needed to
20515 -- deal with side effects, and also create typ_FIRST/LAST
20516 -- entities for bounds if we have a subtype name.
20518 -- Note: we do this transformation even if expansion is not
20519 -- active if we are in GNATprove_Mode since the transformation
20520 -- is in general required to ensure that the resulting tree has
20521 -- proper Ada semantics.
20524 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20526 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20529 -- We use a flag here instead of suppressing checks on the type
20530 -- because the type we check against isn't necessarily the place
20531 -- where we put the check.
20533 if not R_Check_Off then
20534 R_Checks := Get_Range_Checks (R, T);
20536 -- Look up tree to find an appropriate insertion point. We
20537 -- can't just use insert_actions because later processing
20538 -- depends on the insertion node. Prior to Ada 2012 the
20539 -- insertion point could only be a declaration or a loop, but
20540 -- quantified expressions can appear within any context in an
20541 -- expression, and the insertion point can be any statement,
20542 -- pragma, or declaration.
20544 Insert_Node := Parent (R);
20545 while Present (Insert_Node) loop
20547 Nkind (Insert_Node) in N_Declaration
20550 (Insert_Node, N_Component_Declaration,
20551 N_Loop_Parameter_Specification,
20552 N_Function_Specification,
20553 N_Procedure_Specification);
20555 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20556 or else Nkind (Insert_Node) in
20557 N_Statement_Other_Than_Procedure_Call
20558 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20561 Insert_Node := Parent (Insert_Node);
20564 -- Why would Type_Decl not be present??? Without this test,
20565 -- short regression tests fail.
20567 if Present (Insert_Node) then
20569 -- Case of loop statement. Verify that the range is part
20570 -- of the subtype indication of the iteration scheme.
20572 if Nkind (Insert_Node) = N_Loop_Statement then
20577 Indic := Parent (R);
20578 while Present (Indic)
20579 and then Nkind (Indic) /= N_Subtype_Indication
20581 Indic := Parent (Indic);
20584 if Present (Indic) then
20585 Def_Id := Etype (Subtype_Mark (Indic));
20587 Insert_Range_Checks
20591 Sloc (Insert_Node),
20593 Do_Before => True);
20597 -- Insertion before a declaration. If the declaration
20598 -- includes discriminants, the list of applicable checks
20599 -- is given by the caller.
20601 elsif Nkind (Insert_Node) in N_Declaration then
20602 Def_Id := Defining_Identifier (Insert_Node);
20604 if (Ekind (Def_Id) = E_Record_Type
20605 and then Depends_On_Discriminant (R))
20607 (Ekind (Def_Id) = E_Protected_Type
20608 and then Has_Discriminants (Def_Id))
20610 Append_Range_Checks
20612 Check_List, Def_Id, Sloc (Insert_Node), R);
20615 Insert_Range_Checks
20617 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20621 -- Insertion before a statement. Range appears in the
20622 -- context of a quantified expression. Insertion will
20623 -- take place when expression is expanded.
20632 -- Case of other than an explicit N_Range node
20634 -- The forced evaluation removes side effects from expressions, which
20635 -- should occur also in GNATprove mode. Otherwise, we end up with
20636 -- unexpected insertions of actions at places where this is not
20637 -- supposed to occur, e.g. on default parameters of a call.
20639 elsif Expander_Active or GNATprove_Mode then
20640 Get_Index_Bounds (R, Lo, Hi);
20641 Force_Evaluation (Lo);
20642 Force_Evaluation (Hi);
20644 end Process_Range_Expr_In_Decl;
20646 --------------------------------------
20647 -- Process_Real_Range_Specification --
20648 --------------------------------------
20650 procedure Process_Real_Range_Specification (Def : Node_Id) is
20651 Spec : constant Node_Id := Real_Range_Specification (Def);
20654 Err : Boolean := False;
20656 procedure Analyze_Bound (N : Node_Id);
20657 -- Analyze and check one bound
20659 -------------------
20660 -- Analyze_Bound --
20661 -------------------
20663 procedure Analyze_Bound (N : Node_Id) is
20665 Analyze_And_Resolve (N, Any_Real);
20667 if not Is_OK_Static_Expression (N) then
20668 Flag_Non_Static_Expr
20669 ("bound in real type definition is not static!", N);
20674 -- Start of processing for Process_Real_Range_Specification
20677 if Present (Spec) then
20678 Lo := Low_Bound (Spec);
20679 Hi := High_Bound (Spec);
20680 Analyze_Bound (Lo);
20681 Analyze_Bound (Hi);
20683 -- If error, clear away junk range specification
20686 Set_Real_Range_Specification (Def, Empty);
20689 end Process_Real_Range_Specification;
20691 ---------------------
20692 -- Process_Subtype --
20693 ---------------------
20695 function Process_Subtype
20697 Related_Nod : Node_Id;
20698 Related_Id : Entity_Id := Empty;
20699 Suffix : Character := ' ') return Entity_Id
20702 Def_Id : Entity_Id;
20703 Error_Node : Node_Id;
20704 Full_View_Id : Entity_Id;
20705 Subtype_Mark_Id : Entity_Id;
20707 May_Have_Null_Exclusion : Boolean;
20709 procedure Check_Incomplete (T : Node_Id);
20710 -- Called to verify that an incomplete type is not used prematurely
20712 ----------------------
20713 -- Check_Incomplete --
20714 ----------------------
20716 procedure Check_Incomplete (T : Node_Id) is
20718 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20720 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20722 not (Ada_Version >= Ada_2005
20724 (Nkind (Parent (T)) = N_Subtype_Declaration
20725 or else (Nkind (Parent (T)) = N_Subtype_Indication
20726 and then Nkind (Parent (Parent (T))) =
20727 N_Subtype_Declaration)))
20729 Error_Msg_N ("invalid use of type before its full declaration", T);
20731 end Check_Incomplete;
20733 -- Start of processing for Process_Subtype
20736 -- Case of no constraints present
20738 if Nkind (S) /= N_Subtype_Indication then
20740 Check_Incomplete (S);
20743 -- Ada 2005 (AI-231): Static check
20745 if Ada_Version >= Ada_2005
20746 and then Present (P)
20747 and then Null_Exclusion_Present (P)
20748 and then Nkind (P) /= N_Access_To_Object_Definition
20749 and then not Is_Access_Type (Entity (S))
20751 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20754 -- The following is ugly, can't we have a range or even a flag???
20756 May_Have_Null_Exclusion :=
20757 Nkind_In (P, N_Access_Definition,
20758 N_Access_Function_Definition,
20759 N_Access_Procedure_Definition,
20760 N_Access_To_Object_Definition,
20762 N_Component_Definition)
20764 Nkind_In (P, N_Derived_Type_Definition,
20765 N_Discriminant_Specification,
20766 N_Formal_Object_Declaration,
20767 N_Object_Declaration,
20768 N_Object_Renaming_Declaration,
20769 N_Parameter_Specification,
20770 N_Subtype_Declaration);
20772 -- Create an Itype that is a duplicate of Entity (S) but with the
20773 -- null-exclusion attribute.
20775 if May_Have_Null_Exclusion
20776 and then Is_Access_Type (Entity (S))
20777 and then Null_Exclusion_Present (P)
20779 -- No need to check the case of an access to object definition.
20780 -- It is correct to define double not-null pointers.
20783 -- type Not_Null_Int_Ptr is not null access Integer;
20784 -- type Acc is not null access Not_Null_Int_Ptr;
20786 and then Nkind (P) /= N_Access_To_Object_Definition
20788 if Can_Never_Be_Null (Entity (S)) then
20789 case Nkind (Related_Nod) is
20790 when N_Full_Type_Declaration =>
20791 if Nkind (Type_Definition (Related_Nod))
20792 in N_Array_Type_Definition
20796 (Component_Definition
20797 (Type_Definition (Related_Nod)));
20800 Subtype_Indication (Type_Definition (Related_Nod));
20803 when N_Subtype_Declaration =>
20804 Error_Node := Subtype_Indication (Related_Nod);
20806 when N_Object_Declaration =>
20807 Error_Node := Object_Definition (Related_Nod);
20809 when N_Component_Declaration =>
20811 Subtype_Indication (Component_Definition (Related_Nod));
20813 when N_Allocator =>
20814 Error_Node := Expression (Related_Nod);
20817 pragma Assert (False);
20818 Error_Node := Related_Nod;
20822 ("`NOT NULL` not allowed (& already excludes null)",
20828 Create_Null_Excluding_Itype
20830 Related_Nod => P));
20831 Set_Entity (S, Etype (S));
20836 -- Case of constraint present, so that we have an N_Subtype_Indication
20837 -- node (this node is created only if constraints are present).
20840 Find_Type (Subtype_Mark (S));
20842 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
20844 (Nkind (Parent (S)) = N_Subtype_Declaration
20845 and then Is_Itype (Defining_Identifier (Parent (S))))
20847 Check_Incomplete (Subtype_Mark (S));
20851 Subtype_Mark_Id := Entity (Subtype_Mark (S));
20853 -- Explicit subtype declaration case
20855 if Nkind (P) = N_Subtype_Declaration then
20856 Def_Id := Defining_Identifier (P);
20858 -- Explicit derived type definition case
20860 elsif Nkind (P) = N_Derived_Type_Definition then
20861 Def_Id := Defining_Identifier (Parent (P));
20863 -- Implicit case, the Def_Id must be created as an implicit type.
20864 -- The one exception arises in the case of concurrent types, array
20865 -- and access types, where other subsidiary implicit types may be
20866 -- created and must appear before the main implicit type. In these
20867 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
20868 -- has not yet been called to create Def_Id.
20871 if Is_Array_Type (Subtype_Mark_Id)
20872 or else Is_Concurrent_Type (Subtype_Mark_Id)
20873 or else Is_Access_Type (Subtype_Mark_Id)
20877 -- For the other cases, we create a new unattached Itype,
20878 -- and set the indication to ensure it gets attached later.
20882 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20886 -- If the kind of constraint is invalid for this kind of type,
20887 -- then give an error, and then pretend no constraint was given.
20889 if not Is_Valid_Constraint_Kind
20890 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
20893 ("incorrect constraint for this kind of type", Constraint (S));
20895 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
20897 -- Set Ekind of orphan itype, to prevent cascaded errors
20899 if Present (Def_Id) then
20900 Set_Ekind (Def_Id, Ekind (Any_Type));
20903 -- Make recursive call, having got rid of the bogus constraint
20905 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
20908 -- Remaining processing depends on type. Select on Base_Type kind to
20909 -- ensure getting to the concrete type kind in the case of a private
20910 -- subtype (needed when only doing semantic analysis).
20912 case Ekind (Base_Type (Subtype_Mark_Id)) is
20913 when Access_Kind =>
20915 -- If this is a constraint on a class-wide type, discard it.
20916 -- There is currently no way to express a partial discriminant
20917 -- constraint on a type with unknown discriminants. This is
20918 -- a pathology that the ACATS wisely decides not to test.
20920 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
20921 if Comes_From_Source (S) then
20923 ("constraint on class-wide type ignored??",
20927 if Nkind (P) = N_Subtype_Declaration then
20928 Set_Subtype_Indication (P,
20929 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
20932 return Subtype_Mark_Id;
20935 Constrain_Access (Def_Id, S, Related_Nod);
20938 and then Is_Itype (Designated_Type (Def_Id))
20939 and then Nkind (Related_Nod) = N_Subtype_Declaration
20940 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
20942 Build_Itype_Reference
20943 (Designated_Type (Def_Id), Related_Nod);
20947 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
20949 when Decimal_Fixed_Point_Kind =>
20950 Constrain_Decimal (Def_Id, S);
20952 when Enumeration_Kind =>
20953 Constrain_Enumeration (Def_Id, S);
20954 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20956 when Ordinary_Fixed_Point_Kind =>
20957 Constrain_Ordinary_Fixed (Def_Id, S);
20960 Constrain_Float (Def_Id, S);
20962 when Integer_Kind =>
20963 Constrain_Integer (Def_Id, S);
20964 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20966 when E_Record_Type |
20969 E_Incomplete_Type =>
20970 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20972 if Ekind (Def_Id) = E_Incomplete_Type then
20973 Set_Private_Dependents (Def_Id, New_Elmt_List);
20976 when Private_Kind =>
20977 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20979 -- The base type may be private but Def_Id may be a full view
20982 if Is_Private_Type (Def_Id) then
20983 Set_Private_Dependents (Def_Id, New_Elmt_List);
20986 -- In case of an invalid constraint prevent further processing
20987 -- since the type constructed is missing expected fields.
20989 if Etype (Def_Id) = Any_Type then
20993 -- If the full view is that of a task with discriminants,
20994 -- we must constrain both the concurrent type and its
20995 -- corresponding record type. Otherwise we will just propagate
20996 -- the constraint to the full view, if available.
20998 if Present (Full_View (Subtype_Mark_Id))
20999 and then Has_Discriminants (Subtype_Mark_Id)
21000 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21003 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21005 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21006 Constrain_Concurrent (Full_View_Id, S,
21007 Related_Nod, Related_Id, Suffix);
21008 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21009 Set_Full_View (Def_Id, Full_View_Id);
21011 -- Introduce an explicit reference to the private subtype,
21012 -- to prevent scope anomalies in gigi if first use appears
21013 -- in a nested context, e.g. a later function body.
21014 -- Should this be generated in other contexts than a full
21015 -- type declaration?
21017 if Is_Itype (Def_Id)
21019 Nkind (Parent (P)) = N_Full_Type_Declaration
21021 Build_Itype_Reference (Def_Id, Parent (P));
21025 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21028 when Concurrent_Kind =>
21029 Constrain_Concurrent (Def_Id, S,
21030 Related_Nod, Related_Id, Suffix);
21033 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21036 -- Size and Convention are always inherited from the base type
21038 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21039 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21043 end Process_Subtype;
21045 -----------------------------
21046 -- Record_Type_Declaration --
21047 -----------------------------
21049 procedure Record_Type_Declaration
21054 Def : constant Node_Id := Type_Definition (N);
21055 Is_Tagged : Boolean;
21056 Tag_Comp : Entity_Id;
21059 -- These flags must be initialized before calling Process_Discriminants
21060 -- because this routine makes use of them.
21062 Set_Ekind (T, E_Record_Type);
21064 Init_Size_Align (T);
21065 Set_Interfaces (T, No_Elist);
21066 Set_Stored_Constraint (T, No_Elist);
21067 Set_Default_SSO (T);
21071 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21072 if Limited_Present (Def) then
21073 Check_SPARK_05_Restriction ("limited is not allowed", N);
21076 if Abstract_Present (Def) then
21077 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21080 -- The flag Is_Tagged_Type might have already been set by
21081 -- Find_Type_Name if it detected an error for declaration T. This
21082 -- arises in the case of private tagged types where the full view
21083 -- omits the word tagged.
21086 Tagged_Present (Def)
21087 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21089 Set_Is_Limited_Record (T, Limited_Present (Def));
21092 Set_Is_Tagged_Type (T, True);
21093 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21096 -- Type is abstract if full declaration carries keyword, or if
21097 -- previous partial view did.
21099 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21100 or else Abstract_Present (Def));
21103 Check_SPARK_05_Restriction ("interface is not allowed", N);
21106 Analyze_Interface_Declaration (T, Def);
21108 if Present (Discriminant_Specifications (N)) then
21110 ("interface types cannot have discriminants",
21111 Defining_Identifier
21112 (First (Discriminant_Specifications (N))));
21116 -- First pass: if there are self-referential access components,
21117 -- create the required anonymous access type declarations, and if
21118 -- need be an incomplete type declaration for T itself.
21120 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21122 if Ada_Version >= Ada_2005
21123 and then Present (Interface_List (Def))
21125 Check_Interfaces (N, Def);
21128 Ifaces_List : Elist_Id;
21131 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21132 -- already in the parents.
21136 Ifaces_List => Ifaces_List,
21137 Exclude_Parents => True);
21139 Set_Interfaces (T, Ifaces_List);
21143 -- Records constitute a scope for the component declarations within.
21144 -- The scope is created prior to the processing of these declarations.
21145 -- Discriminants are processed first, so that they are visible when
21146 -- processing the other components. The Ekind of the record type itself
21147 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21149 -- Enter record scope
21153 -- If an incomplete or private type declaration was already given for
21154 -- the type, then this scope already exists, and the discriminants have
21155 -- been declared within. We must verify that the full declaration
21156 -- matches the incomplete one.
21158 Check_Or_Process_Discriminants (N, T, Prev);
21160 Set_Is_Constrained (T, not Has_Discriminants (T));
21161 Set_Has_Delayed_Freeze (T, True);
21163 -- For tagged types add a manually analyzed component corresponding
21164 -- to the component _tag, the corresponding piece of tree will be
21165 -- expanded as part of the freezing actions if it is not a CPP_Class.
21169 -- Do not add the tag unless we are in expansion mode
21171 if Expander_Active then
21172 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21173 Enter_Name (Tag_Comp);
21175 Set_Ekind (Tag_Comp, E_Component);
21176 Set_Is_Tag (Tag_Comp);
21177 Set_Is_Aliased (Tag_Comp);
21178 Set_Etype (Tag_Comp, RTE (RE_Tag));
21179 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21180 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21181 Init_Component_Location (Tag_Comp);
21183 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21184 -- implemented interfaces.
21186 if Has_Interfaces (T) then
21187 Add_Interface_Tag_Components (N, T);
21191 Make_Class_Wide_Type (T);
21192 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21195 -- We must suppress range checks when processing record components in
21196 -- the presence of discriminants, since we don't want spurious checks to
21197 -- be generated during their analysis, but Suppress_Range_Checks flags
21198 -- must be reset the after processing the record definition.
21200 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21201 -- couldn't we just use the normal range check suppression method here.
21202 -- That would seem cleaner ???
21204 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21205 Set_Kill_Range_Checks (T, True);
21206 Record_Type_Definition (Def, Prev);
21207 Set_Kill_Range_Checks (T, False);
21209 Record_Type_Definition (Def, Prev);
21212 -- Exit from record scope
21216 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21217 -- the implemented interfaces and associate them an aliased entity.
21220 and then not Is_Empty_List (Interface_List (Def))
21222 Derive_Progenitor_Subprograms (T, T);
21225 Check_Function_Writable_Actuals (N);
21226 end Record_Type_Declaration;
21228 ----------------------------
21229 -- Record_Type_Definition --
21230 ----------------------------
21232 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21233 Component : Entity_Id;
21234 Ctrl_Components : Boolean := False;
21235 Final_Storage_Only : Boolean;
21239 if Ekind (Prev_T) = E_Incomplete_Type then
21240 T := Full_View (Prev_T);
21245 -- In SPARK, tagged types and type extensions may only be declared in
21246 -- the specification of library unit packages.
21248 if Present (Def) and then Is_Tagged_Type (T) then
21254 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21255 Typ := Parent (Def);
21258 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21259 Typ := Parent (Parent (Def));
21262 Ctxt := Parent (Typ);
21264 if Nkind (Ctxt) = N_Package_Body
21265 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21267 Check_SPARK_05_Restriction
21268 ("type should be defined in package specification", Typ);
21270 elsif Nkind (Ctxt) /= N_Package_Specification
21271 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21273 Check_SPARK_05_Restriction
21274 ("type should be defined in library unit package", Typ);
21279 Final_Storage_Only := not Is_Controlled_Active (T);
21281 -- Ada 2005: Check whether an explicit Limited is present in a derived
21282 -- type declaration.
21284 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21285 and then Limited_Present (Parent (Def))
21287 Set_Is_Limited_Record (T);
21290 -- If the component list of a record type is defined by the reserved
21291 -- word null and there is no discriminant part, then the record type has
21292 -- no components and all records of the type are null records (RM 3.7)
21293 -- This procedure is also called to process the extension part of a
21294 -- record extension, in which case the current scope may have inherited
21298 or else No (Component_List (Def))
21299 or else Null_Present (Component_List (Def))
21301 if not Is_Tagged_Type (T) then
21302 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21306 Analyze_Declarations (Component_Items (Component_List (Def)));
21308 if Present (Variant_Part (Component_List (Def))) then
21309 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21310 Analyze (Variant_Part (Component_List (Def)));
21314 -- After completing the semantic analysis of the record definition,
21315 -- record components, both new and inherited, are accessible. Set their
21316 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21317 -- whose Ekind may be void.
21319 Component := First_Entity (Current_Scope);
21320 while Present (Component) loop
21321 if Ekind (Component) = E_Void
21322 and then not Is_Itype (Component)
21324 Set_Ekind (Component, E_Component);
21325 Init_Component_Location (Component);
21328 Propagate_Concurrent_Flags (T, Etype (Component));
21330 if Ekind (Component) /= E_Component then
21333 -- Do not set Has_Controlled_Component on a class-wide equivalent
21334 -- type. See Make_CW_Equivalent_Type.
21336 elsif not Is_Class_Wide_Equivalent_Type (T)
21337 and then (Has_Controlled_Component (Etype (Component))
21338 or else (Chars (Component) /= Name_uParent
21339 and then Is_Controlled_Active
21340 (Etype (Component))))
21342 Set_Has_Controlled_Component (T, True);
21343 Final_Storage_Only :=
21345 and then Finalize_Storage_Only (Etype (Component));
21346 Ctrl_Components := True;
21349 Next_Entity (Component);
21352 -- A Type is Finalize_Storage_Only only if all its controlled components
21355 if Ctrl_Components then
21356 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21359 -- Place reference to end record on the proper entity, which may
21360 -- be a partial view.
21362 if Present (Def) then
21363 Process_End_Label (Def, 'e', Prev_T);
21365 end Record_Type_Definition;
21367 ------------------------
21368 -- Replace_Components --
21369 ------------------------
21371 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21372 function Process (N : Node_Id) return Traverse_Result;
21378 function Process (N : Node_Id) return Traverse_Result is
21382 if Nkind (N) = N_Discriminant_Specification then
21383 Comp := First_Discriminant (Typ);
21384 while Present (Comp) loop
21385 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21386 Set_Defining_Identifier (N, Comp);
21390 Next_Discriminant (Comp);
21393 elsif Nkind (N) = N_Component_Declaration then
21394 Comp := First_Component (Typ);
21395 while Present (Comp) loop
21396 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21397 Set_Defining_Identifier (N, Comp);
21401 Next_Component (Comp);
21408 procedure Replace is new Traverse_Proc (Process);
21410 -- Start of processing for Replace_Components
21414 end Replace_Components;
21416 -------------------------------
21417 -- Set_Completion_Referenced --
21418 -------------------------------
21420 procedure Set_Completion_Referenced (E : Entity_Id) is
21422 -- If in main unit, mark entity that is a completion as referenced,
21423 -- warnings go on the partial view when needed.
21425 if In_Extended_Main_Source_Unit (E) then
21426 Set_Referenced (E);
21428 end Set_Completion_Referenced;
21430 ---------------------
21431 -- Set_Default_SSO --
21432 ---------------------
21434 procedure Set_Default_SSO (T : Entity_Id) is
21436 case Opt.Default_SSO is
21440 Set_SSO_Set_Low_By_Default (T, True);
21442 Set_SSO_Set_High_By_Default (T, True);
21444 raise Program_Error;
21446 end Set_Default_SSO;
21448 ---------------------
21449 -- Set_Fixed_Range --
21450 ---------------------
21452 -- The range for fixed-point types is complicated by the fact that we
21453 -- do not know the exact end points at the time of the declaration. This
21454 -- is true for three reasons:
21456 -- A size clause may affect the fudging of the end-points.
21457 -- A small clause may affect the values of the end-points.
21458 -- We try to include the end-points if it does not affect the size.
21460 -- This means that the actual end-points must be established at the
21461 -- point when the type is frozen. Meanwhile, we first narrow the range
21462 -- as permitted (so that it will fit if necessary in a small specified
21463 -- size), and then build a range subtree with these narrowed bounds.
21464 -- Set_Fixed_Range constructs the range from real literal values, and
21465 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21467 -- The parent of this range is set to point to the entity so that it is
21468 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21469 -- other scalar types, which are just pointers to the range in the
21470 -- original tree, this would otherwise be an orphan).
21472 -- The tree is left unanalyzed. When the type is frozen, the processing
21473 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21474 -- analyzed, and uses this as an indication that it should complete
21475 -- work on the range (it will know the final small and size values).
21477 procedure Set_Fixed_Range
21483 S : constant Node_Id :=
21485 Low_Bound => Make_Real_Literal (Loc, Lo),
21486 High_Bound => Make_Real_Literal (Loc, Hi));
21488 Set_Scalar_Range (E, S);
21491 -- Before the freeze point, the bounds of a fixed point are universal
21492 -- and carry the corresponding type.
21494 Set_Etype (Low_Bound (S), Universal_Real);
21495 Set_Etype (High_Bound (S), Universal_Real);
21496 end Set_Fixed_Range;
21498 ----------------------------------
21499 -- Set_Scalar_Range_For_Subtype --
21500 ----------------------------------
21502 procedure Set_Scalar_Range_For_Subtype
21503 (Def_Id : Entity_Id;
21507 Kind : constant Entity_Kind := Ekind (Def_Id);
21510 -- Defend against previous error
21512 if Nkind (R) = N_Error then
21516 Set_Scalar_Range (Def_Id, R);
21518 -- We need to link the range into the tree before resolving it so
21519 -- that types that are referenced, including importantly the subtype
21520 -- itself, are properly frozen (Freeze_Expression requires that the
21521 -- expression be properly linked into the tree). Of course if it is
21522 -- already linked in, then we do not disturb the current link.
21524 if No (Parent (R)) then
21525 Set_Parent (R, Def_Id);
21528 -- Reset the kind of the subtype during analysis of the range, to
21529 -- catch possible premature use in the bounds themselves.
21531 Set_Ekind (Def_Id, E_Void);
21532 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21533 Set_Ekind (Def_Id, Kind);
21534 end Set_Scalar_Range_For_Subtype;
21536 --------------------------------------------------------
21537 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21538 --------------------------------------------------------
21540 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21544 -- Make sure set if encountered during Expand_To_Stored_Constraint
21546 Set_Stored_Constraint (E, No_Elist);
21548 -- Give it the right value
21550 if Is_Constrained (E) and then Has_Discriminants (E) then
21551 Set_Stored_Constraint (E,
21552 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21554 end Set_Stored_Constraint_From_Discriminant_Constraint;
21556 -------------------------------------
21557 -- Signed_Integer_Type_Declaration --
21558 -------------------------------------
21560 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21561 Implicit_Base : Entity_Id;
21562 Base_Typ : Entity_Id;
21565 Errs : Boolean := False;
21569 function Can_Derive_From (E : Entity_Id) return Boolean;
21570 -- Determine whether given bounds allow derivation from specified type
21572 procedure Check_Bound (Expr : Node_Id);
21573 -- Check bound to make sure it is integral and static. If not, post
21574 -- appropriate error message and set Errs flag
21576 ---------------------
21577 -- Can_Derive_From --
21578 ---------------------
21580 -- Note we check both bounds against both end values, to deal with
21581 -- strange types like ones with a range of 0 .. -12341234.
21583 function Can_Derive_From (E : Entity_Id) return Boolean is
21584 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21585 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21587 return Lo <= Lo_Val and then Lo_Val <= Hi
21589 Lo <= Hi_Val and then Hi_Val <= Hi;
21590 end Can_Derive_From;
21596 procedure Check_Bound (Expr : Node_Id) is
21598 -- If a range constraint is used as an integer type definition, each
21599 -- bound of the range must be defined by a static expression of some
21600 -- integer type, but the two bounds need not have the same integer
21601 -- type (Negative bounds are allowed.) (RM 3.5.4)
21603 if not Is_Integer_Type (Etype (Expr)) then
21605 ("integer type definition bounds must be of integer type", Expr);
21608 elsif not Is_OK_Static_Expression (Expr) then
21609 Flag_Non_Static_Expr
21610 ("non-static expression used for integer type bound!", Expr);
21613 -- The bounds are folded into literals, and we set their type to be
21614 -- universal, to avoid typing difficulties: we cannot set the type
21615 -- of the literal to the new type, because this would be a forward
21616 -- reference for the back end, and if the original type is user-
21617 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21620 if Is_Entity_Name (Expr) then
21621 Fold_Uint (Expr, Expr_Value (Expr), True);
21624 Set_Etype (Expr, Universal_Integer);
21628 -- Start of processing for Signed_Integer_Type_Declaration
21631 -- Create an anonymous base type
21634 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21636 -- Analyze and check the bounds, they can be of any integer type
21638 Lo := Low_Bound (Def);
21639 Hi := High_Bound (Def);
21641 -- Arbitrarily use Integer as the type if either bound had an error
21643 if Hi = Error or else Lo = Error then
21644 Base_Typ := Any_Integer;
21645 Set_Error_Posted (T, True);
21647 -- Here both bounds are OK expressions
21650 Analyze_And_Resolve (Lo, Any_Integer);
21651 Analyze_And_Resolve (Hi, Any_Integer);
21657 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21658 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21661 -- Find type to derive from
21663 Lo_Val := Expr_Value (Lo);
21664 Hi_Val := Expr_Value (Hi);
21666 if Can_Derive_From (Standard_Short_Short_Integer) then
21667 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21669 elsif Can_Derive_From (Standard_Short_Integer) then
21670 Base_Typ := Base_Type (Standard_Short_Integer);
21672 elsif Can_Derive_From (Standard_Integer) then
21673 Base_Typ := Base_Type (Standard_Integer);
21675 elsif Can_Derive_From (Standard_Long_Integer) then
21676 Base_Typ := Base_Type (Standard_Long_Integer);
21678 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21679 Check_Restriction (No_Long_Long_Integers, Def);
21680 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21683 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21684 Error_Msg_N ("integer type definition bounds out of range", Def);
21685 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21686 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21690 -- Complete both implicit base and declared first subtype entities. The
21691 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21692 -- are not clobbered when the signed integer type acts as a full view of
21695 Set_Etype (Implicit_Base, Base_Typ);
21696 Set_Size_Info (Implicit_Base, Base_Typ);
21697 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21698 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21699 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21701 Set_Ekind (T, E_Signed_Integer_Subtype);
21702 Set_Etype (T, Implicit_Base);
21703 Set_Size_Info (T, Implicit_Base);
21704 Inherit_Rep_Item_Chain (T, Implicit_Base);
21705 Set_Scalar_Range (T, Def);
21706 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21707 Set_Is_Constrained (T);
21708 end Signed_Integer_Type_Declaration;