1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2015, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Debug; use Debug;
30 with Elists; use Elists;
31 with Einfo; use Einfo;
32 with Errout; use Errout;
33 with Eval_Fat; use Eval_Fat;
34 with Exp_Ch3; use Exp_Ch3;
35 with Exp_Ch9; use Exp_Ch9;
36 with Exp_Disp; use Exp_Disp;
37 with Exp_Dist; use Exp_Dist;
38 with Exp_Tss; use Exp_Tss;
39 with Exp_Util; use Exp_Util;
40 with Fname; use Fname;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch10; use Sem_Ch10;
61 with Sem_Ch12; use Sem_Ch12;
62 with Sem_Ch13; use Sem_Ch13;
63 with Sem_Dim; use Sem_Dim;
64 with Sem_Disp; use Sem_Disp;
65 with Sem_Dist; use Sem_Dist;
66 with Sem_Elim; use Sem_Elim;
67 with Sem_Eval; use Sem_Eval;
68 with Sem_Mech; use Sem_Mech;
69 with Sem_Prag; use Sem_Prag;
70 with Sem_Res; use Sem_Res;
71 with Sem_Smem; use Sem_Smem;
72 with Sem_Type; use Sem_Type;
73 with Sem_Util; use Sem_Util;
74 with Sem_Warn; use Sem_Warn;
75 with Stand; use Stand;
76 with Sinfo; use Sinfo;
77 with Sinput; use Sinput;
78 with Snames; use Snames;
79 with Targparm; use Targparm;
80 with Tbuild; use Tbuild;
81 with Ttypes; use Ttypes;
82 with Uintp; use Uintp;
83 with Urealp; use Urealp;
85 package body Sem_Ch3 is
87 -----------------------
88 -- Local Subprograms --
89 -----------------------
91 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
92 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
93 -- abstract interface types implemented by a record type or a derived
96 procedure Analyze_Object_Contract (Obj_Id : Entity_Id);
97 -- Analyze all delayed pragmas chained on the contract of object Obj_Id as
98 -- if they appeared at the end of the declarative region. The pragmas to be
106 procedure Build_Derived_Type
108 Parent_Type : Entity_Id;
109 Derived_Type : Entity_Id;
110 Is_Completion : Boolean;
111 Derive_Subps : Boolean := True);
112 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
113 -- the N_Full_Type_Declaration node containing the derived type definition.
114 -- Parent_Type is the entity for the parent type in the derived type
115 -- definition and Derived_Type the actual derived type. Is_Completion must
116 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
117 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
118 -- completion of a private type declaration. If Is_Completion is set to
119 -- True, N is the completion of a private type declaration and Derived_Type
120 -- is different from the defining identifier inside N (i.e. Derived_Type /=
121 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
122 -- subprograms should be derived. The only case where this parameter is
123 -- False is when Build_Derived_Type is recursively called to process an
124 -- implicit derived full type for a type derived from a private type (in
125 -- that case the subprograms must only be derived for the private view of
128 -- ??? These flags need a bit of re-examination and re-documentation:
129 -- ??? are they both necessary (both seem related to the recursion)?
131 procedure Build_Derived_Access_Type
133 Parent_Type : Entity_Id;
134 Derived_Type : Entity_Id);
135 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
136 -- create an implicit base if the parent type is constrained or if the
137 -- subtype indication has a constraint.
139 procedure Build_Derived_Array_Type
141 Parent_Type : Entity_Id;
142 Derived_Type : Entity_Id);
143 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
144 -- create an implicit base if the parent type is constrained or if the
145 -- subtype indication has a constraint.
147 procedure Build_Derived_Concurrent_Type
149 Parent_Type : Entity_Id;
150 Derived_Type : Entity_Id);
151 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
152 -- protected type, inherit entries and protected subprograms, check
153 -- legality of discriminant constraints if any.
155 procedure Build_Derived_Enumeration_Type
157 Parent_Type : Entity_Id;
158 Derived_Type : Entity_Id);
159 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
160 -- type, we must create a new list of literals. Types derived from
161 -- Character and [Wide_]Wide_Character are special-cased.
163 procedure Build_Derived_Numeric_Type
165 Parent_Type : Entity_Id;
166 Derived_Type : Entity_Id);
167 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
168 -- an anonymous base type, and propagate constraint to subtype if needed.
170 procedure Build_Derived_Private_Type
172 Parent_Type : Entity_Id;
173 Derived_Type : Entity_Id;
174 Is_Completion : Boolean;
175 Derive_Subps : Boolean := True);
176 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
177 -- because the parent may or may not have a completion, and the derivation
178 -- may itself be a completion.
180 procedure Build_Derived_Record_Type
182 Parent_Type : Entity_Id;
183 Derived_Type : Entity_Id;
184 Derive_Subps : Boolean := True);
185 -- Subsidiary procedure used for tagged and untagged record types
186 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
187 -- All parameters are as in Build_Derived_Type except that N, in
188 -- addition to being an N_Full_Type_Declaration node, can also be an
189 -- N_Private_Extension_Declaration node. See the definition of this routine
190 -- for much more info. Derive_Subps indicates whether subprograms should be
191 -- derived from the parent type. The only case where Derive_Subps is False
192 -- is for an implicit derived full type for a type derived from a private
193 -- type (see Build_Derived_Type).
195 procedure Build_Discriminal (Discrim : Entity_Id);
196 -- Create the discriminal corresponding to discriminant Discrim, that is
197 -- the parameter corresponding to Discrim to be used in initialization
198 -- procedures for the type where Discrim is a discriminant. Discriminals
199 -- are not used during semantic analysis, and are not fully defined
200 -- entities until expansion. Thus they are not given a scope until
201 -- initialization procedures are built.
203 function Build_Discriminant_Constraints
206 Derived_Def : Boolean := False) return Elist_Id;
207 -- Validate discriminant constraints and return the list of the constraints
208 -- in order of discriminant declarations, where T is the discriminated
209 -- unconstrained type. Def is the N_Subtype_Indication node where the
210 -- discriminants constraints for T are specified. Derived_Def is True
211 -- when building the discriminant constraints in a derived type definition
212 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
213 -- type and Def is the constraint "(xxx)" on T and this routine sets the
214 -- Corresponding_Discriminant field of the discriminants in the derived
215 -- type D to point to the corresponding discriminants in the parent type T.
217 procedure Build_Discriminated_Subtype
221 Related_Nod : Node_Id;
222 For_Access : Boolean := False);
223 -- Subsidiary procedure to Constrain_Discriminated_Type and to
224 -- Process_Incomplete_Dependents. Given
226 -- T (a possibly discriminated base type)
227 -- Def_Id (a very partially built subtype for T),
229 -- the call completes Def_Id to be the appropriate E_*_Subtype.
231 -- The Elist is the list of discriminant constraints if any (it is set
232 -- to No_Elist if T is not a discriminated type, and to an empty list if
233 -- T has discriminants but there are no discriminant constraints). The
234 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
235 -- The For_Access says whether or not this subtype is really constraining
236 -- an access type. That is its sole purpose is the designated type of an
237 -- access type -- in which case a Private_Subtype Is_For_Access_Subtype
238 -- is built to avoid freezing T when the access subtype is frozen.
240 function Build_Scalar_Bound
243 Der_T : Entity_Id) return Node_Id;
244 -- The bounds of a derived scalar type are conversions of the bounds of
245 -- the parent type. Optimize the representation if the bounds are literals.
246 -- Needs a more complete spec--what are the parameters exactly, and what
247 -- exactly is the returned value, and how is Bound affected???
249 procedure Build_Underlying_Full_View
253 -- If the completion of a private type is itself derived from a private
254 -- type, or if the full view of a private subtype is itself private, the
255 -- back-end has no way to compute the actual size of this type. We build
256 -- an internal subtype declaration of the proper parent type to convey
257 -- this information. This extra mechanism is needed because a full
258 -- view cannot itself have a full view (it would get clobbered during
261 procedure Check_Access_Discriminant_Requires_Limited
264 -- Check the restriction that the type to which an access discriminant
265 -- belongs must be a concurrent type or a descendant of a type with
266 -- the reserved word 'limited' in its declaration.
268 procedure Check_Anonymous_Access_Components
272 Comp_List : Node_Id);
273 -- Ada 2005 AI-382: an access component in a record definition can refer to
274 -- the enclosing record, in which case it denotes the type itself, and not
275 -- the current instance of the type. We create an anonymous access type for
276 -- the component, and flag it as an access to a component, so accessibility
277 -- checks are properly performed on it. The declaration of the access type
278 -- is placed ahead of that of the record to prevent order-of-elaboration
279 -- circularity issues in Gigi. We create an incomplete type for the record
280 -- declaration, which is the designated type of the anonymous access.
282 procedure Check_Delta_Expression (E : Node_Id);
283 -- Check that the expression represented by E is suitable for use as a
284 -- delta expression, i.e. it is of real type and is static.
286 procedure Check_Digits_Expression (E : Node_Id);
287 -- Check that the expression represented by E is suitable for use as a
288 -- digits expression, i.e. it is of integer type, positive and static.
290 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
291 -- Validate the initialization of an object declaration. T is the required
292 -- type, and Exp is the initialization expression.
294 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
295 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
297 procedure Check_Or_Process_Discriminants
300 Prev : Entity_Id := Empty);
301 -- If N is the full declaration of the completion T of an incomplete or
302 -- private type, check its discriminants (which are already known to be
303 -- conformant with those of the partial view, see Find_Type_Name),
304 -- otherwise process them. Prev is the entity of the partial declaration,
307 procedure Check_Real_Bound (Bound : Node_Id);
308 -- Check given bound for being of real type and static. If not, post an
309 -- appropriate message, and rewrite the bound with the real literal zero.
311 procedure Constant_Redeclaration
315 -- Various checks on legality of full declaration of deferred constant.
316 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
317 -- node. The caller has not yet set any attributes of this entity.
319 function Contain_Interface
321 Ifaces : Elist_Id) return Boolean;
322 -- Ada 2005: Determine whether Iface is present in the list Ifaces
324 procedure Convert_Scalar_Bounds
326 Parent_Type : Entity_Id;
327 Derived_Type : Entity_Id;
329 -- For derived scalar types, convert the bounds in the type definition to
330 -- the derived type, and complete their analysis. Given a constraint of the
331 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
332 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
333 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
334 -- subtype are conversions of those bounds to the derived_type, so that
335 -- their typing is consistent.
337 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
338 -- Copies attributes from array base type T2 to array base type T1. Copies
339 -- only attributes that apply to base types, but not subtypes.
341 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
342 -- Copies attributes from array subtype T2 to array subtype T1. Copies
343 -- attributes that apply to both subtypes and base types.
345 procedure Create_Constrained_Components
349 Constraints : Elist_Id);
350 -- Build the list of entities for a constrained discriminated record
351 -- subtype. If a component depends on a discriminant, replace its subtype
352 -- using the discriminant values in the discriminant constraint. Subt
353 -- is the defining identifier for the subtype whose list of constrained
354 -- entities we will create. Decl_Node is the type declaration node where
355 -- we will attach all the itypes created. Typ is the base discriminated
356 -- type for the subtype Subt. Constraints is the list of discriminant
357 -- constraints for Typ.
359 function Constrain_Component_Type
361 Constrained_Typ : Entity_Id;
362 Related_Node : Node_Id;
364 Constraints : Elist_Id) return Entity_Id;
365 -- Given a discriminated base type Typ, a list of discriminant constraints,
366 -- Constraints, for Typ and a component Comp of Typ, create and return the
367 -- type corresponding to Etype (Comp) where all discriminant references
368 -- are replaced with the corresponding constraint. If Etype (Comp) contains
369 -- no discriminant references then it is returned as-is. Constrained_Typ
370 -- is the final constrained subtype to which the constrained component
371 -- belongs. Related_Node is the node where we attach all created itypes.
373 procedure Constrain_Access
374 (Def_Id : in out Entity_Id;
376 Related_Nod : Node_Id);
377 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
378 -- an anonymous type created for a subtype indication. In that case it is
379 -- created in the procedure and attached to Related_Nod.
381 procedure Constrain_Array
382 (Def_Id : in out Entity_Id;
384 Related_Nod : Node_Id;
385 Related_Id : Entity_Id;
387 -- Apply a list of index constraints to an unconstrained array type. The
388 -- first parameter is the entity for the resulting subtype. A value of
389 -- Empty for Def_Id indicates that an implicit type must be created, but
390 -- creation is delayed (and must be done by this procedure) because other
391 -- subsidiary implicit types must be created first (which is why Def_Id
392 -- is an in/out parameter). The second parameter is a subtype indication
393 -- node for the constrained array to be created (e.g. something of the
394 -- form string (1 .. 10)). Related_Nod gives the place where this type
395 -- has to be inserted in the tree. The Related_Id and Suffix parameters
396 -- are used to build the associated Implicit type name.
398 procedure Constrain_Concurrent
399 (Def_Id : in out Entity_Id;
401 Related_Nod : Node_Id;
402 Related_Id : Entity_Id;
404 -- Apply list of discriminant constraints to an unconstrained concurrent
407 -- SI is the N_Subtype_Indication node containing the constraint and
408 -- the unconstrained type to constrain.
410 -- Def_Id is the entity for the resulting constrained subtype. A value
411 -- of Empty for Def_Id indicates that an implicit type must be created,
412 -- but creation is delayed (and must be done by this procedure) because
413 -- other subsidiary implicit types must be created first (which is why
414 -- Def_Id is an in/out parameter).
416 -- Related_Nod gives the place where this type has to be inserted
419 -- The last two arguments are used to create its external name if needed.
421 function Constrain_Corresponding_Record
422 (Prot_Subt : Entity_Id;
423 Corr_Rec : Entity_Id;
424 Related_Nod : Node_Id) return Entity_Id;
425 -- When constraining a protected type or task type with discriminants,
426 -- constrain the corresponding record with the same discriminant values.
428 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
429 -- Constrain a decimal fixed point type with a digits constraint and/or a
430 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
432 procedure Constrain_Discriminated_Type
435 Related_Nod : Node_Id;
436 For_Access : Boolean := False);
437 -- Process discriminant constraints of composite type. Verify that values
438 -- have been provided for all discriminants, that the original type is
439 -- unconstrained, and that the types of the supplied expressions match
440 -- the discriminant types. The first three parameters are like in routine
441 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
444 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
445 -- Constrain an enumeration type with a range constraint. This is identical
446 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
448 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
449 -- Constrain a floating point type with either a digits constraint
450 -- and/or a range constraint, building a E_Floating_Point_Subtype.
452 procedure Constrain_Index
455 Related_Nod : Node_Id;
456 Related_Id : Entity_Id;
459 -- Process an index constraint S in a constrained array declaration. The
460 -- constraint can be a subtype name, or a range with or without an explicit
461 -- subtype mark. The index is the corresponding index of the unconstrained
462 -- array. The Related_Id and Suffix parameters are used to build the
463 -- associated Implicit type name.
465 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
466 -- Build subtype of a signed or modular integer type
468 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
469 -- Constrain an ordinary fixed point type with a range constraint, and
470 -- build an E_Ordinary_Fixed_Point_Subtype entity.
472 procedure Copy_And_Swap (Priv, Full : Entity_Id);
473 -- Copy the Priv entity into the entity of its full declaration then swap
474 -- the two entities in such a manner that the former private type is now
475 -- seen as a full type.
477 procedure Decimal_Fixed_Point_Type_Declaration
480 -- Create a new decimal fixed point type, and apply the constraint to
481 -- obtain a subtype of this new type.
483 procedure Complete_Private_Subtype
486 Full_Base : Entity_Id;
487 Related_Nod : Node_Id);
488 -- Complete the implicit full view of a private subtype by setting the
489 -- appropriate semantic fields. If the full view of the parent is a record
490 -- type, build constrained components of subtype.
492 procedure Derive_Progenitor_Subprograms
493 (Parent_Type : Entity_Id;
494 Tagged_Type : Entity_Id);
495 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
496 -- operations of progenitors of Tagged_Type, and replace the subsidiary
497 -- subtypes with Tagged_Type, to build the specs of the inherited interface
498 -- primitives. The derived primitives are aliased to those of the
499 -- interface. This routine takes care also of transferring to the full view
500 -- subprograms associated with the partial view of Tagged_Type that cover
501 -- interface primitives.
503 procedure Derived_Standard_Character
505 Parent_Type : Entity_Id;
506 Derived_Type : Entity_Id);
507 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
508 -- derivations from types Standard.Character and Standard.Wide_Character.
510 procedure Derived_Type_Declaration
513 Is_Completion : Boolean);
514 -- Process a derived type declaration. Build_Derived_Type is invoked
515 -- to process the actual derived type definition. Parameters N and
516 -- Is_Completion have the same meaning as in Build_Derived_Type.
517 -- T is the N_Defining_Identifier for the entity defined in the
518 -- N_Full_Type_Declaration node N, that is T is the derived type.
520 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
521 -- Insert each literal in symbol table, as an overloadable identifier. Each
522 -- enumeration type is mapped into a sequence of integers, and each literal
523 -- is defined as a constant with integer value. If any of the literals are
524 -- character literals, the type is a character type, which means that
525 -- strings are legal aggregates for arrays of components of the type.
527 function Expand_To_Stored_Constraint
529 Constraint : Elist_Id) return Elist_Id;
530 -- Given a constraint (i.e. a list of expressions) on the discriminants of
531 -- Typ, expand it into a constraint on the stored discriminants and return
532 -- the new list of expressions constraining the stored discriminants.
534 function Find_Type_Of_Object
536 Related_Nod : Node_Id) return Entity_Id;
537 -- Get type entity for object referenced by Obj_Def, attaching the implicit
538 -- types generated to Related_Nod.
540 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
541 -- Create a new float and apply the constraint to obtain subtype of it
543 function Has_Range_Constraint (N : Node_Id) return Boolean;
544 -- Given an N_Subtype_Indication node N, return True if a range constraint
545 -- is present, either directly, or as part of a digits or delta constraint.
546 -- In addition, a digits constraint in the decimal case returns True, since
547 -- it establishes a default range if no explicit range is present.
549 function Inherit_Components
551 Parent_Base : Entity_Id;
552 Derived_Base : Entity_Id;
554 Inherit_Discr : Boolean;
555 Discs : Elist_Id) return Elist_Id;
556 -- Called from Build_Derived_Record_Type to inherit the components of
557 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
558 -- For more information on derived types and component inheritance please
559 -- consult the comment above the body of Build_Derived_Record_Type.
561 -- N is the original derived type declaration
563 -- Is_Tagged is set if we are dealing with tagged types
565 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
566 -- Parent_Base, otherwise no discriminants are inherited.
568 -- Discs gives the list of constraints that apply to Parent_Base in the
569 -- derived type declaration. If Discs is set to No_Elist, then we have
570 -- the following situation:
572 -- type Parent (D1..Dn : ..) is [tagged] record ...;
573 -- type Derived is new Parent [with ...];
575 -- which gets treated as
577 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
579 -- For untagged types the returned value is an association list. The list
580 -- starts from the association (Parent_Base => Derived_Base), and then it
581 -- contains a sequence of the associations of the form
583 -- (Old_Component => New_Component),
585 -- where Old_Component is the Entity_Id of a component in Parent_Base and
586 -- New_Component is the Entity_Id of the corresponding component in
587 -- Derived_Base. For untagged records, this association list is needed when
588 -- copying the record declaration for the derived base. In the tagged case
589 -- the value returned is irrelevant.
591 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
592 -- Propagate static and dynamic predicate flags from a parent to the
593 -- subtype in a subtype declaration with and without constraints.
595 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
596 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
597 -- Determine whether subprogram Subp is a procedure subject to pragma
598 -- Extensions_Visible with value False and has at least one controlling
599 -- parameter of mode OUT.
601 function Is_Valid_Constraint_Kind
603 Constraint_Kind : Node_Kind) return Boolean;
604 -- Returns True if it is legal to apply the given kind of constraint to the
605 -- given kind of type (index constraint to an array type, for example).
607 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
608 -- Create new modular type. Verify that modulus is in bounds
610 procedure New_Concatenation_Op (Typ : Entity_Id);
611 -- Create an abbreviated declaration for an operator in order to
612 -- materialize concatenation on array types.
614 procedure Ordinary_Fixed_Point_Type_Declaration
617 -- Create a new ordinary fixed point type, and apply the constraint to
618 -- obtain subtype of it.
620 procedure Prepare_Private_Subtype_Completion
622 Related_Nod : Node_Id);
623 -- Id is a subtype of some private type. Creates the full declaration
624 -- associated with Id whenever possible, i.e. when the full declaration
625 -- of the base type is already known. Records each subtype into
626 -- Private_Dependents of the base type.
628 procedure Process_Incomplete_Dependents
632 -- Process all entities that depend on an incomplete type. There include
633 -- subtypes, subprogram types that mention the incomplete type in their
634 -- profiles, and subprogram with access parameters that designate the
637 -- Inc_T is the defining identifier of an incomplete type declaration, its
638 -- Ekind is E_Incomplete_Type.
640 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
642 -- Full_T is N's defining identifier.
644 -- Subtypes of incomplete types with discriminants are completed when the
645 -- parent type is. This is simpler than private subtypes, because they can
646 -- only appear in the same scope, and there is no need to exchange views.
647 -- Similarly, access_to_subprogram types may have a parameter or a return
648 -- type that is an incomplete type, and that must be replaced with the
651 -- If the full type is tagged, subprogram with access parameters that
652 -- designated the incomplete may be primitive operations of the full type,
653 -- and have to be processed accordingly.
655 procedure Process_Real_Range_Specification (Def : Node_Id);
656 -- Given the type definition for a real type, this procedure processes and
657 -- checks the real range specification of this type definition if one is
658 -- present. If errors are found, error messages are posted, and the
659 -- Real_Range_Specification of Def is reset to Empty.
661 procedure Propagate_Default_Init_Cond_Attributes
662 (From_Typ : Entity_Id;
664 Parent_To_Derivation : Boolean := False;
665 Private_To_Full_View : Boolean := False);
666 -- Subsidiary to routines Build_Derived_Type and Process_Full_View. Inherit
667 -- all attributes related to pragma Default_Initial_Condition from From_Typ
668 -- to To_Typ. Flag Parent_To_Derivation should be set when the context is
669 -- the creation of a derived type. Flag Private_To_Full_View should be set
670 -- when processing both views of a private type.
672 procedure Record_Type_Declaration
676 -- Process a record type declaration (for both untagged and tagged
677 -- records). Parameters T and N are exactly like in procedure
678 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
679 -- for this routine. If this is the completion of an incomplete type
680 -- declaration, Prev is the entity of the incomplete declaration, used for
681 -- cross-referencing. Otherwise Prev = T.
683 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
684 -- This routine is used to process the actual record type definition (both
685 -- for untagged and tagged records). Def is a record type definition node.
686 -- This procedure analyzes the components in this record type definition.
687 -- Prev_T is the entity for the enclosing record type. It is provided so
688 -- that its Has_Task flag can be set if any of the component have Has_Task
689 -- set. If the declaration is the completion of an incomplete type
690 -- declaration, Prev_T is the original incomplete type, whose full view is
693 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
694 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
695 -- build a copy of the declaration tree of the parent, and we create
696 -- independently the list of components for the derived type. Semantic
697 -- information uses the component entities, but record representation
698 -- clauses are validated on the declaration tree. This procedure replaces
699 -- discriminants and components in the declaration with those that have
700 -- been created by Inherit_Components.
702 procedure Set_Fixed_Range
707 -- Build a range node with the given bounds and set it as the Scalar_Range
708 -- of the given fixed-point type entity. Loc is the source location used
709 -- for the constructed range. See body for further details.
711 procedure Set_Scalar_Range_For_Subtype
715 -- This routine is used to set the scalar range field for a subtype given
716 -- Def_Id, the entity for the subtype, and R, the range expression for the
717 -- scalar range. Subt provides the parent subtype to be used to analyze,
718 -- resolve, and check the given range.
720 procedure Set_Default_SSO (T : Entity_Id);
721 -- T is the entity for an array or record being declared. This procedure
722 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
723 -- to the setting of Opt.Default_SSO.
725 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
726 -- Create a new signed integer entity, and apply the constraint to obtain
727 -- the required first named subtype of this type.
729 procedure Set_Stored_Constraint_From_Discriminant_Constraint
731 -- E is some record type. This routine computes E's Stored_Constraint
732 -- from its Discriminant_Constraint.
734 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
735 -- Check that an entity in a list of progenitors is an interface,
736 -- emit error otherwise.
738 -----------------------
739 -- Access_Definition --
740 -----------------------
742 function Access_Definition
743 (Related_Nod : Node_Id;
744 N : Node_Id) return Entity_Id
746 Anon_Type : Entity_Id;
747 Anon_Scope : Entity_Id;
748 Desig_Type : Entity_Id;
749 Enclosing_Prot_Type : Entity_Id := Empty;
752 Check_SPARK_05_Restriction ("access type is not allowed", N);
754 if Is_Entry (Current_Scope)
755 and then Is_Task_Type (Etype (Scope (Current_Scope)))
757 Error_Msg_N ("task entries cannot have access parameters", N);
761 -- Ada 2005: For an object declaration the corresponding anonymous
762 -- type is declared in the current scope.
764 -- If the access definition is the return type of another access to
765 -- function, scope is the current one, because it is the one of the
766 -- current type declaration, except for the pathological case below.
768 if Nkind_In (Related_Nod, N_Object_Declaration,
769 N_Access_Function_Definition)
771 Anon_Scope := Current_Scope;
773 -- A pathological case: function returning access functions that
774 -- return access functions, etc. Each anonymous access type created
775 -- is in the enclosing scope of the outermost function.
782 while Nkind_In (Par, N_Access_Function_Definition,
788 if Nkind (Par) = N_Function_Specification then
789 Anon_Scope := Scope (Defining_Entity (Par));
793 -- For the anonymous function result case, retrieve the scope of the
794 -- function specification's associated entity rather than using the
795 -- current scope. The current scope will be the function itself if the
796 -- formal part is currently being analyzed, but will be the parent scope
797 -- in the case of a parameterless function, and we always want to use
798 -- the function's parent scope. Finally, if the function is a child
799 -- unit, we must traverse the tree to retrieve the proper entity.
801 elsif Nkind (Related_Nod) = N_Function_Specification
802 and then Nkind (Parent (N)) /= N_Parameter_Specification
804 -- If the current scope is a protected type, the anonymous access
805 -- is associated with one of the protected operations, and must
806 -- be available in the scope that encloses the protected declaration.
807 -- Otherwise the type is in the scope enclosing the subprogram.
809 -- If the function has formals, The return type of a subprogram
810 -- declaration is analyzed in the scope of the subprogram (see
811 -- Process_Formals) and thus the protected type, if present, is
812 -- the scope of the current function scope.
814 if Ekind (Current_Scope) = E_Protected_Type then
815 Enclosing_Prot_Type := Current_Scope;
817 elsif Ekind (Current_Scope) = E_Function
818 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
820 Enclosing_Prot_Type := Scope (Current_Scope);
823 if Present (Enclosing_Prot_Type) then
824 Anon_Scope := Scope (Enclosing_Prot_Type);
827 Anon_Scope := Scope (Defining_Entity (Related_Nod));
830 -- For an access type definition, if the current scope is a child
831 -- unit it is the scope of the type.
833 elsif Is_Compilation_Unit (Current_Scope) then
834 Anon_Scope := Current_Scope;
836 -- For access formals, access components, and access discriminants, the
837 -- scope is that of the enclosing declaration,
840 Anon_Scope := Scope (Current_Scope);
845 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
848 and then Ada_Version >= Ada_2005
850 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
853 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
854 -- the corresponding semantic routine
856 if Present (Access_To_Subprogram_Definition (N)) then
858 -- Compiler runtime units are compiled in Ada 2005 mode when building
859 -- the runtime library but must also be compilable in Ada 95 mode
860 -- (when bootstrapping the compiler).
862 Check_Compiler_Unit ("anonymous access to subprogram", N);
864 Access_Subprogram_Declaration
865 (T_Name => Anon_Type,
866 T_Def => Access_To_Subprogram_Definition (N));
868 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
870 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
872 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
875 Set_Can_Use_Internal_Rep
876 (Anon_Type, not Always_Compatible_Rep_On_Target);
878 -- If the anonymous access is associated with a protected operation,
879 -- create a reference to it after the enclosing protected definition
880 -- because the itype will be used in the subsequent bodies.
882 -- If the anonymous access itself is protected, a full type
883 -- declaratiton will be created for it, so that the equivalent
884 -- record type can be constructed. For further details, see
885 -- Replace_Anonymous_Access_To_Protected-Subprogram.
887 if Ekind (Current_Scope) = E_Protected_Type
888 and then not Protected_Present (Access_To_Subprogram_Definition (N))
890 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
896 Find_Type (Subtype_Mark (N));
897 Desig_Type := Entity (Subtype_Mark (N));
899 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
900 Set_Etype (Anon_Type, Anon_Type);
902 -- Make sure the anonymous access type has size and alignment fields
903 -- set, as required by gigi. This is necessary in the case of the
904 -- Task_Body_Procedure.
906 if not Has_Private_Component (Desig_Type) then
907 Layout_Type (Anon_Type);
910 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
911 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
912 -- the null value is allowed. In Ada 95 the null value is never allowed.
914 if Ada_Version >= Ada_2005 then
915 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
917 Set_Can_Never_Be_Null (Anon_Type, True);
920 -- The anonymous access type is as public as the discriminated type or
921 -- subprogram that defines it. It is imported (for back-end purposes)
922 -- if the designated type is.
924 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
926 -- Ada 2005 (AI-231): Propagate the access-constant attribute
928 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
930 -- The context is either a subprogram declaration, object declaration,
931 -- or an access discriminant, in a private or a full type declaration.
932 -- In the case of a subprogram, if the designated type is incomplete,
933 -- the operation will be a primitive operation of the full type, to be
934 -- updated subsequently. If the type is imported through a limited_with
935 -- clause, the subprogram is not a primitive operation of the type
936 -- (which is declared elsewhere in some other scope).
938 if Ekind (Desig_Type) = E_Incomplete_Type
939 and then not From_Limited_With (Desig_Type)
940 and then Is_Overloadable (Current_Scope)
942 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
943 Set_Has_Delayed_Freeze (Current_Scope);
946 -- Ada 2005: If the designated type is an interface that may contain
947 -- tasks, create a Master entity for the declaration. This must be done
948 -- before expansion of the full declaration, because the declaration may
949 -- include an expression that is an allocator, whose expansion needs the
950 -- proper Master for the created tasks.
952 if Nkind (Related_Nod) = N_Object_Declaration and then Expander_Active
954 if Is_Interface (Desig_Type) and then Is_Limited_Record (Desig_Type)
956 Build_Class_Wide_Master (Anon_Type);
958 -- Similarly, if the type is an anonymous access that designates
959 -- tasks, create a master entity for it in the current context.
961 elsif Has_Task (Desig_Type) and then Comes_From_Source (Related_Nod)
963 Build_Master_Entity (Defining_Identifier (Related_Nod));
964 Build_Master_Renaming (Anon_Type);
968 -- For a private component of a protected type, it is imperative that
969 -- the back-end elaborate the type immediately after the protected
970 -- declaration, because this type will be used in the declarations
971 -- created for the component within each protected body, so we must
972 -- create an itype reference for it now.
974 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
975 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
977 -- Similarly, if the access definition is the return result of a
978 -- function, create an itype reference for it because it will be used
979 -- within the function body. For a regular function that is not a
980 -- compilation unit, insert reference after the declaration. For a
981 -- protected operation, insert it after the enclosing protected type
982 -- declaration. In either case, do not create a reference for a type
983 -- obtained through a limited_with clause, because this would introduce
984 -- semantic dependencies.
986 -- Similarly, do not create a reference if the designated type is a
987 -- generic formal, because no use of it will reach the backend.
989 elsif Nkind (Related_Nod) = N_Function_Specification
990 and then not From_Limited_With (Desig_Type)
991 and then not Is_Generic_Type (Desig_Type)
993 if Present (Enclosing_Prot_Type) then
994 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
996 elsif Is_List_Member (Parent (Related_Nod))
997 and then Nkind (Parent (N)) /= N_Parameter_Specification
999 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
1002 -- Finally, create an itype reference for an object declaration of an
1003 -- anonymous access type. This is strictly necessary only for deferred
1004 -- constants, but in any case will avoid out-of-scope problems in the
1007 elsif Nkind (Related_Nod) = N_Object_Declaration then
1008 Build_Itype_Reference (Anon_Type, Related_Nod);
1012 end Access_Definition;
1014 -----------------------------------
1015 -- Access_Subprogram_Declaration --
1016 -----------------------------------
1018 procedure Access_Subprogram_Declaration
1019 (T_Name : Entity_Id;
1022 procedure Check_For_Premature_Usage (Def : Node_Id);
1023 -- Check that type T_Name is not used, directly or recursively, as a
1024 -- parameter or a return type in Def. Def is either a subtype, an
1025 -- access_definition, or an access_to_subprogram_definition.
1027 -------------------------------
1028 -- Check_For_Premature_Usage --
1029 -------------------------------
1031 procedure Check_For_Premature_Usage (Def : Node_Id) is
1035 -- Check for a subtype mark
1037 if Nkind (Def) in N_Has_Etype then
1038 if Etype (Def) = T_Name then
1040 ("type& cannot be used before end of its declaration", Def);
1043 -- If this is not a subtype, then this is an access_definition
1045 elsif Nkind (Def) = N_Access_Definition then
1046 if Present (Access_To_Subprogram_Definition (Def)) then
1047 Check_For_Premature_Usage
1048 (Access_To_Subprogram_Definition (Def));
1050 Check_For_Premature_Usage (Subtype_Mark (Def));
1053 -- The only cases left are N_Access_Function_Definition and
1054 -- N_Access_Procedure_Definition.
1057 if Present (Parameter_Specifications (Def)) then
1058 Param := First (Parameter_Specifications (Def));
1059 while Present (Param) loop
1060 Check_For_Premature_Usage (Parameter_Type (Param));
1061 Param := Next (Param);
1065 if Nkind (Def) = N_Access_Function_Definition then
1066 Check_For_Premature_Usage (Result_Definition (Def));
1069 end Check_For_Premature_Usage;
1073 Formals : constant List_Id := Parameter_Specifications (T_Def);
1076 Desig_Type : constant Entity_Id :=
1077 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1079 -- Start of processing for Access_Subprogram_Declaration
1082 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1084 -- Associate the Itype node with the inner full-type declaration or
1085 -- subprogram spec or entry body. This is required to handle nested
1086 -- anonymous declarations. For example:
1089 -- (X : access procedure
1090 -- (Y : access procedure
1093 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1094 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1095 N_Private_Type_Declaration,
1096 N_Private_Extension_Declaration,
1097 N_Procedure_Specification,
1098 N_Function_Specification,
1102 Nkind_In (D_Ityp, N_Object_Declaration,
1103 N_Object_Renaming_Declaration,
1104 N_Formal_Object_Declaration,
1105 N_Formal_Type_Declaration,
1106 N_Task_Type_Declaration,
1107 N_Protected_Type_Declaration))
1109 D_Ityp := Parent (D_Ityp);
1110 pragma Assert (D_Ityp /= Empty);
1113 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1115 if Nkind_In (D_Ityp, N_Procedure_Specification,
1116 N_Function_Specification)
1118 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1120 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1121 N_Object_Declaration,
1122 N_Object_Renaming_Declaration,
1123 N_Formal_Type_Declaration)
1125 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1128 if Nkind (T_Def) = N_Access_Function_Definition then
1129 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1131 Acc : constant Node_Id := Result_Definition (T_Def);
1134 if Present (Access_To_Subprogram_Definition (Acc))
1136 Protected_Present (Access_To_Subprogram_Definition (Acc))
1140 Replace_Anonymous_Access_To_Protected_Subprogram
1146 Access_Definition (T_Def, Result_Definition (T_Def)));
1151 Analyze (Result_Definition (T_Def));
1154 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1157 -- If a null exclusion is imposed on the result type, then
1158 -- create a null-excluding itype (an access subtype) and use
1159 -- it as the function's Etype.
1161 if Is_Access_Type (Typ)
1162 and then Null_Exclusion_In_Return_Present (T_Def)
1164 Set_Etype (Desig_Type,
1165 Create_Null_Excluding_Itype
1167 Related_Nod => T_Def,
1168 Scope_Id => Current_Scope));
1171 if From_Limited_With (Typ) then
1173 -- AI05-151: Incomplete types are allowed in all basic
1174 -- declarations, including access to subprograms.
1176 if Ada_Version >= Ada_2012 then
1181 ("illegal use of incomplete type&",
1182 Result_Definition (T_Def), Typ);
1185 elsif Ekind (Current_Scope) = E_Package
1186 and then In_Private_Part (Current_Scope)
1188 if Ekind (Typ) = E_Incomplete_Type then
1189 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1191 elsif Is_Class_Wide_Type (Typ)
1192 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1195 (Desig_Type, Private_Dependents (Etype (Typ)));
1199 Set_Etype (Desig_Type, Typ);
1204 if not (Is_Type (Etype (Desig_Type))) then
1206 ("expect type in function specification",
1207 Result_Definition (T_Def));
1211 Set_Etype (Desig_Type, Standard_Void_Type);
1214 if Present (Formals) then
1215 Push_Scope (Desig_Type);
1217 -- Some special tests here. These special tests can be removed
1218 -- if and when Itypes always have proper parent pointers to their
1221 -- Special test 1) Link defining_identifier of formals. Required by
1222 -- First_Formal to provide its functionality.
1228 F := First (Formals);
1230 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1231 -- when it is part of an unconstrained type and subtype expansion
1232 -- is disabled. To avoid back-end problems with shared profiles,
1233 -- use previous subprogram type as the designated type, and then
1234 -- remove scope added above.
1236 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1238 Set_Etype (T_Name, T_Name);
1239 Init_Size_Align (T_Name);
1240 Set_Directly_Designated_Type (T_Name,
1241 Scope (Defining_Identifier (F)));
1246 while Present (F) loop
1247 if No (Parent (Defining_Identifier (F))) then
1248 Set_Parent (Defining_Identifier (F), F);
1255 Process_Formals (Formals, Parent (T_Def));
1257 -- Special test 2) End_Scope requires that the parent pointer be set
1258 -- to something reasonable, but Itypes don't have parent pointers. So
1259 -- we set it and then unset it ???
1261 Set_Parent (Desig_Type, T_Name);
1263 Set_Parent (Desig_Type, Empty);
1266 -- Check for premature usage of the type being defined
1268 Check_For_Premature_Usage (T_Def);
1270 -- The return type and/or any parameter type may be incomplete. Mark the
1271 -- subprogram_type as depending on the incomplete type, so that it can
1272 -- be updated when the full type declaration is seen. This only applies
1273 -- to incomplete types declared in some enclosing scope, not to limited
1274 -- views from other packages.
1276 -- Prior to Ada 2012, access to functions can only have in_parameters.
1278 if Present (Formals) then
1279 Formal := First_Formal (Desig_Type);
1280 while Present (Formal) loop
1281 if Ekind (Formal) /= E_In_Parameter
1282 and then Nkind (T_Def) = N_Access_Function_Definition
1283 and then Ada_Version < Ada_2012
1285 Error_Msg_N ("functions can only have IN parameters", Formal);
1288 if Ekind (Etype (Formal)) = E_Incomplete_Type
1289 and then In_Open_Scopes (Scope (Etype (Formal)))
1291 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1292 Set_Has_Delayed_Freeze (Desig_Type);
1295 Next_Formal (Formal);
1299 -- Check whether an indirect call without actuals may be possible. This
1300 -- is used when resolving calls whose result is then indexed.
1302 May_Need_Actuals (Desig_Type);
1304 -- If the return type is incomplete, this is legal as long as the type
1305 -- is declared in the current scope and will be completed in it (rather
1306 -- than being part of limited view).
1308 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1309 and then not Has_Delayed_Freeze (Desig_Type)
1310 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1312 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1313 Set_Has_Delayed_Freeze (Desig_Type);
1316 Check_Delayed_Subprogram (Desig_Type);
1318 if Protected_Present (T_Def) then
1319 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1320 Set_Convention (Desig_Type, Convention_Protected);
1322 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1325 Set_Can_Use_Internal_Rep (T_Name, not Always_Compatible_Rep_On_Target);
1327 Set_Etype (T_Name, T_Name);
1328 Init_Size_Align (T_Name);
1329 Set_Directly_Designated_Type (T_Name, Desig_Type);
1331 Generate_Reference_To_Formals (T_Name);
1333 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1335 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1337 Check_Restriction (No_Access_Subprograms, T_Def);
1338 end Access_Subprogram_Declaration;
1340 ----------------------------
1341 -- Access_Type_Declaration --
1342 ----------------------------
1344 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1345 P : constant Node_Id := Parent (Def);
1346 S : constant Node_Id := Subtype_Indication (Def);
1348 Full_Desig : Entity_Id;
1351 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1353 -- Check for permissible use of incomplete type
1355 if Nkind (S) /= N_Subtype_Indication then
1358 if Ekind (Root_Type (Entity (S))) = E_Incomplete_Type then
1359 Set_Directly_Designated_Type (T, Entity (S));
1361 -- If the designated type is a limited view, we cannot tell if
1362 -- the full view contains tasks, and there is no way to handle
1363 -- that full view in a client. We create a master entity for the
1364 -- scope, which will be used when a client determines that one
1367 if From_Limited_With (Entity (S))
1368 and then not Is_Class_Wide_Type (Entity (S))
1370 Set_Ekind (T, E_Access_Type);
1371 Build_Master_Entity (T);
1372 Build_Master_Renaming (T);
1376 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1379 -- If the access definition is of the form: ACCESS NOT NULL ..
1380 -- the subtype indication must be of an access type. Create
1381 -- a null-excluding subtype of it.
1383 if Null_Excluding_Subtype (Def) then
1384 if not Is_Access_Type (Entity (S)) then
1385 Error_Msg_N ("null exclusion must apply to access type", Def);
1389 Loc : constant Source_Ptr := Sloc (S);
1391 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1395 Make_Subtype_Declaration (Loc,
1396 Defining_Identifier => Nam,
1397 Subtype_Indication =>
1398 New_Occurrence_Of (Entity (S), Loc));
1399 Set_Null_Exclusion_Present (Decl);
1400 Insert_Before (Parent (Def), Decl);
1402 Set_Entity (S, Nam);
1408 Set_Directly_Designated_Type (T,
1409 Process_Subtype (S, P, T, 'P'));
1412 if All_Present (Def) or Constant_Present (Def) then
1413 Set_Ekind (T, E_General_Access_Type);
1415 Set_Ekind (T, E_Access_Type);
1418 Full_Desig := Designated_Type (T);
1420 if Base_Type (Full_Desig) = T then
1421 Error_Msg_N ("access type cannot designate itself", S);
1423 -- In Ada 2005, the type may have a limited view through some unit in
1424 -- its own context, allowing the following circularity that cannot be
1425 -- detected earlier.
1427 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1430 ("access type cannot designate its own classwide type", S);
1432 -- Clean up indication of tagged status to prevent cascaded errors
1434 Set_Is_Tagged_Type (T, False);
1439 -- If the type has appeared already in a with_type clause, it is frozen
1440 -- and the pointer size is already set. Else, initialize.
1442 if not From_Limited_With (T) then
1443 Init_Size_Align (T);
1446 -- Note that Has_Task is always false, since the access type itself
1447 -- is not a task type. See Einfo for more description on this point.
1448 -- Exactly the same consideration applies to Has_Controlled_Component
1449 -- and to Has_Protected.
1451 Set_Has_Task (T, False);
1452 Set_Has_Controlled_Component (T, False);
1453 Set_Has_Protected (T, False);
1455 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1456 -- problems where an incomplete view of this entity has been previously
1457 -- established by a limited with and an overlaid version of this field
1458 -- (Stored_Constraint) was initialized for the incomplete view.
1460 -- This reset is performed in most cases except where the access type
1461 -- has been created for the purposes of allocating or deallocating a
1462 -- build-in-place object. Such access types have explicitly set pools
1463 -- and finalization masters.
1465 if No (Associated_Storage_Pool (T)) then
1466 Set_Finalization_Master (T, Empty);
1469 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1472 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1473 Set_Is_Access_Constant (T, Constant_Present (Def));
1474 end Access_Type_Declaration;
1476 ----------------------------------
1477 -- Add_Interface_Tag_Components --
1478 ----------------------------------
1480 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1481 Loc : constant Source_Ptr := Sloc (N);
1485 procedure Add_Tag (Iface : Entity_Id);
1486 -- Add tag for one of the progenitor interfaces
1492 procedure Add_Tag (Iface : Entity_Id) is
1499 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1501 -- This is a reasonable place to propagate predicates
1503 if Has_Predicates (Iface) then
1504 Set_Has_Predicates (Typ);
1508 Make_Component_Definition (Loc,
1509 Aliased_Present => True,
1510 Subtype_Indication =>
1511 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1513 Tag := Make_Temporary (Loc, 'V');
1516 Make_Component_Declaration (Loc,
1517 Defining_Identifier => Tag,
1518 Component_Definition => Def);
1520 Analyze_Component_Declaration (Decl);
1522 Set_Analyzed (Decl);
1523 Set_Ekind (Tag, E_Component);
1525 Set_Is_Aliased (Tag);
1526 Set_Related_Type (Tag, Iface);
1527 Init_Component_Location (Tag);
1529 pragma Assert (Is_Frozen (Iface));
1531 Set_DT_Entry_Count (Tag,
1532 DT_Entry_Count (First_Entity (Iface)));
1534 if No (Last_Tag) then
1537 Insert_After (Last_Tag, Decl);
1542 -- If the ancestor has discriminants we need to give special support
1543 -- to store the offset_to_top value of the secondary dispatch tables.
1544 -- For this purpose we add a supplementary component just after the
1545 -- field that contains the tag associated with each secondary DT.
1547 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1549 Make_Component_Definition (Loc,
1550 Subtype_Indication =>
1551 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1553 Offset := Make_Temporary (Loc, 'V');
1556 Make_Component_Declaration (Loc,
1557 Defining_Identifier => Offset,
1558 Component_Definition => Def);
1560 Analyze_Component_Declaration (Decl);
1562 Set_Analyzed (Decl);
1563 Set_Ekind (Offset, E_Component);
1564 Set_Is_Aliased (Offset);
1565 Set_Related_Type (Offset, Iface);
1566 Init_Component_Location (Offset);
1567 Insert_After (Last_Tag, Decl);
1578 -- Start of processing for Add_Interface_Tag_Components
1581 if not RTE_Available (RE_Interface_Tag) then
1583 ("(Ada 2005) interface types not supported by this run-time!",
1588 if Ekind (Typ) /= E_Record_Type
1589 or else (Is_Concurrent_Record_Type (Typ)
1590 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1591 or else (not Is_Concurrent_Record_Type (Typ)
1592 and then No (Interfaces (Typ))
1593 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1598 -- Find the current last tag
1600 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1601 Ext := Record_Extension_Part (Type_Definition (N));
1603 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1604 Ext := Type_Definition (N);
1609 if not (Present (Component_List (Ext))) then
1610 Set_Null_Present (Ext, False);
1612 Set_Component_List (Ext,
1613 Make_Component_List (Loc,
1614 Component_Items => L,
1615 Null_Present => False));
1617 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1618 L := Component_Items
1620 (Record_Extension_Part
1621 (Type_Definition (N))));
1623 L := Component_Items
1625 (Type_Definition (N)));
1628 -- Find the last tag component
1631 while Present (Comp) loop
1632 if Nkind (Comp) = N_Component_Declaration
1633 and then Is_Tag (Defining_Identifier (Comp))
1642 -- At this point L references the list of components and Last_Tag
1643 -- references the current last tag (if any). Now we add the tag
1644 -- corresponding with all the interfaces that are not implemented
1647 if Present (Interfaces (Typ)) then
1648 Elmt := First_Elmt (Interfaces (Typ));
1649 while Present (Elmt) loop
1650 Add_Tag (Node (Elmt));
1654 end Add_Interface_Tag_Components;
1656 -------------------------------------
1657 -- Add_Internal_Interface_Entities --
1658 -------------------------------------
1660 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1663 Iface_Elmt : Elmt_Id;
1664 Iface_Prim : Entity_Id;
1665 Ifaces_List : Elist_Id;
1666 New_Subp : Entity_Id := Empty;
1668 Restore_Scope : Boolean := False;
1671 pragma Assert (Ada_Version >= Ada_2005
1672 and then Is_Record_Type (Tagged_Type)
1673 and then Is_Tagged_Type (Tagged_Type)
1674 and then Has_Interfaces (Tagged_Type)
1675 and then not Is_Interface (Tagged_Type));
1677 -- Ensure that the internal entities are added to the scope of the type
1679 if Scope (Tagged_Type) /= Current_Scope then
1680 Push_Scope (Scope (Tagged_Type));
1681 Restore_Scope := True;
1684 Collect_Interfaces (Tagged_Type, Ifaces_List);
1686 Iface_Elmt := First_Elmt (Ifaces_List);
1687 while Present (Iface_Elmt) loop
1688 Iface := Node (Iface_Elmt);
1690 -- Originally we excluded here from this processing interfaces that
1691 -- are parents of Tagged_Type because their primitives are located
1692 -- in the primary dispatch table (and hence no auxiliary internal
1693 -- entities are required to handle secondary dispatch tables in such
1694 -- case). However, these auxiliary entities are also required to
1695 -- handle derivations of interfaces in formals of generics (see
1696 -- Derive_Subprograms).
1698 Elmt := First_Elmt (Primitive_Operations (Iface));
1699 while Present (Elmt) loop
1700 Iface_Prim := Node (Elmt);
1702 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1704 Find_Primitive_Covering_Interface
1705 (Tagged_Type => Tagged_Type,
1706 Iface_Prim => Iface_Prim);
1708 if No (Prim) and then Serious_Errors_Detected > 0 then
1712 pragma Assert (Present (Prim));
1714 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1715 -- differs from the name of the interface primitive then it is
1716 -- a private primitive inherited from a parent type. In such
1717 -- case, given that Tagged_Type covers the interface, the
1718 -- inherited private primitive becomes visible. For such
1719 -- purpose we add a new entity that renames the inherited
1720 -- private primitive.
1722 if Chars (Prim) /= Chars (Iface_Prim) then
1723 pragma Assert (Has_Suffix (Prim, 'P'));
1725 (New_Subp => New_Subp,
1726 Parent_Subp => Iface_Prim,
1727 Derived_Type => Tagged_Type,
1728 Parent_Type => Iface);
1729 Set_Alias (New_Subp, Prim);
1730 Set_Is_Abstract_Subprogram
1731 (New_Subp, Is_Abstract_Subprogram (Prim));
1735 (New_Subp => New_Subp,
1736 Parent_Subp => Iface_Prim,
1737 Derived_Type => Tagged_Type,
1738 Parent_Type => Iface);
1740 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1741 -- associated with interface types. These entities are
1742 -- only registered in the list of primitives of its
1743 -- corresponding tagged type because they are only used
1744 -- to fill the contents of the secondary dispatch tables.
1745 -- Therefore they are removed from the homonym chains.
1747 Set_Is_Hidden (New_Subp);
1748 Set_Is_Internal (New_Subp);
1749 Set_Alias (New_Subp, Prim);
1750 Set_Is_Abstract_Subprogram
1751 (New_Subp, Is_Abstract_Subprogram (Prim));
1752 Set_Interface_Alias (New_Subp, Iface_Prim);
1754 -- If the returned type is an interface then propagate it to
1755 -- the returned type. Needed by the thunk to generate the code
1756 -- which displaces "this" to reference the corresponding
1757 -- secondary dispatch table in the returned object.
1759 if Is_Interface (Etype (Iface_Prim)) then
1760 Set_Etype (New_Subp, Etype (Iface_Prim));
1763 -- Internal entities associated with interface types are only
1764 -- registered in the list of primitives of the tagged type.
1765 -- They are only used to fill the contents of the secondary
1766 -- dispatch tables. Therefore they are not needed in the
1769 Remove_Homonym (New_Subp);
1771 -- Hidden entities associated with interfaces must have set
1772 -- the Has_Delay_Freeze attribute to ensure that, in case
1773 -- of locally defined tagged types (or compiling with static
1774 -- dispatch tables generation disabled) the corresponding
1775 -- entry of the secondary dispatch table is filled when such
1776 -- an entity is frozen. This is an expansion activity that must
1777 -- be suppressed for ASIS because it leads to gigi elaboration
1778 -- issues in annotate mode.
1780 if not ASIS_Mode then
1781 Set_Has_Delayed_Freeze (New_Subp);
1789 Next_Elmt (Iface_Elmt);
1792 if Restore_Scope then
1795 end Add_Internal_Interface_Entities;
1797 -----------------------------------
1798 -- Analyze_Component_Declaration --
1799 -----------------------------------
1801 procedure Analyze_Component_Declaration (N : Node_Id) is
1802 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1803 Id : constant Entity_Id := Defining_Identifier (N);
1804 E : constant Node_Id := Expression (N);
1805 Typ : constant Node_Id :=
1806 Subtype_Indication (Component_Definition (N));
1810 function Contains_POC (Constr : Node_Id) return Boolean;
1811 -- Determines whether a constraint uses the discriminant of a record
1812 -- type thus becoming a per-object constraint (POC).
1814 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1815 -- Typ is the type of the current component, check whether this type is
1816 -- a limited type. Used to validate declaration against that of
1817 -- enclosing record.
1823 function Contains_POC (Constr : Node_Id) return Boolean is
1825 -- Prevent cascaded errors
1827 if Error_Posted (Constr) then
1831 case Nkind (Constr) is
1832 when N_Attribute_Reference =>
1833 return Attribute_Name (Constr) = Name_Access
1834 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1836 when N_Discriminant_Association =>
1837 return Denotes_Discriminant (Expression (Constr));
1839 when N_Identifier =>
1840 return Denotes_Discriminant (Constr);
1842 when N_Index_Or_Discriminant_Constraint =>
1847 IDC := First (Constraints (Constr));
1848 while Present (IDC) loop
1850 -- One per-object constraint is sufficient
1852 if Contains_POC (IDC) then
1863 return Denotes_Discriminant (Low_Bound (Constr))
1865 Denotes_Discriminant (High_Bound (Constr));
1867 when N_Range_Constraint =>
1868 return Denotes_Discriminant (Range_Expression (Constr));
1876 ----------------------
1877 -- Is_Known_Limited --
1878 ----------------------
1880 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1881 P : constant Entity_Id := Etype (Typ);
1882 R : constant Entity_Id := Root_Type (Typ);
1885 if Is_Limited_Record (Typ) then
1888 -- If the root type is limited (and not a limited interface)
1889 -- so is the current type
1891 elsif Is_Limited_Record (R)
1892 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1896 -- Else the type may have a limited interface progenitor, but a
1897 -- limited record parent.
1899 elsif R /= P and then Is_Limited_Record (P) then
1905 end Is_Known_Limited;
1907 -- Start of processing for Analyze_Component_Declaration
1910 Generate_Definition (Id);
1913 if Present (Typ) then
1914 T := Find_Type_Of_Object
1915 (Subtype_Indication (Component_Definition (N)), N);
1917 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1918 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1921 -- Ada 2005 (AI-230): Access Definition case
1924 pragma Assert (Present
1925 (Access_Definition (Component_Definition (N))));
1927 T := Access_Definition
1929 N => Access_Definition (Component_Definition (N)));
1930 Set_Is_Local_Anonymous_Access (T);
1932 -- Ada 2005 (AI-254)
1934 if Present (Access_To_Subprogram_Definition
1935 (Access_Definition (Component_Definition (N))))
1936 and then Protected_Present (Access_To_Subprogram_Definition
1938 (Component_Definition (N))))
1940 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1944 -- If the subtype is a constrained subtype of the enclosing record,
1945 -- (which must have a partial view) the back-end does not properly
1946 -- handle the recursion. Rewrite the component declaration with an
1947 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1948 -- the tree directly because side effects have already been removed from
1949 -- discriminant constraints.
1951 if Ekind (T) = E_Access_Subtype
1952 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1953 and then Comes_From_Source (T)
1954 and then Nkind (Parent (T)) = N_Subtype_Declaration
1955 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1958 (Subtype_Indication (Component_Definition (N)),
1959 New_Copy_Tree (Subtype_Indication (Parent (T))));
1960 T := Find_Type_Of_Object
1961 (Subtype_Indication (Component_Definition (N)), N);
1964 -- If the component declaration includes a default expression, then we
1965 -- check that the component is not of a limited type (RM 3.7(5)),
1966 -- and do the special preanalysis of the expression (see section on
1967 -- "Handling of Default and Per-Object Expressions" in the spec of
1971 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1972 Preanalyze_Default_Expression (E, T);
1973 Check_Initialization (T, E);
1975 if Ada_Version >= Ada_2005
1976 and then Ekind (T) = E_Anonymous_Access_Type
1977 and then Etype (E) /= Any_Type
1979 -- Check RM 3.9.2(9): "if the expected type for an expression is
1980 -- an anonymous access-to-specific tagged type, then the object
1981 -- designated by the expression shall not be dynamically tagged
1982 -- unless it is a controlling operand in a call on a dispatching
1985 if Is_Tagged_Type (Directly_Designated_Type (T))
1987 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
1989 Ekind (Directly_Designated_Type (Etype (E))) =
1993 ("access to specific tagged type required (RM 3.9.2(9))", E);
1996 -- (Ada 2005: AI-230): Accessibility check for anonymous
1999 if Type_Access_Level (Etype (E)) >
2000 Deepest_Type_Access_Level (T)
2003 ("expression has deeper access level than component " &
2004 "(RM 3.10.2 (12.2))", E);
2007 -- The initialization expression is a reference to an access
2008 -- discriminant. The type of the discriminant is always deeper
2009 -- than any access type.
2011 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2012 and then Is_Entity_Name (E)
2013 and then Ekind (Entity (E)) = E_In_Parameter
2014 and then Present (Discriminal_Link (Entity (E)))
2017 ("discriminant has deeper accessibility level than target",
2023 -- The parent type may be a private view with unknown discriminants,
2024 -- and thus unconstrained. Regular components must be constrained.
2026 if Is_Indefinite_Subtype (T) and then Chars (Id) /= Name_uParent then
2027 if Is_Class_Wide_Type (T) then
2029 ("class-wide subtype with unknown discriminants" &
2030 " in component declaration",
2031 Subtype_Indication (Component_Definition (N)));
2034 ("unconstrained subtype in component declaration",
2035 Subtype_Indication (Component_Definition (N)));
2038 -- Components cannot be abstract, except for the special case of
2039 -- the _Parent field (case of extending an abstract tagged type)
2041 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2042 Error_Msg_N ("type of a component cannot be abstract", N);
2046 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2048 -- The component declaration may have a per-object constraint, set
2049 -- the appropriate flag in the defining identifier of the subtype.
2051 if Present (Subtype_Indication (Component_Definition (N))) then
2053 Sindic : constant Node_Id :=
2054 Subtype_Indication (Component_Definition (N));
2056 if Nkind (Sindic) = N_Subtype_Indication
2057 and then Present (Constraint (Sindic))
2058 and then Contains_POC (Constraint (Sindic))
2060 Set_Has_Per_Object_Constraint (Id);
2065 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2066 -- out some static checks.
2068 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2069 Null_Exclusion_Static_Checks (N);
2072 -- If this component is private (or depends on a private type), flag the
2073 -- record type to indicate that some operations are not available.
2075 P := Private_Component (T);
2079 -- Check for circular definitions
2081 if P = Any_Type then
2082 Set_Etype (Id, Any_Type);
2084 -- There is a gap in the visibility of operations only if the
2085 -- component type is not defined in the scope of the record type.
2087 elsif Scope (P) = Scope (Current_Scope) then
2090 elsif Is_Limited_Type (P) then
2091 Set_Is_Limited_Composite (Current_Scope);
2094 Set_Is_Private_Composite (Current_Scope);
2099 and then Is_Limited_Type (T)
2100 and then Chars (Id) /= Name_uParent
2101 and then Is_Tagged_Type (Current_Scope)
2103 if Is_Derived_Type (Current_Scope)
2104 and then not Is_Known_Limited (Current_Scope)
2107 ("extension of nonlimited type cannot have limited components",
2110 if Is_Interface (Root_Type (Current_Scope)) then
2112 ("\limitedness is not inherited from limited interface", N);
2113 Error_Msg_N ("\add LIMITED to type indication", N);
2116 Explain_Limited_Type (T, N);
2117 Set_Etype (Id, Any_Type);
2118 Set_Is_Limited_Composite (Current_Scope, False);
2120 elsif not Is_Derived_Type (Current_Scope)
2121 and then not Is_Limited_Record (Current_Scope)
2122 and then not Is_Concurrent_Type (Current_Scope)
2125 ("nonlimited tagged type cannot have limited components", N);
2126 Explain_Limited_Type (T, N);
2127 Set_Etype (Id, Any_Type);
2128 Set_Is_Limited_Composite (Current_Scope, False);
2132 -- If the component is an unconstrained task or protected type with
2133 -- discriminants, the component and the enclosing record are limited
2134 -- and the component is constrained by its default values. Compute
2135 -- its actual subtype, else it may be allocated the maximum size by
2136 -- the backend, and possibly overflow.
2138 if Is_Concurrent_Type (T)
2139 and then not Is_Constrained (T)
2140 and then Has_Discriminants (T)
2141 and then not Has_Discriminants (Current_Scope)
2144 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2147 Set_Etype (Id, Act_T);
2149 -- Rewrite component definition to use the constrained subtype
2151 Rewrite (Component_Definition (N),
2152 Make_Component_Definition (Loc,
2153 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2157 Set_Original_Record_Component (Id, Id);
2159 if Has_Aspects (N) then
2160 Analyze_Aspect_Specifications (N, Id);
2163 Analyze_Dimension (N);
2164 end Analyze_Component_Declaration;
2166 --------------------------
2167 -- Analyze_Declarations --
2168 --------------------------
2170 procedure Analyze_Declarations (L : List_Id) is
2173 procedure Adjust_Decl;
2174 -- Adjust Decl not to include implicit label declarations, since these
2175 -- have strange Sloc values that result in elaboration check problems.
2176 -- (They have the sloc of the label as found in the source, and that
2177 -- is ahead of the current declarative part).
2179 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2180 -- Determine whether Body_Decl denotes the body of a late controlled
2181 -- primitive (either Initialize, Adjust or Finalize). If this is the
2182 -- case, add a proper spec if the body lacks one. The spec is inserted
2183 -- before Body_Decl and immedately analyzed.
2185 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2186 -- Spec_Id is the entity of a package that may define abstract states.
2187 -- If the states have visible refinement, remove the visibility of each
2188 -- constituent at the end of the package body declarations.
2194 procedure Adjust_Decl is
2196 while Present (Prev (Decl))
2197 and then Nkind (Decl) = N_Implicit_Label_Declaration
2203 --------------------------------------
2204 -- Handle_Late_Controlled_Primitive --
2205 --------------------------------------
2207 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2208 Body_Spec : constant Node_Id := Specification (Body_Decl);
2209 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2210 Loc : constant Source_Ptr := Sloc (Body_Id);
2211 Params : constant List_Id :=
2212 Parameter_Specifications (Body_Spec);
2214 Spec_Id : Entity_Id;
2218 -- Consider only procedure bodies whose name matches one of the three
2219 -- controlled primitives.
2221 if Nkind (Body_Spec) /= N_Procedure_Specification
2222 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2228 -- A controlled primitive must have exactly one formal which is not
2229 -- an anonymous access type.
2231 elsif List_Length (Params) /= 1 then
2235 Typ := Parameter_Type (First (Params));
2237 if Nkind (Typ) = N_Access_Definition then
2243 -- The type of the formal must be derived from [Limited_]Controlled
2245 if not Is_Controlled (Entity (Typ)) then
2249 -- Check whether a specification exists for this body. We do not
2250 -- analyze the spec of the body in full, because it will be analyzed
2251 -- again when the body is properly analyzed, and we cannot create
2252 -- duplicate entries in the formals chain. We look for an explicit
2253 -- specification because the body may be an overriding operation and
2254 -- an inherited spec may be present.
2256 Spec_Id := Current_Entity (Body_Id);
2258 while Present (Spec_Id) loop
2259 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2260 and then Scope (Spec_Id) = Current_Scope
2261 and then Present (First_Formal (Spec_Id))
2262 and then No (Next_Formal (First_Formal (Spec_Id)))
2263 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2264 and then Comes_From_Source (Spec_Id)
2269 Spec_Id := Homonym (Spec_Id);
2272 -- At this point the body is known to be a late controlled primitive.
2273 -- Generate a matching spec and insert it before the body. Note the
2274 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2275 -- tree in this case.
2277 Spec := Copy_Separate_Tree (Body_Spec);
2279 -- Ensure that the subprogram declaration does not inherit the null
2280 -- indicator from the body as we now have a proper spec/body pair.
2282 Set_Null_Present (Spec, False);
2284 Insert_Before_And_Analyze (Body_Decl,
2285 Make_Subprogram_Declaration (Loc, Specification => Spec));
2286 end Handle_Late_Controlled_Primitive;
2288 --------------------------------
2289 -- Remove_Visible_Refinements --
2290 --------------------------------
2292 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2293 State_Elmt : Elmt_Id;
2295 if Present (Abstract_States (Spec_Id)) then
2296 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2297 while Present (State_Elmt) loop
2298 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2299 Next_Elmt (State_Elmt);
2302 end Remove_Visible_Refinements;
2306 Context : Node_Id := Empty;
2307 Freeze_From : Entity_Id := Empty;
2308 Next_Decl : Node_Id;
2309 Pack_Decl : Node_Id := Empty;
2311 Body_Seen : Boolean := False;
2312 -- Flag set when the first body [stub] is encountered
2314 -- Start of processing for Analyze_Declarations
2317 if Restriction_Check_Required (SPARK_05) then
2318 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2322 while Present (Decl) loop
2324 -- Package spec cannot contain a package declaration in SPARK
2326 if Nkind (Decl) = N_Package_Declaration
2327 and then Nkind (Parent (L)) = N_Package_Specification
2329 Check_SPARK_05_Restriction
2330 ("package specification cannot contain a package declaration",
2334 -- Complete analysis of declaration
2337 Next_Decl := Next (Decl);
2339 if No (Freeze_From) then
2340 Freeze_From := First_Entity (Current_Scope);
2343 -- At the end of a declarative part, freeze remaining entities
2344 -- declared in it. The end of the visible declarations of package
2345 -- specification is not the end of a declarative part if private
2346 -- declarations are present. The end of a package declaration is a
2347 -- freezing point only if it a library package. A task definition or
2348 -- protected type definition is not a freeze point either. Finally,
2349 -- we do not freeze entities in generic scopes, because there is no
2350 -- code generated for them and freeze nodes will be generated for
2353 -- The end of a package instantiation is not a freeze point, but
2354 -- for now we make it one, because the generic body is inserted
2355 -- (currently) immediately after. Generic instantiations will not
2356 -- be a freeze point once delayed freezing of bodies is implemented.
2357 -- (This is needed in any case for early instantiations ???).
2359 if No (Next_Decl) then
2360 if Nkind_In (Parent (L), N_Component_List,
2362 N_Protected_Definition)
2366 elsif Nkind (Parent (L)) /= N_Package_Specification then
2367 if Nkind (Parent (L)) = N_Package_Body then
2368 Freeze_From := First_Entity (Current_Scope);
2371 -- There may have been several freezing points previously,
2372 -- for example object declarations or subprogram bodies, but
2373 -- at the end of a declarative part we check freezing from
2374 -- the beginning, even though entities may already be frozen,
2375 -- in order to perform visibility checks on delayed aspects.
2378 Freeze_All (First_Entity (Current_Scope), Decl);
2379 Freeze_From := Last_Entity (Current_Scope);
2381 elsif Scope (Current_Scope) /= Standard_Standard
2382 and then not Is_Child_Unit (Current_Scope)
2383 and then No (Generic_Parent (Parent (L)))
2387 elsif L /= Visible_Declarations (Parent (L))
2388 or else No (Private_Declarations (Parent (L)))
2389 or else Is_Empty_List (Private_Declarations (Parent (L)))
2392 Freeze_All (First_Entity (Current_Scope), Decl);
2393 Freeze_From := Last_Entity (Current_Scope);
2396 -- If next node is a body then freeze all types before the body.
2397 -- An exception occurs for some expander-generated bodies. If these
2398 -- are generated at places where in general language rules would not
2399 -- allow a freeze point, then we assume that the expander has
2400 -- explicitly checked that all required types are properly frozen,
2401 -- and we do not cause general freezing here. This special circuit
2402 -- is used when the encountered body is marked as having already
2405 -- In all other cases (bodies that come from source, and expander
2406 -- generated bodies that have not been analyzed yet), freeze all
2407 -- types now. Note that in the latter case, the expander must take
2408 -- care to attach the bodies at a proper place in the tree so as to
2409 -- not cause unwanted freezing at that point.
2411 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl) then
2413 -- When a controlled type is frozen, the expander generates stream
2414 -- and controlled type support routines. If the freeze is caused
2415 -- by the stand alone body of Initialize, Adjust and Finalize, the
2416 -- expander will end up using the wrong version of these routines
2417 -- as the body has not been processed yet. To remedy this, detect
2418 -- a late controlled primitive and create a proper spec for it.
2419 -- This ensures that the primitive will override its inherited
2420 -- counterpart before the freeze takes place.
2422 -- If the declaration we just processed is a body, do not attempt
2423 -- to examine Next_Decl as the late primitive idiom can only apply
2424 -- to the first encountered body.
2426 -- The spec of the late primitive is not generated in ASIS mode to
2427 -- ensure a consistent list of primitives that indicates the true
2428 -- semantic structure of the program (which is not relevant when
2429 -- generating executable code.
2431 -- ??? a cleaner approach may be possible and/or this solution
2432 -- could be extended to general-purpose late primitives, TBD.
2434 if not ASIS_Mode and then not Body_Seen and then not Is_Body (Decl)
2438 if Nkind (Next_Decl) = N_Subprogram_Body then
2439 Handle_Late_Controlled_Primitive (Next_Decl);
2444 Freeze_All (Freeze_From, Decl);
2445 Freeze_From := Last_Entity (Current_Scope);
2451 -- Analyze the contracts of packages and their bodies
2454 Context := Parent (L);
2456 if Nkind (Context) = N_Package_Specification then
2457 Pack_Decl := Parent (Context);
2459 -- When a package has private declarations, its contract must be
2460 -- analyzed at the end of the said declarations. This way both the
2461 -- analysis and freeze actions are properly synchronized in case
2462 -- of private type use within the contract.
2464 if L = Private_Declarations (Context) then
2465 Analyze_Package_Contract (Defining_Entity (Context));
2467 -- Build the bodies of the default initial condition procedures
2468 -- for all types subject to pragma Default_Initial_Condition.
2469 -- From a purely Ada stand point, this is a freezing activity,
2470 -- however freezing is not available under GNATprove_Mode. To
2471 -- accomodate both scenarios, the bodies are build at the end
2472 -- of private declaration analysis.
2474 Build_Default_Init_Cond_Procedure_Bodies (L);
2476 -- Otherwise the contract is analyzed at the end of the visible
2479 elsif L = Visible_Declarations (Context)
2480 and then No (Private_Declarations (Context))
2482 Analyze_Package_Contract (Defining_Entity (Context));
2485 elsif Nkind (Context) = N_Package_Body then
2486 Pack_Decl := Context;
2487 Analyze_Package_Body_Contract (Defining_Entity (Context));
2490 -- Analyze the contracts of all subprogram declarations, subprogram
2491 -- bodies and variables now due to the delayed visibility needs of
2492 -- of their aspects and pragmas. Capture global references in generic
2493 -- subprograms or bodies.
2496 while Present (Decl) loop
2497 if Nkind (Decl) = N_Object_Declaration then
2498 Analyze_Object_Contract (Defining_Entity (Decl));
2500 elsif Nkind_In (Decl, N_Abstract_Subprogram_Declaration,
2501 N_Generic_Subprogram_Declaration,
2502 N_Subprogram_Declaration)
2504 Analyze_Subprogram_Contract (Defining_Entity (Decl));
2506 elsif Nkind (Decl) = N_Subprogram_Body then
2507 Analyze_Subprogram_Body_Contract (Defining_Entity (Decl));
2509 elsif Nkind (Decl) = N_Subprogram_Body_Stub then
2510 Analyze_Subprogram_Body_Stub_Contract (Defining_Entity (Decl));
2513 -- Capture all global references in a generic subprogram or a body
2514 -- [stub] now that the contract has been analyzed.
2516 if Nkind_In (Decl, N_Generic_Subprogram_Declaration,
2518 N_Subprogram_Body_Stub)
2519 and then Is_Generic_Declaration_Or_Body (Decl)
2521 Save_Global_References_In_Contract
2522 (Templ => Original_Node (Decl),
2523 Gen_Id => Corresponding_Spec_Of (Decl));
2529 -- The owner of the declarations is a package [body]
2531 if Present (Pack_Decl) then
2533 -- Capture all global references in a generic package or a body
2534 -- after all nested generic subprograms and bodies were subjected
2535 -- to the same processing.
2537 if Is_Generic_Declaration_Or_Body (Pack_Decl) then
2538 Save_Global_References_In_Contract
2539 (Templ => Original_Node (Pack_Decl),
2540 Gen_Id => Corresponding_Spec_Of (Pack_Decl));
2543 -- State refinements are visible upto the end the of the package
2544 -- body declarations. Hide the state refinements from visibility
2545 -- to restore the original state conditions.
2547 if Nkind (Pack_Decl) = N_Package_Body then
2548 Remove_Visible_Refinements (Corresponding_Spec (Pack_Decl));
2552 end Analyze_Declarations;
2554 -----------------------------------
2555 -- Analyze_Full_Type_Declaration --
2556 -----------------------------------
2558 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2559 Def : constant Node_Id := Type_Definition (N);
2560 Def_Id : constant Entity_Id := Defining_Identifier (N);
2564 Is_Remote : constant Boolean :=
2565 (Is_Remote_Types (Current_Scope)
2566 or else Is_Remote_Call_Interface (Current_Scope))
2567 and then not (In_Private_Part (Current_Scope)
2568 or else In_Package_Body (Current_Scope));
2570 procedure Check_Ops_From_Incomplete_Type;
2571 -- If there is a tagged incomplete partial view of the type, traverse
2572 -- the primitives of the incomplete view and change the type of any
2573 -- controlling formals and result to indicate the full view. The
2574 -- primitives will be added to the full type's primitive operations
2575 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2576 -- is called from Process_Incomplete_Dependents).
2578 ------------------------------------
2579 -- Check_Ops_From_Incomplete_Type --
2580 ------------------------------------
2582 procedure Check_Ops_From_Incomplete_Type is
2589 and then Ekind (Prev) = E_Incomplete_Type
2590 and then Is_Tagged_Type (Prev)
2591 and then Is_Tagged_Type (T)
2593 Elmt := First_Elmt (Primitive_Operations (Prev));
2594 while Present (Elmt) loop
2597 Formal := First_Formal (Op);
2598 while Present (Formal) loop
2599 if Etype (Formal) = Prev then
2600 Set_Etype (Formal, T);
2603 Next_Formal (Formal);
2606 if Etype (Op) = Prev then
2613 end Check_Ops_From_Incomplete_Type;
2615 -- Start of processing for Analyze_Full_Type_Declaration
2618 Prev := Find_Type_Name (N);
2620 -- The type declaration may be subject to pragma Ghost with policy
2621 -- Ignore. Set the mode now to ensure that any nodes generated during
2622 -- analysis and expansion are properly flagged as ignored Ghost.
2624 Set_Ghost_Mode (N, Prev);
2626 -- The full view, if present, now points to the current type. If there
2627 -- is an incomplete partial view, set a link to it, to simplify the
2628 -- retrieval of primitive operations of the type.
2630 -- Ada 2005 (AI-50217): If the type was previously decorated when
2631 -- imported through a LIMITED WITH clause, it appears as incomplete
2632 -- but has no full view.
2634 if Ekind (Prev) = E_Incomplete_Type
2635 and then Present (Full_View (Prev))
2637 T := Full_View (Prev);
2638 Set_Incomplete_View (N, Parent (Prev));
2643 Set_Is_Pure (T, Is_Pure (Current_Scope));
2645 -- We set the flag Is_First_Subtype here. It is needed to set the
2646 -- corresponding flag for the Implicit class-wide-type created
2647 -- during tagged types processing.
2649 Set_Is_First_Subtype (T, True);
2651 -- Only composite types other than array types are allowed to have
2656 -- For derived types, the rule will be checked once we've figured
2657 -- out the parent type.
2659 when N_Derived_Type_Definition =>
2662 -- For record types, discriminants are allowed, unless we are in
2665 when N_Record_Definition =>
2666 if Present (Discriminant_Specifications (N)) then
2667 Check_SPARK_05_Restriction
2668 ("discriminant type is not allowed",
2670 (First (Discriminant_Specifications (N))));
2674 if Present (Discriminant_Specifications (N)) then
2676 ("elementary or array type cannot have discriminants",
2678 (First (Discriminant_Specifications (N))));
2682 -- Elaborate the type definition according to kind, and generate
2683 -- subsidiary (implicit) subtypes where needed. We skip this if it was
2684 -- already done (this happens during the reanalysis that follows a call
2685 -- to the high level optimizer).
2687 if not Analyzed (T) then
2691 when N_Access_To_Subprogram_Definition =>
2692 Access_Subprogram_Declaration (T, Def);
2694 -- If this is a remote access to subprogram, we must create the
2695 -- equivalent fat pointer type, and related subprograms.
2698 Process_Remote_AST_Declaration (N);
2701 -- Validate categorization rule against access type declaration
2702 -- usually a violation in Pure unit, Shared_Passive unit.
2704 Validate_Access_Type_Declaration (T, N);
2706 when N_Access_To_Object_Definition =>
2707 Access_Type_Declaration (T, Def);
2709 -- Validate categorization rule against access type declaration
2710 -- usually a violation in Pure unit, Shared_Passive unit.
2712 Validate_Access_Type_Declaration (T, N);
2714 -- If we are in a Remote_Call_Interface package and define a
2715 -- RACW, then calling stubs and specific stream attributes
2719 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
2721 Add_RACW_Features (Def_Id);
2724 when N_Array_Type_Definition =>
2725 Array_Type_Declaration (T, Def);
2727 when N_Derived_Type_Definition =>
2728 Derived_Type_Declaration (T, N, T /= Def_Id);
2730 when N_Enumeration_Type_Definition =>
2731 Enumeration_Type_Declaration (T, Def);
2733 when N_Floating_Point_Definition =>
2734 Floating_Point_Type_Declaration (T, Def);
2736 when N_Decimal_Fixed_Point_Definition =>
2737 Decimal_Fixed_Point_Type_Declaration (T, Def);
2739 when N_Ordinary_Fixed_Point_Definition =>
2740 Ordinary_Fixed_Point_Type_Declaration (T, Def);
2742 when N_Signed_Integer_Type_Definition =>
2743 Signed_Integer_Type_Declaration (T, Def);
2745 when N_Modular_Type_Definition =>
2746 Modular_Type_Declaration (T, Def);
2748 when N_Record_Definition =>
2749 Record_Type_Declaration (T, N, Prev);
2751 -- If declaration has a parse error, nothing to elaborate.
2757 raise Program_Error;
2762 if Etype (T) = Any_Type then
2766 -- Controlled type is not allowed in SPARK
2768 if Is_Visibly_Controlled (T) then
2769 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
2772 -- A type declared within a Ghost region is automatically Ghost
2773 -- (SPARK RM 6.9(2)).
2775 if Comes_From_Source (T) and then Ghost_Mode > None then
2776 Set_Is_Ghost_Entity (T);
2779 -- Some common processing for all types
2781 Set_Depends_On_Private (T, Has_Private_Component (T));
2782 Check_Ops_From_Incomplete_Type;
2784 -- Both the declared entity, and its anonymous base type if one was
2785 -- created, need freeze nodes allocated.
2788 B : constant Entity_Id := Base_Type (T);
2791 -- In the case where the base type differs from the first subtype, we
2792 -- pre-allocate a freeze node, and set the proper link to the first
2793 -- subtype. Freeze_Entity will use this preallocated freeze node when
2794 -- it freezes the entity.
2796 -- This does not apply if the base type is a generic type, whose
2797 -- declaration is independent of the current derived definition.
2799 if B /= T and then not Is_Generic_Type (B) then
2800 Ensure_Freeze_Node (B);
2801 Set_First_Subtype_Link (Freeze_Node (B), T);
2804 -- A type that is imported through a limited_with clause cannot
2805 -- generate any code, and thus need not be frozen. However, an access
2806 -- type with an imported designated type needs a finalization list,
2807 -- which may be referenced in some other package that has non-limited
2808 -- visibility on the designated type. Thus we must create the
2809 -- finalization list at the point the access type is frozen, to
2810 -- prevent unsatisfied references at link time.
2812 if not From_Limited_With (T) or else Is_Access_Type (T) then
2813 Set_Has_Delayed_Freeze (T);
2817 -- Case where T is the full declaration of some private type which has
2818 -- been swapped in Defining_Identifier (N).
2820 if T /= Def_Id and then Is_Private_Type (Def_Id) then
2821 Process_Full_View (N, T, Def_Id);
2823 -- Record the reference. The form of this is a little strange, since
2824 -- the full declaration has been swapped in. So the first parameter
2825 -- here represents the entity to which a reference is made which is
2826 -- the "real" entity, i.e. the one swapped in, and the second
2827 -- parameter provides the reference location.
2829 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
2830 -- since we don't want a complaint about the full type being an
2831 -- unwanted reference to the private type
2834 B : constant Boolean := Has_Pragma_Unreferenced (T);
2836 Set_Has_Pragma_Unreferenced (T, False);
2837 Generate_Reference (T, T, 'c');
2838 Set_Has_Pragma_Unreferenced (T, B);
2841 Set_Completion_Referenced (Def_Id);
2843 -- For completion of incomplete type, process incomplete dependents
2844 -- and always mark the full type as referenced (it is the incomplete
2845 -- type that we get for any real reference).
2847 elsif Ekind (Prev) = E_Incomplete_Type then
2848 Process_Incomplete_Dependents (N, T, Prev);
2849 Generate_Reference (Prev, Def_Id, 'c');
2850 Set_Completion_Referenced (Def_Id);
2852 -- If not private type or incomplete type completion, this is a real
2853 -- definition of a new entity, so record it.
2856 Generate_Definition (Def_Id);
2859 -- Propagate any pending access types whose finalization masters need to
2860 -- be fully initialized from the partial to the full view. Guard against
2861 -- an illegal full view that remains unanalyzed.
2863 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
2864 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
2867 if Chars (Scope (Def_Id)) = Name_System
2868 and then Chars (Def_Id) = Name_Address
2869 and then Is_Predefined_File_Name (Unit_File_Name (Get_Source_Unit (N)))
2871 Set_Is_Descendent_Of_Address (Def_Id);
2872 Set_Is_Descendent_Of_Address (Base_Type (Def_Id));
2873 Set_Is_Descendent_Of_Address (Prev);
2876 Set_Optimize_Alignment_Flags (Def_Id);
2877 Check_Eliminated (Def_Id);
2879 -- If the declaration is a completion and aspects are present, apply
2880 -- them to the entity for the type which is currently the partial
2881 -- view, but which is the one that will be frozen.
2883 if Has_Aspects (N) then
2885 -- In most cases the partial view is a private type, and both views
2886 -- appear in different declarative parts. In the unusual case where
2887 -- the partial view is incomplete, perform the analysis on the
2888 -- full view, to prevent freezing anomalies with the corresponding
2889 -- class-wide type, which otherwise might be frozen before the
2890 -- dispatch table is built.
2893 and then Ekind (Prev) /= E_Incomplete_Type
2895 Analyze_Aspect_Specifications (N, Prev);
2900 Analyze_Aspect_Specifications (N, Def_Id);
2903 end Analyze_Full_Type_Declaration;
2905 ----------------------------------
2906 -- Analyze_Incomplete_Type_Decl --
2907 ----------------------------------
2909 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
2910 F : constant Boolean := Is_Pure (Current_Scope);
2914 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
2916 Generate_Definition (Defining_Identifier (N));
2918 -- Process an incomplete declaration. The identifier must not have been
2919 -- declared already in the scope. However, an incomplete declaration may
2920 -- appear in the private part of a package, for a private type that has
2921 -- already been declared.
2923 -- In this case, the discriminants (if any) must match
2925 T := Find_Type_Name (N);
2927 Set_Ekind (T, E_Incomplete_Type);
2928 Init_Size_Align (T);
2929 Set_Is_First_Subtype (T, True);
2932 -- An incomplete type declared within a Ghost region is automatically
2933 -- Ghost (SPARK RM 6.9(2)).
2935 if Ghost_Mode > None then
2936 Set_Is_Ghost_Entity (T);
2939 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
2940 -- incomplete types.
2942 if Tagged_Present (N) then
2943 Set_Is_Tagged_Type (T, True);
2944 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2945 Make_Class_Wide_Type (T);
2946 Set_Direct_Primitive_Operations (T, New_Elmt_List);
2951 Set_Stored_Constraint (T, No_Elist);
2953 if Present (Discriminant_Specifications (N)) then
2954 Process_Discriminants (N);
2959 -- If the type has discriminants, non-trivial subtypes may be
2960 -- declared before the full view of the type. The full views of those
2961 -- subtypes will be built after the full view of the type.
2963 Set_Private_Dependents (T, New_Elmt_List);
2965 end Analyze_Incomplete_Type_Decl;
2967 -----------------------------------
2968 -- Analyze_Interface_Declaration --
2969 -----------------------------------
2971 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
2972 CW : constant Entity_Id := Class_Wide_Type (T);
2975 Set_Is_Tagged_Type (T);
2976 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
2978 Set_Is_Limited_Record (T, Limited_Present (Def)
2979 or else Task_Present (Def)
2980 or else Protected_Present (Def)
2981 or else Synchronized_Present (Def));
2983 -- Type is abstract if full declaration carries keyword, or if previous
2984 -- partial view did.
2986 Set_Is_Abstract_Type (T);
2987 Set_Is_Interface (T);
2989 -- Type is a limited interface if it includes the keyword limited, task,
2990 -- protected, or synchronized.
2992 Set_Is_Limited_Interface
2993 (T, Limited_Present (Def)
2994 or else Protected_Present (Def)
2995 or else Synchronized_Present (Def)
2996 or else Task_Present (Def));
2998 Set_Interfaces (T, New_Elmt_List);
2999 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3001 -- Complete the decoration of the class-wide entity if it was already
3002 -- built (i.e. during the creation of the limited view)
3004 if Present (CW) then
3005 Set_Is_Interface (CW);
3006 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3009 -- Check runtime support for synchronized interfaces
3011 if VM_Target = No_VM
3012 and then (Is_Task_Interface (T)
3013 or else Is_Protected_Interface (T)
3014 or else Is_Synchronized_Interface (T))
3015 and then not RTE_Available (RE_Select_Specific_Data)
3017 Error_Msg_CRT ("synchronized interfaces", T);
3019 end Analyze_Interface_Declaration;
3021 -----------------------------
3022 -- Analyze_Itype_Reference --
3023 -----------------------------
3025 -- Nothing to do. This node is placed in the tree only for the benefit of
3026 -- back end processing, and has no effect on the semantic processing.
3028 procedure Analyze_Itype_Reference (N : Node_Id) is
3030 pragma Assert (Is_Itype (Itype (N)));
3032 end Analyze_Itype_Reference;
3034 --------------------------------
3035 -- Analyze_Number_Declaration --
3036 --------------------------------
3038 procedure Analyze_Number_Declaration (N : Node_Id) is
3039 Id : constant Entity_Id := Defining_Identifier (N);
3040 E : constant Node_Id := Expression (N);
3042 Index : Interp_Index;
3046 -- The number declaration may be subject to pragma Ghost with policy
3047 -- Ignore. Set the mode now to ensure that any nodes generated during
3048 -- analysis and expansion are properly flagged as ignored Ghost.
3052 Generate_Definition (Id);
3055 -- A number declared within a Ghost region is automatically Ghost
3056 -- (SPARK RM 6.9(2)).
3058 if Ghost_Mode > None then
3059 Set_Is_Ghost_Entity (Id);
3062 -- This is an optimization of a common case of an integer literal
3064 if Nkind (E) = N_Integer_Literal then
3065 Set_Is_Static_Expression (E, True);
3066 Set_Etype (E, Universal_Integer);
3068 Set_Etype (Id, Universal_Integer);
3069 Set_Ekind (Id, E_Named_Integer);
3070 Set_Is_Frozen (Id, True);
3074 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3076 -- Process expression, replacing error by integer zero, to avoid
3077 -- cascaded errors or aborts further along in the processing
3079 -- Replace Error by integer zero, which seems least likely to cause
3083 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3084 Set_Error_Posted (E);
3089 -- Verify that the expression is static and numeric. If
3090 -- the expression is overloaded, we apply the preference
3091 -- rule that favors root numeric types.
3093 if not Is_Overloaded (E) then
3095 if Has_Dynamic_Predicate_Aspect (T) then
3097 ("subtype has dynamic predicate, "
3098 & "not allowed in number declaration", N);
3104 Get_First_Interp (E, Index, It);
3105 while Present (It.Typ) loop
3106 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3107 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3109 if T = Any_Type then
3112 elsif It.Typ = Universal_Real
3114 It.Typ = Universal_Integer
3116 -- Choose universal interpretation over any other
3123 Get_Next_Interp (Index, It);
3127 if Is_Integer_Type (T) then
3129 Set_Etype (Id, Universal_Integer);
3130 Set_Ekind (Id, E_Named_Integer);
3132 elsif Is_Real_Type (T) then
3134 -- Because the real value is converted to universal_real, this is a
3135 -- legal context for a universal fixed expression.
3137 if T = Universal_Fixed then
3139 Loc : constant Source_Ptr := Sloc (N);
3140 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3142 New_Occurrence_Of (Universal_Real, Loc),
3143 Expression => Relocate_Node (E));
3150 elsif T = Any_Fixed then
3151 Error_Msg_N ("illegal context for mixed mode operation", E);
3153 -- Expression is of the form : universal_fixed * integer. Try to
3154 -- resolve as universal_real.
3156 T := Universal_Real;
3161 Set_Etype (Id, Universal_Real);
3162 Set_Ekind (Id, E_Named_Real);
3165 Wrong_Type (E, Any_Numeric);
3169 Set_Ekind (Id, E_Constant);
3170 Set_Never_Set_In_Source (Id, True);
3171 Set_Is_True_Constant (Id, True);
3175 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3176 Set_Etype (E, Etype (Id));
3179 if not Is_OK_Static_Expression (E) then
3180 Flag_Non_Static_Expr
3181 ("non-static expression used in number declaration!", E);
3182 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3183 Set_Etype (E, Any_Type);
3185 end Analyze_Number_Declaration;
3187 -----------------------------
3188 -- Analyze_Object_Contract --
3189 -----------------------------
3191 procedure Analyze_Object_Contract (Obj_Id : Entity_Id) is
3192 Obj_Typ : constant Entity_Id := Etype (Obj_Id);
3193 AR_Val : Boolean := False;
3194 AW_Val : Boolean := False;
3195 ER_Val : Boolean := False;
3196 EW_Val : Boolean := False;
3198 Seen : Boolean := False;
3201 -- The loop parameter in an element iterator over a formal container
3202 -- is declared with an object declaration but no contracts apply.
3204 if Ekind (Obj_Id) = E_Loop_Parameter then
3208 if Ekind (Obj_Id) = E_Constant then
3210 -- A constant cannot be effectively volatile. This check is only
3211 -- relevant with SPARK_Mode on as it is not a standard Ada legality
3212 -- rule. Do not flag internally-generated constants that map generic
3213 -- formals to actuals in instantiations (SPARK RM 7.1.3(6)).
3216 and then Is_Effectively_Volatile (Obj_Id)
3217 and then No (Corresponding_Generic_Association (Parent (Obj_Id)))
3219 -- Don't give this for internally generated entities (such as the
3220 -- FIRST and LAST temporaries generated for bounds).
3222 and then Comes_From_Source (Obj_Id)
3224 Error_Msg_N ("constant cannot be volatile", Obj_Id);
3227 else pragma Assert (Ekind (Obj_Id) = E_Variable);
3229 -- The following checks are only relevant when SPARK_Mode is on as
3230 -- they are not standard Ada legality rules. Internally generated
3231 -- temporaries are ignored.
3233 if SPARK_Mode = On and then Comes_From_Source (Obj_Id) then
3234 if Is_Effectively_Volatile (Obj_Id) then
3236 -- The declaration of an effectively volatile object must
3237 -- appear at the library level (SPARK RM 7.1.3(7), C.6(6)).
3239 if not Is_Library_Level_Entity (Obj_Id) then
3241 ("volatile variable & must be declared at library level",
3244 -- An object of a discriminated type cannot be effectively
3245 -- volatile (SPARK RM C.6(4)).
3247 elsif Has_Discriminants (Obj_Typ) then
3249 ("discriminated object & cannot be volatile", Obj_Id);
3251 -- An object of a tagged type cannot be effectively volatile
3252 -- (SPARK RM C.6(5)).
3254 elsif Is_Tagged_Type (Obj_Typ) then
3255 Error_Msg_N ("tagged object & cannot be volatile", Obj_Id);
3258 -- The object is not effectively volatile
3261 -- A non-effectively volatile object cannot have effectively
3262 -- volatile components (SPARK RM 7.1.3(7)).
3264 if not Is_Effectively_Volatile (Obj_Id)
3265 and then Has_Volatile_Component (Obj_Typ)
3268 ("non-volatile object & cannot have volatile components",
3274 if Is_Ghost_Entity (Obj_Id) then
3276 -- A Ghost object cannot be effectively volatile (SPARK RM 6.9(8))
3278 if Is_Effectively_Volatile (Obj_Id) then
3279 Error_Msg_N ("ghost variable & cannot be volatile", Obj_Id);
3281 -- A Ghost object cannot be imported or exported (SPARK RM 6.9(8))
3283 elsif Is_Imported (Obj_Id) then
3284 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3286 elsif Is_Exported (Obj_Id) then
3287 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3291 -- Analyze all external properties
3293 Prag := Get_Pragma (Obj_Id, Pragma_Async_Readers);
3295 if Present (Prag) then
3296 Analyze_External_Property_In_Decl_Part (Prag, AR_Val);
3300 Prag := Get_Pragma (Obj_Id, Pragma_Async_Writers);
3302 if Present (Prag) then
3303 Analyze_External_Property_In_Decl_Part (Prag, AW_Val);
3307 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Reads);
3309 if Present (Prag) then
3310 Analyze_External_Property_In_Decl_Part (Prag, ER_Val);
3314 Prag := Get_Pragma (Obj_Id, Pragma_Effective_Writes);
3316 if Present (Prag) then
3317 Analyze_External_Property_In_Decl_Part (Prag, EW_Val);
3321 -- Verify the mutual interaction of the various external properties
3324 Check_External_Properties (Obj_Id, AR_Val, AW_Val, ER_Val, EW_Val);
3327 -- Check whether the lack of indicator Part_Of agrees with the
3328 -- placement of the variable with respect to the state space.
3330 Prag := Get_Pragma (Obj_Id, Pragma_Part_Of);
3333 Check_Missing_Part_Of (Obj_Id);
3337 -- A ghost object cannot be imported or exported (SPARK RM 6.9(8))
3339 if Is_Ghost_Entity (Obj_Id) then
3340 if Is_Exported (Obj_Id) then
3341 Error_Msg_N ("ghost object & cannot be exported", Obj_Id);
3343 elsif Is_Imported (Obj_Id) then
3344 Error_Msg_N ("ghost object & cannot be imported", Obj_Id);
3347 end Analyze_Object_Contract;
3349 --------------------------------
3350 -- Analyze_Object_Declaration --
3351 --------------------------------
3353 procedure Analyze_Object_Declaration (N : Node_Id) is
3354 Loc : constant Source_Ptr := Sloc (N);
3355 Id : constant Entity_Id := Defining_Identifier (N);
3359 E : Node_Id := Expression (N);
3360 -- E is set to Expression (N) throughout this routine. When
3361 -- Expression (N) is modified, E is changed accordingly.
3363 Prev_Entity : Entity_Id := Empty;
3365 function Count_Tasks (T : Entity_Id) return Uint;
3366 -- This function is called when a non-generic library level object of a
3367 -- task type is declared. Its function is to count the static number of
3368 -- tasks declared within the type (it is only called if Has_Tasks is set
3369 -- for T). As a side effect, if an array of tasks with non-static bounds
3370 -- or a variant record type is encountered, Check_Restrictions is called
3371 -- indicating the count is unknown.
3373 function Delayed_Aspect_Present return Boolean;
3374 -- If the declaration has an expression that is an aggregate, and it
3375 -- has aspects that require delayed analysis, the resolution of the
3376 -- aggregate must be deferred to the freeze point of the objet. This
3377 -- special processing was created for address clauses, but it must
3378 -- also apply to Alignment. This must be done before the aspect
3379 -- specifications are analyzed because we must handle the aggregate
3380 -- before the analysis of the object declaration is complete.
3382 -- Any other relevant delayed aspects on object declarations ???
3388 function Count_Tasks (T : Entity_Id) return Uint is
3394 if Is_Task_Type (T) then
3397 elsif Is_Record_Type (T) then
3398 if Has_Discriminants (T) then
3399 Check_Restriction (Max_Tasks, N);
3404 C := First_Component (T);
3405 while Present (C) loop
3406 V := V + Count_Tasks (Etype (C));
3413 elsif Is_Array_Type (T) then
3414 X := First_Index (T);
3415 V := Count_Tasks (Component_Type (T));
3416 while Present (X) loop
3419 if not Is_OK_Static_Subtype (C) then
3420 Check_Restriction (Max_Tasks, N);
3423 V := V * (UI_Max (Uint_0,
3424 Expr_Value (Type_High_Bound (C)) -
3425 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3438 ----------------------------
3439 -- Delayed_Aspect_Present --
3440 ----------------------------
3442 function Delayed_Aspect_Present return Boolean is
3447 if Present (Aspect_Specifications (N)) then
3448 A := First (Aspect_Specifications (N));
3449 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3450 while Present (A) loop
3451 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3460 end Delayed_Aspect_Present;
3462 -- Start of processing for Analyze_Object_Declaration
3465 -- There are three kinds of implicit types generated by an
3466 -- object declaration:
3468 -- 1. Those generated by the original Object Definition
3470 -- 2. Those generated by the Expression
3472 -- 3. Those used to constrain the Object Definition with the
3473 -- expression constraints when the definition is unconstrained.
3475 -- They must be generated in this order to avoid order of elaboration
3476 -- issues. Thus the first step (after entering the name) is to analyze
3477 -- the object definition.
3479 if Constant_Present (N) then
3480 Prev_Entity := Current_Entity_In_Scope (Id);
3482 if Present (Prev_Entity)
3484 -- If the homograph is an implicit subprogram, it is overridden
3485 -- by the current declaration.
3487 ((Is_Overloadable (Prev_Entity)
3488 and then Is_Inherited_Operation (Prev_Entity))
3490 -- The current object is a discriminal generated for an entry
3491 -- family index. Even though the index is a constant, in this
3492 -- particular context there is no true constant redeclaration.
3493 -- Enter_Name will handle the visibility.
3496 (Is_Discriminal (Id)
3497 and then Ekind (Discriminal_Link (Id)) =
3498 E_Entry_Index_Parameter)
3500 -- The current object is the renaming for a generic declared
3501 -- within the instance.
3504 (Ekind (Prev_Entity) = E_Package
3505 and then Nkind (Parent (Prev_Entity)) =
3506 N_Package_Renaming_Declaration
3507 and then not Comes_From_Source (Prev_Entity)
3509 Is_Generic_Instance (Renamed_Entity (Prev_Entity))))
3511 Prev_Entity := Empty;
3515 -- The object declaration may be subject to pragma Ghost with policy
3516 -- Ignore. Set the mode now to ensure that any nodes generated during
3517 -- analysis and expansion are properly flagged as ignored Ghost.
3519 Set_Ghost_Mode (N, Prev_Entity);
3521 if Present (Prev_Entity) then
3522 Constant_Redeclaration (Id, N, T);
3524 Generate_Reference (Prev_Entity, Id, 'c');
3525 Set_Completion_Referenced (Id);
3527 if Error_Posted (N) then
3529 -- Type mismatch or illegal redeclaration, Do not analyze
3530 -- expression to avoid cascaded errors.
3532 T := Find_Type_Of_Object (Object_Definition (N), N);
3534 Set_Ekind (Id, E_Variable);
3538 -- In the normal case, enter identifier at the start to catch premature
3539 -- usage in the initialization expression.
3542 Generate_Definition (Id);
3545 Mark_Coextensions (N, Object_Definition (N));
3547 T := Find_Type_Of_Object (Object_Definition (N), N);
3549 if Nkind (Object_Definition (N)) = N_Access_Definition
3551 (Access_To_Subprogram_Definition (Object_Definition (N)))
3552 and then Protected_Present
3553 (Access_To_Subprogram_Definition (Object_Definition (N)))
3555 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
3558 if Error_Posted (Id) then
3560 Set_Ekind (Id, E_Variable);
3565 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
3566 -- out some static checks
3568 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
3570 -- In case of aggregates we must also take care of the correct
3571 -- initialization of nested aggregates bug this is done at the
3572 -- point of the analysis of the aggregate (see sem_aggr.adb).
3574 if Present (Expression (N))
3575 and then Nkind (Expression (N)) = N_Aggregate
3581 Save_Typ : constant Entity_Id := Etype (Id);
3583 Set_Etype (Id, T); -- Temp. decoration for static checks
3584 Null_Exclusion_Static_Checks (N);
3585 Set_Etype (Id, Save_Typ);
3590 -- Object is marked pure if it is in a pure scope
3592 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3594 -- If deferred constant, make sure context is appropriate. We detect
3595 -- a deferred constant as a constant declaration with no expression.
3596 -- A deferred constant can appear in a package body if its completion
3597 -- is by means of an interface pragma.
3599 if Constant_Present (N) and then No (E) then
3601 -- A deferred constant may appear in the declarative part of the
3602 -- following constructs:
3606 -- extended return statements
3609 -- subprogram bodies
3612 -- When declared inside a package spec, a deferred constant must be
3613 -- completed by a full constant declaration or pragma Import. In all
3614 -- other cases, the only proper completion is pragma Import. Extended
3615 -- return statements are flagged as invalid contexts because they do
3616 -- not have a declarative part and so cannot accommodate the pragma.
3618 if Ekind (Current_Scope) = E_Return_Statement then
3620 ("invalid context for deferred constant declaration (RM 7.4)",
3623 ("\declaration requires an initialization expression",
3625 Set_Constant_Present (N, False);
3627 -- In Ada 83, deferred constant must be of private type
3629 elsif not Is_Private_Type (T) then
3630 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
3632 ("(Ada 83) deferred constant must be private type", N);
3636 -- If not a deferred constant, then the object declaration freezes
3637 -- its type, unless the object is of an anonymous type and has delayed
3638 -- aspects. In that case the type is frozen when the object itself is.
3641 Check_Fully_Declared (T, N);
3643 if Has_Delayed_Aspects (Id)
3644 and then Is_Array_Type (T)
3645 and then Is_Itype (T)
3647 Set_Has_Delayed_Freeze (T);
3649 Freeze_Before (N, T);
3653 -- If the object was created by a constrained array definition, then
3654 -- set the link in both the anonymous base type and anonymous subtype
3655 -- that are built to represent the array type to point to the object.
3657 if Nkind (Object_Definition (Declaration_Node (Id))) =
3658 N_Constrained_Array_Definition
3660 Set_Related_Array_Object (T, Id);
3661 Set_Related_Array_Object (Base_Type (T), Id);
3664 -- Special checks for protected objects not at library level
3666 if Is_Protected_Type (T)
3667 and then not Is_Library_Level_Entity (Id)
3669 Check_Restriction (No_Local_Protected_Objects, Id);
3671 -- Protected objects with interrupt handlers must be at library level
3673 -- Ada 2005: This test is not needed (and the corresponding clause
3674 -- in the RM is removed) because accessibility checks are sufficient
3675 -- to make handlers not at the library level illegal.
3677 -- AI05-0303: The AI is in fact a binding interpretation, and thus
3678 -- applies to the '95 version of the language as well.
3680 if Has_Interrupt_Handler (T) and then Ada_Version < Ada_95 then
3682 ("interrupt object can only be declared at library level", Id);
3686 -- The actual subtype of the object is the nominal subtype, unless
3687 -- the nominal one is unconstrained and obtained from the expression.
3691 -- These checks should be performed before the initialization expression
3692 -- is considered, so that the Object_Definition node is still the same
3693 -- as in source code.
3695 -- In SPARK, the nominal subtype is always given by a subtype mark
3696 -- and must not be unconstrained. (The only exception to this is the
3697 -- acceptance of declarations of constants of type String.)
3699 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
3701 Check_SPARK_05_Restriction
3702 ("subtype mark required", Object_Definition (N));
3704 elsif Is_Array_Type (T)
3705 and then not Is_Constrained (T)
3706 and then T /= Standard_String
3708 Check_SPARK_05_Restriction
3709 ("subtype mark of constrained type expected",
3710 Object_Definition (N));
3713 -- There are no aliased objects in SPARK
3715 if Aliased_Present (N) then
3716 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
3719 -- Process initialization expression if present and not in error
3721 if Present (E) and then E /= Error then
3723 -- Generate an error in case of CPP class-wide object initialization.
3724 -- Required because otherwise the expansion of the class-wide
3725 -- assignment would try to use 'size to initialize the object
3726 -- (primitive that is not available in CPP tagged types).
3728 if Is_Class_Wide_Type (Act_T)
3730 (Is_CPP_Class (Root_Type (Etype (Act_T)))
3732 (Present (Full_View (Root_Type (Etype (Act_T))))
3734 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
3737 ("predefined assignment not available for 'C'P'P tagged types",
3741 Mark_Coextensions (N, E);
3744 -- In case of errors detected in the analysis of the expression,
3745 -- decorate it with the expected type to avoid cascaded errors
3747 if No (Etype (E)) then
3751 -- If an initialization expression is present, then we set the
3752 -- Is_True_Constant flag. It will be reset if this is a variable
3753 -- and it is indeed modified.
3755 Set_Is_True_Constant (Id, True);
3757 -- If we are analyzing a constant declaration, set its completion
3758 -- flag after analyzing and resolving the expression.
3760 if Constant_Present (N) then
3761 Set_Has_Completion (Id);
3764 -- Set type and resolve (type may be overridden later on). Note:
3765 -- Ekind (Id) must still be E_Void at this point so that incorrect
3766 -- early usage within E is properly diagnosed.
3770 -- If the expression is an aggregate we must look ahead to detect
3771 -- the possible presence of an address clause, and defer resolution
3772 -- and expansion of the aggregate to the freeze point of the entity.
3774 if Comes_From_Source (N)
3775 and then Expander_Active
3776 and then Nkind (E) = N_Aggregate
3777 and then (Present (Following_Address_Clause (N))
3778 or else Delayed_Aspect_Present)
3786 -- No further action needed if E is a call to an inlined function
3787 -- which returns an unconstrained type and it has been expanded into
3788 -- a procedure call. In that case N has been replaced by an object
3789 -- declaration without initializing expression and it has been
3790 -- analyzed (see Expand_Inlined_Call).
3792 if Back_End_Inlining
3793 and then Expander_Active
3794 and then Nkind (E) = N_Function_Call
3795 and then Nkind (Name (E)) in N_Has_Entity
3796 and then Is_Inlined (Entity (Name (E)))
3797 and then not Is_Constrained (Etype (E))
3798 and then Analyzed (N)
3799 and then No (Expression (N))
3804 -- If E is null and has been replaced by an N_Raise_Constraint_Error
3805 -- node (which was marked already-analyzed), we need to set the type
3806 -- to something other than Any_Access in order to keep gigi happy.
3808 if Etype (E) = Any_Access then
3812 -- If the object is an access to variable, the initialization
3813 -- expression cannot be an access to constant.
3815 if Is_Access_Type (T)
3816 and then not Is_Access_Constant (T)
3817 and then Is_Access_Type (Etype (E))
3818 and then Is_Access_Constant (Etype (E))
3821 ("access to variable cannot be initialized with an "
3822 & "access-to-constant expression", E);
3825 if not Assignment_OK (N) then
3826 Check_Initialization (T, E);
3829 Check_Unset_Reference (E);
3831 -- If this is a variable, then set current value. If this is a
3832 -- declared constant of a scalar type with a static expression,
3833 -- indicate that it is always valid.
3835 if not Constant_Present (N) then
3836 if Compile_Time_Known_Value (E) then
3837 Set_Current_Value (Id, E);
3840 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
3841 Set_Is_Known_Valid (Id);
3844 -- Deal with setting of null flags
3846 if Is_Access_Type (T) then
3847 if Known_Non_Null (E) then
3848 Set_Is_Known_Non_Null (Id, True);
3849 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
3850 Set_Is_Known_Null (Id, True);
3854 -- Check incorrect use of dynamically tagged expressions
3856 if Is_Tagged_Type (T) then
3857 Check_Dynamically_Tagged_Expression
3863 Apply_Scalar_Range_Check (E, T);
3864 Apply_Static_Length_Check (E, T);
3866 if Nkind (Original_Node (N)) = N_Object_Declaration
3867 and then Comes_From_Source (Original_Node (N))
3869 -- Only call test if needed
3871 and then Restriction_Check_Required (SPARK_05)
3872 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
3874 Check_SPARK_05_Restriction
3875 ("initialization expression is not appropriate", E);
3878 -- A formal parameter of a specific tagged type whose related
3879 -- subprogram is subject to pragma Extensions_Visible with value
3880 -- "False" cannot be implicitly converted to a class-wide type by
3881 -- means of an initialization expression (SPARK RM 6.1.7(3)).
3883 if Is_Class_Wide_Type (T) and then Is_EVF_Expression (E) then
3885 ("formal parameter with Extensions_Visible False cannot be "
3886 & "implicitly converted to class-wide type", E);
3890 -- If the No_Streams restriction is set, check that the type of the
3891 -- object is not, and does not contain, any subtype derived from
3892 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
3893 -- Has_Stream just for efficiency reasons. There is no point in
3894 -- spending time on a Has_Stream check if the restriction is not set.
3896 if Restriction_Check_Required (No_Streams) then
3897 if Has_Stream (T) then
3898 Check_Restriction (No_Streams, N);
3902 -- Deal with predicate check before we start to do major rewriting. It
3903 -- is OK to initialize and then check the initialized value, since the
3904 -- object goes out of scope if we get a predicate failure. Note that we
3905 -- do this in the analyzer and not the expander because the analyzer
3906 -- does some substantial rewriting in some cases.
3908 -- We need a predicate check if the type has predicates, and if either
3909 -- there is an initializing expression, or for default initialization
3910 -- when we have at least one case of an explicit default initial value
3911 -- and then this is not an internal declaration whose initialization
3912 -- comes later (as for an aggregate expansion).
3914 if not Suppress_Assignment_Checks (N)
3915 and then Present (Predicate_Function (T))
3916 and then not No_Initialization (N)
3920 Is_Partially_Initialized_Type (T, Include_Implicit => False))
3922 -- If the type has a static predicate and the expression is known at
3923 -- compile time, see if the expression satisfies the predicate.
3926 Check_Expression_Against_Static_Predicate (E, T);
3930 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc)));
3933 -- Case of unconstrained type
3935 if Is_Indefinite_Subtype (T) then
3937 -- In SPARK, a declaration of unconstrained type is allowed
3938 -- only for constants of type string.
3940 if Is_String_Type (T) and then not Constant_Present (N) then
3941 Check_SPARK_05_Restriction
3942 ("declaration of object of unconstrained type not allowed", N);
3945 -- Nothing to do in deferred constant case
3947 if Constant_Present (N) and then No (E) then
3950 -- Case of no initialization present
3953 if No_Initialization (N) then
3956 elsif Is_Class_Wide_Type (T) then
3958 ("initialization required in class-wide declaration ", N);
3962 ("unconstrained subtype not allowed (need initialization)",
3963 Object_Definition (N));
3965 if Is_Record_Type (T) and then Has_Discriminants (T) then
3967 ("\provide initial value or explicit discriminant values",
3968 Object_Definition (N));
3971 ("\or give default discriminant values for type&",
3972 Object_Definition (N), T);
3974 elsif Is_Array_Type (T) then
3976 ("\provide initial value or explicit array bounds",
3977 Object_Definition (N));
3981 -- Case of initialization present but in error. Set initial
3982 -- expression as absent (but do not make above complaints)
3984 elsif E = Error then
3985 Set_Expression (N, Empty);
3988 -- Case of initialization present
3991 -- Check restrictions in Ada 83
3993 if not Constant_Present (N) then
3995 -- Unconstrained variables not allowed in Ada 83 mode
3997 if Ada_Version = Ada_83
3998 and then Comes_From_Source (Object_Definition (N))
4001 ("(Ada 83) unconstrained variable not allowed",
4002 Object_Definition (N));
4006 -- Now we constrain the variable from the initializing expression
4008 -- If the expression is an aggregate, it has been expanded into
4009 -- individual assignments. Retrieve the actual type from the
4010 -- expanded construct.
4012 if Is_Array_Type (T)
4013 and then No_Initialization (N)
4014 and then Nkind (Original_Node (E)) = N_Aggregate
4018 -- In case of class-wide interface object declarations we delay
4019 -- the generation of the equivalent record type declarations until
4020 -- its expansion because there are cases in they are not required.
4022 elsif Is_Interface (T) then
4025 -- In GNATprove mode, Expand_Subtype_From_Expr does nothing. Thus,
4026 -- we should prevent the generation of another Itype with the
4027 -- same name as the one already generated, or we end up with
4028 -- two identical types in GNATprove.
4030 elsif GNATprove_Mode then
4033 -- If the type is an unchecked union, no subtype can be built from
4034 -- the expression. Rewrite declaration as a renaming, which the
4035 -- back-end can handle properly. This is a rather unusual case,
4036 -- because most unchecked_union declarations have default values
4037 -- for discriminants and are thus not indefinite.
4039 elsif Is_Unchecked_Union (T) then
4040 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4041 Set_Ekind (Id, E_Constant);
4043 Set_Ekind (Id, E_Variable);
4046 -- An object declared within a Ghost region is automatically
4047 -- Ghost (SPARK RM 6.9(2)).
4049 if Comes_From_Source (Id) and then Ghost_Mode > None then
4050 Set_Is_Ghost_Entity (Id);
4052 -- The Ghost policy in effect at the point of declaration
4053 -- and at the point of completion must match
4054 -- (SPARK RM 6.9(15)).
4056 if Present (Prev_Entity)
4057 and then Is_Ghost_Entity (Prev_Entity)
4059 Check_Ghost_Completion (Prev_Entity, Id);
4064 Make_Object_Renaming_Declaration (Loc,
4065 Defining_Identifier => Id,
4066 Subtype_Mark => New_Occurrence_Of (T, Loc),
4069 Set_Renamed_Object (Id, E);
4070 Freeze_Before (N, T);
4075 Expand_Subtype_From_Expr (N, T, Object_Definition (N), E);
4076 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4079 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4081 if Aliased_Present (N) then
4082 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4085 Freeze_Before (N, Act_T);
4086 Freeze_Before (N, T);
4089 elsif Is_Array_Type (T)
4090 and then No_Initialization (N)
4091 and then Nkind (Original_Node (E)) = N_Aggregate
4093 if not Is_Entity_Name (Object_Definition (N)) then
4095 Check_Compile_Time_Size (Act_T);
4097 if Aliased_Present (N) then
4098 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4102 -- When the given object definition and the aggregate are specified
4103 -- independently, and their lengths might differ do a length check.
4104 -- This cannot happen if the aggregate is of the form (others =>...)
4106 if not Is_Constrained (T) then
4109 elsif Nkind (E) = N_Raise_Constraint_Error then
4111 -- Aggregate is statically illegal. Place back in declaration
4113 Set_Expression (N, E);
4114 Set_No_Initialization (N, False);
4116 elsif T = Etype (E) then
4119 elsif Nkind (E) = N_Aggregate
4120 and then Present (Component_Associations (E))
4121 and then Present (Choices (First (Component_Associations (E))))
4122 and then Nkind (First
4123 (Choices (First (Component_Associations (E))))) = N_Others_Choice
4128 Apply_Length_Check (E, T);
4131 -- If the type is limited unconstrained with defaulted discriminants and
4132 -- there is no expression, then the object is constrained by the
4133 -- defaults, so it is worthwhile building the corresponding subtype.
4135 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4136 and then not Is_Constrained (T)
4137 and then Has_Discriminants (T)
4140 Act_T := Build_Default_Subtype (T, N);
4142 -- Ada 2005: A limited object may be initialized by means of an
4143 -- aggregate. If the type has default discriminants it has an
4144 -- unconstrained nominal type, Its actual subtype will be obtained
4145 -- from the aggregate, and not from the default discriminants.
4150 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4152 elsif Nkind (E) = N_Function_Call
4153 and then Constant_Present (N)
4154 and then Has_Unconstrained_Elements (Etype (E))
4156 -- The back-end has problems with constants of a discriminated type
4157 -- with defaults, if the initial value is a function call. We
4158 -- generate an intermediate temporary that will receive a reference
4159 -- to the result of the call. The initialization expression then
4160 -- becomes a dereference of that temporary.
4162 Remove_Side_Effects (E);
4164 -- If this is a constant declaration of an unconstrained type and
4165 -- the initialization is an aggregate, we can use the subtype of the
4166 -- aggregate for the declared entity because it is immutable.
4168 elsif not Is_Constrained (T)
4169 and then Has_Discriminants (T)
4170 and then Constant_Present (N)
4171 and then not Has_Unchecked_Union (T)
4172 and then Nkind (E) = N_Aggregate
4177 -- Check No_Wide_Characters restriction
4179 Check_Wide_Character_Restriction (T, Object_Definition (N));
4181 -- Indicate this is not set in source. Certainly true for constants, and
4182 -- true for variables so far (will be reset for a variable if and when
4183 -- we encounter a modification in the source).
4185 Set_Never_Set_In_Source (Id);
4187 -- Now establish the proper kind and type of the object
4189 if Constant_Present (N) then
4190 Set_Ekind (Id, E_Constant);
4191 Set_Is_True_Constant (Id);
4194 Set_Ekind (Id, E_Variable);
4196 -- A variable is set as shared passive if it appears in a shared
4197 -- passive package, and is at the outer level. This is not done for
4198 -- entities generated during expansion, because those are always
4199 -- manipulated locally.
4201 if Is_Shared_Passive (Current_Scope)
4202 and then Is_Library_Level_Entity (Id)
4203 and then Comes_From_Source (Id)
4205 Set_Is_Shared_Passive (Id);
4206 Check_Shared_Var (Id, T, N);
4209 -- Set Has_Initial_Value if initializing expression present. Note
4210 -- that if there is no initializing expression, we leave the state
4211 -- of this flag unchanged (usually it will be False, but notably in
4212 -- the case of exception choice variables, it will already be true).
4215 Set_Has_Initial_Value (Id);
4219 -- Initialize alignment and size and capture alignment setting
4221 Init_Alignment (Id);
4223 Set_Optimize_Alignment_Flags (Id);
4225 -- An object declared within a Ghost region is automatically Ghost
4226 -- (SPARK RM 6.9(2)).
4228 if Comes_From_Source (Id)
4229 and then (Ghost_Mode > None
4230 or else (Present (Prev_Entity)
4231 and then Is_Ghost_Entity (Prev_Entity)))
4233 Set_Is_Ghost_Entity (Id);
4235 -- The Ghost policy in effect at the point of declaration and at the
4236 -- point of completion must match (SPARK RM 6.9(16)).
4238 if Present (Prev_Entity) and then Is_Ghost_Entity (Prev_Entity) then
4239 Check_Ghost_Completion (Prev_Entity, Id);
4243 -- Deal with aliased case
4245 if Aliased_Present (N) then
4246 Set_Is_Aliased (Id);
4248 -- If the object is aliased and the type is unconstrained with
4249 -- defaulted discriminants and there is no expression, then the
4250 -- object is constrained by the defaults, so it is worthwhile
4251 -- building the corresponding subtype.
4253 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4254 -- unconstrained, then only establish an actual subtype if the
4255 -- nominal subtype is indefinite. In definite cases the object is
4256 -- unconstrained in Ada 2005.
4259 and then Is_Record_Type (T)
4260 and then not Is_Constrained (T)
4261 and then Has_Discriminants (T)
4262 and then (Ada_Version < Ada_2005 or else Is_Indefinite_Subtype (T))
4264 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4268 -- Now we can set the type of the object
4270 Set_Etype (Id, Act_T);
4272 -- Non-constant object is marked to be treated as volatile if type is
4273 -- volatile and we clear the Current_Value setting that may have been
4274 -- set above. Doing so for constants isn't required and might interfere
4275 -- with possible uses of the object as a static expression in contexts
4276 -- incompatible with volatility (e.g. as a case-statement alternative).
4278 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4279 Set_Treat_As_Volatile (Id);
4280 Set_Current_Value (Id, Empty);
4283 -- Deal with controlled types
4285 if Has_Controlled_Component (Etype (Id))
4286 or else Is_Controlled (Etype (Id))
4288 if not Is_Library_Level_Entity (Id) then
4289 Check_Restriction (No_Nested_Finalization, N);
4291 Validate_Controlled_Object (Id);
4295 if Has_Task (Etype (Id)) then
4296 Check_Restriction (No_Tasking, N);
4298 -- Deal with counting max tasks
4300 -- Nothing to do if inside a generic
4302 if Inside_A_Generic then
4305 -- If library level entity, then count tasks
4307 elsif Is_Library_Level_Entity (Id) then
4308 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4310 -- If not library level entity, then indicate we don't know max
4311 -- tasks and also check task hierarchy restriction and blocking
4312 -- operation (since starting a task is definitely blocking).
4315 Check_Restriction (Max_Tasks, N);
4316 Check_Restriction (No_Task_Hierarchy, N);
4317 Check_Potentially_Blocking_Operation (N);
4320 -- A rather specialized test. If we see two tasks being declared
4321 -- of the same type in the same object declaration, and the task
4322 -- has an entry with an address clause, we know that program error
4323 -- will be raised at run time since we can't have two tasks with
4324 -- entries at the same address.
4326 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4331 E := First_Entity (Etype (Id));
4332 while Present (E) loop
4333 if Ekind (E) = E_Entry
4334 and then Present (Get_Attribute_Definition_Clause
4335 (E, Attribute_Address))
4337 Error_Msg_Warn := SPARK_Mode /= On;
4339 ("more than one task with same entry address<<", N);
4340 Error_Msg_N ("\Program_Error [<<", N);
4342 Make_Raise_Program_Error (Loc,
4343 Reason => PE_Duplicated_Entry_Address));
4353 -- Some simple constant-propagation: if the expression is a constant
4354 -- string initialized with a literal, share the literal. This avoids
4358 and then Is_Entity_Name (E)
4359 and then Ekind (Entity (E)) = E_Constant
4360 and then Base_Type (Etype (E)) = Standard_String
4363 Val : constant Node_Id := Constant_Value (Entity (E));
4365 if Present (Val) and then Nkind (Val) = N_String_Literal then
4366 Rewrite (E, New_Copy (Val));
4371 -- Another optimization: if the nominal subtype is unconstrained and
4372 -- the expression is a function call that returns an unconstrained
4373 -- type, rewrite the declaration as a renaming of the result of the
4374 -- call. The exceptions below are cases where the copy is expected,
4375 -- either by the back end (Aliased case) or by the semantics, as for
4376 -- initializing controlled types or copying tags for classwide types.
4379 and then Nkind (E) = N_Explicit_Dereference
4380 and then Nkind (Original_Node (E)) = N_Function_Call
4381 and then not Is_Library_Level_Entity (Id)
4382 and then not Is_Constrained (Underlying_Type (T))
4383 and then not Is_Aliased (Id)
4384 and then not Is_Class_Wide_Type (T)
4385 and then not Is_Controlled (T)
4386 and then not Has_Controlled_Component (Base_Type (T))
4387 and then Expander_Active
4390 Make_Object_Renaming_Declaration (Loc,
4391 Defining_Identifier => Id,
4392 Access_Definition => Empty,
4393 Subtype_Mark => New_Occurrence_Of
4394 (Base_Type (Etype (Id)), Loc),
4397 Set_Renamed_Object (Id, E);
4399 -- Force generation of debugging information for the constant and for
4400 -- the renamed function call.
4402 Set_Debug_Info_Needed (Id);
4403 Set_Debug_Info_Needed (Entity (Prefix (E)));
4406 if Present (Prev_Entity)
4407 and then Is_Frozen (Prev_Entity)
4408 and then not Error_Posted (Id)
4410 Error_Msg_N ("full constant declaration appears too late", N);
4413 Check_Eliminated (Id);
4415 -- Deal with setting In_Private_Part flag if in private part
4417 if Ekind (Scope (Id)) = E_Package and then In_Private_Part (Scope (Id))
4419 Set_In_Private_Part (Id);
4422 -- Check for violation of No_Local_Timing_Events
4424 if Restriction_Check_Required (No_Local_Timing_Events)
4425 and then not Is_Library_Level_Entity (Id)
4426 and then Is_RTE (Etype (Id), RE_Timing_Event)
4428 Check_Restriction (No_Local_Timing_Events, N);
4432 -- Initialize the refined state of a variable here because this is a
4433 -- common destination for legal and illegal object declarations.
4435 if Ekind (Id) = E_Variable then
4436 Set_Encapsulating_State (Id, Empty);
4439 if Has_Aspects (N) then
4440 Analyze_Aspect_Specifications (N, Id);
4443 Analyze_Dimension (N);
4445 -- Verify whether the object declaration introduces an illegal hidden
4446 -- state within a package subject to a null abstract state.
4448 if Ekind (Id) = E_Variable then
4449 Check_No_Hidden_State (Id);
4451 end Analyze_Object_Declaration;
4453 ---------------------------
4454 -- Analyze_Others_Choice --
4455 ---------------------------
4457 -- Nothing to do for the others choice node itself, the semantic analysis
4458 -- of the others choice will occur as part of the processing of the parent
4460 procedure Analyze_Others_Choice (N : Node_Id) is
4461 pragma Warnings (Off, N);
4464 end Analyze_Others_Choice;
4466 -------------------------------------------
4467 -- Analyze_Private_Extension_Declaration --
4468 -------------------------------------------
4470 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
4471 T : constant Entity_Id := Defining_Identifier (N);
4472 Indic : constant Node_Id := Subtype_Indication (N);
4473 Parent_Type : Entity_Id;
4474 Parent_Base : Entity_Id;
4477 -- The private extension declaration may be subject to pragma Ghost with
4478 -- policy Ignore. Set the mode now to ensure that any nodes generated
4479 -- during analysis and expansion are properly flagged as ignored Ghost.
4483 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
4485 if Is_Non_Empty_List (Interface_List (N)) then
4491 Intf := First (Interface_List (N));
4492 while Present (Intf) loop
4493 T := Find_Type_Of_Subtype_Indic (Intf);
4495 Diagnose_Interface (Intf, T);
4501 Generate_Definition (T);
4503 -- For other than Ada 2012, just enter the name in the current scope
4505 if Ada_Version < Ada_2012 then
4508 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
4509 -- case of private type that completes an incomplete type.
4516 Prev := Find_Type_Name (N);
4518 pragma Assert (Prev = T
4519 or else (Ekind (Prev) = E_Incomplete_Type
4520 and then Present (Full_View (Prev))
4521 and then Full_View (Prev) = T));
4525 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
4526 Parent_Base := Base_Type (Parent_Type);
4528 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
4529 Set_Ekind (T, Ekind (Parent_Type));
4530 Set_Etype (T, Any_Type);
4533 elsif not Is_Tagged_Type (Parent_Type) then
4535 ("parent of type extension must be a tagged type ", Indic);
4538 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
4539 Error_Msg_N ("premature derivation of incomplete type", Indic);
4542 elsif Is_Concurrent_Type (Parent_Type) then
4544 ("parent type of a private extension cannot be "
4545 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
4547 Set_Etype (T, Any_Type);
4548 Set_Ekind (T, E_Limited_Private_Type);
4549 Set_Private_Dependents (T, New_Elmt_List);
4550 Set_Error_Posted (T);
4554 -- Perhaps the parent type should be changed to the class-wide type's
4555 -- specific type in this case to prevent cascading errors ???
4557 if Is_Class_Wide_Type (Parent_Type) then
4559 ("parent of type extension must not be a class-wide type", Indic);
4563 if (not Is_Package_Or_Generic_Package (Current_Scope)
4564 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
4565 or else In_Private_Part (Current_Scope)
4568 Error_Msg_N ("invalid context for private extension", N);
4571 -- Set common attributes
4573 Set_Is_Pure (T, Is_Pure (Current_Scope));
4574 Set_Scope (T, Current_Scope);
4575 Set_Ekind (T, E_Record_Type_With_Private);
4576 Init_Size_Align (T);
4577 Set_Default_SSO (T);
4579 Set_Etype (T, Parent_Base);
4580 Set_Has_Task (T, Has_Task (Parent_Base));
4581 Set_Has_Protected (T, Has_Task (Parent_Base));
4583 Set_Convention (T, Convention (Parent_Type));
4584 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
4585 Set_Is_First_Subtype (T);
4586 Make_Class_Wide_Type (T);
4588 if Unknown_Discriminants_Present (N) then
4589 Set_Discriminant_Constraint (T, No_Elist);
4592 Build_Derived_Record_Type (N, Parent_Type, T);
4594 -- Propagate inherited invariant information. The new type has
4595 -- invariants, if the parent type has inheritable invariants,
4596 -- and these invariants can in turn be inherited.
4598 if Has_Inheritable_Invariants (Parent_Type) then
4599 Set_Has_Inheritable_Invariants (T);
4600 Set_Has_Invariants (T);
4603 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
4604 -- synchronized formal derived type.
4606 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
4607 Set_Is_Limited_Record (T);
4609 -- Formal derived type case
4611 if Is_Generic_Type (T) then
4613 -- The parent must be a tagged limited type or a synchronized
4616 if (not Is_Tagged_Type (Parent_Type)
4617 or else not Is_Limited_Type (Parent_Type))
4619 (not Is_Interface (Parent_Type)
4620 or else not Is_Synchronized_Interface (Parent_Type))
4622 Error_Msg_NE ("parent type of & must be tagged limited " &
4623 "or synchronized", N, T);
4626 -- The progenitors (if any) must be limited or synchronized
4629 if Present (Interfaces (T)) then
4632 Iface_Elmt : Elmt_Id;
4635 Iface_Elmt := First_Elmt (Interfaces (T));
4636 while Present (Iface_Elmt) loop
4637 Iface := Node (Iface_Elmt);
4639 if not Is_Limited_Interface (Iface)
4640 and then not Is_Synchronized_Interface (Iface)
4642 Error_Msg_NE ("progenitor & must be limited " &
4643 "or synchronized", N, Iface);
4646 Next_Elmt (Iface_Elmt);
4651 -- Regular derived extension, the parent must be a limited or
4652 -- synchronized interface.
4655 if not Is_Interface (Parent_Type)
4656 or else (not Is_Limited_Interface (Parent_Type)
4657 and then not Is_Synchronized_Interface (Parent_Type))
4660 ("parent type of & must be limited interface", N, T);
4664 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
4665 -- extension with a synchronized parent must be explicitly declared
4666 -- synchronized, because the full view will be a synchronized type.
4667 -- This must be checked before the check for limited types below,
4668 -- to ensure that types declared limited are not allowed to extend
4669 -- synchronized interfaces.
4671 elsif Is_Interface (Parent_Type)
4672 and then Is_Synchronized_Interface (Parent_Type)
4673 and then not Synchronized_Present (N)
4676 ("private extension of& must be explicitly synchronized",
4679 elsif Limited_Present (N) then
4680 Set_Is_Limited_Record (T);
4682 if not Is_Limited_Type (Parent_Type)
4684 (not Is_Interface (Parent_Type)
4685 or else not Is_Limited_Interface (Parent_Type))
4687 Error_Msg_NE ("parent type& of limited extension must be limited",
4693 if Has_Aspects (N) then
4694 Analyze_Aspect_Specifications (N, T);
4696 end Analyze_Private_Extension_Declaration;
4698 ---------------------------------
4699 -- Analyze_Subtype_Declaration --
4700 ---------------------------------
4702 procedure Analyze_Subtype_Declaration
4704 Skip : Boolean := False)
4706 Id : constant Entity_Id := Defining_Identifier (N);
4708 R_Checks : Check_Result;
4711 -- The subtype declaration may be subject to pragma Ghost with policy
4712 -- Ignore. Set the mode now to ensure that any nodes generated during
4713 -- analysis and expansion are properly flagged as ignored Ghost.
4717 Generate_Definition (Id);
4718 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4719 Init_Size_Align (Id);
4721 -- The following guard condition on Enter_Name is to handle cases where
4722 -- the defining identifier has already been entered into the scope but
4723 -- the declaration as a whole needs to be analyzed.
4725 -- This case in particular happens for derived enumeration types. The
4726 -- derived enumeration type is processed as an inserted enumeration type
4727 -- declaration followed by a rewritten subtype declaration. The defining
4728 -- identifier, however, is entered into the name scope very early in the
4729 -- processing of the original type declaration and therefore needs to be
4730 -- avoided here, when the created subtype declaration is analyzed. (See
4731 -- Build_Derived_Types)
4733 -- This also happens when the full view of a private type is derived
4734 -- type with constraints. In this case the entity has been introduced
4735 -- in the private declaration.
4737 -- Finally this happens in some complex cases when validity checks are
4738 -- enabled, where the same subtype declaration may be analyzed twice.
4739 -- This can happen if the subtype is created by the pre-analysis of
4740 -- an attribute tht gives the range of a loop statement, and the loop
4741 -- itself appears within an if_statement that will be rewritten during
4745 or else (Present (Etype (Id))
4746 and then (Is_Private_Type (Etype (Id))
4747 or else Is_Task_Type (Etype (Id))
4748 or else Is_Rewrite_Substitution (N)))
4752 elsif Current_Entity (Id) = Id then
4759 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
4761 -- Class-wide equivalent types of records with unknown discriminants
4762 -- involve the generation of an itype which serves as the private view
4763 -- of a constrained record subtype. In such cases the base type of the
4764 -- current subtype we are processing is the private itype. Use the full
4765 -- of the private itype when decorating various attributes.
4768 and then Is_Private_Type (T)
4769 and then Present (Full_View (T))
4774 -- Inherit common attributes
4776 Set_Is_Volatile (Id, Is_Volatile (T));
4777 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
4778 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
4779 Set_Convention (Id, Convention (T));
4781 -- If ancestor has predicates then so does the subtype, and in addition
4782 -- we must delay the freeze to properly arrange predicate inheritance.
4784 -- The Ancestor_Type test is really unpleasant, there seem to be cases
4785 -- in which T = ID, so the above tests and assignments do nothing???
4787 if Has_Predicates (T)
4788 or else (Present (Ancestor_Subtype (T))
4789 and then Has_Predicates (Ancestor_Subtype (T)))
4791 Set_Has_Predicates (Id);
4792 Set_Has_Delayed_Freeze (Id);
4795 -- Subtype of Boolean cannot have a constraint in SPARK
4797 if Is_Boolean_Type (T)
4798 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
4800 Check_SPARK_05_Restriction
4801 ("subtype of Boolean cannot have constraint", N);
4804 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
4806 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
4812 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
4813 One_Cstr := First (Constraints (Cstr));
4814 while Present (One_Cstr) loop
4816 -- Index or discriminant constraint in SPARK must be a
4820 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
4822 Check_SPARK_05_Restriction
4823 ("subtype mark required", One_Cstr);
4825 -- String subtype must have a lower bound of 1 in SPARK.
4826 -- Note that we do not need to test for the non-static case
4827 -- here, since that was already taken care of in
4828 -- Process_Range_Expr_In_Decl.
4830 elsif Base_Type (T) = Standard_String then
4831 Get_Index_Bounds (One_Cstr, Low, High);
4833 if Is_OK_Static_Expression (Low)
4834 and then Expr_Value (Low) /= 1
4836 Check_SPARK_05_Restriction
4837 ("String subtype must have lower bound of 1", N);
4847 -- In the case where there is no constraint given in the subtype
4848 -- indication, Process_Subtype just returns the Subtype_Mark, so its
4849 -- semantic attributes must be established here.
4851 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
4852 Set_Etype (Id, Base_Type (T));
4854 -- Subtype of unconstrained array without constraint is not allowed
4857 if Is_Array_Type (T) and then not Is_Constrained (T) then
4858 Check_SPARK_05_Restriction
4859 ("subtype of unconstrained array must have constraint", N);
4864 Set_Ekind (Id, E_Array_Subtype);
4865 Copy_Array_Subtype_Attributes (Id, T);
4867 when Decimal_Fixed_Point_Kind =>
4868 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
4869 Set_Digits_Value (Id, Digits_Value (T));
4870 Set_Delta_Value (Id, Delta_Value (T));
4871 Set_Scale_Value (Id, Scale_Value (T));
4872 Set_Small_Value (Id, Small_Value (T));
4873 Set_Scalar_Range (Id, Scalar_Range (T));
4874 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
4875 Set_Is_Constrained (Id, Is_Constrained (T));
4876 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4877 Set_RM_Size (Id, RM_Size (T));
4879 when Enumeration_Kind =>
4880 Set_Ekind (Id, E_Enumeration_Subtype);
4881 Set_First_Literal (Id, First_Literal (Base_Type (T)));
4882 Set_Scalar_Range (Id, Scalar_Range (T));
4883 Set_Is_Character_Type (Id, Is_Character_Type (T));
4884 Set_Is_Constrained (Id, Is_Constrained (T));
4885 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4886 Set_RM_Size (Id, RM_Size (T));
4887 Inherit_Predicate_Flags (Id, T);
4889 when Ordinary_Fixed_Point_Kind =>
4890 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
4891 Set_Scalar_Range (Id, Scalar_Range (T));
4892 Set_Small_Value (Id, Small_Value (T));
4893 Set_Delta_Value (Id, Delta_Value (T));
4894 Set_Is_Constrained (Id, Is_Constrained (T));
4895 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4896 Set_RM_Size (Id, RM_Size (T));
4899 Set_Ekind (Id, E_Floating_Point_Subtype);
4900 Set_Scalar_Range (Id, Scalar_Range (T));
4901 Set_Digits_Value (Id, Digits_Value (T));
4902 Set_Is_Constrained (Id, Is_Constrained (T));
4904 when Signed_Integer_Kind =>
4905 Set_Ekind (Id, E_Signed_Integer_Subtype);
4906 Set_Scalar_Range (Id, Scalar_Range (T));
4907 Set_Is_Constrained (Id, Is_Constrained (T));
4908 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4909 Set_RM_Size (Id, RM_Size (T));
4910 Inherit_Predicate_Flags (Id, T);
4912 when Modular_Integer_Kind =>
4913 Set_Ekind (Id, E_Modular_Integer_Subtype);
4914 Set_Scalar_Range (Id, Scalar_Range (T));
4915 Set_Is_Constrained (Id, Is_Constrained (T));
4916 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
4917 Set_RM_Size (Id, RM_Size (T));
4918 Inherit_Predicate_Flags (Id, T);
4920 when Class_Wide_Kind =>
4921 Set_Ekind (Id, E_Class_Wide_Subtype);
4922 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4923 Set_Cloned_Subtype (Id, T);
4924 Set_Is_Tagged_Type (Id, True);
4925 Set_Has_Unknown_Discriminants
4927 Set_No_Tagged_Streams_Pragma
4928 (Id, No_Tagged_Streams_Pragma (T));
4930 if Ekind (T) = E_Class_Wide_Subtype then
4931 Set_Equivalent_Type (Id, Equivalent_Type (T));
4934 when E_Record_Type | E_Record_Subtype =>
4935 Set_Ekind (Id, E_Record_Subtype);
4937 if Ekind (T) = E_Record_Subtype
4938 and then Present (Cloned_Subtype (T))
4940 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
4942 Set_Cloned_Subtype (Id, T);
4945 Set_First_Entity (Id, First_Entity (T));
4946 Set_Last_Entity (Id, Last_Entity (T));
4947 Set_Has_Discriminants (Id, Has_Discriminants (T));
4948 Set_Is_Constrained (Id, Is_Constrained (T));
4949 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4950 Set_Has_Implicit_Dereference
4951 (Id, Has_Implicit_Dereference (T));
4952 Set_Has_Unknown_Discriminants
4953 (Id, Has_Unknown_Discriminants (T));
4955 if Has_Discriminants (T) then
4956 Set_Discriminant_Constraint
4957 (Id, Discriminant_Constraint (T));
4958 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
4960 elsif Has_Unknown_Discriminants (Id) then
4961 Set_Discriminant_Constraint (Id, No_Elist);
4964 if Is_Tagged_Type (T) then
4965 Set_Is_Tagged_Type (Id, True);
4966 Set_No_Tagged_Streams_Pragma
4967 (Id, No_Tagged_Streams_Pragma (T));
4968 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4969 Set_Direct_Primitive_Operations
4970 (Id, Direct_Primitive_Operations (T));
4971 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
4973 if Is_Interface (T) then
4974 Set_Is_Interface (Id);
4975 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
4979 when Private_Kind =>
4980 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
4981 Set_Has_Discriminants (Id, Has_Discriminants (T));
4982 Set_Is_Constrained (Id, Is_Constrained (T));
4983 Set_First_Entity (Id, First_Entity (T));
4984 Set_Last_Entity (Id, Last_Entity (T));
4985 Set_Private_Dependents (Id, New_Elmt_List);
4986 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
4987 Set_Has_Implicit_Dereference
4988 (Id, Has_Implicit_Dereference (T));
4989 Set_Has_Unknown_Discriminants
4990 (Id, Has_Unknown_Discriminants (T));
4991 Set_Known_To_Have_Preelab_Init
4992 (Id, Known_To_Have_Preelab_Init (T));
4994 if Is_Tagged_Type (T) then
4995 Set_Is_Tagged_Type (Id);
4996 Set_No_Tagged_Streams_Pragma (Id,
4997 No_Tagged_Streams_Pragma (T));
4998 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
4999 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5000 Set_Direct_Primitive_Operations (Id,
5001 Direct_Primitive_Operations (T));
5004 -- In general the attributes of the subtype of a private type
5005 -- are the attributes of the partial view of parent. However,
5006 -- the full view may be a discriminated type, and the subtype
5007 -- must share the discriminant constraint to generate correct
5008 -- calls to initialization procedures.
5010 if Has_Discriminants (T) then
5011 Set_Discriminant_Constraint
5012 (Id, Discriminant_Constraint (T));
5013 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5015 elsif Present (Full_View (T))
5016 and then Has_Discriminants (Full_View (T))
5018 Set_Discriminant_Constraint
5019 (Id, Discriminant_Constraint (Full_View (T)));
5020 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5022 -- This would seem semantically correct, but apparently
5023 -- generates spurious errors about missing components ???
5025 -- Set_Has_Discriminants (Id);
5028 Prepare_Private_Subtype_Completion (Id, N);
5030 -- If this is the subtype of a constrained private type with
5031 -- discriminants that has got a full view and we also have
5032 -- built a completion just above, show that the completion
5033 -- is a clone of the full view to the back-end.
5035 if Has_Discriminants (T)
5036 and then not Has_Unknown_Discriminants (T)
5037 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5038 and then Present (Full_View (T))
5039 and then Present (Full_View (Id))
5041 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5045 Set_Ekind (Id, E_Access_Subtype);
5046 Set_Is_Constrained (Id, Is_Constrained (T));
5047 Set_Is_Access_Constant
5048 (Id, Is_Access_Constant (T));
5049 Set_Directly_Designated_Type
5050 (Id, Designated_Type (T));
5051 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5053 -- A Pure library_item must not contain the declaration of a
5054 -- named access type, except within a subprogram, generic
5055 -- subprogram, task unit, or protected unit, or if it has
5056 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5058 if Comes_From_Source (Id)
5059 and then In_Pure_Unit
5060 and then not In_Subprogram_Task_Protected_Unit
5061 and then not No_Pool_Assigned (Id)
5064 ("named access types not allowed in pure unit", N);
5067 when Concurrent_Kind =>
5068 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5069 Set_Corresponding_Record_Type (Id,
5070 Corresponding_Record_Type (T));
5071 Set_First_Entity (Id, First_Entity (T));
5072 Set_First_Private_Entity (Id, First_Private_Entity (T));
5073 Set_Has_Discriminants (Id, Has_Discriminants (T));
5074 Set_Is_Constrained (Id, Is_Constrained (T));
5075 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5076 Set_Last_Entity (Id, Last_Entity (T));
5078 if Is_Tagged_Type (T) then
5079 Set_No_Tagged_Streams_Pragma
5080 (Id, No_Tagged_Streams_Pragma (T));
5083 if Has_Discriminants (T) then
5084 Set_Discriminant_Constraint
5085 (Id, Discriminant_Constraint (T));
5086 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5089 when Incomplete_Kind =>
5090 if Ada_Version >= Ada_2005 then
5092 -- In Ada 2005 an incomplete type can be explicitly tagged:
5093 -- propagate indication. Note that we also have to include
5094 -- subtypes for Ada 2012 extended use of incomplete types.
5096 Set_Ekind (Id, E_Incomplete_Subtype);
5097 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5098 Set_Private_Dependents (Id, New_Elmt_List);
5100 if Is_Tagged_Type (Id) then
5101 Set_No_Tagged_Streams_Pragma
5102 (Id, No_Tagged_Streams_Pragma (T));
5103 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5106 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5107 -- incomplete type visible through a limited with clause.
5109 if From_Limited_With (T)
5110 and then Present (Non_Limited_View (T))
5112 Set_From_Limited_With (Id);
5113 Set_Non_Limited_View (Id, Non_Limited_View (T));
5115 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5116 -- to the private dependents of the original incomplete
5117 -- type for future transformation.
5120 Append_Elmt (Id, Private_Dependents (T));
5123 -- If the subtype name denotes an incomplete type an error
5124 -- was already reported by Process_Subtype.
5127 Set_Etype (Id, Any_Type);
5131 raise Program_Error;
5135 if Etype (Id) = Any_Type then
5139 -- Some common processing on all types
5141 Set_Size_Info (Id, T);
5142 Set_First_Rep_Item (Id, First_Rep_Item (T));
5144 -- If the parent type is a generic actual, so is the subtype. This may
5145 -- happen in a nested instance. Why Comes_From_Source test???
5147 if not Comes_From_Source (N) then
5148 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5153 Set_Is_Immediately_Visible (Id, True);
5154 Set_Depends_On_Private (Id, Has_Private_Component (T));
5155 Set_Is_Descendent_Of_Address (Id, Is_Descendent_Of_Address (T));
5157 if Is_Interface (T) then
5158 Set_Is_Interface (Id);
5161 if Present (Generic_Parent_Type (N))
5163 (Nkind (Parent (Generic_Parent_Type (N))) /=
5164 N_Formal_Type_Declaration
5165 or else Nkind (Formal_Type_Definition
5166 (Parent (Generic_Parent_Type (N)))) /=
5167 N_Formal_Private_Type_Definition)
5169 if Is_Tagged_Type (Id) then
5171 -- If this is a generic actual subtype for a synchronized type,
5172 -- the primitive operations are those of the corresponding record
5173 -- for which there is a separate subtype declaration.
5175 if Is_Concurrent_Type (Id) then
5177 elsif Is_Class_Wide_Type (Id) then
5178 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5180 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5183 elsif Scope (Etype (Id)) /= Standard_Standard then
5184 Derive_Subprograms (Generic_Parent_Type (N), Id);
5188 if Is_Private_Type (T) and then Present (Full_View (T)) then
5189 Conditional_Delay (Id, Full_View (T));
5191 -- The subtypes of components or subcomponents of protected types
5192 -- do not need freeze nodes, which would otherwise appear in the
5193 -- wrong scope (before the freeze node for the protected type). The
5194 -- proper subtypes are those of the subcomponents of the corresponding
5197 elsif Ekind (Scope (Id)) /= E_Protected_Type
5198 and then Present (Scope (Scope (Id))) -- error defense
5199 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5201 Conditional_Delay (Id, T);
5204 -- Check that Constraint_Error is raised for a scalar subtype indication
5205 -- when the lower or upper bound of a non-null range lies outside the
5206 -- range of the type mark.
5208 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5209 if Is_Scalar_Type (Etype (Id))
5210 and then Scalar_Range (Id) /=
5211 Scalar_Range (Etype (Subtype_Mark
5212 (Subtype_Indication (N))))
5216 Etype (Subtype_Mark (Subtype_Indication (N))));
5218 -- In the array case, check compatibility for each index
5220 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5222 -- This really should be a subprogram that finds the indications
5226 Subt_Index : Node_Id := First_Index (Id);
5227 Target_Index : Node_Id :=
5229 (Subtype_Mark (Subtype_Indication (N))));
5230 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5233 while Present (Subt_Index) loop
5234 if ((Nkind (Subt_Index) = N_Identifier
5235 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5236 or else Nkind (Subt_Index) = N_Subtype_Indication)
5238 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5241 Target_Typ : constant Entity_Id :=
5242 Etype (Target_Index);
5246 (Scalar_Range (Etype (Subt_Index)),
5249 Defining_Identifier (N));
5251 -- Reset Has_Dynamic_Range_Check on the subtype to
5252 -- prevent elision of the index check due to a dynamic
5253 -- check generated for a preceding index (needed since
5254 -- Insert_Range_Checks tries to avoid generating
5255 -- redundant checks on a given declaration).
5257 Set_Has_Dynamic_Range_Check (N, False);
5263 Sloc (Defining_Identifier (N)));
5265 -- Record whether this index involved a dynamic check
5268 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5272 Next_Index (Subt_Index);
5273 Next_Index (Target_Index);
5276 -- Finally, mark whether the subtype involves dynamic checks
5278 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5283 -- A type invariant applies to any subtype in its scope, in particular
5284 -- to a generic actual.
5286 if Has_Invariants (T) and then In_Open_Scopes (Scope (T)) then
5287 Set_Has_Invariants (Id);
5288 Set_Invariant_Procedure (Id, Invariant_Procedure (T));
5291 -- Make sure that generic actual types are properly frozen. The subtype
5292 -- is marked as a generic actual type when the enclosing instance is
5293 -- analyzed, so here we identify the subtype from the tree structure.
5296 and then Is_Generic_Actual_Type (Id)
5297 and then In_Instance
5298 and then not Comes_From_Source (N)
5299 and then Nkind (Subtype_Indication (N)) /= N_Subtype_Indication
5300 and then Is_Frozen (T)
5302 Freeze_Before (N, Id);
5305 Set_Optimize_Alignment_Flags (Id);
5306 Check_Eliminated (Id);
5309 if Has_Aspects (N) then
5310 Analyze_Aspect_Specifications (N, Id);
5313 Analyze_Dimension (N);
5314 end Analyze_Subtype_Declaration;
5316 --------------------------------
5317 -- Analyze_Subtype_Indication --
5318 --------------------------------
5320 procedure Analyze_Subtype_Indication (N : Node_Id) is
5321 T : constant Entity_Id := Subtype_Mark (N);
5322 R : constant Node_Id := Range_Expression (Constraint (N));
5329 Set_Etype (N, Etype (R));
5330 Resolve (R, Entity (T));
5332 Set_Error_Posted (R);
5333 Set_Error_Posted (T);
5335 end Analyze_Subtype_Indication;
5337 --------------------------
5338 -- Analyze_Variant_Part --
5339 --------------------------
5341 procedure Analyze_Variant_Part (N : Node_Id) is
5342 Discr_Name : Node_Id;
5343 Discr_Type : Entity_Id;
5345 procedure Process_Variant (A : Node_Id);
5346 -- Analyze declarations for a single variant
5348 package Analyze_Variant_Choices is
5349 new Generic_Analyze_Choices (Process_Variant);
5350 use Analyze_Variant_Choices;
5352 ---------------------
5353 -- Process_Variant --
5354 ---------------------
5356 procedure Process_Variant (A : Node_Id) is
5357 CL : constant Node_Id := Component_List (A);
5359 if not Null_Present (CL) then
5360 Analyze_Declarations (Component_Items (CL));
5362 if Present (Variant_Part (CL)) then
5363 Analyze (Variant_Part (CL));
5366 end Process_Variant;
5368 -- Start of processing for Analyze_Variant_Part
5371 Discr_Name := Name (N);
5372 Analyze (Discr_Name);
5374 -- If Discr_Name bad, get out (prevent cascaded errors)
5376 if Etype (Discr_Name) = Any_Type then
5380 -- Check invalid discriminant in variant part
5382 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
5383 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
5386 Discr_Type := Etype (Entity (Discr_Name));
5388 if not Is_Discrete_Type (Discr_Type) then
5390 ("discriminant in a variant part must be of a discrete type",
5395 -- Now analyze the choices, which also analyzes the declarations that
5396 -- are associated with each choice.
5398 Analyze_Choices (Variants (N), Discr_Type);
5400 -- Note: we used to instantiate and call Check_Choices here to check
5401 -- that the choices covered the discriminant, but it's too early to do
5402 -- that because of statically predicated subtypes, whose analysis may
5403 -- be deferred to their freeze point which may be as late as the freeze
5404 -- point of the containing record. So this call is now to be found in
5405 -- Freeze_Record_Declaration.
5407 end Analyze_Variant_Part;
5409 ----------------------------
5410 -- Array_Type_Declaration --
5411 ----------------------------
5413 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
5414 Component_Def : constant Node_Id := Component_Definition (Def);
5415 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
5416 Element_Type : Entity_Id;
5417 Implicit_Base : Entity_Id;
5419 Related_Id : Entity_Id := Empty;
5421 P : constant Node_Id := Parent (Def);
5425 if Nkind (Def) = N_Constrained_Array_Definition then
5426 Index := First (Discrete_Subtype_Definitions (Def));
5428 Index := First (Subtype_Marks (Def));
5431 -- Find proper names for the implicit types which may be public. In case
5432 -- of anonymous arrays we use the name of the first object of that type
5436 Related_Id := Defining_Identifier (P);
5442 while Present (Index) loop
5445 -- Test for odd case of trying to index a type by the type itself
5447 if Is_Entity_Name (Index) and then Entity (Index) = T then
5448 Error_Msg_N ("type& cannot be indexed by itself", Index);
5449 Set_Entity (Index, Standard_Boolean);
5450 Set_Etype (Index, Standard_Boolean);
5453 -- Check SPARK restriction requiring a subtype mark
5455 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
5456 Check_SPARK_05_Restriction ("subtype mark required", Index);
5459 -- Add a subtype declaration for each index of private array type
5460 -- declaration whose etype is also private. For example:
5463 -- type Index is private;
5465 -- type Table is array (Index) of ...
5468 -- This is currently required by the expander for the internally
5469 -- generated equality subprogram of records with variant parts in
5470 -- which the etype of some component is such private type.
5472 if Ekind (Current_Scope) = E_Package
5473 and then In_Private_Part (Current_Scope)
5474 and then Has_Private_Declaration (Etype (Index))
5477 Loc : constant Source_Ptr := Sloc (Def);
5482 New_E := Make_Temporary (Loc, 'T');
5483 Set_Is_Internal (New_E);
5486 Make_Subtype_Declaration (Loc,
5487 Defining_Identifier => New_E,
5488 Subtype_Indication =>
5489 New_Occurrence_Of (Etype (Index), Loc));
5491 Insert_Before (Parent (Def), Decl);
5493 Set_Etype (Index, New_E);
5495 -- If the index is a range the Entity attribute is not
5496 -- available. Example:
5499 -- type T is private;
5501 -- type T is new Natural;
5502 -- Table : array (T(1) .. T(10)) of Boolean;
5505 if Nkind (Index) /= N_Range then
5506 Set_Entity (Index, New_E);
5511 Make_Index (Index, P, Related_Id, Nb_Index);
5513 -- Check error of subtype with predicate for index type
5515 Bad_Predicated_Subtype_Use
5516 ("subtype& has predicate, not allowed as index subtype",
5517 Index, Etype (Index));
5519 -- Move to next index
5522 Nb_Index := Nb_Index + 1;
5525 -- Process subtype indication if one is present
5527 if Present (Component_Typ) then
5528 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
5530 Set_Etype (Component_Typ, Element_Type);
5532 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
5533 Check_SPARK_05_Restriction
5534 ("subtype mark required", Component_Typ);
5537 -- Ada 2005 (AI-230): Access Definition case
5539 else pragma Assert (Present (Access_Definition (Component_Def)));
5541 -- Indicate that the anonymous access type is created by the
5542 -- array type declaration.
5544 Element_Type := Access_Definition
5546 N => Access_Definition (Component_Def));
5547 Set_Is_Local_Anonymous_Access (Element_Type);
5549 -- Propagate the parent. This field is needed if we have to generate
5550 -- the master_id associated with an anonymous access to task type
5551 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
5553 Set_Parent (Element_Type, Parent (T));
5555 -- Ada 2005 (AI-230): In case of components that are anonymous access
5556 -- types the level of accessibility depends on the enclosing type
5559 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
5561 -- Ada 2005 (AI-254)
5564 CD : constant Node_Id :=
5565 Access_To_Subprogram_Definition
5566 (Access_Definition (Component_Def));
5568 if Present (CD) and then Protected_Present (CD) then
5570 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
5575 -- Constrained array case
5578 T := Create_Itype (E_Void, P, Related_Id, 'T');
5581 if Nkind (Def) = N_Constrained_Array_Definition then
5583 -- Establish Implicit_Base as unconstrained base type
5585 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
5587 Set_Etype (Implicit_Base, Implicit_Base);
5588 Set_Scope (Implicit_Base, Current_Scope);
5589 Set_Has_Delayed_Freeze (Implicit_Base);
5590 Set_Default_SSO (Implicit_Base);
5592 -- The constrained array type is a subtype of the unconstrained one
5594 Set_Ekind (T, E_Array_Subtype);
5595 Init_Size_Align (T);
5596 Set_Etype (T, Implicit_Base);
5597 Set_Scope (T, Current_Scope);
5598 Set_Is_Constrained (T);
5600 First (Discrete_Subtype_Definitions (Def)));
5601 Set_Has_Delayed_Freeze (T);
5603 -- Complete setup of implicit base type
5605 Set_First_Index (Implicit_Base, First_Index (T));
5606 Set_Component_Type (Implicit_Base, Element_Type);
5607 Set_Has_Task (Implicit_Base, Has_Task (Element_Type));
5608 Set_Has_Protected (Implicit_Base, Has_Protected (Element_Type));
5609 Set_Component_Size (Implicit_Base, Uint_0);
5610 Set_Packed_Array_Impl_Type (Implicit_Base, Empty);
5611 Set_Has_Controlled_Component (Implicit_Base,
5612 Has_Controlled_Component (Element_Type)
5613 or else Is_Controlled (Element_Type));
5614 Set_Finalize_Storage_Only (Implicit_Base,
5615 Finalize_Storage_Only (Element_Type));
5617 -- Inherit the "ghostness" from the constrained array type
5619 if Is_Ghost_Entity (T) or else Ghost_Mode > None then
5620 Set_Is_Ghost_Entity (Implicit_Base);
5623 -- Unconstrained array case
5626 Set_Ekind (T, E_Array_Type);
5627 Init_Size_Align (T);
5629 Set_Scope (T, Current_Scope);
5630 Set_Component_Size (T, Uint_0);
5631 Set_Is_Constrained (T, False);
5632 Set_First_Index (T, First (Subtype_Marks (Def)));
5633 Set_Has_Delayed_Freeze (T, True);
5634 Set_Has_Task (T, Has_Task (Element_Type));
5635 Set_Has_Protected (T, Has_Protected (Element_Type));
5636 Set_Has_Controlled_Component (T, Has_Controlled_Component
5639 Is_Controlled (Element_Type));
5640 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
5642 Set_Default_SSO (T);
5645 -- Common attributes for both cases
5647 Set_Component_Type (Base_Type (T), Element_Type);
5648 Set_Packed_Array_Impl_Type (T, Empty);
5650 if Aliased_Present (Component_Definition (Def)) then
5651 Check_SPARK_05_Restriction
5652 ("aliased is not allowed", Component_Definition (Def));
5653 Set_Has_Aliased_Components (Etype (T));
5656 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
5657 -- array type to ensure that objects of this type are initialized.
5659 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
5660 Set_Can_Never_Be_Null (T);
5662 if Null_Exclusion_Present (Component_Definition (Def))
5664 -- No need to check itypes because in their case this check was
5665 -- done at their point of creation
5667 and then not Is_Itype (Element_Type)
5670 ("`NOT NULL` not allowed (null already excluded)",
5671 Subtype_Indication (Component_Definition (Def)));
5675 Priv := Private_Component (Element_Type);
5677 if Present (Priv) then
5679 -- Check for circular definitions
5681 if Priv = Any_Type then
5682 Set_Component_Type (Etype (T), Any_Type);
5684 -- There is a gap in the visibility of operations on the composite
5685 -- type only if the component type is defined in a different scope.
5687 elsif Scope (Priv) = Current_Scope then
5690 elsif Is_Limited_Type (Priv) then
5691 Set_Is_Limited_Composite (Etype (T));
5692 Set_Is_Limited_Composite (T);
5694 Set_Is_Private_Composite (Etype (T));
5695 Set_Is_Private_Composite (T);
5699 -- A syntax error in the declaration itself may lead to an empty index
5700 -- list, in which case do a minimal patch.
5702 if No (First_Index (T)) then
5703 Error_Msg_N ("missing index definition in array type declaration", T);
5706 Indexes : constant List_Id :=
5707 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
5709 Set_Discrete_Subtype_Definitions (Def, Indexes);
5710 Set_First_Index (T, First (Indexes));
5715 -- Create a concatenation operator for the new type. Internal array
5716 -- types created for packed entities do not need such, they are
5717 -- compatible with the user-defined type.
5719 if Number_Dimensions (T) = 1
5720 and then not Is_Packed_Array_Impl_Type (T)
5722 New_Concatenation_Op (T);
5725 -- In the case of an unconstrained array the parser has already verified
5726 -- that all the indexes are unconstrained but we still need to make sure
5727 -- that the element type is constrained.
5729 if Is_Indefinite_Subtype (Element_Type) then
5731 ("unconstrained element type in array declaration",
5732 Subtype_Indication (Component_Def));
5734 elsif Is_Abstract_Type (Element_Type) then
5736 ("the type of a component cannot be abstract",
5737 Subtype_Indication (Component_Def));
5740 -- There may be an invariant declared for the component type, but
5741 -- the construction of the component invariant checking procedure
5742 -- takes place during expansion.
5743 end Array_Type_Declaration;
5745 ------------------------------------------------------
5746 -- Replace_Anonymous_Access_To_Protected_Subprogram --
5747 ------------------------------------------------------
5749 function Replace_Anonymous_Access_To_Protected_Subprogram
5750 (N : Node_Id) return Entity_Id
5752 Loc : constant Source_Ptr := Sloc (N);
5754 Curr_Scope : constant Scope_Stack_Entry :=
5755 Scope_Stack.Table (Scope_Stack.Last);
5757 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
5760 -- Access definition in declaration
5763 -- Object definition or formal definition with an access definition
5766 -- Declaration of anonymous access to subprogram type
5769 -- Original specification in access to subprogram
5774 Set_Is_Internal (Anon);
5777 when N_Component_Declaration |
5778 N_Unconstrained_Array_Definition |
5779 N_Constrained_Array_Definition =>
5780 Comp := Component_Definition (N);
5781 Acc := Access_Definition (Comp);
5783 when N_Discriminant_Specification =>
5784 Comp := Discriminant_Type (N);
5787 when N_Parameter_Specification =>
5788 Comp := Parameter_Type (N);
5791 when N_Access_Function_Definition =>
5792 Comp := Result_Definition (N);
5795 when N_Object_Declaration =>
5796 Comp := Object_Definition (N);
5799 when N_Function_Specification =>
5800 Comp := Result_Definition (N);
5804 raise Program_Error;
5807 Spec := Access_To_Subprogram_Definition (Acc);
5810 Make_Full_Type_Declaration (Loc,
5811 Defining_Identifier => Anon,
5812 Type_Definition => Copy_Separate_Tree (Spec));
5814 Mark_Rewrite_Insertion (Decl);
5816 -- In ASIS mode, analyze the profile on the original node, because
5817 -- the separate copy does not provide enough links to recover the
5818 -- original tree. Analysis is limited to type annotations, within
5819 -- a temporary scope that serves as an anonymous subprogram to collect
5820 -- otherwise useless temporaries and itypes.
5824 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
5827 if Nkind (Spec) = N_Access_Function_Definition then
5828 Set_Ekind (Typ, E_Function);
5830 Set_Ekind (Typ, E_Procedure);
5833 Set_Parent (Typ, N);
5834 Set_Scope (Typ, Current_Scope);
5837 -- Nothing to do if procedure is parameterless
5839 if Present (Parameter_Specifications (Spec)) then
5840 Process_Formals (Parameter_Specifications (Spec), Spec);
5843 if Nkind (Spec) = N_Access_Function_Definition then
5845 Def : constant Node_Id := Result_Definition (Spec);
5848 -- The result might itself be an anonymous access type, so
5851 if Nkind (Def) = N_Access_Definition then
5852 if Present (Access_To_Subprogram_Definition (Def)) then
5855 Replace_Anonymous_Access_To_Protected_Subprogram
5858 Find_Type (Subtype_Mark (Def));
5871 -- Insert the new declaration in the nearest enclosing scope. If the
5872 -- node is a body and N is its return type, the declaration belongs in
5873 -- the enclosing scope.
5877 if Nkind (P) = N_Subprogram_Body
5878 and then Nkind (N) = N_Function_Specification
5883 while Present (P) and then not Has_Declarations (P) loop
5887 pragma Assert (Present (P));
5889 if Nkind (P) = N_Package_Specification then
5890 Prepend (Decl, Visible_Declarations (P));
5892 Prepend (Decl, Declarations (P));
5895 -- Replace the anonymous type with an occurrence of the new declaration.
5896 -- In all cases the rewritten node does not have the null-exclusion
5897 -- attribute because (if present) it was already inherited by the
5898 -- anonymous entity (Anon). Thus, in case of components we do not
5899 -- inherit this attribute.
5901 if Nkind (N) = N_Parameter_Specification then
5902 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5903 Set_Etype (Defining_Identifier (N), Anon);
5904 Set_Null_Exclusion_Present (N, False);
5906 elsif Nkind (N) = N_Object_Declaration then
5907 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5908 Set_Etype (Defining_Identifier (N), Anon);
5910 elsif Nkind (N) = N_Access_Function_Definition then
5911 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5913 elsif Nkind (N) = N_Function_Specification then
5914 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
5915 Set_Etype (Defining_Unit_Name (N), Anon);
5919 Make_Component_Definition (Loc,
5920 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
5923 Mark_Rewrite_Insertion (Comp);
5925 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition) then
5929 -- Temporarily remove the current scope (record or subprogram) from
5930 -- the stack to add the new declarations to the enclosing scope.
5932 Scope_Stack.Decrement_Last;
5934 Set_Is_Itype (Anon);
5935 Scope_Stack.Append (Curr_Scope);
5938 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
5939 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
5941 end Replace_Anonymous_Access_To_Protected_Subprogram;
5943 -------------------------------
5944 -- Build_Derived_Access_Type --
5945 -------------------------------
5947 procedure Build_Derived_Access_Type
5949 Parent_Type : Entity_Id;
5950 Derived_Type : Entity_Id)
5952 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
5954 Desig_Type : Entity_Id;
5956 Discr_Con_Elist : Elist_Id;
5957 Discr_Con_El : Elmt_Id;
5961 -- Set the designated type so it is available in case this is an access
5962 -- to a self-referential type, e.g. a standard list type with a next
5963 -- pointer. Will be reset after subtype is built.
5965 Set_Directly_Designated_Type
5966 (Derived_Type, Designated_Type (Parent_Type));
5968 Subt := Process_Subtype (S, N);
5970 if Nkind (S) /= N_Subtype_Indication
5971 and then Subt /= Base_Type (Subt)
5973 Set_Ekind (Derived_Type, E_Access_Subtype);
5976 if Ekind (Derived_Type) = E_Access_Subtype then
5978 Pbase : constant Entity_Id := Base_Type (Parent_Type);
5979 Ibase : constant Entity_Id :=
5980 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
5981 Svg_Chars : constant Name_Id := Chars (Ibase);
5982 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
5985 Copy_Node (Pbase, Ibase);
5987 Set_Chars (Ibase, Svg_Chars);
5988 Set_Next_Entity (Ibase, Svg_Next_E);
5989 Set_Sloc (Ibase, Sloc (Derived_Type));
5990 Set_Scope (Ibase, Scope (Derived_Type));
5991 Set_Freeze_Node (Ibase, Empty);
5992 Set_Is_Frozen (Ibase, False);
5993 Set_Comes_From_Source (Ibase, False);
5994 Set_Is_First_Subtype (Ibase, False);
5996 Set_Etype (Ibase, Pbase);
5997 Set_Etype (Derived_Type, Ibase);
6001 Set_Directly_Designated_Type
6002 (Derived_Type, Designated_Type (Subt));
6004 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6005 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6006 Set_Size_Info (Derived_Type, Parent_Type);
6007 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6008 Set_Depends_On_Private (Derived_Type,
6009 Has_Private_Component (Derived_Type));
6010 Conditional_Delay (Derived_Type, Subt);
6012 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6013 -- that it is not redundant.
6015 if Null_Exclusion_Present (Type_Definition (N)) then
6016 Set_Can_Never_Be_Null (Derived_Type);
6018 -- What is with the "AND THEN FALSE" here ???
6020 if Can_Never_Be_Null (Parent_Type)
6024 ("`NOT NULL` not allowed (& already excludes null)",
6028 elsif Can_Never_Be_Null (Parent_Type) then
6029 Set_Can_Never_Be_Null (Derived_Type);
6032 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6033 -- the root type for this information.
6035 -- Apply range checks to discriminants for derived record case
6036 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6038 Desig_Type := Designated_Type (Derived_Type);
6039 if Is_Composite_Type (Desig_Type)
6040 and then (not Is_Array_Type (Desig_Type))
6041 and then Has_Discriminants (Desig_Type)
6042 and then Base_Type (Desig_Type) /= Desig_Type
6044 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6045 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6047 Discr := First_Discriminant (Base_Type (Desig_Type));
6048 while Present (Discr_Con_El) loop
6049 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6050 Next_Elmt (Discr_Con_El);
6051 Next_Discriminant (Discr);
6054 end Build_Derived_Access_Type;
6056 ------------------------------
6057 -- Build_Derived_Array_Type --
6058 ------------------------------
6060 procedure Build_Derived_Array_Type
6062 Parent_Type : Entity_Id;
6063 Derived_Type : Entity_Id)
6065 Loc : constant Source_Ptr := Sloc (N);
6066 Tdef : constant Node_Id := Type_Definition (N);
6067 Indic : constant Node_Id := Subtype_Indication (Tdef);
6068 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6069 Implicit_Base : Entity_Id;
6070 New_Indic : Node_Id;
6072 procedure Make_Implicit_Base;
6073 -- If the parent subtype is constrained, the derived type is a subtype
6074 -- of an implicit base type derived from the parent base.
6076 ------------------------
6077 -- Make_Implicit_Base --
6078 ------------------------
6080 procedure Make_Implicit_Base is
6083 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6085 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6086 Set_Etype (Implicit_Base, Parent_Base);
6088 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6089 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6091 Set_Has_Delayed_Freeze (Implicit_Base, True);
6093 -- Inherit the "ghostness" from the parent base type
6095 if Is_Ghost_Entity (Parent_Base) or else Ghost_Mode > None then
6096 Set_Is_Ghost_Entity (Implicit_Base);
6098 end Make_Implicit_Base;
6100 -- Start of processing for Build_Derived_Array_Type
6103 if not Is_Constrained (Parent_Type) then
6104 if Nkind (Indic) /= N_Subtype_Indication then
6105 Set_Ekind (Derived_Type, E_Array_Type);
6107 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6108 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6110 Set_Has_Delayed_Freeze (Derived_Type, True);
6114 Set_Etype (Derived_Type, Implicit_Base);
6117 Make_Subtype_Declaration (Loc,
6118 Defining_Identifier => Derived_Type,
6119 Subtype_Indication =>
6120 Make_Subtype_Indication (Loc,
6121 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6122 Constraint => Constraint (Indic)));
6124 Rewrite (N, New_Indic);
6129 if Nkind (Indic) /= N_Subtype_Indication then
6132 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6133 Set_Etype (Derived_Type, Implicit_Base);
6134 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6137 Error_Msg_N ("illegal constraint on constrained type", Indic);
6141 -- If parent type is not a derived type itself, and is declared in
6142 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6143 -- the new type's concatenation operator since Derive_Subprograms
6144 -- will not inherit the parent's operator. If the parent type is
6145 -- unconstrained, the operator is of the unconstrained base type.
6147 if Number_Dimensions (Parent_Type) = 1
6148 and then not Is_Limited_Type (Parent_Type)
6149 and then not Is_Derived_Type (Parent_Type)
6150 and then not Is_Package_Or_Generic_Package
6151 (Scope (Base_Type (Parent_Type)))
6153 if not Is_Constrained (Parent_Type)
6154 and then Is_Constrained (Derived_Type)
6156 New_Concatenation_Op (Implicit_Base);
6158 New_Concatenation_Op (Derived_Type);
6161 end Build_Derived_Array_Type;
6163 -----------------------------------
6164 -- Build_Derived_Concurrent_Type --
6165 -----------------------------------
6167 procedure Build_Derived_Concurrent_Type
6169 Parent_Type : Entity_Id;
6170 Derived_Type : Entity_Id)
6172 Loc : constant Source_Ptr := Sloc (N);
6174 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6175 Corr_Decl : Node_Id;
6176 Corr_Decl_Needed : Boolean;
6177 -- If the derived type has fewer discriminants than its parent, the
6178 -- corresponding record is also a derived type, in order to account for
6179 -- the bound discriminants. We create a full type declaration for it in
6182 Constraint_Present : constant Boolean :=
6183 Nkind (Subtype_Indication (Type_Definition (N))) =
6184 N_Subtype_Indication;
6186 D_Constraint : Node_Id;
6187 New_Constraint : Elist_Id;
6188 Old_Disc : Entity_Id;
6189 New_Disc : Entity_Id;
6193 Set_Stored_Constraint (Derived_Type, No_Elist);
6194 Corr_Decl_Needed := False;
6197 if Present (Discriminant_Specifications (N))
6198 and then Constraint_Present
6200 Old_Disc := First_Discriminant (Parent_Type);
6201 New_Disc := First (Discriminant_Specifications (N));
6202 while Present (New_Disc) and then Present (Old_Disc) loop
6203 Next_Discriminant (Old_Disc);
6208 if Present (Old_Disc) and then Expander_Active then
6210 -- The new type has fewer discriminants, so we need to create a new
6211 -- corresponding record, which is derived from the corresponding
6212 -- record of the parent, and has a stored constraint that captures
6213 -- the values of the discriminant constraints. The corresponding
6214 -- record is needed only if expander is active and code generation is
6217 -- The type declaration for the derived corresponding record has the
6218 -- same discriminant part and constraints as the current declaration.
6219 -- Copy the unanalyzed tree to build declaration.
6221 Corr_Decl_Needed := True;
6222 New_N := Copy_Separate_Tree (N);
6225 Make_Full_Type_Declaration (Loc,
6226 Defining_Identifier => Corr_Record,
6227 Discriminant_Specifications =>
6228 Discriminant_Specifications (New_N),
6230 Make_Derived_Type_Definition (Loc,
6231 Subtype_Indication =>
6232 Make_Subtype_Indication (Loc,
6235 (Corresponding_Record_Type (Parent_Type), Loc),
6238 (Subtype_Indication (Type_Definition (New_N))))));
6241 -- Copy Storage_Size and Relative_Deadline variables if task case
6243 if Is_Task_Type (Parent_Type) then
6244 Set_Storage_Size_Variable (Derived_Type,
6245 Storage_Size_Variable (Parent_Type));
6246 Set_Relative_Deadline_Variable (Derived_Type,
6247 Relative_Deadline_Variable (Parent_Type));
6250 if Present (Discriminant_Specifications (N)) then
6251 Push_Scope (Derived_Type);
6252 Check_Or_Process_Discriminants (N, Derived_Type);
6254 if Constraint_Present then
6256 Expand_To_Stored_Constraint
6258 Build_Discriminant_Constraints
6260 Subtype_Indication (Type_Definition (N)), True));
6265 elsif Constraint_Present then
6267 -- Build constrained subtype, copying the constraint, and derive
6268 -- from it to create a derived constrained type.
6271 Loc : constant Source_Ptr := Sloc (N);
6272 Anon : constant Entity_Id :=
6273 Make_Defining_Identifier (Loc,
6274 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6279 Make_Subtype_Declaration (Loc,
6280 Defining_Identifier => Anon,
6281 Subtype_Indication =>
6282 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6283 Insert_Before (N, Decl);
6286 Rewrite (Subtype_Indication (Type_Definition (N)),
6287 New_Occurrence_Of (Anon, Loc));
6288 Set_Analyzed (Derived_Type, False);
6294 -- By default, operations and private data are inherited from parent.
6295 -- However, in the presence of bound discriminants, a new corresponding
6296 -- record will be created, see below.
6298 Set_Has_Discriminants
6299 (Derived_Type, Has_Discriminants (Parent_Type));
6300 Set_Corresponding_Record_Type
6301 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6303 -- Is_Constrained is set according the parent subtype, but is set to
6304 -- False if the derived type is declared with new discriminants.
6308 (Is_Constrained (Parent_Type) or else Constraint_Present)
6309 and then not Present (Discriminant_Specifications (N)));
6311 if Constraint_Present then
6312 if not Has_Discriminants (Parent_Type) then
6313 Error_Msg_N ("untagged parent must have discriminants", N);
6315 elsif Present (Discriminant_Specifications (N)) then
6317 -- Verify that new discriminants are used to constrain old ones
6322 (Constraint (Subtype_Indication (Type_Definition (N)))));
6324 Old_Disc := First_Discriminant (Parent_Type);
6326 while Present (D_Constraint) loop
6327 if Nkind (D_Constraint) /= N_Discriminant_Association then
6329 -- Positional constraint. If it is a reference to a new
6330 -- discriminant, it constrains the corresponding old one.
6332 if Nkind (D_Constraint) = N_Identifier then
6333 New_Disc := First_Discriminant (Derived_Type);
6334 while Present (New_Disc) loop
6335 exit when Chars (New_Disc) = Chars (D_Constraint);
6336 Next_Discriminant (New_Disc);
6339 if Present (New_Disc) then
6340 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6344 Next_Discriminant (Old_Disc);
6346 -- if this is a named constraint, search by name for the old
6347 -- discriminants constrained by the new one.
6349 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
6351 -- Find new discriminant with that name
6353 New_Disc := First_Discriminant (Derived_Type);
6354 while Present (New_Disc) loop
6356 Chars (New_Disc) = Chars (Expression (D_Constraint));
6357 Next_Discriminant (New_Disc);
6360 if Present (New_Disc) then
6362 -- Verify that new discriminant renames some discriminant
6363 -- of the parent type, and associate the new discriminant
6364 -- with one or more old ones that it renames.
6370 Selector := First (Selector_Names (D_Constraint));
6371 while Present (Selector) loop
6372 Old_Disc := First_Discriminant (Parent_Type);
6373 while Present (Old_Disc) loop
6374 exit when Chars (Old_Disc) = Chars (Selector);
6375 Next_Discriminant (Old_Disc);
6378 if Present (Old_Disc) then
6379 Set_Corresponding_Discriminant
6380 (New_Disc, Old_Disc);
6389 Next (D_Constraint);
6392 New_Disc := First_Discriminant (Derived_Type);
6393 while Present (New_Disc) loop
6394 if No (Corresponding_Discriminant (New_Disc)) then
6396 ("new discriminant& must constrain old one", N, New_Disc);
6399 Subtypes_Statically_Compatible
6401 Etype (Corresponding_Discriminant (New_Disc)))
6404 ("& not statically compatible with parent discriminant",
6408 Next_Discriminant (New_Disc);
6412 elsif Present (Discriminant_Specifications (N)) then
6414 ("missing discriminant constraint in untagged derivation", N);
6417 -- The entity chain of the derived type includes the new discriminants
6418 -- but shares operations with the parent.
6420 if Present (Discriminant_Specifications (N)) then
6421 Old_Disc := First_Discriminant (Parent_Type);
6422 while Present (Old_Disc) loop
6423 if No (Next_Entity (Old_Disc))
6424 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
6427 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
6431 Next_Discriminant (Old_Disc);
6435 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
6436 if Has_Discriminants (Parent_Type) then
6437 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6438 Set_Discriminant_Constraint (
6439 Derived_Type, Discriminant_Constraint (Parent_Type));
6443 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
6445 Set_Has_Completion (Derived_Type);
6447 if Corr_Decl_Needed then
6448 Set_Stored_Constraint (Derived_Type, New_Constraint);
6449 Insert_After (N, Corr_Decl);
6450 Analyze (Corr_Decl);
6451 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
6453 end Build_Derived_Concurrent_Type;
6455 ------------------------------------
6456 -- Build_Derived_Enumeration_Type --
6457 ------------------------------------
6459 procedure Build_Derived_Enumeration_Type
6461 Parent_Type : Entity_Id;
6462 Derived_Type : Entity_Id)
6464 Loc : constant Source_Ptr := Sloc (N);
6465 Def : constant Node_Id := Type_Definition (N);
6466 Indic : constant Node_Id := Subtype_Indication (Def);
6467 Implicit_Base : Entity_Id;
6468 Literal : Entity_Id;
6469 New_Lit : Entity_Id;
6470 Literals_List : List_Id;
6471 Type_Decl : Node_Id;
6473 Rang_Expr : Node_Id;
6476 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
6477 -- not have explicit literals lists we need to process types derived
6478 -- from them specially. This is handled by Derived_Standard_Character.
6479 -- If the parent type is a generic type, there are no literals either,
6480 -- and we construct the same skeletal representation as for the generic
6483 if Is_Standard_Character_Type (Parent_Type) then
6484 Derived_Standard_Character (N, Parent_Type, Derived_Type);
6486 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
6492 if Nkind (Indic) /= N_Subtype_Indication then
6494 Make_Attribute_Reference (Loc,
6495 Attribute_Name => Name_First,
6496 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6497 Set_Etype (Lo, Derived_Type);
6500 Make_Attribute_Reference (Loc,
6501 Attribute_Name => Name_Last,
6502 Prefix => New_Occurrence_Of (Derived_Type, Loc));
6503 Set_Etype (Hi, Derived_Type);
6505 Set_Scalar_Range (Derived_Type,
6511 -- Analyze subtype indication and verify compatibility
6512 -- with parent type.
6514 if Base_Type (Process_Subtype (Indic, N)) /=
6515 Base_Type (Parent_Type)
6518 ("illegal constraint for formal discrete type", N);
6524 -- If a constraint is present, analyze the bounds to catch
6525 -- premature usage of the derived literals.
6527 if Nkind (Indic) = N_Subtype_Indication
6528 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
6530 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
6531 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
6534 -- Introduce an implicit base type for the derived type even if there
6535 -- is no constraint attached to it, since this seems closer to the
6536 -- Ada semantics. Build a full type declaration tree for the derived
6537 -- type using the implicit base type as the defining identifier. The
6538 -- build a subtype declaration tree which applies the constraint (if
6539 -- any) have it replace the derived type declaration.
6541 Literal := First_Literal (Parent_Type);
6542 Literals_List := New_List;
6543 while Present (Literal)
6544 and then Ekind (Literal) = E_Enumeration_Literal
6546 -- Literals of the derived type have the same representation as
6547 -- those of the parent type, but this representation can be
6548 -- overridden by an explicit representation clause. Indicate
6549 -- that there is no explicit representation given yet. These
6550 -- derived literals are implicit operations of the new type,
6551 -- and can be overridden by explicit ones.
6553 if Nkind (Literal) = N_Defining_Character_Literal then
6555 Make_Defining_Character_Literal (Loc, Chars (Literal));
6557 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
6560 Set_Ekind (New_Lit, E_Enumeration_Literal);
6561 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
6562 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
6563 Set_Enumeration_Rep_Expr (New_Lit, Empty);
6564 Set_Alias (New_Lit, Literal);
6565 Set_Is_Known_Valid (New_Lit, True);
6567 Append (New_Lit, Literals_List);
6568 Next_Literal (Literal);
6572 Make_Defining_Identifier (Sloc (Derived_Type),
6573 Chars => New_External_Name (Chars (Derived_Type), 'B'));
6575 -- Indicate the proper nature of the derived type. This must be done
6576 -- before analysis of the literals, to recognize cases when a literal
6577 -- may be hidden by a previous explicit function definition (cf.
6580 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
6581 Set_Etype (Derived_Type, Implicit_Base);
6584 Make_Full_Type_Declaration (Loc,
6585 Defining_Identifier => Implicit_Base,
6586 Discriminant_Specifications => No_List,
6588 Make_Enumeration_Type_Definition (Loc, Literals_List));
6590 Mark_Rewrite_Insertion (Type_Decl);
6591 Insert_Before (N, Type_Decl);
6592 Analyze (Type_Decl);
6594 -- The anonymous base now has a full declaration, but this base
6595 -- is not a first subtype.
6597 Set_Is_First_Subtype (Implicit_Base, False);
6599 -- After the implicit base is analyzed its Etype needs to be changed
6600 -- to reflect the fact that it is derived from the parent type which
6601 -- was ignored during analysis. We also set the size at this point.
6603 Set_Etype (Implicit_Base, Parent_Type);
6605 Set_Size_Info (Implicit_Base, Parent_Type);
6606 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
6607 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
6609 -- Copy other flags from parent type
6611 Set_Has_Non_Standard_Rep
6612 (Implicit_Base, Has_Non_Standard_Rep
6614 Set_Has_Pragma_Ordered
6615 (Implicit_Base, Has_Pragma_Ordered
6617 Set_Has_Delayed_Freeze (Implicit_Base);
6619 -- Process the subtype indication including a validation check on the
6620 -- constraint, if any. If a constraint is given, its bounds must be
6621 -- implicitly converted to the new type.
6623 if Nkind (Indic) = N_Subtype_Indication then
6625 R : constant Node_Id :=
6626 Range_Expression (Constraint (Indic));
6629 if Nkind (R) = N_Range then
6630 Hi := Build_Scalar_Bound
6631 (High_Bound (R), Parent_Type, Implicit_Base);
6632 Lo := Build_Scalar_Bound
6633 (Low_Bound (R), Parent_Type, Implicit_Base);
6636 -- Constraint is a Range attribute. Replace with explicit
6637 -- mention of the bounds of the prefix, which must be a
6640 Analyze (Prefix (R));
6642 Convert_To (Implicit_Base,
6643 Make_Attribute_Reference (Loc,
6644 Attribute_Name => Name_Last,
6646 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6649 Convert_To (Implicit_Base,
6650 Make_Attribute_Reference (Loc,
6651 Attribute_Name => Name_First,
6653 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
6660 (Type_High_Bound (Parent_Type),
6661 Parent_Type, Implicit_Base);
6664 (Type_Low_Bound (Parent_Type),
6665 Parent_Type, Implicit_Base);
6673 -- If we constructed a default range for the case where no range
6674 -- was given, then the expressions in the range must not freeze
6675 -- since they do not correspond to expressions in the source.
6677 if Nkind (Indic) /= N_Subtype_Indication then
6678 Set_Must_Not_Freeze (Lo);
6679 Set_Must_Not_Freeze (Hi);
6680 Set_Must_Not_Freeze (Rang_Expr);
6684 Make_Subtype_Declaration (Loc,
6685 Defining_Identifier => Derived_Type,
6686 Subtype_Indication =>
6687 Make_Subtype_Indication (Loc,
6688 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6690 Make_Range_Constraint (Loc,
6691 Range_Expression => Rang_Expr))));
6695 -- Propagate the aspects from the original type declaration to the
6696 -- declaration of the implicit base.
6698 Move_Aspects (From => Original_Node (N), To => Type_Decl);
6700 -- Apply a range check. Since this range expression doesn't have an
6701 -- Etype, we have to specifically pass the Source_Typ parameter. Is
6704 if Nkind (Indic) = N_Subtype_Indication then
6706 (Range_Expression (Constraint (Indic)), Parent_Type,
6707 Source_Typ => Entity (Subtype_Mark (Indic)));
6710 end Build_Derived_Enumeration_Type;
6712 --------------------------------
6713 -- Build_Derived_Numeric_Type --
6714 --------------------------------
6716 procedure Build_Derived_Numeric_Type
6718 Parent_Type : Entity_Id;
6719 Derived_Type : Entity_Id)
6721 Loc : constant Source_Ptr := Sloc (N);
6722 Tdef : constant Node_Id := Type_Definition (N);
6723 Indic : constant Node_Id := Subtype_Indication (Tdef);
6724 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6725 No_Constraint : constant Boolean := Nkind (Indic) /=
6726 N_Subtype_Indication;
6727 Implicit_Base : Entity_Id;
6733 -- Process the subtype indication including a validation check on
6734 -- the constraint if any.
6736 Discard_Node (Process_Subtype (Indic, N));
6738 -- Introduce an implicit base type for the derived type even if there
6739 -- is no constraint attached to it, since this seems closer to the Ada
6743 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6745 Set_Etype (Implicit_Base, Parent_Base);
6746 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6747 Set_Size_Info (Implicit_Base, Parent_Base);
6748 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
6749 Set_Parent (Implicit_Base, Parent (Derived_Type));
6750 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
6752 -- Set RM Size for discrete type or decimal fixed-point type
6753 -- Ordinary fixed-point is excluded, why???
6755 if Is_Discrete_Type (Parent_Base)
6756 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
6758 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
6761 Set_Has_Delayed_Freeze (Implicit_Base);
6763 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
6764 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
6766 Set_Scalar_Range (Implicit_Base,
6771 if Has_Infinities (Parent_Base) then
6772 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
6775 -- The Derived_Type, which is the entity of the declaration, is a
6776 -- subtype of the implicit base. Its Ekind is a subtype, even in the
6777 -- absence of an explicit constraint.
6779 Set_Etype (Derived_Type, Implicit_Base);
6781 -- If we did not have a constraint, then the Ekind is set from the
6782 -- parent type (otherwise Process_Subtype has set the bounds)
6784 if No_Constraint then
6785 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
6788 -- If we did not have a range constraint, then set the range from the
6789 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
6791 if No_Constraint or else not Has_Range_Constraint (Indic) then
6792 Set_Scalar_Range (Derived_Type,
6794 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
6795 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
6796 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
6798 if Has_Infinities (Parent_Type) then
6799 Set_Includes_Infinities (Scalar_Range (Derived_Type));
6802 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
6805 Set_Is_Descendent_Of_Address (Derived_Type,
6806 Is_Descendent_Of_Address (Parent_Type));
6807 Set_Is_Descendent_Of_Address (Implicit_Base,
6808 Is_Descendent_Of_Address (Parent_Type));
6810 -- Set remaining type-specific fields, depending on numeric type
6812 if Is_Modular_Integer_Type (Parent_Type) then
6813 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
6815 Set_Non_Binary_Modulus
6816 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
6819 (Implicit_Base, Is_Known_Valid (Parent_Base));
6821 elsif Is_Floating_Point_Type (Parent_Type) then
6823 -- Digits of base type is always copied from the digits value of
6824 -- the parent base type, but the digits of the derived type will
6825 -- already have been set if there was a constraint present.
6827 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6828 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
6830 if No_Constraint then
6831 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
6834 elsif Is_Fixed_Point_Type (Parent_Type) then
6836 -- Small of base type and derived type are always copied from the
6837 -- parent base type, since smalls never change. The delta of the
6838 -- base type is also copied from the parent base type. However the
6839 -- delta of the derived type will have been set already if a
6840 -- constraint was present.
6842 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
6843 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
6844 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
6846 if No_Constraint then
6847 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
6850 -- The scale and machine radix in the decimal case are always
6851 -- copied from the parent base type.
6853 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
6854 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
6855 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
6857 Set_Machine_Radix_10
6858 (Derived_Type, Machine_Radix_10 (Parent_Base));
6859 Set_Machine_Radix_10
6860 (Implicit_Base, Machine_Radix_10 (Parent_Base));
6862 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
6864 if No_Constraint then
6865 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
6868 -- the analysis of the subtype_indication sets the
6869 -- digits value of the derived type.
6876 if Is_Integer_Type (Parent_Type) then
6877 Set_Has_Shift_Operator
6878 (Implicit_Base, Has_Shift_Operator (Parent_Type));
6881 -- The type of the bounds is that of the parent type, and they
6882 -- must be converted to the derived type.
6884 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
6886 -- The implicit_base should be frozen when the derived type is frozen,
6887 -- but note that it is used in the conversions of the bounds. For fixed
6888 -- types we delay the determination of the bounds until the proper
6889 -- freezing point. For other numeric types this is rejected by GCC, for
6890 -- reasons that are currently unclear (???), so we choose to freeze the
6891 -- implicit base now. In the case of integers and floating point types
6892 -- this is harmless because subsequent representation clauses cannot
6893 -- affect anything, but it is still baffling that we cannot use the
6894 -- same mechanism for all derived numeric types.
6896 -- There is a further complication: actually some representation
6897 -- clauses can affect the implicit base type. For example, attribute
6898 -- definition clauses for stream-oriented attributes need to set the
6899 -- corresponding TSS entries on the base type, and this normally
6900 -- cannot be done after the base type is frozen, so the circuitry in
6901 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
6902 -- and not use Set_TSS in this case.
6904 -- There are also consequences for the case of delayed representation
6905 -- aspects for some cases. For example, a Size aspect is delayed and
6906 -- should not be evaluated to the freeze point. This early freezing
6907 -- means that the size attribute evaluation happens too early???
6909 if Is_Fixed_Point_Type (Parent_Type) then
6910 Conditional_Delay (Implicit_Base, Parent_Type);
6912 Freeze_Before (N, Implicit_Base);
6914 end Build_Derived_Numeric_Type;
6916 --------------------------------
6917 -- Build_Derived_Private_Type --
6918 --------------------------------
6920 procedure Build_Derived_Private_Type
6922 Parent_Type : Entity_Id;
6923 Derived_Type : Entity_Id;
6924 Is_Completion : Boolean;
6925 Derive_Subps : Boolean := True)
6927 Loc : constant Source_Ptr := Sloc (N);
6928 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
6929 Par_Scope : constant Entity_Id := Scope (Par_Base);
6930 Full_N : constant Node_Id := New_Copy_Tree (N);
6931 Full_Der : Entity_Id := New_Copy (Derived_Type);
6934 procedure Build_Full_Derivation;
6935 -- Build full derivation, i.e. derive from the full view
6937 procedure Copy_And_Build;
6938 -- Copy derived type declaration, replace parent with its full view,
6939 -- and build derivation
6941 ---------------------------
6942 -- Build_Full_Derivation --
6943 ---------------------------
6945 procedure Build_Full_Derivation is
6947 -- If parent scope is not open, install the declarations
6949 if not In_Open_Scopes (Par_Scope) then
6950 Install_Private_Declarations (Par_Scope);
6951 Install_Visible_Declarations (Par_Scope);
6953 Uninstall_Declarations (Par_Scope);
6955 -- If parent scope is open and in another unit, and parent has a
6956 -- completion, then the derivation is taking place in the visible
6957 -- part of a child unit. In that case retrieve the full view of
6958 -- the parent momentarily.
6960 elsif not In_Same_Source_Unit (N, Parent_Type) then
6961 Full_P := Full_View (Parent_Type);
6962 Exchange_Declarations (Parent_Type);
6964 Exchange_Declarations (Full_P);
6966 -- Otherwise it is a local derivation
6971 end Build_Full_Derivation;
6973 --------------------
6974 -- Copy_And_Build --
6975 --------------------
6977 procedure Copy_And_Build is
6978 Full_Parent : Entity_Id := Parent_Type;
6981 -- If the parent is itself derived from another private type,
6982 -- installing the private declarations has not affected its
6983 -- privacy status, so use its own full view explicitly.
6985 if Is_Private_Type (Full_Parent)
6986 and then Present (Full_View (Full_Parent))
6988 Full_Parent := Full_View (Full_Parent);
6991 -- And its underlying full view if necessary
6993 if Is_Private_Type (Full_Parent)
6994 and then Present (Underlying_Full_View (Full_Parent))
6996 Full_Parent := Underlying_Full_View (Full_Parent);
6999 -- For record, access and most enumeration types, derivation from
7000 -- the full view requires a fully-fledged declaration. In the other
7001 -- cases, just use an itype.
7003 if Ekind (Full_Parent) in Record_Kind
7004 or else Ekind (Full_Parent) in Access_Kind
7006 (Ekind (Full_Parent) in Enumeration_Kind
7007 and then not Is_Standard_Character_Type (Full_Parent)
7008 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7010 -- Copy and adjust declaration to provide a completion for what
7011 -- is originally a private declaration. Indicate that full view
7012 -- is internally generated.
7014 Set_Comes_From_Source (Full_N, False);
7015 Set_Comes_From_Source (Full_Der, False);
7016 Set_Parent (Full_Der, Full_N);
7017 Set_Defining_Identifier (Full_N, Full_Der);
7019 -- If there are no constraints, adjust the subtype mark
7021 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7022 N_Subtype_Indication
7024 Set_Subtype_Indication
7025 (Type_Definition (Full_N),
7026 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7029 Insert_After (N, Full_N);
7031 -- Build full view of derived type from full view of parent which
7032 -- is now installed. Subprograms have been derived on the partial
7033 -- view, the completion does not derive them anew.
7035 if Ekind (Full_Parent) in Record_Kind then
7037 -- If parent type is tagged, the completion inherits the proper
7038 -- primitive operations.
7040 if Is_Tagged_Type (Parent_Type) then
7041 Build_Derived_Record_Type
7042 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7044 Build_Derived_Record_Type
7045 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7050 (Full_N, Full_Parent, Full_Der,
7051 Is_Completion => False, Derive_Subps => False);
7054 -- The full declaration has been introduced into the tree and
7055 -- processed in the step above. It should not be analyzed again
7056 -- (when encountered later in the current list of declarations)
7057 -- to prevent spurious name conflicts. The full entity remains
7060 Set_Analyzed (Full_N);
7064 Make_Defining_Identifier (Sloc (Derived_Type),
7065 Chars => Chars (Derived_Type));
7066 Set_Is_Itype (Full_Der);
7067 Set_Associated_Node_For_Itype (Full_Der, N);
7068 Set_Parent (Full_Der, N);
7070 (N, Full_Parent, Full_Der,
7071 Is_Completion => False, Derive_Subps => False);
7074 Set_Has_Private_Declaration (Full_Der);
7075 Set_Has_Private_Declaration (Derived_Type);
7077 Set_Scope (Full_Der, Scope (Derived_Type));
7078 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7079 Set_Has_Size_Clause (Full_Der, False);
7080 Set_Has_Alignment_Clause (Full_Der, False);
7081 Set_Has_Delayed_Freeze (Full_Der);
7082 Set_Is_Frozen (Full_Der, False);
7083 Set_Freeze_Node (Full_Der, Empty);
7084 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7085 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7087 -- The convention on the base type may be set in the private part
7088 -- and not propagated to the subtype until later, so we obtain the
7089 -- convention from the base type of the parent.
7091 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7094 -- Start of processing for Build_Derived_Private_Type
7097 if Is_Tagged_Type (Parent_Type) then
7098 Full_P := Full_View (Parent_Type);
7100 -- A type extension of a type with unknown discriminants is an
7101 -- indefinite type that the back-end cannot handle directly.
7102 -- We treat it as a private type, and build a completion that is
7103 -- derived from the full view of the parent, and hopefully has
7104 -- known discriminants.
7106 -- If the full view of the parent type has an underlying record view,
7107 -- use it to generate the underlying record view of this derived type
7108 -- (required for chains of derivations with unknown discriminants).
7110 -- Minor optimization: we avoid the generation of useless underlying
7111 -- record view entities if the private type declaration has unknown
7112 -- discriminants but its corresponding full view has no
7115 if Has_Unknown_Discriminants (Parent_Type)
7116 and then Present (Full_P)
7117 and then (Has_Discriminants (Full_P)
7118 or else Present (Underlying_Record_View (Full_P)))
7119 and then not In_Open_Scopes (Par_Scope)
7120 and then Expander_Active
7123 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7124 New_Ext : constant Node_Id :=
7126 (Record_Extension_Part (Type_Definition (N)));
7130 Build_Derived_Record_Type
7131 (N, Parent_Type, Derived_Type, Derive_Subps);
7133 -- Build anonymous completion, as a derivation from the full
7134 -- view of the parent. This is not a completion in the usual
7135 -- sense, because the current type is not private.
7138 Make_Full_Type_Declaration (Loc,
7139 Defining_Identifier => Full_Der,
7141 Make_Derived_Type_Definition (Loc,
7142 Subtype_Indication =>
7144 (Subtype_Indication (Type_Definition (N))),
7145 Record_Extension_Part => New_Ext));
7147 -- If the parent type has an underlying record view, use it
7148 -- here to build the new underlying record view.
7150 if Present (Underlying_Record_View (Full_P)) then
7152 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7154 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7155 Underlying_Record_View (Full_P));
7158 Install_Private_Declarations (Par_Scope);
7159 Install_Visible_Declarations (Par_Scope);
7160 Insert_Before (N, Decl);
7162 -- Mark entity as an underlying record view before analysis,
7163 -- to avoid generating the list of its primitive operations
7164 -- (which is not really required for this entity) and thus
7165 -- prevent spurious errors associated with missing overriding
7166 -- of abstract primitives (overridden only for Derived_Type).
7168 Set_Ekind (Full_Der, E_Record_Type);
7169 Set_Is_Underlying_Record_View (Full_Der);
7170 Set_Default_SSO (Full_Der);
7174 pragma Assert (Has_Discriminants (Full_Der)
7175 and then not Has_Unknown_Discriminants (Full_Der));
7177 Uninstall_Declarations (Par_Scope);
7179 -- Freeze the underlying record view, to prevent generation of
7180 -- useless dispatching information, which is simply shared with
7181 -- the real derived type.
7183 Set_Is_Frozen (Full_Der);
7185 -- If the derived type has access discriminants, create
7186 -- references to their anonymous types now, to prevent
7187 -- back-end problems when their first use is in generated
7188 -- bodies of primitives.
7194 E := First_Entity (Full_Der);
7196 while Present (E) loop
7197 if Ekind (E) = E_Discriminant
7198 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7200 Build_Itype_Reference (Etype (E), Decl);
7207 -- Set up links between real entity and underlying record view
7209 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7210 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7213 -- If discriminants are known, build derived record
7216 Build_Derived_Record_Type
7217 (N, Parent_Type, Derived_Type, Derive_Subps);
7222 elsif Has_Discriminants (Parent_Type) then
7224 -- Build partial view of derived type from partial view of parent.
7225 -- This must be done before building the full derivation because the
7226 -- second derivation will modify the discriminants of the first and
7227 -- the discriminants are chained with the rest of the components in
7228 -- the full derivation.
7230 Build_Derived_Record_Type
7231 (N, Parent_Type, Derived_Type, Derive_Subps);
7233 -- Build the full derivation if this is not the anonymous derived
7234 -- base type created by Build_Derived_Record_Type in the constrained
7235 -- case (see point 5. of its head comment) since we build it for the
7236 -- derived subtype. And skip it for protected types altogether, as
7237 -- gigi does not use these types directly.
7239 if Present (Full_View (Parent_Type))
7240 and then not Is_Itype (Derived_Type)
7241 and then not (Ekind (Full_View (Parent_Type)) in Protected_Kind)
7244 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7246 Last_Discr : Entity_Id;
7249 -- If this is not a completion, construct the implicit full
7250 -- view by deriving from the full view of the parent type.
7251 -- But if this is a completion, the derived private type
7252 -- being built is a full view and the full derivation can
7253 -- only be its underlying full view.
7255 Build_Full_Derivation;
7257 if not Is_Completion then
7258 Set_Full_View (Derived_Type, Full_Der);
7260 Set_Underlying_Full_View (Derived_Type, Full_Der);
7263 if not Is_Base_Type (Derived_Type) then
7264 Set_Full_View (Der_Base, Base_Type (Full_Der));
7267 -- Copy the discriminant list from full view to the partial
7268 -- view (base type and its subtype). Gigi requires that the
7269 -- partial and full views have the same discriminants.
7271 -- Note that since the partial view points to discriminants
7272 -- in the full view, their scope will be that of the full
7273 -- view. This might cause some front end problems and need
7276 Discr := First_Discriminant (Base_Type (Full_Der));
7277 Set_First_Entity (Der_Base, Discr);
7280 Last_Discr := Discr;
7281 Next_Discriminant (Discr);
7282 exit when No (Discr);
7285 Set_Last_Entity (Der_Base, Last_Discr);
7286 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7287 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7289 Set_Stored_Constraint
7290 (Full_Der, Stored_Constraint (Derived_Type));
7294 elsif Present (Full_View (Parent_Type))
7295 and then Has_Discriminants (Full_View (Parent_Type))
7297 if Has_Unknown_Discriminants (Parent_Type)
7298 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7299 N_Subtype_Indication
7302 ("cannot constrain type with unknown discriminants",
7303 Subtype_Indication (Type_Definition (N)));
7307 -- If this is not a completion, construct the implicit full view by
7308 -- deriving from the full view of the parent type. But if this is a
7309 -- completion, the derived private type being built is a full view
7310 -- and the full derivation can only be its underlying full view.
7312 Build_Full_Derivation;
7314 if not Is_Completion then
7315 Set_Full_View (Derived_Type, Full_Der);
7317 Set_Underlying_Full_View (Derived_Type, Full_Der);
7320 -- In any case, the primitive operations are inherited from the
7321 -- parent type, not from the internal full view.
7323 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
7325 if Derive_Subps then
7326 Derive_Subprograms (Parent_Type, Derived_Type);
7329 Set_Stored_Constraint (Derived_Type, No_Elist);
7331 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
7334 -- Untagged type, No discriminants on either view
7336 if Nkind (Subtype_Indication (Type_Definition (N))) =
7337 N_Subtype_Indication
7340 ("illegal constraint on type without discriminants", N);
7343 if Present (Discriminant_Specifications (N))
7344 and then Present (Full_View (Parent_Type))
7345 and then not Is_Tagged_Type (Full_View (Parent_Type))
7347 Error_Msg_N ("cannot add discriminants to untagged type", N);
7350 Set_Stored_Constraint (Derived_Type, No_Elist);
7351 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7352 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
7353 Set_Has_Controlled_Component
7354 (Derived_Type, Has_Controlled_Component
7357 -- Direct controlled types do not inherit Finalize_Storage_Only flag
7359 if not Is_Controlled (Parent_Type) then
7360 Set_Finalize_Storage_Only
7361 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
7364 -- If this is not a completion, construct the implicit full view by
7365 -- deriving from the full view of the parent type.
7367 -- ??? If the parent is untagged private and its completion is
7368 -- tagged, this mechanism will not work because we cannot derive from
7369 -- the tagged full view unless we have an extension.
7371 if Present (Full_View (Parent_Type))
7372 and then not Is_Tagged_Type (Full_View (Parent_Type))
7373 and then not Is_Completion
7375 Build_Full_Derivation;
7376 Set_Full_View (Derived_Type, Full_Der);
7380 Set_Has_Unknown_Discriminants (Derived_Type,
7381 Has_Unknown_Discriminants (Parent_Type));
7383 if Is_Private_Type (Derived_Type) then
7384 Set_Private_Dependents (Derived_Type, New_Elmt_List);
7387 -- If the parent base type is in scope, add the derived type to its
7388 -- list of private dependents, because its full view may become
7389 -- visible subsequently (in a nested private part, a body, or in a
7390 -- further child unit).
7392 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
7393 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
7395 -- Check for unusual case where a type completed by a private
7396 -- derivation occurs within a package nested in a child unit, and
7397 -- the parent is declared in an ancestor.
7399 if Is_Child_Unit (Scope (Current_Scope))
7400 and then Is_Completion
7401 and then In_Private_Part (Current_Scope)
7402 and then Scope (Parent_Type) /= Current_Scope
7404 -- Note that if the parent has a completion in the private part,
7405 -- (which is itself a derivation from some other private type)
7406 -- it is that completion that is visible, there is no full view
7407 -- available, and no special processing is needed.
7409 and then Present (Full_View (Parent_Type))
7411 -- In this case, the full view of the parent type will become
7412 -- visible in the body of the enclosing child, and only then will
7413 -- the current type be possibly non-private. Build an underlying
7414 -- full view that will be installed when the enclosing child body
7417 if Present (Underlying_Full_View (Derived_Type)) then
7418 Full_Der := Underlying_Full_View (Derived_Type);
7420 Build_Full_Derivation;
7421 Set_Underlying_Full_View (Derived_Type, Full_Der);
7424 -- The full view will be used to swap entities on entry/exit to
7425 -- the body, and must appear in the entity list for the package.
7427 Append_Entity (Full_Der, Scope (Derived_Type));
7430 end Build_Derived_Private_Type;
7432 -------------------------------
7433 -- Build_Derived_Record_Type --
7434 -------------------------------
7438 -- Ideally we would like to use the same model of type derivation for
7439 -- tagged and untagged record types. Unfortunately this is not quite
7440 -- possible because the semantics of representation clauses is different
7441 -- for tagged and untagged records under inheritance. Consider the
7444 -- type R (...) is [tagged] record ... end record;
7445 -- type T (...) is new R (...) [with ...];
7447 -- The representation clauses for T can specify a completely different
7448 -- record layout from R's. Hence the same component can be placed in two
7449 -- very different positions in objects of type T and R. If R and T are
7450 -- tagged types, representation clauses for T can only specify the layout
7451 -- of non inherited components, thus components that are common in R and T
7452 -- have the same position in objects of type R and T.
7454 -- This has two implications. The first is that the entire tree for R's
7455 -- declaration needs to be copied for T in the untagged case, so that T
7456 -- can be viewed as a record type of its own with its own representation
7457 -- clauses. The second implication is the way we handle discriminants.
7458 -- Specifically, in the untagged case we need a way to communicate to Gigi
7459 -- what are the real discriminants in the record, while for the semantics
7460 -- we need to consider those introduced by the user to rename the
7461 -- discriminants in the parent type. This is handled by introducing the
7462 -- notion of stored discriminants. See below for more.
7464 -- Fortunately the way regular components are inherited can be handled in
7465 -- the same way in tagged and untagged types.
7467 -- To complicate things a bit more the private view of a private extension
7468 -- cannot be handled in the same way as the full view (for one thing the
7469 -- semantic rules are somewhat different). We will explain what differs
7472 -- 2. DISCRIMINANTS UNDER INHERITANCE
7474 -- The semantic rules governing the discriminants of derived types are
7477 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
7478 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
7480 -- If parent type has discriminants, then the discriminants that are
7481 -- declared in the derived type are [3.4 (11)]:
7483 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
7486 -- o Otherwise, each discriminant of the parent type (implicitly declared
7487 -- in the same order with the same specifications). In this case, the
7488 -- discriminants are said to be "inherited", or if unknown in the parent
7489 -- are also unknown in the derived type.
7491 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
7493 -- o The parent subtype must be constrained;
7495 -- o If the parent type is not a tagged type, then each discriminant of
7496 -- the derived type must be used in the constraint defining a parent
7497 -- subtype. [Implementation note: This ensures that the new discriminant
7498 -- can share storage with an existing discriminant.]
7500 -- For the derived type each discriminant of the parent type is either
7501 -- inherited, constrained to equal some new discriminant of the derived
7502 -- type, or constrained to the value of an expression.
7504 -- When inherited or constrained to equal some new discriminant, the
7505 -- parent discriminant and the discriminant of the derived type are said
7508 -- If a discriminant of the parent type is constrained to a specific value
7509 -- in the derived type definition, then the discriminant is said to be
7510 -- "specified" by that derived type definition.
7512 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
7514 -- We have spoken about stored discriminants in point 1 (introduction)
7515 -- above. There are two sort of stored discriminants: implicit and
7516 -- explicit. As long as the derived type inherits the same discriminants as
7517 -- the root record type, stored discriminants are the same as regular
7518 -- discriminants, and are said to be implicit. However, if any discriminant
7519 -- in the root type was renamed in the derived type, then the derived
7520 -- type will contain explicit stored discriminants. Explicit stored
7521 -- discriminants are discriminants in addition to the semantically visible
7522 -- discriminants defined for the derived type. Stored discriminants are
7523 -- used by Gigi to figure out what are the physical discriminants in
7524 -- objects of the derived type (see precise definition in einfo.ads).
7525 -- As an example, consider the following:
7527 -- type R (D1, D2, D3 : Int) is record ... end record;
7528 -- type T1 is new R;
7529 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
7530 -- type T3 is new T2;
7531 -- type T4 (Y : Int) is new T3 (Y, 99);
7533 -- The following table summarizes the discriminants and stored
7534 -- discriminants in R and T1 through T4.
7536 -- Type Discrim Stored Discrim Comment
7537 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
7538 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
7539 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
7540 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
7541 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
7543 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
7544 -- find the corresponding discriminant in the parent type, while
7545 -- Original_Record_Component (abbreviated ORC below), the actual physical
7546 -- component that is renamed. Finally the field Is_Completely_Hidden
7547 -- (abbreviated ICH below) is set for all explicit stored discriminants
7548 -- (see einfo.ads for more info). For the above example this gives:
7550 -- Discrim CD ORC ICH
7551 -- ^^^^^^^ ^^ ^^^ ^^^
7552 -- D1 in R empty itself no
7553 -- D2 in R empty itself no
7554 -- D3 in R empty itself no
7556 -- D1 in T1 D1 in R itself no
7557 -- D2 in T1 D2 in R itself no
7558 -- D3 in T1 D3 in R itself no
7560 -- X1 in T2 D3 in T1 D3 in T2 no
7561 -- X2 in T2 D1 in T1 D1 in T2 no
7562 -- D1 in T2 empty itself yes
7563 -- D2 in T2 empty itself yes
7564 -- D3 in T2 empty itself yes
7566 -- X1 in T3 X1 in T2 D3 in T3 no
7567 -- X2 in T3 X2 in T2 D1 in T3 no
7568 -- D1 in T3 empty itself yes
7569 -- D2 in T3 empty itself yes
7570 -- D3 in T3 empty itself yes
7572 -- Y in T4 X1 in T3 D3 in T3 no
7573 -- D1 in T3 empty itself yes
7574 -- D2 in T3 empty itself yes
7575 -- D3 in T3 empty itself yes
7577 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
7579 -- Type derivation for tagged types is fairly straightforward. If no
7580 -- discriminants are specified by the derived type, these are inherited
7581 -- from the parent. No explicit stored discriminants are ever necessary.
7582 -- The only manipulation that is done to the tree is that of adding a
7583 -- _parent field with parent type and constrained to the same constraint
7584 -- specified for the parent in the derived type definition. For instance:
7586 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
7587 -- type T1 is new R with null record;
7588 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
7590 -- are changed into:
7592 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
7593 -- _parent : R (D1, D2, D3);
7596 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
7597 -- _parent : T1 (X2, 88, X1);
7600 -- The discriminants actually present in R, T1 and T2 as well as their CD,
7601 -- ORC and ICH fields are:
7603 -- Discrim CD ORC ICH
7604 -- ^^^^^^^ ^^ ^^^ ^^^
7605 -- D1 in R empty itself no
7606 -- D2 in R empty itself no
7607 -- D3 in R empty itself no
7609 -- D1 in T1 D1 in R D1 in R no
7610 -- D2 in T1 D2 in R D2 in R no
7611 -- D3 in T1 D3 in R D3 in R no
7613 -- X1 in T2 D3 in T1 D3 in R no
7614 -- X2 in T2 D1 in T1 D1 in R no
7616 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
7618 -- Regardless of whether we dealing with a tagged or untagged type
7619 -- we will transform all derived type declarations of the form
7621 -- type T is new R (...) [with ...];
7623 -- subtype S is R (...);
7624 -- type T is new S [with ...];
7626 -- type BT is new R [with ...];
7627 -- subtype T is BT (...);
7629 -- That is, the base derived type is constrained only if it has no
7630 -- discriminants. The reason for doing this is that GNAT's semantic model
7631 -- assumes that a base type with discriminants is unconstrained.
7633 -- Note that, strictly speaking, the above transformation is not always
7634 -- correct. Consider for instance the following excerpt from ACVC b34011a:
7636 -- procedure B34011A is
7637 -- type REC (D : integer := 0) is record
7642 -- type T6 is new Rec;
7643 -- function F return T6;
7648 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
7651 -- The definition of Q6.U is illegal. However transforming Q6.U into
7653 -- type BaseU is new T6;
7654 -- subtype U is BaseU (Q6.F.I)
7656 -- turns U into a legal subtype, which is incorrect. To avoid this problem
7657 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
7658 -- the transformation described above.
7660 -- There is another instance where the above transformation is incorrect.
7664 -- type Base (D : Integer) is tagged null record;
7665 -- procedure P (X : Base);
7667 -- type Der is new Base (2) with null record;
7668 -- procedure P (X : Der);
7671 -- Then the above transformation turns this into
7673 -- type Der_Base is new Base with null record;
7674 -- -- procedure P (X : Base) is implicitly inherited here
7675 -- -- as procedure P (X : Der_Base).
7677 -- subtype Der is Der_Base (2);
7678 -- procedure P (X : Der);
7679 -- -- The overriding of P (X : Der_Base) is illegal since we
7680 -- -- have a parameter conformance problem.
7682 -- To get around this problem, after having semantically processed Der_Base
7683 -- and the rewritten subtype declaration for Der, we copy Der_Base field
7684 -- Discriminant_Constraint from Der so that when parameter conformance is
7685 -- checked when P is overridden, no semantic errors are flagged.
7687 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
7689 -- Regardless of whether we are dealing with a tagged or untagged type
7690 -- we will transform all derived type declarations of the form
7692 -- type R (D1, .., Dn : ...) is [tagged] record ...;
7693 -- type T is new R [with ...];
7695 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
7697 -- The reason for such transformation is that it allows us to implement a
7698 -- very clean form of component inheritance as explained below.
7700 -- Note that this transformation is not achieved by direct tree rewriting
7701 -- and manipulation, but rather by redoing the semantic actions that the
7702 -- above transformation will entail. This is done directly in routine
7703 -- Inherit_Components.
7705 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
7707 -- In both tagged and untagged derived types, regular non discriminant
7708 -- components are inherited in the derived type from the parent type. In
7709 -- the absence of discriminants component, inheritance is straightforward
7710 -- as components can simply be copied from the parent.
7712 -- If the parent has discriminants, inheriting components constrained with
7713 -- these discriminants requires caution. Consider the following example:
7715 -- type R (D1, D2 : Positive) is [tagged] record
7716 -- S : String (D1 .. D2);
7719 -- type T1 is new R [with null record];
7720 -- type T2 (X : positive) is new R (1, X) [with null record];
7722 -- As explained in 6. above, T1 is rewritten as
7723 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
7724 -- which makes the treatment for T1 and T2 identical.
7726 -- What we want when inheriting S, is that references to D1 and D2 in R are
7727 -- replaced with references to their correct constraints, i.e. D1 and D2 in
7728 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
7729 -- with either discriminant references in the derived type or expressions.
7730 -- This replacement is achieved as follows: before inheriting R's
7731 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
7732 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
7733 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
7734 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
7735 -- by String (1 .. X).
7737 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
7739 -- We explain here the rules governing private type extensions relevant to
7740 -- type derivation. These rules are explained on the following example:
7742 -- type D [(...)] is new A [(...)] with private; <-- partial view
7743 -- type D [(...)] is new P [(...)] with null record; <-- full view
7745 -- Type A is called the ancestor subtype of the private extension.
7746 -- Type P is the parent type of the full view of the private extension. It
7747 -- must be A or a type derived from A.
7749 -- The rules concerning the discriminants of private type extensions are
7752 -- o If a private extension inherits known discriminants from the ancestor
7753 -- subtype, then the full view must also inherit its discriminants from
7754 -- the ancestor subtype and the parent subtype of the full view must be
7755 -- constrained if and only if the ancestor subtype is constrained.
7757 -- o If a partial view has unknown discriminants, then the full view may
7758 -- define a definite or an indefinite subtype, with or without
7761 -- o If a partial view has neither known nor unknown discriminants, then
7762 -- the full view must define a definite subtype.
7764 -- o If the ancestor subtype of a private extension has constrained
7765 -- discriminants, then the parent subtype of the full view must impose a
7766 -- statically matching constraint on those discriminants.
7768 -- This means that only the following forms of private extensions are
7771 -- type D is new A with private; <-- partial view
7772 -- type D is new P with null record; <-- full view
7774 -- If A has no discriminants than P has no discriminants, otherwise P must
7775 -- inherit A's discriminants.
7777 -- type D is new A (...) with private; <-- partial view
7778 -- type D is new P (:::) with null record; <-- full view
7780 -- P must inherit A's discriminants and (...) and (:::) must statically
7783 -- subtype A is R (...);
7784 -- type D is new A with private; <-- partial view
7785 -- type D is new P with null record; <-- full view
7787 -- P must have inherited R's discriminants and must be derived from A or
7788 -- any of its subtypes.
7790 -- type D (..) is new A with private; <-- partial view
7791 -- type D (..) is new P [(:::)] with null record; <-- full view
7793 -- No specific constraints on P's discriminants or constraint (:::).
7794 -- Note that A can be unconstrained, but the parent subtype P must either
7795 -- be constrained or (:::) must be present.
7797 -- type D (..) is new A [(...)] with private; <-- partial view
7798 -- type D (..) is new P [(:::)] with null record; <-- full view
7800 -- P's constraints on A's discriminants must statically match those
7801 -- imposed by (...).
7803 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
7805 -- The full view of a private extension is handled exactly as described
7806 -- above. The model chose for the private view of a private extension is
7807 -- the same for what concerns discriminants (i.e. they receive the same
7808 -- treatment as in the tagged case). However, the private view of the
7809 -- private extension always inherits the components of the parent base,
7810 -- without replacing any discriminant reference. Strictly speaking this is
7811 -- incorrect. However, Gigi never uses this view to generate code so this
7812 -- is a purely semantic issue. In theory, a set of transformations similar
7813 -- to those given in 5. and 6. above could be applied to private views of
7814 -- private extensions to have the same model of component inheritance as
7815 -- for non private extensions. However, this is not done because it would
7816 -- further complicate private type processing. Semantically speaking, this
7817 -- leaves us in an uncomfortable situation. As an example consider:
7820 -- type R (D : integer) is tagged record
7821 -- S : String (1 .. D);
7823 -- procedure P (X : R);
7824 -- type T is new R (1) with private;
7826 -- type T is new R (1) with null record;
7829 -- This is transformed into:
7832 -- type R (D : integer) is tagged record
7833 -- S : String (1 .. D);
7835 -- procedure P (X : R);
7836 -- type T is new R (1) with private;
7838 -- type BaseT is new R with null record;
7839 -- subtype T is BaseT (1);
7842 -- (strictly speaking the above is incorrect Ada)
7844 -- From the semantic standpoint the private view of private extension T
7845 -- should be flagged as constrained since one can clearly have
7849 -- in a unit withing Pack. However, when deriving subprograms for the
7850 -- private view of private extension T, T must be seen as unconstrained
7851 -- since T has discriminants (this is a constraint of the current
7852 -- subprogram derivation model). Thus, when processing the private view of
7853 -- a private extension such as T, we first mark T as unconstrained, we
7854 -- process it, we perform program derivation and just before returning from
7855 -- Build_Derived_Record_Type we mark T as constrained.
7857 -- ??? Are there are other uncomfortable cases that we will have to
7860 -- 10. RECORD_TYPE_WITH_PRIVATE complications
7862 -- Types that are derived from a visible record type and have a private
7863 -- extension present other peculiarities. They behave mostly like private
7864 -- types, but if they have primitive operations defined, these will not
7865 -- have the proper signatures for further inheritance, because other
7866 -- primitive operations will use the implicit base that we define for
7867 -- private derivations below. This affect subprogram inheritance (see
7868 -- Derive_Subprograms for details). We also derive the implicit base from
7869 -- the base type of the full view, so that the implicit base is a record
7870 -- type and not another private type, This avoids infinite loops.
7872 procedure Build_Derived_Record_Type
7874 Parent_Type : Entity_Id;
7875 Derived_Type : Entity_Id;
7876 Derive_Subps : Boolean := True)
7878 Discriminant_Specs : constant Boolean :=
7879 Present (Discriminant_Specifications (N));
7880 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
7881 Loc : constant Source_Ptr := Sloc (N);
7882 Private_Extension : constant Boolean :=
7883 Nkind (N) = N_Private_Extension_Declaration;
7884 Assoc_List : Elist_Id;
7885 Constraint_Present : Boolean;
7887 Discrim : Entity_Id;
7889 Inherit_Discrims : Boolean := False;
7890 Last_Discrim : Entity_Id;
7891 New_Base : Entity_Id;
7893 New_Discrs : Elist_Id;
7894 New_Indic : Node_Id;
7895 Parent_Base : Entity_Id;
7896 Save_Etype : Entity_Id;
7897 Save_Discr_Constr : Elist_Id;
7898 Save_Next_Entity : Entity_Id;
7901 Discs : Elist_Id := New_Elmt_List;
7902 -- An empty Discs list means that there were no constraints in the
7903 -- subtype indication or that there was an error processing it.
7906 if Ekind (Parent_Type) = E_Record_Type_With_Private
7907 and then Present (Full_View (Parent_Type))
7908 and then Has_Discriminants (Parent_Type)
7910 Parent_Base := Base_Type (Full_View (Parent_Type));
7912 Parent_Base := Base_Type (Parent_Type);
7915 -- AI05-0115 : if this is a derivation from a private type in some
7916 -- other scope that may lead to invisible components for the derived
7917 -- type, mark it accordingly.
7919 if Is_Private_Type (Parent_Type) then
7920 if Scope (Parent_Type) = Scope (Derived_Type) then
7923 elsif In_Open_Scopes (Scope (Parent_Type))
7924 and then In_Private_Part (Scope (Parent_Type))
7929 Set_Has_Private_Ancestor (Derived_Type);
7933 Set_Has_Private_Ancestor
7934 (Derived_Type, Has_Private_Ancestor (Parent_Type));
7937 -- Before we start the previously documented transformations, here is
7938 -- little fix for size and alignment of tagged types. Normally when we
7939 -- derive type D from type P, we copy the size and alignment of P as the
7940 -- default for D, and in the absence of explicit representation clauses
7941 -- for D, the size and alignment are indeed the same as the parent.
7943 -- But this is wrong for tagged types, since fields may be added, and
7944 -- the default size may need to be larger, and the default alignment may
7945 -- need to be larger.
7947 -- We therefore reset the size and alignment fields in the tagged case.
7948 -- Note that the size and alignment will in any case be at least as
7949 -- large as the parent type (since the derived type has a copy of the
7950 -- parent type in the _parent field)
7952 -- The type is also marked as being tagged here, which is needed when
7953 -- processing components with a self-referential anonymous access type
7954 -- in the call to Check_Anonymous_Access_Components below. Note that
7955 -- this flag is also set later on for completeness.
7958 Set_Is_Tagged_Type (Derived_Type);
7959 Init_Size_Align (Derived_Type);
7962 -- STEP 0a: figure out what kind of derived type declaration we have
7964 if Private_Extension then
7966 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
7967 Set_Default_SSO (Derived_Type);
7970 Type_Def := Type_Definition (N);
7972 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
7973 -- Parent_Base can be a private type or private extension. However,
7974 -- for tagged types with an extension the newly added fields are
7975 -- visible and hence the Derived_Type is always an E_Record_Type.
7976 -- (except that the parent may have its own private fields).
7977 -- For untagged types we preserve the Ekind of the Parent_Base.
7979 if Present (Record_Extension_Part (Type_Def)) then
7980 Set_Ekind (Derived_Type, E_Record_Type);
7981 Set_Default_SSO (Derived_Type);
7983 -- Create internal access types for components with anonymous
7986 if Ada_Version >= Ada_2005 then
7987 Check_Anonymous_Access_Components
7988 (N, Derived_Type, Derived_Type,
7989 Component_List (Record_Extension_Part (Type_Def)));
7993 Set_Ekind (Derived_Type, Ekind (Parent_Base));
7997 -- Indic can either be an N_Identifier if the subtype indication
7998 -- contains no constraint or an N_Subtype_Indication if the subtype
7999 -- indication has a constraint.
8001 Indic := Subtype_Indication (Type_Def);
8002 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8004 -- Check that the type has visible discriminants. The type may be
8005 -- a private type with unknown discriminants whose full view has
8006 -- discriminants which are invisible.
8008 if Constraint_Present then
8009 if not Has_Discriminants (Parent_Base)
8011 (Has_Unknown_Discriminants (Parent_Base)
8012 and then Is_Private_Type (Parent_Base))
8015 ("invalid constraint: type has no discriminant",
8016 Constraint (Indic));
8018 Constraint_Present := False;
8019 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8021 elsif Is_Constrained (Parent_Type) then
8023 ("invalid constraint: parent type is already constrained",
8024 Constraint (Indic));
8026 Constraint_Present := False;
8027 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8031 -- STEP 0b: If needed, apply transformation given in point 5. above
8033 if not Private_Extension
8034 and then Has_Discriminants (Parent_Type)
8035 and then not Discriminant_Specs
8036 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8038 -- First, we must analyze the constraint (see comment in point 5.)
8039 -- The constraint may come from the subtype indication of the full
8042 if Constraint_Present then
8043 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8045 -- If there is no explicit constraint, there might be one that is
8046 -- inherited from a constrained parent type. In that case verify that
8047 -- it conforms to the constraint in the partial view. In perverse
8048 -- cases the parent subtypes of the partial and full view can have
8049 -- different constraints.
8051 elsif Present (Stored_Constraint (Parent_Type)) then
8052 New_Discrs := Stored_Constraint (Parent_Type);
8055 New_Discrs := No_Elist;
8058 if Has_Discriminants (Derived_Type)
8059 and then Has_Private_Declaration (Derived_Type)
8060 and then Present (Discriminant_Constraint (Derived_Type))
8061 and then Present (New_Discrs)
8063 -- Verify that constraints of the full view statically match
8064 -- those given in the partial view.
8070 C1 := First_Elmt (New_Discrs);
8071 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8072 while Present (C1) and then Present (C2) loop
8073 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8075 (Is_OK_Static_Expression (Node (C1))
8076 and then Is_OK_Static_Expression (Node (C2))
8078 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8083 if Constraint_Present then
8085 ("constraint not conformant to previous declaration",
8089 ("constraint of full view is incompatible "
8090 & "with partial view", N);
8100 -- Insert and analyze the declaration for the unconstrained base type
8102 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8105 Make_Full_Type_Declaration (Loc,
8106 Defining_Identifier => New_Base,
8108 Make_Derived_Type_Definition (Loc,
8109 Abstract_Present => Abstract_Present (Type_Def),
8110 Limited_Present => Limited_Present (Type_Def),
8111 Subtype_Indication =>
8112 New_Occurrence_Of (Parent_Base, Loc),
8113 Record_Extension_Part =>
8114 Relocate_Node (Record_Extension_Part (Type_Def)),
8115 Interface_List => Interface_List (Type_Def)));
8117 Set_Parent (New_Decl, Parent (N));
8118 Mark_Rewrite_Insertion (New_Decl);
8119 Insert_Before (N, New_Decl);
8121 -- In the extension case, make sure ancestor is frozen appropriately
8122 -- (see also non-discriminated case below).
8124 if Present (Record_Extension_Part (Type_Def))
8125 or else Is_Interface (Parent_Base)
8127 Freeze_Before (New_Decl, Parent_Type);
8130 -- Note that this call passes False for the Derive_Subps parameter
8131 -- because subprogram derivation is deferred until after creating
8132 -- the subtype (see below).
8135 (New_Decl, Parent_Base, New_Base,
8136 Is_Completion => False, Derive_Subps => False);
8138 -- ??? This needs re-examination to determine whether the
8139 -- above call can simply be replaced by a call to Analyze.
8141 Set_Analyzed (New_Decl);
8143 -- Insert and analyze the declaration for the constrained subtype
8145 if Constraint_Present then
8147 Make_Subtype_Indication (Loc,
8148 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8149 Constraint => Relocate_Node (Constraint (Indic)));
8153 Constr_List : constant List_Id := New_List;
8158 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8159 while Present (C) loop
8162 -- It is safe here to call New_Copy_Tree since we called
8163 -- Force_Evaluation on each constraint previously
8164 -- in Build_Discriminant_Constraints.
8166 Append (New_Copy_Tree (Expr), To => Constr_List);
8172 Make_Subtype_Indication (Loc,
8173 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8175 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8180 Make_Subtype_Declaration (Loc,
8181 Defining_Identifier => Derived_Type,
8182 Subtype_Indication => New_Indic));
8186 -- Derivation of subprograms must be delayed until the full subtype
8187 -- has been established, to ensure proper overriding of subprograms
8188 -- inherited by full types. If the derivations occurred as part of
8189 -- the call to Build_Derived_Type above, then the check for type
8190 -- conformance would fail because earlier primitive subprograms
8191 -- could still refer to the full type prior the change to the new
8192 -- subtype and hence would not match the new base type created here.
8193 -- Subprograms are not derived, however, when Derive_Subps is False
8194 -- (since otherwise there could be redundant derivations).
8196 if Derive_Subps then
8197 Derive_Subprograms (Parent_Type, Derived_Type);
8200 -- For tagged types the Discriminant_Constraint of the new base itype
8201 -- is inherited from the first subtype so that no subtype conformance
8202 -- problem arise when the first subtype overrides primitive
8203 -- operations inherited by the implicit base type.
8206 Set_Discriminant_Constraint
8207 (New_Base, Discriminant_Constraint (Derived_Type));
8213 -- If we get here Derived_Type will have no discriminants or it will be
8214 -- a discriminated unconstrained base type.
8216 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8220 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8221 -- The declaration of a specific descendant of an interface type
8222 -- freezes the interface type (RM 13.14).
8224 if not Private_Extension or else Is_Interface (Parent_Base) then
8225 Freeze_Before (N, Parent_Type);
8228 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8229 -- cannot be declared at a deeper level than its parent type is
8230 -- removed. The check on derivation within a generic body is also
8231 -- relaxed, but there's a restriction that a derived tagged type
8232 -- cannot be declared in a generic body if it's derived directly
8233 -- or indirectly from a formal type of that generic.
8235 if Ada_Version >= Ada_2005 then
8236 if Present (Enclosing_Generic_Body (Derived_Type)) then
8238 Ancestor_Type : Entity_Id;
8241 -- Check to see if any ancestor of the derived type is a
8244 Ancestor_Type := Parent_Type;
8245 while not Is_Generic_Type (Ancestor_Type)
8246 and then Etype (Ancestor_Type) /= Ancestor_Type
8248 Ancestor_Type := Etype (Ancestor_Type);
8251 -- If the derived type does have a formal type as an
8252 -- ancestor, then it's an error if the derived type is
8253 -- declared within the body of the generic unit that
8254 -- declares the formal type in its generic formal part. It's
8255 -- sufficient to check whether the ancestor type is declared
8256 -- inside the same generic body as the derived type (such as
8257 -- within a nested generic spec), in which case the
8258 -- derivation is legal. If the formal type is declared
8259 -- outside of that generic body, then it's guaranteed that
8260 -- the derived type is declared within the generic body of
8261 -- the generic unit declaring the formal type.
8263 if Is_Generic_Type (Ancestor_Type)
8264 and then Enclosing_Generic_Body (Ancestor_Type) /=
8265 Enclosing_Generic_Body (Derived_Type)
8268 ("parent type of& must not be descendant of formal type"
8269 & " of an enclosing generic body",
8270 Indic, Derived_Type);
8275 elsif Type_Access_Level (Derived_Type) /=
8276 Type_Access_Level (Parent_Type)
8277 and then not Is_Generic_Type (Derived_Type)
8279 if Is_Controlled (Parent_Type) then
8281 ("controlled type must be declared at the library level",
8285 ("type extension at deeper accessibility level than parent",
8291 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8294 and then GB /= Enclosing_Generic_Body (Parent_Base)
8297 ("parent type of& must not be outside generic body"
8299 Indic, Derived_Type);
8305 -- Ada 2005 (AI-251)
8307 if Ada_Version >= Ada_2005 and then Is_Tagged then
8309 -- "The declaration of a specific descendant of an interface type
8310 -- freezes the interface type" (RM 13.14).
8315 if Is_Non_Empty_List (Interface_List (Type_Def)) then
8316 Iface := First (Interface_List (Type_Def));
8317 while Present (Iface) loop
8318 Freeze_Before (N, Etype (Iface));
8325 -- STEP 1b : preliminary cleanup of the full view of private types
8327 -- If the type is already marked as having discriminants, then it's the
8328 -- completion of a private type or private extension and we need to
8329 -- retain the discriminants from the partial view if the current
8330 -- declaration has Discriminant_Specifications so that we can verify
8331 -- conformance. However, we must remove any existing components that
8332 -- were inherited from the parent (and attached in Copy_And_Swap)
8333 -- because the full type inherits all appropriate components anyway, and
8334 -- we do not want the partial view's components interfering.
8336 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
8337 Discrim := First_Discriminant (Derived_Type);
8339 Last_Discrim := Discrim;
8340 Next_Discriminant (Discrim);
8341 exit when No (Discrim);
8344 Set_Last_Entity (Derived_Type, Last_Discrim);
8346 -- In all other cases wipe out the list of inherited components (even
8347 -- inherited discriminants), it will be properly rebuilt here.
8350 Set_First_Entity (Derived_Type, Empty);
8351 Set_Last_Entity (Derived_Type, Empty);
8354 -- STEP 1c: Initialize some flags for the Derived_Type
8356 -- The following flags must be initialized here so that
8357 -- Process_Discriminants can check that discriminants of tagged types do
8358 -- not have a default initial value and that access discriminants are
8359 -- only specified for limited records. For completeness, these flags are
8360 -- also initialized along with all the other flags below.
8362 -- AI-419: Limitedness is not inherited from an interface parent, so to
8363 -- be limited in that case the type must be explicitly declared as
8364 -- limited. However, task and protected interfaces are always limited.
8366 if Limited_Present (Type_Def) then
8367 Set_Is_Limited_Record (Derived_Type);
8369 elsif Is_Limited_Record (Parent_Type)
8370 or else (Present (Full_View (Parent_Type))
8371 and then Is_Limited_Record (Full_View (Parent_Type)))
8373 if not Is_Interface (Parent_Type)
8374 or else Is_Synchronized_Interface (Parent_Type)
8375 or else Is_Protected_Interface (Parent_Type)
8376 or else Is_Task_Interface (Parent_Type)
8378 Set_Is_Limited_Record (Derived_Type);
8382 -- STEP 2a: process discriminants of derived type if any
8384 Push_Scope (Derived_Type);
8386 if Discriminant_Specs then
8387 Set_Has_Unknown_Discriminants (Derived_Type, False);
8389 -- The following call initializes fields Has_Discriminants and
8390 -- Discriminant_Constraint, unless we are processing the completion
8391 -- of a private type declaration.
8393 Check_Or_Process_Discriminants (N, Derived_Type);
8395 -- For untagged types, the constraint on the Parent_Type must be
8396 -- present and is used to rename the discriminants.
8398 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
8399 Error_Msg_N ("untagged parent must have discriminants", Indic);
8401 elsif not Is_Tagged and then not Constraint_Present then
8403 ("discriminant constraint needed for derived untagged records",
8406 -- Otherwise the parent subtype must be constrained unless we have a
8407 -- private extension.
8409 elsif not Constraint_Present
8410 and then not Private_Extension
8411 and then not Is_Constrained (Parent_Type)
8414 ("unconstrained type not allowed in this context", Indic);
8416 elsif Constraint_Present then
8417 -- The following call sets the field Corresponding_Discriminant
8418 -- for the discriminants in the Derived_Type.
8420 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
8422 -- For untagged types all new discriminants must rename
8423 -- discriminants in the parent. For private extensions new
8424 -- discriminants cannot rename old ones (implied by [7.3(13)]).
8426 Discrim := First_Discriminant (Derived_Type);
8427 while Present (Discrim) loop
8429 and then No (Corresponding_Discriminant (Discrim))
8432 ("new discriminants must constrain old ones", Discrim);
8434 elsif Private_Extension
8435 and then Present (Corresponding_Discriminant (Discrim))
8438 ("only static constraints allowed for parent"
8439 & " discriminants in the partial view", Indic);
8443 -- If a new discriminant is used in the constraint, then its
8444 -- subtype must be statically compatible with the parent
8445 -- discriminant's subtype (3.7(15)).
8447 -- However, if the record contains an array constrained by
8448 -- the discriminant but with some different bound, the compiler
8449 -- attemps to create a smaller range for the discriminant type.
8450 -- (See exp_ch3.Adjust_Discriminants). In this case, where
8451 -- the discriminant type is a scalar type, the check must use
8452 -- the original discriminant type in the parent declaration.
8455 Corr_Disc : constant Entity_Id :=
8456 Corresponding_Discriminant (Discrim);
8457 Disc_Type : constant Entity_Id := Etype (Discrim);
8458 Corr_Type : Entity_Id;
8461 if Present (Corr_Disc) then
8462 if Is_Scalar_Type (Disc_Type) then
8464 Entity (Discriminant_Type (Parent (Corr_Disc)));
8466 Corr_Type := Etype (Corr_Disc);
8470 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
8473 ("subtype must be compatible "
8474 & "with parent discriminant",
8480 Next_Discriminant (Discrim);
8483 -- Check whether the constraints of the full view statically
8484 -- match those imposed by the parent subtype [7.3(13)].
8486 if Present (Stored_Constraint (Derived_Type)) then
8491 C1 := First_Elmt (Discs);
8492 C2 := First_Elmt (Stored_Constraint (Derived_Type));
8493 while Present (C1) and then Present (C2) loop
8495 Fully_Conformant_Expressions (Node (C1), Node (C2))
8498 ("not conformant with previous declaration",
8509 -- STEP 2b: No new discriminants, inherit discriminants if any
8512 if Private_Extension then
8513 Set_Has_Unknown_Discriminants
8515 Has_Unknown_Discriminants (Parent_Type)
8516 or else Unknown_Discriminants_Present (N));
8518 -- The partial view of the parent may have unknown discriminants,
8519 -- but if the full view has discriminants and the parent type is
8520 -- in scope they must be inherited.
8522 elsif Has_Unknown_Discriminants (Parent_Type)
8524 (not Has_Discriminants (Parent_Type)
8525 or else not In_Open_Scopes (Scope (Parent_Type)))
8527 Set_Has_Unknown_Discriminants (Derived_Type);
8530 if not Has_Unknown_Discriminants (Derived_Type)
8531 and then not Has_Unknown_Discriminants (Parent_Base)
8532 and then Has_Discriminants (Parent_Type)
8534 Inherit_Discrims := True;
8535 Set_Has_Discriminants
8536 (Derived_Type, True);
8537 Set_Discriminant_Constraint
8538 (Derived_Type, Discriminant_Constraint (Parent_Base));
8541 -- The following test is true for private types (remember
8542 -- transformation 5. is not applied to those) and in an error
8545 if Constraint_Present then
8546 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
8549 -- For now mark a new derived type as constrained only if it has no
8550 -- discriminants. At the end of Build_Derived_Record_Type we properly
8551 -- set this flag in the case of private extensions. See comments in
8552 -- point 9. just before body of Build_Derived_Record_Type.
8556 not (Inherit_Discrims
8557 or else Has_Unknown_Discriminants (Derived_Type)));
8560 -- STEP 3: initialize fields of derived type
8562 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
8563 Set_Stored_Constraint (Derived_Type, No_Elist);
8565 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
8566 -- but cannot be interfaces
8568 if not Private_Extension
8569 and then Ekind (Derived_Type) /= E_Private_Type
8570 and then Ekind (Derived_Type) /= E_Limited_Private_Type
8572 if Interface_Present (Type_Def) then
8573 Analyze_Interface_Declaration (Derived_Type, Type_Def);
8576 Set_Interfaces (Derived_Type, No_Elist);
8579 -- Fields inherited from the Parent_Type
8581 Set_Has_Specified_Layout
8582 (Derived_Type, Has_Specified_Layout (Parent_Type));
8583 Set_Is_Limited_Composite
8584 (Derived_Type, Is_Limited_Composite (Parent_Type));
8585 Set_Is_Private_Composite
8586 (Derived_Type, Is_Private_Composite (Parent_Type));
8588 if Is_Tagged_Type (Parent_Type) then
8589 Set_No_Tagged_Streams_Pragma
8590 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8593 -- Fields inherited from the Parent_Base
8595 Set_Has_Controlled_Component
8596 (Derived_Type, Has_Controlled_Component (Parent_Base));
8597 Set_Has_Non_Standard_Rep
8598 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8599 Set_Has_Primitive_Operations
8600 (Derived_Type, Has_Primitive_Operations (Parent_Base));
8602 -- Fields inherited from the Parent_Base in the non-private case
8604 if Ekind (Derived_Type) = E_Record_Type then
8605 Set_Has_Complex_Representation
8606 (Derived_Type, Has_Complex_Representation (Parent_Base));
8609 -- Fields inherited from the Parent_Base for record types
8611 if Is_Record_Type (Derived_Type) then
8613 Parent_Full : Entity_Id;
8616 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8617 -- Parent_Base can be a private type or private extension. Go
8618 -- to the full view here to get the E_Record_Type specific flags.
8620 if Present (Full_View (Parent_Base)) then
8621 Parent_Full := Full_View (Parent_Base);
8623 Parent_Full := Parent_Base;
8626 Set_OK_To_Reorder_Components
8627 (Derived_Type, OK_To_Reorder_Components (Parent_Full));
8631 -- Set fields for private derived types
8633 if Is_Private_Type (Derived_Type) then
8634 Set_Depends_On_Private (Derived_Type, True);
8635 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8637 -- Inherit fields from non private record types. If this is the
8638 -- completion of a derivation from a private type, the parent itself
8639 -- is private, and the attributes come from its full view, which must
8643 if Is_Private_Type (Parent_Base)
8644 and then not Is_Record_Type (Parent_Base)
8646 Set_Component_Alignment
8647 (Derived_Type, Component_Alignment (Full_View (Parent_Base)));
8649 (Derived_Type, C_Pass_By_Copy (Full_View (Parent_Base)));
8651 Set_Component_Alignment
8652 (Derived_Type, Component_Alignment (Parent_Base));
8654 (Derived_Type, C_Pass_By_Copy (Parent_Base));
8658 -- Set fields for tagged types
8661 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
8663 -- All tagged types defined in Ada.Finalization are controlled
8665 if Chars (Scope (Derived_Type)) = Name_Finalization
8666 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
8667 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
8669 Set_Is_Controlled (Derived_Type);
8671 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Base));
8674 -- Minor optimization: there is no need to generate the class-wide
8675 -- entity associated with an underlying record view.
8677 if not Is_Underlying_Record_View (Derived_Type) then
8678 Make_Class_Wide_Type (Derived_Type);
8681 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
8683 if Has_Discriminants (Derived_Type)
8684 and then Constraint_Present
8686 Set_Stored_Constraint
8687 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
8690 if Ada_Version >= Ada_2005 then
8692 Ifaces_List : Elist_Id;
8695 -- Checks rules 3.9.4 (13/2 and 14/2)
8697 if Comes_From_Source (Derived_Type)
8698 and then not Is_Private_Type (Derived_Type)
8699 and then Is_Interface (Parent_Type)
8700 and then not Is_Interface (Derived_Type)
8702 if Is_Task_Interface (Parent_Type) then
8704 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
8707 elsif Is_Protected_Interface (Parent_Type) then
8709 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
8714 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
8716 Check_Interfaces (N, Type_Def);
8718 -- Ada 2005 (AI-251): Collect the list of progenitors that are
8719 -- not already in the parents.
8723 Ifaces_List => Ifaces_List,
8724 Exclude_Parents => True);
8726 Set_Interfaces (Derived_Type, Ifaces_List);
8728 -- If the derived type is the anonymous type created for
8729 -- a declaration whose parent has a constraint, propagate
8730 -- the interface list to the source type. This must be done
8731 -- prior to the completion of the analysis of the source type
8732 -- because the components in the extension may contain current
8733 -- instances whose legality depends on some ancestor.
8735 if Is_Itype (Derived_Type) then
8737 Def : constant Node_Id :=
8738 Associated_Node_For_Itype (Derived_Type);
8741 and then Nkind (Def) = N_Full_Type_Declaration
8744 (Defining_Identifier (Def), Ifaces_List);
8749 -- Propagate inherited invariant information of parents
8752 if Ada_Version >= Ada_2012
8753 and then not Is_Interface (Derived_Type)
8755 if Has_Inheritable_Invariants (Parent_Type) then
8756 Set_Has_Invariants (Derived_Type);
8757 Set_Has_Inheritable_Invariants (Derived_Type);
8759 elsif not Is_Empty_Elmt_List (Ifaces_List) then
8764 AI := First_Elmt (Ifaces_List);
8765 while Present (AI) loop
8766 if Has_Inheritable_Invariants (Node (AI)) then
8767 Set_Has_Invariants (Derived_Type);
8768 Set_Has_Inheritable_Invariants (Derived_Type);
8779 -- A type extension is automatically Ghost when one of its
8780 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
8781 -- also inherited when the parent type is Ghost, but this is
8782 -- done in Build_Derived_Type as the mechanism also handles
8783 -- untagged derivations.
8785 if Implements_Ghost_Interface (Derived_Type) then
8786 Set_Is_Ghost_Entity (Derived_Type);
8792 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Base));
8793 Set_Has_Non_Standard_Rep
8794 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
8797 -- STEP 4: Inherit components from the parent base and constrain them.
8798 -- Apply the second transformation described in point 6. above.
8800 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
8801 or else not Has_Discriminants (Parent_Type)
8802 or else not Is_Constrained (Parent_Type)
8806 Constrs := Discriminant_Constraint (Parent_Type);
8811 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
8813 -- STEP 5a: Copy the parent record declaration for untagged types
8815 if not Is_Tagged then
8817 -- Discriminant_Constraint (Derived_Type) has been properly
8818 -- constructed. Save it and temporarily set it to Empty because we
8819 -- do not want the call to New_Copy_Tree below to mess this list.
8821 if Has_Discriminants (Derived_Type) then
8822 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
8823 Set_Discriminant_Constraint (Derived_Type, No_Elist);
8825 Save_Discr_Constr := No_Elist;
8828 -- Save the Etype field of Derived_Type. It is correctly set now,
8829 -- but the call to New_Copy tree may remap it to point to itself,
8830 -- which is not what we want. Ditto for the Next_Entity field.
8832 Save_Etype := Etype (Derived_Type);
8833 Save_Next_Entity := Next_Entity (Derived_Type);
8835 -- Assoc_List maps all stored discriminants in the Parent_Base to
8836 -- stored discriminants in the Derived_Type. It is fundamental that
8837 -- no types or itypes with discriminants other than the stored
8838 -- discriminants appear in the entities declared inside
8839 -- Derived_Type, since the back end cannot deal with it.
8843 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
8845 -- Restore the fields saved prior to the New_Copy_Tree call
8846 -- and compute the stored constraint.
8848 Set_Etype (Derived_Type, Save_Etype);
8849 Set_Next_Entity (Derived_Type, Save_Next_Entity);
8851 if Has_Discriminants (Derived_Type) then
8852 Set_Discriminant_Constraint
8853 (Derived_Type, Save_Discr_Constr);
8854 Set_Stored_Constraint
8855 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
8856 Replace_Components (Derived_Type, New_Decl);
8857 Set_Has_Implicit_Dereference
8858 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
8861 -- Insert the new derived type declaration
8863 Rewrite (N, New_Decl);
8865 -- STEP 5b: Complete the processing for record extensions in generics
8867 -- There is no completion for record extensions declared in the
8868 -- parameter part of a generic, so we need to complete processing for
8869 -- these generic record extensions here. The Record_Type_Definition call
8870 -- will change the Ekind of the components from E_Void to E_Component.
8872 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
8873 Record_Type_Definition (Empty, Derived_Type);
8875 -- STEP 5c: Process the record extension for non private tagged types
8877 elsif not Private_Extension then
8878 Expand_Record_Extension (Derived_Type, Type_Def);
8880 -- Note : previously in ASIS mode we set the Parent_Subtype of the
8881 -- derived type to propagate some semantic information. This led
8882 -- to other ASIS failures and has been removed.
8884 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
8885 -- implemented interfaces if we are in expansion mode
8888 and then Has_Interfaces (Derived_Type)
8890 Add_Interface_Tag_Components (N, Derived_Type);
8893 -- Analyze the record extension
8895 Record_Type_Definition
8896 (Record_Extension_Part (Type_Def), Derived_Type);
8901 -- Nothing else to do if there is an error in the derivation.
8902 -- An unusual case: the full view may be derived from a type in an
8903 -- instance, when the partial view was used illegally as an actual
8904 -- in that instance, leading to a circular definition.
8906 if Etype (Derived_Type) = Any_Type
8907 or else Etype (Parent_Type) = Derived_Type
8912 -- Set delayed freeze and then derive subprograms, we need to do
8913 -- this in this order so that derived subprograms inherit the
8914 -- derived freeze if necessary.
8916 Set_Has_Delayed_Freeze (Derived_Type);
8918 if Derive_Subps then
8919 Derive_Subprograms (Parent_Type, Derived_Type);
8922 -- If we have a private extension which defines a constrained derived
8923 -- type mark as constrained here after we have derived subprograms. See
8924 -- comment on point 9. just above the body of Build_Derived_Record_Type.
8926 if Private_Extension and then Inherit_Discrims then
8927 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
8928 Set_Is_Constrained (Derived_Type, True);
8929 Set_Discriminant_Constraint (Derived_Type, Discs);
8931 elsif Is_Constrained (Parent_Type) then
8933 (Derived_Type, True);
8934 Set_Discriminant_Constraint
8935 (Derived_Type, Discriminant_Constraint (Parent_Type));
8939 -- Update the class-wide type, which shares the now-completed entity
8940 -- list with its specific type. In case of underlying record views,
8941 -- we do not generate the corresponding class wide entity.
8944 and then not Is_Underlying_Record_View (Derived_Type)
8947 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
8949 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
8952 Check_Function_Writable_Actuals (N);
8953 end Build_Derived_Record_Type;
8955 ------------------------
8956 -- Build_Derived_Type --
8957 ------------------------
8959 procedure Build_Derived_Type
8961 Parent_Type : Entity_Id;
8962 Derived_Type : Entity_Id;
8963 Is_Completion : Boolean;
8964 Derive_Subps : Boolean := True)
8966 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
8969 -- Set common attributes
8971 Set_Scope (Derived_Type, Current_Scope);
8973 Set_Etype (Derived_Type, Parent_Base);
8974 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8975 Set_Has_Task (Derived_Type, Has_Task (Parent_Base));
8976 Set_Has_Protected (Derived_Type, Has_Protected (Parent_Base));
8978 Set_Size_Info (Derived_Type, Parent_Type);
8979 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
8980 Set_Is_Controlled (Derived_Type, Is_Controlled (Parent_Type));
8981 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
8982 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
8984 if Is_Tagged_Type (Derived_Type) then
8985 Set_No_Tagged_Streams_Pragma
8986 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
8989 -- If the parent has primitive routines, set the derived type link
8991 if Has_Primitive_Operations (Parent_Type) then
8992 Set_Derived_Type_Link (Parent_Base, Derived_Type);
8995 -- If the parent type is a private subtype, the convention on the base
8996 -- type may be set in the private part, and not propagated to the
8997 -- subtype until later, so we obtain the convention from the base type.
8999 Set_Convention (Derived_Type, Convention (Parent_Base));
9001 -- Set SSO default for record or array type
9003 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9004 and then Is_Base_Type (Derived_Type)
9006 Set_Default_SSO (Derived_Type);
9009 -- Propagate invariant information. The new type has invariants if
9010 -- they are inherited from the parent type, and these invariants can
9011 -- be further inherited, so both flags are set.
9013 -- We similarly inherit predicates
9015 if Has_Predicates (Parent_Type) then
9016 Set_Has_Predicates (Derived_Type);
9019 -- The derived type inherits the representation clauses of the parent
9021 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9023 -- Propagate the attributes related to pragma Default_Initial_Condition
9024 -- from the parent type to the private extension. A derived type always
9025 -- inherits the default initial condition flag from the parent type. If
9026 -- the derived type carries its own Default_Initial_Condition pragma,
9027 -- the flag is later reset in Analyze_Pragma. Note that both flags are
9028 -- mutually exclusive.
9030 Propagate_Default_Init_Cond_Attributes
9031 (From_Typ => Parent_Type,
9032 To_Typ => Derived_Type,
9033 Parent_To_Derivation => True);
9035 -- If the parent type has delayed rep aspects, then mark the derived
9036 -- type as possibly inheriting a delayed rep aspect.
9038 if Has_Delayed_Rep_Aspects (Parent_Type) then
9039 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9042 -- Propagate the attributes related to pragma Ghost from the parent type
9043 -- to the derived type or type extension (SPARK RM 6.9(9)).
9045 if Is_Ghost_Entity (Parent_Type) then
9046 Set_Is_Ghost_Entity (Derived_Type);
9049 -- Type dependent processing
9051 case Ekind (Parent_Type) is
9052 when Numeric_Kind =>
9053 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9056 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9060 | Class_Wide_Kind =>
9061 Build_Derived_Record_Type
9062 (N, Parent_Type, Derived_Type, Derive_Subps);
9065 when Enumeration_Kind =>
9066 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9069 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9071 when Incomplete_Or_Private_Kind =>
9072 Build_Derived_Private_Type
9073 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9075 -- For discriminated types, the derivation includes deriving
9076 -- primitive operations. For others it is done below.
9078 if Is_Tagged_Type (Parent_Type)
9079 or else Has_Discriminants (Parent_Type)
9080 or else (Present (Full_View (Parent_Type))
9081 and then Has_Discriminants (Full_View (Parent_Type)))
9086 when Concurrent_Kind =>
9087 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9090 raise Program_Error;
9093 -- Nothing more to do if some error occurred
9095 if Etype (Derived_Type) = Any_Type then
9099 -- Set delayed freeze and then derive subprograms, we need to do this
9100 -- in this order so that derived subprograms inherit the derived freeze
9103 Set_Has_Delayed_Freeze (Derived_Type);
9105 if Derive_Subps then
9106 Derive_Subprograms (Parent_Type, Derived_Type);
9109 Set_Has_Primitive_Operations
9110 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9111 end Build_Derived_Type;
9113 -----------------------
9114 -- Build_Discriminal --
9115 -----------------------
9117 procedure Build_Discriminal (Discrim : Entity_Id) is
9118 D_Minal : Entity_Id;
9119 CR_Disc : Entity_Id;
9122 -- A discriminal has the same name as the discriminant
9124 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9126 Set_Ekind (D_Minal, E_In_Parameter);
9127 Set_Mechanism (D_Minal, Default_Mechanism);
9128 Set_Etype (D_Minal, Etype (Discrim));
9129 Set_Scope (D_Minal, Current_Scope);
9131 Set_Discriminal (Discrim, D_Minal);
9132 Set_Discriminal_Link (D_Minal, Discrim);
9134 -- For task types, build at once the discriminants of the corresponding
9135 -- record, which are needed if discriminants are used in entry defaults
9136 -- and in family bounds.
9138 if Is_Concurrent_Type (Current_Scope)
9140 Is_Limited_Type (Current_Scope)
9142 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9144 Set_Ekind (CR_Disc, E_In_Parameter);
9145 Set_Mechanism (CR_Disc, Default_Mechanism);
9146 Set_Etype (CR_Disc, Etype (Discrim));
9147 Set_Scope (CR_Disc, Current_Scope);
9148 Set_Discriminal_Link (CR_Disc, Discrim);
9149 Set_CR_Discriminant (Discrim, CR_Disc);
9151 end Build_Discriminal;
9153 ------------------------------------
9154 -- Build_Discriminant_Constraints --
9155 ------------------------------------
9157 function Build_Discriminant_Constraints
9160 Derived_Def : Boolean := False) return Elist_Id
9162 C : constant Node_Id := Constraint (Def);
9163 Nb_Discr : constant Nat := Number_Discriminants (T);
9165 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9166 -- Saves the expression corresponding to a given discriminant in T
9168 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9169 -- Return the Position number within array Discr_Expr of a discriminant
9170 -- D within the discriminant list of the discriminated type T.
9172 procedure Process_Discriminant_Expression
9175 -- If this is a discriminant constraint on a partial view, do not
9176 -- generate an overflow check on the discriminant expression. The check
9177 -- will be generated when constraining the full view. Otherwise the
9178 -- backend creates duplicate symbols for the temporaries corresponding
9179 -- to the expressions to be checked, causing spurious assembler errors.
9185 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9189 Disc := First_Discriminant (T);
9190 for J in Discr_Expr'Range loop
9195 Next_Discriminant (Disc);
9198 -- Note: Since this function is called on discriminants that are
9199 -- known to belong to the discriminated type, falling through the
9200 -- loop with no match signals an internal compiler error.
9202 raise Program_Error;
9205 -------------------------------------
9206 -- Process_Discriminant_Expression --
9207 -------------------------------------
9209 procedure Process_Discriminant_Expression
9213 BDT : constant Entity_Id := Base_Type (Etype (D));
9216 -- If this is a discriminant constraint on a partial view, do
9217 -- not generate an overflow on the discriminant expression. The
9218 -- check will be generated when constraining the full view.
9220 if Is_Private_Type (T)
9221 and then Present (Full_View (T))
9223 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9225 Analyze_And_Resolve (Expr, BDT);
9227 end Process_Discriminant_Expression;
9229 -- Declarations local to Build_Discriminant_Constraints
9233 Elist : constant Elist_Id := New_Elmt_List;
9241 Discrim_Present : Boolean := False;
9243 -- Start of processing for Build_Discriminant_Constraints
9246 -- The following loop will process positional associations only.
9247 -- For a positional association, the (single) discriminant is
9248 -- implicitly specified by position, in textual order (RM 3.7.2).
9250 Discr := First_Discriminant (T);
9251 Constr := First (Constraints (C));
9252 for D in Discr_Expr'Range loop
9253 exit when Nkind (Constr) = N_Discriminant_Association;
9256 Error_Msg_N ("too few discriminants given in constraint", C);
9257 return New_Elmt_List;
9259 elsif Nkind (Constr) = N_Range
9260 or else (Nkind (Constr) = N_Attribute_Reference
9261 and then Attribute_Name (Constr) = Name_Range)
9264 ("a range is not a valid discriminant constraint", Constr);
9265 Discr_Expr (D) := Error;
9268 Process_Discriminant_Expression (Constr, Discr);
9269 Discr_Expr (D) := Constr;
9272 Next_Discriminant (Discr);
9276 if No (Discr) and then Present (Constr) then
9277 Error_Msg_N ("too many discriminants given in constraint", Constr);
9278 return New_Elmt_List;
9281 -- Named associations can be given in any order, but if both positional
9282 -- and named associations are used in the same discriminant constraint,
9283 -- then positional associations must occur first, at their normal
9284 -- position. Hence once a named association is used, the rest of the
9285 -- discriminant constraint must use only named associations.
9287 while Present (Constr) loop
9289 -- Positional association forbidden after a named association
9291 if Nkind (Constr) /= N_Discriminant_Association then
9292 Error_Msg_N ("positional association follows named one", Constr);
9293 return New_Elmt_List;
9295 -- Otherwise it is a named association
9298 -- E records the type of the discriminants in the named
9299 -- association. All the discriminants specified in the same name
9300 -- association must have the same type.
9304 -- Search the list of discriminants in T to see if the simple name
9305 -- given in the constraint matches any of them.
9307 Id := First (Selector_Names (Constr));
9308 while Present (Id) loop
9311 -- If Original_Discriminant is present, we are processing a
9312 -- generic instantiation and this is an instance node. We need
9313 -- to find the name of the corresponding discriminant in the
9314 -- actual record type T and not the name of the discriminant in
9315 -- the generic formal. Example:
9318 -- type G (D : int) is private;
9320 -- subtype W is G (D => 1);
9322 -- type Rec (X : int) is record ... end record;
9323 -- package Q is new P (G => Rec);
9325 -- At the point of the instantiation, formal type G is Rec
9326 -- and therefore when reanalyzing "subtype W is G (D => 1);"
9327 -- which really looks like "subtype W is Rec (D => 1);" at
9328 -- the point of instantiation, we want to find the discriminant
9329 -- that corresponds to D in Rec, i.e. X.
9331 if Present (Original_Discriminant (Id))
9332 and then In_Instance
9334 Discr := Find_Corresponding_Discriminant (Id, T);
9338 Discr := First_Discriminant (T);
9339 while Present (Discr) loop
9340 if Chars (Discr) = Chars (Id) then
9345 Next_Discriminant (Discr);
9349 Error_Msg_N ("& does not match any discriminant", Id);
9350 return New_Elmt_List;
9352 -- If the parent type is a generic formal, preserve the
9353 -- name of the discriminant for subsequent instances.
9354 -- see comment at the beginning of this if statement.
9356 elsif Is_Generic_Type (Root_Type (T)) then
9357 Set_Original_Discriminant (Id, Discr);
9361 Position := Pos_Of_Discr (T, Discr);
9363 if Present (Discr_Expr (Position)) then
9364 Error_Msg_N ("duplicate constraint for discriminant&", Id);
9367 -- Each discriminant specified in the same named association
9368 -- must be associated with a separate copy of the
9369 -- corresponding expression.
9371 if Present (Next (Id)) then
9372 Expr := New_Copy_Tree (Expression (Constr));
9373 Set_Parent (Expr, Parent (Expression (Constr)));
9375 Expr := Expression (Constr);
9378 Discr_Expr (Position) := Expr;
9379 Process_Discriminant_Expression (Expr, Discr);
9382 -- A discriminant association with more than one discriminant
9383 -- name is only allowed if the named discriminants are all of
9384 -- the same type (RM 3.7.1(8)).
9387 E := Base_Type (Etype (Discr));
9389 elsif Base_Type (Etype (Discr)) /= E then
9391 ("all discriminants in an association " &
9392 "must have the same type", Id);
9402 -- A discriminant constraint must provide exactly one value for each
9403 -- discriminant of the type (RM 3.7.1(8)).
9405 for J in Discr_Expr'Range loop
9406 if No (Discr_Expr (J)) then
9407 Error_Msg_N ("too few discriminants given in constraint", C);
9408 return New_Elmt_List;
9412 -- Determine if there are discriminant expressions in the constraint
9414 for J in Discr_Expr'Range loop
9415 if Denotes_Discriminant
9416 (Discr_Expr (J), Check_Concurrent => True)
9418 Discrim_Present := True;
9422 -- Build an element list consisting of the expressions given in the
9423 -- discriminant constraint and apply the appropriate checks. The list
9424 -- is constructed after resolving any named discriminant associations
9425 -- and therefore the expressions appear in the textual order of the
9428 Discr := First_Discriminant (T);
9429 for J in Discr_Expr'Range loop
9430 if Discr_Expr (J) /= Error then
9431 Append_Elmt (Discr_Expr (J), Elist);
9433 -- If any of the discriminant constraints is given by a
9434 -- discriminant and we are in a derived type declaration we
9435 -- have a discriminant renaming. Establish link between new
9436 -- and old discriminant.
9438 if Denotes_Discriminant (Discr_Expr (J)) then
9440 Set_Corresponding_Discriminant
9441 (Entity (Discr_Expr (J)), Discr);
9444 -- Force the evaluation of non-discriminant expressions.
9445 -- If we have found a discriminant in the constraint 3.4(26)
9446 -- and 3.8(18) demand that no range checks are performed are
9447 -- after evaluation. If the constraint is for a component
9448 -- definition that has a per-object constraint, expressions are
9449 -- evaluated but not checked either. In all other cases perform
9453 if Discrim_Present then
9456 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
9458 Has_Per_Object_Constraint
9459 (Defining_Identifier (Parent (Parent (Def))))
9463 elsif Is_Access_Type (Etype (Discr)) then
9464 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
9467 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
9470 Force_Evaluation (Discr_Expr (J));
9473 -- Check that the designated type of an access discriminant's
9474 -- expression is not a class-wide type unless the discriminant's
9475 -- designated type is also class-wide.
9477 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
9478 and then not Is_Class_Wide_Type
9479 (Designated_Type (Etype (Discr)))
9480 and then Etype (Discr_Expr (J)) /= Any_Type
9481 and then Is_Class_Wide_Type
9482 (Designated_Type (Etype (Discr_Expr (J))))
9484 Wrong_Type (Discr_Expr (J), Etype (Discr));
9486 elsif Is_Access_Type (Etype (Discr))
9487 and then not Is_Access_Constant (Etype (Discr))
9488 and then Is_Access_Type (Etype (Discr_Expr (J)))
9489 and then Is_Access_Constant (Etype (Discr_Expr (J)))
9492 ("constraint for discriminant& must be access to variable",
9497 Next_Discriminant (Discr);
9501 end Build_Discriminant_Constraints;
9503 ---------------------------------
9504 -- Build_Discriminated_Subtype --
9505 ---------------------------------
9507 procedure Build_Discriminated_Subtype
9511 Related_Nod : Node_Id;
9512 For_Access : Boolean := False)
9514 Has_Discrs : constant Boolean := Has_Discriminants (T);
9515 Constrained : constant Boolean :=
9517 and then not Is_Empty_Elmt_List (Elist)
9518 and then not Is_Class_Wide_Type (T))
9519 or else Is_Constrained (T);
9522 if Ekind (T) = E_Record_Type then
9524 Set_Ekind (Def_Id, E_Private_Subtype);
9525 Set_Is_For_Access_Subtype (Def_Id, True);
9527 Set_Ekind (Def_Id, E_Record_Subtype);
9530 -- Inherit preelaboration flag from base, for types for which it
9531 -- may have been set: records, private types, protected types.
9533 Set_Known_To_Have_Preelab_Init
9534 (Def_Id, Known_To_Have_Preelab_Init (T));
9536 elsif Ekind (T) = E_Task_Type then
9537 Set_Ekind (Def_Id, E_Task_Subtype);
9539 elsif Ekind (T) = E_Protected_Type then
9540 Set_Ekind (Def_Id, E_Protected_Subtype);
9541 Set_Known_To_Have_Preelab_Init
9542 (Def_Id, Known_To_Have_Preelab_Init (T));
9544 elsif Is_Private_Type (T) then
9545 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
9546 Set_Known_To_Have_Preelab_Init
9547 (Def_Id, Known_To_Have_Preelab_Init (T));
9549 -- Private subtypes may have private dependents
9551 Set_Private_Dependents (Def_Id, New_Elmt_List);
9553 elsif Is_Class_Wide_Type (T) then
9554 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
9557 -- Incomplete type. Attach subtype to list of dependents, to be
9558 -- completed with full view of parent type, unless is it the
9559 -- designated subtype of a record component within an init_proc.
9560 -- This last case arises for a component of an access type whose
9561 -- designated type is incomplete (e.g. a Taft Amendment type).
9562 -- The designated subtype is within an inner scope, and needs no
9563 -- elaboration, because only the access type is needed in the
9564 -- initialization procedure.
9566 Set_Ekind (Def_Id, Ekind (T));
9568 if For_Access and then Within_Init_Proc then
9571 Append_Elmt (Def_Id, Private_Dependents (T));
9575 Set_Etype (Def_Id, T);
9576 Init_Size_Align (Def_Id);
9577 Set_Has_Discriminants (Def_Id, Has_Discrs);
9578 Set_Is_Constrained (Def_Id, Constrained);
9580 Set_First_Entity (Def_Id, First_Entity (T));
9581 Set_Last_Entity (Def_Id, Last_Entity (T));
9582 Set_Has_Implicit_Dereference
9583 (Def_Id, Has_Implicit_Dereference (T));
9585 -- If the subtype is the completion of a private declaration, there may
9586 -- have been representation clauses for the partial view, and they must
9587 -- be preserved. Build_Derived_Type chains the inherited clauses with
9588 -- the ones appearing on the extension. If this comes from a subtype
9589 -- declaration, all clauses are inherited.
9591 if No (First_Rep_Item (Def_Id)) then
9592 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
9595 if Is_Tagged_Type (T) then
9596 Set_Is_Tagged_Type (Def_Id);
9597 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
9598 Make_Class_Wide_Type (Def_Id);
9601 Set_Stored_Constraint (Def_Id, No_Elist);
9604 Set_Discriminant_Constraint (Def_Id, Elist);
9605 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
9608 if Is_Tagged_Type (T) then
9610 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
9611 -- concurrent record type (which has the list of primitive
9614 if Ada_Version >= Ada_2005
9615 and then Is_Concurrent_Type (T)
9617 Set_Corresponding_Record_Type (Def_Id,
9618 Corresponding_Record_Type (T));
9620 Set_Direct_Primitive_Operations (Def_Id,
9621 Direct_Primitive_Operations (T));
9624 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
9627 -- Subtypes introduced by component declarations do not need to be
9628 -- marked as delayed, and do not get freeze nodes, because the semantics
9629 -- verifies that the parents of the subtypes are frozen before the
9630 -- enclosing record is frozen.
9632 if not Is_Type (Scope (Def_Id)) then
9633 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
9635 if Is_Private_Type (T)
9636 and then Present (Full_View (T))
9638 Conditional_Delay (Def_Id, Full_View (T));
9640 Conditional_Delay (Def_Id, T);
9644 if Is_Record_Type (T) then
9645 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
9648 and then not Is_Empty_Elmt_List (Elist)
9649 and then not For_Access
9651 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
9652 elsif not For_Access then
9653 Set_Cloned_Subtype (Def_Id, T);
9656 end Build_Discriminated_Subtype;
9658 ---------------------------
9659 -- Build_Itype_Reference --
9660 ---------------------------
9662 procedure Build_Itype_Reference
9666 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
9669 -- Itype references are only created for use by the back-end
9671 if Inside_A_Generic then
9674 Set_Itype (IR, Ityp);
9675 Insert_After (Nod, IR);
9677 end Build_Itype_Reference;
9679 ------------------------
9680 -- Build_Scalar_Bound --
9681 ------------------------
9683 function Build_Scalar_Bound
9686 Der_T : Entity_Id) return Node_Id
9688 New_Bound : Entity_Id;
9691 -- Note: not clear why this is needed, how can the original bound
9692 -- be unanalyzed at this point? and if it is, what business do we
9693 -- have messing around with it? and why is the base type of the
9694 -- parent type the right type for the resolution. It probably is
9695 -- not. It is OK for the new bound we are creating, but not for
9696 -- the old one??? Still if it never happens, no problem.
9698 Analyze_And_Resolve (Bound, Base_Type (Par_T));
9700 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
9701 New_Bound := New_Copy (Bound);
9702 Set_Etype (New_Bound, Der_T);
9703 Set_Analyzed (New_Bound);
9705 elsif Is_Entity_Name (Bound) then
9706 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
9708 -- The following is almost certainly wrong. What business do we have
9709 -- relocating a node (Bound) that is presumably still attached to
9710 -- the tree elsewhere???
9713 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
9716 Set_Etype (New_Bound, Der_T);
9718 end Build_Scalar_Bound;
9720 --------------------------------
9721 -- Build_Underlying_Full_View --
9722 --------------------------------
9724 procedure Build_Underlying_Full_View
9729 Loc : constant Source_Ptr := Sloc (N);
9730 Subt : constant Entity_Id :=
9731 Make_Defining_Identifier
9732 (Loc, New_External_Name (Chars (Typ), 'S'));
9739 procedure Set_Discriminant_Name (Id : Node_Id);
9740 -- If the derived type has discriminants, they may rename discriminants
9741 -- of the parent. When building the full view of the parent, we need to
9742 -- recover the names of the original discriminants if the constraint is
9743 -- given by named associations.
9745 ---------------------------
9746 -- Set_Discriminant_Name --
9747 ---------------------------
9749 procedure Set_Discriminant_Name (Id : Node_Id) is
9753 Set_Original_Discriminant (Id, Empty);
9755 if Has_Discriminants (Typ) then
9756 Disc := First_Discriminant (Typ);
9757 while Present (Disc) loop
9758 if Chars (Disc) = Chars (Id)
9759 and then Present (Corresponding_Discriminant (Disc))
9761 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
9763 Next_Discriminant (Disc);
9766 end Set_Discriminant_Name;
9768 -- Start of processing for Build_Underlying_Full_View
9771 if Nkind (N) = N_Full_Type_Declaration then
9772 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
9774 elsif Nkind (N) = N_Subtype_Declaration then
9775 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
9777 elsif Nkind (N) = N_Component_Declaration then
9780 (Constraint (Subtype_Indication (Component_Definition (N))));
9783 raise Program_Error;
9786 C := First (Constraints (Constr));
9787 while Present (C) loop
9788 if Nkind (C) = N_Discriminant_Association then
9789 Id := First (Selector_Names (C));
9790 while Present (Id) loop
9791 Set_Discriminant_Name (Id);
9800 Make_Subtype_Declaration (Loc,
9801 Defining_Identifier => Subt,
9802 Subtype_Indication =>
9803 Make_Subtype_Indication (Loc,
9804 Subtype_Mark => New_Occurrence_Of (Par, Loc),
9805 Constraint => New_Copy_Tree (Constr)));
9807 -- If this is a component subtype for an outer itype, it is not
9808 -- a list member, so simply set the parent link for analysis: if
9809 -- the enclosing type does not need to be in a declarative list,
9810 -- neither do the components.
9812 if Is_List_Member (N)
9813 and then Nkind (N) /= N_Component_Declaration
9815 Insert_Before (N, Indic);
9817 Set_Parent (Indic, Parent (N));
9821 Set_Underlying_Full_View (Typ, Full_View (Subt));
9822 end Build_Underlying_Full_View;
9824 -------------------------------
9825 -- Check_Abstract_Overriding --
9826 -------------------------------
9828 procedure Check_Abstract_Overriding (T : Entity_Id) is
9829 Alias_Subp : Entity_Id;
9835 procedure Check_Pragma_Implemented (Subp : Entity_Id);
9836 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
9837 -- which has pragma Implemented already set. Check whether Subp's entity
9838 -- kind conforms to the implementation kind of the overridden routine.
9840 procedure Check_Pragma_Implemented
9842 Iface_Subp : Entity_Id);
9843 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
9844 -- Iface_Subp and both entities have pragma Implemented already set on
9845 -- them. Check whether the two implementation kinds are conforming.
9847 procedure Inherit_Pragma_Implemented
9849 Iface_Subp : Entity_Id);
9850 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
9851 -- subprogram Iface_Subp which has been marked by pragma Implemented.
9852 -- Propagate the implementation kind of Iface_Subp to Subp.
9854 ------------------------------
9855 -- Check_Pragma_Implemented --
9856 ------------------------------
9858 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
9859 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
9860 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
9861 Subp_Alias : constant Entity_Id := Alias (Subp);
9862 Contr_Typ : Entity_Id;
9863 Impl_Subp : Entity_Id;
9866 -- Subp must have an alias since it is a hidden entity used to link
9867 -- an interface subprogram to its overriding counterpart.
9869 pragma Assert (Present (Subp_Alias));
9871 -- Handle aliases to synchronized wrappers
9873 Impl_Subp := Subp_Alias;
9875 if Is_Primitive_Wrapper (Impl_Subp) then
9876 Impl_Subp := Wrapped_Entity (Impl_Subp);
9879 -- Extract the type of the controlling formal
9881 Contr_Typ := Etype (First_Formal (Subp_Alias));
9883 if Is_Concurrent_Record_Type (Contr_Typ) then
9884 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
9887 -- An interface subprogram whose implementation kind is By_Entry must
9888 -- be implemented by an entry.
9890 if Impl_Kind = Name_By_Entry
9891 and then Ekind (Impl_Subp) /= E_Entry
9893 Error_Msg_Node_2 := Iface_Alias;
9895 ("type & must implement abstract subprogram & with an entry",
9896 Subp_Alias, Contr_Typ);
9898 elsif Impl_Kind = Name_By_Protected_Procedure then
9900 -- An interface subprogram whose implementation kind is By_
9901 -- Protected_Procedure cannot be implemented by a primitive
9902 -- procedure of a task type.
9904 if Ekind (Contr_Typ) /= E_Protected_Type then
9905 Error_Msg_Node_2 := Contr_Typ;
9907 ("interface subprogram & cannot be implemented by a " &
9908 "primitive procedure of task type &", Subp_Alias,
9911 -- An interface subprogram whose implementation kind is By_
9912 -- Protected_Procedure must be implemented by a procedure.
9914 elsif Ekind (Impl_Subp) /= E_Procedure then
9915 Error_Msg_Node_2 := Iface_Alias;
9917 ("type & must implement abstract subprogram & with a " &
9918 "procedure", Subp_Alias, Contr_Typ);
9920 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9921 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9923 Error_Msg_Name_1 := Impl_Kind;
9925 ("overriding operation& must have synchronization%",
9929 -- If primitive has Optional synchronization, overriding operation
9930 -- must match if it has an explicit synchronization..
9932 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
9933 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
9935 Error_Msg_Name_1 := Impl_Kind;
9937 ("overriding operation& must have syncrhonization%",
9940 end Check_Pragma_Implemented;
9942 ------------------------------
9943 -- Check_Pragma_Implemented --
9944 ------------------------------
9946 procedure Check_Pragma_Implemented
9948 Iface_Subp : Entity_Id)
9950 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9951 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
9954 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
9955 -- and overriding subprogram are different. In general this is an
9956 -- error except when the implementation kind of the overridden
9957 -- subprograms is By_Any or Optional.
9959 if Iface_Kind /= Subp_Kind
9960 and then Iface_Kind /= Name_By_Any
9961 and then Iface_Kind /= Name_Optional
9963 if Iface_Kind = Name_By_Entry then
9965 ("incompatible implementation kind, overridden subprogram " &
9966 "is marked By_Entry", Subp);
9969 ("incompatible implementation kind, overridden subprogram " &
9970 "is marked By_Protected_Procedure", Subp);
9973 end Check_Pragma_Implemented;
9975 --------------------------------
9976 -- Inherit_Pragma_Implemented --
9977 --------------------------------
9979 procedure Inherit_Pragma_Implemented
9981 Iface_Subp : Entity_Id)
9983 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
9984 Loc : constant Source_Ptr := Sloc (Subp);
9985 Impl_Prag : Node_Id;
9988 -- Since the implementation kind is stored as a representation item
9989 -- rather than a flag, create a pragma node.
9993 Chars => Name_Implemented,
9994 Pragma_Argument_Associations => New_List (
9995 Make_Pragma_Argument_Association (Loc,
9996 Expression => New_Occurrence_Of (Subp, Loc)),
9998 Make_Pragma_Argument_Association (Loc,
9999 Expression => Make_Identifier (Loc, Iface_Kind))));
10001 -- The pragma doesn't need to be analyzed because it is internally
10002 -- built. It is safe to directly register it as a rep item since we
10003 -- are only interested in the characters of the implementation kind.
10005 Record_Rep_Item (Subp, Impl_Prag);
10006 end Inherit_Pragma_Implemented;
10008 -- Start of processing for Check_Abstract_Overriding
10011 Op_List := Primitive_Operations (T);
10013 -- Loop to check primitive operations
10015 Elmt := First_Elmt (Op_List);
10016 while Present (Elmt) loop
10017 Subp := Node (Elmt);
10018 Alias_Subp := Alias (Subp);
10020 -- Inherited subprograms are identified by the fact that they do not
10021 -- come from source, and the associated source location is the
10022 -- location of the first subtype of the derived type.
10024 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10025 -- subprograms that "require overriding".
10027 -- Special exception, do not complain about failure to override the
10028 -- stream routines _Input and _Output, as well as the primitive
10029 -- operations used in dispatching selects since we always provide
10030 -- automatic overridings for these subprograms.
10032 -- Also ignore this rule for convention CIL since .NET libraries
10033 -- do bizarre things with interfaces???
10035 -- The partial view of T may have been a private extension, for
10036 -- which inherited functions dispatching on result are abstract.
10037 -- If the full view is a null extension, there is no need for
10038 -- overriding in Ada 2005, but wrappers need to be built for them
10039 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10041 if Is_Null_Extension (T)
10042 and then Has_Controlling_Result (Subp)
10043 and then Ada_Version >= Ada_2005
10044 and then Present (Alias_Subp)
10045 and then not Comes_From_Source (Subp)
10046 and then not Is_Abstract_Subprogram (Alias_Subp)
10047 and then not Is_Access_Type (Etype (Subp))
10051 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10052 -- processing because this check is done with the aliased
10055 elsif Present (Interface_Alias (Subp)) then
10058 elsif (Is_Abstract_Subprogram (Subp)
10059 or else Requires_Overriding (Subp)
10061 (Has_Controlling_Result (Subp)
10062 and then Present (Alias_Subp)
10063 and then not Comes_From_Source (Subp)
10064 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10065 and then not Is_TSS (Subp, TSS_Stream_Input)
10066 and then not Is_TSS (Subp, TSS_Stream_Output)
10067 and then not Is_Abstract_Type (T)
10068 and then Convention (T) /= Convention_CIL
10069 and then not Is_Predefined_Interface_Primitive (Subp)
10071 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10072 -- with abstract interface types because the check will be done
10073 -- with the aliased entity (otherwise we generate a duplicated
10076 and then not Present (Interface_Alias (Subp))
10078 if Present (Alias_Subp) then
10080 -- Only perform the check for a derived subprogram when the
10081 -- type has an explicit record extension. This avoids incorrect
10082 -- flagging of abstract subprograms for the case of a type
10083 -- without an extension that is derived from a formal type
10084 -- with a tagged actual (can occur within a private part).
10086 -- Ada 2005 (AI-391): In the case of an inherited function with
10087 -- a controlling result of the type, the rule does not apply if
10088 -- the type is a null extension (unless the parent function
10089 -- itself is abstract, in which case the function must still be
10090 -- be overridden). The expander will generate an overriding
10091 -- wrapper function calling the parent subprogram (see
10092 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10094 Type_Def := Type_Definition (Parent (T));
10096 if Nkind (Type_Def) = N_Derived_Type_Definition
10097 and then Present (Record_Extension_Part (Type_Def))
10099 (Ada_Version < Ada_2005
10100 or else not Is_Null_Extension (T)
10101 or else Ekind (Subp) = E_Procedure
10102 or else not Has_Controlling_Result (Subp)
10103 or else Is_Abstract_Subprogram (Alias_Subp)
10104 or else Requires_Overriding (Subp)
10105 or else Is_Access_Type (Etype (Subp)))
10107 -- Avoid reporting error in case of abstract predefined
10108 -- primitive inherited from interface type because the
10109 -- body of internally generated predefined primitives
10110 -- of tagged types are generated later by Freeze_Type
10112 if Is_Interface (Root_Type (T))
10113 and then Is_Abstract_Subprogram (Subp)
10114 and then Is_Predefined_Dispatching_Operation (Subp)
10115 and then not Comes_From_Source (Ultimate_Alias (Subp))
10119 -- A null extension is not obliged to override an inherited
10120 -- procedure subject to pragma Extensions_Visible with value
10121 -- False and at least one controlling OUT parameter
10122 -- (SPARK RM 6.1.7(6)).
10124 elsif Is_Null_Extension (T)
10125 and then Is_EVF_Procedure (Subp)
10131 ("type must be declared abstract or & overridden",
10134 -- Traverse the whole chain of aliased subprograms to
10135 -- complete the error notification. This is especially
10136 -- useful for traceability of the chain of entities when
10137 -- the subprogram corresponds with an interface
10138 -- subprogram (which may be defined in another package).
10140 if Present (Alias_Subp) then
10146 while Present (Alias (E)) loop
10148 -- Avoid reporting redundant errors on entities
10149 -- inherited from interfaces
10151 if Sloc (E) /= Sloc (T) then
10152 Error_Msg_Sloc := Sloc (E);
10154 ("\& has been inherited #", T, Subp);
10160 Error_Msg_Sloc := Sloc (E);
10162 -- AI05-0068: report if there is an overriding
10163 -- non-abstract subprogram that is invisible.
10166 and then not Is_Abstract_Subprogram (E)
10169 ("\& subprogram# is not visible",
10172 -- Clarify the case where a non-null extension must
10173 -- override inherited procedure subject to pragma
10174 -- Extensions_Visible with value False and at least
10175 -- one controlling OUT param.
10177 elsif Is_EVF_Procedure (E) then
10179 ("\& # is subject to Extensions_Visible False",
10184 ("\& has been inherited from subprogram #",
10191 -- Ada 2005 (AI-345): Protected or task type implementing
10192 -- abstract interfaces.
10194 elsif Is_Concurrent_Record_Type (T)
10195 and then Present (Interfaces (T))
10197 -- There is no need to check here RM 9.4(11.9/3) since we
10198 -- are processing the corresponding record type and the
10199 -- mode of the overriding subprograms was verified by
10200 -- Check_Conformance when the corresponding concurrent
10201 -- type declaration was analyzed.
10204 ("interface subprogram & must be overridden", T, Subp);
10206 -- Examine primitive operations of synchronized type to find
10207 -- homonyms that have the wrong profile.
10213 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10214 while Present (Prim) loop
10215 if Chars (Prim) = Chars (Subp) then
10217 ("profile is not type conformant with prefixed "
10218 & "view profile of inherited operation&",
10222 Next_Entity (Prim);
10228 Error_Msg_Node_2 := T;
10230 ("abstract subprogram& not allowed for type&", Subp);
10232 -- Also post unconditional warning on the type (unconditional
10233 -- so that if there are more than one of these cases, we get
10234 -- them all, and not just the first one).
10236 Error_Msg_Node_2 := Subp;
10237 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10240 -- A subprogram subject to pragma Extensions_Visible with value
10241 -- "True" cannot override a subprogram subject to the same pragma
10242 -- with value "False" (SPARK RM 6.1.7(5)).
10244 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10245 and then Present (Overridden_Operation (Subp))
10246 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10247 Extensions_Visible_False
10249 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10251 ("subprogram & with Extensions_Visible True cannot override "
10252 & "subprogram # with Extensions_Visible False", Subp);
10255 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10257 -- Subp is an expander-generated procedure which maps an interface
10258 -- alias to a protected wrapper. The interface alias is flagged by
10259 -- pragma Implemented. Ensure that Subp is a procedure when the
10260 -- implementation kind is By_Protected_Procedure or an entry when
10263 if Ada_Version >= Ada_2012
10264 and then Is_Hidden (Subp)
10265 and then Present (Interface_Alias (Subp))
10266 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10268 Check_Pragma_Implemented (Subp);
10271 -- Subp is an interface primitive which overrides another interface
10272 -- primitive marked with pragma Implemented.
10274 if Ada_Version >= Ada_2012
10275 and then Present (Overridden_Operation (Subp))
10276 and then Has_Rep_Pragma
10277 (Overridden_Operation (Subp), Name_Implemented)
10279 -- If the overriding routine is also marked by Implemented, check
10280 -- that the two implementation kinds are conforming.
10282 if Has_Rep_Pragma (Subp, Name_Implemented) then
10283 Check_Pragma_Implemented
10285 Iface_Subp => Overridden_Operation (Subp));
10287 -- Otherwise the overriding routine inherits the implementation
10288 -- kind from the overridden subprogram.
10291 Inherit_Pragma_Implemented
10293 Iface_Subp => Overridden_Operation (Subp));
10297 -- If the operation is a wrapper for a synchronized primitive, it
10298 -- may be called indirectly through a dispatching select. We assume
10299 -- that it will be referenced elsewhere indirectly, and suppress
10300 -- warnings about an unused entity.
10302 if Is_Primitive_Wrapper (Subp)
10303 and then Present (Wrapped_Entity (Subp))
10305 Set_Referenced (Wrapped_Entity (Subp));
10310 end Check_Abstract_Overriding;
10312 ------------------------------------------------
10313 -- Check_Access_Discriminant_Requires_Limited --
10314 ------------------------------------------------
10316 procedure Check_Access_Discriminant_Requires_Limited
10321 -- A discriminant_specification for an access discriminant shall appear
10322 -- only in the declaration for a task or protected type, or for a type
10323 -- with the reserved word 'limited' in its definition or in one of its
10324 -- ancestors (RM 3.7(10)).
10326 -- AI-0063: The proper condition is that type must be immutably limited,
10327 -- or else be a partial view.
10329 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10330 if Is_Limited_View (Current_Scope)
10332 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10333 and then Limited_Present (Parent (Current_Scope)))
10339 ("access discriminants allowed only for limited types", Loc);
10342 end Check_Access_Discriminant_Requires_Limited;
10344 -----------------------------------
10345 -- Check_Aliased_Component_Types --
10346 -----------------------------------
10348 procedure Check_Aliased_Component_Types (T : Entity_Id) is
10352 -- ??? Also need to check components of record extensions, but not
10353 -- components of protected types (which are always limited).
10355 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
10356 -- types to be unconstrained. This is safe because it is illegal to
10357 -- create access subtypes to such types with explicit discriminant
10360 if not Is_Limited_Type (T) then
10361 if Ekind (T) = E_Record_Type then
10362 C := First_Component (T);
10363 while Present (C) loop
10365 and then Has_Discriminants (Etype (C))
10366 and then not Is_Constrained (Etype (C))
10367 and then not In_Instance_Body
10368 and then Ada_Version < Ada_2005
10371 ("aliased component must be constrained (RM 3.6(11))",
10375 Next_Component (C);
10378 elsif Ekind (T) = E_Array_Type then
10379 if Has_Aliased_Components (T)
10380 and then Has_Discriminants (Component_Type (T))
10381 and then not Is_Constrained (Component_Type (T))
10382 and then not In_Instance_Body
10383 and then Ada_Version < Ada_2005
10386 ("aliased component type must be constrained (RM 3.6(11))",
10391 end Check_Aliased_Component_Types;
10393 ---------------------------------------
10394 -- Check_Anonymous_Access_Components --
10395 ---------------------------------------
10397 procedure Check_Anonymous_Access_Components
10398 (Typ_Decl : Node_Id;
10401 Comp_List : Node_Id)
10403 Loc : constant Source_Ptr := Sloc (Typ_Decl);
10404 Anon_Access : Entity_Id;
10407 Comp_Def : Node_Id;
10409 Type_Def : Node_Id;
10411 procedure Build_Incomplete_Type_Declaration;
10412 -- If the record type contains components that include an access to the
10413 -- current record, then create an incomplete type declaration for the
10414 -- record, to be used as the designated type of the anonymous access.
10415 -- This is done only once, and only if there is no previous partial
10416 -- view of the type.
10418 function Designates_T (Subt : Node_Id) return Boolean;
10419 -- Check whether a node designates the enclosing record type, or 'Class
10422 function Mentions_T (Acc_Def : Node_Id) return Boolean;
10423 -- Check whether an access definition includes a reference to
10424 -- the enclosing record type. The reference can be a subtype mark
10425 -- in the access definition itself, a 'Class attribute reference, or
10426 -- recursively a reference appearing in a parameter specification
10427 -- or result definition of an access_to_subprogram definition.
10429 --------------------------------------
10430 -- Build_Incomplete_Type_Declaration --
10431 --------------------------------------
10433 procedure Build_Incomplete_Type_Declaration is
10438 -- Is_Tagged indicates whether the type is tagged. It is tagged if
10439 -- it's "is new ... with record" or else "is tagged record ...".
10441 Is_Tagged : constant Boolean :=
10442 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
10444 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
10446 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
10447 and then Tagged_Present (Type_Definition (Typ_Decl)));
10450 -- If there is a previous partial view, no need to create a new one
10451 -- If the partial view, given by Prev, is incomplete, If Prev is
10452 -- a private declaration, full declaration is flagged accordingly.
10454 if Prev /= Typ then
10456 Make_Class_Wide_Type (Prev);
10457 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
10458 Set_Etype (Class_Wide_Type (Typ), Typ);
10463 elsif Has_Private_Declaration (Typ) then
10465 -- If we refer to T'Class inside T, and T is the completion of a
10466 -- private type, then make sure the class-wide type exists.
10469 Make_Class_Wide_Type (Typ);
10474 -- If there was a previous anonymous access type, the incomplete
10475 -- type declaration will have been created already.
10477 elsif Present (Current_Entity (Typ))
10478 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
10479 and then Full_View (Current_Entity (Typ)) = Typ
10482 and then Comes_From_Source (Current_Entity (Typ))
10483 and then not Is_Tagged_Type (Current_Entity (Typ))
10485 Make_Class_Wide_Type (Typ);
10487 ("incomplete view of tagged type should be declared tagged??",
10488 Parent (Current_Entity (Typ)));
10493 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
10494 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
10496 -- Type has already been inserted into the current scope. Remove
10497 -- it, and add incomplete declaration for type, so that subsequent
10498 -- anonymous access types can use it. The entity is unchained from
10499 -- the homonym list and from immediate visibility. After analysis,
10500 -- the entity in the incomplete declaration becomes immediately
10501 -- visible in the record declaration that follows.
10503 H := Current_Entity (Typ);
10506 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
10509 and then Homonym (H) /= Typ
10511 H := Homonym (Typ);
10514 Set_Homonym (H, Homonym (Typ));
10517 Insert_Before (Typ_Decl, Decl);
10519 Set_Full_View (Inc_T, Typ);
10523 -- Create a common class-wide type for both views, and set the
10524 -- Etype of the class-wide type to the full view.
10526 Make_Class_Wide_Type (Inc_T);
10527 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
10528 Set_Etype (Class_Wide_Type (Typ), Typ);
10531 end Build_Incomplete_Type_Declaration;
10537 function Designates_T (Subt : Node_Id) return Boolean is
10538 Type_Id : constant Name_Id := Chars (Typ);
10540 function Names_T (Nam : Node_Id) return Boolean;
10541 -- The record type has not been introduced in the current scope
10542 -- yet, so we must examine the name of the type itself, either
10543 -- an identifier T, or an expanded name of the form P.T, where
10544 -- P denotes the current scope.
10550 function Names_T (Nam : Node_Id) return Boolean is
10552 if Nkind (Nam) = N_Identifier then
10553 return Chars (Nam) = Type_Id;
10555 elsif Nkind (Nam) = N_Selected_Component then
10556 if Chars (Selector_Name (Nam)) = Type_Id then
10557 if Nkind (Prefix (Nam)) = N_Identifier then
10558 return Chars (Prefix (Nam)) = Chars (Current_Scope);
10560 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
10561 return Chars (Selector_Name (Prefix (Nam))) =
10562 Chars (Current_Scope);
10576 -- Start of processing for Designates_T
10579 if Nkind (Subt) = N_Identifier then
10580 return Chars (Subt) = Type_Id;
10582 -- Reference can be through an expanded name which has not been
10583 -- analyzed yet, and which designates enclosing scopes.
10585 elsif Nkind (Subt) = N_Selected_Component then
10586 if Names_T (Subt) then
10589 -- Otherwise it must denote an entity that is already visible.
10590 -- The access definition may name a subtype of the enclosing
10591 -- type, if there is a previous incomplete declaration for it.
10594 Find_Selected_Component (Subt);
10596 Is_Entity_Name (Subt)
10597 and then Scope (Entity (Subt)) = Current_Scope
10599 (Chars (Base_Type (Entity (Subt))) = Type_Id
10601 (Is_Class_Wide_Type (Entity (Subt))
10603 Chars (Etype (Base_Type (Entity (Subt)))) =
10607 -- A reference to the current type may appear as the prefix of
10608 -- a 'Class attribute.
10610 elsif Nkind (Subt) = N_Attribute_Reference
10611 and then Attribute_Name (Subt) = Name_Class
10613 return Names_T (Prefix (Subt));
10624 function Mentions_T (Acc_Def : Node_Id) return Boolean is
10625 Param_Spec : Node_Id;
10627 Acc_Subprg : constant Node_Id :=
10628 Access_To_Subprogram_Definition (Acc_Def);
10631 if No (Acc_Subprg) then
10632 return Designates_T (Subtype_Mark (Acc_Def));
10635 -- Component is an access_to_subprogram: examine its formals,
10636 -- and result definition in the case of an access_to_function.
10638 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
10639 while Present (Param_Spec) loop
10640 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
10641 and then Mentions_T (Parameter_Type (Param_Spec))
10645 elsif Designates_T (Parameter_Type (Param_Spec)) then
10652 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
10653 if Nkind (Result_Definition (Acc_Subprg)) =
10654 N_Access_Definition
10656 return Mentions_T (Result_Definition (Acc_Subprg));
10658 return Designates_T (Result_Definition (Acc_Subprg));
10665 -- Start of processing for Check_Anonymous_Access_Components
10668 if No (Comp_List) then
10672 Comp := First (Component_Items (Comp_List));
10673 while Present (Comp) loop
10674 if Nkind (Comp) = N_Component_Declaration
10676 (Access_Definition (Component_Definition (Comp)))
10678 Mentions_T (Access_Definition (Component_Definition (Comp)))
10680 Comp_Def := Component_Definition (Comp);
10682 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
10684 Build_Incomplete_Type_Declaration;
10685 Anon_Access := Make_Temporary (Loc, 'S');
10687 -- Create a declaration for the anonymous access type: either
10688 -- an access_to_object or an access_to_subprogram.
10690 if Present (Acc_Def) then
10691 if Nkind (Acc_Def) = N_Access_Function_Definition then
10693 Make_Access_Function_Definition (Loc,
10694 Parameter_Specifications =>
10695 Parameter_Specifications (Acc_Def),
10696 Result_Definition => Result_Definition (Acc_Def));
10699 Make_Access_Procedure_Definition (Loc,
10700 Parameter_Specifications =>
10701 Parameter_Specifications (Acc_Def));
10706 Make_Access_To_Object_Definition (Loc,
10707 Subtype_Indication =>
10709 (Subtype_Mark (Access_Definition (Comp_Def))));
10711 Set_Constant_Present
10712 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
10714 (Type_Def, All_Present (Access_Definition (Comp_Def)));
10717 Set_Null_Exclusion_Present
10719 Null_Exclusion_Present (Access_Definition (Comp_Def)));
10722 Make_Full_Type_Declaration (Loc,
10723 Defining_Identifier => Anon_Access,
10724 Type_Definition => Type_Def);
10726 Insert_Before (Typ_Decl, Decl);
10729 -- If an access to subprogram, create the extra formals
10731 if Present (Acc_Def) then
10732 Create_Extra_Formals (Designated_Type (Anon_Access));
10734 -- If an access to object, preserve entity of designated type,
10735 -- for ASIS use, before rewriting the component definition.
10742 Desig := Entity (Subtype_Indication (Type_Def));
10744 -- If the access definition is to the current record,
10745 -- the visible entity at this point is an incomplete
10746 -- type. Retrieve the full view to simplify ASIS queries
10748 if Ekind (Desig) = E_Incomplete_Type then
10749 Desig := Full_View (Desig);
10753 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
10758 Make_Component_Definition (Loc,
10759 Subtype_Indication =>
10760 New_Occurrence_Of (Anon_Access, Loc)));
10762 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
10763 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
10765 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
10768 Set_Is_Local_Anonymous_Access (Anon_Access);
10774 if Present (Variant_Part (Comp_List)) then
10778 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
10779 while Present (V) loop
10780 Check_Anonymous_Access_Components
10781 (Typ_Decl, Typ, Prev, Component_List (V));
10782 Next_Non_Pragma (V);
10786 end Check_Anonymous_Access_Components;
10788 ----------------------
10789 -- Check_Completion --
10790 ----------------------
10792 procedure Check_Completion (Body_Id : Node_Id := Empty) is
10795 procedure Post_Error;
10796 -- Post error message for lack of completion for entity E
10802 procedure Post_Error is
10804 procedure Missing_Body;
10805 -- Output missing body message
10811 procedure Missing_Body is
10813 -- Spec is in same unit, so we can post on spec
10815 if In_Same_Source_Unit (Body_Id, E) then
10816 Error_Msg_N ("missing body for &", E);
10818 -- Spec is in a separate unit, so we have to post on the body
10821 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
10825 -- Start of processing for Post_Error
10828 if not Comes_From_Source (E) then
10830 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
10832 -- It may be an anonymous protected type created for a
10833 -- single variable. Post error on variable, if present.
10839 Var := First_Entity (Current_Scope);
10840 while Present (Var) loop
10841 exit when Etype (Var) = E
10842 and then Comes_From_Source (Var);
10847 if Present (Var) then
10854 -- If a generated entity has no completion, then either previous
10855 -- semantic errors have disabled the expansion phase, or else we had
10856 -- missing subunits, or else we are compiling without expansion,
10857 -- or else something is very wrong.
10859 if not Comes_From_Source (E) then
10861 (Serious_Errors_Detected > 0
10862 or else Configurable_Run_Time_Violations > 0
10863 or else Subunits_Missing
10864 or else not Expander_Active);
10867 -- Here for source entity
10870 -- Here if no body to post the error message, so we post the error
10871 -- on the declaration that has no completion. This is not really
10872 -- the right place to post it, think about this later ???
10874 if No (Body_Id) then
10875 if Is_Type (E) then
10877 ("missing full declaration for }", Parent (E), E);
10879 Error_Msg_NE ("missing body for &", Parent (E), E);
10882 -- Package body has no completion for a declaration that appears
10883 -- in the corresponding spec. Post error on the body, with a
10884 -- reference to the non-completed declaration.
10887 Error_Msg_Sloc := Sloc (E);
10889 if Is_Type (E) then
10890 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
10892 elsif Is_Overloadable (E)
10893 and then Current_Entity_In_Scope (E) /= E
10895 -- It may be that the completion is mistyped and appears as
10896 -- a distinct overloading of the entity.
10899 Candidate : constant Entity_Id :=
10900 Current_Entity_In_Scope (E);
10901 Decl : constant Node_Id :=
10902 Unit_Declaration_Node (Candidate);
10905 if Is_Overloadable (Candidate)
10906 and then Ekind (Candidate) = Ekind (E)
10907 and then Nkind (Decl) = N_Subprogram_Body
10908 and then Acts_As_Spec (Decl)
10910 Check_Type_Conformant (Candidate, E);
10926 Pack_Id : constant Entity_Id := Current_Scope;
10928 -- Start of processing for Check_Completion
10931 E := First_Entity (Pack_Id);
10932 while Present (E) loop
10933 if Is_Intrinsic_Subprogram (E) then
10936 -- A Ghost entity declared in a non-Ghost package does not force the
10937 -- need for a body (SPARK RM 6.9(11)).
10939 elsif not Is_Ghost_Entity (Pack_Id) and then Is_Ghost_Entity (E) then
10942 -- The following situation requires special handling: a child unit
10943 -- that appears in the context clause of the body of its parent:
10945 -- procedure Parent.Child (...);
10947 -- with Parent.Child;
10948 -- package body Parent is
10950 -- Here Parent.Child appears as a local entity, but should not be
10951 -- flagged as requiring completion, because it is a compilation
10954 -- Ignore missing completion for a subprogram that does not come from
10955 -- source (including the _Call primitive operation of RAS types,
10956 -- which has to have the flag Comes_From_Source for other purposes):
10957 -- we assume that the expander will provide the missing completion.
10958 -- In case of previous errors, other expansion actions that provide
10959 -- bodies for null procedures with not be invoked, so inhibit message
10962 -- Note that E_Operator is not in the list that follows, because
10963 -- this kind is reserved for predefined operators, that are
10964 -- intrinsic and do not need completion.
10966 elsif Ekind_In (E, E_Function,
10968 E_Generic_Function,
10969 E_Generic_Procedure)
10971 if Has_Completion (E) then
10974 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
10977 elsif Is_Subprogram (E)
10978 and then (not Comes_From_Source (E)
10979 or else Chars (E) = Name_uCall)
10984 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
10988 elsif Nkind (Parent (E)) = N_Procedure_Specification
10989 and then Null_Present (Parent (E))
10990 and then Serious_Errors_Detected > 0
10998 elsif Is_Entry (E) then
10999 if not Has_Completion (E) and then
11000 (Ekind (Scope (E)) = E_Protected_Object
11001 or else Ekind (Scope (E)) = E_Protected_Type)
11006 elsif Is_Package_Or_Generic_Package (E) then
11007 if Unit_Requires_Body (E) then
11008 if not Has_Completion (E)
11009 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11015 elsif not Is_Child_Unit (E) then
11016 May_Need_Implicit_Body (E);
11019 -- A formal incomplete type (Ada 2012) does not require a completion;
11020 -- other incomplete type declarations do.
11022 elsif Ekind (E) = E_Incomplete_Type
11023 and then No (Underlying_Type (E))
11024 and then not Is_Generic_Type (E)
11028 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11029 and then not Has_Completion (E)
11033 -- A single task declared in the current scope is a constant, verify
11034 -- that the body of its anonymous type is in the same scope. If the
11035 -- task is defined elsewhere, this may be a renaming declaration for
11036 -- which no completion is needed.
11038 elsif Ekind (E) = E_Constant
11039 and then Ekind (Etype (E)) = E_Task_Type
11040 and then not Has_Completion (Etype (E))
11041 and then Scope (Etype (E)) = Current_Scope
11045 elsif Ekind (E) = E_Protected_Object
11046 and then not Has_Completion (Etype (E))
11050 elsif Ekind (E) = E_Record_Type then
11051 if Is_Tagged_Type (E) then
11052 Check_Abstract_Overriding (E);
11053 Check_Conventions (E);
11056 Check_Aliased_Component_Types (E);
11058 elsif Ekind (E) = E_Array_Type then
11059 Check_Aliased_Component_Types (E);
11065 end Check_Completion;
11067 ------------------------------------
11068 -- Check_CPP_Type_Has_No_Defaults --
11069 ------------------------------------
11071 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11072 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11077 -- Obtain the component list
11079 if Nkind (Tdef) = N_Record_Definition then
11080 Clist := Component_List (Tdef);
11081 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11082 Clist := Component_List (Record_Extension_Part (Tdef));
11085 -- Check all components to ensure no default expressions
11087 if Present (Clist) then
11088 Comp := First (Component_Items (Clist));
11089 while Present (Comp) loop
11090 if Present (Expression (Comp)) then
11092 ("component of imported 'C'P'P type cannot have "
11093 & "default expression", Expression (Comp));
11099 end Check_CPP_Type_Has_No_Defaults;
11101 ----------------------------
11102 -- Check_Delta_Expression --
11103 ----------------------------
11105 procedure Check_Delta_Expression (E : Node_Id) is
11107 if not (Is_Real_Type (Etype (E))) then
11108 Wrong_Type (E, Any_Real);
11110 elsif not Is_OK_Static_Expression (E) then
11111 Flag_Non_Static_Expr
11112 ("non-static expression used for delta value!", E);
11114 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11115 Error_Msg_N ("delta expression must be positive", E);
11121 -- If any of above errors occurred, then replace the incorrect
11122 -- expression by the real 0.1, which should prevent further errors.
11125 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11126 Analyze_And_Resolve (E, Standard_Float);
11127 end Check_Delta_Expression;
11129 -----------------------------
11130 -- Check_Digits_Expression --
11131 -----------------------------
11133 procedure Check_Digits_Expression (E : Node_Id) is
11135 if not (Is_Integer_Type (Etype (E))) then
11136 Wrong_Type (E, Any_Integer);
11138 elsif not Is_OK_Static_Expression (E) then
11139 Flag_Non_Static_Expr
11140 ("non-static expression used for digits value!", E);
11142 elsif Expr_Value (E) <= 0 then
11143 Error_Msg_N ("digits value must be greater than zero", E);
11149 -- If any of above errors occurred, then replace the incorrect
11150 -- expression by the integer 1, which should prevent further errors.
11152 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11153 Analyze_And_Resolve (E, Standard_Integer);
11155 end Check_Digits_Expression;
11157 --------------------------
11158 -- Check_Initialization --
11159 --------------------------
11161 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11163 -- Special processing for limited types
11165 if Is_Limited_Type (T)
11166 and then not In_Instance
11167 and then not In_Inlined_Body
11169 if not OK_For_Limited_Init (T, Exp) then
11171 -- In GNAT mode, this is just a warning, to allow it to be evilly
11172 -- turned off. Otherwise it is a real error.
11176 ("??cannot initialize entities of limited type!", Exp);
11178 elsif Ada_Version < Ada_2005 then
11180 -- The side effect removal machinery may generate illegal Ada
11181 -- code to avoid the usage of access types and 'reference in
11182 -- SPARK mode. Since this is legal code with respect to theorem
11183 -- proving, do not emit the error.
11186 and then Nkind (Exp) = N_Function_Call
11187 and then Nkind (Parent (Exp)) = N_Object_Declaration
11188 and then not Comes_From_Source
11189 (Defining_Identifier (Parent (Exp)))
11195 ("cannot initialize entities of limited type", Exp);
11196 Explain_Limited_Type (T, Exp);
11200 -- Specialize error message according to kind of illegal
11201 -- initial expression.
11203 if Nkind (Exp) = N_Type_Conversion
11204 and then Nkind (Expression (Exp)) = N_Function_Call
11207 ("illegal context for call"
11208 & " to function with limited result", Exp);
11212 ("initialization of limited object requires aggregate "
11213 & "or function call", Exp);
11219 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11220 -- set unless we can be sure that no range check is required.
11222 if (GNATprove_Mode or not Expander_Active)
11223 and then Is_Scalar_Type (T)
11224 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11226 Set_Do_Range_Check (Exp);
11228 end Check_Initialization;
11230 ----------------------
11231 -- Check_Interfaces --
11232 ----------------------
11234 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11235 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11238 Iface_Def : Node_Id;
11239 Iface_Typ : Entity_Id;
11240 Parent_Node : Node_Id;
11242 Is_Task : Boolean := False;
11243 -- Set True if parent type or any progenitor is a task interface
11245 Is_Protected : Boolean := False;
11246 -- Set True if parent type or any progenitor is a protected interface
11248 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11249 -- Check that a progenitor is compatible with declaration. If an error
11250 -- message is output, it is posted on Error_Node.
11256 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11257 Iface_Id : constant Entity_Id :=
11258 Defining_Identifier (Parent (Iface_Def));
11259 Type_Def : Node_Id;
11262 if Nkind (N) = N_Private_Extension_Declaration then
11265 Type_Def := Type_Definition (N);
11268 if Is_Task_Interface (Iface_Id) then
11271 elsif Is_Protected_Interface (Iface_Id) then
11272 Is_Protected := True;
11275 if Is_Synchronized_Interface (Iface_Id) then
11277 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11278 -- extension derived from a synchronized interface must explicitly
11279 -- be declared synchronized, because the full view will be a
11280 -- synchronized type.
11282 if Nkind (N) = N_Private_Extension_Declaration then
11283 if not Synchronized_Present (N) then
11285 ("private extension of& must be explicitly synchronized",
11289 -- However, by 3.9.4(16/2), a full type that is a record extension
11290 -- is never allowed to derive from a synchronized interface (note
11291 -- that interfaces must be excluded from this check, because those
11292 -- are represented by derived type definitions in some cases).
11294 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11295 and then not Interface_Present (Type_Definition (N))
11297 Error_Msg_N ("record extension cannot derive from synchronized "
11298 & "interface", Error_Node);
11302 -- Check that the characteristics of the progenitor are compatible
11303 -- with the explicit qualifier in the declaration.
11304 -- The check only applies to qualifiers that come from source.
11305 -- Limited_Present also appears in the declaration of corresponding
11306 -- records, and the check does not apply to them.
11308 if Limited_Present (Type_Def)
11310 Is_Concurrent_Record_Type (Defining_Identifier (N))
11312 if Is_Limited_Interface (Parent_Type)
11313 and then not Is_Limited_Interface (Iface_Id)
11316 ("progenitor & must be limited interface",
11317 Error_Node, Iface_Id);
11320 (Task_Present (Iface_Def)
11321 or else Protected_Present (Iface_Def)
11322 or else Synchronized_Present (Iface_Def))
11323 and then Nkind (N) /= N_Private_Extension_Declaration
11324 and then not Error_Posted (N)
11327 ("progenitor & must be limited interface",
11328 Error_Node, Iface_Id);
11331 -- Protected interfaces can only inherit from limited, synchronized
11332 -- or protected interfaces.
11334 elsif Nkind (N) = N_Full_Type_Declaration
11335 and then Protected_Present (Type_Def)
11337 if Limited_Present (Iface_Def)
11338 or else Synchronized_Present (Iface_Def)
11339 or else Protected_Present (Iface_Def)
11343 elsif Task_Present (Iface_Def) then
11344 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11345 & "from task interface", Error_Node);
11348 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
11349 & "from non-limited interface", Error_Node);
11352 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
11353 -- limited and synchronized.
11355 elsif Synchronized_Present (Type_Def) then
11356 if Limited_Present (Iface_Def)
11357 or else Synchronized_Present (Iface_Def)
11361 elsif Protected_Present (Iface_Def)
11362 and then Nkind (N) /= N_Private_Extension_Declaration
11364 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11365 & "from protected interface", Error_Node);
11367 elsif Task_Present (Iface_Def)
11368 and then Nkind (N) /= N_Private_Extension_Declaration
11370 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11371 & "from task interface", Error_Node);
11373 elsif not Is_Limited_Interface (Iface_Id) then
11374 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
11375 & "from non-limited interface", Error_Node);
11378 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
11379 -- synchronized or task interfaces.
11381 elsif Nkind (N) = N_Full_Type_Declaration
11382 and then Task_Present (Type_Def)
11384 if Limited_Present (Iface_Def)
11385 or else Synchronized_Present (Iface_Def)
11386 or else Task_Present (Iface_Def)
11390 elsif Protected_Present (Iface_Def) then
11391 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11392 & "protected interface", Error_Node);
11395 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
11396 & "non-limited interface", Error_Node);
11401 -- Start of processing for Check_Interfaces
11404 if Is_Interface (Parent_Type) then
11405 if Is_Task_Interface (Parent_Type) then
11408 elsif Is_Protected_Interface (Parent_Type) then
11409 Is_Protected := True;
11413 if Nkind (N) = N_Private_Extension_Declaration then
11415 -- Check that progenitors are compatible with declaration
11417 Iface := First (Interface_List (Def));
11418 while Present (Iface) loop
11419 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11421 Parent_Node := Parent (Base_Type (Iface_Typ));
11422 Iface_Def := Type_Definition (Parent_Node);
11424 if not Is_Interface (Iface_Typ) then
11425 Diagnose_Interface (Iface, Iface_Typ);
11427 Check_Ifaces (Iface_Def, Iface);
11433 if Is_Task and Is_Protected then
11435 ("type cannot derive from task and protected interface", N);
11441 -- Full type declaration of derived type.
11442 -- Check compatibility with parent if it is interface type
11444 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11445 and then Is_Interface (Parent_Type)
11447 Parent_Node := Parent (Parent_Type);
11449 -- More detailed checks for interface varieties
11452 (Iface_Def => Type_Definition (Parent_Node),
11453 Error_Node => Subtype_Indication (Type_Definition (N)));
11456 Iface := First (Interface_List (Def));
11457 while Present (Iface) loop
11458 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
11460 Parent_Node := Parent (Base_Type (Iface_Typ));
11461 Iface_Def := Type_Definition (Parent_Node);
11463 if not Is_Interface (Iface_Typ) then
11464 Diagnose_Interface (Iface, Iface_Typ);
11467 -- "The declaration of a specific descendant of an interface
11468 -- type freezes the interface type" RM 13.14
11470 Freeze_Before (N, Iface_Typ);
11471 Check_Ifaces (Iface_Def, Error_Node => Iface);
11477 if Is_Task and Is_Protected then
11479 ("type cannot derive from task and protected interface", N);
11481 end Check_Interfaces;
11483 ------------------------------------
11484 -- Check_Or_Process_Discriminants --
11485 ------------------------------------
11487 -- If an incomplete or private type declaration was already given for the
11488 -- type, the discriminants may have already been processed if they were
11489 -- present on the incomplete declaration. In this case a full conformance
11490 -- check has been performed in Find_Type_Name, and we then recheck here
11491 -- some properties that can't be checked on the partial view alone.
11492 -- Otherwise we call Process_Discriminants.
11494 procedure Check_Or_Process_Discriminants
11497 Prev : Entity_Id := Empty)
11500 if Has_Discriminants (T) then
11502 -- Discriminants are already set on T if they were already present
11503 -- on the partial view. Make them visible to component declarations.
11507 -- Discriminant on T (full view) referencing expr on partial view
11509 Prev_D : Entity_Id;
11510 -- Entity of corresponding discriminant on partial view
11513 -- Discriminant specification for full view, expression is
11514 -- the syntactic copy on full view (which has been checked for
11515 -- conformance with partial view), only used here to post error
11519 D := First_Discriminant (T);
11520 New_D := First (Discriminant_Specifications (N));
11521 while Present (D) loop
11522 Prev_D := Current_Entity (D);
11523 Set_Current_Entity (D);
11524 Set_Is_Immediately_Visible (D);
11525 Set_Homonym (D, Prev_D);
11527 -- Handle the case where there is an untagged partial view and
11528 -- the full view is tagged: must disallow discriminants with
11529 -- defaults, unless compiling for Ada 2012, which allows a
11530 -- limited tagged type to have defaulted discriminants (see
11531 -- AI05-0214). However, suppress error here if it was already
11532 -- reported on the default expression of the partial view.
11534 if Is_Tagged_Type (T)
11535 and then Present (Expression (Parent (D)))
11536 and then (not Is_Limited_Type (Current_Scope)
11537 or else Ada_Version < Ada_2012)
11538 and then not Error_Posted (Expression (Parent (D)))
11540 if Ada_Version >= Ada_2012 then
11542 ("discriminants of nonlimited tagged type cannot have "
11544 Expression (New_D));
11547 ("discriminants of tagged type cannot have defaults",
11548 Expression (New_D));
11552 -- Ada 2005 (AI-230): Access discriminant allowed in
11553 -- non-limited record types.
11555 if Ada_Version < Ada_2005 then
11557 -- This restriction gets applied to the full type here. It
11558 -- has already been applied earlier to the partial view.
11560 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
11563 Next_Discriminant (D);
11568 elsif Present (Discriminant_Specifications (N)) then
11569 Process_Discriminants (N, Prev);
11571 end Check_Or_Process_Discriminants;
11573 ----------------------
11574 -- Check_Real_Bound --
11575 ----------------------
11577 procedure Check_Real_Bound (Bound : Node_Id) is
11579 if not Is_Real_Type (Etype (Bound)) then
11581 ("bound in real type definition must be of real type", Bound);
11583 elsif not Is_OK_Static_Expression (Bound) then
11584 Flag_Non_Static_Expr
11585 ("non-static expression used for real type bound!", Bound);
11592 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
11594 Resolve (Bound, Standard_Float);
11595 end Check_Real_Bound;
11597 ------------------------------
11598 -- Complete_Private_Subtype --
11599 ------------------------------
11601 procedure Complete_Private_Subtype
11604 Full_Base : Entity_Id;
11605 Related_Nod : Node_Id)
11607 Save_Next_Entity : Entity_Id;
11608 Save_Homonym : Entity_Id;
11611 -- Set semantic attributes for (implicit) private subtype completion.
11612 -- If the full type has no discriminants, then it is a copy of the
11613 -- full view of the base. Otherwise, it is a subtype of the base with
11614 -- a possible discriminant constraint. Save and restore the original
11615 -- Next_Entity field of full to ensure that the calls to Copy_Node do
11616 -- not corrupt the entity chain.
11618 -- Note that the type of the full view is the same entity as the type
11619 -- of the partial view. In this fashion, the subtype has access to the
11620 -- correct view of the parent.
11622 Save_Next_Entity := Next_Entity (Full);
11623 Save_Homonym := Homonym (Priv);
11625 case Ekind (Full_Base) is
11626 when E_Record_Type |
11632 Copy_Node (Priv, Full);
11634 Set_Has_Discriminants
11635 (Full, Has_Discriminants (Full_Base));
11636 Set_Has_Unknown_Discriminants
11637 (Full, Has_Unknown_Discriminants (Full_Base));
11638 Set_First_Entity (Full, First_Entity (Full_Base));
11639 Set_Last_Entity (Full, Last_Entity (Full_Base));
11641 -- If the underlying base type is constrained, we know that the
11642 -- full view of the subtype is constrained as well (the converse
11643 -- is not necessarily true).
11645 if Is_Constrained (Full_Base) then
11646 Set_Is_Constrained (Full);
11650 Copy_Node (Full_Base, Full);
11652 Set_Chars (Full, Chars (Priv));
11653 Conditional_Delay (Full, Priv);
11654 Set_Sloc (Full, Sloc (Priv));
11657 Set_Next_Entity (Full, Save_Next_Entity);
11658 Set_Homonym (Full, Save_Homonym);
11659 Set_Associated_Node_For_Itype (Full, Related_Nod);
11661 -- Set common attributes for all subtypes: kind, convention, etc.
11663 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
11664 Set_Convention (Full, Convention (Full_Base));
11666 -- The Etype of the full view is inconsistent. Gigi needs to see the
11667 -- structural full view, which is what the current scheme gives: the
11668 -- Etype of the full view is the etype of the full base. However, if the
11669 -- full base is a derived type, the full view then looks like a subtype
11670 -- of the parent, not a subtype of the full base. If instead we write:
11672 -- Set_Etype (Full, Full_Base);
11674 -- then we get inconsistencies in the front-end (confusion between
11675 -- views). Several outstanding bugs are related to this ???
11677 Set_Is_First_Subtype (Full, False);
11678 Set_Scope (Full, Scope (Priv));
11679 Set_Size_Info (Full, Full_Base);
11680 Set_RM_Size (Full, RM_Size (Full_Base));
11681 Set_Is_Itype (Full);
11683 -- A subtype of a private-type-without-discriminants, whose full-view
11684 -- has discriminants with default expressions, is not constrained.
11686 if not Has_Discriminants (Priv) then
11687 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
11689 if Has_Discriminants (Full_Base) then
11690 Set_Discriminant_Constraint
11691 (Full, Discriminant_Constraint (Full_Base));
11693 -- The partial view may have been indefinite, the full view
11696 Set_Has_Unknown_Discriminants
11697 (Full, Has_Unknown_Discriminants (Full_Base));
11701 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
11702 Set_Depends_On_Private (Full, Has_Private_Component (Full));
11704 -- Freeze the private subtype entity if its parent is delayed, and not
11705 -- already frozen. We skip this processing if the type is an anonymous
11706 -- subtype of a record component, or is the corresponding record of a
11707 -- protected type, since these are processed when the enclosing type
11710 if not Is_Type (Scope (Full)) then
11711 Set_Has_Delayed_Freeze (Full,
11712 Has_Delayed_Freeze (Full_Base)
11713 and then (not Is_Frozen (Full_Base)));
11716 Set_Freeze_Node (Full, Empty);
11717 Set_Is_Frozen (Full, False);
11718 Set_Full_View (Priv, Full);
11720 if Has_Discriminants (Full) then
11721 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
11722 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
11724 if Has_Unknown_Discriminants (Full) then
11725 Set_Discriminant_Constraint (Full, No_Elist);
11729 if Ekind (Full_Base) = E_Record_Type
11730 and then Has_Discriminants (Full_Base)
11731 and then Has_Discriminants (Priv) -- might not, if errors
11732 and then not Has_Unknown_Discriminants (Priv)
11733 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
11735 Create_Constrained_Components
11736 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
11738 -- If the full base is itself derived from private, build a congruent
11739 -- subtype of its underlying type, for use by the back end. For a
11740 -- constrained record component, the declaration cannot be placed on
11741 -- the component list, but it must nevertheless be built an analyzed, to
11742 -- supply enough information for Gigi to compute the size of component.
11744 elsif Ekind (Full_Base) in Private_Kind
11745 and then Is_Derived_Type (Full_Base)
11746 and then Has_Discriminants (Full_Base)
11747 and then (Ekind (Current_Scope) /= E_Record_Subtype)
11749 if not Is_Itype (Priv)
11751 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
11753 Build_Underlying_Full_View
11754 (Parent (Priv), Full, Etype (Full_Base));
11756 elsif Nkind (Related_Nod) = N_Component_Declaration then
11757 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
11760 elsif Is_Record_Type (Full_Base) then
11762 -- Show Full is simply a renaming of Full_Base
11764 Set_Cloned_Subtype (Full, Full_Base);
11767 -- It is unsafe to share the bounds of a scalar type, because the Itype
11768 -- is elaborated on demand, and if a bound is non-static then different
11769 -- orders of elaboration in different units will lead to different
11770 -- external symbols.
11772 if Is_Scalar_Type (Full_Base) then
11773 Set_Scalar_Range (Full,
11774 Make_Range (Sloc (Related_Nod),
11776 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
11778 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
11780 -- This completion inherits the bounds of the full parent, but if
11781 -- the parent is an unconstrained floating point type, so is the
11784 if Is_Floating_Point_Type (Full_Base) then
11785 Set_Includes_Infinities
11786 (Scalar_Range (Full), Has_Infinities (Full_Base));
11790 -- ??? It seems that a lot of fields are missing that should be copied
11791 -- from Full_Base to Full. Here are some that are introduced in a
11792 -- non-disruptive way but a cleanup is necessary.
11794 if Is_Tagged_Type (Full_Base) then
11795 Set_Is_Tagged_Type (Full);
11796 Set_Direct_Primitive_Operations
11797 (Full, Direct_Primitive_Operations (Full_Base));
11798 Set_No_Tagged_Streams_Pragma
11799 (Full, No_Tagged_Streams_Pragma (Full_Base));
11801 -- Inherit class_wide type of full_base in case the partial view was
11802 -- not tagged. Otherwise it has already been created when the private
11803 -- subtype was analyzed.
11805 if No (Class_Wide_Type (Full)) then
11806 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
11809 -- If this is a subtype of a protected or task type, constrain its
11810 -- corresponding record, unless this is a subtype without constraints,
11811 -- i.e. a simple renaming as with an actual subtype in an instance.
11813 elsif Is_Concurrent_Type (Full_Base) then
11814 if Has_Discriminants (Full)
11815 and then Present (Corresponding_Record_Type (Full_Base))
11817 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
11819 Set_Corresponding_Record_Type (Full,
11820 Constrain_Corresponding_Record
11821 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
11824 Set_Corresponding_Record_Type (Full,
11825 Corresponding_Record_Type (Full_Base));
11829 -- Link rep item chain, and also setting of Has_Predicates from private
11830 -- subtype to full subtype, since we will need these on the full subtype
11831 -- to create the predicate function. Note that the full subtype may
11832 -- already have rep items, inherited from the full view of the base
11833 -- type, so we must be sure not to overwrite these entries.
11838 Next_Item : Node_Id;
11841 Item := First_Rep_Item (Full);
11843 -- If no existing rep items on full type, we can just link directly
11844 -- to the list of items on the private type, if any exist.. Same if
11845 -- the rep items are only those inherited from the base
11848 or else Nkind (Item) /= N_Aspect_Specification
11849 or else Entity (Item) = Full_Base)
11850 and then Present (First_Rep_Item (Priv))
11852 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
11854 -- Otherwise, search to the end of items currently linked to the full
11855 -- subtype and append the private items to the end. However, if Priv
11856 -- and Full already have the same list of rep items, then the append
11857 -- is not done, as that would create a circularity.
11859 elsif Item /= First_Rep_Item (Priv) then
11862 Next_Item := Next_Rep_Item (Item);
11863 exit when No (Next_Item);
11866 -- If the private view has aspect specifications, the full view
11867 -- inherits them. Since these aspects may already have been
11868 -- attached to the full view during derivation, do not append
11869 -- them if already present.
11871 if Item = First_Rep_Item (Priv) then
11877 -- And link the private type items at the end of the chain
11880 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
11885 -- Make sure Has_Predicates is set on full type if it is set on the
11886 -- private type. Note that it may already be set on the full type and
11887 -- if so, we don't want to unset it. Similarly, propagate information
11888 -- about delayed aspects, because the corresponding pragmas must be
11889 -- analyzed when one of the views is frozen. This last step is needed
11890 -- in particular when the full type is a scalar type for which an
11891 -- anonymous base type is constructed.
11893 if Has_Predicates (Priv) then
11894 Set_Has_Predicates (Full);
11897 if Has_Delayed_Aspects (Priv) then
11898 Set_Has_Delayed_Aspects (Full);
11900 end Complete_Private_Subtype;
11902 ----------------------------
11903 -- Constant_Redeclaration --
11904 ----------------------------
11906 procedure Constant_Redeclaration
11911 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
11912 Obj_Def : constant Node_Id := Object_Definition (N);
11915 procedure Check_Possible_Deferred_Completion
11916 (Prev_Id : Entity_Id;
11917 Prev_Obj_Def : Node_Id;
11918 Curr_Obj_Def : Node_Id);
11919 -- Determine whether the two object definitions describe the partial
11920 -- and the full view of a constrained deferred constant. Generate
11921 -- a subtype for the full view and verify that it statically matches
11922 -- the subtype of the partial view.
11924 procedure Check_Recursive_Declaration (Typ : Entity_Id);
11925 -- If deferred constant is an access type initialized with an allocator,
11926 -- check whether there is an illegal recursion in the definition,
11927 -- through a default value of some record subcomponent. This is normally
11928 -- detected when generating init procs, but requires this additional
11929 -- mechanism when expansion is disabled.
11931 ----------------------------------------
11932 -- Check_Possible_Deferred_Completion --
11933 ----------------------------------------
11935 procedure Check_Possible_Deferred_Completion
11936 (Prev_Id : Entity_Id;
11937 Prev_Obj_Def : Node_Id;
11938 Curr_Obj_Def : Node_Id)
11941 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
11942 and then Present (Constraint (Prev_Obj_Def))
11943 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
11944 and then Present (Constraint (Curr_Obj_Def))
11947 Loc : constant Source_Ptr := Sloc (N);
11948 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
11949 Decl : constant Node_Id :=
11950 Make_Subtype_Declaration (Loc,
11951 Defining_Identifier => Def_Id,
11952 Subtype_Indication =>
11953 Relocate_Node (Curr_Obj_Def));
11956 Insert_Before_And_Analyze (N, Decl);
11957 Set_Etype (Id, Def_Id);
11959 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
11960 Error_Msg_Sloc := Sloc (Prev_Id);
11961 Error_Msg_N ("subtype does not statically match deferred "
11962 & "declaration #", N);
11966 end Check_Possible_Deferred_Completion;
11968 ---------------------------------
11969 -- Check_Recursive_Declaration --
11970 ---------------------------------
11972 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
11976 if Is_Record_Type (Typ) then
11977 Comp := First_Component (Typ);
11978 while Present (Comp) loop
11979 if Comes_From_Source (Comp) then
11980 if Present (Expression (Parent (Comp)))
11981 and then Is_Entity_Name (Expression (Parent (Comp)))
11982 and then Entity (Expression (Parent (Comp))) = Prev
11984 Error_Msg_Sloc := Sloc (Parent (Comp));
11986 ("illegal circularity with declaration for & #",
11990 elsif Is_Record_Type (Etype (Comp)) then
11991 Check_Recursive_Declaration (Etype (Comp));
11995 Next_Component (Comp);
11998 end Check_Recursive_Declaration;
12000 -- Start of processing for Constant_Redeclaration
12003 if Nkind (Parent (Prev)) = N_Object_Declaration then
12004 if Nkind (Object_Definition
12005 (Parent (Prev))) = N_Subtype_Indication
12007 -- Find type of new declaration. The constraints of the two
12008 -- views must match statically, but there is no point in
12009 -- creating an itype for the full view.
12011 if Nkind (Obj_Def) = N_Subtype_Indication then
12012 Find_Type (Subtype_Mark (Obj_Def));
12013 New_T := Entity (Subtype_Mark (Obj_Def));
12016 Find_Type (Obj_Def);
12017 New_T := Entity (Obj_Def);
12023 -- The full view may impose a constraint, even if the partial
12024 -- view does not, so construct the subtype.
12026 New_T := Find_Type_Of_Object (Obj_Def, N);
12031 -- Current declaration is illegal, diagnosed below in Enter_Name
12037 -- If previous full declaration or a renaming declaration exists, or if
12038 -- a homograph is present, let Enter_Name handle it, either with an
12039 -- error or with the removal of an overridden implicit subprogram.
12040 -- The previous one is a full declaration if it has an expression
12041 -- (which in the case of an aggregate is indicated by the Init flag).
12043 if Ekind (Prev) /= E_Constant
12044 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12045 or else Present (Expression (Parent (Prev)))
12046 or else Has_Init_Expression (Parent (Prev))
12047 or else Present (Full_View (Prev))
12051 -- Verify that types of both declarations match, or else that both types
12052 -- are anonymous access types whose designated subtypes statically match
12053 -- (as allowed in Ada 2005 by AI-385).
12055 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12057 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12058 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12059 or else Is_Access_Constant (Etype (New_T)) /=
12060 Is_Access_Constant (Etype (Prev))
12061 or else Can_Never_Be_Null (Etype (New_T)) /=
12062 Can_Never_Be_Null (Etype (Prev))
12063 or else Null_Exclusion_Present (Parent (Prev)) /=
12064 Null_Exclusion_Present (Parent (Id))
12065 or else not Subtypes_Statically_Match
12066 (Designated_Type (Etype (Prev)),
12067 Designated_Type (Etype (New_T))))
12069 Error_Msg_Sloc := Sloc (Prev);
12070 Error_Msg_N ("type does not match declaration#", N);
12071 Set_Full_View (Prev, Id);
12072 Set_Etype (Id, Any_Type);
12074 -- A deferred constant whose type is an anonymous array is always
12075 -- illegal (unless imported). A detailed error message might be
12076 -- helpful for Ada beginners.
12078 if Nkind (Object_Definition (Parent (Prev)))
12079 = N_Constrained_Array_Definition
12080 and then Nkind (Object_Definition (N))
12081 = N_Constrained_Array_Definition
12083 Error_Msg_N ("\each anonymous array is a distinct type", N);
12084 Error_Msg_N ("a deferred constant must have a named type",
12085 Object_Definition (Parent (Prev)));
12089 Null_Exclusion_Present (Parent (Prev))
12090 and then not Null_Exclusion_Present (N)
12092 Error_Msg_Sloc := Sloc (Prev);
12093 Error_Msg_N ("null-exclusion does not match declaration#", N);
12094 Set_Full_View (Prev, Id);
12095 Set_Etype (Id, Any_Type);
12097 -- If so, process the full constant declaration
12100 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12101 -- the deferred declaration is constrained, then the subtype defined
12102 -- by the subtype_indication in the full declaration shall match it
12105 Check_Possible_Deferred_Completion
12107 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12108 Curr_Obj_Def => Obj_Def);
12110 Set_Full_View (Prev, Id);
12111 Set_Is_Public (Id, Is_Public (Prev));
12112 Set_Is_Internal (Id);
12113 Append_Entity (Id, Current_Scope);
12115 -- Check ALIASED present if present before (RM 7.4(7))
12117 if Is_Aliased (Prev)
12118 and then not Aliased_Present (N)
12120 Error_Msg_Sloc := Sloc (Prev);
12121 Error_Msg_N ("ALIASED required (see declaration #)", N);
12124 -- Check that placement is in private part and that the incomplete
12125 -- declaration appeared in the visible part.
12127 if Ekind (Current_Scope) = E_Package
12128 and then not In_Private_Part (Current_Scope)
12130 Error_Msg_Sloc := Sloc (Prev);
12132 ("full constant for declaration # must be in private part", N);
12134 elsif Ekind (Current_Scope) = E_Package
12136 List_Containing (Parent (Prev)) /=
12137 Visible_Declarations (Package_Specification (Current_Scope))
12140 ("deferred constant must be declared in visible part",
12144 if Is_Access_Type (T)
12145 and then Nkind (Expression (N)) = N_Allocator
12147 Check_Recursive_Declaration (Designated_Type (T));
12150 -- A deferred constant is a visible entity. If type has invariants,
12151 -- verify that the initial value satisfies them.
12153 if Has_Invariants (T) and then Present (Invariant_Procedure (T)) then
12155 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12158 end Constant_Redeclaration;
12160 ----------------------
12161 -- Constrain_Access --
12162 ----------------------
12164 procedure Constrain_Access
12165 (Def_Id : in out Entity_Id;
12167 Related_Nod : Node_Id)
12169 T : constant Entity_Id := Entity (Subtype_Mark (S));
12170 Desig_Type : constant Entity_Id := Designated_Type (T);
12171 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12172 Constraint_OK : Boolean := True;
12175 if Is_Array_Type (Desig_Type) then
12176 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12178 elsif (Is_Record_Type (Desig_Type)
12179 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12180 and then not Is_Constrained (Desig_Type)
12182 -- ??? The following code is a temporary bypass to ignore a
12183 -- discriminant constraint on access type if it is constraining
12184 -- the current record. Avoid creating the implicit subtype of the
12185 -- record we are currently compiling since right now, we cannot
12186 -- handle these. For now, just return the access type itself.
12188 if Desig_Type = Current_Scope
12189 and then No (Def_Id)
12191 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12192 Def_Id := Entity (Subtype_Mark (S));
12194 -- This call added to ensure that the constraint is analyzed
12195 -- (needed for a B test). Note that we still return early from
12196 -- this procedure to avoid recursive processing. ???
12198 Constrain_Discriminated_Type
12199 (Desig_Subtype, S, Related_Nod, For_Access => True);
12203 -- Enforce rule that the constraint is illegal if there is an
12204 -- unconstrained view of the designated type. This means that the
12205 -- partial view (either a private type declaration or a derivation
12206 -- from a private type) has no discriminants. (Defect Report
12207 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12209 -- Rule updated for Ada 2005: The private type is said to have
12210 -- a constrained partial view, given that objects of the type
12211 -- can be declared. Furthermore, the rule applies to all access
12212 -- types, unlike the rule concerning default discriminants (see
12215 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12216 and then Has_Private_Declaration (Desig_Type)
12217 and then In_Open_Scopes (Scope (Desig_Type))
12218 and then Has_Discriminants (Desig_Type)
12221 Pack : constant Node_Id :=
12222 Unit_Declaration_Node (Scope (Desig_Type));
12227 if Nkind (Pack) = N_Package_Declaration then
12228 Decls := Visible_Declarations (Specification (Pack));
12229 Decl := First (Decls);
12230 while Present (Decl) loop
12231 if (Nkind (Decl) = N_Private_Type_Declaration
12232 and then Chars (Defining_Identifier (Decl)) =
12233 Chars (Desig_Type))
12236 (Nkind (Decl) = N_Full_Type_Declaration
12238 Chars (Defining_Identifier (Decl)) =
12240 and then Is_Derived_Type (Desig_Type)
12242 Has_Private_Declaration (Etype (Desig_Type)))
12244 if No (Discriminant_Specifications (Decl)) then
12246 ("cannot constrain access type if designated "
12247 & "type has constrained partial view", S);
12259 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
12260 For_Access => True);
12262 elsif Is_Concurrent_Type (Desig_Type)
12263 and then not Is_Constrained (Desig_Type)
12265 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
12268 Error_Msg_N ("invalid constraint on access type", S);
12270 -- We simply ignore an invalid constraint
12272 Desig_Subtype := Desig_Type;
12273 Constraint_OK := False;
12276 if No (Def_Id) then
12277 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
12279 Set_Ekind (Def_Id, E_Access_Subtype);
12282 if Constraint_OK then
12283 Set_Etype (Def_Id, Base_Type (T));
12285 if Is_Private_Type (Desig_Type) then
12286 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
12289 Set_Etype (Def_Id, Any_Type);
12292 Set_Size_Info (Def_Id, T);
12293 Set_Is_Constrained (Def_Id, Constraint_OK);
12294 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
12295 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12296 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
12298 Conditional_Delay (Def_Id, T);
12300 -- AI-363 : Subtypes of general access types whose designated types have
12301 -- default discriminants are disallowed. In instances, the rule has to
12302 -- be checked against the actual, of which T is the subtype. In a
12303 -- generic body, the rule is checked assuming that the actual type has
12304 -- defaulted discriminants.
12306 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
12307 if Ekind (Base_Type (T)) = E_General_Access_Type
12308 and then Has_Defaulted_Discriminants (Desig_Type)
12310 if Ada_Version < Ada_2005 then
12312 ("access subtype of general access type would not " &
12313 "be allowed in Ada 2005?y?", S);
12316 ("access subtype of general access type not allowed", S);
12319 Error_Msg_N ("\discriminants have defaults", S);
12321 elsif Is_Access_Type (T)
12322 and then Is_Generic_Type (Desig_Type)
12323 and then Has_Discriminants (Desig_Type)
12324 and then In_Package_Body (Current_Scope)
12326 if Ada_Version < Ada_2005 then
12328 ("access subtype would not be allowed in generic body "
12329 & "in Ada 2005?y?", S);
12332 ("access subtype not allowed in generic body", S);
12336 ("\designated type is a discriminated formal", S);
12339 end Constrain_Access;
12341 ---------------------
12342 -- Constrain_Array --
12343 ---------------------
12345 procedure Constrain_Array
12346 (Def_Id : in out Entity_Id;
12348 Related_Nod : Node_Id;
12349 Related_Id : Entity_Id;
12350 Suffix : Character)
12352 C : constant Node_Id := Constraint (SI);
12353 Number_Of_Constraints : Nat := 0;
12356 Constraint_OK : Boolean := True;
12359 T := Entity (Subtype_Mark (SI));
12361 if Is_Access_Type (T) then
12362 T := Designated_Type (T);
12365 -- If an index constraint follows a subtype mark in a subtype indication
12366 -- then the type or subtype denoted by the subtype mark must not already
12367 -- impose an index constraint. The subtype mark must denote either an
12368 -- unconstrained array type or an access type whose designated type
12369 -- is such an array type... (RM 3.6.1)
12371 if Is_Constrained (T) then
12372 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
12373 Constraint_OK := False;
12376 S := First (Constraints (C));
12377 while Present (S) loop
12378 Number_Of_Constraints := Number_Of_Constraints + 1;
12382 -- In either case, the index constraint must provide a discrete
12383 -- range for each index of the array type and the type of each
12384 -- discrete range must be the same as that of the corresponding
12385 -- index. (RM 3.6.1)
12387 if Number_Of_Constraints /= Number_Dimensions (T) then
12388 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
12389 Constraint_OK := False;
12392 S := First (Constraints (C));
12393 Index := First_Index (T);
12396 -- Apply constraints to each index type
12398 for J in 1 .. Number_Of_Constraints loop
12399 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
12407 if No (Def_Id) then
12409 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
12410 Set_Parent (Def_Id, Related_Nod);
12413 Set_Ekind (Def_Id, E_Array_Subtype);
12416 Set_Size_Info (Def_Id, (T));
12417 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
12418 Set_Etype (Def_Id, Base_Type (T));
12420 if Constraint_OK then
12421 Set_First_Index (Def_Id, First (Constraints (C)));
12423 Set_First_Index (Def_Id, First_Index (T));
12426 Set_Is_Constrained (Def_Id, True);
12427 Set_Is_Aliased (Def_Id, Is_Aliased (T));
12428 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12430 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
12431 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
12433 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
12434 -- We need to initialize the attribute because if Def_Id is previously
12435 -- analyzed through a limited_with clause, it will have the attributes
12436 -- of an incomplete type, one of which is an Elist that overlaps the
12437 -- Packed_Array_Impl_Type field.
12439 Set_Packed_Array_Impl_Type (Def_Id, Empty);
12441 -- Build a freeze node if parent still needs one. Also make sure that
12442 -- the Depends_On_Private status is set because the subtype will need
12443 -- reprocessing at the time the base type does, and also we must set a
12444 -- conditional delay.
12446 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
12447 Conditional_Delay (Def_Id, T);
12448 end Constrain_Array;
12450 ------------------------------
12451 -- Constrain_Component_Type --
12452 ------------------------------
12454 function Constrain_Component_Type
12456 Constrained_Typ : Entity_Id;
12457 Related_Node : Node_Id;
12459 Constraints : Elist_Id) return Entity_Id
12461 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
12462 Compon_Type : constant Entity_Id := Etype (Comp);
12464 function Build_Constrained_Array_Type
12465 (Old_Type : Entity_Id) return Entity_Id;
12466 -- If Old_Type is an array type, one of whose indexes is constrained
12467 -- by a discriminant, build an Itype whose constraint replaces the
12468 -- discriminant with its value in the constraint.
12470 function Build_Constrained_Discriminated_Type
12471 (Old_Type : Entity_Id) return Entity_Id;
12472 -- Ditto for record components
12474 function Build_Constrained_Access_Type
12475 (Old_Type : Entity_Id) return Entity_Id;
12476 -- Ditto for access types. Makes use of previous two functions, to
12477 -- constrain designated type.
12479 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
12480 -- T is an array or discriminated type, C is a list of constraints
12481 -- that apply to T. This routine builds the constrained subtype.
12483 function Is_Discriminant (Expr : Node_Id) return Boolean;
12484 -- Returns True if Expr is a discriminant
12486 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
12487 -- Find the value of discriminant Discrim in Constraint
12489 -----------------------------------
12490 -- Build_Constrained_Access_Type --
12491 -----------------------------------
12493 function Build_Constrained_Access_Type
12494 (Old_Type : Entity_Id) return Entity_Id
12496 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
12498 Desig_Subtype : Entity_Id;
12502 -- if the original access type was not embedded in the enclosing
12503 -- type definition, there is no need to produce a new access
12504 -- subtype. In fact every access type with an explicit constraint
12505 -- generates an itype whose scope is the enclosing record.
12507 if not Is_Type (Scope (Old_Type)) then
12510 elsif Is_Array_Type (Desig_Type) then
12511 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
12513 elsif Has_Discriminants (Desig_Type) then
12515 -- This may be an access type to an enclosing record type for
12516 -- which we are constructing the constrained components. Return
12517 -- the enclosing record subtype. This is not always correct,
12518 -- but avoids infinite recursion. ???
12520 Desig_Subtype := Any_Type;
12522 for J in reverse 0 .. Scope_Stack.Last loop
12523 Scop := Scope_Stack.Table (J).Entity;
12526 and then Base_Type (Scop) = Base_Type (Desig_Type)
12528 Desig_Subtype := Scop;
12531 exit when not Is_Type (Scop);
12534 if Desig_Subtype = Any_Type then
12536 Build_Constrained_Discriminated_Type (Desig_Type);
12543 if Desig_Subtype /= Desig_Type then
12545 -- The Related_Node better be here or else we won't be able
12546 -- to attach new itypes to a node in the tree.
12548 pragma Assert (Present (Related_Node));
12550 Itype := Create_Itype (E_Access_Subtype, Related_Node);
12552 Set_Etype (Itype, Base_Type (Old_Type));
12553 Set_Size_Info (Itype, (Old_Type));
12554 Set_Directly_Designated_Type (Itype, Desig_Subtype);
12555 Set_Depends_On_Private (Itype, Has_Private_Component
12557 Set_Is_Access_Constant (Itype, Is_Access_Constant
12560 -- The new itype needs freezing when it depends on a not frozen
12561 -- type and the enclosing subtype needs freezing.
12563 if Has_Delayed_Freeze (Constrained_Typ)
12564 and then not Is_Frozen (Constrained_Typ)
12566 Conditional_Delay (Itype, Base_Type (Old_Type));
12574 end Build_Constrained_Access_Type;
12576 ----------------------------------
12577 -- Build_Constrained_Array_Type --
12578 ----------------------------------
12580 function Build_Constrained_Array_Type
12581 (Old_Type : Entity_Id) return Entity_Id
12585 Old_Index : Node_Id;
12586 Range_Node : Node_Id;
12587 Constr_List : List_Id;
12589 Need_To_Create_Itype : Boolean := False;
12592 Old_Index := First_Index (Old_Type);
12593 while Present (Old_Index) loop
12594 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12596 if Is_Discriminant (Lo_Expr)
12598 Is_Discriminant (Hi_Expr)
12600 Need_To_Create_Itype := True;
12603 Next_Index (Old_Index);
12606 if Need_To_Create_Itype then
12607 Constr_List := New_List;
12609 Old_Index := First_Index (Old_Type);
12610 while Present (Old_Index) loop
12611 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
12613 if Is_Discriminant (Lo_Expr) then
12614 Lo_Expr := Get_Discr_Value (Lo_Expr);
12617 if Is_Discriminant (Hi_Expr) then
12618 Hi_Expr := Get_Discr_Value (Hi_Expr);
12623 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
12625 Append (Range_Node, To => Constr_List);
12627 Next_Index (Old_Index);
12630 return Build_Subtype (Old_Type, Constr_List);
12635 end Build_Constrained_Array_Type;
12637 ------------------------------------------
12638 -- Build_Constrained_Discriminated_Type --
12639 ------------------------------------------
12641 function Build_Constrained_Discriminated_Type
12642 (Old_Type : Entity_Id) return Entity_Id
12645 Constr_List : List_Id;
12646 Old_Constraint : Elmt_Id;
12648 Need_To_Create_Itype : Boolean := False;
12651 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12652 while Present (Old_Constraint) loop
12653 Expr := Node (Old_Constraint);
12655 if Is_Discriminant (Expr) then
12656 Need_To_Create_Itype := True;
12659 Next_Elmt (Old_Constraint);
12662 if Need_To_Create_Itype then
12663 Constr_List := New_List;
12665 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
12666 while Present (Old_Constraint) loop
12667 Expr := Node (Old_Constraint);
12669 if Is_Discriminant (Expr) then
12670 Expr := Get_Discr_Value (Expr);
12673 Append (New_Copy_Tree (Expr), To => Constr_List);
12675 Next_Elmt (Old_Constraint);
12678 return Build_Subtype (Old_Type, Constr_List);
12683 end Build_Constrained_Discriminated_Type;
12685 -------------------
12686 -- Build_Subtype --
12687 -------------------
12689 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
12691 Subtyp_Decl : Node_Id;
12692 Def_Id : Entity_Id;
12693 Btyp : Entity_Id := Base_Type (T);
12696 -- The Related_Node better be here or else we won't be able to
12697 -- attach new itypes to a node in the tree.
12699 pragma Assert (Present (Related_Node));
12701 -- If the view of the component's type is incomplete or private
12702 -- with unknown discriminants, then the constraint must be applied
12703 -- to the full type.
12705 if Has_Unknown_Discriminants (Btyp)
12706 and then Present (Underlying_Type (Btyp))
12708 Btyp := Underlying_Type (Btyp);
12712 Make_Subtype_Indication (Loc,
12713 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
12714 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
12716 Def_Id := Create_Itype (Ekind (T), Related_Node);
12719 Make_Subtype_Declaration (Loc,
12720 Defining_Identifier => Def_Id,
12721 Subtype_Indication => Indic);
12723 Set_Parent (Subtyp_Decl, Parent (Related_Node));
12725 -- Itypes must be analyzed with checks off (see package Itypes)
12727 Analyze (Subtyp_Decl, Suppress => All_Checks);
12732 ---------------------
12733 -- Get_Discr_Value --
12734 ---------------------
12736 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
12741 -- The discriminant may be declared for the type, in which case we
12742 -- find it by iterating over the list of discriminants. If the
12743 -- discriminant is inherited from a parent type, it appears as the
12744 -- corresponding discriminant of the current type. This will be the
12745 -- case when constraining an inherited component whose constraint is
12746 -- given by a discriminant of the parent.
12748 D := First_Discriminant (Typ);
12749 E := First_Elmt (Constraints);
12751 while Present (D) loop
12752 if D = Entity (Discrim)
12753 or else D = CR_Discriminant (Entity (Discrim))
12754 or else Corresponding_Discriminant (D) = Entity (Discrim)
12759 Next_Discriminant (D);
12763 -- The Corresponding_Discriminant mechanism is incomplete, because
12764 -- the correspondence between new and old discriminants is not one
12765 -- to one: one new discriminant can constrain several old ones. In
12766 -- that case, scan sequentially the stored_constraint, the list of
12767 -- discriminants of the parents, and the constraints.
12769 -- Previous code checked for the present of the Stored_Constraint
12770 -- list for the derived type, but did not use it at all. Should it
12771 -- be present when the component is a discriminated task type?
12773 if Is_Derived_Type (Typ)
12774 and then Scope (Entity (Discrim)) = Etype (Typ)
12776 D := First_Discriminant (Etype (Typ));
12777 E := First_Elmt (Constraints);
12778 while Present (D) loop
12779 if D = Entity (Discrim) then
12783 Next_Discriminant (D);
12788 -- Something is wrong if we did not find the value
12790 raise Program_Error;
12791 end Get_Discr_Value;
12793 ---------------------
12794 -- Is_Discriminant --
12795 ---------------------
12797 function Is_Discriminant (Expr : Node_Id) return Boolean is
12798 Discrim_Scope : Entity_Id;
12801 if Denotes_Discriminant (Expr) then
12802 Discrim_Scope := Scope (Entity (Expr));
12804 -- Either we have a reference to one of Typ's discriminants,
12806 pragma Assert (Discrim_Scope = Typ
12808 -- or to the discriminants of the parent type, in the case
12809 -- of a derivation of a tagged type with variants.
12811 or else Discrim_Scope = Etype (Typ)
12812 or else Full_View (Discrim_Scope) = Etype (Typ)
12814 -- or same as above for the case where the discriminants
12815 -- were declared in Typ's private view.
12817 or else (Is_Private_Type (Discrim_Scope)
12818 and then Chars (Discrim_Scope) = Chars (Typ))
12820 -- or else we are deriving from the full view and the
12821 -- discriminant is declared in the private entity.
12823 or else (Is_Private_Type (Typ)
12824 and then Chars (Discrim_Scope) = Chars (Typ))
12826 -- Or we are constrained the corresponding record of a
12827 -- synchronized type that completes a private declaration.
12829 or else (Is_Concurrent_Record_Type (Typ)
12831 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
12833 -- or we have a class-wide type, in which case make sure the
12834 -- discriminant found belongs to the root type.
12836 or else (Is_Class_Wide_Type (Typ)
12837 and then Etype (Typ) = Discrim_Scope));
12842 -- In all other cases we have something wrong
12845 end Is_Discriminant;
12847 -- Start of processing for Constrain_Component_Type
12850 if Nkind (Parent (Comp)) = N_Component_Declaration
12851 and then Comes_From_Source (Parent (Comp))
12852 and then Comes_From_Source
12853 (Subtype_Indication (Component_Definition (Parent (Comp))))
12856 (Subtype_Indication (Component_Definition (Parent (Comp))))
12858 return Compon_Type;
12860 elsif Is_Array_Type (Compon_Type) then
12861 return Build_Constrained_Array_Type (Compon_Type);
12863 elsif Has_Discriminants (Compon_Type) then
12864 return Build_Constrained_Discriminated_Type (Compon_Type);
12866 elsif Is_Access_Type (Compon_Type) then
12867 return Build_Constrained_Access_Type (Compon_Type);
12870 return Compon_Type;
12872 end Constrain_Component_Type;
12874 --------------------------
12875 -- Constrain_Concurrent --
12876 --------------------------
12878 -- For concurrent types, the associated record value type carries the same
12879 -- discriminants, so when we constrain a concurrent type, we must constrain
12880 -- the corresponding record type as well.
12882 procedure Constrain_Concurrent
12883 (Def_Id : in out Entity_Id;
12885 Related_Nod : Node_Id;
12886 Related_Id : Entity_Id;
12887 Suffix : Character)
12889 -- Retrieve Base_Type to ensure getting to the concurrent type in the
12890 -- case of a private subtype (needed when only doing semantic analysis).
12892 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
12896 if Is_Access_Type (T_Ent) then
12897 T_Ent := Designated_Type (T_Ent);
12900 T_Val := Corresponding_Record_Type (T_Ent);
12902 if Present (T_Val) then
12904 if No (Def_Id) then
12905 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12908 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12910 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
12911 Set_Corresponding_Record_Type (Def_Id,
12912 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
12915 -- If there is no associated record, expansion is disabled and this
12916 -- is a generic context. Create a subtype in any case, so that
12917 -- semantic analysis can proceed.
12919 if No (Def_Id) then
12920 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
12923 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
12925 end Constrain_Concurrent;
12927 ------------------------------------
12928 -- Constrain_Corresponding_Record --
12929 ------------------------------------
12931 function Constrain_Corresponding_Record
12932 (Prot_Subt : Entity_Id;
12933 Corr_Rec : Entity_Id;
12934 Related_Nod : Node_Id) return Entity_Id
12936 T_Sub : constant Entity_Id :=
12937 Create_Itype (E_Record_Subtype, Related_Nod, Corr_Rec, 'C');
12940 Set_Etype (T_Sub, Corr_Rec);
12941 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
12942 Set_Is_Constrained (T_Sub, True);
12943 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
12944 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
12946 if Has_Discriminants (Prot_Subt) then -- False only if errors.
12947 Set_Discriminant_Constraint
12948 (T_Sub, Discriminant_Constraint (Prot_Subt));
12949 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
12950 Create_Constrained_Components
12951 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
12954 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
12956 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
12957 Conditional_Delay (T_Sub, Corr_Rec);
12960 -- This is a component subtype: it will be frozen in the context of
12961 -- the enclosing record's init_proc, so that discriminant references
12962 -- are resolved to discriminals. (Note: we used to skip freezing
12963 -- altogether in that case, which caused errors downstream for
12964 -- components of a bit packed array type).
12966 Set_Has_Delayed_Freeze (T_Sub);
12970 end Constrain_Corresponding_Record;
12972 -----------------------
12973 -- Constrain_Decimal --
12974 -----------------------
12976 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
12977 T : constant Entity_Id := Entity (Subtype_Mark (S));
12978 C : constant Node_Id := Constraint (S);
12979 Loc : constant Source_Ptr := Sloc (C);
12980 Range_Expr : Node_Id;
12981 Digits_Expr : Node_Id;
12986 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
12988 if Nkind (C) = N_Range_Constraint then
12989 Range_Expr := Range_Expression (C);
12990 Digits_Val := Digits_Value (T);
12993 pragma Assert (Nkind (C) = N_Digits_Constraint);
12995 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
12997 Digits_Expr := Digits_Expression (C);
12998 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13000 Check_Digits_Expression (Digits_Expr);
13001 Digits_Val := Expr_Value (Digits_Expr);
13003 if Digits_Val > Digits_Value (T) then
13005 ("digits expression is incompatible with subtype", C);
13006 Digits_Val := Digits_Value (T);
13009 if Present (Range_Constraint (C)) then
13010 Range_Expr := Range_Expression (Range_Constraint (C));
13012 Range_Expr := Empty;
13016 Set_Etype (Def_Id, Base_Type (T));
13017 Set_Size_Info (Def_Id, (T));
13018 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13019 Set_Delta_Value (Def_Id, Delta_Value (T));
13020 Set_Scale_Value (Def_Id, Scale_Value (T));
13021 Set_Small_Value (Def_Id, Small_Value (T));
13022 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13023 Set_Digits_Value (Def_Id, Digits_Val);
13025 -- Manufacture range from given digits value if no range present
13027 if No (Range_Expr) then
13028 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13032 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13034 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13037 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13038 Set_Discrete_RM_Size (Def_Id);
13040 -- Unconditionally delay the freeze, since we cannot set size
13041 -- information in all cases correctly until the freeze point.
13043 Set_Has_Delayed_Freeze (Def_Id);
13044 end Constrain_Decimal;
13046 ----------------------------------
13047 -- Constrain_Discriminated_Type --
13048 ----------------------------------
13050 procedure Constrain_Discriminated_Type
13051 (Def_Id : Entity_Id;
13053 Related_Nod : Node_Id;
13054 For_Access : Boolean := False)
13056 E : constant Entity_Id := Entity (Subtype_Mark (S));
13059 Elist : Elist_Id := New_Elmt_List;
13061 procedure Fixup_Bad_Constraint;
13062 -- This is called after finding a bad constraint, and after having
13063 -- posted an appropriate error message. The mission is to leave the
13064 -- entity T in as reasonable state as possible.
13066 --------------------------
13067 -- Fixup_Bad_Constraint --
13068 --------------------------
13070 procedure Fixup_Bad_Constraint is
13072 -- Set a reasonable Ekind for the entity. For an incomplete type,
13073 -- we can't do much, but for other types, we can set the proper
13074 -- corresponding subtype kind.
13076 if Ekind (T) = E_Incomplete_Type then
13077 Set_Ekind (Def_Id, Ekind (T));
13079 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13082 -- Set Etype to the known type, to reduce chances of cascaded errors
13084 Set_Etype (Def_Id, E);
13085 Set_Error_Posted (Def_Id);
13086 end Fixup_Bad_Constraint;
13088 -- Start of processing for Constrain_Discriminated_Type
13091 C := Constraint (S);
13093 -- A discriminant constraint is only allowed in a subtype indication,
13094 -- after a subtype mark. This subtype mark must denote either a type
13095 -- with discriminants, or an access type whose designated type is a
13096 -- type with discriminants. A discriminant constraint specifies the
13097 -- values of these discriminants (RM 3.7.2(5)).
13099 T := Base_Type (Entity (Subtype_Mark (S)));
13101 if Is_Access_Type (T) then
13102 T := Designated_Type (T);
13105 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal.
13106 -- Avoid generating an error for access-to-incomplete subtypes.
13108 if Ada_Version >= Ada_2005
13109 and then Ekind (T) = E_Incomplete_Type
13110 and then Nkind (Parent (S)) = N_Subtype_Declaration
13111 and then not Is_Itype (Def_Id)
13113 -- A little sanity check, emit an error message if the type
13114 -- has discriminants to begin with. Type T may be a regular
13115 -- incomplete type or imported via a limited with clause.
13117 if Has_Discriminants (T)
13118 or else (From_Limited_With (T)
13119 and then Present (Non_Limited_View (T))
13120 and then Nkind (Parent (Non_Limited_View (T))) =
13121 N_Full_Type_Declaration
13122 and then Present (Discriminant_Specifications
13123 (Parent (Non_Limited_View (T)))))
13126 ("(Ada 2005) incomplete subtype may not be constrained", C);
13128 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13131 Fixup_Bad_Constraint;
13134 -- Check that the type has visible discriminants. The type may be
13135 -- a private type with unknown discriminants whose full view has
13136 -- discriminants which are invisible.
13138 elsif not Has_Discriminants (T)
13140 (Has_Unknown_Discriminants (T)
13141 and then Is_Private_Type (T))
13143 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13144 Fixup_Bad_Constraint;
13147 elsif Is_Constrained (E)
13148 or else (Ekind (E) = E_Class_Wide_Subtype
13149 and then Present (Discriminant_Constraint (E)))
13151 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13152 Fixup_Bad_Constraint;
13156 -- T may be an unconstrained subtype (e.g. a generic actual).
13157 -- Constraint applies to the base type.
13159 T := Base_Type (T);
13161 Elist := Build_Discriminant_Constraints (T, S);
13163 -- If the list returned was empty we had an error in building the
13164 -- discriminant constraint. We have also already signalled an error
13165 -- in the incomplete type case
13167 if Is_Empty_Elmt_List (Elist) then
13168 Fixup_Bad_Constraint;
13172 Build_Discriminated_Subtype (T, Def_Id, Elist, Related_Nod, For_Access);
13173 end Constrain_Discriminated_Type;
13175 ---------------------------
13176 -- Constrain_Enumeration --
13177 ---------------------------
13179 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13180 T : constant Entity_Id := Entity (Subtype_Mark (S));
13181 C : constant Node_Id := Constraint (S);
13184 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13186 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
13188 Set_Etype (Def_Id, Base_Type (T));
13189 Set_Size_Info (Def_Id, (T));
13190 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13191 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13193 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13195 Set_Discrete_RM_Size (Def_Id);
13196 end Constrain_Enumeration;
13198 ----------------------
13199 -- Constrain_Float --
13200 ----------------------
13202 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
13203 T : constant Entity_Id := Entity (Subtype_Mark (S));
13209 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
13211 Set_Etype (Def_Id, Base_Type (T));
13212 Set_Size_Info (Def_Id, (T));
13213 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13215 -- Process the constraint
13217 C := Constraint (S);
13219 -- Digits constraint present
13221 if Nkind (C) = N_Digits_Constraint then
13223 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13224 Check_Restriction (No_Obsolescent_Features, C);
13226 if Warn_On_Obsolescent_Feature then
13228 ("subtype digits constraint is an " &
13229 "obsolescent feature (RM J.3(8))?j?", C);
13232 D := Digits_Expression (C);
13233 Analyze_And_Resolve (D, Any_Integer);
13234 Check_Digits_Expression (D);
13235 Set_Digits_Value (Def_Id, Expr_Value (D));
13237 -- Check that digits value is in range. Obviously we can do this
13238 -- at compile time, but it is strictly a runtime check, and of
13239 -- course there is an ACVC test that checks this.
13241 if Digits_Value (Def_Id) > Digits_Value (T) then
13242 Error_Msg_Uint_1 := Digits_Value (T);
13243 Error_Msg_N ("??digits value is too large, maximum is ^", D);
13245 Make_Raise_Constraint_Error (Sloc (D),
13246 Reason => CE_Range_Check_Failed);
13247 Insert_Action (Declaration_Node (Def_Id), Rais);
13250 C := Range_Constraint (C);
13252 -- No digits constraint present
13255 Set_Digits_Value (Def_Id, Digits_Value (T));
13258 -- Range constraint present
13260 if Nkind (C) = N_Range_Constraint then
13261 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13263 -- No range constraint present
13266 pragma Assert (No (C));
13267 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13270 Set_Is_Constrained (Def_Id);
13271 end Constrain_Float;
13273 ---------------------
13274 -- Constrain_Index --
13275 ---------------------
13277 procedure Constrain_Index
13280 Related_Nod : Node_Id;
13281 Related_Id : Entity_Id;
13282 Suffix : Character;
13283 Suffix_Index : Nat)
13285 Def_Id : Entity_Id;
13286 R : Node_Id := Empty;
13287 T : constant Entity_Id := Etype (Index);
13291 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
13292 Set_Etype (Def_Id, Base_Type (T));
13294 if Nkind (S) = N_Range
13296 (Nkind (S) = N_Attribute_Reference
13297 and then Attribute_Name (S) = Name_Range)
13299 -- A Range attribute will be transformed into N_Range by Resolve
13305 Process_Range_Expr_In_Decl (R, T);
13307 if not Error_Posted (S)
13309 (Nkind (S) /= N_Range
13310 or else not Covers (T, (Etype (Low_Bound (S))))
13311 or else not Covers (T, (Etype (High_Bound (S)))))
13313 if Base_Type (T) /= Any_Type
13314 and then Etype (Low_Bound (S)) /= Any_Type
13315 and then Etype (High_Bound (S)) /= Any_Type
13317 Error_Msg_N ("range expected", S);
13321 elsif Nkind (S) = N_Subtype_Indication then
13323 -- The parser has verified that this is a discrete indication
13325 Resolve_Discrete_Subtype_Indication (S, T);
13326 Bad_Predicated_Subtype_Use
13327 ("subtype& has predicate, not allowed in index constraint",
13328 S, Entity (Subtype_Mark (S)));
13330 R := Range_Expression (Constraint (S));
13332 -- Capture values of bounds and generate temporaries for them if
13333 -- needed, since checks may cause duplication of the expressions
13334 -- which must not be reevaluated.
13336 -- The forced evaluation removes side effects from expressions, which
13337 -- should occur also in GNATprove mode. Otherwise, we end up with
13338 -- unexpected insertions of actions at places where this is not
13339 -- supposed to occur, e.g. on default parameters of a call.
13341 if Expander_Active or GNATprove_Mode then
13343 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
13345 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
13348 elsif Nkind (S) = N_Discriminant_Association then
13350 -- Syntactically valid in subtype indication
13352 Error_Msg_N ("invalid index constraint", S);
13353 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13356 -- Subtype_Mark case, no anonymous subtypes to construct
13361 if Is_Entity_Name (S) then
13362 if not Is_Type (Entity (S)) then
13363 Error_Msg_N ("expect subtype mark for index constraint", S);
13365 elsif Base_Type (Entity (S)) /= Base_Type (T) then
13366 Wrong_Type (S, Base_Type (T));
13368 -- Check error of subtype with predicate in index constraint
13371 Bad_Predicated_Subtype_Use
13372 ("subtype& has predicate, not allowed in index constraint",
13379 Error_Msg_N ("invalid index constraint", S);
13380 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
13385 -- Complete construction of the Itype
13387 if Is_Modular_Integer_Type (T) then
13388 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13390 elsif Is_Integer_Type (T) then
13391 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13394 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13395 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
13396 Set_First_Literal (Def_Id, First_Literal (T));
13399 Set_Size_Info (Def_Id, (T));
13400 Set_RM_Size (Def_Id, RM_Size (T));
13401 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13403 Set_Scalar_Range (Def_Id, R);
13405 Set_Etype (S, Def_Id);
13406 Set_Discrete_RM_Size (Def_Id);
13407 end Constrain_Index;
13409 -----------------------
13410 -- Constrain_Integer --
13411 -----------------------
13413 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
13414 T : constant Entity_Id := Entity (Subtype_Mark (S));
13415 C : constant Node_Id := Constraint (S);
13418 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13420 if Is_Modular_Integer_Type (T) then
13421 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
13423 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
13426 Set_Etype (Def_Id, Base_Type (T));
13427 Set_Size_Info (Def_Id, (T));
13428 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13429 Set_Discrete_RM_Size (Def_Id);
13430 end Constrain_Integer;
13432 ------------------------------
13433 -- Constrain_Ordinary_Fixed --
13434 ------------------------------
13436 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
13437 T : constant Entity_Id := Entity (Subtype_Mark (S));
13443 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
13444 Set_Etype (Def_Id, Base_Type (T));
13445 Set_Size_Info (Def_Id, (T));
13446 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13447 Set_Small_Value (Def_Id, Small_Value (T));
13449 -- Process the constraint
13451 C := Constraint (S);
13453 -- Delta constraint present
13455 if Nkind (C) = N_Delta_Constraint then
13457 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
13458 Check_Restriction (No_Obsolescent_Features, C);
13460 if Warn_On_Obsolescent_Feature then
13462 ("subtype delta constraint is an " &
13463 "obsolescent feature (RM J.3(7))?j?");
13466 D := Delta_Expression (C);
13467 Analyze_And_Resolve (D, Any_Real);
13468 Check_Delta_Expression (D);
13469 Set_Delta_Value (Def_Id, Expr_Value_R (D));
13471 -- Check that delta value is in range. Obviously we can do this
13472 -- at compile time, but it is strictly a runtime check, and of
13473 -- course there is an ACVC test that checks this.
13475 if Delta_Value (Def_Id) < Delta_Value (T) then
13476 Error_Msg_N ("??delta value is too small", D);
13478 Make_Raise_Constraint_Error (Sloc (D),
13479 Reason => CE_Range_Check_Failed);
13480 Insert_Action (Declaration_Node (Def_Id), Rais);
13483 C := Range_Constraint (C);
13485 -- No delta constraint present
13488 Set_Delta_Value (Def_Id, Delta_Value (T));
13491 -- Range constraint present
13493 if Nkind (C) = N_Range_Constraint then
13494 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
13496 -- No range constraint present
13499 pragma Assert (No (C));
13500 Set_Scalar_Range (Def_Id, Scalar_Range (T));
13503 Set_Discrete_RM_Size (Def_Id);
13505 -- Unconditionally delay the freeze, since we cannot set size
13506 -- information in all cases correctly until the freeze point.
13508 Set_Has_Delayed_Freeze (Def_Id);
13509 end Constrain_Ordinary_Fixed;
13511 -----------------------
13512 -- Contain_Interface --
13513 -----------------------
13515 function Contain_Interface
13516 (Iface : Entity_Id;
13517 Ifaces : Elist_Id) return Boolean
13519 Iface_Elmt : Elmt_Id;
13522 if Present (Ifaces) then
13523 Iface_Elmt := First_Elmt (Ifaces);
13524 while Present (Iface_Elmt) loop
13525 if Node (Iface_Elmt) = Iface then
13529 Next_Elmt (Iface_Elmt);
13534 end Contain_Interface;
13536 ---------------------------
13537 -- Convert_Scalar_Bounds --
13538 ---------------------------
13540 procedure Convert_Scalar_Bounds
13542 Parent_Type : Entity_Id;
13543 Derived_Type : Entity_Id;
13546 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
13553 -- Defend against previous errors
13555 if No (Scalar_Range (Derived_Type)) then
13556 Check_Error_Detected;
13560 Lo := Build_Scalar_Bound
13561 (Type_Low_Bound (Derived_Type),
13562 Parent_Type, Implicit_Base);
13564 Hi := Build_Scalar_Bound
13565 (Type_High_Bound (Derived_Type),
13566 Parent_Type, Implicit_Base);
13573 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
13575 Set_Parent (Rng, N);
13576 Set_Scalar_Range (Derived_Type, Rng);
13578 -- Analyze the bounds
13580 Analyze_And_Resolve (Lo, Implicit_Base);
13581 Analyze_And_Resolve (Hi, Implicit_Base);
13583 -- Analyze the range itself, except that we do not analyze it if
13584 -- the bounds are real literals, and we have a fixed-point type.
13585 -- The reason for this is that we delay setting the bounds in this
13586 -- case till we know the final Small and Size values (see circuit
13587 -- in Freeze.Freeze_Fixed_Point_Type for further details).
13589 if Is_Fixed_Point_Type (Parent_Type)
13590 and then Nkind (Lo) = N_Real_Literal
13591 and then Nkind (Hi) = N_Real_Literal
13595 -- Here we do the analysis of the range
13597 -- Note: we do this manually, since if we do a normal Analyze and
13598 -- Resolve call, there are problems with the conversions used for
13599 -- the derived type range.
13602 Set_Etype (Rng, Implicit_Base);
13603 Set_Analyzed (Rng, True);
13605 end Convert_Scalar_Bounds;
13607 -------------------
13608 -- Copy_And_Swap --
13609 -------------------
13611 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
13613 -- Initialize new full declaration entity by copying the pertinent
13614 -- fields of the corresponding private declaration entity.
13616 -- We temporarily set Ekind to a value appropriate for a type to
13617 -- avoid assert failures in Einfo from checking for setting type
13618 -- attributes on something that is not a type. Ekind (Priv) is an
13619 -- appropriate choice, since it allowed the attributes to be set
13620 -- in the first place. This Ekind value will be modified later.
13622 Set_Ekind (Full, Ekind (Priv));
13624 -- Also set Etype temporarily to Any_Type, again, in the absence
13625 -- of errors, it will be properly reset, and if there are errors,
13626 -- then we want a value of Any_Type to remain.
13628 Set_Etype (Full, Any_Type);
13630 -- Now start copying attributes
13632 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
13634 if Has_Discriminants (Full) then
13635 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
13636 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
13639 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
13640 Set_Homonym (Full, Homonym (Priv));
13641 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
13642 Set_Is_Public (Full, Is_Public (Priv));
13643 Set_Is_Pure (Full, Is_Pure (Priv));
13644 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
13645 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
13646 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
13647 Set_Has_Pragma_Unreferenced_Objects
13648 (Full, Has_Pragma_Unreferenced_Objects
13651 Conditional_Delay (Full, Priv);
13653 if Is_Tagged_Type (Full) then
13654 Set_Direct_Primitive_Operations
13655 (Full, Direct_Primitive_Operations (Priv));
13656 Set_No_Tagged_Streams_Pragma
13657 (Full, No_Tagged_Streams_Pragma (Priv));
13659 if Is_Base_Type (Priv) then
13660 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
13664 Set_Is_Volatile (Full, Is_Volatile (Priv));
13665 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
13666 Set_Scope (Full, Scope (Priv));
13667 Set_Next_Entity (Full, Next_Entity (Priv));
13668 Set_First_Entity (Full, First_Entity (Priv));
13669 Set_Last_Entity (Full, Last_Entity (Priv));
13671 -- If access types have been recorded for later handling, keep them in
13672 -- the full view so that they get handled when the full view freeze
13673 -- node is expanded.
13675 if Present (Freeze_Node (Priv))
13676 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
13678 Ensure_Freeze_Node (Full);
13679 Set_Access_Types_To_Process
13680 (Freeze_Node (Full),
13681 Access_Types_To_Process (Freeze_Node (Priv)));
13684 -- Swap the two entities. Now Private is the full type entity and Full
13685 -- is the private one. They will be swapped back at the end of the
13686 -- private part. This swapping ensures that the entity that is visible
13687 -- in the private part is the full declaration.
13689 Exchange_Entities (Priv, Full);
13690 Append_Entity (Full, Scope (Full));
13693 -------------------------------------
13694 -- Copy_Array_Base_Type_Attributes --
13695 -------------------------------------
13697 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
13699 Set_Component_Alignment (T1, Component_Alignment (T2));
13700 Set_Component_Type (T1, Component_Type (T2));
13701 Set_Component_Size (T1, Component_Size (T2));
13702 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
13703 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
13704 Set_Has_Protected (T1, Has_Protected (T2));
13705 Set_Has_Task (T1, Has_Task (T2));
13706 Set_Is_Packed (T1, Is_Packed (T2));
13707 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
13708 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
13709 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
13710 end Copy_Array_Base_Type_Attributes;
13712 -----------------------------------
13713 -- Copy_Array_Subtype_Attributes --
13714 -----------------------------------
13716 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
13718 Set_Size_Info (T1, T2);
13720 Set_First_Index (T1, First_Index (T2));
13721 Set_Is_Aliased (T1, Is_Aliased (T2));
13722 Set_Is_Volatile (T1, Is_Volatile (T2));
13723 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
13724 Set_Is_Constrained (T1, Is_Constrained (T2));
13725 Set_Depends_On_Private (T1, Has_Private_Component (T2));
13726 Inherit_Rep_Item_Chain (T1, T2);
13727 Set_Convention (T1, Convention (T2));
13728 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
13729 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
13730 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
13731 end Copy_Array_Subtype_Attributes;
13733 -----------------------------------
13734 -- Create_Constrained_Components --
13735 -----------------------------------
13737 procedure Create_Constrained_Components
13739 Decl_Node : Node_Id;
13741 Constraints : Elist_Id)
13743 Loc : constant Source_Ptr := Sloc (Subt);
13744 Comp_List : constant Elist_Id := New_Elmt_List;
13745 Parent_Type : constant Entity_Id := Etype (Typ);
13746 Assoc_List : constant List_Id := New_List;
13747 Discr_Val : Elmt_Id;
13751 Is_Static : Boolean := True;
13753 procedure Collect_Fixed_Components (Typ : Entity_Id);
13754 -- Collect parent type components that do not appear in a variant part
13756 procedure Create_All_Components;
13757 -- Iterate over Comp_List to create the components of the subtype
13759 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
13760 -- Creates a new component from Old_Compon, copying all the fields from
13761 -- it, including its Etype, inserts the new component in the Subt entity
13762 -- chain and returns the new component.
13764 function Is_Variant_Record (T : Entity_Id) return Boolean;
13765 -- If true, and discriminants are static, collect only components from
13766 -- variants selected by discriminant values.
13768 ------------------------------
13769 -- Collect_Fixed_Components --
13770 ------------------------------
13772 procedure Collect_Fixed_Components (Typ : Entity_Id) is
13774 -- Build association list for discriminants, and find components of the
13775 -- variant part selected by the values of the discriminants.
13777 Old_C := First_Discriminant (Typ);
13778 Discr_Val := First_Elmt (Constraints);
13779 while Present (Old_C) loop
13780 Append_To (Assoc_List,
13781 Make_Component_Association (Loc,
13782 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
13783 Expression => New_Copy (Node (Discr_Val))));
13785 Next_Elmt (Discr_Val);
13786 Next_Discriminant (Old_C);
13789 -- The tag and the possible parent component are unconditionally in
13792 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
13793 Old_C := First_Component (Typ);
13794 while Present (Old_C) loop
13795 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
13796 Append_Elmt (Old_C, Comp_List);
13799 Next_Component (Old_C);
13802 end Collect_Fixed_Components;
13804 ---------------------------
13805 -- Create_All_Components --
13806 ---------------------------
13808 procedure Create_All_Components is
13812 Comp := First_Elmt (Comp_List);
13813 while Present (Comp) loop
13814 Old_C := Node (Comp);
13815 New_C := Create_Component (Old_C);
13819 Constrain_Component_Type
13820 (Old_C, Subt, Decl_Node, Typ, Constraints));
13821 Set_Is_Public (New_C, Is_Public (Subt));
13825 end Create_All_Components;
13827 ----------------------
13828 -- Create_Component --
13829 ----------------------
13831 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
13832 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
13835 if Ekind (Old_Compon) = E_Discriminant
13836 and then Is_Completely_Hidden (Old_Compon)
13838 -- This is a shadow discriminant created for a discriminant of
13839 -- the parent type, which needs to be present in the subtype.
13840 -- Give the shadow discriminant an internal name that cannot
13841 -- conflict with that of visible components.
13843 Set_Chars (New_Compon, New_Internal_Name ('C'));
13846 -- Set the parent so we have a proper link for freezing etc. This is
13847 -- not a real parent pointer, since of course our parent does not own
13848 -- up to us and reference us, we are an illegitimate child of the
13849 -- original parent.
13851 Set_Parent (New_Compon, Parent (Old_Compon));
13853 -- If the old component's Esize was already determined and is a
13854 -- static value, then the new component simply inherits it. Otherwise
13855 -- the old component's size may require run-time determination, but
13856 -- the new component's size still might be statically determinable
13857 -- (if, for example it has a static constraint). In that case we want
13858 -- Layout_Type to recompute the component's size, so we reset its
13859 -- size and positional fields.
13861 if Frontend_Layout_On_Target
13862 and then not Known_Static_Esize (Old_Compon)
13864 Set_Esize (New_Compon, Uint_0);
13865 Init_Normalized_First_Bit (New_Compon);
13866 Init_Normalized_Position (New_Compon);
13867 Init_Normalized_Position_Max (New_Compon);
13870 -- We do not want this node marked as Comes_From_Source, since
13871 -- otherwise it would get first class status and a separate cross-
13872 -- reference line would be generated. Illegitimate children do not
13873 -- rate such recognition.
13875 Set_Comes_From_Source (New_Compon, False);
13877 -- But it is a real entity, and a birth certificate must be properly
13878 -- registered by entering it into the entity list.
13880 Enter_Name (New_Compon);
13883 end Create_Component;
13885 -----------------------
13886 -- Is_Variant_Record --
13887 -----------------------
13889 function Is_Variant_Record (T : Entity_Id) return Boolean is
13891 return Nkind (Parent (T)) = N_Full_Type_Declaration
13892 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
13893 and then Present (Component_List (Type_Definition (Parent (T))))
13896 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
13897 end Is_Variant_Record;
13899 -- Start of processing for Create_Constrained_Components
13902 pragma Assert (Subt /= Base_Type (Subt));
13903 pragma Assert (Typ = Base_Type (Typ));
13905 Set_First_Entity (Subt, Empty);
13906 Set_Last_Entity (Subt, Empty);
13908 -- Check whether constraint is fully static, in which case we can
13909 -- optimize the list of components.
13911 Discr_Val := First_Elmt (Constraints);
13912 while Present (Discr_Val) loop
13913 if not Is_OK_Static_Expression (Node (Discr_Val)) then
13914 Is_Static := False;
13918 Next_Elmt (Discr_Val);
13921 Set_Has_Static_Discriminants (Subt, Is_Static);
13925 -- Inherit the discriminants of the parent type
13927 Add_Discriminants : declare
13933 Old_C := First_Discriminant (Typ);
13935 while Present (Old_C) loop
13936 Num_Disc := Num_Disc + 1;
13937 New_C := Create_Component (Old_C);
13938 Set_Is_Public (New_C, Is_Public (Subt));
13939 Next_Discriminant (Old_C);
13942 -- For an untagged derived subtype, the number of discriminants may
13943 -- be smaller than the number of inherited discriminants, because
13944 -- several of them may be renamed by a single new discriminant or
13945 -- constrained. In this case, add the hidden discriminants back into
13946 -- the subtype, because they need to be present if the optimizer of
13947 -- the GCC 4.x back-end decides to break apart assignments between
13948 -- objects using the parent view into member-wise assignments.
13952 if Is_Derived_Type (Typ)
13953 and then not Is_Tagged_Type (Typ)
13955 Old_C := First_Stored_Discriminant (Typ);
13957 while Present (Old_C) loop
13958 Num_Gird := Num_Gird + 1;
13959 Next_Stored_Discriminant (Old_C);
13963 if Num_Gird > Num_Disc then
13965 -- Find out multiple uses of new discriminants, and add hidden
13966 -- components for the extra renamed discriminants. We recognize
13967 -- multiple uses through the Corresponding_Discriminant of a
13968 -- new discriminant: if it constrains several old discriminants,
13969 -- this field points to the last one in the parent type. The
13970 -- stored discriminants of the derived type have the same name
13971 -- as those of the parent.
13975 New_Discr : Entity_Id;
13976 Old_Discr : Entity_Id;
13979 Constr := First_Elmt (Stored_Constraint (Typ));
13980 Old_Discr := First_Stored_Discriminant (Typ);
13981 while Present (Constr) loop
13982 if Is_Entity_Name (Node (Constr))
13983 and then Ekind (Entity (Node (Constr))) = E_Discriminant
13985 New_Discr := Entity (Node (Constr));
13987 if Chars (Corresponding_Discriminant (New_Discr)) /=
13990 -- The new discriminant has been used to rename a
13991 -- subsequent old discriminant. Introduce a shadow
13992 -- component for the current old discriminant.
13994 New_C := Create_Component (Old_Discr);
13995 Set_Original_Record_Component (New_C, Old_Discr);
13999 -- The constraint has eliminated the old discriminant.
14000 -- Introduce a shadow component.
14002 New_C := Create_Component (Old_Discr);
14003 Set_Original_Record_Component (New_C, Old_Discr);
14006 Next_Elmt (Constr);
14007 Next_Stored_Discriminant (Old_Discr);
14011 end Add_Discriminants;
14014 and then Is_Variant_Record (Typ)
14016 Collect_Fixed_Components (Typ);
14018 Gather_Components (
14020 Component_List (Type_Definition (Parent (Typ))),
14021 Governed_By => Assoc_List,
14023 Report_Errors => Errors);
14024 pragma Assert (not Errors);
14026 Create_All_Components;
14028 -- If the subtype declaration is created for a tagged type derivation
14029 -- with constraints, we retrieve the record definition of the parent
14030 -- type to select the components of the proper variant.
14033 and then Is_Tagged_Type (Typ)
14034 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14036 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14037 and then Is_Variant_Record (Parent_Type)
14039 Collect_Fixed_Components (Typ);
14043 Component_List (Type_Definition (Parent (Parent_Type))),
14044 Governed_By => Assoc_List,
14046 Report_Errors => Errors);
14048 -- Note: previously there was a check at this point that no errors
14049 -- were detected. As a consequence of AI05-220 there may be an error
14050 -- if an inherited discriminant that controls a variant has a non-
14051 -- static constraint.
14053 -- If the tagged derivation has a type extension, collect all the
14054 -- new components therein.
14056 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14058 Old_C := First_Component (Typ);
14059 while Present (Old_C) loop
14060 if Original_Record_Component (Old_C) = Old_C
14061 and then Chars (Old_C) /= Name_uTag
14062 and then Chars (Old_C) /= Name_uParent
14064 Append_Elmt (Old_C, Comp_List);
14067 Next_Component (Old_C);
14071 Create_All_Components;
14074 -- If discriminants are not static, or if this is a multi-level type
14075 -- extension, we have to include all components of the parent type.
14077 Old_C := First_Component (Typ);
14078 while Present (Old_C) loop
14079 New_C := Create_Component (Old_C);
14083 Constrain_Component_Type
14084 (Old_C, Subt, Decl_Node, Typ, Constraints));
14085 Set_Is_Public (New_C, Is_Public (Subt));
14087 Next_Component (Old_C);
14092 end Create_Constrained_Components;
14094 ------------------------------------------
14095 -- Decimal_Fixed_Point_Type_Declaration --
14096 ------------------------------------------
14098 procedure Decimal_Fixed_Point_Type_Declaration
14102 Loc : constant Source_Ptr := Sloc (Def);
14103 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14104 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14105 Implicit_Base : Entity_Id;
14112 Check_SPARK_05_Restriction
14113 ("decimal fixed point type is not allowed", Def);
14114 Check_Restriction (No_Fixed_Point, Def);
14116 -- Create implicit base type
14119 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14120 Set_Etype (Implicit_Base, Implicit_Base);
14122 -- Analyze and process delta expression
14124 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14126 Check_Delta_Expression (Delta_Expr);
14127 Delta_Val := Expr_Value_R (Delta_Expr);
14129 -- Check delta is power of 10, and determine scale value from it
14135 Scale_Val := Uint_0;
14138 if Val < Ureal_1 then
14139 while Val < Ureal_1 loop
14140 Val := Val * Ureal_10;
14141 Scale_Val := Scale_Val + 1;
14144 if Scale_Val > 18 then
14145 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14146 Scale_Val := UI_From_Int (+18);
14150 while Val > Ureal_1 loop
14151 Val := Val / Ureal_10;
14152 Scale_Val := Scale_Val - 1;
14155 if Scale_Val < -18 then
14156 Error_Msg_N ("scale is less than minimum value of -18", Def);
14157 Scale_Val := UI_From_Int (-18);
14161 if Val /= Ureal_1 then
14162 Error_Msg_N ("delta expression must be a power of 10", Def);
14163 Delta_Val := Ureal_10 ** (-Scale_Val);
14167 -- Set delta, scale and small (small = delta for decimal type)
14169 Set_Delta_Value (Implicit_Base, Delta_Val);
14170 Set_Scale_Value (Implicit_Base, Scale_Val);
14171 Set_Small_Value (Implicit_Base, Delta_Val);
14173 -- Analyze and process digits expression
14175 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14176 Check_Digits_Expression (Digs_Expr);
14177 Digs_Val := Expr_Value (Digs_Expr);
14179 if Digs_Val > 18 then
14180 Digs_Val := UI_From_Int (+18);
14181 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14184 Set_Digits_Value (Implicit_Base, Digs_Val);
14185 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14187 -- Set range of base type from digits value for now. This will be
14188 -- expanded to represent the true underlying base range by Freeze.
14190 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14192 -- Note: We leave size as zero for now, size will be set at freeze
14193 -- time. We have to do this for ordinary fixed-point, because the size
14194 -- depends on the specified small, and we might as well do the same for
14195 -- decimal fixed-point.
14197 pragma Assert (Esize (Implicit_Base) = Uint_0);
14199 -- If there are bounds given in the declaration use them as the
14200 -- bounds of the first named subtype.
14202 if Present (Real_Range_Specification (Def)) then
14204 RRS : constant Node_Id := Real_Range_Specification (Def);
14205 Low : constant Node_Id := Low_Bound (RRS);
14206 High : constant Node_Id := High_Bound (RRS);
14211 Analyze_And_Resolve (Low, Any_Real);
14212 Analyze_And_Resolve (High, Any_Real);
14213 Check_Real_Bound (Low);
14214 Check_Real_Bound (High);
14215 Low_Val := Expr_Value_R (Low);
14216 High_Val := Expr_Value_R (High);
14218 if Low_Val < (-Bound_Val) then
14220 ("range low bound too small for digits value", Low);
14221 Low_Val := -Bound_Val;
14224 if High_Val > Bound_Val then
14226 ("range high bound too large for digits value", High);
14227 High_Val := Bound_Val;
14230 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
14233 -- If no explicit range, use range that corresponds to given
14234 -- digits value. This will end up as the final range for the
14238 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
14241 -- Complete entity for first subtype. The inheritance of the rep item
14242 -- chain ensures that SPARK-related pragmas are not clobbered when the
14243 -- decimal fixed point type acts as a full view of a private type.
14245 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
14246 Set_Etype (T, Implicit_Base);
14247 Set_Size_Info (T, Implicit_Base);
14248 Inherit_Rep_Item_Chain (T, Implicit_Base);
14249 Set_Digits_Value (T, Digs_Val);
14250 Set_Delta_Value (T, Delta_Val);
14251 Set_Small_Value (T, Delta_Val);
14252 Set_Scale_Value (T, Scale_Val);
14253 Set_Is_Constrained (T);
14254 end Decimal_Fixed_Point_Type_Declaration;
14256 -----------------------------------
14257 -- Derive_Progenitor_Subprograms --
14258 -----------------------------------
14260 procedure Derive_Progenitor_Subprograms
14261 (Parent_Type : Entity_Id;
14262 Tagged_Type : Entity_Id)
14267 Iface_Elmt : Elmt_Id;
14268 Iface_Subp : Entity_Id;
14269 New_Subp : Entity_Id := Empty;
14270 Prim_Elmt : Elmt_Id;
14275 pragma Assert (Ada_Version >= Ada_2005
14276 and then Is_Record_Type (Tagged_Type)
14277 and then Is_Tagged_Type (Tagged_Type)
14278 and then Has_Interfaces (Tagged_Type));
14280 -- Step 1: Transfer to the full-view primitives associated with the
14281 -- partial-view that cover interface primitives. Conceptually this
14282 -- work should be done later by Process_Full_View; done here to
14283 -- simplify its implementation at later stages. It can be safely
14284 -- done here because interfaces must be visible in the partial and
14285 -- private view (RM 7.3(7.3/2)).
14287 -- Small optimization: This work is only required if the parent may
14288 -- have entities whose Alias attribute reference an interface primitive.
14289 -- Such a situation may occur if the parent is an abstract type and the
14290 -- primitive has not been yet overridden or if the parent is a generic
14291 -- formal type covering interfaces.
14293 -- If the tagged type is not abstract, it cannot have abstract
14294 -- primitives (the only entities in the list of primitives of
14295 -- non-abstract tagged types that can reference abstract primitives
14296 -- through its Alias attribute are the internal entities that have
14297 -- attribute Interface_Alias, and these entities are generated later
14298 -- by Add_Internal_Interface_Entities).
14300 if In_Private_Part (Current_Scope)
14301 and then (Is_Abstract_Type (Parent_Type)
14303 Is_Generic_Type (Parent_Type))
14305 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
14306 while Present (Elmt) loop
14307 Subp := Node (Elmt);
14309 -- At this stage it is not possible to have entities in the list
14310 -- of primitives that have attribute Interface_Alias.
14312 pragma Assert (No (Interface_Alias (Subp)));
14314 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
14316 if Is_Interface (Typ) then
14317 E := Find_Primitive_Covering_Interface
14318 (Tagged_Type => Tagged_Type,
14319 Iface_Prim => Subp);
14322 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
14324 Replace_Elmt (Elmt, E);
14325 Remove_Homonym (Subp);
14333 -- Step 2: Add primitives of progenitors that are not implemented by
14334 -- parents of Tagged_Type.
14336 if Present (Interfaces (Base_Type (Tagged_Type))) then
14337 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
14338 while Present (Iface_Elmt) loop
14339 Iface := Node (Iface_Elmt);
14341 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
14342 while Present (Prim_Elmt) loop
14343 Iface_Subp := Node (Prim_Elmt);
14345 -- Exclude derivation of predefined primitives except those
14346 -- that come from source, or are inherited from one that comes
14347 -- from source. Required to catch declarations of equality
14348 -- operators of interfaces. For example:
14350 -- type Iface is interface;
14351 -- function "=" (Left, Right : Iface) return Boolean;
14353 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
14354 or else Comes_From_Source (Ultimate_Alias (Iface_Subp))
14356 E := Find_Primitive_Covering_Interface
14357 (Tagged_Type => Tagged_Type,
14358 Iface_Prim => Iface_Subp);
14360 -- If not found we derive a new primitive leaving its alias
14361 -- attribute referencing the interface primitive.
14365 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14367 -- Ada 2012 (AI05-0197): If the covering primitive's name
14368 -- differs from the name of the interface primitive then it
14369 -- is a private primitive inherited from a parent type. In
14370 -- such case, given that Tagged_Type covers the interface,
14371 -- the inherited private primitive becomes visible. For such
14372 -- purpose we add a new entity that renames the inherited
14373 -- private primitive.
14375 elsif Chars (E) /= Chars (Iface_Subp) then
14376 pragma Assert (Has_Suffix (E, 'P'));
14378 (New_Subp, Iface_Subp, Tagged_Type, Iface);
14379 Set_Alias (New_Subp, E);
14380 Set_Is_Abstract_Subprogram (New_Subp,
14381 Is_Abstract_Subprogram (E));
14383 -- Propagate to the full view interface entities associated
14384 -- with the partial view.
14386 elsif In_Private_Part (Current_Scope)
14387 and then Present (Alias (E))
14388 and then Alias (E) = Iface_Subp
14390 List_Containing (Parent (E)) /=
14391 Private_Declarations
14393 (Unit_Declaration_Node (Current_Scope)))
14395 Append_Elmt (E, Primitive_Operations (Tagged_Type));
14399 Next_Elmt (Prim_Elmt);
14402 Next_Elmt (Iface_Elmt);
14405 end Derive_Progenitor_Subprograms;
14407 -----------------------
14408 -- Derive_Subprogram --
14409 -----------------------
14411 procedure Derive_Subprogram
14412 (New_Subp : in out Entity_Id;
14413 Parent_Subp : Entity_Id;
14414 Derived_Type : Entity_Id;
14415 Parent_Type : Entity_Id;
14416 Actual_Subp : Entity_Id := Empty)
14418 Formal : Entity_Id;
14419 -- Formal parameter of parent primitive operation
14421 Formal_Of_Actual : Entity_Id;
14422 -- Formal parameter of actual operation, when the derivation is to
14423 -- create a renaming for a primitive operation of an actual in an
14426 New_Formal : Entity_Id;
14427 -- Formal of inherited operation
14429 Visible_Subp : Entity_Id := Parent_Subp;
14431 function Is_Private_Overriding return Boolean;
14432 -- If Subp is a private overriding of a visible operation, the inherited
14433 -- operation derives from the overridden op (even though its body is the
14434 -- overriding one) and the inherited operation is visible now. See
14435 -- sem_disp to see the full details of the handling of the overridden
14436 -- subprogram, which is removed from the list of primitive operations of
14437 -- the type. The overridden subprogram is saved locally in Visible_Subp,
14438 -- and used to diagnose abstract operations that need overriding in the
14441 procedure Replace_Type (Id, New_Id : Entity_Id);
14442 -- When the type is an anonymous access type, create a new access type
14443 -- designating the derived type.
14445 procedure Set_Derived_Name;
14446 -- This procedure sets the appropriate Chars name for New_Subp. This
14447 -- is normally just a copy of the parent name. An exception arises for
14448 -- type support subprograms, where the name is changed to reflect the
14449 -- name of the derived type, e.g. if type foo is derived from type bar,
14450 -- then a procedure barDA is derived with a name fooDA.
14452 ---------------------------
14453 -- Is_Private_Overriding --
14454 ---------------------------
14456 function Is_Private_Overriding return Boolean is
14460 -- If the parent is not a dispatching operation there is no
14461 -- need to investigate overridings
14463 if not Is_Dispatching_Operation (Parent_Subp) then
14467 -- The visible operation that is overridden is a homonym of the
14468 -- parent subprogram. We scan the homonym chain to find the one
14469 -- whose alias is the subprogram we are deriving.
14471 Prev := Current_Entity (Parent_Subp);
14472 while Present (Prev) loop
14473 if Ekind (Prev) = Ekind (Parent_Subp)
14474 and then Alias (Prev) = Parent_Subp
14475 and then Scope (Parent_Subp) = Scope (Prev)
14476 and then not Is_Hidden (Prev)
14478 Visible_Subp := Prev;
14482 Prev := Homonym (Prev);
14486 end Is_Private_Overriding;
14492 procedure Replace_Type (Id, New_Id : Entity_Id) is
14493 Id_Type : constant Entity_Id := Etype (Id);
14494 Acc_Type : Entity_Id;
14495 Par : constant Node_Id := Parent (Derived_Type);
14498 -- When the type is an anonymous access type, create a new access
14499 -- type designating the derived type. This itype must be elaborated
14500 -- at the point of the derivation, not on subsequent calls that may
14501 -- be out of the proper scope for Gigi, so we insert a reference to
14502 -- it after the derivation.
14504 if Ekind (Id_Type) = E_Anonymous_Access_Type then
14506 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
14509 if Ekind (Desig_Typ) = E_Record_Type_With_Private
14510 and then Present (Full_View (Desig_Typ))
14511 and then not Is_Private_Type (Parent_Type)
14513 Desig_Typ := Full_View (Desig_Typ);
14516 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
14518 -- Ada 2005 (AI-251): Handle also derivations of abstract
14519 -- interface primitives.
14521 or else (Is_Interface (Desig_Typ)
14522 and then not Is_Class_Wide_Type (Desig_Typ))
14524 Acc_Type := New_Copy (Id_Type);
14525 Set_Etype (Acc_Type, Acc_Type);
14526 Set_Scope (Acc_Type, New_Subp);
14528 -- Set size of anonymous access type. If we have an access
14529 -- to an unconstrained array, this is a fat pointer, so it
14530 -- is sizes at twice addtress size.
14532 if Is_Array_Type (Desig_Typ)
14533 and then not Is_Constrained (Desig_Typ)
14535 Init_Size (Acc_Type, 2 * System_Address_Size);
14537 -- Other cases use a thin pointer
14540 Init_Size (Acc_Type, System_Address_Size);
14543 -- Set remaining characterstics of anonymous access type
14545 Init_Alignment (Acc_Type);
14546 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
14548 Set_Etype (New_Id, Acc_Type);
14549 Set_Scope (New_Id, New_Subp);
14551 -- Create a reference to it
14553 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
14556 Set_Etype (New_Id, Id_Type);
14560 -- In Ada2012, a formal may have an incomplete type but the type
14561 -- derivation that inherits the primitive follows the full view.
14563 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
14565 (Ekind (Id_Type) = E_Record_Type_With_Private
14566 and then Present (Full_View (Id_Type))
14568 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
14570 (Ada_Version >= Ada_2012
14571 and then Ekind (Id_Type) = E_Incomplete_Type
14572 and then Full_View (Id_Type) = Parent_Type)
14574 -- Constraint checks on formals are generated during expansion,
14575 -- based on the signature of the original subprogram. The bounds
14576 -- of the derived type are not relevant, and thus we can use
14577 -- the base type for the formals. However, the return type may be
14578 -- used in a context that requires that the proper static bounds
14579 -- be used (a case statement, for example) and for those cases
14580 -- we must use the derived type (first subtype), not its base.
14582 -- If the derived_type_definition has no constraints, we know that
14583 -- the derived type has the same constraints as the first subtype
14584 -- of the parent, and we can also use it rather than its base,
14585 -- which can lead to more efficient code.
14587 if Etype (Id) = Parent_Type then
14588 if Is_Scalar_Type (Parent_Type)
14590 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
14592 Set_Etype (New_Id, Derived_Type);
14594 elsif Nkind (Par) = N_Full_Type_Declaration
14596 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
14599 (Subtype_Indication (Type_Definition (Par)))
14601 Set_Etype (New_Id, Derived_Type);
14604 Set_Etype (New_Id, Base_Type (Derived_Type));
14608 Set_Etype (New_Id, Base_Type (Derived_Type));
14612 Set_Etype (New_Id, Etype (Id));
14616 ----------------------
14617 -- Set_Derived_Name --
14618 ----------------------
14620 procedure Set_Derived_Name is
14621 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
14623 if Nm = TSS_Null then
14624 Set_Chars (New_Subp, Chars (Parent_Subp));
14626 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
14628 end Set_Derived_Name;
14630 -- Start of processing for Derive_Subprogram
14633 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
14634 Set_Ekind (New_Subp, Ekind (Parent_Subp));
14636 -- Check whether the inherited subprogram is a private operation that
14637 -- should be inherited but not yet made visible. Such subprograms can
14638 -- become visible at a later point (e.g., the private part of a public
14639 -- child unit) via Declare_Inherited_Private_Subprograms. If the
14640 -- following predicate is true, then this is not such a private
14641 -- operation and the subprogram simply inherits the name of the parent
14642 -- subprogram. Note the special check for the names of controlled
14643 -- operations, which are currently exempted from being inherited with
14644 -- a hidden name because they must be findable for generation of
14645 -- implicit run-time calls.
14647 if not Is_Hidden (Parent_Subp)
14648 or else Is_Internal (Parent_Subp)
14649 or else Is_Private_Overriding
14650 or else Is_Internal_Name (Chars (Parent_Subp))
14651 or else Nam_In (Chars (Parent_Subp), Name_Initialize,
14657 -- An inherited dispatching equality will be overridden by an internally
14658 -- generated one, or by an explicit one, so preserve its name and thus
14659 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
14660 -- private operation it may become invisible if the full view has
14661 -- progenitors, and the dispatch table will be malformed.
14662 -- We check that the type is limited to handle the anomalous declaration
14663 -- of Limited_Controlled, which is derived from a non-limited type, and
14664 -- which is handled specially elsewhere as well.
14666 elsif Chars (Parent_Subp) = Name_Op_Eq
14667 and then Is_Dispatching_Operation (Parent_Subp)
14668 and then Etype (Parent_Subp) = Standard_Boolean
14669 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
14671 Etype (First_Formal (Parent_Subp)) =
14672 Etype (Next_Formal (First_Formal (Parent_Subp)))
14676 -- If parent is hidden, this can be a regular derivation if the
14677 -- parent is immediately visible in a non-instantiating context,
14678 -- or if we are in the private part of an instance. This test
14679 -- should still be refined ???
14681 -- The test for In_Instance_Not_Visible avoids inheriting the derived
14682 -- operation as a non-visible operation in cases where the parent
14683 -- subprogram might not be visible now, but was visible within the
14684 -- original generic, so it would be wrong to make the inherited
14685 -- subprogram non-visible now. (Not clear if this test is fully
14686 -- correct; are there any cases where we should declare the inherited
14687 -- operation as not visible to avoid it being overridden, e.g., when
14688 -- the parent type is a generic actual with private primitives ???)
14690 -- (they should be treated the same as other private inherited
14691 -- subprograms, but it's not clear how to do this cleanly). ???
14693 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
14694 and then Is_Immediately_Visible (Parent_Subp)
14695 and then not In_Instance)
14696 or else In_Instance_Not_Visible
14700 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
14701 -- overrides an interface primitive because interface primitives
14702 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
14704 elsif Ada_Version >= Ada_2005
14705 and then Is_Dispatching_Operation (Parent_Subp)
14706 and then Covers_Some_Interface (Parent_Subp)
14710 -- Otherwise, the type is inheriting a private operation, so enter
14711 -- it with a special name so it can't be overridden.
14714 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
14717 Set_Parent (New_Subp, Parent (Derived_Type));
14719 if Present (Actual_Subp) then
14720 Replace_Type (Actual_Subp, New_Subp);
14722 Replace_Type (Parent_Subp, New_Subp);
14725 Conditional_Delay (New_Subp, Parent_Subp);
14727 -- If we are creating a renaming for a primitive operation of an
14728 -- actual of a generic derived type, we must examine the signature
14729 -- of the actual primitive, not that of the generic formal, which for
14730 -- example may be an interface. However the name and initial value
14731 -- of the inherited operation are those of the formal primitive.
14733 Formal := First_Formal (Parent_Subp);
14735 if Present (Actual_Subp) then
14736 Formal_Of_Actual := First_Formal (Actual_Subp);
14738 Formal_Of_Actual := Empty;
14741 while Present (Formal) loop
14742 New_Formal := New_Copy (Formal);
14744 -- Normally we do not go copying parents, but in the case of
14745 -- formals, we need to link up to the declaration (which is the
14746 -- parameter specification), and it is fine to link up to the
14747 -- original formal's parameter specification in this case.
14749 Set_Parent (New_Formal, Parent (Formal));
14750 Append_Entity (New_Formal, New_Subp);
14752 if Present (Formal_Of_Actual) then
14753 Replace_Type (Formal_Of_Actual, New_Formal);
14754 Next_Formal (Formal_Of_Actual);
14756 Replace_Type (Formal, New_Formal);
14759 Next_Formal (Formal);
14762 -- If this derivation corresponds to a tagged generic actual, then
14763 -- primitive operations rename those of the actual. Otherwise the
14764 -- primitive operations rename those of the parent type, If the parent
14765 -- renames an intrinsic operator, so does the new subprogram. We except
14766 -- concatenation, which is always properly typed, and does not get
14767 -- expanded as other intrinsic operations.
14769 if No (Actual_Subp) then
14770 if Is_Intrinsic_Subprogram (Parent_Subp) then
14771 Set_Is_Intrinsic_Subprogram (New_Subp);
14773 if Present (Alias (Parent_Subp))
14774 and then Chars (Parent_Subp) /= Name_Op_Concat
14776 Set_Alias (New_Subp, Alias (Parent_Subp));
14778 Set_Alias (New_Subp, Parent_Subp);
14782 Set_Alias (New_Subp, Parent_Subp);
14786 Set_Alias (New_Subp, Actual_Subp);
14789 -- Inherit the "ghostness" from the parent subprogram
14791 if Is_Ghost_Entity (Alias (New_Subp)) then
14792 Set_Is_Ghost_Entity (New_Subp);
14795 -- Derived subprograms of a tagged type must inherit the convention
14796 -- of the parent subprogram (a requirement of AI-117). Derived
14797 -- subprograms of untagged types simply get convention Ada by default.
14799 -- If the derived type is a tagged generic formal type with unknown
14800 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
14802 -- However, if the type is derived from a generic formal, the further
14803 -- inherited subprogram has the convention of the non-generic ancestor.
14804 -- Otherwise there would be no way to override the operation.
14805 -- (This is subject to forthcoming ARG discussions).
14807 if Is_Tagged_Type (Derived_Type) then
14808 if Is_Generic_Type (Derived_Type)
14809 and then Has_Unknown_Discriminants (Derived_Type)
14811 Set_Convention (New_Subp, Convention_Intrinsic);
14814 if Is_Generic_Type (Parent_Type)
14815 and then Has_Unknown_Discriminants (Parent_Type)
14817 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
14819 Set_Convention (New_Subp, Convention (Parent_Subp));
14824 -- Predefined controlled operations retain their name even if the parent
14825 -- is hidden (see above), but they are not primitive operations if the
14826 -- ancestor is not visible, for example if the parent is a private
14827 -- extension completed with a controlled extension. Note that a full
14828 -- type that is controlled can break privacy: the flag Is_Controlled is
14829 -- set on both views of the type.
14831 if Is_Controlled (Parent_Type)
14832 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
14835 and then Is_Hidden (Parent_Subp)
14836 and then not Is_Visibly_Controlled (Parent_Type)
14838 Set_Is_Hidden (New_Subp);
14841 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
14842 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
14844 if Ekind (Parent_Subp) = E_Procedure then
14845 Set_Is_Valued_Procedure
14846 (New_Subp, Is_Valued_Procedure (Parent_Subp));
14848 Set_Has_Controlling_Result
14849 (New_Subp, Has_Controlling_Result (Parent_Subp));
14852 -- No_Return must be inherited properly. If this is overridden in the
14853 -- case of a dispatching operation, then a check is made in Sem_Disp
14854 -- that the overriding operation is also No_Return (no such check is
14855 -- required for the case of non-dispatching operation.
14857 Set_No_Return (New_Subp, No_Return (Parent_Subp));
14859 -- A derived function with a controlling result is abstract. If the
14860 -- Derived_Type is a nonabstract formal generic derived type, then
14861 -- inherited operations are not abstract: the required check is done at
14862 -- instantiation time. If the derivation is for a generic actual, the
14863 -- function is not abstract unless the actual is.
14865 if Is_Generic_Type (Derived_Type)
14866 and then not Is_Abstract_Type (Derived_Type)
14870 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
14871 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
14873 -- A subprogram subject to pragma Extensions_Visible with value False
14874 -- requires overriding if the subprogram has at least one controlling
14875 -- OUT parameter (SPARK RM 6.1.7(6)).
14877 elsif Ada_Version >= Ada_2005
14878 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14879 or else (Is_Tagged_Type (Derived_Type)
14880 and then Etype (New_Subp) = Derived_Type
14881 and then not Is_Null_Extension (Derived_Type))
14882 or else (Is_Tagged_Type (Derived_Type)
14883 and then Ekind (Etype (New_Subp)) =
14884 E_Anonymous_Access_Type
14885 and then Designated_Type (Etype (New_Subp)) =
14887 and then not Is_Null_Extension (Derived_Type))
14888 or else (Comes_From_Source (Alias (New_Subp))
14889 and then Is_EVF_Procedure (Alias (New_Subp))))
14890 and then No (Actual_Subp)
14892 if not Is_Tagged_Type (Derived_Type)
14893 or else Is_Abstract_Type (Derived_Type)
14894 or else Is_Abstract_Subprogram (Alias (New_Subp))
14896 Set_Is_Abstract_Subprogram (New_Subp);
14898 Set_Requires_Overriding (New_Subp);
14901 elsif Ada_Version < Ada_2005
14902 and then (Is_Abstract_Subprogram (Alias (New_Subp))
14903 or else (Is_Tagged_Type (Derived_Type)
14904 and then Etype (New_Subp) = Derived_Type
14905 and then No (Actual_Subp)))
14907 Set_Is_Abstract_Subprogram (New_Subp);
14909 -- AI05-0097 : an inherited operation that dispatches on result is
14910 -- abstract if the derived type is abstract, even if the parent type
14911 -- is concrete and the derived type is a null extension.
14913 elsif Has_Controlling_Result (Alias (New_Subp))
14914 and then Is_Abstract_Type (Etype (New_Subp))
14916 Set_Is_Abstract_Subprogram (New_Subp);
14918 -- Finally, if the parent type is abstract we must verify that all
14919 -- inherited operations are either non-abstract or overridden, or that
14920 -- the derived type itself is abstract (this check is performed at the
14921 -- end of a package declaration, in Check_Abstract_Overriding). A
14922 -- private overriding in the parent type will not be visible in the
14923 -- derivation if we are not in an inner package or in a child unit of
14924 -- the parent type, in which case the abstractness of the inherited
14925 -- operation is carried to the new subprogram.
14927 elsif Is_Abstract_Type (Parent_Type)
14928 and then not In_Open_Scopes (Scope (Parent_Type))
14929 and then Is_Private_Overriding
14930 and then Is_Abstract_Subprogram (Visible_Subp)
14932 if No (Actual_Subp) then
14933 Set_Alias (New_Subp, Visible_Subp);
14934 Set_Is_Abstract_Subprogram (New_Subp, True);
14937 -- If this is a derivation for an instance of a formal derived
14938 -- type, abstractness comes from the primitive operation of the
14939 -- actual, not from the operation inherited from the ancestor.
14941 Set_Is_Abstract_Subprogram
14942 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
14946 New_Overloaded_Entity (New_Subp, Derived_Type);
14948 -- Check for case of a derived subprogram for the instantiation of a
14949 -- formal derived tagged type, if so mark the subprogram as dispatching
14950 -- and inherit the dispatching attributes of the actual subprogram. The
14951 -- derived subprogram is effectively renaming of the actual subprogram,
14952 -- so it needs to have the same attributes as the actual.
14954 if Present (Actual_Subp)
14955 and then Is_Dispatching_Operation (Actual_Subp)
14957 Set_Is_Dispatching_Operation (New_Subp);
14959 if Present (DTC_Entity (Actual_Subp)) then
14960 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
14961 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
14965 -- Indicate that a derived subprogram does not require a body and that
14966 -- it does not require processing of default expressions.
14968 Set_Has_Completion (New_Subp);
14969 Set_Default_Expressions_Processed (New_Subp);
14971 if Ekind (New_Subp) = E_Function then
14972 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
14974 end Derive_Subprogram;
14976 ------------------------
14977 -- Derive_Subprograms --
14978 ------------------------
14980 procedure Derive_Subprograms
14981 (Parent_Type : Entity_Id;
14982 Derived_Type : Entity_Id;
14983 Generic_Actual : Entity_Id := Empty)
14985 Op_List : constant Elist_Id :=
14986 Collect_Primitive_Operations (Parent_Type);
14988 function Check_Derived_Type return Boolean;
14989 -- Check that all the entities derived from Parent_Type are found in
14990 -- the list of primitives of Derived_Type exactly in the same order.
14992 procedure Derive_Interface_Subprogram
14993 (New_Subp : in out Entity_Id;
14995 Actual_Subp : Entity_Id);
14996 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
14997 -- (which is an interface primitive). If Generic_Actual is present then
14998 -- Actual_Subp is the actual subprogram corresponding with the generic
14999 -- subprogram Subp.
15001 function Check_Derived_Type return Boolean is
15005 New_Subp : Entity_Id;
15010 -- Traverse list of entities in the current scope searching for
15011 -- an incomplete type whose full-view is derived type
15013 E := First_Entity (Scope (Derived_Type));
15014 while Present (E) and then E /= Derived_Type loop
15015 if Ekind (E) = E_Incomplete_Type
15016 and then Present (Full_View (E))
15017 and then Full_View (E) = Derived_Type
15019 -- Disable this test if Derived_Type completes an incomplete
15020 -- type because in such case more primitives can be added
15021 -- later to the list of primitives of Derived_Type by routine
15022 -- Process_Incomplete_Dependents
15027 E := Next_Entity (E);
15030 List := Collect_Primitive_Operations (Derived_Type);
15031 Elmt := First_Elmt (List);
15033 Op_Elmt := First_Elmt (Op_List);
15034 while Present (Op_Elmt) loop
15035 Subp := Node (Op_Elmt);
15036 New_Subp := Node (Elmt);
15038 -- At this early stage Derived_Type has no entities with attribute
15039 -- Interface_Alias. In addition, such primitives are always
15040 -- located at the end of the list of primitives of Parent_Type.
15041 -- Therefore, if found we can safely stop processing pending
15044 exit when Present (Interface_Alias (Subp));
15046 -- Handle hidden entities
15048 if not Is_Predefined_Dispatching_Operation (Subp)
15049 and then Is_Hidden (Subp)
15051 if Present (New_Subp)
15052 and then Primitive_Names_Match (Subp, New_Subp)
15058 if not Present (New_Subp)
15059 or else Ekind (Subp) /= Ekind (New_Subp)
15060 or else not Primitive_Names_Match (Subp, New_Subp)
15068 Next_Elmt (Op_Elmt);
15072 end Check_Derived_Type;
15074 ---------------------------------
15075 -- Derive_Interface_Subprogram --
15076 ---------------------------------
15078 procedure Derive_Interface_Subprogram
15079 (New_Subp : in out Entity_Id;
15081 Actual_Subp : Entity_Id)
15083 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15084 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15087 pragma Assert (Is_Interface (Iface_Type));
15090 (New_Subp => New_Subp,
15091 Parent_Subp => Iface_Subp,
15092 Derived_Type => Derived_Type,
15093 Parent_Type => Iface_Type,
15094 Actual_Subp => Actual_Subp);
15096 -- Given that this new interface entity corresponds with a primitive
15097 -- of the parent that was not overridden we must leave it associated
15098 -- with its parent primitive to ensure that it will share the same
15099 -- dispatch table slot when overridden.
15101 if No (Actual_Subp) then
15102 Set_Alias (New_Subp, Subp);
15104 -- For instantiations this is not needed since the previous call to
15105 -- Derive_Subprogram leaves the entity well decorated.
15108 pragma Assert (Alias (New_Subp) = Actual_Subp);
15111 end Derive_Interface_Subprogram;
15115 Alias_Subp : Entity_Id;
15116 Act_List : Elist_Id;
15117 Act_Elmt : Elmt_Id;
15118 Act_Subp : Entity_Id := Empty;
15120 Need_Search : Boolean := False;
15121 New_Subp : Entity_Id := Empty;
15122 Parent_Base : Entity_Id;
15125 -- Start of processing for Derive_Subprograms
15128 if Ekind (Parent_Type) = E_Record_Type_With_Private
15129 and then Has_Discriminants (Parent_Type)
15130 and then Present (Full_View (Parent_Type))
15132 Parent_Base := Full_View (Parent_Type);
15134 Parent_Base := Parent_Type;
15137 if Present (Generic_Actual) then
15138 Act_List := Collect_Primitive_Operations (Generic_Actual);
15139 Act_Elmt := First_Elmt (Act_List);
15141 Act_List := No_Elist;
15142 Act_Elmt := No_Elmt;
15145 -- Derive primitives inherited from the parent. Note that if the generic
15146 -- actual is present, this is not really a type derivation, it is a
15147 -- completion within an instance.
15149 -- Case 1: Derived_Type does not implement interfaces
15151 if not Is_Tagged_Type (Derived_Type)
15152 or else (not Has_Interfaces (Derived_Type)
15153 and then not (Present (Generic_Actual)
15154 and then Has_Interfaces (Generic_Actual)))
15156 Elmt := First_Elmt (Op_List);
15157 while Present (Elmt) loop
15158 Subp := Node (Elmt);
15160 -- Literals are derived earlier in the process of building the
15161 -- derived type, and are skipped here.
15163 if Ekind (Subp) = E_Enumeration_Literal then
15166 -- The actual is a direct descendant and the common primitive
15167 -- operations appear in the same order.
15169 -- If the generic parent type is present, the derived type is an
15170 -- instance of a formal derived type, and within the instance its
15171 -- operations are those of the actual. We derive from the formal
15172 -- type but make the inherited operations aliases of the
15173 -- corresponding operations of the actual.
15176 pragma Assert (No (Node (Act_Elmt))
15177 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
15180 (Subp, Node (Act_Elmt),
15181 Skip_Controlling_Formals => True)));
15184 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
15186 if Present (Act_Elmt) then
15187 Next_Elmt (Act_Elmt);
15194 -- Case 2: Derived_Type implements interfaces
15197 -- If the parent type has no predefined primitives we remove
15198 -- predefined primitives from the list of primitives of generic
15199 -- actual to simplify the complexity of this algorithm.
15201 if Present (Generic_Actual) then
15203 Has_Predefined_Primitives : Boolean := False;
15206 -- Check if the parent type has predefined primitives
15208 Elmt := First_Elmt (Op_List);
15209 while Present (Elmt) loop
15210 Subp := Node (Elmt);
15212 if Is_Predefined_Dispatching_Operation (Subp)
15213 and then not Comes_From_Source (Ultimate_Alias (Subp))
15215 Has_Predefined_Primitives := True;
15222 -- Remove predefined primitives of Generic_Actual. We must use
15223 -- an auxiliary list because in case of tagged types the value
15224 -- returned by Collect_Primitive_Operations is the value stored
15225 -- in its Primitive_Operations attribute (and we don't want to
15226 -- modify its current contents).
15228 if not Has_Predefined_Primitives then
15230 Aux_List : constant Elist_Id := New_Elmt_List;
15233 Elmt := First_Elmt (Act_List);
15234 while Present (Elmt) loop
15235 Subp := Node (Elmt);
15237 if not Is_Predefined_Dispatching_Operation (Subp)
15238 or else Comes_From_Source (Subp)
15240 Append_Elmt (Subp, Aux_List);
15246 Act_List := Aux_List;
15250 Act_Elmt := First_Elmt (Act_List);
15251 Act_Subp := Node (Act_Elmt);
15255 -- Stage 1: If the generic actual is not present we derive the
15256 -- primitives inherited from the parent type. If the generic parent
15257 -- type is present, the derived type is an instance of a formal
15258 -- derived type, and within the instance its operations are those of
15259 -- the actual. We derive from the formal type but make the inherited
15260 -- operations aliases of the corresponding operations of the actual.
15262 Elmt := First_Elmt (Op_List);
15263 while Present (Elmt) loop
15264 Subp := Node (Elmt);
15265 Alias_Subp := Ultimate_Alias (Subp);
15267 -- Do not derive internal entities of the parent that link
15268 -- interface primitives with their covering primitive. These
15269 -- entities will be added to this type when frozen.
15271 if Present (Interface_Alias (Subp)) then
15275 -- If the generic actual is present find the corresponding
15276 -- operation in the generic actual. If the parent type is a
15277 -- direct ancestor of the derived type then, even if it is an
15278 -- interface, the operations are inherited from the primary
15279 -- dispatch table and are in the proper order. If we detect here
15280 -- that primitives are not in the same order we traverse the list
15281 -- of primitive operations of the actual to find the one that
15282 -- implements the interface primitive.
15286 (Present (Generic_Actual)
15287 and then Present (Act_Subp)
15289 (Primitive_Names_Match (Subp, Act_Subp)
15291 Type_Conformant (Subp, Act_Subp,
15292 Skip_Controlling_Formals => True)))
15294 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
15295 Use_Full_View => True));
15297 -- Remember that we need searching for all pending primitives
15299 Need_Search := True;
15301 -- Handle entities associated with interface primitives
15303 if Present (Alias_Subp)
15304 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15305 and then not Is_Predefined_Dispatching_Operation (Subp)
15307 -- Search for the primitive in the homonym chain
15310 Find_Primitive_Covering_Interface
15311 (Tagged_Type => Generic_Actual,
15312 Iface_Prim => Alias_Subp);
15314 -- Previous search may not locate primitives covering
15315 -- interfaces defined in generics units or instantiations.
15316 -- (it fails if the covering primitive has formals whose
15317 -- type is also defined in generics or instantiations).
15318 -- In such case we search in the list of primitives of the
15319 -- generic actual for the internal entity that links the
15320 -- interface primitive and the covering primitive.
15323 and then Is_Generic_Type (Parent_Type)
15325 -- This code has been designed to handle only generic
15326 -- formals that implement interfaces that are defined
15327 -- in a generic unit or instantiation. If this code is
15328 -- needed for other cases we must review it because
15329 -- (given that it relies on Original_Location to locate
15330 -- the primitive of Generic_Actual that covers the
15331 -- interface) it could leave linked through attribute
15332 -- Alias entities of unrelated instantiations).
15336 (Scope (Find_Dispatching_Type (Alias_Subp)))
15338 Instantiation_Depth
15339 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
15342 Iface_Prim_Loc : constant Source_Ptr :=
15343 Original_Location (Sloc (Alias_Subp));
15350 First_Elmt (Primitive_Operations (Generic_Actual));
15352 Search : while Present (Elmt) loop
15353 Prim := Node (Elmt);
15355 if Present (Interface_Alias (Prim))
15356 and then Original_Location
15357 (Sloc (Interface_Alias (Prim))) =
15360 Act_Subp := Alias (Prim);
15369 pragma Assert (Present (Act_Subp)
15370 or else Is_Abstract_Type (Generic_Actual)
15371 or else Serious_Errors_Detected > 0);
15373 -- Handle predefined primitives plus the rest of user-defined
15377 Act_Elmt := First_Elmt (Act_List);
15378 while Present (Act_Elmt) loop
15379 Act_Subp := Node (Act_Elmt);
15381 exit when Primitive_Names_Match (Subp, Act_Subp)
15382 and then Type_Conformant
15384 Skip_Controlling_Formals => True)
15385 and then No (Interface_Alias (Act_Subp));
15387 Next_Elmt (Act_Elmt);
15390 if No (Act_Elmt) then
15396 -- Case 1: If the parent is a limited interface then it has the
15397 -- predefined primitives of synchronized interfaces. However, the
15398 -- actual type may be a non-limited type and hence it does not
15399 -- have such primitives.
15401 if Present (Generic_Actual)
15402 and then not Present (Act_Subp)
15403 and then Is_Limited_Interface (Parent_Base)
15404 and then Is_Predefined_Interface_Primitive (Subp)
15408 -- Case 2: Inherit entities associated with interfaces that were
15409 -- not covered by the parent type. We exclude here null interface
15410 -- primitives because they do not need special management.
15412 -- We also exclude interface operations that are renamings. If the
15413 -- subprogram is an explicit renaming of an interface primitive,
15414 -- it is a regular primitive operation, and the presence of its
15415 -- alias is not relevant: it has to be derived like any other
15418 elsif Present (Alias (Subp))
15419 and then Nkind (Unit_Declaration_Node (Subp)) /=
15420 N_Subprogram_Renaming_Declaration
15421 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
15423 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
15424 and then Null_Present (Parent (Alias_Subp)))
15426 -- If this is an abstract private type then we transfer the
15427 -- derivation of the interface primitive from the partial view
15428 -- to the full view. This is safe because all the interfaces
15429 -- must be visible in the partial view. Done to avoid adding
15430 -- a new interface derivation to the private part of the
15431 -- enclosing package; otherwise this new derivation would be
15432 -- decorated as hidden when the analysis of the enclosing
15433 -- package completes.
15435 if Is_Abstract_Type (Derived_Type)
15436 and then In_Private_Part (Current_Scope)
15437 and then Has_Private_Declaration (Derived_Type)
15440 Partial_View : Entity_Id;
15445 Partial_View := First_Entity (Current_Scope);
15447 exit when No (Partial_View)
15448 or else (Has_Private_Declaration (Partial_View)
15450 Full_View (Partial_View) = Derived_Type);
15452 Next_Entity (Partial_View);
15455 -- If the partial view was not found then the source code
15456 -- has errors and the derivation is not needed.
15458 if Present (Partial_View) then
15460 First_Elmt (Primitive_Operations (Partial_View));
15461 while Present (Elmt) loop
15462 Ent := Node (Elmt);
15464 if Present (Alias (Ent))
15465 and then Ultimate_Alias (Ent) = Alias (Subp)
15468 (Ent, Primitive_Operations (Derived_Type));
15475 -- If the interface primitive was not found in the
15476 -- partial view then this interface primitive was
15477 -- overridden. We add a derivation to activate in
15478 -- Derive_Progenitor_Subprograms the machinery to
15482 Derive_Interface_Subprogram
15483 (New_Subp => New_Subp,
15485 Actual_Subp => Act_Subp);
15490 Derive_Interface_Subprogram
15491 (New_Subp => New_Subp,
15493 Actual_Subp => Act_Subp);
15496 -- Case 3: Common derivation
15500 (New_Subp => New_Subp,
15501 Parent_Subp => Subp,
15502 Derived_Type => Derived_Type,
15503 Parent_Type => Parent_Base,
15504 Actual_Subp => Act_Subp);
15507 -- No need to update Act_Elm if we must search for the
15508 -- corresponding operation in the generic actual
15511 and then Present (Act_Elmt)
15513 Next_Elmt (Act_Elmt);
15514 Act_Subp := Node (Act_Elmt);
15521 -- Inherit additional operations from progenitors. If the derived
15522 -- type is a generic actual, there are not new primitive operations
15523 -- for the type because it has those of the actual, and therefore
15524 -- nothing needs to be done. The renamings generated above are not
15525 -- primitive operations, and their purpose is simply to make the
15526 -- proper operations visible within an instantiation.
15528 if No (Generic_Actual) then
15529 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
15533 -- Final check: Direct descendants must have their primitives in the
15534 -- same order. We exclude from this test untagged types and instances
15535 -- of formal derived types. We skip this test if we have already
15536 -- reported serious errors in the sources.
15538 pragma Assert (not Is_Tagged_Type (Derived_Type)
15539 or else Present (Generic_Actual)
15540 or else Serious_Errors_Detected > 0
15541 or else Check_Derived_Type);
15542 end Derive_Subprograms;
15544 --------------------------------
15545 -- Derived_Standard_Character --
15546 --------------------------------
15548 procedure Derived_Standard_Character
15550 Parent_Type : Entity_Id;
15551 Derived_Type : Entity_Id)
15553 Loc : constant Source_Ptr := Sloc (N);
15554 Def : constant Node_Id := Type_Definition (N);
15555 Indic : constant Node_Id := Subtype_Indication (Def);
15556 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
15557 Implicit_Base : constant Entity_Id :=
15559 (E_Enumeration_Type, N, Derived_Type, 'B');
15565 Discard_Node (Process_Subtype (Indic, N));
15567 Set_Etype (Implicit_Base, Parent_Base);
15568 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
15569 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
15571 Set_Is_Character_Type (Implicit_Base, True);
15572 Set_Has_Delayed_Freeze (Implicit_Base);
15574 -- The bounds of the implicit base are the bounds of the parent base.
15575 -- Note that their type is the parent base.
15577 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
15578 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
15580 Set_Scalar_Range (Implicit_Base,
15583 High_Bound => Hi));
15585 Conditional_Delay (Derived_Type, Parent_Type);
15587 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
15588 Set_Etype (Derived_Type, Implicit_Base);
15589 Set_Size_Info (Derived_Type, Parent_Type);
15591 if Unknown_RM_Size (Derived_Type) then
15592 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
15595 Set_Is_Character_Type (Derived_Type, True);
15597 if Nkind (Indic) /= N_Subtype_Indication then
15599 -- If no explicit constraint, the bounds are those
15600 -- of the parent type.
15602 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
15603 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
15604 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
15607 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
15609 -- Because the implicit base is used in the conversion of the bounds, we
15610 -- have to freeze it now. This is similar to what is done for numeric
15611 -- types, and it equally suspicious, but otherwise a non-static bound
15612 -- will have a reference to an unfrozen type, which is rejected by Gigi
15613 -- (???). This requires specific care for definition of stream
15614 -- attributes. For details, see comments at the end of
15615 -- Build_Derived_Numeric_Type.
15617 Freeze_Before (N, Implicit_Base);
15618 end Derived_Standard_Character;
15620 ------------------------------
15621 -- Derived_Type_Declaration --
15622 ------------------------------
15624 procedure Derived_Type_Declaration
15627 Is_Completion : Boolean)
15629 Parent_Type : Entity_Id;
15631 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
15632 -- Check whether the parent type is a generic formal, or derives
15633 -- directly or indirectly from one.
15635 ------------------------
15636 -- Comes_From_Generic --
15637 ------------------------
15639 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
15641 if Is_Generic_Type (Typ) then
15644 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
15647 elsif Is_Private_Type (Typ)
15648 and then Present (Full_View (Typ))
15649 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
15653 elsif Is_Generic_Actual_Type (Typ) then
15659 end Comes_From_Generic;
15663 Def : constant Node_Id := Type_Definition (N);
15664 Iface_Def : Node_Id;
15665 Indic : constant Node_Id := Subtype_Indication (Def);
15666 Extension : constant Node_Id := Record_Extension_Part (Def);
15667 Parent_Node : Node_Id;
15670 -- Start of processing for Derived_Type_Declaration
15673 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
15675 -- Ada 2005 (AI-251): In case of interface derivation check that the
15676 -- parent is also an interface.
15678 if Interface_Present (Def) then
15679 Check_SPARK_05_Restriction ("interface is not allowed", Def);
15681 if not Is_Interface (Parent_Type) then
15682 Diagnose_Interface (Indic, Parent_Type);
15685 Parent_Node := Parent (Base_Type (Parent_Type));
15686 Iface_Def := Type_Definition (Parent_Node);
15688 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
15689 -- other limited interfaces.
15691 if Limited_Present (Def) then
15692 if Limited_Present (Iface_Def) then
15695 elsif Protected_Present (Iface_Def) then
15697 ("descendant of& must be declared"
15698 & " as a protected interface",
15701 elsif Synchronized_Present (Iface_Def) then
15703 ("descendant of& must be declared"
15704 & " as a synchronized interface",
15707 elsif Task_Present (Iface_Def) then
15709 ("descendant of& must be declared as a task interface",
15714 ("(Ada 2005) limited interface cannot "
15715 & "inherit from non-limited interface", Indic);
15718 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
15719 -- from non-limited or limited interfaces.
15721 elsif not Protected_Present (Def)
15722 and then not Synchronized_Present (Def)
15723 and then not Task_Present (Def)
15725 if Limited_Present (Iface_Def) then
15728 elsif Protected_Present (Iface_Def) then
15730 ("descendant of& must be declared"
15731 & " as a protected interface",
15734 elsif Synchronized_Present (Iface_Def) then
15736 ("descendant of& must be declared"
15737 & " as a synchronized interface",
15740 elsif Task_Present (Iface_Def) then
15742 ("descendant of& must be declared as a task interface",
15751 if Is_Tagged_Type (Parent_Type)
15752 and then Is_Concurrent_Type (Parent_Type)
15753 and then not Is_Interface (Parent_Type)
15756 ("parent type of a record extension cannot be "
15757 & "a synchronized tagged type (RM 3.9.1 (3/1))", N);
15758 Set_Etype (T, Any_Type);
15762 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
15765 if Is_Tagged_Type (Parent_Type)
15766 and then Is_Non_Empty_List (Interface_List (Def))
15773 Intf := First (Interface_List (Def));
15774 while Present (Intf) loop
15775 T := Find_Type_Of_Subtype_Indic (Intf);
15777 if not Is_Interface (T) then
15778 Diagnose_Interface (Intf, T);
15780 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
15781 -- a limited type from having a nonlimited progenitor.
15783 elsif (Limited_Present (Def)
15784 or else (not Is_Interface (Parent_Type)
15785 and then Is_Limited_Type (Parent_Type)))
15786 and then not Is_Limited_Interface (T)
15789 ("progenitor interface& of limited type must be limited",
15798 if Parent_Type = Any_Type
15799 or else Etype (Parent_Type) = Any_Type
15800 or else (Is_Class_Wide_Type (Parent_Type)
15801 and then Etype (Parent_Type) = T)
15803 -- If Parent_Type is undefined or illegal, make new type into a
15804 -- subtype of Any_Type, and set a few attributes to prevent cascaded
15805 -- errors. If this is a self-definition, emit error now.
15807 if T = Parent_Type or else T = Etype (Parent_Type) then
15808 Error_Msg_N ("type cannot be used in its own definition", Indic);
15811 Set_Ekind (T, Ekind (Parent_Type));
15812 Set_Etype (T, Any_Type);
15813 Set_Scalar_Range (T, Scalar_Range (Any_Type));
15815 if Is_Tagged_Type (T)
15816 and then Is_Record_Type (T)
15818 Set_Direct_Primitive_Operations (T, New_Elmt_List);
15824 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
15825 -- an interface is special because the list of interfaces in the full
15826 -- view can be given in any order. For example:
15828 -- type A is interface;
15829 -- type B is interface and A;
15830 -- type D is new B with private;
15832 -- type D is new A and B with null record; -- 1 --
15834 -- In this case we perform the following transformation of -1-:
15836 -- type D is new B and A with null record;
15838 -- If the parent of the full-view covers the parent of the partial-view
15839 -- we have two possible cases:
15841 -- 1) They have the same parent
15842 -- 2) The parent of the full-view implements some further interfaces
15844 -- In both cases we do not need to perform the transformation. In the
15845 -- first case the source program is correct and the transformation is
15846 -- not needed; in the second case the source program does not fulfill
15847 -- the no-hidden interfaces rule (AI-396) and the error will be reported
15850 -- This transformation not only simplifies the rest of the analysis of
15851 -- this type declaration but also simplifies the correct generation of
15852 -- the object layout to the expander.
15854 if In_Private_Part (Current_Scope)
15855 and then Is_Interface (Parent_Type)
15859 Partial_View : Entity_Id;
15860 Partial_View_Parent : Entity_Id;
15861 New_Iface : Node_Id;
15864 -- Look for the associated private type declaration
15866 Partial_View := First_Entity (Current_Scope);
15868 exit when No (Partial_View)
15869 or else (Has_Private_Declaration (Partial_View)
15870 and then Full_View (Partial_View) = T);
15872 Next_Entity (Partial_View);
15875 -- If the partial view was not found then the source code has
15876 -- errors and the transformation is not needed.
15878 if Present (Partial_View) then
15879 Partial_View_Parent := Etype (Partial_View);
15881 -- If the parent of the full-view covers the parent of the
15882 -- partial-view we have nothing else to do.
15884 if Interface_Present_In_Ancestor
15885 (Parent_Type, Partial_View_Parent)
15889 -- Traverse the list of interfaces of the full-view to look
15890 -- for the parent of the partial-view and perform the tree
15894 Iface := First (Interface_List (Def));
15895 while Present (Iface) loop
15896 if Etype (Iface) = Etype (Partial_View) then
15897 Rewrite (Subtype_Indication (Def),
15898 New_Copy (Subtype_Indication
15899 (Parent (Partial_View))));
15902 Make_Identifier (Sloc (N), Chars (Parent_Type));
15903 Append (New_Iface, Interface_List (Def));
15905 -- Analyze the transformed code
15907 Derived_Type_Declaration (T, N, Is_Completion);
15918 -- Only composite types other than array types are allowed to have
15921 if Present (Discriminant_Specifications (N)) then
15922 if (Is_Elementary_Type (Parent_Type)
15924 Is_Array_Type (Parent_Type))
15925 and then not Error_Posted (N)
15928 ("elementary or array type cannot have discriminants",
15929 Defining_Identifier (First (Discriminant_Specifications (N))));
15930 Set_Has_Discriminants (T, False);
15932 -- The type is allowed to have discriminants
15935 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
15939 -- In Ada 83, a derived type defined in a package specification cannot
15940 -- be used for further derivation until the end of its visible part.
15941 -- Note that derivation in the private part of the package is allowed.
15943 if Ada_Version = Ada_83
15944 and then Is_Derived_Type (Parent_Type)
15945 and then In_Visible_Part (Scope (Parent_Type))
15947 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
15949 ("(Ada 83): premature use of type for derivation", Indic);
15953 -- Check for early use of incomplete or private type
15955 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
15956 Error_Msg_N ("premature derivation of incomplete type", Indic);
15959 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
15960 and then not Comes_From_Generic (Parent_Type))
15961 or else Has_Private_Component (Parent_Type)
15963 -- The ancestor type of a formal type can be incomplete, in which
15964 -- case only the operations of the partial view are available in the
15965 -- generic. Subsequent checks may be required when the full view is
15966 -- analyzed to verify that a derivation from a tagged type has an
15969 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
15972 elsif No (Underlying_Type (Parent_Type))
15973 or else Has_Private_Component (Parent_Type)
15976 ("premature derivation of derived or private type", Indic);
15978 -- Flag the type itself as being in error, this prevents some
15979 -- nasty problems with subsequent uses of the malformed type.
15981 Set_Error_Posted (T);
15983 -- Check that within the immediate scope of an untagged partial
15984 -- view it's illegal to derive from the partial view if the
15985 -- full view is tagged. (7.3(7))
15987 -- We verify that the Parent_Type is a partial view by checking
15988 -- that it is not a Full_Type_Declaration (i.e. a private type or
15989 -- private extension declaration), to distinguish a partial view
15990 -- from a derivation from a private type which also appears as
15991 -- E_Private_Type. If the parent base type is not declared in an
15992 -- enclosing scope there is no need to check.
15994 elsif Present (Full_View (Parent_Type))
15995 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
15996 and then not Is_Tagged_Type (Parent_Type)
15997 and then Is_Tagged_Type (Full_View (Parent_Type))
15998 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16001 ("premature derivation from type with tagged full view",
16006 -- Check that form of derivation is appropriate
16008 Taggd := Is_Tagged_Type (Parent_Type);
16010 -- Set the parent type to the class-wide type's specific type in this
16011 -- case to prevent cascading errors
16013 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16014 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16015 Set_Etype (T, Etype (Parent_Type));
16019 if Present (Extension) and then not Taggd then
16021 ("type derived from untagged type cannot have extension", Indic);
16023 elsif No (Extension) and then Taggd then
16025 -- If this declaration is within a private part (or body) of a
16026 -- generic instantiation then the derivation is allowed (the parent
16027 -- type can only appear tagged in this case if it's a generic actual
16028 -- type, since it would otherwise have been rejected in the analysis
16029 -- of the generic template).
16031 if not Is_Generic_Actual_Type (Parent_Type)
16032 or else In_Visible_Part (Scope (Parent_Type))
16034 if Is_Class_Wide_Type (Parent_Type) then
16036 ("parent type must not be a class-wide type", Indic);
16038 -- Use specific type to prevent cascaded errors.
16040 Parent_Type := Etype (Parent_Type);
16044 ("type derived from tagged type must have extension", Indic);
16049 -- AI-443: Synchronized formal derived types require a private
16050 -- extension. There is no point in checking the ancestor type or
16051 -- the progenitors since the construct is wrong to begin with.
16053 if Ada_Version >= Ada_2005
16054 and then Is_Generic_Type (T)
16055 and then Present (Original_Node (N))
16058 Decl : constant Node_Id := Original_Node (N);
16061 if Nkind (Decl) = N_Formal_Type_Declaration
16062 and then Nkind (Formal_Type_Definition (Decl)) =
16063 N_Formal_Derived_Type_Definition
16064 and then Synchronized_Present (Formal_Type_Definition (Decl))
16065 and then No (Extension)
16067 -- Avoid emitting a duplicate error message
16069 and then not Error_Posted (Indic)
16072 ("synchronized derived type must have extension", N);
16077 if Null_Exclusion_Present (Def)
16078 and then not Is_Access_Type (Parent_Type)
16080 Error_Msg_N ("null exclusion can only apply to an access type", N);
16083 -- Avoid deriving parent primitives of underlying record views
16085 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16086 Derive_Subps => not Is_Underlying_Record_View (T));
16088 -- AI-419: The parent type of an explicitly limited derived type must
16089 -- be a limited type or a limited interface.
16091 if Limited_Present (Def) then
16092 Set_Is_Limited_Record (T);
16094 if Is_Interface (T) then
16095 Set_Is_Limited_Interface (T);
16098 if not Is_Limited_Type (Parent_Type)
16100 (not Is_Interface (Parent_Type)
16101 or else not Is_Limited_Interface (Parent_Type))
16103 -- AI05-0096: a derivation in the private part of an instance is
16104 -- legal if the generic formal is untagged limited, and the actual
16107 if Is_Generic_Actual_Type (Parent_Type)
16108 and then In_Private_Part (Current_Scope)
16111 (Generic_Parent_Type (Parent (Parent_Type)))
16117 ("parent type& of limited type must be limited",
16123 -- In SPARK, there are no derived type definitions other than type
16124 -- extensions of tagged record types.
16126 if No (Extension) then
16127 Check_SPARK_05_Restriction
16128 ("derived type is not allowed", Original_Node (N));
16130 end Derived_Type_Declaration;
16132 ------------------------
16133 -- Diagnose_Interface --
16134 ------------------------
16136 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
16138 if not Is_Interface (E) and then E /= Any_Type then
16139 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
16141 end Diagnose_Interface;
16143 ----------------------------------
16144 -- Enumeration_Type_Declaration --
16145 ----------------------------------
16147 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16154 -- Create identifier node representing lower bound
16156 B_Node := New_Node (N_Identifier, Sloc (Def));
16157 L := First (Literals (Def));
16158 Set_Chars (B_Node, Chars (L));
16159 Set_Entity (B_Node, L);
16160 Set_Etype (B_Node, T);
16161 Set_Is_Static_Expression (B_Node, True);
16163 R_Node := New_Node (N_Range, Sloc (Def));
16164 Set_Low_Bound (R_Node, B_Node);
16166 Set_Ekind (T, E_Enumeration_Type);
16167 Set_First_Literal (T, L);
16169 Set_Is_Constrained (T);
16173 -- Loop through literals of enumeration type setting pos and rep values
16174 -- except that if the Ekind is already set, then it means the literal
16175 -- was already constructed (case of a derived type declaration and we
16176 -- should not disturb the Pos and Rep values.
16178 while Present (L) loop
16179 if Ekind (L) /= E_Enumeration_Literal then
16180 Set_Ekind (L, E_Enumeration_Literal);
16181 Set_Enumeration_Pos (L, Ev);
16182 Set_Enumeration_Rep (L, Ev);
16183 Set_Is_Known_Valid (L, True);
16187 New_Overloaded_Entity (L);
16188 Generate_Definition (L);
16189 Set_Convention (L, Convention_Intrinsic);
16191 -- Case of character literal
16193 if Nkind (L) = N_Defining_Character_Literal then
16194 Set_Is_Character_Type (T, True);
16196 -- Check violation of No_Wide_Characters
16198 if Restriction_Check_Required (No_Wide_Characters) then
16199 Get_Name_String (Chars (L));
16201 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
16202 Check_Restriction (No_Wide_Characters, L);
16211 -- Now create a node representing upper bound
16213 B_Node := New_Node (N_Identifier, Sloc (Def));
16214 Set_Chars (B_Node, Chars (Last (Literals (Def))));
16215 Set_Entity (B_Node, Last (Literals (Def)));
16216 Set_Etype (B_Node, T);
16217 Set_Is_Static_Expression (B_Node, True);
16219 Set_High_Bound (R_Node, B_Node);
16221 -- Initialize various fields of the type. Some of this information
16222 -- may be overwritten later through rep.clauses.
16224 Set_Scalar_Range (T, R_Node);
16225 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
16226 Set_Enum_Esize (T);
16227 Set_Enum_Pos_To_Rep (T, Empty);
16229 -- Set Discard_Names if configuration pragma set, or if there is
16230 -- a parameterless pragma in the current declarative region
16232 if Global_Discard_Names or else Discard_Names (Scope (T)) then
16233 Set_Discard_Names (T);
16236 -- Process end label if there is one
16238 if Present (Def) then
16239 Process_End_Label (Def, 'e', T);
16241 end Enumeration_Type_Declaration;
16243 ---------------------------------
16244 -- Expand_To_Stored_Constraint --
16245 ---------------------------------
16247 function Expand_To_Stored_Constraint
16249 Constraint : Elist_Id) return Elist_Id
16251 Explicitly_Discriminated_Type : Entity_Id;
16252 Expansion : Elist_Id;
16253 Discriminant : Entity_Id;
16255 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
16256 -- Find the nearest type that actually specifies discriminants
16258 ---------------------------------
16259 -- Type_With_Explicit_Discrims --
16260 ---------------------------------
16262 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
16263 Typ : constant E := Base_Type (Id);
16266 if Ekind (Typ) in Incomplete_Or_Private_Kind then
16267 if Present (Full_View (Typ)) then
16268 return Type_With_Explicit_Discrims (Full_View (Typ));
16272 if Has_Discriminants (Typ) then
16277 if Etype (Typ) = Typ then
16279 elsif Has_Discriminants (Typ) then
16282 return Type_With_Explicit_Discrims (Etype (Typ));
16285 end Type_With_Explicit_Discrims;
16287 -- Start of processing for Expand_To_Stored_Constraint
16290 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
16294 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
16296 if No (Explicitly_Discriminated_Type) then
16300 Expansion := New_Elmt_List;
16303 First_Stored_Discriminant (Explicitly_Discriminated_Type);
16304 while Present (Discriminant) loop
16306 (Get_Discriminant_Value
16307 (Discriminant, Explicitly_Discriminated_Type, Constraint),
16309 Next_Stored_Discriminant (Discriminant);
16313 end Expand_To_Stored_Constraint;
16315 ---------------------------
16316 -- Find_Hidden_Interface --
16317 ---------------------------
16319 function Find_Hidden_Interface
16321 Dest : Elist_Id) return Entity_Id
16324 Iface_Elmt : Elmt_Id;
16327 if Present (Src) and then Present (Dest) then
16328 Iface_Elmt := First_Elmt (Src);
16329 while Present (Iface_Elmt) loop
16330 Iface := Node (Iface_Elmt);
16332 if Is_Interface (Iface)
16333 and then not Contain_Interface (Iface, Dest)
16338 Next_Elmt (Iface_Elmt);
16343 end Find_Hidden_Interface;
16345 --------------------
16346 -- Find_Type_Name --
16347 --------------------
16349 function Find_Type_Name (N : Node_Id) return Entity_Id is
16350 Id : constant Entity_Id := Defining_Identifier (N);
16352 New_Id : Entity_Id;
16353 Prev_Par : Node_Id;
16355 procedure Check_Duplicate_Aspects;
16356 -- Check that aspects specified in a completion have not been specified
16357 -- already in the partial view. Type_Invariant and others can be
16358 -- specified on either view but never on both.
16360 procedure Tag_Mismatch;
16361 -- Diagnose a tagged partial view whose full view is untagged.
16362 -- We post the message on the full view, with a reference to
16363 -- the previous partial view. The partial view can be private
16364 -- or incomplete, and these are handled in a different manner,
16365 -- so we determine the position of the error message from the
16366 -- respective slocs of both.
16368 -----------------------------
16369 -- Check_Duplicate_Aspects --
16370 -----------------------------
16371 procedure Check_Duplicate_Aspects is
16372 Prev_Aspects : constant List_Id := Aspect_Specifications (Prev_Par);
16373 Full_Aspects : constant List_Id := Aspect_Specifications (N);
16374 F_Spec, P_Spec : Node_Id;
16377 if Present (Prev_Aspects) and then Present (Full_Aspects) then
16378 F_Spec := First (Full_Aspects);
16379 while Present (F_Spec) loop
16380 P_Spec := First (Prev_Aspects);
16381 while Present (P_Spec) loop
16382 if Chars (Identifier (P_Spec)) = Chars (Identifier (F_Spec))
16385 ("aspect already specified in private declaration",
16397 end Check_Duplicate_Aspects;
16403 procedure Tag_Mismatch is
16405 if Sloc (Prev) < Sloc (Id) then
16406 if Ada_Version >= Ada_2012
16407 and then Nkind (N) = N_Private_Type_Declaration
16410 ("declaration of private } must be a tagged type ", Id, Prev);
16413 ("full declaration of } must be a tagged type ", Id, Prev);
16417 if Ada_Version >= Ada_2012
16418 and then Nkind (N) = N_Private_Type_Declaration
16421 ("declaration of private } must be a tagged type ", Prev, Id);
16424 ("full declaration of } must be a tagged type ", Prev, Id);
16429 -- Start of processing for Find_Type_Name
16432 -- Find incomplete declaration, if one was given
16434 Prev := Current_Entity_In_Scope (Id);
16436 -- New type declaration
16442 -- Previous declaration exists
16445 Prev_Par := Parent (Prev);
16447 -- Error if not incomplete/private case except if previous
16448 -- declaration is implicit, etc. Enter_Name will emit error if
16451 if not Is_Incomplete_Or_Private_Type (Prev) then
16455 -- Check invalid completion of private or incomplete type
16457 elsif not Nkind_In (N, N_Full_Type_Declaration,
16458 N_Task_Type_Declaration,
16459 N_Protected_Type_Declaration)
16461 (Ada_Version < Ada_2012
16462 or else not Is_Incomplete_Type (Prev)
16463 or else not Nkind_In (N, N_Private_Type_Declaration,
16464 N_Private_Extension_Declaration))
16466 -- Completion must be a full type declarations (RM 7.3(4))
16468 Error_Msg_Sloc := Sloc (Prev);
16469 Error_Msg_NE ("invalid completion of }", Id, Prev);
16471 -- Set scope of Id to avoid cascaded errors. Entity is never
16472 -- examined again, except when saving globals in generics.
16474 Set_Scope (Id, Current_Scope);
16477 -- If this is a repeated incomplete declaration, no further
16478 -- checks are possible.
16480 if Nkind (N) = N_Incomplete_Type_Declaration then
16484 -- Case of full declaration of incomplete type
16486 elsif Ekind (Prev) = E_Incomplete_Type
16487 and then (Ada_Version < Ada_2012
16488 or else No (Full_View (Prev))
16489 or else not Is_Private_Type (Full_View (Prev)))
16491 -- Indicate that the incomplete declaration has a matching full
16492 -- declaration. The defining occurrence of the incomplete
16493 -- declaration remains the visible one, and the procedure
16494 -- Get_Full_View dereferences it whenever the type is used.
16496 if Present (Full_View (Prev)) then
16497 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16500 Set_Full_View (Prev, Id);
16501 Append_Entity (Id, Current_Scope);
16502 Set_Is_Public (Id, Is_Public (Prev));
16503 Set_Is_Internal (Id);
16506 -- If the incomplete view is tagged, a class_wide type has been
16507 -- created already. Use it for the private type as well, in order
16508 -- to prevent multiple incompatible class-wide types that may be
16509 -- created for self-referential anonymous access components.
16511 if Is_Tagged_Type (Prev)
16512 and then Present (Class_Wide_Type (Prev))
16514 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
16515 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
16517 -- The type of the classwide type is the current Id. Previously
16518 -- this was not done for private declarations because of order-
16519 -- of elaboration issues in the back-end, but gigi now handles
16522 Set_Etype (Class_Wide_Type (Id), Id);
16525 -- Case of full declaration of private type
16528 -- If the private type was a completion of an incomplete type then
16529 -- update Prev to reference the private type
16531 if Ada_Version >= Ada_2012
16532 and then Ekind (Prev) = E_Incomplete_Type
16533 and then Present (Full_View (Prev))
16534 and then Is_Private_Type (Full_View (Prev))
16536 Prev := Full_View (Prev);
16537 Prev_Par := Parent (Prev);
16540 if Nkind (N) = N_Full_Type_Declaration
16542 (Type_Definition (N), N_Record_Definition,
16543 N_Derived_Type_Definition)
16544 and then Interface_Present (Type_Definition (N))
16547 ("completion of private type cannot be an interface", N);
16550 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
16551 if Etype (Prev) /= Prev then
16553 -- Prev is a private subtype or a derived type, and needs
16556 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
16559 elsif Ekind (Prev) = E_Private_Type
16560 and then Nkind_In (N, N_Task_Type_Declaration,
16561 N_Protected_Type_Declaration)
16564 ("completion of nonlimited type cannot be limited", N);
16566 elsif Ekind (Prev) = E_Record_Type_With_Private
16567 and then Nkind_In (N, N_Task_Type_Declaration,
16568 N_Protected_Type_Declaration)
16570 if not Is_Limited_Record (Prev) then
16572 ("completion of nonlimited type cannot be limited", N);
16574 elsif No (Interface_List (N)) then
16576 ("completion of tagged private type must be tagged",
16581 -- Ada 2005 (AI-251): Private extension declaration of a task
16582 -- type or a protected type. This case arises when covering
16583 -- interface types.
16585 elsif Nkind_In (N, N_Task_Type_Declaration,
16586 N_Protected_Type_Declaration)
16590 elsif Nkind (N) /= N_Full_Type_Declaration
16591 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
16594 ("full view of private extension must be an extension", N);
16596 elsif not (Abstract_Present (Parent (Prev)))
16597 and then Abstract_Present (Type_Definition (N))
16600 ("full view of non-abstract extension cannot be abstract", N);
16603 if not In_Private_Part (Current_Scope) then
16605 ("declaration of full view must appear in private part", N);
16608 if Ada_Version >= Ada_2012 then
16609 Check_Duplicate_Aspects;
16612 Copy_And_Swap (Prev, Id);
16613 Set_Has_Private_Declaration (Prev);
16614 Set_Has_Private_Declaration (Id);
16616 -- AI12-0133: Indicate whether we have a partial view with
16617 -- unknown discriminants, in which case initialization of objects
16618 -- of the type do not receive an invariant check.
16620 Set_Partial_View_Has_Unknown_Discr
16621 (Prev, Has_Unknown_Discriminants (Id));
16623 -- Preserve aspect and iterator flags that may have been set on
16624 -- the partial view.
16626 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
16627 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
16629 -- If no error, propagate freeze_node from private to full view.
16630 -- It may have been generated for an early operational item.
16632 if Present (Freeze_Node (Id))
16633 and then Serious_Errors_Detected = 0
16634 and then No (Full_View (Id))
16636 Set_Freeze_Node (Prev, Freeze_Node (Id));
16637 Set_Freeze_Node (Id, Empty);
16638 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
16641 Set_Full_View (Id, Prev);
16645 -- Verify that full declaration conforms to partial one
16647 if Is_Incomplete_Or_Private_Type (Prev)
16648 and then Present (Discriminant_Specifications (Prev_Par))
16650 if Present (Discriminant_Specifications (N)) then
16651 if Ekind (Prev) = E_Incomplete_Type then
16652 Check_Discriminant_Conformance (N, Prev, Prev);
16654 Check_Discriminant_Conformance (N, Prev, Id);
16659 ("missing discriminants in full type declaration", N);
16661 -- To avoid cascaded errors on subsequent use, share the
16662 -- discriminants of the partial view.
16664 Set_Discriminant_Specifications (N,
16665 Discriminant_Specifications (Prev_Par));
16669 -- A prior untagged partial view can have an associated class-wide
16670 -- type due to use of the class attribute, and in this case the full
16671 -- type must also be tagged. This Ada 95 usage is deprecated in favor
16672 -- of incomplete tagged declarations, but we check for it.
16675 and then (Is_Tagged_Type (Prev)
16676 or else Present (Class_Wide_Type (Prev)))
16678 -- Ada 2012 (AI05-0162): A private type may be the completion of
16679 -- an incomplete type.
16681 if Ada_Version >= Ada_2012
16682 and then Is_Incomplete_Type (Prev)
16683 and then Nkind_In (N, N_Private_Type_Declaration,
16684 N_Private_Extension_Declaration)
16686 -- No need to check private extensions since they are tagged
16688 if Nkind (N) = N_Private_Type_Declaration
16689 and then not Tagged_Present (N)
16694 -- The full declaration is either a tagged type (including
16695 -- a synchronized type that implements interfaces) or a
16696 -- type extension, otherwise this is an error.
16698 elsif Nkind_In (N, N_Task_Type_Declaration,
16699 N_Protected_Type_Declaration)
16701 if No (Interface_List (N)) and then not Error_Posted (N) then
16705 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
16707 -- Indicate that the previous declaration (tagged incomplete
16708 -- or private declaration) requires the same on the full one.
16710 if not Tagged_Present (Type_Definition (N)) then
16712 Set_Is_Tagged_Type (Id);
16715 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
16716 if No (Record_Extension_Part (Type_Definition (N))) then
16718 ("full declaration of } must be a record extension",
16721 -- Set some attributes to produce a usable full view
16723 Set_Is_Tagged_Type (Id);
16732 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
16733 and then Present (Premature_Use (Parent (Prev)))
16735 Error_Msg_Sloc := Sloc (N);
16737 ("\full declaration #", Premature_Use (Parent (Prev)));
16742 end Find_Type_Name;
16744 -------------------------
16745 -- Find_Type_Of_Object --
16746 -------------------------
16748 function Find_Type_Of_Object
16749 (Obj_Def : Node_Id;
16750 Related_Nod : Node_Id) return Entity_Id
16752 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
16753 P : Node_Id := Parent (Obj_Def);
16758 -- If the parent is a component_definition node we climb to the
16759 -- component_declaration node
16761 if Nkind (P) = N_Component_Definition then
16765 -- Case of an anonymous array subtype
16767 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
16768 N_Unconstrained_Array_Definition)
16771 Array_Type_Declaration (T, Obj_Def);
16773 -- Create an explicit subtype whenever possible
16775 elsif Nkind (P) /= N_Component_Declaration
16776 and then Def_Kind = N_Subtype_Indication
16778 -- Base name of subtype on object name, which will be unique in
16779 -- the current scope.
16781 -- If this is a duplicate declaration, return base type, to avoid
16782 -- generating duplicate anonymous types.
16784 if Error_Posted (P) then
16785 Analyze (Subtype_Mark (Obj_Def));
16786 return Entity (Subtype_Mark (Obj_Def));
16791 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
16793 T := Make_Defining_Identifier (Sloc (P), Nam);
16795 Insert_Action (Obj_Def,
16796 Make_Subtype_Declaration (Sloc (P),
16797 Defining_Identifier => T,
16798 Subtype_Indication => Relocate_Node (Obj_Def)));
16800 -- This subtype may need freezing, and this will not be done
16801 -- automatically if the object declaration is not in declarative
16802 -- part. Since this is an object declaration, the type cannot always
16803 -- be frozen here. Deferred constants do not freeze their type
16804 -- (which often enough will be private).
16806 if Nkind (P) = N_Object_Declaration
16807 and then Constant_Present (P)
16808 and then No (Expression (P))
16812 -- Here we freeze the base type of object type to catch premature use
16813 -- of discriminated private type without a full view.
16816 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
16819 -- Ada 2005 AI-406: the object definition in an object declaration
16820 -- can be an access definition.
16822 elsif Def_Kind = N_Access_Definition then
16823 T := Access_Definition (Related_Nod, Obj_Def);
16825 Set_Is_Local_Anonymous_Access
16827 V => (Ada_Version < Ada_2012)
16828 or else (Nkind (P) /= N_Object_Declaration)
16829 or else Is_Library_Level_Entity (Defining_Identifier (P)));
16831 -- Otherwise, the object definition is just a subtype_mark
16834 T := Process_Subtype (Obj_Def, Related_Nod);
16836 -- If expansion is disabled an object definition that is an aggregate
16837 -- will not get expanded and may lead to scoping problems in the back
16838 -- end, if the object is referenced in an inner scope. In that case
16839 -- create an itype reference for the object definition now. This
16840 -- may be redundant in some cases, but harmless.
16843 and then Nkind (Related_Nod) = N_Object_Declaration
16846 Build_Itype_Reference (T, Related_Nod);
16851 end Find_Type_Of_Object;
16853 --------------------------------
16854 -- Find_Type_Of_Subtype_Indic --
16855 --------------------------------
16857 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
16861 -- Case of subtype mark with a constraint
16863 if Nkind (S) = N_Subtype_Indication then
16864 Find_Type (Subtype_Mark (S));
16865 Typ := Entity (Subtype_Mark (S));
16868 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
16871 ("incorrect constraint for this kind of type", Constraint (S));
16872 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
16875 -- Otherwise we have a subtype mark without a constraint
16877 elsif Error_Posted (S) then
16878 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
16886 -- Check No_Wide_Characters restriction
16888 Check_Wide_Character_Restriction (Typ, S);
16891 end Find_Type_Of_Subtype_Indic;
16893 -------------------------------------
16894 -- Floating_Point_Type_Declaration --
16895 -------------------------------------
16897 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
16898 Digs : constant Node_Id := Digits_Expression (Def);
16899 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
16901 Base_Typ : Entity_Id;
16902 Implicit_Base : Entity_Id;
16905 function Can_Derive_From (E : Entity_Id) return Boolean;
16906 -- Find if given digits value, and possibly a specified range, allows
16907 -- derivation from specified type
16909 function Find_Base_Type return Entity_Id;
16910 -- Find a predefined base type that Def can derive from, or generate
16911 -- an error and substitute Long_Long_Float if none exists.
16913 ---------------------
16914 -- Can_Derive_From --
16915 ---------------------
16917 function Can_Derive_From (E : Entity_Id) return Boolean is
16918 Spec : constant Entity_Id := Real_Range_Specification (Def);
16921 -- Check specified "digits" constraint
16923 if Digs_Val > Digits_Value (E) then
16927 -- Check for matching range, if specified
16929 if Present (Spec) then
16930 if Expr_Value_R (Type_Low_Bound (E)) >
16931 Expr_Value_R (Low_Bound (Spec))
16936 if Expr_Value_R (Type_High_Bound (E)) <
16937 Expr_Value_R (High_Bound (Spec))
16944 end Can_Derive_From;
16946 --------------------
16947 -- Find_Base_Type --
16948 --------------------
16950 function Find_Base_Type return Entity_Id is
16951 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
16954 -- Iterate over the predefined types in order, returning the first
16955 -- one that Def can derive from.
16957 while Present (Choice) loop
16958 if Can_Derive_From (Node (Choice)) then
16959 return Node (Choice);
16962 Next_Elmt (Choice);
16965 -- If we can't derive from any existing type, use Long_Long_Float
16966 -- and give appropriate message explaining the problem.
16968 if Digs_Val > Max_Digs_Val then
16969 -- It might be the case that there is a type with the requested
16970 -- range, just not the combination of digits and range.
16973 ("no predefined type has requested range and precision",
16974 Real_Range_Specification (Def));
16978 ("range too large for any predefined type",
16979 Real_Range_Specification (Def));
16982 return Standard_Long_Long_Float;
16983 end Find_Base_Type;
16985 -- Start of processing for Floating_Point_Type_Declaration
16988 Check_Restriction (No_Floating_Point, Def);
16990 -- Create an implicit base type
16993 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
16995 -- Analyze and verify digits value
16997 Analyze_And_Resolve (Digs, Any_Integer);
16998 Check_Digits_Expression (Digs);
16999 Digs_Val := Expr_Value (Digs);
17001 -- Process possible range spec and find correct type to derive from
17003 Process_Real_Range_Specification (Def);
17005 -- Check that requested number of digits is not too high.
17007 if Digs_Val > Max_Digs_Val then
17009 -- The check for Max_Base_Digits may be somewhat expensive, as it
17010 -- requires reading System, so only do it when necessary.
17013 Max_Base_Digits : constant Uint :=
17016 (Parent (RTE (RE_Max_Base_Digits))));
17019 if Digs_Val > Max_Base_Digits then
17020 Error_Msg_Uint_1 := Max_Base_Digits;
17021 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17023 elsif No (Real_Range_Specification (Def)) then
17024 Error_Msg_Uint_1 := Max_Digs_Val;
17025 Error_Msg_N ("types with more than ^ digits need range spec "
17026 & "(RM 3.5.7(6))", Digs);
17031 -- Find a suitable type to derive from or complain and use a substitute
17033 Base_Typ := Find_Base_Type;
17035 -- If there are bounds given in the declaration use them as the bounds
17036 -- of the type, otherwise use the bounds of the predefined base type
17037 -- that was chosen based on the Digits value.
17039 if Present (Real_Range_Specification (Def)) then
17040 Set_Scalar_Range (T, Real_Range_Specification (Def));
17041 Set_Is_Constrained (T);
17043 -- The bounds of this range must be converted to machine numbers
17044 -- in accordance with RM 4.9(38).
17046 Bound := Type_Low_Bound (T);
17048 if Nkind (Bound) = N_Real_Literal then
17050 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17051 Set_Is_Machine_Number (Bound);
17054 Bound := Type_High_Bound (T);
17056 if Nkind (Bound) = N_Real_Literal then
17058 (Bound, Machine (Base_Typ, Realval (Bound), Round, Bound));
17059 Set_Is_Machine_Number (Bound);
17063 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17066 -- Complete definition of implicit base and declared first subtype. The
17067 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17068 -- are not clobbered when the floating point type acts as a full view of
17071 Set_Etype (Implicit_Base, Base_Typ);
17072 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17073 Set_Size_Info (Implicit_Base, Base_Typ);
17074 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17075 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17076 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17077 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17079 Set_Ekind (T, E_Floating_Point_Subtype);
17080 Set_Etype (T, Implicit_Base);
17081 Set_Size_Info (T, Implicit_Base);
17082 Set_RM_Size (T, RM_Size (Implicit_Base));
17083 Inherit_Rep_Item_Chain (T, Implicit_Base);
17084 Set_Digits_Value (T, Digs_Val);
17085 end Floating_Point_Type_Declaration;
17087 ----------------------------
17088 -- Get_Discriminant_Value --
17089 ----------------------------
17091 -- This is the situation:
17093 -- There is a non-derived type
17095 -- type T0 (Dx, Dy, Dz...)
17097 -- There are zero or more levels of derivation, with each derivation
17098 -- either purely inheriting the discriminants, or defining its own.
17100 -- type Ti is new Ti-1
17102 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
17104 -- subtype Ti is ...
17106 -- The subtype issue is avoided by the use of Original_Record_Component,
17107 -- and the fact that derived subtypes also derive the constraints.
17109 -- This chain leads back from
17111 -- Typ_For_Constraint
17113 -- Typ_For_Constraint has discriminants, and the value for each
17114 -- discriminant is given by its corresponding Elmt of Constraints.
17116 -- Discriminant is some discriminant in this hierarchy
17118 -- We need to return its value
17120 -- We do this by recursively searching each level, and looking for
17121 -- Discriminant. Once we get to the bottom, we start backing up
17122 -- returning the value for it which may in turn be a discriminant
17123 -- further up, so on the backup we continue the substitution.
17125 function Get_Discriminant_Value
17126 (Discriminant : Entity_Id;
17127 Typ_For_Constraint : Entity_Id;
17128 Constraint : Elist_Id) return Node_Id
17130 function Root_Corresponding_Discriminant
17131 (Discr : Entity_Id) return Entity_Id;
17132 -- Given a discriminant, traverse the chain of inherited discriminants
17133 -- and return the topmost discriminant.
17135 function Search_Derivation_Levels
17137 Discrim_Values : Elist_Id;
17138 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
17139 -- This is the routine that performs the recursive search of levels
17140 -- as described above.
17142 -------------------------------------
17143 -- Root_Corresponding_Discriminant --
17144 -------------------------------------
17146 function Root_Corresponding_Discriminant
17147 (Discr : Entity_Id) return Entity_Id
17153 while Present (Corresponding_Discriminant (D)) loop
17154 D := Corresponding_Discriminant (D);
17158 end Root_Corresponding_Discriminant;
17160 ------------------------------
17161 -- Search_Derivation_Levels --
17162 ------------------------------
17164 function Search_Derivation_Levels
17166 Discrim_Values : Elist_Id;
17167 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
17171 Result : Node_Or_Entity_Id;
17172 Result_Entity : Node_Id;
17175 -- If inappropriate type, return Error, this happens only in
17176 -- cascaded error situations, and we want to avoid a blow up.
17178 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
17182 -- Look deeper if possible. Use Stored_Constraints only for
17183 -- untagged types. For tagged types use the given constraint.
17184 -- This asymmetry needs explanation???
17186 if not Stored_Discrim_Values
17187 and then Present (Stored_Constraint (Ti))
17188 and then not Is_Tagged_Type (Ti)
17191 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
17194 Td : constant Entity_Id := Etype (Ti);
17198 Result := Discriminant;
17201 if Present (Stored_Constraint (Ti)) then
17203 Search_Derivation_Levels
17204 (Td, Stored_Constraint (Ti), True);
17207 Search_Derivation_Levels
17208 (Td, Discrim_Values, Stored_Discrim_Values);
17214 -- Extra underlying places to search, if not found above. For
17215 -- concurrent types, the relevant discriminant appears in the
17216 -- corresponding record. For a type derived from a private type
17217 -- without discriminant, the full view inherits the discriminants
17218 -- of the full view of the parent.
17220 if Result = Discriminant then
17221 if Is_Concurrent_Type (Ti)
17222 and then Present (Corresponding_Record_Type (Ti))
17225 Search_Derivation_Levels (
17226 Corresponding_Record_Type (Ti),
17228 Stored_Discrim_Values);
17230 elsif Is_Private_Type (Ti)
17231 and then not Has_Discriminants (Ti)
17232 and then Present (Full_View (Ti))
17233 and then Etype (Full_View (Ti)) /= Ti
17236 Search_Derivation_Levels (
17239 Stored_Discrim_Values);
17243 -- If Result is not a (reference to a) discriminant, return it,
17244 -- otherwise set Result_Entity to the discriminant.
17246 if Nkind (Result) = N_Defining_Identifier then
17247 pragma Assert (Result = Discriminant);
17248 Result_Entity := Result;
17251 if not Denotes_Discriminant (Result) then
17255 Result_Entity := Entity (Result);
17258 -- See if this level of derivation actually has discriminants because
17259 -- tagged derivations can add them, hence the lower levels need not
17262 if not Has_Discriminants (Ti) then
17266 -- Scan Ti's discriminants for Result_Entity, and return its
17267 -- corresponding value, if any.
17269 Result_Entity := Original_Record_Component (Result_Entity);
17271 Assoc := First_Elmt (Discrim_Values);
17273 if Stored_Discrim_Values then
17274 Disc := First_Stored_Discriminant (Ti);
17276 Disc := First_Discriminant (Ti);
17279 while Present (Disc) loop
17280 pragma Assert (Present (Assoc));
17282 if Original_Record_Component (Disc) = Result_Entity then
17283 return Node (Assoc);
17288 if Stored_Discrim_Values then
17289 Next_Stored_Discriminant (Disc);
17291 Next_Discriminant (Disc);
17295 -- Could not find it
17298 end Search_Derivation_Levels;
17302 Result : Node_Or_Entity_Id;
17304 -- Start of processing for Get_Discriminant_Value
17307 -- ??? This routine is a gigantic mess and will be deleted. For the
17308 -- time being just test for the trivial case before calling recurse.
17310 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
17316 D := First_Discriminant (Typ_For_Constraint);
17317 E := First_Elmt (Constraint);
17318 while Present (D) loop
17319 if Chars (D) = Chars (Discriminant) then
17323 Next_Discriminant (D);
17329 Result := Search_Derivation_Levels
17330 (Typ_For_Constraint, Constraint, False);
17332 -- ??? hack to disappear when this routine is gone
17334 if Nkind (Result) = N_Defining_Identifier then
17340 D := First_Discriminant (Typ_For_Constraint);
17341 E := First_Elmt (Constraint);
17342 while Present (D) loop
17343 if Root_Corresponding_Discriminant (D) = Discriminant then
17347 Next_Discriminant (D);
17353 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
17355 end Get_Discriminant_Value;
17357 --------------------------
17358 -- Has_Range_Constraint --
17359 --------------------------
17361 function Has_Range_Constraint (N : Node_Id) return Boolean is
17362 C : constant Node_Id := Constraint (N);
17365 if Nkind (C) = N_Range_Constraint then
17368 elsif Nkind (C) = N_Digits_Constraint then
17370 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
17371 or else Present (Range_Constraint (C));
17373 elsif Nkind (C) = N_Delta_Constraint then
17374 return Present (Range_Constraint (C));
17379 end Has_Range_Constraint;
17381 ------------------------
17382 -- Inherit_Components --
17383 ------------------------
17385 function Inherit_Components
17387 Parent_Base : Entity_Id;
17388 Derived_Base : Entity_Id;
17389 Is_Tagged : Boolean;
17390 Inherit_Discr : Boolean;
17391 Discs : Elist_Id) return Elist_Id
17393 Assoc_List : constant Elist_Id := New_Elmt_List;
17395 procedure Inherit_Component
17396 (Old_C : Entity_Id;
17397 Plain_Discrim : Boolean := False;
17398 Stored_Discrim : Boolean := False);
17399 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
17400 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
17401 -- True, Old_C is a stored discriminant. If they are both false then
17402 -- Old_C is a regular component.
17404 -----------------------
17405 -- Inherit_Component --
17406 -----------------------
17408 procedure Inherit_Component
17409 (Old_C : Entity_Id;
17410 Plain_Discrim : Boolean := False;
17411 Stored_Discrim : Boolean := False)
17413 procedure Set_Anonymous_Type (Id : Entity_Id);
17414 -- Id denotes the entity of an access discriminant or anonymous
17415 -- access component. Set the type of Id to either the same type of
17416 -- Old_C or create a new one depending on whether the parent and
17417 -- the child types are in the same scope.
17419 ------------------------
17420 -- Set_Anonymous_Type --
17421 ------------------------
17423 procedure Set_Anonymous_Type (Id : Entity_Id) is
17424 Old_Typ : constant Entity_Id := Etype (Old_C);
17427 if Scope (Parent_Base) = Scope (Derived_Base) then
17428 Set_Etype (Id, Old_Typ);
17430 -- The parent and the derived type are in two different scopes.
17431 -- Reuse the type of the original discriminant / component by
17432 -- copying it in order to preserve all attributes.
17436 Typ : constant Entity_Id := New_Copy (Old_Typ);
17439 Set_Etype (Id, Typ);
17441 -- Since we do not generate component declarations for
17442 -- inherited components, associate the itype with the
17445 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
17446 Set_Scope (Typ, Derived_Base);
17449 end Set_Anonymous_Type;
17451 -- Local variables and constants
17453 New_C : constant Entity_Id := New_Copy (Old_C);
17455 Corr_Discrim : Entity_Id;
17456 Discrim : Entity_Id;
17458 -- Start of processing for Inherit_Component
17461 pragma Assert (not Is_Tagged or not Stored_Discrim);
17463 Set_Parent (New_C, Parent (Old_C));
17465 -- Regular discriminants and components must be inserted in the scope
17466 -- of the Derived_Base. Do it here.
17468 if not Stored_Discrim then
17469 Enter_Name (New_C);
17472 -- For tagged types the Original_Record_Component must point to
17473 -- whatever this field was pointing to in the parent type. This has
17474 -- already been achieved by the call to New_Copy above.
17476 if not Is_Tagged then
17477 Set_Original_Record_Component (New_C, New_C);
17480 -- Set the proper type of an access discriminant
17482 if Ekind (New_C) = E_Discriminant
17483 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
17485 Set_Anonymous_Type (New_C);
17488 -- If we have inherited a component then see if its Etype contains
17489 -- references to Parent_Base discriminants. In this case, replace
17490 -- these references with the constraints given in Discs. We do not
17491 -- do this for the partial view of private types because this is
17492 -- not needed (only the components of the full view will be used
17493 -- for code generation) and cause problem. We also avoid this
17494 -- transformation in some error situations.
17496 if Ekind (New_C) = E_Component then
17498 -- Set the proper type of an anonymous access component
17500 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
17501 Set_Anonymous_Type (New_C);
17503 elsif (Is_Private_Type (Derived_Base)
17504 and then not Is_Generic_Type (Derived_Base))
17505 or else (Is_Empty_Elmt_List (Discs)
17506 and then not Expander_Active)
17508 Set_Etype (New_C, Etype (Old_C));
17511 -- The current component introduces a circularity of the
17514 -- limited with Pack_2;
17515 -- package Pack_1 is
17516 -- type T_1 is tagged record
17517 -- Comp : access Pack_2.T_2;
17523 -- package Pack_2 is
17524 -- type T_2 is new Pack_1.T_1 with ...;
17529 Constrain_Component_Type
17530 (Old_C, Derived_Base, N, Parent_Base, Discs));
17534 -- In derived tagged types it is illegal to reference a non
17535 -- discriminant component in the parent type. To catch this, mark
17536 -- these components with an Ekind of E_Void. This will be reset in
17537 -- Record_Type_Definition after processing the record extension of
17538 -- the derived type.
17540 -- If the declaration is a private extension, there is no further
17541 -- record extension to process, and the components retain their
17542 -- current kind, because they are visible at this point.
17544 if Is_Tagged and then Ekind (New_C) = E_Component
17545 and then Nkind (N) /= N_Private_Extension_Declaration
17547 Set_Ekind (New_C, E_Void);
17550 if Plain_Discrim then
17551 Set_Corresponding_Discriminant (New_C, Old_C);
17552 Build_Discriminal (New_C);
17554 -- If we are explicitly inheriting a stored discriminant it will be
17555 -- completely hidden.
17557 elsif Stored_Discrim then
17558 Set_Corresponding_Discriminant (New_C, Empty);
17559 Set_Discriminal (New_C, Empty);
17560 Set_Is_Completely_Hidden (New_C);
17562 -- Set the Original_Record_Component of each discriminant in the
17563 -- derived base to point to the corresponding stored that we just
17566 Discrim := First_Discriminant (Derived_Base);
17567 while Present (Discrim) loop
17568 Corr_Discrim := Corresponding_Discriminant (Discrim);
17570 -- Corr_Discrim could be missing in an error situation
17572 if Present (Corr_Discrim)
17573 and then Original_Record_Component (Corr_Discrim) = Old_C
17575 Set_Original_Record_Component (Discrim, New_C);
17578 Next_Discriminant (Discrim);
17581 Append_Entity (New_C, Derived_Base);
17584 if not Is_Tagged then
17585 Append_Elmt (Old_C, Assoc_List);
17586 Append_Elmt (New_C, Assoc_List);
17588 end Inherit_Component;
17590 -- Variables local to Inherit_Component
17592 Loc : constant Source_Ptr := Sloc (N);
17594 Parent_Discrim : Entity_Id;
17595 Stored_Discrim : Entity_Id;
17597 Component : Entity_Id;
17599 -- Start of processing for Inherit_Components
17602 if not Is_Tagged then
17603 Append_Elmt (Parent_Base, Assoc_List);
17604 Append_Elmt (Derived_Base, Assoc_List);
17607 -- Inherit parent discriminants if needed
17609 if Inherit_Discr then
17610 Parent_Discrim := First_Discriminant (Parent_Base);
17611 while Present (Parent_Discrim) loop
17612 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
17613 Next_Discriminant (Parent_Discrim);
17617 -- Create explicit stored discrims for untagged types when necessary
17619 if not Has_Unknown_Discriminants (Derived_Base)
17620 and then Has_Discriminants (Parent_Base)
17621 and then not Is_Tagged
17624 or else First_Discriminant (Parent_Base) /=
17625 First_Stored_Discriminant (Parent_Base))
17627 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
17628 while Present (Stored_Discrim) loop
17629 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
17630 Next_Stored_Discriminant (Stored_Discrim);
17634 -- See if we can apply the second transformation for derived types, as
17635 -- explained in point 6. in the comments above Build_Derived_Record_Type
17636 -- This is achieved by appending Derived_Base discriminants into Discs,
17637 -- which has the side effect of returning a non empty Discs list to the
17638 -- caller of Inherit_Components, which is what we want. This must be
17639 -- done for private derived types if there are explicit stored
17640 -- discriminants, to ensure that we can retrieve the values of the
17641 -- constraints provided in the ancestors.
17644 and then Is_Empty_Elmt_List (Discs)
17645 and then Present (First_Discriminant (Derived_Base))
17647 (not Is_Private_Type (Derived_Base)
17648 or else Is_Completely_Hidden
17649 (First_Stored_Discriminant (Derived_Base))
17650 or else Is_Generic_Type (Derived_Base))
17652 D := First_Discriminant (Derived_Base);
17653 while Present (D) loop
17654 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
17655 Next_Discriminant (D);
17659 -- Finally, inherit non-discriminant components unless they are not
17660 -- visible because defined or inherited from the full view of the
17661 -- parent. Don't inherit the _parent field of the parent type.
17663 Component := First_Entity (Parent_Base);
17664 while Present (Component) loop
17666 -- Ada 2005 (AI-251): Do not inherit components associated with
17667 -- secondary tags of the parent.
17669 if Ekind (Component) = E_Component
17670 and then Present (Related_Type (Component))
17674 elsif Ekind (Component) /= E_Component
17675 or else Chars (Component) = Name_uParent
17679 -- If the derived type is within the parent type's declarative
17680 -- region, then the components can still be inherited even though
17681 -- they aren't visible at this point. This can occur for cases
17682 -- such as within public child units where the components must
17683 -- become visible upon entering the child unit's private part.
17685 elsif not Is_Visible_Component (Component)
17686 and then not In_Open_Scopes (Scope (Parent_Base))
17690 elsif Ekind_In (Derived_Base, E_Private_Type,
17691 E_Limited_Private_Type)
17696 Inherit_Component (Component);
17699 Next_Entity (Component);
17702 -- For tagged derived types, inherited discriminants cannot be used in
17703 -- component declarations of the record extension part. To achieve this
17704 -- we mark the inherited discriminants as not visible.
17706 if Is_Tagged and then Inherit_Discr then
17707 D := First_Discriminant (Derived_Base);
17708 while Present (D) loop
17709 Set_Is_Immediately_Visible (D, False);
17710 Next_Discriminant (D);
17715 end Inherit_Components;
17717 -----------------------------
17718 -- Inherit_Predicate_Flags --
17719 -----------------------------
17721 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
17723 Set_Has_Predicates (Subt, Has_Predicates (Par));
17724 Set_Has_Static_Predicate_Aspect
17725 (Subt, Has_Static_Predicate_Aspect (Par));
17726 Set_Has_Dynamic_Predicate_Aspect
17727 (Subt, Has_Dynamic_Predicate_Aspect (Par));
17728 end Inherit_Predicate_Flags;
17730 ----------------------
17731 -- Is_EVF_Procedure --
17732 ----------------------
17734 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
17735 Formal : Entity_Id;
17738 -- Examine the formals of an Extensions_Visible False procedure looking
17739 -- for a controlling OUT parameter.
17741 if Ekind (Subp) = E_Procedure
17742 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
17744 Formal := First_Formal (Subp);
17745 while Present (Formal) loop
17746 if Ekind (Formal) = E_Out_Parameter
17747 and then Is_Controlling_Formal (Formal)
17752 Next_Formal (Formal);
17757 end Is_EVF_Procedure;
17759 -----------------------
17760 -- Is_Null_Extension --
17761 -----------------------
17763 function Is_Null_Extension (T : Entity_Id) return Boolean is
17764 Type_Decl : constant Node_Id := Parent (Base_Type (T));
17765 Comp_List : Node_Id;
17769 if Nkind (Type_Decl) /= N_Full_Type_Declaration
17770 or else not Is_Tagged_Type (T)
17771 or else Nkind (Type_Definition (Type_Decl)) /=
17772 N_Derived_Type_Definition
17773 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
17779 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
17781 if Present (Discriminant_Specifications (Type_Decl)) then
17784 elsif Present (Comp_List)
17785 and then Is_Non_Empty_List (Component_Items (Comp_List))
17787 Comp := First (Component_Items (Comp_List));
17789 -- Only user-defined components are relevant. The component list
17790 -- may also contain a parent component and internal components
17791 -- corresponding to secondary tags, but these do not determine
17792 -- whether this is a null extension.
17794 while Present (Comp) loop
17795 if Comes_From_Source (Comp) then
17807 end Is_Null_Extension;
17809 ------------------------------
17810 -- Is_Valid_Constraint_Kind --
17811 ------------------------------
17813 function Is_Valid_Constraint_Kind
17814 (T_Kind : Type_Kind;
17815 Constraint_Kind : Node_Kind) return Boolean
17819 when Enumeration_Kind |
17821 return Constraint_Kind = N_Range_Constraint;
17823 when Decimal_Fixed_Point_Kind =>
17824 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17825 N_Range_Constraint);
17827 when Ordinary_Fixed_Point_Kind =>
17828 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
17829 N_Range_Constraint);
17832 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
17833 N_Range_Constraint);
17840 E_Incomplete_Type |
17843 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
17846 return True; -- Error will be detected later
17848 end Is_Valid_Constraint_Kind;
17850 --------------------------
17851 -- Is_Visible_Component --
17852 --------------------------
17854 function Is_Visible_Component
17856 N : Node_Id := Empty) return Boolean
17858 Original_Comp : Entity_Id := Empty;
17859 Original_Scope : Entity_Id;
17860 Type_Scope : Entity_Id;
17862 function Is_Local_Type (Typ : Entity_Id) return Boolean;
17863 -- Check whether parent type of inherited component is declared locally,
17864 -- possibly within a nested package or instance. The current scope is
17865 -- the derived record itself.
17867 -------------------
17868 -- Is_Local_Type --
17869 -------------------
17871 function Is_Local_Type (Typ : Entity_Id) return Boolean is
17875 Scop := Scope (Typ);
17876 while Present (Scop)
17877 and then Scop /= Standard_Standard
17879 if Scop = Scope (Current_Scope) then
17883 Scop := Scope (Scop);
17889 -- Start of processing for Is_Visible_Component
17892 if Ekind_In (C, E_Component, E_Discriminant) then
17893 Original_Comp := Original_Record_Component (C);
17896 if No (Original_Comp) then
17898 -- Premature usage, or previous error
17903 Original_Scope := Scope (Original_Comp);
17904 Type_Scope := Scope (Base_Type (Scope (C)));
17907 -- This test only concerns tagged types
17909 if not Is_Tagged_Type (Original_Scope) then
17912 -- If it is _Parent or _Tag, there is no visibility issue
17914 elsif not Comes_From_Source (Original_Comp) then
17917 -- Discriminants are visible unless the (private) type has unknown
17918 -- discriminants. If the discriminant reference is inserted for a
17919 -- discriminant check on a full view it is also visible.
17921 elsif Ekind (Original_Comp) = E_Discriminant
17923 (not Has_Unknown_Discriminants (Original_Scope)
17924 or else (Present (N)
17925 and then Nkind (N) = N_Selected_Component
17926 and then Nkind (Prefix (N)) = N_Type_Conversion
17927 and then not Comes_From_Source (Prefix (N))))
17931 -- In the body of an instantiation, no need to check for the visibility
17934 elsif In_Instance_Body then
17937 -- If the component has been declared in an ancestor which is currently
17938 -- a private type, then it is not visible. The same applies if the
17939 -- component's containing type is not in an open scope and the original
17940 -- component's enclosing type is a visible full view of a private type
17941 -- (which can occur in cases where an attempt is being made to reference
17942 -- a component in a sibling package that is inherited from a visible
17943 -- component of a type in an ancestor package; the component in the
17944 -- sibling package should not be visible even though the component it
17945 -- inherited from is visible). This does not apply however in the case
17946 -- where the scope of the type is a private child unit, or when the
17947 -- parent comes from a local package in which the ancestor is currently
17948 -- visible. The latter suppression of visibility is needed for cases
17949 -- that are tested in B730006.
17951 elsif Is_Private_Type (Original_Scope)
17953 (not Is_Private_Descendant (Type_Scope)
17954 and then not In_Open_Scopes (Type_Scope)
17955 and then Has_Private_Declaration (Original_Scope))
17957 -- If the type derives from an entity in a formal package, there
17958 -- are no additional visible components.
17960 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
17961 N_Formal_Package_Declaration
17965 -- if we are not in the private part of the current package, there
17966 -- are no additional visible components.
17968 elsif Ekind (Scope (Current_Scope)) = E_Package
17969 and then not In_Private_Part (Scope (Current_Scope))
17974 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
17975 and then In_Open_Scopes (Scope (Original_Scope))
17976 and then Is_Local_Type (Type_Scope);
17979 -- There is another weird way in which a component may be invisible when
17980 -- the private and the full view are not derived from the same ancestor.
17981 -- Here is an example :
17983 -- type A1 is tagged record F1 : integer; end record;
17984 -- type A2 is new A1 with record F2 : integer; end record;
17985 -- type T is new A1 with private;
17987 -- type T is new A2 with null record;
17989 -- In this case, the full view of T inherits F1 and F2 but the private
17990 -- view inherits only F1
17994 Ancestor : Entity_Id := Scope (C);
17998 if Ancestor = Original_Scope then
18000 elsif Ancestor = Etype (Ancestor) then
18004 Ancestor := Etype (Ancestor);
18008 end Is_Visible_Component;
18010 --------------------------
18011 -- Make_Class_Wide_Type --
18012 --------------------------
18014 procedure Make_Class_Wide_Type (T : Entity_Id) is
18015 CW_Type : Entity_Id;
18017 Next_E : Entity_Id;
18020 if Present (Class_Wide_Type (T)) then
18022 -- The class-wide type is a partially decorated entity created for a
18023 -- unanalyzed tagged type referenced through a limited with clause.
18024 -- When the tagged type is analyzed, its class-wide type needs to be
18025 -- redecorated. Note that we reuse the entity created by Decorate_
18026 -- Tagged_Type in order to preserve all links.
18028 if Materialize_Entity (Class_Wide_Type (T)) then
18029 CW_Type := Class_Wide_Type (T);
18030 Set_Materialize_Entity (CW_Type, False);
18032 -- The class wide type can have been defined by the partial view, in
18033 -- which case everything is already done.
18039 -- Default case, we need to create a new class-wide type
18043 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
18046 -- Inherit root type characteristics
18048 CW_Name := Chars (CW_Type);
18049 Next_E := Next_Entity (CW_Type);
18050 Copy_Node (T, CW_Type);
18051 Set_Comes_From_Source (CW_Type, False);
18052 Set_Chars (CW_Type, CW_Name);
18053 Set_Parent (CW_Type, Parent (T));
18054 Set_Next_Entity (CW_Type, Next_E);
18056 -- Ensure we have a new freeze node for the class-wide type. The partial
18057 -- view may have freeze action of its own, requiring a proper freeze
18058 -- node, and the same freeze node cannot be shared between the two
18061 Set_Has_Delayed_Freeze (CW_Type);
18062 Set_Freeze_Node (CW_Type, Empty);
18064 -- Customize the class-wide type: It has no prim. op., it cannot be
18065 -- abstract and its Etype points back to the specific root type.
18067 Set_Ekind (CW_Type, E_Class_Wide_Type);
18068 Set_Is_Tagged_Type (CW_Type, True);
18069 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
18070 Set_Is_Abstract_Type (CW_Type, False);
18071 Set_Is_Constrained (CW_Type, False);
18072 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
18073 Set_Default_SSO (CW_Type);
18075 if Ekind (T) = E_Class_Wide_Subtype then
18076 Set_Etype (CW_Type, Etype (Base_Type (T)));
18078 Set_Etype (CW_Type, T);
18081 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
18083 -- If this is the class_wide type of a constrained subtype, it does
18084 -- not have discriminants.
18086 Set_Has_Discriminants (CW_Type,
18087 Has_Discriminants (T) and then not Is_Constrained (T));
18089 Set_Has_Unknown_Discriminants (CW_Type, True);
18090 Set_Class_Wide_Type (T, CW_Type);
18091 Set_Equivalent_Type (CW_Type, Empty);
18093 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
18095 Set_Class_Wide_Type (CW_Type, CW_Type);
18096 end Make_Class_Wide_Type;
18102 procedure Make_Index
18104 Related_Nod : Node_Id;
18105 Related_Id : Entity_Id := Empty;
18106 Suffix_Index : Nat := 1;
18107 In_Iter_Schm : Boolean := False)
18111 Def_Id : Entity_Id := Empty;
18112 Found : Boolean := False;
18115 -- For a discrete range used in a constrained array definition and
18116 -- defined by a range, an implicit conversion to the predefined type
18117 -- INTEGER is assumed if each bound is either a numeric literal, a named
18118 -- number, or an attribute, and the type of both bounds (prior to the
18119 -- implicit conversion) is the type universal_integer. Otherwise, both
18120 -- bounds must be of the same discrete type, other than universal
18121 -- integer; this type must be determinable independently of the
18122 -- context, but using the fact that the type must be discrete and that
18123 -- both bounds must have the same type.
18125 -- Character literals also have a universal type in the absence of
18126 -- of additional context, and are resolved to Standard_Character.
18128 if Nkind (N) = N_Range then
18130 -- The index is given by a range constraint. The bounds are known
18131 -- to be of a consistent type.
18133 if not Is_Overloaded (N) then
18136 -- For universal bounds, choose the specific predefined type
18138 if T = Universal_Integer then
18139 T := Standard_Integer;
18141 elsif T = Any_Character then
18142 Ambiguous_Character (Low_Bound (N));
18144 T := Standard_Character;
18147 -- The node may be overloaded because some user-defined operators
18148 -- are available, but if a universal interpretation exists it is
18149 -- also the selected one.
18151 elsif Universal_Interpretation (N) = Universal_Integer then
18152 T := Standard_Integer;
18158 Ind : Interp_Index;
18162 Get_First_Interp (N, Ind, It);
18163 while Present (It.Typ) loop
18164 if Is_Discrete_Type (It.Typ) then
18167 and then not Covers (It.Typ, T)
18168 and then not Covers (T, It.Typ)
18170 Error_Msg_N ("ambiguous bounds in discrete range", N);
18178 Get_Next_Interp (Ind, It);
18181 if T = Any_Type then
18182 Error_Msg_N ("discrete type required for range", N);
18183 Set_Etype (N, Any_Type);
18186 elsif T = Universal_Integer then
18187 T := Standard_Integer;
18192 if not Is_Discrete_Type (T) then
18193 Error_Msg_N ("discrete type required for range", N);
18194 Set_Etype (N, Any_Type);
18198 if Nkind (Low_Bound (N)) = N_Attribute_Reference
18199 and then Attribute_Name (Low_Bound (N)) = Name_First
18200 and then Is_Entity_Name (Prefix (Low_Bound (N)))
18201 and then Is_Type (Entity (Prefix (Low_Bound (N))))
18202 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
18204 -- The type of the index will be the type of the prefix, as long
18205 -- as the upper bound is 'Last of the same type.
18207 Def_Id := Entity (Prefix (Low_Bound (N)));
18209 if Nkind (High_Bound (N)) /= N_Attribute_Reference
18210 or else Attribute_Name (High_Bound (N)) /= Name_Last
18211 or else not Is_Entity_Name (Prefix (High_Bound (N)))
18212 or else Entity (Prefix (High_Bound (N))) /= Def_Id
18219 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
18221 elsif Nkind (N) = N_Subtype_Indication then
18223 -- The index is given by a subtype with a range constraint
18225 T := Base_Type (Entity (Subtype_Mark (N)));
18227 if not Is_Discrete_Type (T) then
18228 Error_Msg_N ("discrete type required for range", N);
18229 Set_Etype (N, Any_Type);
18233 R := Range_Expression (Constraint (N));
18236 Process_Range_Expr_In_Decl
18237 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
18239 elsif Nkind (N) = N_Attribute_Reference then
18241 -- Catch beginner's error (use of attribute other than 'Range)
18243 if Attribute_Name (N) /= Name_Range then
18244 Error_Msg_N ("expect attribute ''Range", N);
18245 Set_Etype (N, Any_Type);
18249 -- If the node denotes the range of a type mark, that is also the
18250 -- resulting type, and we do not need to create an Itype for it.
18252 if Is_Entity_Name (Prefix (N))
18253 and then Comes_From_Source (N)
18254 and then Is_Type (Entity (Prefix (N)))
18255 and then Is_Discrete_Type (Entity (Prefix (N)))
18257 Def_Id := Entity (Prefix (N));
18260 Analyze_And_Resolve (N);
18264 -- If none of the above, must be a subtype. We convert this to a
18265 -- range attribute reference because in the case of declared first
18266 -- named subtypes, the types in the range reference can be different
18267 -- from the type of the entity. A range attribute normalizes the
18268 -- reference and obtains the correct types for the bounds.
18270 -- This transformation is in the nature of an expansion, is only
18271 -- done if expansion is active. In particular, it is not done on
18272 -- formal generic types, because we need to retain the name of the
18273 -- original index for instantiation purposes.
18276 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
18277 Error_Msg_N ("invalid subtype mark in discrete range ", N);
18278 Set_Etype (N, Any_Integer);
18282 -- The type mark may be that of an incomplete type. It is only
18283 -- now that we can get the full view, previous analysis does
18284 -- not look specifically for a type mark.
18286 Set_Entity (N, Get_Full_View (Entity (N)));
18287 Set_Etype (N, Entity (N));
18288 Def_Id := Entity (N);
18290 if not Is_Discrete_Type (Def_Id) then
18291 Error_Msg_N ("discrete type required for index", N);
18292 Set_Etype (N, Any_Type);
18297 if Expander_Active then
18299 Make_Attribute_Reference (Sloc (N),
18300 Attribute_Name => Name_Range,
18301 Prefix => Relocate_Node (N)));
18303 -- The original was a subtype mark that does not freeze. This
18304 -- means that the rewritten version must not freeze either.
18306 Set_Must_Not_Freeze (N);
18307 Set_Must_Not_Freeze (Prefix (N));
18308 Analyze_And_Resolve (N);
18312 -- If expander is inactive, type is legal, nothing else to construct
18319 if not Is_Discrete_Type (T) then
18320 Error_Msg_N ("discrete type required for range", N);
18321 Set_Etype (N, Any_Type);
18324 elsif T = Any_Type then
18325 Set_Etype (N, Any_Type);
18329 -- We will now create the appropriate Itype to describe the range, but
18330 -- first a check. If we originally had a subtype, then we just label
18331 -- the range with this subtype. Not only is there no need to construct
18332 -- a new subtype, but it is wrong to do so for two reasons:
18334 -- 1. A legality concern, if we have a subtype, it must not freeze,
18335 -- and the Itype would cause freezing incorrectly
18337 -- 2. An efficiency concern, if we created an Itype, it would not be
18338 -- recognized as the same type for the purposes of eliminating
18339 -- checks in some circumstances.
18341 -- We signal this case by setting the subtype entity in Def_Id
18343 if No (Def_Id) then
18345 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
18346 Set_Etype (Def_Id, Base_Type (T));
18348 if Is_Signed_Integer_Type (T) then
18349 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
18351 elsif Is_Modular_Integer_Type (T) then
18352 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
18355 Set_Ekind (Def_Id, E_Enumeration_Subtype);
18356 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
18357 Set_First_Literal (Def_Id, First_Literal (T));
18360 Set_Size_Info (Def_Id, (T));
18361 Set_RM_Size (Def_Id, RM_Size (T));
18362 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
18364 Set_Scalar_Range (Def_Id, R);
18365 Conditional_Delay (Def_Id, T);
18367 if Nkind (N) = N_Subtype_Indication then
18368 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
18371 -- In the subtype indication case, if the immediate parent of the
18372 -- new subtype is non-static, then the subtype we create is non-
18373 -- static, even if its bounds are static.
18375 if Nkind (N) = N_Subtype_Indication
18376 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
18378 Set_Is_Non_Static_Subtype (Def_Id);
18382 -- Final step is to label the index with this constructed type
18384 Set_Etype (N, Def_Id);
18387 ------------------------------
18388 -- Modular_Type_Declaration --
18389 ------------------------------
18391 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
18392 Mod_Expr : constant Node_Id := Expression (Def);
18395 procedure Set_Modular_Size (Bits : Int);
18396 -- Sets RM_Size to Bits, and Esize to normal word size above this
18398 ----------------------
18399 -- Set_Modular_Size --
18400 ----------------------
18402 procedure Set_Modular_Size (Bits : Int) is
18404 Set_RM_Size (T, UI_From_Int (Bits));
18409 elsif Bits <= 16 then
18410 Init_Esize (T, 16);
18412 elsif Bits <= 32 then
18413 Init_Esize (T, 32);
18416 Init_Esize (T, System_Max_Binary_Modulus_Power);
18419 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
18420 Set_Is_Known_Valid (T);
18422 end Set_Modular_Size;
18424 -- Start of processing for Modular_Type_Declaration
18427 -- If the mod expression is (exactly) 2 * literal, where literal is
18428 -- 64 or less,then almost certainly the * was meant to be **. Warn.
18430 if Warn_On_Suspicious_Modulus_Value
18431 and then Nkind (Mod_Expr) = N_Op_Multiply
18432 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
18433 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
18434 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
18435 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
18438 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
18441 -- Proceed with analysis of mod expression
18443 Analyze_And_Resolve (Mod_Expr, Any_Integer);
18445 Set_Ekind (T, E_Modular_Integer_Type);
18446 Init_Alignment (T);
18447 Set_Is_Constrained (T);
18449 if not Is_OK_Static_Expression (Mod_Expr) then
18450 Flag_Non_Static_Expr
18451 ("non-static expression used for modular type bound!", Mod_Expr);
18452 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18454 M_Val := Expr_Value (Mod_Expr);
18458 Error_Msg_N ("modulus value must be positive", Mod_Expr);
18459 M_Val := 2 ** System_Max_Binary_Modulus_Power;
18462 if M_Val > 2 ** Standard_Long_Integer_Size then
18463 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
18466 Set_Modulus (T, M_Val);
18468 -- Create bounds for the modular type based on the modulus given in
18469 -- the type declaration and then analyze and resolve those bounds.
18471 Set_Scalar_Range (T,
18472 Make_Range (Sloc (Mod_Expr),
18473 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
18474 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
18476 -- Properly analyze the literals for the range. We do this manually
18477 -- because we can't go calling Resolve, since we are resolving these
18478 -- bounds with the type, and this type is certainly not complete yet.
18480 Set_Etype (Low_Bound (Scalar_Range (T)), T);
18481 Set_Etype (High_Bound (Scalar_Range (T)), T);
18482 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
18483 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
18485 -- Loop through powers of two to find number of bits required
18487 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
18491 if M_Val = 2 ** Bits then
18492 Set_Modular_Size (Bits);
18497 elsif M_Val < 2 ** Bits then
18498 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
18499 Set_Non_Binary_Modulus (T);
18501 if Bits > System_Max_Nonbinary_Modulus_Power then
18502 Error_Msg_Uint_1 :=
18503 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
18505 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
18506 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18510 -- In the non-binary case, set size as per RM 13.3(55)
18512 Set_Modular_Size (Bits);
18519 -- If we fall through, then the size exceed System.Max_Binary_Modulus
18520 -- so we just signal an error and set the maximum size.
18522 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
18523 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
18525 Set_Modular_Size (System_Max_Binary_Modulus_Power);
18526 Init_Alignment (T);
18528 end Modular_Type_Declaration;
18530 --------------------------
18531 -- New_Concatenation_Op --
18532 --------------------------
18534 procedure New_Concatenation_Op (Typ : Entity_Id) is
18535 Loc : constant Source_Ptr := Sloc (Typ);
18538 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
18539 -- Create abbreviated declaration for the formal of a predefined
18540 -- Operator 'Op' of type 'Typ'
18542 --------------------
18543 -- Make_Op_Formal --
18544 --------------------
18546 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
18547 Formal : Entity_Id;
18549 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
18550 Set_Etype (Formal, Typ);
18551 Set_Mechanism (Formal, Default_Mechanism);
18553 end Make_Op_Formal;
18555 -- Start of processing for New_Concatenation_Op
18558 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
18560 Set_Ekind (Op, E_Operator);
18561 Set_Scope (Op, Current_Scope);
18562 Set_Etype (Op, Typ);
18563 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
18564 Set_Is_Immediately_Visible (Op);
18565 Set_Is_Intrinsic_Subprogram (Op);
18566 Set_Has_Completion (Op);
18567 Append_Entity (Op, Current_Scope);
18569 Set_Name_Entity_Id (Name_Op_Concat, Op);
18571 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18572 Append_Entity (Make_Op_Formal (Typ, Op), Op);
18573 end New_Concatenation_Op;
18575 -------------------------
18576 -- OK_For_Limited_Init --
18577 -------------------------
18579 -- ???Check all calls of this, and compare the conditions under which it's
18582 function OK_For_Limited_Init
18584 Exp : Node_Id) return Boolean
18587 return Is_CPP_Constructor_Call (Exp)
18588 or else (Ada_Version >= Ada_2005
18589 and then not Debug_Flag_Dot_L
18590 and then OK_For_Limited_Init_In_05 (Typ, Exp));
18591 end OK_For_Limited_Init;
18593 -------------------------------
18594 -- OK_For_Limited_Init_In_05 --
18595 -------------------------------
18597 function OK_For_Limited_Init_In_05
18599 Exp : Node_Id) return Boolean
18602 -- An object of a limited interface type can be initialized with any
18603 -- expression of a nonlimited descendant type.
18605 if Is_Class_Wide_Type (Typ)
18606 and then Is_Limited_Interface (Typ)
18607 and then not Is_Limited_Type (Etype (Exp))
18612 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
18613 -- case of limited aggregates (including extension aggregates), and
18614 -- function calls. The function call may have been given in prefixed
18615 -- notation, in which case the original node is an indexed component.
18616 -- If the function is parameterless, the original node was an explicit
18617 -- dereference. The function may also be parameterless, in which case
18618 -- the source node is just an identifier.
18620 case Nkind (Original_Node (Exp)) is
18621 when N_Aggregate | N_Extension_Aggregate | N_Function_Call | N_Op =>
18624 when N_Identifier =>
18625 return Present (Entity (Original_Node (Exp)))
18626 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
18628 when N_Qualified_Expression =>
18630 OK_For_Limited_Init_In_05
18631 (Typ, Expression (Original_Node (Exp)));
18633 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
18634 -- with a function call, the expander has rewritten the call into an
18635 -- N_Type_Conversion node to force displacement of the pointer to
18636 -- reference the component containing the secondary dispatch table.
18637 -- Otherwise a type conversion is not a legal context.
18638 -- A return statement for a build-in-place function returning a
18639 -- synchronized type also introduces an unchecked conversion.
18641 when N_Type_Conversion |
18642 N_Unchecked_Type_Conversion =>
18643 return not Comes_From_Source (Exp)
18645 OK_For_Limited_Init_In_05
18646 (Typ, Expression (Original_Node (Exp)));
18648 when N_Indexed_Component |
18649 N_Selected_Component |
18650 N_Explicit_Dereference =>
18651 return Nkind (Exp) = N_Function_Call;
18653 -- A use of 'Input is a function call, hence allowed. Normally the
18654 -- attribute will be changed to a call, but the attribute by itself
18655 -- can occur with -gnatc.
18657 when N_Attribute_Reference =>
18658 return Attribute_Name (Original_Node (Exp)) = Name_Input;
18660 -- For a case expression, all dependent expressions must be legal
18662 when N_Case_Expression =>
18667 Alt := First (Alternatives (Original_Node (Exp)));
18668 while Present (Alt) loop
18669 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
18679 -- For an if expression, all dependent expressions must be legal
18681 when N_If_Expression =>
18683 Then_Expr : constant Node_Id :=
18684 Next (First (Expressions (Original_Node (Exp))));
18685 Else_Expr : constant Node_Id := Next (Then_Expr);
18687 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
18689 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
18695 end OK_For_Limited_Init_In_05;
18697 -------------------------------------------
18698 -- Ordinary_Fixed_Point_Type_Declaration --
18699 -------------------------------------------
18701 procedure Ordinary_Fixed_Point_Type_Declaration
18705 Loc : constant Source_Ptr := Sloc (Def);
18706 Delta_Expr : constant Node_Id := Delta_Expression (Def);
18707 RRS : constant Node_Id := Real_Range_Specification (Def);
18708 Implicit_Base : Entity_Id;
18715 Check_Restriction (No_Fixed_Point, Def);
18717 -- Create implicit base type
18720 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
18721 Set_Etype (Implicit_Base, Implicit_Base);
18723 -- Analyze and process delta expression
18725 Analyze_And_Resolve (Delta_Expr, Any_Real);
18727 Check_Delta_Expression (Delta_Expr);
18728 Delta_Val := Expr_Value_R (Delta_Expr);
18730 Set_Delta_Value (Implicit_Base, Delta_Val);
18732 -- Compute default small from given delta, which is the largest power
18733 -- of two that does not exceed the given delta value.
18743 if Delta_Val < Ureal_1 then
18744 while Delta_Val < Tmp loop
18745 Tmp := Tmp / Ureal_2;
18746 Scale := Scale + 1;
18751 Tmp := Tmp * Ureal_2;
18752 exit when Tmp > Delta_Val;
18753 Scale := Scale - 1;
18757 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
18760 Set_Small_Value (Implicit_Base, Small_Val);
18762 -- If no range was given, set a dummy range
18764 if RRS <= Empty_Or_Error then
18765 Low_Val := -Small_Val;
18766 High_Val := Small_Val;
18768 -- Otherwise analyze and process given range
18772 Low : constant Node_Id := Low_Bound (RRS);
18773 High : constant Node_Id := High_Bound (RRS);
18776 Analyze_And_Resolve (Low, Any_Real);
18777 Analyze_And_Resolve (High, Any_Real);
18778 Check_Real_Bound (Low);
18779 Check_Real_Bound (High);
18781 -- Obtain and set the range
18783 Low_Val := Expr_Value_R (Low);
18784 High_Val := Expr_Value_R (High);
18786 if Low_Val > High_Val then
18787 Error_Msg_NE ("??fixed point type& has null range", Def, T);
18792 -- The range for both the implicit base and the declared first subtype
18793 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
18794 -- set a temporary range in place. Note that the bounds of the base
18795 -- type will be widened to be symmetrical and to fill the available
18796 -- bits when the type is frozen.
18798 -- We could do this with all discrete types, and probably should, but
18799 -- we absolutely have to do it for fixed-point, since the end-points
18800 -- of the range and the size are determined by the small value, which
18801 -- could be reset before the freeze point.
18803 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
18804 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
18806 -- Complete definition of first subtype. The inheritance of the rep item
18807 -- chain ensures that SPARK-related pragmas are not clobbered when the
18808 -- ordinary fixed point type acts as a full view of a private type.
18810 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
18811 Set_Etype (T, Implicit_Base);
18812 Init_Size_Align (T);
18813 Inherit_Rep_Item_Chain (T, Implicit_Base);
18814 Set_Small_Value (T, Small_Val);
18815 Set_Delta_Value (T, Delta_Val);
18816 Set_Is_Constrained (T);
18817 end Ordinary_Fixed_Point_Type_Declaration;
18819 ----------------------------------
18820 -- Preanalyze_Assert_Expression --
18821 ----------------------------------
18823 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
18825 In_Assertion_Expr := In_Assertion_Expr + 1;
18826 Preanalyze_Spec_Expression (N, T);
18827 In_Assertion_Expr := In_Assertion_Expr - 1;
18828 end Preanalyze_Assert_Expression;
18830 -----------------------------------
18831 -- Preanalyze_Default_Expression --
18832 -----------------------------------
18834 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
18835 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
18837 In_Default_Expr := True;
18838 Preanalyze_Spec_Expression (N, T);
18839 In_Default_Expr := Save_In_Default_Expr;
18840 end Preanalyze_Default_Expression;
18842 --------------------------------
18843 -- Preanalyze_Spec_Expression --
18844 --------------------------------
18846 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
18847 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
18849 In_Spec_Expression := True;
18850 Preanalyze_And_Resolve (N, T);
18851 In_Spec_Expression := Save_In_Spec_Expression;
18852 end Preanalyze_Spec_Expression;
18854 ----------------------------------------
18855 -- Prepare_Private_Subtype_Completion --
18856 ----------------------------------------
18858 procedure Prepare_Private_Subtype_Completion
18860 Related_Nod : Node_Id)
18862 Id_B : constant Entity_Id := Base_Type (Id);
18863 Full_B : Entity_Id := Full_View (Id_B);
18867 if Present (Full_B) then
18869 -- Get to the underlying full view if necessary
18871 if Is_Private_Type (Full_B)
18872 and then Present (Underlying_Full_View (Full_B))
18874 Full_B := Underlying_Full_View (Full_B);
18877 -- The Base_Type is already completed, we can complete the subtype
18878 -- now. We have to create a new entity with the same name, Thus we
18879 -- can't use Create_Itype.
18881 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
18882 Set_Is_Itype (Full);
18883 Set_Associated_Node_For_Itype (Full, Related_Nod);
18884 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
18887 -- The parent subtype may be private, but the base might not, in some
18888 -- nested instances. In that case, the subtype does not need to be
18889 -- exchanged. It would still be nice to make private subtypes and their
18890 -- bases consistent at all times ???
18892 if Is_Private_Type (Id_B) then
18893 Append_Elmt (Id, Private_Dependents (Id_B));
18895 end Prepare_Private_Subtype_Completion;
18897 ---------------------------
18898 -- Process_Discriminants --
18899 ---------------------------
18901 procedure Process_Discriminants
18903 Prev : Entity_Id := Empty)
18905 Elist : constant Elist_Id := New_Elmt_List;
18908 Discr_Number : Uint;
18909 Discr_Type : Entity_Id;
18910 Default_Present : Boolean := False;
18911 Default_Not_Present : Boolean := False;
18914 -- A composite type other than an array type can have discriminants.
18915 -- On entry, the current scope is the composite type.
18917 -- The discriminants are initially entered into the scope of the type
18918 -- via Enter_Name with the default Ekind of E_Void to prevent premature
18919 -- use, as explained at the end of this procedure.
18921 Discr := First (Discriminant_Specifications (N));
18922 while Present (Discr) loop
18923 Enter_Name (Defining_Identifier (Discr));
18925 -- For navigation purposes we add a reference to the discriminant
18926 -- in the entity for the type. If the current declaration is a
18927 -- completion, place references on the partial view. Otherwise the
18928 -- type is the current scope.
18930 if Present (Prev) then
18932 -- The references go on the partial view, if present. If the
18933 -- partial view has discriminants, the references have been
18934 -- generated already.
18936 if not Has_Discriminants (Prev) then
18937 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
18941 (Current_Scope, Defining_Identifier (Discr), 'd');
18944 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
18945 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
18947 -- Ada 2005 (AI-254)
18949 if Present (Access_To_Subprogram_Definition
18950 (Discriminant_Type (Discr)))
18951 and then Protected_Present (Access_To_Subprogram_Definition
18952 (Discriminant_Type (Discr)))
18955 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
18959 Find_Type (Discriminant_Type (Discr));
18960 Discr_Type := Etype (Discriminant_Type (Discr));
18962 if Error_Posted (Discriminant_Type (Discr)) then
18963 Discr_Type := Any_Type;
18967 -- Handling of discriminants that are access types
18969 if Is_Access_Type (Discr_Type) then
18971 -- Ada 2005 (AI-230): Access discriminant allowed in non-
18972 -- limited record types
18974 if Ada_Version < Ada_2005 then
18975 Check_Access_Discriminant_Requires_Limited
18976 (Discr, Discriminant_Type (Discr));
18979 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
18981 ("(Ada 83) access discriminant not allowed", Discr);
18984 -- If not access type, must be a discrete type
18986 elsif not Is_Discrete_Type (Discr_Type) then
18988 ("discriminants must have a discrete or access type",
18989 Discriminant_Type (Discr));
18992 Set_Etype (Defining_Identifier (Discr), Discr_Type);
18994 -- If a discriminant specification includes the assignment compound
18995 -- delimiter followed by an expression, the expression is the default
18996 -- expression of the discriminant; the default expression must be of
18997 -- the type of the discriminant. (RM 3.7.1) Since this expression is
18998 -- a default expression, we do the special preanalysis, since this
18999 -- expression does not freeze (see section "Handling of Default and
19000 -- Per-Object Expressions" in spec of package Sem).
19002 if Present (Expression (Discr)) then
19003 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
19007 if Nkind (N) = N_Formal_Type_Declaration then
19009 ("discriminant defaults not allowed for formal type",
19010 Expression (Discr));
19012 -- Flag an error for a tagged type with defaulted discriminants,
19013 -- excluding limited tagged types when compiling for Ada 2012
19014 -- (see AI05-0214).
19016 elsif Is_Tagged_Type (Current_Scope)
19017 and then (not Is_Limited_Type (Current_Scope)
19018 or else Ada_Version < Ada_2012)
19019 and then Comes_From_Source (N)
19021 -- Note: see similar test in Check_Or_Process_Discriminants, to
19022 -- handle the (illegal) case of the completion of an untagged
19023 -- view with discriminants with defaults by a tagged full view.
19024 -- We skip the check if Discr does not come from source, to
19025 -- account for the case of an untagged derived type providing
19026 -- defaults for a renamed discriminant from a private untagged
19027 -- ancestor with a tagged full view (ACATS B460006).
19029 if Ada_Version >= Ada_2012 then
19031 ("discriminants of nonlimited tagged type cannot have"
19033 Expression (Discr));
19036 ("discriminants of tagged type cannot have defaults",
19037 Expression (Discr));
19041 Default_Present := True;
19042 Append_Elmt (Expression (Discr), Elist);
19044 -- Tag the defining identifiers for the discriminants with
19045 -- their corresponding default expressions from the tree.
19047 Set_Discriminant_Default_Value
19048 (Defining_Identifier (Discr), Expression (Discr));
19051 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
19052 -- gets set unless we can be sure that no range check is required.
19054 if (GNATprove_Mode or not Expander_Active)
19057 (Expression (Discr), Discr_Type, Assume_Valid => True)
19059 Set_Do_Range_Check (Expression (Discr));
19062 -- No default discriminant value given
19065 Default_Not_Present := True;
19068 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
19069 -- Discr_Type but with the null-exclusion attribute
19071 if Ada_Version >= Ada_2005 then
19073 -- Ada 2005 (AI-231): Static checks
19075 if Can_Never_Be_Null (Discr_Type) then
19076 Null_Exclusion_Static_Checks (Discr);
19078 elsif Is_Access_Type (Discr_Type)
19079 and then Null_Exclusion_Present (Discr)
19081 -- No need to check itypes because in their case this check
19082 -- was done at their point of creation
19084 and then not Is_Itype (Discr_Type)
19086 if Can_Never_Be_Null (Discr_Type) then
19088 ("`NOT NULL` not allowed (& already excludes null)",
19093 Set_Etype (Defining_Identifier (Discr),
19094 Create_Null_Excluding_Itype
19096 Related_Nod => Discr));
19098 -- Check for improper null exclusion if the type is otherwise
19099 -- legal for a discriminant.
19101 elsif Null_Exclusion_Present (Discr)
19102 and then Is_Discrete_Type (Discr_Type)
19105 ("null exclusion can only apply to an access type", Discr);
19108 -- Ada 2005 (AI-402): access discriminants of nonlimited types
19109 -- can't have defaults. Synchronized types, or types that are
19110 -- explicitly limited are fine, but special tests apply to derived
19111 -- types in generics: in a generic body we have to assume the
19112 -- worst, and therefore defaults are not allowed if the parent is
19113 -- a generic formal private type (see ACATS B370001).
19115 if Is_Access_Type (Discr_Type) and then Default_Present then
19116 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
19117 or else Is_Limited_Record (Current_Scope)
19118 or else Is_Concurrent_Type (Current_Scope)
19119 or else Is_Concurrent_Record_Type (Current_Scope)
19120 or else Ekind (Current_Scope) = E_Limited_Private_Type
19122 if not Is_Derived_Type (Current_Scope)
19123 or else not Is_Generic_Type (Etype (Current_Scope))
19124 or else not In_Package_Body (Scope (Etype (Current_Scope)))
19125 or else Limited_Present
19126 (Type_Definition (Parent (Current_Scope)))
19132 ("access discriminants of nonlimited types cannot "
19133 & "have defaults", Expression (Discr));
19136 elsif Present (Expression (Discr)) then
19138 ("(Ada 2005) access discriminants of nonlimited types "
19139 & "cannot have defaults", Expression (Discr));
19144 -- A discriminant cannot be effectively volatile. This check is only
19145 -- relevant when SPARK_Mode is on as it is not standard Ada legality
19146 -- rule (SPARK RM 7.1.3(6)).
19149 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
19151 Error_Msg_N ("discriminant cannot be volatile", Discr);
19157 -- An element list consisting of the default expressions of the
19158 -- discriminants is constructed in the above loop and used to set
19159 -- the Discriminant_Constraint attribute for the type. If an object
19160 -- is declared of this (record or task) type without any explicit
19161 -- discriminant constraint given, this element list will form the
19162 -- actual parameters for the corresponding initialization procedure
19165 Set_Discriminant_Constraint (Current_Scope, Elist);
19166 Set_Stored_Constraint (Current_Scope, No_Elist);
19168 -- Default expressions must be provided either for all or for none
19169 -- of the discriminants of a discriminant part. (RM 3.7.1)
19171 if Default_Present and then Default_Not_Present then
19173 ("incomplete specification of defaults for discriminants", N);
19176 -- The use of the name of a discriminant is not allowed in default
19177 -- expressions of a discriminant part if the specification of the
19178 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
19180 -- To detect this, the discriminant names are entered initially with an
19181 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
19182 -- attempt to use a void entity (for example in an expression that is
19183 -- type-checked) produces the error message: premature usage. Now after
19184 -- completing the semantic analysis of the discriminant part, we can set
19185 -- the Ekind of all the discriminants appropriately.
19187 Discr := First (Discriminant_Specifications (N));
19188 Discr_Number := Uint_1;
19189 while Present (Discr) loop
19190 Id := Defining_Identifier (Discr);
19191 Set_Ekind (Id, E_Discriminant);
19192 Init_Component_Location (Id);
19194 Set_Discriminant_Number (Id, Discr_Number);
19196 -- Make sure this is always set, even in illegal programs
19198 Set_Corresponding_Discriminant (Id, Empty);
19200 -- Initialize the Original_Record_Component to the entity itself.
19201 -- Inherit_Components will propagate the right value to
19202 -- discriminants in derived record types.
19204 Set_Original_Record_Component (Id, Id);
19206 -- Create the discriminal for the discriminant
19208 Build_Discriminal (Id);
19211 Discr_Number := Discr_Number + 1;
19214 Set_Has_Discriminants (Current_Scope);
19215 end Process_Discriminants;
19217 -----------------------
19218 -- Process_Full_View --
19219 -----------------------
19221 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
19222 procedure Collect_Implemented_Interfaces
19224 Ifaces : Elist_Id);
19225 -- Ada 2005: Gather all the interfaces that Typ directly or
19226 -- inherently implements. Duplicate entries are not added to
19227 -- the list Ifaces.
19229 ------------------------------------
19230 -- Collect_Implemented_Interfaces --
19231 ------------------------------------
19233 procedure Collect_Implemented_Interfaces
19238 Iface_Elmt : Elmt_Id;
19241 -- Abstract interfaces are only associated with tagged record types
19243 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
19247 -- Recursively climb to the ancestors
19249 if Etype (Typ) /= Typ
19251 -- Protect the frontend against wrong cyclic declarations like:
19253 -- type B is new A with private;
19254 -- type C is new A with private;
19256 -- type B is new C with null record;
19257 -- type C is new B with null record;
19259 and then Etype (Typ) /= Priv_T
19260 and then Etype (Typ) /= Full_T
19262 -- Keep separate the management of private type declarations
19264 if Ekind (Typ) = E_Record_Type_With_Private then
19266 -- Handle the following illegal usage:
19267 -- type Private_Type is tagged private;
19269 -- type Private_Type is new Type_Implementing_Iface;
19271 if Present (Full_View (Typ))
19272 and then Etype (Typ) /= Full_View (Typ)
19274 if Is_Interface (Etype (Typ)) then
19275 Append_Unique_Elmt (Etype (Typ), Ifaces);
19278 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19281 -- Non-private types
19284 if Is_Interface (Etype (Typ)) then
19285 Append_Unique_Elmt (Etype (Typ), Ifaces);
19288 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
19292 -- Handle entities in the list of abstract interfaces
19294 if Present (Interfaces (Typ)) then
19295 Iface_Elmt := First_Elmt (Interfaces (Typ));
19296 while Present (Iface_Elmt) loop
19297 Iface := Node (Iface_Elmt);
19299 pragma Assert (Is_Interface (Iface));
19301 if not Contain_Interface (Iface, Ifaces) then
19302 Append_Elmt (Iface, Ifaces);
19303 Collect_Implemented_Interfaces (Iface, Ifaces);
19306 Next_Elmt (Iface_Elmt);
19309 end Collect_Implemented_Interfaces;
19313 Full_Indic : Node_Id;
19314 Full_Parent : Entity_Id;
19315 Priv_Parent : Entity_Id;
19317 -- Start of processing for Process_Full_View
19320 -- First some sanity checks that must be done after semantic
19321 -- decoration of the full view and thus cannot be placed with other
19322 -- similar checks in Find_Type_Name
19324 if not Is_Limited_Type (Priv_T)
19325 and then (Is_Limited_Type (Full_T)
19326 or else Is_Limited_Composite (Full_T))
19328 if In_Instance then
19332 ("completion of nonlimited type cannot be limited", Full_T);
19333 Explain_Limited_Type (Full_T, Full_T);
19336 elsif Is_Abstract_Type (Full_T)
19337 and then not Is_Abstract_Type (Priv_T)
19340 ("completion of nonabstract type cannot be abstract", Full_T);
19342 elsif Is_Tagged_Type (Priv_T)
19343 and then Is_Limited_Type (Priv_T)
19344 and then not Is_Limited_Type (Full_T)
19346 -- If pragma CPP_Class was applied to the private declaration
19347 -- propagate the limitedness to the full-view
19349 if Is_CPP_Class (Priv_T) then
19350 Set_Is_Limited_Record (Full_T);
19352 -- GNAT allow its own definition of Limited_Controlled to disobey
19353 -- this rule in order in ease the implementation. This test is safe
19354 -- because Root_Controlled is defined in a child of System that
19355 -- normal programs are not supposed to use.
19357 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
19358 Set_Is_Limited_Composite (Full_T);
19361 ("completion of limited tagged type must be limited", Full_T);
19364 elsif Is_Generic_Type (Priv_T) then
19365 Error_Msg_N ("generic type cannot have a completion", Full_T);
19368 -- Check that ancestor interfaces of private and full views are
19369 -- consistent. We omit this check for synchronized types because
19370 -- they are performed on the corresponding record type when frozen.
19372 if Ada_Version >= Ada_2005
19373 and then Is_Tagged_Type (Priv_T)
19374 and then Is_Tagged_Type (Full_T)
19375 and then not Is_Concurrent_Type (Full_T)
19379 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
19380 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
19383 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
19384 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
19386 -- Ada 2005 (AI-251): The partial view shall be a descendant of
19387 -- an interface type if and only if the full type is descendant
19388 -- of the interface type (AARM 7.3 (7.3/2)).
19390 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
19392 if Present (Iface) then
19394 ("interface in partial view& not implemented by full type "
19395 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19398 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
19400 if Present (Iface) then
19402 ("interface & not implemented by partial view "
19403 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
19408 if Is_Tagged_Type (Priv_T)
19409 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19410 and then Is_Derived_Type (Full_T)
19412 Priv_Parent := Etype (Priv_T);
19414 -- The full view of a private extension may have been transformed
19415 -- into an unconstrained derived type declaration and a subtype
19416 -- declaration (see build_derived_record_type for details).
19418 if Nkind (N) = N_Subtype_Declaration then
19419 Full_Indic := Subtype_Indication (N);
19420 Full_Parent := Etype (Base_Type (Full_T));
19422 Full_Indic := Subtype_Indication (Type_Definition (N));
19423 Full_Parent := Etype (Full_T);
19426 -- Check that the parent type of the full type is a descendant of
19427 -- the ancestor subtype given in the private extension. If either
19428 -- entity has an Etype equal to Any_Type then we had some previous
19429 -- error situation [7.3(8)].
19431 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
19434 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
19435 -- any order. Therefore we don't have to check that its parent must
19436 -- be a descendant of the parent of the private type declaration.
19438 elsif Is_Interface (Priv_Parent)
19439 and then Is_Interface (Full_Parent)
19443 -- Ada 2005 (AI-251): If the parent of the private type declaration
19444 -- is an interface there is no need to check that it is an ancestor
19445 -- of the associated full type declaration. The required tests for
19446 -- this case are performed by Build_Derived_Record_Type.
19448 elsif not Is_Interface (Base_Type (Priv_Parent))
19449 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
19452 ("parent of full type must descend from parent"
19453 & " of private extension", Full_Indic);
19455 -- First check a formal restriction, and then proceed with checking
19456 -- Ada rules. Since the formal restriction is not a serious error, we
19457 -- don't prevent further error detection for this check, hence the
19461 -- In formal mode, when completing a private extension the type
19462 -- named in the private part must be exactly the same as that
19463 -- named in the visible part.
19465 if Priv_Parent /= Full_Parent then
19466 Error_Msg_Name_1 := Chars (Priv_Parent);
19467 Check_SPARK_05_Restriction ("% expected", Full_Indic);
19470 -- Check the rules of 7.3(10): if the private extension inherits
19471 -- known discriminants, then the full type must also inherit those
19472 -- discriminants from the same (ancestor) type, and the parent
19473 -- subtype of the full type must be constrained if and only if
19474 -- the ancestor subtype of the private extension is constrained.
19476 if No (Discriminant_Specifications (Parent (Priv_T)))
19477 and then not Has_Unknown_Discriminants (Priv_T)
19478 and then Has_Discriminants (Base_Type (Priv_Parent))
19481 Priv_Indic : constant Node_Id :=
19482 Subtype_Indication (Parent (Priv_T));
19484 Priv_Constr : constant Boolean :=
19485 Is_Constrained (Priv_Parent)
19487 Nkind (Priv_Indic) = N_Subtype_Indication
19489 Is_Constrained (Entity (Priv_Indic));
19491 Full_Constr : constant Boolean :=
19492 Is_Constrained (Full_Parent)
19494 Nkind (Full_Indic) = N_Subtype_Indication
19496 Is_Constrained (Entity (Full_Indic));
19498 Priv_Discr : Entity_Id;
19499 Full_Discr : Entity_Id;
19502 Priv_Discr := First_Discriminant (Priv_Parent);
19503 Full_Discr := First_Discriminant (Full_Parent);
19504 while Present (Priv_Discr) and then Present (Full_Discr) loop
19505 if Original_Record_Component (Priv_Discr) =
19506 Original_Record_Component (Full_Discr)
19508 Corresponding_Discriminant (Priv_Discr) =
19509 Corresponding_Discriminant (Full_Discr)
19516 Next_Discriminant (Priv_Discr);
19517 Next_Discriminant (Full_Discr);
19520 if Present (Priv_Discr) or else Present (Full_Discr) then
19522 ("full view must inherit discriminants of the parent"
19523 & " type used in the private extension", Full_Indic);
19525 elsif Priv_Constr and then not Full_Constr then
19527 ("parent subtype of full type must be constrained",
19530 elsif Full_Constr and then not Priv_Constr then
19532 ("parent subtype of full type must be unconstrained",
19537 -- Check the rules of 7.3(12): if a partial view has neither
19538 -- known or unknown discriminants, then the full type
19539 -- declaration shall define a definite subtype.
19541 elsif not Has_Unknown_Discriminants (Priv_T)
19542 and then not Has_Discriminants (Priv_T)
19543 and then not Is_Constrained (Full_T)
19546 ("full view must define a constrained type if partial view"
19547 & " has no discriminants", Full_T);
19550 -- ??????? Do we implement the following properly ?????
19551 -- If the ancestor subtype of a private extension has constrained
19552 -- discriminants, then the parent subtype of the full view shall
19553 -- impose a statically matching constraint on those discriminants
19558 -- For untagged types, verify that a type without discriminants is
19559 -- not completed with an unconstrained type. A separate error message
19560 -- is produced if the full type has defaulted discriminants.
19562 if not Is_Indefinite_Subtype (Priv_T)
19563 and then Is_Indefinite_Subtype (Full_T)
19565 Error_Msg_Sloc := Sloc (Parent (Priv_T));
19567 ("full view of& not compatible with declaration#",
19570 if not Is_Tagged_Type (Full_T) then
19572 ("\one is constrained, the other unconstrained", Full_T);
19577 -- AI-419: verify that the use of "limited" is consistent
19580 Orig_Decl : constant Node_Id := Original_Node (N);
19583 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19584 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
19586 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
19588 if not Limited_Present (Parent (Priv_T))
19589 and then not Synchronized_Present (Parent (Priv_T))
19590 and then Limited_Present (Type_Definition (Orig_Decl))
19593 ("full view of non-limited extension cannot be limited", N);
19595 -- Conversely, if the partial view carries the limited keyword,
19596 -- the full view must as well, even if it may be redundant.
19598 elsif Limited_Present (Parent (Priv_T))
19599 and then not Limited_Present (Type_Definition (Orig_Decl))
19602 ("full view of limited extension must be explicitly limited",
19608 -- Ada 2005 (AI-443): A synchronized private extension must be
19609 -- completed by a task or protected type.
19611 if Ada_Version >= Ada_2005
19612 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
19613 and then Synchronized_Present (Parent (Priv_T))
19614 and then not Is_Concurrent_Type (Full_T)
19616 Error_Msg_N ("full view of synchronized extension must " &
19617 "be synchronized type", N);
19620 -- Ada 2005 AI-363: if the full view has discriminants with
19621 -- defaults, it is illegal to declare constrained access subtypes
19622 -- whose designated type is the current type. This allows objects
19623 -- of the type that are declared in the heap to be unconstrained.
19625 if not Has_Unknown_Discriminants (Priv_T)
19626 and then not Has_Discriminants (Priv_T)
19627 and then Has_Discriminants (Full_T)
19629 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
19631 Set_Has_Constrained_Partial_View (Full_T);
19632 Set_Has_Constrained_Partial_View (Priv_T);
19635 -- Create a full declaration for all its subtypes recorded in
19636 -- Private_Dependents and swap them similarly to the base type. These
19637 -- are subtypes that have been define before the full declaration of
19638 -- the private type. We also swap the entry in Private_Dependents list
19639 -- so we can properly restore the private view on exit from the scope.
19642 Priv_Elmt : Elmt_Id;
19643 Priv_Scop : Entity_Id;
19648 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
19649 while Present (Priv_Elmt) loop
19650 Priv := Node (Priv_Elmt);
19651 Priv_Scop := Scope (Priv);
19653 if Ekind_In (Priv, E_Private_Subtype,
19654 E_Limited_Private_Subtype,
19655 E_Record_Subtype_With_Private)
19657 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
19658 Set_Is_Itype (Full);
19659 Set_Parent (Full, Parent (Priv));
19660 Set_Associated_Node_For_Itype (Full, N);
19662 -- Now we need to complete the private subtype, but since the
19663 -- base type has already been swapped, we must also swap the
19664 -- subtypes (and thus, reverse the arguments in the call to
19665 -- Complete_Private_Subtype). Also note that we may need to
19666 -- re-establish the scope of the private subtype.
19668 Copy_And_Swap (Priv, Full);
19670 if not In_Open_Scopes (Priv_Scop) then
19671 Push_Scope (Priv_Scop);
19674 -- Reset Priv_Scop to Empty to indicate no scope was pushed
19676 Priv_Scop := Empty;
19679 Complete_Private_Subtype (Full, Priv, Full_T, N);
19681 if Present (Priv_Scop) then
19685 Replace_Elmt (Priv_Elmt, Full);
19688 Next_Elmt (Priv_Elmt);
19692 -- If the private view was tagged, copy the new primitive operations
19693 -- from the private view to the full view.
19695 if Is_Tagged_Type (Full_T) then
19697 Disp_Typ : Entity_Id;
19698 Full_List : Elist_Id;
19700 Prim_Elmt : Elmt_Id;
19701 Priv_List : Elist_Id;
19705 L : Elist_Id) return Boolean;
19706 -- Determine whether list L contains element E
19714 L : Elist_Id) return Boolean
19716 List_Elmt : Elmt_Id;
19719 List_Elmt := First_Elmt (L);
19720 while Present (List_Elmt) loop
19721 if Node (List_Elmt) = E then
19725 Next_Elmt (List_Elmt);
19731 -- Start of processing
19734 if Is_Tagged_Type (Priv_T) then
19735 Priv_List := Primitive_Operations (Priv_T);
19736 Prim_Elmt := First_Elmt (Priv_List);
19738 -- In the case of a concurrent type completing a private tagged
19739 -- type, primitives may have been declared in between the two
19740 -- views. These subprograms need to be wrapped the same way
19741 -- entries and protected procedures are handled because they
19742 -- cannot be directly shared by the two views.
19744 if Is_Concurrent_Type (Full_T) then
19746 Conc_Typ : constant Entity_Id :=
19747 Corresponding_Record_Type (Full_T);
19748 Curr_Nod : Node_Id := Parent (Conc_Typ);
19749 Wrap_Spec : Node_Id;
19752 while Present (Prim_Elmt) loop
19753 Prim := Node (Prim_Elmt);
19755 if Comes_From_Source (Prim)
19756 and then not Is_Abstract_Subprogram (Prim)
19759 Make_Subprogram_Declaration (Sloc (Prim),
19763 Obj_Typ => Conc_Typ,
19765 Parameter_Specifications (
19768 Insert_After (Curr_Nod, Wrap_Spec);
19769 Curr_Nod := Wrap_Spec;
19771 Analyze (Wrap_Spec);
19774 Next_Elmt (Prim_Elmt);
19780 -- For non-concurrent types, transfer explicit primitives, but
19781 -- omit those inherited from the parent of the private view
19782 -- since they will be re-inherited later on.
19785 Full_List := Primitive_Operations (Full_T);
19787 while Present (Prim_Elmt) loop
19788 Prim := Node (Prim_Elmt);
19790 if Comes_From_Source (Prim)
19791 and then not Contains (Prim, Full_List)
19793 Append_Elmt (Prim, Full_List);
19796 Next_Elmt (Prim_Elmt);
19800 -- Untagged private view
19803 Full_List := Primitive_Operations (Full_T);
19805 -- In this case the partial view is untagged, so here we locate
19806 -- all of the earlier primitives that need to be treated as
19807 -- dispatching (those that appear between the two views). Note
19808 -- that these additional operations must all be new operations
19809 -- (any earlier operations that override inherited operations
19810 -- of the full view will already have been inserted in the
19811 -- primitives list, marked by Check_Operation_From_Private_View
19812 -- as dispatching. Note that implicit "/=" operators are
19813 -- excluded from being added to the primitives list since they
19814 -- shouldn't be treated as dispatching (tagged "/=" is handled
19817 Prim := Next_Entity (Full_T);
19818 while Present (Prim) and then Prim /= Priv_T loop
19819 if Ekind_In (Prim, E_Procedure, E_Function) then
19820 Disp_Typ := Find_Dispatching_Type (Prim);
19822 if Disp_Typ = Full_T
19823 and then (Chars (Prim) /= Name_Op_Ne
19824 or else Comes_From_Source (Prim))
19826 Check_Controlling_Formals (Full_T, Prim);
19828 if not Is_Dispatching_Operation (Prim) then
19829 Append_Elmt (Prim, Full_List);
19830 Set_Is_Dispatching_Operation (Prim, True);
19831 Set_DT_Position_Value (Prim, No_Uint);
19834 elsif Is_Dispatching_Operation (Prim)
19835 and then Disp_Typ /= Full_T
19838 -- Verify that it is not otherwise controlled by a
19839 -- formal or a return value of type T.
19841 Check_Controlling_Formals (Disp_Typ, Prim);
19845 Next_Entity (Prim);
19849 -- For the tagged case, the two views can share the same primitive
19850 -- operations list and the same class-wide type. Update attributes
19851 -- of the class-wide type which depend on the full declaration.
19853 if Is_Tagged_Type (Priv_T) then
19854 Set_Direct_Primitive_Operations (Priv_T, Full_List);
19855 Set_Class_Wide_Type
19856 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
19858 Set_Has_Task (Class_Wide_Type (Priv_T), Has_Task (Full_T));
19860 (Class_Wide_Type (Priv_T), Has_Protected (Full_T));
19865 -- Ada 2005 AI 161: Check preelaborable initialization consistency
19867 if Known_To_Have_Preelab_Init (Priv_T) then
19869 -- Case where there is a pragma Preelaborable_Initialization. We
19870 -- always allow this in predefined units, which is cheating a bit,
19871 -- but it means we don't have to struggle to meet the requirements in
19872 -- the RM for having Preelaborable Initialization. Otherwise we
19873 -- require that the type meets the RM rules. But we can't check that
19874 -- yet, because of the rule about overriding Initialize, so we simply
19875 -- set a flag that will be checked at freeze time.
19877 if not In_Predefined_Unit (Full_T) then
19878 Set_Must_Have_Preelab_Init (Full_T);
19882 -- If pragma CPP_Class was applied to the private type declaration,
19883 -- propagate it now to the full type declaration.
19885 if Is_CPP_Class (Priv_T) then
19886 Set_Is_CPP_Class (Full_T);
19887 Set_Convention (Full_T, Convention_CPP);
19889 -- Check that components of imported CPP types do not have default
19892 Check_CPP_Type_Has_No_Defaults (Full_T);
19895 -- If the private view has user specified stream attributes, then so has
19898 -- Why the test, how could these flags be already set in Full_T ???
19900 if Has_Specified_Stream_Read (Priv_T) then
19901 Set_Has_Specified_Stream_Read (Full_T);
19904 if Has_Specified_Stream_Write (Priv_T) then
19905 Set_Has_Specified_Stream_Write (Full_T);
19908 if Has_Specified_Stream_Input (Priv_T) then
19909 Set_Has_Specified_Stream_Input (Full_T);
19912 if Has_Specified_Stream_Output (Priv_T) then
19913 Set_Has_Specified_Stream_Output (Full_T);
19916 -- Propagate the attributes related to pragma Default_Initial_Condition
19917 -- from the private to the full view. Note that both flags are mutually
19920 if Has_Default_Init_Cond (Priv_T)
19921 or else Has_Inherited_Default_Init_Cond (Priv_T)
19923 Propagate_Default_Init_Cond_Attributes
19924 (From_Typ => Priv_T,
19926 Private_To_Full_View => True);
19928 -- In the case where the full view is derived from another private type,
19929 -- the attributes related to pragma Default_Initial_Condition must be
19930 -- propagated from the full to the private view to maintain consistency
19934 -- type Parent_Typ is private
19935 -- with Default_Initial_Condition ...;
19937 -- type Parent_Typ is ...;
19940 -- with Pack; use Pack;
19941 -- package Pack_2 is
19942 -- type Deriv_Typ is private; -- must inherit
19944 -- type Deriv_Typ is new Parent_Typ; -- must inherit
19947 elsif Has_Default_Init_Cond (Full_T)
19948 or else Has_Inherited_Default_Init_Cond (Full_T)
19950 Propagate_Default_Init_Cond_Attributes
19951 (From_Typ => Full_T,
19953 Private_To_Full_View => True);
19956 -- Propagate the attributes related to pragma Ghost from the private to
19959 if Is_Ghost_Entity (Priv_T) then
19960 Set_Is_Ghost_Entity (Full_T);
19962 -- The Ghost policy in effect at the point of declaration and at the
19963 -- point of completion must match (SPARK RM 6.9(15)).
19965 Check_Ghost_Completion (Priv_T, Full_T);
19967 -- In the case where the private view of a tagged type lacks a parent
19968 -- type and is subject to pragma Ghost, ensure that the parent type
19969 -- specified by the full view is also Ghost (SPARK RM 6.9(9)).
19971 if Is_Derived_Type (Full_T) then
19972 Check_Ghost_Derivation (Full_T);
19976 -- Propagate invariants to full type
19978 if Has_Invariants (Priv_T) then
19979 Set_Has_Invariants (Full_T);
19980 Set_Invariant_Procedure (Full_T, Invariant_Procedure (Priv_T));
19983 if Has_Inheritable_Invariants (Priv_T) then
19984 Set_Has_Inheritable_Invariants (Full_T);
19987 -- Check hidden inheritance of class-wide type invariants
19989 if Ada_Version >= Ada_2012
19990 and then not Has_Inheritable_Invariants (Full_T)
19991 and then In_Private_Part (Current_Scope)
19992 and then Has_Interfaces (Full_T)
19999 Collect_Interfaces (Full_T, Ifaces, Exclude_Parents => True);
20001 AI := First_Elmt (Ifaces);
20002 while Present (AI) loop
20003 if Has_Inheritable_Invariants (Node (AI)) then
20005 ("hidden inheritance of class-wide type invariants " &
20015 -- Propagate predicates to full type, and predicate function if already
20016 -- defined. It is not clear that this can actually happen? the partial
20017 -- view cannot be frozen yet, and the predicate function has not been
20018 -- built. Still it is a cheap check and seems safer to make it.
20020 if Has_Predicates (Priv_T) then
20021 if Present (Predicate_Function (Priv_T)) then
20022 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
20025 Set_Has_Predicates (Full_T);
20027 end Process_Full_View;
20029 -----------------------------------
20030 -- Process_Incomplete_Dependents --
20031 -----------------------------------
20033 procedure Process_Incomplete_Dependents
20035 Full_T : Entity_Id;
20038 Inc_Elmt : Elmt_Id;
20039 Priv_Dep : Entity_Id;
20040 New_Subt : Entity_Id;
20042 Disc_Constraint : Elist_Id;
20045 if No (Private_Dependents (Inc_T)) then
20049 -- Itypes that may be generated by the completion of an incomplete
20050 -- subtype are not used by the back-end and not attached to the tree.
20051 -- They are created only for constraint-checking purposes.
20053 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
20054 while Present (Inc_Elmt) loop
20055 Priv_Dep := Node (Inc_Elmt);
20057 if Ekind (Priv_Dep) = E_Subprogram_Type then
20059 -- An Access_To_Subprogram type may have a return type or a
20060 -- parameter type that is incomplete. Replace with the full view.
20062 if Etype (Priv_Dep) = Inc_T then
20063 Set_Etype (Priv_Dep, Full_T);
20067 Formal : Entity_Id;
20070 Formal := First_Formal (Priv_Dep);
20071 while Present (Formal) loop
20072 if Etype (Formal) = Inc_T then
20073 Set_Etype (Formal, Full_T);
20076 Next_Formal (Formal);
20080 elsif Is_Overloadable (Priv_Dep) then
20082 -- If a subprogram in the incomplete dependents list is primitive
20083 -- for a tagged full type then mark it as a dispatching operation,
20084 -- check whether it overrides an inherited subprogram, and check
20085 -- restrictions on its controlling formals. Note that a protected
20086 -- operation is never dispatching: only its wrapper operation
20087 -- (which has convention Ada) is.
20089 if Is_Tagged_Type (Full_T)
20090 and then Is_Primitive (Priv_Dep)
20091 and then Convention (Priv_Dep) /= Convention_Protected
20093 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
20094 Set_Is_Dispatching_Operation (Priv_Dep);
20095 Check_Controlling_Formals (Full_T, Priv_Dep);
20098 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
20100 -- Can happen during processing of a body before the completion
20101 -- of a TA type. Ignore, because spec is also on dependent list.
20105 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
20106 -- corresponding subtype of the full view.
20108 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype then
20109 Set_Subtype_Indication
20110 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
20111 Set_Etype (Priv_Dep, Full_T);
20112 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
20113 Set_Analyzed (Parent (Priv_Dep), False);
20115 -- Reanalyze the declaration, suppressing the call to
20116 -- Enter_Name to avoid duplicate names.
20118 Analyze_Subtype_Declaration
20119 (N => Parent (Priv_Dep),
20122 -- Dependent is a subtype
20125 -- We build a new subtype indication using the full view of the
20126 -- incomplete parent. The discriminant constraints have been
20127 -- elaborated already at the point of the subtype declaration.
20129 New_Subt := Create_Itype (E_Void, N);
20131 if Has_Discriminants (Full_T) then
20132 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
20134 Disc_Constraint := No_Elist;
20137 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
20138 Set_Full_View (Priv_Dep, New_Subt);
20141 Next_Elmt (Inc_Elmt);
20143 end Process_Incomplete_Dependents;
20145 --------------------------------
20146 -- Process_Range_Expr_In_Decl --
20147 --------------------------------
20149 procedure Process_Range_Expr_In_Decl
20152 Subtyp : Entity_Id := Empty;
20153 Check_List : List_Id := Empty_List;
20154 R_Check_Off : Boolean := False;
20155 In_Iter_Schm : Boolean := False)
20158 R_Checks : Check_Result;
20159 Insert_Node : Node_Id;
20160 Def_Id : Entity_Id;
20163 Analyze_And_Resolve (R, Base_Type (T));
20165 if Nkind (R) = N_Range then
20167 -- In SPARK, all ranges should be static, with the exception of the
20168 -- discrete type definition of a loop parameter specification.
20170 if not In_Iter_Schm
20171 and then not Is_OK_Static_Range (R)
20173 Check_SPARK_05_Restriction ("range should be static", R);
20176 Lo := Low_Bound (R);
20177 Hi := High_Bound (R);
20179 -- Validity checks on the range of a quantified expression are
20180 -- delayed until the construct is transformed into a loop.
20182 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
20183 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
20187 -- We need to ensure validity of the bounds here, because if we
20188 -- go ahead and do the expansion, then the expanded code will get
20189 -- analyzed with range checks suppressed and we miss the check.
20191 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
20192 -- the temporaries generated by routine Remove_Side_Effects by means
20193 -- of validity checks must use the same names. When a range appears
20194 -- in the parent of a generic, the range is processed with checks
20195 -- disabled as part of the generic context and with checks enabled
20196 -- for code generation purposes. This leads to link issues as the
20197 -- generic contains references to xxx_FIRST/_LAST, but the inlined
20198 -- template sees the temporaries generated by Remove_Side_Effects.
20201 Validity_Check_Range (R, Subtyp);
20204 -- If there were errors in the declaration, try and patch up some
20205 -- common mistakes in the bounds. The cases handled are literals
20206 -- which are Integer where the expected type is Real and vice versa.
20207 -- These corrections allow the compilation process to proceed further
20208 -- along since some basic assumptions of the format of the bounds
20211 if Etype (R) = Any_Type then
20212 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
20214 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
20216 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
20218 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
20220 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
20222 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
20224 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
20226 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
20233 -- If the bounds of the range have been mistakenly given as string
20234 -- literals (perhaps in place of character literals), then an error
20235 -- has already been reported, but we rewrite the string literal as a
20236 -- bound of the range's type to avoid blowups in later processing
20237 -- that looks at static values.
20239 if Nkind (Lo) = N_String_Literal then
20241 Make_Attribute_Reference (Sloc (Lo),
20242 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
20243 Attribute_Name => Name_First));
20244 Analyze_And_Resolve (Lo);
20247 if Nkind (Hi) = N_String_Literal then
20249 Make_Attribute_Reference (Sloc (Hi),
20250 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
20251 Attribute_Name => Name_First));
20252 Analyze_And_Resolve (Hi);
20255 -- If bounds aren't scalar at this point then exit, avoiding
20256 -- problems with further processing of the range in this procedure.
20258 if not Is_Scalar_Type (Etype (Lo)) then
20262 -- Resolve (actually Sem_Eval) has checked that the bounds are in
20263 -- then range of the base type. Here we check whether the bounds
20264 -- are in the range of the subtype itself. Note that if the bounds
20265 -- represent the null range the Constraint_Error exception should
20268 -- ??? The following code should be cleaned up as follows
20270 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
20271 -- is done in the call to Range_Check (R, T); below
20273 -- 2. The use of R_Check_Off should be investigated and possibly
20274 -- removed, this would clean up things a bit.
20276 if Is_Null_Range (Lo, Hi) then
20280 -- Capture values of bounds and generate temporaries for them
20281 -- if needed, before applying checks, since checks may cause
20282 -- duplication of the expression without forcing evaluation.
20284 -- The forced evaluation removes side effects from expressions,
20285 -- which should occur also in GNATprove mode. Otherwise, we end up
20286 -- with unexpected insertions of actions at places where this is
20287 -- not supposed to occur, e.g. on default parameters of a call.
20289 if Expander_Active or GNATprove_Mode then
20291 -- Call Force_Evaluation to create declarations as needed to
20292 -- deal with side effects, and also create typ_FIRST/LAST
20293 -- entities for bounds if we have a subtype name.
20295 -- Note: we do this transformation even if expansion is not
20296 -- active if we are in GNATprove_Mode since the transformation
20297 -- is in general required to ensure that the resulting tree has
20298 -- proper Ada semantics.
20301 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
20303 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
20306 -- We use a flag here instead of suppressing checks on the type
20307 -- because the type we check against isn't necessarily the place
20308 -- where we put the check.
20310 if not R_Check_Off then
20311 R_Checks := Get_Range_Checks (R, T);
20313 -- Look up tree to find an appropriate insertion point. We
20314 -- can't just use insert_actions because later processing
20315 -- depends on the insertion node. Prior to Ada 2012 the
20316 -- insertion point could only be a declaration or a loop, but
20317 -- quantified expressions can appear within any context in an
20318 -- expression, and the insertion point can be any statement,
20319 -- pragma, or declaration.
20321 Insert_Node := Parent (R);
20322 while Present (Insert_Node) loop
20324 Nkind (Insert_Node) in N_Declaration
20327 (Insert_Node, N_Component_Declaration,
20328 N_Loop_Parameter_Specification,
20329 N_Function_Specification,
20330 N_Procedure_Specification);
20332 exit when Nkind (Insert_Node) in N_Later_Decl_Item
20333 or else Nkind (Insert_Node) in
20334 N_Statement_Other_Than_Procedure_Call
20335 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
20338 Insert_Node := Parent (Insert_Node);
20341 -- Why would Type_Decl not be present??? Without this test,
20342 -- short regression tests fail.
20344 if Present (Insert_Node) then
20346 -- Case of loop statement. Verify that the range is part
20347 -- of the subtype indication of the iteration scheme.
20349 if Nkind (Insert_Node) = N_Loop_Statement then
20354 Indic := Parent (R);
20355 while Present (Indic)
20356 and then Nkind (Indic) /= N_Subtype_Indication
20358 Indic := Parent (Indic);
20361 if Present (Indic) then
20362 Def_Id := Etype (Subtype_Mark (Indic));
20364 Insert_Range_Checks
20368 Sloc (Insert_Node),
20370 Do_Before => True);
20374 -- Insertion before a declaration. If the declaration
20375 -- includes discriminants, the list of applicable checks
20376 -- is given by the caller.
20378 elsif Nkind (Insert_Node) in N_Declaration then
20379 Def_Id := Defining_Identifier (Insert_Node);
20381 if (Ekind (Def_Id) = E_Record_Type
20382 and then Depends_On_Discriminant (R))
20384 (Ekind (Def_Id) = E_Protected_Type
20385 and then Has_Discriminants (Def_Id))
20387 Append_Range_Checks
20389 Check_List, Def_Id, Sloc (Insert_Node), R);
20392 Insert_Range_Checks
20394 Insert_Node, Def_Id, Sloc (Insert_Node), R);
20398 -- Insertion before a statement. Range appears in the
20399 -- context of a quantified expression. Insertion will
20400 -- take place when expression is expanded.
20409 -- Case of other than an explicit N_Range node
20411 -- The forced evaluation removes side effects from expressions, which
20412 -- should occur also in GNATprove mode. Otherwise, we end up with
20413 -- unexpected insertions of actions at places where this is not
20414 -- supposed to occur, e.g. on default parameters of a call.
20416 elsif Expander_Active or GNATprove_Mode then
20417 Get_Index_Bounds (R, Lo, Hi);
20418 Force_Evaluation (Lo);
20419 Force_Evaluation (Hi);
20421 end Process_Range_Expr_In_Decl;
20423 --------------------------------------
20424 -- Process_Real_Range_Specification --
20425 --------------------------------------
20427 procedure Process_Real_Range_Specification (Def : Node_Id) is
20428 Spec : constant Node_Id := Real_Range_Specification (Def);
20431 Err : Boolean := False;
20433 procedure Analyze_Bound (N : Node_Id);
20434 -- Analyze and check one bound
20436 -------------------
20437 -- Analyze_Bound --
20438 -------------------
20440 procedure Analyze_Bound (N : Node_Id) is
20442 Analyze_And_Resolve (N, Any_Real);
20444 if not Is_OK_Static_Expression (N) then
20445 Flag_Non_Static_Expr
20446 ("bound in real type definition is not static!", N);
20451 -- Start of processing for Process_Real_Range_Specification
20454 if Present (Spec) then
20455 Lo := Low_Bound (Spec);
20456 Hi := High_Bound (Spec);
20457 Analyze_Bound (Lo);
20458 Analyze_Bound (Hi);
20460 -- If error, clear away junk range specification
20463 Set_Real_Range_Specification (Def, Empty);
20466 end Process_Real_Range_Specification;
20468 ---------------------
20469 -- Process_Subtype --
20470 ---------------------
20472 function Process_Subtype
20474 Related_Nod : Node_Id;
20475 Related_Id : Entity_Id := Empty;
20476 Suffix : Character := ' ') return Entity_Id
20479 Def_Id : Entity_Id;
20480 Error_Node : Node_Id;
20481 Full_View_Id : Entity_Id;
20482 Subtype_Mark_Id : Entity_Id;
20484 May_Have_Null_Exclusion : Boolean;
20486 procedure Check_Incomplete (T : Entity_Id);
20487 -- Called to verify that an incomplete type is not used prematurely
20489 ----------------------
20490 -- Check_Incomplete --
20491 ----------------------
20493 procedure Check_Incomplete (T : Entity_Id) is
20495 -- Ada 2005 (AI-412): Incomplete subtypes are legal
20497 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
20499 not (Ada_Version >= Ada_2005
20501 (Nkind (Parent (T)) = N_Subtype_Declaration
20502 or else (Nkind (Parent (T)) = N_Subtype_Indication
20503 and then Nkind (Parent (Parent (T))) =
20504 N_Subtype_Declaration)))
20506 Error_Msg_N ("invalid use of type before its full declaration", T);
20508 end Check_Incomplete;
20510 -- Start of processing for Process_Subtype
20513 -- Case of no constraints present
20515 if Nkind (S) /= N_Subtype_Indication then
20517 Check_Incomplete (S);
20520 -- Ada 2005 (AI-231): Static check
20522 if Ada_Version >= Ada_2005
20523 and then Present (P)
20524 and then Null_Exclusion_Present (P)
20525 and then Nkind (P) /= N_Access_To_Object_Definition
20526 and then not Is_Access_Type (Entity (S))
20528 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
20531 -- The following is ugly, can't we have a range or even a flag???
20533 May_Have_Null_Exclusion :=
20534 Nkind_In (P, N_Access_Definition,
20535 N_Access_Function_Definition,
20536 N_Access_Procedure_Definition,
20537 N_Access_To_Object_Definition,
20539 N_Component_Definition)
20541 Nkind_In (P, N_Derived_Type_Definition,
20542 N_Discriminant_Specification,
20543 N_Formal_Object_Declaration,
20544 N_Object_Declaration,
20545 N_Object_Renaming_Declaration,
20546 N_Parameter_Specification,
20547 N_Subtype_Declaration);
20549 -- Create an Itype that is a duplicate of Entity (S) but with the
20550 -- null-exclusion attribute.
20552 if May_Have_Null_Exclusion
20553 and then Is_Access_Type (Entity (S))
20554 and then Null_Exclusion_Present (P)
20556 -- No need to check the case of an access to object definition.
20557 -- It is correct to define double not-null pointers.
20560 -- type Not_Null_Int_Ptr is not null access Integer;
20561 -- type Acc is not null access Not_Null_Int_Ptr;
20563 and then Nkind (P) /= N_Access_To_Object_Definition
20565 if Can_Never_Be_Null (Entity (S)) then
20566 case Nkind (Related_Nod) is
20567 when N_Full_Type_Declaration =>
20568 if Nkind (Type_Definition (Related_Nod))
20569 in N_Array_Type_Definition
20573 (Component_Definition
20574 (Type_Definition (Related_Nod)));
20577 Subtype_Indication (Type_Definition (Related_Nod));
20580 when N_Subtype_Declaration =>
20581 Error_Node := Subtype_Indication (Related_Nod);
20583 when N_Object_Declaration =>
20584 Error_Node := Object_Definition (Related_Nod);
20586 when N_Component_Declaration =>
20588 Subtype_Indication (Component_Definition (Related_Nod));
20590 when N_Allocator =>
20591 Error_Node := Expression (Related_Nod);
20594 pragma Assert (False);
20595 Error_Node := Related_Nod;
20599 ("`NOT NULL` not allowed (& already excludes null)",
20605 Create_Null_Excluding_Itype
20607 Related_Nod => P));
20608 Set_Entity (S, Etype (S));
20613 -- Case of constraint present, so that we have an N_Subtype_Indication
20614 -- node (this node is created only if constraints are present).
20617 Find_Type (Subtype_Mark (S));
20619 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
20621 (Nkind (Parent (S)) = N_Subtype_Declaration
20622 and then Is_Itype (Defining_Identifier (Parent (S))))
20624 Check_Incomplete (Subtype_Mark (S));
20628 Subtype_Mark_Id := Entity (Subtype_Mark (S));
20630 -- Explicit subtype declaration case
20632 if Nkind (P) = N_Subtype_Declaration then
20633 Def_Id := Defining_Identifier (P);
20635 -- Explicit derived type definition case
20637 elsif Nkind (P) = N_Derived_Type_Definition then
20638 Def_Id := Defining_Identifier (Parent (P));
20640 -- Implicit case, the Def_Id must be created as an implicit type.
20641 -- The one exception arises in the case of concurrent types, array
20642 -- and access types, where other subsidiary implicit types may be
20643 -- created and must appear before the main implicit type. In these
20644 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
20645 -- has not yet been called to create Def_Id.
20648 if Is_Array_Type (Subtype_Mark_Id)
20649 or else Is_Concurrent_Type (Subtype_Mark_Id)
20650 or else Is_Access_Type (Subtype_Mark_Id)
20654 -- For the other cases, we create a new unattached Itype,
20655 -- and set the indication to ensure it gets attached later.
20659 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20663 -- If the kind of constraint is invalid for this kind of type,
20664 -- then give an error, and then pretend no constraint was given.
20666 if not Is_Valid_Constraint_Kind
20667 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
20670 ("incorrect constraint for this kind of type", Constraint (S));
20672 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
20674 -- Set Ekind of orphan itype, to prevent cascaded errors
20676 if Present (Def_Id) then
20677 Set_Ekind (Def_Id, Ekind (Any_Type));
20680 -- Make recursive call, having got rid of the bogus constraint
20682 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
20685 -- Remaining processing depends on type. Select on Base_Type kind to
20686 -- ensure getting to the concrete type kind in the case of a private
20687 -- subtype (needed when only doing semantic analysis).
20689 case Ekind (Base_Type (Subtype_Mark_Id)) is
20690 when Access_Kind =>
20692 -- If this is a constraint on a class-wide type, discard it.
20693 -- There is currently no way to express a partial discriminant
20694 -- constraint on a type with unknown discriminants. This is
20695 -- a pathology that the ACATS wisely decides not to test.
20697 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
20698 if Comes_From_Source (S) then
20700 ("constraint on class-wide type ignored??",
20704 if Nkind (P) = N_Subtype_Declaration then
20705 Set_Subtype_Indication (P,
20706 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
20709 return Subtype_Mark_Id;
20712 Constrain_Access (Def_Id, S, Related_Nod);
20715 and then Is_Itype (Designated_Type (Def_Id))
20716 and then Nkind (Related_Nod) = N_Subtype_Declaration
20717 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
20719 Build_Itype_Reference
20720 (Designated_Type (Def_Id), Related_Nod);
20724 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
20726 when Decimal_Fixed_Point_Kind =>
20727 Constrain_Decimal (Def_Id, S);
20729 when Enumeration_Kind =>
20730 Constrain_Enumeration (Def_Id, S);
20731 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20733 when Ordinary_Fixed_Point_Kind =>
20734 Constrain_Ordinary_Fixed (Def_Id, S);
20737 Constrain_Float (Def_Id, S);
20739 when Integer_Kind =>
20740 Constrain_Integer (Def_Id, S);
20741 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
20743 when E_Record_Type |
20746 E_Incomplete_Type =>
20747 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20749 if Ekind (Def_Id) = E_Incomplete_Type then
20750 Set_Private_Dependents (Def_Id, New_Elmt_List);
20753 when Private_Kind =>
20754 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
20755 Set_Private_Dependents (Def_Id, New_Elmt_List);
20757 -- In case of an invalid constraint prevent further processing
20758 -- since the type constructed is missing expected fields.
20760 if Etype (Def_Id) = Any_Type then
20764 -- If the full view is that of a task with discriminants,
20765 -- we must constrain both the concurrent type and its
20766 -- corresponding record type. Otherwise we will just propagate
20767 -- the constraint to the full view, if available.
20769 if Present (Full_View (Subtype_Mark_Id))
20770 and then Has_Discriminants (Subtype_Mark_Id)
20771 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
20774 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
20776 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
20777 Constrain_Concurrent (Full_View_Id, S,
20778 Related_Nod, Related_Id, Suffix);
20779 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
20780 Set_Full_View (Def_Id, Full_View_Id);
20782 -- Introduce an explicit reference to the private subtype,
20783 -- to prevent scope anomalies in gigi if first use appears
20784 -- in a nested context, e.g. a later function body.
20785 -- Should this be generated in other contexts than a full
20786 -- type declaration?
20788 if Is_Itype (Def_Id)
20790 Nkind (Parent (P)) = N_Full_Type_Declaration
20792 Build_Itype_Reference (Def_Id, Parent (P));
20796 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
20799 when Concurrent_Kind =>
20800 Constrain_Concurrent (Def_Id, S,
20801 Related_Nod, Related_Id, Suffix);
20804 Error_Msg_N ("invalid subtype mark in subtype indication", S);
20807 -- Size and Convention are always inherited from the base type
20809 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
20810 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
20814 end Process_Subtype;
20816 --------------------------------------------
20817 -- Propagate_Default_Init_Cond_Attributes --
20818 --------------------------------------------
20820 procedure Propagate_Default_Init_Cond_Attributes
20821 (From_Typ : Entity_Id;
20822 To_Typ : Entity_Id;
20823 Parent_To_Derivation : Boolean := False;
20824 Private_To_Full_View : Boolean := False)
20826 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id);
20827 -- Remove the default initial procedure (if any) from the rep chain of
20830 ----------------------------------------
20831 -- Remove_Default_Init_Cond_Procedure --
20832 ----------------------------------------
20834 procedure Remove_Default_Init_Cond_Procedure (Typ : Entity_Id) is
20835 Found : Boolean := False;
20841 Subp := Subprograms_For_Type (Typ);
20842 while Present (Subp) loop
20843 if Is_Default_Init_Cond_Procedure (Subp) then
20849 Subp := Subprograms_For_Type (Subp);
20853 Set_Subprograms_For_Type (Prev, Subprograms_For_Type (Subp));
20854 Set_Subprograms_For_Type (Subp, Empty);
20856 end Remove_Default_Init_Cond_Procedure;
20860 Inherit_Procedure : Boolean := False;
20862 -- Start of processing for Propagate_Default_Init_Cond_Attributes
20865 if Has_Default_Init_Cond (From_Typ) then
20867 -- A derived type inherits the attributes from its parent type
20869 if Parent_To_Derivation then
20870 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20872 -- A full view shares the attributes with its private view
20875 Set_Has_Default_Init_Cond (To_Typ);
20878 Inherit_Procedure := True;
20880 -- Due to the order of expansion, a derived private type is processed
20881 -- by two routines which both attempt to set the attributes related
20882 -- to pragma Default_Initial_Condition - Build_Derived_Type and then
20883 -- Process_Full_View.
20886 -- type Parent_Typ is private
20887 -- with Default_Initial_Condition ...;
20889 -- type Parent_Typ is ...;
20892 -- with Pack; use Pack;
20893 -- package Pack_2 is
20894 -- type Deriv_Typ is private
20895 -- with Default_Initial_Condition ...;
20897 -- type Deriv_Typ is new Parent_Typ;
20900 -- When Build_Derived_Type operates, it sets the attributes on the
20901 -- full view without taking into account that the private view may
20902 -- define its own default initial condition procedure. This becomes
20903 -- apparent in Process_Full_View which must undo some of the work by
20904 -- Build_Derived_Type and propagate the attributes from the private
20905 -- to the full view.
20907 if Private_To_Full_View then
20908 Set_Has_Inherited_Default_Init_Cond (To_Typ, False);
20909 Remove_Default_Init_Cond_Procedure (To_Typ);
20912 -- A type must inherit the default initial condition procedure from a
20913 -- parent type when the parent itself is inheriting the procedure or
20914 -- when it is defining one. This circuitry is also used when dealing
20915 -- with the private / full view of a type.
20917 elsif Has_Inherited_Default_Init_Cond (From_Typ)
20918 or (Parent_To_Derivation
20919 and Present (Get_Pragma
20920 (From_Typ, Pragma_Default_Initial_Condition)))
20922 Set_Has_Inherited_Default_Init_Cond (To_Typ);
20923 Inherit_Procedure := True;
20926 if Inherit_Procedure
20927 and then No (Default_Init_Cond_Procedure (To_Typ))
20929 Set_Default_Init_Cond_Procedure
20930 (To_Typ, Default_Init_Cond_Procedure (From_Typ));
20932 end Propagate_Default_Init_Cond_Attributes;
20934 -----------------------------
20935 -- Record_Type_Declaration --
20936 -----------------------------
20938 procedure Record_Type_Declaration
20943 Def : constant Node_Id := Type_Definition (N);
20944 Is_Tagged : Boolean;
20945 Tag_Comp : Entity_Id;
20948 -- These flags must be initialized before calling Process_Discriminants
20949 -- because this routine makes use of them.
20951 Set_Ekind (T, E_Record_Type);
20953 Init_Size_Align (T);
20954 Set_Interfaces (T, No_Elist);
20955 Set_Stored_Constraint (T, No_Elist);
20956 Set_Default_SSO (T);
20960 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
20961 if Limited_Present (Def) then
20962 Check_SPARK_05_Restriction ("limited is not allowed", N);
20965 if Abstract_Present (Def) then
20966 Check_SPARK_05_Restriction ("abstract is not allowed", N);
20969 -- The flag Is_Tagged_Type might have already been set by
20970 -- Find_Type_Name if it detected an error for declaration T. This
20971 -- arises in the case of private tagged types where the full view
20972 -- omits the word tagged.
20975 Tagged_Present (Def)
20976 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
20978 Set_Is_Limited_Record (T, Limited_Present (Def));
20981 Set_Is_Tagged_Type (T, True);
20982 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
20985 -- Type is abstract if full declaration carries keyword, or if
20986 -- previous partial view did.
20988 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
20989 or else Abstract_Present (Def));
20992 Check_SPARK_05_Restriction ("interface is not allowed", N);
20995 Analyze_Interface_Declaration (T, Def);
20997 if Present (Discriminant_Specifications (N)) then
20999 ("interface types cannot have discriminants",
21000 Defining_Identifier
21001 (First (Discriminant_Specifications (N))));
21005 -- First pass: if there are self-referential access components,
21006 -- create the required anonymous access type declarations, and if
21007 -- need be an incomplete type declaration for T itself.
21009 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21011 if Ada_Version >= Ada_2005
21012 and then Present (Interface_List (Def))
21014 Check_Interfaces (N, Def);
21017 Ifaces_List : Elist_Id;
21020 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21021 -- already in the parents.
21025 Ifaces_List => Ifaces_List,
21026 Exclude_Parents => True);
21028 Set_Interfaces (T, Ifaces_List);
21032 -- Records constitute a scope for the component declarations within.
21033 -- The scope is created prior to the processing of these declarations.
21034 -- Discriminants are processed first, so that they are visible when
21035 -- processing the other components. The Ekind of the record type itself
21036 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21038 -- Enter record scope
21042 -- If an incomplete or private type declaration was already given for
21043 -- the type, then this scope already exists, and the discriminants have
21044 -- been declared within. We must verify that the full declaration
21045 -- matches the incomplete one.
21047 Check_Or_Process_Discriminants (N, T, Prev);
21049 Set_Is_Constrained (T, not Has_Discriminants (T));
21050 Set_Has_Delayed_Freeze (T, True);
21052 -- For tagged types add a manually analyzed component corresponding
21053 -- to the component _tag, the corresponding piece of tree will be
21054 -- expanded as part of the freezing actions if it is not a CPP_Class.
21058 -- Do not add the tag unless we are in expansion mode
21060 if Expander_Active then
21061 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21062 Enter_Name (Tag_Comp);
21064 Set_Ekind (Tag_Comp, E_Component);
21065 Set_Is_Tag (Tag_Comp);
21066 Set_Is_Aliased (Tag_Comp);
21067 Set_Etype (Tag_Comp, RTE (RE_Tag));
21068 Set_DT_Entry_Count (Tag_Comp, No_Uint);
21069 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
21070 Init_Component_Location (Tag_Comp);
21072 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
21073 -- implemented interfaces.
21075 if Has_Interfaces (T) then
21076 Add_Interface_Tag_Components (N, T);
21080 Make_Class_Wide_Type (T);
21081 Set_Direct_Primitive_Operations (T, New_Elmt_List);
21084 -- We must suppress range checks when processing record components in
21085 -- the presence of discriminants, since we don't want spurious checks to
21086 -- be generated during their analysis, but Suppress_Range_Checks flags
21087 -- must be reset the after processing the record definition.
21089 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
21090 -- couldn't we just use the normal range check suppression method here.
21091 -- That would seem cleaner ???
21093 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
21094 Set_Kill_Range_Checks (T, True);
21095 Record_Type_Definition (Def, Prev);
21096 Set_Kill_Range_Checks (T, False);
21098 Record_Type_Definition (Def, Prev);
21101 -- Exit from record scope
21105 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
21106 -- the implemented interfaces and associate them an aliased entity.
21109 and then not Is_Empty_List (Interface_List (Def))
21111 Derive_Progenitor_Subprograms (T, T);
21114 Check_Function_Writable_Actuals (N);
21115 end Record_Type_Declaration;
21117 ----------------------------
21118 -- Record_Type_Definition --
21119 ----------------------------
21121 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
21122 Component : Entity_Id;
21123 Ctrl_Components : Boolean := False;
21124 Final_Storage_Only : Boolean;
21128 if Ekind (Prev_T) = E_Incomplete_Type then
21129 T := Full_View (Prev_T);
21134 -- In SPARK, tagged types and type extensions may only be declared in
21135 -- the specification of library unit packages.
21137 if Present (Def) and then Is_Tagged_Type (T) then
21143 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
21144 Typ := Parent (Def);
21147 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
21148 Typ := Parent (Parent (Def));
21151 Ctxt := Parent (Typ);
21153 if Nkind (Ctxt) = N_Package_Body
21154 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
21156 Check_SPARK_05_Restriction
21157 ("type should be defined in package specification", Typ);
21159 elsif Nkind (Ctxt) /= N_Package_Specification
21160 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
21162 Check_SPARK_05_Restriction
21163 ("type should be defined in library unit package", Typ);
21168 Final_Storage_Only := not Is_Controlled (T);
21170 -- Ada 2005: Check whether an explicit Limited is present in a derived
21171 -- type declaration.
21173 if Nkind (Parent (Def)) = N_Derived_Type_Definition
21174 and then Limited_Present (Parent (Def))
21176 Set_Is_Limited_Record (T);
21179 -- If the component list of a record type is defined by the reserved
21180 -- word null and there is no discriminant part, then the record type has
21181 -- no components and all records of the type are null records (RM 3.7)
21182 -- This procedure is also called to process the extension part of a
21183 -- record extension, in which case the current scope may have inherited
21187 or else No (Component_List (Def))
21188 or else Null_Present (Component_List (Def))
21190 if not Is_Tagged_Type (T) then
21191 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
21195 Analyze_Declarations (Component_Items (Component_List (Def)));
21197 if Present (Variant_Part (Component_List (Def))) then
21198 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
21199 Analyze (Variant_Part (Component_List (Def)));
21203 -- After completing the semantic analysis of the record definition,
21204 -- record components, both new and inherited, are accessible. Set their
21205 -- kind accordingly. Exclude malformed itypes from illegal declarations,
21206 -- whose Ekind may be void.
21208 Component := First_Entity (Current_Scope);
21209 while Present (Component) loop
21210 if Ekind (Component) = E_Void
21211 and then not Is_Itype (Component)
21213 Set_Ekind (Component, E_Component);
21214 Init_Component_Location (Component);
21217 if Has_Task (Etype (Component)) then
21221 if Has_Protected (Etype (Component)) then
21222 Set_Has_Protected (T);
21225 if Ekind (Component) /= E_Component then
21228 -- Do not set Has_Controlled_Component on a class-wide equivalent
21229 -- type. See Make_CW_Equivalent_Type.
21231 elsif not Is_Class_Wide_Equivalent_Type (T)
21232 and then (Has_Controlled_Component (Etype (Component))
21233 or else (Chars (Component) /= Name_uParent
21234 and then Is_Controlled (Etype (Component))))
21236 Set_Has_Controlled_Component (T, True);
21237 Final_Storage_Only :=
21239 and then Finalize_Storage_Only (Etype (Component));
21240 Ctrl_Components := True;
21243 Next_Entity (Component);
21246 -- A Type is Finalize_Storage_Only only if all its controlled components
21249 if Ctrl_Components then
21250 Set_Finalize_Storage_Only (T, Final_Storage_Only);
21253 -- Place reference to end record on the proper entity, which may
21254 -- be a partial view.
21256 if Present (Def) then
21257 Process_End_Label (Def, 'e', Prev_T);
21259 end Record_Type_Definition;
21261 ------------------------
21262 -- Replace_Components --
21263 ------------------------
21265 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
21266 function Process (N : Node_Id) return Traverse_Result;
21272 function Process (N : Node_Id) return Traverse_Result is
21276 if Nkind (N) = N_Discriminant_Specification then
21277 Comp := First_Discriminant (Typ);
21278 while Present (Comp) loop
21279 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21280 Set_Defining_Identifier (N, Comp);
21284 Next_Discriminant (Comp);
21287 elsif Nkind (N) = N_Component_Declaration then
21288 Comp := First_Component (Typ);
21289 while Present (Comp) loop
21290 if Chars (Comp) = Chars (Defining_Identifier (N)) then
21291 Set_Defining_Identifier (N, Comp);
21295 Next_Component (Comp);
21302 procedure Replace is new Traverse_Proc (Process);
21304 -- Start of processing for Replace_Components
21308 end Replace_Components;
21310 -------------------------------
21311 -- Set_Completion_Referenced --
21312 -------------------------------
21314 procedure Set_Completion_Referenced (E : Entity_Id) is
21316 -- If in main unit, mark entity that is a completion as referenced,
21317 -- warnings go on the partial view when needed.
21319 if In_Extended_Main_Source_Unit (E) then
21320 Set_Referenced (E);
21322 end Set_Completion_Referenced;
21324 ---------------------
21325 -- Set_Default_SSO --
21326 ---------------------
21328 procedure Set_Default_SSO (T : Entity_Id) is
21330 case Opt.Default_SSO is
21334 Set_SSO_Set_Low_By_Default (T, True);
21336 Set_SSO_Set_High_By_Default (T, True);
21338 raise Program_Error;
21340 end Set_Default_SSO;
21342 ---------------------
21343 -- Set_Fixed_Range --
21344 ---------------------
21346 -- The range for fixed-point types is complicated by the fact that we
21347 -- do not know the exact end points at the time of the declaration. This
21348 -- is true for three reasons:
21350 -- A size clause may affect the fudging of the end-points.
21351 -- A small clause may affect the values of the end-points.
21352 -- We try to include the end-points if it does not affect the size.
21354 -- This means that the actual end-points must be established at the
21355 -- point when the type is frozen. Meanwhile, we first narrow the range
21356 -- as permitted (so that it will fit if necessary in a small specified
21357 -- size), and then build a range subtree with these narrowed bounds.
21358 -- Set_Fixed_Range constructs the range from real literal values, and
21359 -- sets the range as the Scalar_Range of the given fixed-point type entity.
21361 -- The parent of this range is set to point to the entity so that it is
21362 -- properly hooked into the tree (unlike normal Scalar_Range entries for
21363 -- other scalar types, which are just pointers to the range in the
21364 -- original tree, this would otherwise be an orphan).
21366 -- The tree is left unanalyzed. When the type is frozen, the processing
21367 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
21368 -- analyzed, and uses this as an indication that it should complete
21369 -- work on the range (it will know the final small and size values).
21371 procedure Set_Fixed_Range
21377 S : constant Node_Id :=
21379 Low_Bound => Make_Real_Literal (Loc, Lo),
21380 High_Bound => Make_Real_Literal (Loc, Hi));
21382 Set_Scalar_Range (E, S);
21385 -- Before the freeze point, the bounds of a fixed point are universal
21386 -- and carry the corresponding type.
21388 Set_Etype (Low_Bound (S), Universal_Real);
21389 Set_Etype (High_Bound (S), Universal_Real);
21390 end Set_Fixed_Range;
21392 ----------------------------------
21393 -- Set_Scalar_Range_For_Subtype --
21394 ----------------------------------
21396 procedure Set_Scalar_Range_For_Subtype
21397 (Def_Id : Entity_Id;
21401 Kind : constant Entity_Kind := Ekind (Def_Id);
21404 -- Defend against previous error
21406 if Nkind (R) = N_Error then
21410 Set_Scalar_Range (Def_Id, R);
21412 -- We need to link the range into the tree before resolving it so
21413 -- that types that are referenced, including importantly the subtype
21414 -- itself, are properly frozen (Freeze_Expression requires that the
21415 -- expression be properly linked into the tree). Of course if it is
21416 -- already linked in, then we do not disturb the current link.
21418 if No (Parent (R)) then
21419 Set_Parent (R, Def_Id);
21422 -- Reset the kind of the subtype during analysis of the range, to
21423 -- catch possible premature use in the bounds themselves.
21425 Set_Ekind (Def_Id, E_Void);
21426 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
21427 Set_Ekind (Def_Id, Kind);
21428 end Set_Scalar_Range_For_Subtype;
21430 --------------------------------------------------------
21431 -- Set_Stored_Constraint_From_Discriminant_Constraint --
21432 --------------------------------------------------------
21434 procedure Set_Stored_Constraint_From_Discriminant_Constraint
21438 -- Make sure set if encountered during Expand_To_Stored_Constraint
21440 Set_Stored_Constraint (E, No_Elist);
21442 -- Give it the right value
21444 if Is_Constrained (E) and then Has_Discriminants (E) then
21445 Set_Stored_Constraint (E,
21446 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
21448 end Set_Stored_Constraint_From_Discriminant_Constraint;
21450 -------------------------------------
21451 -- Signed_Integer_Type_Declaration --
21452 -------------------------------------
21454 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
21455 Implicit_Base : Entity_Id;
21456 Base_Typ : Entity_Id;
21459 Errs : Boolean := False;
21463 function Can_Derive_From (E : Entity_Id) return Boolean;
21464 -- Determine whether given bounds allow derivation from specified type
21466 procedure Check_Bound (Expr : Node_Id);
21467 -- Check bound to make sure it is integral and static. If not, post
21468 -- appropriate error message and set Errs flag
21470 ---------------------
21471 -- Can_Derive_From --
21472 ---------------------
21474 -- Note we check both bounds against both end values, to deal with
21475 -- strange types like ones with a range of 0 .. -12341234.
21477 function Can_Derive_From (E : Entity_Id) return Boolean is
21478 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
21479 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
21481 return Lo <= Lo_Val and then Lo_Val <= Hi
21483 Lo <= Hi_Val and then Hi_Val <= Hi;
21484 end Can_Derive_From;
21490 procedure Check_Bound (Expr : Node_Id) is
21492 -- If a range constraint is used as an integer type definition, each
21493 -- bound of the range must be defined by a static expression of some
21494 -- integer type, but the two bounds need not have the same integer
21495 -- type (Negative bounds are allowed.) (RM 3.5.4)
21497 if not Is_Integer_Type (Etype (Expr)) then
21499 ("integer type definition bounds must be of integer type", Expr);
21502 elsif not Is_OK_Static_Expression (Expr) then
21503 Flag_Non_Static_Expr
21504 ("non-static expression used for integer type bound!", Expr);
21507 -- The bounds are folded into literals, and we set their type to be
21508 -- universal, to avoid typing difficulties: we cannot set the type
21509 -- of the literal to the new type, because this would be a forward
21510 -- reference for the back end, and if the original type is user-
21511 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
21514 if Is_Entity_Name (Expr) then
21515 Fold_Uint (Expr, Expr_Value (Expr), True);
21518 Set_Etype (Expr, Universal_Integer);
21522 -- Start of processing for Signed_Integer_Type_Declaration
21525 -- Create an anonymous base type
21528 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
21530 -- Analyze and check the bounds, they can be of any integer type
21532 Lo := Low_Bound (Def);
21533 Hi := High_Bound (Def);
21535 -- Arbitrarily use Integer as the type if either bound had an error
21537 if Hi = Error or else Lo = Error then
21538 Base_Typ := Any_Integer;
21539 Set_Error_Posted (T, True);
21541 -- Here both bounds are OK expressions
21544 Analyze_And_Resolve (Lo, Any_Integer);
21545 Analyze_And_Resolve (Hi, Any_Integer);
21551 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21552 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21555 -- Find type to derive from
21557 Lo_Val := Expr_Value (Lo);
21558 Hi_Val := Expr_Value (Hi);
21560 if Can_Derive_From (Standard_Short_Short_Integer) then
21561 Base_Typ := Base_Type (Standard_Short_Short_Integer);
21563 elsif Can_Derive_From (Standard_Short_Integer) then
21564 Base_Typ := Base_Type (Standard_Short_Integer);
21566 elsif Can_Derive_From (Standard_Integer) then
21567 Base_Typ := Base_Type (Standard_Integer);
21569 elsif Can_Derive_From (Standard_Long_Integer) then
21570 Base_Typ := Base_Type (Standard_Long_Integer);
21572 elsif Can_Derive_From (Standard_Long_Long_Integer) then
21573 Check_Restriction (No_Long_Long_Integers, Def);
21574 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21577 Base_Typ := Base_Type (Standard_Long_Long_Integer);
21578 Error_Msg_N ("integer type definition bounds out of range", Def);
21579 Hi := Type_High_Bound (Standard_Long_Long_Integer);
21580 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
21584 -- Complete both implicit base and declared first subtype entities. The
21585 -- inheritance of the rep item chain ensures that SPARK-related pragmas
21586 -- are not clobbered when the signed integer type acts as a full view of
21589 Set_Etype (Implicit_Base, Base_Typ);
21590 Set_Size_Info (Implicit_Base, Base_Typ);
21591 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
21592 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
21593 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
21595 Set_Ekind (T, E_Signed_Integer_Subtype);
21596 Set_Etype (T, Implicit_Base);
21597 Set_Size_Info (T, Implicit_Base);
21598 Inherit_Rep_Item_Chain (T, Implicit_Base);
21599 Set_Scalar_Range (T, Def);
21600 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
21601 Set_Is_Constrained (T);
21602 end Signed_Integer_Type_Declaration;