1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2020, Free Software Foundation, Inc. --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
17 -- for more details. You should have received a copy of the GNU General --
18 -- Public License distributed with GNAT; see file COPYING3. If not, go to --
19 -- http://www.gnu.org/licenses for a complete copy of the license. --
21 -- GNAT was originally developed by the GNAT team at New York University. --
22 -- Extensive contributions were provided by Ada Core Technologies Inc. --
24 ------------------------------------------------------------------------------
26 with Aspects; use Aspects;
27 with Atree; use Atree;
28 with Checks; use Checks;
29 with Contracts; use Contracts;
30 with Debug; use Debug;
31 with Elists; use Elists;
32 with Einfo; use Einfo;
33 with Errout; use Errout;
34 with Eval_Fat; use Eval_Fat;
35 with Exp_Ch3; use Exp_Ch3;
36 with Exp_Ch9; use Exp_Ch9;
37 with Exp_Disp; use Exp_Disp;
38 with Exp_Dist; use Exp_Dist;
39 with Exp_Tss; use Exp_Tss;
40 with Exp_Util; use Exp_Util;
41 with Freeze; use Freeze;
42 with Ghost; use Ghost;
43 with Itypes; use Itypes;
44 with Layout; use Layout;
46 with Lib.Xref; use Lib.Xref;
47 with Namet; use Namet;
48 with Nmake; use Nmake;
50 with Restrict; use Restrict;
51 with Rident; use Rident;
52 with Rtsfind; use Rtsfind;
54 with Sem_Aux; use Sem_Aux;
55 with Sem_Case; use Sem_Case;
56 with Sem_Cat; use Sem_Cat;
57 with Sem_Ch6; use Sem_Ch6;
58 with Sem_Ch7; use Sem_Ch7;
59 with Sem_Ch8; use Sem_Ch8;
60 with Sem_Ch13; use Sem_Ch13;
61 with Sem_Dim; use Sem_Dim;
62 with Sem_Disp; use Sem_Disp;
63 with Sem_Dist; use Sem_Dist;
64 with Sem_Elab; use Sem_Elab;
65 with Sem_Elim; use Sem_Elim;
66 with Sem_Eval; use Sem_Eval;
67 with Sem_Mech; use Sem_Mech;
68 with Sem_Res; use Sem_Res;
69 with Sem_Smem; use Sem_Smem;
70 with Sem_Type; use Sem_Type;
71 with Sem_Util; use Sem_Util;
72 with Sem_Warn; use Sem_Warn;
73 with Stand; use Stand;
74 with Sinfo; use Sinfo;
75 with Sinput; use Sinput;
76 with Snames; use Snames;
77 with Targparm; use Targparm;
78 with Tbuild; use Tbuild;
79 with Ttypes; use Ttypes;
80 with Uintp; use Uintp;
81 with Urealp; use Urealp;
83 package body Sem_Ch3 is
85 -----------------------
86 -- Local Subprograms --
87 -----------------------
89 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id);
90 -- Ada 2005 (AI-251): Add the tag components corresponding to all the
91 -- abstract interface types implemented by a record type or a derived
94 procedure Build_Derived_Type
96 Parent_Type : Entity_Id;
97 Derived_Type : Entity_Id;
98 Is_Completion : Boolean;
99 Derive_Subps : Boolean := True);
100 -- Create and decorate a Derived_Type given the Parent_Type entity. N is
101 -- the N_Full_Type_Declaration node containing the derived type definition.
102 -- Parent_Type is the entity for the parent type in the derived type
103 -- definition and Derived_Type the actual derived type. Is_Completion must
104 -- be set to False if Derived_Type is the N_Defining_Identifier node in N
105 -- (i.e. Derived_Type = Defining_Identifier (N)). In this case N is not the
106 -- completion of a private type declaration. If Is_Completion is set to
107 -- True, N is the completion of a private type declaration and Derived_Type
108 -- is different from the defining identifier inside N (i.e. Derived_Type /=
109 -- Defining_Identifier (N)). Derive_Subps indicates whether the parent
110 -- subprograms should be derived. The only case where this parameter is
111 -- False is when Build_Derived_Type is recursively called to process an
112 -- implicit derived full type for a type derived from a private type (in
113 -- that case the subprograms must only be derived for the private view of
116 -- ??? These flags need a bit of re-examination and re-documentation:
117 -- ??? are they both necessary (both seem related to the recursion)?
119 procedure Build_Derived_Access_Type
121 Parent_Type : Entity_Id;
122 Derived_Type : Entity_Id);
123 -- Subsidiary procedure to Build_Derived_Type. For a derived access type,
124 -- create an implicit base if the parent type is constrained or if the
125 -- subtype indication has a constraint.
127 procedure Build_Derived_Array_Type
129 Parent_Type : Entity_Id;
130 Derived_Type : Entity_Id);
131 -- Subsidiary procedure to Build_Derived_Type. For a derived array type,
132 -- create an implicit base if the parent type is constrained or if the
133 -- subtype indication has a constraint.
135 procedure Build_Derived_Concurrent_Type
137 Parent_Type : Entity_Id;
138 Derived_Type : Entity_Id);
139 -- Subsidiary procedure to Build_Derived_Type. For a derived task or
140 -- protected type, inherit entries and protected subprograms, check
141 -- legality of discriminant constraints if any.
143 procedure Build_Derived_Enumeration_Type
145 Parent_Type : Entity_Id;
146 Derived_Type : Entity_Id);
147 -- Subsidiary procedure to Build_Derived_Type. For a derived enumeration
148 -- type, we must create a new list of literals. Types derived from
149 -- Character and [Wide_]Wide_Character are special-cased.
151 procedure Build_Derived_Numeric_Type
153 Parent_Type : Entity_Id;
154 Derived_Type : Entity_Id);
155 -- Subsidiary procedure to Build_Derived_Type. For numeric types, create
156 -- an anonymous base type, and propagate constraint to subtype if needed.
158 procedure Build_Derived_Private_Type
160 Parent_Type : Entity_Id;
161 Derived_Type : Entity_Id;
162 Is_Completion : Boolean;
163 Derive_Subps : Boolean := True);
164 -- Subsidiary procedure to Build_Derived_Type. This procedure is complex
165 -- because the parent may or may not have a completion, and the derivation
166 -- may itself be a completion.
168 procedure Build_Derived_Record_Type
170 Parent_Type : Entity_Id;
171 Derived_Type : Entity_Id;
172 Derive_Subps : Boolean := True);
173 -- Subsidiary procedure used for tagged and untagged record types
174 -- by Build_Derived_Type and Analyze_Private_Extension_Declaration.
175 -- All parameters are as in Build_Derived_Type except that N, in
176 -- addition to being an N_Full_Type_Declaration node, can also be an
177 -- N_Private_Extension_Declaration node. See the definition of this routine
178 -- for much more info. Derive_Subps indicates whether subprograms should be
179 -- derived from the parent type. The only case where Derive_Subps is False
180 -- is for an implicit derived full type for a type derived from a private
181 -- type (see Build_Derived_Type).
183 procedure Build_Discriminal (Discrim : Entity_Id);
184 -- Create the discriminal corresponding to discriminant Discrim, that is
185 -- the parameter corresponding to Discrim to be used in initialization
186 -- procedures for the type where Discrim is a discriminant. Discriminals
187 -- are not used during semantic analysis, and are not fully defined
188 -- entities until expansion. Thus they are not given a scope until
189 -- initialization procedures are built.
191 function Build_Discriminant_Constraints
194 Derived_Def : Boolean := False) return Elist_Id;
195 -- Validate discriminant constraints and return the list of the constraints
196 -- in order of discriminant declarations, where T is the discriminated
197 -- unconstrained type. Def is the N_Subtype_Indication node where the
198 -- discriminants constraints for T are specified. Derived_Def is True
199 -- when building the discriminant constraints in a derived type definition
200 -- of the form "type D (...) is new T (xxx)". In this case T is the parent
201 -- type and Def is the constraint "(xxx)" on T and this routine sets the
202 -- Corresponding_Discriminant field of the discriminants in the derived
203 -- type D to point to the corresponding discriminants in the parent type T.
205 procedure Build_Discriminated_Subtype
209 Related_Nod : Node_Id;
210 For_Access : Boolean := False);
211 -- Subsidiary procedure to Constrain_Discriminated_Type and to
212 -- Process_Incomplete_Dependents. Given
214 -- T (a possibly discriminated base type)
215 -- Def_Id (a very partially built subtype for T),
217 -- the call completes Def_Id to be the appropriate E_*_Subtype.
219 -- The Elist is the list of discriminant constraints if any (it is set
220 -- to No_Elist if T is not a discriminated type, and to an empty list if
221 -- T has discriminants but there are no discriminant constraints). The
222 -- Related_Nod is the same as Decl_Node in Create_Constrained_Components.
223 -- The For_Access says whether or not this subtype is really constraining
226 function Build_Scalar_Bound
229 Der_T : Entity_Id) return Node_Id;
230 -- The bounds of a derived scalar type are conversions of the bounds of
231 -- the parent type. Optimize the representation if the bounds are literals.
232 -- Needs a more complete spec--what are the parameters exactly, and what
233 -- exactly is the returned value, and how is Bound affected???
235 procedure Check_Access_Discriminant_Requires_Limited
238 -- Check the restriction that the type to which an access discriminant
239 -- belongs must be a concurrent type or a descendant of a type with
240 -- the reserved word 'limited' in its declaration.
242 procedure Check_Anonymous_Access_Components
246 Comp_List : Node_Id);
247 -- Ada 2005 AI-382: an access component in a record definition can refer to
248 -- the enclosing record, in which case it denotes the type itself, and not
249 -- the current instance of the type. We create an anonymous access type for
250 -- the component, and flag it as an access to a component, so accessibility
251 -- checks are properly performed on it. The declaration of the access type
252 -- is placed ahead of that of the record to prevent order-of-elaboration
253 -- circularity issues in Gigi. We create an incomplete type for the record
254 -- declaration, which is the designated type of the anonymous access.
256 procedure Check_Delta_Expression (E : Node_Id);
257 -- Check that the expression represented by E is suitable for use as a
258 -- delta expression, i.e. it is of real type and is static.
260 procedure Check_Digits_Expression (E : Node_Id);
261 -- Check that the expression represented by E is suitable for use as a
262 -- digits expression, i.e. it is of integer type, positive and static.
264 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
265 -- Validate the initialization of an object declaration. T is the required
266 -- type, and Exp is the initialization expression.
268 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
269 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
271 procedure Check_Or_Process_Discriminants
274 Prev : Entity_Id := Empty);
275 -- If N is the full declaration of the completion T of an incomplete or
276 -- private type, check its discriminants (which are already known to be
277 -- conformant with those of the partial view, see Find_Type_Name),
278 -- otherwise process them. Prev is the entity of the partial declaration,
281 procedure Check_Real_Bound (Bound : Node_Id);
282 -- Check given bound for being of real type and static. If not, post an
283 -- appropriate message, and rewrite the bound with the real literal zero.
285 procedure Constant_Redeclaration
289 -- Various checks on legality of full declaration of deferred constant.
290 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
291 -- node. The caller has not yet set any attributes of this entity.
293 function Contain_Interface
295 Ifaces : Elist_Id) return Boolean;
296 -- Ada 2005: Determine whether Iface is present in the list Ifaces
298 procedure Convert_Scalar_Bounds
300 Parent_Type : Entity_Id;
301 Derived_Type : Entity_Id;
303 -- For derived scalar types, convert the bounds in the type definition to
304 -- the derived type, and complete their analysis. Given a constraint of the
305 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
306 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
307 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
308 -- subtype are conversions of those bounds to the derived_type, so that
309 -- their typing is consistent.
311 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
312 -- Copies attributes from array base type T2 to array base type T1. Copies
313 -- only attributes that apply to base types, but not subtypes.
315 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
316 -- Copies attributes from array subtype T2 to array subtype T1. Copies
317 -- attributes that apply to both subtypes and base types.
319 procedure Create_Constrained_Components
323 Constraints : Elist_Id);
324 -- Build the list of entities for a constrained discriminated record
325 -- subtype. If a component depends on a discriminant, replace its subtype
326 -- using the discriminant values in the discriminant constraint. Subt
327 -- is the defining identifier for the subtype whose list of constrained
328 -- entities we will create. Decl_Node is the type declaration node where
329 -- we will attach all the itypes created. Typ is the base discriminated
330 -- type for the subtype Subt. Constraints is the list of discriminant
331 -- constraints for Typ.
333 function Constrain_Component_Type
335 Constrained_Typ : Entity_Id;
336 Related_Node : Node_Id;
338 Constraints : Elist_Id) return Entity_Id;
339 -- Given a discriminated base type Typ, a list of discriminant constraints,
340 -- Constraints, for Typ and a component Comp of Typ, create and return the
341 -- type corresponding to Etype (Comp) where all discriminant references
342 -- are replaced with the corresponding constraint. If Etype (Comp) contains
343 -- no discriminant references then it is returned as-is. Constrained_Typ
344 -- is the final constrained subtype to which the constrained component
345 -- belongs. Related_Node is the node where we attach all created itypes.
347 procedure Constrain_Access
348 (Def_Id : in out Entity_Id;
350 Related_Nod : Node_Id);
351 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
352 -- an anonymous type created for a subtype indication. In that case it is
353 -- created in the procedure and attached to Related_Nod.
355 procedure Constrain_Array
356 (Def_Id : in out Entity_Id;
358 Related_Nod : Node_Id;
359 Related_Id : Entity_Id;
361 -- Apply a list of index constraints to an unconstrained array type. The
362 -- first parameter is the entity for the resulting subtype. A value of
363 -- Empty for Def_Id indicates that an implicit type must be created, but
364 -- creation is delayed (and must be done by this procedure) because other
365 -- subsidiary implicit types must be created first (which is why Def_Id
366 -- is an in/out parameter). The second parameter is a subtype indication
367 -- node for the constrained array to be created (e.g. something of the
368 -- form string (1 .. 10)). Related_Nod gives the place where this type
369 -- has to be inserted in the tree. The Related_Id and Suffix parameters
370 -- are used to build the associated Implicit type name.
372 procedure Constrain_Concurrent
373 (Def_Id : in out Entity_Id;
375 Related_Nod : Node_Id;
376 Related_Id : Entity_Id;
378 -- Apply list of discriminant constraints to an unconstrained concurrent
381 -- SI is the N_Subtype_Indication node containing the constraint and
382 -- the unconstrained type to constrain.
384 -- Def_Id is the entity for the resulting constrained subtype. A value
385 -- of Empty for Def_Id indicates that an implicit type must be created,
386 -- but creation is delayed (and must be done by this procedure) because
387 -- other subsidiary implicit types must be created first (which is why
388 -- Def_Id is an in/out parameter).
390 -- Related_Nod gives the place where this type has to be inserted
393 -- The last two arguments are used to create its external name if needed.
395 function Constrain_Corresponding_Record
396 (Prot_Subt : Entity_Id;
397 Corr_Rec : Entity_Id;
398 Related_Nod : Node_Id) return Entity_Id;
399 -- When constraining a protected type or task type with discriminants,
400 -- constrain the corresponding record with the same discriminant values.
402 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
403 -- Constrain a decimal fixed point type with a digits constraint and/or a
404 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
406 procedure Constrain_Discriminated_Type
409 Related_Nod : Node_Id;
410 For_Access : Boolean := False);
411 -- Process discriminant constraints of composite type. Verify that values
412 -- have been provided for all discriminants, that the original type is
413 -- unconstrained, and that the types of the supplied expressions match
414 -- the discriminant types. The first three parameters are like in routine
415 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
418 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
419 -- Constrain an enumeration type with a range constraint. This is identical
420 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
422 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
423 -- Constrain a floating point type with either a digits constraint
424 -- and/or a range constraint, building a E_Floating_Point_Subtype.
426 procedure Constrain_Index
429 Related_Nod : Node_Id;
430 Related_Id : Entity_Id;
433 -- Process an index constraint S in a constrained array declaration. The
434 -- constraint can be a subtype name, or a range with or without an explicit
435 -- subtype mark. The index is the corresponding index of the unconstrained
436 -- array. The Related_Id and Suffix parameters are used to build the
437 -- associated Implicit type name.
439 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
440 -- Build subtype of a signed or modular integer type
442 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
443 -- Constrain an ordinary fixed point type with a range constraint, and
444 -- build an E_Ordinary_Fixed_Point_Subtype entity.
446 procedure Copy_And_Swap (Priv, Full : Entity_Id);
447 -- Copy the Priv entity into the entity of its full declaration then swap
448 -- the two entities in such a manner that the former private type is now
449 -- seen as a full type.
451 procedure Decimal_Fixed_Point_Type_Declaration
454 -- Create a new decimal fixed point type, and apply the constraint to
455 -- obtain a subtype of this new type.
457 procedure Complete_Private_Subtype
460 Full_Base : Entity_Id;
461 Related_Nod : Node_Id);
462 -- Complete the implicit full view of a private subtype by setting the
463 -- appropriate semantic fields. If the full view of the parent is a record
464 -- type, build constrained components of subtype.
466 procedure Derive_Progenitor_Subprograms
467 (Parent_Type : Entity_Id;
468 Tagged_Type : Entity_Id);
469 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
470 -- operations of progenitors of Tagged_Type, and replace the subsidiary
471 -- subtypes with Tagged_Type, to build the specs of the inherited interface
472 -- primitives. The derived primitives are aliased to those of the
473 -- interface. This routine takes care also of transferring to the full view
474 -- subprograms associated with the partial view of Tagged_Type that cover
475 -- interface primitives.
477 procedure Derived_Standard_Character
479 Parent_Type : Entity_Id;
480 Derived_Type : Entity_Id);
481 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
482 -- derivations from types Standard.Character and Standard.Wide_Character.
484 procedure Derived_Type_Declaration
487 Is_Completion : Boolean);
488 -- Process a derived type declaration. Build_Derived_Type is invoked
489 -- to process the actual derived type definition. Parameters N and
490 -- Is_Completion have the same meaning as in Build_Derived_Type.
491 -- T is the N_Defining_Identifier for the entity defined in the
492 -- N_Full_Type_Declaration node N, that is T is the derived type.
494 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
495 -- Insert each literal in symbol table, as an overloadable identifier. Each
496 -- enumeration type is mapped into a sequence of integers, and each literal
497 -- is defined as a constant with integer value. If any of the literals are
498 -- character literals, the type is a character type, which means that
499 -- strings are legal aggregates for arrays of components of the type.
501 function Expand_To_Stored_Constraint
503 Constraint : Elist_Id) return Elist_Id;
504 -- Given a constraint (i.e. a list of expressions) on the discriminants of
505 -- Typ, expand it into a constraint on the stored discriminants and return
506 -- the new list of expressions constraining the stored discriminants.
508 function Find_Type_Of_Object
510 Related_Nod : Node_Id) return Entity_Id;
511 -- Get type entity for object referenced by Obj_Def, attaching the implicit
512 -- types generated to Related_Nod.
514 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
515 -- Create a new float and apply the constraint to obtain subtype of it
517 function Has_Range_Constraint (N : Node_Id) return Boolean;
518 -- Given an N_Subtype_Indication node N, return True if a range constraint
519 -- is present, either directly, or as part of a digits or delta constraint.
520 -- In addition, a digits constraint in the decimal case returns True, since
521 -- it establishes a default range if no explicit range is present.
523 function Inherit_Components
525 Parent_Base : Entity_Id;
526 Derived_Base : Entity_Id;
528 Inherit_Discr : Boolean;
529 Discs : Elist_Id) return Elist_Id;
530 -- Called from Build_Derived_Record_Type to inherit the components of
531 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
532 -- For more information on derived types and component inheritance please
533 -- consult the comment above the body of Build_Derived_Record_Type.
535 -- N is the original derived type declaration
537 -- Is_Tagged is set if we are dealing with tagged types
539 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
540 -- Parent_Base, otherwise no discriminants are inherited.
542 -- Discs gives the list of constraints that apply to Parent_Base in the
543 -- derived type declaration. If Discs is set to No_Elist, then we have
544 -- the following situation:
546 -- type Parent (D1..Dn : ..) is [tagged] record ...;
547 -- type Derived is new Parent [with ...];
549 -- which gets treated as
551 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
553 -- For untagged types the returned value is an association list. The list
554 -- starts from the association (Parent_Base => Derived_Base), and then it
555 -- contains a sequence of the associations of the form
557 -- (Old_Component => New_Component),
559 -- where Old_Component is the Entity_Id of a component in Parent_Base and
560 -- New_Component is the Entity_Id of the corresponding component in
561 -- Derived_Base. For untagged records, this association list is needed when
562 -- copying the record declaration for the derived base. In the tagged case
563 -- the value returned is irrelevant.
565 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
566 -- Propagate static and dynamic predicate flags from a parent to the
567 -- subtype in a subtype declaration with and without constraints.
569 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
570 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
571 -- Determine whether subprogram Subp is a procedure subject to pragma
572 -- Extensions_Visible with value False and has at least one controlling
573 -- parameter of mode OUT.
575 function Is_Valid_Constraint_Kind
577 Constraint_Kind : Node_Kind) return Boolean;
578 -- Returns True if it is legal to apply the given kind of constraint to the
579 -- given kind of type (index constraint to an array type, for example).
581 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
582 -- Create new modular type. Verify that modulus is in bounds
584 procedure New_Concatenation_Op (Typ : Entity_Id);
585 -- Create an abbreviated declaration for an operator in order to
586 -- materialize concatenation on array types.
588 procedure Ordinary_Fixed_Point_Type_Declaration
591 -- Create a new ordinary fixed point type, and apply the constraint to
592 -- obtain subtype of it.
594 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id);
595 -- Wrapper on Preanalyze_Spec_Expression for default expressions, so that
596 -- In_Default_Expr can be properly adjusted.
598 procedure Prepare_Private_Subtype_Completion
600 Related_Nod : Node_Id);
601 -- Id is a subtype of some private type. Creates the full declaration
602 -- associated with Id whenever possible, i.e. when the full declaration
603 -- of the base type is already known. Records each subtype into
604 -- Private_Dependents of the base type.
606 procedure Process_Incomplete_Dependents
610 -- Process all entities that depend on an incomplete type. There include
611 -- subtypes, subprogram types that mention the incomplete type in their
612 -- profiles, and subprogram with access parameters that designate the
615 -- Inc_T is the defining identifier of an incomplete type declaration, its
616 -- Ekind is E_Incomplete_Type.
618 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
620 -- Full_T is N's defining identifier.
622 -- Subtypes of incomplete types with discriminants are completed when the
623 -- parent type is. This is simpler than private subtypes, because they can
624 -- only appear in the same scope, and there is no need to exchange views.
625 -- Similarly, access_to_subprogram types may have a parameter or a return
626 -- type that is an incomplete type, and that must be replaced with the
629 -- If the full type is tagged, subprogram with access parameters that
630 -- designated the incomplete may be primitive operations of the full type,
631 -- and have to be processed accordingly.
633 procedure Process_Real_Range_Specification (Def : Node_Id);
634 -- Given the type definition for a real type, this procedure processes and
635 -- checks the real range specification of this type definition if one is
636 -- present. If errors are found, error messages are posted, and the
637 -- Real_Range_Specification of Def is reset to Empty.
639 procedure Record_Type_Declaration
643 -- Process a record type declaration (for both untagged and tagged
644 -- records). Parameters T and N are exactly like in procedure
645 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
646 -- for this routine. If this is the completion of an incomplete type
647 -- declaration, Prev is the entity of the incomplete declaration, used for
648 -- cross-referencing. Otherwise Prev = T.
650 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
651 -- This routine is used to process the actual record type definition (both
652 -- for untagged and tagged records). Def is a record type definition node.
653 -- This procedure analyzes the components in this record type definition.
654 -- Prev_T is the entity for the enclosing record type. It is provided so
655 -- that its Has_Task flag can be set if any of the component have Has_Task
656 -- set. If the declaration is the completion of an incomplete type
657 -- declaration, Prev_T is the original incomplete type, whose full view is
660 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id);
661 -- Subsidiary to Build_Derived_Record_Type. For untagged record types, we
662 -- first create the list of components for the derived type from that of
663 -- the parent by means of Inherit_Components and then build a copy of the
664 -- declaration tree of the parent with the help of the mapping returned by
665 -- Inherit_Components, which will for example be used to validate record
666 -- representation clauses given for the derived type. If the parent type
667 -- is private and has discriminants, the ancestor discriminants used in the
668 -- inheritance are that of the private declaration, whereas the ancestor
669 -- discriminants present in the declaration tree of the parent are that of
670 -- the full declaration; as a consequence, the remapping done during the
671 -- copy will leave the references to the ancestor discriminants unchanged
672 -- in the declaration tree and they need to be fixed up. If the derived
673 -- type has a known discriminant part, then the remapping done during the
674 -- copy will only create references to the girder discriminants and they
675 -- need to be replaced with references to the non-girder discriminants.
677 procedure Set_Fixed_Range
682 -- Build a range node with the given bounds and set it as the Scalar_Range
683 -- of the given fixed-point type entity. Loc is the source location used
684 -- for the constructed range. See body for further details.
686 procedure Set_Scalar_Range_For_Subtype
690 -- This routine is used to set the scalar range field for a subtype given
691 -- Def_Id, the entity for the subtype, and R, the range expression for the
692 -- scalar range. Subt provides the parent subtype to be used to analyze,
693 -- resolve, and check the given range.
695 procedure Set_Default_SSO (T : Entity_Id);
696 -- T is the entity for an array or record being declared. This procedure
697 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
698 -- to the setting of Opt.Default_SSO.
700 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
701 -- Create a new signed integer entity, and apply the constraint to obtain
702 -- the required first named subtype of this type.
704 procedure Set_Stored_Constraint_From_Discriminant_Constraint
706 -- E is some record type. This routine computes E's Stored_Constraint
707 -- from its Discriminant_Constraint.
709 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
710 -- Check that an entity in a list of progenitors is an interface,
711 -- emit error otherwise.
713 -----------------------
714 -- Access_Definition --
715 -----------------------
717 function Access_Definition
718 (Related_Nod : Node_Id;
719 N : Node_Id) return Entity_Id
721 Anon_Type : Entity_Id;
722 Anon_Scope : Entity_Id;
723 Desig_Type : Entity_Id;
724 Enclosing_Prot_Type : Entity_Id := Empty;
727 Check_SPARK_05_Restriction ("access type is not allowed", N);
729 if Is_Entry (Current_Scope)
730 and then Is_Task_Type (Etype (Scope (Current_Scope)))
732 Error_Msg_N ("task entries cannot have access parameters", N);
736 -- Ada 2005: For an object declaration the corresponding anonymous
737 -- type is declared in the current scope.
739 -- If the access definition is the return type of another access to
740 -- function, scope is the current one, because it is the one of the
741 -- current type declaration, except for the pathological case below.
743 if Nkind_In (Related_Nod, N_Object_Declaration,
744 N_Access_Function_Definition)
746 Anon_Scope := Current_Scope;
748 -- A pathological case: function returning access functions that
749 -- return access functions, etc. Each anonymous access type created
750 -- is in the enclosing scope of the outermost function.
757 while Nkind_In (Par, N_Access_Function_Definition,
763 if Nkind (Par) = N_Function_Specification then
764 Anon_Scope := Scope (Defining_Entity (Par));
768 -- For the anonymous function result case, retrieve the scope of the
769 -- function specification's associated entity rather than using the
770 -- current scope. The current scope will be the function itself if the
771 -- formal part is currently being analyzed, but will be the parent scope
772 -- in the case of a parameterless function, and we always want to use
773 -- the function's parent scope. Finally, if the function is a child
774 -- unit, we must traverse the tree to retrieve the proper entity.
776 elsif Nkind (Related_Nod) = N_Function_Specification
777 and then Nkind (Parent (N)) /= N_Parameter_Specification
779 -- If the current scope is a protected type, the anonymous access
780 -- is associated with one of the protected operations, and must
781 -- be available in the scope that encloses the protected declaration.
782 -- Otherwise the type is in the scope enclosing the subprogram.
784 -- If the function has formals, The return type of a subprogram
785 -- declaration is analyzed in the scope of the subprogram (see
786 -- Process_Formals) and thus the protected type, if present, is
787 -- the scope of the current function scope.
789 if Ekind (Current_Scope) = E_Protected_Type then
790 Enclosing_Prot_Type := Current_Scope;
792 elsif Ekind (Current_Scope) = E_Function
793 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
795 Enclosing_Prot_Type := Scope (Current_Scope);
798 if Present (Enclosing_Prot_Type) then
799 Anon_Scope := Scope (Enclosing_Prot_Type);
802 Anon_Scope := Scope (Defining_Entity (Related_Nod));
805 -- For an access type definition, if the current scope is a child
806 -- unit it is the scope of the type.
808 elsif Is_Compilation_Unit (Current_Scope) then
809 Anon_Scope := Current_Scope;
811 -- For access formals, access components, and access discriminants, the
812 -- scope is that of the enclosing declaration,
815 Anon_Scope := Scope (Current_Scope);
820 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
823 and then Ada_Version >= Ada_2005
825 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
828 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
829 -- the corresponding semantic routine
831 if Present (Access_To_Subprogram_Definition (N)) then
833 -- Compiler runtime units are compiled in Ada 2005 mode when building
834 -- the runtime library but must also be compilable in Ada 95 mode
835 -- (when bootstrapping the compiler).
837 Check_Compiler_Unit ("anonymous access to subprogram", N);
839 Access_Subprogram_Declaration
840 (T_Name => Anon_Type,
841 T_Def => Access_To_Subprogram_Definition (N));
843 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
845 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
847 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
850 Set_Can_Use_Internal_Rep
851 (Anon_Type, not Always_Compatible_Rep_On_Target);
853 -- If the anonymous access is associated with a protected operation,
854 -- create a reference to it after the enclosing protected definition
855 -- because the itype will be used in the subsequent bodies.
857 -- If the anonymous access itself is protected, a full type
858 -- declaratiton will be created for it, so that the equivalent
859 -- record type can be constructed. For further details, see
860 -- Replace_Anonymous_Access_To_Protected-Subprogram.
862 if Ekind (Current_Scope) = E_Protected_Type
863 and then not Protected_Present (Access_To_Subprogram_Definition (N))
865 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
871 Find_Type (Subtype_Mark (N));
872 Desig_Type := Entity (Subtype_Mark (N));
874 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
875 Set_Etype (Anon_Type, Anon_Type);
877 -- Make sure the anonymous access type has size and alignment fields
878 -- set, as required by gigi. This is necessary in the case of the
879 -- Task_Body_Procedure.
881 if not Has_Private_Component (Desig_Type) then
882 Layout_Type (Anon_Type);
885 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
886 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
887 -- the null value is allowed. In Ada 95 the null value is never allowed.
889 if Ada_Version >= Ada_2005 then
890 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
892 Set_Can_Never_Be_Null (Anon_Type, True);
895 -- The anonymous access type is as public as the discriminated type or
896 -- subprogram that defines it. It is imported (for back-end purposes)
897 -- if the designated type is.
899 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
901 -- Ada 2005 (AI-231): Propagate the access-constant attribute
903 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
905 -- The context is either a subprogram declaration, object declaration,
906 -- or an access discriminant, in a private or a full type declaration.
907 -- In the case of a subprogram, if the designated type is incomplete,
908 -- the operation will be a primitive operation of the full type, to be
909 -- updated subsequently. If the type is imported through a limited_with
910 -- clause, the subprogram is not a primitive operation of the type
911 -- (which is declared elsewhere in some other scope).
913 if Ekind (Desig_Type) = E_Incomplete_Type
914 and then not From_Limited_With (Desig_Type)
915 and then Is_Overloadable (Current_Scope)
917 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
918 Set_Has_Delayed_Freeze (Current_Scope);
921 -- If the designated type is limited and class-wide, the object might
922 -- contain tasks, so we create a Master entity for the declaration. This
923 -- must be done before expansion of the full declaration, because the
924 -- declaration may include an expression that is an allocator, whose
925 -- expansion needs the proper Master for the created tasks.
928 and then Nkind (Related_Nod) = N_Object_Declaration
930 if Is_Limited_Record (Desig_Type)
931 and then Is_Class_Wide_Type (Desig_Type)
932 and then Tasking_Allowed
934 Build_Class_Wide_Master (Anon_Type);
936 -- Similarly, if the type is an anonymous access that designates
937 -- tasks, create a master entity for it in the current context.
939 elsif Has_Task (Desig_Type)
940 and then Comes_From_Source (Related_Nod)
942 Build_Master_Entity (Defining_Identifier (Related_Nod));
943 Build_Master_Renaming (Anon_Type);
947 -- For a private component of a protected type, it is imperative that
948 -- the back-end elaborate the type immediately after the protected
949 -- declaration, because this type will be used in the declarations
950 -- created for the component within each protected body, so we must
951 -- create an itype reference for it now.
953 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
954 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
956 -- Similarly, if the access definition is the return result of a
957 -- function, create an itype reference for it because it will be used
958 -- within the function body. For a regular function that is not a
959 -- compilation unit, insert reference after the declaration. For a
960 -- protected operation, insert it after the enclosing protected type
961 -- declaration. In either case, do not create a reference for a type
962 -- obtained through a limited_with clause, because this would introduce
963 -- semantic dependencies.
965 -- Similarly, do not create a reference if the designated type is a
966 -- generic formal, because no use of it will reach the backend.
968 elsif Nkind (Related_Nod) = N_Function_Specification
969 and then not From_Limited_With (Desig_Type)
970 and then not Is_Generic_Type (Desig_Type)
972 if Present (Enclosing_Prot_Type) then
973 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
975 elsif Is_List_Member (Parent (Related_Nod))
976 and then Nkind (Parent (N)) /= N_Parameter_Specification
978 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
981 -- Finally, create an itype reference for an object declaration of an
982 -- anonymous access type. This is strictly necessary only for deferred
983 -- constants, but in any case will avoid out-of-scope problems in the
986 elsif Nkind (Related_Nod) = N_Object_Declaration then
987 Build_Itype_Reference (Anon_Type, Related_Nod);
991 end Access_Definition;
993 -----------------------------------
994 -- Access_Subprogram_Declaration --
995 -----------------------------------
997 procedure Access_Subprogram_Declaration
1001 procedure Check_For_Premature_Usage (Def : Node_Id);
1002 -- Check that type T_Name is not used, directly or recursively, as a
1003 -- parameter or a return type in Def. Def is either a subtype, an
1004 -- access_definition, or an access_to_subprogram_definition.
1006 -------------------------------
1007 -- Check_For_Premature_Usage --
1008 -------------------------------
1010 procedure Check_For_Premature_Usage (Def : Node_Id) is
1014 -- Check for a subtype mark
1016 if Nkind (Def) in N_Has_Etype then
1017 if Etype (Def) = T_Name then
1019 ("type& cannot be used before end of its declaration", Def);
1022 -- If this is not a subtype, then this is an access_definition
1024 elsif Nkind (Def) = N_Access_Definition then
1025 if Present (Access_To_Subprogram_Definition (Def)) then
1026 Check_For_Premature_Usage
1027 (Access_To_Subprogram_Definition (Def));
1029 Check_For_Premature_Usage (Subtype_Mark (Def));
1032 -- The only cases left are N_Access_Function_Definition and
1033 -- N_Access_Procedure_Definition.
1036 if Present (Parameter_Specifications (Def)) then
1037 Param := First (Parameter_Specifications (Def));
1038 while Present (Param) loop
1039 Check_For_Premature_Usage (Parameter_Type (Param));
1040 Param := Next (Param);
1044 if Nkind (Def) = N_Access_Function_Definition then
1045 Check_For_Premature_Usage (Result_Definition (Def));
1048 end Check_For_Premature_Usage;
1052 Formals : constant List_Id := Parameter_Specifications (T_Def);
1055 Desig_Type : constant Entity_Id :=
1056 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1058 -- Start of processing for Access_Subprogram_Declaration
1061 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1063 -- Associate the Itype node with the inner full-type declaration or
1064 -- subprogram spec or entry body. This is required to handle nested
1065 -- anonymous declarations. For example:
1068 -- (X : access procedure
1069 -- (Y : access procedure
1072 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1073 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1074 N_Private_Type_Declaration,
1075 N_Private_Extension_Declaration,
1076 N_Procedure_Specification,
1077 N_Function_Specification,
1081 Nkind_In (D_Ityp, N_Object_Declaration,
1082 N_Object_Renaming_Declaration,
1083 N_Formal_Object_Declaration,
1084 N_Formal_Type_Declaration,
1085 N_Task_Type_Declaration,
1086 N_Protected_Type_Declaration))
1088 D_Ityp := Parent (D_Ityp);
1089 pragma Assert (D_Ityp /= Empty);
1092 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1094 if Nkind_In (D_Ityp, N_Procedure_Specification,
1095 N_Function_Specification)
1097 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1099 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1100 N_Object_Declaration,
1101 N_Object_Renaming_Declaration,
1102 N_Formal_Type_Declaration)
1104 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1107 if Nkind (T_Def) = N_Access_Function_Definition then
1108 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1110 Acc : constant Node_Id := Result_Definition (T_Def);
1113 if Present (Access_To_Subprogram_Definition (Acc))
1115 Protected_Present (Access_To_Subprogram_Definition (Acc))
1119 Replace_Anonymous_Access_To_Protected_Subprogram
1125 Access_Definition (T_Def, Result_Definition (T_Def)));
1130 Analyze (Result_Definition (T_Def));
1133 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1136 -- If a null exclusion is imposed on the result type, then
1137 -- create a null-excluding itype (an access subtype) and use
1138 -- it as the function's Etype.
1140 if Is_Access_Type (Typ)
1141 and then Null_Exclusion_In_Return_Present (T_Def)
1143 Set_Etype (Desig_Type,
1144 Create_Null_Excluding_Itype
1146 Related_Nod => T_Def,
1147 Scope_Id => Current_Scope));
1150 if From_Limited_With (Typ) then
1152 -- AI05-151: Incomplete types are allowed in all basic
1153 -- declarations, including access to subprograms.
1155 if Ada_Version >= Ada_2012 then
1160 ("illegal use of incomplete type&",
1161 Result_Definition (T_Def), Typ);
1164 elsif Ekind (Current_Scope) = E_Package
1165 and then In_Private_Part (Current_Scope)
1167 if Ekind (Typ) = E_Incomplete_Type then
1168 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1170 elsif Is_Class_Wide_Type (Typ)
1171 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1174 (Desig_Type, Private_Dependents (Etype (Typ)));
1178 Set_Etype (Desig_Type, Typ);
1183 if not (Is_Type (Etype (Desig_Type))) then
1185 ("expect type in function specification",
1186 Result_Definition (T_Def));
1190 Set_Etype (Desig_Type, Standard_Void_Type);
1193 if Present (Formals) then
1194 Push_Scope (Desig_Type);
1196 -- Some special tests here. These special tests can be removed
1197 -- if and when Itypes always have proper parent pointers to their
1200 -- Special test 1) Link defining_identifier of formals. Required by
1201 -- First_Formal to provide its functionality.
1207 F := First (Formals);
1209 while Present (F) loop
1210 if No (Parent (Defining_Identifier (F))) then
1211 Set_Parent (Defining_Identifier (F), F);
1218 Process_Formals (Formals, Parent (T_Def));
1220 -- Special test 2) End_Scope requires that the parent pointer be set
1221 -- to something reasonable, but Itypes don't have parent pointers. So
1222 -- we set it and then unset it ???
1224 Set_Parent (Desig_Type, T_Name);
1226 Set_Parent (Desig_Type, Empty);
1229 -- Check for premature usage of the type being defined
1231 Check_For_Premature_Usage (T_Def);
1233 -- The return type and/or any parameter type may be incomplete. Mark the
1234 -- subprogram_type as depending on the incomplete type, so that it can
1235 -- be updated when the full type declaration is seen. This only applies
1236 -- to incomplete types declared in some enclosing scope, not to limited
1237 -- views from other packages.
1239 -- Prior to Ada 2012, access to functions can only have in_parameters.
1241 if Present (Formals) then
1242 Formal := First_Formal (Desig_Type);
1243 while Present (Formal) loop
1244 if Ekind (Formal) /= E_In_Parameter
1245 and then Nkind (T_Def) = N_Access_Function_Definition
1246 and then Ada_Version < Ada_2012
1248 Error_Msg_N ("functions can only have IN parameters", Formal);
1251 if Ekind (Etype (Formal)) = E_Incomplete_Type
1252 and then In_Open_Scopes (Scope (Etype (Formal)))
1254 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1255 Set_Has_Delayed_Freeze (Desig_Type);
1258 Next_Formal (Formal);
1262 -- Check whether an indirect call without actuals may be possible. This
1263 -- is used when resolving calls whose result is then indexed.
1265 May_Need_Actuals (Desig_Type);
1267 -- If the return type is incomplete, this is legal as long as the type
1268 -- is declared in the current scope and will be completed in it (rather
1269 -- than being part of limited view).
1271 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1272 and then not Has_Delayed_Freeze (Desig_Type)
1273 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1275 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1276 Set_Has_Delayed_Freeze (Desig_Type);
1279 Check_Delayed_Subprogram (Desig_Type);
1281 if Protected_Present (T_Def) then
1282 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1283 Set_Convention (Desig_Type, Convention_Protected);
1285 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1288 Set_Can_Use_Internal_Rep (T_Name,
1289 not Always_Compatible_Rep_On_Target);
1290 Set_Etype (T_Name, T_Name);
1291 Init_Size_Align (T_Name);
1292 Set_Directly_Designated_Type (T_Name, Desig_Type);
1294 -- If the access_to_subprogram is not declared at the library level,
1295 -- it can only point to subprograms that are at the same or deeper
1296 -- accessibility level. The corresponding subprogram type might
1297 -- require an activation record when compiling for C.
1299 Set_Needs_Activation_Record (Desig_Type,
1300 not Is_Library_Level_Entity (T_Name));
1302 Generate_Reference_To_Formals (T_Name);
1304 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1306 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1308 Check_Restriction (No_Access_Subprograms, T_Def);
1309 end Access_Subprogram_Declaration;
1311 ----------------------------
1312 -- Access_Type_Declaration --
1313 ----------------------------
1315 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1316 P : constant Node_Id := Parent (Def);
1317 S : constant Node_Id := Subtype_Indication (Def);
1319 Full_Desig : Entity_Id;
1322 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1324 -- Check for permissible use of incomplete type
1326 if Nkind (S) /= N_Subtype_Indication then
1329 if Present (Entity (S))
1330 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1332 Set_Directly_Designated_Type (T, Entity (S));
1334 -- If the designated type is a limited view, we cannot tell if
1335 -- the full view contains tasks, and there is no way to handle
1336 -- that full view in a client. We create a master entity for the
1337 -- scope, which will be used when a client determines that one
1340 if From_Limited_With (Entity (S))
1341 and then not Is_Class_Wide_Type (Entity (S))
1343 Set_Ekind (T, E_Access_Type);
1344 Build_Master_Entity (T);
1345 Build_Master_Renaming (T);
1349 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1352 -- If the access definition is of the form: ACCESS NOT NULL ..
1353 -- the subtype indication must be of an access type. Create
1354 -- a null-excluding subtype of it.
1356 if Null_Excluding_Subtype (Def) then
1357 if not Is_Access_Type (Entity (S)) then
1358 Error_Msg_N ("null exclusion must apply to access type", Def);
1362 Loc : constant Source_Ptr := Sloc (S);
1364 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1368 Make_Subtype_Declaration (Loc,
1369 Defining_Identifier => Nam,
1370 Subtype_Indication =>
1371 New_Occurrence_Of (Entity (S), Loc));
1372 Set_Null_Exclusion_Present (Decl);
1373 Insert_Before (Parent (Def), Decl);
1375 Set_Entity (S, Nam);
1381 Set_Directly_Designated_Type (T,
1382 Process_Subtype (S, P, T, 'P'));
1385 if All_Present (Def) or Constant_Present (Def) then
1386 Set_Ekind (T, E_General_Access_Type);
1388 Set_Ekind (T, E_Access_Type);
1391 Full_Desig := Designated_Type (T);
1393 if Base_Type (Full_Desig) = T then
1394 Error_Msg_N ("access type cannot designate itself", S);
1396 -- In Ada 2005, the type may have a limited view through some unit in
1397 -- its own context, allowing the following circularity that cannot be
1398 -- detected earlier.
1400 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1403 ("access type cannot designate its own class-wide type", S);
1405 -- Clean up indication of tagged status to prevent cascaded errors
1407 Set_Is_Tagged_Type (T, False);
1412 -- If the type has appeared already in a with_type clause, it is frozen
1413 -- and the pointer size is already set. Else, initialize.
1415 if not From_Limited_With (T) then
1416 Init_Size_Align (T);
1419 -- Note that Has_Task is always false, since the access type itself
1420 -- is not a task type. See Einfo for more description on this point.
1421 -- Exactly the same consideration applies to Has_Controlled_Component
1422 -- and to Has_Protected.
1424 Set_Has_Task (T, False);
1425 Set_Has_Protected (T, False);
1426 Set_Has_Timing_Event (T, False);
1427 Set_Has_Controlled_Component (T, False);
1429 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1430 -- problems where an incomplete view of this entity has been previously
1431 -- established by a limited with and an overlaid version of this field
1432 -- (Stored_Constraint) was initialized for the incomplete view.
1434 -- This reset is performed in most cases except where the access type
1435 -- has been created for the purposes of allocating or deallocating a
1436 -- build-in-place object. Such access types have explicitly set pools
1437 -- and finalization masters.
1439 if No (Associated_Storage_Pool (T)) then
1440 Set_Finalization_Master (T, Empty);
1443 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1446 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1447 Set_Is_Access_Constant (T, Constant_Present (Def));
1448 end Access_Type_Declaration;
1450 ----------------------------------
1451 -- Add_Interface_Tag_Components --
1452 ----------------------------------
1454 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1455 Loc : constant Source_Ptr := Sloc (N);
1459 procedure Add_Tag (Iface : Entity_Id);
1460 -- Add tag for one of the progenitor interfaces
1466 procedure Add_Tag (Iface : Entity_Id) is
1473 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1475 -- This is a reasonable place to propagate predicates
1477 if Has_Predicates (Iface) then
1478 Set_Has_Predicates (Typ);
1482 Make_Component_Definition (Loc,
1483 Aliased_Present => True,
1484 Subtype_Indication =>
1485 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1487 Tag := Make_Temporary (Loc, 'V');
1490 Make_Component_Declaration (Loc,
1491 Defining_Identifier => Tag,
1492 Component_Definition => Def);
1494 Analyze_Component_Declaration (Decl);
1496 Set_Analyzed (Decl);
1497 Set_Ekind (Tag, E_Component);
1499 Set_Is_Aliased (Tag);
1500 Set_Is_Independent (Tag);
1501 Set_Related_Type (Tag, Iface);
1502 Init_Component_Location (Tag);
1504 pragma Assert (Is_Frozen (Iface));
1506 Set_DT_Entry_Count (Tag,
1507 DT_Entry_Count (First_Entity (Iface)));
1509 if No (Last_Tag) then
1512 Insert_After (Last_Tag, Decl);
1517 -- If the ancestor has discriminants we need to give special support
1518 -- to store the offset_to_top value of the secondary dispatch tables.
1519 -- For this purpose we add a supplementary component just after the
1520 -- field that contains the tag associated with each secondary DT.
1522 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1524 Make_Component_Definition (Loc,
1525 Subtype_Indication =>
1526 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1528 Offset := Make_Temporary (Loc, 'V');
1531 Make_Component_Declaration (Loc,
1532 Defining_Identifier => Offset,
1533 Component_Definition => Def);
1535 Analyze_Component_Declaration (Decl);
1537 Set_Analyzed (Decl);
1538 Set_Ekind (Offset, E_Component);
1539 Set_Is_Aliased (Offset);
1540 Set_Is_Independent (Offset);
1541 Set_Related_Type (Offset, Iface);
1542 Init_Component_Location (Offset);
1543 Insert_After (Last_Tag, Decl);
1554 -- Start of processing for Add_Interface_Tag_Components
1557 if not RTE_Available (RE_Interface_Tag) then
1559 ("(Ada 2005) interface types not supported by this run-time!",
1564 if Ekind (Typ) /= E_Record_Type
1565 or else (Is_Concurrent_Record_Type (Typ)
1566 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1567 or else (not Is_Concurrent_Record_Type (Typ)
1568 and then No (Interfaces (Typ))
1569 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1574 -- Find the current last tag
1576 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1577 Ext := Record_Extension_Part (Type_Definition (N));
1579 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1580 Ext := Type_Definition (N);
1585 if not (Present (Component_List (Ext))) then
1586 Set_Null_Present (Ext, False);
1588 Set_Component_List (Ext,
1589 Make_Component_List (Loc,
1590 Component_Items => L,
1591 Null_Present => False));
1593 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1594 L := Component_Items
1596 (Record_Extension_Part
1597 (Type_Definition (N))));
1599 L := Component_Items
1601 (Type_Definition (N)));
1604 -- Find the last tag component
1607 while Present (Comp) loop
1608 if Nkind (Comp) = N_Component_Declaration
1609 and then Is_Tag (Defining_Identifier (Comp))
1618 -- At this point L references the list of components and Last_Tag
1619 -- references the current last tag (if any). Now we add the tag
1620 -- corresponding with all the interfaces that are not implemented
1623 if Present (Interfaces (Typ)) then
1624 Elmt := First_Elmt (Interfaces (Typ));
1625 while Present (Elmt) loop
1626 Add_Tag (Node (Elmt));
1630 end Add_Interface_Tag_Components;
1632 -------------------------------------
1633 -- Add_Internal_Interface_Entities --
1634 -------------------------------------
1636 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1639 Iface_Elmt : Elmt_Id;
1640 Iface_Prim : Entity_Id;
1641 Ifaces_List : Elist_Id;
1642 New_Subp : Entity_Id := Empty;
1644 Restore_Scope : Boolean := False;
1647 pragma Assert (Ada_Version >= Ada_2005
1648 and then Is_Record_Type (Tagged_Type)
1649 and then Is_Tagged_Type (Tagged_Type)
1650 and then Has_Interfaces (Tagged_Type)
1651 and then not Is_Interface (Tagged_Type));
1653 -- Ensure that the internal entities are added to the scope of the type
1655 if Scope (Tagged_Type) /= Current_Scope then
1656 Push_Scope (Scope (Tagged_Type));
1657 Restore_Scope := True;
1660 Collect_Interfaces (Tagged_Type, Ifaces_List);
1662 Iface_Elmt := First_Elmt (Ifaces_List);
1663 while Present (Iface_Elmt) loop
1664 Iface := Node (Iface_Elmt);
1666 -- Originally we excluded here from this processing interfaces that
1667 -- are parents of Tagged_Type because their primitives are located
1668 -- in the primary dispatch table (and hence no auxiliary internal
1669 -- entities are required to handle secondary dispatch tables in such
1670 -- case). However, these auxiliary entities are also required to
1671 -- handle derivations of interfaces in formals of generics (see
1672 -- Derive_Subprograms).
1674 Elmt := First_Elmt (Primitive_Operations (Iface));
1675 while Present (Elmt) loop
1676 Iface_Prim := Node (Elmt);
1678 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1680 Find_Primitive_Covering_Interface
1681 (Tagged_Type => Tagged_Type,
1682 Iface_Prim => Iface_Prim);
1684 if No (Prim) and then Serious_Errors_Detected > 0 then
1688 pragma Assert (Present (Prim));
1690 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1691 -- differs from the name of the interface primitive then it is
1692 -- a private primitive inherited from a parent type. In such
1693 -- case, given that Tagged_Type covers the interface, the
1694 -- inherited private primitive becomes visible. For such
1695 -- purpose we add a new entity that renames the inherited
1696 -- private primitive.
1698 if Chars (Prim) /= Chars (Iface_Prim) then
1699 pragma Assert (Has_Suffix (Prim, 'P'));
1701 (New_Subp => New_Subp,
1702 Parent_Subp => Iface_Prim,
1703 Derived_Type => Tagged_Type,
1704 Parent_Type => Iface);
1705 Set_Alias (New_Subp, Prim);
1706 Set_Is_Abstract_Subprogram
1707 (New_Subp, Is_Abstract_Subprogram (Prim));
1711 (New_Subp => New_Subp,
1712 Parent_Subp => Iface_Prim,
1713 Derived_Type => Tagged_Type,
1714 Parent_Type => Iface);
1719 if Is_Inherited_Operation (Prim)
1720 and then Present (Alias (Prim))
1722 Anc := Alias (Prim);
1724 Anc := Overridden_Operation (Prim);
1727 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1728 -- nonconforming preconditions in both an ancestor and
1729 -- a progenitor operation.
1731 -- If the operation is a primitive wrapper it is an explicit
1732 -- (overriding) operqtion and all is fine.
1735 and then Has_Non_Trivial_Precondition (Anc)
1736 and then Has_Non_Trivial_Precondition (Iface_Prim)
1738 if Is_Abstract_Subprogram (Prim)
1740 (Ekind (Prim) = E_Procedure
1741 and then Nkind (Parent (Prim)) =
1742 N_Procedure_Specification
1743 and then Null_Present (Parent (Prim)))
1744 or else Is_Primitive_Wrapper (Prim)
1748 -- The operation is inherited and must be overridden
1750 elsif not Comes_From_Source (Prim) then
1752 ("&inherits non-conforming preconditions and must "
1753 & "be overridden (RM 6.1.1 (10-16)",
1754 Parent (Tagged_Type), Prim);
1759 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1760 -- associated with interface types. These entities are
1761 -- only registered in the list of primitives of its
1762 -- corresponding tagged type because they are only used
1763 -- to fill the contents of the secondary dispatch tables.
1764 -- Therefore they are removed from the homonym chains.
1766 Set_Is_Hidden (New_Subp);
1767 Set_Is_Internal (New_Subp);
1768 Set_Alias (New_Subp, Prim);
1769 Set_Is_Abstract_Subprogram
1770 (New_Subp, Is_Abstract_Subprogram (Prim));
1771 Set_Interface_Alias (New_Subp, Iface_Prim);
1773 -- If the returned type is an interface then propagate it to
1774 -- the returned type. Needed by the thunk to generate the code
1775 -- which displaces "this" to reference the corresponding
1776 -- secondary dispatch table in the returned object.
1778 if Is_Interface (Etype (Iface_Prim)) then
1779 Set_Etype (New_Subp, Etype (Iface_Prim));
1782 -- Internal entities associated with interface types are only
1783 -- registered in the list of primitives of the tagged type.
1784 -- They are only used to fill the contents of the secondary
1785 -- dispatch tables. Therefore they are not needed in the
1788 Remove_Homonym (New_Subp);
1790 -- Hidden entities associated with interfaces must have set
1791 -- the Has_Delay_Freeze attribute to ensure that, in case
1792 -- of locally defined tagged types (or compiling with static
1793 -- dispatch tables generation disabled) the corresponding
1794 -- entry of the secondary dispatch table is filled when such
1795 -- an entity is frozen.
1797 Set_Has_Delayed_Freeze (New_Subp);
1804 Next_Elmt (Iface_Elmt);
1807 if Restore_Scope then
1810 end Add_Internal_Interface_Entities;
1812 -----------------------------------
1813 -- Analyze_Component_Declaration --
1814 -----------------------------------
1816 procedure Analyze_Component_Declaration (N : Node_Id) is
1817 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1818 Id : constant Entity_Id := Defining_Identifier (N);
1819 E : constant Node_Id := Expression (N);
1820 Typ : constant Node_Id :=
1821 Subtype_Indication (Component_Definition (N));
1825 function Contains_POC (Constr : Node_Id) return Boolean;
1826 -- Determines whether a constraint uses the discriminant of a record
1827 -- type thus becoming a per-object constraint (POC).
1829 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1830 -- Typ is the type of the current component, check whether this type is
1831 -- a limited type. Used to validate declaration against that of
1832 -- enclosing record.
1838 function Contains_POC (Constr : Node_Id) return Boolean is
1840 -- Prevent cascaded errors
1842 if Error_Posted (Constr) then
1846 case Nkind (Constr) is
1847 when N_Attribute_Reference =>
1848 return Attribute_Name (Constr) = Name_Access
1849 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1851 when N_Discriminant_Association =>
1852 return Denotes_Discriminant (Expression (Constr));
1854 when N_Identifier =>
1855 return Denotes_Discriminant (Constr);
1857 when N_Index_Or_Discriminant_Constraint =>
1862 IDC := First (Constraints (Constr));
1863 while Present (IDC) loop
1865 -- One per-object constraint is sufficient
1867 if Contains_POC (IDC) then
1878 return Denotes_Discriminant (Low_Bound (Constr))
1880 Denotes_Discriminant (High_Bound (Constr));
1882 when N_Range_Constraint =>
1883 return Denotes_Discriminant (Range_Expression (Constr));
1890 ----------------------
1891 -- Is_Known_Limited --
1892 ----------------------
1894 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1895 P : constant Entity_Id := Etype (Typ);
1896 R : constant Entity_Id := Root_Type (Typ);
1899 if Is_Limited_Record (Typ) then
1902 -- If the root type is limited (and not a limited interface) so is
1903 -- the current type.
1905 elsif Is_Limited_Record (R)
1906 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1910 -- Else the type may have a limited interface progenitor, but a
1911 -- limited record parent that is not an interface.
1914 and then Is_Limited_Record (P)
1915 and then not Is_Interface (P)
1922 end Is_Known_Limited;
1924 -- Start of processing for Analyze_Component_Declaration
1927 Generate_Definition (Id);
1930 if Present (Typ) then
1931 T := Find_Type_Of_Object
1932 (Subtype_Indication (Component_Definition (N)), N);
1934 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1935 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1938 -- Ada 2005 (AI-230): Access Definition case
1941 pragma Assert (Present
1942 (Access_Definition (Component_Definition (N))));
1944 T := Access_Definition
1946 N => Access_Definition (Component_Definition (N)));
1947 Set_Is_Local_Anonymous_Access (T);
1949 -- Ada 2005 (AI-254)
1951 if Present (Access_To_Subprogram_Definition
1952 (Access_Definition (Component_Definition (N))))
1953 and then Protected_Present (Access_To_Subprogram_Definition
1955 (Component_Definition (N))))
1957 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1961 -- If the subtype is a constrained subtype of the enclosing record,
1962 -- (which must have a partial view) the back-end does not properly
1963 -- handle the recursion. Rewrite the component declaration with an
1964 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1965 -- the tree directly because side effects have already been removed from
1966 -- discriminant constraints.
1968 if Ekind (T) = E_Access_Subtype
1969 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1970 and then Comes_From_Source (T)
1971 and then Nkind (Parent (T)) = N_Subtype_Declaration
1972 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1975 (Subtype_Indication (Component_Definition (N)),
1976 New_Copy_Tree (Subtype_Indication (Parent (T))));
1977 T := Find_Type_Of_Object
1978 (Subtype_Indication (Component_Definition (N)), N);
1981 -- If the component declaration includes a default expression, then we
1982 -- check that the component is not of a limited type (RM 3.7(5)),
1983 -- and do the special preanalysis of the expression (see section on
1984 -- "Handling of Default and Per-Object Expressions" in the spec of
1988 Check_SPARK_05_Restriction ("default expression is not allowed", E);
1989 Preanalyze_Default_Expression (E, T);
1990 Check_Initialization (T, E);
1992 if Ada_Version >= Ada_2005
1993 and then Ekind (T) = E_Anonymous_Access_Type
1994 and then Etype (E) /= Any_Type
1996 -- Check RM 3.9.2(9): "if the expected type for an expression is
1997 -- an anonymous access-to-specific tagged type, then the object
1998 -- designated by the expression shall not be dynamically tagged
1999 -- unless it is a controlling operand in a call on a dispatching
2002 if Is_Tagged_Type (Directly_Designated_Type (T))
2004 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2006 Ekind (Directly_Designated_Type (Etype (E))) =
2010 ("access to specific tagged type required (RM 3.9.2(9))", E);
2013 -- (Ada 2005: AI-230): Accessibility check for anonymous
2016 if Type_Access_Level (Etype (E)) >
2017 Deepest_Type_Access_Level (T)
2020 ("expression has deeper access level than component " &
2021 "(RM 3.10.2 (12.2))", E);
2024 -- The initialization expression is a reference to an access
2025 -- discriminant. The type of the discriminant is always deeper
2026 -- than any access type.
2028 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2029 and then Is_Entity_Name (E)
2030 and then Ekind (Entity (E)) = E_In_Parameter
2031 and then Present (Discriminal_Link (Entity (E)))
2034 ("discriminant has deeper accessibility level than target",
2040 -- Avoid reporting spurious errors if the component is initialized with
2041 -- a raise expression (which is legal in any expression context)
2045 (Nkind (E) = N_Raise_Expression
2046 or else (Nkind (E) = N_Qualified_Expression
2047 and then Nkind (Expression (E)) = N_Raise_Expression))
2051 -- The parent type may be a private view with unknown discriminants,
2052 -- and thus unconstrained. Regular components must be constrained.
2054 elsif not Is_Definite_Subtype (T)
2055 and then Chars (Id) /= Name_uParent
2057 if Is_Class_Wide_Type (T) then
2059 ("class-wide subtype with unknown discriminants" &
2060 " in component declaration",
2061 Subtype_Indication (Component_Definition (N)));
2064 ("unconstrained subtype in component declaration",
2065 Subtype_Indication (Component_Definition (N)));
2068 -- Components cannot be abstract, except for the special case of
2069 -- the _Parent field (case of extending an abstract tagged type)
2071 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2072 Error_Msg_N ("type of a component cannot be abstract", N);
2077 if Aliased_Present (Component_Definition (N)) then
2078 Set_Is_Aliased (Id);
2080 -- AI12-001: All aliased objects are considered to be specified as
2081 -- independently addressable (RM C.6(8.1/4)).
2083 Set_Is_Independent (Id);
2086 -- The component declaration may have a per-object constraint, set
2087 -- the appropriate flag in the defining identifier of the subtype.
2089 if Present (Subtype_Indication (Component_Definition (N))) then
2091 Sindic : constant Node_Id :=
2092 Subtype_Indication (Component_Definition (N));
2094 if Nkind (Sindic) = N_Subtype_Indication
2095 and then Present (Constraint (Sindic))
2096 and then Contains_POC (Constraint (Sindic))
2098 Set_Has_Per_Object_Constraint (Id);
2103 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2104 -- out some static checks.
2106 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2107 Null_Exclusion_Static_Checks (N);
2110 -- If this component is private (or depends on a private type), flag the
2111 -- record type to indicate that some operations are not available.
2113 P := Private_Component (T);
2117 -- Check for circular definitions
2119 if P = Any_Type then
2120 Set_Etype (Id, Any_Type);
2122 -- There is a gap in the visibility of operations only if the
2123 -- component type is not defined in the scope of the record type.
2125 elsif Scope (P) = Scope (Current_Scope) then
2128 elsif Is_Limited_Type (P) then
2129 Set_Is_Limited_Composite (Current_Scope);
2132 Set_Is_Private_Composite (Current_Scope);
2137 and then Is_Limited_Type (T)
2138 and then Chars (Id) /= Name_uParent
2139 and then Is_Tagged_Type (Current_Scope)
2141 if Is_Derived_Type (Current_Scope)
2142 and then not Is_Known_Limited (Current_Scope)
2145 ("extension of nonlimited type cannot have limited components",
2148 if Is_Interface (Root_Type (Current_Scope)) then
2150 ("\limitedness is not inherited from limited interface", N);
2151 Error_Msg_N ("\add LIMITED to type indication", N);
2154 Explain_Limited_Type (T, N);
2155 Set_Etype (Id, Any_Type);
2156 Set_Is_Limited_Composite (Current_Scope, False);
2158 elsif not Is_Derived_Type (Current_Scope)
2159 and then not Is_Limited_Record (Current_Scope)
2160 and then not Is_Concurrent_Type (Current_Scope)
2163 ("nonlimited tagged type cannot have limited components", N);
2164 Explain_Limited_Type (T, N);
2165 Set_Etype (Id, Any_Type);
2166 Set_Is_Limited_Composite (Current_Scope, False);
2170 -- If the component is an unconstrained task or protected type with
2171 -- discriminants, the component and the enclosing record are limited
2172 -- and the component is constrained by its default values. Compute
2173 -- its actual subtype, else it may be allocated the maximum size by
2174 -- the backend, and possibly overflow.
2176 if Is_Concurrent_Type (T)
2177 and then not Is_Constrained (T)
2178 and then Has_Discriminants (T)
2179 and then not Has_Discriminants (Current_Scope)
2182 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2185 Set_Etype (Id, Act_T);
2187 -- Rewrite component definition to use the constrained subtype
2189 Rewrite (Component_Definition (N),
2190 Make_Component_Definition (Loc,
2191 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2195 Set_Original_Record_Component (Id, Id);
2197 if Has_Aspects (N) then
2198 Analyze_Aspect_Specifications (N, Id);
2201 Analyze_Dimension (N);
2202 end Analyze_Component_Declaration;
2204 --------------------------
2205 -- Analyze_Declarations --
2206 --------------------------
2208 procedure Analyze_Declarations (L : List_Id) is
2211 procedure Adjust_Decl;
2212 -- Adjust Decl not to include implicit label declarations, since these
2213 -- have strange Sloc values that result in elaboration check problems.
2214 -- (They have the sloc of the label as found in the source, and that
2215 -- is ahead of the current declarative part).
2217 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2218 -- Create the subprogram bodies which verify the run-time semantics of
2219 -- the pragmas listed below for each elibigle type found in declarative
2220 -- list Decls. The pragmas are:
2222 -- Default_Initial_Condition
2226 -- Context denotes the owner of the declarative list.
2228 procedure Check_Entry_Contracts;
2229 -- Perform a preanalysis of the pre- and postconditions of an entry
2230 -- declaration. This must be done before full resolution and creation
2231 -- of the parameter block, etc. to catch illegal uses within the
2232 -- contract expression. Full analysis of the expression is done when
2233 -- the contract is processed.
2235 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2236 -- Check if a nested package has entities within it that rely on library
2237 -- level private types where the full view has not been completed for
2238 -- the purposes of checking if it is acceptable to freeze an expression
2239 -- function at the point of declaration.
2241 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2242 -- Determine whether Body_Decl denotes the body of a late controlled
2243 -- primitive (either Initialize, Adjust or Finalize). If this is the
2244 -- case, add a proper spec if the body lacks one. The spec is inserted
2245 -- before Body_Decl and immediately analyzed.
2247 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2248 -- Spec_Id is the entity of a package that may define abstract states,
2249 -- and in the case of a child unit, whose ancestors may define abstract
2250 -- states. If the states have partial visible refinement, remove the
2251 -- partial visibility of each constituent at the end of the package
2252 -- spec and body declarations.
2254 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2255 -- Spec_Id is the entity of a package that may define abstract states.
2256 -- If the states have visible refinement, remove the visibility of each
2257 -- constituent at the end of the package body declaration.
2259 procedure Resolve_Aspects;
2260 -- Utility to resolve the expressions of aspects at the end of a list of
2261 -- declarations, or before a declaration that freezes previous entities,
2262 -- such as in a subprogram body.
2268 procedure Adjust_Decl is
2270 while Present (Prev (Decl))
2271 and then Nkind (Decl) = N_Implicit_Label_Declaration
2277 ----------------------------
2278 -- Build_Assertion_Bodies --
2279 ----------------------------
2281 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2282 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2283 -- Create the subprogram bodies which verify the run-time semantics
2284 -- of the pragmas listed below for type Typ. The pragmas are:
2286 -- Default_Initial_Condition
2290 -------------------------------------
2291 -- Build_Assertion_Bodies_For_Type --
2292 -------------------------------------
2294 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2296 -- Preanalyze and resolve the Default_Initial_Condition assertion
2297 -- expression at the end of the declarations to catch any errors.
2299 if Has_DIC (Typ) then
2300 Build_DIC_Procedure_Body (Typ);
2303 if Nkind (Context) = N_Package_Specification then
2305 -- Preanalyze and resolve the class-wide invariants of an
2306 -- interface at the end of whichever declarative part has the
2307 -- interface type. Note that an interface may be declared in
2308 -- any non-package declarative part, but reaching the end of
2309 -- such a declarative part will always freeze the type and
2310 -- generate the invariant procedure (see Freeze_Type).
2312 if Is_Interface (Typ) then
2314 -- Interfaces are treated as the partial view of a private
2315 -- type, in order to achieve uniformity with the general
2316 -- case. As a result, an interface receives only a "partial"
2317 -- invariant procedure, which is never called.
2319 if Has_Own_Invariants (Typ) then
2320 Build_Invariant_Procedure_Body
2322 Partial_Invariant => True);
2325 -- Preanalyze and resolve the invariants of a private type
2326 -- at the end of the visible declarations to catch potential
2327 -- errors. Inherited class-wide invariants are not included
2328 -- because they have already been resolved.
2330 elsif Decls = Visible_Declarations (Context)
2331 and then Ekind_In (Typ, E_Limited_Private_Type,
2333 E_Record_Type_With_Private)
2334 and then Has_Own_Invariants (Typ)
2336 Build_Invariant_Procedure_Body
2338 Partial_Invariant => True);
2340 -- Preanalyze and resolve the invariants of a private type's
2341 -- full view at the end of the private declarations to catch
2342 -- potential errors.
2344 elsif Decls = Private_Declarations (Context)
2345 and then not Is_Private_Type (Typ)
2346 and then Has_Private_Declaration (Typ)
2347 and then Has_Invariants (Typ)
2349 Build_Invariant_Procedure_Body (Typ);
2352 end Build_Assertion_Bodies_For_Type;
2357 Decl_Id : Entity_Id;
2359 -- Start of processing for Build_Assertion_Bodies
2362 Decl := First (Decls);
2363 while Present (Decl) loop
2364 if Is_Declaration (Decl) then
2365 Decl_Id := Defining_Entity (Decl);
2367 if Is_Type (Decl_Id) then
2368 Build_Assertion_Bodies_For_Type (Decl_Id);
2374 end Build_Assertion_Bodies;
2376 ---------------------------
2377 -- Check_Entry_Contracts --
2378 ---------------------------
2380 procedure Check_Entry_Contracts is
2386 Ent := First_Entity (Current_Scope);
2387 while Present (Ent) loop
2389 -- This only concerns entries with pre/postconditions
2391 if Ekind (Ent) = E_Entry
2392 and then Present (Contract (Ent))
2393 and then Present (Pre_Post_Conditions (Contract (Ent)))
2395 ASN := Pre_Post_Conditions (Contract (Ent));
2397 Install_Formals (Ent);
2399 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2400 -- is performed on a copy of the pragma expression, to prevent
2401 -- modifying the original expression.
2403 while Present (ASN) loop
2404 if Nkind (ASN) = N_Pragma then
2408 (First (Pragma_Argument_Associations (ASN))));
2409 Set_Parent (Exp, ASN);
2411 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2414 ASN := Next_Pragma (ASN);
2422 end Check_Entry_Contracts;
2424 ----------------------------------
2425 -- Contains_Lib_Incomplete_Type --
2426 ----------------------------------
2428 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2432 -- Avoid looking through scopes that do not meet the precondition of
2433 -- Pkg not being within a library unit spec.
2435 if not Is_Compilation_Unit (Pkg)
2436 and then not Is_Generic_Instance (Pkg)
2437 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2439 -- Loop through all entities in the current scope to identify
2440 -- an entity that depends on a private type.
2442 Curr := First_Entity (Pkg);
2444 if Nkind (Curr) in N_Entity
2445 and then Depends_On_Private (Curr)
2450 exit when Last_Entity (Current_Scope) = Curr;
2456 end Contains_Lib_Incomplete_Type;
2458 --------------------------------------
2459 -- Handle_Late_Controlled_Primitive --
2460 --------------------------------------
2462 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2463 Body_Spec : constant Node_Id := Specification (Body_Decl);
2464 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2465 Loc : constant Source_Ptr := Sloc (Body_Id);
2466 Params : constant List_Id :=
2467 Parameter_Specifications (Body_Spec);
2469 Spec_Id : Entity_Id;
2473 -- Consider only procedure bodies whose name matches one of the three
2474 -- controlled primitives.
2476 if Nkind (Body_Spec) /= N_Procedure_Specification
2477 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2483 -- A controlled primitive must have exactly one formal which is not
2484 -- an anonymous access type.
2486 elsif List_Length (Params) /= 1 then
2490 Typ := Parameter_Type (First (Params));
2492 if Nkind (Typ) = N_Access_Definition then
2498 -- The type of the formal must be derived from [Limited_]Controlled
2500 if not Is_Controlled (Entity (Typ)) then
2504 -- Check whether a specification exists for this body. We do not
2505 -- analyze the spec of the body in full, because it will be analyzed
2506 -- again when the body is properly analyzed, and we cannot create
2507 -- duplicate entries in the formals chain. We look for an explicit
2508 -- specification because the body may be an overriding operation and
2509 -- an inherited spec may be present.
2511 Spec_Id := Current_Entity (Body_Id);
2513 while Present (Spec_Id) loop
2514 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2515 and then Scope (Spec_Id) = Current_Scope
2516 and then Present (First_Formal (Spec_Id))
2517 and then No (Next_Formal (First_Formal (Spec_Id)))
2518 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2519 and then Comes_From_Source (Spec_Id)
2524 Spec_Id := Homonym (Spec_Id);
2527 -- At this point the body is known to be a late controlled primitive.
2528 -- Generate a matching spec and insert it before the body. Note the
2529 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2530 -- tree in this case.
2532 Spec := Copy_Separate_Tree (Body_Spec);
2534 -- Ensure that the subprogram declaration does not inherit the null
2535 -- indicator from the body as we now have a proper spec/body pair.
2537 Set_Null_Present (Spec, False);
2539 -- Ensure that the freeze node is inserted after the declaration of
2540 -- the primitive since its expansion will freeze the primitive.
2542 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2544 Insert_Before_And_Analyze (Body_Decl, Decl);
2545 end Handle_Late_Controlled_Primitive;
2547 ----------------------------------------
2548 -- Remove_Partial_Visible_Refinements --
2549 ----------------------------------------
2551 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2552 State_Elmt : Elmt_Id;
2554 if Present (Abstract_States (Spec_Id)) then
2555 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2556 while Present (State_Elmt) loop
2557 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2558 Next_Elmt (State_Elmt);
2562 -- For a child unit, also hide the partial state refinement from
2563 -- ancestor packages.
2565 if Is_Child_Unit (Spec_Id) then
2566 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2568 end Remove_Partial_Visible_Refinements;
2570 --------------------------------
2571 -- Remove_Visible_Refinements --
2572 --------------------------------
2574 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2575 State_Elmt : Elmt_Id;
2577 if Present (Abstract_States (Spec_Id)) then
2578 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2579 while Present (State_Elmt) loop
2580 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2581 Next_Elmt (State_Elmt);
2584 end Remove_Visible_Refinements;
2586 ---------------------
2587 -- Resolve_Aspects --
2588 ---------------------
2590 procedure Resolve_Aspects is
2594 E := First_Entity (Current_Scope);
2595 while Present (E) loop
2596 Resolve_Aspect_Expressions (E);
2599 end Resolve_Aspects;
2603 Context : Node_Id := Empty;
2604 Freeze_From : Entity_Id := Empty;
2605 Next_Decl : Node_Id;
2607 Body_Seen : Boolean := False;
2608 -- Flag set when the first body [stub] is encountered
2610 -- Start of processing for Analyze_Declarations
2613 if Restriction_Check_Required (SPARK_05) then
2614 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2618 while Present (Decl) loop
2620 -- Package spec cannot contain a package declaration in SPARK
2622 if Nkind (Decl) = N_Package_Declaration
2623 and then Nkind (Parent (L)) = N_Package_Specification
2625 Check_SPARK_05_Restriction
2626 ("package specification cannot contain a package declaration",
2630 -- Complete analysis of declaration
2633 Next_Decl := Next (Decl);
2635 if No (Freeze_From) then
2636 Freeze_From := First_Entity (Current_Scope);
2639 -- At the end of a declarative part, freeze remaining entities
2640 -- declared in it. The end of the visible declarations of package
2641 -- specification is not the end of a declarative part if private
2642 -- declarations are present. The end of a package declaration is a
2643 -- freezing point only if it a library package. A task definition or
2644 -- protected type definition is not a freeze point either. Finally,
2645 -- we do not freeze entities in generic scopes, because there is no
2646 -- code generated for them and freeze nodes will be generated for
2649 -- The end of a package instantiation is not a freeze point, but
2650 -- for now we make it one, because the generic body is inserted
2651 -- (currently) immediately after. Generic instantiations will not
2652 -- be a freeze point once delayed freezing of bodies is implemented.
2653 -- (This is needed in any case for early instantiations ???).
2655 if No (Next_Decl) then
2656 if Nkind (Parent (L)) = N_Component_List then
2659 elsif Nkind_In (Parent (L), N_Protected_Definition,
2662 Check_Entry_Contracts;
2664 elsif Nkind (Parent (L)) /= N_Package_Specification then
2665 if Nkind (Parent (L)) = N_Package_Body then
2666 Freeze_From := First_Entity (Current_Scope);
2669 -- There may have been several freezing points previously,
2670 -- for example object declarations or subprogram bodies, but
2671 -- at the end of a declarative part we check freezing from
2672 -- the beginning, even though entities may already be frozen,
2673 -- in order to perform visibility checks on delayed aspects.
2677 -- If the current scope is a generic subprogram body. Skip the
2678 -- generic formal parameters that are not frozen here.
2680 if Is_Subprogram (Current_Scope)
2681 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2682 N_Generic_Subprogram_Declaration
2683 and then Present (First_Entity (Current_Scope))
2685 while Is_Generic_Formal (Freeze_From) loop
2686 Next_Entity (Freeze_From);
2689 Freeze_All (Freeze_From, Decl);
2690 Freeze_From := Last_Entity (Current_Scope);
2693 -- For declarations in a subprogram body there is no issue
2694 -- with name resolution in aspect specifications.
2696 Freeze_All (First_Entity (Current_Scope), Decl);
2697 Freeze_From := Last_Entity (Current_Scope);
2700 -- Current scope is a package specification
2702 elsif Scope (Current_Scope) /= Standard_Standard
2703 and then not Is_Child_Unit (Current_Scope)
2704 and then No (Generic_Parent (Parent (L)))
2706 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2707 -- resolved at the end of the immediately enclosing declaration
2708 -- list (AI05-0183-1).
2712 elsif L /= Visible_Declarations (Parent (L))
2713 or else No (Private_Declarations (Parent (L)))
2714 or else Is_Empty_List (Private_Declarations (Parent (L)))
2718 -- End of a package declaration
2720 -- This is a freeze point because it is the end of a
2721 -- compilation unit.
2723 Freeze_All (First_Entity (Current_Scope), Decl);
2724 Freeze_From := Last_Entity (Current_Scope);
2726 -- At the end of the visible declarations the expressions in
2727 -- aspects of all entities declared so far must be resolved.
2728 -- The entities themselves might be frozen later, and the
2729 -- generated pragmas and attribute definition clauses analyzed
2730 -- in full at that point, but name resolution must take place
2732 -- In addition to being the proper semantics, this is mandatory
2733 -- within generic units, because global name capture requires
2734 -- those expressions to be analyzed, given that the generated
2735 -- pragmas do not appear in the original generic tree.
2737 elsif Serious_Errors_Detected = 0 then
2741 -- If next node is a body then freeze all types before the body.
2742 -- An exception occurs for some expander-generated bodies. If these
2743 -- are generated at places where in general language rules would not
2744 -- allow a freeze point, then we assume that the expander has
2745 -- explicitly checked that all required types are properly frozen,
2746 -- and we do not cause general freezing here. This special circuit
2747 -- is used when the encountered body is marked as having already
2750 -- In all other cases (bodies that come from source, and expander
2751 -- generated bodies that have not been analyzed yet), freeze all
2752 -- types now. Note that in the latter case, the expander must take
2753 -- care to attach the bodies at a proper place in the tree so as to
2754 -- not cause unwanted freezing at that point.
2756 -- It is also necessary to check for a case where both an expression
2757 -- function is used and the current scope depends on an incomplete
2758 -- private type from a library unit, otherwise premature freezing of
2759 -- the private type will occur.
2761 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2762 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2763 or else not Was_Expression_Function (Next_Decl))
2764 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2765 and then not Contains_Lib_Incomplete_Type
2768 -- When a controlled type is frozen, the expander generates stream
2769 -- and controlled-type support routines. If the freeze is caused
2770 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2771 -- expander will end up using the wrong version of these routines,
2772 -- as the body has not been processed yet. To remedy this, detect
2773 -- a late controlled primitive and create a proper spec for it.
2774 -- This ensures that the primitive will override its inherited
2775 -- counterpart before the freeze takes place.
2777 -- If the declaration we just processed is a body, do not attempt
2778 -- to examine Next_Decl as the late primitive idiom can only apply
2779 -- to the first encountered body.
2781 -- ??? A cleaner approach may be possible and/or this solution
2782 -- could be extended to general-purpose late primitives, TBD.
2784 if not Body_Seen and then not Is_Body (Decl) then
2787 if Nkind (Next_Decl) = N_Subprogram_Body then
2788 Handle_Late_Controlled_Primitive (Next_Decl);
2792 -- In ASIS mode, if the next declaration is a body, complete
2793 -- the analysis of declarations so far.
2794 -- Is this still needed???
2801 -- The generated body of an expression function does not freeze,
2802 -- unless it is a completion, in which case only the expression
2803 -- itself freezes. This is handled when the body itself is
2804 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2806 Freeze_All (Freeze_From, Decl);
2807 Freeze_From := Last_Entity (Current_Scope);
2813 -- Post-freezing actions
2816 Context := Parent (L);
2818 -- Certain contract annocations have forward visibility semantics and
2819 -- must be analyzed after all declarative items have been processed.
2820 -- This timing ensures that entities referenced by such contracts are
2823 -- Analyze the contract of an immediately enclosing package spec or
2824 -- body first because other contracts may depend on its information.
2826 if Nkind (Context) = N_Package_Body then
2827 Analyze_Package_Body_Contract (Defining_Entity (Context));
2829 elsif Nkind (Context) = N_Package_Specification then
2830 Analyze_Package_Contract (Defining_Entity (Context));
2833 -- Analyze the contracts of various constructs in the declarative
2836 Analyze_Contracts (L);
2838 if Nkind (Context) = N_Package_Body then
2840 -- Ensure that all abstract states and objects declared in the
2841 -- state space of a package body are utilized as constituents.
2843 Check_Unused_Body_States (Defining_Entity (Context));
2845 -- State refinements are visible up to the end of the package body
2846 -- declarations. Hide the state refinements from visibility to
2847 -- restore the original state conditions.
2849 Remove_Visible_Refinements (Corresponding_Spec (Context));
2850 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2852 elsif Nkind (Context) = N_Package_Specification then
2854 -- Partial state refinements are visible up to the end of the
2855 -- package spec declarations. Hide the partial state refinements
2856 -- from visibility to restore the original state conditions.
2858 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2861 -- Verify that all abstract states found in any package declared in
2862 -- the input declarative list have proper refinements. The check is
2863 -- performed only when the context denotes a block, entry, package,
2864 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2866 Check_State_Refinements (Context);
2868 -- Create the subprogram bodies which verify the run-time semantics
2869 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2870 -- types within the current declarative list. This ensures that all
2871 -- assertion expressions are preanalyzed and resolved at the end of
2872 -- the declarative part. Note that the resolution happens even when
2873 -- freezing does not take place.
2875 Build_Assertion_Bodies (L, Context);
2877 end Analyze_Declarations;
2879 -----------------------------------
2880 -- Analyze_Full_Type_Declaration --
2881 -----------------------------------
2883 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2884 Def : constant Node_Id := Type_Definition (N);
2885 Def_Id : constant Entity_Id := Defining_Identifier (N);
2889 Is_Remote : constant Boolean :=
2890 (Is_Remote_Types (Current_Scope)
2891 or else Is_Remote_Call_Interface (Current_Scope))
2892 and then not (In_Private_Part (Current_Scope)
2893 or else In_Package_Body (Current_Scope));
2895 procedure Check_Nonoverridable_Aspects;
2896 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2897 -- be overridden, and can only be confirmed on derivation.
2899 procedure Check_Ops_From_Incomplete_Type;
2900 -- If there is a tagged incomplete partial view of the type, traverse
2901 -- the primitives of the incomplete view and change the type of any
2902 -- controlling formals and result to indicate the full view. The
2903 -- primitives will be added to the full type's primitive operations
2904 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2905 -- is called from Process_Incomplete_Dependents).
2907 ----------------------------------
2908 -- Check_Nonoverridable_Aspects --
2909 ----------------------------------
2911 procedure Check_Nonoverridable_Aspects is
2912 function Get_Aspect_Spec
2914 Aspect_Name : Name_Id) return Node_Id;
2915 -- Check whether a list of aspect specifications includes an entry
2916 -- for a specific aspect. The list is either that of a partial or
2919 ---------------------
2920 -- Get_Aspect_Spec --
2921 ---------------------
2923 function Get_Aspect_Spec
2925 Aspect_Name : Name_Id) return Node_Id
2930 Spec := First (Specs);
2931 while Present (Spec) loop
2932 if Chars (Identifier (Spec)) = Aspect_Name then
2939 end Get_Aspect_Spec;
2943 Prev_Aspects : constant List_Id :=
2944 Aspect_Specifications (Parent (Def_Id));
2945 Par_Type : Entity_Id;
2946 Prev_Aspect : Node_Id;
2948 -- Start of processing for Check_Nonoverridable_Aspects
2951 -- Get parent type of derived type. Note that Prev is the entity in
2952 -- the partial declaration, but its contents are now those of full
2953 -- view, while Def_Id reflects the partial view.
2955 if Is_Private_Type (Def_Id) then
2956 Par_Type := Etype (Full_View (Def_Id));
2958 Par_Type := Etype (Def_Id);
2961 -- If there is an inherited Implicit_Dereference, verify that it is
2962 -- made explicit in the partial view.
2964 if Has_Discriminants (Base_Type (Par_Type))
2965 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2966 and then Present (Discriminant_Specifications (Parent (Prev)))
2967 and then Present (Get_Reference_Discriminant (Par_Type))
2970 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2974 (Discriminant_Specifications
2975 (Original_Node (Parent (Prev))))
2978 ("type does not inherit implicit dereference", Prev);
2981 -- If one of the views has the aspect specified, verify that it
2982 -- is consistent with that of the parent.
2985 Cur_Discr : constant Entity_Id :=
2986 Get_Reference_Discriminant (Prev);
2987 Par_Discr : constant Entity_Id :=
2988 Get_Reference_Discriminant (Par_Type);
2991 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
2993 ("aspect inconsistent with that of parent", N);
2996 -- Check that specification in partial view matches the
2997 -- inherited aspect. Compare names directly because aspect
2998 -- expression may not be analyzed.
3000 if Present (Prev_Aspect)
3001 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3002 and then Chars (Expression (Prev_Aspect)) /=
3006 ("aspect inconsistent with that of parent", N);
3012 -- TBD : other nonoverridable aspects.
3013 end Check_Nonoverridable_Aspects;
3015 ------------------------------------
3016 -- Check_Ops_From_Incomplete_Type --
3017 ------------------------------------
3019 procedure Check_Ops_From_Incomplete_Type is
3026 and then Ekind (Prev) = E_Incomplete_Type
3027 and then Is_Tagged_Type (Prev)
3028 and then Is_Tagged_Type (T)
3030 Elmt := First_Elmt (Primitive_Operations (Prev));
3031 while Present (Elmt) loop
3034 Formal := First_Formal (Op);
3035 while Present (Formal) loop
3036 if Etype (Formal) = Prev then
3037 Set_Etype (Formal, T);
3040 Next_Formal (Formal);
3043 if Etype (Op) = Prev then
3050 end Check_Ops_From_Incomplete_Type;
3052 -- Start of processing for Analyze_Full_Type_Declaration
3055 Prev := Find_Type_Name (N);
3057 -- The full view, if present, now points to the current type. If there
3058 -- is an incomplete partial view, set a link to it, to simplify the
3059 -- retrieval of primitive operations of the type.
3061 -- Ada 2005 (AI-50217): If the type was previously decorated when
3062 -- imported through a LIMITED WITH clause, it appears as incomplete
3063 -- but has no full view.
3065 if Ekind (Prev) = E_Incomplete_Type
3066 and then Present (Full_View (Prev))
3068 T := Full_View (Prev);
3069 Set_Incomplete_View (N, Parent (Prev));
3074 Set_Is_Pure (T, Is_Pure (Current_Scope));
3076 -- We set the flag Is_First_Subtype here. It is needed to set the
3077 -- corresponding flag for the Implicit class-wide-type created
3078 -- during tagged types processing.
3080 Set_Is_First_Subtype (T, True);
3082 -- Only composite types other than array types are allowed to have
3087 -- For derived types, the rule will be checked once we've figured
3088 -- out the parent type.
3090 when N_Derived_Type_Definition =>
3093 -- For record types, discriminants are allowed, unless we are in
3096 when N_Record_Definition =>
3097 if Present (Discriminant_Specifications (N)) then
3098 Check_SPARK_05_Restriction
3099 ("discriminant type is not allowed",
3101 (First (Discriminant_Specifications (N))));
3105 if Present (Discriminant_Specifications (N)) then
3107 ("elementary or array type cannot have discriminants",
3109 (First (Discriminant_Specifications (N))));
3113 -- Elaborate the type definition according to kind, and generate
3114 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3115 -- already done (this happens during the reanalysis that follows a call
3116 -- to the high level optimizer).
3118 if not Analyzed (T) then
3121 -- Set the SPARK mode from the current context
3123 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3124 Set_SPARK_Pragma_Inherited (T);
3127 when N_Access_To_Subprogram_Definition =>
3128 Access_Subprogram_Declaration (T, Def);
3130 -- If this is a remote access to subprogram, we must create the
3131 -- equivalent fat pointer type, and related subprograms.
3134 Process_Remote_AST_Declaration (N);
3137 -- Validate categorization rule against access type declaration
3138 -- usually a violation in Pure unit, Shared_Passive unit.
3140 Validate_Access_Type_Declaration (T, N);
3142 when N_Access_To_Object_Definition =>
3143 Access_Type_Declaration (T, Def);
3145 -- Validate categorization rule against access type declaration
3146 -- usually a violation in Pure unit, Shared_Passive unit.
3148 Validate_Access_Type_Declaration (T, N);
3150 -- If we are in a Remote_Call_Interface package and define a
3151 -- RACW, then calling stubs and specific stream attributes
3155 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3157 Add_RACW_Features (Def_Id);
3160 when N_Array_Type_Definition =>
3161 Array_Type_Declaration (T, Def);
3163 when N_Derived_Type_Definition =>
3164 Derived_Type_Declaration (T, N, T /= Def_Id);
3166 -- Inherit predicates from parent, and protect against illegal
3169 if Is_Type (T) and then Has_Predicates (T) then
3170 Set_Has_Predicates (Def_Id);
3173 -- Save the scenario for examination by the ABE Processing
3176 Record_Elaboration_Scenario (N);
3178 when N_Enumeration_Type_Definition =>
3179 Enumeration_Type_Declaration (T, Def);
3181 when N_Floating_Point_Definition =>
3182 Floating_Point_Type_Declaration (T, Def);
3184 when N_Decimal_Fixed_Point_Definition =>
3185 Decimal_Fixed_Point_Type_Declaration (T, Def);
3187 when N_Ordinary_Fixed_Point_Definition =>
3188 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3190 when N_Signed_Integer_Type_Definition =>
3191 Signed_Integer_Type_Declaration (T, Def);
3193 when N_Modular_Type_Definition =>
3194 Modular_Type_Declaration (T, Def);
3196 when N_Record_Definition =>
3197 Record_Type_Declaration (T, N, Prev);
3199 -- If declaration has a parse error, nothing to elaborate.
3205 raise Program_Error;
3209 if Etype (T) = Any_Type then
3213 -- Controlled type is not allowed in SPARK
3215 if Is_Visibly_Controlled (T) then
3216 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3219 -- Some common processing for all types
3221 Set_Depends_On_Private (T, Has_Private_Component (T));
3222 Check_Ops_From_Incomplete_Type;
3224 -- Both the declared entity, and its anonymous base type if one was
3225 -- created, need freeze nodes allocated.
3228 B : constant Entity_Id := Base_Type (T);
3231 -- In the case where the base type differs from the first subtype, we
3232 -- pre-allocate a freeze node, and set the proper link to the first
3233 -- subtype. Freeze_Entity will use this preallocated freeze node when
3234 -- it freezes the entity.
3236 -- This does not apply if the base type is a generic type, whose
3237 -- declaration is independent of the current derived definition.
3239 if B /= T and then not Is_Generic_Type (B) then
3240 Ensure_Freeze_Node (B);
3241 Set_First_Subtype_Link (Freeze_Node (B), T);
3244 -- A type that is imported through a limited_with clause cannot
3245 -- generate any code, and thus need not be frozen. However, an access
3246 -- type with an imported designated type needs a finalization list,
3247 -- which may be referenced in some other package that has non-limited
3248 -- visibility on the designated type. Thus we must create the
3249 -- finalization list at the point the access type is frozen, to
3250 -- prevent unsatisfied references at link time.
3252 if not From_Limited_With (T) or else Is_Access_Type (T) then
3253 Set_Has_Delayed_Freeze (T);
3257 -- Case where T is the full declaration of some private type which has
3258 -- been swapped in Defining_Identifier (N).
3260 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3261 Process_Full_View (N, T, Def_Id);
3263 -- Record the reference. The form of this is a little strange, since
3264 -- the full declaration has been swapped in. So the first parameter
3265 -- here represents the entity to which a reference is made which is
3266 -- the "real" entity, i.e. the one swapped in, and the second
3267 -- parameter provides the reference location.
3269 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3270 -- since we don't want a complaint about the full type being an
3271 -- unwanted reference to the private type
3274 B : constant Boolean := Has_Pragma_Unreferenced (T);
3276 Set_Has_Pragma_Unreferenced (T, False);
3277 Generate_Reference (T, T, 'c');
3278 Set_Has_Pragma_Unreferenced (T, B);
3281 Set_Completion_Referenced (Def_Id);
3283 -- For completion of incomplete type, process incomplete dependents
3284 -- and always mark the full type as referenced (it is the incomplete
3285 -- type that we get for any real reference).
3287 elsif Ekind (Prev) = E_Incomplete_Type then
3288 Process_Incomplete_Dependents (N, T, Prev);
3289 Generate_Reference (Prev, Def_Id, 'c');
3290 Set_Completion_Referenced (Def_Id);
3292 -- If not private type or incomplete type completion, this is a real
3293 -- definition of a new entity, so record it.
3296 Generate_Definition (Def_Id);
3299 -- Propagate any pending access types whose finalization masters need to
3300 -- be fully initialized from the partial to the full view. Guard against
3301 -- an illegal full view that remains unanalyzed.
3303 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3304 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3307 if Chars (Scope (Def_Id)) = Name_System
3308 and then Chars (Def_Id) = Name_Address
3309 and then In_Predefined_Unit (N)
3311 Set_Is_Descendant_Of_Address (Def_Id);
3312 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3313 Set_Is_Descendant_Of_Address (Prev);
3316 Set_Optimize_Alignment_Flags (Def_Id);
3317 Check_Eliminated (Def_Id);
3319 -- If the declaration is a completion and aspects are present, apply
3320 -- them to the entity for the type which is currently the partial
3321 -- view, but which is the one that will be frozen.
3323 if Has_Aspects (N) then
3325 -- In most cases the partial view is a private type, and both views
3326 -- appear in different declarative parts. In the unusual case where
3327 -- the partial view is incomplete, perform the analysis on the
3328 -- full view, to prevent freezing anomalies with the corresponding
3329 -- class-wide type, which otherwise might be frozen before the
3330 -- dispatch table is built.
3333 and then Ekind (Prev) /= E_Incomplete_Type
3335 Analyze_Aspect_Specifications (N, Prev);
3340 Analyze_Aspect_Specifications (N, Def_Id);
3344 if Is_Derived_Type (Prev)
3345 and then Def_Id /= Prev
3347 Check_Nonoverridable_Aspects;
3349 end Analyze_Full_Type_Declaration;
3351 ----------------------------------
3352 -- Analyze_Incomplete_Type_Decl --
3353 ----------------------------------
3355 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3356 F : constant Boolean := Is_Pure (Current_Scope);
3360 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3362 Generate_Definition (Defining_Identifier (N));
3364 -- Process an incomplete declaration. The identifier must not have been
3365 -- declared already in the scope. However, an incomplete declaration may
3366 -- appear in the private part of a package, for a private type that has
3367 -- already been declared.
3369 -- In this case, the discriminants (if any) must match
3371 T := Find_Type_Name (N);
3373 Set_Ekind (T, E_Incomplete_Type);
3375 Set_Is_First_Subtype (T);
3376 Init_Size_Align (T);
3378 -- Set the SPARK mode from the current context
3380 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3381 Set_SPARK_Pragma_Inherited (T);
3383 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3384 -- incomplete types.
3386 if Tagged_Present (N) then
3387 Set_Is_Tagged_Type (T, True);
3388 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3389 Make_Class_Wide_Type (T);
3390 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3393 Set_Stored_Constraint (T, No_Elist);
3395 if Present (Discriminant_Specifications (N)) then
3397 Process_Discriminants (N);
3401 -- If the type has discriminants, nontrivial subtypes may be declared
3402 -- before the full view of the type. The full views of those subtypes
3403 -- will be built after the full view of the type.
3405 Set_Private_Dependents (T, New_Elmt_List);
3407 end Analyze_Incomplete_Type_Decl;
3409 -----------------------------------
3410 -- Analyze_Interface_Declaration --
3411 -----------------------------------
3413 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3414 CW : constant Entity_Id := Class_Wide_Type (T);
3417 Set_Is_Tagged_Type (T);
3418 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3420 Set_Is_Limited_Record (T, Limited_Present (Def)
3421 or else Task_Present (Def)
3422 or else Protected_Present (Def)
3423 or else Synchronized_Present (Def));
3425 -- Type is abstract if full declaration carries keyword, or if previous
3426 -- partial view did.
3428 Set_Is_Abstract_Type (T);
3429 Set_Is_Interface (T);
3431 -- Type is a limited interface if it includes the keyword limited, task,
3432 -- protected, or synchronized.
3434 Set_Is_Limited_Interface
3435 (T, Limited_Present (Def)
3436 or else Protected_Present (Def)
3437 or else Synchronized_Present (Def)
3438 or else Task_Present (Def));
3440 Set_Interfaces (T, New_Elmt_List);
3441 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3443 -- Complete the decoration of the class-wide entity if it was already
3444 -- built (i.e. during the creation of the limited view)
3446 if Present (CW) then
3447 Set_Is_Interface (CW);
3448 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3451 -- Check runtime support for synchronized interfaces
3453 if (Is_Task_Interface (T)
3454 or else Is_Protected_Interface (T)
3455 or else Is_Synchronized_Interface (T))
3456 and then not RTE_Available (RE_Select_Specific_Data)
3458 Error_Msg_CRT ("synchronized interfaces", T);
3460 end Analyze_Interface_Declaration;
3462 -----------------------------
3463 -- Analyze_Itype_Reference --
3464 -----------------------------
3466 -- Nothing to do. This node is placed in the tree only for the benefit of
3467 -- back end processing, and has no effect on the semantic processing.
3469 procedure Analyze_Itype_Reference (N : Node_Id) is
3471 pragma Assert (Is_Itype (Itype (N)));
3473 end Analyze_Itype_Reference;
3475 --------------------------------
3476 -- Analyze_Number_Declaration --
3477 --------------------------------
3479 procedure Analyze_Number_Declaration (N : Node_Id) is
3480 E : constant Node_Id := Expression (N);
3481 Id : constant Entity_Id := Defining_Identifier (N);
3482 Index : Interp_Index;
3487 Generate_Definition (Id);
3490 -- This is an optimization of a common case of an integer literal
3492 if Nkind (E) = N_Integer_Literal then
3493 Set_Is_Static_Expression (E, True);
3494 Set_Etype (E, Universal_Integer);
3496 Set_Etype (Id, Universal_Integer);
3497 Set_Ekind (Id, E_Named_Integer);
3498 Set_Is_Frozen (Id, True);
3500 Set_Debug_Info_Needed (Id);
3504 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3506 -- Process expression, replacing error by integer zero, to avoid
3507 -- cascaded errors or aborts further along in the processing
3509 -- Replace Error by integer zero, which seems least likely to cause
3513 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3514 Set_Error_Posted (E);
3519 -- Verify that the expression is static and numeric. If
3520 -- the expression is overloaded, we apply the preference
3521 -- rule that favors root numeric types.
3523 if not Is_Overloaded (E) then
3525 if Has_Dynamic_Predicate_Aspect (T) then
3527 ("subtype has dynamic predicate, "
3528 & "not allowed in number declaration", N);
3534 Get_First_Interp (E, Index, It);
3535 while Present (It.Typ) loop
3536 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3537 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3539 if T = Any_Type then
3542 elsif It.Typ = Universal_Real
3544 It.Typ = Universal_Integer
3546 -- Choose universal interpretation over any other
3553 Get_Next_Interp (Index, It);
3557 if Is_Integer_Type (T) then
3559 Set_Etype (Id, Universal_Integer);
3560 Set_Ekind (Id, E_Named_Integer);
3562 elsif Is_Real_Type (T) then
3564 -- Because the real value is converted to universal_real, this is a
3565 -- legal context for a universal fixed expression.
3567 if T = Universal_Fixed then
3569 Loc : constant Source_Ptr := Sloc (N);
3570 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3572 New_Occurrence_Of (Universal_Real, Loc),
3573 Expression => Relocate_Node (E));
3580 elsif T = Any_Fixed then
3581 Error_Msg_N ("illegal context for mixed mode operation", E);
3583 -- Expression is of the form : universal_fixed * integer. Try to
3584 -- resolve as universal_real.
3586 T := Universal_Real;
3591 Set_Etype (Id, Universal_Real);
3592 Set_Ekind (Id, E_Named_Real);
3595 Wrong_Type (E, Any_Numeric);
3599 Set_Ekind (Id, E_Constant);
3600 Set_Never_Set_In_Source (Id, True);
3601 Set_Is_True_Constant (Id, True);
3605 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3606 Set_Etype (E, Etype (Id));
3609 if not Is_OK_Static_Expression (E) then
3610 Flag_Non_Static_Expr
3611 ("non-static expression used in number declaration!", E);
3612 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3613 Set_Etype (E, Any_Type);
3616 Analyze_Dimension (N);
3617 end Analyze_Number_Declaration;
3619 --------------------------------
3620 -- Analyze_Object_Declaration --
3621 --------------------------------
3623 -- WARNING: This routine manages Ghost regions. Return statements must be
3624 -- replaced by gotos which jump to the end of the routine and restore the
3627 procedure Analyze_Object_Declaration (N : Node_Id) is
3628 Loc : constant Source_Ptr := Sloc (N);
3629 Id : constant Entity_Id := Defining_Identifier (N);
3630 Next_Decl : constant Node_Id := Next (N);
3635 E : Node_Id := Expression (N);
3636 -- E is set to Expression (N) throughout this routine. When Expression
3637 -- (N) is modified, E is changed accordingly.
3639 Prev_Entity : Entity_Id := Empty;
3641 procedure Check_Dynamic_Object (Typ : Entity_Id);
3642 -- A library-level object with nonstatic discriminant constraints may
3643 -- require dynamic allocation. The declaration is illegal if the
3644 -- profile includes the restriction No_Implicit_Heap_Allocations.
3646 procedure Check_For_Null_Excluding_Components
3647 (Obj_Typ : Entity_Id;
3648 Obj_Decl : Node_Id);
3649 -- Verify that each null-excluding component of object declaration
3650 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3651 -- a compile-time warning if this is not the case.
3653 function Count_Tasks (T : Entity_Id) return Uint;
3654 -- This function is called when a non-generic library level object of a
3655 -- task type is declared. Its function is to count the static number of
3656 -- tasks declared within the type (it is only called if Has_Task is set
3657 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3658 -- or a variant record type is encountered, Check_Restriction is called
3659 -- indicating the count is unknown.
3661 function Delayed_Aspect_Present return Boolean;
3662 -- If the declaration has an expression that is an aggregate, and it
3663 -- has aspects that require delayed analysis, the resolution of the
3664 -- aggregate must be deferred to the freeze point of the object. This
3665 -- special processing was created for address clauses, but it must
3666 -- also apply to Alignment. This must be done before the aspect
3667 -- specifications are analyzed because we must handle the aggregate
3668 -- before the analysis of the object declaration is complete.
3670 -- Any other relevant delayed aspects on object declarations ???
3672 --------------------------
3673 -- Check_Dynamic_Object --
3674 --------------------------
3676 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3678 Obj_Type : Entity_Id;
3683 if Is_Private_Type (Obj_Type)
3684 and then Present (Full_View (Obj_Type))
3686 Obj_Type := Full_View (Obj_Type);
3689 if Known_Static_Esize (Obj_Type) then
3693 if Restriction_Active (No_Implicit_Heap_Allocations)
3694 and then Expander_Active
3695 and then Has_Discriminants (Obj_Type)
3697 Comp := First_Component (Obj_Type);
3698 while Present (Comp) loop
3699 if Known_Static_Esize (Etype (Comp))
3700 or else Size_Known_At_Compile_Time (Etype (Comp))
3704 elsif not Discriminated_Size (Comp)
3705 and then Comes_From_Source (Comp)
3708 ("component& of non-static size will violate restriction "
3709 & "No_Implicit_Heap_Allocation?", N, Comp);
3711 elsif Is_Record_Type (Etype (Comp)) then
3712 Check_Dynamic_Object (Etype (Comp));
3715 Next_Component (Comp);
3718 end Check_Dynamic_Object;
3720 -----------------------------------------
3721 -- Check_For_Null_Excluding_Components --
3722 -----------------------------------------
3724 procedure Check_For_Null_Excluding_Components
3725 (Obj_Typ : Entity_Id;
3728 procedure Check_Component
3729 (Comp_Typ : Entity_Id;
3730 Comp_Decl : Node_Id := Empty;
3731 Array_Comp : Boolean := False);
3732 -- Apply a compile-time null-exclusion check on a component denoted
3733 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3734 -- subcomponents (if any).
3736 ---------------------
3737 -- Check_Component --
3738 ---------------------
3740 procedure Check_Component
3741 (Comp_Typ : Entity_Id;
3742 Comp_Decl : Node_Id := Empty;
3743 Array_Comp : Boolean := False)
3749 -- Do not consider internally-generated components or those that
3750 -- are already initialized.
3752 if Present (Comp_Decl)
3753 and then (not Comes_From_Source (Comp_Decl)
3754 or else Present (Expression (Comp_Decl)))
3759 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3760 and then Present (Full_View (Comp_Typ))
3762 T := Full_View (Comp_Typ);
3767 -- Verify a component of a null-excluding access type
3769 if Is_Access_Type (T)
3770 and then Can_Never_Be_Null (T)
3772 if Comp_Decl = Obj_Decl then
3773 Null_Exclusion_Static_Checks
3776 Array_Comp => Array_Comp);
3779 Null_Exclusion_Static_Checks
3782 Array_Comp => Array_Comp);
3785 -- Check array components
3787 elsif Is_Array_Type (T) then
3789 -- There is no suitable component when the object is of an
3790 -- array type. However, a namable component may appear at some
3791 -- point during the recursive inspection, but not at the top
3792 -- level. At the top level just indicate array component case.
3794 if Comp_Decl = Obj_Decl then
3795 Check_Component (Component_Type (T), Array_Comp => True);
3797 Check_Component (Component_Type (T), Comp_Decl);
3800 -- Verify all components of type T
3802 -- Note: No checks are performed on types with discriminants due
3803 -- to complexities involving variants. ???
3805 elsif (Is_Concurrent_Type (T)
3806 or else Is_Incomplete_Or_Private_Type (T)
3807 or else Is_Record_Type (T))
3808 and then not Has_Discriminants (T)
3810 Comp := First_Component (T);
3811 while Present (Comp) loop
3812 Check_Component (Etype (Comp), Parent (Comp));
3814 Next_Component (Comp);
3817 end Check_Component;
3819 -- Start processing for Check_For_Null_Excluding_Components
3822 Check_Component (Obj_Typ, Obj_Decl);
3823 end Check_For_Null_Excluding_Components;
3829 function Count_Tasks (T : Entity_Id) return Uint is
3835 if Is_Task_Type (T) then
3838 elsif Is_Record_Type (T) then
3839 if Has_Discriminants (T) then
3840 Check_Restriction (Max_Tasks, N);
3845 C := First_Component (T);
3846 while Present (C) loop
3847 V := V + Count_Tasks (Etype (C));
3854 elsif Is_Array_Type (T) then
3855 X := First_Index (T);
3856 V := Count_Tasks (Component_Type (T));
3857 while Present (X) loop
3860 if not Is_OK_Static_Subtype (C) then
3861 Check_Restriction (Max_Tasks, N);
3864 V := V * (UI_Max (Uint_0,
3865 Expr_Value (Type_High_Bound (C)) -
3866 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3879 ----------------------------
3880 -- Delayed_Aspect_Present --
3881 ----------------------------
3883 function Delayed_Aspect_Present return Boolean is
3888 if Present (Aspect_Specifications (N)) then
3889 A := First (Aspect_Specifications (N));
3890 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3891 while Present (A) loop
3892 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3894 -- Set flag on object entity, for later processing at
3895 -- the freeze point.
3897 Set_Has_Delayed_Aspects (Id);
3906 end Delayed_Aspect_Present;
3910 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3911 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3912 -- Save the Ghost-related attributes to restore on exit
3914 Related_Id : Entity_Id;
3915 Full_View_Present : Boolean := False;
3917 -- Start of processing for Analyze_Object_Declaration
3920 -- There are three kinds of implicit types generated by an
3921 -- object declaration:
3923 -- 1. Those generated by the original Object Definition
3925 -- 2. Those generated by the Expression
3927 -- 3. Those used to constrain the Object Definition with the
3928 -- expression constraints when the definition is unconstrained.
3930 -- They must be generated in this order to avoid order of elaboration
3931 -- issues. Thus the first step (after entering the name) is to analyze
3932 -- the object definition.
3934 if Constant_Present (N) then
3935 Prev_Entity := Current_Entity_In_Scope (Id);
3937 if Present (Prev_Entity)
3939 -- If the homograph is an implicit subprogram, it is overridden
3940 -- by the current declaration.
3942 ((Is_Overloadable (Prev_Entity)
3943 and then Is_Inherited_Operation (Prev_Entity))
3945 -- The current object is a discriminal generated for an entry
3946 -- family index. Even though the index is a constant, in this
3947 -- particular context there is no true constant redeclaration.
3948 -- Enter_Name will handle the visibility.
3951 (Is_Discriminal (Id)
3952 and then Ekind (Discriminal_Link (Id)) =
3953 E_Entry_Index_Parameter)
3955 -- The current object is the renaming for a generic declared
3956 -- within the instance.
3959 (Ekind (Prev_Entity) = E_Package
3960 and then Nkind (Parent (Prev_Entity)) =
3961 N_Package_Renaming_Declaration
3962 and then not Comes_From_Source (Prev_Entity)
3964 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3966 -- The entity may be a homonym of a private component of the
3967 -- enclosing protected object, for which we create a local
3968 -- renaming declaration. The declaration is legal, even if
3969 -- useless when it just captures that component.
3972 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3973 and then Nkind (Parent (Prev_Entity)) =
3974 N_Object_Renaming_Declaration))
3976 Prev_Entity := Empty;
3980 if Present (Prev_Entity) then
3982 -- The object declaration is Ghost when it completes a deferred Ghost
3985 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
3987 Constant_Redeclaration (Id, N, T);
3989 Generate_Reference (Prev_Entity, Id, 'c');
3990 Set_Completion_Referenced (Id);
3992 if Error_Posted (N) then
3994 -- Type mismatch or illegal redeclaration; do not analyze
3995 -- expression to avoid cascaded errors.
3997 T := Find_Type_Of_Object (Object_Definition (N), N);
3999 Set_Ekind (Id, E_Variable);
4003 -- In the normal case, enter identifier at the start to catch premature
4004 -- usage in the initialization expression.
4007 Generate_Definition (Id);
4010 Mark_Coextensions (N, Object_Definition (N));
4012 T := Find_Type_Of_Object (Object_Definition (N), N);
4014 if Nkind (Object_Definition (N)) = N_Access_Definition
4016 (Access_To_Subprogram_Definition (Object_Definition (N)))
4017 and then Protected_Present
4018 (Access_To_Subprogram_Definition (Object_Definition (N)))
4020 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4023 if Error_Posted (Id) then
4025 Set_Ekind (Id, E_Variable);
4030 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4031 -- out some static checks.
4033 if Ada_Version >= Ada_2005 then
4035 -- In case of aggregates we must also take care of the correct
4036 -- initialization of nested aggregates bug this is done at the
4037 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4039 if Can_Never_Be_Null (T) then
4040 if Present (Expression (N))
4041 and then Nkind (Expression (N)) = N_Aggregate
4047 Save_Typ : constant Entity_Id := Etype (Id);
4049 Set_Etype (Id, T); -- Temp. decoration for static checks
4050 Null_Exclusion_Static_Checks (N);
4051 Set_Etype (Id, Save_Typ);
4055 -- We might be dealing with an object of a composite type containing
4056 -- null-excluding components without an aggregate, so we must verify
4057 -- that such components have default initialization.
4060 Check_For_Null_Excluding_Components (T, N);
4064 -- Object is marked pure if it is in a pure scope
4066 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4068 -- If deferred constant, make sure context is appropriate. We detect
4069 -- a deferred constant as a constant declaration with no expression.
4070 -- A deferred constant can appear in a package body if its completion
4071 -- is by means of an interface pragma.
4073 if Constant_Present (N) and then No (E) then
4075 -- A deferred constant may appear in the declarative part of the
4076 -- following constructs:
4080 -- extended return statements
4083 -- subprogram bodies
4086 -- When declared inside a package spec, a deferred constant must be
4087 -- completed by a full constant declaration or pragma Import. In all
4088 -- other cases, the only proper completion is pragma Import. Extended
4089 -- return statements are flagged as invalid contexts because they do
4090 -- not have a declarative part and so cannot accommodate the pragma.
4092 if Ekind (Current_Scope) = E_Return_Statement then
4094 ("invalid context for deferred constant declaration (RM 7.4)",
4097 ("\declaration requires an initialization expression",
4099 Set_Constant_Present (N, False);
4101 -- In Ada 83, deferred constant must be of private type
4103 elsif not Is_Private_Type (T) then
4104 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4106 ("(Ada 83) deferred constant must be private type", N);
4110 -- If not a deferred constant, then the object declaration freezes
4111 -- its type, unless the object is of an anonymous type and has delayed
4112 -- aspects. In that case the type is frozen when the object itself is.
4115 Check_Fully_Declared (T, N);
4117 if Has_Delayed_Aspects (Id)
4118 and then Is_Array_Type (T)
4119 and then Is_Itype (T)
4121 Set_Has_Delayed_Freeze (T);
4123 Freeze_Before (N, T);
4127 -- If the object was created by a constrained array definition, then
4128 -- set the link in both the anonymous base type and anonymous subtype
4129 -- that are built to represent the array type to point to the object.
4131 if Nkind (Object_Definition (Declaration_Node (Id))) =
4132 N_Constrained_Array_Definition
4134 Set_Related_Array_Object (T, Id);
4135 Set_Related_Array_Object (Base_Type (T), Id);
4138 -- Special checks for protected objects not at library level
4140 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4141 Check_Restriction (No_Local_Protected_Objects, Id);
4143 -- Protected objects with interrupt handlers must be at library level
4145 -- Ada 2005: This test is not needed (and the corresponding clause
4146 -- in the RM is removed) because accessibility checks are sufficient
4147 -- to make handlers not at the library level illegal.
4149 -- AI05-0303: The AI is in fact a binding interpretation, and thus
4150 -- applies to the '95 version of the language as well.
4152 if Is_Protected_Type (T)
4153 and then Has_Interrupt_Handler (T)
4154 and then Ada_Version < Ada_95
4157 ("interrupt object can only be declared at library level", Id);
4161 -- Check for violation of No_Local_Timing_Events
4163 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4164 Check_Restriction (No_Local_Timing_Events, Id);
4167 -- The actual subtype of the object is the nominal subtype, unless
4168 -- the nominal one is unconstrained and obtained from the expression.
4172 -- These checks should be performed before the initialization expression
4173 -- is considered, so that the Object_Definition node is still the same
4174 -- as in source code.
4176 -- In SPARK, the nominal subtype is always given by a subtype mark
4177 -- and must not be unconstrained. (The only exception to this is the
4178 -- acceptance of declarations of constants of type String.)
4180 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
4182 Check_SPARK_05_Restriction
4183 ("subtype mark required", Object_Definition (N));
4185 elsif Is_Array_Type (T)
4186 and then not Is_Constrained (T)
4187 and then T /= Standard_String
4189 Check_SPARK_05_Restriction
4190 ("subtype mark of constrained type expected",
4191 Object_Definition (N));
4194 if Is_Library_Level_Entity (Id) then
4195 Check_Dynamic_Object (T);
4198 -- There are no aliased objects in SPARK
4200 if Aliased_Present (N) then
4201 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
4204 -- Process initialization expression if present and not in error
4206 if Present (E) and then E /= Error then
4208 -- Generate an error in case of CPP class-wide object initialization.
4209 -- Required because otherwise the expansion of the class-wide
4210 -- assignment would try to use 'size to initialize the object
4211 -- (primitive that is not available in CPP tagged types).
4213 if Is_Class_Wide_Type (Act_T)
4215 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4217 (Present (Full_View (Root_Type (Etype (Act_T))))
4219 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4222 ("predefined assignment not available for 'C'P'P tagged types",
4226 Mark_Coextensions (N, E);
4229 -- In case of errors detected in the analysis of the expression,
4230 -- decorate it with the expected type to avoid cascaded errors
4232 if No (Etype (E)) then
4236 -- If an initialization expression is present, then we set the
4237 -- Is_True_Constant flag. It will be reset if this is a variable
4238 -- and it is indeed modified.
4240 Set_Is_True_Constant (Id, True);
4242 -- If we are analyzing a constant declaration, set its completion
4243 -- flag after analyzing and resolving the expression.
4245 if Constant_Present (N) then
4246 Set_Has_Completion (Id);
4249 -- Set type and resolve (type may be overridden later on). Note:
4250 -- Ekind (Id) must still be E_Void at this point so that incorrect
4251 -- early usage within E is properly diagnosed.
4255 -- If the expression is an aggregate we must look ahead to detect
4256 -- the possible presence of an address clause, and defer resolution
4257 -- and expansion of the aggregate to the freeze point of the entity.
4259 -- This is not always legal because the aggregate may contain other
4260 -- references that need freezing, e.g. references to other entities
4261 -- with address clauses. In any case, when compiling with -gnatI the
4262 -- presence of the address clause must be ignored.
4264 if Comes_From_Source (N)
4265 and then Expander_Active
4266 and then Nkind (E) = N_Aggregate
4268 ((Present (Following_Address_Clause (N))
4269 and then not Ignore_Rep_Clauses)
4270 or else Delayed_Aspect_Present)
4274 -- If the aggregate is limited it will be built in place, and its
4275 -- expansion is deferred until the object declaration is expanded.
4277 -- This is also required when generating C code to ensure that an
4278 -- object with an alignment or address clause can be initialized
4279 -- by means of component by component assignments.
4281 if Is_Limited_Type (T) or else Modify_Tree_For_C then
4282 Set_Expansion_Delayed (E);
4286 -- If the expression is a formal that is a "subprogram pointer"
4287 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4288 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4289 -- the corresponding check, as is done for assignments.
4291 if Is_Entity_Name (E)
4292 and then Present (Entity (E))
4293 and then Is_Formal (Entity (E))
4295 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4296 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4298 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4304 -- No further action needed if E is a call to an inlined function
4305 -- which returns an unconstrained type and it has been expanded into
4306 -- a procedure call. In that case N has been replaced by an object
4307 -- declaration without initializing expression and it has been
4308 -- analyzed (see Expand_Inlined_Call).
4310 if Back_End_Inlining
4311 and then Expander_Active
4312 and then Nkind (E) = N_Function_Call
4313 and then Nkind (Name (E)) in N_Has_Entity
4314 and then Is_Inlined (Entity (Name (E)))
4315 and then not Is_Constrained (Etype (E))
4316 and then Analyzed (N)
4317 and then No (Expression (N))
4322 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4323 -- node (which was marked already-analyzed), we need to set the type
4324 -- to something other than Any_Access in order to keep gigi happy.
4326 if Etype (E) = Any_Access then
4330 -- If the object is an access to variable, the initialization
4331 -- expression cannot be an access to constant.
4333 if Is_Access_Type (T)
4334 and then not Is_Access_Constant (T)
4335 and then Is_Access_Type (Etype (E))
4336 and then Is_Access_Constant (Etype (E))
4339 ("access to variable cannot be initialized with an "
4340 & "access-to-constant expression", E);
4343 if not Assignment_OK (N) then
4344 Check_Initialization (T, E);
4347 Check_Unset_Reference (E);
4349 -- If this is a variable, then set current value. If this is a
4350 -- declared constant of a scalar type with a static expression,
4351 -- indicate that it is always valid.
4353 if not Constant_Present (N) then
4354 if Compile_Time_Known_Value (E) then
4355 Set_Current_Value (Id, E);
4358 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4359 Set_Is_Known_Valid (Id);
4361 -- If it is a constant initialized with a valid nonstatic entity,
4362 -- the constant is known valid as well, and can inherit the subtype
4363 -- of the entity if it is a subtype of the given type. This info
4364 -- is preserved on the actual subtype of the constant.
4366 elsif Is_Scalar_Type (T)
4367 and then Is_Entity_Name (E)
4368 and then Is_Known_Valid (Entity (E))
4369 and then In_Subrange_Of (Etype (Entity (E)), T)
4371 Set_Is_Known_Valid (Id);
4372 Set_Ekind (Id, E_Constant);
4373 Set_Actual_Subtype (Id, Etype (Entity (E)));
4376 -- Deal with setting of null flags
4378 if Is_Access_Type (T) then
4379 if Known_Non_Null (E) then
4380 Set_Is_Known_Non_Null (Id, True);
4381 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4382 Set_Is_Known_Null (Id, True);
4386 -- Check incorrect use of dynamically tagged expressions
4388 if Is_Tagged_Type (T) then
4389 Check_Dynamically_Tagged_Expression
4395 Apply_Scalar_Range_Check (E, T);
4396 Apply_Static_Length_Check (E, T);
4398 if Nkind (Original_Node (N)) = N_Object_Declaration
4399 and then Comes_From_Source (Original_Node (N))
4401 -- Only call test if needed
4403 and then Restriction_Check_Required (SPARK_05)
4404 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4406 Check_SPARK_05_Restriction
4407 ("initialization expression is not appropriate", E);
4410 -- A formal parameter of a specific tagged type whose related
4411 -- subprogram is subject to pragma Extensions_Visible with value
4412 -- "False" cannot be implicitly converted to a class-wide type by
4413 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4414 -- not consider internally generated expressions.
4416 if Is_Class_Wide_Type (T)
4417 and then Comes_From_Source (E)
4418 and then Is_EVF_Expression (E)
4421 ("formal parameter cannot be implicitly converted to "
4422 & "class-wide type when Extensions_Visible is False", E);
4426 -- If the No_Streams restriction is set, check that the type of the
4427 -- object is not, and does not contain, any subtype derived from
4428 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4429 -- Has_Stream just for efficiency reasons. There is no point in
4430 -- spending time on a Has_Stream check if the restriction is not set.
4432 if Restriction_Check_Required (No_Streams) then
4433 if Has_Stream (T) then
4434 Check_Restriction (No_Streams, N);
4438 -- Deal with predicate check before we start to do major rewriting. It
4439 -- is OK to initialize and then check the initialized value, since the
4440 -- object goes out of scope if we get a predicate failure. Note that we
4441 -- do this in the analyzer and not the expander because the analyzer
4442 -- does some substantial rewriting in some cases.
4444 -- We need a predicate check if the type has predicates that are not
4445 -- ignored, and if either there is an initializing expression, or for
4446 -- default initialization when we have at least one case of an explicit
4447 -- default initial value and then this is not an internal declaration
4448 -- whose initialization comes later (as for an aggregate expansion).
4449 -- If expression is an aggregate it may be expanded into assignments
4450 -- and the declaration itself is marked with No_Initialization, but
4451 -- the predicate still applies.
4453 if not Suppress_Assignment_Checks (N)
4454 and then Present (Predicate_Function (T))
4455 and then not Predicates_Ignored (T)
4457 (not No_Initialization (N)
4458 or else (Present (E) and then Nkind (E) = N_Aggregate))
4462 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4464 -- If the type has a static predicate and the expression is known at
4465 -- compile time, see if the expression satisfies the predicate.
4468 Check_Expression_Against_Static_Predicate (E, T);
4471 -- If the type is a null record and there is no explicit initial
4472 -- expression, no predicate check applies.
4474 if No (E) and then Is_Null_Record_Type (T) then
4477 -- Do not generate a predicate check if the initialization expression
4478 -- is a type conversion because the conversion has been subjected to
4479 -- the same check. This is a small optimization which avoid redundant
4482 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4486 -- The check must be inserted after the expanded aggregate
4487 -- expansion code, if any.
4490 Check : constant Node_Id :=
4491 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4494 if No (Next_Decl) then
4495 Append_To (List_Containing (N), Check);
4497 Insert_Before (Next_Decl, Check);
4503 -- Case of unconstrained type
4505 if not Is_Definite_Subtype (T) then
4507 -- In SPARK, a declaration of unconstrained type is allowed
4508 -- only for constants of type string.
4510 if Is_String_Type (T) and then not Constant_Present (N) then
4511 Check_SPARK_05_Restriction
4512 ("declaration of object of unconstrained type not allowed", N);
4515 -- Nothing to do in deferred constant case
4517 if Constant_Present (N) and then No (E) then
4520 -- Case of no initialization present
4523 if No_Initialization (N) then
4526 elsif Is_Class_Wide_Type (T) then
4528 ("initialization required in class-wide declaration ", N);
4532 ("unconstrained subtype not allowed (need initialization)",
4533 Object_Definition (N));
4535 if Is_Record_Type (T) and then Has_Discriminants (T) then
4537 ("\provide initial value or explicit discriminant values",
4538 Object_Definition (N));
4541 ("\or give default discriminant values for type&",
4542 Object_Definition (N), T);
4544 elsif Is_Array_Type (T) then
4546 ("\provide initial value or explicit array bounds",
4547 Object_Definition (N));
4551 -- Case of initialization present but in error. Set initial
4552 -- expression as absent (but do not make above complaints)
4554 elsif E = Error then
4555 Set_Expression (N, Empty);
4558 -- Case of initialization present
4561 -- Check restrictions in Ada 83
4563 if not Constant_Present (N) then
4565 -- Unconstrained variables not allowed in Ada 83 mode
4567 if Ada_Version = Ada_83
4568 and then Comes_From_Source (Object_Definition (N))
4571 ("(Ada 83) unconstrained variable not allowed",
4572 Object_Definition (N));
4576 -- Now we constrain the variable from the initializing expression
4578 -- If the expression is an aggregate, it has been expanded into
4579 -- individual assignments. Retrieve the actual type from the
4580 -- expanded construct.
4582 if Is_Array_Type (T)
4583 and then No_Initialization (N)
4584 and then Nkind (Original_Node (E)) = N_Aggregate
4588 -- In case of class-wide interface object declarations we delay
4589 -- the generation of the equivalent record type declarations until
4590 -- its expansion because there are cases in they are not required.
4592 elsif Is_Interface (T) then
4595 -- If the type is an unchecked union, no subtype can be built from
4596 -- the expression. Rewrite declaration as a renaming, which the
4597 -- back-end can handle properly. This is a rather unusual case,
4598 -- because most unchecked_union declarations have default values
4599 -- for discriminants and are thus not indefinite.
4601 elsif Is_Unchecked_Union (T) then
4602 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4603 Set_Ekind (Id, E_Constant);
4605 Set_Ekind (Id, E_Variable);
4609 Make_Object_Renaming_Declaration (Loc,
4610 Defining_Identifier => Id,
4611 Subtype_Mark => New_Occurrence_Of (T, Loc),
4614 Set_Renamed_Object (Id, E);
4615 Freeze_Before (N, T);
4620 -- Ensure that the generated subtype has a unique external name
4621 -- when the related object is public. This guarantees that the
4622 -- subtype and its bounds will not be affected by switches or
4623 -- pragmas that may offset the internal counter due to extra
4626 if Is_Public (Id) then
4629 Related_Id := Empty;
4632 Expand_Subtype_From_Expr
4635 Subtype_Indic => Object_Definition (N),
4637 Related_Id => Related_Id);
4639 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4642 -- Propagate attributes to full view when needed.
4644 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4646 if Is_Private_Type (Act_T) and then Present (Full_View (Act_T))
4648 Full_View_Present := True;
4651 if Full_View_Present then
4652 Set_Is_Constr_Subt_For_U_Nominal (Full_View (Act_T));
4655 if Aliased_Present (N) then
4656 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4658 if Full_View_Present then
4659 Set_Is_Constr_Subt_For_UN_Aliased (Full_View (Act_T));
4663 Freeze_Before (N, Act_T);
4664 Freeze_Before (N, T);
4667 elsif Is_Array_Type (T)
4668 and then No_Initialization (N)
4669 and then (Nkind (Original_Node (E)) = N_Aggregate
4670 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4671 and then Nkind (Original_Node (Expression
4672 (Original_Node (E)))) = N_Aggregate))
4674 if not Is_Entity_Name (Object_Definition (N)) then
4676 Check_Compile_Time_Size (Act_T);
4678 if Aliased_Present (N) then
4679 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4683 -- When the given object definition and the aggregate are specified
4684 -- independently, and their lengths might differ do a length check.
4685 -- This cannot happen if the aggregate is of the form (others =>...)
4687 if not Is_Constrained (T) then
4690 elsif Nkind (E) = N_Raise_Constraint_Error then
4692 -- Aggregate is statically illegal. Place back in declaration
4694 Set_Expression (N, E);
4695 Set_No_Initialization (N, False);
4697 elsif T = Etype (E) then
4700 elsif Nkind (E) = N_Aggregate
4701 and then Present (Component_Associations (E))
4702 and then Present (Choice_List (First (Component_Associations (E))))
4704 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4710 Apply_Length_Check (E, T);
4713 -- If the type is limited unconstrained with defaulted discriminants and
4714 -- there is no expression, then the object is constrained by the
4715 -- defaults, so it is worthwhile building the corresponding subtype.
4717 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4718 and then not Is_Constrained (T)
4719 and then Has_Discriminants (T)
4722 Act_T := Build_Default_Subtype (T, N);
4724 -- Ada 2005: A limited object may be initialized by means of an
4725 -- aggregate. If the type has default discriminants it has an
4726 -- unconstrained nominal type, Its actual subtype will be obtained
4727 -- from the aggregate, and not from the default discriminants.
4732 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4734 elsif Nkind (E) = N_Function_Call
4735 and then Constant_Present (N)
4736 and then Has_Unconstrained_Elements (Etype (E))
4738 -- The back-end has problems with constants of a discriminated type
4739 -- with defaults, if the initial value is a function call. We
4740 -- generate an intermediate temporary that will receive a reference
4741 -- to the result of the call. The initialization expression then
4742 -- becomes a dereference of that temporary.
4744 Remove_Side_Effects (E);
4746 -- If this is a constant declaration of an unconstrained type and
4747 -- the initialization is an aggregate, we can use the subtype of the
4748 -- aggregate for the declared entity because it is immutable.
4750 elsif not Is_Constrained (T)
4751 and then Has_Discriminants (T)
4752 and then Constant_Present (N)
4753 and then not Has_Unchecked_Union (T)
4754 and then Nkind (E) = N_Aggregate
4759 -- Check No_Wide_Characters restriction
4761 Check_Wide_Character_Restriction (T, Object_Definition (N));
4763 -- Indicate this is not set in source. Certainly true for constants, and
4764 -- true for variables so far (will be reset for a variable if and when
4765 -- we encounter a modification in the source).
4767 Set_Never_Set_In_Source (Id);
4769 -- Now establish the proper kind and type of the object
4771 if Constant_Present (N) then
4772 Set_Ekind (Id, E_Constant);
4773 Set_Is_True_Constant (Id);
4776 Set_Ekind (Id, E_Variable);
4778 -- A variable is set as shared passive if it appears in a shared
4779 -- passive package, and is at the outer level. This is not done for
4780 -- entities generated during expansion, because those are always
4781 -- manipulated locally.
4783 if Is_Shared_Passive (Current_Scope)
4784 and then Is_Library_Level_Entity (Id)
4785 and then Comes_From_Source (Id)
4787 Set_Is_Shared_Passive (Id);
4788 Check_Shared_Var (Id, T, N);
4791 -- Set Has_Initial_Value if initializing expression present. Note
4792 -- that if there is no initializing expression, we leave the state
4793 -- of this flag unchanged (usually it will be False, but notably in
4794 -- the case of exception choice variables, it will already be true).
4797 Set_Has_Initial_Value (Id);
4801 -- Set the SPARK mode from the current context (may be overwritten later
4802 -- with explicit pragma).
4804 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4805 Set_SPARK_Pragma_Inherited (Id);
4807 -- Preserve relevant elaboration-related attributes of the context which
4808 -- are no longer available or very expensive to recompute once analysis,
4809 -- resolution, and expansion are over.
4811 Mark_Elaboration_Attributes
4816 -- Initialize alignment and size and capture alignment setting
4818 Init_Alignment (Id);
4820 Set_Optimize_Alignment_Flags (Id);
4822 -- Deal with aliased case
4824 if Aliased_Present (N) then
4825 Set_Is_Aliased (Id);
4827 -- AI12-001: All aliased objects are considered to be specified as
4828 -- independently addressable (RM C.6(8.1/4)).
4830 Set_Is_Independent (Id);
4832 -- If the object is aliased and the type is unconstrained with
4833 -- defaulted discriminants and there is no expression, then the
4834 -- object is constrained by the defaults, so it is worthwhile
4835 -- building the corresponding subtype.
4837 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4838 -- unconstrained, then only establish an actual subtype if the
4839 -- nominal subtype is indefinite. In definite cases the object is
4840 -- unconstrained in Ada 2005.
4843 and then Is_Record_Type (T)
4844 and then not Is_Constrained (T)
4845 and then Has_Discriminants (T)
4846 and then (Ada_Version < Ada_2005
4847 or else not Is_Definite_Subtype (T))
4849 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4853 -- Now we can set the type of the object
4855 Set_Etype (Id, Act_T);
4857 -- Non-constant object is marked to be treated as volatile if type is
4858 -- volatile and we clear the Current_Value setting that may have been
4859 -- set above. Doing so for constants isn't required and might interfere
4860 -- with possible uses of the object as a static expression in contexts
4861 -- incompatible with volatility (e.g. as a case-statement alternative).
4863 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4864 Set_Treat_As_Volatile (Id);
4865 Set_Current_Value (Id, Empty);
4868 -- Deal with controlled types
4870 if Has_Controlled_Component (Etype (Id))
4871 or else Is_Controlled (Etype (Id))
4873 if not Is_Library_Level_Entity (Id) then
4874 Check_Restriction (No_Nested_Finalization, N);
4876 Validate_Controlled_Object (Id);
4880 if Has_Task (Etype (Id)) then
4881 Check_Restriction (No_Tasking, N);
4883 -- Deal with counting max tasks
4885 -- Nothing to do if inside a generic
4887 if Inside_A_Generic then
4890 -- If library level entity, then count tasks
4892 elsif Is_Library_Level_Entity (Id) then
4893 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4895 -- If not library level entity, then indicate we don't know max
4896 -- tasks and also check task hierarchy restriction and blocking
4897 -- operation (since starting a task is definitely blocking).
4900 Check_Restriction (Max_Tasks, N);
4901 Check_Restriction (No_Task_Hierarchy, N);
4902 Check_Potentially_Blocking_Operation (N);
4905 -- A rather specialized test. If we see two tasks being declared
4906 -- of the same type in the same object declaration, and the task
4907 -- has an entry with an address clause, we know that program error
4908 -- will be raised at run time since we can't have two tasks with
4909 -- entries at the same address.
4911 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4916 E := First_Entity (Etype (Id));
4917 while Present (E) loop
4918 if Ekind (E) = E_Entry
4919 and then Present (Get_Attribute_Definition_Clause
4920 (E, Attribute_Address))
4922 Error_Msg_Warn := SPARK_Mode /= On;
4924 ("more than one task with same entry address<<", N);
4925 Error_Msg_N ("\Program_Error [<<", N);
4927 Make_Raise_Program_Error (Loc,
4928 Reason => PE_Duplicated_Entry_Address));
4938 -- Some simple constant-propagation: if the expression is a constant
4939 -- string initialized with a literal, share the literal. This avoids
4943 and then Is_Entity_Name (E)
4944 and then Ekind (Entity (E)) = E_Constant
4945 and then Base_Type (Etype (E)) = Standard_String
4948 Val : constant Node_Id := Constant_Value (Entity (E));
4950 if Present (Val) and then Nkind (Val) = N_String_Literal then
4951 Rewrite (E, New_Copy (Val));
4956 -- Another optimization: if the nominal subtype is unconstrained and
4957 -- the expression is a function call that returns an unconstrained
4958 -- type, rewrite the declaration as a renaming of the result of the
4959 -- call. The exceptions below are cases where the copy is expected,
4960 -- either by the back end (Aliased case) or by the semantics, as for
4961 -- initializing controlled types or copying tags for class-wide types.
4964 and then Nkind (E) = N_Explicit_Dereference
4965 and then Nkind (Original_Node (E)) = N_Function_Call
4966 and then not Is_Library_Level_Entity (Id)
4967 and then not Is_Constrained (Underlying_Type (T))
4968 and then not Is_Aliased (Id)
4969 and then not Is_Class_Wide_Type (T)
4970 and then not Is_Controlled (T)
4971 and then not Has_Controlled_Component (Base_Type (T))
4972 and then Expander_Active
4975 Make_Object_Renaming_Declaration (Loc,
4976 Defining_Identifier => Id,
4977 Access_Definition => Empty,
4978 Subtype_Mark => New_Occurrence_Of
4979 (Base_Type (Etype (Id)), Loc),
4982 Set_Renamed_Object (Id, E);
4984 -- Force generation of debugging information for the constant and for
4985 -- the renamed function call.
4987 Set_Debug_Info_Needed (Id);
4988 Set_Debug_Info_Needed (Entity (Prefix (E)));
4991 if Present (Prev_Entity)
4992 and then Is_Frozen (Prev_Entity)
4993 and then not Error_Posted (Id)
4995 Error_Msg_N ("full constant declaration appears too late", N);
4998 Check_Eliminated (Id);
5000 -- Deal with setting In_Private_Part flag if in private part
5002 if Ekind (Scope (Id)) = E_Package
5003 and then In_Private_Part (Scope (Id))
5005 Set_In_Private_Part (Id);
5009 -- Initialize the refined state of a variable here because this is a
5010 -- common destination for legal and illegal object declarations.
5012 if Ekind (Id) = E_Variable then
5013 Set_Encapsulating_State (Id, Empty);
5016 if Has_Aspects (N) then
5017 Analyze_Aspect_Specifications (N, Id);
5020 Analyze_Dimension (N);
5022 -- Verify whether the object declaration introduces an illegal hidden
5023 -- state within a package subject to a null abstract state.
5025 if Ekind (Id) = E_Variable then
5026 Check_No_Hidden_State (Id);
5029 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5030 end Analyze_Object_Declaration;
5032 ---------------------------
5033 -- Analyze_Others_Choice --
5034 ---------------------------
5036 -- Nothing to do for the others choice node itself, the semantic analysis
5037 -- of the others choice will occur as part of the processing of the parent
5039 procedure Analyze_Others_Choice (N : Node_Id) is
5040 pragma Warnings (Off, N);
5043 end Analyze_Others_Choice;
5045 -------------------------------------------
5046 -- Analyze_Private_Extension_Declaration --
5047 -------------------------------------------
5049 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5050 Indic : constant Node_Id := Subtype_Indication (N);
5051 T : constant Entity_Id := Defining_Identifier (N);
5053 Iface_Elmt : Elmt_Id;
5054 Parent_Base : Entity_Id;
5055 Parent_Type : Entity_Id;
5058 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5060 if Is_Non_Empty_List (Interface_List (N)) then
5066 Intf := First (Interface_List (N));
5067 while Present (Intf) loop
5068 T := Find_Type_Of_Subtype_Indic (Intf);
5070 Diagnose_Interface (Intf, T);
5076 Generate_Definition (T);
5078 -- For other than Ada 2012, just enter the name in the current scope
5080 if Ada_Version < Ada_2012 then
5083 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5084 -- case of private type that completes an incomplete type.
5091 Prev := Find_Type_Name (N);
5093 pragma Assert (Prev = T
5094 or else (Ekind (Prev) = E_Incomplete_Type
5095 and then Present (Full_View (Prev))
5096 and then Full_View (Prev) = T));
5100 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5101 Parent_Base := Base_Type (Parent_Type);
5103 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5104 Set_Ekind (T, Ekind (Parent_Type));
5105 Set_Etype (T, Any_Type);
5108 elsif not Is_Tagged_Type (Parent_Type) then
5110 ("parent of type extension must be a tagged type ", Indic);
5113 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
5114 Error_Msg_N ("premature derivation of incomplete type", Indic);
5117 elsif Is_Concurrent_Type (Parent_Type) then
5119 ("parent type of a private extension cannot be a synchronized "
5120 & "tagged type (RM 3.9.1 (3/1))", N);
5122 Set_Etype (T, Any_Type);
5123 Set_Ekind (T, E_Limited_Private_Type);
5124 Set_Private_Dependents (T, New_Elmt_List);
5125 Set_Error_Posted (T);
5129 -- Perhaps the parent type should be changed to the class-wide type's
5130 -- specific type in this case to prevent cascading errors ???
5132 if Is_Class_Wide_Type (Parent_Type) then
5134 ("parent of type extension must not be a class-wide type", Indic);
5138 if (not Is_Package_Or_Generic_Package (Current_Scope)
5139 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5140 or else In_Private_Part (Current_Scope)
5142 Error_Msg_N ("invalid context for private extension", N);
5145 -- Set common attributes
5147 Set_Is_Pure (T, Is_Pure (Current_Scope));
5148 Set_Scope (T, Current_Scope);
5149 Set_Ekind (T, E_Record_Type_With_Private);
5150 Init_Size_Align (T);
5151 Set_Default_SSO (T);
5152 Set_No_Reordering (T, No_Component_Reordering);
5154 Set_Etype (T, Parent_Base);
5155 Propagate_Concurrent_Flags (T, Parent_Base);
5157 Set_Convention (T, Convention (Parent_Type));
5158 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5159 Set_Is_First_Subtype (T);
5160 Make_Class_Wide_Type (T);
5162 -- Set the SPARK mode from the current context
5164 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5165 Set_SPARK_Pragma_Inherited (T);
5167 if Unknown_Discriminants_Present (N) then
5168 Set_Discriminant_Constraint (T, No_Elist);
5171 Build_Derived_Record_Type (N, Parent_Type, T);
5173 -- A private extension inherits the Default_Initial_Condition pragma
5174 -- coming from any parent type within the derivation chain.
5176 if Has_DIC (Parent_Type) then
5177 Set_Has_Inherited_DIC (T);
5180 -- A private extension inherits any class-wide invariants coming from a
5181 -- parent type or an interface. Note that the invariant procedure of the
5182 -- parent type should not be inherited because the private extension may
5183 -- define invariants of its own.
5185 if Has_Inherited_Invariants (Parent_Type)
5186 or else Has_Inheritable_Invariants (Parent_Type)
5188 Set_Has_Inherited_Invariants (T);
5190 elsif Present (Interfaces (T)) then
5191 Iface_Elmt := First_Elmt (Interfaces (T));
5192 while Present (Iface_Elmt) loop
5193 Iface := Node (Iface_Elmt);
5195 if Has_Inheritable_Invariants (Iface) then
5196 Set_Has_Inherited_Invariants (T);
5200 Next_Elmt (Iface_Elmt);
5204 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5205 -- synchronized formal derived type.
5207 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5208 Set_Is_Limited_Record (T);
5210 -- Formal derived type case
5212 if Is_Generic_Type (T) then
5214 -- The parent must be a tagged limited type or a synchronized
5217 if (not Is_Tagged_Type (Parent_Type)
5218 or else not Is_Limited_Type (Parent_Type))
5220 (not Is_Interface (Parent_Type)
5221 or else not Is_Synchronized_Interface (Parent_Type))
5224 ("parent type of & must be tagged limited or synchronized",
5228 -- The progenitors (if any) must be limited or synchronized
5231 if Present (Interfaces (T)) then
5232 Iface_Elmt := First_Elmt (Interfaces (T));
5233 while Present (Iface_Elmt) loop
5234 Iface := Node (Iface_Elmt);
5236 if not Is_Limited_Interface (Iface)
5237 and then not Is_Synchronized_Interface (Iface)
5240 ("progenitor & must be limited or synchronized",
5244 Next_Elmt (Iface_Elmt);
5248 -- Regular derived extension, the parent must be a limited or
5249 -- synchronized interface.
5252 if not Is_Interface (Parent_Type)
5253 or else (not Is_Limited_Interface (Parent_Type)
5254 and then not Is_Synchronized_Interface (Parent_Type))
5257 ("parent type of & must be limited interface", N, T);
5261 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5262 -- extension with a synchronized parent must be explicitly declared
5263 -- synchronized, because the full view will be a synchronized type.
5264 -- This must be checked before the check for limited types below,
5265 -- to ensure that types declared limited are not allowed to extend
5266 -- synchronized interfaces.
5268 elsif Is_Interface (Parent_Type)
5269 and then Is_Synchronized_Interface (Parent_Type)
5270 and then not Synchronized_Present (N)
5273 ("private extension of& must be explicitly synchronized",
5276 elsif Limited_Present (N) then
5277 Set_Is_Limited_Record (T);
5279 if not Is_Limited_Type (Parent_Type)
5281 (not Is_Interface (Parent_Type)
5282 or else not Is_Limited_Interface (Parent_Type))
5284 Error_Msg_NE ("parent type& of limited extension must be limited",
5289 -- Remember that its parent type has a private extension. Used to warn
5290 -- on public primitives of the parent type defined after its private
5291 -- extensions (see Check_Dispatching_Operation).
5293 Set_Has_Private_Extension (Parent_Type);
5296 if Has_Aspects (N) then
5297 Analyze_Aspect_Specifications (N, T);
5299 end Analyze_Private_Extension_Declaration;
5301 ---------------------------------
5302 -- Analyze_Subtype_Declaration --
5303 ---------------------------------
5305 procedure Analyze_Subtype_Declaration
5307 Skip : Boolean := False)
5309 Id : constant Entity_Id := Defining_Identifier (N);
5310 R_Checks : Check_Result;
5314 Generate_Definition (Id);
5315 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5316 Init_Size_Align (Id);
5318 -- The following guard condition on Enter_Name is to handle cases where
5319 -- the defining identifier has already been entered into the scope but
5320 -- the declaration as a whole needs to be analyzed.
5322 -- This case in particular happens for derived enumeration types. The
5323 -- derived enumeration type is processed as an inserted enumeration type
5324 -- declaration followed by a rewritten subtype declaration. The defining
5325 -- identifier, however, is entered into the name scope very early in the
5326 -- processing of the original type declaration and therefore needs to be
5327 -- avoided here, when the created subtype declaration is analyzed. (See
5328 -- Build_Derived_Types)
5330 -- This also happens when the full view of a private type is derived
5331 -- type with constraints. In this case the entity has been introduced
5332 -- in the private declaration.
5334 -- Finally this happens in some complex cases when validity checks are
5335 -- enabled, where the same subtype declaration may be analyzed twice.
5336 -- This can happen if the subtype is created by the preanalysis of
5337 -- an attribute tht gives the range of a loop statement, and the loop
5338 -- itself appears within an if_statement that will be rewritten during
5342 or else (Present (Etype (Id))
5343 and then (Is_Private_Type (Etype (Id))
5344 or else Is_Task_Type (Etype (Id))
5345 or else Is_Rewrite_Substitution (N)))
5349 elsif Current_Entity (Id) = Id then
5356 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5358 -- Class-wide equivalent types of records with unknown discriminants
5359 -- involve the generation of an itype which serves as the private view
5360 -- of a constrained record subtype. In such cases the base type of the
5361 -- current subtype we are processing is the private itype. Use the full
5362 -- of the private itype when decorating various attributes.
5365 and then Is_Private_Type (T)
5366 and then Present (Full_View (T))
5371 -- Inherit common attributes
5373 Set_Is_Volatile (Id, Is_Volatile (T));
5374 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5375 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5376 Set_Convention (Id, Convention (T));
5378 -- If ancestor has predicates then so does the subtype, and in addition
5379 -- we must delay the freeze to properly arrange predicate inheritance.
5381 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5382 -- in which T = ID, so the above tests and assignments do nothing???
5384 if Has_Predicates (T)
5385 or else (Present (Ancestor_Subtype (T))
5386 and then Has_Predicates (Ancestor_Subtype (T)))
5388 Set_Has_Predicates (Id);
5389 Set_Has_Delayed_Freeze (Id);
5391 -- Generated subtypes inherit the predicate function from the parent
5392 -- (no aspects to examine on the generated declaration).
5394 if not Comes_From_Source (N) then
5395 Set_Ekind (Id, Ekind (T));
5397 if Present (Predicate_Function (Id)) then
5400 elsif Present (Predicate_Function (T)) then
5401 Set_Predicate_Function (Id, Predicate_Function (T));
5403 elsif Present (Ancestor_Subtype (T))
5404 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5406 Set_Predicate_Function (Id,
5407 Predicate_Function (Ancestor_Subtype (T)));
5412 -- Subtype of Boolean cannot have a constraint in SPARK
5414 if Is_Boolean_Type (T)
5415 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5417 Check_SPARK_05_Restriction
5418 ("subtype of Boolean cannot have constraint", N);
5421 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5423 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5429 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5430 One_Cstr := First (Constraints (Cstr));
5431 while Present (One_Cstr) loop
5433 -- Index or discriminant constraint in SPARK must be a
5437 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5439 Check_SPARK_05_Restriction
5440 ("subtype mark required", One_Cstr);
5442 -- String subtype must have a lower bound of 1 in SPARK.
5443 -- Note that we do not need to test for the nonstatic case
5444 -- here, since that was already taken care of in
5445 -- Process_Range_Expr_In_Decl.
5447 elsif Base_Type (T) = Standard_String then
5448 Get_Index_Bounds (One_Cstr, Low, High);
5450 if Is_OK_Static_Expression (Low)
5451 and then Expr_Value (Low) /= 1
5453 Check_SPARK_05_Restriction
5454 ("String subtype must have lower bound of 1", N);
5464 -- In the case where there is no constraint given in the subtype
5465 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5466 -- semantic attributes must be established here.
5468 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5469 Set_Etype (Id, Base_Type (T));
5471 -- Subtype of unconstrained array without constraint is not allowed
5474 if Is_Array_Type (T) and then not Is_Constrained (T) then
5475 Check_SPARK_05_Restriction
5476 ("subtype of unconstrained array must have constraint", N);
5481 Set_Ekind (Id, E_Array_Subtype);
5482 Copy_Array_Subtype_Attributes (Id, T);
5484 when Decimal_Fixed_Point_Kind =>
5485 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5486 Set_Digits_Value (Id, Digits_Value (T));
5487 Set_Delta_Value (Id, Delta_Value (T));
5488 Set_Scale_Value (Id, Scale_Value (T));
5489 Set_Small_Value (Id, Small_Value (T));
5490 Set_Scalar_Range (Id, Scalar_Range (T));
5491 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5492 Set_Is_Constrained (Id, Is_Constrained (T));
5493 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5494 Set_RM_Size (Id, RM_Size (T));
5496 when Enumeration_Kind =>
5497 Set_Ekind (Id, E_Enumeration_Subtype);
5498 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5499 Set_Scalar_Range (Id, Scalar_Range (T));
5500 Set_Is_Character_Type (Id, Is_Character_Type (T));
5501 Set_Is_Constrained (Id, Is_Constrained (T));
5502 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5503 Set_RM_Size (Id, RM_Size (T));
5505 when Ordinary_Fixed_Point_Kind =>
5506 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5507 Set_Scalar_Range (Id, Scalar_Range (T));
5508 Set_Small_Value (Id, Small_Value (T));
5509 Set_Delta_Value (Id, Delta_Value (T));
5510 Set_Is_Constrained (Id, Is_Constrained (T));
5511 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5512 Set_RM_Size (Id, RM_Size (T));
5515 Set_Ekind (Id, E_Floating_Point_Subtype);
5516 Set_Scalar_Range (Id, Scalar_Range (T));
5517 Set_Digits_Value (Id, Digits_Value (T));
5518 Set_Is_Constrained (Id, Is_Constrained (T));
5520 -- If the floating point type has dimensions, these will be
5521 -- inherited subsequently when Analyze_Dimensions is called.
5523 when Signed_Integer_Kind =>
5524 Set_Ekind (Id, E_Signed_Integer_Subtype);
5525 Set_Scalar_Range (Id, Scalar_Range (T));
5526 Set_Is_Constrained (Id, Is_Constrained (T));
5527 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5528 Set_RM_Size (Id, RM_Size (T));
5530 when Modular_Integer_Kind =>
5531 Set_Ekind (Id, E_Modular_Integer_Subtype);
5532 Set_Scalar_Range (Id, Scalar_Range (T));
5533 Set_Is_Constrained (Id, Is_Constrained (T));
5534 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5535 Set_RM_Size (Id, RM_Size (T));
5537 when Class_Wide_Kind =>
5538 Set_Ekind (Id, E_Class_Wide_Subtype);
5539 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5540 Set_Cloned_Subtype (Id, T);
5541 Set_Is_Tagged_Type (Id, True);
5542 Set_Has_Unknown_Discriminants
5544 Set_No_Tagged_Streams_Pragma
5545 (Id, No_Tagged_Streams_Pragma (T));
5547 if Ekind (T) = E_Class_Wide_Subtype then
5548 Set_Equivalent_Type (Id, Equivalent_Type (T));
5551 when E_Record_Subtype
5554 Set_Ekind (Id, E_Record_Subtype);
5556 -- Subtype declarations introduced for formal type parameters
5557 -- in generic instantiations should inherit the Size value of
5558 -- the type they rename.
5560 if Present (Generic_Parent_Type (N)) then
5561 Set_RM_Size (Id, RM_Size (T));
5564 if Ekind (T) = E_Record_Subtype
5565 and then Present (Cloned_Subtype (T))
5567 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5569 Set_Cloned_Subtype (Id, T);
5572 Set_First_Entity (Id, First_Entity (T));
5573 Set_Last_Entity (Id, Last_Entity (T));
5574 Set_Has_Discriminants (Id, Has_Discriminants (T));
5575 Set_Is_Constrained (Id, Is_Constrained (T));
5576 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5577 Set_Has_Implicit_Dereference
5578 (Id, Has_Implicit_Dereference (T));
5579 Set_Has_Unknown_Discriminants
5580 (Id, Has_Unknown_Discriminants (T));
5582 if Has_Discriminants (T) then
5583 Set_Discriminant_Constraint
5584 (Id, Discriminant_Constraint (T));
5585 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5587 elsif Has_Unknown_Discriminants (Id) then
5588 Set_Discriminant_Constraint (Id, No_Elist);
5591 if Is_Tagged_Type (T) then
5592 Set_Is_Tagged_Type (Id, True);
5593 Set_No_Tagged_Streams_Pragma
5594 (Id, No_Tagged_Streams_Pragma (T));
5595 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5596 Set_Direct_Primitive_Operations
5597 (Id, Direct_Primitive_Operations (T));
5598 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5600 if Is_Interface (T) then
5601 Set_Is_Interface (Id);
5602 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5606 when Private_Kind =>
5607 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5608 Set_Has_Discriminants (Id, Has_Discriminants (T));
5609 Set_Is_Constrained (Id, Is_Constrained (T));
5610 Set_First_Entity (Id, First_Entity (T));
5611 Set_Last_Entity (Id, Last_Entity (T));
5612 Set_Private_Dependents (Id, New_Elmt_List);
5613 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5614 Set_Has_Implicit_Dereference
5615 (Id, Has_Implicit_Dereference (T));
5616 Set_Has_Unknown_Discriminants
5617 (Id, Has_Unknown_Discriminants (T));
5618 Set_Known_To_Have_Preelab_Init
5619 (Id, Known_To_Have_Preelab_Init (T));
5621 if Is_Tagged_Type (T) then
5622 Set_Is_Tagged_Type (Id);
5623 Set_No_Tagged_Streams_Pragma (Id,
5624 No_Tagged_Streams_Pragma (T));
5625 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5626 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5627 Set_Direct_Primitive_Operations (Id,
5628 Direct_Primitive_Operations (T));
5631 -- In general the attributes of the subtype of a private type
5632 -- are the attributes of the partial view of parent. However,
5633 -- the full view may be a discriminated type, and the subtype
5634 -- must share the discriminant constraint to generate correct
5635 -- calls to initialization procedures.
5637 if Has_Discriminants (T) then
5638 Set_Discriminant_Constraint
5639 (Id, Discriminant_Constraint (T));
5640 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5642 elsif Present (Full_View (T))
5643 and then Has_Discriminants (Full_View (T))
5645 Set_Discriminant_Constraint
5646 (Id, Discriminant_Constraint (Full_View (T)));
5647 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5649 -- This would seem semantically correct, but apparently
5650 -- generates spurious errors about missing components ???
5652 -- Set_Has_Discriminants (Id);
5655 Prepare_Private_Subtype_Completion (Id, N);
5657 -- If this is the subtype of a constrained private type with
5658 -- discriminants that has got a full view and we also have
5659 -- built a completion just above, show that the completion
5660 -- is a clone of the full view to the back-end.
5662 if Has_Discriminants (T)
5663 and then not Has_Unknown_Discriminants (T)
5664 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5665 and then Present (Full_View (T))
5666 and then Present (Full_View (Id))
5668 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5672 Set_Ekind (Id, E_Access_Subtype);
5673 Set_Is_Constrained (Id, Is_Constrained (T));
5674 Set_Is_Access_Constant
5675 (Id, Is_Access_Constant (T));
5676 Set_Directly_Designated_Type
5677 (Id, Designated_Type (T));
5678 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5680 -- A Pure library_item must not contain the declaration of a
5681 -- named access type, except within a subprogram, generic
5682 -- subprogram, task unit, or protected unit, or if it has
5683 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5685 if Comes_From_Source (Id)
5686 and then In_Pure_Unit
5687 and then not In_Subprogram_Task_Protected_Unit
5688 and then not No_Pool_Assigned (Id)
5691 ("named access types not allowed in pure unit", N);
5694 when Concurrent_Kind =>
5695 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5696 Set_Corresponding_Record_Type (Id,
5697 Corresponding_Record_Type (T));
5698 Set_First_Entity (Id, First_Entity (T));
5699 Set_First_Private_Entity (Id, First_Private_Entity (T));
5700 Set_Has_Discriminants (Id, Has_Discriminants (T));
5701 Set_Is_Constrained (Id, Is_Constrained (T));
5702 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5703 Set_Last_Entity (Id, Last_Entity (T));
5705 if Is_Tagged_Type (T) then
5706 Set_No_Tagged_Streams_Pragma
5707 (Id, No_Tagged_Streams_Pragma (T));
5710 if Has_Discriminants (T) then
5711 Set_Discriminant_Constraint
5712 (Id, Discriminant_Constraint (T));
5713 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5716 when Incomplete_Kind =>
5717 if Ada_Version >= Ada_2005 then
5719 -- In Ada 2005 an incomplete type can be explicitly tagged:
5720 -- propagate indication. Note that we also have to include
5721 -- subtypes for Ada 2012 extended use of incomplete types.
5723 Set_Ekind (Id, E_Incomplete_Subtype);
5724 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5725 Set_Private_Dependents (Id, New_Elmt_List);
5727 if Is_Tagged_Type (Id) then
5728 Set_No_Tagged_Streams_Pragma
5729 (Id, No_Tagged_Streams_Pragma (T));
5730 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5733 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5734 -- incomplete type visible through a limited with clause.
5736 if From_Limited_With (T)
5737 and then Present (Non_Limited_View (T))
5739 Set_From_Limited_With (Id);
5740 Set_Non_Limited_View (Id, Non_Limited_View (T));
5742 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5743 -- to the private dependents of the original incomplete
5744 -- type for future transformation.
5747 Append_Elmt (Id, Private_Dependents (T));
5750 -- If the subtype name denotes an incomplete type an error
5751 -- was already reported by Process_Subtype.
5754 Set_Etype (Id, Any_Type);
5758 raise Program_Error;
5761 -- If there is no constraint in the subtype indication, the
5762 -- declared entity inherits predicates from the parent.
5764 Inherit_Predicate_Flags (Id, T);
5767 if Etype (Id) = Any_Type then
5771 -- Some common processing on all types
5773 Set_Size_Info (Id, T);
5774 Set_First_Rep_Item (Id, First_Rep_Item (T));
5776 -- If the parent type is a generic actual, so is the subtype. This may
5777 -- happen in a nested instance. Why Comes_From_Source test???
5779 if not Comes_From_Source (N) then
5780 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5783 -- If this is a subtype declaration for an actual in an instance,
5784 -- inherit static and dynamic predicates if any.
5786 -- If declaration has no aspect specifications, inherit predicate
5787 -- info as well. Unclear how to handle the case of both specified
5788 -- and inherited predicates ??? Other inherited aspects, such as
5789 -- invariants, should be OK, but the combination with later pragmas
5790 -- may also require special merging.
5792 if Has_Predicates (T)
5793 and then Present (Predicate_Function (T))
5795 ((In_Instance and then not Comes_From_Source (N))
5796 or else No (Aspect_Specifications (N)))
5798 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5800 if Has_Static_Predicate (T) then
5801 Set_Has_Static_Predicate (Id);
5802 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5806 -- Remaining processing depends on characteristics of base type
5810 Set_Is_Immediately_Visible (Id, True);
5811 Set_Depends_On_Private (Id, Has_Private_Component (T));
5812 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5814 if Is_Interface (T) then
5815 Set_Is_Interface (Id);
5818 if Present (Generic_Parent_Type (N))
5820 (Nkind (Parent (Generic_Parent_Type (N))) /=
5821 N_Formal_Type_Declaration
5822 or else Nkind (Formal_Type_Definition
5823 (Parent (Generic_Parent_Type (N)))) /=
5824 N_Formal_Private_Type_Definition)
5826 if Is_Tagged_Type (Id) then
5828 -- If this is a generic actual subtype for a synchronized type,
5829 -- the primitive operations are those of the corresponding record
5830 -- for which there is a separate subtype declaration.
5832 if Is_Concurrent_Type (Id) then
5834 elsif Is_Class_Wide_Type (Id) then
5835 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5837 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5840 elsif Scope (Etype (Id)) /= Standard_Standard then
5841 Derive_Subprograms (Generic_Parent_Type (N), Id);
5845 if Is_Private_Type (T) and then Present (Full_View (T)) then
5846 Conditional_Delay (Id, Full_View (T));
5848 -- The subtypes of components or subcomponents of protected types
5849 -- do not need freeze nodes, which would otherwise appear in the
5850 -- wrong scope (before the freeze node for the protected type). The
5851 -- proper subtypes are those of the subcomponents of the corresponding
5854 elsif Ekind (Scope (Id)) /= E_Protected_Type
5855 and then Present (Scope (Scope (Id))) -- error defense
5856 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5858 Conditional_Delay (Id, T);
5861 -- If we have a subtype of an incomplete type whose full type is a
5862 -- derived numeric type, we need to have a freeze node for the subtype.
5863 -- Otherwise gigi will complain while computing the (static) bounds of
5867 and then Is_Elementary_Type (Id)
5868 and then Etype (Id) /= Id
5871 Partial : constant Entity_Id :=
5872 Incomplete_Or_Partial_View (First_Subtype (Id));
5874 if Present (Partial)
5875 and then Ekind (Partial) = E_Incomplete_Type
5877 Set_Has_Delayed_Freeze (Id);
5882 -- Check that Constraint_Error is raised for a scalar subtype indication
5883 -- when the lower or upper bound of a non-null range lies outside the
5884 -- range of the type mark.
5886 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5887 if Is_Scalar_Type (Etype (Id))
5888 and then Scalar_Range (Id) /=
5890 (Etype (Subtype_Mark (Subtype_Indication (N))))
5894 Etype (Subtype_Mark (Subtype_Indication (N))));
5896 -- In the array case, check compatibility for each index
5898 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5900 -- This really should be a subprogram that finds the indications
5904 Subt_Index : Node_Id := First_Index (Id);
5905 Target_Index : Node_Id :=
5907 (Subtype_Mark (Subtype_Indication (N))));
5908 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5911 while Present (Subt_Index) loop
5912 if ((Nkind (Subt_Index) = N_Identifier
5913 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5914 or else Nkind (Subt_Index) = N_Subtype_Indication)
5916 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5919 Target_Typ : constant Entity_Id :=
5920 Etype (Target_Index);
5924 (Scalar_Range (Etype (Subt_Index)),
5927 Defining_Identifier (N));
5929 -- Reset Has_Dynamic_Range_Check on the subtype to
5930 -- prevent elision of the index check due to a dynamic
5931 -- check generated for a preceding index (needed since
5932 -- Insert_Range_Checks tries to avoid generating
5933 -- redundant checks on a given declaration).
5935 Set_Has_Dynamic_Range_Check (N, False);
5941 Sloc (Defining_Identifier (N)));
5943 -- Record whether this index involved a dynamic check
5946 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5950 Next_Index (Subt_Index);
5951 Next_Index (Target_Index);
5954 -- Finally, mark whether the subtype involves dynamic checks
5956 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5961 Set_Optimize_Alignment_Flags (Id);
5962 Check_Eliminated (Id);
5965 if Has_Aspects (N) then
5966 Analyze_Aspect_Specifications (N, Id);
5969 Analyze_Dimension (N);
5971 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5972 -- indications on composite types where the constraints are dynamic.
5973 -- Note that object declarations and aggregates generate implicit
5974 -- subtype declarations, which this covers. One special case is that the
5975 -- implicitly generated "=" for discriminated types includes an
5976 -- offending subtype declaration, which is harmless, so we ignore it
5979 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5981 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5983 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5984 and then not (Is_Internal (Id)
5985 and then Is_TSS (Scope (Id),
5986 TSS_Composite_Equality))
5987 and then not Within_Init_Proc
5988 and then not All_Composite_Constraints_Static (Cstr)
5990 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5994 end Analyze_Subtype_Declaration;
5996 --------------------------------
5997 -- Analyze_Subtype_Indication --
5998 --------------------------------
6000 procedure Analyze_Subtype_Indication (N : Node_Id) is
6001 T : constant Entity_Id := Subtype_Mark (N);
6002 R : constant Node_Id := Range_Expression (Constraint (N));
6009 Set_Etype (N, Etype (R));
6010 Resolve (R, Entity (T));
6012 Set_Error_Posted (R);
6013 Set_Error_Posted (T);
6015 end Analyze_Subtype_Indication;
6017 --------------------------
6018 -- Analyze_Variant_Part --
6019 --------------------------
6021 procedure Analyze_Variant_Part (N : Node_Id) is
6022 Discr_Name : Node_Id;
6023 Discr_Type : Entity_Id;
6025 procedure Process_Variant (A : Node_Id);
6026 -- Analyze declarations for a single variant
6028 package Analyze_Variant_Choices is
6029 new Generic_Analyze_Choices (Process_Variant);
6030 use Analyze_Variant_Choices;
6032 ---------------------
6033 -- Process_Variant --
6034 ---------------------
6036 procedure Process_Variant (A : Node_Id) is
6037 CL : constant Node_Id := Component_List (A);
6039 if not Null_Present (CL) then
6040 Analyze_Declarations (Component_Items (CL));
6042 if Present (Variant_Part (CL)) then
6043 Analyze (Variant_Part (CL));
6046 end Process_Variant;
6048 -- Start of processing for Analyze_Variant_Part
6051 Discr_Name := Name (N);
6052 Analyze (Discr_Name);
6054 -- If Discr_Name bad, get out (prevent cascaded errors)
6056 if Etype (Discr_Name) = Any_Type then
6060 -- Check invalid discriminant in variant part
6062 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6063 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6066 Discr_Type := Etype (Entity (Discr_Name));
6068 if not Is_Discrete_Type (Discr_Type) then
6070 ("discriminant in a variant part must be of a discrete type",
6075 -- Now analyze the choices, which also analyzes the declarations that
6076 -- are associated with each choice.
6078 Analyze_Choices (Variants (N), Discr_Type);
6080 -- Note: we used to instantiate and call Check_Choices here to check
6081 -- that the choices covered the discriminant, but it's too early to do
6082 -- that because of statically predicated subtypes, whose analysis may
6083 -- be deferred to their freeze point which may be as late as the freeze
6084 -- point of the containing record. So this call is now to be found in
6085 -- Freeze_Record_Declaration.
6087 end Analyze_Variant_Part;
6089 ----------------------------
6090 -- Array_Type_Declaration --
6091 ----------------------------
6093 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6094 Component_Def : constant Node_Id := Component_Definition (Def);
6095 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6096 P : constant Node_Id := Parent (Def);
6097 Element_Type : Entity_Id;
6098 Implicit_Base : Entity_Id;
6102 Related_Id : Entity_Id := Empty;
6105 if Nkind (Def) = N_Constrained_Array_Definition then
6106 Index := First (Discrete_Subtype_Definitions (Def));
6108 Index := First (Subtype_Marks (Def));
6111 -- Find proper names for the implicit types which may be public. In case
6112 -- of anonymous arrays we use the name of the first object of that type
6116 Related_Id := Defining_Identifier (P);
6122 while Present (Index) loop
6125 -- Test for odd case of trying to index a type by the type itself
6127 if Is_Entity_Name (Index) and then Entity (Index) = T then
6128 Error_Msg_N ("type& cannot be indexed by itself", Index);
6129 Set_Entity (Index, Standard_Boolean);
6130 Set_Etype (Index, Standard_Boolean);
6133 -- Check SPARK restriction requiring a subtype mark
6135 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
6136 Check_SPARK_05_Restriction ("subtype mark required", Index);
6139 -- Add a subtype declaration for each index of private array type
6140 -- declaration whose etype is also private. For example:
6143 -- type Index is private;
6145 -- type Table is array (Index) of ...
6148 -- This is currently required by the expander for the internally
6149 -- generated equality subprogram of records with variant parts in
6150 -- which the etype of some component is such private type.
6152 if Ekind (Current_Scope) = E_Package
6153 and then In_Private_Part (Current_Scope)
6154 and then Has_Private_Declaration (Etype (Index))
6157 Loc : constant Source_Ptr := Sloc (Def);
6162 New_E := Make_Temporary (Loc, 'T');
6163 Set_Is_Internal (New_E);
6166 Make_Subtype_Declaration (Loc,
6167 Defining_Identifier => New_E,
6168 Subtype_Indication =>
6169 New_Occurrence_Of (Etype (Index), Loc));
6171 Insert_Before (Parent (Def), Decl);
6173 Set_Etype (Index, New_E);
6175 -- If the index is a range or a subtype indication it carries
6176 -- no entity. Example:
6179 -- type T is private;
6181 -- type T is new Natural;
6182 -- Table : array (T(1) .. T(10)) of Boolean;
6185 -- Otherwise the type of the reference is its entity.
6187 if Is_Entity_Name (Index) then
6188 Set_Entity (Index, New_E);
6193 Make_Index (Index, P, Related_Id, Nb_Index);
6195 -- Check error of subtype with predicate for index type
6197 Bad_Predicated_Subtype_Use
6198 ("subtype& has predicate, not allowed as index subtype",
6199 Index, Etype (Index));
6201 -- Move to next index
6204 Nb_Index := Nb_Index + 1;
6207 -- Process subtype indication if one is present
6209 if Present (Component_Typ) then
6210 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6212 Set_Etype (Component_Typ, Element_Type);
6214 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
6215 Check_SPARK_05_Restriction
6216 ("subtype mark required", Component_Typ);
6219 -- Ada 2005 (AI-230): Access Definition case
6221 else pragma Assert (Present (Access_Definition (Component_Def)));
6223 -- Indicate that the anonymous access type is created by the
6224 -- array type declaration.
6226 Element_Type := Access_Definition
6228 N => Access_Definition (Component_Def));
6229 Set_Is_Local_Anonymous_Access (Element_Type);
6231 -- Propagate the parent. This field is needed if we have to generate
6232 -- the master_id associated with an anonymous access to task type
6233 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6235 Set_Parent (Element_Type, Parent (T));
6237 -- Ada 2005 (AI-230): In case of components that are anonymous access
6238 -- types the level of accessibility depends on the enclosing type
6241 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6243 -- Ada 2005 (AI-254)
6246 CD : constant Node_Id :=
6247 Access_To_Subprogram_Definition
6248 (Access_Definition (Component_Def));
6250 if Present (CD) and then Protected_Present (CD) then
6252 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6257 -- Constrained array case
6260 T := Create_Itype (E_Void, P, Related_Id, 'T');
6263 if Nkind (Def) = N_Constrained_Array_Definition then
6265 -- Establish Implicit_Base as unconstrained base type
6267 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6269 Set_Etype (Implicit_Base, Implicit_Base);
6270 Set_Scope (Implicit_Base, Current_Scope);
6271 Set_Has_Delayed_Freeze (Implicit_Base);
6272 Set_Default_SSO (Implicit_Base);
6274 -- The constrained array type is a subtype of the unconstrained one
6276 Set_Ekind (T, E_Array_Subtype);
6277 Init_Size_Align (T);
6278 Set_Etype (T, Implicit_Base);
6279 Set_Scope (T, Current_Scope);
6280 Set_Is_Constrained (T);
6282 First (Discrete_Subtype_Definitions (Def)));
6283 Set_Has_Delayed_Freeze (T);
6285 -- Complete setup of implicit base type
6287 Set_Component_Size (Implicit_Base, Uint_0);
6288 Set_Component_Type (Implicit_Base, Element_Type);
6289 Set_Finalize_Storage_Only
6291 Finalize_Storage_Only (Element_Type));
6292 Set_First_Index (Implicit_Base, First_Index (T));
6293 Set_Has_Controlled_Component
6295 Has_Controlled_Component (Element_Type)
6296 or else Is_Controlled (Element_Type));
6297 Set_Packed_Array_Impl_Type
6298 (Implicit_Base, Empty);
6300 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6302 -- Unconstrained array case
6305 Set_Ekind (T, E_Array_Type);
6306 Init_Size_Align (T);
6308 Set_Scope (T, Current_Scope);
6309 Set_Component_Size (T, Uint_0);
6310 Set_Is_Constrained (T, False);
6311 Set_First_Index (T, First (Subtype_Marks (Def)));
6312 Set_Has_Delayed_Freeze (T, True);
6313 Propagate_Concurrent_Flags (T, Element_Type);
6314 Set_Has_Controlled_Component (T, Has_Controlled_Component
6317 Is_Controlled (Element_Type));
6318 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6320 Set_Default_SSO (T);
6323 -- Common attributes for both cases
6325 Set_Component_Type (Base_Type (T), Element_Type);
6326 Set_Packed_Array_Impl_Type (T, Empty);
6328 if Aliased_Present (Component_Definition (Def)) then
6329 Check_SPARK_05_Restriction
6330 ("aliased is not allowed", Component_Definition (Def));
6331 Set_Has_Aliased_Components (Etype (T));
6333 -- AI12-001: All aliased objects are considered to be specified as
6334 -- independently addressable (RM C.6(8.1/4)).
6336 Set_Has_Independent_Components (Etype (T));
6339 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6340 -- array type to ensure that objects of this type are initialized.
6342 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6343 Set_Can_Never_Be_Null (T);
6345 if Null_Exclusion_Present (Component_Definition (Def))
6347 -- No need to check itypes because in their case this check was
6348 -- done at their point of creation
6350 and then not Is_Itype (Element_Type)
6353 ("`NOT NULL` not allowed (null already excluded)",
6354 Subtype_Indication (Component_Definition (Def)));
6358 Priv := Private_Component (Element_Type);
6360 if Present (Priv) then
6362 -- Check for circular definitions
6364 if Priv = Any_Type then
6365 Set_Component_Type (Etype (T), Any_Type);
6367 -- There is a gap in the visibility of operations on the composite
6368 -- type only if the component type is defined in a different scope.
6370 elsif Scope (Priv) = Current_Scope then
6373 elsif Is_Limited_Type (Priv) then
6374 Set_Is_Limited_Composite (Etype (T));
6375 Set_Is_Limited_Composite (T);
6377 Set_Is_Private_Composite (Etype (T));
6378 Set_Is_Private_Composite (T);
6382 -- A syntax error in the declaration itself may lead to an empty index
6383 -- list, in which case do a minimal patch.
6385 if No (First_Index (T)) then
6386 Error_Msg_N ("missing index definition in array type declaration", T);
6389 Indexes : constant List_Id :=
6390 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6392 Set_Discrete_Subtype_Definitions (Def, Indexes);
6393 Set_First_Index (T, First (Indexes));
6398 -- Create a concatenation operator for the new type. Internal array
6399 -- types created for packed entities do not need such, they are
6400 -- compatible with the user-defined type.
6402 if Number_Dimensions (T) = 1
6403 and then not Is_Packed_Array_Impl_Type (T)
6405 New_Concatenation_Op (T);
6408 -- In the case of an unconstrained array the parser has already verified
6409 -- that all the indexes are unconstrained but we still need to make sure
6410 -- that the element type is constrained.
6412 if not Is_Definite_Subtype (Element_Type) then
6414 ("unconstrained element type in array declaration",
6415 Subtype_Indication (Component_Def));
6417 elsif Is_Abstract_Type (Element_Type) then
6419 ("the type of a component cannot be abstract",
6420 Subtype_Indication (Component_Def));
6423 -- There may be an invariant declared for the component type, but
6424 -- the construction of the component invariant checking procedure
6425 -- takes place during expansion.
6426 end Array_Type_Declaration;
6428 ------------------------------------------------------
6429 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6430 ------------------------------------------------------
6432 function Replace_Anonymous_Access_To_Protected_Subprogram
6433 (N : Node_Id) return Entity_Id
6435 Loc : constant Source_Ptr := Sloc (N);
6437 Curr_Scope : constant Scope_Stack_Entry :=
6438 Scope_Stack.Table (Scope_Stack.Last);
6440 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6443 -- Access definition in declaration
6446 -- Object definition or formal definition with an access definition
6449 -- Declaration of anonymous access to subprogram type
6452 -- Original specification in access to subprogram
6457 Set_Is_Internal (Anon);
6460 when N_Constrained_Array_Definition
6461 | N_Component_Declaration
6462 | N_Unconstrained_Array_Definition
6464 Comp := Component_Definition (N);
6465 Acc := Access_Definition (Comp);
6467 when N_Discriminant_Specification =>
6468 Comp := Discriminant_Type (N);
6471 when N_Parameter_Specification =>
6472 Comp := Parameter_Type (N);
6475 when N_Access_Function_Definition =>
6476 Comp := Result_Definition (N);
6479 when N_Object_Declaration =>
6480 Comp := Object_Definition (N);
6483 when N_Function_Specification =>
6484 Comp := Result_Definition (N);
6488 raise Program_Error;
6491 Spec := Access_To_Subprogram_Definition (Acc);
6494 Make_Full_Type_Declaration (Loc,
6495 Defining_Identifier => Anon,
6496 Type_Definition => Copy_Separate_Tree (Spec));
6498 Mark_Rewrite_Insertion (Decl);
6500 -- Insert the new declaration in the nearest enclosing scope. If the
6501 -- parent is a body and N is its return type, the declaration belongs
6502 -- in the enclosing scope. Likewise if N is the type of a parameter.
6506 if Nkind (N) = N_Function_Specification
6507 and then Nkind (P) = N_Subprogram_Body
6510 elsif Nkind (N) = N_Parameter_Specification
6511 and then Nkind (P) in N_Subprogram_Specification
6512 and then Nkind (Parent (P)) = N_Subprogram_Body
6514 P := Parent (Parent (P));
6517 while Present (P) and then not Has_Declarations (P) loop
6521 pragma Assert (Present (P));
6523 if Nkind (P) = N_Package_Specification then
6524 Prepend (Decl, Visible_Declarations (P));
6526 Prepend (Decl, Declarations (P));
6529 -- Replace the anonymous type with an occurrence of the new declaration.
6530 -- In all cases the rewritten node does not have the null-exclusion
6531 -- attribute because (if present) it was already inherited by the
6532 -- anonymous entity (Anon). Thus, in case of components we do not
6533 -- inherit this attribute.
6535 if Nkind (N) = N_Parameter_Specification then
6536 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6537 Set_Etype (Defining_Identifier (N), Anon);
6538 Set_Null_Exclusion_Present (N, False);
6540 elsif Nkind (N) = N_Object_Declaration then
6541 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6542 Set_Etype (Defining_Identifier (N), Anon);
6544 elsif Nkind (N) = N_Access_Function_Definition then
6545 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6547 elsif Nkind (N) = N_Function_Specification then
6548 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6549 Set_Etype (Defining_Unit_Name (N), Anon);
6553 Make_Component_Definition (Loc,
6554 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6557 Mark_Rewrite_Insertion (Comp);
6559 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6560 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6561 and then not Is_Type (Current_Scope))
6564 -- Declaration can be analyzed in the current scope.
6569 -- Temporarily remove the current scope (record or subprogram) from
6570 -- the stack to add the new declarations to the enclosing scope.
6571 -- The anonymous entity is an Itype with the proper attributes.
6573 Scope_Stack.Decrement_Last;
6575 Set_Is_Itype (Anon);
6576 Set_Associated_Node_For_Itype (Anon, N);
6577 Scope_Stack.Append (Curr_Scope);
6580 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6581 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6583 end Replace_Anonymous_Access_To_Protected_Subprogram;
6585 -------------------------------
6586 -- Build_Derived_Access_Type --
6587 -------------------------------
6589 procedure Build_Derived_Access_Type
6591 Parent_Type : Entity_Id;
6592 Derived_Type : Entity_Id)
6594 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6596 Desig_Type : Entity_Id;
6598 Discr_Con_Elist : Elist_Id;
6599 Discr_Con_El : Elmt_Id;
6603 -- Set the designated type so it is available in case this is an access
6604 -- to a self-referential type, e.g. a standard list type with a next
6605 -- pointer. Will be reset after subtype is built.
6607 Set_Directly_Designated_Type
6608 (Derived_Type, Designated_Type (Parent_Type));
6610 Subt := Process_Subtype (S, N);
6612 if Nkind (S) /= N_Subtype_Indication
6613 and then Subt /= Base_Type (Subt)
6615 Set_Ekind (Derived_Type, E_Access_Subtype);
6618 if Ekind (Derived_Type) = E_Access_Subtype then
6620 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6621 Ibase : constant Entity_Id :=
6622 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6623 Svg_Chars : constant Name_Id := Chars (Ibase);
6624 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6625 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
6628 Copy_Node (Pbase, Ibase);
6630 -- Restore Itype status after Copy_Node
6632 Set_Is_Itype (Ibase);
6633 Set_Associated_Node_For_Itype (Ibase, N);
6635 Set_Chars (Ibase, Svg_Chars);
6636 Set_Prev_Entity (Ibase, Svg_Prev_E);
6637 Set_Next_Entity (Ibase, Svg_Next_E);
6638 Set_Sloc (Ibase, Sloc (Derived_Type));
6639 Set_Scope (Ibase, Scope (Derived_Type));
6640 Set_Freeze_Node (Ibase, Empty);
6641 Set_Is_Frozen (Ibase, False);
6642 Set_Comes_From_Source (Ibase, False);
6643 Set_Is_First_Subtype (Ibase, False);
6645 Set_Etype (Ibase, Pbase);
6646 Set_Etype (Derived_Type, Ibase);
6650 Set_Directly_Designated_Type
6651 (Derived_Type, Designated_Type (Subt));
6653 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6654 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6655 Set_Size_Info (Derived_Type, Parent_Type);
6656 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6657 Set_Depends_On_Private (Derived_Type,
6658 Has_Private_Component (Derived_Type));
6659 Conditional_Delay (Derived_Type, Subt);
6661 if Is_Access_Subprogram_Type (Derived_Type) then
6662 Set_Can_Use_Internal_Rep
6663 (Derived_Type, Can_Use_Internal_Rep (Parent_Type));
6666 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6667 -- that it is not redundant.
6669 if Null_Exclusion_Present (Type_Definition (N)) then
6670 Set_Can_Never_Be_Null (Derived_Type);
6672 elsif Can_Never_Be_Null (Parent_Type) then
6673 Set_Can_Never_Be_Null (Derived_Type);
6676 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6677 -- the root type for this information.
6679 -- Apply range checks to discriminants for derived record case
6680 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6682 Desig_Type := Designated_Type (Derived_Type);
6684 if Is_Composite_Type (Desig_Type)
6685 and then (not Is_Array_Type (Desig_Type))
6686 and then Has_Discriminants (Desig_Type)
6687 and then Base_Type (Desig_Type) /= Desig_Type
6689 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6690 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6692 Discr := First_Discriminant (Base_Type (Desig_Type));
6693 while Present (Discr_Con_El) loop
6694 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6695 Next_Elmt (Discr_Con_El);
6696 Next_Discriminant (Discr);
6699 end Build_Derived_Access_Type;
6701 ------------------------------
6702 -- Build_Derived_Array_Type --
6703 ------------------------------
6705 procedure Build_Derived_Array_Type
6707 Parent_Type : Entity_Id;
6708 Derived_Type : Entity_Id)
6710 Loc : constant Source_Ptr := Sloc (N);
6711 Tdef : constant Node_Id := Type_Definition (N);
6712 Indic : constant Node_Id := Subtype_Indication (Tdef);
6713 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6714 Implicit_Base : Entity_Id := Empty;
6715 New_Indic : Node_Id;
6717 procedure Make_Implicit_Base;
6718 -- If the parent subtype is constrained, the derived type is a subtype
6719 -- of an implicit base type derived from the parent base.
6721 ------------------------
6722 -- Make_Implicit_Base --
6723 ------------------------
6725 procedure Make_Implicit_Base is
6728 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6730 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6731 Set_Etype (Implicit_Base, Parent_Base);
6733 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6734 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6736 Set_Has_Delayed_Freeze (Implicit_Base, True);
6737 end Make_Implicit_Base;
6739 -- Start of processing for Build_Derived_Array_Type
6742 if not Is_Constrained (Parent_Type) then
6743 if Nkind (Indic) /= N_Subtype_Indication then
6744 Set_Ekind (Derived_Type, E_Array_Type);
6746 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6747 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6749 Set_Has_Delayed_Freeze (Derived_Type, True);
6753 Set_Etype (Derived_Type, Implicit_Base);
6756 Make_Subtype_Declaration (Loc,
6757 Defining_Identifier => Derived_Type,
6758 Subtype_Indication =>
6759 Make_Subtype_Indication (Loc,
6760 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6761 Constraint => Constraint (Indic)));
6763 Rewrite (N, New_Indic);
6768 if Nkind (Indic) /= N_Subtype_Indication then
6771 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6772 Set_Etype (Derived_Type, Implicit_Base);
6773 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6776 Error_Msg_N ("illegal constraint on constrained type", Indic);
6780 -- If parent type is not a derived type itself, and is declared in
6781 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6782 -- the new type's concatenation operator since Derive_Subprograms
6783 -- will not inherit the parent's operator. If the parent type is
6784 -- unconstrained, the operator is of the unconstrained base type.
6786 if Number_Dimensions (Parent_Type) = 1
6787 and then not Is_Limited_Type (Parent_Type)
6788 and then not Is_Derived_Type (Parent_Type)
6789 and then not Is_Package_Or_Generic_Package
6790 (Scope (Base_Type (Parent_Type)))
6792 if not Is_Constrained (Parent_Type)
6793 and then Is_Constrained (Derived_Type)
6795 New_Concatenation_Op (Implicit_Base);
6797 New_Concatenation_Op (Derived_Type);
6800 end Build_Derived_Array_Type;
6802 -----------------------------------
6803 -- Build_Derived_Concurrent_Type --
6804 -----------------------------------
6806 procedure Build_Derived_Concurrent_Type
6808 Parent_Type : Entity_Id;
6809 Derived_Type : Entity_Id)
6811 Loc : constant Source_Ptr := Sloc (N);
6812 Def : constant Node_Id := Type_Definition (N);
6813 Indic : constant Node_Id := Subtype_Indication (Def);
6815 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6816 Corr_Decl : Node_Id;
6817 Corr_Decl_Needed : Boolean;
6818 -- If the derived type has fewer discriminants than its parent, the
6819 -- corresponding record is also a derived type, in order to account for
6820 -- the bound discriminants. We create a full type declaration for it in
6823 Constraint_Present : constant Boolean :=
6824 Nkind (Indic) = N_Subtype_Indication;
6826 D_Constraint : Node_Id;
6827 New_Constraint : Elist_Id := No_Elist;
6828 Old_Disc : Entity_Id;
6829 New_Disc : Entity_Id;
6833 Set_Stored_Constraint (Derived_Type, No_Elist);
6834 Corr_Decl_Needed := False;
6837 if Present (Discriminant_Specifications (N))
6838 and then Constraint_Present
6840 Old_Disc := First_Discriminant (Parent_Type);
6841 New_Disc := First (Discriminant_Specifications (N));
6842 while Present (New_Disc) and then Present (Old_Disc) loop
6843 Next_Discriminant (Old_Disc);
6848 if Present (Old_Disc) and then Expander_Active then
6850 -- The new type has fewer discriminants, so we need to create a new
6851 -- corresponding record, which is derived from the corresponding
6852 -- record of the parent, and has a stored constraint that captures
6853 -- the values of the discriminant constraints. The corresponding
6854 -- record is needed only if expander is active and code generation is
6857 -- The type declaration for the derived corresponding record has the
6858 -- same discriminant part and constraints as the current declaration.
6859 -- Copy the unanalyzed tree to build declaration.
6861 Corr_Decl_Needed := True;
6862 New_N := Copy_Separate_Tree (N);
6865 Make_Full_Type_Declaration (Loc,
6866 Defining_Identifier => Corr_Record,
6867 Discriminant_Specifications =>
6868 Discriminant_Specifications (New_N),
6870 Make_Derived_Type_Definition (Loc,
6871 Subtype_Indication =>
6872 Make_Subtype_Indication (Loc,
6875 (Corresponding_Record_Type (Parent_Type), Loc),
6878 (Subtype_Indication (Type_Definition (New_N))))));
6881 -- Copy Storage_Size and Relative_Deadline variables if task case
6883 if Is_Task_Type (Parent_Type) then
6884 Set_Storage_Size_Variable (Derived_Type,
6885 Storage_Size_Variable (Parent_Type));
6886 Set_Relative_Deadline_Variable (Derived_Type,
6887 Relative_Deadline_Variable (Parent_Type));
6890 if Present (Discriminant_Specifications (N)) then
6891 Push_Scope (Derived_Type);
6892 Check_Or_Process_Discriminants (N, Derived_Type);
6894 if Constraint_Present then
6896 Expand_To_Stored_Constraint
6898 Build_Discriminant_Constraints
6899 (Parent_Type, Indic, True));
6904 elsif Constraint_Present then
6906 -- Build an unconstrained derived type and rewrite the derived type
6907 -- as a subtype of this new base type.
6910 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6911 New_Base : Entity_Id;
6913 New_Indic : Node_Id;
6917 Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
6920 Make_Full_Type_Declaration (Loc,
6921 Defining_Identifier => New_Base,
6923 Make_Derived_Type_Definition (Loc,
6924 Abstract_Present => Abstract_Present (Def),
6925 Limited_Present => Limited_Present (Def),
6926 Subtype_Indication =>
6927 New_Occurrence_Of (Parent_Base, Loc)));
6929 Mark_Rewrite_Insertion (New_Decl);
6930 Insert_Before (N, New_Decl);
6934 Make_Subtype_Indication (Loc,
6935 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
6936 Constraint => Relocate_Node (Constraint (Indic)));
6939 Make_Subtype_Declaration (Loc,
6940 Defining_Identifier => Derived_Type,
6941 Subtype_Indication => New_Indic));
6948 -- By default, operations and private data are inherited from parent.
6949 -- However, in the presence of bound discriminants, a new corresponding
6950 -- record will be created, see below.
6952 Set_Has_Discriminants
6953 (Derived_Type, Has_Discriminants (Parent_Type));
6954 Set_Corresponding_Record_Type
6955 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6957 -- Is_Constrained is set according the parent subtype, but is set to
6958 -- False if the derived type is declared with new discriminants.
6962 (Is_Constrained (Parent_Type) or else Constraint_Present)
6963 and then not Present (Discriminant_Specifications (N)));
6965 if Constraint_Present then
6966 if not Has_Discriminants (Parent_Type) then
6967 Error_Msg_N ("untagged parent must have discriminants", N);
6969 elsif Present (Discriminant_Specifications (N)) then
6971 -- Verify that new discriminants are used to constrain old ones
6973 D_Constraint := First (Constraints (Constraint (Indic)));
6975 Old_Disc := First_Discriminant (Parent_Type);
6977 while Present (D_Constraint) loop
6978 if Nkind (D_Constraint) /= N_Discriminant_Association then
6980 -- Positional constraint. If it is a reference to a new
6981 -- discriminant, it constrains the corresponding old one.
6983 if Nkind (D_Constraint) = N_Identifier then
6984 New_Disc := First_Discriminant (Derived_Type);
6985 while Present (New_Disc) loop
6986 exit when Chars (New_Disc) = Chars (D_Constraint);
6987 Next_Discriminant (New_Disc);
6990 if Present (New_Disc) then
6991 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
6995 Next_Discriminant (Old_Disc);
6997 -- if this is a named constraint, search by name for the old
6998 -- discriminants constrained by the new one.
7000 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7002 -- Find new discriminant with that name
7004 New_Disc := First_Discriminant (Derived_Type);
7005 while Present (New_Disc) loop
7007 Chars (New_Disc) = Chars (Expression (D_Constraint));
7008 Next_Discriminant (New_Disc);
7011 if Present (New_Disc) then
7013 -- Verify that new discriminant renames some discriminant
7014 -- of the parent type, and associate the new discriminant
7015 -- with one or more old ones that it renames.
7021 Selector := First (Selector_Names (D_Constraint));
7022 while Present (Selector) loop
7023 Old_Disc := First_Discriminant (Parent_Type);
7024 while Present (Old_Disc) loop
7025 exit when Chars (Old_Disc) = Chars (Selector);
7026 Next_Discriminant (Old_Disc);
7029 if Present (Old_Disc) then
7030 Set_Corresponding_Discriminant
7031 (New_Disc, Old_Disc);
7040 Next (D_Constraint);
7043 New_Disc := First_Discriminant (Derived_Type);
7044 while Present (New_Disc) loop
7045 if No (Corresponding_Discriminant (New_Disc)) then
7047 ("new discriminant& must constrain old one", N, New_Disc);
7050 Subtypes_Statically_Compatible
7052 Etype (Corresponding_Discriminant (New_Disc)))
7055 ("& not statically compatible with parent discriminant",
7059 Next_Discriminant (New_Disc);
7063 elsif Present (Discriminant_Specifications (N)) then
7065 ("missing discriminant constraint in untagged derivation", N);
7068 -- The entity chain of the derived type includes the new discriminants
7069 -- but shares operations with the parent.
7071 if Present (Discriminant_Specifications (N)) then
7072 Old_Disc := First_Discriminant (Parent_Type);
7073 while Present (Old_Disc) loop
7074 if No (Next_Entity (Old_Disc))
7075 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7078 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7082 Next_Discriminant (Old_Disc);
7086 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7087 if Has_Discriminants (Parent_Type) then
7088 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7089 Set_Discriminant_Constraint (
7090 Derived_Type, Discriminant_Constraint (Parent_Type));
7094 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7096 Set_Has_Completion (Derived_Type);
7098 if Corr_Decl_Needed then
7099 Set_Stored_Constraint (Derived_Type, New_Constraint);
7100 Insert_After (N, Corr_Decl);
7101 Analyze (Corr_Decl);
7102 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7104 end Build_Derived_Concurrent_Type;
7106 ------------------------------------
7107 -- Build_Derived_Enumeration_Type --
7108 ------------------------------------
7110 procedure Build_Derived_Enumeration_Type
7112 Parent_Type : Entity_Id;
7113 Derived_Type : Entity_Id)
7115 function Bound_Belongs_To_Type (B : Node_Id) return Boolean;
7116 -- When the type declaration includes a constraint, we generate
7117 -- a subtype declaration of an anonymous base type, with the constraint
7118 -- given in the original type declaration. Conceptually, the bounds
7119 -- are converted to the new base type, and this conversion freezes
7120 -- (prematurely) that base type, when the bounds are simply literals.
7121 -- As a result, a representation clause for the derived type is then
7122 -- rejected or ignored. This procedure recognizes the simple case of
7123 -- literal bounds, which allows us to indicate that the conversions
7124 -- are not freeze points, and the subsequent representation clause
7126 -- A similar approach might be used to resolve the long-standing
7127 -- problem of premature freezing of derived numeric types ???
7129 function Bound_Belongs_To_Type (B : Node_Id) return Boolean is
7131 return Nkind (B) = N_Type_Conversion
7132 and then Is_Entity_Name (Expression (B))
7133 and then Ekind (Entity (Expression (B))) = E_Enumeration_Literal;
7134 end Bound_Belongs_To_Type;
7136 Loc : constant Source_Ptr := Sloc (N);
7137 Def : constant Node_Id := Type_Definition (N);
7138 Indic : constant Node_Id := Subtype_Indication (Def);
7139 Implicit_Base : Entity_Id;
7140 Literal : Entity_Id;
7141 New_Lit : Entity_Id;
7142 Literals_List : List_Id;
7143 Type_Decl : Node_Id;
7145 Rang_Expr : Node_Id;
7148 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7149 -- not have explicit literals lists we need to process types derived
7150 -- from them specially. This is handled by Derived_Standard_Character.
7151 -- If the parent type is a generic type, there are no literals either,
7152 -- and we construct the same skeletal representation as for the generic
7155 if Is_Standard_Character_Type (Parent_Type) then
7156 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7158 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7164 if Nkind (Indic) /= N_Subtype_Indication then
7166 Make_Attribute_Reference (Loc,
7167 Attribute_Name => Name_First,
7168 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7169 Set_Etype (Lo, Derived_Type);
7172 Make_Attribute_Reference (Loc,
7173 Attribute_Name => Name_Last,
7174 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7175 Set_Etype (Hi, Derived_Type);
7177 Set_Scalar_Range (Derived_Type,
7183 -- Analyze subtype indication and verify compatibility
7184 -- with parent type.
7186 if Base_Type (Process_Subtype (Indic, N)) /=
7187 Base_Type (Parent_Type)
7190 ("illegal constraint for formal discrete type", N);
7196 -- If a constraint is present, analyze the bounds to catch
7197 -- premature usage of the derived literals.
7199 if Nkind (Indic) = N_Subtype_Indication
7200 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7202 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7203 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7206 -- Introduce an implicit base type for the derived type even if there
7207 -- is no constraint attached to it, since this seems closer to the
7208 -- Ada semantics. Build a full type declaration tree for the derived
7209 -- type using the implicit base type as the defining identifier. The
7210 -- build a subtype declaration tree which applies the constraint (if
7211 -- any) have it replace the derived type declaration.
7213 Literal := First_Literal (Parent_Type);
7214 Literals_List := New_List;
7215 while Present (Literal)
7216 and then Ekind (Literal) = E_Enumeration_Literal
7218 -- Literals of the derived type have the same representation as
7219 -- those of the parent type, but this representation can be
7220 -- overridden by an explicit representation clause. Indicate
7221 -- that there is no explicit representation given yet. These
7222 -- derived literals are implicit operations of the new type,
7223 -- and can be overridden by explicit ones.
7225 if Nkind (Literal) = N_Defining_Character_Literal then
7227 Make_Defining_Character_Literal (Loc, Chars (Literal));
7229 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7232 Set_Ekind (New_Lit, E_Enumeration_Literal);
7233 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7234 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7235 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7236 Set_Alias (New_Lit, Literal);
7237 Set_Is_Known_Valid (New_Lit, True);
7239 Append (New_Lit, Literals_List);
7240 Next_Literal (Literal);
7244 Make_Defining_Identifier (Sloc (Derived_Type),
7245 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7247 -- Indicate the proper nature of the derived type. This must be done
7248 -- before analysis of the literals, to recognize cases when a literal
7249 -- may be hidden by a previous explicit function definition (cf.
7252 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
7253 Set_Etype (Derived_Type, Implicit_Base);
7256 Make_Full_Type_Declaration (Loc,
7257 Defining_Identifier => Implicit_Base,
7258 Discriminant_Specifications => No_List,
7260 Make_Enumeration_Type_Definition (Loc, Literals_List));
7262 Mark_Rewrite_Insertion (Type_Decl);
7263 Insert_Before (N, Type_Decl);
7264 Analyze (Type_Decl);
7266 -- The anonymous base now has a full declaration, but this base
7267 -- is not a first subtype.
7269 Set_Is_First_Subtype (Implicit_Base, False);
7271 -- After the implicit base is analyzed its Etype needs to be changed
7272 -- to reflect the fact that it is derived from the parent type which
7273 -- was ignored during analysis. We also set the size at this point.
7275 Set_Etype (Implicit_Base, Parent_Type);
7277 Set_Size_Info (Implicit_Base, Parent_Type);
7278 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7279 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7281 -- Copy other flags from parent type
7283 Set_Has_Non_Standard_Rep
7284 (Implicit_Base, Has_Non_Standard_Rep
7286 Set_Has_Pragma_Ordered
7287 (Implicit_Base, Has_Pragma_Ordered
7289 Set_Has_Delayed_Freeze (Implicit_Base);
7291 -- Process the subtype indication including a validation check on the
7292 -- constraint, if any. If a constraint is given, its bounds must be
7293 -- implicitly converted to the new type.
7295 if Nkind (Indic) = N_Subtype_Indication then
7297 R : constant Node_Id :=
7298 Range_Expression (Constraint (Indic));
7301 if Nkind (R) = N_Range then
7302 Hi := Build_Scalar_Bound
7303 (High_Bound (R), Parent_Type, Implicit_Base);
7304 Lo := Build_Scalar_Bound
7305 (Low_Bound (R), Parent_Type, Implicit_Base);
7308 -- Constraint is a Range attribute. Replace with explicit
7309 -- mention of the bounds of the prefix, which must be a
7312 Analyze (Prefix (R));
7314 Convert_To (Implicit_Base,
7315 Make_Attribute_Reference (Loc,
7316 Attribute_Name => Name_Last,
7318 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7321 Convert_To (Implicit_Base,
7322 Make_Attribute_Reference (Loc,
7323 Attribute_Name => Name_First,
7325 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7332 (Type_High_Bound (Parent_Type),
7333 Parent_Type, Implicit_Base);
7336 (Type_Low_Bound (Parent_Type),
7337 Parent_Type, Implicit_Base);
7345 -- If we constructed a default range for the case where no range
7346 -- was given, then the expressions in the range must not freeze
7347 -- since they do not correspond to expressions in the source.
7348 -- However, if the type inherits predicates the expressions will
7349 -- be elaborated earlier and must freeze.
7351 if (Nkind (Indic) /= N_Subtype_Indication
7353 (Bound_Belongs_To_Type (Lo) and then Bound_Belongs_To_Type (Hi)))
7354 and then not Has_Predicates (Derived_Type)
7356 Set_Must_Not_Freeze (Lo);
7357 Set_Must_Not_Freeze (Hi);
7358 Set_Must_Not_Freeze (Rang_Expr);
7362 Make_Subtype_Declaration (Loc,
7363 Defining_Identifier => Derived_Type,
7364 Subtype_Indication =>
7365 Make_Subtype_Indication (Loc,
7366 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
7368 Make_Range_Constraint (Loc,
7369 Range_Expression => Rang_Expr))));
7373 -- Propagate the aspects from the original type declaration to the
7374 -- declaration of the implicit base.
7376 Move_Aspects (From => Original_Node (N), To => Type_Decl);
7378 -- Apply a range check. Since this range expression doesn't have an
7379 -- Etype, we have to specifically pass the Source_Typ parameter. Is
7382 if Nkind (Indic) = N_Subtype_Indication then
7384 (Range_Expression (Constraint (Indic)), Parent_Type,
7385 Source_Typ => Entity (Subtype_Mark (Indic)));
7388 end Build_Derived_Enumeration_Type;
7390 --------------------------------
7391 -- Build_Derived_Numeric_Type --
7392 --------------------------------
7394 procedure Build_Derived_Numeric_Type
7396 Parent_Type : Entity_Id;
7397 Derived_Type : Entity_Id)
7399 Loc : constant Source_Ptr := Sloc (N);
7400 Tdef : constant Node_Id := Type_Definition (N);
7401 Indic : constant Node_Id := Subtype_Indication (Tdef);
7402 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
7403 No_Constraint : constant Boolean := Nkind (Indic) /=
7404 N_Subtype_Indication;
7405 Implicit_Base : Entity_Id;
7411 -- Process the subtype indication including a validation check on
7412 -- the constraint if any.
7414 Discard_Node (Process_Subtype (Indic, N));
7416 -- Introduce an implicit base type for the derived type even if there
7417 -- is no constraint attached to it, since this seems closer to the Ada
7421 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
7423 Set_Etype (Implicit_Base, Parent_Base);
7424 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
7425 Set_Size_Info (Implicit_Base, Parent_Base);
7426 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Base));
7427 Set_Parent (Implicit_Base, Parent (Derived_Type));
7428 Set_Is_Known_Valid (Implicit_Base, Is_Known_Valid (Parent_Base));
7430 -- Set RM Size for discrete type or decimal fixed-point type
7431 -- Ordinary fixed-point is excluded, why???
7433 if Is_Discrete_Type (Parent_Base)
7434 or else Is_Decimal_Fixed_Point_Type (Parent_Base)
7436 Set_RM_Size (Implicit_Base, RM_Size (Parent_Base));
7439 Set_Has_Delayed_Freeze (Implicit_Base);
7441 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
7442 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
7444 Set_Scalar_Range (Implicit_Base,
7449 if Has_Infinities (Parent_Base) then
7450 Set_Includes_Infinities (Scalar_Range (Implicit_Base));
7453 -- The Derived_Type, which is the entity of the declaration, is a
7454 -- subtype of the implicit base. Its Ekind is a subtype, even in the
7455 -- absence of an explicit constraint.
7457 Set_Etype (Derived_Type, Implicit_Base);
7459 -- If we did not have a constraint, then the Ekind is set from the
7460 -- parent type (otherwise Process_Subtype has set the bounds)
7462 if No_Constraint then
7463 Set_Ekind (Derived_Type, Subtype_Kind (Ekind (Parent_Type)));
7466 -- If we did not have a range constraint, then set the range from the
7467 -- parent type. Otherwise, the Process_Subtype call has set the bounds.
7469 if No_Constraint or else not Has_Range_Constraint (Indic) then
7470 Set_Scalar_Range (Derived_Type,
7472 Low_Bound => New_Copy_Tree (Type_Low_Bound (Parent_Type)),
7473 High_Bound => New_Copy_Tree (Type_High_Bound (Parent_Type))));
7474 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7476 if Has_Infinities (Parent_Type) then
7477 Set_Includes_Infinities (Scalar_Range (Derived_Type));
7480 Set_Is_Known_Valid (Derived_Type, Is_Known_Valid (Parent_Type));
7483 Set_Is_Descendant_Of_Address (Derived_Type,
7484 Is_Descendant_Of_Address (Parent_Type));
7485 Set_Is_Descendant_Of_Address (Implicit_Base,
7486 Is_Descendant_Of_Address (Parent_Type));
7488 -- Set remaining type-specific fields, depending on numeric type
7490 if Is_Modular_Integer_Type (Parent_Type) then
7491 Set_Modulus (Implicit_Base, Modulus (Parent_Base));
7493 Set_Non_Binary_Modulus
7494 (Implicit_Base, Non_Binary_Modulus (Parent_Base));
7497 (Implicit_Base, Is_Known_Valid (Parent_Base));
7499 elsif Is_Floating_Point_Type (Parent_Type) then
7501 -- Digits of base type is always copied from the digits value of
7502 -- the parent base type, but the digits of the derived type will
7503 -- already have been set if there was a constraint present.
7505 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7506 Set_Float_Rep (Implicit_Base, Float_Rep (Parent_Base));
7508 if No_Constraint then
7509 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Type));
7512 elsif Is_Fixed_Point_Type (Parent_Type) then
7514 -- Small of base type and derived type are always copied from the
7515 -- parent base type, since smalls never change. The delta of the
7516 -- base type is also copied from the parent base type. However the
7517 -- delta of the derived type will have been set already if a
7518 -- constraint was present.
7520 Set_Small_Value (Derived_Type, Small_Value (Parent_Base));
7521 Set_Small_Value (Implicit_Base, Small_Value (Parent_Base));
7522 Set_Delta_Value (Implicit_Base, Delta_Value (Parent_Base));
7524 if No_Constraint then
7525 Set_Delta_Value (Derived_Type, Delta_Value (Parent_Type));
7528 -- The scale and machine radix in the decimal case are always
7529 -- copied from the parent base type.
7531 if Is_Decimal_Fixed_Point_Type (Parent_Type) then
7532 Set_Scale_Value (Derived_Type, Scale_Value (Parent_Base));
7533 Set_Scale_Value (Implicit_Base, Scale_Value (Parent_Base));
7535 Set_Machine_Radix_10
7536 (Derived_Type, Machine_Radix_10 (Parent_Base));
7537 Set_Machine_Radix_10
7538 (Implicit_Base, Machine_Radix_10 (Parent_Base));
7540 Set_Digits_Value (Implicit_Base, Digits_Value (Parent_Base));
7542 if No_Constraint then
7543 Set_Digits_Value (Derived_Type, Digits_Value (Parent_Base));
7546 -- the analysis of the subtype_indication sets the
7547 -- digits value of the derived type.
7554 if Is_Integer_Type (Parent_Type) then
7555 Set_Has_Shift_Operator
7556 (Implicit_Base, Has_Shift_Operator (Parent_Type));
7559 -- The type of the bounds is that of the parent type, and they
7560 -- must be converted to the derived type.
7562 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
7564 -- The implicit_base should be frozen when the derived type is frozen,
7565 -- but note that it is used in the conversions of the bounds. For fixed
7566 -- types we delay the determination of the bounds until the proper
7567 -- freezing point. For other numeric types this is rejected by GCC, for
7568 -- reasons that are currently unclear (???), so we choose to freeze the
7569 -- implicit base now. In the case of integers and floating point types
7570 -- this is harmless because subsequent representation clauses cannot
7571 -- affect anything, but it is still baffling that we cannot use the
7572 -- same mechanism for all derived numeric types.
7574 -- There is a further complication: actually some representation
7575 -- clauses can affect the implicit base type. For example, attribute
7576 -- definition clauses for stream-oriented attributes need to set the
7577 -- corresponding TSS entries on the base type, and this normally
7578 -- cannot be done after the base type is frozen, so the circuitry in
7579 -- Sem_Ch13.New_Stream_Subprogram must account for this possibility
7580 -- and not use Set_TSS in this case.
7582 -- There are also consequences for the case of delayed representation
7583 -- aspects for some cases. For example, a Size aspect is delayed and
7584 -- should not be evaluated to the freeze point. This early freezing
7585 -- means that the size attribute evaluation happens too early???
7587 if Is_Fixed_Point_Type (Parent_Type) then
7588 Conditional_Delay (Implicit_Base, Parent_Type);
7590 Freeze_Before (N, Implicit_Base);
7592 end Build_Derived_Numeric_Type;
7594 --------------------------------
7595 -- Build_Derived_Private_Type --
7596 --------------------------------
7598 procedure Build_Derived_Private_Type
7600 Parent_Type : Entity_Id;
7601 Derived_Type : Entity_Id;
7602 Is_Completion : Boolean;
7603 Derive_Subps : Boolean := True)
7605 Loc : constant Source_Ptr := Sloc (N);
7606 Par_Base : constant Entity_Id := Base_Type (Parent_Type);
7607 Par_Scope : constant Entity_Id := Scope (Par_Base);
7608 Full_N : constant Node_Id := New_Copy_Tree (N);
7609 Full_Der : Entity_Id := New_Copy (Derived_Type);
7612 procedure Build_Full_Derivation;
7613 -- Build full derivation, i.e. derive from the full view
7615 procedure Copy_And_Build;
7616 -- Copy derived type declaration, replace parent with its full view,
7617 -- and build derivation
7619 ---------------------------
7620 -- Build_Full_Derivation --
7621 ---------------------------
7623 procedure Build_Full_Derivation is
7625 -- If parent scope is not open, install the declarations
7627 if not In_Open_Scopes (Par_Scope) then
7628 Install_Private_Declarations (Par_Scope);
7629 Install_Visible_Declarations (Par_Scope);
7631 Uninstall_Declarations (Par_Scope);
7633 -- If parent scope is open and in another unit, and parent has a
7634 -- completion, then the derivation is taking place in the visible
7635 -- part of a child unit. In that case retrieve the full view of
7636 -- the parent momentarily.
7638 elsif not In_Same_Source_Unit (N, Parent_Type) then
7639 Full_P := Full_View (Parent_Type);
7640 Exchange_Declarations (Parent_Type);
7642 Exchange_Declarations (Full_P);
7644 -- Otherwise it is a local derivation
7649 end Build_Full_Derivation;
7651 --------------------
7652 -- Copy_And_Build --
7653 --------------------
7655 procedure Copy_And_Build is
7656 Full_Parent : Entity_Id := Parent_Type;
7659 -- If the parent is itself derived from another private type,
7660 -- installing the private declarations has not affected its
7661 -- privacy status, so use its own full view explicitly.
7663 if Is_Private_Type (Full_Parent)
7664 and then Present (Full_View (Full_Parent))
7666 Full_Parent := Full_View (Full_Parent);
7669 -- And its underlying full view if necessary
7671 if Is_Private_Type (Full_Parent)
7672 and then Present (Underlying_Full_View (Full_Parent))
7674 Full_Parent := Underlying_Full_View (Full_Parent);
7677 -- For record, concurrent, access and most enumeration types, the
7678 -- derivation from full view requires a fully-fledged declaration.
7679 -- In the other cases, just use an itype.
7681 if Is_Record_Type (Full_Parent)
7682 or else Is_Concurrent_Type (Full_Parent)
7683 or else Is_Access_Type (Full_Parent)
7685 (Is_Enumeration_Type (Full_Parent)
7686 and then not Is_Standard_Character_Type (Full_Parent)
7687 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7689 -- Copy and adjust declaration to provide a completion for what
7690 -- is originally a private declaration. Indicate that full view
7691 -- is internally generated.
7693 Set_Comes_From_Source (Full_N, False);
7694 Set_Comes_From_Source (Full_Der, False);
7695 Set_Parent (Full_Der, Full_N);
7696 Set_Defining_Identifier (Full_N, Full_Der);
7698 -- If there are no constraints, adjust the subtype mark
7700 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7701 N_Subtype_Indication
7703 Set_Subtype_Indication
7704 (Type_Definition (Full_N),
7705 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7708 Insert_After (N, Full_N);
7710 -- Build full view of derived type from full view of parent which
7711 -- is now installed. Subprograms have been derived on the partial
7712 -- view, the completion does not derive them anew.
7714 if Is_Record_Type (Full_Parent) then
7716 -- If parent type is tagged, the completion inherits the proper
7717 -- primitive operations.
7719 if Is_Tagged_Type (Parent_Type) then
7720 Build_Derived_Record_Type
7721 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7723 Build_Derived_Record_Type
7724 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7729 (Full_N, Full_Parent, Full_Der,
7730 Is_Completion => False, Derive_Subps => False);
7733 -- The full declaration has been introduced into the tree and
7734 -- processed in the step above. It should not be analyzed again
7735 -- (when encountered later in the current list of declarations)
7736 -- to prevent spurious name conflicts. The full entity remains
7739 Set_Analyzed (Full_N);
7743 Make_Defining_Identifier (Sloc (Derived_Type),
7744 Chars => Chars (Derived_Type));
7745 Set_Is_Itype (Full_Der);
7746 Set_Associated_Node_For_Itype (Full_Der, N);
7747 Set_Parent (Full_Der, N);
7749 (N, Full_Parent, Full_Der,
7750 Is_Completion => False, Derive_Subps => False);
7753 Set_Has_Private_Declaration (Full_Der);
7754 Set_Has_Private_Declaration (Derived_Type);
7756 Set_Scope (Full_Der, Scope (Derived_Type));
7757 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7758 Set_Has_Size_Clause (Full_Der, False);
7759 Set_Has_Alignment_Clause (Full_Der, False);
7760 Set_Has_Delayed_Freeze (Full_Der);
7761 Set_Is_Frozen (Full_Der, False);
7762 Set_Freeze_Node (Full_Der, Empty);
7763 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7764 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7766 -- The convention on the base type may be set in the private part
7767 -- and not propagated to the subtype until later, so we obtain the
7768 -- convention from the base type of the parent.
7770 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7773 -- Start of processing for Build_Derived_Private_Type
7776 if Is_Tagged_Type (Parent_Type) then
7777 Full_P := Full_View (Parent_Type);
7779 -- A type extension of a type with unknown discriminants is an
7780 -- indefinite type that the back-end cannot handle directly.
7781 -- We treat it as a private type, and build a completion that is
7782 -- derived from the full view of the parent, and hopefully has
7783 -- known discriminants.
7785 -- If the full view of the parent type has an underlying record view,
7786 -- use it to generate the underlying record view of this derived type
7787 -- (required for chains of derivations with unknown discriminants).
7789 -- Minor optimization: we avoid the generation of useless underlying
7790 -- record view entities if the private type declaration has unknown
7791 -- discriminants but its corresponding full view has no
7794 if Has_Unknown_Discriminants (Parent_Type)
7795 and then Present (Full_P)
7796 and then (Has_Discriminants (Full_P)
7797 or else Present (Underlying_Record_View (Full_P)))
7798 and then not In_Open_Scopes (Par_Scope)
7799 and then Expander_Active
7802 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7803 New_Ext : constant Node_Id :=
7805 (Record_Extension_Part (Type_Definition (N)));
7809 Build_Derived_Record_Type
7810 (N, Parent_Type, Derived_Type, Derive_Subps);
7812 -- Build anonymous completion, as a derivation from the full
7813 -- view of the parent. This is not a completion in the usual
7814 -- sense, because the current type is not private.
7817 Make_Full_Type_Declaration (Loc,
7818 Defining_Identifier => Full_Der,
7820 Make_Derived_Type_Definition (Loc,
7821 Subtype_Indication =>
7823 (Subtype_Indication (Type_Definition (N))),
7824 Record_Extension_Part => New_Ext));
7826 -- If the parent type has an underlying record view, use it
7827 -- here to build the new underlying record view.
7829 if Present (Underlying_Record_View (Full_P)) then
7831 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7833 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7834 Underlying_Record_View (Full_P));
7837 Install_Private_Declarations (Par_Scope);
7838 Install_Visible_Declarations (Par_Scope);
7839 Insert_Before (N, Decl);
7841 -- Mark entity as an underlying record view before analysis,
7842 -- to avoid generating the list of its primitive operations
7843 -- (which is not really required for this entity) and thus
7844 -- prevent spurious errors associated with missing overriding
7845 -- of abstract primitives (overridden only for Derived_Type).
7847 Set_Ekind (Full_Der, E_Record_Type);
7848 Set_Is_Underlying_Record_View (Full_Der);
7849 Set_Default_SSO (Full_Der);
7850 Set_No_Reordering (Full_Der, No_Component_Reordering);
7854 pragma Assert (Has_Discriminants (Full_Der)
7855 and then not Has_Unknown_Discriminants (Full_Der));
7857 Uninstall_Declarations (Par_Scope);
7859 -- Freeze the underlying record view, to prevent generation of
7860 -- useless dispatching information, which is simply shared with
7861 -- the real derived type.
7863 Set_Is_Frozen (Full_Der);
7865 -- If the derived type has access discriminants, create
7866 -- references to their anonymous types now, to prevent
7867 -- back-end problems when their first use is in generated
7868 -- bodies of primitives.
7874 E := First_Entity (Full_Der);
7876 while Present (E) loop
7877 if Ekind (E) = E_Discriminant
7878 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7880 Build_Itype_Reference (Etype (E), Decl);
7887 -- Set up links between real entity and underlying record view
7889 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7890 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7893 -- If discriminants are known, build derived record
7896 Build_Derived_Record_Type
7897 (N, Parent_Type, Derived_Type, Derive_Subps);
7902 elsif Has_Discriminants (Parent_Type) then
7904 -- Build partial view of derived type from partial view of parent.
7905 -- This must be done before building the full derivation because the
7906 -- second derivation will modify the discriminants of the first and
7907 -- the discriminants are chained with the rest of the components in
7908 -- the full derivation.
7910 Build_Derived_Record_Type
7911 (N, Parent_Type, Derived_Type, Derive_Subps);
7913 -- Build the full derivation if this is not the anonymous derived
7914 -- base type created by Build_Derived_Record_Type in the constrained
7915 -- case (see point 5. of its head comment) since we build it for the
7918 if Present (Full_View (Parent_Type))
7919 and then not Is_Itype (Derived_Type)
7922 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7924 Last_Discr : Entity_Id;
7927 -- If this is not a completion, construct the implicit full
7928 -- view by deriving from the full view of the parent type.
7929 -- But if this is a completion, the derived private type
7930 -- being built is a full view and the full derivation can
7931 -- only be its underlying full view.
7933 Build_Full_Derivation;
7935 if not Is_Completion then
7936 Set_Full_View (Derived_Type, Full_Der);
7938 Set_Underlying_Full_View (Derived_Type, Full_Der);
7939 Set_Is_Underlying_Full_View (Full_Der);
7942 if not Is_Base_Type (Derived_Type) then
7943 Set_Full_View (Der_Base, Base_Type (Full_Der));
7946 -- Copy the discriminant list from full view to the partial
7947 -- view (base type and its subtype). Gigi requires that the
7948 -- partial and full views have the same discriminants.
7950 -- Note that since the partial view points to discriminants
7951 -- in the full view, their scope will be that of the full
7952 -- view. This might cause some front end problems and need
7955 Discr := First_Discriminant (Base_Type (Full_Der));
7956 Set_First_Entity (Der_Base, Discr);
7959 Last_Discr := Discr;
7960 Next_Discriminant (Discr);
7961 exit when No (Discr);
7964 Set_Last_Entity (Der_Base, Last_Discr);
7965 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7966 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7970 elsif Present (Full_View (Parent_Type))
7971 and then Has_Discriminants (Full_View (Parent_Type))
7973 if Has_Unknown_Discriminants (Parent_Type)
7974 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7975 N_Subtype_Indication
7978 ("cannot constrain type with unknown discriminants",
7979 Subtype_Indication (Type_Definition (N)));
7983 -- If this is not a completion, construct the implicit full view by
7984 -- deriving from the full view of the parent type. But if this is a
7985 -- completion, the derived private type being built is a full view
7986 -- and the full derivation can only be its underlying full view.
7988 Build_Full_Derivation;
7990 if not Is_Completion then
7991 Set_Full_View (Derived_Type, Full_Der);
7993 Set_Underlying_Full_View (Derived_Type, Full_Der);
7994 Set_Is_Underlying_Full_View (Full_Der);
7997 -- In any case, the primitive operations are inherited from the
7998 -- parent type, not from the internal full view.
8000 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8002 if Derive_Subps then
8003 Derive_Subprograms (Parent_Type, Derived_Type);
8006 Set_Stored_Constraint (Derived_Type, No_Elist);
8008 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
8011 -- Untagged type, No discriminants on either view
8013 if Nkind (Subtype_Indication (Type_Definition (N))) =
8014 N_Subtype_Indication
8017 ("illegal constraint on type without discriminants", N);
8020 if Present (Discriminant_Specifications (N))
8021 and then Present (Full_View (Parent_Type))
8022 and then not Is_Tagged_Type (Full_View (Parent_Type))
8024 Error_Msg_N ("cannot add discriminants to untagged type", N);
8027 Set_Stored_Constraint (Derived_Type, No_Elist);
8028 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8030 Set_Is_Controlled_Active
8031 (Derived_Type, Is_Controlled_Active (Parent_Type));
8033 Set_Disable_Controlled
8034 (Derived_Type, Disable_Controlled (Parent_Type));
8036 Set_Has_Controlled_Component
8037 (Derived_Type, Has_Controlled_Component (Parent_Type));
8039 -- Direct controlled types do not inherit Finalize_Storage_Only flag
8041 if not Is_Controlled (Parent_Type) then
8042 Set_Finalize_Storage_Only
8043 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8046 -- If this is not a completion, construct the implicit full view by
8047 -- deriving from the full view of the parent type.
8049 -- ??? If the parent is untagged private and its completion is
8050 -- tagged, this mechanism will not work because we cannot derive from
8051 -- the tagged full view unless we have an extension.
8053 if Present (Full_View (Parent_Type))
8054 and then not Is_Tagged_Type (Full_View (Parent_Type))
8055 and then not Is_Completion
8057 Build_Full_Derivation;
8058 Set_Full_View (Derived_Type, Full_Der);
8062 Set_Has_Unknown_Discriminants (Derived_Type,
8063 Has_Unknown_Discriminants (Parent_Type));
8065 if Is_Private_Type (Derived_Type) then
8066 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8069 -- If the parent base type is in scope, add the derived type to its
8070 -- list of private dependents, because its full view may become
8071 -- visible subsequently (in a nested private part, a body, or in a
8072 -- further child unit).
8074 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8075 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8077 -- Check for unusual case where a type completed by a private
8078 -- derivation occurs within a package nested in a child unit, and
8079 -- the parent is declared in an ancestor.
8081 if Is_Child_Unit (Scope (Current_Scope))
8082 and then Is_Completion
8083 and then In_Private_Part (Current_Scope)
8084 and then Scope (Parent_Type) /= Current_Scope
8086 -- Note that if the parent has a completion in the private part,
8087 -- (which is itself a derivation from some other private type)
8088 -- it is that completion that is visible, there is no full view
8089 -- available, and no special processing is needed.
8091 and then Present (Full_View (Parent_Type))
8093 -- In this case, the full view of the parent type will become
8094 -- visible in the body of the enclosing child, and only then will
8095 -- the current type be possibly non-private. Build an underlying
8096 -- full view that will be installed when the enclosing child body
8099 if Present (Underlying_Full_View (Derived_Type)) then
8100 Full_Der := Underlying_Full_View (Derived_Type);
8102 Build_Full_Derivation;
8103 Set_Underlying_Full_View (Derived_Type, Full_Der);
8104 Set_Is_Underlying_Full_View (Full_Der);
8107 -- The full view will be used to swap entities on entry/exit to
8108 -- the body, and must appear in the entity list for the package.
8110 Append_Entity (Full_Der, Scope (Derived_Type));
8113 end Build_Derived_Private_Type;
8115 -------------------------------
8116 -- Build_Derived_Record_Type --
8117 -------------------------------
8121 -- Ideally we would like to use the same model of type derivation for
8122 -- tagged and untagged record types. Unfortunately this is not quite
8123 -- possible because the semantics of representation clauses is different
8124 -- for tagged and untagged records under inheritance. Consider the
8127 -- type R (...) is [tagged] record ... end record;
8128 -- type T (...) is new R (...) [with ...];
8130 -- The representation clauses for T can specify a completely different
8131 -- record layout from R's. Hence the same component can be placed in two
8132 -- very different positions in objects of type T and R. If R and T are
8133 -- tagged types, representation clauses for T can only specify the layout
8134 -- of non inherited components, thus components that are common in R and T
8135 -- have the same position in objects of type R and T.
8137 -- This has two implications. The first is that the entire tree for R's
8138 -- declaration needs to be copied for T in the untagged case, so that T
8139 -- can be viewed as a record type of its own with its own representation
8140 -- clauses. The second implication is the way we handle discriminants.
8141 -- Specifically, in the untagged case we need a way to communicate to Gigi
8142 -- what are the real discriminants in the record, while for the semantics
8143 -- we need to consider those introduced by the user to rename the
8144 -- discriminants in the parent type. This is handled by introducing the
8145 -- notion of stored discriminants. See below for more.
8147 -- Fortunately the way regular components are inherited can be handled in
8148 -- the same way in tagged and untagged types.
8150 -- To complicate things a bit more the private view of a private extension
8151 -- cannot be handled in the same way as the full view (for one thing the
8152 -- semantic rules are somewhat different). We will explain what differs
8155 -- 2. DISCRIMINANTS UNDER INHERITANCE
8157 -- The semantic rules governing the discriminants of derived types are
8160 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8161 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8163 -- If parent type has discriminants, then the discriminants that are
8164 -- declared in the derived type are [3.4 (11)]:
8166 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8169 -- o Otherwise, each discriminant of the parent type (implicitly declared
8170 -- in the same order with the same specifications). In this case, the
8171 -- discriminants are said to be "inherited", or if unknown in the parent
8172 -- are also unknown in the derived type.
8174 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8176 -- o The parent subtype must be constrained;
8178 -- o If the parent type is not a tagged type, then each discriminant of
8179 -- the derived type must be used in the constraint defining a parent
8180 -- subtype. [Implementation note: This ensures that the new discriminant
8181 -- can share storage with an existing discriminant.]
8183 -- For the derived type each discriminant of the parent type is either
8184 -- inherited, constrained to equal some new discriminant of the derived
8185 -- type, or constrained to the value of an expression.
8187 -- When inherited or constrained to equal some new discriminant, the
8188 -- parent discriminant and the discriminant of the derived type are said
8191 -- If a discriminant of the parent type is constrained to a specific value
8192 -- in the derived type definition, then the discriminant is said to be
8193 -- "specified" by that derived type definition.
8195 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8197 -- We have spoken about stored discriminants in point 1 (introduction)
8198 -- above. There are two sorts of stored discriminants: implicit and
8199 -- explicit. As long as the derived type inherits the same discriminants as
8200 -- the root record type, stored discriminants are the same as regular
8201 -- discriminants, and are said to be implicit. However, if any discriminant
8202 -- in the root type was renamed in the derived type, then the derived
8203 -- type will contain explicit stored discriminants. Explicit stored
8204 -- discriminants are discriminants in addition to the semantically visible
8205 -- discriminants defined for the derived type. Stored discriminants are
8206 -- used by Gigi to figure out what are the physical discriminants in
8207 -- objects of the derived type (see precise definition in einfo.ads).
8208 -- As an example, consider the following:
8210 -- type R (D1, D2, D3 : Int) is record ... end record;
8211 -- type T1 is new R;
8212 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8213 -- type T3 is new T2;
8214 -- type T4 (Y : Int) is new T3 (Y, 99);
8216 -- The following table summarizes the discriminants and stored
8217 -- discriminants in R and T1 through T4:
8219 -- Type Discrim Stored Discrim Comment
8220 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8221 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8222 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8223 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8224 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8226 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8227 -- find the corresponding discriminant in the parent type, while
8228 -- Original_Record_Component (abbreviated ORC below) the actual physical
8229 -- component that is renamed. Finally the field Is_Completely_Hidden
8230 -- (abbreviated ICH below) is set for all explicit stored discriminants
8231 -- (see einfo.ads for more info). For the above example this gives:
8233 -- Discrim CD ORC ICH
8234 -- ^^^^^^^ ^^ ^^^ ^^^
8235 -- D1 in R empty itself no
8236 -- D2 in R empty itself no
8237 -- D3 in R empty itself no
8239 -- D1 in T1 D1 in R itself no
8240 -- D2 in T1 D2 in R itself no
8241 -- D3 in T1 D3 in R itself no
8243 -- X1 in T2 D3 in T1 D3 in T2 no
8244 -- X2 in T2 D1 in T1 D1 in T2 no
8245 -- D1 in T2 empty itself yes
8246 -- D2 in T2 empty itself yes
8247 -- D3 in T2 empty itself yes
8249 -- X1 in T3 X1 in T2 D3 in T3 no
8250 -- X2 in T3 X2 in T2 D1 in T3 no
8251 -- D1 in T3 empty itself yes
8252 -- D2 in T3 empty itself yes
8253 -- D3 in T3 empty itself yes
8255 -- Y in T4 X1 in T3 D3 in T4 no
8256 -- D1 in T4 empty itself yes
8257 -- D2 in T4 empty itself yes
8258 -- D3 in T4 empty itself yes
8260 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8262 -- Type derivation for tagged types is fairly straightforward. If no
8263 -- discriminants are specified by the derived type, these are inherited
8264 -- from the parent. No explicit stored discriminants are ever necessary.
8265 -- The only manipulation that is done to the tree is that of adding a
8266 -- _parent field with parent type and constrained to the same constraint
8267 -- specified for the parent in the derived type definition. For instance:
8269 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8270 -- type T1 is new R with null record;
8271 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8273 -- are changed into:
8275 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8276 -- _parent : R (D1, D2, D3);
8279 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8280 -- _parent : T1 (X2, 88, X1);
8283 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8284 -- ORC and ICH fields are:
8286 -- Discrim CD ORC ICH
8287 -- ^^^^^^^ ^^ ^^^ ^^^
8288 -- D1 in R empty itself no
8289 -- D2 in R empty itself no
8290 -- D3 in R empty itself no
8292 -- D1 in T1 D1 in R D1 in R no
8293 -- D2 in T1 D2 in R D2 in R no
8294 -- D3 in T1 D3 in R D3 in R no
8296 -- X1 in T2 D3 in T1 D3 in R no
8297 -- X2 in T2 D1 in T1 D1 in R no
8299 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8301 -- Regardless of whether we dealing with a tagged or untagged type
8302 -- we will transform all derived type declarations of the form
8304 -- type T is new R (...) [with ...];
8306 -- subtype S is R (...);
8307 -- type T is new S [with ...];
8309 -- type BT is new R [with ...];
8310 -- subtype T is BT (...);
8312 -- That is, the base derived type is constrained only if it has no
8313 -- discriminants. The reason for doing this is that GNAT's semantic model
8314 -- assumes that a base type with discriminants is unconstrained.
8316 -- Note that, strictly speaking, the above transformation is not always
8317 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8319 -- procedure B34011A is
8320 -- type REC (D : integer := 0) is record
8325 -- type T6 is new Rec;
8326 -- function F return T6;
8331 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8334 -- The definition of Q6.U is illegal. However transforming Q6.U into
8336 -- type BaseU is new T6;
8337 -- subtype U is BaseU (Q6.F.I)
8339 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8340 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8341 -- the transformation described above.
8343 -- There is another instance where the above transformation is incorrect.
8347 -- type Base (D : Integer) is tagged null record;
8348 -- procedure P (X : Base);
8350 -- type Der is new Base (2) with null record;
8351 -- procedure P (X : Der);
8354 -- Then the above transformation turns this into
8356 -- type Der_Base is new Base with null record;
8357 -- -- procedure P (X : Base) is implicitly inherited here
8358 -- -- as procedure P (X : Der_Base).
8360 -- subtype Der is Der_Base (2);
8361 -- procedure P (X : Der);
8362 -- -- The overriding of P (X : Der_Base) is illegal since we
8363 -- -- have a parameter conformance problem.
8365 -- To get around this problem, after having semantically processed Der_Base
8366 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8367 -- Discriminant_Constraint from Der so that when parameter conformance is
8368 -- checked when P is overridden, no semantic errors are flagged.
8370 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8372 -- Regardless of whether we are dealing with a tagged or untagged type
8373 -- we will transform all derived type declarations of the form
8375 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8376 -- type T is new R [with ...];
8378 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8380 -- The reason for such transformation is that it allows us to implement a
8381 -- very clean form of component inheritance as explained below.
8383 -- Note that this transformation is not achieved by direct tree rewriting
8384 -- and manipulation, but rather by redoing the semantic actions that the
8385 -- above transformation will entail. This is done directly in routine
8386 -- Inherit_Components.
8388 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8390 -- In both tagged and untagged derived types, regular non discriminant
8391 -- components are inherited in the derived type from the parent type. In
8392 -- the absence of discriminants component, inheritance is straightforward
8393 -- as components can simply be copied from the parent.
8395 -- If the parent has discriminants, inheriting components constrained with
8396 -- these discriminants requires caution. Consider the following example:
8398 -- type R (D1, D2 : Positive) is [tagged] record
8399 -- S : String (D1 .. D2);
8402 -- type T1 is new R [with null record];
8403 -- type T2 (X : positive) is new R (1, X) [with null record];
8405 -- As explained in 6. above, T1 is rewritten as
8406 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8407 -- which makes the treatment for T1 and T2 identical.
8409 -- What we want when inheriting S, is that references to D1 and D2 in R are
8410 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8411 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8412 -- with either discriminant references in the derived type or expressions.
8413 -- This replacement is achieved as follows: before inheriting R's
8414 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8415 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8416 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8417 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8418 -- by String (1 .. X).
8420 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8422 -- We explain here the rules governing private type extensions relevant to
8423 -- type derivation. These rules are explained on the following example:
8425 -- type D [(...)] is new A [(...)] with private; <-- partial view
8426 -- type D [(...)] is new P [(...)] with null record; <-- full view
8428 -- Type A is called the ancestor subtype of the private extension.
8429 -- Type P is the parent type of the full view of the private extension. It
8430 -- must be A or a type derived from A.
8432 -- The rules concerning the discriminants of private type extensions are
8435 -- o If a private extension inherits known discriminants from the ancestor
8436 -- subtype, then the full view must also inherit its discriminants from
8437 -- the ancestor subtype and the parent subtype of the full view must be
8438 -- constrained if and only if the ancestor subtype is constrained.
8440 -- o If a partial view has unknown discriminants, then the full view may
8441 -- define a definite or an indefinite subtype, with or without
8444 -- o If a partial view has neither known nor unknown discriminants, then
8445 -- the full view must define a definite subtype.
8447 -- o If the ancestor subtype of a private extension has constrained
8448 -- discriminants, then the parent subtype of the full view must impose a
8449 -- statically matching constraint on those discriminants.
8451 -- This means that only the following forms of private extensions are
8454 -- type D is new A with private; <-- partial view
8455 -- type D is new P with null record; <-- full view
8457 -- If A has no discriminants than P has no discriminants, otherwise P must
8458 -- inherit A's discriminants.
8460 -- type D is new A (...) with private; <-- partial view
8461 -- type D is new P (:::) with null record; <-- full view
8463 -- P must inherit A's discriminants and (...) and (:::) must statically
8466 -- subtype A is R (...);
8467 -- type D is new A with private; <-- partial view
8468 -- type D is new P with null record; <-- full view
8470 -- P must have inherited R's discriminants and must be derived from A or
8471 -- any of its subtypes.
8473 -- type D (..) is new A with private; <-- partial view
8474 -- type D (..) is new P [(:::)] with null record; <-- full view
8476 -- No specific constraints on P's discriminants or constraint (:::).
8477 -- Note that A can be unconstrained, but the parent subtype P must either
8478 -- be constrained or (:::) must be present.
8480 -- type D (..) is new A [(...)] with private; <-- partial view
8481 -- type D (..) is new P [(:::)] with null record; <-- full view
8483 -- P's constraints on A's discriminants must statically match those
8484 -- imposed by (...).
8486 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8488 -- The full view of a private extension is handled exactly as described
8489 -- above. The model chose for the private view of a private extension is
8490 -- the same for what concerns discriminants (i.e. they receive the same
8491 -- treatment as in the tagged case). However, the private view of the
8492 -- private extension always inherits the components of the parent base,
8493 -- without replacing any discriminant reference. Strictly speaking this is
8494 -- incorrect. However, Gigi never uses this view to generate code so this
8495 -- is a purely semantic issue. In theory, a set of transformations similar
8496 -- to those given in 5. and 6. above could be applied to private views of
8497 -- private extensions to have the same model of component inheritance as
8498 -- for non private extensions. However, this is not done because it would
8499 -- further complicate private type processing. Semantically speaking, this
8500 -- leaves us in an uncomfortable situation. As an example consider:
8503 -- type R (D : integer) is tagged record
8504 -- S : String (1 .. D);
8506 -- procedure P (X : R);
8507 -- type T is new R (1) with private;
8509 -- type T is new R (1) with null record;
8512 -- This is transformed into:
8515 -- type R (D : integer) is tagged record
8516 -- S : String (1 .. D);
8518 -- procedure P (X : R);
8519 -- type T is new R (1) with private;
8521 -- type BaseT is new R with null record;
8522 -- subtype T is BaseT (1);
8525 -- (strictly speaking the above is incorrect Ada)
8527 -- From the semantic standpoint the private view of private extension T
8528 -- should be flagged as constrained since one can clearly have
8532 -- in a unit withing Pack. However, when deriving subprograms for the
8533 -- private view of private extension T, T must be seen as unconstrained
8534 -- since T has discriminants (this is a constraint of the current
8535 -- subprogram derivation model). Thus, when processing the private view of
8536 -- a private extension such as T, we first mark T as unconstrained, we
8537 -- process it, we perform program derivation and just before returning from
8538 -- Build_Derived_Record_Type we mark T as constrained.
8540 -- ??? Are there are other uncomfortable cases that we will have to
8543 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8545 -- Types that are derived from a visible record type and have a private
8546 -- extension present other peculiarities. They behave mostly like private
8547 -- types, but if they have primitive operations defined, these will not
8548 -- have the proper signatures for further inheritance, because other
8549 -- primitive operations will use the implicit base that we define for
8550 -- private derivations below. This affect subprogram inheritance (see
8551 -- Derive_Subprograms for details). We also derive the implicit base from
8552 -- the base type of the full view, so that the implicit base is a record
8553 -- type and not another private type, This avoids infinite loops.
8555 procedure Build_Derived_Record_Type
8557 Parent_Type : Entity_Id;
8558 Derived_Type : Entity_Id;
8559 Derive_Subps : Boolean := True)
8561 Discriminant_Specs : constant Boolean :=
8562 Present (Discriminant_Specifications (N));
8563 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8564 Loc : constant Source_Ptr := Sloc (N);
8565 Private_Extension : constant Boolean :=
8566 Nkind (N) = N_Private_Extension_Declaration;
8567 Assoc_List : Elist_Id;
8568 Constraint_Present : Boolean;
8570 Discrim : Entity_Id;
8572 Inherit_Discrims : Boolean := False;
8573 Last_Discrim : Entity_Id;
8574 New_Base : Entity_Id;
8576 New_Discrs : Elist_Id;
8577 New_Indic : Node_Id;
8578 Parent_Base : Entity_Id;
8579 Save_Etype : Entity_Id;
8580 Save_Discr_Constr : Elist_Id;
8581 Save_Next_Entity : Entity_Id;
8584 Discs : Elist_Id := New_Elmt_List;
8585 -- An empty Discs list means that there were no constraints in the
8586 -- subtype indication or that there was an error processing it.
8588 procedure Check_Generic_Ancestors;
8589 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8590 -- cannot be declared at a deeper level than its parent type is
8591 -- removed. The check on derivation within a generic body is also
8592 -- relaxed, but there's a restriction that a derived tagged type
8593 -- cannot be declared in a generic body if it's derived directly
8594 -- or indirectly from a formal type of that generic. This applies
8595 -- to progenitors as well.
8597 -----------------------------
8598 -- Check_Generic_Ancestors --
8599 -----------------------------
8601 procedure Check_Generic_Ancestors is
8602 Ancestor_Type : Entity_Id;
8603 Intf_List : List_Id;
8604 Intf_Name : Node_Id;
8606 procedure Check_Ancestor;
8607 -- For parent and progenitors.
8609 --------------------
8610 -- Check_Ancestor --
8611 --------------------
8613 procedure Check_Ancestor is
8615 -- If the derived type does have a formal type as an ancestor
8616 -- then it's an error if the derived type is declared within
8617 -- the body of the generic unit that declares the formal type
8618 -- in its generic formal part. It's sufficient to check whether
8619 -- the ancestor type is declared inside the same generic body
8620 -- as the derived type (such as within a nested generic spec),
8621 -- in which case the derivation is legal. If the formal type is
8622 -- declared outside of that generic body, then it's certain
8623 -- that the derived type is declared within the generic body
8624 -- of the generic unit declaring the formal type.
8626 if Is_Generic_Type (Ancestor_Type)
8627 and then Enclosing_Generic_Body (Ancestor_Type) /=
8628 Enclosing_Generic_Body (Derived_Type)
8631 ("ancestor type& is formal type of enclosing"
8632 & " generic unit (RM 3.9.1 (4/2))",
8633 Indic, Ancestor_Type);
8638 if Nkind (N) = N_Private_Extension_Declaration then
8639 Intf_List := Interface_List (N);
8641 Intf_List := Interface_List (Type_Definition (N));
8644 if Present (Enclosing_Generic_Body (Derived_Type)) then
8645 Ancestor_Type := Parent_Type;
8647 while not Is_Generic_Type (Ancestor_Type)
8648 and then Etype (Ancestor_Type) /= Ancestor_Type
8650 Ancestor_Type := Etype (Ancestor_Type);
8655 if Present (Intf_List) then
8656 Intf_Name := First (Intf_List);
8657 while Present (Intf_Name) loop
8658 Ancestor_Type := Entity (Intf_Name);
8664 end Check_Generic_Ancestors;
8666 -- Start of processing for Build_Derived_Record_Type
8669 if Ekind (Parent_Type) = E_Record_Type_With_Private
8670 and then Present (Full_View (Parent_Type))
8671 and then Has_Discriminants (Parent_Type)
8673 Parent_Base := Base_Type (Full_View (Parent_Type));
8675 Parent_Base := Base_Type (Parent_Type);
8678 -- If the parent type is declared as a subtype of another private
8679 -- type with inherited discriminants, its generated base type is
8680 -- itself a record subtype. To further inherit the constraint we
8681 -- need to use its own base to have an unconstrained type on which
8682 -- to apply the inherited constraint.
8684 if Ekind (Parent_Base) = E_Record_Subtype then
8685 Parent_Base := Base_Type (Parent_Base);
8688 -- AI05-0115: if this is a derivation from a private type in some
8689 -- other scope that may lead to invisible components for the derived
8690 -- type, mark it accordingly.
8692 if Is_Private_Type (Parent_Type) then
8693 if Scope (Parent_Base) = Scope (Derived_Type) then
8696 elsif In_Open_Scopes (Scope (Parent_Base))
8697 and then In_Private_Part (Scope (Parent_Base))
8702 Set_Has_Private_Ancestor (Derived_Type);
8706 Set_Has_Private_Ancestor
8707 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8710 -- Before we start the previously documented transformations, here is
8711 -- little fix for size and alignment of tagged types. Normally when we
8712 -- derive type D from type P, we copy the size and alignment of P as the
8713 -- default for D, and in the absence of explicit representation clauses
8714 -- for D, the size and alignment are indeed the same as the parent.
8716 -- But this is wrong for tagged types, since fields may be added, and
8717 -- the default size may need to be larger, and the default alignment may
8718 -- need to be larger.
8720 -- We therefore reset the size and alignment fields in the tagged case.
8721 -- Note that the size and alignment will in any case be at least as
8722 -- large as the parent type (since the derived type has a copy of the
8723 -- parent type in the _parent field)
8725 -- The type is also marked as being tagged here, which is needed when
8726 -- processing components with a self-referential anonymous access type
8727 -- in the call to Check_Anonymous_Access_Components below. Note that
8728 -- this flag is also set later on for completeness.
8731 Set_Is_Tagged_Type (Derived_Type);
8732 Init_Size_Align (Derived_Type);
8735 -- STEP 0a: figure out what kind of derived type declaration we have
8737 if Private_Extension then
8739 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8740 Set_Default_SSO (Derived_Type);
8741 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8744 Type_Def := Type_Definition (N);
8746 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8747 -- Parent_Base can be a private type or private extension. However,
8748 -- for tagged types with an extension the newly added fields are
8749 -- visible and hence the Derived_Type is always an E_Record_Type.
8750 -- (except that the parent may have its own private fields).
8751 -- For untagged types we preserve the Ekind of the Parent_Base.
8753 if Present (Record_Extension_Part (Type_Def)) then
8754 Set_Ekind (Derived_Type, E_Record_Type);
8755 Set_Default_SSO (Derived_Type);
8756 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8758 -- Create internal access types for components with anonymous
8761 if Ada_Version >= Ada_2005 then
8762 Check_Anonymous_Access_Components
8763 (N, Derived_Type, Derived_Type,
8764 Component_List (Record_Extension_Part (Type_Def)));
8768 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8772 -- Indic can either be an N_Identifier if the subtype indication
8773 -- contains no constraint or an N_Subtype_Indication if the subtype
8774 -- indication has a constraint. In either case it can include an
8777 Indic := Subtype_Indication (Type_Def);
8778 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8780 -- Check that the type has visible discriminants. The type may be
8781 -- a private type with unknown discriminants whose full view has
8782 -- discriminants which are invisible.
8784 if Constraint_Present then
8785 if not Has_Discriminants (Parent_Base)
8787 (Has_Unknown_Discriminants (Parent_Base)
8788 and then Is_Private_Type (Parent_Base))
8791 ("invalid constraint: type has no discriminant",
8792 Constraint (Indic));
8794 Constraint_Present := False;
8795 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8797 elsif Is_Constrained (Parent_Type) then
8799 ("invalid constraint: parent type is already constrained",
8800 Constraint (Indic));
8802 Constraint_Present := False;
8803 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8807 -- STEP 0b: If needed, apply transformation given in point 5. above
8809 if not Private_Extension
8810 and then Has_Discriminants (Parent_Type)
8811 and then not Discriminant_Specs
8812 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8814 -- First, we must analyze the constraint (see comment in point 5.)
8815 -- The constraint may come from the subtype indication of the full
8818 if Constraint_Present then
8819 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8821 -- If there is no explicit constraint, there might be one that is
8822 -- inherited from a constrained parent type. In that case verify that
8823 -- it conforms to the constraint in the partial view. In perverse
8824 -- cases the parent subtypes of the partial and full view can have
8825 -- different constraints.
8827 elsif Present (Stored_Constraint (Parent_Type)) then
8828 New_Discrs := Stored_Constraint (Parent_Type);
8831 New_Discrs := No_Elist;
8834 if Has_Discriminants (Derived_Type)
8835 and then Has_Private_Declaration (Derived_Type)
8836 and then Present (Discriminant_Constraint (Derived_Type))
8837 and then Present (New_Discrs)
8839 -- Verify that constraints of the full view statically match
8840 -- those given in the partial view.
8846 C1 := First_Elmt (New_Discrs);
8847 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8848 while Present (C1) and then Present (C2) loop
8849 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8851 (Is_OK_Static_Expression (Node (C1))
8852 and then Is_OK_Static_Expression (Node (C2))
8854 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8859 if Constraint_Present then
8861 ("constraint not conformant to previous declaration",
8865 ("constraint of full view is incompatible "
8866 & "with partial view", N);
8876 -- Insert and analyze the declaration for the unconstrained base type
8878 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8881 Make_Full_Type_Declaration (Loc,
8882 Defining_Identifier => New_Base,
8884 Make_Derived_Type_Definition (Loc,
8885 Abstract_Present => Abstract_Present (Type_Def),
8886 Limited_Present => Limited_Present (Type_Def),
8887 Subtype_Indication =>
8888 New_Occurrence_Of (Parent_Base, Loc),
8889 Record_Extension_Part =>
8890 Relocate_Node (Record_Extension_Part (Type_Def)),
8891 Interface_List => Interface_List (Type_Def)));
8893 Set_Parent (New_Decl, Parent (N));
8894 Mark_Rewrite_Insertion (New_Decl);
8895 Insert_Before (N, New_Decl);
8897 -- In the extension case, make sure ancestor is frozen appropriately
8898 -- (see also non-discriminated case below).
8900 if Present (Record_Extension_Part (Type_Def))
8901 or else Is_Interface (Parent_Base)
8903 Freeze_Before (New_Decl, Parent_Type);
8906 -- Note that this call passes False for the Derive_Subps parameter
8907 -- because subprogram derivation is deferred until after creating
8908 -- the subtype (see below).
8911 (New_Decl, Parent_Base, New_Base,
8912 Is_Completion => False, Derive_Subps => False);
8914 -- ??? This needs re-examination to determine whether the
8915 -- above call can simply be replaced by a call to Analyze.
8917 Set_Analyzed (New_Decl);
8919 -- Insert and analyze the declaration for the constrained subtype
8921 if Constraint_Present then
8923 Make_Subtype_Indication (Loc,
8924 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8925 Constraint => Relocate_Node (Constraint (Indic)));
8929 Constr_List : constant List_Id := New_List;
8934 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8935 while Present (C) loop
8938 -- It is safe here to call New_Copy_Tree since we called
8939 -- Force_Evaluation on each constraint previously
8940 -- in Build_Discriminant_Constraints.
8942 Append (New_Copy_Tree (Expr), To => Constr_List);
8948 Make_Subtype_Indication (Loc,
8949 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8951 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8956 Make_Subtype_Declaration (Loc,
8957 Defining_Identifier => Derived_Type,
8958 Subtype_Indication => New_Indic));
8962 -- Derivation of subprograms must be delayed until the full subtype
8963 -- has been established, to ensure proper overriding of subprograms
8964 -- inherited by full types. If the derivations occurred as part of
8965 -- the call to Build_Derived_Type above, then the check for type
8966 -- conformance would fail because earlier primitive subprograms
8967 -- could still refer to the full type prior the change to the new
8968 -- subtype and hence would not match the new base type created here.
8969 -- Subprograms are not derived, however, when Derive_Subps is False
8970 -- (since otherwise there could be redundant derivations).
8972 if Derive_Subps then
8973 Derive_Subprograms (Parent_Type, Derived_Type);
8976 -- For tagged types the Discriminant_Constraint of the new base itype
8977 -- is inherited from the first subtype so that no subtype conformance
8978 -- problem arise when the first subtype overrides primitive
8979 -- operations inherited by the implicit base type.
8982 Set_Discriminant_Constraint
8983 (New_Base, Discriminant_Constraint (Derived_Type));
8989 -- If we get here Derived_Type will have no discriminants or it will be
8990 -- a discriminated unconstrained base type.
8992 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8996 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8997 -- The declaration of a specific descendant of an interface type
8998 -- freezes the interface type (RM 13.14).
9000 if not Private_Extension or else Is_Interface (Parent_Base) then
9001 Freeze_Before (N, Parent_Type);
9004 if Ada_Version >= Ada_2005 then
9005 Check_Generic_Ancestors;
9007 elsif Type_Access_Level (Derived_Type) /=
9008 Type_Access_Level (Parent_Type)
9009 and then not Is_Generic_Type (Derived_Type)
9011 if Is_Controlled (Parent_Type) then
9013 ("controlled type must be declared at the library level",
9017 ("type extension at deeper accessibility level than parent",
9023 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
9026 and then GB /= Enclosing_Generic_Body (Parent_Base)
9029 ("parent type of& must not be outside generic body"
9031 Indic, Derived_Type);
9037 -- Ada 2005 (AI-251)
9039 if Ada_Version >= Ada_2005 and then Is_Tagged then
9041 -- "The declaration of a specific descendant of an interface type
9042 -- freezes the interface type" (RM 13.14).
9047 if Is_Non_Empty_List (Interface_List (Type_Def)) then
9048 Iface := First (Interface_List (Type_Def));
9049 while Present (Iface) loop
9050 Freeze_Before (N, Etype (Iface));
9057 -- STEP 1b : preliminary cleanup of the full view of private types
9059 -- If the type is already marked as having discriminants, then it's the
9060 -- completion of a private type or private extension and we need to
9061 -- retain the discriminants from the partial view if the current
9062 -- declaration has Discriminant_Specifications so that we can verify
9063 -- conformance. However, we must remove any existing components that
9064 -- were inherited from the parent (and attached in Copy_And_Swap)
9065 -- because the full type inherits all appropriate components anyway, and
9066 -- we do not want the partial view's components interfering.
9068 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9069 Discrim := First_Discriminant (Derived_Type);
9071 Last_Discrim := Discrim;
9072 Next_Discriminant (Discrim);
9073 exit when No (Discrim);
9076 Set_Last_Entity (Derived_Type, Last_Discrim);
9078 -- In all other cases wipe out the list of inherited components (even
9079 -- inherited discriminants), it will be properly rebuilt here.
9082 Set_First_Entity (Derived_Type, Empty);
9083 Set_Last_Entity (Derived_Type, Empty);
9086 -- STEP 1c: Initialize some flags for the Derived_Type
9088 -- The following flags must be initialized here so that
9089 -- Process_Discriminants can check that discriminants of tagged types do
9090 -- not have a default initial value and that access discriminants are
9091 -- only specified for limited records. For completeness, these flags are
9092 -- also initialized along with all the other flags below.
9094 -- AI-419: Limitedness is not inherited from an interface parent, so to
9095 -- be limited in that case the type must be explicitly declared as
9096 -- limited. However, task and protected interfaces are always limited.
9098 if Limited_Present (Type_Def) then
9099 Set_Is_Limited_Record (Derived_Type);
9101 elsif Is_Limited_Record (Parent_Type)
9102 or else (Present (Full_View (Parent_Type))
9103 and then Is_Limited_Record (Full_View (Parent_Type)))
9105 if not Is_Interface (Parent_Type)
9106 or else Is_Synchronized_Interface (Parent_Type)
9107 or else Is_Protected_Interface (Parent_Type)
9108 or else Is_Task_Interface (Parent_Type)
9110 Set_Is_Limited_Record (Derived_Type);
9114 -- STEP 2a: process discriminants of derived type if any
9116 Push_Scope (Derived_Type);
9118 if Discriminant_Specs then
9119 Set_Has_Unknown_Discriminants (Derived_Type, False);
9121 -- The following call initializes fields Has_Discriminants and
9122 -- Discriminant_Constraint, unless we are processing the completion
9123 -- of a private type declaration.
9125 Check_Or_Process_Discriminants (N, Derived_Type);
9127 -- For untagged types, the constraint on the Parent_Type must be
9128 -- present and is used to rename the discriminants.
9130 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9131 Error_Msg_N ("untagged parent must have discriminants", Indic);
9133 elsif not Is_Tagged and then not Constraint_Present then
9135 ("discriminant constraint needed for derived untagged records",
9138 -- Otherwise the parent subtype must be constrained unless we have a
9139 -- private extension.
9141 elsif not Constraint_Present
9142 and then not Private_Extension
9143 and then not Is_Constrained (Parent_Type)
9146 ("unconstrained type not allowed in this context", Indic);
9148 elsif Constraint_Present then
9149 -- The following call sets the field Corresponding_Discriminant
9150 -- for the discriminants in the Derived_Type.
9152 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9154 -- For untagged types all new discriminants must rename
9155 -- discriminants in the parent. For private extensions new
9156 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9158 Discrim := First_Discriminant (Derived_Type);
9159 while Present (Discrim) loop
9161 and then No (Corresponding_Discriminant (Discrim))
9164 ("new discriminants must constrain old ones", Discrim);
9166 elsif Private_Extension
9167 and then Present (Corresponding_Discriminant (Discrim))
9170 ("only static constraints allowed for parent"
9171 & " discriminants in the partial view", Indic);
9175 -- If a new discriminant is used in the constraint, then its
9176 -- subtype must be statically compatible with the parent
9177 -- discriminant's subtype (3.7(15)).
9179 -- However, if the record contains an array constrained by
9180 -- the discriminant but with some different bound, the compiler
9181 -- tries to create a smaller range for the discriminant type.
9182 -- (See exp_ch3.Adjust_Discriminants). In this case, where
9183 -- the discriminant type is a scalar type, the check must use
9184 -- the original discriminant type in the parent declaration.
9187 Corr_Disc : constant Entity_Id :=
9188 Corresponding_Discriminant (Discrim);
9189 Disc_Type : constant Entity_Id := Etype (Discrim);
9190 Corr_Type : Entity_Id;
9193 if Present (Corr_Disc) then
9194 if Is_Scalar_Type (Disc_Type) then
9196 Entity (Discriminant_Type (Parent (Corr_Disc)));
9198 Corr_Type := Etype (Corr_Disc);
9202 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
9205 ("subtype must be compatible "
9206 & "with parent discriminant",
9212 Next_Discriminant (Discrim);
9215 -- Check whether the constraints of the full view statically
9216 -- match those imposed by the parent subtype [7.3(13)].
9218 if Present (Stored_Constraint (Derived_Type)) then
9223 C1 := First_Elmt (Discs);
9224 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9225 while Present (C1) and then Present (C2) loop
9227 Fully_Conformant_Expressions (Node (C1), Node (C2))
9230 ("not conformant with previous declaration",
9241 -- STEP 2b: No new discriminants, inherit discriminants if any
9244 if Private_Extension then
9245 Set_Has_Unknown_Discriminants
9247 Has_Unknown_Discriminants (Parent_Type)
9248 or else Unknown_Discriminants_Present (N));
9250 -- The partial view of the parent may have unknown discriminants,
9251 -- but if the full view has discriminants and the parent type is
9252 -- in scope they must be inherited.
9254 elsif Has_Unknown_Discriminants (Parent_Type)
9256 (not Has_Discriminants (Parent_Type)
9257 or else not In_Open_Scopes (Scope (Parent_Base)))
9259 Set_Has_Unknown_Discriminants (Derived_Type);
9262 if not Has_Unknown_Discriminants (Derived_Type)
9263 and then not Has_Unknown_Discriminants (Parent_Base)
9264 and then Has_Discriminants (Parent_Type)
9266 Inherit_Discrims := True;
9267 Set_Has_Discriminants
9268 (Derived_Type, True);
9269 Set_Discriminant_Constraint
9270 (Derived_Type, Discriminant_Constraint (Parent_Base));
9273 -- The following test is true for private types (remember
9274 -- transformation 5. is not applied to those) and in an error
9277 if Constraint_Present then
9278 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9281 -- For now mark a new derived type as constrained only if it has no
9282 -- discriminants. At the end of Build_Derived_Record_Type we properly
9283 -- set this flag in the case of private extensions. See comments in
9284 -- point 9. just before body of Build_Derived_Record_Type.
9288 not (Inherit_Discrims
9289 or else Has_Unknown_Discriminants (Derived_Type)));
9292 -- STEP 3: initialize fields of derived type
9294 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9295 Set_Stored_Constraint (Derived_Type, No_Elist);
9297 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9298 -- but cannot be interfaces
9300 if not Private_Extension
9301 and then Ekind (Derived_Type) /= E_Private_Type
9302 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9304 if Interface_Present (Type_Def) then
9305 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9308 Set_Interfaces (Derived_Type, No_Elist);
9311 -- Fields inherited from the Parent_Type
9313 Set_Has_Specified_Layout
9314 (Derived_Type, Has_Specified_Layout (Parent_Type));
9315 Set_Is_Limited_Composite
9316 (Derived_Type, Is_Limited_Composite (Parent_Type));
9317 Set_Is_Private_Composite
9318 (Derived_Type, Is_Private_Composite (Parent_Type));
9320 if Is_Tagged_Type (Parent_Type) then
9321 Set_No_Tagged_Streams_Pragma
9322 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9325 -- Fields inherited from the Parent_Base
9327 Set_Has_Controlled_Component
9328 (Derived_Type, Has_Controlled_Component (Parent_Base));
9329 Set_Has_Non_Standard_Rep
9330 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9331 Set_Has_Primitive_Operations
9332 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9334 -- Set fields for private derived types
9336 if Is_Private_Type (Derived_Type) then
9337 Set_Depends_On_Private (Derived_Type, True);
9338 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9341 -- Inherit fields for non-private types. If this is the completion of a
9342 -- derivation from a private type, the parent itself is private and the
9343 -- attributes come from its full view, which must be present.
9345 if Is_Record_Type (Derived_Type) then
9347 Parent_Full : Entity_Id;
9350 if Is_Private_Type (Parent_Base)
9351 and then not Is_Record_Type (Parent_Base)
9353 Parent_Full := Full_View (Parent_Base);
9355 Parent_Full := Parent_Base;
9358 Set_Component_Alignment
9359 (Derived_Type, Component_Alignment (Parent_Full));
9361 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9362 Set_Has_Complex_Representation
9363 (Derived_Type, Has_Complex_Representation (Parent_Full));
9365 -- For untagged types, inherit the layout by default to avoid
9366 -- costly changes of representation for type conversions.
9368 if not Is_Tagged then
9369 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9370 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9375 -- Set fields for tagged types
9378 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9380 -- All tagged types defined in Ada.Finalization are controlled
9382 if Chars (Scope (Derived_Type)) = Name_Finalization
9383 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9384 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9386 Set_Is_Controlled_Active (Derived_Type);
9388 Set_Is_Controlled_Active
9389 (Derived_Type, Is_Controlled_Active (Parent_Base));
9392 -- Minor optimization: there is no need to generate the class-wide
9393 -- entity associated with an underlying record view.
9395 if not Is_Underlying_Record_View (Derived_Type) then
9396 Make_Class_Wide_Type (Derived_Type);
9399 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9401 if Has_Discriminants (Derived_Type)
9402 and then Constraint_Present
9404 Set_Stored_Constraint
9405 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9408 if Ada_Version >= Ada_2005 then
9410 Ifaces_List : Elist_Id;
9413 -- Checks rules 3.9.4 (13/2 and 14/2)
9415 if Comes_From_Source (Derived_Type)
9416 and then not Is_Private_Type (Derived_Type)
9417 and then Is_Interface (Parent_Type)
9418 and then not Is_Interface (Derived_Type)
9420 if Is_Task_Interface (Parent_Type) then
9422 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9425 elsif Is_Protected_Interface (Parent_Type) then
9427 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9432 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9434 Check_Interfaces (N, Type_Def);
9436 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9437 -- not already in the parents.
9441 Ifaces_List => Ifaces_List,
9442 Exclude_Parents => True);
9444 Set_Interfaces (Derived_Type, Ifaces_List);
9446 -- If the derived type is the anonymous type created for
9447 -- a declaration whose parent has a constraint, propagate
9448 -- the interface list to the source type. This must be done
9449 -- prior to the completion of the analysis of the source type
9450 -- because the components in the extension may contain current
9451 -- instances whose legality depends on some ancestor.
9453 if Is_Itype (Derived_Type) then
9455 Def : constant Node_Id :=
9456 Associated_Node_For_Itype (Derived_Type);
9459 and then Nkind (Def) = N_Full_Type_Declaration
9462 (Defining_Identifier (Def), Ifaces_List);
9467 -- A type extension is automatically Ghost when one of its
9468 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9469 -- also inherited when the parent type is Ghost, but this is
9470 -- done in Build_Derived_Type as the mechanism also handles
9471 -- untagged derivations.
9473 if Implements_Ghost_Interface (Derived_Type) then
9474 Set_Is_Ghost_Entity (Derived_Type);
9480 -- STEP 4: Inherit components from the parent base and constrain them.
9481 -- Apply the second transformation described in point 6. above.
9483 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9484 or else not Has_Discriminants (Parent_Type)
9485 or else not Is_Constrained (Parent_Type)
9489 Constrs := Discriminant_Constraint (Parent_Type);
9494 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9496 -- STEP 5a: Copy the parent record declaration for untagged types
9498 Set_Has_Implicit_Dereference
9499 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9501 if not Is_Tagged then
9503 -- Discriminant_Constraint (Derived_Type) has been properly
9504 -- constructed. Save it and temporarily set it to Empty because we
9505 -- do not want the call to New_Copy_Tree below to mess this list.
9507 if Has_Discriminants (Derived_Type) then
9508 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9509 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9511 Save_Discr_Constr := No_Elist;
9514 -- Save the Etype field of Derived_Type. It is correctly set now,
9515 -- but the call to New_Copy tree may remap it to point to itself,
9516 -- which is not what we want. Ditto for the Next_Entity field.
9518 Save_Etype := Etype (Derived_Type);
9519 Save_Next_Entity := Next_Entity (Derived_Type);
9521 -- Assoc_List maps all stored discriminants in the Parent_Base to
9522 -- stored discriminants in the Derived_Type. It is fundamental that
9523 -- no types or itypes with discriminants other than the stored
9524 -- discriminants appear in the entities declared inside
9525 -- Derived_Type, since the back end cannot deal with it.
9529 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9530 Copy_Dimensions_Of_Components (Derived_Type);
9532 -- Restore the fields saved prior to the New_Copy_Tree call
9533 -- and compute the stored constraint.
9535 Set_Etype (Derived_Type, Save_Etype);
9536 Link_Entities (Derived_Type, Save_Next_Entity);
9538 if Has_Discriminants (Derived_Type) then
9539 Set_Discriminant_Constraint
9540 (Derived_Type, Save_Discr_Constr);
9541 Set_Stored_Constraint
9542 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9544 Replace_Discriminants (Derived_Type, New_Decl);
9547 -- Insert the new derived type declaration
9549 Rewrite (N, New_Decl);
9551 -- STEP 5b: Complete the processing for record extensions in generics
9553 -- There is no completion for record extensions declared in the
9554 -- parameter part of a generic, so we need to complete processing for
9555 -- these generic record extensions here. The Record_Type_Definition call
9556 -- will change the Ekind of the components from E_Void to E_Component.
9558 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9559 Record_Type_Definition (Empty, Derived_Type);
9561 -- STEP 5c: Process the record extension for non private tagged types
9563 elsif not Private_Extension then
9564 Expand_Record_Extension (Derived_Type, Type_Def);
9566 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9567 -- implemented interfaces if we are in expansion mode
9570 and then Has_Interfaces (Derived_Type)
9572 Add_Interface_Tag_Components (N, Derived_Type);
9575 -- Analyze the record extension
9577 Record_Type_Definition
9578 (Record_Extension_Part (Type_Def), Derived_Type);
9583 -- Nothing else to do if there is an error in the derivation.
9584 -- An unusual case: the full view may be derived from a type in an
9585 -- instance, when the partial view was used illegally as an actual
9586 -- in that instance, leading to a circular definition.
9588 if Etype (Derived_Type) = Any_Type
9589 or else Etype (Parent_Type) = Derived_Type
9594 -- Set delayed freeze and then derive subprograms, we need to do
9595 -- this in this order so that derived subprograms inherit the
9596 -- derived freeze if necessary.
9598 Set_Has_Delayed_Freeze (Derived_Type);
9600 if Derive_Subps then
9601 Derive_Subprograms (Parent_Type, Derived_Type);
9604 -- If we have a private extension which defines a constrained derived
9605 -- type mark as constrained here after we have derived subprograms. See
9606 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9608 if Private_Extension and then Inherit_Discrims then
9609 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9610 Set_Is_Constrained (Derived_Type, True);
9611 Set_Discriminant_Constraint (Derived_Type, Discs);
9613 elsif Is_Constrained (Parent_Type) then
9615 (Derived_Type, True);
9616 Set_Discriminant_Constraint
9617 (Derived_Type, Discriminant_Constraint (Parent_Type));
9621 -- Update the class-wide type, which shares the now-completed entity
9622 -- list with its specific type. In case of underlying record views,
9623 -- we do not generate the corresponding class wide entity.
9626 and then not Is_Underlying_Record_View (Derived_Type)
9629 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9631 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9634 Check_Function_Writable_Actuals (N);
9635 end Build_Derived_Record_Type;
9637 ------------------------
9638 -- Build_Derived_Type --
9639 ------------------------
9641 procedure Build_Derived_Type
9643 Parent_Type : Entity_Id;
9644 Derived_Type : Entity_Id;
9645 Is_Completion : Boolean;
9646 Derive_Subps : Boolean := True)
9648 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9651 -- Set common attributes
9653 Set_Scope (Derived_Type, Current_Scope);
9654 Set_Etype (Derived_Type, Parent_Base);
9655 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9656 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9658 Set_Size_Info (Derived_Type, Parent_Type);
9659 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9661 Set_Is_Controlled_Active
9662 (Derived_Type, Is_Controlled_Active (Parent_Type));
9664 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9665 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9666 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9668 if Is_Tagged_Type (Derived_Type) then
9669 Set_No_Tagged_Streams_Pragma
9670 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9673 -- If the parent has primitive routines and may have not-seen-yet aspect
9674 -- specifications (e.g., a Pack pragma), then set the derived type link
9675 -- in order to later diagnose "early derivation" issues. If in different
9676 -- compilation units, then "early derivation" cannot be an issue (and we
9677 -- don't like interunit references that go in the opposite direction of
9678 -- semantic dependencies).
9680 if Has_Primitive_Operations (Parent_Type)
9681 and then Enclosing_Comp_Unit_Node (Parent_Type) =
9682 Enclosing_Comp_Unit_Node (Derived_Type)
9684 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9687 -- If the parent type is a private subtype, the convention on the base
9688 -- type may be set in the private part, and not propagated to the
9689 -- subtype until later, so we obtain the convention from the base type.
9691 Set_Convention (Derived_Type, Convention (Parent_Base));
9693 -- Set SSO default for record or array type
9695 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9696 and then Is_Base_Type (Derived_Type)
9698 Set_Default_SSO (Derived_Type);
9701 -- A derived type inherits the Default_Initial_Condition pragma coming
9702 -- from any parent type within the derivation chain.
9704 if Has_DIC (Parent_Type) then
9705 Set_Has_Inherited_DIC (Derived_Type);
9708 -- A derived type inherits any class-wide invariants coming from a
9709 -- parent type or an interface. Note that the invariant procedure of
9710 -- the parent type should not be inherited because the derived type may
9711 -- define invariants of its own.
9713 if not Is_Interface (Derived_Type) then
9714 if Has_Inherited_Invariants (Parent_Type)
9715 or else Has_Inheritable_Invariants (Parent_Type)
9717 Set_Has_Inherited_Invariants (Derived_Type);
9719 elsif Is_Concurrent_Type (Derived_Type)
9720 or else Is_Tagged_Type (Derived_Type)
9725 Iface_Elmt : Elmt_Id;
9730 Ifaces_List => Ifaces,
9731 Exclude_Parents => True);
9733 if Present (Ifaces) then
9734 Iface_Elmt := First_Elmt (Ifaces);
9735 while Present (Iface_Elmt) loop
9736 Iface := Node (Iface_Elmt);
9738 if Has_Inheritable_Invariants (Iface) then
9739 Set_Has_Inherited_Invariants (Derived_Type);
9743 Next_Elmt (Iface_Elmt);
9750 -- We similarly inherit predicates. Note that for scalar derived types
9751 -- the predicate is inherited from the first subtype, and not from its
9752 -- (anonymous) base type.
9754 if Has_Predicates (Parent_Type)
9755 or else Has_Predicates (First_Subtype (Parent_Type))
9757 Set_Has_Predicates (Derived_Type);
9760 -- The derived type inherits representation clauses from the parent
9761 -- type, and from any interfaces.
9763 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9766 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
9768 while Present (Iface) loop
9769 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
9774 -- If the parent type has delayed rep aspects, then mark the derived
9775 -- type as possibly inheriting a delayed rep aspect.
9777 if Has_Delayed_Rep_Aspects (Parent_Type) then
9778 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9781 -- A derived type becomes Ghost when its parent type is also Ghost
9782 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9783 -- directly inherited because the Ghost policy in effect may differ.
9785 if Is_Ghost_Entity (Parent_Type) then
9786 Set_Is_Ghost_Entity (Derived_Type);
9789 -- Type dependent processing
9791 case Ekind (Parent_Type) is
9792 when Numeric_Kind =>
9793 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9796 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9798 when Class_Wide_Kind
9802 Build_Derived_Record_Type
9803 (N, Parent_Type, Derived_Type, Derive_Subps);
9806 when Enumeration_Kind =>
9807 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9810 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9812 when Incomplete_Or_Private_Kind =>
9813 Build_Derived_Private_Type
9814 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9816 -- For discriminated types, the derivation includes deriving
9817 -- primitive operations. For others it is done below.
9819 if Is_Tagged_Type (Parent_Type)
9820 or else Has_Discriminants (Parent_Type)
9821 or else (Present (Full_View (Parent_Type))
9822 and then Has_Discriminants (Full_View (Parent_Type)))
9827 when Concurrent_Kind =>
9828 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9831 raise Program_Error;
9834 -- Nothing more to do if some error occurred
9836 if Etype (Derived_Type) = Any_Type then
9840 -- Set delayed freeze and then derive subprograms, we need to do this
9841 -- in this order so that derived subprograms inherit the derived freeze
9844 Set_Has_Delayed_Freeze (Derived_Type);
9846 if Derive_Subps then
9847 Derive_Subprograms (Parent_Type, Derived_Type);
9850 Set_Has_Primitive_Operations
9851 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9852 end Build_Derived_Type;
9854 -----------------------
9855 -- Build_Discriminal --
9856 -----------------------
9858 procedure Build_Discriminal (Discrim : Entity_Id) is
9859 D_Minal : Entity_Id;
9860 CR_Disc : Entity_Id;
9863 -- A discriminal has the same name as the discriminant
9865 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9867 Set_Ekind (D_Minal, E_In_Parameter);
9868 Set_Mechanism (D_Minal, Default_Mechanism);
9869 Set_Etype (D_Minal, Etype (Discrim));
9870 Set_Scope (D_Minal, Current_Scope);
9871 Set_Parent (D_Minal, Parent (Discrim));
9873 Set_Discriminal (Discrim, D_Minal);
9874 Set_Discriminal_Link (D_Minal, Discrim);
9876 -- For task types, build at once the discriminants of the corresponding
9877 -- record, which are needed if discriminants are used in entry defaults
9878 -- and in family bounds.
9880 if Is_Concurrent_Type (Current_Scope)
9882 Is_Limited_Type (Current_Scope)
9884 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9886 Set_Ekind (CR_Disc, E_In_Parameter);
9887 Set_Mechanism (CR_Disc, Default_Mechanism);
9888 Set_Etype (CR_Disc, Etype (Discrim));
9889 Set_Scope (CR_Disc, Current_Scope);
9890 Set_Discriminal_Link (CR_Disc, Discrim);
9891 Set_CR_Discriminant (Discrim, CR_Disc);
9893 end Build_Discriminal;
9895 ------------------------------------
9896 -- Build_Discriminant_Constraints --
9897 ------------------------------------
9899 function Build_Discriminant_Constraints
9902 Derived_Def : Boolean := False) return Elist_Id
9904 C : constant Node_Id := Constraint (Def);
9905 Nb_Discr : constant Nat := Number_Discriminants (T);
9907 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9908 -- Saves the expression corresponding to a given discriminant in T
9910 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9911 -- Return the Position number within array Discr_Expr of a discriminant
9912 -- D within the discriminant list of the discriminated type T.
9914 procedure Process_Discriminant_Expression
9917 -- If this is a discriminant constraint on a partial view, do not
9918 -- generate an overflow check on the discriminant expression. The check
9919 -- will be generated when constraining the full view. Otherwise the
9920 -- backend creates duplicate symbols for the temporaries corresponding
9921 -- to the expressions to be checked, causing spurious assembler errors.
9927 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9931 Disc := First_Discriminant (T);
9932 for J in Discr_Expr'Range loop
9937 Next_Discriminant (Disc);
9940 -- Note: Since this function is called on discriminants that are
9941 -- known to belong to the discriminated type, falling through the
9942 -- loop with no match signals an internal compiler error.
9944 raise Program_Error;
9947 -------------------------------------
9948 -- Process_Discriminant_Expression --
9949 -------------------------------------
9951 procedure Process_Discriminant_Expression
9955 BDT : constant Entity_Id := Base_Type (Etype (D));
9958 -- If this is a discriminant constraint on a partial view, do
9959 -- not generate an overflow on the discriminant expression. The
9960 -- check will be generated when constraining the full view.
9962 if Is_Private_Type (T)
9963 and then Present (Full_View (T))
9965 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9967 Analyze_And_Resolve (Expr, BDT);
9969 end Process_Discriminant_Expression;
9971 -- Declarations local to Build_Discriminant_Constraints
9975 Elist : constant Elist_Id := New_Elmt_List;
9983 Discrim_Present : Boolean := False;
9985 -- Start of processing for Build_Discriminant_Constraints
9988 -- The following loop will process positional associations only.
9989 -- For a positional association, the (single) discriminant is
9990 -- implicitly specified by position, in textual order (RM 3.7.2).
9992 Discr := First_Discriminant (T);
9993 Constr := First (Constraints (C));
9994 for D in Discr_Expr'Range loop
9995 exit when Nkind (Constr) = N_Discriminant_Association;
9998 Error_Msg_N ("too few discriminants given in constraint", C);
9999 return New_Elmt_List;
10001 elsif Nkind (Constr) = N_Range
10002 or else (Nkind (Constr) = N_Attribute_Reference
10003 and then Attribute_Name (Constr) = Name_Range)
10006 ("a range is not a valid discriminant constraint", Constr);
10007 Discr_Expr (D) := Error;
10009 elsif Nkind (Constr) = N_Subtype_Indication then
10011 ("a subtype indication is not a valid discriminant constraint",
10013 Discr_Expr (D) := Error;
10016 Process_Discriminant_Expression (Constr, Discr);
10017 Discr_Expr (D) := Constr;
10020 Next_Discriminant (Discr);
10024 if No (Discr) and then Present (Constr) then
10025 Error_Msg_N ("too many discriminants given in constraint", Constr);
10026 return New_Elmt_List;
10029 -- Named associations can be given in any order, but if both positional
10030 -- and named associations are used in the same discriminant constraint,
10031 -- then positional associations must occur first, at their normal
10032 -- position. Hence once a named association is used, the rest of the
10033 -- discriminant constraint must use only named associations.
10035 while Present (Constr) loop
10037 -- Positional association forbidden after a named association
10039 if Nkind (Constr) /= N_Discriminant_Association then
10040 Error_Msg_N ("positional association follows named one", Constr);
10041 return New_Elmt_List;
10043 -- Otherwise it is a named association
10046 -- E records the type of the discriminants in the named
10047 -- association. All the discriminants specified in the same name
10048 -- association must have the same type.
10052 -- Search the list of discriminants in T to see if the simple name
10053 -- given in the constraint matches any of them.
10055 Id := First (Selector_Names (Constr));
10056 while Present (Id) loop
10059 -- If Original_Discriminant is present, we are processing a
10060 -- generic instantiation and this is an instance node. We need
10061 -- to find the name of the corresponding discriminant in the
10062 -- actual record type T and not the name of the discriminant in
10063 -- the generic formal. Example:
10066 -- type G (D : int) is private;
10068 -- subtype W is G (D => 1);
10070 -- type Rec (X : int) is record ... end record;
10071 -- package Q is new P (G => Rec);
10073 -- At the point of the instantiation, formal type G is Rec
10074 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10075 -- which really looks like "subtype W is Rec (D => 1);" at
10076 -- the point of instantiation, we want to find the discriminant
10077 -- that corresponds to D in Rec, i.e. X.
10079 if Present (Original_Discriminant (Id))
10080 and then In_Instance
10082 Discr := Find_Corresponding_Discriminant (Id, T);
10086 Discr := First_Discriminant (T);
10087 while Present (Discr) loop
10088 if Chars (Discr) = Chars (Id) then
10093 Next_Discriminant (Discr);
10097 Error_Msg_N ("& does not match any discriminant", Id);
10098 return New_Elmt_List;
10100 -- If the parent type is a generic formal, preserve the
10101 -- name of the discriminant for subsequent instances.
10102 -- see comment at the beginning of this if statement.
10104 elsif Is_Generic_Type (Root_Type (T)) then
10105 Set_Original_Discriminant (Id, Discr);
10109 Position := Pos_Of_Discr (T, Discr);
10111 if Present (Discr_Expr (Position)) then
10112 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10115 -- Each discriminant specified in the same named association
10116 -- must be associated with a separate copy of the
10117 -- corresponding expression.
10119 if Present (Next (Id)) then
10120 Expr := New_Copy_Tree (Expression (Constr));
10121 Set_Parent (Expr, Parent (Expression (Constr)));
10123 Expr := Expression (Constr);
10126 Discr_Expr (Position) := Expr;
10127 Process_Discriminant_Expression (Expr, Discr);
10130 -- A discriminant association with more than one discriminant
10131 -- name is only allowed if the named discriminants are all of
10132 -- the same type (RM 3.7.1(8)).
10135 E := Base_Type (Etype (Discr));
10137 elsif Base_Type (Etype (Discr)) /= E then
10139 ("all discriminants in an association " &
10140 "must have the same type", Id);
10150 -- A discriminant constraint must provide exactly one value for each
10151 -- discriminant of the type (RM 3.7.1(8)).
10153 for J in Discr_Expr'Range loop
10154 if No (Discr_Expr (J)) then
10155 Error_Msg_N ("too few discriminants given in constraint", C);
10156 return New_Elmt_List;
10160 -- Determine if there are discriminant expressions in the constraint
10162 for J in Discr_Expr'Range loop
10163 if Denotes_Discriminant
10164 (Discr_Expr (J), Check_Concurrent => True)
10166 Discrim_Present := True;
10170 -- Build an element list consisting of the expressions given in the
10171 -- discriminant constraint and apply the appropriate checks. The list
10172 -- is constructed after resolving any named discriminant associations
10173 -- and therefore the expressions appear in the textual order of the
10176 Discr := First_Discriminant (T);
10177 for J in Discr_Expr'Range loop
10178 if Discr_Expr (J) /= Error then
10179 Append_Elmt (Discr_Expr (J), Elist);
10181 -- If any of the discriminant constraints is given by a
10182 -- discriminant and we are in a derived type declaration we
10183 -- have a discriminant renaming. Establish link between new
10184 -- and old discriminant. The new discriminant has an implicit
10185 -- dereference if the old one does.
10187 if Denotes_Discriminant (Discr_Expr (J)) then
10188 if Derived_Def then
10190 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10193 Set_Corresponding_Discriminant (New_Discr, Discr);
10194 Set_Has_Implicit_Dereference (New_Discr,
10195 Has_Implicit_Dereference (Discr));
10199 -- Force the evaluation of non-discriminant expressions.
10200 -- If we have found a discriminant in the constraint 3.4(26)
10201 -- and 3.8(18) demand that no range checks are performed are
10202 -- after evaluation. If the constraint is for a component
10203 -- definition that has a per-object constraint, expressions are
10204 -- evaluated but not checked either. In all other cases perform
10208 if Discrim_Present then
10211 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
10212 and then Has_Per_Object_Constraint
10213 (Defining_Identifier (Parent (Parent (Def))))
10217 elsif Is_Access_Type (Etype (Discr)) then
10218 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10221 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10224 Force_Evaluation (Discr_Expr (J));
10227 -- Check that the designated type of an access discriminant's
10228 -- expression is not a class-wide type unless the discriminant's
10229 -- designated type is also class-wide.
10231 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10232 and then not Is_Class_Wide_Type
10233 (Designated_Type (Etype (Discr)))
10234 and then Etype (Discr_Expr (J)) /= Any_Type
10235 and then Is_Class_Wide_Type
10236 (Designated_Type (Etype (Discr_Expr (J))))
10238 Wrong_Type (Discr_Expr (J), Etype (Discr));
10240 elsif Is_Access_Type (Etype (Discr))
10241 and then not Is_Access_Constant (Etype (Discr))
10242 and then Is_Access_Type (Etype (Discr_Expr (J)))
10243 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10246 ("constraint for discriminant& must be access to variable",
10251 Next_Discriminant (Discr);
10255 end Build_Discriminant_Constraints;
10257 ---------------------------------
10258 -- Build_Discriminated_Subtype --
10259 ---------------------------------
10261 procedure Build_Discriminated_Subtype
10263 Def_Id : Entity_Id;
10265 Related_Nod : Node_Id;
10266 For_Access : Boolean := False)
10268 Has_Discrs : constant Boolean := Has_Discriminants (T);
10269 Constrained : constant Boolean :=
10271 and then not Is_Empty_Elmt_List (Elist)
10272 and then not Is_Class_Wide_Type (T))
10273 or else Is_Constrained (T);
10276 if Ekind (T) = E_Record_Type then
10277 Set_Ekind (Def_Id, E_Record_Subtype);
10279 -- Inherit preelaboration flag from base, for types for which it
10280 -- may have been set: records, private types, protected types.
10282 Set_Known_To_Have_Preelab_Init
10283 (Def_Id, Known_To_Have_Preelab_Init (T));
10285 elsif Ekind (T) = E_Task_Type then
10286 Set_Ekind (Def_Id, E_Task_Subtype);
10288 elsif Ekind (T) = E_Protected_Type then
10289 Set_Ekind (Def_Id, E_Protected_Subtype);
10290 Set_Known_To_Have_Preelab_Init
10291 (Def_Id, Known_To_Have_Preelab_Init (T));
10293 elsif Is_Private_Type (T) then
10294 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10295 Set_Known_To_Have_Preelab_Init
10296 (Def_Id, Known_To_Have_Preelab_Init (T));
10298 -- Private subtypes may have private dependents
10300 Set_Private_Dependents (Def_Id, New_Elmt_List);
10302 elsif Is_Class_Wide_Type (T) then
10303 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
10306 -- Incomplete type. Attach subtype to list of dependents, to be
10307 -- completed with full view of parent type, unless is it the
10308 -- designated subtype of a record component within an init_proc.
10309 -- This last case arises for a component of an access type whose
10310 -- designated type is incomplete (e.g. a Taft Amendment type).
10311 -- The designated subtype is within an inner scope, and needs no
10312 -- elaboration, because only the access type is needed in the
10313 -- initialization procedure.
10315 if Ekind (T) = E_Incomplete_Type then
10316 Set_Ekind (Def_Id, E_Incomplete_Subtype);
10318 Set_Ekind (Def_Id, Ekind (T));
10321 if For_Access and then Within_Init_Proc then
10324 Append_Elmt (Def_Id, Private_Dependents (T));
10328 Set_Etype (Def_Id, T);
10329 Init_Size_Align (Def_Id);
10330 Set_Has_Discriminants (Def_Id, Has_Discrs);
10331 Set_Is_Constrained (Def_Id, Constrained);
10333 Set_First_Entity (Def_Id, First_Entity (T));
10334 Set_Last_Entity (Def_Id, Last_Entity (T));
10335 Set_Has_Implicit_Dereference
10336 (Def_Id, Has_Implicit_Dereference (T));
10337 Set_Has_Pragma_Unreferenced_Objects
10338 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10340 -- If the subtype is the completion of a private declaration, there may
10341 -- have been representation clauses for the partial view, and they must
10342 -- be preserved. Build_Derived_Type chains the inherited clauses with
10343 -- the ones appearing on the extension. If this comes from a subtype
10344 -- declaration, all clauses are inherited.
10346 if No (First_Rep_Item (Def_Id)) then
10347 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10350 if Is_Tagged_Type (T) then
10351 Set_Is_Tagged_Type (Def_Id);
10352 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10353 Make_Class_Wide_Type (Def_Id);
10356 Set_Stored_Constraint (Def_Id, No_Elist);
10359 Set_Discriminant_Constraint (Def_Id, Elist);
10360 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10363 if Is_Tagged_Type (T) then
10365 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10366 -- concurrent record type (which has the list of primitive
10369 if Ada_Version >= Ada_2005
10370 and then Is_Concurrent_Type (T)
10372 Set_Corresponding_Record_Type (Def_Id,
10373 Corresponding_Record_Type (T));
10375 Set_Direct_Primitive_Operations (Def_Id,
10376 Direct_Primitive_Operations (T));
10379 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10382 -- Subtypes introduced by component declarations do not need to be
10383 -- marked as delayed, and do not get freeze nodes, because the semantics
10384 -- verifies that the parents of the subtypes are frozen before the
10385 -- enclosing record is frozen.
10387 if not Is_Type (Scope (Def_Id)) then
10388 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10390 if Is_Private_Type (T)
10391 and then Present (Full_View (T))
10393 Conditional_Delay (Def_Id, Full_View (T));
10395 Conditional_Delay (Def_Id, T);
10399 if Is_Record_Type (T) then
10400 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10403 and then not Is_Empty_Elmt_List (Elist)
10404 and then not For_Access
10406 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10409 Set_Cloned_Subtype (Def_Id, T);
10412 end Build_Discriminated_Subtype;
10414 ---------------------------
10415 -- Build_Itype_Reference --
10416 ---------------------------
10418 procedure Build_Itype_Reference
10422 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10425 -- Itype references are only created for use by the back-end
10427 if Inside_A_Generic then
10430 Set_Itype (IR, Ityp);
10432 -- If Nod is a library unit entity, then Insert_After won't work,
10433 -- because Nod is not a member of any list. Therefore, we use
10434 -- Add_Global_Declaration in this case. This can happen if we have a
10435 -- build-in-place library function, child unit or not.
10437 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10438 or else (Nkind_In (Nod, N_Defining_Program_Unit_Name,
10439 N_Subprogram_Declaration)
10440 and then Is_Compilation_Unit (Defining_Entity (Nod)))
10442 Add_Global_Declaration (IR);
10444 Insert_After (Nod, IR);
10447 end Build_Itype_Reference;
10449 ------------------------
10450 -- Build_Scalar_Bound --
10451 ------------------------
10453 function Build_Scalar_Bound
10456 Der_T : Entity_Id) return Node_Id
10458 New_Bound : Entity_Id;
10461 -- Note: not clear why this is needed, how can the original bound
10462 -- be unanalyzed at this point? and if it is, what business do we
10463 -- have messing around with it? and why is the base type of the
10464 -- parent type the right type for the resolution. It probably is
10465 -- not. It is OK for the new bound we are creating, but not for
10466 -- the old one??? Still if it never happens, no problem.
10468 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10470 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10471 New_Bound := New_Copy (Bound);
10472 Set_Etype (New_Bound, Der_T);
10473 Set_Analyzed (New_Bound);
10475 elsif Is_Entity_Name (Bound) then
10476 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10478 -- The following is almost certainly wrong. What business do we have
10479 -- relocating a node (Bound) that is presumably still attached to
10480 -- the tree elsewhere???
10483 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10486 Set_Etype (New_Bound, Der_T);
10488 end Build_Scalar_Bound;
10490 -------------------------------
10491 -- Check_Abstract_Overriding --
10492 -------------------------------
10494 procedure Check_Abstract_Overriding (T : Entity_Id) is
10495 Alias_Subp : Entity_Id;
10497 Op_List : Elist_Id;
10499 Type_Def : Node_Id;
10501 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10502 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10503 -- which has pragma Implemented already set. Check whether Subp's entity
10504 -- kind conforms to the implementation kind of the overridden routine.
10506 procedure Check_Pragma_Implemented
10508 Iface_Subp : Entity_Id);
10509 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10510 -- Iface_Subp and both entities have pragma Implemented already set on
10511 -- them. Check whether the two implementation kinds are conforming.
10513 procedure Inherit_Pragma_Implemented
10515 Iface_Subp : Entity_Id);
10516 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10517 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10518 -- Propagate the implementation kind of Iface_Subp to Subp.
10520 ------------------------------
10521 -- Check_Pragma_Implemented --
10522 ------------------------------
10524 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10525 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10526 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10527 Subp_Alias : constant Entity_Id := Alias (Subp);
10528 Contr_Typ : Entity_Id;
10529 Impl_Subp : Entity_Id;
10532 -- Subp must have an alias since it is a hidden entity used to link
10533 -- an interface subprogram to its overriding counterpart.
10535 pragma Assert (Present (Subp_Alias));
10537 -- Handle aliases to synchronized wrappers
10539 Impl_Subp := Subp_Alias;
10541 if Is_Primitive_Wrapper (Impl_Subp) then
10542 Impl_Subp := Wrapped_Entity (Impl_Subp);
10545 -- Extract the type of the controlling formal
10547 Contr_Typ := Etype (First_Formal (Subp_Alias));
10549 if Is_Concurrent_Record_Type (Contr_Typ) then
10550 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10553 -- An interface subprogram whose implementation kind is By_Entry must
10554 -- be implemented by an entry.
10556 if Impl_Kind = Name_By_Entry
10557 and then Ekind (Impl_Subp) /= E_Entry
10559 Error_Msg_Node_2 := Iface_Alias;
10561 ("type & must implement abstract subprogram & with an entry",
10562 Subp_Alias, Contr_Typ);
10564 elsif Impl_Kind = Name_By_Protected_Procedure then
10566 -- An interface subprogram whose implementation kind is By_
10567 -- Protected_Procedure cannot be implemented by a primitive
10568 -- procedure of a task type.
10570 if Ekind (Contr_Typ) /= E_Protected_Type then
10571 Error_Msg_Node_2 := Contr_Typ;
10573 ("interface subprogram & cannot be implemented by a "
10574 & "primitive procedure of task type &",
10575 Subp_Alias, Iface_Alias);
10577 -- An interface subprogram whose implementation kind is By_
10578 -- Protected_Procedure must be implemented by a procedure.
10580 elsif Ekind (Impl_Subp) /= E_Procedure then
10581 Error_Msg_Node_2 := Iface_Alias;
10583 ("type & must implement abstract subprogram & with a "
10584 & "procedure", Subp_Alias, Contr_Typ);
10586 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10587 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10589 Error_Msg_Name_1 := Impl_Kind;
10591 ("overriding operation& must have synchronization%",
10595 -- If primitive has Optional synchronization, overriding operation
10596 -- must match if it has an explicit synchronization.
10598 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10599 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10601 Error_Msg_Name_1 := Impl_Kind;
10603 ("overriding operation& must have synchronization%", Subp_Alias);
10605 end Check_Pragma_Implemented;
10607 ------------------------------
10608 -- Check_Pragma_Implemented --
10609 ------------------------------
10611 procedure Check_Pragma_Implemented
10613 Iface_Subp : Entity_Id)
10615 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10616 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10619 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10620 -- and overriding subprogram are different. In general this is an
10621 -- error except when the implementation kind of the overridden
10622 -- subprograms is By_Any or Optional.
10624 if Iface_Kind /= Subp_Kind
10625 and then Iface_Kind /= Name_By_Any
10626 and then Iface_Kind /= Name_Optional
10628 if Iface_Kind = Name_By_Entry then
10630 ("incompatible implementation kind, overridden subprogram " &
10631 "is marked By_Entry", Subp);
10634 ("incompatible implementation kind, overridden subprogram " &
10635 "is marked By_Protected_Procedure", Subp);
10638 end Check_Pragma_Implemented;
10640 --------------------------------
10641 -- Inherit_Pragma_Implemented --
10642 --------------------------------
10644 procedure Inherit_Pragma_Implemented
10646 Iface_Subp : Entity_Id)
10648 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10649 Loc : constant Source_Ptr := Sloc (Subp);
10650 Impl_Prag : Node_Id;
10653 -- Since the implementation kind is stored as a representation item
10654 -- rather than a flag, create a pragma node.
10658 Chars => Name_Implemented,
10659 Pragma_Argument_Associations => New_List (
10660 Make_Pragma_Argument_Association (Loc,
10661 Expression => New_Occurrence_Of (Subp, Loc)),
10663 Make_Pragma_Argument_Association (Loc,
10664 Expression => Make_Identifier (Loc, Iface_Kind))));
10666 -- The pragma doesn't need to be analyzed because it is internally
10667 -- built. It is safe to directly register it as a rep item since we
10668 -- are only interested in the characters of the implementation kind.
10670 Record_Rep_Item (Subp, Impl_Prag);
10671 end Inherit_Pragma_Implemented;
10673 -- Start of processing for Check_Abstract_Overriding
10676 Op_List := Primitive_Operations (T);
10678 -- Loop to check primitive operations
10680 Elmt := First_Elmt (Op_List);
10681 while Present (Elmt) loop
10682 Subp := Node (Elmt);
10683 Alias_Subp := Alias (Subp);
10685 -- Inherited subprograms are identified by the fact that they do not
10686 -- come from source, and the associated source location is the
10687 -- location of the first subtype of the derived type.
10689 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10690 -- subprograms that "require overriding".
10692 -- Special exception, do not complain about failure to override the
10693 -- stream routines _Input and _Output, as well as the primitive
10694 -- operations used in dispatching selects since we always provide
10695 -- automatic overridings for these subprograms.
10697 -- The partial view of T may have been a private extension, for
10698 -- which inherited functions dispatching on result are abstract.
10699 -- If the full view is a null extension, there is no need for
10700 -- overriding in Ada 2005, but wrappers need to be built for them
10701 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10703 if Is_Null_Extension (T)
10704 and then Has_Controlling_Result (Subp)
10705 and then Ada_Version >= Ada_2005
10706 and then Present (Alias_Subp)
10707 and then not Comes_From_Source (Subp)
10708 and then not Is_Abstract_Subprogram (Alias_Subp)
10709 and then not Is_Access_Type (Etype (Subp))
10713 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10714 -- processing because this check is done with the aliased
10717 elsif Present (Interface_Alias (Subp)) then
10720 elsif (Is_Abstract_Subprogram (Subp)
10721 or else Requires_Overriding (Subp)
10723 (Has_Controlling_Result (Subp)
10724 and then Present (Alias_Subp)
10725 and then not Comes_From_Source (Subp)
10726 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10727 and then not Is_TSS (Subp, TSS_Stream_Input)
10728 and then not Is_TSS (Subp, TSS_Stream_Output)
10729 and then not Is_Abstract_Type (T)
10730 and then not Is_Predefined_Interface_Primitive (Subp)
10732 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10733 -- with abstract interface types because the check will be done
10734 -- with the aliased entity (otherwise we generate a duplicated
10737 and then not Present (Interface_Alias (Subp))
10739 if Present (Alias_Subp) then
10741 -- Only perform the check for a derived subprogram when the
10742 -- type has an explicit record extension. This avoids incorrect
10743 -- flagging of abstract subprograms for the case of a type
10744 -- without an extension that is derived from a formal type
10745 -- with a tagged actual (can occur within a private part).
10747 -- Ada 2005 (AI-391): In the case of an inherited function with
10748 -- a controlling result of the type, the rule does not apply if
10749 -- the type is a null extension (unless the parent function
10750 -- itself is abstract, in which case the function must still be
10751 -- be overridden). The expander will generate an overriding
10752 -- wrapper function calling the parent subprogram (see
10753 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10755 Type_Def := Type_Definition (Parent (T));
10757 if Nkind (Type_Def) = N_Derived_Type_Definition
10758 and then Present (Record_Extension_Part (Type_Def))
10760 (Ada_Version < Ada_2005
10761 or else not Is_Null_Extension (T)
10762 or else Ekind (Subp) = E_Procedure
10763 or else not Has_Controlling_Result (Subp)
10764 or else Is_Abstract_Subprogram (Alias_Subp)
10765 or else Requires_Overriding (Subp)
10766 or else Is_Access_Type (Etype (Subp)))
10768 -- Avoid reporting error in case of abstract predefined
10769 -- primitive inherited from interface type because the
10770 -- body of internally generated predefined primitives
10771 -- of tagged types are generated later by Freeze_Type
10773 if Is_Interface (Root_Type (T))
10774 and then Is_Abstract_Subprogram (Subp)
10775 and then Is_Predefined_Dispatching_Operation (Subp)
10776 and then not Comes_From_Source (Ultimate_Alias (Subp))
10780 -- A null extension is not obliged to override an inherited
10781 -- procedure subject to pragma Extensions_Visible with value
10782 -- False and at least one controlling OUT parameter
10783 -- (SPARK RM 6.1.7(6)).
10785 elsif Is_Null_Extension (T)
10786 and then Is_EVF_Procedure (Subp)
10792 ("type must be declared abstract or & overridden",
10795 -- Traverse the whole chain of aliased subprograms to
10796 -- complete the error notification. This is especially
10797 -- useful for traceability of the chain of entities when
10798 -- the subprogram corresponds with an interface
10799 -- subprogram (which may be defined in another package).
10801 if Present (Alias_Subp) then
10807 while Present (Alias (E)) loop
10809 -- Avoid reporting redundant errors on entities
10810 -- inherited from interfaces
10812 if Sloc (E) /= Sloc (T) then
10813 Error_Msg_Sloc := Sloc (E);
10815 ("\& has been inherited #", T, Subp);
10821 Error_Msg_Sloc := Sloc (E);
10823 -- AI05-0068: report if there is an overriding
10824 -- non-abstract subprogram that is invisible.
10827 and then not Is_Abstract_Subprogram (E)
10830 ("\& subprogram# is not visible",
10833 -- Clarify the case where a non-null extension must
10834 -- override inherited procedure subject to pragma
10835 -- Extensions_Visible with value False and at least
10836 -- one controlling OUT param.
10838 elsif Is_EVF_Procedure (E) then
10840 ("\& # is subject to Extensions_Visible False",
10845 ("\& has been inherited from subprogram #",
10852 -- Ada 2005 (AI-345): Protected or task type implementing
10853 -- abstract interfaces.
10855 elsif Is_Concurrent_Record_Type (T)
10856 and then Present (Interfaces (T))
10858 -- There is no need to check here RM 9.4(11.9/3) since we
10859 -- are processing the corresponding record type and the
10860 -- mode of the overriding subprograms was verified by
10861 -- Check_Conformance when the corresponding concurrent
10862 -- type declaration was analyzed.
10865 ("interface subprogram & must be overridden", T, Subp);
10867 -- Examine primitive operations of synchronized type to find
10868 -- homonyms that have the wrong profile.
10874 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10875 while Present (Prim) loop
10876 if Chars (Prim) = Chars (Subp) then
10878 ("profile is not type conformant with prefixed "
10879 & "view profile of inherited operation&",
10883 Next_Entity (Prim);
10889 Error_Msg_Node_2 := T;
10891 ("abstract subprogram& not allowed for type&", Subp);
10893 -- Also post unconditional warning on the type (unconditional
10894 -- so that if there are more than one of these cases, we get
10895 -- them all, and not just the first one).
10897 Error_Msg_Node_2 := Subp;
10898 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10901 -- A subprogram subject to pragma Extensions_Visible with value
10902 -- "True" cannot override a subprogram subject to the same pragma
10903 -- with value "False" (SPARK RM 6.1.7(5)).
10905 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10906 and then Present (Overridden_Operation (Subp))
10907 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10908 Extensions_Visible_False
10910 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10912 ("subprogram & with Extensions_Visible True cannot override "
10913 & "subprogram # with Extensions_Visible False", Subp);
10916 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10918 -- Subp is an expander-generated procedure which maps an interface
10919 -- alias to a protected wrapper. The interface alias is flagged by
10920 -- pragma Implemented. Ensure that Subp is a procedure when the
10921 -- implementation kind is By_Protected_Procedure or an entry when
10924 if Ada_Version >= Ada_2012
10925 and then Is_Hidden (Subp)
10926 and then Present (Interface_Alias (Subp))
10927 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10929 Check_Pragma_Implemented (Subp);
10932 -- Subp is an interface primitive which overrides another interface
10933 -- primitive marked with pragma Implemented.
10935 if Ada_Version >= Ada_2012
10936 and then Present (Overridden_Operation (Subp))
10937 and then Has_Rep_Pragma
10938 (Overridden_Operation (Subp), Name_Implemented)
10940 -- If the overriding routine is also marked by Implemented, check
10941 -- that the two implementation kinds are conforming.
10943 if Has_Rep_Pragma (Subp, Name_Implemented) then
10944 Check_Pragma_Implemented
10946 Iface_Subp => Overridden_Operation (Subp));
10948 -- Otherwise the overriding routine inherits the implementation
10949 -- kind from the overridden subprogram.
10952 Inherit_Pragma_Implemented
10954 Iface_Subp => Overridden_Operation (Subp));
10958 -- If the operation is a wrapper for a synchronized primitive, it
10959 -- may be called indirectly through a dispatching select. We assume
10960 -- that it will be referenced elsewhere indirectly, and suppress
10961 -- warnings about an unused entity.
10963 if Is_Primitive_Wrapper (Subp)
10964 and then Present (Wrapped_Entity (Subp))
10966 Set_Referenced (Wrapped_Entity (Subp));
10971 end Check_Abstract_Overriding;
10973 ------------------------------------------------
10974 -- Check_Access_Discriminant_Requires_Limited --
10975 ------------------------------------------------
10977 procedure Check_Access_Discriminant_Requires_Limited
10982 -- A discriminant_specification for an access discriminant shall appear
10983 -- only in the declaration for a task or protected type, or for a type
10984 -- with the reserved word 'limited' in its definition or in one of its
10985 -- ancestors (RM 3.7(10)).
10987 -- AI-0063: The proper condition is that type must be immutably limited,
10988 -- or else be a partial view.
10990 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
10991 if Is_Limited_View (Current_Scope)
10993 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
10994 and then Limited_Present (Parent (Current_Scope)))
11000 ("access discriminants allowed only for limited types", Loc);
11003 end Check_Access_Discriminant_Requires_Limited;
11005 -----------------------------------
11006 -- Check_Aliased_Component_Types --
11007 -----------------------------------
11009 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11013 -- ??? Also need to check components of record extensions, but not
11014 -- components of protected types (which are always limited).
11016 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11017 -- types to be unconstrained. This is safe because it is illegal to
11018 -- create access subtypes to such types with explicit discriminant
11021 if not Is_Limited_Type (T) then
11022 if Ekind (T) = E_Record_Type then
11023 C := First_Component (T);
11024 while Present (C) loop
11026 and then Has_Discriminants (Etype (C))
11027 and then not Is_Constrained (Etype (C))
11028 and then not In_Instance_Body
11029 and then Ada_Version < Ada_2005
11032 ("aliased component must be constrained (RM 3.6(11))",
11036 Next_Component (C);
11039 elsif Ekind (T) = E_Array_Type then
11040 if Has_Aliased_Components (T)
11041 and then Has_Discriminants (Component_Type (T))
11042 and then not Is_Constrained (Component_Type (T))
11043 and then not In_Instance_Body
11044 and then Ada_Version < Ada_2005
11047 ("aliased component type must be constrained (RM 3.6(11))",
11052 end Check_Aliased_Component_Types;
11054 ---------------------------------------
11055 -- Check_Anonymous_Access_Components --
11056 ---------------------------------------
11058 procedure Check_Anonymous_Access_Components
11059 (Typ_Decl : Node_Id;
11062 Comp_List : Node_Id)
11064 Loc : constant Source_Ptr := Sloc (Typ_Decl);
11065 Anon_Access : Entity_Id;
11068 Comp_Def : Node_Id;
11070 Type_Def : Node_Id;
11072 procedure Build_Incomplete_Type_Declaration;
11073 -- If the record type contains components that include an access to the
11074 -- current record, then create an incomplete type declaration for the
11075 -- record, to be used as the designated type of the anonymous access.
11076 -- This is done only once, and only if there is no previous partial
11077 -- view of the type.
11079 function Designates_T (Subt : Node_Id) return Boolean;
11080 -- Check whether a node designates the enclosing record type, or 'Class
11083 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11084 -- Check whether an access definition includes a reference to
11085 -- the enclosing record type. The reference can be a subtype mark
11086 -- in the access definition itself, a 'Class attribute reference, or
11087 -- recursively a reference appearing in a parameter specification
11088 -- or result definition of an access_to_subprogram definition.
11090 --------------------------------------
11091 -- Build_Incomplete_Type_Declaration --
11092 --------------------------------------
11094 procedure Build_Incomplete_Type_Declaration is
11099 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11100 -- it's "is new ... with record" or else "is tagged record ...".
11102 Is_Tagged : constant Boolean :=
11103 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
11105 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
11107 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
11108 and then Tagged_Present (Type_Definition (Typ_Decl)));
11111 -- If there is a previous partial view, no need to create a new one
11112 -- If the partial view, given by Prev, is incomplete, If Prev is
11113 -- a private declaration, full declaration is flagged accordingly.
11115 if Prev /= Typ then
11117 Make_Class_Wide_Type (Prev);
11118 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11119 Set_Etype (Class_Wide_Type (Typ), Typ);
11124 elsif Has_Private_Declaration (Typ) then
11126 -- If we refer to T'Class inside T, and T is the completion of a
11127 -- private type, then make sure the class-wide type exists.
11130 Make_Class_Wide_Type (Typ);
11135 -- If there was a previous anonymous access type, the incomplete
11136 -- type declaration will have been created already.
11138 elsif Present (Current_Entity (Typ))
11139 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11140 and then Full_View (Current_Entity (Typ)) = Typ
11143 and then Comes_From_Source (Current_Entity (Typ))
11144 and then not Is_Tagged_Type (Current_Entity (Typ))
11146 Make_Class_Wide_Type (Typ);
11148 ("incomplete view of tagged type should be declared tagged??",
11149 Parent (Current_Entity (Typ)));
11154 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11155 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11157 -- Type has already been inserted into the current scope. Remove
11158 -- it, and add incomplete declaration for type, so that subsequent
11159 -- anonymous access types can use it. The entity is unchained from
11160 -- the homonym list and from immediate visibility. After analysis,
11161 -- the entity in the incomplete declaration becomes immediately
11162 -- visible in the record declaration that follows.
11164 H := Current_Entity (Typ);
11167 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11170 and then Homonym (H) /= Typ
11172 H := Homonym (Typ);
11175 Set_Homonym (H, Homonym (Typ));
11178 Insert_Before (Typ_Decl, Decl);
11180 Set_Full_View (Inc_T, Typ);
11184 -- Create a common class-wide type for both views, and set the
11185 -- Etype of the class-wide type to the full view.
11187 Make_Class_Wide_Type (Inc_T);
11188 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11189 Set_Etype (Class_Wide_Type (Typ), Typ);
11192 end Build_Incomplete_Type_Declaration;
11198 function Designates_T (Subt : Node_Id) return Boolean is
11199 Type_Id : constant Name_Id := Chars (Typ);
11201 function Names_T (Nam : Node_Id) return Boolean;
11202 -- The record type has not been introduced in the current scope
11203 -- yet, so we must examine the name of the type itself, either
11204 -- an identifier T, or an expanded name of the form P.T, where
11205 -- P denotes the current scope.
11211 function Names_T (Nam : Node_Id) return Boolean is
11213 if Nkind (Nam) = N_Identifier then
11214 return Chars (Nam) = Type_Id;
11216 elsif Nkind (Nam) = N_Selected_Component then
11217 if Chars (Selector_Name (Nam)) = Type_Id then
11218 if Nkind (Prefix (Nam)) = N_Identifier then
11219 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11221 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11222 return Chars (Selector_Name (Prefix (Nam))) =
11223 Chars (Current_Scope);
11237 -- Start of processing for Designates_T
11240 if Nkind (Subt) = N_Identifier then
11241 return Chars (Subt) = Type_Id;
11243 -- Reference can be through an expanded name which has not been
11244 -- analyzed yet, and which designates enclosing scopes.
11246 elsif Nkind (Subt) = N_Selected_Component then
11247 if Names_T (Subt) then
11250 -- Otherwise it must denote an entity that is already visible.
11251 -- The access definition may name a subtype of the enclosing
11252 -- type, if there is a previous incomplete declaration for it.
11255 Find_Selected_Component (Subt);
11257 Is_Entity_Name (Subt)
11258 and then Scope (Entity (Subt)) = Current_Scope
11260 (Chars (Base_Type (Entity (Subt))) = Type_Id
11262 (Is_Class_Wide_Type (Entity (Subt))
11264 Chars (Etype (Base_Type (Entity (Subt)))) =
11268 -- A reference to the current type may appear as the prefix of
11269 -- a 'Class attribute.
11271 elsif Nkind (Subt) = N_Attribute_Reference
11272 and then Attribute_Name (Subt) = Name_Class
11274 return Names_T (Prefix (Subt));
11285 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11286 Param_Spec : Node_Id;
11288 Acc_Subprg : constant Node_Id :=
11289 Access_To_Subprogram_Definition (Acc_Def);
11292 if No (Acc_Subprg) then
11293 return Designates_T (Subtype_Mark (Acc_Def));
11296 -- Component is an access_to_subprogram: examine its formals,
11297 -- and result definition in the case of an access_to_function.
11299 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11300 while Present (Param_Spec) loop
11301 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11302 and then Mentions_T (Parameter_Type (Param_Spec))
11306 elsif Designates_T (Parameter_Type (Param_Spec)) then
11313 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11314 if Nkind (Result_Definition (Acc_Subprg)) =
11315 N_Access_Definition
11317 return Mentions_T (Result_Definition (Acc_Subprg));
11319 return Designates_T (Result_Definition (Acc_Subprg));
11326 -- Start of processing for Check_Anonymous_Access_Components
11329 if No (Comp_List) then
11333 Comp := First (Component_Items (Comp_List));
11334 while Present (Comp) loop
11335 if Nkind (Comp) = N_Component_Declaration
11337 (Access_Definition (Component_Definition (Comp)))
11339 Mentions_T (Access_Definition (Component_Definition (Comp)))
11341 Comp_Def := Component_Definition (Comp);
11343 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11345 Build_Incomplete_Type_Declaration;
11346 Anon_Access := Make_Temporary (Loc, 'S');
11348 -- Create a declaration for the anonymous access type: either
11349 -- an access_to_object or an access_to_subprogram.
11351 if Present (Acc_Def) then
11352 if Nkind (Acc_Def) = N_Access_Function_Definition then
11354 Make_Access_Function_Definition (Loc,
11355 Parameter_Specifications =>
11356 Parameter_Specifications (Acc_Def),
11357 Result_Definition => Result_Definition (Acc_Def));
11360 Make_Access_Procedure_Definition (Loc,
11361 Parameter_Specifications =>
11362 Parameter_Specifications (Acc_Def));
11367 Make_Access_To_Object_Definition (Loc,
11368 Subtype_Indication =>
11370 (Subtype_Mark (Access_Definition (Comp_Def))));
11372 Set_Constant_Present
11373 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11375 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11378 Set_Null_Exclusion_Present
11380 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11383 Make_Full_Type_Declaration (Loc,
11384 Defining_Identifier => Anon_Access,
11385 Type_Definition => Type_Def);
11387 Insert_Before (Typ_Decl, Decl);
11390 -- If an access to subprogram, create the extra formals
11392 if Present (Acc_Def) then
11393 Create_Extra_Formals (Designated_Type (Anon_Access));
11395 -- If an access to object, preserve entity of designated type,
11396 -- for ASIS use, before rewriting the component definition.
11397 -- Is this still needed???
11404 Desig := Entity (Subtype_Indication (Type_Def));
11406 -- If the access definition is to the current record,
11407 -- the visible entity at this point is an incomplete
11408 -- type. Retrieve the full view to simplify ASIS queries
11410 if Ekind (Desig) = E_Incomplete_Type then
11411 Desig := Full_View (Desig);
11415 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11420 Make_Component_Definition (Loc,
11421 Subtype_Indication =>
11422 New_Occurrence_Of (Anon_Access, Loc)));
11424 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11425 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11427 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11430 Set_Is_Local_Anonymous_Access (Anon_Access);
11436 if Present (Variant_Part (Comp_List)) then
11440 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11441 while Present (V) loop
11442 Check_Anonymous_Access_Components
11443 (Typ_Decl, Typ, Prev, Component_List (V));
11444 Next_Non_Pragma (V);
11448 end Check_Anonymous_Access_Components;
11450 ----------------------
11451 -- Check_Completion --
11452 ----------------------
11454 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11457 procedure Post_Error;
11458 -- Post error message for lack of completion for entity E
11464 procedure Post_Error is
11465 procedure Missing_Body;
11466 -- Output missing body message
11472 procedure Missing_Body is
11474 -- Spec is in same unit, so we can post on spec
11476 if In_Same_Source_Unit (Body_Id, E) then
11477 Error_Msg_N ("missing body for &", E);
11479 -- Spec is in a separate unit, so we have to post on the body
11482 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11486 -- Start of processing for Post_Error
11489 if not Comes_From_Source (E) then
11490 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11492 -- It may be an anonymous protected type created for a
11493 -- single variable. Post error on variable, if present.
11499 Var := First_Entity (Current_Scope);
11500 while Present (Var) loop
11501 exit when Etype (Var) = E
11502 and then Comes_From_Source (Var);
11507 if Present (Var) then
11514 -- If a generated entity has no completion, then either previous
11515 -- semantic errors have disabled the expansion phase, or else we had
11516 -- missing subunits, or else we are compiling without expansion,
11517 -- or else something is very wrong.
11519 if not Comes_From_Source (E) then
11521 (Serious_Errors_Detected > 0
11522 or else Configurable_Run_Time_Violations > 0
11523 or else Subunits_Missing
11524 or else not Expander_Active);
11527 -- Here for source entity
11530 -- Here if no body to post the error message, so we post the error
11531 -- on the declaration that has no completion. This is not really
11532 -- the right place to post it, think about this later ???
11534 if No (Body_Id) then
11535 if Is_Type (E) then
11537 ("missing full declaration for }", Parent (E), E);
11539 Error_Msg_NE ("missing body for &", Parent (E), E);
11542 -- Package body has no completion for a declaration that appears
11543 -- in the corresponding spec. Post error on the body, with a
11544 -- reference to the non-completed declaration.
11547 Error_Msg_Sloc := Sloc (E);
11549 if Is_Type (E) then
11550 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11552 elsif Is_Overloadable (E)
11553 and then Current_Entity_In_Scope (E) /= E
11555 -- It may be that the completion is mistyped and appears as
11556 -- a distinct overloading of the entity.
11559 Candidate : constant Entity_Id :=
11560 Current_Entity_In_Scope (E);
11561 Decl : constant Node_Id :=
11562 Unit_Declaration_Node (Candidate);
11565 if Is_Overloadable (Candidate)
11566 and then Ekind (Candidate) = Ekind (E)
11567 and then Nkind (Decl) = N_Subprogram_Body
11568 and then Acts_As_Spec (Decl)
11570 Check_Type_Conformant (Candidate, E);
11586 Pack_Id : constant Entity_Id := Current_Scope;
11588 -- Start of processing for Check_Completion
11591 E := First_Entity (Pack_Id);
11592 while Present (E) loop
11593 if Is_Intrinsic_Subprogram (E) then
11596 -- The following situation requires special handling: a child unit
11597 -- that appears in the context clause of the body of its parent:
11599 -- procedure Parent.Child (...);
11601 -- with Parent.Child;
11602 -- package body Parent is
11604 -- Here Parent.Child appears as a local entity, but should not be
11605 -- flagged as requiring completion, because it is a compilation
11608 -- Ignore missing completion for a subprogram that does not come from
11609 -- source (including the _Call primitive operation of RAS types,
11610 -- which has to have the flag Comes_From_Source for other purposes):
11611 -- we assume that the expander will provide the missing completion.
11612 -- In case of previous errors, other expansion actions that provide
11613 -- bodies for null procedures with not be invoked, so inhibit message
11616 -- Note that E_Operator is not in the list that follows, because
11617 -- this kind is reserved for predefined operators, that are
11618 -- intrinsic and do not need completion.
11620 elsif Ekind_In (E, E_Function,
11622 E_Generic_Function,
11623 E_Generic_Procedure)
11625 if Has_Completion (E) then
11628 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11631 elsif Is_Subprogram (E)
11632 and then (not Comes_From_Source (E)
11633 or else Chars (E) = Name_uCall)
11638 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11642 elsif Nkind (Parent (E)) = N_Procedure_Specification
11643 and then Null_Present (Parent (E))
11644 and then Serious_Errors_Detected > 0
11652 elsif Is_Entry (E) then
11653 if not Has_Completion (E) and then
11654 (Ekind (Scope (E)) = E_Protected_Object
11655 or else Ekind (Scope (E)) = E_Protected_Type)
11660 elsif Is_Package_Or_Generic_Package (E) then
11661 if Unit_Requires_Body (E) then
11662 if not Has_Completion (E)
11663 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11669 elsif not Is_Child_Unit (E) then
11670 May_Need_Implicit_Body (E);
11673 -- A formal incomplete type (Ada 2012) does not require a completion;
11674 -- other incomplete type declarations do.
11676 elsif Ekind (E) = E_Incomplete_Type
11677 and then No (Underlying_Type (E))
11678 and then not Is_Generic_Type (E)
11682 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11683 and then not Has_Completion (E)
11687 -- A single task declared in the current scope is a constant, verify
11688 -- that the body of its anonymous type is in the same scope. If the
11689 -- task is defined elsewhere, this may be a renaming declaration for
11690 -- which no completion is needed.
11692 elsif Ekind (E) = E_Constant
11693 and then Ekind (Etype (E)) = E_Task_Type
11694 and then not Has_Completion (Etype (E))
11695 and then Scope (Etype (E)) = Current_Scope
11699 elsif Ekind (E) = E_Protected_Object
11700 and then not Has_Completion (Etype (E))
11704 elsif Ekind (E) = E_Record_Type then
11705 if Is_Tagged_Type (E) then
11706 Check_Abstract_Overriding (E);
11707 Check_Conventions (E);
11710 Check_Aliased_Component_Types (E);
11712 elsif Ekind (E) = E_Array_Type then
11713 Check_Aliased_Component_Types (E);
11719 end Check_Completion;
11721 ------------------------------------
11722 -- Check_CPP_Type_Has_No_Defaults --
11723 ------------------------------------
11725 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11726 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11731 -- Obtain the component list
11733 if Nkind (Tdef) = N_Record_Definition then
11734 Clist := Component_List (Tdef);
11735 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11736 Clist := Component_List (Record_Extension_Part (Tdef));
11739 -- Check all components to ensure no default expressions
11741 if Present (Clist) then
11742 Comp := First (Component_Items (Clist));
11743 while Present (Comp) loop
11744 if Present (Expression (Comp)) then
11746 ("component of imported 'C'P'P type cannot have "
11747 & "default expression", Expression (Comp));
11753 end Check_CPP_Type_Has_No_Defaults;
11755 ----------------------------
11756 -- Check_Delta_Expression --
11757 ----------------------------
11759 procedure Check_Delta_Expression (E : Node_Id) is
11761 if not (Is_Real_Type (Etype (E))) then
11762 Wrong_Type (E, Any_Real);
11764 elsif not Is_OK_Static_Expression (E) then
11765 Flag_Non_Static_Expr
11766 ("non-static expression used for delta value!", E);
11768 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11769 Error_Msg_N ("delta expression must be positive", E);
11775 -- If any of above errors occurred, then replace the incorrect
11776 -- expression by the real 0.1, which should prevent further errors.
11779 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11780 Analyze_And_Resolve (E, Standard_Float);
11781 end Check_Delta_Expression;
11783 -----------------------------
11784 -- Check_Digits_Expression --
11785 -----------------------------
11787 procedure Check_Digits_Expression (E : Node_Id) is
11789 if not (Is_Integer_Type (Etype (E))) then
11790 Wrong_Type (E, Any_Integer);
11792 elsif not Is_OK_Static_Expression (E) then
11793 Flag_Non_Static_Expr
11794 ("non-static expression used for digits value!", E);
11796 elsif Expr_Value (E) <= 0 then
11797 Error_Msg_N ("digits value must be greater than zero", E);
11803 -- If any of above errors occurred, then replace the incorrect
11804 -- expression by the integer 1, which should prevent further errors.
11806 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11807 Analyze_And_Resolve (E, Standard_Integer);
11809 end Check_Digits_Expression;
11811 --------------------------
11812 -- Check_Initialization --
11813 --------------------------
11815 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11817 -- Special processing for limited types
11819 if Is_Limited_Type (T)
11820 and then not In_Instance
11821 and then not In_Inlined_Body
11823 if not OK_For_Limited_Init (T, Exp) then
11825 -- In GNAT mode, this is just a warning, to allow it to be evilly
11826 -- turned off. Otherwise it is a real error.
11830 ("??cannot initialize entities of limited type!", Exp);
11832 elsif Ada_Version < Ada_2005 then
11834 -- The side effect removal machinery may generate illegal Ada
11835 -- code to avoid the usage of access types and 'reference in
11836 -- SPARK mode. Since this is legal code with respect to theorem
11837 -- proving, do not emit the error.
11840 and then Nkind (Exp) = N_Function_Call
11841 and then Nkind (Parent (Exp)) = N_Object_Declaration
11842 and then not Comes_From_Source
11843 (Defining_Identifier (Parent (Exp)))
11849 ("cannot initialize entities of limited type", Exp);
11850 Explain_Limited_Type (T, Exp);
11854 -- Specialize error message according to kind of illegal
11855 -- initial expression. We check the Original_Node to cover
11856 -- cases where the initialization expression of an object
11857 -- declaration generated by the compiler has been rewritten
11858 -- (such as for dispatching calls).
11860 if Nkind (Original_Node (Exp)) = N_Type_Conversion
11862 Nkind (Expression (Original_Node (Exp))) = N_Function_Call
11864 -- No error for internally-generated object declarations,
11865 -- which can come from build-in-place assignment statements.
11867 if Nkind (Parent (Exp)) = N_Object_Declaration
11868 and then not Comes_From_Source
11869 (Defining_Identifier (Parent (Exp)))
11875 ("illegal context for call to function with limited "
11881 ("initialization of limited object requires aggregate or "
11882 & "function call", Exp);
11888 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11889 -- set unless we can be sure that no range check is required.
11891 if (GNATprove_Mode or not Expander_Active)
11892 and then Is_Scalar_Type (T)
11893 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11895 Set_Do_Range_Check (Exp);
11897 end Check_Initialization;
11899 ----------------------
11900 -- Check_Interfaces --
11901 ----------------------
11903 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11904 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11907 Iface_Def : Node_Id;
11908 Iface_Typ : Entity_Id;
11909 Parent_Node : Node_Id;
11911 Is_Task : Boolean := False;
11912 -- Set True if parent type or any progenitor is a task interface
11914 Is_Protected : Boolean := False;
11915 -- Set True if parent type or any progenitor is a protected interface
11917 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11918 -- Check that a progenitor is compatible with declaration. If an error
11919 -- message is output, it is posted on Error_Node.
11925 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11926 Iface_Id : constant Entity_Id :=
11927 Defining_Identifier (Parent (Iface_Def));
11928 Type_Def : Node_Id;
11931 if Nkind (N) = N_Private_Extension_Declaration then
11934 Type_Def := Type_Definition (N);
11937 if Is_Task_Interface (Iface_Id) then
11940 elsif Is_Protected_Interface (Iface_Id) then
11941 Is_Protected := True;
11944 if Is_Synchronized_Interface (Iface_Id) then
11946 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
11947 -- extension derived from a synchronized interface must explicitly
11948 -- be declared synchronized, because the full view will be a
11949 -- synchronized type.
11951 if Nkind (N) = N_Private_Extension_Declaration then
11952 if not Synchronized_Present (N) then
11954 ("private extension of& must be explicitly synchronized",
11958 -- However, by 3.9.4(16/2), a full type that is a record extension
11959 -- is never allowed to derive from a synchronized interface (note
11960 -- that interfaces must be excluded from this check, because those
11961 -- are represented by derived type definitions in some cases).
11963 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
11964 and then not Interface_Present (Type_Definition (N))
11966 Error_Msg_N ("record extension cannot derive from synchronized "
11967 & "interface", Error_Node);
11971 -- Check that the characteristics of the progenitor are compatible
11972 -- with the explicit qualifier in the declaration.
11973 -- The check only applies to qualifiers that come from source.
11974 -- Limited_Present also appears in the declaration of corresponding
11975 -- records, and the check does not apply to them.
11977 if Limited_Present (Type_Def)
11979 Is_Concurrent_Record_Type (Defining_Identifier (N))
11981 if Is_Limited_Interface (Parent_Type)
11982 and then not Is_Limited_Interface (Iface_Id)
11985 ("progenitor & must be limited interface",
11986 Error_Node, Iface_Id);
11989 (Task_Present (Iface_Def)
11990 or else Protected_Present (Iface_Def)
11991 or else Synchronized_Present (Iface_Def))
11992 and then Nkind (N) /= N_Private_Extension_Declaration
11993 and then not Error_Posted (N)
11996 ("progenitor & must be limited interface",
11997 Error_Node, Iface_Id);
12000 -- Protected interfaces can only inherit from limited, synchronized
12001 -- or protected interfaces.
12003 elsif Nkind (N) = N_Full_Type_Declaration
12004 and then Protected_Present (Type_Def)
12006 if Limited_Present (Iface_Def)
12007 or else Synchronized_Present (Iface_Def)
12008 or else Protected_Present (Iface_Def)
12012 elsif Task_Present (Iface_Def) then
12013 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12014 & "from task interface", Error_Node);
12017 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12018 & "from non-limited interface", Error_Node);
12021 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12022 -- limited and synchronized.
12024 elsif Synchronized_Present (Type_Def) then
12025 if Limited_Present (Iface_Def)
12026 or else Synchronized_Present (Iface_Def)
12030 elsif Protected_Present (Iface_Def)
12031 and then Nkind (N) /= N_Private_Extension_Declaration
12033 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12034 & "from protected interface", Error_Node);
12036 elsif Task_Present (Iface_Def)
12037 and then Nkind (N) /= N_Private_Extension_Declaration
12039 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12040 & "from task interface", Error_Node);
12042 elsif not Is_Limited_Interface (Iface_Id) then
12043 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12044 & "from non-limited interface", Error_Node);
12047 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12048 -- synchronized or task interfaces.
12050 elsif Nkind (N) = N_Full_Type_Declaration
12051 and then Task_Present (Type_Def)
12053 if Limited_Present (Iface_Def)
12054 or else Synchronized_Present (Iface_Def)
12055 or else Task_Present (Iface_Def)
12059 elsif Protected_Present (Iface_Def) then
12060 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12061 & "protected interface", Error_Node);
12064 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12065 & "non-limited interface", Error_Node);
12070 -- Start of processing for Check_Interfaces
12073 if Is_Interface (Parent_Type) then
12074 if Is_Task_Interface (Parent_Type) then
12077 elsif Is_Protected_Interface (Parent_Type) then
12078 Is_Protected := True;
12082 if Nkind (N) = N_Private_Extension_Declaration then
12084 -- Check that progenitors are compatible with declaration
12086 Iface := First (Interface_List (Def));
12087 while Present (Iface) loop
12088 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12090 Parent_Node := Parent (Base_Type (Iface_Typ));
12091 Iface_Def := Type_Definition (Parent_Node);
12093 if not Is_Interface (Iface_Typ) then
12094 Diagnose_Interface (Iface, Iface_Typ);
12096 Check_Ifaces (Iface_Def, Iface);
12102 if Is_Task and Is_Protected then
12104 ("type cannot derive from task and protected interface", N);
12110 -- Full type declaration of derived type.
12111 -- Check compatibility with parent if it is interface type
12113 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12114 and then Is_Interface (Parent_Type)
12116 Parent_Node := Parent (Parent_Type);
12118 -- More detailed checks for interface varieties
12121 (Iface_Def => Type_Definition (Parent_Node),
12122 Error_Node => Subtype_Indication (Type_Definition (N)));
12125 Iface := First (Interface_List (Def));
12126 while Present (Iface) loop
12127 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12129 Parent_Node := Parent (Base_Type (Iface_Typ));
12130 Iface_Def := Type_Definition (Parent_Node);
12132 if not Is_Interface (Iface_Typ) then
12133 Diagnose_Interface (Iface, Iface_Typ);
12136 -- "The declaration of a specific descendant of an interface
12137 -- type freezes the interface type" RM 13.14
12139 Freeze_Before (N, Iface_Typ);
12140 Check_Ifaces (Iface_Def, Error_Node => Iface);
12146 if Is_Task and Is_Protected then
12148 ("type cannot derive from task and protected interface", N);
12150 end Check_Interfaces;
12152 ------------------------------------
12153 -- Check_Or_Process_Discriminants --
12154 ------------------------------------
12156 -- If an incomplete or private type declaration was already given for the
12157 -- type, the discriminants may have already been processed if they were
12158 -- present on the incomplete declaration. In this case a full conformance
12159 -- check has been performed in Find_Type_Name, and we then recheck here
12160 -- some properties that can't be checked on the partial view alone.
12161 -- Otherwise we call Process_Discriminants.
12163 procedure Check_Or_Process_Discriminants
12166 Prev : Entity_Id := Empty)
12169 if Has_Discriminants (T) then
12171 -- Discriminants are already set on T if they were already present
12172 -- on the partial view. Make them visible to component declarations.
12176 -- Discriminant on T (full view) referencing expr on partial view
12178 Prev_D : Entity_Id;
12179 -- Entity of corresponding discriminant on partial view
12182 -- Discriminant specification for full view, expression is
12183 -- the syntactic copy on full view (which has been checked for
12184 -- conformance with partial view), only used here to post error
12188 D := First_Discriminant (T);
12189 New_D := First (Discriminant_Specifications (N));
12190 while Present (D) loop
12191 Prev_D := Current_Entity (D);
12192 Set_Current_Entity (D);
12193 Set_Is_Immediately_Visible (D);
12194 Set_Homonym (D, Prev_D);
12196 -- Handle the case where there is an untagged partial view and
12197 -- the full view is tagged: must disallow discriminants with
12198 -- defaults, unless compiling for Ada 2012, which allows a
12199 -- limited tagged type to have defaulted discriminants (see
12200 -- AI05-0214). However, suppress error here if it was already
12201 -- reported on the default expression of the partial view.
12203 if Is_Tagged_Type (T)
12204 and then Present (Expression (Parent (D)))
12205 and then (not Is_Limited_Type (Current_Scope)
12206 or else Ada_Version < Ada_2012)
12207 and then not Error_Posted (Expression (Parent (D)))
12209 if Ada_Version >= Ada_2012 then
12211 ("discriminants of nonlimited tagged type cannot have "
12213 Expression (New_D));
12216 ("discriminants of tagged type cannot have defaults",
12217 Expression (New_D));
12221 -- Ada 2005 (AI-230): Access discriminant allowed in
12222 -- non-limited record types.
12224 if Ada_Version < Ada_2005 then
12226 -- This restriction gets applied to the full type here. It
12227 -- has already been applied earlier to the partial view.
12229 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12232 Next_Discriminant (D);
12237 elsif Present (Discriminant_Specifications (N)) then
12238 Process_Discriminants (N, Prev);
12240 end Check_Or_Process_Discriminants;
12242 ----------------------
12243 -- Check_Real_Bound --
12244 ----------------------
12246 procedure Check_Real_Bound (Bound : Node_Id) is
12248 if not Is_Real_Type (Etype (Bound)) then
12250 ("bound in real type definition must be of real type", Bound);
12252 elsif not Is_OK_Static_Expression (Bound) then
12253 Flag_Non_Static_Expr
12254 ("non-static expression used for real type bound!", Bound);
12261 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12263 Resolve (Bound, Standard_Float);
12264 end Check_Real_Bound;
12266 ------------------------------
12267 -- Complete_Private_Subtype --
12268 ------------------------------
12270 procedure Complete_Private_Subtype
12273 Full_Base : Entity_Id;
12274 Related_Nod : Node_Id)
12276 Save_Next_Entity : Entity_Id;
12277 Save_Homonym : Entity_Id;
12280 -- Set semantic attributes for (implicit) private subtype completion.
12281 -- If the full type has no discriminants, then it is a copy of the
12282 -- full view of the base. Otherwise, it is a subtype of the base with
12283 -- a possible discriminant constraint. Save and restore the original
12284 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12285 -- not corrupt the entity chain.
12287 Save_Next_Entity := Next_Entity (Full);
12288 Save_Homonym := Homonym (Priv);
12290 if Is_Private_Type (Full_Base)
12291 or else Is_Record_Type (Full_Base)
12292 or else Is_Concurrent_Type (Full_Base)
12294 Copy_Node (Priv, Full);
12296 -- Note that the Etype of the full view is the same as the Etype of
12297 -- the partial view. In this fashion, the subtype has access to the
12298 -- correct view of the parent.
12300 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
12301 Set_Has_Unknown_Discriminants
12302 (Full, Has_Unknown_Discriminants (Full_Base));
12303 Set_First_Entity (Full, First_Entity (Full_Base));
12304 Set_Last_Entity (Full, Last_Entity (Full_Base));
12306 -- If the underlying base type is constrained, we know that the
12307 -- full view of the subtype is constrained as well (the converse
12308 -- is not necessarily true).
12310 if Is_Constrained (Full_Base) then
12311 Set_Is_Constrained (Full);
12315 Copy_Node (Full_Base, Full);
12317 -- The following subtlety with the Etype of the full view needs to be
12318 -- taken into account here. One could think that it must naturally be
12319 -- set to the base type of the full base:
12321 -- Set_Etype (Full, Base_Type (Full_Base));
12323 -- so that the full view becomes a subtype of the full base when the
12324 -- latter is a base type, which must for example happen when the full
12325 -- base is declared as derived type. That's also correct if the full
12326 -- base is declared as an array type, or a floating-point type, or a
12327 -- fixed-point type, or a signed integer type, as these declarations
12328 -- create an implicit base type and a first subtype so the Etype of
12329 -- the full views must be the implicit base type. But that's wrong
12330 -- if the full base is declared as an access type, or an enumeration
12331 -- type, or a modular integer type, as these declarations directly
12332 -- create a base type, i.e. with Etype pointing to itself. Moreover
12333 -- the full base being declared in the private part, i.e. when the
12334 -- views are swapped, the end result is that the Etype of the full
12335 -- base is set to its private view in this case and that we need to
12336 -- propagate this setting to the full view in order for the subtype
12337 -- to be compatible with the base type.
12339 if Is_Base_Type (Full_Base)
12340 and then (Is_Derived_Type (Full_Base)
12341 or else Ekind (Full_Base) in Array_Kind
12342 or else Ekind (Full_Base) in Fixed_Point_Kind
12343 or else Ekind (Full_Base) in Float_Kind
12344 or else Ekind (Full_Base) in Signed_Integer_Kind)
12346 Set_Etype (Full, Full_Base);
12349 Set_Chars (Full, Chars (Priv));
12350 Set_Sloc (Full, Sloc (Priv));
12351 Conditional_Delay (Full, Priv);
12354 Link_Entities (Full, Save_Next_Entity);
12355 Set_Homonym (Full, Save_Homonym);
12356 Set_Associated_Node_For_Itype (Full, Related_Nod);
12358 -- Set common attributes for all subtypes: kind, convention, etc.
12360 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12361 Set_Convention (Full, Convention (Full_Base));
12362 Set_Is_First_Subtype (Full, False);
12363 Set_Scope (Full, Scope (Priv));
12364 Set_Size_Info (Full, Full_Base);
12365 Set_RM_Size (Full, RM_Size (Full_Base));
12366 Set_Is_Itype (Full);
12368 -- A subtype of a private-type-without-discriminants, whose full-view
12369 -- has discriminants with default expressions, is not constrained.
12371 if not Has_Discriminants (Priv) then
12372 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12374 if Has_Discriminants (Full_Base) then
12375 Set_Discriminant_Constraint
12376 (Full, Discriminant_Constraint (Full_Base));
12378 -- The partial view may have been indefinite, the full view
12381 Set_Has_Unknown_Discriminants
12382 (Full, Has_Unknown_Discriminants (Full_Base));
12386 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12387 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12389 -- Freeze the private subtype entity if its parent is delayed, and not
12390 -- already frozen. We skip this processing if the type is an anonymous
12391 -- subtype of a record component, or is the corresponding record of a
12392 -- protected type, since these are processed when the enclosing type
12393 -- is frozen. If the parent type is declared in a nested package then
12394 -- the freezing of the private and full views also happens later.
12396 if not Is_Type (Scope (Full)) then
12398 and then In_Same_Source_Unit (Full, Full_Base)
12399 and then Scope (Full_Base) /= Scope (Full)
12401 Set_Has_Delayed_Freeze (Full);
12402 Set_Has_Delayed_Freeze (Priv);
12405 Set_Has_Delayed_Freeze (Full,
12406 Has_Delayed_Freeze (Full_Base)
12407 and then not Is_Frozen (Full_Base));
12411 Set_Freeze_Node (Full, Empty);
12412 Set_Is_Frozen (Full, False);
12414 if Has_Discriminants (Full) then
12415 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12416 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12418 if Has_Unknown_Discriminants (Full) then
12419 Set_Discriminant_Constraint (Full, No_Elist);
12423 if Ekind (Full_Base) = E_Record_Type
12424 and then Has_Discriminants (Full_Base)
12425 and then Has_Discriminants (Priv) -- might not, if errors
12426 and then not Has_Unknown_Discriminants (Priv)
12427 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12429 Create_Constrained_Components
12430 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12432 -- If the full base is itself derived from private, build a congruent
12433 -- subtype of its underlying full view, for use by the back end.
12435 elsif Is_Private_Type (Full_Base)
12436 and then Present (Underlying_Full_View (Full_Base))
12439 Underlying_Full_Base : constant Entity_Id
12440 := Underlying_Full_View (Full_Base);
12441 Underlying_Full : constant Entity_Id
12442 := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
12444 Set_Is_Itype (Underlying_Full);
12445 Set_Associated_Node_For_Itype (Underlying_Full, Related_Nod);
12446 Complete_Private_Subtype
12447 (Priv, Underlying_Full, Underlying_Full_Base, Related_Nod);
12448 Set_Underlying_Full_View (Full, Underlying_Full);
12449 Set_Is_Underlying_Full_View (Underlying_Full);
12452 elsif Is_Record_Type (Full_Base) then
12454 -- Show Full is simply a renaming of Full_Base
12456 Set_Cloned_Subtype (Full, Full_Base);
12458 -- Propagate predicates
12460 if Has_Predicates (Full_Base) then
12461 Set_Has_Predicates (Full);
12463 if Present (Predicate_Function (Full_Base))
12464 and then No (Predicate_Function (Full))
12466 Set_Predicate_Function (Full, Predicate_Function (Full_Base));
12471 -- It is unsafe to share the bounds of a scalar type, because the Itype
12472 -- is elaborated on demand, and if a bound is nonstatic, then different
12473 -- orders of elaboration in different units will lead to different
12474 -- external symbols.
12476 if Is_Scalar_Type (Full_Base) then
12477 Set_Scalar_Range (Full,
12478 Make_Range (Sloc (Related_Nod),
12480 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12482 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12484 -- This completion inherits the bounds of the full parent, but if
12485 -- the parent is an unconstrained floating point type, so is the
12488 if Is_Floating_Point_Type (Full_Base) then
12489 Set_Includes_Infinities
12490 (Scalar_Range (Full), Has_Infinities (Full_Base));
12494 -- ??? It seems that a lot of fields are missing that should be copied
12495 -- from Full_Base to Full. Here are some that are introduced in a
12496 -- non-disruptive way but a cleanup is necessary.
12498 if Is_Tagged_Type (Full_Base) then
12499 Set_Is_Tagged_Type (Full);
12500 Set_Direct_Primitive_Operations
12501 (Full, Direct_Primitive_Operations (Full_Base));
12502 Set_No_Tagged_Streams_Pragma
12503 (Full, No_Tagged_Streams_Pragma (Full_Base));
12505 -- Inherit class_wide type of full_base in case the partial view was
12506 -- not tagged. Otherwise it has already been created when the private
12507 -- subtype was analyzed.
12509 if No (Class_Wide_Type (Full)) then
12510 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12513 -- If this is a subtype of a protected or task type, constrain its
12514 -- corresponding record, unless this is a subtype without constraints,
12515 -- i.e. a simple renaming as with an actual subtype in an instance.
12517 elsif Is_Concurrent_Type (Full_Base) then
12518 if Has_Discriminants (Full)
12519 and then Present (Corresponding_Record_Type (Full_Base))
12521 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12523 Set_Corresponding_Record_Type (Full,
12524 Constrain_Corresponding_Record
12525 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12528 Set_Corresponding_Record_Type (Full,
12529 Corresponding_Record_Type (Full_Base));
12533 -- Link rep item chain, and also setting of Has_Predicates from private
12534 -- subtype to full subtype, since we will need these on the full subtype
12535 -- to create the predicate function. Note that the full subtype may
12536 -- already have rep items, inherited from the full view of the base
12537 -- type, so we must be sure not to overwrite these entries.
12542 Next_Item : Node_Id;
12543 Priv_Item : Node_Id;
12546 Item := First_Rep_Item (Full);
12547 Priv_Item := First_Rep_Item (Priv);
12549 -- If no existing rep items on full type, we can just link directly
12550 -- to the list of items on the private type, if any exist.. Same if
12551 -- the rep items are only those inherited from the base
12554 or else Nkind (Item) /= N_Aspect_Specification
12555 or else Entity (Item) = Full_Base)
12556 and then Present (First_Rep_Item (Priv))
12558 Set_First_Rep_Item (Full, Priv_Item);
12560 -- Otherwise, search to the end of items currently linked to the full
12561 -- subtype and append the private items to the end. However, if Priv
12562 -- and Full already have the same list of rep items, then the append
12563 -- is not done, as that would create a circularity.
12565 -- The partial view may have a predicate and the rep item lists of
12566 -- both views agree when inherited from the same ancestor. In that
12567 -- case, simply propagate the list from one view to the other.
12568 -- A more complex analysis needed here ???
12570 elsif Present (Priv_Item)
12571 and then Item = Next_Rep_Item (Priv_Item)
12573 Set_First_Rep_Item (Full, Priv_Item);
12575 elsif Item /= Priv_Item then
12578 Next_Item := Next_Rep_Item (Item);
12579 exit when No (Next_Item);
12582 -- If the private view has aspect specifications, the full view
12583 -- inherits them. Since these aspects may already have been
12584 -- attached to the full view during derivation, do not append
12585 -- them if already present.
12587 if Item = First_Rep_Item (Priv) then
12593 -- And link the private type items at the end of the chain
12596 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12601 -- Make sure Has_Predicates is set on full type if it is set on the
12602 -- private type. Note that it may already be set on the full type and
12603 -- if so, we don't want to unset it. Similarly, propagate information
12604 -- about delayed aspects, because the corresponding pragmas must be
12605 -- analyzed when one of the views is frozen. This last step is needed
12606 -- in particular when the full type is a scalar type for which an
12607 -- anonymous base type is constructed.
12609 -- The predicate functions are generated either at the freeze point
12610 -- of the type or at the end of the visible part, and we must avoid
12611 -- generating them twice.
12613 if Has_Predicates (Priv) then
12614 Set_Has_Predicates (Full);
12616 if Present (Predicate_Function (Priv))
12617 and then No (Predicate_Function (Full))
12619 Set_Predicate_Function (Full, Predicate_Function (Priv));
12623 if Has_Delayed_Aspects (Priv) then
12624 Set_Has_Delayed_Aspects (Full);
12626 end Complete_Private_Subtype;
12628 ----------------------------
12629 -- Constant_Redeclaration --
12630 ----------------------------
12632 procedure Constant_Redeclaration
12637 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12638 Obj_Def : constant Node_Id := Object_Definition (N);
12641 procedure Check_Possible_Deferred_Completion
12642 (Prev_Id : Entity_Id;
12643 Prev_Obj_Def : Node_Id;
12644 Curr_Obj_Def : Node_Id);
12645 -- Determine whether the two object definitions describe the partial
12646 -- and the full view of a constrained deferred constant. Generate
12647 -- a subtype for the full view and verify that it statically matches
12648 -- the subtype of the partial view.
12650 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12651 -- If deferred constant is an access type initialized with an allocator,
12652 -- check whether there is an illegal recursion in the definition,
12653 -- through a default value of some record subcomponent. This is normally
12654 -- detected when generating init procs, but requires this additional
12655 -- mechanism when expansion is disabled.
12657 ----------------------------------------
12658 -- Check_Possible_Deferred_Completion --
12659 ----------------------------------------
12661 procedure Check_Possible_Deferred_Completion
12662 (Prev_Id : Entity_Id;
12663 Prev_Obj_Def : Node_Id;
12664 Curr_Obj_Def : Node_Id)
12667 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12668 and then Present (Constraint (Prev_Obj_Def))
12669 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12670 and then Present (Constraint (Curr_Obj_Def))
12673 Loc : constant Source_Ptr := Sloc (N);
12674 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12675 Decl : constant Node_Id :=
12676 Make_Subtype_Declaration (Loc,
12677 Defining_Identifier => Def_Id,
12678 Subtype_Indication =>
12679 Relocate_Node (Curr_Obj_Def));
12682 Insert_Before_And_Analyze (N, Decl);
12683 Set_Etype (Id, Def_Id);
12685 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12686 Error_Msg_Sloc := Sloc (Prev_Id);
12687 Error_Msg_N ("subtype does not statically match deferred "
12688 & "declaration #", N);
12692 end Check_Possible_Deferred_Completion;
12694 ---------------------------------
12695 -- Check_Recursive_Declaration --
12696 ---------------------------------
12698 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12702 if Is_Record_Type (Typ) then
12703 Comp := First_Component (Typ);
12704 while Present (Comp) loop
12705 if Comes_From_Source (Comp) then
12706 if Present (Expression (Parent (Comp)))
12707 and then Is_Entity_Name (Expression (Parent (Comp)))
12708 and then Entity (Expression (Parent (Comp))) = Prev
12710 Error_Msg_Sloc := Sloc (Parent (Comp));
12712 ("illegal circularity with declaration for & #",
12716 elsif Is_Record_Type (Etype (Comp)) then
12717 Check_Recursive_Declaration (Etype (Comp));
12721 Next_Component (Comp);
12724 end Check_Recursive_Declaration;
12726 -- Start of processing for Constant_Redeclaration
12729 if Nkind (Parent (Prev)) = N_Object_Declaration then
12730 if Nkind (Object_Definition
12731 (Parent (Prev))) = N_Subtype_Indication
12733 -- Find type of new declaration. The constraints of the two
12734 -- views must match statically, but there is no point in
12735 -- creating an itype for the full view.
12737 if Nkind (Obj_Def) = N_Subtype_Indication then
12738 Find_Type (Subtype_Mark (Obj_Def));
12739 New_T := Entity (Subtype_Mark (Obj_Def));
12742 Find_Type (Obj_Def);
12743 New_T := Entity (Obj_Def);
12749 -- The full view may impose a constraint, even if the partial
12750 -- view does not, so construct the subtype.
12752 New_T := Find_Type_Of_Object (Obj_Def, N);
12757 -- Current declaration is illegal, diagnosed below in Enter_Name
12763 -- If previous full declaration or a renaming declaration exists, or if
12764 -- a homograph is present, let Enter_Name handle it, either with an
12765 -- error or with the removal of an overridden implicit subprogram.
12766 -- The previous one is a full declaration if it has an expression
12767 -- (which in the case of an aggregate is indicated by the Init flag).
12769 if Ekind (Prev) /= E_Constant
12770 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12771 or else Present (Expression (Parent (Prev)))
12772 or else Has_Init_Expression (Parent (Prev))
12773 or else Present (Full_View (Prev))
12777 -- Verify that types of both declarations match, or else that both types
12778 -- are anonymous access types whose designated subtypes statically match
12779 -- (as allowed in Ada 2005 by AI-385).
12781 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12783 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12784 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12785 or else Is_Access_Constant (Etype (New_T)) /=
12786 Is_Access_Constant (Etype (Prev))
12787 or else Can_Never_Be_Null (Etype (New_T)) /=
12788 Can_Never_Be_Null (Etype (Prev))
12789 or else Null_Exclusion_Present (Parent (Prev)) /=
12790 Null_Exclusion_Present (Parent (Id))
12791 or else not Subtypes_Statically_Match
12792 (Designated_Type (Etype (Prev)),
12793 Designated_Type (Etype (New_T))))
12795 Error_Msg_Sloc := Sloc (Prev);
12796 Error_Msg_N ("type does not match declaration#", N);
12797 Set_Full_View (Prev, Id);
12798 Set_Etype (Id, Any_Type);
12800 -- A deferred constant whose type is an anonymous array is always
12801 -- illegal (unless imported). A detailed error message might be
12802 -- helpful for Ada beginners.
12804 if Nkind (Object_Definition (Parent (Prev)))
12805 = N_Constrained_Array_Definition
12806 and then Nkind (Object_Definition (N))
12807 = N_Constrained_Array_Definition
12809 Error_Msg_N ("\each anonymous array is a distinct type", N);
12810 Error_Msg_N ("a deferred constant must have a named type",
12811 Object_Definition (Parent (Prev)));
12815 Null_Exclusion_Present (Parent (Prev))
12816 and then not Null_Exclusion_Present (N)
12818 Error_Msg_Sloc := Sloc (Prev);
12819 Error_Msg_N ("null-exclusion does not match declaration#", N);
12820 Set_Full_View (Prev, Id);
12821 Set_Etype (Id, Any_Type);
12823 -- If so, process the full constant declaration
12826 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12827 -- the deferred declaration is constrained, then the subtype defined
12828 -- by the subtype_indication in the full declaration shall match it
12831 Check_Possible_Deferred_Completion
12833 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12834 Curr_Obj_Def => Obj_Def);
12836 Set_Full_View (Prev, Id);
12837 Set_Is_Public (Id, Is_Public (Prev));
12838 Set_Is_Internal (Id);
12839 Append_Entity (Id, Current_Scope);
12841 -- Check ALIASED present if present before (RM 7.4(7))
12843 if Is_Aliased (Prev)
12844 and then not Aliased_Present (N)
12846 Error_Msg_Sloc := Sloc (Prev);
12847 Error_Msg_N ("ALIASED required (see declaration #)", N);
12850 -- Check that placement is in private part and that the incomplete
12851 -- declaration appeared in the visible part.
12853 if Ekind (Current_Scope) = E_Package
12854 and then not In_Private_Part (Current_Scope)
12856 Error_Msg_Sloc := Sloc (Prev);
12858 ("full constant for declaration # must be in private part", N);
12860 elsif Ekind (Current_Scope) = E_Package
12862 List_Containing (Parent (Prev)) /=
12863 Visible_Declarations (Package_Specification (Current_Scope))
12866 ("deferred constant must be declared in visible part",
12870 if Is_Access_Type (T)
12871 and then Nkind (Expression (N)) = N_Allocator
12873 Check_Recursive_Declaration (Designated_Type (T));
12876 -- A deferred constant is a visible entity. If type has invariants,
12877 -- verify that the initial value satisfies them. This is not done in
12878 -- GNATprove mode, as GNATprove handles invariant checks itself.
12880 if Has_Invariants (T)
12881 and then Present (Invariant_Procedure (T))
12882 and then not GNATprove_Mode
12885 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12888 end Constant_Redeclaration;
12890 ----------------------
12891 -- Constrain_Access --
12892 ----------------------
12894 procedure Constrain_Access
12895 (Def_Id : in out Entity_Id;
12897 Related_Nod : Node_Id)
12899 T : constant Entity_Id := Entity (Subtype_Mark (S));
12900 Desig_Type : constant Entity_Id := Designated_Type (T);
12901 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12902 Constraint_OK : Boolean := True;
12905 if Is_Array_Type (Desig_Type) then
12906 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12908 elsif (Is_Record_Type (Desig_Type)
12909 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12910 and then not Is_Constrained (Desig_Type)
12912 -- If this is a constrained access definition for a record
12913 -- component, we leave the type as an unconstrained access,
12914 -- and mark the component so that its actual type is built
12915 -- at a point of use (e.g., an assignment statement). This
12916 -- is handled in Sem_Util.Build_Actual_Subtype_Of_Component.
12918 if Desig_Type = Current_Scope
12919 and then No (Def_Id)
12923 (E_Void, Related_Nod, Scope_Id => Scope (Desig_Type));
12924 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12925 Def_Id := Entity (Subtype_Mark (S));
12927 -- We indicate that the component has a per-object constraint
12928 -- for treatment at a point of use, even though the constraint
12929 -- may be independent of discriminants of the enclosing type.
12931 if Nkind (Related_Nod) = N_Component_Declaration then
12932 Set_Has_Per_Object_Constraint
12933 (Defining_Identifier (Related_Nod));
12936 -- This call added to ensure that the constraint is analyzed
12937 -- (needed for a B test). Note that we still return early from
12938 -- this procedure to avoid recursive processing.
12940 Constrain_Discriminated_Type
12941 (Desig_Subtype, S, Related_Nod, For_Access => True);
12945 -- Enforce rule that the constraint is illegal if there is an
12946 -- unconstrained view of the designated type. This means that the
12947 -- partial view (either a private type declaration or a derivation
12948 -- from a private type) has no discriminants. (Defect Report
12949 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12951 -- Rule updated for Ada 2005: The private type is said to have
12952 -- a constrained partial view, given that objects of the type
12953 -- can be declared. Furthermore, the rule applies to all access
12954 -- types, unlike the rule concerning default discriminants (see
12957 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12958 and then Has_Private_Declaration (Desig_Type)
12959 and then In_Open_Scopes (Scope (Desig_Type))
12960 and then Has_Discriminants (Desig_Type)
12963 Pack : constant Node_Id :=
12964 Unit_Declaration_Node (Scope (Desig_Type));
12969 if Nkind (Pack) = N_Package_Declaration then
12970 Decls := Visible_Declarations (Specification (Pack));
12971 Decl := First (Decls);
12972 while Present (Decl) loop
12973 if (Nkind (Decl) = N_Private_Type_Declaration
12974 and then Chars (Defining_Identifier (Decl)) =
12975 Chars (Desig_Type))
12978 (Nkind (Decl) = N_Full_Type_Declaration
12980 Chars (Defining_Identifier (Decl)) =
12982 and then Is_Derived_Type (Desig_Type)
12984 Has_Private_Declaration (Etype (Desig_Type)))
12986 if No (Discriminant_Specifications (Decl)) then
12988 ("cannot constrain access type if designated "
12989 & "type has constrained partial view", S);
13001 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13002 For_Access => True);
13004 elsif Is_Concurrent_Type (Desig_Type)
13005 and then not Is_Constrained (Desig_Type)
13007 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13010 Error_Msg_N ("invalid constraint on access type", S);
13012 -- We simply ignore an invalid constraint
13014 Desig_Subtype := Desig_Type;
13015 Constraint_OK := False;
13018 if No (Def_Id) then
13019 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13021 Set_Ekind (Def_Id, E_Access_Subtype);
13024 if Constraint_OK then
13025 Set_Etype (Def_Id, Base_Type (T));
13027 if Is_Private_Type (Desig_Type) then
13028 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13031 Set_Etype (Def_Id, Any_Type);
13034 Set_Size_Info (Def_Id, T);
13035 Set_Is_Constrained (Def_Id, Constraint_OK);
13036 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13037 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13038 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13040 Conditional_Delay (Def_Id, T);
13042 -- AI-363 : Subtypes of general access types whose designated types have
13043 -- default discriminants are disallowed. In instances, the rule has to
13044 -- be checked against the actual, of which T is the subtype. In a
13045 -- generic body, the rule is checked assuming that the actual type has
13046 -- defaulted discriminants.
13048 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13049 if Ekind (Base_Type (T)) = E_General_Access_Type
13050 and then Has_Defaulted_Discriminants (Desig_Type)
13052 if Ada_Version < Ada_2005 then
13054 ("access subtype of general access type would not " &
13055 "be allowed in Ada 2005?y?", S);
13058 ("access subtype of general access type not allowed", S);
13061 Error_Msg_N ("\discriminants have defaults", S);
13063 elsif Is_Access_Type (T)
13064 and then Is_Generic_Type (Desig_Type)
13065 and then Has_Discriminants (Desig_Type)
13066 and then In_Package_Body (Current_Scope)
13068 if Ada_Version < Ada_2005 then
13070 ("access subtype would not be allowed in generic body "
13071 & "in Ada 2005?y?", S);
13074 ("access subtype not allowed in generic body", S);
13078 ("\designated type is a discriminated formal", S);
13081 end Constrain_Access;
13083 ---------------------
13084 -- Constrain_Array --
13085 ---------------------
13087 procedure Constrain_Array
13088 (Def_Id : in out Entity_Id;
13090 Related_Nod : Node_Id;
13091 Related_Id : Entity_Id;
13092 Suffix : Character)
13094 C : constant Node_Id := Constraint (SI);
13095 Number_Of_Constraints : Nat := 0;
13098 Constraint_OK : Boolean := True;
13101 T := Entity (Subtype_Mark (SI));
13103 if Is_Access_Type (T) then
13104 T := Designated_Type (T);
13107 -- If an index constraint follows a subtype mark in a subtype indication
13108 -- then the type or subtype denoted by the subtype mark must not already
13109 -- impose an index constraint. The subtype mark must denote either an
13110 -- unconstrained array type or an access type whose designated type
13111 -- is such an array type... (RM 3.6.1)
13113 if Is_Constrained (T) then
13114 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13115 Constraint_OK := False;
13118 S := First (Constraints (C));
13119 while Present (S) loop
13120 Number_Of_Constraints := Number_Of_Constraints + 1;
13124 -- In either case, the index constraint must provide a discrete
13125 -- range for each index of the array type and the type of each
13126 -- discrete range must be the same as that of the corresponding
13127 -- index. (RM 3.6.1)
13129 if Number_Of_Constraints /= Number_Dimensions (T) then
13130 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13131 Constraint_OK := False;
13134 S := First (Constraints (C));
13135 Index := First_Index (T);
13138 -- Apply constraints to each index type
13140 for J in 1 .. Number_Of_Constraints loop
13141 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13149 if No (Def_Id) then
13151 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13152 Set_Parent (Def_Id, Related_Nod);
13155 Set_Ekind (Def_Id, E_Array_Subtype);
13158 Set_Size_Info (Def_Id, (T));
13159 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13160 Set_Etype (Def_Id, Base_Type (T));
13162 if Constraint_OK then
13163 Set_First_Index (Def_Id, First (Constraints (C)));
13165 Set_First_Index (Def_Id, First_Index (T));
13168 Set_Is_Constrained (Def_Id, True);
13169 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13170 Set_Is_Independent (Def_Id, Is_Independent (T));
13171 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13173 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13174 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13176 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13177 -- We need to initialize the attribute because if Def_Id is previously
13178 -- analyzed through a limited_with clause, it will have the attributes
13179 -- of an incomplete type, one of which is an Elist that overlaps the
13180 -- Packed_Array_Impl_Type field.
13182 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13184 -- Build a freeze node if parent still needs one. Also make sure that
13185 -- the Depends_On_Private status is set because the subtype will need
13186 -- reprocessing at the time the base type does, and also we must set a
13187 -- conditional delay.
13189 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13190 Conditional_Delay (Def_Id, T);
13191 end Constrain_Array;
13193 ------------------------------
13194 -- Constrain_Component_Type --
13195 ------------------------------
13197 function Constrain_Component_Type
13199 Constrained_Typ : Entity_Id;
13200 Related_Node : Node_Id;
13202 Constraints : Elist_Id) return Entity_Id
13204 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13205 Compon_Type : constant Entity_Id := Etype (Comp);
13207 function Build_Constrained_Array_Type
13208 (Old_Type : Entity_Id) return Entity_Id;
13209 -- If Old_Type is an array type, one of whose indexes is constrained
13210 -- by a discriminant, build an Itype whose constraint replaces the
13211 -- discriminant with its value in the constraint.
13213 function Build_Constrained_Discriminated_Type
13214 (Old_Type : Entity_Id) return Entity_Id;
13215 -- Ditto for record components. Handle the case where the constraint
13216 -- is a conversion of the discriminant value, introduced during
13219 function Build_Constrained_Access_Type
13220 (Old_Type : Entity_Id) return Entity_Id;
13221 -- Ditto for access types. Makes use of previous two functions, to
13222 -- constrain designated type.
13224 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
13225 -- T is an array or discriminated type, C is a list of constraints
13226 -- that apply to T. This routine builds the constrained subtype.
13228 function Is_Discriminant (Expr : Node_Id) return Boolean;
13229 -- Returns True if Expr is a discriminant
13231 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
13232 -- Find the value of discriminant Discrim in Constraint
13234 -----------------------------------
13235 -- Build_Constrained_Access_Type --
13236 -----------------------------------
13238 function Build_Constrained_Access_Type
13239 (Old_Type : Entity_Id) return Entity_Id
13241 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13243 Desig_Subtype : Entity_Id;
13247 -- if the original access type was not embedded in the enclosing
13248 -- type definition, there is no need to produce a new access
13249 -- subtype. In fact every access type with an explicit constraint
13250 -- generates an itype whose scope is the enclosing record.
13252 if not Is_Type (Scope (Old_Type)) then
13255 elsif Is_Array_Type (Desig_Type) then
13256 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13258 elsif Has_Discriminants (Desig_Type) then
13260 -- This may be an access type to an enclosing record type for
13261 -- which we are constructing the constrained components. Return
13262 -- the enclosing record subtype. This is not always correct,
13263 -- but avoids infinite recursion. ???
13265 Desig_Subtype := Any_Type;
13267 for J in reverse 0 .. Scope_Stack.Last loop
13268 Scop := Scope_Stack.Table (J).Entity;
13271 and then Base_Type (Scop) = Base_Type (Desig_Type)
13273 Desig_Subtype := Scop;
13276 exit when not Is_Type (Scop);
13279 if Desig_Subtype = Any_Type then
13281 Build_Constrained_Discriminated_Type (Desig_Type);
13288 if Desig_Subtype /= Desig_Type then
13290 -- The Related_Node better be here or else we won't be able
13291 -- to attach new itypes to a node in the tree.
13293 pragma Assert (Present (Related_Node));
13295 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13297 Set_Etype (Itype, Base_Type (Old_Type));
13298 Set_Size_Info (Itype, (Old_Type));
13299 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13300 Set_Depends_On_Private (Itype, Has_Private_Component
13302 Set_Is_Access_Constant (Itype, Is_Access_Constant
13305 -- The new itype needs freezing when it depends on a not frozen
13306 -- type and the enclosing subtype needs freezing.
13308 if Has_Delayed_Freeze (Constrained_Typ)
13309 and then not Is_Frozen (Constrained_Typ)
13311 Conditional_Delay (Itype, Base_Type (Old_Type));
13319 end Build_Constrained_Access_Type;
13321 ----------------------------------
13322 -- Build_Constrained_Array_Type --
13323 ----------------------------------
13325 function Build_Constrained_Array_Type
13326 (Old_Type : Entity_Id) return Entity_Id
13330 Old_Index : Node_Id;
13331 Range_Node : Node_Id;
13332 Constr_List : List_Id;
13334 Need_To_Create_Itype : Boolean := False;
13337 Old_Index := First_Index (Old_Type);
13338 while Present (Old_Index) loop
13339 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13341 if Is_Discriminant (Lo_Expr)
13343 Is_Discriminant (Hi_Expr)
13345 Need_To_Create_Itype := True;
13348 Next_Index (Old_Index);
13351 if Need_To_Create_Itype then
13352 Constr_List := New_List;
13354 Old_Index := First_Index (Old_Type);
13355 while Present (Old_Index) loop
13356 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13358 if Is_Discriminant (Lo_Expr) then
13359 Lo_Expr := Get_Discr_Value (Lo_Expr);
13362 if Is_Discriminant (Hi_Expr) then
13363 Hi_Expr := Get_Discr_Value (Hi_Expr);
13368 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13370 Append (Range_Node, To => Constr_List);
13372 Next_Index (Old_Index);
13375 return Build_Subtype (Old_Type, Constr_List);
13380 end Build_Constrained_Array_Type;
13382 ------------------------------------------
13383 -- Build_Constrained_Discriminated_Type --
13384 ------------------------------------------
13386 function Build_Constrained_Discriminated_Type
13387 (Old_Type : Entity_Id) return Entity_Id
13390 Constr_List : List_Id;
13391 Old_Constraint : Elmt_Id;
13393 Need_To_Create_Itype : Boolean := False;
13396 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13397 while Present (Old_Constraint) loop
13398 Expr := Node (Old_Constraint);
13400 if Is_Discriminant (Expr) then
13401 Need_To_Create_Itype := True;
13403 -- After expansion of discriminated task types, the value
13404 -- of the discriminant may be converted to a run-time type
13405 -- for restricted run-times. Propagate the value of the
13406 -- discriminant as well, so that e.g. the secondary stack
13407 -- component has a static constraint. Necessary for LLVM.
13409 elsif Nkind (Expr) = N_Type_Conversion
13410 and then Is_Discriminant (Expression (Expr))
13412 Need_To_Create_Itype := True;
13415 Next_Elmt (Old_Constraint);
13418 if Need_To_Create_Itype then
13419 Constr_List := New_List;
13421 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13422 while Present (Old_Constraint) loop
13423 Expr := Node (Old_Constraint);
13425 if Is_Discriminant (Expr) then
13426 Expr := Get_Discr_Value (Expr);
13428 elsif Nkind (Expr) = N_Type_Conversion
13429 and then Is_Discriminant (Expression (Expr))
13431 Expr := New_Copy_Tree (Expr);
13432 Set_Expression (Expr, Get_Discr_Value (Expression (Expr)));
13435 Append (New_Copy_Tree (Expr), To => Constr_List);
13437 Next_Elmt (Old_Constraint);
13440 return Build_Subtype (Old_Type, Constr_List);
13445 end Build_Constrained_Discriminated_Type;
13447 -------------------
13448 -- Build_Subtype --
13449 -------------------
13451 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
13453 Subtyp_Decl : Node_Id;
13454 Def_Id : Entity_Id;
13455 Btyp : Entity_Id := Base_Type (T);
13458 -- The Related_Node better be here or else we won't be able to
13459 -- attach new itypes to a node in the tree.
13461 pragma Assert (Present (Related_Node));
13463 -- If the view of the component's type is incomplete or private
13464 -- with unknown discriminants, then the constraint must be applied
13465 -- to the full type.
13467 if Has_Unknown_Discriminants (Btyp)
13468 and then Present (Underlying_Type (Btyp))
13470 Btyp := Underlying_Type (Btyp);
13474 Make_Subtype_Indication (Loc,
13475 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13476 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13478 Def_Id := Create_Itype (Ekind (T), Related_Node);
13481 Make_Subtype_Declaration (Loc,
13482 Defining_Identifier => Def_Id,
13483 Subtype_Indication => Indic);
13485 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13487 -- Itypes must be analyzed with checks off (see package Itypes)
13489 Analyze (Subtyp_Decl, Suppress => All_Checks);
13491 if Is_Itype (Def_Id) and then Has_Predicates (T) then
13492 Inherit_Predicate_Flags (Def_Id, T);
13494 -- Indicate where the predicate function may be found
13496 if Is_Itype (T) then
13497 if Present (Predicate_Function (Def_Id)) then
13500 elsif Present (Predicate_Function (T)) then
13501 Set_Predicate_Function (Def_Id, Predicate_Function (T));
13504 Set_Predicated_Parent (Def_Id, Predicated_Parent (T));
13507 elsif No (Predicate_Function (Def_Id)) then
13508 Set_Predicated_Parent (Def_Id, T);
13515 ---------------------
13516 -- Get_Discr_Value --
13517 ---------------------
13519 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13524 -- The discriminant may be declared for the type, in which case we
13525 -- find it by iterating over the list of discriminants. If the
13526 -- discriminant is inherited from a parent type, it appears as the
13527 -- corresponding discriminant of the current type. This will be the
13528 -- case when constraining an inherited component whose constraint is
13529 -- given by a discriminant of the parent.
13531 D := First_Discriminant (Typ);
13532 E := First_Elmt (Constraints);
13534 while Present (D) loop
13535 if D = Entity (Discrim)
13536 or else D = CR_Discriminant (Entity (Discrim))
13537 or else Corresponding_Discriminant (D) = Entity (Discrim)
13542 Next_Discriminant (D);
13546 -- The Corresponding_Discriminant mechanism is incomplete, because
13547 -- the correspondence between new and old discriminants is not one
13548 -- to one: one new discriminant can constrain several old ones. In
13549 -- that case, scan sequentially the stored_constraint, the list of
13550 -- discriminants of the parents, and the constraints.
13552 -- Previous code checked for the present of the Stored_Constraint
13553 -- list for the derived type, but did not use it at all. Should it
13554 -- be present when the component is a discriminated task type?
13556 if Is_Derived_Type (Typ)
13557 and then Scope (Entity (Discrim)) = Etype (Typ)
13559 D := First_Discriminant (Etype (Typ));
13560 E := First_Elmt (Constraints);
13561 while Present (D) loop
13562 if D = Entity (Discrim) then
13566 Next_Discriminant (D);
13571 -- Something is wrong if we did not find the value
13573 raise Program_Error;
13574 end Get_Discr_Value;
13576 ---------------------
13577 -- Is_Discriminant --
13578 ---------------------
13580 function Is_Discriminant (Expr : Node_Id) return Boolean is
13581 Discrim_Scope : Entity_Id;
13584 if Denotes_Discriminant (Expr) then
13585 Discrim_Scope := Scope (Entity (Expr));
13587 -- Either we have a reference to one of Typ's discriminants,
13589 pragma Assert (Discrim_Scope = Typ
13591 -- or to the discriminants of the parent type, in the case
13592 -- of a derivation of a tagged type with variants.
13594 or else Discrim_Scope = Etype (Typ)
13595 or else Full_View (Discrim_Scope) = Etype (Typ)
13597 -- or same as above for the case where the discriminants
13598 -- were declared in Typ's private view.
13600 or else (Is_Private_Type (Discrim_Scope)
13601 and then Chars (Discrim_Scope) = Chars (Typ))
13603 -- or else we are deriving from the full view and the
13604 -- discriminant is declared in the private entity.
13606 or else (Is_Private_Type (Typ)
13607 and then Chars (Discrim_Scope) = Chars (Typ))
13609 -- Or we are constrained the corresponding record of a
13610 -- synchronized type that completes a private declaration.
13612 or else (Is_Concurrent_Record_Type (Typ)
13614 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13616 -- or we have a class-wide type, in which case make sure the
13617 -- discriminant found belongs to the root type.
13619 or else (Is_Class_Wide_Type (Typ)
13620 and then Etype (Typ) = Discrim_Scope));
13625 -- In all other cases we have something wrong
13628 end Is_Discriminant;
13630 -- Start of processing for Constrain_Component_Type
13633 if Nkind (Parent (Comp)) = N_Component_Declaration
13634 and then Comes_From_Source (Parent (Comp))
13635 and then Comes_From_Source
13636 (Subtype_Indication (Component_Definition (Parent (Comp))))
13639 (Subtype_Indication (Component_Definition (Parent (Comp))))
13641 return Compon_Type;
13643 elsif Is_Array_Type (Compon_Type) then
13644 return Build_Constrained_Array_Type (Compon_Type);
13646 elsif Has_Discriminants (Compon_Type) then
13647 return Build_Constrained_Discriminated_Type (Compon_Type);
13649 elsif Is_Access_Type (Compon_Type) then
13650 return Build_Constrained_Access_Type (Compon_Type);
13653 return Compon_Type;
13655 end Constrain_Component_Type;
13657 --------------------------
13658 -- Constrain_Concurrent --
13659 --------------------------
13661 -- For concurrent types, the associated record value type carries the same
13662 -- discriminants, so when we constrain a concurrent type, we must constrain
13663 -- the corresponding record type as well.
13665 procedure Constrain_Concurrent
13666 (Def_Id : in out Entity_Id;
13668 Related_Nod : Node_Id;
13669 Related_Id : Entity_Id;
13670 Suffix : Character)
13672 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13673 -- case of a private subtype (needed when only doing semantic analysis).
13675 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13679 if Is_Access_Type (T_Ent) then
13680 T_Ent := Designated_Type (T_Ent);
13683 T_Val := Corresponding_Record_Type (T_Ent);
13685 if Present (T_Val) then
13687 if No (Def_Id) then
13688 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13690 -- Elaborate itype now, as it may be used in a subsequent
13691 -- synchronized operation in another scope.
13693 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13694 Build_Itype_Reference (Def_Id, Related_Nod);
13698 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13699 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
13701 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13702 Set_Corresponding_Record_Type (Def_Id,
13703 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13706 -- If there is no associated record, expansion is disabled and this
13707 -- is a generic context. Create a subtype in any case, so that
13708 -- semantic analysis can proceed.
13710 if No (Def_Id) then
13711 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13714 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13716 end Constrain_Concurrent;
13718 ------------------------------------
13719 -- Constrain_Corresponding_Record --
13720 ------------------------------------
13722 function Constrain_Corresponding_Record
13723 (Prot_Subt : Entity_Id;
13724 Corr_Rec : Entity_Id;
13725 Related_Nod : Node_Id) return Entity_Id
13727 T_Sub : constant Entity_Id :=
13729 (Ekind => E_Record_Subtype,
13730 Related_Nod => Related_Nod,
13731 Related_Id => Corr_Rec,
13733 Suffix_Index => -1);
13736 Set_Etype (T_Sub, Corr_Rec);
13737 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13738 Set_Is_Tagged_Type (T_Sub, Is_Tagged_Type (Corr_Rec));
13739 Set_Is_Constrained (T_Sub, True);
13740 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13741 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13743 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13744 Set_Discriminant_Constraint
13745 (T_Sub, Discriminant_Constraint (Prot_Subt));
13746 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13747 Create_Constrained_Components
13748 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13751 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13753 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13754 Conditional_Delay (T_Sub, Corr_Rec);
13757 -- This is a component subtype: it will be frozen in the context of
13758 -- the enclosing record's init_proc, so that discriminant references
13759 -- are resolved to discriminals. (Note: we used to skip freezing
13760 -- altogether in that case, which caused errors downstream for
13761 -- components of a bit packed array type).
13763 Set_Has_Delayed_Freeze (T_Sub);
13767 end Constrain_Corresponding_Record;
13769 -----------------------
13770 -- Constrain_Decimal --
13771 -----------------------
13773 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13774 T : constant Entity_Id := Entity (Subtype_Mark (S));
13775 C : constant Node_Id := Constraint (S);
13776 Loc : constant Source_Ptr := Sloc (C);
13777 Range_Expr : Node_Id;
13778 Digits_Expr : Node_Id;
13783 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13785 if Nkind (C) = N_Range_Constraint then
13786 Range_Expr := Range_Expression (C);
13787 Digits_Val := Digits_Value (T);
13790 pragma Assert (Nkind (C) = N_Digits_Constraint);
13792 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13794 Digits_Expr := Digits_Expression (C);
13795 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13797 Check_Digits_Expression (Digits_Expr);
13798 Digits_Val := Expr_Value (Digits_Expr);
13800 if Digits_Val > Digits_Value (T) then
13802 ("digits expression is incompatible with subtype", C);
13803 Digits_Val := Digits_Value (T);
13806 if Present (Range_Constraint (C)) then
13807 Range_Expr := Range_Expression (Range_Constraint (C));
13809 Range_Expr := Empty;
13813 Set_Etype (Def_Id, Base_Type (T));
13814 Set_Size_Info (Def_Id, (T));
13815 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13816 Set_Delta_Value (Def_Id, Delta_Value (T));
13817 Set_Scale_Value (Def_Id, Scale_Value (T));
13818 Set_Small_Value (Def_Id, Small_Value (T));
13819 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13820 Set_Digits_Value (Def_Id, Digits_Val);
13822 -- Manufacture range from given digits value if no range present
13824 if No (Range_Expr) then
13825 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13829 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13831 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13834 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13835 Set_Discrete_RM_Size (Def_Id);
13837 -- Unconditionally delay the freeze, since we cannot set size
13838 -- information in all cases correctly until the freeze point.
13840 Set_Has_Delayed_Freeze (Def_Id);
13841 end Constrain_Decimal;
13843 ----------------------------------
13844 -- Constrain_Discriminated_Type --
13845 ----------------------------------
13847 procedure Constrain_Discriminated_Type
13848 (Def_Id : Entity_Id;
13850 Related_Nod : Node_Id;
13851 For_Access : Boolean := False)
13853 E : Entity_Id := Entity (Subtype_Mark (S));
13856 procedure Fixup_Bad_Constraint;
13857 -- Called after finding a bad constraint, and after having posted an
13858 -- appropriate error message. The goal is to leave type Def_Id in as
13859 -- reasonable state as possible.
13861 --------------------------
13862 -- Fixup_Bad_Constraint --
13863 --------------------------
13865 procedure Fixup_Bad_Constraint is
13867 -- Set a reasonable Ekind for the entity, including incomplete types.
13869 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13871 -- Set Etype to the known type, to reduce chances of cascaded errors
13873 Set_Etype (Def_Id, E);
13874 Set_Error_Posted (Def_Id);
13875 end Fixup_Bad_Constraint;
13880 Constr : Elist_Id := New_Elmt_List;
13882 -- Start of processing for Constrain_Discriminated_Type
13885 C := Constraint (S);
13887 -- A discriminant constraint is only allowed in a subtype indication,
13888 -- after a subtype mark. This subtype mark must denote either a type
13889 -- with discriminants, or an access type whose designated type is a
13890 -- type with discriminants. A discriminant constraint specifies the
13891 -- values of these discriminants (RM 3.7.2(5)).
13893 T := Base_Type (Entity (Subtype_Mark (S)));
13895 if Is_Access_Type (T) then
13896 T := Designated_Type (T);
13899 -- In an instance it may be necessary to retrieve the full view of a
13900 -- type with unknown discriminants, or a full view with defaulted
13901 -- discriminants. In other contexts the constraint is illegal.
13904 and then Is_Private_Type (T)
13905 and then Present (Full_View (T))
13907 (Has_Unknown_Discriminants (T)
13909 (not Has_Discriminants (T)
13910 and then Has_Discriminants (Full_View (T))
13911 and then Present (Discriminant_Default_Value
13912 (First_Discriminant (Full_View (T))))))
13914 T := Full_View (T);
13915 E := Full_View (E);
13918 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13919 -- generating an error for access-to-incomplete subtypes.
13921 if Ada_Version >= Ada_2005
13922 and then Ekind (T) = E_Incomplete_Type
13923 and then Nkind (Parent (S)) = N_Subtype_Declaration
13924 and then not Is_Itype (Def_Id)
13926 -- A little sanity check: emit an error message if the type has
13927 -- discriminants to begin with. Type T may be a regular incomplete
13928 -- type or imported via a limited with clause.
13930 if Has_Discriminants (T)
13931 or else (From_Limited_With (T)
13932 and then Present (Non_Limited_View (T))
13933 and then Nkind (Parent (Non_Limited_View (T))) =
13934 N_Full_Type_Declaration
13935 and then Present (Discriminant_Specifications
13936 (Parent (Non_Limited_View (T)))))
13939 ("(Ada 2005) incomplete subtype may not be constrained", C);
13941 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13944 Fixup_Bad_Constraint;
13947 -- Check that the type has visible discriminants. The type may be
13948 -- a private type with unknown discriminants whose full view has
13949 -- discriminants which are invisible.
13951 elsif not Has_Discriminants (T)
13953 (Has_Unknown_Discriminants (T)
13954 and then Is_Private_Type (T))
13956 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13957 Fixup_Bad_Constraint;
13960 elsif Is_Constrained (E)
13961 or else (Ekind (E) = E_Class_Wide_Subtype
13962 and then Present (Discriminant_Constraint (E)))
13964 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13965 Fixup_Bad_Constraint;
13969 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13970 -- applies to the base type.
13972 T := Base_Type (T);
13974 Constr := Build_Discriminant_Constraints (T, S);
13976 -- If the list returned was empty we had an error in building the
13977 -- discriminant constraint. We have also already signalled an error
13978 -- in the incomplete type case
13980 if Is_Empty_Elmt_List (Constr) then
13981 Fixup_Bad_Constraint;
13985 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
13986 end Constrain_Discriminated_Type;
13988 ---------------------------
13989 -- Constrain_Enumeration --
13990 ---------------------------
13992 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
13993 T : constant Entity_Id := Entity (Subtype_Mark (S));
13994 C : constant Node_Id := Constraint (S);
13997 Set_Ekind (Def_Id, E_Enumeration_Subtype);
13999 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
14001 Set_Etype (Def_Id, Base_Type (T));
14002 Set_Size_Info (Def_Id, (T));
14003 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14004 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14006 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14008 Set_Discrete_RM_Size (Def_Id);
14009 end Constrain_Enumeration;
14011 ----------------------
14012 -- Constrain_Float --
14013 ----------------------
14015 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
14016 T : constant Entity_Id := Entity (Subtype_Mark (S));
14022 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
14024 Set_Etype (Def_Id, Base_Type (T));
14025 Set_Size_Info (Def_Id, (T));
14026 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14028 -- Process the constraint
14030 C := Constraint (S);
14032 -- Digits constraint present
14034 if Nkind (C) = N_Digits_Constraint then
14036 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
14037 Check_Restriction (No_Obsolescent_Features, C);
14039 if Warn_On_Obsolescent_Feature then
14041 ("subtype digits constraint is an " &
14042 "obsolescent feature (RM J.3(8))?j?", C);
14045 D := Digits_Expression (C);
14046 Analyze_And_Resolve (D, Any_Integer);
14047 Check_Digits_Expression (D);
14048 Set_Digits_Value (Def_Id, Expr_Value (D));
14050 -- Check that digits value is in range. Obviously we can do this
14051 -- at compile time, but it is strictly a runtime check, and of
14052 -- course there is an ACVC test that checks this.
14054 if Digits_Value (Def_Id) > Digits_Value (T) then
14055 Error_Msg_Uint_1 := Digits_Value (T);
14056 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14058 Make_Raise_Constraint_Error (Sloc (D),
14059 Reason => CE_Range_Check_Failed);
14060 Insert_Action (Declaration_Node (Def_Id), Rais);
14063 C := Range_Constraint (C);
14065 -- No digits constraint present
14068 Set_Digits_Value (Def_Id, Digits_Value (T));
14071 -- Range constraint present
14073 if Nkind (C) = N_Range_Constraint then
14074 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14076 -- No range constraint present
14079 pragma Assert (No (C));
14080 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14083 Set_Is_Constrained (Def_Id);
14084 end Constrain_Float;
14086 ---------------------
14087 -- Constrain_Index --
14088 ---------------------
14090 procedure Constrain_Index
14093 Related_Nod : Node_Id;
14094 Related_Id : Entity_Id;
14095 Suffix : Character;
14096 Suffix_Index : Nat)
14098 Def_Id : Entity_Id;
14099 R : Node_Id := Empty;
14100 T : constant Entity_Id := Etype (Index);
14104 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14105 Set_Etype (Def_Id, Base_Type (T));
14107 if Nkind (S) = N_Range
14109 (Nkind (S) = N_Attribute_Reference
14110 and then Attribute_Name (S) = Name_Range)
14112 -- A Range attribute will be transformed into N_Range by Resolve
14118 Process_Range_Expr_In_Decl (R, T);
14120 if not Error_Posted (S)
14122 (Nkind (S) /= N_Range
14123 or else not Covers (T, (Etype (Low_Bound (S))))
14124 or else not Covers (T, (Etype (High_Bound (S)))))
14126 if Base_Type (T) /= Any_Type
14127 and then Etype (Low_Bound (S)) /= Any_Type
14128 and then Etype (High_Bound (S)) /= Any_Type
14130 Error_Msg_N ("range expected", S);
14134 elsif Nkind (S) = N_Subtype_Indication then
14136 -- The parser has verified that this is a discrete indication
14138 Resolve_Discrete_Subtype_Indication (S, T);
14139 Bad_Predicated_Subtype_Use
14140 ("subtype& has predicate, not allowed in index constraint",
14141 S, Entity (Subtype_Mark (S)));
14143 R := Range_Expression (Constraint (S));
14145 -- Capture values of bounds and generate temporaries for them if
14146 -- needed, since checks may cause duplication of the expressions
14147 -- which must not be reevaluated.
14149 -- The forced evaluation removes side effects from expressions, which
14150 -- should occur also in GNATprove mode. Otherwise, we end up with
14151 -- unexpected insertions of actions at places where this is not
14152 -- supposed to occur, e.g. on default parameters of a call.
14154 if Expander_Active or GNATprove_Mode then
14156 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14158 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14161 elsif Nkind (S) = N_Discriminant_Association then
14163 -- Syntactically valid in subtype indication
14165 Error_Msg_N ("invalid index constraint", S);
14166 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14169 -- Subtype_Mark case, no anonymous subtypes to construct
14174 if Is_Entity_Name (S) then
14175 if not Is_Type (Entity (S)) then
14176 Error_Msg_N ("expect subtype mark for index constraint", S);
14178 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14179 Wrong_Type (S, Base_Type (T));
14181 -- Check error of subtype with predicate in index constraint
14184 Bad_Predicated_Subtype_Use
14185 ("subtype& has predicate, not allowed in index constraint",
14192 Error_Msg_N ("invalid index constraint", S);
14193 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14198 -- Complete construction of the Itype
14200 if Is_Modular_Integer_Type (T) then
14201 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14203 elsif Is_Integer_Type (T) then
14204 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14207 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14208 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14209 Set_First_Literal (Def_Id, First_Literal (T));
14212 Set_Size_Info (Def_Id, (T));
14213 Set_RM_Size (Def_Id, RM_Size (T));
14214 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14216 Set_Scalar_Range (Def_Id, R);
14218 Set_Etype (S, Def_Id);
14219 Set_Discrete_RM_Size (Def_Id);
14220 end Constrain_Index;
14222 -----------------------
14223 -- Constrain_Integer --
14224 -----------------------
14226 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
14227 T : constant Entity_Id := Entity (Subtype_Mark (S));
14228 C : constant Node_Id := Constraint (S);
14231 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14233 if Is_Modular_Integer_Type (T) then
14234 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14236 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14239 Set_Etype (Def_Id, Base_Type (T));
14240 Set_Size_Info (Def_Id, (T));
14241 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14242 Set_Discrete_RM_Size (Def_Id);
14243 end Constrain_Integer;
14245 ------------------------------
14246 -- Constrain_Ordinary_Fixed --
14247 ------------------------------
14249 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
14250 T : constant Entity_Id := Entity (Subtype_Mark (S));
14256 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14257 Set_Etype (Def_Id, Base_Type (T));
14258 Set_Size_Info (Def_Id, (T));
14259 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14260 Set_Small_Value (Def_Id, Small_Value (T));
14262 -- Process the constraint
14264 C := Constraint (S);
14266 -- Delta constraint present
14268 if Nkind (C) = N_Delta_Constraint then
14270 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
14271 Check_Restriction (No_Obsolescent_Features, C);
14273 if Warn_On_Obsolescent_Feature then
14275 ("subtype delta constraint is an " &
14276 "obsolescent feature (RM J.3(7))?j?");
14279 D := Delta_Expression (C);
14280 Analyze_And_Resolve (D, Any_Real);
14281 Check_Delta_Expression (D);
14282 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14284 -- Check that delta value is in range. Obviously we can do this
14285 -- at compile time, but it is strictly a runtime check, and of
14286 -- course there is an ACVC test that checks this.
14288 if Delta_Value (Def_Id) < Delta_Value (T) then
14289 Error_Msg_N ("??delta value is too small", D);
14291 Make_Raise_Constraint_Error (Sloc (D),
14292 Reason => CE_Range_Check_Failed);
14293 Insert_Action (Declaration_Node (Def_Id), Rais);
14296 C := Range_Constraint (C);
14298 -- No delta constraint present
14301 Set_Delta_Value (Def_Id, Delta_Value (T));
14304 -- Range constraint present
14306 if Nkind (C) = N_Range_Constraint then
14307 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14309 -- No range constraint present
14312 pragma Assert (No (C));
14313 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14316 Set_Discrete_RM_Size (Def_Id);
14318 -- Unconditionally delay the freeze, since we cannot set size
14319 -- information in all cases correctly until the freeze point.
14321 Set_Has_Delayed_Freeze (Def_Id);
14322 end Constrain_Ordinary_Fixed;
14324 -----------------------
14325 -- Contain_Interface --
14326 -----------------------
14328 function Contain_Interface
14329 (Iface : Entity_Id;
14330 Ifaces : Elist_Id) return Boolean
14332 Iface_Elmt : Elmt_Id;
14335 if Present (Ifaces) then
14336 Iface_Elmt := First_Elmt (Ifaces);
14337 while Present (Iface_Elmt) loop
14338 if Node (Iface_Elmt) = Iface then
14342 Next_Elmt (Iface_Elmt);
14347 end Contain_Interface;
14349 ---------------------------
14350 -- Convert_Scalar_Bounds --
14351 ---------------------------
14353 procedure Convert_Scalar_Bounds
14355 Parent_Type : Entity_Id;
14356 Derived_Type : Entity_Id;
14359 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14366 -- Defend against previous errors
14368 if No (Scalar_Range (Derived_Type)) then
14369 Check_Error_Detected;
14373 Lo := Build_Scalar_Bound
14374 (Type_Low_Bound (Derived_Type),
14375 Parent_Type, Implicit_Base);
14377 Hi := Build_Scalar_Bound
14378 (Type_High_Bound (Derived_Type),
14379 Parent_Type, Implicit_Base);
14386 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14388 Set_Parent (Rng, N);
14389 Set_Scalar_Range (Derived_Type, Rng);
14391 -- Analyze the bounds
14393 Analyze_And_Resolve (Lo, Implicit_Base);
14394 Analyze_And_Resolve (Hi, Implicit_Base);
14396 -- Analyze the range itself, except that we do not analyze it if
14397 -- the bounds are real literals, and we have a fixed-point type.
14398 -- The reason for this is that we delay setting the bounds in this
14399 -- case till we know the final Small and Size values (see circuit
14400 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14402 if Is_Fixed_Point_Type (Parent_Type)
14403 and then Nkind (Lo) = N_Real_Literal
14404 and then Nkind (Hi) = N_Real_Literal
14408 -- Here we do the analysis of the range
14410 -- Note: we do this manually, since if we do a normal Analyze and
14411 -- Resolve call, there are problems with the conversions used for
14412 -- the derived type range.
14415 Set_Etype (Rng, Implicit_Base);
14416 Set_Analyzed (Rng, True);
14418 end Convert_Scalar_Bounds;
14420 -------------------
14421 -- Copy_And_Swap --
14422 -------------------
14424 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14426 -- Initialize new full declaration entity by copying the pertinent
14427 -- fields of the corresponding private declaration entity.
14429 -- We temporarily set Ekind to a value appropriate for a type to
14430 -- avoid assert failures in Einfo from checking for setting type
14431 -- attributes on something that is not a type. Ekind (Priv) is an
14432 -- appropriate choice, since it allowed the attributes to be set
14433 -- in the first place. This Ekind value will be modified later.
14435 Set_Ekind (Full, Ekind (Priv));
14437 -- Also set Etype temporarily to Any_Type, again, in the absence
14438 -- of errors, it will be properly reset, and if there are errors,
14439 -- then we want a value of Any_Type to remain.
14441 Set_Etype (Full, Any_Type);
14443 -- Now start copying attributes
14445 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14447 if Has_Discriminants (Full) then
14448 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14449 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14452 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14453 Set_Homonym (Full, Homonym (Priv));
14454 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14455 Set_Is_Public (Full, Is_Public (Priv));
14456 Set_Is_Pure (Full, Is_Pure (Priv));
14457 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14458 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14459 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14460 Set_Has_Pragma_Unreferenced_Objects
14461 (Full, Has_Pragma_Unreferenced_Objects
14464 Conditional_Delay (Full, Priv);
14466 if Is_Tagged_Type (Full) then
14467 Set_Direct_Primitive_Operations
14468 (Full, Direct_Primitive_Operations (Priv));
14469 Set_No_Tagged_Streams_Pragma
14470 (Full, No_Tagged_Streams_Pragma (Priv));
14472 if Is_Base_Type (Priv) then
14473 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14477 Set_Is_Volatile (Full, Is_Volatile (Priv));
14478 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14479 Set_Scope (Full, Scope (Priv));
14480 Set_Prev_Entity (Full, Prev_Entity (Priv));
14481 Set_Next_Entity (Full, Next_Entity (Priv));
14482 Set_First_Entity (Full, First_Entity (Priv));
14483 Set_Last_Entity (Full, Last_Entity (Priv));
14485 -- If access types have been recorded for later handling, keep them in
14486 -- the full view so that they get handled when the full view freeze
14487 -- node is expanded.
14489 if Present (Freeze_Node (Priv))
14490 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14492 Ensure_Freeze_Node (Full);
14493 Set_Access_Types_To_Process
14494 (Freeze_Node (Full),
14495 Access_Types_To_Process (Freeze_Node (Priv)));
14498 -- Swap the two entities. Now Private is the full type entity and Full
14499 -- is the private one. They will be swapped back at the end of the
14500 -- private part. This swapping ensures that the entity that is visible
14501 -- in the private part is the full declaration.
14503 Exchange_Entities (Priv, Full);
14504 Append_Entity (Full, Scope (Full));
14507 -------------------------------------
14508 -- Copy_Array_Base_Type_Attributes --
14509 -------------------------------------
14511 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14513 Set_Component_Alignment (T1, Component_Alignment (T2));
14514 Set_Component_Type (T1, Component_Type (T2));
14515 Set_Component_Size (T1, Component_Size (T2));
14516 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14517 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14518 Propagate_Concurrent_Flags (T1, T2);
14519 Set_Is_Packed (T1, Is_Packed (T2));
14520 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14521 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14522 Set_Has_Independent_Components (T1, Has_Independent_Components (T2));
14523 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14524 end Copy_Array_Base_Type_Attributes;
14526 -----------------------------------
14527 -- Copy_Array_Subtype_Attributes --
14528 -----------------------------------
14530 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14532 Set_Size_Info (T1, T2);
14534 Set_First_Index (T1, First_Index (T2));
14535 Set_Is_Aliased (T1, Is_Aliased (T2));
14536 Set_Is_Atomic (T1, Is_Atomic (T2));
14537 Set_Is_Independent (T1, Is_Independent (T2));
14538 Set_Is_Volatile (T1, Is_Volatile (T2));
14539 Set_Is_Volatile_Full_Access (T1, Is_Volatile_Full_Access (T2));
14540 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14541 Set_Is_Constrained (T1, Is_Constrained (T2));
14542 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14543 Inherit_Rep_Item_Chain (T1, T2);
14544 Set_Convention (T1, Convention (T2));
14545 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14546 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14547 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14548 end Copy_Array_Subtype_Attributes;
14550 -----------------------------------
14551 -- Create_Constrained_Components --
14552 -----------------------------------
14554 procedure Create_Constrained_Components
14556 Decl_Node : Node_Id;
14558 Constraints : Elist_Id)
14560 Loc : constant Source_Ptr := Sloc (Subt);
14561 Comp_List : constant Elist_Id := New_Elmt_List;
14562 Parent_Type : constant Entity_Id := Etype (Typ);
14563 Assoc_List : constant List_Id := New_List;
14564 Discr_Val : Elmt_Id;
14568 Is_Static : Boolean := True;
14570 procedure Collect_Fixed_Components (Typ : Entity_Id);
14571 -- Collect parent type components that do not appear in a variant part
14573 procedure Create_All_Components;
14574 -- Iterate over Comp_List to create the components of the subtype
14576 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14577 -- Creates a new component from Old_Compon, copying all the fields from
14578 -- it, including its Etype, inserts the new component in the Subt entity
14579 -- chain and returns the new component.
14581 function Is_Variant_Record (T : Entity_Id) return Boolean;
14582 -- If true, and discriminants are static, collect only components from
14583 -- variants selected by discriminant values.
14585 ------------------------------
14586 -- Collect_Fixed_Components --
14587 ------------------------------
14589 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14591 -- Build association list for discriminants, and find components of the
14592 -- variant part selected by the values of the discriminants.
14594 Old_C := First_Discriminant (Typ);
14595 Discr_Val := First_Elmt (Constraints);
14596 while Present (Old_C) loop
14597 Append_To (Assoc_List,
14598 Make_Component_Association (Loc,
14599 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14600 Expression => New_Copy (Node (Discr_Val))));
14602 Next_Elmt (Discr_Val);
14603 Next_Discriminant (Old_C);
14606 -- The tag and the possible parent component are unconditionally in
14609 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14610 Old_C := First_Component (Typ);
14611 while Present (Old_C) loop
14612 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14613 Append_Elmt (Old_C, Comp_List);
14616 Next_Component (Old_C);
14619 end Collect_Fixed_Components;
14621 ---------------------------
14622 -- Create_All_Components --
14623 ---------------------------
14625 procedure Create_All_Components is
14629 Comp := First_Elmt (Comp_List);
14630 while Present (Comp) loop
14631 Old_C := Node (Comp);
14632 New_C := Create_Component (Old_C);
14636 Constrain_Component_Type
14637 (Old_C, Subt, Decl_Node, Typ, Constraints));
14638 Set_Is_Public (New_C, Is_Public (Subt));
14642 end Create_All_Components;
14644 ----------------------
14645 -- Create_Component --
14646 ----------------------
14648 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14649 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14652 if Ekind (Old_Compon) = E_Discriminant
14653 and then Is_Completely_Hidden (Old_Compon)
14655 -- This is a shadow discriminant created for a discriminant of
14656 -- the parent type, which needs to be present in the subtype.
14657 -- Give the shadow discriminant an internal name that cannot
14658 -- conflict with that of visible components.
14660 Set_Chars (New_Compon, New_Internal_Name ('C'));
14663 -- Set the parent so we have a proper link for freezing etc. This is
14664 -- not a real parent pointer, since of course our parent does not own
14665 -- up to us and reference us, we are an illegitimate child of the
14666 -- original parent.
14668 Set_Parent (New_Compon, Parent (Old_Compon));
14670 -- We do not want this node marked as Comes_From_Source, since
14671 -- otherwise it would get first class status and a separate cross-
14672 -- reference line would be generated. Illegitimate children do not
14673 -- rate such recognition.
14675 Set_Comes_From_Source (New_Compon, False);
14677 -- But it is a real entity, and a birth certificate must be properly
14678 -- registered by entering it into the entity list, and setting its
14679 -- scope to the given subtype. This turns out to be useful for the
14680 -- LLVM code generator, but that scope is not used otherwise.
14682 Enter_Name (New_Compon);
14683 Set_Scope (New_Compon, Subt);
14686 end Create_Component;
14688 -----------------------
14689 -- Is_Variant_Record --
14690 -----------------------
14692 function Is_Variant_Record (T : Entity_Id) return Boolean is
14694 return Nkind (Parent (T)) = N_Full_Type_Declaration
14695 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14696 and then Present (Component_List (Type_Definition (Parent (T))))
14699 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14700 end Is_Variant_Record;
14702 -- Start of processing for Create_Constrained_Components
14705 pragma Assert (Subt /= Base_Type (Subt));
14706 pragma Assert (Typ = Base_Type (Typ));
14708 Set_First_Entity (Subt, Empty);
14709 Set_Last_Entity (Subt, Empty);
14711 -- Check whether constraint is fully static, in which case we can
14712 -- optimize the list of components.
14714 Discr_Val := First_Elmt (Constraints);
14715 while Present (Discr_Val) loop
14716 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14717 Is_Static := False;
14721 Next_Elmt (Discr_Val);
14724 Set_Has_Static_Discriminants (Subt, Is_Static);
14728 -- Inherit the discriminants of the parent type
14730 Add_Discriminants : declare
14736 Old_C := First_Discriminant (Typ);
14738 while Present (Old_C) loop
14739 Num_Disc := Num_Disc + 1;
14740 New_C := Create_Component (Old_C);
14741 Set_Is_Public (New_C, Is_Public (Subt));
14742 Next_Discriminant (Old_C);
14745 -- For an untagged derived subtype, the number of discriminants may
14746 -- be smaller than the number of inherited discriminants, because
14747 -- several of them may be renamed by a single new discriminant or
14748 -- constrained. In this case, add the hidden discriminants back into
14749 -- the subtype, because they need to be present if the optimizer of
14750 -- the GCC 4.x back-end decides to break apart assignments between
14751 -- objects using the parent view into member-wise assignments.
14755 if Is_Derived_Type (Typ)
14756 and then not Is_Tagged_Type (Typ)
14758 Old_C := First_Stored_Discriminant (Typ);
14760 while Present (Old_C) loop
14761 Num_Gird := Num_Gird + 1;
14762 Next_Stored_Discriminant (Old_C);
14766 if Num_Gird > Num_Disc then
14768 -- Find out multiple uses of new discriminants, and add hidden
14769 -- components for the extra renamed discriminants. We recognize
14770 -- multiple uses through the Corresponding_Discriminant of a
14771 -- new discriminant: if it constrains several old discriminants,
14772 -- this field points to the last one in the parent type. The
14773 -- stored discriminants of the derived type have the same name
14774 -- as those of the parent.
14778 New_Discr : Entity_Id;
14779 Old_Discr : Entity_Id;
14782 Constr := First_Elmt (Stored_Constraint (Typ));
14783 Old_Discr := First_Stored_Discriminant (Typ);
14784 while Present (Constr) loop
14785 if Is_Entity_Name (Node (Constr))
14786 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14788 New_Discr := Entity (Node (Constr));
14790 if Chars (Corresponding_Discriminant (New_Discr)) /=
14793 -- The new discriminant has been used to rename a
14794 -- subsequent old discriminant. Introduce a shadow
14795 -- component for the current old discriminant.
14797 New_C := Create_Component (Old_Discr);
14798 Set_Original_Record_Component (New_C, Old_Discr);
14802 -- The constraint has eliminated the old discriminant.
14803 -- Introduce a shadow component.
14805 New_C := Create_Component (Old_Discr);
14806 Set_Original_Record_Component (New_C, Old_Discr);
14809 Next_Elmt (Constr);
14810 Next_Stored_Discriminant (Old_Discr);
14814 end Add_Discriminants;
14817 and then Is_Variant_Record (Typ)
14819 Collect_Fixed_Components (Typ);
14821 Gather_Components (
14823 Component_List (Type_Definition (Parent (Typ))),
14824 Governed_By => Assoc_List,
14826 Report_Errors => Errors);
14827 pragma Assert (not Errors
14828 or else Serious_Errors_Detected > 0);
14830 Create_All_Components;
14832 -- If the subtype declaration is created for a tagged type derivation
14833 -- with constraints, we retrieve the record definition of the parent
14834 -- type to select the components of the proper variant.
14837 and then Is_Tagged_Type (Typ)
14838 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14840 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14841 and then Is_Variant_Record (Parent_Type)
14843 Collect_Fixed_Components (Typ);
14847 Component_List (Type_Definition (Parent (Parent_Type))),
14848 Governed_By => Assoc_List,
14850 Report_Errors => Errors);
14852 -- Note: previously there was a check at this point that no errors
14853 -- were detected. As a consequence of AI05-220 there may be an error
14854 -- if an inherited discriminant that controls a variant has a non-
14855 -- static constraint.
14857 -- If the tagged derivation has a type extension, collect all the
14858 -- new components therein.
14860 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14862 Old_C := First_Component (Typ);
14863 while Present (Old_C) loop
14864 if Original_Record_Component (Old_C) = Old_C
14865 and then Chars (Old_C) /= Name_uTag
14866 and then Chars (Old_C) /= Name_uParent
14868 Append_Elmt (Old_C, Comp_List);
14871 Next_Component (Old_C);
14875 Create_All_Components;
14878 -- If discriminants are not static, or if this is a multi-level type
14879 -- extension, we have to include all components of the parent type.
14881 Old_C := First_Component (Typ);
14882 while Present (Old_C) loop
14883 New_C := Create_Component (Old_C);
14887 Constrain_Component_Type
14888 (Old_C, Subt, Decl_Node, Typ, Constraints));
14889 Set_Is_Public (New_C, Is_Public (Subt));
14891 Next_Component (Old_C);
14896 end Create_Constrained_Components;
14898 ------------------------------------------
14899 -- Decimal_Fixed_Point_Type_Declaration --
14900 ------------------------------------------
14902 procedure Decimal_Fixed_Point_Type_Declaration
14906 Loc : constant Source_Ptr := Sloc (Def);
14907 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14908 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14909 Implicit_Base : Entity_Id;
14916 Check_SPARK_05_Restriction
14917 ("decimal fixed point type is not allowed", Def);
14918 Check_Restriction (No_Fixed_Point, Def);
14920 -- Create implicit base type
14923 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14924 Set_Etype (Implicit_Base, Implicit_Base);
14926 -- Analyze and process delta expression
14928 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14930 Check_Delta_Expression (Delta_Expr);
14931 Delta_Val := Expr_Value_R (Delta_Expr);
14933 -- Check delta is power of 10, and determine scale value from it
14939 Scale_Val := Uint_0;
14942 if Val < Ureal_1 then
14943 while Val < Ureal_1 loop
14944 Val := Val * Ureal_10;
14945 Scale_Val := Scale_Val + 1;
14948 if Scale_Val > 18 then
14949 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14950 Scale_Val := UI_From_Int (+18);
14954 while Val > Ureal_1 loop
14955 Val := Val / Ureal_10;
14956 Scale_Val := Scale_Val - 1;
14959 if Scale_Val < -18 then
14960 Error_Msg_N ("scale is less than minimum value of -18", Def);
14961 Scale_Val := UI_From_Int (-18);
14965 if Val /= Ureal_1 then
14966 Error_Msg_N ("delta expression must be a power of 10", Def);
14967 Delta_Val := Ureal_10 ** (-Scale_Val);
14971 -- Set delta, scale and small (small = delta for decimal type)
14973 Set_Delta_Value (Implicit_Base, Delta_Val);
14974 Set_Scale_Value (Implicit_Base, Scale_Val);
14975 Set_Small_Value (Implicit_Base, Delta_Val);
14977 -- Analyze and process digits expression
14979 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14980 Check_Digits_Expression (Digs_Expr);
14981 Digs_Val := Expr_Value (Digs_Expr);
14983 if Digs_Val > 18 then
14984 Digs_Val := UI_From_Int (+18);
14985 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
14988 Set_Digits_Value (Implicit_Base, Digs_Val);
14989 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
14991 -- Set range of base type from digits value for now. This will be
14992 -- expanded to represent the true underlying base range by Freeze.
14994 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
14996 -- Note: We leave size as zero for now, size will be set at freeze
14997 -- time. We have to do this for ordinary fixed-point, because the size
14998 -- depends on the specified small, and we might as well do the same for
14999 -- decimal fixed-point.
15001 pragma Assert (Esize (Implicit_Base) = Uint_0);
15003 -- If there are bounds given in the declaration use them as the
15004 -- bounds of the first named subtype.
15006 if Present (Real_Range_Specification (Def)) then
15008 RRS : constant Node_Id := Real_Range_Specification (Def);
15009 Low : constant Node_Id := Low_Bound (RRS);
15010 High : constant Node_Id := High_Bound (RRS);
15015 Analyze_And_Resolve (Low, Any_Real);
15016 Analyze_And_Resolve (High, Any_Real);
15017 Check_Real_Bound (Low);
15018 Check_Real_Bound (High);
15019 Low_Val := Expr_Value_R (Low);
15020 High_Val := Expr_Value_R (High);
15022 if Low_Val < (-Bound_Val) then
15024 ("range low bound too small for digits value", Low);
15025 Low_Val := -Bound_Val;
15028 if High_Val > Bound_Val then
15030 ("range high bound too large for digits value", High);
15031 High_Val := Bound_Val;
15034 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15037 -- If no explicit range, use range that corresponds to given
15038 -- digits value. This will end up as the final range for the
15042 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15045 -- Complete entity for first subtype. The inheritance of the rep item
15046 -- chain ensures that SPARK-related pragmas are not clobbered when the
15047 -- decimal fixed point type acts as a full view of a private type.
15049 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15050 Set_Etype (T, Implicit_Base);
15051 Set_Size_Info (T, Implicit_Base);
15052 Inherit_Rep_Item_Chain (T, Implicit_Base);
15053 Set_Digits_Value (T, Digs_Val);
15054 Set_Delta_Value (T, Delta_Val);
15055 Set_Small_Value (T, Delta_Val);
15056 Set_Scale_Value (T, Scale_Val);
15057 Set_Is_Constrained (T);
15058 end Decimal_Fixed_Point_Type_Declaration;
15060 -----------------------------------
15061 -- Derive_Progenitor_Subprograms --
15062 -----------------------------------
15064 procedure Derive_Progenitor_Subprograms
15065 (Parent_Type : Entity_Id;
15066 Tagged_Type : Entity_Id)
15071 Iface_Alias : Entity_Id;
15072 Iface_Elmt : Elmt_Id;
15073 Iface_Subp : Entity_Id;
15074 New_Subp : Entity_Id := Empty;
15075 Prim_Elmt : Elmt_Id;
15080 pragma Assert (Ada_Version >= Ada_2005
15081 and then Is_Record_Type (Tagged_Type)
15082 and then Is_Tagged_Type (Tagged_Type)
15083 and then Has_Interfaces (Tagged_Type));
15085 -- Step 1: Transfer to the full-view primitives associated with the
15086 -- partial-view that cover interface primitives. Conceptually this
15087 -- work should be done later by Process_Full_View; done here to
15088 -- simplify its implementation at later stages. It can be safely
15089 -- done here because interfaces must be visible in the partial and
15090 -- private view (RM 7.3(7.3/2)).
15092 -- Small optimization: This work is only required if the parent may
15093 -- have entities whose Alias attribute reference an interface primitive.
15094 -- Such a situation may occur if the parent is an abstract type and the
15095 -- primitive has not been yet overridden or if the parent is a generic
15096 -- formal type covering interfaces.
15098 -- If the tagged type is not abstract, it cannot have abstract
15099 -- primitives (the only entities in the list of primitives of
15100 -- non-abstract tagged types that can reference abstract primitives
15101 -- through its Alias attribute are the internal entities that have
15102 -- attribute Interface_Alias, and these entities are generated later
15103 -- by Add_Internal_Interface_Entities).
15105 if In_Private_Part (Current_Scope)
15106 and then (Is_Abstract_Type (Parent_Type)
15108 Is_Generic_Type (Parent_Type))
15110 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15111 while Present (Elmt) loop
15112 Subp := Node (Elmt);
15114 -- At this stage it is not possible to have entities in the list
15115 -- of primitives that have attribute Interface_Alias.
15117 pragma Assert (No (Interface_Alias (Subp)));
15119 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15121 if Is_Interface (Typ) then
15122 E := Find_Primitive_Covering_Interface
15123 (Tagged_Type => Tagged_Type,
15124 Iface_Prim => Subp);
15127 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15129 Replace_Elmt (Elmt, E);
15130 Remove_Homonym (Subp);
15138 -- Step 2: Add primitives of progenitors that are not implemented by
15139 -- parents of Tagged_Type.
15141 if Present (Interfaces (Base_Type (Tagged_Type))) then
15142 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15143 while Present (Iface_Elmt) loop
15144 Iface := Node (Iface_Elmt);
15146 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15147 while Present (Prim_Elmt) loop
15148 Iface_Subp := Node (Prim_Elmt);
15149 Iface_Alias := Ultimate_Alias (Iface_Subp);
15151 -- Exclude derivation of predefined primitives except those
15152 -- that come from source, or are inherited from one that comes
15153 -- from source. Required to catch declarations of equality
15154 -- operators of interfaces. For example:
15156 -- type Iface is interface;
15157 -- function "=" (Left, Right : Iface) return Boolean;
15159 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15160 or else Comes_From_Source (Iface_Alias)
15163 Find_Primitive_Covering_Interface
15164 (Tagged_Type => Tagged_Type,
15165 Iface_Prim => Iface_Subp);
15167 -- If not found we derive a new primitive leaving its alias
15168 -- attribute referencing the interface primitive.
15172 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15174 -- Ada 2012 (AI05-0197): If the covering primitive's name
15175 -- differs from the name of the interface primitive then it
15176 -- is a private primitive inherited from a parent type. In
15177 -- such case, given that Tagged_Type covers the interface,
15178 -- the inherited private primitive becomes visible. For such
15179 -- purpose we add a new entity that renames the inherited
15180 -- private primitive.
15182 elsif Chars (E) /= Chars (Iface_Subp) then
15183 pragma Assert (Has_Suffix (E, 'P'));
15185 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15186 Set_Alias (New_Subp, E);
15187 Set_Is_Abstract_Subprogram (New_Subp,
15188 Is_Abstract_Subprogram (E));
15190 -- Propagate to the full view interface entities associated
15191 -- with the partial view.
15193 elsif In_Private_Part (Current_Scope)
15194 and then Present (Alias (E))
15195 and then Alias (E) = Iface_Subp
15197 List_Containing (Parent (E)) /=
15198 Private_Declarations
15200 (Unit_Declaration_Node (Current_Scope)))
15202 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15206 Next_Elmt (Prim_Elmt);
15209 Next_Elmt (Iface_Elmt);
15212 end Derive_Progenitor_Subprograms;
15214 -----------------------
15215 -- Derive_Subprogram --
15216 -----------------------
15218 procedure Derive_Subprogram
15219 (New_Subp : out Entity_Id;
15220 Parent_Subp : Entity_Id;
15221 Derived_Type : Entity_Id;
15222 Parent_Type : Entity_Id;
15223 Actual_Subp : Entity_Id := Empty)
15225 Formal : Entity_Id;
15226 -- Formal parameter of parent primitive operation
15228 Formal_Of_Actual : Entity_Id;
15229 -- Formal parameter of actual operation, when the derivation is to
15230 -- create a renaming for a primitive operation of an actual in an
15233 New_Formal : Entity_Id;
15234 -- Formal of inherited operation
15236 Visible_Subp : Entity_Id := Parent_Subp;
15238 function Is_Private_Overriding return Boolean;
15239 -- If Subp is a private overriding of a visible operation, the inherited
15240 -- operation derives from the overridden op (even though its body is the
15241 -- overriding one) and the inherited operation is visible now. See
15242 -- sem_disp to see the full details of the handling of the overridden
15243 -- subprogram, which is removed from the list of primitive operations of
15244 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15245 -- and used to diagnose abstract operations that need overriding in the
15248 procedure Replace_Type (Id, New_Id : Entity_Id);
15249 -- When the type is an anonymous access type, create a new access type
15250 -- designating the derived type.
15252 procedure Set_Derived_Name;
15253 -- This procedure sets the appropriate Chars name for New_Subp. This
15254 -- is normally just a copy of the parent name. An exception arises for
15255 -- type support subprograms, where the name is changed to reflect the
15256 -- name of the derived type, e.g. if type foo is derived from type bar,
15257 -- then a procedure barDA is derived with a name fooDA.
15259 ---------------------------
15260 -- Is_Private_Overriding --
15261 ---------------------------
15263 function Is_Private_Overriding return Boolean is
15267 -- If the parent is not a dispatching operation there is no
15268 -- need to investigate overridings
15270 if not Is_Dispatching_Operation (Parent_Subp) then
15274 -- The visible operation that is overridden is a homonym of the
15275 -- parent subprogram. We scan the homonym chain to find the one
15276 -- whose alias is the subprogram we are deriving.
15278 Prev := Current_Entity (Parent_Subp);
15279 while Present (Prev) loop
15280 if Ekind (Prev) = Ekind (Parent_Subp)
15281 and then Alias (Prev) = Parent_Subp
15282 and then Scope (Parent_Subp) = Scope (Prev)
15283 and then not Is_Hidden (Prev)
15285 Visible_Subp := Prev;
15289 Prev := Homonym (Prev);
15293 end Is_Private_Overriding;
15299 procedure Replace_Type (Id, New_Id : Entity_Id) is
15300 Id_Type : constant Entity_Id := Etype (Id);
15301 Acc_Type : Entity_Id;
15302 Par : constant Node_Id := Parent (Derived_Type);
15305 -- When the type is an anonymous access type, create a new access
15306 -- type designating the derived type. This itype must be elaborated
15307 -- at the point of the derivation, not on subsequent calls that may
15308 -- be out of the proper scope for Gigi, so we insert a reference to
15309 -- it after the derivation.
15311 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15313 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15316 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15317 and then Present (Full_View (Desig_Typ))
15318 and then not Is_Private_Type (Parent_Type)
15320 Desig_Typ := Full_View (Desig_Typ);
15323 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15325 -- Ada 2005 (AI-251): Handle also derivations of abstract
15326 -- interface primitives.
15328 or else (Is_Interface (Desig_Typ)
15329 and then not Is_Class_Wide_Type (Desig_Typ))
15331 Acc_Type := New_Copy (Id_Type);
15332 Set_Etype (Acc_Type, Acc_Type);
15333 Set_Scope (Acc_Type, New_Subp);
15335 -- Set size of anonymous access type. If we have an access
15336 -- to an unconstrained array, this is a fat pointer, so it
15337 -- is sizes at twice addtress size.
15339 if Is_Array_Type (Desig_Typ)
15340 and then not Is_Constrained (Desig_Typ)
15342 Init_Size (Acc_Type, 2 * System_Address_Size);
15344 -- Other cases use a thin pointer
15347 Init_Size (Acc_Type, System_Address_Size);
15350 -- Set remaining characterstics of anonymous access type
15352 Init_Alignment (Acc_Type);
15353 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15355 Set_Etype (New_Id, Acc_Type);
15356 Set_Scope (New_Id, New_Subp);
15358 -- Create a reference to it
15360 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15363 Set_Etype (New_Id, Id_Type);
15367 -- In Ada2012, a formal may have an incomplete type but the type
15368 -- derivation that inherits the primitive follows the full view.
15370 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15372 (Ekind (Id_Type) = E_Record_Type_With_Private
15373 and then Present (Full_View (Id_Type))
15375 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15377 (Ada_Version >= Ada_2012
15378 and then Ekind (Id_Type) = E_Incomplete_Type
15379 and then Full_View (Id_Type) = Parent_Type)
15381 -- Constraint checks on formals are generated during expansion,
15382 -- based on the signature of the original subprogram. The bounds
15383 -- of the derived type are not relevant, and thus we can use
15384 -- the base type for the formals. However, the return type may be
15385 -- used in a context that requires that the proper static bounds
15386 -- be used (a case statement, for example) and for those cases
15387 -- we must use the derived type (first subtype), not its base.
15389 -- If the derived_type_definition has no constraints, we know that
15390 -- the derived type has the same constraints as the first subtype
15391 -- of the parent, and we can also use it rather than its base,
15392 -- which can lead to more efficient code.
15394 if Etype (Id) = Parent_Type then
15395 if Is_Scalar_Type (Parent_Type)
15397 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15399 Set_Etype (New_Id, Derived_Type);
15401 elsif Nkind (Par) = N_Full_Type_Declaration
15403 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15406 (Subtype_Indication (Type_Definition (Par)))
15408 Set_Etype (New_Id, Derived_Type);
15411 Set_Etype (New_Id, Base_Type (Derived_Type));
15415 Set_Etype (New_Id, Base_Type (Derived_Type));
15419 Set_Etype (New_Id, Etype (Id));
15423 ----------------------
15424 -- Set_Derived_Name --
15425 ----------------------
15427 procedure Set_Derived_Name is
15428 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15430 if Nm = TSS_Null then
15431 Set_Chars (New_Subp, Chars (Parent_Subp));
15433 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15435 end Set_Derived_Name;
15437 -- Start of processing for Derive_Subprogram
15440 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15441 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15443 -- Check whether the inherited subprogram is a private operation that
15444 -- should be inherited but not yet made visible. Such subprograms can
15445 -- become visible at a later point (e.g., the private part of a public
15446 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15447 -- following predicate is true, then this is not such a private
15448 -- operation and the subprogram simply inherits the name of the parent
15449 -- subprogram. Note the special check for the names of controlled
15450 -- operations, which are currently exempted from being inherited with
15451 -- a hidden name because they must be findable for generation of
15452 -- implicit run-time calls.
15454 if not Is_Hidden (Parent_Subp)
15455 or else Is_Internal (Parent_Subp)
15456 or else Is_Private_Overriding
15457 or else Is_Internal_Name (Chars (Parent_Subp))
15458 or else (Is_Controlled (Parent_Type)
15459 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15465 -- An inherited dispatching equality will be overridden by an internally
15466 -- generated one, or by an explicit one, so preserve its name and thus
15467 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15468 -- private operation it may become invisible if the full view has
15469 -- progenitors, and the dispatch table will be malformed.
15470 -- We check that the type is limited to handle the anomalous declaration
15471 -- of Limited_Controlled, which is derived from a non-limited type, and
15472 -- which is handled specially elsewhere as well.
15474 elsif Chars (Parent_Subp) = Name_Op_Eq
15475 and then Is_Dispatching_Operation (Parent_Subp)
15476 and then Etype (Parent_Subp) = Standard_Boolean
15477 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15479 Etype (First_Formal (Parent_Subp)) =
15480 Etype (Next_Formal (First_Formal (Parent_Subp)))
15484 -- If parent is hidden, this can be a regular derivation if the
15485 -- parent is immediately visible in a non-instantiating context,
15486 -- or if we are in the private part of an instance. This test
15487 -- should still be refined ???
15489 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15490 -- operation as a non-visible operation in cases where the parent
15491 -- subprogram might not be visible now, but was visible within the
15492 -- original generic, so it would be wrong to make the inherited
15493 -- subprogram non-visible now. (Not clear if this test is fully
15494 -- correct; are there any cases where we should declare the inherited
15495 -- operation as not visible to avoid it being overridden, e.g., when
15496 -- the parent type is a generic actual with private primitives ???)
15498 -- (they should be treated the same as other private inherited
15499 -- subprograms, but it's not clear how to do this cleanly). ???
15501 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15502 and then Is_Immediately_Visible (Parent_Subp)
15503 and then not In_Instance)
15504 or else In_Instance_Not_Visible
15508 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15509 -- overrides an interface primitive because interface primitives
15510 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15512 elsif Ada_Version >= Ada_2005
15513 and then Is_Dispatching_Operation (Parent_Subp)
15514 and then Present (Covered_Interface_Op (Parent_Subp))
15518 -- Otherwise, the type is inheriting a private operation, so enter it
15519 -- with a special name so it can't be overridden. See also below, where
15520 -- we check for this case, and if so avoid setting Requires_Overriding.
15523 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15526 Set_Parent (New_Subp, Parent (Derived_Type));
15528 if Present (Actual_Subp) then
15529 Replace_Type (Actual_Subp, New_Subp);
15531 Replace_Type (Parent_Subp, New_Subp);
15534 Conditional_Delay (New_Subp, Parent_Subp);
15536 -- If we are creating a renaming for a primitive operation of an
15537 -- actual of a generic derived type, we must examine the signature
15538 -- of the actual primitive, not that of the generic formal, which for
15539 -- example may be an interface. However the name and initial value
15540 -- of the inherited operation are those of the formal primitive.
15542 Formal := First_Formal (Parent_Subp);
15544 if Present (Actual_Subp) then
15545 Formal_Of_Actual := First_Formal (Actual_Subp);
15547 Formal_Of_Actual := Empty;
15550 while Present (Formal) loop
15551 New_Formal := New_Copy (Formal);
15553 -- Normally we do not go copying parents, but in the case of
15554 -- formals, we need to link up to the declaration (which is the
15555 -- parameter specification), and it is fine to link up to the
15556 -- original formal's parameter specification in this case.
15558 Set_Parent (New_Formal, Parent (Formal));
15559 Append_Entity (New_Formal, New_Subp);
15561 if Present (Formal_Of_Actual) then
15562 Replace_Type (Formal_Of_Actual, New_Formal);
15563 Next_Formal (Formal_Of_Actual);
15565 Replace_Type (Formal, New_Formal);
15568 Next_Formal (Formal);
15571 -- If this derivation corresponds to a tagged generic actual, then
15572 -- primitive operations rename those of the actual. Otherwise the
15573 -- primitive operations rename those of the parent type, If the parent
15574 -- renames an intrinsic operator, so does the new subprogram. We except
15575 -- concatenation, which is always properly typed, and does not get
15576 -- expanded as other intrinsic operations.
15578 if No (Actual_Subp) then
15579 if Is_Intrinsic_Subprogram (Parent_Subp) then
15580 Set_Is_Intrinsic_Subprogram (New_Subp);
15582 if Present (Alias (Parent_Subp))
15583 and then Chars (Parent_Subp) /= Name_Op_Concat
15585 Set_Alias (New_Subp, Alias (Parent_Subp));
15587 Set_Alias (New_Subp, Parent_Subp);
15591 Set_Alias (New_Subp, Parent_Subp);
15595 Set_Alias (New_Subp, Actual_Subp);
15598 -- Derived subprograms of a tagged type must inherit the convention
15599 -- of the parent subprogram (a requirement of AI-117). Derived
15600 -- subprograms of untagged types simply get convention Ada by default.
15602 -- If the derived type is a tagged generic formal type with unknown
15603 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15605 -- However, if the type is derived from a generic formal, the further
15606 -- inherited subprogram has the convention of the non-generic ancestor.
15607 -- Otherwise there would be no way to override the operation.
15608 -- (This is subject to forthcoming ARG discussions).
15610 if Is_Tagged_Type (Derived_Type) then
15611 if Is_Generic_Type (Derived_Type)
15612 and then Has_Unknown_Discriminants (Derived_Type)
15614 Set_Convention (New_Subp, Convention_Intrinsic);
15617 if Is_Generic_Type (Parent_Type)
15618 and then Has_Unknown_Discriminants (Parent_Type)
15620 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15622 Set_Convention (New_Subp, Convention (Parent_Subp));
15627 -- Predefined controlled operations retain their name even if the parent
15628 -- is hidden (see above), but they are not primitive operations if the
15629 -- ancestor is not visible, for example if the parent is a private
15630 -- extension completed with a controlled extension. Note that a full
15631 -- type that is controlled can break privacy: the flag Is_Controlled is
15632 -- set on both views of the type.
15634 if Is_Controlled (Parent_Type)
15635 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15638 and then Is_Hidden (Parent_Subp)
15639 and then not Is_Visibly_Controlled (Parent_Type)
15641 Set_Is_Hidden (New_Subp);
15644 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15645 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15647 if Ekind (Parent_Subp) = E_Procedure then
15648 Set_Is_Valued_Procedure
15649 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15651 Set_Has_Controlling_Result
15652 (New_Subp, Has_Controlling_Result (Parent_Subp));
15655 -- No_Return must be inherited properly. If this is overridden in the
15656 -- case of a dispatching operation, then a check is made in Sem_Disp
15657 -- that the overriding operation is also No_Return (no such check is
15658 -- required for the case of non-dispatching operation.
15660 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15662 -- A derived function with a controlling result is abstract. If the
15663 -- Derived_Type is a nonabstract formal generic derived type, then
15664 -- inherited operations are not abstract: the required check is done at
15665 -- instantiation time. If the derivation is for a generic actual, the
15666 -- function is not abstract unless the actual is.
15668 if Is_Generic_Type (Derived_Type)
15669 and then not Is_Abstract_Type (Derived_Type)
15673 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15674 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15676 -- A subprogram subject to pragma Extensions_Visible with value False
15677 -- requires overriding if the subprogram has at least one controlling
15678 -- OUT parameter (SPARK RM 6.1.7(6)).
15680 elsif Ada_Version >= Ada_2005
15681 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15682 or else (Is_Tagged_Type (Derived_Type)
15683 and then Etype (New_Subp) = Derived_Type
15684 and then not Is_Null_Extension (Derived_Type))
15685 or else (Is_Tagged_Type (Derived_Type)
15686 and then Ekind (Etype (New_Subp)) =
15687 E_Anonymous_Access_Type
15688 and then Designated_Type (Etype (New_Subp)) =
15690 and then not Is_Null_Extension (Derived_Type))
15691 or else (Comes_From_Source (Alias (New_Subp))
15692 and then Is_EVF_Procedure (Alias (New_Subp))))
15693 and then No (Actual_Subp)
15695 if not Is_Tagged_Type (Derived_Type)
15696 or else Is_Abstract_Type (Derived_Type)
15697 or else Is_Abstract_Subprogram (Alias (New_Subp))
15699 Set_Is_Abstract_Subprogram (New_Subp);
15701 -- If the Chars of the new subprogram is different from that of the
15702 -- parent's one, it means that we entered it with a special name so
15703 -- it can't be overridden (see above). In that case we had better not
15704 -- *require* it to be overridden. This is the case where the parent
15705 -- type inherited the operation privately, so there's no danger of
15706 -- dangling dispatching.
15708 elsif Chars (New_Subp) = Chars (Alias (New_Subp)) then
15709 Set_Requires_Overriding (New_Subp);
15712 elsif Ada_Version < Ada_2005
15713 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15714 or else (Is_Tagged_Type (Derived_Type)
15715 and then Etype (New_Subp) = Derived_Type
15716 and then No (Actual_Subp)))
15718 Set_Is_Abstract_Subprogram (New_Subp);
15720 -- AI05-0097 : an inherited operation that dispatches on result is
15721 -- abstract if the derived type is abstract, even if the parent type
15722 -- is concrete and the derived type is a null extension.
15724 elsif Has_Controlling_Result (Alias (New_Subp))
15725 and then Is_Abstract_Type (Etype (New_Subp))
15727 Set_Is_Abstract_Subprogram (New_Subp);
15729 -- Finally, if the parent type is abstract we must verify that all
15730 -- inherited operations are either non-abstract or overridden, or that
15731 -- the derived type itself is abstract (this check is performed at the
15732 -- end of a package declaration, in Check_Abstract_Overriding). A
15733 -- private overriding in the parent type will not be visible in the
15734 -- derivation if we are not in an inner package or in a child unit of
15735 -- the parent type, in which case the abstractness of the inherited
15736 -- operation is carried to the new subprogram.
15738 elsif Is_Abstract_Type (Parent_Type)
15739 and then not In_Open_Scopes (Scope (Parent_Type))
15740 and then Is_Private_Overriding
15741 and then Is_Abstract_Subprogram (Visible_Subp)
15743 if No (Actual_Subp) then
15744 Set_Alias (New_Subp, Visible_Subp);
15745 Set_Is_Abstract_Subprogram (New_Subp, True);
15748 -- If this is a derivation for an instance of a formal derived
15749 -- type, abstractness comes from the primitive operation of the
15750 -- actual, not from the operation inherited from the ancestor.
15752 Set_Is_Abstract_Subprogram
15753 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15757 New_Overloaded_Entity (New_Subp, Derived_Type);
15759 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15760 -- preconditions and the derived type is abstract, the derived operation
15761 -- is abstract as well if parent subprogram is not abstract or null.
15763 if Is_Abstract_Type (Derived_Type)
15764 and then Has_Non_Trivial_Precondition (Parent_Subp)
15765 and then Present (Interfaces (Derived_Type))
15768 -- Add useful attributes of subprogram before the freeze point,
15769 -- in case freezing is delayed or there are previous errors.
15771 Set_Is_Dispatching_Operation (New_Subp);
15774 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15777 if Present (Iface_Prim)
15778 and then Has_Non_Trivial_Precondition (Iface_Prim)
15780 Set_Is_Abstract_Subprogram (New_Subp);
15785 -- Check for case of a derived subprogram for the instantiation of a
15786 -- formal derived tagged type, if so mark the subprogram as dispatching
15787 -- and inherit the dispatching attributes of the actual subprogram. The
15788 -- derived subprogram is effectively renaming of the actual subprogram,
15789 -- so it needs to have the same attributes as the actual.
15791 if Present (Actual_Subp)
15792 and then Is_Dispatching_Operation (Actual_Subp)
15794 Set_Is_Dispatching_Operation (New_Subp);
15796 if Present (DTC_Entity (Actual_Subp)) then
15797 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15798 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15802 -- Indicate that a derived subprogram does not require a body and that
15803 -- it does not require processing of default expressions.
15805 Set_Has_Completion (New_Subp);
15806 Set_Default_Expressions_Processed (New_Subp);
15808 if Ekind (New_Subp) = E_Function then
15809 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15811 end Derive_Subprogram;
15813 ------------------------
15814 -- Derive_Subprograms --
15815 ------------------------
15817 procedure Derive_Subprograms
15818 (Parent_Type : Entity_Id;
15819 Derived_Type : Entity_Id;
15820 Generic_Actual : Entity_Id := Empty)
15822 Op_List : constant Elist_Id :=
15823 Collect_Primitive_Operations (Parent_Type);
15825 function Check_Derived_Type return Boolean;
15826 -- Check that all the entities derived from Parent_Type are found in
15827 -- the list of primitives of Derived_Type exactly in the same order.
15829 procedure Derive_Interface_Subprogram
15830 (New_Subp : out Entity_Id;
15832 Actual_Subp : Entity_Id);
15833 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15834 -- (which is an interface primitive). If Generic_Actual is present then
15835 -- Actual_Subp is the actual subprogram corresponding with the generic
15836 -- subprogram Subp.
15838 ------------------------
15839 -- Check_Derived_Type --
15840 ------------------------
15842 function Check_Derived_Type return Boolean is
15846 New_Subp : Entity_Id;
15851 -- Traverse list of entities in the current scope searching for
15852 -- an incomplete type whose full-view is derived type.
15854 E := First_Entity (Scope (Derived_Type));
15855 while Present (E) and then E /= Derived_Type loop
15856 if Ekind (E) = E_Incomplete_Type
15857 and then Present (Full_View (E))
15858 and then Full_View (E) = Derived_Type
15860 -- Disable this test if Derived_Type completes an incomplete
15861 -- type because in such case more primitives can be added
15862 -- later to the list of primitives of Derived_Type by routine
15863 -- Process_Incomplete_Dependents
15871 List := Collect_Primitive_Operations (Derived_Type);
15872 Elmt := First_Elmt (List);
15874 Op_Elmt := First_Elmt (Op_List);
15875 while Present (Op_Elmt) loop
15876 Subp := Node (Op_Elmt);
15877 New_Subp := Node (Elmt);
15879 -- At this early stage Derived_Type has no entities with attribute
15880 -- Interface_Alias. In addition, such primitives are always
15881 -- located at the end of the list of primitives of Parent_Type.
15882 -- Therefore, if found we can safely stop processing pending
15885 exit when Present (Interface_Alias (Subp));
15887 -- Handle hidden entities
15889 if not Is_Predefined_Dispatching_Operation (Subp)
15890 and then Is_Hidden (Subp)
15892 if Present (New_Subp)
15893 and then Primitive_Names_Match (Subp, New_Subp)
15899 if not Present (New_Subp)
15900 or else Ekind (Subp) /= Ekind (New_Subp)
15901 or else not Primitive_Names_Match (Subp, New_Subp)
15909 Next_Elmt (Op_Elmt);
15913 end Check_Derived_Type;
15915 ---------------------------------
15916 -- Derive_Interface_Subprogram --
15917 ---------------------------------
15919 procedure Derive_Interface_Subprogram
15920 (New_Subp : out Entity_Id;
15922 Actual_Subp : Entity_Id)
15924 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15925 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15928 pragma Assert (Is_Interface (Iface_Type));
15931 (New_Subp => New_Subp,
15932 Parent_Subp => Iface_Subp,
15933 Derived_Type => Derived_Type,
15934 Parent_Type => Iface_Type,
15935 Actual_Subp => Actual_Subp);
15937 -- Given that this new interface entity corresponds with a primitive
15938 -- of the parent that was not overridden we must leave it associated
15939 -- with its parent primitive to ensure that it will share the same
15940 -- dispatch table slot when overridden. We must set the Alias to Subp
15941 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15942 -- (in case we inherited Subp from Iface_Type via a nonabstract
15943 -- generic formal type).
15945 if No (Actual_Subp) then
15946 Set_Alias (New_Subp, Subp);
15949 T : Entity_Id := Find_Dispatching_Type (Subp);
15951 while Etype (T) /= T loop
15952 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15953 Set_Is_Abstract_Subprogram (New_Subp, False);
15961 -- For instantiations this is not needed since the previous call to
15962 -- Derive_Subprogram leaves the entity well decorated.
15965 pragma Assert (Alias (New_Subp) = Actual_Subp);
15968 end Derive_Interface_Subprogram;
15972 Alias_Subp : Entity_Id;
15973 Act_List : Elist_Id;
15974 Act_Elmt : Elmt_Id;
15975 Act_Subp : Entity_Id := Empty;
15977 Need_Search : Boolean := False;
15978 New_Subp : Entity_Id := Empty;
15979 Parent_Base : Entity_Id;
15982 -- Start of processing for Derive_Subprograms
15985 if Ekind (Parent_Type) = E_Record_Type_With_Private
15986 and then Has_Discriminants (Parent_Type)
15987 and then Present (Full_View (Parent_Type))
15989 Parent_Base := Full_View (Parent_Type);
15991 Parent_Base := Parent_Type;
15994 if Present (Generic_Actual) then
15995 Act_List := Collect_Primitive_Operations (Generic_Actual);
15996 Act_Elmt := First_Elmt (Act_List);
15998 Act_List := No_Elist;
15999 Act_Elmt := No_Elmt;
16002 -- Derive primitives inherited from the parent. Note that if the generic
16003 -- actual is present, this is not really a type derivation, it is a
16004 -- completion within an instance.
16006 -- Case 1: Derived_Type does not implement interfaces
16008 if not Is_Tagged_Type (Derived_Type)
16009 or else (not Has_Interfaces (Derived_Type)
16010 and then not (Present (Generic_Actual)
16011 and then Has_Interfaces (Generic_Actual)))
16013 Elmt := First_Elmt (Op_List);
16014 while Present (Elmt) loop
16015 Subp := Node (Elmt);
16017 -- Literals are derived earlier in the process of building the
16018 -- derived type, and are skipped here.
16020 if Ekind (Subp) = E_Enumeration_Literal then
16023 -- The actual is a direct descendant and the common primitive
16024 -- operations appear in the same order.
16026 -- If the generic parent type is present, the derived type is an
16027 -- instance of a formal derived type, and within the instance its
16028 -- operations are those of the actual. We derive from the formal
16029 -- type but make the inherited operations aliases of the
16030 -- corresponding operations of the actual.
16033 pragma Assert (No (Node (Act_Elmt))
16034 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
16037 (Subp, Node (Act_Elmt),
16038 Skip_Controlling_Formals => True)));
16041 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
16043 if Present (Act_Elmt) then
16044 Next_Elmt (Act_Elmt);
16051 -- Case 2: Derived_Type implements interfaces
16054 -- If the parent type has no predefined primitives we remove
16055 -- predefined primitives from the list of primitives of generic
16056 -- actual to simplify the complexity of this algorithm.
16058 if Present (Generic_Actual) then
16060 Has_Predefined_Primitives : Boolean := False;
16063 -- Check if the parent type has predefined primitives
16065 Elmt := First_Elmt (Op_List);
16066 while Present (Elmt) loop
16067 Subp := Node (Elmt);
16069 if Is_Predefined_Dispatching_Operation (Subp)
16070 and then not Comes_From_Source (Ultimate_Alias (Subp))
16072 Has_Predefined_Primitives := True;
16079 -- Remove predefined primitives of Generic_Actual. We must use
16080 -- an auxiliary list because in case of tagged types the value
16081 -- returned by Collect_Primitive_Operations is the value stored
16082 -- in its Primitive_Operations attribute (and we don't want to
16083 -- modify its current contents).
16085 if not Has_Predefined_Primitives then
16087 Aux_List : constant Elist_Id := New_Elmt_List;
16090 Elmt := First_Elmt (Act_List);
16091 while Present (Elmt) loop
16092 Subp := Node (Elmt);
16094 if not Is_Predefined_Dispatching_Operation (Subp)
16095 or else Comes_From_Source (Subp)
16097 Append_Elmt (Subp, Aux_List);
16103 Act_List := Aux_List;
16107 Act_Elmt := First_Elmt (Act_List);
16108 Act_Subp := Node (Act_Elmt);
16112 -- Stage 1: If the generic actual is not present we derive the
16113 -- primitives inherited from the parent type. If the generic parent
16114 -- type is present, the derived type is an instance of a formal
16115 -- derived type, and within the instance its operations are those of
16116 -- the actual. We derive from the formal type but make the inherited
16117 -- operations aliases of the corresponding operations of the actual.
16119 Elmt := First_Elmt (Op_List);
16120 while Present (Elmt) loop
16121 Subp := Node (Elmt);
16122 Alias_Subp := Ultimate_Alias (Subp);
16124 -- Do not derive internal entities of the parent that link
16125 -- interface primitives with their covering primitive. These
16126 -- entities will be added to this type when frozen.
16128 if Present (Interface_Alias (Subp)) then
16132 -- If the generic actual is present find the corresponding
16133 -- operation in the generic actual. If the parent type is a
16134 -- direct ancestor of the derived type then, even if it is an
16135 -- interface, the operations are inherited from the primary
16136 -- dispatch table and are in the proper order. If we detect here
16137 -- that primitives are not in the same order we traverse the list
16138 -- of primitive operations of the actual to find the one that
16139 -- implements the interface primitive.
16143 (Present (Generic_Actual)
16144 and then Present (Act_Subp)
16146 (Primitive_Names_Match (Subp, Act_Subp)
16148 Type_Conformant (Subp, Act_Subp,
16149 Skip_Controlling_Formals => True)))
16151 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16152 Use_Full_View => True));
16154 -- Remember that we need searching for all pending primitives
16156 Need_Search := True;
16158 -- Handle entities associated with interface primitives
16160 if Present (Alias_Subp)
16161 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16162 and then not Is_Predefined_Dispatching_Operation (Subp)
16164 -- Search for the primitive in the homonym chain
16167 Find_Primitive_Covering_Interface
16168 (Tagged_Type => Generic_Actual,
16169 Iface_Prim => Alias_Subp);
16171 -- Previous search may not locate primitives covering
16172 -- interfaces defined in generics units or instantiations.
16173 -- (it fails if the covering primitive has formals whose
16174 -- type is also defined in generics or instantiations).
16175 -- In such case we search in the list of primitives of the
16176 -- generic actual for the internal entity that links the
16177 -- interface primitive and the covering primitive.
16180 and then Is_Generic_Type (Parent_Type)
16182 -- This code has been designed to handle only generic
16183 -- formals that implement interfaces that are defined
16184 -- in a generic unit or instantiation. If this code is
16185 -- needed for other cases we must review it because
16186 -- (given that it relies on Original_Location to locate
16187 -- the primitive of Generic_Actual that covers the
16188 -- interface) it could leave linked through attribute
16189 -- Alias entities of unrelated instantiations).
16193 (Scope (Find_Dispatching_Type (Alias_Subp)))
16195 Instantiation_Depth
16196 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16199 Iface_Prim_Loc : constant Source_Ptr :=
16200 Original_Location (Sloc (Alias_Subp));
16207 First_Elmt (Primitive_Operations (Generic_Actual));
16209 Search : while Present (Elmt) loop
16210 Prim := Node (Elmt);
16212 if Present (Interface_Alias (Prim))
16213 and then Original_Location
16214 (Sloc (Interface_Alias (Prim))) =
16217 Act_Subp := Alias (Prim);
16226 pragma Assert (Present (Act_Subp)
16227 or else Is_Abstract_Type (Generic_Actual)
16228 or else Serious_Errors_Detected > 0);
16230 -- Handle predefined primitives plus the rest of user-defined
16234 Act_Elmt := First_Elmt (Act_List);
16235 while Present (Act_Elmt) loop
16236 Act_Subp := Node (Act_Elmt);
16238 exit when Primitive_Names_Match (Subp, Act_Subp)
16239 and then Type_Conformant
16241 Skip_Controlling_Formals => True)
16242 and then No (Interface_Alias (Act_Subp));
16244 Next_Elmt (Act_Elmt);
16247 if No (Act_Elmt) then
16253 -- Case 1: If the parent is a limited interface then it has the
16254 -- predefined primitives of synchronized interfaces. However, the
16255 -- actual type may be a non-limited type and hence it does not
16256 -- have such primitives.
16258 if Present (Generic_Actual)
16259 and then not Present (Act_Subp)
16260 and then Is_Limited_Interface (Parent_Base)
16261 and then Is_Predefined_Interface_Primitive (Subp)
16265 -- Case 2: Inherit entities associated with interfaces that were
16266 -- not covered by the parent type. We exclude here null interface
16267 -- primitives because they do not need special management.
16269 -- We also exclude interface operations that are renamings. If the
16270 -- subprogram is an explicit renaming of an interface primitive,
16271 -- it is a regular primitive operation, and the presence of its
16272 -- alias is not relevant: it has to be derived like any other
16275 elsif Present (Alias (Subp))
16276 and then Nkind (Unit_Declaration_Node (Subp)) /=
16277 N_Subprogram_Renaming_Declaration
16278 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16280 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16281 and then Null_Present (Parent (Alias_Subp)))
16283 -- If this is an abstract private type then we transfer the
16284 -- derivation of the interface primitive from the partial view
16285 -- to the full view. This is safe because all the interfaces
16286 -- must be visible in the partial view. Done to avoid adding
16287 -- a new interface derivation to the private part of the
16288 -- enclosing package; otherwise this new derivation would be
16289 -- decorated as hidden when the analysis of the enclosing
16290 -- package completes.
16292 if Is_Abstract_Type (Derived_Type)
16293 and then In_Private_Part (Current_Scope)
16294 and then Has_Private_Declaration (Derived_Type)
16297 Partial_View : Entity_Id;
16302 Partial_View := First_Entity (Current_Scope);
16304 exit when No (Partial_View)
16305 or else (Has_Private_Declaration (Partial_View)
16307 Full_View (Partial_View) = Derived_Type);
16309 Next_Entity (Partial_View);
16312 -- If the partial view was not found then the source code
16313 -- has errors and the derivation is not needed.
16315 if Present (Partial_View) then
16317 First_Elmt (Primitive_Operations (Partial_View));
16318 while Present (Elmt) loop
16319 Ent := Node (Elmt);
16321 if Present (Alias (Ent))
16322 and then Ultimate_Alias (Ent) = Alias (Subp)
16325 (Ent, Primitive_Operations (Derived_Type));
16332 -- If the interface primitive was not found in the
16333 -- partial view then this interface primitive was
16334 -- overridden. We add a derivation to activate in
16335 -- Derive_Progenitor_Subprograms the machinery to
16339 Derive_Interface_Subprogram
16340 (New_Subp => New_Subp,
16342 Actual_Subp => Act_Subp);
16347 Derive_Interface_Subprogram
16348 (New_Subp => New_Subp,
16350 Actual_Subp => Act_Subp);
16353 -- Case 3: Common derivation
16357 (New_Subp => New_Subp,
16358 Parent_Subp => Subp,
16359 Derived_Type => Derived_Type,
16360 Parent_Type => Parent_Base,
16361 Actual_Subp => Act_Subp);
16364 -- No need to update Act_Elm if we must search for the
16365 -- corresponding operation in the generic actual
16368 and then Present (Act_Elmt)
16370 Next_Elmt (Act_Elmt);
16371 Act_Subp := Node (Act_Elmt);
16378 -- Inherit additional operations from progenitors. If the derived
16379 -- type is a generic actual, there are not new primitive operations
16380 -- for the type because it has those of the actual, and therefore
16381 -- nothing needs to be done. The renamings generated above are not
16382 -- primitive operations, and their purpose is simply to make the
16383 -- proper operations visible within an instantiation.
16385 if No (Generic_Actual) then
16386 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16390 -- Final check: Direct descendants must have their primitives in the
16391 -- same order. We exclude from this test untagged types and instances
16392 -- of formal derived types. We skip this test if we have already
16393 -- reported serious errors in the sources.
16395 pragma Assert (not Is_Tagged_Type (Derived_Type)
16396 or else Present (Generic_Actual)
16397 or else Serious_Errors_Detected > 0
16398 or else Check_Derived_Type);
16399 end Derive_Subprograms;
16401 --------------------------------
16402 -- Derived_Standard_Character --
16403 --------------------------------
16405 procedure Derived_Standard_Character
16407 Parent_Type : Entity_Id;
16408 Derived_Type : Entity_Id)
16410 Loc : constant Source_Ptr := Sloc (N);
16411 Def : constant Node_Id := Type_Definition (N);
16412 Indic : constant Node_Id := Subtype_Indication (Def);
16413 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16414 Implicit_Base : constant Entity_Id :=
16416 (E_Enumeration_Type, N, Derived_Type, 'B');
16422 Discard_Node (Process_Subtype (Indic, N));
16424 Set_Etype (Implicit_Base, Parent_Base);
16425 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16426 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16428 Set_Is_Character_Type (Implicit_Base, True);
16429 Set_Has_Delayed_Freeze (Implicit_Base);
16431 -- The bounds of the implicit base are the bounds of the parent base.
16432 -- Note that their type is the parent base.
16434 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16435 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16437 Set_Scalar_Range (Implicit_Base,
16440 High_Bound => Hi));
16442 Conditional_Delay (Derived_Type, Parent_Type);
16444 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16445 Set_Etype (Derived_Type, Implicit_Base);
16446 Set_Size_Info (Derived_Type, Parent_Type);
16448 if Unknown_RM_Size (Derived_Type) then
16449 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16452 Set_Is_Character_Type (Derived_Type, True);
16454 if Nkind (Indic) /= N_Subtype_Indication then
16456 -- If no explicit constraint, the bounds are those
16457 -- of the parent type.
16459 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16460 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16461 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16464 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16466 -- Because the implicit base is used in the conversion of the bounds, we
16467 -- have to freeze it now. This is similar to what is done for numeric
16468 -- types, and it equally suspicious, but otherwise a nonstatic bound
16469 -- will have a reference to an unfrozen type, which is rejected by Gigi
16470 -- (???). This requires specific care for definition of stream
16471 -- attributes. For details, see comments at the end of
16472 -- Build_Derived_Numeric_Type.
16474 Freeze_Before (N, Implicit_Base);
16475 end Derived_Standard_Character;
16477 ------------------------------
16478 -- Derived_Type_Declaration --
16479 ------------------------------
16481 procedure Derived_Type_Declaration
16484 Is_Completion : Boolean)
16486 Parent_Type : Entity_Id;
16488 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16489 -- Check whether the parent type is a generic formal, or derives
16490 -- directly or indirectly from one.
16492 ------------------------
16493 -- Comes_From_Generic --
16494 ------------------------
16496 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16498 if Is_Generic_Type (Typ) then
16501 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16504 elsif Is_Private_Type (Typ)
16505 and then Present (Full_View (Typ))
16506 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16510 elsif Is_Generic_Actual_Type (Typ) then
16516 end Comes_From_Generic;
16520 Def : constant Node_Id := Type_Definition (N);
16521 Iface_Def : Node_Id;
16522 Indic : constant Node_Id := Subtype_Indication (Def);
16523 Extension : constant Node_Id := Record_Extension_Part (Def);
16524 Parent_Node : Node_Id;
16527 -- Start of processing for Derived_Type_Declaration
16530 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16533 and then Is_Tagged_Type (Parent_Type)
16536 Partial_View : constant Entity_Id :=
16537 Incomplete_Or_Partial_View (Parent_Type);
16540 -- If the partial view was not found then the parent type is not
16541 -- a private type. Otherwise check if the partial view is a tagged
16544 if Present (Partial_View)
16545 and then Is_Private_Type (Partial_View)
16546 and then not Is_Tagged_Type (Partial_View)
16549 ("cannot derive from & declared as untagged private "
16550 & "(SPARK RM 3.4(1))", N, Partial_View);
16555 -- Ada 2005 (AI-251): In case of interface derivation check that the
16556 -- parent is also an interface.
16558 if Interface_Present (Def) then
16559 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16561 if not Is_Interface (Parent_Type) then
16562 Diagnose_Interface (Indic, Parent_Type);
16565 Parent_Node := Parent (Base_Type (Parent_Type));
16566 Iface_Def := Type_Definition (Parent_Node);
16568 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16569 -- other limited interfaces.
16571 if Limited_Present (Def) then
16572 if Limited_Present (Iface_Def) then
16575 elsif Protected_Present (Iface_Def) then
16577 ("descendant of & must be declared as a protected "
16578 & "interface", N, Parent_Type);
16580 elsif Synchronized_Present (Iface_Def) then
16582 ("descendant of & must be declared as a synchronized "
16583 & "interface", N, Parent_Type);
16585 elsif Task_Present (Iface_Def) then
16587 ("descendant of & must be declared as a task interface",
16592 ("(Ada 2005) limited interface cannot inherit from "
16593 & "non-limited interface", Indic);
16596 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16597 -- from non-limited or limited interfaces.
16599 elsif not Protected_Present (Def)
16600 and then not Synchronized_Present (Def)
16601 and then not Task_Present (Def)
16603 if Limited_Present (Iface_Def) then
16606 elsif Protected_Present (Iface_Def) then
16608 ("descendant of & must be declared as a protected "
16609 & "interface", N, Parent_Type);
16611 elsif Synchronized_Present (Iface_Def) then
16613 ("descendant of & must be declared as a synchronized "
16614 & "interface", N, Parent_Type);
16616 elsif Task_Present (Iface_Def) then
16618 ("descendant of & must be declared as a task interface",
16627 if Is_Tagged_Type (Parent_Type)
16628 and then Is_Concurrent_Type (Parent_Type)
16629 and then not Is_Interface (Parent_Type)
16632 ("parent type of a record extension cannot be a synchronized "
16633 & "tagged type (RM 3.9.1 (3/1))", N);
16634 Set_Etype (T, Any_Type);
16638 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16641 if Is_Tagged_Type (Parent_Type)
16642 and then Is_Non_Empty_List (Interface_List (Def))
16649 Intf := First (Interface_List (Def));
16650 while Present (Intf) loop
16651 T := Find_Type_Of_Subtype_Indic (Intf);
16653 if not Is_Interface (T) then
16654 Diagnose_Interface (Intf, T);
16656 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16657 -- a limited type from having a nonlimited progenitor.
16659 elsif (Limited_Present (Def)
16660 or else (not Is_Interface (Parent_Type)
16661 and then Is_Limited_Type (Parent_Type)))
16662 and then not Is_Limited_Interface (T)
16665 ("progenitor interface& of limited type must be limited",
16674 if Parent_Type = Any_Type
16675 or else Etype (Parent_Type) = Any_Type
16676 or else (Is_Class_Wide_Type (Parent_Type)
16677 and then Etype (Parent_Type) = T)
16679 -- If Parent_Type is undefined or illegal, make new type into a
16680 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16681 -- errors. If this is a self-definition, emit error now.
16683 if T = Parent_Type or else T = Etype (Parent_Type) then
16684 Error_Msg_N ("type cannot be used in its own definition", Indic);
16687 Set_Ekind (T, Ekind (Parent_Type));
16688 Set_Etype (T, Any_Type);
16689 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16691 if Is_Tagged_Type (T)
16692 and then Is_Record_Type (T)
16694 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16700 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16701 -- an interface is special because the list of interfaces in the full
16702 -- view can be given in any order. For example:
16704 -- type A is interface;
16705 -- type B is interface and A;
16706 -- type D is new B with private;
16708 -- type D is new A and B with null record; -- 1 --
16710 -- In this case we perform the following transformation of -1-:
16712 -- type D is new B and A with null record;
16714 -- If the parent of the full-view covers the parent of the partial-view
16715 -- we have two possible cases:
16717 -- 1) They have the same parent
16718 -- 2) The parent of the full-view implements some further interfaces
16720 -- In both cases we do not need to perform the transformation. In the
16721 -- first case the source program is correct and the transformation is
16722 -- not needed; in the second case the source program does not fulfill
16723 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16726 -- This transformation not only simplifies the rest of the analysis of
16727 -- this type declaration but also simplifies the correct generation of
16728 -- the object layout to the expander.
16730 if In_Private_Part (Current_Scope)
16731 and then Is_Interface (Parent_Type)
16735 Partial_View : Entity_Id;
16736 Partial_View_Parent : Entity_Id;
16737 New_Iface : Node_Id;
16740 -- Look for the associated private type declaration
16742 Partial_View := Incomplete_Or_Partial_View (T);
16744 -- If the partial view was not found then the source code has
16745 -- errors and the transformation is not needed.
16747 if Present (Partial_View) then
16748 Partial_View_Parent := Etype (Partial_View);
16750 -- If the parent of the full-view covers the parent of the
16751 -- partial-view we have nothing else to do.
16753 if Interface_Present_In_Ancestor
16754 (Parent_Type, Partial_View_Parent)
16758 -- Traverse the list of interfaces of the full-view to look
16759 -- for the parent of the partial-view and perform the tree
16763 Iface := First (Interface_List (Def));
16764 while Present (Iface) loop
16765 if Etype (Iface) = Etype (Partial_View) then
16766 Rewrite (Subtype_Indication (Def),
16767 New_Copy (Subtype_Indication
16768 (Parent (Partial_View))));
16771 Make_Identifier (Sloc (N), Chars (Parent_Type));
16772 Append (New_Iface, Interface_List (Def));
16774 -- Analyze the transformed code
16776 Derived_Type_Declaration (T, N, Is_Completion);
16787 -- Only composite types other than array types are allowed to have
16790 if Present (Discriminant_Specifications (N)) then
16791 if (Is_Elementary_Type (Parent_Type)
16793 Is_Array_Type (Parent_Type))
16794 and then not Error_Posted (N)
16797 ("elementary or array type cannot have discriminants",
16798 Defining_Identifier (First (Discriminant_Specifications (N))));
16800 -- Unset Has_Discriminants flag to prevent cascaded errors, but
16801 -- only if we are not already processing a malformed syntax tree.
16803 if Is_Type (T) then
16804 Set_Has_Discriminants (T, False);
16807 -- The type is allowed to have discriminants
16810 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16814 -- In Ada 83, a derived type defined in a package specification cannot
16815 -- be used for further derivation until the end of its visible part.
16816 -- Note that derivation in the private part of the package is allowed.
16818 if Ada_Version = Ada_83
16819 and then Is_Derived_Type (Parent_Type)
16820 and then In_Visible_Part (Scope (Parent_Type))
16822 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16824 ("(Ada 83): premature use of type for derivation", Indic);
16828 -- Check for early use of incomplete or private type
16830 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16831 Error_Msg_N ("premature derivation of incomplete type", Indic);
16834 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16835 and then not Comes_From_Generic (Parent_Type))
16836 or else Has_Private_Component (Parent_Type)
16838 -- The ancestor type of a formal type can be incomplete, in which
16839 -- case only the operations of the partial view are available in the
16840 -- generic. Subsequent checks may be required when the full view is
16841 -- analyzed to verify that a derivation from a tagged type has an
16844 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16847 elsif No (Underlying_Type (Parent_Type))
16848 or else Has_Private_Component (Parent_Type)
16851 ("premature derivation of derived or private type", Indic);
16853 -- Flag the type itself as being in error, this prevents some
16854 -- nasty problems with subsequent uses of the malformed type.
16856 Set_Error_Posted (T);
16858 -- Check that within the immediate scope of an untagged partial
16859 -- view it's illegal to derive from the partial view if the
16860 -- full view is tagged. (7.3(7))
16862 -- We verify that the Parent_Type is a partial view by checking
16863 -- that it is not a Full_Type_Declaration (i.e. a private type or
16864 -- private extension declaration), to distinguish a partial view
16865 -- from a derivation from a private type which also appears as
16866 -- E_Private_Type. If the parent base type is not declared in an
16867 -- enclosing scope there is no need to check.
16869 elsif Present (Full_View (Parent_Type))
16870 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16871 and then not Is_Tagged_Type (Parent_Type)
16872 and then Is_Tagged_Type (Full_View (Parent_Type))
16873 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16876 ("premature derivation from type with tagged full view",
16881 -- Check that form of derivation is appropriate
16883 Taggd := Is_Tagged_Type (Parent_Type);
16885 -- Set the parent type to the class-wide type's specific type in this
16886 -- case to prevent cascading errors
16888 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16889 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16890 Set_Etype (T, Etype (Parent_Type));
16894 if Present (Extension) and then not Taggd then
16896 ("type derived from untagged type cannot have extension", Indic);
16898 elsif No (Extension) and then Taggd then
16900 -- If this declaration is within a private part (or body) of a
16901 -- generic instantiation then the derivation is allowed (the parent
16902 -- type can only appear tagged in this case if it's a generic actual
16903 -- type, since it would otherwise have been rejected in the analysis
16904 -- of the generic template).
16906 if not Is_Generic_Actual_Type (Parent_Type)
16907 or else In_Visible_Part (Scope (Parent_Type))
16909 if Is_Class_Wide_Type (Parent_Type) then
16911 ("parent type must not be a class-wide type", Indic);
16913 -- Use specific type to prevent cascaded errors.
16915 Parent_Type := Etype (Parent_Type);
16919 ("type derived from tagged type must have extension", Indic);
16924 -- AI-443: Synchronized formal derived types require a private
16925 -- extension. There is no point in checking the ancestor type or
16926 -- the progenitors since the construct is wrong to begin with.
16928 if Ada_Version >= Ada_2005
16929 and then Is_Generic_Type (T)
16930 and then Present (Original_Node (N))
16933 Decl : constant Node_Id := Original_Node (N);
16936 if Nkind (Decl) = N_Formal_Type_Declaration
16937 and then Nkind (Formal_Type_Definition (Decl)) =
16938 N_Formal_Derived_Type_Definition
16939 and then Synchronized_Present (Formal_Type_Definition (Decl))
16940 and then No (Extension)
16942 -- Avoid emitting a duplicate error message
16944 and then not Error_Posted (Indic)
16947 ("synchronized derived type must have extension", N);
16952 if Null_Exclusion_Present (Def)
16953 and then not Is_Access_Type (Parent_Type)
16955 Error_Msg_N ("null exclusion can only apply to an access type", N);
16958 -- Avoid deriving parent primitives of underlying record views
16960 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16961 Derive_Subps => not Is_Underlying_Record_View (T));
16963 -- AI-419: The parent type of an explicitly limited derived type must
16964 -- be a limited type or a limited interface.
16966 if Limited_Present (Def) then
16967 Set_Is_Limited_Record (T);
16969 if Is_Interface (T) then
16970 Set_Is_Limited_Interface (T);
16973 if not Is_Limited_Type (Parent_Type)
16975 (not Is_Interface (Parent_Type)
16976 or else not Is_Limited_Interface (Parent_Type))
16978 -- AI05-0096: a derivation in the private part of an instance is
16979 -- legal if the generic formal is untagged limited, and the actual
16982 if Is_Generic_Actual_Type (Parent_Type)
16983 and then In_Private_Part (Current_Scope)
16986 (Generic_Parent_Type (Parent (Parent_Type)))
16992 ("parent type& of limited type must be limited",
16998 -- In SPARK, there are no derived type definitions other than type
16999 -- extensions of tagged record types.
17001 if No (Extension) then
17002 Check_SPARK_05_Restriction
17003 ("derived type is not allowed", Original_Node (N));
17005 end Derived_Type_Declaration;
17007 ------------------------
17008 -- Diagnose_Interface --
17009 ------------------------
17011 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
17013 if not Is_Interface (E) and then E /= Any_Type then
17014 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
17016 end Diagnose_Interface;
17018 ----------------------------------
17019 -- Enumeration_Type_Declaration --
17020 ----------------------------------
17022 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17029 -- Create identifier node representing lower bound
17031 B_Node := New_Node (N_Identifier, Sloc (Def));
17032 L := First (Literals (Def));
17033 Set_Chars (B_Node, Chars (L));
17034 Set_Entity (B_Node, L);
17035 Set_Etype (B_Node, T);
17036 Set_Is_Static_Expression (B_Node, True);
17038 R_Node := New_Node (N_Range, Sloc (Def));
17039 Set_Low_Bound (R_Node, B_Node);
17041 Set_Ekind (T, E_Enumeration_Type);
17042 Set_First_Literal (T, L);
17044 Set_Is_Constrained (T);
17048 -- Loop through literals of enumeration type setting pos and rep values
17049 -- except that if the Ekind is already set, then it means the literal
17050 -- was already constructed (case of a derived type declaration and we
17051 -- should not disturb the Pos and Rep values.
17053 while Present (L) loop
17054 if Ekind (L) /= E_Enumeration_Literal then
17055 Set_Ekind (L, E_Enumeration_Literal);
17056 Set_Enumeration_Pos (L, Ev);
17057 Set_Enumeration_Rep (L, Ev);
17058 Set_Is_Known_Valid (L, True);
17062 New_Overloaded_Entity (L);
17063 Generate_Definition (L);
17064 Set_Convention (L, Convention_Intrinsic);
17066 -- Case of character literal
17068 if Nkind (L) = N_Defining_Character_Literal then
17069 Set_Is_Character_Type (T, True);
17071 -- Check violation of No_Wide_Characters
17073 if Restriction_Check_Required (No_Wide_Characters) then
17074 Get_Name_String (Chars (L));
17076 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17077 Check_Restriction (No_Wide_Characters, L);
17086 -- Now create a node representing upper bound
17088 B_Node := New_Node (N_Identifier, Sloc (Def));
17089 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17090 Set_Entity (B_Node, Last (Literals (Def)));
17091 Set_Etype (B_Node, T);
17092 Set_Is_Static_Expression (B_Node, True);
17094 Set_High_Bound (R_Node, B_Node);
17096 -- Initialize various fields of the type. Some of this information
17097 -- may be overwritten later through rep.clauses.
17099 Set_Scalar_Range (T, R_Node);
17100 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17101 Set_Enum_Esize (T);
17102 Set_Enum_Pos_To_Rep (T, Empty);
17104 -- Set Discard_Names if configuration pragma set, or if there is
17105 -- a parameterless pragma in the current declarative region
17107 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17108 Set_Discard_Names (T);
17111 -- Process end label if there is one
17113 if Present (Def) then
17114 Process_End_Label (Def, 'e', T);
17116 end Enumeration_Type_Declaration;
17118 ---------------------------------
17119 -- Expand_To_Stored_Constraint --
17120 ---------------------------------
17122 function Expand_To_Stored_Constraint
17124 Constraint : Elist_Id) return Elist_Id
17126 Explicitly_Discriminated_Type : Entity_Id;
17127 Expansion : Elist_Id;
17128 Discriminant : Entity_Id;
17130 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17131 -- Find the nearest type that actually specifies discriminants
17133 ---------------------------------
17134 -- Type_With_Explicit_Discrims --
17135 ---------------------------------
17137 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17138 Typ : constant E := Base_Type (Id);
17141 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17142 if Present (Full_View (Typ)) then
17143 return Type_With_Explicit_Discrims (Full_View (Typ));
17147 if Has_Discriminants (Typ) then
17152 if Etype (Typ) = Typ then
17154 elsif Has_Discriminants (Typ) then
17157 return Type_With_Explicit_Discrims (Etype (Typ));
17160 end Type_With_Explicit_Discrims;
17162 -- Start of processing for Expand_To_Stored_Constraint
17165 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17169 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17171 if No (Explicitly_Discriminated_Type) then
17175 Expansion := New_Elmt_List;
17178 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17179 while Present (Discriminant) loop
17181 (Get_Discriminant_Value
17182 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17184 Next_Stored_Discriminant (Discriminant);
17188 end Expand_To_Stored_Constraint;
17190 ---------------------------
17191 -- Find_Hidden_Interface --
17192 ---------------------------
17194 function Find_Hidden_Interface
17196 Dest : Elist_Id) return Entity_Id
17199 Iface_Elmt : Elmt_Id;
17202 if Present (Src) and then Present (Dest) then
17203 Iface_Elmt := First_Elmt (Src);
17204 while Present (Iface_Elmt) loop
17205 Iface := Node (Iface_Elmt);
17207 if Is_Interface (Iface)
17208 and then not Contain_Interface (Iface, Dest)
17213 Next_Elmt (Iface_Elmt);
17218 end Find_Hidden_Interface;
17220 --------------------
17221 -- Find_Type_Name --
17222 --------------------
17224 function Find_Type_Name (N : Node_Id) return Entity_Id is
17225 Id : constant Entity_Id := Defining_Identifier (N);
17226 New_Id : Entity_Id;
17228 Prev_Par : Node_Id;
17230 procedure Check_Duplicate_Aspects;
17231 -- Check that aspects specified in a completion have not been specified
17232 -- already in the partial view.
17234 procedure Tag_Mismatch;
17235 -- Diagnose a tagged partial view whose full view is untagged. We post
17236 -- the message on the full view, with a reference to the previous
17237 -- partial view. The partial view can be private or incomplete, and
17238 -- these are handled in a different manner, so we determine the position
17239 -- of the error message from the respective slocs of both.
17241 -----------------------------
17242 -- Check_Duplicate_Aspects --
17243 -----------------------------
17245 procedure Check_Duplicate_Aspects is
17246 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17247 -- Return the corresponding aspect of the partial view which matches
17248 -- the aspect id of Asp. Return Empty is no such aspect exists.
17250 -----------------------------
17251 -- Get_Partial_View_Aspect --
17252 -----------------------------
17254 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17255 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17256 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17257 Prev_Asp : Node_Id;
17260 if Present (Prev_Asps) then
17261 Prev_Asp := First (Prev_Asps);
17262 while Present (Prev_Asp) loop
17263 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17272 end Get_Partial_View_Aspect;
17276 Full_Asps : constant List_Id := Aspect_Specifications (N);
17277 Full_Asp : Node_Id;
17278 Part_Asp : Node_Id;
17280 -- Start of processing for Check_Duplicate_Aspects
17283 if Present (Full_Asps) then
17284 Full_Asp := First (Full_Asps);
17285 while Present (Full_Asp) loop
17286 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17288 -- An aspect and its class-wide counterpart are two distinct
17289 -- aspects and may apply to both views of an entity.
17291 if Present (Part_Asp)
17292 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17295 ("aspect already specified in private declaration",
17302 if Has_Discriminants (Prev)
17303 and then not Has_Unknown_Discriminants (Prev)
17304 and then Get_Aspect_Id (Full_Asp) =
17305 Aspect_Implicit_Dereference
17308 ("cannot specify aspect if partial view has known "
17309 & "discriminants", Full_Asp);
17315 end Check_Duplicate_Aspects;
17321 procedure Tag_Mismatch is
17323 if Sloc (Prev) < Sloc (Id) then
17324 if Ada_Version >= Ada_2012
17325 and then Nkind (N) = N_Private_Type_Declaration
17328 ("declaration of private } must be a tagged type ", Id, Prev);
17331 ("full declaration of } must be a tagged type ", Id, Prev);
17335 if Ada_Version >= Ada_2012
17336 and then Nkind (N) = N_Private_Type_Declaration
17339 ("declaration of private } must be a tagged type ", Prev, Id);
17342 ("full declaration of } must be a tagged type ", Prev, Id);
17347 -- Start of processing for Find_Type_Name
17350 -- Find incomplete declaration, if one was given
17352 Prev := Current_Entity_In_Scope (Id);
17354 -- New type declaration
17360 -- Previous declaration exists
17363 Prev_Par := Parent (Prev);
17365 -- Error if not incomplete/private case except if previous
17366 -- declaration is implicit, etc. Enter_Name will emit error if
17369 if not Is_Incomplete_Or_Private_Type (Prev) then
17373 -- Check invalid completion of private or incomplete type
17375 elsif not Nkind_In (N, N_Full_Type_Declaration,
17376 N_Task_Type_Declaration,
17377 N_Protected_Type_Declaration)
17379 (Ada_Version < Ada_2012
17380 or else not Is_Incomplete_Type (Prev)
17381 or else not Nkind_In (N, N_Private_Type_Declaration,
17382 N_Private_Extension_Declaration))
17384 -- Completion must be a full type declarations (RM 7.3(4))
17386 Error_Msg_Sloc := Sloc (Prev);
17387 Error_Msg_NE ("invalid completion of }", Id, Prev);
17389 -- Set scope of Id to avoid cascaded errors. Entity is never
17390 -- examined again, except when saving globals in generics.
17392 Set_Scope (Id, Current_Scope);
17395 -- If this is a repeated incomplete declaration, no further
17396 -- checks are possible.
17398 if Nkind (N) = N_Incomplete_Type_Declaration then
17402 -- Case of full declaration of incomplete type
17404 elsif Ekind (Prev) = E_Incomplete_Type
17405 and then (Ada_Version < Ada_2012
17406 or else No (Full_View (Prev))
17407 or else not Is_Private_Type (Full_View (Prev)))
17409 -- Indicate that the incomplete declaration has a matching full
17410 -- declaration. The defining occurrence of the incomplete
17411 -- declaration remains the visible one, and the procedure
17412 -- Get_Full_View dereferences it whenever the type is used.
17414 if Present (Full_View (Prev)) then
17415 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17418 Set_Full_View (Prev, Id);
17419 Append_Entity (Id, Current_Scope);
17420 Set_Is_Public (Id, Is_Public (Prev));
17421 Set_Is_Internal (Id);
17424 -- If the incomplete view is tagged, a class_wide type has been
17425 -- created already. Use it for the private type as well, in order
17426 -- to prevent multiple incompatible class-wide types that may be
17427 -- created for self-referential anonymous access components.
17429 if Is_Tagged_Type (Prev)
17430 and then Present (Class_Wide_Type (Prev))
17432 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17433 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17435 -- Type of the class-wide type is the current Id. Previously
17436 -- this was not done for private declarations because of order-
17437 -- of-elaboration issues in the back end, but gigi now handles
17440 Set_Etype (Class_Wide_Type (Id), Id);
17443 -- Case of full declaration of private type
17446 -- If the private type was a completion of an incomplete type then
17447 -- update Prev to reference the private type
17449 if Ada_Version >= Ada_2012
17450 and then Ekind (Prev) = E_Incomplete_Type
17451 and then Present (Full_View (Prev))
17452 and then Is_Private_Type (Full_View (Prev))
17454 Prev := Full_View (Prev);
17455 Prev_Par := Parent (Prev);
17458 if Nkind (N) = N_Full_Type_Declaration
17460 (Type_Definition (N), N_Record_Definition,
17461 N_Derived_Type_Definition)
17462 and then Interface_Present (Type_Definition (N))
17465 ("completion of private type cannot be an interface", N);
17468 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17469 if Etype (Prev) /= Prev then
17471 -- Prev is a private subtype or a derived type, and needs
17474 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17477 elsif Ekind (Prev) = E_Private_Type
17478 and then Nkind_In (N, N_Task_Type_Declaration,
17479 N_Protected_Type_Declaration)
17482 ("completion of nonlimited type cannot be limited", N);
17484 elsif Ekind (Prev) = E_Record_Type_With_Private
17485 and then Nkind_In (N, N_Task_Type_Declaration,
17486 N_Protected_Type_Declaration)
17488 if not Is_Limited_Record (Prev) then
17490 ("completion of nonlimited type cannot be limited", N);
17492 elsif No (Interface_List (N)) then
17494 ("completion of tagged private type must be tagged",
17499 -- Ada 2005 (AI-251): Private extension declaration of a task
17500 -- type or a protected type. This case arises when covering
17501 -- interface types.
17503 elsif Nkind_In (N, N_Task_Type_Declaration,
17504 N_Protected_Type_Declaration)
17508 elsif Nkind (N) /= N_Full_Type_Declaration
17509 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17512 ("full view of private extension must be an extension", N);
17514 elsif not (Abstract_Present (Parent (Prev)))
17515 and then Abstract_Present (Type_Definition (N))
17518 ("full view of non-abstract extension cannot be abstract", N);
17521 if not In_Private_Part (Current_Scope) then
17523 ("declaration of full view must appear in private part", N);
17526 if Ada_Version >= Ada_2012 then
17527 Check_Duplicate_Aspects;
17530 Copy_And_Swap (Prev, Id);
17531 Set_Has_Private_Declaration (Prev);
17532 Set_Has_Private_Declaration (Id);
17534 -- AI12-0133: Indicate whether we have a partial view with
17535 -- unknown discriminants, in which case initialization of objects
17536 -- of the type do not receive an invariant check.
17538 Set_Partial_View_Has_Unknown_Discr
17539 (Prev, Has_Unknown_Discriminants (Id));
17541 -- Preserve aspect and iterator flags that may have been set on
17542 -- the partial view.
17544 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17545 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17547 -- If no error, propagate freeze_node from private to full view.
17548 -- It may have been generated for an early operational item.
17550 if Present (Freeze_Node (Id))
17551 and then Serious_Errors_Detected = 0
17552 and then No (Full_View (Id))
17554 Set_Freeze_Node (Prev, Freeze_Node (Id));
17555 Set_Freeze_Node (Id, Empty);
17556 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17559 Set_Full_View (Id, Prev);
17563 -- Verify that full declaration conforms to partial one
17565 if Is_Incomplete_Or_Private_Type (Prev)
17566 and then Present (Discriminant_Specifications (Prev_Par))
17568 if Present (Discriminant_Specifications (N)) then
17569 if Ekind (Prev) = E_Incomplete_Type then
17570 Check_Discriminant_Conformance (N, Prev, Prev);
17572 Check_Discriminant_Conformance (N, Prev, Id);
17577 ("missing discriminants in full type declaration", N);
17579 -- To avoid cascaded errors on subsequent use, share the
17580 -- discriminants of the partial view.
17582 Set_Discriminant_Specifications (N,
17583 Discriminant_Specifications (Prev_Par));
17587 -- A prior untagged partial view can have an associated class-wide
17588 -- type due to use of the class attribute, and in this case the full
17589 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17590 -- of incomplete tagged declarations, but we check for it.
17593 and then (Is_Tagged_Type (Prev)
17594 or else Present (Class_Wide_Type (Prev)))
17596 -- Ada 2012 (AI05-0162): A private type may be the completion of
17597 -- an incomplete type.
17599 if Ada_Version >= Ada_2012
17600 and then Is_Incomplete_Type (Prev)
17601 and then Nkind_In (N, N_Private_Type_Declaration,
17602 N_Private_Extension_Declaration)
17604 -- No need to check private extensions since they are tagged
17606 if Nkind (N) = N_Private_Type_Declaration
17607 and then not Tagged_Present (N)
17612 -- The full declaration is either a tagged type (including
17613 -- a synchronized type that implements interfaces) or a
17614 -- type extension, otherwise this is an error.
17616 elsif Nkind_In (N, N_Task_Type_Declaration,
17617 N_Protected_Type_Declaration)
17619 if No (Interface_List (N)) and then not Error_Posted (N) then
17623 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17625 -- Indicate that the previous declaration (tagged incomplete
17626 -- or private declaration) requires the same on the full one.
17628 if not Tagged_Present (Type_Definition (N)) then
17630 Set_Is_Tagged_Type (Id);
17633 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17634 if No (Record_Extension_Part (Type_Definition (N))) then
17636 ("full declaration of } must be a record extension",
17639 -- Set some attributes to produce a usable full view
17641 Set_Is_Tagged_Type (Id);
17650 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17651 and then Present (Premature_Use (Parent (Prev)))
17653 Error_Msg_Sloc := Sloc (N);
17655 ("\full declaration #", Premature_Use (Parent (Prev)));
17660 end Find_Type_Name;
17662 -------------------------
17663 -- Find_Type_Of_Object --
17664 -------------------------
17666 function Find_Type_Of_Object
17667 (Obj_Def : Node_Id;
17668 Related_Nod : Node_Id) return Entity_Id
17670 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17671 P : Node_Id := Parent (Obj_Def);
17676 -- If the parent is a component_definition node we climb to the
17677 -- component_declaration node
17679 if Nkind (P) = N_Component_Definition then
17683 -- Case of an anonymous array subtype
17685 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17686 N_Unconstrained_Array_Definition)
17689 Array_Type_Declaration (T, Obj_Def);
17691 -- Create an explicit subtype whenever possible
17693 elsif Nkind (P) /= N_Component_Declaration
17694 and then Def_Kind = N_Subtype_Indication
17696 -- Base name of subtype on object name, which will be unique in
17697 -- the current scope.
17699 -- If this is a duplicate declaration, return base type, to avoid
17700 -- generating duplicate anonymous types.
17702 if Error_Posted (P) then
17703 Analyze (Subtype_Mark (Obj_Def));
17704 return Entity (Subtype_Mark (Obj_Def));
17709 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17711 T := Make_Defining_Identifier (Sloc (P), Nam);
17713 Insert_Action (Obj_Def,
17714 Make_Subtype_Declaration (Sloc (P),
17715 Defining_Identifier => T,
17716 Subtype_Indication => Relocate_Node (Obj_Def)));
17718 -- This subtype may need freezing, and this will not be done
17719 -- automatically if the object declaration is not in declarative
17720 -- part. Since this is an object declaration, the type cannot always
17721 -- be frozen here. Deferred constants do not freeze their type
17722 -- (which often enough will be private).
17724 if Nkind (P) = N_Object_Declaration
17725 and then Constant_Present (P)
17726 and then No (Expression (P))
17730 -- Here we freeze the base type of object type to catch premature use
17731 -- of discriminated private type without a full view.
17734 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17737 -- Ada 2005 AI-406: the object definition in an object declaration
17738 -- can be an access definition.
17740 elsif Def_Kind = N_Access_Definition then
17741 T := Access_Definition (Related_Nod, Obj_Def);
17743 Set_Is_Local_Anonymous_Access
17745 V => (Ada_Version < Ada_2012)
17746 or else (Nkind (P) /= N_Object_Declaration)
17747 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17749 -- Otherwise, the object definition is just a subtype_mark
17752 T := Process_Subtype (Obj_Def, Related_Nod);
17756 end Find_Type_Of_Object;
17758 --------------------------------
17759 -- Find_Type_Of_Subtype_Indic --
17760 --------------------------------
17762 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17766 -- Case of subtype mark with a constraint
17768 if Nkind (S) = N_Subtype_Indication then
17769 Find_Type (Subtype_Mark (S));
17770 Typ := Entity (Subtype_Mark (S));
17773 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17776 ("incorrect constraint for this kind of type", Constraint (S));
17777 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17780 -- Otherwise we have a subtype mark without a constraint
17782 elsif Error_Posted (S) then
17783 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17791 -- Check No_Wide_Characters restriction
17793 Check_Wide_Character_Restriction (Typ, S);
17796 end Find_Type_Of_Subtype_Indic;
17798 -------------------------------------
17799 -- Floating_Point_Type_Declaration --
17800 -------------------------------------
17802 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17803 Digs : constant Node_Id := Digits_Expression (Def);
17804 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17806 Base_Typ : Entity_Id;
17807 Implicit_Base : Entity_Id;
17809 function Can_Derive_From (E : Entity_Id) return Boolean;
17810 -- Find if given digits value, and possibly a specified range, allows
17811 -- derivation from specified type
17813 procedure Convert_Bound (B : Node_Id);
17814 -- If specified, the bounds must be static but may be of different
17815 -- types. They must be converted into machine numbers of the base type,
17816 -- in accordance with RM 4.9(38).
17818 function Find_Base_Type return Entity_Id;
17819 -- Find a predefined base type that Def can derive from, or generate
17820 -- an error and substitute Long_Long_Float if none exists.
17822 ---------------------
17823 -- Can_Derive_From --
17824 ---------------------
17826 function Can_Derive_From (E : Entity_Id) return Boolean is
17827 Spec : constant Entity_Id := Real_Range_Specification (Def);
17830 -- Check specified "digits" constraint
17832 if Digs_Val > Digits_Value (E) then
17836 -- Check for matching range, if specified
17838 if Present (Spec) then
17839 if Expr_Value_R (Type_Low_Bound (E)) >
17840 Expr_Value_R (Low_Bound (Spec))
17845 if Expr_Value_R (Type_High_Bound (E)) <
17846 Expr_Value_R (High_Bound (Spec))
17853 end Can_Derive_From;
17855 -------------------
17856 -- Convert_Bound --
17857 --------------------
17859 procedure Convert_Bound (B : Node_Id) is
17861 -- If the bound is not a literal it can only be static if it is
17862 -- a static constant, possibly of a specified type.
17864 if Is_Entity_Name (B)
17865 and then Ekind (Entity (B)) = E_Constant
17867 Rewrite (B, Constant_Value (Entity (B)));
17870 if Nkind (B) = N_Real_Literal then
17871 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
17872 Set_Is_Machine_Number (B);
17873 Set_Etype (B, Base_Typ);
17877 --------------------
17878 -- Find_Base_Type --
17879 --------------------
17881 function Find_Base_Type return Entity_Id is
17882 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17885 -- Iterate over the predefined types in order, returning the first
17886 -- one that Def can derive from.
17888 while Present (Choice) loop
17889 if Can_Derive_From (Node (Choice)) then
17890 return Node (Choice);
17893 Next_Elmt (Choice);
17896 -- If we can't derive from any existing type, use Long_Long_Float
17897 -- and give appropriate message explaining the problem.
17899 if Digs_Val > Max_Digs_Val then
17900 -- It might be the case that there is a type with the requested
17901 -- range, just not the combination of digits and range.
17904 ("no predefined type has requested range and precision",
17905 Real_Range_Specification (Def));
17909 ("range too large for any predefined type",
17910 Real_Range_Specification (Def));
17913 return Standard_Long_Long_Float;
17914 end Find_Base_Type;
17916 -- Start of processing for Floating_Point_Type_Declaration
17919 Check_Restriction (No_Floating_Point, Def);
17921 -- Create an implicit base type
17924 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17926 -- Analyze and verify digits value
17928 Analyze_And_Resolve (Digs, Any_Integer);
17929 Check_Digits_Expression (Digs);
17930 Digs_Val := Expr_Value (Digs);
17932 -- Process possible range spec and find correct type to derive from
17934 Process_Real_Range_Specification (Def);
17936 -- Check that requested number of digits is not too high.
17938 if Digs_Val > Max_Digs_Val then
17940 -- The check for Max_Base_Digits may be somewhat expensive, as it
17941 -- requires reading System, so only do it when necessary.
17944 Max_Base_Digits : constant Uint :=
17947 (Parent (RTE (RE_Max_Base_Digits))));
17950 if Digs_Val > Max_Base_Digits then
17951 Error_Msg_Uint_1 := Max_Base_Digits;
17952 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17954 elsif No (Real_Range_Specification (Def)) then
17955 Error_Msg_Uint_1 := Max_Digs_Val;
17956 Error_Msg_N ("types with more than ^ digits need range spec "
17957 & "(RM 3.5.7(6))", Digs);
17962 -- Find a suitable type to derive from or complain and use a substitute
17964 Base_Typ := Find_Base_Type;
17966 -- If there are bounds given in the declaration use them as the bounds
17967 -- of the type, otherwise use the bounds of the predefined base type
17968 -- that was chosen based on the Digits value.
17970 if Present (Real_Range_Specification (Def)) then
17971 Set_Scalar_Range (T, Real_Range_Specification (Def));
17972 Set_Is_Constrained (T);
17974 Convert_Bound (Type_Low_Bound (T));
17975 Convert_Bound (Type_High_Bound (T));
17978 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
17981 -- Complete definition of implicit base and declared first subtype. The
17982 -- inheritance of the rep item chain ensures that SPARK-related pragmas
17983 -- are not clobbered when the floating point type acts as a full view of
17986 Set_Etype (Implicit_Base, Base_Typ);
17987 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
17988 Set_Size_Info (Implicit_Base, Base_Typ);
17989 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
17990 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
17991 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
17992 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
17994 Set_Ekind (T, E_Floating_Point_Subtype);
17995 Set_Etype (T, Implicit_Base);
17996 Set_Size_Info (T, Implicit_Base);
17997 Set_RM_Size (T, RM_Size (Implicit_Base));
17998 Inherit_Rep_Item_Chain (T, Implicit_Base);
17999 Set_Digits_Value (T, Digs_Val);
18000 end Floating_Point_Type_Declaration;
18002 ----------------------------
18003 -- Get_Discriminant_Value --
18004 ----------------------------
18006 -- This is the situation:
18008 -- There is a non-derived type
18010 -- type T0 (Dx, Dy, Dz...)
18012 -- There are zero or more levels of derivation, with each derivation
18013 -- either purely inheriting the discriminants, or defining its own.
18015 -- type Ti is new Ti-1
18017 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
18019 -- subtype Ti is ...
18021 -- The subtype issue is avoided by the use of Original_Record_Component,
18022 -- and the fact that derived subtypes also derive the constraints.
18024 -- This chain leads back from
18026 -- Typ_For_Constraint
18028 -- Typ_For_Constraint has discriminants, and the value for each
18029 -- discriminant is given by its corresponding Elmt of Constraints.
18031 -- Discriminant is some discriminant in this hierarchy
18033 -- We need to return its value
18035 -- We do this by recursively searching each level, and looking for
18036 -- Discriminant. Once we get to the bottom, we start backing up
18037 -- returning the value for it which may in turn be a discriminant
18038 -- further up, so on the backup we continue the substitution.
18040 function Get_Discriminant_Value
18041 (Discriminant : Entity_Id;
18042 Typ_For_Constraint : Entity_Id;
18043 Constraint : Elist_Id) return Node_Id
18045 function Root_Corresponding_Discriminant
18046 (Discr : Entity_Id) return Entity_Id;
18047 -- Given a discriminant, traverse the chain of inherited discriminants
18048 -- and return the topmost discriminant.
18050 function Search_Derivation_Levels
18052 Discrim_Values : Elist_Id;
18053 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18054 -- This is the routine that performs the recursive search of levels
18055 -- as described above.
18057 -------------------------------------
18058 -- Root_Corresponding_Discriminant --
18059 -------------------------------------
18061 function Root_Corresponding_Discriminant
18062 (Discr : Entity_Id) return Entity_Id
18068 while Present (Corresponding_Discriminant (D)) loop
18069 D := Corresponding_Discriminant (D);
18073 end Root_Corresponding_Discriminant;
18075 ------------------------------
18076 -- Search_Derivation_Levels --
18077 ------------------------------
18079 function Search_Derivation_Levels
18081 Discrim_Values : Elist_Id;
18082 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18086 Result : Node_Or_Entity_Id;
18087 Result_Entity : Node_Id;
18090 -- If inappropriate type, return Error, this happens only in
18091 -- cascaded error situations, and we want to avoid a blow up.
18093 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18097 -- Look deeper if possible. Use Stored_Constraints only for
18098 -- untagged types. For tagged types use the given constraint.
18099 -- This asymmetry needs explanation???
18101 if not Stored_Discrim_Values
18102 and then Present (Stored_Constraint (Ti))
18103 and then not Is_Tagged_Type (Ti)
18106 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18110 Td : Entity_Id := Etype (Ti);
18113 -- If the parent type is private, the full view may include
18114 -- renamed discriminants, and it is those stored values that
18115 -- may be needed (the partial view never has more information
18116 -- than the full view).
18118 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18119 Td := Full_View (Td);
18123 Result := Discriminant;
18126 if Present (Stored_Constraint (Ti)) then
18128 Search_Derivation_Levels
18129 (Td, Stored_Constraint (Ti), True);
18132 Search_Derivation_Levels
18133 (Td, Discrim_Values, Stored_Discrim_Values);
18139 -- Extra underlying places to search, if not found above. For
18140 -- concurrent types, the relevant discriminant appears in the
18141 -- corresponding record. For a type derived from a private type
18142 -- without discriminant, the full view inherits the discriminants
18143 -- of the full view of the parent.
18145 if Result = Discriminant then
18146 if Is_Concurrent_Type (Ti)
18147 and then Present (Corresponding_Record_Type (Ti))
18150 Search_Derivation_Levels (
18151 Corresponding_Record_Type (Ti),
18153 Stored_Discrim_Values);
18155 elsif Is_Private_Type (Ti)
18156 and then not Has_Discriminants (Ti)
18157 and then Present (Full_View (Ti))
18158 and then Etype (Full_View (Ti)) /= Ti
18161 Search_Derivation_Levels (
18164 Stored_Discrim_Values);
18168 -- If Result is not a (reference to a) discriminant, return it,
18169 -- otherwise set Result_Entity to the discriminant.
18171 if Nkind (Result) = N_Defining_Identifier then
18172 pragma Assert (Result = Discriminant);
18173 Result_Entity := Result;
18176 if not Denotes_Discriminant (Result) then
18180 Result_Entity := Entity (Result);
18183 -- See if this level of derivation actually has discriminants because
18184 -- tagged derivations can add them, hence the lower levels need not
18187 if not Has_Discriminants (Ti) then
18191 -- Scan Ti's discriminants for Result_Entity, and return its
18192 -- corresponding value, if any.
18194 Result_Entity := Original_Record_Component (Result_Entity);
18196 Assoc := First_Elmt (Discrim_Values);
18198 if Stored_Discrim_Values then
18199 Disc := First_Stored_Discriminant (Ti);
18201 Disc := First_Discriminant (Ti);
18204 while Present (Disc) loop
18206 -- If no further associations return the discriminant, value will
18207 -- be found on the second pass.
18213 if Original_Record_Component (Disc) = Result_Entity then
18214 return Node (Assoc);
18219 if Stored_Discrim_Values then
18220 Next_Stored_Discriminant (Disc);
18222 Next_Discriminant (Disc);
18226 -- Could not find it
18229 end Search_Derivation_Levels;
18233 Result : Node_Or_Entity_Id;
18235 -- Start of processing for Get_Discriminant_Value
18238 -- ??? This routine is a gigantic mess and will be deleted. For the
18239 -- time being just test for the trivial case before calling recurse.
18241 -- We are now celebrating the 20th anniversary of this comment!
18243 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18249 D := First_Discriminant (Typ_For_Constraint);
18250 E := First_Elmt (Constraint);
18251 while Present (D) loop
18252 if Chars (D) = Chars (Discriminant) then
18256 Next_Discriminant (D);
18262 Result := Search_Derivation_Levels
18263 (Typ_For_Constraint, Constraint, False);
18265 -- ??? hack to disappear when this routine is gone
18267 if Nkind (Result) = N_Defining_Identifier then
18273 D := First_Discriminant (Typ_For_Constraint);
18274 E := First_Elmt (Constraint);
18275 while Present (D) loop
18276 if Root_Corresponding_Discriminant (D) = Discriminant then
18280 Next_Discriminant (D);
18286 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18288 end Get_Discriminant_Value;
18290 --------------------------
18291 -- Has_Range_Constraint --
18292 --------------------------
18294 function Has_Range_Constraint (N : Node_Id) return Boolean is
18295 C : constant Node_Id := Constraint (N);
18298 if Nkind (C) = N_Range_Constraint then
18301 elsif Nkind (C) = N_Digits_Constraint then
18303 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18304 or else Present (Range_Constraint (C));
18306 elsif Nkind (C) = N_Delta_Constraint then
18307 return Present (Range_Constraint (C));
18312 end Has_Range_Constraint;
18314 ------------------------
18315 -- Inherit_Components --
18316 ------------------------
18318 function Inherit_Components
18320 Parent_Base : Entity_Id;
18321 Derived_Base : Entity_Id;
18322 Is_Tagged : Boolean;
18323 Inherit_Discr : Boolean;
18324 Discs : Elist_Id) return Elist_Id
18326 Assoc_List : constant Elist_Id := New_Elmt_List;
18328 procedure Inherit_Component
18329 (Old_C : Entity_Id;
18330 Plain_Discrim : Boolean := False;
18331 Stored_Discrim : Boolean := False);
18332 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18333 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18334 -- True, Old_C is a stored discriminant. If they are both false then
18335 -- Old_C is a regular component.
18337 -----------------------
18338 -- Inherit_Component --
18339 -----------------------
18341 procedure Inherit_Component
18342 (Old_C : Entity_Id;
18343 Plain_Discrim : Boolean := False;
18344 Stored_Discrim : Boolean := False)
18346 procedure Set_Anonymous_Type (Id : Entity_Id);
18347 -- Id denotes the entity of an access discriminant or anonymous
18348 -- access component. Set the type of Id to either the same type of
18349 -- Old_C or create a new one depending on whether the parent and
18350 -- the child types are in the same scope.
18352 ------------------------
18353 -- Set_Anonymous_Type --
18354 ------------------------
18356 procedure Set_Anonymous_Type (Id : Entity_Id) is
18357 Old_Typ : constant Entity_Id := Etype (Old_C);
18360 if Scope (Parent_Base) = Scope (Derived_Base) then
18361 Set_Etype (Id, Old_Typ);
18363 -- The parent and the derived type are in two different scopes.
18364 -- Reuse the type of the original discriminant / component by
18365 -- copying it in order to preserve all attributes.
18369 Typ : constant Entity_Id := New_Copy (Old_Typ);
18372 Set_Etype (Id, Typ);
18374 -- Since we do not generate component declarations for
18375 -- inherited components, associate the itype with the
18378 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18379 Set_Scope (Typ, Derived_Base);
18382 end Set_Anonymous_Type;
18384 -- Local variables and constants
18386 New_C : constant Entity_Id := New_Copy (Old_C);
18388 Corr_Discrim : Entity_Id;
18389 Discrim : Entity_Id;
18391 -- Start of processing for Inherit_Component
18394 pragma Assert (not Is_Tagged or not Stored_Discrim);
18396 Set_Parent (New_C, Parent (Old_C));
18398 -- Regular discriminants and components must be inserted in the scope
18399 -- of the Derived_Base. Do it here.
18401 if not Stored_Discrim then
18402 Enter_Name (New_C);
18405 -- For tagged types the Original_Record_Component must point to
18406 -- whatever this field was pointing to in the parent type. This has
18407 -- already been achieved by the call to New_Copy above.
18409 if not Is_Tagged then
18410 Set_Original_Record_Component (New_C, New_C);
18411 Set_Corresponding_Record_Component (New_C, Old_C);
18414 -- Set the proper type of an access discriminant
18416 if Ekind (New_C) = E_Discriminant
18417 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18419 Set_Anonymous_Type (New_C);
18422 -- If we have inherited a component then see if its Etype contains
18423 -- references to Parent_Base discriminants. In this case, replace
18424 -- these references with the constraints given in Discs. We do not
18425 -- do this for the partial view of private types because this is
18426 -- not needed (only the components of the full view will be used
18427 -- for code generation) and cause problem. We also avoid this
18428 -- transformation in some error situations.
18430 if Ekind (New_C) = E_Component then
18432 -- Set the proper type of an anonymous access component
18434 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18435 Set_Anonymous_Type (New_C);
18437 elsif (Is_Private_Type (Derived_Base)
18438 and then not Is_Generic_Type (Derived_Base))
18439 or else (Is_Empty_Elmt_List (Discs)
18440 and then not Expander_Active)
18442 Set_Etype (New_C, Etype (Old_C));
18445 -- The current component introduces a circularity of the
18448 -- limited with Pack_2;
18449 -- package Pack_1 is
18450 -- type T_1 is tagged record
18451 -- Comp : access Pack_2.T_2;
18457 -- package Pack_2 is
18458 -- type T_2 is new Pack_1.T_1 with ...;
18463 Constrain_Component_Type
18464 (Old_C, Derived_Base, N, Parent_Base, Discs));
18468 -- In derived tagged types it is illegal to reference a non
18469 -- discriminant component in the parent type. To catch this, mark
18470 -- these components with an Ekind of E_Void. This will be reset in
18471 -- Record_Type_Definition after processing the record extension of
18472 -- the derived type.
18474 -- If the declaration is a private extension, there is no further
18475 -- record extension to process, and the components retain their
18476 -- current kind, because they are visible at this point.
18478 if Is_Tagged and then Ekind (New_C) = E_Component
18479 and then Nkind (N) /= N_Private_Extension_Declaration
18481 Set_Ekind (New_C, E_Void);
18484 if Plain_Discrim then
18485 Set_Corresponding_Discriminant (New_C, Old_C);
18486 Build_Discriminal (New_C);
18488 -- If we are explicitly inheriting a stored discriminant it will be
18489 -- completely hidden.
18491 elsif Stored_Discrim then
18492 Set_Corresponding_Discriminant (New_C, Empty);
18493 Set_Discriminal (New_C, Empty);
18494 Set_Is_Completely_Hidden (New_C);
18496 -- Set the Original_Record_Component of each discriminant in the
18497 -- derived base to point to the corresponding stored that we just
18500 Discrim := First_Discriminant (Derived_Base);
18501 while Present (Discrim) loop
18502 Corr_Discrim := Corresponding_Discriminant (Discrim);
18504 -- Corr_Discrim could be missing in an error situation
18506 if Present (Corr_Discrim)
18507 and then Original_Record_Component (Corr_Discrim) = Old_C
18509 Set_Original_Record_Component (Discrim, New_C);
18510 Set_Corresponding_Record_Component (Discrim, Empty);
18513 Next_Discriminant (Discrim);
18516 Append_Entity (New_C, Derived_Base);
18519 if not Is_Tagged then
18520 Append_Elmt (Old_C, Assoc_List);
18521 Append_Elmt (New_C, Assoc_List);
18523 end Inherit_Component;
18525 -- Variables local to Inherit_Component
18527 Loc : constant Source_Ptr := Sloc (N);
18529 Parent_Discrim : Entity_Id;
18530 Stored_Discrim : Entity_Id;
18532 Component : Entity_Id;
18534 -- Start of processing for Inherit_Components
18537 if not Is_Tagged then
18538 Append_Elmt (Parent_Base, Assoc_List);
18539 Append_Elmt (Derived_Base, Assoc_List);
18542 -- Inherit parent discriminants if needed
18544 if Inherit_Discr then
18545 Parent_Discrim := First_Discriminant (Parent_Base);
18546 while Present (Parent_Discrim) loop
18547 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18548 Next_Discriminant (Parent_Discrim);
18552 -- Create explicit stored discrims for untagged types when necessary
18554 if not Has_Unknown_Discriminants (Derived_Base)
18555 and then Has_Discriminants (Parent_Base)
18556 and then not Is_Tagged
18559 or else First_Discriminant (Parent_Base) /=
18560 First_Stored_Discriminant (Parent_Base))
18562 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18563 while Present (Stored_Discrim) loop
18564 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18565 Next_Stored_Discriminant (Stored_Discrim);
18569 -- See if we can apply the second transformation for derived types, as
18570 -- explained in point 6. in the comments above Build_Derived_Record_Type
18571 -- This is achieved by appending Derived_Base discriminants into Discs,
18572 -- which has the side effect of returning a non empty Discs list to the
18573 -- caller of Inherit_Components, which is what we want. This must be
18574 -- done for private derived types if there are explicit stored
18575 -- discriminants, to ensure that we can retrieve the values of the
18576 -- constraints provided in the ancestors.
18579 and then Is_Empty_Elmt_List (Discs)
18580 and then Present (First_Discriminant (Derived_Base))
18582 (not Is_Private_Type (Derived_Base)
18583 or else Is_Completely_Hidden
18584 (First_Stored_Discriminant (Derived_Base))
18585 or else Is_Generic_Type (Derived_Base))
18587 D := First_Discriminant (Derived_Base);
18588 while Present (D) loop
18589 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18590 Next_Discriminant (D);
18594 -- Finally, inherit non-discriminant components unless they are not
18595 -- visible because defined or inherited from the full view of the
18596 -- parent. Don't inherit the _parent field of the parent type.
18598 Component := First_Entity (Parent_Base);
18599 while Present (Component) loop
18601 -- Ada 2005 (AI-251): Do not inherit components associated with
18602 -- secondary tags of the parent.
18604 if Ekind (Component) = E_Component
18605 and then Present (Related_Type (Component))
18609 elsif Ekind (Component) /= E_Component
18610 or else Chars (Component) = Name_uParent
18614 -- If the derived type is within the parent type's declarative
18615 -- region, then the components can still be inherited even though
18616 -- they aren't visible at this point. This can occur for cases
18617 -- such as within public child units where the components must
18618 -- become visible upon entering the child unit's private part.
18620 elsif not Is_Visible_Component (Component)
18621 and then not In_Open_Scopes (Scope (Parent_Base))
18625 elsif Ekind_In (Derived_Base, E_Private_Type,
18626 E_Limited_Private_Type)
18631 Inherit_Component (Component);
18634 Next_Entity (Component);
18637 -- For tagged derived types, inherited discriminants cannot be used in
18638 -- component declarations of the record extension part. To achieve this
18639 -- we mark the inherited discriminants as not visible.
18641 if Is_Tagged and then Inherit_Discr then
18642 D := First_Discriminant (Derived_Base);
18643 while Present (D) loop
18644 Set_Is_Immediately_Visible (D, False);
18645 Next_Discriminant (D);
18650 end Inherit_Components;
18652 -----------------------------
18653 -- Inherit_Predicate_Flags --
18654 -----------------------------
18656 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18658 if Present (Predicate_Function (Subt)) then
18662 Set_Has_Predicates (Subt, Has_Predicates (Par));
18663 Set_Has_Static_Predicate_Aspect
18664 (Subt, Has_Static_Predicate_Aspect (Par));
18665 Set_Has_Dynamic_Predicate_Aspect
18666 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18668 -- A named subtype does not inherit the predicate function of its
18669 -- parent but an itype declared for a loop index needs the discrete
18670 -- predicate information of its parent to execute the loop properly.
18671 -- A non-discrete type may has a static predicate (for example True)
18672 -- but has no static_discrete_predicate.
18674 if Is_Itype (Subt) and then Present (Predicate_Function (Par)) then
18675 Set_Subprograms_For_Type (Subt, Subprograms_For_Type (Par));
18677 if Has_Static_Predicate (Par) and then Is_Discrete_Type (Par) then
18678 Set_Static_Discrete_Predicate
18679 (Subt, Static_Discrete_Predicate (Par));
18682 end Inherit_Predicate_Flags;
18684 ----------------------
18685 -- Is_EVF_Procedure --
18686 ----------------------
18688 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18689 Formal : Entity_Id;
18692 -- Examine the formals of an Extensions_Visible False procedure looking
18693 -- for a controlling OUT parameter.
18695 if Ekind (Subp) = E_Procedure
18696 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18698 Formal := First_Formal (Subp);
18699 while Present (Formal) loop
18700 if Ekind (Formal) = E_Out_Parameter
18701 and then Is_Controlling_Formal (Formal)
18706 Next_Formal (Formal);
18711 end Is_EVF_Procedure;
18713 -----------------------
18714 -- Is_Null_Extension --
18715 -----------------------
18717 function Is_Null_Extension (T : Entity_Id) return Boolean is
18718 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18719 Comp_List : Node_Id;
18723 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18724 or else not Is_Tagged_Type (T)
18725 or else Nkind (Type_Definition (Type_Decl)) /=
18726 N_Derived_Type_Definition
18727 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18733 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18735 if Present (Discriminant_Specifications (Type_Decl)) then
18738 elsif Present (Comp_List)
18739 and then Is_Non_Empty_List (Component_Items (Comp_List))
18741 Comp := First (Component_Items (Comp_List));
18743 -- Only user-defined components are relevant. The component list
18744 -- may also contain a parent component and internal components
18745 -- corresponding to secondary tags, but these do not determine
18746 -- whether this is a null extension.
18748 while Present (Comp) loop
18749 if Comes_From_Source (Comp) then
18761 end Is_Null_Extension;
18763 ------------------------------
18764 -- Is_Valid_Constraint_Kind --
18765 ------------------------------
18767 function Is_Valid_Constraint_Kind
18768 (T_Kind : Type_Kind;
18769 Constraint_Kind : Node_Kind) return Boolean
18773 when Enumeration_Kind
18776 return Constraint_Kind = N_Range_Constraint;
18778 when Decimal_Fixed_Point_Kind =>
18779 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18780 N_Range_Constraint);
18782 when Ordinary_Fixed_Point_Kind =>
18783 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18784 N_Range_Constraint);
18787 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18788 N_Range_Constraint);
18795 | E_Incomplete_Type
18799 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18802 return True; -- Error will be detected later
18804 end Is_Valid_Constraint_Kind;
18806 --------------------------
18807 -- Is_Visible_Component --
18808 --------------------------
18810 function Is_Visible_Component
18812 N : Node_Id := Empty) return Boolean
18814 Original_Comp : Entity_Id := Empty;
18815 Original_Type : Entity_Id;
18816 Type_Scope : Entity_Id;
18818 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18819 -- Check whether parent type of inherited component is declared locally,
18820 -- possibly within a nested package or instance. The current scope is
18821 -- the derived record itself.
18823 -------------------
18824 -- Is_Local_Type --
18825 -------------------
18827 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18831 Scop := Scope (Typ);
18832 while Present (Scop)
18833 and then Scop /= Standard_Standard
18835 if Scop = Scope (Current_Scope) then
18839 Scop := Scope (Scop);
18845 -- Start of processing for Is_Visible_Component
18848 if Ekind_In (C, E_Component, E_Discriminant) then
18849 Original_Comp := Original_Record_Component (C);
18852 if No (Original_Comp) then
18854 -- Premature usage, or previous error
18859 Original_Type := Scope (Original_Comp);
18860 Type_Scope := Scope (Base_Type (Scope (C)));
18863 -- This test only concerns tagged types
18865 if not Is_Tagged_Type (Original_Type) then
18867 -- Check if this is a renamed discriminant (hidden either by the
18868 -- derived type or by some ancestor), unless we are analyzing code
18869 -- generated by the expander since it may reference such components
18870 -- (for example see the expansion of Deep_Adjust).
18872 if Ekind (C) = E_Discriminant and then Present (N) then
18874 not Comes_From_Source (N)
18875 or else not Is_Completely_Hidden (C);
18880 -- If it is _Parent or _Tag, there is no visibility issue
18882 elsif not Comes_From_Source (Original_Comp) then
18885 -- Discriminants are visible unless the (private) type has unknown
18886 -- discriminants. If the discriminant reference is inserted for a
18887 -- discriminant check on a full view it is also visible.
18889 elsif Ekind (Original_Comp) = E_Discriminant
18891 (not Has_Unknown_Discriminants (Original_Type)
18892 or else (Present (N)
18893 and then Nkind (N) = N_Selected_Component
18894 and then Nkind (Prefix (N)) = N_Type_Conversion
18895 and then not Comes_From_Source (Prefix (N))))
18899 -- In the body of an instantiation, check the visibility of a component
18900 -- in case it has a homograph that is a primitive operation of a private
18901 -- type which was not visible in the generic unit.
18903 -- Should Is_Prefixed_Call be propagated from template to instance???
18905 elsif In_Instance_Body then
18906 if not Is_Tagged_Type (Original_Type)
18907 or else not Is_Private_Type (Original_Type)
18913 Subp_Elmt : Elmt_Id;
18916 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18917 while Present (Subp_Elmt) loop
18919 -- The component is hidden by a primitive operation
18921 if Chars (Node (Subp_Elmt)) = Chars (C) then
18925 Next_Elmt (Subp_Elmt);
18932 -- If the component has been declared in an ancestor which is currently
18933 -- a private type, then it is not visible. The same applies if the
18934 -- component's containing type is not in an open scope and the original
18935 -- component's enclosing type is a visible full view of a private type
18936 -- (which can occur in cases where an attempt is being made to reference
18937 -- a component in a sibling package that is inherited from a visible
18938 -- component of a type in an ancestor package; the component in the
18939 -- sibling package should not be visible even though the component it
18940 -- inherited from is visible). This does not apply however in the case
18941 -- where the scope of the type is a private child unit, or when the
18942 -- parent comes from a local package in which the ancestor is currently
18943 -- visible. The latter suppression of visibility is needed for cases
18944 -- that are tested in B730006.
18946 elsif Is_Private_Type (Original_Type)
18948 (not Is_Private_Descendant (Type_Scope)
18949 and then not In_Open_Scopes (Type_Scope)
18950 and then Has_Private_Declaration (Original_Type))
18952 -- If the type derives from an entity in a formal package, there
18953 -- are no additional visible components.
18955 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18956 N_Formal_Package_Declaration
18960 -- if we are not in the private part of the current package, there
18961 -- are no additional visible components.
18963 elsif Ekind (Scope (Current_Scope)) = E_Package
18964 and then not In_Private_Part (Scope (Current_Scope))
18969 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18970 and then In_Open_Scopes (Scope (Original_Type))
18971 and then Is_Local_Type (Type_Scope);
18974 -- There is another weird way in which a component may be invisible when
18975 -- the private and the full view are not derived from the same ancestor.
18976 -- Here is an example :
18978 -- type A1 is tagged record F1 : integer; end record;
18979 -- type A2 is new A1 with record F2 : integer; end record;
18980 -- type T is new A1 with private;
18982 -- type T is new A2 with null record;
18984 -- In this case, the full view of T inherits F1 and F2 but the private
18985 -- view inherits only F1
18989 Ancestor : Entity_Id := Scope (C);
18993 if Ancestor = Original_Type then
18996 -- The ancestor may have a partial view of the original type,
18997 -- but if the full view is in scope, as in a child body, the
18998 -- component is visible.
19000 elsif In_Private_Part (Scope (Original_Type))
19001 and then Full_View (Ancestor) = Original_Type
19005 elsif Ancestor = Etype (Ancestor) then
19007 -- No further ancestors to examine
19012 Ancestor := Etype (Ancestor);
19016 end Is_Visible_Component;
19018 --------------------------
19019 -- Make_Class_Wide_Type --
19020 --------------------------
19022 procedure Make_Class_Wide_Type (T : Entity_Id) is
19023 CW_Type : Entity_Id;
19025 Next_E : Entity_Id;
19026 Prev_E : Entity_Id;
19029 if Present (Class_Wide_Type (T)) then
19031 -- The class-wide type is a partially decorated entity created for a
19032 -- unanalyzed tagged type referenced through a limited with clause.
19033 -- When the tagged type is analyzed, its class-wide type needs to be
19034 -- redecorated. Note that we reuse the entity created by Decorate_
19035 -- Tagged_Type in order to preserve all links.
19037 if Materialize_Entity (Class_Wide_Type (T)) then
19038 CW_Type := Class_Wide_Type (T);
19039 Set_Materialize_Entity (CW_Type, False);
19041 -- The class wide type can have been defined by the partial view, in
19042 -- which case everything is already done.
19048 -- Default case, we need to create a new class-wide type
19052 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19055 -- Inherit root type characteristics
19057 CW_Name := Chars (CW_Type);
19058 Next_E := Next_Entity (CW_Type);
19059 Prev_E := Prev_Entity (CW_Type);
19060 Copy_Node (T, CW_Type);
19061 Set_Comes_From_Source (CW_Type, False);
19062 Set_Chars (CW_Type, CW_Name);
19063 Set_Parent (CW_Type, Parent (T));
19064 Set_Prev_Entity (CW_Type, Prev_E);
19065 Set_Next_Entity (CW_Type, Next_E);
19067 -- Ensure we have a new freeze node for the class-wide type. The partial
19068 -- view may have freeze action of its own, requiring a proper freeze
19069 -- node, and the same freeze node cannot be shared between the two
19072 Set_Has_Delayed_Freeze (CW_Type);
19073 Set_Freeze_Node (CW_Type, Empty);
19075 -- Customize the class-wide type: It has no prim. op., it cannot be
19076 -- abstract, its Etype points back to the specific root type, and it
19077 -- cannot have any invariants.
19079 Set_Ekind (CW_Type, E_Class_Wide_Type);
19080 Set_Is_Tagged_Type (CW_Type, True);
19081 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19082 Set_Is_Abstract_Type (CW_Type, False);
19083 Set_Is_Constrained (CW_Type, False);
19084 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19085 Set_Default_SSO (CW_Type);
19086 Set_Has_Inheritable_Invariants (CW_Type, False);
19087 Set_Has_Inherited_Invariants (CW_Type, False);
19088 Set_Has_Own_Invariants (CW_Type, False);
19090 if Ekind (T) = E_Class_Wide_Subtype then
19091 Set_Etype (CW_Type, Etype (Base_Type (T)));
19093 Set_Etype (CW_Type, T);
19096 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19098 -- If this is the class_wide type of a constrained subtype, it does
19099 -- not have discriminants.
19101 Set_Has_Discriminants (CW_Type,
19102 Has_Discriminants (T) and then not Is_Constrained (T));
19104 Set_Has_Unknown_Discriminants (CW_Type, True);
19105 Set_Class_Wide_Type (T, CW_Type);
19106 Set_Equivalent_Type (CW_Type, Empty);
19108 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19110 Set_Class_Wide_Type (CW_Type, CW_Type);
19111 end Make_Class_Wide_Type;
19117 procedure Make_Index
19119 Related_Nod : Node_Id;
19120 Related_Id : Entity_Id := Empty;
19121 Suffix_Index : Nat := 1;
19122 In_Iter_Schm : Boolean := False)
19126 Def_Id : Entity_Id := Empty;
19127 Found : Boolean := False;
19130 -- For a discrete range used in a constrained array definition and
19131 -- defined by a range, an implicit conversion to the predefined type
19132 -- INTEGER is assumed if each bound is either a numeric literal, a named
19133 -- number, or an attribute, and the type of both bounds (prior to the
19134 -- implicit conversion) is the type universal_integer. Otherwise, both
19135 -- bounds must be of the same discrete type, other than universal
19136 -- integer; this type must be determinable independently of the
19137 -- context, but using the fact that the type must be discrete and that
19138 -- both bounds must have the same type.
19140 -- Character literals also have a universal type in the absence of
19141 -- of additional context, and are resolved to Standard_Character.
19143 if Nkind (N) = N_Range then
19145 -- The index is given by a range constraint. The bounds are known
19146 -- to be of a consistent type.
19148 if not Is_Overloaded (N) then
19151 -- For universal bounds, choose the specific predefined type
19153 if T = Universal_Integer then
19154 T := Standard_Integer;
19156 elsif T = Any_Character then
19157 Ambiguous_Character (Low_Bound (N));
19159 T := Standard_Character;
19162 -- The node may be overloaded because some user-defined operators
19163 -- are available, but if a universal interpretation exists it is
19164 -- also the selected one.
19166 elsif Universal_Interpretation (N) = Universal_Integer then
19167 T := Standard_Integer;
19173 Ind : Interp_Index;
19177 Get_First_Interp (N, Ind, It);
19178 while Present (It.Typ) loop
19179 if Is_Discrete_Type (It.Typ) then
19182 and then not Covers (It.Typ, T)
19183 and then not Covers (T, It.Typ)
19185 Error_Msg_N ("ambiguous bounds in discrete range", N);
19193 Get_Next_Interp (Ind, It);
19196 if T = Any_Type then
19197 Error_Msg_N ("discrete type required for range", N);
19198 Set_Etype (N, Any_Type);
19201 elsif T = Universal_Integer then
19202 T := Standard_Integer;
19207 if not Is_Discrete_Type (T) then
19208 Error_Msg_N ("discrete type required for range", N);
19209 Set_Etype (N, Any_Type);
19213 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19214 and then Attribute_Name (Low_Bound (N)) = Name_First
19215 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19216 and then Is_Type (Entity (Prefix (Low_Bound (N))))
19217 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19219 -- The type of the index will be the type of the prefix, as long
19220 -- as the upper bound is 'Last of the same type.
19222 Def_Id := Entity (Prefix (Low_Bound (N)));
19224 if Nkind (High_Bound (N)) /= N_Attribute_Reference
19225 or else Attribute_Name (High_Bound (N)) /= Name_Last
19226 or else not Is_Entity_Name (Prefix (High_Bound (N)))
19227 or else Entity (Prefix (High_Bound (N))) /= Def_Id
19234 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
19236 elsif Nkind (N) = N_Subtype_Indication then
19238 -- The index is given by a subtype with a range constraint
19240 T := Base_Type (Entity (Subtype_Mark (N)));
19242 if not Is_Discrete_Type (T) then
19243 Error_Msg_N ("discrete type required for range", N);
19244 Set_Etype (N, Any_Type);
19248 R := Range_Expression (Constraint (N));
19251 Process_Range_Expr_In_Decl
19252 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
19254 elsif Nkind (N) = N_Attribute_Reference then
19256 -- Catch beginner's error (use of attribute other than 'Range)
19258 if Attribute_Name (N) /= Name_Range then
19259 Error_Msg_N ("expect attribute ''Range", N);
19260 Set_Etype (N, Any_Type);
19264 -- If the node denotes the range of a type mark, that is also the
19265 -- resulting type, and we do not need to create an Itype for it.
19267 if Is_Entity_Name (Prefix (N))
19268 and then Comes_From_Source (N)
19269 and then Is_Type (Entity (Prefix (N)))
19270 and then Is_Discrete_Type (Entity (Prefix (N)))
19272 Def_Id := Entity (Prefix (N));
19275 Analyze_And_Resolve (N);
19279 -- If none of the above, must be a subtype. We convert this to a
19280 -- range attribute reference because in the case of declared first
19281 -- named subtypes, the types in the range reference can be different
19282 -- from the type of the entity. A range attribute normalizes the
19283 -- reference and obtains the correct types for the bounds.
19285 -- This transformation is in the nature of an expansion, is only
19286 -- done if expansion is active. In particular, it is not done on
19287 -- formal generic types, because we need to retain the name of the
19288 -- original index for instantiation purposes.
19291 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19292 Error_Msg_N ("invalid subtype mark in discrete range ", N);
19293 Set_Etype (N, Any_Integer);
19297 -- The type mark may be that of an incomplete type. It is only
19298 -- now that we can get the full view, previous analysis does
19299 -- not look specifically for a type mark.
19301 Set_Entity (N, Get_Full_View (Entity (N)));
19302 Set_Etype (N, Entity (N));
19303 Def_Id := Entity (N);
19305 if not Is_Discrete_Type (Def_Id) then
19306 Error_Msg_N ("discrete type required for index", N);
19307 Set_Etype (N, Any_Type);
19312 if Expander_Active then
19314 Make_Attribute_Reference (Sloc (N),
19315 Attribute_Name => Name_Range,
19316 Prefix => Relocate_Node (N)));
19318 -- The original was a subtype mark that does not freeze. This
19319 -- means that the rewritten version must not freeze either.
19321 Set_Must_Not_Freeze (N);
19322 Set_Must_Not_Freeze (Prefix (N));
19323 Analyze_And_Resolve (N);
19327 -- If expander is inactive, type is legal, nothing else to construct
19334 if not Is_Discrete_Type (T) then
19335 Error_Msg_N ("discrete type required for range", N);
19336 Set_Etype (N, Any_Type);
19339 elsif T = Any_Type then
19340 Set_Etype (N, Any_Type);
19344 -- We will now create the appropriate Itype to describe the range, but
19345 -- first a check. If we originally had a subtype, then we just label
19346 -- the range with this subtype. Not only is there no need to construct
19347 -- a new subtype, but it is wrong to do so for two reasons:
19349 -- 1. A legality concern, if we have a subtype, it must not freeze,
19350 -- and the Itype would cause freezing incorrectly
19352 -- 2. An efficiency concern, if we created an Itype, it would not be
19353 -- recognized as the same type for the purposes of eliminating
19354 -- checks in some circumstances.
19356 -- We signal this case by setting the subtype entity in Def_Id
19358 if No (Def_Id) then
19360 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19361 Set_Etype (Def_Id, Base_Type (T));
19363 if Is_Signed_Integer_Type (T) then
19364 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19366 elsif Is_Modular_Integer_Type (T) then
19367 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19370 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19371 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19372 Set_First_Literal (Def_Id, First_Literal (T));
19375 Set_Size_Info (Def_Id, (T));
19376 Set_RM_Size (Def_Id, RM_Size (T));
19377 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19379 Set_Scalar_Range (Def_Id, R);
19380 Conditional_Delay (Def_Id, T);
19382 if Nkind (N) = N_Subtype_Indication then
19383 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19386 -- In the subtype indication case, if the immediate parent of the
19387 -- new subtype is nonstatic, then the subtype we create is nonstatic,
19388 -- even if its bounds are static.
19390 if Nkind (N) = N_Subtype_Indication
19391 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19393 Set_Is_Non_Static_Subtype (Def_Id);
19397 -- Final step is to label the index with this constructed type
19399 Set_Etype (N, Def_Id);
19402 ------------------------------
19403 -- Modular_Type_Declaration --
19404 ------------------------------
19406 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19407 Mod_Expr : constant Node_Id := Expression (Def);
19410 procedure Set_Modular_Size (Bits : Int);
19411 -- Sets RM_Size to Bits, and Esize to normal word size above this
19413 ----------------------
19414 -- Set_Modular_Size --
19415 ----------------------
19417 procedure Set_Modular_Size (Bits : Int) is
19419 Set_RM_Size (T, UI_From_Int (Bits));
19424 elsif Bits <= 16 then
19425 Init_Esize (T, 16);
19427 elsif Bits <= 32 then
19428 Init_Esize (T, 32);
19431 Init_Esize (T, System_Max_Binary_Modulus_Power);
19434 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19435 Set_Is_Known_Valid (T);
19437 end Set_Modular_Size;
19439 -- Start of processing for Modular_Type_Declaration
19442 -- If the mod expression is (exactly) 2 * literal, where literal is
19443 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19445 if Warn_On_Suspicious_Modulus_Value
19446 and then Nkind (Mod_Expr) = N_Op_Multiply
19447 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19448 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19449 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19450 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19453 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19456 -- Proceed with analysis of mod expression
19458 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19460 Set_Ekind (T, E_Modular_Integer_Type);
19461 Init_Alignment (T);
19462 Set_Is_Constrained (T);
19464 if not Is_OK_Static_Expression (Mod_Expr) then
19465 Flag_Non_Static_Expr
19466 ("non-static expression used for modular type bound!", Mod_Expr);
19467 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19469 M_Val := Expr_Value (Mod_Expr);
19473 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19474 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19477 if M_Val > 2 ** Standard_Long_Integer_Size then
19478 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19481 Set_Modulus (T, M_Val);
19483 -- Create bounds for the modular type based on the modulus given in
19484 -- the type declaration and then analyze and resolve those bounds.
19486 Set_Scalar_Range (T,
19487 Make_Range (Sloc (Mod_Expr),
19488 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19489 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19491 -- Properly analyze the literals for the range. We do this manually
19492 -- because we can't go calling Resolve, since we are resolving these
19493 -- bounds with the type, and this type is certainly not complete yet.
19495 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19496 Set_Etype (High_Bound (Scalar_Range (T)), T);
19497 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19498 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19500 -- Loop through powers of two to find number of bits required
19502 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19506 if M_Val = 2 ** Bits then
19507 Set_Modular_Size (Bits);
19512 elsif M_Val < 2 ** Bits then
19513 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
19514 Set_Non_Binary_Modulus (T);
19516 if Bits > System_Max_Nonbinary_Modulus_Power then
19517 Error_Msg_Uint_1 :=
19518 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19520 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19521 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19525 -- In the nonbinary case, set size as per RM 13.3(55)
19527 Set_Modular_Size (Bits);
19534 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19535 -- so we just signal an error and set the maximum size.
19537 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19538 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19540 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19541 Init_Alignment (T);
19543 end Modular_Type_Declaration;
19545 --------------------------
19546 -- New_Concatenation_Op --
19547 --------------------------
19549 procedure New_Concatenation_Op (Typ : Entity_Id) is
19550 Loc : constant Source_Ptr := Sloc (Typ);
19553 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19554 -- Create abbreviated declaration for the formal of a predefined
19555 -- Operator 'Op' of type 'Typ'
19557 --------------------
19558 -- Make_Op_Formal --
19559 --------------------
19561 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19562 Formal : Entity_Id;
19564 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19565 Set_Etype (Formal, Typ);
19566 Set_Mechanism (Formal, Default_Mechanism);
19568 end Make_Op_Formal;
19570 -- Start of processing for New_Concatenation_Op
19573 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19575 Set_Ekind (Op, E_Operator);
19576 Set_Scope (Op, Current_Scope);
19577 Set_Etype (Op, Typ);
19578 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19579 Set_Is_Immediately_Visible (Op);
19580 Set_Is_Intrinsic_Subprogram (Op);
19581 Set_Has_Completion (Op);
19582 Append_Entity (Op, Current_Scope);
19584 Set_Name_Entity_Id (Name_Op_Concat, Op);
19586 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19587 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19588 end New_Concatenation_Op;
19590 -------------------------
19591 -- OK_For_Limited_Init --
19592 -------------------------
19594 -- ???Check all calls of this, and compare the conditions under which it's
19597 function OK_For_Limited_Init
19599 Exp : Node_Id) return Boolean
19602 return Is_CPP_Constructor_Call (Exp)
19603 or else (Ada_Version >= Ada_2005
19604 and then not Debug_Flag_Dot_L
19605 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19606 end OK_For_Limited_Init;
19608 -------------------------------
19609 -- OK_For_Limited_Init_In_05 --
19610 -------------------------------
19612 function OK_For_Limited_Init_In_05
19614 Exp : Node_Id) return Boolean
19617 -- An object of a limited interface type can be initialized with any
19618 -- expression of a nonlimited descendant type. However this does not
19619 -- apply if this is a view conversion of some other expression. This
19620 -- is checked below.
19622 if Is_Class_Wide_Type (Typ)
19623 and then Is_Limited_Interface (Typ)
19624 and then not Is_Limited_Type (Etype (Exp))
19625 and then Nkind (Exp) /= N_Type_Conversion
19630 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19631 -- case of limited aggregates (including extension aggregates), and
19632 -- function calls. The function call may have been given in prefixed
19633 -- notation, in which case the original node is an indexed component.
19634 -- If the function is parameterless, the original node was an explicit
19635 -- dereference. The function may also be parameterless, in which case
19636 -- the source node is just an identifier.
19638 -- A branch of a conditional expression may have been removed if the
19639 -- condition is statically known. This happens during expansion, and
19640 -- thus will not happen if previous errors were encountered. The check
19641 -- will have been performed on the chosen branch, which replaces the
19642 -- original conditional expression.
19648 case Nkind (Original_Node (Exp)) is
19650 | N_Extension_Aggregate
19656 when N_Identifier =>
19657 return Present (Entity (Original_Node (Exp)))
19658 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19660 when N_Qualified_Expression =>
19662 OK_For_Limited_Init_In_05
19663 (Typ, Expression (Original_Node (Exp)));
19665 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19666 -- with a function call, the expander has rewritten the call into an
19667 -- N_Type_Conversion node to force displacement of the pointer to
19668 -- reference the component containing the secondary dispatch table.
19669 -- Otherwise a type conversion is not a legal context.
19670 -- A return statement for a build-in-place function returning a
19671 -- synchronized type also introduces an unchecked conversion.
19673 when N_Type_Conversion
19674 | N_Unchecked_Type_Conversion
19676 return not Comes_From_Source (Exp)
19678 -- If the conversion has been rewritten, check Original_Node
19680 ((Original_Node (Exp) /= Exp
19682 OK_For_Limited_Init_In_05 (Typ, Original_Node (Exp)))
19684 -- Otherwise, check the expression of the compiler-generated
19685 -- conversion (which is a conversion that we want to ignore
19686 -- for purposes of the limited-initialization restrictions).
19689 (Original_Node (Exp) = Exp
19691 OK_For_Limited_Init_In_05 (Typ, Expression (Exp))));
19693 when N_Explicit_Dereference
19694 | N_Indexed_Component
19695 | N_Selected_Component
19697 return Nkind (Exp) = N_Function_Call;
19699 -- A use of 'Input is a function call, hence allowed. Normally the
19700 -- attribute will be changed to a call, but the attribute by itself
19701 -- can occur with -gnatc.
19703 when N_Attribute_Reference =>
19704 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19706 -- "return raise ..." is OK
19708 when N_Raise_Expression =>
19711 -- For a case expression, all dependent expressions must be legal
19713 when N_Case_Expression =>
19718 Alt := First (Alternatives (Original_Node (Exp)));
19719 while Present (Alt) loop
19720 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19730 -- For an if expression, all dependent expressions must be legal
19732 when N_If_Expression =>
19734 Then_Expr : constant Node_Id :=
19735 Next (First (Expressions (Original_Node (Exp))));
19736 Else_Expr : constant Node_Id := Next (Then_Expr);
19738 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19740 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19746 end OK_For_Limited_Init_In_05;
19748 -------------------------------------------
19749 -- Ordinary_Fixed_Point_Type_Declaration --
19750 -------------------------------------------
19752 procedure Ordinary_Fixed_Point_Type_Declaration
19756 Loc : constant Source_Ptr := Sloc (Def);
19757 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19758 RRS : constant Node_Id := Real_Range_Specification (Def);
19759 Implicit_Base : Entity_Id;
19766 Check_Restriction (No_Fixed_Point, Def);
19768 -- Create implicit base type
19771 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19772 Set_Etype (Implicit_Base, Implicit_Base);
19774 -- Analyze and process delta expression
19776 Analyze_And_Resolve (Delta_Expr, Any_Real);
19778 Check_Delta_Expression (Delta_Expr);
19779 Delta_Val := Expr_Value_R (Delta_Expr);
19781 Set_Delta_Value (Implicit_Base, Delta_Val);
19783 -- Compute default small from given delta, which is the largest power
19784 -- of two that does not exceed the given delta value.
19794 if Delta_Val < Ureal_1 then
19795 while Delta_Val < Tmp loop
19796 Tmp := Tmp / Ureal_2;
19797 Scale := Scale + 1;
19802 Tmp := Tmp * Ureal_2;
19803 exit when Tmp > Delta_Val;
19804 Scale := Scale - 1;
19808 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19811 Set_Small_Value (Implicit_Base, Small_Val);
19813 -- If no range was given, set a dummy range
19815 if RRS <= Empty_Or_Error then
19816 Low_Val := -Small_Val;
19817 High_Val := Small_Val;
19819 -- Otherwise analyze and process given range
19823 Low : constant Node_Id := Low_Bound (RRS);
19824 High : constant Node_Id := High_Bound (RRS);
19827 Analyze_And_Resolve (Low, Any_Real);
19828 Analyze_And_Resolve (High, Any_Real);
19829 Check_Real_Bound (Low);
19830 Check_Real_Bound (High);
19832 -- Obtain and set the range
19834 Low_Val := Expr_Value_R (Low);
19835 High_Val := Expr_Value_R (High);
19837 if Low_Val > High_Val then
19838 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19843 -- The range for both the implicit base and the declared first subtype
19844 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19845 -- set a temporary range in place. Note that the bounds of the base
19846 -- type will be widened to be symmetrical and to fill the available
19847 -- bits when the type is frozen.
19849 -- We could do this with all discrete types, and probably should, but
19850 -- we absolutely have to do it for fixed-point, since the end-points
19851 -- of the range and the size are determined by the small value, which
19852 -- could be reset before the freeze point.
19854 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19855 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19857 -- Complete definition of first subtype. The inheritance of the rep item
19858 -- chain ensures that SPARK-related pragmas are not clobbered when the
19859 -- ordinary fixed point type acts as a full view of a private type.
19861 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19862 Set_Etype (T, Implicit_Base);
19863 Init_Size_Align (T);
19864 Inherit_Rep_Item_Chain (T, Implicit_Base);
19865 Set_Small_Value (T, Small_Val);
19866 Set_Delta_Value (T, Delta_Val);
19867 Set_Is_Constrained (T);
19868 end Ordinary_Fixed_Point_Type_Declaration;
19870 ----------------------------------
19871 -- Preanalyze_Assert_Expression --
19872 ----------------------------------
19874 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19876 In_Assertion_Expr := In_Assertion_Expr + 1;
19877 Preanalyze_Spec_Expression (N, T);
19878 In_Assertion_Expr := In_Assertion_Expr - 1;
19879 end Preanalyze_Assert_Expression;
19881 -----------------------------------
19882 -- Preanalyze_Default_Expression --
19883 -----------------------------------
19885 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19886 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19887 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19890 In_Default_Expr := True;
19891 In_Spec_Expression := True;
19893 Preanalyze_With_Freezing_And_Resolve (N, T);
19895 In_Default_Expr := Save_In_Default_Expr;
19896 In_Spec_Expression := Save_In_Spec_Expression;
19897 end Preanalyze_Default_Expression;
19899 --------------------------------
19900 -- Preanalyze_Spec_Expression --
19901 --------------------------------
19903 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19904 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19906 In_Spec_Expression := True;
19907 Preanalyze_And_Resolve (N, T);
19908 In_Spec_Expression := Save_In_Spec_Expression;
19909 end Preanalyze_Spec_Expression;
19911 ----------------------------------------
19912 -- Prepare_Private_Subtype_Completion --
19913 ----------------------------------------
19915 procedure Prepare_Private_Subtype_Completion
19917 Related_Nod : Node_Id)
19919 Id_B : constant Entity_Id := Base_Type (Id);
19920 Full_B : constant Entity_Id := Full_View (Id_B);
19924 if Present (Full_B) then
19926 -- The Base_Type is already completed, we can complete the subtype
19927 -- now. We have to create a new entity with the same name, Thus we
19928 -- can't use Create_Itype.
19930 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19931 Set_Is_Itype (Full);
19932 Set_Associated_Node_For_Itype (Full, Related_Nod);
19933 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19934 Set_Full_View (Id, Full);
19937 -- The parent subtype may be private, but the base might not, in some
19938 -- nested instances. In that case, the subtype does not need to be
19939 -- exchanged. It would still be nice to make private subtypes and their
19940 -- bases consistent at all times ???
19942 if Is_Private_Type (Id_B) then
19943 Append_Elmt (Id, Private_Dependents (Id_B));
19945 end Prepare_Private_Subtype_Completion;
19947 ---------------------------
19948 -- Process_Discriminants --
19949 ---------------------------
19951 procedure Process_Discriminants
19953 Prev : Entity_Id := Empty)
19955 Elist : constant Elist_Id := New_Elmt_List;
19958 Discr_Number : Uint;
19959 Discr_Type : Entity_Id;
19960 Default_Present : Boolean := False;
19961 Default_Not_Present : Boolean := False;
19964 -- A composite type other than an array type can have discriminants.
19965 -- On entry, the current scope is the composite type.
19967 -- The discriminants are initially entered into the scope of the type
19968 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19969 -- use, as explained at the end of this procedure.
19971 Discr := First (Discriminant_Specifications (N));
19972 while Present (Discr) loop
19973 Enter_Name (Defining_Identifier (Discr));
19975 -- For navigation purposes we add a reference to the discriminant
19976 -- in the entity for the type. If the current declaration is a
19977 -- completion, place references on the partial view. Otherwise the
19978 -- type is the current scope.
19980 if Present (Prev) then
19982 -- The references go on the partial view, if present. If the
19983 -- partial view has discriminants, the references have been
19984 -- generated already.
19986 if not Has_Discriminants (Prev) then
19987 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
19991 (Current_Scope, Defining_Identifier (Discr), 'd');
19994 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
19995 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
19997 -- Ada 2005 (AI-254)
19999 if Present (Access_To_Subprogram_Definition
20000 (Discriminant_Type (Discr)))
20001 and then Protected_Present (Access_To_Subprogram_Definition
20002 (Discriminant_Type (Discr)))
20005 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
20009 Find_Type (Discriminant_Type (Discr));
20010 Discr_Type := Etype (Discriminant_Type (Discr));
20012 if Error_Posted (Discriminant_Type (Discr)) then
20013 Discr_Type := Any_Type;
20017 -- Handling of discriminants that are access types
20019 if Is_Access_Type (Discr_Type) then
20021 -- Ada 2005 (AI-230): Access discriminant allowed in non-
20022 -- limited record types
20024 if Ada_Version < Ada_2005 then
20025 Check_Access_Discriminant_Requires_Limited
20026 (Discr, Discriminant_Type (Discr));
20029 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
20031 ("(Ada 83) access discriminant not allowed", Discr);
20034 -- If not access type, must be a discrete type
20036 elsif not Is_Discrete_Type (Discr_Type) then
20038 ("discriminants must have a discrete or access type",
20039 Discriminant_Type (Discr));
20042 Set_Etype (Defining_Identifier (Discr), Discr_Type);
20044 -- If a discriminant specification includes the assignment compound
20045 -- delimiter followed by an expression, the expression is the default
20046 -- expression of the discriminant; the default expression must be of
20047 -- the type of the discriminant. (RM 3.7.1) Since this expression is
20048 -- a default expression, we do the special preanalysis, since this
20049 -- expression does not freeze (see section "Handling of Default and
20050 -- Per-Object Expressions" in spec of package Sem).
20052 if Present (Expression (Discr)) then
20053 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
20057 if Nkind (N) = N_Formal_Type_Declaration then
20059 ("discriminant defaults not allowed for formal type",
20060 Expression (Discr));
20062 -- Flag an error for a tagged type with defaulted discriminants,
20063 -- excluding limited tagged types when compiling for Ada 2012
20064 -- (see AI05-0214).
20066 elsif Is_Tagged_Type (Current_Scope)
20067 and then (not Is_Limited_Type (Current_Scope)
20068 or else Ada_Version < Ada_2012)
20069 and then Comes_From_Source (N)
20071 -- Note: see similar test in Check_Or_Process_Discriminants, to
20072 -- handle the (illegal) case of the completion of an untagged
20073 -- view with discriminants with defaults by a tagged full view.
20074 -- We skip the check if Discr does not come from source, to
20075 -- account for the case of an untagged derived type providing
20076 -- defaults for a renamed discriminant from a private untagged
20077 -- ancestor with a tagged full view (ACATS B460006).
20079 if Ada_Version >= Ada_2012 then
20081 ("discriminants of nonlimited tagged type cannot have"
20083 Expression (Discr));
20086 ("discriminants of tagged type cannot have defaults",
20087 Expression (Discr));
20091 Default_Present := True;
20092 Append_Elmt (Expression (Discr), Elist);
20094 -- Tag the defining identifiers for the discriminants with
20095 -- their corresponding default expressions from the tree.
20097 Set_Discriminant_Default_Value
20098 (Defining_Identifier (Discr), Expression (Discr));
20101 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
20102 -- gets set unless we can be sure that no range check is required.
20104 if (GNATprove_Mode or not Expander_Active)
20107 (Expression (Discr), Discr_Type, Assume_Valid => True)
20109 Set_Do_Range_Check (Expression (Discr));
20112 -- No default discriminant value given
20115 Default_Not_Present := True;
20118 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20119 -- Discr_Type but with the null-exclusion attribute
20121 if Ada_Version >= Ada_2005 then
20123 -- Ada 2005 (AI-231): Static checks
20125 if Can_Never_Be_Null (Discr_Type) then
20126 Null_Exclusion_Static_Checks (Discr);
20128 elsif Is_Access_Type (Discr_Type)
20129 and then Null_Exclusion_Present (Discr)
20131 -- No need to check itypes because in their case this check
20132 -- was done at their point of creation
20134 and then not Is_Itype (Discr_Type)
20136 if Can_Never_Be_Null (Discr_Type) then
20138 ("`NOT NULL` not allowed (& already excludes null)",
20143 Set_Etype (Defining_Identifier (Discr),
20144 Create_Null_Excluding_Itype
20146 Related_Nod => Discr));
20148 -- Check for improper null exclusion if the type is otherwise
20149 -- legal for a discriminant.
20151 elsif Null_Exclusion_Present (Discr)
20152 and then Is_Discrete_Type (Discr_Type)
20155 ("null exclusion can only apply to an access type", Discr);
20158 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20159 -- can't have defaults. Synchronized types, or types that are
20160 -- explicitly limited are fine, but special tests apply to derived
20161 -- types in generics: in a generic body we have to assume the
20162 -- worst, and therefore defaults are not allowed if the parent is
20163 -- a generic formal private type (see ACATS B370001).
20165 if Is_Access_Type (Discr_Type) and then Default_Present then
20166 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20167 or else Is_Limited_Record (Current_Scope)
20168 or else Is_Concurrent_Type (Current_Scope)
20169 or else Is_Concurrent_Record_Type (Current_Scope)
20170 or else Ekind (Current_Scope) = E_Limited_Private_Type
20172 if not Is_Derived_Type (Current_Scope)
20173 or else not Is_Generic_Type (Etype (Current_Scope))
20174 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20175 or else Limited_Present
20176 (Type_Definition (Parent (Current_Scope)))
20182 ("access discriminants of nonlimited types cannot "
20183 & "have defaults", Expression (Discr));
20186 elsif Present (Expression (Discr)) then
20188 ("(Ada 2005) access discriminants of nonlimited types "
20189 & "cannot have defaults", Expression (Discr));
20194 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(4)).
20195 -- This check is relevant only when SPARK_Mode is on as it is not a
20196 -- standard Ada legality rule.
20199 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
20201 Error_Msg_N ("discriminant cannot be volatile", Discr);
20207 -- An element list consisting of the default expressions of the
20208 -- discriminants is constructed in the above loop and used to set
20209 -- the Discriminant_Constraint attribute for the type. If an object
20210 -- is declared of this (record or task) type without any explicit
20211 -- discriminant constraint given, this element list will form the
20212 -- actual parameters for the corresponding initialization procedure
20215 Set_Discriminant_Constraint (Current_Scope, Elist);
20216 Set_Stored_Constraint (Current_Scope, No_Elist);
20218 -- Default expressions must be provided either for all or for none
20219 -- of the discriminants of a discriminant part. (RM 3.7.1)
20221 if Default_Present and then Default_Not_Present then
20223 ("incomplete specification of defaults for discriminants", N);
20226 -- The use of the name of a discriminant is not allowed in default
20227 -- expressions of a discriminant part if the specification of the
20228 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20230 -- To detect this, the discriminant names are entered initially with an
20231 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20232 -- attempt to use a void entity (for example in an expression that is
20233 -- type-checked) produces the error message: premature usage. Now after
20234 -- completing the semantic analysis of the discriminant part, we can set
20235 -- the Ekind of all the discriminants appropriately.
20237 Discr := First (Discriminant_Specifications (N));
20238 Discr_Number := Uint_1;
20239 while Present (Discr) loop
20240 Id := Defining_Identifier (Discr);
20241 Set_Ekind (Id, E_Discriminant);
20242 Init_Component_Location (Id);
20244 Set_Discriminant_Number (Id, Discr_Number);
20246 -- Make sure this is always set, even in illegal programs
20248 Set_Corresponding_Discriminant (Id, Empty);
20250 -- Initialize the Original_Record_Component to the entity itself.
20251 -- Inherit_Components will propagate the right value to
20252 -- discriminants in derived record types.
20254 Set_Original_Record_Component (Id, Id);
20256 -- Create the discriminal for the discriminant
20258 Build_Discriminal (Id);
20261 Discr_Number := Discr_Number + 1;
20264 Set_Has_Discriminants (Current_Scope);
20265 end Process_Discriminants;
20267 -----------------------
20268 -- Process_Full_View --
20269 -----------------------
20271 -- WARNING: This routine manages Ghost regions. Return statements must be
20272 -- replaced by gotos which jump to the end of the routine and restore the
20275 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20276 procedure Collect_Implemented_Interfaces
20278 Ifaces : Elist_Id);
20279 -- Ada 2005: Gather all the interfaces that Typ directly or
20280 -- inherently implements. Duplicate entries are not added to
20281 -- the list Ifaces.
20283 ------------------------------------
20284 -- Collect_Implemented_Interfaces --
20285 ------------------------------------
20287 procedure Collect_Implemented_Interfaces
20292 Iface_Elmt : Elmt_Id;
20295 -- Abstract interfaces are only associated with tagged record types
20297 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20301 -- Recursively climb to the ancestors
20303 if Etype (Typ) /= Typ
20305 -- Protect the frontend against wrong cyclic declarations like:
20307 -- type B is new A with private;
20308 -- type C is new A with private;
20310 -- type B is new C with null record;
20311 -- type C is new B with null record;
20313 and then Etype (Typ) /= Priv_T
20314 and then Etype (Typ) /= Full_T
20316 -- Keep separate the management of private type declarations
20318 if Ekind (Typ) = E_Record_Type_With_Private then
20320 -- Handle the following illegal usage:
20321 -- type Private_Type is tagged private;
20323 -- type Private_Type is new Type_Implementing_Iface;
20325 if Present (Full_View (Typ))
20326 and then Etype (Typ) /= Full_View (Typ)
20328 if Is_Interface (Etype (Typ)) then
20329 Append_Unique_Elmt (Etype (Typ), Ifaces);
20332 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20335 -- Non-private types
20338 if Is_Interface (Etype (Typ)) then
20339 Append_Unique_Elmt (Etype (Typ), Ifaces);
20342 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20346 -- Handle entities in the list of abstract interfaces
20348 if Present (Interfaces (Typ)) then
20349 Iface_Elmt := First_Elmt (Interfaces (Typ));
20350 while Present (Iface_Elmt) loop
20351 Iface := Node (Iface_Elmt);
20353 pragma Assert (Is_Interface (Iface));
20355 if not Contain_Interface (Iface, Ifaces) then
20356 Append_Elmt (Iface, Ifaces);
20357 Collect_Implemented_Interfaces (Iface, Ifaces);
20360 Next_Elmt (Iface_Elmt);
20363 end Collect_Implemented_Interfaces;
20367 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20368 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
20369 -- Save the Ghost-related attributes to restore on exit
20371 Full_Indic : Node_Id;
20372 Full_Parent : Entity_Id;
20373 Priv_Parent : Entity_Id;
20375 -- Start of processing for Process_Full_View
20378 Mark_And_Set_Ghost_Completion (N, Priv_T);
20380 -- First some sanity checks that must be done after semantic
20381 -- decoration of the full view and thus cannot be placed with other
20382 -- similar checks in Find_Type_Name
20384 if not Is_Limited_Type (Priv_T)
20385 and then (Is_Limited_Type (Full_T)
20386 or else Is_Limited_Composite (Full_T))
20388 if In_Instance then
20392 ("completion of nonlimited type cannot be limited", Full_T);
20393 Explain_Limited_Type (Full_T, Full_T);
20396 elsif Is_Abstract_Type (Full_T)
20397 and then not Is_Abstract_Type (Priv_T)
20400 ("completion of nonabstract type cannot be abstract", Full_T);
20402 elsif Is_Tagged_Type (Priv_T)
20403 and then Is_Limited_Type (Priv_T)
20404 and then not Is_Limited_Type (Full_T)
20406 -- If pragma CPP_Class was applied to the private declaration
20407 -- propagate the limitedness to the full-view
20409 if Is_CPP_Class (Priv_T) then
20410 Set_Is_Limited_Record (Full_T);
20412 -- GNAT allow its own definition of Limited_Controlled to disobey
20413 -- this rule in order in ease the implementation. This test is safe
20414 -- because Root_Controlled is defined in a child of System that
20415 -- normal programs are not supposed to use.
20417 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20418 Set_Is_Limited_Composite (Full_T);
20421 ("completion of limited tagged type must be limited", Full_T);
20424 elsif Is_Generic_Type (Priv_T) then
20425 Error_Msg_N ("generic type cannot have a completion", Full_T);
20428 -- Check that ancestor interfaces of private and full views are
20429 -- consistent. We omit this check for synchronized types because
20430 -- they are performed on the corresponding record type when frozen.
20432 if Ada_Version >= Ada_2005
20433 and then Is_Tagged_Type (Priv_T)
20434 and then Is_Tagged_Type (Full_T)
20435 and then not Is_Concurrent_Type (Full_T)
20439 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20440 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20443 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20444 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20446 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20447 -- an interface type if and only if the full type is descendant
20448 -- of the interface type (AARM 7.3 (7.3/2)).
20450 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20452 if Present (Iface) then
20454 ("interface in partial view& not implemented by full type "
20455 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20458 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20460 if Present (Iface) then
20462 ("interface & not implemented by partial view "
20463 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20468 if Is_Tagged_Type (Priv_T)
20469 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20470 and then Is_Derived_Type (Full_T)
20472 Priv_Parent := Etype (Priv_T);
20474 -- The full view of a private extension may have been transformed
20475 -- into an unconstrained derived type declaration and a subtype
20476 -- declaration (see build_derived_record_type for details).
20478 if Nkind (N) = N_Subtype_Declaration then
20479 Full_Indic := Subtype_Indication (N);
20480 Full_Parent := Etype (Base_Type (Full_T));
20482 Full_Indic := Subtype_Indication (Type_Definition (N));
20483 Full_Parent := Etype (Full_T);
20486 -- Check that the parent type of the full type is a descendant of
20487 -- the ancestor subtype given in the private extension. If either
20488 -- entity has an Etype equal to Any_Type then we had some previous
20489 -- error situation [7.3(8)].
20491 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20494 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20495 -- any order. Therefore we don't have to check that its parent must
20496 -- be a descendant of the parent of the private type declaration.
20498 elsif Is_Interface (Priv_Parent)
20499 and then Is_Interface (Full_Parent)
20503 -- Ada 2005 (AI-251): If the parent of the private type declaration
20504 -- is an interface there is no need to check that it is an ancestor
20505 -- of the associated full type declaration. The required tests for
20506 -- this case are performed by Build_Derived_Record_Type.
20508 elsif not Is_Interface (Base_Type (Priv_Parent))
20509 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20512 ("parent of full type must descend from parent of private "
20513 & "extension", Full_Indic);
20515 -- First check a formal restriction, and then proceed with checking
20516 -- Ada rules. Since the formal restriction is not a serious error, we
20517 -- don't prevent further error detection for this check, hence the
20521 -- In formal mode, when completing a private extension the type
20522 -- named in the private part must be exactly the same as that
20523 -- named in the visible part.
20525 if Priv_Parent /= Full_Parent then
20526 Error_Msg_Name_1 := Chars (Priv_Parent);
20527 Check_SPARK_05_Restriction ("% expected", Full_Indic);
20530 -- Check the rules of 7.3(10): if the private extension inherits
20531 -- known discriminants, then the full type must also inherit those
20532 -- discriminants from the same (ancestor) type, and the parent
20533 -- subtype of the full type must be constrained if and only if
20534 -- the ancestor subtype of the private extension is constrained.
20536 if No (Discriminant_Specifications (Parent (Priv_T)))
20537 and then not Has_Unknown_Discriminants (Priv_T)
20538 and then Has_Discriminants (Base_Type (Priv_Parent))
20541 Priv_Indic : constant Node_Id :=
20542 Subtype_Indication (Parent (Priv_T));
20544 Priv_Constr : constant Boolean :=
20545 Is_Constrained (Priv_Parent)
20547 Nkind (Priv_Indic) = N_Subtype_Indication
20549 Is_Constrained (Entity (Priv_Indic));
20551 Full_Constr : constant Boolean :=
20552 Is_Constrained (Full_Parent)
20554 Nkind (Full_Indic) = N_Subtype_Indication
20556 Is_Constrained (Entity (Full_Indic));
20558 Priv_Discr : Entity_Id;
20559 Full_Discr : Entity_Id;
20562 Priv_Discr := First_Discriminant (Priv_Parent);
20563 Full_Discr := First_Discriminant (Full_Parent);
20564 while Present (Priv_Discr) and then Present (Full_Discr) loop
20565 if Original_Record_Component (Priv_Discr) =
20566 Original_Record_Component (Full_Discr)
20568 Corresponding_Discriminant (Priv_Discr) =
20569 Corresponding_Discriminant (Full_Discr)
20576 Next_Discriminant (Priv_Discr);
20577 Next_Discriminant (Full_Discr);
20580 if Present (Priv_Discr) or else Present (Full_Discr) then
20582 ("full view must inherit discriminants of the parent "
20583 & "type used in the private extension", Full_Indic);
20585 elsif Priv_Constr and then not Full_Constr then
20587 ("parent subtype of full type must be constrained",
20590 elsif Full_Constr and then not Priv_Constr then
20592 ("parent subtype of full type must be unconstrained",
20597 -- Check the rules of 7.3(12): if a partial view has neither
20598 -- known or unknown discriminants, then the full type
20599 -- declaration shall define a definite subtype.
20601 elsif not Has_Unknown_Discriminants (Priv_T)
20602 and then not Has_Discriminants (Priv_T)
20603 and then not Is_Constrained (Full_T)
20606 ("full view must define a constrained type if partial view "
20607 & "has no discriminants", Full_T);
20610 -- ??????? Do we implement the following properly ?????
20611 -- If the ancestor subtype of a private extension has constrained
20612 -- discriminants, then the parent subtype of the full view shall
20613 -- impose a statically matching constraint on those discriminants
20618 -- For untagged types, verify that a type without discriminants is
20619 -- not completed with an unconstrained type. A separate error message
20620 -- is produced if the full type has defaulted discriminants.
20622 if Is_Definite_Subtype (Priv_T)
20623 and then not Is_Definite_Subtype (Full_T)
20625 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20627 ("full view of& not compatible with declaration#",
20630 if not Is_Tagged_Type (Full_T) then
20632 ("\one is constrained, the other unconstrained", Full_T);
20637 -- AI-419: verify that the use of "limited" is consistent
20640 Orig_Decl : constant Node_Id := Original_Node (N);
20643 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20644 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20646 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20648 if not Limited_Present (Parent (Priv_T))
20649 and then not Synchronized_Present (Parent (Priv_T))
20650 and then Limited_Present (Type_Definition (Orig_Decl))
20653 ("full view of non-limited extension cannot be limited", N);
20655 -- Conversely, if the partial view carries the limited keyword,
20656 -- the full view must as well, even if it may be redundant.
20658 elsif Limited_Present (Parent (Priv_T))
20659 and then not Limited_Present (Type_Definition (Orig_Decl))
20662 ("full view of limited extension must be explicitly limited",
20668 -- Ada 2005 (AI-443): A synchronized private extension must be
20669 -- completed by a task or protected type.
20671 if Ada_Version >= Ada_2005
20672 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20673 and then Synchronized_Present (Parent (Priv_T))
20674 and then not Is_Concurrent_Type (Full_T)
20676 Error_Msg_N ("full view of synchronized extension must " &
20677 "be synchronized type", N);
20680 -- Ada 2005 AI-363: if the full view has discriminants with
20681 -- defaults, it is illegal to declare constrained access subtypes
20682 -- whose designated type is the current type. This allows objects
20683 -- of the type that are declared in the heap to be unconstrained.
20685 if not Has_Unknown_Discriminants (Priv_T)
20686 and then not Has_Discriminants (Priv_T)
20687 and then Has_Discriminants (Full_T)
20689 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20691 Set_Has_Constrained_Partial_View (Full_T);
20692 Set_Has_Constrained_Partial_View (Priv_T);
20695 -- Create a full declaration for all its subtypes recorded in
20696 -- Private_Dependents and swap them similarly to the base type. These
20697 -- are subtypes that have been define before the full declaration of
20698 -- the private type. We also swap the entry in Private_Dependents list
20699 -- so we can properly restore the private view on exit from the scope.
20702 Priv_Elmt : Elmt_Id;
20703 Priv_Scop : Entity_Id;
20708 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20709 while Present (Priv_Elmt) loop
20710 Priv := Node (Priv_Elmt);
20711 Priv_Scop := Scope (Priv);
20713 if Ekind_In (Priv, E_Private_Subtype,
20714 E_Limited_Private_Subtype,
20715 E_Record_Subtype_With_Private)
20717 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20718 Set_Is_Itype (Full);
20719 Set_Parent (Full, Parent (Priv));
20720 Set_Associated_Node_For_Itype (Full, N);
20722 -- Now we need to complete the private subtype, but since the
20723 -- base type has already been swapped, we must also swap the
20724 -- subtypes (and thus, reverse the arguments in the call to
20725 -- Complete_Private_Subtype). Also note that we may need to
20726 -- re-establish the scope of the private subtype.
20728 Copy_And_Swap (Priv, Full);
20730 if not In_Open_Scopes (Priv_Scop) then
20731 Push_Scope (Priv_Scop);
20734 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20736 Priv_Scop := Empty;
20739 Complete_Private_Subtype (Full, Priv, Full_T, N);
20740 Set_Full_View (Full, Priv);
20742 if Present (Priv_Scop) then
20746 Replace_Elmt (Priv_Elmt, Full);
20749 Next_Elmt (Priv_Elmt);
20753 -- If the private view was tagged, copy the new primitive operations
20754 -- from the private view to the full view.
20756 if Is_Tagged_Type (Full_T) then
20758 Disp_Typ : Entity_Id;
20759 Full_List : Elist_Id;
20761 Prim_Elmt : Elmt_Id;
20762 Priv_List : Elist_Id;
20766 L : Elist_Id) return Boolean;
20767 -- Determine whether list L contains element E
20775 L : Elist_Id) return Boolean
20777 List_Elmt : Elmt_Id;
20780 List_Elmt := First_Elmt (L);
20781 while Present (List_Elmt) loop
20782 if Node (List_Elmt) = E then
20786 Next_Elmt (List_Elmt);
20792 -- Start of processing
20795 if Is_Tagged_Type (Priv_T) then
20796 Priv_List := Primitive_Operations (Priv_T);
20797 Prim_Elmt := First_Elmt (Priv_List);
20799 -- In the case of a concurrent type completing a private tagged
20800 -- type, primitives may have been declared in between the two
20801 -- views. These subprograms need to be wrapped the same way
20802 -- entries and protected procedures are handled because they
20803 -- cannot be directly shared by the two views.
20805 if Is_Concurrent_Type (Full_T) then
20807 Conc_Typ : constant Entity_Id :=
20808 Corresponding_Record_Type (Full_T);
20809 Curr_Nod : Node_Id := Parent (Conc_Typ);
20810 Wrap_Spec : Node_Id;
20813 while Present (Prim_Elmt) loop
20814 Prim := Node (Prim_Elmt);
20816 if Comes_From_Source (Prim)
20817 and then not Is_Abstract_Subprogram (Prim)
20820 Make_Subprogram_Declaration (Sloc (Prim),
20824 Obj_Typ => Conc_Typ,
20826 Parameter_Specifications
20829 Insert_After (Curr_Nod, Wrap_Spec);
20830 Curr_Nod := Wrap_Spec;
20832 Analyze (Wrap_Spec);
20834 -- Remove the wrapper from visibility to avoid
20835 -- spurious conflict with the wrapped entity.
20837 Set_Is_Immediately_Visible
20838 (Defining_Entity (Specification (Wrap_Spec)),
20842 Next_Elmt (Prim_Elmt);
20848 -- For non-concurrent types, transfer explicit primitives, but
20849 -- omit those inherited from the parent of the private view
20850 -- since they will be re-inherited later on.
20853 Full_List := Primitive_Operations (Full_T);
20854 while Present (Prim_Elmt) loop
20855 Prim := Node (Prim_Elmt);
20857 if Comes_From_Source (Prim)
20858 and then not Contains (Prim, Full_List)
20860 Append_Elmt (Prim, Full_List);
20863 Next_Elmt (Prim_Elmt);
20867 -- Untagged private view
20870 Full_List := Primitive_Operations (Full_T);
20872 -- In this case the partial view is untagged, so here we locate
20873 -- all of the earlier primitives that need to be treated as
20874 -- dispatching (those that appear between the two views). Note
20875 -- that these additional operations must all be new operations
20876 -- (any earlier operations that override inherited operations
20877 -- of the full view will already have been inserted in the
20878 -- primitives list, marked by Check_Operation_From_Private_View
20879 -- as dispatching. Note that implicit "/=" operators are
20880 -- excluded from being added to the primitives list since they
20881 -- shouldn't be treated as dispatching (tagged "/=" is handled
20884 Prim := Next_Entity (Full_T);
20885 while Present (Prim) and then Prim /= Priv_T loop
20886 if Ekind_In (Prim, E_Procedure, E_Function) then
20887 Disp_Typ := Find_Dispatching_Type (Prim);
20889 if Disp_Typ = Full_T
20890 and then (Chars (Prim) /= Name_Op_Ne
20891 or else Comes_From_Source (Prim))
20893 Check_Controlling_Formals (Full_T, Prim);
20895 if Is_Suitable_Primitive (Prim)
20896 and then not Is_Dispatching_Operation (Prim)
20898 Append_Elmt (Prim, Full_List);
20899 Set_Is_Dispatching_Operation (Prim);
20900 Set_DT_Position_Value (Prim, No_Uint);
20903 elsif Is_Dispatching_Operation (Prim)
20904 and then Disp_Typ /= Full_T
20906 -- Verify that it is not otherwise controlled by a
20907 -- formal or a return value of type T.
20909 Check_Controlling_Formals (Disp_Typ, Prim);
20913 Next_Entity (Prim);
20917 -- For the tagged case, the two views can share the same primitive
20918 -- operations list and the same class-wide type. Update attributes
20919 -- of the class-wide type which depend on the full declaration.
20921 if Is_Tagged_Type (Priv_T) then
20922 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20923 Set_Class_Wide_Type
20924 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20926 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20931 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20933 if Known_To_Have_Preelab_Init (Priv_T) then
20935 -- Case where there is a pragma Preelaborable_Initialization. We
20936 -- always allow this in predefined units, which is cheating a bit,
20937 -- but it means we don't have to struggle to meet the requirements in
20938 -- the RM for having Preelaborable Initialization. Otherwise we
20939 -- require that the type meets the RM rules. But we can't check that
20940 -- yet, because of the rule about overriding Initialize, so we simply
20941 -- set a flag that will be checked at freeze time.
20943 if not In_Predefined_Unit (Full_T) then
20944 Set_Must_Have_Preelab_Init (Full_T);
20948 -- If pragma CPP_Class was applied to the private type declaration,
20949 -- propagate it now to the full type declaration.
20951 if Is_CPP_Class (Priv_T) then
20952 Set_Is_CPP_Class (Full_T);
20953 Set_Convention (Full_T, Convention_CPP);
20955 -- Check that components of imported CPP types do not have default
20958 Check_CPP_Type_Has_No_Defaults (Full_T);
20961 -- If the private view has user specified stream attributes, then so has
20964 -- Why the test, how could these flags be already set in Full_T ???
20966 if Has_Specified_Stream_Read (Priv_T) then
20967 Set_Has_Specified_Stream_Read (Full_T);
20970 if Has_Specified_Stream_Write (Priv_T) then
20971 Set_Has_Specified_Stream_Write (Full_T);
20974 if Has_Specified_Stream_Input (Priv_T) then
20975 Set_Has_Specified_Stream_Input (Full_T);
20978 if Has_Specified_Stream_Output (Priv_T) then
20979 Set_Has_Specified_Stream_Output (Full_T);
20982 -- Propagate Default_Initial_Condition-related attributes from the
20983 -- partial view to the full view and its base type.
20985 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20986 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20988 -- Propagate invariant-related attributes from the partial view to the
20989 -- full view and its base type.
20991 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
20992 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
20994 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
20995 -- in the full view without advertising the inheritance in the partial
20996 -- view. This can only occur when the partial view has no parent type
20997 -- and the full view has an interface as a parent. Any other scenarios
20998 -- are illegal because implemented interfaces must match between the
21001 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
21003 Full_Par : constant Entity_Id := Etype (Full_T);
21004 Priv_Par : constant Entity_Id := Etype (Priv_T);
21007 if not Is_Interface (Priv_Par)
21008 and then Is_Interface (Full_Par)
21009 and then Has_Inheritable_Invariants (Full_Par)
21012 ("hidden inheritance of class-wide type invariants not "
21018 -- Propagate predicates to full type, and predicate function if already
21019 -- defined. It is not clear that this can actually happen? the partial
21020 -- view cannot be frozen yet, and the predicate function has not been
21021 -- built. Still it is a cheap check and seems safer to make it.
21023 if Has_Predicates (Priv_T) then
21024 Set_Has_Predicates (Full_T);
21026 if Present (Predicate_Function (Priv_T)) then
21027 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
21032 Restore_Ghost_Region (Saved_GM, Saved_IGR);
21033 end Process_Full_View;
21035 -----------------------------------
21036 -- Process_Incomplete_Dependents --
21037 -----------------------------------
21039 procedure Process_Incomplete_Dependents
21041 Full_T : Entity_Id;
21044 Inc_Elmt : Elmt_Id;
21045 Priv_Dep : Entity_Id;
21046 New_Subt : Entity_Id;
21048 Disc_Constraint : Elist_Id;
21051 if No (Private_Dependents (Inc_T)) then
21055 -- Itypes that may be generated by the completion of an incomplete
21056 -- subtype are not used by the back-end and not attached to the tree.
21057 -- They are created only for constraint-checking purposes.
21059 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
21060 while Present (Inc_Elmt) loop
21061 Priv_Dep := Node (Inc_Elmt);
21063 if Ekind (Priv_Dep) = E_Subprogram_Type then
21065 -- An Access_To_Subprogram type may have a return type or a
21066 -- parameter type that is incomplete. Replace with the full view.
21068 if Etype (Priv_Dep) = Inc_T then
21069 Set_Etype (Priv_Dep, Full_T);
21073 Formal : Entity_Id;
21076 Formal := First_Formal (Priv_Dep);
21077 while Present (Formal) loop
21078 if Etype (Formal) = Inc_T then
21079 Set_Etype (Formal, Full_T);
21082 Next_Formal (Formal);
21086 elsif Is_Overloadable (Priv_Dep) then
21088 -- If a subprogram in the incomplete dependents list is primitive
21089 -- for a tagged full type then mark it as a dispatching operation,
21090 -- check whether it overrides an inherited subprogram, and check
21091 -- restrictions on its controlling formals. Note that a protected
21092 -- operation is never dispatching: only its wrapper operation
21093 -- (which has convention Ada) is.
21095 if Is_Tagged_Type (Full_T)
21096 and then Is_Primitive (Priv_Dep)
21097 and then Convention (Priv_Dep) /= Convention_Protected
21099 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21100 Set_Is_Dispatching_Operation (Priv_Dep);
21101 Check_Controlling_Formals (Full_T, Priv_Dep);
21104 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21106 -- Can happen during processing of a body before the completion
21107 -- of a TA type. Ignore, because spec is also on dependent list.
21111 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21112 -- corresponding subtype of the full view.
21114 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21115 and then Comes_From_Source (Priv_Dep)
21117 Set_Subtype_Indication
21118 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21119 Set_Etype (Priv_Dep, Full_T);
21120 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21121 Set_Analyzed (Parent (Priv_Dep), False);
21123 -- Reanalyze the declaration, suppressing the call to Enter_Name
21124 -- to avoid duplicate names.
21126 Analyze_Subtype_Declaration
21127 (N => Parent (Priv_Dep),
21130 -- Dependent is a subtype
21133 -- We build a new subtype indication using the full view of the
21134 -- incomplete parent. The discriminant constraints have been
21135 -- elaborated already at the point of the subtype declaration.
21137 New_Subt := Create_Itype (E_Void, N);
21139 if Has_Discriminants (Full_T) then
21140 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21142 Disc_Constraint := No_Elist;
21145 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21146 Set_Full_View (Priv_Dep, New_Subt);
21149 Next_Elmt (Inc_Elmt);
21151 end Process_Incomplete_Dependents;
21153 --------------------------------
21154 -- Process_Range_Expr_In_Decl --
21155 --------------------------------
21157 procedure Process_Range_Expr_In_Decl
21160 Subtyp : Entity_Id := Empty;
21161 Check_List : List_Id := Empty_List;
21162 R_Check_Off : Boolean := False;
21163 In_Iter_Schm : Boolean := False)
21166 R_Checks : Check_Result;
21167 Insert_Node : Node_Id;
21168 Def_Id : Entity_Id;
21171 Analyze_And_Resolve (R, Base_Type (T));
21173 if Nkind (R) = N_Range then
21175 -- In SPARK, all ranges should be static, with the exception of the
21176 -- discrete type definition of a loop parameter specification.
21178 if not In_Iter_Schm
21179 and then not Is_OK_Static_Range (R)
21181 Check_SPARK_05_Restriction ("range should be static", R);
21184 Lo := Low_Bound (R);
21185 Hi := High_Bound (R);
21187 -- Validity checks on the range of a quantified expression are
21188 -- delayed until the construct is transformed into a loop.
21190 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21191 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21195 -- We need to ensure validity of the bounds here, because if we
21196 -- go ahead and do the expansion, then the expanded code will get
21197 -- analyzed with range checks suppressed and we miss the check.
21199 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21200 -- the temporaries generated by routine Remove_Side_Effects by means
21201 -- of validity checks must use the same names. When a range appears
21202 -- in the parent of a generic, the range is processed with checks
21203 -- disabled as part of the generic context and with checks enabled
21204 -- for code generation purposes. This leads to link issues as the
21205 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21206 -- template sees the temporaries generated by Remove_Side_Effects.
21209 Validity_Check_Range (R, Subtyp);
21212 -- If there were errors in the declaration, try and patch up some
21213 -- common mistakes in the bounds. The cases handled are literals
21214 -- which are Integer where the expected type is Real and vice versa.
21215 -- These corrections allow the compilation process to proceed further
21216 -- along since some basic assumptions of the format of the bounds
21219 if Etype (R) = Any_Type then
21220 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21222 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21224 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21226 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21228 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21230 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21232 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21234 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21241 -- If the bounds of the range have been mistakenly given as string
21242 -- literals (perhaps in place of character literals), then an error
21243 -- has already been reported, but we rewrite the string literal as a
21244 -- bound of the range's type to avoid blowups in later processing
21245 -- that looks at static values.
21247 if Nkind (Lo) = N_String_Literal then
21249 Make_Attribute_Reference (Sloc (Lo),
21250 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21251 Attribute_Name => Name_First));
21252 Analyze_And_Resolve (Lo);
21255 if Nkind (Hi) = N_String_Literal then
21257 Make_Attribute_Reference (Sloc (Hi),
21258 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21259 Attribute_Name => Name_First));
21260 Analyze_And_Resolve (Hi);
21263 -- If bounds aren't scalar at this point then exit, avoiding
21264 -- problems with further processing of the range in this procedure.
21266 if not Is_Scalar_Type (Etype (Lo)) then
21270 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21271 -- then range of the base type. Here we check whether the bounds
21272 -- are in the range of the subtype itself. Note that if the bounds
21273 -- represent the null range the Constraint_Error exception should
21276 -- ??? The following code should be cleaned up as follows
21278 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21279 -- is done in the call to Range_Check (R, T); below
21281 -- 2. The use of R_Check_Off should be investigated and possibly
21282 -- removed, this would clean up things a bit.
21284 if Is_Null_Range (Lo, Hi) then
21288 -- Capture values of bounds and generate temporaries for them
21289 -- if needed, before applying checks, since checks may cause
21290 -- duplication of the expression without forcing evaluation.
21292 -- The forced evaluation removes side effects from expressions,
21293 -- which should occur also in GNATprove mode. Otherwise, we end up
21294 -- with unexpected insertions of actions at places where this is
21295 -- not supposed to occur, e.g. on default parameters of a call.
21297 if Expander_Active or GNATprove_Mode then
21299 -- Call Force_Evaluation to create declarations as needed to
21300 -- deal with side effects, and also create typ_FIRST/LAST
21301 -- entities for bounds if we have a subtype name.
21303 -- Note: we do this transformation even if expansion is not
21304 -- active if we are in GNATprove_Mode since the transformation
21305 -- is in general required to ensure that the resulting tree has
21306 -- proper Ada semantics.
21309 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21311 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21314 -- We use a flag here instead of suppressing checks on the type
21315 -- because the type we check against isn't necessarily the place
21316 -- where we put the check.
21318 if not R_Check_Off then
21319 R_Checks := Get_Range_Checks (R, T);
21321 -- Look up tree to find an appropriate insertion point. We
21322 -- can't just use insert_actions because later processing
21323 -- depends on the insertion node. Prior to Ada 2012 the
21324 -- insertion point could only be a declaration or a loop, but
21325 -- quantified expressions can appear within any context in an
21326 -- expression, and the insertion point can be any statement,
21327 -- pragma, or declaration.
21329 Insert_Node := Parent (R);
21330 while Present (Insert_Node) loop
21332 Nkind (Insert_Node) in N_Declaration
21335 (Insert_Node, N_Component_Declaration,
21336 N_Loop_Parameter_Specification,
21337 N_Function_Specification,
21338 N_Procedure_Specification);
21340 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21341 or else Nkind (Insert_Node) in
21342 N_Statement_Other_Than_Procedure_Call
21343 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21346 Insert_Node := Parent (Insert_Node);
21349 -- Why would Type_Decl not be present??? Without this test,
21350 -- short regression tests fail.
21352 if Present (Insert_Node) then
21354 -- Case of loop statement. Verify that the range is part
21355 -- of the subtype indication of the iteration scheme.
21357 if Nkind (Insert_Node) = N_Loop_Statement then
21362 Indic := Parent (R);
21363 while Present (Indic)
21364 and then Nkind (Indic) /= N_Subtype_Indication
21366 Indic := Parent (Indic);
21369 if Present (Indic) then
21370 Def_Id := Etype (Subtype_Mark (Indic));
21372 Insert_Range_Checks
21376 Sloc (Insert_Node),
21378 Do_Before => True);
21382 -- Insertion before a declaration. If the declaration
21383 -- includes discriminants, the list of applicable checks
21384 -- is given by the caller.
21386 elsif Nkind (Insert_Node) in N_Declaration then
21387 Def_Id := Defining_Identifier (Insert_Node);
21389 if (Ekind (Def_Id) = E_Record_Type
21390 and then Depends_On_Discriminant (R))
21392 (Ekind (Def_Id) = E_Protected_Type
21393 and then Has_Discriminants (Def_Id))
21395 Append_Range_Checks
21397 Check_List, Def_Id, Sloc (Insert_Node), R);
21400 Insert_Range_Checks
21402 Insert_Node, Def_Id, Sloc (Insert_Node), R);
21406 -- Insertion before a statement. Range appears in the
21407 -- context of a quantified expression. Insertion will
21408 -- take place when expression is expanded.
21417 -- Case of other than an explicit N_Range node
21419 -- The forced evaluation removes side effects from expressions, which
21420 -- should occur also in GNATprove mode. Otherwise, we end up with
21421 -- unexpected insertions of actions at places where this is not
21422 -- supposed to occur, e.g. on default parameters of a call.
21424 elsif Expander_Active or GNATprove_Mode then
21425 Get_Index_Bounds (R, Lo, Hi);
21426 Force_Evaluation (Lo);
21427 Force_Evaluation (Hi);
21429 end Process_Range_Expr_In_Decl;
21431 --------------------------------------
21432 -- Process_Real_Range_Specification --
21433 --------------------------------------
21435 procedure Process_Real_Range_Specification (Def : Node_Id) is
21436 Spec : constant Node_Id := Real_Range_Specification (Def);
21439 Err : Boolean := False;
21441 procedure Analyze_Bound (N : Node_Id);
21442 -- Analyze and check one bound
21444 -------------------
21445 -- Analyze_Bound --
21446 -------------------
21448 procedure Analyze_Bound (N : Node_Id) is
21450 Analyze_And_Resolve (N, Any_Real);
21452 if not Is_OK_Static_Expression (N) then
21453 Flag_Non_Static_Expr
21454 ("bound in real type definition is not static!", N);
21459 -- Start of processing for Process_Real_Range_Specification
21462 if Present (Spec) then
21463 Lo := Low_Bound (Spec);
21464 Hi := High_Bound (Spec);
21465 Analyze_Bound (Lo);
21466 Analyze_Bound (Hi);
21468 -- If error, clear away junk range specification
21471 Set_Real_Range_Specification (Def, Empty);
21474 end Process_Real_Range_Specification;
21476 ---------------------
21477 -- Process_Subtype --
21478 ---------------------
21480 function Process_Subtype
21482 Related_Nod : Node_Id;
21483 Related_Id : Entity_Id := Empty;
21484 Suffix : Character := ' ') return Entity_Id
21487 Def_Id : Entity_Id;
21488 Error_Node : Node_Id;
21489 Full_View_Id : Entity_Id;
21490 Subtype_Mark_Id : Entity_Id;
21492 May_Have_Null_Exclusion : Boolean;
21494 procedure Check_Incomplete (T : Node_Id);
21495 -- Called to verify that an incomplete type is not used prematurely
21497 ----------------------
21498 -- Check_Incomplete --
21499 ----------------------
21501 procedure Check_Incomplete (T : Node_Id) is
21503 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21505 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21507 not (Ada_Version >= Ada_2005
21509 (Nkind (Parent (T)) = N_Subtype_Declaration
21510 or else (Nkind (Parent (T)) = N_Subtype_Indication
21511 and then Nkind (Parent (Parent (T))) =
21512 N_Subtype_Declaration)))
21514 Error_Msg_N ("invalid use of type before its full declaration", T);
21516 end Check_Incomplete;
21518 -- Start of processing for Process_Subtype
21521 -- Case of no constraints present
21523 if Nkind (S) /= N_Subtype_Indication then
21526 -- No way to proceed if the subtype indication is malformed. This
21527 -- will happen for example when the subtype indication in an object
21528 -- declaration is missing altogether and the expression is analyzed
21529 -- as if it were that indication.
21531 if not Is_Entity_Name (S) then
21535 Check_Incomplete (S);
21538 -- Ada 2005 (AI-231): Static check
21540 if Ada_Version >= Ada_2005
21541 and then Present (P)
21542 and then Null_Exclusion_Present (P)
21543 and then Nkind (P) /= N_Access_To_Object_Definition
21544 and then not Is_Access_Type (Entity (S))
21546 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21549 -- The following is ugly, can't we have a range or even a flag???
21551 May_Have_Null_Exclusion :=
21552 Nkind_In (P, N_Access_Definition,
21553 N_Access_Function_Definition,
21554 N_Access_Procedure_Definition,
21555 N_Access_To_Object_Definition,
21557 N_Component_Definition)
21559 Nkind_In (P, N_Derived_Type_Definition,
21560 N_Discriminant_Specification,
21561 N_Formal_Object_Declaration,
21562 N_Object_Declaration,
21563 N_Object_Renaming_Declaration,
21564 N_Parameter_Specification,
21565 N_Subtype_Declaration);
21567 -- Create an Itype that is a duplicate of Entity (S) but with the
21568 -- null-exclusion attribute.
21570 if May_Have_Null_Exclusion
21571 and then Is_Access_Type (Entity (S))
21572 and then Null_Exclusion_Present (P)
21574 -- No need to check the case of an access to object definition.
21575 -- It is correct to define double not-null pointers.
21578 -- type Not_Null_Int_Ptr is not null access Integer;
21579 -- type Acc is not null access Not_Null_Int_Ptr;
21581 and then Nkind (P) /= N_Access_To_Object_Definition
21583 if Can_Never_Be_Null (Entity (S)) then
21584 case Nkind (Related_Nod) is
21585 when N_Full_Type_Declaration =>
21586 if Nkind (Type_Definition (Related_Nod))
21587 in N_Array_Type_Definition
21591 (Component_Definition
21592 (Type_Definition (Related_Nod)));
21595 Subtype_Indication (Type_Definition (Related_Nod));
21598 when N_Subtype_Declaration =>
21599 Error_Node := Subtype_Indication (Related_Nod);
21601 when N_Object_Declaration =>
21602 Error_Node := Object_Definition (Related_Nod);
21604 when N_Component_Declaration =>
21606 Subtype_Indication (Component_Definition (Related_Nod));
21608 when N_Allocator =>
21609 Error_Node := Expression (Related_Nod);
21612 pragma Assert (False);
21613 Error_Node := Related_Nod;
21617 ("`NOT NULL` not allowed (& already excludes null)",
21623 Create_Null_Excluding_Itype
21625 Related_Nod => P));
21626 Set_Entity (S, Etype (S));
21631 -- Case of constraint present, so that we have an N_Subtype_Indication
21632 -- node (this node is created only if constraints are present).
21635 Find_Type (Subtype_Mark (S));
21637 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21639 (Nkind (Parent (S)) = N_Subtype_Declaration
21640 and then Is_Itype (Defining_Identifier (Parent (S))))
21642 Check_Incomplete (Subtype_Mark (S));
21646 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21648 -- Explicit subtype declaration case
21650 if Nkind (P) = N_Subtype_Declaration then
21651 Def_Id := Defining_Identifier (P);
21653 -- Explicit derived type definition case
21655 elsif Nkind (P) = N_Derived_Type_Definition then
21656 Def_Id := Defining_Identifier (Parent (P));
21658 -- Implicit case, the Def_Id must be created as an implicit type.
21659 -- The one exception arises in the case of concurrent types, array
21660 -- and access types, where other subsidiary implicit types may be
21661 -- created and must appear before the main implicit type. In these
21662 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21663 -- has not yet been called to create Def_Id.
21666 if Is_Array_Type (Subtype_Mark_Id)
21667 or else Is_Concurrent_Type (Subtype_Mark_Id)
21668 or else Is_Access_Type (Subtype_Mark_Id)
21672 -- For the other cases, we create a new unattached Itype,
21673 -- and set the indication to ensure it gets attached later.
21677 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21681 -- If the kind of constraint is invalid for this kind of type,
21682 -- then give an error, and then pretend no constraint was given.
21684 if not Is_Valid_Constraint_Kind
21685 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21688 ("incorrect constraint for this kind of type", Constraint (S));
21690 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21692 -- Set Ekind of orphan itype, to prevent cascaded errors
21694 if Present (Def_Id) then
21695 Set_Ekind (Def_Id, Ekind (Any_Type));
21698 -- Make recursive call, having got rid of the bogus constraint
21700 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21703 -- Remaining processing depends on type. Select on Base_Type kind to
21704 -- ensure getting to the concrete type kind in the case of a private
21705 -- subtype (needed when only doing semantic analysis).
21707 case Ekind (Base_Type (Subtype_Mark_Id)) is
21708 when Access_Kind =>
21710 -- If this is a constraint on a class-wide type, discard it.
21711 -- There is currently no way to express a partial discriminant
21712 -- constraint on a type with unknown discriminants. This is
21713 -- a pathology that the ACATS wisely decides not to test.
21715 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21716 if Comes_From_Source (S) then
21718 ("constraint on class-wide type ignored??",
21722 if Nkind (P) = N_Subtype_Declaration then
21723 Set_Subtype_Indication (P,
21724 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21727 return Subtype_Mark_Id;
21730 Constrain_Access (Def_Id, S, Related_Nod);
21733 and then Is_Itype (Designated_Type (Def_Id))
21734 and then Nkind (Related_Nod) = N_Subtype_Declaration
21735 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21737 Build_Itype_Reference
21738 (Designated_Type (Def_Id), Related_Nod);
21742 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21744 when Decimal_Fixed_Point_Kind =>
21745 Constrain_Decimal (Def_Id, S);
21747 when Enumeration_Kind =>
21748 Constrain_Enumeration (Def_Id, S);
21750 when Ordinary_Fixed_Point_Kind =>
21751 Constrain_Ordinary_Fixed (Def_Id, S);
21754 Constrain_Float (Def_Id, S);
21756 when Integer_Kind =>
21757 Constrain_Integer (Def_Id, S);
21759 when Class_Wide_Kind
21760 | E_Incomplete_Type
21764 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21766 if Ekind (Def_Id) = E_Incomplete_Type then
21767 Set_Private_Dependents (Def_Id, New_Elmt_List);
21770 when Private_Kind =>
21772 -- A private type with unknown discriminants may be completed
21773 -- by an unconstrained array type.
21775 if Has_Unknown_Discriminants (Subtype_Mark_Id)
21776 and then Present (Full_View (Subtype_Mark_Id))
21777 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
21779 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21781 -- ... but more commonly is completed by a discriminated record
21785 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21788 -- The base type may be private but Def_Id may be a full view
21791 if Is_Private_Type (Def_Id) then
21792 Set_Private_Dependents (Def_Id, New_Elmt_List);
21795 -- In case of an invalid constraint prevent further processing
21796 -- since the type constructed is missing expected fields.
21798 if Etype (Def_Id) = Any_Type then
21802 -- If the full view is that of a task with discriminants,
21803 -- we must constrain both the concurrent type and its
21804 -- corresponding record type. Otherwise we will just propagate
21805 -- the constraint to the full view, if available.
21807 if Present (Full_View (Subtype_Mark_Id))
21808 and then Has_Discriminants (Subtype_Mark_Id)
21809 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21812 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21814 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21815 Constrain_Concurrent (Full_View_Id, S,
21816 Related_Nod, Related_Id, Suffix);
21817 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21818 Set_Full_View (Def_Id, Full_View_Id);
21820 -- Introduce an explicit reference to the private subtype,
21821 -- to prevent scope anomalies in gigi if first use appears
21822 -- in a nested context, e.g. a later function body.
21823 -- Should this be generated in other contexts than a full
21824 -- type declaration?
21826 if Is_Itype (Def_Id)
21828 Nkind (Parent (P)) = N_Full_Type_Declaration
21830 Build_Itype_Reference (Def_Id, Parent (P));
21834 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21837 when Concurrent_Kind =>
21838 Constrain_Concurrent (Def_Id, S,
21839 Related_Nod, Related_Id, Suffix);
21842 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21845 -- Size, Alignment, Representation aspects and Convention are always
21846 -- inherited from the base type.
21848 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21849 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
21850 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21852 -- The anonymous subtype created for the subtype indication
21853 -- inherits the predicates of the parent.
21855 if Has_Predicates (Subtype_Mark_Id) then
21856 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21858 -- Indicate where the predicate function may be found
21860 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
21861 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
21867 end Process_Subtype;
21869 -----------------------------
21870 -- Record_Type_Declaration --
21871 -----------------------------
21873 procedure Record_Type_Declaration
21878 Def : constant Node_Id := Type_Definition (N);
21879 Is_Tagged : Boolean;
21880 Tag_Comp : Entity_Id;
21883 -- These flags must be initialized before calling Process_Discriminants
21884 -- because this routine makes use of them.
21886 Set_Ekind (T, E_Record_Type);
21888 Init_Size_Align (T);
21889 Set_Interfaces (T, No_Elist);
21890 Set_Stored_Constraint (T, No_Elist);
21891 Set_Default_SSO (T);
21892 Set_No_Reordering (T, No_Component_Reordering);
21896 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21897 if Limited_Present (Def) then
21898 Check_SPARK_05_Restriction ("limited is not allowed", N);
21901 if Abstract_Present (Def) then
21902 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21905 -- The flag Is_Tagged_Type might have already been set by
21906 -- Find_Type_Name if it detected an error for declaration T. This
21907 -- arises in the case of private tagged types where the full view
21908 -- omits the word tagged.
21911 Tagged_Present (Def)
21912 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21914 Set_Is_Limited_Record (T, Limited_Present (Def));
21917 Set_Is_Tagged_Type (T, True);
21918 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21921 -- Type is abstract if full declaration carries keyword, or if
21922 -- previous partial view did.
21924 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21925 or else Abstract_Present (Def));
21928 Check_SPARK_05_Restriction ("interface is not allowed", N);
21931 Analyze_Interface_Declaration (T, Def);
21933 if Present (Discriminant_Specifications (N)) then
21935 ("interface types cannot have discriminants",
21936 Defining_Identifier
21937 (First (Discriminant_Specifications (N))));
21941 -- First pass: if there are self-referential access components,
21942 -- create the required anonymous access type declarations, and if
21943 -- need be an incomplete type declaration for T itself.
21945 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21947 if Ada_Version >= Ada_2005
21948 and then Present (Interface_List (Def))
21950 Check_Interfaces (N, Def);
21953 Ifaces_List : Elist_Id;
21956 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21957 -- already in the parents.
21961 Ifaces_List => Ifaces_List,
21962 Exclude_Parents => True);
21964 Set_Interfaces (T, Ifaces_List);
21968 -- Records constitute a scope for the component declarations within.
21969 -- The scope is created prior to the processing of these declarations.
21970 -- Discriminants are processed first, so that they are visible when
21971 -- processing the other components. The Ekind of the record type itself
21972 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21974 -- Enter record scope
21978 -- If an incomplete or private type declaration was already given for
21979 -- the type, then this scope already exists, and the discriminants have
21980 -- been declared within. We must verify that the full declaration
21981 -- matches the incomplete one.
21983 Check_Or_Process_Discriminants (N, T, Prev);
21985 Set_Is_Constrained (T, not Has_Discriminants (T));
21986 Set_Has_Delayed_Freeze (T, True);
21988 -- For tagged types add a manually analyzed component corresponding
21989 -- to the component _tag, the corresponding piece of tree will be
21990 -- expanded as part of the freezing actions if it is not a CPP_Class.
21994 -- Do not add the tag unless we are in expansion mode
21996 if Expander_Active then
21997 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
21998 Enter_Name (Tag_Comp);
22000 Set_Ekind (Tag_Comp, E_Component);
22001 Set_Is_Tag (Tag_Comp);
22002 Set_Is_Aliased (Tag_Comp);
22003 Set_Is_Independent (Tag_Comp);
22004 Set_Etype (Tag_Comp, RTE (RE_Tag));
22005 Set_DT_Entry_Count (Tag_Comp, No_Uint);
22006 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
22007 Init_Component_Location (Tag_Comp);
22009 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
22010 -- implemented interfaces.
22012 if Has_Interfaces (T) then
22013 Add_Interface_Tag_Components (N, T);
22017 Make_Class_Wide_Type (T);
22018 Set_Direct_Primitive_Operations (T, New_Elmt_List);
22021 -- We must suppress range checks when processing record components in
22022 -- the presence of discriminants, since we don't want spurious checks to
22023 -- be generated during their analysis, but Suppress_Range_Checks flags
22024 -- must be reset the after processing the record definition.
22026 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
22027 -- couldn't we just use the normal range check suppression method here.
22028 -- That would seem cleaner ???
22030 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
22031 Set_Kill_Range_Checks (T, True);
22032 Record_Type_Definition (Def, Prev);
22033 Set_Kill_Range_Checks (T, False);
22035 Record_Type_Definition (Def, Prev);
22038 -- Exit from record scope
22042 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
22043 -- the implemented interfaces and associate them an aliased entity.
22046 and then not Is_Empty_List (Interface_List (Def))
22048 Derive_Progenitor_Subprograms (T, T);
22051 Check_Function_Writable_Actuals (N);
22052 end Record_Type_Declaration;
22054 ----------------------------
22055 -- Record_Type_Definition --
22056 ----------------------------
22058 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
22059 Component : Entity_Id;
22060 Ctrl_Components : Boolean := False;
22061 Final_Storage_Only : Boolean;
22065 if Ekind (Prev_T) = E_Incomplete_Type then
22066 T := Full_View (Prev_T);
22071 -- In SPARK, tagged types and type extensions may only be declared in
22072 -- the specification of library unit packages.
22074 if Present (Def) and then Is_Tagged_Type (T) then
22080 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
22081 Typ := Parent (Def);
22084 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
22085 Typ := Parent (Parent (Def));
22088 Ctxt := Parent (Typ);
22090 if Nkind (Ctxt) = N_Package_Body
22091 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
22093 Check_SPARK_05_Restriction
22094 ("type should be defined in package specification", Typ);
22096 elsif Nkind (Ctxt) /= N_Package_Specification
22097 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
22099 Check_SPARK_05_Restriction
22100 ("type should be defined in library unit package", Typ);
22105 Final_Storage_Only := not Is_Controlled (T);
22107 -- Ada 2005: Check whether an explicit Limited is present in a derived
22108 -- type declaration.
22110 if Nkind (Parent (Def)) = N_Derived_Type_Definition
22111 and then Limited_Present (Parent (Def))
22113 Set_Is_Limited_Record (T);
22116 -- If the component list of a record type is defined by the reserved
22117 -- word null and there is no discriminant part, then the record type has
22118 -- no components and all records of the type are null records (RM 3.7)
22119 -- This procedure is also called to process the extension part of a
22120 -- record extension, in which case the current scope may have inherited
22124 or else No (Component_List (Def))
22125 or else Null_Present (Component_List (Def))
22127 if not Is_Tagged_Type (T) then
22128 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
22132 Analyze_Declarations (Component_Items (Component_List (Def)));
22134 if Present (Variant_Part (Component_List (Def))) then
22135 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
22136 Analyze (Variant_Part (Component_List (Def)));
22140 -- After completing the semantic analysis of the record definition,
22141 -- record components, both new and inherited, are accessible. Set their
22142 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22143 -- whose Ekind may be void.
22145 Component := First_Entity (Current_Scope);
22146 while Present (Component) loop
22147 if Ekind (Component) = E_Void
22148 and then not Is_Itype (Component)
22150 Set_Ekind (Component, E_Component);
22151 Init_Component_Location (Component);
22154 Propagate_Concurrent_Flags (T, Etype (Component));
22156 if Ekind (Component) /= E_Component then
22159 -- Do not set Has_Controlled_Component on a class-wide equivalent
22160 -- type. See Make_CW_Equivalent_Type.
22162 elsif not Is_Class_Wide_Equivalent_Type (T)
22163 and then (Has_Controlled_Component (Etype (Component))
22164 or else (Chars (Component) /= Name_uParent
22165 and then Is_Controlled (Etype (Component))))
22167 Set_Has_Controlled_Component (T, True);
22168 Final_Storage_Only :=
22170 and then Finalize_Storage_Only (Etype (Component));
22171 Ctrl_Components := True;
22174 Next_Entity (Component);
22177 -- A Type is Finalize_Storage_Only only if all its controlled components
22180 if Ctrl_Components then
22181 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22184 -- Place reference to end record on the proper entity, which may
22185 -- be a partial view.
22187 if Present (Def) then
22188 Process_End_Label (Def, 'e', Prev_T);
22190 end Record_Type_Definition;
22192 ---------------------------
22193 -- Replace_Discriminants --
22194 ---------------------------
22196 procedure Replace_Discriminants (Typ : Entity_Id; Decl : Node_Id) is
22197 function Process (N : Node_Id) return Traverse_Result;
22203 function Process (N : Node_Id) return Traverse_Result is
22207 if Nkind (N) = N_Discriminant_Specification then
22208 Comp := First_Discriminant (Typ);
22209 while Present (Comp) loop
22210 if Original_Record_Component (Comp) = Defining_Identifier (N)
22211 or else Chars (Comp) = Chars (Defining_Identifier (N))
22213 Set_Defining_Identifier (N, Comp);
22217 Next_Discriminant (Comp);
22220 elsif Nkind (N) = N_Variant_Part then
22221 Comp := First_Discriminant (Typ);
22222 while Present (Comp) loop
22223 if Original_Record_Component (Comp) = Entity (Name (N))
22224 or else Chars (Comp) = Chars (Name (N))
22226 -- Make sure to preserve the type coming from the parent on
22227 -- the Name, even if the subtype of the discriminant can be
22228 -- constrained, so that discrete choices inherited from the
22229 -- parent in the variant part are not flagged as violating
22230 -- the constraints of the subtype.
22233 Typ : constant Entity_Id := Etype (Name (N));
22235 Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
22236 Set_Etype (Name (N), Typ);
22241 Next_Discriminant (Comp);
22248 procedure Replace is new Traverse_Proc (Process);
22250 -- Start of processing for Replace_Discriminants
22254 end Replace_Discriminants;
22256 -------------------------------
22257 -- Set_Completion_Referenced --
22258 -------------------------------
22260 procedure Set_Completion_Referenced (E : Entity_Id) is
22262 -- If in main unit, mark entity that is a completion as referenced,
22263 -- warnings go on the partial view when needed.
22265 if In_Extended_Main_Source_Unit (E) then
22266 Set_Referenced (E);
22268 end Set_Completion_Referenced;
22270 ---------------------
22271 -- Set_Default_SSO --
22272 ---------------------
22274 procedure Set_Default_SSO (T : Entity_Id) is
22276 case Opt.Default_SSO is
22280 Set_SSO_Set_Low_By_Default (T, True);
22282 Set_SSO_Set_High_By_Default (T, True);
22284 raise Program_Error;
22286 end Set_Default_SSO;
22288 ---------------------
22289 -- Set_Fixed_Range --
22290 ---------------------
22292 -- The range for fixed-point types is complicated by the fact that we
22293 -- do not know the exact end points at the time of the declaration. This
22294 -- is true for three reasons:
22296 -- A size clause may affect the fudging of the end-points.
22297 -- A small clause may affect the values of the end-points.
22298 -- We try to include the end-points if it does not affect the size.
22300 -- This means that the actual end-points must be established at the
22301 -- point when the type is frozen. Meanwhile, we first narrow the range
22302 -- as permitted (so that it will fit if necessary in a small specified
22303 -- size), and then build a range subtree with these narrowed bounds.
22304 -- Set_Fixed_Range constructs the range from real literal values, and
22305 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22307 -- The parent of this range is set to point to the entity so that it is
22308 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22309 -- other scalar types, which are just pointers to the range in the
22310 -- original tree, this would otherwise be an orphan).
22312 -- The tree is left unanalyzed. When the type is frozen, the processing
22313 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22314 -- analyzed, and uses this as an indication that it should complete
22315 -- work on the range (it will know the final small and size values).
22317 procedure Set_Fixed_Range
22323 S : constant Node_Id :=
22325 Low_Bound => Make_Real_Literal (Loc, Lo),
22326 High_Bound => Make_Real_Literal (Loc, Hi));
22328 Set_Scalar_Range (E, S);
22331 -- Before the freeze point, the bounds of a fixed point are universal
22332 -- and carry the corresponding type.
22334 Set_Etype (Low_Bound (S), Universal_Real);
22335 Set_Etype (High_Bound (S), Universal_Real);
22336 end Set_Fixed_Range;
22338 ----------------------------------
22339 -- Set_Scalar_Range_For_Subtype --
22340 ----------------------------------
22342 procedure Set_Scalar_Range_For_Subtype
22343 (Def_Id : Entity_Id;
22347 Kind : constant Entity_Kind := Ekind (Def_Id);
22350 -- Defend against previous error
22352 if Nkind (R) = N_Error then
22356 Set_Scalar_Range (Def_Id, R);
22358 -- We need to link the range into the tree before resolving it so
22359 -- that types that are referenced, including importantly the subtype
22360 -- itself, are properly frozen (Freeze_Expression requires that the
22361 -- expression be properly linked into the tree). Of course if it is
22362 -- already linked in, then we do not disturb the current link.
22364 if No (Parent (R)) then
22365 Set_Parent (R, Def_Id);
22368 -- Reset the kind of the subtype during analysis of the range, to
22369 -- catch possible premature use in the bounds themselves.
22371 Set_Ekind (Def_Id, E_Void);
22372 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22373 Set_Ekind (Def_Id, Kind);
22374 end Set_Scalar_Range_For_Subtype;
22376 --------------------------------------------------------
22377 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22378 --------------------------------------------------------
22380 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22384 -- Make sure set if encountered during Expand_To_Stored_Constraint
22386 Set_Stored_Constraint (E, No_Elist);
22388 -- Give it the right value
22390 if Is_Constrained (E) and then Has_Discriminants (E) then
22391 Set_Stored_Constraint (E,
22392 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22394 end Set_Stored_Constraint_From_Discriminant_Constraint;
22396 -------------------------------------
22397 -- Signed_Integer_Type_Declaration --
22398 -------------------------------------
22400 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22401 Implicit_Base : Entity_Id;
22402 Base_Typ : Entity_Id;
22405 Errs : Boolean := False;
22409 function Can_Derive_From (E : Entity_Id) return Boolean;
22410 -- Determine whether given bounds allow derivation from specified type
22412 procedure Check_Bound (Expr : Node_Id);
22413 -- Check bound to make sure it is integral and static. If not, post
22414 -- appropriate error message and set Errs flag
22416 ---------------------
22417 -- Can_Derive_From --
22418 ---------------------
22420 -- Note we check both bounds against both end values, to deal with
22421 -- strange types like ones with a range of 0 .. -12341234.
22423 function Can_Derive_From (E : Entity_Id) return Boolean is
22424 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22425 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22427 return Lo <= Lo_Val and then Lo_Val <= Hi
22429 Lo <= Hi_Val and then Hi_Val <= Hi;
22430 end Can_Derive_From;
22436 procedure Check_Bound (Expr : Node_Id) is
22438 -- If a range constraint is used as an integer type definition, each
22439 -- bound of the range must be defined by a static expression of some
22440 -- integer type, but the two bounds need not have the same integer
22441 -- type (Negative bounds are allowed.) (RM 3.5.4)
22443 if not Is_Integer_Type (Etype (Expr)) then
22445 ("integer type definition bounds must be of integer type", Expr);
22448 elsif not Is_OK_Static_Expression (Expr) then
22449 Flag_Non_Static_Expr
22450 ("non-static expression used for integer type bound!", Expr);
22453 -- Otherwise the bounds are folded into literals
22455 elsif Is_Entity_Name (Expr) then
22456 Fold_Uint (Expr, Expr_Value (Expr), True);
22460 -- Start of processing for Signed_Integer_Type_Declaration
22463 -- Create an anonymous base type
22466 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22468 -- Analyze and check the bounds, they can be of any integer type
22470 Lo := Low_Bound (Def);
22471 Hi := High_Bound (Def);
22473 -- Arbitrarily use Integer as the type if either bound had an error
22475 if Hi = Error or else Lo = Error then
22476 Base_Typ := Any_Integer;
22477 Set_Error_Posted (T, True);
22480 -- Here both bounds are OK expressions
22483 Analyze_And_Resolve (Lo, Any_Integer);
22484 Analyze_And_Resolve (Hi, Any_Integer);
22490 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22491 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22494 -- Find type to derive from
22496 Lo_Val := Expr_Value (Lo);
22497 Hi_Val := Expr_Value (Hi);
22499 if Can_Derive_From (Standard_Short_Short_Integer) then
22500 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22502 elsif Can_Derive_From (Standard_Short_Integer) then
22503 Base_Typ := Base_Type (Standard_Short_Integer);
22505 elsif Can_Derive_From (Standard_Integer) then
22506 Base_Typ := Base_Type (Standard_Integer);
22508 elsif Can_Derive_From (Standard_Long_Integer) then
22509 Base_Typ := Base_Type (Standard_Long_Integer);
22511 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22512 Check_Restriction (No_Long_Long_Integers, Def);
22513 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22516 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22517 Error_Msg_N ("integer type definition bounds out of range", Def);
22518 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22519 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22523 -- Set the type of the bounds to the implicit base: we cannot set it to
22524 -- the new type, because this would be a forward reference for the code
22525 -- generator and, if the original type is user-defined, this could even
22526 -- lead to spurious semantic errors. Furthermore we do not set it to be
22527 -- universal, because this could make it much larger than needed here.
22530 Set_Etype (Lo, Implicit_Base);
22531 Set_Etype (Hi, Implicit_Base);
22534 -- Complete both implicit base and declared first subtype entities. The
22535 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22536 -- are not clobbered when the signed integer type acts as a full view of
22539 Set_Etype (Implicit_Base, Base_Typ);
22540 Set_Size_Info (Implicit_Base, Base_Typ);
22541 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22542 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22543 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22545 Set_Ekind (T, E_Signed_Integer_Subtype);
22546 Set_Etype (T, Implicit_Base);
22547 Set_Size_Info (T, Implicit_Base);
22548 Inherit_Rep_Item_Chain (T, Implicit_Base);
22549 Set_Scalar_Range (T, Def);
22550 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22551 Set_Is_Constrained (T);
22552 end Signed_Integer_Type_Declaration;