1 ------------------------------------------------------------------------------
3 -- GNAT COMPILER COMPONENTS --
9 -- Copyright (C) 1992-2019, 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 Build_Underlying_Full_View
239 -- If the completion of a private type is itself derived from a private
240 -- type, or if the full view of a private subtype is itself private, the
241 -- back-end has no way to compute the actual size of this type. We build
242 -- an internal subtype declaration of the proper parent type to convey
243 -- this information. This extra mechanism is needed because a full
244 -- view cannot itself have a full view (it would get clobbered during
247 procedure Check_Access_Discriminant_Requires_Limited
250 -- Check the restriction that the type to which an access discriminant
251 -- belongs must be a concurrent type or a descendant of a type with
252 -- the reserved word 'limited' in its declaration.
254 procedure Check_Anonymous_Access_Components
258 Comp_List : Node_Id);
259 -- Ada 2005 AI-382: an access component in a record definition can refer to
260 -- the enclosing record, in which case it denotes the type itself, and not
261 -- the current instance of the type. We create an anonymous access type for
262 -- the component, and flag it as an access to a component, so accessibility
263 -- checks are properly performed on it. The declaration of the access type
264 -- is placed ahead of that of the record to prevent order-of-elaboration
265 -- circularity issues in Gigi. We create an incomplete type for the record
266 -- declaration, which is the designated type of the anonymous access.
268 procedure Check_Delta_Expression (E : Node_Id);
269 -- Check that the expression represented by E is suitable for use as a
270 -- delta expression, i.e. it is of real type and is static.
272 procedure Check_Digits_Expression (E : Node_Id);
273 -- Check that the expression represented by E is suitable for use as a
274 -- digits expression, i.e. it is of integer type, positive and static.
276 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id);
277 -- Validate the initialization of an object declaration. T is the required
278 -- type, and Exp is the initialization expression.
280 procedure Check_Interfaces (N : Node_Id; Def : Node_Id);
281 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
283 procedure Check_Or_Process_Discriminants
286 Prev : Entity_Id := Empty);
287 -- If N is the full declaration of the completion T of an incomplete or
288 -- private type, check its discriminants (which are already known to be
289 -- conformant with those of the partial view, see Find_Type_Name),
290 -- otherwise process them. Prev is the entity of the partial declaration,
293 procedure Check_Real_Bound (Bound : Node_Id);
294 -- Check given bound for being of real type and static. If not, post an
295 -- appropriate message, and rewrite the bound with the real literal zero.
297 procedure Constant_Redeclaration
301 -- Various checks on legality of full declaration of deferred constant.
302 -- Id is the entity for the redeclaration, N is the N_Object_Declaration,
303 -- node. The caller has not yet set any attributes of this entity.
305 function Contain_Interface
307 Ifaces : Elist_Id) return Boolean;
308 -- Ada 2005: Determine whether Iface is present in the list Ifaces
310 procedure Convert_Scalar_Bounds
312 Parent_Type : Entity_Id;
313 Derived_Type : Entity_Id;
315 -- For derived scalar types, convert the bounds in the type definition to
316 -- the derived type, and complete their analysis. Given a constraint of the
317 -- form ".. new T range Lo .. Hi", Lo and Hi are analyzed and resolved with
318 -- T'Base, the parent_type. The bounds of the derived type (the anonymous
319 -- base) are copies of Lo and Hi. Finally, the bounds of the derived
320 -- subtype are conversions of those bounds to the derived_type, so that
321 -- their typing is consistent.
323 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id);
324 -- Copies attributes from array base type T2 to array base type T1. Copies
325 -- only attributes that apply to base types, but not subtypes.
327 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id);
328 -- Copies attributes from array subtype T2 to array subtype T1. Copies
329 -- attributes that apply to both subtypes and base types.
331 procedure Create_Constrained_Components
335 Constraints : Elist_Id);
336 -- Build the list of entities for a constrained discriminated record
337 -- subtype. If a component depends on a discriminant, replace its subtype
338 -- using the discriminant values in the discriminant constraint. Subt
339 -- is the defining identifier for the subtype whose list of constrained
340 -- entities we will create. Decl_Node is the type declaration node where
341 -- we will attach all the itypes created. Typ is the base discriminated
342 -- type for the subtype Subt. Constraints is the list of discriminant
343 -- constraints for Typ.
345 function Constrain_Component_Type
347 Constrained_Typ : Entity_Id;
348 Related_Node : Node_Id;
350 Constraints : Elist_Id) return Entity_Id;
351 -- Given a discriminated base type Typ, a list of discriminant constraints,
352 -- Constraints, for Typ and a component Comp of Typ, create and return the
353 -- type corresponding to Etype (Comp) where all discriminant references
354 -- are replaced with the corresponding constraint. If Etype (Comp) contains
355 -- no discriminant references then it is returned as-is. Constrained_Typ
356 -- is the final constrained subtype to which the constrained component
357 -- belongs. Related_Node is the node where we attach all created itypes.
359 procedure Constrain_Access
360 (Def_Id : in out Entity_Id;
362 Related_Nod : Node_Id);
363 -- Apply a list of constraints to an access type. If Def_Id is empty, it is
364 -- an anonymous type created for a subtype indication. In that case it is
365 -- created in the procedure and attached to Related_Nod.
367 procedure Constrain_Array
368 (Def_Id : in out Entity_Id;
370 Related_Nod : Node_Id;
371 Related_Id : Entity_Id;
373 -- Apply a list of index constraints to an unconstrained array type. The
374 -- first parameter is the entity for the resulting subtype. A value of
375 -- Empty for Def_Id indicates that an implicit type must be created, but
376 -- creation is delayed (and must be done by this procedure) because other
377 -- subsidiary implicit types must be created first (which is why Def_Id
378 -- is an in/out parameter). The second parameter is a subtype indication
379 -- node for the constrained array to be created (e.g. something of the
380 -- form string (1 .. 10)). Related_Nod gives the place where this type
381 -- has to be inserted in the tree. The Related_Id and Suffix parameters
382 -- are used to build the associated Implicit type name.
384 procedure Constrain_Concurrent
385 (Def_Id : in out Entity_Id;
387 Related_Nod : Node_Id;
388 Related_Id : Entity_Id;
390 -- Apply list of discriminant constraints to an unconstrained concurrent
393 -- SI is the N_Subtype_Indication node containing the constraint and
394 -- the unconstrained type to constrain.
396 -- Def_Id is the entity for the resulting constrained subtype. A value
397 -- of Empty for Def_Id indicates that an implicit type must be created,
398 -- but creation is delayed (and must be done by this procedure) because
399 -- other subsidiary implicit types must be created first (which is why
400 -- Def_Id is an in/out parameter).
402 -- Related_Nod gives the place where this type has to be inserted
405 -- The last two arguments are used to create its external name if needed.
407 function Constrain_Corresponding_Record
408 (Prot_Subt : Entity_Id;
409 Corr_Rec : Entity_Id;
410 Related_Nod : Node_Id) return Entity_Id;
411 -- When constraining a protected type or task type with discriminants,
412 -- constrain the corresponding record with the same discriminant values.
414 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id);
415 -- Constrain a decimal fixed point type with a digits constraint and/or a
416 -- range constraint, and build E_Decimal_Fixed_Point_Subtype entity.
418 procedure Constrain_Discriminated_Type
421 Related_Nod : Node_Id;
422 For_Access : Boolean := False);
423 -- Process discriminant constraints of composite type. Verify that values
424 -- have been provided for all discriminants, that the original type is
425 -- unconstrained, and that the types of the supplied expressions match
426 -- the discriminant types. The first three parameters are like in routine
427 -- Constrain_Concurrent. See Build_Discriminated_Subtype for an explanation
430 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id);
431 -- Constrain an enumeration type with a range constraint. This is identical
432 -- to Constrain_Integer, but for the Ekind of the resulting subtype.
434 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id);
435 -- Constrain a floating point type with either a digits constraint
436 -- and/or a range constraint, building a E_Floating_Point_Subtype.
438 procedure Constrain_Index
441 Related_Nod : Node_Id;
442 Related_Id : Entity_Id;
445 -- Process an index constraint S in a constrained array declaration. The
446 -- constraint can be a subtype name, or a range with or without an explicit
447 -- subtype mark. The index is the corresponding index of the unconstrained
448 -- array. The Related_Id and Suffix parameters are used to build the
449 -- associated Implicit type name.
451 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id);
452 -- Build subtype of a signed or modular integer type
454 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id);
455 -- Constrain an ordinary fixed point type with a range constraint, and
456 -- build an E_Ordinary_Fixed_Point_Subtype entity.
458 procedure Copy_And_Swap (Priv, Full : Entity_Id);
459 -- Copy the Priv entity into the entity of its full declaration then swap
460 -- the two entities in such a manner that the former private type is now
461 -- seen as a full type.
463 procedure Decimal_Fixed_Point_Type_Declaration
466 -- Create a new decimal fixed point type, and apply the constraint to
467 -- obtain a subtype of this new type.
469 procedure Complete_Private_Subtype
472 Full_Base : Entity_Id;
473 Related_Nod : Node_Id);
474 -- Complete the implicit full view of a private subtype by setting the
475 -- appropriate semantic fields. If the full view of the parent is a record
476 -- type, build constrained components of subtype.
478 procedure Derive_Progenitor_Subprograms
479 (Parent_Type : Entity_Id;
480 Tagged_Type : Entity_Id);
481 -- Ada 2005 (AI-251): To complete type derivation, collect the primitive
482 -- operations of progenitors of Tagged_Type, and replace the subsidiary
483 -- subtypes with Tagged_Type, to build the specs of the inherited interface
484 -- primitives. The derived primitives are aliased to those of the
485 -- interface. This routine takes care also of transferring to the full view
486 -- subprograms associated with the partial view of Tagged_Type that cover
487 -- interface primitives.
489 procedure Derived_Standard_Character
491 Parent_Type : Entity_Id;
492 Derived_Type : Entity_Id);
493 -- Subsidiary procedure to Build_Derived_Enumeration_Type which handles
494 -- derivations from types Standard.Character and Standard.Wide_Character.
496 procedure Derived_Type_Declaration
499 Is_Completion : Boolean);
500 -- Process a derived type declaration. Build_Derived_Type is invoked
501 -- to process the actual derived type definition. Parameters N and
502 -- Is_Completion have the same meaning as in Build_Derived_Type.
503 -- T is the N_Defining_Identifier for the entity defined in the
504 -- N_Full_Type_Declaration node N, that is T is the derived type.
506 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id);
507 -- Insert each literal in symbol table, as an overloadable identifier. Each
508 -- enumeration type is mapped into a sequence of integers, and each literal
509 -- is defined as a constant with integer value. If any of the literals are
510 -- character literals, the type is a character type, which means that
511 -- strings are legal aggregates for arrays of components of the type.
513 function Expand_To_Stored_Constraint
515 Constraint : Elist_Id) return Elist_Id;
516 -- Given a constraint (i.e. a list of expressions) on the discriminants of
517 -- Typ, expand it into a constraint on the stored discriminants and return
518 -- the new list of expressions constraining the stored discriminants.
520 function Find_Type_Of_Object
522 Related_Nod : Node_Id) return Entity_Id;
523 -- Get type entity for object referenced by Obj_Def, attaching the implicit
524 -- types generated to Related_Nod.
526 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id);
527 -- Create a new float and apply the constraint to obtain subtype of it
529 function Has_Range_Constraint (N : Node_Id) return Boolean;
530 -- Given an N_Subtype_Indication node N, return True if a range constraint
531 -- is present, either directly, or as part of a digits or delta constraint.
532 -- In addition, a digits constraint in the decimal case returns True, since
533 -- it establishes a default range if no explicit range is present.
535 function Inherit_Components
537 Parent_Base : Entity_Id;
538 Derived_Base : Entity_Id;
540 Inherit_Discr : Boolean;
541 Discs : Elist_Id) return Elist_Id;
542 -- Called from Build_Derived_Record_Type to inherit the components of
543 -- Parent_Base (a base type) into the Derived_Base (the derived base type).
544 -- For more information on derived types and component inheritance please
545 -- consult the comment above the body of Build_Derived_Record_Type.
547 -- N is the original derived type declaration
549 -- Is_Tagged is set if we are dealing with tagged types
551 -- If Inherit_Discr is set, Derived_Base inherits its discriminants from
552 -- Parent_Base, otherwise no discriminants are inherited.
554 -- Discs gives the list of constraints that apply to Parent_Base in the
555 -- derived type declaration. If Discs is set to No_Elist, then we have
556 -- the following situation:
558 -- type Parent (D1..Dn : ..) is [tagged] record ...;
559 -- type Derived is new Parent [with ...];
561 -- which gets treated as
563 -- type Derived (D1..Dn : ..) is new Parent (D1,..,Dn) [with ...];
565 -- For untagged types the returned value is an association list. The list
566 -- starts from the association (Parent_Base => Derived_Base), and then it
567 -- contains a sequence of the associations of the form
569 -- (Old_Component => New_Component),
571 -- where Old_Component is the Entity_Id of a component in Parent_Base and
572 -- New_Component is the Entity_Id of the corresponding component in
573 -- Derived_Base. For untagged records, this association list is needed when
574 -- copying the record declaration for the derived base. In the tagged case
575 -- the value returned is irrelevant.
577 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id);
578 -- Propagate static and dynamic predicate flags from a parent to the
579 -- subtype in a subtype declaration with and without constraints.
581 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean;
582 -- Subsidiary to Check_Abstract_Overriding and Derive_Subprogram.
583 -- Determine whether subprogram Subp is a procedure subject to pragma
584 -- Extensions_Visible with value False and has at least one controlling
585 -- parameter of mode OUT.
587 function Is_Valid_Constraint_Kind
589 Constraint_Kind : Node_Kind) return Boolean;
590 -- Returns True if it is legal to apply the given kind of constraint to the
591 -- given kind of type (index constraint to an array type, for example).
593 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id);
594 -- Create new modular type. Verify that modulus is in bounds
596 procedure New_Concatenation_Op (Typ : Entity_Id);
597 -- Create an abbreviated declaration for an operator in order to
598 -- materialize concatenation on array types.
600 procedure Ordinary_Fixed_Point_Type_Declaration
603 -- Create a new ordinary fixed point type, and apply the constraint to
604 -- obtain subtype of it.
606 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id);
607 -- Wrapper on Preanalyze_Spec_Expression for default expressions, so that
608 -- In_Default_Expr can be properly adjusted.
610 procedure Prepare_Private_Subtype_Completion
612 Related_Nod : Node_Id);
613 -- Id is a subtype of some private type. Creates the full declaration
614 -- associated with Id whenever possible, i.e. when the full declaration
615 -- of the base type is already known. Records each subtype into
616 -- Private_Dependents of the base type.
618 procedure Process_Incomplete_Dependents
622 -- Process all entities that depend on an incomplete type. There include
623 -- subtypes, subprogram types that mention the incomplete type in their
624 -- profiles, and subprogram with access parameters that designate the
627 -- Inc_T is the defining identifier of an incomplete type declaration, its
628 -- Ekind is E_Incomplete_Type.
630 -- N is the corresponding N_Full_Type_Declaration for Inc_T.
632 -- Full_T is N's defining identifier.
634 -- Subtypes of incomplete types with discriminants are completed when the
635 -- parent type is. This is simpler than private subtypes, because they can
636 -- only appear in the same scope, and there is no need to exchange views.
637 -- Similarly, access_to_subprogram types may have a parameter or a return
638 -- type that is an incomplete type, and that must be replaced with the
641 -- If the full type is tagged, subprogram with access parameters that
642 -- designated the incomplete may be primitive operations of the full type,
643 -- and have to be processed accordingly.
645 procedure Process_Real_Range_Specification (Def : Node_Id);
646 -- Given the type definition for a real type, this procedure processes and
647 -- checks the real range specification of this type definition if one is
648 -- present. If errors are found, error messages are posted, and the
649 -- Real_Range_Specification of Def is reset to Empty.
651 procedure Record_Type_Declaration
655 -- Process a record type declaration (for both untagged and tagged
656 -- records). Parameters T and N are exactly like in procedure
657 -- Derived_Type_Declaration, except that no flag Is_Completion is needed
658 -- for this routine. If this is the completion of an incomplete type
659 -- declaration, Prev is the entity of the incomplete declaration, used for
660 -- cross-referencing. Otherwise Prev = T.
662 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id);
663 -- This routine is used to process the actual record type definition (both
664 -- for untagged and tagged records). Def is a record type definition node.
665 -- This procedure analyzes the components in this record type definition.
666 -- Prev_T is the entity for the enclosing record type. It is provided so
667 -- that its Has_Task flag can be set if any of the component have Has_Task
668 -- set. If the declaration is the completion of an incomplete type
669 -- declaration, Prev_T is the original incomplete type, whose full view is
672 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id);
673 -- Subsidiary to Build_Derived_Record_Type. For untagged records, we
674 -- build a copy of the declaration tree of the parent, and we create
675 -- independently the list of components for the derived type. Semantic
676 -- information uses the component entities, but record representation
677 -- clauses are validated on the declaration tree. This procedure replaces
678 -- discriminants and components in the declaration with those that have
679 -- been created by Inherit_Components.
681 procedure Set_Fixed_Range
686 -- Build a range node with the given bounds and set it as the Scalar_Range
687 -- of the given fixed-point type entity. Loc is the source location used
688 -- for the constructed range. See body for further details.
690 procedure Set_Scalar_Range_For_Subtype
694 -- This routine is used to set the scalar range field for a subtype given
695 -- Def_Id, the entity for the subtype, and R, the range expression for the
696 -- scalar range. Subt provides the parent subtype to be used to analyze,
697 -- resolve, and check the given range.
699 procedure Set_Default_SSO (T : Entity_Id);
700 -- T is the entity for an array or record being declared. This procedure
701 -- sets the flags SSO_Set_Low_By_Default/SSO_Set_High_By_Default according
702 -- to the setting of Opt.Default_SSO.
704 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id);
705 -- Create a new signed integer entity, and apply the constraint to obtain
706 -- the required first named subtype of this type.
708 procedure Set_Stored_Constraint_From_Discriminant_Constraint
710 -- E is some record type. This routine computes E's Stored_Constraint
711 -- from its Discriminant_Constraint.
713 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id);
714 -- Check that an entity in a list of progenitors is an interface,
715 -- emit error otherwise.
717 -----------------------
718 -- Access_Definition --
719 -----------------------
721 function Access_Definition
722 (Related_Nod : Node_Id;
723 N : Node_Id) return Entity_Id
725 Anon_Type : Entity_Id;
726 Anon_Scope : Entity_Id;
727 Desig_Type : Entity_Id;
728 Enclosing_Prot_Type : Entity_Id := Empty;
731 Check_SPARK_05_Restriction ("access type is not allowed", N);
733 if Is_Entry (Current_Scope)
734 and then Is_Task_Type (Etype (Scope (Current_Scope)))
736 Error_Msg_N ("task entries cannot have access parameters", N);
740 -- Ada 2005: For an object declaration the corresponding anonymous
741 -- type is declared in the current scope.
743 -- If the access definition is the return type of another access to
744 -- function, scope is the current one, because it is the one of the
745 -- current type declaration, except for the pathological case below.
747 if Nkind_In (Related_Nod, N_Object_Declaration,
748 N_Access_Function_Definition)
750 Anon_Scope := Current_Scope;
752 -- A pathological case: function returning access functions that
753 -- return access functions, etc. Each anonymous access type created
754 -- is in the enclosing scope of the outermost function.
761 while Nkind_In (Par, N_Access_Function_Definition,
767 if Nkind (Par) = N_Function_Specification then
768 Anon_Scope := Scope (Defining_Entity (Par));
772 -- For the anonymous function result case, retrieve the scope of the
773 -- function specification's associated entity rather than using the
774 -- current scope. The current scope will be the function itself if the
775 -- formal part is currently being analyzed, but will be the parent scope
776 -- in the case of a parameterless function, and we always want to use
777 -- the function's parent scope. Finally, if the function is a child
778 -- unit, we must traverse the tree to retrieve the proper entity.
780 elsif Nkind (Related_Nod) = N_Function_Specification
781 and then Nkind (Parent (N)) /= N_Parameter_Specification
783 -- If the current scope is a protected type, the anonymous access
784 -- is associated with one of the protected operations, and must
785 -- be available in the scope that encloses the protected declaration.
786 -- Otherwise the type is in the scope enclosing the subprogram.
788 -- If the function has formals, The return type of a subprogram
789 -- declaration is analyzed in the scope of the subprogram (see
790 -- Process_Formals) and thus the protected type, if present, is
791 -- the scope of the current function scope.
793 if Ekind (Current_Scope) = E_Protected_Type then
794 Enclosing_Prot_Type := Current_Scope;
796 elsif Ekind (Current_Scope) = E_Function
797 and then Ekind (Scope (Current_Scope)) = E_Protected_Type
799 Enclosing_Prot_Type := Scope (Current_Scope);
802 if Present (Enclosing_Prot_Type) then
803 Anon_Scope := Scope (Enclosing_Prot_Type);
806 Anon_Scope := Scope (Defining_Entity (Related_Nod));
809 -- For an access type definition, if the current scope is a child
810 -- unit it is the scope of the type.
812 elsif Is_Compilation_Unit (Current_Scope) then
813 Anon_Scope := Current_Scope;
815 -- For access formals, access components, and access discriminants, the
816 -- scope is that of the enclosing declaration,
819 Anon_Scope := Scope (Current_Scope);
824 (E_Anonymous_Access_Type, Related_Nod, Scope_Id => Anon_Scope);
827 and then Ada_Version >= Ada_2005
829 Error_Msg_N ("ALL is not permitted for anonymous access types", N);
832 -- Ada 2005 (AI-254): In case of anonymous access to subprograms call
833 -- the corresponding semantic routine
835 if Present (Access_To_Subprogram_Definition (N)) then
837 -- Compiler runtime units are compiled in Ada 2005 mode when building
838 -- the runtime library but must also be compilable in Ada 95 mode
839 -- (when bootstrapping the compiler).
841 Check_Compiler_Unit ("anonymous access to subprogram", N);
843 Access_Subprogram_Declaration
844 (T_Name => Anon_Type,
845 T_Def => Access_To_Subprogram_Definition (N));
847 if Ekind (Anon_Type) = E_Access_Protected_Subprogram_Type then
849 (Anon_Type, E_Anonymous_Access_Protected_Subprogram_Type);
851 Set_Ekind (Anon_Type, E_Anonymous_Access_Subprogram_Type);
854 Set_Can_Use_Internal_Rep
855 (Anon_Type, not Always_Compatible_Rep_On_Target);
857 -- If the anonymous access is associated with a protected operation,
858 -- create a reference to it after the enclosing protected definition
859 -- because the itype will be used in the subsequent bodies.
861 -- If the anonymous access itself is protected, a full type
862 -- declaratiton will be created for it, so that the equivalent
863 -- record type can be constructed. For further details, see
864 -- Replace_Anonymous_Access_To_Protected-Subprogram.
866 if Ekind (Current_Scope) = E_Protected_Type
867 and then not Protected_Present (Access_To_Subprogram_Definition (N))
869 Build_Itype_Reference (Anon_Type, Parent (Current_Scope));
875 Find_Type (Subtype_Mark (N));
876 Desig_Type := Entity (Subtype_Mark (N));
878 Set_Directly_Designated_Type (Anon_Type, Desig_Type);
879 Set_Etype (Anon_Type, Anon_Type);
881 -- Make sure the anonymous access type has size and alignment fields
882 -- set, as required by gigi. This is necessary in the case of the
883 -- Task_Body_Procedure.
885 if not Has_Private_Component (Desig_Type) then
886 Layout_Type (Anon_Type);
889 -- Ada 2005 (AI-231): Ada 2005 semantics for anonymous access differs
890 -- from Ada 95 semantics. In Ada 2005, anonymous access must specify if
891 -- the null value is allowed. In Ada 95 the null value is never allowed.
893 if Ada_Version >= Ada_2005 then
894 Set_Can_Never_Be_Null (Anon_Type, Null_Exclusion_Present (N));
896 Set_Can_Never_Be_Null (Anon_Type, True);
899 -- The anonymous access type is as public as the discriminated type or
900 -- subprogram that defines it. It is imported (for back-end purposes)
901 -- if the designated type is.
903 Set_Is_Public (Anon_Type, Is_Public (Scope (Anon_Type)));
905 -- Ada 2005 (AI-231): Propagate the access-constant attribute
907 Set_Is_Access_Constant (Anon_Type, Constant_Present (N));
909 -- The context is either a subprogram declaration, object declaration,
910 -- or an access discriminant, in a private or a full type declaration.
911 -- In the case of a subprogram, if the designated type is incomplete,
912 -- the operation will be a primitive operation of the full type, to be
913 -- updated subsequently. If the type is imported through a limited_with
914 -- clause, the subprogram is not a primitive operation of the type
915 -- (which is declared elsewhere in some other scope).
917 if Ekind (Desig_Type) = E_Incomplete_Type
918 and then not From_Limited_With (Desig_Type)
919 and then Is_Overloadable (Current_Scope)
921 Append_Elmt (Current_Scope, Private_Dependents (Desig_Type));
922 Set_Has_Delayed_Freeze (Current_Scope);
925 -- If the designated type is limited and class-wide, the object might
926 -- contain tasks, so we create a Master entity for the declaration. This
927 -- must be done before expansion of the full declaration, because the
928 -- declaration may include an expression that is an allocator, whose
929 -- expansion needs the proper Master for the created tasks.
932 and then Nkind (Related_Nod) = N_Object_Declaration
934 if Is_Limited_Record (Desig_Type)
935 and then Is_Class_Wide_Type (Desig_Type)
936 and then Tasking_Allowed
938 Build_Class_Wide_Master (Anon_Type);
940 -- Similarly, if the type is an anonymous access that designates
941 -- tasks, create a master entity for it in the current context.
943 elsif Has_Task (Desig_Type)
944 and then Comes_From_Source (Related_Nod)
946 Build_Master_Entity (Defining_Identifier (Related_Nod));
947 Build_Master_Renaming (Anon_Type);
951 -- For a private component of a protected type, it is imperative that
952 -- the back-end elaborate the type immediately after the protected
953 -- declaration, because this type will be used in the declarations
954 -- created for the component within each protected body, so we must
955 -- create an itype reference for it now.
957 if Nkind (Parent (Related_Nod)) = N_Protected_Definition then
958 Build_Itype_Reference (Anon_Type, Parent (Parent (Related_Nod)));
960 -- Similarly, if the access definition is the return result of a
961 -- function, create an itype reference for it because it will be used
962 -- within the function body. For a regular function that is not a
963 -- compilation unit, insert reference after the declaration. For a
964 -- protected operation, insert it after the enclosing protected type
965 -- declaration. In either case, do not create a reference for a type
966 -- obtained through a limited_with clause, because this would introduce
967 -- semantic dependencies.
969 -- Similarly, do not create a reference if the designated type is a
970 -- generic formal, because no use of it will reach the backend.
972 elsif Nkind (Related_Nod) = N_Function_Specification
973 and then not From_Limited_With (Desig_Type)
974 and then not Is_Generic_Type (Desig_Type)
976 if Present (Enclosing_Prot_Type) then
977 Build_Itype_Reference (Anon_Type, Parent (Enclosing_Prot_Type));
979 elsif Is_List_Member (Parent (Related_Nod))
980 and then Nkind (Parent (N)) /= N_Parameter_Specification
982 Build_Itype_Reference (Anon_Type, Parent (Related_Nod));
985 -- Finally, create an itype reference for an object declaration of an
986 -- anonymous access type. This is strictly necessary only for deferred
987 -- constants, but in any case will avoid out-of-scope problems in the
990 elsif Nkind (Related_Nod) = N_Object_Declaration then
991 Build_Itype_Reference (Anon_Type, Related_Nod);
995 end Access_Definition;
997 -----------------------------------
998 -- Access_Subprogram_Declaration --
999 -----------------------------------
1001 procedure Access_Subprogram_Declaration
1002 (T_Name : Entity_Id;
1005 procedure Check_For_Premature_Usage (Def : Node_Id);
1006 -- Check that type T_Name is not used, directly or recursively, as a
1007 -- parameter or a return type in Def. Def is either a subtype, an
1008 -- access_definition, or an access_to_subprogram_definition.
1010 -------------------------------
1011 -- Check_For_Premature_Usage --
1012 -------------------------------
1014 procedure Check_For_Premature_Usage (Def : Node_Id) is
1018 -- Check for a subtype mark
1020 if Nkind (Def) in N_Has_Etype then
1021 if Etype (Def) = T_Name then
1023 ("type& cannot be used before end of its declaration", Def);
1026 -- If this is not a subtype, then this is an access_definition
1028 elsif Nkind (Def) = N_Access_Definition then
1029 if Present (Access_To_Subprogram_Definition (Def)) then
1030 Check_For_Premature_Usage
1031 (Access_To_Subprogram_Definition (Def));
1033 Check_For_Premature_Usage (Subtype_Mark (Def));
1036 -- The only cases left are N_Access_Function_Definition and
1037 -- N_Access_Procedure_Definition.
1040 if Present (Parameter_Specifications (Def)) then
1041 Param := First (Parameter_Specifications (Def));
1042 while Present (Param) loop
1043 Check_For_Premature_Usage (Parameter_Type (Param));
1044 Param := Next (Param);
1048 if Nkind (Def) = N_Access_Function_Definition then
1049 Check_For_Premature_Usage (Result_Definition (Def));
1052 end Check_For_Premature_Usage;
1056 Formals : constant List_Id := Parameter_Specifications (T_Def);
1059 Desig_Type : constant Entity_Id :=
1060 Create_Itype (E_Subprogram_Type, Parent (T_Def));
1062 -- Start of processing for Access_Subprogram_Declaration
1065 Check_SPARK_05_Restriction ("access type is not allowed", T_Def);
1067 -- Associate the Itype node with the inner full-type declaration or
1068 -- subprogram spec or entry body. This is required to handle nested
1069 -- anonymous declarations. For example:
1072 -- (X : access procedure
1073 -- (Y : access procedure
1076 D_Ityp := Associated_Node_For_Itype (Desig_Type);
1077 while not (Nkind_In (D_Ityp, N_Full_Type_Declaration,
1078 N_Private_Type_Declaration,
1079 N_Private_Extension_Declaration,
1080 N_Procedure_Specification,
1081 N_Function_Specification,
1085 Nkind_In (D_Ityp, N_Object_Declaration,
1086 N_Object_Renaming_Declaration,
1087 N_Formal_Object_Declaration,
1088 N_Formal_Type_Declaration,
1089 N_Task_Type_Declaration,
1090 N_Protected_Type_Declaration))
1092 D_Ityp := Parent (D_Ityp);
1093 pragma Assert (D_Ityp /= Empty);
1096 Set_Associated_Node_For_Itype (Desig_Type, D_Ityp);
1098 if Nkind_In (D_Ityp, N_Procedure_Specification,
1099 N_Function_Specification)
1101 Set_Scope (Desig_Type, Scope (Defining_Entity (D_Ityp)));
1103 elsif Nkind_In (D_Ityp, N_Full_Type_Declaration,
1104 N_Object_Declaration,
1105 N_Object_Renaming_Declaration,
1106 N_Formal_Type_Declaration)
1108 Set_Scope (Desig_Type, Scope (Defining_Identifier (D_Ityp)));
1111 if Nkind (T_Def) = N_Access_Function_Definition then
1112 if Nkind (Result_Definition (T_Def)) = N_Access_Definition then
1114 Acc : constant Node_Id := Result_Definition (T_Def);
1117 if Present (Access_To_Subprogram_Definition (Acc))
1119 Protected_Present (Access_To_Subprogram_Definition (Acc))
1123 Replace_Anonymous_Access_To_Protected_Subprogram
1129 Access_Definition (T_Def, Result_Definition (T_Def)));
1134 Analyze (Result_Definition (T_Def));
1137 Typ : constant Entity_Id := Entity (Result_Definition (T_Def));
1140 -- If a null exclusion is imposed on the result type, then
1141 -- create a null-excluding itype (an access subtype) and use
1142 -- it as the function's Etype.
1144 if Is_Access_Type (Typ)
1145 and then Null_Exclusion_In_Return_Present (T_Def)
1147 Set_Etype (Desig_Type,
1148 Create_Null_Excluding_Itype
1150 Related_Nod => T_Def,
1151 Scope_Id => Current_Scope));
1154 if From_Limited_With (Typ) then
1156 -- AI05-151: Incomplete types are allowed in all basic
1157 -- declarations, including access to subprograms.
1159 if Ada_Version >= Ada_2012 then
1164 ("illegal use of incomplete type&",
1165 Result_Definition (T_Def), Typ);
1168 elsif Ekind (Current_Scope) = E_Package
1169 and then In_Private_Part (Current_Scope)
1171 if Ekind (Typ) = E_Incomplete_Type then
1172 Append_Elmt (Desig_Type, Private_Dependents (Typ));
1174 elsif Is_Class_Wide_Type (Typ)
1175 and then Ekind (Etype (Typ)) = E_Incomplete_Type
1178 (Desig_Type, Private_Dependents (Etype (Typ)));
1182 Set_Etype (Desig_Type, Typ);
1187 if not (Is_Type (Etype (Desig_Type))) then
1189 ("expect type in function specification",
1190 Result_Definition (T_Def));
1194 Set_Etype (Desig_Type, Standard_Void_Type);
1197 if Present (Formals) then
1198 Push_Scope (Desig_Type);
1200 -- Some special tests here. These special tests can be removed
1201 -- if and when Itypes always have proper parent pointers to their
1204 -- Special test 1) Link defining_identifier of formals. Required by
1205 -- First_Formal to provide its functionality.
1211 F := First (Formals);
1213 -- In ASIS mode, the access_to_subprogram may be analyzed twice,
1214 -- when it is part of an unconstrained type and subtype expansion
1215 -- is disabled. To avoid back-end problems with shared profiles,
1216 -- use previous subprogram type as the designated type, and then
1217 -- remove scope added above.
1219 if ASIS_Mode and then Present (Scope (Defining_Identifier (F)))
1221 Set_Etype (T_Name, T_Name);
1222 Init_Size_Align (T_Name);
1223 Set_Directly_Designated_Type (T_Name,
1224 Scope (Defining_Identifier (F)));
1229 while Present (F) loop
1230 if No (Parent (Defining_Identifier (F))) then
1231 Set_Parent (Defining_Identifier (F), F);
1238 Process_Formals (Formals, Parent (T_Def));
1240 -- Special test 2) End_Scope requires that the parent pointer be set
1241 -- to something reasonable, but Itypes don't have parent pointers. So
1242 -- we set it and then unset it ???
1244 Set_Parent (Desig_Type, T_Name);
1246 Set_Parent (Desig_Type, Empty);
1249 -- Check for premature usage of the type being defined
1251 Check_For_Premature_Usage (T_Def);
1253 -- The return type and/or any parameter type may be incomplete. Mark the
1254 -- subprogram_type as depending on the incomplete type, so that it can
1255 -- be updated when the full type declaration is seen. This only applies
1256 -- to incomplete types declared in some enclosing scope, not to limited
1257 -- views from other packages.
1259 -- Prior to Ada 2012, access to functions can only have in_parameters.
1261 if Present (Formals) then
1262 Formal := First_Formal (Desig_Type);
1263 while Present (Formal) loop
1264 if Ekind (Formal) /= E_In_Parameter
1265 and then Nkind (T_Def) = N_Access_Function_Definition
1266 and then Ada_Version < Ada_2012
1268 Error_Msg_N ("functions can only have IN parameters", Formal);
1271 if Ekind (Etype (Formal)) = E_Incomplete_Type
1272 and then In_Open_Scopes (Scope (Etype (Formal)))
1274 Append_Elmt (Desig_Type, Private_Dependents (Etype (Formal)));
1275 Set_Has_Delayed_Freeze (Desig_Type);
1278 Next_Formal (Formal);
1282 -- Check whether an indirect call without actuals may be possible. This
1283 -- is used when resolving calls whose result is then indexed.
1285 May_Need_Actuals (Desig_Type);
1287 -- If the return type is incomplete, this is legal as long as the type
1288 -- is declared in the current scope and will be completed in it (rather
1289 -- than being part of limited view).
1291 if Ekind (Etype (Desig_Type)) = E_Incomplete_Type
1292 and then not Has_Delayed_Freeze (Desig_Type)
1293 and then In_Open_Scopes (Scope (Etype (Desig_Type)))
1295 Append_Elmt (Desig_Type, Private_Dependents (Etype (Desig_Type)));
1296 Set_Has_Delayed_Freeze (Desig_Type);
1299 Check_Delayed_Subprogram (Desig_Type);
1301 if Protected_Present (T_Def) then
1302 Set_Ekind (T_Name, E_Access_Protected_Subprogram_Type);
1303 Set_Convention (Desig_Type, Convention_Protected);
1305 Set_Ekind (T_Name, E_Access_Subprogram_Type);
1308 Set_Can_Use_Internal_Rep (T_Name,
1309 not Always_Compatible_Rep_On_Target);
1310 Set_Etype (T_Name, T_Name);
1311 Init_Size_Align (T_Name);
1312 Set_Directly_Designated_Type (T_Name, Desig_Type);
1314 -- If the access_to_subprogram is not declared at the library level,
1315 -- it can only point to subprograms that are at the same or deeper
1316 -- accessibility level. The corresponding subprogram type might
1317 -- require an activation record when compiling for C.
1319 Set_Needs_Activation_Record (Desig_Type,
1320 not Is_Library_Level_Entity (T_Name));
1322 Generate_Reference_To_Formals (T_Name);
1324 -- Ada 2005 (AI-231): Propagate the null-excluding attribute
1326 Set_Can_Never_Be_Null (T_Name, Null_Exclusion_Present (T_Def));
1328 Check_Restriction (No_Access_Subprograms, T_Def);
1329 end Access_Subprogram_Declaration;
1331 ----------------------------
1332 -- Access_Type_Declaration --
1333 ----------------------------
1335 procedure Access_Type_Declaration (T : Entity_Id; Def : Node_Id) is
1336 P : constant Node_Id := Parent (Def);
1337 S : constant Node_Id := Subtype_Indication (Def);
1339 Full_Desig : Entity_Id;
1342 Check_SPARK_05_Restriction ("access type is not allowed", Def);
1344 -- Check for permissible use of incomplete type
1346 if Nkind (S) /= N_Subtype_Indication then
1349 if Present (Entity (S))
1350 and then Ekind (Root_Type (Entity (S))) = E_Incomplete_Type
1352 Set_Directly_Designated_Type (T, Entity (S));
1354 -- If the designated type is a limited view, we cannot tell if
1355 -- the full view contains tasks, and there is no way to handle
1356 -- that full view in a client. We create a master entity for the
1357 -- scope, which will be used when a client determines that one
1360 if From_Limited_With (Entity (S))
1361 and then not Is_Class_Wide_Type (Entity (S))
1363 Set_Ekind (T, E_Access_Type);
1364 Build_Master_Entity (T);
1365 Build_Master_Renaming (T);
1369 Set_Directly_Designated_Type (T, Process_Subtype (S, P, T, 'P'));
1372 -- If the access definition is of the form: ACCESS NOT NULL ..
1373 -- the subtype indication must be of an access type. Create
1374 -- a null-excluding subtype of it.
1376 if Null_Excluding_Subtype (Def) then
1377 if not Is_Access_Type (Entity (S)) then
1378 Error_Msg_N ("null exclusion must apply to access type", Def);
1382 Loc : constant Source_Ptr := Sloc (S);
1384 Nam : constant Entity_Id := Make_Temporary (Loc, 'S');
1388 Make_Subtype_Declaration (Loc,
1389 Defining_Identifier => Nam,
1390 Subtype_Indication =>
1391 New_Occurrence_Of (Entity (S), Loc));
1392 Set_Null_Exclusion_Present (Decl);
1393 Insert_Before (Parent (Def), Decl);
1395 Set_Entity (S, Nam);
1401 Set_Directly_Designated_Type (T,
1402 Process_Subtype (S, P, T, 'P'));
1405 if All_Present (Def) or Constant_Present (Def) then
1406 Set_Ekind (T, E_General_Access_Type);
1408 Set_Ekind (T, E_Access_Type);
1411 Full_Desig := Designated_Type (T);
1413 if Base_Type (Full_Desig) = T then
1414 Error_Msg_N ("access type cannot designate itself", S);
1416 -- In Ada 2005, the type may have a limited view through some unit in
1417 -- its own context, allowing the following circularity that cannot be
1418 -- detected earlier.
1420 elsif Is_Class_Wide_Type (Full_Desig) and then Etype (Full_Desig) = T
1423 ("access type cannot designate its own class-wide type", S);
1425 -- Clean up indication of tagged status to prevent cascaded errors
1427 Set_Is_Tagged_Type (T, False);
1432 -- If the type has appeared already in a with_type clause, it is frozen
1433 -- and the pointer size is already set. Else, initialize.
1435 if not From_Limited_With (T) then
1436 Init_Size_Align (T);
1439 -- Note that Has_Task is always false, since the access type itself
1440 -- is not a task type. See Einfo for more description on this point.
1441 -- Exactly the same consideration applies to Has_Controlled_Component
1442 -- and to Has_Protected.
1444 Set_Has_Task (T, False);
1445 Set_Has_Protected (T, False);
1446 Set_Has_Timing_Event (T, False);
1447 Set_Has_Controlled_Component (T, False);
1449 -- Initialize field Finalization_Master explicitly to Empty, to avoid
1450 -- problems where an incomplete view of this entity has been previously
1451 -- established by a limited with and an overlaid version of this field
1452 -- (Stored_Constraint) was initialized for the incomplete view.
1454 -- This reset is performed in most cases except where the access type
1455 -- has been created for the purposes of allocating or deallocating a
1456 -- build-in-place object. Such access types have explicitly set pools
1457 -- and finalization masters.
1459 if No (Associated_Storage_Pool (T)) then
1460 Set_Finalization_Master (T, Empty);
1463 -- Ada 2005 (AI-231): Propagate the null-excluding and access-constant
1466 Set_Can_Never_Be_Null (T, Null_Exclusion_Present (Def));
1467 Set_Is_Access_Constant (T, Constant_Present (Def));
1468 end Access_Type_Declaration;
1470 ----------------------------------
1471 -- Add_Interface_Tag_Components --
1472 ----------------------------------
1474 procedure Add_Interface_Tag_Components (N : Node_Id; Typ : Entity_Id) is
1475 Loc : constant Source_Ptr := Sloc (N);
1479 procedure Add_Tag (Iface : Entity_Id);
1480 -- Add tag for one of the progenitor interfaces
1486 procedure Add_Tag (Iface : Entity_Id) is
1493 pragma Assert (Is_Tagged_Type (Iface) and then Is_Interface (Iface));
1495 -- This is a reasonable place to propagate predicates
1497 if Has_Predicates (Iface) then
1498 Set_Has_Predicates (Typ);
1502 Make_Component_Definition (Loc,
1503 Aliased_Present => True,
1504 Subtype_Indication =>
1505 New_Occurrence_Of (RTE (RE_Interface_Tag), Loc));
1507 Tag := Make_Temporary (Loc, 'V');
1510 Make_Component_Declaration (Loc,
1511 Defining_Identifier => Tag,
1512 Component_Definition => Def);
1514 Analyze_Component_Declaration (Decl);
1516 Set_Analyzed (Decl);
1517 Set_Ekind (Tag, E_Component);
1519 Set_Is_Aliased (Tag);
1520 Set_Related_Type (Tag, Iface);
1521 Init_Component_Location (Tag);
1523 pragma Assert (Is_Frozen (Iface));
1525 Set_DT_Entry_Count (Tag,
1526 DT_Entry_Count (First_Entity (Iface)));
1528 if No (Last_Tag) then
1531 Insert_After (Last_Tag, Decl);
1536 -- If the ancestor has discriminants we need to give special support
1537 -- to store the offset_to_top value of the secondary dispatch tables.
1538 -- For this purpose we add a supplementary component just after the
1539 -- field that contains the tag associated with each secondary DT.
1541 if Typ /= Etype (Typ) and then Has_Discriminants (Etype (Typ)) then
1543 Make_Component_Definition (Loc,
1544 Subtype_Indication =>
1545 New_Occurrence_Of (RTE (RE_Storage_Offset), Loc));
1547 Offset := Make_Temporary (Loc, 'V');
1550 Make_Component_Declaration (Loc,
1551 Defining_Identifier => Offset,
1552 Component_Definition => Def);
1554 Analyze_Component_Declaration (Decl);
1556 Set_Analyzed (Decl);
1557 Set_Ekind (Offset, E_Component);
1558 Set_Is_Aliased (Offset);
1559 Set_Related_Type (Offset, Iface);
1560 Init_Component_Location (Offset);
1561 Insert_After (Last_Tag, Decl);
1572 -- Start of processing for Add_Interface_Tag_Components
1575 if not RTE_Available (RE_Interface_Tag) then
1577 ("(Ada 2005) interface types not supported by this run-time!",
1582 if Ekind (Typ) /= E_Record_Type
1583 or else (Is_Concurrent_Record_Type (Typ)
1584 and then Is_Empty_List (Abstract_Interface_List (Typ)))
1585 or else (not Is_Concurrent_Record_Type (Typ)
1586 and then No (Interfaces (Typ))
1587 and then Is_Empty_Elmt_List (Interfaces (Typ)))
1592 -- Find the current last tag
1594 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1595 Ext := Record_Extension_Part (Type_Definition (N));
1597 pragma Assert (Nkind (Type_Definition (N)) = N_Record_Definition);
1598 Ext := Type_Definition (N);
1603 if not (Present (Component_List (Ext))) then
1604 Set_Null_Present (Ext, False);
1606 Set_Component_List (Ext,
1607 Make_Component_List (Loc,
1608 Component_Items => L,
1609 Null_Present => False));
1611 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
1612 L := Component_Items
1614 (Record_Extension_Part
1615 (Type_Definition (N))));
1617 L := Component_Items
1619 (Type_Definition (N)));
1622 -- Find the last tag component
1625 while Present (Comp) loop
1626 if Nkind (Comp) = N_Component_Declaration
1627 and then Is_Tag (Defining_Identifier (Comp))
1636 -- At this point L references the list of components and Last_Tag
1637 -- references the current last tag (if any). Now we add the tag
1638 -- corresponding with all the interfaces that are not implemented
1641 if Present (Interfaces (Typ)) then
1642 Elmt := First_Elmt (Interfaces (Typ));
1643 while Present (Elmt) loop
1644 Add_Tag (Node (Elmt));
1648 end Add_Interface_Tag_Components;
1650 -------------------------------------
1651 -- Add_Internal_Interface_Entities --
1652 -------------------------------------
1654 procedure Add_Internal_Interface_Entities (Tagged_Type : Entity_Id) is
1657 Iface_Elmt : Elmt_Id;
1658 Iface_Prim : Entity_Id;
1659 Ifaces_List : Elist_Id;
1660 New_Subp : Entity_Id := Empty;
1662 Restore_Scope : Boolean := False;
1665 pragma Assert (Ada_Version >= Ada_2005
1666 and then Is_Record_Type (Tagged_Type)
1667 and then Is_Tagged_Type (Tagged_Type)
1668 and then Has_Interfaces (Tagged_Type)
1669 and then not Is_Interface (Tagged_Type));
1671 -- Ensure that the internal entities are added to the scope of the type
1673 if Scope (Tagged_Type) /= Current_Scope then
1674 Push_Scope (Scope (Tagged_Type));
1675 Restore_Scope := True;
1678 Collect_Interfaces (Tagged_Type, Ifaces_List);
1680 Iface_Elmt := First_Elmt (Ifaces_List);
1681 while Present (Iface_Elmt) loop
1682 Iface := Node (Iface_Elmt);
1684 -- Originally we excluded here from this processing interfaces that
1685 -- are parents of Tagged_Type because their primitives are located
1686 -- in the primary dispatch table (and hence no auxiliary internal
1687 -- entities are required to handle secondary dispatch tables in such
1688 -- case). However, these auxiliary entities are also required to
1689 -- handle derivations of interfaces in formals of generics (see
1690 -- Derive_Subprograms).
1692 Elmt := First_Elmt (Primitive_Operations (Iface));
1693 while Present (Elmt) loop
1694 Iface_Prim := Node (Elmt);
1696 if not Is_Predefined_Dispatching_Operation (Iface_Prim) then
1698 Find_Primitive_Covering_Interface
1699 (Tagged_Type => Tagged_Type,
1700 Iface_Prim => Iface_Prim);
1702 if No (Prim) and then Serious_Errors_Detected > 0 then
1706 pragma Assert (Present (Prim));
1708 -- Ada 2012 (AI05-0197): If the name of the covering primitive
1709 -- differs from the name of the interface primitive then it is
1710 -- a private primitive inherited from a parent type. In such
1711 -- case, given that Tagged_Type covers the interface, the
1712 -- inherited private primitive becomes visible. For such
1713 -- purpose we add a new entity that renames the inherited
1714 -- private primitive.
1716 if Chars (Prim) /= Chars (Iface_Prim) then
1717 pragma Assert (Has_Suffix (Prim, 'P'));
1719 (New_Subp => New_Subp,
1720 Parent_Subp => Iface_Prim,
1721 Derived_Type => Tagged_Type,
1722 Parent_Type => Iface);
1723 Set_Alias (New_Subp, Prim);
1724 Set_Is_Abstract_Subprogram
1725 (New_Subp, Is_Abstract_Subprogram (Prim));
1729 (New_Subp => New_Subp,
1730 Parent_Subp => Iface_Prim,
1731 Derived_Type => Tagged_Type,
1732 Parent_Type => Iface);
1737 if Is_Inherited_Operation (Prim)
1738 and then Present (Alias (Prim))
1740 Anc := Alias (Prim);
1742 Anc := Overridden_Operation (Prim);
1745 -- Apply legality checks in RM 6.1.1 (10-13) concerning
1746 -- nonconforming preconditions in both an ancestor and
1747 -- a progenitor operation.
1749 -- If the operation is a primitive wrapper it is an explicit
1750 -- (overriding) operqtion and all is fine.
1753 and then Has_Non_Trivial_Precondition (Anc)
1754 and then Has_Non_Trivial_Precondition (Iface_Prim)
1756 if Is_Abstract_Subprogram (Prim)
1758 (Ekind (Prim) = E_Procedure
1759 and then Nkind (Parent (Prim)) =
1760 N_Procedure_Specification
1761 and then Null_Present (Parent (Prim)))
1762 or else Is_Primitive_Wrapper (Prim)
1766 -- The operation is inherited and must be overridden
1768 elsif not Comes_From_Source (Prim) then
1770 ("&inherits non-conforming preconditions and must "
1771 & "be overridden (RM 6.1.1 (10-16)",
1772 Parent (Tagged_Type), Prim);
1777 -- Ada 2005 (AI-251): Decorate internal entity Iface_Subp
1778 -- associated with interface types. These entities are
1779 -- only registered in the list of primitives of its
1780 -- corresponding tagged type because they are only used
1781 -- to fill the contents of the secondary dispatch tables.
1782 -- Therefore they are removed from the homonym chains.
1784 Set_Is_Hidden (New_Subp);
1785 Set_Is_Internal (New_Subp);
1786 Set_Alias (New_Subp, Prim);
1787 Set_Is_Abstract_Subprogram
1788 (New_Subp, Is_Abstract_Subprogram (Prim));
1789 Set_Interface_Alias (New_Subp, Iface_Prim);
1791 -- If the returned type is an interface then propagate it to
1792 -- the returned type. Needed by the thunk to generate the code
1793 -- which displaces "this" to reference the corresponding
1794 -- secondary dispatch table in the returned object.
1796 if Is_Interface (Etype (Iface_Prim)) then
1797 Set_Etype (New_Subp, Etype (Iface_Prim));
1800 -- Internal entities associated with interface types are only
1801 -- registered in the list of primitives of the tagged type.
1802 -- They are only used to fill the contents of the secondary
1803 -- dispatch tables. Therefore they are not needed in the
1806 Remove_Homonym (New_Subp);
1808 -- Hidden entities associated with interfaces must have set
1809 -- the Has_Delay_Freeze attribute to ensure that, in case
1810 -- of locally defined tagged types (or compiling with static
1811 -- dispatch tables generation disabled) the corresponding
1812 -- entry of the secondary dispatch table is filled when such
1813 -- an entity is frozen. This is an expansion activity that must
1814 -- be suppressed for ASIS because it leads to gigi elaboration
1815 -- issues in annotate mode.
1817 if not ASIS_Mode then
1818 Set_Has_Delayed_Freeze (New_Subp);
1826 Next_Elmt (Iface_Elmt);
1829 if Restore_Scope then
1832 end Add_Internal_Interface_Entities;
1834 -----------------------------------
1835 -- Analyze_Component_Declaration --
1836 -----------------------------------
1838 procedure Analyze_Component_Declaration (N : Node_Id) is
1839 Loc : constant Source_Ptr := Sloc (Component_Definition (N));
1840 Id : constant Entity_Id := Defining_Identifier (N);
1841 E : constant Node_Id := Expression (N);
1842 Typ : constant Node_Id :=
1843 Subtype_Indication (Component_Definition (N));
1847 function Contains_POC (Constr : Node_Id) return Boolean;
1848 -- Determines whether a constraint uses the discriminant of a record
1849 -- type thus becoming a per-object constraint (POC).
1851 function Is_Known_Limited (Typ : Entity_Id) return Boolean;
1852 -- Typ is the type of the current component, check whether this type is
1853 -- a limited type. Used to validate declaration against that of
1854 -- enclosing record.
1860 function Contains_POC (Constr : Node_Id) return Boolean is
1862 -- Prevent cascaded errors
1864 if Error_Posted (Constr) then
1868 case Nkind (Constr) is
1869 when N_Attribute_Reference =>
1870 return Attribute_Name (Constr) = Name_Access
1871 and then Prefix (Constr) = Scope (Entity (Prefix (Constr)));
1873 when N_Discriminant_Association =>
1874 return Denotes_Discriminant (Expression (Constr));
1876 when N_Identifier =>
1877 return Denotes_Discriminant (Constr);
1879 when N_Index_Or_Discriminant_Constraint =>
1884 IDC := First (Constraints (Constr));
1885 while Present (IDC) loop
1887 -- One per-object constraint is sufficient
1889 if Contains_POC (IDC) then
1900 return Denotes_Discriminant (Low_Bound (Constr))
1902 Denotes_Discriminant (High_Bound (Constr));
1904 when N_Range_Constraint =>
1905 return Denotes_Discriminant (Range_Expression (Constr));
1912 ----------------------
1913 -- Is_Known_Limited --
1914 ----------------------
1916 function Is_Known_Limited (Typ : Entity_Id) return Boolean is
1917 P : constant Entity_Id := Etype (Typ);
1918 R : constant Entity_Id := Root_Type (Typ);
1921 if Is_Limited_Record (Typ) then
1924 -- If the root type is limited (and not a limited interface) so is
1925 -- the current type.
1927 elsif Is_Limited_Record (R)
1928 and then (not Is_Interface (R) or else not Is_Limited_Interface (R))
1932 -- Else the type may have a limited interface progenitor, but a
1933 -- limited record parent that is not an interface.
1936 and then Is_Limited_Record (P)
1937 and then not Is_Interface (P)
1944 end Is_Known_Limited;
1946 -- Start of processing for Analyze_Component_Declaration
1949 Generate_Definition (Id);
1952 if Present (Typ) then
1953 T := Find_Type_Of_Object
1954 (Subtype_Indication (Component_Definition (N)), N);
1956 if not Nkind_In (Typ, N_Identifier, N_Expanded_Name) then
1957 Check_SPARK_05_Restriction ("subtype mark required", Typ);
1960 -- Ada 2005 (AI-230): Access Definition case
1963 pragma Assert (Present
1964 (Access_Definition (Component_Definition (N))));
1966 T := Access_Definition
1968 N => Access_Definition (Component_Definition (N)));
1969 Set_Is_Local_Anonymous_Access (T);
1971 -- Ada 2005 (AI-254)
1973 if Present (Access_To_Subprogram_Definition
1974 (Access_Definition (Component_Definition (N))))
1975 and then Protected_Present (Access_To_Subprogram_Definition
1977 (Component_Definition (N))))
1979 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
1983 -- If the subtype is a constrained subtype of the enclosing record,
1984 -- (which must have a partial view) the back-end does not properly
1985 -- handle the recursion. Rewrite the component declaration with an
1986 -- explicit subtype indication, which is acceptable to Gigi. We can copy
1987 -- the tree directly because side effects have already been removed from
1988 -- discriminant constraints.
1990 if Ekind (T) = E_Access_Subtype
1991 and then Is_Entity_Name (Subtype_Indication (Component_Definition (N)))
1992 and then Comes_From_Source (T)
1993 and then Nkind (Parent (T)) = N_Subtype_Declaration
1994 and then Etype (Directly_Designated_Type (T)) = Current_Scope
1997 (Subtype_Indication (Component_Definition (N)),
1998 New_Copy_Tree (Subtype_Indication (Parent (T))));
1999 T := Find_Type_Of_Object
2000 (Subtype_Indication (Component_Definition (N)), N);
2003 -- If the component declaration includes a default expression, then we
2004 -- check that the component is not of a limited type (RM 3.7(5)),
2005 -- and do the special preanalysis of the expression (see section on
2006 -- "Handling of Default and Per-Object Expressions" in the spec of
2010 Check_SPARK_05_Restriction ("default expression is not allowed", E);
2011 Preanalyze_Default_Expression (E, T);
2012 Check_Initialization (T, E);
2014 if Ada_Version >= Ada_2005
2015 and then Ekind (T) = E_Anonymous_Access_Type
2016 and then Etype (E) /= Any_Type
2018 -- Check RM 3.9.2(9): "if the expected type for an expression is
2019 -- an anonymous access-to-specific tagged type, then the object
2020 -- designated by the expression shall not be dynamically tagged
2021 -- unless it is a controlling operand in a call on a dispatching
2024 if Is_Tagged_Type (Directly_Designated_Type (T))
2026 Ekind (Directly_Designated_Type (T)) /= E_Class_Wide_Type
2028 Ekind (Directly_Designated_Type (Etype (E))) =
2032 ("access to specific tagged type required (RM 3.9.2(9))", E);
2035 -- (Ada 2005: AI-230): Accessibility check for anonymous
2038 if Type_Access_Level (Etype (E)) >
2039 Deepest_Type_Access_Level (T)
2042 ("expression has deeper access level than component " &
2043 "(RM 3.10.2 (12.2))", E);
2046 -- The initialization expression is a reference to an access
2047 -- discriminant. The type of the discriminant is always deeper
2048 -- than any access type.
2050 if Ekind (Etype (E)) = E_Anonymous_Access_Type
2051 and then Is_Entity_Name (E)
2052 and then Ekind (Entity (E)) = E_In_Parameter
2053 and then Present (Discriminal_Link (Entity (E)))
2056 ("discriminant has deeper accessibility level than target",
2062 -- The parent type may be a private view with unknown discriminants,
2063 -- and thus unconstrained. Regular components must be constrained.
2065 if not Is_Definite_Subtype (T) and then Chars (Id) /= Name_uParent then
2066 if Is_Class_Wide_Type (T) then
2068 ("class-wide subtype with unknown discriminants" &
2069 " in component declaration",
2070 Subtype_Indication (Component_Definition (N)));
2073 ("unconstrained subtype in component declaration",
2074 Subtype_Indication (Component_Definition (N)));
2077 -- Components cannot be abstract, except for the special case of
2078 -- the _Parent field (case of extending an abstract tagged type)
2080 elsif Is_Abstract_Type (T) and then Chars (Id) /= Name_uParent then
2081 Error_Msg_N ("type of a component cannot be abstract", N);
2085 Set_Is_Aliased (Id, Aliased_Present (Component_Definition (N)));
2087 -- The component declaration may have a per-object constraint, set
2088 -- the appropriate flag in the defining identifier of the subtype.
2090 if Present (Subtype_Indication (Component_Definition (N))) then
2092 Sindic : constant Node_Id :=
2093 Subtype_Indication (Component_Definition (N));
2095 if Nkind (Sindic) = N_Subtype_Indication
2096 and then Present (Constraint (Sindic))
2097 and then Contains_POC (Constraint (Sindic))
2099 Set_Has_Per_Object_Constraint (Id);
2104 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
2105 -- out some static checks.
2107 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (T) then
2108 Null_Exclusion_Static_Checks (N);
2111 -- If this component is private (or depends on a private type), flag the
2112 -- record type to indicate that some operations are not available.
2114 P := Private_Component (T);
2118 -- Check for circular definitions
2120 if P = Any_Type then
2121 Set_Etype (Id, Any_Type);
2123 -- There is a gap in the visibility of operations only if the
2124 -- component type is not defined in the scope of the record type.
2126 elsif Scope (P) = Scope (Current_Scope) then
2129 elsif Is_Limited_Type (P) then
2130 Set_Is_Limited_Composite (Current_Scope);
2133 Set_Is_Private_Composite (Current_Scope);
2138 and then Is_Limited_Type (T)
2139 and then Chars (Id) /= Name_uParent
2140 and then Is_Tagged_Type (Current_Scope)
2142 if Is_Derived_Type (Current_Scope)
2143 and then not Is_Known_Limited (Current_Scope)
2146 ("extension of nonlimited type cannot have limited components",
2149 if Is_Interface (Root_Type (Current_Scope)) then
2151 ("\limitedness is not inherited from limited interface", N);
2152 Error_Msg_N ("\add LIMITED to type indication", N);
2155 Explain_Limited_Type (T, N);
2156 Set_Etype (Id, Any_Type);
2157 Set_Is_Limited_Composite (Current_Scope, False);
2159 elsif not Is_Derived_Type (Current_Scope)
2160 and then not Is_Limited_Record (Current_Scope)
2161 and then not Is_Concurrent_Type (Current_Scope)
2164 ("nonlimited tagged type cannot have limited components", N);
2165 Explain_Limited_Type (T, N);
2166 Set_Etype (Id, Any_Type);
2167 Set_Is_Limited_Composite (Current_Scope, False);
2171 -- If the component is an unconstrained task or protected type with
2172 -- discriminants, the component and the enclosing record are limited
2173 -- and the component is constrained by its default values. Compute
2174 -- its actual subtype, else it may be allocated the maximum size by
2175 -- the backend, and possibly overflow.
2177 if Is_Concurrent_Type (T)
2178 and then not Is_Constrained (T)
2179 and then Has_Discriminants (T)
2180 and then not Has_Discriminants (Current_Scope)
2183 Act_T : constant Entity_Id := Build_Default_Subtype (T, N);
2186 Set_Etype (Id, Act_T);
2188 -- Rewrite component definition to use the constrained subtype
2190 Rewrite (Component_Definition (N),
2191 Make_Component_Definition (Loc,
2192 Subtype_Indication => New_Occurrence_Of (Act_T, Loc)));
2196 Set_Original_Record_Component (Id, Id);
2198 if Has_Aspects (N) then
2199 Analyze_Aspect_Specifications (N, Id);
2202 Analyze_Dimension (N);
2203 end Analyze_Component_Declaration;
2205 --------------------------
2206 -- Analyze_Declarations --
2207 --------------------------
2209 procedure Analyze_Declarations (L : List_Id) is
2212 procedure Adjust_Decl;
2213 -- Adjust Decl not to include implicit label declarations, since these
2214 -- have strange Sloc values that result in elaboration check problems.
2215 -- (They have the sloc of the label as found in the source, and that
2216 -- is ahead of the current declarative part).
2218 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id);
2219 -- Create the subprogram bodies which verify the run-time semantics of
2220 -- the pragmas listed below for each elibigle type found in declarative
2221 -- list Decls. The pragmas are:
2223 -- Default_Initial_Condition
2227 -- Context denotes the owner of the declarative list.
2229 procedure Check_Entry_Contracts;
2230 -- Perform a preanalysis of the pre- and postconditions of an entry
2231 -- declaration. This must be done before full resolution and creation
2232 -- of the parameter block, etc. to catch illegal uses within the
2233 -- contract expression. Full analysis of the expression is done when
2234 -- the contract is processed.
2236 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean;
2237 -- Check if a nested package has entities within it that rely on library
2238 -- level private types where the full view has not been completed for
2239 -- the purposes of checking if it is acceptable to freeze an expression
2240 -- function at the point of declaration.
2242 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id);
2243 -- Determine whether Body_Decl denotes the body of a late controlled
2244 -- primitive (either Initialize, Adjust or Finalize). If this is the
2245 -- case, add a proper spec if the body lacks one. The spec is inserted
2246 -- before Body_Decl and immediately analyzed.
2248 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id);
2249 -- Spec_Id is the entity of a package that may define abstract states,
2250 -- and in the case of a child unit, whose ancestors may define abstract
2251 -- states. If the states have partial visible refinement, remove the
2252 -- partial visibility of each constituent at the end of the package
2253 -- spec and body declarations.
2255 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id);
2256 -- Spec_Id is the entity of a package that may define abstract states.
2257 -- If the states have visible refinement, remove the visibility of each
2258 -- constituent at the end of the package body declaration.
2260 procedure Resolve_Aspects;
2261 -- Utility to resolve the expressions of aspects at the end of a list of
2262 -- declarations, or before a declaration that freezes previous entities,
2263 -- such as in a subprogram body.
2269 procedure Adjust_Decl is
2271 while Present (Prev (Decl))
2272 and then Nkind (Decl) = N_Implicit_Label_Declaration
2278 ----------------------------
2279 -- Build_Assertion_Bodies --
2280 ----------------------------
2282 procedure Build_Assertion_Bodies (Decls : List_Id; Context : Node_Id) is
2283 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id);
2284 -- Create the subprogram bodies which verify the run-time semantics
2285 -- of the pragmas listed below for type Typ. The pragmas are:
2287 -- Default_Initial_Condition
2291 -------------------------------------
2292 -- Build_Assertion_Bodies_For_Type --
2293 -------------------------------------
2295 procedure Build_Assertion_Bodies_For_Type (Typ : Entity_Id) is
2297 -- Preanalyze and resolve the Default_Initial_Condition assertion
2298 -- expression at the end of the declarations to catch any errors.
2300 if Has_DIC (Typ) then
2301 Build_DIC_Procedure_Body (Typ);
2304 if Nkind (Context) = N_Package_Specification then
2306 -- Preanalyze and resolve the class-wide invariants of an
2307 -- interface at the end of whichever declarative part has the
2308 -- interface type. Note that an interface may be declared in
2309 -- any non-package declarative part, but reaching the end of
2310 -- such a declarative part will always freeze the type and
2311 -- generate the invariant procedure (see Freeze_Type).
2313 if Is_Interface (Typ) then
2315 -- Interfaces are treated as the partial view of a private
2316 -- type, in order to achieve uniformity with the general
2317 -- case. As a result, an interface receives only a "partial"
2318 -- invariant procedure, which is never called.
2320 if Has_Own_Invariants (Typ) then
2321 Build_Invariant_Procedure_Body
2323 Partial_Invariant => True);
2326 -- Preanalyze and resolve the invariants of a private type
2327 -- at the end of the visible declarations to catch potential
2328 -- errors. Inherited class-wide invariants are not included
2329 -- because they have already been resolved.
2331 elsif Decls = Visible_Declarations (Context)
2332 and then Ekind_In (Typ, E_Limited_Private_Type,
2334 E_Record_Type_With_Private)
2335 and then Has_Own_Invariants (Typ)
2337 Build_Invariant_Procedure_Body
2339 Partial_Invariant => True);
2341 -- Preanalyze and resolve the invariants of a private type's
2342 -- full view at the end of the private declarations to catch
2343 -- potential errors.
2345 elsif Decls = Private_Declarations (Context)
2346 and then not Is_Private_Type (Typ)
2347 and then Has_Private_Declaration (Typ)
2348 and then Has_Invariants (Typ)
2350 Build_Invariant_Procedure_Body (Typ);
2353 end Build_Assertion_Bodies_For_Type;
2358 Decl_Id : Entity_Id;
2360 -- Start of processing for Build_Assertion_Bodies
2363 Decl := First (Decls);
2364 while Present (Decl) loop
2365 if Is_Declaration (Decl) then
2366 Decl_Id := Defining_Entity (Decl);
2368 if Is_Type (Decl_Id) then
2369 Build_Assertion_Bodies_For_Type (Decl_Id);
2375 end Build_Assertion_Bodies;
2377 ---------------------------
2378 -- Check_Entry_Contracts --
2379 ---------------------------
2381 procedure Check_Entry_Contracts is
2387 Ent := First_Entity (Current_Scope);
2388 while Present (Ent) loop
2390 -- This only concerns entries with pre/postconditions
2392 if Ekind (Ent) = E_Entry
2393 and then Present (Contract (Ent))
2394 and then Present (Pre_Post_Conditions (Contract (Ent)))
2396 ASN := Pre_Post_Conditions (Contract (Ent));
2398 Install_Formals (Ent);
2400 -- Pre/postconditions are rewritten as Check pragmas. Analysis
2401 -- is performed on a copy of the pragma expression, to prevent
2402 -- modifying the original expression.
2404 while Present (ASN) loop
2405 if Nkind (ASN) = N_Pragma then
2409 (First (Pragma_Argument_Associations (ASN))));
2410 Set_Parent (Exp, ASN);
2412 Preanalyze_Assert_Expression (Exp, Standard_Boolean);
2415 ASN := Next_Pragma (ASN);
2423 end Check_Entry_Contracts;
2425 ----------------------------------
2426 -- Contains_Lib_Incomplete_Type --
2427 ----------------------------------
2429 function Contains_Lib_Incomplete_Type (Pkg : Entity_Id) return Boolean is
2433 -- Avoid looking through scopes that do not meet the precondition of
2434 -- Pkg not being within a library unit spec.
2436 if not Is_Compilation_Unit (Pkg)
2437 and then not Is_Generic_Instance (Pkg)
2438 and then not In_Package_Body (Enclosing_Lib_Unit_Entity (Pkg))
2440 -- Loop through all entities in the current scope to identify
2441 -- an entity that depends on a private type.
2443 Curr := First_Entity (Pkg);
2445 if Nkind (Curr) in N_Entity
2446 and then Depends_On_Private (Curr)
2451 exit when Last_Entity (Current_Scope) = Curr;
2452 Curr := Next_Entity (Curr);
2457 end Contains_Lib_Incomplete_Type;
2459 --------------------------------------
2460 -- Handle_Late_Controlled_Primitive --
2461 --------------------------------------
2463 procedure Handle_Late_Controlled_Primitive (Body_Decl : Node_Id) is
2464 Body_Spec : constant Node_Id := Specification (Body_Decl);
2465 Body_Id : constant Entity_Id := Defining_Entity (Body_Spec);
2466 Loc : constant Source_Ptr := Sloc (Body_Id);
2467 Params : constant List_Id :=
2468 Parameter_Specifications (Body_Spec);
2470 Spec_Id : Entity_Id;
2474 -- Consider only procedure bodies whose name matches one of the three
2475 -- controlled primitives.
2477 if Nkind (Body_Spec) /= N_Procedure_Specification
2478 or else not Nam_In (Chars (Body_Id), Name_Adjust,
2484 -- A controlled primitive must have exactly one formal which is not
2485 -- an anonymous access type.
2487 elsif List_Length (Params) /= 1 then
2491 Typ := Parameter_Type (First (Params));
2493 if Nkind (Typ) = N_Access_Definition then
2499 -- The type of the formal must be derived from [Limited_]Controlled
2501 if not Is_Controlled (Entity (Typ)) then
2505 -- Check whether a specification exists for this body. We do not
2506 -- analyze the spec of the body in full, because it will be analyzed
2507 -- again when the body is properly analyzed, and we cannot create
2508 -- duplicate entries in the formals chain. We look for an explicit
2509 -- specification because the body may be an overriding operation and
2510 -- an inherited spec may be present.
2512 Spec_Id := Current_Entity (Body_Id);
2514 while Present (Spec_Id) loop
2515 if Ekind_In (Spec_Id, E_Procedure, E_Generic_Procedure)
2516 and then Scope (Spec_Id) = Current_Scope
2517 and then Present (First_Formal (Spec_Id))
2518 and then No (Next_Formal (First_Formal (Spec_Id)))
2519 and then Etype (First_Formal (Spec_Id)) = Entity (Typ)
2520 and then Comes_From_Source (Spec_Id)
2525 Spec_Id := Homonym (Spec_Id);
2528 -- At this point the body is known to be a late controlled primitive.
2529 -- Generate a matching spec and insert it before the body. Note the
2530 -- use of Copy_Separate_Tree - we want an entirely separate semantic
2531 -- tree in this case.
2533 Spec := Copy_Separate_Tree (Body_Spec);
2535 -- Ensure that the subprogram declaration does not inherit the null
2536 -- indicator from the body as we now have a proper spec/body pair.
2538 Set_Null_Present (Spec, False);
2540 -- Ensure that the freeze node is inserted after the declaration of
2541 -- the primitive since its expansion will freeze the primitive.
2543 Decl := Make_Subprogram_Declaration (Loc, Specification => Spec);
2545 Insert_Before_And_Analyze (Body_Decl, Decl);
2546 end Handle_Late_Controlled_Primitive;
2548 ----------------------------------------
2549 -- Remove_Partial_Visible_Refinements --
2550 ----------------------------------------
2552 procedure Remove_Partial_Visible_Refinements (Spec_Id : Entity_Id) is
2553 State_Elmt : Elmt_Id;
2555 if Present (Abstract_States (Spec_Id)) then
2556 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2557 while Present (State_Elmt) loop
2558 Set_Has_Partial_Visible_Refinement (Node (State_Elmt), False);
2559 Next_Elmt (State_Elmt);
2563 -- For a child unit, also hide the partial state refinement from
2564 -- ancestor packages.
2566 if Is_Child_Unit (Spec_Id) then
2567 Remove_Partial_Visible_Refinements (Scope (Spec_Id));
2569 end Remove_Partial_Visible_Refinements;
2571 --------------------------------
2572 -- Remove_Visible_Refinements --
2573 --------------------------------
2575 procedure Remove_Visible_Refinements (Spec_Id : Entity_Id) is
2576 State_Elmt : Elmt_Id;
2578 if Present (Abstract_States (Spec_Id)) then
2579 State_Elmt := First_Elmt (Abstract_States (Spec_Id));
2580 while Present (State_Elmt) loop
2581 Set_Has_Visible_Refinement (Node (State_Elmt), False);
2582 Next_Elmt (State_Elmt);
2585 end Remove_Visible_Refinements;
2587 ---------------------
2588 -- Resolve_Aspects --
2589 ---------------------
2591 procedure Resolve_Aspects is
2595 E := First_Entity (Current_Scope);
2596 while Present (E) loop
2597 Resolve_Aspect_Expressions (E);
2600 end Resolve_Aspects;
2604 Context : Node_Id := Empty;
2605 Freeze_From : Entity_Id := Empty;
2606 Next_Decl : Node_Id;
2608 Body_Seen : Boolean := False;
2609 -- Flag set when the first body [stub] is encountered
2611 -- Start of processing for Analyze_Declarations
2614 if Restriction_Check_Required (SPARK_05) then
2615 Check_Later_Vs_Basic_Declarations (L, During_Parsing => False);
2619 while Present (Decl) loop
2621 -- Package spec cannot contain a package declaration in SPARK
2623 if Nkind (Decl) = N_Package_Declaration
2624 and then Nkind (Parent (L)) = N_Package_Specification
2626 Check_SPARK_05_Restriction
2627 ("package specification cannot contain a package declaration",
2631 -- Complete analysis of declaration
2634 Next_Decl := Next (Decl);
2636 if No (Freeze_From) then
2637 Freeze_From := First_Entity (Current_Scope);
2640 -- At the end of a declarative part, freeze remaining entities
2641 -- declared in it. The end of the visible declarations of package
2642 -- specification is not the end of a declarative part if private
2643 -- declarations are present. The end of a package declaration is a
2644 -- freezing point only if it a library package. A task definition or
2645 -- protected type definition is not a freeze point either. Finally,
2646 -- we do not freeze entities in generic scopes, because there is no
2647 -- code generated for them and freeze nodes will be generated for
2650 -- The end of a package instantiation is not a freeze point, but
2651 -- for now we make it one, because the generic body is inserted
2652 -- (currently) immediately after. Generic instantiations will not
2653 -- be a freeze point once delayed freezing of bodies is implemented.
2654 -- (This is needed in any case for early instantiations ???).
2656 if No (Next_Decl) then
2657 if Nkind (Parent (L)) = N_Component_List then
2660 elsif Nkind_In (Parent (L), N_Protected_Definition,
2663 Check_Entry_Contracts;
2665 elsif Nkind (Parent (L)) /= N_Package_Specification then
2666 if Nkind (Parent (L)) = N_Package_Body then
2667 Freeze_From := First_Entity (Current_Scope);
2670 -- There may have been several freezing points previously,
2671 -- for example object declarations or subprogram bodies, but
2672 -- at the end of a declarative part we check freezing from
2673 -- the beginning, even though entities may already be frozen,
2674 -- in order to perform visibility checks on delayed aspects.
2678 -- If the current scope is a generic subprogram body. Skip the
2679 -- generic formal parameters that are not frozen here.
2681 if Is_Subprogram (Current_Scope)
2682 and then Nkind (Unit_Declaration_Node (Current_Scope)) =
2683 N_Generic_Subprogram_Declaration
2684 and then Present (First_Entity (Current_Scope))
2686 while Is_Generic_Formal (Freeze_From) loop
2687 Freeze_From := Next_Entity (Freeze_From);
2690 Freeze_All (Freeze_From, Decl);
2691 Freeze_From := Last_Entity (Current_Scope);
2694 -- For declarations in a subprogram body there is no issue
2695 -- with name resolution in aspect specifications, but in
2696 -- ASIS mode we need to preanalyze aspect specifications
2697 -- that may otherwise only be analyzed during expansion
2698 -- (e.g. during generation of a related subprogram).
2704 Freeze_All (First_Entity (Current_Scope), Decl);
2705 Freeze_From := Last_Entity (Current_Scope);
2708 -- Current scope is a package specification
2710 elsif Scope (Current_Scope) /= Standard_Standard
2711 and then not Is_Child_Unit (Current_Scope)
2712 and then No (Generic_Parent (Parent (L)))
2714 -- ARM rule 13.1.1(11/3): usage names in aspect definitions are
2715 -- resolved at the end of the immediately enclosing declaration
2716 -- list (AI05-0183-1).
2720 elsif L /= Visible_Declarations (Parent (L))
2721 or else No (Private_Declarations (Parent (L)))
2722 or else Is_Empty_List (Private_Declarations (Parent (L)))
2726 -- End of a package declaration
2728 -- In compilation mode the expansion of freeze node takes care
2729 -- of resolving expressions of all aspects in the list. In ASIS
2730 -- mode this must be done explicitly.
2733 and then Scope (Current_Scope) = Standard_Standard
2738 -- This is a freeze point because it is the end of a
2739 -- compilation unit.
2741 Freeze_All (First_Entity (Current_Scope), Decl);
2742 Freeze_From := Last_Entity (Current_Scope);
2744 -- At the end of the visible declarations the expressions in
2745 -- aspects of all entities declared so far must be resolved.
2746 -- The entities themselves might be frozen later, and the
2747 -- generated pragmas and attribute definition clauses analyzed
2748 -- in full at that point, but name resolution must take place
2750 -- In addition to being the proper semantics, this is mandatory
2751 -- within generic units, because global name capture requires
2752 -- those expressions to be analyzed, given that the generated
2753 -- pragmas do not appear in the original generic tree.
2755 elsif Serious_Errors_Detected = 0 then
2759 -- If next node is a body then freeze all types before the body.
2760 -- An exception occurs for some expander-generated bodies. If these
2761 -- are generated at places where in general language rules would not
2762 -- allow a freeze point, then we assume that the expander has
2763 -- explicitly checked that all required types are properly frozen,
2764 -- and we do not cause general freezing here. This special circuit
2765 -- is used when the encountered body is marked as having already
2768 -- In all other cases (bodies that come from source, and expander
2769 -- generated bodies that have not been analyzed yet), freeze all
2770 -- types now. Note that in the latter case, the expander must take
2771 -- care to attach the bodies at a proper place in the tree so as to
2772 -- not cause unwanted freezing at that point.
2774 -- It is also necessary to check for a case where both an expression
2775 -- function is used and the current scope depends on an incomplete
2776 -- private type from a library unit, otherwise premature freezing of
2777 -- the private type will occur.
2779 elsif not Analyzed (Next_Decl) and then Is_Body (Next_Decl)
2780 and then ((Nkind (Next_Decl) /= N_Subprogram_Body
2781 or else not Was_Expression_Function (Next_Decl))
2782 or else (not Is_Ignored_Ghost_Entity (Current_Scope)
2783 and then not Contains_Lib_Incomplete_Type
2786 -- When a controlled type is frozen, the expander generates stream
2787 -- and controlled-type support routines. If the freeze is caused
2788 -- by the stand-alone body of Initialize, Adjust, or Finalize, the
2789 -- expander will end up using the wrong version of these routines,
2790 -- as the body has not been processed yet. To remedy this, detect
2791 -- a late controlled primitive and create a proper spec for it.
2792 -- This ensures that the primitive will override its inherited
2793 -- counterpart before the freeze takes place.
2795 -- If the declaration we just processed is a body, do not attempt
2796 -- to examine Next_Decl as the late primitive idiom can only apply
2797 -- to the first encountered body.
2799 -- The spec of the late primitive is not generated in ASIS mode to
2800 -- ensure a consistent list of primitives that indicates the true
2801 -- semantic structure of the program (which is not relevant when
2802 -- generating executable code).
2804 -- ??? A cleaner approach may be possible and/or this solution
2805 -- could be extended to general-purpose late primitives, TBD.
2808 and then not Body_Seen
2809 and then not Is_Body (Decl)
2813 if Nkind (Next_Decl) = N_Subprogram_Body then
2814 Handle_Late_Controlled_Primitive (Next_Decl);
2818 -- In ASIS mode, if the next declaration is a body, complete
2819 -- the analysis of declarations so far.
2826 -- The generated body of an expression function does not freeze,
2827 -- unless it is a completion, in which case only the expression
2828 -- itself freezes. This is handled when the body itself is
2829 -- analyzed (see Freeze_Expr_Types, sem_ch6.adb).
2831 Freeze_All (Freeze_From, Decl);
2832 Freeze_From := Last_Entity (Current_Scope);
2838 -- Post-freezing actions
2841 Context := Parent (L);
2843 -- Certain contract annocations have forward visibility semantics and
2844 -- must be analyzed after all declarative items have been processed.
2845 -- This timing ensures that entities referenced by such contracts are
2848 -- Analyze the contract of an immediately enclosing package spec or
2849 -- body first because other contracts may depend on its information.
2851 if Nkind (Context) = N_Package_Body then
2852 Analyze_Package_Body_Contract (Defining_Entity (Context));
2854 elsif Nkind (Context) = N_Package_Specification then
2855 Analyze_Package_Contract (Defining_Entity (Context));
2858 -- Analyze the contracts of various constructs in the declarative
2861 Analyze_Contracts (L);
2863 if Nkind (Context) = N_Package_Body then
2865 -- Ensure that all abstract states and objects declared in the
2866 -- state space of a package body are utilized as constituents.
2868 Check_Unused_Body_States (Defining_Entity (Context));
2870 -- State refinements are visible up to the end of the package body
2871 -- declarations. Hide the state refinements from visibility to
2872 -- restore the original state conditions.
2874 Remove_Visible_Refinements (Corresponding_Spec (Context));
2875 Remove_Partial_Visible_Refinements (Corresponding_Spec (Context));
2877 elsif Nkind (Context) = N_Package_Specification then
2879 -- Partial state refinements are visible up to the end of the
2880 -- package spec declarations. Hide the partial state refinements
2881 -- from visibility to restore the original state conditions.
2883 Remove_Partial_Visible_Refinements (Defining_Entity (Context));
2886 -- Verify that all abstract states found in any package declared in
2887 -- the input declarative list have proper refinements. The check is
2888 -- performed only when the context denotes a block, entry, package,
2889 -- protected, subprogram, or task body (SPARK RM 7.2.2(3)).
2891 Check_State_Refinements (Context);
2893 -- Create the subprogram bodies which verify the run-time semantics
2894 -- of pragmas Default_Initial_Condition and [Type_]Invariant for all
2895 -- types within the current declarative list. This ensures that all
2896 -- assertion expressions are preanalyzed and resolved at the end of
2897 -- the declarative part. Note that the resolution happens even when
2898 -- freezing does not take place.
2900 Build_Assertion_Bodies (L, Context);
2902 end Analyze_Declarations;
2904 -----------------------------------
2905 -- Analyze_Full_Type_Declaration --
2906 -----------------------------------
2908 procedure Analyze_Full_Type_Declaration (N : Node_Id) is
2909 Def : constant Node_Id := Type_Definition (N);
2910 Def_Id : constant Entity_Id := Defining_Identifier (N);
2914 Is_Remote : constant Boolean :=
2915 (Is_Remote_Types (Current_Scope)
2916 or else Is_Remote_Call_Interface (Current_Scope))
2917 and then not (In_Private_Part (Current_Scope)
2918 or else In_Package_Body (Current_Scope));
2920 procedure Check_Nonoverridable_Aspects;
2921 -- Apply the rule in RM 13.1.1(18.4/4) on iterator aspects that cannot
2922 -- be overridden, and can only be confirmed on derivation.
2924 procedure Check_Ops_From_Incomplete_Type;
2925 -- If there is a tagged incomplete partial view of the type, traverse
2926 -- the primitives of the incomplete view and change the type of any
2927 -- controlling formals and result to indicate the full view. The
2928 -- primitives will be added to the full type's primitive operations
2929 -- list later in Sem_Disp.Check_Operation_From_Incomplete_Type (which
2930 -- is called from Process_Incomplete_Dependents).
2932 ----------------------------------
2933 -- Check_Nonoverridable_Aspects --
2934 ----------------------------------
2936 procedure Check_Nonoverridable_Aspects is
2937 function Get_Aspect_Spec
2939 Aspect_Name : Name_Id) return Node_Id;
2940 -- Check whether a list of aspect specifications includes an entry
2941 -- for a specific aspect. The list is either that of a partial or
2944 ---------------------
2945 -- Get_Aspect_Spec --
2946 ---------------------
2948 function Get_Aspect_Spec
2950 Aspect_Name : Name_Id) return Node_Id
2955 Spec := First (Specs);
2956 while Present (Spec) loop
2957 if Chars (Identifier (Spec)) = Aspect_Name then
2964 end Get_Aspect_Spec;
2968 Prev_Aspects : constant List_Id :=
2969 Aspect_Specifications (Parent (Def_Id));
2970 Par_Type : Entity_Id;
2971 Prev_Aspect : Node_Id;
2973 -- Start of processing for Check_Nonoverridable_Aspects
2976 -- Get parent type of derived type. Note that Prev is the entity in
2977 -- the partial declaration, but its contents are now those of full
2978 -- view, while Def_Id reflects the partial view.
2980 if Is_Private_Type (Def_Id) then
2981 Par_Type := Etype (Full_View (Def_Id));
2983 Par_Type := Etype (Def_Id);
2986 -- If there is an inherited Implicit_Dereference, verify that it is
2987 -- made explicit in the partial view.
2989 if Has_Discriminants (Base_Type (Par_Type))
2990 and then Nkind (Parent (Prev)) = N_Full_Type_Declaration
2991 and then Present (Discriminant_Specifications (Parent (Prev)))
2992 and then Present (Get_Reference_Discriminant (Par_Type))
2995 Get_Aspect_Spec (Prev_Aspects, Name_Implicit_Dereference);
2999 (Discriminant_Specifications
3000 (Original_Node (Parent (Prev))))
3003 ("type does not inherit implicit dereference", Prev);
3006 -- If one of the views has the aspect specified, verify that it
3007 -- is consistent with that of the parent.
3010 Cur_Discr : constant Entity_Id :=
3011 Get_Reference_Discriminant (Prev);
3012 Par_Discr : constant Entity_Id :=
3013 Get_Reference_Discriminant (Par_Type);
3016 if Corresponding_Discriminant (Cur_Discr) /= Par_Discr then
3018 ("aspect inconsistent with that of parent", N);
3021 -- Check that specification in partial view matches the
3022 -- inherited aspect. Compare names directly because aspect
3023 -- expression may not be analyzed.
3025 if Present (Prev_Aspect)
3026 and then Nkind (Expression (Prev_Aspect)) = N_Identifier
3027 and then Chars (Expression (Prev_Aspect)) /=
3031 ("aspect inconsistent with that of parent", N);
3037 -- TBD : other nonoverridable aspects.
3038 end Check_Nonoverridable_Aspects;
3040 ------------------------------------
3041 -- Check_Ops_From_Incomplete_Type --
3042 ------------------------------------
3044 procedure Check_Ops_From_Incomplete_Type is
3051 and then Ekind (Prev) = E_Incomplete_Type
3052 and then Is_Tagged_Type (Prev)
3053 and then Is_Tagged_Type (T)
3055 Elmt := First_Elmt (Primitive_Operations (Prev));
3056 while Present (Elmt) loop
3059 Formal := First_Formal (Op);
3060 while Present (Formal) loop
3061 if Etype (Formal) = Prev then
3062 Set_Etype (Formal, T);
3065 Next_Formal (Formal);
3068 if Etype (Op) = Prev then
3075 end Check_Ops_From_Incomplete_Type;
3077 -- Start of processing for Analyze_Full_Type_Declaration
3080 Prev := Find_Type_Name (N);
3082 -- The full view, if present, now points to the current type. If there
3083 -- is an incomplete partial view, set a link to it, to simplify the
3084 -- retrieval of primitive operations of the type.
3086 -- Ada 2005 (AI-50217): If the type was previously decorated when
3087 -- imported through a LIMITED WITH clause, it appears as incomplete
3088 -- but has no full view.
3090 if Ekind (Prev) = E_Incomplete_Type
3091 and then Present (Full_View (Prev))
3093 T := Full_View (Prev);
3094 Set_Incomplete_View (N, Parent (Prev));
3099 Set_Is_Pure (T, Is_Pure (Current_Scope));
3101 -- We set the flag Is_First_Subtype here. It is needed to set the
3102 -- corresponding flag for the Implicit class-wide-type created
3103 -- during tagged types processing.
3105 Set_Is_First_Subtype (T, True);
3107 -- Only composite types other than array types are allowed to have
3112 -- For derived types, the rule will be checked once we've figured
3113 -- out the parent type.
3115 when N_Derived_Type_Definition =>
3118 -- For record types, discriminants are allowed, unless we are in
3121 when N_Record_Definition =>
3122 if Present (Discriminant_Specifications (N)) then
3123 Check_SPARK_05_Restriction
3124 ("discriminant type is not allowed",
3126 (First (Discriminant_Specifications (N))));
3130 if Present (Discriminant_Specifications (N)) then
3132 ("elementary or array type cannot have discriminants",
3134 (First (Discriminant_Specifications (N))));
3138 -- Elaborate the type definition according to kind, and generate
3139 -- subsidiary (implicit) subtypes where needed. We skip this if it was
3140 -- already done (this happens during the reanalysis that follows a call
3141 -- to the high level optimizer).
3143 if not Analyzed (T) then
3146 -- Set the SPARK mode from the current context
3148 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3149 Set_SPARK_Pragma_Inherited (T);
3152 when N_Access_To_Subprogram_Definition =>
3153 Access_Subprogram_Declaration (T, Def);
3155 -- If this is a remote access to subprogram, we must create the
3156 -- equivalent fat pointer type, and related subprograms.
3159 Process_Remote_AST_Declaration (N);
3162 -- Validate categorization rule against access type declaration
3163 -- usually a violation in Pure unit, Shared_Passive unit.
3165 Validate_Access_Type_Declaration (T, N);
3167 when N_Access_To_Object_Definition =>
3168 Access_Type_Declaration (T, Def);
3170 -- Validate categorization rule against access type declaration
3171 -- usually a violation in Pure unit, Shared_Passive unit.
3173 Validate_Access_Type_Declaration (T, N);
3175 -- If we are in a Remote_Call_Interface package and define a
3176 -- RACW, then calling stubs and specific stream attributes
3180 and then Is_Remote_Access_To_Class_Wide_Type (Def_Id)
3182 Add_RACW_Features (Def_Id);
3185 when N_Array_Type_Definition =>
3186 Array_Type_Declaration (T, Def);
3188 when N_Derived_Type_Definition =>
3189 Derived_Type_Declaration (T, N, T /= Def_Id);
3191 -- Inherit predicates from parent, and protect against illegal
3194 if Is_Type (T) and then Has_Predicates (T) then
3195 Set_Has_Predicates (Def_Id);
3198 -- Save the scenario for examination by the ABE Processing
3201 Record_Elaboration_Scenario (N);
3203 when N_Enumeration_Type_Definition =>
3204 Enumeration_Type_Declaration (T, Def);
3206 when N_Floating_Point_Definition =>
3207 Floating_Point_Type_Declaration (T, Def);
3209 when N_Decimal_Fixed_Point_Definition =>
3210 Decimal_Fixed_Point_Type_Declaration (T, Def);
3212 when N_Ordinary_Fixed_Point_Definition =>
3213 Ordinary_Fixed_Point_Type_Declaration (T, Def);
3215 when N_Signed_Integer_Type_Definition =>
3216 Signed_Integer_Type_Declaration (T, Def);
3218 when N_Modular_Type_Definition =>
3219 Modular_Type_Declaration (T, Def);
3221 when N_Record_Definition =>
3222 Record_Type_Declaration (T, N, Prev);
3224 -- If declaration has a parse error, nothing to elaborate.
3230 raise Program_Error;
3234 if Etype (T) = Any_Type then
3238 -- Controlled type is not allowed in SPARK
3240 if Is_Visibly_Controlled (T) then
3241 Check_SPARK_05_Restriction ("controlled type is not allowed", N);
3244 -- Some common processing for all types
3246 Set_Depends_On_Private (T, Has_Private_Component (T));
3247 Check_Ops_From_Incomplete_Type;
3249 -- Both the declared entity, and its anonymous base type if one was
3250 -- created, need freeze nodes allocated.
3253 B : constant Entity_Id := Base_Type (T);
3256 -- In the case where the base type differs from the first subtype, we
3257 -- pre-allocate a freeze node, and set the proper link to the first
3258 -- subtype. Freeze_Entity will use this preallocated freeze node when
3259 -- it freezes the entity.
3261 -- This does not apply if the base type is a generic type, whose
3262 -- declaration is independent of the current derived definition.
3264 if B /= T and then not Is_Generic_Type (B) then
3265 Ensure_Freeze_Node (B);
3266 Set_First_Subtype_Link (Freeze_Node (B), T);
3269 -- A type that is imported through a limited_with clause cannot
3270 -- generate any code, and thus need not be frozen. However, an access
3271 -- type with an imported designated type needs a finalization list,
3272 -- which may be referenced in some other package that has non-limited
3273 -- visibility on the designated type. Thus we must create the
3274 -- finalization list at the point the access type is frozen, to
3275 -- prevent unsatisfied references at link time.
3277 if not From_Limited_With (T) or else Is_Access_Type (T) then
3278 Set_Has_Delayed_Freeze (T);
3282 -- Case where T is the full declaration of some private type which has
3283 -- been swapped in Defining_Identifier (N).
3285 if T /= Def_Id and then Is_Private_Type (Def_Id) then
3286 Process_Full_View (N, T, Def_Id);
3288 -- Record the reference. The form of this is a little strange, since
3289 -- the full declaration has been swapped in. So the first parameter
3290 -- here represents the entity to which a reference is made which is
3291 -- the "real" entity, i.e. the one swapped in, and the second
3292 -- parameter provides the reference location.
3294 -- Also, we want to kill Has_Pragma_Unreferenced temporarily here
3295 -- since we don't want a complaint about the full type being an
3296 -- unwanted reference to the private type
3299 B : constant Boolean := Has_Pragma_Unreferenced (T);
3301 Set_Has_Pragma_Unreferenced (T, False);
3302 Generate_Reference (T, T, 'c');
3303 Set_Has_Pragma_Unreferenced (T, B);
3306 Set_Completion_Referenced (Def_Id);
3308 -- For completion of incomplete type, process incomplete dependents
3309 -- and always mark the full type as referenced (it is the incomplete
3310 -- type that we get for any real reference).
3312 elsif Ekind (Prev) = E_Incomplete_Type then
3313 Process_Incomplete_Dependents (N, T, Prev);
3314 Generate_Reference (Prev, Def_Id, 'c');
3315 Set_Completion_Referenced (Def_Id);
3317 -- If not private type or incomplete type completion, this is a real
3318 -- definition of a new entity, so record it.
3321 Generate_Definition (Def_Id);
3324 -- Propagate any pending access types whose finalization masters need to
3325 -- be fully initialized from the partial to the full view. Guard against
3326 -- an illegal full view that remains unanalyzed.
3328 if Is_Type (Def_Id) and then Is_Incomplete_Or_Private_Type (Prev) then
3329 Set_Pending_Access_Types (Def_Id, Pending_Access_Types (Prev));
3332 if Chars (Scope (Def_Id)) = Name_System
3333 and then Chars (Def_Id) = Name_Address
3334 and then In_Predefined_Unit (N)
3336 Set_Is_Descendant_Of_Address (Def_Id);
3337 Set_Is_Descendant_Of_Address (Base_Type (Def_Id));
3338 Set_Is_Descendant_Of_Address (Prev);
3341 Set_Optimize_Alignment_Flags (Def_Id);
3342 Check_Eliminated (Def_Id);
3344 -- If the declaration is a completion and aspects are present, apply
3345 -- them to the entity for the type which is currently the partial
3346 -- view, but which is the one that will be frozen.
3348 if Has_Aspects (N) then
3350 -- In most cases the partial view is a private type, and both views
3351 -- appear in different declarative parts. In the unusual case where
3352 -- the partial view is incomplete, perform the analysis on the
3353 -- full view, to prevent freezing anomalies with the corresponding
3354 -- class-wide type, which otherwise might be frozen before the
3355 -- dispatch table is built.
3358 and then Ekind (Prev) /= E_Incomplete_Type
3360 Analyze_Aspect_Specifications (N, Prev);
3365 Analyze_Aspect_Specifications (N, Def_Id);
3369 if Is_Derived_Type (Prev)
3370 and then Def_Id /= Prev
3372 Check_Nonoverridable_Aspects;
3374 end Analyze_Full_Type_Declaration;
3376 ----------------------------------
3377 -- Analyze_Incomplete_Type_Decl --
3378 ----------------------------------
3380 procedure Analyze_Incomplete_Type_Decl (N : Node_Id) is
3381 F : constant Boolean := Is_Pure (Current_Scope);
3385 Check_SPARK_05_Restriction ("incomplete type is not allowed", N);
3387 Generate_Definition (Defining_Identifier (N));
3389 -- Process an incomplete declaration. The identifier must not have been
3390 -- declared already in the scope. However, an incomplete declaration may
3391 -- appear in the private part of a package, for a private type that has
3392 -- already been declared.
3394 -- In this case, the discriminants (if any) must match
3396 T := Find_Type_Name (N);
3398 Set_Ekind (T, E_Incomplete_Type);
3400 Set_Is_First_Subtype (T);
3401 Init_Size_Align (T);
3403 -- Set the SPARK mode from the current context
3405 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
3406 Set_SPARK_Pragma_Inherited (T);
3408 -- Ada 2005 (AI-326): Minimum decoration to give support to tagged
3409 -- incomplete types.
3411 if Tagged_Present (N) then
3412 Set_Is_Tagged_Type (T, True);
3413 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3414 Make_Class_Wide_Type (T);
3415 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3418 Set_Stored_Constraint (T, No_Elist);
3420 if Present (Discriminant_Specifications (N)) then
3422 Process_Discriminants (N);
3426 -- If the type has discriminants, nontrivial subtypes may be declared
3427 -- before the full view of the type. The full views of those subtypes
3428 -- will be built after the full view of the type.
3430 Set_Private_Dependents (T, New_Elmt_List);
3432 end Analyze_Incomplete_Type_Decl;
3434 -----------------------------------
3435 -- Analyze_Interface_Declaration --
3436 -----------------------------------
3438 procedure Analyze_Interface_Declaration (T : Entity_Id; Def : Node_Id) is
3439 CW : constant Entity_Id := Class_Wide_Type (T);
3442 Set_Is_Tagged_Type (T);
3443 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
3445 Set_Is_Limited_Record (T, Limited_Present (Def)
3446 or else Task_Present (Def)
3447 or else Protected_Present (Def)
3448 or else Synchronized_Present (Def));
3450 -- Type is abstract if full declaration carries keyword, or if previous
3451 -- partial view did.
3453 Set_Is_Abstract_Type (T);
3454 Set_Is_Interface (T);
3456 -- Type is a limited interface if it includes the keyword limited, task,
3457 -- protected, or synchronized.
3459 Set_Is_Limited_Interface
3460 (T, Limited_Present (Def)
3461 or else Protected_Present (Def)
3462 or else Synchronized_Present (Def)
3463 or else Task_Present (Def));
3465 Set_Interfaces (T, New_Elmt_List);
3466 Set_Direct_Primitive_Operations (T, New_Elmt_List);
3468 -- Complete the decoration of the class-wide entity if it was already
3469 -- built (i.e. during the creation of the limited view)
3471 if Present (CW) then
3472 Set_Is_Interface (CW);
3473 Set_Is_Limited_Interface (CW, Is_Limited_Interface (T));
3476 -- Check runtime support for synchronized interfaces
3478 if (Is_Task_Interface (T)
3479 or else Is_Protected_Interface (T)
3480 or else Is_Synchronized_Interface (T))
3481 and then not RTE_Available (RE_Select_Specific_Data)
3483 Error_Msg_CRT ("synchronized interfaces", T);
3485 end Analyze_Interface_Declaration;
3487 -----------------------------
3488 -- Analyze_Itype_Reference --
3489 -----------------------------
3491 -- Nothing to do. This node is placed in the tree only for the benefit of
3492 -- back end processing, and has no effect on the semantic processing.
3494 procedure Analyze_Itype_Reference (N : Node_Id) is
3496 pragma Assert (Is_Itype (Itype (N)));
3498 end Analyze_Itype_Reference;
3500 --------------------------------
3501 -- Analyze_Number_Declaration --
3502 --------------------------------
3504 procedure Analyze_Number_Declaration (N : Node_Id) is
3505 E : constant Node_Id := Expression (N);
3506 Id : constant Entity_Id := Defining_Identifier (N);
3507 Index : Interp_Index;
3512 Generate_Definition (Id);
3515 -- This is an optimization of a common case of an integer literal
3517 if Nkind (E) = N_Integer_Literal then
3518 Set_Is_Static_Expression (E, True);
3519 Set_Etype (E, Universal_Integer);
3521 Set_Etype (Id, Universal_Integer);
3522 Set_Ekind (Id, E_Named_Integer);
3523 Set_Is_Frozen (Id, True);
3527 Set_Is_Pure (Id, Is_Pure (Current_Scope));
3529 -- Process expression, replacing error by integer zero, to avoid
3530 -- cascaded errors or aborts further along in the processing
3532 -- Replace Error by integer zero, which seems least likely to cause
3536 Rewrite (E, Make_Integer_Literal (Sloc (E), Uint_0));
3537 Set_Error_Posted (E);
3542 -- Verify that the expression is static and numeric. If
3543 -- the expression is overloaded, we apply the preference
3544 -- rule that favors root numeric types.
3546 if not Is_Overloaded (E) then
3548 if Has_Dynamic_Predicate_Aspect (T) then
3550 ("subtype has dynamic predicate, "
3551 & "not allowed in number declaration", N);
3557 Get_First_Interp (E, Index, It);
3558 while Present (It.Typ) loop
3559 if (Is_Integer_Type (It.Typ) or else Is_Real_Type (It.Typ))
3560 and then (Scope (Base_Type (It.Typ))) = Standard_Standard
3562 if T = Any_Type then
3565 elsif It.Typ = Universal_Real
3567 It.Typ = Universal_Integer
3569 -- Choose universal interpretation over any other
3576 Get_Next_Interp (Index, It);
3580 if Is_Integer_Type (T) then
3582 Set_Etype (Id, Universal_Integer);
3583 Set_Ekind (Id, E_Named_Integer);
3585 elsif Is_Real_Type (T) then
3587 -- Because the real value is converted to universal_real, this is a
3588 -- legal context for a universal fixed expression.
3590 if T = Universal_Fixed then
3592 Loc : constant Source_Ptr := Sloc (N);
3593 Conv : constant Node_Id := Make_Type_Conversion (Loc,
3595 New_Occurrence_Of (Universal_Real, Loc),
3596 Expression => Relocate_Node (E));
3603 elsif T = Any_Fixed then
3604 Error_Msg_N ("illegal context for mixed mode operation", E);
3606 -- Expression is of the form : universal_fixed * integer. Try to
3607 -- resolve as universal_real.
3609 T := Universal_Real;
3614 Set_Etype (Id, Universal_Real);
3615 Set_Ekind (Id, E_Named_Real);
3618 Wrong_Type (E, Any_Numeric);
3622 Set_Ekind (Id, E_Constant);
3623 Set_Never_Set_In_Source (Id, True);
3624 Set_Is_True_Constant (Id, True);
3628 if Nkind_In (E, N_Integer_Literal, N_Real_Literal) then
3629 Set_Etype (E, Etype (Id));
3632 if not Is_OK_Static_Expression (E) then
3633 Flag_Non_Static_Expr
3634 ("non-static expression used in number declaration!", E);
3635 Rewrite (E, Make_Integer_Literal (Sloc (N), 1));
3636 Set_Etype (E, Any_Type);
3639 Analyze_Dimension (N);
3640 end Analyze_Number_Declaration;
3642 --------------------------------
3643 -- Analyze_Object_Declaration --
3644 --------------------------------
3646 -- WARNING: This routine manages Ghost regions. Return statements must be
3647 -- replaced by gotos which jump to the end of the routine and restore the
3650 procedure Analyze_Object_Declaration (N : Node_Id) is
3651 Loc : constant Source_Ptr := Sloc (N);
3652 Id : constant Entity_Id := Defining_Identifier (N);
3653 Next_Decl : constant Node_Id := Next (N);
3658 E : Node_Id := Expression (N);
3659 -- E is set to Expression (N) throughout this routine. When Expression
3660 -- (N) is modified, E is changed accordingly.
3662 Prev_Entity : Entity_Id := Empty;
3664 procedure Check_Dynamic_Object (Typ : Entity_Id);
3665 -- A library-level object with nonstatic discriminant constraints may
3666 -- require dynamic allocation. The declaration is illegal if the
3667 -- profile includes the restriction No_Implicit_Heap_Allocations.
3669 procedure Check_For_Null_Excluding_Components
3670 (Obj_Typ : Entity_Id;
3671 Obj_Decl : Node_Id);
3672 -- Verify that each null-excluding component of object declaration
3673 -- Obj_Decl carrying type Obj_Typ has explicit initialization. Emit
3674 -- a compile-time warning if this is not the case.
3676 function Count_Tasks (T : Entity_Id) return Uint;
3677 -- This function is called when a non-generic library level object of a
3678 -- task type is declared. Its function is to count the static number of
3679 -- tasks declared within the type (it is only called if Has_Task is set
3680 -- for T). As a side effect, if an array of tasks with nonstatic bounds
3681 -- or a variant record type is encountered, Check_Restriction is called
3682 -- indicating the count is unknown.
3684 function Delayed_Aspect_Present return Boolean;
3685 -- If the declaration has an expression that is an aggregate, and it
3686 -- has aspects that require delayed analysis, the resolution of the
3687 -- aggregate must be deferred to the freeze point of the object. This
3688 -- special processing was created for address clauses, but it must
3689 -- also apply to Alignment. This must be done before the aspect
3690 -- specifications are analyzed because we must handle the aggregate
3691 -- before the analysis of the object declaration is complete.
3693 -- Any other relevant delayed aspects on object declarations ???
3695 --------------------------
3696 -- Check_Dynamic_Object --
3697 --------------------------
3699 procedure Check_Dynamic_Object (Typ : Entity_Id) is
3701 Obj_Type : Entity_Id;
3706 if Is_Private_Type (Obj_Type)
3707 and then Present (Full_View (Obj_Type))
3709 Obj_Type := Full_View (Obj_Type);
3712 if Known_Static_Esize (Obj_Type) then
3716 if Restriction_Active (No_Implicit_Heap_Allocations)
3717 and then Expander_Active
3718 and then Has_Discriminants (Obj_Type)
3720 Comp := First_Component (Obj_Type);
3721 while Present (Comp) loop
3722 if Known_Static_Esize (Etype (Comp))
3723 or else Size_Known_At_Compile_Time (Etype (Comp))
3727 elsif not Discriminated_Size (Comp)
3728 and then Comes_From_Source (Comp)
3731 ("component& of non-static size will violate restriction "
3732 & "No_Implicit_Heap_Allocation?", N, Comp);
3734 elsif Is_Record_Type (Etype (Comp)) then
3735 Check_Dynamic_Object (Etype (Comp));
3738 Next_Component (Comp);
3741 end Check_Dynamic_Object;
3743 -----------------------------------------
3744 -- Check_For_Null_Excluding_Components --
3745 -----------------------------------------
3747 procedure Check_For_Null_Excluding_Components
3748 (Obj_Typ : Entity_Id;
3751 procedure Check_Component
3752 (Comp_Typ : Entity_Id;
3753 Comp_Decl : Node_Id := Empty;
3754 Array_Comp : Boolean := False);
3755 -- Apply a compile-time null-exclusion check on a component denoted
3756 -- by its declaration Comp_Decl and type Comp_Typ, and all of its
3757 -- subcomponents (if any).
3759 ---------------------
3760 -- Check_Component --
3761 ---------------------
3763 procedure Check_Component
3764 (Comp_Typ : Entity_Id;
3765 Comp_Decl : Node_Id := Empty;
3766 Array_Comp : Boolean := False)
3772 -- Do not consider internally-generated components or those that
3773 -- are already initialized.
3775 if Present (Comp_Decl)
3776 and then (not Comes_From_Source (Comp_Decl)
3777 or else Present (Expression (Comp_Decl)))
3782 if Is_Incomplete_Or_Private_Type (Comp_Typ)
3783 and then Present (Full_View (Comp_Typ))
3785 T := Full_View (Comp_Typ);
3790 -- Verify a component of a null-excluding access type
3792 if Is_Access_Type (T)
3793 and then Can_Never_Be_Null (T)
3795 if Comp_Decl = Obj_Decl then
3796 Null_Exclusion_Static_Checks
3799 Array_Comp => Array_Comp);
3802 Null_Exclusion_Static_Checks
3805 Array_Comp => Array_Comp);
3808 -- Check array components
3810 elsif Is_Array_Type (T) then
3812 -- There is no suitable component when the object is of an
3813 -- array type. However, a namable component may appear at some
3814 -- point during the recursive inspection, but not at the top
3815 -- level. At the top level just indicate array component case.
3817 if Comp_Decl = Obj_Decl then
3818 Check_Component (Component_Type (T), Array_Comp => True);
3820 Check_Component (Component_Type (T), Comp_Decl);
3823 -- Verify all components of type T
3825 -- Note: No checks are performed on types with discriminants due
3826 -- to complexities involving variants. ???
3828 elsif (Is_Concurrent_Type (T)
3829 or else Is_Incomplete_Or_Private_Type (T)
3830 or else Is_Record_Type (T))
3831 and then not Has_Discriminants (T)
3833 Comp := First_Component (T);
3834 while Present (Comp) loop
3835 Check_Component (Etype (Comp), Parent (Comp));
3837 Comp := Next_Component (Comp);
3840 end Check_Component;
3842 -- Start processing for Check_For_Null_Excluding_Components
3845 Check_Component (Obj_Typ, Obj_Decl);
3846 end Check_For_Null_Excluding_Components;
3852 function Count_Tasks (T : Entity_Id) return Uint is
3858 if Is_Task_Type (T) then
3861 elsif Is_Record_Type (T) then
3862 if Has_Discriminants (T) then
3863 Check_Restriction (Max_Tasks, N);
3868 C := First_Component (T);
3869 while Present (C) loop
3870 V := V + Count_Tasks (Etype (C));
3877 elsif Is_Array_Type (T) then
3878 X := First_Index (T);
3879 V := Count_Tasks (Component_Type (T));
3880 while Present (X) loop
3883 if not Is_OK_Static_Subtype (C) then
3884 Check_Restriction (Max_Tasks, N);
3887 V := V * (UI_Max (Uint_0,
3888 Expr_Value (Type_High_Bound (C)) -
3889 Expr_Value (Type_Low_Bound (C)) + Uint_1));
3902 ----------------------------
3903 -- Delayed_Aspect_Present --
3904 ----------------------------
3906 function Delayed_Aspect_Present return Boolean is
3911 if Present (Aspect_Specifications (N)) then
3912 A := First (Aspect_Specifications (N));
3913 A_Id := Get_Aspect_Id (Chars (Identifier (A)));
3914 while Present (A) loop
3915 if A_Id = Aspect_Alignment or else A_Id = Aspect_Address then
3917 -- Set flag on object entity, for later processing at
3918 -- the freeze point.
3920 Set_Has_Delayed_Aspects (Id);
3929 end Delayed_Aspect_Present;
3933 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
3934 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
3935 -- Save the Ghost-related attributes to restore on exit
3937 Related_Id : Entity_Id;
3939 -- Start of processing for Analyze_Object_Declaration
3942 -- There are three kinds of implicit types generated by an
3943 -- object declaration:
3945 -- 1. Those generated by the original Object Definition
3947 -- 2. Those generated by the Expression
3949 -- 3. Those used to constrain the Object Definition with the
3950 -- expression constraints when the definition is unconstrained.
3952 -- They must be generated in this order to avoid order of elaboration
3953 -- issues. Thus the first step (after entering the name) is to analyze
3954 -- the object definition.
3956 if Constant_Present (N) then
3957 Prev_Entity := Current_Entity_In_Scope (Id);
3959 if Present (Prev_Entity)
3961 -- If the homograph is an implicit subprogram, it is overridden
3962 -- by the current declaration.
3964 ((Is_Overloadable (Prev_Entity)
3965 and then Is_Inherited_Operation (Prev_Entity))
3967 -- The current object is a discriminal generated for an entry
3968 -- family index. Even though the index is a constant, in this
3969 -- particular context there is no true constant redeclaration.
3970 -- Enter_Name will handle the visibility.
3973 (Is_Discriminal (Id)
3974 and then Ekind (Discriminal_Link (Id)) =
3975 E_Entry_Index_Parameter)
3977 -- The current object is the renaming for a generic declared
3978 -- within the instance.
3981 (Ekind (Prev_Entity) = E_Package
3982 and then Nkind (Parent (Prev_Entity)) =
3983 N_Package_Renaming_Declaration
3984 and then not Comes_From_Source (Prev_Entity)
3986 Is_Generic_Instance (Renamed_Entity (Prev_Entity)))
3988 -- The entity may be a homonym of a private component of the
3989 -- enclosing protected object, for which we create a local
3990 -- renaming declaration. The declaration is legal, even if
3991 -- useless when it just captures that component.
3994 (Ekind (Scope (Current_Scope)) = E_Protected_Type
3995 and then Nkind (Parent (Prev_Entity)) =
3996 N_Object_Renaming_Declaration))
3998 Prev_Entity := Empty;
4002 if Present (Prev_Entity) then
4004 -- The object declaration is Ghost when it completes a deferred Ghost
4007 Mark_And_Set_Ghost_Completion (N, Prev_Entity);
4009 Constant_Redeclaration (Id, N, T);
4011 Generate_Reference (Prev_Entity, Id, 'c');
4012 Set_Completion_Referenced (Id);
4014 if Error_Posted (N) then
4016 -- Type mismatch or illegal redeclaration; do not analyze
4017 -- expression to avoid cascaded errors.
4019 T := Find_Type_Of_Object (Object_Definition (N), N);
4021 Set_Ekind (Id, E_Variable);
4025 -- In the normal case, enter identifier at the start to catch premature
4026 -- usage in the initialization expression.
4029 Generate_Definition (Id);
4032 Mark_Coextensions (N, Object_Definition (N));
4034 T := Find_Type_Of_Object (Object_Definition (N), N);
4036 if Nkind (Object_Definition (N)) = N_Access_Definition
4038 (Access_To_Subprogram_Definition (Object_Definition (N)))
4039 and then Protected_Present
4040 (Access_To_Subprogram_Definition (Object_Definition (N)))
4042 T := Replace_Anonymous_Access_To_Protected_Subprogram (N);
4045 if Error_Posted (Id) then
4047 Set_Ekind (Id, E_Variable);
4052 -- Ada 2005 (AI-231): Propagate the null-excluding attribute and carry
4053 -- out some static checks.
4055 if Ada_Version >= Ada_2005 then
4057 -- In case of aggregates we must also take care of the correct
4058 -- initialization of nested aggregates bug this is done at the
4059 -- point of the analysis of the aggregate (see sem_aggr.adb) ???
4061 if Can_Never_Be_Null (T) then
4062 if Present (Expression (N))
4063 and then Nkind (Expression (N)) = N_Aggregate
4069 Save_Typ : constant Entity_Id := Etype (Id);
4071 Set_Etype (Id, T); -- Temp. decoration for static checks
4072 Null_Exclusion_Static_Checks (N);
4073 Set_Etype (Id, Save_Typ);
4077 -- We might be dealing with an object of a composite type containing
4078 -- null-excluding components without an aggregate, so we must verify
4079 -- that such components have default initialization.
4082 Check_For_Null_Excluding_Components (T, N);
4086 -- Object is marked pure if it is in a pure scope
4088 Set_Is_Pure (Id, Is_Pure (Current_Scope));
4090 -- If deferred constant, make sure context is appropriate. We detect
4091 -- a deferred constant as a constant declaration with no expression.
4092 -- A deferred constant can appear in a package body if its completion
4093 -- is by means of an interface pragma.
4095 if Constant_Present (N) and then No (E) then
4097 -- A deferred constant may appear in the declarative part of the
4098 -- following constructs:
4102 -- extended return statements
4105 -- subprogram bodies
4108 -- When declared inside a package spec, a deferred constant must be
4109 -- completed by a full constant declaration or pragma Import. In all
4110 -- other cases, the only proper completion is pragma Import. Extended
4111 -- return statements are flagged as invalid contexts because they do
4112 -- not have a declarative part and so cannot accommodate the pragma.
4114 if Ekind (Current_Scope) = E_Return_Statement then
4116 ("invalid context for deferred constant declaration (RM 7.4)",
4119 ("\declaration requires an initialization expression",
4121 Set_Constant_Present (N, False);
4123 -- In Ada 83, deferred constant must be of private type
4125 elsif not Is_Private_Type (T) then
4126 if Ada_Version = Ada_83 and then Comes_From_Source (N) then
4128 ("(Ada 83) deferred constant must be private type", N);
4132 -- If not a deferred constant, then the object declaration freezes
4133 -- its type, unless the object is of an anonymous type and has delayed
4134 -- aspects. In that case the type is frozen when the object itself is.
4137 Check_Fully_Declared (T, N);
4139 if Has_Delayed_Aspects (Id)
4140 and then Is_Array_Type (T)
4141 and then Is_Itype (T)
4143 Set_Has_Delayed_Freeze (T);
4145 Freeze_Before (N, T);
4149 -- If the object was created by a constrained array definition, then
4150 -- set the link in both the anonymous base type and anonymous subtype
4151 -- that are built to represent the array type to point to the object.
4153 if Nkind (Object_Definition (Declaration_Node (Id))) =
4154 N_Constrained_Array_Definition
4156 Set_Related_Array_Object (T, Id);
4157 Set_Related_Array_Object (Base_Type (T), Id);
4160 -- Special checks for protected objects not at library level
4162 if Has_Protected (T) and then not Is_Library_Level_Entity (Id) then
4163 Check_Restriction (No_Local_Protected_Objects, Id);
4165 -- Protected objects with interrupt handlers must be at library level
4167 -- Ada 2005: This test is not needed (and the corresponding clause
4168 -- in the RM is removed) because accessibility checks are sufficient
4169 -- to make handlers not at the library level illegal.
4171 -- AI05-0303: The AI is in fact a binding interpretation, and thus
4172 -- applies to the '95 version of the language as well.
4174 if Is_Protected_Type (T)
4175 and then Has_Interrupt_Handler (T)
4176 and then Ada_Version < Ada_95
4179 ("interrupt object can only be declared at library level", Id);
4183 -- Check for violation of No_Local_Timing_Events
4185 if Has_Timing_Event (T) and then not Is_Library_Level_Entity (Id) then
4186 Check_Restriction (No_Local_Timing_Events, Id);
4189 -- The actual subtype of the object is the nominal subtype, unless
4190 -- the nominal one is unconstrained and obtained from the expression.
4194 -- These checks should be performed before the initialization expression
4195 -- is considered, so that the Object_Definition node is still the same
4196 -- as in source code.
4198 -- In SPARK, the nominal subtype is always given by a subtype mark
4199 -- and must not be unconstrained. (The only exception to this is the
4200 -- acceptance of declarations of constants of type String.)
4202 if not Nkind_In (Object_Definition (N), N_Expanded_Name, N_Identifier)
4204 Check_SPARK_05_Restriction
4205 ("subtype mark required", Object_Definition (N));
4207 elsif Is_Array_Type (T)
4208 and then not Is_Constrained (T)
4209 and then T /= Standard_String
4211 Check_SPARK_05_Restriction
4212 ("subtype mark of constrained type expected",
4213 Object_Definition (N));
4216 if Is_Library_Level_Entity (Id) then
4217 Check_Dynamic_Object (T);
4220 -- There are no aliased objects in SPARK
4222 if Aliased_Present (N) then
4223 Check_SPARK_05_Restriction ("aliased object is not allowed", N);
4226 -- Process initialization expression if present and not in error
4228 if Present (E) and then E /= Error then
4230 -- Generate an error in case of CPP class-wide object initialization.
4231 -- Required because otherwise the expansion of the class-wide
4232 -- assignment would try to use 'size to initialize the object
4233 -- (primitive that is not available in CPP tagged types).
4235 if Is_Class_Wide_Type (Act_T)
4237 (Is_CPP_Class (Root_Type (Etype (Act_T)))
4239 (Present (Full_View (Root_Type (Etype (Act_T))))
4241 Is_CPP_Class (Full_View (Root_Type (Etype (Act_T))))))
4244 ("predefined assignment not available for 'C'P'P tagged types",
4248 Mark_Coextensions (N, E);
4251 -- In case of errors detected in the analysis of the expression,
4252 -- decorate it with the expected type to avoid cascaded errors
4254 if No (Etype (E)) then
4258 -- If an initialization expression is present, then we set the
4259 -- Is_True_Constant flag. It will be reset if this is a variable
4260 -- and it is indeed modified.
4262 Set_Is_True_Constant (Id, True);
4264 -- If we are analyzing a constant declaration, set its completion
4265 -- flag after analyzing and resolving the expression.
4267 if Constant_Present (N) then
4268 Set_Has_Completion (Id);
4271 -- Set type and resolve (type may be overridden later on). Note:
4272 -- Ekind (Id) must still be E_Void at this point so that incorrect
4273 -- early usage within E is properly diagnosed.
4277 -- If the expression is an aggregate we must look ahead to detect
4278 -- the possible presence of an address clause, and defer resolution
4279 -- and expansion of the aggregate to the freeze point of the entity.
4281 -- This is not always legal because the aggregate may contain other
4282 -- references that need freezing, e.g. references to other entities
4283 -- with address clauses. In any case, when compiling with -gnatI the
4284 -- presence of the address clause must be ignored.
4286 if Comes_From_Source (N)
4287 and then Expander_Active
4288 and then Nkind (E) = N_Aggregate
4290 ((Present (Following_Address_Clause (N))
4291 and then not Ignore_Rep_Clauses)
4292 or else Delayed_Aspect_Present)
4296 -- If the aggregate is limited it will be built in place, and its
4297 -- expansion is deferred until the object declaration is expanded.
4299 if Is_Limited_Type (T) then
4300 Set_Expansion_Delayed (E);
4304 -- If the expression is a formal that is a "subprogram pointer"
4305 -- this is illegal in accessibility terms (see RM 3.10.2 (13.1/2)
4306 -- and AARM 3.10.2 (13.b/2)). Add an explicit conversion to force
4307 -- the corresponding check, as is done for assignments.
4309 if Is_Entity_Name (E)
4310 and then Present (Entity (E))
4311 and then Is_Formal (Entity (E))
4313 Ekind (Etype (Entity (E))) = E_Anonymous_Access_Subprogram_Type
4314 and then Ekind (T) /= E_Anonymous_Access_Subprogram_Type
4316 Rewrite (E, Convert_To (T, Relocate_Node (E)));
4322 -- No further action needed if E is a call to an inlined function
4323 -- which returns an unconstrained type and it has been expanded into
4324 -- a procedure call. In that case N has been replaced by an object
4325 -- declaration without initializing expression and it has been
4326 -- analyzed (see Expand_Inlined_Call).
4328 if Back_End_Inlining
4329 and then Expander_Active
4330 and then Nkind (E) = N_Function_Call
4331 and then Nkind (Name (E)) in N_Has_Entity
4332 and then Is_Inlined (Entity (Name (E)))
4333 and then not Is_Constrained (Etype (E))
4334 and then Analyzed (N)
4335 and then No (Expression (N))
4340 -- If E is null and has been replaced by an N_Raise_Constraint_Error
4341 -- node (which was marked already-analyzed), we need to set the type
4342 -- to something other than Any_Access in order to keep gigi happy.
4344 if Etype (E) = Any_Access then
4348 -- If the object is an access to variable, the initialization
4349 -- expression cannot be an access to constant.
4351 if Is_Access_Type (T)
4352 and then not Is_Access_Constant (T)
4353 and then Is_Access_Type (Etype (E))
4354 and then Is_Access_Constant (Etype (E))
4357 ("access to variable cannot be initialized with an "
4358 & "access-to-constant expression", E);
4361 if not Assignment_OK (N) then
4362 Check_Initialization (T, E);
4365 Check_Unset_Reference (E);
4367 -- If this is a variable, then set current value. If this is a
4368 -- declared constant of a scalar type with a static expression,
4369 -- indicate that it is always valid.
4371 if not Constant_Present (N) then
4372 if Compile_Time_Known_Value (E) then
4373 Set_Current_Value (Id, E);
4376 elsif Is_Scalar_Type (T) and then Is_OK_Static_Expression (E) then
4377 Set_Is_Known_Valid (Id);
4379 -- If it is a constant initialized with a valid nonstatic entity,
4380 -- the constant is known valid as well, and can inherit the subtype
4381 -- of the entity if it is a subtype of the given type. This info
4382 -- is preserved on the actual subtype of the constant.
4384 elsif Is_Scalar_Type (T)
4385 and then Is_Entity_Name (E)
4386 and then Is_Known_Valid (Entity (E))
4387 and then In_Subrange_Of (Etype (Entity (E)), T)
4389 Set_Is_Known_Valid (Id);
4390 Set_Ekind (Id, E_Constant);
4391 Set_Actual_Subtype (Id, Etype (Entity (E)));
4394 -- Deal with setting of null flags
4396 if Is_Access_Type (T) then
4397 if Known_Non_Null (E) then
4398 Set_Is_Known_Non_Null (Id, True);
4399 elsif Known_Null (E) and then not Can_Never_Be_Null (Id) then
4400 Set_Is_Known_Null (Id, True);
4404 -- Check incorrect use of dynamically tagged expressions
4406 if Is_Tagged_Type (T) then
4407 Check_Dynamically_Tagged_Expression
4413 Apply_Scalar_Range_Check (E, T);
4414 Apply_Static_Length_Check (E, T);
4416 if Nkind (Original_Node (N)) = N_Object_Declaration
4417 and then Comes_From_Source (Original_Node (N))
4419 -- Only call test if needed
4421 and then Restriction_Check_Required (SPARK_05)
4422 and then not Is_SPARK_05_Initialization_Expr (Original_Node (E))
4424 Check_SPARK_05_Restriction
4425 ("initialization expression is not appropriate", E);
4428 -- A formal parameter of a specific tagged type whose related
4429 -- subprogram is subject to pragma Extensions_Visible with value
4430 -- "False" cannot be implicitly converted to a class-wide type by
4431 -- means of an initialization expression (SPARK RM 6.1.7(3)). Do
4432 -- not consider internally generated expressions.
4434 if Is_Class_Wide_Type (T)
4435 and then Comes_From_Source (E)
4436 and then Is_EVF_Expression (E)
4439 ("formal parameter cannot be implicitly converted to "
4440 & "class-wide type when Extensions_Visible is False", E);
4444 -- If the No_Streams restriction is set, check that the type of the
4445 -- object is not, and does not contain, any subtype derived from
4446 -- Ada.Streams.Root_Stream_Type. Note that we guard the call to
4447 -- Has_Stream just for efficiency reasons. There is no point in
4448 -- spending time on a Has_Stream check if the restriction is not set.
4450 if Restriction_Check_Required (No_Streams) then
4451 if Has_Stream (T) then
4452 Check_Restriction (No_Streams, N);
4456 -- Deal with predicate check before we start to do major rewriting. It
4457 -- is OK to initialize and then check the initialized value, since the
4458 -- object goes out of scope if we get a predicate failure. Note that we
4459 -- do this in the analyzer and not the expander because the analyzer
4460 -- does some substantial rewriting in some cases.
4462 -- We need a predicate check if the type has predicates that are not
4463 -- ignored, and if either there is an initializing expression, or for
4464 -- default initialization when we have at least one case of an explicit
4465 -- default initial value and then this is not an internal declaration
4466 -- whose initialization comes later (as for an aggregate expansion).
4467 -- If expression is an aggregate it may be expanded into assignments
4468 -- and the declaration itself is marked with No_Initialization, but
4469 -- the predicate still applies.
4471 if not Suppress_Assignment_Checks (N)
4472 and then Present (Predicate_Function (T))
4473 and then not Predicates_Ignored (T)
4475 (not No_Initialization (N)
4476 or else (Present (E) and then Nkind (E) = N_Aggregate))
4480 Is_Partially_Initialized_Type (T, Include_Implicit => False))
4482 -- If the type has a static predicate and the expression is known at
4483 -- compile time, see if the expression satisfies the predicate.
4486 Check_Expression_Against_Static_Predicate (E, T);
4489 -- If the type is a null record and there is no explicit initial
4490 -- expression, no predicate check applies.
4492 if No (E) and then Is_Null_Record_Type (T) then
4495 -- Do not generate a predicate check if the initialization expression
4496 -- is a type conversion because the conversion has been subjected to
4497 -- the same check. This is a small optimization which avoid redundant
4500 elsif Present (E) and then Nkind (E) = N_Type_Conversion then
4504 -- The check must be inserted after the expanded aggregate
4505 -- expansion code, if any.
4508 Check : constant Node_Id :=
4509 Make_Predicate_Check (T, New_Occurrence_Of (Id, Loc));
4512 if No (Next_Decl) then
4513 Append_To (List_Containing (N), Check);
4515 Insert_Before (Next_Decl, Check);
4521 -- Case of unconstrained type
4523 if not Is_Definite_Subtype (T) then
4525 -- In SPARK, a declaration of unconstrained type is allowed
4526 -- only for constants of type string.
4528 if Is_String_Type (T) and then not Constant_Present (N) then
4529 Check_SPARK_05_Restriction
4530 ("declaration of object of unconstrained type not allowed", N);
4533 -- Nothing to do in deferred constant case
4535 if Constant_Present (N) and then No (E) then
4538 -- Case of no initialization present
4541 if No_Initialization (N) then
4544 elsif Is_Class_Wide_Type (T) then
4546 ("initialization required in class-wide declaration ", N);
4550 ("unconstrained subtype not allowed (need initialization)",
4551 Object_Definition (N));
4553 if Is_Record_Type (T) and then Has_Discriminants (T) then
4555 ("\provide initial value or explicit discriminant values",
4556 Object_Definition (N));
4559 ("\or give default discriminant values for type&",
4560 Object_Definition (N), T);
4562 elsif Is_Array_Type (T) then
4564 ("\provide initial value or explicit array bounds",
4565 Object_Definition (N));
4569 -- Case of initialization present but in error. Set initial
4570 -- expression as absent (but do not make above complaints)
4572 elsif E = Error then
4573 Set_Expression (N, Empty);
4576 -- Case of initialization present
4579 -- Check restrictions in Ada 83
4581 if not Constant_Present (N) then
4583 -- Unconstrained variables not allowed in Ada 83 mode
4585 if Ada_Version = Ada_83
4586 and then Comes_From_Source (Object_Definition (N))
4589 ("(Ada 83) unconstrained variable not allowed",
4590 Object_Definition (N));
4594 -- Now we constrain the variable from the initializing expression
4596 -- If the expression is an aggregate, it has been expanded into
4597 -- individual assignments. Retrieve the actual type from the
4598 -- expanded construct.
4600 if Is_Array_Type (T)
4601 and then No_Initialization (N)
4602 and then Nkind (Original_Node (E)) = N_Aggregate
4606 -- In case of class-wide interface object declarations we delay
4607 -- the generation of the equivalent record type declarations until
4608 -- its expansion because there are cases in they are not required.
4610 elsif Is_Interface (T) then
4613 -- If the type is an unchecked union, no subtype can be built from
4614 -- the expression. Rewrite declaration as a renaming, which the
4615 -- back-end can handle properly. This is a rather unusual case,
4616 -- because most unchecked_union declarations have default values
4617 -- for discriminants and are thus not indefinite.
4619 elsif Is_Unchecked_Union (T) then
4620 if Constant_Present (N) or else Nkind (E) = N_Function_Call then
4621 Set_Ekind (Id, E_Constant);
4623 Set_Ekind (Id, E_Variable);
4627 Make_Object_Renaming_Declaration (Loc,
4628 Defining_Identifier => Id,
4629 Subtype_Mark => New_Occurrence_Of (T, Loc),
4632 Set_Renamed_Object (Id, E);
4633 Freeze_Before (N, T);
4638 -- Ensure that the generated subtype has a unique external name
4639 -- when the related object is public. This guarantees that the
4640 -- subtype and its bounds will not be affected by switches or
4641 -- pragmas that may offset the internal counter due to extra
4644 if Is_Public (Id) then
4647 Related_Id := Empty;
4650 Expand_Subtype_From_Expr
4653 Subtype_Indic => Object_Definition (N),
4655 Related_Id => Related_Id);
4657 Act_T := Find_Type_Of_Object (Object_Definition (N), N);
4660 Set_Is_Constr_Subt_For_U_Nominal (Act_T);
4662 if Aliased_Present (N) then
4663 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4666 Freeze_Before (N, Act_T);
4667 Freeze_Before (N, T);
4670 elsif Is_Array_Type (T)
4671 and then No_Initialization (N)
4672 and then (Nkind (Original_Node (E)) = N_Aggregate
4673 or else (Nkind (Original_Node (E)) = N_Qualified_Expression
4674 and then Nkind (Original_Node (Expression
4675 (Original_Node (E)))) = N_Aggregate))
4677 if not Is_Entity_Name (Object_Definition (N)) then
4679 Check_Compile_Time_Size (Act_T);
4681 if Aliased_Present (N) then
4682 Set_Is_Constr_Subt_For_UN_Aliased (Act_T);
4686 -- When the given object definition and the aggregate are specified
4687 -- independently, and their lengths might differ do a length check.
4688 -- This cannot happen if the aggregate is of the form (others =>...)
4690 if not Is_Constrained (T) then
4693 elsif Nkind (E) = N_Raise_Constraint_Error then
4695 -- Aggregate is statically illegal. Place back in declaration
4697 Set_Expression (N, E);
4698 Set_No_Initialization (N, False);
4700 elsif T = Etype (E) then
4703 elsif Nkind (E) = N_Aggregate
4704 and then Present (Component_Associations (E))
4705 and then Present (Choice_List (First (Component_Associations (E))))
4707 Nkind (First (Choice_List (First (Component_Associations (E))))) =
4713 Apply_Length_Check (E, T);
4716 -- If the type is limited unconstrained with defaulted discriminants and
4717 -- there is no expression, then the object is constrained by the
4718 -- defaults, so it is worthwhile building the corresponding subtype.
4720 elsif (Is_Limited_Record (T) or else Is_Concurrent_Type (T))
4721 and then not Is_Constrained (T)
4722 and then Has_Discriminants (T)
4725 Act_T := Build_Default_Subtype (T, N);
4727 -- Ada 2005: A limited object may be initialized by means of an
4728 -- aggregate. If the type has default discriminants it has an
4729 -- unconstrained nominal type, Its actual subtype will be obtained
4730 -- from the aggregate, and not from the default discriminants.
4735 Rewrite (Object_Definition (N), New_Occurrence_Of (Act_T, Loc));
4737 elsif Nkind (E) = N_Function_Call
4738 and then Constant_Present (N)
4739 and then Has_Unconstrained_Elements (Etype (E))
4741 -- The back-end has problems with constants of a discriminated type
4742 -- with defaults, if the initial value is a function call. We
4743 -- generate an intermediate temporary that will receive a reference
4744 -- to the result of the call. The initialization expression then
4745 -- becomes a dereference of that temporary.
4747 Remove_Side_Effects (E);
4749 -- If this is a constant declaration of an unconstrained type and
4750 -- the initialization is an aggregate, we can use the subtype of the
4751 -- aggregate for the declared entity because it is immutable.
4753 elsif not Is_Constrained (T)
4754 and then Has_Discriminants (T)
4755 and then Constant_Present (N)
4756 and then not Has_Unchecked_Union (T)
4757 and then Nkind (E) = N_Aggregate
4762 -- Check No_Wide_Characters restriction
4764 Check_Wide_Character_Restriction (T, Object_Definition (N));
4766 -- Indicate this is not set in source. Certainly true for constants, and
4767 -- true for variables so far (will be reset for a variable if and when
4768 -- we encounter a modification in the source).
4770 Set_Never_Set_In_Source (Id);
4772 -- Now establish the proper kind and type of the object
4774 if Constant_Present (N) then
4775 Set_Ekind (Id, E_Constant);
4776 Set_Is_True_Constant (Id);
4779 Set_Ekind (Id, E_Variable);
4781 -- A variable is set as shared passive if it appears in a shared
4782 -- passive package, and is at the outer level. This is not done for
4783 -- entities generated during expansion, because those are always
4784 -- manipulated locally.
4786 if Is_Shared_Passive (Current_Scope)
4787 and then Is_Library_Level_Entity (Id)
4788 and then Comes_From_Source (Id)
4790 Set_Is_Shared_Passive (Id);
4791 Check_Shared_Var (Id, T, N);
4794 -- Set Has_Initial_Value if initializing expression present. Note
4795 -- that if there is no initializing expression, we leave the state
4796 -- of this flag unchanged (usually it will be False, but notably in
4797 -- the case of exception choice variables, it will already be true).
4800 Set_Has_Initial_Value (Id);
4804 -- Set the SPARK mode from the current context (may be overwritten later
4805 -- with explicit pragma).
4807 Set_SPARK_Pragma (Id, SPARK_Mode_Pragma);
4808 Set_SPARK_Pragma_Inherited (Id);
4810 -- Preserve relevant elaboration-related attributes of the context which
4811 -- are no longer available or very expensive to recompute once analysis,
4812 -- resolution, and expansion are over.
4814 Mark_Elaboration_Attributes
4819 -- Initialize alignment and size and capture alignment setting
4821 Init_Alignment (Id);
4823 Set_Optimize_Alignment_Flags (Id);
4825 -- Deal with aliased case
4827 if Aliased_Present (N) then
4828 Set_Is_Aliased (Id);
4830 -- If the object is aliased and the type is unconstrained with
4831 -- defaulted discriminants and there is no expression, then the
4832 -- object is constrained by the defaults, so it is worthwhile
4833 -- building the corresponding subtype.
4835 -- Ada 2005 (AI-363): If the aliased object is discriminated and
4836 -- unconstrained, then only establish an actual subtype if the
4837 -- nominal subtype is indefinite. In definite cases the object is
4838 -- unconstrained in Ada 2005.
4841 and then Is_Record_Type (T)
4842 and then not Is_Constrained (T)
4843 and then Has_Discriminants (T)
4844 and then (Ada_Version < Ada_2005
4845 or else not Is_Definite_Subtype (T))
4847 Set_Actual_Subtype (Id, Build_Default_Subtype (T, N));
4851 -- Now we can set the type of the object
4853 Set_Etype (Id, Act_T);
4855 -- Non-constant object is marked to be treated as volatile if type is
4856 -- volatile and we clear the Current_Value setting that may have been
4857 -- set above. Doing so for constants isn't required and might interfere
4858 -- with possible uses of the object as a static expression in contexts
4859 -- incompatible with volatility (e.g. as a case-statement alternative).
4861 if Ekind (Id) /= E_Constant and then Treat_As_Volatile (Etype (Id)) then
4862 Set_Treat_As_Volatile (Id);
4863 Set_Current_Value (Id, Empty);
4866 -- Deal with controlled types
4868 if Has_Controlled_Component (Etype (Id))
4869 or else Is_Controlled (Etype (Id))
4871 if not Is_Library_Level_Entity (Id) then
4872 Check_Restriction (No_Nested_Finalization, N);
4874 Validate_Controlled_Object (Id);
4878 if Has_Task (Etype (Id)) then
4879 Check_Restriction (No_Tasking, N);
4881 -- Deal with counting max tasks
4883 -- Nothing to do if inside a generic
4885 if Inside_A_Generic then
4888 -- If library level entity, then count tasks
4890 elsif Is_Library_Level_Entity (Id) then
4891 Check_Restriction (Max_Tasks, N, Count_Tasks (Etype (Id)));
4893 -- If not library level entity, then indicate we don't know max
4894 -- tasks and also check task hierarchy restriction and blocking
4895 -- operation (since starting a task is definitely blocking).
4898 Check_Restriction (Max_Tasks, N);
4899 Check_Restriction (No_Task_Hierarchy, N);
4900 Check_Potentially_Blocking_Operation (N);
4903 -- A rather specialized test. If we see two tasks being declared
4904 -- of the same type in the same object declaration, and the task
4905 -- has an entry with an address clause, we know that program error
4906 -- will be raised at run time since we can't have two tasks with
4907 -- entries at the same address.
4909 if Is_Task_Type (Etype (Id)) and then More_Ids (N) then
4914 E := First_Entity (Etype (Id));
4915 while Present (E) loop
4916 if Ekind (E) = E_Entry
4917 and then Present (Get_Attribute_Definition_Clause
4918 (E, Attribute_Address))
4920 Error_Msg_Warn := SPARK_Mode /= On;
4922 ("more than one task with same entry address<<", N);
4923 Error_Msg_N ("\Program_Error [<<", N);
4925 Make_Raise_Program_Error (Loc,
4926 Reason => PE_Duplicated_Entry_Address));
4936 -- Some simple constant-propagation: if the expression is a constant
4937 -- string initialized with a literal, share the literal. This avoids
4941 and then Is_Entity_Name (E)
4942 and then Ekind (Entity (E)) = E_Constant
4943 and then Base_Type (Etype (E)) = Standard_String
4946 Val : constant Node_Id := Constant_Value (Entity (E));
4948 if Present (Val) and then Nkind (Val) = N_String_Literal then
4949 Rewrite (E, New_Copy (Val));
4954 -- Another optimization: if the nominal subtype is unconstrained and
4955 -- the expression is a function call that returns an unconstrained
4956 -- type, rewrite the declaration as a renaming of the result of the
4957 -- call. The exceptions below are cases where the copy is expected,
4958 -- either by the back end (Aliased case) or by the semantics, as for
4959 -- initializing controlled types or copying tags for class-wide types.
4962 and then Nkind (E) = N_Explicit_Dereference
4963 and then Nkind (Original_Node (E)) = N_Function_Call
4964 and then not Is_Library_Level_Entity (Id)
4965 and then not Is_Constrained (Underlying_Type (T))
4966 and then not Is_Aliased (Id)
4967 and then not Is_Class_Wide_Type (T)
4968 and then not Is_Controlled (T)
4969 and then not Has_Controlled_Component (Base_Type (T))
4970 and then Expander_Active
4973 Make_Object_Renaming_Declaration (Loc,
4974 Defining_Identifier => Id,
4975 Access_Definition => Empty,
4976 Subtype_Mark => New_Occurrence_Of
4977 (Base_Type (Etype (Id)), Loc),
4980 Set_Renamed_Object (Id, E);
4982 -- Force generation of debugging information for the constant and for
4983 -- the renamed function call.
4985 Set_Debug_Info_Needed (Id);
4986 Set_Debug_Info_Needed (Entity (Prefix (E)));
4989 if Present (Prev_Entity)
4990 and then Is_Frozen (Prev_Entity)
4991 and then not Error_Posted (Id)
4993 Error_Msg_N ("full constant declaration appears too late", N);
4996 Check_Eliminated (Id);
4998 -- Deal with setting In_Private_Part flag if in private part
5000 if Ekind (Scope (Id)) = E_Package
5001 and then In_Private_Part (Scope (Id))
5003 Set_In_Private_Part (Id);
5007 -- Initialize the refined state of a variable here because this is a
5008 -- common destination for legal and illegal object declarations.
5010 if Ekind (Id) = E_Variable then
5011 Set_Encapsulating_State (Id, Empty);
5014 if Has_Aspects (N) then
5015 Analyze_Aspect_Specifications (N, Id);
5018 Analyze_Dimension (N);
5020 -- Verify whether the object declaration introduces an illegal hidden
5021 -- state within a package subject to a null abstract state.
5023 if Ekind (Id) = E_Variable then
5024 Check_No_Hidden_State (Id);
5027 Restore_Ghost_Region (Saved_GM, Saved_IGR);
5028 end Analyze_Object_Declaration;
5030 ---------------------------
5031 -- Analyze_Others_Choice --
5032 ---------------------------
5034 -- Nothing to do for the others choice node itself, the semantic analysis
5035 -- of the others choice will occur as part of the processing of the parent
5037 procedure Analyze_Others_Choice (N : Node_Id) is
5038 pragma Warnings (Off, N);
5041 end Analyze_Others_Choice;
5043 -------------------------------------------
5044 -- Analyze_Private_Extension_Declaration --
5045 -------------------------------------------
5047 procedure Analyze_Private_Extension_Declaration (N : Node_Id) is
5048 Indic : constant Node_Id := Subtype_Indication (N);
5049 T : constant Entity_Id := Defining_Identifier (N);
5051 Iface_Elmt : Elmt_Id;
5052 Parent_Base : Entity_Id;
5053 Parent_Type : Entity_Id;
5056 -- Ada 2005 (AI-251): Decorate all names in list of ancestor interfaces
5058 if Is_Non_Empty_List (Interface_List (N)) then
5064 Intf := First (Interface_List (N));
5065 while Present (Intf) loop
5066 T := Find_Type_Of_Subtype_Indic (Intf);
5068 Diagnose_Interface (Intf, T);
5074 Generate_Definition (T);
5076 -- For other than Ada 2012, just enter the name in the current scope
5078 if Ada_Version < Ada_2012 then
5081 -- Ada 2012 (AI05-0162): Enter the name in the current scope handling
5082 -- case of private type that completes an incomplete type.
5089 Prev := Find_Type_Name (N);
5091 pragma Assert (Prev = T
5092 or else (Ekind (Prev) = E_Incomplete_Type
5093 and then Present (Full_View (Prev))
5094 and then Full_View (Prev) = T));
5098 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
5099 Parent_Base := Base_Type (Parent_Type);
5101 if Parent_Type = Any_Type or else Etype (Parent_Type) = Any_Type then
5102 Set_Ekind (T, Ekind (Parent_Type));
5103 Set_Etype (T, Any_Type);
5106 elsif not Is_Tagged_Type (Parent_Type) then
5108 ("parent of type extension must be a tagged type ", Indic);
5111 elsif Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
5112 Error_Msg_N ("premature derivation of incomplete type", Indic);
5115 elsif Is_Concurrent_Type (Parent_Type) then
5117 ("parent type of a private extension cannot be a synchronized "
5118 & "tagged type (RM 3.9.1 (3/1))", N);
5120 Set_Etype (T, Any_Type);
5121 Set_Ekind (T, E_Limited_Private_Type);
5122 Set_Private_Dependents (T, New_Elmt_List);
5123 Set_Error_Posted (T);
5127 -- Perhaps the parent type should be changed to the class-wide type's
5128 -- specific type in this case to prevent cascading errors ???
5130 if Is_Class_Wide_Type (Parent_Type) then
5132 ("parent of type extension must not be a class-wide type", Indic);
5136 if (not Is_Package_Or_Generic_Package (Current_Scope)
5137 and then Nkind (Parent (N)) /= N_Generic_Subprogram_Declaration)
5138 or else In_Private_Part (Current_Scope)
5140 Error_Msg_N ("invalid context for private extension", N);
5143 -- Set common attributes
5145 Set_Is_Pure (T, Is_Pure (Current_Scope));
5146 Set_Scope (T, Current_Scope);
5147 Set_Ekind (T, E_Record_Type_With_Private);
5148 Init_Size_Align (T);
5149 Set_Default_SSO (T);
5150 Set_No_Reordering (T, No_Component_Reordering);
5152 Set_Etype (T, Parent_Base);
5153 Propagate_Concurrent_Flags (T, Parent_Base);
5155 Set_Convention (T, Convention (Parent_Type));
5156 Set_First_Rep_Item (T, First_Rep_Item (Parent_Type));
5157 Set_Is_First_Subtype (T);
5158 Make_Class_Wide_Type (T);
5160 -- Set the SPARK mode from the current context
5162 Set_SPARK_Pragma (T, SPARK_Mode_Pragma);
5163 Set_SPARK_Pragma_Inherited (T);
5165 if Unknown_Discriminants_Present (N) then
5166 Set_Discriminant_Constraint (T, No_Elist);
5169 Build_Derived_Record_Type (N, Parent_Type, T);
5171 -- A private extension inherits the Default_Initial_Condition pragma
5172 -- coming from any parent type within the derivation chain.
5174 if Has_DIC (Parent_Type) then
5175 Set_Has_Inherited_DIC (T);
5178 -- A private extension inherits any class-wide invariants coming from a
5179 -- parent type or an interface. Note that the invariant procedure of the
5180 -- parent type should not be inherited because the private extension may
5181 -- define invariants of its own.
5183 if Has_Inherited_Invariants (Parent_Type)
5184 or else Has_Inheritable_Invariants (Parent_Type)
5186 Set_Has_Inherited_Invariants (T);
5188 elsif Present (Interfaces (T)) then
5189 Iface_Elmt := First_Elmt (Interfaces (T));
5190 while Present (Iface_Elmt) loop
5191 Iface := Node (Iface_Elmt);
5193 if Has_Inheritable_Invariants (Iface) then
5194 Set_Has_Inherited_Invariants (T);
5198 Next_Elmt (Iface_Elmt);
5202 -- Ada 2005 (AI-443): Synchronized private extension or a rewritten
5203 -- synchronized formal derived type.
5205 if Ada_Version >= Ada_2005 and then Synchronized_Present (N) then
5206 Set_Is_Limited_Record (T);
5208 -- Formal derived type case
5210 if Is_Generic_Type (T) then
5212 -- The parent must be a tagged limited type or a synchronized
5215 if (not Is_Tagged_Type (Parent_Type)
5216 or else not Is_Limited_Type (Parent_Type))
5218 (not Is_Interface (Parent_Type)
5219 or else not Is_Synchronized_Interface (Parent_Type))
5222 ("parent type of & must be tagged limited or synchronized",
5226 -- The progenitors (if any) must be limited or synchronized
5229 if Present (Interfaces (T)) then
5230 Iface_Elmt := First_Elmt (Interfaces (T));
5231 while Present (Iface_Elmt) loop
5232 Iface := Node (Iface_Elmt);
5234 if not Is_Limited_Interface (Iface)
5235 and then not Is_Synchronized_Interface (Iface)
5238 ("progenitor & must be limited or synchronized",
5242 Next_Elmt (Iface_Elmt);
5246 -- Regular derived extension, the parent must be a limited or
5247 -- synchronized interface.
5250 if not Is_Interface (Parent_Type)
5251 or else (not Is_Limited_Interface (Parent_Type)
5252 and then not Is_Synchronized_Interface (Parent_Type))
5255 ("parent type of & must be limited interface", N, T);
5259 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
5260 -- extension with a synchronized parent must be explicitly declared
5261 -- synchronized, because the full view will be a synchronized type.
5262 -- This must be checked before the check for limited types below,
5263 -- to ensure that types declared limited are not allowed to extend
5264 -- synchronized interfaces.
5266 elsif Is_Interface (Parent_Type)
5267 and then Is_Synchronized_Interface (Parent_Type)
5268 and then not Synchronized_Present (N)
5271 ("private extension of& must be explicitly synchronized",
5274 elsif Limited_Present (N) then
5275 Set_Is_Limited_Record (T);
5277 if not Is_Limited_Type (Parent_Type)
5279 (not Is_Interface (Parent_Type)
5280 or else not Is_Limited_Interface (Parent_Type))
5282 Error_Msg_NE ("parent type& of limited extension must be limited",
5287 -- Remember that its parent type has a private extension. Used to warn
5288 -- on public primitives of the parent type defined after its private
5289 -- extensions (see Check_Dispatching_Operation).
5291 Set_Has_Private_Extension (Parent_Type);
5294 if Has_Aspects (N) then
5295 Analyze_Aspect_Specifications (N, T);
5297 end Analyze_Private_Extension_Declaration;
5299 ---------------------------------
5300 -- Analyze_Subtype_Declaration --
5301 ---------------------------------
5303 procedure Analyze_Subtype_Declaration
5305 Skip : Boolean := False)
5307 Id : constant Entity_Id := Defining_Identifier (N);
5308 R_Checks : Check_Result;
5312 Generate_Definition (Id);
5313 Set_Is_Pure (Id, Is_Pure (Current_Scope));
5314 Init_Size_Align (Id);
5316 -- The following guard condition on Enter_Name is to handle cases where
5317 -- the defining identifier has already been entered into the scope but
5318 -- the declaration as a whole needs to be analyzed.
5320 -- This case in particular happens for derived enumeration types. The
5321 -- derived enumeration type is processed as an inserted enumeration type
5322 -- declaration followed by a rewritten subtype declaration. The defining
5323 -- identifier, however, is entered into the name scope very early in the
5324 -- processing of the original type declaration and therefore needs to be
5325 -- avoided here, when the created subtype declaration is analyzed. (See
5326 -- Build_Derived_Types)
5328 -- This also happens when the full view of a private type is derived
5329 -- type with constraints. In this case the entity has been introduced
5330 -- in the private declaration.
5332 -- Finally this happens in some complex cases when validity checks are
5333 -- enabled, where the same subtype declaration may be analyzed twice.
5334 -- This can happen if the subtype is created by the preanalysis of
5335 -- an attribute tht gives the range of a loop statement, and the loop
5336 -- itself appears within an if_statement that will be rewritten during
5340 or else (Present (Etype (Id))
5341 and then (Is_Private_Type (Etype (Id))
5342 or else Is_Task_Type (Etype (Id))
5343 or else Is_Rewrite_Substitution (N)))
5347 elsif Current_Entity (Id) = Id then
5354 T := Process_Subtype (Subtype_Indication (N), N, Id, 'P');
5356 -- Class-wide equivalent types of records with unknown discriminants
5357 -- involve the generation of an itype which serves as the private view
5358 -- of a constrained record subtype. In such cases the base type of the
5359 -- current subtype we are processing is the private itype. Use the full
5360 -- of the private itype when decorating various attributes.
5363 and then Is_Private_Type (T)
5364 and then Present (Full_View (T))
5369 -- Inherit common attributes
5371 Set_Is_Volatile (Id, Is_Volatile (T));
5372 Set_Treat_As_Volatile (Id, Treat_As_Volatile (T));
5373 Set_Is_Generic_Type (Id, Is_Generic_Type (Base_Type (T)));
5374 Set_Convention (Id, Convention (T));
5376 -- If ancestor has predicates then so does the subtype, and in addition
5377 -- we must delay the freeze to properly arrange predicate inheritance.
5379 -- The Ancestor_Type test is really unpleasant, there seem to be cases
5380 -- in which T = ID, so the above tests and assignments do nothing???
5382 if Has_Predicates (T)
5383 or else (Present (Ancestor_Subtype (T))
5384 and then Has_Predicates (Ancestor_Subtype (T)))
5386 Set_Has_Predicates (Id);
5387 Set_Has_Delayed_Freeze (Id);
5389 -- Generated subtypes inherit the predicate function from the parent
5390 -- (no aspects to examine on the generated declaration).
5392 if not Comes_From_Source (N) then
5393 Set_Ekind (Id, Ekind (T));
5395 if Present (Predicate_Function (Id)) then
5398 elsif Present (Predicate_Function (T)) then
5399 Set_Predicate_Function (Id, Predicate_Function (T));
5401 elsif Present (Ancestor_Subtype (T))
5402 and then Present (Predicate_Function (Ancestor_Subtype (T)))
5404 Set_Predicate_Function (Id,
5405 Predicate_Function (Ancestor_Subtype (T)));
5410 -- Subtype of Boolean cannot have a constraint in SPARK
5412 if Is_Boolean_Type (T)
5413 and then Nkind (Subtype_Indication (N)) = N_Subtype_Indication
5415 Check_SPARK_05_Restriction
5416 ("subtype of Boolean cannot have constraint", N);
5419 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5421 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5427 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint then
5428 One_Cstr := First (Constraints (Cstr));
5429 while Present (One_Cstr) loop
5431 -- Index or discriminant constraint in SPARK must be a
5435 Nkind_In (One_Cstr, N_Identifier, N_Expanded_Name)
5437 Check_SPARK_05_Restriction
5438 ("subtype mark required", One_Cstr);
5440 -- String subtype must have a lower bound of 1 in SPARK.
5441 -- Note that we do not need to test for the nonstatic case
5442 -- here, since that was already taken care of in
5443 -- Process_Range_Expr_In_Decl.
5445 elsif Base_Type (T) = Standard_String then
5446 Get_Index_Bounds (One_Cstr, Low, High);
5448 if Is_OK_Static_Expression (Low)
5449 and then Expr_Value (Low) /= 1
5451 Check_SPARK_05_Restriction
5452 ("String subtype must have lower bound of 1", N);
5462 -- In the case where there is no constraint given in the subtype
5463 -- indication, Process_Subtype just returns the Subtype_Mark, so its
5464 -- semantic attributes must be established here.
5466 if Nkind (Subtype_Indication (N)) /= N_Subtype_Indication then
5467 Set_Etype (Id, Base_Type (T));
5469 -- Subtype of unconstrained array without constraint is not allowed
5472 if Is_Array_Type (T) and then not Is_Constrained (T) then
5473 Check_SPARK_05_Restriction
5474 ("subtype of unconstrained array must have constraint", N);
5479 Set_Ekind (Id, E_Array_Subtype);
5480 Copy_Array_Subtype_Attributes (Id, T);
5482 when Decimal_Fixed_Point_Kind =>
5483 Set_Ekind (Id, E_Decimal_Fixed_Point_Subtype);
5484 Set_Digits_Value (Id, Digits_Value (T));
5485 Set_Delta_Value (Id, Delta_Value (T));
5486 Set_Scale_Value (Id, Scale_Value (T));
5487 Set_Small_Value (Id, Small_Value (T));
5488 Set_Scalar_Range (Id, Scalar_Range (T));
5489 Set_Machine_Radix_10 (Id, Machine_Radix_10 (T));
5490 Set_Is_Constrained (Id, Is_Constrained (T));
5491 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5492 Set_RM_Size (Id, RM_Size (T));
5494 when Enumeration_Kind =>
5495 Set_Ekind (Id, E_Enumeration_Subtype);
5496 Set_First_Literal (Id, First_Literal (Base_Type (T)));
5497 Set_Scalar_Range (Id, Scalar_Range (T));
5498 Set_Is_Character_Type (Id, Is_Character_Type (T));
5499 Set_Is_Constrained (Id, Is_Constrained (T));
5500 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5501 Set_RM_Size (Id, RM_Size (T));
5503 when Ordinary_Fixed_Point_Kind =>
5504 Set_Ekind (Id, E_Ordinary_Fixed_Point_Subtype);
5505 Set_Scalar_Range (Id, Scalar_Range (T));
5506 Set_Small_Value (Id, Small_Value (T));
5507 Set_Delta_Value (Id, Delta_Value (T));
5508 Set_Is_Constrained (Id, Is_Constrained (T));
5509 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5510 Set_RM_Size (Id, RM_Size (T));
5513 Set_Ekind (Id, E_Floating_Point_Subtype);
5514 Set_Scalar_Range (Id, Scalar_Range (T));
5515 Set_Digits_Value (Id, Digits_Value (T));
5516 Set_Is_Constrained (Id, Is_Constrained (T));
5518 -- If the floating point type has dimensions, these will be
5519 -- inherited subsequently when Analyze_Dimensions is called.
5521 when Signed_Integer_Kind =>
5522 Set_Ekind (Id, E_Signed_Integer_Subtype);
5523 Set_Scalar_Range (Id, Scalar_Range (T));
5524 Set_Is_Constrained (Id, Is_Constrained (T));
5525 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5526 Set_RM_Size (Id, RM_Size (T));
5528 when Modular_Integer_Kind =>
5529 Set_Ekind (Id, E_Modular_Integer_Subtype);
5530 Set_Scalar_Range (Id, Scalar_Range (T));
5531 Set_Is_Constrained (Id, Is_Constrained (T));
5532 Set_Is_Known_Valid (Id, Is_Known_Valid (T));
5533 Set_RM_Size (Id, RM_Size (T));
5535 when Class_Wide_Kind =>
5536 Set_Ekind (Id, E_Class_Wide_Subtype);
5537 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5538 Set_Cloned_Subtype (Id, T);
5539 Set_Is_Tagged_Type (Id, True);
5540 Set_Has_Unknown_Discriminants
5542 Set_No_Tagged_Streams_Pragma
5543 (Id, No_Tagged_Streams_Pragma (T));
5545 if Ekind (T) = E_Class_Wide_Subtype then
5546 Set_Equivalent_Type (Id, Equivalent_Type (T));
5549 when E_Record_Subtype
5552 Set_Ekind (Id, E_Record_Subtype);
5554 if Ekind (T) = E_Record_Subtype
5555 and then Present (Cloned_Subtype (T))
5557 Set_Cloned_Subtype (Id, Cloned_Subtype (T));
5559 Set_Cloned_Subtype (Id, T);
5562 Set_First_Entity (Id, First_Entity (T));
5563 Set_Last_Entity (Id, Last_Entity (T));
5564 Set_Has_Discriminants (Id, Has_Discriminants (T));
5565 Set_Is_Constrained (Id, Is_Constrained (T));
5566 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5567 Set_Has_Implicit_Dereference
5568 (Id, Has_Implicit_Dereference (T));
5569 Set_Has_Unknown_Discriminants
5570 (Id, Has_Unknown_Discriminants (T));
5572 if Has_Discriminants (T) then
5573 Set_Discriminant_Constraint
5574 (Id, Discriminant_Constraint (T));
5575 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5577 elsif Has_Unknown_Discriminants (Id) then
5578 Set_Discriminant_Constraint (Id, No_Elist);
5581 if Is_Tagged_Type (T) then
5582 Set_Is_Tagged_Type (Id, True);
5583 Set_No_Tagged_Streams_Pragma
5584 (Id, No_Tagged_Streams_Pragma (T));
5585 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5586 Set_Direct_Primitive_Operations
5587 (Id, Direct_Primitive_Operations (T));
5588 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5590 if Is_Interface (T) then
5591 Set_Is_Interface (Id);
5592 Set_Is_Limited_Interface (Id, Is_Limited_Interface (T));
5596 when Private_Kind =>
5597 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5598 Set_Has_Discriminants (Id, Has_Discriminants (T));
5599 Set_Is_Constrained (Id, Is_Constrained (T));
5600 Set_First_Entity (Id, First_Entity (T));
5601 Set_Last_Entity (Id, Last_Entity (T));
5602 Set_Private_Dependents (Id, New_Elmt_List);
5603 Set_Is_Limited_Record (Id, Is_Limited_Record (T));
5604 Set_Has_Implicit_Dereference
5605 (Id, Has_Implicit_Dereference (T));
5606 Set_Has_Unknown_Discriminants
5607 (Id, Has_Unknown_Discriminants (T));
5608 Set_Known_To_Have_Preelab_Init
5609 (Id, Known_To_Have_Preelab_Init (T));
5611 if Is_Tagged_Type (T) then
5612 Set_Is_Tagged_Type (Id);
5613 Set_No_Tagged_Streams_Pragma (Id,
5614 No_Tagged_Streams_Pragma (T));
5615 Set_Is_Abstract_Type (Id, Is_Abstract_Type (T));
5616 Set_Class_Wide_Type (Id, Class_Wide_Type (T));
5617 Set_Direct_Primitive_Operations (Id,
5618 Direct_Primitive_Operations (T));
5621 -- In general the attributes of the subtype of a private type
5622 -- are the attributes of the partial view of parent. However,
5623 -- the full view may be a discriminated type, and the subtype
5624 -- must share the discriminant constraint to generate correct
5625 -- calls to initialization procedures.
5627 if Has_Discriminants (T) then
5628 Set_Discriminant_Constraint
5629 (Id, Discriminant_Constraint (T));
5630 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5632 elsif Present (Full_View (T))
5633 and then Has_Discriminants (Full_View (T))
5635 Set_Discriminant_Constraint
5636 (Id, Discriminant_Constraint (Full_View (T)));
5637 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5639 -- This would seem semantically correct, but apparently
5640 -- generates spurious errors about missing components ???
5642 -- Set_Has_Discriminants (Id);
5645 Prepare_Private_Subtype_Completion (Id, N);
5647 -- If this is the subtype of a constrained private type with
5648 -- discriminants that has got a full view and we also have
5649 -- built a completion just above, show that the completion
5650 -- is a clone of the full view to the back-end.
5652 if Has_Discriminants (T)
5653 and then not Has_Unknown_Discriminants (T)
5654 and then not Is_Empty_Elmt_List (Discriminant_Constraint (T))
5655 and then Present (Full_View (T))
5656 and then Present (Full_View (Id))
5658 Set_Cloned_Subtype (Full_View (Id), Full_View (T));
5662 Set_Ekind (Id, E_Access_Subtype);
5663 Set_Is_Constrained (Id, Is_Constrained (T));
5664 Set_Is_Access_Constant
5665 (Id, Is_Access_Constant (T));
5666 Set_Directly_Designated_Type
5667 (Id, Designated_Type (T));
5668 Set_Can_Never_Be_Null (Id, Can_Never_Be_Null (T));
5670 -- A Pure library_item must not contain the declaration of a
5671 -- named access type, except within a subprogram, generic
5672 -- subprogram, task unit, or protected unit, or if it has
5673 -- a specified Storage_Size of zero (RM05-10.2.1(15.4-15.5)).
5675 if Comes_From_Source (Id)
5676 and then In_Pure_Unit
5677 and then not In_Subprogram_Task_Protected_Unit
5678 and then not No_Pool_Assigned (Id)
5681 ("named access types not allowed in pure unit", N);
5684 when Concurrent_Kind =>
5685 Set_Ekind (Id, Subtype_Kind (Ekind (T)));
5686 Set_Corresponding_Record_Type (Id,
5687 Corresponding_Record_Type (T));
5688 Set_First_Entity (Id, First_Entity (T));
5689 Set_First_Private_Entity (Id, First_Private_Entity (T));
5690 Set_Has_Discriminants (Id, Has_Discriminants (T));
5691 Set_Is_Constrained (Id, Is_Constrained (T));
5692 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5693 Set_Last_Entity (Id, Last_Entity (T));
5695 if Is_Tagged_Type (T) then
5696 Set_No_Tagged_Streams_Pragma
5697 (Id, No_Tagged_Streams_Pragma (T));
5700 if Has_Discriminants (T) then
5701 Set_Discriminant_Constraint
5702 (Id, Discriminant_Constraint (T));
5703 Set_Stored_Constraint_From_Discriminant_Constraint (Id);
5706 when Incomplete_Kind =>
5707 if Ada_Version >= Ada_2005 then
5709 -- In Ada 2005 an incomplete type can be explicitly tagged:
5710 -- propagate indication. Note that we also have to include
5711 -- subtypes for Ada 2012 extended use of incomplete types.
5713 Set_Ekind (Id, E_Incomplete_Subtype);
5714 Set_Is_Tagged_Type (Id, Is_Tagged_Type (T));
5715 Set_Private_Dependents (Id, New_Elmt_List);
5717 if Is_Tagged_Type (Id) then
5718 Set_No_Tagged_Streams_Pragma
5719 (Id, No_Tagged_Streams_Pragma (T));
5720 Set_Direct_Primitive_Operations (Id, New_Elmt_List);
5723 -- Ada 2005 (AI-412): Decorate an incomplete subtype of an
5724 -- incomplete type visible through a limited with clause.
5726 if From_Limited_With (T)
5727 and then Present (Non_Limited_View (T))
5729 Set_From_Limited_With (Id);
5730 Set_Non_Limited_View (Id, Non_Limited_View (T));
5732 -- Ada 2005 (AI-412): Add the regular incomplete subtype
5733 -- to the private dependents of the original incomplete
5734 -- type for future transformation.
5737 Append_Elmt (Id, Private_Dependents (T));
5740 -- If the subtype name denotes an incomplete type an error
5741 -- was already reported by Process_Subtype.
5744 Set_Etype (Id, Any_Type);
5748 raise Program_Error;
5751 -- If there is no constraint in the subtype indication, the
5752 -- declared entity inherits predicates from the parent.
5754 Inherit_Predicate_Flags (Id, T);
5757 if Etype (Id) = Any_Type then
5761 -- Some common processing on all types
5763 Set_Size_Info (Id, T);
5764 Set_First_Rep_Item (Id, First_Rep_Item (T));
5766 -- If the parent type is a generic actual, so is the subtype. This may
5767 -- happen in a nested instance. Why Comes_From_Source test???
5769 if not Comes_From_Source (N) then
5770 Set_Is_Generic_Actual_Type (Id, Is_Generic_Actual_Type (T));
5773 -- If this is a subtype declaration for an actual in an instance,
5774 -- inherit static and dynamic predicates if any.
5776 -- If declaration has no aspect specifications, inherit predicate
5777 -- info as well. Unclear how to handle the case of both specified
5778 -- and inherited predicates ??? Other inherited aspects, such as
5779 -- invariants, should be OK, but the combination with later pragmas
5780 -- may also require special merging.
5782 if Has_Predicates (T)
5783 and then Present (Predicate_Function (T))
5785 ((In_Instance and then not Comes_From_Source (N))
5786 or else No (Aspect_Specifications (N)))
5788 Set_Subprograms_For_Type (Id, Subprograms_For_Type (T));
5790 if Has_Static_Predicate (T) then
5791 Set_Has_Static_Predicate (Id);
5792 Set_Static_Discrete_Predicate (Id, Static_Discrete_Predicate (T));
5796 -- Remaining processing depends on characteristics of base type
5800 Set_Is_Immediately_Visible (Id, True);
5801 Set_Depends_On_Private (Id, Has_Private_Component (T));
5802 Set_Is_Descendant_Of_Address (Id, Is_Descendant_Of_Address (T));
5804 if Is_Interface (T) then
5805 Set_Is_Interface (Id);
5808 if Present (Generic_Parent_Type (N))
5810 (Nkind (Parent (Generic_Parent_Type (N))) /=
5811 N_Formal_Type_Declaration
5812 or else Nkind (Formal_Type_Definition
5813 (Parent (Generic_Parent_Type (N)))) /=
5814 N_Formal_Private_Type_Definition)
5816 if Is_Tagged_Type (Id) then
5818 -- If this is a generic actual subtype for a synchronized type,
5819 -- the primitive operations are those of the corresponding record
5820 -- for which there is a separate subtype declaration.
5822 if Is_Concurrent_Type (Id) then
5824 elsif Is_Class_Wide_Type (Id) then
5825 Derive_Subprograms (Generic_Parent_Type (N), Id, Etype (T));
5827 Derive_Subprograms (Generic_Parent_Type (N), Id, T);
5830 elsif Scope (Etype (Id)) /= Standard_Standard then
5831 Derive_Subprograms (Generic_Parent_Type (N), Id);
5835 if Is_Private_Type (T) and then Present (Full_View (T)) then
5836 Conditional_Delay (Id, Full_View (T));
5838 -- The subtypes of components or subcomponents of protected types
5839 -- do not need freeze nodes, which would otherwise appear in the
5840 -- wrong scope (before the freeze node for the protected type). The
5841 -- proper subtypes are those of the subcomponents of the corresponding
5844 elsif Ekind (Scope (Id)) /= E_Protected_Type
5845 and then Present (Scope (Scope (Id))) -- error defense
5846 and then Ekind (Scope (Scope (Id))) /= E_Protected_Type
5848 Conditional_Delay (Id, T);
5851 -- If we have a subtype of an incomplete type whose full type is a
5852 -- derived numeric type, we need to have a freeze node for the subtype.
5853 -- Otherwise gigi will complain while computing the (static) bounds of
5857 and then Is_Elementary_Type (Id)
5858 and then Etype (Id) /= Id
5861 Partial : constant Entity_Id :=
5862 Incomplete_Or_Partial_View (First_Subtype (Id));
5864 if Present (Partial)
5865 and then Ekind (Partial) = E_Incomplete_Type
5867 Set_Has_Delayed_Freeze (Id);
5872 -- Check that Constraint_Error is raised for a scalar subtype indication
5873 -- when the lower or upper bound of a non-null range lies outside the
5874 -- range of the type mark.
5876 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5877 if Is_Scalar_Type (Etype (Id))
5878 and then Scalar_Range (Id) /=
5880 (Etype (Subtype_Mark (Subtype_Indication (N))))
5884 Etype (Subtype_Mark (Subtype_Indication (N))));
5886 -- In the array case, check compatibility for each index
5888 elsif Is_Array_Type (Etype (Id)) and then Present (First_Index (Id))
5890 -- This really should be a subprogram that finds the indications
5894 Subt_Index : Node_Id := First_Index (Id);
5895 Target_Index : Node_Id :=
5897 (Subtype_Mark (Subtype_Indication (N))));
5898 Has_Dyn_Chk : Boolean := Has_Dynamic_Range_Check (N);
5901 while Present (Subt_Index) loop
5902 if ((Nkind (Subt_Index) = N_Identifier
5903 and then Ekind (Entity (Subt_Index)) in Scalar_Kind)
5904 or else Nkind (Subt_Index) = N_Subtype_Indication)
5906 Nkind (Scalar_Range (Etype (Subt_Index))) = N_Range
5909 Target_Typ : constant Entity_Id :=
5910 Etype (Target_Index);
5914 (Scalar_Range (Etype (Subt_Index)),
5917 Defining_Identifier (N));
5919 -- Reset Has_Dynamic_Range_Check on the subtype to
5920 -- prevent elision of the index check due to a dynamic
5921 -- check generated for a preceding index (needed since
5922 -- Insert_Range_Checks tries to avoid generating
5923 -- redundant checks on a given declaration).
5925 Set_Has_Dynamic_Range_Check (N, False);
5931 Sloc (Defining_Identifier (N)));
5933 -- Record whether this index involved a dynamic check
5936 Has_Dyn_Chk or else Has_Dynamic_Range_Check (N);
5940 Next_Index (Subt_Index);
5941 Next_Index (Target_Index);
5944 -- Finally, mark whether the subtype involves dynamic checks
5946 Set_Has_Dynamic_Range_Check (N, Has_Dyn_Chk);
5951 Set_Optimize_Alignment_Flags (Id);
5952 Check_Eliminated (Id);
5955 if Has_Aspects (N) then
5956 Analyze_Aspect_Specifications (N, Id);
5959 Analyze_Dimension (N);
5961 -- Check No_Dynamic_Sized_Objects restriction, which disallows subtype
5962 -- indications on composite types where the constraints are dynamic.
5963 -- Note that object declarations and aggregates generate implicit
5964 -- subtype declarations, which this covers. One special case is that the
5965 -- implicitly generated "=" for discriminated types includes an
5966 -- offending subtype declaration, which is harmless, so we ignore it
5969 if Nkind (Subtype_Indication (N)) = N_Subtype_Indication then
5971 Cstr : constant Node_Id := Constraint (Subtype_Indication (N));
5973 if Nkind (Cstr) = N_Index_Or_Discriminant_Constraint
5974 and then not (Is_Internal (Id)
5975 and then Is_TSS (Scope (Id),
5976 TSS_Composite_Equality))
5977 and then not Within_Init_Proc
5978 and then not All_Composite_Constraints_Static (Cstr)
5980 Check_Restriction (No_Dynamic_Sized_Objects, Cstr);
5984 end Analyze_Subtype_Declaration;
5986 --------------------------------
5987 -- Analyze_Subtype_Indication --
5988 --------------------------------
5990 procedure Analyze_Subtype_Indication (N : Node_Id) is
5991 T : constant Entity_Id := Subtype_Mark (N);
5992 R : constant Node_Id := Range_Expression (Constraint (N));
5999 Set_Etype (N, Etype (R));
6000 Resolve (R, Entity (T));
6002 Set_Error_Posted (R);
6003 Set_Error_Posted (T);
6005 end Analyze_Subtype_Indication;
6007 --------------------------
6008 -- Analyze_Variant_Part --
6009 --------------------------
6011 procedure Analyze_Variant_Part (N : Node_Id) is
6012 Discr_Name : Node_Id;
6013 Discr_Type : Entity_Id;
6015 procedure Process_Variant (A : Node_Id);
6016 -- Analyze declarations for a single variant
6018 package Analyze_Variant_Choices is
6019 new Generic_Analyze_Choices (Process_Variant);
6020 use Analyze_Variant_Choices;
6022 ---------------------
6023 -- Process_Variant --
6024 ---------------------
6026 procedure Process_Variant (A : Node_Id) is
6027 CL : constant Node_Id := Component_List (A);
6029 if not Null_Present (CL) then
6030 Analyze_Declarations (Component_Items (CL));
6032 if Present (Variant_Part (CL)) then
6033 Analyze (Variant_Part (CL));
6036 end Process_Variant;
6038 -- Start of processing for Analyze_Variant_Part
6041 Discr_Name := Name (N);
6042 Analyze (Discr_Name);
6044 -- If Discr_Name bad, get out (prevent cascaded errors)
6046 if Etype (Discr_Name) = Any_Type then
6050 -- Check invalid discriminant in variant part
6052 if Ekind (Entity (Discr_Name)) /= E_Discriminant then
6053 Error_Msg_N ("invalid discriminant name in variant part", Discr_Name);
6056 Discr_Type := Etype (Entity (Discr_Name));
6058 if not Is_Discrete_Type (Discr_Type) then
6060 ("discriminant in a variant part must be of a discrete type",
6065 -- Now analyze the choices, which also analyzes the declarations that
6066 -- are associated with each choice.
6068 Analyze_Choices (Variants (N), Discr_Type);
6070 -- Note: we used to instantiate and call Check_Choices here to check
6071 -- that the choices covered the discriminant, but it's too early to do
6072 -- that because of statically predicated subtypes, whose analysis may
6073 -- be deferred to their freeze point which may be as late as the freeze
6074 -- point of the containing record. So this call is now to be found in
6075 -- Freeze_Record_Declaration.
6077 end Analyze_Variant_Part;
6079 ----------------------------
6080 -- Array_Type_Declaration --
6081 ----------------------------
6083 procedure Array_Type_Declaration (T : in out Entity_Id; Def : Node_Id) is
6084 Component_Def : constant Node_Id := Component_Definition (Def);
6085 Component_Typ : constant Node_Id := Subtype_Indication (Component_Def);
6086 P : constant Node_Id := Parent (Def);
6087 Element_Type : Entity_Id;
6088 Implicit_Base : Entity_Id;
6092 Related_Id : Entity_Id := Empty;
6095 if Nkind (Def) = N_Constrained_Array_Definition then
6096 Index := First (Discrete_Subtype_Definitions (Def));
6098 Index := First (Subtype_Marks (Def));
6101 -- Find proper names for the implicit types which may be public. In case
6102 -- of anonymous arrays we use the name of the first object of that type
6106 Related_Id := Defining_Identifier (P);
6112 while Present (Index) loop
6115 -- Test for odd case of trying to index a type by the type itself
6117 if Is_Entity_Name (Index) and then Entity (Index) = T then
6118 Error_Msg_N ("type& cannot be indexed by itself", Index);
6119 Set_Entity (Index, Standard_Boolean);
6120 Set_Etype (Index, Standard_Boolean);
6123 -- Check SPARK restriction requiring a subtype mark
6125 if not Nkind_In (Index, N_Identifier, N_Expanded_Name) then
6126 Check_SPARK_05_Restriction ("subtype mark required", Index);
6129 -- Add a subtype declaration for each index of private array type
6130 -- declaration whose etype is also private. For example:
6133 -- type Index is private;
6135 -- type Table is array (Index) of ...
6138 -- This is currently required by the expander for the internally
6139 -- generated equality subprogram of records with variant parts in
6140 -- which the etype of some component is such private type.
6142 if Ekind (Current_Scope) = E_Package
6143 and then In_Private_Part (Current_Scope)
6144 and then Has_Private_Declaration (Etype (Index))
6147 Loc : constant Source_Ptr := Sloc (Def);
6152 New_E := Make_Temporary (Loc, 'T');
6153 Set_Is_Internal (New_E);
6156 Make_Subtype_Declaration (Loc,
6157 Defining_Identifier => New_E,
6158 Subtype_Indication =>
6159 New_Occurrence_Of (Etype (Index), Loc));
6161 Insert_Before (Parent (Def), Decl);
6163 Set_Etype (Index, New_E);
6165 -- If the index is a range or a subtype indication it carries
6166 -- no entity. Example:
6169 -- type T is private;
6171 -- type T is new Natural;
6172 -- Table : array (T(1) .. T(10)) of Boolean;
6175 -- Otherwise the type of the reference is its entity.
6177 if Is_Entity_Name (Index) then
6178 Set_Entity (Index, New_E);
6183 Make_Index (Index, P, Related_Id, Nb_Index);
6185 -- Check error of subtype with predicate for index type
6187 Bad_Predicated_Subtype_Use
6188 ("subtype& has predicate, not allowed as index subtype",
6189 Index, Etype (Index));
6191 -- Move to next index
6194 Nb_Index := Nb_Index + 1;
6197 -- Process subtype indication if one is present
6199 if Present (Component_Typ) then
6200 Element_Type := Process_Subtype (Component_Typ, P, Related_Id, 'C');
6202 Set_Etype (Component_Typ, Element_Type);
6204 if not Nkind_In (Component_Typ, N_Identifier, N_Expanded_Name) then
6205 Check_SPARK_05_Restriction
6206 ("subtype mark required", Component_Typ);
6209 -- Ada 2005 (AI-230): Access Definition case
6211 else pragma Assert (Present (Access_Definition (Component_Def)));
6213 -- Indicate that the anonymous access type is created by the
6214 -- array type declaration.
6216 Element_Type := Access_Definition
6218 N => Access_Definition (Component_Def));
6219 Set_Is_Local_Anonymous_Access (Element_Type);
6221 -- Propagate the parent. This field is needed if we have to generate
6222 -- the master_id associated with an anonymous access to task type
6223 -- component (see Expand_N_Full_Type_Declaration.Build_Master)
6225 Set_Parent (Element_Type, Parent (T));
6227 -- Ada 2005 (AI-230): In case of components that are anonymous access
6228 -- types the level of accessibility depends on the enclosing type
6231 Set_Scope (Element_Type, Current_Scope); -- Ada 2005 (AI-230)
6233 -- Ada 2005 (AI-254)
6236 CD : constant Node_Id :=
6237 Access_To_Subprogram_Definition
6238 (Access_Definition (Component_Def));
6240 if Present (CD) and then Protected_Present (CD) then
6242 Replace_Anonymous_Access_To_Protected_Subprogram (Def);
6247 -- Constrained array case
6250 T := Create_Itype (E_Void, P, Related_Id, 'T');
6253 if Nkind (Def) = N_Constrained_Array_Definition then
6255 -- Establish Implicit_Base as unconstrained base type
6257 Implicit_Base := Create_Itype (E_Array_Type, P, Related_Id, 'B');
6259 Set_Etype (Implicit_Base, Implicit_Base);
6260 Set_Scope (Implicit_Base, Current_Scope);
6261 Set_Has_Delayed_Freeze (Implicit_Base);
6262 Set_Default_SSO (Implicit_Base);
6264 -- The constrained array type is a subtype of the unconstrained one
6266 Set_Ekind (T, E_Array_Subtype);
6267 Init_Size_Align (T);
6268 Set_Etype (T, Implicit_Base);
6269 Set_Scope (T, Current_Scope);
6270 Set_Is_Constrained (T);
6272 First (Discrete_Subtype_Definitions (Def)));
6273 Set_Has_Delayed_Freeze (T);
6275 -- Complete setup of implicit base type
6277 Set_Component_Size (Implicit_Base, Uint_0);
6278 Set_Component_Type (Implicit_Base, Element_Type);
6279 Set_Finalize_Storage_Only
6281 Finalize_Storage_Only (Element_Type));
6282 Set_First_Index (Implicit_Base, First_Index (T));
6283 Set_Has_Controlled_Component
6285 Has_Controlled_Component (Element_Type)
6286 or else Is_Controlled (Element_Type));
6287 Set_Packed_Array_Impl_Type
6288 (Implicit_Base, Empty);
6290 Propagate_Concurrent_Flags (Implicit_Base, Element_Type);
6292 -- Unconstrained array case
6295 Set_Ekind (T, E_Array_Type);
6296 Init_Size_Align (T);
6298 Set_Scope (T, Current_Scope);
6299 Set_Component_Size (T, Uint_0);
6300 Set_Is_Constrained (T, False);
6301 Set_First_Index (T, First (Subtype_Marks (Def)));
6302 Set_Has_Delayed_Freeze (T, True);
6303 Propagate_Concurrent_Flags (T, Element_Type);
6304 Set_Has_Controlled_Component (T, Has_Controlled_Component
6307 Is_Controlled (Element_Type));
6308 Set_Finalize_Storage_Only (T, Finalize_Storage_Only
6310 Set_Default_SSO (T);
6313 -- Common attributes for both cases
6315 Set_Component_Type (Base_Type (T), Element_Type);
6316 Set_Packed_Array_Impl_Type (T, Empty);
6318 if Aliased_Present (Component_Definition (Def)) then
6319 Check_SPARK_05_Restriction
6320 ("aliased is not allowed", Component_Definition (Def));
6321 Set_Has_Aliased_Components (Etype (T));
6324 -- Ada 2005 (AI-231): Propagate the null-excluding attribute to the
6325 -- array type to ensure that objects of this type are initialized.
6327 if Ada_Version >= Ada_2005 and then Can_Never_Be_Null (Element_Type) then
6328 Set_Can_Never_Be_Null (T);
6330 if Null_Exclusion_Present (Component_Definition (Def))
6332 -- No need to check itypes because in their case this check was
6333 -- done at their point of creation
6335 and then not Is_Itype (Element_Type)
6338 ("`NOT NULL` not allowed (null already excluded)",
6339 Subtype_Indication (Component_Definition (Def)));
6343 Priv := Private_Component (Element_Type);
6345 if Present (Priv) then
6347 -- Check for circular definitions
6349 if Priv = Any_Type then
6350 Set_Component_Type (Etype (T), Any_Type);
6352 -- There is a gap in the visibility of operations on the composite
6353 -- type only if the component type is defined in a different scope.
6355 elsif Scope (Priv) = Current_Scope then
6358 elsif Is_Limited_Type (Priv) then
6359 Set_Is_Limited_Composite (Etype (T));
6360 Set_Is_Limited_Composite (T);
6362 Set_Is_Private_Composite (Etype (T));
6363 Set_Is_Private_Composite (T);
6367 -- A syntax error in the declaration itself may lead to an empty index
6368 -- list, in which case do a minimal patch.
6370 if No (First_Index (T)) then
6371 Error_Msg_N ("missing index definition in array type declaration", T);
6374 Indexes : constant List_Id :=
6375 New_List (New_Occurrence_Of (Any_Id, Sloc (T)));
6377 Set_Discrete_Subtype_Definitions (Def, Indexes);
6378 Set_First_Index (T, First (Indexes));
6383 -- Create a concatenation operator for the new type. Internal array
6384 -- types created for packed entities do not need such, they are
6385 -- compatible with the user-defined type.
6387 if Number_Dimensions (T) = 1
6388 and then not Is_Packed_Array_Impl_Type (T)
6390 New_Concatenation_Op (T);
6393 -- In the case of an unconstrained array the parser has already verified
6394 -- that all the indexes are unconstrained but we still need to make sure
6395 -- that the element type is constrained.
6397 if not Is_Definite_Subtype (Element_Type) then
6399 ("unconstrained element type in array declaration",
6400 Subtype_Indication (Component_Def));
6402 elsif Is_Abstract_Type (Element_Type) then
6404 ("the type of a component cannot be abstract",
6405 Subtype_Indication (Component_Def));
6408 -- There may be an invariant declared for the component type, but
6409 -- the construction of the component invariant checking procedure
6410 -- takes place during expansion.
6411 end Array_Type_Declaration;
6413 ------------------------------------------------------
6414 -- Replace_Anonymous_Access_To_Protected_Subprogram --
6415 ------------------------------------------------------
6417 function Replace_Anonymous_Access_To_Protected_Subprogram
6418 (N : Node_Id) return Entity_Id
6420 Loc : constant Source_Ptr := Sloc (N);
6422 Curr_Scope : constant Scope_Stack_Entry :=
6423 Scope_Stack.Table (Scope_Stack.Last);
6425 Anon : constant Entity_Id := Make_Temporary (Loc, 'S');
6428 -- Access definition in declaration
6431 -- Object definition or formal definition with an access definition
6434 -- Declaration of anonymous access to subprogram type
6437 -- Original specification in access to subprogram
6442 Set_Is_Internal (Anon);
6445 when N_Constrained_Array_Definition
6446 | N_Component_Declaration
6447 | N_Unconstrained_Array_Definition
6449 Comp := Component_Definition (N);
6450 Acc := Access_Definition (Comp);
6452 when N_Discriminant_Specification =>
6453 Comp := Discriminant_Type (N);
6456 when N_Parameter_Specification =>
6457 Comp := Parameter_Type (N);
6460 when N_Access_Function_Definition =>
6461 Comp := Result_Definition (N);
6464 when N_Object_Declaration =>
6465 Comp := Object_Definition (N);
6468 when N_Function_Specification =>
6469 Comp := Result_Definition (N);
6473 raise Program_Error;
6476 Spec := Access_To_Subprogram_Definition (Acc);
6479 Make_Full_Type_Declaration (Loc,
6480 Defining_Identifier => Anon,
6481 Type_Definition => Copy_Separate_Tree (Spec));
6483 Mark_Rewrite_Insertion (Decl);
6485 -- In ASIS mode, analyze the profile on the original node, because
6486 -- the separate copy does not provide enough links to recover the
6487 -- original tree. Analysis is limited to type annotations, within
6488 -- a temporary scope that serves as an anonymous subprogram to collect
6489 -- otherwise useless temporaries and itypes.
6493 Typ : constant Entity_Id := Make_Temporary (Loc, 'S');
6496 if Nkind (Spec) = N_Access_Function_Definition then
6497 Set_Ekind (Typ, E_Function);
6499 Set_Ekind (Typ, E_Procedure);
6502 Set_Parent (Typ, N);
6503 Set_Scope (Typ, Current_Scope);
6506 -- Nothing to do if procedure is parameterless
6508 if Present (Parameter_Specifications (Spec)) then
6509 Process_Formals (Parameter_Specifications (Spec), Spec);
6512 if Nkind (Spec) = N_Access_Function_Definition then
6514 Def : constant Node_Id := Result_Definition (Spec);
6517 -- The result might itself be an anonymous access type, so
6520 if Nkind (Def) = N_Access_Definition then
6521 if Present (Access_To_Subprogram_Definition (Def)) then
6524 Replace_Anonymous_Access_To_Protected_Subprogram
6527 Find_Type (Subtype_Mark (Def));
6540 -- Insert the new declaration in the nearest enclosing scope. If the
6541 -- parent is a body and N is its return type, the declaration belongs
6542 -- in the enclosing scope. Likewise if N is the type of a parameter.
6546 if Nkind (N) = N_Function_Specification
6547 and then Nkind (P) = N_Subprogram_Body
6550 elsif Nkind (N) = N_Parameter_Specification
6551 and then Nkind (P) in N_Subprogram_Specification
6552 and then Nkind (Parent (P)) = N_Subprogram_Body
6554 P := Parent (Parent (P));
6557 while Present (P) and then not Has_Declarations (P) loop
6561 pragma Assert (Present (P));
6563 if Nkind (P) = N_Package_Specification then
6564 Prepend (Decl, Visible_Declarations (P));
6566 Prepend (Decl, Declarations (P));
6569 -- Replace the anonymous type with an occurrence of the new declaration.
6570 -- In all cases the rewritten node does not have the null-exclusion
6571 -- attribute because (if present) it was already inherited by the
6572 -- anonymous entity (Anon). Thus, in case of components we do not
6573 -- inherit this attribute.
6575 if Nkind (N) = N_Parameter_Specification then
6576 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6577 Set_Etype (Defining_Identifier (N), Anon);
6578 Set_Null_Exclusion_Present (N, False);
6580 elsif Nkind (N) = N_Object_Declaration then
6581 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6582 Set_Etype (Defining_Identifier (N), Anon);
6584 elsif Nkind (N) = N_Access_Function_Definition then
6585 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6587 elsif Nkind (N) = N_Function_Specification then
6588 Rewrite (Comp, New_Occurrence_Of (Anon, Loc));
6589 Set_Etype (Defining_Unit_Name (N), Anon);
6593 Make_Component_Definition (Loc,
6594 Subtype_Indication => New_Occurrence_Of (Anon, Loc)));
6597 Mark_Rewrite_Insertion (Comp);
6599 if Nkind_In (N, N_Object_Declaration, N_Access_Function_Definition)
6600 or else (Nkind (Parent (N)) = N_Full_Type_Declaration
6601 and then not Is_Type (Current_Scope))
6604 -- Declaration can be analyzed in the current scope.
6609 -- Temporarily remove the current scope (record or subprogram) from
6610 -- the stack to add the new declarations to the enclosing scope.
6611 -- The anonymous entity is an Itype with the proper attributes.
6613 Scope_Stack.Decrement_Last;
6615 Set_Is_Itype (Anon);
6616 Set_Associated_Node_For_Itype (Anon, N);
6617 Scope_Stack.Append (Curr_Scope);
6620 Set_Ekind (Anon, E_Anonymous_Access_Protected_Subprogram_Type);
6621 Set_Can_Use_Internal_Rep (Anon, not Always_Compatible_Rep_On_Target);
6623 end Replace_Anonymous_Access_To_Protected_Subprogram;
6625 -------------------------------
6626 -- Build_Derived_Access_Type --
6627 -------------------------------
6629 procedure Build_Derived_Access_Type
6631 Parent_Type : Entity_Id;
6632 Derived_Type : Entity_Id)
6634 S : constant Node_Id := Subtype_Indication (Type_Definition (N));
6636 Desig_Type : Entity_Id;
6638 Discr_Con_Elist : Elist_Id;
6639 Discr_Con_El : Elmt_Id;
6643 -- Set the designated type so it is available in case this is an access
6644 -- to a self-referential type, e.g. a standard list type with a next
6645 -- pointer. Will be reset after subtype is built.
6647 Set_Directly_Designated_Type
6648 (Derived_Type, Designated_Type (Parent_Type));
6650 Subt := Process_Subtype (S, N);
6652 if Nkind (S) /= N_Subtype_Indication
6653 and then Subt /= Base_Type (Subt)
6655 Set_Ekind (Derived_Type, E_Access_Subtype);
6658 if Ekind (Derived_Type) = E_Access_Subtype then
6660 Pbase : constant Entity_Id := Base_Type (Parent_Type);
6661 Ibase : constant Entity_Id :=
6662 Create_Itype (Ekind (Pbase), N, Derived_Type, 'B');
6663 Svg_Chars : constant Name_Id := Chars (Ibase);
6664 Svg_Next_E : constant Entity_Id := Next_Entity (Ibase);
6665 Svg_Prev_E : constant Entity_Id := Prev_Entity (Ibase);
6668 Copy_Node (Pbase, Ibase);
6670 -- Restore Itype status after Copy_Node
6672 Set_Is_Itype (Ibase);
6673 Set_Associated_Node_For_Itype (Ibase, N);
6675 Set_Chars (Ibase, Svg_Chars);
6676 Set_Prev_Entity (Ibase, Svg_Prev_E);
6677 Set_Next_Entity (Ibase, Svg_Next_E);
6678 Set_Sloc (Ibase, Sloc (Derived_Type));
6679 Set_Scope (Ibase, Scope (Derived_Type));
6680 Set_Freeze_Node (Ibase, Empty);
6681 Set_Is_Frozen (Ibase, False);
6682 Set_Comes_From_Source (Ibase, False);
6683 Set_Is_First_Subtype (Ibase, False);
6685 Set_Etype (Ibase, Pbase);
6686 Set_Etype (Derived_Type, Ibase);
6690 Set_Directly_Designated_Type
6691 (Derived_Type, Designated_Type (Subt));
6693 Set_Is_Constrained (Derived_Type, Is_Constrained (Subt));
6694 Set_Is_Access_Constant (Derived_Type, Is_Access_Constant (Parent_Type));
6695 Set_Size_Info (Derived_Type, Parent_Type);
6696 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
6697 Set_Depends_On_Private (Derived_Type,
6698 Has_Private_Component (Derived_Type));
6699 Conditional_Delay (Derived_Type, Subt);
6701 -- Ada 2005 (AI-231): Set the null-exclusion attribute, and verify
6702 -- that it is not redundant.
6704 if Null_Exclusion_Present (Type_Definition (N)) then
6705 Set_Can_Never_Be_Null (Derived_Type);
6707 elsif Can_Never_Be_Null (Parent_Type) then
6708 Set_Can_Never_Be_Null (Derived_Type);
6711 -- Note: we do not copy the Storage_Size_Variable, since we always go to
6712 -- the root type for this information.
6714 -- Apply range checks to discriminants for derived record case
6715 -- ??? THIS CODE SHOULD NOT BE HERE REALLY.
6717 Desig_Type := Designated_Type (Derived_Type);
6719 if Is_Composite_Type (Desig_Type)
6720 and then (not Is_Array_Type (Desig_Type))
6721 and then Has_Discriminants (Desig_Type)
6722 and then Base_Type (Desig_Type) /= Desig_Type
6724 Discr_Con_Elist := Discriminant_Constraint (Desig_Type);
6725 Discr_Con_El := First_Elmt (Discr_Con_Elist);
6727 Discr := First_Discriminant (Base_Type (Desig_Type));
6728 while Present (Discr_Con_El) loop
6729 Apply_Range_Check (Node (Discr_Con_El), Etype (Discr));
6730 Next_Elmt (Discr_Con_El);
6731 Next_Discriminant (Discr);
6734 end Build_Derived_Access_Type;
6736 ------------------------------
6737 -- Build_Derived_Array_Type --
6738 ------------------------------
6740 procedure Build_Derived_Array_Type
6742 Parent_Type : Entity_Id;
6743 Derived_Type : Entity_Id)
6745 Loc : constant Source_Ptr := Sloc (N);
6746 Tdef : constant Node_Id := Type_Definition (N);
6747 Indic : constant Node_Id := Subtype_Indication (Tdef);
6748 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
6749 Implicit_Base : Entity_Id := Empty;
6750 New_Indic : Node_Id;
6752 procedure Make_Implicit_Base;
6753 -- If the parent subtype is constrained, the derived type is a subtype
6754 -- of an implicit base type derived from the parent base.
6756 ------------------------
6757 -- Make_Implicit_Base --
6758 ------------------------
6760 procedure Make_Implicit_Base is
6763 Create_Itype (Ekind (Parent_Base), N, Derived_Type, 'B');
6765 Set_Ekind (Implicit_Base, Ekind (Parent_Base));
6766 Set_Etype (Implicit_Base, Parent_Base);
6768 Copy_Array_Subtype_Attributes (Implicit_Base, Parent_Base);
6769 Copy_Array_Base_Type_Attributes (Implicit_Base, Parent_Base);
6771 Set_Has_Delayed_Freeze (Implicit_Base, True);
6772 end Make_Implicit_Base;
6774 -- Start of processing for Build_Derived_Array_Type
6777 if not Is_Constrained (Parent_Type) then
6778 if Nkind (Indic) /= N_Subtype_Indication then
6779 Set_Ekind (Derived_Type, E_Array_Type);
6781 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6782 Copy_Array_Base_Type_Attributes (Derived_Type, Parent_Type);
6784 Set_Has_Delayed_Freeze (Derived_Type, True);
6788 Set_Etype (Derived_Type, Implicit_Base);
6791 Make_Subtype_Declaration (Loc,
6792 Defining_Identifier => Derived_Type,
6793 Subtype_Indication =>
6794 Make_Subtype_Indication (Loc,
6795 Subtype_Mark => New_Occurrence_Of (Implicit_Base, Loc),
6796 Constraint => Constraint (Indic)));
6798 Rewrite (N, New_Indic);
6803 if Nkind (Indic) /= N_Subtype_Indication then
6806 Set_Ekind (Derived_Type, Ekind (Parent_Type));
6807 Set_Etype (Derived_Type, Implicit_Base);
6808 Copy_Array_Subtype_Attributes (Derived_Type, Parent_Type);
6811 Error_Msg_N ("illegal constraint on constrained type", Indic);
6815 -- If parent type is not a derived type itself, and is declared in
6816 -- closed scope (e.g. a subprogram), then we must explicitly introduce
6817 -- the new type's concatenation operator since Derive_Subprograms
6818 -- will not inherit the parent's operator. If the parent type is
6819 -- unconstrained, the operator is of the unconstrained base type.
6821 if Number_Dimensions (Parent_Type) = 1
6822 and then not Is_Limited_Type (Parent_Type)
6823 and then not Is_Derived_Type (Parent_Type)
6824 and then not Is_Package_Or_Generic_Package
6825 (Scope (Base_Type (Parent_Type)))
6827 if not Is_Constrained (Parent_Type)
6828 and then Is_Constrained (Derived_Type)
6830 New_Concatenation_Op (Implicit_Base);
6832 New_Concatenation_Op (Derived_Type);
6835 end Build_Derived_Array_Type;
6837 -----------------------------------
6838 -- Build_Derived_Concurrent_Type --
6839 -----------------------------------
6841 procedure Build_Derived_Concurrent_Type
6843 Parent_Type : Entity_Id;
6844 Derived_Type : Entity_Id)
6846 Loc : constant Source_Ptr := Sloc (N);
6848 Corr_Record : constant Entity_Id := Make_Temporary (Loc, 'C');
6849 Corr_Decl : Node_Id;
6850 Corr_Decl_Needed : Boolean;
6851 -- If the derived type has fewer discriminants than its parent, the
6852 -- corresponding record is also a derived type, in order to account for
6853 -- the bound discriminants. We create a full type declaration for it in
6856 Constraint_Present : constant Boolean :=
6857 Nkind (Subtype_Indication (Type_Definition (N))) =
6858 N_Subtype_Indication;
6860 D_Constraint : Node_Id;
6861 New_Constraint : Elist_Id := No_Elist;
6862 Old_Disc : Entity_Id;
6863 New_Disc : Entity_Id;
6867 Set_Stored_Constraint (Derived_Type, No_Elist);
6868 Corr_Decl_Needed := False;
6871 if Present (Discriminant_Specifications (N))
6872 and then Constraint_Present
6874 Old_Disc := First_Discriminant (Parent_Type);
6875 New_Disc := First (Discriminant_Specifications (N));
6876 while Present (New_Disc) and then Present (Old_Disc) loop
6877 Next_Discriminant (Old_Disc);
6882 if Present (Old_Disc) and then Expander_Active then
6884 -- The new type has fewer discriminants, so we need to create a new
6885 -- corresponding record, which is derived from the corresponding
6886 -- record of the parent, and has a stored constraint that captures
6887 -- the values of the discriminant constraints. The corresponding
6888 -- record is needed only if expander is active and code generation is
6891 -- The type declaration for the derived corresponding record has the
6892 -- same discriminant part and constraints as the current declaration.
6893 -- Copy the unanalyzed tree to build declaration.
6895 Corr_Decl_Needed := True;
6896 New_N := Copy_Separate_Tree (N);
6899 Make_Full_Type_Declaration (Loc,
6900 Defining_Identifier => Corr_Record,
6901 Discriminant_Specifications =>
6902 Discriminant_Specifications (New_N),
6904 Make_Derived_Type_Definition (Loc,
6905 Subtype_Indication =>
6906 Make_Subtype_Indication (Loc,
6909 (Corresponding_Record_Type (Parent_Type), Loc),
6912 (Subtype_Indication (Type_Definition (New_N))))));
6915 -- Copy Storage_Size and Relative_Deadline variables if task case
6917 if Is_Task_Type (Parent_Type) then
6918 Set_Storage_Size_Variable (Derived_Type,
6919 Storage_Size_Variable (Parent_Type));
6920 Set_Relative_Deadline_Variable (Derived_Type,
6921 Relative_Deadline_Variable (Parent_Type));
6924 if Present (Discriminant_Specifications (N)) then
6925 Push_Scope (Derived_Type);
6926 Check_Or_Process_Discriminants (N, Derived_Type);
6928 if Constraint_Present then
6930 Expand_To_Stored_Constraint
6932 Build_Discriminant_Constraints
6934 Subtype_Indication (Type_Definition (N)), True));
6939 elsif Constraint_Present then
6941 -- Build constrained subtype, copying the constraint, and derive
6942 -- from it to create a derived constrained type.
6945 Loc : constant Source_Ptr := Sloc (N);
6946 Anon : constant Entity_Id :=
6947 Make_Defining_Identifier (Loc,
6948 Chars => New_External_Name (Chars (Derived_Type), 'T'));
6953 Make_Subtype_Declaration (Loc,
6954 Defining_Identifier => Anon,
6955 Subtype_Indication =>
6956 New_Copy_Tree (Subtype_Indication (Type_Definition (N))));
6957 Insert_Before (N, Decl);
6960 Rewrite (Subtype_Indication (Type_Definition (N)),
6961 New_Occurrence_Of (Anon, Loc));
6962 Set_Analyzed (Derived_Type, False);
6968 -- By default, operations and private data are inherited from parent.
6969 -- However, in the presence of bound discriminants, a new corresponding
6970 -- record will be created, see below.
6972 Set_Has_Discriminants
6973 (Derived_Type, Has_Discriminants (Parent_Type));
6974 Set_Corresponding_Record_Type
6975 (Derived_Type, Corresponding_Record_Type (Parent_Type));
6977 -- Is_Constrained is set according the parent subtype, but is set to
6978 -- False if the derived type is declared with new discriminants.
6982 (Is_Constrained (Parent_Type) or else Constraint_Present)
6983 and then not Present (Discriminant_Specifications (N)));
6985 if Constraint_Present then
6986 if not Has_Discriminants (Parent_Type) then
6987 Error_Msg_N ("untagged parent must have discriminants", N);
6989 elsif Present (Discriminant_Specifications (N)) then
6991 -- Verify that new discriminants are used to constrain old ones
6996 (Constraint (Subtype_Indication (Type_Definition (N)))));
6998 Old_Disc := First_Discriminant (Parent_Type);
7000 while Present (D_Constraint) loop
7001 if Nkind (D_Constraint) /= N_Discriminant_Association then
7003 -- Positional constraint. If it is a reference to a new
7004 -- discriminant, it constrains the corresponding old one.
7006 if Nkind (D_Constraint) = N_Identifier then
7007 New_Disc := First_Discriminant (Derived_Type);
7008 while Present (New_Disc) loop
7009 exit when Chars (New_Disc) = Chars (D_Constraint);
7010 Next_Discriminant (New_Disc);
7013 if Present (New_Disc) then
7014 Set_Corresponding_Discriminant (New_Disc, Old_Disc);
7018 Next_Discriminant (Old_Disc);
7020 -- if this is a named constraint, search by name for the old
7021 -- discriminants constrained by the new one.
7023 elsif Nkind (Expression (D_Constraint)) = N_Identifier then
7025 -- Find new discriminant with that name
7027 New_Disc := First_Discriminant (Derived_Type);
7028 while Present (New_Disc) loop
7030 Chars (New_Disc) = Chars (Expression (D_Constraint));
7031 Next_Discriminant (New_Disc);
7034 if Present (New_Disc) then
7036 -- Verify that new discriminant renames some discriminant
7037 -- of the parent type, and associate the new discriminant
7038 -- with one or more old ones that it renames.
7044 Selector := First (Selector_Names (D_Constraint));
7045 while Present (Selector) loop
7046 Old_Disc := First_Discriminant (Parent_Type);
7047 while Present (Old_Disc) loop
7048 exit when Chars (Old_Disc) = Chars (Selector);
7049 Next_Discriminant (Old_Disc);
7052 if Present (Old_Disc) then
7053 Set_Corresponding_Discriminant
7054 (New_Disc, Old_Disc);
7063 Next (D_Constraint);
7066 New_Disc := First_Discriminant (Derived_Type);
7067 while Present (New_Disc) loop
7068 if No (Corresponding_Discriminant (New_Disc)) then
7070 ("new discriminant& must constrain old one", N, New_Disc);
7073 Subtypes_Statically_Compatible
7075 Etype (Corresponding_Discriminant (New_Disc)))
7078 ("& not statically compatible with parent discriminant",
7082 Next_Discriminant (New_Disc);
7086 elsif Present (Discriminant_Specifications (N)) then
7088 ("missing discriminant constraint in untagged derivation", N);
7091 -- The entity chain of the derived type includes the new discriminants
7092 -- but shares operations with the parent.
7094 if Present (Discriminant_Specifications (N)) then
7095 Old_Disc := First_Discriminant (Parent_Type);
7096 while Present (Old_Disc) loop
7097 if No (Next_Entity (Old_Disc))
7098 or else Ekind (Next_Entity (Old_Disc)) /= E_Discriminant
7101 (Last_Entity (Derived_Type), Next_Entity (Old_Disc));
7105 Next_Discriminant (Old_Disc);
7109 Set_First_Entity (Derived_Type, First_Entity (Parent_Type));
7110 if Has_Discriminants (Parent_Type) then
7111 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
7112 Set_Discriminant_Constraint (
7113 Derived_Type, Discriminant_Constraint (Parent_Type));
7117 Set_Last_Entity (Derived_Type, Last_Entity (Parent_Type));
7119 Set_Has_Completion (Derived_Type);
7121 if Corr_Decl_Needed then
7122 Set_Stored_Constraint (Derived_Type, New_Constraint);
7123 Insert_After (N, Corr_Decl);
7124 Analyze (Corr_Decl);
7125 Set_Corresponding_Record_Type (Derived_Type, Corr_Record);
7127 end Build_Derived_Concurrent_Type;
7129 ------------------------------------
7130 -- Build_Derived_Enumeration_Type --
7131 ------------------------------------
7133 procedure Build_Derived_Enumeration_Type
7135 Parent_Type : Entity_Id;
7136 Derived_Type : Entity_Id)
7138 Loc : constant Source_Ptr := Sloc (N);
7139 Def : constant Node_Id := Type_Definition (N);
7140 Indic : constant Node_Id := Subtype_Indication (Def);
7141 Implicit_Base : Entity_Id;
7142 Literal : Entity_Id;
7143 New_Lit : Entity_Id;
7144 Literals_List : List_Id;
7145 Type_Decl : Node_Id;
7147 Rang_Expr : Node_Id;
7150 -- Since types Standard.Character and Standard.[Wide_]Wide_Character do
7151 -- not have explicit literals lists we need to process types derived
7152 -- from them specially. This is handled by Derived_Standard_Character.
7153 -- If the parent type is a generic type, there are no literals either,
7154 -- and we construct the same skeletal representation as for the generic
7157 if Is_Standard_Character_Type (Parent_Type) then
7158 Derived_Standard_Character (N, Parent_Type, Derived_Type);
7160 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
7166 if Nkind (Indic) /= N_Subtype_Indication then
7168 Make_Attribute_Reference (Loc,
7169 Attribute_Name => Name_First,
7170 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7171 Set_Etype (Lo, Derived_Type);
7174 Make_Attribute_Reference (Loc,
7175 Attribute_Name => Name_Last,
7176 Prefix => New_Occurrence_Of (Derived_Type, Loc));
7177 Set_Etype (Hi, Derived_Type);
7179 Set_Scalar_Range (Derived_Type,
7185 -- Analyze subtype indication and verify compatibility
7186 -- with parent type.
7188 if Base_Type (Process_Subtype (Indic, N)) /=
7189 Base_Type (Parent_Type)
7192 ("illegal constraint for formal discrete type", N);
7198 -- If a constraint is present, analyze the bounds to catch
7199 -- premature usage of the derived literals.
7201 if Nkind (Indic) = N_Subtype_Indication
7202 and then Nkind (Range_Expression (Constraint (Indic))) = N_Range
7204 Analyze (Low_Bound (Range_Expression (Constraint (Indic))));
7205 Analyze (High_Bound (Range_Expression (Constraint (Indic))));
7208 -- Introduce an implicit base type for the derived type even if there
7209 -- is no constraint attached to it, since this seems closer to the
7210 -- Ada semantics. Build a full type declaration tree for the derived
7211 -- type using the implicit base type as the defining identifier. The
7212 -- build a subtype declaration tree which applies the constraint (if
7213 -- any) have it replace the derived type declaration.
7215 Literal := First_Literal (Parent_Type);
7216 Literals_List := New_List;
7217 while Present (Literal)
7218 and then Ekind (Literal) = E_Enumeration_Literal
7220 -- Literals of the derived type have the same representation as
7221 -- those of the parent type, but this representation can be
7222 -- overridden by an explicit representation clause. Indicate
7223 -- that there is no explicit representation given yet. These
7224 -- derived literals are implicit operations of the new type,
7225 -- and can be overridden by explicit ones.
7227 if Nkind (Literal) = N_Defining_Character_Literal then
7229 Make_Defining_Character_Literal (Loc, Chars (Literal));
7231 New_Lit := Make_Defining_Identifier (Loc, Chars (Literal));
7234 Set_Ekind (New_Lit, E_Enumeration_Literal);
7235 Set_Enumeration_Pos (New_Lit, Enumeration_Pos (Literal));
7236 Set_Enumeration_Rep (New_Lit, Enumeration_Rep (Literal));
7237 Set_Enumeration_Rep_Expr (New_Lit, Empty);
7238 Set_Alias (New_Lit, Literal);
7239 Set_Is_Known_Valid (New_Lit, True);
7241 Append (New_Lit, Literals_List);
7242 Next_Literal (Literal);
7246 Make_Defining_Identifier (Sloc (Derived_Type),
7247 Chars => New_External_Name (Chars (Derived_Type), 'B'));
7249 -- Indicate the proper nature of the derived type. This must be done
7250 -- before analysis of the literals, to recognize cases when a literal
7251 -- may be hidden by a previous explicit function definition (cf.
7254 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
7255 Set_Etype (Derived_Type, Implicit_Base);
7258 Make_Full_Type_Declaration (Loc,
7259 Defining_Identifier => Implicit_Base,
7260 Discriminant_Specifications => No_List,
7262 Make_Enumeration_Type_Definition (Loc, Literals_List));
7264 Mark_Rewrite_Insertion (Type_Decl);
7265 Insert_Before (N, Type_Decl);
7266 Analyze (Type_Decl);
7268 -- The anonymous base now has a full declaration, but this base
7269 -- is not a first subtype.
7271 Set_Is_First_Subtype (Implicit_Base, False);
7273 -- After the implicit base is analyzed its Etype needs to be changed
7274 -- to reflect the fact that it is derived from the parent type which
7275 -- was ignored during analysis. We also set the size at this point.
7277 Set_Etype (Implicit_Base, Parent_Type);
7279 Set_Size_Info (Implicit_Base, Parent_Type);
7280 Set_RM_Size (Implicit_Base, RM_Size (Parent_Type));
7281 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Parent_Type));
7283 -- Copy other flags from parent type
7285 Set_Has_Non_Standard_Rep
7286 (Implicit_Base, Has_Non_Standard_Rep
7288 Set_Has_Pragma_Ordered
7289 (Implicit_Base, Has_Pragma_Ordered
7291 Set_Has_Delayed_Freeze (Implicit_Base);
7293 -- Process the subtype indication including a validation check on the
7294 -- constraint, if any. If a constraint is given, its bounds must be
7295 -- implicitly converted to the new type.
7297 if Nkind (Indic) = N_Subtype_Indication then
7299 R : constant Node_Id :=
7300 Range_Expression (Constraint (Indic));
7303 if Nkind (R) = N_Range then
7304 Hi := Build_Scalar_Bound
7305 (High_Bound (R), Parent_Type, Implicit_Base);
7306 Lo := Build_Scalar_Bound
7307 (Low_Bound (R), Parent_Type, Implicit_Base);
7310 -- Constraint is a Range attribute. Replace with explicit
7311 -- mention of the bounds of the prefix, which must be a
7314 Analyze (Prefix (R));
7316 Convert_To (Implicit_Base,
7317 Make_Attribute_Reference (Loc,
7318 Attribute_Name => Name_Last,
7320 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7323 Convert_To (Implicit_Base,
7324 Make_Attribute_Reference (Loc,
7325 Attribute_Name => Name_First,
7327 New_Occurrence_Of (Entity (Prefix (R)), Loc)));
7334 (Type_High_Bound (Parent_Type),
7335 Parent_Type, Implicit_Base);
7338 (Type_Low_Bound (Parent_Type),
7339 Parent_Type, Implicit_Base);
7347 -- If we constructed a default range for the case where no range
7348 -- was given, then the expressions in the range must not freeze
7349 -- since they do not correspond to expressions in the source.
7350 -- However, if the type inherits predicates the expressions will
7351 -- be elaborated earlier and must freeze.
7353 if Nkind (Indic) /= N_Subtype_Indication
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, access and most enumeration types, derivation from
7678 -- the full view requires a fully-fledged declaration. In the other
7679 -- cases, just use an itype.
7681 if Ekind (Full_Parent) in Record_Kind
7682 or else Ekind (Full_Parent) in Access_Kind
7684 (Ekind (Full_Parent) in Enumeration_Kind
7685 and then not Is_Standard_Character_Type (Full_Parent)
7686 and then not Is_Generic_Type (Root_Type (Full_Parent)))
7688 -- Copy and adjust declaration to provide a completion for what
7689 -- is originally a private declaration. Indicate that full view
7690 -- is internally generated.
7692 Set_Comes_From_Source (Full_N, False);
7693 Set_Comes_From_Source (Full_Der, False);
7694 Set_Parent (Full_Der, Full_N);
7695 Set_Defining_Identifier (Full_N, Full_Der);
7697 -- If there are no constraints, adjust the subtype mark
7699 if Nkind (Subtype_Indication (Type_Definition (Full_N))) /=
7700 N_Subtype_Indication
7702 Set_Subtype_Indication
7703 (Type_Definition (Full_N),
7704 New_Occurrence_Of (Full_Parent, Sloc (Full_N)));
7707 Insert_After (N, Full_N);
7709 -- Build full view of derived type from full view of parent which
7710 -- is now installed. Subprograms have been derived on the partial
7711 -- view, the completion does not derive them anew.
7713 if Ekind (Full_Parent) in Record_Kind then
7715 -- If parent type is tagged, the completion inherits the proper
7716 -- primitive operations.
7718 if Is_Tagged_Type (Parent_Type) then
7719 Build_Derived_Record_Type
7720 (Full_N, Full_Parent, Full_Der, Derive_Subps);
7722 Build_Derived_Record_Type
7723 (Full_N, Full_Parent, Full_Der, Derive_Subps => False);
7728 (Full_N, Full_Parent, Full_Der,
7729 Is_Completion => False, Derive_Subps => False);
7732 -- The full declaration has been introduced into the tree and
7733 -- processed in the step above. It should not be analyzed again
7734 -- (when encountered later in the current list of declarations)
7735 -- to prevent spurious name conflicts. The full entity remains
7738 Set_Analyzed (Full_N);
7742 Make_Defining_Identifier (Sloc (Derived_Type),
7743 Chars => Chars (Derived_Type));
7744 Set_Is_Itype (Full_Der);
7745 Set_Associated_Node_For_Itype (Full_Der, N);
7746 Set_Parent (Full_Der, N);
7748 (N, Full_Parent, Full_Der,
7749 Is_Completion => False, Derive_Subps => False);
7752 Set_Has_Private_Declaration (Full_Der);
7753 Set_Has_Private_Declaration (Derived_Type);
7755 Set_Scope (Full_Der, Scope (Derived_Type));
7756 Set_Is_First_Subtype (Full_Der, Is_First_Subtype (Derived_Type));
7757 Set_Has_Size_Clause (Full_Der, False);
7758 Set_Has_Alignment_Clause (Full_Der, False);
7759 Set_Has_Delayed_Freeze (Full_Der);
7760 Set_Is_Frozen (Full_Der, False);
7761 Set_Freeze_Node (Full_Der, Empty);
7762 Set_Depends_On_Private (Full_Der, Has_Private_Component (Full_Der));
7763 Set_Is_Public (Full_Der, Is_Public (Derived_Type));
7765 -- The convention on the base type may be set in the private part
7766 -- and not propagated to the subtype until later, so we obtain the
7767 -- convention from the base type of the parent.
7769 Set_Convention (Full_Der, Convention (Base_Type (Full_Parent)));
7772 -- Start of processing for Build_Derived_Private_Type
7775 if Is_Tagged_Type (Parent_Type) then
7776 Full_P := Full_View (Parent_Type);
7778 -- A type extension of a type with unknown discriminants is an
7779 -- indefinite type that the back-end cannot handle directly.
7780 -- We treat it as a private type, and build a completion that is
7781 -- derived from the full view of the parent, and hopefully has
7782 -- known discriminants.
7784 -- If the full view of the parent type has an underlying record view,
7785 -- use it to generate the underlying record view of this derived type
7786 -- (required for chains of derivations with unknown discriminants).
7788 -- Minor optimization: we avoid the generation of useless underlying
7789 -- record view entities if the private type declaration has unknown
7790 -- discriminants but its corresponding full view has no
7793 if Has_Unknown_Discriminants (Parent_Type)
7794 and then Present (Full_P)
7795 and then (Has_Discriminants (Full_P)
7796 or else Present (Underlying_Record_View (Full_P)))
7797 and then not In_Open_Scopes (Par_Scope)
7798 and then Expander_Active
7801 Full_Der : constant Entity_Id := Make_Temporary (Loc, 'T');
7802 New_Ext : constant Node_Id :=
7804 (Record_Extension_Part (Type_Definition (N)));
7808 Build_Derived_Record_Type
7809 (N, Parent_Type, Derived_Type, Derive_Subps);
7811 -- Build anonymous completion, as a derivation from the full
7812 -- view of the parent. This is not a completion in the usual
7813 -- sense, because the current type is not private.
7816 Make_Full_Type_Declaration (Loc,
7817 Defining_Identifier => Full_Der,
7819 Make_Derived_Type_Definition (Loc,
7820 Subtype_Indication =>
7822 (Subtype_Indication (Type_Definition (N))),
7823 Record_Extension_Part => New_Ext));
7825 -- If the parent type has an underlying record view, use it
7826 -- here to build the new underlying record view.
7828 if Present (Underlying_Record_View (Full_P)) then
7830 (Nkind (Subtype_Indication (Type_Definition (Decl)))
7832 Set_Entity (Subtype_Indication (Type_Definition (Decl)),
7833 Underlying_Record_View (Full_P));
7836 Install_Private_Declarations (Par_Scope);
7837 Install_Visible_Declarations (Par_Scope);
7838 Insert_Before (N, Decl);
7840 -- Mark entity as an underlying record view before analysis,
7841 -- to avoid generating the list of its primitive operations
7842 -- (which is not really required for this entity) and thus
7843 -- prevent spurious errors associated with missing overriding
7844 -- of abstract primitives (overridden only for Derived_Type).
7846 Set_Ekind (Full_Der, E_Record_Type);
7847 Set_Is_Underlying_Record_View (Full_Der);
7848 Set_Default_SSO (Full_Der);
7849 Set_No_Reordering (Full_Der, No_Component_Reordering);
7853 pragma Assert (Has_Discriminants (Full_Der)
7854 and then not Has_Unknown_Discriminants (Full_Der));
7856 Uninstall_Declarations (Par_Scope);
7858 -- Freeze the underlying record view, to prevent generation of
7859 -- useless dispatching information, which is simply shared with
7860 -- the real derived type.
7862 Set_Is_Frozen (Full_Der);
7864 -- If the derived type has access discriminants, create
7865 -- references to their anonymous types now, to prevent
7866 -- back-end problems when their first use is in generated
7867 -- bodies of primitives.
7873 E := First_Entity (Full_Der);
7875 while Present (E) loop
7876 if Ekind (E) = E_Discriminant
7877 and then Ekind (Etype (E)) = E_Anonymous_Access_Type
7879 Build_Itype_Reference (Etype (E), Decl);
7886 -- Set up links between real entity and underlying record view
7888 Set_Underlying_Record_View (Derived_Type, Base_Type (Full_Der));
7889 Set_Underlying_Record_View (Base_Type (Full_Der), Derived_Type);
7892 -- If discriminants are known, build derived record
7895 Build_Derived_Record_Type
7896 (N, Parent_Type, Derived_Type, Derive_Subps);
7901 elsif Has_Discriminants (Parent_Type) then
7903 -- Build partial view of derived type from partial view of parent.
7904 -- This must be done before building the full derivation because the
7905 -- second derivation will modify the discriminants of the first and
7906 -- the discriminants are chained with the rest of the components in
7907 -- the full derivation.
7909 Build_Derived_Record_Type
7910 (N, Parent_Type, Derived_Type, Derive_Subps);
7912 -- Build the full derivation if this is not the anonymous derived
7913 -- base type created by Build_Derived_Record_Type in the constrained
7914 -- case (see point 5. of its head comment) since we build it for the
7915 -- derived subtype. And skip it for synchronized types altogether, as
7916 -- gigi does not use these types directly.
7918 if Present (Full_View (Parent_Type))
7919 and then not Is_Itype (Derived_Type)
7920 and then not Is_Concurrent_Type (Full_View (Parent_Type))
7923 Der_Base : constant Entity_Id := Base_Type (Derived_Type);
7925 Last_Discr : Entity_Id;
7928 -- If this is not a completion, construct the implicit full
7929 -- view by deriving from the full view of the parent type.
7930 -- But if this is a completion, the derived private type
7931 -- being built is a full view and the full derivation can
7932 -- only be its underlying full view.
7934 Build_Full_Derivation;
7936 if not Is_Completion then
7937 Set_Full_View (Derived_Type, Full_Der);
7939 Set_Underlying_Full_View (Derived_Type, Full_Der);
7940 Set_Is_Underlying_Full_View (Full_Der);
7943 if not Is_Base_Type (Derived_Type) then
7944 Set_Full_View (Der_Base, Base_Type (Full_Der));
7947 -- Copy the discriminant list from full view to the partial
7948 -- view (base type and its subtype). Gigi requires that the
7949 -- partial and full views have the same discriminants.
7951 -- Note that since the partial view points to discriminants
7952 -- in the full view, their scope will be that of the full
7953 -- view. This might cause some front end problems and need
7956 Discr := First_Discriminant (Base_Type (Full_Der));
7957 Set_First_Entity (Der_Base, Discr);
7960 Last_Discr := Discr;
7961 Next_Discriminant (Discr);
7962 exit when No (Discr);
7965 Set_Last_Entity (Der_Base, Last_Discr);
7966 Set_First_Entity (Derived_Type, First_Entity (Der_Base));
7967 Set_Last_Entity (Derived_Type, Last_Entity (Der_Base));
7971 elsif Present (Full_View (Parent_Type))
7972 and then Has_Discriminants (Full_View (Parent_Type))
7974 if Has_Unknown_Discriminants (Parent_Type)
7975 and then Nkind (Subtype_Indication (Type_Definition (N))) =
7976 N_Subtype_Indication
7979 ("cannot constrain type with unknown discriminants",
7980 Subtype_Indication (Type_Definition (N)));
7984 -- If this is not a completion, construct the implicit full view by
7985 -- deriving from the full view of the parent type. But if this is a
7986 -- completion, the derived private type being built is a full view
7987 -- and the full derivation can only be its underlying full view.
7989 Build_Full_Derivation;
7991 if not Is_Completion then
7992 Set_Full_View (Derived_Type, Full_Der);
7994 Set_Underlying_Full_View (Derived_Type, Full_Der);
7995 Set_Is_Underlying_Full_View (Full_Der);
7998 -- In any case, the primitive operations are inherited from the
7999 -- parent type, not from the internal full view.
8001 Set_Etype (Base_Type (Derived_Type), Base_Type (Parent_Type));
8003 if Derive_Subps then
8004 Derive_Subprograms (Parent_Type, Derived_Type);
8007 Set_Stored_Constraint (Derived_Type, No_Elist);
8009 (Derived_Type, Is_Constrained (Full_View (Parent_Type)));
8012 -- Untagged type, No discriminants on either view
8014 if Nkind (Subtype_Indication (Type_Definition (N))) =
8015 N_Subtype_Indication
8018 ("illegal constraint on type without discriminants", N);
8021 if Present (Discriminant_Specifications (N))
8022 and then Present (Full_View (Parent_Type))
8023 and then not Is_Tagged_Type (Full_View (Parent_Type))
8025 Error_Msg_N ("cannot add discriminants to untagged type", N);
8028 Set_Stored_Constraint (Derived_Type, No_Elist);
8029 Set_Is_Constrained (Derived_Type, Is_Constrained (Parent_Type));
8031 Set_Is_Controlled_Active
8032 (Derived_Type, Is_Controlled_Active (Parent_Type));
8034 Set_Disable_Controlled
8035 (Derived_Type, Disable_Controlled (Parent_Type));
8037 Set_Has_Controlled_Component
8038 (Derived_Type, Has_Controlled_Component (Parent_Type));
8040 -- Direct controlled types do not inherit Finalize_Storage_Only flag
8042 if not Is_Controlled (Parent_Type) then
8043 Set_Finalize_Storage_Only
8044 (Base_Type (Derived_Type), Finalize_Storage_Only (Parent_Type));
8047 -- If this is not a completion, construct the implicit full view by
8048 -- deriving from the full view of the parent type.
8050 -- ??? If the parent is untagged private and its completion is
8051 -- tagged, this mechanism will not work because we cannot derive from
8052 -- the tagged full view unless we have an extension.
8054 if Present (Full_View (Parent_Type))
8055 and then not Is_Tagged_Type (Full_View (Parent_Type))
8056 and then not Is_Completion
8058 Build_Full_Derivation;
8059 Set_Full_View (Derived_Type, Full_Der);
8063 Set_Has_Unknown_Discriminants (Derived_Type,
8064 Has_Unknown_Discriminants (Parent_Type));
8066 if Is_Private_Type (Derived_Type) then
8067 Set_Private_Dependents (Derived_Type, New_Elmt_List);
8070 -- If the parent base type is in scope, add the derived type to its
8071 -- list of private dependents, because its full view may become
8072 -- visible subsequently (in a nested private part, a body, or in a
8073 -- further child unit).
8075 if Is_Private_Type (Par_Base) and then In_Open_Scopes (Par_Scope) then
8076 Append_Elmt (Derived_Type, Private_Dependents (Parent_Type));
8078 -- Check for unusual case where a type completed by a private
8079 -- derivation occurs within a package nested in a child unit, and
8080 -- the parent is declared in an ancestor.
8082 if Is_Child_Unit (Scope (Current_Scope))
8083 and then Is_Completion
8084 and then In_Private_Part (Current_Scope)
8085 and then Scope (Parent_Type) /= Current_Scope
8087 -- Note that if the parent has a completion in the private part,
8088 -- (which is itself a derivation from some other private type)
8089 -- it is that completion that is visible, there is no full view
8090 -- available, and no special processing is needed.
8092 and then Present (Full_View (Parent_Type))
8094 -- In this case, the full view of the parent type will become
8095 -- visible in the body of the enclosing child, and only then will
8096 -- the current type be possibly non-private. Build an underlying
8097 -- full view that will be installed when the enclosing child body
8100 if Present (Underlying_Full_View (Derived_Type)) then
8101 Full_Der := Underlying_Full_View (Derived_Type);
8103 Build_Full_Derivation;
8104 Set_Underlying_Full_View (Derived_Type, Full_Der);
8105 Set_Is_Underlying_Full_View (Full_Der);
8108 -- The full view will be used to swap entities on entry/exit to
8109 -- the body, and must appear in the entity list for the package.
8111 Append_Entity (Full_Der, Scope (Derived_Type));
8114 end Build_Derived_Private_Type;
8116 -------------------------------
8117 -- Build_Derived_Record_Type --
8118 -------------------------------
8122 -- Ideally we would like to use the same model of type derivation for
8123 -- tagged and untagged record types. Unfortunately this is not quite
8124 -- possible because the semantics of representation clauses is different
8125 -- for tagged and untagged records under inheritance. Consider the
8128 -- type R (...) is [tagged] record ... end record;
8129 -- type T (...) is new R (...) [with ...];
8131 -- The representation clauses for T can specify a completely different
8132 -- record layout from R's. Hence the same component can be placed in two
8133 -- very different positions in objects of type T and R. If R and T are
8134 -- tagged types, representation clauses for T can only specify the layout
8135 -- of non inherited components, thus components that are common in R and T
8136 -- have the same position in objects of type R and T.
8138 -- This has two implications. The first is that the entire tree for R's
8139 -- declaration needs to be copied for T in the untagged case, so that T
8140 -- can be viewed as a record type of its own with its own representation
8141 -- clauses. The second implication is the way we handle discriminants.
8142 -- Specifically, in the untagged case we need a way to communicate to Gigi
8143 -- what are the real discriminants in the record, while for the semantics
8144 -- we need to consider those introduced by the user to rename the
8145 -- discriminants in the parent type. This is handled by introducing the
8146 -- notion of stored discriminants. See below for more.
8148 -- Fortunately the way regular components are inherited can be handled in
8149 -- the same way in tagged and untagged types.
8151 -- To complicate things a bit more the private view of a private extension
8152 -- cannot be handled in the same way as the full view (for one thing the
8153 -- semantic rules are somewhat different). We will explain what differs
8156 -- 2. DISCRIMINANTS UNDER INHERITANCE
8158 -- The semantic rules governing the discriminants of derived types are
8161 -- type Derived_Type_Name [KNOWN_DISCRIMINANT_PART] is new
8162 -- [abstract] Parent_Type_Name [CONSTRAINT] [RECORD_EXTENSION_PART]
8164 -- If parent type has discriminants, then the discriminants that are
8165 -- declared in the derived type are [3.4 (11)]:
8167 -- o The discriminants specified by a new KNOWN_DISCRIMINANT_PART, if
8170 -- o Otherwise, each discriminant of the parent type (implicitly declared
8171 -- in the same order with the same specifications). In this case, the
8172 -- discriminants are said to be "inherited", or if unknown in the parent
8173 -- are also unknown in the derived type.
8175 -- Furthermore if a KNOWN_DISCRIMINANT_PART is provided, then [3.7(13-18)]:
8177 -- o The parent subtype must be constrained;
8179 -- o If the parent type is not a tagged type, then each discriminant of
8180 -- the derived type must be used in the constraint defining a parent
8181 -- subtype. [Implementation note: This ensures that the new discriminant
8182 -- can share storage with an existing discriminant.]
8184 -- For the derived type each discriminant of the parent type is either
8185 -- inherited, constrained to equal some new discriminant of the derived
8186 -- type, or constrained to the value of an expression.
8188 -- When inherited or constrained to equal some new discriminant, the
8189 -- parent discriminant and the discriminant of the derived type are said
8192 -- If a discriminant of the parent type is constrained to a specific value
8193 -- in the derived type definition, then the discriminant is said to be
8194 -- "specified" by that derived type definition.
8196 -- 3. DISCRIMINANTS IN DERIVED UNTAGGED RECORD TYPES
8198 -- We have spoken about stored discriminants in point 1 (introduction)
8199 -- above. There are two sorts of stored discriminants: implicit and
8200 -- explicit. As long as the derived type inherits the same discriminants as
8201 -- the root record type, stored discriminants are the same as regular
8202 -- discriminants, and are said to be implicit. However, if any discriminant
8203 -- in the root type was renamed in the derived type, then the derived
8204 -- type will contain explicit stored discriminants. Explicit stored
8205 -- discriminants are discriminants in addition to the semantically visible
8206 -- discriminants defined for the derived type. Stored discriminants are
8207 -- used by Gigi to figure out what are the physical discriminants in
8208 -- objects of the derived type (see precise definition in einfo.ads).
8209 -- As an example, consider the following:
8211 -- type R (D1, D2, D3 : Int) is record ... end record;
8212 -- type T1 is new R;
8213 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1);
8214 -- type T3 is new T2;
8215 -- type T4 (Y : Int) is new T3 (Y, 99);
8217 -- The following table summarizes the discriminants and stored
8218 -- discriminants in R and T1 through T4:
8220 -- Type Discrim Stored Discrim Comment
8221 -- R (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in R
8222 -- T1 (D1, D2, D3) (D1, D2, D3) Girder discrims implicit in T1
8223 -- T2 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T2
8224 -- T3 (X1, X2) (D1, D2, D3) Girder discrims EXPLICIT in T3
8225 -- T4 (Y) (D1, D2, D3) Girder discrims EXPLICIT in T4
8227 -- Field Corresponding_Discriminant (abbreviated CD below) allows us to
8228 -- find the corresponding discriminant in the parent type, while
8229 -- Original_Record_Component (abbreviated ORC below) the actual physical
8230 -- component that is renamed. Finally the field Is_Completely_Hidden
8231 -- (abbreviated ICH below) is set for all explicit stored discriminants
8232 -- (see einfo.ads for more info). For the above example this gives:
8234 -- Discrim CD ORC ICH
8235 -- ^^^^^^^ ^^ ^^^ ^^^
8236 -- D1 in R empty itself no
8237 -- D2 in R empty itself no
8238 -- D3 in R empty itself no
8240 -- D1 in T1 D1 in R itself no
8241 -- D2 in T1 D2 in R itself no
8242 -- D3 in T1 D3 in R itself no
8244 -- X1 in T2 D3 in T1 D3 in T2 no
8245 -- X2 in T2 D1 in T1 D1 in T2 no
8246 -- D1 in T2 empty itself yes
8247 -- D2 in T2 empty itself yes
8248 -- D3 in T2 empty itself yes
8250 -- X1 in T3 X1 in T2 D3 in T3 no
8251 -- X2 in T3 X2 in T2 D1 in T3 no
8252 -- D1 in T3 empty itself yes
8253 -- D2 in T3 empty itself yes
8254 -- D3 in T3 empty itself yes
8256 -- Y in T4 X1 in T3 D3 in T4 no
8257 -- D1 in T4 empty itself yes
8258 -- D2 in T4 empty itself yes
8259 -- D3 in T4 empty itself yes
8261 -- 4. DISCRIMINANTS IN DERIVED TAGGED RECORD TYPES
8263 -- Type derivation for tagged types is fairly straightforward. If no
8264 -- discriminants are specified by the derived type, these are inherited
8265 -- from the parent. No explicit stored discriminants are ever necessary.
8266 -- The only manipulation that is done to the tree is that of adding a
8267 -- _parent field with parent type and constrained to the same constraint
8268 -- specified for the parent in the derived type definition. For instance:
8270 -- type R (D1, D2, D3 : Int) is tagged record ... end record;
8271 -- type T1 is new R with null record;
8272 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with null record;
8274 -- are changed into:
8276 -- type T1 (D1, D2, D3 : Int) is new R (D1, D2, D3) with record
8277 -- _parent : R (D1, D2, D3);
8280 -- type T2 (X1, X2: Int) is new T1 (X2, 88, X1) with record
8281 -- _parent : T1 (X2, 88, X1);
8284 -- The discriminants actually present in R, T1 and T2 as well as their CD,
8285 -- ORC and ICH fields are:
8287 -- Discrim CD ORC ICH
8288 -- ^^^^^^^ ^^ ^^^ ^^^
8289 -- D1 in R empty itself no
8290 -- D2 in R empty itself no
8291 -- D3 in R empty itself no
8293 -- D1 in T1 D1 in R D1 in R no
8294 -- D2 in T1 D2 in R D2 in R no
8295 -- D3 in T1 D3 in R D3 in R no
8297 -- X1 in T2 D3 in T1 D3 in R no
8298 -- X2 in T2 D1 in T1 D1 in R no
8300 -- 5. FIRST TRANSFORMATION FOR DERIVED RECORDS
8302 -- Regardless of whether we dealing with a tagged or untagged type
8303 -- we will transform all derived type declarations of the form
8305 -- type T is new R (...) [with ...];
8307 -- subtype S is R (...);
8308 -- type T is new S [with ...];
8310 -- type BT is new R [with ...];
8311 -- subtype T is BT (...);
8313 -- That is, the base derived type is constrained only if it has no
8314 -- discriminants. The reason for doing this is that GNAT's semantic model
8315 -- assumes that a base type with discriminants is unconstrained.
8317 -- Note that, strictly speaking, the above transformation is not always
8318 -- correct. Consider for instance the following excerpt from ACVC b34011a:
8320 -- procedure B34011A is
8321 -- type REC (D : integer := 0) is record
8326 -- type T6 is new Rec;
8327 -- function F return T6;
8332 -- type U is new T6 (Q6.F.I); -- ERROR: Q6.F.
8335 -- The definition of Q6.U is illegal. However transforming Q6.U into
8337 -- type BaseU is new T6;
8338 -- subtype U is BaseU (Q6.F.I)
8340 -- turns U into a legal subtype, which is incorrect. To avoid this problem
8341 -- we always analyze the constraint (in this case (Q6.F.I)) before applying
8342 -- the transformation described above.
8344 -- There is another instance where the above transformation is incorrect.
8348 -- type Base (D : Integer) is tagged null record;
8349 -- procedure P (X : Base);
8351 -- type Der is new Base (2) with null record;
8352 -- procedure P (X : Der);
8355 -- Then the above transformation turns this into
8357 -- type Der_Base is new Base with null record;
8358 -- -- procedure P (X : Base) is implicitly inherited here
8359 -- -- as procedure P (X : Der_Base).
8361 -- subtype Der is Der_Base (2);
8362 -- procedure P (X : Der);
8363 -- -- The overriding of P (X : Der_Base) is illegal since we
8364 -- -- have a parameter conformance problem.
8366 -- To get around this problem, after having semantically processed Der_Base
8367 -- and the rewritten subtype declaration for Der, we copy Der_Base field
8368 -- Discriminant_Constraint from Der so that when parameter conformance is
8369 -- checked when P is overridden, no semantic errors are flagged.
8371 -- 6. SECOND TRANSFORMATION FOR DERIVED RECORDS
8373 -- Regardless of whether we are dealing with a tagged or untagged type
8374 -- we will transform all derived type declarations of the form
8376 -- type R (D1, .., Dn : ...) is [tagged] record ...;
8377 -- type T is new R [with ...];
8379 -- type T (D1, .., Dn : ...) is new R (D1, .., Dn) [with ...];
8381 -- The reason for such transformation is that it allows us to implement a
8382 -- very clean form of component inheritance as explained below.
8384 -- Note that this transformation is not achieved by direct tree rewriting
8385 -- and manipulation, but rather by redoing the semantic actions that the
8386 -- above transformation will entail. This is done directly in routine
8387 -- Inherit_Components.
8389 -- 7. TYPE DERIVATION AND COMPONENT INHERITANCE
8391 -- In both tagged and untagged derived types, regular non discriminant
8392 -- components are inherited in the derived type from the parent type. In
8393 -- the absence of discriminants component, inheritance is straightforward
8394 -- as components can simply be copied from the parent.
8396 -- If the parent has discriminants, inheriting components constrained with
8397 -- these discriminants requires caution. Consider the following example:
8399 -- type R (D1, D2 : Positive) is [tagged] record
8400 -- S : String (D1 .. D2);
8403 -- type T1 is new R [with null record];
8404 -- type T2 (X : positive) is new R (1, X) [with null record];
8406 -- As explained in 6. above, T1 is rewritten as
8407 -- type T1 (D1, D2 : Positive) is new R (D1, D2) [with null record];
8408 -- which makes the treatment for T1 and T2 identical.
8410 -- What we want when inheriting S, is that references to D1 and D2 in R are
8411 -- replaced with references to their correct constraints, i.e. D1 and D2 in
8412 -- T1 and 1 and X in T2. So all R's discriminant references are replaced
8413 -- with either discriminant references in the derived type or expressions.
8414 -- This replacement is achieved as follows: before inheriting R's
8415 -- components, a subtype R (D1, D2) for T1 (resp. R (1, X) for T2) is
8416 -- created in the scope of T1 (resp. scope of T2) so that discriminants D1
8417 -- and D2 of T1 are visible (resp. discriminant X of T2 is visible).
8418 -- For T2, for instance, this has the effect of replacing String (D1 .. D2)
8419 -- by String (1 .. X).
8421 -- 8. TYPE DERIVATION IN PRIVATE TYPE EXTENSIONS
8423 -- We explain here the rules governing private type extensions relevant to
8424 -- type derivation. These rules are explained on the following example:
8426 -- type D [(...)] is new A [(...)] with private; <-- partial view
8427 -- type D [(...)] is new P [(...)] with null record; <-- full view
8429 -- Type A is called the ancestor subtype of the private extension.
8430 -- Type P is the parent type of the full view of the private extension. It
8431 -- must be A or a type derived from A.
8433 -- The rules concerning the discriminants of private type extensions are
8436 -- o If a private extension inherits known discriminants from the ancestor
8437 -- subtype, then the full view must also inherit its discriminants from
8438 -- the ancestor subtype and the parent subtype of the full view must be
8439 -- constrained if and only if the ancestor subtype is constrained.
8441 -- o If a partial view has unknown discriminants, then the full view may
8442 -- define a definite or an indefinite subtype, with or without
8445 -- o If a partial view has neither known nor unknown discriminants, then
8446 -- the full view must define a definite subtype.
8448 -- o If the ancestor subtype of a private extension has constrained
8449 -- discriminants, then the parent subtype of the full view must impose a
8450 -- statically matching constraint on those discriminants.
8452 -- This means that only the following forms of private extensions are
8455 -- type D is new A with private; <-- partial view
8456 -- type D is new P with null record; <-- full view
8458 -- If A has no discriminants than P has no discriminants, otherwise P must
8459 -- inherit A's discriminants.
8461 -- type D is new A (...) with private; <-- partial view
8462 -- type D is new P (:::) with null record; <-- full view
8464 -- P must inherit A's discriminants and (...) and (:::) must statically
8467 -- subtype A is R (...);
8468 -- type D is new A with private; <-- partial view
8469 -- type D is new P with null record; <-- full view
8471 -- P must have inherited R's discriminants and must be derived from A or
8472 -- any of its subtypes.
8474 -- type D (..) is new A with private; <-- partial view
8475 -- type D (..) is new P [(:::)] with null record; <-- full view
8477 -- No specific constraints on P's discriminants or constraint (:::).
8478 -- Note that A can be unconstrained, but the parent subtype P must either
8479 -- be constrained or (:::) must be present.
8481 -- type D (..) is new A [(...)] with private; <-- partial view
8482 -- type D (..) is new P [(:::)] with null record; <-- full view
8484 -- P's constraints on A's discriminants must statically match those
8485 -- imposed by (...).
8487 -- 9. IMPLEMENTATION OF TYPE DERIVATION FOR PRIVATE EXTENSIONS
8489 -- The full view of a private extension is handled exactly as described
8490 -- above. The model chose for the private view of a private extension is
8491 -- the same for what concerns discriminants (i.e. they receive the same
8492 -- treatment as in the tagged case). However, the private view of the
8493 -- private extension always inherits the components of the parent base,
8494 -- without replacing any discriminant reference. Strictly speaking this is
8495 -- incorrect. However, Gigi never uses this view to generate code so this
8496 -- is a purely semantic issue. In theory, a set of transformations similar
8497 -- to those given in 5. and 6. above could be applied to private views of
8498 -- private extensions to have the same model of component inheritance as
8499 -- for non private extensions. However, this is not done because it would
8500 -- further complicate private type processing. Semantically speaking, this
8501 -- leaves us in an uncomfortable situation. As an example consider:
8504 -- type R (D : integer) is tagged record
8505 -- S : String (1 .. D);
8507 -- procedure P (X : R);
8508 -- type T is new R (1) with private;
8510 -- type T is new R (1) with null record;
8513 -- This is transformed into:
8516 -- type R (D : integer) is tagged record
8517 -- S : String (1 .. D);
8519 -- procedure P (X : R);
8520 -- type T is new R (1) with private;
8522 -- type BaseT is new R with null record;
8523 -- subtype T is BaseT (1);
8526 -- (strictly speaking the above is incorrect Ada)
8528 -- From the semantic standpoint the private view of private extension T
8529 -- should be flagged as constrained since one can clearly have
8533 -- in a unit withing Pack. However, when deriving subprograms for the
8534 -- private view of private extension T, T must be seen as unconstrained
8535 -- since T has discriminants (this is a constraint of the current
8536 -- subprogram derivation model). Thus, when processing the private view of
8537 -- a private extension such as T, we first mark T as unconstrained, we
8538 -- process it, we perform program derivation and just before returning from
8539 -- Build_Derived_Record_Type we mark T as constrained.
8541 -- ??? Are there are other uncomfortable cases that we will have to
8544 -- 10. RECORD_TYPE_WITH_PRIVATE complications
8546 -- Types that are derived from a visible record type and have a private
8547 -- extension present other peculiarities. They behave mostly like private
8548 -- types, but if they have primitive operations defined, these will not
8549 -- have the proper signatures for further inheritance, because other
8550 -- primitive operations will use the implicit base that we define for
8551 -- private derivations below. This affect subprogram inheritance (see
8552 -- Derive_Subprograms for details). We also derive the implicit base from
8553 -- the base type of the full view, so that the implicit base is a record
8554 -- type and not another private type, This avoids infinite loops.
8556 procedure Build_Derived_Record_Type
8558 Parent_Type : Entity_Id;
8559 Derived_Type : Entity_Id;
8560 Derive_Subps : Boolean := True)
8562 Discriminant_Specs : constant Boolean :=
8563 Present (Discriminant_Specifications (N));
8564 Is_Tagged : constant Boolean := Is_Tagged_Type (Parent_Type);
8565 Loc : constant Source_Ptr := Sloc (N);
8566 Private_Extension : constant Boolean :=
8567 Nkind (N) = N_Private_Extension_Declaration;
8568 Assoc_List : Elist_Id;
8569 Constraint_Present : Boolean;
8571 Discrim : Entity_Id;
8573 Inherit_Discrims : Boolean := False;
8574 Last_Discrim : Entity_Id;
8575 New_Base : Entity_Id;
8577 New_Discrs : Elist_Id;
8578 New_Indic : Node_Id;
8579 Parent_Base : Entity_Id;
8580 Save_Etype : Entity_Id;
8581 Save_Discr_Constr : Elist_Id;
8582 Save_Next_Entity : Entity_Id;
8585 Discs : Elist_Id := New_Elmt_List;
8586 -- An empty Discs list means that there were no constraints in the
8587 -- subtype indication or that there was an error processing it.
8590 if Ekind (Parent_Type) = E_Record_Type_With_Private
8591 and then Present (Full_View (Parent_Type))
8592 and then Has_Discriminants (Parent_Type)
8594 Parent_Base := Base_Type (Full_View (Parent_Type));
8596 Parent_Base := Base_Type (Parent_Type);
8599 -- If the parent type is declared as a subtype of another private
8600 -- type with inherited discriminants, its generated base type is
8601 -- itself a record subtype. To further inherit the constraint we
8602 -- need to use its own base to have an unconstrained type on which
8603 -- to apply the inherited constraint.
8605 if Ekind (Parent_Base) = E_Record_Subtype then
8606 Parent_Base := Base_Type (Parent_Base);
8609 -- AI05-0115: if this is a derivation from a private type in some
8610 -- other scope that may lead to invisible components for the derived
8611 -- type, mark it accordingly.
8613 if Is_Private_Type (Parent_Type) then
8614 if Scope (Parent_Base) = Scope (Derived_Type) then
8617 elsif In_Open_Scopes (Scope (Parent_Base))
8618 and then In_Private_Part (Scope (Parent_Base))
8623 Set_Has_Private_Ancestor (Derived_Type);
8627 Set_Has_Private_Ancestor
8628 (Derived_Type, Has_Private_Ancestor (Parent_Type));
8631 -- Before we start the previously documented transformations, here is
8632 -- little fix for size and alignment of tagged types. Normally when we
8633 -- derive type D from type P, we copy the size and alignment of P as the
8634 -- default for D, and in the absence of explicit representation clauses
8635 -- for D, the size and alignment are indeed the same as the parent.
8637 -- But this is wrong for tagged types, since fields may be added, and
8638 -- the default size may need to be larger, and the default alignment may
8639 -- need to be larger.
8641 -- We therefore reset the size and alignment fields in the tagged case.
8642 -- Note that the size and alignment will in any case be at least as
8643 -- large as the parent type (since the derived type has a copy of the
8644 -- parent type in the _parent field)
8646 -- The type is also marked as being tagged here, which is needed when
8647 -- processing components with a self-referential anonymous access type
8648 -- in the call to Check_Anonymous_Access_Components below. Note that
8649 -- this flag is also set later on for completeness.
8652 Set_Is_Tagged_Type (Derived_Type);
8653 Init_Size_Align (Derived_Type);
8656 -- STEP 0a: figure out what kind of derived type declaration we have
8658 if Private_Extension then
8660 Set_Ekind (Derived_Type, E_Record_Type_With_Private);
8661 Set_Default_SSO (Derived_Type);
8662 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8665 Type_Def := Type_Definition (N);
8667 -- Ekind (Parent_Base) is not necessarily E_Record_Type since
8668 -- Parent_Base can be a private type or private extension. However,
8669 -- for tagged types with an extension the newly added fields are
8670 -- visible and hence the Derived_Type is always an E_Record_Type.
8671 -- (except that the parent may have its own private fields).
8672 -- For untagged types we preserve the Ekind of the Parent_Base.
8674 if Present (Record_Extension_Part (Type_Def)) then
8675 Set_Ekind (Derived_Type, E_Record_Type);
8676 Set_Default_SSO (Derived_Type);
8677 Set_No_Reordering (Derived_Type, No_Component_Reordering);
8679 -- Create internal access types for components with anonymous
8682 if Ada_Version >= Ada_2005 then
8683 Check_Anonymous_Access_Components
8684 (N, Derived_Type, Derived_Type,
8685 Component_List (Record_Extension_Part (Type_Def)));
8689 Set_Ekind (Derived_Type, Ekind (Parent_Base));
8693 -- Indic can either be an N_Identifier if the subtype indication
8694 -- contains no constraint or an N_Subtype_Indication if the subtype
8695 -- indication has a constraint.
8697 Indic := Subtype_Indication (Type_Def);
8698 Constraint_Present := (Nkind (Indic) = N_Subtype_Indication);
8700 -- Check that the type has visible discriminants. The type may be
8701 -- a private type with unknown discriminants whose full view has
8702 -- discriminants which are invisible.
8704 if Constraint_Present then
8705 if not Has_Discriminants (Parent_Base)
8707 (Has_Unknown_Discriminants (Parent_Base)
8708 and then Is_Private_Type (Parent_Base))
8711 ("invalid constraint: type has no discriminant",
8712 Constraint (Indic));
8714 Constraint_Present := False;
8715 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8717 elsif Is_Constrained (Parent_Type) then
8719 ("invalid constraint: parent type is already constrained",
8720 Constraint (Indic));
8722 Constraint_Present := False;
8723 Rewrite (Indic, New_Copy_Tree (Subtype_Mark (Indic)));
8727 -- STEP 0b: If needed, apply transformation given in point 5. above
8729 if not Private_Extension
8730 and then Has_Discriminants (Parent_Type)
8731 and then not Discriminant_Specs
8732 and then (Is_Constrained (Parent_Type) or else Constraint_Present)
8734 -- First, we must analyze the constraint (see comment in point 5.)
8735 -- The constraint may come from the subtype indication of the full
8738 if Constraint_Present then
8739 New_Discrs := Build_Discriminant_Constraints (Parent_Type, Indic);
8741 -- If there is no explicit constraint, there might be one that is
8742 -- inherited from a constrained parent type. In that case verify that
8743 -- it conforms to the constraint in the partial view. In perverse
8744 -- cases the parent subtypes of the partial and full view can have
8745 -- different constraints.
8747 elsif Present (Stored_Constraint (Parent_Type)) then
8748 New_Discrs := Stored_Constraint (Parent_Type);
8751 New_Discrs := No_Elist;
8754 if Has_Discriminants (Derived_Type)
8755 and then Has_Private_Declaration (Derived_Type)
8756 and then Present (Discriminant_Constraint (Derived_Type))
8757 and then Present (New_Discrs)
8759 -- Verify that constraints of the full view statically match
8760 -- those given in the partial view.
8766 C1 := First_Elmt (New_Discrs);
8767 C2 := First_Elmt (Discriminant_Constraint (Derived_Type));
8768 while Present (C1) and then Present (C2) loop
8769 if Fully_Conformant_Expressions (Node (C1), Node (C2))
8771 (Is_OK_Static_Expression (Node (C1))
8772 and then Is_OK_Static_Expression (Node (C2))
8774 Expr_Value (Node (C1)) = Expr_Value (Node (C2)))
8779 if Constraint_Present then
8781 ("constraint not conformant to previous declaration",
8785 ("constraint of full view is incompatible "
8786 & "with partial view", N);
8796 -- Insert and analyze the declaration for the unconstrained base type
8798 New_Base := Create_Itype (Ekind (Derived_Type), N, Derived_Type, 'B');
8801 Make_Full_Type_Declaration (Loc,
8802 Defining_Identifier => New_Base,
8804 Make_Derived_Type_Definition (Loc,
8805 Abstract_Present => Abstract_Present (Type_Def),
8806 Limited_Present => Limited_Present (Type_Def),
8807 Subtype_Indication =>
8808 New_Occurrence_Of (Parent_Base, Loc),
8809 Record_Extension_Part =>
8810 Relocate_Node (Record_Extension_Part (Type_Def)),
8811 Interface_List => Interface_List (Type_Def)));
8813 Set_Parent (New_Decl, Parent (N));
8814 Mark_Rewrite_Insertion (New_Decl);
8815 Insert_Before (N, New_Decl);
8817 -- In the extension case, make sure ancestor is frozen appropriately
8818 -- (see also non-discriminated case below).
8820 if Present (Record_Extension_Part (Type_Def))
8821 or else Is_Interface (Parent_Base)
8823 Freeze_Before (New_Decl, Parent_Type);
8826 -- Note that this call passes False for the Derive_Subps parameter
8827 -- because subprogram derivation is deferred until after creating
8828 -- the subtype (see below).
8831 (New_Decl, Parent_Base, New_Base,
8832 Is_Completion => False, Derive_Subps => False);
8834 -- ??? This needs re-examination to determine whether the
8835 -- above call can simply be replaced by a call to Analyze.
8837 Set_Analyzed (New_Decl);
8839 -- Insert and analyze the declaration for the constrained subtype
8841 if Constraint_Present then
8843 Make_Subtype_Indication (Loc,
8844 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8845 Constraint => Relocate_Node (Constraint (Indic)));
8849 Constr_List : constant List_Id := New_List;
8854 C := First_Elmt (Discriminant_Constraint (Parent_Type));
8855 while Present (C) loop
8858 -- It is safe here to call New_Copy_Tree since we called
8859 -- Force_Evaluation on each constraint previously
8860 -- in Build_Discriminant_Constraints.
8862 Append (New_Copy_Tree (Expr), To => Constr_List);
8868 Make_Subtype_Indication (Loc,
8869 Subtype_Mark => New_Occurrence_Of (New_Base, Loc),
8871 Make_Index_Or_Discriminant_Constraint (Loc, Constr_List));
8876 Make_Subtype_Declaration (Loc,
8877 Defining_Identifier => Derived_Type,
8878 Subtype_Indication => New_Indic));
8882 -- Derivation of subprograms must be delayed until the full subtype
8883 -- has been established, to ensure proper overriding of subprograms
8884 -- inherited by full types. If the derivations occurred as part of
8885 -- the call to Build_Derived_Type above, then the check for type
8886 -- conformance would fail because earlier primitive subprograms
8887 -- could still refer to the full type prior the change to the new
8888 -- subtype and hence would not match the new base type created here.
8889 -- Subprograms are not derived, however, when Derive_Subps is False
8890 -- (since otherwise there could be redundant derivations).
8892 if Derive_Subps then
8893 Derive_Subprograms (Parent_Type, Derived_Type);
8896 -- For tagged types the Discriminant_Constraint of the new base itype
8897 -- is inherited from the first subtype so that no subtype conformance
8898 -- problem arise when the first subtype overrides primitive
8899 -- operations inherited by the implicit base type.
8902 Set_Discriminant_Constraint
8903 (New_Base, Discriminant_Constraint (Derived_Type));
8909 -- If we get here Derived_Type will have no discriminants or it will be
8910 -- a discriminated unconstrained base type.
8912 -- STEP 1a: perform preliminary actions/checks for derived tagged types
8916 -- The parent type is frozen for non-private extensions (RM 13.14(7))
8917 -- The declaration of a specific descendant of an interface type
8918 -- freezes the interface type (RM 13.14).
8920 if not Private_Extension or else Is_Interface (Parent_Base) then
8921 Freeze_Before (N, Parent_Type);
8924 -- In Ada 2005 (AI-344), the restriction that a derived tagged type
8925 -- cannot be declared at a deeper level than its parent type is
8926 -- removed. The check on derivation within a generic body is also
8927 -- relaxed, but there's a restriction that a derived tagged type
8928 -- cannot be declared in a generic body if it's derived directly
8929 -- or indirectly from a formal type of that generic.
8931 if Ada_Version >= Ada_2005 then
8932 if Present (Enclosing_Generic_Body (Derived_Type)) then
8934 Ancestor_Type : Entity_Id;
8937 -- Check to see if any ancestor of the derived type is a
8940 Ancestor_Type := Parent_Type;
8941 while not Is_Generic_Type (Ancestor_Type)
8942 and then Etype (Ancestor_Type) /= Ancestor_Type
8944 Ancestor_Type := Etype (Ancestor_Type);
8947 -- If the derived type does have a formal type as an
8948 -- ancestor, then it's an error if the derived type is
8949 -- declared within the body of the generic unit that
8950 -- declares the formal type in its generic formal part. It's
8951 -- sufficient to check whether the ancestor type is declared
8952 -- inside the same generic body as the derived type (such as
8953 -- within a nested generic spec), in which case the
8954 -- derivation is legal. If the formal type is declared
8955 -- outside of that generic body, then it's guaranteed that
8956 -- the derived type is declared within the generic body of
8957 -- the generic unit declaring the formal type.
8959 if Is_Generic_Type (Ancestor_Type)
8960 and then Enclosing_Generic_Body (Ancestor_Type) /=
8961 Enclosing_Generic_Body (Derived_Type)
8964 ("parent type of& must not be descendant of formal type"
8965 & " of an enclosing generic body",
8966 Indic, Derived_Type);
8971 elsif Type_Access_Level (Derived_Type) /=
8972 Type_Access_Level (Parent_Type)
8973 and then not Is_Generic_Type (Derived_Type)
8975 if Is_Controlled (Parent_Type) then
8977 ("controlled type must be declared at the library level",
8981 ("type extension at deeper accessibility level than parent",
8987 GB : constant Node_Id := Enclosing_Generic_Body (Derived_Type);
8990 and then GB /= Enclosing_Generic_Body (Parent_Base)
8993 ("parent type of& must not be outside generic body"
8995 Indic, Derived_Type);
9001 -- Ada 2005 (AI-251)
9003 if Ada_Version >= Ada_2005 and then Is_Tagged then
9005 -- "The declaration of a specific descendant of an interface type
9006 -- freezes the interface type" (RM 13.14).
9011 if Is_Non_Empty_List (Interface_List (Type_Def)) then
9012 Iface := First (Interface_List (Type_Def));
9013 while Present (Iface) loop
9014 Freeze_Before (N, Etype (Iface));
9021 -- STEP 1b : preliminary cleanup of the full view of private types
9023 -- If the type is already marked as having discriminants, then it's the
9024 -- completion of a private type or private extension and we need to
9025 -- retain the discriminants from the partial view if the current
9026 -- declaration has Discriminant_Specifications so that we can verify
9027 -- conformance. However, we must remove any existing components that
9028 -- were inherited from the parent (and attached in Copy_And_Swap)
9029 -- because the full type inherits all appropriate components anyway, and
9030 -- we do not want the partial view's components interfering.
9032 if Has_Discriminants (Derived_Type) and then Discriminant_Specs then
9033 Discrim := First_Discriminant (Derived_Type);
9035 Last_Discrim := Discrim;
9036 Next_Discriminant (Discrim);
9037 exit when No (Discrim);
9040 Set_Last_Entity (Derived_Type, Last_Discrim);
9042 -- In all other cases wipe out the list of inherited components (even
9043 -- inherited discriminants), it will be properly rebuilt here.
9046 Set_First_Entity (Derived_Type, Empty);
9047 Set_Last_Entity (Derived_Type, Empty);
9050 -- STEP 1c: Initialize some flags for the Derived_Type
9052 -- The following flags must be initialized here so that
9053 -- Process_Discriminants can check that discriminants of tagged types do
9054 -- not have a default initial value and that access discriminants are
9055 -- only specified for limited records. For completeness, these flags are
9056 -- also initialized along with all the other flags below.
9058 -- AI-419: Limitedness is not inherited from an interface parent, so to
9059 -- be limited in that case the type must be explicitly declared as
9060 -- limited. However, task and protected interfaces are always limited.
9062 if Limited_Present (Type_Def) then
9063 Set_Is_Limited_Record (Derived_Type);
9065 elsif Is_Limited_Record (Parent_Type)
9066 or else (Present (Full_View (Parent_Type))
9067 and then Is_Limited_Record (Full_View (Parent_Type)))
9069 if not Is_Interface (Parent_Type)
9070 or else Is_Synchronized_Interface (Parent_Type)
9071 or else Is_Protected_Interface (Parent_Type)
9072 or else Is_Task_Interface (Parent_Type)
9074 Set_Is_Limited_Record (Derived_Type);
9078 -- STEP 2a: process discriminants of derived type if any
9080 Push_Scope (Derived_Type);
9082 if Discriminant_Specs then
9083 Set_Has_Unknown_Discriminants (Derived_Type, False);
9085 -- The following call initializes fields Has_Discriminants and
9086 -- Discriminant_Constraint, unless we are processing the completion
9087 -- of a private type declaration.
9089 Check_Or_Process_Discriminants (N, Derived_Type);
9091 -- For untagged types, the constraint on the Parent_Type must be
9092 -- present and is used to rename the discriminants.
9094 if not Is_Tagged and then not Has_Discriminants (Parent_Type) then
9095 Error_Msg_N ("untagged parent must have discriminants", Indic);
9097 elsif not Is_Tagged and then not Constraint_Present then
9099 ("discriminant constraint needed for derived untagged records",
9102 -- Otherwise the parent subtype must be constrained unless we have a
9103 -- private extension.
9105 elsif not Constraint_Present
9106 and then not Private_Extension
9107 and then not Is_Constrained (Parent_Type)
9110 ("unconstrained type not allowed in this context", Indic);
9112 elsif Constraint_Present then
9113 -- The following call sets the field Corresponding_Discriminant
9114 -- for the discriminants in the Derived_Type.
9116 Discs := Build_Discriminant_Constraints (Parent_Type, Indic, True);
9118 -- For untagged types all new discriminants must rename
9119 -- discriminants in the parent. For private extensions new
9120 -- discriminants cannot rename old ones (implied by [7.3(13)]).
9122 Discrim := First_Discriminant (Derived_Type);
9123 while Present (Discrim) loop
9125 and then No (Corresponding_Discriminant (Discrim))
9128 ("new discriminants must constrain old ones", Discrim);
9130 elsif Private_Extension
9131 and then Present (Corresponding_Discriminant (Discrim))
9134 ("only static constraints allowed for parent"
9135 & " discriminants in the partial view", Indic);
9139 -- If a new discriminant is used in the constraint, then its
9140 -- subtype must be statically compatible with the parent
9141 -- discriminant's subtype (3.7(15)).
9143 -- However, if the record contains an array constrained by
9144 -- the discriminant but with some different bound, the compiler
9145 -- tries to create a smaller range for the discriminant type.
9146 -- (See exp_ch3.Adjust_Discriminants). In this case, where
9147 -- the discriminant type is a scalar type, the check must use
9148 -- the original discriminant type in the parent declaration.
9151 Corr_Disc : constant Entity_Id :=
9152 Corresponding_Discriminant (Discrim);
9153 Disc_Type : constant Entity_Id := Etype (Discrim);
9154 Corr_Type : Entity_Id;
9157 if Present (Corr_Disc) then
9158 if Is_Scalar_Type (Disc_Type) then
9160 Entity (Discriminant_Type (Parent (Corr_Disc)));
9162 Corr_Type := Etype (Corr_Disc);
9166 Subtypes_Statically_Compatible (Disc_Type, Corr_Type)
9169 ("subtype must be compatible "
9170 & "with parent discriminant",
9176 Next_Discriminant (Discrim);
9179 -- Check whether the constraints of the full view statically
9180 -- match those imposed by the parent subtype [7.3(13)].
9182 if Present (Stored_Constraint (Derived_Type)) then
9187 C1 := First_Elmt (Discs);
9188 C2 := First_Elmt (Stored_Constraint (Derived_Type));
9189 while Present (C1) and then Present (C2) loop
9191 Fully_Conformant_Expressions (Node (C1), Node (C2))
9194 ("not conformant with previous declaration",
9205 -- STEP 2b: No new discriminants, inherit discriminants if any
9208 if Private_Extension then
9209 Set_Has_Unknown_Discriminants
9211 Has_Unknown_Discriminants (Parent_Type)
9212 or else Unknown_Discriminants_Present (N));
9214 -- The partial view of the parent may have unknown discriminants,
9215 -- but if the full view has discriminants and the parent type is
9216 -- in scope they must be inherited.
9218 elsif Has_Unknown_Discriminants (Parent_Type)
9220 (not Has_Discriminants (Parent_Type)
9221 or else not In_Open_Scopes (Scope (Parent_Base)))
9223 Set_Has_Unknown_Discriminants (Derived_Type);
9226 if not Has_Unknown_Discriminants (Derived_Type)
9227 and then not Has_Unknown_Discriminants (Parent_Base)
9228 and then Has_Discriminants (Parent_Type)
9230 Inherit_Discrims := True;
9231 Set_Has_Discriminants
9232 (Derived_Type, True);
9233 Set_Discriminant_Constraint
9234 (Derived_Type, Discriminant_Constraint (Parent_Base));
9237 -- The following test is true for private types (remember
9238 -- transformation 5. is not applied to those) and in an error
9241 if Constraint_Present then
9242 Discs := Build_Discriminant_Constraints (Parent_Type, Indic);
9245 -- For now mark a new derived type as constrained only if it has no
9246 -- discriminants. At the end of Build_Derived_Record_Type we properly
9247 -- set this flag in the case of private extensions. See comments in
9248 -- point 9. just before body of Build_Derived_Record_Type.
9252 not (Inherit_Discrims
9253 or else Has_Unknown_Discriminants (Derived_Type)));
9256 -- STEP 3: initialize fields of derived type
9258 Set_Is_Tagged_Type (Derived_Type, Is_Tagged);
9259 Set_Stored_Constraint (Derived_Type, No_Elist);
9261 -- Ada 2005 (AI-251): Private type-declarations can implement interfaces
9262 -- but cannot be interfaces
9264 if not Private_Extension
9265 and then Ekind (Derived_Type) /= E_Private_Type
9266 and then Ekind (Derived_Type) /= E_Limited_Private_Type
9268 if Interface_Present (Type_Def) then
9269 Analyze_Interface_Declaration (Derived_Type, Type_Def);
9272 Set_Interfaces (Derived_Type, No_Elist);
9275 -- Fields inherited from the Parent_Type
9277 Set_Has_Specified_Layout
9278 (Derived_Type, Has_Specified_Layout (Parent_Type));
9279 Set_Is_Limited_Composite
9280 (Derived_Type, Is_Limited_Composite (Parent_Type));
9281 Set_Is_Private_Composite
9282 (Derived_Type, Is_Private_Composite (Parent_Type));
9284 if Is_Tagged_Type (Parent_Type) then
9285 Set_No_Tagged_Streams_Pragma
9286 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9289 -- Fields inherited from the Parent_Base
9291 Set_Has_Controlled_Component
9292 (Derived_Type, Has_Controlled_Component (Parent_Base));
9293 Set_Has_Non_Standard_Rep
9294 (Derived_Type, Has_Non_Standard_Rep (Parent_Base));
9295 Set_Has_Primitive_Operations
9296 (Derived_Type, Has_Primitive_Operations (Parent_Base));
9298 -- Set fields for private derived types
9300 if Is_Private_Type (Derived_Type) then
9301 Set_Depends_On_Private (Derived_Type, True);
9302 Set_Private_Dependents (Derived_Type, New_Elmt_List);
9305 -- Inherit fields for non-private types. If this is the completion of a
9306 -- derivation from a private type, the parent itself is private and the
9307 -- attributes come from its full view, which must be present.
9309 if Is_Record_Type (Derived_Type) then
9311 Parent_Full : Entity_Id;
9314 if Is_Private_Type (Parent_Base)
9315 and then not Is_Record_Type (Parent_Base)
9317 Parent_Full := Full_View (Parent_Base);
9319 Parent_Full := Parent_Base;
9322 Set_Component_Alignment
9323 (Derived_Type, Component_Alignment (Parent_Full));
9325 (Derived_Type, C_Pass_By_Copy (Parent_Full));
9326 Set_Has_Complex_Representation
9327 (Derived_Type, Has_Complex_Representation (Parent_Full));
9329 -- For untagged types, inherit the layout by default to avoid
9330 -- costly changes of representation for type conversions.
9332 if not Is_Tagged then
9333 Set_Is_Packed (Derived_Type, Is_Packed (Parent_Full));
9334 Set_No_Reordering (Derived_Type, No_Reordering (Parent_Full));
9339 -- Set fields for tagged types
9342 Set_Direct_Primitive_Operations (Derived_Type, New_Elmt_List);
9344 -- All tagged types defined in Ada.Finalization are controlled
9346 if Chars (Scope (Derived_Type)) = Name_Finalization
9347 and then Chars (Scope (Scope (Derived_Type))) = Name_Ada
9348 and then Scope (Scope (Scope (Derived_Type))) = Standard_Standard
9350 Set_Is_Controlled_Active (Derived_Type);
9352 Set_Is_Controlled_Active
9353 (Derived_Type, Is_Controlled_Active (Parent_Base));
9356 -- Minor optimization: there is no need to generate the class-wide
9357 -- entity associated with an underlying record view.
9359 if not Is_Underlying_Record_View (Derived_Type) then
9360 Make_Class_Wide_Type (Derived_Type);
9363 Set_Is_Abstract_Type (Derived_Type, Abstract_Present (Type_Def));
9365 if Has_Discriminants (Derived_Type)
9366 and then Constraint_Present
9368 Set_Stored_Constraint
9369 (Derived_Type, Expand_To_Stored_Constraint (Parent_Base, Discs));
9372 if Ada_Version >= Ada_2005 then
9374 Ifaces_List : Elist_Id;
9377 -- Checks rules 3.9.4 (13/2 and 14/2)
9379 if Comes_From_Source (Derived_Type)
9380 and then not Is_Private_Type (Derived_Type)
9381 and then Is_Interface (Parent_Type)
9382 and then not Is_Interface (Derived_Type)
9384 if Is_Task_Interface (Parent_Type) then
9386 ("(Ada 2005) task type required (RM 3.9.4 (13.2))",
9389 elsif Is_Protected_Interface (Parent_Type) then
9391 ("(Ada 2005) protected type required (RM 3.9.4 (14.2))",
9396 -- Check ARM rules 3.9.4 (15/2), 9.1 (9.d/2) and 9.4 (11.d/2)
9398 Check_Interfaces (N, Type_Def);
9400 -- Ada 2005 (AI-251): Collect the list of progenitors that are
9401 -- not already in the parents.
9405 Ifaces_List => Ifaces_List,
9406 Exclude_Parents => True);
9408 Set_Interfaces (Derived_Type, Ifaces_List);
9410 -- If the derived type is the anonymous type created for
9411 -- a declaration whose parent has a constraint, propagate
9412 -- the interface list to the source type. This must be done
9413 -- prior to the completion of the analysis of the source type
9414 -- because the components in the extension may contain current
9415 -- instances whose legality depends on some ancestor.
9417 if Is_Itype (Derived_Type) then
9419 Def : constant Node_Id :=
9420 Associated_Node_For_Itype (Derived_Type);
9423 and then Nkind (Def) = N_Full_Type_Declaration
9426 (Defining_Identifier (Def), Ifaces_List);
9431 -- A type extension is automatically Ghost when one of its
9432 -- progenitors is Ghost (SPARK RM 6.9(9)). This property is
9433 -- also inherited when the parent type is Ghost, but this is
9434 -- done in Build_Derived_Type as the mechanism also handles
9435 -- untagged derivations.
9437 if Implements_Ghost_Interface (Derived_Type) then
9438 Set_Is_Ghost_Entity (Derived_Type);
9444 -- STEP 4: Inherit components from the parent base and constrain them.
9445 -- Apply the second transformation described in point 6. above.
9447 if (not Is_Empty_Elmt_List (Discs) or else Inherit_Discrims)
9448 or else not Has_Discriminants (Parent_Type)
9449 or else not Is_Constrained (Parent_Type)
9453 Constrs := Discriminant_Constraint (Parent_Type);
9458 (N, Parent_Base, Derived_Type, Is_Tagged, Inherit_Discrims, Constrs);
9460 -- STEP 5a: Copy the parent record declaration for untagged types
9462 Set_Has_Implicit_Dereference
9463 (Derived_Type, Has_Implicit_Dereference (Parent_Type));
9465 if not Is_Tagged then
9467 -- Discriminant_Constraint (Derived_Type) has been properly
9468 -- constructed. Save it and temporarily set it to Empty because we
9469 -- do not want the call to New_Copy_Tree below to mess this list.
9471 if Has_Discriminants (Derived_Type) then
9472 Save_Discr_Constr := Discriminant_Constraint (Derived_Type);
9473 Set_Discriminant_Constraint (Derived_Type, No_Elist);
9475 Save_Discr_Constr := No_Elist;
9478 -- Save the Etype field of Derived_Type. It is correctly set now,
9479 -- but the call to New_Copy tree may remap it to point to itself,
9480 -- which is not what we want. Ditto for the Next_Entity field.
9482 Save_Etype := Etype (Derived_Type);
9483 Save_Next_Entity := Next_Entity (Derived_Type);
9485 -- Assoc_List maps all stored discriminants in the Parent_Base to
9486 -- stored discriminants in the Derived_Type. It is fundamental that
9487 -- no types or itypes with discriminants other than the stored
9488 -- discriminants appear in the entities declared inside
9489 -- Derived_Type, since the back end cannot deal with it.
9493 (Parent (Parent_Base), Map => Assoc_List, New_Sloc => Loc);
9494 Copy_Dimensions_Of_Components (Derived_Type);
9496 -- Restore the fields saved prior to the New_Copy_Tree call
9497 -- and compute the stored constraint.
9499 Set_Etype (Derived_Type, Save_Etype);
9500 Link_Entities (Derived_Type, Save_Next_Entity);
9502 if Has_Discriminants (Derived_Type) then
9503 Set_Discriminant_Constraint
9504 (Derived_Type, Save_Discr_Constr);
9505 Set_Stored_Constraint
9506 (Derived_Type, Expand_To_Stored_Constraint (Parent_Type, Discs));
9508 Replace_Components (Derived_Type, New_Decl);
9511 -- Insert the new derived type declaration
9513 Rewrite (N, New_Decl);
9515 -- STEP 5b: Complete the processing for record extensions in generics
9517 -- There is no completion for record extensions declared in the
9518 -- parameter part of a generic, so we need to complete processing for
9519 -- these generic record extensions here. The Record_Type_Definition call
9520 -- will change the Ekind of the components from E_Void to E_Component.
9522 elsif Private_Extension and then Is_Generic_Type (Derived_Type) then
9523 Record_Type_Definition (Empty, Derived_Type);
9525 -- STEP 5c: Process the record extension for non private tagged types
9527 elsif not Private_Extension then
9528 Expand_Record_Extension (Derived_Type, Type_Def);
9530 -- Note : previously in ASIS mode we set the Parent_Subtype of the
9531 -- derived type to propagate some semantic information. This led
9532 -- to other ASIS failures and has been removed.
9534 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
9535 -- implemented interfaces if we are in expansion mode
9538 and then Has_Interfaces (Derived_Type)
9540 Add_Interface_Tag_Components (N, Derived_Type);
9543 -- Analyze the record extension
9545 Record_Type_Definition
9546 (Record_Extension_Part (Type_Def), Derived_Type);
9551 -- Nothing else to do if there is an error in the derivation.
9552 -- An unusual case: the full view may be derived from a type in an
9553 -- instance, when the partial view was used illegally as an actual
9554 -- in that instance, leading to a circular definition.
9556 if Etype (Derived_Type) = Any_Type
9557 or else Etype (Parent_Type) = Derived_Type
9562 -- Set delayed freeze and then derive subprograms, we need to do
9563 -- this in this order so that derived subprograms inherit the
9564 -- derived freeze if necessary.
9566 Set_Has_Delayed_Freeze (Derived_Type);
9568 if Derive_Subps then
9569 Derive_Subprograms (Parent_Type, Derived_Type);
9572 -- If we have a private extension which defines a constrained derived
9573 -- type mark as constrained here after we have derived subprograms. See
9574 -- comment on point 9. just above the body of Build_Derived_Record_Type.
9576 if Private_Extension and then Inherit_Discrims then
9577 if Constraint_Present and then not Is_Empty_Elmt_List (Discs) then
9578 Set_Is_Constrained (Derived_Type, True);
9579 Set_Discriminant_Constraint (Derived_Type, Discs);
9581 elsif Is_Constrained (Parent_Type) then
9583 (Derived_Type, True);
9584 Set_Discriminant_Constraint
9585 (Derived_Type, Discriminant_Constraint (Parent_Type));
9589 -- Update the class-wide type, which shares the now-completed entity
9590 -- list with its specific type. In case of underlying record views,
9591 -- we do not generate the corresponding class wide entity.
9594 and then not Is_Underlying_Record_View (Derived_Type)
9597 (Class_Wide_Type (Derived_Type), First_Entity (Derived_Type));
9599 (Class_Wide_Type (Derived_Type), Last_Entity (Derived_Type));
9602 Check_Function_Writable_Actuals (N);
9603 end Build_Derived_Record_Type;
9605 ------------------------
9606 -- Build_Derived_Type --
9607 ------------------------
9609 procedure Build_Derived_Type
9611 Parent_Type : Entity_Id;
9612 Derived_Type : Entity_Id;
9613 Is_Completion : Boolean;
9614 Derive_Subps : Boolean := True)
9616 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
9619 -- Set common attributes
9621 Set_Scope (Derived_Type, Current_Scope);
9622 Set_Etype (Derived_Type, Parent_Base);
9623 Set_Ekind (Derived_Type, Ekind (Parent_Base));
9624 Propagate_Concurrent_Flags (Derived_Type, Parent_Base);
9626 Set_Size_Info (Derived_Type, Parent_Type);
9627 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
9629 Set_Is_Controlled_Active
9630 (Derived_Type, Is_Controlled_Active (Parent_Type));
9632 Set_Disable_Controlled (Derived_Type, Disable_Controlled (Parent_Type));
9633 Set_Is_Tagged_Type (Derived_Type, Is_Tagged_Type (Parent_Type));
9634 Set_Is_Volatile (Derived_Type, Is_Volatile (Parent_Type));
9636 if Is_Tagged_Type (Derived_Type) then
9637 Set_No_Tagged_Streams_Pragma
9638 (Derived_Type, No_Tagged_Streams_Pragma (Parent_Type));
9641 -- If the parent has primitive routines, set the derived type link
9643 if Has_Primitive_Operations (Parent_Type) then
9644 Set_Derived_Type_Link (Parent_Base, Derived_Type);
9647 -- If the parent type is a private subtype, the convention on the base
9648 -- type may be set in the private part, and not propagated to the
9649 -- subtype until later, so we obtain the convention from the base type.
9651 Set_Convention (Derived_Type, Convention (Parent_Base));
9653 -- Set SSO default for record or array type
9655 if (Is_Array_Type (Derived_Type) or else Is_Record_Type (Derived_Type))
9656 and then Is_Base_Type (Derived_Type)
9658 Set_Default_SSO (Derived_Type);
9661 -- A derived type inherits the Default_Initial_Condition pragma coming
9662 -- from any parent type within the derivation chain.
9664 if Has_DIC (Parent_Type) then
9665 Set_Has_Inherited_DIC (Derived_Type);
9668 -- A derived type inherits any class-wide invariants coming from a
9669 -- parent type or an interface. Note that the invariant procedure of
9670 -- the parent type should not be inherited because the derived type may
9671 -- define invariants of its own.
9673 if not Is_Interface (Derived_Type) then
9674 if Has_Inherited_Invariants (Parent_Type)
9675 or else Has_Inheritable_Invariants (Parent_Type)
9677 Set_Has_Inherited_Invariants (Derived_Type);
9679 elsif Is_Concurrent_Type (Derived_Type)
9680 or else Is_Tagged_Type (Derived_Type)
9685 Iface_Elmt : Elmt_Id;
9690 Ifaces_List => Ifaces,
9691 Exclude_Parents => True);
9693 if Present (Ifaces) then
9694 Iface_Elmt := First_Elmt (Ifaces);
9695 while Present (Iface_Elmt) loop
9696 Iface := Node (Iface_Elmt);
9698 if Has_Inheritable_Invariants (Iface) then
9699 Set_Has_Inherited_Invariants (Derived_Type);
9703 Next_Elmt (Iface_Elmt);
9710 -- We similarly inherit predicates. Note that for scalar derived types
9711 -- the predicate is inherited from the first subtype, and not from its
9712 -- (anonymous) base type.
9714 if Has_Predicates (Parent_Type)
9715 or else Has_Predicates (First_Subtype (Parent_Type))
9717 Set_Has_Predicates (Derived_Type);
9720 -- The derived type inherits representation clauses from the parent
9721 -- type, and from any interfaces.
9723 Inherit_Rep_Item_Chain (Derived_Type, Parent_Type);
9726 Iface : Node_Id := First (Abstract_Interface_List (Derived_Type));
9728 while Present (Iface) loop
9729 Inherit_Rep_Item_Chain (Derived_Type, Entity (Iface));
9734 -- If the parent type has delayed rep aspects, then mark the derived
9735 -- type as possibly inheriting a delayed rep aspect.
9737 if Has_Delayed_Rep_Aspects (Parent_Type) then
9738 Set_May_Inherit_Delayed_Rep_Aspects (Derived_Type);
9741 -- A derived type becomes Ghost when its parent type is also Ghost
9742 -- (SPARK RM 6.9(9)). Note that the Ghost-related attributes are not
9743 -- directly inherited because the Ghost policy in effect may differ.
9745 if Is_Ghost_Entity (Parent_Type) then
9746 Set_Is_Ghost_Entity (Derived_Type);
9749 -- Type dependent processing
9751 case Ekind (Parent_Type) is
9752 when Numeric_Kind =>
9753 Build_Derived_Numeric_Type (N, Parent_Type, Derived_Type);
9756 Build_Derived_Array_Type (N, Parent_Type, Derived_Type);
9758 when Class_Wide_Kind
9762 Build_Derived_Record_Type
9763 (N, Parent_Type, Derived_Type, Derive_Subps);
9766 when Enumeration_Kind =>
9767 Build_Derived_Enumeration_Type (N, Parent_Type, Derived_Type);
9770 Build_Derived_Access_Type (N, Parent_Type, Derived_Type);
9772 when Incomplete_Or_Private_Kind =>
9773 Build_Derived_Private_Type
9774 (N, Parent_Type, Derived_Type, Is_Completion, Derive_Subps);
9776 -- For discriminated types, the derivation includes deriving
9777 -- primitive operations. For others it is done below.
9779 if Is_Tagged_Type (Parent_Type)
9780 or else Has_Discriminants (Parent_Type)
9781 or else (Present (Full_View (Parent_Type))
9782 and then Has_Discriminants (Full_View (Parent_Type)))
9787 when Concurrent_Kind =>
9788 Build_Derived_Concurrent_Type (N, Parent_Type, Derived_Type);
9791 raise Program_Error;
9794 -- Nothing more to do if some error occurred
9796 if Etype (Derived_Type) = Any_Type then
9800 -- Set delayed freeze and then derive subprograms, we need to do this
9801 -- in this order so that derived subprograms inherit the derived freeze
9804 Set_Has_Delayed_Freeze (Derived_Type);
9806 if Derive_Subps then
9807 Derive_Subprograms (Parent_Type, Derived_Type);
9810 Set_Has_Primitive_Operations
9811 (Base_Type (Derived_Type), Has_Primitive_Operations (Parent_Type));
9812 end Build_Derived_Type;
9814 -----------------------
9815 -- Build_Discriminal --
9816 -----------------------
9818 procedure Build_Discriminal (Discrim : Entity_Id) is
9819 D_Minal : Entity_Id;
9820 CR_Disc : Entity_Id;
9823 -- A discriminal has the same name as the discriminant
9825 D_Minal := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9827 Set_Ekind (D_Minal, E_In_Parameter);
9828 Set_Mechanism (D_Minal, Default_Mechanism);
9829 Set_Etype (D_Minal, Etype (Discrim));
9830 Set_Scope (D_Minal, Current_Scope);
9831 Set_Parent (D_Minal, Parent (Discrim));
9833 Set_Discriminal (Discrim, D_Minal);
9834 Set_Discriminal_Link (D_Minal, Discrim);
9836 -- For task types, build at once the discriminants of the corresponding
9837 -- record, which are needed if discriminants are used in entry defaults
9838 -- and in family bounds.
9840 if Is_Concurrent_Type (Current_Scope)
9842 Is_Limited_Type (Current_Scope)
9844 CR_Disc := Make_Defining_Identifier (Sloc (Discrim), Chars (Discrim));
9846 Set_Ekind (CR_Disc, E_In_Parameter);
9847 Set_Mechanism (CR_Disc, Default_Mechanism);
9848 Set_Etype (CR_Disc, Etype (Discrim));
9849 Set_Scope (CR_Disc, Current_Scope);
9850 Set_Discriminal_Link (CR_Disc, Discrim);
9851 Set_CR_Discriminant (Discrim, CR_Disc);
9853 end Build_Discriminal;
9855 ------------------------------------
9856 -- Build_Discriminant_Constraints --
9857 ------------------------------------
9859 function Build_Discriminant_Constraints
9862 Derived_Def : Boolean := False) return Elist_Id
9864 C : constant Node_Id := Constraint (Def);
9865 Nb_Discr : constant Nat := Number_Discriminants (T);
9867 Discr_Expr : array (1 .. Nb_Discr) of Node_Id := (others => Empty);
9868 -- Saves the expression corresponding to a given discriminant in T
9870 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat;
9871 -- Return the Position number within array Discr_Expr of a discriminant
9872 -- D within the discriminant list of the discriminated type T.
9874 procedure Process_Discriminant_Expression
9877 -- If this is a discriminant constraint on a partial view, do not
9878 -- generate an overflow check on the discriminant expression. The check
9879 -- will be generated when constraining the full view. Otherwise the
9880 -- backend creates duplicate symbols for the temporaries corresponding
9881 -- to the expressions to be checked, causing spurious assembler errors.
9887 function Pos_Of_Discr (T : Entity_Id; D : Entity_Id) return Nat is
9891 Disc := First_Discriminant (T);
9892 for J in Discr_Expr'Range loop
9897 Next_Discriminant (Disc);
9900 -- Note: Since this function is called on discriminants that are
9901 -- known to belong to the discriminated type, falling through the
9902 -- loop with no match signals an internal compiler error.
9904 raise Program_Error;
9907 -------------------------------------
9908 -- Process_Discriminant_Expression --
9909 -------------------------------------
9911 procedure Process_Discriminant_Expression
9915 BDT : constant Entity_Id := Base_Type (Etype (D));
9918 -- If this is a discriminant constraint on a partial view, do
9919 -- not generate an overflow on the discriminant expression. The
9920 -- check will be generated when constraining the full view.
9922 if Is_Private_Type (T)
9923 and then Present (Full_View (T))
9925 Analyze_And_Resolve (Expr, BDT, Suppress => Overflow_Check);
9927 Analyze_And_Resolve (Expr, BDT);
9929 end Process_Discriminant_Expression;
9931 -- Declarations local to Build_Discriminant_Constraints
9935 Elist : constant Elist_Id := New_Elmt_List;
9943 Discrim_Present : Boolean := False;
9945 -- Start of processing for Build_Discriminant_Constraints
9948 -- The following loop will process positional associations only.
9949 -- For a positional association, the (single) discriminant is
9950 -- implicitly specified by position, in textual order (RM 3.7.2).
9952 Discr := First_Discriminant (T);
9953 Constr := First (Constraints (C));
9954 for D in Discr_Expr'Range loop
9955 exit when Nkind (Constr) = N_Discriminant_Association;
9958 Error_Msg_N ("too few discriminants given in constraint", C);
9959 return New_Elmt_List;
9961 elsif Nkind (Constr) = N_Range
9962 or else (Nkind (Constr) = N_Attribute_Reference
9963 and then Attribute_Name (Constr) = Name_Range)
9966 ("a range is not a valid discriminant constraint", Constr);
9967 Discr_Expr (D) := Error;
9969 elsif Nkind (Constr) = N_Subtype_Indication then
9971 ("a subtype indication is not a valid discriminant constraint",
9973 Discr_Expr (D) := Error;
9976 Process_Discriminant_Expression (Constr, Discr);
9977 Discr_Expr (D) := Constr;
9980 Next_Discriminant (Discr);
9984 if No (Discr) and then Present (Constr) then
9985 Error_Msg_N ("too many discriminants given in constraint", Constr);
9986 return New_Elmt_List;
9989 -- Named associations can be given in any order, but if both positional
9990 -- and named associations are used in the same discriminant constraint,
9991 -- then positional associations must occur first, at their normal
9992 -- position. Hence once a named association is used, the rest of the
9993 -- discriminant constraint must use only named associations.
9995 while Present (Constr) loop
9997 -- Positional association forbidden after a named association
9999 if Nkind (Constr) /= N_Discriminant_Association then
10000 Error_Msg_N ("positional association follows named one", Constr);
10001 return New_Elmt_List;
10003 -- Otherwise it is a named association
10006 -- E records the type of the discriminants in the named
10007 -- association. All the discriminants specified in the same name
10008 -- association must have the same type.
10012 -- Search the list of discriminants in T to see if the simple name
10013 -- given in the constraint matches any of them.
10015 Id := First (Selector_Names (Constr));
10016 while Present (Id) loop
10019 -- If Original_Discriminant is present, we are processing a
10020 -- generic instantiation and this is an instance node. We need
10021 -- to find the name of the corresponding discriminant in the
10022 -- actual record type T and not the name of the discriminant in
10023 -- the generic formal. Example:
10026 -- type G (D : int) is private;
10028 -- subtype W is G (D => 1);
10030 -- type Rec (X : int) is record ... end record;
10031 -- package Q is new P (G => Rec);
10033 -- At the point of the instantiation, formal type G is Rec
10034 -- and therefore when reanalyzing "subtype W is G (D => 1);"
10035 -- which really looks like "subtype W is Rec (D => 1);" at
10036 -- the point of instantiation, we want to find the discriminant
10037 -- that corresponds to D in Rec, i.e. X.
10039 if Present (Original_Discriminant (Id))
10040 and then In_Instance
10042 Discr := Find_Corresponding_Discriminant (Id, T);
10046 Discr := First_Discriminant (T);
10047 while Present (Discr) loop
10048 if Chars (Discr) = Chars (Id) then
10053 Next_Discriminant (Discr);
10057 Error_Msg_N ("& does not match any discriminant", Id);
10058 return New_Elmt_List;
10060 -- If the parent type is a generic formal, preserve the
10061 -- name of the discriminant for subsequent instances.
10062 -- see comment at the beginning of this if statement.
10064 elsif Is_Generic_Type (Root_Type (T)) then
10065 Set_Original_Discriminant (Id, Discr);
10069 Position := Pos_Of_Discr (T, Discr);
10071 if Present (Discr_Expr (Position)) then
10072 Error_Msg_N ("duplicate constraint for discriminant&", Id);
10075 -- Each discriminant specified in the same named association
10076 -- must be associated with a separate copy of the
10077 -- corresponding expression.
10079 if Present (Next (Id)) then
10080 Expr := New_Copy_Tree (Expression (Constr));
10081 Set_Parent (Expr, Parent (Expression (Constr)));
10083 Expr := Expression (Constr);
10086 Discr_Expr (Position) := Expr;
10087 Process_Discriminant_Expression (Expr, Discr);
10090 -- A discriminant association with more than one discriminant
10091 -- name is only allowed if the named discriminants are all of
10092 -- the same type (RM 3.7.1(8)).
10095 E := Base_Type (Etype (Discr));
10097 elsif Base_Type (Etype (Discr)) /= E then
10099 ("all discriminants in an association " &
10100 "must have the same type", Id);
10110 -- A discriminant constraint must provide exactly one value for each
10111 -- discriminant of the type (RM 3.7.1(8)).
10113 for J in Discr_Expr'Range loop
10114 if No (Discr_Expr (J)) then
10115 Error_Msg_N ("too few discriminants given in constraint", C);
10116 return New_Elmt_List;
10120 -- Determine if there are discriminant expressions in the constraint
10122 for J in Discr_Expr'Range loop
10123 if Denotes_Discriminant
10124 (Discr_Expr (J), Check_Concurrent => True)
10126 Discrim_Present := True;
10130 -- Build an element list consisting of the expressions given in the
10131 -- discriminant constraint and apply the appropriate checks. The list
10132 -- is constructed after resolving any named discriminant associations
10133 -- and therefore the expressions appear in the textual order of the
10136 Discr := First_Discriminant (T);
10137 for J in Discr_Expr'Range loop
10138 if Discr_Expr (J) /= Error then
10139 Append_Elmt (Discr_Expr (J), Elist);
10141 -- If any of the discriminant constraints is given by a
10142 -- discriminant and we are in a derived type declaration we
10143 -- have a discriminant renaming. Establish link between new
10144 -- and old discriminant. The new discriminant has an implicit
10145 -- dereference if the old one does.
10147 if Denotes_Discriminant (Discr_Expr (J)) then
10148 if Derived_Def then
10150 New_Discr : constant Entity_Id := Entity (Discr_Expr (J));
10153 Set_Corresponding_Discriminant (New_Discr, Discr);
10154 Set_Has_Implicit_Dereference (New_Discr,
10155 Has_Implicit_Dereference (Discr));
10159 -- Force the evaluation of non-discriminant expressions.
10160 -- If we have found a discriminant in the constraint 3.4(26)
10161 -- and 3.8(18) demand that no range checks are performed are
10162 -- after evaluation. If the constraint is for a component
10163 -- definition that has a per-object constraint, expressions are
10164 -- evaluated but not checked either. In all other cases perform
10168 if Discrim_Present then
10171 elsif Nkind (Parent (Parent (Def))) = N_Component_Declaration
10172 and then Has_Per_Object_Constraint
10173 (Defining_Identifier (Parent (Parent (Def))))
10177 elsif Is_Access_Type (Etype (Discr)) then
10178 Apply_Constraint_Check (Discr_Expr (J), Etype (Discr));
10181 Apply_Range_Check (Discr_Expr (J), Etype (Discr));
10184 Force_Evaluation (Discr_Expr (J));
10187 -- Check that the designated type of an access discriminant's
10188 -- expression is not a class-wide type unless the discriminant's
10189 -- designated type is also class-wide.
10191 if Ekind (Etype (Discr)) = E_Anonymous_Access_Type
10192 and then not Is_Class_Wide_Type
10193 (Designated_Type (Etype (Discr)))
10194 and then Etype (Discr_Expr (J)) /= Any_Type
10195 and then Is_Class_Wide_Type
10196 (Designated_Type (Etype (Discr_Expr (J))))
10198 Wrong_Type (Discr_Expr (J), Etype (Discr));
10200 elsif Is_Access_Type (Etype (Discr))
10201 and then not Is_Access_Constant (Etype (Discr))
10202 and then Is_Access_Type (Etype (Discr_Expr (J)))
10203 and then Is_Access_Constant (Etype (Discr_Expr (J)))
10206 ("constraint for discriminant& must be access to variable",
10211 Next_Discriminant (Discr);
10215 end Build_Discriminant_Constraints;
10217 ---------------------------------
10218 -- Build_Discriminated_Subtype --
10219 ---------------------------------
10221 procedure Build_Discriminated_Subtype
10223 Def_Id : Entity_Id;
10225 Related_Nod : Node_Id;
10226 For_Access : Boolean := False)
10228 Has_Discrs : constant Boolean := Has_Discriminants (T);
10229 Constrained : constant Boolean :=
10231 and then not Is_Empty_Elmt_List (Elist)
10232 and then not Is_Class_Wide_Type (T))
10233 or else Is_Constrained (T);
10236 if Ekind (T) = E_Record_Type then
10237 Set_Ekind (Def_Id, E_Record_Subtype);
10239 -- Inherit preelaboration flag from base, for types for which it
10240 -- may have been set: records, private types, protected types.
10242 Set_Known_To_Have_Preelab_Init
10243 (Def_Id, Known_To_Have_Preelab_Init (T));
10245 elsif Ekind (T) = E_Task_Type then
10246 Set_Ekind (Def_Id, E_Task_Subtype);
10248 elsif Ekind (T) = E_Protected_Type then
10249 Set_Ekind (Def_Id, E_Protected_Subtype);
10250 Set_Known_To_Have_Preelab_Init
10251 (Def_Id, Known_To_Have_Preelab_Init (T));
10253 elsif Is_Private_Type (T) then
10254 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
10255 Set_Known_To_Have_Preelab_Init
10256 (Def_Id, Known_To_Have_Preelab_Init (T));
10258 -- Private subtypes may have private dependents
10260 Set_Private_Dependents (Def_Id, New_Elmt_List);
10262 elsif Is_Class_Wide_Type (T) then
10263 Set_Ekind (Def_Id, E_Class_Wide_Subtype);
10266 -- Incomplete type. Attach subtype to list of dependents, to be
10267 -- completed with full view of parent type, unless is it the
10268 -- designated subtype of a record component within an init_proc.
10269 -- This last case arises for a component of an access type whose
10270 -- designated type is incomplete (e.g. a Taft Amendment type).
10271 -- The designated subtype is within an inner scope, and needs no
10272 -- elaboration, because only the access type is needed in the
10273 -- initialization procedure.
10275 if Ekind (T) = E_Incomplete_Type then
10276 Set_Ekind (Def_Id, E_Incomplete_Subtype);
10278 Set_Ekind (Def_Id, Ekind (T));
10281 if For_Access and then Within_Init_Proc then
10284 Append_Elmt (Def_Id, Private_Dependents (T));
10288 Set_Etype (Def_Id, T);
10289 Init_Size_Align (Def_Id);
10290 Set_Has_Discriminants (Def_Id, Has_Discrs);
10291 Set_Is_Constrained (Def_Id, Constrained);
10293 Set_First_Entity (Def_Id, First_Entity (T));
10294 Set_Last_Entity (Def_Id, Last_Entity (T));
10295 Set_Has_Implicit_Dereference
10296 (Def_Id, Has_Implicit_Dereference (T));
10297 Set_Has_Pragma_Unreferenced_Objects
10298 (Def_Id, Has_Pragma_Unreferenced_Objects (T));
10300 -- If the subtype is the completion of a private declaration, there may
10301 -- have been representation clauses for the partial view, and they must
10302 -- be preserved. Build_Derived_Type chains the inherited clauses with
10303 -- the ones appearing on the extension. If this comes from a subtype
10304 -- declaration, all clauses are inherited.
10306 if No (First_Rep_Item (Def_Id)) then
10307 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
10310 if Is_Tagged_Type (T) then
10311 Set_Is_Tagged_Type (Def_Id);
10312 Set_No_Tagged_Streams_Pragma (Def_Id, No_Tagged_Streams_Pragma (T));
10313 Make_Class_Wide_Type (Def_Id);
10316 Set_Stored_Constraint (Def_Id, No_Elist);
10319 Set_Discriminant_Constraint (Def_Id, Elist);
10320 Set_Stored_Constraint_From_Discriminant_Constraint (Def_Id);
10323 if Is_Tagged_Type (T) then
10325 -- Ada 2005 (AI-251): In case of concurrent types we inherit the
10326 -- concurrent record type (which has the list of primitive
10329 if Ada_Version >= Ada_2005
10330 and then Is_Concurrent_Type (T)
10332 Set_Corresponding_Record_Type (Def_Id,
10333 Corresponding_Record_Type (T));
10335 Set_Direct_Primitive_Operations (Def_Id,
10336 Direct_Primitive_Operations (T));
10339 Set_Is_Abstract_Type (Def_Id, Is_Abstract_Type (T));
10342 -- Subtypes introduced by component declarations do not need to be
10343 -- marked as delayed, and do not get freeze nodes, because the semantics
10344 -- verifies that the parents of the subtypes are frozen before the
10345 -- enclosing record is frozen.
10347 if not Is_Type (Scope (Def_Id)) then
10348 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
10350 if Is_Private_Type (T)
10351 and then Present (Full_View (T))
10353 Conditional_Delay (Def_Id, Full_View (T));
10355 Conditional_Delay (Def_Id, T);
10359 if Is_Record_Type (T) then
10360 Set_Is_Limited_Record (Def_Id, Is_Limited_Record (T));
10363 and then not Is_Empty_Elmt_List (Elist)
10364 and then not For_Access
10366 Create_Constrained_Components (Def_Id, Related_Nod, T, Elist);
10369 Set_Cloned_Subtype (Def_Id, T);
10372 end Build_Discriminated_Subtype;
10374 ---------------------------
10375 -- Build_Itype_Reference --
10376 ---------------------------
10378 procedure Build_Itype_Reference
10382 IR : constant Node_Id := Make_Itype_Reference (Sloc (Nod));
10385 -- Itype references are only created for use by the back-end
10387 if Inside_A_Generic then
10390 Set_Itype (IR, Ityp);
10392 -- If Nod is a library unit entity, then Insert_After won't work,
10393 -- because Nod is not a member of any list. Therefore, we use
10394 -- Add_Global_Declaration in this case. This can happen if we have a
10395 -- build-in-place library function, child unit or not.
10397 if (Nkind (Nod) in N_Entity and then Is_Compilation_Unit (Nod))
10398 or else (Nkind_In (Nod, N_Defining_Program_Unit_Name,
10399 N_Subprogram_Declaration)
10400 and then Is_Compilation_Unit (Defining_Entity (Nod)))
10402 Add_Global_Declaration (IR);
10404 Insert_After (Nod, IR);
10407 end Build_Itype_Reference;
10409 ------------------------
10410 -- Build_Scalar_Bound --
10411 ------------------------
10413 function Build_Scalar_Bound
10416 Der_T : Entity_Id) return Node_Id
10418 New_Bound : Entity_Id;
10421 -- Note: not clear why this is needed, how can the original bound
10422 -- be unanalyzed at this point? and if it is, what business do we
10423 -- have messing around with it? and why is the base type of the
10424 -- parent type the right type for the resolution. It probably is
10425 -- not. It is OK for the new bound we are creating, but not for
10426 -- the old one??? Still if it never happens, no problem.
10428 Analyze_And_Resolve (Bound, Base_Type (Par_T));
10430 if Nkind_In (Bound, N_Integer_Literal, N_Real_Literal) then
10431 New_Bound := New_Copy (Bound);
10432 Set_Etype (New_Bound, Der_T);
10433 Set_Analyzed (New_Bound);
10435 elsif Is_Entity_Name (Bound) then
10436 New_Bound := OK_Convert_To (Der_T, New_Copy (Bound));
10438 -- The following is almost certainly wrong. What business do we have
10439 -- relocating a node (Bound) that is presumably still attached to
10440 -- the tree elsewhere???
10443 New_Bound := OK_Convert_To (Der_T, Relocate_Node (Bound));
10446 Set_Etype (New_Bound, Der_T);
10448 end Build_Scalar_Bound;
10450 --------------------------------
10451 -- Build_Underlying_Full_View --
10452 --------------------------------
10454 procedure Build_Underlying_Full_View
10459 Loc : constant Source_Ptr := Sloc (N);
10460 Subt : constant Entity_Id :=
10461 Make_Defining_Identifier
10462 (Loc, New_External_Name (Chars (Typ), 'S'));
10469 procedure Set_Discriminant_Name (Id : Node_Id);
10470 -- If the derived type has discriminants, they may rename discriminants
10471 -- of the parent. When building the full view of the parent, we need to
10472 -- recover the names of the original discriminants if the constraint is
10473 -- given by named associations.
10475 ---------------------------
10476 -- Set_Discriminant_Name --
10477 ---------------------------
10479 procedure Set_Discriminant_Name (Id : Node_Id) is
10483 Set_Original_Discriminant (Id, Empty);
10485 if Has_Discriminants (Typ) then
10486 Disc := First_Discriminant (Typ);
10487 while Present (Disc) loop
10488 if Chars (Disc) = Chars (Id)
10489 and then Present (Corresponding_Discriminant (Disc))
10491 Set_Chars (Id, Chars (Corresponding_Discriminant (Disc)));
10493 Next_Discriminant (Disc);
10496 end Set_Discriminant_Name;
10498 -- Start of processing for Build_Underlying_Full_View
10501 if Nkind (N) = N_Full_Type_Declaration then
10502 Constr := Constraint (Subtype_Indication (Type_Definition (N)));
10504 elsif Nkind (N) = N_Subtype_Declaration then
10505 Constr := New_Copy_Tree (Constraint (Subtype_Indication (N)));
10507 elsif Nkind (N) = N_Component_Declaration then
10510 (Constraint (Subtype_Indication (Component_Definition (N))));
10513 raise Program_Error;
10516 C := First (Constraints (Constr));
10517 while Present (C) loop
10518 if Nkind (C) = N_Discriminant_Association then
10519 Id := First (Selector_Names (C));
10520 while Present (Id) loop
10521 Set_Discriminant_Name (Id);
10530 Make_Subtype_Declaration (Loc,
10531 Defining_Identifier => Subt,
10532 Subtype_Indication =>
10533 Make_Subtype_Indication (Loc,
10534 Subtype_Mark => New_Occurrence_Of (Par, Loc),
10535 Constraint => New_Copy_Tree (Constr)));
10537 -- If this is a component subtype for an outer itype, it is not
10538 -- a list member, so simply set the parent link for analysis: if
10539 -- the enclosing type does not need to be in a declarative list,
10540 -- neither do the components.
10542 if Is_List_Member (N)
10543 and then Nkind (N) /= N_Component_Declaration
10545 Insert_Before (N, Indic);
10547 Set_Parent (Indic, Parent (N));
10551 Set_Underlying_Full_View (Typ, Full_View (Subt));
10552 Set_Is_Underlying_Full_View (Full_View (Subt));
10553 end Build_Underlying_Full_View;
10555 -------------------------------
10556 -- Check_Abstract_Overriding --
10557 -------------------------------
10559 procedure Check_Abstract_Overriding (T : Entity_Id) is
10560 Alias_Subp : Entity_Id;
10562 Op_List : Elist_Id;
10564 Type_Def : Node_Id;
10566 procedure Check_Pragma_Implemented (Subp : Entity_Id);
10567 -- Ada 2012 (AI05-0030): Subprogram Subp overrides an interface routine
10568 -- which has pragma Implemented already set. Check whether Subp's entity
10569 -- kind conforms to the implementation kind of the overridden routine.
10571 procedure Check_Pragma_Implemented
10573 Iface_Subp : Entity_Id);
10574 -- Ada 2012 (AI05-0030): Subprogram Subp overrides interface routine
10575 -- Iface_Subp and both entities have pragma Implemented already set on
10576 -- them. Check whether the two implementation kinds are conforming.
10578 procedure Inherit_Pragma_Implemented
10580 Iface_Subp : Entity_Id);
10581 -- Ada 2012 (AI05-0030): Interface primitive Subp overrides interface
10582 -- subprogram Iface_Subp which has been marked by pragma Implemented.
10583 -- Propagate the implementation kind of Iface_Subp to Subp.
10585 ------------------------------
10586 -- Check_Pragma_Implemented --
10587 ------------------------------
10589 procedure Check_Pragma_Implemented (Subp : Entity_Id) is
10590 Iface_Alias : constant Entity_Id := Interface_Alias (Subp);
10591 Impl_Kind : constant Name_Id := Implementation_Kind (Iface_Alias);
10592 Subp_Alias : constant Entity_Id := Alias (Subp);
10593 Contr_Typ : Entity_Id;
10594 Impl_Subp : Entity_Id;
10597 -- Subp must have an alias since it is a hidden entity used to link
10598 -- an interface subprogram to its overriding counterpart.
10600 pragma Assert (Present (Subp_Alias));
10602 -- Handle aliases to synchronized wrappers
10604 Impl_Subp := Subp_Alias;
10606 if Is_Primitive_Wrapper (Impl_Subp) then
10607 Impl_Subp := Wrapped_Entity (Impl_Subp);
10610 -- Extract the type of the controlling formal
10612 Contr_Typ := Etype (First_Formal (Subp_Alias));
10614 if Is_Concurrent_Record_Type (Contr_Typ) then
10615 Contr_Typ := Corresponding_Concurrent_Type (Contr_Typ);
10618 -- An interface subprogram whose implementation kind is By_Entry must
10619 -- be implemented by an entry.
10621 if Impl_Kind = Name_By_Entry
10622 and then Ekind (Impl_Subp) /= E_Entry
10624 Error_Msg_Node_2 := Iface_Alias;
10626 ("type & must implement abstract subprogram & with an entry",
10627 Subp_Alias, Contr_Typ);
10629 elsif Impl_Kind = Name_By_Protected_Procedure then
10631 -- An interface subprogram whose implementation kind is By_
10632 -- Protected_Procedure cannot be implemented by a primitive
10633 -- procedure of a task type.
10635 if Ekind (Contr_Typ) /= E_Protected_Type then
10636 Error_Msg_Node_2 := Contr_Typ;
10638 ("interface subprogram & cannot be implemented by a "
10639 & "primitive procedure of task type &",
10640 Subp_Alias, Iface_Alias);
10642 -- An interface subprogram whose implementation kind is By_
10643 -- Protected_Procedure must be implemented by a procedure.
10645 elsif Ekind (Impl_Subp) /= E_Procedure then
10646 Error_Msg_Node_2 := Iface_Alias;
10648 ("type & must implement abstract subprogram & with a "
10649 & "procedure", Subp_Alias, Contr_Typ);
10651 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10652 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10654 Error_Msg_Name_1 := Impl_Kind;
10656 ("overriding operation& must have synchronization%",
10660 -- If primitive has Optional synchronization, overriding operation
10661 -- must match if it has an explicit synchronization.
10663 elsif Present (Get_Rep_Pragma (Impl_Subp, Name_Implemented))
10664 and then Implementation_Kind (Impl_Subp) /= Impl_Kind
10666 Error_Msg_Name_1 := Impl_Kind;
10668 ("overriding operation& must have synchronization%", Subp_Alias);
10670 end Check_Pragma_Implemented;
10672 ------------------------------
10673 -- Check_Pragma_Implemented --
10674 ------------------------------
10676 procedure Check_Pragma_Implemented
10678 Iface_Subp : Entity_Id)
10680 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10681 Subp_Kind : constant Name_Id := Implementation_Kind (Subp);
10684 -- Ada 2012 (AI05-0030): The implementation kinds of an overridden
10685 -- and overriding subprogram are different. In general this is an
10686 -- error except when the implementation kind of the overridden
10687 -- subprograms is By_Any or Optional.
10689 if Iface_Kind /= Subp_Kind
10690 and then Iface_Kind /= Name_By_Any
10691 and then Iface_Kind /= Name_Optional
10693 if Iface_Kind = Name_By_Entry then
10695 ("incompatible implementation kind, overridden subprogram " &
10696 "is marked By_Entry", Subp);
10699 ("incompatible implementation kind, overridden subprogram " &
10700 "is marked By_Protected_Procedure", Subp);
10703 end Check_Pragma_Implemented;
10705 --------------------------------
10706 -- Inherit_Pragma_Implemented --
10707 --------------------------------
10709 procedure Inherit_Pragma_Implemented
10711 Iface_Subp : Entity_Id)
10713 Iface_Kind : constant Name_Id := Implementation_Kind (Iface_Subp);
10714 Loc : constant Source_Ptr := Sloc (Subp);
10715 Impl_Prag : Node_Id;
10718 -- Since the implementation kind is stored as a representation item
10719 -- rather than a flag, create a pragma node.
10723 Chars => Name_Implemented,
10724 Pragma_Argument_Associations => New_List (
10725 Make_Pragma_Argument_Association (Loc,
10726 Expression => New_Occurrence_Of (Subp, Loc)),
10728 Make_Pragma_Argument_Association (Loc,
10729 Expression => Make_Identifier (Loc, Iface_Kind))));
10731 -- The pragma doesn't need to be analyzed because it is internally
10732 -- built. It is safe to directly register it as a rep item since we
10733 -- are only interested in the characters of the implementation kind.
10735 Record_Rep_Item (Subp, Impl_Prag);
10736 end Inherit_Pragma_Implemented;
10738 -- Start of processing for Check_Abstract_Overriding
10741 Op_List := Primitive_Operations (T);
10743 -- Loop to check primitive operations
10745 Elmt := First_Elmt (Op_List);
10746 while Present (Elmt) loop
10747 Subp := Node (Elmt);
10748 Alias_Subp := Alias (Subp);
10750 -- Inherited subprograms are identified by the fact that they do not
10751 -- come from source, and the associated source location is the
10752 -- location of the first subtype of the derived type.
10754 -- Ada 2005 (AI-228): Apply the rules of RM-3.9.3(6/2) for
10755 -- subprograms that "require overriding".
10757 -- Special exception, do not complain about failure to override the
10758 -- stream routines _Input and _Output, as well as the primitive
10759 -- operations used in dispatching selects since we always provide
10760 -- automatic overridings for these subprograms.
10762 -- The partial view of T may have been a private extension, for
10763 -- which inherited functions dispatching on result are abstract.
10764 -- If the full view is a null extension, there is no need for
10765 -- overriding in Ada 2005, but wrappers need to be built for them
10766 -- (see exp_ch3, Build_Controlling_Function_Wrappers).
10768 if Is_Null_Extension (T)
10769 and then Has_Controlling_Result (Subp)
10770 and then Ada_Version >= Ada_2005
10771 and then Present (Alias_Subp)
10772 and then not Comes_From_Source (Subp)
10773 and then not Is_Abstract_Subprogram (Alias_Subp)
10774 and then not Is_Access_Type (Etype (Subp))
10778 -- Ada 2005 (AI-251): Internal entities of interfaces need no
10779 -- processing because this check is done with the aliased
10782 elsif Present (Interface_Alias (Subp)) then
10785 elsif (Is_Abstract_Subprogram (Subp)
10786 or else Requires_Overriding (Subp)
10788 (Has_Controlling_Result (Subp)
10789 and then Present (Alias_Subp)
10790 and then not Comes_From_Source (Subp)
10791 and then Sloc (Subp) = Sloc (First_Subtype (T))))
10792 and then not Is_TSS (Subp, TSS_Stream_Input)
10793 and then not Is_TSS (Subp, TSS_Stream_Output)
10794 and then not Is_Abstract_Type (T)
10795 and then not Is_Predefined_Interface_Primitive (Subp)
10797 -- Ada 2005 (AI-251): Do not consider hidden entities associated
10798 -- with abstract interface types because the check will be done
10799 -- with the aliased entity (otherwise we generate a duplicated
10802 and then not Present (Interface_Alias (Subp))
10804 if Present (Alias_Subp) then
10806 -- Only perform the check for a derived subprogram when the
10807 -- type has an explicit record extension. This avoids incorrect
10808 -- flagging of abstract subprograms for the case of a type
10809 -- without an extension that is derived from a formal type
10810 -- with a tagged actual (can occur within a private part).
10812 -- Ada 2005 (AI-391): In the case of an inherited function with
10813 -- a controlling result of the type, the rule does not apply if
10814 -- the type is a null extension (unless the parent function
10815 -- itself is abstract, in which case the function must still be
10816 -- be overridden). The expander will generate an overriding
10817 -- wrapper function calling the parent subprogram (see
10818 -- Exp_Ch3.Make_Controlling_Wrapper_Functions).
10820 Type_Def := Type_Definition (Parent (T));
10822 if Nkind (Type_Def) = N_Derived_Type_Definition
10823 and then Present (Record_Extension_Part (Type_Def))
10825 (Ada_Version < Ada_2005
10826 or else not Is_Null_Extension (T)
10827 or else Ekind (Subp) = E_Procedure
10828 or else not Has_Controlling_Result (Subp)
10829 or else Is_Abstract_Subprogram (Alias_Subp)
10830 or else Requires_Overriding (Subp)
10831 or else Is_Access_Type (Etype (Subp)))
10833 -- Avoid reporting error in case of abstract predefined
10834 -- primitive inherited from interface type because the
10835 -- body of internally generated predefined primitives
10836 -- of tagged types are generated later by Freeze_Type
10838 if Is_Interface (Root_Type (T))
10839 and then Is_Abstract_Subprogram (Subp)
10840 and then Is_Predefined_Dispatching_Operation (Subp)
10841 and then not Comes_From_Source (Ultimate_Alias (Subp))
10845 -- A null extension is not obliged to override an inherited
10846 -- procedure subject to pragma Extensions_Visible with value
10847 -- False and at least one controlling OUT parameter
10848 -- (SPARK RM 6.1.7(6)).
10850 elsif Is_Null_Extension (T)
10851 and then Is_EVF_Procedure (Subp)
10857 ("type must be declared abstract or & overridden",
10860 -- Traverse the whole chain of aliased subprograms to
10861 -- complete the error notification. This is especially
10862 -- useful for traceability of the chain of entities when
10863 -- the subprogram corresponds with an interface
10864 -- subprogram (which may be defined in another package).
10866 if Present (Alias_Subp) then
10872 while Present (Alias (E)) loop
10874 -- Avoid reporting redundant errors on entities
10875 -- inherited from interfaces
10877 if Sloc (E) /= Sloc (T) then
10878 Error_Msg_Sloc := Sloc (E);
10880 ("\& has been inherited #", T, Subp);
10886 Error_Msg_Sloc := Sloc (E);
10888 -- AI05-0068: report if there is an overriding
10889 -- non-abstract subprogram that is invisible.
10892 and then not Is_Abstract_Subprogram (E)
10895 ("\& subprogram# is not visible",
10898 -- Clarify the case where a non-null extension must
10899 -- override inherited procedure subject to pragma
10900 -- Extensions_Visible with value False and at least
10901 -- one controlling OUT param.
10903 elsif Is_EVF_Procedure (E) then
10905 ("\& # is subject to Extensions_Visible False",
10910 ("\& has been inherited from subprogram #",
10917 -- Ada 2005 (AI-345): Protected or task type implementing
10918 -- abstract interfaces.
10920 elsif Is_Concurrent_Record_Type (T)
10921 and then Present (Interfaces (T))
10923 -- There is no need to check here RM 9.4(11.9/3) since we
10924 -- are processing the corresponding record type and the
10925 -- mode of the overriding subprograms was verified by
10926 -- Check_Conformance when the corresponding concurrent
10927 -- type declaration was analyzed.
10930 ("interface subprogram & must be overridden", T, Subp);
10932 -- Examine primitive operations of synchronized type to find
10933 -- homonyms that have the wrong profile.
10939 Prim := First_Entity (Corresponding_Concurrent_Type (T));
10940 while Present (Prim) loop
10941 if Chars (Prim) = Chars (Subp) then
10943 ("profile is not type conformant with prefixed "
10944 & "view profile of inherited operation&",
10948 Next_Entity (Prim);
10954 Error_Msg_Node_2 := T;
10956 ("abstract subprogram& not allowed for type&", Subp);
10958 -- Also post unconditional warning on the type (unconditional
10959 -- so that if there are more than one of these cases, we get
10960 -- them all, and not just the first one).
10962 Error_Msg_Node_2 := Subp;
10963 Error_Msg_N ("nonabstract type& has abstract subprogram&!", T);
10966 -- A subprogram subject to pragma Extensions_Visible with value
10967 -- "True" cannot override a subprogram subject to the same pragma
10968 -- with value "False" (SPARK RM 6.1.7(5)).
10970 elsif Extensions_Visible_Status (Subp) = Extensions_Visible_True
10971 and then Present (Overridden_Operation (Subp))
10972 and then Extensions_Visible_Status (Overridden_Operation (Subp)) =
10973 Extensions_Visible_False
10975 Error_Msg_Sloc := Sloc (Overridden_Operation (Subp));
10977 ("subprogram & with Extensions_Visible True cannot override "
10978 & "subprogram # with Extensions_Visible False", Subp);
10981 -- Ada 2012 (AI05-0030): Perform checks related to pragma Implemented
10983 -- Subp is an expander-generated procedure which maps an interface
10984 -- alias to a protected wrapper. The interface alias is flagged by
10985 -- pragma Implemented. Ensure that Subp is a procedure when the
10986 -- implementation kind is By_Protected_Procedure or an entry when
10989 if Ada_Version >= Ada_2012
10990 and then Is_Hidden (Subp)
10991 and then Present (Interface_Alias (Subp))
10992 and then Has_Rep_Pragma (Interface_Alias (Subp), Name_Implemented)
10994 Check_Pragma_Implemented (Subp);
10997 -- Subp is an interface primitive which overrides another interface
10998 -- primitive marked with pragma Implemented.
11000 if Ada_Version >= Ada_2012
11001 and then Present (Overridden_Operation (Subp))
11002 and then Has_Rep_Pragma
11003 (Overridden_Operation (Subp), Name_Implemented)
11005 -- If the overriding routine is also marked by Implemented, check
11006 -- that the two implementation kinds are conforming.
11008 if Has_Rep_Pragma (Subp, Name_Implemented) then
11009 Check_Pragma_Implemented
11011 Iface_Subp => Overridden_Operation (Subp));
11013 -- Otherwise the overriding routine inherits the implementation
11014 -- kind from the overridden subprogram.
11017 Inherit_Pragma_Implemented
11019 Iface_Subp => Overridden_Operation (Subp));
11023 -- If the operation is a wrapper for a synchronized primitive, it
11024 -- may be called indirectly through a dispatching select. We assume
11025 -- that it will be referenced elsewhere indirectly, and suppress
11026 -- warnings about an unused entity.
11028 if Is_Primitive_Wrapper (Subp)
11029 and then Present (Wrapped_Entity (Subp))
11031 Set_Referenced (Wrapped_Entity (Subp));
11036 end Check_Abstract_Overriding;
11038 ------------------------------------------------
11039 -- Check_Access_Discriminant_Requires_Limited --
11040 ------------------------------------------------
11042 procedure Check_Access_Discriminant_Requires_Limited
11047 -- A discriminant_specification for an access discriminant shall appear
11048 -- only in the declaration for a task or protected type, or for a type
11049 -- with the reserved word 'limited' in its definition or in one of its
11050 -- ancestors (RM 3.7(10)).
11052 -- AI-0063: The proper condition is that type must be immutably limited,
11053 -- or else be a partial view.
11055 if Nkind (Discriminant_Type (D)) = N_Access_Definition then
11056 if Is_Limited_View (Current_Scope)
11058 (Nkind (Parent (Current_Scope)) = N_Private_Type_Declaration
11059 and then Limited_Present (Parent (Current_Scope)))
11065 ("access discriminants allowed only for limited types", Loc);
11068 end Check_Access_Discriminant_Requires_Limited;
11070 -----------------------------------
11071 -- Check_Aliased_Component_Types --
11072 -----------------------------------
11074 procedure Check_Aliased_Component_Types (T : Entity_Id) is
11078 -- ??? Also need to check components of record extensions, but not
11079 -- components of protected types (which are always limited).
11081 -- Ada 2005: AI-363 relaxes this rule, to allow heap objects of such
11082 -- types to be unconstrained. This is safe because it is illegal to
11083 -- create access subtypes to such types with explicit discriminant
11086 if not Is_Limited_Type (T) then
11087 if Ekind (T) = E_Record_Type then
11088 C := First_Component (T);
11089 while Present (C) loop
11091 and then Has_Discriminants (Etype (C))
11092 and then not Is_Constrained (Etype (C))
11093 and then not In_Instance_Body
11094 and then Ada_Version < Ada_2005
11097 ("aliased component must be constrained (RM 3.6(11))",
11101 Next_Component (C);
11104 elsif Ekind (T) = E_Array_Type then
11105 if Has_Aliased_Components (T)
11106 and then Has_Discriminants (Component_Type (T))
11107 and then not Is_Constrained (Component_Type (T))
11108 and then not In_Instance_Body
11109 and then Ada_Version < Ada_2005
11112 ("aliased component type must be constrained (RM 3.6(11))",
11117 end Check_Aliased_Component_Types;
11119 ---------------------------------------
11120 -- Check_Anonymous_Access_Components --
11121 ---------------------------------------
11123 procedure Check_Anonymous_Access_Components
11124 (Typ_Decl : Node_Id;
11127 Comp_List : Node_Id)
11129 Loc : constant Source_Ptr := Sloc (Typ_Decl);
11130 Anon_Access : Entity_Id;
11133 Comp_Def : Node_Id;
11135 Type_Def : Node_Id;
11137 procedure Build_Incomplete_Type_Declaration;
11138 -- If the record type contains components that include an access to the
11139 -- current record, then create an incomplete type declaration for the
11140 -- record, to be used as the designated type of the anonymous access.
11141 -- This is done only once, and only if there is no previous partial
11142 -- view of the type.
11144 function Designates_T (Subt : Node_Id) return Boolean;
11145 -- Check whether a node designates the enclosing record type, or 'Class
11148 function Mentions_T (Acc_Def : Node_Id) return Boolean;
11149 -- Check whether an access definition includes a reference to
11150 -- the enclosing record type. The reference can be a subtype mark
11151 -- in the access definition itself, a 'Class attribute reference, or
11152 -- recursively a reference appearing in a parameter specification
11153 -- or result definition of an access_to_subprogram definition.
11155 --------------------------------------
11156 -- Build_Incomplete_Type_Declaration --
11157 --------------------------------------
11159 procedure Build_Incomplete_Type_Declaration is
11164 -- Is_Tagged indicates whether the type is tagged. It is tagged if
11165 -- it's "is new ... with record" or else "is tagged record ...".
11167 Is_Tagged : constant Boolean :=
11168 (Nkind (Type_Definition (Typ_Decl)) = N_Derived_Type_Definition
11170 Present (Record_Extension_Part (Type_Definition (Typ_Decl))))
11172 (Nkind (Type_Definition (Typ_Decl)) = N_Record_Definition
11173 and then Tagged_Present (Type_Definition (Typ_Decl)));
11176 -- If there is a previous partial view, no need to create a new one
11177 -- If the partial view, given by Prev, is incomplete, If Prev is
11178 -- a private declaration, full declaration is flagged accordingly.
11180 if Prev /= Typ then
11182 Make_Class_Wide_Type (Prev);
11183 Set_Class_Wide_Type (Typ, Class_Wide_Type (Prev));
11184 Set_Etype (Class_Wide_Type (Typ), Typ);
11189 elsif Has_Private_Declaration (Typ) then
11191 -- If we refer to T'Class inside T, and T is the completion of a
11192 -- private type, then make sure the class-wide type exists.
11195 Make_Class_Wide_Type (Typ);
11200 -- If there was a previous anonymous access type, the incomplete
11201 -- type declaration will have been created already.
11203 elsif Present (Current_Entity (Typ))
11204 and then Ekind (Current_Entity (Typ)) = E_Incomplete_Type
11205 and then Full_View (Current_Entity (Typ)) = Typ
11208 and then Comes_From_Source (Current_Entity (Typ))
11209 and then not Is_Tagged_Type (Current_Entity (Typ))
11211 Make_Class_Wide_Type (Typ);
11213 ("incomplete view of tagged type should be declared tagged??",
11214 Parent (Current_Entity (Typ)));
11219 Inc_T := Make_Defining_Identifier (Loc, Chars (Typ));
11220 Decl := Make_Incomplete_Type_Declaration (Loc, Inc_T);
11222 -- Type has already been inserted into the current scope. Remove
11223 -- it, and add incomplete declaration for type, so that subsequent
11224 -- anonymous access types can use it. The entity is unchained from
11225 -- the homonym list and from immediate visibility. After analysis,
11226 -- the entity in the incomplete declaration becomes immediately
11227 -- visible in the record declaration that follows.
11229 H := Current_Entity (Typ);
11232 Set_Name_Entity_Id (Chars (Typ), Homonym (Typ));
11235 and then Homonym (H) /= Typ
11237 H := Homonym (Typ);
11240 Set_Homonym (H, Homonym (Typ));
11243 Insert_Before (Typ_Decl, Decl);
11245 Set_Full_View (Inc_T, Typ);
11249 -- Create a common class-wide type for both views, and set the
11250 -- Etype of the class-wide type to the full view.
11252 Make_Class_Wide_Type (Inc_T);
11253 Set_Class_Wide_Type (Typ, Class_Wide_Type (Inc_T));
11254 Set_Etype (Class_Wide_Type (Typ), Typ);
11257 end Build_Incomplete_Type_Declaration;
11263 function Designates_T (Subt : Node_Id) return Boolean is
11264 Type_Id : constant Name_Id := Chars (Typ);
11266 function Names_T (Nam : Node_Id) return Boolean;
11267 -- The record type has not been introduced in the current scope
11268 -- yet, so we must examine the name of the type itself, either
11269 -- an identifier T, or an expanded name of the form P.T, where
11270 -- P denotes the current scope.
11276 function Names_T (Nam : Node_Id) return Boolean is
11278 if Nkind (Nam) = N_Identifier then
11279 return Chars (Nam) = Type_Id;
11281 elsif Nkind (Nam) = N_Selected_Component then
11282 if Chars (Selector_Name (Nam)) = Type_Id then
11283 if Nkind (Prefix (Nam)) = N_Identifier then
11284 return Chars (Prefix (Nam)) = Chars (Current_Scope);
11286 elsif Nkind (Prefix (Nam)) = N_Selected_Component then
11287 return Chars (Selector_Name (Prefix (Nam))) =
11288 Chars (Current_Scope);
11302 -- Start of processing for Designates_T
11305 if Nkind (Subt) = N_Identifier then
11306 return Chars (Subt) = Type_Id;
11308 -- Reference can be through an expanded name which has not been
11309 -- analyzed yet, and which designates enclosing scopes.
11311 elsif Nkind (Subt) = N_Selected_Component then
11312 if Names_T (Subt) then
11315 -- Otherwise it must denote an entity that is already visible.
11316 -- The access definition may name a subtype of the enclosing
11317 -- type, if there is a previous incomplete declaration for it.
11320 Find_Selected_Component (Subt);
11322 Is_Entity_Name (Subt)
11323 and then Scope (Entity (Subt)) = Current_Scope
11325 (Chars (Base_Type (Entity (Subt))) = Type_Id
11327 (Is_Class_Wide_Type (Entity (Subt))
11329 Chars (Etype (Base_Type (Entity (Subt)))) =
11333 -- A reference to the current type may appear as the prefix of
11334 -- a 'Class attribute.
11336 elsif Nkind (Subt) = N_Attribute_Reference
11337 and then Attribute_Name (Subt) = Name_Class
11339 return Names_T (Prefix (Subt));
11350 function Mentions_T (Acc_Def : Node_Id) return Boolean is
11351 Param_Spec : Node_Id;
11353 Acc_Subprg : constant Node_Id :=
11354 Access_To_Subprogram_Definition (Acc_Def);
11357 if No (Acc_Subprg) then
11358 return Designates_T (Subtype_Mark (Acc_Def));
11361 -- Component is an access_to_subprogram: examine its formals,
11362 -- and result definition in the case of an access_to_function.
11364 Param_Spec := First (Parameter_Specifications (Acc_Subprg));
11365 while Present (Param_Spec) loop
11366 if Nkind (Parameter_Type (Param_Spec)) = N_Access_Definition
11367 and then Mentions_T (Parameter_Type (Param_Spec))
11371 elsif Designates_T (Parameter_Type (Param_Spec)) then
11378 if Nkind (Acc_Subprg) = N_Access_Function_Definition then
11379 if Nkind (Result_Definition (Acc_Subprg)) =
11380 N_Access_Definition
11382 return Mentions_T (Result_Definition (Acc_Subprg));
11384 return Designates_T (Result_Definition (Acc_Subprg));
11391 -- Start of processing for Check_Anonymous_Access_Components
11394 if No (Comp_List) then
11398 Comp := First (Component_Items (Comp_List));
11399 while Present (Comp) loop
11400 if Nkind (Comp) = N_Component_Declaration
11402 (Access_Definition (Component_Definition (Comp)))
11404 Mentions_T (Access_Definition (Component_Definition (Comp)))
11406 Comp_Def := Component_Definition (Comp);
11408 Access_To_Subprogram_Definition (Access_Definition (Comp_Def));
11410 Build_Incomplete_Type_Declaration;
11411 Anon_Access := Make_Temporary (Loc, 'S');
11413 -- Create a declaration for the anonymous access type: either
11414 -- an access_to_object or an access_to_subprogram.
11416 if Present (Acc_Def) then
11417 if Nkind (Acc_Def) = N_Access_Function_Definition then
11419 Make_Access_Function_Definition (Loc,
11420 Parameter_Specifications =>
11421 Parameter_Specifications (Acc_Def),
11422 Result_Definition => Result_Definition (Acc_Def));
11425 Make_Access_Procedure_Definition (Loc,
11426 Parameter_Specifications =>
11427 Parameter_Specifications (Acc_Def));
11432 Make_Access_To_Object_Definition (Loc,
11433 Subtype_Indication =>
11435 (Subtype_Mark (Access_Definition (Comp_Def))));
11437 Set_Constant_Present
11438 (Type_Def, Constant_Present (Access_Definition (Comp_Def)));
11440 (Type_Def, All_Present (Access_Definition (Comp_Def)));
11443 Set_Null_Exclusion_Present
11445 Null_Exclusion_Present (Access_Definition (Comp_Def)));
11448 Make_Full_Type_Declaration (Loc,
11449 Defining_Identifier => Anon_Access,
11450 Type_Definition => Type_Def);
11452 Insert_Before (Typ_Decl, Decl);
11455 -- If an access to subprogram, create the extra formals
11457 if Present (Acc_Def) then
11458 Create_Extra_Formals (Designated_Type (Anon_Access));
11460 -- If an access to object, preserve entity of designated type,
11461 -- for ASIS use, before rewriting the component definition.
11468 Desig := Entity (Subtype_Indication (Type_Def));
11470 -- If the access definition is to the current record,
11471 -- the visible entity at this point is an incomplete
11472 -- type. Retrieve the full view to simplify ASIS queries
11474 if Ekind (Desig) = E_Incomplete_Type then
11475 Desig := Full_View (Desig);
11479 (Subtype_Mark (Access_Definition (Comp_Def)), Desig);
11484 Make_Component_Definition (Loc,
11485 Subtype_Indication =>
11486 New_Occurrence_Of (Anon_Access, Loc)));
11488 if Ekind (Designated_Type (Anon_Access)) = E_Subprogram_Type then
11489 Set_Ekind (Anon_Access, E_Anonymous_Access_Subprogram_Type);
11491 Set_Ekind (Anon_Access, E_Anonymous_Access_Type);
11494 Set_Is_Local_Anonymous_Access (Anon_Access);
11500 if Present (Variant_Part (Comp_List)) then
11504 V := First_Non_Pragma (Variants (Variant_Part (Comp_List)));
11505 while Present (V) loop
11506 Check_Anonymous_Access_Components
11507 (Typ_Decl, Typ, Prev, Component_List (V));
11508 Next_Non_Pragma (V);
11512 end Check_Anonymous_Access_Components;
11514 ----------------------
11515 -- Check_Completion --
11516 ----------------------
11518 procedure Check_Completion (Body_Id : Node_Id := Empty) is
11521 procedure Post_Error;
11522 -- Post error message for lack of completion for entity E
11528 procedure Post_Error is
11529 procedure Missing_Body;
11530 -- Output missing body message
11536 procedure Missing_Body is
11538 -- Spec is in same unit, so we can post on spec
11540 if In_Same_Source_Unit (Body_Id, E) then
11541 Error_Msg_N ("missing body for &", E);
11543 -- Spec is in a separate unit, so we have to post on the body
11546 Error_Msg_NE ("missing body for & declared#!", Body_Id, E);
11550 -- Start of processing for Post_Error
11553 if not Comes_From_Source (E) then
11554 if Ekind_In (E, E_Task_Type, E_Protected_Type) then
11556 -- It may be an anonymous protected type created for a
11557 -- single variable. Post error on variable, if present.
11563 Var := First_Entity (Current_Scope);
11564 while Present (Var) loop
11565 exit when Etype (Var) = E
11566 and then Comes_From_Source (Var);
11571 if Present (Var) then
11578 -- If a generated entity has no completion, then either previous
11579 -- semantic errors have disabled the expansion phase, or else we had
11580 -- missing subunits, or else we are compiling without expansion,
11581 -- or else something is very wrong.
11583 if not Comes_From_Source (E) then
11585 (Serious_Errors_Detected > 0
11586 or else Configurable_Run_Time_Violations > 0
11587 or else Subunits_Missing
11588 or else not Expander_Active);
11591 -- Here for source entity
11594 -- Here if no body to post the error message, so we post the error
11595 -- on the declaration that has no completion. This is not really
11596 -- the right place to post it, think about this later ???
11598 if No (Body_Id) then
11599 if Is_Type (E) then
11601 ("missing full declaration for }", Parent (E), E);
11603 Error_Msg_NE ("missing body for &", Parent (E), E);
11606 -- Package body has no completion for a declaration that appears
11607 -- in the corresponding spec. Post error on the body, with a
11608 -- reference to the non-completed declaration.
11611 Error_Msg_Sloc := Sloc (E);
11613 if Is_Type (E) then
11614 Error_Msg_NE ("missing full declaration for }!", Body_Id, E);
11616 elsif Is_Overloadable (E)
11617 and then Current_Entity_In_Scope (E) /= E
11619 -- It may be that the completion is mistyped and appears as
11620 -- a distinct overloading of the entity.
11623 Candidate : constant Entity_Id :=
11624 Current_Entity_In_Scope (E);
11625 Decl : constant Node_Id :=
11626 Unit_Declaration_Node (Candidate);
11629 if Is_Overloadable (Candidate)
11630 and then Ekind (Candidate) = Ekind (E)
11631 and then Nkind (Decl) = N_Subprogram_Body
11632 and then Acts_As_Spec (Decl)
11634 Check_Type_Conformant (Candidate, E);
11650 Pack_Id : constant Entity_Id := Current_Scope;
11652 -- Start of processing for Check_Completion
11655 E := First_Entity (Pack_Id);
11656 while Present (E) loop
11657 if Is_Intrinsic_Subprogram (E) then
11660 -- The following situation requires special handling: a child unit
11661 -- that appears in the context clause of the body of its parent:
11663 -- procedure Parent.Child (...);
11665 -- with Parent.Child;
11666 -- package body Parent is
11668 -- Here Parent.Child appears as a local entity, but should not be
11669 -- flagged as requiring completion, because it is a compilation
11672 -- Ignore missing completion for a subprogram that does not come from
11673 -- source (including the _Call primitive operation of RAS types,
11674 -- which has to have the flag Comes_From_Source for other purposes):
11675 -- we assume that the expander will provide the missing completion.
11676 -- In case of previous errors, other expansion actions that provide
11677 -- bodies for null procedures with not be invoked, so inhibit message
11680 -- Note that E_Operator is not in the list that follows, because
11681 -- this kind is reserved for predefined operators, that are
11682 -- intrinsic and do not need completion.
11684 elsif Ekind_In (E, E_Function,
11686 E_Generic_Function,
11687 E_Generic_Procedure)
11689 if Has_Completion (E) then
11692 elsif Is_Subprogram (E) and then Is_Abstract_Subprogram (E) then
11695 elsif Is_Subprogram (E)
11696 and then (not Comes_From_Source (E)
11697 or else Chars (E) = Name_uCall)
11702 Nkind (Parent (Unit_Declaration_Node (E))) = N_Compilation_Unit
11706 elsif Nkind (Parent (E)) = N_Procedure_Specification
11707 and then Null_Present (Parent (E))
11708 and then Serious_Errors_Detected > 0
11716 elsif Is_Entry (E) then
11717 if not Has_Completion (E) and then
11718 (Ekind (Scope (E)) = E_Protected_Object
11719 or else Ekind (Scope (E)) = E_Protected_Type)
11724 elsif Is_Package_Or_Generic_Package (E) then
11725 if Unit_Requires_Body (E) then
11726 if not Has_Completion (E)
11727 and then Nkind (Parent (Unit_Declaration_Node (E))) /=
11733 elsif not Is_Child_Unit (E) then
11734 May_Need_Implicit_Body (E);
11737 -- A formal incomplete type (Ada 2012) does not require a completion;
11738 -- other incomplete type declarations do.
11740 elsif Ekind (E) = E_Incomplete_Type
11741 and then No (Underlying_Type (E))
11742 and then not Is_Generic_Type (E)
11746 elsif Ekind_In (E, E_Task_Type, E_Protected_Type)
11747 and then not Has_Completion (E)
11751 -- A single task declared in the current scope is a constant, verify
11752 -- that the body of its anonymous type is in the same scope. If the
11753 -- task is defined elsewhere, this may be a renaming declaration for
11754 -- which no completion is needed.
11756 elsif Ekind (E) = E_Constant
11757 and then Ekind (Etype (E)) = E_Task_Type
11758 and then not Has_Completion (Etype (E))
11759 and then Scope (Etype (E)) = Current_Scope
11763 elsif Ekind (E) = E_Protected_Object
11764 and then not Has_Completion (Etype (E))
11768 elsif Ekind (E) = E_Record_Type then
11769 if Is_Tagged_Type (E) then
11770 Check_Abstract_Overriding (E);
11771 Check_Conventions (E);
11774 Check_Aliased_Component_Types (E);
11776 elsif Ekind (E) = E_Array_Type then
11777 Check_Aliased_Component_Types (E);
11783 end Check_Completion;
11785 ------------------------------------
11786 -- Check_CPP_Type_Has_No_Defaults --
11787 ------------------------------------
11789 procedure Check_CPP_Type_Has_No_Defaults (T : Entity_Id) is
11790 Tdef : constant Node_Id := Type_Definition (Declaration_Node (T));
11795 -- Obtain the component list
11797 if Nkind (Tdef) = N_Record_Definition then
11798 Clist := Component_List (Tdef);
11799 else pragma Assert (Nkind (Tdef) = N_Derived_Type_Definition);
11800 Clist := Component_List (Record_Extension_Part (Tdef));
11803 -- Check all components to ensure no default expressions
11805 if Present (Clist) then
11806 Comp := First (Component_Items (Clist));
11807 while Present (Comp) loop
11808 if Present (Expression (Comp)) then
11810 ("component of imported 'C'P'P type cannot have "
11811 & "default expression", Expression (Comp));
11817 end Check_CPP_Type_Has_No_Defaults;
11819 ----------------------------
11820 -- Check_Delta_Expression --
11821 ----------------------------
11823 procedure Check_Delta_Expression (E : Node_Id) is
11825 if not (Is_Real_Type (Etype (E))) then
11826 Wrong_Type (E, Any_Real);
11828 elsif not Is_OK_Static_Expression (E) then
11829 Flag_Non_Static_Expr
11830 ("non-static expression used for delta value!", E);
11832 elsif not UR_Is_Positive (Expr_Value_R (E)) then
11833 Error_Msg_N ("delta expression must be positive", E);
11839 -- If any of above errors occurred, then replace the incorrect
11840 -- expression by the real 0.1, which should prevent further errors.
11843 Make_Real_Literal (Sloc (E), Ureal_Tenth));
11844 Analyze_And_Resolve (E, Standard_Float);
11845 end Check_Delta_Expression;
11847 -----------------------------
11848 -- Check_Digits_Expression --
11849 -----------------------------
11851 procedure Check_Digits_Expression (E : Node_Id) is
11853 if not (Is_Integer_Type (Etype (E))) then
11854 Wrong_Type (E, Any_Integer);
11856 elsif not Is_OK_Static_Expression (E) then
11857 Flag_Non_Static_Expr
11858 ("non-static expression used for digits value!", E);
11860 elsif Expr_Value (E) <= 0 then
11861 Error_Msg_N ("digits value must be greater than zero", E);
11867 -- If any of above errors occurred, then replace the incorrect
11868 -- expression by the integer 1, which should prevent further errors.
11870 Rewrite (E, Make_Integer_Literal (Sloc (E), 1));
11871 Analyze_And_Resolve (E, Standard_Integer);
11873 end Check_Digits_Expression;
11875 --------------------------
11876 -- Check_Initialization --
11877 --------------------------
11879 procedure Check_Initialization (T : Entity_Id; Exp : Node_Id) is
11881 -- Special processing for limited types
11883 if Is_Limited_Type (T)
11884 and then not In_Instance
11885 and then not In_Inlined_Body
11887 if not OK_For_Limited_Init (T, Exp) then
11889 -- In GNAT mode, this is just a warning, to allow it to be evilly
11890 -- turned off. Otherwise it is a real error.
11894 ("??cannot initialize entities of limited type!", Exp);
11896 elsif Ada_Version < Ada_2005 then
11898 -- The side effect removal machinery may generate illegal Ada
11899 -- code to avoid the usage of access types and 'reference in
11900 -- SPARK mode. Since this is legal code with respect to theorem
11901 -- proving, do not emit the error.
11904 and then Nkind (Exp) = N_Function_Call
11905 and then Nkind (Parent (Exp)) = N_Object_Declaration
11906 and then not Comes_From_Source
11907 (Defining_Identifier (Parent (Exp)))
11913 ("cannot initialize entities of limited type", Exp);
11914 Explain_Limited_Type (T, Exp);
11918 -- Specialize error message according to kind of illegal
11919 -- initial expression.
11921 if Nkind (Exp) = N_Type_Conversion
11922 and then Nkind (Expression (Exp)) = N_Function_Call
11924 -- No error for internally-generated object declarations,
11925 -- which can come from build-in-place assignment statements.
11927 if Nkind (Parent (Exp)) = N_Object_Declaration
11928 and then not Comes_From_Source
11929 (Defining_Identifier (Parent (Exp)))
11935 ("illegal context for call to function with limited "
11941 ("initialization of limited object requires aggregate or "
11942 & "function call", Exp);
11948 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag gets
11949 -- set unless we can be sure that no range check is required.
11951 if (GNATprove_Mode or not Expander_Active)
11952 and then Is_Scalar_Type (T)
11953 and then not Is_In_Range (Exp, T, Assume_Valid => True)
11955 Set_Do_Range_Check (Exp);
11957 end Check_Initialization;
11959 ----------------------
11960 -- Check_Interfaces --
11961 ----------------------
11963 procedure Check_Interfaces (N : Node_Id; Def : Node_Id) is
11964 Parent_Type : constant Entity_Id := Etype (Defining_Identifier (N));
11967 Iface_Def : Node_Id;
11968 Iface_Typ : Entity_Id;
11969 Parent_Node : Node_Id;
11971 Is_Task : Boolean := False;
11972 -- Set True if parent type or any progenitor is a task interface
11974 Is_Protected : Boolean := False;
11975 -- Set True if parent type or any progenitor is a protected interface
11977 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id);
11978 -- Check that a progenitor is compatible with declaration. If an error
11979 -- message is output, it is posted on Error_Node.
11985 procedure Check_Ifaces (Iface_Def : Node_Id; Error_Node : Node_Id) is
11986 Iface_Id : constant Entity_Id :=
11987 Defining_Identifier (Parent (Iface_Def));
11988 Type_Def : Node_Id;
11991 if Nkind (N) = N_Private_Extension_Declaration then
11994 Type_Def := Type_Definition (N);
11997 if Is_Task_Interface (Iface_Id) then
12000 elsif Is_Protected_Interface (Iface_Id) then
12001 Is_Protected := True;
12004 if Is_Synchronized_Interface (Iface_Id) then
12006 -- A consequence of 3.9.4 (6/2) and 7.3 (7.2/2) is that a private
12007 -- extension derived from a synchronized interface must explicitly
12008 -- be declared synchronized, because the full view will be a
12009 -- synchronized type.
12011 if Nkind (N) = N_Private_Extension_Declaration then
12012 if not Synchronized_Present (N) then
12014 ("private extension of& must be explicitly synchronized",
12018 -- However, by 3.9.4(16/2), a full type that is a record extension
12019 -- is never allowed to derive from a synchronized interface (note
12020 -- that interfaces must be excluded from this check, because those
12021 -- are represented by derived type definitions in some cases).
12023 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12024 and then not Interface_Present (Type_Definition (N))
12026 Error_Msg_N ("record extension cannot derive from synchronized "
12027 & "interface", Error_Node);
12031 -- Check that the characteristics of the progenitor are compatible
12032 -- with the explicit qualifier in the declaration.
12033 -- The check only applies to qualifiers that come from source.
12034 -- Limited_Present also appears in the declaration of corresponding
12035 -- records, and the check does not apply to them.
12037 if Limited_Present (Type_Def)
12039 Is_Concurrent_Record_Type (Defining_Identifier (N))
12041 if Is_Limited_Interface (Parent_Type)
12042 and then not Is_Limited_Interface (Iface_Id)
12045 ("progenitor & must be limited interface",
12046 Error_Node, Iface_Id);
12049 (Task_Present (Iface_Def)
12050 or else Protected_Present (Iface_Def)
12051 or else Synchronized_Present (Iface_Def))
12052 and then Nkind (N) /= N_Private_Extension_Declaration
12053 and then not Error_Posted (N)
12056 ("progenitor & must be limited interface",
12057 Error_Node, Iface_Id);
12060 -- Protected interfaces can only inherit from limited, synchronized
12061 -- or protected interfaces.
12063 elsif Nkind (N) = N_Full_Type_Declaration
12064 and then Protected_Present (Type_Def)
12066 if Limited_Present (Iface_Def)
12067 or else Synchronized_Present (Iface_Def)
12068 or else Protected_Present (Iface_Def)
12072 elsif Task_Present (Iface_Def) then
12073 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12074 & "from task interface", Error_Node);
12077 Error_Msg_N ("(Ada 2005) protected interface cannot inherit "
12078 & "from non-limited interface", Error_Node);
12081 -- Ada 2005 (AI-345): Synchronized interfaces can only inherit from
12082 -- limited and synchronized.
12084 elsif Synchronized_Present (Type_Def) then
12085 if Limited_Present (Iface_Def)
12086 or else Synchronized_Present (Iface_Def)
12090 elsif Protected_Present (Iface_Def)
12091 and then Nkind (N) /= N_Private_Extension_Declaration
12093 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12094 & "from protected interface", Error_Node);
12096 elsif Task_Present (Iface_Def)
12097 and then Nkind (N) /= N_Private_Extension_Declaration
12099 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12100 & "from task interface", Error_Node);
12102 elsif not Is_Limited_Interface (Iface_Id) then
12103 Error_Msg_N ("(Ada 2005) synchronized interface cannot inherit "
12104 & "from non-limited interface", Error_Node);
12107 -- Ada 2005 (AI-345): Task interfaces can only inherit from limited,
12108 -- synchronized or task interfaces.
12110 elsif Nkind (N) = N_Full_Type_Declaration
12111 and then Task_Present (Type_Def)
12113 if Limited_Present (Iface_Def)
12114 or else Synchronized_Present (Iface_Def)
12115 or else Task_Present (Iface_Def)
12119 elsif Protected_Present (Iface_Def) then
12120 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12121 & "protected interface", Error_Node);
12124 Error_Msg_N ("(Ada 2005) task interface cannot inherit from "
12125 & "non-limited interface", Error_Node);
12130 -- Start of processing for Check_Interfaces
12133 if Is_Interface (Parent_Type) then
12134 if Is_Task_Interface (Parent_Type) then
12137 elsif Is_Protected_Interface (Parent_Type) then
12138 Is_Protected := True;
12142 if Nkind (N) = N_Private_Extension_Declaration then
12144 -- Check that progenitors are compatible with declaration
12146 Iface := First (Interface_List (Def));
12147 while Present (Iface) loop
12148 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12150 Parent_Node := Parent (Base_Type (Iface_Typ));
12151 Iface_Def := Type_Definition (Parent_Node);
12153 if not Is_Interface (Iface_Typ) then
12154 Diagnose_Interface (Iface, Iface_Typ);
12156 Check_Ifaces (Iface_Def, Iface);
12162 if Is_Task and Is_Protected then
12164 ("type cannot derive from task and protected interface", N);
12170 -- Full type declaration of derived type.
12171 -- Check compatibility with parent if it is interface type
12173 if Nkind (Type_Definition (N)) = N_Derived_Type_Definition
12174 and then Is_Interface (Parent_Type)
12176 Parent_Node := Parent (Parent_Type);
12178 -- More detailed checks for interface varieties
12181 (Iface_Def => Type_Definition (Parent_Node),
12182 Error_Node => Subtype_Indication (Type_Definition (N)));
12185 Iface := First (Interface_List (Def));
12186 while Present (Iface) loop
12187 Iface_Typ := Find_Type_Of_Subtype_Indic (Iface);
12189 Parent_Node := Parent (Base_Type (Iface_Typ));
12190 Iface_Def := Type_Definition (Parent_Node);
12192 if not Is_Interface (Iface_Typ) then
12193 Diagnose_Interface (Iface, Iface_Typ);
12196 -- "The declaration of a specific descendant of an interface
12197 -- type freezes the interface type" RM 13.14
12199 Freeze_Before (N, Iface_Typ);
12200 Check_Ifaces (Iface_Def, Error_Node => Iface);
12206 if Is_Task and Is_Protected then
12208 ("type cannot derive from task and protected interface", N);
12210 end Check_Interfaces;
12212 ------------------------------------
12213 -- Check_Or_Process_Discriminants --
12214 ------------------------------------
12216 -- If an incomplete or private type declaration was already given for the
12217 -- type, the discriminants may have already been processed if they were
12218 -- present on the incomplete declaration. In this case a full conformance
12219 -- check has been performed in Find_Type_Name, and we then recheck here
12220 -- some properties that can't be checked on the partial view alone.
12221 -- Otherwise we call Process_Discriminants.
12223 procedure Check_Or_Process_Discriminants
12226 Prev : Entity_Id := Empty)
12229 if Has_Discriminants (T) then
12231 -- Discriminants are already set on T if they were already present
12232 -- on the partial view. Make them visible to component declarations.
12236 -- Discriminant on T (full view) referencing expr on partial view
12238 Prev_D : Entity_Id;
12239 -- Entity of corresponding discriminant on partial view
12242 -- Discriminant specification for full view, expression is
12243 -- the syntactic copy on full view (which has been checked for
12244 -- conformance with partial view), only used here to post error
12248 D := First_Discriminant (T);
12249 New_D := First (Discriminant_Specifications (N));
12250 while Present (D) loop
12251 Prev_D := Current_Entity (D);
12252 Set_Current_Entity (D);
12253 Set_Is_Immediately_Visible (D);
12254 Set_Homonym (D, Prev_D);
12256 -- Handle the case where there is an untagged partial view and
12257 -- the full view is tagged: must disallow discriminants with
12258 -- defaults, unless compiling for Ada 2012, which allows a
12259 -- limited tagged type to have defaulted discriminants (see
12260 -- AI05-0214). However, suppress error here if it was already
12261 -- reported on the default expression of the partial view.
12263 if Is_Tagged_Type (T)
12264 and then Present (Expression (Parent (D)))
12265 and then (not Is_Limited_Type (Current_Scope)
12266 or else Ada_Version < Ada_2012)
12267 and then not Error_Posted (Expression (Parent (D)))
12269 if Ada_Version >= Ada_2012 then
12271 ("discriminants of nonlimited tagged type cannot have "
12273 Expression (New_D));
12276 ("discriminants of tagged type cannot have defaults",
12277 Expression (New_D));
12281 -- Ada 2005 (AI-230): Access discriminant allowed in
12282 -- non-limited record types.
12284 if Ada_Version < Ada_2005 then
12286 -- This restriction gets applied to the full type here. It
12287 -- has already been applied earlier to the partial view.
12289 Check_Access_Discriminant_Requires_Limited (Parent (D), N);
12292 Next_Discriminant (D);
12297 elsif Present (Discriminant_Specifications (N)) then
12298 Process_Discriminants (N, Prev);
12300 end Check_Or_Process_Discriminants;
12302 ----------------------
12303 -- Check_Real_Bound --
12304 ----------------------
12306 procedure Check_Real_Bound (Bound : Node_Id) is
12308 if not Is_Real_Type (Etype (Bound)) then
12310 ("bound in real type definition must be of real type", Bound);
12312 elsif not Is_OK_Static_Expression (Bound) then
12313 Flag_Non_Static_Expr
12314 ("non-static expression used for real type bound!", Bound);
12321 (Bound, Make_Real_Literal (Sloc (Bound), Ureal_0));
12323 Resolve (Bound, Standard_Float);
12324 end Check_Real_Bound;
12326 ------------------------------
12327 -- Complete_Private_Subtype --
12328 ------------------------------
12330 procedure Complete_Private_Subtype
12333 Full_Base : Entity_Id;
12334 Related_Nod : Node_Id)
12336 Save_Next_Entity : Entity_Id;
12337 Save_Homonym : Entity_Id;
12340 -- Set semantic attributes for (implicit) private subtype completion.
12341 -- If the full type has no discriminants, then it is a copy of the
12342 -- full view of the base. Otherwise, it is a subtype of the base with
12343 -- a possible discriminant constraint. Save and restore the original
12344 -- Next_Entity field of full to ensure that the calls to Copy_Node do
12345 -- not corrupt the entity chain.
12347 Save_Next_Entity := Next_Entity (Full);
12348 Save_Homonym := Homonym (Priv);
12350 if Ekind (Full_Base) in Private_Kind
12351 or else Ekind (Full_Base) in Protected_Kind
12352 or else Ekind (Full_Base) in Record_Kind
12353 or else Ekind (Full_Base) in Task_Kind
12355 Copy_Node (Priv, Full);
12357 -- Note that the Etype of the full view is the same as the Etype of
12358 -- the partial view. In this fashion, the subtype has access to the
12359 -- correct view of the parent.
12361 Set_Has_Discriminants (Full, Has_Discriminants (Full_Base));
12362 Set_Has_Unknown_Discriminants
12363 (Full, Has_Unknown_Discriminants (Full_Base));
12364 Set_First_Entity (Full, First_Entity (Full_Base));
12365 Set_Last_Entity (Full, Last_Entity (Full_Base));
12367 -- If the underlying base type is constrained, we know that the
12368 -- full view of the subtype is constrained as well (the converse
12369 -- is not necessarily true).
12371 if Is_Constrained (Full_Base) then
12372 Set_Is_Constrained (Full);
12376 Copy_Node (Full_Base, Full);
12378 -- The following subtlety with the Etype of the full view needs to be
12379 -- taken into account here. One could think that it must naturally be
12380 -- set to the base type of the full base:
12382 -- Set_Etype (Full, Base_Type (Full_Base));
12384 -- so that the full view becomes a subtype of the full base when the
12385 -- latter is a base type, which must for example happen when the full
12386 -- base is declared as derived type. That's also correct if the full
12387 -- base is declared as an array type, or a floating-point type, or a
12388 -- fixed-point type, or a signed integer type, as these declarations
12389 -- create an implicit base type and a first subtype so the Etype of
12390 -- the full views must be the implicit base type. But that's wrong
12391 -- if the full base is declared as an access type, or an enumeration
12392 -- type, or a modular integer type, as these declarations directly
12393 -- create a base type, i.e. with Etype pointing to itself. Moreover
12394 -- the full base being declared in the private part, i.e. when the
12395 -- views are swapped, the end result is that the Etype of the full
12396 -- base is set to its private view in this case and that we need to
12397 -- propagate this setting to the full view in order for the subtype
12398 -- to be compatible with the base type.
12400 if Is_Base_Type (Full_Base)
12401 and then (Is_Derived_Type (Full_Base)
12402 or else Ekind (Full_Base) in Array_Kind
12403 or else Ekind (Full_Base) in Fixed_Point_Kind
12404 or else Ekind (Full_Base) in Float_Kind
12405 or else Ekind (Full_Base) in Signed_Integer_Kind)
12407 Set_Etype (Full, Full_Base);
12410 Set_Chars (Full, Chars (Priv));
12411 Set_Sloc (Full, Sloc (Priv));
12412 Conditional_Delay (Full, Priv);
12415 Link_Entities (Full, Save_Next_Entity);
12416 Set_Homonym (Full, Save_Homonym);
12417 Set_Associated_Node_For_Itype (Full, Related_Nod);
12419 -- Set common attributes for all subtypes: kind, convention, etc.
12421 Set_Ekind (Full, Subtype_Kind (Ekind (Full_Base)));
12422 Set_Convention (Full, Convention (Full_Base));
12423 Set_Is_First_Subtype (Full, False);
12424 Set_Scope (Full, Scope (Priv));
12425 Set_Size_Info (Full, Full_Base);
12426 Set_RM_Size (Full, RM_Size (Full_Base));
12427 Set_Is_Itype (Full);
12429 -- A subtype of a private-type-without-discriminants, whose full-view
12430 -- has discriminants with default expressions, is not constrained.
12432 if not Has_Discriminants (Priv) then
12433 Set_Is_Constrained (Full, Is_Constrained (Full_Base));
12435 if Has_Discriminants (Full_Base) then
12436 Set_Discriminant_Constraint
12437 (Full, Discriminant_Constraint (Full_Base));
12439 -- The partial view may have been indefinite, the full view
12442 Set_Has_Unknown_Discriminants
12443 (Full, Has_Unknown_Discriminants (Full_Base));
12447 Set_First_Rep_Item (Full, First_Rep_Item (Full_Base));
12448 Set_Depends_On_Private (Full, Has_Private_Component (Full));
12450 -- Freeze the private subtype entity if its parent is delayed, and not
12451 -- already frozen. We skip this processing if the type is an anonymous
12452 -- subtype of a record component, or is the corresponding record of a
12453 -- protected type, since these are processed when the enclosing type
12454 -- is frozen. If the parent type is declared in a nested package then
12455 -- the freezing of the private and full views also happens later.
12457 if not Is_Type (Scope (Full)) then
12459 and then In_Same_Source_Unit (Full, Full_Base)
12460 and then Scope (Full_Base) /= Scope (Full)
12462 Set_Has_Delayed_Freeze (Full);
12463 Set_Has_Delayed_Freeze (Priv);
12466 Set_Has_Delayed_Freeze (Full,
12467 Has_Delayed_Freeze (Full_Base)
12468 and then not Is_Frozen (Full_Base));
12472 Set_Freeze_Node (Full, Empty);
12473 Set_Is_Frozen (Full, False);
12474 Set_Full_View (Priv, Full);
12476 if Has_Discriminants (Full) then
12477 Set_Stored_Constraint_From_Discriminant_Constraint (Full);
12478 Set_Stored_Constraint (Priv, Stored_Constraint (Full));
12480 if Has_Unknown_Discriminants (Full) then
12481 Set_Discriminant_Constraint (Full, No_Elist);
12485 if Ekind (Full_Base) = E_Record_Type
12486 and then Has_Discriminants (Full_Base)
12487 and then Has_Discriminants (Priv) -- might not, if errors
12488 and then not Has_Unknown_Discriminants (Priv)
12489 and then not Is_Empty_Elmt_List (Discriminant_Constraint (Priv))
12491 Create_Constrained_Components
12492 (Full, Related_Nod, Full_Base, Discriminant_Constraint (Priv));
12494 -- If the full base is itself derived from private, build a congruent
12495 -- subtype of its underlying type, for use by the back end. For a
12496 -- constrained record component, the declaration cannot be placed on
12497 -- the component list, but it must nevertheless be built an analyzed, to
12498 -- supply enough information for Gigi to compute the size of component.
12500 elsif Ekind (Full_Base) in Private_Kind
12501 and then Is_Derived_Type (Full_Base)
12502 and then Has_Discriminants (Full_Base)
12503 and then (Ekind (Current_Scope) /= E_Record_Subtype)
12505 if not Is_Itype (Priv)
12507 Nkind (Subtype_Indication (Parent (Priv))) = N_Subtype_Indication
12509 Build_Underlying_Full_View
12510 (Parent (Priv), Full, Etype (Full_Base));
12512 elsif Nkind (Related_Nod) = N_Component_Declaration then
12513 Build_Underlying_Full_View (Related_Nod, Full, Etype (Full_Base));
12516 elsif Is_Record_Type (Full_Base) then
12518 -- Show Full is simply a renaming of Full_Base
12520 Set_Cloned_Subtype (Full, Full_Base);
12523 -- It is unsafe to share the bounds of a scalar type, because the Itype
12524 -- is elaborated on demand, and if a bound is nonstatic, then different
12525 -- orders of elaboration in different units will lead to different
12526 -- external symbols.
12528 if Is_Scalar_Type (Full_Base) then
12529 Set_Scalar_Range (Full,
12530 Make_Range (Sloc (Related_Nod),
12532 Duplicate_Subexpr_No_Checks (Type_Low_Bound (Full_Base)),
12534 Duplicate_Subexpr_No_Checks (Type_High_Bound (Full_Base))));
12536 -- This completion inherits the bounds of the full parent, but if
12537 -- the parent is an unconstrained floating point type, so is the
12540 if Is_Floating_Point_Type (Full_Base) then
12541 Set_Includes_Infinities
12542 (Scalar_Range (Full), Has_Infinities (Full_Base));
12546 -- ??? It seems that a lot of fields are missing that should be copied
12547 -- from Full_Base to Full. Here are some that are introduced in a
12548 -- non-disruptive way but a cleanup is necessary.
12550 if Is_Tagged_Type (Full_Base) then
12551 Set_Is_Tagged_Type (Full);
12552 Set_Direct_Primitive_Operations
12553 (Full, Direct_Primitive_Operations (Full_Base));
12554 Set_No_Tagged_Streams_Pragma
12555 (Full, No_Tagged_Streams_Pragma (Full_Base));
12557 -- Inherit class_wide type of full_base in case the partial view was
12558 -- not tagged. Otherwise it has already been created when the private
12559 -- subtype was analyzed.
12561 if No (Class_Wide_Type (Full)) then
12562 Set_Class_Wide_Type (Full, Class_Wide_Type (Full_Base));
12565 -- If this is a subtype of a protected or task type, constrain its
12566 -- corresponding record, unless this is a subtype without constraints,
12567 -- i.e. a simple renaming as with an actual subtype in an instance.
12569 elsif Is_Concurrent_Type (Full_Base) then
12570 if Has_Discriminants (Full)
12571 and then Present (Corresponding_Record_Type (Full_Base))
12573 not Is_Empty_Elmt_List (Discriminant_Constraint (Full))
12575 Set_Corresponding_Record_Type (Full,
12576 Constrain_Corresponding_Record
12577 (Full, Corresponding_Record_Type (Full_Base), Related_Nod));
12580 Set_Corresponding_Record_Type (Full,
12581 Corresponding_Record_Type (Full_Base));
12585 -- Link rep item chain, and also setting of Has_Predicates from private
12586 -- subtype to full subtype, since we will need these on the full subtype
12587 -- to create the predicate function. Note that the full subtype may
12588 -- already have rep items, inherited from the full view of the base
12589 -- type, so we must be sure not to overwrite these entries.
12594 Next_Item : Node_Id;
12595 Priv_Item : Node_Id;
12598 Item := First_Rep_Item (Full);
12599 Priv_Item := First_Rep_Item (Priv);
12601 -- If no existing rep items on full type, we can just link directly
12602 -- to the list of items on the private type, if any exist.. Same if
12603 -- the rep items are only those inherited from the base
12606 or else Nkind (Item) /= N_Aspect_Specification
12607 or else Entity (Item) = Full_Base)
12608 and then Present (First_Rep_Item (Priv))
12610 Set_First_Rep_Item (Full, Priv_Item);
12612 -- Otherwise, search to the end of items currently linked to the full
12613 -- subtype and append the private items to the end. However, if Priv
12614 -- and Full already have the same list of rep items, then the append
12615 -- is not done, as that would create a circularity.
12617 -- The partial view may have a predicate and the rep item lists of
12618 -- both views agree when inherited from the same ancestor. In that
12619 -- case, simply propagate the list from one view to the other.
12620 -- A more complex analysis needed here ???
12622 elsif Present (Priv_Item)
12623 and then Item = Next_Rep_Item (Priv_Item)
12625 Set_First_Rep_Item (Full, Priv_Item);
12627 elsif Item /= Priv_Item then
12630 Next_Item := Next_Rep_Item (Item);
12631 exit when No (Next_Item);
12634 -- If the private view has aspect specifications, the full view
12635 -- inherits them. Since these aspects may already have been
12636 -- attached to the full view during derivation, do not append
12637 -- them if already present.
12639 if Item = First_Rep_Item (Priv) then
12645 -- And link the private type items at the end of the chain
12648 Set_Next_Rep_Item (Item, First_Rep_Item (Priv));
12653 -- Make sure Has_Predicates is set on full type if it is set on the
12654 -- private type. Note that it may already be set on the full type and
12655 -- if so, we don't want to unset it. Similarly, propagate information
12656 -- about delayed aspects, because the corresponding pragmas must be
12657 -- analyzed when one of the views is frozen. This last step is needed
12658 -- in particular when the full type is a scalar type for which an
12659 -- anonymous base type is constructed.
12661 -- The predicate functions are generated either at the freeze point
12662 -- of the type or at the end of the visible part, and we must avoid
12663 -- generating them twice.
12665 if Has_Predicates (Priv) then
12666 Set_Has_Predicates (Full);
12668 if Present (Predicate_Function (Priv))
12669 and then No (Predicate_Function (Full))
12671 Set_Predicate_Function (Full, Predicate_Function (Priv));
12675 if Has_Delayed_Aspects (Priv) then
12676 Set_Has_Delayed_Aspects (Full);
12678 end Complete_Private_Subtype;
12680 ----------------------------
12681 -- Constant_Redeclaration --
12682 ----------------------------
12684 procedure Constant_Redeclaration
12689 Prev : constant Entity_Id := Current_Entity_In_Scope (Id);
12690 Obj_Def : constant Node_Id := Object_Definition (N);
12693 procedure Check_Possible_Deferred_Completion
12694 (Prev_Id : Entity_Id;
12695 Prev_Obj_Def : Node_Id;
12696 Curr_Obj_Def : Node_Id);
12697 -- Determine whether the two object definitions describe the partial
12698 -- and the full view of a constrained deferred constant. Generate
12699 -- a subtype for the full view and verify that it statically matches
12700 -- the subtype of the partial view.
12702 procedure Check_Recursive_Declaration (Typ : Entity_Id);
12703 -- If deferred constant is an access type initialized with an allocator,
12704 -- check whether there is an illegal recursion in the definition,
12705 -- through a default value of some record subcomponent. This is normally
12706 -- detected when generating init procs, but requires this additional
12707 -- mechanism when expansion is disabled.
12709 ----------------------------------------
12710 -- Check_Possible_Deferred_Completion --
12711 ----------------------------------------
12713 procedure Check_Possible_Deferred_Completion
12714 (Prev_Id : Entity_Id;
12715 Prev_Obj_Def : Node_Id;
12716 Curr_Obj_Def : Node_Id)
12719 if Nkind (Prev_Obj_Def) = N_Subtype_Indication
12720 and then Present (Constraint (Prev_Obj_Def))
12721 and then Nkind (Curr_Obj_Def) = N_Subtype_Indication
12722 and then Present (Constraint (Curr_Obj_Def))
12725 Loc : constant Source_Ptr := Sloc (N);
12726 Def_Id : constant Entity_Id := Make_Temporary (Loc, 'S');
12727 Decl : constant Node_Id :=
12728 Make_Subtype_Declaration (Loc,
12729 Defining_Identifier => Def_Id,
12730 Subtype_Indication =>
12731 Relocate_Node (Curr_Obj_Def));
12734 Insert_Before_And_Analyze (N, Decl);
12735 Set_Etype (Id, Def_Id);
12737 if not Subtypes_Statically_Match (Etype (Prev_Id), Def_Id) then
12738 Error_Msg_Sloc := Sloc (Prev_Id);
12739 Error_Msg_N ("subtype does not statically match deferred "
12740 & "declaration #", N);
12744 end Check_Possible_Deferred_Completion;
12746 ---------------------------------
12747 -- Check_Recursive_Declaration --
12748 ---------------------------------
12750 procedure Check_Recursive_Declaration (Typ : Entity_Id) is
12754 if Is_Record_Type (Typ) then
12755 Comp := First_Component (Typ);
12756 while Present (Comp) loop
12757 if Comes_From_Source (Comp) then
12758 if Present (Expression (Parent (Comp)))
12759 and then Is_Entity_Name (Expression (Parent (Comp)))
12760 and then Entity (Expression (Parent (Comp))) = Prev
12762 Error_Msg_Sloc := Sloc (Parent (Comp));
12764 ("illegal circularity with declaration for & #",
12768 elsif Is_Record_Type (Etype (Comp)) then
12769 Check_Recursive_Declaration (Etype (Comp));
12773 Next_Component (Comp);
12776 end Check_Recursive_Declaration;
12778 -- Start of processing for Constant_Redeclaration
12781 if Nkind (Parent (Prev)) = N_Object_Declaration then
12782 if Nkind (Object_Definition
12783 (Parent (Prev))) = N_Subtype_Indication
12785 -- Find type of new declaration. The constraints of the two
12786 -- views must match statically, but there is no point in
12787 -- creating an itype for the full view.
12789 if Nkind (Obj_Def) = N_Subtype_Indication then
12790 Find_Type (Subtype_Mark (Obj_Def));
12791 New_T := Entity (Subtype_Mark (Obj_Def));
12794 Find_Type (Obj_Def);
12795 New_T := Entity (Obj_Def);
12801 -- The full view may impose a constraint, even if the partial
12802 -- view does not, so construct the subtype.
12804 New_T := Find_Type_Of_Object (Obj_Def, N);
12809 -- Current declaration is illegal, diagnosed below in Enter_Name
12815 -- If previous full declaration or a renaming declaration exists, or if
12816 -- a homograph is present, let Enter_Name handle it, either with an
12817 -- error or with the removal of an overridden implicit subprogram.
12818 -- The previous one is a full declaration if it has an expression
12819 -- (which in the case of an aggregate is indicated by the Init flag).
12821 if Ekind (Prev) /= E_Constant
12822 or else Nkind (Parent (Prev)) = N_Object_Renaming_Declaration
12823 or else Present (Expression (Parent (Prev)))
12824 or else Has_Init_Expression (Parent (Prev))
12825 or else Present (Full_View (Prev))
12829 -- Verify that types of both declarations match, or else that both types
12830 -- are anonymous access types whose designated subtypes statically match
12831 -- (as allowed in Ada 2005 by AI-385).
12833 elsif Base_Type (Etype (Prev)) /= Base_Type (New_T)
12835 (Ekind (Etype (Prev)) /= E_Anonymous_Access_Type
12836 or else Ekind (Etype (New_T)) /= E_Anonymous_Access_Type
12837 or else Is_Access_Constant (Etype (New_T)) /=
12838 Is_Access_Constant (Etype (Prev))
12839 or else Can_Never_Be_Null (Etype (New_T)) /=
12840 Can_Never_Be_Null (Etype (Prev))
12841 or else Null_Exclusion_Present (Parent (Prev)) /=
12842 Null_Exclusion_Present (Parent (Id))
12843 or else not Subtypes_Statically_Match
12844 (Designated_Type (Etype (Prev)),
12845 Designated_Type (Etype (New_T))))
12847 Error_Msg_Sloc := Sloc (Prev);
12848 Error_Msg_N ("type does not match declaration#", N);
12849 Set_Full_View (Prev, Id);
12850 Set_Etype (Id, Any_Type);
12852 -- A deferred constant whose type is an anonymous array is always
12853 -- illegal (unless imported). A detailed error message might be
12854 -- helpful for Ada beginners.
12856 if Nkind (Object_Definition (Parent (Prev)))
12857 = N_Constrained_Array_Definition
12858 and then Nkind (Object_Definition (N))
12859 = N_Constrained_Array_Definition
12861 Error_Msg_N ("\each anonymous array is a distinct type", N);
12862 Error_Msg_N ("a deferred constant must have a named type",
12863 Object_Definition (Parent (Prev)));
12867 Null_Exclusion_Present (Parent (Prev))
12868 and then not Null_Exclusion_Present (N)
12870 Error_Msg_Sloc := Sloc (Prev);
12871 Error_Msg_N ("null-exclusion does not match declaration#", N);
12872 Set_Full_View (Prev, Id);
12873 Set_Etype (Id, Any_Type);
12875 -- If so, process the full constant declaration
12878 -- RM 7.4 (6): If the subtype defined by the subtype_indication in
12879 -- the deferred declaration is constrained, then the subtype defined
12880 -- by the subtype_indication in the full declaration shall match it
12883 Check_Possible_Deferred_Completion
12885 Prev_Obj_Def => Object_Definition (Parent (Prev)),
12886 Curr_Obj_Def => Obj_Def);
12888 Set_Full_View (Prev, Id);
12889 Set_Is_Public (Id, Is_Public (Prev));
12890 Set_Is_Internal (Id);
12891 Append_Entity (Id, Current_Scope);
12893 -- Check ALIASED present if present before (RM 7.4(7))
12895 if Is_Aliased (Prev)
12896 and then not Aliased_Present (N)
12898 Error_Msg_Sloc := Sloc (Prev);
12899 Error_Msg_N ("ALIASED required (see declaration #)", N);
12902 -- Check that placement is in private part and that the incomplete
12903 -- declaration appeared in the visible part.
12905 if Ekind (Current_Scope) = E_Package
12906 and then not In_Private_Part (Current_Scope)
12908 Error_Msg_Sloc := Sloc (Prev);
12910 ("full constant for declaration # must be in private part", N);
12912 elsif Ekind (Current_Scope) = E_Package
12914 List_Containing (Parent (Prev)) /=
12915 Visible_Declarations (Package_Specification (Current_Scope))
12918 ("deferred constant must be declared in visible part",
12922 if Is_Access_Type (T)
12923 and then Nkind (Expression (N)) = N_Allocator
12925 Check_Recursive_Declaration (Designated_Type (T));
12928 -- A deferred constant is a visible entity. If type has invariants,
12929 -- verify that the initial value satisfies them. This is not done in
12930 -- GNATprove mode, as GNATprove handles invariant checks itself.
12932 if Has_Invariants (T)
12933 and then Present (Invariant_Procedure (T))
12934 and then not GNATprove_Mode
12937 Make_Invariant_Call (New_Occurrence_Of (Prev, Sloc (N))));
12940 end Constant_Redeclaration;
12942 ----------------------
12943 -- Constrain_Access --
12944 ----------------------
12946 procedure Constrain_Access
12947 (Def_Id : in out Entity_Id;
12949 Related_Nod : Node_Id)
12951 T : constant Entity_Id := Entity (Subtype_Mark (S));
12952 Desig_Type : constant Entity_Id := Designated_Type (T);
12953 Desig_Subtype : Entity_Id := Create_Itype (E_Void, Related_Nod);
12954 Constraint_OK : Boolean := True;
12957 if Is_Array_Type (Desig_Type) then
12958 Constrain_Array (Desig_Subtype, S, Related_Nod, Def_Id, 'P');
12960 elsif (Is_Record_Type (Desig_Type)
12961 or else Is_Incomplete_Or_Private_Type (Desig_Type))
12962 and then not Is_Constrained (Desig_Type)
12964 -- ??? The following code is a temporary bypass to ignore a
12965 -- discriminant constraint on access type if it is constraining
12966 -- the current record. Avoid creating the implicit subtype of the
12967 -- record we are currently compiling since right now, we cannot
12968 -- handle these. For now, just return the access type itself.
12970 if Desig_Type = Current_Scope
12971 and then No (Def_Id)
12973 Set_Ekind (Desig_Subtype, E_Record_Subtype);
12974 Def_Id := Entity (Subtype_Mark (S));
12976 -- This call added to ensure that the constraint is analyzed
12977 -- (needed for a B test). Note that we still return early from
12978 -- this procedure to avoid recursive processing. ???
12980 Constrain_Discriminated_Type
12981 (Desig_Subtype, S, Related_Nod, For_Access => True);
12985 -- Enforce rule that the constraint is illegal if there is an
12986 -- unconstrained view of the designated type. This means that the
12987 -- partial view (either a private type declaration or a derivation
12988 -- from a private type) has no discriminants. (Defect Report
12989 -- 8652/0008, Technical Corrigendum 1, checked by ACATS B371001).
12991 -- Rule updated for Ada 2005: The private type is said to have
12992 -- a constrained partial view, given that objects of the type
12993 -- can be declared. Furthermore, the rule applies to all access
12994 -- types, unlike the rule concerning default discriminants (see
12997 if (Ekind (T) = E_General_Access_Type or else Ada_Version >= Ada_2005)
12998 and then Has_Private_Declaration (Desig_Type)
12999 and then In_Open_Scopes (Scope (Desig_Type))
13000 and then Has_Discriminants (Desig_Type)
13003 Pack : constant Node_Id :=
13004 Unit_Declaration_Node (Scope (Desig_Type));
13009 if Nkind (Pack) = N_Package_Declaration then
13010 Decls := Visible_Declarations (Specification (Pack));
13011 Decl := First (Decls);
13012 while Present (Decl) loop
13013 if (Nkind (Decl) = N_Private_Type_Declaration
13014 and then Chars (Defining_Identifier (Decl)) =
13015 Chars (Desig_Type))
13018 (Nkind (Decl) = N_Full_Type_Declaration
13020 Chars (Defining_Identifier (Decl)) =
13022 and then Is_Derived_Type (Desig_Type)
13024 Has_Private_Declaration (Etype (Desig_Type)))
13026 if No (Discriminant_Specifications (Decl)) then
13028 ("cannot constrain access type if designated "
13029 & "type has constrained partial view", S);
13041 Constrain_Discriminated_Type (Desig_Subtype, S, Related_Nod,
13042 For_Access => True);
13044 elsif Is_Concurrent_Type (Desig_Type)
13045 and then not Is_Constrained (Desig_Type)
13047 Constrain_Concurrent (Desig_Subtype, S, Related_Nod, Desig_Type, ' ');
13050 Error_Msg_N ("invalid constraint on access type", S);
13052 -- We simply ignore an invalid constraint
13054 Desig_Subtype := Desig_Type;
13055 Constraint_OK := False;
13058 if No (Def_Id) then
13059 Def_Id := Create_Itype (E_Access_Subtype, Related_Nod);
13061 Set_Ekind (Def_Id, E_Access_Subtype);
13064 if Constraint_OK then
13065 Set_Etype (Def_Id, Base_Type (T));
13067 if Is_Private_Type (Desig_Type) then
13068 Prepare_Private_Subtype_Completion (Desig_Subtype, Related_Nod);
13071 Set_Etype (Def_Id, Any_Type);
13074 Set_Size_Info (Def_Id, T);
13075 Set_Is_Constrained (Def_Id, Constraint_OK);
13076 Set_Directly_Designated_Type (Def_Id, Desig_Subtype);
13077 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13078 Set_Is_Access_Constant (Def_Id, Is_Access_Constant (T));
13080 Conditional_Delay (Def_Id, T);
13082 -- AI-363 : Subtypes of general access types whose designated types have
13083 -- default discriminants are disallowed. In instances, the rule has to
13084 -- be checked against the actual, of which T is the subtype. In a
13085 -- generic body, the rule is checked assuming that the actual type has
13086 -- defaulted discriminants.
13088 if Ada_Version >= Ada_2005 or else Warn_On_Ada_2005_Compatibility then
13089 if Ekind (Base_Type (T)) = E_General_Access_Type
13090 and then Has_Defaulted_Discriminants (Desig_Type)
13092 if Ada_Version < Ada_2005 then
13094 ("access subtype of general access type would not " &
13095 "be allowed in Ada 2005?y?", S);
13098 ("access subtype of general access type not allowed", S);
13101 Error_Msg_N ("\discriminants have defaults", S);
13103 elsif Is_Access_Type (T)
13104 and then Is_Generic_Type (Desig_Type)
13105 and then Has_Discriminants (Desig_Type)
13106 and then In_Package_Body (Current_Scope)
13108 if Ada_Version < Ada_2005 then
13110 ("access subtype would not be allowed in generic body "
13111 & "in Ada 2005?y?", S);
13114 ("access subtype not allowed in generic body", S);
13118 ("\designated type is a discriminated formal", S);
13121 end Constrain_Access;
13123 ---------------------
13124 -- Constrain_Array --
13125 ---------------------
13127 procedure Constrain_Array
13128 (Def_Id : in out Entity_Id;
13130 Related_Nod : Node_Id;
13131 Related_Id : Entity_Id;
13132 Suffix : Character)
13134 C : constant Node_Id := Constraint (SI);
13135 Number_Of_Constraints : Nat := 0;
13138 Constraint_OK : Boolean := True;
13141 T := Entity (Subtype_Mark (SI));
13143 if Is_Access_Type (T) then
13144 T := Designated_Type (T);
13147 -- If an index constraint follows a subtype mark in a subtype indication
13148 -- then the type or subtype denoted by the subtype mark must not already
13149 -- impose an index constraint. The subtype mark must denote either an
13150 -- unconstrained array type or an access type whose designated type
13151 -- is such an array type... (RM 3.6.1)
13153 if Is_Constrained (T) then
13154 Error_Msg_N ("array type is already constrained", Subtype_Mark (SI));
13155 Constraint_OK := False;
13158 S := First (Constraints (C));
13159 while Present (S) loop
13160 Number_Of_Constraints := Number_Of_Constraints + 1;
13164 -- In either case, the index constraint must provide a discrete
13165 -- range for each index of the array type and the type of each
13166 -- discrete range must be the same as that of the corresponding
13167 -- index. (RM 3.6.1)
13169 if Number_Of_Constraints /= Number_Dimensions (T) then
13170 Error_Msg_NE ("incorrect number of index constraints for }", C, T);
13171 Constraint_OK := False;
13174 S := First (Constraints (C));
13175 Index := First_Index (T);
13178 -- Apply constraints to each index type
13180 for J in 1 .. Number_Of_Constraints loop
13181 Constrain_Index (Index, S, Related_Nod, Related_Id, Suffix, J);
13189 if No (Def_Id) then
13191 Create_Itype (E_Array_Subtype, Related_Nod, Related_Id, Suffix);
13192 Set_Parent (Def_Id, Related_Nod);
13195 Set_Ekind (Def_Id, E_Array_Subtype);
13198 Set_Size_Info (Def_Id, (T));
13199 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13200 Set_Etype (Def_Id, Base_Type (T));
13202 if Constraint_OK then
13203 Set_First_Index (Def_Id, First (Constraints (C)));
13205 Set_First_Index (Def_Id, First_Index (T));
13208 Set_Is_Constrained (Def_Id, True);
13209 Set_Is_Aliased (Def_Id, Is_Aliased (T));
13210 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13212 Set_Is_Private_Composite (Def_Id, Is_Private_Composite (T));
13213 Set_Is_Limited_Composite (Def_Id, Is_Limited_Composite (T));
13215 -- A subtype does not inherit the Packed_Array_Impl_Type of is parent.
13216 -- We need to initialize the attribute because if Def_Id is previously
13217 -- analyzed through a limited_with clause, it will have the attributes
13218 -- of an incomplete type, one of which is an Elist that overlaps the
13219 -- Packed_Array_Impl_Type field.
13221 Set_Packed_Array_Impl_Type (Def_Id, Empty);
13223 -- Build a freeze node if parent still needs one. Also make sure that
13224 -- the Depends_On_Private status is set because the subtype will need
13225 -- reprocessing at the time the base type does, and also we must set a
13226 -- conditional delay.
13228 Set_Depends_On_Private (Def_Id, Depends_On_Private (T));
13229 Conditional_Delay (Def_Id, T);
13230 end Constrain_Array;
13232 ------------------------------
13233 -- Constrain_Component_Type --
13234 ------------------------------
13236 function Constrain_Component_Type
13238 Constrained_Typ : Entity_Id;
13239 Related_Node : Node_Id;
13241 Constraints : Elist_Id) return Entity_Id
13243 Loc : constant Source_Ptr := Sloc (Constrained_Typ);
13244 Compon_Type : constant Entity_Id := Etype (Comp);
13246 function Build_Constrained_Array_Type
13247 (Old_Type : Entity_Id) return Entity_Id;
13248 -- If Old_Type is an array type, one of whose indexes is constrained
13249 -- by a discriminant, build an Itype whose constraint replaces the
13250 -- discriminant with its value in the constraint.
13252 function Build_Constrained_Discriminated_Type
13253 (Old_Type : Entity_Id) return Entity_Id;
13254 -- Ditto for record components
13256 function Build_Constrained_Access_Type
13257 (Old_Type : Entity_Id) return Entity_Id;
13258 -- Ditto for access types. Makes use of previous two functions, to
13259 -- constrain designated type.
13261 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id;
13262 -- T is an array or discriminated type, C is a list of constraints
13263 -- that apply to T. This routine builds the constrained subtype.
13265 function Is_Discriminant (Expr : Node_Id) return Boolean;
13266 -- Returns True if Expr is a discriminant
13268 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id;
13269 -- Find the value of discriminant Discrim in Constraint
13271 -----------------------------------
13272 -- Build_Constrained_Access_Type --
13273 -----------------------------------
13275 function Build_Constrained_Access_Type
13276 (Old_Type : Entity_Id) return Entity_Id
13278 Desig_Type : constant Entity_Id := Designated_Type (Old_Type);
13280 Desig_Subtype : Entity_Id;
13284 -- if the original access type was not embedded in the enclosing
13285 -- type definition, there is no need to produce a new access
13286 -- subtype. In fact every access type with an explicit constraint
13287 -- generates an itype whose scope is the enclosing record.
13289 if not Is_Type (Scope (Old_Type)) then
13292 elsif Is_Array_Type (Desig_Type) then
13293 Desig_Subtype := Build_Constrained_Array_Type (Desig_Type);
13295 elsif Has_Discriminants (Desig_Type) then
13297 -- This may be an access type to an enclosing record type for
13298 -- which we are constructing the constrained components. Return
13299 -- the enclosing record subtype. This is not always correct,
13300 -- but avoids infinite recursion. ???
13302 Desig_Subtype := Any_Type;
13304 for J in reverse 0 .. Scope_Stack.Last loop
13305 Scop := Scope_Stack.Table (J).Entity;
13308 and then Base_Type (Scop) = Base_Type (Desig_Type)
13310 Desig_Subtype := Scop;
13313 exit when not Is_Type (Scop);
13316 if Desig_Subtype = Any_Type then
13318 Build_Constrained_Discriminated_Type (Desig_Type);
13325 if Desig_Subtype /= Desig_Type then
13327 -- The Related_Node better be here or else we won't be able
13328 -- to attach new itypes to a node in the tree.
13330 pragma Assert (Present (Related_Node));
13332 Itype := Create_Itype (E_Access_Subtype, Related_Node);
13334 Set_Etype (Itype, Base_Type (Old_Type));
13335 Set_Size_Info (Itype, (Old_Type));
13336 Set_Directly_Designated_Type (Itype, Desig_Subtype);
13337 Set_Depends_On_Private (Itype, Has_Private_Component
13339 Set_Is_Access_Constant (Itype, Is_Access_Constant
13342 -- The new itype needs freezing when it depends on a not frozen
13343 -- type and the enclosing subtype needs freezing.
13345 if Has_Delayed_Freeze (Constrained_Typ)
13346 and then not Is_Frozen (Constrained_Typ)
13348 Conditional_Delay (Itype, Base_Type (Old_Type));
13356 end Build_Constrained_Access_Type;
13358 ----------------------------------
13359 -- Build_Constrained_Array_Type --
13360 ----------------------------------
13362 function Build_Constrained_Array_Type
13363 (Old_Type : Entity_Id) return Entity_Id
13367 Old_Index : Node_Id;
13368 Range_Node : Node_Id;
13369 Constr_List : List_Id;
13371 Need_To_Create_Itype : Boolean := False;
13374 Old_Index := First_Index (Old_Type);
13375 while Present (Old_Index) loop
13376 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13378 if Is_Discriminant (Lo_Expr)
13380 Is_Discriminant (Hi_Expr)
13382 Need_To_Create_Itype := True;
13385 Next_Index (Old_Index);
13388 if Need_To_Create_Itype then
13389 Constr_List := New_List;
13391 Old_Index := First_Index (Old_Type);
13392 while Present (Old_Index) loop
13393 Get_Index_Bounds (Old_Index, Lo_Expr, Hi_Expr);
13395 if Is_Discriminant (Lo_Expr) then
13396 Lo_Expr := Get_Discr_Value (Lo_Expr);
13399 if Is_Discriminant (Hi_Expr) then
13400 Hi_Expr := Get_Discr_Value (Hi_Expr);
13405 (Loc, New_Copy_Tree (Lo_Expr), New_Copy_Tree (Hi_Expr));
13407 Append (Range_Node, To => Constr_List);
13409 Next_Index (Old_Index);
13412 return Build_Subtype (Old_Type, Constr_List);
13417 end Build_Constrained_Array_Type;
13419 ------------------------------------------
13420 -- Build_Constrained_Discriminated_Type --
13421 ------------------------------------------
13423 function Build_Constrained_Discriminated_Type
13424 (Old_Type : Entity_Id) return Entity_Id
13427 Constr_List : List_Id;
13428 Old_Constraint : Elmt_Id;
13430 Need_To_Create_Itype : Boolean := False;
13433 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13434 while Present (Old_Constraint) loop
13435 Expr := Node (Old_Constraint);
13437 if Is_Discriminant (Expr) then
13438 Need_To_Create_Itype := True;
13441 Next_Elmt (Old_Constraint);
13444 if Need_To_Create_Itype then
13445 Constr_List := New_List;
13447 Old_Constraint := First_Elmt (Discriminant_Constraint (Old_Type));
13448 while Present (Old_Constraint) loop
13449 Expr := Node (Old_Constraint);
13451 if Is_Discriminant (Expr) then
13452 Expr := Get_Discr_Value (Expr);
13455 Append (New_Copy_Tree (Expr), To => Constr_List);
13457 Next_Elmt (Old_Constraint);
13460 return Build_Subtype (Old_Type, Constr_List);
13465 end Build_Constrained_Discriminated_Type;
13467 -------------------
13468 -- Build_Subtype --
13469 -------------------
13471 function Build_Subtype (T : Entity_Id; C : List_Id) return Entity_Id is
13473 Subtyp_Decl : Node_Id;
13474 Def_Id : Entity_Id;
13475 Btyp : Entity_Id := Base_Type (T);
13478 -- The Related_Node better be here or else we won't be able to
13479 -- attach new itypes to a node in the tree.
13481 pragma Assert (Present (Related_Node));
13483 -- If the view of the component's type is incomplete or private
13484 -- with unknown discriminants, then the constraint must be applied
13485 -- to the full type.
13487 if Has_Unknown_Discriminants (Btyp)
13488 and then Present (Underlying_Type (Btyp))
13490 Btyp := Underlying_Type (Btyp);
13494 Make_Subtype_Indication (Loc,
13495 Subtype_Mark => New_Occurrence_Of (Btyp, Loc),
13496 Constraint => Make_Index_Or_Discriminant_Constraint (Loc, C));
13498 Def_Id := Create_Itype (Ekind (T), Related_Node);
13501 Make_Subtype_Declaration (Loc,
13502 Defining_Identifier => Def_Id,
13503 Subtype_Indication => Indic);
13505 Set_Parent (Subtyp_Decl, Parent (Related_Node));
13507 -- Itypes must be analyzed with checks off (see package Itypes)
13509 Analyze (Subtyp_Decl, Suppress => All_Checks);
13511 if Is_Itype (Def_Id) and then Has_Predicates (T) then
13512 Inherit_Predicate_Flags (Def_Id, T);
13514 -- Indicate where the predicate function may be found
13516 if Is_Itype (T) then
13517 if Present (Predicate_Function (Def_Id)) then
13520 elsif Present (Predicate_Function (T)) then
13521 Set_Predicate_Function (Def_Id, Predicate_Function (T));
13524 Set_Predicated_Parent (Def_Id, Predicated_Parent (T));
13527 elsif No (Predicate_Function (Def_Id)) then
13528 Set_Predicated_Parent (Def_Id, T);
13535 ---------------------
13536 -- Get_Discr_Value --
13537 ---------------------
13539 function Get_Discr_Value (Discrim : Entity_Id) return Node_Id is
13544 -- The discriminant may be declared for the type, in which case we
13545 -- find it by iterating over the list of discriminants. If the
13546 -- discriminant is inherited from a parent type, it appears as the
13547 -- corresponding discriminant of the current type. This will be the
13548 -- case when constraining an inherited component whose constraint is
13549 -- given by a discriminant of the parent.
13551 D := First_Discriminant (Typ);
13552 E := First_Elmt (Constraints);
13554 while Present (D) loop
13555 if D = Entity (Discrim)
13556 or else D = CR_Discriminant (Entity (Discrim))
13557 or else Corresponding_Discriminant (D) = Entity (Discrim)
13562 Next_Discriminant (D);
13566 -- The Corresponding_Discriminant mechanism is incomplete, because
13567 -- the correspondence between new and old discriminants is not one
13568 -- to one: one new discriminant can constrain several old ones. In
13569 -- that case, scan sequentially the stored_constraint, the list of
13570 -- discriminants of the parents, and the constraints.
13572 -- Previous code checked for the present of the Stored_Constraint
13573 -- list for the derived type, but did not use it at all. Should it
13574 -- be present when the component is a discriminated task type?
13576 if Is_Derived_Type (Typ)
13577 and then Scope (Entity (Discrim)) = Etype (Typ)
13579 D := First_Discriminant (Etype (Typ));
13580 E := First_Elmt (Constraints);
13581 while Present (D) loop
13582 if D = Entity (Discrim) then
13586 Next_Discriminant (D);
13591 -- Something is wrong if we did not find the value
13593 raise Program_Error;
13594 end Get_Discr_Value;
13596 ---------------------
13597 -- Is_Discriminant --
13598 ---------------------
13600 function Is_Discriminant (Expr : Node_Id) return Boolean is
13601 Discrim_Scope : Entity_Id;
13604 if Denotes_Discriminant (Expr) then
13605 Discrim_Scope := Scope (Entity (Expr));
13607 -- Either we have a reference to one of Typ's discriminants,
13609 pragma Assert (Discrim_Scope = Typ
13611 -- or to the discriminants of the parent type, in the case
13612 -- of a derivation of a tagged type with variants.
13614 or else Discrim_Scope = Etype (Typ)
13615 or else Full_View (Discrim_Scope) = Etype (Typ)
13617 -- or same as above for the case where the discriminants
13618 -- were declared in Typ's private view.
13620 or else (Is_Private_Type (Discrim_Scope)
13621 and then Chars (Discrim_Scope) = Chars (Typ))
13623 -- or else we are deriving from the full view and the
13624 -- discriminant is declared in the private entity.
13626 or else (Is_Private_Type (Typ)
13627 and then Chars (Discrim_Scope) = Chars (Typ))
13629 -- Or we are constrained the corresponding record of a
13630 -- synchronized type that completes a private declaration.
13632 or else (Is_Concurrent_Record_Type (Typ)
13634 Corresponding_Concurrent_Type (Typ) = Discrim_Scope)
13636 -- or we have a class-wide type, in which case make sure the
13637 -- discriminant found belongs to the root type.
13639 or else (Is_Class_Wide_Type (Typ)
13640 and then Etype (Typ) = Discrim_Scope));
13645 -- In all other cases we have something wrong
13648 end Is_Discriminant;
13650 -- Start of processing for Constrain_Component_Type
13653 if Nkind (Parent (Comp)) = N_Component_Declaration
13654 and then Comes_From_Source (Parent (Comp))
13655 and then Comes_From_Source
13656 (Subtype_Indication (Component_Definition (Parent (Comp))))
13659 (Subtype_Indication (Component_Definition (Parent (Comp))))
13661 return Compon_Type;
13663 elsif Is_Array_Type (Compon_Type) then
13664 return Build_Constrained_Array_Type (Compon_Type);
13666 elsif Has_Discriminants (Compon_Type) then
13667 return Build_Constrained_Discriminated_Type (Compon_Type);
13669 elsif Is_Access_Type (Compon_Type) then
13670 return Build_Constrained_Access_Type (Compon_Type);
13673 return Compon_Type;
13675 end Constrain_Component_Type;
13677 --------------------------
13678 -- Constrain_Concurrent --
13679 --------------------------
13681 -- For concurrent types, the associated record value type carries the same
13682 -- discriminants, so when we constrain a concurrent type, we must constrain
13683 -- the corresponding record type as well.
13685 procedure Constrain_Concurrent
13686 (Def_Id : in out Entity_Id;
13688 Related_Nod : Node_Id;
13689 Related_Id : Entity_Id;
13690 Suffix : Character)
13692 -- Retrieve Base_Type to ensure getting to the concurrent type in the
13693 -- case of a private subtype (needed when only doing semantic analysis).
13695 T_Ent : Entity_Id := Base_Type (Entity (Subtype_Mark (SI)));
13699 if Is_Access_Type (T_Ent) then
13700 T_Ent := Designated_Type (T_Ent);
13703 T_Val := Corresponding_Record_Type (T_Ent);
13705 if Present (T_Val) then
13707 if No (Def_Id) then
13708 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13710 -- Elaborate itype now, as it may be used in a subsequent
13711 -- synchronized operation in another scope.
13713 if Nkind (Related_Nod) = N_Full_Type_Declaration then
13714 Build_Itype_Reference (Def_Id, Related_Nod);
13718 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13719 Set_First_Private_Entity (Def_Id, First_Private_Entity (T_Ent));
13721 Set_Depends_On_Private (Def_Id, Has_Private_Component (Def_Id));
13722 Set_Corresponding_Record_Type (Def_Id,
13723 Constrain_Corresponding_Record (Def_Id, T_Val, Related_Nod));
13726 -- If there is no associated record, expansion is disabled and this
13727 -- is a generic context. Create a subtype in any case, so that
13728 -- semantic analysis can proceed.
13730 if No (Def_Id) then
13731 Def_Id := Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
13734 Constrain_Discriminated_Type (Def_Id, SI, Related_Nod);
13736 end Constrain_Concurrent;
13738 ------------------------------------
13739 -- Constrain_Corresponding_Record --
13740 ------------------------------------
13742 function Constrain_Corresponding_Record
13743 (Prot_Subt : Entity_Id;
13744 Corr_Rec : Entity_Id;
13745 Related_Nod : Node_Id) return Entity_Id
13747 T_Sub : constant Entity_Id :=
13749 (Ekind => E_Record_Subtype,
13750 Related_Nod => Related_Nod,
13751 Related_Id => Corr_Rec,
13753 Suffix_Index => -1);
13756 Set_Etype (T_Sub, Corr_Rec);
13757 Set_Has_Discriminants (T_Sub, Has_Discriminants (Prot_Subt));
13758 Set_Is_Constrained (T_Sub, True);
13759 Set_First_Entity (T_Sub, First_Entity (Corr_Rec));
13760 Set_Last_Entity (T_Sub, Last_Entity (Corr_Rec));
13762 if Has_Discriminants (Prot_Subt) then -- False only if errors.
13763 Set_Discriminant_Constraint
13764 (T_Sub, Discriminant_Constraint (Prot_Subt));
13765 Set_Stored_Constraint_From_Discriminant_Constraint (T_Sub);
13766 Create_Constrained_Components
13767 (T_Sub, Related_Nod, Corr_Rec, Discriminant_Constraint (T_Sub));
13770 Set_Depends_On_Private (T_Sub, Has_Private_Component (T_Sub));
13772 if Ekind (Scope (Prot_Subt)) /= E_Record_Type then
13773 Conditional_Delay (T_Sub, Corr_Rec);
13776 -- This is a component subtype: it will be frozen in the context of
13777 -- the enclosing record's init_proc, so that discriminant references
13778 -- are resolved to discriminals. (Note: we used to skip freezing
13779 -- altogether in that case, which caused errors downstream for
13780 -- components of a bit packed array type).
13782 Set_Has_Delayed_Freeze (T_Sub);
13786 end Constrain_Corresponding_Record;
13788 -----------------------
13789 -- Constrain_Decimal --
13790 -----------------------
13792 procedure Constrain_Decimal (Def_Id : Node_Id; S : Node_Id) is
13793 T : constant Entity_Id := Entity (Subtype_Mark (S));
13794 C : constant Node_Id := Constraint (S);
13795 Loc : constant Source_Ptr := Sloc (C);
13796 Range_Expr : Node_Id;
13797 Digits_Expr : Node_Id;
13802 Set_Ekind (Def_Id, E_Decimal_Fixed_Point_Subtype);
13804 if Nkind (C) = N_Range_Constraint then
13805 Range_Expr := Range_Expression (C);
13806 Digits_Val := Digits_Value (T);
13809 pragma Assert (Nkind (C) = N_Digits_Constraint);
13811 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
13813 Digits_Expr := Digits_Expression (C);
13814 Analyze_And_Resolve (Digits_Expr, Any_Integer);
13816 Check_Digits_Expression (Digits_Expr);
13817 Digits_Val := Expr_Value (Digits_Expr);
13819 if Digits_Val > Digits_Value (T) then
13821 ("digits expression is incompatible with subtype", C);
13822 Digits_Val := Digits_Value (T);
13825 if Present (Range_Constraint (C)) then
13826 Range_Expr := Range_Expression (Range_Constraint (C));
13828 Range_Expr := Empty;
13832 Set_Etype (Def_Id, Base_Type (T));
13833 Set_Size_Info (Def_Id, (T));
13834 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
13835 Set_Delta_Value (Def_Id, Delta_Value (T));
13836 Set_Scale_Value (Def_Id, Scale_Value (T));
13837 Set_Small_Value (Def_Id, Small_Value (T));
13838 Set_Machine_Radix_10 (Def_Id, Machine_Radix_10 (T));
13839 Set_Digits_Value (Def_Id, Digits_Val);
13841 -- Manufacture range from given digits value if no range present
13843 if No (Range_Expr) then
13844 Bound_Val := (Ureal_10 ** Digits_Val - Ureal_1) * Small_Value (T);
13848 Convert_To (T, Make_Real_Literal (Loc, (-Bound_Val))),
13850 Convert_To (T, Make_Real_Literal (Loc, Bound_Val)));
13853 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expr, T);
13854 Set_Discrete_RM_Size (Def_Id);
13856 -- Unconditionally delay the freeze, since we cannot set size
13857 -- information in all cases correctly until the freeze point.
13859 Set_Has_Delayed_Freeze (Def_Id);
13860 end Constrain_Decimal;
13862 ----------------------------------
13863 -- Constrain_Discriminated_Type --
13864 ----------------------------------
13866 procedure Constrain_Discriminated_Type
13867 (Def_Id : Entity_Id;
13869 Related_Nod : Node_Id;
13870 For_Access : Boolean := False)
13872 E : Entity_Id := Entity (Subtype_Mark (S));
13875 procedure Fixup_Bad_Constraint;
13876 -- Called after finding a bad constraint, and after having posted an
13877 -- appropriate error message. The goal is to leave type Def_Id in as
13878 -- reasonable state as possible.
13880 --------------------------
13881 -- Fixup_Bad_Constraint --
13882 --------------------------
13884 procedure Fixup_Bad_Constraint is
13886 -- Set a reasonable Ekind for the entity, including incomplete types.
13888 Set_Ekind (Def_Id, Subtype_Kind (Ekind (T)));
13890 -- Set Etype to the known type, to reduce chances of cascaded errors
13892 Set_Etype (Def_Id, E);
13893 Set_Error_Posted (Def_Id);
13894 end Fixup_Bad_Constraint;
13899 Constr : Elist_Id := New_Elmt_List;
13901 -- Start of processing for Constrain_Discriminated_Type
13904 C := Constraint (S);
13906 -- A discriminant constraint is only allowed in a subtype indication,
13907 -- after a subtype mark. This subtype mark must denote either a type
13908 -- with discriminants, or an access type whose designated type is a
13909 -- type with discriminants. A discriminant constraint specifies the
13910 -- values of these discriminants (RM 3.7.2(5)).
13912 T := Base_Type (Entity (Subtype_Mark (S)));
13914 if Is_Access_Type (T) then
13915 T := Designated_Type (T);
13918 -- In an instance it may be necessary to retrieve the full view of a
13919 -- type with unknown discriminants, or a full view with defaulted
13920 -- discriminants. In other contexts the constraint is illegal.
13923 and then Is_Private_Type (T)
13924 and then Present (Full_View (T))
13926 (Has_Unknown_Discriminants (T)
13928 (not Has_Discriminants (T)
13929 and then Has_Discriminants (Full_View (T))
13930 and then Present (Discriminant_Default_Value
13931 (First_Discriminant (Full_View (T))))))
13933 T := Full_View (T);
13934 E := Full_View (E);
13937 -- Ada 2005 (AI-412): Constrained incomplete subtypes are illegal. Avoid
13938 -- generating an error for access-to-incomplete subtypes.
13940 if Ada_Version >= Ada_2005
13941 and then Ekind (T) = E_Incomplete_Type
13942 and then Nkind (Parent (S)) = N_Subtype_Declaration
13943 and then not Is_Itype (Def_Id)
13945 -- A little sanity check: emit an error message if the type has
13946 -- discriminants to begin with. Type T may be a regular incomplete
13947 -- type or imported via a limited with clause.
13949 if Has_Discriminants (T)
13950 or else (From_Limited_With (T)
13951 and then Present (Non_Limited_View (T))
13952 and then Nkind (Parent (Non_Limited_View (T))) =
13953 N_Full_Type_Declaration
13954 and then Present (Discriminant_Specifications
13955 (Parent (Non_Limited_View (T)))))
13958 ("(Ada 2005) incomplete subtype may not be constrained", C);
13960 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13963 Fixup_Bad_Constraint;
13966 -- Check that the type has visible discriminants. The type may be
13967 -- a private type with unknown discriminants whose full view has
13968 -- discriminants which are invisible.
13970 elsif not Has_Discriminants (T)
13972 (Has_Unknown_Discriminants (T)
13973 and then Is_Private_Type (T))
13975 Error_Msg_N ("invalid constraint: type has no discriminant", C);
13976 Fixup_Bad_Constraint;
13979 elsif Is_Constrained (E)
13980 or else (Ekind (E) = E_Class_Wide_Subtype
13981 and then Present (Discriminant_Constraint (E)))
13983 Error_Msg_N ("type is already constrained", Subtype_Mark (S));
13984 Fixup_Bad_Constraint;
13988 -- T may be an unconstrained subtype (e.g. a generic actual). Constraint
13989 -- applies to the base type.
13991 T := Base_Type (T);
13993 Constr := Build_Discriminant_Constraints (T, S);
13995 -- If the list returned was empty we had an error in building the
13996 -- discriminant constraint. We have also already signalled an error
13997 -- in the incomplete type case
13999 if Is_Empty_Elmt_List (Constr) then
14000 Fixup_Bad_Constraint;
14004 Build_Discriminated_Subtype (T, Def_Id, Constr, Related_Nod, For_Access);
14005 end Constrain_Discriminated_Type;
14007 ---------------------------
14008 -- Constrain_Enumeration --
14009 ---------------------------
14011 procedure Constrain_Enumeration (Def_Id : Node_Id; S : Node_Id) is
14012 T : constant Entity_Id := Entity (Subtype_Mark (S));
14013 C : constant Node_Id := Constraint (S);
14016 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14018 Set_First_Literal (Def_Id, First_Literal (Base_Type (T)));
14020 Set_Etype (Def_Id, Base_Type (T));
14021 Set_Size_Info (Def_Id, (T));
14022 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14023 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14025 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14027 Set_Discrete_RM_Size (Def_Id);
14028 end Constrain_Enumeration;
14030 ----------------------
14031 -- Constrain_Float --
14032 ----------------------
14034 procedure Constrain_Float (Def_Id : Node_Id; S : Node_Id) is
14035 T : constant Entity_Id := Entity (Subtype_Mark (S));
14041 Set_Ekind (Def_Id, E_Floating_Point_Subtype);
14043 Set_Etype (Def_Id, Base_Type (T));
14044 Set_Size_Info (Def_Id, (T));
14045 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14047 -- Process the constraint
14049 C := Constraint (S);
14051 -- Digits constraint present
14053 if Nkind (C) = N_Digits_Constraint then
14055 Check_SPARK_05_Restriction ("digits constraint is not allowed", S);
14056 Check_Restriction (No_Obsolescent_Features, C);
14058 if Warn_On_Obsolescent_Feature then
14060 ("subtype digits constraint is an " &
14061 "obsolescent feature (RM J.3(8))?j?", C);
14064 D := Digits_Expression (C);
14065 Analyze_And_Resolve (D, Any_Integer);
14066 Check_Digits_Expression (D);
14067 Set_Digits_Value (Def_Id, Expr_Value (D));
14069 -- Check that digits value is in range. Obviously we can do this
14070 -- at compile time, but it is strictly a runtime check, and of
14071 -- course there is an ACVC test that checks this.
14073 if Digits_Value (Def_Id) > Digits_Value (T) then
14074 Error_Msg_Uint_1 := Digits_Value (T);
14075 Error_Msg_N ("??digits value is too large, maximum is ^", D);
14077 Make_Raise_Constraint_Error (Sloc (D),
14078 Reason => CE_Range_Check_Failed);
14079 Insert_Action (Declaration_Node (Def_Id), Rais);
14082 C := Range_Constraint (C);
14084 -- No digits constraint present
14087 Set_Digits_Value (Def_Id, Digits_Value (T));
14090 -- Range constraint present
14092 if Nkind (C) = N_Range_Constraint then
14093 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14095 -- No range constraint present
14098 pragma Assert (No (C));
14099 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14102 Set_Is_Constrained (Def_Id);
14103 end Constrain_Float;
14105 ---------------------
14106 -- Constrain_Index --
14107 ---------------------
14109 procedure Constrain_Index
14112 Related_Nod : Node_Id;
14113 Related_Id : Entity_Id;
14114 Suffix : Character;
14115 Suffix_Index : Nat)
14117 Def_Id : Entity_Id;
14118 R : Node_Id := Empty;
14119 T : constant Entity_Id := Etype (Index);
14123 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix, Suffix_Index);
14124 Set_Etype (Def_Id, Base_Type (T));
14126 if Nkind (S) = N_Range
14128 (Nkind (S) = N_Attribute_Reference
14129 and then Attribute_Name (S) = Name_Range)
14131 -- A Range attribute will be transformed into N_Range by Resolve
14137 Process_Range_Expr_In_Decl (R, T);
14139 if not Error_Posted (S)
14141 (Nkind (S) /= N_Range
14142 or else not Covers (T, (Etype (Low_Bound (S))))
14143 or else not Covers (T, (Etype (High_Bound (S)))))
14145 if Base_Type (T) /= Any_Type
14146 and then Etype (Low_Bound (S)) /= Any_Type
14147 and then Etype (High_Bound (S)) /= Any_Type
14149 Error_Msg_N ("range expected", S);
14153 elsif Nkind (S) = N_Subtype_Indication then
14155 -- The parser has verified that this is a discrete indication
14157 Resolve_Discrete_Subtype_Indication (S, T);
14158 Bad_Predicated_Subtype_Use
14159 ("subtype& has predicate, not allowed in index constraint",
14160 S, Entity (Subtype_Mark (S)));
14162 R := Range_Expression (Constraint (S));
14164 -- Capture values of bounds and generate temporaries for them if
14165 -- needed, since checks may cause duplication of the expressions
14166 -- which must not be reevaluated.
14168 -- The forced evaluation removes side effects from expressions, which
14169 -- should occur also in GNATprove mode. Otherwise, we end up with
14170 -- unexpected insertions of actions at places where this is not
14171 -- supposed to occur, e.g. on default parameters of a call.
14173 if Expander_Active or GNATprove_Mode then
14175 (Low_Bound (R), Related_Id => Def_Id, Is_Low_Bound => True);
14177 (High_Bound (R), Related_Id => Def_Id, Is_High_Bound => True);
14180 elsif Nkind (S) = N_Discriminant_Association then
14182 -- Syntactically valid in subtype indication
14184 Error_Msg_N ("invalid index constraint", S);
14185 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14188 -- Subtype_Mark case, no anonymous subtypes to construct
14193 if Is_Entity_Name (S) then
14194 if not Is_Type (Entity (S)) then
14195 Error_Msg_N ("expect subtype mark for index constraint", S);
14197 elsif Base_Type (Entity (S)) /= Base_Type (T) then
14198 Wrong_Type (S, Base_Type (T));
14200 -- Check error of subtype with predicate in index constraint
14203 Bad_Predicated_Subtype_Use
14204 ("subtype& has predicate, not allowed in index constraint",
14211 Error_Msg_N ("invalid index constraint", S);
14212 Rewrite (S, New_Occurrence_Of (T, Sloc (S)));
14217 -- Complete construction of the Itype
14219 if Is_Modular_Integer_Type (T) then
14220 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14222 elsif Is_Integer_Type (T) then
14223 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14226 Set_Ekind (Def_Id, E_Enumeration_Subtype);
14227 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
14228 Set_First_Literal (Def_Id, First_Literal (T));
14231 Set_Size_Info (Def_Id, (T));
14232 Set_RM_Size (Def_Id, RM_Size (T));
14233 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14235 Set_Scalar_Range (Def_Id, R);
14237 Set_Etype (S, Def_Id);
14238 Set_Discrete_RM_Size (Def_Id);
14239 end Constrain_Index;
14241 -----------------------
14242 -- Constrain_Integer --
14243 -----------------------
14245 procedure Constrain_Integer (Def_Id : Node_Id; S : Node_Id) is
14246 T : constant Entity_Id := Entity (Subtype_Mark (S));
14247 C : constant Node_Id := Constraint (S);
14250 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14252 if Is_Modular_Integer_Type (T) then
14253 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
14255 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
14258 Set_Etype (Def_Id, Base_Type (T));
14259 Set_Size_Info (Def_Id, (T));
14260 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14261 Set_Discrete_RM_Size (Def_Id);
14262 end Constrain_Integer;
14264 ------------------------------
14265 -- Constrain_Ordinary_Fixed --
14266 ------------------------------
14268 procedure Constrain_Ordinary_Fixed (Def_Id : Node_Id; S : Node_Id) is
14269 T : constant Entity_Id := Entity (Subtype_Mark (S));
14275 Set_Ekind (Def_Id, E_Ordinary_Fixed_Point_Subtype);
14276 Set_Etype (Def_Id, Base_Type (T));
14277 Set_Size_Info (Def_Id, (T));
14278 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
14279 Set_Small_Value (Def_Id, Small_Value (T));
14281 -- Process the constraint
14283 C := Constraint (S);
14285 -- Delta constraint present
14287 if Nkind (C) = N_Delta_Constraint then
14289 Check_SPARK_05_Restriction ("delta constraint is not allowed", S);
14290 Check_Restriction (No_Obsolescent_Features, C);
14292 if Warn_On_Obsolescent_Feature then
14294 ("subtype delta constraint is an " &
14295 "obsolescent feature (RM J.3(7))?j?");
14298 D := Delta_Expression (C);
14299 Analyze_And_Resolve (D, Any_Real);
14300 Check_Delta_Expression (D);
14301 Set_Delta_Value (Def_Id, Expr_Value_R (D));
14303 -- Check that delta value is in range. Obviously we can do this
14304 -- at compile time, but it is strictly a runtime check, and of
14305 -- course there is an ACVC test that checks this.
14307 if Delta_Value (Def_Id) < Delta_Value (T) then
14308 Error_Msg_N ("??delta value is too small", D);
14310 Make_Raise_Constraint_Error (Sloc (D),
14311 Reason => CE_Range_Check_Failed);
14312 Insert_Action (Declaration_Node (Def_Id), Rais);
14315 C := Range_Constraint (C);
14317 -- No delta constraint present
14320 Set_Delta_Value (Def_Id, Delta_Value (T));
14323 -- Range constraint present
14325 if Nkind (C) = N_Range_Constraint then
14326 Set_Scalar_Range_For_Subtype (Def_Id, Range_Expression (C), T);
14328 -- No range constraint present
14331 pragma Assert (No (C));
14332 Set_Scalar_Range (Def_Id, Scalar_Range (T));
14335 Set_Discrete_RM_Size (Def_Id);
14337 -- Unconditionally delay the freeze, since we cannot set size
14338 -- information in all cases correctly until the freeze point.
14340 Set_Has_Delayed_Freeze (Def_Id);
14341 end Constrain_Ordinary_Fixed;
14343 -----------------------
14344 -- Contain_Interface --
14345 -----------------------
14347 function Contain_Interface
14348 (Iface : Entity_Id;
14349 Ifaces : Elist_Id) return Boolean
14351 Iface_Elmt : Elmt_Id;
14354 if Present (Ifaces) then
14355 Iface_Elmt := First_Elmt (Ifaces);
14356 while Present (Iface_Elmt) loop
14357 if Node (Iface_Elmt) = Iface then
14361 Next_Elmt (Iface_Elmt);
14366 end Contain_Interface;
14368 ---------------------------
14369 -- Convert_Scalar_Bounds --
14370 ---------------------------
14372 procedure Convert_Scalar_Bounds
14374 Parent_Type : Entity_Id;
14375 Derived_Type : Entity_Id;
14378 Implicit_Base : constant Entity_Id := Base_Type (Derived_Type);
14385 -- Defend against previous errors
14387 if No (Scalar_Range (Derived_Type)) then
14388 Check_Error_Detected;
14392 Lo := Build_Scalar_Bound
14393 (Type_Low_Bound (Derived_Type),
14394 Parent_Type, Implicit_Base);
14396 Hi := Build_Scalar_Bound
14397 (Type_High_Bound (Derived_Type),
14398 Parent_Type, Implicit_Base);
14405 Set_Includes_Infinities (Rng, Has_Infinities (Derived_Type));
14407 Set_Parent (Rng, N);
14408 Set_Scalar_Range (Derived_Type, Rng);
14410 -- Analyze the bounds
14412 Analyze_And_Resolve (Lo, Implicit_Base);
14413 Analyze_And_Resolve (Hi, Implicit_Base);
14415 -- Analyze the range itself, except that we do not analyze it if
14416 -- the bounds are real literals, and we have a fixed-point type.
14417 -- The reason for this is that we delay setting the bounds in this
14418 -- case till we know the final Small and Size values (see circuit
14419 -- in Freeze.Freeze_Fixed_Point_Type for further details).
14421 if Is_Fixed_Point_Type (Parent_Type)
14422 and then Nkind (Lo) = N_Real_Literal
14423 and then Nkind (Hi) = N_Real_Literal
14427 -- Here we do the analysis of the range
14429 -- Note: we do this manually, since if we do a normal Analyze and
14430 -- Resolve call, there are problems with the conversions used for
14431 -- the derived type range.
14434 Set_Etype (Rng, Implicit_Base);
14435 Set_Analyzed (Rng, True);
14437 end Convert_Scalar_Bounds;
14439 -------------------
14440 -- Copy_And_Swap --
14441 -------------------
14443 procedure Copy_And_Swap (Priv, Full : Entity_Id) is
14445 -- Initialize new full declaration entity by copying the pertinent
14446 -- fields of the corresponding private declaration entity.
14448 -- We temporarily set Ekind to a value appropriate for a type to
14449 -- avoid assert failures in Einfo from checking for setting type
14450 -- attributes on something that is not a type. Ekind (Priv) is an
14451 -- appropriate choice, since it allowed the attributes to be set
14452 -- in the first place. This Ekind value will be modified later.
14454 Set_Ekind (Full, Ekind (Priv));
14456 -- Also set Etype temporarily to Any_Type, again, in the absence
14457 -- of errors, it will be properly reset, and if there are errors,
14458 -- then we want a value of Any_Type to remain.
14460 Set_Etype (Full, Any_Type);
14462 -- Now start copying attributes
14464 Set_Has_Discriminants (Full, Has_Discriminants (Priv));
14466 if Has_Discriminants (Full) then
14467 Set_Discriminant_Constraint (Full, Discriminant_Constraint (Priv));
14468 Set_Stored_Constraint (Full, Stored_Constraint (Priv));
14471 Set_First_Rep_Item (Full, First_Rep_Item (Priv));
14472 Set_Homonym (Full, Homonym (Priv));
14473 Set_Is_Immediately_Visible (Full, Is_Immediately_Visible (Priv));
14474 Set_Is_Public (Full, Is_Public (Priv));
14475 Set_Is_Pure (Full, Is_Pure (Priv));
14476 Set_Is_Tagged_Type (Full, Is_Tagged_Type (Priv));
14477 Set_Has_Pragma_Unmodified (Full, Has_Pragma_Unmodified (Priv));
14478 Set_Has_Pragma_Unreferenced (Full, Has_Pragma_Unreferenced (Priv));
14479 Set_Has_Pragma_Unreferenced_Objects
14480 (Full, Has_Pragma_Unreferenced_Objects
14483 Conditional_Delay (Full, Priv);
14485 if Is_Tagged_Type (Full) then
14486 Set_Direct_Primitive_Operations
14487 (Full, Direct_Primitive_Operations (Priv));
14488 Set_No_Tagged_Streams_Pragma
14489 (Full, No_Tagged_Streams_Pragma (Priv));
14491 if Is_Base_Type (Priv) then
14492 Set_Class_Wide_Type (Full, Class_Wide_Type (Priv));
14496 Set_Is_Volatile (Full, Is_Volatile (Priv));
14497 Set_Treat_As_Volatile (Full, Treat_As_Volatile (Priv));
14498 Set_Scope (Full, Scope (Priv));
14499 Set_Prev_Entity (Full, Prev_Entity (Priv));
14500 Set_Next_Entity (Full, Next_Entity (Priv));
14501 Set_First_Entity (Full, First_Entity (Priv));
14502 Set_Last_Entity (Full, Last_Entity (Priv));
14504 -- If access types have been recorded for later handling, keep them in
14505 -- the full view so that they get handled when the full view freeze
14506 -- node is expanded.
14508 if Present (Freeze_Node (Priv))
14509 and then Present (Access_Types_To_Process (Freeze_Node (Priv)))
14511 Ensure_Freeze_Node (Full);
14512 Set_Access_Types_To_Process
14513 (Freeze_Node (Full),
14514 Access_Types_To_Process (Freeze_Node (Priv)));
14517 -- Swap the two entities. Now Private is the full type entity and Full
14518 -- is the private one. They will be swapped back at the end of the
14519 -- private part. This swapping ensures that the entity that is visible
14520 -- in the private part is the full declaration.
14522 Exchange_Entities (Priv, Full);
14523 Append_Entity (Full, Scope (Full));
14526 -------------------------------------
14527 -- Copy_Array_Base_Type_Attributes --
14528 -------------------------------------
14530 procedure Copy_Array_Base_Type_Attributes (T1, T2 : Entity_Id) is
14532 Set_Component_Alignment (T1, Component_Alignment (T2));
14533 Set_Component_Type (T1, Component_Type (T2));
14534 Set_Component_Size (T1, Component_Size (T2));
14535 Set_Has_Controlled_Component (T1, Has_Controlled_Component (T2));
14536 Set_Has_Non_Standard_Rep (T1, Has_Non_Standard_Rep (T2));
14537 Propagate_Concurrent_Flags (T1, T2);
14538 Set_Is_Packed (T1, Is_Packed (T2));
14539 Set_Has_Aliased_Components (T1, Has_Aliased_Components (T2));
14540 Set_Has_Atomic_Components (T1, Has_Atomic_Components (T2));
14541 Set_Has_Volatile_Components (T1, Has_Volatile_Components (T2));
14542 end Copy_Array_Base_Type_Attributes;
14544 -----------------------------------
14545 -- Copy_Array_Subtype_Attributes --
14546 -----------------------------------
14548 procedure Copy_Array_Subtype_Attributes (T1, T2 : Entity_Id) is
14550 Set_Size_Info (T1, T2);
14552 Set_First_Index (T1, First_Index (T2));
14553 Set_Is_Aliased (T1, Is_Aliased (T2));
14554 Set_Is_Volatile (T1, Is_Volatile (T2));
14555 Set_Treat_As_Volatile (T1, Treat_As_Volatile (T2));
14556 Set_Is_Constrained (T1, Is_Constrained (T2));
14557 Set_Depends_On_Private (T1, Has_Private_Component (T2));
14558 Inherit_Rep_Item_Chain (T1, T2);
14559 Set_Convention (T1, Convention (T2));
14560 Set_Is_Limited_Composite (T1, Is_Limited_Composite (T2));
14561 Set_Is_Private_Composite (T1, Is_Private_Composite (T2));
14562 Set_Packed_Array_Impl_Type (T1, Packed_Array_Impl_Type (T2));
14563 end Copy_Array_Subtype_Attributes;
14565 -----------------------------------
14566 -- Create_Constrained_Components --
14567 -----------------------------------
14569 procedure Create_Constrained_Components
14571 Decl_Node : Node_Id;
14573 Constraints : Elist_Id)
14575 Loc : constant Source_Ptr := Sloc (Subt);
14576 Comp_List : constant Elist_Id := New_Elmt_List;
14577 Parent_Type : constant Entity_Id := Etype (Typ);
14578 Assoc_List : constant List_Id := New_List;
14579 Discr_Val : Elmt_Id;
14583 Is_Static : Boolean := True;
14585 procedure Collect_Fixed_Components (Typ : Entity_Id);
14586 -- Collect parent type components that do not appear in a variant part
14588 procedure Create_All_Components;
14589 -- Iterate over Comp_List to create the components of the subtype
14591 function Create_Component (Old_Compon : Entity_Id) return Entity_Id;
14592 -- Creates a new component from Old_Compon, copying all the fields from
14593 -- it, including its Etype, inserts the new component in the Subt entity
14594 -- chain and returns the new component.
14596 function Is_Variant_Record (T : Entity_Id) return Boolean;
14597 -- If true, and discriminants are static, collect only components from
14598 -- variants selected by discriminant values.
14600 ------------------------------
14601 -- Collect_Fixed_Components --
14602 ------------------------------
14604 procedure Collect_Fixed_Components (Typ : Entity_Id) is
14606 -- Build association list for discriminants, and find components of the
14607 -- variant part selected by the values of the discriminants.
14609 Old_C := First_Discriminant (Typ);
14610 Discr_Val := First_Elmt (Constraints);
14611 while Present (Old_C) loop
14612 Append_To (Assoc_List,
14613 Make_Component_Association (Loc,
14614 Choices => New_List (New_Occurrence_Of (Old_C, Loc)),
14615 Expression => New_Copy (Node (Discr_Val))));
14617 Next_Elmt (Discr_Val);
14618 Next_Discriminant (Old_C);
14621 -- The tag and the possible parent component are unconditionally in
14624 if Is_Tagged_Type (Typ) or else Has_Controlled_Component (Typ) then
14625 Old_C := First_Component (Typ);
14626 while Present (Old_C) loop
14627 if Nam_In (Chars (Old_C), Name_uTag, Name_uParent) then
14628 Append_Elmt (Old_C, Comp_List);
14631 Next_Component (Old_C);
14634 end Collect_Fixed_Components;
14636 ---------------------------
14637 -- Create_All_Components --
14638 ---------------------------
14640 procedure Create_All_Components is
14644 Comp := First_Elmt (Comp_List);
14645 while Present (Comp) loop
14646 Old_C := Node (Comp);
14647 New_C := Create_Component (Old_C);
14651 Constrain_Component_Type
14652 (Old_C, Subt, Decl_Node, Typ, Constraints));
14653 Set_Is_Public (New_C, Is_Public (Subt));
14657 end Create_All_Components;
14659 ----------------------
14660 -- Create_Component --
14661 ----------------------
14663 function Create_Component (Old_Compon : Entity_Id) return Entity_Id is
14664 New_Compon : constant Entity_Id := New_Copy (Old_Compon);
14667 if Ekind (Old_Compon) = E_Discriminant
14668 and then Is_Completely_Hidden (Old_Compon)
14670 -- This is a shadow discriminant created for a discriminant of
14671 -- the parent type, which needs to be present in the subtype.
14672 -- Give the shadow discriminant an internal name that cannot
14673 -- conflict with that of visible components.
14675 Set_Chars (New_Compon, New_Internal_Name ('C'));
14678 -- Set the parent so we have a proper link for freezing etc. This is
14679 -- not a real parent pointer, since of course our parent does not own
14680 -- up to us and reference us, we are an illegitimate child of the
14681 -- original parent.
14683 Set_Parent (New_Compon, Parent (Old_Compon));
14685 -- We do not want this node marked as Comes_From_Source, since
14686 -- otherwise it would get first class status and a separate cross-
14687 -- reference line would be generated. Illegitimate children do not
14688 -- rate such recognition.
14690 Set_Comes_From_Source (New_Compon, False);
14692 -- But it is a real entity, and a birth certificate must be properly
14693 -- registered by entering it into the entity list, and setting its
14694 -- scope to the given subtype. This turns out to be useful for the
14695 -- LLVM code generator, but that scope is not used otherwise.
14697 Enter_Name (New_Compon);
14698 Set_Scope (New_Compon, Subt);
14701 end Create_Component;
14703 -----------------------
14704 -- Is_Variant_Record --
14705 -----------------------
14707 function Is_Variant_Record (T : Entity_Id) return Boolean is
14709 return Nkind (Parent (T)) = N_Full_Type_Declaration
14710 and then Nkind (Type_Definition (Parent (T))) = N_Record_Definition
14711 and then Present (Component_List (Type_Definition (Parent (T))))
14714 (Variant_Part (Component_List (Type_Definition (Parent (T)))));
14715 end Is_Variant_Record;
14717 -- Start of processing for Create_Constrained_Components
14720 pragma Assert (Subt /= Base_Type (Subt));
14721 pragma Assert (Typ = Base_Type (Typ));
14723 Set_First_Entity (Subt, Empty);
14724 Set_Last_Entity (Subt, Empty);
14726 -- Check whether constraint is fully static, in which case we can
14727 -- optimize the list of components.
14729 Discr_Val := First_Elmt (Constraints);
14730 while Present (Discr_Val) loop
14731 if not Is_OK_Static_Expression (Node (Discr_Val)) then
14732 Is_Static := False;
14736 Next_Elmt (Discr_Val);
14739 Set_Has_Static_Discriminants (Subt, Is_Static);
14743 -- Inherit the discriminants of the parent type
14745 Add_Discriminants : declare
14751 Old_C := First_Discriminant (Typ);
14753 while Present (Old_C) loop
14754 Num_Disc := Num_Disc + 1;
14755 New_C := Create_Component (Old_C);
14756 Set_Is_Public (New_C, Is_Public (Subt));
14757 Next_Discriminant (Old_C);
14760 -- For an untagged derived subtype, the number of discriminants may
14761 -- be smaller than the number of inherited discriminants, because
14762 -- several of them may be renamed by a single new discriminant or
14763 -- constrained. In this case, add the hidden discriminants back into
14764 -- the subtype, because they need to be present if the optimizer of
14765 -- the GCC 4.x back-end decides to break apart assignments between
14766 -- objects using the parent view into member-wise assignments.
14770 if Is_Derived_Type (Typ)
14771 and then not Is_Tagged_Type (Typ)
14773 Old_C := First_Stored_Discriminant (Typ);
14775 while Present (Old_C) loop
14776 Num_Gird := Num_Gird + 1;
14777 Next_Stored_Discriminant (Old_C);
14781 if Num_Gird > Num_Disc then
14783 -- Find out multiple uses of new discriminants, and add hidden
14784 -- components for the extra renamed discriminants. We recognize
14785 -- multiple uses through the Corresponding_Discriminant of a
14786 -- new discriminant: if it constrains several old discriminants,
14787 -- this field points to the last one in the parent type. The
14788 -- stored discriminants of the derived type have the same name
14789 -- as those of the parent.
14793 New_Discr : Entity_Id;
14794 Old_Discr : Entity_Id;
14797 Constr := First_Elmt (Stored_Constraint (Typ));
14798 Old_Discr := First_Stored_Discriminant (Typ);
14799 while Present (Constr) loop
14800 if Is_Entity_Name (Node (Constr))
14801 and then Ekind (Entity (Node (Constr))) = E_Discriminant
14803 New_Discr := Entity (Node (Constr));
14805 if Chars (Corresponding_Discriminant (New_Discr)) /=
14808 -- The new discriminant has been used to rename a
14809 -- subsequent old discriminant. Introduce a shadow
14810 -- component for the current old discriminant.
14812 New_C := Create_Component (Old_Discr);
14813 Set_Original_Record_Component (New_C, Old_Discr);
14817 -- The constraint has eliminated the old discriminant.
14818 -- Introduce a shadow component.
14820 New_C := Create_Component (Old_Discr);
14821 Set_Original_Record_Component (New_C, Old_Discr);
14824 Next_Elmt (Constr);
14825 Next_Stored_Discriminant (Old_Discr);
14829 end Add_Discriminants;
14832 and then Is_Variant_Record (Typ)
14834 Collect_Fixed_Components (Typ);
14836 Gather_Components (
14838 Component_List (Type_Definition (Parent (Typ))),
14839 Governed_By => Assoc_List,
14841 Report_Errors => Errors);
14842 pragma Assert (not Errors
14843 or else Serious_Errors_Detected > 0);
14845 Create_All_Components;
14847 -- If the subtype declaration is created for a tagged type derivation
14848 -- with constraints, we retrieve the record definition of the parent
14849 -- type to select the components of the proper variant.
14852 and then Is_Tagged_Type (Typ)
14853 and then Nkind (Parent (Typ)) = N_Full_Type_Declaration
14855 Nkind (Type_Definition (Parent (Typ))) = N_Derived_Type_Definition
14856 and then Is_Variant_Record (Parent_Type)
14858 Collect_Fixed_Components (Typ);
14862 Component_List (Type_Definition (Parent (Parent_Type))),
14863 Governed_By => Assoc_List,
14865 Report_Errors => Errors);
14867 -- Note: previously there was a check at this point that no errors
14868 -- were detected. As a consequence of AI05-220 there may be an error
14869 -- if an inherited discriminant that controls a variant has a non-
14870 -- static constraint.
14872 -- If the tagged derivation has a type extension, collect all the
14873 -- new components therein.
14875 if Present (Record_Extension_Part (Type_Definition (Parent (Typ))))
14877 Old_C := First_Component (Typ);
14878 while Present (Old_C) loop
14879 if Original_Record_Component (Old_C) = Old_C
14880 and then Chars (Old_C) /= Name_uTag
14881 and then Chars (Old_C) /= Name_uParent
14883 Append_Elmt (Old_C, Comp_List);
14886 Next_Component (Old_C);
14890 Create_All_Components;
14893 -- If discriminants are not static, or if this is a multi-level type
14894 -- extension, we have to include all components of the parent type.
14896 Old_C := First_Component (Typ);
14897 while Present (Old_C) loop
14898 New_C := Create_Component (Old_C);
14902 Constrain_Component_Type
14903 (Old_C, Subt, Decl_Node, Typ, Constraints));
14904 Set_Is_Public (New_C, Is_Public (Subt));
14906 Next_Component (Old_C);
14911 end Create_Constrained_Components;
14913 ------------------------------------------
14914 -- Decimal_Fixed_Point_Type_Declaration --
14915 ------------------------------------------
14917 procedure Decimal_Fixed_Point_Type_Declaration
14921 Loc : constant Source_Ptr := Sloc (Def);
14922 Digs_Expr : constant Node_Id := Digits_Expression (Def);
14923 Delta_Expr : constant Node_Id := Delta_Expression (Def);
14924 Implicit_Base : Entity_Id;
14931 Check_SPARK_05_Restriction
14932 ("decimal fixed point type is not allowed", Def);
14933 Check_Restriction (No_Fixed_Point, Def);
14935 -- Create implicit base type
14938 Create_Itype (E_Decimal_Fixed_Point_Type, Parent (Def), T, 'B');
14939 Set_Etype (Implicit_Base, Implicit_Base);
14941 -- Analyze and process delta expression
14943 Analyze_And_Resolve (Delta_Expr, Universal_Real);
14945 Check_Delta_Expression (Delta_Expr);
14946 Delta_Val := Expr_Value_R (Delta_Expr);
14948 -- Check delta is power of 10, and determine scale value from it
14954 Scale_Val := Uint_0;
14957 if Val < Ureal_1 then
14958 while Val < Ureal_1 loop
14959 Val := Val * Ureal_10;
14960 Scale_Val := Scale_Val + 1;
14963 if Scale_Val > 18 then
14964 Error_Msg_N ("scale exceeds maximum value of 18", Def);
14965 Scale_Val := UI_From_Int (+18);
14969 while Val > Ureal_1 loop
14970 Val := Val / Ureal_10;
14971 Scale_Val := Scale_Val - 1;
14974 if Scale_Val < -18 then
14975 Error_Msg_N ("scale is less than minimum value of -18", Def);
14976 Scale_Val := UI_From_Int (-18);
14980 if Val /= Ureal_1 then
14981 Error_Msg_N ("delta expression must be a power of 10", Def);
14982 Delta_Val := Ureal_10 ** (-Scale_Val);
14986 -- Set delta, scale and small (small = delta for decimal type)
14988 Set_Delta_Value (Implicit_Base, Delta_Val);
14989 Set_Scale_Value (Implicit_Base, Scale_Val);
14990 Set_Small_Value (Implicit_Base, Delta_Val);
14992 -- Analyze and process digits expression
14994 Analyze_And_Resolve (Digs_Expr, Any_Integer);
14995 Check_Digits_Expression (Digs_Expr);
14996 Digs_Val := Expr_Value (Digs_Expr);
14998 if Digs_Val > 18 then
14999 Digs_Val := UI_From_Int (+18);
15000 Error_Msg_N ("digits value out of range, maximum is 18", Digs_Expr);
15003 Set_Digits_Value (Implicit_Base, Digs_Val);
15004 Bound_Val := UR_From_Uint (10 ** Digs_Val - 1) * Delta_Val;
15006 -- Set range of base type from digits value for now. This will be
15007 -- expanded to represent the true underlying base range by Freeze.
15009 Set_Fixed_Range (Implicit_Base, Loc, -Bound_Val, Bound_Val);
15011 -- Note: We leave size as zero for now, size will be set at freeze
15012 -- time. We have to do this for ordinary fixed-point, because the size
15013 -- depends on the specified small, and we might as well do the same for
15014 -- decimal fixed-point.
15016 pragma Assert (Esize (Implicit_Base) = Uint_0);
15018 -- If there are bounds given in the declaration use them as the
15019 -- bounds of the first named subtype.
15021 if Present (Real_Range_Specification (Def)) then
15023 RRS : constant Node_Id := Real_Range_Specification (Def);
15024 Low : constant Node_Id := Low_Bound (RRS);
15025 High : constant Node_Id := High_Bound (RRS);
15030 Analyze_And_Resolve (Low, Any_Real);
15031 Analyze_And_Resolve (High, Any_Real);
15032 Check_Real_Bound (Low);
15033 Check_Real_Bound (High);
15034 Low_Val := Expr_Value_R (Low);
15035 High_Val := Expr_Value_R (High);
15037 if Low_Val < (-Bound_Val) then
15039 ("range low bound too small for digits value", Low);
15040 Low_Val := -Bound_Val;
15043 if High_Val > Bound_Val then
15045 ("range high bound too large for digits value", High);
15046 High_Val := Bound_Val;
15049 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
15052 -- If no explicit range, use range that corresponds to given
15053 -- digits value. This will end up as the final range for the
15057 Set_Fixed_Range (T, Loc, -Bound_Val, Bound_Val);
15060 -- Complete entity for first subtype. The inheritance of the rep item
15061 -- chain ensures that SPARK-related pragmas are not clobbered when the
15062 -- decimal fixed point type acts as a full view of a private type.
15064 Set_Ekind (T, E_Decimal_Fixed_Point_Subtype);
15065 Set_Etype (T, Implicit_Base);
15066 Set_Size_Info (T, Implicit_Base);
15067 Inherit_Rep_Item_Chain (T, Implicit_Base);
15068 Set_Digits_Value (T, Digs_Val);
15069 Set_Delta_Value (T, Delta_Val);
15070 Set_Small_Value (T, Delta_Val);
15071 Set_Scale_Value (T, Scale_Val);
15072 Set_Is_Constrained (T);
15073 end Decimal_Fixed_Point_Type_Declaration;
15075 -----------------------------------
15076 -- Derive_Progenitor_Subprograms --
15077 -----------------------------------
15079 procedure Derive_Progenitor_Subprograms
15080 (Parent_Type : Entity_Id;
15081 Tagged_Type : Entity_Id)
15086 Iface_Alias : Entity_Id;
15087 Iface_Elmt : Elmt_Id;
15088 Iface_Subp : Entity_Id;
15089 New_Subp : Entity_Id := Empty;
15090 Prim_Elmt : Elmt_Id;
15095 pragma Assert (Ada_Version >= Ada_2005
15096 and then Is_Record_Type (Tagged_Type)
15097 and then Is_Tagged_Type (Tagged_Type)
15098 and then Has_Interfaces (Tagged_Type));
15100 -- Step 1: Transfer to the full-view primitives associated with the
15101 -- partial-view that cover interface primitives. Conceptually this
15102 -- work should be done later by Process_Full_View; done here to
15103 -- simplify its implementation at later stages. It can be safely
15104 -- done here because interfaces must be visible in the partial and
15105 -- private view (RM 7.3(7.3/2)).
15107 -- Small optimization: This work is only required if the parent may
15108 -- have entities whose Alias attribute reference an interface primitive.
15109 -- Such a situation may occur if the parent is an abstract type and the
15110 -- primitive has not been yet overridden or if the parent is a generic
15111 -- formal type covering interfaces.
15113 -- If the tagged type is not abstract, it cannot have abstract
15114 -- primitives (the only entities in the list of primitives of
15115 -- non-abstract tagged types that can reference abstract primitives
15116 -- through its Alias attribute are the internal entities that have
15117 -- attribute Interface_Alias, and these entities are generated later
15118 -- by Add_Internal_Interface_Entities).
15120 if In_Private_Part (Current_Scope)
15121 and then (Is_Abstract_Type (Parent_Type)
15123 Is_Generic_Type (Parent_Type))
15125 Elmt := First_Elmt (Primitive_Operations (Tagged_Type));
15126 while Present (Elmt) loop
15127 Subp := Node (Elmt);
15129 -- At this stage it is not possible to have entities in the list
15130 -- of primitives that have attribute Interface_Alias.
15132 pragma Assert (No (Interface_Alias (Subp)));
15134 Typ := Find_Dispatching_Type (Ultimate_Alias (Subp));
15136 if Is_Interface (Typ) then
15137 E := Find_Primitive_Covering_Interface
15138 (Tagged_Type => Tagged_Type,
15139 Iface_Prim => Subp);
15142 and then Find_Dispatching_Type (Ultimate_Alias (E)) /= Typ
15144 Replace_Elmt (Elmt, E);
15145 Remove_Homonym (Subp);
15153 -- Step 2: Add primitives of progenitors that are not implemented by
15154 -- parents of Tagged_Type.
15156 if Present (Interfaces (Base_Type (Tagged_Type))) then
15157 Iface_Elmt := First_Elmt (Interfaces (Base_Type (Tagged_Type)));
15158 while Present (Iface_Elmt) loop
15159 Iface := Node (Iface_Elmt);
15161 Prim_Elmt := First_Elmt (Primitive_Operations (Iface));
15162 while Present (Prim_Elmt) loop
15163 Iface_Subp := Node (Prim_Elmt);
15164 Iface_Alias := Ultimate_Alias (Iface_Subp);
15166 -- Exclude derivation of predefined primitives except those
15167 -- that come from source, or are inherited from one that comes
15168 -- from source. Required to catch declarations of equality
15169 -- operators of interfaces. For example:
15171 -- type Iface is interface;
15172 -- function "=" (Left, Right : Iface) return Boolean;
15174 if not Is_Predefined_Dispatching_Operation (Iface_Subp)
15175 or else Comes_From_Source (Iface_Alias)
15178 Find_Primitive_Covering_Interface
15179 (Tagged_Type => Tagged_Type,
15180 Iface_Prim => Iface_Subp);
15182 -- If not found we derive a new primitive leaving its alias
15183 -- attribute referencing the interface primitive.
15187 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15189 -- Ada 2012 (AI05-0197): If the covering primitive's name
15190 -- differs from the name of the interface primitive then it
15191 -- is a private primitive inherited from a parent type. In
15192 -- such case, given that Tagged_Type covers the interface,
15193 -- the inherited private primitive becomes visible. For such
15194 -- purpose we add a new entity that renames the inherited
15195 -- private primitive.
15197 elsif Chars (E) /= Chars (Iface_Subp) then
15198 pragma Assert (Has_Suffix (E, 'P'));
15200 (New_Subp, Iface_Subp, Tagged_Type, Iface);
15201 Set_Alias (New_Subp, E);
15202 Set_Is_Abstract_Subprogram (New_Subp,
15203 Is_Abstract_Subprogram (E));
15205 -- Propagate to the full view interface entities associated
15206 -- with the partial view.
15208 elsif In_Private_Part (Current_Scope)
15209 and then Present (Alias (E))
15210 and then Alias (E) = Iface_Subp
15212 List_Containing (Parent (E)) /=
15213 Private_Declarations
15215 (Unit_Declaration_Node (Current_Scope)))
15217 Append_Elmt (E, Primitive_Operations (Tagged_Type));
15221 Next_Elmt (Prim_Elmt);
15224 Next_Elmt (Iface_Elmt);
15227 end Derive_Progenitor_Subprograms;
15229 -----------------------
15230 -- Derive_Subprogram --
15231 -----------------------
15233 procedure Derive_Subprogram
15234 (New_Subp : out Entity_Id;
15235 Parent_Subp : Entity_Id;
15236 Derived_Type : Entity_Id;
15237 Parent_Type : Entity_Id;
15238 Actual_Subp : Entity_Id := Empty)
15240 Formal : Entity_Id;
15241 -- Formal parameter of parent primitive operation
15243 Formal_Of_Actual : Entity_Id;
15244 -- Formal parameter of actual operation, when the derivation is to
15245 -- create a renaming for a primitive operation of an actual in an
15248 New_Formal : Entity_Id;
15249 -- Formal of inherited operation
15251 Visible_Subp : Entity_Id := Parent_Subp;
15253 function Is_Private_Overriding return Boolean;
15254 -- If Subp is a private overriding of a visible operation, the inherited
15255 -- operation derives from the overridden op (even though its body is the
15256 -- overriding one) and the inherited operation is visible now. See
15257 -- sem_disp to see the full details of the handling of the overridden
15258 -- subprogram, which is removed from the list of primitive operations of
15259 -- the type. The overridden subprogram is saved locally in Visible_Subp,
15260 -- and used to diagnose abstract operations that need overriding in the
15263 procedure Replace_Type (Id, New_Id : Entity_Id);
15264 -- When the type is an anonymous access type, create a new access type
15265 -- designating the derived type.
15267 procedure Set_Derived_Name;
15268 -- This procedure sets the appropriate Chars name for New_Subp. This
15269 -- is normally just a copy of the parent name. An exception arises for
15270 -- type support subprograms, where the name is changed to reflect the
15271 -- name of the derived type, e.g. if type foo is derived from type bar,
15272 -- then a procedure barDA is derived with a name fooDA.
15274 ---------------------------
15275 -- Is_Private_Overriding --
15276 ---------------------------
15278 function Is_Private_Overriding return Boolean is
15282 -- If the parent is not a dispatching operation there is no
15283 -- need to investigate overridings
15285 if not Is_Dispatching_Operation (Parent_Subp) then
15289 -- The visible operation that is overridden is a homonym of the
15290 -- parent subprogram. We scan the homonym chain to find the one
15291 -- whose alias is the subprogram we are deriving.
15293 Prev := Current_Entity (Parent_Subp);
15294 while Present (Prev) loop
15295 if Ekind (Prev) = Ekind (Parent_Subp)
15296 and then Alias (Prev) = Parent_Subp
15297 and then Scope (Parent_Subp) = Scope (Prev)
15298 and then not Is_Hidden (Prev)
15300 Visible_Subp := Prev;
15304 Prev := Homonym (Prev);
15308 end Is_Private_Overriding;
15314 procedure Replace_Type (Id, New_Id : Entity_Id) is
15315 Id_Type : constant Entity_Id := Etype (Id);
15316 Acc_Type : Entity_Id;
15317 Par : constant Node_Id := Parent (Derived_Type);
15320 -- When the type is an anonymous access type, create a new access
15321 -- type designating the derived type. This itype must be elaborated
15322 -- at the point of the derivation, not on subsequent calls that may
15323 -- be out of the proper scope for Gigi, so we insert a reference to
15324 -- it after the derivation.
15326 if Ekind (Id_Type) = E_Anonymous_Access_Type then
15328 Desig_Typ : Entity_Id := Designated_Type (Id_Type);
15331 if Ekind (Desig_Typ) = E_Record_Type_With_Private
15332 and then Present (Full_View (Desig_Typ))
15333 and then not Is_Private_Type (Parent_Type)
15335 Desig_Typ := Full_View (Desig_Typ);
15338 if Base_Type (Desig_Typ) = Base_Type (Parent_Type)
15340 -- Ada 2005 (AI-251): Handle also derivations of abstract
15341 -- interface primitives.
15343 or else (Is_Interface (Desig_Typ)
15344 and then not Is_Class_Wide_Type (Desig_Typ))
15346 Acc_Type := New_Copy (Id_Type);
15347 Set_Etype (Acc_Type, Acc_Type);
15348 Set_Scope (Acc_Type, New_Subp);
15350 -- Set size of anonymous access type. If we have an access
15351 -- to an unconstrained array, this is a fat pointer, so it
15352 -- is sizes at twice addtress size.
15354 if Is_Array_Type (Desig_Typ)
15355 and then not Is_Constrained (Desig_Typ)
15357 Init_Size (Acc_Type, 2 * System_Address_Size);
15359 -- Other cases use a thin pointer
15362 Init_Size (Acc_Type, System_Address_Size);
15365 -- Set remaining characterstics of anonymous access type
15367 Init_Alignment (Acc_Type);
15368 Set_Directly_Designated_Type (Acc_Type, Derived_Type);
15370 Set_Etype (New_Id, Acc_Type);
15371 Set_Scope (New_Id, New_Subp);
15373 -- Create a reference to it
15375 Build_Itype_Reference (Acc_Type, Parent (Derived_Type));
15378 Set_Etype (New_Id, Id_Type);
15382 -- In Ada2012, a formal may have an incomplete type but the type
15383 -- derivation that inherits the primitive follows the full view.
15385 elsif Base_Type (Id_Type) = Base_Type (Parent_Type)
15387 (Ekind (Id_Type) = E_Record_Type_With_Private
15388 and then Present (Full_View (Id_Type))
15390 Base_Type (Full_View (Id_Type)) = Base_Type (Parent_Type))
15392 (Ada_Version >= Ada_2012
15393 and then Ekind (Id_Type) = E_Incomplete_Type
15394 and then Full_View (Id_Type) = Parent_Type)
15396 -- Constraint checks on formals are generated during expansion,
15397 -- based on the signature of the original subprogram. The bounds
15398 -- of the derived type are not relevant, and thus we can use
15399 -- the base type for the formals. However, the return type may be
15400 -- used in a context that requires that the proper static bounds
15401 -- be used (a case statement, for example) and for those cases
15402 -- we must use the derived type (first subtype), not its base.
15404 -- If the derived_type_definition has no constraints, we know that
15405 -- the derived type has the same constraints as the first subtype
15406 -- of the parent, and we can also use it rather than its base,
15407 -- which can lead to more efficient code.
15409 if Etype (Id) = Parent_Type then
15410 if Is_Scalar_Type (Parent_Type)
15412 Subtypes_Statically_Compatible (Parent_Type, Derived_Type)
15414 Set_Etype (New_Id, Derived_Type);
15416 elsif Nkind (Par) = N_Full_Type_Declaration
15418 Nkind (Type_Definition (Par)) = N_Derived_Type_Definition
15421 (Subtype_Indication (Type_Definition (Par)))
15423 Set_Etype (New_Id, Derived_Type);
15426 Set_Etype (New_Id, Base_Type (Derived_Type));
15430 Set_Etype (New_Id, Base_Type (Derived_Type));
15434 Set_Etype (New_Id, Etype (Id));
15438 ----------------------
15439 -- Set_Derived_Name --
15440 ----------------------
15442 procedure Set_Derived_Name is
15443 Nm : constant TSS_Name_Type := Get_TSS_Name (Parent_Subp);
15445 if Nm = TSS_Null then
15446 Set_Chars (New_Subp, Chars (Parent_Subp));
15448 Set_Chars (New_Subp, Make_TSS_Name (Base_Type (Derived_Type), Nm));
15450 end Set_Derived_Name;
15452 -- Start of processing for Derive_Subprogram
15455 New_Subp := New_Entity (Nkind (Parent_Subp), Sloc (Derived_Type));
15456 Set_Ekind (New_Subp, Ekind (Parent_Subp));
15458 -- Check whether the inherited subprogram is a private operation that
15459 -- should be inherited but not yet made visible. Such subprograms can
15460 -- become visible at a later point (e.g., the private part of a public
15461 -- child unit) via Declare_Inherited_Private_Subprograms. If the
15462 -- following predicate is true, then this is not such a private
15463 -- operation and the subprogram simply inherits the name of the parent
15464 -- subprogram. Note the special check for the names of controlled
15465 -- operations, which are currently exempted from being inherited with
15466 -- a hidden name because they must be findable for generation of
15467 -- implicit run-time calls.
15469 if not Is_Hidden (Parent_Subp)
15470 or else Is_Internal (Parent_Subp)
15471 or else Is_Private_Overriding
15472 or else Is_Internal_Name (Chars (Parent_Subp))
15473 or else (Is_Controlled (Parent_Type)
15474 and then Nam_In (Chars (Parent_Subp), Name_Adjust,
15480 -- An inherited dispatching equality will be overridden by an internally
15481 -- generated one, or by an explicit one, so preserve its name and thus
15482 -- its entry in the dispatch table. Otherwise, if Parent_Subp is a
15483 -- private operation it may become invisible if the full view has
15484 -- progenitors, and the dispatch table will be malformed.
15485 -- We check that the type is limited to handle the anomalous declaration
15486 -- of Limited_Controlled, which is derived from a non-limited type, and
15487 -- which is handled specially elsewhere as well.
15489 elsif Chars (Parent_Subp) = Name_Op_Eq
15490 and then Is_Dispatching_Operation (Parent_Subp)
15491 and then Etype (Parent_Subp) = Standard_Boolean
15492 and then not Is_Limited_Type (Etype (First_Formal (Parent_Subp)))
15494 Etype (First_Formal (Parent_Subp)) =
15495 Etype (Next_Formal (First_Formal (Parent_Subp)))
15499 -- If parent is hidden, this can be a regular derivation if the
15500 -- parent is immediately visible in a non-instantiating context,
15501 -- or if we are in the private part of an instance. This test
15502 -- should still be refined ???
15504 -- The test for In_Instance_Not_Visible avoids inheriting the derived
15505 -- operation as a non-visible operation in cases where the parent
15506 -- subprogram might not be visible now, but was visible within the
15507 -- original generic, so it would be wrong to make the inherited
15508 -- subprogram non-visible now. (Not clear if this test is fully
15509 -- correct; are there any cases where we should declare the inherited
15510 -- operation as not visible to avoid it being overridden, e.g., when
15511 -- the parent type is a generic actual with private primitives ???)
15513 -- (they should be treated the same as other private inherited
15514 -- subprograms, but it's not clear how to do this cleanly). ???
15516 elsif (In_Open_Scopes (Scope (Base_Type (Parent_Type)))
15517 and then Is_Immediately_Visible (Parent_Subp)
15518 and then not In_Instance)
15519 or else In_Instance_Not_Visible
15523 -- Ada 2005 (AI-251): Regular derivation if the parent subprogram
15524 -- overrides an interface primitive because interface primitives
15525 -- must be visible in the partial view of the parent (RM 7.3 (7.3/2))
15527 elsif Ada_Version >= Ada_2005
15528 and then Is_Dispatching_Operation (Parent_Subp)
15529 and then Present (Covered_Interface_Op (Parent_Subp))
15533 -- Otherwise, the type is inheriting a private operation, so enter it
15534 -- with a special name so it can't be overridden.
15537 Set_Chars (New_Subp, New_External_Name (Chars (Parent_Subp), 'P'));
15540 Set_Parent (New_Subp, Parent (Derived_Type));
15542 if Present (Actual_Subp) then
15543 Replace_Type (Actual_Subp, New_Subp);
15545 Replace_Type (Parent_Subp, New_Subp);
15548 Conditional_Delay (New_Subp, Parent_Subp);
15550 -- If we are creating a renaming for a primitive operation of an
15551 -- actual of a generic derived type, we must examine the signature
15552 -- of the actual primitive, not that of the generic formal, which for
15553 -- example may be an interface. However the name and initial value
15554 -- of the inherited operation are those of the formal primitive.
15556 Formal := First_Formal (Parent_Subp);
15558 if Present (Actual_Subp) then
15559 Formal_Of_Actual := First_Formal (Actual_Subp);
15561 Formal_Of_Actual := Empty;
15564 while Present (Formal) loop
15565 New_Formal := New_Copy (Formal);
15567 -- Normally we do not go copying parents, but in the case of
15568 -- formals, we need to link up to the declaration (which is the
15569 -- parameter specification), and it is fine to link up to the
15570 -- original formal's parameter specification in this case.
15572 Set_Parent (New_Formal, Parent (Formal));
15573 Append_Entity (New_Formal, New_Subp);
15575 if Present (Formal_Of_Actual) then
15576 Replace_Type (Formal_Of_Actual, New_Formal);
15577 Next_Formal (Formal_Of_Actual);
15579 Replace_Type (Formal, New_Formal);
15582 Next_Formal (Formal);
15585 -- If this derivation corresponds to a tagged generic actual, then
15586 -- primitive operations rename those of the actual. Otherwise the
15587 -- primitive operations rename those of the parent type, If the parent
15588 -- renames an intrinsic operator, so does the new subprogram. We except
15589 -- concatenation, which is always properly typed, and does not get
15590 -- expanded as other intrinsic operations.
15592 if No (Actual_Subp) then
15593 if Is_Intrinsic_Subprogram (Parent_Subp) then
15594 Set_Is_Intrinsic_Subprogram (New_Subp);
15596 if Present (Alias (Parent_Subp))
15597 and then Chars (Parent_Subp) /= Name_Op_Concat
15599 Set_Alias (New_Subp, Alias (Parent_Subp));
15601 Set_Alias (New_Subp, Parent_Subp);
15605 Set_Alias (New_Subp, Parent_Subp);
15609 Set_Alias (New_Subp, Actual_Subp);
15612 -- Derived subprograms of a tagged type must inherit the convention
15613 -- of the parent subprogram (a requirement of AI-117). Derived
15614 -- subprograms of untagged types simply get convention Ada by default.
15616 -- If the derived type is a tagged generic formal type with unknown
15617 -- discriminants, its convention is intrinsic (RM 6.3.1 (8)).
15619 -- However, if the type is derived from a generic formal, the further
15620 -- inherited subprogram has the convention of the non-generic ancestor.
15621 -- Otherwise there would be no way to override the operation.
15622 -- (This is subject to forthcoming ARG discussions).
15624 if Is_Tagged_Type (Derived_Type) then
15625 if Is_Generic_Type (Derived_Type)
15626 and then Has_Unknown_Discriminants (Derived_Type)
15628 Set_Convention (New_Subp, Convention_Intrinsic);
15631 if Is_Generic_Type (Parent_Type)
15632 and then Has_Unknown_Discriminants (Parent_Type)
15634 Set_Convention (New_Subp, Convention (Alias (Parent_Subp)));
15636 Set_Convention (New_Subp, Convention (Parent_Subp));
15641 -- Predefined controlled operations retain their name even if the parent
15642 -- is hidden (see above), but they are not primitive operations if the
15643 -- ancestor is not visible, for example if the parent is a private
15644 -- extension completed with a controlled extension. Note that a full
15645 -- type that is controlled can break privacy: the flag Is_Controlled is
15646 -- set on both views of the type.
15648 if Is_Controlled (Parent_Type)
15649 and then Nam_In (Chars (Parent_Subp), Name_Initialize,
15652 and then Is_Hidden (Parent_Subp)
15653 and then not Is_Visibly_Controlled (Parent_Type)
15655 Set_Is_Hidden (New_Subp);
15658 Set_Is_Imported (New_Subp, Is_Imported (Parent_Subp));
15659 Set_Is_Exported (New_Subp, Is_Exported (Parent_Subp));
15661 if Ekind (Parent_Subp) = E_Procedure then
15662 Set_Is_Valued_Procedure
15663 (New_Subp, Is_Valued_Procedure (Parent_Subp));
15665 Set_Has_Controlling_Result
15666 (New_Subp, Has_Controlling_Result (Parent_Subp));
15669 -- No_Return must be inherited properly. If this is overridden in the
15670 -- case of a dispatching operation, then a check is made in Sem_Disp
15671 -- that the overriding operation is also No_Return (no such check is
15672 -- required for the case of non-dispatching operation.
15674 Set_No_Return (New_Subp, No_Return (Parent_Subp));
15676 -- A derived function with a controlling result is abstract. If the
15677 -- Derived_Type is a nonabstract formal generic derived type, then
15678 -- inherited operations are not abstract: the required check is done at
15679 -- instantiation time. If the derivation is for a generic actual, the
15680 -- function is not abstract unless the actual is.
15682 if Is_Generic_Type (Derived_Type)
15683 and then not Is_Abstract_Type (Derived_Type)
15687 -- Ada 2005 (AI-228): Calculate the "require overriding" and "abstract"
15688 -- properties of the subprogram, as defined in RM-3.9.3(4/2-6/2).
15690 -- A subprogram subject to pragma Extensions_Visible with value False
15691 -- requires overriding if the subprogram has at least one controlling
15692 -- OUT parameter (SPARK RM 6.1.7(6)).
15694 elsif Ada_Version >= Ada_2005
15695 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15696 or else (Is_Tagged_Type (Derived_Type)
15697 and then Etype (New_Subp) = Derived_Type
15698 and then not Is_Null_Extension (Derived_Type))
15699 or else (Is_Tagged_Type (Derived_Type)
15700 and then Ekind (Etype (New_Subp)) =
15701 E_Anonymous_Access_Type
15702 and then Designated_Type (Etype (New_Subp)) =
15704 and then not Is_Null_Extension (Derived_Type))
15705 or else (Comes_From_Source (Alias (New_Subp))
15706 and then Is_EVF_Procedure (Alias (New_Subp))))
15707 and then No (Actual_Subp)
15709 if not Is_Tagged_Type (Derived_Type)
15710 or else Is_Abstract_Type (Derived_Type)
15711 or else Is_Abstract_Subprogram (Alias (New_Subp))
15713 Set_Is_Abstract_Subprogram (New_Subp);
15715 Set_Requires_Overriding (New_Subp);
15718 elsif Ada_Version < Ada_2005
15719 and then (Is_Abstract_Subprogram (Alias (New_Subp))
15720 or else (Is_Tagged_Type (Derived_Type)
15721 and then Etype (New_Subp) = Derived_Type
15722 and then No (Actual_Subp)))
15724 Set_Is_Abstract_Subprogram (New_Subp);
15726 -- AI05-0097 : an inherited operation that dispatches on result is
15727 -- abstract if the derived type is abstract, even if the parent type
15728 -- is concrete and the derived type is a null extension.
15730 elsif Has_Controlling_Result (Alias (New_Subp))
15731 and then Is_Abstract_Type (Etype (New_Subp))
15733 Set_Is_Abstract_Subprogram (New_Subp);
15735 -- Finally, if the parent type is abstract we must verify that all
15736 -- inherited operations are either non-abstract or overridden, or that
15737 -- the derived type itself is abstract (this check is performed at the
15738 -- end of a package declaration, in Check_Abstract_Overriding). A
15739 -- private overriding in the parent type will not be visible in the
15740 -- derivation if we are not in an inner package or in a child unit of
15741 -- the parent type, in which case the abstractness of the inherited
15742 -- operation is carried to the new subprogram.
15744 elsif Is_Abstract_Type (Parent_Type)
15745 and then not In_Open_Scopes (Scope (Parent_Type))
15746 and then Is_Private_Overriding
15747 and then Is_Abstract_Subprogram (Visible_Subp)
15749 if No (Actual_Subp) then
15750 Set_Alias (New_Subp, Visible_Subp);
15751 Set_Is_Abstract_Subprogram (New_Subp, True);
15754 -- If this is a derivation for an instance of a formal derived
15755 -- type, abstractness comes from the primitive operation of the
15756 -- actual, not from the operation inherited from the ancestor.
15758 Set_Is_Abstract_Subprogram
15759 (New_Subp, Is_Abstract_Subprogram (Actual_Subp));
15763 New_Overloaded_Entity (New_Subp, Derived_Type);
15765 -- Ada RM 6.1.1 (15): If a subprogram inherits nonconforming class-wide
15766 -- preconditions and the derived type is abstract, the derived operation
15767 -- is abstract as well if parent subprogram is not abstract or null.
15769 if Is_Abstract_Type (Derived_Type)
15770 and then Has_Non_Trivial_Precondition (Parent_Subp)
15771 and then Present (Interfaces (Derived_Type))
15774 -- Add useful attributes of subprogram before the freeze point,
15775 -- in case freezing is delayed or there are previous errors.
15777 Set_Is_Dispatching_Operation (New_Subp);
15780 Iface_Prim : constant Entity_Id := Covered_Interface_Op (New_Subp);
15783 if Present (Iface_Prim)
15784 and then Has_Non_Trivial_Precondition (Iface_Prim)
15786 Set_Is_Abstract_Subprogram (New_Subp);
15791 -- Check for case of a derived subprogram for the instantiation of a
15792 -- formal derived tagged type, if so mark the subprogram as dispatching
15793 -- and inherit the dispatching attributes of the actual subprogram. The
15794 -- derived subprogram is effectively renaming of the actual subprogram,
15795 -- so it needs to have the same attributes as the actual.
15797 if Present (Actual_Subp)
15798 and then Is_Dispatching_Operation (Actual_Subp)
15800 Set_Is_Dispatching_Operation (New_Subp);
15802 if Present (DTC_Entity (Actual_Subp)) then
15803 Set_DTC_Entity (New_Subp, DTC_Entity (Actual_Subp));
15804 Set_DT_Position_Value (New_Subp, DT_Position (Actual_Subp));
15808 -- Indicate that a derived subprogram does not require a body and that
15809 -- it does not require processing of default expressions.
15811 Set_Has_Completion (New_Subp);
15812 Set_Default_Expressions_Processed (New_Subp);
15814 if Ekind (New_Subp) = E_Function then
15815 Set_Mechanism (New_Subp, Mechanism (Parent_Subp));
15817 end Derive_Subprogram;
15819 ------------------------
15820 -- Derive_Subprograms --
15821 ------------------------
15823 procedure Derive_Subprograms
15824 (Parent_Type : Entity_Id;
15825 Derived_Type : Entity_Id;
15826 Generic_Actual : Entity_Id := Empty)
15828 Op_List : constant Elist_Id :=
15829 Collect_Primitive_Operations (Parent_Type);
15831 function Check_Derived_Type return Boolean;
15832 -- Check that all the entities derived from Parent_Type are found in
15833 -- the list of primitives of Derived_Type exactly in the same order.
15835 procedure Derive_Interface_Subprogram
15836 (New_Subp : out Entity_Id;
15838 Actual_Subp : Entity_Id);
15839 -- Derive New_Subp from the ultimate alias of the parent subprogram Subp
15840 -- (which is an interface primitive). If Generic_Actual is present then
15841 -- Actual_Subp is the actual subprogram corresponding with the generic
15842 -- subprogram Subp.
15844 ------------------------
15845 -- Check_Derived_Type --
15846 ------------------------
15848 function Check_Derived_Type return Boolean is
15852 New_Subp : Entity_Id;
15857 -- Traverse list of entities in the current scope searching for
15858 -- an incomplete type whose full-view is derived type.
15860 E := First_Entity (Scope (Derived_Type));
15861 while Present (E) and then E /= Derived_Type loop
15862 if Ekind (E) = E_Incomplete_Type
15863 and then Present (Full_View (E))
15864 and then Full_View (E) = Derived_Type
15866 -- Disable this test if Derived_Type completes an incomplete
15867 -- type because in such case more primitives can be added
15868 -- later to the list of primitives of Derived_Type by routine
15869 -- Process_Incomplete_Dependents
15874 E := Next_Entity (E);
15877 List := Collect_Primitive_Operations (Derived_Type);
15878 Elmt := First_Elmt (List);
15880 Op_Elmt := First_Elmt (Op_List);
15881 while Present (Op_Elmt) loop
15882 Subp := Node (Op_Elmt);
15883 New_Subp := Node (Elmt);
15885 -- At this early stage Derived_Type has no entities with attribute
15886 -- Interface_Alias. In addition, such primitives are always
15887 -- located at the end of the list of primitives of Parent_Type.
15888 -- Therefore, if found we can safely stop processing pending
15891 exit when Present (Interface_Alias (Subp));
15893 -- Handle hidden entities
15895 if not Is_Predefined_Dispatching_Operation (Subp)
15896 and then Is_Hidden (Subp)
15898 if Present (New_Subp)
15899 and then Primitive_Names_Match (Subp, New_Subp)
15905 if not Present (New_Subp)
15906 or else Ekind (Subp) /= Ekind (New_Subp)
15907 or else not Primitive_Names_Match (Subp, New_Subp)
15915 Next_Elmt (Op_Elmt);
15919 end Check_Derived_Type;
15921 ---------------------------------
15922 -- Derive_Interface_Subprogram --
15923 ---------------------------------
15925 procedure Derive_Interface_Subprogram
15926 (New_Subp : out Entity_Id;
15928 Actual_Subp : Entity_Id)
15930 Iface_Subp : constant Entity_Id := Ultimate_Alias (Subp);
15931 Iface_Type : constant Entity_Id := Find_Dispatching_Type (Iface_Subp);
15934 pragma Assert (Is_Interface (Iface_Type));
15937 (New_Subp => New_Subp,
15938 Parent_Subp => Iface_Subp,
15939 Derived_Type => Derived_Type,
15940 Parent_Type => Iface_Type,
15941 Actual_Subp => Actual_Subp);
15943 -- Given that this new interface entity corresponds with a primitive
15944 -- of the parent that was not overridden we must leave it associated
15945 -- with its parent primitive to ensure that it will share the same
15946 -- dispatch table slot when overridden. We must set the Alias to Subp
15947 -- (instead of Iface_Subp), and we must fix Is_Abstract_Subprogram
15948 -- (in case we inherited Subp from Iface_Type via a nonabstract
15949 -- generic formal type).
15951 if No (Actual_Subp) then
15952 Set_Alias (New_Subp, Subp);
15955 T : Entity_Id := Find_Dispatching_Type (Subp);
15957 while Etype (T) /= T loop
15958 if Is_Generic_Type (T) and then not Is_Abstract_Type (T) then
15959 Set_Is_Abstract_Subprogram (New_Subp, False);
15967 -- For instantiations this is not needed since the previous call to
15968 -- Derive_Subprogram leaves the entity well decorated.
15971 pragma Assert (Alias (New_Subp) = Actual_Subp);
15974 end Derive_Interface_Subprogram;
15978 Alias_Subp : Entity_Id;
15979 Act_List : Elist_Id;
15980 Act_Elmt : Elmt_Id;
15981 Act_Subp : Entity_Id := Empty;
15983 Need_Search : Boolean := False;
15984 New_Subp : Entity_Id := Empty;
15985 Parent_Base : Entity_Id;
15988 -- Start of processing for Derive_Subprograms
15991 if Ekind (Parent_Type) = E_Record_Type_With_Private
15992 and then Has_Discriminants (Parent_Type)
15993 and then Present (Full_View (Parent_Type))
15995 Parent_Base := Full_View (Parent_Type);
15997 Parent_Base := Parent_Type;
16000 if Present (Generic_Actual) then
16001 Act_List := Collect_Primitive_Operations (Generic_Actual);
16002 Act_Elmt := First_Elmt (Act_List);
16004 Act_List := No_Elist;
16005 Act_Elmt := No_Elmt;
16008 -- Derive primitives inherited from the parent. Note that if the generic
16009 -- actual is present, this is not really a type derivation, it is a
16010 -- completion within an instance.
16012 -- Case 1: Derived_Type does not implement interfaces
16014 if not Is_Tagged_Type (Derived_Type)
16015 or else (not Has_Interfaces (Derived_Type)
16016 and then not (Present (Generic_Actual)
16017 and then Has_Interfaces (Generic_Actual)))
16019 Elmt := First_Elmt (Op_List);
16020 while Present (Elmt) loop
16021 Subp := Node (Elmt);
16023 -- Literals are derived earlier in the process of building the
16024 -- derived type, and are skipped here.
16026 if Ekind (Subp) = E_Enumeration_Literal then
16029 -- The actual is a direct descendant and the common primitive
16030 -- operations appear in the same order.
16032 -- If the generic parent type is present, the derived type is an
16033 -- instance of a formal derived type, and within the instance its
16034 -- operations are those of the actual. We derive from the formal
16035 -- type but make the inherited operations aliases of the
16036 -- corresponding operations of the actual.
16039 pragma Assert (No (Node (Act_Elmt))
16040 or else (Primitive_Names_Match (Subp, Node (Act_Elmt))
16043 (Subp, Node (Act_Elmt),
16044 Skip_Controlling_Formals => True)));
16047 (New_Subp, Subp, Derived_Type, Parent_Base, Node (Act_Elmt));
16049 if Present (Act_Elmt) then
16050 Next_Elmt (Act_Elmt);
16057 -- Case 2: Derived_Type implements interfaces
16060 -- If the parent type has no predefined primitives we remove
16061 -- predefined primitives from the list of primitives of generic
16062 -- actual to simplify the complexity of this algorithm.
16064 if Present (Generic_Actual) then
16066 Has_Predefined_Primitives : Boolean := False;
16069 -- Check if the parent type has predefined primitives
16071 Elmt := First_Elmt (Op_List);
16072 while Present (Elmt) loop
16073 Subp := Node (Elmt);
16075 if Is_Predefined_Dispatching_Operation (Subp)
16076 and then not Comes_From_Source (Ultimate_Alias (Subp))
16078 Has_Predefined_Primitives := True;
16085 -- Remove predefined primitives of Generic_Actual. We must use
16086 -- an auxiliary list because in case of tagged types the value
16087 -- returned by Collect_Primitive_Operations is the value stored
16088 -- in its Primitive_Operations attribute (and we don't want to
16089 -- modify its current contents).
16091 if not Has_Predefined_Primitives then
16093 Aux_List : constant Elist_Id := New_Elmt_List;
16096 Elmt := First_Elmt (Act_List);
16097 while Present (Elmt) loop
16098 Subp := Node (Elmt);
16100 if not Is_Predefined_Dispatching_Operation (Subp)
16101 or else Comes_From_Source (Subp)
16103 Append_Elmt (Subp, Aux_List);
16109 Act_List := Aux_List;
16113 Act_Elmt := First_Elmt (Act_List);
16114 Act_Subp := Node (Act_Elmt);
16118 -- Stage 1: If the generic actual is not present we derive the
16119 -- primitives inherited from the parent type. If the generic parent
16120 -- type is present, the derived type is an instance of a formal
16121 -- derived type, and within the instance its operations are those of
16122 -- the actual. We derive from the formal type but make the inherited
16123 -- operations aliases of the corresponding operations of the actual.
16125 Elmt := First_Elmt (Op_List);
16126 while Present (Elmt) loop
16127 Subp := Node (Elmt);
16128 Alias_Subp := Ultimate_Alias (Subp);
16130 -- Do not derive internal entities of the parent that link
16131 -- interface primitives with their covering primitive. These
16132 -- entities will be added to this type when frozen.
16134 if Present (Interface_Alias (Subp)) then
16138 -- If the generic actual is present find the corresponding
16139 -- operation in the generic actual. If the parent type is a
16140 -- direct ancestor of the derived type then, even if it is an
16141 -- interface, the operations are inherited from the primary
16142 -- dispatch table and are in the proper order. If we detect here
16143 -- that primitives are not in the same order we traverse the list
16144 -- of primitive operations of the actual to find the one that
16145 -- implements the interface primitive.
16149 (Present (Generic_Actual)
16150 and then Present (Act_Subp)
16152 (Primitive_Names_Match (Subp, Act_Subp)
16154 Type_Conformant (Subp, Act_Subp,
16155 Skip_Controlling_Formals => True)))
16157 pragma Assert (not Is_Ancestor (Parent_Base, Generic_Actual,
16158 Use_Full_View => True));
16160 -- Remember that we need searching for all pending primitives
16162 Need_Search := True;
16164 -- Handle entities associated with interface primitives
16166 if Present (Alias_Subp)
16167 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16168 and then not Is_Predefined_Dispatching_Operation (Subp)
16170 -- Search for the primitive in the homonym chain
16173 Find_Primitive_Covering_Interface
16174 (Tagged_Type => Generic_Actual,
16175 Iface_Prim => Alias_Subp);
16177 -- Previous search may not locate primitives covering
16178 -- interfaces defined in generics units or instantiations.
16179 -- (it fails if the covering primitive has formals whose
16180 -- type is also defined in generics or instantiations).
16181 -- In such case we search in the list of primitives of the
16182 -- generic actual for the internal entity that links the
16183 -- interface primitive and the covering primitive.
16186 and then Is_Generic_Type (Parent_Type)
16188 -- This code has been designed to handle only generic
16189 -- formals that implement interfaces that are defined
16190 -- in a generic unit or instantiation. If this code is
16191 -- needed for other cases we must review it because
16192 -- (given that it relies on Original_Location to locate
16193 -- the primitive of Generic_Actual that covers the
16194 -- interface) it could leave linked through attribute
16195 -- Alias entities of unrelated instantiations).
16199 (Scope (Find_Dispatching_Type (Alias_Subp)))
16201 Instantiation_Depth
16202 (Sloc (Find_Dispatching_Type (Alias_Subp))) > 0);
16205 Iface_Prim_Loc : constant Source_Ptr :=
16206 Original_Location (Sloc (Alias_Subp));
16213 First_Elmt (Primitive_Operations (Generic_Actual));
16215 Search : while Present (Elmt) loop
16216 Prim := Node (Elmt);
16218 if Present (Interface_Alias (Prim))
16219 and then Original_Location
16220 (Sloc (Interface_Alias (Prim))) =
16223 Act_Subp := Alias (Prim);
16232 pragma Assert (Present (Act_Subp)
16233 or else Is_Abstract_Type (Generic_Actual)
16234 or else Serious_Errors_Detected > 0);
16236 -- Handle predefined primitives plus the rest of user-defined
16240 Act_Elmt := First_Elmt (Act_List);
16241 while Present (Act_Elmt) loop
16242 Act_Subp := Node (Act_Elmt);
16244 exit when Primitive_Names_Match (Subp, Act_Subp)
16245 and then Type_Conformant
16247 Skip_Controlling_Formals => True)
16248 and then No (Interface_Alias (Act_Subp));
16250 Next_Elmt (Act_Elmt);
16253 if No (Act_Elmt) then
16259 -- Case 1: If the parent is a limited interface then it has the
16260 -- predefined primitives of synchronized interfaces. However, the
16261 -- actual type may be a non-limited type and hence it does not
16262 -- have such primitives.
16264 if Present (Generic_Actual)
16265 and then not Present (Act_Subp)
16266 and then Is_Limited_Interface (Parent_Base)
16267 and then Is_Predefined_Interface_Primitive (Subp)
16271 -- Case 2: Inherit entities associated with interfaces that were
16272 -- not covered by the parent type. We exclude here null interface
16273 -- primitives because they do not need special management.
16275 -- We also exclude interface operations that are renamings. If the
16276 -- subprogram is an explicit renaming of an interface primitive,
16277 -- it is a regular primitive operation, and the presence of its
16278 -- alias is not relevant: it has to be derived like any other
16281 elsif Present (Alias (Subp))
16282 and then Nkind (Unit_Declaration_Node (Subp)) /=
16283 N_Subprogram_Renaming_Declaration
16284 and then Is_Interface (Find_Dispatching_Type (Alias_Subp))
16286 (Nkind (Parent (Alias_Subp)) = N_Procedure_Specification
16287 and then Null_Present (Parent (Alias_Subp)))
16289 -- If this is an abstract private type then we transfer the
16290 -- derivation of the interface primitive from the partial view
16291 -- to the full view. This is safe because all the interfaces
16292 -- must be visible in the partial view. Done to avoid adding
16293 -- a new interface derivation to the private part of the
16294 -- enclosing package; otherwise this new derivation would be
16295 -- decorated as hidden when the analysis of the enclosing
16296 -- package completes.
16298 if Is_Abstract_Type (Derived_Type)
16299 and then In_Private_Part (Current_Scope)
16300 and then Has_Private_Declaration (Derived_Type)
16303 Partial_View : Entity_Id;
16308 Partial_View := First_Entity (Current_Scope);
16310 exit when No (Partial_View)
16311 or else (Has_Private_Declaration (Partial_View)
16313 Full_View (Partial_View) = Derived_Type);
16315 Next_Entity (Partial_View);
16318 -- If the partial view was not found then the source code
16319 -- has errors and the derivation is not needed.
16321 if Present (Partial_View) then
16323 First_Elmt (Primitive_Operations (Partial_View));
16324 while Present (Elmt) loop
16325 Ent := Node (Elmt);
16327 if Present (Alias (Ent))
16328 and then Ultimate_Alias (Ent) = Alias (Subp)
16331 (Ent, Primitive_Operations (Derived_Type));
16338 -- If the interface primitive was not found in the
16339 -- partial view then this interface primitive was
16340 -- overridden. We add a derivation to activate in
16341 -- Derive_Progenitor_Subprograms the machinery to
16345 Derive_Interface_Subprogram
16346 (New_Subp => New_Subp,
16348 Actual_Subp => Act_Subp);
16353 Derive_Interface_Subprogram
16354 (New_Subp => New_Subp,
16356 Actual_Subp => Act_Subp);
16359 -- Case 3: Common derivation
16363 (New_Subp => New_Subp,
16364 Parent_Subp => Subp,
16365 Derived_Type => Derived_Type,
16366 Parent_Type => Parent_Base,
16367 Actual_Subp => Act_Subp);
16370 -- No need to update Act_Elm if we must search for the
16371 -- corresponding operation in the generic actual
16374 and then Present (Act_Elmt)
16376 Next_Elmt (Act_Elmt);
16377 Act_Subp := Node (Act_Elmt);
16384 -- Inherit additional operations from progenitors. If the derived
16385 -- type is a generic actual, there are not new primitive operations
16386 -- for the type because it has those of the actual, and therefore
16387 -- nothing needs to be done. The renamings generated above are not
16388 -- primitive operations, and their purpose is simply to make the
16389 -- proper operations visible within an instantiation.
16391 if No (Generic_Actual) then
16392 Derive_Progenitor_Subprograms (Parent_Base, Derived_Type);
16396 -- Final check: Direct descendants must have their primitives in the
16397 -- same order. We exclude from this test untagged types and instances
16398 -- of formal derived types. We skip this test if we have already
16399 -- reported serious errors in the sources.
16401 pragma Assert (not Is_Tagged_Type (Derived_Type)
16402 or else Present (Generic_Actual)
16403 or else Serious_Errors_Detected > 0
16404 or else Check_Derived_Type);
16405 end Derive_Subprograms;
16407 --------------------------------
16408 -- Derived_Standard_Character --
16409 --------------------------------
16411 procedure Derived_Standard_Character
16413 Parent_Type : Entity_Id;
16414 Derived_Type : Entity_Id)
16416 Loc : constant Source_Ptr := Sloc (N);
16417 Def : constant Node_Id := Type_Definition (N);
16418 Indic : constant Node_Id := Subtype_Indication (Def);
16419 Parent_Base : constant Entity_Id := Base_Type (Parent_Type);
16420 Implicit_Base : constant Entity_Id :=
16422 (E_Enumeration_Type, N, Derived_Type, 'B');
16428 Discard_Node (Process_Subtype (Indic, N));
16430 Set_Etype (Implicit_Base, Parent_Base);
16431 Set_Size_Info (Implicit_Base, Root_Type (Parent_Type));
16432 Set_RM_Size (Implicit_Base, RM_Size (Root_Type (Parent_Type)));
16434 Set_Is_Character_Type (Implicit_Base, True);
16435 Set_Has_Delayed_Freeze (Implicit_Base);
16437 -- The bounds of the implicit base are the bounds of the parent base.
16438 -- Note that their type is the parent base.
16440 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Base));
16441 Hi := New_Copy_Tree (Type_High_Bound (Parent_Base));
16443 Set_Scalar_Range (Implicit_Base,
16446 High_Bound => Hi));
16448 Conditional_Delay (Derived_Type, Parent_Type);
16450 Set_Ekind (Derived_Type, E_Enumeration_Subtype);
16451 Set_Etype (Derived_Type, Implicit_Base);
16452 Set_Size_Info (Derived_Type, Parent_Type);
16454 if Unknown_RM_Size (Derived_Type) then
16455 Set_RM_Size (Derived_Type, RM_Size (Parent_Type));
16458 Set_Is_Character_Type (Derived_Type, True);
16460 if Nkind (Indic) /= N_Subtype_Indication then
16462 -- If no explicit constraint, the bounds are those
16463 -- of the parent type.
16465 Lo := New_Copy_Tree (Type_Low_Bound (Parent_Type));
16466 Hi := New_Copy_Tree (Type_High_Bound (Parent_Type));
16467 Set_Scalar_Range (Derived_Type, Make_Range (Loc, Lo, Hi));
16470 Convert_Scalar_Bounds (N, Parent_Type, Derived_Type, Loc);
16472 -- Because the implicit base is used in the conversion of the bounds, we
16473 -- have to freeze it now. This is similar to what is done for numeric
16474 -- types, and it equally suspicious, but otherwise a nonstatic bound
16475 -- will have a reference to an unfrozen type, which is rejected by Gigi
16476 -- (???). This requires specific care for definition of stream
16477 -- attributes. For details, see comments at the end of
16478 -- Build_Derived_Numeric_Type.
16480 Freeze_Before (N, Implicit_Base);
16481 end Derived_Standard_Character;
16483 ------------------------------
16484 -- Derived_Type_Declaration --
16485 ------------------------------
16487 procedure Derived_Type_Declaration
16490 Is_Completion : Boolean)
16492 Parent_Type : Entity_Id;
16494 function Comes_From_Generic (Typ : Entity_Id) return Boolean;
16495 -- Check whether the parent type is a generic formal, or derives
16496 -- directly or indirectly from one.
16498 ------------------------
16499 -- Comes_From_Generic --
16500 ------------------------
16502 function Comes_From_Generic (Typ : Entity_Id) return Boolean is
16504 if Is_Generic_Type (Typ) then
16507 elsif Is_Generic_Type (Root_Type (Parent_Type)) then
16510 elsif Is_Private_Type (Typ)
16511 and then Present (Full_View (Typ))
16512 and then Is_Generic_Type (Root_Type (Full_View (Typ)))
16516 elsif Is_Generic_Actual_Type (Typ) then
16522 end Comes_From_Generic;
16526 Def : constant Node_Id := Type_Definition (N);
16527 Iface_Def : Node_Id;
16528 Indic : constant Node_Id := Subtype_Indication (Def);
16529 Extension : constant Node_Id := Record_Extension_Part (Def);
16530 Parent_Node : Node_Id;
16533 -- Start of processing for Derived_Type_Declaration
16536 Parent_Type := Find_Type_Of_Subtype_Indic (Indic);
16539 and then Is_Tagged_Type (Parent_Type)
16542 Partial_View : constant Entity_Id :=
16543 Incomplete_Or_Partial_View (Parent_Type);
16546 -- If the partial view was not found then the parent type is not
16547 -- a private type. Otherwise check if the partial view is a tagged
16550 if Present (Partial_View)
16551 and then Is_Private_Type (Partial_View)
16552 and then not Is_Tagged_Type (Partial_View)
16555 ("cannot derive from & declared as untagged private "
16556 & "(SPARK RM 3.4(1))", N, Partial_View);
16561 -- Ada 2005 (AI-251): In case of interface derivation check that the
16562 -- parent is also an interface.
16564 if Interface_Present (Def) then
16565 Check_SPARK_05_Restriction ("interface is not allowed", Def);
16567 if not Is_Interface (Parent_Type) then
16568 Diagnose_Interface (Indic, Parent_Type);
16571 Parent_Node := Parent (Base_Type (Parent_Type));
16572 Iface_Def := Type_Definition (Parent_Node);
16574 -- Ada 2005 (AI-251): Limited interfaces can only inherit from
16575 -- other limited interfaces.
16577 if Limited_Present (Def) then
16578 if Limited_Present (Iface_Def) then
16581 elsif Protected_Present (Iface_Def) then
16583 ("descendant of & must be declared as a protected "
16584 & "interface", N, Parent_Type);
16586 elsif Synchronized_Present (Iface_Def) then
16588 ("descendant of & must be declared as a synchronized "
16589 & "interface", N, Parent_Type);
16591 elsif Task_Present (Iface_Def) then
16593 ("descendant of & must be declared as a task interface",
16598 ("(Ada 2005) limited interface cannot inherit from "
16599 & "non-limited interface", Indic);
16602 -- Ada 2005 (AI-345): Non-limited interfaces can only inherit
16603 -- from non-limited or limited interfaces.
16605 elsif not Protected_Present (Def)
16606 and then not Synchronized_Present (Def)
16607 and then not Task_Present (Def)
16609 if Limited_Present (Iface_Def) then
16612 elsif Protected_Present (Iface_Def) then
16614 ("descendant of & must be declared as a protected "
16615 & "interface", N, Parent_Type);
16617 elsif Synchronized_Present (Iface_Def) then
16619 ("descendant of & must be declared as a synchronized "
16620 & "interface", N, Parent_Type);
16622 elsif Task_Present (Iface_Def) then
16624 ("descendant of & must be declared as a task interface",
16633 if Is_Tagged_Type (Parent_Type)
16634 and then Is_Concurrent_Type (Parent_Type)
16635 and then not Is_Interface (Parent_Type)
16638 ("parent type of a record extension cannot be a synchronized "
16639 & "tagged type (RM 3.9.1 (3/1))", N);
16640 Set_Etype (T, Any_Type);
16644 -- Ada 2005 (AI-251): Decorate all the names in the list of ancestor
16647 if Is_Tagged_Type (Parent_Type)
16648 and then Is_Non_Empty_List (Interface_List (Def))
16655 Intf := First (Interface_List (Def));
16656 while Present (Intf) loop
16657 T := Find_Type_Of_Subtype_Indic (Intf);
16659 if not Is_Interface (T) then
16660 Diagnose_Interface (Intf, T);
16662 -- Check the rules of 3.9.4(12/2) and 7.5(2/2) that disallow
16663 -- a limited type from having a nonlimited progenitor.
16665 elsif (Limited_Present (Def)
16666 or else (not Is_Interface (Parent_Type)
16667 and then Is_Limited_Type (Parent_Type)))
16668 and then not Is_Limited_Interface (T)
16671 ("progenitor interface& of limited type must be limited",
16680 if Parent_Type = Any_Type
16681 or else Etype (Parent_Type) = Any_Type
16682 or else (Is_Class_Wide_Type (Parent_Type)
16683 and then Etype (Parent_Type) = T)
16685 -- If Parent_Type is undefined or illegal, make new type into a
16686 -- subtype of Any_Type, and set a few attributes to prevent cascaded
16687 -- errors. If this is a self-definition, emit error now.
16689 if T = Parent_Type or else T = Etype (Parent_Type) then
16690 Error_Msg_N ("type cannot be used in its own definition", Indic);
16693 Set_Ekind (T, Ekind (Parent_Type));
16694 Set_Etype (T, Any_Type);
16695 Set_Scalar_Range (T, Scalar_Range (Any_Type));
16697 if Is_Tagged_Type (T)
16698 and then Is_Record_Type (T)
16700 Set_Direct_Primitive_Operations (T, New_Elmt_List);
16706 -- Ada 2005 (AI-251): The case in which the parent of the full-view is
16707 -- an interface is special because the list of interfaces in the full
16708 -- view can be given in any order. For example:
16710 -- type A is interface;
16711 -- type B is interface and A;
16712 -- type D is new B with private;
16714 -- type D is new A and B with null record; -- 1 --
16716 -- In this case we perform the following transformation of -1-:
16718 -- type D is new B and A with null record;
16720 -- If the parent of the full-view covers the parent of the partial-view
16721 -- we have two possible cases:
16723 -- 1) They have the same parent
16724 -- 2) The parent of the full-view implements some further interfaces
16726 -- In both cases we do not need to perform the transformation. In the
16727 -- first case the source program is correct and the transformation is
16728 -- not needed; in the second case the source program does not fulfill
16729 -- the no-hidden interfaces rule (AI-396) and the error will be reported
16732 -- This transformation not only simplifies the rest of the analysis of
16733 -- this type declaration but also simplifies the correct generation of
16734 -- the object layout to the expander.
16736 if In_Private_Part (Current_Scope)
16737 and then Is_Interface (Parent_Type)
16741 Partial_View : Entity_Id;
16742 Partial_View_Parent : Entity_Id;
16743 New_Iface : Node_Id;
16746 -- Look for the associated private type declaration
16748 Partial_View := Incomplete_Or_Partial_View (T);
16750 -- If the partial view was not found then the source code has
16751 -- errors and the transformation is not needed.
16753 if Present (Partial_View) then
16754 Partial_View_Parent := Etype (Partial_View);
16756 -- If the parent of the full-view covers the parent of the
16757 -- partial-view we have nothing else to do.
16759 if Interface_Present_In_Ancestor
16760 (Parent_Type, Partial_View_Parent)
16764 -- Traverse the list of interfaces of the full-view to look
16765 -- for the parent of the partial-view and perform the tree
16769 Iface := First (Interface_List (Def));
16770 while Present (Iface) loop
16771 if Etype (Iface) = Etype (Partial_View) then
16772 Rewrite (Subtype_Indication (Def),
16773 New_Copy (Subtype_Indication
16774 (Parent (Partial_View))));
16777 Make_Identifier (Sloc (N), Chars (Parent_Type));
16778 Append (New_Iface, Interface_List (Def));
16780 -- Analyze the transformed code
16782 Derived_Type_Declaration (T, N, Is_Completion);
16793 -- Only composite types other than array types are allowed to have
16796 if Present (Discriminant_Specifications (N)) then
16797 if (Is_Elementary_Type (Parent_Type)
16799 Is_Array_Type (Parent_Type))
16800 and then not Error_Posted (N)
16803 ("elementary or array type cannot have discriminants",
16804 Defining_Identifier (First (Discriminant_Specifications (N))));
16806 -- Unset Has_Discriminants flag to prevent cascaded errors, but
16807 -- only if we are not already processing a malformed syntax tree.
16809 if Is_Type (T) then
16810 Set_Has_Discriminants (T, False);
16813 -- The type is allowed to have discriminants
16816 Check_SPARK_05_Restriction ("discriminant type is not allowed", N);
16820 -- In Ada 83, a derived type defined in a package specification cannot
16821 -- be used for further derivation until the end of its visible part.
16822 -- Note that derivation in the private part of the package is allowed.
16824 if Ada_Version = Ada_83
16825 and then Is_Derived_Type (Parent_Type)
16826 and then In_Visible_Part (Scope (Parent_Type))
16828 if Ada_Version = Ada_83 and then Comes_From_Source (Indic) then
16830 ("(Ada 83): premature use of type for derivation", Indic);
16834 -- Check for early use of incomplete or private type
16836 if Ekind_In (Parent_Type, E_Void, E_Incomplete_Type) then
16837 Error_Msg_N ("premature derivation of incomplete type", Indic);
16840 elsif (Is_Incomplete_Or_Private_Type (Parent_Type)
16841 and then not Comes_From_Generic (Parent_Type))
16842 or else Has_Private_Component (Parent_Type)
16844 -- The ancestor type of a formal type can be incomplete, in which
16845 -- case only the operations of the partial view are available in the
16846 -- generic. Subsequent checks may be required when the full view is
16847 -- analyzed to verify that a derivation from a tagged type has an
16850 if Nkind (Original_Node (N)) = N_Formal_Type_Declaration then
16853 elsif No (Underlying_Type (Parent_Type))
16854 or else Has_Private_Component (Parent_Type)
16857 ("premature derivation of derived or private type", Indic);
16859 -- Flag the type itself as being in error, this prevents some
16860 -- nasty problems with subsequent uses of the malformed type.
16862 Set_Error_Posted (T);
16864 -- Check that within the immediate scope of an untagged partial
16865 -- view it's illegal to derive from the partial view if the
16866 -- full view is tagged. (7.3(7))
16868 -- We verify that the Parent_Type is a partial view by checking
16869 -- that it is not a Full_Type_Declaration (i.e. a private type or
16870 -- private extension declaration), to distinguish a partial view
16871 -- from a derivation from a private type which also appears as
16872 -- E_Private_Type. If the parent base type is not declared in an
16873 -- enclosing scope there is no need to check.
16875 elsif Present (Full_View (Parent_Type))
16876 and then Nkind (Parent (Parent_Type)) /= N_Full_Type_Declaration
16877 and then not Is_Tagged_Type (Parent_Type)
16878 and then Is_Tagged_Type (Full_View (Parent_Type))
16879 and then In_Open_Scopes (Scope (Base_Type (Parent_Type)))
16882 ("premature derivation from type with tagged full view",
16887 -- Check that form of derivation is appropriate
16889 Taggd := Is_Tagged_Type (Parent_Type);
16891 -- Set the parent type to the class-wide type's specific type in this
16892 -- case to prevent cascading errors
16894 if Present (Extension) and then Is_Class_Wide_Type (Parent_Type) then
16895 Error_Msg_N ("parent type must not be a class-wide type", Indic);
16896 Set_Etype (T, Etype (Parent_Type));
16900 if Present (Extension) and then not Taggd then
16902 ("type derived from untagged type cannot have extension", Indic);
16904 elsif No (Extension) and then Taggd then
16906 -- If this declaration is within a private part (or body) of a
16907 -- generic instantiation then the derivation is allowed (the parent
16908 -- type can only appear tagged in this case if it's a generic actual
16909 -- type, since it would otherwise have been rejected in the analysis
16910 -- of the generic template).
16912 if not Is_Generic_Actual_Type (Parent_Type)
16913 or else In_Visible_Part (Scope (Parent_Type))
16915 if Is_Class_Wide_Type (Parent_Type) then
16917 ("parent type must not be a class-wide type", Indic);
16919 -- Use specific type to prevent cascaded errors.
16921 Parent_Type := Etype (Parent_Type);
16925 ("type derived from tagged type must have extension", Indic);
16930 -- AI-443: Synchronized formal derived types require a private
16931 -- extension. There is no point in checking the ancestor type or
16932 -- the progenitors since the construct is wrong to begin with.
16934 if Ada_Version >= Ada_2005
16935 and then Is_Generic_Type (T)
16936 and then Present (Original_Node (N))
16939 Decl : constant Node_Id := Original_Node (N);
16942 if Nkind (Decl) = N_Formal_Type_Declaration
16943 and then Nkind (Formal_Type_Definition (Decl)) =
16944 N_Formal_Derived_Type_Definition
16945 and then Synchronized_Present (Formal_Type_Definition (Decl))
16946 and then No (Extension)
16948 -- Avoid emitting a duplicate error message
16950 and then not Error_Posted (Indic)
16953 ("synchronized derived type must have extension", N);
16958 if Null_Exclusion_Present (Def)
16959 and then not Is_Access_Type (Parent_Type)
16961 Error_Msg_N ("null exclusion can only apply to an access type", N);
16964 -- Avoid deriving parent primitives of underlying record views
16966 Build_Derived_Type (N, Parent_Type, T, Is_Completion,
16967 Derive_Subps => not Is_Underlying_Record_View (T));
16969 -- AI-419: The parent type of an explicitly limited derived type must
16970 -- be a limited type or a limited interface.
16972 if Limited_Present (Def) then
16973 Set_Is_Limited_Record (T);
16975 if Is_Interface (T) then
16976 Set_Is_Limited_Interface (T);
16979 if not Is_Limited_Type (Parent_Type)
16981 (not Is_Interface (Parent_Type)
16982 or else not Is_Limited_Interface (Parent_Type))
16984 -- AI05-0096: a derivation in the private part of an instance is
16985 -- legal if the generic formal is untagged limited, and the actual
16988 if Is_Generic_Actual_Type (Parent_Type)
16989 and then In_Private_Part (Current_Scope)
16992 (Generic_Parent_Type (Parent (Parent_Type)))
16998 ("parent type& of limited type must be limited",
17004 -- In SPARK, there are no derived type definitions other than type
17005 -- extensions of tagged record types.
17007 if No (Extension) then
17008 Check_SPARK_05_Restriction
17009 ("derived type is not allowed", Original_Node (N));
17011 end Derived_Type_Declaration;
17013 ------------------------
17014 -- Diagnose_Interface --
17015 ------------------------
17017 procedure Diagnose_Interface (N : Node_Id; E : Entity_Id) is
17019 if not Is_Interface (E) and then E /= Any_Type then
17020 Error_Msg_NE ("(Ada 2005) & must be an interface", N, E);
17022 end Diagnose_Interface;
17024 ----------------------------------
17025 -- Enumeration_Type_Declaration --
17026 ----------------------------------
17028 procedure Enumeration_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17035 -- Create identifier node representing lower bound
17037 B_Node := New_Node (N_Identifier, Sloc (Def));
17038 L := First (Literals (Def));
17039 Set_Chars (B_Node, Chars (L));
17040 Set_Entity (B_Node, L);
17041 Set_Etype (B_Node, T);
17042 Set_Is_Static_Expression (B_Node, True);
17044 R_Node := New_Node (N_Range, Sloc (Def));
17045 Set_Low_Bound (R_Node, B_Node);
17047 Set_Ekind (T, E_Enumeration_Type);
17048 Set_First_Literal (T, L);
17050 Set_Is_Constrained (T);
17054 -- Loop through literals of enumeration type setting pos and rep values
17055 -- except that if the Ekind is already set, then it means the literal
17056 -- was already constructed (case of a derived type declaration and we
17057 -- should not disturb the Pos and Rep values.
17059 while Present (L) loop
17060 if Ekind (L) /= E_Enumeration_Literal then
17061 Set_Ekind (L, E_Enumeration_Literal);
17062 Set_Enumeration_Pos (L, Ev);
17063 Set_Enumeration_Rep (L, Ev);
17064 Set_Is_Known_Valid (L, True);
17068 New_Overloaded_Entity (L);
17069 Generate_Definition (L);
17070 Set_Convention (L, Convention_Intrinsic);
17072 -- Case of character literal
17074 if Nkind (L) = N_Defining_Character_Literal then
17075 Set_Is_Character_Type (T, True);
17077 -- Check violation of No_Wide_Characters
17079 if Restriction_Check_Required (No_Wide_Characters) then
17080 Get_Name_String (Chars (L));
17082 if Name_Len >= 3 and then Name_Buffer (1 .. 2) = "QW" then
17083 Check_Restriction (No_Wide_Characters, L);
17092 -- Now create a node representing upper bound
17094 B_Node := New_Node (N_Identifier, Sloc (Def));
17095 Set_Chars (B_Node, Chars (Last (Literals (Def))));
17096 Set_Entity (B_Node, Last (Literals (Def)));
17097 Set_Etype (B_Node, T);
17098 Set_Is_Static_Expression (B_Node, True);
17100 Set_High_Bound (R_Node, B_Node);
17102 -- Initialize various fields of the type. Some of this information
17103 -- may be overwritten later through rep.clauses.
17105 Set_Scalar_Range (T, R_Node);
17106 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
17107 Set_Enum_Esize (T);
17108 Set_Enum_Pos_To_Rep (T, Empty);
17110 -- Set Discard_Names if configuration pragma set, or if there is
17111 -- a parameterless pragma in the current declarative region
17113 if Global_Discard_Names or else Discard_Names (Scope (T)) then
17114 Set_Discard_Names (T);
17117 -- Process end label if there is one
17119 if Present (Def) then
17120 Process_End_Label (Def, 'e', T);
17122 end Enumeration_Type_Declaration;
17124 ---------------------------------
17125 -- Expand_To_Stored_Constraint --
17126 ---------------------------------
17128 function Expand_To_Stored_Constraint
17130 Constraint : Elist_Id) return Elist_Id
17132 Explicitly_Discriminated_Type : Entity_Id;
17133 Expansion : Elist_Id;
17134 Discriminant : Entity_Id;
17136 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id;
17137 -- Find the nearest type that actually specifies discriminants
17139 ---------------------------------
17140 -- Type_With_Explicit_Discrims --
17141 ---------------------------------
17143 function Type_With_Explicit_Discrims (Id : Entity_Id) return Entity_Id is
17144 Typ : constant E := Base_Type (Id);
17147 if Ekind (Typ) in Incomplete_Or_Private_Kind then
17148 if Present (Full_View (Typ)) then
17149 return Type_With_Explicit_Discrims (Full_View (Typ));
17153 if Has_Discriminants (Typ) then
17158 if Etype (Typ) = Typ then
17160 elsif Has_Discriminants (Typ) then
17163 return Type_With_Explicit_Discrims (Etype (Typ));
17166 end Type_With_Explicit_Discrims;
17168 -- Start of processing for Expand_To_Stored_Constraint
17171 if No (Constraint) or else Is_Empty_Elmt_List (Constraint) then
17175 Explicitly_Discriminated_Type := Type_With_Explicit_Discrims (Typ);
17177 if No (Explicitly_Discriminated_Type) then
17181 Expansion := New_Elmt_List;
17184 First_Stored_Discriminant (Explicitly_Discriminated_Type);
17185 while Present (Discriminant) loop
17187 (Get_Discriminant_Value
17188 (Discriminant, Explicitly_Discriminated_Type, Constraint),
17190 Next_Stored_Discriminant (Discriminant);
17194 end Expand_To_Stored_Constraint;
17196 ---------------------------
17197 -- Find_Hidden_Interface --
17198 ---------------------------
17200 function Find_Hidden_Interface
17202 Dest : Elist_Id) return Entity_Id
17205 Iface_Elmt : Elmt_Id;
17208 if Present (Src) and then Present (Dest) then
17209 Iface_Elmt := First_Elmt (Src);
17210 while Present (Iface_Elmt) loop
17211 Iface := Node (Iface_Elmt);
17213 if Is_Interface (Iface)
17214 and then not Contain_Interface (Iface, Dest)
17219 Next_Elmt (Iface_Elmt);
17224 end Find_Hidden_Interface;
17226 --------------------
17227 -- Find_Type_Name --
17228 --------------------
17230 function Find_Type_Name (N : Node_Id) return Entity_Id is
17231 Id : constant Entity_Id := Defining_Identifier (N);
17232 New_Id : Entity_Id;
17234 Prev_Par : Node_Id;
17236 procedure Check_Duplicate_Aspects;
17237 -- Check that aspects specified in a completion have not been specified
17238 -- already in the partial view.
17240 procedure Tag_Mismatch;
17241 -- Diagnose a tagged partial view whose full view is untagged. We post
17242 -- the message on the full view, with a reference to the previous
17243 -- partial view. The partial view can be private or incomplete, and
17244 -- these are handled in a different manner, so we determine the position
17245 -- of the error message from the respective slocs of both.
17247 -----------------------------
17248 -- Check_Duplicate_Aspects --
17249 -----------------------------
17251 procedure Check_Duplicate_Aspects is
17252 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id;
17253 -- Return the corresponding aspect of the partial view which matches
17254 -- the aspect id of Asp. Return Empty is no such aspect exists.
17256 -----------------------------
17257 -- Get_Partial_View_Aspect --
17258 -----------------------------
17260 function Get_Partial_View_Aspect (Asp : Node_Id) return Node_Id is
17261 Asp_Id : constant Aspect_Id := Get_Aspect_Id (Asp);
17262 Prev_Asps : constant List_Id := Aspect_Specifications (Prev_Par);
17263 Prev_Asp : Node_Id;
17266 if Present (Prev_Asps) then
17267 Prev_Asp := First (Prev_Asps);
17268 while Present (Prev_Asp) loop
17269 if Get_Aspect_Id (Prev_Asp) = Asp_Id then
17278 end Get_Partial_View_Aspect;
17282 Full_Asps : constant List_Id := Aspect_Specifications (N);
17283 Full_Asp : Node_Id;
17284 Part_Asp : Node_Id;
17286 -- Start of processing for Check_Duplicate_Aspects
17289 if Present (Full_Asps) then
17290 Full_Asp := First (Full_Asps);
17291 while Present (Full_Asp) loop
17292 Part_Asp := Get_Partial_View_Aspect (Full_Asp);
17294 -- An aspect and its class-wide counterpart are two distinct
17295 -- aspects and may apply to both views of an entity.
17297 if Present (Part_Asp)
17298 and then Class_Present (Part_Asp) = Class_Present (Full_Asp)
17301 ("aspect already specified in private declaration",
17308 if Has_Discriminants (Prev)
17309 and then not Has_Unknown_Discriminants (Prev)
17310 and then Get_Aspect_Id (Full_Asp) =
17311 Aspect_Implicit_Dereference
17314 ("cannot specify aspect if partial view has known "
17315 & "discriminants", Full_Asp);
17321 end Check_Duplicate_Aspects;
17327 procedure Tag_Mismatch is
17329 if Sloc (Prev) < Sloc (Id) then
17330 if Ada_Version >= Ada_2012
17331 and then Nkind (N) = N_Private_Type_Declaration
17334 ("declaration of private } must be a tagged type ", Id, Prev);
17337 ("full declaration of } must be a tagged type ", Id, Prev);
17341 if Ada_Version >= Ada_2012
17342 and then Nkind (N) = N_Private_Type_Declaration
17345 ("declaration of private } must be a tagged type ", Prev, Id);
17348 ("full declaration of } must be a tagged type ", Prev, Id);
17353 -- Start of processing for Find_Type_Name
17356 -- Find incomplete declaration, if one was given
17358 Prev := Current_Entity_In_Scope (Id);
17360 -- New type declaration
17366 -- Previous declaration exists
17369 Prev_Par := Parent (Prev);
17371 -- Error if not incomplete/private case except if previous
17372 -- declaration is implicit, etc. Enter_Name will emit error if
17375 if not Is_Incomplete_Or_Private_Type (Prev) then
17379 -- Check invalid completion of private or incomplete type
17381 elsif not Nkind_In (N, N_Full_Type_Declaration,
17382 N_Task_Type_Declaration,
17383 N_Protected_Type_Declaration)
17385 (Ada_Version < Ada_2012
17386 or else not Is_Incomplete_Type (Prev)
17387 or else not Nkind_In (N, N_Private_Type_Declaration,
17388 N_Private_Extension_Declaration))
17390 -- Completion must be a full type declarations (RM 7.3(4))
17392 Error_Msg_Sloc := Sloc (Prev);
17393 Error_Msg_NE ("invalid completion of }", Id, Prev);
17395 -- Set scope of Id to avoid cascaded errors. Entity is never
17396 -- examined again, except when saving globals in generics.
17398 Set_Scope (Id, Current_Scope);
17401 -- If this is a repeated incomplete declaration, no further
17402 -- checks are possible.
17404 if Nkind (N) = N_Incomplete_Type_Declaration then
17408 -- Case of full declaration of incomplete type
17410 elsif Ekind (Prev) = E_Incomplete_Type
17411 and then (Ada_Version < Ada_2012
17412 or else No (Full_View (Prev))
17413 or else not Is_Private_Type (Full_View (Prev)))
17415 -- Indicate that the incomplete declaration has a matching full
17416 -- declaration. The defining occurrence of the incomplete
17417 -- declaration remains the visible one, and the procedure
17418 -- Get_Full_View dereferences it whenever the type is used.
17420 if Present (Full_View (Prev)) then
17421 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17424 Set_Full_View (Prev, Id);
17425 Append_Entity (Id, Current_Scope);
17426 Set_Is_Public (Id, Is_Public (Prev));
17427 Set_Is_Internal (Id);
17430 -- If the incomplete view is tagged, a class_wide type has been
17431 -- created already. Use it for the private type as well, in order
17432 -- to prevent multiple incompatible class-wide types that may be
17433 -- created for self-referential anonymous access components.
17435 if Is_Tagged_Type (Prev)
17436 and then Present (Class_Wide_Type (Prev))
17438 Set_Ekind (Id, Ekind (Prev)); -- will be reset later
17439 Set_Class_Wide_Type (Id, Class_Wide_Type (Prev));
17441 -- Type of the class-wide type is the current Id. Previously
17442 -- this was not done for private declarations because of order-
17443 -- of-elaboration issues in the back end, but gigi now handles
17446 Set_Etype (Class_Wide_Type (Id), Id);
17449 -- Case of full declaration of private type
17452 -- If the private type was a completion of an incomplete type then
17453 -- update Prev to reference the private type
17455 if Ada_Version >= Ada_2012
17456 and then Ekind (Prev) = E_Incomplete_Type
17457 and then Present (Full_View (Prev))
17458 and then Is_Private_Type (Full_View (Prev))
17460 Prev := Full_View (Prev);
17461 Prev_Par := Parent (Prev);
17464 if Nkind (N) = N_Full_Type_Declaration
17466 (Type_Definition (N), N_Record_Definition,
17467 N_Derived_Type_Definition)
17468 and then Interface_Present (Type_Definition (N))
17471 ("completion of private type cannot be an interface", N);
17474 if Nkind (Parent (Prev)) /= N_Private_Extension_Declaration then
17475 if Etype (Prev) /= Prev then
17477 -- Prev is a private subtype or a derived type, and needs
17480 Error_Msg_NE ("invalid redeclaration of }", Id, Prev);
17483 elsif Ekind (Prev) = E_Private_Type
17484 and then Nkind_In (N, N_Task_Type_Declaration,
17485 N_Protected_Type_Declaration)
17488 ("completion of nonlimited type cannot be limited", N);
17490 elsif Ekind (Prev) = E_Record_Type_With_Private
17491 and then Nkind_In (N, N_Task_Type_Declaration,
17492 N_Protected_Type_Declaration)
17494 if not Is_Limited_Record (Prev) then
17496 ("completion of nonlimited type cannot be limited", N);
17498 elsif No (Interface_List (N)) then
17500 ("completion of tagged private type must be tagged",
17505 -- Ada 2005 (AI-251): Private extension declaration of a task
17506 -- type or a protected type. This case arises when covering
17507 -- interface types.
17509 elsif Nkind_In (N, N_Task_Type_Declaration,
17510 N_Protected_Type_Declaration)
17514 elsif Nkind (N) /= N_Full_Type_Declaration
17515 or else Nkind (Type_Definition (N)) /= N_Derived_Type_Definition
17518 ("full view of private extension must be an extension", N);
17520 elsif not (Abstract_Present (Parent (Prev)))
17521 and then Abstract_Present (Type_Definition (N))
17524 ("full view of non-abstract extension cannot be abstract", N);
17527 if not In_Private_Part (Current_Scope) then
17529 ("declaration of full view must appear in private part", N);
17532 if Ada_Version >= Ada_2012 then
17533 Check_Duplicate_Aspects;
17536 Copy_And_Swap (Prev, Id);
17537 Set_Has_Private_Declaration (Prev);
17538 Set_Has_Private_Declaration (Id);
17540 -- AI12-0133: Indicate whether we have a partial view with
17541 -- unknown discriminants, in which case initialization of objects
17542 -- of the type do not receive an invariant check.
17544 Set_Partial_View_Has_Unknown_Discr
17545 (Prev, Has_Unknown_Discriminants (Id));
17547 -- Preserve aspect and iterator flags that may have been set on
17548 -- the partial view.
17550 Set_Has_Delayed_Aspects (Prev, Has_Delayed_Aspects (Id));
17551 Set_Has_Implicit_Dereference (Prev, Has_Implicit_Dereference (Id));
17553 -- If no error, propagate freeze_node from private to full view.
17554 -- It may have been generated for an early operational item.
17556 if Present (Freeze_Node (Id))
17557 and then Serious_Errors_Detected = 0
17558 and then No (Full_View (Id))
17560 Set_Freeze_Node (Prev, Freeze_Node (Id));
17561 Set_Freeze_Node (Id, Empty);
17562 Set_First_Rep_Item (Prev, First_Rep_Item (Id));
17565 Set_Full_View (Id, Prev);
17569 -- Verify that full declaration conforms to partial one
17571 if Is_Incomplete_Or_Private_Type (Prev)
17572 and then Present (Discriminant_Specifications (Prev_Par))
17574 if Present (Discriminant_Specifications (N)) then
17575 if Ekind (Prev) = E_Incomplete_Type then
17576 Check_Discriminant_Conformance (N, Prev, Prev);
17578 Check_Discriminant_Conformance (N, Prev, Id);
17583 ("missing discriminants in full type declaration", N);
17585 -- To avoid cascaded errors on subsequent use, share the
17586 -- discriminants of the partial view.
17588 Set_Discriminant_Specifications (N,
17589 Discriminant_Specifications (Prev_Par));
17593 -- A prior untagged partial view can have an associated class-wide
17594 -- type due to use of the class attribute, and in this case the full
17595 -- type must also be tagged. This Ada 95 usage is deprecated in favor
17596 -- of incomplete tagged declarations, but we check for it.
17599 and then (Is_Tagged_Type (Prev)
17600 or else Present (Class_Wide_Type (Prev)))
17602 -- Ada 2012 (AI05-0162): A private type may be the completion of
17603 -- an incomplete type.
17605 if Ada_Version >= Ada_2012
17606 and then Is_Incomplete_Type (Prev)
17607 and then Nkind_In (N, N_Private_Type_Declaration,
17608 N_Private_Extension_Declaration)
17610 -- No need to check private extensions since they are tagged
17612 if Nkind (N) = N_Private_Type_Declaration
17613 and then not Tagged_Present (N)
17618 -- The full declaration is either a tagged type (including
17619 -- a synchronized type that implements interfaces) or a
17620 -- type extension, otherwise this is an error.
17622 elsif Nkind_In (N, N_Task_Type_Declaration,
17623 N_Protected_Type_Declaration)
17625 if No (Interface_List (N)) and then not Error_Posted (N) then
17629 elsif Nkind (Type_Definition (N)) = N_Record_Definition then
17631 -- Indicate that the previous declaration (tagged incomplete
17632 -- or private declaration) requires the same on the full one.
17634 if not Tagged_Present (Type_Definition (N)) then
17636 Set_Is_Tagged_Type (Id);
17639 elsif Nkind (Type_Definition (N)) = N_Derived_Type_Definition then
17640 if No (Record_Extension_Part (Type_Definition (N))) then
17642 ("full declaration of } must be a record extension",
17645 -- Set some attributes to produce a usable full view
17647 Set_Is_Tagged_Type (Id);
17656 and then Nkind (Parent (Prev)) = N_Incomplete_Type_Declaration
17657 and then Present (Premature_Use (Parent (Prev)))
17659 Error_Msg_Sloc := Sloc (N);
17661 ("\full declaration #", Premature_Use (Parent (Prev)));
17666 end Find_Type_Name;
17668 -------------------------
17669 -- Find_Type_Of_Object --
17670 -------------------------
17672 function Find_Type_Of_Object
17673 (Obj_Def : Node_Id;
17674 Related_Nod : Node_Id) return Entity_Id
17676 Def_Kind : constant Node_Kind := Nkind (Obj_Def);
17677 P : Node_Id := Parent (Obj_Def);
17682 -- If the parent is a component_definition node we climb to the
17683 -- component_declaration node
17685 if Nkind (P) = N_Component_Definition then
17689 -- Case of an anonymous array subtype
17691 if Nkind_In (Def_Kind, N_Constrained_Array_Definition,
17692 N_Unconstrained_Array_Definition)
17695 Array_Type_Declaration (T, Obj_Def);
17697 -- Create an explicit subtype whenever possible
17699 elsif Nkind (P) /= N_Component_Declaration
17700 and then Def_Kind = N_Subtype_Indication
17702 -- Base name of subtype on object name, which will be unique in
17703 -- the current scope.
17705 -- If this is a duplicate declaration, return base type, to avoid
17706 -- generating duplicate anonymous types.
17708 if Error_Posted (P) then
17709 Analyze (Subtype_Mark (Obj_Def));
17710 return Entity (Subtype_Mark (Obj_Def));
17715 (Chars (Defining_Identifier (Related_Nod)), 'S', 0, 'T');
17717 T := Make_Defining_Identifier (Sloc (P), Nam);
17719 Insert_Action (Obj_Def,
17720 Make_Subtype_Declaration (Sloc (P),
17721 Defining_Identifier => T,
17722 Subtype_Indication => Relocate_Node (Obj_Def)));
17724 -- This subtype may need freezing, and this will not be done
17725 -- automatically if the object declaration is not in declarative
17726 -- part. Since this is an object declaration, the type cannot always
17727 -- be frozen here. Deferred constants do not freeze their type
17728 -- (which often enough will be private).
17730 if Nkind (P) = N_Object_Declaration
17731 and then Constant_Present (P)
17732 and then No (Expression (P))
17736 -- Here we freeze the base type of object type to catch premature use
17737 -- of discriminated private type without a full view.
17740 Insert_Actions (Obj_Def, Freeze_Entity (Base_Type (T), P));
17743 -- Ada 2005 AI-406: the object definition in an object declaration
17744 -- can be an access definition.
17746 elsif Def_Kind = N_Access_Definition then
17747 T := Access_Definition (Related_Nod, Obj_Def);
17749 Set_Is_Local_Anonymous_Access
17751 V => (Ada_Version < Ada_2012)
17752 or else (Nkind (P) /= N_Object_Declaration)
17753 or else Is_Library_Level_Entity (Defining_Identifier (P)));
17755 -- Otherwise, the object definition is just a subtype_mark
17758 T := Process_Subtype (Obj_Def, Related_Nod);
17760 -- If expansion is disabled an object definition that is an aggregate
17761 -- will not get expanded and may lead to scoping problems in the back
17762 -- end, if the object is referenced in an inner scope. In that case
17763 -- create an itype reference for the object definition now. This
17764 -- may be redundant in some cases, but harmless.
17767 and then Nkind (Related_Nod) = N_Object_Declaration
17770 Build_Itype_Reference (T, Related_Nod);
17775 end Find_Type_Of_Object;
17777 --------------------------------
17778 -- Find_Type_Of_Subtype_Indic --
17779 --------------------------------
17781 function Find_Type_Of_Subtype_Indic (S : Node_Id) return Entity_Id is
17785 -- Case of subtype mark with a constraint
17787 if Nkind (S) = N_Subtype_Indication then
17788 Find_Type (Subtype_Mark (S));
17789 Typ := Entity (Subtype_Mark (S));
17792 Is_Valid_Constraint_Kind (Ekind (Typ), Nkind (Constraint (S)))
17795 ("incorrect constraint for this kind of type", Constraint (S));
17796 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
17799 -- Otherwise we have a subtype mark without a constraint
17801 elsif Error_Posted (S) then
17802 Rewrite (S, New_Occurrence_Of (Any_Id, Sloc (S)));
17810 -- Check No_Wide_Characters restriction
17812 Check_Wide_Character_Restriction (Typ, S);
17815 end Find_Type_Of_Subtype_Indic;
17817 -------------------------------------
17818 -- Floating_Point_Type_Declaration --
17819 -------------------------------------
17821 procedure Floating_Point_Type_Declaration (T : Entity_Id; Def : Node_Id) is
17822 Digs : constant Node_Id := Digits_Expression (Def);
17823 Max_Digs_Val : constant Uint := Digits_Value (Standard_Long_Long_Float);
17825 Base_Typ : Entity_Id;
17826 Implicit_Base : Entity_Id;
17828 function Can_Derive_From (E : Entity_Id) return Boolean;
17829 -- Find if given digits value, and possibly a specified range, allows
17830 -- derivation from specified type
17832 procedure Convert_Bound (B : Node_Id);
17833 -- If specified, the bounds must be static but may be of different
17834 -- types. They must be converted into machine numbers of the base type,
17835 -- in accordance with RM 4.9(38).
17837 function Find_Base_Type return Entity_Id;
17838 -- Find a predefined base type that Def can derive from, or generate
17839 -- an error and substitute Long_Long_Float if none exists.
17841 ---------------------
17842 -- Can_Derive_From --
17843 ---------------------
17845 function Can_Derive_From (E : Entity_Id) return Boolean is
17846 Spec : constant Entity_Id := Real_Range_Specification (Def);
17849 -- Check specified "digits" constraint
17851 if Digs_Val > Digits_Value (E) then
17855 -- Check for matching range, if specified
17857 if Present (Spec) then
17858 if Expr_Value_R (Type_Low_Bound (E)) >
17859 Expr_Value_R (Low_Bound (Spec))
17864 if Expr_Value_R (Type_High_Bound (E)) <
17865 Expr_Value_R (High_Bound (Spec))
17872 end Can_Derive_From;
17874 -------------------
17875 -- Convert_Bound --
17876 --------------------
17878 procedure Convert_Bound (B : Node_Id) is
17880 -- If the bound is not a literal it can only be static if it is
17881 -- a static constant, possibly of a specified type.
17883 if Is_Entity_Name (B)
17884 and then Ekind (Entity (B)) = E_Constant
17886 Rewrite (B, Constant_Value (Entity (B)));
17889 if Nkind (B) = N_Real_Literal then
17890 Set_Realval (B, Machine (Base_Typ, Realval (B), Round, B));
17891 Set_Is_Machine_Number (B);
17892 Set_Etype (B, Base_Typ);
17896 --------------------
17897 -- Find_Base_Type --
17898 --------------------
17900 function Find_Base_Type return Entity_Id is
17901 Choice : Elmt_Id := First_Elmt (Predefined_Float_Types);
17904 -- Iterate over the predefined types in order, returning the first
17905 -- one that Def can derive from.
17907 while Present (Choice) loop
17908 if Can_Derive_From (Node (Choice)) then
17909 return Node (Choice);
17912 Next_Elmt (Choice);
17915 -- If we can't derive from any existing type, use Long_Long_Float
17916 -- and give appropriate message explaining the problem.
17918 if Digs_Val > Max_Digs_Val then
17919 -- It might be the case that there is a type with the requested
17920 -- range, just not the combination of digits and range.
17923 ("no predefined type has requested range and precision",
17924 Real_Range_Specification (Def));
17928 ("range too large for any predefined type",
17929 Real_Range_Specification (Def));
17932 return Standard_Long_Long_Float;
17933 end Find_Base_Type;
17935 -- Start of processing for Floating_Point_Type_Declaration
17938 Check_Restriction (No_Floating_Point, Def);
17940 -- Create an implicit base type
17943 Create_Itype (E_Floating_Point_Type, Parent (Def), T, 'B');
17945 -- Analyze and verify digits value
17947 Analyze_And_Resolve (Digs, Any_Integer);
17948 Check_Digits_Expression (Digs);
17949 Digs_Val := Expr_Value (Digs);
17951 -- Process possible range spec and find correct type to derive from
17953 Process_Real_Range_Specification (Def);
17955 -- Check that requested number of digits is not too high.
17957 if Digs_Val > Max_Digs_Val then
17959 -- The check for Max_Base_Digits may be somewhat expensive, as it
17960 -- requires reading System, so only do it when necessary.
17963 Max_Base_Digits : constant Uint :=
17966 (Parent (RTE (RE_Max_Base_Digits))));
17969 if Digs_Val > Max_Base_Digits then
17970 Error_Msg_Uint_1 := Max_Base_Digits;
17971 Error_Msg_N ("digits value out of range, maximum is ^", Digs);
17973 elsif No (Real_Range_Specification (Def)) then
17974 Error_Msg_Uint_1 := Max_Digs_Val;
17975 Error_Msg_N ("types with more than ^ digits need range spec "
17976 & "(RM 3.5.7(6))", Digs);
17981 -- Find a suitable type to derive from or complain and use a substitute
17983 Base_Typ := Find_Base_Type;
17985 -- If there are bounds given in the declaration use them as the bounds
17986 -- of the type, otherwise use the bounds of the predefined base type
17987 -- that was chosen based on the Digits value.
17989 if Present (Real_Range_Specification (Def)) then
17990 Set_Scalar_Range (T, Real_Range_Specification (Def));
17991 Set_Is_Constrained (T);
17993 Convert_Bound (Type_Low_Bound (T));
17994 Convert_Bound (Type_High_Bound (T));
17997 Set_Scalar_Range (T, Scalar_Range (Base_Typ));
18000 -- Complete definition of implicit base and declared first subtype. The
18001 -- inheritance of the rep item chain ensures that SPARK-related pragmas
18002 -- are not clobbered when the floating point type acts as a full view of
18005 Set_Etype (Implicit_Base, Base_Typ);
18006 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
18007 Set_Size_Info (Implicit_Base, Base_Typ);
18008 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
18009 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
18010 Set_Digits_Value (Implicit_Base, Digits_Value (Base_Typ));
18011 Set_Float_Rep (Implicit_Base, Float_Rep (Base_Typ));
18013 Set_Ekind (T, E_Floating_Point_Subtype);
18014 Set_Etype (T, Implicit_Base);
18015 Set_Size_Info (T, Implicit_Base);
18016 Set_RM_Size (T, RM_Size (Implicit_Base));
18017 Inherit_Rep_Item_Chain (T, Implicit_Base);
18018 Set_Digits_Value (T, Digs_Val);
18019 end Floating_Point_Type_Declaration;
18021 ----------------------------
18022 -- Get_Discriminant_Value --
18023 ----------------------------
18025 -- This is the situation:
18027 -- There is a non-derived type
18029 -- type T0 (Dx, Dy, Dz...)
18031 -- There are zero or more levels of derivation, with each derivation
18032 -- either purely inheriting the discriminants, or defining its own.
18034 -- type Ti is new Ti-1
18036 -- type Ti (Dw) is new Ti-1(Dw, 1, X+Y)
18038 -- subtype Ti is ...
18040 -- The subtype issue is avoided by the use of Original_Record_Component,
18041 -- and the fact that derived subtypes also derive the constraints.
18043 -- This chain leads back from
18045 -- Typ_For_Constraint
18047 -- Typ_For_Constraint has discriminants, and the value for each
18048 -- discriminant is given by its corresponding Elmt of Constraints.
18050 -- Discriminant is some discriminant in this hierarchy
18052 -- We need to return its value
18054 -- We do this by recursively searching each level, and looking for
18055 -- Discriminant. Once we get to the bottom, we start backing up
18056 -- returning the value for it which may in turn be a discriminant
18057 -- further up, so on the backup we continue the substitution.
18059 function Get_Discriminant_Value
18060 (Discriminant : Entity_Id;
18061 Typ_For_Constraint : Entity_Id;
18062 Constraint : Elist_Id) return Node_Id
18064 function Root_Corresponding_Discriminant
18065 (Discr : Entity_Id) return Entity_Id;
18066 -- Given a discriminant, traverse the chain of inherited discriminants
18067 -- and return the topmost discriminant.
18069 function Search_Derivation_Levels
18071 Discrim_Values : Elist_Id;
18072 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id;
18073 -- This is the routine that performs the recursive search of levels
18074 -- as described above.
18076 -------------------------------------
18077 -- Root_Corresponding_Discriminant --
18078 -------------------------------------
18080 function Root_Corresponding_Discriminant
18081 (Discr : Entity_Id) return Entity_Id
18087 while Present (Corresponding_Discriminant (D)) loop
18088 D := Corresponding_Discriminant (D);
18092 end Root_Corresponding_Discriminant;
18094 ------------------------------
18095 -- Search_Derivation_Levels --
18096 ------------------------------
18098 function Search_Derivation_Levels
18100 Discrim_Values : Elist_Id;
18101 Stored_Discrim_Values : Boolean) return Node_Or_Entity_Id
18105 Result : Node_Or_Entity_Id;
18106 Result_Entity : Node_Id;
18109 -- If inappropriate type, return Error, this happens only in
18110 -- cascaded error situations, and we want to avoid a blow up.
18112 if not Is_Composite_Type (Ti) or else Is_Array_Type (Ti) then
18116 -- Look deeper if possible. Use Stored_Constraints only for
18117 -- untagged types. For tagged types use the given constraint.
18118 -- This asymmetry needs explanation???
18120 if not Stored_Discrim_Values
18121 and then Present (Stored_Constraint (Ti))
18122 and then not Is_Tagged_Type (Ti)
18125 Search_Derivation_Levels (Ti, Stored_Constraint (Ti), True);
18129 Td : Entity_Id := Etype (Ti);
18132 -- If the parent type is private, the full view may include
18133 -- renamed discriminants, and it is those stored values that
18134 -- may be needed (the partial view never has more information
18135 -- than the full view).
18137 if Is_Private_Type (Td) and then Present (Full_View (Td)) then
18138 Td := Full_View (Td);
18142 Result := Discriminant;
18145 if Present (Stored_Constraint (Ti)) then
18147 Search_Derivation_Levels
18148 (Td, Stored_Constraint (Ti), True);
18151 Search_Derivation_Levels
18152 (Td, Discrim_Values, Stored_Discrim_Values);
18158 -- Extra underlying places to search, if not found above. For
18159 -- concurrent types, the relevant discriminant appears in the
18160 -- corresponding record. For a type derived from a private type
18161 -- without discriminant, the full view inherits the discriminants
18162 -- of the full view of the parent.
18164 if Result = Discriminant then
18165 if Is_Concurrent_Type (Ti)
18166 and then Present (Corresponding_Record_Type (Ti))
18169 Search_Derivation_Levels (
18170 Corresponding_Record_Type (Ti),
18172 Stored_Discrim_Values);
18174 elsif Is_Private_Type (Ti)
18175 and then not Has_Discriminants (Ti)
18176 and then Present (Full_View (Ti))
18177 and then Etype (Full_View (Ti)) /= Ti
18180 Search_Derivation_Levels (
18183 Stored_Discrim_Values);
18187 -- If Result is not a (reference to a) discriminant, return it,
18188 -- otherwise set Result_Entity to the discriminant.
18190 if Nkind (Result) = N_Defining_Identifier then
18191 pragma Assert (Result = Discriminant);
18192 Result_Entity := Result;
18195 if not Denotes_Discriminant (Result) then
18199 Result_Entity := Entity (Result);
18202 -- See if this level of derivation actually has discriminants because
18203 -- tagged derivations can add them, hence the lower levels need not
18206 if not Has_Discriminants (Ti) then
18210 -- Scan Ti's discriminants for Result_Entity, and return its
18211 -- corresponding value, if any.
18213 Result_Entity := Original_Record_Component (Result_Entity);
18215 Assoc := First_Elmt (Discrim_Values);
18217 if Stored_Discrim_Values then
18218 Disc := First_Stored_Discriminant (Ti);
18220 Disc := First_Discriminant (Ti);
18223 while Present (Disc) loop
18225 -- If no further associations return the discriminant, value will
18226 -- be found on the second pass.
18232 if Original_Record_Component (Disc) = Result_Entity then
18233 return Node (Assoc);
18238 if Stored_Discrim_Values then
18239 Next_Stored_Discriminant (Disc);
18241 Next_Discriminant (Disc);
18245 -- Could not find it
18248 end Search_Derivation_Levels;
18252 Result : Node_Or_Entity_Id;
18254 -- Start of processing for Get_Discriminant_Value
18257 -- ??? This routine is a gigantic mess and will be deleted. For the
18258 -- time being just test for the trivial case before calling recurse.
18260 -- We are now celebrating the 20th anniversary of this comment!
18262 if Base_Type (Scope (Discriminant)) = Base_Type (Typ_For_Constraint) then
18268 D := First_Discriminant (Typ_For_Constraint);
18269 E := First_Elmt (Constraint);
18270 while Present (D) loop
18271 if Chars (D) = Chars (Discriminant) then
18275 Next_Discriminant (D);
18281 Result := Search_Derivation_Levels
18282 (Typ_For_Constraint, Constraint, False);
18284 -- ??? hack to disappear when this routine is gone
18286 if Nkind (Result) = N_Defining_Identifier then
18292 D := First_Discriminant (Typ_For_Constraint);
18293 E := First_Elmt (Constraint);
18294 while Present (D) loop
18295 if Root_Corresponding_Discriminant (D) = Discriminant then
18299 Next_Discriminant (D);
18305 pragma Assert (Nkind (Result) /= N_Defining_Identifier);
18307 end Get_Discriminant_Value;
18309 --------------------------
18310 -- Has_Range_Constraint --
18311 --------------------------
18313 function Has_Range_Constraint (N : Node_Id) return Boolean is
18314 C : constant Node_Id := Constraint (N);
18317 if Nkind (C) = N_Range_Constraint then
18320 elsif Nkind (C) = N_Digits_Constraint then
18322 Is_Decimal_Fixed_Point_Type (Entity (Subtype_Mark (N)))
18323 or else Present (Range_Constraint (C));
18325 elsif Nkind (C) = N_Delta_Constraint then
18326 return Present (Range_Constraint (C));
18331 end Has_Range_Constraint;
18333 ------------------------
18334 -- Inherit_Components --
18335 ------------------------
18337 function Inherit_Components
18339 Parent_Base : Entity_Id;
18340 Derived_Base : Entity_Id;
18341 Is_Tagged : Boolean;
18342 Inherit_Discr : Boolean;
18343 Discs : Elist_Id) return Elist_Id
18345 Assoc_List : constant Elist_Id := New_Elmt_List;
18347 procedure Inherit_Component
18348 (Old_C : Entity_Id;
18349 Plain_Discrim : Boolean := False;
18350 Stored_Discrim : Boolean := False);
18351 -- Inherits component Old_C from Parent_Base to the Derived_Base. If
18352 -- Plain_Discrim is True, Old_C is a discriminant. If Stored_Discrim is
18353 -- True, Old_C is a stored discriminant. If they are both false then
18354 -- Old_C is a regular component.
18356 -----------------------
18357 -- Inherit_Component --
18358 -----------------------
18360 procedure Inherit_Component
18361 (Old_C : Entity_Id;
18362 Plain_Discrim : Boolean := False;
18363 Stored_Discrim : Boolean := False)
18365 procedure Set_Anonymous_Type (Id : Entity_Id);
18366 -- Id denotes the entity of an access discriminant or anonymous
18367 -- access component. Set the type of Id to either the same type of
18368 -- Old_C or create a new one depending on whether the parent and
18369 -- the child types are in the same scope.
18371 ------------------------
18372 -- Set_Anonymous_Type --
18373 ------------------------
18375 procedure Set_Anonymous_Type (Id : Entity_Id) is
18376 Old_Typ : constant Entity_Id := Etype (Old_C);
18379 if Scope (Parent_Base) = Scope (Derived_Base) then
18380 Set_Etype (Id, Old_Typ);
18382 -- The parent and the derived type are in two different scopes.
18383 -- Reuse the type of the original discriminant / component by
18384 -- copying it in order to preserve all attributes.
18388 Typ : constant Entity_Id := New_Copy (Old_Typ);
18391 Set_Etype (Id, Typ);
18393 -- Since we do not generate component declarations for
18394 -- inherited components, associate the itype with the
18397 Set_Associated_Node_For_Itype (Typ, Parent (Derived_Base));
18398 Set_Scope (Typ, Derived_Base);
18401 end Set_Anonymous_Type;
18403 -- Local variables and constants
18405 New_C : constant Entity_Id := New_Copy (Old_C);
18407 Corr_Discrim : Entity_Id;
18408 Discrim : Entity_Id;
18410 -- Start of processing for Inherit_Component
18413 pragma Assert (not Is_Tagged or not Stored_Discrim);
18415 Set_Parent (New_C, Parent (Old_C));
18417 -- Regular discriminants and components must be inserted in the scope
18418 -- of the Derived_Base. Do it here.
18420 if not Stored_Discrim then
18421 Enter_Name (New_C);
18424 -- For tagged types the Original_Record_Component must point to
18425 -- whatever this field was pointing to in the parent type. This has
18426 -- already been achieved by the call to New_Copy above.
18428 if not Is_Tagged then
18429 Set_Original_Record_Component (New_C, New_C);
18430 Set_Corresponding_Record_Component (New_C, Old_C);
18433 -- Set the proper type of an access discriminant
18435 if Ekind (New_C) = E_Discriminant
18436 and then Ekind (Etype (New_C)) = E_Anonymous_Access_Type
18438 Set_Anonymous_Type (New_C);
18441 -- If we have inherited a component then see if its Etype contains
18442 -- references to Parent_Base discriminants. In this case, replace
18443 -- these references with the constraints given in Discs. We do not
18444 -- do this for the partial view of private types because this is
18445 -- not needed (only the components of the full view will be used
18446 -- for code generation) and cause problem. We also avoid this
18447 -- transformation in some error situations.
18449 if Ekind (New_C) = E_Component then
18451 -- Set the proper type of an anonymous access component
18453 if Ekind (Etype (New_C)) = E_Anonymous_Access_Type then
18454 Set_Anonymous_Type (New_C);
18456 elsif (Is_Private_Type (Derived_Base)
18457 and then not Is_Generic_Type (Derived_Base))
18458 or else (Is_Empty_Elmt_List (Discs)
18459 and then not Expander_Active)
18461 Set_Etype (New_C, Etype (Old_C));
18464 -- The current component introduces a circularity of the
18467 -- limited with Pack_2;
18468 -- package Pack_1 is
18469 -- type T_1 is tagged record
18470 -- Comp : access Pack_2.T_2;
18476 -- package Pack_2 is
18477 -- type T_2 is new Pack_1.T_1 with ...;
18482 Constrain_Component_Type
18483 (Old_C, Derived_Base, N, Parent_Base, Discs));
18487 -- In derived tagged types it is illegal to reference a non
18488 -- discriminant component in the parent type. To catch this, mark
18489 -- these components with an Ekind of E_Void. This will be reset in
18490 -- Record_Type_Definition after processing the record extension of
18491 -- the derived type.
18493 -- If the declaration is a private extension, there is no further
18494 -- record extension to process, and the components retain their
18495 -- current kind, because they are visible at this point.
18497 if Is_Tagged and then Ekind (New_C) = E_Component
18498 and then Nkind (N) /= N_Private_Extension_Declaration
18500 Set_Ekind (New_C, E_Void);
18503 if Plain_Discrim then
18504 Set_Corresponding_Discriminant (New_C, Old_C);
18505 Build_Discriminal (New_C);
18507 -- If we are explicitly inheriting a stored discriminant it will be
18508 -- completely hidden.
18510 elsif Stored_Discrim then
18511 Set_Corresponding_Discriminant (New_C, Empty);
18512 Set_Discriminal (New_C, Empty);
18513 Set_Is_Completely_Hidden (New_C);
18515 -- Set the Original_Record_Component of each discriminant in the
18516 -- derived base to point to the corresponding stored that we just
18519 Discrim := First_Discriminant (Derived_Base);
18520 while Present (Discrim) loop
18521 Corr_Discrim := Corresponding_Discriminant (Discrim);
18523 -- Corr_Discrim could be missing in an error situation
18525 if Present (Corr_Discrim)
18526 and then Original_Record_Component (Corr_Discrim) = Old_C
18528 Set_Original_Record_Component (Discrim, New_C);
18529 Set_Corresponding_Record_Component (Discrim, Empty);
18532 Next_Discriminant (Discrim);
18535 Append_Entity (New_C, Derived_Base);
18538 if not Is_Tagged then
18539 Append_Elmt (Old_C, Assoc_List);
18540 Append_Elmt (New_C, Assoc_List);
18542 end Inherit_Component;
18544 -- Variables local to Inherit_Component
18546 Loc : constant Source_Ptr := Sloc (N);
18548 Parent_Discrim : Entity_Id;
18549 Stored_Discrim : Entity_Id;
18551 Component : Entity_Id;
18553 -- Start of processing for Inherit_Components
18556 if not Is_Tagged then
18557 Append_Elmt (Parent_Base, Assoc_List);
18558 Append_Elmt (Derived_Base, Assoc_List);
18561 -- Inherit parent discriminants if needed
18563 if Inherit_Discr then
18564 Parent_Discrim := First_Discriminant (Parent_Base);
18565 while Present (Parent_Discrim) loop
18566 Inherit_Component (Parent_Discrim, Plain_Discrim => True);
18567 Next_Discriminant (Parent_Discrim);
18571 -- Create explicit stored discrims for untagged types when necessary
18573 if not Has_Unknown_Discriminants (Derived_Base)
18574 and then Has_Discriminants (Parent_Base)
18575 and then not Is_Tagged
18578 or else First_Discriminant (Parent_Base) /=
18579 First_Stored_Discriminant (Parent_Base))
18581 Stored_Discrim := First_Stored_Discriminant (Parent_Base);
18582 while Present (Stored_Discrim) loop
18583 Inherit_Component (Stored_Discrim, Stored_Discrim => True);
18584 Next_Stored_Discriminant (Stored_Discrim);
18588 -- See if we can apply the second transformation for derived types, as
18589 -- explained in point 6. in the comments above Build_Derived_Record_Type
18590 -- This is achieved by appending Derived_Base discriminants into Discs,
18591 -- which has the side effect of returning a non empty Discs list to the
18592 -- caller of Inherit_Components, which is what we want. This must be
18593 -- done for private derived types if there are explicit stored
18594 -- discriminants, to ensure that we can retrieve the values of the
18595 -- constraints provided in the ancestors.
18598 and then Is_Empty_Elmt_List (Discs)
18599 and then Present (First_Discriminant (Derived_Base))
18601 (not Is_Private_Type (Derived_Base)
18602 or else Is_Completely_Hidden
18603 (First_Stored_Discriminant (Derived_Base))
18604 or else Is_Generic_Type (Derived_Base))
18606 D := First_Discriminant (Derived_Base);
18607 while Present (D) loop
18608 Append_Elmt (New_Occurrence_Of (D, Loc), Discs);
18609 Next_Discriminant (D);
18613 -- Finally, inherit non-discriminant components unless they are not
18614 -- visible because defined or inherited from the full view of the
18615 -- parent. Don't inherit the _parent field of the parent type.
18617 Component := First_Entity (Parent_Base);
18618 while Present (Component) loop
18620 -- Ada 2005 (AI-251): Do not inherit components associated with
18621 -- secondary tags of the parent.
18623 if Ekind (Component) = E_Component
18624 and then Present (Related_Type (Component))
18628 elsif Ekind (Component) /= E_Component
18629 or else Chars (Component) = Name_uParent
18633 -- If the derived type is within the parent type's declarative
18634 -- region, then the components can still be inherited even though
18635 -- they aren't visible at this point. This can occur for cases
18636 -- such as within public child units where the components must
18637 -- become visible upon entering the child unit's private part.
18639 elsif not Is_Visible_Component (Component)
18640 and then not In_Open_Scopes (Scope (Parent_Base))
18644 elsif Ekind_In (Derived_Base, E_Private_Type,
18645 E_Limited_Private_Type)
18650 Inherit_Component (Component);
18653 Next_Entity (Component);
18656 -- For tagged derived types, inherited discriminants cannot be used in
18657 -- component declarations of the record extension part. To achieve this
18658 -- we mark the inherited discriminants as not visible.
18660 if Is_Tagged and then Inherit_Discr then
18661 D := First_Discriminant (Derived_Base);
18662 while Present (D) loop
18663 Set_Is_Immediately_Visible (D, False);
18664 Next_Discriminant (D);
18669 end Inherit_Components;
18671 -----------------------------
18672 -- Inherit_Predicate_Flags --
18673 -----------------------------
18675 procedure Inherit_Predicate_Flags (Subt, Par : Entity_Id) is
18677 if Present (Predicate_Function (Subt)) then
18681 Set_Has_Predicates (Subt, Has_Predicates (Par));
18682 Set_Has_Static_Predicate_Aspect
18683 (Subt, Has_Static_Predicate_Aspect (Par));
18684 Set_Has_Dynamic_Predicate_Aspect
18685 (Subt, Has_Dynamic_Predicate_Aspect (Par));
18687 -- A named subtype does not inherit the predicate function of its
18688 -- parent but an itype declared for a loop index needs the discrete
18689 -- predicate information of its parent to execute the loop properly.
18690 -- A non-discrete type may has a static predicate (for example True)
18691 -- but has no static_discrete_predicate.
18693 if Is_Itype (Subt) and then Present (Predicate_Function (Par)) then
18694 Set_Subprograms_For_Type (Subt, Subprograms_For_Type (Par));
18696 if Has_Static_Predicate (Par) and then Is_Discrete_Type (Par) then
18697 Set_Static_Discrete_Predicate
18698 (Subt, Static_Discrete_Predicate (Par));
18701 end Inherit_Predicate_Flags;
18703 ----------------------
18704 -- Is_EVF_Procedure --
18705 ----------------------
18707 function Is_EVF_Procedure (Subp : Entity_Id) return Boolean is
18708 Formal : Entity_Id;
18711 -- Examine the formals of an Extensions_Visible False procedure looking
18712 -- for a controlling OUT parameter.
18714 if Ekind (Subp) = E_Procedure
18715 and then Extensions_Visible_Status (Subp) = Extensions_Visible_False
18717 Formal := First_Formal (Subp);
18718 while Present (Formal) loop
18719 if Ekind (Formal) = E_Out_Parameter
18720 and then Is_Controlling_Formal (Formal)
18725 Next_Formal (Formal);
18730 end Is_EVF_Procedure;
18732 -----------------------
18733 -- Is_Null_Extension --
18734 -----------------------
18736 function Is_Null_Extension (T : Entity_Id) return Boolean is
18737 Type_Decl : constant Node_Id := Parent (Base_Type (T));
18738 Comp_List : Node_Id;
18742 if Nkind (Type_Decl) /= N_Full_Type_Declaration
18743 or else not Is_Tagged_Type (T)
18744 or else Nkind (Type_Definition (Type_Decl)) /=
18745 N_Derived_Type_Definition
18746 or else No (Record_Extension_Part (Type_Definition (Type_Decl)))
18752 Component_List (Record_Extension_Part (Type_Definition (Type_Decl)));
18754 if Present (Discriminant_Specifications (Type_Decl)) then
18757 elsif Present (Comp_List)
18758 and then Is_Non_Empty_List (Component_Items (Comp_List))
18760 Comp := First (Component_Items (Comp_List));
18762 -- Only user-defined components are relevant. The component list
18763 -- may also contain a parent component and internal components
18764 -- corresponding to secondary tags, but these do not determine
18765 -- whether this is a null extension.
18767 while Present (Comp) loop
18768 if Comes_From_Source (Comp) then
18780 end Is_Null_Extension;
18782 ------------------------------
18783 -- Is_Valid_Constraint_Kind --
18784 ------------------------------
18786 function Is_Valid_Constraint_Kind
18787 (T_Kind : Type_Kind;
18788 Constraint_Kind : Node_Kind) return Boolean
18792 when Enumeration_Kind
18795 return Constraint_Kind = N_Range_Constraint;
18797 when Decimal_Fixed_Point_Kind =>
18798 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18799 N_Range_Constraint);
18801 when Ordinary_Fixed_Point_Kind =>
18802 return Nkind_In (Constraint_Kind, N_Delta_Constraint,
18803 N_Range_Constraint);
18806 return Nkind_In (Constraint_Kind, N_Digits_Constraint,
18807 N_Range_Constraint);
18814 | E_Incomplete_Type
18818 return Constraint_Kind = N_Index_Or_Discriminant_Constraint;
18821 return True; -- Error will be detected later
18823 end Is_Valid_Constraint_Kind;
18825 --------------------------
18826 -- Is_Visible_Component --
18827 --------------------------
18829 function Is_Visible_Component
18831 N : Node_Id := Empty) return Boolean
18833 Original_Comp : Entity_Id := Empty;
18834 Original_Type : Entity_Id;
18835 Type_Scope : Entity_Id;
18837 function Is_Local_Type (Typ : Entity_Id) return Boolean;
18838 -- Check whether parent type of inherited component is declared locally,
18839 -- possibly within a nested package or instance. The current scope is
18840 -- the derived record itself.
18842 -------------------
18843 -- Is_Local_Type --
18844 -------------------
18846 function Is_Local_Type (Typ : Entity_Id) return Boolean is
18850 Scop := Scope (Typ);
18851 while Present (Scop)
18852 and then Scop /= Standard_Standard
18854 if Scop = Scope (Current_Scope) then
18858 Scop := Scope (Scop);
18864 -- Start of processing for Is_Visible_Component
18867 if Ekind_In (C, E_Component, E_Discriminant) then
18868 Original_Comp := Original_Record_Component (C);
18871 if No (Original_Comp) then
18873 -- Premature usage, or previous error
18878 Original_Type := Scope (Original_Comp);
18879 Type_Scope := Scope (Base_Type (Scope (C)));
18882 -- This test only concerns tagged types
18884 if not Is_Tagged_Type (Original_Type) then
18886 -- Check if this is a renamed discriminant (hidden either by the
18887 -- derived type or by some ancestor), unless we are analyzing code
18888 -- generated by the expander since it may reference such components
18889 -- (for example see the expansion of Deep_Adjust).
18891 if Ekind (C) = E_Discriminant and then Present (N) then
18893 not Comes_From_Source (N)
18894 or else not Is_Completely_Hidden (C);
18899 -- If it is _Parent or _Tag, there is no visibility issue
18901 elsif not Comes_From_Source (Original_Comp) then
18904 -- Discriminants are visible unless the (private) type has unknown
18905 -- discriminants. If the discriminant reference is inserted for a
18906 -- discriminant check on a full view it is also visible.
18908 elsif Ekind (Original_Comp) = E_Discriminant
18910 (not Has_Unknown_Discriminants (Original_Type)
18911 or else (Present (N)
18912 and then Nkind (N) = N_Selected_Component
18913 and then Nkind (Prefix (N)) = N_Type_Conversion
18914 and then not Comes_From_Source (Prefix (N))))
18918 -- In the body of an instantiation, check the visibility of a component
18919 -- in case it has a homograph that is a primitive operation of a private
18920 -- type which was not visible in the generic unit.
18922 -- Should Is_Prefixed_Call be propagated from template to instance???
18924 elsif In_Instance_Body then
18925 if not Is_Tagged_Type (Original_Type)
18926 or else not Is_Private_Type (Original_Type)
18932 Subp_Elmt : Elmt_Id;
18935 Subp_Elmt := First_Elmt (Primitive_Operations (Original_Type));
18936 while Present (Subp_Elmt) loop
18938 -- The component is hidden by a primitive operation
18940 if Chars (Node (Subp_Elmt)) = Chars (C) then
18944 Next_Elmt (Subp_Elmt);
18951 -- If the component has been declared in an ancestor which is currently
18952 -- a private type, then it is not visible. The same applies if the
18953 -- component's containing type is not in an open scope and the original
18954 -- component's enclosing type is a visible full view of a private type
18955 -- (which can occur in cases where an attempt is being made to reference
18956 -- a component in a sibling package that is inherited from a visible
18957 -- component of a type in an ancestor package; the component in the
18958 -- sibling package should not be visible even though the component it
18959 -- inherited from is visible). This does not apply however in the case
18960 -- where the scope of the type is a private child unit, or when the
18961 -- parent comes from a local package in which the ancestor is currently
18962 -- visible. The latter suppression of visibility is needed for cases
18963 -- that are tested in B730006.
18965 elsif Is_Private_Type (Original_Type)
18967 (not Is_Private_Descendant (Type_Scope)
18968 and then not In_Open_Scopes (Type_Scope)
18969 and then Has_Private_Declaration (Original_Type))
18971 -- If the type derives from an entity in a formal package, there
18972 -- are no additional visible components.
18974 if Nkind (Original_Node (Unit_Declaration_Node (Type_Scope))) =
18975 N_Formal_Package_Declaration
18979 -- if we are not in the private part of the current package, there
18980 -- are no additional visible components.
18982 elsif Ekind (Scope (Current_Scope)) = E_Package
18983 and then not In_Private_Part (Scope (Current_Scope))
18988 Is_Child_Unit (Cunit_Entity (Current_Sem_Unit))
18989 and then In_Open_Scopes (Scope (Original_Type))
18990 and then Is_Local_Type (Type_Scope);
18993 -- There is another weird way in which a component may be invisible when
18994 -- the private and the full view are not derived from the same ancestor.
18995 -- Here is an example :
18997 -- type A1 is tagged record F1 : integer; end record;
18998 -- type A2 is new A1 with record F2 : integer; end record;
18999 -- type T is new A1 with private;
19001 -- type T is new A2 with null record;
19003 -- In this case, the full view of T inherits F1 and F2 but the private
19004 -- view inherits only F1
19008 Ancestor : Entity_Id := Scope (C);
19012 if Ancestor = Original_Type then
19015 -- The ancestor may have a partial view of the original type,
19016 -- but if the full view is in scope, as in a child body, the
19017 -- component is visible.
19019 elsif In_Private_Part (Scope (Original_Type))
19020 and then Full_View (Ancestor) = Original_Type
19024 elsif Ancestor = Etype (Ancestor) then
19026 -- No further ancestors to examine
19031 Ancestor := Etype (Ancestor);
19035 end Is_Visible_Component;
19037 --------------------------
19038 -- Make_Class_Wide_Type --
19039 --------------------------
19041 procedure Make_Class_Wide_Type (T : Entity_Id) is
19042 CW_Type : Entity_Id;
19044 Next_E : Entity_Id;
19045 Prev_E : Entity_Id;
19048 if Present (Class_Wide_Type (T)) then
19050 -- The class-wide type is a partially decorated entity created for a
19051 -- unanalyzed tagged type referenced through a limited with clause.
19052 -- When the tagged type is analyzed, its class-wide type needs to be
19053 -- redecorated. Note that we reuse the entity created by Decorate_
19054 -- Tagged_Type in order to preserve all links.
19056 if Materialize_Entity (Class_Wide_Type (T)) then
19057 CW_Type := Class_Wide_Type (T);
19058 Set_Materialize_Entity (CW_Type, False);
19060 -- The class wide type can have been defined by the partial view, in
19061 -- which case everything is already done.
19067 -- Default case, we need to create a new class-wide type
19071 New_External_Entity (E_Void, Scope (T), Sloc (T), T, 'C', 0, 'T');
19074 -- Inherit root type characteristics
19076 CW_Name := Chars (CW_Type);
19077 Next_E := Next_Entity (CW_Type);
19078 Prev_E := Prev_Entity (CW_Type);
19079 Copy_Node (T, CW_Type);
19080 Set_Comes_From_Source (CW_Type, False);
19081 Set_Chars (CW_Type, CW_Name);
19082 Set_Parent (CW_Type, Parent (T));
19083 Set_Prev_Entity (CW_Type, Prev_E);
19084 Set_Next_Entity (CW_Type, Next_E);
19086 -- Ensure we have a new freeze node for the class-wide type. The partial
19087 -- view may have freeze action of its own, requiring a proper freeze
19088 -- node, and the same freeze node cannot be shared between the two
19091 Set_Has_Delayed_Freeze (CW_Type);
19092 Set_Freeze_Node (CW_Type, Empty);
19094 -- Customize the class-wide type: It has no prim. op., it cannot be
19095 -- abstract, its Etype points back to the specific root type, and it
19096 -- cannot have any invariants.
19098 Set_Ekind (CW_Type, E_Class_Wide_Type);
19099 Set_Is_Tagged_Type (CW_Type, True);
19100 Set_Direct_Primitive_Operations (CW_Type, New_Elmt_List);
19101 Set_Is_Abstract_Type (CW_Type, False);
19102 Set_Is_Constrained (CW_Type, False);
19103 Set_Is_First_Subtype (CW_Type, Is_First_Subtype (T));
19104 Set_Default_SSO (CW_Type);
19105 Set_Has_Inheritable_Invariants (CW_Type, False);
19106 Set_Has_Inherited_Invariants (CW_Type, False);
19107 Set_Has_Own_Invariants (CW_Type, False);
19109 if Ekind (T) = E_Class_Wide_Subtype then
19110 Set_Etype (CW_Type, Etype (Base_Type (T)));
19112 Set_Etype (CW_Type, T);
19115 Set_No_Tagged_Streams_Pragma (CW_Type, No_Tagged_Streams);
19117 -- If this is the class_wide type of a constrained subtype, it does
19118 -- not have discriminants.
19120 Set_Has_Discriminants (CW_Type,
19121 Has_Discriminants (T) and then not Is_Constrained (T));
19123 Set_Has_Unknown_Discriminants (CW_Type, True);
19124 Set_Class_Wide_Type (T, CW_Type);
19125 Set_Equivalent_Type (CW_Type, Empty);
19127 -- The class-wide type of a class-wide type is itself (RM 3.9(14))
19129 Set_Class_Wide_Type (CW_Type, CW_Type);
19130 end Make_Class_Wide_Type;
19136 procedure Make_Index
19138 Related_Nod : Node_Id;
19139 Related_Id : Entity_Id := Empty;
19140 Suffix_Index : Nat := 1;
19141 In_Iter_Schm : Boolean := False)
19145 Def_Id : Entity_Id := Empty;
19146 Found : Boolean := False;
19149 -- For a discrete range used in a constrained array definition and
19150 -- defined by a range, an implicit conversion to the predefined type
19151 -- INTEGER is assumed if each bound is either a numeric literal, a named
19152 -- number, or an attribute, and the type of both bounds (prior to the
19153 -- implicit conversion) is the type universal_integer. Otherwise, both
19154 -- bounds must be of the same discrete type, other than universal
19155 -- integer; this type must be determinable independently of the
19156 -- context, but using the fact that the type must be discrete and that
19157 -- both bounds must have the same type.
19159 -- Character literals also have a universal type in the absence of
19160 -- of additional context, and are resolved to Standard_Character.
19162 if Nkind (N) = N_Range then
19164 -- The index is given by a range constraint. The bounds are known
19165 -- to be of a consistent type.
19167 if not Is_Overloaded (N) then
19170 -- For universal bounds, choose the specific predefined type
19172 if T = Universal_Integer then
19173 T := Standard_Integer;
19175 elsif T = Any_Character then
19176 Ambiguous_Character (Low_Bound (N));
19178 T := Standard_Character;
19181 -- The node may be overloaded because some user-defined operators
19182 -- are available, but if a universal interpretation exists it is
19183 -- also the selected one.
19185 elsif Universal_Interpretation (N) = Universal_Integer then
19186 T := Standard_Integer;
19192 Ind : Interp_Index;
19196 Get_First_Interp (N, Ind, It);
19197 while Present (It.Typ) loop
19198 if Is_Discrete_Type (It.Typ) then
19201 and then not Covers (It.Typ, T)
19202 and then not Covers (T, It.Typ)
19204 Error_Msg_N ("ambiguous bounds in discrete range", N);
19212 Get_Next_Interp (Ind, It);
19215 if T = Any_Type then
19216 Error_Msg_N ("discrete type required for range", N);
19217 Set_Etype (N, Any_Type);
19220 elsif T = Universal_Integer then
19221 T := Standard_Integer;
19226 if not Is_Discrete_Type (T) then
19227 Error_Msg_N ("discrete type required for range", N);
19228 Set_Etype (N, Any_Type);
19232 if Nkind (Low_Bound (N)) = N_Attribute_Reference
19233 and then Attribute_Name (Low_Bound (N)) = Name_First
19234 and then Is_Entity_Name (Prefix (Low_Bound (N)))
19235 and then Is_Type (Entity (Prefix (Low_Bound (N))))
19236 and then Is_Discrete_Type (Entity (Prefix (Low_Bound (N))))
19238 -- The type of the index will be the type of the prefix, as long
19239 -- as the upper bound is 'Last of the same type.
19241 Def_Id := Entity (Prefix (Low_Bound (N)));
19243 if Nkind (High_Bound (N)) /= N_Attribute_Reference
19244 or else Attribute_Name (High_Bound (N)) /= Name_Last
19245 or else not Is_Entity_Name (Prefix (High_Bound (N)))
19246 or else Entity (Prefix (High_Bound (N))) /= Def_Id
19253 Process_Range_Expr_In_Decl (R, T, In_Iter_Schm => In_Iter_Schm);
19255 elsif Nkind (N) = N_Subtype_Indication then
19257 -- The index is given by a subtype with a range constraint
19259 T := Base_Type (Entity (Subtype_Mark (N)));
19261 if not Is_Discrete_Type (T) then
19262 Error_Msg_N ("discrete type required for range", N);
19263 Set_Etype (N, Any_Type);
19267 R := Range_Expression (Constraint (N));
19270 Process_Range_Expr_In_Decl
19271 (R, Entity (Subtype_Mark (N)), In_Iter_Schm => In_Iter_Schm);
19273 elsif Nkind (N) = N_Attribute_Reference then
19275 -- Catch beginner's error (use of attribute other than 'Range)
19277 if Attribute_Name (N) /= Name_Range then
19278 Error_Msg_N ("expect attribute ''Range", N);
19279 Set_Etype (N, Any_Type);
19283 -- If the node denotes the range of a type mark, that is also the
19284 -- resulting type, and we do not need to create an Itype for it.
19286 if Is_Entity_Name (Prefix (N))
19287 and then Comes_From_Source (N)
19288 and then Is_Type (Entity (Prefix (N)))
19289 and then Is_Discrete_Type (Entity (Prefix (N)))
19291 Def_Id := Entity (Prefix (N));
19294 Analyze_And_Resolve (N);
19298 -- If none of the above, must be a subtype. We convert this to a
19299 -- range attribute reference because in the case of declared first
19300 -- named subtypes, the types in the range reference can be different
19301 -- from the type of the entity. A range attribute normalizes the
19302 -- reference and obtains the correct types for the bounds.
19304 -- This transformation is in the nature of an expansion, is only
19305 -- done if expansion is active. In particular, it is not done on
19306 -- formal generic types, because we need to retain the name of the
19307 -- original index for instantiation purposes.
19310 if not Is_Entity_Name (N) or else not Is_Type (Entity (N)) then
19311 Error_Msg_N ("invalid subtype mark in discrete range ", N);
19312 Set_Etype (N, Any_Integer);
19316 -- The type mark may be that of an incomplete type. It is only
19317 -- now that we can get the full view, previous analysis does
19318 -- not look specifically for a type mark.
19320 Set_Entity (N, Get_Full_View (Entity (N)));
19321 Set_Etype (N, Entity (N));
19322 Def_Id := Entity (N);
19324 if not Is_Discrete_Type (Def_Id) then
19325 Error_Msg_N ("discrete type required for index", N);
19326 Set_Etype (N, Any_Type);
19331 if Expander_Active then
19333 Make_Attribute_Reference (Sloc (N),
19334 Attribute_Name => Name_Range,
19335 Prefix => Relocate_Node (N)));
19337 -- The original was a subtype mark that does not freeze. This
19338 -- means that the rewritten version must not freeze either.
19340 Set_Must_Not_Freeze (N);
19341 Set_Must_Not_Freeze (Prefix (N));
19342 Analyze_And_Resolve (N);
19346 -- If expander is inactive, type is legal, nothing else to construct
19353 if not Is_Discrete_Type (T) then
19354 Error_Msg_N ("discrete type required for range", N);
19355 Set_Etype (N, Any_Type);
19358 elsif T = Any_Type then
19359 Set_Etype (N, Any_Type);
19363 -- We will now create the appropriate Itype to describe the range, but
19364 -- first a check. If we originally had a subtype, then we just label
19365 -- the range with this subtype. Not only is there no need to construct
19366 -- a new subtype, but it is wrong to do so for two reasons:
19368 -- 1. A legality concern, if we have a subtype, it must not freeze,
19369 -- and the Itype would cause freezing incorrectly
19371 -- 2. An efficiency concern, if we created an Itype, it would not be
19372 -- recognized as the same type for the purposes of eliminating
19373 -- checks in some circumstances.
19375 -- We signal this case by setting the subtype entity in Def_Id
19377 if No (Def_Id) then
19379 Create_Itype (E_Void, Related_Nod, Related_Id, 'D', Suffix_Index);
19380 Set_Etype (Def_Id, Base_Type (T));
19382 if Is_Signed_Integer_Type (T) then
19383 Set_Ekind (Def_Id, E_Signed_Integer_Subtype);
19385 elsif Is_Modular_Integer_Type (T) then
19386 Set_Ekind (Def_Id, E_Modular_Integer_Subtype);
19389 Set_Ekind (Def_Id, E_Enumeration_Subtype);
19390 Set_Is_Character_Type (Def_Id, Is_Character_Type (T));
19391 Set_First_Literal (Def_Id, First_Literal (T));
19394 Set_Size_Info (Def_Id, (T));
19395 Set_RM_Size (Def_Id, RM_Size (T));
19396 Set_First_Rep_Item (Def_Id, First_Rep_Item (T));
19398 Set_Scalar_Range (Def_Id, R);
19399 Conditional_Delay (Def_Id, T);
19401 if Nkind (N) = N_Subtype_Indication then
19402 Inherit_Predicate_Flags (Def_Id, Entity (Subtype_Mark (N)));
19405 -- In the subtype indication case, if the immediate parent of the
19406 -- new subtype is nonstatic, then the subtype we create is nonstatic,
19407 -- even if its bounds are static.
19409 if Nkind (N) = N_Subtype_Indication
19410 and then not Is_OK_Static_Subtype (Entity (Subtype_Mark (N)))
19412 Set_Is_Non_Static_Subtype (Def_Id);
19416 -- Final step is to label the index with this constructed type
19418 Set_Etype (N, Def_Id);
19421 ------------------------------
19422 -- Modular_Type_Declaration --
19423 ------------------------------
19425 procedure Modular_Type_Declaration (T : Entity_Id; Def : Node_Id) is
19426 Mod_Expr : constant Node_Id := Expression (Def);
19429 procedure Set_Modular_Size (Bits : Int);
19430 -- Sets RM_Size to Bits, and Esize to normal word size above this
19432 ----------------------
19433 -- Set_Modular_Size --
19434 ----------------------
19436 procedure Set_Modular_Size (Bits : Int) is
19438 Set_RM_Size (T, UI_From_Int (Bits));
19443 elsif Bits <= 16 then
19444 Init_Esize (T, 16);
19446 elsif Bits <= 32 then
19447 Init_Esize (T, 32);
19450 Init_Esize (T, System_Max_Binary_Modulus_Power);
19453 if not Non_Binary_Modulus (T) and then Esize (T) = RM_Size (T) then
19454 Set_Is_Known_Valid (T);
19456 end Set_Modular_Size;
19458 -- Start of processing for Modular_Type_Declaration
19461 -- If the mod expression is (exactly) 2 * literal, where literal is
19462 -- 64 or less,then almost certainly the * was meant to be **. Warn.
19464 if Warn_On_Suspicious_Modulus_Value
19465 and then Nkind (Mod_Expr) = N_Op_Multiply
19466 and then Nkind (Left_Opnd (Mod_Expr)) = N_Integer_Literal
19467 and then Intval (Left_Opnd (Mod_Expr)) = Uint_2
19468 and then Nkind (Right_Opnd (Mod_Expr)) = N_Integer_Literal
19469 and then Intval (Right_Opnd (Mod_Expr)) <= Uint_64
19472 ("suspicious MOD value, was '*'* intended'??M?", Mod_Expr);
19475 -- Proceed with analysis of mod expression
19477 Analyze_And_Resolve (Mod_Expr, Any_Integer);
19479 Set_Ekind (T, E_Modular_Integer_Type);
19480 Init_Alignment (T);
19481 Set_Is_Constrained (T);
19483 if not Is_OK_Static_Expression (Mod_Expr) then
19484 Flag_Non_Static_Expr
19485 ("non-static expression used for modular type bound!", Mod_Expr);
19486 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19488 M_Val := Expr_Value (Mod_Expr);
19492 Error_Msg_N ("modulus value must be positive", Mod_Expr);
19493 M_Val := 2 ** System_Max_Binary_Modulus_Power;
19496 if M_Val > 2 ** Standard_Long_Integer_Size then
19497 Check_Restriction (No_Long_Long_Integers, Mod_Expr);
19500 Set_Modulus (T, M_Val);
19502 -- Create bounds for the modular type based on the modulus given in
19503 -- the type declaration and then analyze and resolve those bounds.
19505 Set_Scalar_Range (T,
19506 Make_Range (Sloc (Mod_Expr),
19507 Low_Bound => Make_Integer_Literal (Sloc (Mod_Expr), 0),
19508 High_Bound => Make_Integer_Literal (Sloc (Mod_Expr), M_Val - 1)));
19510 -- Properly analyze the literals for the range. We do this manually
19511 -- because we can't go calling Resolve, since we are resolving these
19512 -- bounds with the type, and this type is certainly not complete yet.
19514 Set_Etype (Low_Bound (Scalar_Range (T)), T);
19515 Set_Etype (High_Bound (Scalar_Range (T)), T);
19516 Set_Is_Static_Expression (Low_Bound (Scalar_Range (T)));
19517 Set_Is_Static_Expression (High_Bound (Scalar_Range (T)));
19519 -- Loop through powers of two to find number of bits required
19521 for Bits in Int range 0 .. System_Max_Binary_Modulus_Power loop
19525 if M_Val = 2 ** Bits then
19526 Set_Modular_Size (Bits);
19531 elsif M_Val < 2 ** Bits then
19532 Check_SPARK_05_Restriction ("modulus should be a power of 2", T);
19533 Set_Non_Binary_Modulus (T);
19535 if Bits > System_Max_Nonbinary_Modulus_Power then
19536 Error_Msg_Uint_1 :=
19537 UI_From_Int (System_Max_Nonbinary_Modulus_Power);
19539 ("nonbinary modulus exceeds limit (2 '*'*^ - 1)", Mod_Expr);
19540 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19544 -- In the nonbinary case, set size as per RM 13.3(55)
19546 Set_Modular_Size (Bits);
19553 -- If we fall through, then the size exceed System.Max_Binary_Modulus
19554 -- so we just signal an error and set the maximum size.
19556 Error_Msg_Uint_1 := UI_From_Int (System_Max_Binary_Modulus_Power);
19557 Error_Msg_F ("modulus exceeds limit (2 '*'*^)", Mod_Expr);
19559 Set_Modular_Size (System_Max_Binary_Modulus_Power);
19560 Init_Alignment (T);
19562 end Modular_Type_Declaration;
19564 --------------------------
19565 -- New_Concatenation_Op --
19566 --------------------------
19568 procedure New_Concatenation_Op (Typ : Entity_Id) is
19569 Loc : constant Source_Ptr := Sloc (Typ);
19572 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id;
19573 -- Create abbreviated declaration for the formal of a predefined
19574 -- Operator 'Op' of type 'Typ'
19576 --------------------
19577 -- Make_Op_Formal --
19578 --------------------
19580 function Make_Op_Formal (Typ, Op : Entity_Id) return Entity_Id is
19581 Formal : Entity_Id;
19583 Formal := New_Internal_Entity (E_In_Parameter, Op, Loc, 'P');
19584 Set_Etype (Formal, Typ);
19585 Set_Mechanism (Formal, Default_Mechanism);
19587 end Make_Op_Formal;
19589 -- Start of processing for New_Concatenation_Op
19592 Op := Make_Defining_Operator_Symbol (Loc, Name_Op_Concat);
19594 Set_Ekind (Op, E_Operator);
19595 Set_Scope (Op, Current_Scope);
19596 Set_Etype (Op, Typ);
19597 Set_Homonym (Op, Get_Name_Entity_Id (Name_Op_Concat));
19598 Set_Is_Immediately_Visible (Op);
19599 Set_Is_Intrinsic_Subprogram (Op);
19600 Set_Has_Completion (Op);
19601 Append_Entity (Op, Current_Scope);
19603 Set_Name_Entity_Id (Name_Op_Concat, Op);
19605 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19606 Append_Entity (Make_Op_Formal (Typ, Op), Op);
19607 end New_Concatenation_Op;
19609 -------------------------
19610 -- OK_For_Limited_Init --
19611 -------------------------
19613 -- ???Check all calls of this, and compare the conditions under which it's
19616 function OK_For_Limited_Init
19618 Exp : Node_Id) return Boolean
19621 return Is_CPP_Constructor_Call (Exp)
19622 or else (Ada_Version >= Ada_2005
19623 and then not Debug_Flag_Dot_L
19624 and then OK_For_Limited_Init_In_05 (Typ, Exp));
19625 end OK_For_Limited_Init;
19627 -------------------------------
19628 -- OK_For_Limited_Init_In_05 --
19629 -------------------------------
19631 function OK_For_Limited_Init_In_05
19633 Exp : Node_Id) return Boolean
19636 -- An object of a limited interface type can be initialized with any
19637 -- expression of a nonlimited descendant type. However this does not
19638 -- apply if this is a view conversion of some other expression. This
19639 -- is checked below.
19641 if Is_Class_Wide_Type (Typ)
19642 and then Is_Limited_Interface (Typ)
19643 and then not Is_Limited_Type (Etype (Exp))
19644 and then Nkind (Exp) /= N_Type_Conversion
19649 -- Ada 2005 (AI-287, AI-318): Relax the strictness of the front end in
19650 -- case of limited aggregates (including extension aggregates), and
19651 -- function calls. The function call may have been given in prefixed
19652 -- notation, in which case the original node is an indexed component.
19653 -- If the function is parameterless, the original node was an explicit
19654 -- dereference. The function may also be parameterless, in which case
19655 -- the source node is just an identifier.
19657 -- A branch of a conditional expression may have been removed if the
19658 -- condition is statically known. This happens during expansion, and
19659 -- thus will not happen if previous errors were encountered. The check
19660 -- will have been performed on the chosen branch, which replaces the
19661 -- original conditional expression.
19667 case Nkind (Original_Node (Exp)) is
19669 | N_Extension_Aggregate
19675 when N_Identifier =>
19676 return Present (Entity (Original_Node (Exp)))
19677 and then Ekind (Entity (Original_Node (Exp))) = E_Function;
19679 when N_Qualified_Expression =>
19681 OK_For_Limited_Init_In_05
19682 (Typ, Expression (Original_Node (Exp)));
19684 -- Ada 2005 (AI-251): If a class-wide interface object is initialized
19685 -- with a function call, the expander has rewritten the call into an
19686 -- N_Type_Conversion node to force displacement of the pointer to
19687 -- reference the component containing the secondary dispatch table.
19688 -- Otherwise a type conversion is not a legal context.
19689 -- A return statement for a build-in-place function returning a
19690 -- synchronized type also introduces an unchecked conversion.
19692 when N_Type_Conversion
19693 | N_Unchecked_Type_Conversion
19695 return not Comes_From_Source (Exp)
19697 OK_For_Limited_Init_In_05
19698 (Typ, Expression (Original_Node (Exp)));
19700 when N_Explicit_Dereference
19701 | N_Indexed_Component
19702 | N_Selected_Component
19704 return Nkind (Exp) = N_Function_Call;
19706 -- A use of 'Input is a function call, hence allowed. Normally the
19707 -- attribute will be changed to a call, but the attribute by itself
19708 -- can occur with -gnatc.
19710 when N_Attribute_Reference =>
19711 return Attribute_Name (Original_Node (Exp)) = Name_Input;
19713 -- "return raise ..." is OK
19715 when N_Raise_Expression =>
19718 -- For a case expression, all dependent expressions must be legal
19720 when N_Case_Expression =>
19725 Alt := First (Alternatives (Original_Node (Exp)));
19726 while Present (Alt) loop
19727 if not OK_For_Limited_Init_In_05 (Typ, Expression (Alt)) then
19737 -- For an if expression, all dependent expressions must be legal
19739 when N_If_Expression =>
19741 Then_Expr : constant Node_Id :=
19742 Next (First (Expressions (Original_Node (Exp))));
19743 Else_Expr : constant Node_Id := Next (Then_Expr);
19745 return OK_For_Limited_Init_In_05 (Typ, Then_Expr)
19747 OK_For_Limited_Init_In_05 (Typ, Else_Expr);
19753 end OK_For_Limited_Init_In_05;
19755 -------------------------------------------
19756 -- Ordinary_Fixed_Point_Type_Declaration --
19757 -------------------------------------------
19759 procedure Ordinary_Fixed_Point_Type_Declaration
19763 Loc : constant Source_Ptr := Sloc (Def);
19764 Delta_Expr : constant Node_Id := Delta_Expression (Def);
19765 RRS : constant Node_Id := Real_Range_Specification (Def);
19766 Implicit_Base : Entity_Id;
19773 Check_Restriction (No_Fixed_Point, Def);
19775 -- Create implicit base type
19778 Create_Itype (E_Ordinary_Fixed_Point_Type, Parent (Def), T, 'B');
19779 Set_Etype (Implicit_Base, Implicit_Base);
19781 -- Analyze and process delta expression
19783 Analyze_And_Resolve (Delta_Expr, Any_Real);
19785 Check_Delta_Expression (Delta_Expr);
19786 Delta_Val := Expr_Value_R (Delta_Expr);
19788 Set_Delta_Value (Implicit_Base, Delta_Val);
19790 -- Compute default small from given delta, which is the largest power
19791 -- of two that does not exceed the given delta value.
19801 if Delta_Val < Ureal_1 then
19802 while Delta_Val < Tmp loop
19803 Tmp := Tmp / Ureal_2;
19804 Scale := Scale + 1;
19809 Tmp := Tmp * Ureal_2;
19810 exit when Tmp > Delta_Val;
19811 Scale := Scale - 1;
19815 Small_Val := UR_From_Components (Uint_1, UI_From_Int (Scale), 2);
19818 Set_Small_Value (Implicit_Base, Small_Val);
19820 -- If no range was given, set a dummy range
19822 if RRS <= Empty_Or_Error then
19823 Low_Val := -Small_Val;
19824 High_Val := Small_Val;
19826 -- Otherwise analyze and process given range
19830 Low : constant Node_Id := Low_Bound (RRS);
19831 High : constant Node_Id := High_Bound (RRS);
19834 Analyze_And_Resolve (Low, Any_Real);
19835 Analyze_And_Resolve (High, Any_Real);
19836 Check_Real_Bound (Low);
19837 Check_Real_Bound (High);
19839 -- Obtain and set the range
19841 Low_Val := Expr_Value_R (Low);
19842 High_Val := Expr_Value_R (High);
19844 if Low_Val > High_Val then
19845 Error_Msg_NE ("??fixed point type& has null range", Def, T);
19850 -- The range for both the implicit base and the declared first subtype
19851 -- cannot be set yet, so we use the special routine Set_Fixed_Range to
19852 -- set a temporary range in place. Note that the bounds of the base
19853 -- type will be widened to be symmetrical and to fill the available
19854 -- bits when the type is frozen.
19856 -- We could do this with all discrete types, and probably should, but
19857 -- we absolutely have to do it for fixed-point, since the end-points
19858 -- of the range and the size are determined by the small value, which
19859 -- could be reset before the freeze point.
19861 Set_Fixed_Range (Implicit_Base, Loc, Low_Val, High_Val);
19862 Set_Fixed_Range (T, Loc, Low_Val, High_Val);
19864 -- Complete definition of first subtype. The inheritance of the rep item
19865 -- chain ensures that SPARK-related pragmas are not clobbered when the
19866 -- ordinary fixed point type acts as a full view of a private type.
19868 Set_Ekind (T, E_Ordinary_Fixed_Point_Subtype);
19869 Set_Etype (T, Implicit_Base);
19870 Init_Size_Align (T);
19871 Inherit_Rep_Item_Chain (T, Implicit_Base);
19872 Set_Small_Value (T, Small_Val);
19873 Set_Delta_Value (T, Delta_Val);
19874 Set_Is_Constrained (T);
19875 end Ordinary_Fixed_Point_Type_Declaration;
19877 ----------------------------------
19878 -- Preanalyze_Assert_Expression --
19879 ----------------------------------
19881 procedure Preanalyze_Assert_Expression (N : Node_Id; T : Entity_Id) is
19883 In_Assertion_Expr := In_Assertion_Expr + 1;
19884 Preanalyze_Spec_Expression (N, T);
19885 In_Assertion_Expr := In_Assertion_Expr - 1;
19886 end Preanalyze_Assert_Expression;
19888 -----------------------------------
19889 -- Preanalyze_Default_Expression --
19890 -----------------------------------
19892 procedure Preanalyze_Default_Expression (N : Node_Id; T : Entity_Id) is
19893 Save_In_Default_Expr : constant Boolean := In_Default_Expr;
19894 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19897 In_Default_Expr := True;
19898 In_Spec_Expression := True;
19900 Preanalyze_With_Freezing_And_Resolve (N, T);
19902 In_Default_Expr := Save_In_Default_Expr;
19903 In_Spec_Expression := Save_In_Spec_Expression;
19904 end Preanalyze_Default_Expression;
19906 --------------------------------
19907 -- Preanalyze_Spec_Expression --
19908 --------------------------------
19910 procedure Preanalyze_Spec_Expression (N : Node_Id; T : Entity_Id) is
19911 Save_In_Spec_Expression : constant Boolean := In_Spec_Expression;
19913 In_Spec_Expression := True;
19914 Preanalyze_And_Resolve (N, T);
19915 In_Spec_Expression := Save_In_Spec_Expression;
19916 end Preanalyze_Spec_Expression;
19918 ----------------------------------------
19919 -- Prepare_Private_Subtype_Completion --
19920 ----------------------------------------
19922 procedure Prepare_Private_Subtype_Completion
19924 Related_Nod : Node_Id)
19926 Id_B : constant Entity_Id := Base_Type (Id);
19927 Full_B : Entity_Id := Full_View (Id_B);
19931 if Present (Full_B) then
19933 -- Get to the underlying full view if necessary
19935 if Is_Private_Type (Full_B)
19936 and then Present (Underlying_Full_View (Full_B))
19938 Full_B := Underlying_Full_View (Full_B);
19941 -- The Base_Type is already completed, we can complete the subtype
19942 -- now. We have to create a new entity with the same name, Thus we
19943 -- can't use Create_Itype.
19945 Full := Make_Defining_Identifier (Sloc (Id), Chars (Id));
19946 Set_Is_Itype (Full);
19947 Set_Associated_Node_For_Itype (Full, Related_Nod);
19948 Complete_Private_Subtype (Id, Full, Full_B, Related_Nod);
19951 -- The parent subtype may be private, but the base might not, in some
19952 -- nested instances. In that case, the subtype does not need to be
19953 -- exchanged. It would still be nice to make private subtypes and their
19954 -- bases consistent at all times ???
19956 if Is_Private_Type (Id_B) then
19957 Append_Elmt (Id, Private_Dependents (Id_B));
19959 end Prepare_Private_Subtype_Completion;
19961 ---------------------------
19962 -- Process_Discriminants --
19963 ---------------------------
19965 procedure Process_Discriminants
19967 Prev : Entity_Id := Empty)
19969 Elist : constant Elist_Id := New_Elmt_List;
19972 Discr_Number : Uint;
19973 Discr_Type : Entity_Id;
19974 Default_Present : Boolean := False;
19975 Default_Not_Present : Boolean := False;
19978 -- A composite type other than an array type can have discriminants.
19979 -- On entry, the current scope is the composite type.
19981 -- The discriminants are initially entered into the scope of the type
19982 -- via Enter_Name with the default Ekind of E_Void to prevent premature
19983 -- use, as explained at the end of this procedure.
19985 Discr := First (Discriminant_Specifications (N));
19986 while Present (Discr) loop
19987 Enter_Name (Defining_Identifier (Discr));
19989 -- For navigation purposes we add a reference to the discriminant
19990 -- in the entity for the type. If the current declaration is a
19991 -- completion, place references on the partial view. Otherwise the
19992 -- type is the current scope.
19994 if Present (Prev) then
19996 -- The references go on the partial view, if present. If the
19997 -- partial view has discriminants, the references have been
19998 -- generated already.
20000 if not Has_Discriminants (Prev) then
20001 Generate_Reference (Prev, Defining_Identifier (Discr), 'd');
20005 (Current_Scope, Defining_Identifier (Discr), 'd');
20008 if Nkind (Discriminant_Type (Discr)) = N_Access_Definition then
20009 Discr_Type := Access_Definition (Discr, Discriminant_Type (Discr));
20011 -- Ada 2005 (AI-254)
20013 if Present (Access_To_Subprogram_Definition
20014 (Discriminant_Type (Discr)))
20015 and then Protected_Present (Access_To_Subprogram_Definition
20016 (Discriminant_Type (Discr)))
20019 Replace_Anonymous_Access_To_Protected_Subprogram (Discr);
20023 Find_Type (Discriminant_Type (Discr));
20024 Discr_Type := Etype (Discriminant_Type (Discr));
20026 if Error_Posted (Discriminant_Type (Discr)) then
20027 Discr_Type := Any_Type;
20031 -- Handling of discriminants that are access types
20033 if Is_Access_Type (Discr_Type) then
20035 -- Ada 2005 (AI-230): Access discriminant allowed in non-
20036 -- limited record types
20038 if Ada_Version < Ada_2005 then
20039 Check_Access_Discriminant_Requires_Limited
20040 (Discr, Discriminant_Type (Discr));
20043 if Ada_Version = Ada_83 and then Comes_From_Source (Discr) then
20045 ("(Ada 83) access discriminant not allowed", Discr);
20048 -- If not access type, must be a discrete type
20050 elsif not Is_Discrete_Type (Discr_Type) then
20052 ("discriminants must have a discrete or access type",
20053 Discriminant_Type (Discr));
20056 Set_Etype (Defining_Identifier (Discr), Discr_Type);
20058 -- If a discriminant specification includes the assignment compound
20059 -- delimiter followed by an expression, the expression is the default
20060 -- expression of the discriminant; the default expression must be of
20061 -- the type of the discriminant. (RM 3.7.1) Since this expression is
20062 -- a default expression, we do the special preanalysis, since this
20063 -- expression does not freeze (see section "Handling of Default and
20064 -- Per-Object Expressions" in spec of package Sem).
20066 if Present (Expression (Discr)) then
20067 Preanalyze_Spec_Expression (Expression (Discr), Discr_Type);
20071 if Nkind (N) = N_Formal_Type_Declaration then
20073 ("discriminant defaults not allowed for formal type",
20074 Expression (Discr));
20076 -- Flag an error for a tagged type with defaulted discriminants,
20077 -- excluding limited tagged types when compiling for Ada 2012
20078 -- (see AI05-0214).
20080 elsif Is_Tagged_Type (Current_Scope)
20081 and then (not Is_Limited_Type (Current_Scope)
20082 or else Ada_Version < Ada_2012)
20083 and then Comes_From_Source (N)
20085 -- Note: see similar test in Check_Or_Process_Discriminants, to
20086 -- handle the (illegal) case of the completion of an untagged
20087 -- view with discriminants with defaults by a tagged full view.
20088 -- We skip the check if Discr does not come from source, to
20089 -- account for the case of an untagged derived type providing
20090 -- defaults for a renamed discriminant from a private untagged
20091 -- ancestor with a tagged full view (ACATS B460006).
20093 if Ada_Version >= Ada_2012 then
20095 ("discriminants of nonlimited tagged type cannot have"
20097 Expression (Discr));
20100 ("discriminants of tagged type cannot have defaults",
20101 Expression (Discr));
20105 Default_Present := True;
20106 Append_Elmt (Expression (Discr), Elist);
20108 -- Tag the defining identifiers for the discriminants with
20109 -- their corresponding default expressions from the tree.
20111 Set_Discriminant_Default_Value
20112 (Defining_Identifier (Discr), Expression (Discr));
20115 -- In gnatc or gnatprove mode, make sure set Do_Range_Check flag
20116 -- gets set unless we can be sure that no range check is required.
20118 if (GNATprove_Mode or not Expander_Active)
20121 (Expression (Discr), Discr_Type, Assume_Valid => True)
20123 Set_Do_Range_Check (Expression (Discr));
20126 -- No default discriminant value given
20129 Default_Not_Present := True;
20132 -- Ada 2005 (AI-231): Create an Itype that is a duplicate of
20133 -- Discr_Type but with the null-exclusion attribute
20135 if Ada_Version >= Ada_2005 then
20137 -- Ada 2005 (AI-231): Static checks
20139 if Can_Never_Be_Null (Discr_Type) then
20140 Null_Exclusion_Static_Checks (Discr);
20142 elsif Is_Access_Type (Discr_Type)
20143 and then Null_Exclusion_Present (Discr)
20145 -- No need to check itypes because in their case this check
20146 -- was done at their point of creation
20148 and then not Is_Itype (Discr_Type)
20150 if Can_Never_Be_Null (Discr_Type) then
20152 ("`NOT NULL` not allowed (& already excludes null)",
20157 Set_Etype (Defining_Identifier (Discr),
20158 Create_Null_Excluding_Itype
20160 Related_Nod => Discr));
20162 -- Check for improper null exclusion if the type is otherwise
20163 -- legal for a discriminant.
20165 elsif Null_Exclusion_Present (Discr)
20166 and then Is_Discrete_Type (Discr_Type)
20169 ("null exclusion can only apply to an access type", Discr);
20172 -- Ada 2005 (AI-402): access discriminants of nonlimited types
20173 -- can't have defaults. Synchronized types, or types that are
20174 -- explicitly limited are fine, but special tests apply to derived
20175 -- types in generics: in a generic body we have to assume the
20176 -- worst, and therefore defaults are not allowed if the parent is
20177 -- a generic formal private type (see ACATS B370001).
20179 if Is_Access_Type (Discr_Type) and then Default_Present then
20180 if Ekind (Discr_Type) /= E_Anonymous_Access_Type
20181 or else Is_Limited_Record (Current_Scope)
20182 or else Is_Concurrent_Type (Current_Scope)
20183 or else Is_Concurrent_Record_Type (Current_Scope)
20184 or else Ekind (Current_Scope) = E_Limited_Private_Type
20186 if not Is_Derived_Type (Current_Scope)
20187 or else not Is_Generic_Type (Etype (Current_Scope))
20188 or else not In_Package_Body (Scope (Etype (Current_Scope)))
20189 or else Limited_Present
20190 (Type_Definition (Parent (Current_Scope)))
20196 ("access discriminants of nonlimited types cannot "
20197 & "have defaults", Expression (Discr));
20200 elsif Present (Expression (Discr)) then
20202 ("(Ada 2005) access discriminants of nonlimited types "
20203 & "cannot have defaults", Expression (Discr));
20208 -- A discriminant cannot be effectively volatile (SPARK RM 7.1.3(4)).
20209 -- This check is relevant only when SPARK_Mode is on as it is not a
20210 -- standard Ada legality rule.
20213 and then Is_Effectively_Volatile (Defining_Identifier (Discr))
20215 Error_Msg_N ("discriminant cannot be volatile", Discr);
20221 -- An element list consisting of the default expressions of the
20222 -- discriminants is constructed in the above loop and used to set
20223 -- the Discriminant_Constraint attribute for the type. If an object
20224 -- is declared of this (record or task) type without any explicit
20225 -- discriminant constraint given, this element list will form the
20226 -- actual parameters for the corresponding initialization procedure
20229 Set_Discriminant_Constraint (Current_Scope, Elist);
20230 Set_Stored_Constraint (Current_Scope, No_Elist);
20232 -- Default expressions must be provided either for all or for none
20233 -- of the discriminants of a discriminant part. (RM 3.7.1)
20235 if Default_Present and then Default_Not_Present then
20237 ("incomplete specification of defaults for discriminants", N);
20240 -- The use of the name of a discriminant is not allowed in default
20241 -- expressions of a discriminant part if the specification of the
20242 -- discriminant is itself given in the discriminant part. (RM 3.7.1)
20244 -- To detect this, the discriminant names are entered initially with an
20245 -- Ekind of E_Void (which is the default Ekind given by Enter_Name). Any
20246 -- attempt to use a void entity (for example in an expression that is
20247 -- type-checked) produces the error message: premature usage. Now after
20248 -- completing the semantic analysis of the discriminant part, we can set
20249 -- the Ekind of all the discriminants appropriately.
20251 Discr := First (Discriminant_Specifications (N));
20252 Discr_Number := Uint_1;
20253 while Present (Discr) loop
20254 Id := Defining_Identifier (Discr);
20255 Set_Ekind (Id, E_Discriminant);
20256 Init_Component_Location (Id);
20258 Set_Discriminant_Number (Id, Discr_Number);
20260 -- Make sure this is always set, even in illegal programs
20262 Set_Corresponding_Discriminant (Id, Empty);
20264 -- Initialize the Original_Record_Component to the entity itself.
20265 -- Inherit_Components will propagate the right value to
20266 -- discriminants in derived record types.
20268 Set_Original_Record_Component (Id, Id);
20270 -- Create the discriminal for the discriminant
20272 Build_Discriminal (Id);
20275 Discr_Number := Discr_Number + 1;
20278 Set_Has_Discriminants (Current_Scope);
20279 end Process_Discriminants;
20281 -----------------------
20282 -- Process_Full_View --
20283 -----------------------
20285 -- WARNING: This routine manages Ghost regions. Return statements must be
20286 -- replaced by gotos which jump to the end of the routine and restore the
20289 procedure Process_Full_View (N : Node_Id; Full_T, Priv_T : Entity_Id) is
20290 procedure Collect_Implemented_Interfaces
20292 Ifaces : Elist_Id);
20293 -- Ada 2005: Gather all the interfaces that Typ directly or
20294 -- inherently implements. Duplicate entries are not added to
20295 -- the list Ifaces.
20297 ------------------------------------
20298 -- Collect_Implemented_Interfaces --
20299 ------------------------------------
20301 procedure Collect_Implemented_Interfaces
20306 Iface_Elmt : Elmt_Id;
20309 -- Abstract interfaces are only associated with tagged record types
20311 if not Is_Tagged_Type (Typ) or else not Is_Record_Type (Typ) then
20315 -- Recursively climb to the ancestors
20317 if Etype (Typ) /= Typ
20319 -- Protect the frontend against wrong cyclic declarations like:
20321 -- type B is new A with private;
20322 -- type C is new A with private;
20324 -- type B is new C with null record;
20325 -- type C is new B with null record;
20327 and then Etype (Typ) /= Priv_T
20328 and then Etype (Typ) /= Full_T
20330 -- Keep separate the management of private type declarations
20332 if Ekind (Typ) = E_Record_Type_With_Private then
20334 -- Handle the following illegal usage:
20335 -- type Private_Type is tagged private;
20337 -- type Private_Type is new Type_Implementing_Iface;
20339 if Present (Full_View (Typ))
20340 and then Etype (Typ) /= Full_View (Typ)
20342 if Is_Interface (Etype (Typ)) then
20343 Append_Unique_Elmt (Etype (Typ), Ifaces);
20346 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20349 -- Non-private types
20352 if Is_Interface (Etype (Typ)) then
20353 Append_Unique_Elmt (Etype (Typ), Ifaces);
20356 Collect_Implemented_Interfaces (Etype (Typ), Ifaces);
20360 -- Handle entities in the list of abstract interfaces
20362 if Present (Interfaces (Typ)) then
20363 Iface_Elmt := First_Elmt (Interfaces (Typ));
20364 while Present (Iface_Elmt) loop
20365 Iface := Node (Iface_Elmt);
20367 pragma Assert (Is_Interface (Iface));
20369 if not Contain_Interface (Iface, Ifaces) then
20370 Append_Elmt (Iface, Ifaces);
20371 Collect_Implemented_Interfaces (Iface, Ifaces);
20374 Next_Elmt (Iface_Elmt);
20377 end Collect_Implemented_Interfaces;
20381 Saved_GM : constant Ghost_Mode_Type := Ghost_Mode;
20382 Saved_IGR : constant Node_Id := Ignored_Ghost_Region;
20383 -- Save the Ghost-related attributes to restore on exit
20385 Full_Indic : Node_Id;
20386 Full_Parent : Entity_Id;
20387 Priv_Parent : Entity_Id;
20389 -- Start of processing for Process_Full_View
20392 Mark_And_Set_Ghost_Completion (N, Priv_T);
20394 -- First some sanity checks that must be done after semantic
20395 -- decoration of the full view and thus cannot be placed with other
20396 -- similar checks in Find_Type_Name
20398 if not Is_Limited_Type (Priv_T)
20399 and then (Is_Limited_Type (Full_T)
20400 or else Is_Limited_Composite (Full_T))
20402 if In_Instance then
20406 ("completion of nonlimited type cannot be limited", Full_T);
20407 Explain_Limited_Type (Full_T, Full_T);
20410 elsif Is_Abstract_Type (Full_T)
20411 and then not Is_Abstract_Type (Priv_T)
20414 ("completion of nonabstract type cannot be abstract", Full_T);
20416 elsif Is_Tagged_Type (Priv_T)
20417 and then Is_Limited_Type (Priv_T)
20418 and then not Is_Limited_Type (Full_T)
20420 -- If pragma CPP_Class was applied to the private declaration
20421 -- propagate the limitedness to the full-view
20423 if Is_CPP_Class (Priv_T) then
20424 Set_Is_Limited_Record (Full_T);
20426 -- GNAT allow its own definition of Limited_Controlled to disobey
20427 -- this rule in order in ease the implementation. This test is safe
20428 -- because Root_Controlled is defined in a child of System that
20429 -- normal programs are not supposed to use.
20431 elsif Is_RTE (Etype (Full_T), RE_Root_Controlled) then
20432 Set_Is_Limited_Composite (Full_T);
20435 ("completion of limited tagged type must be limited", Full_T);
20438 elsif Is_Generic_Type (Priv_T) then
20439 Error_Msg_N ("generic type cannot have a completion", Full_T);
20442 -- Check that ancestor interfaces of private and full views are
20443 -- consistent. We omit this check for synchronized types because
20444 -- they are performed on the corresponding record type when frozen.
20446 if Ada_Version >= Ada_2005
20447 and then Is_Tagged_Type (Priv_T)
20448 and then Is_Tagged_Type (Full_T)
20449 and then not Is_Concurrent_Type (Full_T)
20453 Priv_T_Ifaces : constant Elist_Id := New_Elmt_List;
20454 Full_T_Ifaces : constant Elist_Id := New_Elmt_List;
20457 Collect_Implemented_Interfaces (Priv_T, Priv_T_Ifaces);
20458 Collect_Implemented_Interfaces (Full_T, Full_T_Ifaces);
20460 -- Ada 2005 (AI-251): The partial view shall be a descendant of
20461 -- an interface type if and only if the full type is descendant
20462 -- of the interface type (AARM 7.3 (7.3/2)).
20464 Iface := Find_Hidden_Interface (Priv_T_Ifaces, Full_T_Ifaces);
20466 if Present (Iface) then
20468 ("interface in partial view& not implemented by full type "
20469 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20472 Iface := Find_Hidden_Interface (Full_T_Ifaces, Priv_T_Ifaces);
20474 if Present (Iface) then
20476 ("interface & not implemented by partial view "
20477 & "(RM-2005 7.3 (7.3/2))", Full_T, Iface);
20482 if Is_Tagged_Type (Priv_T)
20483 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20484 and then Is_Derived_Type (Full_T)
20486 Priv_Parent := Etype (Priv_T);
20488 -- The full view of a private extension may have been transformed
20489 -- into an unconstrained derived type declaration and a subtype
20490 -- declaration (see build_derived_record_type for details).
20492 if Nkind (N) = N_Subtype_Declaration then
20493 Full_Indic := Subtype_Indication (N);
20494 Full_Parent := Etype (Base_Type (Full_T));
20496 Full_Indic := Subtype_Indication (Type_Definition (N));
20497 Full_Parent := Etype (Full_T);
20500 -- Check that the parent type of the full type is a descendant of
20501 -- the ancestor subtype given in the private extension. If either
20502 -- entity has an Etype equal to Any_Type then we had some previous
20503 -- error situation [7.3(8)].
20505 if Priv_Parent = Any_Type or else Full_Parent = Any_Type then
20508 -- Ada 2005 (AI-251): Interfaces in the full type can be given in
20509 -- any order. Therefore we don't have to check that its parent must
20510 -- be a descendant of the parent of the private type declaration.
20512 elsif Is_Interface (Priv_Parent)
20513 and then Is_Interface (Full_Parent)
20517 -- Ada 2005 (AI-251): If the parent of the private type declaration
20518 -- is an interface there is no need to check that it is an ancestor
20519 -- of the associated full type declaration. The required tests for
20520 -- this case are performed by Build_Derived_Record_Type.
20522 elsif not Is_Interface (Base_Type (Priv_Parent))
20523 and then not Is_Ancestor (Base_Type (Priv_Parent), Full_Parent)
20526 ("parent of full type must descend from parent of private "
20527 & "extension", Full_Indic);
20529 -- First check a formal restriction, and then proceed with checking
20530 -- Ada rules. Since the formal restriction is not a serious error, we
20531 -- don't prevent further error detection for this check, hence the
20535 -- In formal mode, when completing a private extension the type
20536 -- named in the private part must be exactly the same as that
20537 -- named in the visible part.
20539 if Priv_Parent /= Full_Parent then
20540 Error_Msg_Name_1 := Chars (Priv_Parent);
20541 Check_SPARK_05_Restriction ("% expected", Full_Indic);
20544 -- Check the rules of 7.3(10): if the private extension inherits
20545 -- known discriminants, then the full type must also inherit those
20546 -- discriminants from the same (ancestor) type, and the parent
20547 -- subtype of the full type must be constrained if and only if
20548 -- the ancestor subtype of the private extension is constrained.
20550 if No (Discriminant_Specifications (Parent (Priv_T)))
20551 and then not Has_Unknown_Discriminants (Priv_T)
20552 and then Has_Discriminants (Base_Type (Priv_Parent))
20555 Priv_Indic : constant Node_Id :=
20556 Subtype_Indication (Parent (Priv_T));
20558 Priv_Constr : constant Boolean :=
20559 Is_Constrained (Priv_Parent)
20561 Nkind (Priv_Indic) = N_Subtype_Indication
20563 Is_Constrained (Entity (Priv_Indic));
20565 Full_Constr : constant Boolean :=
20566 Is_Constrained (Full_Parent)
20568 Nkind (Full_Indic) = N_Subtype_Indication
20570 Is_Constrained (Entity (Full_Indic));
20572 Priv_Discr : Entity_Id;
20573 Full_Discr : Entity_Id;
20576 Priv_Discr := First_Discriminant (Priv_Parent);
20577 Full_Discr := First_Discriminant (Full_Parent);
20578 while Present (Priv_Discr) and then Present (Full_Discr) loop
20579 if Original_Record_Component (Priv_Discr) =
20580 Original_Record_Component (Full_Discr)
20582 Corresponding_Discriminant (Priv_Discr) =
20583 Corresponding_Discriminant (Full_Discr)
20590 Next_Discriminant (Priv_Discr);
20591 Next_Discriminant (Full_Discr);
20594 if Present (Priv_Discr) or else Present (Full_Discr) then
20596 ("full view must inherit discriminants of the parent "
20597 & "type used in the private extension", Full_Indic);
20599 elsif Priv_Constr and then not Full_Constr then
20601 ("parent subtype of full type must be constrained",
20604 elsif Full_Constr and then not Priv_Constr then
20606 ("parent subtype of full type must be unconstrained",
20611 -- Check the rules of 7.3(12): if a partial view has neither
20612 -- known or unknown discriminants, then the full type
20613 -- declaration shall define a definite subtype.
20615 elsif not Has_Unknown_Discriminants (Priv_T)
20616 and then not Has_Discriminants (Priv_T)
20617 and then not Is_Constrained (Full_T)
20620 ("full view must define a constrained type if partial view "
20621 & "has no discriminants", Full_T);
20624 -- ??????? Do we implement the following properly ?????
20625 -- If the ancestor subtype of a private extension has constrained
20626 -- discriminants, then the parent subtype of the full view shall
20627 -- impose a statically matching constraint on those discriminants
20632 -- For untagged types, verify that a type without discriminants is
20633 -- not completed with an unconstrained type. A separate error message
20634 -- is produced if the full type has defaulted discriminants.
20636 if Is_Definite_Subtype (Priv_T)
20637 and then not Is_Definite_Subtype (Full_T)
20639 Error_Msg_Sloc := Sloc (Parent (Priv_T));
20641 ("full view of& not compatible with declaration#",
20644 if not Is_Tagged_Type (Full_T) then
20646 ("\one is constrained, the other unconstrained", Full_T);
20651 -- AI-419: verify that the use of "limited" is consistent
20654 Orig_Decl : constant Node_Id := Original_Node (N);
20657 if Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20658 and then Nkind (Orig_Decl) = N_Full_Type_Declaration
20660 (Type_Definition (Orig_Decl)) = N_Derived_Type_Definition
20662 if not Limited_Present (Parent (Priv_T))
20663 and then not Synchronized_Present (Parent (Priv_T))
20664 and then Limited_Present (Type_Definition (Orig_Decl))
20667 ("full view of non-limited extension cannot be limited", N);
20669 -- Conversely, if the partial view carries the limited keyword,
20670 -- the full view must as well, even if it may be redundant.
20672 elsif Limited_Present (Parent (Priv_T))
20673 and then not Limited_Present (Type_Definition (Orig_Decl))
20676 ("full view of limited extension must be explicitly limited",
20682 -- Ada 2005 (AI-443): A synchronized private extension must be
20683 -- completed by a task or protected type.
20685 if Ada_Version >= Ada_2005
20686 and then Nkind (Parent (Priv_T)) = N_Private_Extension_Declaration
20687 and then Synchronized_Present (Parent (Priv_T))
20688 and then not Is_Concurrent_Type (Full_T)
20690 Error_Msg_N ("full view of synchronized extension must " &
20691 "be synchronized type", N);
20694 -- Ada 2005 AI-363: if the full view has discriminants with
20695 -- defaults, it is illegal to declare constrained access subtypes
20696 -- whose designated type is the current type. This allows objects
20697 -- of the type that are declared in the heap to be unconstrained.
20699 if not Has_Unknown_Discriminants (Priv_T)
20700 and then not Has_Discriminants (Priv_T)
20701 and then Has_Discriminants (Full_T)
20703 Present (Discriminant_Default_Value (First_Discriminant (Full_T)))
20705 Set_Has_Constrained_Partial_View (Full_T);
20706 Set_Has_Constrained_Partial_View (Priv_T);
20709 -- Create a full declaration for all its subtypes recorded in
20710 -- Private_Dependents and swap them similarly to the base type. These
20711 -- are subtypes that have been define before the full declaration of
20712 -- the private type. We also swap the entry in Private_Dependents list
20713 -- so we can properly restore the private view on exit from the scope.
20716 Priv_Elmt : Elmt_Id;
20717 Priv_Scop : Entity_Id;
20722 Priv_Elmt := First_Elmt (Private_Dependents (Priv_T));
20723 while Present (Priv_Elmt) loop
20724 Priv := Node (Priv_Elmt);
20725 Priv_Scop := Scope (Priv);
20727 if Ekind_In (Priv, E_Private_Subtype,
20728 E_Limited_Private_Subtype,
20729 E_Record_Subtype_With_Private)
20731 Full := Make_Defining_Identifier (Sloc (Priv), Chars (Priv));
20732 Set_Is_Itype (Full);
20733 Set_Parent (Full, Parent (Priv));
20734 Set_Associated_Node_For_Itype (Full, N);
20736 -- Now we need to complete the private subtype, but since the
20737 -- base type has already been swapped, we must also swap the
20738 -- subtypes (and thus, reverse the arguments in the call to
20739 -- Complete_Private_Subtype). Also note that we may need to
20740 -- re-establish the scope of the private subtype.
20742 Copy_And_Swap (Priv, Full);
20744 if not In_Open_Scopes (Priv_Scop) then
20745 Push_Scope (Priv_Scop);
20748 -- Reset Priv_Scop to Empty to indicate no scope was pushed
20750 Priv_Scop := Empty;
20753 Complete_Private_Subtype (Full, Priv, Full_T, N);
20755 if Present (Priv_Scop) then
20759 Replace_Elmt (Priv_Elmt, Full);
20762 Next_Elmt (Priv_Elmt);
20766 -- If the private view was tagged, copy the new primitive operations
20767 -- from the private view to the full view.
20769 if Is_Tagged_Type (Full_T) then
20771 Disp_Typ : Entity_Id;
20772 Full_List : Elist_Id;
20774 Prim_Elmt : Elmt_Id;
20775 Priv_List : Elist_Id;
20779 L : Elist_Id) return Boolean;
20780 -- Determine whether list L contains element E
20788 L : Elist_Id) return Boolean
20790 List_Elmt : Elmt_Id;
20793 List_Elmt := First_Elmt (L);
20794 while Present (List_Elmt) loop
20795 if Node (List_Elmt) = E then
20799 Next_Elmt (List_Elmt);
20805 -- Start of processing
20808 if Is_Tagged_Type (Priv_T) then
20809 Priv_List := Primitive_Operations (Priv_T);
20810 Prim_Elmt := First_Elmt (Priv_List);
20812 -- In the case of a concurrent type completing a private tagged
20813 -- type, primitives may have been declared in between the two
20814 -- views. These subprograms need to be wrapped the same way
20815 -- entries and protected procedures are handled because they
20816 -- cannot be directly shared by the two views.
20818 if Is_Concurrent_Type (Full_T) then
20820 Conc_Typ : constant Entity_Id :=
20821 Corresponding_Record_Type (Full_T);
20822 Curr_Nod : Node_Id := Parent (Conc_Typ);
20823 Wrap_Spec : Node_Id;
20826 while Present (Prim_Elmt) loop
20827 Prim := Node (Prim_Elmt);
20829 if Comes_From_Source (Prim)
20830 and then not Is_Abstract_Subprogram (Prim)
20833 Make_Subprogram_Declaration (Sloc (Prim),
20837 Obj_Typ => Conc_Typ,
20839 Parameter_Specifications
20842 Insert_After (Curr_Nod, Wrap_Spec);
20843 Curr_Nod := Wrap_Spec;
20845 Analyze (Wrap_Spec);
20847 -- Remove the wrapper from visibility to avoid
20848 -- spurious conflict with the wrapped entity.
20850 Set_Is_Immediately_Visible
20851 (Defining_Entity (Specification (Wrap_Spec)),
20855 Next_Elmt (Prim_Elmt);
20861 -- For non-concurrent types, transfer explicit primitives, but
20862 -- omit those inherited from the parent of the private view
20863 -- since they will be re-inherited later on.
20866 Full_List := Primitive_Operations (Full_T);
20867 while Present (Prim_Elmt) loop
20868 Prim := Node (Prim_Elmt);
20870 if Comes_From_Source (Prim)
20871 and then not Contains (Prim, Full_List)
20873 Append_Elmt (Prim, Full_List);
20876 Next_Elmt (Prim_Elmt);
20880 -- Untagged private view
20883 Full_List := Primitive_Operations (Full_T);
20885 -- In this case the partial view is untagged, so here we locate
20886 -- all of the earlier primitives that need to be treated as
20887 -- dispatching (those that appear between the two views). Note
20888 -- that these additional operations must all be new operations
20889 -- (any earlier operations that override inherited operations
20890 -- of the full view will already have been inserted in the
20891 -- primitives list, marked by Check_Operation_From_Private_View
20892 -- as dispatching. Note that implicit "/=" operators are
20893 -- excluded from being added to the primitives list since they
20894 -- shouldn't be treated as dispatching (tagged "/=" is handled
20897 Prim := Next_Entity (Full_T);
20898 while Present (Prim) and then Prim /= Priv_T loop
20899 if Ekind_In (Prim, E_Procedure, E_Function) then
20900 Disp_Typ := Find_Dispatching_Type (Prim);
20902 if Disp_Typ = Full_T
20903 and then (Chars (Prim) /= Name_Op_Ne
20904 or else Comes_From_Source (Prim))
20906 Check_Controlling_Formals (Full_T, Prim);
20908 if Is_Suitable_Primitive (Prim)
20909 and then not Is_Dispatching_Operation (Prim)
20911 Append_Elmt (Prim, Full_List);
20912 Set_Is_Dispatching_Operation (Prim);
20913 Set_DT_Position_Value (Prim, No_Uint);
20916 elsif Is_Dispatching_Operation (Prim)
20917 and then Disp_Typ /= Full_T
20919 -- Verify that it is not otherwise controlled by a
20920 -- formal or a return value of type T.
20922 Check_Controlling_Formals (Disp_Typ, Prim);
20926 Next_Entity (Prim);
20930 -- For the tagged case, the two views can share the same primitive
20931 -- operations list and the same class-wide type. Update attributes
20932 -- of the class-wide type which depend on the full declaration.
20934 if Is_Tagged_Type (Priv_T) then
20935 Set_Direct_Primitive_Operations (Priv_T, Full_List);
20936 Set_Class_Wide_Type
20937 (Base_Type (Full_T), Class_Wide_Type (Priv_T));
20939 Propagate_Concurrent_Flags (Class_Wide_Type (Priv_T), Full_T);
20944 -- Ada 2005 AI 161: Check preelaborable initialization consistency
20946 if Known_To_Have_Preelab_Init (Priv_T) then
20948 -- Case where there is a pragma Preelaborable_Initialization. We
20949 -- always allow this in predefined units, which is cheating a bit,
20950 -- but it means we don't have to struggle to meet the requirements in
20951 -- the RM for having Preelaborable Initialization. Otherwise we
20952 -- require that the type meets the RM rules. But we can't check that
20953 -- yet, because of the rule about overriding Initialize, so we simply
20954 -- set a flag that will be checked at freeze time.
20956 if not In_Predefined_Unit (Full_T) then
20957 Set_Must_Have_Preelab_Init (Full_T);
20961 -- If pragma CPP_Class was applied to the private type declaration,
20962 -- propagate it now to the full type declaration.
20964 if Is_CPP_Class (Priv_T) then
20965 Set_Is_CPP_Class (Full_T);
20966 Set_Convention (Full_T, Convention_CPP);
20968 -- Check that components of imported CPP types do not have default
20971 Check_CPP_Type_Has_No_Defaults (Full_T);
20974 -- If the private view has user specified stream attributes, then so has
20977 -- Why the test, how could these flags be already set in Full_T ???
20979 if Has_Specified_Stream_Read (Priv_T) then
20980 Set_Has_Specified_Stream_Read (Full_T);
20983 if Has_Specified_Stream_Write (Priv_T) then
20984 Set_Has_Specified_Stream_Write (Full_T);
20987 if Has_Specified_Stream_Input (Priv_T) then
20988 Set_Has_Specified_Stream_Input (Full_T);
20991 if Has_Specified_Stream_Output (Priv_T) then
20992 Set_Has_Specified_Stream_Output (Full_T);
20995 -- Propagate Default_Initial_Condition-related attributes from the
20996 -- partial view to the full view and its base type.
20998 Propagate_DIC_Attributes (Full_T, From_Typ => Priv_T);
20999 Propagate_DIC_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
21001 -- Propagate invariant-related attributes from the partial view to the
21002 -- full view and its base type.
21004 Propagate_Invariant_Attributes (Full_T, From_Typ => Priv_T);
21005 Propagate_Invariant_Attributes (Base_Type (Full_T), From_Typ => Priv_T);
21007 -- AI12-0041: Detect an attempt to inherit a class-wide type invariant
21008 -- in the full view without advertising the inheritance in the partial
21009 -- view. This can only occur when the partial view has no parent type
21010 -- and the full view has an interface as a parent. Any other scenarios
21011 -- are illegal because implemented interfaces must match between the
21014 if Is_Tagged_Type (Priv_T) and then Is_Tagged_Type (Full_T) then
21016 Full_Par : constant Entity_Id := Etype (Full_T);
21017 Priv_Par : constant Entity_Id := Etype (Priv_T);
21020 if not Is_Interface (Priv_Par)
21021 and then Is_Interface (Full_Par)
21022 and then Has_Inheritable_Invariants (Full_Par)
21025 ("hidden inheritance of class-wide type invariants not "
21031 -- Propagate predicates to full type, and predicate function if already
21032 -- defined. It is not clear that this can actually happen? the partial
21033 -- view cannot be frozen yet, and the predicate function has not been
21034 -- built. Still it is a cheap check and seems safer to make it.
21036 if Has_Predicates (Priv_T) then
21037 Set_Has_Predicates (Full_T);
21039 if Present (Predicate_Function (Priv_T)) then
21040 Set_Predicate_Function (Full_T, Predicate_Function (Priv_T));
21045 Restore_Ghost_Region (Saved_GM, Saved_IGR);
21046 end Process_Full_View;
21048 -----------------------------------
21049 -- Process_Incomplete_Dependents --
21050 -----------------------------------
21052 procedure Process_Incomplete_Dependents
21054 Full_T : Entity_Id;
21057 Inc_Elmt : Elmt_Id;
21058 Priv_Dep : Entity_Id;
21059 New_Subt : Entity_Id;
21061 Disc_Constraint : Elist_Id;
21064 if No (Private_Dependents (Inc_T)) then
21068 -- Itypes that may be generated by the completion of an incomplete
21069 -- subtype are not used by the back-end and not attached to the tree.
21070 -- They are created only for constraint-checking purposes.
21072 Inc_Elmt := First_Elmt (Private_Dependents (Inc_T));
21073 while Present (Inc_Elmt) loop
21074 Priv_Dep := Node (Inc_Elmt);
21076 if Ekind (Priv_Dep) = E_Subprogram_Type then
21078 -- An Access_To_Subprogram type may have a return type or a
21079 -- parameter type that is incomplete. Replace with the full view.
21081 if Etype (Priv_Dep) = Inc_T then
21082 Set_Etype (Priv_Dep, Full_T);
21086 Formal : Entity_Id;
21089 Formal := First_Formal (Priv_Dep);
21090 while Present (Formal) loop
21091 if Etype (Formal) = Inc_T then
21092 Set_Etype (Formal, Full_T);
21095 Next_Formal (Formal);
21099 elsif Is_Overloadable (Priv_Dep) then
21101 -- If a subprogram in the incomplete dependents list is primitive
21102 -- for a tagged full type then mark it as a dispatching operation,
21103 -- check whether it overrides an inherited subprogram, and check
21104 -- restrictions on its controlling formals. Note that a protected
21105 -- operation is never dispatching: only its wrapper operation
21106 -- (which has convention Ada) is.
21108 if Is_Tagged_Type (Full_T)
21109 and then Is_Primitive (Priv_Dep)
21110 and then Convention (Priv_Dep) /= Convention_Protected
21112 Check_Operation_From_Incomplete_Type (Priv_Dep, Inc_T);
21113 Set_Is_Dispatching_Operation (Priv_Dep);
21114 Check_Controlling_Formals (Full_T, Priv_Dep);
21117 elsif Ekind (Priv_Dep) = E_Subprogram_Body then
21119 -- Can happen during processing of a body before the completion
21120 -- of a TA type. Ignore, because spec is also on dependent list.
21124 -- Ada 2005 (AI-412): Transform a regular incomplete subtype into a
21125 -- corresponding subtype of the full view.
21127 elsif Ekind (Priv_Dep) = E_Incomplete_Subtype
21128 and then Comes_From_Source (Priv_Dep)
21130 Set_Subtype_Indication
21131 (Parent (Priv_Dep), New_Occurrence_Of (Full_T, Sloc (Priv_Dep)));
21132 Set_Etype (Priv_Dep, Full_T);
21133 Set_Ekind (Priv_Dep, Subtype_Kind (Ekind (Full_T)));
21134 Set_Analyzed (Parent (Priv_Dep), False);
21136 -- Reanalyze the declaration, suppressing the call to Enter_Name
21137 -- to avoid duplicate names.
21139 Analyze_Subtype_Declaration
21140 (N => Parent (Priv_Dep),
21143 -- Dependent is a subtype
21146 -- We build a new subtype indication using the full view of the
21147 -- incomplete parent. The discriminant constraints have been
21148 -- elaborated already at the point of the subtype declaration.
21150 New_Subt := Create_Itype (E_Void, N);
21152 if Has_Discriminants (Full_T) then
21153 Disc_Constraint := Discriminant_Constraint (Priv_Dep);
21155 Disc_Constraint := No_Elist;
21158 Build_Discriminated_Subtype (Full_T, New_Subt, Disc_Constraint, N);
21159 Set_Full_View (Priv_Dep, New_Subt);
21162 Next_Elmt (Inc_Elmt);
21164 end Process_Incomplete_Dependents;
21166 --------------------------------
21167 -- Process_Range_Expr_In_Decl --
21168 --------------------------------
21170 procedure Process_Range_Expr_In_Decl
21173 Subtyp : Entity_Id := Empty;
21174 Check_List : List_Id := Empty_List;
21175 R_Check_Off : Boolean := False;
21176 In_Iter_Schm : Boolean := False)
21179 R_Checks : Check_Result;
21180 Insert_Node : Node_Id;
21181 Def_Id : Entity_Id;
21184 Analyze_And_Resolve (R, Base_Type (T));
21186 if Nkind (R) = N_Range then
21188 -- In SPARK, all ranges should be static, with the exception of the
21189 -- discrete type definition of a loop parameter specification.
21191 if not In_Iter_Schm
21192 and then not Is_OK_Static_Range (R)
21194 Check_SPARK_05_Restriction ("range should be static", R);
21197 Lo := Low_Bound (R);
21198 Hi := High_Bound (R);
21200 -- Validity checks on the range of a quantified expression are
21201 -- delayed until the construct is transformed into a loop.
21203 if Nkind (Parent (R)) = N_Loop_Parameter_Specification
21204 and then Nkind (Parent (Parent (R))) = N_Quantified_Expression
21208 -- We need to ensure validity of the bounds here, because if we
21209 -- go ahead and do the expansion, then the expanded code will get
21210 -- analyzed with range checks suppressed and we miss the check.
21212 -- WARNING: The capture of the range bounds with xxx_FIRST/_LAST and
21213 -- the temporaries generated by routine Remove_Side_Effects by means
21214 -- of validity checks must use the same names. When a range appears
21215 -- in the parent of a generic, the range is processed with checks
21216 -- disabled as part of the generic context and with checks enabled
21217 -- for code generation purposes. This leads to link issues as the
21218 -- generic contains references to xxx_FIRST/_LAST, but the inlined
21219 -- template sees the temporaries generated by Remove_Side_Effects.
21222 Validity_Check_Range (R, Subtyp);
21225 -- If there were errors in the declaration, try and patch up some
21226 -- common mistakes in the bounds. The cases handled are literals
21227 -- which are Integer where the expected type is Real and vice versa.
21228 -- These corrections allow the compilation process to proceed further
21229 -- along since some basic assumptions of the format of the bounds
21232 if Etype (R) = Any_Type then
21233 if Nkind (Lo) = N_Integer_Literal and then Is_Real_Type (T) then
21235 Make_Real_Literal (Sloc (Lo), UR_From_Uint (Intval (Lo))));
21237 elsif Nkind (Hi) = N_Integer_Literal and then Is_Real_Type (T) then
21239 Make_Real_Literal (Sloc (Hi), UR_From_Uint (Intval (Hi))));
21241 elsif Nkind (Lo) = N_Real_Literal and then Is_Integer_Type (T) then
21243 Make_Integer_Literal (Sloc (Lo), UR_To_Uint (Realval (Lo))));
21245 elsif Nkind (Hi) = N_Real_Literal and then Is_Integer_Type (T) then
21247 Make_Integer_Literal (Sloc (Hi), UR_To_Uint (Realval (Hi))));
21254 -- If the bounds of the range have been mistakenly given as string
21255 -- literals (perhaps in place of character literals), then an error
21256 -- has already been reported, but we rewrite the string literal as a
21257 -- bound of the range's type to avoid blowups in later processing
21258 -- that looks at static values.
21260 if Nkind (Lo) = N_String_Literal then
21262 Make_Attribute_Reference (Sloc (Lo),
21263 Prefix => New_Occurrence_Of (T, Sloc (Lo)),
21264 Attribute_Name => Name_First));
21265 Analyze_And_Resolve (Lo);
21268 if Nkind (Hi) = N_String_Literal then
21270 Make_Attribute_Reference (Sloc (Hi),
21271 Prefix => New_Occurrence_Of (T, Sloc (Hi)),
21272 Attribute_Name => Name_First));
21273 Analyze_And_Resolve (Hi);
21276 -- If bounds aren't scalar at this point then exit, avoiding
21277 -- problems with further processing of the range in this procedure.
21279 if not Is_Scalar_Type (Etype (Lo)) then
21283 -- Resolve (actually Sem_Eval) has checked that the bounds are in
21284 -- then range of the base type. Here we check whether the bounds
21285 -- are in the range of the subtype itself. Note that if the bounds
21286 -- represent the null range the Constraint_Error exception should
21289 -- ??? The following code should be cleaned up as follows
21291 -- 1. The Is_Null_Range (Lo, Hi) test should disappear since it
21292 -- is done in the call to Range_Check (R, T); below
21294 -- 2. The use of R_Check_Off should be investigated and possibly
21295 -- removed, this would clean up things a bit.
21297 if Is_Null_Range (Lo, Hi) then
21301 -- Capture values of bounds and generate temporaries for them
21302 -- if needed, before applying checks, since checks may cause
21303 -- duplication of the expression without forcing evaluation.
21305 -- The forced evaluation removes side effects from expressions,
21306 -- which should occur also in GNATprove mode. Otherwise, we end up
21307 -- with unexpected insertions of actions at places where this is
21308 -- not supposed to occur, e.g. on default parameters of a call.
21310 if Expander_Active or GNATprove_Mode then
21312 -- Call Force_Evaluation to create declarations as needed to
21313 -- deal with side effects, and also create typ_FIRST/LAST
21314 -- entities for bounds if we have a subtype name.
21316 -- Note: we do this transformation even if expansion is not
21317 -- active if we are in GNATprove_Mode since the transformation
21318 -- is in general required to ensure that the resulting tree has
21319 -- proper Ada semantics.
21322 (Lo, Related_Id => Subtyp, Is_Low_Bound => True);
21324 (Hi, Related_Id => Subtyp, Is_High_Bound => True);
21327 -- We use a flag here instead of suppressing checks on the type
21328 -- because the type we check against isn't necessarily the place
21329 -- where we put the check.
21331 if not R_Check_Off then
21332 R_Checks := Get_Range_Checks (R, T);
21334 -- Look up tree to find an appropriate insertion point. We
21335 -- can't just use insert_actions because later processing
21336 -- depends on the insertion node. Prior to Ada 2012 the
21337 -- insertion point could only be a declaration or a loop, but
21338 -- quantified expressions can appear within any context in an
21339 -- expression, and the insertion point can be any statement,
21340 -- pragma, or declaration.
21342 Insert_Node := Parent (R);
21343 while Present (Insert_Node) loop
21345 Nkind (Insert_Node) in N_Declaration
21348 (Insert_Node, N_Component_Declaration,
21349 N_Loop_Parameter_Specification,
21350 N_Function_Specification,
21351 N_Procedure_Specification);
21353 exit when Nkind (Insert_Node) in N_Later_Decl_Item
21354 or else Nkind (Insert_Node) in
21355 N_Statement_Other_Than_Procedure_Call
21356 or else Nkind_In (Insert_Node, N_Procedure_Call_Statement,
21359 Insert_Node := Parent (Insert_Node);
21362 -- Why would Type_Decl not be present??? Without this test,
21363 -- short regression tests fail.
21365 if Present (Insert_Node) then
21367 -- Case of loop statement. Verify that the range is part
21368 -- of the subtype indication of the iteration scheme.
21370 if Nkind (Insert_Node) = N_Loop_Statement then
21375 Indic := Parent (R);
21376 while Present (Indic)
21377 and then Nkind (Indic) /= N_Subtype_Indication
21379 Indic := Parent (Indic);
21382 if Present (Indic) then
21383 Def_Id := Etype (Subtype_Mark (Indic));
21385 Insert_Range_Checks
21389 Sloc (Insert_Node),
21391 Do_Before => True);
21395 -- Insertion before a declaration. If the declaration
21396 -- includes discriminants, the list of applicable checks
21397 -- is given by the caller.
21399 elsif Nkind (Insert_Node) in N_Declaration then
21400 Def_Id := Defining_Identifier (Insert_Node);
21402 if (Ekind (Def_Id) = E_Record_Type
21403 and then Depends_On_Discriminant (R))
21405 (Ekind (Def_Id) = E_Protected_Type
21406 and then Has_Discriminants (Def_Id))
21408 Append_Range_Checks
21410 Check_List, Def_Id, Sloc (Insert_Node), R);
21413 Insert_Range_Checks
21415 Insert_Node, Def_Id, Sloc (Insert_Node), R);
21419 -- Insertion before a statement. Range appears in the
21420 -- context of a quantified expression. Insertion will
21421 -- take place when expression is expanded.
21430 -- Case of other than an explicit N_Range node
21432 -- The forced evaluation removes side effects from expressions, which
21433 -- should occur also in GNATprove mode. Otherwise, we end up with
21434 -- unexpected insertions of actions at places where this is not
21435 -- supposed to occur, e.g. on default parameters of a call.
21437 elsif Expander_Active or GNATprove_Mode then
21438 Get_Index_Bounds (R, Lo, Hi);
21439 Force_Evaluation (Lo);
21440 Force_Evaluation (Hi);
21442 end Process_Range_Expr_In_Decl;
21444 --------------------------------------
21445 -- Process_Real_Range_Specification --
21446 --------------------------------------
21448 procedure Process_Real_Range_Specification (Def : Node_Id) is
21449 Spec : constant Node_Id := Real_Range_Specification (Def);
21452 Err : Boolean := False;
21454 procedure Analyze_Bound (N : Node_Id);
21455 -- Analyze and check one bound
21457 -------------------
21458 -- Analyze_Bound --
21459 -------------------
21461 procedure Analyze_Bound (N : Node_Id) is
21463 Analyze_And_Resolve (N, Any_Real);
21465 if not Is_OK_Static_Expression (N) then
21466 Flag_Non_Static_Expr
21467 ("bound in real type definition is not static!", N);
21472 -- Start of processing for Process_Real_Range_Specification
21475 if Present (Spec) then
21476 Lo := Low_Bound (Spec);
21477 Hi := High_Bound (Spec);
21478 Analyze_Bound (Lo);
21479 Analyze_Bound (Hi);
21481 -- If error, clear away junk range specification
21484 Set_Real_Range_Specification (Def, Empty);
21487 end Process_Real_Range_Specification;
21489 ---------------------
21490 -- Process_Subtype --
21491 ---------------------
21493 function Process_Subtype
21495 Related_Nod : Node_Id;
21496 Related_Id : Entity_Id := Empty;
21497 Suffix : Character := ' ') return Entity_Id
21500 Def_Id : Entity_Id;
21501 Error_Node : Node_Id;
21502 Full_View_Id : Entity_Id;
21503 Subtype_Mark_Id : Entity_Id;
21505 May_Have_Null_Exclusion : Boolean;
21507 procedure Check_Incomplete (T : Node_Id);
21508 -- Called to verify that an incomplete type is not used prematurely
21510 ----------------------
21511 -- Check_Incomplete --
21512 ----------------------
21514 procedure Check_Incomplete (T : Node_Id) is
21516 -- Ada 2005 (AI-412): Incomplete subtypes are legal
21518 if Ekind (Root_Type (Entity (T))) = E_Incomplete_Type
21520 not (Ada_Version >= Ada_2005
21522 (Nkind (Parent (T)) = N_Subtype_Declaration
21523 or else (Nkind (Parent (T)) = N_Subtype_Indication
21524 and then Nkind (Parent (Parent (T))) =
21525 N_Subtype_Declaration)))
21527 Error_Msg_N ("invalid use of type before its full declaration", T);
21529 end Check_Incomplete;
21531 -- Start of processing for Process_Subtype
21534 -- Case of no constraints present
21536 if Nkind (S) /= N_Subtype_Indication then
21539 -- No way to proceed if the subtype indication is malformed. This
21540 -- will happen for example when the subtype indication in an object
21541 -- declaration is missing altogether and the expression is analyzed
21542 -- as if it were that indication.
21544 if not Is_Entity_Name (S) then
21548 Check_Incomplete (S);
21551 -- Ada 2005 (AI-231): Static check
21553 if Ada_Version >= Ada_2005
21554 and then Present (P)
21555 and then Null_Exclusion_Present (P)
21556 and then Nkind (P) /= N_Access_To_Object_Definition
21557 and then not Is_Access_Type (Entity (S))
21559 Error_Msg_N ("`NOT NULL` only allowed for an access type", S);
21562 -- The following is ugly, can't we have a range or even a flag???
21564 May_Have_Null_Exclusion :=
21565 Nkind_In (P, N_Access_Definition,
21566 N_Access_Function_Definition,
21567 N_Access_Procedure_Definition,
21568 N_Access_To_Object_Definition,
21570 N_Component_Definition)
21572 Nkind_In (P, N_Derived_Type_Definition,
21573 N_Discriminant_Specification,
21574 N_Formal_Object_Declaration,
21575 N_Object_Declaration,
21576 N_Object_Renaming_Declaration,
21577 N_Parameter_Specification,
21578 N_Subtype_Declaration);
21580 -- Create an Itype that is a duplicate of Entity (S) but with the
21581 -- null-exclusion attribute.
21583 if May_Have_Null_Exclusion
21584 and then Is_Access_Type (Entity (S))
21585 and then Null_Exclusion_Present (P)
21587 -- No need to check the case of an access to object definition.
21588 -- It is correct to define double not-null pointers.
21591 -- type Not_Null_Int_Ptr is not null access Integer;
21592 -- type Acc is not null access Not_Null_Int_Ptr;
21594 and then Nkind (P) /= N_Access_To_Object_Definition
21596 if Can_Never_Be_Null (Entity (S)) then
21597 case Nkind (Related_Nod) is
21598 when N_Full_Type_Declaration =>
21599 if Nkind (Type_Definition (Related_Nod))
21600 in N_Array_Type_Definition
21604 (Component_Definition
21605 (Type_Definition (Related_Nod)));
21608 Subtype_Indication (Type_Definition (Related_Nod));
21611 when N_Subtype_Declaration =>
21612 Error_Node := Subtype_Indication (Related_Nod);
21614 when N_Object_Declaration =>
21615 Error_Node := Object_Definition (Related_Nod);
21617 when N_Component_Declaration =>
21619 Subtype_Indication (Component_Definition (Related_Nod));
21621 when N_Allocator =>
21622 Error_Node := Expression (Related_Nod);
21625 pragma Assert (False);
21626 Error_Node := Related_Nod;
21630 ("`NOT NULL` not allowed (& already excludes null)",
21636 Create_Null_Excluding_Itype
21638 Related_Nod => P));
21639 Set_Entity (S, Etype (S));
21644 -- Case of constraint present, so that we have an N_Subtype_Indication
21645 -- node (this node is created only if constraints are present).
21648 Find_Type (Subtype_Mark (S));
21650 if Nkind (Parent (S)) /= N_Access_To_Object_Definition
21652 (Nkind (Parent (S)) = N_Subtype_Declaration
21653 and then Is_Itype (Defining_Identifier (Parent (S))))
21655 Check_Incomplete (Subtype_Mark (S));
21659 Subtype_Mark_Id := Entity (Subtype_Mark (S));
21661 -- Explicit subtype declaration case
21663 if Nkind (P) = N_Subtype_Declaration then
21664 Def_Id := Defining_Identifier (P);
21666 -- Explicit derived type definition case
21668 elsif Nkind (P) = N_Derived_Type_Definition then
21669 Def_Id := Defining_Identifier (Parent (P));
21671 -- Implicit case, the Def_Id must be created as an implicit type.
21672 -- The one exception arises in the case of concurrent types, array
21673 -- and access types, where other subsidiary implicit types may be
21674 -- created and must appear before the main implicit type. In these
21675 -- cases we leave Def_Id set to Empty as a signal that Create_Itype
21676 -- has not yet been called to create Def_Id.
21679 if Is_Array_Type (Subtype_Mark_Id)
21680 or else Is_Concurrent_Type (Subtype_Mark_Id)
21681 or else Is_Access_Type (Subtype_Mark_Id)
21685 -- For the other cases, we create a new unattached Itype,
21686 -- and set the indication to ensure it gets attached later.
21690 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21694 -- If the kind of constraint is invalid for this kind of type,
21695 -- then give an error, and then pretend no constraint was given.
21697 if not Is_Valid_Constraint_Kind
21698 (Ekind (Subtype_Mark_Id), Nkind (Constraint (S)))
21701 ("incorrect constraint for this kind of type", Constraint (S));
21703 Rewrite (S, New_Copy_Tree (Subtype_Mark (S)));
21705 -- Set Ekind of orphan itype, to prevent cascaded errors
21707 if Present (Def_Id) then
21708 Set_Ekind (Def_Id, Ekind (Any_Type));
21711 -- Make recursive call, having got rid of the bogus constraint
21713 return Process_Subtype (S, Related_Nod, Related_Id, Suffix);
21716 -- Remaining processing depends on type. Select on Base_Type kind to
21717 -- ensure getting to the concrete type kind in the case of a private
21718 -- subtype (needed when only doing semantic analysis).
21720 case Ekind (Base_Type (Subtype_Mark_Id)) is
21721 when Access_Kind =>
21723 -- If this is a constraint on a class-wide type, discard it.
21724 -- There is currently no way to express a partial discriminant
21725 -- constraint on a type with unknown discriminants. This is
21726 -- a pathology that the ACATS wisely decides not to test.
21728 if Is_Class_Wide_Type (Designated_Type (Subtype_Mark_Id)) then
21729 if Comes_From_Source (S) then
21731 ("constraint on class-wide type ignored??",
21735 if Nkind (P) = N_Subtype_Declaration then
21736 Set_Subtype_Indication (P,
21737 New_Occurrence_Of (Subtype_Mark_Id, Sloc (S)));
21740 return Subtype_Mark_Id;
21743 Constrain_Access (Def_Id, S, Related_Nod);
21746 and then Is_Itype (Designated_Type (Def_Id))
21747 and then Nkind (Related_Nod) = N_Subtype_Declaration
21748 and then not Is_Incomplete_Type (Designated_Type (Def_Id))
21750 Build_Itype_Reference
21751 (Designated_Type (Def_Id), Related_Nod);
21755 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21757 when Decimal_Fixed_Point_Kind =>
21758 Constrain_Decimal (Def_Id, S);
21760 when Enumeration_Kind =>
21761 Constrain_Enumeration (Def_Id, S);
21763 when Ordinary_Fixed_Point_Kind =>
21764 Constrain_Ordinary_Fixed (Def_Id, S);
21767 Constrain_Float (Def_Id, S);
21769 when Integer_Kind =>
21770 Constrain_Integer (Def_Id, S);
21772 when Class_Wide_Kind
21773 | E_Incomplete_Type
21777 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21779 if Ekind (Def_Id) = E_Incomplete_Type then
21780 Set_Private_Dependents (Def_Id, New_Elmt_List);
21783 when Private_Kind =>
21785 -- A private type with unknown discriminants may be completed
21786 -- by an unconstrained array type.
21788 if Has_Unknown_Discriminants (Subtype_Mark_Id)
21789 and then Present (Full_View (Subtype_Mark_Id))
21790 and then Is_Array_Type (Full_View (Subtype_Mark_Id))
21792 Constrain_Array (Def_Id, S, Related_Nod, Related_Id, Suffix);
21794 -- ... but more commonly is completed by a discriminated record
21798 Constrain_Discriminated_Type (Def_Id, S, Related_Nod);
21801 -- The base type may be private but Def_Id may be a full view
21804 if Is_Private_Type (Def_Id) then
21805 Set_Private_Dependents (Def_Id, New_Elmt_List);
21808 -- In case of an invalid constraint prevent further processing
21809 -- since the type constructed is missing expected fields.
21811 if Etype (Def_Id) = Any_Type then
21815 -- If the full view is that of a task with discriminants,
21816 -- we must constrain both the concurrent type and its
21817 -- corresponding record type. Otherwise we will just propagate
21818 -- the constraint to the full view, if available.
21820 if Present (Full_View (Subtype_Mark_Id))
21821 and then Has_Discriminants (Subtype_Mark_Id)
21822 and then Is_Concurrent_Type (Full_View (Subtype_Mark_Id))
21825 Create_Itype (E_Void, Related_Nod, Related_Id, Suffix);
21827 Set_Entity (Subtype_Mark (S), Full_View (Subtype_Mark_Id));
21828 Constrain_Concurrent (Full_View_Id, S,
21829 Related_Nod, Related_Id, Suffix);
21830 Set_Entity (Subtype_Mark (S), Subtype_Mark_Id);
21831 Set_Full_View (Def_Id, Full_View_Id);
21833 -- Introduce an explicit reference to the private subtype,
21834 -- to prevent scope anomalies in gigi if first use appears
21835 -- in a nested context, e.g. a later function body.
21836 -- Should this be generated in other contexts than a full
21837 -- type declaration?
21839 if Is_Itype (Def_Id)
21841 Nkind (Parent (P)) = N_Full_Type_Declaration
21843 Build_Itype_Reference (Def_Id, Parent (P));
21847 Prepare_Private_Subtype_Completion (Def_Id, Related_Nod);
21850 when Concurrent_Kind =>
21851 Constrain_Concurrent (Def_Id, S,
21852 Related_Nod, Related_Id, Suffix);
21855 Error_Msg_N ("invalid subtype mark in subtype indication", S);
21858 -- Size, Alignment, Representation aspects and Convention are always
21859 -- inherited from the base type.
21861 Set_Size_Info (Def_Id, (Subtype_Mark_Id));
21862 Set_Rep_Info (Def_Id, (Subtype_Mark_Id));
21863 Set_Convention (Def_Id, Convention (Subtype_Mark_Id));
21865 -- The anonymous subtype created for the subtype indication
21866 -- inherits the predicates of the parent.
21868 if Has_Predicates (Subtype_Mark_Id) then
21869 Inherit_Predicate_Flags (Def_Id, Subtype_Mark_Id);
21871 -- Indicate where the predicate function may be found
21873 if No (Predicate_Function (Def_Id)) and then Is_Itype (Def_Id) then
21874 Set_Predicated_Parent (Def_Id, Subtype_Mark_Id);
21880 end Process_Subtype;
21882 -----------------------------
21883 -- Record_Type_Declaration --
21884 -----------------------------
21886 procedure Record_Type_Declaration
21891 Def : constant Node_Id := Type_Definition (N);
21892 Is_Tagged : Boolean;
21893 Tag_Comp : Entity_Id;
21896 -- These flags must be initialized before calling Process_Discriminants
21897 -- because this routine makes use of them.
21899 Set_Ekind (T, E_Record_Type);
21901 Init_Size_Align (T);
21902 Set_Interfaces (T, No_Elist);
21903 Set_Stored_Constraint (T, No_Elist);
21904 Set_Default_SSO (T);
21905 Set_No_Reordering (T, No_Component_Reordering);
21909 if Ada_Version < Ada_2005 or else not Interface_Present (Def) then
21910 if Limited_Present (Def) then
21911 Check_SPARK_05_Restriction ("limited is not allowed", N);
21914 if Abstract_Present (Def) then
21915 Check_SPARK_05_Restriction ("abstract is not allowed", N);
21918 -- The flag Is_Tagged_Type might have already been set by
21919 -- Find_Type_Name if it detected an error for declaration T. This
21920 -- arises in the case of private tagged types where the full view
21921 -- omits the word tagged.
21924 Tagged_Present (Def)
21925 or else (Serious_Errors_Detected > 0 and then Is_Tagged_Type (T));
21927 Set_Is_Limited_Record (T, Limited_Present (Def));
21930 Set_Is_Tagged_Type (T, True);
21931 Set_No_Tagged_Streams_Pragma (T, No_Tagged_Streams);
21934 -- Type is abstract if full declaration carries keyword, or if
21935 -- previous partial view did.
21937 Set_Is_Abstract_Type (T, Is_Abstract_Type (T)
21938 or else Abstract_Present (Def));
21941 Check_SPARK_05_Restriction ("interface is not allowed", N);
21944 Analyze_Interface_Declaration (T, Def);
21946 if Present (Discriminant_Specifications (N)) then
21948 ("interface types cannot have discriminants",
21949 Defining_Identifier
21950 (First (Discriminant_Specifications (N))));
21954 -- First pass: if there are self-referential access components,
21955 -- create the required anonymous access type declarations, and if
21956 -- need be an incomplete type declaration for T itself.
21958 Check_Anonymous_Access_Components (N, T, Prev, Component_List (Def));
21960 if Ada_Version >= Ada_2005
21961 and then Present (Interface_List (Def))
21963 Check_Interfaces (N, Def);
21966 Ifaces_List : Elist_Id;
21969 -- Ada 2005 (AI-251): Collect the list of progenitors that are not
21970 -- already in the parents.
21974 Ifaces_List => Ifaces_List,
21975 Exclude_Parents => True);
21977 Set_Interfaces (T, Ifaces_List);
21981 -- Records constitute a scope for the component declarations within.
21982 -- The scope is created prior to the processing of these declarations.
21983 -- Discriminants are processed first, so that they are visible when
21984 -- processing the other components. The Ekind of the record type itself
21985 -- is set to E_Record_Type (subtypes appear as E_Record_Subtype).
21987 -- Enter record scope
21991 -- If an incomplete or private type declaration was already given for
21992 -- the type, then this scope already exists, and the discriminants have
21993 -- been declared within. We must verify that the full declaration
21994 -- matches the incomplete one.
21996 Check_Or_Process_Discriminants (N, T, Prev);
21998 Set_Is_Constrained (T, not Has_Discriminants (T));
21999 Set_Has_Delayed_Freeze (T, True);
22001 -- For tagged types add a manually analyzed component corresponding
22002 -- to the component _tag, the corresponding piece of tree will be
22003 -- expanded as part of the freezing actions if it is not a CPP_Class.
22007 -- Do not add the tag unless we are in expansion mode
22009 if Expander_Active then
22010 Tag_Comp := Make_Defining_Identifier (Sloc (Def), Name_uTag);
22011 Enter_Name (Tag_Comp);
22013 Set_Ekind (Tag_Comp, E_Component);
22014 Set_Is_Tag (Tag_Comp);
22015 Set_Is_Aliased (Tag_Comp);
22016 Set_Etype (Tag_Comp, RTE (RE_Tag));
22017 Set_DT_Entry_Count (Tag_Comp, No_Uint);
22018 Set_Original_Record_Component (Tag_Comp, Tag_Comp);
22019 Init_Component_Location (Tag_Comp);
22021 -- Ada 2005 (AI-251): Addition of the Tag corresponding to all the
22022 -- implemented interfaces.
22024 if Has_Interfaces (T) then
22025 Add_Interface_Tag_Components (N, T);
22029 Make_Class_Wide_Type (T);
22030 Set_Direct_Primitive_Operations (T, New_Elmt_List);
22033 -- We must suppress range checks when processing record components in
22034 -- the presence of discriminants, since we don't want spurious checks to
22035 -- be generated during their analysis, but Suppress_Range_Checks flags
22036 -- must be reset the after processing the record definition.
22038 -- Note: this is the only use of Kill_Range_Checks, and is a bit odd,
22039 -- couldn't we just use the normal range check suppression method here.
22040 -- That would seem cleaner ???
22042 if Has_Discriminants (T) and then not Range_Checks_Suppressed (T) then
22043 Set_Kill_Range_Checks (T, True);
22044 Record_Type_Definition (Def, Prev);
22045 Set_Kill_Range_Checks (T, False);
22047 Record_Type_Definition (Def, Prev);
22050 -- Exit from record scope
22054 -- Ada 2005 (AI-251 and AI-345): Derive the interface subprograms of all
22055 -- the implemented interfaces and associate them an aliased entity.
22058 and then not Is_Empty_List (Interface_List (Def))
22060 Derive_Progenitor_Subprograms (T, T);
22063 Check_Function_Writable_Actuals (N);
22064 end Record_Type_Declaration;
22066 ----------------------------
22067 -- Record_Type_Definition --
22068 ----------------------------
22070 procedure Record_Type_Definition (Def : Node_Id; Prev_T : Entity_Id) is
22071 Component : Entity_Id;
22072 Ctrl_Components : Boolean := False;
22073 Final_Storage_Only : Boolean;
22077 if Ekind (Prev_T) = E_Incomplete_Type then
22078 T := Full_View (Prev_T);
22083 -- In SPARK, tagged types and type extensions may only be declared in
22084 -- the specification of library unit packages.
22086 if Present (Def) and then Is_Tagged_Type (T) then
22092 if Nkind (Parent (Def)) = N_Full_Type_Declaration then
22093 Typ := Parent (Def);
22096 (Nkind (Parent (Def)) = N_Derived_Type_Definition);
22097 Typ := Parent (Parent (Def));
22100 Ctxt := Parent (Typ);
22102 if Nkind (Ctxt) = N_Package_Body
22103 and then Nkind (Parent (Ctxt)) = N_Compilation_Unit
22105 Check_SPARK_05_Restriction
22106 ("type should be defined in package specification", Typ);
22108 elsif Nkind (Ctxt) /= N_Package_Specification
22109 or else Nkind (Parent (Parent (Ctxt))) /= N_Compilation_Unit
22111 Check_SPARK_05_Restriction
22112 ("type should be defined in library unit package", Typ);
22117 Final_Storage_Only := not Is_Controlled (T);
22119 -- Ada 2005: Check whether an explicit Limited is present in a derived
22120 -- type declaration.
22122 if Nkind (Parent (Def)) = N_Derived_Type_Definition
22123 and then Limited_Present (Parent (Def))
22125 Set_Is_Limited_Record (T);
22128 -- If the component list of a record type is defined by the reserved
22129 -- word null and there is no discriminant part, then the record type has
22130 -- no components and all records of the type are null records (RM 3.7)
22131 -- This procedure is also called to process the extension part of a
22132 -- record extension, in which case the current scope may have inherited
22136 or else No (Component_List (Def))
22137 or else Null_Present (Component_List (Def))
22139 if not Is_Tagged_Type (T) then
22140 Check_SPARK_05_Restriction ("untagged record cannot be null", Def);
22144 Analyze_Declarations (Component_Items (Component_List (Def)));
22146 if Present (Variant_Part (Component_List (Def))) then
22147 Check_SPARK_05_Restriction ("variant part is not allowed", Def);
22148 Analyze (Variant_Part (Component_List (Def)));
22152 -- After completing the semantic analysis of the record definition,
22153 -- record components, both new and inherited, are accessible. Set their
22154 -- kind accordingly. Exclude malformed itypes from illegal declarations,
22155 -- whose Ekind may be void.
22157 Component := First_Entity (Current_Scope);
22158 while Present (Component) loop
22159 if Ekind (Component) = E_Void
22160 and then not Is_Itype (Component)
22162 Set_Ekind (Component, E_Component);
22163 Init_Component_Location (Component);
22166 Propagate_Concurrent_Flags (T, Etype (Component));
22168 if Ekind (Component) /= E_Component then
22171 -- Do not set Has_Controlled_Component on a class-wide equivalent
22172 -- type. See Make_CW_Equivalent_Type.
22174 elsif not Is_Class_Wide_Equivalent_Type (T)
22175 and then (Has_Controlled_Component (Etype (Component))
22176 or else (Chars (Component) /= Name_uParent
22177 and then Is_Controlled (Etype (Component))))
22179 Set_Has_Controlled_Component (T, True);
22180 Final_Storage_Only :=
22182 and then Finalize_Storage_Only (Etype (Component));
22183 Ctrl_Components := True;
22186 Next_Entity (Component);
22189 -- A Type is Finalize_Storage_Only only if all its controlled components
22192 if Ctrl_Components then
22193 Set_Finalize_Storage_Only (T, Final_Storage_Only);
22196 -- Place reference to end record on the proper entity, which may
22197 -- be a partial view.
22199 if Present (Def) then
22200 Process_End_Label (Def, 'e', Prev_T);
22202 end Record_Type_Definition;
22204 ------------------------
22205 -- Replace_Components --
22206 ------------------------
22208 procedure Replace_Components (Typ : Entity_Id; Decl : Node_Id) is
22209 function Process (N : Node_Id) return Traverse_Result;
22215 function Process (N : Node_Id) return Traverse_Result is
22219 if Nkind (N) = N_Discriminant_Specification then
22220 Comp := First_Discriminant (Typ);
22221 while Present (Comp) loop
22222 if Chars (Comp) = Chars (Defining_Identifier (N)) then
22223 Set_Defining_Identifier (N, Comp);
22227 Next_Discriminant (Comp);
22230 elsif Nkind (N) = N_Variant_Part then
22231 Comp := First_Discriminant (Typ);
22232 while Present (Comp) loop
22233 if Chars (Comp) = Chars (Name (N)) then
22234 Set_Entity (Name (N), Comp);
22238 Next_Discriminant (Comp);
22241 elsif Nkind (N) = N_Component_Declaration then
22242 Comp := First_Component (Typ);
22243 while Present (Comp) loop
22244 if Chars (Comp) = Chars (Defining_Identifier (N)) then
22245 Set_Defining_Identifier (N, Comp);
22249 Next_Component (Comp);
22256 procedure Replace is new Traverse_Proc (Process);
22258 -- Start of processing for Replace_Components
22262 end Replace_Components;
22264 -------------------------------
22265 -- Set_Completion_Referenced --
22266 -------------------------------
22268 procedure Set_Completion_Referenced (E : Entity_Id) is
22270 -- If in main unit, mark entity that is a completion as referenced,
22271 -- warnings go on the partial view when needed.
22273 if In_Extended_Main_Source_Unit (E) then
22274 Set_Referenced (E);
22276 end Set_Completion_Referenced;
22278 ---------------------
22279 -- Set_Default_SSO --
22280 ---------------------
22282 procedure Set_Default_SSO (T : Entity_Id) is
22284 case Opt.Default_SSO is
22288 Set_SSO_Set_Low_By_Default (T, True);
22290 Set_SSO_Set_High_By_Default (T, True);
22292 raise Program_Error;
22294 end Set_Default_SSO;
22296 ---------------------
22297 -- Set_Fixed_Range --
22298 ---------------------
22300 -- The range for fixed-point types is complicated by the fact that we
22301 -- do not know the exact end points at the time of the declaration. This
22302 -- is true for three reasons:
22304 -- A size clause may affect the fudging of the end-points.
22305 -- A small clause may affect the values of the end-points.
22306 -- We try to include the end-points if it does not affect the size.
22308 -- This means that the actual end-points must be established at the
22309 -- point when the type is frozen. Meanwhile, we first narrow the range
22310 -- as permitted (so that it will fit if necessary in a small specified
22311 -- size), and then build a range subtree with these narrowed bounds.
22312 -- Set_Fixed_Range constructs the range from real literal values, and
22313 -- sets the range as the Scalar_Range of the given fixed-point type entity.
22315 -- The parent of this range is set to point to the entity so that it is
22316 -- properly hooked into the tree (unlike normal Scalar_Range entries for
22317 -- other scalar types, which are just pointers to the range in the
22318 -- original tree, this would otherwise be an orphan).
22320 -- The tree is left unanalyzed. When the type is frozen, the processing
22321 -- in Freeze.Freeze_Fixed_Point_Type notices that the range is not
22322 -- analyzed, and uses this as an indication that it should complete
22323 -- work on the range (it will know the final small and size values).
22325 procedure Set_Fixed_Range
22331 S : constant Node_Id :=
22333 Low_Bound => Make_Real_Literal (Loc, Lo),
22334 High_Bound => Make_Real_Literal (Loc, Hi));
22336 Set_Scalar_Range (E, S);
22339 -- Before the freeze point, the bounds of a fixed point are universal
22340 -- and carry the corresponding type.
22342 Set_Etype (Low_Bound (S), Universal_Real);
22343 Set_Etype (High_Bound (S), Universal_Real);
22344 end Set_Fixed_Range;
22346 ----------------------------------
22347 -- Set_Scalar_Range_For_Subtype --
22348 ----------------------------------
22350 procedure Set_Scalar_Range_For_Subtype
22351 (Def_Id : Entity_Id;
22355 Kind : constant Entity_Kind := Ekind (Def_Id);
22358 -- Defend against previous error
22360 if Nkind (R) = N_Error then
22364 Set_Scalar_Range (Def_Id, R);
22366 -- We need to link the range into the tree before resolving it so
22367 -- that types that are referenced, including importantly the subtype
22368 -- itself, are properly frozen (Freeze_Expression requires that the
22369 -- expression be properly linked into the tree). Of course if it is
22370 -- already linked in, then we do not disturb the current link.
22372 if No (Parent (R)) then
22373 Set_Parent (R, Def_Id);
22376 -- Reset the kind of the subtype during analysis of the range, to
22377 -- catch possible premature use in the bounds themselves.
22379 Set_Ekind (Def_Id, E_Void);
22380 Process_Range_Expr_In_Decl (R, Subt, Subtyp => Def_Id);
22381 Set_Ekind (Def_Id, Kind);
22382 end Set_Scalar_Range_For_Subtype;
22384 --------------------------------------------------------
22385 -- Set_Stored_Constraint_From_Discriminant_Constraint --
22386 --------------------------------------------------------
22388 procedure Set_Stored_Constraint_From_Discriminant_Constraint
22392 -- Make sure set if encountered during Expand_To_Stored_Constraint
22394 Set_Stored_Constraint (E, No_Elist);
22396 -- Give it the right value
22398 if Is_Constrained (E) and then Has_Discriminants (E) then
22399 Set_Stored_Constraint (E,
22400 Expand_To_Stored_Constraint (E, Discriminant_Constraint (E)));
22402 end Set_Stored_Constraint_From_Discriminant_Constraint;
22404 -------------------------------------
22405 -- Signed_Integer_Type_Declaration --
22406 -------------------------------------
22408 procedure Signed_Integer_Type_Declaration (T : Entity_Id; Def : Node_Id) is
22409 Implicit_Base : Entity_Id;
22410 Base_Typ : Entity_Id;
22413 Errs : Boolean := False;
22417 function Can_Derive_From (E : Entity_Id) return Boolean;
22418 -- Determine whether given bounds allow derivation from specified type
22420 procedure Check_Bound (Expr : Node_Id);
22421 -- Check bound to make sure it is integral and static. If not, post
22422 -- appropriate error message and set Errs flag
22424 ---------------------
22425 -- Can_Derive_From --
22426 ---------------------
22428 -- Note we check both bounds against both end values, to deal with
22429 -- strange types like ones with a range of 0 .. -12341234.
22431 function Can_Derive_From (E : Entity_Id) return Boolean is
22432 Lo : constant Uint := Expr_Value (Type_Low_Bound (E));
22433 Hi : constant Uint := Expr_Value (Type_High_Bound (E));
22435 return Lo <= Lo_Val and then Lo_Val <= Hi
22437 Lo <= Hi_Val and then Hi_Val <= Hi;
22438 end Can_Derive_From;
22444 procedure Check_Bound (Expr : Node_Id) is
22446 -- If a range constraint is used as an integer type definition, each
22447 -- bound of the range must be defined by a static expression of some
22448 -- integer type, but the two bounds need not have the same integer
22449 -- type (Negative bounds are allowed.) (RM 3.5.4)
22451 if not Is_Integer_Type (Etype (Expr)) then
22453 ("integer type definition bounds must be of integer type", Expr);
22456 elsif not Is_OK_Static_Expression (Expr) then
22457 Flag_Non_Static_Expr
22458 ("non-static expression used for integer type bound!", Expr);
22461 -- The bounds are folded into literals, and we set their type to be
22462 -- universal, to avoid typing difficulties: we cannot set the type
22463 -- of the literal to the new type, because this would be a forward
22464 -- reference for the back end, and if the original type is user-
22465 -- defined this can lead to spurious semantic errors (e.g. 2928-003).
22468 if Is_Entity_Name (Expr) then
22469 Fold_Uint (Expr, Expr_Value (Expr), True);
22472 Set_Etype (Expr, Universal_Integer);
22476 -- Start of processing for Signed_Integer_Type_Declaration
22479 -- Create an anonymous base type
22482 Create_Itype (E_Signed_Integer_Type, Parent (Def), T, 'B');
22484 -- Analyze and check the bounds, they can be of any integer type
22486 Lo := Low_Bound (Def);
22487 Hi := High_Bound (Def);
22489 -- Arbitrarily use Integer as the type if either bound had an error
22491 if Hi = Error or else Lo = Error then
22492 Base_Typ := Any_Integer;
22493 Set_Error_Posted (T, True);
22495 -- Here both bounds are OK expressions
22498 Analyze_And_Resolve (Lo, Any_Integer);
22499 Analyze_And_Resolve (Hi, Any_Integer);
22505 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22506 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22509 -- Find type to derive from
22511 Lo_Val := Expr_Value (Lo);
22512 Hi_Val := Expr_Value (Hi);
22514 if Can_Derive_From (Standard_Short_Short_Integer) then
22515 Base_Typ := Base_Type (Standard_Short_Short_Integer);
22517 elsif Can_Derive_From (Standard_Short_Integer) then
22518 Base_Typ := Base_Type (Standard_Short_Integer);
22520 elsif Can_Derive_From (Standard_Integer) then
22521 Base_Typ := Base_Type (Standard_Integer);
22523 elsif Can_Derive_From (Standard_Long_Integer) then
22524 Base_Typ := Base_Type (Standard_Long_Integer);
22526 elsif Can_Derive_From (Standard_Long_Long_Integer) then
22527 Check_Restriction (No_Long_Long_Integers, Def);
22528 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22531 Base_Typ := Base_Type (Standard_Long_Long_Integer);
22532 Error_Msg_N ("integer type definition bounds out of range", Def);
22533 Hi := Type_High_Bound (Standard_Long_Long_Integer);
22534 Lo := Type_Low_Bound (Standard_Long_Long_Integer);
22538 -- Complete both implicit base and declared first subtype entities. The
22539 -- inheritance of the rep item chain ensures that SPARK-related pragmas
22540 -- are not clobbered when the signed integer type acts as a full view of
22543 Set_Etype (Implicit_Base, Base_Typ);
22544 Set_Size_Info (Implicit_Base, Base_Typ);
22545 Set_RM_Size (Implicit_Base, RM_Size (Base_Typ));
22546 Set_First_Rep_Item (Implicit_Base, First_Rep_Item (Base_Typ));
22547 Set_Scalar_Range (Implicit_Base, Scalar_Range (Base_Typ));
22549 Set_Ekind (T, E_Signed_Integer_Subtype);
22550 Set_Etype (T, Implicit_Base);
22551 Set_Size_Info (T, Implicit_Base);
22552 Inherit_Rep_Item_Chain (T, Implicit_Base);
22553 Set_Scalar_Range (T, Def);
22554 Set_RM_Size (T, UI_From_Int (Minimum_Size (T)));
22555 Set_Is_Constrained (T);
22556 end Signed_Integer_Type_Declaration;